blob: 30bef8936d64792d8b714f3183d5b889241b898e [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
Damien Miller3db5f532002-02-13 14:10:32 +11002 * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
Damien Miller33804262001-02-04 23:20:18 +11003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25/* XXX: memleaks */
26/* XXX: signed vs unsigned */
Damien Miller3db5f532002-02-13 14:10:32 +110027/* XXX: remove all logging, only return status codes */
Damien Miller33804262001-02-04 23:20:18 +110028/* XXX: copy between two remote sites */
29
30#include "includes.h"
Ben Lindstrom8e879cf2002-11-09 15:48:49 +000031RCSID("$OpenBSD: sftp-client.c,v 1.36 2002/11/05 20:10:37 markus Exp $");
Damien Miller16a13332002-02-13 14:03:56 +110032
Damien Miller9b481512002-09-12 10:43:29 +100033#include "openbsd-compat/sys-queue.h"
Damien Miller33804262001-02-04 23:20:18 +110034
Damien Miller33804262001-02-04 23:20:18 +110035#include "buffer.h"
36#include "bufaux.h"
37#include "getput.h"
38#include "xmalloc.h"
39#include "log.h"
40#include "atomicio.h"
Damien Miller33804262001-02-04 23:20:18 +110041
42#include "sftp.h"
43#include "sftp-common.h"
44#include "sftp-client.h"
45
Damien Miller16a13332002-02-13 14:03:56 +110046/* Minimum amount of data to read at at time */
47#define MIN_READ_SIZE 512
48
Damien Miller3db5f532002-02-13 14:10:32 +110049struct sftp_conn {
50 int fd_in;
51 int fd_out;
52 u_int transfer_buflen;
53 u_int num_requests;
54 u_int version;
55 u_int msg_id;
56};
Ben Lindstrom288cc392001-02-09 02:58:04 +000057
Ben Lindstrombba81212001-06-25 05:01:22 +000058static void
Damien Miller33804262001-02-04 23:20:18 +110059send_msg(int fd, Buffer *m)
60{
61 int mlen = buffer_len(m);
62 int len;
63 Buffer oqueue;
64
65 buffer_init(&oqueue);
66 buffer_put_int(&oqueue, mlen);
67 buffer_append(&oqueue, buffer_ptr(m), mlen);
68 buffer_consume(m, mlen);
69
70 len = atomicio(write, fd, buffer_ptr(&oqueue), buffer_len(&oqueue));
71 if (len <= 0)
72 fatal("Couldn't send packet: %s", strerror(errno));
73
74 buffer_free(&oqueue);
75}
76
Ben Lindstrombba81212001-06-25 05:01:22 +000077static void
Damien Miller33804262001-02-04 23:20:18 +110078get_msg(int fd, Buffer *m)
79{
80 u_int len, msg_len;
81 unsigned char buf[4096];
82
83 len = atomicio(read, fd, buf, 4);
Damien Millercafff192001-03-19 22:29:46 +110084 if (len == 0)
85 fatal("Connection closed");
86 else if (len == -1)
Damien Miller33804262001-02-04 23:20:18 +110087 fatal("Couldn't read packet: %s", strerror(errno));
88
89 msg_len = GET_32BIT(buf);
90 if (msg_len > 256 * 1024)
Ben Lindstromb1f483f2002-06-23 21:27:18 +000091 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +110092
93 while (msg_len) {
94 len = atomicio(read, fd, buf, MIN(msg_len, sizeof(buf)));
Damien Millercafff192001-03-19 22:29:46 +110095 if (len == 0)
96 fatal("Connection closed");
97 else if (len == -1)
Damien Miller33804262001-02-04 23:20:18 +110098 fatal("Couldn't read packet: %s", strerror(errno));
99
100 msg_len -= len;
101 buffer_append(m, buf, len);
102 }
103}
104
Ben Lindstrombba81212001-06-25 05:01:22 +0000105static void
Damien Miller33804262001-02-04 23:20:18 +1100106send_string_request(int fd, u_int id, u_int code, char *s,
107 u_int len)
108{
109 Buffer msg;
110
111 buffer_init(&msg);
112 buffer_put_char(&msg, code);
113 buffer_put_int(&msg, id);
114 buffer_put_string(&msg, s, len);
115 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000116 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100117 buffer_free(&msg);
118}
119
Ben Lindstrombba81212001-06-25 05:01:22 +0000120static void
Damien Miller33804262001-02-04 23:20:18 +1100121send_string_attrs_request(int fd, u_int id, u_int code, char *s,
122 u_int len, Attrib *a)
123{
124 Buffer msg;
125
126 buffer_init(&msg);
127 buffer_put_char(&msg, code);
128 buffer_put_int(&msg, id);
129 buffer_put_string(&msg, s, len);
130 encode_attrib(&msg, a);
131 send_msg(fd, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000132 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100133 buffer_free(&msg);
134}
135
Ben Lindstrombba81212001-06-25 05:01:22 +0000136static u_int
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000137get_status(int fd, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100138{
139 Buffer msg;
140 u_int type, id, status;
141
142 buffer_init(&msg);
143 get_msg(fd, &msg);
144 type = buffer_get_char(&msg);
145 id = buffer_get_int(&msg);
146
147 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000148 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100149 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000150 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100151 SSH2_FXP_STATUS, type);
152
153 status = buffer_get_int(&msg);
154 buffer_free(&msg);
155
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000156 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100157
158 return(status);
159}
160
Ben Lindstrombba81212001-06-25 05:01:22 +0000161static char *
Damien Miller33804262001-02-04 23:20:18 +1100162get_handle(int fd, u_int expected_id, u_int *len)
163{
164 Buffer msg;
165 u_int type, id;
166 char *handle;
167
168 buffer_init(&msg);
169 get_msg(fd, &msg);
170 type = buffer_get_char(&msg);
171 id = buffer_get_int(&msg);
172
173 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000174 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100175 if (type == SSH2_FXP_STATUS) {
176 int status = buffer_get_int(&msg);
177
178 error("Couldn't get handle: %s", fx2txt(status));
179 return(NULL);
180 } else if (type != SSH2_FXP_HANDLE)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000181 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100182 SSH2_FXP_HANDLE, type);
183
184 handle = buffer_get_string(&msg, len);
185 buffer_free(&msg);
186
187 return(handle);
188}
189
Ben Lindstrombba81212001-06-25 05:01:22 +0000190static Attrib *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000191get_decode_stat(int fd, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100192{
193 Buffer msg;
194 u_int type, id;
195 Attrib *a;
196
197 buffer_init(&msg);
198 get_msg(fd, &msg);
199
200 type = buffer_get_char(&msg);
201 id = buffer_get_int(&msg);
202
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000203 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100204 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000205 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100206 if (type == SSH2_FXP_STATUS) {
207 int status = buffer_get_int(&msg);
208
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000209 if (quiet)
210 debug("Couldn't stat remote file: %s", fx2txt(status));
211 else
212 error("Couldn't stat remote file: %s", fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100213 return(NULL);
214 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000215 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100216 SSH2_FXP_ATTRS, type);
217 }
218 a = decode_attrib(&msg);
219 buffer_free(&msg);
220
221 return(a);
222}
223
Damien Miller3db5f532002-02-13 14:10:32 +1100224struct sftp_conn *
225do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
Damien Miller33804262001-02-04 23:20:18 +1100226{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000227 u_int type;
228 int version;
Damien Miller33804262001-02-04 23:20:18 +1100229 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100230 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100231
232 buffer_init(&msg);
233 buffer_put_char(&msg, SSH2_FXP_INIT);
234 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
235 send_msg(fd_out, &msg);
236
237 buffer_clear(&msg);
238
239 get_msg(fd_in, &msg);
240
Kevin Stevesef4eea92001-02-05 12:42:17 +0000241 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100242 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000243 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100244 type);
245 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100246 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100247 }
248 version = buffer_get_int(&msg);
249
250 debug2("Remote version: %d", version);
251
252 /* Check for extensions */
253 while (buffer_len(&msg) > 0) {
254 char *name = buffer_get_string(&msg, NULL);
255 char *value = buffer_get_string(&msg, NULL);
256
257 debug2("Init extension: \"%s\"", name);
258 xfree(name);
259 xfree(value);
260 }
261
262 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100263
Damien Miller3db5f532002-02-13 14:10:32 +1100264 ret = xmalloc(sizeof(*ret));
265 ret->fd_in = fd_in;
266 ret->fd_out = fd_out;
267 ret->transfer_buflen = transfer_buflen;
268 ret->num_requests = num_requests;
269 ret->version = version;
270 ret->msg_id = 1;
271
272 /* Some filexfer v.0 servers don't support large packets */
273 if (version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000274 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100275
276 return(ret);
277}
278
279u_int
280sftp_proto_version(struct sftp_conn *conn)
281{
282 return(conn->version);
Damien Miller33804262001-02-04 23:20:18 +1100283}
284
285int
Damien Miller3db5f532002-02-13 14:10:32 +1100286do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100287{
288 u_int id, status;
289 Buffer msg;
290
291 buffer_init(&msg);
292
Damien Miller3db5f532002-02-13 14:10:32 +1100293 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100294 buffer_put_char(&msg, SSH2_FXP_CLOSE);
295 buffer_put_int(&msg, id);
296 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100297 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000298 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100299
Damien Miller3db5f532002-02-13 14:10:32 +1100300 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100301 if (status != SSH2_FX_OK)
302 error("Couldn't close file: %s", fx2txt(status));
303
304 buffer_free(&msg);
305
306 return(status);
307}
308
Damien Miller4870afd2001-03-14 10:27:09 +1100309
Ben Lindstrombba81212001-06-25 05:01:22 +0000310static int
Damien Miller3db5f532002-02-13 14:10:32 +1100311do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100312 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100313{
314 Buffer msg;
Ben Lindstrom025df4a2001-03-14 15:16:34 +0000315 u_int type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100316 char *handle;
317
Damien Miller3db5f532002-02-13 14:10:32 +1100318 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100319
320 buffer_init(&msg);
321 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
322 buffer_put_int(&msg, id);
323 buffer_put_cstring(&msg, path);
Damien Miller3db5f532002-02-13 14:10:32 +1100324 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100325
326 buffer_clear(&msg);
327
Damien Miller3db5f532002-02-13 14:10:32 +1100328 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100329 if (handle == NULL)
330 return(-1);
331
Damien Miller4870afd2001-03-14 10:27:09 +1100332 if (dir) {
333 ents = 0;
334 *dir = xmalloc(sizeof(**dir));
335 (*dir)[0] = NULL;
336 }
Damien Miller4870afd2001-03-14 10:27:09 +1100337
Damien Miller9f0f5c62001-12-21 14:45:46 +1100338 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +1100339 int count;
340
Damien Miller3db5f532002-02-13 14:10:32 +1100341 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100342
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000343 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100344
345 buffer_clear(&msg);
346 buffer_put_char(&msg, SSH2_FXP_READDIR);
347 buffer_put_int(&msg, id);
348 buffer_put_string(&msg, handle, handle_len);
Damien Miller3db5f532002-02-13 14:10:32 +1100349 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100350
351 buffer_clear(&msg);
352
Damien Miller3db5f532002-02-13 14:10:32 +1100353 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100354
355 type = buffer_get_char(&msg);
356 id = buffer_get_int(&msg);
357
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000358 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100359
360 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000361 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100362
363 if (type == SSH2_FXP_STATUS) {
364 int status = buffer_get_int(&msg);
365
366 debug3("Received SSH2_FXP_STATUS %d", status);
367
368 if (status == SSH2_FX_EOF) {
369 break;
370 } else {
371 error("Couldn't read directory: %s",
372 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100373 do_close(conn, handle, handle_len);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000374 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100375 }
376 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000377 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100378 SSH2_FXP_NAME, type);
379
380 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100381 if (count == 0)
382 break;
383 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100384 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100385 char *filename, *longname;
386 Attrib *a;
387
388 filename = buffer_get_string(&msg, NULL);
389 longname = buffer_get_string(&msg, NULL);
390 a = decode_attrib(&msg);
391
Damien Miller4870afd2001-03-14 10:27:09 +1100392 if (printflag)
393 printf("%s\n", longname);
394
395 if (dir) {
Ben Lindstroma3700052001-04-05 23:26:32 +0000396 *dir = xrealloc(*dir, sizeof(**dir) *
Damien Miller4870afd2001-03-14 10:27:09 +1100397 (ents + 2));
398 (*dir)[ents] = xmalloc(sizeof(***dir));
399 (*dir)[ents]->filename = xstrdup(filename);
400 (*dir)[ents]->longname = xstrdup(longname);
401 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
402 (*dir)[++ents] = NULL;
403 }
Damien Miller33804262001-02-04 23:20:18 +1100404
405 xfree(filename);
406 xfree(longname);
407 }
408 }
409
410 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100411 do_close(conn, handle, handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100412 xfree(handle);
413
414 return(0);
415}
416
417int
Damien Miller3db5f532002-02-13 14:10:32 +1100418do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100419{
Damien Miller3db5f532002-02-13 14:10:32 +1100420 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100421}
422
423void free_sftp_dirents(SFTP_DIRENT **s)
424{
425 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100426
427 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100428 xfree(s[i]->filename);
429 xfree(s[i]->longname);
430 xfree(s[i]);
431 }
432 xfree(s);
433}
434
435int
Damien Miller3db5f532002-02-13 14:10:32 +1100436do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100437{
438 u_int status, id;
439
440 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
441
Damien Miller3db5f532002-02-13 14:10:32 +1100442 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000443 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100444 strlen(path));
445 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100446 if (status != SSH2_FX_OK)
447 error("Couldn't delete file: %s", fx2txt(status));
448 return(status);
449}
450
451int
Damien Miller3db5f532002-02-13 14:10:32 +1100452do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100453{
454 u_int status, id;
455
Damien Miller3db5f532002-02-13 14:10:32 +1100456 id = conn->msg_id++;
457 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100458 strlen(path), a);
459
Damien Miller3db5f532002-02-13 14:10:32 +1100460 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100461 if (status != SSH2_FX_OK)
462 error("Couldn't create directory: %s", fx2txt(status));
463
464 return(status);
465}
466
467int
Damien Miller3db5f532002-02-13 14:10:32 +1100468do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100469{
470 u_int status, id;
471
Damien Miller3db5f532002-02-13 14:10:32 +1100472 id = conn->msg_id++;
473 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
474 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100475
Damien Miller3db5f532002-02-13 14:10:32 +1100476 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100477 if (status != SSH2_FX_OK)
478 error("Couldn't remove directory: %s", fx2txt(status));
479
480 return(status);
481}
482
483Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100484do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100485{
486 u_int id;
487
Damien Miller3db5f532002-02-13 14:10:32 +1100488 id = conn->msg_id++;
489
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000490 send_string_request(conn->fd_out, id,
491 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100492 path, strlen(path));
493
494 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100495}
496
497Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100498do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100499{
500 u_int id;
501
Damien Miller3db5f532002-02-13 14:10:32 +1100502 if (conn->version == 0) {
503 if (quiet)
504 debug("Server version does not support lstat operation");
505 else
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000506 log("Server version does not support lstat operation");
507 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100508 }
509
510 id = conn->msg_id++;
511 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
512 strlen(path));
513
514 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100515}
516
517Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100518do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100519{
520 u_int id;
521
Damien Miller3db5f532002-02-13 14:10:32 +1100522 id = conn->msg_id++;
523 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
524 handle_len);
525
526 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100527}
528
529int
Damien Miller3db5f532002-02-13 14:10:32 +1100530do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100531{
532 u_int status, id;
533
Damien Miller3db5f532002-02-13 14:10:32 +1100534 id = conn->msg_id++;
535 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100536 strlen(path), a);
537
Damien Miller3db5f532002-02-13 14:10:32 +1100538 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100539 if (status != SSH2_FX_OK)
540 error("Couldn't setstat on \"%s\": %s", path,
541 fx2txt(status));
542
543 return(status);
544}
545
546int
Damien Miller3db5f532002-02-13 14:10:32 +1100547do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100548 Attrib *a)
549{
550 u_int status, id;
551
Damien Miller3db5f532002-02-13 14:10:32 +1100552 id = conn->msg_id++;
553 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100554 handle_len, a);
555
Damien Miller3db5f532002-02-13 14:10:32 +1100556 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100557 if (status != SSH2_FX_OK)
558 error("Couldn't fsetstat: %s", fx2txt(status));
559
560 return(status);
561}
562
563char *
Damien Miller3db5f532002-02-13 14:10:32 +1100564do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100565{
566 Buffer msg;
567 u_int type, expected_id, count, id;
568 char *filename, *longname;
569 Attrib *a;
570
Damien Miller3db5f532002-02-13 14:10:32 +1100571 expected_id = id = conn->msg_id++;
572 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
573 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100574
575 buffer_init(&msg);
576
Damien Miller3db5f532002-02-13 14:10:32 +1100577 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100578 type = buffer_get_char(&msg);
579 id = buffer_get_int(&msg);
580
581 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000582 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100583
584 if (type == SSH2_FXP_STATUS) {
585 u_int status = buffer_get_int(&msg);
586
587 error("Couldn't canonicalise: %s", fx2txt(status));
588 return(NULL);
589 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000590 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100591 SSH2_FXP_NAME, type);
592
593 count = buffer_get_int(&msg);
594 if (count != 1)
595 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
596
597 filename = buffer_get_string(&msg, NULL);
598 longname = buffer_get_string(&msg, NULL);
599 a = decode_attrib(&msg);
600
601 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
602
603 xfree(longname);
604
605 buffer_free(&msg);
606
607 return(filename);
608}
609
610int
Damien Miller3db5f532002-02-13 14:10:32 +1100611do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100612{
613 Buffer msg;
614 u_int status, id;
615
616 buffer_init(&msg);
617
618 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100619 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100620 buffer_put_char(&msg, SSH2_FXP_RENAME);
621 buffer_put_int(&msg, id);
622 buffer_put_cstring(&msg, oldpath);
623 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100624 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100625 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
626 newpath);
627 buffer_free(&msg);
628
Damien Miller3db5f532002-02-13 14:10:32 +1100629 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100630 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100631 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
632 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100633
634 return(status);
635}
636
637int
Damien Miller3db5f532002-02-13 14:10:32 +1100638do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100639{
640 Buffer msg;
641 u_int status, id;
642
Damien Miller3db5f532002-02-13 14:10:32 +1100643 if (conn->version < 3) {
644 error("This server does not support the symlink operation");
645 return(SSH2_FX_OP_UNSUPPORTED);
646 }
647
Damien Miller058316f2001-03-08 10:08:49 +1100648 buffer_init(&msg);
649
650 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100651 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100652 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
653 buffer_put_int(&msg, id);
654 buffer_put_cstring(&msg, oldpath);
655 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100656 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100657 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
658 newpath);
659 buffer_free(&msg);
660
Damien Miller3db5f532002-02-13 14:10:32 +1100661 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100662 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000663 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100664 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100665
666 return(status);
667}
668
669char *
Damien Miller3db5f532002-02-13 14:10:32 +1100670do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100671{
672 Buffer msg;
673 u_int type, expected_id, count, id;
674 char *filename, *longname;
675 Attrib *a;
676
Damien Miller3db5f532002-02-13 14:10:32 +1100677 expected_id = id = conn->msg_id++;
678 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
679 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100680
681 buffer_init(&msg);
682
Damien Miller3db5f532002-02-13 14:10:32 +1100683 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100684 type = buffer_get_char(&msg);
685 id = buffer_get_int(&msg);
686
687 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000688 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100689
690 if (type == SSH2_FXP_STATUS) {
691 u_int status = buffer_get_int(&msg);
692
693 error("Couldn't readlink: %s", fx2txt(status));
694 return(NULL);
695 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000696 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100697 SSH2_FXP_NAME, type);
698
699 count = buffer_get_int(&msg);
700 if (count != 1)
701 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
702
703 filename = buffer_get_string(&msg, NULL);
704 longname = buffer_get_string(&msg, NULL);
705 a = decode_attrib(&msg);
706
707 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
708
709 xfree(longname);
710
711 buffer_free(&msg);
712
713 return(filename);
714}
715
Damien Miller16a13332002-02-13 14:03:56 +1100716static void
717send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
718 char *handle, u_int handle_len)
719{
720 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000721
Damien Miller16a13332002-02-13 14:03:56 +1100722 buffer_init(&msg);
723 buffer_clear(&msg);
724 buffer_put_char(&msg, SSH2_FXP_READ);
725 buffer_put_int(&msg, id);
726 buffer_put_string(&msg, handle, handle_len);
727 buffer_put_int64(&msg, offset);
728 buffer_put_int(&msg, len);
729 send_msg(fd_out, &msg);
730 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000731}
Damien Miller16a13332002-02-13 14:03:56 +1100732
Damien Miller058316f2001-03-08 10:08:49 +1100733int
Damien Miller3db5f532002-02-13 14:10:32 +1100734do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
735 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100736{
Damien Miller33804262001-02-04 23:20:18 +1100737 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100738 Buffer msg;
739 char *handle;
740 int local_fd, status, num_req, max_req, write_error;
741 int read_error, write_errno;
742 u_int64_t offset, size;
Damien Miller3db5f532002-02-13 14:10:32 +1100743 u_int handle_len, mode, type, id, buflen;
Damien Miller16a13332002-02-13 14:03:56 +1100744 struct request {
745 u_int id;
746 u_int len;
747 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000748 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100749 };
750 TAILQ_HEAD(reqhead, request) requests;
751 struct request *req;
752
753 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100754
Damien Miller3db5f532002-02-13 14:10:32 +1100755 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100756 if (a == NULL)
757 return(-1);
758
759 /* XXX: should we preserve set[ug]id? */
760 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
761 mode = S_IWRITE | (a->perm & 0777);
762 else
763 mode = 0666;
764
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000765 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
766 (a->perm & S_IFDIR)) {
767 error("Cannot download a directory: %s", remote_path);
768 return(-1);
769 }
770
Damien Miller16a13332002-02-13 14:03:56 +1100771 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
772 size = a->size;
773 else
774 size = 0;
775
Damien Miller3db5f532002-02-13 14:10:32 +1100776 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100777 buffer_init(&msg);
778
779 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100780 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100781 buffer_put_char(&msg, SSH2_FXP_OPEN);
782 buffer_put_int(&msg, id);
783 buffer_put_cstring(&msg, remote_path);
784 buffer_put_int(&msg, SSH2_FXF_READ);
785 attrib_clear(&junk); /* Send empty attributes */
786 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100787 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000788 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100789
Damien Miller3db5f532002-02-13 14:10:32 +1100790 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100791 if (handle == NULL) {
792 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100793 return(-1);
794 }
795
Damien Miller3db5f532002-02-13 14:10:32 +1100796 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
797 if (local_fd == -1) {
798 error("Couldn't open local file \"%s\" for writing: %s",
799 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000800 buffer_free(&msg);
801 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100802 return(-1);
803 }
804
Damien Miller33804262001-02-04 23:20:18 +1100805 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100806 write_error = read_error = write_errno = num_req = offset = 0;
807 max_req = 1;
808 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100809 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100810 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100811
Damien Miller16a13332002-02-13 14:03:56 +1100812 /* Send some more requests */
813 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000814 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000815 (unsigned long long)offset,
816 (unsigned long long)offset + buflen - 1,
817 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100818 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100819 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100820 req->len = buflen;
821 req->offset = offset;
822 offset += buflen;
823 num_req++;
824 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000825 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100826 req->len, handle, handle_len);
827 }
Damien Miller33804262001-02-04 23:20:18 +1100828
829 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100830 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100831 type = buffer_get_char(&msg);
832 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000833 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100834
Damien Miller16a13332002-02-13 14:03:56 +1100835 /* Find the request in our queue */
836 for(req = TAILQ_FIRST(&requests);
837 req != NULL && req->id != id;
838 req = TAILQ_NEXT(req, tq))
839 ;
840 if (req == NULL)
841 fatal("Unexpected reply %u", id);
842
843 switch (type) {
844 case SSH2_FXP_STATUS:
845 status = buffer_get_int(&msg);
846 if (status != SSH2_FX_EOF)
847 read_error = 1;
848 max_req = 0;
849 TAILQ_REMOVE(&requests, req, tq);
850 xfree(req);
851 num_req--;
852 break;
853 case SSH2_FXP_DATA:
854 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000855 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000856 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000857 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100858 if (len > req->len)
859 fatal("Received more data than asked for "
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000860 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100861 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
862 atomicio(write, local_fd, data, len) != len) &&
863 !write_error) {
864 write_errno = errno;
865 write_error = 1;
866 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100867 }
Damien Miller16a13332002-02-13 14:03:56 +1100868 xfree(data);
869
870 if (len == req->len) {
871 TAILQ_REMOVE(&requests, req, tq);
872 xfree(req);
873 num_req--;
874 } else {
875 /* Resend the request for the missing data */
876 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000877 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000878 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000879 (unsigned long long)req->offset +
880 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100881 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100882 req->len -= len;
883 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000884 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100885 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100886 /* Reduce the request size */
887 if (len < buflen)
888 buflen = MAX(MIN_READ_SIZE, len);
889 }
890 if (max_req > 0) { /* max_req = 0 iff EOF received */
891 if (size > 0 && offset > size) {
892 /* Only one request at a time
893 * after the expected EOF */
894 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000895 (unsigned long long)offset,
896 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100897 max_req = 1;
898 }
Damien Miller3db5f532002-02-13 14:10:32 +1100899 else if (max_req < conn->num_requests + 1) {
Damien Miller16a13332002-02-13 14:03:56 +1100900 ++max_req;
901 }
902 }
903 break;
904 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000905 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100906 SSH2_FXP_DATA, type);
907 }
Damien Miller33804262001-02-04 23:20:18 +1100908 }
Damien Miller33804262001-02-04 23:20:18 +1100909
Damien Miller16a13332002-02-13 14:03:56 +1100910 /* Sanity check */
911 if (TAILQ_FIRST(&requests) != NULL)
912 fatal("Transfer complete, but requests still in queue");
913
914 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000915 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100916 remote_path, fx2txt(status));
917 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100918 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100919 error("Couldn't write to \"%s\": %s", local_path,
920 strerror(write_errno));
921 status = -1;
922 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100923 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100924 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100925
926 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000927#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100928 if (pflag && fchmod(local_fd, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000929#else
Damien Miller16a13332002-02-13 14:03:56 +1100930 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000931#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100932 error("Couldn't set mode on \"%s\": %s", local_path,
933 strerror(errno));
934 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
935 struct timeval tv[2];
936 tv[0].tv_sec = a->atime;
937 tv[1].tv_sec = a->mtime;
938 tv[0].tv_usec = tv[1].tv_usec = 0;
939 if (utimes(local_path, tv) == -1)
940 error("Can't set times on \"%s\": %s",
941 local_path, strerror(errno));
942 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000943 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100944 close(local_fd);
945 buffer_free(&msg);
946 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100947
948 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100949}
950
951int
Damien Miller3db5f532002-02-13 14:10:32 +1100952do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
953 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100954{
Damien Miller8829d362002-02-08 22:04:05 +1100955 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +1100956 u_int handle_len, id, type;
Damien Miller33804262001-02-04 23:20:18 +1100957 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100958 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100959 Buffer msg;
960 struct stat sb;
961 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +1100962 u_int32_t startid;
963 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +1100964 struct outstanding_ack {
965 u_int id;
966 u_int len;
967 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000968 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +1100969 };
970 TAILQ_HEAD(ackhead, outstanding_ack) acks;
971 struct outstanding_ack *ack;
972
973 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +1100974
975 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
976 error("Couldn't open local file \"%s\" for reading: %s",
977 local_path, strerror(errno));
978 return(-1);
979 }
980 if (fstat(local_fd, &sb) == -1) {
981 error("Couldn't fstat local file \"%s\": %s",
982 local_path, strerror(errno));
983 close(local_fd);
984 return(-1);
985 }
986 stat_to_attrib(&sb, &a);
987
988 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
989 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
990 a.perm &= 0777;
991 if (!pflag)
992 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
993
994 buffer_init(&msg);
995
996 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100997 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100998 buffer_put_char(&msg, SSH2_FXP_OPEN);
999 buffer_put_int(&msg, id);
1000 buffer_put_cstring(&msg, remote_path);
1001 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1002 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001003 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001004 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001005
1006 buffer_clear(&msg);
1007
Damien Miller3db5f532002-02-13 14:10:32 +11001008 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001009 if (handle == NULL) {
1010 close(local_fd);
1011 buffer_free(&msg);
1012 return(-1);
1013 }
1014
Damien Miller16a13332002-02-13 14:03:56 +11001015 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001016 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001017
Damien Miller33804262001-02-04 23:20:18 +11001018 /* Read from local and write to remote */
1019 offset = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001020 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001021 int len;
Damien Miller33804262001-02-04 23:20:18 +11001022
1023 /*
1024 * Can't use atomicio here because it returns 0 on EOF, thus losing
1025 * the last block of the file
1026 */
1027 do
Damien Miller3db5f532002-02-13 14:10:32 +11001028 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001029 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1030
1031 if (len == -1)
1032 fatal("Couldn't read from \"%s\": %s", local_path,
1033 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001034
1035 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001036 ack = xmalloc(sizeof(*ack));
1037 ack->id = ++id;
1038 ack->offset = offset;
1039 ack->len = len;
1040 TAILQ_INSERT_TAIL(&acks, ack, tq);
1041
Damien Miller16a13332002-02-13 14:03:56 +11001042 buffer_clear(&msg);
1043 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001044 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001045 buffer_put_string(&msg, handle, handle_len);
1046 buffer_put_int64(&msg, offset);
1047 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001048 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001049 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstromeb505452002-03-22 01:03:15 +00001050 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001051 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001052 break;
1053
Damien Miller5873dfd2002-02-13 14:04:37 +11001054 if (ack == NULL)
1055 fatal("Unexpected ACK %u", id);
1056
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001057 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001058 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001059 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001060
Damien Miller5873dfd2002-02-13 14:04:37 +11001061 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001062 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001063 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001064 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001065
1066 if (type != SSH2_FXP_STATUS)
1067 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1068 "got %d", SSH2_FXP_STATUS, type);
1069
1070 status = buffer_get_int(&msg);
1071 debug3("SSH2_FXP_STATUS %d", status);
1072
1073 /* Find the request in our queue */
1074 for(ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001075 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001076 ack = TAILQ_NEXT(ack, tq))
1077 ;
1078 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001079 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001080 TAILQ_REMOVE(&acks, ack, tq);
1081
Damien Miller16a13332002-02-13 14:03:56 +11001082 if (status != SSH2_FX_OK) {
1083 error("Couldn't write to remote file \"%s\": %s",
1084 remote_path, fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +11001085 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001086 close(local_fd);
1087 goto done;
1088 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001089 debug3("In write loop, ack for %u %u bytes at %llu",
Ben Lindstromeb505452002-03-22 01:03:15 +00001090 ack->id, ack->len, (unsigned long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001091 ++ackid;
Ben Lindstromeec16fc2002-07-04 00:06:15 +00001092 xfree(ack);
Damien Miller33804262001-02-04 23:20:18 +11001093 }
Damien Miller33804262001-02-04 23:20:18 +11001094 offset += len;
1095 }
Damien Miller8829d362002-02-08 22:04:05 +11001096 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001097
1098 if (close(local_fd) == -1) {
1099 error("Couldn't close local file \"%s\": %s", local_path,
1100 strerror(errno));
Damien Miller3db5f532002-02-13 14:10:32 +11001101 do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001102 status = -1;
1103 goto done;
Damien Miller33804262001-02-04 23:20:18 +11001104 }
1105
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001106 /* Override umask and utimes if asked */
1107 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001108 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001109
Damien Miller3db5f532002-02-13 14:10:32 +11001110 status = do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001111
1112done:
1113 xfree(handle);
1114 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001115 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001116}