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