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