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