1 /* $OpenBSD: sftp-server.c,v 1.67 2006/07/26 02:35:17 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>
20 #include <sys/param.h>
22 #ifdef HAVE_SYS_TIME_H
23 # include <sys/time.h>
42 #include "sftp-common.h"
45 #define get_int64() buffer_get_int64(&iqueue);
46 #define get_int() buffer_get_int(&iqueue);
47 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
50 LogLevel log_level = SYSLOG_LEVEL_ERROR;
53 struct passwd *pw = NULL;
54 char *client_addr = NULL;
56 /* input and output queue */
60 /* Version of client */
63 /* portable attributes, etc. */
65 typedef struct Stat Stat;
74 errno_to_portable(int unixerrno)
86 ret = SSH2_FX_NO_SUCH_FILE;
91 ret = SSH2_FX_PERMISSION_DENIED;
95 ret = SSH2_FX_BAD_MESSAGE;
98 ret = SSH2_FX_FAILURE;
105 flags_from_portable(int pflags)
109 if ((pflags & SSH2_FXF_READ) &&
110 (pflags & SSH2_FXF_WRITE)) {
112 } else if (pflags & SSH2_FXF_READ) {
114 } else if (pflags & SSH2_FXF_WRITE) {
117 if (pflags & SSH2_FXF_CREAT)
119 if (pflags & SSH2_FXF_TRUNC)
121 if (pflags & SSH2_FXF_EXCL)
127 string_from_portable(int pflags)
129 static char ret[128];
133 #define PAPPEND(str) { \
135 strlcat(ret, ",", sizeof(ret)); \
136 strlcat(ret, str, sizeof(ret)); \
139 if (pflags & SSH2_FXF_READ)
141 if (pflags & SSH2_FXF_WRITE)
143 if (pflags & SSH2_FXF_CREAT)
145 if (pflags & SSH2_FXF_TRUNC)
147 if (pflags & SSH2_FXF_EXCL)
156 return decode_attrib(&iqueue);
161 typedef struct Handle Handle;
167 u_int64_t bytes_read, bytes_write;
183 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
184 handles[i].use = HANDLE_UNUSED;
188 handle_new(int use, const char *name, int fd, DIR *dirp)
192 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
193 if (handles[i].use == HANDLE_UNUSED) {
194 handles[i].use = use;
195 handles[i].dirp = dirp;
197 handles[i].name = xstrdup(name);
198 handles[i].bytes_read = handles[i].bytes_write = 0;
206 handle_is_ok(int i, int type)
208 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
209 handles[i].use == type;
213 handle_to_string(int handle, char **stringp, int *hlenp)
215 if (stringp == NULL || hlenp == NULL)
217 *stringp = xmalloc(sizeof(int32_t));
218 put_u32(*stringp, handle);
219 *hlenp = sizeof(int32_t);
224 handle_from_string(const char *handle, u_int hlen)
228 if (hlen != sizeof(int32_t))
230 val = get_u32(handle);
231 if (handle_is_ok(val, HANDLE_FILE) ||
232 handle_is_ok(val, HANDLE_DIR))
238 handle_to_name(int handle)
240 if (handle_is_ok(handle, HANDLE_DIR)||
241 handle_is_ok(handle, HANDLE_FILE))
242 return handles[handle].name;
247 handle_to_dir(int handle)
249 if (handle_is_ok(handle, HANDLE_DIR))
250 return handles[handle].dirp;
255 handle_to_fd(int handle)
257 if (handle_is_ok(handle, HANDLE_FILE))
258 return handles[handle].fd;
263 handle_update_read(int handle, ssize_t bytes)
265 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
266 handles[handle].bytes_read += bytes;
270 handle_update_write(int handle, ssize_t bytes)
272 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
273 handles[handle].bytes_write += bytes;
277 handle_bytes_read(int handle)
279 if (handle_is_ok(handle, HANDLE_FILE))
280 return (handles[handle].bytes_read);
285 handle_bytes_write(int handle)
287 if (handle_is_ok(handle, HANDLE_FILE))
288 return (handles[handle].bytes_write);
293 handle_close(int handle)
297 if (handle_is_ok(handle, HANDLE_FILE)) {
298 ret = close(handles[handle].fd);
299 handles[handle].use = HANDLE_UNUSED;
300 xfree(handles[handle].name);
301 } else if (handle_is_ok(handle, HANDLE_DIR)) {
302 ret = closedir(handles[handle].dirp);
303 handles[handle].use = HANDLE_UNUSED;
304 xfree(handles[handle].name);
312 handle_log_close(int handle, char *emsg)
314 if (handle_is_ok(handle, HANDLE_FILE)) {
315 logit("%s%sclose \"%s\" bytes read %llu written %llu",
316 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
317 handle_to_name(handle),
318 handle_bytes_read(handle), handle_bytes_write(handle));
320 logit("%s%sclosedir \"%s\"",
321 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
322 handle_to_name(handle));
327 handle_log_exit(void)
331 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
332 if (handles[i].use != HANDLE_UNUSED)
333 handle_log_close(i, "forced");
343 handle = get_string(&hlen);
345 val = handle_from_string(handle, hlen);
355 int mlen = buffer_len(m);
357 buffer_put_int(&oqueue, mlen);
358 buffer_append(&oqueue, buffer_ptr(m), mlen);
359 buffer_consume(m, mlen);
363 status_to_message(u_int32_t status)
365 const char *status_messages[] = {
366 "Success", /* SSH_FX_OK */
367 "End of file", /* SSH_FX_EOF */
368 "No such file", /* SSH_FX_NO_SUCH_FILE */
369 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
370 "Failure", /* SSH_FX_FAILURE */
371 "Bad message", /* SSH_FX_BAD_MESSAGE */
372 "No connection", /* SSH_FX_NO_CONNECTION */
373 "Connection lost", /* SSH_FX_CONNECTION_LOST */
374 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
375 "Unknown error" /* Others */
377 return (status_messages[MIN(status,SSH2_FX_MAX)]);
381 send_status(u_int32_t id, u_int32_t status)
385 debug3("request %u: sent status %u", id, status);
386 if (log_level > SYSLOG_LEVEL_VERBOSE ||
387 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
388 logit("sent status %s", status_to_message(status));
390 buffer_put_char(&msg, SSH2_FXP_STATUS);
391 buffer_put_int(&msg, id);
392 buffer_put_int(&msg, status);
394 buffer_put_cstring(&msg, status_to_message(status));
395 buffer_put_cstring(&msg, "");
401 send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
406 buffer_put_char(&msg, type);
407 buffer_put_int(&msg, id);
408 buffer_put_string(&msg, data, dlen);
414 send_data(u_int32_t id, const char *data, int dlen)
416 debug("request %u: sent data len %d", id, dlen);
417 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
421 send_handle(u_int32_t id, int handle)
426 handle_to_string(handle, &string, &hlen);
427 debug("request %u: sent handle handle %d", id, handle);
428 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
433 send_names(u_int32_t id, int count, const Stat *stats)
439 buffer_put_char(&msg, SSH2_FXP_NAME);
440 buffer_put_int(&msg, id);
441 buffer_put_int(&msg, count);
442 debug("request %u: sent names count %d", id, count);
443 for (i = 0; i < count; i++) {
444 buffer_put_cstring(&msg, stats[i].name);
445 buffer_put_cstring(&msg, stats[i].long_name);
446 encode_attrib(&msg, &stats[i].attrib);
453 send_attrib(u_int32_t id, const Attrib *a)
457 debug("request %u: sent attrib have 0x%x", id, a->flags);
459 buffer_put_char(&msg, SSH2_FXP_ATTRS);
460 buffer_put_int(&msg, id);
461 encode_attrib(&msg, a);
474 verbose("received client version %d", version);
476 buffer_put_char(&msg, SSH2_FXP_VERSION);
477 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
485 u_int32_t id, pflags;
488 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
491 name = get_string(NULL);
492 pflags = get_int(); /* portable flags */
493 debug3("request %u: open flags %d", id, pflags);
495 flags = flags_from_portable(pflags);
496 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
497 logit("open \"%s\" flags %s mode 0%o",
498 name, string_from_portable(pflags), mode);
499 fd = open(name, flags, mode);
501 status = errno_to_portable(errno);
503 handle = handle_new(HANDLE_FILE, name, fd, NULL);
507 send_handle(id, handle);
511 if (status != SSH2_FX_OK)
512 send_status(id, status);
520 int handle, ret, status = SSH2_FX_FAILURE;
523 handle = get_handle();
524 debug3("request %u: close handle %u", id, handle);
525 handle_log_close(handle, NULL);
526 ret = handle_close(handle);
527 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
528 send_status(id, status);
536 int handle, fd, ret, status = SSH2_FX_FAILURE;
540 handle = get_handle();
544 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
545 id, handle_to_name(handle), handle, (unsigned long long)off, len);
546 if (len > sizeof buf) {
548 debug2("read change len %d", len);
550 fd = handle_to_fd(handle);
552 if (lseek(fd, off, SEEK_SET) < 0) {
553 error("process_read: seek failed");
554 status = errno_to_portable(errno);
556 ret = read(fd, buf, len);
558 status = errno_to_portable(errno);
559 } else if (ret == 0) {
560 status = SSH2_FX_EOF;
562 send_data(id, buf, ret);
564 handle_update_read(handle, ret);
568 if (status != SSH2_FX_OK)
569 send_status(id, status);
578 int handle, fd, ret, status = SSH2_FX_FAILURE;
582 handle = get_handle();
584 data = get_string(&len);
586 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
587 id, handle_to_name(handle), handle, (unsigned long long)off, len);
588 fd = handle_to_fd(handle);
590 if (lseek(fd, off, SEEK_SET) < 0) {
591 status = errno_to_portable(errno);
592 error("process_write: seek failed");
595 ret = write(fd, data, len);
597 error("process_write: write failed");
598 status = errno_to_portable(errno);
599 } else if ((size_t)ret == len) {
601 handle_update_write(handle, ret);
603 debug2("nothing at all written");
607 send_status(id, status);
612 process_do_stat(int do_lstat)
618 int ret, status = SSH2_FX_FAILURE;
621 name = get_string(NULL);
622 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
623 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
624 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
626 status = errno_to_portable(errno);
628 stat_to_attrib(&st, &a);
632 if (status != SSH2_FX_OK)
633 send_status(id, status);
655 int fd, ret, handle, status = SSH2_FX_FAILURE;
658 handle = get_handle();
659 debug("request %u: fstat \"%s\" (handle %u)",
660 id, handle_to_name(handle), handle);
661 fd = handle_to_fd(handle);
663 ret = fstat(fd, &st);
665 status = errno_to_portable(errno);
667 stat_to_attrib(&st, &a);
672 if (status != SSH2_FX_OK)
673 send_status(id, status);
676 static struct timeval *
677 attrib_to_tv(const Attrib *a)
679 static struct timeval tv[2];
681 tv[0].tv_sec = a->atime;
683 tv[1].tv_sec = a->mtime;
689 process_setstat(void)
694 int status = SSH2_FX_OK, ret;
697 name = get_string(NULL);
699 debug("request %u: setstat name \"%s\"", id, name);
700 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
701 logit("set \"%s\" size %llu", name, a->size);
702 ret = truncate(name, a->size);
704 status = errno_to_portable(errno);
706 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
707 logit("set \"%s\" mode %04o", name, a->perm);
708 ret = chmod(name, a->perm & 0777);
710 status = errno_to_portable(errno);
712 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
716 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
718 logit("set \"%s\" modtime %s", name, buf);
719 ret = utimes(name, attrib_to_tv(a));
721 status = errno_to_portable(errno);
723 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
724 logit("set \"%s\" owner %lu group %lu", name,
725 (u_long)a->uid, (u_long)a->gid);
726 ret = chown(name, a->uid, a->gid);
728 status = errno_to_portable(errno);
730 send_status(id, status);
735 process_fsetstat(void)
740 int status = SSH2_FX_OK;
743 handle = get_handle();
745 debug("request %u: fsetstat handle %d", id, handle);
746 fd = handle_to_fd(handle);
748 status = SSH2_FX_FAILURE;
750 char *name = handle_to_name(handle);
752 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
753 logit("set \"%s\" size %llu", name, a->size);
754 ret = ftruncate(fd, a->size);
756 status = errno_to_portable(errno);
758 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
759 logit("set \"%s\" mode %04o", name, a->perm);
761 ret = fchmod(fd, a->perm & 0777);
763 ret = chmod(name, a->perm & 0777);
766 status = errno_to_portable(errno);
768 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
772 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
774 logit("set \"%s\" modtime %s", name, buf);
776 ret = futimes(fd, attrib_to_tv(a));
778 ret = utimes(name, attrib_to_tv(a));
781 status = errno_to_portable(errno);
783 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
784 logit("set \"%s\" owner %lu group %lu", name,
785 (u_long)a->uid, (u_long)a->gid);
787 ret = fchown(fd, a->uid, a->gid);
789 ret = chown(name, a->uid, a->gid);
792 status = errno_to_portable(errno);
795 send_status(id, status);
799 process_opendir(void)
803 int handle, status = SSH2_FX_FAILURE;
807 path = get_string(NULL);
808 debug3("request %u: opendir", id);
809 logit("opendir \"%s\"", path);
810 dirp = opendir(path);
812 status = errno_to_portable(errno);
814 handle = handle_new(HANDLE_DIR, path, 0, dirp);
818 send_handle(id, handle);
823 if (status != SSH2_FX_OK)
824 send_status(id, status);
829 process_readdir(void)
838 handle = get_handle();
839 debug("request %u: readdir \"%s\" (handle %d)", id,
840 handle_to_name(handle), handle);
841 dirp = handle_to_dir(handle);
842 path = handle_to_name(handle);
843 if (dirp == NULL || path == NULL) {
844 send_status(id, SSH2_FX_FAILURE);
847 char pathname[MAXPATHLEN];
849 int nstats = 10, count = 0, i;
851 stats = xcalloc(nstats, sizeof(Stat));
852 while ((dp = readdir(dirp)) != NULL) {
853 if (count >= nstats) {
855 stats = xrealloc(stats, nstats, sizeof(Stat));
858 snprintf(pathname, sizeof pathname, "%s%s%s", path,
859 strcmp(path, "/") ? "/" : "", dp->d_name);
860 if (lstat(pathname, &st) < 0)
862 stat_to_attrib(&st, &(stats[count].attrib));
863 stats[count].name = xstrdup(dp->d_name);
864 stats[count].long_name = ls_file(dp->d_name, &st, 0);
866 /* send up to 100 entries in one message */
867 /* XXX check packet size instead */
872 send_names(id, count, stats);
873 for (i = 0; i < count; i++) {
874 xfree(stats[i].name);
875 xfree(stats[i].long_name);
878 send_status(id, SSH2_FX_EOF);
889 int status = SSH2_FX_FAILURE;
893 name = get_string(NULL);
894 debug3("request %u: remove", id);
895 logit("remove name \"%s\"", name);
897 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
898 send_status(id, status);
908 int ret, mode, status = SSH2_FX_FAILURE;
911 name = get_string(NULL);
913 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
914 a->perm & 0777 : 0777;
915 debug3("request %u: mkdir", id);
916 logit("mkdir name \"%s\" mode 0%o", name, mode);
917 ret = mkdir(name, mode);
918 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
919 send_status(id, status);
931 name = get_string(NULL);
932 debug3("request %u: rmdir", id);
933 logit("rmdir name \"%s\"", name);
935 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
936 send_status(id, status);
941 process_realpath(void)
943 char resolvedname[MAXPATHLEN];
948 path = get_string(NULL);
949 if (path[0] == '\0') {
953 debug3("request %u: realpath", id);
954 verbose("realpath \"%s\"", path);
955 if (realpath(path, resolvedname) == NULL) {
956 send_status(id, errno_to_portable(errno));
959 attrib_clear(&s.attrib);
960 s.name = s.long_name = resolvedname;
961 send_names(id, 1, &s);
970 char *oldpath, *newpath;
975 oldpath = get_string(NULL);
976 newpath = get_string(NULL);
977 debug3("request %u: rename", id);
978 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
979 status = SSH2_FX_FAILURE;
980 if (lstat(oldpath, &sb) == -1)
981 status = errno_to_portable(errno);
982 else if (S_ISREG(sb.st_mode)) {
983 /* Race-free rename of regular files */
984 if (link(oldpath, newpath) == -1) {
985 if (errno == EOPNOTSUPP
986 #ifdef LINK_OPNOTSUPP_ERRNO
987 || errno == LINK_OPNOTSUPP_ERRNO
993 * fs doesn't support links, so fall back to
994 * stat+rename. This is racy.
996 if (stat(newpath, &st) == -1) {
997 if (rename(oldpath, newpath) == -1)
999 errno_to_portable(errno);
1001 status = SSH2_FX_OK;
1004 status = errno_to_portable(errno);
1006 } else if (unlink(oldpath) == -1) {
1007 status = errno_to_portable(errno);
1008 /* clean spare link */
1011 status = SSH2_FX_OK;
1012 } else if (stat(newpath, &sb) == -1) {
1013 if (rename(oldpath, newpath) == -1)
1014 status = errno_to_portable(errno);
1016 status = SSH2_FX_OK;
1018 send_status(id, status);
1024 process_readlink(void)
1028 char buf[MAXPATHLEN];
1032 path = get_string(NULL);
1033 debug3("request %u: readlink", id);
1034 verbose("readlink \"%s\"", path);
1035 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
1036 send_status(id, errno_to_portable(errno));
1041 attrib_clear(&s.attrib);
1042 s.name = s.long_name = buf;
1043 send_names(id, 1, &s);
1049 process_symlink(void)
1052 char *oldpath, *newpath;
1056 oldpath = get_string(NULL);
1057 newpath = get_string(NULL);
1058 debug3("request %u: symlink", id);
1059 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
1060 /* this will fail if 'newpath' exists */
1061 ret = symlink(oldpath, newpath);
1062 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1063 send_status(id, status);
1069 process_extended(void)
1075 request = get_string(NULL);
1076 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1080 /* stolen from ssh-agent */
1091 buf_len = buffer_len(&iqueue);
1093 return; /* Incomplete message. */
1094 cp = buffer_ptr(&iqueue);
1095 msg_len = get_u32(cp);
1096 if (msg_len > SFTP_MAX_MSG_LENGTH) {
1097 error("bad message from %s local user %s",
1098 client_addr, pw->pw_name);
1101 if (buf_len < msg_len + 4)
1103 buffer_consume(&iqueue, 4);
1105 type = buffer_get_char(&iqueue);
1113 case SSH2_FXP_CLOSE:
1119 case SSH2_FXP_WRITE:
1122 case SSH2_FXP_LSTAT:
1125 case SSH2_FXP_FSTAT:
1128 case SSH2_FXP_SETSTAT:
1131 case SSH2_FXP_FSETSTAT:
1134 case SSH2_FXP_OPENDIR:
1137 case SSH2_FXP_READDIR:
1140 case SSH2_FXP_REMOVE:
1143 case SSH2_FXP_MKDIR:
1146 case SSH2_FXP_RMDIR:
1149 case SSH2_FXP_REALPATH:
1155 case SSH2_FXP_RENAME:
1158 case SSH2_FXP_READLINK:
1161 case SSH2_FXP_SYMLINK:
1164 case SSH2_FXP_EXTENDED:
1168 error("Unknown message %d", type);
1171 /* discard the remaining bytes from the current packet */
1172 if (buf_len < buffer_len(&iqueue))
1173 fatal("iqueue grew unexpectedly");
1174 consumed = buf_len - buffer_len(&iqueue);
1175 if (msg_len < consumed)
1176 fatal("msg_len %d < consumed %d", msg_len, consumed);
1177 if (msg_len > consumed)
1178 buffer_consume(&iqueue, msg_len - consumed);
1181 /* Cleanup handler that logs active handles upon normal exit */
1185 if (pw != NULL && client_addr != NULL) {
1187 logit("session closed for local user %s from [%s]",
1188 pw->pw_name, client_addr);
1196 extern char *__progname;
1199 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1204 main(int argc, char **argv)
1206 fd_set *rset, *wset;
1207 int in, out, max, ch, skipargs = 0, log_stderr = 0;
1208 ssize_t len, olen, set_size;
1209 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1212 extern char *optarg;
1213 extern char *__progname;
1215 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1218 __progname = ssh_get_progname(argv[0]);
1219 log_init(__progname, log_level, log_facility, log_stderr);
1221 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1225 * Ignore all arguments if we are invoked as a
1226 * shell using "sftp-server -c command"
1234 log_level = log_level_number(optarg);
1235 if (log_level == SYSLOG_LEVEL_NOT_SET)
1236 error("Invalid log level \"%s\"", optarg);
1239 log_facility = log_facility_number(optarg);
1240 if (log_level == SYSLOG_FACILITY_NOT_SET)
1241 error("Invalid log facility \"%s\"", optarg);
1249 log_init(__progname, log_level, log_facility, log_stderr);
1251 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1252 client_addr = xstrdup(cp);
1253 if ((cp = strchr(client_addr, ' ')) == NULL)
1254 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1255 getenv("SSH_CONNECTION"));
1258 client_addr = xstrdup("UNKNOWN");
1260 if ((pw = getpwuid(getuid())) == NULL)
1261 fatal("No user found for uid %lu", (u_long)getuid());
1264 logit("session opened for local user %s from [%s]",
1265 pw->pw_name, client_addr);
1269 in = dup(STDIN_FILENO);
1270 out = dup(STDOUT_FILENO);
1273 setmode(in, O_BINARY);
1274 setmode(out, O_BINARY);
1283 buffer_init(&iqueue);
1284 buffer_init(&oqueue);
1286 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1287 rset = (fd_set *)xmalloc(set_size);
1288 wset = (fd_set *)xmalloc(set_size);
1291 memset(rset, 0, set_size);
1292 memset(wset, 0, set_size);
1295 olen = buffer_len(&oqueue);
1299 if (select(max+1, rset, wset, NULL, NULL) < 0) {
1302 error("select: %s", strerror(errno));
1306 /* copy stdin to iqueue */
1307 if (FD_ISSET(in, rset)) {
1309 len = read(in, buf, sizeof buf);
1313 } else if (len < 0) {
1314 error("read: %s", strerror(errno));
1317 buffer_append(&iqueue, buf, len);
1320 /* send oqueue to stdout */
1321 if (FD_ISSET(out, wset)) {
1322 len = write(out, buffer_ptr(&oqueue), olen);
1324 error("write: %s", strerror(errno));
1327 buffer_consume(&oqueue, len);
1330 /* process requests from client */