1 /* $OpenBSD: sftp-server.c,v 1.63 2006/07/17 01:31:09 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>
36 #include "sftp-common.h"
39 #define get_int64() buffer_get_int64(&iqueue);
40 #define get_int() buffer_get_int(&iqueue);
41 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
44 LogLevel log_level
= SYSLOG_LEVEL_ERROR
;
47 struct passwd
*pw
= NULL
;
48 char *client_addr
= NULL
;
50 /* input and output queue */
54 /* Version of client */
57 /* portable attributes, etc. */
59 typedef struct Stat Stat
;
68 errno_to_portable(int unixerrno
)
80 ret
= SSH2_FX_NO_SUCH_FILE
;
85 ret
= SSH2_FX_PERMISSION_DENIED
;
89 ret
= SSH2_FX_BAD_MESSAGE
;
92 ret
= SSH2_FX_FAILURE
;
99 flags_from_portable(int pflags
)
103 if ((pflags
& SSH2_FXF_READ
) &&
104 (pflags
& SSH2_FXF_WRITE
)) {
106 } else if (pflags
& SSH2_FXF_READ
) {
108 } else if (pflags
& SSH2_FXF_WRITE
) {
111 if (pflags
& SSH2_FXF_CREAT
)
113 if (pflags
& SSH2_FXF_TRUNC
)
115 if (pflags
& SSH2_FXF_EXCL
)
121 string_from_portable(int pflags
)
123 static char ret
[128];
127 #define PAPPEND(str) { \
129 strlcat(ret, ",", sizeof(ret)); \
130 strlcat(ret, str, sizeof(ret)); \
133 if (pflags
& SSH2_FXF_READ
)
135 if (pflags
& SSH2_FXF_WRITE
)
137 if (pflags
& SSH2_FXF_CREAT
)
139 if (pflags
& SSH2_FXF_TRUNC
)
141 if (pflags
& SSH2_FXF_EXCL
)
150 return decode_attrib(&iqueue
);
155 typedef struct Handle Handle
;
161 u_int64_t bytes_read
, bytes_write
;
177 for (i
= 0; i
< sizeof(handles
)/sizeof(Handle
); i
++)
178 handles
[i
].use
= HANDLE_UNUSED
;
182 handle_new(int use
, const char *name
, int fd
, DIR *dirp
)
186 for (i
= 0; i
< sizeof(handles
)/sizeof(Handle
); i
++) {
187 if (handles
[i
].use
== HANDLE_UNUSED
) {
188 handles
[i
].use
= use
;
189 handles
[i
].dirp
= dirp
;
191 handles
[i
].name
= xstrdup(name
);
192 handles
[i
].bytes_read
= handles
[i
].bytes_write
= 0;
200 handle_is_ok(int i
, int type
)
202 return i
>= 0 && (u_int
)i
< sizeof(handles
)/sizeof(Handle
) &&
203 handles
[i
].use
== type
;
207 handle_to_string(int handle
, char **stringp
, int *hlenp
)
209 if (stringp
== NULL
|| hlenp
== NULL
)
211 *stringp
= xmalloc(sizeof(int32_t));
212 put_u32(*stringp
, handle
);
213 *hlenp
= sizeof(int32_t);
218 handle_from_string(const char *handle
, u_int hlen
)
222 if (hlen
!= sizeof(int32_t))
224 val
= get_u32(handle
);
225 if (handle_is_ok(val
, HANDLE_FILE
) ||
226 handle_is_ok(val
, HANDLE_DIR
))
232 handle_to_name(int handle
)
234 if (handle_is_ok(handle
, HANDLE_DIR
)||
235 handle_is_ok(handle
, HANDLE_FILE
))
236 return handles
[handle
].name
;
241 handle_to_dir(int handle
)
243 if (handle_is_ok(handle
, HANDLE_DIR
))
244 return handles
[handle
].dirp
;
249 handle_to_fd(int handle
)
251 if (handle_is_ok(handle
, HANDLE_FILE
))
252 return handles
[handle
].fd
;
257 handle_update_read(int handle
, ssize_t bytes
)
259 if (handle_is_ok(handle
, HANDLE_FILE
) && bytes
> 0)
260 handles
[handle
].bytes_read
+= bytes
;
264 handle_update_write(int handle
, ssize_t bytes
)
266 if (handle_is_ok(handle
, HANDLE_FILE
) && bytes
> 0)
267 handles
[handle
].bytes_write
+= bytes
;
271 handle_bytes_read(int handle
)
273 if (handle_is_ok(handle
, HANDLE_FILE
))
274 return (handles
[handle
].bytes_read
);
279 handle_bytes_write(int handle
)
281 if (handle_is_ok(handle
, HANDLE_FILE
))
282 return (handles
[handle
].bytes_write
);
287 handle_close(int handle
)
291 if (handle_is_ok(handle
, HANDLE_FILE
)) {
292 ret
= close(handles
[handle
].fd
);
293 handles
[handle
].use
= HANDLE_UNUSED
;
294 xfree(handles
[handle
].name
);
295 } else if (handle_is_ok(handle
, HANDLE_DIR
)) {
296 ret
= closedir(handles
[handle
].dirp
);
297 handles
[handle
].use
= HANDLE_UNUSED
;
298 xfree(handles
[handle
].name
);
306 handle_log_close(int handle
, char *emsg
)
308 if (handle_is_ok(handle
, HANDLE_FILE
)) {
309 logit("%s%sclose \"%s\" bytes read %llu written %llu",
310 emsg
== NULL
? "" : emsg
, emsg
== NULL
? "" : " ",
311 handle_to_name(handle
),
312 handle_bytes_read(handle
), handle_bytes_write(handle
));
314 logit("%s%sclosedir \"%s\"",
315 emsg
== NULL
? "" : emsg
, emsg
== NULL
? "" : " ",
316 handle_to_name(handle
));
321 handle_log_exit(void)
325 for (i
= 0; i
< sizeof(handles
)/sizeof(Handle
); i
++)
326 if (handles
[i
].use
!= HANDLE_UNUSED
)
327 handle_log_close(i
, "forced");
337 handle
= get_string(&hlen
);
339 val
= handle_from_string(handle
, hlen
);
349 int mlen
= buffer_len(m
);
351 buffer_put_int(&oqueue
, mlen
);
352 buffer_append(&oqueue
, buffer_ptr(m
), mlen
);
353 buffer_consume(m
, mlen
);
357 status_to_message(u_int32_t status
)
359 const char *status_messages
[] = {
360 "Success", /* SSH_FX_OK */
361 "End of file", /* SSH_FX_EOF */
362 "No such file", /* SSH_FX_NO_SUCH_FILE */
363 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
364 "Failure", /* SSH_FX_FAILURE */
365 "Bad message", /* SSH_FX_BAD_MESSAGE */
366 "No connection", /* SSH_FX_NO_CONNECTION */
367 "Connection lost", /* SSH_FX_CONNECTION_LOST */
368 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
369 "Unknown error" /* Others */
371 return (status_messages
[MIN(status
,SSH2_FX_MAX
)]);
375 send_status(u_int32_t id
, u_int32_t status
)
379 debug3("request %u: sent status %u", id
, status
);
380 if (log_level
> SYSLOG_LEVEL_VERBOSE
||
381 (status
!= SSH2_FX_OK
&& status
!= SSH2_FX_EOF
))
382 logit("sent status %s", status_to_message(status
));
384 buffer_put_char(&msg
, SSH2_FXP_STATUS
);
385 buffer_put_int(&msg
, id
);
386 buffer_put_int(&msg
, status
);
388 buffer_put_cstring(&msg
, status_to_message(status
));
389 buffer_put_cstring(&msg
, "");
395 send_data_or_handle(char type
, u_int32_t id
, const char *data
, int dlen
)
400 buffer_put_char(&msg
, type
);
401 buffer_put_int(&msg
, id
);
402 buffer_put_string(&msg
, data
, dlen
);
408 send_data(u_int32_t id
, const char *data
, int dlen
)
410 debug("request %u: sent data len %d", id
, dlen
);
411 send_data_or_handle(SSH2_FXP_DATA
, id
, data
, dlen
);
415 send_handle(u_int32_t id
, int handle
)
420 handle_to_string(handle
, &string
, &hlen
);
421 debug("request %u: sent handle handle %d", id
, handle
);
422 send_data_or_handle(SSH2_FXP_HANDLE
, id
, string
, hlen
);
427 send_names(u_int32_t id
, int count
, const Stat
*stats
)
433 buffer_put_char(&msg
, SSH2_FXP_NAME
);
434 buffer_put_int(&msg
, id
);
435 buffer_put_int(&msg
, count
);
436 debug("request %u: sent names count %d", id
, count
);
437 for (i
= 0; i
< count
; i
++) {
438 buffer_put_cstring(&msg
, stats
[i
].name
);
439 buffer_put_cstring(&msg
, stats
[i
].long_name
);
440 encode_attrib(&msg
, &stats
[i
].attrib
);
447 send_attrib(u_int32_t id
, const Attrib
*a
)
451 debug("request %u: sent attrib have 0x%x", id
, a
->flags
);
453 buffer_put_char(&msg
, SSH2_FXP_ATTRS
);
454 buffer_put_int(&msg
, id
);
455 encode_attrib(&msg
, a
);
468 verbose("received client version %d", version
);
470 buffer_put_char(&msg
, SSH2_FXP_VERSION
);
471 buffer_put_int(&msg
, SSH2_FILEXFER_VERSION
);
479 u_int32_t id
, pflags
;
482 int handle
, fd
, flags
, mode
, status
= SSH2_FX_FAILURE
;
485 name
= get_string(NULL
);
486 pflags
= get_int(); /* portable flags */
487 debug3("request %u: open flags %d", id
, pflags
);
489 flags
= flags_from_portable(pflags
);
490 mode
= (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) ? a
->perm
: 0666;
491 logit("open \"%s\" flags %s mode 0%o",
492 name
, string_from_portable(pflags
), mode
);
493 fd
= open(name
, flags
, mode
);
495 status
= errno_to_portable(errno
);
497 handle
= handle_new(HANDLE_FILE
, name
, fd
, NULL
);
501 send_handle(id
, handle
);
505 if (status
!= SSH2_FX_OK
)
506 send_status(id
, status
);
514 int handle
, ret
, status
= SSH2_FX_FAILURE
;
517 handle
= get_handle();
518 debug3("request %u: close handle %u", id
, handle
);
519 handle_log_close(handle
, NULL
);
520 ret
= handle_close(handle
);
521 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
522 send_status(id
, status
);
530 int handle
, fd
, ret
, status
= SSH2_FX_FAILURE
;
534 handle
= get_handle();
538 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
539 id
, handle_to_name(handle
), handle
, (unsigned long long)off
, len
);
540 if (len
> sizeof buf
) {
542 debug2("read change len %d", len
);
544 fd
= handle_to_fd(handle
);
546 if (lseek(fd
, off
, SEEK_SET
) < 0) {
547 error("process_read: seek failed");
548 status
= errno_to_portable(errno
);
550 ret
= read(fd
, buf
, len
);
552 status
= errno_to_portable(errno
);
553 } else if (ret
== 0) {
554 status
= SSH2_FX_EOF
;
556 send_data(id
, buf
, ret
);
558 handle_update_read(handle
, ret
);
562 if (status
!= SSH2_FX_OK
)
563 send_status(id
, status
);
572 int handle
, fd
, ret
, status
= SSH2_FX_FAILURE
;
576 handle
= get_handle();
578 data
= get_string(&len
);
580 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
581 id
, handle_to_name(handle
), handle
, (unsigned long long)off
, len
);
582 fd
= handle_to_fd(handle
);
584 if (lseek(fd
, off
, SEEK_SET
) < 0) {
585 status
= errno_to_portable(errno
);
586 error("process_write: seek failed");
589 ret
= write(fd
, data
, len
);
591 error("process_write: write failed");
592 status
= errno_to_portable(errno
);
593 } else if ((size_t)ret
== len
) {
595 handle_update_write(handle
, ret
);
597 debug2("nothing at all written");
601 send_status(id
, status
);
606 process_do_stat(int do_lstat
)
612 int ret
, status
= SSH2_FX_FAILURE
;
615 name
= get_string(NULL
);
616 debug3("request %u: %sstat", id
, do_lstat
? "l" : "");
617 verbose("%sstat name \"%s\"", do_lstat
? "l" : "", name
);
618 ret
= do_lstat
? lstat(name
, &st
) : stat(name
, &st
);
620 status
= errno_to_portable(errno
);
622 stat_to_attrib(&st
, &a
);
626 if (status
!= SSH2_FX_OK
)
627 send_status(id
, status
);
649 int fd
, ret
, handle
, status
= SSH2_FX_FAILURE
;
652 handle
= get_handle();
653 debug("request %u: fstat \"%s\" (handle %u)",
654 id
, handle_to_name(handle
), handle
);
655 fd
= handle_to_fd(handle
);
657 ret
= fstat(fd
, &st
);
659 status
= errno_to_portable(errno
);
661 stat_to_attrib(&st
, &a
);
666 if (status
!= SSH2_FX_OK
)
667 send_status(id
, status
);
670 static struct timeval
*
671 attrib_to_tv(const Attrib
*a
)
673 static struct timeval tv
[2];
675 tv
[0].tv_sec
= a
->atime
;
677 tv
[1].tv_sec
= a
->mtime
;
683 process_setstat(void)
688 int status
= SSH2_FX_OK
, ret
;
691 name
= get_string(NULL
);
693 debug("request %u: setstat name \"%s\"", id
, name
);
694 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
) {
695 logit("set \"%s\" size %llu", name
, a
->size
);
696 ret
= truncate(name
, a
->size
);
698 status
= errno_to_portable(errno
);
700 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
701 logit("set \"%s\" mode %04o", name
, a
->perm
);
702 ret
= chmod(name
, a
->perm
& 0777);
704 status
= errno_to_portable(errno
);
706 if (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
) {
710 strftime(buf
, sizeof(buf
), "%Y%m%d-%H:%M:%S",
712 logit("set \"%s\" modtime %s", name
, buf
);
713 ret
= utimes(name
, attrib_to_tv(a
));
715 status
= errno_to_portable(errno
);
717 if (a
->flags
& SSH2_FILEXFER_ATTR_UIDGID
) {
718 logit("set \"%s\" owner %lu group %lu", name
,
719 (u_long
)a
->uid
, (u_long
)a
->gid
);
720 ret
= chown(name
, a
->uid
, a
->gid
);
722 status
= errno_to_portable(errno
);
724 send_status(id
, status
);
729 process_fsetstat(void)
734 int status
= SSH2_FX_OK
;
737 handle
= get_handle();
739 debug("request %u: fsetstat handle %d", id
, handle
);
740 fd
= handle_to_fd(handle
);
742 status
= SSH2_FX_FAILURE
;
744 char *name
= handle_to_name(handle
);
746 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
) {
747 logit("set \"%s\" size %llu", name
, a
->size
);
748 ret
= ftruncate(fd
, a
->size
);
750 status
= errno_to_portable(errno
);
752 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
753 logit("set \"%s\" mode %04o", name
, a
->perm
);
755 ret
= fchmod(fd
, a
->perm
& 0777);
757 ret
= chmod(name
, a
->perm
& 0777);
760 status
= errno_to_portable(errno
);
762 if (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
) {
766 strftime(buf
, sizeof(buf
), "%Y%m%d-%H:%M:%S",
768 logit("set \"%s\" modtime %s", name
, buf
);
770 ret
= futimes(fd
, attrib_to_tv(a
));
772 ret
= utimes(name
, attrib_to_tv(a
));
775 status
= errno_to_portable(errno
);
777 if (a
->flags
& SSH2_FILEXFER_ATTR_UIDGID
) {
778 logit("set \"%s\" owner %lu group %lu", name
,
779 (u_long
)a
->uid
, (u_long
)a
->gid
);
781 ret
= fchown(fd
, a
->uid
, a
->gid
);
783 ret
= chown(name
, a
->uid
, a
->gid
);
786 status
= errno_to_portable(errno
);
789 send_status(id
, status
);
793 process_opendir(void)
797 int handle
, status
= SSH2_FX_FAILURE
;
801 path
= get_string(NULL
);
802 debug3("request %u: opendir", id
);
803 logit("opendir \"%s\"", path
);
804 dirp
= opendir(path
);
806 status
= errno_to_portable(errno
);
808 handle
= handle_new(HANDLE_DIR
, path
, 0, dirp
);
812 send_handle(id
, handle
);
817 if (status
!= SSH2_FX_OK
)
818 send_status(id
, status
);
823 process_readdir(void)
832 handle
= get_handle();
833 debug("request %u: readdir \"%s\" (handle %d)", id
,
834 handle_to_name(handle
), handle
);
835 dirp
= handle_to_dir(handle
);
836 path
= handle_to_name(handle
);
837 if (dirp
== NULL
|| path
== NULL
) {
838 send_status(id
, SSH2_FX_FAILURE
);
841 char pathname
[MAXPATHLEN
];
843 int nstats
= 10, count
= 0, i
;
845 stats
= xcalloc(nstats
, sizeof(Stat
));
846 while ((dp
= readdir(dirp
)) != NULL
) {
847 if (count
>= nstats
) {
849 stats
= xrealloc(stats
, nstats
, sizeof(Stat
));
852 snprintf(pathname
, sizeof pathname
, "%s%s%s", path
,
853 strcmp(path
, "/") ? "/" : "", dp
->d_name
);
854 if (lstat(pathname
, &st
) < 0)
856 stat_to_attrib(&st
, &(stats
[count
].attrib
));
857 stats
[count
].name
= xstrdup(dp
->d_name
);
858 stats
[count
].long_name
= ls_file(dp
->d_name
, &st
, 0);
860 /* send up to 100 entries in one message */
861 /* XXX check packet size instead */
866 send_names(id
, count
, stats
);
867 for (i
= 0; i
< count
; i
++) {
868 xfree(stats
[i
].name
);
869 xfree(stats
[i
].long_name
);
872 send_status(id
, SSH2_FX_EOF
);
883 int status
= SSH2_FX_FAILURE
;
887 name
= get_string(NULL
);
888 debug3("request %u: remove", id
);
889 logit("remove name \"%s\"", name
);
891 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
892 send_status(id
, status
);
902 int ret
, mode
, status
= SSH2_FX_FAILURE
;
905 name
= get_string(NULL
);
907 mode
= (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) ?
908 a
->perm
& 0777 : 0777;
909 debug3("request %u: mkdir", id
);
910 logit("mkdir name \"%s\" mode 0%o", name
, mode
);
911 ret
= mkdir(name
, mode
);
912 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
913 send_status(id
, status
);
925 name
= get_string(NULL
);
926 debug3("request %u: rmdir", id
);
927 logit("rmdir name \"%s\"", name
);
929 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
930 send_status(id
, status
);
935 process_realpath(void)
937 char resolvedname
[MAXPATHLEN
];
942 path
= get_string(NULL
);
943 if (path
[0] == '\0') {
947 debug3("request %u: realpath", id
);
948 verbose("realpath \"%s\"", path
);
949 if (realpath(path
, resolvedname
) == NULL
) {
950 send_status(id
, errno_to_portable(errno
));
953 attrib_clear(&s
.attrib
);
954 s
.name
= s
.long_name
= resolvedname
;
955 send_names(id
, 1, &s
);
964 char *oldpath
, *newpath
;
969 oldpath
= get_string(NULL
);
970 newpath
= get_string(NULL
);
971 debug3("request %u: rename", id
);
972 logit("rename old \"%s\" new \"%s\"", oldpath
, newpath
);
973 status
= SSH2_FX_FAILURE
;
974 if (lstat(oldpath
, &sb
) == -1)
975 status
= errno_to_portable(errno
);
976 else if (S_ISREG(sb
.st_mode
)) {
977 /* Race-free rename of regular files */
978 if (link(oldpath
, newpath
) == -1) {
979 if (errno
== EOPNOTSUPP
980 #ifdef LINK_OPNOTSUPP_ERRNO
981 || errno
== LINK_OPNOTSUPP_ERRNO
987 * fs doesn't support links, so fall back to
988 * stat+rename. This is racy.
990 if (stat(newpath
, &st
) == -1) {
991 if (rename(oldpath
, newpath
) == -1)
993 errno_to_portable(errno
);
998 status
= errno_to_portable(errno
);
1000 } else if (unlink(oldpath
) == -1) {
1001 status
= errno_to_portable(errno
);
1002 /* clean spare link */
1005 status
= SSH2_FX_OK
;
1006 } else if (stat(newpath
, &sb
) == -1) {
1007 if (rename(oldpath
, newpath
) == -1)
1008 status
= errno_to_portable(errno
);
1010 status
= SSH2_FX_OK
;
1012 send_status(id
, status
);
1018 process_readlink(void)
1022 char buf
[MAXPATHLEN
];
1026 path
= get_string(NULL
);
1027 debug3("request %u: readlink", id
);
1028 verbose("readlink \"%s\"", path
);
1029 if ((len
= readlink(path
, buf
, sizeof(buf
) - 1)) == -1)
1030 send_status(id
, errno_to_portable(errno
));
1035 attrib_clear(&s
.attrib
);
1036 s
.name
= s
.long_name
= buf
;
1037 send_names(id
, 1, &s
);
1043 process_symlink(void)
1046 char *oldpath
, *newpath
;
1050 oldpath
= get_string(NULL
);
1051 newpath
= get_string(NULL
);
1052 debug3("request %u: symlink", id
);
1053 logit("symlink old \"%s\" new \"%s\"", oldpath
, newpath
);
1054 /* this will fail if 'newpath' exists */
1055 ret
= symlink(oldpath
, newpath
);
1056 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
1057 send_status(id
, status
);
1063 process_extended(void)
1069 request
= get_string(NULL
);
1070 send_status(id
, SSH2_FX_OP_UNSUPPORTED
); /* MUST */
1074 /* stolen from ssh-agent */
1085 buf_len
= buffer_len(&iqueue
);
1087 return; /* Incomplete message. */
1088 cp
= buffer_ptr(&iqueue
);
1089 msg_len
= get_u32(cp
);
1090 if (msg_len
> SFTP_MAX_MSG_LENGTH
) {
1091 error("bad message from %s local user %s",
1092 client_addr
, pw
->pw_name
);
1095 if (buf_len
< msg_len
+ 4)
1097 buffer_consume(&iqueue
, 4);
1099 type
= buffer_get_char(&iqueue
);
1107 case SSH2_FXP_CLOSE
:
1113 case SSH2_FXP_WRITE
:
1116 case SSH2_FXP_LSTAT
:
1119 case SSH2_FXP_FSTAT
:
1122 case SSH2_FXP_SETSTAT
:
1125 case SSH2_FXP_FSETSTAT
:
1128 case SSH2_FXP_OPENDIR
:
1131 case SSH2_FXP_READDIR
:
1134 case SSH2_FXP_REMOVE
:
1137 case SSH2_FXP_MKDIR
:
1140 case SSH2_FXP_RMDIR
:
1143 case SSH2_FXP_REALPATH
:
1149 case SSH2_FXP_RENAME
:
1152 case SSH2_FXP_READLINK
:
1155 case SSH2_FXP_SYMLINK
:
1158 case SSH2_FXP_EXTENDED
:
1162 error("Unknown message %d", type
);
1165 /* discard the remaining bytes from the current packet */
1166 if (buf_len
< buffer_len(&iqueue
))
1167 fatal("iqueue grew unexpectedly");
1168 consumed
= buf_len
- buffer_len(&iqueue
);
1169 if (msg_len
< consumed
)
1170 fatal("msg_len %d < consumed %d", msg_len
, consumed
);
1171 if (msg_len
> consumed
)
1172 buffer_consume(&iqueue
, msg_len
- consumed
);
1175 /* Cleanup handler that logs active handles upon normal exit */
1179 if (pw
!= NULL
&& client_addr
!= NULL
) {
1181 logit("session closed for local user %s from [%s]",
1182 pw
->pw_name
, client_addr
);
1190 extern char *__progname
;
1193 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname
);
1198 main(int argc
, char **argv
)
1200 fd_set
*rset
, *wset
;
1201 int in
, out
, max
, ch
, skipargs
= 0, log_stderr
= 0;
1202 ssize_t len
, olen
, set_size
;
1203 SyslogFacility log_facility
= SYSLOG_FACILITY_AUTH
;
1206 extern char *optarg
;
1207 extern char *__progname
;
1209 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1212 __progname
= ssh_get_progname(argv
[0]);
1213 log_init(__progname
, log_level
, log_facility
, log_stderr
);
1215 while (!skipargs
&& (ch
= getopt(argc
, argv
, "C:f:l:che")) != -1) {
1219 * Ignore all arguments if we are invoked as a
1220 * shell using "sftp-server -c command"
1228 log_level
= log_level_number(optarg
);
1229 if (log_level
== SYSLOG_LEVEL_NOT_SET
)
1230 error("Invalid log level \"%s\"", optarg
);
1233 log_facility
= log_facility_number(optarg
);
1234 if (log_level
== SYSLOG_FACILITY_NOT_SET
)
1235 error("Invalid log facility \"%s\"", optarg
);
1243 log_init(__progname
, log_level
, log_facility
, log_stderr
);
1245 if ((cp
= getenv("SSH_CONNECTION")) != NULL
) {
1246 client_addr
= xstrdup(cp
);
1247 if ((cp
= strchr(client_addr
, ' ')) == NULL
)
1248 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1249 getenv("SSH_CONNECTION"));
1252 client_addr
= xstrdup("UNKNOWN");
1254 if ((pw
= getpwuid(getuid())) == NULL
)
1255 fatal("No user found for uid %lu", (u_long
)getuid());
1258 logit("session opened for local user %s from [%s]",
1259 pw
->pw_name
, client_addr
);
1263 in
= dup(STDIN_FILENO
);
1264 out
= dup(STDOUT_FILENO
);
1267 setmode(in
, O_BINARY
);
1268 setmode(out
, O_BINARY
);
1277 buffer_init(&iqueue
);
1278 buffer_init(&oqueue
);
1280 set_size
= howmany(max
+ 1, NFDBITS
) * sizeof(fd_mask
);
1281 rset
= (fd_set
*)xmalloc(set_size
);
1282 wset
= (fd_set
*)xmalloc(set_size
);
1285 memset(rset
, 0, set_size
);
1286 memset(wset
, 0, set_size
);
1289 olen
= buffer_len(&oqueue
);
1293 if (select(max
+1, rset
, wset
, NULL
, NULL
) < 0) {
1296 error("select: %s", strerror(errno
));
1300 /* copy stdin to iqueue */
1301 if (FD_ISSET(in
, rset
)) {
1303 len
= read(in
, buf
, sizeof buf
);
1307 } else if (len
< 0) {
1308 error("read: %s", strerror(errno
));
1311 buffer_append(&iqueue
, buf
, len
);
1314 /* send oqueue to stdout */
1315 if (FD_ISSET(out
, wset
)) {
1316 len
= write(out
, buffer_ptr(&oqueue
), olen
);
1318 error("write: %s", strerror(errno
));
1321 buffer_consume(&oqueue
, len
);
1324 /* process requests from client */