djm@openbsd.org | dbbc7e0 | 2019-01-16 23:22:10 +0000 | [diff] [blame] | 1 | /* $OpenBSD: sftp-server.c,v 1.114 2019/01/16 23:22:10 djm Exp $ */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 2 | /* |
Darren Tucker | 37bd366 | 2004-02-24 09:19:15 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2004 Markus Friedl. All rights reserved. |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 4 | * |
Darren Tucker | 37bd366 | 2004-02-24 09:19:15 +1100 | [diff] [blame] | 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. |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 8 | * |
Darren Tucker | 37bd366 | 2004-02-24 09:19:15 +1100 | [diff] [blame] | 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. |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 16 | */ |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 17 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 18 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 19 | |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 22 | #ifdef HAVE_SYS_TIME_H |
| 23 | # include <sys/time.h> |
| 24 | #endif |
Darren Tucker | 5b2e2ba | 2008-06-08 09:25:28 +1000 | [diff] [blame] | 25 | #ifdef HAVE_SYS_MOUNT_H |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 26 | #include <sys/mount.h> |
Darren Tucker | 5b2e2ba | 2008-06-08 09:25:28 +1000 | [diff] [blame] | 27 | #endif |
| 28 | #ifdef HAVE_SYS_STATVFS_H |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 29 | #include <sys/statvfs.h> |
Darren Tucker | 5b2e2ba | 2008-06-08 09:25:28 +1000 | [diff] [blame] | 30 | #endif |
Damien Miller | 88f254b | 2006-03-15 11:25:13 +1100 | [diff] [blame] | 31 | |
| 32 | #include <dirent.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 33 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 34 | #include <fcntl.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 35 | #include <pwd.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 36 | #include <stdlib.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 37 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 38 | #include <string.h> |
Damien Miller | 5598b4f | 2006-07-24 14:09:40 +1000 | [diff] [blame] | 39 | #include <time.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 40 | #include <unistd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 41 | #include <stdarg.h> |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 42 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 43 | #include "xmalloc.h" |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 44 | #include "sshbuf.h" |
| 45 | #include "ssherr.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include "log.h" |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 47 | #include "misc.h" |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 48 | #include "match.h" |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 49 | #include "uidswap.h" |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 50 | |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 51 | #include "sftp.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 52 | #include "sftp-common.h" |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 53 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 54 | /* Our verbosity */ |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 55 | static LogLevel log_level = SYSLOG_LEVEL_ERROR; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 56 | |
| 57 | /* Our client */ |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 58 | static struct passwd *pw = NULL; |
| 59 | static char *client_addr = NULL; |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 60 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 61 | /* input and output queue */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 62 | struct sshbuf *iqueue; |
| 63 | struct sshbuf *oqueue; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 64 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 65 | /* Version of client */ |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 66 | static u_int version; |
| 67 | |
| 68 | /* SSH2_FXP_INIT received */ |
| 69 | static int init_done; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 70 | |
Darren Tucker | db7bf82 | 2010-01-09 22:24:33 +1100 | [diff] [blame] | 71 | /* Disable writes */ |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 72 | static int readonly; |
| 73 | |
| 74 | /* Requests that are allowed/denied */ |
| 75 | static char *request_whitelist, *request_blacklist; |
Darren Tucker | db7bf82 | 2010-01-09 22:24:33 +1100 | [diff] [blame] | 76 | |
Darren Tucker | a6612d4 | 2003-06-28 12:39:03 +1000 | [diff] [blame] | 77 | /* portable attributes, etc. */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 78 | typedef struct Stat Stat; |
| 79 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 80 | struct Stat { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 81 | char *name; |
| 82 | char *long_name; |
| 83 | Attrib attrib; |
| 84 | }; |
| 85 | |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 86 | /* Packet handlers */ |
| 87 | static void process_open(u_int32_t id); |
| 88 | static void process_close(u_int32_t id); |
| 89 | static void process_read(u_int32_t id); |
| 90 | static void process_write(u_int32_t id); |
| 91 | static void process_stat(u_int32_t id); |
| 92 | static void process_lstat(u_int32_t id); |
| 93 | static void process_fstat(u_int32_t id); |
| 94 | static void process_setstat(u_int32_t id); |
| 95 | static void process_fsetstat(u_int32_t id); |
| 96 | static void process_opendir(u_int32_t id); |
| 97 | static void process_readdir(u_int32_t id); |
| 98 | static void process_remove(u_int32_t id); |
| 99 | static void process_mkdir(u_int32_t id); |
| 100 | static void process_rmdir(u_int32_t id); |
| 101 | static void process_realpath(u_int32_t id); |
| 102 | static void process_rename(u_int32_t id); |
| 103 | static void process_readlink(u_int32_t id); |
| 104 | static void process_symlink(u_int32_t id); |
| 105 | static void process_extended_posix_rename(u_int32_t id); |
| 106 | static void process_extended_statvfs(u_int32_t id); |
| 107 | static void process_extended_fstatvfs(u_int32_t id); |
| 108 | static void process_extended_hardlink(u_int32_t id); |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 109 | static void process_extended_fsync(u_int32_t id); |
djm@openbsd.org | dbbc7e0 | 2019-01-16 23:22:10 +0000 | [diff] [blame] | 110 | static void process_extended_lsetstat(u_int32_t id); |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 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 | |
djm@openbsd.org | 5bed70a | 2019-01-01 23:10:53 +0000 | [diff] [blame] | 121 | static const struct sftp_handler handlers[] = { |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 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 */ |
djm@openbsd.org | 5bed70a | 2019-01-01 23:10:53 +0000 | [diff] [blame] | 145 | static const struct sftp_handler extended_handlers[] = { |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 146 | { "posix-rename", "posix-rename@openssh.com", 0, |
| 147 | process_extended_posix_rename, 1 }, |
| 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 }, |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 151 | { "fsync", "fsync@openssh.com", 0, process_extended_fsync, 1 }, |
djm@openbsd.org | dbbc7e0 | 2019-01-16 23:22:10 +0000 | [diff] [blame] | 152 | { "lsetstat", "lsetstat@openssh.com", 0, process_extended_lsetstat, 1 }, |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 153 | { NULL, NULL, 0, NULL, 0 } |
| 154 | }; |
| 155 | |
| 156 | static int |
djm@openbsd.org | 5bed70a | 2019-01-01 23:10:53 +0000 | [diff] [blame] | 157 | request_permitted(const struct sftp_handler *h) |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 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 | } |
| 165 | if (request_blacklist != NULL && |
| 166 | ((result = match_list(h->name, request_blacklist, NULL))) != NULL) { |
| 167 | free(result); |
| 168 | verbose("Refusing blacklisted %s request", h->name); |
| 169 | return 0; |
| 170 | } |
| 171 | if (request_whitelist != NULL && |
| 172 | ((result = match_list(h->name, request_whitelist, NULL))) != NULL) { |
| 173 | free(result); |
| 174 | debug2("Permitting whitelisted %s request", h->name); |
| 175 | return 1; |
| 176 | } |
| 177 | if (request_whitelist != NULL) { |
| 178 | verbose("Refusing non-whitelisted %s request", h->name); |
| 179 | return 0; |
| 180 | } |
| 181 | return 1; |
| 182 | } |
| 183 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 184 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 185 | errno_to_portable(int unixerrno) |
| 186 | { |
| 187 | int ret = 0; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 188 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 189 | switch (unixerrno) { |
| 190 | case 0: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 191 | ret = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 192 | break; |
| 193 | case ENOENT: |
| 194 | case ENOTDIR: |
| 195 | case EBADF: |
| 196 | case ELOOP: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 197 | ret = SSH2_FX_NO_SUCH_FILE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 198 | break; |
| 199 | case EPERM: |
| 200 | case EACCES: |
| 201 | case EFAULT: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 202 | ret = SSH2_FX_PERMISSION_DENIED; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 203 | break; |
| 204 | case ENAMETOOLONG: |
| 205 | case EINVAL: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 206 | ret = SSH2_FX_BAD_MESSAGE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 207 | break; |
Darren Tucker | 422c34c | 2008-06-09 22:48:31 +1000 | [diff] [blame] | 208 | case ENOSYS: |
| 209 | ret = SSH2_FX_OP_UNSUPPORTED; |
| 210 | break; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 211 | default: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 212 | ret = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | return ret; |
| 216 | } |
| 217 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 218 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 219 | flags_from_portable(int pflags) |
| 220 | { |
| 221 | int flags = 0; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 222 | |
Ben Lindstrom | 3659251 | 2001-03-05 05:02:08 +0000 | [diff] [blame] | 223 | if ((pflags & SSH2_FXF_READ) && |
| 224 | (pflags & SSH2_FXF_WRITE)) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 225 | flags = O_RDWR; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 226 | } else if (pflags & SSH2_FXF_READ) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 227 | flags = O_RDONLY; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 228 | } else if (pflags & SSH2_FXF_WRITE) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 229 | flags = O_WRONLY; |
| 230 | } |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 231 | if (pflags & SSH2_FXF_APPEND) |
| 232 | flags |= O_APPEND; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 233 | if (pflags & SSH2_FXF_CREAT) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 234 | flags |= O_CREAT; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 235 | if (pflags & SSH2_FXF_TRUNC) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 236 | flags |= O_TRUNC; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 237 | if (pflags & SSH2_FXF_EXCL) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 238 | flags |= O_EXCL; |
| 239 | return flags; |
| 240 | } |
| 241 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 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)); \ |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 252 | strlcat(ret, str, sizeof(ret)); \ |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | if (pflags & SSH2_FXF_READ) |
| 256 | PAPPEND("READ") |
| 257 | if (pflags & SSH2_FXF_WRITE) |
| 258 | PAPPEND("WRITE") |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 259 | if (pflags & SSH2_FXF_APPEND) |
| 260 | PAPPEND("APPEND") |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 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 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 271 | /* handle handles */ |
| 272 | |
| 273 | typedef struct Handle Handle; |
| 274 | struct Handle { |
| 275 | int use; |
| 276 | DIR *dirp; |
| 277 | int fd; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 278 | int flags; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 279 | char *name; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 280 | u_int64_t bytes_read, bytes_write; |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 281 | int next_unused; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 282 | }; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 283 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 284 | enum { |
| 285 | HANDLE_UNUSED, |
| 286 | HANDLE_DIR, |
| 287 | HANDLE_FILE |
| 288 | }; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 289 | |
djm@openbsd.org | 5bed70a | 2019-01-01 23:10:53 +0000 | [diff] [blame] | 290 | static Handle *handles = NULL; |
| 291 | static u_int num_handles = 0; |
| 292 | static int first_unused_handle = -1; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 293 | |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 294 | static void handle_unused(int i) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 295 | { |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 296 | handles[i].use = HANDLE_UNUSED; |
| 297 | handles[i].next_unused = first_unused_handle; |
| 298 | first_unused_handle = i; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 299 | } |
| 300 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 301 | static int |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 302 | handle_new(int use, const char *name, int fd, int flags, DIR *dirp) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 303 | { |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 304 | int i; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 305 | |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 306 | if (first_unused_handle == -1) { |
| 307 | if (num_handles + 1 <= num_handles) |
| 308 | return -1; |
| 309 | num_handles++; |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 310 | handles = xreallocarray(handles, num_handles, sizeof(Handle)); |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 311 | handle_unused(num_handles - 1); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 312 | } |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 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; |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 320 | handles[i].flags = flags; |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 321 | handles[i].name = xstrdup(name); |
| 322 | handles[i].bytes_read = handles[i].bytes_write = 0; |
| 323 | |
| 324 | return i; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 325 | } |
| 326 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 327 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 328 | handle_is_ok(int i, int type) |
| 329 | { |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 330 | return i >= 0 && (u_int)i < num_handles && handles[i].use == type; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 331 | } |
| 332 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 333 | static int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 334 | handle_to_string(int handle, u_char **stringp, int *hlenp) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 335 | { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 336 | if (stringp == NULL || hlenp == NULL) |
| 337 | return -1; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 338 | *stringp = xmalloc(sizeof(int32_t)); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 339 | put_u32(*stringp, handle); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 340 | *hlenp = sizeof(int32_t); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 344 | static int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 345 | handle_from_string(const u_char *handle, u_int hlen) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 346 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 347 | int val; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 348 | |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 349 | if (hlen != sizeof(int32_t)) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 350 | return -1; |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 351 | val = get_u32(handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 352 | if (handle_is_ok(val, HANDLE_FILE) || |
| 353 | handle_is_ok(val, HANDLE_DIR)) |
| 354 | return val; |
| 355 | return -1; |
| 356 | } |
| 357 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 358 | static char * |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 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 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 367 | static DIR * |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 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 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 375 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 376 | handle_to_fd(int handle) |
| 377 | { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 378 | if (handle_is_ok(handle, HANDLE_FILE)) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 379 | return handles[handle].fd; |
| 380 | return -1; |
| 381 | } |
| 382 | |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 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 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 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 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 421 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 422 | handle_close(int handle) |
| 423 | { |
| 424 | int ret = -1; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 425 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 426 | if (handle_is_ok(handle, HANDLE_FILE)) { |
| 427 | ret = close(handles[handle].fd); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 428 | free(handles[handle].name); |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 429 | handle_unused(handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 430 | } else if (handle_is_ok(handle, HANDLE_DIR)) { |
| 431 | ret = closedir(handles[handle].dirp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 432 | free(handles[handle].name); |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 433 | handle_unused(handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 434 | } else { |
| 435 | errno = ENOENT; |
| 436 | } |
| 437 | return ret; |
| 438 | } |
| 439 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 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), |
Darren Tucker | 86473c5 | 2007-05-20 14:59:32 +1000 | [diff] [blame] | 447 | (unsigned long long)handle_bytes_read(handle), |
| 448 | (unsigned long long)handle_bytes_write(handle)); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 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 | |
Damien Miller | 3397d0e | 2008-02-10 22:26:51 +1100 | [diff] [blame] | 461 | for (i = 0; i < num_handles; i++) |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 462 | if (handles[i].use != HANDLE_UNUSED) |
| 463 | handle_log_close(i, "forced"); |
| 464 | } |
| 465 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 466 | static int |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 467 | get_handle(struct sshbuf *queue, int *hp) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 468 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 469 | u_char *handle; |
| 470 | int r; |
| 471 | size_t hlen; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 472 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 473 | *hp = -1; |
| 474 | if ((r = sshbuf_get_string(queue, &handle, &hlen)) != 0) |
| 475 | return r; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 476 | if (hlen < 256) |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 477 | *hp = handle_from_string(handle, hlen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 478 | free(handle); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 479 | return 0; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* send replies */ |
| 483 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 484 | static void |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 485 | send_msg(struct sshbuf *m) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 486 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 487 | int r; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 488 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 489 | if ((r = sshbuf_put_stringb(oqueue, m)) != 0) |
| 490 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 491 | sshbuf_reset(m); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 492 | } |
| 493 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 494 | static const char * |
| 495 | status_to_message(u_int32_t status) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 496 | { |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 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 | }; |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 509 | return (status_messages[MINIMUM(status,SSH2_FX_MAX)]); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 510 | } |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 511 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 512 | static void |
| 513 | send_status(u_int32_t id, u_int32_t status) |
| 514 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 515 | struct sshbuf *msg; |
| 516 | int r; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 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)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 522 | if ((msg = sshbuf_new()) == NULL) |
| 523 | fatal("%s: sshbuf_new failed", __func__); |
| 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) |
| 527 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 528 | if (version >= 3) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 529 | if ((r = sshbuf_put_cstring(msg, |
| 530 | status_to_message(status))) != 0 || |
| 531 | (r = sshbuf_put_cstring(msg, "")) != 0) |
| 532 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 533 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 534 | send_msg(msg); |
| 535 | sshbuf_free(msg); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 536 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 537 | static void |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 538 | send_data_or_handle(char type, u_int32_t id, const u_char *data, int dlen) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 539 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 540 | struct sshbuf *msg; |
| 541 | int r; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 542 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 543 | if ((msg = sshbuf_new()) == NULL) |
| 544 | fatal("%s: sshbuf_new failed", __func__); |
| 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) |
| 548 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 549 | send_msg(msg); |
| 550 | sshbuf_free(msg); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 551 | } |
| 552 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 553 | static void |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 554 | send_data(u_int32_t id, const u_char *data, int dlen) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 555 | { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 556 | debug("request %u: sent data len %d", id, dlen); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 557 | send_data_or_handle(SSH2_FXP_DATA, id, data, dlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 558 | } |
| 559 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 560 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 561 | send_handle(u_int32_t id, int handle) |
| 562 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 563 | u_char *string; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 564 | int hlen; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 565 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 566 | handle_to_string(handle, &string, &hlen); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 567 | debug("request %u: sent handle handle %d", id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 568 | send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 569 | free(string); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 570 | } |
| 571 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 572 | static void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 573 | send_names(u_int32_t id, int count, const Stat *stats) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 574 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 575 | struct sshbuf *msg; |
| 576 | int i, r; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 577 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 578 | if ((msg = sshbuf_new()) == NULL) |
| 579 | fatal("%s: sshbuf_new failed", __func__); |
| 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) |
| 583 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 584 | debug("request %u: sent names count %d", id, count); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 585 | for (i = 0; i < count; i++) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 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) |
| 589 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 590 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 591 | send_msg(msg); |
| 592 | sshbuf_free(msg); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 593 | } |
| 594 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 595 | static void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 596 | send_attrib(u_int32_t id, const Attrib *a) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 597 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 598 | struct sshbuf *msg; |
| 599 | int r; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 600 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 601 | debug("request %u: sent attrib have 0x%x", id, a->flags); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 602 | if ((msg = sshbuf_new()) == NULL) |
| 603 | fatal("%s: sshbuf_new failed", __func__); |
| 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) |
| 607 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 608 | send_msg(msg); |
| 609 | sshbuf_free(msg); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 610 | } |
| 611 | |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 612 | static void |
| 613 | send_statvfs(u_int32_t id, struct statvfs *st) |
| 614 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 615 | struct sshbuf *msg; |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 616 | u_int64_t flag; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 617 | int r; |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 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 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 622 | if ((msg = sshbuf_new()) == NULL) |
| 623 | fatal("%s: sshbuf_new failed", __func__); |
| 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, FSID_TO_ULONG(st->f_fsid))) != 0 || |
| 635 | (r = sshbuf_put_u64(msg, flag)) != 0 || |
| 636 | (r = sshbuf_put_u64(msg, st->f_namemax)) != 0) |
| 637 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 638 | send_msg(msg); |
| 639 | sshbuf_free(msg); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 640 | } |
| 641 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 642 | /* parse incoming */ |
| 643 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 644 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 645 | process_init(void) |
| 646 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 647 | struct sshbuf *msg; |
| 648 | int r; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 649 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 650 | if ((r = sshbuf_get_u32(iqueue, &version)) != 0) |
| 651 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | f145a5b | 2011-06-20 14:42:51 +1000 | [diff] [blame] | 652 | verbose("received client version %u", version); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 653 | if ((msg = sshbuf_new()) == NULL) |
| 654 | fatal("%s: sshbuf_new failed", __func__); |
| 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 || |
djm@openbsd.org | dbbc7e0 | 2019-01-16 23:22:10 +0000 | [diff] [blame] | 671 | (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */ |
| 672 | (r = sshbuf_put_cstring(msg, "lsetstat@openssh.com")) != 0 || |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 673 | (r = sshbuf_put_cstring(msg, "1")) != 0) /* version */ |
| 674 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 675 | send_msg(msg); |
| 676 | sshbuf_free(msg); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 677 | } |
| 678 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 679 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 680 | process_open(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 681 | { |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 682 | u_int32_t pflags; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 683 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 684 | char *name; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 685 | int r, handle, fd, flags, mode, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 686 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 687 | if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 || |
| 688 | (r = sshbuf_get_u32(iqueue, &pflags)) != 0 || /* portable flags */ |
| 689 | (r = decode_attrib(iqueue, &a)) != 0) |
| 690 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 691 | |
Damien Miller | 6444fe9 | 2006-07-10 21:31:27 +1000 | [diff] [blame] | 692 | debug3("request %u: open flags %d", id, pflags); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 693 | flags = flags_from_portable(pflags); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 694 | mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a.perm : 0666; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 695 | logit("open \"%s\" flags %s mode 0%o", |
| 696 | name, string_from_portable(pflags), mode); |
Darren Tucker | db7bf82 | 2010-01-09 22:24:33 +1100 | [diff] [blame] | 697 | if (readonly && |
djm@openbsd.org | 4d827f0 | 2017-04-04 00:24:56 +0000 | [diff] [blame] | 698 | ((flags & O_ACCMODE) != O_RDONLY || |
| 699 | (flags & (O_CREAT|O_TRUNC)) != 0)) { |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 700 | verbose("Refusing open request in read-only mode"); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 701 | status = SSH2_FX_PERMISSION_DENIED; |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 702 | } else { |
Darren Tucker | db7bf82 | 2010-01-09 22:24:33 +1100 | [diff] [blame] | 703 | fd = open(name, flags, mode); |
| 704 | if (fd < 0) { |
| 705 | status = errno_to_portable(errno); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 706 | } else { |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 707 | handle = handle_new(HANDLE_FILE, name, fd, flags, NULL); |
Darren Tucker | db7bf82 | 2010-01-09 22:24:33 +1100 | [diff] [blame] | 708 | if (handle < 0) { |
| 709 | close(fd); |
| 710 | } else { |
| 711 | send_handle(id, handle); |
| 712 | status = SSH2_FX_OK; |
| 713 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 714 | } |
| 715 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 716 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 717 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 718 | free(name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 719 | } |
| 720 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 721 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 722 | process_close(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 723 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 724 | int r, handle, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 725 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 726 | if ((r = get_handle(iqueue, &handle)) != 0) |
| 727 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 728 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 729 | debug3("request %u: close handle %u", id, handle); |
| 730 | handle_log_close(handle, NULL); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 731 | ret = handle_close(handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 732 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 733 | send_status(id, status); |
| 734 | } |
| 735 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 736 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 737 | process_read(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 738 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 739 | u_char buf[64*1024]; |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 740 | u_int32_t len; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 741 | int r, handle, fd, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 742 | u_int64_t off; |
| 743 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 744 | if ((r = get_handle(iqueue, &handle)) != 0 || |
| 745 | (r = sshbuf_get_u64(iqueue, &off)) != 0 || |
| 746 | (r = sshbuf_get_u32(iqueue, &len)) != 0) |
| 747 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 748 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 749 | debug("request %u: read \"%s\" (handle %d) off %llu len %d", |
| 750 | id, handle_to_name(handle), handle, (unsigned long long)off, len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 751 | if (len > sizeof buf) { |
| 752 | len = sizeof buf; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 753 | debug2("read change len %d", len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 754 | } |
| 755 | fd = handle_to_fd(handle); |
| 756 | if (fd >= 0) { |
| 757 | if (lseek(fd, off, SEEK_SET) < 0) { |
| 758 | error("process_read: seek failed"); |
| 759 | status = errno_to_portable(errno); |
| 760 | } else { |
| 761 | ret = read(fd, buf, len); |
| 762 | if (ret < 0) { |
| 763 | status = errno_to_portable(errno); |
| 764 | } else if (ret == 0) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 765 | status = SSH2_FX_EOF; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 766 | } else { |
| 767 | send_data(id, buf, ret); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 768 | status = SSH2_FX_OK; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 769 | handle_update_read(handle, ret); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 773 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 774 | send_status(id, status); |
| 775 | } |
| 776 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 777 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 778 | process_write(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 779 | { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 780 | u_int64_t off; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 781 | size_t len; |
| 782 | int r, handle, fd, ret, status; |
| 783 | u_char *data; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 784 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 785 | if ((r = get_handle(iqueue, &handle)) != 0 || |
| 786 | (r = sshbuf_get_u64(iqueue, &off)) != 0 || |
| 787 | (r = sshbuf_get_string(iqueue, &data, &len)) != 0) |
| 788 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 789 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 790 | debug("request %u: write \"%s\" (handle %d) off %llu len %zu", |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 791 | id, handle_to_name(handle), handle, (unsigned long long)off, len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 792 | fd = handle_to_fd(handle); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 793 | |
Darren Tucker | db7bf82 | 2010-01-09 22:24:33 +1100 | [diff] [blame] | 794 | if (fd < 0) |
| 795 | status = SSH2_FX_FAILURE; |
Darren Tucker | db7bf82 | 2010-01-09 22:24:33 +1100 | [diff] [blame] | 796 | else { |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 797 | if (!(handle_to_flags(handle) & O_APPEND) && |
| 798 | lseek(fd, off, SEEK_SET) < 0) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 799 | status = errno_to_portable(errno); |
| 800 | error("process_write: seek failed"); |
| 801 | } else { |
| 802 | /* XXX ATOMICIO ? */ |
| 803 | ret = write(fd, data, len); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 804 | if (ret < 0) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 805 | error("process_write: write failed"); |
| 806 | status = errno_to_portable(errno); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 807 | } else if ((size_t)ret == len) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 808 | status = SSH2_FX_OK; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 809 | handle_update_write(handle, ret); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 810 | } else { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 811 | debug2("nothing at all written"); |
Darren Tucker | db7bf82 | 2010-01-09 22:24:33 +1100 | [diff] [blame] | 812 | status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | } |
| 816 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 817 | free(data); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 818 | } |
| 819 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 820 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 821 | process_do_stat(u_int32_t id, int do_lstat) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 822 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 823 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 824 | struct stat st; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 825 | char *name; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 826 | int r, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 827 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 828 | if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0) |
| 829 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 830 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 831 | debug3("request %u: %sstat", id, do_lstat ? "l" : ""); |
| 832 | verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 833 | r = do_lstat ? lstat(name, &st) : stat(name, &st); |
| 834 | if (r < 0) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 835 | status = errno_to_portable(errno); |
| 836 | } else { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 837 | stat_to_attrib(&st, &a); |
| 838 | send_attrib(id, &a); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 839 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 840 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 841 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 842 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 843 | free(name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 844 | } |
| 845 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 846 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 847 | process_stat(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 848 | { |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 849 | process_do_stat(id, 0); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 850 | } |
| 851 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 852 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 853 | process_lstat(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 854 | { |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 855 | process_do_stat(id, 1); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 856 | } |
| 857 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 858 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 859 | process_fstat(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 860 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 861 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 862 | struct stat st; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 863 | int fd, r, handle, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 864 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 865 | if ((r = get_handle(iqueue, &handle)) != 0) |
| 866 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 867 | debug("request %u: fstat \"%s\" (handle %u)", |
| 868 | id, handle_to_name(handle), handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 869 | fd = handle_to_fd(handle); |
Damien Miller | e2334d6 | 2007-01-05 16:31:02 +1100 | [diff] [blame] | 870 | if (fd >= 0) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 871 | r = fstat(fd, &st); |
| 872 | if (r < 0) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 873 | status = errno_to_portable(errno); |
| 874 | } else { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 875 | stat_to_attrib(&st, &a); |
| 876 | send_attrib(id, &a); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 877 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 878 | } |
| 879 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 880 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 881 | send_status(id, status); |
| 882 | } |
| 883 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 884 | static struct timeval * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 885 | attrib_to_tv(const Attrib *a) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 886 | { |
| 887 | static struct timeval tv[2]; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 888 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 889 | tv[0].tv_sec = a->atime; |
| 890 | tv[0].tv_usec = 0; |
| 891 | tv[1].tv_sec = a->mtime; |
| 892 | tv[1].tv_usec = 0; |
| 893 | return tv; |
| 894 | } |
| 895 | |
djm@openbsd.org | dbbc7e0 | 2019-01-16 23:22:10 +0000 | [diff] [blame] | 896 | static struct timespec * |
| 897 | attrib_to_ts(const Attrib *a) |
| 898 | { |
| 899 | static struct timespec ts[2]; |
| 900 | |
| 901 | ts[0].tv_sec = a->atime; |
| 902 | ts[0].tv_nsec = 0; |
| 903 | ts[1].tv_sec = a->mtime; |
| 904 | ts[1].tv_nsec = 0; |
| 905 | return ts; |
| 906 | } |
| 907 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 908 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 909 | process_setstat(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 910 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 911 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 912 | char *name; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 913 | int r, status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 914 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 915 | if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 || |
| 916 | (r = decode_attrib(iqueue, &a)) != 0) |
| 917 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 918 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 919 | debug("request %u: setstat name \"%s\"", id, name); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 920 | if (a.flags & SSH2_FILEXFER_ATTR_SIZE) { |
Darren Tucker | 86473c5 | 2007-05-20 14:59:32 +1000 | [diff] [blame] | 921 | logit("set \"%s\" size %llu", |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 922 | name, (unsigned long long)a.size); |
| 923 | r = truncate(name, a.size); |
| 924 | if (r == -1) |
Damien Miller | 00c9217 | 2002-02-13 14:05:00 +1100 | [diff] [blame] | 925 | status = errno_to_portable(errno); |
| 926 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 927 | if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
| 928 | logit("set \"%s\" mode %04o", name, a.perm); |
| 929 | r = chmod(name, a.perm & 07777); |
| 930 | if (r == -1) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 931 | status = errno_to_portable(errno); |
| 932 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 933 | if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 934 | char buf[64]; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 935 | time_t t = a.mtime; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 936 | |
| 937 | strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S", |
| 938 | localtime(&t)); |
| 939 | logit("set \"%s\" modtime %s", name, buf); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 940 | r = utimes(name, attrib_to_tv(&a)); |
| 941 | if (r == -1) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 942 | status = errno_to_portable(errno); |
| 943 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 944 | if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 945 | logit("set \"%s\" owner %lu group %lu", name, |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 946 | (u_long)a.uid, (u_long)a.gid); |
| 947 | r = chown(name, a.uid, a.gid); |
| 948 | if (r == -1) |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 949 | status = errno_to_portable(errno); |
| 950 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 951 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 952 | free(name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 953 | } |
| 954 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 955 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 956 | process_fsetstat(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 957 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 958 | Attrib a; |
| 959 | int handle, fd, r; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 960 | int status = SSH2_FX_OK; |
Kevin Steves | f7ffab3 | 2001-01-24 20:11:06 +0000 | [diff] [blame] | 961 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 962 | if ((r = get_handle(iqueue, &handle)) != 0 || |
| 963 | (r = decode_attrib(iqueue, &a)) != 0) |
| 964 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 965 | |
| 966 | debug("request %u: fsetstat handle %d", id, handle); |
| 967 | fd = handle_to_fd(handle); |
| 968 | if (fd < 0) |
| 969 | status = SSH2_FX_FAILURE; |
| 970 | else { |
| 971 | char *name = handle_to_name(handle); |
| 972 | |
| 973 | if (a.flags & SSH2_FILEXFER_ATTR_SIZE) { |
| 974 | logit("set \"%s\" size %llu", |
| 975 | name, (unsigned long long)a.size); |
| 976 | r = ftruncate(fd, a.size); |
| 977 | if (r == -1) |
| 978 | status = errno_to_portable(errno); |
| 979 | } |
| 980 | if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
| 981 | logit("set \"%s\" mode %04o", name, a.perm); |
| 982 | #ifdef HAVE_FCHMOD |
Damien Miller | 83b9678 | 2015-01-15 02:35:50 +1100 | [diff] [blame] | 983 | r = fchmod(fd, a.perm & 07777); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 984 | #else |
Damien Miller | 83b9678 | 2015-01-15 02:35:50 +1100 | [diff] [blame] | 985 | r = chmod(name, a.perm & 07777); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 986 | #endif |
| 987 | if (r == -1) |
| 988 | status = errno_to_portable(errno); |
| 989 | } |
| 990 | if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
| 991 | char buf[64]; |
| 992 | time_t t = a.mtime; |
| 993 | |
| 994 | strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S", |
| 995 | localtime(&t)); |
| 996 | logit("set \"%s\" modtime %s", name, buf); |
| 997 | #ifdef HAVE_FUTIMES |
Damien Miller | 83b9678 | 2015-01-15 02:35:50 +1100 | [diff] [blame] | 998 | r = futimes(fd, attrib_to_tv(&a)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 999 | #else |
Damien Miller | 83b9678 | 2015-01-15 02:35:50 +1100 | [diff] [blame] | 1000 | r = utimes(name, attrib_to_tv(&a)); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1001 | #endif |
| 1002 | if (r == -1) |
| 1003 | status = errno_to_portable(errno); |
| 1004 | } |
| 1005 | if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) { |
| 1006 | logit("set \"%s\" owner %lu group %lu", name, |
| 1007 | (u_long)a.uid, (u_long)a.gid); |
| 1008 | #ifdef HAVE_FCHOWN |
Damien Miller | 83b9678 | 2015-01-15 02:35:50 +1100 | [diff] [blame] | 1009 | r = fchown(fd, a.uid, a.gid); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1010 | #else |
Damien Miller | 83b9678 | 2015-01-15 02:35:50 +1100 | [diff] [blame] | 1011 | r = chown(name, a.uid, a.gid); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1012 | #endif |
| 1013 | if (r == -1) |
| 1014 | status = errno_to_portable(errno); |
| 1015 | } |
| 1016 | } |
| 1017 | send_status(id, status); |
| 1018 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1019 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1020 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1021 | process_opendir(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1022 | { |
| 1023 | DIR *dirp = NULL; |
| 1024 | char *path; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1025 | int r, handle, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1026 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1027 | if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0) |
| 1028 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1029 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1030 | debug3("request %u: opendir", id); |
| 1031 | logit("opendir \"%s\"", path); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1032 | dirp = opendir(path); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1033 | if (dirp == NULL) { |
| 1034 | status = errno_to_portable(errno); |
| 1035 | } else { |
Damien Miller | e9fc72e | 2013-10-15 12:14:12 +1100 | [diff] [blame] | 1036 | handle = handle_new(HANDLE_DIR, path, 0, 0, dirp); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1037 | if (handle < 0) { |
| 1038 | closedir(dirp); |
| 1039 | } else { |
| 1040 | send_handle(id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1041 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1042 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1043 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1044 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1045 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1046 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1047 | free(path); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1048 | } |
| 1049 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1050 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1051 | process_readdir(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1052 | { |
| 1053 | DIR *dirp; |
| 1054 | struct dirent *dp; |
| 1055 | char *path; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1056 | int r, handle; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1057 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1058 | if ((r = get_handle(iqueue, &handle)) != 0) |
| 1059 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1060 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1061 | debug("request %u: readdir \"%s\" (handle %d)", id, |
| 1062 | handle_to_name(handle), handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1063 | dirp = handle_to_dir(handle); |
| 1064 | path = handle_to_name(handle); |
| 1065 | if (dirp == NULL || path == NULL) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1066 | send_status(id, SSH2_FX_FAILURE); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1067 | } else { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1068 | struct stat st; |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 1069 | char pathname[PATH_MAX]; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1070 | Stat *stats; |
| 1071 | int nstats = 10, count = 0, i; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1072 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1073 | stats = xcalloc(nstats, sizeof(Stat)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1074 | while ((dp = readdir(dirp)) != NULL) { |
| 1075 | if (count >= nstats) { |
| 1076 | nstats *= 2; |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 1077 | stats = xreallocarray(stats, nstats, sizeof(Stat)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1078 | } |
| 1079 | /* XXX OVERFLOW ? */ |
Ben Lindstrom | 95148e3 | 2001-08-06 21:30:53 +0000 | [diff] [blame] | 1080 | snprintf(pathname, sizeof pathname, "%s%s%s", path, |
| 1081 | strcmp(path, "/") ? "/" : "", dp->d_name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1082 | if (lstat(pathname, &st) < 0) |
| 1083 | continue; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1084 | stat_to_attrib(&st, &(stats[count].attrib)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1085 | stats[count].name = xstrdup(dp->d_name); |
Darren Tucker | 2901e2d | 2010-01-13 22:44:06 +1100 | [diff] [blame] | 1086 | stats[count].long_name = ls_file(dp->d_name, &st, 0, 0); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1087 | count++; |
| 1088 | /* send up to 100 entries in one message */ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1089 | /* XXX check packet size instead */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1090 | if (count == 100) |
| 1091 | break; |
| 1092 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1093 | if (count > 0) { |
| 1094 | send_names(id, count, stats); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1095 | for (i = 0; i < count; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1096 | free(stats[i].name); |
| 1097 | free(stats[i].long_name); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1098 | } |
| 1099 | } else { |
| 1100 | send_status(id, SSH2_FX_EOF); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1101 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1102 | free(stats); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1106 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1107 | process_remove(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1108 | { |
| 1109 | char *name; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1110 | int r, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1111 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1112 | if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0) |
| 1113 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1114 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1115 | debug3("request %u: remove", id); |
| 1116 | logit("remove name \"%s\"", name); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1117 | r = unlink(name); |
| 1118 | status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1119 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1120 | free(name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1121 | } |
| 1122 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1123 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1124 | process_mkdir(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1125 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1126 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1127 | char *name; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1128 | int r, mode, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1129 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1130 | if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 || |
| 1131 | (r = decode_attrib(iqueue, &a)) != 0) |
| 1132 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1133 | |
| 1134 | mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? |
| 1135 | a.perm & 07777 : 0777; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1136 | debug3("request %u: mkdir", id); |
| 1137 | logit("mkdir name \"%s\" mode 0%o", name, mode); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1138 | r = mkdir(name, mode); |
| 1139 | status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1140 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1141 | free(name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1142 | } |
| 1143 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1144 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1145 | process_rmdir(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1146 | { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1147 | char *name; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1148 | int r, status; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1149 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1150 | if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0) |
| 1151 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1152 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1153 | debug3("request %u: rmdir", id); |
| 1154 | logit("rmdir name \"%s\"", name); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1155 | r = rmdir(name); |
| 1156 | status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1157 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1158 | free(name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1159 | } |
| 1160 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1161 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1162 | process_realpath(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1163 | { |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 1164 | char resolvedname[PATH_MAX]; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1165 | char *path; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1166 | int r; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1167 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1168 | if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0) |
| 1169 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1170 | |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 1171 | if (path[0] == '\0') { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1172 | free(path); |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 1173 | path = xstrdup("."); |
| 1174 | } |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1175 | debug3("request %u: realpath", id); |
| 1176 | verbose("realpath \"%s\"", path); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1177 | if (realpath(path, resolvedname) == NULL) { |
| 1178 | send_status(id, errno_to_portable(errno)); |
| 1179 | } else { |
| 1180 | Stat s; |
| 1181 | attrib_clear(&s.attrib); |
| 1182 | s.name = s.long_name = resolvedname; |
| 1183 | send_names(id, 1, &s); |
| 1184 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1185 | free(path); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1186 | } |
| 1187 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1188 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1189 | process_rename(u_int32_t id) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1190 | { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1191 | char *oldpath, *newpath; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1192 | int r, status; |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 1193 | struct stat sb; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1194 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1195 | if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 || |
| 1196 | (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0) |
| 1197 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1198 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1199 | debug3("request %u: rename", id); |
| 1200 | logit("rename old \"%s\" new \"%s\"", oldpath, newpath); |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 1201 | status = SSH2_FX_FAILURE; |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1202 | if (lstat(oldpath, &sb) == -1) |
Damien Miller | 9e51a73 | 2003-02-24 11:58:44 +1100 | [diff] [blame] | 1203 | status = errno_to_portable(errno); |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 1204 | else if (S_ISREG(sb.st_mode)) { |
| 1205 | /* Race-free rename of regular files */ |
Darren Tucker | aedc1d6 | 2004-06-25 17:06:02 +1000 | [diff] [blame] | 1206 | if (link(oldpath, newpath) == -1) { |
Damien Miller | 0e26551 | 2009-08-28 10:43:13 +1000 | [diff] [blame] | 1207 | if (errno == EOPNOTSUPP || errno == ENOSYS |
Darren Tucker | f7fa706 | 2008-07-04 14:10:19 +1000 | [diff] [blame] | 1208 | #ifdef EXDEV |
| 1209 | || errno == EXDEV |
| 1210 | #endif |
Darren Tucker | e59b508 | 2004-06-28 16:01:19 +1000 | [diff] [blame] | 1211 | #ifdef LINK_OPNOTSUPP_ERRNO |
| 1212 | || errno == LINK_OPNOTSUPP_ERRNO |
| 1213 | #endif |
| 1214 | ) { |
Darren Tucker | aedc1d6 | 2004-06-25 17:06:02 +1000 | [diff] [blame] | 1215 | struct stat st; |
| 1216 | |
| 1217 | /* |
| 1218 | * fs doesn't support links, so fall back to |
| 1219 | * stat+rename. This is racy. |
| 1220 | */ |
| 1221 | if (stat(newpath, &st) == -1) { |
| 1222 | if (rename(oldpath, newpath) == -1) |
| 1223 | status = |
| 1224 | errno_to_portable(errno); |
| 1225 | else |
| 1226 | status = SSH2_FX_OK; |
| 1227 | } |
| 1228 | } else { |
| 1229 | status = errno_to_portable(errno); |
| 1230 | } |
| 1231 | } else if (unlink(oldpath) == -1) { |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 1232 | status = errno_to_portable(errno); |
| 1233 | /* clean spare link */ |
| 1234 | unlink(newpath); |
| 1235 | } else |
| 1236 | status = SSH2_FX_OK; |
| 1237 | } else if (stat(newpath, &sb) == -1) { |
| 1238 | if (rename(oldpath, newpath) == -1) |
| 1239 | status = errno_to_portable(errno); |
| 1240 | else |
| 1241 | status = SSH2_FX_OK; |
| 1242 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1243 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1244 | free(oldpath); |
| 1245 | free(newpath); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1246 | } |
| 1247 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1248 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1249 | process_readlink(u_int32_t id) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1250 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1251 | int r, len; |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 1252 | char buf[PATH_MAX]; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1253 | char *path; |
| 1254 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1255 | if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0) |
| 1256 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1257 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1258 | debug3("request %u: readlink", id); |
| 1259 | verbose("readlink \"%s\"", path); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1260 | if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1261 | send_status(id, errno_to_portable(errno)); |
| 1262 | else { |
| 1263 | Stat s; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1264 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1265 | buf[len] = '\0'; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1266 | attrib_clear(&s.attrib); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1267 | s.name = s.long_name = buf; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1268 | send_names(id, 1, &s); |
| 1269 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1270 | free(path); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1271 | } |
| 1272 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1273 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1274 | process_symlink(u_int32_t id) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1275 | { |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1276 | char *oldpath, *newpath; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1277 | int r, status; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1278 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1279 | if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 || |
| 1280 | (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0) |
| 1281 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1282 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1283 | debug3("request %u: symlink", id); |
| 1284 | logit("symlink old \"%s\" new \"%s\"", oldpath, newpath); |
Damien Miller | 9e51a73 | 2003-02-24 11:58:44 +1100 | [diff] [blame] | 1285 | /* this will fail if 'newpath' exists */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1286 | r = symlink(oldpath, newpath); |
| 1287 | status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1288 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1289 | free(oldpath); |
| 1290 | free(newpath); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1291 | } |
| 1292 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1293 | static void |
Damien Miller | 7c29661 | 2008-03-07 18:33:53 +1100 | [diff] [blame] | 1294 | process_extended_posix_rename(u_int32_t id) |
| 1295 | { |
| 1296 | char *oldpath, *newpath; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1297 | int r, status; |
Damien Miller | 7c29661 | 2008-03-07 18:33:53 +1100 | [diff] [blame] | 1298 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1299 | if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 || |
| 1300 | (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0) |
| 1301 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1302 | |
Damien Miller | 7c29661 | 2008-03-07 18:33:53 +1100 | [diff] [blame] | 1303 | debug3("request %u: posix-rename", id); |
| 1304 | logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1305 | r = rename(oldpath, newpath); |
| 1306 | status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Darren Tucker | db7bf82 | 2010-01-09 22:24:33 +1100 | [diff] [blame] | 1307 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1308 | free(oldpath); |
| 1309 | free(newpath); |
Damien Miller | 7c29661 | 2008-03-07 18:33:53 +1100 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | static void |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1313 | process_extended_statvfs(u_int32_t id) |
| 1314 | { |
| 1315 | char *path; |
| 1316 | struct statvfs st; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1317 | int r; |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1318 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1319 | if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0) |
| 1320 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Darren Tucker | 2aca159 | 2014-01-19 15:25:34 +1100 | [diff] [blame] | 1321 | debug3("request %u: statvfs", id); |
| 1322 | logit("statvfs \"%s\"", path); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1323 | |
| 1324 | if (statvfs(path, &st) != 0) |
| 1325 | send_status(id, errno_to_portable(errno)); |
| 1326 | else |
| 1327 | send_statvfs(id, &st); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1328 | free(path); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | static void |
| 1332 | process_extended_fstatvfs(u_int32_t id) |
| 1333 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1334 | int r, handle, fd; |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1335 | struct statvfs st; |
| 1336 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1337 | if ((r = get_handle(iqueue, &handle)) != 0) |
| 1338 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | d671e5a | 2008-05-19 14:53:33 +1000 | [diff] [blame] | 1339 | debug("request %u: fstatvfs \"%s\" (handle %u)", |
| 1340 | id, handle_to_name(handle), handle); |
| 1341 | if ((fd = handle_to_fd(handle)) < 0) { |
| 1342 | send_status(id, SSH2_FX_FAILURE); |
| 1343 | return; |
| 1344 | } |
| 1345 | if (fstatvfs(fd, &st) != 0) |
| 1346 | send_status(id, errno_to_portable(errno)); |
| 1347 | else |
| 1348 | send_statvfs(id, &st); |
| 1349 | } |
| 1350 | |
| 1351 | static void |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 1352 | process_extended_hardlink(u_int32_t id) |
| 1353 | { |
| 1354 | char *oldpath, *newpath; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1355 | int r, status; |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 1356 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1357 | if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 || |
| 1358 | (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0) |
| 1359 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1360 | |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 1361 | debug3("request %u: hardlink", id); |
| 1362 | logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1363 | r = link(oldpath, newpath); |
| 1364 | status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 1365 | send_status(id, status); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1366 | free(oldpath); |
| 1367 | free(newpath); |
Darren Tucker | af1f909 | 2010-12-05 09:02:47 +1100 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | static void |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1371 | process_extended_fsync(u_int32_t id) |
| 1372 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1373 | int handle, fd, r, status = SSH2_FX_OP_UNSUPPORTED; |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1374 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1375 | if ((r = get_handle(iqueue, &handle)) != 0) |
| 1376 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1377 | debug3("request %u: fsync (handle %u)", id, handle); |
| 1378 | verbose("fsync \"%s\"", handle_to_name(handle)); |
| 1379 | if ((fd = handle_to_fd(handle)) < 0) |
| 1380 | status = SSH2_FX_NO_SUCH_FILE; |
| 1381 | else if (handle_is_ok(handle, HANDLE_FILE)) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1382 | r = fsync(fd); |
| 1383 | status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | f29238e | 2013-10-17 11:48:52 +1100 | [diff] [blame] | 1384 | } |
| 1385 | send_status(id, status); |
| 1386 | } |
| 1387 | |
| 1388 | static void |
djm@openbsd.org | dbbc7e0 | 2019-01-16 23:22:10 +0000 | [diff] [blame] | 1389 | process_extended_lsetstat(u_int32_t id) |
| 1390 | { |
| 1391 | Attrib a; |
| 1392 | char *name; |
| 1393 | int r, status = SSH2_FX_OK; |
| 1394 | |
| 1395 | if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 || |
| 1396 | (r = decode_attrib(iqueue, &a)) != 0) |
| 1397 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1398 | |
| 1399 | debug("request %u: lsetstat name \"%s\"", id, name); |
| 1400 | if (a.flags & SSH2_FILEXFER_ATTR_SIZE) { |
| 1401 | /* nonsensical for links */ |
| 1402 | status = SSH2_FX_BAD_MESSAGE; |
| 1403 | goto out; |
| 1404 | } |
| 1405 | if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
| 1406 | logit("set \"%s\" mode %04o", name, a.perm); |
| 1407 | r = fchmodat(AT_FDCWD, name, |
| 1408 | a.perm & 07777, AT_SYMLINK_NOFOLLOW); |
| 1409 | if (r == -1) |
| 1410 | status = errno_to_portable(errno); |
| 1411 | } |
| 1412 | if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
| 1413 | char buf[64]; |
| 1414 | time_t t = a.mtime; |
| 1415 | |
| 1416 | strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S", |
| 1417 | localtime(&t)); |
| 1418 | logit("set \"%s\" modtime %s", name, buf); |
| 1419 | r = utimensat(AT_FDCWD, name, |
| 1420 | attrib_to_ts(&a), AT_SYMLINK_NOFOLLOW); |
| 1421 | if (r == -1) |
| 1422 | status = errno_to_portable(errno); |
| 1423 | } |
| 1424 | if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) { |
| 1425 | logit("set \"%s\" owner %lu group %lu", name, |
| 1426 | (u_long)a.uid, (u_long)a.gid); |
| 1427 | r = fchownat(AT_FDCWD, name, a.uid, a.gid, |
| 1428 | AT_SYMLINK_NOFOLLOW); |
| 1429 | if (r == -1) |
| 1430 | status = errno_to_portable(errno); |
| 1431 | } |
| 1432 | out: |
| 1433 | send_status(id, status); |
| 1434 | free(name); |
| 1435 | } |
| 1436 | |
| 1437 | static void |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1438 | process_extended(u_int32_t id) |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1439 | { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1440 | char *request; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1441 | int i, r; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1442 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1443 | if ((r = sshbuf_get_cstring(iqueue, &request, NULL)) != 0) |
| 1444 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1445 | for (i = 0; extended_handlers[i].handler != NULL; i++) { |
| 1446 | if (strcmp(request, extended_handlers[i].ext_name) == 0) { |
| 1447 | if (!request_permitted(&extended_handlers[i])) |
| 1448 | send_status(id, SSH2_FX_PERMISSION_DENIED); |
| 1449 | else |
| 1450 | extended_handlers[i].handler(id); |
| 1451 | break; |
| 1452 | } |
| 1453 | } |
| 1454 | if (extended_handlers[i].handler == NULL) { |
| 1455 | error("Unknown extended request \"%.100s\"", request); |
Damien Miller | 7c29661 | 2008-03-07 18:33:53 +1100 | [diff] [blame] | 1456 | send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */ |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1457 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1458 | free(request); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1459 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1460 | |
| 1461 | /* stolen from ssh-agent */ |
| 1462 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1463 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1464 | process(void) |
| 1465 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1466 | u_int msg_len; |
| 1467 | u_int buf_len; |
| 1468 | u_int consumed; |
| 1469 | u_char type; |
| 1470 | const u_char *cp; |
| 1471 | int i, r; |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1472 | u_int32_t id; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1473 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1474 | buf_len = sshbuf_len(iqueue); |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1475 | if (buf_len < 5) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1476 | return; /* Incomplete message. */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1477 | cp = sshbuf_ptr(iqueue); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1478 | msg_len = get_u32(cp); |
Damien Miller | 5444618 | 2006-01-02 23:40:50 +1100 | [diff] [blame] | 1479 | if (msg_len > SFTP_MAX_MSG_LENGTH) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1480 | error("bad message from %s local user %s", |
| 1481 | client_addr, pw->pw_name); |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1482 | sftp_server_cleanup_exit(11); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1483 | } |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1484 | if (buf_len < msg_len + 4) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1485 | return; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1486 | if ((r = sshbuf_consume(iqueue, 4)) != 0) |
| 1487 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1488 | buf_len -= 4; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1489 | if ((r = sshbuf_get_u8(iqueue, &type)) != 0) |
| 1490 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1491 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1492 | switch (type) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1493 | case SSH2_FXP_INIT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1494 | process_init(); |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1495 | init_done = 1; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1496 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1497 | case SSH2_FXP_EXTENDED: |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1498 | if (!init_done) |
| 1499 | fatal("Received extended request before init"); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1500 | if ((r = sshbuf_get_u32(iqueue, &id)) != 0) |
| 1501 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1502 | process_extended(id); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1503 | break; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1504 | default: |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1505 | if (!init_done) |
| 1506 | fatal("Received %u request before init", type); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1507 | if ((r = sshbuf_get_u32(iqueue, &id)) != 0) |
| 1508 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1509 | for (i = 0; handlers[i].handler != NULL; i++) { |
| 1510 | if (type == handlers[i].type) { |
| 1511 | if (!request_permitted(&handlers[i])) { |
| 1512 | send_status(id, |
| 1513 | SSH2_FX_PERMISSION_DENIED); |
| 1514 | } else { |
| 1515 | handlers[i].handler(id); |
| 1516 | } |
| 1517 | break; |
| 1518 | } |
| 1519 | } |
| 1520 | if (handlers[i].handler == NULL) |
| 1521 | error("Unknown message %u", type); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1522 | } |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1523 | /* discard the remaining bytes from the current packet */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1524 | if (buf_len < sshbuf_len(iqueue)) { |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1525 | error("iqueue grew unexpectedly"); |
| 1526 | sftp_server_cleanup_exit(255); |
| 1527 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1528 | consumed = buf_len - sshbuf_len(iqueue); |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1529 | if (msg_len < consumed) { |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1530 | error("msg_len %u < consumed %u", msg_len, consumed); |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1531 | sftp_server_cleanup_exit(255); |
| 1532 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1533 | if (msg_len > consumed && |
| 1534 | (r = sshbuf_consume(iqueue, msg_len - consumed)) != 0) |
| 1535 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1536 | } |
| 1537 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1538 | /* Cleanup handler that logs active handles upon normal exit */ |
| 1539 | void |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1540 | sftp_server_cleanup_exit(int i) |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1541 | { |
| 1542 | if (pw != NULL && client_addr != NULL) { |
| 1543 | handle_log_exit(); |
| 1544 | logit("session closed for local user %s from [%s]", |
| 1545 | pw->pw_name, client_addr); |
| 1546 | } |
| 1547 | _exit(i); |
| 1548 | } |
| 1549 | |
| 1550 | static void |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1551 | sftp_server_usage(void) |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1552 | { |
| 1553 | extern char *__progname; |
| 1554 | |
| 1555 | fprintf(stderr, |
Damien Miller | aa7ad30 | 2013-01-09 15:58:21 +1100 | [diff] [blame] | 1556 | "usage: %s [-ehR] [-d start_directory] [-f log_facility] " |
Damien Miller | 6efab27 | 2013-10-15 12:07:05 +1100 | [diff] [blame] | 1557 | "[-l log_level]\n\t[-P blacklisted_requests] " |
| 1558 | "[-p whitelisted_requests] [-u umask]\n" |
| 1559 | " %s -Q protocol_feature\n", |
| 1560 | __progname, __progname); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1561 | exit(1); |
| 1562 | } |
| 1563 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1564 | int |
Damien Miller | d8cb1f1 | 2008-02-10 22:40:12 +1100 | [diff] [blame] | 1565 | sftp_server_main(int argc, char **argv, struct passwd *user_pw) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1566 | { |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1567 | fd_set *rset, *wset; |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1568 | int i, r, in, out, max, ch, skipargs = 0, log_stderr = 0; |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1569 | ssize_t len, olen, set_size; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1570 | SyslogFacility log_facility = SYSLOG_FACILITY_AUTH; |
djm@openbsd.org | 9c935dd | 2018-06-01 03:33:53 +0000 | [diff] [blame] | 1571 | char *cp, *homedir = NULL, uidstr[32], buf[4*4096]; |
Damien Miller | 0733121 | 2010-11-05 10:20:31 +1100 | [diff] [blame] | 1572 | long mask; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1573 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1574 | extern char *optarg; |
| 1575 | extern char *__progname; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1576 | |
dtucker@openbsd.org | ffb1e7e | 2016-02-15 09:47:49 +0000 | [diff] [blame] | 1577 | ssh_malloc_init(); /* must be called before any mallocs */ |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1578 | __progname = ssh_get_progname(argv[0]); |
| 1579 | log_init(__progname, log_level, log_facility, log_stderr); |
Ben Lindstrom | c7f4ccd | 2001-03-15 00:09:15 +0000 | [diff] [blame] | 1580 | |
Damien Miller | 502ab0e | 2013-01-09 15:57:36 +1100 | [diff] [blame] | 1581 | pw = pwcopy(user_pw); |
| 1582 | |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1583 | while (!skipargs && (ch = getopt(argc, argv, |
| 1584 | "d:f:l:P:p:Q:u:cehR")) != -1) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1585 | switch (ch) { |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1586 | case 'Q': |
| 1587 | if (strcasecmp(optarg, "requests") != 0) { |
| 1588 | fprintf(stderr, "Invalid query type\n"); |
| 1589 | exit(1); |
| 1590 | } |
| 1591 | for (i = 0; handlers[i].handler != NULL; i++) |
| 1592 | printf("%s\n", handlers[i].name); |
| 1593 | for (i = 0; extended_handlers[i].handler != NULL; i++) |
| 1594 | printf("%s\n", extended_handlers[i].name); |
| 1595 | exit(0); |
| 1596 | break; |
Darren Tucker | db7bf82 | 2010-01-09 22:24:33 +1100 | [diff] [blame] | 1597 | case 'R': |
| 1598 | readonly = 1; |
| 1599 | break; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1600 | case 'c': |
| 1601 | /* |
| 1602 | * Ignore all arguments if we are invoked as a |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 1603 | * shell using "sftp-server -c command" |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1604 | */ |
| 1605 | skipargs = 1; |
| 1606 | break; |
| 1607 | case 'e': |
| 1608 | log_stderr = 1; |
| 1609 | break; |
| 1610 | case 'l': |
| 1611 | log_level = log_level_number(optarg); |
| 1612 | if (log_level == SYSLOG_LEVEL_NOT_SET) |
| 1613 | error("Invalid log level \"%s\"", optarg); |
| 1614 | break; |
| 1615 | case 'f': |
| 1616 | log_facility = log_facility_number(optarg); |
Damien Miller | 35e18db | 2007-09-17 16:11:33 +1000 | [diff] [blame] | 1617 | if (log_facility == SYSLOG_FACILITY_NOT_SET) |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1618 | error("Invalid log facility \"%s\"", optarg); |
| 1619 | break; |
Damien Miller | 502ab0e | 2013-01-09 15:57:36 +1100 | [diff] [blame] | 1620 | case 'd': |
| 1621 | cp = tilde_expand_filename(optarg, user_pw->pw_uid); |
djm@openbsd.org | 9c935dd | 2018-06-01 03:33:53 +0000 | [diff] [blame] | 1622 | snprintf(uidstr, sizeof(uidstr), "%llu", |
| 1623 | (unsigned long long)pw->pw_uid); |
Damien Miller | 502ab0e | 2013-01-09 15:57:36 +1100 | [diff] [blame] | 1624 | homedir = percent_expand(cp, "d", user_pw->pw_dir, |
djm@openbsd.org | 9c935dd | 2018-06-01 03:33:53 +0000 | [diff] [blame] | 1625 | "u", user_pw->pw_name, "U", uidstr, (char *)NULL); |
Damien Miller | 502ab0e | 2013-01-09 15:57:36 +1100 | [diff] [blame] | 1626 | free(cp); |
| 1627 | break; |
Damien Miller | 6eaeebf | 2013-10-15 11:55:57 +1100 | [diff] [blame] | 1628 | case 'p': |
| 1629 | if (request_whitelist != NULL) |
| 1630 | fatal("Permitted requests already set"); |
| 1631 | request_whitelist = xstrdup(optarg); |
| 1632 | break; |
| 1633 | case 'P': |
| 1634 | if (request_blacklist != NULL) |
| 1635 | fatal("Refused requests already set"); |
| 1636 | request_blacklist = xstrdup(optarg); |
| 1637 | break; |
Darren Tucker | 7dc4850 | 2009-10-07 08:44:42 +1100 | [diff] [blame] | 1638 | case 'u': |
Damien Miller | 0733121 | 2010-11-05 10:20:31 +1100 | [diff] [blame] | 1639 | errno = 0; |
| 1640 | mask = strtol(optarg, &cp, 8); |
| 1641 | if (mask < 0 || mask > 0777 || *cp != '\0' || |
| 1642 | cp == optarg || (mask == 0 && errno != 0)) |
| 1643 | fatal("Invalid umask \"%s\"", optarg); |
| 1644 | (void)umask((mode_t)mask); |
Darren Tucker | 7dc4850 | 2009-10-07 08:44:42 +1100 | [diff] [blame] | 1645 | break; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1646 | case 'h': |
| 1647 | default: |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1648 | sftp_server_usage(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | log_init(__progname, log_level, log_facility, log_stderr); |
| 1653 | |
Damien Miller | 14928b7 | 2014-04-01 14:38:07 +1100 | [diff] [blame] | 1654 | /* |
Darren Tucker | 0fb7f59 | 2016-06-09 16:23:07 +1000 | [diff] [blame] | 1655 | * On platforms where we can, avoid making /proc/self/{mem,maps} |
Damien Miller | 14928b7 | 2014-04-01 14:38:07 +1100 | [diff] [blame] | 1656 | * available to the user so that sftp access doesn't automatically |
| 1657 | * imply arbitrary code execution access that will break |
| 1658 | * restricted configurations. |
| 1659 | */ |
Darren Tucker | 0fb7f59 | 2016-06-09 16:23:07 +1000 | [diff] [blame] | 1660 | platform_disable_tracing(1); /* strict */ |
Damien Miller | 14928b7 | 2014-04-01 14:38:07 +1100 | [diff] [blame] | 1661 | |
Damien Miller | 4626cba | 2016-01-08 14:24:56 +1100 | [diff] [blame] | 1662 | /* Drop any fine-grained privileges we don't need */ |
| 1663 | platform_pledge_sftp_server(); |
| 1664 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1665 | if ((cp = getenv("SSH_CONNECTION")) != NULL) { |
| 1666 | client_addr = xstrdup(cp); |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1667 | if ((cp = strchr(client_addr, ' ')) == NULL) { |
| 1668 | error("Malformed SSH_CONNECTION variable: \"%s\"", |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1669 | getenv("SSH_CONNECTION")); |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1670 | sftp_server_cleanup_exit(255); |
| 1671 | } |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1672 | *cp = '\0'; |
| 1673 | } else |
| 1674 | client_addr = xstrdup("UNKNOWN"); |
| 1675 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1676 | logit("session opened for local user %s from [%s]", |
| 1677 | pw->pw_name, client_addr); |
| 1678 | |
Darren Tucker | aaf51d2 | 2010-01-08 19:04:49 +1100 | [diff] [blame] | 1679 | in = STDIN_FILENO; |
| 1680 | out = STDOUT_FILENO; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1681 | |
Damien Miller | 402b331 | 2001-04-14 00:28:42 +1000 | [diff] [blame] | 1682 | #ifdef HAVE_CYGWIN |
| 1683 | setmode(in, O_BINARY); |
| 1684 | setmode(out, O_BINARY); |
| 1685 | #endif |
| 1686 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1687 | max = 0; |
| 1688 | if (in > max) |
| 1689 | max = in; |
| 1690 | if (out > max) |
| 1691 | max = out; |
| 1692 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1693 | if ((iqueue = sshbuf_new()) == NULL) |
| 1694 | fatal("%s: sshbuf_new failed", __func__); |
| 1695 | if ((oqueue = sshbuf_new()) == NULL) |
| 1696 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1697 | |
logan@openbsd.org | db6f8dc | 2015-11-16 06:13:04 +0000 | [diff] [blame] | 1698 | rset = xcalloc(howmany(max + 1, NFDBITS), sizeof(fd_mask)); |
| 1699 | wset = xcalloc(howmany(max + 1, NFDBITS), sizeof(fd_mask)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1700 | |
Damien Miller | 502ab0e | 2013-01-09 15:57:36 +1100 | [diff] [blame] | 1701 | if (homedir != NULL) { |
| 1702 | if (chdir(homedir) != 0) { |
| 1703 | error("chdir to \"%s\" failed: %s", homedir, |
| 1704 | strerror(errno)); |
| 1705 | } |
| 1706 | } |
| 1707 | |
logan@openbsd.org | db6f8dc | 2015-11-16 06:13:04 +0000 | [diff] [blame] | 1708 | set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask); |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1709 | for (;;) { |
| 1710 | memset(rset, 0, set_size); |
| 1711 | memset(wset, 0, set_size); |
| 1712 | |
Darren Tucker | e940598 | 2007-05-20 15:09:04 +1000 | [diff] [blame] | 1713 | /* |
| 1714 | * Ensure that we can read a full buffer and handle |
| 1715 | * the worst-case length packet it can generate, |
| 1716 | * otherwise apply backpressure by stopping reads. |
| 1717 | */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1718 | if ((r = sshbuf_check_reserve(iqueue, sizeof(buf))) == 0 && |
| 1719 | (r = sshbuf_check_reserve(oqueue, |
| 1720 | SFTP_MAX_MSG_LENGTH)) == 0) |
Darren Tucker | e940598 | 2007-05-20 15:09:04 +1000 | [diff] [blame] | 1721 | FD_SET(in, rset); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1722 | else if (r != SSH_ERR_NO_BUFFER_SPACE) |
| 1723 | fatal("%s: sshbuf_check_reserve failed: %s", |
| 1724 | __func__, ssh_err(r)); |
Darren Tucker | e940598 | 2007-05-20 15:09:04 +1000 | [diff] [blame] | 1725 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1726 | olen = sshbuf_len(oqueue); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1727 | if (olen > 0) |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1728 | FD_SET(out, wset); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1729 | |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1730 | if (select(max+1, rset, wset, NULL, NULL) < 0) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1731 | if (errno == EINTR) |
| 1732 | continue; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1733 | error("select: %s", strerror(errno)); |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1734 | sftp_server_cleanup_exit(2); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | /* copy stdin to iqueue */ |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1738 | if (FD_ISSET(in, rset)) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1739 | len = read(in, buf, sizeof buf); |
| 1740 | if (len == 0) { |
| 1741 | debug("read eof"); |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1742 | sftp_server_cleanup_exit(0); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1743 | } else if (len < 0) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1744 | error("read: %s", strerror(errno)); |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1745 | sftp_server_cleanup_exit(1); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1746 | } else if ((r = sshbuf_put(iqueue, buf, len)) != 0) { |
| 1747 | fatal("%s: buffer error: %s", |
| 1748 | __func__, ssh_err(r)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1749 | } |
| 1750 | } |
| 1751 | /* send oqueue to stdout */ |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1752 | if (FD_ISSET(out, wset)) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1753 | len = write(out, sshbuf_ptr(oqueue), olen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1754 | if (len < 0) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame] | 1755 | error("write: %s", strerror(errno)); |
Damien Miller | dfc2425 | 2008-02-10 22:29:40 +1100 | [diff] [blame] | 1756 | sftp_server_cleanup_exit(1); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1757 | } else if ((r = sshbuf_consume(oqueue, len)) != 0) { |
| 1758 | fatal("%s: buffer error: %s", |
| 1759 | __func__, ssh_err(r)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1760 | } |
| 1761 | } |
Darren Tucker | e940598 | 2007-05-20 15:09:04 +1000 | [diff] [blame] | 1762 | |
| 1763 | /* |
| 1764 | * Process requests from client if we can fit the results |
| 1765 | * into the output buffer, otherwise stop processing input |
| 1766 | * and let the output queue drain. |
| 1767 | */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1768 | r = sshbuf_check_reserve(oqueue, SFTP_MAX_MSG_LENGTH); |
| 1769 | if (r == 0) |
Darren Tucker | e940598 | 2007-05-20 15:09:04 +1000 | [diff] [blame] | 1770 | process(); |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 1771 | else if (r != SSH_ERR_NO_BUFFER_SPACE) |
| 1772 | fatal("%s: sshbuf_check_reserve: %s", |
| 1773 | __func__, ssh_err(r)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1774 | } |
| 1775 | } |