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