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