1 /* $OpenBSD: sftp-server.c,v 1.66 2006/07/25 02:59:21 stevesk 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>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
41 #include "sftp-common.h"
44 #define get_int64() buffer_get_int64(&iqueue);
45 #define get_int() buffer_get_int(&iqueue);
46 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
49 LogLevel log_level = SYSLOG_LEVEL_ERROR;
52 struct passwd *pw = NULL;
53 char *client_addr = NULL;
55 /* input and output queue */
59 /* Version of client */
62 /* portable attributes, etc. */
64 typedef struct Stat Stat;
73 errno_to_portable(int unixerrno)
85 ret = SSH2_FX_NO_SUCH_FILE;
90 ret = SSH2_FX_PERMISSION_DENIED;
94 ret = SSH2_FX_BAD_MESSAGE;
97 ret = SSH2_FX_FAILURE;
104 flags_from_portable(int pflags)
108 if ((pflags & SSH2_FXF_READ) &&
109 (pflags & SSH2_FXF_WRITE)) {
111 } else if (pflags & SSH2_FXF_READ) {
113 } else if (pflags & SSH2_FXF_WRITE) {
116 if (pflags & SSH2_FXF_CREAT)
118 if (pflags & SSH2_FXF_TRUNC)
120 if (pflags & SSH2_FXF_EXCL)
126 string_from_portable(int pflags)
128 static char ret[128];
132 #define PAPPEND(str) { \
134 strlcat(ret, ",", sizeof(ret)); \
135 strlcat(ret, str, sizeof(ret)); \
138 if (pflags & SSH2_FXF_READ)
140 if (pflags & SSH2_FXF_WRITE)
142 if (pflags & SSH2_FXF_CREAT)
144 if (pflags & SSH2_FXF_TRUNC)
146 if (pflags & SSH2_FXF_EXCL)
155 return decode_attrib(&iqueue);
160 typedef struct Handle Handle;
166 u_int64_t bytes_read, bytes_write;
182 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
183 handles[i].use = HANDLE_UNUSED;
187 handle_new(int use, const char *name, int fd, DIR *dirp)
191 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
192 if (handles[i].use == HANDLE_UNUSED) {
193 handles[i].use = use;
194 handles[i].dirp = dirp;
196 handles[i].name = xstrdup(name);
197 handles[i].bytes_read = handles[i].bytes_write = 0;
205 handle_is_ok(int i, int type)
207 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
208 handles[i].use == type;
212 handle_to_string(int handle, char **stringp, int *hlenp)
214 if (stringp == NULL || hlenp == NULL)
216 *stringp = xmalloc(sizeof(int32_t));
217 put_u32(*stringp, handle);
218 *hlenp = sizeof(int32_t);
223 handle_from_string(const char *handle, u_int hlen)
227 if (hlen != sizeof(int32_t))
229 val = get_u32(handle);
230 if (handle_is_ok(val, HANDLE_FILE) ||
231 handle_is_ok(val, HANDLE_DIR))
237 handle_to_name(int handle)
239 if (handle_is_ok(handle, HANDLE_DIR)||
240 handle_is_ok(handle, HANDLE_FILE))
241 return handles[handle].name;
246 handle_to_dir(int handle)
248 if (handle_is_ok(handle, HANDLE_DIR))
249 return handles[handle].dirp;
254 handle_to_fd(int handle)
256 if (handle_is_ok(handle, HANDLE_FILE))
257 return handles[handle].fd;
262 handle_update_read(int handle, ssize_t bytes)
264 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
265 handles[handle].bytes_read += bytes;
269 handle_update_write(int handle, ssize_t bytes)
271 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
272 handles[handle].bytes_write += bytes;
276 handle_bytes_read(int handle)
278 if (handle_is_ok(handle, HANDLE_FILE))
279 return (handles[handle].bytes_read);
284 handle_bytes_write(int handle)
286 if (handle_is_ok(handle, HANDLE_FILE))
287 return (handles[handle].bytes_write);
292 handle_close(int handle)
296 if (handle_is_ok(handle, HANDLE_FILE)) {
297 ret = close(handles[handle].fd);
298 handles[handle].use = HANDLE_UNUSED;
299 xfree(handles[handle].name);
300 } else if (handle_is_ok(handle, HANDLE_DIR)) {
301 ret = closedir(handles[handle].dirp);
302 handles[handle].use = HANDLE_UNUSED;
303 xfree(handles[handle].name);
311 handle_log_close(int handle, char *emsg)
313 if (handle_is_ok(handle, HANDLE_FILE)) {
314 logit("%s%sclose \"%s\" bytes read %llu written %llu",
315 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
316 handle_to_name(handle),
317 handle_bytes_read(handle), handle_bytes_write(handle));
319 logit("%s%sclosedir \"%s\"",
320 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
321 handle_to_name(handle));
326 handle_log_exit(void)
330 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
331 if (handles[i].use != HANDLE_UNUSED)
332 handle_log_close(i, "forced");
342 handle = get_string(&hlen);
344 val = handle_from_string(handle, hlen);
354 int mlen = buffer_len(m);
356 buffer_put_int(&oqueue, mlen);
357 buffer_append(&oqueue, buffer_ptr(m), mlen);
358 buffer_consume(m, mlen);
362 status_to_message(u_int32_t status)
364 const char *status_messages[] = {
365 "Success", /* SSH_FX_OK */
366 "End of file", /* SSH_FX_EOF */
367 "No such file", /* SSH_FX_NO_SUCH_FILE */
368 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
369 "Failure", /* SSH_FX_FAILURE */
370 "Bad message", /* SSH_FX_BAD_MESSAGE */
371 "No connection", /* SSH_FX_NO_CONNECTION */
372 "Connection lost", /* SSH_FX_CONNECTION_LOST */
373 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
374 "Unknown error" /* Others */
376 return (status_messages[MIN(status,SSH2_FX_MAX)]);
380 send_status(u_int32_t id, u_int32_t status)
384 debug3("request %u: sent status %u", id, status);
385 if (log_level > SYSLOG_LEVEL_VERBOSE ||
386 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
387 logit("sent status %s", status_to_message(status));
389 buffer_put_char(&msg, SSH2_FXP_STATUS);
390 buffer_put_int(&msg, id);
391 buffer_put_int(&msg, status);
393 buffer_put_cstring(&msg, status_to_message(status));
394 buffer_put_cstring(&msg, "");
400 send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
405 buffer_put_char(&msg, type);
406 buffer_put_int(&msg, id);
407 buffer_put_string(&msg, data, dlen);
413 send_data(u_int32_t id, const char *data, int dlen)
415 debug("request %u: sent data len %d", id, dlen);
416 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
420 send_handle(u_int32_t id, int handle)
425 handle_to_string(handle, &string, &hlen);
426 debug("request %u: sent handle handle %d", id, handle);
427 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
432 send_names(u_int32_t id, int count, const Stat *stats)
438 buffer_put_char(&msg, SSH2_FXP_NAME);
439 buffer_put_int(&msg, id);
440 buffer_put_int(&msg, count);
441 debug("request %u: sent names count %d", id, count);
442 for (i = 0; i < count; i++) {
443 buffer_put_cstring(&msg, stats[i].name);
444 buffer_put_cstring(&msg, stats[i].long_name);
445 encode_attrib(&msg, &stats[i].attrib);
452 send_attrib(u_int32_t id, const Attrib *a)
456 debug("request %u: sent attrib have 0x%x", id, a->flags);
458 buffer_put_char(&msg, SSH2_FXP_ATTRS);
459 buffer_put_int(&msg, id);
460 encode_attrib(&msg, a);
473 verbose("received client version %d", version);
475 buffer_put_char(&msg, SSH2_FXP_VERSION);
476 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
484 u_int32_t id, pflags;
487 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
490 name = get_string(NULL);
491 pflags = get_int(); /* portable flags */
492 debug3("request %u: open flags %d", id, pflags);
494 flags = flags_from_portable(pflags);
495 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
496 logit("open \"%s\" flags %s mode 0%o",
497 name, string_from_portable(pflags), mode);
498 fd = open(name, flags, mode);
500 status = errno_to_portable(errno);
502 handle = handle_new(HANDLE_FILE, name, fd, NULL);
506 send_handle(id, handle);
510 if (status != SSH2_FX_OK)
511 send_status(id, status);
519 int handle, ret, status = SSH2_FX_FAILURE;
522 handle = get_handle();
523 debug3("request %u: close handle %u", id, handle);
524 handle_log_close(handle, NULL);
525 ret = handle_close(handle);
526 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
527 send_status(id, status);
535 int handle, fd, ret, status = SSH2_FX_FAILURE;
539 handle = get_handle();
543 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
544 id, handle_to_name(handle), handle, (unsigned long long)off, len);
545 if (len > sizeof buf) {
547 debug2("read change len %d", len);
549 fd = handle_to_fd(handle);
551 if (lseek(fd, off, SEEK_SET) < 0) {
552 error("process_read: seek failed");
553 status = errno_to_portable(errno);
555 ret = read(fd, buf, len);
557 status = errno_to_portable(errno);
558 } else if (ret == 0) {
559 status = SSH2_FX_EOF;
561 send_data(id, buf, ret);
563 handle_update_read(handle, ret);
567 if (status != SSH2_FX_OK)
568 send_status(id, status);
577 int handle, fd, ret, status = SSH2_FX_FAILURE;
581 handle = get_handle();
583 data = get_string(&len);
585 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
586 id, handle_to_name(handle), handle, (unsigned long long)off, len);
587 fd = handle_to_fd(handle);
589 if (lseek(fd, off, SEEK_SET) < 0) {
590 status = errno_to_portable(errno);
591 error("process_write: seek failed");
594 ret = write(fd, data, len);
596 error("process_write: write failed");
597 status = errno_to_portable(errno);
598 } else if ((size_t)ret == len) {
600 handle_update_write(handle, ret);
602 debug2("nothing at all written");
606 send_status(id, status);
611 process_do_stat(int do_lstat)
617 int ret, status = SSH2_FX_FAILURE;
620 name = get_string(NULL);
621 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
622 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
623 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
625 status = errno_to_portable(errno);
627 stat_to_attrib(&st, &a);
631 if (status != SSH2_FX_OK)
632 send_status(id, status);
654 int fd, ret, handle, status = SSH2_FX_FAILURE;
657 handle = get_handle();
658 debug("request %u: fstat \"%s\" (handle %u)",
659 id, handle_to_name(handle), handle);
660 fd = handle_to_fd(handle);
662 ret = fstat(fd, &st);
664 status = errno_to_portable(errno);
666 stat_to_attrib(&st, &a);
671 if (status != SSH2_FX_OK)
672 send_status(id, status);
675 static struct timeval *
676 attrib_to_tv(const Attrib *a)
678 static struct timeval tv[2];
680 tv[0].tv_sec = a->atime;
682 tv[1].tv_sec = a->mtime;
688 process_setstat(void)
693 int status = SSH2_FX_OK, ret;
696 name = get_string(NULL);
698 debug("request %u: setstat name \"%s\"", id, name);
699 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
700 logit("set \"%s\" size %llu", name, a->size);
701 ret = truncate(name, a->size);
703 status = errno_to_portable(errno);
705 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
706 logit("set \"%s\" mode %04o", name, a->perm);
707 ret = chmod(name, a->perm & 0777);
709 status = errno_to_portable(errno);
711 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
715 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
717 logit("set \"%s\" modtime %s", name, buf);
718 ret = utimes(name, attrib_to_tv(a));
720 status = errno_to_portable(errno);
722 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
723 logit("set \"%s\" owner %lu group %lu", name,
724 (u_long)a->uid, (u_long)a->gid);
725 ret = chown(name, a->uid, a->gid);
727 status = errno_to_portable(errno);
729 send_status(id, status);
734 process_fsetstat(void)
739 int status = SSH2_FX_OK;
742 handle = get_handle();
744 debug("request %u: fsetstat handle %d", id, handle);
745 fd = handle_to_fd(handle);
747 status = SSH2_FX_FAILURE;
749 char *name = handle_to_name(handle);
751 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
752 logit("set \"%s\" size %llu", name, a->size);
753 ret = ftruncate(fd, a->size);
755 status = errno_to_portable(errno);
757 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
758 logit("set \"%s\" mode %04o", name, a->perm);
760 ret = fchmod(fd, a->perm & 0777);
762 ret = chmod(name, a->perm & 0777);
765 status = errno_to_portable(errno);
767 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
771 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
773 logit("set \"%s\" modtime %s", name, buf);
775 ret = futimes(fd, attrib_to_tv(a));
777 ret = utimes(name, attrib_to_tv(a));
780 status = errno_to_portable(errno);
782 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
783 logit("set \"%s\" owner %lu group %lu", name,
784 (u_long)a->uid, (u_long)a->gid);
786 ret = fchown(fd, a->uid, a->gid);
788 ret = chown(name, a->uid, a->gid);
791 status = errno_to_portable(errno);
794 send_status(id, status);
798 process_opendir(void)
802 int handle, status = SSH2_FX_FAILURE;
806 path = get_string(NULL);
807 debug3("request %u: opendir", id);
808 logit("opendir \"%s\"", path);
809 dirp = opendir(path);
811 status = errno_to_portable(errno);
813 handle = handle_new(HANDLE_DIR, path, 0, dirp);
817 send_handle(id, handle);
822 if (status != SSH2_FX_OK)
823 send_status(id, status);
828 process_readdir(void)
837 handle = get_handle();
838 debug("request %u: readdir \"%s\" (handle %d)", id,
839 handle_to_name(handle), handle);
840 dirp = handle_to_dir(handle);
841 path = handle_to_name(handle);
842 if (dirp == NULL || path == NULL) {
843 send_status(id, SSH2_FX_FAILURE);
846 char pathname[MAXPATHLEN];
848 int nstats = 10, count = 0, i;
850 stats = xcalloc(nstats, sizeof(Stat));
851 while ((dp = readdir(dirp)) != NULL) {
852 if (count >= nstats) {
854 stats = xrealloc(stats, nstats, sizeof(Stat));
857 snprintf(pathname, sizeof pathname, "%s%s%s", path,
858 strcmp(path, "/") ? "/" : "", dp->d_name);
859 if (lstat(pathname, &st) < 0)
861 stat_to_attrib(&st, &(stats[count].attrib));
862 stats[count].name = xstrdup(dp->d_name);
863 stats[count].long_name = ls_file(dp->d_name, &st, 0);
865 /* send up to 100 entries in one message */
866 /* XXX check packet size instead */
871 send_names(id, count, stats);
872 for (i = 0; i < count; i++) {
873 xfree(stats[i].name);
874 xfree(stats[i].long_name);
877 send_status(id, SSH2_FX_EOF);
888 int status = SSH2_FX_FAILURE;
892 name = get_string(NULL);
893 debug3("request %u: remove", id);
894 logit("remove name \"%s\"", name);
896 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
897 send_status(id, status);
907 int ret, mode, status = SSH2_FX_FAILURE;
910 name = get_string(NULL);
912 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
913 a->perm & 0777 : 0777;
914 debug3("request %u: mkdir", id);
915 logit("mkdir name \"%s\" mode 0%o", name, mode);
916 ret = mkdir(name, mode);
917 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
918 send_status(id, status);
930 name = get_string(NULL);
931 debug3("request %u: rmdir", id);
932 logit("rmdir name \"%s\"", name);
934 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
935 send_status(id, status);
940 process_realpath(void)
942 char resolvedname[MAXPATHLEN];
947 path = get_string(NULL);
948 if (path[0] == '\0') {
952 debug3("request %u: realpath", id);
953 verbose("realpath \"%s\"", path);
954 if (realpath(path, resolvedname) == NULL) {
955 send_status(id, errno_to_portable(errno));
958 attrib_clear(&s.attrib);
959 s.name = s.long_name = resolvedname;
960 send_names(id, 1, &s);
969 char *oldpath, *newpath;
974 oldpath = get_string(NULL);
975 newpath = get_string(NULL);
976 debug3("request %u: rename", id);
977 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
978 status = SSH2_FX_FAILURE;
979 if (lstat(oldpath, &sb) == -1)
980 status = errno_to_portable(errno);
981 else if (S_ISREG(sb.st_mode)) {
982 /* Race-free rename of regular files */
983 if (link(oldpath, newpath) == -1) {
984 if (errno == EOPNOTSUPP
985 #ifdef LINK_OPNOTSUPP_ERRNO
986 || errno == LINK_OPNOTSUPP_ERRNO
992 * fs doesn't support links, so fall back to
993 * stat+rename. This is racy.
995 if (stat(newpath, &st) == -1) {
996 if (rename(oldpath, newpath) == -1)
998 errno_to_portable(errno);
1000 status = SSH2_FX_OK;
1003 status = errno_to_portable(errno);
1005 } else if (unlink(oldpath) == -1) {
1006 status = errno_to_portable(errno);
1007 /* clean spare link */
1010 status = SSH2_FX_OK;
1011 } else if (stat(newpath, &sb) == -1) {
1012 if (rename(oldpath, newpath) == -1)
1013 status = errno_to_portable(errno);
1015 status = SSH2_FX_OK;
1017 send_status(id, status);
1023 process_readlink(void)
1027 char buf[MAXPATHLEN];
1031 path = get_string(NULL);
1032 debug3("request %u: readlink", id);
1033 verbose("readlink \"%s\"", path);
1034 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
1035 send_status(id, errno_to_portable(errno));
1040 attrib_clear(&s.attrib);
1041 s.name = s.long_name = buf;
1042 send_names(id, 1, &s);
1048 process_symlink(void)
1051 char *oldpath, *newpath;
1055 oldpath = get_string(NULL);
1056 newpath = get_string(NULL);
1057 debug3("request %u: symlink", id);
1058 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
1059 /* this will fail if 'newpath' exists */
1060 ret = symlink(oldpath, newpath);
1061 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1062 send_status(id, status);
1068 process_extended(void)
1074 request = get_string(NULL);
1075 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1079 /* stolen from ssh-agent */
1090 buf_len = buffer_len(&iqueue);
1092 return; /* Incomplete message. */
1093 cp = buffer_ptr(&iqueue);
1094 msg_len = get_u32(cp);
1095 if (msg_len > SFTP_MAX_MSG_LENGTH) {
1096 error("bad message from %s local user %s",
1097 client_addr, pw->pw_name);
1100 if (buf_len < msg_len + 4)
1102 buffer_consume(&iqueue, 4);
1104 type = buffer_get_char(&iqueue);
1112 case SSH2_FXP_CLOSE:
1118 case SSH2_FXP_WRITE:
1121 case SSH2_FXP_LSTAT:
1124 case SSH2_FXP_FSTAT:
1127 case SSH2_FXP_SETSTAT:
1130 case SSH2_FXP_FSETSTAT:
1133 case SSH2_FXP_OPENDIR:
1136 case SSH2_FXP_READDIR:
1139 case SSH2_FXP_REMOVE:
1142 case SSH2_FXP_MKDIR:
1145 case SSH2_FXP_RMDIR:
1148 case SSH2_FXP_REALPATH:
1154 case SSH2_FXP_RENAME:
1157 case SSH2_FXP_READLINK:
1160 case SSH2_FXP_SYMLINK:
1163 case SSH2_FXP_EXTENDED:
1167 error("Unknown message %d", type);
1170 /* discard the remaining bytes from the current packet */
1171 if (buf_len < buffer_len(&iqueue))
1172 fatal("iqueue grew unexpectedly");
1173 consumed = buf_len - buffer_len(&iqueue);
1174 if (msg_len < consumed)
1175 fatal("msg_len %d < consumed %d", msg_len, consumed);
1176 if (msg_len > consumed)
1177 buffer_consume(&iqueue, msg_len - consumed);
1180 /* Cleanup handler that logs active handles upon normal exit */
1184 if (pw != NULL && client_addr != NULL) {
1186 logit("session closed for local user %s from [%s]",
1187 pw->pw_name, client_addr);
1195 extern char *__progname;
1198 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1203 main(int argc, char **argv)
1205 fd_set *rset, *wset;
1206 int in, out, max, ch, skipargs = 0, log_stderr = 0;
1207 ssize_t len, olen, set_size;
1208 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1211 extern char *optarg;
1212 extern char *__progname;
1214 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1217 __progname = ssh_get_progname(argv[0]);
1218 log_init(__progname, log_level, log_facility, log_stderr);
1220 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1224 * Ignore all arguments if we are invoked as a
1225 * shell using "sftp-server -c command"
1233 log_level = log_level_number(optarg);
1234 if (log_level == SYSLOG_LEVEL_NOT_SET)
1235 error("Invalid log level \"%s\"", optarg);
1238 log_facility = log_facility_number(optarg);
1239 if (log_level == SYSLOG_FACILITY_NOT_SET)
1240 error("Invalid log facility \"%s\"", optarg);
1248 log_init(__progname, log_level, log_facility, log_stderr);
1250 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1251 client_addr = xstrdup(cp);
1252 if ((cp = strchr(client_addr, ' ')) == NULL)
1253 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1254 getenv("SSH_CONNECTION"));
1257 client_addr = xstrdup("UNKNOWN");
1259 if ((pw = getpwuid(getuid())) == NULL)
1260 fatal("No user found for uid %lu", (u_long)getuid());
1263 logit("session opened for local user %s from [%s]",
1264 pw->pw_name, client_addr);
1268 in = dup(STDIN_FILENO);
1269 out = dup(STDOUT_FILENO);
1272 setmode(in, O_BINARY);
1273 setmode(out, O_BINARY);
1282 buffer_init(&iqueue);
1283 buffer_init(&oqueue);
1285 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1286 rset = (fd_set *)xmalloc(set_size);
1287 wset = (fd_set *)xmalloc(set_size);
1290 memset(rset, 0, set_size);
1291 memset(wset, 0, set_size);
1294 olen = buffer_len(&oqueue);
1298 if (select(max+1, rset, wset, NULL, NULL) < 0) {
1301 error("select: %s", strerror(errno));
1305 /* copy stdin to iqueue */
1306 if (FD_ISSET(in, rset)) {
1308 len = read(in, buf, sizeof buf);
1312 } else if (len < 0) {
1313 error("read: %s", strerror(errno));
1316 buffer_append(&iqueue, buf, len);
1319 /* send oqueue to stdout */
1320 if (FD_ISSET(out, wset)) {
1321 len = write(out, buffer_ptr(&oqueue), olen);
1323 error("write: %s", strerror(errno));
1326 buffer_consume(&oqueue, len);
1329 /* process requests from client */