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