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