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