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