]> andersk Git - openssh.git/blame_incremental - sftp-server.c
- djm@cvs.openbsd.org 2008/05/19 20:53:52
[openssh.git] / sftp-server.c
... / ...
CommitLineData
1/* $OpenBSD: sftp-server.c,v 1.80 2008/05/18 21:29:05 djm Exp $ */
2/*
3 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
4 *
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.
8 *
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.
16 */
17
18#include "includes.h"
19
20#include <sys/types.h>
21#include <sys/param.h>
22#include <sys/stat.h>
23#ifdef HAVE_SYS_TIME_H
24# include <sys/time.h>
25#endif
26#ifdef HAVE_SYS_MOUNT_H
27#include <sys/mount.h>
28#endif
29#ifdef HAVE_SYS_STATVFS_H
30#include <sys/statvfs.h>
31#endif
32
33#include <dirent.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <pwd.h>
37#include <stdlib.h>
38#include <stdio.h>
39#include <string.h>
40#include <pwd.h>
41#include <time.h>
42#include <unistd.h>
43#include <stdarg.h>
44
45#include "xmalloc.h"
46#include "buffer.h"
47#include "log.h"
48#include "misc.h"
49#include "uidswap.h"
50
51#include "sftp.h"
52#include "sftp-common.h"
53
54/* helper */
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);
58
59/* Our verbosity */
60LogLevel log_level = SYSLOG_LEVEL_ERROR;
61
62/* Our client */
63struct passwd *pw = NULL;
64char *client_addr = NULL;
65
66/* input and output queue */
67Buffer iqueue;
68Buffer oqueue;
69
70/* Version of client */
71int version;
72
73/* portable attributes, etc. */
74
75typedef struct Stat Stat;
76
77struct Stat {
78 char *name;
79 char *long_name;
80 Attrib attrib;
81};
82
83static int
84errno_to_portable(int unixerrno)
85{
86 int ret = 0;
87
88 switch (unixerrno) {
89 case 0:
90 ret = SSH2_FX_OK;
91 break;
92 case ENOENT:
93 case ENOTDIR:
94 case EBADF:
95 case ELOOP:
96 ret = SSH2_FX_NO_SUCH_FILE;
97 break;
98 case EPERM:
99 case EACCES:
100 case EFAULT:
101 ret = SSH2_FX_PERMISSION_DENIED;
102 break;
103 case ENAMETOOLONG:
104 case EINVAL:
105 ret = SSH2_FX_BAD_MESSAGE;
106 break;
107 default:
108 ret = SSH2_FX_FAILURE;
109 break;
110 }
111 return ret;
112}
113
114static int
115flags_from_portable(int pflags)
116{
117 int flags = 0;
118
119 if ((pflags & SSH2_FXF_READ) &&
120 (pflags & SSH2_FXF_WRITE)) {
121 flags = O_RDWR;
122 } else if (pflags & SSH2_FXF_READ) {
123 flags = O_RDONLY;
124 } else if (pflags & SSH2_FXF_WRITE) {
125 flags = O_WRONLY;
126 }
127 if (pflags & SSH2_FXF_CREAT)
128 flags |= O_CREAT;
129 if (pflags & SSH2_FXF_TRUNC)
130 flags |= O_TRUNC;
131 if (pflags & SSH2_FXF_EXCL)
132 flags |= O_EXCL;
133 return flags;
134}
135
136static const char *
137string_from_portable(int pflags)
138{
139 static char ret[128];
140
141 *ret = '\0';
142
143#define PAPPEND(str) { \
144 if (*ret != '\0') \
145 strlcat(ret, ",", sizeof(ret)); \
146 strlcat(ret, str, sizeof(ret)); \
147 }
148
149 if (pflags & SSH2_FXF_READ)
150 PAPPEND("READ")
151 if (pflags & SSH2_FXF_WRITE)
152 PAPPEND("WRITE")
153 if (pflags & SSH2_FXF_CREAT)
154 PAPPEND("CREATE")
155 if (pflags & SSH2_FXF_TRUNC)
156 PAPPEND("TRUNCATE")
157 if (pflags & SSH2_FXF_EXCL)
158 PAPPEND("EXCL")
159
160 return ret;
161}
162
163static Attrib *
164get_attrib(void)
165{
166 return decode_attrib(&iqueue);
167}
168
169/* handle handles */
170
171typedef struct Handle Handle;
172struct Handle {
173 int use;
174 DIR *dirp;
175 int fd;
176 char *name;
177 u_int64_t bytes_read, bytes_write;
178 int next_unused;
179};
180
181enum {
182 HANDLE_UNUSED,
183 HANDLE_DIR,
184 HANDLE_FILE
185};
186
187Handle *handles = NULL;
188u_int num_handles = 0;
189int first_unused_handle = -1;
190
191static void handle_unused(int i)
192{
193 handles[i].use = HANDLE_UNUSED;
194 handles[i].next_unused = first_unused_handle;
195 first_unused_handle = i;
196}
197
198static int
199handle_new(int use, const char *name, int fd, DIR *dirp)
200{
201 int i;
202
203 if (first_unused_handle == -1) {
204 if (num_handles + 1 <= num_handles)
205 return -1;
206 num_handles++;
207 handles = xrealloc(handles, num_handles, sizeof(Handle));
208 handle_unused(num_handles - 1);
209 }
210
211 i = first_unused_handle;
212 first_unused_handle = handles[i].next_unused;
213
214 handles[i].use = use;
215 handles[i].dirp = dirp;
216 handles[i].fd = fd;
217 handles[i].name = xstrdup(name);
218 handles[i].bytes_read = handles[i].bytes_write = 0;
219
220 return i;
221}
222
223static int
224handle_is_ok(int i, int type)
225{
226 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
227}
228
229static int
230handle_to_string(int handle, char **stringp, int *hlenp)
231{
232 if (stringp == NULL || hlenp == NULL)
233 return -1;
234 *stringp = xmalloc(sizeof(int32_t));
235 put_u32(*stringp, handle);
236 *hlenp = sizeof(int32_t);
237 return 0;
238}
239
240static int
241handle_from_string(const char *handle, u_int hlen)
242{
243 int val;
244
245 if (hlen != sizeof(int32_t))
246 return -1;
247 val = get_u32(handle);
248 if (handle_is_ok(val, HANDLE_FILE) ||
249 handle_is_ok(val, HANDLE_DIR))
250 return val;
251 return -1;
252}
253
254static char *
255handle_to_name(int handle)
256{
257 if (handle_is_ok(handle, HANDLE_DIR)||
258 handle_is_ok(handle, HANDLE_FILE))
259 return handles[handle].name;
260 return NULL;
261}
262
263static DIR *
264handle_to_dir(int handle)
265{
266 if (handle_is_ok(handle, HANDLE_DIR))
267 return handles[handle].dirp;
268 return NULL;
269}
270
271static int
272handle_to_fd(int handle)
273{
274 if (handle_is_ok(handle, HANDLE_FILE))
275 return handles[handle].fd;
276 return -1;
277}
278
279static void
280handle_update_read(int handle, ssize_t bytes)
281{
282 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
283 handles[handle].bytes_read += bytes;
284}
285
286static void
287handle_update_write(int handle, ssize_t bytes)
288{
289 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
290 handles[handle].bytes_write += bytes;
291}
292
293static u_int64_t
294handle_bytes_read(int handle)
295{
296 if (handle_is_ok(handle, HANDLE_FILE))
297 return (handles[handle].bytes_read);
298 return 0;
299}
300
301static u_int64_t
302handle_bytes_write(int handle)
303{
304 if (handle_is_ok(handle, HANDLE_FILE))
305 return (handles[handle].bytes_write);
306 return 0;
307}
308
309static int
310handle_close(int handle)
311{
312 int ret = -1;
313
314 if (handle_is_ok(handle, HANDLE_FILE)) {
315 ret = close(handles[handle].fd);
316 xfree(handles[handle].name);
317 handle_unused(handle);
318 } else if (handle_is_ok(handle, HANDLE_DIR)) {
319 ret = closedir(handles[handle].dirp);
320 xfree(handles[handle].name);
321 handle_unused(handle);
322 } else {
323 errno = ENOENT;
324 }
325 return ret;
326}
327
328static void
329handle_log_close(int handle, char *emsg)
330{
331 if (handle_is_ok(handle, HANDLE_FILE)) {
332 logit("%s%sclose \"%s\" bytes read %llu written %llu",
333 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
334 handle_to_name(handle),
335 (unsigned long long)handle_bytes_read(handle),
336 (unsigned long long)handle_bytes_write(handle));
337 } else {
338 logit("%s%sclosedir \"%s\"",
339 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
340 handle_to_name(handle));
341 }
342}
343
344static void
345handle_log_exit(void)
346{
347 u_int i;
348
349 for (i = 0; i < num_handles; i++)
350 if (handles[i].use != HANDLE_UNUSED)
351 handle_log_close(i, "forced");
352}
353
354static int
355get_handle(void)
356{
357 char *handle;
358 int val = -1;
359 u_int hlen;
360
361 handle = get_string(&hlen);
362 if (hlen < 256)
363 val = handle_from_string(handle, hlen);
364 xfree(handle);
365 return val;
366}
367
368/* send replies */
369
370static void
371send_msg(Buffer *m)
372{
373 int mlen = buffer_len(m);
374
375 buffer_put_int(&oqueue, mlen);
376 buffer_append(&oqueue, buffer_ptr(m), mlen);
377 buffer_consume(m, mlen);
378}
379
380static const char *
381status_to_message(u_int32_t status)
382{
383 const char *status_messages[] = {
384 "Success", /* SSH_FX_OK */
385 "End of file", /* SSH_FX_EOF */
386 "No such file", /* SSH_FX_NO_SUCH_FILE */
387 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
388 "Failure", /* SSH_FX_FAILURE */
389 "Bad message", /* SSH_FX_BAD_MESSAGE */
390 "No connection", /* SSH_FX_NO_CONNECTION */
391 "Connection lost", /* SSH_FX_CONNECTION_LOST */
392 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
393 "Unknown error" /* Others */
394 };
395 return (status_messages[MIN(status,SSH2_FX_MAX)]);
396}
397
398static void
399send_status(u_int32_t id, u_int32_t status)
400{
401 Buffer msg;
402
403 debug3("request %u: sent status %u", id, status);
404 if (log_level > SYSLOG_LEVEL_VERBOSE ||
405 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
406 logit("sent status %s", status_to_message(status));
407 buffer_init(&msg);
408 buffer_put_char(&msg, SSH2_FXP_STATUS);
409 buffer_put_int(&msg, id);
410 buffer_put_int(&msg, status);
411 if (version >= 3) {
412 buffer_put_cstring(&msg, status_to_message(status));
413 buffer_put_cstring(&msg, "");
414 }
415 send_msg(&msg);
416 buffer_free(&msg);
417}
418static void
419send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
420{
421 Buffer msg;
422
423 buffer_init(&msg);
424 buffer_put_char(&msg, type);
425 buffer_put_int(&msg, id);
426 buffer_put_string(&msg, data, dlen);
427 send_msg(&msg);
428 buffer_free(&msg);
429}
430
431static void
432send_data(u_int32_t id, const char *data, int dlen)
433{
434 debug("request %u: sent data len %d", id, dlen);
435 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
436}
437
438static void
439send_handle(u_int32_t id, int handle)
440{
441 char *string;
442 int hlen;
443
444 handle_to_string(handle, &string, &hlen);
445 debug("request %u: sent handle handle %d", id, handle);
446 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
447 xfree(string);
448}
449
450static void
451send_names(u_int32_t id, int count, const Stat *stats)
452{
453 Buffer msg;
454 int i;
455
456 buffer_init(&msg);
457 buffer_put_char(&msg, SSH2_FXP_NAME);
458 buffer_put_int(&msg, id);
459 buffer_put_int(&msg, count);
460 debug("request %u: sent names count %d", id, count);
461 for (i = 0; i < count; i++) {
462 buffer_put_cstring(&msg, stats[i].name);
463 buffer_put_cstring(&msg, stats[i].long_name);
464 encode_attrib(&msg, &stats[i].attrib);
465 }
466 send_msg(&msg);
467 buffer_free(&msg);
468}
469
470static void
471send_attrib(u_int32_t id, const Attrib *a)
472{
473 Buffer msg;
474
475 debug("request %u: sent attrib have 0x%x", id, a->flags);
476 buffer_init(&msg);
477 buffer_put_char(&msg, SSH2_FXP_ATTRS);
478 buffer_put_int(&msg, id);
479 encode_attrib(&msg, a);
480 send_msg(&msg);
481 buffer_free(&msg);
482}
483
484#ifdef USE_STATVFS
485static void
486send_statvfs(u_int32_t id, struct statvfs *st)
487{
488 Buffer msg;
489 u_int64_t flag;
490
491 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
492 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
493
494 buffer_init(&msg);
495 buffer_put_char(&msg, SSH2_FXP_EXTENDED_REPLY);
496 buffer_put_int(&msg, id);
497 buffer_put_int(&msg, st->f_bsize);
498 buffer_put_int(&msg, st->f_frsize);
499 buffer_put_int64(&msg, st->f_blocks);
500 buffer_put_int64(&msg, st->f_bfree);
501 buffer_put_int64(&msg, st->f_bavail);
502 buffer_put_int64(&msg, st->f_files);
503 buffer_put_int64(&msg, st->f_ffree);
504 buffer_put_int64(&msg, st->f_favail);
505 buffer_put_int(&msg, st->f_fsid);
506 buffer_put_int(&msg, flag);
507 buffer_put_int(&msg, st->f_namemax);
508 send_msg(&msg);
509 buffer_free(&msg);
510}
511#endif
512
513/* parse incoming */
514
515static void
516process_init(void)
517{
518 Buffer msg;
519
520 version = get_int();
521 verbose("received client version %d", version);
522 buffer_init(&msg);
523 buffer_put_char(&msg, SSH2_FXP_VERSION);
524 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
525 /* POSIX rename extension */
526 buffer_put_cstring(&msg, "posix-rename@openssh.com");
527 buffer_put_cstring(&msg, "1"); /* version */
528#ifdef USEE_STATVFS
529 /* statvfs extension */
530 buffer_put_cstring(&msg, "statvfs@openssh.com");
531 buffer_put_cstring(&msg, "1"); /* version */
532 /* fstatvfs extension */
533 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
534 buffer_put_cstring(&msg, "1"); /* version */
535#endif
536 send_msg(&msg);
537 buffer_free(&msg);
538}
539
540static void
541process_open(void)
542{
543 u_int32_t id, pflags;
544 Attrib *a;
545 char *name;
546 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
547
548 id = get_int();
549 name = get_string(NULL);
550 pflags = get_int(); /* portable flags */
551 debug3("request %u: open flags %d", id, pflags);
552 a = get_attrib();
553 flags = flags_from_portable(pflags);
554 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
555 logit("open \"%s\" flags %s mode 0%o",
556 name, string_from_portable(pflags), mode);
557 fd = open(name, flags, mode);
558 if (fd < 0) {
559 status = errno_to_portable(errno);
560 } else {
561 handle = handle_new(HANDLE_FILE, name, fd, NULL);
562 if (handle < 0) {
563 close(fd);
564 } else {
565 send_handle(id, handle);
566 status = SSH2_FX_OK;
567 }
568 }
569 if (status != SSH2_FX_OK)
570 send_status(id, status);
571 xfree(name);
572}
573
574static void
575process_close(void)
576{
577 u_int32_t id;
578 int handle, ret, status = SSH2_FX_FAILURE;
579
580 id = get_int();
581 handle = get_handle();
582 debug3("request %u: close handle %u", id, handle);
583 handle_log_close(handle, NULL);
584 ret = handle_close(handle);
585 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
586 send_status(id, status);
587}
588
589static void
590process_read(void)
591{
592 char buf[64*1024];
593 u_int32_t id, len;
594 int handle, fd, ret, status = SSH2_FX_FAILURE;
595 u_int64_t off;
596
597 id = get_int();
598 handle = get_handle();
599 off = get_int64();
600 len = get_int();
601
602 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
603 id, handle_to_name(handle), handle, (unsigned long long)off, len);
604 if (len > sizeof buf) {
605 len = sizeof buf;
606 debug2("read change len %d", len);
607 }
608 fd = handle_to_fd(handle);
609 if (fd >= 0) {
610 if (lseek(fd, off, SEEK_SET) < 0) {
611 error("process_read: seek failed");
612 status = errno_to_portable(errno);
613 } else {
614 ret = read(fd, buf, len);
615 if (ret < 0) {
616 status = errno_to_portable(errno);
617 } else if (ret == 0) {
618 status = SSH2_FX_EOF;
619 } else {
620 send_data(id, buf, ret);
621 status = SSH2_FX_OK;
622 handle_update_read(handle, ret);
623 }
624 }
625 }
626 if (status != SSH2_FX_OK)
627 send_status(id, status);
628}
629
630static void
631process_write(void)
632{
633 u_int32_t id;
634 u_int64_t off;
635 u_int len;
636 int handle, fd, ret, status = SSH2_FX_FAILURE;
637 char *data;
638
639 id = get_int();
640 handle = get_handle();
641 off = get_int64();
642 data = get_string(&len);
643
644 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
645 id, handle_to_name(handle), handle, (unsigned long long)off, len);
646 fd = handle_to_fd(handle);
647 if (fd >= 0) {
648 if (lseek(fd, off, SEEK_SET) < 0) {
649 status = errno_to_portable(errno);
650 error("process_write: seek failed");
651 } else {
652/* XXX ATOMICIO ? */
653 ret = write(fd, data, len);
654 if (ret < 0) {
655 error("process_write: write failed");
656 status = errno_to_portable(errno);
657 } else if ((size_t)ret == len) {
658 status = SSH2_FX_OK;
659 handle_update_write(handle, ret);
660 } else {
661 debug2("nothing at all written");
662 }
663 }
664 }
665 send_status(id, status);
666 xfree(data);
667}
668
669static void
670process_do_stat(int do_lstat)
671{
672 Attrib a;
673 struct stat st;
674 u_int32_t id;
675 char *name;
676 int ret, status = SSH2_FX_FAILURE;
677
678 id = get_int();
679 name = get_string(NULL);
680 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
681 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
682 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
683 if (ret < 0) {
684 status = errno_to_portable(errno);
685 } else {
686 stat_to_attrib(&st, &a);
687 send_attrib(id, &a);
688 status = SSH2_FX_OK;
689 }
690 if (status != SSH2_FX_OK)
691 send_status(id, status);
692 xfree(name);
693}
694
695static void
696process_stat(void)
697{
698 process_do_stat(0);
699}
700
701static void
702process_lstat(void)
703{
704 process_do_stat(1);
705}
706
707static void
708process_fstat(void)
709{
710 Attrib a;
711 struct stat st;
712 u_int32_t id;
713 int fd, ret, handle, status = SSH2_FX_FAILURE;
714
715 id = get_int();
716 handle = get_handle();
717 debug("request %u: fstat \"%s\" (handle %u)",
718 id, handle_to_name(handle), handle);
719 fd = handle_to_fd(handle);
720 if (fd >= 0) {
721 ret = fstat(fd, &st);
722 if (ret < 0) {
723 status = errno_to_portable(errno);
724 } else {
725 stat_to_attrib(&st, &a);
726 send_attrib(id, &a);
727 status = SSH2_FX_OK;
728 }
729 }
730 if (status != SSH2_FX_OK)
731 send_status(id, status);
732}
733
734static struct timeval *
735attrib_to_tv(const Attrib *a)
736{
737 static struct timeval tv[2];
738
739 tv[0].tv_sec = a->atime;
740 tv[0].tv_usec = 0;
741 tv[1].tv_sec = a->mtime;
742 tv[1].tv_usec = 0;
743 return tv;
744}
745
746static void
747process_setstat(void)
748{
749 Attrib *a;
750 u_int32_t id;
751 char *name;
752 int status = SSH2_FX_OK, ret;
753
754 id = get_int();
755 name = get_string(NULL);
756 a = get_attrib();
757 debug("request %u: setstat name \"%s\"", id, name);
758 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
759 logit("set \"%s\" size %llu",
760 name, (unsigned long long)a->size);
761 ret = truncate(name, a->size);
762 if (ret == -1)
763 status = errno_to_portable(errno);
764 }
765 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
766 logit("set \"%s\" mode %04o", name, a->perm);
767 ret = chmod(name, a->perm & 0777);
768 if (ret == -1)
769 status = errno_to_portable(errno);
770 }
771 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
772 char buf[64];
773 time_t t = a->mtime;
774
775 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
776 localtime(&t));
777 logit("set \"%s\" modtime %s", name, buf);
778 ret = utimes(name, attrib_to_tv(a));
779 if (ret == -1)
780 status = errno_to_portable(errno);
781 }
782 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
783 logit("set \"%s\" owner %lu group %lu", name,
784 (u_long)a->uid, (u_long)a->gid);
785 ret = chown(name, a->uid, a->gid);
786 if (ret == -1)
787 status = errno_to_portable(errno);
788 }
789 send_status(id, status);
790 xfree(name);
791}
792
793static void
794process_fsetstat(void)
795{
796 Attrib *a;
797 u_int32_t id;
798 int handle, fd, ret;
799 int status = SSH2_FX_OK;
800
801 id = get_int();
802 handle = get_handle();
803 a = get_attrib();
804 debug("request %u: fsetstat handle %d", id, handle);
805 fd = handle_to_fd(handle);
806 if (fd < 0) {
807 status = SSH2_FX_FAILURE;
808 } else {
809 char *name = handle_to_name(handle);
810
811 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
812 logit("set \"%s\" size %llu",
813 name, (unsigned long long)a->size);
814 ret = ftruncate(fd, a->size);
815 if (ret == -1)
816 status = errno_to_portable(errno);
817 }
818 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
819 logit("set \"%s\" mode %04o", name, a->perm);
820#ifdef HAVE_FCHMOD
821 ret = fchmod(fd, a->perm & 0777);
822#else
823 ret = chmod(name, a->perm & 0777);
824#endif
825 if (ret == -1)
826 status = errno_to_portable(errno);
827 }
828 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
829 char buf[64];
830 time_t t = a->mtime;
831
832 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
833 localtime(&t));
834 logit("set \"%s\" modtime %s", name, buf);
835#ifdef HAVE_FUTIMES
836 ret = futimes(fd, attrib_to_tv(a));
837#else
838 ret = utimes(name, attrib_to_tv(a));
839#endif
840 if (ret == -1)
841 status = errno_to_portable(errno);
842 }
843 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
844 logit("set \"%s\" owner %lu group %lu", name,
845 (u_long)a->uid, (u_long)a->gid);
846#ifdef HAVE_FCHOWN
847 ret = fchown(fd, a->uid, a->gid);
848#else
849 ret = chown(name, a->uid, a->gid);
850#endif
851 if (ret == -1)
852 status = errno_to_portable(errno);
853 }
854 }
855 send_status(id, status);
856}
857
858static void
859process_opendir(void)
860{
861 DIR *dirp = NULL;
862 char *path;
863 int handle, status = SSH2_FX_FAILURE;
864 u_int32_t id;
865
866 id = get_int();
867 path = get_string(NULL);
868 debug3("request %u: opendir", id);
869 logit("opendir \"%s\"", path);
870 dirp = opendir(path);
871 if (dirp == NULL) {
872 status = errno_to_portable(errno);
873 } else {
874 handle = handle_new(HANDLE_DIR, path, 0, dirp);
875 if (handle < 0) {
876 closedir(dirp);
877 } else {
878 send_handle(id, handle);
879 status = SSH2_FX_OK;
880 }
881
882 }
883 if (status != SSH2_FX_OK)
884 send_status(id, status);
885 xfree(path);
886}
887
888static void
889process_readdir(void)
890{
891 DIR *dirp;
892 struct dirent *dp;
893 char *path;
894 int handle;
895 u_int32_t id;
896
897 id = get_int();
898 handle = get_handle();
899 debug("request %u: readdir \"%s\" (handle %d)", id,
900 handle_to_name(handle), handle);
901 dirp = handle_to_dir(handle);
902 path = handle_to_name(handle);
903 if (dirp == NULL || path == NULL) {
904 send_status(id, SSH2_FX_FAILURE);
905 } else {
906 struct stat st;
907 char pathname[MAXPATHLEN];
908 Stat *stats;
909 int nstats = 10, count = 0, i;
910
911 stats = xcalloc(nstats, sizeof(Stat));
912 while ((dp = readdir(dirp)) != NULL) {
913 if (count >= nstats) {
914 nstats *= 2;
915 stats = xrealloc(stats, nstats, sizeof(Stat));
916 }
917/* XXX OVERFLOW ? */
918 snprintf(pathname, sizeof pathname, "%s%s%s", path,
919 strcmp(path, "/") ? "/" : "", dp->d_name);
920 if (lstat(pathname, &st) < 0)
921 continue;
922 stat_to_attrib(&st, &(stats[count].attrib));
923 stats[count].name = xstrdup(dp->d_name);
924 stats[count].long_name = ls_file(dp->d_name, &st, 0);
925 count++;
926 /* send up to 100 entries in one message */
927 /* XXX check packet size instead */
928 if (count == 100)
929 break;
930 }
931 if (count > 0) {
932 send_names(id, count, stats);
933 for (i = 0; i < count; i++) {
934 xfree(stats[i].name);
935 xfree(stats[i].long_name);
936 }
937 } else {
938 send_status(id, SSH2_FX_EOF);
939 }
940 xfree(stats);
941 }
942}
943
944static void
945process_remove(void)
946{
947 char *name;
948 u_int32_t id;
949 int status = SSH2_FX_FAILURE;
950 int ret;
951
952 id = get_int();
953 name = get_string(NULL);
954 debug3("request %u: remove", id);
955 logit("remove name \"%s\"", name);
956 ret = unlink(name);
957 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
958 send_status(id, status);
959 xfree(name);
960}
961
962static void
963process_mkdir(void)
964{
965 Attrib *a;
966 u_int32_t id;
967 char *name;
968 int ret, mode, status = SSH2_FX_FAILURE;
969
970 id = get_int();
971 name = get_string(NULL);
972 a = get_attrib();
973 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
974 a->perm & 0777 : 0777;
975 debug3("request %u: mkdir", id);
976 logit("mkdir name \"%s\" mode 0%o", name, mode);
977 ret = mkdir(name, mode);
978 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
979 send_status(id, status);
980 xfree(name);
981}
982
983static void
984process_rmdir(void)
985{
986 u_int32_t id;
987 char *name;
988 int ret, status;
989
990 id = get_int();
991 name = get_string(NULL);
992 debug3("request %u: rmdir", id);
993 logit("rmdir name \"%s\"", name);
994 ret = rmdir(name);
995 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
996 send_status(id, status);
997 xfree(name);
998}
999
1000static void
1001process_realpath(void)
1002{
1003 char resolvedname[MAXPATHLEN];
1004 u_int32_t id;
1005 char *path;
1006
1007 id = get_int();
1008 path = get_string(NULL);
1009 if (path[0] == '\0') {
1010 xfree(path);
1011 path = xstrdup(".");
1012 }
1013 debug3("request %u: realpath", id);
1014 verbose("realpath \"%s\"", path);
1015 if (realpath(path, resolvedname) == NULL) {
1016 send_status(id, errno_to_portable(errno));
1017 } else {
1018 Stat s;
1019 attrib_clear(&s.attrib);
1020 s.name = s.long_name = resolvedname;
1021 send_names(id, 1, &s);
1022 }
1023 xfree(path);
1024}
1025
1026static void
1027process_rename(void)
1028{
1029 u_int32_t id;
1030 char *oldpath, *newpath;
1031 int status;
1032 struct stat sb;
1033
1034 id = get_int();
1035 oldpath = get_string(NULL);
1036 newpath = get_string(NULL);
1037 debug3("request %u: rename", id);
1038 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
1039 status = SSH2_FX_FAILURE;
1040 if (lstat(oldpath, &sb) == -1)
1041 status = errno_to_portable(errno);
1042 else if (S_ISREG(sb.st_mode)) {
1043 /* Race-free rename of regular files */
1044 if (link(oldpath, newpath) == -1) {
1045 if (errno == EOPNOTSUPP
1046#ifdef LINK_OPNOTSUPP_ERRNO
1047 || errno == LINK_OPNOTSUPP_ERRNO
1048#endif
1049 ) {
1050 struct stat st;
1051
1052 /*
1053 * fs doesn't support links, so fall back to
1054 * stat+rename. This is racy.
1055 */
1056 if (stat(newpath, &st) == -1) {
1057 if (rename(oldpath, newpath) == -1)
1058 status =
1059 errno_to_portable(errno);
1060 else
1061 status = SSH2_FX_OK;
1062 }
1063 } else {
1064 status = errno_to_portable(errno);
1065 }
1066 } else if (unlink(oldpath) == -1) {
1067 status = errno_to_portable(errno);
1068 /* clean spare link */
1069 unlink(newpath);
1070 } else
1071 status = SSH2_FX_OK;
1072 } else if (stat(newpath, &sb) == -1) {
1073 if (rename(oldpath, newpath) == -1)
1074 status = errno_to_portable(errno);
1075 else
1076 status = SSH2_FX_OK;
1077 }
1078 send_status(id, status);
1079 xfree(oldpath);
1080 xfree(newpath);
1081}
1082
1083static void
1084process_readlink(void)
1085{
1086 u_int32_t id;
1087 int len;
1088 char buf[MAXPATHLEN];
1089 char *path;
1090
1091 id = get_int();
1092 path = get_string(NULL);
1093 debug3("request %u: readlink", id);
1094 verbose("readlink \"%s\"", path);
1095 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
1096 send_status(id, errno_to_portable(errno));
1097 else {
1098 Stat s;
1099
1100 buf[len] = '\0';
1101 attrib_clear(&s.attrib);
1102 s.name = s.long_name = buf;
1103 send_names(id, 1, &s);
1104 }
1105 xfree(path);
1106}
1107
1108static void
1109process_symlink(void)
1110{
1111 u_int32_t id;
1112 char *oldpath, *newpath;
1113 int ret, status;
1114
1115 id = get_int();
1116 oldpath = get_string(NULL);
1117 newpath = get_string(NULL);
1118 debug3("request %u: symlink", id);
1119 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
1120 /* this will fail if 'newpath' exists */
1121 ret = symlink(oldpath, newpath);
1122 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1123 send_status(id, status);
1124 xfree(oldpath);
1125 xfree(newpath);
1126}
1127
1128static void
1129process_extended_posix_rename(u_int32_t id)
1130{
1131 char *oldpath, *newpath;
1132
1133 oldpath = get_string(NULL);
1134 newpath = get_string(NULL);
1135 debug3("request %u: posix-rename", id);
1136 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
1137 if (rename(oldpath, newpath) == -1)
1138 send_status(id, errno_to_portable(errno));
1139 else
1140 send_status(id, SSH2_FX_OK);
1141 xfree(oldpath);
1142 xfree(newpath);
1143}
1144
1145#ifdef USE_STATVFS
1146static void
1147process_extended_statvfs(u_int32_t id)
1148{
1149 char *path;
1150 struct statvfs st;
1151
1152 path = get_string(NULL);
1153 debug3("request %u: statfs", id);
1154 logit("statfs \"%s\"", path);
1155
1156 if (statvfs(path, &st) != 0)
1157 send_status(id, errno_to_portable(errno));
1158 else
1159 send_statvfs(id, &st);
1160 xfree(path);
1161}
1162
1163static void
1164process_extended_fstatvfs(u_int32_t id)
1165{
1166 int handle, fd;
1167 struct statvfs st;
1168
1169 handle = get_handle();
1170 debug("request %u: fstatvfs \"%s\" (handle %u)",
1171 id, handle_to_name(handle), handle);
1172 if ((fd = handle_to_fd(handle)) < 0) {
1173 send_status(id, SSH2_FX_FAILURE);
1174 return;
1175 }
1176 if (fstatvfs(fd, &st) != 0)
1177 send_status(id, errno_to_portable(errno));
1178 else
1179 send_statvfs(id, &st);
1180}
1181#endif
1182
1183static void
1184process_extended(void)
1185{
1186 u_int32_t id;
1187 char *request;
1188
1189 id = get_int();
1190 request = get_string(NULL);
1191 if (strcmp(request, "posix-rename@openssh.com") == 0)
1192 process_extended_posix_rename(id);
1193#ifdef USE_STATVFS
1194 else if (strcmp(request, "statvfs@openssh.com") == 0)
1195 process_extended_statvfs(id);
1196 else if (strcmp(request, "fstatvfs@openssh.com") == 0)
1197 process_extended_fstatvfs(id);
1198#endif
1199 else
1200 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1201 xfree(request);
1202}
1203
1204/* stolen from ssh-agent */
1205
1206static void
1207process(void)
1208{
1209 u_int msg_len;
1210 u_int buf_len;
1211 u_int consumed;
1212 u_int type;
1213 u_char *cp;
1214
1215 buf_len = buffer_len(&iqueue);
1216 if (buf_len < 5)
1217 return; /* Incomplete message. */
1218 cp = buffer_ptr(&iqueue);
1219 msg_len = get_u32(cp);
1220 if (msg_len > SFTP_MAX_MSG_LENGTH) {
1221 error("bad message from %s local user %s",
1222 client_addr, pw->pw_name);
1223 sftp_server_cleanup_exit(11);
1224 }
1225 if (buf_len < msg_len + 4)
1226 return;
1227 buffer_consume(&iqueue, 4);
1228 buf_len -= 4;
1229 type = buffer_get_char(&iqueue);
1230 switch (type) {
1231 case SSH2_FXP_INIT:
1232 process_init();
1233 break;
1234 case SSH2_FXP_OPEN:
1235 process_open();
1236 break;
1237 case SSH2_FXP_CLOSE:
1238 process_close();
1239 break;
1240 case SSH2_FXP_READ:
1241 process_read();
1242 break;
1243 case SSH2_FXP_WRITE:
1244 process_write();
1245 break;
1246 case SSH2_FXP_LSTAT:
1247 process_lstat();
1248 break;
1249 case SSH2_FXP_FSTAT:
1250 process_fstat();
1251 break;
1252 case SSH2_FXP_SETSTAT:
1253 process_setstat();
1254 break;
1255 case SSH2_FXP_FSETSTAT:
1256 process_fsetstat();
1257 break;
1258 case SSH2_FXP_OPENDIR:
1259 process_opendir();
1260 break;
1261 case SSH2_FXP_READDIR:
1262 process_readdir();
1263 break;
1264 case SSH2_FXP_REMOVE:
1265 process_remove();
1266 break;
1267 case SSH2_FXP_MKDIR:
1268 process_mkdir();
1269 break;
1270 case SSH2_FXP_RMDIR:
1271 process_rmdir();
1272 break;
1273 case SSH2_FXP_REALPATH:
1274 process_realpath();
1275 break;
1276 case SSH2_FXP_STAT:
1277 process_stat();
1278 break;
1279 case SSH2_FXP_RENAME:
1280 process_rename();
1281 break;
1282 case SSH2_FXP_READLINK:
1283 process_readlink();
1284 break;
1285 case SSH2_FXP_SYMLINK:
1286 process_symlink();
1287 break;
1288 case SSH2_FXP_EXTENDED:
1289 process_extended();
1290 break;
1291 default:
1292 error("Unknown message %d", type);
1293 break;
1294 }
1295 /* discard the remaining bytes from the current packet */
1296 if (buf_len < buffer_len(&iqueue)) {
1297 error("iqueue grew unexpectedly");
1298 sftp_server_cleanup_exit(255);
1299 }
1300 consumed = buf_len - buffer_len(&iqueue);
1301 if (msg_len < consumed) {
1302 error("msg_len %d < consumed %d", msg_len, consumed);
1303 sftp_server_cleanup_exit(255);
1304 }
1305 if (msg_len > consumed)
1306 buffer_consume(&iqueue, msg_len - consumed);
1307}
1308
1309/* Cleanup handler that logs active handles upon normal exit */
1310void
1311sftp_server_cleanup_exit(int i)
1312{
1313 if (pw != NULL && client_addr != NULL) {
1314 handle_log_exit();
1315 logit("session closed for local user %s from [%s]",
1316 pw->pw_name, client_addr);
1317 }
1318 _exit(i);
1319}
1320
1321static void
1322sftp_server_usage(void)
1323{
1324 extern char *__progname;
1325
1326 fprintf(stderr,
1327 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1328 exit(1);
1329}
1330
1331int
1332sftp_server_main(int argc, char **argv, struct passwd *user_pw)
1333{
1334 fd_set *rset, *wset;
1335 int in, out, max, ch, skipargs = 0, log_stderr = 0;
1336 ssize_t len, olen, set_size;
1337 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1338 char *cp, buf[4*4096];
1339
1340 extern char *optarg;
1341 extern char *__progname;
1342
1343 __progname = ssh_get_progname(argv[0]);
1344 log_init(__progname, log_level, log_facility, log_stderr);
1345
1346 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1347 switch (ch) {
1348 case 'c':
1349 /*
1350 * Ignore all arguments if we are invoked as a
1351 * shell using "sftp-server -c command"
1352 */
1353 skipargs = 1;
1354 break;
1355 case 'e':
1356 log_stderr = 1;
1357 break;
1358 case 'l':
1359 log_level = log_level_number(optarg);
1360 if (log_level == SYSLOG_LEVEL_NOT_SET)
1361 error("Invalid log level \"%s\"", optarg);
1362 break;
1363 case 'f':
1364 log_facility = log_facility_number(optarg);
1365 if (log_facility == SYSLOG_FACILITY_NOT_SET)
1366 error("Invalid log facility \"%s\"", optarg);
1367 break;
1368 case 'h':
1369 default:
1370 sftp_server_usage();
1371 }
1372 }
1373
1374 log_init(__progname, log_level, log_facility, log_stderr);
1375
1376 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1377 client_addr = xstrdup(cp);
1378 if ((cp = strchr(client_addr, ' ')) == NULL) {
1379 error("Malformed SSH_CONNECTION variable: \"%s\"",
1380 getenv("SSH_CONNECTION"));
1381 sftp_server_cleanup_exit(255);
1382 }
1383 *cp = '\0';
1384 } else
1385 client_addr = xstrdup("UNKNOWN");
1386
1387 pw = pwcopy(user_pw);
1388
1389 logit("session opened for local user %s from [%s]",
1390 pw->pw_name, client_addr);
1391
1392 in = dup(STDIN_FILENO);
1393 out = dup(STDOUT_FILENO);
1394
1395#ifdef HAVE_CYGWIN
1396 setmode(in, O_BINARY);
1397 setmode(out, O_BINARY);
1398#endif
1399
1400 max = 0;
1401 if (in > max)
1402 max = in;
1403 if (out > max)
1404 max = out;
1405
1406 buffer_init(&iqueue);
1407 buffer_init(&oqueue);
1408
1409 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1410 rset = (fd_set *)xmalloc(set_size);
1411 wset = (fd_set *)xmalloc(set_size);
1412
1413 for (;;) {
1414 memset(rset, 0, set_size);
1415 memset(wset, 0, set_size);
1416
1417 /*
1418 * Ensure that we can read a full buffer and handle
1419 * the worst-case length packet it can generate,
1420 * otherwise apply backpressure by stopping reads.
1421 */
1422 if (buffer_check_alloc(&iqueue, sizeof(buf)) &&
1423 buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1424 FD_SET(in, rset);
1425
1426 olen = buffer_len(&oqueue);
1427 if (olen > 0)
1428 FD_SET(out, wset);
1429
1430 if (select(max+1, rset, wset, NULL, NULL) < 0) {
1431 if (errno == EINTR)
1432 continue;
1433 error("select: %s", strerror(errno));
1434 sftp_server_cleanup_exit(2);
1435 }
1436
1437 /* copy stdin to iqueue */
1438 if (FD_ISSET(in, rset)) {
1439 len = read(in, buf, sizeof buf);
1440 if (len == 0) {
1441 debug("read eof");
1442 sftp_server_cleanup_exit(0);
1443 } else if (len < 0) {
1444 error("read: %s", strerror(errno));
1445 sftp_server_cleanup_exit(1);
1446 } else {
1447 buffer_append(&iqueue, buf, len);
1448 }
1449 }
1450 /* send oqueue to stdout */
1451 if (FD_ISSET(out, wset)) {
1452 len = write(out, buffer_ptr(&oqueue), olen);
1453 if (len < 0) {
1454 error("write: %s", strerror(errno));
1455 sftp_server_cleanup_exit(1);
1456 } else {
1457 buffer_consume(&oqueue, len);
1458 }
1459 }
1460
1461 /*
1462 * Process requests from client if we can fit the results
1463 * into the output buffer, otherwise stop processing input
1464 * and let the output queue drain.
1465 */
1466 if (buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1467 process();
1468 }
1469}
This page took 0.055303 seconds and 5 git commands to generate.