]>
Commit | Line | Data |
---|---|---|
681efe9f | 1 | /* $OpenBSD: sftp-client.c,v 1.86 2008/06/26 06:10:09 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> |
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); | |
ad39a852 | 329 | int known = 0; |
61e96248 | 330 | |
360b43ab | 331 | if (strcmp(name, "posix-rename@openssh.com") == 0 && |
ad39a852 | 332 | strcmp(value, "1") == 0) { |
530f04a8 | 333 | exts |= SFTP_EXT_POSIX_RENAME; |
ad39a852 | 334 | known = 1; |
335 | } else if (strcmp(name, "statvfs@openssh.com") == 0 && | |
336 | strcmp(value, "2") == 0) { | |
360b43ab | 337 | exts |= SFTP_EXT_STATVFS; |
ad39a852 | 338 | known = 1; |
339 | } if (strcmp(name, "fstatvfs@openssh.com") == 0 && | |
340 | strcmp(value, "2") == 0) { | |
360b43ab | 341 | exts |= SFTP_EXT_FSTATVFS; |
ad39a852 | 342 | known = 1; |
343 | } | |
344 | if (known) { | |
345 | debug2("Server supports extension \"%s\" revision %s", | |
346 | name, value); | |
347 | } else { | |
348 | debug2("Unrecognised server extension \"%s\"", name); | |
349 | } | |
61e96248 | 350 | xfree(name); |
351 | xfree(value); | |
352 | } | |
353 | ||
354 | buffer_free(&msg); | |
3a7fe5ba | 355 | |
22e6c827 | 356 | ret = xmalloc(sizeof(*ret)); |
357 | ret->fd_in = fd_in; | |
358 | ret->fd_out = fd_out; | |
359 | ret->transfer_buflen = transfer_buflen; | |
360 | ret->num_requests = num_requests; | |
361 | ret->version = version; | |
362 | ret->msg_id = 1; | |
530f04a8 | 363 | ret->exts = exts; |
22e6c827 | 364 | |
365 | /* Some filexfer v.0 servers don't support large packets */ | |
366 | if (version == 0) | |
92053302 | 367 | ret->transfer_buflen = MIN(ret->transfer_buflen, 20480); |
22e6c827 | 368 | |
369 | return(ret); | |
370 | } | |
371 | ||
372 | u_int | |
373 | sftp_proto_version(struct sftp_conn *conn) | |
374 | { | |
375 | return(conn->version); | |
61e96248 | 376 | } |
377 | ||
378 | int | |
22e6c827 | 379 | do_close(struct sftp_conn *conn, char *handle, u_int handle_len) |
61e96248 | 380 | { |
381 | u_int id, status; | |
382 | Buffer msg; | |
383 | ||
384 | buffer_init(&msg); | |
385 | ||
22e6c827 | 386 | id = conn->msg_id++; |
61e96248 | 387 | buffer_put_char(&msg, SSH2_FXP_CLOSE); |
388 | buffer_put_int(&msg, id); | |
389 | buffer_put_string(&msg, handle, handle_len); | |
22e6c827 | 390 | send_msg(conn->fd_out, &msg); |
9906a836 | 391 | debug3("Sent message SSH2_FXP_CLOSE I:%u", id); |
61e96248 | 392 | |
22e6c827 | 393 | status = get_status(conn->fd_in, id); |
61e96248 | 394 | if (status != SSH2_FX_OK) |
395 | error("Couldn't close file: %s", fx2txt(status)); | |
396 | ||
397 | buffer_free(&msg); | |
398 | ||
399 | return(status); | |
400 | } | |
401 | ||
2e4fb373 | 402 | |
396c147e | 403 | static int |
22e6c827 | 404 | do_lsreaddir(struct sftp_conn *conn, char *path, int printflag, |
2e4fb373 | 405 | SFTP_DIRENT ***dir) |
61e96248 | 406 | { |
407 | Buffer msg; | |
2ceb8101 | 408 | u_int count, type, id, handle_len, i, expected_id, ents = 0; |
61e96248 | 409 | char *handle; |
410 | ||
22e6c827 | 411 | id = conn->msg_id++; |
61e96248 | 412 | |
413 | buffer_init(&msg); | |
414 | buffer_put_char(&msg, SSH2_FXP_OPENDIR); | |
415 | buffer_put_int(&msg, id); | |
416 | buffer_put_cstring(&msg, path); | |
22e6c827 | 417 | send_msg(conn->fd_out, &msg); |
61e96248 | 418 | |
419 | buffer_clear(&msg); | |
420 | ||
22e6c827 | 421 | handle = get_handle(conn->fd_in, id, &handle_len); |
61e96248 | 422 | if (handle == NULL) |
423 | return(-1); | |
424 | ||
2e4fb373 | 425 | if (dir) { |
426 | ents = 0; | |
427 | *dir = xmalloc(sizeof(**dir)); | |
428 | (*dir)[0] = NULL; | |
429 | } | |
2e4fb373 | 430 | |
0e5de6f8 | 431 | for (; !interrupted;) { |
22e6c827 | 432 | id = expected_id = conn->msg_id++; |
61e96248 | 433 | |
9906a836 | 434 | debug3("Sending SSH2_FXP_READDIR I:%u", id); |
61e96248 | 435 | |
436 | buffer_clear(&msg); | |
437 | buffer_put_char(&msg, SSH2_FXP_READDIR); | |
438 | buffer_put_int(&msg, id); | |
439 | buffer_put_string(&msg, handle, handle_len); | |
22e6c827 | 440 | send_msg(conn->fd_out, &msg); |
61e96248 | 441 | |
442 | buffer_clear(&msg); | |
443 | ||
22e6c827 | 444 | get_msg(conn->fd_in, &msg); |
61e96248 | 445 | |
446 | type = buffer_get_char(&msg); | |
447 | id = buffer_get_int(&msg); | |
448 | ||
9906a836 | 449 | debug3("Received reply T:%u I:%u", type, id); |
61e96248 | 450 | |
451 | if (id != expected_id) | |
9906a836 | 452 | fatal("ID mismatch (%u != %u)", id, expected_id); |
61e96248 | 453 | |
454 | if (type == SSH2_FXP_STATUS) { | |
455 | int status = buffer_get_int(&msg); | |
456 | ||
457 | debug3("Received SSH2_FXP_STATUS %d", status); | |
458 | ||
459 | if (status == SSH2_FX_EOF) { | |
460 | break; | |
461 | } else { | |
462 | error("Couldn't read directory: %s", | |
463 | fx2txt(status)); | |
22e6c827 | 464 | do_close(conn, handle, handle_len); |
3e2f2431 | 465 | xfree(handle); |
b655a207 | 466 | return(status); |
61e96248 | 467 | } |
468 | } else if (type != SSH2_FXP_NAME) | |
9906a836 | 469 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
61e96248 | 470 | SSH2_FXP_NAME, type); |
471 | ||
472 | count = buffer_get_int(&msg); | |
0426a3b4 | 473 | if (count == 0) |
474 | break; | |
475 | debug3("Received %d SSH2_FXP_NAME responses", count); | |
184eed6a | 476 | for (i = 0; i < count; i++) { |
61e96248 | 477 | char *filename, *longname; |
478 | Attrib *a; | |
479 | ||
480 | filename = buffer_get_string(&msg, NULL); | |
481 | longname = buffer_get_string(&msg, NULL); | |
482 | a = decode_attrib(&msg); | |
483 | ||
2e4fb373 | 484 | if (printflag) |
485 | printf("%s\n", longname); | |
486 | ||
487 | if (dir) { | |
c5d10563 | 488 | *dir = xrealloc(*dir, ents + 2, sizeof(**dir)); |
2e4fb373 | 489 | (*dir)[ents] = xmalloc(sizeof(***dir)); |
490 | (*dir)[ents]->filename = xstrdup(filename); | |
491 | (*dir)[ents]->longname = xstrdup(longname); | |
492 | memcpy(&(*dir)[ents]->a, a, sizeof(*a)); | |
493 | (*dir)[++ents] = NULL; | |
494 | } | |
61e96248 | 495 | |
496 | xfree(filename); | |
497 | xfree(longname); | |
498 | } | |
499 | } | |
500 | ||
501 | buffer_free(&msg); | |
22e6c827 | 502 | do_close(conn, handle, handle_len); |
61e96248 | 503 | xfree(handle); |
504 | ||
0e5de6f8 | 505 | /* Don't return partial matches on interrupt */ |
506 | if (interrupted && dir != NULL && *dir != NULL) { | |
507 | free_sftp_dirents(*dir); | |
508 | *dir = xmalloc(sizeof(**dir)); | |
509 | **dir = NULL; | |
510 | } | |
511 | ||
61e96248 | 512 | return(0); |
513 | } | |
514 | ||
2e4fb373 | 515 | int |
22e6c827 | 516 | do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir) |
2e4fb373 | 517 | { |
22e6c827 | 518 | return(do_lsreaddir(conn, path, 0, dir)); |
2e4fb373 | 519 | } |
520 | ||
521 | void free_sftp_dirents(SFTP_DIRENT **s) | |
522 | { | |
523 | int i; | |
184eed6a | 524 | |
525 | for (i = 0; s[i]; i++) { | |
2e4fb373 | 526 | xfree(s[i]->filename); |
527 | xfree(s[i]->longname); | |
528 | xfree(s[i]); | |
529 | } | |
530 | xfree(s); | |
531 | } | |
532 | ||
61e96248 | 533 | int |
22e6c827 | 534 | do_rm(struct sftp_conn *conn, char *path) |
61e96248 | 535 | { |
536 | u_int status, id; | |
537 | ||
538 | debug2("Sending SSH2_FXP_REMOVE \"%s\"", path); | |
539 | ||
22e6c827 | 540 | id = conn->msg_id++; |
762715ce | 541 | send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path, |
22e6c827 | 542 | strlen(path)); |
543 | status = get_status(conn->fd_in, id); | |
61e96248 | 544 | if (status != SSH2_FX_OK) |
545 | error("Couldn't delete file: %s", fx2txt(status)); | |
546 | return(status); | |
547 | } | |
548 | ||
549 | int | |
22e6c827 | 550 | do_mkdir(struct sftp_conn *conn, char *path, Attrib *a) |
61e96248 | 551 | { |
552 | u_int status, id; | |
553 | ||
22e6c827 | 554 | id = conn->msg_id++; |
555 | send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path, | |
61e96248 | 556 | strlen(path), a); |
557 | ||
22e6c827 | 558 | status = get_status(conn->fd_in, id); |
61e96248 | 559 | if (status != SSH2_FX_OK) |
560 | error("Couldn't create directory: %s", fx2txt(status)); | |
561 | ||
562 | return(status); | |
563 | } | |
564 | ||
565 | int | |
22e6c827 | 566 | do_rmdir(struct sftp_conn *conn, char *path) |
61e96248 | 567 | { |
568 | u_int status, id; | |
569 | ||
22e6c827 | 570 | id = conn->msg_id++; |
571 | send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path, | |
572 | strlen(path)); | |
61e96248 | 573 | |
22e6c827 | 574 | status = get_status(conn->fd_in, id); |
61e96248 | 575 | if (status != SSH2_FX_OK) |
576 | error("Couldn't remove directory: %s", fx2txt(status)); | |
577 | ||
578 | return(status); | |
579 | } | |
580 | ||
581 | Attrib * | |
22e6c827 | 582 | do_stat(struct sftp_conn *conn, char *path, int quiet) |
61e96248 | 583 | { |
584 | u_int id; | |
585 | ||
22e6c827 | 586 | id = conn->msg_id++; |
587 | ||
762715ce | 588 | send_string_request(conn->fd_out, id, |
589 | conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT, | |
22e6c827 | 590 | path, strlen(path)); |
591 | ||
592 | return(get_decode_stat(conn->fd_in, id, quiet)); | |
61e96248 | 593 | } |
594 | ||
595 | Attrib * | |
22e6c827 | 596 | do_lstat(struct sftp_conn *conn, char *path, int quiet) |
61e96248 | 597 | { |
598 | u_int id; | |
599 | ||
22e6c827 | 600 | if (conn->version == 0) { |
601 | if (quiet) | |
602 | debug("Server version does not support lstat operation"); | |
603 | else | |
bbe88b6d | 604 | logit("Server version does not support lstat operation"); |
9c74a24d | 605 | return(do_stat(conn, path, quiet)); |
22e6c827 | 606 | } |
607 | ||
608 | id = conn->msg_id++; | |
609 | send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path, | |
610 | strlen(path)); | |
611 | ||
612 | return(get_decode_stat(conn->fd_in, id, quiet)); | |
61e96248 | 613 | } |
614 | ||
737cce6f | 615 | #ifdef notyet |
61e96248 | 616 | Attrib * |
22e6c827 | 617 | do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet) |
61e96248 | 618 | { |
619 | u_int id; | |
620 | ||
22e6c827 | 621 | id = conn->msg_id++; |
622 | send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle, | |
623 | handle_len); | |
624 | ||
625 | return(get_decode_stat(conn->fd_in, id, quiet)); | |
61e96248 | 626 | } |
737cce6f | 627 | #endif |
61e96248 | 628 | |
629 | int | |
22e6c827 | 630 | do_setstat(struct sftp_conn *conn, char *path, Attrib *a) |
61e96248 | 631 | { |
632 | u_int status, id; | |
633 | ||
22e6c827 | 634 | id = conn->msg_id++; |
635 | send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path, | |
61e96248 | 636 | strlen(path), a); |
637 | ||
22e6c827 | 638 | status = get_status(conn->fd_in, id); |
61e96248 | 639 | if (status != SSH2_FX_OK) |
640 | error("Couldn't setstat on \"%s\": %s", path, | |
641 | fx2txt(status)); | |
642 | ||
643 | return(status); | |
644 | } | |
645 | ||
646 | int | |
22e6c827 | 647 | do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len, |
61e96248 | 648 | Attrib *a) |
649 | { | |
650 | u_int status, id; | |
651 | ||
22e6c827 | 652 | id = conn->msg_id++; |
653 | send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle, | |
61e96248 | 654 | handle_len, a); |
655 | ||
22e6c827 | 656 | status = get_status(conn->fd_in, id); |
61e96248 | 657 | if (status != SSH2_FX_OK) |
658 | error("Couldn't fsetstat: %s", fx2txt(status)); | |
659 | ||
660 | return(status); | |
661 | } | |
662 | ||
663 | char * | |
22e6c827 | 664 | do_realpath(struct sftp_conn *conn, char *path) |
61e96248 | 665 | { |
666 | Buffer msg; | |
667 | u_int type, expected_id, count, id; | |
668 | char *filename, *longname; | |
669 | Attrib *a; | |
670 | ||
22e6c827 | 671 | expected_id = id = conn->msg_id++; |
672 | send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path, | |
673 | strlen(path)); | |
61e96248 | 674 | |
675 | buffer_init(&msg); | |
676 | ||
22e6c827 | 677 | get_msg(conn->fd_in, &msg); |
61e96248 | 678 | type = buffer_get_char(&msg); |
679 | id = buffer_get_int(&msg); | |
680 | ||
681 | if (id != expected_id) | |
9906a836 | 682 | fatal("ID mismatch (%u != %u)", id, expected_id); |
61e96248 | 683 | |
684 | if (type == SSH2_FXP_STATUS) { | |
685 | u_int status = buffer_get_int(&msg); | |
686 | ||
687 | error("Couldn't canonicalise: %s", fx2txt(status)); | |
688 | return(NULL); | |
689 | } else if (type != SSH2_FXP_NAME) | |
9906a836 | 690 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
61e96248 | 691 | SSH2_FXP_NAME, type); |
692 | ||
693 | count = buffer_get_int(&msg); | |
694 | if (count != 1) | |
695 | fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count); | |
696 | ||
697 | filename = buffer_get_string(&msg, NULL); | |
698 | longname = buffer_get_string(&msg, NULL); | |
699 | a = decode_attrib(&msg); | |
700 | ||
701 | debug3("SSH_FXP_REALPATH %s -> %s", path, filename); | |
702 | ||
703 | xfree(longname); | |
704 | ||
705 | buffer_free(&msg); | |
706 | ||
707 | return(filename); | |
708 | } | |
709 | ||
710 | int | |
22e6c827 | 711 | do_rename(struct sftp_conn *conn, char *oldpath, char *newpath) |
61e96248 | 712 | { |
713 | Buffer msg; | |
714 | u_int status, id; | |
715 | ||
716 | buffer_init(&msg); | |
717 | ||
718 | /* Send rename request */ | |
22e6c827 | 719 | id = conn->msg_id++; |
530f04a8 | 720 | if ((conn->exts & SFTP_EXT_POSIX_RENAME)) { |
721 | buffer_put_char(&msg, SSH2_FXP_EXTENDED); | |
722 | buffer_put_int(&msg, id); | |
723 | buffer_put_cstring(&msg, "posix-rename@openssh.com"); | |
724 | } else { | |
725 | buffer_put_char(&msg, SSH2_FXP_RENAME); | |
726 | buffer_put_int(&msg, id); | |
727 | } | |
61e96248 | 728 | buffer_put_cstring(&msg, oldpath); |
729 | buffer_put_cstring(&msg, newpath); | |
22e6c827 | 730 | send_msg(conn->fd_out, &msg); |
530f04a8 | 731 | debug3("Sent message %s \"%s\" -> \"%s\"", |
732 | (conn->exts & SFTP_EXT_POSIX_RENAME) ? "posix-rename@openssh.com" : | |
733 | "SSH2_FXP_RENAME", oldpath, newpath); | |
61e96248 | 734 | buffer_free(&msg); |
735 | ||
22e6c827 | 736 | status = get_status(conn->fd_in, id); |
61e96248 | 737 | if (status != SSH2_FX_OK) |
22e6c827 | 738 | error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath, |
739 | newpath, fx2txt(status)); | |
61e96248 | 740 | |
741 | return(status); | |
742 | } | |
743 | ||
3a7fe5ba | 744 | int |
22e6c827 | 745 | do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath) |
3a7fe5ba | 746 | { |
747 | Buffer msg; | |
748 | u_int status, id; | |
749 | ||
22e6c827 | 750 | if (conn->version < 3) { |
751 | error("This server does not support the symlink operation"); | |
752 | return(SSH2_FX_OP_UNSUPPORTED); | |
753 | } | |
754 | ||
3a7fe5ba | 755 | buffer_init(&msg); |
756 | ||
b093b499 | 757 | /* Send symlink request */ |
22e6c827 | 758 | id = conn->msg_id++; |
3a7fe5ba | 759 | buffer_put_char(&msg, SSH2_FXP_SYMLINK); |
760 | buffer_put_int(&msg, id); | |
761 | buffer_put_cstring(&msg, oldpath); | |
762 | buffer_put_cstring(&msg, newpath); | |
22e6c827 | 763 | send_msg(conn->fd_out, &msg); |
3a7fe5ba | 764 | debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath, |
765 | newpath); | |
766 | buffer_free(&msg); | |
767 | ||
22e6c827 | 768 | status = get_status(conn->fd_in, id); |
3a7fe5ba | 769 | if (status != SSH2_FX_OK) |
9db1a8e9 | 770 | error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath, |
22e6c827 | 771 | newpath, fx2txt(status)); |
3a7fe5ba | 772 | |
773 | return(status); | |
774 | } | |
775 | ||
737cce6f | 776 | #ifdef notyet |
3a7fe5ba | 777 | char * |
22e6c827 | 778 | do_readlink(struct sftp_conn *conn, char *path) |
3a7fe5ba | 779 | { |
780 | Buffer msg; | |
781 | u_int type, expected_id, count, id; | |
782 | char *filename, *longname; | |
783 | Attrib *a; | |
784 | ||
22e6c827 | 785 | expected_id = id = conn->msg_id++; |
786 | send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path, | |
787 | strlen(path)); | |
3a7fe5ba | 788 | |
789 | buffer_init(&msg); | |
790 | ||
22e6c827 | 791 | get_msg(conn->fd_in, &msg); |
3a7fe5ba | 792 | type = buffer_get_char(&msg); |
793 | id = buffer_get_int(&msg); | |
794 | ||
795 | if (id != expected_id) | |
9906a836 | 796 | fatal("ID mismatch (%u != %u)", id, expected_id); |
3a7fe5ba | 797 | |
798 | if (type == SSH2_FXP_STATUS) { | |
799 | u_int status = buffer_get_int(&msg); | |
800 | ||
801 | error("Couldn't readlink: %s", fx2txt(status)); | |
802 | return(NULL); | |
803 | } else if (type != SSH2_FXP_NAME) | |
9906a836 | 804 | fatal("Expected SSH2_FXP_NAME(%u) packet, got %u", |
3a7fe5ba | 805 | SSH2_FXP_NAME, type); |
806 | ||
807 | count = buffer_get_int(&msg); | |
808 | if (count != 1) | |
809 | fatal("Got multiple names (%d) from SSH_FXP_READLINK", count); | |
810 | ||
811 | filename = buffer_get_string(&msg, NULL); | |
812 | longname = buffer_get_string(&msg, NULL); | |
813 | a = decode_attrib(&msg); | |
814 | ||
815 | debug3("SSH_FXP_READLINK %s -> %s", path, filename); | |
816 | ||
817 | xfree(longname); | |
818 | ||
819 | buffer_free(&msg); | |
820 | ||
821 | return(filename); | |
822 | } | |
737cce6f | 823 | #endif |
3a7fe5ba | 824 | |
360b43ab | 825 | int |
5a3cde15 | 826 | do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st, |
360b43ab | 827 | int quiet) |
828 | { | |
829 | Buffer msg; | |
830 | u_int id; | |
831 | ||
832 | if ((conn->exts & SFTP_EXT_STATVFS) == 0) { | |
833 | error("Server does not support statvfs@openssh.com extension"); | |
834 | return -1; | |
835 | } | |
836 | ||
837 | id = conn->msg_id++; | |
838 | ||
839 | buffer_init(&msg); | |
840 | buffer_clear(&msg); | |
841 | buffer_put_char(&msg, SSH2_FXP_EXTENDED); | |
842 | buffer_put_int(&msg, id); | |
843 | buffer_put_cstring(&msg, "statvfs@openssh.com"); | |
844 | buffer_put_cstring(&msg, path); | |
845 | send_msg(conn->fd_out, &msg); | |
846 | buffer_free(&msg); | |
847 | ||
848 | return get_decode_statvfs(conn->fd_in, st, id, quiet); | |
849 | } | |
850 | ||
851 | #ifdef notyet | |
852 | int | |
853 | do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len, | |
5a3cde15 | 854 | struct sftp_statvfs *st, int quiet) |
360b43ab | 855 | { |
856 | Buffer msg; | |
857 | u_int id; | |
858 | ||
859 | if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) { | |
860 | error("Server does not support fstatvfs@openssh.com extension"); | |
861 | return -1; | |
862 | } | |
863 | ||
864 | id = conn->msg_id++; | |
865 | ||
866 | buffer_init(&msg); | |
867 | buffer_clear(&msg); | |
868 | buffer_put_char(&msg, SSH2_FXP_EXTENDED); | |
869 | buffer_put_int(&msg, id); | |
870 | buffer_put_cstring(&msg, "fstatvfs@openssh.com"); | |
871 | buffer_put_string(&msg, handle, handle_len); | |
872 | send_msg(conn->fd_out, &msg); | |
873 | buffer_free(&msg); | |
874 | ||
875 | return get_decode_statvfs(conn->fd_in, st, id, quiet); | |
876 | } | |
877 | #endif | |
878 | ||
c25d3df7 | 879 | static void |
880 | send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len, | |
881 | char *handle, u_int handle_len) | |
882 | { | |
883 | Buffer msg; | |
762715ce | 884 | |
c25d3df7 | 885 | buffer_init(&msg); |
886 | buffer_clear(&msg); | |
887 | buffer_put_char(&msg, SSH2_FXP_READ); | |
888 | buffer_put_int(&msg, id); | |
889 | buffer_put_string(&msg, handle, handle_len); | |
890 | buffer_put_int64(&msg, offset); | |
891 | buffer_put_int(&msg, len); | |
892 | send_msg(fd_out, &msg); | |
893 | buffer_free(&msg); | |
762715ce | 894 | } |
c25d3df7 | 895 | |
61e96248 | 896 | int |
22e6c827 | 897 | do_download(struct sftp_conn *conn, char *remote_path, char *local_path, |
898 | int pflag) | |
61e96248 | 899 | { |
61e96248 | 900 | Attrib junk, *a; |
c25d3df7 | 901 | Buffer msg; |
902 | char *handle; | |
a056dfa2 | 903 | int local_fd, status = 0, write_error; |
c25d3df7 | 904 | int read_error, write_errno; |
905 | u_int64_t offset, size; | |
2ceb8101 | 906 | u_int handle_len, mode, type, id, buflen, num_req, max_req; |
b65c3807 | 907 | off_t progress_counter; |
c25d3df7 | 908 | struct request { |
909 | u_int id; | |
910 | u_int len; | |
911 | u_int64_t offset; | |
762715ce | 912 | TAILQ_ENTRY(request) tq; |
c25d3df7 | 913 | }; |
914 | TAILQ_HEAD(reqhead, request) requests; | |
915 | struct request *req; | |
916 | ||
917 | TAILQ_INIT(&requests); | |
61e96248 | 918 | |
22e6c827 | 919 | a = do_stat(conn, remote_path, 0); |
61e96248 | 920 | if (a == NULL) |
921 | return(-1); | |
922 | ||
681efe9f | 923 | /* Do not preserve set[ug]id here, as we do not preserve ownership */ |
61e96248 | 924 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) |
bfd934b9 | 925 | mode = a->perm & 0777; |
61e96248 | 926 | else |
927 | mode = 0666; | |
928 | ||
d50d9b63 | 929 | if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
538840a2 | 930 | (!S_ISREG(a->perm))) { |
931 | error("Cannot download non-regular file: %s", remote_path); | |
d50d9b63 | 932 | return(-1); |
933 | } | |
934 | ||
c25d3df7 | 935 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) |
936 | size = a->size; | |
937 | else | |
938 | size = 0; | |
939 | ||
22e6c827 | 940 | buflen = conn->transfer_buflen; |
61e96248 | 941 | buffer_init(&msg); |
942 | ||
943 | /* Send open request */ | |
22e6c827 | 944 | id = conn->msg_id++; |
61e96248 | 945 | buffer_put_char(&msg, SSH2_FXP_OPEN); |
946 | buffer_put_int(&msg, id); | |
947 | buffer_put_cstring(&msg, remote_path); | |
948 | buffer_put_int(&msg, SSH2_FXF_READ); | |
949 | attrib_clear(&junk); /* Send empty attributes */ | |
950 | encode_attrib(&msg, &junk); | |
22e6c827 | 951 | send_msg(conn->fd_out, &msg); |
9906a836 | 952 | debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path); |
61e96248 | 953 | |
22e6c827 | 954 | handle = get_handle(conn->fd_in, id, &handle_len); |
61e96248 | 955 | if (handle == NULL) { |
956 | buffer_free(&msg); | |
61e96248 | 957 | return(-1); |
958 | } | |
959 | ||
aff51935 | 960 | local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, |
bfd934b9 | 961 | mode | S_IWRITE); |
22e6c827 | 962 | if (local_fd == -1) { |
963 | error("Couldn't open local file \"%s\" for writing: %s", | |
964 | local_path, strerror(errno)); | |
1c861236 | 965 | do_close(conn, handle, handle_len); |
f60ace9f | 966 | buffer_free(&msg); |
967 | xfree(handle); | |
22e6c827 | 968 | return(-1); |
969 | } | |
970 | ||
61e96248 | 971 | /* Read from remote and write to local */ |
c25d3df7 | 972 | write_error = read_error = write_errno = num_req = offset = 0; |
973 | max_req = 1; | |
b65c3807 | 974 | progress_counter = 0; |
975 | ||
213bab61 | 976 | if (showprogress && size != 0) |
977 | start_progress_meter(remote_path, size, &progress_counter); | |
b65c3807 | 978 | |
c25d3df7 | 979 | while (num_req > 0 || max_req > 0) { |
61e96248 | 980 | char *data; |
c25d3df7 | 981 | u_int len; |
61e96248 | 982 | |
0e5de6f8 | 983 | /* |
f2107e97 | 984 | * Simulate EOF on interrupt: stop sending new requests and |
0e5de6f8 | 985 | * allow outstanding requests to drain gracefully |
986 | */ | |
987 | if (interrupted) { | |
988 | if (num_req == 0) /* If we haven't started yet... */ | |
989 | break; | |
990 | max_req = 0; | |
991 | } | |
992 | ||
c25d3df7 | 993 | /* Send some more requests */ |
994 | while (num_req < max_req) { | |
762715ce | 995 | debug3("Request range %llu -> %llu (%d/%d)", |
8627f3e0 | 996 | (unsigned long long)offset, |
997 | (unsigned long long)offset + buflen - 1, | |
998 | num_req, max_req); | |
c25d3df7 | 999 | req = xmalloc(sizeof(*req)); |
22e6c827 | 1000 | req->id = conn->msg_id++; |
c25d3df7 | 1001 | req->len = buflen; |
1002 | req->offset = offset; | |
1003 | offset += buflen; | |
1004 | num_req++; | |
1005 | TAILQ_INSERT_TAIL(&requests, req, tq); | |
762715ce | 1006 | send_read_request(conn->fd_out, req->id, req->offset, |
c25d3df7 | 1007 | req->len, handle, handle_len); |
1008 | } | |
61e96248 | 1009 | |
1010 | buffer_clear(&msg); | |
22e6c827 | 1011 | get_msg(conn->fd_in, &msg); |
61e96248 | 1012 | type = buffer_get_char(&msg); |
1013 | id = buffer_get_int(&msg); | |
9906a836 | 1014 | debug3("Received reply T:%u I:%u R:%d", type, id, max_req); |
c25d3df7 | 1015 | |
1016 | /* Find the request in our queue */ | |
f8cc7664 | 1017 | for (req = TAILQ_FIRST(&requests); |
c25d3df7 | 1018 | req != NULL && req->id != id; |
1019 | req = TAILQ_NEXT(req, tq)) | |
1020 | ; | |
1021 | if (req == NULL) | |
1022 | fatal("Unexpected reply %u", id); | |
1023 | ||
1024 | switch (type) { | |
1025 | case SSH2_FXP_STATUS: | |
0426a3b4 | 1026 | status = buffer_get_int(&msg); |
c25d3df7 | 1027 | if (status != SSH2_FX_EOF) |
1028 | read_error = 1; | |
1029 | max_req = 0; | |
1030 | TAILQ_REMOVE(&requests, req, tq); | |
1031 | xfree(req); | |
1032 | num_req--; | |
1033 | break; | |
1034 | case SSH2_FXP_DATA: | |
1035 | data = buffer_get_string(&msg, &len); | |
bfa7f960 | 1036 | debug3("Received data %llu -> %llu", |
762715ce | 1037 | (unsigned long long)req->offset, |
bfa7f960 | 1038 | (unsigned long long)req->offset + len - 1); |
c25d3df7 | 1039 | if (len > req->len) |
1040 | fatal("Received more data than asked for " | |
b77a87e5 | 1041 | "%u > %u", len, req->len); |
c25d3df7 | 1042 | if ((lseek(local_fd, req->offset, SEEK_SET) == -1 || |
dc54438a | 1043 | atomicio(vwrite, local_fd, data, len) != len) && |
c25d3df7 | 1044 | !write_error) { |
1045 | write_errno = errno; | |
1046 | write_error = 1; | |
1047 | max_req = 0; | |
1048 | } | |
b65c3807 | 1049 | progress_counter += len; |
c25d3df7 | 1050 | xfree(data); |
61e96248 | 1051 | |
c25d3df7 | 1052 | if (len == req->len) { |
1053 | TAILQ_REMOVE(&requests, req, tq); | |
1054 | xfree(req); | |
1055 | num_req--; | |
1056 | } else { | |
1057 | /* Resend the request for the missing data */ | |
1058 | debug3("Short data block, re-requesting " | |
bfa7f960 | 1059 | "%llu -> %llu (%2d)", |
762715ce | 1060 | (unsigned long long)req->offset + len, |
5fc7dbc9 | 1061 | (unsigned long long)req->offset + |
1062 | req->len - 1, num_req); | |
22e6c827 | 1063 | req->id = conn->msg_id++; |
c25d3df7 | 1064 | req->len -= len; |
1065 | req->offset += len; | |
762715ce | 1066 | send_read_request(conn->fd_out, req->id, |
22e6c827 | 1067 | req->offset, req->len, handle, handle_len); |
c25d3df7 | 1068 | /* Reduce the request size */ |
1069 | if (len < buflen) | |
1070 | buflen = MAX(MIN_READ_SIZE, len); | |
1071 | } | |
1072 | if (max_req > 0) { /* max_req = 0 iff EOF received */ | |
1073 | if (size > 0 && offset > size) { | |
1074 | /* Only one request at a time | |
1075 | * after the expected EOF */ | |
1076 | debug3("Finish at %llu (%2d)", | |
bfa7f960 | 1077 | (unsigned long long)offset, |
1078 | num_req); | |
c25d3df7 | 1079 | max_req = 1; |
0e5de6f8 | 1080 | } else if (max_req <= conn->num_requests) { |
c25d3df7 | 1081 | ++max_req; |
1082 | } | |
61e96248 | 1083 | } |
c25d3df7 | 1084 | break; |
1085 | default: | |
9906a836 | 1086 | fatal("Expected SSH2_FXP_DATA(%u) packet, got %u", |
61e96248 | 1087 | SSH2_FXP_DATA, type); |
1088 | } | |
61e96248 | 1089 | } |
61e96248 | 1090 | |
b65c3807 | 1091 | if (showprogress && size) |
1092 | stop_progress_meter(); | |
1093 | ||
c25d3df7 | 1094 | /* Sanity check */ |
1095 | if (TAILQ_FIRST(&requests) != NULL) | |
1096 | fatal("Transfer complete, but requests still in queue"); | |
1097 | ||
1098 | if (read_error) { | |
762715ce | 1099 | error("Couldn't read from remote file \"%s\" : %s", |
22e6c827 | 1100 | remote_path, fx2txt(status)); |
1101 | do_close(conn, handle, handle_len); | |
c25d3df7 | 1102 | } else if (write_error) { |
22e6c827 | 1103 | error("Couldn't write to \"%s\": %s", local_path, |
1104 | strerror(write_errno)); | |
1105 | status = -1; | |
1106 | do_close(conn, handle, handle_len); | |
c25d3df7 | 1107 | } else { |
22e6c827 | 1108 | status = do_close(conn, handle, handle_len); |
c25d3df7 | 1109 | |
1110 | /* Override umask and utimes if asked */ | |
663fd560 | 1111 | #ifdef HAVE_FCHMOD |
c25d3df7 | 1112 | if (pflag && fchmod(local_fd, mode) == -1) |
aff51935 | 1113 | #else |
c25d3df7 | 1114 | if (pflag && chmod(local_path, mode) == -1) |
663fd560 | 1115 | #endif /* HAVE_FCHMOD */ |
c25d3df7 | 1116 | error("Couldn't set mode on \"%s\": %s", local_path, |
b77a87e5 | 1117 | strerror(errno)); |
c25d3df7 | 1118 | if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) { |
1119 | struct timeval tv[2]; | |
1120 | tv[0].tv_sec = a->atime; | |
1121 | tv[1].tv_sec = a->mtime; | |
1122 | tv[0].tv_usec = tv[1].tv_usec = 0; | |
1123 | if (utimes(local_path, tv) == -1) | |
1124 | error("Can't set times on \"%s\": %s", | |
b77a87e5 | 1125 | local_path, strerror(errno)); |
c25d3df7 | 1126 | } |
58c54a79 | 1127 | } |
0426a3b4 | 1128 | close(local_fd); |
1129 | buffer_free(&msg); | |
1130 | xfree(handle); | |
22e6c827 | 1131 | |
1132 | return(status); | |
61e96248 | 1133 | } |
1134 | ||
1135 | int | |
22e6c827 | 1136 | do_upload(struct sftp_conn *conn, char *local_path, char *remote_path, |
1137 | int pflag) | |
61e96248 | 1138 | { |
514a858e | 1139 | int local_fd; |
1140 | int status = SSH2_FX_OK; | |
b2bab059 | 1141 | u_int handle_len, id, type; |
9e7f4c4f | 1142 | off_t offset; |
375f867e | 1143 | char *handle, *data; |
61e96248 | 1144 | Buffer msg; |
1145 | struct stat sb; | |
1146 | Attrib a; | |
c25d3df7 | 1147 | u_int32_t startid; |
1148 | u_int32_t ackid; | |
b2bab059 | 1149 | struct outstanding_ack { |
1150 | u_int id; | |
1151 | u_int len; | |
9e7f4c4f | 1152 | off_t offset; |
762715ce | 1153 | TAILQ_ENTRY(outstanding_ack) tq; |
b2bab059 | 1154 | }; |
1155 | TAILQ_HEAD(ackhead, outstanding_ack) acks; | |
6e007f08 | 1156 | struct outstanding_ack *ack = NULL; |
b2bab059 | 1157 | |
1158 | TAILQ_INIT(&acks); | |
61e96248 | 1159 | |
1160 | if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) { | |
1161 | error("Couldn't open local file \"%s\" for reading: %s", | |
1162 | local_path, strerror(errno)); | |
1163 | return(-1); | |
1164 | } | |
1165 | if (fstat(local_fd, &sb) == -1) { | |
1166 | error("Couldn't fstat local file \"%s\": %s", | |
1167 | local_path, strerror(errno)); | |
1168 | close(local_fd); | |
1169 | return(-1); | |
1170 | } | |
538840a2 | 1171 | if (!S_ISREG(sb.st_mode)) { |
1172 | error("%s is not a regular file", local_path); | |
1173 | close(local_fd); | |
1174 | return(-1); | |
1175 | } | |
61e96248 | 1176 | stat_to_attrib(&sb, &a); |
1177 | ||
1178 | a.flags &= ~SSH2_FILEXFER_ATTR_SIZE; | |
1179 | a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID; | |
1180 | a.perm &= 0777; | |
1181 | if (!pflag) | |
1182 | a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME; | |
1183 | ||
1184 | buffer_init(&msg); | |
1185 | ||
1186 | /* Send open request */ | |
22e6c827 | 1187 | id = conn->msg_id++; |
61e96248 | 1188 | buffer_put_char(&msg, SSH2_FXP_OPEN); |
1189 | buffer_put_int(&msg, id); | |
1190 | buffer_put_cstring(&msg, remote_path); | |
1191 | buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC); | |
1192 | encode_attrib(&msg, &a); | |
22e6c827 | 1193 | send_msg(conn->fd_out, &msg); |
9906a836 | 1194 | debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path); |
61e96248 | 1195 | |
1196 | buffer_clear(&msg); | |
1197 | ||
22e6c827 | 1198 | handle = get_handle(conn->fd_in, id, &handle_len); |
61e96248 | 1199 | if (handle == NULL) { |
1200 | close(local_fd); | |
1201 | buffer_free(&msg); | |
514a858e | 1202 | return -1; |
61e96248 | 1203 | } |
1204 | ||
c25d3df7 | 1205 | startid = ackid = id + 1; |
22e6c827 | 1206 | data = xmalloc(conn->transfer_buflen); |
375f867e | 1207 | |
61e96248 | 1208 | /* Read from local and write to remote */ |
1209 | offset = 0; | |
b65c3807 | 1210 | if (showprogress) |
1211 | start_progress_meter(local_path, sb.st_size, &offset); | |
b65c3807 | 1212 | |
184eed6a | 1213 | for (;;) { |
61e96248 | 1214 | int len; |
61e96248 | 1215 | |
1216 | /* | |
f2107e97 | 1217 | * Can't use atomicio here because it returns 0 on EOF, |
0e5de6f8 | 1218 | * thus losing the last block of the file. |
f2107e97 | 1219 | * Simulate an EOF on interrupt, allowing ACKs from the |
0e5de6f8 | 1220 | * server to drain. |
61e96248 | 1221 | */ |
514a858e | 1222 | if (interrupted || status != SSH2_FX_OK) |
0e5de6f8 | 1223 | len = 0; |
1224 | else do | |
22e6c827 | 1225 | len = read(local_fd, data, conn->transfer_buflen); |
61e96248 | 1226 | while ((len == -1) && (errno == EINTR || errno == EAGAIN)); |
1227 | ||
1228 | if (len == -1) | |
1229 | fatal("Couldn't read from \"%s\": %s", local_path, | |
1230 | strerror(errno)); | |
c25d3df7 | 1231 | |
1232 | if (len != 0) { | |
b2bab059 | 1233 | ack = xmalloc(sizeof(*ack)); |
1234 | ack->id = ++id; | |
1235 | ack->offset = offset; | |
1236 | ack->len = len; | |
1237 | TAILQ_INSERT_TAIL(&acks, ack, tq); | |
1238 | ||
c25d3df7 | 1239 | buffer_clear(&msg); |
1240 | buffer_put_char(&msg, SSH2_FXP_WRITE); | |
b2bab059 | 1241 | buffer_put_int(&msg, ack->id); |
c25d3df7 | 1242 | buffer_put_string(&msg, handle, handle_len); |
1243 | buffer_put_int64(&msg, offset); | |
1244 | buffer_put_string(&msg, data, len); | |
22e6c827 | 1245 | send_msg(conn->fd_out, &msg); |
9906a836 | 1246 | debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u", |
b77a87e5 | 1247 | id, (unsigned long long)offset, len); |
b2bab059 | 1248 | } else if (TAILQ_FIRST(&acks) == NULL) |
61e96248 | 1249 | break; |
1250 | ||
b2bab059 | 1251 | if (ack == NULL) |
1252 | fatal("Unexpected ACK %u", id); | |
1253 | ||
762715ce | 1254 | if (id == startid || len == 0 || |
22e6c827 | 1255 | id - ackid >= conn->num_requests) { |
875ec275 | 1256 | u_int r_id; |
af2b3cd9 | 1257 | |
b2bab059 | 1258 | buffer_clear(&msg); |
22e6c827 | 1259 | get_msg(conn->fd_in, &msg); |
b2bab059 | 1260 | type = buffer_get_char(&msg); |
af2b3cd9 | 1261 | r_id = buffer_get_int(&msg); |
b2bab059 | 1262 | |
1263 | if (type != SSH2_FXP_STATUS) | |
1264 | fatal("Expected SSH2_FXP_STATUS(%d) packet, " | |
1265 | "got %d", SSH2_FXP_STATUS, type); | |
1266 | ||
1267 | status = buffer_get_int(&msg); | |
1268 | debug3("SSH2_FXP_STATUS %d", status); | |
1269 | ||
1270 | /* Find the request in our queue */ | |
f8cc7664 | 1271 | for (ack = TAILQ_FIRST(&acks); |
af2b3cd9 | 1272 | ack != NULL && ack->id != r_id; |
b2bab059 | 1273 | ack = TAILQ_NEXT(ack, tq)) |
1274 | ; | |
1275 | if (ack == NULL) | |
9906a836 | 1276 | fatal("Can't find request for ID %u", r_id); |
b2bab059 | 1277 | TAILQ_REMOVE(&acks, ack, tq); |
9e7f4c4f | 1278 | debug3("In write loop, ack for %u %u bytes at %lld", |
1279 | ack->id, ack->len, (long long)ack->offset); | |
c25d3df7 | 1280 | ++ackid; |
30e37ee6 | 1281 | xfree(ack); |
61e96248 | 1282 | } |
61e96248 | 1283 | offset += len; |
9e7f4c4f | 1284 | if (offset < 0) |
1285 | fatal("%s: offset < 0", __func__); | |
61e96248 | 1286 | } |
514a858e | 1287 | buffer_free(&msg); |
1288 | ||
b65c3807 | 1289 | if (showprogress) |
1290 | stop_progress_meter(); | |
375f867e | 1291 | xfree(data); |
61e96248 | 1292 | |
514a858e | 1293 | if (status != SSH2_FX_OK) { |
1294 | error("Couldn't write to remote file \"%s\": %s", | |
1295 | remote_path, fx2txt(status)); | |
1296 | status = -1; | |
1297 | } | |
1298 | ||
61e96248 | 1299 | if (close(local_fd) == -1) { |
1300 | error("Couldn't close local file \"%s\": %s", local_path, | |
1301 | strerror(errno)); | |
0426a3b4 | 1302 | status = -1; |
61e96248 | 1303 | } |
1304 | ||
58c54a79 | 1305 | /* Override umask and utimes if asked */ |
1306 | if (pflag) | |
22e6c827 | 1307 | do_fsetstat(conn, handle, handle_len, &a); |
58c54a79 | 1308 | |
514a858e | 1309 | if (do_close(conn, handle, handle_len) != SSH2_FX_OK) |
1310 | status = -1; | |
0426a3b4 | 1311 | xfree(handle); |
514a858e | 1312 | |
1313 | return status; | |
61e96248 | 1314 | } |