blob: 39a6bdab4e092a3e9897c9445e25e3e1125c5e53 [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 Tuckeraedc1d62004-06-25 17:06:02 +100017RCSID("$OpenBSD: sftp-server.c,v 1.47 2004/06/25 05:38:48 dtucker 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"
24
Ben Lindstrom2f959b42001-01-11 06:20:23 +000025#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110026#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110027
28/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000029#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110030#define get_int() buffer_get_int(&iqueue);
31#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Ben Lindstrom2f959b42001-01-11 06:20:23 +000032#define TRACE debug
Damien Miller7b28dc52000-09-05 13:34:53 +110033
Ben Lindstrom49a79c02000-11-17 03:47:20 +000034#ifdef HAVE___PROGNAME
35extern char *__progname;
36#else
37char *__progname;
38#endif
39
Damien Miller7b28dc52000-09-05 13:34:53 +110040/* input and output queue */
41Buffer iqueue;
42Buffer oqueue;
43
Damien Miller058316f2001-03-08 10:08:49 +110044/* Version of client */
45int version;
46
Darren Tuckera6612d42003-06-28 12:39:03 +100047/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110048
Damien Miller7b28dc52000-09-05 13:34:53 +110049typedef struct Stat Stat;
50
Damien Miller33804262001-02-04 23:20:18 +110051struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110052 char *name;
53 char *long_name;
54 Attrib attrib;
55};
56
Ben Lindstrombba81212001-06-25 05:01:22 +000057static int
Damien Miller7b28dc52000-09-05 13:34:53 +110058errno_to_portable(int unixerrno)
59{
60 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000061
Damien Miller7b28dc52000-09-05 13:34:53 +110062 switch (unixerrno) {
63 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000064 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110065 break;
66 case ENOENT:
67 case ENOTDIR:
68 case EBADF:
69 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000070 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110071 break;
72 case EPERM:
73 case EACCES:
74 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000075 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110076 break;
77 case ENAMETOOLONG:
78 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000079 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +110080 break;
81 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000082 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +110083 break;
84 }
85 return ret;
86}
87
Ben Lindstrombba81212001-06-25 05:01:22 +000088static int
Damien Miller7b28dc52000-09-05 13:34:53 +110089flags_from_portable(int pflags)
90{
91 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000092
Ben Lindstrom36592512001-03-05 05:02:08 +000093 if ((pflags & SSH2_FXF_READ) &&
94 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +110095 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +000096 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +110097 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +000098 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +110099 flags = O_WRONLY;
100 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000101 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100102 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000103 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100104 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000105 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100106 flags |= O_EXCL;
107 return flags;
108}
109
Ben Lindstrombba81212001-06-25 05:01:22 +0000110static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100111get_attrib(void)
112{
113 return decode_attrib(&iqueue);
114}
115
116/* handle handles */
117
118typedef struct Handle Handle;
119struct Handle {
120 int use;
121 DIR *dirp;
122 int fd;
123 char *name;
124};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000125
Damien Miller7b28dc52000-09-05 13:34:53 +1100126enum {
127 HANDLE_UNUSED,
128 HANDLE_DIR,
129 HANDLE_FILE
130};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000131
Damien Miller7b28dc52000-09-05 13:34:53 +1100132Handle handles[100];
133
Ben Lindstrombba81212001-06-25 05:01:22 +0000134static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100135handle_init(void)
136{
137 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000138
Damien Miller9f0f5c62001-12-21 14:45:46 +1100139 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
Damien Miller7b28dc52000-09-05 13:34:53 +1100140 handles[i].use = HANDLE_UNUSED;
141}
142
Ben Lindstrombba81212001-06-25 05:01:22 +0000143static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100144handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100145{
146 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000147
Damien Miller9f0f5c62001-12-21 14:45:46 +1100148 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100149 if (handles[i].use == HANDLE_UNUSED) {
150 handles[i].use = use;
151 handles[i].dirp = dirp;
152 handles[i].fd = fd;
Damien Miller00111382003-03-10 11:21:17 +1100153 handles[i].name = xstrdup(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100154 return i;
155 }
156 }
157 return -1;
158}
159
Ben Lindstrombba81212001-06-25 05:01:22 +0000160static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100161handle_is_ok(int i, int type)
162{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000163 return i >= 0 && i < sizeof(handles)/sizeof(Handle) &&
164 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100165}
166
Ben Lindstrombba81212001-06-25 05:01:22 +0000167static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100168handle_to_string(int handle, char **stringp, int *hlenp)
169{
Damien Miller7b28dc52000-09-05 13:34:53 +1100170 if (stringp == NULL || hlenp == NULL)
171 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000172 *stringp = xmalloc(sizeof(int32_t));
173 PUT_32BIT(*stringp, handle);
174 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100175 return 0;
176}
177
Ben Lindstrombba81212001-06-25 05:01:22 +0000178static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100179handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100180{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000181 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000182
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000183 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100184 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000185 val = GET_32BIT(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100186 if (handle_is_ok(val, HANDLE_FILE) ||
187 handle_is_ok(val, HANDLE_DIR))
188 return val;
189 return -1;
190}
191
Ben Lindstrombba81212001-06-25 05:01:22 +0000192static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100193handle_to_name(int handle)
194{
195 if (handle_is_ok(handle, HANDLE_DIR)||
196 handle_is_ok(handle, HANDLE_FILE))
197 return handles[handle].name;
198 return NULL;
199}
200
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100202handle_to_dir(int handle)
203{
204 if (handle_is_ok(handle, HANDLE_DIR))
205 return handles[handle].dirp;
206 return NULL;
207}
208
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100210handle_to_fd(int handle)
211{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000212 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100213 return handles[handle].fd;
214 return -1;
215}
216
Ben Lindstrombba81212001-06-25 05:01:22 +0000217static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100218handle_close(int handle)
219{
220 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000221
Damien Miller7b28dc52000-09-05 13:34:53 +1100222 if (handle_is_ok(handle, HANDLE_FILE)) {
223 ret = close(handles[handle].fd);
224 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100225 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100226 } else if (handle_is_ok(handle, HANDLE_DIR)) {
227 ret = closedir(handles[handle].dirp);
228 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100229 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100230 } else {
231 errno = ENOENT;
232 }
233 return ret;
234}
235
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100237get_handle(void)
238{
239 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000240 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100241 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000242
Damien Miller7b28dc52000-09-05 13:34:53 +1100243 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000244 if (hlen < 256)
245 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100246 xfree(handle);
247 return val;
248}
249
250/* send replies */
251
Ben Lindstrombba81212001-06-25 05:01:22 +0000252static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100253send_msg(Buffer *m)
254{
255 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000256
Damien Miller7b28dc52000-09-05 13:34:53 +1100257 buffer_put_int(&oqueue, mlen);
258 buffer_append(&oqueue, buffer_ptr(m), mlen);
259 buffer_consume(m, mlen);
260}
261
Ben Lindstrombba81212001-06-25 05:01:22 +0000262static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000263send_status(u_int32_t id, u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100264{
265 Buffer msg;
Damien Miller058316f2001-03-08 10:08:49 +1100266 const char *status_messages[] = {
267 "Success", /* SSH_FX_OK */
268 "End of file", /* SSH_FX_EOF */
269 "No such file", /* SSH_FX_NO_SUCH_FILE */
270 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
271 "Failure", /* SSH_FX_FAILURE */
272 "Bad message", /* SSH_FX_BAD_MESSAGE */
273 "No connection", /* SSH_FX_NO_CONNECTION */
274 "Connection lost", /* SSH_FX_CONNECTION_LOST */
275 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
276 "Unknown error" /* Others */
277 };
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000278
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000279 TRACE("sent status id %u error %u", id, status);
Damien Miller7b28dc52000-09-05 13:34:53 +1100280 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000281 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100282 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000283 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100284 if (version >= 3) {
Ben Lindstroma3700052001-04-05 23:26:32 +0000285 buffer_put_cstring(&msg,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000286 status_messages[MIN(status,SSH2_FX_MAX)]);
Damien Miller058316f2001-03-08 10:08:49 +1100287 buffer_put_cstring(&msg, "");
288 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100289 send_msg(&msg);
290 buffer_free(&msg);
291}
Ben Lindstrombba81212001-06-25 05:01:22 +0000292static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100293send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100294{
295 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000296
Damien Miller7b28dc52000-09-05 13:34:53 +1100297 buffer_init(&msg);
298 buffer_put_char(&msg, type);
299 buffer_put_int(&msg, id);
300 buffer_put_string(&msg, data, dlen);
301 send_msg(&msg);
302 buffer_free(&msg);
303}
304
Ben Lindstrombba81212001-06-25 05:01:22 +0000305static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100306send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100307{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000308 TRACE("sent data id %u len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000309 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100310}
311
Ben Lindstrombba81212001-06-25 05:01:22 +0000312static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100313send_handle(u_int32_t id, int handle)
314{
315 char *string;
316 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000317
Damien Miller7b28dc52000-09-05 13:34:53 +1100318 handle_to_string(handle, &string, &hlen);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000319 TRACE("sent handle id %u handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000320 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100321 xfree(string);
322}
323
Ben Lindstrombba81212001-06-25 05:01:22 +0000324static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100325send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100326{
327 Buffer msg;
328 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000329
Damien Miller7b28dc52000-09-05 13:34:53 +1100330 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000331 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100332 buffer_put_int(&msg, id);
333 buffer_put_int(&msg, count);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000334 TRACE("sent names id %u count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100335 for (i = 0; i < count; i++) {
336 buffer_put_cstring(&msg, stats[i].name);
337 buffer_put_cstring(&msg, stats[i].long_name);
338 encode_attrib(&msg, &stats[i].attrib);
339 }
340 send_msg(&msg);
341 buffer_free(&msg);
342}
343
Ben Lindstrombba81212001-06-25 05:01:22 +0000344static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100345send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100346{
347 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000348
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000349 TRACE("sent attrib id %u have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100350 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000351 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100352 buffer_put_int(&msg, id);
353 encode_attrib(&msg, a);
354 send_msg(&msg);
355 buffer_free(&msg);
356}
357
358/* parse incoming */
359
Ben Lindstrombba81212001-06-25 05:01:22 +0000360static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100361process_init(void)
362{
363 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100364
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000365 version = get_int();
Damien Miller7b28dc52000-09-05 13:34:53 +1100366 TRACE("client version %d", version);
367 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000368 buffer_put_char(&msg, SSH2_FXP_VERSION);
369 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100370 send_msg(&msg);
371 buffer_free(&msg);
372}
373
Ben Lindstrombba81212001-06-25 05:01:22 +0000374static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100375process_open(void)
376{
377 u_int32_t id, pflags;
378 Attrib *a;
379 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000380 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100381
382 id = get_int();
383 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000384 pflags = get_int(); /* portable flags */
Damien Miller7b28dc52000-09-05 13:34:53 +1100385 a = get_attrib();
386 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000387 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000388 TRACE("open id %u name %s flags %d mode 0%o", id, name, pflags, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100389 fd = open(name, flags, mode);
390 if (fd < 0) {
391 status = errno_to_portable(errno);
392 } else {
Damien Miller00111382003-03-10 11:21:17 +1100393 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100394 if (handle < 0) {
395 close(fd);
396 } else {
397 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000398 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100399 }
400 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000401 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100402 send_status(id, status);
403 xfree(name);
404}
405
Ben Lindstrombba81212001-06-25 05:01:22 +0000406static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100407process_close(void)
408{
409 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000410 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100411
412 id = get_int();
413 handle = get_handle();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000414 TRACE("close id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100415 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000416 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100417 send_status(id, status);
418}
419
Ben Lindstrombba81212001-06-25 05:01:22 +0000420static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100421process_read(void)
422{
423 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000424 u_int32_t id, len;
425 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100426 u_int64_t off;
427
428 id = get_int();
429 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000430 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100431 len = get_int();
432
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000433 TRACE("read id %u handle %d off %llu len %d", id, handle,
Ben Lindstrom416d8742001-02-25 02:02:43 +0000434 (u_int64_t)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100435 if (len > sizeof buf) {
436 len = sizeof buf;
Damien Miller996acd22003-04-09 20:59:48 +1000437 logit("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100438 }
439 fd = handle_to_fd(handle);
440 if (fd >= 0) {
441 if (lseek(fd, off, SEEK_SET) < 0) {
442 error("process_read: seek failed");
443 status = errno_to_portable(errno);
444 } else {
445 ret = read(fd, buf, len);
446 if (ret < 0) {
447 status = errno_to_portable(errno);
448 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000449 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100450 } else {
451 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000452 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100453 }
454 }
455 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000456 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100457 send_status(id, status);
458}
459
Ben Lindstrombba81212001-06-25 05:01:22 +0000460static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100461process_write(void)
462{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000463 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100464 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100465 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000466 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100467 char *data;
468
469 id = get_int();
470 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000471 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100472 data = get_string(&len);
473
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000474 TRACE("write id %u handle %d off %llu len %d", id, handle,
Ben Lindstrom416d8742001-02-25 02:02:43 +0000475 (u_int64_t)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100476 fd = handle_to_fd(handle);
477 if (fd >= 0) {
478 if (lseek(fd, off, SEEK_SET) < 0) {
479 status = errno_to_portable(errno);
480 error("process_write: seek failed");
481 } else {
482/* XXX ATOMICIO ? */
483 ret = write(fd, data, len);
484 if (ret == -1) {
485 error("process_write: write failed");
486 status = errno_to_portable(errno);
487 } else if (ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000488 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100489 } else {
Damien Miller996acd22003-04-09 20:59:48 +1000490 logit("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100491 }
492 }
493 }
494 send_status(id, status);
495 xfree(data);
496}
497
Ben Lindstrombba81212001-06-25 05:01:22 +0000498static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100499process_do_stat(int do_lstat)
500{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000501 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100502 struct stat st;
503 u_int32_t id;
504 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000505 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100506
507 id = get_int();
508 name = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000509 TRACE("%sstat id %u name %s", do_lstat ? "l" : "", id, name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100510 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
511 if (ret < 0) {
512 status = errno_to_portable(errno);
513 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000514 stat_to_attrib(&st, &a);
515 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000516 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100517 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000518 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100519 send_status(id, status);
520 xfree(name);
521}
522
Ben Lindstrombba81212001-06-25 05:01:22 +0000523static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100524process_stat(void)
525{
526 process_do_stat(0);
527}
528
Ben Lindstrombba81212001-06-25 05:01:22 +0000529static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100530process_lstat(void)
531{
532 process_do_stat(1);
533}
534
Ben Lindstrombba81212001-06-25 05:01:22 +0000535static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100536process_fstat(void)
537{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000538 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100539 struct stat st;
540 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000541 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100542
543 id = get_int();
544 handle = get_handle();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000545 TRACE("fstat id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100546 fd = handle_to_fd(handle);
547 if (fd >= 0) {
548 ret = fstat(fd, &st);
549 if (ret < 0) {
550 status = errno_to_portable(errno);
551 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000552 stat_to_attrib(&st, &a);
553 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000554 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100555 }
556 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000557 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100558 send_status(id, status);
559}
560
Ben Lindstrombba81212001-06-25 05:01:22 +0000561static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100562attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100563{
564 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000565
Damien Miller7b28dc52000-09-05 13:34:53 +1100566 tv[0].tv_sec = a->atime;
567 tv[0].tv_usec = 0;
568 tv[1].tv_sec = a->mtime;
569 tv[1].tv_usec = 0;
570 return tv;
571}
572
Ben Lindstrombba81212001-06-25 05:01:22 +0000573static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100574process_setstat(void)
575{
576 Attrib *a;
577 u_int32_t id;
578 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000579 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100580
581 id = get_int();
582 name = get_string(NULL);
583 a = get_attrib();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000584 TRACE("setstat id %u name %s", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100585 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
586 ret = truncate(name, a->size);
587 if (ret == -1)
588 status = errno_to_portable(errno);
589 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000590 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100591 ret = chmod(name, a->perm & 0777);
592 if (ret == -1)
593 status = errno_to_portable(errno);
594 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000595 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100596 ret = utimes(name, attrib_to_tv(a));
597 if (ret == -1)
598 status = errno_to_portable(errno);
599 }
Kevin Steves8e743932001-02-05 13:24:35 +0000600 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
601 ret = chown(name, a->uid, a->gid);
602 if (ret == -1)
603 status = errno_to_portable(errno);
604 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100605 send_status(id, status);
606 xfree(name);
607}
608
Ben Lindstrombba81212001-06-25 05:01:22 +0000609static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100610process_fsetstat(void)
611{
612 Attrib *a;
613 u_int32_t id;
614 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000615 int status = SSH2_FX_OK;
Damien Millere4340be2000-09-16 13:29:08 +1100616 char *name;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000617
Damien Miller7b28dc52000-09-05 13:34:53 +1100618 id = get_int();
619 handle = get_handle();
620 a = get_attrib();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000621 TRACE("fsetstat id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100622 fd = handle_to_fd(handle);
623 name = handle_to_name(handle);
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000624 if (fd < 0 || name == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000625 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100626 } else {
Damien Miller00c92172002-02-13 14:05:00 +1100627 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
628 ret = ftruncate(fd, a->size);
629 if (ret == -1)
630 status = errno_to_portable(errno);
631 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000632 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000633#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100634 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000635#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000636 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000637#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100638 if (ret == -1)
639 status = errno_to_portable(errno);
640 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000641 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100642#ifdef HAVE_FUTIMES
643 ret = futimes(fd, attrib_to_tv(a));
644#else
645 ret = utimes(name, attrib_to_tv(a));
646#endif
647 if (ret == -1)
648 status = errno_to_portable(errno);
649 }
Kevin Steves8e743932001-02-05 13:24:35 +0000650 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000651#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000652 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000653#else
654 ret = chown(name, a->uid, a->gid);
655#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000656 if (ret == -1)
657 status = errno_to_portable(errno);
658 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100659 }
660 send_status(id, status);
661}
662
Ben Lindstrombba81212001-06-25 05:01:22 +0000663static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100664process_opendir(void)
665{
666 DIR *dirp = NULL;
667 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000668 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100669 u_int32_t id;
670
671 id = get_int();
672 path = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000673 TRACE("opendir id %u path %s", id, path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000674 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100675 if (dirp == NULL) {
676 status = errno_to_portable(errno);
677 } else {
Damien Miller00111382003-03-10 11:21:17 +1100678 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100679 if (handle < 0) {
680 closedir(dirp);
681 } else {
682 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000683 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100684 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000685
Damien Miller7b28dc52000-09-05 13:34:53 +1100686 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000687 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100688 send_status(id, status);
689 xfree(path);
690}
691
Ben Lindstrombba81212001-06-25 05:01:22 +0000692static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100693process_readdir(void)
694{
695 DIR *dirp;
696 struct dirent *dp;
697 char *path;
698 int handle;
699 u_int32_t id;
700
701 id = get_int();
702 handle = get_handle();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000703 TRACE("readdir id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100704 dirp = handle_to_dir(handle);
705 path = handle_to_name(handle);
706 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000707 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100708 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100709 struct stat st;
710 char pathname[1024];
711 Stat *stats;
712 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000713
Damien Miller7b28dc52000-09-05 13:34:53 +1100714 stats = xmalloc(nstats * sizeof(Stat));
715 while ((dp = readdir(dirp)) != NULL) {
716 if (count >= nstats) {
717 nstats *= 2;
718 stats = xrealloc(stats, nstats * sizeof(Stat));
719 }
720/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000721 snprintf(pathname, sizeof pathname, "%s%s%s", path,
722 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100723 if (lstat(pathname, &st) < 0)
724 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000725 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100726 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000727 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100728 count++;
729 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000730 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100731 if (count == 100)
732 break;
733 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000734 if (count > 0) {
735 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100736 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000737 xfree(stats[i].name);
738 xfree(stats[i].long_name);
739 }
740 } else {
741 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100742 }
743 xfree(stats);
744 }
745}
746
Ben Lindstrombba81212001-06-25 05:01:22 +0000747static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100748process_remove(void)
749{
750 char *name;
751 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000752 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100753 int ret;
754
755 id = get_int();
756 name = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000757 TRACE("remove id %u name %s", id, name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000758 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000759 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100760 send_status(id, status);
761 xfree(name);
762}
763
Ben Lindstrombba81212001-06-25 05:01:22 +0000764static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100765process_mkdir(void)
766{
767 Attrib *a;
768 u_int32_t id;
769 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000770 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100771
772 id = get_int();
773 name = get_string(NULL);
774 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000775 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
776 a->perm & 0777 : 0777;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000777 TRACE("mkdir id %u name %s mode 0%o", id, name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100778 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000779 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100780 send_status(id, status);
781 xfree(name);
782}
783
Ben Lindstrombba81212001-06-25 05:01:22 +0000784static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100785process_rmdir(void)
786{
787 u_int32_t id;
788 char *name;
789 int ret, status;
790
791 id = get_int();
792 name = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000793 TRACE("rmdir id %u name %s", id, name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100794 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000795 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100796 send_status(id, status);
797 xfree(name);
798}
799
Ben Lindstrombba81212001-06-25 05:01:22 +0000800static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100801process_realpath(void)
802{
803 char resolvedname[MAXPATHLEN];
804 u_int32_t id;
805 char *path;
806
807 id = get_int();
808 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000809 if (path[0] == '\0') {
810 xfree(path);
811 path = xstrdup(".");
812 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000813 TRACE("realpath id %u path %s", id, path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100814 if (realpath(path, resolvedname) == NULL) {
815 send_status(id, errno_to_portable(errno));
816 } else {
817 Stat s;
818 attrib_clear(&s.attrib);
819 s.name = s.long_name = resolvedname;
820 send_names(id, 1, &s);
821 }
822 xfree(path);
823}
824
Ben Lindstrombba81212001-06-25 05:01:22 +0000825static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100826process_rename(void)
827{
828 u_int32_t id;
829 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100830 int status;
Damien Millerb3207e82003-03-26 16:01:11 +1100831 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +1100832
833 id = get_int();
834 oldpath = get_string(NULL);
835 newpath = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000836 TRACE("rename id %u old %s new %s", id, oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +1100837 status = SSH2_FX_FAILURE;
838 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +1100839 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +1100840 else if (S_ISREG(sb.st_mode)) {
841 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000842 if (link(oldpath, newpath) == -1) {
843 if (errno == EOPNOTSUPP) {
844 struct stat st;
845
846 /*
847 * fs doesn't support links, so fall back to
848 * stat+rename. This is racy.
849 */
850 if (stat(newpath, &st) == -1) {
851 if (rename(oldpath, newpath) == -1)
852 status =
853 errno_to_portable(errno);
854 else
855 status = SSH2_FX_OK;
856 }
857 } else {
858 status = errno_to_portable(errno);
859 }
860 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +1100861 status = errno_to_portable(errno);
862 /* clean spare link */
863 unlink(newpath);
864 } else
865 status = SSH2_FX_OK;
866 } else if (stat(newpath, &sb) == -1) {
867 if (rename(oldpath, newpath) == -1)
868 status = errno_to_portable(errno);
869 else
870 status = SSH2_FX_OK;
871 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100872 send_status(id, status);
873 xfree(oldpath);
874 xfree(newpath);
875}
876
Ben Lindstrombba81212001-06-25 05:01:22 +0000877static void
Damien Miller058316f2001-03-08 10:08:49 +1100878process_readlink(void)
879{
880 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +0000881 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000882 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +1100883 char *path;
884
885 id = get_int();
886 path = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000887 TRACE("readlink id %u path %s", id, path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000888 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +1100889 send_status(id, errno_to_portable(errno));
890 else {
891 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100892
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000893 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +1100894 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000895 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +1100896 send_names(id, 1, &s);
897 }
898 xfree(path);
899}
900
Ben Lindstrombba81212001-06-25 05:01:22 +0000901static void
Damien Miller058316f2001-03-08 10:08:49 +1100902process_symlink(void)
903{
904 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +1100905 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100906 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +1100907
908 id = get_int();
909 oldpath = get_string(NULL);
910 newpath = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000911 TRACE("symlink id %u old %s new %s", id, oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +1100912 /* this will fail if 'newpath' exists */
913 ret = symlink(oldpath, newpath);
914 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +1100915 send_status(id, status);
916 xfree(oldpath);
917 xfree(newpath);
918}
919
Ben Lindstrombba81212001-06-25 05:01:22 +0000920static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000921process_extended(void)
922{
923 u_int32_t id;
924 char *request;
925
926 id = get_int();
927 request = get_string(NULL);
928 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
929 xfree(request);
930}
Damien Miller7b28dc52000-09-05 13:34:53 +1100931
932/* stolen from ssh-agent */
933
Ben Lindstrombba81212001-06-25 05:01:22 +0000934static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100935process(void)
936{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000937 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +0000938 u_int buf_len;
939 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000940 u_int type;
941 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +1100942
Ben Lindstrom2c140472002-06-06 21:57:54 +0000943 buf_len = buffer_len(&iqueue);
944 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +1100945 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100946 cp = buffer_ptr(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +1100947 msg_len = GET_32BIT(cp);
948 if (msg_len > 256 * 1024) {
949 error("bad message ");
950 exit(11);
951 }
Ben Lindstrom2c140472002-06-06 21:57:54 +0000952 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +1100953 return;
954 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +0000955 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +1100956 type = buffer_get_char(&iqueue);
957 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000958 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100959 process_init();
960 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000961 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +1100962 process_open();
963 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000964 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100965 process_close();
966 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000967 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +1100968 process_read();
969 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000970 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100971 process_write();
972 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000973 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100974 process_lstat();
975 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000976 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100977 process_fstat();
978 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000979 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100980 process_setstat();
981 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000982 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100983 process_fsetstat();
984 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000985 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100986 process_opendir();
987 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000988 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100989 process_readdir();
990 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000991 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100992 process_remove();
993 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000994 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100995 process_mkdir();
996 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000997 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100998 process_rmdir();
999 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001000 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001001 process_realpath();
1002 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001003 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001004 process_stat();
1005 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001006 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001007 process_rename();
1008 break;
Damien Miller058316f2001-03-08 10:08:49 +11001009 case SSH2_FXP_READLINK:
1010 process_readlink();
1011 break;
1012 case SSH2_FXP_SYMLINK:
1013 process_symlink();
1014 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001015 case SSH2_FXP_EXTENDED:
1016 process_extended();
1017 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001018 default:
1019 error("Unknown message %d", type);
1020 break;
1021 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001022 /* discard the remaining bytes from the current packet */
1023 if (buf_len < buffer_len(&iqueue))
1024 fatal("iqueue grows");
1025 consumed = buf_len - buffer_len(&iqueue);
1026 if (msg_len < consumed)
1027 fatal("msg_len %d < consumed %d", msg_len, consumed);
1028 if (msg_len > consumed)
1029 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001030}
1031
1032int
1033main(int ac, char **av)
1034{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001035 fd_set *rset, *wset;
Damien Miller7b28dc52000-09-05 13:34:53 +11001036 int in, out, max;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001037 ssize_t len, olen, set_size;
Damien Miller7b28dc52000-09-05 13:34:53 +11001038
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001039 /* XXX should use getopt */
1040
Damien Miller59d3d5b2003-08-22 09:34:41 +10001041 __progname = ssh_get_progname(av[0]);
Damien Miller7b28dc52000-09-05 13:34:53 +11001042 handle_init();
1043
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001044#ifdef DEBUG_SFTP_SERVER
Kevin Stevesef4eea92001-02-05 12:42:17 +00001045 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001046#endif
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001047
Damien Miller7b28dc52000-09-05 13:34:53 +11001048 in = dup(STDIN_FILENO);
1049 out = dup(STDOUT_FILENO);
1050
Damien Miller402b3312001-04-14 00:28:42 +10001051#ifdef HAVE_CYGWIN
1052 setmode(in, O_BINARY);
1053 setmode(out, O_BINARY);
1054#endif
1055
Damien Miller7b28dc52000-09-05 13:34:53 +11001056 max = 0;
1057 if (in > max)
1058 max = in;
1059 if (out > max)
1060 max = out;
1061
1062 buffer_init(&iqueue);
1063 buffer_init(&oqueue);
1064
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001065 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1066 rset = (fd_set *)xmalloc(set_size);
1067 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001068
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001069 for (;;) {
1070 memset(rset, 0, set_size);
1071 memset(wset, 0, set_size);
1072
1073 FD_SET(in, rset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001074 olen = buffer_len(&oqueue);
1075 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001076 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001077
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001078 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001079 if (errno == EINTR)
1080 continue;
1081 exit(2);
1082 }
1083
1084 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001085 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001086 char buf[4*4096];
1087 len = read(in, buf, sizeof buf);
1088 if (len == 0) {
1089 debug("read eof");
1090 exit(0);
1091 } else if (len < 0) {
1092 error("read error");
1093 exit(1);
1094 } else {
1095 buffer_append(&iqueue, buf, len);
1096 }
1097 }
1098 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001099 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001100 len = write(out, buffer_ptr(&oqueue), olen);
1101 if (len < 0) {
1102 error("write error");
1103 exit(1);
1104 } else {
1105 buffer_consume(&oqueue, len);
1106 }
1107 }
1108 /* process requests from client */
1109 process();
1110 }
1111}