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.25 2001/04/05 10:42:53 markus 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 /* input and output queue */
46 /* Version of client */
49 /* portable attibutes, etc. */
51 typedef struct Stat Stat;
60 errno_to_portable(int unixerrno)
72 ret = SSH2_FX_NO_SUCH_FILE;
77 ret = SSH2_FX_PERMISSION_DENIED;
81 ret = SSH2_FX_BAD_MESSAGE;
84 ret = SSH2_FX_FAILURE;
91 flags_from_portable(int pflags)
95 if ((pflags & SSH2_FXF_READ) &&
96 (pflags & SSH2_FXF_WRITE)) {
98 } else if (pflags & SSH2_FXF_READ) {
100 } else if (pflags & SSH2_FXF_WRITE) {
103 if (pflags & SSH2_FXF_CREAT)
105 if (pflags & SSH2_FXF_TRUNC)
107 if (pflags & SSH2_FXF_EXCL)
115 return decode_attrib(&iqueue);
120 typedef struct Handle Handle;
141 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++)
142 handles[i].use = HANDLE_UNUSED;
146 handle_new(int use, char *name, int fd, DIR *dirp)
150 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
151 if (handles[i].use == HANDLE_UNUSED) {
152 handles[i].use = use;
153 handles[i].dirp = dirp;
155 handles[i].name = name;
163 handle_is_ok(int i, int type)
165 return i >= 0 && i < sizeof(handles)/sizeof(Handle) &&
166 handles[i].use == type;
170 handle_to_string(int handle, char **stringp, int *hlenp)
172 if (stringp == NULL || hlenp == NULL)
174 *stringp = xmalloc(sizeof(int32_t));
175 PUT_32BIT(*stringp, handle);
176 *hlenp = sizeof(int32_t);
181 handle_from_string(char *handle, u_int hlen)
185 if (hlen != sizeof(int32_t))
187 val = GET_32BIT(handle);
188 if (handle_is_ok(val, HANDLE_FILE) ||
189 handle_is_ok(val, HANDLE_DIR))
195 handle_to_name(int handle)
197 if (handle_is_ok(handle, HANDLE_DIR)||
198 handle_is_ok(handle, HANDLE_FILE))
199 return handles[handle].name;
204 handle_to_dir(int handle)
206 if (handle_is_ok(handle, HANDLE_DIR))
207 return handles[handle].dirp;
212 handle_to_fd(int handle)
214 if (handle_is_ok(handle, HANDLE_FILE))
215 return handles[handle].fd;
220 handle_close(int handle)
224 if (handle_is_ok(handle, HANDLE_FILE)) {
225 ret = close(handles[handle].fd);
226 handles[handle].use = HANDLE_UNUSED;
227 } else if (handle_is_ok(handle, HANDLE_DIR)) {
228 ret = closedir(handles[handle].dirp);
229 handles[handle].use = HANDLE_UNUSED;
243 handle = get_string(&hlen);
245 val = handle_from_string(handle, hlen);
255 int mlen = buffer_len(m);
257 buffer_put_int(&oqueue, mlen);
258 buffer_append(&oqueue, buffer_ptr(m), mlen);
259 buffer_consume(m, mlen);
263 send_status(u_int32_t id, u_int32_t error)
266 const char *status_messages[] = {
267 "Success", /* SSH_FX_OK */
268 "End of file", /* SSH_FX_EOF */
269 "No such file", /* SSH_FX_NO_SUCH_FILE */
270 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
271 "Failure", /* SSH_FX_FAILURE */
272 "Bad message", /* SSH_FX_BAD_MESSAGE */
273 "No connection", /* SSH_FX_NO_CONNECTION */
274 "Connection lost", /* SSH_FX_CONNECTION_LOST */
275 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
276 "Unknown error" /* Others */
279 TRACE("sent status id %d error %d", id, error);
281 buffer_put_char(&msg, SSH2_FXP_STATUS);
282 buffer_put_int(&msg, id);
283 buffer_put_int(&msg, error);
285 buffer_put_cstring(&msg,
286 status_messages[MIN(error,SSH2_FX_MAX)]);
287 buffer_put_cstring(&msg, "");
293 send_data_or_handle(char type, u_int32_t id, char *data, int dlen)
298 buffer_put_char(&msg, type);
299 buffer_put_int(&msg, id);
300 buffer_put_string(&msg, data, dlen);
306 send_data(u_int32_t id, char *data, int dlen)
308 TRACE("sent data id %d len %d", id, dlen);
309 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
313 send_handle(u_int32_t id, int handle)
318 handle_to_string(handle, &string, &hlen);
319 TRACE("sent handle id %d handle %d", id, handle);
320 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
325 send_names(u_int32_t id, int count, Stat *stats)
331 buffer_put_char(&msg, SSH2_FXP_NAME);
332 buffer_put_int(&msg, id);
333 buffer_put_int(&msg, count);
334 TRACE("sent names id %d count %d", id, count);
335 for (i = 0; i < count; i++) {
336 buffer_put_cstring(&msg, stats[i].name);
337 buffer_put_cstring(&msg, stats[i].long_name);
338 encode_attrib(&msg, &stats[i].attrib);
345 send_attrib(u_int32_t id, Attrib *a)
349 TRACE("sent attrib id %d have 0x%x", id, a->flags);
351 buffer_put_char(&msg, SSH2_FXP_ATTRS);
352 buffer_put_int(&msg, id);
353 encode_attrib(&msg, a);
365 version = buffer_get_int(&iqueue);
366 TRACE("client version %d", version);
368 buffer_put_char(&msg, SSH2_FXP_VERSION);
369 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
377 u_int32_t id, pflags;
380 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
383 name = get_string(NULL);
384 pflags = get_int(); /* portable flags */
386 flags = flags_from_portable(pflags);
387 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
388 TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode);
389 fd = open(name, flags, mode);
391 status = errno_to_portable(errno);
393 handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL);
397 send_handle(id, handle);
401 if (status != SSH2_FX_OK)
402 send_status(id, status);
410 int handle, ret, status = SSH2_FX_FAILURE;
413 handle = get_handle();
414 TRACE("close id %d handle %d", id, handle);
415 ret = handle_close(handle);
416 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
417 send_status(id, status);
425 int handle, fd, ret, status = SSH2_FX_FAILURE;
429 handle = get_handle();
433 TRACE("read id %d handle %d off %llu len %d", id, handle,
434 (unsigned long long)off, len);
435 if (len > sizeof buf) {
437 log("read change len %d", len);
439 fd = handle_to_fd(handle);
441 if (lseek(fd, off, SEEK_SET) < 0) {
442 error("process_read: seek failed");
443 status = errno_to_portable(errno);
445 ret = read(fd, buf, len);
447 status = errno_to_portable(errno);
448 } else if (ret == 0) {
449 status = SSH2_FX_EOF;
451 send_data(id, buf, ret);
456 if (status != SSH2_FX_OK)
457 send_status(id, status);
466 int handle, fd, ret, status = SSH2_FX_FAILURE;
470 handle = get_handle();
472 data = get_string(&len);
474 TRACE("write id %d handle %d off %llu len %d", id, handle,
475 (unsigned long long)off, len);
476 fd = handle_to_fd(handle);
478 if (lseek(fd, off, SEEK_SET) < 0) {
479 status = errno_to_portable(errno);
480 error("process_write: seek failed");
483 ret = write(fd, data, len);
485 error("process_write: write failed");
486 status = errno_to_portable(errno);
487 } else if (ret == len) {
490 log("nothing at all written");
494 send_status(id, status);
499 process_do_stat(int do_lstat)
505 int ret, status = SSH2_FX_FAILURE;
508 name = get_string(NULL);
509 TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name);
510 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
512 status = errno_to_portable(errno);
514 stat_to_attrib(&st, &a);
518 if (status != SSH2_FX_OK)
519 send_status(id, status);
541 int fd, ret, handle, status = SSH2_FX_FAILURE;
544 handle = get_handle();
545 TRACE("fstat id %d handle %d", id, handle);
546 fd = handle_to_fd(handle);
548 ret = fstat(fd, &st);
550 status = errno_to_portable(errno);
552 stat_to_attrib(&st, &a);
557 if (status != SSH2_FX_OK)
558 send_status(id, status);
562 attrib_to_tv(Attrib *a)
564 static struct timeval tv[2];
566 tv[0].tv_sec = a->atime;
568 tv[1].tv_sec = a->mtime;
574 process_setstat(void)
580 int status = SSH2_FX_OK;
583 name = get_string(NULL);
585 TRACE("setstat id %d name %s", id, name);
586 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
587 ret = chmod(name, a->perm & 0777);
589 status = errno_to_portable(errno);
591 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
592 ret = utimes(name, attrib_to_tv(a));
594 status = errno_to_portable(errno);
596 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
597 ret = chown(name, a->uid, a->gid);
599 status = errno_to_portable(errno);
601 send_status(id, status);
606 process_fsetstat(void)
611 int status = SSH2_FX_OK;
614 handle = get_handle();
616 TRACE("fsetstat id %d handle %d", id, handle);
617 fd = handle_to_fd(handle);
619 status = SSH2_FX_FAILURE;
621 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
622 ret = fchmod(fd, a->perm & 0777);
624 status = errno_to_portable(errno);
626 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
627 ret = futimes(fd, attrib_to_tv(a));
629 status = errno_to_portable(errno);
631 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
632 ret = fchown(fd, 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, (unsigned long long)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_readlink(void)
866 char link[MAXPATHLEN];
870 path = get_string(NULL);
871 TRACE("readlink id %d path %s", id, path);
872 if (readlink(path, link, sizeof(link) - 1) == -1)
873 send_status(id, errno_to_portable(errno));
877 link[sizeof(link) - 1] = '\0';
878 attrib_clear(&s.attrib);
879 s.name = s.long_name = link;
880 send_names(id, 1, &s);
886 process_symlink(void)
890 char *oldpath, *newpath;
891 int ret, status = SSH2_FX_FAILURE;
894 oldpath = get_string(NULL);
895 newpath = get_string(NULL);
896 TRACE("symlink id %d old %s new %s", id, oldpath, newpath);
897 /* fail if 'newpath' exists */
898 if (stat(newpath, &st) == -1) {
899 ret = symlink(oldpath, newpath);
900 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
902 send_status(id, status);
908 process_extended(void)
914 request = get_string(NULL);
915 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
919 /* stolen from ssh-agent */
928 if (buffer_len(&iqueue) < 5)
929 return; /* Incomplete message. */
930 cp = (u_char *) buffer_ptr(&iqueue);
931 msg_len = GET_32BIT(cp);
932 if (msg_len > 256 * 1024) {
933 error("bad message ");
936 if (buffer_len(&iqueue) < msg_len + 4)
938 buffer_consume(&iqueue, 4);
939 type = buffer_get_char(&iqueue);
962 case SSH2_FXP_SETSTAT:
965 case SSH2_FXP_FSETSTAT:
968 case SSH2_FXP_OPENDIR:
971 case SSH2_FXP_READDIR:
974 case SSH2_FXP_REMOVE:
983 case SSH2_FXP_REALPATH:
989 case SSH2_FXP_RENAME:
992 case SSH2_FXP_READLINK:
995 case SSH2_FXP_SYMLINK:
998 case SSH2_FXP_EXTENDED:
1002 error("Unknown message %d", type);
1008 main(int ac, char **av)
1010 fd_set *rset, *wset;
1012 ssize_t len, olen, set_size;
1014 /* XXX should use getopt */
1018 #ifdef DEBUG_SFTP_SERVER
1019 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
1022 in = dup(STDIN_FILENO);
1023 out = dup(STDOUT_FILENO);
1031 buffer_init(&iqueue);
1032 buffer_init(&oqueue);
1034 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1035 rset = (fd_set *)xmalloc(set_size);
1036 wset = (fd_set *)xmalloc(set_size);
1039 memset(rset, 0, set_size);
1040 memset(wset, 0, set_size);
1043 olen = buffer_len(&oqueue);
1047 if (select(max+1, rset, wset, NULL, NULL) < 0) {
1053 /* copy stdin to iqueue */
1054 if (FD_ISSET(in, rset)) {
1056 len = read(in, buf, sizeof buf);
1060 } else if (len < 0) {
1061 error("read error");
1064 buffer_append(&iqueue, buf, len);
1067 /* send oqueue to stdout */
1068 if (FD_ISSET(out, wset)) {
1069 len = write(out, buffer_ptr(&oqueue), olen);
1071 error("write error");
1074 buffer_consume(&oqueue, len);
1077 /* process requests from client */