blob: 0291da2063de61dc4981cdb6aac29d9de1b1fb32 [file] [log] [blame]
Damien Miller7b28dc52000-09-05 13:34:53 +11001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Miller7b28dc52000-09-05 13:34:53 +110012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24#include "includes.h"
Ben Lindstromafed4d92001-02-10 22:57:44 +000025RCSID("$OpenBSD: sftp-server.c,v 1.19 2001/02/07 18:01:18 itojun Exp $");
Damien Miller7b28dc52000-09-05 13:34:53 +110026
Damien Miller7b28dc52000-09-05 13:34:53 +110027#include "buffer.h"
28#include "bufaux.h"
29#include "getput.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include "log.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110031#include "xmalloc.h"
32
Ben Lindstrom2f959b42001-01-11 06:20:23 +000033#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110034#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110035
36/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000037#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110038#define get_int() buffer_get_int(&iqueue);
39#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Ben Lindstrom2f959b42001-01-11 06:20:23 +000040#define TRACE debug
Damien Miller7b28dc52000-09-05 13:34:53 +110041
Ben Lindstrom49a79c02000-11-17 03:47:20 +000042#ifdef HAVE___PROGNAME
43extern char *__progname;
44#else
45char *__progname;
46#endif
47
Damien Miller7b28dc52000-09-05 13:34:53 +110048/* input and output queue */
49Buffer iqueue;
50Buffer oqueue;
51
52/* portable attibutes, etc. */
53
Damien Miller7b28dc52000-09-05 13:34:53 +110054typedef struct Stat Stat;
55
Damien Miller33804262001-02-04 23:20:18 +110056struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110057 char *name;
58 char *long_name;
59 Attrib attrib;
60};
61
62int
63errno_to_portable(int unixerrno)
64{
65 int ret = 0;
66 switch (unixerrno) {
67 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000068 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110069 break;
70 case ENOENT:
71 case ENOTDIR:
72 case EBADF:
73 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000074 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110075 break;
76 case EPERM:
77 case EACCES:
78 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000079 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110080 break;
81 case ENAMETOOLONG:
82 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000083 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +110084 break;
85 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000086 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +110087 break;
88 }
89 return ret;
90}
91
92int
93flags_from_portable(int pflags)
94{
95 int flags = 0;
Ben Lindstrom2f959b42001-01-11 06:20:23 +000096 if (pflags & SSH2_FXF_READ &&
97 pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +110098 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +000099 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100100 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000101 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100102 flags = O_WRONLY;
103 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000104 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100105 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000106 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100107 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000108 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100109 flags |= O_EXCL;
110 return flags;
111}
112
Damien Miller7b28dc52000-09-05 13:34:53 +1100113Attrib *
114get_attrib(void)
115{
116 return decode_attrib(&iqueue);
117}
118
119/* handle handles */
120
121typedef struct Handle Handle;
122struct Handle {
123 int use;
124 DIR *dirp;
125 int fd;
126 char *name;
127};
128enum {
129 HANDLE_UNUSED,
130 HANDLE_DIR,
131 HANDLE_FILE
132};
133Handle handles[100];
134
135void
136handle_init(void)
137{
138 int i;
139 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++)
140 handles[i].use = HANDLE_UNUSED;
141}
142
143int
144handle_new(int use, char *name, int fd, DIR *dirp)
145{
146 int i;
147 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
148 if (handles[i].use == HANDLE_UNUSED) {
149 handles[i].use = use;
150 handles[i].dirp = dirp;
151 handles[i].fd = fd;
152 handles[i].name = name;
153 return i;
154 }
155 }
156 return -1;
157}
158
159int
160handle_is_ok(int i, int type)
161{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000162 return i >= 0 && i < sizeof(handles)/sizeof(Handle) &&
163 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100164}
165
166int
167handle_to_string(int handle, char **stringp, int *hlenp)
168{
Damien Miller7b28dc52000-09-05 13:34:53 +1100169 if (stringp == NULL || hlenp == NULL)
170 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000171 *stringp = xmalloc(sizeof(int32_t));
172 PUT_32BIT(*stringp, handle);
173 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100174 return 0;
175}
176
177int
Damien Millere4340be2000-09-16 13:29:08 +1100178handle_from_string(char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100179{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000180 int val;
181 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100182 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000183 val = GET_32BIT(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100184 if (handle_is_ok(val, HANDLE_FILE) ||
185 handle_is_ok(val, HANDLE_DIR))
186 return val;
187 return -1;
188}
189
190char *
191handle_to_name(int handle)
192{
193 if (handle_is_ok(handle, HANDLE_DIR)||
194 handle_is_ok(handle, HANDLE_FILE))
195 return handles[handle].name;
196 return NULL;
197}
198
199DIR *
200handle_to_dir(int handle)
201{
202 if (handle_is_ok(handle, HANDLE_DIR))
203 return handles[handle].dirp;
204 return NULL;
205}
206
207int
208handle_to_fd(int handle)
209{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000210 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100211 return handles[handle].fd;
212 return -1;
213}
214
215int
216handle_close(int handle)
217{
218 int ret = -1;
219 if (handle_is_ok(handle, HANDLE_FILE)) {
220 ret = close(handles[handle].fd);
221 handles[handle].use = HANDLE_UNUSED;
222 } else if (handle_is_ok(handle, HANDLE_DIR)) {
223 ret = closedir(handles[handle].dirp);
224 handles[handle].use = HANDLE_UNUSED;
225 } else {
226 errno = ENOENT;
227 }
228 return ret;
229}
230
231int
232get_handle(void)
233{
234 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000235 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100236 u_int hlen;
Damien Miller7b28dc52000-09-05 13:34:53 +1100237 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000238 if (hlen < 256)
239 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100240 xfree(handle);
241 return val;
242}
243
244/* send replies */
245
246void
247send_msg(Buffer *m)
248{
249 int mlen = buffer_len(m);
250 buffer_put_int(&oqueue, mlen);
251 buffer_append(&oqueue, buffer_ptr(m), mlen);
252 buffer_consume(m, mlen);
253}
254
255void
256send_status(u_int32_t id, u_int32_t error)
257{
258 Buffer msg;
259 TRACE("sent status id %d error %d", id, error);
260 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000261 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100262 buffer_put_int(&msg, id);
263 buffer_put_int(&msg, error);
264 send_msg(&msg);
265 buffer_free(&msg);
266}
267void
268send_data_or_handle(char type, u_int32_t id, char *data, int dlen)
269{
270 Buffer msg;
271 buffer_init(&msg);
272 buffer_put_char(&msg, type);
273 buffer_put_int(&msg, id);
274 buffer_put_string(&msg, data, dlen);
275 send_msg(&msg);
276 buffer_free(&msg);
277}
278
279void
280send_data(u_int32_t id, char *data, int dlen)
281{
282 TRACE("sent data id %d len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000283 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100284}
285
286void
287send_handle(u_int32_t id, int handle)
288{
289 char *string;
290 int hlen;
291 handle_to_string(handle, &string, &hlen);
292 TRACE("sent handle id %d handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000293 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100294 xfree(string);
295}
296
297void
298send_names(u_int32_t id, int count, Stat *stats)
299{
300 Buffer msg;
301 int i;
302 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000303 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100304 buffer_put_int(&msg, id);
305 buffer_put_int(&msg, count);
306 TRACE("sent names id %d count %d", id, count);
307 for (i = 0; i < count; i++) {
308 buffer_put_cstring(&msg, stats[i].name);
309 buffer_put_cstring(&msg, stats[i].long_name);
310 encode_attrib(&msg, &stats[i].attrib);
311 }
312 send_msg(&msg);
313 buffer_free(&msg);
314}
315
316void
317send_attrib(u_int32_t id, Attrib *a)
318{
319 Buffer msg;
320 TRACE("sent attrib id %d have 0x%x", id, a->flags);
321 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000322 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100323 buffer_put_int(&msg, id);
324 encode_attrib(&msg, a);
325 send_msg(&msg);
326 buffer_free(&msg);
327}
328
329/* parse incoming */
330
331void
332process_init(void)
333{
334 Buffer msg;
335 int version = buffer_get_int(&iqueue);
336
337 TRACE("client version %d", version);
338 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000339 buffer_put_char(&msg, SSH2_FXP_VERSION);
340 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100341 send_msg(&msg);
342 buffer_free(&msg);
343}
344
345void
346process_open(void)
347{
348 u_int32_t id, pflags;
349 Attrib *a;
350 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000351 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100352
353 id = get_int();
354 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000355 pflags = get_int(); /* portable flags */
Damien Miller7b28dc52000-09-05 13:34:53 +1100356 a = get_attrib();
357 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000358 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Miller7b28dc52000-09-05 13:34:53 +1100359 TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode);
360 fd = open(name, flags, mode);
361 if (fd < 0) {
362 status = errno_to_portable(errno);
363 } else {
364 handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL);
365 if (handle < 0) {
366 close(fd);
367 } else {
368 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000369 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100370 }
371 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000372 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100373 send_status(id, status);
374 xfree(name);
375}
376
377void
378process_close(void)
379{
380 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000381 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100382
383 id = get_int();
384 handle = get_handle();
385 TRACE("close id %d handle %d", id, handle);
386 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000387 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100388 send_status(id, status);
389}
390
391void
392process_read(void)
393{
394 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000395 u_int32_t id, len;
396 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100397 u_int64_t off;
398
399 id = get_int();
400 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000401 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100402 len = get_int();
403
Kevin Stevesec1c1402001-02-05 15:39:22 +0000404 TRACE("read id %d handle %d off %llu len %d", id, handle,
405 (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100406 if (len > sizeof buf) {
407 len = sizeof buf;
408 log("read change len %d", len);
409 }
410 fd = handle_to_fd(handle);
411 if (fd >= 0) {
412 if (lseek(fd, off, SEEK_SET) < 0) {
413 error("process_read: seek failed");
414 status = errno_to_portable(errno);
415 } else {
416 ret = read(fd, buf, len);
417 if (ret < 0) {
418 status = errno_to_portable(errno);
419 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000420 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100421 } else {
422 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000423 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100424 }
425 }
426 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000427 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100428 send_status(id, status);
429}
430
431void
432process_write(void)
433{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000434 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100435 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100436 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000437 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100438 char *data;
439
440 id = get_int();
441 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000442 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100443 data = get_string(&len);
444
Kevin Stevesec1c1402001-02-05 15:39:22 +0000445 TRACE("write id %d handle %d off %llu len %d", id, handle,
446 (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100447 fd = handle_to_fd(handle);
448 if (fd >= 0) {
449 if (lseek(fd, off, SEEK_SET) < 0) {
450 status = errno_to_portable(errno);
451 error("process_write: seek failed");
452 } else {
453/* XXX ATOMICIO ? */
454 ret = write(fd, data, len);
455 if (ret == -1) {
456 error("process_write: write failed");
457 status = errno_to_portable(errno);
458 } else if (ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000459 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100460 } else {
461 log("nothing at all written");
462 }
463 }
464 }
465 send_status(id, status);
466 xfree(data);
467}
468
469void
470process_do_stat(int do_lstat)
471{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000472 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100473 struct stat st;
474 u_int32_t id;
475 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000476 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100477
478 id = get_int();
479 name = get_string(NULL);
480 TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name);
481 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
482 if (ret < 0) {
483 status = errno_to_portable(errno);
484 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000485 stat_to_attrib(&st, &a);
486 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000487 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100488 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000489 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100490 send_status(id, status);
491 xfree(name);
492}
493
494void
495process_stat(void)
496{
497 process_do_stat(0);
498}
499
500void
501process_lstat(void)
502{
503 process_do_stat(1);
504}
505
506void
507process_fstat(void)
508{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000509 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100510 struct stat st;
511 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000512 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100513
514 id = get_int();
515 handle = get_handle();
516 TRACE("fstat id %d handle %d", id, handle);
517 fd = handle_to_fd(handle);
518 if (fd >= 0) {
519 ret = fstat(fd, &st);
520 if (ret < 0) {
521 status = errno_to_portable(errno);
522 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000523 stat_to_attrib(&st, &a);
524 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000525 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100526 }
527 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000528 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100529 send_status(id, status);
530}
531
532struct timeval *
533attrib_to_tv(Attrib *a)
534{
535 static struct timeval tv[2];
536 tv[0].tv_sec = a->atime;
537 tv[0].tv_usec = 0;
538 tv[1].tv_sec = a->mtime;
539 tv[1].tv_usec = 0;
540 return tv;
541}
542
543void
544process_setstat(void)
545{
546 Attrib *a;
547 u_int32_t id;
548 char *name;
549 int ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000550 int status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100551
552 id = get_int();
553 name = get_string(NULL);
554 a = get_attrib();
555 TRACE("setstat id %d name %s", id, name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000556 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100557 ret = chmod(name, a->perm & 0777);
558 if (ret == -1)
559 status = errno_to_portable(errno);
560 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000561 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100562 ret = utimes(name, attrib_to_tv(a));
563 if (ret == -1)
564 status = errno_to_portable(errno);
565 }
Kevin Steves8e743932001-02-05 13:24:35 +0000566 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
567 ret = chown(name, a->uid, a->gid);
568 if (ret == -1)
569 status = errno_to_portable(errno);
570 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100571 send_status(id, status);
572 xfree(name);
573}
574
575void
576process_fsetstat(void)
577{
578 Attrib *a;
579 u_int32_t id;
580 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000581 int status = SSH2_FX_OK;
Damien Millere4340be2000-09-16 13:29:08 +1100582 char *name;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000583
Damien Miller7b28dc52000-09-05 13:34:53 +1100584 id = get_int();
585 handle = get_handle();
586 a = get_attrib();
587 TRACE("fsetstat id %d handle %d", id, handle);
588 fd = handle_to_fd(handle);
589 name = handle_to_name(handle);
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000590 if (fd < 0 || name == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000591 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100592 } else {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000593 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000594#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100595 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000596#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000597 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000598#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100599 if (ret == -1)
600 status = errno_to_portable(errno);
601 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000602 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100603#ifdef HAVE_FUTIMES
604 ret = futimes(fd, attrib_to_tv(a));
605#else
606 ret = utimes(name, attrib_to_tv(a));
607#endif
608 if (ret == -1)
609 status = errno_to_portable(errno);
610 }
Kevin Steves8e743932001-02-05 13:24:35 +0000611 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000612#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000613 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000614#else
615 ret = chown(name, a->uid, a->gid);
616#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000617 if (ret == -1)
618 status = errno_to_portable(errno);
619 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100620 }
621 send_status(id, status);
622}
623
624void
625process_opendir(void)
626{
627 DIR *dirp = NULL;
628 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000629 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100630 u_int32_t id;
631
632 id = get_int();
633 path = get_string(NULL);
634 TRACE("opendir id %d path %s", id, path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000635 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100636 if (dirp == NULL) {
637 status = errno_to_portable(errno);
638 } else {
639 handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
640 if (handle < 0) {
641 closedir(dirp);
642 } else {
643 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000644 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100645 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000646
Damien Miller7b28dc52000-09-05 13:34:53 +1100647 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000648 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100649 send_status(id, status);
650 xfree(path);
651}
652
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000653/*
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000654 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000655 */
Damien Miller7b28dc52000-09-05 13:34:53 +1100656char *
657ls_file(char *name, struct stat *st)
658{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000659 int sz = 0;
660 struct passwd *pw;
661 struct group *gr;
662 struct tm *ltime = localtime(&st->st_mtime);
663 char *user, *group;
664 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
665
666 strmode(st->st_mode, mode);
667 if ((pw = getpwuid(st->st_uid)) != NULL) {
668 user = pw->pw_name;
669 } else {
670 snprintf(ubuf, sizeof ubuf, "%d", st->st_uid);
671 user = ubuf;
672 }
673 if ((gr = getgrgid(st->st_gid)) != NULL) {
674 group = gr->gr_name;
675 } else {
676 snprintf(gbuf, sizeof gbuf, "%d", st->st_gid);
677 group = gbuf;
678 }
679 if (ltime != NULL) {
680 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
681 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
682 else
683 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
684 }
685 if (sz == 0)
686 tbuf[0] = '\0';
Ben Lindstrom7e9aff52001-02-10 23:00:22 +0000687 snprintf(buf, sizeof buf, "%s %3d %-8.8s %-8.8s %8llu %s %s", mode,
Ben Lindstrome8e9fd52001-02-10 23:02:23 +0000688 st->st_nlink, user, group, (unsigned long long)st->st_size, tbuf, name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100689 return xstrdup(buf);
690}
691
692void
693process_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();
703 TRACE("readdir id %d handle %d", id, handle);
704 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;
713 stats = xmalloc(nstats * sizeof(Stat));
714 while ((dp = readdir(dirp)) != NULL) {
715 if (count >= nstats) {
716 nstats *= 2;
717 stats = xrealloc(stats, nstats * sizeof(Stat));
718 }
719/* XXX OVERFLOW ? */
720 snprintf(pathname, sizeof pathname,
721 "%s/%s", path, dp->d_name);
722 if (lstat(pathname, &st) < 0)
723 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000724 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100725 stats[count].name = xstrdup(dp->d_name);
726 stats[count].long_name = ls_file(dp->d_name, &st);
727 count++;
728 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000729 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100730 if (count == 100)
731 break;
732 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000733 if (count > 0) {
734 send_names(id, count, stats);
735 for(i = 0; i < count; i++) {
736 xfree(stats[i].name);
737 xfree(stats[i].long_name);
738 }
739 } else {
740 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100741 }
742 xfree(stats);
743 }
744}
745
746void
747process_remove(void)
748{
749 char *name;
750 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000751 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100752 int ret;
753
754 id = get_int();
755 name = get_string(NULL);
756 TRACE("remove id %d name %s", id, name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000757 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000758 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100759 send_status(id, status);
760 xfree(name);
761}
762
763void
764process_mkdir(void)
765{
766 Attrib *a;
767 u_int32_t id;
768 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000769 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100770
771 id = get_int();
772 name = get_string(NULL);
773 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000774 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
775 a->perm & 0777 : 0777;
Damien Miller7b28dc52000-09-05 13:34:53 +1100776 TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
777 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000778 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100779 send_status(id, status);
780 xfree(name);
781}
782
783void
784process_rmdir(void)
785{
786 u_int32_t id;
787 char *name;
788 int ret, status;
789
790 id = get_int();
791 name = get_string(NULL);
792 TRACE("rmdir id %d name %s", id, name);
793 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000794 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100795 send_status(id, status);
796 xfree(name);
797}
798
799void
800process_realpath(void)
801{
802 char resolvedname[MAXPATHLEN];
803 u_int32_t id;
804 char *path;
805
806 id = get_int();
807 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000808 if (path[0] == '\0') {
809 xfree(path);
810 path = xstrdup(".");
811 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100812 TRACE("realpath id %d path %s", id, path);
813 if (realpath(path, resolvedname) == NULL) {
814 send_status(id, errno_to_portable(errno));
815 } else {
816 Stat s;
817 attrib_clear(&s.attrib);
818 s.name = s.long_name = resolvedname;
819 send_names(id, 1, &s);
820 }
821 xfree(path);
822}
823
824void
825process_rename(void)
826{
827 u_int32_t id;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000828 struct stat st;
Damien Miller7b28dc52000-09-05 13:34:53 +1100829 char *oldpath, *newpath;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000830 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100831
832 id = get_int();
833 oldpath = get_string(NULL);
834 newpath = get_string(NULL);
835 TRACE("rename id %d old %s new %s", id, oldpath, newpath);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000836 /* fail if 'newpath' exists */
837 if (stat(newpath, &st) == -1) {
838 ret = rename(oldpath, newpath);
839 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
840 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100841 send_status(id, status);
842 xfree(oldpath);
843 xfree(newpath);
844}
845
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000846void
847process_extended(void)
848{
849 u_int32_t id;
850 char *request;
851
852 id = get_int();
853 request = get_string(NULL);
854 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
855 xfree(request);
856}
Damien Miller7b28dc52000-09-05 13:34:53 +1100857
858/* stolen from ssh-agent */
859
860void
861process(void)
862{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000863 u_int msg_len;
864 u_int type;
865 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +1100866
867 if (buffer_len(&iqueue) < 5)
868 return; /* Incomplete message. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000869 cp = (u_char *) buffer_ptr(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +1100870 msg_len = GET_32BIT(cp);
871 if (msg_len > 256 * 1024) {
872 error("bad message ");
873 exit(11);
874 }
875 if (buffer_len(&iqueue) < msg_len + 4)
876 return;
877 buffer_consume(&iqueue, 4);
878 type = buffer_get_char(&iqueue);
879 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000880 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100881 process_init();
882 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000883 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +1100884 process_open();
885 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000886 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100887 process_close();
888 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000889 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +1100890 process_read();
891 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000892 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100893 process_write();
894 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000895 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100896 process_lstat();
897 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000898 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100899 process_fstat();
900 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000901 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100902 process_setstat();
903 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000904 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100905 process_fsetstat();
906 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000907 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100908 process_opendir();
909 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000910 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100911 process_readdir();
912 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000913 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100914 process_remove();
915 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000916 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100917 process_mkdir();
918 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000919 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100920 process_rmdir();
921 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000922 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +1100923 process_realpath();
924 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000925 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100926 process_stat();
927 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000928 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +1100929 process_rename();
930 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000931 case SSH2_FXP_EXTENDED:
932 process_extended();
933 break;
Damien Miller7b28dc52000-09-05 13:34:53 +1100934 default:
935 error("Unknown message %d", type);
936 break;
937 }
938}
939
940int
941main(int ac, char **av)
942{
943 fd_set rset, wset;
944 int in, out, max;
Damien Millere4340be2000-09-16 13:29:08 +1100945 ssize_t len, olen;
Damien Miller7b28dc52000-09-05 13:34:53 +1100946
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000947 __progname = get_progname(av[0]);
Damien Miller7b28dc52000-09-05 13:34:53 +1100948 handle_init();
949
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000950#ifdef DEBUG_SFTP_SERVER
Kevin Stevesef4eea92001-02-05 12:42:17 +0000951 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000952#endif
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000953
Damien Miller7b28dc52000-09-05 13:34:53 +1100954 in = dup(STDIN_FILENO);
955 out = dup(STDOUT_FILENO);
956
957 max = 0;
958 if (in > max)
959 max = in;
960 if (out > max)
961 max = out;
962
963 buffer_init(&iqueue);
964 buffer_init(&oqueue);
965
966 for (;;) {
967 FD_ZERO(&rset);
968 FD_ZERO(&wset);
969
970 FD_SET(in, &rset);
971 olen = buffer_len(&oqueue);
972 if (olen > 0)
973 FD_SET(out, &wset);
974
975 if (select(max+1, &rset, &wset, NULL, NULL) < 0) {
976 if (errno == EINTR)
977 continue;
978 exit(2);
979 }
980
981 /* copy stdin to iqueue */
982 if (FD_ISSET(in, &rset)) {
983 char buf[4*4096];
984 len = read(in, buf, sizeof buf);
985 if (len == 0) {
986 debug("read eof");
987 exit(0);
988 } else if (len < 0) {
989 error("read error");
990 exit(1);
991 } else {
992 buffer_append(&iqueue, buf, len);
993 }
994 }
995 /* send oqueue to stdout */
996 if (FD_ISSET(out, &wset)) {
997 len = write(out, buffer_ptr(&oqueue), olen);
998 if (len < 0) {
999 error("write error");
1000 exit(1);
1001 } else {
1002 buffer_consume(&oqueue, len);
1003 }
1004 }
1005 /* process requests from client */
1006 process();
1007 }
1008}