Annotation of src/usr.bin/ssh/sftp-server.c, Revision 1.124
1.124 ! djm 1: /* $OpenBSD: sftp-server.c,v 1.123 2021/03/16 06:15:43 djm Exp $ */
1.1 markus 2: /*
1.45 markus 3: * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
1.1 markus 4: *
1.45 markus 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.
1.1 markus 8: *
1.45 markus 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.
1.1 markus 16: */
1.52 stevesk 17:
18: #include <sys/types.h>
1.122 djm 19: #include <sys/resource.h>
1.52 stevesk 20: #include <sys/stat.h>
1.66 stevesk 21: #include <sys/time.h>
1.79 djm 22: #include <sys/mount.h>
23: #include <sys/statvfs.h>
1.51 stevesk 24:
25: #include <dirent.h>
1.62 stevesk 26: #include <errno.h>
1.59 stevesk 27: #include <fcntl.h>
1.68 stevesk 28: #include <stdlib.h>
1.69 stevesk 29: #include <stdio.h>
1.65 stevesk 30: #include <string.h>
1.70 deraadt 31: #include <pwd.h>
1.64 stevesk 32: #include <time.h>
1.63 stevesk 33: #include <unistd.h>
1.70 deraadt 34: #include <stdarg.h>
1.1 markus 35:
1.70 deraadt 36: #include "xmalloc.h"
1.104 djm 37: #include "sshbuf.h"
38: #include "ssherr.h"
1.14 markus 39: #include "log.h"
1.49 djm 40: #include "misc.h"
1.98 djm 41: #include "match.h"
1.58 djm 42: #include "uidswap.h"
1.1 markus 43:
1.10 markus 44: #include "sftp.h"
1.15 djm 45: #include "sftp-common.h"
1.1 markus 46:
1.117 djm 47: char *sftp_realpath(const char *, char *); /* sftp-realpath.c */
48:
1.122 djm 49: /* Maximum data read that we are willing to accept */
1.124 ! djm 50: #define SFTP_MAX_READ_LENGTH (SFTP_MAX_MSG_LENGTH - 1024)
1.122 djm 51:
1.58 djm 52: /* Our verbosity */
1.98 djm 53: static LogLevel log_level = SYSLOG_LEVEL_ERROR;
1.58 djm 54:
55: /* Our client */
1.98 djm 56: static struct passwd *pw = NULL;
57: static char *client_addr = NULL;
1.1 markus 58:
59: /* input and output queue */
1.104 djm 60: struct sshbuf *iqueue;
61: struct sshbuf *oqueue;
1.1 markus 62:
1.23 djm 63: /* Version of client */
1.98 djm 64: static u_int version;
65:
66: /* SSH2_FXP_INIT received */
67: static int init_done;
1.23 djm 68:
1.90 djm 69: /* Disable writes */
1.98 djm 70: static int readonly;
71:
72: /* Requests that are allowed/denied */
1.118 djm 73: static char *request_allowlist, *request_denylist;
1.90 djm 74:
1.43 miod 75: /* portable attributes, etc. */
1.1 markus 76: typedef struct Stat Stat;
77:
1.15 djm 78: struct Stat {
1.1 markus 79: char *name;
80: char *long_name;
81: Attrib attrib;
82: };
83:
1.98 djm 84: /* Packet handlers */
85: static void process_open(u_int32_t id);
86: static void process_close(u_int32_t id);
87: static void process_read(u_int32_t id);
88: static void process_write(u_int32_t id);
89: static void process_stat(u_int32_t id);
90: static void process_lstat(u_int32_t id);
91: static void process_fstat(u_int32_t id);
92: static void process_setstat(u_int32_t id);
93: static void process_fsetstat(u_int32_t id);
94: static void process_opendir(u_int32_t id);
95: static void process_readdir(u_int32_t id);
96: static void process_remove(u_int32_t id);
97: static void process_mkdir(u_int32_t id);
98: static void process_rmdir(u_int32_t id);
99: static void process_realpath(u_int32_t id);
100: static void process_rename(u_int32_t id);
101: static void process_readlink(u_int32_t id);
102: static void process_symlink(u_int32_t id);
103: static void process_extended_posix_rename(u_int32_t id);
104: static void process_extended_statvfs(u_int32_t id);
105: static void process_extended_fstatvfs(u_int32_t id);
106: static void process_extended_hardlink(u_int32_t id);
1.102 djm 107: static void process_extended_fsync(u_int32_t id);
1.114 djm 108: static void process_extended_lsetstat(u_int32_t id);
1.122 djm 109: static void process_extended_limits(u_int32_t id);
1.98 djm 110: static void process_extended(u_int32_t id);
111:
112: struct sftp_handler {
113: const char *name; /* user-visible name for fine-grained perms */
114: const char *ext_name; /* extended request name */
115: u_int type; /* packet type, for non extended packets */
116: void (*handler)(u_int32_t);
117: int does_write; /* if nonzero, banned for readonly mode */
118: };
119:
1.113 djm 120: static const struct sftp_handler handlers[] = {
1.98 djm 121: /* NB. SSH2_FXP_OPEN does the readonly check in the handler itself */
122: { "open", NULL, SSH2_FXP_OPEN, process_open, 0 },
123: { "close", NULL, SSH2_FXP_CLOSE, process_close, 0 },
124: { "read", NULL, SSH2_FXP_READ, process_read, 0 },
125: { "write", NULL, SSH2_FXP_WRITE, process_write, 1 },
126: { "lstat", NULL, SSH2_FXP_LSTAT, process_lstat, 0 },
127: { "fstat", NULL, SSH2_FXP_FSTAT, process_fstat, 0 },
128: { "setstat", NULL, SSH2_FXP_SETSTAT, process_setstat, 1 },
129: { "fsetstat", NULL, SSH2_FXP_FSETSTAT, process_fsetstat, 1 },
130: { "opendir", NULL, SSH2_FXP_OPENDIR, process_opendir, 0 },
131: { "readdir", NULL, SSH2_FXP_READDIR, process_readdir, 0 },
132: { "remove", NULL, SSH2_FXP_REMOVE, process_remove, 1 },
133: { "mkdir", NULL, SSH2_FXP_MKDIR, process_mkdir, 1 },
134: { "rmdir", NULL, SSH2_FXP_RMDIR, process_rmdir, 1 },
135: { "realpath", NULL, SSH2_FXP_REALPATH, process_realpath, 0 },
136: { "stat", NULL, SSH2_FXP_STAT, process_stat, 0 },
137: { "rename", NULL, SSH2_FXP_RENAME, process_rename, 1 },
138: { "readlink", NULL, SSH2_FXP_READLINK, process_readlink, 0 },
139: { "symlink", NULL, SSH2_FXP_SYMLINK, process_symlink, 1 },
140: { NULL, NULL, 0, NULL, 0 }
141: };
142:
143: /* SSH2_FXP_EXTENDED submessages */
1.113 djm 144: static const struct sftp_handler extended_handlers[] = {
1.98 djm 145: { "posix-rename", "posix-rename@openssh.com", 0,
146: process_extended_posix_rename, 1 },
147: { "statvfs", "statvfs@openssh.com", 0, process_extended_statvfs, 0 },
148: { "fstatvfs", "fstatvfs@openssh.com", 0, process_extended_fstatvfs, 0 },
149: { "hardlink", "hardlink@openssh.com", 0, process_extended_hardlink, 1 },
1.102 djm 150: { "fsync", "fsync@openssh.com", 0, process_extended_fsync, 1 },
1.114 djm 151: { "lsetstat", "lsetstat@openssh.com", 0, process_extended_lsetstat, 1 },
1.122 djm 152: { "limits", "limits@openssh.com", 0, process_extended_limits, 1 },
1.98 djm 153: { NULL, NULL, 0, NULL, 0 }
154: };
155:
156: static int
1.113 djm 157: request_permitted(const struct sftp_handler *h)
1.98 djm 158: {
159: char *result;
160:
161: if (readonly && h->does_write) {
162: verbose("Refusing %s request in read-only mode", h->name);
163: return 0;
164: }
1.118 djm 165: if (request_denylist != NULL &&
166: ((result = match_list(h->name, request_denylist, NULL))) != NULL) {
1.98 djm 167: free(result);
1.118 djm 168: verbose("Refusing denylisted %s request", h->name);
1.98 djm 169: return 0;
170: }
1.118 djm 171: if (request_allowlist != NULL &&
172: ((result = match_list(h->name, request_allowlist, NULL))) != NULL) {
1.98 djm 173: free(result);
1.118 djm 174: debug2("Permitting allowlisted %s request", h->name);
1.98 djm 175: return 1;
176: }
1.118 djm 177: if (request_allowlist != NULL) {
178: verbose("Refusing non-allowlisted %s request", h->name);
1.98 djm 179: return 0;
180: }
181: return 1;
182: }
183:
1.28 itojun 184: static int
1.2 markus 185: errno_to_portable(int unixerrno)
1.1 markus 186: {
187: int ret = 0;
1.22 deraadt 188:
1.2 markus 189: switch (unixerrno) {
1.1 markus 190: case 0:
1.10 markus 191: ret = SSH2_FX_OK;
1.1 markus 192: break;
193: case ENOENT:
194: case ENOTDIR:
195: case EBADF:
196: case ELOOP:
1.10 markus 197: ret = SSH2_FX_NO_SUCH_FILE;
1.1 markus 198: break;
199: case EPERM:
200: case EACCES:
201: case EFAULT:
1.10 markus 202: ret = SSH2_FX_PERMISSION_DENIED;
1.1 markus 203: break;
204: case ENAMETOOLONG:
205: case EINVAL:
1.10 markus 206: ret = SSH2_FX_BAD_MESSAGE;
1.82 dtucker 207: break;
208: case ENOSYS:
209: ret = SSH2_FX_OP_UNSUPPORTED;
1.1 markus 210: break;
211: default:
1.10 markus 212: ret = SSH2_FX_FAILURE;
1.1 markus 213: break;
214: }
215: return ret;
216: }
217:
1.28 itojun 218: static int
1.1 markus 219: flags_from_portable(int pflags)
220: {
221: int flags = 0;
1.22 deraadt 222:
1.20 deraadt 223: if ((pflags & SSH2_FXF_READ) &&
224: (pflags & SSH2_FXF_WRITE)) {
1.1 markus 225: flags = O_RDWR;
1.10 markus 226: } else if (pflags & SSH2_FXF_READ) {
1.1 markus 227: flags = O_RDONLY;
1.10 markus 228: } else if (pflags & SSH2_FXF_WRITE) {
1.1 markus 229: flags = O_WRONLY;
230: }
1.101 djm 231: if (pflags & SSH2_FXF_APPEND)
232: flags |= O_APPEND;
1.10 markus 233: if (pflags & SSH2_FXF_CREAT)
1.1 markus 234: flags |= O_CREAT;
1.10 markus 235: if (pflags & SSH2_FXF_TRUNC)
1.1 markus 236: flags |= O_TRUNC;
1.10 markus 237: if (pflags & SSH2_FXF_EXCL)
1.1 markus 238: flags |= O_EXCL;
239: return flags;
240: }
241:
1.58 djm 242: static const char *
243: string_from_portable(int pflags)
244: {
245: static char ret[128];
246:
247: *ret = '\0';
248:
249: #define PAPPEND(str) { \
250: if (*ret != '\0') \
251: strlcat(ret, ",", sizeof(ret)); \
1.70 deraadt 252: strlcat(ret, str, sizeof(ret)); \
1.58 djm 253: }
254:
255: if (pflags & SSH2_FXF_READ)
256: PAPPEND("READ")
257: if (pflags & SSH2_FXF_WRITE)
258: PAPPEND("WRITE")
1.101 djm 259: if (pflags & SSH2_FXF_APPEND)
260: PAPPEND("APPEND")
1.58 djm 261: if (pflags & SSH2_FXF_CREAT)
262: PAPPEND("CREATE")
263: if (pflags & SSH2_FXF_TRUNC)
264: PAPPEND("TRUNCATE")
265: if (pflags & SSH2_FXF_EXCL)
266: PAPPEND("EXCL")
267:
268: return ret;
269: }
270:
1.1 markus 271: /* handle handles */
272:
273: typedef struct Handle Handle;
274: struct Handle {
275: int use;
276: DIR *dirp;
277: int fd;
1.101 djm 278: int flags;
1.1 markus 279: char *name;
1.58 djm 280: u_int64_t bytes_read, bytes_write;
1.75 djm 281: int next_unused;
1.1 markus 282: };
1.22 deraadt 283:
1.1 markus 284: enum {
285: HANDLE_UNUSED,
286: HANDLE_DIR,
287: HANDLE_FILE
288: };
1.22 deraadt 289:
1.113 djm 290: static Handle *handles = NULL;
291: static u_int num_handles = 0;
292: static int first_unused_handle = -1;
1.75 djm 293:
294: static void handle_unused(int i)
295: {
296: handles[i].use = HANDLE_UNUSED;
297: handles[i].next_unused = first_unused_handle;
298: first_unused_handle = i;
1.1 markus 299: }
300:
1.28 itojun 301: static int
1.101 djm 302: handle_new(int use, const char *name, int fd, int flags, DIR *dirp)
1.1 markus 303: {
1.75 djm 304: int i;
305:
306: if (first_unused_handle == -1) {
307: if (num_handles + 1 <= num_handles)
308: return -1;
309: num_handles++;
1.106 deraadt 310: handles = xreallocarray(handles, num_handles, sizeof(Handle));
1.75 djm 311: handle_unused(num_handles - 1);
312: }
313:
314: i = first_unused_handle;
315: first_unused_handle = handles[i].next_unused;
316:
317: handles[i].use = use;
318: handles[i].dirp = dirp;
319: handles[i].fd = fd;
1.101 djm 320: handles[i].flags = flags;
1.75 djm 321: handles[i].name = xstrdup(name);
322: handles[i].bytes_read = handles[i].bytes_write = 0;
1.22 deraadt 323:
1.75 djm 324: return i;
1.1 markus 325: }
326:
1.28 itojun 327: static int
1.1 markus 328: handle_is_ok(int i, int type)
329: {
1.75 djm 330: return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
1.1 markus 331: }
332:
1.28 itojun 333: static int
1.104 djm 334: handle_to_string(int handle, u_char **stringp, int *hlenp)
1.1 markus 335: {
336: if (stringp == NULL || hlenp == NULL)
337: return -1;
1.13 markus 338: *stringp = xmalloc(sizeof(int32_t));
1.57 djm 339: put_u32(*stringp, handle);
1.13 markus 340: *hlenp = sizeof(int32_t);
1.1 markus 341: return 0;
342: }
343:
1.28 itojun 344: static int
1.104 djm 345: handle_from_string(const u_char *handle, u_int hlen)
1.1 markus 346: {
1.13 markus 347: int val;
1.22 deraadt 348:
1.13 markus 349: if (hlen != sizeof(int32_t))
1.1 markus 350: return -1;
1.57 djm 351: val = get_u32(handle);
1.1 markus 352: if (handle_is_ok(val, HANDLE_FILE) ||
353: handle_is_ok(val, HANDLE_DIR))
354: return val;
355: return -1;
356: }
357:
1.28 itojun 358: static char *
1.1 markus 359: handle_to_name(int handle)
360: {
361: if (handle_is_ok(handle, HANDLE_DIR)||
362: handle_is_ok(handle, HANDLE_FILE))
363: return handles[handle].name;
364: return NULL;
365: }
366:
1.28 itojun 367: static DIR *
1.1 markus 368: handle_to_dir(int handle)
369: {
370: if (handle_is_ok(handle, HANDLE_DIR))
371: return handles[handle].dirp;
372: return NULL;
373: }
374:
1.28 itojun 375: static int
1.1 markus 376: handle_to_fd(int handle)
377: {
1.17 stevesk 378: if (handle_is_ok(handle, HANDLE_FILE))
1.1 markus 379: return handles[handle].fd;
380: return -1;
381: }
382:
1.101 djm 383: static int
384: handle_to_flags(int handle)
385: {
386: if (handle_is_ok(handle, HANDLE_FILE))
387: return handles[handle].flags;
388: return 0;
389: }
390:
1.58 djm 391: static void
392: handle_update_read(int handle, ssize_t bytes)
393: {
394: if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
395: handles[handle].bytes_read += bytes;
396: }
397:
398: static void
399: handle_update_write(int handle, ssize_t bytes)
400: {
401: if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
402: handles[handle].bytes_write += bytes;
403: }
404:
405: static u_int64_t
406: handle_bytes_read(int handle)
407: {
408: if (handle_is_ok(handle, HANDLE_FILE))
409: return (handles[handle].bytes_read);
410: return 0;
411: }
412:
413: static u_int64_t
414: handle_bytes_write(int handle)
415: {
416: if (handle_is_ok(handle, HANDLE_FILE))
417: return (handles[handle].bytes_write);
418: return 0;
419: }
420:
1.28 itojun 421: static int
1.1 markus 422: handle_close(int handle)
423: {
424: int ret = -1;
1.22 deraadt 425:
1.1 markus 426: if (handle_is_ok(handle, HANDLE_FILE)) {
427: ret = close(handles[handle].fd);
1.97 djm 428: free(handles[handle].name);
1.75 djm 429: handle_unused(handle);
1.1 markus 430: } else if (handle_is_ok(handle, HANDLE_DIR)) {
431: ret = closedir(handles[handle].dirp);
1.97 djm 432: free(handles[handle].name);
1.75 djm 433: handle_unused(handle);
1.1 markus 434: } else {
435: errno = ENOENT;
436: }
437: return ret;
438: }
439:
1.58 djm 440: static void
441: handle_log_close(int handle, char *emsg)
442: {
443: if (handle_is_ok(handle, HANDLE_FILE)) {
444: logit("%s%sclose \"%s\" bytes read %llu written %llu",
445: emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
446: handle_to_name(handle),
1.72 stevesk 447: (unsigned long long)handle_bytes_read(handle),
448: (unsigned long long)handle_bytes_write(handle));
1.58 djm 449: } else {
450: logit("%s%sclosedir \"%s\"",
451: emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
452: handle_to_name(handle));
453: }
454: }
455:
456: static void
457: handle_log_exit(void)
458: {
459: u_int i;
460:
1.75 djm 461: for (i = 0; i < num_handles; i++)
1.58 djm 462: if (handles[i].use != HANDLE_UNUSED)
463: handle_log_close(i, "forced");
464: }
465:
1.28 itojun 466: static int
1.104 djm 467: get_handle(struct sshbuf *queue, int *hp)
1.1 markus 468: {
1.104 djm 469: u_char *handle;
470: int r;
471: size_t hlen;
472:
473: *hp = -1;
474: if ((r = sshbuf_get_string(queue, &handle, &hlen)) != 0)
475: return r;
1.10 markus 476: if (hlen < 256)
1.104 djm 477: *hp = handle_from_string(handle, hlen);
1.97 djm 478: free(handle);
1.104 djm 479: return 0;
1.1 markus 480: }
481:
482: /* send replies */
483:
1.28 itojun 484: static void
1.104 djm 485: send_msg(struct sshbuf *m)
1.1 markus 486: {
1.104 djm 487: int r;
1.22 deraadt 488:
1.104 djm 489: if ((r = sshbuf_put_stringb(oqueue, m)) != 0)
1.120 djm 490: fatal_fr(r, "enqueue");
1.104 djm 491: sshbuf_reset(m);
1.1 markus 492: }
493:
1.58 djm 494: static const char *
495: status_to_message(u_int32_t status)
1.1 markus 496: {
1.23 djm 497: const char *status_messages[] = {
498: "Success", /* SSH_FX_OK */
499: "End of file", /* SSH_FX_EOF */
500: "No such file", /* SSH_FX_NO_SUCH_FILE */
501: "Permission denied", /* SSH_FX_PERMISSION_DENIED */
502: "Failure", /* SSH_FX_FAILURE */
503: "Bad message", /* SSH_FX_BAD_MESSAGE */
504: "No connection", /* SSH_FX_NO_CONNECTION */
505: "Connection lost", /* SSH_FX_CONNECTION_LOST */
506: "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
507: "Unknown error" /* Others */
508: };
1.110 deraadt 509: return (status_messages[MINIMUM(status,SSH2_FX_MAX)]);
1.58 djm 510: }
1.22 deraadt 511:
1.58 djm 512: static void
513: send_status(u_int32_t id, u_int32_t status)
514: {
1.104 djm 515: struct sshbuf *msg;
516: int r;
1.58 djm 517:
518: debug3("request %u: sent status %u", id, status);
519: if (log_level > SYSLOG_LEVEL_VERBOSE ||
520: (status != SSH2_FX_OK && status != SSH2_FX_EOF))
521: logit("sent status %s", status_to_message(status));
1.104 djm 522: if ((msg = sshbuf_new()) == NULL)
1.120 djm 523: fatal_f("sshbuf_new failed");
1.104 djm 524: if ((r = sshbuf_put_u8(msg, SSH2_FXP_STATUS)) != 0 ||
525: (r = sshbuf_put_u32(msg, id)) != 0 ||
526: (r = sshbuf_put_u32(msg, status)) != 0)
1.120 djm 527: fatal_fr(r, "compose");
1.23 djm 528: if (version >= 3) {
1.104 djm 529: if ((r = sshbuf_put_cstring(msg,
530: status_to_message(status))) != 0 ||
531: (r = sshbuf_put_cstring(msg, "")) != 0)
1.120 djm 532: fatal_fr(r, "compose message");
1.23 djm 533: }
1.104 djm 534: send_msg(msg);
535: sshbuf_free(msg);
1.1 markus 536: }
1.28 itojun 537: static void
1.104 djm 538: send_data_or_handle(char type, u_int32_t id, const u_char *data, int dlen)
1.1 markus 539: {
1.104 djm 540: struct sshbuf *msg;
541: int r;
1.22 deraadt 542:
1.104 djm 543: if ((msg = sshbuf_new()) == NULL)
1.120 djm 544: fatal_f("sshbuf_new failed");
1.104 djm 545: if ((r = sshbuf_put_u8(msg, type)) != 0 ||
546: (r = sshbuf_put_u32(msg, id)) != 0 ||
547: (r = sshbuf_put_string(msg, data, dlen)) != 0)
1.120 djm 548: fatal_fr(r, "compose");
1.104 djm 549: send_msg(msg);
550: sshbuf_free(msg);
1.1 markus 551: }
552:
1.28 itojun 553: static void
1.104 djm 554: send_data(u_int32_t id, const u_char *data, int dlen)
1.1 markus 555: {
1.58 djm 556: debug("request %u: sent data len %d", id, dlen);
1.10 markus 557: send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
1.1 markus 558: }
559:
1.28 itojun 560: static void
1.1 markus 561: send_handle(u_int32_t id, int handle)
562: {
1.104 djm 563: u_char *string;
1.1 markus 564: int hlen;
1.22 deraadt 565:
1.1 markus 566: handle_to_string(handle, &string, &hlen);
1.58 djm 567: debug("request %u: sent handle handle %d", id, handle);
1.10 markus 568: send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
1.97 djm 569: free(string);
1.1 markus 570: }
571:
1.28 itojun 572: static void
1.44 jakob 573: send_names(u_int32_t id, int count, const Stat *stats)
1.1 markus 574: {
1.104 djm 575: struct sshbuf *msg;
576: int i, r;
1.22 deraadt 577:
1.104 djm 578: if ((msg = sshbuf_new()) == NULL)
1.120 djm 579: fatal_f("sshbuf_new failed");
1.104 djm 580: if ((r = sshbuf_put_u8(msg, SSH2_FXP_NAME)) != 0 ||
581: (r = sshbuf_put_u32(msg, id)) != 0 ||
582: (r = sshbuf_put_u32(msg, count)) != 0)
1.120 djm 583: fatal_fr(r, "compose");
1.58 djm 584: debug("request %u: sent names count %d", id, count);
1.1 markus 585: for (i = 0; i < count; i++) {
1.104 djm 586: if ((r = sshbuf_put_cstring(msg, stats[i].name)) != 0 ||
587: (r = sshbuf_put_cstring(msg, stats[i].long_name)) != 0 ||
588: (r = encode_attrib(msg, &stats[i].attrib)) != 0)
1.120 djm 589: fatal_fr(r, "compose filenames/attrib");
1.1 markus 590: }
1.104 djm 591: send_msg(msg);
592: sshbuf_free(msg);
1.1 markus 593: }
594:
1.28 itojun 595: static void
1.44 jakob 596: send_attrib(u_int32_t id, const Attrib *a)
1.1 markus 597: {
1.104 djm 598: struct sshbuf *msg;
599: int r;
1.22 deraadt 600:
1.58 djm 601: debug("request %u: sent attrib have 0x%x", id, a->flags);
1.104 djm 602: if ((msg = sshbuf_new()) == NULL)
1.120 djm 603: fatal_f("sshbuf_new failed");
1.104 djm 604: if ((r = sshbuf_put_u8(msg, SSH2_FXP_ATTRS)) != 0 ||
605: (r = sshbuf_put_u32(msg, id)) != 0 ||
606: (r = encode_attrib(msg, a)) != 0)
1.120 djm 607: fatal_fr(r, "compose");
1.104 djm 608: send_msg(msg);
609: sshbuf_free(msg);
1.1 markus 610: }
611:
1.79 djm 612: static void
613: send_statvfs(u_int32_t id, struct statvfs *st)
614: {
1.104 djm 615: struct sshbuf *msg;
1.79 djm 616: u_int64_t flag;
1.104 djm 617: int r;
1.79 djm 618:
619: flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
620: flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
621:
1.104 djm 622: if ((msg = sshbuf_new()) == NULL)
1.120 djm 623: fatal_f("sshbuf_new failed");
1.104 djm 624: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED_REPLY)) != 0 ||
625: (r = sshbuf_put_u32(msg, id)) != 0 ||
626: (r = sshbuf_put_u64(msg, st->f_bsize)) != 0 ||
627: (r = sshbuf_put_u64(msg, st->f_frsize)) != 0 ||
628: (r = sshbuf_put_u64(msg, st->f_blocks)) != 0 ||
629: (r = sshbuf_put_u64(msg, st->f_bfree)) != 0 ||
630: (r = sshbuf_put_u64(msg, st->f_bavail)) != 0 ||
631: (r = sshbuf_put_u64(msg, st->f_files)) != 0 ||
632: (r = sshbuf_put_u64(msg, st->f_ffree)) != 0 ||
633: (r = sshbuf_put_u64(msg, st->f_favail)) != 0 ||
634: (r = sshbuf_put_u64(msg, st->f_fsid)) != 0 ||
635: (r = sshbuf_put_u64(msg, flag)) != 0 ||
636: (r = sshbuf_put_u64(msg, st->f_namemax)) != 0)
1.120 djm 637: fatal_fr(r, "compose");
1.104 djm 638: send_msg(msg);
639: sshbuf_free(msg);
1.79 djm 640: }
641:
1.1 markus 642: /* parse incoming */
643:
1.28 itojun 644: static void
1.1 markus 645: process_init(void)
646: {
1.104 djm 647: struct sshbuf *msg;
648: int r;
1.1 markus 649:
1.104 djm 650: if ((r = sshbuf_get_u32(iqueue, &version)) != 0)
1.120 djm 651: fatal_fr(r, "parse");
1.94 djm 652: verbose("received client version %u", version);
1.104 djm 653: if ((msg = sshbuf_new()) == NULL)
1.120 djm 654: fatal_f("sshbuf_new failed");
1.104 djm 655: if ((r = sshbuf_put_u8(msg, SSH2_FXP_VERSION)) != 0 ||
656: (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0 ||
657: /* POSIX rename extension */
658: (r = sshbuf_put_cstring(msg, "posix-rename@openssh.com")) != 0 ||
659: (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
660: /* statvfs extension */
661: (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
662: (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
663: /* fstatvfs extension */
664: (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
665: (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
666: /* hardlink extension */
667: (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
668: (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
669: /* fsync extension */
670: (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
1.114 djm 671: (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
1.121 djm 672: /* lsetstat extension */
1.114 djm 673: (r = sshbuf_put_cstring(msg, "lsetstat@openssh.com")) != 0 ||
1.122 djm 674: (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
675: /* limits extension */
676: (r = sshbuf_put_cstring(msg, "limits@openssh.com")) != 0 ||
1.104 djm 677: (r = sshbuf_put_cstring(msg, "1")) != 0) /* version */
1.120 djm 678: fatal_fr(r, "compose");
1.104 djm 679: send_msg(msg);
680: sshbuf_free(msg);
1.1 markus 681: }
682:
1.28 itojun 683: static void
1.98 djm 684: process_open(u_int32_t id)
1.1 markus 685: {
1.98 djm 686: u_int32_t pflags;
1.104 djm 687: Attrib a;
1.1 markus 688: char *name;
1.104 djm 689: int r, handle, fd, flags, mode, status = SSH2_FX_FAILURE;
690:
691: if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
692: (r = sshbuf_get_u32(iqueue, &pflags)) != 0 || /* portable flags */
693: (r = decode_attrib(iqueue, &a)) != 0)
1.120 djm 694: fatal_fr(r, "parse");
1.1 markus 695:
1.61 djm 696: debug3("request %u: open flags %d", id, pflags);
1.1 markus 697: flags = flags_from_portable(pflags);
1.104 djm 698: mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a.perm : 0666;
1.58 djm 699: logit("open \"%s\" flags %s mode 0%o",
700: name, string_from_portable(pflags), mode);
1.90 djm 701: if (readonly &&
1.111 djm 702: ((flags & O_ACCMODE) != O_RDONLY ||
703: (flags & (O_CREAT|O_TRUNC)) != 0)) {
1.98 djm 704: verbose("Refusing open request in read-only mode");
1.104 djm 705: status = SSH2_FX_PERMISSION_DENIED;
1.98 djm 706: } else {
1.90 djm 707: fd = open(name, flags, mode);
1.116 deraadt 708: if (fd == -1) {
1.90 djm 709: status = errno_to_portable(errno);
1.1 markus 710: } else {
1.101 djm 711: handle = handle_new(HANDLE_FILE, name, fd, flags, NULL);
1.90 djm 712: if (handle < 0) {
713: close(fd);
714: } else {
715: send_handle(id, handle);
716: status = SSH2_FX_OK;
717: }
1.1 markus 718: }
719: }
1.10 markus 720: if (status != SSH2_FX_OK)
1.1 markus 721: send_status(id, status);
1.97 djm 722: free(name);
1.1 markus 723: }
724:
1.28 itojun 725: static void
1.98 djm 726: process_close(u_int32_t id)
1.1 markus 727: {
1.104 djm 728: int r, handle, ret, status = SSH2_FX_FAILURE;
729:
730: if ((r = get_handle(iqueue, &handle)) != 0)
1.120 djm 731: fatal_fr(r, "parse");
1.1 markus 732:
1.58 djm 733: debug3("request %u: close handle %u", id, handle);
734: handle_log_close(handle, NULL);
1.1 markus 735: ret = handle_close(handle);
1.10 markus 736: status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.1 markus 737: send_status(id, status);
738: }
739:
1.28 itojun 740: static void
1.98 djm 741: process_read(u_int32_t id)
1.1 markus 742: {
1.124 ! djm 743: static u_char *buf;
! 744: static size_t buflen;
1.98 djm 745: u_int32_t len;
1.104 djm 746: int r, handle, fd, ret, status = SSH2_FX_FAILURE;
1.1 markus 747: u_int64_t off;
748:
1.104 djm 749: if ((r = get_handle(iqueue, &handle)) != 0 ||
750: (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
751: (r = sshbuf_get_u32(iqueue, &len)) != 0)
1.120 djm 752: fatal_fr(r, "parse");
1.1 markus 753:
1.124 ! djm 754: debug("request %u: read \"%s\" (handle %d) off %llu len %u",
1.58 djm 755: id, handle_to_name(handle), handle, (unsigned long long)off, len);
1.124 ! djm 756: if ((fd = handle_to_fd(handle)) == -1)
! 757: goto out;
! 758: if (len > SFTP_MAX_READ_LENGTH) {
! 759: debug2("read change len %u to %u", len, SFTP_MAX_READ_LENGTH);
! 760: len = SFTP_MAX_READ_LENGTH;
! 761: }
! 762: if (len > buflen) {
! 763: debug3_f("allocate %zu => %u", buflen, len);
! 764: if ((buf = realloc(NULL, len)) == NULL)
! 765: fatal_f("realloc failed");
! 766: buflen = len;
! 767: }
! 768: if (lseek(fd, off, SEEK_SET) == -1) {
! 769: status = errno_to_portable(errno);
! 770: error_f("seek \"%.100s\": %s", handle_to_name(handle),
! 771: strerror(errno));
! 772: goto out;
! 773: }
! 774: if (len == 0) {
! 775: /* weird, but not strictly disallowed */
! 776: ret = 0;
! 777: } else if ((ret = read(fd, buf, len)) == -1) {
! 778: status = errno_to_portable(errno);
! 779: error_f("read \"%.100s\": %s", handle_to_name(handle),
! 780: strerror(errno));
! 781: goto out;
! 782: } else if (ret == 0) {
! 783: status = SSH2_FX_EOF;
! 784: goto out;
! 785: }
! 786: send_data(id, buf, ret);
! 787: handle_update_read(handle, ret);
! 788: /* success */
! 789: status = SSH2_FX_OK;
! 790: out:
1.10 markus 791: if (status != SSH2_FX_OK)
1.1 markus 792: send_status(id, status);
793: }
794:
1.28 itojun 795: static void
1.98 djm 796: process_write(u_int32_t id)
1.1 markus 797: {
798: u_int64_t off;
1.104 djm 799: size_t len;
800: int r, handle, fd, ret, status;
801: u_char *data;
802:
803: if ((r = get_handle(iqueue, &handle)) != 0 ||
804: (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
805: (r = sshbuf_get_string(iqueue, &data, &len)) != 0)
1.120 djm 806: fatal_fr(r, "parse");
1.1 markus 807:
1.104 djm 808: debug("request %u: write \"%s\" (handle %d) off %llu len %zu",
1.58 djm 809: id, handle_to_name(handle), handle, (unsigned long long)off, len);
1.1 markus 810: fd = handle_to_fd(handle);
1.104 djm 811:
1.90 djm 812: if (fd < 0)
813: status = SSH2_FX_FAILURE;
814: else {
1.101 djm 815: if (!(handle_to_flags(handle) & O_APPEND) &&
1.123 djm 816: lseek(fd, off, SEEK_SET) == -1) {
1.1 markus 817: status = errno_to_portable(errno);
1.123 djm 818: error_f("seek \"%.100s\": %s", handle_to_name(handle),
819: strerror(errno));
1.1 markus 820: } else {
821: /* XXX ATOMICIO ? */
822: ret = write(fd, data, len);
1.116 deraadt 823: if (ret == -1) {
1.1 markus 824: status = errno_to_portable(errno);
1.123 djm 825: error_f("write \"%.100s\": %s",
826: handle_to_name(handle), strerror(errno));
1.48 djm 827: } else if ((size_t)ret == len) {
1.10 markus 828: status = SSH2_FX_OK;
1.58 djm 829: handle_update_write(handle, ret);
1.1 markus 830: } else {
1.120 djm 831: debug2_f("nothing at all written");
1.90 djm 832: status = SSH2_FX_FAILURE;
1.1 markus 833: }
834: }
835: }
836: send_status(id, status);
1.97 djm 837: free(data);
1.1 markus 838: }
839:
1.28 itojun 840: static void
1.98 djm 841: process_do_stat(u_int32_t id, int do_lstat)
1.1 markus 842: {
1.13 markus 843: Attrib a;
1.1 markus 844: struct stat st;
845: char *name;
1.104 djm 846: int r, status = SSH2_FX_FAILURE;
847:
848: if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1.120 djm 849: fatal_fr(r, "parse");
1.1 markus 850:
1.58 djm 851: debug3("request %u: %sstat", id, do_lstat ? "l" : "");
852: verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
1.104 djm 853: r = do_lstat ? lstat(name, &st) : stat(name, &st);
1.116 deraadt 854: if (r == -1) {
1.1 markus 855: status = errno_to_portable(errno);
856: } else {
1.13 markus 857: stat_to_attrib(&st, &a);
858: send_attrib(id, &a);
1.10 markus 859: status = SSH2_FX_OK;
1.1 markus 860: }
1.10 markus 861: if (status != SSH2_FX_OK)
1.1 markus 862: send_status(id, status);
1.97 djm 863: free(name);
1.1 markus 864: }
865:
1.28 itojun 866: static void
1.98 djm 867: process_stat(u_int32_t id)
1.1 markus 868: {
1.98 djm 869: process_do_stat(id, 0);
1.1 markus 870: }
871:
1.28 itojun 872: static void
1.98 djm 873: process_lstat(u_int32_t id)
1.1 markus 874: {
1.98 djm 875: process_do_stat(id, 1);
1.1 markus 876: }
877:
1.28 itojun 878: static void
1.98 djm 879: process_fstat(u_int32_t id)
1.1 markus 880: {
1.13 markus 881: Attrib a;
1.1 markus 882: struct stat st;
1.104 djm 883: int fd, r, handle, status = SSH2_FX_FAILURE;
1.1 markus 884:
1.104 djm 885: if ((r = get_handle(iqueue, &handle)) != 0)
1.120 djm 886: fatal_fr(r, "parse");
1.58 djm 887: debug("request %u: fstat \"%s\" (handle %u)",
888: id, handle_to_name(handle), handle);
1.1 markus 889: fd = handle_to_fd(handle);
1.71 stevesk 890: if (fd >= 0) {
1.104 djm 891: r = fstat(fd, &st);
1.116 deraadt 892: if (r == -1) {
1.1 markus 893: status = errno_to_portable(errno);
894: } else {
1.13 markus 895: stat_to_attrib(&st, &a);
896: send_attrib(id, &a);
1.10 markus 897: status = SSH2_FX_OK;
1.1 markus 898: }
899: }
1.10 markus 900: if (status != SSH2_FX_OK)
1.1 markus 901: send_status(id, status);
902: }
903:
1.28 itojun 904: static struct timeval *
1.44 jakob 905: attrib_to_tv(const Attrib *a)
1.1 markus 906: {
907: static struct timeval tv[2];
1.22 deraadt 908:
1.1 markus 909: tv[0].tv_sec = a->atime;
910: tv[0].tv_usec = 0;
911: tv[1].tv_sec = a->mtime;
912: tv[1].tv_usec = 0;
913: return tv;
914: }
915:
1.114 djm 916: static struct timespec *
917: attrib_to_ts(const Attrib *a)
918: {
919: static struct timespec ts[2];
920:
921: ts[0].tv_sec = a->atime;
922: ts[0].tv_nsec = 0;
923: ts[1].tv_sec = a->mtime;
924: ts[1].tv_nsec = 0;
925: return ts;
926: }
927:
1.28 itojun 928: static void
1.98 djm 929: process_setstat(u_int32_t id)
1.1 markus 930: {
1.104 djm 931: Attrib a;
1.1 markus 932: char *name;
1.104 djm 933: int r, status = SSH2_FX_OK;
934:
935: if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
936: (r = decode_attrib(iqueue, &a)) != 0)
1.120 djm 937: fatal_fr(r, "parse");
1.1 markus 938:
1.58 djm 939: debug("request %u: setstat name \"%s\"", id, name);
1.104 djm 940: if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
1.72 stevesk 941: logit("set \"%s\" size %llu",
1.104 djm 942: name, (unsigned long long)a.size);
943: r = truncate(name, a.size);
944: if (r == -1)
1.33 markus 945: status = errno_to_portable(errno);
946: }
1.104 djm 947: if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
948: logit("set \"%s\" mode %04o", name, a.perm);
949: r = chmod(name, a.perm & 07777);
950: if (r == -1)
1.1 markus 951: status = errno_to_portable(errno);
952: }
1.104 djm 953: if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1.58 djm 954: char buf[64];
1.104 djm 955: time_t t = a.mtime;
1.58 djm 956:
957: strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
958: localtime(&t));
959: logit("set \"%s\" modtime %s", name, buf);
1.104 djm 960: r = utimes(name, attrib_to_tv(&a));
961: if (r == -1)
1.1 markus 962: status = errno_to_portable(errno);
963: }
1.104 djm 964: if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
1.58 djm 965: logit("set \"%s\" owner %lu group %lu", name,
1.104 djm 966: (u_long)a.uid, (u_long)a.gid);
967: r = chown(name, a.uid, a.gid);
968: if (r == -1)
1.18 stevesk 969: status = errno_to_portable(errno);
970: }
1.1 markus 971: send_status(id, status);
1.97 djm 972: free(name);
1.1 markus 973: }
974:
1.28 itojun 975: static void
1.98 djm 976: process_fsetstat(u_int32_t id)
1.1 markus 977: {
1.104 djm 978: Attrib a;
979: int handle, fd, r;
1.10 markus 980: int status = SSH2_FX_OK;
1.1 markus 981:
1.104 djm 982: if ((r = get_handle(iqueue, &handle)) != 0 ||
983: (r = decode_attrib(iqueue, &a)) != 0)
1.120 djm 984: fatal_fr(r, "parse");
1.104 djm 985:
1.58 djm 986: debug("request %u: fsetstat handle %d", id, handle);
1.1 markus 987: fd = handle_to_fd(handle);
1.90 djm 988: if (fd < 0)
1.10 markus 989: status = SSH2_FX_FAILURE;
1.90 djm 990: else {
1.58 djm 991: char *name = handle_to_name(handle);
992:
1.104 djm 993: if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
1.72 stevesk 994: logit("set \"%s\" size %llu",
1.104 djm 995: name, (unsigned long long)a.size);
996: r = ftruncate(fd, a.size);
997: if (r == -1)
1.33 markus 998: status = errno_to_portable(errno);
999: }
1.104 djm 1000: if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1001: logit("set \"%s\" mode %04o", name, a.perm);
1002: r = fchmod(fd, a.perm & 07777);
1003: if (r == -1)
1.1 markus 1004: status = errno_to_portable(errno);
1005: }
1.104 djm 1006: if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1.58 djm 1007: char buf[64];
1.104 djm 1008: time_t t = a.mtime;
1.58 djm 1009:
1010: strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
1011: localtime(&t));
1012: logit("set \"%s\" modtime %s", name, buf);
1.104 djm 1013: r = futimes(fd, attrib_to_tv(&a));
1014: if (r == -1)
1.18 stevesk 1015: status = errno_to_portable(errno);
1016: }
1.104 djm 1017: if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
1.58 djm 1018: logit("set \"%s\" owner %lu group %lu", name,
1.104 djm 1019: (u_long)a.uid, (u_long)a.gid);
1020: r = fchown(fd, a.uid, a.gid);
1021: if (r == -1)
1.1 markus 1022: status = errno_to_portable(errno);
1023: }
1024: }
1025: send_status(id, status);
1026: }
1027:
1.28 itojun 1028: static void
1.98 djm 1029: process_opendir(u_int32_t id)
1.1 markus 1030: {
1031: DIR *dirp = NULL;
1032: char *path;
1.104 djm 1033: int r, handle, status = SSH2_FX_FAILURE;
1034:
1035: if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1.120 djm 1036: fatal_fr(r, "parse");
1.1 markus 1037:
1.58 djm 1038: debug3("request %u: opendir", id);
1039: logit("opendir \"%s\"", path);
1.17 stevesk 1040: dirp = opendir(path);
1.1 markus 1041: if (dirp == NULL) {
1042: status = errno_to_portable(errno);
1043: } else {
1.101 djm 1044: handle = handle_new(HANDLE_DIR, path, 0, 0, dirp);
1.1 markus 1045: if (handle < 0) {
1046: closedir(dirp);
1047: } else {
1048: send_handle(id, handle);
1.10 markus 1049: status = SSH2_FX_OK;
1.1 markus 1050: }
1.17 stevesk 1051:
1.1 markus 1052: }
1.10 markus 1053: if (status != SSH2_FX_OK)
1.1 markus 1054: send_status(id, status);
1.97 djm 1055: free(path);
1.1 markus 1056: }
1057:
1.28 itojun 1058: static void
1.98 djm 1059: process_readdir(u_int32_t id)
1.1 markus 1060: {
1061: DIR *dirp;
1062: struct dirent *dp;
1063: char *path;
1.104 djm 1064: int r, handle;
1065:
1066: if ((r = get_handle(iqueue, &handle)) != 0)
1.120 djm 1067: fatal_fr(r, "parse");
1.1 markus 1068:
1.58 djm 1069: debug("request %u: readdir \"%s\" (handle %d)", id,
1070: handle_to_name(handle), handle);
1.1 markus 1071: dirp = handle_to_dir(handle);
1072: path = handle_to_name(handle);
1073: if (dirp == NULL || path == NULL) {
1.10 markus 1074: send_status(id, SSH2_FX_FAILURE);
1.1 markus 1075: } else {
1076: struct stat st;
1.105 deraadt 1077: char pathname[PATH_MAX];
1.1 markus 1078: Stat *stats;
1079: int nstats = 10, count = 0, i;
1.36 deraadt 1080:
1.54 djm 1081: stats = xcalloc(nstats, sizeof(Stat));
1.1 markus 1082: while ((dp = readdir(dirp)) != NULL) {
1083: if (count >= nstats) {
1084: nstats *= 2;
1.106 deraadt 1085: stats = xreallocarray(stats, nstats, sizeof(Stat));
1.1 markus 1086: }
1087: /* XXX OVERFLOW ? */
1.30 jakob 1088: snprintf(pathname, sizeof pathname, "%s%s%s", path,
1089: strcmp(path, "/") ? "/" : "", dp->d_name);
1.116 deraadt 1090: if (lstat(pathname, &st) == -1)
1.1 markus 1091: continue;
1.13 markus 1092: stat_to_attrib(&st, &(stats[count].attrib));
1.1 markus 1093: stats[count].name = xstrdup(dp->d_name);
1.91 djm 1094: stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
1.1 markus 1095: count++;
1096: /* send up to 100 entries in one message */
1.11 markus 1097: /* XXX check packet size instead */
1.1 markus 1098: if (count == 100)
1099: break;
1100: }
1.10 markus 1101: if (count > 0) {
1102: send_names(id, count, stats);
1.31 deraadt 1103: for (i = 0; i < count; i++) {
1.97 djm 1104: free(stats[i].name);
1105: free(stats[i].long_name);
1.10 markus 1106: }
1107: } else {
1108: send_status(id, SSH2_FX_EOF);
1.1 markus 1109: }
1.97 djm 1110: free(stats);
1.1 markus 1111: }
1112: }
1113:
1.28 itojun 1114: static void
1.98 djm 1115: process_remove(u_int32_t id)
1.1 markus 1116: {
1117: char *name;
1.104 djm 1118: int r, status = SSH2_FX_FAILURE;
1119:
1120: if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1.120 djm 1121: fatal_fr(r, "parse");
1.1 markus 1122:
1.58 djm 1123: debug3("request %u: remove", id);
1124: logit("remove name \"%s\"", name);
1.104 djm 1125: r = unlink(name);
1126: status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.1 markus 1127: send_status(id, status);
1.97 djm 1128: free(name);
1.1 markus 1129: }
1130:
1.28 itojun 1131: static void
1.98 djm 1132: process_mkdir(u_int32_t id)
1.1 markus 1133: {
1.104 djm 1134: Attrib a;
1.1 markus 1135: char *name;
1.104 djm 1136: int r, mode, status = SSH2_FX_FAILURE;
1137:
1138: if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
1139: (r = decode_attrib(iqueue, &a)) != 0)
1.120 djm 1140: fatal_fr(r, "parse");
1.1 markus 1141:
1.104 djm 1142: mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
1143: a.perm & 07777 : 0777;
1.58 djm 1144: debug3("request %u: mkdir", id);
1145: logit("mkdir name \"%s\" mode 0%o", name, mode);
1.104 djm 1146: r = mkdir(name, mode);
1147: status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.1 markus 1148: send_status(id, status);
1.97 djm 1149: free(name);
1.1 markus 1150: }
1151:
1.28 itojun 1152: static void
1.98 djm 1153: process_rmdir(u_int32_t id)
1.1 markus 1154: {
1155: char *name;
1.104 djm 1156: int r, status;
1157:
1158: if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1.120 djm 1159: fatal_fr(r, "parse");
1.1 markus 1160:
1.58 djm 1161: debug3("request %u: rmdir", id);
1162: logit("rmdir name \"%s\"", name);
1.104 djm 1163: r = rmdir(name);
1164: status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.1 markus 1165: send_status(id, status);
1.97 djm 1166: free(name);
1.1 markus 1167: }
1168:
1.28 itojun 1169: static void
1.98 djm 1170: process_realpath(u_int32_t id)
1.1 markus 1171: {
1.105 deraadt 1172: char resolvedname[PATH_MAX];
1.1 markus 1173: char *path;
1.104 djm 1174: int r;
1175:
1176: if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1.120 djm 1177: fatal_fr(r, "parse");
1.1 markus 1178:
1.7 markus 1179: if (path[0] == '\0') {
1.97 djm 1180: free(path);
1.7 markus 1181: path = xstrdup(".");
1182: }
1.58 djm 1183: debug3("request %u: realpath", id);
1184: verbose("realpath \"%s\"", path);
1.117 djm 1185: if (sftp_realpath(path, resolvedname) == NULL) {
1.1 markus 1186: send_status(id, errno_to_portable(errno));
1187: } else {
1188: Stat s;
1189: attrib_clear(&s.attrib);
1190: s.name = s.long_name = resolvedname;
1191: send_names(id, 1, &s);
1192: }
1.97 djm 1193: free(path);
1.1 markus 1194: }
1195:
1.28 itojun 1196: static void
1.98 djm 1197: process_rename(u_int32_t id)
1.1 markus 1198: {
1199: char *oldpath, *newpath;
1.104 djm 1200: int r, status;
1.41 deraadt 1201: struct stat sb;
1.1 markus 1202:
1.104 djm 1203: if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1204: (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1.120 djm 1205: fatal_fr(r, "parse");
1.104 djm 1206:
1.58 djm 1207: debug3("request %u: rename", id);
1208: logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
1.41 deraadt 1209: status = SSH2_FX_FAILURE;
1.98 djm 1210: if (lstat(oldpath, &sb) == -1)
1.39 markus 1211: status = errno_to_portable(errno);
1.41 deraadt 1212: else if (S_ISREG(sb.st_mode)) {
1213: /* Race-free rename of regular files */
1.47 dtucker 1214: if (link(oldpath, newpath) == -1) {
1215: if (errno == EOPNOTSUPP) {
1216: struct stat st;
1217:
1218: /*
1219: * fs doesn't support links, so fall back to
1220: * stat+rename. This is racy.
1221: */
1222: if (stat(newpath, &st) == -1) {
1223: if (rename(oldpath, newpath) == -1)
1224: status =
1225: errno_to_portable(errno);
1226: else
1227: status = SSH2_FX_OK;
1228: }
1229: } else {
1230: status = errno_to_portable(errno);
1231: }
1232: } else if (unlink(oldpath) == -1) {
1.41 deraadt 1233: status = errno_to_portable(errno);
1234: /* clean spare link */
1235: unlink(newpath);
1236: } else
1237: status = SSH2_FX_OK;
1238: } else if (stat(newpath, &sb) == -1) {
1239: if (rename(oldpath, newpath) == -1)
1240: status = errno_to_portable(errno);
1241: else
1242: status = SSH2_FX_OK;
1243: }
1.1 markus 1244: send_status(id, status);
1.97 djm 1245: free(oldpath);
1246: free(newpath);
1.1 markus 1247: }
1248:
1.28 itojun 1249: static void
1.98 djm 1250: process_readlink(u_int32_t id)
1.23 djm 1251: {
1.104 djm 1252: int r, len;
1.105 deraadt 1253: char buf[PATH_MAX];
1.23 djm 1254: char *path;
1255:
1.104 djm 1256: if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1.120 djm 1257: fatal_fr(r, "parse");
1.104 djm 1258:
1.58 djm 1259: debug3("request %u: readlink", id);
1260: verbose("readlink \"%s\"", path);
1.46 avsm 1261: if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
1.23 djm 1262: send_status(id, errno_to_portable(errno));
1263: else {
1264: Stat s;
1.31 deraadt 1265:
1.46 avsm 1266: buf[len] = '\0';
1.23 djm 1267: attrib_clear(&s.attrib);
1.46 avsm 1268: s.name = s.long_name = buf;
1.23 djm 1269: send_names(id, 1, &s);
1270: }
1.97 djm 1271: free(path);
1.23 djm 1272: }
1273:
1.28 itojun 1274: static void
1.98 djm 1275: process_symlink(u_int32_t id)
1.23 djm 1276: {
1277: char *oldpath, *newpath;
1.104 djm 1278: int r, status;
1279:
1280: if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1281: (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1.120 djm 1282: fatal_fr(r, "parse");
1.23 djm 1283:
1.58 djm 1284: debug3("request %u: symlink", id);
1285: logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
1.39 markus 1286: /* this will fail if 'newpath' exists */
1.104 djm 1287: r = symlink(oldpath, newpath);
1288: status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.23 djm 1289: send_status(id, status);
1.97 djm 1290: free(oldpath);
1291: free(newpath);
1.23 djm 1292: }
1293:
1.28 itojun 1294: static void
1.78 djm 1295: process_extended_posix_rename(u_int32_t id)
1296: {
1297: char *oldpath, *newpath;
1.104 djm 1298: int r, status;
1299:
1300: if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1301: (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1.120 djm 1302: fatal_fr(r, "parse");
1.78 djm 1303:
1304: debug3("request %u: posix-rename", id);
1305: logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
1.104 djm 1306: r = rename(oldpath, newpath);
1307: status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.90 djm 1308: send_status(id, status);
1.97 djm 1309: free(oldpath);
1310: free(newpath);
1.78 djm 1311: }
1312:
1313: static void
1.79 djm 1314: process_extended_statvfs(u_int32_t id)
1315: {
1316: char *path;
1317: struct statvfs st;
1.104 djm 1318: int r;
1.79 djm 1319:
1.104 djm 1320: if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1.120 djm 1321: fatal_fr(r, "parse");
1.103 dtucker 1322: debug3("request %u: statvfs", id);
1323: logit("statvfs \"%s\"", path);
1.79 djm 1324:
1325: if (statvfs(path, &st) != 0)
1326: send_status(id, errno_to_portable(errno));
1327: else
1328: send_statvfs(id, &st);
1.97 djm 1329: free(path);
1.79 djm 1330: }
1331:
1332: static void
1333: process_extended_fstatvfs(u_int32_t id)
1334: {
1.104 djm 1335: int r, handle, fd;
1.79 djm 1336: struct statvfs st;
1337:
1.104 djm 1338: if ((r = get_handle(iqueue, &handle)) != 0)
1.120 djm 1339: fatal_fr(r, "parse");
1.79 djm 1340: debug("request %u: fstatvfs \"%s\" (handle %u)",
1341: id, handle_to_name(handle), handle);
1342: if ((fd = handle_to_fd(handle)) < 0) {
1343: send_status(id, SSH2_FX_FAILURE);
1344: return;
1345: }
1346: if (fstatvfs(fd, &st) != 0)
1347: send_status(id, errno_to_portable(errno));
1348: else
1349: send_statvfs(id, &st);
1350: }
1351:
1352: static void
1.93 djm 1353: process_extended_hardlink(u_int32_t id)
1354: {
1355: char *oldpath, *newpath;
1.104 djm 1356: int r, status;
1357:
1358: if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1359: (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1.120 djm 1360: fatal_fr(r, "parse");
1.93 djm 1361:
1362: debug3("request %u: hardlink", id);
1363: logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
1.104 djm 1364: r = link(oldpath, newpath);
1365: status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.93 djm 1366: send_status(id, status);
1.97 djm 1367: free(oldpath);
1368: free(newpath);
1.102 djm 1369: }
1370:
1371: static void
1372: process_extended_fsync(u_int32_t id)
1373: {
1.104 djm 1374: int handle, fd, r, status = SSH2_FX_OP_UNSUPPORTED;
1.102 djm 1375:
1.104 djm 1376: if ((r = get_handle(iqueue, &handle)) != 0)
1.120 djm 1377: fatal_fr(r, "parse");
1.102 djm 1378: debug3("request %u: fsync (handle %u)", id, handle);
1379: verbose("fsync \"%s\"", handle_to_name(handle));
1380: if ((fd = handle_to_fd(handle)) < 0)
1381: status = SSH2_FX_NO_SUCH_FILE;
1382: else if (handle_is_ok(handle, HANDLE_FILE)) {
1.104 djm 1383: r = fsync(fd);
1384: status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.102 djm 1385: }
1386: send_status(id, status);
1.114 djm 1387: }
1388:
1389: static void
1390: process_extended_lsetstat(u_int32_t id)
1391: {
1392: Attrib a;
1393: char *name;
1394: int r, status = SSH2_FX_OK;
1395:
1396: if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
1397: (r = decode_attrib(iqueue, &a)) != 0)
1.120 djm 1398: fatal_fr(r, "parse");
1.114 djm 1399:
1400: debug("request %u: lsetstat name \"%s\"", id, name);
1401: if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
1402: /* nonsensical for links */
1403: status = SSH2_FX_BAD_MESSAGE;
1404: goto out;
1405: }
1406: if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1407: logit("set \"%s\" mode %04o", name, a.perm);
1408: r = fchmodat(AT_FDCWD, name,
1409: a.perm & 07777, AT_SYMLINK_NOFOLLOW);
1410: if (r == -1)
1411: status = errno_to_portable(errno);
1412: }
1413: if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1414: char buf[64];
1415: time_t t = a.mtime;
1416:
1417: strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
1418: localtime(&t));
1419: logit("set \"%s\" modtime %s", name, buf);
1420: r = utimensat(AT_FDCWD, name,
1421: attrib_to_ts(&a), AT_SYMLINK_NOFOLLOW);
1422: if (r == -1)
1423: status = errno_to_portable(errno);
1424: }
1425: if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
1426: logit("set \"%s\" owner %lu group %lu", name,
1427: (u_long)a.uid, (u_long)a.gid);
1428: r = fchownat(AT_FDCWD, name, a.uid, a.gid,
1429: AT_SYMLINK_NOFOLLOW);
1430: if (r == -1)
1431: status = errno_to_portable(errno);
1432: }
1433: out:
1434: send_status(id, status);
1435: free(name);
1.122 djm 1436: }
1437:
1438: static void
1439: process_extended_limits(u_int32_t id)
1440: {
1441: struct sshbuf *msg;
1442: int r;
1443: uint64_t nfiles = 0;
1444: struct rlimit rlim;
1445:
1446: debug("request %u: limits", id);
1447:
1448: if (getrlimit(RLIMIT_NOFILE, &rlim) != -1 && rlim.rlim_cur > 5)
1449: nfiles = rlim.rlim_cur - 5; /* stdio(3) + syslog + spare */
1450:
1451: if ((msg = sshbuf_new()) == NULL)
1452: fatal_f("sshbuf_new failed");
1453: if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED_REPLY)) != 0 ||
1454: (r = sshbuf_put_u32(msg, id)) != 0 ||
1455: /* max-packet-length */
1456: (r = sshbuf_put_u64(msg, SFTP_MAX_MSG_LENGTH)) != 0 ||
1457: /* max-read-length */
1458: (r = sshbuf_put_u64(msg, SFTP_MAX_READ_LENGTH)) != 0 ||
1459: /* max-write-length */
1460: (r = sshbuf_put_u64(msg, SFTP_MAX_MSG_LENGTH - 1024)) != 0 ||
1461: /* max-open-handles */
1462: (r = sshbuf_put_u64(msg, nfiles)) != 0)
1463: fatal_fr(r, "compose");
1464: send_msg(msg);
1465: sshbuf_free(msg);
1.93 djm 1466: }
1467:
1468: static void
1.98 djm 1469: process_extended(u_int32_t id)
1.10 markus 1470: {
1471: char *request;
1.104 djm 1472: int i, r;
1.10 markus 1473:
1.104 djm 1474: if ((r = sshbuf_get_cstring(iqueue, &request, NULL)) != 0)
1.120 djm 1475: fatal_fr(r, "parse");
1.98 djm 1476: for (i = 0; extended_handlers[i].handler != NULL; i++) {
1477: if (strcmp(request, extended_handlers[i].ext_name) == 0) {
1478: if (!request_permitted(&extended_handlers[i]))
1479: send_status(id, SSH2_FX_PERMISSION_DENIED);
1480: else
1481: extended_handlers[i].handler(id);
1482: break;
1483: }
1484: }
1485: if (extended_handlers[i].handler == NULL) {
1486: error("Unknown extended request \"%.100s\"", request);
1.78 djm 1487: send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1.98 djm 1488: }
1.97 djm 1489: free(request);
1.10 markus 1490: }
1.1 markus 1491:
1492: /* stolen from ssh-agent */
1493:
1.28 itojun 1494: static void
1.1 markus 1495: process(void)
1496: {
1.104 djm 1497: u_int msg_len;
1498: u_int buf_len;
1499: u_int consumed;
1500: u_char type;
1501: const u_char *cp;
1502: int i, r;
1.98 djm 1503: u_int32_t id;
1.1 markus 1504:
1.104 djm 1505: buf_len = sshbuf_len(iqueue);
1.34 markus 1506: if (buf_len < 5)
1.1 markus 1507: return; /* Incomplete message. */
1.104 djm 1508: cp = sshbuf_ptr(iqueue);
1.57 djm 1509: msg_len = get_u32(cp);
1.50 djm 1510: if (msg_len > SFTP_MAX_MSG_LENGTH) {
1.58 djm 1511: error("bad message from %s local user %s",
1512: client_addr, pw->pw_name);
1.76 markus 1513: sftp_server_cleanup_exit(11);
1.1 markus 1514: }
1.34 markus 1515: if (buf_len < msg_len + 4)
1.1 markus 1516: return;
1.104 djm 1517: if ((r = sshbuf_consume(iqueue, 4)) != 0)
1.120 djm 1518: fatal_fr(r, "consume");
1.34 markus 1519: buf_len -= 4;
1.104 djm 1520: if ((r = sshbuf_get_u8(iqueue, &type)) != 0)
1.120 djm 1521: fatal_fr(r, "parse type");
1.98 djm 1522:
1.1 markus 1523: switch (type) {
1.10 markus 1524: case SSH2_FXP_INIT:
1.1 markus 1525: process_init();
1.98 djm 1526: init_done = 1;
1.1 markus 1527: break;
1.10 markus 1528: case SSH2_FXP_EXTENDED:
1.98 djm 1529: if (!init_done)
1530: fatal("Received extended request before init");
1.104 djm 1531: if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1.120 djm 1532: fatal_fr(r, "parse extended ID");
1.98 djm 1533: process_extended(id);
1.10 markus 1534: break;
1.1 markus 1535: default:
1.98 djm 1536: if (!init_done)
1537: fatal("Received %u request before init", type);
1.104 djm 1538: if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1.120 djm 1539: fatal_fr(r, "parse ID");
1.98 djm 1540: for (i = 0; handlers[i].handler != NULL; i++) {
1541: if (type == handlers[i].type) {
1542: if (!request_permitted(&handlers[i])) {
1543: send_status(id,
1544: SSH2_FX_PERMISSION_DENIED);
1545: } else {
1546: handlers[i].handler(id);
1547: }
1548: break;
1549: }
1550: }
1551: if (handlers[i].handler == NULL)
1552: error("Unknown message %u", type);
1.1 markus 1553: }
1.34 markus 1554: /* discard the remaining bytes from the current packet */
1.104 djm 1555: if (buf_len < sshbuf_len(iqueue)) {
1.76 markus 1556: error("iqueue grew unexpectedly");
1557: sftp_server_cleanup_exit(255);
1558: }
1.104 djm 1559: consumed = buf_len - sshbuf_len(iqueue);
1.76 markus 1560: if (msg_len < consumed) {
1.98 djm 1561: error("msg_len %u < consumed %u", msg_len, consumed);
1.76 markus 1562: sftp_server_cleanup_exit(255);
1563: }
1.104 djm 1564: if (msg_len > consumed &&
1565: (r = sshbuf_consume(iqueue, msg_len - consumed)) != 0)
1.120 djm 1566: fatal_fr(r, "consume");
1.1 markus 1567: }
1568:
1.58 djm 1569: /* Cleanup handler that logs active handles upon normal exit */
1570: void
1.76 markus 1571: sftp_server_cleanup_exit(int i)
1.58 djm 1572: {
1573: if (pw != NULL && client_addr != NULL) {
1574: handle_log_exit();
1575: logit("session closed for local user %s from [%s]",
1576: pw->pw_name, client_addr);
1577: }
1578: _exit(i);
1579: }
1580:
1581: static void
1.76 markus 1582: sftp_server_usage(void)
1.58 djm 1583: {
1584: extern char *__progname;
1585:
1586: fprintf(stderr,
1.96 jmc 1587: "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
1.118 djm 1588: "[-l log_level]\n\t[-P denied_requests] "
1589: "[-p allowed_requests] [-u umask]\n"
1.100 jmc 1590: " %s -Q protocol_feature\n",
1591: __progname, __progname);
1.58 djm 1592: exit(1);
1593: }
1594:
1.1 markus 1595: int
1.77 djm 1596: sftp_server_main(int argc, char **argv, struct passwd *user_pw)
1.1 markus 1597: {
1.21 millert 1598: fd_set *rset, *wset;
1.104 djm 1599: int i, r, in, out, max, ch, skipargs = 0, log_stderr = 0;
1.21 millert 1600: ssize_t len, olen, set_size;
1.58 djm 1601: SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1.112 djm 1602: char *cp, *homedir = NULL, uidstr[32], buf[4*4096];
1.92 djm 1603: long mask;
1.58 djm 1604:
1605: extern char *optarg;
1606: extern char *__progname;
1.49 djm 1607:
1.58 djm 1608: log_init(__progname, log_level, log_facility, log_stderr);
1609:
1.95 djm 1610: pw = pwcopy(user_pw);
1611:
1.98 djm 1612: while (!skipargs && (ch = getopt(argc, argv,
1613: "d:f:l:P:p:Q:u:cehR")) != -1) {
1.58 djm 1614: switch (ch) {
1.98 djm 1615: case 'Q':
1616: if (strcasecmp(optarg, "requests") != 0) {
1617: fprintf(stderr, "Invalid query type\n");
1618: exit(1);
1619: }
1620: for (i = 0; handlers[i].handler != NULL; i++)
1621: printf("%s\n", handlers[i].name);
1622: for (i = 0; extended_handlers[i].handler != NULL; i++)
1623: printf("%s\n", extended_handlers[i].name);
1624: exit(0);
1625: break;
1.90 djm 1626: case 'R':
1627: readonly = 1;
1628: break;
1.58 djm 1629: case 'c':
1630: /*
1631: * Ignore all arguments if we are invoked as a
1.70 deraadt 1632: * shell using "sftp-server -c command"
1.58 djm 1633: */
1634: skipargs = 1;
1635: break;
1636: case 'e':
1637: log_stderr = 1;
1638: break;
1639: case 'l':
1640: log_level = log_level_number(optarg);
1641: if (log_level == SYSLOG_LEVEL_NOT_SET)
1642: error("Invalid log level \"%s\"", optarg);
1643: break;
1644: case 'f':
1645: log_facility = log_facility_number(optarg);
1.74 djm 1646: if (log_facility == SYSLOG_FACILITY_NOT_SET)
1.58 djm 1647: error("Invalid log facility \"%s\"", optarg);
1.86 djm 1648: break;
1.95 djm 1649: case 'd':
1650: cp = tilde_expand_filename(optarg, user_pw->pw_uid);
1.112 djm 1651: snprintf(uidstr, sizeof(uidstr), "%llu",
1652: (unsigned long long)pw->pw_uid);
1.95 djm 1653: homedir = percent_expand(cp, "d", user_pw->pw_dir,
1.112 djm 1654: "u", user_pw->pw_name, "U", uidstr, (char *)NULL);
1.95 djm 1655: free(cp);
1.98 djm 1656: break;
1657: case 'p':
1.118 djm 1658: if (request_allowlist != NULL)
1.98 djm 1659: fatal("Permitted requests already set");
1.118 djm 1660: request_allowlist = xstrdup(optarg);
1.98 djm 1661: break;
1662: case 'P':
1.118 djm 1663: if (request_denylist != NULL)
1.98 djm 1664: fatal("Refused requests already set");
1.118 djm 1665: request_denylist = xstrdup(optarg);
1.95 djm 1666: break;
1.86 djm 1667: case 'u':
1.92 djm 1668: errno = 0;
1669: mask = strtol(optarg, &cp, 8);
1670: if (mask < 0 || mask > 0777 || *cp != '\0' ||
1671: cp == optarg || (mask == 0 && errno != 0))
1672: fatal("Invalid umask \"%s\"", optarg);
1673: (void)umask((mode_t)mask);
1.58 djm 1674: break;
1675: case 'h':
1676: default:
1.76 markus 1677: sftp_server_usage();
1.58 djm 1678: }
1679: }
1680:
1681: log_init(__progname, log_level, log_facility, log_stderr);
1682:
1683: if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1684: client_addr = xstrdup(cp);
1.76 markus 1685: if ((cp = strchr(client_addr, ' ')) == NULL) {
1686: error("Malformed SSH_CONNECTION variable: \"%s\"",
1.58 djm 1687: getenv("SSH_CONNECTION"));
1.76 markus 1688: sftp_server_cleanup_exit(255);
1689: }
1.58 djm 1690: *cp = '\0';
1691: } else
1692: client_addr = xstrdup("UNKNOWN");
1693:
1694: logit("session opened for local user %s from [%s]",
1695: pw->pw_name, client_addr);
1.10 markus 1696:
1.89 djm 1697: in = STDIN_FILENO;
1698: out = STDOUT_FILENO;
1.1 markus 1699:
1700: max = 0;
1701: if (in > max)
1702: max = in;
1703: if (out > max)
1704: max = out;
1705:
1.104 djm 1706: if ((iqueue = sshbuf_new()) == NULL)
1.120 djm 1707: fatal_f("sshbuf_new failed");
1.104 djm 1708: if ((oqueue = sshbuf_new()) == NULL)
1.120 djm 1709: fatal_f("sshbuf_new failed");
1.1 markus 1710:
1.108 logan 1711: rset = xcalloc(howmany(max + 1, NFDBITS), sizeof(fd_mask));
1712: wset = xcalloc(howmany(max + 1, NFDBITS), sizeof(fd_mask));
1.95 djm 1713:
1714: if (homedir != NULL) {
1715: if (chdir(homedir) != 0) {
1716: error("chdir to \"%s\" failed: %s", homedir,
1717: strerror(errno));
1718: }
1719: }
1.21 millert 1720:
1.108 logan 1721: set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1.1 markus 1722: for (;;) {
1.21 millert 1723: memset(rset, 0, set_size);
1724: memset(wset, 0, set_size);
1.1 markus 1725:
1.73 djm 1726: /*
1727: * Ensure that we can read a full buffer and handle
1728: * the worst-case length packet it can generate,
1729: * otherwise apply backpressure by stopping reads.
1730: */
1.104 djm 1731: if ((r = sshbuf_check_reserve(iqueue, sizeof(buf))) == 0 &&
1732: (r = sshbuf_check_reserve(oqueue,
1733: SFTP_MAX_MSG_LENGTH)) == 0)
1.73 djm 1734: FD_SET(in, rset);
1.104 djm 1735: else if (r != SSH_ERR_NO_BUFFER_SPACE)
1.120 djm 1736: fatal_fr(r, "reserve");
1.73 djm 1737:
1.104 djm 1738: olen = sshbuf_len(oqueue);
1.1 markus 1739: if (olen > 0)
1.21 millert 1740: FD_SET(out, wset);
1.1 markus 1741:
1.116 deraadt 1742: if (select(max+1, rset, wset, NULL, NULL) == -1) {
1.1 markus 1743: if (errno == EINTR)
1744: continue;
1.58 djm 1745: error("select: %s", strerror(errno));
1.76 markus 1746: sftp_server_cleanup_exit(2);
1.1 markus 1747: }
1748:
1749: /* copy stdin to iqueue */
1.21 millert 1750: if (FD_ISSET(in, rset)) {
1.1 markus 1751: len = read(in, buf, sizeof buf);
1752: if (len == 0) {
1753: debug("read eof");
1.76 markus 1754: sftp_server_cleanup_exit(0);
1.116 deraadt 1755: } else if (len == -1) {
1.58 djm 1756: error("read: %s", strerror(errno));
1.76 markus 1757: sftp_server_cleanup_exit(1);
1.120 djm 1758: } else if ((r = sshbuf_put(iqueue, buf, len)) != 0)
1759: fatal_fr(r, "sshbuf_put");
1.1 markus 1760: }
1761: /* send oqueue to stdout */
1.21 millert 1762: if (FD_ISSET(out, wset)) {
1.104 djm 1763: len = write(out, sshbuf_ptr(oqueue), olen);
1.116 deraadt 1764: if (len == -1) {
1.58 djm 1765: error("write: %s", strerror(errno));
1.76 markus 1766: sftp_server_cleanup_exit(1);
1.120 djm 1767: } else if ((r = sshbuf_consume(oqueue, len)) != 0)
1768: fatal_fr(r, "consume");
1.1 markus 1769: }
1.73 djm 1770:
1771: /*
1772: * Process requests from client if we can fit the results
1773: * into the output buffer, otherwise stop processing input
1774: * and let the output queue drain.
1775: */
1.104 djm 1776: r = sshbuf_check_reserve(oqueue, SFTP_MAX_MSG_LENGTH);
1777: if (r == 0)
1.73 djm 1778: process();
1.104 djm 1779: else if (r != SSH_ERR_NO_BUFFER_SPACE)
1.120 djm 1780: fatal_fr(r, "reserve");
1.1 markus 1781: }
1782: }