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.6 2000/09/07 20:27:53 deraadt Exp $");
34 #define SSH_FILEXFER_VERSION 2
36 /* client to server */
37 #define SSH_FXP_INIT 1
38 #define SSH_FXP_OPEN 3
39 #define SSH_FXP_CLOSE 4
40 #define SSH_FXP_READ 5
41 #define SSH_FXP_WRITE 6
42 #define SSH_FXP_LSTAT 7
43 #define SSH_FXP_FSTAT 8
44 #define SSH_FXP_SETSTAT 9
45 #define SSH_FXP_FSETSTAT 10
46 #define SSH_FXP_OPENDIR 11
47 #define SSH_FXP_READDIR 12
48 #define SSH_FXP_REMOVE 13
49 #define SSH_FXP_MKDIR 14
50 #define SSH_FXP_RMDIR 15
51 #define SSH_FXP_REALPATH 16
52 #define SSH_FXP_STAT 17
53 #define SSH_FXP_RENAME 18
55 /* server to client */
56 #define SSH_FXP_VERSION 2
57 #define SSH_FXP_STATUS 101
58 #define SSH_FXP_HANDLE 102
59 #define SSH_FXP_DATA 103
60 #define SSH_FXP_NAME 104
61 #define SSH_FXP_ATTRS 105
63 /* portable open modes */
64 #define SSH_FXF_READ 0x01
65 #define SSH_FXF_WRITE 0x02
66 #define SSH_FXF_APPEND 0x04
67 #define SSH_FXF_CREAT 0x08
68 #define SSH_FXF_TRUNC 0x10
69 #define SSH_FXF_EXCL 0x20
72 #define SSH_FXA_HAVE_SIZE 0x01
73 #define SSH_FXA_HAVE_UGID 0x02
74 #define SSH_FXA_HAVE_PERM 0x04
75 #define SSH_FXA_HAVE_TIME 0x08
78 #define SSH_FX_OK 0x00
79 #define SSH_FX_EOF 0x01
80 #define SSH_FX_NO_SUCH_FILE 0x02
81 #define SSH_FX_PERMISSION_DENIED 0x03
82 #define SSH_FX_FAILURE 0x04
83 #define SSH_FX_BAD_MESSAGE 0x05
84 #define SSH_FX_NO_CONNECTION 0x06
85 #define SSH_FX_CONNECTION_LOST 0x07
89 #define get_int() buffer_get_int(&iqueue);
90 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
93 #ifdef HAVE___PROGNAME
94 extern char *__progname;
99 /* input and output queue */
103 /* portable attibutes, etc. */
105 typedef struct Attrib Attrib;
106 typedef struct Stat Stat;
129 errno_to_portable(int unixerrno)
140 ret = SSH_FX_NO_SUCH_FILE;
145 ret = SSH_FX_PERMISSION_DENIED;
149 ret = SSH_FX_BAD_MESSAGE;
152 ret = SSH_FX_FAILURE;
159 flags_from_portable(int pflags)
162 if (pflags & SSH_FXF_READ &&
163 pflags & SSH_FXF_WRITE) {
165 } else if (pflags & SSH_FXF_READ) {
167 } else if (pflags & SSH_FXF_WRITE) {
170 if (pflags & SSH_FXF_CREAT)
172 if (pflags & SSH_FXF_TRUNC)
174 if (pflags & SSH_FXF_EXCL)
180 attrib_clear(Attrib *a)
194 decode_attrib(Buffer *b)
198 a.flags = buffer_get_int(b);
199 if (a.flags & SSH_FXA_HAVE_SIZE) {
200 a.size_high = buffer_get_int(b);
201 a.size_low = buffer_get_int(b);
202 a.size = (((u_int64_t) a.size_high) << 32) + a.size_low;
204 if (a.flags & SSH_FXA_HAVE_UGID) {
205 a.uid = buffer_get_int(b);
206 a.gid = buffer_get_int(b);
208 if (a.flags & SSH_FXA_HAVE_PERM) {
209 a.perm = buffer_get_int(b);
211 if (a.flags & SSH_FXA_HAVE_TIME) {
212 a.atime = buffer_get_int(b);
213 a.mtime = buffer_get_int(b);
219 encode_attrib(Buffer *b, Attrib *a)
221 buffer_put_int(b, a->flags);
222 if (a->flags & SSH_FXA_HAVE_SIZE) {
223 buffer_put_int(b, a->size_high);
224 buffer_put_int(b, a->size_low);
226 if (a->flags & SSH_FXA_HAVE_UGID) {
227 buffer_put_int(b, a->uid);
228 buffer_put_int(b, a->gid);
230 if (a->flags & SSH_FXA_HAVE_PERM) {
231 buffer_put_int(b, a->perm);
233 if (a->flags & SSH_FXA_HAVE_TIME) {
234 buffer_put_int(b, a->atime);
235 buffer_put_int(b, a->mtime);
240 stat_to_attrib(struct stat *st)
245 a.flags |= SSH_FXA_HAVE_SIZE;
246 a.size = st->st_size;
248 a.size_high = (u_int32_t) (a.size >> 32);
249 a.flags |= SSH_FXA_HAVE_UGID;
252 a.flags |= SSH_FXA_HAVE_PERM;
253 a.perm = st->st_mode;
254 a.flags |= SSH_FXA_HAVE_TIME;
255 a.atime = st->st_atime;
256 a.mtime = st->st_mtime;
263 return decode_attrib(&iqueue);
268 typedef struct Handle Handle;
286 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++)
287 handles[i].use = HANDLE_UNUSED;
291 handle_new(int use, char *name, int fd, DIR *dirp)
294 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
295 if (handles[i].use == HANDLE_UNUSED) {
296 handles[i].use = use;
297 handles[i].dirp = dirp;
299 handles[i].name = name;
307 handle_is_ok(int i, int type)
309 return i >= 0 && i < sizeof(handles)/sizeof(Handle) && handles[i].use == type;
313 handle_to_string(int handle, char **stringp, int *hlenp)
316 if (stringp == NULL || hlenp == NULL)
318 snprintf(buf, sizeof buf, "%d", handle);
319 *stringp = xstrdup(buf);
320 *hlenp = strlen(*stringp);
325 handle_from_string(char *handle, u_int hlen)
329 long lval = strtol(handle, &ep, 10);
333 if (handle_is_ok(val, HANDLE_FILE) ||
334 handle_is_ok(val, HANDLE_DIR))
340 handle_to_name(int handle)
342 if (handle_is_ok(handle, HANDLE_DIR)||
343 handle_is_ok(handle, HANDLE_FILE))
344 return handles[handle].name;
349 handle_to_dir(int handle)
351 if (handle_is_ok(handle, HANDLE_DIR))
352 return handles[handle].dirp;
357 handle_to_fd(int handle)
359 if (handle_is_ok(handle, HANDLE_FILE))
360 return handles[handle].fd;
365 handle_close(int handle)
368 if (handle_is_ok(handle, HANDLE_FILE)) {
369 ret = close(handles[handle].fd);
370 handles[handle].use = HANDLE_UNUSED;
371 } else if (handle_is_ok(handle, HANDLE_DIR)) {
372 ret = closedir(handles[handle].dirp);
373 handles[handle].use = HANDLE_UNUSED;
386 handle = get_string(&hlen);
387 val = handle_from_string(handle, hlen);
397 int mlen = buffer_len(m);
398 buffer_put_int(&oqueue, mlen);
399 buffer_append(&oqueue, buffer_ptr(m), mlen);
400 buffer_consume(m, mlen);
404 send_status(u_int32_t id, u_int32_t error)
407 TRACE("sent status id %d error %d", id, error);
409 buffer_put_char(&msg, SSH_FXP_STATUS);
410 buffer_put_int(&msg, id);
411 buffer_put_int(&msg, error);
416 send_data_or_handle(char type, u_int32_t id, char *data, int dlen)
420 buffer_put_char(&msg, type);
421 buffer_put_int(&msg, id);
422 buffer_put_string(&msg, data, dlen);
428 send_data(u_int32_t id, char *data, int dlen)
430 TRACE("sent data id %d len %d", id, dlen);
431 send_data_or_handle(SSH_FXP_DATA, id, data, dlen);
435 send_handle(u_int32_t id, int handle)
439 handle_to_string(handle, &string, &hlen);
440 TRACE("sent handle id %d handle %d", id, handle);
441 send_data_or_handle(SSH_FXP_HANDLE, id, string, hlen);
446 send_names(u_int32_t id, int count, Stat *stats)
451 buffer_put_char(&msg, SSH_FXP_NAME);
452 buffer_put_int(&msg, id);
453 buffer_put_int(&msg, count);
454 TRACE("sent names id %d count %d", id, count);
455 for (i = 0; i < count; i++) {
456 buffer_put_cstring(&msg, stats[i].name);
457 buffer_put_cstring(&msg, stats[i].long_name);
458 encode_attrib(&msg, &stats[i].attrib);
465 send_attrib(u_int32_t id, Attrib *a)
468 TRACE("sent attrib id %d have 0x%x", id, a->flags);
470 buffer_put_char(&msg, SSH_FXP_ATTRS);
471 buffer_put_int(&msg, id);
472 encode_attrib(&msg, a);
483 int version = buffer_get_int(&iqueue);
485 TRACE("client version %d", version);
487 buffer_put_char(&msg, SSH_FXP_VERSION);
488 buffer_put_int(&msg, SSH_FILEXFER_VERSION);
496 u_int32_t id, pflags;
499 int handle, fd, flags, mode, status = SSH_FX_FAILURE;
502 name = get_string(NULL);
505 flags = flags_from_portable(pflags);
506 mode = (a->flags & SSH_FXA_HAVE_PERM) ? a->perm : 0666;
507 TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode);
508 fd = open(name, flags, mode);
510 status = errno_to_portable(errno);
512 handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL);
516 send_handle(id, handle);
520 if (status != SSH_FX_OK)
521 send_status(id, status);
529 int handle, ret, status = SSH_FX_FAILURE;
532 handle = get_handle();
533 TRACE("close id %d handle %d", id, handle);
534 ret = handle_close(handle);
535 status = (ret == -1) ? errno_to_portable(errno) : SSH_FX_OK;
536 send_status(id, status);
543 u_int32_t id, off_high, off_low, len;
544 int handle, fd, ret, status = SSH_FX_FAILURE;
548 handle = get_handle();
549 off_high = get_int();
553 off = (((u_int64_t) off_high) << 32) + off_low;
554 TRACE("read id %d handle %d off %lld len %d", id, handle, off, len);
555 if (len > sizeof buf) {
557 log("read change len %d", len);
559 fd = handle_to_fd(handle);
561 if (lseek(fd, off, SEEK_SET) < 0) {
562 error("process_read: seek failed");
563 status = errno_to_portable(errno);
565 ret = read(fd, buf, len);
567 status = errno_to_portable(errno);
568 } else if (ret == 0) {
571 send_data(id, buf, ret);
576 if (status != SSH_FX_OK)
577 send_status(id, status);
583 u_int32_t id, off_high, off_low;
586 int handle, fd, ret, status = SSH_FX_FAILURE;
590 handle = get_handle();
591 off_high = get_int();
593 data = get_string(&len);
595 off = (((u_int64_t) off_high) << 32) + off_low;
596 TRACE("write id %d handle %d off %lld len %d", id, handle, off, len);
597 fd = handle_to_fd(handle);
599 if (lseek(fd, off, SEEK_SET) < 0) {
600 status = errno_to_portable(errno);
601 error("process_write: seek failed");
604 ret = write(fd, data, len);
606 error("process_write: write failed");
607 status = errno_to_portable(errno);
608 } else if (ret == len) {
611 log("nothing at all written");
615 send_status(id, status);
620 process_do_stat(int do_lstat)
626 int ret, status = SSH_FX_FAILURE;
629 name = get_string(NULL);
630 TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name);
631 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
633 status = errno_to_portable(errno);
635 a = stat_to_attrib(&st);
639 if (status != SSH_FX_OK)
640 send_status(id, status);
662 int fd, ret, handle, status = SSH_FX_FAILURE;
665 handle = get_handle();
666 TRACE("fstat id %d handle %d", id, handle);
667 fd = handle_to_fd(handle);
669 ret = fstat(fd, &st);
671 status = errno_to_portable(errno);
673 a = stat_to_attrib(&st);
678 if (status != SSH_FX_OK)
679 send_status(id, status);
683 attrib_to_tv(Attrib *a)
685 static struct timeval tv[2];
686 tv[0].tv_sec = a->atime;
688 tv[1].tv_sec = a->mtime;
694 process_setstat(void)
700 int status = SSH_FX_OK;
703 name = get_string(NULL);
705 TRACE("setstat id %d name %s", id, name);
706 if (a->flags & SSH_FXA_HAVE_PERM) {
707 ret = chmod(name, a->perm & 0777);
709 status = errno_to_portable(errno);
711 if (a->flags & SSH_FXA_HAVE_TIME) {
712 ret = utimes(name, attrib_to_tv(a));
714 status = errno_to_portable(errno);
716 send_status(id, status);
721 process_fsetstat(void)
726 int status = SSH_FX_OK;
730 handle = get_handle();
732 TRACE("fsetstat id %d handle %d", id, handle);
733 fd = handle_to_fd(handle);
734 name = handle_to_name(handle);
735 if ((fd < 0) || (name == NULL)) {
736 status = SSH_FX_FAILURE;
738 if (a->flags & SSH_FXA_HAVE_PERM) {
739 ret = fchmod(fd, a->perm & 0777);
741 status = errno_to_portable(errno);
743 if (a->flags & SSH_FXA_HAVE_TIME) {
745 ret = futimes(fd, attrib_to_tv(a));
747 ret = utimes(name, attrib_to_tv(a));
750 status = errno_to_portable(errno);
753 send_status(id, status);
757 process_opendir(void)
761 int handle, status = SSH_FX_FAILURE;
765 path = get_string(NULL);
766 TRACE("opendir id %d path %s", id, path);
767 dirp = opendir(path);
769 status = errno_to_portable(errno);
771 handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
775 send_handle(id, handle);
780 if (status != SSH_FX_OK)
781 send_status(id, status);
786 ls_file(char *name, struct stat *st)
789 snprintf(buf, sizeof buf, "0%o %d %d %lld %d %s",
790 st->st_mode, st->st_uid, st->st_gid, (long long)st->st_size,(int) st->st_mtime,
796 process_readdir(void)
805 handle = get_handle();
806 TRACE("readdir id %d handle %d", id, handle);
807 dirp = handle_to_dir(handle);
808 path = handle_to_name(handle);
809 if (dirp == NULL || path == NULL) {
810 send_status(id, SSH_FX_FAILURE);
816 int nstats = 10, count = 0, i;
817 stats = xmalloc(nstats * sizeof(Stat));
818 while ((dp = readdir(dirp)) != NULL) {
819 if (count >= nstats) {
821 stats = xrealloc(stats, nstats * sizeof(Stat));
824 snprintf(pathname, sizeof pathname,
825 "%s/%s", path, dp->d_name);
826 if (lstat(pathname, &st) < 0)
828 a = stat_to_attrib(&st);
829 stats[count].attrib = *a;
830 stats[count].name = xstrdup(dp->d_name);
831 stats[count].long_name = ls_file(dp->d_name, &st);
833 /* send up to 100 entries in one message */
837 send_names(id, count, stats);
838 for(i = 0; i < count; i++) {
839 xfree(stats[i].name);
840 xfree(stats[i].long_name);
851 int status = SSH_FX_FAILURE;
855 name = get_string(NULL);
856 TRACE("remove id %d name %s", id, name);
858 status = (ret == -1) ? errno_to_portable(errno) : SSH_FX_OK;
859 send_status(id, status);
869 int ret, mode, status = SSH_FX_FAILURE;
872 name = get_string(NULL);
874 mode = (a->flags & SSH_FXA_HAVE_PERM) ? a->perm & 0777 : 0777;
875 TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
876 ret = mkdir(name, mode);
877 status = (ret == -1) ? errno_to_portable(errno) : SSH_FX_OK;
878 send_status(id, status);
890 name = get_string(NULL);
891 TRACE("rmdir id %d name %s", id, name);
893 status = (ret == -1) ? errno_to_portable(errno) : SSH_FX_OK;
894 send_status(id, status);
899 process_realpath(void)
901 char resolvedname[MAXPATHLEN];
906 path = get_string(NULL);
907 TRACE("realpath id %d path %s", id, path);
908 if (realpath(path, resolvedname) == NULL) {
909 send_status(id, errno_to_portable(errno));
912 attrib_clear(&s.attrib);
913 s.name = s.long_name = resolvedname;
914 send_names(id, 1, &s);
923 char *oldpath, *newpath;
927 oldpath = get_string(NULL);
928 newpath = get_string(NULL);
929 TRACE("rename id %d old %s new %s", id, oldpath, newpath);
930 ret = rename(oldpath, newpath);
931 status = (ret == -1) ? errno_to_portable(errno) : SSH_FX_OK;
932 send_status(id, status);
938 /* stolen from ssh-agent */
943 unsigned int msg_len;
947 if (buffer_len(&iqueue) < 5)
948 return; /* Incomplete message. */
949 cp = (unsigned 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 SSH_FXP_SETSTAT:
984 case SSH_FXP_FSETSTAT:
987 case SSH_FXP_OPENDIR:
990 case SSH_FXP_READDIR:
1002 case SSH_FXP_REALPATH:
1008 case SSH_FXP_RENAME:
1012 error("Unknown message %d", type);
1018 main(int ac, char **av)
1024 __progname = get_progname(av[0]);
1027 in = dup(STDIN_FILENO);
1028 out = dup(STDOUT_FILENO);
1036 buffer_init(&iqueue);
1037 buffer_init(&oqueue);
1044 olen = buffer_len(&oqueue);
1048 if (select(max+1, &rset, &wset, NULL, NULL) < 0) {
1054 /* copy stdin to iqueue */
1055 if (FD_ISSET(in, &rset)) {
1057 len = read(in, buf, sizeof buf);
1061 } else if (len < 0) {
1062 error("read error");
1065 buffer_append(&iqueue, buf, len);
1068 /* send oqueue to stdout */
1069 if (FD_ISSET(out, &wset)) {
1070 len = write(out, buffer_ptr(&oqueue), olen);
1072 error("write error");
1075 buffer_consume(&oqueue, len);
1078 /* process requests from client */