1 /* $OpenBSD: sftp-server.c,v 1.58 2006/07/06 10:47:57 djm 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.
19 #include <sys/types.h>
33 #include "sftp-common.h"
36 #define get_int64() buffer_get_int64(&iqueue);
37 #define get_int() buffer_get_int(&iqueue);
38 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
41 LogLevel log_level = SYSLOG_LEVEL_ERROR;
44 struct passwd *pw = NULL;
45 char *client_addr = NULL;
47 /* input and output queue */
51 /* Version of client */
54 /* portable attributes, etc. */
56 typedef struct Stat Stat;
65 errno_to_portable(int unixerrno)
77 ret = SSH2_FX_NO_SUCH_FILE;
82 ret = SSH2_FX_PERMISSION_DENIED;
86 ret = SSH2_FX_BAD_MESSAGE;
89 ret = SSH2_FX_FAILURE;
96 flags_from_portable(int pflags)
100 if ((pflags & SSH2_FXF_READ) &&
101 (pflags & SSH2_FXF_WRITE)) {
103 } else if (pflags & SSH2_FXF_READ) {
105 } else if (pflags & SSH2_FXF_WRITE) {
108 if (pflags & SSH2_FXF_CREAT)
110 if (pflags & SSH2_FXF_TRUNC)
112 if (pflags & SSH2_FXF_EXCL)
118 string_from_portable(int pflags)
120 static char ret[128];
124 #define PAPPEND(str) { \
126 strlcat(ret, ",", sizeof(ret)); \
127 strlcat(ret, str, sizeof(ret)); \
130 if (pflags & SSH2_FXF_READ)
132 if (pflags & SSH2_FXF_WRITE)
134 if (pflags & SSH2_FXF_CREAT)
136 if (pflags & SSH2_FXF_TRUNC)
138 if (pflags & SSH2_FXF_EXCL)
147 return decode_attrib(&iqueue);
152 typedef struct Handle Handle;
158 u_int64_t bytes_read, bytes_write;
174 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
175 handles[i].use = HANDLE_UNUSED;
179 handle_new(int use, const char *name, int fd, DIR *dirp)
183 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
184 if (handles[i].use == HANDLE_UNUSED) {
185 handles[i].use = use;
186 handles[i].dirp = dirp;
188 handles[i].name = xstrdup(name);
189 handles[i].bytes_read = handles[i].bytes_write = 0;
197 handle_is_ok(int i, int type)
199 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
200 handles[i].use == type;
204 handle_to_string(int handle, char **stringp, int *hlenp)
206 if (stringp == NULL || hlenp == NULL)
208 *stringp = xmalloc(sizeof(int32_t));
209 put_u32(*stringp, handle);
210 *hlenp = sizeof(int32_t);
215 handle_from_string(const char *handle, u_int hlen)
219 if (hlen != sizeof(int32_t))
221 val = get_u32(handle);
222 if (handle_is_ok(val, HANDLE_FILE) ||
223 handle_is_ok(val, HANDLE_DIR))
229 handle_to_name(int handle)
231 if (handle_is_ok(handle, HANDLE_DIR)||
232 handle_is_ok(handle, HANDLE_FILE))
233 return handles[handle].name;
238 handle_to_dir(int handle)
240 if (handle_is_ok(handle, HANDLE_DIR))
241 return handles[handle].dirp;
246 handle_to_fd(int handle)
248 if (handle_is_ok(handle, HANDLE_FILE))
249 return handles[handle].fd;
254 handle_update_read(int handle, ssize_t bytes)
256 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
257 handles[handle].bytes_read += bytes;
261 handle_update_write(int handle, ssize_t bytes)
263 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
264 handles[handle].bytes_write += bytes;
268 handle_bytes_read(int handle)
270 if (handle_is_ok(handle, HANDLE_FILE))
271 return (handles[handle].bytes_read);
276 handle_bytes_write(int handle)
278 if (handle_is_ok(handle, HANDLE_FILE))
279 return (handles[handle].bytes_write);
284 handle_close(int handle)
288 if (handle_is_ok(handle, HANDLE_FILE)) {
289 ret = close(handles[handle].fd);
290 handles[handle].use = HANDLE_UNUSED;
291 xfree(handles[handle].name);
292 } else if (handle_is_ok(handle, HANDLE_DIR)) {
293 ret = closedir(handles[handle].dirp);
294 handles[handle].use = HANDLE_UNUSED;
295 xfree(handles[handle].name);
303 handle_log_close(int handle, char *emsg)
305 if (handle_is_ok(handle, HANDLE_FILE)) {
306 logit("%s%sclose \"%s\" bytes read %llu written %llu",
307 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
308 handle_to_name(handle),
309 handle_bytes_read(handle), handle_bytes_write(handle));
311 logit("%s%sclosedir \"%s\"",
312 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
313 handle_to_name(handle));
318 handle_log_exit(void)
322 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
323 if (handles[i].use != HANDLE_UNUSED)
324 handle_log_close(i, "forced");
334 handle = get_string(&hlen);
336 val = handle_from_string(handle, hlen);
346 int mlen = buffer_len(m);
348 buffer_put_int(&oqueue, mlen);
349 buffer_append(&oqueue, buffer_ptr(m), mlen);
350 buffer_consume(m, mlen);
354 status_to_message(u_int32_t status)
356 const char *status_messages[] = {
357 "Success", /* SSH_FX_OK */
358 "End of file", /* SSH_FX_EOF */
359 "No such file", /* SSH_FX_NO_SUCH_FILE */
360 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
361 "Failure", /* SSH_FX_FAILURE */
362 "Bad message", /* SSH_FX_BAD_MESSAGE */
363 "No connection", /* SSH_FX_NO_CONNECTION */
364 "Connection lost", /* SSH_FX_CONNECTION_LOST */
365 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
366 "Unknown error" /* Others */
368 return (status_messages[MIN(status,SSH2_FX_MAX)]);
372 send_status(u_int32_t id, u_int32_t status)
376 debug3("request %u: sent status %u", id, status);
377 if (log_level > SYSLOG_LEVEL_VERBOSE ||
378 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
379 logit("sent status %s", status_to_message(status));
381 buffer_put_char(&msg, SSH2_FXP_STATUS);
382 buffer_put_int(&msg, id);
383 buffer_put_int(&msg, status);
385 buffer_put_cstring(&msg, status_to_message(status));
386 buffer_put_cstring(&msg, "");
392 send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
397 buffer_put_char(&msg, type);
398 buffer_put_int(&msg, id);
399 buffer_put_string(&msg, data, dlen);
405 send_data(u_int32_t id, const char *data, int dlen)
407 debug("request %u: sent data len %d", id, dlen);
408 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
412 send_handle(u_int32_t id, int handle)
417 handle_to_string(handle, &string, &hlen);
418 debug("request %u: sent handle handle %d", id, handle);
419 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
424 send_names(u_int32_t id, int count, const Stat *stats)
430 buffer_put_char(&msg, SSH2_FXP_NAME);
431 buffer_put_int(&msg, id);
432 buffer_put_int(&msg, count);
433 debug("request %u: sent names count %d", id, count);
434 for (i = 0; i < count; i++) {
435 buffer_put_cstring(&msg, stats[i].name);
436 buffer_put_cstring(&msg, stats[i].long_name);
437 encode_attrib(&msg, &stats[i].attrib);
444 send_attrib(u_int32_t id, const Attrib *a)
448 debug("request %u: sent attrib have 0x%x", id, a->flags);
450 buffer_put_char(&msg, SSH2_FXP_ATTRS);
451 buffer_put_int(&msg, id);
452 encode_attrib(&msg, a);
465 verbose("received client version %d", version);
467 buffer_put_char(&msg, SSH2_FXP_VERSION);
468 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
476 u_int32_t id, pflags;
479 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
482 debug3("request %u: open flags %d", id, pflags);
483 name = get_string(NULL);
484 pflags = get_int(); /* portable flags */
486 flags = flags_from_portable(pflags);
487 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
488 logit("open \"%s\" flags %s mode 0%o",
489 name, string_from_portable(pflags), mode);
490 fd = open(name, flags, mode);
492 status = errno_to_portable(errno);
494 handle = handle_new(HANDLE_FILE, name, fd, NULL);
498 send_handle(id, handle);
502 if (status != SSH2_FX_OK)
503 send_status(id, status);
511 int handle, ret, status = SSH2_FX_FAILURE;
514 handle = get_handle();
515 debug3("request %u: close handle %u", id, handle);
516 handle_log_close(handle, NULL);
517 ret = handle_close(handle);
518 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
519 send_status(id, status);
527 int handle, fd, ret, status = SSH2_FX_FAILURE;
531 handle = get_handle();
535 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
536 id, handle_to_name(handle), handle, (unsigned long long)off, len);
537 if (len > sizeof buf) {
539 debug2("read change len %d", len);
541 fd = handle_to_fd(handle);
543 if (lseek(fd, off, SEEK_SET) < 0) {
544 error("process_read: seek failed");
545 status = errno_to_portable(errno);
547 ret = read(fd, buf, len);
549 status = errno_to_portable(errno);
550 } else if (ret == 0) {
551 status = SSH2_FX_EOF;
553 send_data(id, buf, ret);
555 handle_update_read(handle, ret);
559 if (status != SSH2_FX_OK)
560 send_status(id, status);
569 int handle, fd, ret, status = SSH2_FX_FAILURE;
573 handle = get_handle();
575 data = get_string(&len);
577 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
578 id, handle_to_name(handle), handle, (unsigned long long)off, len);
579 fd = handle_to_fd(handle);
581 if (lseek(fd, off, SEEK_SET) < 0) {
582 status = errno_to_portable(errno);
583 error("process_write: seek failed");
586 ret = write(fd, data, len);
588 error("process_write: write failed");
589 status = errno_to_portable(errno);
590 } else if ((size_t)ret == len) {
592 handle_update_write(handle, ret);
594 debug2("nothing at all written");
598 send_status(id, status);
603 process_do_stat(int do_lstat)
609 int ret, status = SSH2_FX_FAILURE;
612 name = get_string(NULL);
613 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
614 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
615 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
617 status = errno_to_portable(errno);
619 stat_to_attrib(&st, &a);
623 if (status != SSH2_FX_OK)
624 send_status(id, status);
646 int fd, ret, handle, status = SSH2_FX_FAILURE;
649 handle = get_handle();
650 debug("request %u: fstat \"%s\" (handle %u)",
651 id, handle_to_name(handle), handle);
652 fd = handle_to_fd(handle);
654 ret = fstat(fd, &st);
656 status = errno_to_portable(errno);
658 stat_to_attrib(&st, &a);
663 if (status != SSH2_FX_OK)
664 send_status(id, status);
667 static struct timeval *
668 attrib_to_tv(const Attrib *a)
670 static struct timeval tv[2];
672 tv[0].tv_sec = a->atime;
674 tv[1].tv_sec = a->mtime;
680 process_setstat(void)
685 int status = SSH2_FX_OK, ret;
688 name = get_string(NULL);
690 debug("request %u: setstat name \"%s\"", id, name);
691 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
692 logit("set \"%s\" size %llu", name, a->size);
693 ret = truncate(name, a->size);
695 status = errno_to_portable(errno);
697 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
698 logit("set \"%s\" mode %04o", name, a->perm);
699 ret = chmod(name, a->perm & 0777);
701 status = errno_to_portable(errno);
703 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
707 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
709 logit("set \"%s\" modtime %s", name, buf);
710 ret = utimes(name, attrib_to_tv(a));
712 status = errno_to_portable(errno);
714 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
715 logit("set \"%s\" owner %lu group %lu", name,
716 (u_long)a->uid, (u_long)a->gid);
717 ret = chown(name, a->uid, a->gid);
719 status = errno_to_portable(errno);
721 send_status(id, status);
726 process_fsetstat(void)
731 int status = SSH2_FX_OK;
734 handle = get_handle();
736 debug("request %u: fsetstat handle %d", id, handle);
737 fd = handle_to_fd(handle);
739 status = SSH2_FX_FAILURE;
741 char *name = handle_to_name(handle);
743 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
744 logit("set \"%s\" size %llu", name, a->size);
745 ret = ftruncate(fd, a->size);
747 status = errno_to_portable(errno);
749 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
750 logit("set \"%s\" mode %04o", name, a->perm);
752 ret = fchmod(fd, a->perm & 0777);
754 ret = chmod(name, a->perm & 0777);
757 status = errno_to_portable(errno);
759 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
763 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
765 logit("set \"%s\" modtime %s", name, buf);
767 ret = futimes(fd, attrib_to_tv(a));
769 ret = utimes(name, attrib_to_tv(a));
772 status = errno_to_portable(errno);
774 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
775 logit("set \"%s\" owner %lu group %lu", name,
776 (u_long)a->uid, (u_long)a->gid);
778 ret = fchown(fd, a->uid, a->gid);
780 ret = chown(name, a->uid, a->gid);
783 status = errno_to_portable(errno);
786 send_status(id, status);
790 process_opendir(void)
794 int handle, status = SSH2_FX_FAILURE;
798 path = get_string(NULL);
799 debug3("request %u: opendir", id);
800 logit("opendir \"%s\"", path);
801 dirp = opendir(path);
803 status = errno_to_portable(errno);
805 handle = handle_new(HANDLE_DIR, path, 0, dirp);
809 send_handle(id, handle);
814 if (status != SSH2_FX_OK)
815 send_status(id, status);
820 process_readdir(void)
829 handle = get_handle();
830 debug("request %u: readdir \"%s\" (handle %d)", id,
831 handle_to_name(handle), handle);
832 dirp = handle_to_dir(handle);
833 path = handle_to_name(handle);
834 if (dirp == NULL || path == NULL) {
835 send_status(id, SSH2_FX_FAILURE);
838 char pathname[MAXPATHLEN];
840 int nstats = 10, count = 0, i;
842 stats = xcalloc(nstats, sizeof(Stat));
843 while ((dp = readdir(dirp)) != NULL) {
844 if (count >= nstats) {
846 stats = xrealloc(stats, nstats, sizeof(Stat));
849 snprintf(pathname, sizeof pathname, "%s%s%s", path,
850 strcmp(path, "/") ? "/" : "", dp->d_name);
851 if (lstat(pathname, &st) < 0)
853 stat_to_attrib(&st, &(stats[count].attrib));
854 stats[count].name = xstrdup(dp->d_name);
855 stats[count].long_name = ls_file(dp->d_name, &st, 0);
857 /* send up to 100 entries in one message */
858 /* XXX check packet size instead */
863 send_names(id, count, stats);
864 for (i = 0; i < count; i++) {
865 xfree(stats[i].name);
866 xfree(stats[i].long_name);
869 send_status(id, SSH2_FX_EOF);
880 int status = SSH2_FX_FAILURE;
884 name = get_string(NULL);
885 debug3("request %u: remove", id);
886 logit("remove name \"%s\"", name);
888 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
889 send_status(id, status);
899 int ret, mode, status = SSH2_FX_FAILURE;
902 name = get_string(NULL);
904 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
905 a->perm & 0777 : 0777;
906 debug3("request %u: mkdir", id);
907 logit("mkdir name \"%s\" mode 0%o", name, mode);
908 ret = mkdir(name, mode);
909 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
910 send_status(id, status);
922 name = get_string(NULL);
923 debug3("request %u: rmdir", id);
924 logit("rmdir name \"%s\"", name);
926 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
927 send_status(id, status);
932 process_realpath(void)
934 char resolvedname[MAXPATHLEN];
939 path = get_string(NULL);
940 if (path[0] == '\0') {
944 debug3("request %u: realpath", id);
945 verbose("realpath \"%s\"", path);
946 if (realpath(path, resolvedname) == NULL) {
947 send_status(id, errno_to_portable(errno));
950 attrib_clear(&s.attrib);
951 s.name = s.long_name = resolvedname;
952 send_names(id, 1, &s);
961 char *oldpath, *newpath;
966 oldpath = get_string(NULL);
967 newpath = get_string(NULL);
968 debug3("request %u: rename", id);
969 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
970 status = SSH2_FX_FAILURE;
971 if (lstat(oldpath, &sb) == -1)
972 status = errno_to_portable(errno);
973 else if (S_ISREG(sb.st_mode)) {
974 /* Race-free rename of regular files */
975 if (link(oldpath, newpath) == -1) {
976 if (errno == EOPNOTSUPP
977 #ifdef LINK_OPNOTSUPP_ERRNO
978 || errno == LINK_OPNOTSUPP_ERRNO
984 * fs doesn't support links, so fall back to
985 * stat+rename. This is racy.
987 if (stat(newpath, &st) == -1) {
988 if (rename(oldpath, newpath) == -1)
990 errno_to_portable(errno);
995 status = errno_to_portable(errno);
997 } else if (unlink(oldpath) == -1) {
998 status = errno_to_portable(errno);
999 /* clean spare link */
1002 status = SSH2_FX_OK;
1003 } else if (stat(newpath, &sb) == -1) {
1004 if (rename(oldpath, newpath) == -1)
1005 status = errno_to_portable(errno);
1007 status = SSH2_FX_OK;
1009 send_status(id, status);
1015 process_readlink(void)
1019 char buf[MAXPATHLEN];
1023 path = get_string(NULL);
1024 debug3("request %u: readlink", id);
1025 verbose("readlink \"%s\"", path);
1026 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
1027 send_status(id, errno_to_portable(errno));
1032 attrib_clear(&s.attrib);
1033 s.name = s.long_name = buf;
1034 send_names(id, 1, &s);
1040 process_symlink(void)
1043 char *oldpath, *newpath;
1047 oldpath = get_string(NULL);
1048 newpath = get_string(NULL);
1049 debug3("request %u: symlink", id);
1050 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
1051 /* this will fail if 'newpath' exists */
1052 ret = symlink(oldpath, newpath);
1053 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1054 send_status(id, status);
1060 process_extended(void)
1066 request = get_string(NULL);
1067 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1071 /* stolen from ssh-agent */
1082 buf_len = buffer_len(&iqueue);
1084 return; /* Incomplete message. */
1085 cp = buffer_ptr(&iqueue);
1086 msg_len = get_u32(cp);
1087 if (msg_len > SFTP_MAX_MSG_LENGTH) {
1088 error("bad message from %s local user %s",
1089 client_addr, pw->pw_name);
1092 if (buf_len < msg_len + 4)
1094 buffer_consume(&iqueue, 4);
1096 type = buffer_get_char(&iqueue);
1104 case SSH2_FXP_CLOSE:
1110 case SSH2_FXP_WRITE:
1113 case SSH2_FXP_LSTAT:
1116 case SSH2_FXP_FSTAT:
1119 case SSH2_FXP_SETSTAT:
1122 case SSH2_FXP_FSETSTAT:
1125 case SSH2_FXP_OPENDIR:
1128 case SSH2_FXP_READDIR:
1131 case SSH2_FXP_REMOVE:
1134 case SSH2_FXP_MKDIR:
1137 case SSH2_FXP_RMDIR:
1140 case SSH2_FXP_REALPATH:
1146 case SSH2_FXP_RENAME:
1149 case SSH2_FXP_READLINK:
1152 case SSH2_FXP_SYMLINK:
1155 case SSH2_FXP_EXTENDED:
1159 error("Unknown message %d", type);
1162 /* discard the remaining bytes from the current packet */
1163 if (buf_len < buffer_len(&iqueue))
1164 fatal("iqueue grew unexpectedly");
1165 consumed = buf_len - buffer_len(&iqueue);
1166 if (msg_len < consumed)
1167 fatal("msg_len %d < consumed %d", msg_len, consumed);
1168 if (msg_len > consumed)
1169 buffer_consume(&iqueue, msg_len - consumed);
1172 /* Cleanup handler that logs active handles upon normal exit */
1176 if (pw != NULL && client_addr != NULL) {
1178 logit("session closed for local user %s from [%s]",
1179 pw->pw_name, client_addr);
1187 extern char *__progname;
1190 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1195 main(int argc, char **argv)
1197 fd_set *rset, *wset;
1198 int in, out, max, ch, skipargs = 0, log_stderr = 0;
1199 ssize_t len, olen, set_size;
1200 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1204 extern char *optarg;
1205 extern char *__progname;
1207 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1210 __progname = ssh_get_progname(argv[0]);
1211 log_init(__progname, log_level, log_facility, log_stderr);
1213 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1217 * Ignore all arguments if we are invoked as a
1218 * shell using "sftp-server -c command"
1226 log_level = log_level_number(optarg);
1227 if (log_level == SYSLOG_LEVEL_NOT_SET)
1228 error("Invalid log level \"%s\"", optarg);
1231 log_facility = log_facility_number(optarg);
1232 if (log_level == SYSLOG_FACILITY_NOT_SET)
1233 error("Invalid log facility \"%s\"", optarg);
1241 log_init(__progname, log_level, log_facility, log_stderr);
1243 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1244 client_addr = xstrdup(cp);
1245 if ((cp = strchr(client_addr, ' ')) == NULL)
1246 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1247 getenv("SSH_CONNECTION"));
1250 client_addr = xstrdup("UNKNOWN");
1252 if ((pw = getpwuid(getuid())) == NULL)
1253 fatal("No user found for uid %lu", (u_long)getuid());
1256 logit("session opened for local user %s from [%s]",
1257 pw->pw_name, client_addr);
1261 in = dup(STDIN_FILENO);
1262 out = dup(STDOUT_FILENO);
1265 setmode(in, O_BINARY);
1266 setmode(out, O_BINARY);
1275 buffer_init(&iqueue);
1276 buffer_init(&oqueue);
1278 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1279 rset = (fd_set *)xmalloc(set_size);
1280 wset = (fd_set *)xmalloc(set_size);
1283 memset(rset, 0, set_size);
1284 memset(wset, 0, set_size);
1287 olen = buffer_len(&oqueue);
1291 if (select(max+1, rset, wset, NULL, NULL) < 0) {
1294 error("select: %s", strerror(errno));
1298 /* copy stdin to iqueue */
1299 if (FD_ISSET(in, rset)) {
1301 len = read(in, buf, sizeof buf);
1305 } else if (len < 0) {
1306 error("read: %s", strerror(errno));
1309 buffer_append(&iqueue, buf, len);
1312 /* send oqueue to stdout */
1313 if (FD_ISSET(out, wset)) {
1314 len = write(out, buffer_ptr(&oqueue), olen);
1316 error("write: %s", strerror(errno));
1319 buffer_consume(&oqueue, len);
1322 /* process requests from client */