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.18 2001/02/04 22:21:19 stevesk 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 (unsigned long long)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 (unsigned long long)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) {
612 ret = fchown(fd, a->uid, a->gid);
614 status = errno_to_portable(errno);
617 send_status(id, status);
621 process_opendir(void)
625 int handle, status = SSH2_FX_FAILURE;
629 path = get_string(NULL);
630 TRACE("opendir id %d path %s", id, path);
631 dirp = opendir(path);
633 status = errno_to_portable(errno);
635 handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
639 send_handle(id, handle);
644 if (status != SSH2_FX_OK)
645 send_status(id, status);
650 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
653 ls_file(char *name, struct stat *st)
658 struct tm *ltime = localtime(&st->st_mtime);
660 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
662 strmode(st->st_mode, mode);
663 if ((pw = getpwuid(st->st_uid)) != NULL) {
666 snprintf(ubuf, sizeof ubuf, "%d", st->st_uid);
669 if ((gr = getgrgid(st->st_gid)) != NULL) {
672 snprintf(gbuf, sizeof gbuf, "%d", st->st_gid);
676 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
677 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
679 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
683 snprintf(buf, sizeof buf, "%s %3d %-8.8s %-8.8s %8lld %s %s", mode,
684 st->st_nlink, user, group, (long long)st->st_size, tbuf, name);
689 process_readdir(void)
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) {
703 send_status(id, SSH2_FX_FAILURE);
708 int nstats = 10, count = 0, i;
709 stats = xmalloc(nstats * sizeof(Stat));
710 while ((dp = readdir(dirp)) != NULL) {
711 if (count >= nstats) {
713 stats = xrealloc(stats, nstats * sizeof(Stat));
716 snprintf(pathname, sizeof pathname,
717 "%s/%s", path, dp->d_name);
718 if (lstat(pathname, &st) < 0)
720 stat_to_attrib(&st, &(stats[count].attrib));
721 stats[count].name = xstrdup(dp->d_name);
722 stats[count].long_name = ls_file(dp->d_name, &st);
724 /* send up to 100 entries in one message */
725 /* XXX check packet size instead */
730 send_names(id, count, stats);
731 for(i = 0; i < count; i++) {
732 xfree(stats[i].name);
733 xfree(stats[i].long_name);
736 send_status(id, SSH2_FX_EOF);
747 int status = SSH2_FX_FAILURE;
751 name = get_string(NULL);
752 TRACE("remove id %d name %s", id, name);
754 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
755 send_status(id, status);
765 int ret, mode, status = SSH2_FX_FAILURE;
768 name = get_string(NULL);
770 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
771 a->perm & 0777 : 0777;
772 TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
773 ret = mkdir(name, mode);
774 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
775 send_status(id, status);
787 name = get_string(NULL);
788 TRACE("rmdir id %d name %s", id, name);
790 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
791 send_status(id, status);
796 process_realpath(void)
798 char resolvedname[MAXPATHLEN];
803 path = get_string(NULL);
804 if (path[0] == '\0') {
808 TRACE("realpath id %d path %s", id, path);
809 if (realpath(path, resolvedname) == NULL) {
810 send_status(id, errno_to_portable(errno));
813 attrib_clear(&s.attrib);
814 s.name = s.long_name = resolvedname;
815 send_names(id, 1, &s);
825 char *oldpath, *newpath;
826 int ret, status = SSH2_FX_FAILURE;
829 oldpath = get_string(NULL);
830 newpath = get_string(NULL);
831 TRACE("rename id %d old %s new %s", id, oldpath, newpath);
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;
837 send_status(id, status);
843 process_extended(void)
849 request = get_string(NULL);
850 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
854 /* stolen from ssh-agent */
863 if (buffer_len(&iqueue) < 5)
864 return; /* Incomplete message. */
865 cp = (u_char *) buffer_ptr(&iqueue);
866 msg_len = GET_32BIT(cp);
867 if (msg_len > 256 * 1024) {
868 error("bad message ");
871 if (buffer_len(&iqueue) < msg_len + 4)
873 buffer_consume(&iqueue, 4);
874 type = buffer_get_char(&iqueue);
897 case SSH2_FXP_SETSTAT:
900 case SSH2_FXP_FSETSTAT:
903 case SSH2_FXP_OPENDIR:
906 case SSH2_FXP_READDIR:
909 case SSH2_FXP_REMOVE:
918 case SSH2_FXP_REALPATH:
924 case SSH2_FXP_RENAME:
927 case SSH2_FXP_EXTENDED:
931 error("Unknown message %d", type);
937 main(int ac, char **av)
943 __progname = get_progname(av[0]);
946 #ifdef DEBUG_SFTP_SERVER
947 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
950 in = dup(STDIN_FILENO);
951 out = dup(STDOUT_FILENO);
959 buffer_init(&iqueue);
960 buffer_init(&oqueue);
967 olen = buffer_len(&oqueue);
971 if (select(max+1, &rset, &wset, NULL, NULL) < 0) {
977 /* copy stdin to iqueue */
978 if (FD_ISSET(in, &rset)) {
980 len = read(in, buf, sizeof buf);
984 } else if (len < 0) {
988 buffer_append(&iqueue, buf, len);
991 /* send oqueue to stdout */
992 if (FD_ISSET(out, &wset)) {
993 len = write(out, buffer_ptr(&oqueue), olen);
995 error("write error");
998 buffer_consume(&oqueue, len);
1001 /* process requests from client */