2 * Copyright (c) 2000, 2001 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.30 2001/07/31 12:42:50 jakob 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 /* Version of client */
55 /* portable attibutes, etc. */
57 typedef struct Stat Stat;
66 errno_to_portable(int unixerrno)
78 ret = SSH2_FX_NO_SUCH_FILE;
83 ret = SSH2_FX_PERMISSION_DENIED;
87 ret = SSH2_FX_BAD_MESSAGE;
90 ret = SSH2_FX_FAILURE;
97 flags_from_portable(int pflags)
101 if ((pflags & SSH2_FXF_READ) &&
102 (pflags & SSH2_FXF_WRITE)) {
104 } else if (pflags & SSH2_FXF_READ) {
106 } else if (pflags & SSH2_FXF_WRITE) {
109 if (pflags & SSH2_FXF_CREAT)
111 if (pflags & SSH2_FXF_TRUNC)
113 if (pflags & SSH2_FXF_EXCL)
121 return decode_attrib(&iqueue);
126 typedef struct Handle Handle;
147 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++)
148 handles[i].use = HANDLE_UNUSED;
152 handle_new(int use, char *name, int fd, DIR *dirp)
156 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
157 if (handles[i].use == HANDLE_UNUSED) {
158 handles[i].use = use;
159 handles[i].dirp = dirp;
161 handles[i].name = name;
169 handle_is_ok(int i, int type)
171 return i >= 0 && i < sizeof(handles)/sizeof(Handle) &&
172 handles[i].use == type;
176 handle_to_string(int handle, char **stringp, int *hlenp)
178 if (stringp == NULL || hlenp == NULL)
180 *stringp = xmalloc(sizeof(int32_t));
181 PUT_32BIT(*stringp, handle);
182 *hlenp = sizeof(int32_t);
187 handle_from_string(char *handle, u_int hlen)
191 if (hlen != sizeof(int32_t))
193 val = GET_32BIT(handle);
194 if (handle_is_ok(val, HANDLE_FILE) ||
195 handle_is_ok(val, HANDLE_DIR))
201 handle_to_name(int handle)
203 if (handle_is_ok(handle, HANDLE_DIR)||
204 handle_is_ok(handle, HANDLE_FILE))
205 return handles[handle].name;
210 handle_to_dir(int handle)
212 if (handle_is_ok(handle, HANDLE_DIR))
213 return handles[handle].dirp;
218 handle_to_fd(int handle)
220 if (handle_is_ok(handle, HANDLE_FILE))
221 return handles[handle].fd;
226 handle_close(int handle)
230 if (handle_is_ok(handle, HANDLE_FILE)) {
231 ret = close(handles[handle].fd);
232 handles[handle].use = HANDLE_UNUSED;
233 } else if (handle_is_ok(handle, HANDLE_DIR)) {
234 ret = closedir(handles[handle].dirp);
235 handles[handle].use = HANDLE_UNUSED;
249 handle = get_string(&hlen);
251 val = handle_from_string(handle, hlen);
261 int mlen = buffer_len(m);
263 buffer_put_int(&oqueue, mlen);
264 buffer_append(&oqueue, buffer_ptr(m), mlen);
265 buffer_consume(m, mlen);
269 send_status(u_int32_t id, u_int32_t error)
272 const char *status_messages[] = {
273 "Success", /* SSH_FX_OK */
274 "End of file", /* SSH_FX_EOF */
275 "No such file", /* SSH_FX_NO_SUCH_FILE */
276 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
277 "Failure", /* SSH_FX_FAILURE */
278 "Bad message", /* SSH_FX_BAD_MESSAGE */
279 "No connection", /* SSH_FX_NO_CONNECTION */
280 "Connection lost", /* SSH_FX_CONNECTION_LOST */
281 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
282 "Unknown error" /* Others */
285 TRACE("sent status id %d error %d", id, error);
287 buffer_put_char(&msg, SSH2_FXP_STATUS);
288 buffer_put_int(&msg, id);
289 buffer_put_int(&msg, error);
291 buffer_put_cstring(&msg,
292 status_messages[MIN(error,SSH2_FX_MAX)]);
293 buffer_put_cstring(&msg, "");
299 send_data_or_handle(char type, u_int32_t id, char *data, int dlen)
304 buffer_put_char(&msg, type);
305 buffer_put_int(&msg, id);
306 buffer_put_string(&msg, data, dlen);
312 send_data(u_int32_t id, char *data, int dlen)
314 TRACE("sent data id %d len %d", id, dlen);
315 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
319 send_handle(u_int32_t id, int handle)
324 handle_to_string(handle, &string, &hlen);
325 TRACE("sent handle id %d handle %d", id, handle);
326 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
331 send_names(u_int32_t id, int count, Stat *stats)
337 buffer_put_char(&msg, SSH2_FXP_NAME);
338 buffer_put_int(&msg, id);
339 buffer_put_int(&msg, count);
340 TRACE("sent names id %d count %d", id, count);
341 for (i = 0; i < count; i++) {
342 buffer_put_cstring(&msg, stats[i].name);
343 buffer_put_cstring(&msg, stats[i].long_name);
344 encode_attrib(&msg, &stats[i].attrib);
351 send_attrib(u_int32_t id, Attrib *a)
355 TRACE("sent attrib id %d have 0x%x", id, a->flags);
357 buffer_put_char(&msg, SSH2_FXP_ATTRS);
358 buffer_put_int(&msg, id);
359 encode_attrib(&msg, a);
371 version = buffer_get_int(&iqueue);
372 TRACE("client version %d", version);
374 buffer_put_char(&msg, SSH2_FXP_VERSION);
375 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
383 u_int32_t id, pflags;
386 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
389 name = get_string(NULL);
390 pflags = get_int(); /* portable flags */
392 flags = flags_from_portable(pflags);
393 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
394 TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode);
395 fd = open(name, flags, mode);
397 status = errno_to_portable(errno);
399 handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL);
403 send_handle(id, handle);
407 if (status != SSH2_FX_OK)
408 send_status(id, status);
416 int handle, ret, status = SSH2_FX_FAILURE;
419 handle = get_handle();
420 TRACE("close id %d handle %d", id, handle);
421 ret = handle_close(handle);
422 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
423 send_status(id, status);
431 int handle, fd, ret, status = SSH2_FX_FAILURE;
435 handle = get_handle();
439 TRACE("read id %d handle %d off %llu len %d", id, handle,
440 (u_int64_t)off, len);
441 if (len > sizeof buf) {
443 log("read change len %d", len);
445 fd = handle_to_fd(handle);
447 if (lseek(fd, off, SEEK_SET) < 0) {
448 error("process_read: seek failed");
449 status = errno_to_portable(errno);
451 ret = read(fd, buf, len);
453 status = errno_to_portable(errno);
454 } else if (ret == 0) {
455 status = SSH2_FX_EOF;
457 send_data(id, buf, ret);
462 if (status != SSH2_FX_OK)
463 send_status(id, status);
472 int handle, fd, ret, status = SSH2_FX_FAILURE;
476 handle = get_handle();
478 data = get_string(&len);
480 TRACE("write id %d handle %d off %llu len %d", id, handle,
481 (u_int64_t)off, len);
482 fd = handle_to_fd(handle);
484 if (lseek(fd, off, SEEK_SET) < 0) {
485 status = errno_to_portable(errno);
486 error("process_write: seek failed");
489 ret = write(fd, data, len);
491 error("process_write: write failed");
492 status = errno_to_portable(errno);
493 } else if (ret == len) {
496 log("nothing at all written");
500 send_status(id, status);
505 process_do_stat(int do_lstat)
511 int ret, status = SSH2_FX_FAILURE;
514 name = get_string(NULL);
515 TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name);
516 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
518 status = errno_to_portable(errno);
520 stat_to_attrib(&st, &a);
524 if (status != SSH2_FX_OK)
525 send_status(id, status);
547 int fd, ret, handle, status = SSH2_FX_FAILURE;
550 handle = get_handle();
551 TRACE("fstat id %d handle %d", id, handle);
552 fd = handle_to_fd(handle);
554 ret = fstat(fd, &st);
556 status = errno_to_portable(errno);
558 stat_to_attrib(&st, &a);
563 if (status != SSH2_FX_OK)
564 send_status(id, status);
567 static struct timeval *
568 attrib_to_tv(Attrib *a)
570 static struct timeval tv[2];
572 tv[0].tv_sec = a->atime;
574 tv[1].tv_sec = a->mtime;
580 process_setstat(void)
586 int status = SSH2_FX_OK;
589 name = get_string(NULL);
591 TRACE("setstat id %d name %s", id, name);
592 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
593 ret = chmod(name, a->perm & 0777);
595 status = errno_to_portable(errno);
597 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
598 ret = utimes(name, attrib_to_tv(a));
600 status = errno_to_portable(errno);
602 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
603 ret = chown(name, a->uid, a->gid);
605 status = errno_to_portable(errno);
607 send_status(id, status);
612 process_fsetstat(void)
617 int status = SSH2_FX_OK;
621 handle = get_handle();
623 TRACE("fsetstat id %d handle %d", id, handle);
624 fd = handle_to_fd(handle);
625 name = handle_to_name(handle);
626 if (fd < 0 || name == NULL) {
627 status = SSH2_FX_FAILURE;
629 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
631 ret = fchmod(fd, a->perm & 0777);
633 ret = chmod(name, a->perm & 0777);
636 status = errno_to_portable(errno);
638 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
640 ret = futimes(fd, attrib_to_tv(a));
642 ret = utimes(name, attrib_to_tv(a));
645 status = errno_to_portable(errno);
647 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
649 ret = fchown(fd, a->uid, a->gid);
651 ret = chown(name, a->uid, a->gid);
654 status = errno_to_portable(errno);
657 send_status(id, status);
661 process_opendir(void)
665 int handle, status = SSH2_FX_FAILURE;
669 path = get_string(NULL);
670 TRACE("opendir id %d path %s", id, path);
671 dirp = opendir(path);
673 status = errno_to_portable(errno);
675 handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
679 send_handle(id, handle);
684 if (status != SSH2_FX_OK)
685 send_status(id, status);
690 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
693 ls_file(char *name, struct stat *st)
695 int ulen, glen, sz = 0;
698 struct tm *ltime = localtime(&st->st_mtime);
700 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
702 strmode(st->st_mode, mode);
703 if ((pw = getpwuid(st->st_uid)) != NULL) {
706 snprintf(ubuf, sizeof ubuf, "%d", st->st_uid);
709 if ((gr = getgrgid(st->st_gid)) != NULL) {
712 snprintf(gbuf, sizeof gbuf, "%d", st->st_gid);
716 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
717 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
719 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
723 ulen = MAX(strlen(user), 8);
724 glen = MAX(strlen(group), 8);
725 snprintf(buf, sizeof buf, "%s %3d %-*s %-*s %8llu %s %s", mode,
726 st->st_nlink, ulen, user, glen, group,
727 (u_int64_t)st->st_size, tbuf, name);
732 process_readdir(void)
741 handle = get_handle();
742 TRACE("readdir id %d handle %d", id, handle);
743 dirp = handle_to_dir(handle);
744 path = handle_to_name(handle);
745 if (dirp == NULL || path == NULL) {
746 send_status(id, SSH2_FX_FAILURE);
751 int nstats = 10, count = 0, i;
752 stats = xmalloc(nstats * sizeof(Stat));
753 while ((dp = readdir(dirp)) != NULL) {
754 if (count >= nstats) {
756 stats = xrealloc(stats, nstats * sizeof(Stat));
759 snprintf(pathname, sizeof pathname, "%s%s%s", path,
760 strcmp(path, "/") ? "/" : "", dp->d_name);
761 if (lstat(pathname, &st) < 0)
763 stat_to_attrib(&st, &(stats[count].attrib));
764 stats[count].name = xstrdup(dp->d_name);
765 stats[count].long_name = ls_file(dp->d_name, &st);
767 /* send up to 100 entries in one message */
768 /* XXX check packet size instead */
773 send_names(id, count, stats);
774 for(i = 0; i < count; i++) {
775 xfree(stats[i].name);
776 xfree(stats[i].long_name);
779 send_status(id, SSH2_FX_EOF);
790 int status = SSH2_FX_FAILURE;
794 name = get_string(NULL);
795 TRACE("remove id %d name %s", id, name);
797 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
798 send_status(id, status);
808 int ret, mode, status = SSH2_FX_FAILURE;
811 name = get_string(NULL);
813 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
814 a->perm & 0777 : 0777;
815 TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
816 ret = mkdir(name, mode);
817 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
818 send_status(id, status);
830 name = get_string(NULL);
831 TRACE("rmdir id %d name %s", id, name);
833 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
834 send_status(id, status);
839 process_realpath(void)
841 char resolvedname[MAXPATHLEN];
846 path = get_string(NULL);
847 if (path[0] == '\0') {
851 TRACE("realpath id %d path %s", id, path);
852 if (realpath(path, resolvedname) == NULL) {
853 send_status(id, errno_to_portable(errno));
856 attrib_clear(&s.attrib);
857 s.name = s.long_name = resolvedname;
858 send_names(id, 1, &s);
868 char *oldpath, *newpath;
869 int ret, status = SSH2_FX_FAILURE;
872 oldpath = get_string(NULL);
873 newpath = get_string(NULL);
874 TRACE("rename id %d old %s new %s", id, oldpath, newpath);
875 /* fail if 'newpath' exists */
876 if (stat(newpath, &st) == -1) {
877 ret = rename(oldpath, newpath);
878 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
880 send_status(id, status);
886 process_readlink(void)
890 char link[MAXPATHLEN];
894 path = get_string(NULL);
895 TRACE("readlink id %d path %s", id, path);
896 if ((len = readlink(path, link, sizeof(link) - 1)) == -1)
897 send_status(id, errno_to_portable(errno));
902 attrib_clear(&s.attrib);
903 s.name = s.long_name = link;
904 send_names(id, 1, &s);
910 process_symlink(void)
914 char *oldpath, *newpath;
915 int ret, status = SSH2_FX_FAILURE;
918 oldpath = get_string(NULL);
919 newpath = get_string(NULL);
920 TRACE("symlink id %d old %s new %s", id, oldpath, newpath);
921 /* fail if 'newpath' exists */
922 if (stat(newpath, &st) == -1) {
923 ret = symlink(oldpath, newpath);
924 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
926 send_status(id, status);
932 process_extended(void)
938 request = get_string(NULL);
939 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
943 /* stolen from ssh-agent */
952 if (buffer_len(&iqueue) < 5)
953 return; /* Incomplete message. */
954 cp = (u_char *) buffer_ptr(&iqueue);
955 msg_len = GET_32BIT(cp);
956 if (msg_len > 256 * 1024) {
957 error("bad message ");
960 if (buffer_len(&iqueue) < msg_len + 4)
962 buffer_consume(&iqueue, 4);
963 type = buffer_get_char(&iqueue);
986 case SSH2_FXP_SETSTAT:
989 case SSH2_FXP_FSETSTAT:
992 case SSH2_FXP_OPENDIR:
995 case SSH2_FXP_READDIR:
998 case SSH2_FXP_REMOVE:
1001 case SSH2_FXP_MKDIR:
1004 case SSH2_FXP_RMDIR:
1007 case SSH2_FXP_REALPATH:
1013 case SSH2_FXP_RENAME:
1016 case SSH2_FXP_READLINK:
1019 case SSH2_FXP_SYMLINK:
1022 case SSH2_FXP_EXTENDED:
1026 error("Unknown message %d", type);
1032 main(int ac, char **av)
1034 fd_set *rset, *wset;
1036 ssize_t len, olen, set_size;
1038 /* XXX should use getopt */
1040 __progname = get_progname(av[0]);
1043 #ifdef DEBUG_SFTP_SERVER
1044 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
1047 in = dup(STDIN_FILENO);
1048 out = dup(STDOUT_FILENO);
1051 setmode(in, O_BINARY);
1052 setmode(out, O_BINARY);
1061 buffer_init(&iqueue);
1062 buffer_init(&oqueue);
1064 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1065 rset = (fd_set *)xmalloc(set_size);
1066 wset = (fd_set *)xmalloc(set_size);
1069 memset(rset, 0, set_size);
1070 memset(wset, 0, set_size);
1073 olen = buffer_len(&oqueue);
1077 if (select(max+1, rset, wset, NULL, NULL) < 0) {
1083 /* copy stdin to iqueue */
1084 if (FD_ISSET(in, rset)) {
1086 len = read(in, buf, sizeof buf);
1090 } else if (len < 0) {
1091 error("read error");
1094 buffer_append(&iqueue, buf, len);
1097 /* send oqueue to stdout */
1098 if (FD_ISSET(out, wset)) {
1099 len = write(out, buffer_ptr(&oqueue), olen);
1101 error("write error");
1104 buffer_consume(&oqueue, len);
1107 /* process requests from client */