1 /* $OpenBSD: sftp-server.c,v 1.70 2006/08/03 03:34:42 deraadt 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/types.h>
21 #include <sys/param.h>
23 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
46 #include "sftp-common.h"
49 #define get_int64() buffer_get_int64(&iqueue);
50 #define get_int() buffer_get_int(&iqueue);
51 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
54 LogLevel log_level
= SYSLOG_LEVEL_ERROR
;
57 struct passwd
*pw
= NULL
;
58 char *client_addr
= NULL
;
60 /* input and output queue */
64 /* Version of client */
67 /* portable attributes, etc. */
69 typedef struct Stat Stat
;
78 errno_to_portable(int unixerrno
)
90 ret
= SSH2_FX_NO_SUCH_FILE
;
95 ret
= SSH2_FX_PERMISSION_DENIED
;
99 ret
= SSH2_FX_BAD_MESSAGE
;
102 ret
= SSH2_FX_FAILURE
;
109 flags_from_portable(int pflags
)
113 if ((pflags
& SSH2_FXF_READ
) &&
114 (pflags
& SSH2_FXF_WRITE
)) {
116 } else if (pflags
& SSH2_FXF_READ
) {
118 } else if (pflags
& SSH2_FXF_WRITE
) {
121 if (pflags
& SSH2_FXF_CREAT
)
123 if (pflags
& SSH2_FXF_TRUNC
)
125 if (pflags
& SSH2_FXF_EXCL
)
131 string_from_portable(int pflags
)
133 static char ret
[128];
137 #define PAPPEND(str) { \
139 strlcat(ret, ",", sizeof(ret)); \
140 strlcat(ret, str, sizeof(ret)); \
143 if (pflags
& SSH2_FXF_READ
)
145 if (pflags
& SSH2_FXF_WRITE
)
147 if (pflags
& SSH2_FXF_CREAT
)
149 if (pflags
& SSH2_FXF_TRUNC
)
151 if (pflags
& SSH2_FXF_EXCL
)
160 return decode_attrib(&iqueue
);
165 typedef struct Handle Handle
;
171 u_int64_t bytes_read
, bytes_write
;
187 for (i
= 0; i
< sizeof(handles
)/sizeof(Handle
); i
++)
188 handles
[i
].use
= HANDLE_UNUSED
;
192 handle_new(int use
, const char *name
, int fd
, DIR *dirp
)
196 for (i
= 0; i
< sizeof(handles
)/sizeof(Handle
); i
++) {
197 if (handles
[i
].use
== HANDLE_UNUSED
) {
198 handles
[i
].use
= use
;
199 handles
[i
].dirp
= dirp
;
201 handles
[i
].name
= xstrdup(name
);
202 handles
[i
].bytes_read
= handles
[i
].bytes_write
= 0;
210 handle_is_ok(int i
, int type
)
212 return i
>= 0 && (u_int
)i
< sizeof(handles
)/sizeof(Handle
) &&
213 handles
[i
].use
== type
;
217 handle_to_string(int handle
, char **stringp
, int *hlenp
)
219 if (stringp
== NULL
|| hlenp
== NULL
)
221 *stringp
= xmalloc(sizeof(int32_t));
222 put_u32(*stringp
, handle
);
223 *hlenp
= sizeof(int32_t);
228 handle_from_string(const char *handle
, u_int hlen
)
232 if (hlen
!= sizeof(int32_t))
234 val
= get_u32(handle
);
235 if (handle_is_ok(val
, HANDLE_FILE
) ||
236 handle_is_ok(val
, HANDLE_DIR
))
242 handle_to_name(int handle
)
244 if (handle_is_ok(handle
, HANDLE_DIR
)||
245 handle_is_ok(handle
, HANDLE_FILE
))
246 return handles
[handle
].name
;
251 handle_to_dir(int handle
)
253 if (handle_is_ok(handle
, HANDLE_DIR
))
254 return handles
[handle
].dirp
;
259 handle_to_fd(int handle
)
261 if (handle_is_ok(handle
, HANDLE_FILE
))
262 return handles
[handle
].fd
;
267 handle_update_read(int handle
, ssize_t bytes
)
269 if (handle_is_ok(handle
, HANDLE_FILE
) && bytes
> 0)
270 handles
[handle
].bytes_read
+= bytes
;
274 handle_update_write(int handle
, ssize_t bytes
)
276 if (handle_is_ok(handle
, HANDLE_FILE
) && bytes
> 0)
277 handles
[handle
].bytes_write
+= bytes
;
281 handle_bytes_read(int handle
)
283 if (handle_is_ok(handle
, HANDLE_FILE
))
284 return (handles
[handle
].bytes_read
);
289 handle_bytes_write(int handle
)
291 if (handle_is_ok(handle
, HANDLE_FILE
))
292 return (handles
[handle
].bytes_write
);
297 handle_close(int handle
)
301 if (handle_is_ok(handle
, HANDLE_FILE
)) {
302 ret
= close(handles
[handle
].fd
);
303 handles
[handle
].use
= HANDLE_UNUSED
;
304 xfree(handles
[handle
].name
);
305 } else if (handle_is_ok(handle
, HANDLE_DIR
)) {
306 ret
= closedir(handles
[handle
].dirp
);
307 handles
[handle
].use
= HANDLE_UNUSED
;
308 xfree(handles
[handle
].name
);
316 handle_log_close(int handle
, char *emsg
)
318 if (handle_is_ok(handle
, HANDLE_FILE
)) {
319 logit("%s%sclose \"%s\" bytes read %llu written %llu",
320 emsg
== NULL
? "" : emsg
, emsg
== NULL
? "" : " ",
321 handle_to_name(handle
),
322 handle_bytes_read(handle
), handle_bytes_write(handle
));
324 logit("%s%sclosedir \"%s\"",
325 emsg
== NULL
? "" : emsg
, emsg
== NULL
? "" : " ",
326 handle_to_name(handle
));
331 handle_log_exit(void)
335 for (i
= 0; i
< sizeof(handles
)/sizeof(Handle
); i
++)
336 if (handles
[i
].use
!= HANDLE_UNUSED
)
337 handle_log_close(i
, "forced");
347 handle
= get_string(&hlen
);
349 val
= handle_from_string(handle
, hlen
);
359 int mlen
= buffer_len(m
);
361 buffer_put_int(&oqueue
, mlen
);
362 buffer_append(&oqueue
, buffer_ptr(m
), mlen
);
363 buffer_consume(m
, mlen
);
367 status_to_message(u_int32_t status
)
369 const char *status_messages
[] = {
370 "Success", /* SSH_FX_OK */
371 "End of file", /* SSH_FX_EOF */
372 "No such file", /* SSH_FX_NO_SUCH_FILE */
373 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
374 "Failure", /* SSH_FX_FAILURE */
375 "Bad message", /* SSH_FX_BAD_MESSAGE */
376 "No connection", /* SSH_FX_NO_CONNECTION */
377 "Connection lost", /* SSH_FX_CONNECTION_LOST */
378 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
379 "Unknown error" /* Others */
381 return (status_messages
[MIN(status
,SSH2_FX_MAX
)]);
385 send_status(u_int32_t id
, u_int32_t status
)
389 debug3("request %u: sent status %u", id
, status
);
390 if (log_level
> SYSLOG_LEVEL_VERBOSE
||
391 (status
!= SSH2_FX_OK
&& status
!= SSH2_FX_EOF
))
392 logit("sent status %s", status_to_message(status
));
394 buffer_put_char(&msg
, SSH2_FXP_STATUS
);
395 buffer_put_int(&msg
, id
);
396 buffer_put_int(&msg
, status
);
398 buffer_put_cstring(&msg
, status_to_message(status
));
399 buffer_put_cstring(&msg
, "");
405 send_data_or_handle(char type
, u_int32_t id
, const char *data
, int dlen
)
410 buffer_put_char(&msg
, type
);
411 buffer_put_int(&msg
, id
);
412 buffer_put_string(&msg
, data
, dlen
);
418 send_data(u_int32_t id
, const char *data
, int dlen
)
420 debug("request %u: sent data len %d", id
, dlen
);
421 send_data_or_handle(SSH2_FXP_DATA
, id
, data
, dlen
);
425 send_handle(u_int32_t id
, int handle
)
430 handle_to_string(handle
, &string
, &hlen
);
431 debug("request %u: sent handle handle %d", id
, handle
);
432 send_data_or_handle(SSH2_FXP_HANDLE
, id
, string
, hlen
);
437 send_names(u_int32_t id
, int count
, const Stat
*stats
)
443 buffer_put_char(&msg
, SSH2_FXP_NAME
);
444 buffer_put_int(&msg
, id
);
445 buffer_put_int(&msg
, count
);
446 debug("request %u: sent names count %d", id
, count
);
447 for (i
= 0; i
< count
; i
++) {
448 buffer_put_cstring(&msg
, stats
[i
].name
);
449 buffer_put_cstring(&msg
, stats
[i
].long_name
);
450 encode_attrib(&msg
, &stats
[i
].attrib
);
457 send_attrib(u_int32_t id
, const Attrib
*a
)
461 debug("request %u: sent attrib have 0x%x", id
, a
->flags
);
463 buffer_put_char(&msg
, SSH2_FXP_ATTRS
);
464 buffer_put_int(&msg
, id
);
465 encode_attrib(&msg
, a
);
478 verbose("received client version %d", version
);
480 buffer_put_char(&msg
, SSH2_FXP_VERSION
);
481 buffer_put_int(&msg
, SSH2_FILEXFER_VERSION
);
489 u_int32_t id
, pflags
;
492 int handle
, fd
, flags
, mode
, status
= SSH2_FX_FAILURE
;
495 name
= get_string(NULL
);
496 pflags
= get_int(); /* portable flags */
497 debug3("request %u: open flags %d", id
, pflags
);
499 flags
= flags_from_portable(pflags
);
500 mode
= (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) ? a
->perm
: 0666;
501 logit("open \"%s\" flags %s mode 0%o",
502 name
, string_from_portable(pflags
), mode
);
503 fd
= open(name
, flags
, mode
);
505 status
= errno_to_portable(errno
);
507 handle
= handle_new(HANDLE_FILE
, name
, fd
, NULL
);
511 send_handle(id
, handle
);
515 if (status
!= SSH2_FX_OK
)
516 send_status(id
, status
);
524 int handle
, ret
, status
= SSH2_FX_FAILURE
;
527 handle
= get_handle();
528 debug3("request %u: close handle %u", id
, handle
);
529 handle_log_close(handle
, NULL
);
530 ret
= handle_close(handle
);
531 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
532 send_status(id
, status
);
540 int handle
, fd
, ret
, status
= SSH2_FX_FAILURE
;
544 handle
= get_handle();
548 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
549 id
, handle_to_name(handle
), handle
, (unsigned long long)off
, len
);
550 if (len
> sizeof buf
) {
552 debug2("read change len %d", len
);
554 fd
= handle_to_fd(handle
);
556 if (lseek(fd
, off
, SEEK_SET
) < 0) {
557 error("process_read: seek failed");
558 status
= errno_to_portable(errno
);
560 ret
= read(fd
, buf
, len
);
562 status
= errno_to_portable(errno
);
563 } else if (ret
== 0) {
564 status
= SSH2_FX_EOF
;
566 send_data(id
, buf
, ret
);
568 handle_update_read(handle
, ret
);
572 if (status
!= SSH2_FX_OK
)
573 send_status(id
, status
);
582 int handle
, fd
, ret
, status
= SSH2_FX_FAILURE
;
586 handle
= get_handle();
588 data
= get_string(&len
);
590 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
591 id
, handle_to_name(handle
), handle
, (unsigned long long)off
, len
);
592 fd
= handle_to_fd(handle
);
594 if (lseek(fd
, off
, SEEK_SET
) < 0) {
595 status
= errno_to_portable(errno
);
596 error("process_write: seek failed");
599 ret
= write(fd
, data
, len
);
601 error("process_write: write failed");
602 status
= errno_to_portable(errno
);
603 } else if ((size_t)ret
== len
) {
605 handle_update_write(handle
, ret
);
607 debug2("nothing at all written");
611 send_status(id
, status
);
616 process_do_stat(int do_lstat
)
622 int ret
, status
= SSH2_FX_FAILURE
;
625 name
= get_string(NULL
);
626 debug3("request %u: %sstat", id
, do_lstat
? "l" : "");
627 verbose("%sstat name \"%s\"", do_lstat
? "l" : "", name
);
628 ret
= do_lstat
? lstat(name
, &st
) : stat(name
, &st
);
630 status
= errno_to_portable(errno
);
632 stat_to_attrib(&st
, &a
);
636 if (status
!= SSH2_FX_OK
)
637 send_status(id
, status
);
659 int fd
, ret
, handle
, status
= SSH2_FX_FAILURE
;
662 handle
= get_handle();
663 debug("request %u: fstat \"%s\" (handle %u)",
664 id
, handle_to_name(handle
), handle
);
665 fd
= handle_to_fd(handle
);
667 ret
= fstat(fd
, &st
);
669 status
= errno_to_portable(errno
);
671 stat_to_attrib(&st
, &a
);
676 if (status
!= SSH2_FX_OK
)
677 send_status(id
, status
);
680 static struct timeval
*
681 attrib_to_tv(const Attrib
*a
)
683 static struct timeval tv
[2];
685 tv
[0].tv_sec
= a
->atime
;
687 tv
[1].tv_sec
= a
->mtime
;
693 process_setstat(void)
698 int status
= SSH2_FX_OK
, ret
;
701 name
= get_string(NULL
);
703 debug("request %u: setstat name \"%s\"", id
, name
);
704 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
) {
705 logit("set \"%s\" size %llu", name
, a
->size
);
706 ret
= truncate(name
, a
->size
);
708 status
= errno_to_portable(errno
);
710 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
711 logit("set \"%s\" mode %04o", name
, a
->perm
);
712 ret
= chmod(name
, a
->perm
& 0777);
714 status
= errno_to_portable(errno
);
716 if (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
) {
720 strftime(buf
, sizeof(buf
), "%Y%m%d-%H:%M:%S",
722 logit("set \"%s\" modtime %s", name
, buf
);
723 ret
= utimes(name
, attrib_to_tv(a
));
725 status
= errno_to_portable(errno
);
727 if (a
->flags
& SSH2_FILEXFER_ATTR_UIDGID
) {
728 logit("set \"%s\" owner %lu group %lu", name
,
729 (u_long
)a
->uid
, (u_long
)a
->gid
);
730 ret
= chown(name
, a
->uid
, a
->gid
);
732 status
= errno_to_portable(errno
);
734 send_status(id
, status
);
739 process_fsetstat(void)
744 int status
= SSH2_FX_OK
;
747 handle
= get_handle();
749 debug("request %u: fsetstat handle %d", id
, handle
);
750 fd
= handle_to_fd(handle
);
752 status
= SSH2_FX_FAILURE
;
754 char *name
= handle_to_name(handle
);
756 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
) {
757 logit("set \"%s\" size %llu", name
, a
->size
);
758 ret
= ftruncate(fd
, a
->size
);
760 status
= errno_to_portable(errno
);
762 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
763 logit("set \"%s\" mode %04o", name
, a
->perm
);
765 ret
= fchmod(fd
, a
->perm
& 0777);
767 ret
= chmod(name
, a
->perm
& 0777);
770 status
= errno_to_portable(errno
);
772 if (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
) {
776 strftime(buf
, sizeof(buf
), "%Y%m%d-%H:%M:%S",
778 logit("set \"%s\" modtime %s", name
, buf
);
780 ret
= futimes(fd
, attrib_to_tv(a
));
782 ret
= utimes(name
, attrib_to_tv(a
));
785 status
= errno_to_portable(errno
);
787 if (a
->flags
& SSH2_FILEXFER_ATTR_UIDGID
) {
788 logit("set \"%s\" owner %lu group %lu", name
,
789 (u_long
)a
->uid
, (u_long
)a
->gid
);
791 ret
= fchown(fd
, a
->uid
, a
->gid
);
793 ret
= chown(name
, a
->uid
, a
->gid
);
796 status
= errno_to_portable(errno
);
799 send_status(id
, status
);
803 process_opendir(void)
807 int handle
, status
= SSH2_FX_FAILURE
;
811 path
= get_string(NULL
);
812 debug3("request %u: opendir", id
);
813 logit("opendir \"%s\"", path
);
814 dirp
= opendir(path
);
816 status
= errno_to_portable(errno
);
818 handle
= handle_new(HANDLE_DIR
, path
, 0, dirp
);
822 send_handle(id
, handle
);
827 if (status
!= SSH2_FX_OK
)
828 send_status(id
, status
);
833 process_readdir(void)
842 handle
= get_handle();
843 debug("request %u: readdir \"%s\" (handle %d)", id
,
844 handle_to_name(handle
), handle
);
845 dirp
= handle_to_dir(handle
);
846 path
= handle_to_name(handle
);
847 if (dirp
== NULL
|| path
== NULL
) {
848 send_status(id
, SSH2_FX_FAILURE
);
851 char pathname
[MAXPATHLEN
];
853 int nstats
= 10, count
= 0, i
;
855 stats
= xcalloc(nstats
, sizeof(Stat
));
856 while ((dp
= readdir(dirp
)) != NULL
) {
857 if (count
>= nstats
) {
859 stats
= xrealloc(stats
, nstats
, sizeof(Stat
));
862 snprintf(pathname
, sizeof pathname
, "%s%s%s", path
,
863 strcmp(path
, "/") ? "/" : "", dp
->d_name
);
864 if (lstat(pathname
, &st
) < 0)
866 stat_to_attrib(&st
, &(stats
[count
].attrib
));
867 stats
[count
].name
= xstrdup(dp
->d_name
);
868 stats
[count
].long_name
= ls_file(dp
->d_name
, &st
, 0);
870 /* send up to 100 entries in one message */
871 /* XXX check packet size instead */
876 send_names(id
, count
, stats
);
877 for (i
= 0; i
< count
; i
++) {
878 xfree(stats
[i
].name
);
879 xfree(stats
[i
].long_name
);
882 send_status(id
, SSH2_FX_EOF
);
893 int status
= SSH2_FX_FAILURE
;
897 name
= get_string(NULL
);
898 debug3("request %u: remove", id
);
899 logit("remove name \"%s\"", name
);
901 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
902 send_status(id
, status
);
912 int ret
, mode
, status
= SSH2_FX_FAILURE
;
915 name
= get_string(NULL
);
917 mode
= (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) ?
918 a
->perm
& 0777 : 0777;
919 debug3("request %u: mkdir", id
);
920 logit("mkdir name \"%s\" mode 0%o", name
, mode
);
921 ret
= mkdir(name
, mode
);
922 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
923 send_status(id
, status
);
935 name
= get_string(NULL
);
936 debug3("request %u: rmdir", id
);
937 logit("rmdir name \"%s\"", name
);
939 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
940 send_status(id
, status
);
945 process_realpath(void)
947 char resolvedname
[MAXPATHLEN
];
952 path
= get_string(NULL
);
953 if (path
[0] == '\0') {
957 debug3("request %u: realpath", id
);
958 verbose("realpath \"%s\"", path
);
959 if (realpath(path
, resolvedname
) == NULL
) {
960 send_status(id
, errno_to_portable(errno
));
963 attrib_clear(&s
.attrib
);
964 s
.name
= s
.long_name
= resolvedname
;
965 send_names(id
, 1, &s
);
974 char *oldpath
, *newpath
;
979 oldpath
= get_string(NULL
);
980 newpath
= get_string(NULL
);
981 debug3("request %u: rename", id
);
982 logit("rename old \"%s\" new \"%s\"", oldpath
, newpath
);
983 status
= SSH2_FX_FAILURE
;
984 if (lstat(oldpath
, &sb
) == -1)
985 status
= errno_to_portable(errno
);
986 else if (S_ISREG(sb
.st_mode
)) {
987 /* Race-free rename of regular files */
988 if (link(oldpath
, newpath
) == -1) {
989 if (errno
== EOPNOTSUPP
990 #ifdef LINK_OPNOTSUPP_ERRNO
991 || errno
== LINK_OPNOTSUPP_ERRNO
997 * fs doesn't support links, so fall back to
998 * stat+rename. This is racy.
1000 if (stat(newpath
, &st
) == -1) {
1001 if (rename(oldpath
, newpath
) == -1)
1003 errno_to_portable(errno
);
1005 status
= SSH2_FX_OK
;
1008 status
= errno_to_portable(errno
);
1010 } else if (unlink(oldpath
) == -1) {
1011 status
= errno_to_portable(errno
);
1012 /* clean spare link */
1015 status
= SSH2_FX_OK
;
1016 } else if (stat(newpath
, &sb
) == -1) {
1017 if (rename(oldpath
, newpath
) == -1)
1018 status
= errno_to_portable(errno
);
1020 status
= SSH2_FX_OK
;
1022 send_status(id
, status
);
1028 process_readlink(void)
1032 char buf
[MAXPATHLEN
];
1036 path
= get_string(NULL
);
1037 debug3("request %u: readlink", id
);
1038 verbose("readlink \"%s\"", path
);
1039 if ((len
= readlink(path
, buf
, sizeof(buf
) - 1)) == -1)
1040 send_status(id
, errno_to_portable(errno
));
1045 attrib_clear(&s
.attrib
);
1046 s
.name
= s
.long_name
= buf
;
1047 send_names(id
, 1, &s
);
1053 process_symlink(void)
1056 char *oldpath
, *newpath
;
1060 oldpath
= get_string(NULL
);
1061 newpath
= get_string(NULL
);
1062 debug3("request %u: symlink", id
);
1063 logit("symlink old \"%s\" new \"%s\"", oldpath
, newpath
);
1064 /* this will fail if 'newpath' exists */
1065 ret
= symlink(oldpath
, newpath
);
1066 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
1067 send_status(id
, status
);
1073 process_extended(void)
1079 request
= get_string(NULL
);
1080 send_status(id
, SSH2_FX_OP_UNSUPPORTED
); /* MUST */
1084 /* stolen from ssh-agent */
1095 buf_len
= buffer_len(&iqueue
);
1097 return; /* Incomplete message. */
1098 cp
= buffer_ptr(&iqueue
);
1099 msg_len
= get_u32(cp
);
1100 if (msg_len
> SFTP_MAX_MSG_LENGTH
) {
1101 error("bad message from %s local user %s",
1102 client_addr
, pw
->pw_name
);
1105 if (buf_len
< msg_len
+ 4)
1107 buffer_consume(&iqueue
, 4);
1109 type
= buffer_get_char(&iqueue
);
1117 case SSH2_FXP_CLOSE
:
1123 case SSH2_FXP_WRITE
:
1126 case SSH2_FXP_LSTAT
:
1129 case SSH2_FXP_FSTAT
:
1132 case SSH2_FXP_SETSTAT
:
1135 case SSH2_FXP_FSETSTAT
:
1138 case SSH2_FXP_OPENDIR
:
1141 case SSH2_FXP_READDIR
:
1144 case SSH2_FXP_REMOVE
:
1147 case SSH2_FXP_MKDIR
:
1150 case SSH2_FXP_RMDIR
:
1153 case SSH2_FXP_REALPATH
:
1159 case SSH2_FXP_RENAME
:
1162 case SSH2_FXP_READLINK
:
1165 case SSH2_FXP_SYMLINK
:
1168 case SSH2_FXP_EXTENDED
:
1172 error("Unknown message %d", type
);
1175 /* discard the remaining bytes from the current packet */
1176 if (buf_len
< buffer_len(&iqueue
))
1177 fatal("iqueue grew unexpectedly");
1178 consumed
= buf_len
- buffer_len(&iqueue
);
1179 if (msg_len
< consumed
)
1180 fatal("msg_len %d < consumed %d", msg_len
, consumed
);
1181 if (msg_len
> consumed
)
1182 buffer_consume(&iqueue
, msg_len
- consumed
);
1185 /* Cleanup handler that logs active handles upon normal exit */
1189 if (pw
!= NULL
&& client_addr
!= NULL
) {
1191 logit("session closed for local user %s from [%s]",
1192 pw
->pw_name
, client_addr
);
1200 extern char *__progname
;
1203 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname
);
1208 main(int argc
, char **argv
)
1210 fd_set
*rset
, *wset
;
1211 int in
, out
, max
, ch
, skipargs
= 0, log_stderr
= 0;
1212 ssize_t len
, olen
, set_size
;
1213 SyslogFacility log_facility
= SYSLOG_FACILITY_AUTH
;
1216 extern char *optarg
;
1217 extern char *__progname
;
1219 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1222 __progname
= ssh_get_progname(argv
[0]);
1223 log_init(__progname
, log_level
, log_facility
, log_stderr
);
1225 while (!skipargs
&& (ch
= getopt(argc
, argv
, "C:f:l:che")) != -1) {
1229 * Ignore all arguments if we are invoked as a
1230 * shell using "sftp-server -c command"
1238 log_level
= log_level_number(optarg
);
1239 if (log_level
== SYSLOG_LEVEL_NOT_SET
)
1240 error("Invalid log level \"%s\"", optarg
);
1243 log_facility
= log_facility_number(optarg
);
1244 if (log_level
== SYSLOG_FACILITY_NOT_SET
)
1245 error("Invalid log facility \"%s\"", optarg
);
1253 log_init(__progname
, log_level
, log_facility
, log_stderr
);
1255 if ((cp
= getenv("SSH_CONNECTION")) != NULL
) {
1256 client_addr
= xstrdup(cp
);
1257 if ((cp
= strchr(client_addr
, ' ')) == NULL
)
1258 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1259 getenv("SSH_CONNECTION"));
1262 client_addr
= xstrdup("UNKNOWN");
1264 if ((pw
= getpwuid(getuid())) == NULL
)
1265 fatal("No user found for uid %lu", (u_long
)getuid());
1268 logit("session opened for local user %s from [%s]",
1269 pw
->pw_name
, client_addr
);
1273 in
= dup(STDIN_FILENO
);
1274 out
= dup(STDOUT_FILENO
);
1277 setmode(in
, O_BINARY
);
1278 setmode(out
, O_BINARY
);
1287 buffer_init(&iqueue
);
1288 buffer_init(&oqueue
);
1290 set_size
= howmany(max
+ 1, NFDBITS
) * sizeof(fd_mask
);
1291 rset
= (fd_set
*)xmalloc(set_size
);
1292 wset
= (fd_set
*)xmalloc(set_size
);
1295 memset(rset
, 0, set_size
);
1296 memset(wset
, 0, set_size
);
1299 olen
= buffer_len(&oqueue
);
1303 if (select(max
+1, rset
, wset
, NULL
, NULL
) < 0) {
1306 error("select: %s", strerror(errno
));
1310 /* copy stdin to iqueue */
1311 if (FD_ISSET(in
, rset
)) {
1313 len
= read(in
, buf
, sizeof buf
);
1317 } else if (len
< 0) {
1318 error("read: %s", strerror(errno
));
1321 buffer_append(&iqueue
, buf
, len
);
1324 /* send oqueue to stdout */
1325 if (FD_ISSET(out
, wset
)) {
1326 len
= write(out
, buffer_ptr(&oqueue
), olen
);
1328 error("write: %s", strerror(errno
));
1331 buffer_consume(&oqueue
, len
);
1334 /* process requests from client */