blob: 6870e7732039ec94661aea29fd3f703da0a31f9c [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"
Damien Millereccb9de2005-06-17 12:59:34 +100017RCSID("$OpenBSD: sftp-server.c,v 1.48 2005/06/17 02:44:33 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"
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 +000034extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000035
Damien Miller7b28dc52000-09-05 13:34:53 +110036/* input and output queue */
37Buffer iqueue;
38Buffer oqueue;
39
Damien Miller058316f2001-03-08 10:08:49 +110040/* Version of client */
41int version;
42
Darren Tuckera6612d42003-06-28 12:39:03 +100043/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110044
Damien Miller7b28dc52000-09-05 13:34:53 +110045typedef struct Stat Stat;
46
Damien Miller33804262001-02-04 23:20:18 +110047struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110048 char *name;
49 char *long_name;
50 Attrib attrib;
51};
52
Ben Lindstrombba81212001-06-25 05:01:22 +000053static int
Damien Miller7b28dc52000-09-05 13:34:53 +110054errno_to_portable(int unixerrno)
55{
56 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000057
Damien Miller7b28dc52000-09-05 13:34:53 +110058 switch (unixerrno) {
59 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000060 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110061 break;
62 case ENOENT:
63 case ENOTDIR:
64 case EBADF:
65 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000066 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110067 break;
68 case EPERM:
69 case EACCES:
70 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000071 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110072 break;
73 case ENAMETOOLONG:
74 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000075 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +110076 break;
77 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000078 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +110079 break;
80 }
81 return ret;
82}
83
Ben Lindstrombba81212001-06-25 05:01:22 +000084static int
Damien Miller7b28dc52000-09-05 13:34:53 +110085flags_from_portable(int pflags)
86{
87 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000088
Ben Lindstrom36592512001-03-05 05:02:08 +000089 if ((pflags & SSH2_FXF_READ) &&
90 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +110091 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +000092 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +110093 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +000094 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +110095 flags = O_WRONLY;
96 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +000097 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +110098 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +000099 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100100 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000101 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100102 flags |= O_EXCL;
103 return flags;
104}
105
Ben Lindstrombba81212001-06-25 05:01:22 +0000106static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100107get_attrib(void)
108{
109 return decode_attrib(&iqueue);
110}
111
112/* handle handles */
113
114typedef struct Handle Handle;
115struct Handle {
116 int use;
117 DIR *dirp;
118 int fd;
119 char *name;
120};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000121
Damien Miller7b28dc52000-09-05 13:34:53 +1100122enum {
123 HANDLE_UNUSED,
124 HANDLE_DIR,
125 HANDLE_FILE
126};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000127
Damien Miller7b28dc52000-09-05 13:34:53 +1100128Handle handles[100];
129
Ben Lindstrombba81212001-06-25 05:01:22 +0000130static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100131handle_init(void)
132{
Damien Millereccb9de2005-06-17 12:59:34 +1000133 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000134
Damien Miller9f0f5c62001-12-21 14:45:46 +1100135 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
Damien Miller7b28dc52000-09-05 13:34:53 +1100136 handles[i].use = HANDLE_UNUSED;
137}
138
Ben Lindstrombba81212001-06-25 05:01:22 +0000139static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100140handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100141{
Damien Millereccb9de2005-06-17 12:59:34 +1000142 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000143
Damien Miller9f0f5c62001-12-21 14:45:46 +1100144 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100145 if (handles[i].use == HANDLE_UNUSED) {
146 handles[i].use = use;
147 handles[i].dirp = dirp;
148 handles[i].fd = fd;
Damien Miller00111382003-03-10 11:21:17 +1100149 handles[i].name = xstrdup(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100150 return i;
151 }
152 }
153 return -1;
154}
155
Ben Lindstrombba81212001-06-25 05:01:22 +0000156static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100157handle_is_ok(int i, int type)
158{
Damien Millereccb9de2005-06-17 12:59:34 +1000159 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000160 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100161}
162
Ben Lindstrombba81212001-06-25 05:01:22 +0000163static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100164handle_to_string(int handle, char **stringp, int *hlenp)
165{
Damien Miller7b28dc52000-09-05 13:34:53 +1100166 if (stringp == NULL || hlenp == NULL)
167 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000168 *stringp = xmalloc(sizeof(int32_t));
169 PUT_32BIT(*stringp, handle);
170 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100171 return 0;
172}
173
Ben Lindstrombba81212001-06-25 05:01:22 +0000174static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100175handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100176{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000177 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000178
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000179 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100180 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000181 val = GET_32BIT(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100182 if (handle_is_ok(val, HANDLE_FILE) ||
183 handle_is_ok(val, HANDLE_DIR))
184 return val;
185 return -1;
186}
187
Ben Lindstrombba81212001-06-25 05:01:22 +0000188static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100189handle_to_name(int handle)
190{
191 if (handle_is_ok(handle, HANDLE_DIR)||
192 handle_is_ok(handle, HANDLE_FILE))
193 return handles[handle].name;
194 return NULL;
195}
196
Ben Lindstrombba81212001-06-25 05:01:22 +0000197static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100198handle_to_dir(int handle)
199{
200 if (handle_is_ok(handle, HANDLE_DIR))
201 return handles[handle].dirp;
202 return NULL;
203}
204
Ben Lindstrombba81212001-06-25 05:01:22 +0000205static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100206handle_to_fd(int handle)
207{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000208 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100209 return handles[handle].fd;
210 return -1;
211}
212
Ben Lindstrombba81212001-06-25 05:01:22 +0000213static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100214handle_close(int handle)
215{
216 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000217
Damien Miller7b28dc52000-09-05 13:34:53 +1100218 if (handle_is_ok(handle, HANDLE_FILE)) {
219 ret = close(handles[handle].fd);
220 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100221 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100222 } else if (handle_is_ok(handle, HANDLE_DIR)) {
223 ret = closedir(handles[handle].dirp);
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 {
227 errno = ENOENT;
228 }
229 return ret;
230}
231
Ben Lindstrombba81212001-06-25 05:01:22 +0000232static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100233get_handle(void)
234{
235 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000236 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100237 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000238
Damien Miller7b28dc52000-09-05 13:34:53 +1100239 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000240 if (hlen < 256)
241 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100242 xfree(handle);
243 return val;
244}
245
246/* send replies */
247
Ben Lindstrombba81212001-06-25 05:01:22 +0000248static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100249send_msg(Buffer *m)
250{
251 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000252
Damien Miller7b28dc52000-09-05 13:34:53 +1100253 buffer_put_int(&oqueue, mlen);
254 buffer_append(&oqueue, buffer_ptr(m), mlen);
255 buffer_consume(m, mlen);
256}
257
Ben Lindstrombba81212001-06-25 05:01:22 +0000258static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000259send_status(u_int32_t id, u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100260{
261 Buffer msg;
Damien Miller058316f2001-03-08 10:08:49 +1100262 const char *status_messages[] = {
263 "Success", /* SSH_FX_OK */
264 "End of file", /* SSH_FX_EOF */
265 "No such file", /* SSH_FX_NO_SUCH_FILE */
266 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
267 "Failure", /* SSH_FX_FAILURE */
268 "Bad message", /* SSH_FX_BAD_MESSAGE */
269 "No connection", /* SSH_FX_NO_CONNECTION */
270 "Connection lost", /* SSH_FX_CONNECTION_LOST */
271 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
272 "Unknown error" /* Others */
273 };
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000274
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000275 TRACE("sent status id %u error %u", id, status);
Damien Miller7b28dc52000-09-05 13:34:53 +1100276 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000277 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100278 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000279 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100280 if (version >= 3) {
Ben Lindstroma3700052001-04-05 23:26:32 +0000281 buffer_put_cstring(&msg,
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000282 status_messages[MIN(status,SSH2_FX_MAX)]);
Damien Miller058316f2001-03-08 10:08:49 +1100283 buffer_put_cstring(&msg, "");
284 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100285 send_msg(&msg);
286 buffer_free(&msg);
287}
Ben Lindstrombba81212001-06-25 05:01:22 +0000288static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100289send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100290{
291 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000292
Damien Miller7b28dc52000-09-05 13:34:53 +1100293 buffer_init(&msg);
294 buffer_put_char(&msg, type);
295 buffer_put_int(&msg, id);
296 buffer_put_string(&msg, data, dlen);
297 send_msg(&msg);
298 buffer_free(&msg);
299}
300
Ben Lindstrombba81212001-06-25 05:01:22 +0000301static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100302send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100303{
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000304 TRACE("sent data id %u len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000305 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100306}
307
Ben Lindstrombba81212001-06-25 05:01:22 +0000308static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100309send_handle(u_int32_t id, int handle)
310{
311 char *string;
312 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000313
Damien Miller7b28dc52000-09-05 13:34:53 +1100314 handle_to_string(handle, &string, &hlen);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000315 TRACE("sent handle id %u handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000316 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100317 xfree(string);
318}
319
Ben Lindstrombba81212001-06-25 05:01:22 +0000320static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100321send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100322{
323 Buffer msg;
324 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000325
Damien Miller7b28dc52000-09-05 13:34:53 +1100326 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000327 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100328 buffer_put_int(&msg, id);
329 buffer_put_int(&msg, count);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000330 TRACE("sent names id %u count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100331 for (i = 0; i < count; i++) {
332 buffer_put_cstring(&msg, stats[i].name);
333 buffer_put_cstring(&msg, stats[i].long_name);
334 encode_attrib(&msg, &stats[i].attrib);
335 }
336 send_msg(&msg);
337 buffer_free(&msg);
338}
339
Ben Lindstrombba81212001-06-25 05:01:22 +0000340static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100341send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100342{
343 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000344
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000345 TRACE("sent attrib id %u have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100346 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000347 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100348 buffer_put_int(&msg, id);
349 encode_attrib(&msg, a);
350 send_msg(&msg);
351 buffer_free(&msg);
352}
353
354/* parse incoming */
355
Ben Lindstrombba81212001-06-25 05:01:22 +0000356static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100357process_init(void)
358{
359 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100360
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000361 version = get_int();
Damien Miller7b28dc52000-09-05 13:34:53 +1100362 TRACE("client version %d", version);
363 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000364 buffer_put_char(&msg, SSH2_FXP_VERSION);
365 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100366 send_msg(&msg);
367 buffer_free(&msg);
368}
369
Ben Lindstrombba81212001-06-25 05:01:22 +0000370static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100371process_open(void)
372{
373 u_int32_t id, pflags;
374 Attrib *a;
375 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000376 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100377
378 id = get_int();
379 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000380 pflags = get_int(); /* portable flags */
Damien Miller7b28dc52000-09-05 13:34:53 +1100381 a = get_attrib();
382 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000383 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000384 TRACE("open id %u name %s flags %d mode 0%o", id, name, pflags, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100385 fd = open(name, flags, mode);
386 if (fd < 0) {
387 status = errno_to_portable(errno);
388 } else {
Damien Miller00111382003-03-10 11:21:17 +1100389 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100390 if (handle < 0) {
391 close(fd);
392 } else {
393 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000394 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100395 }
396 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000397 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100398 send_status(id, status);
399 xfree(name);
400}
401
Ben Lindstrombba81212001-06-25 05:01:22 +0000402static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100403process_close(void)
404{
405 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000406 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100407
408 id = get_int();
409 handle = get_handle();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000410 TRACE("close id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100411 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000412 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100413 send_status(id, status);
414}
415
Ben Lindstrombba81212001-06-25 05:01:22 +0000416static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100417process_read(void)
418{
419 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000420 u_int32_t id, len;
421 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100422 u_int64_t off;
423
424 id = get_int();
425 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000426 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100427 len = get_int();
428
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000429 TRACE("read id %u handle %d off %llu len %d", id, handle,
Ben Lindstrom416d8742001-02-25 02:02:43 +0000430 (u_int64_t)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100431 if (len > sizeof buf) {
432 len = sizeof buf;
Damien Miller996acd22003-04-09 20:59:48 +1000433 logit("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100434 }
435 fd = handle_to_fd(handle);
436 if (fd >= 0) {
437 if (lseek(fd, off, SEEK_SET) < 0) {
438 error("process_read: seek failed");
439 status = errno_to_portable(errno);
440 } else {
441 ret = read(fd, buf, len);
442 if (ret < 0) {
443 status = errno_to_portable(errno);
444 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000445 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100446 } else {
447 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000448 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100449 }
450 }
451 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000452 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100453 send_status(id, status);
454}
455
Ben Lindstrombba81212001-06-25 05:01:22 +0000456static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100457process_write(void)
458{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000459 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100460 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100461 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000462 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100463 char *data;
464
465 id = get_int();
466 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000467 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100468 data = get_string(&len);
469
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000470 TRACE("write id %u handle %d off %llu len %d", id, handle,
Ben Lindstrom416d8742001-02-25 02:02:43 +0000471 (u_int64_t)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100472 fd = handle_to_fd(handle);
473 if (fd >= 0) {
474 if (lseek(fd, off, SEEK_SET) < 0) {
475 status = errno_to_portable(errno);
476 error("process_write: seek failed");
477 } else {
478/* XXX ATOMICIO ? */
479 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000480 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100481 error("process_write: write failed");
482 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000483 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000484 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100485 } else {
Damien Miller996acd22003-04-09 20:59:48 +1000486 logit("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100487 }
488 }
489 }
490 send_status(id, status);
491 xfree(data);
492}
493
Ben Lindstrombba81212001-06-25 05:01:22 +0000494static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100495process_do_stat(int do_lstat)
496{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000497 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100498 struct stat st;
499 u_int32_t id;
500 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000501 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100502
503 id = get_int();
504 name = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000505 TRACE("%sstat id %u name %s", do_lstat ? "l" : "", id, name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100506 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
507 if (ret < 0) {
508 status = errno_to_portable(errno);
509 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000510 stat_to_attrib(&st, &a);
511 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000512 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100513 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000514 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100515 send_status(id, status);
516 xfree(name);
517}
518
Ben Lindstrombba81212001-06-25 05:01:22 +0000519static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100520process_stat(void)
521{
522 process_do_stat(0);
523}
524
Ben Lindstrombba81212001-06-25 05:01:22 +0000525static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100526process_lstat(void)
527{
528 process_do_stat(1);
529}
530
Ben Lindstrombba81212001-06-25 05:01:22 +0000531static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100532process_fstat(void)
533{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000534 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100535 struct stat st;
536 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000537 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100538
539 id = get_int();
540 handle = get_handle();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000541 TRACE("fstat id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100542 fd = handle_to_fd(handle);
543 if (fd >= 0) {
544 ret = fstat(fd, &st);
545 if (ret < 0) {
546 status = errno_to_portable(errno);
547 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000548 stat_to_attrib(&st, &a);
549 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000550 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100551 }
552 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000553 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100554 send_status(id, status);
555}
556
Ben Lindstrombba81212001-06-25 05:01:22 +0000557static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100558attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100559{
560 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000561
Damien Miller7b28dc52000-09-05 13:34:53 +1100562 tv[0].tv_sec = a->atime;
563 tv[0].tv_usec = 0;
564 tv[1].tv_sec = a->mtime;
565 tv[1].tv_usec = 0;
566 return tv;
567}
568
Ben Lindstrombba81212001-06-25 05:01:22 +0000569static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100570process_setstat(void)
571{
572 Attrib *a;
573 u_int32_t id;
574 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000575 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100576
577 id = get_int();
578 name = get_string(NULL);
579 a = get_attrib();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000580 TRACE("setstat id %u name %s", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100581 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
582 ret = truncate(name, a->size);
583 if (ret == -1)
584 status = errno_to_portable(errno);
585 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000586 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100587 ret = chmod(name, a->perm & 0777);
588 if (ret == -1)
589 status = errno_to_portable(errno);
590 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000591 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100592 ret = utimes(name, attrib_to_tv(a));
593 if (ret == -1)
594 status = errno_to_portable(errno);
595 }
Kevin Steves8e743932001-02-05 13:24:35 +0000596 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
597 ret = chown(name, a->uid, a->gid);
598 if (ret == -1)
599 status = errno_to_portable(errno);
600 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100601 send_status(id, status);
602 xfree(name);
603}
604
Ben Lindstrombba81212001-06-25 05:01:22 +0000605static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100606process_fsetstat(void)
607{
608 Attrib *a;
609 u_int32_t id;
610 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000611 int status = SSH2_FX_OK;
Damien Millere4340be2000-09-16 13:29:08 +1100612 char *name;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000613
Damien Miller7b28dc52000-09-05 13:34:53 +1100614 id = get_int();
615 handle = get_handle();
616 a = get_attrib();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000617 TRACE("fsetstat id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100618 fd = handle_to_fd(handle);
619 name = handle_to_name(handle);
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000620 if (fd < 0 || name == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000621 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100622 } else {
Damien Miller00c92172002-02-13 14:05:00 +1100623 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
624 ret = ftruncate(fd, a->size);
625 if (ret == -1)
626 status = errno_to_portable(errno);
627 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000628 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000629#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100630 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000631#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000632 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000633#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100634 if (ret == -1)
635 status = errno_to_portable(errno);
636 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000637 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100638#ifdef HAVE_FUTIMES
639 ret = futimes(fd, attrib_to_tv(a));
640#else
641 ret = utimes(name, attrib_to_tv(a));
642#endif
643 if (ret == -1)
644 status = errno_to_portable(errno);
645 }
Kevin Steves8e743932001-02-05 13:24:35 +0000646 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000647#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000648 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000649#else
650 ret = chown(name, a->uid, a->gid);
651#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000652 if (ret == -1)
653 status = errno_to_portable(errno);
654 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100655 }
656 send_status(id, status);
657}
658
Ben Lindstrombba81212001-06-25 05:01:22 +0000659static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100660process_opendir(void)
661{
662 DIR *dirp = NULL;
663 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000664 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100665 u_int32_t id;
666
667 id = get_int();
668 path = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000669 TRACE("opendir id %u path %s", id, path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000670 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100671 if (dirp == NULL) {
672 status = errno_to_portable(errno);
673 } else {
Damien Miller00111382003-03-10 11:21:17 +1100674 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100675 if (handle < 0) {
676 closedir(dirp);
677 } else {
678 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000679 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100680 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000681
Damien Miller7b28dc52000-09-05 13:34:53 +1100682 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000683 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100684 send_status(id, status);
685 xfree(path);
686}
687
Ben Lindstrombba81212001-06-25 05:01:22 +0000688static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100689process_readdir(void)
690{
691 DIR *dirp;
692 struct dirent *dp;
693 char *path;
694 int handle;
695 u_int32_t id;
696
697 id = get_int();
698 handle = get_handle();
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000699 TRACE("readdir id %u handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100700 dirp = handle_to_dir(handle);
701 path = handle_to_name(handle);
702 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000703 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100704 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100705 struct stat st;
706 char pathname[1024];
707 Stat *stats;
708 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000709
Damien Miller7b28dc52000-09-05 13:34:53 +1100710 stats = xmalloc(nstats * sizeof(Stat));
711 while ((dp = readdir(dirp)) != NULL) {
712 if (count >= nstats) {
713 nstats *= 2;
714 stats = xrealloc(stats, nstats * sizeof(Stat));
715 }
716/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000717 snprintf(pathname, sizeof pathname, "%s%s%s", path,
718 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100719 if (lstat(pathname, &st) < 0)
720 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000721 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100722 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000723 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100724 count++;
725 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000726 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100727 if (count == 100)
728 break;
729 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000730 if (count > 0) {
731 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100732 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000733 xfree(stats[i].name);
734 xfree(stats[i].long_name);
735 }
736 } else {
737 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100738 }
739 xfree(stats);
740 }
741}
742
Ben Lindstrombba81212001-06-25 05:01:22 +0000743static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100744process_remove(void)
745{
746 char *name;
747 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000748 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100749 int ret;
750
751 id = get_int();
752 name = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000753 TRACE("remove id %u name %s", id, name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000754 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000755 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100756 send_status(id, status);
757 xfree(name);
758}
759
Ben Lindstrombba81212001-06-25 05:01:22 +0000760static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100761process_mkdir(void)
762{
763 Attrib *a;
764 u_int32_t id;
765 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000766 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100767
768 id = get_int();
769 name = get_string(NULL);
770 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000771 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
772 a->perm & 0777 : 0777;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000773 TRACE("mkdir id %u name %s mode 0%o", id, name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100774 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000775 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100776 send_status(id, status);
777 xfree(name);
778}
779
Ben Lindstrombba81212001-06-25 05:01:22 +0000780static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100781process_rmdir(void)
782{
783 u_int32_t id;
784 char *name;
785 int ret, status;
786
787 id = get_int();
788 name = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000789 TRACE("rmdir id %u name %s", id, name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100790 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000791 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100792 send_status(id, status);
793 xfree(name);
794}
795
Ben Lindstrombba81212001-06-25 05:01:22 +0000796static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100797process_realpath(void)
798{
799 char resolvedname[MAXPATHLEN];
800 u_int32_t id;
801 char *path;
802
803 id = get_int();
804 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000805 if (path[0] == '\0') {
806 xfree(path);
807 path = xstrdup(".");
808 }
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000809 TRACE("realpath id %u path %s", id, path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100810 if (realpath(path, resolvedname) == NULL) {
811 send_status(id, errno_to_portable(errno));
812 } else {
813 Stat s;
814 attrib_clear(&s.attrib);
815 s.name = s.long_name = resolvedname;
816 send_names(id, 1, &s);
817 }
818 xfree(path);
819}
820
Ben Lindstrombba81212001-06-25 05:01:22 +0000821static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100822process_rename(void)
823{
824 u_int32_t id;
825 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100826 int status;
Damien Millerb3207e82003-03-26 16:01:11 +1100827 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +1100828
829 id = get_int();
830 oldpath = get_string(NULL);
831 newpath = get_string(NULL);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000832 TRACE("rename id %u old %s new %s", id, oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +1100833 status = SSH2_FX_FAILURE;
834 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +1100835 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +1100836 else if (S_ISREG(sb.st_mode)) {
837 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000838 if (link(oldpath, newpath) == -1) {
Darren Tuckere59b5082004-06-28 16:01:19 +1000839 if (errno == EOPNOTSUPP
840#ifdef LINK_OPNOTSUPP_ERRNO
841 || errno == LINK_OPNOTSUPP_ERRNO
842#endif
843 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000844 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}