blob: 362814d42ef85e4d2efb2d53f9decd97949d949a [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
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"
Damien Miller8829d362002-02-08 22:04:05 +110032RCSID("$OpenBSD: sftp-client.c,v 1.20 2002/02/05 00:00:46 djm Exp $");
Damien Miller33804262001-02-04 23:20:18 +110033
Damien Miller33804262001-02-04 23:20:18 +110034#include "buffer.h"
35#include "bufaux.h"
36#include "getput.h"
37#include "xmalloc.h"
38#include "log.h"
39#include "atomicio.h"
Damien Miller33804262001-02-04 23:20:18 +110040
41#include "sftp.h"
42#include "sftp-common.h"
43#include "sftp-client.h"
44
Ben Lindstrom288cc392001-02-09 02:58:04 +000045/* Message ID */
46static u_int msg_id = 1;
47
Ben Lindstrombba81212001-06-25 05:01:22 +000048static void
Damien Miller33804262001-02-04 23:20:18 +110049send_msg(int fd, Buffer *m)
50{
51 int mlen = buffer_len(m);
52 int len;
53 Buffer oqueue;
54
55 buffer_init(&oqueue);
56 buffer_put_int(&oqueue, mlen);
57 buffer_append(&oqueue, buffer_ptr(m), mlen);
58 buffer_consume(m, mlen);
59
60 len = atomicio(write, fd, buffer_ptr(&oqueue), buffer_len(&oqueue));
61 if (len <= 0)
62 fatal("Couldn't send packet: %s", strerror(errno));
63
64 buffer_free(&oqueue);
65}
66
Ben Lindstrombba81212001-06-25 05:01:22 +000067static void
Damien Miller33804262001-02-04 23:20:18 +110068get_msg(int fd, Buffer *m)
69{
70 u_int len, msg_len;
71 unsigned char buf[4096];
72
73 len = atomicio(read, fd, buf, 4);
Damien Millercafff192001-03-19 22:29:46 +110074 if (len == 0)
75 fatal("Connection closed");
76 else if (len == -1)
Damien Miller33804262001-02-04 23:20:18 +110077 fatal("Couldn't read packet: %s", strerror(errno));
78
79 msg_len = GET_32BIT(buf);
80 if (msg_len > 256 * 1024)
81 fatal("Received message too long %d", msg_len);
82
83 while (msg_len) {
84 len = atomicio(read, fd, buf, MIN(msg_len, sizeof(buf)));
Damien Millercafff192001-03-19 22:29:46 +110085 if (len == 0)
86 fatal("Connection closed");
87 else if (len == -1)
Damien Miller33804262001-02-04 23:20:18 +110088 fatal("Couldn't read packet: %s", strerror(errno));
89
90 msg_len -= len;
91 buffer_append(m, buf, len);
92 }
93}
94
Ben Lindstrombba81212001-06-25 05:01:22 +000095static void
Damien Miller33804262001-02-04 23:20:18 +110096send_string_request(int fd, u_int id, u_int code, char *s,
97 u_int len)
98{
99 Buffer msg;
100
101 buffer_init(&msg);
102 buffer_put_char(&msg, code);
103 buffer_put_int(&msg, id);
104 buffer_put_string(&msg, s, len);
105 send_msg(fd, &msg);
106 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
107 buffer_free(&msg);
108}
109
Ben Lindstrombba81212001-06-25 05:01:22 +0000110static void
Damien Miller33804262001-02-04 23:20:18 +1100111send_string_attrs_request(int fd, u_int id, u_int code, char *s,
112 u_int len, Attrib *a)
113{
114 Buffer msg;
115
116 buffer_init(&msg);
117 buffer_put_char(&msg, code);
118 buffer_put_int(&msg, id);
119 buffer_put_string(&msg, s, len);
120 encode_attrib(&msg, a);
121 send_msg(fd, &msg);
122 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
123 buffer_free(&msg);
124}
125
Ben Lindstrombba81212001-06-25 05:01:22 +0000126static u_int
Damien Miller33804262001-02-04 23:20:18 +1100127get_status(int fd, int expected_id)
128{
129 Buffer msg;
130 u_int type, id, status;
131
132 buffer_init(&msg);
133 get_msg(fd, &msg);
134 type = buffer_get_char(&msg);
135 id = buffer_get_int(&msg);
136
137 if (id != expected_id)
138 fatal("ID mismatch (%d != %d)", id, expected_id);
139 if (type != SSH2_FXP_STATUS)
140 fatal("Expected SSH2_FXP_STATUS(%d) packet, got %d",
141 SSH2_FXP_STATUS, type);
142
143 status = buffer_get_int(&msg);
144 buffer_free(&msg);
145
146 debug3("SSH2_FXP_STATUS %d", status);
147
148 return(status);
149}
150
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static char *
Damien Miller33804262001-02-04 23:20:18 +1100152get_handle(int fd, u_int expected_id, u_int *len)
153{
154 Buffer msg;
155 u_int type, id;
156 char *handle;
157
158 buffer_init(&msg);
159 get_msg(fd, &msg);
160 type = buffer_get_char(&msg);
161 id = buffer_get_int(&msg);
162
163 if (id != expected_id)
164 fatal("ID mismatch (%d != %d)", id, expected_id);
165 if (type == SSH2_FXP_STATUS) {
166 int status = buffer_get_int(&msg);
167
168 error("Couldn't get handle: %s", fx2txt(status));
169 return(NULL);
170 } else if (type != SSH2_FXP_HANDLE)
171 fatal("Expected SSH2_FXP_HANDLE(%d) packet, got %d",
172 SSH2_FXP_HANDLE, type);
173
174 handle = buffer_get_string(&msg, len);
175 buffer_free(&msg);
176
177 return(handle);
178}
179
Ben Lindstrombba81212001-06-25 05:01:22 +0000180static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000181get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100182{
183 Buffer msg;
184 u_int type, id;
185 Attrib *a;
186
187 buffer_init(&msg);
188 get_msg(fd, &msg);
189
190 type = buffer_get_char(&msg);
191 id = buffer_get_int(&msg);
192
193 debug3("Received stat reply T:%d I:%d", type, id);
194 if (id != expected_id)
195 fatal("ID mismatch (%d != %d)", id, expected_id);
196 if (type == SSH2_FXP_STATUS) {
197 int status = buffer_get_int(&msg);
198
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000199 if (quiet)
200 debug("Couldn't stat remote file: %s", fx2txt(status));
201 else
202 error("Couldn't stat remote file: %s", fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100203 return(NULL);
204 } else if (type != SSH2_FXP_ATTRS) {
205 fatal("Expected SSH2_FXP_ATTRS(%d) packet, got %d",
206 SSH2_FXP_ATTRS, type);
207 }
208 a = decode_attrib(&msg);
209 buffer_free(&msg);
210
211 return(a);
212}
213
214int
215do_init(int fd_in, int fd_out)
216{
217 int type, version;
218 Buffer msg;
219
220 buffer_init(&msg);
221 buffer_put_char(&msg, SSH2_FXP_INIT);
222 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
223 send_msg(fd_out, &msg);
224
225 buffer_clear(&msg);
226
227 get_msg(fd_in, &msg);
228
Kevin Stevesef4eea92001-02-05 12:42:17 +0000229 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100230 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
231 error("Invalid packet back from SSH2_FXP_INIT (type %d)",
232 type);
233 buffer_free(&msg);
234 return(-1);
235 }
236 version = buffer_get_int(&msg);
237
238 debug2("Remote version: %d", version);
239
240 /* Check for extensions */
241 while (buffer_len(&msg) > 0) {
242 char *name = buffer_get_string(&msg, NULL);
243 char *value = buffer_get_string(&msg, NULL);
244
245 debug2("Init extension: \"%s\"", name);
246 xfree(name);
247 xfree(value);
248 }
249
250 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100251
252 return(version);
Damien Miller33804262001-02-04 23:20:18 +1100253}
254
255int
256do_close(int fd_in, int fd_out, char *handle, u_int handle_len)
257{
258 u_int id, status;
259 Buffer msg;
260
261 buffer_init(&msg);
262
Ben Lindstrom288cc392001-02-09 02:58:04 +0000263 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100264 buffer_put_char(&msg, SSH2_FXP_CLOSE);
265 buffer_put_int(&msg, id);
266 buffer_put_string(&msg, handle, handle_len);
267 send_msg(fd_out, &msg);
268 debug3("Sent message SSH2_FXP_CLOSE I:%d", id);
269
270 status = get_status(fd_in, id);
271 if (status != SSH2_FX_OK)
272 error("Couldn't close file: %s", fx2txt(status));
273
274 buffer_free(&msg);
275
276 return(status);
277}
278
Damien Miller4870afd2001-03-14 10:27:09 +1100279
Ben Lindstrombba81212001-06-25 05:01:22 +0000280static int
Ben Lindstroma3700052001-04-05 23:26:32 +0000281do_lsreaddir(int fd_in, int fd_out, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100282 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100283{
284 Buffer msg;
Ben Lindstrom025df4a2001-03-14 15:16:34 +0000285 u_int type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100286 char *handle;
287
Ben Lindstrom288cc392001-02-09 02:58:04 +0000288 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100289
290 buffer_init(&msg);
291 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
292 buffer_put_int(&msg, id);
293 buffer_put_cstring(&msg, path);
294 send_msg(fd_out, &msg);
295
296 buffer_clear(&msg);
297
298 handle = get_handle(fd_in, id, &handle_len);
299 if (handle == NULL)
300 return(-1);
301
Damien Miller4870afd2001-03-14 10:27:09 +1100302 if (dir) {
303 ents = 0;
304 *dir = xmalloc(sizeof(**dir));
305 (*dir)[0] = NULL;
306 }
Damien Miller4870afd2001-03-14 10:27:09 +1100307
Damien Miller9f0f5c62001-12-21 14:45:46 +1100308 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +1100309 int count;
310
Ben Lindstrom288cc392001-02-09 02:58:04 +0000311 id = expected_id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100312
313 debug3("Sending SSH2_FXP_READDIR I:%d", id);
314
315 buffer_clear(&msg);
316 buffer_put_char(&msg, SSH2_FXP_READDIR);
317 buffer_put_int(&msg, id);
318 buffer_put_string(&msg, handle, handle_len);
319 send_msg(fd_out, &msg);
320
321 buffer_clear(&msg);
322
323 get_msg(fd_in, &msg);
324
325 type = buffer_get_char(&msg);
326 id = buffer_get_int(&msg);
327
328 debug3("Received reply T:%d I:%d", type, id);
329
330 if (id != expected_id)
331 fatal("ID mismatch (%d != %d)", id, expected_id);
332
333 if (type == SSH2_FXP_STATUS) {
334 int status = buffer_get_int(&msg);
335
336 debug3("Received SSH2_FXP_STATUS %d", status);
337
338 if (status == SSH2_FX_EOF) {
339 break;
340 } else {
341 error("Couldn't read directory: %s",
342 fx2txt(status));
343 do_close(fd_in, fd_out, handle, handle_len);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000344 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100345 }
346 } else if (type != SSH2_FXP_NAME)
347 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
348 SSH2_FXP_NAME, type);
349
350 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100351 if (count == 0)
352 break;
353 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100354 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100355 char *filename, *longname;
356 Attrib *a;
357
358 filename = buffer_get_string(&msg, NULL);
359 longname = buffer_get_string(&msg, NULL);
360 a = decode_attrib(&msg);
361
Damien Miller4870afd2001-03-14 10:27:09 +1100362 if (printflag)
363 printf("%s\n", longname);
364
365 if (dir) {
Ben Lindstroma3700052001-04-05 23:26:32 +0000366 *dir = xrealloc(*dir, sizeof(**dir) *
Damien Miller4870afd2001-03-14 10:27:09 +1100367 (ents + 2));
368 (*dir)[ents] = xmalloc(sizeof(***dir));
369 (*dir)[ents]->filename = xstrdup(filename);
370 (*dir)[ents]->longname = xstrdup(longname);
371 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
372 (*dir)[++ents] = NULL;
373 }
Damien Miller33804262001-02-04 23:20:18 +1100374
375 xfree(filename);
376 xfree(longname);
377 }
378 }
379
380 buffer_free(&msg);
381 do_close(fd_in, fd_out, handle, handle_len);
382 xfree(handle);
383
384 return(0);
385}
386
387int
Damien Miller4870afd2001-03-14 10:27:09 +1100388do_ls(int fd_in, int fd_out, char *path)
389{
390 return(do_lsreaddir(fd_in, fd_out, path, 1, NULL));
391}
392
393int
394do_readdir(int fd_in, int fd_out, char *path, SFTP_DIRENT ***dir)
395{
396 return(do_lsreaddir(fd_in, fd_out, path, 0, dir));
397}
398
399void free_sftp_dirents(SFTP_DIRENT **s)
400{
401 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100402
403 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100404 xfree(s[i]->filename);
405 xfree(s[i]->longname);
406 xfree(s[i]);
407 }
408 xfree(s);
409}
410
411int
Damien Miller33804262001-02-04 23:20:18 +1100412do_rm(int fd_in, int fd_out, char *path)
413{
414 u_int status, id;
415
416 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
417
Ben Lindstrom288cc392001-02-09 02:58:04 +0000418 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100419 send_string_request(fd_out, id, SSH2_FXP_REMOVE, path, strlen(path));
420 status = get_status(fd_in, id);
421 if (status != SSH2_FX_OK)
422 error("Couldn't delete file: %s", fx2txt(status));
423 return(status);
424}
425
426int
427do_mkdir(int fd_in, int fd_out, char *path, Attrib *a)
428{
429 u_int status, id;
430
Ben Lindstrom288cc392001-02-09 02:58:04 +0000431 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100432 send_string_attrs_request(fd_out, id, SSH2_FXP_MKDIR, path,
433 strlen(path), a);
434
435 status = get_status(fd_in, id);
436 if (status != SSH2_FX_OK)
437 error("Couldn't create directory: %s", fx2txt(status));
438
439 return(status);
440}
441
442int
443do_rmdir(int fd_in, int fd_out, char *path)
444{
445 u_int status, id;
446
Ben Lindstrom288cc392001-02-09 02:58:04 +0000447 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100448 send_string_request(fd_out, id, SSH2_FXP_RMDIR, path, strlen(path));
449
450 status = get_status(fd_in, id);
451 if (status != SSH2_FX_OK)
452 error("Couldn't remove directory: %s", fx2txt(status));
453
454 return(status);
455}
456
457Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000458do_stat(int fd_in, int fd_out, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100459{
460 u_int id;
461
Ben Lindstrom288cc392001-02-09 02:58:04 +0000462 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100463 send_string_request(fd_out, id, SSH2_FXP_STAT, path, strlen(path));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000464 return(get_decode_stat(fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100465}
466
467Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000468do_lstat(int fd_in, int fd_out, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100469{
470 u_int id;
471
Ben Lindstrom288cc392001-02-09 02:58:04 +0000472 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100473 send_string_request(fd_out, id, SSH2_FXP_LSTAT, path, strlen(path));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000474 return(get_decode_stat(fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100475}
476
477Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000478do_fstat(int fd_in, int fd_out, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100479{
480 u_int id;
481
Ben Lindstrom288cc392001-02-09 02:58:04 +0000482 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100483 send_string_request(fd_out, id, SSH2_FXP_FSTAT, handle, handle_len);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000484 return(get_decode_stat(fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100485}
486
487int
488do_setstat(int fd_in, int fd_out, char *path, Attrib *a)
489{
490 u_int status, id;
491
Ben Lindstrom288cc392001-02-09 02:58:04 +0000492 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100493 send_string_attrs_request(fd_out, id, SSH2_FXP_SETSTAT, path,
494 strlen(path), a);
495
496 status = get_status(fd_in, id);
497 if (status != SSH2_FX_OK)
498 error("Couldn't setstat on \"%s\": %s", path,
499 fx2txt(status));
500
501 return(status);
502}
503
504int
505do_fsetstat(int fd_in, int fd_out, char *handle, u_int handle_len,
506 Attrib *a)
507{
508 u_int status, id;
509
Ben Lindstrom288cc392001-02-09 02:58:04 +0000510 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100511 send_string_attrs_request(fd_out, id, SSH2_FXP_FSETSTAT, handle,
512 handle_len, a);
513
514 status = get_status(fd_in, id);
515 if (status != SSH2_FX_OK)
516 error("Couldn't fsetstat: %s", fx2txt(status));
517
518 return(status);
519}
520
521char *
522do_realpath(int fd_in, int fd_out, char *path)
523{
524 Buffer msg;
525 u_int type, expected_id, count, id;
526 char *filename, *longname;
527 Attrib *a;
528
Ben Lindstrom288cc392001-02-09 02:58:04 +0000529 expected_id = id = msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100530 send_string_request(fd_out, id, SSH2_FXP_REALPATH, path, strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100531
532 buffer_init(&msg);
533
534 get_msg(fd_in, &msg);
535 type = buffer_get_char(&msg);
536 id = buffer_get_int(&msg);
537
538 if (id != expected_id)
539 fatal("ID mismatch (%d != %d)", id, expected_id);
540
541 if (type == SSH2_FXP_STATUS) {
542 u_int status = buffer_get_int(&msg);
543
544 error("Couldn't canonicalise: %s", fx2txt(status));
545 return(NULL);
546 } else if (type != SSH2_FXP_NAME)
547 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
548 SSH2_FXP_NAME, type);
549
550 count = buffer_get_int(&msg);
551 if (count != 1)
552 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
553
554 filename = buffer_get_string(&msg, NULL);
555 longname = buffer_get_string(&msg, NULL);
556 a = decode_attrib(&msg);
557
558 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
559
560 xfree(longname);
561
562 buffer_free(&msg);
563
564 return(filename);
565}
566
567int
568do_rename(int fd_in, int fd_out, char *oldpath, char *newpath)
569{
570 Buffer msg;
571 u_int status, id;
572
573 buffer_init(&msg);
574
575 /* Send rename request */
Ben Lindstrom288cc392001-02-09 02:58:04 +0000576 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100577 buffer_put_char(&msg, SSH2_FXP_RENAME);
578 buffer_put_int(&msg, id);
579 buffer_put_cstring(&msg, oldpath);
580 buffer_put_cstring(&msg, newpath);
581 send_msg(fd_out, &msg);
582 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
583 newpath);
584 buffer_free(&msg);
585
586 status = get_status(fd_in, id);
587 if (status != SSH2_FX_OK)
588 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
589 fx2txt(status));
590
591 return(status);
592}
593
594int
Damien Miller058316f2001-03-08 10:08:49 +1100595do_symlink(int fd_in, int fd_out, char *oldpath, char *newpath)
596{
597 Buffer msg;
598 u_int status, id;
599
600 buffer_init(&msg);
601
602 /* Send rename request */
603 id = msg_id++;
604 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
605 buffer_put_int(&msg, id);
606 buffer_put_cstring(&msg, oldpath);
607 buffer_put_cstring(&msg, newpath);
608 send_msg(fd_out, &msg);
609 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
610 newpath);
611 buffer_free(&msg);
612
613 status = get_status(fd_in, id);
614 if (status != SSH2_FX_OK)
615 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, newpath,
616 fx2txt(status));
617
618 return(status);
619}
620
621char *
622do_readlink(int fd_in, int fd_out, char *path)
623{
624 Buffer msg;
625 u_int type, expected_id, count, id;
626 char *filename, *longname;
627 Attrib *a;
628
629 expected_id = id = msg_id++;
630 send_string_request(fd_out, id, SSH2_FXP_READLINK, path, strlen(path));
631
632 buffer_init(&msg);
633
634 get_msg(fd_in, &msg);
635 type = buffer_get_char(&msg);
636 id = buffer_get_int(&msg);
637
638 if (id != expected_id)
639 fatal("ID mismatch (%d != %d)", id, expected_id);
640
641 if (type == SSH2_FXP_STATUS) {
642 u_int status = buffer_get_int(&msg);
643
644 error("Couldn't readlink: %s", fx2txt(status));
645 return(NULL);
646 } else if (type != SSH2_FXP_NAME)
647 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
648 SSH2_FXP_NAME, type);
649
650 count = buffer_get_int(&msg);
651 if (count != 1)
652 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
653
654 filename = buffer_get_string(&msg, NULL);
655 longname = buffer_get_string(&msg, NULL);
656 a = decode_attrib(&msg);
657
658 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
659
660 xfree(longname);
661
662 buffer_free(&msg);
663
664 return(filename);
665}
666
667int
Damien Miller33804262001-02-04 23:20:18 +1100668do_download(int fd_in, int fd_out, char *remote_path, char *local_path,
Damien Miller8829d362002-02-08 22:04:05 +1100669 int pflag, size_t buflen)
Damien Miller33804262001-02-04 23:20:18 +1100670{
Damien Miller8829d362002-02-08 22:04:05 +1100671 int local_fd, status;
Damien Miller33804262001-02-04 23:20:18 +1100672 u_int expected_id, handle_len, mode, type, id;
673 u_int64_t offset;
674 char *handle;
675 Buffer msg;
676 Attrib junk, *a;
677
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000678 a = do_stat(fd_in, fd_out, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100679 if (a == NULL)
680 return(-1);
681
682 /* XXX: should we preserve set[ug]id? */
683 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
684 mode = S_IWRITE | (a->perm & 0777);
685 else
686 mode = 0666;
687
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000688 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
689 (a->perm & S_IFDIR)) {
690 error("Cannot download a directory: %s", remote_path);
691 return(-1);
692 }
693
Damien Miller33804262001-02-04 23:20:18 +1100694 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
695 if (local_fd == -1) {
696 error("Couldn't open local file \"%s\" for writing: %s",
697 local_path, strerror(errno));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000698 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100699 }
700
Damien Miller33804262001-02-04 23:20:18 +1100701 buffer_init(&msg);
702
703 /* Send open request */
Ben Lindstrom288cc392001-02-09 02:58:04 +0000704 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100705 buffer_put_char(&msg, SSH2_FXP_OPEN);
706 buffer_put_int(&msg, id);
707 buffer_put_cstring(&msg, remote_path);
708 buffer_put_int(&msg, SSH2_FXF_READ);
709 attrib_clear(&junk); /* Send empty attributes */
710 encode_attrib(&msg, &junk);
711 send_msg(fd_out, &msg);
712 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
713
714 handle = get_handle(fd_in, id, &handle_len);
715 if (handle == NULL) {
716 buffer_free(&msg);
717 close(local_fd);
718 return(-1);
719 }
720
721 /* Read from remote and write to local */
722 offset = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100723 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +1100724 u_int len;
725 char *data;
726
Ben Lindstrom288cc392001-02-09 02:58:04 +0000727 id = expected_id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100728
729 buffer_clear(&msg);
730 buffer_put_char(&msg, SSH2_FXP_READ);
731 buffer_put_int(&msg, id);
732 buffer_put_string(&msg, handle, handle_len);
733 buffer_put_int64(&msg, offset);
Damien Miller8829d362002-02-08 22:04:05 +1100734 buffer_put_int(&msg, buflen);
Damien Miller33804262001-02-04 23:20:18 +1100735 send_msg(fd_out, &msg);
736 debug3("Sent message SSH2_FXP_READ I:%d O:%llu S:%u",
Damien Miller8829d362002-02-08 22:04:05 +1100737 id, (u_int64_t)offset, buflen);
Damien Miller33804262001-02-04 23:20:18 +1100738
739 buffer_clear(&msg);
740
741 get_msg(fd_in, &msg);
742 type = buffer_get_char(&msg);
743 id = buffer_get_int(&msg);
744 debug3("Received reply T:%d I:%d", type, id);
745 if (id != expected_id)
746 fatal("ID mismatch (%d != %d)", id, expected_id);
747 if (type == SSH2_FXP_STATUS) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100748 status = buffer_get_int(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100749
750 if (status == SSH2_FX_EOF)
751 break;
752 else {
753 error("Couldn't read from remote "
754 "file \"%s\" : %s", remote_path,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100755 fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100756 do_close(fd_in, fd_out, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +1100757 goto done;
Damien Miller33804262001-02-04 23:20:18 +1100758 }
759 } else if (type != SSH2_FXP_DATA) {
760 fatal("Expected SSH2_FXP_DATA(%d) packet, got %d",
761 SSH2_FXP_DATA, type);
762 }
763
764 data = buffer_get_string(&msg, &len);
Damien Miller8829d362002-02-08 22:04:05 +1100765 if (len > buflen)
Damien Miller33804262001-02-04 23:20:18 +1100766 fatal("Received more data than asked for %d > %d",
Damien Miller8829d362002-02-08 22:04:05 +1100767 len, buflen);
Damien Miller33804262001-02-04 23:20:18 +1100768
Damien Millerd7686fd2001-02-10 00:40:03 +1100769 debug3("In read loop, got %d offset %llu", len,
Ben Lindstrom416d8742001-02-25 02:02:43 +0000770 (u_int64_t)offset);
Damien Miller33804262001-02-04 23:20:18 +1100771 if (atomicio(write, local_fd, data, len) != len) {
772 error("Couldn't write to \"%s\": %s", local_path,
773 strerror(errno));
774 do_close(fd_in, fd_out, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +1100775 status = -1;
Damien Miller33804262001-02-04 23:20:18 +1100776 xfree(data);
Damien Millerd7686fd2001-02-10 00:40:03 +1100777 goto done;
Damien Miller33804262001-02-04 23:20:18 +1100778 }
779
780 offset += len;
781 xfree(data);
782 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100783 status = do_close(fd_in, fd_out, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100784
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000785 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000786#ifdef HAVE_FCHMOD
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000787 if (pflag && fchmod(local_fd, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000788#else
789 if (pflag && chmod(local_path, mode) == -1)
790#endif /* HAVE_FCHMOD */
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000791 error("Couldn't set mode on \"%s\": %s", local_path,
792 strerror(errno));
793 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
794 struct timeval tv[2];
795 tv[0].tv_sec = a->atime;
796 tv[1].tv_sec = a->mtime;
797 tv[0].tv_usec = tv[1].tv_usec = 0;
798 if (utimes(local_path, tv) == -1)
799 error("Can't set times on \"%s\": %s", local_path,
800 strerror(errno));
801 }
802
Damien Millerd7686fd2001-02-10 00:40:03 +1100803done:
804 close(local_fd);
805 buffer_free(&msg);
806 xfree(handle);
807 return status;
Damien Miller33804262001-02-04 23:20:18 +1100808}
809
810int
811do_upload(int fd_in, int fd_out, char *local_path, char *remote_path,
Damien Miller8829d362002-02-08 22:04:05 +1100812 int pflag, size_t buflen)
Damien Miller33804262001-02-04 23:20:18 +1100813{
Damien Miller8829d362002-02-08 22:04:05 +1100814 int local_fd, status;
Damien Miller33804262001-02-04 23:20:18 +1100815 u_int handle_len, id;
816 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100817 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100818 Buffer msg;
819 struct stat sb;
820 Attrib a;
821
822 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
823 error("Couldn't open local file \"%s\" for reading: %s",
824 local_path, strerror(errno));
825 return(-1);
826 }
827 if (fstat(local_fd, &sb) == -1) {
828 error("Couldn't fstat local file \"%s\": %s",
829 local_path, strerror(errno));
830 close(local_fd);
831 return(-1);
832 }
833 stat_to_attrib(&sb, &a);
834
835 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
836 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
837 a.perm &= 0777;
838 if (!pflag)
839 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
840
841 buffer_init(&msg);
842
843 /* Send open request */
Ben Lindstrom288cc392001-02-09 02:58:04 +0000844 id = msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100845 buffer_put_char(&msg, SSH2_FXP_OPEN);
846 buffer_put_int(&msg, id);
847 buffer_put_cstring(&msg, remote_path);
848 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
849 encode_attrib(&msg, &a);
850 send_msg(fd_out, &msg);
851 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
852
853 buffer_clear(&msg);
854
855 handle = get_handle(fd_in, id, &handle_len);
856 if (handle == NULL) {
857 close(local_fd);
858 buffer_free(&msg);
859 return(-1);
860 }
861
Damien Miller8829d362002-02-08 22:04:05 +1100862 data = xmalloc(buflen);
863
Damien Miller33804262001-02-04 23:20:18 +1100864 /* Read from local and write to remote */
865 offset = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100866 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +1100867 int len;
Damien Miller33804262001-02-04 23:20:18 +1100868
869 /*
870 * Can't use atomicio here because it returns 0 on EOF, thus losing
871 * the last block of the file
872 */
873 do
Damien Miller8829d362002-02-08 22:04:05 +1100874 len = read(local_fd, data, buflen);
Damien Miller33804262001-02-04 23:20:18 +1100875 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
876
877 if (len == -1)
878 fatal("Couldn't read from \"%s\": %s", local_path,
879 strerror(errno));
880 if (len == 0)
881 break;
882
883 buffer_clear(&msg);
884 buffer_put_char(&msg, SSH2_FXP_WRITE);
885 buffer_put_int(&msg, ++id);
886 buffer_put_string(&msg, handle, handle_len);
887 buffer_put_int64(&msg, offset);
888 buffer_put_string(&msg, data, len);
889 send_msg(fd_out, &msg);
890 debug3("Sent message SSH2_FXP_WRITE I:%d O:%llu S:%u",
Ben Lindstrom416d8742001-02-25 02:02:43 +0000891 id, (u_int64_t)offset, len);
Damien Miller33804262001-02-04 23:20:18 +1100892
893 status = get_status(fd_in, id);
894 if (status != SSH2_FX_OK) {
895 error("Couldn't write to remote file \"%s\": %s",
896 remote_path, fx2txt(status));
897 do_close(fd_in, fd_out, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100898 close(local_fd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100899 goto done;
Damien Miller33804262001-02-04 23:20:18 +1100900 }
Kevin Stevesec1c1402001-02-05 15:39:22 +0000901 debug3("In write loop, got %d offset %llu", len,
Ben Lindstrom416d8742001-02-25 02:02:43 +0000902 (u_int64_t)offset);
Damien Miller33804262001-02-04 23:20:18 +1100903
904 offset += len;
905 }
Damien Miller8829d362002-02-08 22:04:05 +1100906 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +1100907
908 if (close(local_fd) == -1) {
909 error("Couldn't close local file \"%s\": %s", local_path,
910 strerror(errno));
911 do_close(fd_in, fd_out, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +1100912 status = -1;
913 goto done;
Damien Miller33804262001-02-04 23:20:18 +1100914 }
915
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000916 /* Override umask and utimes if asked */
917 if (pflag)
918 do_fsetstat(fd_in, fd_out, handle, handle_len, &a);
919
Damien Millerd7686fd2001-02-10 00:40:03 +1100920 status = do_close(fd_in, fd_out, handle, handle_len);
921
922done:
923 xfree(handle);
924 buffer_free(&msg);
925 return status;
Damien Miller33804262001-02-04 23:20:18 +1100926}
Damien Miller4870afd2001-03-14 10:27:09 +1100927