blob: 4fa07e2f55ad8045ce9fb37921674a71253649c7 [file] [log] [blame]
Damien Miller7b28dc52000-09-05 13:34:53 +11001/*
Darren Tucker37bd3662004-02-24 09:19:15 +11002 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
Damien Miller7b28dc52000-09-05 13:34:53 +11003 *
Darren Tucker37bd3662004-02-24 09:19:15 +11004 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
Damien Miller7b28dc52000-09-05 13:34:53 +11007 *
Darren Tucker37bd3662004-02-24 09:19:15 +11008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller7b28dc52000-09-05 13:34:53 +110015 */
16#include "includes.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100017RCSID("$OpenBSD: sftp-server.c,v 1.49 2005/09/13 23:40:07 djm Exp $");
Damien Miller7b28dc52000-09-05 13:34:53 +110018
Damien Miller7b28dc52000-09-05 13:34:53 +110019#include "buffer.h"
20#include "bufaux.h"
21#include "getput.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000022#include "log.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110023#include "xmalloc.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100024#include "misc.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110025
Ben Lindstrom2f959b42001-01-11 06:20:23 +000026#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110027#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110028
29/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000030#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110031#define get_int() buffer_get_int(&iqueue);
32#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Ben Lindstrom2f959b42001-01-11 06:20:23 +000033#define TRACE debug
Damien Miller7b28dc52000-09-05 13:34:53 +110034
Ben Lindstrom49a79c02000-11-17 03:47:20 +000035extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000036
Damien Miller7b28dc52000-09-05 13:34:53 +110037/* input and output queue */
38Buffer iqueue;
39Buffer oqueue;
40
Damien Miller058316f2001-03-08 10:08:49 +110041/* Version of client */
42int version;
43
Darren Tuckera6612d42003-06-28 12:39:03 +100044/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110045
Damien Miller7b28dc52000-09-05 13:34:53 +110046typedef struct Stat Stat;
47
Damien Miller33804262001-02-04 23:20:18 +110048struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110049 char *name;
50 char *long_name;
51 Attrib attrib;
52};
53
Ben Lindstrombba81212001-06-25 05:01:22 +000054static int
Damien Miller7b28dc52000-09-05 13:34:53 +110055errno_to_portable(int unixerrno)
56{
57 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000058
Damien Miller7b28dc52000-09-05 13:34:53 +110059 switch (unixerrno) {
60 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000061 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110062 break;
63 case ENOENT:
64 case ENOTDIR:
65 case EBADF:
66 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000067 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110068 break;
69 case EPERM:
70 case EACCES:
71 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000072 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110073 break;
74 case ENAMETOOLONG:
75 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000076 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +110077 break;
78 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000079 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +110080 break;
81 }
82 return ret;
83}
84
Ben Lindstrombba81212001-06-25 05:01:22 +000085static int
Damien Miller7b28dc52000-09-05 13:34:53 +110086flags_from_portable(int pflags)
87{
88 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000089
Ben Lindstrom36592512001-03-05 05:02:08 +000090 if ((pflags & SSH2_FXF_READ) &&
91 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +110092 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +000093 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +110094 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +000095 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +110096 flags = O_WRONLY;
97 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +000098 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +110099 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000100 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100101 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000102 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100103 flags |= O_EXCL;
104 return flags;
105}
106
Ben Lindstrombba81212001-06-25 05:01:22 +0000107static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100108get_attrib(void)
109{
110 return decode_attrib(&iqueue);
111}
112
113/* handle handles */
114
115typedef struct Handle Handle;
116struct Handle {
117 int use;
118 DIR *dirp;
119 int fd;
120 char *name;
121};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000122
Damien Miller7b28dc52000-09-05 13:34:53 +1100123enum {
124 HANDLE_UNUSED,
125 HANDLE_DIR,
126 HANDLE_FILE
127};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000128
Damien Miller7b28dc52000-09-05 13:34:53 +1100129Handle handles[100];
130
Ben Lindstrombba81212001-06-25 05:01:22 +0000131static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100132handle_init(void)
133{
Damien Millereccb9de2005-06-17 12:59:34 +1000134 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000135
Damien Miller9f0f5c62001-12-21 14:45:46 +1100136 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
Damien Miller7b28dc52000-09-05 13:34:53 +1100137 handles[i].use = HANDLE_UNUSED;
138}
139
Ben Lindstrombba81212001-06-25 05:01:22 +0000140static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100141handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100142{
Damien Millereccb9de2005-06-17 12:59:34 +1000143 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000144
Damien Miller9f0f5c62001-12-21 14:45:46 +1100145 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100146 if (handles[i].use == HANDLE_UNUSED) {
147 handles[i].use = use;
148 handles[i].dirp = dirp;
149 handles[i].fd = fd;
Damien Miller00111382003-03-10 11:21:17 +1100150 handles[i].name = xstrdup(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100151 return i;
152 }
153 }
154 return -1;
155}
156
Ben Lindstrombba81212001-06-25 05:01:22 +0000157static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100158handle_is_ok(int i, int type)
159{
Damien Millereccb9de2005-06-17 12:59:34 +1000160 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000161 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100162}
163
Ben Lindstrombba81212001-06-25 05:01:22 +0000164static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100165handle_to_string(int handle, char **stringp, int *hlenp)
166{
Damien Miller7b28dc52000-09-05 13:34:53 +1100167 if (stringp == NULL || hlenp == NULL)
168 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000169 *stringp = xmalloc(sizeof(int32_t));
170 PUT_32BIT(*stringp, handle);
171 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100172 return 0;
173}
174
Ben Lindstrombba81212001-06-25 05:01:22 +0000175static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100176handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100177{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000178 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000179
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000180 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100181 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000182 val = GET_32BIT(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100183 if (handle_is_ok(val, HANDLE_FILE) ||
184 handle_is_ok(val, HANDLE_DIR))
185 return val;
186 return -1;
187}
188
Ben Lindstrombba81212001-06-25 05:01:22 +0000189static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100190handle_to_name(int handle)
191{
192 if (handle_is_ok(handle, HANDLE_DIR)||
193 handle_is_ok(handle, HANDLE_FILE))
194 return handles[handle].name;
195 return NULL;
196}
197
Ben Lindstrombba81212001-06-25 05:01:22 +0000198static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100199handle_to_dir(int handle)
200{
201 if (handle_is_ok(handle, HANDLE_DIR))
202 return handles[handle].dirp;
203 return NULL;
204}
205
Ben Lindstrombba81212001-06-25 05:01:22 +0000206static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100207handle_to_fd(int handle)
208{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000209 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100210 return handles[handle].fd;
211 return -1;
212}
213
Ben Lindstrombba81212001-06-25 05:01:22 +0000214static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100215handle_close(int handle)
216{
217 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000218
Damien Miller7b28dc52000-09-05 13:34:53 +1100219 if (handle_is_ok(handle, HANDLE_FILE)) {
220 ret = close(handles[handle].fd);
221 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100222 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100223 } else if (handle_is_ok(handle, HANDLE_DIR)) {
224 ret = closedir(handles[handle].dirp);
225 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100226 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100227 } else {
228 errno = ENOENT;
229 }
230 return ret;
231}
232
Ben Lindstrombba81212001-06-25 05:01:22 +0000233static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100234get_handle(void)
235{
236 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000237 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100238 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000239
Damien Miller7b28dc52000-09-05 13:34:53 +1100240 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000241 if (hlen < 256)
242 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100243 xfree(handle);
244 return val;
245}
246
247/* send replies */
248
Ben Lindstrombba81212001-06-25 05:01:22 +0000249static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100250send_msg(Buffer *m)
251{
252 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000253
Damien Miller7b28dc52000-09-05 13:34:53 +1100254 buffer_put_int(&oqueue, mlen);
255 buffer_append(&oqueue, buffer_ptr(m), mlen);
256 buffer_consume(m, mlen);
257}
258
Ben Lindstrombba81212001-06-25 05:01:22 +0000259static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000260send_status(u_int32_t id, u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100261{
262 Buffer msg;
Damien Miller058316f2001-03-08 10:08:49 +1100263 const char *status_messages[] = {
264 "Success", /* SSH_FX_OK */
265 "End of file", /* SSH_FX_EOF */
266 "No such file", /* SSH_FX_NO_SUCH_FILE */
267 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
268 "Failure", /* SSH_FX_FAILURE */
269 "Bad message", /* SSH_FX_BAD_MESSAGE */
270 "No connection", /* SSH_FX_NO_CONNECTION */
271 "Connection lost", /* SSH_FX_CONNECTION_LOST */
272 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
273 "Unknown error" /* Others */
274 };
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000275
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000276 TRACE("sent status id %u error %u", id, status);
Damien Miller7b28dc52000-09-05 13:34:53 +1100277 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000278 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100279 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000280 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100281 if (version >= 3) {
Ben Lindstroma3700052001-04-05 23:26:32 +0000282 buffer_put_cstring(&msg,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000283 status_messages[MIN(status,SSH2_FX_MAX)]);
Damien Miller058316f2001-03-08 10:08:49 +1100284 buffer_put_cstring(&msg, "");
285 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100286 send_msg(&msg);
287 buffer_free(&msg);
288}
Ben Lindstrombba81212001-06-25 05:01:22 +0000289static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100290send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100291{
292 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000293
Damien Miller7b28dc52000-09-05 13:34:53 +1100294 buffer_init(&msg);
295 buffer_put_char(&msg, type);
296 buffer_put_int(&msg, id);
297 buffer_put_string(&msg, data, dlen);
298 send_msg(&msg);
299 buffer_free(&msg);
300}
301
Ben Lindstrombba81212001-06-25 05:01:22 +0000302static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100303send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100304{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000305 TRACE("sent data id %u len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000306 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100307}
308
Ben Lindstrombba81212001-06-25 05:01:22 +0000309static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100310send_handle(u_int32_t id, int handle)
311{
312 char *string;
313 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000314
Damien Miller7b28dc52000-09-05 13:34:53 +1100315 handle_to_string(handle, &string, &hlen);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000316 TRACE("sent handle id %u handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000317 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100318 xfree(string);
319}
320
Ben Lindstrombba81212001-06-25 05:01:22 +0000321static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100322send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100323{
324 Buffer msg;
325 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000326
Damien Miller7b28dc52000-09-05 13:34:53 +1100327 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000328 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100329 buffer_put_int(&msg, id);
330 buffer_put_int(&msg, count);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000331 TRACE("sent names id %u count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100332 for (i = 0; i < count; i++) {
333 buffer_put_cstring(&msg, stats[i].name);
334 buffer_put_cstring(&msg, stats[i].long_name);
335 encode_attrib(&msg, &stats[i].attrib);
336 }
337 send_msg(&msg);
338 buffer_free(&msg);
339}
340
Ben Lindstrombba81212001-06-25 05:01:22 +0000341static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100342send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100343{
344 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000345
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000346 TRACE("sent attrib id %u have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100347 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000348 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100349 buffer_put_int(&msg, id);
350 encode_attrib(&msg, a);
351 send_msg(&msg);
352 buffer_free(&msg);
353}
354
355/* parse incoming */
356
Ben Lindstrombba81212001-06-25 05:01:22 +0000357static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100358process_init(void)
359{
360 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100361
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000362 version = get_int();
Damien Miller7b28dc52000-09-05 13:34:53 +1100363 TRACE("client version %d", version);
364 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000365 buffer_put_char(&msg, SSH2_FXP_VERSION);
366 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100367 send_msg(&msg);
368 buffer_free(&msg);
369}
370
Ben Lindstrombba81212001-06-25 05:01:22 +0000371static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100372process_open(void)
373{
374 u_int32_t id, pflags;
375 Attrib *a;
376 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000377 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100378
379 id = get_int();
380 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000381 pflags = get_int(); /* portable flags */
Damien Miller7b28dc52000-09-05 13:34:53 +1100382 a = get_attrib();
383 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000384 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000385 TRACE("open id %u name %s flags %d mode 0%o", id, name, pflags, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100386 fd = open(name, flags, mode);
387 if (fd < 0) {
388 status = errno_to_portable(errno);
389 } else {
Damien Miller00111382003-03-10 11:21:17 +1100390 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100391 if (handle < 0) {
392 close(fd);
393 } else {
394 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000395 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100396 }
397 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000398 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100399 send_status(id, status);
400 xfree(name);
401}
402
Ben Lindstrombba81212001-06-25 05:01:22 +0000403static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100404process_close(void)
405{
406 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000407 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100408
409 id = get_int();
410 handle = get_handle();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000411 TRACE("close id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100412 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000413 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100414 send_status(id, status);
415}
416
Ben Lindstrombba81212001-06-25 05:01:22 +0000417static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100418process_read(void)
419{
420 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000421 u_int32_t id, len;
422 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100423 u_int64_t off;
424
425 id = get_int();
426 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000427 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100428 len = get_int();
429
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000430 TRACE("read id %u handle %d off %llu len %d", id, handle,
Darren Tuckere0be3042005-11-25 14:44:55 +1100431 (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100432 if (len > sizeof buf) {
433 len = sizeof buf;
Damien Miller996acd22003-04-09 20:59:48 +1000434 logit("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100435 }
436 fd = handle_to_fd(handle);
437 if (fd >= 0) {
438 if (lseek(fd, off, SEEK_SET) < 0) {
439 error("process_read: seek failed");
440 status = errno_to_portable(errno);
441 } else {
442 ret = read(fd, buf, len);
443 if (ret < 0) {
444 status = errno_to_portable(errno);
445 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000446 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100447 } else {
448 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000449 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100450 }
451 }
452 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000453 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100454 send_status(id, status);
455}
456
Ben Lindstrombba81212001-06-25 05:01:22 +0000457static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100458process_write(void)
459{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000460 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100461 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100462 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000463 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100464 char *data;
465
466 id = get_int();
467 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000468 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100469 data = get_string(&len);
470
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000471 TRACE("write id %u handle %d off %llu len %d", id, handle,
Darren Tuckere0be3042005-11-25 14:44:55 +1100472 (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100473 fd = handle_to_fd(handle);
474 if (fd >= 0) {
475 if (lseek(fd, off, SEEK_SET) < 0) {
476 status = errno_to_portable(errno);
477 error("process_write: seek failed");
478 } else {
479/* XXX ATOMICIO ? */
480 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000481 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100482 error("process_write: write failed");
483 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000484 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000485 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100486 } else {
Damien Miller996acd22003-04-09 20:59:48 +1000487 logit("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100488 }
489 }
490 }
491 send_status(id, status);
492 xfree(data);
493}
494
Ben Lindstrombba81212001-06-25 05:01:22 +0000495static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100496process_do_stat(int do_lstat)
497{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000498 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100499 struct stat st;
500 u_int32_t id;
501 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000502 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100503
504 id = get_int();
505 name = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000506 TRACE("%sstat id %u name %s", do_lstat ? "l" : "", id, name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100507 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
508 if (ret < 0) {
509 status = errno_to_portable(errno);
510 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000511 stat_to_attrib(&st, &a);
512 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000513 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100514 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000515 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100516 send_status(id, status);
517 xfree(name);
518}
519
Ben Lindstrombba81212001-06-25 05:01:22 +0000520static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100521process_stat(void)
522{
523 process_do_stat(0);
524}
525
Ben Lindstrombba81212001-06-25 05:01:22 +0000526static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100527process_lstat(void)
528{
529 process_do_stat(1);
530}
531
Ben Lindstrombba81212001-06-25 05:01:22 +0000532static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100533process_fstat(void)
534{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000535 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100536 struct stat st;
537 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000538 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100539
540 id = get_int();
541 handle = get_handle();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000542 TRACE("fstat id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100543 fd = handle_to_fd(handle);
544 if (fd >= 0) {
545 ret = fstat(fd, &st);
546 if (ret < 0) {
547 status = errno_to_portable(errno);
548 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000549 stat_to_attrib(&st, &a);
550 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000551 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100552 }
553 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000554 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100555 send_status(id, status);
556}
557
Ben Lindstrombba81212001-06-25 05:01:22 +0000558static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100559attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100560{
561 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000562
Damien Miller7b28dc52000-09-05 13:34:53 +1100563 tv[0].tv_sec = a->atime;
564 tv[0].tv_usec = 0;
565 tv[1].tv_sec = a->mtime;
566 tv[1].tv_usec = 0;
567 return tv;
568}
569
Ben Lindstrombba81212001-06-25 05:01:22 +0000570static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100571process_setstat(void)
572{
573 Attrib *a;
574 u_int32_t id;
575 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000576 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100577
578 id = get_int();
579 name = get_string(NULL);
580 a = get_attrib();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000581 TRACE("setstat id %u name %s", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100582 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
583 ret = truncate(name, a->size);
584 if (ret == -1)
585 status = errno_to_portable(errno);
586 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000587 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100588 ret = chmod(name, a->perm & 0777);
589 if (ret == -1)
590 status = errno_to_portable(errno);
591 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000592 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100593 ret = utimes(name, attrib_to_tv(a));
594 if (ret == -1)
595 status = errno_to_portable(errno);
596 }
Kevin Steves8e743932001-02-05 13:24:35 +0000597 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
598 ret = chown(name, a->uid, a->gid);
599 if (ret == -1)
600 status = errno_to_portable(errno);
601 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100602 send_status(id, status);
603 xfree(name);
604}
605
Ben Lindstrombba81212001-06-25 05:01:22 +0000606static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100607process_fsetstat(void)
608{
609 Attrib *a;
610 u_int32_t id;
611 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000612 int status = SSH2_FX_OK;
Damien Millere4340be2000-09-16 13:29:08 +1100613 char *name;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000614
Damien Miller7b28dc52000-09-05 13:34:53 +1100615 id = get_int();
616 handle = get_handle();
617 a = get_attrib();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000618 TRACE("fsetstat id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100619 fd = handle_to_fd(handle);
620 name = handle_to_name(handle);
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000621 if (fd < 0 || name == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000622 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100623 } else {
Damien Miller00c92172002-02-13 14:05:00 +1100624 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
625 ret = ftruncate(fd, a->size);
626 if (ret == -1)
627 status = errno_to_portable(errno);
628 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000629 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000630#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100631 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000632#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000633 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000634#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100635 if (ret == -1)
636 status = errno_to_portable(errno);
637 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000638 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100639#ifdef HAVE_FUTIMES
640 ret = futimes(fd, attrib_to_tv(a));
641#else
642 ret = utimes(name, attrib_to_tv(a));
643#endif
644 if (ret == -1)
645 status = errno_to_portable(errno);
646 }
Kevin Steves8e743932001-02-05 13:24:35 +0000647 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000648#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000649 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000650#else
651 ret = chown(name, a->uid, a->gid);
652#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000653 if (ret == -1)
654 status = errno_to_portable(errno);
655 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100656 }
657 send_status(id, status);
658}
659
Ben Lindstrombba81212001-06-25 05:01:22 +0000660static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100661process_opendir(void)
662{
663 DIR *dirp = NULL;
664 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000665 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100666 u_int32_t id;
667
668 id = get_int();
669 path = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000670 TRACE("opendir id %u path %s", id, path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000671 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100672 if (dirp == NULL) {
673 status = errno_to_portable(errno);
674 } else {
Damien Miller00111382003-03-10 11:21:17 +1100675 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100676 if (handle < 0) {
677 closedir(dirp);
678 } else {
679 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000680 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100681 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000682
Damien Miller7b28dc52000-09-05 13:34:53 +1100683 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000684 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100685 send_status(id, status);
686 xfree(path);
687}
688
Ben Lindstrombba81212001-06-25 05:01:22 +0000689static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100690process_readdir(void)
691{
692 DIR *dirp;
693 struct dirent *dp;
694 char *path;
695 int handle;
696 u_int32_t id;
697
698 id = get_int();
699 handle = get_handle();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000700 TRACE("readdir id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100701 dirp = handle_to_dir(handle);
702 path = handle_to_name(handle);
703 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000704 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100705 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100706 struct stat st;
707 char pathname[1024];
708 Stat *stats;
709 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000710
Damien Miller7b28dc52000-09-05 13:34:53 +1100711 stats = xmalloc(nstats * sizeof(Stat));
712 while ((dp = readdir(dirp)) != NULL) {
713 if (count >= nstats) {
714 nstats *= 2;
715 stats = xrealloc(stats, nstats * sizeof(Stat));
716 }
717/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000718 snprintf(pathname, sizeof pathname, "%s%s%s", path,
719 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100720 if (lstat(pathname, &st) < 0)
721 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000722 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100723 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000724 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100725 count++;
726 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000727 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100728 if (count == 100)
729 break;
730 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000731 if (count > 0) {
732 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100733 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000734 xfree(stats[i].name);
735 xfree(stats[i].long_name);
736 }
737 } else {
738 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100739 }
740 xfree(stats);
741 }
742}
743
Ben Lindstrombba81212001-06-25 05:01:22 +0000744static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100745process_remove(void)
746{
747 char *name;
748 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000749 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100750 int ret;
751
752 id = get_int();
753 name = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000754 TRACE("remove id %u name %s", id, name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000755 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000756 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100757 send_status(id, status);
758 xfree(name);
759}
760
Ben Lindstrombba81212001-06-25 05:01:22 +0000761static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100762process_mkdir(void)
763{
764 Attrib *a;
765 u_int32_t id;
766 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000767 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100768
769 id = get_int();
770 name = get_string(NULL);
771 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000772 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
773 a->perm & 0777 : 0777;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000774 TRACE("mkdir id %u name %s mode 0%o", id, name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100775 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000776 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100777 send_status(id, status);
778 xfree(name);
779}
780
Ben Lindstrombba81212001-06-25 05:01:22 +0000781static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100782process_rmdir(void)
783{
784 u_int32_t id;
785 char *name;
786 int ret, status;
787
788 id = get_int();
789 name = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000790 TRACE("rmdir id %u name %s", id, name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100791 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000792 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100793 send_status(id, status);
794 xfree(name);
795}
796
Ben Lindstrombba81212001-06-25 05:01:22 +0000797static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100798process_realpath(void)
799{
800 char resolvedname[MAXPATHLEN];
801 u_int32_t id;
802 char *path;
803
804 id = get_int();
805 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000806 if (path[0] == '\0') {
807 xfree(path);
808 path = xstrdup(".");
809 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000810 TRACE("realpath id %u path %s", id, path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100811 if (realpath(path, resolvedname) == NULL) {
812 send_status(id, errno_to_portable(errno));
813 } else {
814 Stat s;
815 attrib_clear(&s.attrib);
816 s.name = s.long_name = resolvedname;
817 send_names(id, 1, &s);
818 }
819 xfree(path);
820}
821
Ben Lindstrombba81212001-06-25 05:01:22 +0000822static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100823process_rename(void)
824{
825 u_int32_t id;
826 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100827 int status;
Damien Millerb3207e82003-03-26 16:01:11 +1100828 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +1100829
830 id = get_int();
831 oldpath = get_string(NULL);
832 newpath = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000833 TRACE("rename id %u old %s new %s", id, oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +1100834 status = SSH2_FX_FAILURE;
835 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +1100836 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +1100837 else if (S_ISREG(sb.st_mode)) {
838 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000839 if (link(oldpath, newpath) == -1) {
Darren Tuckere59b5082004-06-28 16:01:19 +1000840 if (errno == EOPNOTSUPP
841#ifdef LINK_OPNOTSUPP_ERRNO
842 || errno == LINK_OPNOTSUPP_ERRNO
843#endif
844 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000845 struct stat st;
846
847 /*
848 * fs doesn't support links, so fall back to
849 * stat+rename. This is racy.
850 */
851 if (stat(newpath, &st) == -1) {
852 if (rename(oldpath, newpath) == -1)
853 status =
854 errno_to_portable(errno);
855 else
856 status = SSH2_FX_OK;
857 }
858 } else {
859 status = errno_to_portable(errno);
860 }
861 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +1100862 status = errno_to_portable(errno);
863 /* clean spare link */
864 unlink(newpath);
865 } else
866 status = SSH2_FX_OK;
867 } else if (stat(newpath, &sb) == -1) {
868 if (rename(oldpath, newpath) == -1)
869 status = errno_to_portable(errno);
870 else
871 status = SSH2_FX_OK;
872 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100873 send_status(id, status);
874 xfree(oldpath);
875 xfree(newpath);
876}
877
Ben Lindstrombba81212001-06-25 05:01:22 +0000878static void
Damien Miller058316f2001-03-08 10:08:49 +1100879process_readlink(void)
880{
881 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +0000882 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000883 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +1100884 char *path;
885
886 id = get_int();
887 path = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000888 TRACE("readlink id %u path %s", id, path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000889 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +1100890 send_status(id, errno_to_portable(errno));
891 else {
892 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100893
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000894 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +1100895 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000896 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +1100897 send_names(id, 1, &s);
898 }
899 xfree(path);
900}
901
Ben Lindstrombba81212001-06-25 05:01:22 +0000902static void
Damien Miller058316f2001-03-08 10:08:49 +1100903process_symlink(void)
904{
905 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +1100906 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100907 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +1100908
909 id = get_int();
910 oldpath = get_string(NULL);
911 newpath = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000912 TRACE("symlink id %u old %s new %s", id, oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +1100913 /* this will fail if 'newpath' exists */
914 ret = symlink(oldpath, newpath);
915 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +1100916 send_status(id, status);
917 xfree(oldpath);
918 xfree(newpath);
919}
920
Ben Lindstrombba81212001-06-25 05:01:22 +0000921static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000922process_extended(void)
923{
924 u_int32_t id;
925 char *request;
926
927 id = get_int();
928 request = get_string(NULL);
929 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
930 xfree(request);
931}
Damien Miller7b28dc52000-09-05 13:34:53 +1100932
933/* stolen from ssh-agent */
934
Ben Lindstrombba81212001-06-25 05:01:22 +0000935static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100936process(void)
937{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000938 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +0000939 u_int buf_len;
940 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000941 u_int type;
942 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +1100943
Ben Lindstrom2c140472002-06-06 21:57:54 +0000944 buf_len = buffer_len(&iqueue);
945 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +1100946 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100947 cp = buffer_ptr(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +1100948 msg_len = GET_32BIT(cp);
949 if (msg_len > 256 * 1024) {
950 error("bad message ");
951 exit(11);
952 }
Ben Lindstrom2c140472002-06-06 21:57:54 +0000953 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +1100954 return;
955 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +0000956 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +1100957 type = buffer_get_char(&iqueue);
958 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000959 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100960 process_init();
961 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000962 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +1100963 process_open();
964 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000965 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100966 process_close();
967 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000968 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +1100969 process_read();
970 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000971 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100972 process_write();
973 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000974 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100975 process_lstat();
976 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000977 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100978 process_fstat();
979 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000980 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100981 process_setstat();
982 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000983 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100984 process_fsetstat();
985 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000986 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100987 process_opendir();
988 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000989 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100990 process_readdir();
991 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000992 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100993 process_remove();
994 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000995 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100996 process_mkdir();
997 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000998 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100999 process_rmdir();
1000 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001001 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001002 process_realpath();
1003 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001004 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001005 process_stat();
1006 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001007 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001008 process_rename();
1009 break;
Damien Miller058316f2001-03-08 10:08:49 +11001010 case SSH2_FXP_READLINK:
1011 process_readlink();
1012 break;
1013 case SSH2_FXP_SYMLINK:
1014 process_symlink();
1015 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001016 case SSH2_FXP_EXTENDED:
1017 process_extended();
1018 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001019 default:
1020 error("Unknown message %d", type);
1021 break;
1022 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001023 /* discard the remaining bytes from the current packet */
1024 if (buf_len < buffer_len(&iqueue))
1025 fatal("iqueue grows");
1026 consumed = buf_len - buffer_len(&iqueue);
1027 if (msg_len < consumed)
1028 fatal("msg_len %d < consumed %d", msg_len, consumed);
1029 if (msg_len > consumed)
1030 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001031}
1032
1033int
1034main(int ac, char **av)
1035{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001036 fd_set *rset, *wset;
Damien Miller7b28dc52000-09-05 13:34:53 +11001037 int in, out, max;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001038 ssize_t len, olen, set_size;
Damien Miller7b28dc52000-09-05 13:34:53 +11001039
Darren Tuckerce321d82005-10-03 18:11:24 +10001040 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1041 sanitise_stdfd();
1042
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001043 /* XXX should use getopt */
1044
Damien Miller59d3d5b2003-08-22 09:34:41 +10001045 __progname = ssh_get_progname(av[0]);
Damien Miller7b28dc52000-09-05 13:34:53 +11001046 handle_init();
1047
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001048#ifdef DEBUG_SFTP_SERVER
Kevin Stevesef4eea92001-02-05 12:42:17 +00001049 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001050#endif
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001051
Damien Miller7b28dc52000-09-05 13:34:53 +11001052 in = dup(STDIN_FILENO);
1053 out = dup(STDOUT_FILENO);
1054
Damien Miller402b3312001-04-14 00:28:42 +10001055#ifdef HAVE_CYGWIN
1056 setmode(in, O_BINARY);
1057 setmode(out, O_BINARY);
1058#endif
1059
Damien Miller7b28dc52000-09-05 13:34:53 +11001060 max = 0;
1061 if (in > max)
1062 max = in;
1063 if (out > max)
1064 max = out;
1065
1066 buffer_init(&iqueue);
1067 buffer_init(&oqueue);
1068
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001069 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1070 rset = (fd_set *)xmalloc(set_size);
1071 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001072
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001073 for (;;) {
1074 memset(rset, 0, set_size);
1075 memset(wset, 0, set_size);
1076
1077 FD_SET(in, rset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001078 olen = buffer_len(&oqueue);
1079 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001080 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001081
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001082 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001083 if (errno == EINTR)
1084 continue;
1085 exit(2);
1086 }
1087
1088 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001089 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001090 char buf[4*4096];
1091 len = read(in, buf, sizeof buf);
1092 if (len == 0) {
1093 debug("read eof");
1094 exit(0);
1095 } else if (len < 0) {
1096 error("read error");
1097 exit(1);
1098 } else {
1099 buffer_append(&iqueue, buf, len);
1100 }
1101 }
1102 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001103 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001104 len = write(out, buffer_ptr(&oqueue), olen);
1105 if (len < 0) {
1106 error("write error");
1107 exit(1);
1108 } else {
1109 buffer_consume(&oqueue, len);
1110 }
1111 }
1112 /* process requests from client */
1113 process();
1114 }
1115}