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