]>
Commit | Line | Data |
---|---|---|
536c14e8 | 1 | /* $OpenBSD: sftp-client.c,v 1.72 2006/07/26 02:35:17 stevesk 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> |
4095f623 | 27 | #ifdef HAVE_SYS_STAT_H |
28 | # include <sys/stat.h> | |
29 | #endif | |
e264ac72 | 30 | #ifdef HAVE_SYS_TIME_H |
31 | # include <sys/time.h> | |
32 | #endif | |
d3221cca | 33 | |
028094f4 | 34 | #include <errno.h> |
d3221cca | 35 | #include <fcntl.h> |
36 | #include <signal.h> | |
00146caa | 37 | #include <string.h> |
5188ba17 | 38 | #include <unistd.h> |
c25d3df7 | 39 | |
9fd2a215 | 40 | #include "openbsd-compat/sys-queue.h" |
61e96248 | 41 | |
61e96248 | 42 | #include "buffer.h" |
43 | #include "bufaux.h" | |
61e96248 | 44 | #include "xmalloc.h" |
45 | #include "log.h" | |
46 | #include "atomicio.h" | |
b65c3807 | 47 | #include "progressmeter.h" |
51e7a012 | 48 | #include "misc.h" |
61e96248 | 49 | |
50 | #include "sftp.h" | |
51 | #include "sftp-common.h" | |
52 | #include "sftp-client.h" | |
53 | ||
0e5de6f8 | 54 | extern volatile sig_atomic_t interrupted; |
b65c3807 | 55 | extern int showprogress; |
56 | ||
2db34ac9 | 57 | /* Minimum amount of data to read at a time */ |
c25d3df7 | 58 | #define MIN_READ_SIZE 512 |
59 | ||
22e6c827 | 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 | }; | |
9c5a8165 | 68 | |
396c147e | 69 | static void |
61e96248 | 70 | send_msg(int fd, Buffer *m) |
71 | { | |
bf0bf24b | 72 | u_char mlen[4]; |
2c4369de | 73 | struct iovec iov[2]; |
bf0bf24b | 74 | |
3eee3b86 | 75 | if (buffer_len(m) > SFTP_MAX_MSG_LENGTH) |
bf0bf24b | 76 | fatal("Outbound message too long %u", buffer_len(m)); |
61e96248 | 77 | |
bf0bf24b | 78 | /* Send length first */ |
51e7a012 | 79 | put_u32(mlen, buffer_len(m)); |
2c4369de | 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)) | |
61e96248 | 86 | fatal("Couldn't send packet: %s", strerror(errno)); |
87 | ||
bf0bf24b | 88 | buffer_clear(m); |
61e96248 | 89 | } |
90 | ||
396c147e | 91 | static void |
61e96248 | 92 | get_msg(int fd, Buffer *m) |
93 | { | |
bf0bf24b | 94 | u_int msg_len; |
61e96248 | 95 | |
bf0bf24b | 96 | buffer_append_space(m, 4); |
05624c18 | 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 | } | |
61e96248 | 103 | |
bf0bf24b | 104 | msg_len = buffer_get_int(m); |
3eee3b86 | 105 | if (msg_len > SFTP_MAX_MSG_LENGTH) |
9906a836 | 106 | fatal("Received message too long %u", msg_len); |
61e96248 | 107 | |
bf0bf24b | 108 | buffer_append_space(m, msg_len); |
05624c18 | 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 | } | |
61e96248 | 115 | } |
116 | ||
396c147e | 117 | static void |
61e96248 | 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); | |
9906a836 | 128 | debug3("Sent message fd %d T:%u I:%u", fd, code, id); |
61e96248 | 129 | buffer_free(&msg); |
130 | } | |
131 | ||
396c147e | 132 | static void |
61e96248 | 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); | |
9906a836 | 144 | debug3("Sent message fd %d T:%u I:%u", fd, code, id); |
61e96248 | 145 | buffer_free(&msg); |
146 | } | |
147 | ||
396c147e | 148 | static u_int |
9906a836 | 149 | get_status(int fd, u_int expected_id) |
61e96248 | 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) | |
9906a836 | 160 | fatal("ID mismatch (%u != %u)", id, expected_id); |
61e96248 | 161 | if (type != SSH2_FXP_STATUS) |
9906a836 | 162 | fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u", |
61e96248 | 163 | SSH2_FXP_STATUS, type); |
164 | ||
165 | status = buffer_get_int(&msg); | |
166 | buffer_free(&msg); | |
167 | ||
9906a836 | 168 | debug3("SSH2_FXP_STATUS %u", status); |
61e96248 | 169 | |
170 | return(status); | |
171 | } | |
172 | ||
396c147e | 173 | static char * |
61e96248 | 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) | |
9906a836 | 186 | fatal("ID mismatch (%u != %u)", id, expected_id); |
61e96248 | 187 | if (type == SSH2_FXP_STATUS) { |
188 | int status = buffer_get_int(&msg); | |
189 | ||
190 | error("Couldn't get handle: %s", fx2txt(status)); | |
aa41be57 | 191 | buffer_free(&msg); |
61e96248 | 192 | return(NULL); |
193 | } else if (type != SSH2_FXP_HANDLE) | |
9906a836 | 194 | fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u", |
61e96248 | 195 | SSH2_FXP_HANDLE, type); |
196 | ||
197 | handle = buffer_get_string(&msg, len); | |
198 | buffer_free(&msg); | |
199 | ||
200 | return(handle); | |
201 | } | |
202 | ||
396c147e | 203 | static Attrib * |
d50d9b63 | 204 | get_decode_stat(int fd, u_int expected_id, int quiet) |
61e96248 | 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 | ||
9906a836 | 216 | debug3("Received stat reply T:%u I:%u", type, id); |
61e96248 | 217 | if (id != expected_id) |
9906a836 | 218 | fatal("ID mismatch (%u != %u)", id, expected_id); |
61e96248 | 219 | if (type == SSH2_FXP_STATUS) { |
220 | int status = buffer_get_int(&msg); | |
221 | ||
d50d9b63 | 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)); | |
aa41be57 | 226 | buffer_free(&msg); |
61e96248 | 227 | return(NULL); |
228 | } else if (type != SSH2_FXP_ATTRS) { | |
9906a836 | 229 | fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u", |
61e96248 | 230 | SSH2_FXP_ATTRS, type); |
231 | } | |
232 | a = decode_attrib(&msg); | |
233 | buffer_free(&msg); | |
234 | ||
235 | return(a); | |
236 | } | |
237 | ||
22e6c827 | 238 | struct sftp_conn * |
239 | do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests) | |
61e96248 | 240 | { |
9906a836 | 241 | u_int type; |
242 | int version; | |
61e96248 | 243 | Buffer msg; |
22e6c827 | 244 | struct sftp_conn *ret; |
61e96248 | 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 | ||
2b87da3b | 255 | /* Expecting a VERSION reply */ |
61e96248 | 256 | if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) { |
9906a836 | 257 | error("Invalid packet back from SSH2_FXP_INIT (type %u)", |
61e96248 | 258 | type); |
259 | buffer_free(&msg); | |
22e6c827 | 260 | return(NULL); |
61e96248 | 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); | |
3a7fe5ba | 277 | |
22e6c827 | 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) | |
92053302 | 288 | ret->transfer_buflen = MIN(ret->transfer_buflen, 20480); |
22e6c827 | 289 | |
290 | return(ret); | |
291 | } | |
292 | ||
293 | u_int | |
294 | sftp_proto_version(struct sftp_conn *conn) | |
295 | { | |
296 | return(conn->version); | |
61e96248 | 297 | } |
298 | ||
299 | int | |
22e6c827 | 300 | do_close(struct sftp_conn *conn, char *handle, u_int handle_len) |
61e96248 | 301 | { |
302 | u_int id, status; | |
303 | Buffer msg; | |
304 | ||
305 | buffer_init(&msg); | |
306 | ||
22e6c827 | 307 | id = conn->msg_id++; |
61e96248 | 308 | buffer_put_char(&msg, SSH2_FXP_CLOSE); |
309 | buffer_put_int(&msg, id); | |
310 | buffer_put_string(&msg, handle, handle_len); | |
22e6c827 | 311 | send_msg(conn->fd_out, &msg); |
9906a836 | 312 | debug3("Sent message SSH2_FXP_CLOSE I:%u", id); |
61e96248 | 313 | |
22e6c827 | 314 | status = get_status(conn->fd_in, id); |
61e96248 | 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 | ||
2e4fb373 | 323 | |
396c147e | 324 | static int |
22e6c827 | 325 | do_lsreaddir(struct sftp_conn *conn, char *path, int printflag, |
2e4fb373 | 326 | SFTP_DIRENT ***dir) |
61e96248 | 327 | { |
328 | Buffer msg; | |
2ceb8101 | 329 | u_int count, type, id, handle_len, i, expected_id, ents = 0; |
61e96248 | 330 | char *handle; |
331 | ||
22e6c827 | 332 | id = conn->msg_id++; |
61e96248 | 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); | |
22e6c827 | 338 | send_msg(conn->fd_out, &msg); |
61e96248 | 339 | |
340 | buffer_clear(&msg); | |
341 | ||
22e6c827 | 342 | handle = get_handle(conn->fd_in, id, &handle_len); |
61e96248 | 343 | if (handle == NULL) |
344 | return(-1); | |
345 | ||
2e4fb373 | 346 | if (dir) { |
347 | ents = 0; | |
348 | *dir = xmalloc(sizeof(**dir)); | |
349 | (*dir)[0] = NULL; | |
350 | } | |
2e4fb373 | 351 | |
0e5de6f8 | 352 | for (; !interrupted;) { |
22e6c827 | 353 | id = expected_id = conn->msg_id++; |
61e96248 | 354 | |
9906a836 | 355 | debug3("Sending SSH2_FXP_READDIR I:%u", id); |
61e96248 | 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); | |
22e6c827 | 361 | send_msg(conn->fd_out, &msg); |
61e96248 | 362 | |
363 | buffer_clear(&msg); | |
364 | ||
22e6c827 | 365 | get_msg(conn->fd_in, &msg); |
61e96248 | 366 | |
367 | type = buffer_get_char(&msg); | |
368 | id = buffer_get_int(&msg); | |
369 | ||
9906a836 | 370 | debug3("Received reply T:%u I:%u", type, id); |
61e96248 | 371 | |
372 | if (id != expected_id) | |
9906a836 | 373 | fatal("ID mismatch (%u != %u)", id, expected_id); |
61e96248 | 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)); | |
22e6c827 | 385 | do_close(conn, handle, handle_len); |
3e2f2431 | 386 | xfree(handle); |
b655a207 | 387 | return(status); |
61e96248 | 388 | } |
389 | } else if (type != SSH2_FXP_NAME) | |
9906a836 | 390 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
61e96248 | 391 | SSH2_FXP_NAME, type); |
392 | ||
393 | count = buffer_get_int(&msg); | |
0426a3b4 | 394 | if (count == 0) |
395 | break; | |
396 | debug3("Received %d SSH2_FXP_NAME responses", count); | |
184eed6a | 397 | for (i = 0; i < count; i++) { |
61e96248 | 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 | ||
2e4fb373 | 405 | if (printflag) |
406 | printf("%s\n", longname); | |
407 | ||
408 | if (dir) { | |
c5d10563 | 409 | *dir = xrealloc(*dir, ents + 2, sizeof(**dir)); |
2e4fb373 | 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 | } | |
61e96248 | 416 | |
417 | xfree(filename); | |
418 | xfree(longname); | |
419 | } | |
420 | } | |
421 | ||
422 | buffer_free(&msg); | |
22e6c827 | 423 | do_close(conn, handle, handle_len); |
61e96248 | 424 | xfree(handle); |
425 | ||
0e5de6f8 | 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 | ||
61e96248 | 433 | return(0); |
434 | } | |
435 | ||
2e4fb373 | 436 | int |
22e6c827 | 437 | do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir) |
2e4fb373 | 438 | { |
22e6c827 | 439 | return(do_lsreaddir(conn, path, 0, dir)); |
2e4fb373 | 440 | } |
441 | ||
442 | void free_sftp_dirents(SFTP_DIRENT **s) | |
443 | { | |
444 | int i; | |
184eed6a | 445 | |
446 | for (i = 0; s[i]; i++) { | |
2e4fb373 | 447 | xfree(s[i]->filename); |
448 | xfree(s[i]->longname); | |
449 | xfree(s[i]); | |
450 | } | |
451 | xfree(s); | |
452 | } | |
453 | ||
61e96248 | 454 | int |
22e6c827 | 455 | do_rm(struct sftp_conn *conn, char *path) |
61e96248 | 456 | { |
457 | u_int status, id; | |
458 | ||
459 | debug2("Sending SSH2_FXP_REMOVE \"%s\"", path); | |
460 | ||
22e6c827 | 461 | id = conn->msg_id++; |
762715ce | 462 | send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path, |
22e6c827 | 463 | strlen(path)); |
464 | status = get_status(conn->fd_in, id); | |
61e96248 | 465 | if (status != SSH2_FX_OK) |
466 | error("Couldn't delete file: %s", fx2txt(status)); | |
467 | return(status); | |
468 | } | |
469 | ||
470 | int | |
22e6c827 | 471 | do_mkdir(struct sftp_conn *conn, char *path, Attrib *a) |
61e96248 | 472 | { |
473 | u_int status, id; | |
474 | ||
22e6c827 | 475 | id = conn->msg_id++; |
476 | send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path, | |
61e96248 | 477 | strlen(path), a); |
478 | ||
22e6c827 | 479 | status = get_status(conn->fd_in, id); |
61e96248 | 480 | if (status != SSH2_FX_OK) |
481 | error("Couldn't create directory: %s", fx2txt(status)); | |
482 | ||
483 | return(status); | |
484 | } | |
485 | ||
486 | int | |
22e6c827 | 487 | do_rmdir(struct sftp_conn *conn, char *path) |
61e96248 | 488 | { |
489 | u_int status, id; | |
490 | ||
22e6c827 | 491 | id = conn->msg_id++; |
492 | send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path, | |
493 | strlen(path)); | |
61e96248 | 494 | |
22e6c827 | 495 | status = get_status(conn->fd_in, id); |
61e96248 | 496 | if (status != SSH2_FX_OK) |
497 | error("Couldn't remove directory: %s", fx2txt(status)); | |
498 | ||
499 | return(status); | |
500 | } | |
501 | ||
502 | Attrib * | |
22e6c827 | 503 | do_stat(struct sftp_conn *conn, char *path, int quiet) |
61e96248 | 504 | { |
505 | u_int id; | |
506 | ||
22e6c827 | 507 | id = conn->msg_id++; |
508 | ||
762715ce | 509 | send_string_request(conn->fd_out, id, |
510 | conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT, | |
22e6c827 | 511 | path, strlen(path)); |
512 | ||
513 | return(get_decode_stat(conn->fd_in, id, quiet)); | |
61e96248 | 514 | } |
515 | ||
516 | Attrib * | |
22e6c827 | 517 | do_lstat(struct sftp_conn *conn, char *path, int quiet) |
61e96248 | 518 | { |
519 | u_int id; | |
520 | ||
22e6c827 | 521 | if (conn->version == 0) { |
522 | if (quiet) | |
523 | debug("Server version does not support lstat operation"); | |
524 | else | |
bbe88b6d | 525 | logit("Server version does not support lstat operation"); |
9c74a24d | 526 | return(do_stat(conn, path, quiet)); |
22e6c827 | 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)); | |
61e96248 | 534 | } |
535 | ||
536 | Attrib * | |
22e6c827 | 537 | do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet) |
61e96248 | 538 | { |
539 | u_int id; | |
540 | ||
22e6c827 | 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)); | |
61e96248 | 546 | } |
547 | ||
548 | int | |
22e6c827 | 549 | do_setstat(struct sftp_conn *conn, char *path, Attrib *a) |
61e96248 | 550 | { |
551 | u_int status, id; | |
552 | ||
22e6c827 | 553 | id = conn->msg_id++; |
554 | send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path, | |
61e96248 | 555 | strlen(path), a); |
556 | ||
22e6c827 | 557 | status = get_status(conn->fd_in, id); |
61e96248 | 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 | |
22e6c827 | 566 | do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len, |
61e96248 | 567 | Attrib *a) |
568 | { | |
569 | u_int status, id; | |
570 | ||
22e6c827 | 571 | id = conn->msg_id++; |
572 | send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle, | |
61e96248 | 573 | handle_len, a); |
574 | ||
22e6c827 | 575 | status = get_status(conn->fd_in, id); |
61e96248 | 576 | if (status != SSH2_FX_OK) |
577 | error("Couldn't fsetstat: %s", fx2txt(status)); | |
578 | ||
579 | return(status); | |
580 | } | |
581 | ||
582 | char * | |
22e6c827 | 583 | do_realpath(struct sftp_conn *conn, char *path) |
61e96248 | 584 | { |
585 | Buffer msg; | |
586 | u_int type, expected_id, count, id; | |
587 | char *filename, *longname; | |
588 | Attrib *a; | |
589 | ||
22e6c827 | 590 | expected_id = id = conn->msg_id++; |
591 | send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path, | |
592 | strlen(path)); | |
61e96248 | 593 | |
594 | buffer_init(&msg); | |
595 | ||
22e6c827 | 596 | get_msg(conn->fd_in, &msg); |
61e96248 | 597 | type = buffer_get_char(&msg); |
598 | id = buffer_get_int(&msg); | |
599 | ||
600 | if (id != expected_id) | |
9906a836 | 601 | fatal("ID mismatch (%u != %u)", id, expected_id); |
61e96248 | 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) | |
9906a836 | 609 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
61e96248 | 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 | |
22e6c827 | 630 | do_rename(struct sftp_conn *conn, char *oldpath, char *newpath) |
61e96248 | 631 | { |
632 | Buffer msg; | |
633 | u_int status, id; | |
634 | ||
635 | buffer_init(&msg); | |
636 | ||
637 | /* Send rename request */ | |
22e6c827 | 638 | id = conn->msg_id++; |
61e96248 | 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); | |
22e6c827 | 643 | send_msg(conn->fd_out, &msg); |
61e96248 | 644 | debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath, |
645 | newpath); | |
646 | buffer_free(&msg); | |
647 | ||
22e6c827 | 648 | status = get_status(conn->fd_in, id); |
61e96248 | 649 | if (status != SSH2_FX_OK) |
22e6c827 | 650 | error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, |
651 | newpath, fx2txt(status)); | |
61e96248 | 652 | |
653 | return(status); | |
654 | } | |
655 | ||
3a7fe5ba | 656 | int |
22e6c827 | 657 | do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath) |
3a7fe5ba | 658 | { |
659 | Buffer msg; | |
660 | u_int status, id; | |
661 | ||
22e6c827 | 662 | if (conn->version < 3) { |
663 | error("This server does not support the symlink operation"); | |
664 | return(SSH2_FX_OP_UNSUPPORTED); | |
665 | } | |
666 | ||
3a7fe5ba | 667 | buffer_init(&msg); |
668 | ||
b093b499 | 669 | /* Send symlink request */ |
22e6c827 | 670 | id = conn->msg_id++; |
3a7fe5ba | 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); | |
22e6c827 | 675 | send_msg(conn->fd_out, &msg); |
3a7fe5ba | 676 | debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath, |
677 | newpath); | |
678 | buffer_free(&msg); | |
679 | ||
22e6c827 | 680 | status = get_status(conn->fd_in, id); |
3a7fe5ba | 681 | if (status != SSH2_FX_OK) |
9db1a8e9 | 682 | error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath, |
22e6c827 | 683 | newpath, fx2txt(status)); |
3a7fe5ba | 684 | |
685 | return(status); | |
686 | } | |
687 | ||
688 | char * | |
22e6c827 | 689 | do_readlink(struct sftp_conn *conn, char *path) |
3a7fe5ba | 690 | { |
691 | Buffer msg; | |
692 | u_int type, expected_id, count, id; | |
693 | char *filename, *longname; | |
694 | Attrib *a; | |
695 | ||
22e6c827 | 696 | expected_id = id = conn->msg_id++; |
697 | send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path, | |
698 | strlen(path)); | |
3a7fe5ba | 699 | |
700 | buffer_init(&msg); | |
701 | ||
22e6c827 | 702 | get_msg(conn->fd_in, &msg); |
3a7fe5ba | 703 | type = buffer_get_char(&msg); |
704 | id = buffer_get_int(&msg); | |
705 | ||
706 | if (id != expected_id) | |
9906a836 | 707 | fatal("ID mismatch (%u != %u)", id, expected_id); |
3a7fe5ba | 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) | |
9906a836 | 715 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
3a7fe5ba | 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 | ||
c25d3df7 | 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; | |
762715ce | 740 | |
c25d3df7 | 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); | |
762715ce | 750 | } |
c25d3df7 | 751 | |
61e96248 | 752 | int |
22e6c827 | 753 | do_download(struct sftp_conn *conn, char *remote_path, char *local_path, |
754 | int pflag) | |
61e96248 | 755 | { |
61e96248 | 756 | Attrib junk, *a; |
c25d3df7 | 757 | Buffer msg; |
758 | char *handle; | |
a056dfa2 | 759 | int local_fd, status = 0, write_error; |
c25d3df7 | 760 | int read_error, write_errno; |
761 | u_int64_t offset, size; | |
2ceb8101 | 762 | u_int handle_len, mode, type, id, buflen, num_req, max_req; |
b65c3807 | 763 | off_t progress_counter; |
c25d3df7 | 764 | struct request { |
765 | u_int id; | |
766 | u_int len; | |
767 | u_int64_t offset; | |
762715ce | 768 | TAILQ_ENTRY(request) tq; |
c25d3df7 | 769 | }; |
770 | TAILQ_HEAD(reqhead, request) requests; | |
771 | struct request *req; | |
772 | ||
773 | TAILQ_INIT(&requests); | |
61e96248 | 774 | |
22e6c827 | 775 | a = do_stat(conn, remote_path, 0); |
61e96248 | 776 | if (a == NULL) |
777 | return(-1); | |
778 | ||
779 | /* XXX: should we preserve set[ug]id? */ | |
780 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) | |
bfd934b9 | 781 | mode = a->perm & 0777; |
61e96248 | 782 | else |
783 | mode = 0666; | |
784 | ||
d50d9b63 | 785 | if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
538840a2 | 786 | (!S_ISREG(a->perm))) { |
787 | error("Cannot download non-regular file: %s", remote_path); | |
d50d9b63 | 788 | return(-1); |
789 | } | |
790 | ||
c25d3df7 | 791 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) |
792 | size = a->size; | |
793 | else | |
794 | size = 0; | |
795 | ||
22e6c827 | 796 | buflen = conn->transfer_buflen; |
61e96248 | 797 | buffer_init(&msg); |
798 | ||
799 | /* Send open request */ | |
22e6c827 | 800 | id = conn->msg_id++; |
61e96248 | 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); | |
22e6c827 | 807 | send_msg(conn->fd_out, &msg); |
9906a836 | 808 | debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path); |
61e96248 | 809 | |
22e6c827 | 810 | handle = get_handle(conn->fd_in, id, &handle_len); |
61e96248 | 811 | if (handle == NULL) { |
812 | buffer_free(&msg); | |
61e96248 | 813 | return(-1); |
814 | } | |
815 | ||
aff51935 | 816 | local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, |
bfd934b9 | 817 | mode | S_IWRITE); |
22e6c827 | 818 | if (local_fd == -1) { |
819 | error("Couldn't open local file \"%s\" for writing: %s", | |
820 | local_path, strerror(errno)); | |
f60ace9f | 821 | buffer_free(&msg); |
822 | xfree(handle); | |
22e6c827 | 823 | return(-1); |
824 | } | |
825 | ||
61e96248 | 826 | /* Read from remote and write to local */ |
c25d3df7 | 827 | write_error = read_error = write_errno = num_req = offset = 0; |
828 | max_req = 1; | |
b65c3807 | 829 | progress_counter = 0; |
830 | ||
213bab61 | 831 | if (showprogress && size != 0) |
832 | start_progress_meter(remote_path, size, &progress_counter); | |
b65c3807 | 833 | |
c25d3df7 | 834 | while (num_req > 0 || max_req > 0) { |
61e96248 | 835 | char *data; |
c25d3df7 | 836 | u_int len; |
61e96248 | 837 | |
0e5de6f8 | 838 | /* |
f2107e97 | 839 | * Simulate EOF on interrupt: stop sending new requests and |
0e5de6f8 | 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 | ||
c25d3df7 | 848 | /* Send some more requests */ |
849 | while (num_req < max_req) { | |
762715ce | 850 | debug3("Request range %llu -> %llu (%d/%d)", |
8627f3e0 | 851 | (unsigned long long)offset, |
852 | (unsigned long long)offset + buflen - 1, | |
853 | num_req, max_req); | |
c25d3df7 | 854 | req = xmalloc(sizeof(*req)); |
22e6c827 | 855 | req->id = conn->msg_id++; |
c25d3df7 | 856 | req->len = buflen; |
857 | req->offset = offset; | |
858 | offset += buflen; | |
859 | num_req++; | |
860 | TAILQ_INSERT_TAIL(&requests, req, tq); | |
762715ce | 861 | send_read_request(conn->fd_out, req->id, req->offset, |
c25d3df7 | 862 | req->len, handle, handle_len); |
863 | } | |
61e96248 | 864 | |
865 | buffer_clear(&msg); | |
22e6c827 | 866 | get_msg(conn->fd_in, &msg); |
61e96248 | 867 | type = buffer_get_char(&msg); |
868 | id = buffer_get_int(&msg); | |
9906a836 | 869 | debug3("Received reply T:%u I:%u R:%d", type, id, max_req); |
c25d3df7 | 870 | |
871 | /* Find the request in our queue */ | |
f8cc7664 | 872 | for (req = TAILQ_FIRST(&requests); |
c25d3df7 | 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: | |
0426a3b4 | 881 | status = buffer_get_int(&msg); |
c25d3df7 | 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); | |
bfa7f960 | 891 | debug3("Received data %llu -> %llu", |
762715ce | 892 | (unsigned long long)req->offset, |
bfa7f960 | 893 | (unsigned long long)req->offset + len - 1); |
c25d3df7 | 894 | if (len > req->len) |
895 | fatal("Received more data than asked for " | |
b77a87e5 | 896 | "%u > %u", len, req->len); |
c25d3df7 | 897 | if ((lseek(local_fd, req->offset, SEEK_SET) == -1 || |
dc54438a | 898 | atomicio(vwrite, local_fd, data, len) != len) && |
c25d3df7 | 899 | !write_error) { |
900 | write_errno = errno; | |
901 | write_error = 1; | |
902 | max_req = 0; | |
903 | } | |
b65c3807 | 904 | progress_counter += len; |
c25d3df7 | 905 | xfree(data); |
61e96248 | 906 | |
c25d3df7 | 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 " | |
bfa7f960 | 914 | "%llu -> %llu (%2d)", |
762715ce | 915 | (unsigned long long)req->offset + len, |
5fc7dbc9 | 916 | (unsigned long long)req->offset + |
917 | req->len - 1, num_req); | |
22e6c827 | 918 | req->id = conn->msg_id++; |
c25d3df7 | 919 | req->len -= len; |
920 | req->offset += len; | |
762715ce | 921 | send_read_request(conn->fd_out, req->id, |
22e6c827 | 922 | req->offset, req->len, handle, handle_len); |
c25d3df7 | 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)", | |
bfa7f960 | 932 | (unsigned long long)offset, |
933 | num_req); | |
c25d3df7 | 934 | max_req = 1; |
0e5de6f8 | 935 | } else if (max_req <= conn->num_requests) { |
c25d3df7 | 936 | ++max_req; |
937 | } | |
61e96248 | 938 | } |
c25d3df7 | 939 | break; |
940 | default: | |
9906a836 | 941 | fatal("Expected SSH2_FXP_DATA(%u) packet, got %u", |
61e96248 | 942 | SSH2_FXP_DATA, type); |
943 | } | |
61e96248 | 944 | } |
61e96248 | 945 | |
b65c3807 | 946 | if (showprogress && size) |
947 | stop_progress_meter(); | |
948 | ||
c25d3df7 | 949 | /* Sanity check */ |
950 | if (TAILQ_FIRST(&requests) != NULL) | |
951 | fatal("Transfer complete, but requests still in queue"); | |
952 | ||
953 | if (read_error) { | |
762715ce | 954 | error("Couldn't read from remote file \"%s\" : %s", |
22e6c827 | 955 | remote_path, fx2txt(status)); |
956 | do_close(conn, handle, handle_len); | |
c25d3df7 | 957 | } else if (write_error) { |
22e6c827 | 958 | error("Couldn't write to \"%s\": %s", local_path, |
959 | strerror(write_errno)); | |
960 | status = -1; | |
961 | do_close(conn, handle, handle_len); | |
c25d3df7 | 962 | } else { |
22e6c827 | 963 | status = do_close(conn, handle, handle_len); |
c25d3df7 | 964 | |
965 | /* Override umask and utimes if asked */ | |
663fd560 | 966 | #ifdef HAVE_FCHMOD |
c25d3df7 | 967 | if (pflag && fchmod(local_fd, mode) == -1) |
aff51935 | 968 | #else |
c25d3df7 | 969 | if (pflag && chmod(local_path, mode) == -1) |
663fd560 | 970 | #endif /* HAVE_FCHMOD */ |
c25d3df7 | 971 | error("Couldn't set mode on \"%s\": %s", local_path, |
b77a87e5 | 972 | strerror(errno)); |
c25d3df7 | 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", | |
b77a87e5 | 980 | local_path, strerror(errno)); |
c25d3df7 | 981 | } |
58c54a79 | 982 | } |
0426a3b4 | 983 | close(local_fd); |
984 | buffer_free(&msg); | |
985 | xfree(handle); | |
22e6c827 | 986 | |
987 | return(status); | |
61e96248 | 988 | } |
989 | ||
990 | int | |
22e6c827 | 991 | do_upload(struct sftp_conn *conn, char *local_path, char *remote_path, |
992 | int pflag) | |
61e96248 | 993 | { |
375f867e | 994 | int local_fd, status; |
b2bab059 | 995 | u_int handle_len, id, type; |
61e96248 | 996 | u_int64_t offset; |
375f867e | 997 | char *handle, *data; |
61e96248 | 998 | Buffer msg; |
999 | struct stat sb; | |
1000 | Attrib a; | |
c25d3df7 | 1001 | u_int32_t startid; |
1002 | u_int32_t ackid; | |
b2bab059 | 1003 | struct outstanding_ack { |
1004 | u_int id; | |
1005 | u_int len; | |
1006 | u_int64_t offset; | |
762715ce | 1007 | TAILQ_ENTRY(outstanding_ack) tq; |
b2bab059 | 1008 | }; |
1009 | TAILQ_HEAD(ackhead, outstanding_ack) acks; | |
6e007f08 | 1010 | struct outstanding_ack *ack = NULL; |
b2bab059 | 1011 | |
1012 | TAILQ_INIT(&acks); | |
61e96248 | 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 | } | |
538840a2 | 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 | } | |
61e96248 | 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 */ | |
22e6c827 | 1041 | id = conn->msg_id++; |
61e96248 | 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); | |
22e6c827 | 1047 | send_msg(conn->fd_out, &msg); |
9906a836 | 1048 | debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path); |
61e96248 | 1049 | |
1050 | buffer_clear(&msg); | |
1051 | ||
22e6c827 | 1052 | handle = get_handle(conn->fd_in, id, &handle_len); |
61e96248 | 1053 | if (handle == NULL) { |
1054 | close(local_fd); | |
1055 | buffer_free(&msg); | |
1056 | return(-1); | |
1057 | } | |
1058 | ||
c25d3df7 | 1059 | startid = ackid = id + 1; |
22e6c827 | 1060 | data = xmalloc(conn->transfer_buflen); |
375f867e | 1061 | |
61e96248 | 1062 | /* Read from local and write to remote */ |
1063 | offset = 0; | |
b65c3807 | 1064 | if (showprogress) |
1065 | start_progress_meter(local_path, sb.st_size, &offset); | |
b65c3807 | 1066 | |
184eed6a | 1067 | for (;;) { |
61e96248 | 1068 | int len; |
61e96248 | 1069 | |
1070 | /* | |
f2107e97 | 1071 | * Can't use atomicio here because it returns 0 on EOF, |
0e5de6f8 | 1072 | * thus losing the last block of the file. |
f2107e97 | 1073 | * Simulate an EOF on interrupt, allowing ACKs from the |
0e5de6f8 | 1074 | * server to drain. |
61e96248 | 1075 | */ |
0e5de6f8 | 1076 | if (interrupted) |
1077 | len = 0; | |
1078 | else do | |
22e6c827 | 1079 | len = read(local_fd, data, conn->transfer_buflen); |
61e96248 | 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)); | |
c25d3df7 | 1085 | |
1086 | if (len != 0) { | |
b2bab059 | 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 | ||
c25d3df7 | 1093 | buffer_clear(&msg); |
1094 | buffer_put_char(&msg, SSH2_FXP_WRITE); | |
b2bab059 | 1095 | buffer_put_int(&msg, ack->id); |
c25d3df7 | 1096 | buffer_put_string(&msg, handle, handle_len); |
1097 | buffer_put_int64(&msg, offset); | |
1098 | buffer_put_string(&msg, data, len); | |
22e6c827 | 1099 | send_msg(conn->fd_out, &msg); |
9906a836 | 1100 | debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u", |
b77a87e5 | 1101 | id, (unsigned long long)offset, len); |
b2bab059 | 1102 | } else if (TAILQ_FIRST(&acks) == NULL) |
61e96248 | 1103 | break; |
1104 | ||
b2bab059 | 1105 | if (ack == NULL) |
1106 | fatal("Unexpected ACK %u", id); | |
1107 | ||
762715ce | 1108 | if (id == startid || len == 0 || |
22e6c827 | 1109 | id - ackid >= conn->num_requests) { |
875ec275 | 1110 | u_int r_id; |
af2b3cd9 | 1111 | |
b2bab059 | 1112 | buffer_clear(&msg); |
22e6c827 | 1113 | get_msg(conn->fd_in, &msg); |
b2bab059 | 1114 | type = buffer_get_char(&msg); |
af2b3cd9 | 1115 | r_id = buffer_get_int(&msg); |
b2bab059 | 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 */ | |
f8cc7664 | 1125 | for (ack = TAILQ_FIRST(&acks); |
af2b3cd9 | 1126 | ack != NULL && ack->id != r_id; |
b2bab059 | 1127 | ack = TAILQ_NEXT(ack, tq)) |
1128 | ; | |
1129 | if (ack == NULL) | |
9906a836 | 1130 | fatal("Can't find request for ID %u", r_id); |
b2bab059 | 1131 | TAILQ_REMOVE(&acks, ack, tq); |
1132 | ||
c25d3df7 | 1133 | if (status != SSH2_FX_OK) { |
1134 | error("Couldn't write to remote file \"%s\": %s", | |
b77a87e5 | 1135 | remote_path, fx2txt(status)); |
22e6c827 | 1136 | do_close(conn, handle, handle_len); |
c25d3df7 | 1137 | close(local_fd); |
3e2f2431 | 1138 | xfree(data); |
1139 | xfree(ack); | |
c25d3df7 | 1140 | goto done; |
1141 | } | |
9906a836 | 1142 | debug3("In write loop, ack for %u %u bytes at %llu", |
4e2e5cfd | 1143 | ack->id, ack->len, (unsigned long long)ack->offset); |
c25d3df7 | 1144 | ++ackid; |
30e37ee6 | 1145 | xfree(ack); |
61e96248 | 1146 | } |
61e96248 | 1147 | offset += len; |
1148 | } | |
b65c3807 | 1149 | if (showprogress) |
1150 | stop_progress_meter(); | |
375f867e | 1151 | xfree(data); |
61e96248 | 1152 | |
1153 | if (close(local_fd) == -1) { | |
1154 | error("Couldn't close local file \"%s\": %s", local_path, | |
1155 | strerror(errno)); | |
22e6c827 | 1156 | do_close(conn, handle, handle_len); |
0426a3b4 | 1157 | status = -1; |
1158 | goto done; | |
61e96248 | 1159 | } |
1160 | ||
58c54a79 | 1161 | /* Override umask and utimes if asked */ |
1162 | if (pflag) | |
22e6c827 | 1163 | do_fsetstat(conn, handle, handle_len, &a); |
58c54a79 | 1164 | |
22e6c827 | 1165 | status = do_close(conn, handle, handle_len); |
0426a3b4 | 1166 | |
1167 | done: | |
1168 | xfree(handle); | |
1169 | buffer_free(&msg); | |
22e6c827 | 1170 | return(status); |
61e96248 | 1171 | } |