1 /* $OpenBSD: sftp-server.c,v 1.84 2008/06/26 06:10:09 djm 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>
26 #ifdef HAVE_SYS_MOUNT_H
27 #include <sys/mount.h>
29 #ifdef HAVE_SYS_STATVFS_H
30 #include <sys/statvfs.h>
52 #include "sftp-common.h"
55 #define get_int64() buffer_get_int64(&iqueue);
56 #define get_int() buffer_get_int(&iqueue);
57 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
60 LogLevel log_level
= SYSLOG_LEVEL_ERROR
;
63 struct passwd
*pw
= NULL
;
64 char *client_addr
= NULL
;
66 /* input and output queue */
70 /* Version of client */
73 /* portable attributes, etc. */
75 typedef struct Stat Stat
;
84 errno_to_portable(int unixerrno
)
96 ret
= SSH2_FX_NO_SUCH_FILE
;
101 ret
= SSH2_FX_PERMISSION_DENIED
;
105 ret
= SSH2_FX_BAD_MESSAGE
;
108 ret
= SSH2_FX_OP_UNSUPPORTED
;
111 ret
= SSH2_FX_FAILURE
;
118 flags_from_portable(int pflags
)
122 if ((pflags
& SSH2_FXF_READ
) &&
123 (pflags
& SSH2_FXF_WRITE
)) {
125 } else if (pflags
& SSH2_FXF_READ
) {
127 } else if (pflags
& SSH2_FXF_WRITE
) {
130 if (pflags
& SSH2_FXF_CREAT
)
132 if (pflags
& SSH2_FXF_TRUNC
)
134 if (pflags
& SSH2_FXF_EXCL
)
140 string_from_portable(int pflags
)
142 static char ret
[128];
146 #define PAPPEND(str) { \
148 strlcat(ret, ",", sizeof(ret)); \
149 strlcat(ret, str, sizeof(ret)); \
152 if (pflags
& SSH2_FXF_READ
)
154 if (pflags
& SSH2_FXF_WRITE
)
156 if (pflags
& SSH2_FXF_CREAT
)
158 if (pflags
& SSH2_FXF_TRUNC
)
160 if (pflags
& SSH2_FXF_EXCL
)
169 return decode_attrib(&iqueue
);
174 typedef struct Handle Handle
;
180 u_int64_t bytes_read
, bytes_write
;
190 Handle
*handles
= NULL
;
191 u_int num_handles
= 0;
192 int first_unused_handle
= -1;
194 static void handle_unused(int i
)
196 handles
[i
].use
= HANDLE_UNUSED
;
197 handles
[i
].next_unused
= first_unused_handle
;
198 first_unused_handle
= i
;
202 handle_new(int use
, const char *name
, int fd
, DIR *dirp
)
206 if (first_unused_handle
== -1) {
207 if (num_handles
+ 1 <= num_handles
)
210 handles
= xrealloc(handles
, num_handles
, sizeof(Handle
));
211 handle_unused(num_handles
- 1);
214 i
= first_unused_handle
;
215 first_unused_handle
= handles
[i
].next_unused
;
217 handles
[i
].use
= use
;
218 handles
[i
].dirp
= dirp
;
220 handles
[i
].name
= xstrdup(name
);
221 handles
[i
].bytes_read
= handles
[i
].bytes_write
= 0;
227 handle_is_ok(int i
, int type
)
229 return i
>= 0 && (u_int
)i
< num_handles
&& handles
[i
].use
== type
;
233 handle_to_string(int handle
, char **stringp
, int *hlenp
)
235 if (stringp
== NULL
|| hlenp
== NULL
)
237 *stringp
= xmalloc(sizeof(int32_t));
238 put_u32(*stringp
, handle
);
239 *hlenp
= sizeof(int32_t);
244 handle_from_string(const char *handle
, u_int hlen
)
248 if (hlen
!= sizeof(int32_t))
250 val
= get_u32(handle
);
251 if (handle_is_ok(val
, HANDLE_FILE
) ||
252 handle_is_ok(val
, HANDLE_DIR
))
258 handle_to_name(int handle
)
260 if (handle_is_ok(handle
, HANDLE_DIR
)||
261 handle_is_ok(handle
, HANDLE_FILE
))
262 return handles
[handle
].name
;
267 handle_to_dir(int handle
)
269 if (handle_is_ok(handle
, HANDLE_DIR
))
270 return handles
[handle
].dirp
;
275 handle_to_fd(int handle
)
277 if (handle_is_ok(handle
, HANDLE_FILE
))
278 return handles
[handle
].fd
;
283 handle_update_read(int handle
, ssize_t bytes
)
285 if (handle_is_ok(handle
, HANDLE_FILE
) && bytes
> 0)
286 handles
[handle
].bytes_read
+= bytes
;
290 handle_update_write(int handle
, ssize_t bytes
)
292 if (handle_is_ok(handle
, HANDLE_FILE
) && bytes
> 0)
293 handles
[handle
].bytes_write
+= bytes
;
297 handle_bytes_read(int handle
)
299 if (handle_is_ok(handle
, HANDLE_FILE
))
300 return (handles
[handle
].bytes_read
);
305 handle_bytes_write(int handle
)
307 if (handle_is_ok(handle
, HANDLE_FILE
))
308 return (handles
[handle
].bytes_write
);
313 handle_close(int handle
)
317 if (handle_is_ok(handle
, HANDLE_FILE
)) {
318 ret
= close(handles
[handle
].fd
);
319 xfree(handles
[handle
].name
);
320 handle_unused(handle
);
321 } else if (handle_is_ok(handle
, HANDLE_DIR
)) {
322 ret
= closedir(handles
[handle
].dirp
);
323 xfree(handles
[handle
].name
);
324 handle_unused(handle
);
332 handle_log_close(int handle
, char *emsg
)
334 if (handle_is_ok(handle
, HANDLE_FILE
)) {
335 logit("%s%sclose \"%s\" bytes read %llu written %llu",
336 emsg
== NULL
? "" : emsg
, emsg
== NULL
? "" : " ",
337 handle_to_name(handle
),
338 (unsigned long long)handle_bytes_read(handle
),
339 (unsigned long long)handle_bytes_write(handle
));
341 logit("%s%sclosedir \"%s\"",
342 emsg
== NULL
? "" : emsg
, emsg
== NULL
? "" : " ",
343 handle_to_name(handle
));
348 handle_log_exit(void)
352 for (i
= 0; i
< num_handles
; i
++)
353 if (handles
[i
].use
!= HANDLE_UNUSED
)
354 handle_log_close(i
, "forced");
364 handle
= get_string(&hlen
);
366 val
= handle_from_string(handle
, hlen
);
376 int mlen
= buffer_len(m
);
378 buffer_put_int(&oqueue
, mlen
);
379 buffer_append(&oqueue
, buffer_ptr(m
), mlen
);
380 buffer_consume(m
, mlen
);
384 status_to_message(u_int32_t status
)
386 const char *status_messages
[] = {
387 "Success", /* SSH_FX_OK */
388 "End of file", /* SSH_FX_EOF */
389 "No such file", /* SSH_FX_NO_SUCH_FILE */
390 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
391 "Failure", /* SSH_FX_FAILURE */
392 "Bad message", /* SSH_FX_BAD_MESSAGE */
393 "No connection", /* SSH_FX_NO_CONNECTION */
394 "Connection lost", /* SSH_FX_CONNECTION_LOST */
395 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
396 "Unknown error" /* Others */
398 return (status_messages
[MIN(status
,SSH2_FX_MAX
)]);
402 send_status(u_int32_t id
, u_int32_t status
)
406 debug3("request %u: sent status %u", id
, status
);
407 if (log_level
> SYSLOG_LEVEL_VERBOSE
||
408 (status
!= SSH2_FX_OK
&& status
!= SSH2_FX_EOF
))
409 logit("sent status %s", status_to_message(status
));
411 buffer_put_char(&msg
, SSH2_FXP_STATUS
);
412 buffer_put_int(&msg
, id
);
413 buffer_put_int(&msg
, status
);
415 buffer_put_cstring(&msg
, status_to_message(status
));
416 buffer_put_cstring(&msg
, "");
422 send_data_or_handle(char type
, u_int32_t id
, const char *data
, int dlen
)
427 buffer_put_char(&msg
, type
);
428 buffer_put_int(&msg
, id
);
429 buffer_put_string(&msg
, data
, dlen
);
435 send_data(u_int32_t id
, const char *data
, int dlen
)
437 debug("request %u: sent data len %d", id
, dlen
);
438 send_data_or_handle(SSH2_FXP_DATA
, id
, data
, dlen
);
442 send_handle(u_int32_t id
, int handle
)
447 handle_to_string(handle
, &string
, &hlen
);
448 debug("request %u: sent handle handle %d", id
, handle
);
449 send_data_or_handle(SSH2_FXP_HANDLE
, id
, string
, hlen
);
454 send_names(u_int32_t id
, int count
, const Stat
*stats
)
460 buffer_put_char(&msg
, SSH2_FXP_NAME
);
461 buffer_put_int(&msg
, id
);
462 buffer_put_int(&msg
, count
);
463 debug("request %u: sent names count %d", id
, count
);
464 for (i
= 0; i
< count
; i
++) {
465 buffer_put_cstring(&msg
, stats
[i
].name
);
466 buffer_put_cstring(&msg
, stats
[i
].long_name
);
467 encode_attrib(&msg
, &stats
[i
].attrib
);
474 send_attrib(u_int32_t id
, const Attrib
*a
)
478 debug("request %u: sent attrib have 0x%x", id
, a
->flags
);
480 buffer_put_char(&msg
, SSH2_FXP_ATTRS
);
481 buffer_put_int(&msg
, id
);
482 encode_attrib(&msg
, a
);
488 send_statvfs(u_int32_t id
, struct statvfs
*st
)
493 flag
= (st
->f_flag
& ST_RDONLY
) ? SSH2_FXE_STATVFS_ST_RDONLY
: 0;
494 flag
|= (st
->f_flag
& ST_NOSUID
) ? SSH2_FXE_STATVFS_ST_NOSUID
: 0;
497 buffer_put_char(&msg
, SSH2_FXP_EXTENDED_REPLY
);
498 buffer_put_int(&msg
, id
);
499 buffer_put_int64(&msg
, st
->f_bsize
);
500 buffer_put_int64(&msg
, st
->f_frsize
);
501 buffer_put_int64(&msg
, st
->f_blocks
);
502 buffer_put_int64(&msg
, st
->f_bfree
);
503 buffer_put_int64(&msg
, st
->f_bavail
);
504 buffer_put_int64(&msg
, st
->f_files
);
505 buffer_put_int64(&msg
, st
->f_ffree
);
506 buffer_put_int64(&msg
, st
->f_favail
);
507 buffer_put_int64(&msg
, FSID_TO_ULONG(st
->f_fsid
));
508 buffer_put_int64(&msg
, flag
);
509 buffer_put_int64(&msg
, st
->f_namemax
);
522 verbose("received client version %d", version
);
524 buffer_put_char(&msg
, SSH2_FXP_VERSION
);
525 buffer_put_int(&msg
, SSH2_FILEXFER_VERSION
);
526 /* POSIX rename extension */
527 buffer_put_cstring(&msg
, "posix-rename@openssh.com");
528 buffer_put_cstring(&msg
, "1"); /* version */
529 /* statvfs extension */
530 buffer_put_cstring(&msg
, "statvfs@openssh.com");
531 buffer_put_cstring(&msg
, "2"); /* version */
532 /* fstatvfs extension */
533 buffer_put_cstring(&msg
, "fstatvfs@openssh.com");
534 buffer_put_cstring(&msg
, "2"); /* version */
542 u_int32_t id
, pflags
;
545 int handle
, fd
, flags
, mode
, status
= SSH2_FX_FAILURE
;
548 name
= get_string(NULL
);
549 pflags
= get_int(); /* portable flags */
550 debug3("request %u: open flags %d", id
, pflags
);
552 flags
= flags_from_portable(pflags
);
553 mode
= (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) ? a
->perm
: 0666;
554 logit("open \"%s\" flags %s mode 0%o",
555 name
, string_from_portable(pflags
), mode
);
556 fd
= open(name
, flags
, mode
);
558 status
= errno_to_portable(errno
);
560 handle
= handle_new(HANDLE_FILE
, name
, fd
, NULL
);
564 send_handle(id
, handle
);
568 if (status
!= SSH2_FX_OK
)
569 send_status(id
, status
);
577 int handle
, ret
, status
= SSH2_FX_FAILURE
;
580 handle
= get_handle();
581 debug3("request %u: close handle %u", id
, handle
);
582 handle_log_close(handle
, NULL
);
583 ret
= handle_close(handle
);
584 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
585 send_status(id
, status
);
593 int handle
, fd
, ret
, status
= SSH2_FX_FAILURE
;
597 handle
= get_handle();
601 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
602 id
, handle_to_name(handle
), handle
, (unsigned long long)off
, len
);
603 if (len
> sizeof buf
) {
605 debug2("read change len %d", len
);
607 fd
= handle_to_fd(handle
);
609 if (lseek(fd
, off
, SEEK_SET
) < 0) {
610 error("process_read: seek failed");
611 status
= errno_to_portable(errno
);
613 ret
= read(fd
, buf
, len
);
615 status
= errno_to_portable(errno
);
616 } else if (ret
== 0) {
617 status
= SSH2_FX_EOF
;
619 send_data(id
, buf
, ret
);
621 handle_update_read(handle
, ret
);
625 if (status
!= SSH2_FX_OK
)
626 send_status(id
, status
);
635 int handle
, fd
, ret
, status
= SSH2_FX_FAILURE
;
639 handle
= get_handle();
641 data
= get_string(&len
);
643 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
644 id
, handle_to_name(handle
), handle
, (unsigned long long)off
, len
);
645 fd
= handle_to_fd(handle
);
647 if (lseek(fd
, off
, SEEK_SET
) < 0) {
648 status
= errno_to_portable(errno
);
649 error("process_write: seek failed");
652 ret
= write(fd
, data
, len
);
654 error("process_write: write failed");
655 status
= errno_to_portable(errno
);
656 } else if ((size_t)ret
== len
) {
658 handle_update_write(handle
, ret
);
660 debug2("nothing at all written");
664 send_status(id
, status
);
669 process_do_stat(int do_lstat
)
675 int ret
, status
= SSH2_FX_FAILURE
;
678 name
= get_string(NULL
);
679 debug3("request %u: %sstat", id
, do_lstat
? "l" : "");
680 verbose("%sstat name \"%s\"", do_lstat
? "l" : "", name
);
681 ret
= do_lstat
? lstat(name
, &st
) : stat(name
, &st
);
683 status
= errno_to_portable(errno
);
685 stat_to_attrib(&st
, &a
);
689 if (status
!= SSH2_FX_OK
)
690 send_status(id
, status
);
712 int fd
, ret
, handle
, status
= SSH2_FX_FAILURE
;
715 handle
= get_handle();
716 debug("request %u: fstat \"%s\" (handle %u)",
717 id
, handle_to_name(handle
), handle
);
718 fd
= handle_to_fd(handle
);
720 ret
= fstat(fd
, &st
);
722 status
= errno_to_portable(errno
);
724 stat_to_attrib(&st
, &a
);
729 if (status
!= SSH2_FX_OK
)
730 send_status(id
, status
);
733 static struct timeval
*
734 attrib_to_tv(const Attrib
*a
)
736 static struct timeval tv
[2];
738 tv
[0].tv_sec
= a
->atime
;
740 tv
[1].tv_sec
= a
->mtime
;
746 process_setstat(void)
751 int status
= SSH2_FX_OK
, ret
;
754 name
= get_string(NULL
);
756 debug("request %u: setstat name \"%s\"", id
, name
);
757 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
) {
758 logit("set \"%s\" size %llu",
759 name
, (unsigned long long)a
->size
);
760 ret
= truncate(name
, a
->size
);
762 status
= errno_to_portable(errno
);
764 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
765 logit("set \"%s\" mode %04o", name
, a
->perm
);
766 ret
= chmod(name
, a
->perm
& 07777);
768 status
= errno_to_portable(errno
);
770 if (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
) {
774 strftime(buf
, sizeof(buf
), "%Y%m%d-%H:%M:%S",
776 logit("set \"%s\" modtime %s", name
, buf
);
777 ret
= utimes(name
, attrib_to_tv(a
));
779 status
= errno_to_portable(errno
);
781 if (a
->flags
& SSH2_FILEXFER_ATTR_UIDGID
) {
782 logit("set \"%s\" owner %lu group %lu", name
,
783 (u_long
)a
->uid
, (u_long
)a
->gid
);
784 ret
= chown(name
, a
->uid
, a
->gid
);
786 status
= errno_to_portable(errno
);
788 send_status(id
, status
);
793 process_fsetstat(void)
798 int status
= SSH2_FX_OK
;
801 handle
= get_handle();
803 debug("request %u: fsetstat handle %d", id
, handle
);
804 fd
= handle_to_fd(handle
);
806 status
= SSH2_FX_FAILURE
;
808 char *name
= handle_to_name(handle
);
810 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
) {
811 logit("set \"%s\" size %llu",
812 name
, (unsigned long long)a
->size
);
813 ret
= ftruncate(fd
, a
->size
);
815 status
= errno_to_portable(errno
);
817 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) {
818 logit("set \"%s\" mode %04o", name
, a
->perm
);
820 ret
= fchmod(fd
, a
->perm
& 07777);
822 ret
= chmod(name
, a
->perm
& 07777);
825 status
= errno_to_portable(errno
);
827 if (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
) {
831 strftime(buf
, sizeof(buf
), "%Y%m%d-%H:%M:%S",
833 logit("set \"%s\" modtime %s", name
, buf
);
835 ret
= futimes(fd
, attrib_to_tv(a
));
837 ret
= utimes(name
, attrib_to_tv(a
));
840 status
= errno_to_portable(errno
);
842 if (a
->flags
& SSH2_FILEXFER_ATTR_UIDGID
) {
843 logit("set \"%s\" owner %lu group %lu", name
,
844 (u_long
)a
->uid
, (u_long
)a
->gid
);
846 ret
= fchown(fd
, a
->uid
, a
->gid
);
848 ret
= chown(name
, a
->uid
, a
->gid
);
851 status
= errno_to_portable(errno
);
854 send_status(id
, status
);
858 process_opendir(void)
862 int handle
, status
= SSH2_FX_FAILURE
;
866 path
= get_string(NULL
);
867 debug3("request %u: opendir", id
);
868 logit("opendir \"%s\"", path
);
869 dirp
= opendir(path
);
871 status
= errno_to_portable(errno
);
873 handle
= handle_new(HANDLE_DIR
, path
, 0, dirp
);
877 send_handle(id
, handle
);
882 if (status
!= SSH2_FX_OK
)
883 send_status(id
, status
);
888 process_readdir(void)
897 handle
= get_handle();
898 debug("request %u: readdir \"%s\" (handle %d)", id
,
899 handle_to_name(handle
), handle
);
900 dirp
= handle_to_dir(handle
);
901 path
= handle_to_name(handle
);
902 if (dirp
== NULL
|| path
== NULL
) {
903 send_status(id
, SSH2_FX_FAILURE
);
906 char pathname
[MAXPATHLEN
];
908 int nstats
= 10, count
= 0, i
;
910 stats
= xcalloc(nstats
, sizeof(Stat
));
911 while ((dp
= readdir(dirp
)) != NULL
) {
912 if (count
>= nstats
) {
914 stats
= xrealloc(stats
, nstats
, sizeof(Stat
));
917 snprintf(pathname
, sizeof pathname
, "%s%s%s", path
,
918 strcmp(path
, "/") ? "/" : "", dp
->d_name
);
919 if (lstat(pathname
, &st
) < 0)
921 stat_to_attrib(&st
, &(stats
[count
].attrib
));
922 stats
[count
].name
= xstrdup(dp
->d_name
);
923 stats
[count
].long_name
= ls_file(dp
->d_name
, &st
, 0);
925 /* send up to 100 entries in one message */
926 /* XXX check packet size instead */
931 send_names(id
, count
, stats
);
932 for (i
= 0; i
< count
; i
++) {
933 xfree(stats
[i
].name
);
934 xfree(stats
[i
].long_name
);
937 send_status(id
, SSH2_FX_EOF
);
948 int status
= SSH2_FX_FAILURE
;
952 name
= get_string(NULL
);
953 debug3("request %u: remove", id
);
954 logit("remove name \"%s\"", name
);
956 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
957 send_status(id
, status
);
967 int ret
, mode
, status
= SSH2_FX_FAILURE
;
970 name
= get_string(NULL
);
972 mode
= (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) ?
973 a
->perm
& 07777 : 0777;
974 debug3("request %u: mkdir", id
);
975 logit("mkdir name \"%s\" mode 0%o", name
, mode
);
976 ret
= mkdir(name
, mode
);
977 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
978 send_status(id
, status
);
990 name
= get_string(NULL
);
991 debug3("request %u: rmdir", id
);
992 logit("rmdir name \"%s\"", name
);
994 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
995 send_status(id
, status
);
1000 process_realpath(void)
1002 char resolvedname
[MAXPATHLEN
];
1007 path
= get_string(NULL
);
1008 if (path
[0] == '\0') {
1010 path
= xstrdup(".");
1012 debug3("request %u: realpath", id
);
1013 verbose("realpath \"%s\"", path
);
1014 if (realpath(path
, resolvedname
) == NULL
) {
1015 send_status(id
, errno_to_portable(errno
));
1018 attrib_clear(&s
.attrib
);
1019 s
.name
= s
.long_name
= resolvedname
;
1020 send_names(id
, 1, &s
);
1026 process_rename(void)
1029 char *oldpath
, *newpath
;
1034 oldpath
= get_string(NULL
);
1035 newpath
= get_string(NULL
);
1036 debug3("request %u: rename", id
);
1037 logit("rename old \"%s\" new \"%s\"", oldpath
, newpath
);
1038 status
= SSH2_FX_FAILURE
;
1039 if (lstat(oldpath
, &sb
) == -1)
1040 status
= errno_to_portable(errno
);
1041 else if (S_ISREG(sb
.st_mode
)) {
1042 /* Race-free rename of regular files */
1043 if (link(oldpath
, newpath
) == -1) {
1044 if (errno
== EOPNOTSUPP
1048 #ifdef LINK_OPNOTSUPP_ERRNO
1049 || errno
== LINK_OPNOTSUPP_ERRNO
1055 * fs doesn't support links, so fall back to
1056 * stat+rename. This is racy.
1058 if (stat(newpath
, &st
) == -1) {
1059 if (rename(oldpath
, newpath
) == -1)
1061 errno_to_portable(errno
);
1063 status
= SSH2_FX_OK
;
1066 status
= errno_to_portable(errno
);
1068 } else if (unlink(oldpath
) == -1) {
1069 status
= errno_to_portable(errno
);
1070 /* clean spare link */
1073 status
= SSH2_FX_OK
;
1074 } else if (stat(newpath
, &sb
) == -1) {
1075 if (rename(oldpath
, newpath
) == -1)
1076 status
= errno_to_portable(errno
);
1078 status
= SSH2_FX_OK
;
1080 send_status(id
, status
);
1086 process_readlink(void)
1090 char buf
[MAXPATHLEN
];
1094 path
= get_string(NULL
);
1095 debug3("request %u: readlink", id
);
1096 verbose("readlink \"%s\"", path
);
1097 if ((len
= readlink(path
, buf
, sizeof(buf
) - 1)) == -1)
1098 send_status(id
, errno_to_portable(errno
));
1103 attrib_clear(&s
.attrib
);
1104 s
.name
= s
.long_name
= buf
;
1105 send_names(id
, 1, &s
);
1111 process_symlink(void)
1114 char *oldpath
, *newpath
;
1118 oldpath
= get_string(NULL
);
1119 newpath
= get_string(NULL
);
1120 debug3("request %u: symlink", id
);
1121 logit("symlink old \"%s\" new \"%s\"", oldpath
, newpath
);
1122 /* this will fail if 'newpath' exists */
1123 ret
= symlink(oldpath
, newpath
);
1124 status
= (ret
== -1) ? errno_to_portable(errno
) : SSH2_FX_OK
;
1125 send_status(id
, status
);
1131 process_extended_posix_rename(u_int32_t id
)
1133 char *oldpath
, *newpath
;
1135 oldpath
= get_string(NULL
);
1136 newpath
= get_string(NULL
);
1137 debug3("request %u: posix-rename", id
);
1138 logit("posix-rename old \"%s\" new \"%s\"", oldpath
, newpath
);
1139 if (rename(oldpath
, newpath
) == -1)
1140 send_status(id
, errno_to_portable(errno
));
1142 send_status(id
, SSH2_FX_OK
);
1148 process_extended_statvfs(u_int32_t id
)
1153 path
= get_string(NULL
);
1154 debug3("request %u: statfs", id
);
1155 logit("statfs \"%s\"", path
);
1157 if (statvfs(path
, &st
) != 0)
1158 send_status(id
, errno_to_portable(errno
));
1160 send_statvfs(id
, &st
);
1165 process_extended_fstatvfs(u_int32_t id
)
1170 handle
= get_handle();
1171 debug("request %u: fstatvfs \"%s\" (handle %u)",
1172 id
, handle_to_name(handle
), handle
);
1173 if ((fd
= handle_to_fd(handle
)) < 0) {
1174 send_status(id
, SSH2_FX_FAILURE
);
1177 if (fstatvfs(fd
, &st
) != 0)
1178 send_status(id
, errno_to_portable(errno
));
1180 send_statvfs(id
, &st
);
1184 process_extended(void)
1190 request
= get_string(NULL
);
1191 if (strcmp(request
, "posix-rename@openssh.com") == 0)
1192 process_extended_posix_rename(id
);
1193 else if (strcmp(request
, "statvfs@openssh.com") == 0)
1194 process_extended_statvfs(id
);
1195 else if (strcmp(request
, "fstatvfs@openssh.com") == 0)
1196 process_extended_fstatvfs(id
);
1198 send_status(id
, SSH2_FX_OP_UNSUPPORTED
); /* MUST */
1202 /* stolen from ssh-agent */
1213 buf_len
= buffer_len(&iqueue
);
1215 return; /* Incomplete message. */
1216 cp
= buffer_ptr(&iqueue
);
1217 msg_len
= get_u32(cp
);
1218 if (msg_len
> SFTP_MAX_MSG_LENGTH
) {
1219 error("bad message from %s local user %s",
1220 client_addr
, pw
->pw_name
);
1221 sftp_server_cleanup_exit(11);
1223 if (buf_len
< msg_len
+ 4)
1225 buffer_consume(&iqueue
, 4);
1227 type
= buffer_get_char(&iqueue
);
1235 case SSH2_FXP_CLOSE
:
1241 case SSH2_FXP_WRITE
:
1244 case SSH2_FXP_LSTAT
:
1247 case SSH2_FXP_FSTAT
:
1250 case SSH2_FXP_SETSTAT
:
1253 case SSH2_FXP_FSETSTAT
:
1256 case SSH2_FXP_OPENDIR
:
1259 case SSH2_FXP_READDIR
:
1262 case SSH2_FXP_REMOVE
:
1265 case SSH2_FXP_MKDIR
:
1268 case SSH2_FXP_RMDIR
:
1271 case SSH2_FXP_REALPATH
:
1277 case SSH2_FXP_RENAME
:
1280 case SSH2_FXP_READLINK
:
1283 case SSH2_FXP_SYMLINK
:
1286 case SSH2_FXP_EXTENDED
:
1290 error("Unknown message %d", type
);
1293 /* discard the remaining bytes from the current packet */
1294 if (buf_len
< buffer_len(&iqueue
)) {
1295 error("iqueue grew unexpectedly");
1296 sftp_server_cleanup_exit(255);
1298 consumed
= buf_len
- buffer_len(&iqueue
);
1299 if (msg_len
< consumed
) {
1300 error("msg_len %d < consumed %d", msg_len
, consumed
);
1301 sftp_server_cleanup_exit(255);
1303 if (msg_len
> consumed
)
1304 buffer_consume(&iqueue
, msg_len
- consumed
);
1307 /* Cleanup handler that logs active handles upon normal exit */
1309 sftp_server_cleanup_exit(int i
)
1311 if (pw
!= NULL
&& client_addr
!= NULL
) {
1313 logit("session closed for local user %s from [%s]",
1314 pw
->pw_name
, client_addr
);
1320 sftp_server_usage(void)
1322 extern char *__progname
;
1325 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname
);
1330 sftp_server_main(int argc
, char **argv
, struct passwd
*user_pw
)
1332 fd_set
*rset
, *wset
;
1333 int in
, out
, max
, ch
, skipargs
= 0, log_stderr
= 0;
1334 ssize_t len
, olen
, set_size
;
1335 SyslogFacility log_facility
= SYSLOG_FACILITY_AUTH
;
1336 char *cp
, buf
[4*4096];
1338 extern char *optarg
;
1339 extern char *__progname
;
1341 __progname
= ssh_get_progname(argv
[0]);
1342 log_init(__progname
, log_level
, log_facility
, log_stderr
);
1344 while (!skipargs
&& (ch
= getopt(argc
, argv
, "C:f:l:che")) != -1) {
1348 * Ignore all arguments if we are invoked as a
1349 * shell using "sftp-server -c command"
1357 log_level
= log_level_number(optarg
);
1358 if (log_level
== SYSLOG_LEVEL_NOT_SET
)
1359 error("Invalid log level \"%s\"", optarg
);
1362 log_facility
= log_facility_number(optarg
);
1363 if (log_facility
== SYSLOG_FACILITY_NOT_SET
)
1364 error("Invalid log facility \"%s\"", optarg
);
1368 sftp_server_usage();
1372 log_init(__progname
, log_level
, log_facility
, log_stderr
);
1374 if ((cp
= getenv("SSH_CONNECTION")) != NULL
) {
1375 client_addr
= xstrdup(cp
);
1376 if ((cp
= strchr(client_addr
, ' ')) == NULL
) {
1377 error("Malformed SSH_CONNECTION variable: \"%s\"",
1378 getenv("SSH_CONNECTION"));
1379 sftp_server_cleanup_exit(255);
1383 client_addr
= xstrdup("UNKNOWN");
1385 pw
= pwcopy(user_pw
);
1387 logit("session opened for local user %s from [%s]",
1388 pw
->pw_name
, client_addr
);
1390 in
= dup(STDIN_FILENO
);
1391 out
= dup(STDOUT_FILENO
);
1394 setmode(in
, O_BINARY
);
1395 setmode(out
, O_BINARY
);
1404 buffer_init(&iqueue
);
1405 buffer_init(&oqueue
);
1407 set_size
= howmany(max
+ 1, NFDBITS
) * sizeof(fd_mask
);
1408 rset
= (fd_set
*)xmalloc(set_size
);
1409 wset
= (fd_set
*)xmalloc(set_size
);
1412 memset(rset
, 0, set_size
);
1413 memset(wset
, 0, set_size
);
1416 * Ensure that we can read a full buffer and handle
1417 * the worst-case length packet it can generate,
1418 * otherwise apply backpressure by stopping reads.
1420 if (buffer_check_alloc(&iqueue
, sizeof(buf
)) &&
1421 buffer_check_alloc(&oqueue
, SFTP_MAX_MSG_LENGTH
))
1424 olen
= buffer_len(&oqueue
);
1428 if (select(max
+1, rset
, wset
, NULL
, NULL
) < 0) {
1431 error("select: %s", strerror(errno
));
1432 sftp_server_cleanup_exit(2);
1435 /* copy stdin to iqueue */
1436 if (FD_ISSET(in
, rset
)) {
1437 len
= read(in
, buf
, sizeof buf
);
1440 sftp_server_cleanup_exit(0);
1441 } else if (len
< 0) {
1442 error("read: %s", strerror(errno
));
1443 sftp_server_cleanup_exit(1);
1445 buffer_append(&iqueue
, buf
, len
);
1448 /* send oqueue to stdout */
1449 if (FD_ISSET(out
, wset
)) {
1450 len
= write(out
, buffer_ptr(&oqueue
), olen
);
1452 error("write: %s", strerror(errno
));
1453 sftp_server_cleanup_exit(1);
1455 buffer_consume(&oqueue
, len
);
1460 * Process requests from client if we can fit the results
1461 * into the output buffer, otherwise stop processing input
1462 * and let the output queue drain.
1464 if (buffer_check_alloc(&oqueue
, SFTP_MAX_MSG_LENGTH
))