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