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