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