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