1 /* $OpenBSD: sftp-server.c,v 1.65 2006/07/22 20:48:23 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>
38 #include "sftp-common.h"
41 #define get_int64() buffer_get_int64(&iqueue);
42 #define get_int() buffer_get_int(&iqueue);
43 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
46 LogLevel log_level
= SYSLOG_LEVEL_ERROR
;
49 struct passwd
*pw
= NULL
;
50 char *client_addr
= NULL
;
52 /* input and output queue */
56 /* Version of client */
59 /* portable attributes, etc. */
61 typedef struct Stat Stat
;
70 errno_to_portable(int unixerrno
)
82 ret
= SSH2_FX_NO_SUCH_FILE
;
87 ret
= SSH2_FX_PERMISSION_DENIED
;
91 ret
= SSH2_FX_BAD_MESSAGE
;
94 ret
= SSH2_FX_FAILURE
;
101 flags_from_portable(int pflags
)
105 if ((pflags
& SSH2_FXF_READ
) &&
106 (pflags
& SSH2_FXF_WRITE
)) {
108 } else if (pflags
& SSH2_FXF_READ
) {
110 } else if (pflags
& SSH2_FXF_WRITE
) {
113 if (pflags
& SSH2_FXF_CREAT
)
115 if (pflags
& SSH2_FXF_TRUNC
)
117 if (pflags
& SSH2_FXF_EXCL
)
123 string_from_portable(int pflags
)
125 static char ret
[128];
129 #define PAPPEND(str) { \
131 strlcat(ret, ",", sizeof(ret)); \
132 strlcat(ret, str, sizeof(ret)); \
135 if (pflags
& SSH2_FXF_READ
)
137 if (pflags
& SSH2_FXF_WRITE
)
139 if (pflags
& SSH2_FXF_CREAT
)
141 if (pflags
& SSH2_FXF_TRUNC
)
143 if (pflags
& SSH2_FXF_EXCL
)
152 return decode_attrib(&iqueue
);
157 typedef struct Handle Handle
;
163 u_int64_t bytes_read
, bytes_write
;
179 for (i
= 0; i
< sizeof(handles
)/sizeof(Handle
); i
++)
180 handles
[i
].use
= HANDLE_UNUSED
;
184 handle_new(int use
, const char *name
, int fd
, DIR *dirp
)
188 for (i
= 0; i
< sizeof(handles
)/sizeof(Handle
); i
++) {
189 if (handles
[i
].use
== HANDLE_UNUSED
) {
190 handles
[i
].use
= use
;
191 handles
[i
].dirp
= dirp
;
193 handles
[i
].name
= xstrdup(name
);
194 handles
[i
].bytes_read
= handles
[i
].bytes_write
= 0;
202 handle_is_ok(int i
, int type
)
204 return i
>= 0 && (u_int
)i
< sizeof(handles
)/sizeof(Handle
) &&
205 handles
[i
].use
== type
;
209 handle_to_string(int handle
, char **stringp
, int *hlenp
)
211 if (stringp
== NULL
|| hlenp
== NULL
)
213 *stringp
= xmalloc(sizeof(int32_t));
214 put_u32(*stringp
, handle
);
215 *hlenp
= sizeof(int32_t);
220 handle_from_string(const char *handle
, u_int hlen
)
224 if (hlen
!= sizeof(int32_t))
226 val
= get_u32(handle
);
227 if (handle_is_ok(val
, HANDLE_FILE
) ||
228 handle_is_ok(val
, HANDLE_DIR
))
234 handle_to_name(int handle
)
236 if (handle_is_ok(handle
, HANDLE_DIR
)||
237 handle_is_ok(handle
, HANDLE_FILE
))
238 return handles
[handle
].name
;
243 handle_to_dir(int handle
)
245 if (handle_is_ok(handle
, HANDLE_DIR
))
246 return handles
[handle
].dirp
;
251 handle_to_fd(int handle
)
253 if (handle_is_ok(handle
, HANDLE_FILE
))
254 return handles
[handle
].fd
;
259 handle_update_read(int handle
, ssize_t bytes
)
261 if (handle_is_ok(handle
, HANDLE_FILE
) && bytes
> 0)
262 handles
[handle
].bytes_read
+= bytes
;
266 handle_update_write(int handle
, ssize_t bytes
)
268 if (handle_is_ok(handle
, HANDLE_FILE
) && bytes
> 0)
269 handles
[handle
].bytes_write
+= bytes
;
273 handle_bytes_read(int handle
)
275 if (handle_is_ok(handle
, HANDLE_FILE
))
276 return (handles
[handle
].bytes_read
);
281 handle_bytes_write(int handle
)
283 if (handle_is_ok(handle
, HANDLE_FILE
))
284 return (handles
[handle
].bytes_write
);
289 handle_close(int handle
)
293 if (handle_is_ok(handle
, HANDLE_FILE
)) {
294 ret
= close(handles
[handle
].fd
);
295 handles
[handle
].use
= HANDLE_UNUSED
;
296 xfree(handles
[handle
].name
);
297 } else if (handle_is_ok(handle
, HANDLE_DIR
)) {
298 ret
= closedir(handles
[handle
].dirp
);
299 handles
[handle
].use
= HANDLE_UNUSED
;
300 xfree(handles
[handle
].name
);
308 handle_log_close(int handle
, char *emsg
)
310 if (handle_is_ok(handle
, HANDLE_FILE
)) {
311 logit("%s%sclose \"%s\" bytes read %llu written %llu",
312 emsg
== NULL
? "" : emsg
, emsg
== NULL
? "" : " ",
313 handle_to_name(handle
),
314 handle_bytes_read(handle
), handle_bytes_write(handle
));
316 logit("%s%sclosedir \"%s\"",
317 emsg
== NULL
? "" : emsg
, emsg
== NULL
? "" : " ",
318 handle_to_name(handle
));
323 handle_log_exit(void)
327 for (i
= 0; i
< sizeof(handles
)/sizeof(Handle
); i
++)
328 if (handles
[i
].use
!= HANDLE_UNUSED
)
329 handle_log_close(i
, "forced");
339 handle
= get_string(&hlen
);
341 val
= handle_from_string(handle
, hlen
);
351 int mlen
= buffer_len(m
);
353 buffer_put_int(&oqueue
, mlen
);
354 buffer_append(&oqueue
, buffer_ptr(m
), mlen
);
355 buffer_consume(m
, mlen
);
359 status_to_message(u_int32_t status
)
361 const char *status_messages
[] = {
362 "Success", /* SSH_FX_OK */
363 "End of file", /* SSH_FX_EOF */
364 "No such file", /* SSH_FX_NO_SUCH_FILE */
365 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
366 "Failure", /* SSH_FX_FAILURE */
367 "Bad message", /* SSH_FX_BAD_MESSAGE */
368 "No connection", /* SSH_FX_NO_CONNECTION */
369 "Connection lost", /* SSH_FX_CONNECTION_LOST */
370 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
371 "Unknown error" /* Others */
373 return (status_messages
[MIN(status
,SSH2_FX_MAX
)]);
377 send_status(u_int32_t id
, u_int32_t status
)
381 debug3("request %u: sent status %u", id
, status
);
382 if (log_level
> SYSLOG_LEVEL_VERBOSE
||
383 (status
!= SSH2_FX_OK
&& status
!= SSH2_FX_EOF
))
384 logit("sent status %s", status_to_message(status
));
386 buffer_put_char(&msg
, SSH2_FXP_STATUS
);
387 buffer_put_int(&msg
, id
);
388 buffer_put_int(&msg
, status
);
390 buffer_put_cstring(&msg
, status_to_message(status
));
391 buffer_put_cstring(&msg
, "");
397 send_data_or_handle(char type
, u_int32_t id
, const char *data
, int dlen
)
402 buffer_put_char(&msg
, type
);
403 buffer_put_int(&msg
, id
);
404 buffer_put_string(&msg
, data
, dlen
);
410 send_data(u_int32_t id
, const char *data
, int dlen
)
412 debug("request %u: sent data len %d", id
, dlen
);
413 send_data_or_handle(SSH2_FXP_DATA
, id
, data
, dlen
);
417 send_handle(u_int32_t id
, int handle
)
422 handle_to_string(handle
, &string
, &hlen
);
423 debug("request %u: sent handle handle %d", id
, handle
);
424 send_data_or_handle(SSH2_FXP_HANDLE
, id
, string
, hlen
);
429 send_names(u_int32_t id
, int count
, const Stat
*stats
)
435 buffer_put_char(&msg
, SSH2_FXP_NAME
);
436 buffer_put_int(&msg
, id
);
437 buffer_put_int(&msg
, count
);
438 debug("request %u: sent names count %d", id
, count
);
439 for (i
= 0; i
< count
; i
++) {
440 buffer_put_cstring(&msg
, stats
[i
].name
);
441 buffer_put_cstring(&msg
, stats
[i
].long_name
);
442 encode_attrib(&msg
, &stats
[i
].attrib
);
449 send_attrib(u_int32_t id
, const Attrib
*a
)
453 debug("request %u: sent attrib have 0x%x", id
, a
->flags
);
455 buffer_put_char(&msg
, SSH2_FXP_ATTRS
);
456 buffer_put_int(&msg
, id
);
457 encode_attrib(&msg
, a
);
470 verbose("received client version %d", version
);
472 buffer_put_char(&msg
, SSH2_FXP_VERSION
);
473 buffer_put_int(&msg
, SSH2_FILEXFER_VERSION
);
481 u_int32_t id
, pflags
;
484 int handle
, fd
, flags
, mode
, status
= SSH2_FX_FAILURE
;
487 name
= get_string(NULL
);
488 pflags
= get_int(); /* portable flags */
489 debug3("request %u: open flags %d", id
, pflags
);
491 flags
= flags_from_portable(pflags
);
492 mode
= (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) ? a
->perm
: 0666;
493 logit("open \"%s\" flags %s mode 0%o",
494 name
, string_from_portable(pflags
), mode
);
495 fd
= open(name
, flags
, mode
);
497 status
= errno_to_portable(errno
);
499 handle
= handle_new(HANDLE_FILE
, name
, fd
, NULL
);
503 send_handle(id
, handle
);
507 if (status
!= SSH2_FX_OK
)
508 send_status(id
, status
);
516 int handle
, ret
, status
= SSH2_FX_FAILURE
;
519 handle
= get_handle();
520 debug3("request %u: close handle %u", id
, handle
);
521 handle_log_close(handle
, NULL
);
522 ret
= handle_close(handle
);
523 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
524 send_status(id
, status
);
532 int handle
, fd
, ret
, status
= SSH2_FX_FAILURE
;
536 handle
= get_handle();
540 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
541 id
, handle_to_name(handle
), handle
, (unsigned long long)off
, len
);
542 if (len
> sizeof buf
) {
544 debug2("read change len %d", len
);
546 fd
= handle_to_fd(handle
);
548 if (lseek(fd
, off
, SEEK_SET
) < 0) {
549 error("process_read: seek failed");
550 status
= errno_to_portable(errno
);
552 ret
= read(fd
, buf
, len
);
554 status
= errno_to_portable(errno
);
555 } else if (ret
== 0) {
556 status
= SSH2_FX_EOF
;
558 send_data(id
, buf
, ret
);
560 handle_update_read(handle
, ret
);
564 if (status
!= SSH2_FX_OK
)
565 send_status(id
, status
);
574 int handle
, fd
, ret
, status
= SSH2_FX_FAILURE
;
578 handle
= get_handle();
580 data
= get_string(&len
);
582 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
583 id
, handle_to_name(handle
), handle
, (unsigned long long)off
, len
);
584 fd
= handle_to_fd(handle
);
586 if (lseek(fd
, off
, SEEK_SET
) < 0) {
587 status
= errno_to_portable(errno
);
588 error("process_write: seek failed");
591 ret
= write(fd
, data
, len
);
593 error("process_write: write failed");
594 status
= errno_to_portable(errno
);
595 } else if ((size_t)ret
== len
) {
597 handle_update_write(handle
, ret
);
599 debug2("nothing at all written");
603 send_status(id
, status
);
608 process_do_stat(int do_lstat
)
614 int ret
, status
= SSH2_FX_FAILURE
;
617 name
= get_string(NULL
);
618 debug3("request %u: %sstat", id
, do_lstat
? "l" : "");
619 verbose("%sstat name \"%s\"", do_lstat
? "l" : "", name
);
620 ret
= do_lstat
? lstat(name
, &st
) : stat(name
, &st
);
622 status
= errno_to_portable(errno
);
624 stat_to_attrib(&st
, &a
);
628 if (status
!= SSH2_FX_OK
)
629 send_status(id
, status
);
651 int fd
, ret
, handle
, status
= SSH2_FX_FAILURE
;
654 handle
= get_handle();
655 debug("request %u: fstat \"%s\" (handle %u)",
656 id
, handle_to_name(handle
), handle
);
657 fd
= handle_to_fd(handle
);
659 ret
= fstat(fd
, &st
);
661 status
= errno_to_portable(errno
);
663 stat_to_attrib(&st
, &a
);
668 if (status
!= SSH2_FX_OK
)
669 send_status(id
, status
);
672 static struct timeval
*
673 attrib_to_tv(const Attrib
*a
)
675 static struct timeval tv
[2];
677 tv
[0].tv_sec
= a
->atime
;
679 tv
[1].tv_sec
= a
->mtime
;
685 process_setstat(void)
690 int status
= SSH2_FX_OK
, ret
;
693 name
= get_string(NULL
);
695 debug("request %u: setstat name \"%s\"", id
, name
);
696 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
) {
697 logit("set \"%s\" size %llu", name
, a
->size
);
698 ret
= truncate(name
, a
->size
);
700 status
= errno_to_portable(errno
);
702 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
703 logit("set \"%s\" mode %04o", name
, a
->perm
);
704 ret
= chmod(name
, a
->perm
& 0777);
706 status
= errno_to_portable(errno
);
708 if (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
) {
712 strftime(buf
, sizeof(buf
), "%Y%m%d-%H:%M:%S",
714 logit("set \"%s\" modtime %s", name
, buf
);
715 ret
= utimes(name
, attrib_to_tv(a
));
717 status
= errno_to_portable(errno
);
719 if (a
->flags
& SSH2_FILEXFER_ATTR_UIDGID
) {
720 logit("set \"%s\" owner %lu group %lu", name
,
721 (u_long
)a
->uid
, (u_long
)a
->gid
);
722 ret
= chown(name
, a
->uid
, a
->gid
);
724 status
= errno_to_portable(errno
);
726 send_status(id
, status
);
731 process_fsetstat(void)
736 int status
= SSH2_FX_OK
;
739 handle
= get_handle();
741 debug("request %u: fsetstat handle %d", id
, handle
);
742 fd
= handle_to_fd(handle
);
744 status
= SSH2_FX_FAILURE
;
746 char *name
= handle_to_name(handle
);
748 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
) {
749 logit("set \"%s\" size %llu", name
, a
->size
);
750 ret
= ftruncate(fd
, a
->size
);
752 status
= errno_to_portable(errno
);
754 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
755 logit("set \"%s\" mode %04o", name
, a
->perm
);
757 ret
= fchmod(fd
, a
->perm
& 0777);
759 ret
= chmod(name
, a
->perm
& 0777);
762 status
= errno_to_portable(errno
);
764 if (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
) {
768 strftime(buf
, sizeof(buf
), "%Y%m%d-%H:%M:%S",
770 logit("set \"%s\" modtime %s", name
, buf
);
772 ret
= futimes(fd
, attrib_to_tv(a
));
774 ret
= utimes(name
, attrib_to_tv(a
));
777 status
= errno_to_portable(errno
);
779 if (a
->flags
& SSH2_FILEXFER_ATTR_UIDGID
) {
780 logit("set \"%s\" owner %lu group %lu", name
,
781 (u_long
)a
->uid
, (u_long
)a
->gid
);
783 ret
= fchown(fd
, a
->uid
, a
->gid
);
785 ret
= chown(name
, a
->uid
, a
->gid
);
788 status
= errno_to_portable(errno
);
791 send_status(id
, status
);
795 process_opendir(void)
799 int handle
, status
= SSH2_FX_FAILURE
;
803 path
= get_string(NULL
);
804 debug3("request %u: opendir", id
);
805 logit("opendir \"%s\"", path
);
806 dirp
= opendir(path
);
808 status
= errno_to_portable(errno
);
810 handle
= handle_new(HANDLE_DIR
, path
, 0, dirp
);
814 send_handle(id
, handle
);
819 if (status
!= SSH2_FX_OK
)
820 send_status(id
, status
);
825 process_readdir(void)
834 handle
= get_handle();
835 debug("request %u: readdir \"%s\" (handle %d)", id
,
836 handle_to_name(handle
), handle
);
837 dirp
= handle_to_dir(handle
);
838 path
= handle_to_name(handle
);
839 if (dirp
== NULL
|| path
== NULL
) {
840 send_status(id
, SSH2_FX_FAILURE
);
843 char pathname
[MAXPATHLEN
];
845 int nstats
= 10, count
= 0, i
;
847 stats
= xcalloc(nstats
, sizeof(Stat
));
848 while ((dp
= readdir(dirp
)) != NULL
) {
849 if (count
>= nstats
) {
851 stats
= xrealloc(stats
, nstats
, sizeof(Stat
));
854 snprintf(pathname
, sizeof pathname
, "%s%s%s", path
,
855 strcmp(path
, "/") ? "/" : "", dp
->d_name
);
856 if (lstat(pathname
, &st
) < 0)
858 stat_to_attrib(&st
, &(stats
[count
].attrib
));
859 stats
[count
].name
= xstrdup(dp
->d_name
);
860 stats
[count
].long_name
= ls_file(dp
->d_name
, &st
, 0);
862 /* send up to 100 entries in one message */
863 /* XXX check packet size instead */
868 send_names(id
, count
, stats
);
869 for (i
= 0; i
< count
; i
++) {
870 xfree(stats
[i
].name
);
871 xfree(stats
[i
].long_name
);
874 send_status(id
, SSH2_FX_EOF
);
885 int status
= SSH2_FX_FAILURE
;
889 name
= get_string(NULL
);
890 debug3("request %u: remove", id
);
891 logit("remove name \"%s\"", name
);
893 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
894 send_status(id
, status
);
904 int ret
, mode
, status
= SSH2_FX_FAILURE
;
907 name
= get_string(NULL
);
909 mode
= (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) ?
910 a
->perm
& 0777 : 0777;
911 debug3("request %u: mkdir", id
);
912 logit("mkdir name \"%s\" mode 0%o", name
, mode
);
913 ret
= mkdir(name
, mode
);
914 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
915 send_status(id
, status
);
927 name
= get_string(NULL
);
928 debug3("request %u: rmdir", id
);
929 logit("rmdir name \"%s\"", name
);
931 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
932 send_status(id
, status
);
937 process_realpath(void)
939 char resolvedname
[MAXPATHLEN
];
944 path
= get_string(NULL
);
945 if (path
[0] == '\0') {
949 debug3("request %u: realpath", id
);
950 verbose("realpath \"%s\"", path
);
951 if (realpath(path
, resolvedname
) == NULL
) {
952 send_status(id
, errno_to_portable(errno
));
955 attrib_clear(&s
.attrib
);
956 s
.name
= s
.long_name
= resolvedname
;
957 send_names(id
, 1, &s
);
966 char *oldpath
, *newpath
;
971 oldpath
= get_string(NULL
);
972 newpath
= get_string(NULL
);
973 debug3("request %u: rename", id
);
974 logit("rename old \"%s\" new \"%s\"", oldpath
, newpath
);
975 status
= SSH2_FX_FAILURE
;
976 if (lstat(oldpath
, &sb
) == -1)
977 status
= errno_to_portable(errno
);
978 else if (S_ISREG(sb
.st_mode
)) {
979 /* Race-free rename of regular files */
980 if (link(oldpath
, newpath
) == -1) {
981 if (errno
== EOPNOTSUPP
982 #ifdef LINK_OPNOTSUPP_ERRNO
983 || errno
== LINK_OPNOTSUPP_ERRNO
989 * fs doesn't support links, so fall back to
990 * stat+rename. This is racy.
992 if (stat(newpath
, &st
) == -1) {
993 if (rename(oldpath
, newpath
) == -1)
995 errno_to_portable(errno
);
1000 status
= errno_to_portable(errno
);
1002 } else if (unlink(oldpath
) == -1) {
1003 status
= errno_to_portable(errno
);
1004 /* clean spare link */
1007 status
= SSH2_FX_OK
;
1008 } else if (stat(newpath
, &sb
) == -1) {
1009 if (rename(oldpath
, newpath
) == -1)
1010 status
= errno_to_portable(errno
);
1012 status
= SSH2_FX_OK
;
1014 send_status(id
, status
);
1020 process_readlink(void)
1024 char buf
[MAXPATHLEN
];
1028 path
= get_string(NULL
);
1029 debug3("request %u: readlink", id
);
1030 verbose("readlink \"%s\"", path
);
1031 if ((len
= readlink(path
, buf
, sizeof(buf
) - 1)) == -1)
1032 send_status(id
, errno_to_portable(errno
));
1037 attrib_clear(&s
.attrib
);
1038 s
.name
= s
.long_name
= buf
;
1039 send_names(id
, 1, &s
);
1045 process_symlink(void)
1048 char *oldpath
, *newpath
;
1052 oldpath
= get_string(NULL
);
1053 newpath
= get_string(NULL
);
1054 debug3("request %u: symlink", id
);
1055 logit("symlink old \"%s\" new \"%s\"", oldpath
, newpath
);
1056 /* this will fail if 'newpath' exists */
1057 ret
= symlink(oldpath
, newpath
);
1058 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
1059 send_status(id
, status
);
1065 process_extended(void)
1071 request
= get_string(NULL
);
1072 send_status(id
, SSH2_FX_OP_UNSUPPORTED
); /* MUST */
1076 /* stolen from ssh-agent */
1087 buf_len
= buffer_len(&iqueue
);
1089 return; /* Incomplete message. */
1090 cp
= buffer_ptr(&iqueue
);
1091 msg_len
= get_u32(cp
);
1092 if (msg_len
> SFTP_MAX_MSG_LENGTH
) {
1093 error("bad message from %s local user %s",
1094 client_addr
, pw
->pw_name
);
1097 if (buf_len
< msg_len
+ 4)
1099 buffer_consume(&iqueue
, 4);
1101 type
= buffer_get_char(&iqueue
);
1109 case SSH2_FXP_CLOSE
:
1115 case SSH2_FXP_WRITE
:
1118 case SSH2_FXP_LSTAT
:
1121 case SSH2_FXP_FSTAT
:
1124 case SSH2_FXP_SETSTAT
:
1127 case SSH2_FXP_FSETSTAT
:
1130 case SSH2_FXP_OPENDIR
:
1133 case SSH2_FXP_READDIR
:
1136 case SSH2_FXP_REMOVE
:
1139 case SSH2_FXP_MKDIR
:
1142 case SSH2_FXP_RMDIR
:
1145 case SSH2_FXP_REALPATH
:
1151 case SSH2_FXP_RENAME
:
1154 case SSH2_FXP_READLINK
:
1157 case SSH2_FXP_SYMLINK
:
1160 case SSH2_FXP_EXTENDED
:
1164 error("Unknown message %d", type
);
1167 /* discard the remaining bytes from the current packet */
1168 if (buf_len
< buffer_len(&iqueue
))
1169 fatal("iqueue grew unexpectedly");
1170 consumed
= buf_len
- buffer_len(&iqueue
);
1171 if (msg_len
< consumed
)
1172 fatal("msg_len %d < consumed %d", msg_len
, consumed
);
1173 if (msg_len
> consumed
)
1174 buffer_consume(&iqueue
, msg_len
- consumed
);
1177 /* Cleanup handler that logs active handles upon normal exit */
1181 if (pw
!= NULL
&& client_addr
!= NULL
) {
1183 logit("session closed for local user %s from [%s]",
1184 pw
->pw_name
, client_addr
);
1192 extern char *__progname
;
1195 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname
);
1200 main(int argc
, char **argv
)
1202 fd_set
*rset
, *wset
;
1203 int in
, out
, max
, ch
, skipargs
= 0, log_stderr
= 0;
1204 ssize_t len
, olen
, set_size
;
1205 SyslogFacility log_facility
= SYSLOG_FACILITY_AUTH
;
1208 extern char *optarg
;
1209 extern char *__progname
;
1211 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1214 __progname
= ssh_get_progname(argv
[0]);
1215 log_init(__progname
, log_level
, log_facility
, log_stderr
);
1217 while (!skipargs
&& (ch
= getopt(argc
, argv
, "C:f:l:che")) != -1) {
1221 * Ignore all arguments if we are invoked as a
1222 * shell using "sftp-server -c command"
1230 log_level
= log_level_number(optarg
);
1231 if (log_level
== SYSLOG_LEVEL_NOT_SET
)
1232 error("Invalid log level \"%s\"", optarg
);
1235 log_facility
= log_facility_number(optarg
);
1236 if (log_level
== SYSLOG_FACILITY_NOT_SET
)
1237 error("Invalid log facility \"%s\"", optarg
);
1245 log_init(__progname
, log_level
, log_facility
, log_stderr
);
1247 if ((cp
= getenv("SSH_CONNECTION")) != NULL
) {
1248 client_addr
= xstrdup(cp
);
1249 if ((cp
= strchr(client_addr
, ' ')) == NULL
)
1250 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1251 getenv("SSH_CONNECTION"));
1254 client_addr
= xstrdup("UNKNOWN");
1256 if ((pw
= getpwuid(getuid())) == NULL
)
1257 fatal("No user found for uid %lu", (u_long
)getuid());
1260 logit("session opened for local user %s from [%s]",
1261 pw
->pw_name
, client_addr
);
1265 in
= dup(STDIN_FILENO
);
1266 out
= dup(STDOUT_FILENO
);
1269 setmode(in
, O_BINARY
);
1270 setmode(out
, O_BINARY
);
1279 buffer_init(&iqueue
);
1280 buffer_init(&oqueue
);
1282 set_size
= howmany(max
+ 1, NFDBITS
) * sizeof(fd_mask
);
1283 rset
= (fd_set
*)xmalloc(set_size
);
1284 wset
= (fd_set
*)xmalloc(set_size
);
1287 memset(rset
, 0, set_size
);
1288 memset(wset
, 0, set_size
);
1291 olen
= buffer_len(&oqueue
);
1295 if (select(max
+1, rset
, wset
, NULL
, NULL
) < 0) {
1298 error("select: %s", strerror(errno
));
1302 /* copy stdin to iqueue */
1303 if (FD_ISSET(in
, rset
)) {
1305 len
= read(in
, buf
, sizeof buf
);
1309 } else if (len
< 0) {
1310 error("read: %s", strerror(errno
));
1313 buffer_append(&iqueue
, buf
, len
);
1316 /* send oqueue to stdout */
1317 if (FD_ISSET(out
, wset
)) {
1318 len
= write(out
, buffer_ptr(&oqueue
), olen
);
1320 error("write: %s", strerror(errno
));
1323 buffer_consume(&oqueue
, len
);
1326 /* process requests from client */