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 /* input and output queue */
97 /* portable attibutes, etc. */
99 typedef struct Attrib Attrib;
100 typedef struct Stat Stat;
123 errno_to_portable(int unixerrno)
134 ret = SSH_FX_NO_SUCH_FILE;
139 ret = SSH_FX_PERMISSION_DENIED;
143 ret = SSH_FX_BAD_MESSAGE;
146 ret = SSH_FX_FAILURE;
153 flags_from_portable(int pflags)
156 if (pflags & SSH_FXF_READ &&
157 pflags & SSH_FXF_WRITE) {
159 } else if (pflags & SSH_FXF_READ) {
161 } else if (pflags & SSH_FXF_WRITE) {
164 if (pflags & SSH_FXF_CREAT)
166 if (pflags & SSH_FXF_TRUNC)
168 if (pflags & SSH_FXF_EXCL)
174 attrib_clear(Attrib *a)
188 decode_attrib(Buffer *b)
192 a.flags = buffer_get_int(b);
193 if (a.flags & SSH_FXA_HAVE_SIZE) {
194 a.size_high = buffer_get_int(b);
195 a.size_low = buffer_get_int(b);
196 a.size = (((u_int64_t) a.size_high) << 32) + a.size_low;
198 if (a.flags & SSH_FXA_HAVE_UGID) {
199 a.uid = buffer_get_int(b);
200 a.gid = buffer_get_int(b);
202 if (a.flags & SSH_FXA_HAVE_PERM) {
203 a.perm = buffer_get_int(b);
205 if (a.flags & SSH_FXA_HAVE_TIME) {
206 a.atime = buffer_get_int(b);
207 a.mtime = buffer_get_int(b);
213 encode_attrib(Buffer *b, Attrib *a)
215 buffer_put_int(b, a->flags);
216 if (a->flags & SSH_FXA_HAVE_SIZE) {
217 buffer_put_int(b, a->size_high);
218 buffer_put_int(b, a->size_low);
220 if (a->flags & SSH_FXA_HAVE_UGID) {
221 buffer_put_int(b, a->uid);
222 buffer_put_int(b, a->gid);
224 if (a->flags & SSH_FXA_HAVE_PERM) {
225 buffer_put_int(b, a->perm);
227 if (a->flags & SSH_FXA_HAVE_TIME) {
228 buffer_put_int(b, a->atime);
229 buffer_put_int(b, a->mtime);
234 stat_to_attrib(struct stat *st)
239 a.flags |= SSH_FXA_HAVE_SIZE;
240 a.size = st->st_size;
242 a.size_high = (u_int32_t) (a.size >> 32);
243 a.flags |= SSH_FXA_HAVE_UGID;
246 a.flags |= SSH_FXA_HAVE_PERM;
247 a.perm = st->st_mode;
248 a.flags |= SSH_FXA_HAVE_TIME;
249 a.atime = st->st_atime;
250 a.mtime = st->st_mtime;
257 return decode_attrib(&iqueue);
262 typedef struct Handle Handle;
280 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++)
281 handles[i].use = HANDLE_UNUSED;
285 handle_new(int use, char *name, int fd, DIR *dirp)
288 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
289 if (handles[i].use == HANDLE_UNUSED) {
290 handles[i].use = use;
291 handles[i].dirp = dirp;
293 handles[i].name = name;
301 handle_is_ok(int i, int type)
303 return i >= 0 && i < sizeof(handles)/sizeof(Handle) && handles[i].use == type;
307 handle_to_string(int handle, char **stringp, int *hlenp)
310 if (stringp == NULL || hlenp == NULL)
312 snprintf(buf, sizeof buf, "%d", handle);
313 *stringp = xstrdup(buf);
314 *hlenp = strlen(*stringp);
319 handle_from_string(char *handle, u_int hlen)
323 long lval = strtol(handle, &ep, 10);
327 if (handle_is_ok(val, HANDLE_FILE) ||
328 handle_is_ok(val, HANDLE_DIR))
334 handle_to_name(int handle)
336 if (handle_is_ok(handle, HANDLE_DIR)||
337 handle_is_ok(handle, HANDLE_FILE))
338 return handles[handle].name;
343 handle_to_dir(int handle)
345 if (handle_is_ok(handle, HANDLE_DIR))
346 return handles[handle].dirp;
351 handle_to_fd(int handle)
353 if (handle_is_ok(handle, HANDLE_FILE))
354 return handles[handle].fd;
359 handle_close(int handle)
362 if (handle_is_ok(handle, HANDLE_FILE)) {
363 ret = close(handles[handle].fd);
364 handles[handle].use = HANDLE_UNUSED;
365 } else if (handle_is_ok(handle, HANDLE_DIR)) {
366 ret = closedir(handles[handle].dirp);
367 handles[handle].use = HANDLE_UNUSED;
380 handle = get_string(&hlen);
381 val = handle_from_string(handle, hlen);
391 int mlen = buffer_len(m);
392 buffer_put_int(&oqueue, mlen);
393 buffer_append(&oqueue, buffer_ptr(m), mlen);
394 buffer_consume(m, mlen);
398 send_status(u_int32_t id, u_int32_t error)
401 TRACE("sent status id %d error %d", id, error);
403 buffer_put_char(&msg, SSH_FXP_STATUS);
404 buffer_put_int(&msg, id);
405 buffer_put_int(&msg, error);
410 send_data_or_handle(char type, u_int32_t id, char *data, int dlen)
414 buffer_put_char(&msg, type);
415 buffer_put_int(&msg, id);
416 buffer_put_string(&msg, data, dlen);
422 send_data(u_int32_t id, char *data, int dlen)
424 TRACE("sent data id %d len %d", id, dlen);
425 send_data_or_handle(SSH_FXP_DATA, id, data, dlen);
429 send_handle(u_int32_t id, int handle)
433 handle_to_string(handle, &string, &hlen);
434 TRACE("sent handle id %d handle %d", id, handle);
435 send_data_or_handle(SSH_FXP_HANDLE, id, string, hlen);
440 send_names(u_int32_t id, int count, Stat *stats)
445 buffer_put_char(&msg, SSH_FXP_NAME);
446 buffer_put_int(&msg, id);
447 buffer_put_int(&msg, count);
448 TRACE("sent names id %d count %d", id, count);
449 for (i = 0; i < count; i++) {
450 buffer_put_cstring(&msg, stats[i].name);
451 buffer_put_cstring(&msg, stats[i].long_name);
452 encode_attrib(&msg, &stats[i].attrib);
459 send_attrib(u_int32_t id, Attrib *a)
462 TRACE("sent attrib id %d have 0x%x", id, a->flags);
464 buffer_put_char(&msg, SSH_FXP_ATTRS);
465 buffer_put_int(&msg, id);
466 encode_attrib(&msg, a);
477 int version = buffer_get_int(&iqueue);
479 TRACE("client version %d", version);
481 buffer_put_char(&msg, SSH_FXP_VERSION);
482 buffer_put_int(&msg, SSH_FILEXFER_VERSION);
490 u_int32_t id, pflags;
493 int handle, fd, flags, mode, status = SSH_FX_FAILURE;
496 name = get_string(NULL);
499 flags = flags_from_portable(pflags);
500 mode = (a->flags & SSH_FXA_HAVE_PERM) ? a->perm : 0666;
501 TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode);
502 fd = open(name, flags, mode);
504 status = errno_to_portable(errno);
506 handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL);
510 send_handle(id, handle);
514 if (status != SSH_FX_OK)
515 send_status(id, status);
523 int handle, ret, status = SSH_FX_FAILURE;
526 handle = get_handle();
527 TRACE("close id %d handle %d", id, handle);
528 ret = handle_close(handle);
529 status = (ret == -1) ? errno_to_portable(errno) : SSH_FX_OK;
530 send_status(id, status);
537 u_int32_t id, off_high, off_low, len;
538 int handle, fd, ret, status = SSH_FX_FAILURE;
542 handle = get_handle();
543 off_high = get_int();
547 off = (((u_int64_t) off_high) << 32) + off_low;
548 TRACE("read id %d handle %d off %qd len %d", id, handle, off, len);
549 if (len > sizeof buf) {
551 log("read change len %d", len);
553 fd = handle_to_fd(handle);
555 if (lseek(fd, off, SEEK_SET) < 0) {
556 error("process_read: seek failed");
557 status = errno_to_portable(errno);
559 ret = read(fd, buf, len);
561 status = errno_to_portable(errno);
562 } else if (ret == 0) {
565 send_data(id, buf, ret);
570 if (status != SSH_FX_OK)
571 send_status(id, status);
577 u_int32_t id, off_high, off_low;
580 int handle, fd, ret, status = SSH_FX_FAILURE;
584 handle = get_handle();
585 off_high = get_int();
587 data = get_string(&len);
589 off = (((u_int64_t) off_high) << 32) + off_low;
590 TRACE("write id %d handle %d off %qd len %d", id, handle, off, len);
591 fd = handle_to_fd(handle);
593 if (lseek(fd, off, SEEK_SET) < 0) {
594 status = errno_to_portable(errno);
595 error("process_write: seek failed");
598 ret = write(fd, data, len);
600 error("process_write: write failed");
601 status = errno_to_portable(errno);
602 } else if (ret == len) {
605 log("nothing at all written");
609 send_status(id, status);
614 process_do_stat(int do_lstat)
620 int ret, status = SSH_FX_FAILURE;
623 name = get_string(NULL);
624 TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name);
625 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
627 status = errno_to_portable(errno);
629 a = stat_to_attrib(&st);
633 if (status != SSH_FX_OK)
634 send_status(id, status);
656 int fd, ret, handle, status = SSH_FX_FAILURE;
659 handle = get_handle();
660 TRACE("fstat id %d handle %d", id, handle);
661 fd = handle_to_fd(handle);
663 ret = fstat(fd, &st);
665 status = errno_to_portable(errno);
667 a = stat_to_attrib(&st);
672 if (status != SSH_FX_OK)
673 send_status(id, status);
677 attrib_to_tv(Attrib *a)
679 static struct timeval tv[2];
680 tv[0].tv_sec = a->atime;
682 tv[1].tv_sec = a->mtime;
688 process_setstat(void)
694 int status = SSH_FX_OK;
697 name = get_string(NULL);
699 TRACE("setstat id %d name %s", id, name);
700 if (a->flags & SSH_FXA_HAVE_PERM) {
701 ret = chmod(name, a->perm & 0777);
703 status = errno_to_portable(errno);
705 if (a->flags & SSH_FXA_HAVE_TIME) {
706 ret = utimes(name, attrib_to_tv(a));
708 status = errno_to_portable(errno);
710 send_status(id, status);
715 process_fsetstat(void)
720 int status = SSH_FX_OK;
723 handle = get_handle();
725 TRACE("fsetstat id %d handle %d", id, handle);
726 fd = handle_to_fd(handle);
728 status = SSH_FX_FAILURE;
730 if (a->flags & SSH_FXA_HAVE_PERM) {
731 ret = fchmod(fd, a->perm & 0777);
733 status = errno_to_portable(errno);
735 if (a->flags & SSH_FXA_HAVE_TIME) {
736 ret = futimes(fd, attrib_to_tv(a));
738 status = errno_to_portable(errno);
741 send_status(id, status);
745 process_opendir(void)
749 int handle, status = SSH_FX_FAILURE;
753 path = get_string(NULL);
754 TRACE("opendir id %d path %s", id, path);
755 dirp = opendir(path);
757 status = errno_to_portable(errno);
759 handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
763 send_handle(id, handle);
768 if (status != SSH_FX_OK)
769 send_status(id, status);
774 ls_file(char *name, struct stat *st)
777 snprintf(buf, sizeof buf, "0%o %d %d %qd %d %s",
778 st->st_mode, st->st_uid, st->st_gid, (long long)st->st_size,(int) st->st_mtime,
784 process_readdir(void)
793 handle = get_handle();
794 TRACE("readdir id %d handle %d", id, handle);
795 dirp = handle_to_dir(handle);
796 path = handle_to_name(handle);
797 if (dirp == NULL || path == NULL) {
798 send_status(id, SSH_FX_FAILURE);
804 int nstats = 10, count = 0, i;
805 stats = xmalloc(nstats * sizeof(Stat));
806 while ((dp = readdir(dirp)) != NULL) {
807 if (count >= nstats) {
809 stats = xrealloc(stats, nstats * sizeof(Stat));
812 snprintf(pathname, sizeof pathname,
813 "%s/%s", path, dp->d_name);
814 if (lstat(pathname, &st) < 0)
816 a = stat_to_attrib(&st);
817 stats[count].attrib = *a;
818 stats[count].name = xstrdup(dp->d_name);
819 stats[count].long_name = ls_file(dp->d_name, &st);
821 /* send up to 100 entries in one message */
825 send_names(id, count, stats);
826 for(i = 0; i < count; i++) {
827 xfree(stats[i].name);
828 xfree(stats[i].long_name);
839 int status = SSH_FX_FAILURE;
843 name = get_string(NULL);
844 TRACE("remove id %d name %s", id, name);
846 status = (ret == -1) ? errno_to_portable(errno) : SSH_FX_OK;
847 send_status(id, status);
857 int ret, mode, status = SSH_FX_FAILURE;
860 name = get_string(NULL);
862 mode = (a->flags & SSH_FXA_HAVE_PERM) ? a->perm & 0777 : 0777;
863 TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
864 ret = mkdir(name, mode);
865 status = (ret == -1) ? errno_to_portable(errno) : SSH_FX_OK;
866 send_status(id, status);
878 name = get_string(NULL);
879 TRACE("rmdir id %d name %s", id, name);
881 status = (ret == -1) ? errno_to_portable(errno) : SSH_FX_OK;
882 send_status(id, status);
887 process_realpath(void)
889 char resolvedname[MAXPATHLEN];
894 path = get_string(NULL);
895 TRACE("realpath id %d path %s", id, path);
896 if (realpath(path, resolvedname) == NULL) {
897 send_status(id, errno_to_portable(errno));
900 attrib_clear(&s.attrib);
901 s.name = s.long_name = resolvedname;
902 send_names(id, 1, &s);
911 char *oldpath, *newpath;
915 oldpath = get_string(NULL);
916 newpath = get_string(NULL);
917 TRACE("rename id %d old %s new %s", id, oldpath, newpath);
918 ret = rename(oldpath, newpath);
919 status = (ret == -1) ? errno_to_portable(errno) : SSH_FX_OK;
920 send_status(id, status);
926 /* stolen from ssh-agent */
931 unsigned int msg_len;
935 if (buffer_len(&iqueue) < 5)
936 return; /* Incomplete message. */
937 cp = (unsigned char *) buffer_ptr(&iqueue);
938 msg_len = GET_32BIT(cp);
939 if (msg_len > 256 * 1024) {
940 error("bad message ");
943 if (buffer_len(&iqueue) < msg_len + 4)
945 buffer_consume(&iqueue, 4);
946 type = buffer_get_char(&iqueue);
969 case SSH_FXP_SETSTAT:
972 case SSH_FXP_FSETSTAT:
975 case SSH_FXP_OPENDIR:
978 case SSH_FXP_READDIR:
990 case SSH_FXP_REALPATH:
1000 error("Unknown message %d", type);
1006 main(int ac, char **av)
1014 in = dup(STDIN_FILENO);
1015 out = dup(STDOUT_FILENO);
1023 buffer_init(&iqueue);
1024 buffer_init(&oqueue);
1031 olen = buffer_len(&oqueue);
1035 if (select(max+1, &rset, &wset, NULL, NULL) < 0) {
1041 /* copy stdin to iqueue */
1042 if (FD_ISSET(in, &rset)) {
1044 len = read(in, buf, sizeof buf);
1048 } else if (len < 0) {
1049 error("read error");
1052 buffer_append(&iqueue, buf, len);
1055 /* send oqueue to stdout */
1056 if (FD_ISSET(out, &wset)) {
1057 len = write(out, buffer_ptr(&oqueue), olen);
1059 error("write error");
1062 buffer_consume(&oqueue, len);
1065 /* process requests from client */