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