1 /* $OpenBSD: sftp-server.c,v 1.109 2016/02/15 09:47:49 dtucker Exp $ */
3 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/param.h> /* MIN */
21 #include <sys/types.h>
23 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
26 #ifdef HAVE_SYS_MOUNT_H
27 #include <sys/mount.h>
29 #ifdef HAVE_SYS_STATVFS_H
30 #include <sys/statvfs.h>
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
56 #include "sftp-common.h"
59 static LogLevel log_level = SYSLOG_LEVEL_ERROR;
62 static struct passwd *pw = NULL;
63 static char *client_addr = NULL;
65 /* input and output queue */
66 struct sshbuf *iqueue;
67 struct sshbuf *oqueue;
69 /* Version of client */
72 /* SSH2_FXP_INIT received */
78 /* Requests that are allowed/denied */
79 static char *request_whitelist, *request_blacklist;
81 /* portable attributes, etc. */
82 typedef struct Stat Stat;
91 static void process_open(u_int32_t id);
92 static void process_close(u_int32_t id);
93 static void process_read(u_int32_t id);
94 static void process_write(u_int32_t id);
95 static void process_stat(u_int32_t id);
96 static void process_lstat(u_int32_t id);
97 static void process_fstat(u_int32_t id);
98 static void process_setstat(u_int32_t id);
99 static void process_fsetstat(u_int32_t id);
100 static void process_opendir(u_int32_t id);
101 static void process_readdir(u_int32_t id);
102 static void process_remove(u_int32_t id);
103 static void process_mkdir(u_int32_t id);
104 static void process_rmdir(u_int32_t id);
105 static void process_realpath(u_int32_t id);
106 static void process_rename(u_int32_t id);
107 static void process_readlink(u_int32_t id);
108 static void process_symlink(u_int32_t id);
109 static void process_extended_posix_rename(u_int32_t id);
110 static void process_extended_statvfs(u_int32_t id);
111 static void process_extended_fstatvfs(u_int32_t id);
112 static void process_extended_hardlink(u_int32_t id);
113 static void process_extended_fsync(u_int32_t id);
114 static void process_extended(u_int32_t id);
116 struct sftp_handler {
117 const char *name; /* user-visible name for fine-grained perms */
118 const char *ext_name; /* extended request name */
119 u_int type; /* packet type, for non extended packets */
120 void (*handler)(u_int32_t);
121 int does_write; /* if nonzero, banned for readonly mode */
124 struct sftp_handler handlers[] = {
125 /* NB. SSH2_FXP_OPEN does the readonly check in the handler itself */
126 { "open", NULL, SSH2_FXP_OPEN, process_open, 0 },
127 { "close", NULL, SSH2_FXP_CLOSE, process_close, 0 },
128 { "read", NULL, SSH2_FXP_READ, process_read, 0 },
129 { "write", NULL, SSH2_FXP_WRITE, process_write, 1 },
130 { "lstat", NULL, SSH2_FXP_LSTAT, process_lstat, 0 },
131 { "fstat", NULL, SSH2_FXP_FSTAT, process_fstat, 0 },
132 { "setstat", NULL, SSH2_FXP_SETSTAT, process_setstat, 1 },
133 { "fsetstat", NULL, SSH2_FXP_FSETSTAT, process_fsetstat, 1 },
134 { "opendir", NULL, SSH2_FXP_OPENDIR, process_opendir, 0 },
135 { "readdir", NULL, SSH2_FXP_READDIR, process_readdir, 0 },
136 { "remove", NULL, SSH2_FXP_REMOVE, process_remove, 1 },
137 { "mkdir", NULL, SSH2_FXP_MKDIR, process_mkdir, 1 },
138 { "rmdir", NULL, SSH2_FXP_RMDIR, process_rmdir, 1 },
139 { "realpath", NULL, SSH2_FXP_REALPATH, process_realpath, 0 },
140 { "stat", NULL, SSH2_FXP_STAT, process_stat, 0 },
141 { "rename", NULL, SSH2_FXP_RENAME, process_rename, 1 },
142 { "readlink", NULL, SSH2_FXP_READLINK, process_readlink, 0 },
143 { "symlink", NULL, SSH2_FXP_SYMLINK, process_symlink, 1 },
144 { NULL, NULL, 0, NULL, 0 }
147 /* SSH2_FXP_EXTENDED submessages */
148 struct sftp_handler extended_handlers[] = {
149 { "posix-rename", "posix-rename@openssh.com", 0,
150 process_extended_posix_rename, 1 },
151 { "statvfs", "statvfs@openssh.com", 0, process_extended_statvfs, 0 },
152 { "fstatvfs", "fstatvfs@openssh.com", 0, process_extended_fstatvfs, 0 },
153 { "hardlink", "hardlink@openssh.com", 0, process_extended_hardlink, 1 },
154 { "fsync", "fsync@openssh.com", 0, process_extended_fsync, 1 },
155 { NULL, NULL, 0, NULL, 0 }
159 request_permitted(struct sftp_handler *h)
163 if (readonly && h->does_write) {
164 verbose("Refusing %s request in read-only mode", h->name);
167 if (request_blacklist != NULL &&
168 ((result = match_list(h->name, request_blacklist, NULL))) != NULL) {
170 verbose("Refusing blacklisted %s request", h->name);
173 if (request_whitelist != NULL &&
174 ((result = match_list(h->name, request_whitelist, NULL))) != NULL) {
176 debug2("Permitting whitelisted %s request", h->name);
179 if (request_whitelist != NULL) {
180 verbose("Refusing non-whitelisted %s request", h->name);
187 errno_to_portable(int unixerrno)
199 ret = SSH2_FX_NO_SUCH_FILE;
204 ret = SSH2_FX_PERMISSION_DENIED;
208 ret = SSH2_FX_BAD_MESSAGE;
211 ret = SSH2_FX_OP_UNSUPPORTED;
214 ret = SSH2_FX_FAILURE;
221 flags_from_portable(int pflags)
225 if ((pflags & SSH2_FXF_READ) &&
226 (pflags & SSH2_FXF_WRITE)) {
228 } else if (pflags & SSH2_FXF_READ) {
230 } else if (pflags & SSH2_FXF_WRITE) {
233 if (pflags & SSH2_FXF_APPEND)
235 if (pflags & SSH2_FXF_CREAT)
237 if (pflags & SSH2_FXF_TRUNC)
239 if (pflags & SSH2_FXF_EXCL)
245 string_from_portable(int pflags)
247 static char ret[128];
251 #define PAPPEND(str) { \
253 strlcat(ret, ",", sizeof(ret)); \
254 strlcat(ret, str, sizeof(ret)); \
257 if (pflags & SSH2_FXF_READ)
259 if (pflags & SSH2_FXF_WRITE)
261 if (pflags & SSH2_FXF_APPEND)
263 if (pflags & SSH2_FXF_CREAT)
265 if (pflags & SSH2_FXF_TRUNC)
267 if (pflags & SSH2_FXF_EXCL)
275 typedef struct Handle Handle;
282 u_int64_t bytes_read, bytes_write;
292 Handle *handles = NULL;
293 u_int num_handles = 0;
294 int first_unused_handle = -1;
296 static void handle_unused(int i)
298 handles[i].use = HANDLE_UNUSED;
299 handles[i].next_unused = first_unused_handle;
300 first_unused_handle = i;
304 handle_new(int use, const char *name, int fd, int flags, DIR *dirp)
308 if (first_unused_handle == -1) {
309 if (num_handles + 1 <= num_handles)
312 handles = xreallocarray(handles, num_handles, sizeof(Handle));
313 handle_unused(num_handles - 1);
316 i = first_unused_handle;
317 first_unused_handle = handles[i].next_unused;
319 handles[i].use = use;
320 handles[i].dirp = dirp;
322 handles[i].flags = flags;
323 handles[i].name = xstrdup(name);
324 handles[i].bytes_read = handles[i].bytes_write = 0;
330 handle_is_ok(int i, int type)
332 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
336 handle_to_string(int handle, u_char **stringp, int *hlenp)
338 if (stringp == NULL || hlenp == NULL)
340 *stringp = xmalloc(sizeof(int32_t));
341 put_u32(*stringp, handle);
342 *hlenp = sizeof(int32_t);
347 handle_from_string(const u_char *handle, u_int hlen)
351 if (hlen != sizeof(int32_t))
353 val = get_u32(handle);
354 if (handle_is_ok(val, HANDLE_FILE) ||
355 handle_is_ok(val, HANDLE_DIR))
361 handle_to_name(int handle)
363 if (handle_is_ok(handle, HANDLE_DIR)||
364 handle_is_ok(handle, HANDLE_FILE))
365 return handles[handle].name;
370 handle_to_dir(int handle)
372 if (handle_is_ok(handle, HANDLE_DIR))
373 return handles[handle].dirp;
378 handle_to_fd(int handle)
380 if (handle_is_ok(handle, HANDLE_FILE))
381 return handles[handle].fd;
386 handle_to_flags(int handle)
388 if (handle_is_ok(handle, HANDLE_FILE))
389 return handles[handle].flags;
394 handle_update_read(int handle, ssize_t bytes)
396 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
397 handles[handle].bytes_read += bytes;
401 handle_update_write(int handle, ssize_t bytes)
403 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
404 handles[handle].bytes_write += bytes;
408 handle_bytes_read(int handle)
410 if (handle_is_ok(handle, HANDLE_FILE))
411 return (handles[handle].bytes_read);
416 handle_bytes_write(int handle)
418 if (handle_is_ok(handle, HANDLE_FILE))
419 return (handles[handle].bytes_write);
424 handle_close(int handle)
428 if (handle_is_ok(handle, HANDLE_FILE)) {
429 ret = close(handles[handle].fd);
430 free(handles[handle].name);
431 handle_unused(handle);
432 } else if (handle_is_ok(handle, HANDLE_DIR)) {
433 ret = closedir(handles[handle].dirp);
434 free(handles[handle].name);
435 handle_unused(handle);
443 handle_log_close(int handle, char *emsg)
445 if (handle_is_ok(handle, HANDLE_FILE)) {
446 logit("%s%sclose \"%s\" bytes read %llu written %llu",
447 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
448 handle_to_name(handle),
449 (unsigned long long)handle_bytes_read(handle),
450 (unsigned long long)handle_bytes_write(handle));
452 logit("%s%sclosedir \"%s\"",
453 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
454 handle_to_name(handle));
459 handle_log_exit(void)
463 for (i = 0; i < num_handles; i++)
464 if (handles[i].use != HANDLE_UNUSED)
465 handle_log_close(i, "forced");
469 get_handle(struct sshbuf *queue, int *hp)
476 if ((r = sshbuf_get_string(queue, &handle, &hlen)) != 0)
479 *hp = handle_from_string(handle, hlen);
487 send_msg(struct sshbuf *m)
491 if ((r = sshbuf_put_stringb(oqueue, m)) != 0)
492 fatal("%s: buffer error: %s", __func__, ssh_err(r));
497 status_to_message(u_int32_t status)
499 const char *status_messages[] = {
500 "Success", /* SSH_FX_OK */
501 "End of file", /* SSH_FX_EOF */
502 "No such file", /* SSH_FX_NO_SUCH_FILE */
503 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
504 "Failure", /* SSH_FX_FAILURE */
505 "Bad message", /* SSH_FX_BAD_MESSAGE */
506 "No connection", /* SSH_FX_NO_CONNECTION */
507 "Connection lost", /* SSH_FX_CONNECTION_LOST */
508 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
509 "Unknown error" /* Others */
511 return (status_messages[MIN(status,SSH2_FX_MAX)]);
515 send_status(u_int32_t id, u_int32_t status)
520 debug3("request %u: sent status %u", id, status);
521 if (log_level > SYSLOG_LEVEL_VERBOSE ||
522 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
523 logit("sent status %s", status_to_message(status));
524 if ((msg = sshbuf_new()) == NULL)
525 fatal("%s: sshbuf_new failed", __func__);
526 if ((r = sshbuf_put_u8(msg, SSH2_FXP_STATUS)) != 0 ||
527 (r = sshbuf_put_u32(msg, id)) != 0 ||
528 (r = sshbuf_put_u32(msg, status)) != 0)
529 fatal("%s: buffer error: %s", __func__, ssh_err(r));
531 if ((r = sshbuf_put_cstring(msg,
532 status_to_message(status))) != 0 ||
533 (r = sshbuf_put_cstring(msg, "")) != 0)
534 fatal("%s: buffer error: %s", __func__, ssh_err(r));
540 send_data_or_handle(char type, u_int32_t id, const u_char *data, int dlen)
545 if ((msg = sshbuf_new()) == NULL)
546 fatal("%s: sshbuf_new failed", __func__);
547 if ((r = sshbuf_put_u8(msg, type)) != 0 ||
548 (r = sshbuf_put_u32(msg, id)) != 0 ||
549 (r = sshbuf_put_string(msg, data, dlen)) != 0)
550 fatal("%s: buffer error: %s", __func__, ssh_err(r));
556 send_data(u_int32_t id, const u_char *data, int dlen)
558 debug("request %u: sent data len %d", id, dlen);
559 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
563 send_handle(u_int32_t id, int handle)
568 handle_to_string(handle, &string, &hlen);
569 debug("request %u: sent handle handle %d", id, handle);
570 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
575 send_names(u_int32_t id, int count, const Stat *stats)
580 if ((msg = sshbuf_new()) == NULL)
581 fatal("%s: sshbuf_new failed", __func__);
582 if ((r = sshbuf_put_u8(msg, SSH2_FXP_NAME)) != 0 ||
583 (r = sshbuf_put_u32(msg, id)) != 0 ||
584 (r = sshbuf_put_u32(msg, count)) != 0)
585 fatal("%s: buffer error: %s", __func__, ssh_err(r));
586 debug("request %u: sent names count %d", id, count);
587 for (i = 0; i < count; i++) {
588 if ((r = sshbuf_put_cstring(msg, stats[i].name)) != 0 ||
589 (r = sshbuf_put_cstring(msg, stats[i].long_name)) != 0 ||
590 (r = encode_attrib(msg, &stats[i].attrib)) != 0)
591 fatal("%s: buffer error: %s", __func__, ssh_err(r));
598 send_attrib(u_int32_t id, const Attrib *a)
603 debug("request %u: sent attrib have 0x%x", id, a->flags);
604 if ((msg = sshbuf_new()) == NULL)
605 fatal("%s: sshbuf_new failed", __func__);
606 if ((r = sshbuf_put_u8(msg, SSH2_FXP_ATTRS)) != 0 ||
607 (r = sshbuf_put_u32(msg, id)) != 0 ||
608 (r = encode_attrib(msg, a)) != 0)
609 fatal("%s: buffer error: %s", __func__, ssh_err(r));
615 send_statvfs(u_int32_t id, struct statvfs *st)
621 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
622 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
624 if ((msg = sshbuf_new()) == NULL)
625 fatal("%s: sshbuf_new failed", __func__);
626 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED_REPLY)) != 0 ||
627 (r = sshbuf_put_u32(msg, id)) != 0 ||
628 (r = sshbuf_put_u64(msg, st->f_bsize)) != 0 ||
629 (r = sshbuf_put_u64(msg, st->f_frsize)) != 0 ||
630 (r = sshbuf_put_u64(msg, st->f_blocks)) != 0 ||
631 (r = sshbuf_put_u64(msg, st->f_bfree)) != 0 ||
632 (r = sshbuf_put_u64(msg, st->f_bavail)) != 0 ||
633 (r = sshbuf_put_u64(msg, st->f_files)) != 0 ||
634 (r = sshbuf_put_u64(msg, st->f_ffree)) != 0 ||
635 (r = sshbuf_put_u64(msg, st->f_favail)) != 0 ||
636 (r = sshbuf_put_u64(msg, FSID_TO_ULONG(st->f_fsid))) != 0 ||
637 (r = sshbuf_put_u64(msg, flag)) != 0 ||
638 (r = sshbuf_put_u64(msg, st->f_namemax)) != 0)
639 fatal("%s: buffer error: %s", __func__, ssh_err(r));
652 if ((r = sshbuf_get_u32(iqueue, &version)) != 0)
653 fatal("%s: buffer error: %s", __func__, ssh_err(r));
654 verbose("received client version %u", version);
655 if ((msg = sshbuf_new()) == NULL)
656 fatal("%s: sshbuf_new failed", __func__);
657 if ((r = sshbuf_put_u8(msg, SSH2_FXP_VERSION)) != 0 ||
658 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0 ||
659 /* POSIX rename extension */
660 (r = sshbuf_put_cstring(msg, "posix-rename@openssh.com")) != 0 ||
661 (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
662 /* statvfs extension */
663 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
664 (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
665 /* fstatvfs extension */
666 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
667 (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
668 /* hardlink extension */
669 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
670 (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
671 /* fsync extension */
672 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
673 (r = sshbuf_put_cstring(msg, "1")) != 0) /* version */
674 fatal("%s: buffer error: %s", __func__, ssh_err(r));
680 process_open(u_int32_t id)
685 int r, handle, fd, flags, mode, status = SSH2_FX_FAILURE;
687 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
688 (r = sshbuf_get_u32(iqueue, &pflags)) != 0 || /* portable flags */
689 (r = decode_attrib(iqueue, &a)) != 0)
690 fatal("%s: buffer error: %s", __func__, ssh_err(r));
692 debug3("request %u: open flags %d", id, pflags);
693 flags = flags_from_portable(pflags);
694 mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a.perm : 0666;
695 logit("open \"%s\" flags %s mode 0%o",
696 name, string_from_portable(pflags), mode);
698 ((flags & O_ACCMODE) == O_WRONLY ||
699 (flags & O_ACCMODE) == O_RDWR)) {
700 verbose("Refusing open request in read-only mode");
701 status = SSH2_FX_PERMISSION_DENIED;
703 fd = open(name, flags, mode);
705 status = errno_to_portable(errno);
707 handle = handle_new(HANDLE_FILE, name, fd, flags, NULL);
711 send_handle(id, handle);
716 if (status != SSH2_FX_OK)
717 send_status(id, status);
722 process_close(u_int32_t id)
724 int r, handle, ret, status = SSH2_FX_FAILURE;
726 if ((r = get_handle(iqueue, &handle)) != 0)
727 fatal("%s: buffer error: %s", __func__, ssh_err(r));
729 debug3("request %u: close handle %u", id, handle);
730 handle_log_close(handle, NULL);
731 ret = handle_close(handle);
732 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
733 send_status(id, status);
737 process_read(u_int32_t id)
741 int r, handle, fd, ret, status = SSH2_FX_FAILURE;
744 if ((r = get_handle(iqueue, &handle)) != 0 ||
745 (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
746 (r = sshbuf_get_u32(iqueue, &len)) != 0)
747 fatal("%s: buffer error: %s", __func__, ssh_err(r));
749 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
750 id, handle_to_name(handle), handle, (unsigned long long)off, len);
751 if (len > sizeof buf) {
753 debug2("read change len %d", len);
755 fd = handle_to_fd(handle);
757 if (lseek(fd, off, SEEK_SET) < 0) {
758 error("process_read: seek failed");
759 status = errno_to_portable(errno);
761 ret = read(fd, buf, len);
763 status = errno_to_portable(errno);
764 } else if (ret == 0) {
765 status = SSH2_FX_EOF;
767 send_data(id, buf, ret);
769 handle_update_read(handle, ret);
773 if (status != SSH2_FX_OK)
774 send_status(id, status);
778 process_write(u_int32_t id)
782 int r, handle, fd, ret, status;
785 if ((r = get_handle(iqueue, &handle)) != 0 ||
786 (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
787 (r = sshbuf_get_string(iqueue, &data, &len)) != 0)
788 fatal("%s: buffer error: %s", __func__, ssh_err(r));
790 debug("request %u: write \"%s\" (handle %d) off %llu len %zu",
791 id, handle_to_name(handle), handle, (unsigned long long)off, len);
792 fd = handle_to_fd(handle);
795 status = SSH2_FX_FAILURE;
797 if (!(handle_to_flags(handle) & O_APPEND) &&
798 lseek(fd, off, SEEK_SET) < 0) {
799 status = errno_to_portable(errno);
800 error("process_write: seek failed");
803 ret = write(fd, data, len);
805 error("process_write: write failed");
806 status = errno_to_portable(errno);
807 } else if ((size_t)ret == len) {
809 handle_update_write(handle, ret);
811 debug2("nothing at all written");
812 status = SSH2_FX_FAILURE;
816 send_status(id, status);
821 process_do_stat(u_int32_t id, int do_lstat)
826 int r, status = SSH2_FX_FAILURE;
828 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
829 fatal("%s: buffer error: %s", __func__, ssh_err(r));
831 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
832 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
833 r = do_lstat ? lstat(name, &st) : stat(name, &st);
835 status = errno_to_portable(errno);
837 stat_to_attrib(&st, &a);
841 if (status != SSH2_FX_OK)
842 send_status(id, status);
847 process_stat(u_int32_t id)
849 process_do_stat(id, 0);
853 process_lstat(u_int32_t id)
855 process_do_stat(id, 1);
859 process_fstat(u_int32_t id)
863 int fd, r, handle, status = SSH2_FX_FAILURE;
865 if ((r = get_handle(iqueue, &handle)) != 0)
866 fatal("%s: buffer error: %s", __func__, ssh_err(r));
867 debug("request %u: fstat \"%s\" (handle %u)",
868 id, handle_to_name(handle), handle);
869 fd = handle_to_fd(handle);
873 status = errno_to_portable(errno);
875 stat_to_attrib(&st, &a);
880 if (status != SSH2_FX_OK)
881 send_status(id, status);
884 static struct timeval *
885 attrib_to_tv(const Attrib *a)
887 static struct timeval tv[2];
889 tv[0].tv_sec = a->atime;
891 tv[1].tv_sec = a->mtime;
897 process_setstat(u_int32_t id)
901 int r, status = SSH2_FX_OK;
903 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
904 (r = decode_attrib(iqueue, &a)) != 0)
905 fatal("%s: buffer error: %s", __func__, ssh_err(r));
907 debug("request %u: setstat name \"%s\"", id, name);
908 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
909 logit("set \"%s\" size %llu",
910 name, (unsigned long long)a.size);
911 r = truncate(name, a.size);
913 status = errno_to_portable(errno);
915 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
916 logit("set \"%s\" mode %04o", name, a.perm);
917 r = chmod(name, a.perm & 07777);
919 status = errno_to_portable(errno);
921 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
925 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
927 logit("set \"%s\" modtime %s", name, buf);
928 r = utimes(name, attrib_to_tv(&a));
930 status = errno_to_portable(errno);
932 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
933 logit("set \"%s\" owner %lu group %lu", name,
934 (u_long)a.uid, (u_long)a.gid);
935 r = chown(name, a.uid, a.gid);
937 status = errno_to_portable(errno);
939 send_status(id, status);
944 process_fsetstat(u_int32_t id)
948 int status = SSH2_FX_OK;
950 if ((r = get_handle(iqueue, &handle)) != 0 ||
951 (r = decode_attrib(iqueue, &a)) != 0)
952 fatal("%s: buffer error: %s", __func__, ssh_err(r));
954 debug("request %u: fsetstat handle %d", id, handle);
955 fd = handle_to_fd(handle);
957 status = SSH2_FX_FAILURE;
959 char *name = handle_to_name(handle);
961 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
962 logit("set \"%s\" size %llu",
963 name, (unsigned long long)a.size);
964 r = ftruncate(fd, a.size);
966 status = errno_to_portable(errno);
968 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
969 logit("set \"%s\" mode %04o", name, a.perm);
971 r = fchmod(fd, a.perm & 07777);
973 r = chmod(name, a.perm & 07777);
976 status = errno_to_portable(errno);
978 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
982 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
984 logit("set \"%s\" modtime %s", name, buf);
986 r = futimes(fd, attrib_to_tv(&a));
988 r = utimes(name, attrib_to_tv(&a));
991 status = errno_to_portable(errno);
993 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
994 logit("set \"%s\" owner %lu group %lu", name,
995 (u_long)a.uid, (u_long)a.gid);
997 r = fchown(fd, a.uid, a.gid);
999 r = chown(name, a.uid, a.gid);
1002 status = errno_to_portable(errno);
1005 send_status(id, status);
1009 process_opendir(u_int32_t id)
1013 int r, handle, status = SSH2_FX_FAILURE;
1015 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1016 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1018 debug3("request %u: opendir", id);
1019 logit("opendir \"%s\"", path);
1020 dirp = opendir(path);
1022 status = errno_to_portable(errno);
1024 handle = handle_new(HANDLE_DIR, path, 0, 0, dirp);
1028 send_handle(id, handle);
1029 status = SSH2_FX_OK;
1033 if (status != SSH2_FX_OK)
1034 send_status(id, status);
1039 process_readdir(u_int32_t id)
1046 if ((r = get_handle(iqueue, &handle)) != 0)
1047 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1049 debug("request %u: readdir \"%s\" (handle %d)", id,
1050 handle_to_name(handle), handle);
1051 dirp = handle_to_dir(handle);
1052 path = handle_to_name(handle);
1053 if (dirp == NULL || path == NULL) {
1054 send_status(id, SSH2_FX_FAILURE);
1057 char pathname[PATH_MAX];
1059 int nstats = 10, count = 0, i;
1061 stats = xcalloc(nstats, sizeof(Stat));
1062 while ((dp = readdir(dirp)) != NULL) {
1063 if (count >= nstats) {
1065 stats = xreallocarray(stats, nstats, sizeof(Stat));
1067 /* XXX OVERFLOW ? */
1068 snprintf(pathname, sizeof pathname, "%s%s%s", path,
1069 strcmp(path, "/") ? "/" : "", dp->d_name);
1070 if (lstat(pathname, &st) < 0)
1072 stat_to_attrib(&st, &(stats[count].attrib));
1073 stats[count].name = xstrdup(dp->d_name);
1074 stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
1076 /* send up to 100 entries in one message */
1077 /* XXX check packet size instead */
1082 send_names(id, count, stats);
1083 for (i = 0; i < count; i++) {
1084 free(stats[i].name);
1085 free(stats[i].long_name);
1088 send_status(id, SSH2_FX_EOF);
1095 process_remove(u_int32_t id)
1098 int r, status = SSH2_FX_FAILURE;
1100 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1101 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1103 debug3("request %u: remove", id);
1104 logit("remove name \"%s\"", name);
1106 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1107 send_status(id, status);
1112 process_mkdir(u_int32_t id)
1116 int r, mode, status = SSH2_FX_FAILURE;
1118 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
1119 (r = decode_attrib(iqueue, &a)) != 0)
1120 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1122 mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
1123 a.perm & 07777 : 0777;
1124 debug3("request %u: mkdir", id);
1125 logit("mkdir name \"%s\" mode 0%o", name, mode);
1126 r = mkdir(name, mode);
1127 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1128 send_status(id, status);
1133 process_rmdir(u_int32_t id)
1138 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1139 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1141 debug3("request %u: rmdir", id);
1142 logit("rmdir name \"%s\"", name);
1144 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1145 send_status(id, status);
1150 process_realpath(u_int32_t id)
1152 char resolvedname[PATH_MAX];
1156 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1157 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1159 if (path[0] == '\0') {
1161 path = xstrdup(".");
1163 debug3("request %u: realpath", id);
1164 verbose("realpath \"%s\"", path);
1165 if (realpath(path, resolvedname) == NULL) {
1166 send_status(id, errno_to_portable(errno));
1169 attrib_clear(&s.attrib);
1170 s.name = s.long_name = resolvedname;
1171 send_names(id, 1, &s);
1177 process_rename(u_int32_t id)
1179 char *oldpath, *newpath;
1183 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1184 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1185 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1187 debug3("request %u: rename", id);
1188 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
1189 status = SSH2_FX_FAILURE;
1190 if (lstat(oldpath, &sb) == -1)
1191 status = errno_to_portable(errno);
1192 else if (S_ISREG(sb.st_mode)) {
1193 /* Race-free rename of regular files */
1194 if (link(oldpath, newpath) == -1) {
1195 if (errno == EOPNOTSUPP || errno == ENOSYS
1199 #ifdef LINK_OPNOTSUPP_ERRNO
1200 || errno == LINK_OPNOTSUPP_ERRNO
1206 * fs doesn't support links, so fall back to
1207 * stat+rename. This is racy.
1209 if (stat(newpath, &st) == -1) {
1210 if (rename(oldpath, newpath) == -1)
1212 errno_to_portable(errno);
1214 status = SSH2_FX_OK;
1217 status = errno_to_portable(errno);
1219 } else if (unlink(oldpath) == -1) {
1220 status = errno_to_portable(errno);
1221 /* clean spare link */
1224 status = SSH2_FX_OK;
1225 } else if (stat(newpath, &sb) == -1) {
1226 if (rename(oldpath, newpath) == -1)
1227 status = errno_to_portable(errno);
1229 status = SSH2_FX_OK;
1231 send_status(id, status);
1237 process_readlink(u_int32_t id)
1243 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1244 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1246 debug3("request %u: readlink", id);
1247 verbose("readlink \"%s\"", path);
1248 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
1249 send_status(id, errno_to_portable(errno));
1254 attrib_clear(&s.attrib);
1255 s.name = s.long_name = buf;
1256 send_names(id, 1, &s);
1262 process_symlink(u_int32_t id)
1264 char *oldpath, *newpath;
1267 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1268 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1269 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1271 debug3("request %u: symlink", id);
1272 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
1273 /* this will fail if 'newpath' exists */
1274 r = symlink(oldpath, newpath);
1275 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1276 send_status(id, status);
1282 process_extended_posix_rename(u_int32_t id)
1284 char *oldpath, *newpath;
1287 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1288 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1289 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1291 debug3("request %u: posix-rename", id);
1292 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
1293 r = rename(oldpath, newpath);
1294 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1295 send_status(id, status);
1301 process_extended_statvfs(u_int32_t id)
1307 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1308 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1309 debug3("request %u: statvfs", id);
1310 logit("statvfs \"%s\"", path);
1312 if (statvfs(path, &st) != 0)
1313 send_status(id, errno_to_portable(errno));
1315 send_statvfs(id, &st);
1320 process_extended_fstatvfs(u_int32_t id)
1325 if ((r = get_handle(iqueue, &handle)) != 0)
1326 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1327 debug("request %u: fstatvfs \"%s\" (handle %u)",
1328 id, handle_to_name(handle), handle);
1329 if ((fd = handle_to_fd(handle)) < 0) {
1330 send_status(id, SSH2_FX_FAILURE);
1333 if (fstatvfs(fd, &st) != 0)
1334 send_status(id, errno_to_portable(errno));
1336 send_statvfs(id, &st);
1340 process_extended_hardlink(u_int32_t id)
1342 char *oldpath, *newpath;
1345 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1346 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1347 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1349 debug3("request %u: hardlink", id);
1350 logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
1351 r = link(oldpath, newpath);
1352 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1353 send_status(id, status);
1359 process_extended_fsync(u_int32_t id)
1361 int handle, fd, r, status = SSH2_FX_OP_UNSUPPORTED;
1363 if ((r = get_handle(iqueue, &handle)) != 0)
1364 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1365 debug3("request %u: fsync (handle %u)", id, handle);
1366 verbose("fsync \"%s\"", handle_to_name(handle));
1367 if ((fd = handle_to_fd(handle)) < 0)
1368 status = SSH2_FX_NO_SUCH_FILE;
1369 else if (handle_is_ok(handle, HANDLE_FILE)) {
1371 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1373 send_status(id, status);
1377 process_extended(u_int32_t id)
1382 if ((r = sshbuf_get_cstring(iqueue, &request, NULL)) != 0)
1383 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1384 for (i = 0; extended_handlers[i].handler != NULL; i++) {
1385 if (strcmp(request, extended_handlers[i].ext_name) == 0) {
1386 if (!request_permitted(&extended_handlers[i]))
1387 send_status(id, SSH2_FX_PERMISSION_DENIED);
1389 extended_handlers[i].handler(id);
1393 if (extended_handlers[i].handler == NULL) {
1394 error("Unknown extended request \"%.100s\"", request);
1395 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1400 /* stolen from ssh-agent */
1413 buf_len = sshbuf_len(iqueue);
1415 return; /* Incomplete message. */
1416 cp = sshbuf_ptr(iqueue);
1417 msg_len = get_u32(cp);
1418 if (msg_len > SFTP_MAX_MSG_LENGTH) {
1419 error("bad message from %s local user %s",
1420 client_addr, pw->pw_name);
1421 sftp_server_cleanup_exit(11);
1423 if (buf_len < msg_len + 4)
1425 if ((r = sshbuf_consume(iqueue, 4)) != 0)
1426 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1428 if ((r = sshbuf_get_u8(iqueue, &type)) != 0)
1429 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1436 case SSH2_FXP_EXTENDED:
1438 fatal("Received extended request before init");
1439 if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1440 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1441 process_extended(id);
1445 fatal("Received %u request before init", type);
1446 if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1447 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1448 for (i = 0; handlers[i].handler != NULL; i++) {
1449 if (type == handlers[i].type) {
1450 if (!request_permitted(&handlers[i])) {
1452 SSH2_FX_PERMISSION_DENIED);
1454 handlers[i].handler(id);
1459 if (handlers[i].handler == NULL)
1460 error("Unknown message %u", type);
1462 /* discard the remaining bytes from the current packet */
1463 if (buf_len < sshbuf_len(iqueue)) {
1464 error("iqueue grew unexpectedly");
1465 sftp_server_cleanup_exit(255);
1467 consumed = buf_len - sshbuf_len(iqueue);
1468 if (msg_len < consumed) {
1469 error("msg_len %u < consumed %u", msg_len, consumed);
1470 sftp_server_cleanup_exit(255);
1472 if (msg_len > consumed &&
1473 (r = sshbuf_consume(iqueue, msg_len - consumed)) != 0)
1474 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1477 /* Cleanup handler that logs active handles upon normal exit */
1479 sftp_server_cleanup_exit(int i)
1481 if (pw != NULL && client_addr != NULL) {
1483 logit("session closed for local user %s from [%s]",
1484 pw->pw_name, client_addr);
1490 sftp_server_usage(void)
1492 extern char *__progname;
1495 "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
1496 "[-l log_level]\n\t[-P blacklisted_requests] "
1497 "[-p whitelisted_requests] [-u umask]\n"
1498 " %s -Q protocol_feature\n",
1499 __progname, __progname);
1504 sftp_server_main(int argc, char **argv, struct passwd *user_pw)
1506 fd_set *rset, *wset;
1507 int i, r, in, out, max, ch, skipargs = 0, log_stderr = 0;
1508 ssize_t len, olen, set_size;
1509 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1510 char *cp, *homedir = NULL, buf[4*4096];
1513 extern char *optarg;
1514 extern char *__progname;
1516 ssh_malloc_init(); /* must be called before any mallocs */
1517 __progname = ssh_get_progname(argv[0]);
1518 log_init(__progname, log_level, log_facility, log_stderr);
1520 pw = pwcopy(user_pw);
1522 while (!skipargs && (ch = getopt(argc, argv,
1523 "d:f:l:P:p:Q:u:cehR")) != -1) {
1526 if (strcasecmp(optarg, "requests") != 0) {
1527 fprintf(stderr, "Invalid query type\n");
1530 for (i = 0; handlers[i].handler != NULL; i++)
1531 printf("%s\n", handlers[i].name);
1532 for (i = 0; extended_handlers[i].handler != NULL; i++)
1533 printf("%s\n", extended_handlers[i].name);
1541 * Ignore all arguments if we are invoked as a
1542 * shell using "sftp-server -c command"
1550 log_level = log_level_number(optarg);
1551 if (log_level == SYSLOG_LEVEL_NOT_SET)
1552 error("Invalid log level \"%s\"", optarg);
1555 log_facility = log_facility_number(optarg);
1556 if (log_facility == SYSLOG_FACILITY_NOT_SET)
1557 error("Invalid log facility \"%s\"", optarg);
1560 cp = tilde_expand_filename(optarg, user_pw->pw_uid);
1561 homedir = percent_expand(cp, "d", user_pw->pw_dir,
1562 "u", user_pw->pw_name, (char *)NULL);
1566 if (request_whitelist != NULL)
1567 fatal("Permitted requests already set");
1568 request_whitelist = xstrdup(optarg);
1571 if (request_blacklist != NULL)
1572 fatal("Refused requests already set");
1573 request_blacklist = xstrdup(optarg);
1577 mask = strtol(optarg, &cp, 8);
1578 if (mask < 0 || mask > 0777 || *cp != '\0' ||
1579 cp == optarg || (mask == 0 && errno != 0))
1580 fatal("Invalid umask \"%s\"", optarg);
1581 (void)umask((mode_t)mask);
1585 sftp_server_usage();
1589 log_init(__progname, log_level, log_facility, log_stderr);
1591 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1593 * On Linux, we should try to avoid making /proc/self/{mem,maps}
1594 * available to the user so that sftp access doesn't automatically
1595 * imply arbitrary code execution access that will break
1596 * restricted configurations.
1598 if (prctl(PR_SET_DUMPABLE, 0) != 0)
1599 fatal("unable to make the process undumpable");
1600 #endif /* defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE) */
1602 /* Drop any fine-grained privileges we don't need */
1603 platform_pledge_sftp_server();
1605 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1606 client_addr = xstrdup(cp);
1607 if ((cp = strchr(client_addr, ' ')) == NULL) {
1608 error("Malformed SSH_CONNECTION variable: \"%s\"",
1609 getenv("SSH_CONNECTION"));
1610 sftp_server_cleanup_exit(255);
1614 client_addr = xstrdup("UNKNOWN");
1616 logit("session opened for local user %s from [%s]",
1617 pw->pw_name, client_addr);
1620 out = STDOUT_FILENO;
1623 setmode(in, O_BINARY);
1624 setmode(out, O_BINARY);
1633 if ((iqueue = sshbuf_new()) == NULL)
1634 fatal("%s: sshbuf_new failed", __func__);
1635 if ((oqueue = sshbuf_new()) == NULL)
1636 fatal("%s: sshbuf_new failed", __func__);
1638 rset = xcalloc(howmany(max + 1, NFDBITS), sizeof(fd_mask));
1639 wset = xcalloc(howmany(max + 1, NFDBITS), sizeof(fd_mask));
1641 if (homedir != NULL) {
1642 if (chdir(homedir) != 0) {
1643 error("chdir to \"%s\" failed: %s", homedir,
1648 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1650 memset(rset, 0, set_size);
1651 memset(wset, 0, set_size);
1654 * Ensure that we can read a full buffer and handle
1655 * the worst-case length packet it can generate,
1656 * otherwise apply backpressure by stopping reads.
1658 if ((r = sshbuf_check_reserve(iqueue, sizeof(buf))) == 0 &&
1659 (r = sshbuf_check_reserve(oqueue,
1660 SFTP_MAX_MSG_LENGTH)) == 0)
1662 else if (r != SSH_ERR_NO_BUFFER_SPACE)
1663 fatal("%s: sshbuf_check_reserve failed: %s",
1664 __func__, ssh_err(r));
1666 olen = sshbuf_len(oqueue);
1670 if (select(max+1, rset, wset, NULL, NULL) < 0) {
1673 error("select: %s", strerror(errno));
1674 sftp_server_cleanup_exit(2);
1677 /* copy stdin to iqueue */
1678 if (FD_ISSET(in, rset)) {
1679 len = read(in, buf, sizeof buf);
1682 sftp_server_cleanup_exit(0);
1683 } else if (len < 0) {
1684 error("read: %s", strerror(errno));
1685 sftp_server_cleanup_exit(1);
1686 } else if ((r = sshbuf_put(iqueue, buf, len)) != 0) {
1687 fatal("%s: buffer error: %s",
1688 __func__, ssh_err(r));
1691 /* send oqueue to stdout */
1692 if (FD_ISSET(out, wset)) {
1693 len = write(out, sshbuf_ptr(oqueue), olen);
1695 error("write: %s", strerror(errno));
1696 sftp_server_cleanup_exit(1);
1697 } else if ((r = sshbuf_consume(oqueue, len)) != 0) {
1698 fatal("%s: buffer error: %s",
1699 __func__, ssh_err(r));
1704 * Process requests from client if we can fit the results
1705 * into the output buffer, otherwise stop processing input
1706 * and let the output queue drain.
1708 r = sshbuf_check_reserve(oqueue, SFTP_MAX_MSG_LENGTH);
1711 else if (r != SSH_ERR_NO_BUFFER_SPACE)
1712 fatal("%s: sshbuf_check_reserve: %s",
1713 __func__, ssh_err(r));