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