]> andersk Git - openssh.git/blame - sftp-client.c
- stevesk@cvs.openbsd.org 2006/07/11 20:07:25
[openssh.git] / sftp-client.c
CommitLineData
028094f4 1/* $OpenBSD: sftp-client.c,v 1.67 2006/07/11 20:07:25 stevesk Exp $ */
61e96248 2/*
ab3932ab 3 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
61e96248 4 *
ab3932ab 5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
61e96248 8 *
ab3932ab 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
61e96248 16 */
17
18/* XXX: memleaks */
19/* XXX: signed vs unsigned */
22e6c827 20/* XXX: remove all logging, only return status codes */
61e96248 21/* XXX: copy between two remote sites */
22
23#include "includes.h"
4095f623 24
25#include <sys/types.h>
26#ifdef HAVE_SYS_STAT_H
27# include <sys/stat.h>
28#endif
d3221cca 29
028094f4 30#include <errno.h>
d3221cca 31#include <fcntl.h>
32#include <signal.h>
c25d3df7 33
9fd2a215 34#include "openbsd-compat/sys-queue.h"
61e96248 35
61e96248 36#include "buffer.h"
37#include "bufaux.h"
61e96248 38#include "xmalloc.h"
39#include "log.h"
40#include "atomicio.h"
b65c3807 41#include "progressmeter.h"
51e7a012 42#include "misc.h"
61e96248 43
44#include "sftp.h"
45#include "sftp-common.h"
46#include "sftp-client.h"
47
0e5de6f8 48extern volatile sig_atomic_t interrupted;
b65c3807 49extern int showprogress;
50
2db34ac9 51/* Minimum amount of data to read at a time */
c25d3df7 52#define MIN_READ_SIZE 512
53
22e6c827 54struct sftp_conn {
55 int fd_in;
56 int fd_out;
57 u_int transfer_buflen;
58 u_int num_requests;
59 u_int version;
60 u_int msg_id;
61};
9c5a8165 62
396c147e 63static void
61e96248 64send_msg(int fd, Buffer *m)
65{
bf0bf24b 66 u_char mlen[4];
2c4369de 67 struct iovec iov[2];
bf0bf24b 68
3eee3b86 69 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
bf0bf24b 70 fatal("Outbound message too long %u", buffer_len(m));
61e96248 71
bf0bf24b 72 /* Send length first */
51e7a012 73 put_u32(mlen, buffer_len(m));
2c4369de 74 iov[0].iov_base = mlen;
75 iov[0].iov_len = sizeof(mlen);
76 iov[1].iov_base = buffer_ptr(m);
77 iov[1].iov_len = buffer_len(m);
78
79 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
61e96248 80 fatal("Couldn't send packet: %s", strerror(errno));
81
bf0bf24b 82 buffer_clear(m);
61e96248 83}
84
396c147e 85static void
61e96248 86get_msg(int fd, Buffer *m)
87{
bf0bf24b 88 u_int msg_len;
61e96248 89
bf0bf24b 90 buffer_append_space(m, 4);
05624c18 91 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
92 if (errno == EPIPE)
93 fatal("Connection closed");
94 else
95 fatal("Couldn't read packet: %s", strerror(errno));
96 }
61e96248 97
bf0bf24b 98 msg_len = buffer_get_int(m);
3eee3b86 99 if (msg_len > SFTP_MAX_MSG_LENGTH)
9906a836 100 fatal("Received message too long %u", msg_len);
61e96248 101
bf0bf24b 102 buffer_append_space(m, msg_len);
05624c18 103 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
104 if (errno == EPIPE)
105 fatal("Connection closed");
106 else
107 fatal("Read packet: %s", strerror(errno));
108 }
61e96248 109}
110
396c147e 111static void
61e96248 112send_string_request(int fd, u_int id, u_int code, char *s,
113 u_int len)
114{
115 Buffer msg;
116
117 buffer_init(&msg);
118 buffer_put_char(&msg, code);
119 buffer_put_int(&msg, id);
120 buffer_put_string(&msg, s, len);
121 send_msg(fd, &msg);
9906a836 122 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
61e96248 123 buffer_free(&msg);
124}
125
396c147e 126static void
61e96248 127send_string_attrs_request(int fd, u_int id, u_int code, char *s,
128 u_int len, Attrib *a)
129{
130 Buffer msg;
131
132 buffer_init(&msg);
133 buffer_put_char(&msg, code);
134 buffer_put_int(&msg, id);
135 buffer_put_string(&msg, s, len);
136 encode_attrib(&msg, a);
137 send_msg(fd, &msg);
9906a836 138 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
61e96248 139 buffer_free(&msg);
140}
141
396c147e 142static u_int
9906a836 143get_status(int fd, u_int expected_id)
61e96248 144{
145 Buffer msg;
146 u_int type, id, status;
147
148 buffer_init(&msg);
149 get_msg(fd, &msg);
150 type = buffer_get_char(&msg);
151 id = buffer_get_int(&msg);
152
153 if (id != expected_id)
9906a836 154 fatal("ID mismatch (%u != %u)", id, expected_id);
61e96248 155 if (type != SSH2_FXP_STATUS)
9906a836 156 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
61e96248 157 SSH2_FXP_STATUS, type);
158
159 status = buffer_get_int(&msg);
160 buffer_free(&msg);
161
9906a836 162 debug3("SSH2_FXP_STATUS %u", status);
61e96248 163
164 return(status);
165}
166
396c147e 167static char *
61e96248 168get_handle(int fd, u_int expected_id, u_int *len)
169{
170 Buffer msg;
171 u_int type, id;
172 char *handle;
173
174 buffer_init(&msg);
175 get_msg(fd, &msg);
176 type = buffer_get_char(&msg);
177 id = buffer_get_int(&msg);
178
179 if (id != expected_id)
9906a836 180 fatal("ID mismatch (%u != %u)", id, expected_id);
61e96248 181 if (type == SSH2_FXP_STATUS) {
182 int status = buffer_get_int(&msg);
183
184 error("Couldn't get handle: %s", fx2txt(status));
aa41be57 185 buffer_free(&msg);
61e96248 186 return(NULL);
187 } else if (type != SSH2_FXP_HANDLE)
9906a836 188 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
61e96248 189 SSH2_FXP_HANDLE, type);
190
191 handle = buffer_get_string(&msg, len);
192 buffer_free(&msg);
193
194 return(handle);
195}
196
396c147e 197static Attrib *
d50d9b63 198get_decode_stat(int fd, u_int expected_id, int quiet)
61e96248 199{
200 Buffer msg;
201 u_int type, id;
202 Attrib *a;
203
204 buffer_init(&msg);
205 get_msg(fd, &msg);
206
207 type = buffer_get_char(&msg);
208 id = buffer_get_int(&msg);
209
9906a836 210 debug3("Received stat reply T:%u I:%u", type, id);
61e96248 211 if (id != expected_id)
9906a836 212 fatal("ID mismatch (%u != %u)", id, expected_id);
61e96248 213 if (type == SSH2_FXP_STATUS) {
214 int status = buffer_get_int(&msg);
215
d50d9b63 216 if (quiet)
217 debug("Couldn't stat remote file: %s", fx2txt(status));
218 else
219 error("Couldn't stat remote file: %s", fx2txt(status));
aa41be57 220 buffer_free(&msg);
61e96248 221 return(NULL);
222 } else if (type != SSH2_FXP_ATTRS) {
9906a836 223 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
61e96248 224 SSH2_FXP_ATTRS, type);
225 }
226 a = decode_attrib(&msg);
227 buffer_free(&msg);
228
229 return(a);
230}
231
22e6c827 232struct sftp_conn *
233do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
61e96248 234{
9906a836 235 u_int type;
236 int version;
61e96248 237 Buffer msg;
22e6c827 238 struct sftp_conn *ret;
61e96248 239
240 buffer_init(&msg);
241 buffer_put_char(&msg, SSH2_FXP_INIT);
242 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
243 send_msg(fd_out, &msg);
244
245 buffer_clear(&msg);
246
247 get_msg(fd_in, &msg);
248
2b87da3b 249 /* Expecting a VERSION reply */
61e96248 250 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
9906a836 251 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
61e96248 252 type);
253 buffer_free(&msg);
22e6c827 254 return(NULL);
61e96248 255 }
256 version = buffer_get_int(&msg);
257
258 debug2("Remote version: %d", version);
259
260 /* Check for extensions */
261 while (buffer_len(&msg) > 0) {
262 char *name = buffer_get_string(&msg, NULL);
263 char *value = buffer_get_string(&msg, NULL);
264
265 debug2("Init extension: \"%s\"", name);
266 xfree(name);
267 xfree(value);
268 }
269
270 buffer_free(&msg);
3a7fe5ba 271
22e6c827 272 ret = xmalloc(sizeof(*ret));
273 ret->fd_in = fd_in;
274 ret->fd_out = fd_out;
275 ret->transfer_buflen = transfer_buflen;
276 ret->num_requests = num_requests;
277 ret->version = version;
278 ret->msg_id = 1;
279
280 /* Some filexfer v.0 servers don't support large packets */
281 if (version == 0)
92053302 282 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
22e6c827 283
284 return(ret);
285}
286
287u_int
288sftp_proto_version(struct sftp_conn *conn)
289{
290 return(conn->version);
61e96248 291}
292
293int
22e6c827 294do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
61e96248 295{
296 u_int id, status;
297 Buffer msg;
298
299 buffer_init(&msg);
300
22e6c827 301 id = conn->msg_id++;
61e96248 302 buffer_put_char(&msg, SSH2_FXP_CLOSE);
303 buffer_put_int(&msg, id);
304 buffer_put_string(&msg, handle, handle_len);
22e6c827 305 send_msg(conn->fd_out, &msg);
9906a836 306 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
61e96248 307
22e6c827 308 status = get_status(conn->fd_in, id);
61e96248 309 if (status != SSH2_FX_OK)
310 error("Couldn't close file: %s", fx2txt(status));
311
312 buffer_free(&msg);
313
314 return(status);
315}
316
2e4fb373 317
396c147e 318static int
22e6c827 319do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
2e4fb373 320 SFTP_DIRENT ***dir)
61e96248 321{
322 Buffer msg;
2ceb8101 323 u_int count, type, id, handle_len, i, expected_id, ents = 0;
61e96248 324 char *handle;
325
22e6c827 326 id = conn->msg_id++;
61e96248 327
328 buffer_init(&msg);
329 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
330 buffer_put_int(&msg, id);
331 buffer_put_cstring(&msg, path);
22e6c827 332 send_msg(conn->fd_out, &msg);
61e96248 333
334 buffer_clear(&msg);
335
22e6c827 336 handle = get_handle(conn->fd_in, id, &handle_len);
61e96248 337 if (handle == NULL)
338 return(-1);
339
2e4fb373 340 if (dir) {
341 ents = 0;
342 *dir = xmalloc(sizeof(**dir));
343 (*dir)[0] = NULL;
344 }
2e4fb373 345
0e5de6f8 346 for (; !interrupted;) {
22e6c827 347 id = expected_id = conn->msg_id++;
61e96248 348
9906a836 349 debug3("Sending SSH2_FXP_READDIR I:%u", id);
61e96248 350
351 buffer_clear(&msg);
352 buffer_put_char(&msg, SSH2_FXP_READDIR);
353 buffer_put_int(&msg, id);
354 buffer_put_string(&msg, handle, handle_len);
22e6c827 355 send_msg(conn->fd_out, &msg);
61e96248 356
357 buffer_clear(&msg);
358
22e6c827 359 get_msg(conn->fd_in, &msg);
61e96248 360
361 type = buffer_get_char(&msg);
362 id = buffer_get_int(&msg);
363
9906a836 364 debug3("Received reply T:%u I:%u", type, id);
61e96248 365
366 if (id != expected_id)
9906a836 367 fatal("ID mismatch (%u != %u)", id, expected_id);
61e96248 368
369 if (type == SSH2_FXP_STATUS) {
370 int status = buffer_get_int(&msg);
371
372 debug3("Received SSH2_FXP_STATUS %d", status);
373
374 if (status == SSH2_FX_EOF) {
375 break;
376 } else {
377 error("Couldn't read directory: %s",
378 fx2txt(status));
22e6c827 379 do_close(conn, handle, handle_len);
3e2f2431 380 xfree(handle);
b655a207 381 return(status);
61e96248 382 }
383 } else if (type != SSH2_FXP_NAME)
9906a836 384 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
61e96248 385 SSH2_FXP_NAME, type);
386
387 count = buffer_get_int(&msg);
0426a3b4 388 if (count == 0)
389 break;
390 debug3("Received %d SSH2_FXP_NAME responses", count);
184eed6a 391 for (i = 0; i < count; i++) {
61e96248 392 char *filename, *longname;
393 Attrib *a;
394
395 filename = buffer_get_string(&msg, NULL);
396 longname = buffer_get_string(&msg, NULL);
397 a = decode_attrib(&msg);
398
2e4fb373 399 if (printflag)
400 printf("%s\n", longname);
401
402 if (dir) {
c5d10563 403 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
2e4fb373 404 (*dir)[ents] = xmalloc(sizeof(***dir));
405 (*dir)[ents]->filename = xstrdup(filename);
406 (*dir)[ents]->longname = xstrdup(longname);
407 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
408 (*dir)[++ents] = NULL;
409 }
61e96248 410
411 xfree(filename);
412 xfree(longname);
413 }
414 }
415
416 buffer_free(&msg);
22e6c827 417 do_close(conn, handle, handle_len);
61e96248 418 xfree(handle);
419
0e5de6f8 420 /* Don't return partial matches on interrupt */
421 if (interrupted && dir != NULL && *dir != NULL) {
422 free_sftp_dirents(*dir);
423 *dir = xmalloc(sizeof(**dir));
424 **dir = NULL;
425 }
426
61e96248 427 return(0);
428}
429
2e4fb373 430int
22e6c827 431do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
2e4fb373 432{
22e6c827 433 return(do_lsreaddir(conn, path, 0, dir));
2e4fb373 434}
435
436void free_sftp_dirents(SFTP_DIRENT **s)
437{
438 int i;
184eed6a 439
440 for (i = 0; s[i]; i++) {
2e4fb373 441 xfree(s[i]->filename);
442 xfree(s[i]->longname);
443 xfree(s[i]);
444 }
445 xfree(s);
446}
447
61e96248 448int
22e6c827 449do_rm(struct sftp_conn *conn, char *path)
61e96248 450{
451 u_int status, id;
452
453 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
454
22e6c827 455 id = conn->msg_id++;
762715ce 456 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
22e6c827 457 strlen(path));
458 status = get_status(conn->fd_in, id);
61e96248 459 if (status != SSH2_FX_OK)
460 error("Couldn't delete file: %s", fx2txt(status));
461 return(status);
462}
463
464int
22e6c827 465do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
61e96248 466{
467 u_int status, id;
468
22e6c827 469 id = conn->msg_id++;
470 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
61e96248 471 strlen(path), a);
472
22e6c827 473 status = get_status(conn->fd_in, id);
61e96248 474 if (status != SSH2_FX_OK)
475 error("Couldn't create directory: %s", fx2txt(status));
476
477 return(status);
478}
479
480int
22e6c827 481do_rmdir(struct sftp_conn *conn, char *path)
61e96248 482{
483 u_int status, id;
484
22e6c827 485 id = conn->msg_id++;
486 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
487 strlen(path));
61e96248 488
22e6c827 489 status = get_status(conn->fd_in, id);
61e96248 490 if (status != SSH2_FX_OK)
491 error("Couldn't remove directory: %s", fx2txt(status));
492
493 return(status);
494}
495
496Attrib *
22e6c827 497do_stat(struct sftp_conn *conn, char *path, int quiet)
61e96248 498{
499 u_int id;
500
22e6c827 501 id = conn->msg_id++;
502
762715ce 503 send_string_request(conn->fd_out, id,
504 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
22e6c827 505 path, strlen(path));
506
507 return(get_decode_stat(conn->fd_in, id, quiet));
61e96248 508}
509
510Attrib *
22e6c827 511do_lstat(struct sftp_conn *conn, char *path, int quiet)
61e96248 512{
513 u_int id;
514
22e6c827 515 if (conn->version == 0) {
516 if (quiet)
517 debug("Server version does not support lstat operation");
518 else
bbe88b6d 519 logit("Server version does not support lstat operation");
9c74a24d 520 return(do_stat(conn, path, quiet));
22e6c827 521 }
522
523 id = conn->msg_id++;
524 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
525 strlen(path));
526
527 return(get_decode_stat(conn->fd_in, id, quiet));
61e96248 528}
529
530Attrib *
22e6c827 531do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
61e96248 532{
533 u_int id;
534
22e6c827 535 id = conn->msg_id++;
536 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
537 handle_len);
538
539 return(get_decode_stat(conn->fd_in, id, quiet));
61e96248 540}
541
542int
22e6c827 543do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
61e96248 544{
545 u_int status, id;
546
22e6c827 547 id = conn->msg_id++;
548 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
61e96248 549 strlen(path), a);
550
22e6c827 551 status = get_status(conn->fd_in, id);
61e96248 552 if (status != SSH2_FX_OK)
553 error("Couldn't setstat on \"%s\": %s", path,
554 fx2txt(status));
555
556 return(status);
557}
558
559int
22e6c827 560do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
61e96248 561 Attrib *a)
562{
563 u_int status, id;
564
22e6c827 565 id = conn->msg_id++;
566 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
61e96248 567 handle_len, a);
568
22e6c827 569 status = get_status(conn->fd_in, id);
61e96248 570 if (status != SSH2_FX_OK)
571 error("Couldn't fsetstat: %s", fx2txt(status));
572
573 return(status);
574}
575
576char *
22e6c827 577do_realpath(struct sftp_conn *conn, char *path)
61e96248 578{
579 Buffer msg;
580 u_int type, expected_id, count, id;
581 char *filename, *longname;
582 Attrib *a;
583
22e6c827 584 expected_id = id = conn->msg_id++;
585 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
586 strlen(path));
61e96248 587
588 buffer_init(&msg);
589
22e6c827 590 get_msg(conn->fd_in, &msg);
61e96248 591 type = buffer_get_char(&msg);
592 id = buffer_get_int(&msg);
593
594 if (id != expected_id)
9906a836 595 fatal("ID mismatch (%u != %u)", id, expected_id);
61e96248 596
597 if (type == SSH2_FXP_STATUS) {
598 u_int status = buffer_get_int(&msg);
599
600 error("Couldn't canonicalise: %s", fx2txt(status));
601 return(NULL);
602 } else if (type != SSH2_FXP_NAME)
9906a836 603 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
61e96248 604 SSH2_FXP_NAME, type);
605
606 count = buffer_get_int(&msg);
607 if (count != 1)
608 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
609
610 filename = buffer_get_string(&msg, NULL);
611 longname = buffer_get_string(&msg, NULL);
612 a = decode_attrib(&msg);
613
614 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
615
616 xfree(longname);
617
618 buffer_free(&msg);
619
620 return(filename);
621}
622
623int
22e6c827 624do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
61e96248 625{
626 Buffer msg;
627 u_int status, id;
628
629 buffer_init(&msg);
630
631 /* Send rename request */
22e6c827 632 id = conn->msg_id++;
61e96248 633 buffer_put_char(&msg, SSH2_FXP_RENAME);
634 buffer_put_int(&msg, id);
635 buffer_put_cstring(&msg, oldpath);
636 buffer_put_cstring(&msg, newpath);
22e6c827 637 send_msg(conn->fd_out, &msg);
61e96248 638 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
639 newpath);
640 buffer_free(&msg);
641
22e6c827 642 status = get_status(conn->fd_in, id);
61e96248 643 if (status != SSH2_FX_OK)
22e6c827 644 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
645 newpath, fx2txt(status));
61e96248 646
647 return(status);
648}
649
3a7fe5ba 650int
22e6c827 651do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
3a7fe5ba 652{
653 Buffer msg;
654 u_int status, id;
655
22e6c827 656 if (conn->version < 3) {
657 error("This server does not support the symlink operation");
658 return(SSH2_FX_OP_UNSUPPORTED);
659 }
660
3a7fe5ba 661 buffer_init(&msg);
662
b093b499 663 /* Send symlink request */
22e6c827 664 id = conn->msg_id++;
3a7fe5ba 665 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
666 buffer_put_int(&msg, id);
667 buffer_put_cstring(&msg, oldpath);
668 buffer_put_cstring(&msg, newpath);
22e6c827 669 send_msg(conn->fd_out, &msg);
3a7fe5ba 670 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
671 newpath);
672 buffer_free(&msg);
673
22e6c827 674 status = get_status(conn->fd_in, id);
3a7fe5ba 675 if (status != SSH2_FX_OK)
9db1a8e9 676 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
22e6c827 677 newpath, fx2txt(status));
3a7fe5ba 678
679 return(status);
680}
681
682char *
22e6c827 683do_readlink(struct sftp_conn *conn, char *path)
3a7fe5ba 684{
685 Buffer msg;
686 u_int type, expected_id, count, id;
687 char *filename, *longname;
688 Attrib *a;
689
22e6c827 690 expected_id = id = conn->msg_id++;
691 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
692 strlen(path));
3a7fe5ba 693
694 buffer_init(&msg);
695
22e6c827 696 get_msg(conn->fd_in, &msg);
3a7fe5ba 697 type = buffer_get_char(&msg);
698 id = buffer_get_int(&msg);
699
700 if (id != expected_id)
9906a836 701 fatal("ID mismatch (%u != %u)", id, expected_id);
3a7fe5ba 702
703 if (type == SSH2_FXP_STATUS) {
704 u_int status = buffer_get_int(&msg);
705
706 error("Couldn't readlink: %s", fx2txt(status));
707 return(NULL);
708 } else if (type != SSH2_FXP_NAME)
9906a836 709 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
3a7fe5ba 710 SSH2_FXP_NAME, type);
711
712 count = buffer_get_int(&msg);
713 if (count != 1)
714 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
715
716 filename = buffer_get_string(&msg, NULL);
717 longname = buffer_get_string(&msg, NULL);
718 a = decode_attrib(&msg);
719
720 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
721
722 xfree(longname);
723
724 buffer_free(&msg);
725
726 return(filename);
727}
728
c25d3df7 729static void
730send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
731 char *handle, u_int handle_len)
732{
733 Buffer msg;
762715ce 734
c25d3df7 735 buffer_init(&msg);
736 buffer_clear(&msg);
737 buffer_put_char(&msg, SSH2_FXP_READ);
738 buffer_put_int(&msg, id);
739 buffer_put_string(&msg, handle, handle_len);
740 buffer_put_int64(&msg, offset);
741 buffer_put_int(&msg, len);
742 send_msg(fd_out, &msg);
743 buffer_free(&msg);
762715ce 744}
c25d3df7 745
61e96248 746int
22e6c827 747do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
748 int pflag)
61e96248 749{
61e96248 750 Attrib junk, *a;
c25d3df7 751 Buffer msg;
752 char *handle;
a056dfa2 753 int local_fd, status = 0, write_error;
c25d3df7 754 int read_error, write_errno;
755 u_int64_t offset, size;
2ceb8101 756 u_int handle_len, mode, type, id, buflen, num_req, max_req;
b65c3807 757 off_t progress_counter;
c25d3df7 758 struct request {
759 u_int id;
760 u_int len;
761 u_int64_t offset;
762715ce 762 TAILQ_ENTRY(request) tq;
c25d3df7 763 };
764 TAILQ_HEAD(reqhead, request) requests;
765 struct request *req;
766
767 TAILQ_INIT(&requests);
61e96248 768
22e6c827 769 a = do_stat(conn, remote_path, 0);
61e96248 770 if (a == NULL)
771 return(-1);
772
773 /* XXX: should we preserve set[ug]id? */
774 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
bfd934b9 775 mode = a->perm & 0777;
61e96248 776 else
777 mode = 0666;
778
d50d9b63 779 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
538840a2 780 (!S_ISREG(a->perm))) {
781 error("Cannot download non-regular file: %s", remote_path);
d50d9b63 782 return(-1);
783 }
784
c25d3df7 785 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
786 size = a->size;
787 else
788 size = 0;
789
22e6c827 790 buflen = conn->transfer_buflen;
61e96248 791 buffer_init(&msg);
792
793 /* Send open request */
22e6c827 794 id = conn->msg_id++;
61e96248 795 buffer_put_char(&msg, SSH2_FXP_OPEN);
796 buffer_put_int(&msg, id);
797 buffer_put_cstring(&msg, remote_path);
798 buffer_put_int(&msg, SSH2_FXF_READ);
799 attrib_clear(&junk); /* Send empty attributes */
800 encode_attrib(&msg, &junk);
22e6c827 801 send_msg(conn->fd_out, &msg);
9906a836 802 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
61e96248 803
22e6c827 804 handle = get_handle(conn->fd_in, id, &handle_len);
61e96248 805 if (handle == NULL) {
806 buffer_free(&msg);
61e96248 807 return(-1);
808 }
809
aff51935 810 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
bfd934b9 811 mode | S_IWRITE);
22e6c827 812 if (local_fd == -1) {
813 error("Couldn't open local file \"%s\" for writing: %s",
814 local_path, strerror(errno));
f60ace9f 815 buffer_free(&msg);
816 xfree(handle);
22e6c827 817 return(-1);
818 }
819
61e96248 820 /* Read from remote and write to local */
c25d3df7 821 write_error = read_error = write_errno = num_req = offset = 0;
822 max_req = 1;
b65c3807 823 progress_counter = 0;
824
213bab61 825 if (showprogress && size != 0)
826 start_progress_meter(remote_path, size, &progress_counter);
b65c3807 827
c25d3df7 828 while (num_req > 0 || max_req > 0) {
61e96248 829 char *data;
c25d3df7 830 u_int len;
61e96248 831
0e5de6f8 832 /*
f2107e97 833 * Simulate EOF on interrupt: stop sending new requests and
0e5de6f8 834 * allow outstanding requests to drain gracefully
835 */
836 if (interrupted) {
837 if (num_req == 0) /* If we haven't started yet... */
838 break;
839 max_req = 0;
840 }
841
c25d3df7 842 /* Send some more requests */
843 while (num_req < max_req) {
762715ce 844 debug3("Request range %llu -> %llu (%d/%d)",
8627f3e0 845 (unsigned long long)offset,
846 (unsigned long long)offset + buflen - 1,
847 num_req, max_req);
c25d3df7 848 req = xmalloc(sizeof(*req));
22e6c827 849 req->id = conn->msg_id++;
c25d3df7 850 req->len = buflen;
851 req->offset = offset;
852 offset += buflen;
853 num_req++;
854 TAILQ_INSERT_TAIL(&requests, req, tq);
762715ce 855 send_read_request(conn->fd_out, req->id, req->offset,
c25d3df7 856 req->len, handle, handle_len);
857 }
61e96248 858
859 buffer_clear(&msg);
22e6c827 860 get_msg(conn->fd_in, &msg);
61e96248 861 type = buffer_get_char(&msg);
862 id = buffer_get_int(&msg);
9906a836 863 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
c25d3df7 864
865 /* Find the request in our queue */
f8cc7664 866 for (req = TAILQ_FIRST(&requests);
c25d3df7 867 req != NULL && req->id != id;
868 req = TAILQ_NEXT(req, tq))
869 ;
870 if (req == NULL)
871 fatal("Unexpected reply %u", id);
872
873 switch (type) {
874 case SSH2_FXP_STATUS:
0426a3b4 875 status = buffer_get_int(&msg);
c25d3df7 876 if (status != SSH2_FX_EOF)
877 read_error = 1;
878 max_req = 0;
879 TAILQ_REMOVE(&requests, req, tq);
880 xfree(req);
881 num_req--;
882 break;
883 case SSH2_FXP_DATA:
884 data = buffer_get_string(&msg, &len);
bfa7f960 885 debug3("Received data %llu -> %llu",
762715ce 886 (unsigned long long)req->offset,
bfa7f960 887 (unsigned long long)req->offset + len - 1);
c25d3df7 888 if (len > req->len)
889 fatal("Received more data than asked for "
b77a87e5 890 "%u > %u", len, req->len);
c25d3df7 891 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
dc54438a 892 atomicio(vwrite, local_fd, data, len) != len) &&
c25d3df7 893 !write_error) {
894 write_errno = errno;
895 write_error = 1;
896 max_req = 0;
897 }
b65c3807 898 progress_counter += len;
c25d3df7 899 xfree(data);
61e96248 900
c25d3df7 901 if (len == req->len) {
902 TAILQ_REMOVE(&requests, req, tq);
903 xfree(req);
904 num_req--;
905 } else {
906 /* Resend the request for the missing data */
907 debug3("Short data block, re-requesting "
bfa7f960 908 "%llu -> %llu (%2d)",
762715ce 909 (unsigned long long)req->offset + len,
5fc7dbc9 910 (unsigned long long)req->offset +
911 req->len - 1, num_req);
22e6c827 912 req->id = conn->msg_id++;
c25d3df7 913 req->len -= len;
914 req->offset += len;
762715ce 915 send_read_request(conn->fd_out, req->id,
22e6c827 916 req->offset, req->len, handle, handle_len);
c25d3df7 917 /* Reduce the request size */
918 if (len < buflen)
919 buflen = MAX(MIN_READ_SIZE, len);
920 }
921 if (max_req > 0) { /* max_req = 0 iff EOF received */
922 if (size > 0 && offset > size) {
923 /* Only one request at a time
924 * after the expected EOF */
925 debug3("Finish at %llu (%2d)",
bfa7f960 926 (unsigned long long)offset,
927 num_req);
c25d3df7 928 max_req = 1;
0e5de6f8 929 } else if (max_req <= conn->num_requests) {
c25d3df7 930 ++max_req;
931 }
61e96248 932 }
c25d3df7 933 break;
934 default:
9906a836 935 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
61e96248 936 SSH2_FXP_DATA, type);
937 }
61e96248 938 }
61e96248 939
b65c3807 940 if (showprogress && size)
941 stop_progress_meter();
942
c25d3df7 943 /* Sanity check */
944 if (TAILQ_FIRST(&requests) != NULL)
945 fatal("Transfer complete, but requests still in queue");
946
947 if (read_error) {
762715ce 948 error("Couldn't read from remote file \"%s\" : %s",
22e6c827 949 remote_path, fx2txt(status));
950 do_close(conn, handle, handle_len);
c25d3df7 951 } else if (write_error) {
22e6c827 952 error("Couldn't write to \"%s\": %s", local_path,
953 strerror(write_errno));
954 status = -1;
955 do_close(conn, handle, handle_len);
c25d3df7 956 } else {
22e6c827 957 status = do_close(conn, handle, handle_len);
c25d3df7 958
959 /* Override umask and utimes if asked */
663fd560 960#ifdef HAVE_FCHMOD
c25d3df7 961 if (pflag && fchmod(local_fd, mode) == -1)
aff51935 962#else
c25d3df7 963 if (pflag && chmod(local_path, mode) == -1)
663fd560 964#endif /* HAVE_FCHMOD */
c25d3df7 965 error("Couldn't set mode on \"%s\": %s", local_path,
b77a87e5 966 strerror(errno));
c25d3df7 967 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
968 struct timeval tv[2];
969 tv[0].tv_sec = a->atime;
970 tv[1].tv_sec = a->mtime;
971 tv[0].tv_usec = tv[1].tv_usec = 0;
972 if (utimes(local_path, tv) == -1)
973 error("Can't set times on \"%s\": %s",
b77a87e5 974 local_path, strerror(errno));
c25d3df7 975 }
58c54a79 976 }
0426a3b4 977 close(local_fd);
978 buffer_free(&msg);
979 xfree(handle);
22e6c827 980
981 return(status);
61e96248 982}
983
984int
22e6c827 985do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
986 int pflag)
61e96248 987{
375f867e 988 int local_fd, status;
b2bab059 989 u_int handle_len, id, type;
61e96248 990 u_int64_t offset;
375f867e 991 char *handle, *data;
61e96248 992 Buffer msg;
993 struct stat sb;
994 Attrib a;
c25d3df7 995 u_int32_t startid;
996 u_int32_t ackid;
b2bab059 997 struct outstanding_ack {
998 u_int id;
999 u_int len;
1000 u_int64_t offset;
762715ce 1001 TAILQ_ENTRY(outstanding_ack) tq;
b2bab059 1002 };
1003 TAILQ_HEAD(ackhead, outstanding_ack) acks;
6e007f08 1004 struct outstanding_ack *ack = NULL;
b2bab059 1005
1006 TAILQ_INIT(&acks);
61e96248 1007
1008 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1009 error("Couldn't open local file \"%s\" for reading: %s",
1010 local_path, strerror(errno));
1011 return(-1);
1012 }
1013 if (fstat(local_fd, &sb) == -1) {
1014 error("Couldn't fstat local file \"%s\": %s",
1015 local_path, strerror(errno));
1016 close(local_fd);
1017 return(-1);
1018 }
538840a2 1019 if (!S_ISREG(sb.st_mode)) {
1020 error("%s is not a regular file", local_path);
1021 close(local_fd);
1022 return(-1);
1023 }
61e96248 1024 stat_to_attrib(&sb, &a);
1025
1026 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1027 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1028 a.perm &= 0777;
1029 if (!pflag)
1030 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1031
1032 buffer_init(&msg);
1033
1034 /* Send open request */
22e6c827 1035 id = conn->msg_id++;
61e96248 1036 buffer_put_char(&msg, SSH2_FXP_OPEN);
1037 buffer_put_int(&msg, id);
1038 buffer_put_cstring(&msg, remote_path);
1039 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1040 encode_attrib(&msg, &a);
22e6c827 1041 send_msg(conn->fd_out, &msg);
9906a836 1042 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
61e96248 1043
1044 buffer_clear(&msg);
1045
22e6c827 1046 handle = get_handle(conn->fd_in, id, &handle_len);
61e96248 1047 if (handle == NULL) {
1048 close(local_fd);
1049 buffer_free(&msg);
1050 return(-1);
1051 }
1052
c25d3df7 1053 startid = ackid = id + 1;
22e6c827 1054 data = xmalloc(conn->transfer_buflen);
375f867e 1055
61e96248 1056 /* Read from local and write to remote */
1057 offset = 0;
b65c3807 1058 if (showprogress)
1059 start_progress_meter(local_path, sb.st_size, &offset);
b65c3807 1060
184eed6a 1061 for (;;) {
61e96248 1062 int len;
61e96248 1063
1064 /*
f2107e97 1065 * Can't use atomicio here because it returns 0 on EOF,
0e5de6f8 1066 * thus losing the last block of the file.
f2107e97 1067 * Simulate an EOF on interrupt, allowing ACKs from the
0e5de6f8 1068 * server to drain.
61e96248 1069 */
0e5de6f8 1070 if (interrupted)
1071 len = 0;
1072 else do
22e6c827 1073 len = read(local_fd, data, conn->transfer_buflen);
61e96248 1074 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1075
1076 if (len == -1)
1077 fatal("Couldn't read from \"%s\": %s", local_path,
1078 strerror(errno));
c25d3df7 1079
1080 if (len != 0) {
b2bab059 1081 ack = xmalloc(sizeof(*ack));
1082 ack->id = ++id;
1083 ack->offset = offset;
1084 ack->len = len;
1085 TAILQ_INSERT_TAIL(&acks, ack, tq);
1086
c25d3df7 1087 buffer_clear(&msg);
1088 buffer_put_char(&msg, SSH2_FXP_WRITE);
b2bab059 1089 buffer_put_int(&msg, ack->id);
c25d3df7 1090 buffer_put_string(&msg, handle, handle_len);
1091 buffer_put_int64(&msg, offset);
1092 buffer_put_string(&msg, data, len);
22e6c827 1093 send_msg(conn->fd_out, &msg);
9906a836 1094 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
b77a87e5 1095 id, (unsigned long long)offset, len);
b2bab059 1096 } else if (TAILQ_FIRST(&acks) == NULL)
61e96248 1097 break;
1098
b2bab059 1099 if (ack == NULL)
1100 fatal("Unexpected ACK %u", id);
1101
762715ce 1102 if (id == startid || len == 0 ||
22e6c827 1103 id - ackid >= conn->num_requests) {
875ec275 1104 u_int r_id;
af2b3cd9 1105
b2bab059 1106 buffer_clear(&msg);
22e6c827 1107 get_msg(conn->fd_in, &msg);
b2bab059 1108 type = buffer_get_char(&msg);
af2b3cd9 1109 r_id = buffer_get_int(&msg);
b2bab059 1110
1111 if (type != SSH2_FXP_STATUS)
1112 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1113 "got %d", SSH2_FXP_STATUS, type);
1114
1115 status = buffer_get_int(&msg);
1116 debug3("SSH2_FXP_STATUS %d", status);
1117
1118 /* Find the request in our queue */
f8cc7664 1119 for (ack = TAILQ_FIRST(&acks);
af2b3cd9 1120 ack != NULL && ack->id != r_id;
b2bab059 1121 ack = TAILQ_NEXT(ack, tq))
1122 ;
1123 if (ack == NULL)
9906a836 1124 fatal("Can't find request for ID %u", r_id);
b2bab059 1125 TAILQ_REMOVE(&acks, ack, tq);
1126
c25d3df7 1127 if (status != SSH2_FX_OK) {
1128 error("Couldn't write to remote file \"%s\": %s",
b77a87e5 1129 remote_path, fx2txt(status));
22e6c827 1130 do_close(conn, handle, handle_len);
c25d3df7 1131 close(local_fd);
3e2f2431 1132 xfree(data);
1133 xfree(ack);
c25d3df7 1134 goto done;
1135 }
9906a836 1136 debug3("In write loop, ack for %u %u bytes at %llu",
4e2e5cfd 1137 ack->id, ack->len, (unsigned long long)ack->offset);
c25d3df7 1138 ++ackid;
30e37ee6 1139 xfree(ack);
61e96248 1140 }
61e96248 1141 offset += len;
1142 }
b65c3807 1143 if (showprogress)
1144 stop_progress_meter();
375f867e 1145 xfree(data);
61e96248 1146
1147 if (close(local_fd) == -1) {
1148 error("Couldn't close local file \"%s\": %s", local_path,
1149 strerror(errno));
22e6c827 1150 do_close(conn, handle, handle_len);
0426a3b4 1151 status = -1;
1152 goto done;
61e96248 1153 }
1154
58c54a79 1155 /* Override umask and utimes if asked */
1156 if (pflag)
22e6c827 1157 do_fsetstat(conn, handle, handle_len, &a);
58c54a79 1158
22e6c827 1159 status = do_close(conn, handle, handle_len);
0426a3b4 1160
1161done:
1162 xfree(handle);
1163 buffer_free(&msg);
22e6c827 1164 return(status);
61e96248 1165}
This page took 0.432136 seconds and 5 git commands to generate.