]> andersk Git - openssh.git/blame - sftp-server.c
CVS Sync with OpenBSD
[openssh.git] / sftp-server.c
CommitLineData
b5e300c2 1/*
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.
b5e300c2 12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24#include "includes.h"
ac95f51a 25RCSID("$OpenBSD: sftp-server.c,v 1.19 2001/02/07 18:01:18 itojun Exp $");
b5e300c2 26
b5e300c2 27#include "buffer.h"
28#include "bufaux.h"
29#include "getput.h"
42f11eb2 30#include "log.h"
b5e300c2 31#include "xmalloc.h"
32
f546c780 33#include "sftp.h"
61e96248 34#include "sftp-common.h"
b5e300c2 35
36/* helper */
f546c780 37#define get_int64() buffer_get_int64(&iqueue);
b5e300c2 38#define get_int() buffer_get_int(&iqueue);
39#define get_string(lenp) buffer_get_string(&iqueue, lenp);
f546c780 40#define TRACE debug
b5e300c2 41
260d427b 42#ifdef HAVE___PROGNAME
43extern char *__progname;
44#else
45char *__progname;
46#endif
47
b5e300c2 48/* input and output queue */
49Buffer iqueue;
50Buffer oqueue;
51
52/* portable attibutes, etc. */
53
b5e300c2 54typedef struct Stat Stat;
55
61e96248 56struct Stat {
b5e300c2 57 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:
f546c780 68 ret = SSH2_FX_OK;
b5e300c2 69 break;
70 case ENOENT:
71 case ENOTDIR:
72 case EBADF:
73 case ELOOP:
f546c780 74 ret = SSH2_FX_NO_SUCH_FILE;
b5e300c2 75 break;
76 case EPERM:
77 case EACCES:
78 case EFAULT:
f546c780 79 ret = SSH2_FX_PERMISSION_DENIED;
b5e300c2 80 break;
81 case ENAMETOOLONG:
82 case EINVAL:
f546c780 83 ret = SSH2_FX_BAD_MESSAGE;
b5e300c2 84 break;
85 default:
f546c780 86 ret = SSH2_FX_FAILURE;
b5e300c2 87 break;
88 }
89 return ret;
90}
91
92int
93flags_from_portable(int pflags)
94{
95 int flags = 0;
f546c780 96 if (pflags & SSH2_FXF_READ &&
97 pflags & SSH2_FXF_WRITE) {
b5e300c2 98 flags = O_RDWR;
f546c780 99 } else if (pflags & SSH2_FXF_READ) {
b5e300c2 100 flags = O_RDONLY;
f546c780 101 } else if (pflags & SSH2_FXF_WRITE) {
b5e300c2 102 flags = O_WRONLY;
103 }
f546c780 104 if (pflags & SSH2_FXF_CREAT)
b5e300c2 105 flags |= O_CREAT;
f546c780 106 if (pflags & SSH2_FXF_TRUNC)
b5e300c2 107 flags |= O_TRUNC;
f546c780 108 if (pflags & SSH2_FXF_EXCL)
b5e300c2 109 flags |= O_EXCL;
110 return flags;
111}
112
b5e300c2 113Attrib *
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{
f546c780 162 return i >= 0 && i < sizeof(handles)/sizeof(Handle) &&
163 handles[i].use == type;
b5e300c2 164}
165
166int
167handle_to_string(int handle, char **stringp, int *hlenp)
168{
b5e300c2 169 if (stringp == NULL || hlenp == NULL)
170 return -1;
b5c334cc 171 *stringp = xmalloc(sizeof(int32_t));
172 PUT_32BIT(*stringp, handle);
173 *hlenp = sizeof(int32_t);
b5e300c2 174 return 0;
175}
176
177int
bcbf86ec 178handle_from_string(char *handle, u_int hlen)
b5e300c2 179{
b5c334cc 180 int val;
181 if (hlen != sizeof(int32_t))
b5e300c2 182 return -1;
b5c334cc 183 val = GET_32BIT(handle);
b5e300c2 184 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{
2b87da3b 210 if (handle_is_ok(handle, HANDLE_FILE))
b5e300c2 211 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;
f546c780 235 int val = -1;
bcbf86ec 236 u_int hlen;
b5e300c2 237 handle = get_string(&hlen);
f546c780 238 if (hlen < 256)
239 val = handle_from_string(handle, hlen);
b5e300c2 240 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);
f546c780 261 buffer_put_char(&msg, SSH2_FXP_STATUS);
b5e300c2 262 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);
f546c780 283 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
b5e300c2 284}
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);
f546c780 293 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
b5e300c2 294 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);
f546c780 303 buffer_put_char(&msg, SSH2_FXP_NAME);
b5e300c2 304 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);
f546c780 322 buffer_put_char(&msg, SSH2_FXP_ATTRS);
b5e300c2 323 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);
f546c780 339 buffer_put_char(&msg, SSH2_FXP_VERSION);
340 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
b5e300c2 341 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;
f546c780 351 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
b5e300c2 352
353 id = get_int();
354 name = get_string(NULL);
f546c780 355 pflags = get_int(); /* portable flags */
b5e300c2 356 a = get_attrib();
357 flags = flags_from_portable(pflags);
f546c780 358 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
b5e300c2 359 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);
f546c780 369 status = SSH2_FX_OK;
b5e300c2 370 }
371 }
f546c780 372 if (status != SSH2_FX_OK)
b5e300c2 373 send_status(id, status);
374 xfree(name);
375}
376
377void
378process_close(void)
379{
380 u_int32_t id;
f546c780 381 int handle, ret, status = SSH2_FX_FAILURE;
b5e300c2 382
383 id = get_int();
384 handle = get_handle();
385 TRACE("close id %d handle %d", id, handle);
386 ret = handle_close(handle);
f546c780 387 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 388 send_status(id, status);
389}
390
391void
392process_read(void)
393{
394 char buf[64*1024];
f546c780 395 u_int32_t id, len;
396 int handle, fd, ret, status = SSH2_FX_FAILURE;
b5e300c2 397 u_int64_t off;
398
399 id = get_int();
400 handle = get_handle();
f546c780 401 off = get_int64();
b5e300c2 402 len = get_int();
403
9378f292 404 TRACE("read id %d handle %d off %llu len %d", id, handle,
405 (unsigned long long)off, len);
b5e300c2 406 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) {
f546c780 420 status = SSH2_FX_EOF;
b5e300c2 421 } else {
422 send_data(id, buf, ret);
f546c780 423 status = SSH2_FX_OK;
b5e300c2 424 }
425 }
426 }
f546c780 427 if (status != SSH2_FX_OK)
b5e300c2 428 send_status(id, status);
429}
430
431void
432process_write(void)
433{
f546c780 434 u_int32_t id;
b5e300c2 435 u_int64_t off;
bcbf86ec 436 u_int len;
f546c780 437 int handle, fd, ret, status = SSH2_FX_FAILURE;
b5e300c2 438 char *data;
439
440 id = get_int();
441 handle = get_handle();
f546c780 442 off = get_int64();
b5e300c2 443 data = get_string(&len);
444
9378f292 445 TRACE("write id %d handle %d off %llu len %d", id, handle,
446 (unsigned long long)off, len);
b5e300c2 447 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) {
f546c780 459 status = SSH2_FX_OK;
b5e300c2 460 } 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{
b5c334cc 472 Attrib a;
b5e300c2 473 struct stat st;
474 u_int32_t id;
475 char *name;
f546c780 476 int ret, status = SSH2_FX_FAILURE;
b5e300c2 477
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 {
b5c334cc 485 stat_to_attrib(&st, &a);
486 send_attrib(id, &a);
f546c780 487 status = SSH2_FX_OK;
b5e300c2 488 }
f546c780 489 if (status != SSH2_FX_OK)
b5e300c2 490 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{
b5c334cc 509 Attrib a;
b5e300c2 510 struct stat st;
511 u_int32_t id;
f546c780 512 int fd, ret, handle, status = SSH2_FX_FAILURE;
b5e300c2 513
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 {
b5c334cc 523 stat_to_attrib(&st, &a);
524 send_attrib(id, &a);
f546c780 525 status = SSH2_FX_OK;
b5e300c2 526 }
527 }
f546c780 528 if (status != SSH2_FX_OK)
b5e300c2 529 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;
f546c780 550 int status = SSH2_FX_OK;
b5e300c2 551
552 id = get_int();
553 name = get_string(NULL);
554 a = get_attrib();
555 TRACE("setstat id %d name %s", id, name);
f546c780 556 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
b5e300c2 557 ret = chmod(name, a->perm & 0777);
558 if (ret == -1)
559 status = errno_to_portable(errno);
560 }
f546c780 561 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
b5e300c2 562 ret = utimes(name, attrib_to_tv(a));
563 if (ret == -1)
564 status = errno_to_portable(errno);
565 }
408ba72f 566 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 }
b5e300c2 571 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;
f546c780 581 int status = SSH2_FX_OK;
bcbf86ec 582 char *name;
16e538d4 583
b5e300c2 584 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);
16e538d4 590 if (fd < 0 || name == NULL) {
f546c780 591 status = SSH2_FX_FAILURE;
b5e300c2 592 } else {
f546c780 593 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
2fd3c144 594#ifdef HAVE_FCHMOD
b5e300c2 595 ret = fchmod(fd, a->perm & 0777);
2fd3c144 596#else
c33f0b36 597 ret = chmod(name, a->perm & 0777);
2fd3c144 598#endif
b5e300c2 599 if (ret == -1)
600 status = errno_to_portable(errno);
601 }
f546c780 602 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
b5e300c2 603#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 }
408ba72f 611 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
612 ret = fchown(fd, a->uid, a->gid);
613 if (ret == -1)
614 status = errno_to_portable(errno);
615 }
b5e300c2 616 }
617 send_status(id, status);
618}
619
620void
621process_opendir(void)
622{
623 DIR *dirp = NULL;
624 char *path;
f546c780 625 int handle, status = SSH2_FX_FAILURE;
b5e300c2 626 u_int32_t id;
627
628 id = get_int();
629 path = get_string(NULL);
630 TRACE("opendir id %d path %s", id, path);
2b87da3b 631 dirp = opendir(path);
b5e300c2 632 if (dirp == NULL) {
633 status = errno_to_portable(errno);
634 } else {
635 handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
636 if (handle < 0) {
637 closedir(dirp);
638 } else {
639 send_handle(id, handle);
f546c780 640 status = SSH2_FX_OK;
b5e300c2 641 }
2b87da3b 642
b5e300c2 643 }
f546c780 644 if (status != SSH2_FX_OK)
b5e300c2 645 send_status(id, status);
646 xfree(path);
647}
648
f546c780 649/*
b5c334cc 650 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
f546c780 651 */
b5e300c2 652char *
653ls_file(char *name, struct stat *st)
654{
b5c334cc 655 int sz = 0;
656 struct passwd *pw;
657 struct group *gr;
658 struct tm *ltime = localtime(&st->st_mtime);
659 char *user, *group;
660 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
661
662 strmode(st->st_mode, mode);
663 if ((pw = getpwuid(st->st_uid)) != NULL) {
664 user = pw->pw_name;
665 } else {
666 snprintf(ubuf, sizeof ubuf, "%d", st->st_uid);
667 user = ubuf;
668 }
669 if ((gr = getgrgid(st->st_gid)) != NULL) {
670 group = gr->gr_name;
671 } else {
672 snprintf(gbuf, sizeof gbuf, "%d", st->st_gid);
673 group = gbuf;
674 }
675 if (ltime != NULL) {
676 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
677 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
678 else
679 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
680 }
681 if (sz == 0)
682 tbuf[0] = '\0';
f29ef605 683 snprintf(buf, sizeof buf, "%s %3d %-8.8s %-8.8s %8lld %s %s", mode,
b5c334cc 684 st->st_nlink, user, group, (long long)st->st_size, tbuf, name);
b5e300c2 685 return xstrdup(buf);
686}
687
688void
689process_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();
699 TRACE("readdir id %d handle %d", id, handle);
700 dirp = handle_to_dir(handle);
701 path = handle_to_name(handle);
702 if (dirp == NULL || path == NULL) {
f546c780 703 send_status(id, SSH2_FX_FAILURE);
b5e300c2 704 } else {
b5e300c2 705 struct stat st;
706 char pathname[1024];
707 Stat *stats;
708 int nstats = 10, count = 0, i;
709 stats = xmalloc(nstats * sizeof(Stat));
710 while ((dp = readdir(dirp)) != NULL) {
711 if (count >= nstats) {
712 nstats *= 2;
713 stats = xrealloc(stats, nstats * sizeof(Stat));
714 }
715/* XXX OVERFLOW ? */
716 snprintf(pathname, sizeof pathname,
717 "%s/%s", path, dp->d_name);
718 if (lstat(pathname, &st) < 0)
719 continue;
b5c334cc 720 stat_to_attrib(&st, &(stats[count].attrib));
b5e300c2 721 stats[count].name = xstrdup(dp->d_name);
722 stats[count].long_name = ls_file(dp->d_name, &st);
723 count++;
724 /* send up to 100 entries in one message */
b5c334cc 725 /* XXX check packet size instead */
b5e300c2 726 if (count == 100)
727 break;
728 }
f546c780 729 if (count > 0) {
730 send_names(id, count, stats);
731 for(i = 0; i < count; i++) {
732 xfree(stats[i].name);
733 xfree(stats[i].long_name);
734 }
735 } else {
736 send_status(id, SSH2_FX_EOF);
b5e300c2 737 }
738 xfree(stats);
739 }
740}
741
742void
743process_remove(void)
744{
745 char *name;
746 u_int32_t id;
f546c780 747 int status = SSH2_FX_FAILURE;
b5e300c2 748 int ret;
749
750 id = get_int();
751 name = get_string(NULL);
752 TRACE("remove id %d name %s", id, name);
67b0facb 753 ret = unlink(name);
f546c780 754 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 755 send_status(id, status);
756 xfree(name);
757}
758
759void
760process_mkdir(void)
761{
762 Attrib *a;
763 u_int32_t id;
764 char *name;
f546c780 765 int ret, mode, status = SSH2_FX_FAILURE;
b5e300c2 766
767 id = get_int();
768 name = get_string(NULL);
769 a = get_attrib();
f546c780 770 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
771 a->perm & 0777 : 0777;
b5e300c2 772 TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
773 ret = mkdir(name, mode);
f546c780 774 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 775 send_status(id, status);
776 xfree(name);
777}
778
779void
780process_rmdir(void)
781{
782 u_int32_t id;
783 char *name;
784 int ret, status;
785
786 id = get_int();
787 name = get_string(NULL);
788 TRACE("rmdir id %d name %s", id, name);
789 ret = rmdir(name);
f546c780 790 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 791 send_status(id, status);
792 xfree(name);
793}
794
795void
796process_realpath(void)
797{
798 char resolvedname[MAXPATHLEN];
799 u_int32_t id;
800 char *path;
801
802 id = get_int();
803 path = get_string(NULL);
6b523bae 804 if (path[0] == '\0') {
805 xfree(path);
806 path = xstrdup(".");
807 }
b5e300c2 808 TRACE("realpath id %d path %s", id, path);
809 if (realpath(path, resolvedname) == NULL) {
810 send_status(id, errno_to_portable(errno));
811 } else {
812 Stat s;
813 attrib_clear(&s.attrib);
814 s.name = s.long_name = resolvedname;
815 send_names(id, 1, &s);
816 }
817 xfree(path);
818}
819
820void
821process_rename(void)
822{
823 u_int32_t id;
b5c334cc 824 struct stat st;
b5e300c2 825 char *oldpath, *newpath;
b5c334cc 826 int ret, status = SSH2_FX_FAILURE;
b5e300c2 827
828 id = get_int();
829 oldpath = get_string(NULL);
830 newpath = get_string(NULL);
831 TRACE("rename id %d old %s new %s", id, oldpath, newpath);
b5c334cc 832 /* fail if 'newpath' exists */
833 if (stat(newpath, &st) == -1) {
834 ret = rename(oldpath, newpath);
835 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
836 }
b5e300c2 837 send_status(id, status);
838 xfree(oldpath);
839 xfree(newpath);
840}
841
f546c780 842void
843process_extended(void)
844{
845 u_int32_t id;
846 char *request;
847
848 id = get_int();
849 request = get_string(NULL);
850 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
851 xfree(request);
852}
b5e300c2 853
854/* stolen from ssh-agent */
855
856void
857process(void)
858{
1e3b8b07 859 u_int msg_len;
860 u_int type;
861 u_char *cp;
b5e300c2 862
863 if (buffer_len(&iqueue) < 5)
864 return; /* Incomplete message. */
1e3b8b07 865 cp = (u_char *) buffer_ptr(&iqueue);
b5e300c2 866 msg_len = GET_32BIT(cp);
867 if (msg_len > 256 * 1024) {
868 error("bad message ");
869 exit(11);
870 }
871 if (buffer_len(&iqueue) < msg_len + 4)
872 return;
873 buffer_consume(&iqueue, 4);
874 type = buffer_get_char(&iqueue);
875 switch (type) {
f546c780 876 case SSH2_FXP_INIT:
b5e300c2 877 process_init();
878 break;
f546c780 879 case SSH2_FXP_OPEN:
b5e300c2 880 process_open();
881 break;
f546c780 882 case SSH2_FXP_CLOSE:
b5e300c2 883 process_close();
884 break;
f546c780 885 case SSH2_FXP_READ:
b5e300c2 886 process_read();
887 break;
f546c780 888 case SSH2_FXP_WRITE:
b5e300c2 889 process_write();
890 break;
f546c780 891 case SSH2_FXP_LSTAT:
b5e300c2 892 process_lstat();
893 break;
f546c780 894 case SSH2_FXP_FSTAT:
b5e300c2 895 process_fstat();
896 break;
f546c780 897 case SSH2_FXP_SETSTAT:
b5e300c2 898 process_setstat();
899 break;
f546c780 900 case SSH2_FXP_FSETSTAT:
b5e300c2 901 process_fsetstat();
902 break;
f546c780 903 case SSH2_FXP_OPENDIR:
b5e300c2 904 process_opendir();
905 break;
f546c780 906 case SSH2_FXP_READDIR:
b5e300c2 907 process_readdir();
908 break;
f546c780 909 case SSH2_FXP_REMOVE:
b5e300c2 910 process_remove();
911 break;
f546c780 912 case SSH2_FXP_MKDIR:
b5e300c2 913 process_mkdir();
914 break;
f546c780 915 case SSH2_FXP_RMDIR:
b5e300c2 916 process_rmdir();
917 break;
f546c780 918 case SSH2_FXP_REALPATH:
b5e300c2 919 process_realpath();
920 break;
f546c780 921 case SSH2_FXP_STAT:
b5e300c2 922 process_stat();
923 break;
f546c780 924 case SSH2_FXP_RENAME:
b5e300c2 925 process_rename();
926 break;
f546c780 927 case SSH2_FXP_EXTENDED:
928 process_extended();
929 break;
b5e300c2 930 default:
931 error("Unknown message %d", type);
932 break;
933 }
934}
935
936int
937main(int ac, char **av)
938{
939 fd_set rset, wset;
940 int in, out, max;
bcbf86ec 941 ssize_t len, olen;
b5e300c2 942
260d427b 943 __progname = get_progname(av[0]);
b5e300c2 944 handle_init();
945
b5c334cc 946#ifdef DEBUG_SFTP_SERVER
2b87da3b 947 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
b5c334cc 948#endif
f546c780 949
b5e300c2 950 in = dup(STDIN_FILENO);
951 out = dup(STDOUT_FILENO);
952
953 max = 0;
954 if (in > max)
955 max = in;
956 if (out > max)
957 max = out;
958
959 buffer_init(&iqueue);
960 buffer_init(&oqueue);
961
962 for (;;) {
963 FD_ZERO(&rset);
964 FD_ZERO(&wset);
965
966 FD_SET(in, &rset);
967 olen = buffer_len(&oqueue);
968 if (olen > 0)
969 FD_SET(out, &wset);
970
971 if (select(max+1, &rset, &wset, NULL, NULL) < 0) {
972 if (errno == EINTR)
973 continue;
974 exit(2);
975 }
976
977 /* copy stdin to iqueue */
978 if (FD_ISSET(in, &rset)) {
979 char buf[4*4096];
980 len = read(in, buf, sizeof buf);
981 if (len == 0) {
982 debug("read eof");
983 exit(0);
984 } else if (len < 0) {
985 error("read error");
986 exit(1);
987 } else {
988 buffer_append(&iqueue, buf, len);
989 }
990 }
991 /* send oqueue to stdout */
992 if (FD_ISSET(out, &wset)) {
993 len = write(out, buffer_ptr(&oqueue), olen);
994 if (len < 0) {
995 error("write error");
996 exit(1);
997 } else {
998 buffer_consume(&oqueue, len);
999 }
1000 }
1001 /* process requests from client */
1002 process();
1003 }
1004}
This page took 0.206935 seconds and 5 git commands to generate.