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.22 2001/03/03 22:07:50 deraadt 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)
75 ret = SSH2_FX_NO_SUCH_FILE;
80 ret = SSH2_FX_PERMISSION_DENIED;
84 ret = SSH2_FX_BAD_MESSAGE;
87 ret = SSH2_FX_FAILURE;
94 flags_from_portable(int pflags)
98 if ((pflags & SSH2_FXF_READ) &&
99 (pflags & SSH2_FXF_WRITE)) {
101 } else if (pflags & SSH2_FXF_READ) {
103 } else if (pflags & SSH2_FXF_WRITE) {
106 if (pflags & SSH2_FXF_CREAT)
108 if (pflags & SSH2_FXF_TRUNC)
110 if (pflags & SSH2_FXF_EXCL)
118 return decode_attrib(&iqueue);
123 typedef struct Handle Handle;
144 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++)
145 handles[i].use = HANDLE_UNUSED;
149 handle_new(int use, char *name, int fd, DIR *dirp)
153 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
154 if (handles[i].use == HANDLE_UNUSED) {
155 handles[i].use = use;
156 handles[i].dirp = dirp;
158 handles[i].name = name;
166 handle_is_ok(int i, int type)
168 return i >= 0 && i < sizeof(handles)/sizeof(Handle) &&
169 handles[i].use == type;
173 handle_to_string(int handle, char **stringp, int *hlenp)
175 if (stringp == NULL || hlenp == NULL)
177 *stringp = xmalloc(sizeof(int32_t));
178 PUT_32BIT(*stringp, handle);
179 *hlenp = sizeof(int32_t);
184 handle_from_string(char *handle, u_int hlen)
188 if (hlen != sizeof(int32_t))
190 val = GET_32BIT(handle);
191 if (handle_is_ok(val, HANDLE_FILE) ||
192 handle_is_ok(val, HANDLE_DIR))
198 handle_to_name(int handle)
200 if (handle_is_ok(handle, HANDLE_DIR)||
201 handle_is_ok(handle, HANDLE_FILE))
202 return handles[handle].name;
207 handle_to_dir(int handle)
209 if (handle_is_ok(handle, HANDLE_DIR))
210 return handles[handle].dirp;
215 handle_to_fd(int handle)
217 if (handle_is_ok(handle, HANDLE_FILE))
218 return handles[handle].fd;
223 handle_close(int handle)
227 if (handle_is_ok(handle, HANDLE_FILE)) {
228 ret = close(handles[handle].fd);
229 handles[handle].use = HANDLE_UNUSED;
230 } else if (handle_is_ok(handle, HANDLE_DIR)) {
231 ret = closedir(handles[handle].dirp);
232 handles[handle].use = HANDLE_UNUSED;
246 handle = get_string(&hlen);
248 val = handle_from_string(handle, hlen);
258 int mlen = buffer_len(m);
260 buffer_put_int(&oqueue, mlen);
261 buffer_append(&oqueue, buffer_ptr(m), mlen);
262 buffer_consume(m, mlen);
266 send_status(u_int32_t id, u_int32_t error)
270 TRACE("sent status id %d error %d", id, error);
272 buffer_put_char(&msg, SSH2_FXP_STATUS);
273 buffer_put_int(&msg, id);
274 buffer_put_int(&msg, error);
279 send_data_or_handle(char type, u_int32_t id, char *data, int dlen)
284 buffer_put_char(&msg, type);
285 buffer_put_int(&msg, id);
286 buffer_put_string(&msg, data, dlen);
292 send_data(u_int32_t id, char *data, int dlen)
294 TRACE("sent data id %d len %d", id, dlen);
295 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
299 send_handle(u_int32_t id, int handle)
304 handle_to_string(handle, &string, &hlen);
305 TRACE("sent handle id %d handle %d", id, handle);
306 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
311 send_names(u_int32_t id, int count, Stat *stats)
317 buffer_put_char(&msg, SSH2_FXP_NAME);
318 buffer_put_int(&msg, id);
319 buffer_put_int(&msg, count);
320 TRACE("sent names id %d count %d", id, count);
321 for (i = 0; i < count; i++) {
322 buffer_put_cstring(&msg, stats[i].name);
323 buffer_put_cstring(&msg, stats[i].long_name);
324 encode_attrib(&msg, &stats[i].attrib);
331 send_attrib(u_int32_t id, Attrib *a)
335 TRACE("sent attrib id %d have 0x%x", id, a->flags);
337 buffer_put_char(&msg, SSH2_FXP_ATTRS);
338 buffer_put_int(&msg, id);
339 encode_attrib(&msg, a);
350 int version = buffer_get_int(&iqueue);
352 TRACE("client version %d", version);
354 buffer_put_char(&msg, SSH2_FXP_VERSION);
355 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
363 u_int32_t id, pflags;
366 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
369 name = get_string(NULL);
370 pflags = get_int(); /* portable flags */
372 flags = flags_from_portable(pflags);
373 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
374 TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode);
375 fd = open(name, flags, mode);
377 status = errno_to_portable(errno);
379 handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL);
383 send_handle(id, handle);
387 if (status != SSH2_FX_OK)
388 send_status(id, status);
396 int handle, ret, status = SSH2_FX_FAILURE;
399 handle = get_handle();
400 TRACE("close id %d handle %d", id, handle);
401 ret = handle_close(handle);
402 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
403 send_status(id, status);
411 int handle, fd, ret, status = SSH2_FX_FAILURE;
415 handle = get_handle();
419 TRACE("read id %d handle %d off %llu len %d", id, handle,
420 (u_int64_t)off, len);
421 if (len > sizeof buf) {
423 log("read change len %d", len);
425 fd = handle_to_fd(handle);
427 if (lseek(fd, off, SEEK_SET) < 0) {
428 error("process_read: seek failed");
429 status = errno_to_portable(errno);
431 ret = read(fd, buf, len);
433 status = errno_to_portable(errno);
434 } else if (ret == 0) {
435 status = SSH2_FX_EOF;
437 send_data(id, buf, ret);
442 if (status != SSH2_FX_OK)
443 send_status(id, status);
452 int handle, fd, ret, status = SSH2_FX_FAILURE;
456 handle = get_handle();
458 data = get_string(&len);
460 TRACE("write id %d handle %d off %llu len %d", id, handle,
461 (u_int64_t)off, len);
462 fd = handle_to_fd(handle);
464 if (lseek(fd, off, SEEK_SET) < 0) {
465 status = errno_to_portable(errno);
466 error("process_write: seek failed");
469 ret = write(fd, data, len);
471 error("process_write: write failed");
472 status = errno_to_portable(errno);
473 } else if (ret == len) {
476 log("nothing at all written");
480 send_status(id, status);
485 process_do_stat(int do_lstat)
491 int ret, status = SSH2_FX_FAILURE;
494 name = get_string(NULL);
495 TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name);
496 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
498 status = errno_to_portable(errno);
500 stat_to_attrib(&st, &a);
504 if (status != SSH2_FX_OK)
505 send_status(id, status);
527 int fd, ret, handle, status = SSH2_FX_FAILURE;
530 handle = get_handle();
531 TRACE("fstat id %d handle %d", id, handle);
532 fd = handle_to_fd(handle);
534 ret = fstat(fd, &st);
536 status = errno_to_portable(errno);
538 stat_to_attrib(&st, &a);
543 if (status != SSH2_FX_OK)
544 send_status(id, status);
548 attrib_to_tv(Attrib *a)
550 static struct timeval tv[2];
552 tv[0].tv_sec = a->atime;
554 tv[1].tv_sec = a->mtime;
560 process_setstat(void)
566 int status = SSH2_FX_OK;
569 name = get_string(NULL);
571 TRACE("setstat id %d name %s", id, name);
572 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
573 ret = chmod(name, a->perm & 0777);
575 status = errno_to_portable(errno);
577 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
578 ret = utimes(name, attrib_to_tv(a));
580 status = errno_to_portable(errno);
582 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
583 ret = chown(name, a->uid, a->gid);
585 status = errno_to_portable(errno);
587 send_status(id, status);
592 process_fsetstat(void)
597 int status = SSH2_FX_OK;
601 handle = get_handle();
603 TRACE("fsetstat id %d handle %d", id, handle);
604 fd = handle_to_fd(handle);
605 name = handle_to_name(handle);
606 if (fd < 0 || name == NULL) {
607 status = SSH2_FX_FAILURE;
609 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
611 ret = fchmod(fd, a->perm & 0777);
613 ret = chmod(name, a->perm & 0777);
616 status = errno_to_portable(errno);
618 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
620 ret = futimes(fd, attrib_to_tv(a));
622 ret = utimes(name, attrib_to_tv(a));
625 status = errno_to_portable(errno);
627 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
629 ret = fchown(fd, a->uid, a->gid);
631 ret = chown(name, a->uid, a->gid);
634 status = errno_to_portable(errno);
637 send_status(id, status);
641 process_opendir(void)
645 int handle, status = SSH2_FX_FAILURE;
649 path = get_string(NULL);
650 TRACE("opendir id %d path %s", id, path);
651 dirp = opendir(path);
653 status = errno_to_portable(errno);
655 handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
659 send_handle(id, handle);
664 if (status != SSH2_FX_OK)
665 send_status(id, status);
670 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
673 ls_file(char *name, struct stat *st)
678 struct tm *ltime = localtime(&st->st_mtime);
680 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
682 strmode(st->st_mode, mode);
683 if ((pw = getpwuid(st->st_uid)) != NULL) {
686 snprintf(ubuf, sizeof ubuf, "%d", st->st_uid);
689 if ((gr = getgrgid(st->st_gid)) != NULL) {
692 snprintf(gbuf, sizeof gbuf, "%d", st->st_gid);
696 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
697 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
699 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
703 snprintf(buf, sizeof buf, "%s %3d %-8.8s %-8.8s %8llu %s %s", mode,
704 st->st_nlink, user, group, (u_int64_t)st->st_size, tbuf, name);
709 process_readdir(void)
718 handle = get_handle();
719 TRACE("readdir id %d handle %d", id, handle);
720 dirp = handle_to_dir(handle);
721 path = handle_to_name(handle);
722 if (dirp == NULL || path == NULL) {
723 send_status(id, SSH2_FX_FAILURE);
728 int nstats = 10, count = 0, i;
729 stats = xmalloc(nstats * sizeof(Stat));
730 while ((dp = readdir(dirp)) != NULL) {
731 if (count >= nstats) {
733 stats = xrealloc(stats, nstats * sizeof(Stat));
736 snprintf(pathname, sizeof pathname,
737 "%s/%s", path, dp->d_name);
738 if (lstat(pathname, &st) < 0)
740 stat_to_attrib(&st, &(stats[count].attrib));
741 stats[count].name = xstrdup(dp->d_name);
742 stats[count].long_name = ls_file(dp->d_name, &st);
744 /* send up to 100 entries in one message */
745 /* XXX check packet size instead */
750 send_names(id, count, stats);
751 for(i = 0; i < count; i++) {
752 xfree(stats[i].name);
753 xfree(stats[i].long_name);
756 send_status(id, SSH2_FX_EOF);
767 int status = SSH2_FX_FAILURE;
771 name = get_string(NULL);
772 TRACE("remove id %d name %s", id, name);
774 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
775 send_status(id, status);
785 int ret, mode, status = SSH2_FX_FAILURE;
788 name = get_string(NULL);
790 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
791 a->perm & 0777 : 0777;
792 TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
793 ret = mkdir(name, mode);
794 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
795 send_status(id, status);
807 name = get_string(NULL);
808 TRACE("rmdir id %d name %s", id, name);
810 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
811 send_status(id, status);
816 process_realpath(void)
818 char resolvedname[MAXPATHLEN];
823 path = get_string(NULL);
824 if (path[0] == '\0') {
828 TRACE("realpath id %d path %s", id, path);
829 if (realpath(path, resolvedname) == NULL) {
830 send_status(id, errno_to_portable(errno));
833 attrib_clear(&s.attrib);
834 s.name = s.long_name = resolvedname;
835 send_names(id, 1, &s);
845 char *oldpath, *newpath;
846 int ret, status = SSH2_FX_FAILURE;
849 oldpath = get_string(NULL);
850 newpath = get_string(NULL);
851 TRACE("rename id %d old %s new %s", id, oldpath, newpath);
852 /* fail if 'newpath' exists */
853 if (stat(newpath, &st) == -1) {
854 ret = rename(oldpath, newpath);
855 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
857 send_status(id, status);
863 process_extended(void)
869 request = get_string(NULL);
870 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
874 /* stolen from ssh-agent */
883 if (buffer_len(&iqueue) < 5)
884 return; /* Incomplete message. */
885 cp = (u_char *) buffer_ptr(&iqueue);
886 msg_len = GET_32BIT(cp);
887 if (msg_len > 256 * 1024) {
888 error("bad message ");
891 if (buffer_len(&iqueue) < msg_len + 4)
893 buffer_consume(&iqueue, 4);
894 type = buffer_get_char(&iqueue);
917 case SSH2_FXP_SETSTAT:
920 case SSH2_FXP_FSETSTAT:
923 case SSH2_FXP_OPENDIR:
926 case SSH2_FXP_READDIR:
929 case SSH2_FXP_REMOVE:
938 case SSH2_FXP_REALPATH:
944 case SSH2_FXP_RENAME:
947 case SSH2_FXP_EXTENDED:
951 error("Unknown message %d", type);
957 main(int ac, char **av)
961 ssize_t len, olen, set_size;
963 __progname = get_progname(av[0]);
966 #ifdef DEBUG_SFTP_SERVER
967 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
970 in = dup(STDIN_FILENO);
971 out = dup(STDOUT_FILENO);
979 buffer_init(&iqueue);
980 buffer_init(&oqueue);
982 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
983 rset = (fd_set *)xmalloc(set_size);
984 wset = (fd_set *)xmalloc(set_size);
987 memset(rset, 0, set_size);
988 memset(wset, 0, set_size);
991 olen = buffer_len(&oqueue);
995 if (select(max+1, rset, wset, NULL, NULL) < 0) {
1001 /* copy stdin to iqueue */
1002 if (FD_ISSET(in, rset)) {
1004 len = read(in, buf, sizeof buf);
1008 } else if (len < 0) {
1009 error("read error");
1012 buffer_append(&iqueue, buf, len);
1015 /* send oqueue to stdout */
1016 if (FD_ISSET(out, wset)) {
1017 len = write(out, buffer_ptr(&oqueue), olen);
1019 error("write error");
1022 buffer_consume(&oqueue, len);
1025 /* process requests from client */