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