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