2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
25 RCSID("$OpenBSD: sftp-server.c,v 1.21 2001/03/03 21:40:30 millert Exp $");
34 #include "sftp-common.h"
37 #define get_int64() buffer_get_int64(&iqueue);
38 #define get_int() buffer_get_int(&iqueue);
39 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
42 #ifdef HAVE___PROGNAME
43 extern char *__progname;
48 /* input and output queue */
52 /* portable attibutes, etc. */
54 typedef struct Stat Stat;
63 errno_to_portable(int unixerrno)
74 ret = SSH2_FX_NO_SUCH_FILE;
79 ret = SSH2_FX_PERMISSION_DENIED;
83 ret = SSH2_FX_BAD_MESSAGE;
86 ret = SSH2_FX_FAILURE;
93 flags_from_portable(int pflags)
96 if ((pflags & SSH2_FXF_READ) &&
97 (pflags & SSH2_FXF_WRITE)) {
99 } else if (pflags & SSH2_FXF_READ) {
101 } else if (pflags & SSH2_FXF_WRITE) {
104 if (pflags & SSH2_FXF_CREAT)
106 if (pflags & SSH2_FXF_TRUNC)
108 if (pflags & SSH2_FXF_EXCL)
116 return decode_attrib(&iqueue);
121 typedef struct Handle Handle;
139 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++)
140 handles[i].use = HANDLE_UNUSED;
144 handle_new(int use, char *name, int fd, DIR *dirp)
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;
152 handles[i].name = name;
160 handle_is_ok(int i, int type)
162 return i >= 0 && i < sizeof(handles)/sizeof(Handle) &&
163 handles[i].use == type;
167 handle_to_string(int handle, char **stringp, int *hlenp)
169 if (stringp == NULL || hlenp == NULL)
171 *stringp = xmalloc(sizeof(int32_t));
172 PUT_32BIT(*stringp, handle);
173 *hlenp = sizeof(int32_t);
178 handle_from_string(char *handle, u_int hlen)
181 if (hlen != sizeof(int32_t))
183 val = GET_32BIT(handle);
184 if (handle_is_ok(val, HANDLE_FILE) ||
185 handle_is_ok(val, HANDLE_DIR))
191 handle_to_name(int handle)
193 if (handle_is_ok(handle, HANDLE_DIR)||
194 handle_is_ok(handle, HANDLE_FILE))
195 return handles[handle].name;
200 handle_to_dir(int handle)
202 if (handle_is_ok(handle, HANDLE_DIR))
203 return handles[handle].dirp;
208 handle_to_fd(int handle)
210 if (handle_is_ok(handle, HANDLE_FILE))
211 return handles[handle].fd;
216 handle_close(int handle)
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;
237 handle = get_string(&hlen);
239 val = handle_from_string(handle, hlen);
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);
256 send_status(u_int32_t id, u_int32_t error)
259 TRACE("sent status id %d error %d", id, error);
261 buffer_put_char(&msg, SSH2_FXP_STATUS);
262 buffer_put_int(&msg, id);
263 buffer_put_int(&msg, error);
268 send_data_or_handle(char type, u_int32_t id, char *data, int dlen)
272 buffer_put_char(&msg, type);
273 buffer_put_int(&msg, id);
274 buffer_put_string(&msg, data, dlen);
280 send_data(u_int32_t id, char *data, int dlen)
282 TRACE("sent data id %d len %d", id, dlen);
283 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
287 send_handle(u_int32_t id, int handle)
291 handle_to_string(handle, &string, &hlen);
292 TRACE("sent handle id %d handle %d", id, handle);
293 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
298 send_names(u_int32_t id, int count, Stat *stats)
303 buffer_put_char(&msg, SSH2_FXP_NAME);
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);
317 send_attrib(u_int32_t id, Attrib *a)
320 TRACE("sent attrib id %d have 0x%x", id, a->flags);
322 buffer_put_char(&msg, SSH2_FXP_ATTRS);
323 buffer_put_int(&msg, id);
324 encode_attrib(&msg, a);
335 int version = buffer_get_int(&iqueue);
337 TRACE("client version %d", version);
339 buffer_put_char(&msg, SSH2_FXP_VERSION);
340 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
348 u_int32_t id, pflags;
351 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
354 name = get_string(NULL);
355 pflags = get_int(); /* portable flags */
357 flags = flags_from_portable(pflags);
358 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
359 TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode);
360 fd = open(name, flags, mode);
362 status = errno_to_portable(errno);
364 handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL);
368 send_handle(id, handle);
372 if (status != SSH2_FX_OK)
373 send_status(id, status);
381 int handle, ret, status = SSH2_FX_FAILURE;
384 handle = get_handle();
385 TRACE("close id %d handle %d", id, handle);
386 ret = handle_close(handle);
387 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
388 send_status(id, status);
396 int handle, fd, ret, status = SSH2_FX_FAILURE;
400 handle = get_handle();
404 TRACE("read id %d handle %d off %llu len %d", id, handle,
405 (u_int64_t)off, len);
406 if (len > sizeof buf) {
408 log("read change len %d", len);
410 fd = handle_to_fd(handle);
412 if (lseek(fd, off, SEEK_SET) < 0) {
413 error("process_read: seek failed");
414 status = errno_to_portable(errno);
416 ret = read(fd, buf, len);
418 status = errno_to_portable(errno);
419 } else if (ret == 0) {
420 status = SSH2_FX_EOF;
422 send_data(id, buf, ret);
427 if (status != SSH2_FX_OK)
428 send_status(id, status);
437 int handle, fd, ret, status = SSH2_FX_FAILURE;
441 handle = get_handle();
443 data = get_string(&len);
445 TRACE("write id %d handle %d off %llu len %d", id, handle,
446 (u_int64_t)off, len);
447 fd = handle_to_fd(handle);
449 if (lseek(fd, off, SEEK_SET) < 0) {
450 status = errno_to_portable(errno);
451 error("process_write: seek failed");
454 ret = write(fd, data, len);
456 error("process_write: write failed");
457 status = errno_to_portable(errno);
458 } else if (ret == len) {
461 log("nothing at all written");
465 send_status(id, status);
470 process_do_stat(int do_lstat)
476 int ret, status = SSH2_FX_FAILURE;
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);
483 status = errno_to_portable(errno);
485 stat_to_attrib(&st, &a);
489 if (status != SSH2_FX_OK)
490 send_status(id, status);
512 int fd, ret, handle, status = SSH2_FX_FAILURE;
515 handle = get_handle();
516 TRACE("fstat id %d handle %d", id, handle);
517 fd = handle_to_fd(handle);
519 ret = fstat(fd, &st);
521 status = errno_to_portable(errno);
523 stat_to_attrib(&st, &a);
528 if (status != SSH2_FX_OK)
529 send_status(id, status);
533 attrib_to_tv(Attrib *a)
535 static struct timeval tv[2];
536 tv[0].tv_sec = a->atime;
538 tv[1].tv_sec = a->mtime;
544 process_setstat(void)
550 int status = SSH2_FX_OK;
553 name = get_string(NULL);
555 TRACE("setstat id %d name %s", id, name);
556 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
557 ret = chmod(name, a->perm & 0777);
559 status = errno_to_portable(errno);
561 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
562 ret = utimes(name, attrib_to_tv(a));
564 status = errno_to_portable(errno);
566 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
567 ret = chown(name, a->uid, a->gid);
569 status = errno_to_portable(errno);
571 send_status(id, status);
576 process_fsetstat(void)
581 int status = SSH2_FX_OK;
585 handle = get_handle();
587 TRACE("fsetstat id %d handle %d", id, handle);
588 fd = handle_to_fd(handle);
589 name = handle_to_name(handle);
590 if (fd < 0 || name == NULL) {
591 status = SSH2_FX_FAILURE;
593 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
595 ret = fchmod(fd, a->perm & 0777);
597 ret = chmod(name, a->perm & 0777);
600 status = errno_to_portable(errno);
602 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
604 ret = futimes(fd, attrib_to_tv(a));
606 ret = utimes(name, attrib_to_tv(a));
609 status = errno_to_portable(errno);
611 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
613 ret = fchown(fd, a->uid, a->gid);
615 ret = chown(name, a->uid, a->gid);
618 status = errno_to_portable(errno);
621 send_status(id, status);
625 process_opendir(void)
629 int handle, status = SSH2_FX_FAILURE;
633 path = get_string(NULL);
634 TRACE("opendir id %d path %s", id, path);
635 dirp = opendir(path);
637 status = errno_to_portable(errno);
639 handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
643 send_handle(id, handle);
648 if (status != SSH2_FX_OK)
649 send_status(id, status);
654 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
657 ls_file(char *name, struct stat *st)
662 struct tm *ltime = localtime(&st->st_mtime);
664 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
666 strmode(st->st_mode, mode);
667 if ((pw = getpwuid(st->st_uid)) != NULL) {
670 snprintf(ubuf, sizeof ubuf, "%d", st->st_uid);
673 if ((gr = getgrgid(st->st_gid)) != NULL) {
676 snprintf(gbuf, sizeof gbuf, "%d", st->st_gid);
680 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
681 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
683 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
687 snprintf(buf, sizeof buf, "%s %3d %-8.8s %-8.8s %8llu %s %s", mode,
688 st->st_nlink, user, group, (u_int64_t)st->st_size, tbuf, name);
693 process_readdir(void)
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) {
707 send_status(id, SSH2_FX_FAILURE);
712 int nstats = 10, count = 0, i;
713 stats = xmalloc(nstats * sizeof(Stat));
714 while ((dp = readdir(dirp)) != NULL) {
715 if (count >= nstats) {
717 stats = xrealloc(stats, nstats * sizeof(Stat));
720 snprintf(pathname, sizeof pathname,
721 "%s/%s", path, dp->d_name);
722 if (lstat(pathname, &st) < 0)
724 stat_to_attrib(&st, &(stats[count].attrib));
725 stats[count].name = xstrdup(dp->d_name);
726 stats[count].long_name = ls_file(dp->d_name, &st);
728 /* send up to 100 entries in one message */
729 /* XXX check packet size instead */
734 send_names(id, count, stats);
735 for(i = 0; i < count; i++) {
736 xfree(stats[i].name);
737 xfree(stats[i].long_name);
740 send_status(id, SSH2_FX_EOF);
751 int status = SSH2_FX_FAILURE;
755 name = get_string(NULL);
756 TRACE("remove id %d name %s", id, name);
758 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
759 send_status(id, status);
769 int ret, mode, status = SSH2_FX_FAILURE;
772 name = get_string(NULL);
774 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
775 a->perm & 0777 : 0777;
776 TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
777 ret = mkdir(name, mode);
778 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
779 send_status(id, status);
791 name = get_string(NULL);
792 TRACE("rmdir id %d name %s", id, name);
794 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
795 send_status(id, status);
800 process_realpath(void)
802 char resolvedname[MAXPATHLEN];
807 path = get_string(NULL);
808 if (path[0] == '\0') {
812 TRACE("realpath id %d path %s", id, path);
813 if (realpath(path, resolvedname) == NULL) {
814 send_status(id, errno_to_portable(errno));
817 attrib_clear(&s.attrib);
818 s.name = s.long_name = resolvedname;
819 send_names(id, 1, &s);
829 char *oldpath, *newpath;
830 int ret, status = SSH2_FX_FAILURE;
833 oldpath = get_string(NULL);
834 newpath = get_string(NULL);
835 TRACE("rename id %d old %s new %s", id, oldpath, newpath);
836 /* 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;
841 send_status(id, status);
847 process_extended(void)
853 request = get_string(NULL);
854 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
858 /* stolen from ssh-agent */
867 if (buffer_len(&iqueue) < 5)
868 return; /* Incomplete message. */
869 cp = (u_char *) buffer_ptr(&iqueue);
870 msg_len = GET_32BIT(cp);
871 if (msg_len > 256 * 1024) {
872 error("bad message ");
875 if (buffer_len(&iqueue) < msg_len + 4)
877 buffer_consume(&iqueue, 4);
878 type = buffer_get_char(&iqueue);
901 case SSH2_FXP_SETSTAT:
904 case SSH2_FXP_FSETSTAT:
907 case SSH2_FXP_OPENDIR:
910 case SSH2_FXP_READDIR:
913 case SSH2_FXP_REMOVE:
922 case SSH2_FXP_REALPATH:
928 case SSH2_FXP_RENAME:
931 case SSH2_FXP_EXTENDED:
935 error("Unknown message %d", type);
941 main(int ac, char **av)
945 ssize_t len, olen, set_size;
947 __progname = get_progname(av[0]);
950 #ifdef DEBUG_SFTP_SERVER
951 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
954 in = dup(STDIN_FILENO);
955 out = dup(STDOUT_FILENO);
963 buffer_init(&iqueue);
964 buffer_init(&oqueue);
966 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
967 rset = (fd_set *)xmalloc(set_size);
968 wset = (fd_set *)xmalloc(set_size);
971 memset(rset, 0, set_size);
972 memset(wset, 0, set_size);
975 olen = buffer_len(&oqueue);
979 if (select(max+1, rset, wset, NULL, NULL) < 0) {
985 /* copy stdin to iqueue */
986 if (FD_ISSET(in, rset)) {
988 len = read(in, buf, sizeof buf);
992 } else if (len < 0) {
996 buffer_append(&iqueue, buf, len);
999 /* send oqueue to stdout */
1000 if (FD_ISSET(out, wset)) {
1001 len = write(out, buffer_ptr(&oqueue), olen);
1003 error("write error");
1006 buffer_consume(&oqueue, len);
1009 /* process requests from client */