Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1 | /* $OpenBSD: sftp-server.c,v 1.58 2006/07/06 10:47:57 djm Exp $ */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 2 | /* |
Darren Tucker | 37bd366 | 2004-02-24 09:19:15 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2004 Markus Friedl. All rights reserved. |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 4 | * |
Darren Tucker | 37bd366 | 2004-02-24 09:19:15 +1100 | [diff] [blame] | 5 | * Permission to use, copy, modify, and distribute this software for any |
| 6 | * purpose with or without fee is hereby granted, provided that the above |
| 7 | * copyright notice and this permission notice appear in all copies. |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 8 | * |
Darren Tucker | 37bd366 | 2004-02-24 09:19:15 +1100 | [diff] [blame] | 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 16 | */ |
| 17 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 18 | |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
Damien Miller | 88f254b | 2006-03-15 11:25:13 +1100 | [diff] [blame] | 21 | |
| 22 | #include <dirent.h> |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 23 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 24 | #include "buffer.h" |
| 25 | #include "bufaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 26 | #include "log.h" |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 27 | #include "xmalloc.h" |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 28 | #include "misc.h" |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 29 | #include "uidswap.h" |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 30 | |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 31 | #include "sftp.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 32 | #include "sftp-common.h" |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 33 | |
| 34 | /* helper */ |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 35 | #define get_int64() buffer_get_int64(&iqueue); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 36 | #define get_int() buffer_get_int(&iqueue); |
| 37 | #define get_string(lenp) buffer_get_string(&iqueue, lenp); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 38 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 39 | /* Our verbosity */ |
| 40 | LogLevel log_level = SYSLOG_LEVEL_ERROR; |
| 41 | |
| 42 | /* Our client */ |
| 43 | struct passwd *pw = NULL; |
| 44 | char *client_addr = NULL; |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 45 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 46 | /* input and output queue */ |
| 47 | Buffer iqueue; |
| 48 | Buffer oqueue; |
| 49 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 50 | /* Version of client */ |
| 51 | int version; |
| 52 | |
Darren Tucker | a6612d4 | 2003-06-28 12:39:03 +1000 | [diff] [blame] | 53 | /* portable attributes, etc. */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 54 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 55 | typedef struct Stat Stat; |
| 56 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 57 | struct Stat { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 58 | char *name; |
| 59 | char *long_name; |
| 60 | Attrib attrib; |
| 61 | }; |
| 62 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 63 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 64 | errno_to_portable(int unixerrno) |
| 65 | { |
| 66 | int ret = 0; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 67 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 68 | switch (unixerrno) { |
| 69 | case 0: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 70 | ret = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 71 | break; |
| 72 | case ENOENT: |
| 73 | case ENOTDIR: |
| 74 | case EBADF: |
| 75 | case ELOOP: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 76 | ret = SSH2_FX_NO_SUCH_FILE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 77 | break; |
| 78 | case EPERM: |
| 79 | case EACCES: |
| 80 | case EFAULT: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 81 | ret = SSH2_FX_PERMISSION_DENIED; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 82 | break; |
| 83 | case ENAMETOOLONG: |
| 84 | case EINVAL: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 85 | ret = SSH2_FX_BAD_MESSAGE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 86 | break; |
| 87 | default: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 88 | ret = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 89 | break; |
| 90 | } |
| 91 | return ret; |
| 92 | } |
| 93 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 94 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 95 | flags_from_portable(int pflags) |
| 96 | { |
| 97 | int flags = 0; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 98 | |
Ben Lindstrom | 3659251 | 2001-03-05 05:02:08 +0000 | [diff] [blame] | 99 | if ((pflags & SSH2_FXF_READ) && |
| 100 | (pflags & SSH2_FXF_WRITE)) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 101 | flags = O_RDWR; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 102 | } else if (pflags & SSH2_FXF_READ) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 103 | flags = O_RDONLY; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 104 | } else if (pflags & SSH2_FXF_WRITE) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 105 | flags = O_WRONLY; |
| 106 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 107 | if (pflags & SSH2_FXF_CREAT) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 108 | flags |= O_CREAT; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 109 | if (pflags & SSH2_FXF_TRUNC) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 110 | flags |= O_TRUNC; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 111 | if (pflags & SSH2_FXF_EXCL) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 112 | flags |= O_EXCL; |
| 113 | return flags; |
| 114 | } |
| 115 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 116 | static const char * |
| 117 | string_from_portable(int pflags) |
| 118 | { |
| 119 | static char ret[128]; |
| 120 | |
| 121 | *ret = '\0'; |
| 122 | |
| 123 | #define PAPPEND(str) { \ |
| 124 | if (*ret != '\0') \ |
| 125 | strlcat(ret, ",", sizeof(ret)); \ |
| 126 | strlcat(ret, str, sizeof(ret)); \ |
| 127 | } |
| 128 | |
| 129 | if (pflags & SSH2_FXF_READ) |
| 130 | PAPPEND("READ") |
| 131 | if (pflags & SSH2_FXF_WRITE) |
| 132 | PAPPEND("WRITE") |
| 133 | if (pflags & SSH2_FXF_CREAT) |
| 134 | PAPPEND("CREATE") |
| 135 | if (pflags & SSH2_FXF_TRUNC) |
| 136 | PAPPEND("TRUNCATE") |
| 137 | if (pflags & SSH2_FXF_EXCL) |
| 138 | PAPPEND("EXCL") |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 143 | static Attrib * |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 144 | get_attrib(void) |
| 145 | { |
| 146 | return decode_attrib(&iqueue); |
| 147 | } |
| 148 | |
| 149 | /* handle handles */ |
| 150 | |
| 151 | typedef struct Handle Handle; |
| 152 | struct Handle { |
| 153 | int use; |
| 154 | DIR *dirp; |
| 155 | int fd; |
| 156 | char *name; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 157 | u_int64_t bytes_read, bytes_write; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 158 | }; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 159 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 160 | enum { |
| 161 | HANDLE_UNUSED, |
| 162 | HANDLE_DIR, |
| 163 | HANDLE_FILE |
| 164 | }; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 165 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 166 | Handle handles[100]; |
| 167 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 168 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 169 | handle_init(void) |
| 170 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 171 | u_int i; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 172 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 173 | for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 174 | handles[i].use = HANDLE_UNUSED; |
| 175 | } |
| 176 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 177 | static int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 178 | handle_new(int use, const char *name, int fd, DIR *dirp) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 179 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 180 | u_int i; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 181 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 182 | for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 183 | if (handles[i].use == HANDLE_UNUSED) { |
| 184 | handles[i].use = use; |
| 185 | handles[i].dirp = dirp; |
| 186 | handles[i].fd = fd; |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 187 | handles[i].name = xstrdup(name); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 188 | handles[i].bytes_read = handles[i].bytes_write = 0; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 189 | return i; |
| 190 | } |
| 191 | } |
| 192 | return -1; |
| 193 | } |
| 194 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 195 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 196 | handle_is_ok(int i, int type) |
| 197 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 198 | return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) && |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 199 | handles[i].use == type; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 200 | } |
| 201 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 202 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 203 | handle_to_string(int handle, char **stringp, int *hlenp) |
| 204 | { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 205 | if (stringp == NULL || hlenp == NULL) |
| 206 | return -1; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 207 | *stringp = xmalloc(sizeof(int32_t)); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 208 | put_u32(*stringp, handle); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 209 | *hlenp = sizeof(int32_t); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 213 | static int |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 214 | handle_from_string(const char *handle, u_int hlen) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 215 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 216 | int val; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 217 | |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 218 | if (hlen != sizeof(int32_t)) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 219 | return -1; |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 220 | val = get_u32(handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 221 | if (handle_is_ok(val, HANDLE_FILE) || |
| 222 | handle_is_ok(val, HANDLE_DIR)) |
| 223 | return val; |
| 224 | return -1; |
| 225 | } |
| 226 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 227 | static char * |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 228 | handle_to_name(int handle) |
| 229 | { |
| 230 | if (handle_is_ok(handle, HANDLE_DIR)|| |
| 231 | handle_is_ok(handle, HANDLE_FILE)) |
| 232 | return handles[handle].name; |
| 233 | return NULL; |
| 234 | } |
| 235 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 236 | static DIR * |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 237 | handle_to_dir(int handle) |
| 238 | { |
| 239 | if (handle_is_ok(handle, HANDLE_DIR)) |
| 240 | return handles[handle].dirp; |
| 241 | return NULL; |
| 242 | } |
| 243 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 244 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 245 | handle_to_fd(int handle) |
| 246 | { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 247 | if (handle_is_ok(handle, HANDLE_FILE)) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 248 | return handles[handle].fd; |
| 249 | return -1; |
| 250 | } |
| 251 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 252 | static void |
| 253 | handle_update_read(int handle, ssize_t bytes) |
| 254 | { |
| 255 | if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0) |
| 256 | handles[handle].bytes_read += bytes; |
| 257 | } |
| 258 | |
| 259 | static void |
| 260 | handle_update_write(int handle, ssize_t bytes) |
| 261 | { |
| 262 | if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0) |
| 263 | handles[handle].bytes_write += bytes; |
| 264 | } |
| 265 | |
| 266 | static u_int64_t |
| 267 | handle_bytes_read(int handle) |
| 268 | { |
| 269 | if (handle_is_ok(handle, HANDLE_FILE)) |
| 270 | return (handles[handle].bytes_read); |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static u_int64_t |
| 275 | handle_bytes_write(int handle) |
| 276 | { |
| 277 | if (handle_is_ok(handle, HANDLE_FILE)) |
| 278 | return (handles[handle].bytes_write); |
| 279 | return 0; |
| 280 | } |
| 281 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 282 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 283 | handle_close(int handle) |
| 284 | { |
| 285 | int ret = -1; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 286 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 287 | if (handle_is_ok(handle, HANDLE_FILE)) { |
| 288 | ret = close(handles[handle].fd); |
| 289 | handles[handle].use = HANDLE_UNUSED; |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 290 | xfree(handles[handle].name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 291 | } else if (handle_is_ok(handle, HANDLE_DIR)) { |
| 292 | ret = closedir(handles[handle].dirp); |
| 293 | handles[handle].use = HANDLE_UNUSED; |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 294 | xfree(handles[handle].name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 295 | } else { |
| 296 | errno = ENOENT; |
| 297 | } |
| 298 | return ret; |
| 299 | } |
| 300 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 301 | static void |
| 302 | handle_log_close(int handle, char *emsg) |
| 303 | { |
| 304 | if (handle_is_ok(handle, HANDLE_FILE)) { |
| 305 | logit("%s%sclose \"%s\" bytes read %llu written %llu", |
| 306 | emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ", |
| 307 | handle_to_name(handle), |
| 308 | handle_bytes_read(handle), handle_bytes_write(handle)); |
| 309 | } else { |
| 310 | logit("%s%sclosedir \"%s\"", |
| 311 | emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ", |
| 312 | handle_to_name(handle)); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | static void |
| 317 | handle_log_exit(void) |
| 318 | { |
| 319 | u_int i; |
| 320 | |
| 321 | for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) |
| 322 | if (handles[i].use != HANDLE_UNUSED) |
| 323 | handle_log_close(i, "forced"); |
| 324 | } |
| 325 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 326 | static int |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 327 | get_handle(void) |
| 328 | { |
| 329 | char *handle; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 330 | int val = -1; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 331 | u_int hlen; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 332 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 333 | handle = get_string(&hlen); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 334 | if (hlen < 256) |
| 335 | val = handle_from_string(handle, hlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 336 | xfree(handle); |
| 337 | return val; |
| 338 | } |
| 339 | |
| 340 | /* send replies */ |
| 341 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 342 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 343 | send_msg(Buffer *m) |
| 344 | { |
| 345 | int mlen = buffer_len(m); |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 346 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 347 | buffer_put_int(&oqueue, mlen); |
| 348 | buffer_append(&oqueue, buffer_ptr(m), mlen); |
| 349 | buffer_consume(m, mlen); |
| 350 | } |
| 351 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 352 | static const char * |
| 353 | status_to_message(u_int32_t status) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 354 | { |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 355 | const char *status_messages[] = { |
| 356 | "Success", /* SSH_FX_OK */ |
| 357 | "End of file", /* SSH_FX_EOF */ |
| 358 | "No such file", /* SSH_FX_NO_SUCH_FILE */ |
| 359 | "Permission denied", /* SSH_FX_PERMISSION_DENIED */ |
| 360 | "Failure", /* SSH_FX_FAILURE */ |
| 361 | "Bad message", /* SSH_FX_BAD_MESSAGE */ |
| 362 | "No connection", /* SSH_FX_NO_CONNECTION */ |
| 363 | "Connection lost", /* SSH_FX_CONNECTION_LOST */ |
| 364 | "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */ |
| 365 | "Unknown error" /* Others */ |
| 366 | }; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 367 | return (status_messages[MIN(status,SSH2_FX_MAX)]); |
| 368 | } |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 369 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 370 | static void |
| 371 | send_status(u_int32_t id, u_int32_t status) |
| 372 | { |
| 373 | Buffer msg; |
| 374 | |
| 375 | debug3("request %u: sent status %u", id, status); |
| 376 | if (log_level > SYSLOG_LEVEL_VERBOSE || |
| 377 | (status != SSH2_FX_OK && status != SSH2_FX_EOF)) |
| 378 | logit("sent status %s", status_to_message(status)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 379 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 380 | buffer_put_char(&msg, SSH2_FXP_STATUS); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 381 | buffer_put_int(&msg, id); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 382 | buffer_put_int(&msg, status); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 383 | if (version >= 3) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 384 | buffer_put_cstring(&msg, status_to_message(status)); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 385 | buffer_put_cstring(&msg, ""); |
| 386 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 387 | send_msg(&msg); |
| 388 | buffer_free(&msg); |
| 389 | } |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 390 | static void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 391 | 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] | 392 | { |
| 393 | Buffer msg; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 394 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 395 | buffer_init(&msg); |
| 396 | buffer_put_char(&msg, type); |
| 397 | buffer_put_int(&msg, id); |
| 398 | buffer_put_string(&msg, data, dlen); |
| 399 | send_msg(&msg); |
| 400 | buffer_free(&msg); |
| 401 | } |
| 402 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 403 | static void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 404 | send_data(u_int32_t id, const char *data, int dlen) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 405 | { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 406 | debug("request %u: sent data len %d", id, dlen); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 407 | send_data_or_handle(SSH2_FXP_DATA, id, data, dlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 408 | } |
| 409 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 410 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 411 | send_handle(u_int32_t id, int handle) |
| 412 | { |
| 413 | char *string; |
| 414 | int hlen; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 415 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 416 | handle_to_string(handle, &string, &hlen); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 417 | debug("request %u: sent handle handle %d", id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 418 | send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 419 | xfree(string); |
| 420 | } |
| 421 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 422 | static void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 423 | send_names(u_int32_t id, int count, const Stat *stats) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 424 | { |
| 425 | Buffer msg; |
| 426 | int i; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 427 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 428 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 429 | buffer_put_char(&msg, SSH2_FXP_NAME); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 430 | buffer_put_int(&msg, id); |
| 431 | buffer_put_int(&msg, count); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 432 | debug("request %u: sent names count %d", id, count); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 433 | for (i = 0; i < count; i++) { |
| 434 | buffer_put_cstring(&msg, stats[i].name); |
| 435 | buffer_put_cstring(&msg, stats[i].long_name); |
| 436 | encode_attrib(&msg, &stats[i].attrib); |
| 437 | } |
| 438 | send_msg(&msg); |
| 439 | buffer_free(&msg); |
| 440 | } |
| 441 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 442 | static void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 443 | send_attrib(u_int32_t id, const Attrib *a) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 444 | { |
| 445 | Buffer msg; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 446 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 447 | debug("request %u: sent attrib have 0x%x", id, a->flags); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 448 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 449 | buffer_put_char(&msg, SSH2_FXP_ATTRS); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 450 | buffer_put_int(&msg, id); |
| 451 | encode_attrib(&msg, a); |
| 452 | send_msg(&msg); |
| 453 | buffer_free(&msg); |
| 454 | } |
| 455 | |
| 456 | /* parse incoming */ |
| 457 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 458 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 459 | process_init(void) |
| 460 | { |
| 461 | Buffer msg; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 462 | |
Ben Lindstrom | 937df1d | 2002-06-06 21:58:35 +0000 | [diff] [blame] | 463 | version = get_int(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 464 | verbose("received client version %d", version); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 465 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 466 | buffer_put_char(&msg, SSH2_FXP_VERSION); |
| 467 | buffer_put_int(&msg, SSH2_FILEXFER_VERSION); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 468 | send_msg(&msg); |
| 469 | buffer_free(&msg); |
| 470 | } |
| 471 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 472 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 473 | process_open(void) |
| 474 | { |
| 475 | u_int32_t id, pflags; |
| 476 | Attrib *a; |
| 477 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 478 | int handle, fd, flags, mode, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 479 | |
| 480 | id = get_int(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 481 | debug3("request %u: open flags %d", id, pflags); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 482 | name = get_string(NULL); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 483 | pflags = get_int(); /* portable flags */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 484 | a = get_attrib(); |
| 485 | flags = flags_from_portable(pflags); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 486 | mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 487 | logit("open \"%s\" flags %s mode 0%o", |
| 488 | name, string_from_portable(pflags), mode); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 489 | fd = open(name, flags, mode); |
| 490 | if (fd < 0) { |
| 491 | status = errno_to_portable(errno); |
| 492 | } else { |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 493 | handle = handle_new(HANDLE_FILE, name, fd, NULL); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 494 | if (handle < 0) { |
| 495 | close(fd); |
| 496 | } else { |
| 497 | send_handle(id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 498 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 499 | } |
| 500 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 501 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 502 | send_status(id, status); |
| 503 | xfree(name); |
| 504 | } |
| 505 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 506 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 507 | process_close(void) |
| 508 | { |
| 509 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 510 | int handle, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 511 | |
| 512 | id = get_int(); |
| 513 | handle = get_handle(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 514 | debug3("request %u: close handle %u", id, handle); |
| 515 | handle_log_close(handle, NULL); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 516 | ret = handle_close(handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 517 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 518 | send_status(id, status); |
| 519 | } |
| 520 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 521 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 522 | process_read(void) |
| 523 | { |
| 524 | char buf[64*1024]; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 525 | u_int32_t id, len; |
| 526 | int handle, fd, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 527 | u_int64_t off; |
| 528 | |
| 529 | id = get_int(); |
| 530 | handle = get_handle(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 531 | off = get_int64(); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 532 | len = get_int(); |
| 533 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 534 | debug("request %u: read \"%s\" (handle %d) off %llu len %d", |
| 535 | id, handle_to_name(handle), handle, (unsigned long long)off, len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 536 | if (len > sizeof buf) { |
| 537 | len = sizeof buf; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 538 | debug2("read change len %d", len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 539 | } |
| 540 | fd = handle_to_fd(handle); |
| 541 | if (fd >= 0) { |
| 542 | if (lseek(fd, off, SEEK_SET) < 0) { |
| 543 | error("process_read: seek failed"); |
| 544 | status = errno_to_portable(errno); |
| 545 | } else { |
| 546 | ret = read(fd, buf, len); |
| 547 | if (ret < 0) { |
| 548 | status = errno_to_portable(errno); |
| 549 | } else if (ret == 0) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 550 | status = SSH2_FX_EOF; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 551 | } else { |
| 552 | send_data(id, buf, ret); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 553 | status = SSH2_FX_OK; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 554 | handle_update_read(handle, ret); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 558 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 559 | send_status(id, status); |
| 560 | } |
| 561 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 562 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 563 | process_write(void) |
| 564 | { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 565 | u_int32_t id; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 566 | u_int64_t off; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 567 | u_int len; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 568 | int handle, fd, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 569 | char *data; |
| 570 | |
| 571 | id = get_int(); |
| 572 | handle = get_handle(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 573 | off = get_int64(); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 574 | data = get_string(&len); |
| 575 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 576 | debug("request %u: write \"%s\" (handle %d) off %llu len %d", |
| 577 | id, handle_to_name(handle), handle, (unsigned long long)off, len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 578 | fd = handle_to_fd(handle); |
| 579 | if (fd >= 0) { |
| 580 | if (lseek(fd, off, SEEK_SET) < 0) { |
| 581 | status = errno_to_portable(errno); |
| 582 | error("process_write: seek failed"); |
| 583 | } else { |
| 584 | /* XXX ATOMICIO ? */ |
| 585 | ret = write(fd, data, len); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 586 | if (ret < 0) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 587 | error("process_write: write failed"); |
| 588 | status = errno_to_portable(errno); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 589 | } else if ((size_t)ret == len) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 590 | status = SSH2_FX_OK; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 591 | handle_update_write(handle, ret); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 592 | } else { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 593 | debug2("nothing at all written"); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | } |
| 597 | send_status(id, status); |
| 598 | xfree(data); |
| 599 | } |
| 600 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 601 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 602 | process_do_stat(int do_lstat) |
| 603 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 604 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 605 | struct stat st; |
| 606 | u_int32_t id; |
| 607 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 608 | int ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 609 | |
| 610 | id = get_int(); |
| 611 | name = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 612 | debug3("request %u: %sstat", id, do_lstat ? "l" : ""); |
| 613 | verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 614 | ret = do_lstat ? lstat(name, &st) : stat(name, &st); |
| 615 | if (ret < 0) { |
| 616 | status = errno_to_portable(errno); |
| 617 | } else { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 618 | stat_to_attrib(&st, &a); |
| 619 | send_attrib(id, &a); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 620 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 621 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 622 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 623 | send_status(id, status); |
| 624 | xfree(name); |
| 625 | } |
| 626 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 627 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 628 | process_stat(void) |
| 629 | { |
| 630 | process_do_stat(0); |
| 631 | } |
| 632 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 633 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 634 | process_lstat(void) |
| 635 | { |
| 636 | process_do_stat(1); |
| 637 | } |
| 638 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 639 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 640 | process_fstat(void) |
| 641 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 642 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 643 | struct stat st; |
| 644 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 645 | int fd, ret, handle, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 646 | |
| 647 | id = get_int(); |
| 648 | handle = get_handle(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 649 | debug("request %u: fstat \"%s\" (handle %u)", |
| 650 | id, handle_to_name(handle), handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 651 | fd = handle_to_fd(handle); |
| 652 | if (fd >= 0) { |
| 653 | ret = fstat(fd, &st); |
| 654 | if (ret < 0) { |
| 655 | status = errno_to_portable(errno); |
| 656 | } else { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 657 | stat_to_attrib(&st, &a); |
| 658 | send_attrib(id, &a); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 659 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 660 | } |
| 661 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 662 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 663 | send_status(id, status); |
| 664 | } |
| 665 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 666 | static struct timeval * |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 667 | attrib_to_tv(const Attrib *a) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 668 | { |
| 669 | static struct timeval tv[2]; |
Ben Lindstrom | 1addabd | 2001-03-05 07:09:11 +0000 | [diff] [blame] | 670 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 671 | tv[0].tv_sec = a->atime; |
| 672 | tv[0].tv_usec = 0; |
| 673 | tv[1].tv_sec = a->mtime; |
| 674 | tv[1].tv_usec = 0; |
| 675 | return tv; |
| 676 | } |
| 677 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 678 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 679 | process_setstat(void) |
| 680 | { |
| 681 | Attrib *a; |
| 682 | u_int32_t id; |
| 683 | char *name; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 684 | int status = SSH2_FX_OK, ret; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 685 | |
| 686 | id = get_int(); |
| 687 | name = get_string(NULL); |
| 688 | a = get_attrib(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 689 | debug("request %u: setstat name \"%s\"", id, name); |
Damien Miller | 00c9217 | 2002-02-13 14:05:00 +1100 | [diff] [blame] | 690 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 691 | logit("set \"%s\" size %llu", name, a->size); |
Damien Miller | 00c9217 | 2002-02-13 14:05:00 +1100 | [diff] [blame] | 692 | ret = truncate(name, a->size); |
| 693 | if (ret == -1) |
| 694 | status = errno_to_portable(errno); |
| 695 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 696 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 697 | logit("set \"%s\" mode %04o", name, a->perm); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 698 | ret = chmod(name, a->perm & 0777); |
| 699 | if (ret == -1) |
| 700 | status = errno_to_portable(errno); |
| 701 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 702 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 703 | char buf[64]; |
| 704 | time_t t = a->mtime; |
| 705 | |
| 706 | strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S", |
| 707 | localtime(&t)); |
| 708 | logit("set \"%s\" modtime %s", name, buf); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 709 | ret = utimes(name, attrib_to_tv(a)); |
| 710 | if (ret == -1) |
| 711 | status = errno_to_portable(errno); |
| 712 | } |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 713 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 714 | logit("set \"%s\" owner %lu group %lu", name, |
| 715 | (u_long)a->uid, (u_long)a->gid); |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 716 | ret = chown(name, a->uid, a->gid); |
| 717 | if (ret == -1) |
| 718 | status = errno_to_portable(errno); |
| 719 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 720 | send_status(id, status); |
| 721 | xfree(name); |
| 722 | } |
| 723 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 724 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 725 | process_fsetstat(void) |
| 726 | { |
| 727 | Attrib *a; |
| 728 | u_int32_t id; |
| 729 | int handle, fd, ret; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 730 | int status = SSH2_FX_OK; |
Kevin Steves | f7ffab3 | 2001-01-24 20:11:06 +0000 | [diff] [blame] | 731 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 732 | id = get_int(); |
| 733 | handle = get_handle(); |
| 734 | a = get_attrib(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 735 | debug("request %u: fsetstat handle %d", id, handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 736 | fd = handle_to_fd(handle); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 737 | if (fd < 0) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 738 | status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 739 | } else { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 740 | char *name = handle_to_name(handle); |
| 741 | |
Damien Miller | 00c9217 | 2002-02-13 14:05:00 +1100 | [diff] [blame] | 742 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 743 | logit("set \"%s\" size %llu", name, a->size); |
Damien Miller | 00c9217 | 2002-02-13 14:05:00 +1100 | [diff] [blame] | 744 | ret = ftruncate(fd, a->size); |
| 745 | if (ret == -1) |
| 746 | status = errno_to_portable(errno); |
| 747 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 748 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 749 | logit("set \"%s\" mode %04o", name, a->perm); |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 750 | #ifdef HAVE_FCHMOD |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 751 | ret = fchmod(fd, a->perm & 0777); |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 752 | #else |
Kevin Steves | b6b37ba | 2001-01-24 20:01:44 +0000 | [diff] [blame] | 753 | ret = chmod(name, a->perm & 0777); |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 754 | #endif |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 755 | if (ret == -1) |
| 756 | status = errno_to_portable(errno); |
| 757 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 758 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 759 | char buf[64]; |
| 760 | time_t t = a->mtime; |
| 761 | |
| 762 | strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S", |
| 763 | localtime(&t)); |
| 764 | logit("set \"%s\" modtime %s", name, buf); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 765 | #ifdef HAVE_FUTIMES |
| 766 | ret = futimes(fd, attrib_to_tv(a)); |
| 767 | #else |
| 768 | ret = utimes(name, attrib_to_tv(a)); |
| 769 | #endif |
| 770 | if (ret == -1) |
| 771 | status = errno_to_portable(errno); |
| 772 | } |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 773 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 774 | logit("set \"%s\" owner %lu group %lu", name, |
| 775 | (u_long)a->uid, (u_long)a->gid); |
Ben Lindstrom | 34bb0c7 | 2001-02-13 02:40:56 +0000 | [diff] [blame] | 776 | #ifdef HAVE_FCHOWN |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 777 | ret = fchown(fd, a->uid, a->gid); |
Ben Lindstrom | 34bb0c7 | 2001-02-13 02:40:56 +0000 | [diff] [blame] | 778 | #else |
| 779 | ret = chown(name, a->uid, a->gid); |
| 780 | #endif |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 781 | if (ret == -1) |
| 782 | status = errno_to_portable(errno); |
| 783 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 784 | } |
| 785 | send_status(id, status); |
| 786 | } |
| 787 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 788 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 789 | process_opendir(void) |
| 790 | { |
| 791 | DIR *dirp = NULL; |
| 792 | char *path; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 793 | int handle, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 794 | u_int32_t id; |
| 795 | |
| 796 | id = get_int(); |
| 797 | path = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 798 | debug3("request %u: opendir", id); |
| 799 | logit("opendir \"%s\"", path); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 800 | dirp = opendir(path); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 801 | if (dirp == NULL) { |
| 802 | status = errno_to_portable(errno); |
| 803 | } else { |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 804 | handle = handle_new(HANDLE_DIR, path, 0, dirp); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 805 | if (handle < 0) { |
| 806 | closedir(dirp); |
| 807 | } else { |
| 808 | send_handle(id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 809 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 810 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 811 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 812 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 813 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 814 | send_status(id, status); |
| 815 | xfree(path); |
| 816 | } |
| 817 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 818 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 819 | process_readdir(void) |
| 820 | { |
| 821 | DIR *dirp; |
| 822 | struct dirent *dp; |
| 823 | char *path; |
| 824 | int handle; |
| 825 | u_int32_t id; |
| 826 | |
| 827 | id = get_int(); |
| 828 | handle = get_handle(); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 829 | debug("request %u: readdir \"%s\" (handle %d)", id, |
| 830 | handle_to_name(handle), handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 831 | dirp = handle_to_dir(handle); |
| 832 | path = handle_to_name(handle); |
| 833 | if (dirp == NULL || path == NULL) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 834 | send_status(id, SSH2_FX_FAILURE); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 835 | } else { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 836 | struct stat st; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 837 | char pathname[MAXPATHLEN]; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 838 | Stat *stats; |
| 839 | int nstats = 10, count = 0, i; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 840 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 841 | stats = xcalloc(nstats, sizeof(Stat)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 842 | while ((dp = readdir(dirp)) != NULL) { |
| 843 | if (count >= nstats) { |
| 844 | nstats *= 2; |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 845 | stats = xrealloc(stats, nstats, sizeof(Stat)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 846 | } |
| 847 | /* XXX OVERFLOW ? */ |
Ben Lindstrom | 95148e3 | 2001-08-06 21:30:53 +0000 | [diff] [blame] | 848 | snprintf(pathname, sizeof pathname, "%s%s%s", path, |
| 849 | strcmp(path, "/") ? "/" : "", dp->d_name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 850 | if (lstat(pathname, &st) < 0) |
| 851 | continue; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 852 | stat_to_attrib(&st, &(stats[count].attrib)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 853 | stats[count].name = xstrdup(dp->d_name); |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 854 | stats[count].long_name = ls_file(dp->d_name, &st, 0); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 855 | count++; |
| 856 | /* send up to 100 entries in one message */ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 857 | /* XXX check packet size instead */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 858 | if (count == 100) |
| 859 | break; |
| 860 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 861 | if (count > 0) { |
| 862 | send_names(id, count, stats); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 863 | for (i = 0; i < count; i++) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 864 | xfree(stats[i].name); |
| 865 | xfree(stats[i].long_name); |
| 866 | } |
| 867 | } else { |
| 868 | send_status(id, SSH2_FX_EOF); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 869 | } |
| 870 | xfree(stats); |
| 871 | } |
| 872 | } |
| 873 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 874 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 875 | process_remove(void) |
| 876 | { |
| 877 | char *name; |
| 878 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 879 | int status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 880 | int ret; |
| 881 | |
| 882 | id = get_int(); |
| 883 | name = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 884 | debug3("request %u: remove", id); |
| 885 | logit("remove name \"%s\"", name); |
Kevin Steves | a074feb | 2000-12-21 22:33:45 +0000 | [diff] [blame] | 886 | ret = unlink(name); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 887 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 888 | send_status(id, status); |
| 889 | xfree(name); |
| 890 | } |
| 891 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 892 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 893 | process_mkdir(void) |
| 894 | { |
| 895 | Attrib *a; |
| 896 | u_int32_t id; |
| 897 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 898 | int ret, mode, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 899 | |
| 900 | id = get_int(); |
| 901 | name = get_string(NULL); |
| 902 | a = get_attrib(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 903 | mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? |
| 904 | a->perm & 0777 : 0777; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 905 | debug3("request %u: mkdir", id); |
| 906 | logit("mkdir name \"%s\" mode 0%o", name, mode); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 907 | ret = mkdir(name, mode); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 908 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 909 | send_status(id, status); |
| 910 | xfree(name); |
| 911 | } |
| 912 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 913 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 914 | process_rmdir(void) |
| 915 | { |
| 916 | u_int32_t id; |
| 917 | char *name; |
| 918 | int ret, status; |
| 919 | |
| 920 | id = get_int(); |
| 921 | name = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 922 | debug3("request %u: rmdir", id); |
| 923 | logit("rmdir name \"%s\"", name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 924 | ret = rmdir(name); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 925 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 926 | send_status(id, status); |
| 927 | xfree(name); |
| 928 | } |
| 929 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 930 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 931 | process_realpath(void) |
| 932 | { |
| 933 | char resolvedname[MAXPATHLEN]; |
| 934 | u_int32_t id; |
| 935 | char *path; |
| 936 | |
| 937 | id = get_int(); |
| 938 | path = get_string(NULL); |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 939 | if (path[0] == '\0') { |
| 940 | xfree(path); |
| 941 | path = xstrdup("."); |
| 942 | } |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 943 | debug3("request %u: realpath", id); |
| 944 | verbose("realpath \"%s\"", path); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 945 | if (realpath(path, resolvedname) == NULL) { |
| 946 | send_status(id, errno_to_portable(errno)); |
| 947 | } else { |
| 948 | Stat s; |
| 949 | attrib_clear(&s.attrib); |
| 950 | s.name = s.long_name = resolvedname; |
| 951 | send_names(id, 1, &s); |
| 952 | } |
| 953 | xfree(path); |
| 954 | } |
| 955 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 956 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 957 | process_rename(void) |
| 958 | { |
| 959 | u_int32_t id; |
| 960 | char *oldpath, *newpath; |
Damien Miller | 9e51a73 | 2003-02-24 11:58:44 +1100 | [diff] [blame] | 961 | int status; |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 962 | struct stat sb; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 963 | |
| 964 | id = get_int(); |
| 965 | oldpath = get_string(NULL); |
| 966 | newpath = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 967 | debug3("request %u: rename", id); |
| 968 | logit("rename old \"%s\" new \"%s\"", oldpath, newpath); |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 969 | status = SSH2_FX_FAILURE; |
| 970 | if (lstat(oldpath, &sb) == -1) |
Damien Miller | 9e51a73 | 2003-02-24 11:58:44 +1100 | [diff] [blame] | 971 | status = errno_to_portable(errno); |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 972 | else if (S_ISREG(sb.st_mode)) { |
| 973 | /* Race-free rename of regular files */ |
Darren Tucker | aedc1d6 | 2004-06-25 17:06:02 +1000 | [diff] [blame] | 974 | if (link(oldpath, newpath) == -1) { |
Darren Tucker | e59b508 | 2004-06-28 16:01:19 +1000 | [diff] [blame] | 975 | if (errno == EOPNOTSUPP |
| 976 | #ifdef LINK_OPNOTSUPP_ERRNO |
| 977 | || errno == LINK_OPNOTSUPP_ERRNO |
| 978 | #endif |
| 979 | ) { |
Darren Tucker | aedc1d6 | 2004-06-25 17:06:02 +1000 | [diff] [blame] | 980 | struct stat st; |
| 981 | |
| 982 | /* |
| 983 | * fs doesn't support links, so fall back to |
| 984 | * stat+rename. This is racy. |
| 985 | */ |
| 986 | if (stat(newpath, &st) == -1) { |
| 987 | if (rename(oldpath, newpath) == -1) |
| 988 | status = |
| 989 | errno_to_portable(errno); |
| 990 | else |
| 991 | status = SSH2_FX_OK; |
| 992 | } |
| 993 | } else { |
| 994 | status = errno_to_portable(errno); |
| 995 | } |
| 996 | } else if (unlink(oldpath) == -1) { |
Damien Miller | b3207e8 | 2003-03-26 16:01:11 +1100 | [diff] [blame] | 997 | status = errno_to_portable(errno); |
| 998 | /* clean spare link */ |
| 999 | unlink(newpath); |
| 1000 | } else |
| 1001 | status = SSH2_FX_OK; |
| 1002 | } else if (stat(newpath, &sb) == -1) { |
| 1003 | if (rename(oldpath, newpath) == -1) |
| 1004 | status = errno_to_portable(errno); |
| 1005 | else |
| 1006 | status = SSH2_FX_OK; |
| 1007 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1008 | send_status(id, status); |
| 1009 | xfree(oldpath); |
| 1010 | xfree(newpath); |
| 1011 | } |
| 1012 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1013 | static void |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1014 | process_readlink(void) |
| 1015 | { |
| 1016 | u_int32_t id; |
Ben Lindstrom | abbb73d | 2001-05-17 03:14:57 +0000 | [diff] [blame] | 1017 | int len; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1018 | char buf[MAXPATHLEN]; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1019 | char *path; |
| 1020 | |
| 1021 | id = get_int(); |
| 1022 | path = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1023 | debug3("request %u: readlink", id); |
| 1024 | verbose("readlink \"%s\"", path); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1025 | if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1026 | send_status(id, errno_to_portable(errno)); |
| 1027 | else { |
| 1028 | Stat s; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1029 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1030 | buf[len] = '\0'; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1031 | attrib_clear(&s.attrib); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1032 | s.name = s.long_name = buf; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1033 | send_names(id, 1, &s); |
| 1034 | } |
| 1035 | xfree(path); |
| 1036 | } |
| 1037 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1038 | static void |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1039 | process_symlink(void) |
| 1040 | { |
| 1041 | u_int32_t id; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1042 | char *oldpath, *newpath; |
Damien Miller | 9e51a73 | 2003-02-24 11:58:44 +1100 | [diff] [blame] | 1043 | int ret, status; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1044 | |
| 1045 | id = get_int(); |
| 1046 | oldpath = get_string(NULL); |
| 1047 | newpath = get_string(NULL); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1048 | debug3("request %u: symlink", id); |
| 1049 | logit("symlink old \"%s\" new \"%s\"", oldpath, newpath); |
Damien Miller | 9e51a73 | 2003-02-24 11:58:44 +1100 | [diff] [blame] | 1050 | /* this will fail if 'newpath' exists */ |
| 1051 | ret = symlink(oldpath, newpath); |
| 1052 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1053 | send_status(id, status); |
| 1054 | xfree(oldpath); |
| 1055 | xfree(newpath); |
| 1056 | } |
| 1057 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1058 | static void |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1059 | process_extended(void) |
| 1060 | { |
| 1061 | u_int32_t id; |
| 1062 | char *request; |
| 1063 | |
| 1064 | id = get_int(); |
| 1065 | request = get_string(NULL); |
| 1066 | send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */ |
| 1067 | xfree(request); |
| 1068 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1069 | |
| 1070 | /* stolen from ssh-agent */ |
| 1071 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1072 | static void |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1073 | process(void) |
| 1074 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1075 | u_int msg_len; |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1076 | u_int buf_len; |
| 1077 | u_int consumed; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1078 | u_int type; |
| 1079 | u_char *cp; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1080 | |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1081 | buf_len = buffer_len(&iqueue); |
| 1082 | if (buf_len < 5) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1083 | return; /* Incomplete message. */ |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 1084 | cp = buffer_ptr(&iqueue); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 1085 | msg_len = get_u32(cp); |
Damien Miller | 5444618 | 2006-01-02 23:40:50 +1100 | [diff] [blame] | 1086 | if (msg_len > SFTP_MAX_MSG_LENGTH) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1087 | error("bad message from %s local user %s", |
| 1088 | client_addr, pw->pw_name); |
| 1089 | cleanup_exit(11); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1090 | } |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1091 | if (buf_len < msg_len + 4) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1092 | return; |
| 1093 | buffer_consume(&iqueue, 4); |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1094 | buf_len -= 4; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1095 | type = buffer_get_char(&iqueue); |
| 1096 | switch (type) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1097 | case SSH2_FXP_INIT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1098 | process_init(); |
| 1099 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1100 | case SSH2_FXP_OPEN: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1101 | process_open(); |
| 1102 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1103 | case SSH2_FXP_CLOSE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1104 | process_close(); |
| 1105 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1106 | case SSH2_FXP_READ: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1107 | process_read(); |
| 1108 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1109 | case SSH2_FXP_WRITE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1110 | process_write(); |
| 1111 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1112 | case SSH2_FXP_LSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1113 | process_lstat(); |
| 1114 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1115 | case SSH2_FXP_FSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1116 | process_fstat(); |
| 1117 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1118 | case SSH2_FXP_SETSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1119 | process_setstat(); |
| 1120 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1121 | case SSH2_FXP_FSETSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1122 | process_fsetstat(); |
| 1123 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1124 | case SSH2_FXP_OPENDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1125 | process_opendir(); |
| 1126 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1127 | case SSH2_FXP_READDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1128 | process_readdir(); |
| 1129 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1130 | case SSH2_FXP_REMOVE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1131 | process_remove(); |
| 1132 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1133 | case SSH2_FXP_MKDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1134 | process_mkdir(); |
| 1135 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1136 | case SSH2_FXP_RMDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1137 | process_rmdir(); |
| 1138 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1139 | case SSH2_FXP_REALPATH: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1140 | process_realpath(); |
| 1141 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1142 | case SSH2_FXP_STAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1143 | process_stat(); |
| 1144 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1145 | case SSH2_FXP_RENAME: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1146 | process_rename(); |
| 1147 | break; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1148 | case SSH2_FXP_READLINK: |
| 1149 | process_readlink(); |
| 1150 | break; |
| 1151 | case SSH2_FXP_SYMLINK: |
| 1152 | process_symlink(); |
| 1153 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 1154 | case SSH2_FXP_EXTENDED: |
| 1155 | process_extended(); |
| 1156 | break; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1157 | default: |
| 1158 | error("Unknown message %d", type); |
| 1159 | break; |
| 1160 | } |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1161 | /* discard the remaining bytes from the current packet */ |
| 1162 | if (buf_len < buffer_len(&iqueue)) |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1163 | fatal("iqueue grew unexpectedly"); |
Ben Lindstrom | 2c14047 | 2002-06-06 21:57:54 +0000 | [diff] [blame] | 1164 | consumed = buf_len - buffer_len(&iqueue); |
| 1165 | if (msg_len < consumed) |
| 1166 | fatal("msg_len %d < consumed %d", msg_len, consumed); |
| 1167 | if (msg_len > consumed) |
| 1168 | buffer_consume(&iqueue, msg_len - consumed); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1169 | } |
| 1170 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1171 | /* Cleanup handler that logs active handles upon normal exit */ |
| 1172 | void |
| 1173 | cleanup_exit(int i) |
| 1174 | { |
| 1175 | if (pw != NULL && client_addr != NULL) { |
| 1176 | handle_log_exit(); |
| 1177 | logit("session closed for local user %s from [%s]", |
| 1178 | pw->pw_name, client_addr); |
| 1179 | } |
| 1180 | _exit(i); |
| 1181 | } |
| 1182 | |
| 1183 | static void |
| 1184 | usage(void) |
| 1185 | { |
| 1186 | extern char *__progname; |
| 1187 | |
| 1188 | fprintf(stderr, |
| 1189 | "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname); |
| 1190 | exit(1); |
| 1191 | } |
| 1192 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1193 | int |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1194 | main(int argc, char **argv) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1195 | { |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1196 | fd_set *rset, *wset; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1197 | int in, out, max, ch, skipargs = 0, log_stderr = 0; |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1198 | ssize_t len, olen, set_size; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1199 | SyslogFacility log_facility = SYSLOG_FACILITY_AUTH; |
| 1200 | char *cp; |
| 1201 | |
| 1202 | extern int optind; |
| 1203 | extern char *optarg; |
| 1204 | extern char *__progname; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1205 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1206 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 1207 | sanitise_stdfd(); |
| 1208 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1209 | __progname = ssh_get_progname(argv[0]); |
| 1210 | log_init(__progname, log_level, log_facility, log_stderr); |
Ben Lindstrom | c7f4ccd | 2001-03-15 00:09:15 +0000 | [diff] [blame] | 1211 | |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1212 | while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) { |
| 1213 | switch (ch) { |
| 1214 | case 'c': |
| 1215 | /* |
| 1216 | * Ignore all arguments if we are invoked as a |
| 1217 | * shell using "sftp-server -c command" |
| 1218 | */ |
| 1219 | skipargs = 1; |
| 1220 | break; |
| 1221 | case 'e': |
| 1222 | log_stderr = 1; |
| 1223 | break; |
| 1224 | case 'l': |
| 1225 | log_level = log_level_number(optarg); |
| 1226 | if (log_level == SYSLOG_LEVEL_NOT_SET) |
| 1227 | error("Invalid log level \"%s\"", optarg); |
| 1228 | break; |
| 1229 | case 'f': |
| 1230 | log_facility = log_facility_number(optarg); |
| 1231 | if (log_level == SYSLOG_FACILITY_NOT_SET) |
| 1232 | error("Invalid log facility \"%s\"", optarg); |
| 1233 | break; |
| 1234 | case 'h': |
| 1235 | default: |
| 1236 | usage(); |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | log_init(__progname, log_level, log_facility, log_stderr); |
| 1241 | |
| 1242 | if ((cp = getenv("SSH_CONNECTION")) != NULL) { |
| 1243 | client_addr = xstrdup(cp); |
| 1244 | if ((cp = strchr(client_addr, ' ')) == NULL) |
| 1245 | fatal("Malformed SSH_CONNECTION variable: \"%s\"", |
| 1246 | getenv("SSH_CONNECTION")); |
| 1247 | *cp = '\0'; |
| 1248 | } else |
| 1249 | client_addr = xstrdup("UNKNOWN"); |
| 1250 | |
| 1251 | if ((pw = getpwuid(getuid())) == NULL) |
| 1252 | fatal("No user found for uid %lu", (u_long)getuid()); |
| 1253 | pw = pwcopy(pw); |
| 1254 | |
| 1255 | logit("session opened for local user %s from [%s]", |
| 1256 | pw->pw_name, client_addr); |
| 1257 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1258 | handle_init(); |
| 1259 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1260 | in = dup(STDIN_FILENO); |
| 1261 | out = dup(STDOUT_FILENO); |
| 1262 | |
Damien Miller | 402b331 | 2001-04-14 00:28:42 +1000 | [diff] [blame] | 1263 | #ifdef HAVE_CYGWIN |
| 1264 | setmode(in, O_BINARY); |
| 1265 | setmode(out, O_BINARY); |
| 1266 | #endif |
| 1267 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1268 | max = 0; |
| 1269 | if (in > max) |
| 1270 | max = in; |
| 1271 | if (out > max) |
| 1272 | max = out; |
| 1273 | |
| 1274 | buffer_init(&iqueue); |
| 1275 | buffer_init(&oqueue); |
| 1276 | |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1277 | set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask); |
| 1278 | rset = (fd_set *)xmalloc(set_size); |
| 1279 | wset = (fd_set *)xmalloc(set_size); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1280 | |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1281 | for (;;) { |
| 1282 | memset(rset, 0, set_size); |
| 1283 | memset(wset, 0, set_size); |
| 1284 | |
| 1285 | FD_SET(in, rset); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1286 | olen = buffer_len(&oqueue); |
| 1287 | if (olen > 0) |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1288 | FD_SET(out, wset); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1289 | |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1290 | if (select(max+1, rset, wset, NULL, NULL) < 0) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1291 | if (errno == EINTR) |
| 1292 | continue; |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1293 | error("select: %s", strerror(errno)); |
| 1294 | cleanup_exit(2); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /* copy stdin to iqueue */ |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1298 | if (FD_ISSET(in, rset)) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1299 | char buf[4*4096]; |
| 1300 | len = read(in, buf, sizeof buf); |
| 1301 | if (len == 0) { |
| 1302 | debug("read eof"); |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1303 | cleanup_exit(0); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1304 | } else if (len < 0) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1305 | error("read: %s", strerror(errno)); |
| 1306 | cleanup_exit(1); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1307 | } else { |
| 1308 | buffer_append(&iqueue, buf, len); |
| 1309 | } |
| 1310 | } |
| 1311 | /* send oqueue to stdout */ |
Ben Lindstrom | cb80bdf | 2001-03-05 07:06:12 +0000 | [diff] [blame] | 1312 | if (FD_ISSET(out, wset)) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1313 | len = write(out, buffer_ptr(&oqueue), olen); |
| 1314 | if (len < 0) { |
Damien Miller | fef95ad | 2006-07-10 20:46:55 +1000 | [diff] [blame^] | 1315 | error("write: %s", strerror(errno)); |
| 1316 | cleanup_exit(1); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1317 | } else { |
| 1318 | buffer_consume(&oqueue, len); |
| 1319 | } |
| 1320 | } |
| 1321 | /* process requests from client */ |
| 1322 | process(); |
| 1323 | } |
| 1324 | } |