blob: 10b7992d0939b5d4fc8fa8e2b893c6c2479bf1fc [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 Lindstromb1f483f2002-06-23 21:27:18 +000031RCSID("$OpenBSD: sftp-client.c,v 1.33 2002/06/23 09:30:14 deraadt Exp $");
Damien Miller16a13332002-02-13 14:03:56 +110032
Damien Miller16a13332002-02-13 14:03:56 +110033#include "openbsd-compat/fake-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_ls(struct sftp_conn *conn, char *path)
Damien Miller4870afd2001-03-14 10:27:09 +1100419{
Damien Miller3db5f532002-02-13 14:10:32 +1100420 return(do_lsreaddir(conn, path, 1, NULL));
Damien Miller4870afd2001-03-14 10:27:09 +1100421}
422
423int
Damien Miller3db5f532002-02-13 14:10:32 +1100424do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100425{
Damien Miller3db5f532002-02-13 14:10:32 +1100426 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100427}
428
429void free_sftp_dirents(SFTP_DIRENT **s)
430{
431 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100432
433 for (i = 0; s[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100434 xfree(s[i]->filename);
435 xfree(s[i]->longname);
436 xfree(s[i]);
437 }
438 xfree(s);
439}
440
441int
Damien Miller3db5f532002-02-13 14:10:32 +1100442do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100443{
444 u_int status, id;
445
446 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
447
Damien Miller3db5f532002-02-13 14:10:32 +1100448 id = conn->msg_id++;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000449 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100450 strlen(path));
451 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100452 if (status != SSH2_FX_OK)
453 error("Couldn't delete file: %s", fx2txt(status));
454 return(status);
455}
456
457int
Damien Miller3db5f532002-02-13 14:10:32 +1100458do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100459{
460 u_int status, id;
461
Damien Miller3db5f532002-02-13 14:10:32 +1100462 id = conn->msg_id++;
463 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100464 strlen(path), a);
465
Damien Miller3db5f532002-02-13 14:10:32 +1100466 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100467 if (status != SSH2_FX_OK)
468 error("Couldn't create directory: %s", fx2txt(status));
469
470 return(status);
471}
472
473int
Damien Miller3db5f532002-02-13 14:10:32 +1100474do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100475{
476 u_int status, id;
477
Damien Miller3db5f532002-02-13 14:10:32 +1100478 id = conn->msg_id++;
479 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
480 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100481
Damien Miller3db5f532002-02-13 14:10:32 +1100482 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100483 if (status != SSH2_FX_OK)
484 error("Couldn't remove directory: %s", fx2txt(status));
485
486 return(status);
487}
488
489Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100490do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100491{
492 u_int id;
493
Damien Miller3db5f532002-02-13 14:10:32 +1100494 id = conn->msg_id++;
495
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000496 send_string_request(conn->fd_out, id,
497 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100498 path, strlen(path));
499
500 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100501}
502
503Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100504do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100505{
506 u_int id;
507
Damien Miller3db5f532002-02-13 14:10:32 +1100508 if (conn->version == 0) {
509 if (quiet)
510 debug("Server version does not support lstat operation");
511 else
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000512 log("Server version does not support lstat operation");
513 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100514 }
515
516 id = conn->msg_id++;
517 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
518 strlen(path));
519
520 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100521}
522
523Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100524do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100525{
526 u_int id;
527
Damien Miller3db5f532002-02-13 14:10:32 +1100528 id = conn->msg_id++;
529 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
530 handle_len);
531
532 return(get_decode_stat(conn->fd_in, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100533}
534
535int
Damien Miller3db5f532002-02-13 14:10:32 +1100536do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100537{
538 u_int status, id;
539
Damien Miller3db5f532002-02-13 14:10:32 +1100540 id = conn->msg_id++;
541 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100542 strlen(path), a);
543
Damien Miller3db5f532002-02-13 14:10:32 +1100544 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100545 if (status != SSH2_FX_OK)
546 error("Couldn't setstat on \"%s\": %s", path,
547 fx2txt(status));
548
549 return(status);
550}
551
552int
Damien Miller3db5f532002-02-13 14:10:32 +1100553do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100554 Attrib *a)
555{
556 u_int status, id;
557
Damien Miller3db5f532002-02-13 14:10:32 +1100558 id = conn->msg_id++;
559 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100560 handle_len, a);
561
Damien Miller3db5f532002-02-13 14:10:32 +1100562 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100563 if (status != SSH2_FX_OK)
564 error("Couldn't fsetstat: %s", fx2txt(status));
565
566 return(status);
567}
568
569char *
Damien Miller3db5f532002-02-13 14:10:32 +1100570do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100571{
572 Buffer msg;
573 u_int type, expected_id, count, id;
574 char *filename, *longname;
575 Attrib *a;
576
Damien Miller3db5f532002-02-13 14:10:32 +1100577 expected_id = id = conn->msg_id++;
578 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
579 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100580
581 buffer_init(&msg);
582
Damien Miller3db5f532002-02-13 14:10:32 +1100583 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100584 type = buffer_get_char(&msg);
585 id = buffer_get_int(&msg);
586
587 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000588 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100589
590 if (type == SSH2_FXP_STATUS) {
591 u_int status = buffer_get_int(&msg);
592
593 error("Couldn't canonicalise: %s", fx2txt(status));
594 return(NULL);
595 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000596 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100597 SSH2_FXP_NAME, type);
598
599 count = buffer_get_int(&msg);
600 if (count != 1)
601 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
602
603 filename = buffer_get_string(&msg, NULL);
604 longname = buffer_get_string(&msg, NULL);
605 a = decode_attrib(&msg);
606
607 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
608
609 xfree(longname);
610
611 buffer_free(&msg);
612
613 return(filename);
614}
615
616int
Damien Miller3db5f532002-02-13 14:10:32 +1100617do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller33804262001-02-04 23:20:18 +1100618{
619 Buffer msg;
620 u_int status, id;
621
622 buffer_init(&msg);
623
624 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100625 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100626 buffer_put_char(&msg, SSH2_FXP_RENAME);
627 buffer_put_int(&msg, id);
628 buffer_put_cstring(&msg, oldpath);
629 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100630 send_msg(conn->fd_out, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100631 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
632 newpath);
633 buffer_free(&msg);
634
Damien Miller3db5f532002-02-13 14:10:32 +1100635 status = get_status(conn->fd_in, id);
Damien Miller33804262001-02-04 23:20:18 +1100636 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100637 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
638 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100639
640 return(status);
641}
642
643int
Damien Miller3db5f532002-02-13 14:10:32 +1100644do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100645{
646 Buffer msg;
647 u_int status, id;
648
Damien Miller3db5f532002-02-13 14:10:32 +1100649 if (conn->version < 3) {
650 error("This server does not support the symlink operation");
651 return(SSH2_FX_OP_UNSUPPORTED);
652 }
653
Damien Miller058316f2001-03-08 10:08:49 +1100654 buffer_init(&msg);
655
656 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100657 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100658 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
659 buffer_put_int(&msg, id);
660 buffer_put_cstring(&msg, oldpath);
661 buffer_put_cstring(&msg, newpath);
Damien Miller3db5f532002-02-13 14:10:32 +1100662 send_msg(conn->fd_out, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100663 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
664 newpath);
665 buffer_free(&msg);
666
Damien Miller3db5f532002-02-13 14:10:32 +1100667 status = get_status(conn->fd_in, id);
Damien Miller058316f2001-03-08 10:08:49 +1100668 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100669 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
670 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100671
672 return(status);
673}
674
675char *
Damien Miller3db5f532002-02-13 14:10:32 +1100676do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100677{
678 Buffer msg;
679 u_int type, expected_id, count, id;
680 char *filename, *longname;
681 Attrib *a;
682
Damien Miller3db5f532002-02-13 14:10:32 +1100683 expected_id = id = conn->msg_id++;
684 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
685 strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100686
687 buffer_init(&msg);
688
Damien Miller3db5f532002-02-13 14:10:32 +1100689 get_msg(conn->fd_in, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100690 type = buffer_get_char(&msg);
691 id = buffer_get_int(&msg);
692
693 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000694 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100695
696 if (type == SSH2_FXP_STATUS) {
697 u_int status = buffer_get_int(&msg);
698
699 error("Couldn't readlink: %s", fx2txt(status));
700 return(NULL);
701 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000702 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100703 SSH2_FXP_NAME, type);
704
705 count = buffer_get_int(&msg);
706 if (count != 1)
707 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
708
709 filename = buffer_get_string(&msg, NULL);
710 longname = buffer_get_string(&msg, NULL);
711 a = decode_attrib(&msg);
712
713 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
714
715 xfree(longname);
716
717 buffer_free(&msg);
718
719 return(filename);
720}
721
Damien Miller16a13332002-02-13 14:03:56 +1100722static void
723send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
724 char *handle, u_int handle_len)
725{
726 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000727
Damien Miller16a13332002-02-13 14:03:56 +1100728 buffer_init(&msg);
729 buffer_clear(&msg);
730 buffer_put_char(&msg, SSH2_FXP_READ);
731 buffer_put_int(&msg, id);
732 buffer_put_string(&msg, handle, handle_len);
733 buffer_put_int64(&msg, offset);
734 buffer_put_int(&msg, len);
735 send_msg(fd_out, &msg);
736 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000737}
Damien Miller16a13332002-02-13 14:03:56 +1100738
Damien Miller058316f2001-03-08 10:08:49 +1100739int
Damien Miller3db5f532002-02-13 14:10:32 +1100740do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
741 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100742{
Damien Miller33804262001-02-04 23:20:18 +1100743 Attrib junk, *a;
Damien Miller16a13332002-02-13 14:03:56 +1100744 Buffer msg;
745 char *handle;
746 int local_fd, status, num_req, max_req, write_error;
747 int read_error, write_errno;
748 u_int64_t offset, size;
Damien Miller3db5f532002-02-13 14:10:32 +1100749 u_int handle_len, mode, type, id, buflen;
Damien Miller16a13332002-02-13 14:03:56 +1100750 struct request {
751 u_int id;
752 u_int len;
753 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000754 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +1100755 };
756 TAILQ_HEAD(reqhead, request) requests;
757 struct request *req;
758
759 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +1100760
Damien Miller3db5f532002-02-13 14:10:32 +1100761 a = do_stat(conn, remote_path, 0);
Damien Miller33804262001-02-04 23:20:18 +1100762 if (a == NULL)
763 return(-1);
764
765 /* XXX: should we preserve set[ug]id? */
766 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
767 mode = S_IWRITE | (a->perm & 0777);
768 else
769 mode = 0666;
770
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000771 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
772 (a->perm & S_IFDIR)) {
773 error("Cannot download a directory: %s", remote_path);
774 return(-1);
775 }
776
Damien Miller16a13332002-02-13 14:03:56 +1100777 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
778 size = a->size;
779 else
780 size = 0;
781
Damien Miller3db5f532002-02-13 14:10:32 +1100782 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +1100783 buffer_init(&msg);
784
785 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +1100786 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100787 buffer_put_char(&msg, SSH2_FXP_OPEN);
788 buffer_put_int(&msg, id);
789 buffer_put_cstring(&msg, remote_path);
790 buffer_put_int(&msg, SSH2_FXF_READ);
791 attrib_clear(&junk); /* Send empty attributes */
792 encode_attrib(&msg, &junk);
Damien Miller3db5f532002-02-13 14:10:32 +1100793 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000794 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +1100795
Damien Miller3db5f532002-02-13 14:10:32 +1100796 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +1100797 if (handle == NULL) {
798 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100799 return(-1);
800 }
801
Damien Miller3db5f532002-02-13 14:10:32 +1100802 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
803 if (local_fd == -1) {
804 error("Couldn't open local file \"%s\" for writing: %s",
805 local_path, strerror(errno));
Ben Lindstrom021fcd32002-02-26 18:02:43 +0000806 buffer_free(&msg);
807 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100808 return(-1);
809 }
810
Damien Miller33804262001-02-04 23:20:18 +1100811 /* Read from remote and write to local */
Damien Miller16a13332002-02-13 14:03:56 +1100812 write_error = read_error = write_errno = num_req = offset = 0;
813 max_req = 1;
814 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +1100815 char *data;
Damien Miller16a13332002-02-13 14:03:56 +1100816 u_int len;
Damien Miller33804262001-02-04 23:20:18 +1100817
Damien Miller16a13332002-02-13 14:03:56 +1100818 /* Send some more requests */
819 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000820 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +0000821 (unsigned long long)offset,
822 (unsigned long long)offset + buflen - 1,
823 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +1100824 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +1100825 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100826 req->len = buflen;
827 req->offset = offset;
828 offset += buflen;
829 num_req++;
830 TAILQ_INSERT_TAIL(&requests, req, tq);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000831 send_read_request(conn->fd_out, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +1100832 req->len, handle, handle_len);
833 }
Damien Miller33804262001-02-04 23:20:18 +1100834
835 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100836 get_msg(conn->fd_in, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100837 type = buffer_get_char(&msg);
838 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000839 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +1100840
Damien Miller16a13332002-02-13 14:03:56 +1100841 /* Find the request in our queue */
842 for(req = TAILQ_FIRST(&requests);
843 req != NULL && req->id != id;
844 req = TAILQ_NEXT(req, tq))
845 ;
846 if (req == NULL)
847 fatal("Unexpected reply %u", id);
848
849 switch (type) {
850 case SSH2_FXP_STATUS:
851 status = buffer_get_int(&msg);
852 if (status != SSH2_FX_EOF)
853 read_error = 1;
854 max_req = 0;
855 TAILQ_REMOVE(&requests, req, tq);
856 xfree(req);
857 num_req--;
858 break;
859 case SSH2_FXP_DATA:
860 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +0000861 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000862 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +0000863 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +1100864 if (len > req->len)
865 fatal("Received more data than asked for "
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000866 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +1100867 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
868 atomicio(write, local_fd, data, len) != len) &&
869 !write_error) {
870 write_errno = errno;
871 write_error = 1;
872 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +1100873 }
Damien Miller16a13332002-02-13 14:03:56 +1100874 xfree(data);
875
876 if (len == req->len) {
877 TAILQ_REMOVE(&requests, req, tq);
878 xfree(req);
879 num_req--;
880 } else {
881 /* Resend the request for the missing data */
882 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +0000883 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000884 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +0000885 (unsigned long long)req->offset +
886 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +1100887 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +1100888 req->len -= len;
889 req->offset += len;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000890 send_read_request(conn->fd_out, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +1100891 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100892 /* Reduce the request size */
893 if (len < buflen)
894 buflen = MAX(MIN_READ_SIZE, len);
895 }
896 if (max_req > 0) { /* max_req = 0 iff EOF received */
897 if (size > 0 && offset > size) {
898 /* Only one request at a time
899 * after the expected EOF */
900 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +0000901 (unsigned long long)offset,
902 num_req);
Damien Miller16a13332002-02-13 14:03:56 +1100903 max_req = 1;
904 }
Damien Miller3db5f532002-02-13 14:10:32 +1100905 else if (max_req < conn->num_requests + 1) {
Damien Miller16a13332002-02-13 14:03:56 +1100906 ++max_req;
907 }
908 }
909 break;
910 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000911 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100912 SSH2_FXP_DATA, type);
913 }
Damien Miller33804262001-02-04 23:20:18 +1100914 }
Damien Miller33804262001-02-04 23:20:18 +1100915
Damien Miller16a13332002-02-13 14:03:56 +1100916 /* Sanity check */
917 if (TAILQ_FIRST(&requests) != NULL)
918 fatal("Transfer complete, but requests still in queue");
919
920 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000921 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +1100922 remote_path, fx2txt(status));
923 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100924 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +1100925 error("Couldn't write to \"%s\": %s", local_path,
926 strerror(write_errno));
927 status = -1;
928 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100929 } else {
Damien Miller3db5f532002-02-13 14:10:32 +1100930 status = do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +1100931
932 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000933#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +1100934 if (pflag && fchmod(local_fd, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000935#else
Damien Miller16a13332002-02-13 14:03:56 +1100936 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +0000937#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +1100938 error("Couldn't set mode on \"%s\": %s", local_path,
939 strerror(errno));
940 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
941 struct timeval tv[2];
942 tv[0].tv_sec = a->atime;
943 tv[1].tv_sec = a->mtime;
944 tv[0].tv_usec = tv[1].tv_usec = 0;
945 if (utimes(local_path, tv) == -1)
946 error("Can't set times on \"%s\": %s",
947 local_path, strerror(errno));
948 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000949 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100950 close(local_fd);
951 buffer_free(&msg);
952 xfree(handle);
Damien Miller3db5f532002-02-13 14:10:32 +1100953
954 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100955}
956
957int
Damien Miller3db5f532002-02-13 14:10:32 +1100958do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
959 int pflag)
Damien Miller33804262001-02-04 23:20:18 +1100960{
Damien Miller8829d362002-02-08 22:04:05 +1100961 int local_fd, status;
Damien Miller5873dfd2002-02-13 14:04:37 +1100962 u_int handle_len, id, type;
Damien Miller33804262001-02-04 23:20:18 +1100963 u_int64_t offset;
Damien Miller8829d362002-02-08 22:04:05 +1100964 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +1100965 Buffer msg;
966 struct stat sb;
967 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +1100968 u_int32_t startid;
969 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +1100970 struct outstanding_ack {
971 u_int id;
972 u_int len;
973 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000974 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +1100975 };
976 TAILQ_HEAD(ackhead, outstanding_ack) acks;
977 struct outstanding_ack *ack;
978
979 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +1100980
981 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
982 error("Couldn't open local file \"%s\" for reading: %s",
983 local_path, strerror(errno));
984 return(-1);
985 }
986 if (fstat(local_fd, &sb) == -1) {
987 error("Couldn't fstat local file \"%s\": %s",
988 local_path, strerror(errno));
989 close(local_fd);
990 return(-1);
991 }
992 stat_to_attrib(&sb, &a);
993
994 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
995 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
996 a.perm &= 0777;
997 if (!pflag)
998 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
999
1000 buffer_init(&msg);
1001
1002 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001003 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001004 buffer_put_char(&msg, SSH2_FXP_OPEN);
1005 buffer_put_int(&msg, id);
1006 buffer_put_cstring(&msg, remote_path);
1007 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1008 encode_attrib(&msg, &a);
Damien Miller3db5f532002-02-13 14:10:32 +11001009 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001010 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001011
1012 buffer_clear(&msg);
1013
Damien Miller3db5f532002-02-13 14:10:32 +11001014 handle = get_handle(conn->fd_in, id, &handle_len);
Damien Miller33804262001-02-04 23:20:18 +11001015 if (handle == NULL) {
1016 close(local_fd);
1017 buffer_free(&msg);
1018 return(-1);
1019 }
1020
Damien Miller16a13332002-02-13 14:03:56 +11001021 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001022 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001023
Damien Miller33804262001-02-04 23:20:18 +11001024 /* Read from local and write to remote */
1025 offset = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001026 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001027 int len;
Damien Miller33804262001-02-04 23:20:18 +11001028
1029 /*
1030 * Can't use atomicio here because it returns 0 on EOF, thus losing
1031 * the last block of the file
1032 */
1033 do
Damien Miller3db5f532002-02-13 14:10:32 +11001034 len = read(local_fd, data, conn->transfer_buflen);
Damien Miller33804262001-02-04 23:20:18 +11001035 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1036
1037 if (len == -1)
1038 fatal("Couldn't read from \"%s\": %s", local_path,
1039 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001040
1041 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001042 ack = xmalloc(sizeof(*ack));
1043 ack->id = ++id;
1044 ack->offset = offset;
1045 ack->len = len;
1046 TAILQ_INSERT_TAIL(&acks, ack, tq);
1047
Damien Miller16a13332002-02-13 14:03:56 +11001048 buffer_clear(&msg);
1049 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001050 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001051 buffer_put_string(&msg, handle, handle_len);
1052 buffer_put_int64(&msg, offset);
1053 buffer_put_string(&msg, data, len);
Damien Miller3db5f532002-02-13 14:10:32 +11001054 send_msg(conn->fd_out, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001055 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstromeb505452002-03-22 01:03:15 +00001056 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001057 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001058 break;
1059
Damien Miller5873dfd2002-02-13 14:04:37 +11001060 if (ack == NULL)
1061 fatal("Unexpected ACK %u", id);
1062
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001063 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001064 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001065 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001066
Damien Miller5873dfd2002-02-13 14:04:37 +11001067 buffer_clear(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001068 get_msg(conn->fd_in, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001069 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001070 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001071
1072 if (type != SSH2_FXP_STATUS)
1073 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1074 "got %d", SSH2_FXP_STATUS, type);
1075
1076 status = buffer_get_int(&msg);
1077 debug3("SSH2_FXP_STATUS %d", status);
1078
1079 /* Find the request in our queue */
1080 for(ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001081 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001082 ack = TAILQ_NEXT(ack, tq))
1083 ;
1084 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001085 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001086 TAILQ_REMOVE(&acks, ack, tq);
1087
Damien Miller16a13332002-02-13 14:03:56 +11001088 if (status != SSH2_FX_OK) {
1089 error("Couldn't write to remote file \"%s\": %s",
1090 remote_path, fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +11001091 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001092 close(local_fd);
1093 goto done;
1094 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001095 debug3("In write loop, ack for %u %u bytes at %llu",
Ben Lindstromeb505452002-03-22 01:03:15 +00001096 ack->id, ack->len, (unsigned long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001097 ++ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001098 free(ack);
Damien Miller33804262001-02-04 23:20:18 +11001099 }
Damien Miller33804262001-02-04 23:20:18 +11001100 offset += len;
1101 }
Damien Miller8829d362002-02-08 22:04:05 +11001102 xfree(data);
Damien Miller33804262001-02-04 23:20:18 +11001103
1104 if (close(local_fd) == -1) {
1105 error("Couldn't close local file \"%s\": %s", local_path,
1106 strerror(errno));
Damien Miller3db5f532002-02-13 14:10:32 +11001107 do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001108 status = -1;
1109 goto done;
Damien Miller33804262001-02-04 23:20:18 +11001110 }
1111
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001112 /* Override umask and utimes if asked */
1113 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001114 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001115
Damien Miller3db5f532002-02-13 14:10:32 +11001116 status = do_close(conn, handle, handle_len);
Damien Millerd7686fd2001-02-10 00:40:03 +11001117
1118done:
1119 xfree(handle);
1120 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +11001121 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001122}