1 /* $OpenBSD: sftp-server.c,v 1.68 2006/07/26 13:57: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>
43 #include "sftp-common.h"
46 #define get_int64() buffer_get_int64(&iqueue);
47 #define get_int() buffer_get_int(&iqueue);
48 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
51 LogLevel log_level = SYSLOG_LEVEL_ERROR;
54 struct passwd *pw = NULL;
55 char *client_addr = NULL;
57 /* input and output queue */
61 /* Version of client */
64 /* portable attributes, etc. */
66 typedef struct Stat Stat;
75 errno_to_portable(int unixerrno)
87 ret = SSH2_FX_NO_SUCH_FILE;
92 ret = SSH2_FX_PERMISSION_DENIED;
96 ret = SSH2_FX_BAD_MESSAGE;
99 ret = SSH2_FX_FAILURE;
106 flags_from_portable(int pflags)
110 if ((pflags & SSH2_FXF_READ) &&
111 (pflags & SSH2_FXF_WRITE)) {
113 } else if (pflags & SSH2_FXF_READ) {
115 } else if (pflags & SSH2_FXF_WRITE) {
118 if (pflags & SSH2_FXF_CREAT)
120 if (pflags & SSH2_FXF_TRUNC)
122 if (pflags & SSH2_FXF_EXCL)
128 string_from_portable(int pflags)
130 static char ret[128];
134 #define PAPPEND(str) { \
136 strlcat(ret, ",", sizeof(ret)); \
137 strlcat(ret, str, sizeof(ret)); \
140 if (pflags & SSH2_FXF_READ)
142 if (pflags & SSH2_FXF_WRITE)
144 if (pflags & SSH2_FXF_CREAT)
146 if (pflags & SSH2_FXF_TRUNC)
148 if (pflags & SSH2_FXF_EXCL)
157 return decode_attrib(&iqueue);
162 typedef struct Handle Handle;
168 u_int64_t bytes_read, bytes_write;
184 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
185 handles[i].use = HANDLE_UNUSED;
189 handle_new(int use, const char *name, int fd, DIR *dirp)
193 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
194 if (handles[i].use == HANDLE_UNUSED) {
195 handles[i].use = use;
196 handles[i].dirp = dirp;
198 handles[i].name = xstrdup(name);
199 handles[i].bytes_read = handles[i].bytes_write = 0;
207 handle_is_ok(int i, int type)
209 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
210 handles[i].use == type;
214 handle_to_string(int handle, char **stringp, int *hlenp)
216 if (stringp == NULL || hlenp == NULL)
218 *stringp = xmalloc(sizeof(int32_t));
219 put_u32(*stringp, handle);
220 *hlenp = sizeof(int32_t);
225 handle_from_string(const char *handle, u_int hlen)
229 if (hlen != sizeof(int32_t))
231 val = get_u32(handle);
232 if (handle_is_ok(val, HANDLE_FILE) ||
233 handle_is_ok(val, HANDLE_DIR))
239 handle_to_name(int handle)
241 if (handle_is_ok(handle, HANDLE_DIR)||
242 handle_is_ok(handle, HANDLE_FILE))
243 return handles[handle].name;
248 handle_to_dir(int handle)
250 if (handle_is_ok(handle, HANDLE_DIR))
251 return handles[handle].dirp;
256 handle_to_fd(int handle)
258 if (handle_is_ok(handle, HANDLE_FILE))
259 return handles[handle].fd;
264 handle_update_read(int handle, ssize_t bytes)
266 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
267 handles[handle].bytes_read += bytes;
271 handle_update_write(int handle, ssize_t bytes)
273 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
274 handles[handle].bytes_write += bytes;
278 handle_bytes_read(int handle)
280 if (handle_is_ok(handle, HANDLE_FILE))
281 return (handles[handle].bytes_read);
286 handle_bytes_write(int handle)
288 if (handle_is_ok(handle, HANDLE_FILE))
289 return (handles[handle].bytes_write);
294 handle_close(int handle)
298 if (handle_is_ok(handle, HANDLE_FILE)) {
299 ret = close(handles[handle].fd);
300 handles[handle].use = HANDLE_UNUSED;
301 xfree(handles[handle].name);
302 } else if (handle_is_ok(handle, HANDLE_DIR)) {
303 ret = closedir(handles[handle].dirp);
304 handles[handle].use = HANDLE_UNUSED;
305 xfree(handles[handle].name);
313 handle_log_close(int handle, char *emsg)
315 if (handle_is_ok(handle, HANDLE_FILE)) {
316 logit("%s%sclose \"%s\" bytes read %llu written %llu",
317 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
318 handle_to_name(handle),
319 handle_bytes_read(handle), handle_bytes_write(handle));
321 logit("%s%sclosedir \"%s\"",
322 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
323 handle_to_name(handle));
328 handle_log_exit(void)
332 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
333 if (handles[i].use != HANDLE_UNUSED)
334 handle_log_close(i, "forced");
344 handle = get_string(&hlen);
346 val = handle_from_string(handle, hlen);
356 int mlen = buffer_len(m);
358 buffer_put_int(&oqueue, mlen);
359 buffer_append(&oqueue, buffer_ptr(m), mlen);
360 buffer_consume(m, mlen);
364 status_to_message(u_int32_t status)
366 const char *status_messages[] = {
367 "Success", /* SSH_FX_OK */
368 "End of file", /* SSH_FX_EOF */
369 "No such file", /* SSH_FX_NO_SUCH_FILE */
370 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
371 "Failure", /* SSH_FX_FAILURE */
372 "Bad message", /* SSH_FX_BAD_MESSAGE */
373 "No connection", /* SSH_FX_NO_CONNECTION */
374 "Connection lost", /* SSH_FX_CONNECTION_LOST */
375 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
376 "Unknown error" /* Others */
378 return (status_messages[MIN(status,SSH2_FX_MAX)]);
382 send_status(u_int32_t id, u_int32_t status)
386 debug3("request %u: sent status %u", id, status);
387 if (log_level > SYSLOG_LEVEL_VERBOSE ||
388 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
389 logit("sent status %s", status_to_message(status));
391 buffer_put_char(&msg, SSH2_FXP_STATUS);
392 buffer_put_int(&msg, id);
393 buffer_put_int(&msg, status);
395 buffer_put_cstring(&msg, status_to_message(status));
396 buffer_put_cstring(&msg, "");
402 send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
407 buffer_put_char(&msg, type);
408 buffer_put_int(&msg, id);
409 buffer_put_string(&msg, data, dlen);
415 send_data(u_int32_t id, const char *data, int dlen)
417 debug("request %u: sent data len %d", id, dlen);
418 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
422 send_handle(u_int32_t id, int handle)
427 handle_to_string(handle, &string, &hlen);
428 debug("request %u: sent handle handle %d", id, handle);
429 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
434 send_names(u_int32_t id, int count, const Stat *stats)
440 buffer_put_char(&msg, SSH2_FXP_NAME);
441 buffer_put_int(&msg, id);
442 buffer_put_int(&msg, count);
443 debug("request %u: sent names count %d", id, count);
444 for (i = 0; i < count; i++) {
445 buffer_put_cstring(&msg, stats[i].name);
446 buffer_put_cstring(&msg, stats[i].long_name);
447 encode_attrib(&msg, &stats[i].attrib);
454 send_attrib(u_int32_t id, const Attrib *a)
458 debug("request %u: sent attrib have 0x%x", id, a->flags);
460 buffer_put_char(&msg, SSH2_FXP_ATTRS);
461 buffer_put_int(&msg, id);
462 encode_attrib(&msg, a);
475 verbose("received client version %d", version);
477 buffer_put_char(&msg, SSH2_FXP_VERSION);
478 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
486 u_int32_t id, pflags;
489 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
492 name = get_string(NULL);
493 pflags = get_int(); /* portable flags */
494 debug3("request %u: open flags %d", id, pflags);
496 flags = flags_from_portable(pflags);
497 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
498 logit("open \"%s\" flags %s mode 0%o",
499 name, string_from_portable(pflags), mode);
500 fd = open(name, flags, mode);
502 status = errno_to_portable(errno);
504 handle = handle_new(HANDLE_FILE, name, fd, NULL);
508 send_handle(id, handle);
512 if (status != SSH2_FX_OK)
513 send_status(id, status);
521 int handle, ret, status = SSH2_FX_FAILURE;
524 handle = get_handle();
525 debug3("request %u: close handle %u", id, handle);
526 handle_log_close(handle, NULL);
527 ret = handle_close(handle);
528 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
529 send_status(id, status);
537 int handle, fd, ret, status = SSH2_FX_FAILURE;
541 handle = get_handle();
545 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
546 id, handle_to_name(handle), handle, (unsigned long long)off, len);
547 if (len > sizeof buf) {
549 debug2("read change len %d", len);
551 fd = handle_to_fd(handle);
553 if (lseek(fd, off, SEEK_SET) < 0) {
554 error("process_read: seek failed");
555 status = errno_to_portable(errno);
557 ret = read(fd, buf, len);
559 status = errno_to_portable(errno);
560 } else if (ret == 0) {
561 status = SSH2_FX_EOF;
563 send_data(id, buf, ret);
565 handle_update_read(handle, ret);
569 if (status != SSH2_FX_OK)
570 send_status(id, status);
579 int handle, fd, ret, status = SSH2_FX_FAILURE;
583 handle = get_handle();
585 data = get_string(&len);
587 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
588 id, handle_to_name(handle), handle, (unsigned long long)off, len);
589 fd = handle_to_fd(handle);
591 if (lseek(fd, off, SEEK_SET) < 0) {
592 status = errno_to_portable(errno);
593 error("process_write: seek failed");
596 ret = write(fd, data, len);
598 error("process_write: write failed");
599 status = errno_to_portable(errno);
600 } else if ((size_t)ret == len) {
602 handle_update_write(handle, ret);
604 debug2("nothing at all written");
608 send_status(id, status);
613 process_do_stat(int do_lstat)
619 int ret, status = SSH2_FX_FAILURE;
622 name = get_string(NULL);
623 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
624 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
625 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
627 status = errno_to_portable(errno);
629 stat_to_attrib(&st, &a);
633 if (status != SSH2_FX_OK)
634 send_status(id, status);
656 int fd, ret, handle, status = SSH2_FX_FAILURE;
659 handle = get_handle();
660 debug("request %u: fstat \"%s\" (handle %u)",
661 id, handle_to_name(handle), handle);
662 fd = handle_to_fd(handle);
664 ret = fstat(fd, &st);
666 status = errno_to_portable(errno);
668 stat_to_attrib(&st, &a);
673 if (status != SSH2_FX_OK)
674 send_status(id, status);
677 static struct timeval *
678 attrib_to_tv(const Attrib *a)
680 static struct timeval tv[2];
682 tv[0].tv_sec = a->atime;
684 tv[1].tv_sec = a->mtime;
690 process_setstat(void)
695 int status = SSH2_FX_OK, ret;
698 name = get_string(NULL);
700 debug("request %u: setstat name \"%s\"", id, name);
701 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
702 logit("set \"%s\" size %llu", name, a->size);
703 ret = truncate(name, a->size);
705 status = errno_to_portable(errno);
707 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
708 logit("set \"%s\" mode %04o", name, a->perm);
709 ret = chmod(name, a->perm & 0777);
711 status = errno_to_portable(errno);
713 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
717 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
719 logit("set \"%s\" modtime %s", name, buf);
720 ret = utimes(name, attrib_to_tv(a));
722 status = errno_to_portable(errno);
724 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
725 logit("set \"%s\" owner %lu group %lu", name,
726 (u_long)a->uid, (u_long)a->gid);
727 ret = chown(name, a->uid, a->gid);
729 status = errno_to_portable(errno);
731 send_status(id, status);
736 process_fsetstat(void)
741 int status = SSH2_FX_OK;
744 handle = get_handle();
746 debug("request %u: fsetstat handle %d", id, handle);
747 fd = handle_to_fd(handle);
749 status = SSH2_FX_FAILURE;
751 char *name = handle_to_name(handle);
753 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
754 logit("set \"%s\" size %llu", name, a->size);
755 ret = ftruncate(fd, a->size);
757 status = errno_to_portable(errno);
759 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
760 logit("set \"%s\" mode %04o", name, a->perm);
762 ret = fchmod(fd, a->perm & 0777);
764 ret = chmod(name, a->perm & 0777);
767 status = errno_to_portable(errno);
769 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
773 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
775 logit("set \"%s\" modtime %s", name, buf);
777 ret = futimes(fd, attrib_to_tv(a));
779 ret = utimes(name, attrib_to_tv(a));
782 status = errno_to_portable(errno);
784 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
785 logit("set \"%s\" owner %lu group %lu", name,
786 (u_long)a->uid, (u_long)a->gid);
788 ret = fchown(fd, a->uid, a->gid);
790 ret = chown(name, a->uid, a->gid);
793 status = errno_to_portable(errno);
796 send_status(id, status);
800 process_opendir(void)
804 int handle, status = SSH2_FX_FAILURE;
808 path = get_string(NULL);
809 debug3("request %u: opendir", id);
810 logit("opendir \"%s\"", path);
811 dirp = opendir(path);
813 status = errno_to_portable(errno);
815 handle = handle_new(HANDLE_DIR, path, 0, dirp);
819 send_handle(id, handle);
824 if (status != SSH2_FX_OK)
825 send_status(id, status);
830 process_readdir(void)
839 handle = get_handle();
840 debug("request %u: readdir \"%s\" (handle %d)", id,
841 handle_to_name(handle), handle);
842 dirp = handle_to_dir(handle);
843 path = handle_to_name(handle);
844 if (dirp == NULL || path == NULL) {
845 send_status(id, SSH2_FX_FAILURE);
848 char pathname[MAXPATHLEN];
850 int nstats = 10, count = 0, i;
852 stats = xcalloc(nstats, sizeof(Stat));
853 while ((dp = readdir(dirp)) != NULL) {
854 if (count >= nstats) {
856 stats = xrealloc(stats, nstats, sizeof(Stat));
859 snprintf(pathname, sizeof pathname, "%s%s%s", path,
860 strcmp(path, "/") ? "/" : "", dp->d_name);
861 if (lstat(pathname, &st) < 0)
863 stat_to_attrib(&st, &(stats[count].attrib));
864 stats[count].name = xstrdup(dp->d_name);
865 stats[count].long_name = ls_file(dp->d_name, &st, 0);
867 /* send up to 100 entries in one message */
868 /* XXX check packet size instead */
873 send_names(id, count, stats);
874 for (i = 0; i < count; i++) {
875 xfree(stats[i].name);
876 xfree(stats[i].long_name);
879 send_status(id, SSH2_FX_EOF);
890 int status = SSH2_FX_FAILURE;
894 name = get_string(NULL);
895 debug3("request %u: remove", id);
896 logit("remove name \"%s\"", name);
898 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
899 send_status(id, status);
909 int ret, mode, status = SSH2_FX_FAILURE;
912 name = get_string(NULL);
914 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
915 a->perm & 0777 : 0777;
916 debug3("request %u: mkdir", id);
917 logit("mkdir name \"%s\" mode 0%o", name, mode);
918 ret = mkdir(name, mode);
919 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
920 send_status(id, status);
932 name = get_string(NULL);
933 debug3("request %u: rmdir", id);
934 logit("rmdir name \"%s\"", name);
936 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
937 send_status(id, status);
942 process_realpath(void)
944 char resolvedname[MAXPATHLEN];
949 path = get_string(NULL);
950 if (path[0] == '\0') {
954 debug3("request %u: realpath", id);
955 verbose("realpath \"%s\"", path);
956 if (realpath(path, resolvedname) == NULL) {
957 send_status(id, errno_to_portable(errno));
960 attrib_clear(&s.attrib);
961 s.name = s.long_name = resolvedname;
962 send_names(id, 1, &s);
971 char *oldpath, *newpath;
976 oldpath = get_string(NULL);
977 newpath = get_string(NULL);
978 debug3("request %u: rename", id);
979 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
980 status = SSH2_FX_FAILURE;
981 if (lstat(oldpath, &sb) == -1)
982 status = errno_to_portable(errno);
983 else if (S_ISREG(sb.st_mode)) {
984 /* Race-free rename of regular files */
985 if (link(oldpath, newpath) == -1) {
986 if (errno == EOPNOTSUPP
987 #ifdef LINK_OPNOTSUPP_ERRNO
988 || errno == LINK_OPNOTSUPP_ERRNO
994 * fs doesn't support links, so fall back to
995 * stat+rename. This is racy.
997 if (stat(newpath, &st) == -1) {
998 if (rename(oldpath, newpath) == -1)
1000 errno_to_portable(errno);
1002 status = SSH2_FX_OK;
1005 status = errno_to_portable(errno);
1007 } else if (unlink(oldpath) == -1) {
1008 status = errno_to_portable(errno);
1009 /* clean spare link */
1012 status = SSH2_FX_OK;
1013 } else if (stat(newpath, &sb) == -1) {
1014 if (rename(oldpath, newpath) == -1)
1015 status = errno_to_portable(errno);
1017 status = SSH2_FX_OK;
1019 send_status(id, status);
1025 process_readlink(void)
1029 char buf[MAXPATHLEN];
1033 path = get_string(NULL);
1034 debug3("request %u: readlink", id);
1035 verbose("readlink \"%s\"", path);
1036 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
1037 send_status(id, errno_to_portable(errno));
1042 attrib_clear(&s.attrib);
1043 s.name = s.long_name = buf;
1044 send_names(id, 1, &s);
1050 process_symlink(void)
1053 char *oldpath, *newpath;
1057 oldpath = get_string(NULL);
1058 newpath = get_string(NULL);
1059 debug3("request %u: symlink", id);
1060 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
1061 /* this will fail if 'newpath' exists */
1062 ret = symlink(oldpath, newpath);
1063 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1064 send_status(id, status);
1070 process_extended(void)
1076 request = get_string(NULL);
1077 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1081 /* stolen from ssh-agent */
1092 buf_len = buffer_len(&iqueue);
1094 return; /* Incomplete message. */
1095 cp = buffer_ptr(&iqueue);
1096 msg_len = get_u32(cp);
1097 if (msg_len > SFTP_MAX_MSG_LENGTH) {
1098 error("bad message from %s local user %s",
1099 client_addr, pw->pw_name);
1102 if (buf_len < msg_len + 4)
1104 buffer_consume(&iqueue, 4);
1106 type = buffer_get_char(&iqueue);
1114 case SSH2_FXP_CLOSE:
1120 case SSH2_FXP_WRITE:
1123 case SSH2_FXP_LSTAT:
1126 case SSH2_FXP_FSTAT:
1129 case SSH2_FXP_SETSTAT:
1132 case SSH2_FXP_FSETSTAT:
1135 case SSH2_FXP_OPENDIR:
1138 case SSH2_FXP_READDIR:
1141 case SSH2_FXP_REMOVE:
1144 case SSH2_FXP_MKDIR:
1147 case SSH2_FXP_RMDIR:
1150 case SSH2_FXP_REALPATH:
1156 case SSH2_FXP_RENAME:
1159 case SSH2_FXP_READLINK:
1162 case SSH2_FXP_SYMLINK:
1165 case SSH2_FXP_EXTENDED:
1169 error("Unknown message %d", type);
1172 /* discard the remaining bytes from the current packet */
1173 if (buf_len < buffer_len(&iqueue))
1174 fatal("iqueue grew unexpectedly");
1175 consumed = buf_len - buffer_len(&iqueue);
1176 if (msg_len < consumed)
1177 fatal("msg_len %d < consumed %d", msg_len, consumed);
1178 if (msg_len > consumed)
1179 buffer_consume(&iqueue, msg_len - consumed);
1182 /* Cleanup handler that logs active handles upon normal exit */
1186 if (pw != NULL && client_addr != NULL) {
1188 logit("session closed for local user %s from [%s]",
1189 pw->pw_name, client_addr);
1197 extern char *__progname;
1200 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1205 main(int argc, char **argv)
1207 fd_set *rset, *wset;
1208 int in, out, max, ch, skipargs = 0, log_stderr = 0;
1209 ssize_t len, olen, set_size;
1210 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1213 extern char *optarg;
1214 extern char *__progname;
1216 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1219 __progname = ssh_get_progname(argv[0]);
1220 log_init(__progname, log_level, log_facility, log_stderr);
1222 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1226 * Ignore all arguments if we are invoked as a
1227 * shell using "sftp-server -c command"
1235 log_level = log_level_number(optarg);
1236 if (log_level == SYSLOG_LEVEL_NOT_SET)
1237 error("Invalid log level \"%s\"", optarg);
1240 log_facility = log_facility_number(optarg);
1241 if (log_level == SYSLOG_FACILITY_NOT_SET)
1242 error("Invalid log facility \"%s\"", optarg);
1250 log_init(__progname, log_level, log_facility, log_stderr);
1252 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1253 client_addr = xstrdup(cp);
1254 if ((cp = strchr(client_addr, ' ')) == NULL)
1255 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1256 getenv("SSH_CONNECTION"));
1259 client_addr = xstrdup("UNKNOWN");
1261 if ((pw = getpwuid(getuid())) == NULL)
1262 fatal("No user found for uid %lu", (u_long)getuid());
1265 logit("session opened for local user %s from [%s]",
1266 pw->pw_name, client_addr);
1270 in = dup(STDIN_FILENO);
1271 out = dup(STDOUT_FILENO);
1274 setmode(in, O_BINARY);
1275 setmode(out, O_BINARY);
1284 buffer_init(&iqueue);
1285 buffer_init(&oqueue);
1287 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1288 rset = (fd_set *)xmalloc(set_size);
1289 wset = (fd_set *)xmalloc(set_size);
1292 memset(rset, 0, set_size);
1293 memset(wset, 0, set_size);
1296 olen = buffer_len(&oqueue);
1300 if (select(max+1, rset, wset, NULL, NULL) < 0) {
1303 error("select: %s", strerror(errno));
1307 /* copy stdin to iqueue */
1308 if (FD_ISSET(in, rset)) {
1310 len = read(in, buf, sizeof buf);
1314 } else if (len < 0) {
1315 error("read: %s", strerror(errno));
1318 buffer_append(&iqueue, buf, len);
1321 /* send oqueue to stdout */
1322 if (FD_ISSET(out, wset)) {
1323 len = write(out, buffer_ptr(&oqueue), olen);
1325 error("write: %s", strerror(errno));
1328 buffer_consume(&oqueue, len);
1331 /* process requests from client */