]> andersk Git - openssh.git/blob - sftp-client.c
- markus@cvs.openbsd.org 2001/03/17 17:27:59
[openssh.git] / sftp-client.c
1 /*
2  * Copyright (c) 2001 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: redesign to allow concurrent overlapped operations */
28 /* XXX: we use fatal too much, error may be more appropriate in places */
29 /* XXX: copy between two remote sites */
30
31 #include "includes.h"
32 RCSID("$OpenBSD: sftp-client.c,v 1.14 2001/03/16 08:16:17 djm Exp $");
33
34 #include "ssh.h"
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 "pathnames.h"
42
43 #include "sftp.h"
44 #include "sftp-common.h"
45 #include "sftp-client.h"
46
47 /* How much data to read/write at at time during copies */
48 /* XXX: what should this be? */
49 #define COPY_SIZE       8192
50
51 /* Message ID */
52 static u_int msg_id = 1;
53
54 void
55 send_msg(int fd, Buffer *m)
56 {
57         int mlen = buffer_len(m);
58         int len;
59         Buffer oqueue;
60
61         buffer_init(&oqueue);
62         buffer_put_int(&oqueue, mlen);
63         buffer_append(&oqueue, buffer_ptr(m), mlen);
64         buffer_consume(m, mlen);
65
66         len = atomicio(write, fd, buffer_ptr(&oqueue), buffer_len(&oqueue));
67         if (len <= 0)
68                 fatal("Couldn't send packet: %s", strerror(errno));
69
70         buffer_free(&oqueue);
71 }
72
73 void
74 get_msg(int fd, Buffer *m)
75 {
76         u_int len, msg_len;
77         unsigned char buf[4096];
78
79         len = atomicio(read, fd, buf, 4);
80         if (len != 4)
81                 fatal("Couldn't read packet: %s", strerror(errno));
82
83         msg_len = GET_32BIT(buf);
84         if (msg_len > 256 * 1024)
85                 fatal("Received message too long %d", msg_len);
86
87         while (msg_len) {
88                 len = atomicio(read, fd, buf, MIN(msg_len, sizeof(buf)));
89                 if (len <= 0)
90                         fatal("Couldn't read packet: %s", strerror(errno));
91
92                 msg_len -= len;
93                 buffer_append(m, buf, len);
94         }
95 }
96
97 void
98 send_string_request(int fd, u_int id, u_int code, char *s,
99     u_int len)
100 {
101         Buffer msg;
102
103         buffer_init(&msg);
104         buffer_put_char(&msg, code);
105         buffer_put_int(&msg, id);
106         buffer_put_string(&msg, s, len);
107         send_msg(fd, &msg);
108         debug3("Sent message fd %d T:%d I:%d", fd, code, id);
109         buffer_free(&msg);
110 }
111
112 void
113 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
114     u_int len, Attrib *a)
115 {
116         Buffer msg;
117
118         buffer_init(&msg);
119         buffer_put_char(&msg, code);
120         buffer_put_int(&msg, id);
121         buffer_put_string(&msg, s, len);
122         encode_attrib(&msg, a);
123         send_msg(fd, &msg);
124         debug3("Sent message fd %d T:%d I:%d", fd, code, id);
125         buffer_free(&msg);
126 }
127
128 u_int
129 get_status(int fd, int expected_id)
130 {
131         Buffer msg;
132         u_int type, id, status;
133
134         buffer_init(&msg);
135         get_msg(fd, &msg);
136         type = buffer_get_char(&msg);
137         id = buffer_get_int(&msg);
138
139         if (id != expected_id)
140                 fatal("ID mismatch (%d != %d)", id, expected_id);
141         if (type != SSH2_FXP_STATUS)
142                 fatal("Expected SSH2_FXP_STATUS(%d) packet, got %d",
143                     SSH2_FXP_STATUS, type);
144
145         status = buffer_get_int(&msg);
146         buffer_free(&msg);
147
148         debug3("SSH2_FXP_STATUS %d", status);
149
150         return(status);
151 }
152
153 char *
154 get_handle(int fd, u_int expected_id, u_int *len)
155 {
156         Buffer msg;
157         u_int type, id;
158         char *handle;
159
160         buffer_init(&msg);
161         get_msg(fd, &msg);
162         type = buffer_get_char(&msg);
163         id = buffer_get_int(&msg);
164
165         if (id != expected_id)
166                 fatal("ID mismatch (%d != %d)", id, expected_id);
167         if (type == SSH2_FXP_STATUS) {
168                 int status = buffer_get_int(&msg);
169
170                 error("Couldn't get handle: %s", fx2txt(status));
171                 return(NULL);
172         } else if (type != SSH2_FXP_HANDLE)
173                 fatal("Expected SSH2_FXP_HANDLE(%d) packet, got %d",
174                     SSH2_FXP_HANDLE, type);
175
176         handle = buffer_get_string(&msg, len);
177         buffer_free(&msg);
178
179         return(handle);
180 }
181
182 Attrib *
183 get_decode_stat(int fd, u_int expected_id, int quiet)
184 {
185         Buffer msg;
186         u_int type, id;
187         Attrib *a;
188
189         buffer_init(&msg);
190         get_msg(fd, &msg);
191
192         type = buffer_get_char(&msg);
193         id = buffer_get_int(&msg);
194
195         debug3("Received stat reply T:%d I:%d", type, id);
196         if (id != expected_id)
197                 fatal("ID mismatch (%d != %d)", id, expected_id);
198         if (type == SSH2_FXP_STATUS) {
199                 int status = buffer_get_int(&msg);
200
201                 if (quiet)
202                         debug("Couldn't stat remote file: %s", fx2txt(status));
203                 else
204                         error("Couldn't stat remote file: %s", fx2txt(status));
205                 return(NULL);
206         } else if (type != SSH2_FXP_ATTRS) {
207                 fatal("Expected SSH2_FXP_ATTRS(%d) packet, got %d",
208                     SSH2_FXP_ATTRS, type);
209         }
210         a = decode_attrib(&msg);
211         buffer_free(&msg);
212
213         return(a);
214 }
215
216 int
217 do_init(int fd_in, int fd_out)
218 {
219         int type, version;
220         Buffer msg;
221
222         buffer_init(&msg);
223         buffer_put_char(&msg, SSH2_FXP_INIT);
224         buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
225         send_msg(fd_out, &msg);
226
227         buffer_clear(&msg);
228
229         get_msg(fd_in, &msg);
230
231         /* Expecting a VERSION reply */
232         if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
233                 error("Invalid packet back from SSH2_FXP_INIT (type %d)",
234                     type);
235                 buffer_free(&msg);
236                 return(-1);
237         }
238         version = buffer_get_int(&msg);
239
240         debug2("Remote version: %d", version);
241
242         /* Check for extensions */
243         while (buffer_len(&msg) > 0) {
244                 char *name = buffer_get_string(&msg, NULL);
245                 char *value = buffer_get_string(&msg, NULL);
246
247                 debug2("Init extension: \"%s\"", name);
248                 xfree(name);
249                 xfree(value);
250         }
251
252         buffer_free(&msg);
253
254         return(version);
255 }
256
257 int
258 do_close(int fd_in, int fd_out, char *handle, u_int handle_len)
259 {
260         u_int id, status;
261         Buffer msg;
262
263         buffer_init(&msg);
264
265         id = msg_id++;
266         buffer_put_char(&msg, SSH2_FXP_CLOSE);
267         buffer_put_int(&msg, id);
268         buffer_put_string(&msg, handle, handle_len);
269         send_msg(fd_out, &msg);
270         debug3("Sent message SSH2_FXP_CLOSE I:%d", id);
271
272         status = get_status(fd_in, id);
273         if (status != SSH2_FX_OK)
274                 error("Couldn't close file: %s", fx2txt(status));
275
276         buffer_free(&msg);
277
278         return(status);
279 }
280
281
282 int
283 do_lsreaddir(int fd_in, int fd_out, char *path, int printflag, 
284     SFTP_DIRENT ***dir)
285 {
286         Buffer msg;
287         u_int type, id, handle_len, i, expected_id, ents = 0;
288         char *handle;
289
290         id = msg_id++;
291
292         buffer_init(&msg);
293         buffer_put_char(&msg, SSH2_FXP_OPENDIR);
294         buffer_put_int(&msg, id);
295         buffer_put_cstring(&msg, path);
296         send_msg(fd_out, &msg);
297
298         buffer_clear(&msg);
299
300         handle = get_handle(fd_in, id, &handle_len);
301         if (handle == NULL)
302                 return(-1);
303
304         if (dir) {
305                 ents = 0;
306                 *dir = xmalloc(sizeof(**dir));
307                 (*dir)[0] = NULL;
308         }
309         
310
311         for(;;) {
312                 int count;
313
314                 id = expected_id = msg_id++;
315
316                 debug3("Sending SSH2_FXP_READDIR I:%d", id);
317
318                 buffer_clear(&msg);
319                 buffer_put_char(&msg, SSH2_FXP_READDIR);
320                 buffer_put_int(&msg, id);
321                 buffer_put_string(&msg, handle, handle_len);
322                 send_msg(fd_out, &msg);
323
324                 buffer_clear(&msg);
325
326                 get_msg(fd_in, &msg);
327
328                 type = buffer_get_char(&msg);
329                 id = buffer_get_int(&msg);
330
331                 debug3("Received reply T:%d I:%d", type, id);
332
333                 if (id != expected_id)
334                         fatal("ID mismatch (%d != %d)", id, expected_id);
335
336                 if (type == SSH2_FXP_STATUS) {
337                         int status = buffer_get_int(&msg);
338
339                         debug3("Received SSH2_FXP_STATUS %d", status);
340
341                         if (status == SSH2_FX_EOF) {
342                                 break;
343                         } else {
344                                 error("Couldn't read directory: %s",
345                                     fx2txt(status));
346                                 do_close(fd_in, fd_out, handle, handle_len);
347                                 return(status);
348                         }
349                 } else if (type != SSH2_FXP_NAME)
350                         fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
351                             SSH2_FXP_NAME, type);
352
353                 count = buffer_get_int(&msg);
354                 if (count == 0)
355                         break;
356                 debug3("Received %d SSH2_FXP_NAME responses", count);
357                 for(i = 0; i < count; i++) {
358                         char *filename, *longname;
359                         Attrib *a;
360
361                         filename = buffer_get_string(&msg, NULL);
362                         longname = buffer_get_string(&msg, NULL);
363                         a = decode_attrib(&msg);
364
365                         if (printflag)
366                                 printf("%s\n", longname);
367
368                         if (dir) {
369                                 *dir = xrealloc(*dir, sizeof(**dir) * 
370                                     (ents + 2));
371                                 (*dir)[ents] = xmalloc(sizeof(***dir));
372                                 (*dir)[ents]->filename = xstrdup(filename);
373                                 (*dir)[ents]->longname = xstrdup(longname);
374                                 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
375                                 (*dir)[++ents] = NULL;
376                         }
377
378                         xfree(filename);
379                         xfree(longname);
380                 }
381         }
382
383         buffer_free(&msg);
384         do_close(fd_in, fd_out, handle, handle_len);
385         xfree(handle);
386
387         return(0);
388 }
389
390 int
391 do_ls(int fd_in, int fd_out, char *path)
392 {
393         return(do_lsreaddir(fd_in, fd_out, path, 1, NULL));
394 }
395
396 int
397 do_readdir(int fd_in, int fd_out, char *path, SFTP_DIRENT ***dir)
398 {
399         return(do_lsreaddir(fd_in, fd_out, path, 0, dir));
400 }
401
402 void free_sftp_dirents(SFTP_DIRENT **s)
403 {
404         int i;
405         
406         for(i = 0; s[i]; i++) {
407                 xfree(s[i]->filename);
408                 xfree(s[i]->longname);
409                 xfree(s[i]);
410         }
411         xfree(s);
412 }
413
414 int
415 do_rm(int fd_in, int fd_out, char *path)
416 {
417         u_int status, id;
418
419         debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
420
421         id = msg_id++;
422         send_string_request(fd_out, id, SSH2_FXP_REMOVE, path, strlen(path));
423         status = get_status(fd_in, id);
424         if (status != SSH2_FX_OK)
425                 error("Couldn't delete file: %s", fx2txt(status));
426         return(status);
427 }
428
429 int
430 do_mkdir(int fd_in, int fd_out, char *path, Attrib *a)
431 {
432         u_int status, id;
433
434         id = msg_id++;
435         send_string_attrs_request(fd_out, id, SSH2_FXP_MKDIR, path,
436             strlen(path), a);
437
438         status = get_status(fd_in, id);
439         if (status != SSH2_FX_OK)
440                 error("Couldn't create directory: %s", fx2txt(status));
441
442         return(status);
443 }
444
445 int
446 do_rmdir(int fd_in, int fd_out, char *path)
447 {
448         u_int status, id;
449
450         id = msg_id++;
451         send_string_request(fd_out, id, SSH2_FXP_RMDIR, path, strlen(path));
452
453         status = get_status(fd_in, id);
454         if (status != SSH2_FX_OK)
455                 error("Couldn't remove directory: %s", fx2txt(status));
456
457         return(status);
458 }
459
460 Attrib *
461 do_stat(int fd_in, int fd_out, char *path, int quiet)
462 {
463         u_int id;
464
465         id = msg_id++;
466         send_string_request(fd_out, id, SSH2_FXP_STAT, path, strlen(path));
467         return(get_decode_stat(fd_in, id, quiet));
468 }
469
470 Attrib *
471 do_lstat(int fd_in, int fd_out, char *path, int quiet)
472 {
473         u_int id;
474
475         id = msg_id++;
476         send_string_request(fd_out, id, SSH2_FXP_LSTAT, path, strlen(path));
477         return(get_decode_stat(fd_in, id, quiet));
478 }
479
480 Attrib *
481 do_fstat(int fd_in, int fd_out, char *handle, u_int handle_len, int quiet)
482 {
483         u_int id;
484
485         id = msg_id++;
486         send_string_request(fd_out, id, SSH2_FXP_FSTAT, handle, handle_len);
487         return(get_decode_stat(fd_in, id, quiet));
488 }
489
490 int
491 do_setstat(int fd_in, int fd_out, char *path, Attrib *a)
492 {
493         u_int status, id;
494
495         id = msg_id++;
496         send_string_attrs_request(fd_out, id, SSH2_FXP_SETSTAT, path,
497             strlen(path), a);
498
499         status = get_status(fd_in, id);
500         if (status != SSH2_FX_OK)
501                 error("Couldn't setstat on \"%s\": %s", path,
502                     fx2txt(status));
503
504         return(status);
505 }
506
507 int
508 do_fsetstat(int fd_in, int fd_out, char *handle, u_int handle_len,
509     Attrib *a)
510 {
511         u_int status, id;
512
513         id = msg_id++;
514         send_string_attrs_request(fd_out, id, SSH2_FXP_FSETSTAT, handle,
515             handle_len, a);
516
517         status = get_status(fd_in, id);
518         if (status != SSH2_FX_OK)
519                 error("Couldn't fsetstat: %s", fx2txt(status));
520
521         return(status);
522 }
523
524 char *
525 do_realpath(int fd_in, int fd_out, char *path)
526 {
527         Buffer msg;
528         u_int type, expected_id, count, id;
529         char *filename, *longname;
530         Attrib *a;
531
532         expected_id = id = msg_id++;
533         send_string_request(fd_out, id, SSH2_FXP_REALPATH, path, strlen(path));
534
535         buffer_init(&msg);
536
537         get_msg(fd_in, &msg);
538         type = buffer_get_char(&msg);
539         id = buffer_get_int(&msg);
540
541         if (id != expected_id)
542                 fatal("ID mismatch (%d != %d)", id, expected_id);
543
544         if (type == SSH2_FXP_STATUS) {
545                 u_int status = buffer_get_int(&msg);
546
547                 error("Couldn't canonicalise: %s", fx2txt(status));
548                 return(NULL);
549         } else if (type != SSH2_FXP_NAME)
550                 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
551                     SSH2_FXP_NAME, type);
552
553         count = buffer_get_int(&msg);
554         if (count != 1)
555                 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
556
557         filename = buffer_get_string(&msg, NULL);
558         longname = buffer_get_string(&msg, NULL);
559         a = decode_attrib(&msg);
560
561         debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
562
563         xfree(longname);
564
565         buffer_free(&msg);
566
567         return(filename);
568 }
569
570 int
571 do_rename(int fd_in, int fd_out, char *oldpath, char *newpath)
572 {
573         Buffer msg;
574         u_int status, id;
575
576         buffer_init(&msg);
577
578         /* Send rename request */
579         id = msg_id++;
580         buffer_put_char(&msg, SSH2_FXP_RENAME);
581         buffer_put_int(&msg, id);
582         buffer_put_cstring(&msg, oldpath);
583         buffer_put_cstring(&msg, newpath);
584         send_msg(fd_out, &msg);
585         debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
586             newpath);
587         buffer_free(&msg);
588
589         status = get_status(fd_in, id);
590         if (status != SSH2_FX_OK)
591                 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
592                     fx2txt(status));
593
594         return(status);
595 }
596
597 int
598 do_symlink(int fd_in, int fd_out, char *oldpath, char *newpath)
599 {
600         Buffer msg;
601         u_int status, id;
602
603         buffer_init(&msg);
604
605         /* Send rename request */
606         id = msg_id++;
607         buffer_put_char(&msg, SSH2_FXP_SYMLINK);
608         buffer_put_int(&msg, id);
609         buffer_put_cstring(&msg, oldpath);
610         buffer_put_cstring(&msg, newpath);
611         send_msg(fd_out, &msg);
612         debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
613             newpath);
614         buffer_free(&msg);
615
616         status = get_status(fd_in, id);
617         if (status != SSH2_FX_OK)
618                 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
619                     fx2txt(status));
620
621         return(status);
622 }
623
624 char *
625 do_readlink(int fd_in, int fd_out, char *path)
626 {
627         Buffer msg;
628         u_int type, expected_id, count, id;
629         char *filename, *longname;
630         Attrib *a;
631
632         expected_id = id = msg_id++;
633         send_string_request(fd_out, id, SSH2_FXP_READLINK, path, strlen(path));
634
635         buffer_init(&msg);
636
637         get_msg(fd_in, &msg);
638         type = buffer_get_char(&msg);
639         id = buffer_get_int(&msg);
640
641         if (id != expected_id)
642                 fatal("ID mismatch (%d != %d)", id, expected_id);
643
644         if (type == SSH2_FXP_STATUS) {
645                 u_int status = buffer_get_int(&msg);
646
647                 error("Couldn't readlink: %s", fx2txt(status));
648                 return(NULL);
649         } else if (type != SSH2_FXP_NAME)
650                 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
651                     SSH2_FXP_NAME, type);
652
653         count = buffer_get_int(&msg);
654         if (count != 1)
655                 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
656
657         filename = buffer_get_string(&msg, NULL);
658         longname = buffer_get_string(&msg, NULL);
659         a = decode_attrib(&msg);
660
661         debug3("SSH_FXP_READLINK %s -> %s", path, filename);
662
663         xfree(longname);
664
665         buffer_free(&msg);
666
667         return(filename);
668 }
669
670 int
671 do_download(int fd_in, int fd_out, char *remote_path, char *local_path,
672     int pflag)
673 {
674         int local_fd;
675         u_int expected_id, handle_len, mode, type, id;
676         u_int64_t offset;
677         char *handle;
678         Buffer msg;
679         Attrib junk, *a;
680         int status;
681
682         a = do_stat(fd_in, fd_out, remote_path, 0);
683         if (a == NULL)
684                 return(-1);
685
686         /* XXX: should we preserve set[ug]id? */
687         if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
688                 mode = S_IWRITE | (a->perm & 0777);
689         else
690                 mode = 0666;
691
692         if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
693             (a->perm & S_IFDIR)) {
694                 error("Cannot download a directory: %s", remote_path);
695                 return(-1);
696         }
697
698         local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
699         if (local_fd == -1) {
700                 error("Couldn't open local file \"%s\" for writing: %s",
701                     local_path, strerror(errno));
702                 return(-1);
703         }
704
705         buffer_init(&msg);
706
707         /* Send open request */
708         id = msg_id++;
709         buffer_put_char(&msg, SSH2_FXP_OPEN);
710         buffer_put_int(&msg, id);
711         buffer_put_cstring(&msg, remote_path);
712         buffer_put_int(&msg, SSH2_FXF_READ);
713         attrib_clear(&junk); /* Send empty attributes */
714         encode_attrib(&msg, &junk);
715         send_msg(fd_out, &msg);
716         debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
717
718         handle = get_handle(fd_in, id, &handle_len);
719         if (handle == NULL) {
720                 buffer_free(&msg);
721                 close(local_fd);
722                 return(-1);
723         }
724
725         /* Read from remote and write to local */
726         offset = 0;
727         for(;;) {
728                 u_int len;
729                 char *data;
730
731                 id = expected_id = msg_id++;
732
733                 buffer_clear(&msg);
734                 buffer_put_char(&msg, SSH2_FXP_READ);
735                 buffer_put_int(&msg, id);
736                 buffer_put_string(&msg, handle, handle_len);
737                 buffer_put_int64(&msg, offset);
738                 buffer_put_int(&msg, COPY_SIZE);
739                 send_msg(fd_out, &msg);
740                 debug3("Sent message SSH2_FXP_READ I:%d O:%llu S:%u",
741                     id, (u_int64_t)offset, COPY_SIZE);
742
743                 buffer_clear(&msg);
744
745                 get_msg(fd_in, &msg);
746                 type = buffer_get_char(&msg);
747                 id = buffer_get_int(&msg);
748                 debug3("Received reply T:%d I:%d", type, id);
749                 if (id != expected_id)
750                         fatal("ID mismatch (%d != %d)", id, expected_id);
751                 if (type == SSH2_FXP_STATUS) {
752                         status = buffer_get_int(&msg);
753
754                         if (status == SSH2_FX_EOF)
755                                 break;
756                         else {
757                                 error("Couldn't read from remote "
758                                     "file \"%s\" : %s", remote_path,
759                                      fx2txt(status));
760                                 do_close(fd_in, fd_out, handle, handle_len);
761                                 goto done;
762                         }
763                 } else if (type != SSH2_FXP_DATA) {
764                         fatal("Expected SSH2_FXP_DATA(%d) packet, got %d",
765                             SSH2_FXP_DATA, type);
766                 }
767
768                 data = buffer_get_string(&msg, &len);
769                 if (len > COPY_SIZE)
770                         fatal("Received more data than asked for %d > %d",
771                             len, COPY_SIZE);
772
773                 debug3("In read loop, got %d offset %llu", len,
774                     (u_int64_t)offset);
775                 if (atomicio(write, local_fd, data, len) != len) {
776                         error("Couldn't write to \"%s\": %s", local_path,
777                             strerror(errno));
778                         do_close(fd_in, fd_out, handle, handle_len);
779                         status = -1;
780                         xfree(data);
781                         goto done;
782                 }
783
784                 offset += len;
785                 xfree(data);
786         }
787         status = do_close(fd_in, fd_out, handle, handle_len);
788
789         /* Override umask and utimes if asked */
790 #ifdef HAVE_FCHMOD
791         if (pflag && fchmod(local_fd, mode) == -1)
792 #else 
793         if (pflag && chmod(local_path, mode) == -1)
794 #endif /* HAVE_FCHMOD */
795                 error("Couldn't set mode on \"%s\": %s", local_path,
796                     strerror(errno));
797         if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
798                 struct timeval tv[2];
799                 tv[0].tv_sec = a->atime;
800                 tv[1].tv_sec = a->mtime;
801                 tv[0].tv_usec = tv[1].tv_usec = 0;
802                 if (utimes(local_path, tv) == -1)
803                         error("Can't set times on \"%s\": %s", local_path,
804                             strerror(errno));
805         }
806
807 done:
808         close(local_fd);
809         buffer_free(&msg);
810         xfree(handle);
811         return status;
812 }
813
814 int
815 do_upload(int fd_in, int fd_out, char *local_path, char *remote_path,
816     int pflag)
817 {
818         int local_fd;
819         u_int handle_len, id;
820         u_int64_t offset;
821         char *handle;
822         Buffer msg;
823         struct stat sb;
824         Attrib a;
825         int status;
826
827         if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
828                 error("Couldn't open local file \"%s\" for reading: %s",
829                     local_path, strerror(errno));
830                 return(-1);
831         }
832         if (fstat(local_fd, &sb) == -1) {
833                 error("Couldn't fstat local file \"%s\": %s",
834                     local_path, strerror(errno));
835                 close(local_fd);
836                 return(-1);
837         }
838         stat_to_attrib(&sb, &a);
839
840         a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
841         a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
842         a.perm &= 0777;
843         if (!pflag)
844                 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
845
846         buffer_init(&msg);
847
848         /* Send open request */
849         id = msg_id++;
850         buffer_put_char(&msg, SSH2_FXP_OPEN);
851         buffer_put_int(&msg, id);
852         buffer_put_cstring(&msg, remote_path);
853         buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
854         encode_attrib(&msg, &a);
855         send_msg(fd_out, &msg);
856         debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
857
858         buffer_clear(&msg);
859
860         handle = get_handle(fd_in, id, &handle_len);
861         if (handle == NULL) {
862                 close(local_fd);
863                 buffer_free(&msg);
864                 return(-1);
865         }
866
867         /* Read from local and write to remote */
868         offset = 0;
869         for(;;) {
870                 int len;
871                 char data[COPY_SIZE];
872
873                 /*
874                  * Can't use atomicio here because it returns 0 on EOF, thus losing
875                  * the last block of the file
876                  */
877                 do
878                         len = read(local_fd, data, COPY_SIZE);
879                 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
880
881                 if (len == -1)
882                         fatal("Couldn't read from \"%s\": %s", local_path,
883                             strerror(errno));
884                 if (len == 0)
885                         break;
886
887                 buffer_clear(&msg);
888                 buffer_put_char(&msg, SSH2_FXP_WRITE);
889                 buffer_put_int(&msg, ++id);
890                 buffer_put_string(&msg, handle, handle_len);
891                 buffer_put_int64(&msg, offset);
892                 buffer_put_string(&msg, data, len);
893                 send_msg(fd_out, &msg);
894                 debug3("Sent message SSH2_FXP_WRITE I:%d O:%llu S:%u",
895                     id, (u_int64_t)offset, len);
896
897                 status = get_status(fd_in, id);
898                 if (status != SSH2_FX_OK) {
899                         error("Couldn't write to remote file \"%s\": %s",
900                             remote_path, fx2txt(status));
901                         do_close(fd_in, fd_out, handle, handle_len);
902                         close(local_fd);
903                         goto done;
904                 }
905                 debug3("In write loop, got %d offset %llu", len,
906                     (u_int64_t)offset);
907
908                 offset += len;
909         }
910
911         if (close(local_fd) == -1) {
912                 error("Couldn't close local file \"%s\": %s", local_path,
913                     strerror(errno));
914                 do_close(fd_in, fd_out, handle, handle_len);
915                 status = -1;
916                 goto done;
917         }
918
919         /* Override umask and utimes if asked */
920         if (pflag)
921                 do_fsetstat(fd_in, fd_out, handle, handle_len, &a);
922
923         status = do_close(fd_in, fd_out, handle, handle_len);
924
925 done:
926         xfree(handle);
927         buffer_free(&msg);
928         return status;
929 }
930
This page took 0.118879 seconds and 5 git commands to generate.