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.14 2001/01/21 19:05:56 markus Exp $");
36 #define get_int64() buffer_get_int64(&iqueue);
37 #define get_int() buffer_get_int(&iqueue);
38 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
41 #ifdef HAVE___PROGNAME
42 extern char *__progname;
47 /* input and output queue */
51 /* portable attibutes, etc. */
53 typedef struct Attrib Attrib;
54 typedef struct Stat Stat;
75 errno_to_portable(int unixerrno)
86 ret = SSH2_FX_NO_SUCH_FILE;
91 ret = SSH2_FX_PERMISSION_DENIED;
95 ret = SSH2_FX_BAD_MESSAGE;
98 ret = SSH2_FX_FAILURE;
105 flags_from_portable(int pflags)
108 if (pflags & SSH2_FXF_READ &&
109 pflags & SSH2_FXF_WRITE) {
111 } else if (pflags & SSH2_FXF_READ) {
113 } else if (pflags & SSH2_FXF_WRITE) {
116 if (pflags & SSH2_FXF_CREAT)
118 if (pflags & SSH2_FXF_TRUNC)
120 if (pflags & SSH2_FXF_EXCL)
126 attrib_clear(Attrib *a)
138 decode_attrib(Buffer *b)
142 a.flags = buffer_get_int(b);
143 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
144 a.size = buffer_get_int64(b);
146 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
147 a.uid = buffer_get_int(b);
148 a.gid = buffer_get_int(b);
150 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
151 a.perm = buffer_get_int(b);
153 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
154 a.atime = buffer_get_int(b);
155 a.mtime = buffer_get_int(b);
157 /* vendor-specific extensions */
158 if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) {
161 count = buffer_get_int(b);
162 for (i = 0; i < count; i++) {
163 type = buffer_get_string(b, NULL);
164 data = buffer_get_string(b, NULL);
173 encode_attrib(Buffer *b, Attrib *a)
175 buffer_put_int(b, a->flags);
176 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
177 buffer_put_int64(b, a->size);
179 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
180 buffer_put_int(b, a->uid);
181 buffer_put_int(b, a->gid);
183 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
184 buffer_put_int(b, a->perm);
186 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
187 buffer_put_int(b, a->atime);
188 buffer_put_int(b, a->mtime);
193 stat_to_attrib(struct stat *st, Attrib *a)
197 a->flags |= SSH2_FILEXFER_ATTR_SIZE;
198 a->size = st->st_size;
199 a->flags |= SSH2_FILEXFER_ATTR_UIDGID;
202 a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
203 a->perm = st->st_mode;
204 a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME;
205 a->atime = st->st_atime;
206 a->mtime = st->st_mtime;
212 return decode_attrib(&iqueue);
217 typedef struct Handle Handle;
235 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++)
236 handles[i].use = HANDLE_UNUSED;
240 handle_new(int use, char *name, int fd, DIR *dirp)
243 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
244 if (handles[i].use == HANDLE_UNUSED) {
245 handles[i].use = use;
246 handles[i].dirp = dirp;
248 handles[i].name = name;
256 handle_is_ok(int i, int type)
258 return i >= 0 && i < sizeof(handles)/sizeof(Handle) &&
259 handles[i].use == type;
263 handle_to_string(int handle, char **stringp, int *hlenp)
265 if (stringp == NULL || hlenp == NULL)
267 *stringp = xmalloc(sizeof(int32_t));
268 PUT_32BIT(*stringp, handle);
269 *hlenp = sizeof(int32_t);
274 handle_from_string(char *handle, u_int hlen)
277 if (hlen != sizeof(int32_t))
279 val = GET_32BIT(handle);
280 if (handle_is_ok(val, HANDLE_FILE) ||
281 handle_is_ok(val, HANDLE_DIR))
287 handle_to_name(int handle)
289 if (handle_is_ok(handle, HANDLE_DIR)||
290 handle_is_ok(handle, HANDLE_FILE))
291 return handles[handle].name;
296 handle_to_dir(int handle)
298 if (handle_is_ok(handle, HANDLE_DIR))
299 return handles[handle].dirp;
304 handle_to_fd(int handle)
306 if (handle_is_ok(handle, HANDLE_FILE))
307 return handles[handle].fd;
312 handle_close(int handle)
315 if (handle_is_ok(handle, HANDLE_FILE)) {
316 ret = close(handles[handle].fd);
317 handles[handle].use = HANDLE_UNUSED;
318 } else if (handle_is_ok(handle, HANDLE_DIR)) {
319 ret = closedir(handles[handle].dirp);
320 handles[handle].use = HANDLE_UNUSED;
333 handle = get_string(&hlen);
335 val = handle_from_string(handle, hlen);
345 int mlen = buffer_len(m);
346 buffer_put_int(&oqueue, mlen);
347 buffer_append(&oqueue, buffer_ptr(m), mlen);
348 buffer_consume(m, mlen);
352 send_status(u_int32_t id, u_int32_t error)
355 TRACE("sent status id %d error %d", id, error);
357 buffer_put_char(&msg, SSH2_FXP_STATUS);
358 buffer_put_int(&msg, id);
359 buffer_put_int(&msg, error);
364 send_data_or_handle(char type, u_int32_t id, char *data, int dlen)
368 buffer_put_char(&msg, type);
369 buffer_put_int(&msg, id);
370 buffer_put_string(&msg, data, dlen);
376 send_data(u_int32_t id, char *data, int dlen)
378 TRACE("sent data id %d len %d", id, dlen);
379 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
383 send_handle(u_int32_t id, int handle)
387 handle_to_string(handle, &string, &hlen);
388 TRACE("sent handle id %d handle %d", id, handle);
389 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
394 send_names(u_int32_t id, int count, Stat *stats)
399 buffer_put_char(&msg, SSH2_FXP_NAME);
400 buffer_put_int(&msg, id);
401 buffer_put_int(&msg, count);
402 TRACE("sent names id %d count %d", id, count);
403 for (i = 0; i < count; i++) {
404 buffer_put_cstring(&msg, stats[i].name);
405 buffer_put_cstring(&msg, stats[i].long_name);
406 encode_attrib(&msg, &stats[i].attrib);
413 send_attrib(u_int32_t id, Attrib *a)
416 TRACE("sent attrib id %d have 0x%x", id, a->flags);
418 buffer_put_char(&msg, SSH2_FXP_ATTRS);
419 buffer_put_int(&msg, id);
420 encode_attrib(&msg, a);
431 int version = buffer_get_int(&iqueue);
433 TRACE("client version %d", version);
435 buffer_put_char(&msg, SSH2_FXP_VERSION);
436 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
444 u_int32_t id, pflags;
447 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
450 name = get_string(NULL);
451 pflags = get_int(); /* portable flags */
453 flags = flags_from_portable(pflags);
454 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
455 TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode);
456 fd = open(name, flags, mode);
458 status = errno_to_portable(errno);
460 handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL);
464 send_handle(id, handle);
468 if (status != SSH2_FX_OK)
469 send_status(id, status);
477 int handle, ret, status = SSH2_FX_FAILURE;
480 handle = get_handle();
481 TRACE("close id %d handle %d", id, handle);
482 ret = handle_close(handle);
483 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
484 send_status(id, status);
492 int handle, fd, ret, status = SSH2_FX_FAILURE;
496 handle = get_handle();
500 TRACE("read id %d handle %d off %lld len %d", id, handle, off, len);
501 if (len > sizeof buf) {
503 log("read change len %d", len);
505 fd = handle_to_fd(handle);
507 if (lseek(fd, off, SEEK_SET) < 0) {
508 error("process_read: seek failed");
509 status = errno_to_portable(errno);
511 ret = read(fd, buf, len);
513 status = errno_to_portable(errno);
514 } else if (ret == 0) {
515 status = SSH2_FX_EOF;
517 send_data(id, buf, ret);
522 if (status != SSH2_FX_OK)
523 send_status(id, status);
532 int handle, fd, ret, status = SSH2_FX_FAILURE;
536 handle = get_handle();
538 data = get_string(&len);
540 TRACE("write id %d handle %d off %lld len %d", id, handle, off, len);
541 fd = handle_to_fd(handle);
543 if (lseek(fd, off, SEEK_SET) < 0) {
544 status = errno_to_portable(errno);
545 error("process_write: seek failed");
548 ret = write(fd, data, len);
550 error("process_write: write failed");
551 status = errno_to_portable(errno);
552 } else if (ret == len) {
555 log("nothing at all written");
559 send_status(id, status);
564 process_do_stat(int do_lstat)
570 int ret, status = SSH2_FX_FAILURE;
573 name = get_string(NULL);
574 TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name);
575 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
577 status = errno_to_portable(errno);
579 stat_to_attrib(&st, &a);
583 if (status != SSH2_FX_OK)
584 send_status(id, status);
606 int fd, ret, handle, status = SSH2_FX_FAILURE;
609 handle = get_handle();
610 TRACE("fstat id %d handle %d", id, handle);
611 fd = handle_to_fd(handle);
613 ret = fstat(fd, &st);
615 status = errno_to_portable(errno);
617 stat_to_attrib(&st, &a);
622 if (status != SSH2_FX_OK)
623 send_status(id, status);
627 attrib_to_tv(Attrib *a)
629 static struct timeval tv[2];
630 tv[0].tv_sec = a->atime;
632 tv[1].tv_sec = a->mtime;
638 process_setstat(void)
644 int status = SSH2_FX_OK;
647 name = get_string(NULL);
649 TRACE("setstat id %d name %s", id, name);
650 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
651 ret = chmod(name, a->perm & 0777);
653 status = errno_to_portable(errno);
655 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
656 ret = utimes(name, attrib_to_tv(a));
658 status = errno_to_portable(errno);
660 send_status(id, status);
665 process_fsetstat(void)
670 int status = SSH2_FX_OK;
674 handle = get_handle();
676 TRACE("fsetstat id %d handle %d", id, handle);
677 fd = handle_to_fd(handle);
678 name = handle_to_name(handle);
679 if (fd < 0 || name == NULL) {
680 status = SSH2_FX_FAILURE;
682 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
684 ret = fchmod(fd, a->perm & 0777);
686 ret = chmod(name, a->perm & 0777);
689 status = errno_to_portable(errno);
691 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
693 ret = futimes(fd, attrib_to_tv(a));
695 ret = utimes(name, attrib_to_tv(a));
698 status = errno_to_portable(errno);
701 send_status(id, status);
705 process_opendir(void)
709 int handle, status = SSH2_FX_FAILURE;
713 path = get_string(NULL);
714 TRACE("opendir id %d path %s", id, path);
715 dirp = opendir(path);
717 status = errno_to_portable(errno);
719 handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
723 send_handle(id, handle);
728 if (status != SSH2_FX_OK)
729 send_status(id, status);
734 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
737 ls_file(char *name, struct stat *st)
742 struct tm *ltime = localtime(&st->st_mtime);
744 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
746 strmode(st->st_mode, mode);
747 if ((pw = getpwuid(st->st_uid)) != NULL) {
750 snprintf(ubuf, sizeof ubuf, "%d", st->st_uid);
753 if ((gr = getgrgid(st->st_gid)) != NULL) {
756 snprintf(gbuf, sizeof gbuf, "%d", st->st_gid);
760 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
761 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
763 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
767 snprintf(buf, sizeof buf, "%s %3d %-8.8s %-8.8s %8qd %s %s", mode,
768 st->st_nlink, user, group, (long long)st->st_size, tbuf, name);
773 process_readdir(void)
782 handle = get_handle();
783 TRACE("readdir id %d handle %d", id, handle);
784 dirp = handle_to_dir(handle);
785 path = handle_to_name(handle);
786 if (dirp == NULL || path == NULL) {
787 send_status(id, SSH2_FX_FAILURE);
792 int nstats = 10, count = 0, i;
793 stats = xmalloc(nstats * sizeof(Stat));
794 while ((dp = readdir(dirp)) != NULL) {
795 if (count >= nstats) {
797 stats = xrealloc(stats, nstats * sizeof(Stat));
800 snprintf(pathname, sizeof pathname,
801 "%s/%s", path, dp->d_name);
802 if (lstat(pathname, &st) < 0)
804 stat_to_attrib(&st, &(stats[count].attrib));
805 stats[count].name = xstrdup(dp->d_name);
806 stats[count].long_name = ls_file(dp->d_name, &st);
808 /* send up to 100 entries in one message */
809 /* XXX check packet size instead */
814 send_names(id, count, stats);
815 for(i = 0; i < count; i++) {
816 xfree(stats[i].name);
817 xfree(stats[i].long_name);
820 send_status(id, SSH2_FX_EOF);
831 int status = SSH2_FX_FAILURE;
835 name = get_string(NULL);
836 TRACE("remove id %d name %s", id, name);
838 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
839 send_status(id, status);
849 int ret, mode, status = SSH2_FX_FAILURE;
852 name = get_string(NULL);
854 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
855 a->perm & 0777 : 0777;
856 TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
857 ret = mkdir(name, mode);
858 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
859 send_status(id, status);
871 name = get_string(NULL);
872 TRACE("rmdir id %d name %s", id, name);
874 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
875 send_status(id, status);
880 process_realpath(void)
882 char resolvedname[MAXPATHLEN];
887 path = get_string(NULL);
888 if (path[0] == '\0') {
892 TRACE("realpath id %d path %s", id, path);
893 if (realpath(path, resolvedname) == NULL) {
894 send_status(id, errno_to_portable(errno));
897 attrib_clear(&s.attrib);
898 s.name = s.long_name = resolvedname;
899 send_names(id, 1, &s);
909 char *oldpath, *newpath;
910 int ret, status = SSH2_FX_FAILURE;
913 oldpath = get_string(NULL);
914 newpath = get_string(NULL);
915 TRACE("rename id %d old %s new %s", id, oldpath, newpath);
916 /* fail if 'newpath' exists */
917 if (stat(newpath, &st) == -1) {
918 ret = rename(oldpath, newpath);
919 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
921 send_status(id, status);
927 process_extended(void)
933 request = get_string(NULL);
934 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
938 /* stolen from ssh-agent */
947 if (buffer_len(&iqueue) < 5)
948 return; /* Incomplete message. */
949 cp = (u_char *) buffer_ptr(&iqueue);
950 msg_len = GET_32BIT(cp);
951 if (msg_len > 256 * 1024) {
952 error("bad message ");
955 if (buffer_len(&iqueue) < msg_len + 4)
957 buffer_consume(&iqueue, 4);
958 type = buffer_get_char(&iqueue);
981 case SSH2_FXP_SETSTAT:
984 case SSH2_FXP_FSETSTAT:
987 case SSH2_FXP_OPENDIR:
990 case SSH2_FXP_READDIR:
993 case SSH2_FXP_REMOVE:
1002 case SSH2_FXP_REALPATH:
1008 case SSH2_FXP_RENAME:
1011 case SSH2_FXP_EXTENDED:
1015 error("Unknown message %d", type);
1021 main(int ac, char **av)
1027 __progname = get_progname(av[0]);
1030 #ifdef DEBUG_SFTP_SERVER
1031 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
1034 in = dup(STDIN_FILENO);
1035 out = dup(STDOUT_FILENO);
1043 buffer_init(&iqueue);
1044 buffer_init(&oqueue);
1051 olen = buffer_len(&oqueue);
1055 if (select(max+1, &rset, &wset, NULL, NULL) < 0) {
1061 /* copy stdin to iqueue */
1062 if (FD_ISSET(in, &rset)) {
1064 len = read(in, buf, sizeof buf);
1068 } else if (len < 0) {
1069 error("read error");
1072 buffer_append(&iqueue, buf, len);
1075 /* send oqueue to stdout */
1076 if (FD_ISSET(out, &wset)) {
1077 len = write(out, buffer_ptr(&oqueue), olen);
1079 error("write error");
1082 buffer_consume(&oqueue, len);
1085 /* process requests from client */