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