Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | #include "includes.h" |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 25 | RCSID("$OpenBSD: sftp-server.c,v 1.18 2001/02/04 22:21:19 stevesk Exp $"); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 26 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 27 | #include "buffer.h" |
| 28 | #include "bufaux.h" |
| 29 | #include "getput.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 30 | #include "log.h" |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 31 | #include "xmalloc.h" |
| 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); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 40 | #define TRACE debug |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 41 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 42 | #ifdef HAVE___PROGNAME |
| 43 | extern char *__progname; |
| 44 | #else |
| 45 | char *__progname; |
| 46 | #endif |
| 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 | |
| 52 | /* portable attibutes, etc. */ |
| 53 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 54 | typedef struct Stat Stat; |
| 55 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 56 | struct Stat { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 57 | char *name; |
| 58 | char *long_name; |
| 59 | Attrib attrib; |
| 60 | }; |
| 61 | |
| 62 | int |
| 63 | errno_to_portable(int unixerrno) |
| 64 | { |
| 65 | int ret = 0; |
| 66 | switch (unixerrno) { |
| 67 | case 0: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 68 | ret = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 69 | break; |
| 70 | case ENOENT: |
| 71 | case ENOTDIR: |
| 72 | case EBADF: |
| 73 | case ELOOP: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 74 | ret = SSH2_FX_NO_SUCH_FILE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 75 | break; |
| 76 | case EPERM: |
| 77 | case EACCES: |
| 78 | case EFAULT: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 79 | ret = SSH2_FX_PERMISSION_DENIED; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 80 | break; |
| 81 | case ENAMETOOLONG: |
| 82 | case EINVAL: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 83 | ret = SSH2_FX_BAD_MESSAGE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 84 | break; |
| 85 | default: |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 86 | ret = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | int |
| 93 | flags_from_portable(int pflags) |
| 94 | { |
| 95 | int flags = 0; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 96 | if (pflags & SSH2_FXF_READ && |
| 97 | pflags & SSH2_FXF_WRITE) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 98 | flags = O_RDWR; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 99 | } else if (pflags & SSH2_FXF_READ) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 100 | flags = O_RDONLY; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 101 | } else if (pflags & SSH2_FXF_WRITE) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 102 | flags = O_WRONLY; |
| 103 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 104 | if (pflags & SSH2_FXF_CREAT) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 105 | flags |= O_CREAT; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 106 | if (pflags & SSH2_FXF_TRUNC) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 107 | flags |= O_TRUNC; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 108 | if (pflags & SSH2_FXF_EXCL) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 109 | flags |= O_EXCL; |
| 110 | return flags; |
| 111 | } |
| 112 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 113 | Attrib * |
| 114 | get_attrib(void) |
| 115 | { |
| 116 | return decode_attrib(&iqueue); |
| 117 | } |
| 118 | |
| 119 | /* handle handles */ |
| 120 | |
| 121 | typedef struct Handle Handle; |
| 122 | struct Handle { |
| 123 | int use; |
| 124 | DIR *dirp; |
| 125 | int fd; |
| 126 | char *name; |
| 127 | }; |
| 128 | enum { |
| 129 | HANDLE_UNUSED, |
| 130 | HANDLE_DIR, |
| 131 | HANDLE_FILE |
| 132 | }; |
| 133 | Handle handles[100]; |
| 134 | |
| 135 | void |
| 136 | handle_init(void) |
| 137 | { |
| 138 | int i; |
| 139 | for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) |
| 140 | handles[i].use = HANDLE_UNUSED; |
| 141 | } |
| 142 | |
| 143 | int |
| 144 | handle_new(int use, char *name, int fd, DIR *dirp) |
| 145 | { |
| 146 | int i; |
| 147 | for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) { |
| 148 | if (handles[i].use == HANDLE_UNUSED) { |
| 149 | handles[i].use = use; |
| 150 | handles[i].dirp = dirp; |
| 151 | handles[i].fd = fd; |
| 152 | handles[i].name = name; |
| 153 | return i; |
| 154 | } |
| 155 | } |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | int |
| 160 | handle_is_ok(int i, int type) |
| 161 | { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 162 | return i >= 0 && i < sizeof(handles)/sizeof(Handle) && |
| 163 | handles[i].use == type; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | int |
| 167 | handle_to_string(int handle, char **stringp, int *hlenp) |
| 168 | { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 169 | if (stringp == NULL || hlenp == NULL) |
| 170 | return -1; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 171 | *stringp = xmalloc(sizeof(int32_t)); |
| 172 | PUT_32BIT(*stringp, handle); |
| 173 | *hlenp = sizeof(int32_t); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 178 | handle_from_string(char *handle, u_int hlen) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 179 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 180 | int val; |
| 181 | if (hlen != sizeof(int32_t)) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 182 | return -1; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 183 | val = GET_32BIT(handle); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 184 | if (handle_is_ok(val, HANDLE_FILE) || |
| 185 | handle_is_ok(val, HANDLE_DIR)) |
| 186 | return val; |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | char * |
| 191 | handle_to_name(int handle) |
| 192 | { |
| 193 | if (handle_is_ok(handle, HANDLE_DIR)|| |
| 194 | handle_is_ok(handle, HANDLE_FILE)) |
| 195 | return handles[handle].name; |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | DIR * |
| 200 | handle_to_dir(int handle) |
| 201 | { |
| 202 | if (handle_is_ok(handle, HANDLE_DIR)) |
| 203 | return handles[handle].dirp; |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | int |
| 208 | handle_to_fd(int handle) |
| 209 | { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 210 | if (handle_is_ok(handle, HANDLE_FILE)) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 211 | return handles[handle].fd; |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | int |
| 216 | handle_close(int handle) |
| 217 | { |
| 218 | int ret = -1; |
| 219 | if (handle_is_ok(handle, HANDLE_FILE)) { |
| 220 | ret = close(handles[handle].fd); |
| 221 | handles[handle].use = HANDLE_UNUSED; |
| 222 | } else if (handle_is_ok(handle, HANDLE_DIR)) { |
| 223 | ret = closedir(handles[handle].dirp); |
| 224 | handles[handle].use = HANDLE_UNUSED; |
| 225 | } else { |
| 226 | errno = ENOENT; |
| 227 | } |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | int |
| 232 | get_handle(void) |
| 233 | { |
| 234 | char *handle; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 235 | int val = -1; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 236 | u_int hlen; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 237 | handle = get_string(&hlen); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 238 | if (hlen < 256) |
| 239 | val = handle_from_string(handle, hlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 240 | xfree(handle); |
| 241 | return val; |
| 242 | } |
| 243 | |
| 244 | /* send replies */ |
| 245 | |
| 246 | void |
| 247 | send_msg(Buffer *m) |
| 248 | { |
| 249 | int mlen = buffer_len(m); |
| 250 | buffer_put_int(&oqueue, mlen); |
| 251 | buffer_append(&oqueue, buffer_ptr(m), mlen); |
| 252 | buffer_consume(m, mlen); |
| 253 | } |
| 254 | |
| 255 | void |
| 256 | send_status(u_int32_t id, u_int32_t error) |
| 257 | { |
| 258 | Buffer msg; |
| 259 | TRACE("sent status id %d error %d", id, error); |
| 260 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 261 | buffer_put_char(&msg, SSH2_FXP_STATUS); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 262 | buffer_put_int(&msg, id); |
| 263 | buffer_put_int(&msg, error); |
| 264 | send_msg(&msg); |
| 265 | buffer_free(&msg); |
| 266 | } |
| 267 | void |
| 268 | send_data_or_handle(char type, u_int32_t id, char *data, int dlen) |
| 269 | { |
| 270 | Buffer msg; |
| 271 | buffer_init(&msg); |
| 272 | buffer_put_char(&msg, type); |
| 273 | buffer_put_int(&msg, id); |
| 274 | buffer_put_string(&msg, data, dlen); |
| 275 | send_msg(&msg); |
| 276 | buffer_free(&msg); |
| 277 | } |
| 278 | |
| 279 | void |
| 280 | send_data(u_int32_t id, char *data, int dlen) |
| 281 | { |
| 282 | TRACE("sent data id %d len %d", id, dlen); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 283 | send_data_or_handle(SSH2_FXP_DATA, id, data, dlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void |
| 287 | send_handle(u_int32_t id, int handle) |
| 288 | { |
| 289 | char *string; |
| 290 | int hlen; |
| 291 | handle_to_string(handle, &string, &hlen); |
| 292 | TRACE("sent handle id %d handle %d", id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 293 | send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 294 | xfree(string); |
| 295 | } |
| 296 | |
| 297 | void |
| 298 | send_names(u_int32_t id, int count, Stat *stats) |
| 299 | { |
| 300 | Buffer msg; |
| 301 | int i; |
| 302 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 303 | buffer_put_char(&msg, SSH2_FXP_NAME); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 304 | buffer_put_int(&msg, id); |
| 305 | buffer_put_int(&msg, count); |
| 306 | TRACE("sent names id %d count %d", id, count); |
| 307 | for (i = 0; i < count; i++) { |
| 308 | buffer_put_cstring(&msg, stats[i].name); |
| 309 | buffer_put_cstring(&msg, stats[i].long_name); |
| 310 | encode_attrib(&msg, &stats[i].attrib); |
| 311 | } |
| 312 | send_msg(&msg); |
| 313 | buffer_free(&msg); |
| 314 | } |
| 315 | |
| 316 | void |
| 317 | send_attrib(u_int32_t id, Attrib *a) |
| 318 | { |
| 319 | Buffer msg; |
| 320 | TRACE("sent attrib id %d have 0x%x", id, a->flags); |
| 321 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 322 | buffer_put_char(&msg, SSH2_FXP_ATTRS); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 323 | buffer_put_int(&msg, id); |
| 324 | encode_attrib(&msg, a); |
| 325 | send_msg(&msg); |
| 326 | buffer_free(&msg); |
| 327 | } |
| 328 | |
| 329 | /* parse incoming */ |
| 330 | |
| 331 | void |
| 332 | process_init(void) |
| 333 | { |
| 334 | Buffer msg; |
| 335 | int version = buffer_get_int(&iqueue); |
| 336 | |
| 337 | TRACE("client version %d", version); |
| 338 | buffer_init(&msg); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 339 | buffer_put_char(&msg, SSH2_FXP_VERSION); |
| 340 | buffer_put_int(&msg, SSH2_FILEXFER_VERSION); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 341 | send_msg(&msg); |
| 342 | buffer_free(&msg); |
| 343 | } |
| 344 | |
| 345 | void |
| 346 | process_open(void) |
| 347 | { |
| 348 | u_int32_t id, pflags; |
| 349 | Attrib *a; |
| 350 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 351 | int handle, fd, flags, mode, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 352 | |
| 353 | id = get_int(); |
| 354 | name = get_string(NULL); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 355 | pflags = get_int(); /* portable flags */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 356 | a = get_attrib(); |
| 357 | flags = flags_from_portable(pflags); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 358 | mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 359 | TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode); |
| 360 | fd = open(name, flags, mode); |
| 361 | if (fd < 0) { |
| 362 | status = errno_to_portable(errno); |
| 363 | } else { |
| 364 | handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL); |
| 365 | if (handle < 0) { |
| 366 | close(fd); |
| 367 | } else { |
| 368 | send_handle(id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 369 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 370 | } |
| 371 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 372 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 373 | send_status(id, status); |
| 374 | xfree(name); |
| 375 | } |
| 376 | |
| 377 | void |
| 378 | process_close(void) |
| 379 | { |
| 380 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 381 | int handle, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 382 | |
| 383 | id = get_int(); |
| 384 | handle = get_handle(); |
| 385 | TRACE("close id %d handle %d", id, handle); |
| 386 | ret = handle_close(handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 387 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 388 | send_status(id, status); |
| 389 | } |
| 390 | |
| 391 | void |
| 392 | process_read(void) |
| 393 | { |
| 394 | char buf[64*1024]; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 395 | u_int32_t id, len; |
| 396 | int handle, fd, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 397 | u_int64_t off; |
| 398 | |
| 399 | id = get_int(); |
| 400 | handle = get_handle(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 401 | off = get_int64(); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 402 | len = get_int(); |
| 403 | |
Damien Miller | f056e23 | 2000-09-23 14:58:32 +1100 | [diff] [blame] | 404 | TRACE("read id %d handle %d off %lld len %d", id, handle, off, len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 405 | if (len > sizeof buf) { |
| 406 | len = sizeof buf; |
| 407 | log("read change len %d", len); |
| 408 | } |
| 409 | fd = handle_to_fd(handle); |
| 410 | if (fd >= 0) { |
| 411 | if (lseek(fd, off, SEEK_SET) < 0) { |
| 412 | error("process_read: seek failed"); |
| 413 | status = errno_to_portable(errno); |
| 414 | } else { |
| 415 | ret = read(fd, buf, len); |
| 416 | if (ret < 0) { |
| 417 | status = errno_to_portable(errno); |
| 418 | } else if (ret == 0) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 419 | status = SSH2_FX_EOF; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 420 | } else { |
| 421 | send_data(id, buf, ret); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 422 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 426 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 427 | send_status(id, status); |
| 428 | } |
| 429 | |
| 430 | void |
| 431 | process_write(void) |
| 432 | { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 433 | u_int32_t id; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 434 | u_int64_t off; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 435 | u_int len; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 436 | int handle, fd, ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 437 | char *data; |
| 438 | |
| 439 | id = get_int(); |
| 440 | handle = get_handle(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 441 | off = get_int64(); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 442 | data = get_string(&len); |
| 443 | |
Damien Miller | f056e23 | 2000-09-23 14:58:32 +1100 | [diff] [blame] | 444 | TRACE("write id %d handle %d off %lld len %d", id, handle, off, len); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 445 | fd = handle_to_fd(handle); |
| 446 | if (fd >= 0) { |
| 447 | if (lseek(fd, off, SEEK_SET) < 0) { |
| 448 | status = errno_to_portable(errno); |
| 449 | error("process_write: seek failed"); |
| 450 | } else { |
| 451 | /* XXX ATOMICIO ? */ |
| 452 | ret = write(fd, data, len); |
| 453 | if (ret == -1) { |
| 454 | error("process_write: write failed"); |
| 455 | status = errno_to_portable(errno); |
| 456 | } else if (ret == len) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 457 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 458 | } else { |
| 459 | log("nothing at all written"); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | send_status(id, status); |
| 464 | xfree(data); |
| 465 | } |
| 466 | |
| 467 | void |
| 468 | process_do_stat(int do_lstat) |
| 469 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 470 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 471 | struct stat st; |
| 472 | u_int32_t id; |
| 473 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 474 | int ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 475 | |
| 476 | id = get_int(); |
| 477 | name = get_string(NULL); |
| 478 | TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name); |
| 479 | ret = do_lstat ? lstat(name, &st) : stat(name, &st); |
| 480 | if (ret < 0) { |
| 481 | status = errno_to_portable(errno); |
| 482 | } else { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 483 | stat_to_attrib(&st, &a); |
| 484 | send_attrib(id, &a); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 485 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 486 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 487 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 488 | send_status(id, status); |
| 489 | xfree(name); |
| 490 | } |
| 491 | |
| 492 | void |
| 493 | process_stat(void) |
| 494 | { |
| 495 | process_do_stat(0); |
| 496 | } |
| 497 | |
| 498 | void |
| 499 | process_lstat(void) |
| 500 | { |
| 501 | process_do_stat(1); |
| 502 | } |
| 503 | |
| 504 | void |
| 505 | process_fstat(void) |
| 506 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 507 | Attrib a; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 508 | struct stat st; |
| 509 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 510 | int fd, ret, handle, 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(); |
| 514 | TRACE("fstat id %d handle %d", id, handle); |
| 515 | fd = handle_to_fd(handle); |
| 516 | if (fd >= 0) { |
| 517 | ret = fstat(fd, &st); |
| 518 | if (ret < 0) { |
| 519 | status = errno_to_portable(errno); |
| 520 | } else { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 521 | stat_to_attrib(&st, &a); |
| 522 | send_attrib(id, &a); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 523 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 524 | } |
| 525 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 526 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 527 | send_status(id, status); |
| 528 | } |
| 529 | |
| 530 | struct timeval * |
| 531 | attrib_to_tv(Attrib *a) |
| 532 | { |
| 533 | static struct timeval tv[2]; |
| 534 | tv[0].tv_sec = a->atime; |
| 535 | tv[0].tv_usec = 0; |
| 536 | tv[1].tv_sec = a->mtime; |
| 537 | tv[1].tv_usec = 0; |
| 538 | return tv; |
| 539 | } |
| 540 | |
| 541 | void |
| 542 | process_setstat(void) |
| 543 | { |
| 544 | Attrib *a; |
| 545 | u_int32_t id; |
| 546 | char *name; |
| 547 | int ret; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 548 | int status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 549 | |
| 550 | id = get_int(); |
| 551 | name = get_string(NULL); |
| 552 | a = get_attrib(); |
| 553 | TRACE("setstat id %d name %s", id, name); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 554 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 555 | ret = chmod(name, a->perm & 0777); |
| 556 | if (ret == -1) |
| 557 | status = errno_to_portable(errno); |
| 558 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 559 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 560 | ret = utimes(name, attrib_to_tv(a)); |
| 561 | if (ret == -1) |
| 562 | status = errno_to_portable(errno); |
| 563 | } |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 564 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
| 565 | ret = chown(name, a->uid, a->gid); |
| 566 | if (ret == -1) |
| 567 | status = errno_to_portable(errno); |
| 568 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 569 | send_status(id, status); |
| 570 | xfree(name); |
| 571 | } |
| 572 | |
| 573 | void |
| 574 | process_fsetstat(void) |
| 575 | { |
| 576 | Attrib *a; |
| 577 | u_int32_t id; |
| 578 | int handle, fd, ret; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 579 | int status = SSH2_FX_OK; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 580 | char *name; |
Kevin Steves | f7ffab3 | 2001-01-24 20:11:06 +0000 | [diff] [blame] | 581 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 582 | id = get_int(); |
| 583 | handle = get_handle(); |
| 584 | a = get_attrib(); |
| 585 | TRACE("fsetstat id %d handle %d", id, handle); |
| 586 | fd = handle_to_fd(handle); |
| 587 | name = handle_to_name(handle); |
Kevin Steves | f7ffab3 | 2001-01-24 20:11:06 +0000 | [diff] [blame] | 588 | if (fd < 0 || name == NULL) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 589 | status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 590 | } else { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 591 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 592 | #ifdef HAVE_FCHMOD |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 593 | ret = fchmod(fd, a->perm & 0777); |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 594 | #else |
Kevin Steves | b6b37ba | 2001-01-24 20:01:44 +0000 | [diff] [blame] | 595 | ret = chmod(name, a->perm & 0777); |
Ben Lindstrom | 200e3c9 | 2001-01-15 01:56:46 +0000 | [diff] [blame] | 596 | #endif |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 597 | if (ret == -1) |
| 598 | status = errno_to_portable(errno); |
| 599 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 600 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 601 | #ifdef HAVE_FUTIMES |
| 602 | ret = futimes(fd, attrib_to_tv(a)); |
| 603 | #else |
| 604 | ret = utimes(name, attrib_to_tv(a)); |
| 605 | #endif |
| 606 | if (ret == -1) |
| 607 | status = errno_to_portable(errno); |
| 608 | } |
Kevin Steves | 8e74393 | 2001-02-05 13:24:35 +0000 | [diff] [blame] | 609 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
| 610 | ret = fchown(fd, a->uid, a->gid); |
| 611 | if (ret == -1) |
| 612 | status = errno_to_portable(errno); |
| 613 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 614 | } |
| 615 | send_status(id, status); |
| 616 | } |
| 617 | |
| 618 | void |
| 619 | process_opendir(void) |
| 620 | { |
| 621 | DIR *dirp = NULL; |
| 622 | char *path; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 623 | int handle, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 624 | u_int32_t id; |
| 625 | |
| 626 | id = get_int(); |
| 627 | path = get_string(NULL); |
| 628 | TRACE("opendir id %d path %s", id, path); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 629 | dirp = opendir(path); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 630 | if (dirp == NULL) { |
| 631 | status = errno_to_portable(errno); |
| 632 | } else { |
| 633 | handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp); |
| 634 | if (handle < 0) { |
| 635 | closedir(dirp); |
| 636 | } else { |
| 637 | send_handle(id, handle); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 638 | status = SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 639 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 640 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 641 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 642 | if (status != SSH2_FX_OK) |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 643 | send_status(id, status); |
| 644 | xfree(path); |
| 645 | } |
| 646 | |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 647 | /* |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 648 | * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 649 | */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 650 | char * |
| 651 | ls_file(char *name, struct stat *st) |
| 652 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 653 | int sz = 0; |
| 654 | struct passwd *pw; |
| 655 | struct group *gr; |
| 656 | struct tm *ltime = localtime(&st->st_mtime); |
| 657 | char *user, *group; |
| 658 | char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; |
| 659 | |
| 660 | strmode(st->st_mode, mode); |
| 661 | if ((pw = getpwuid(st->st_uid)) != NULL) { |
| 662 | user = pw->pw_name; |
| 663 | } else { |
| 664 | snprintf(ubuf, sizeof ubuf, "%d", st->st_uid); |
| 665 | user = ubuf; |
| 666 | } |
| 667 | if ((gr = getgrgid(st->st_gid)) != NULL) { |
| 668 | group = gr->gr_name; |
| 669 | } else { |
| 670 | snprintf(gbuf, sizeof gbuf, "%d", st->st_gid); |
| 671 | group = gbuf; |
| 672 | } |
| 673 | if (ltime != NULL) { |
| 674 | if (time(NULL) - st->st_mtime < (365*24*60*60)/2) |
| 675 | sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); |
| 676 | else |
| 677 | sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); |
| 678 | } |
| 679 | if (sz == 0) |
| 680 | tbuf[0] = '\0'; |
Kevin Steves | b71eb58 | 2001-01-29 16:57:27 +0000 | [diff] [blame] | 681 | snprintf(buf, sizeof buf, "%s %3d %-8.8s %-8.8s %8lld %s %s", mode, |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 682 | st->st_nlink, user, group, (long long)st->st_size, tbuf, name); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 683 | return xstrdup(buf); |
| 684 | } |
| 685 | |
| 686 | void |
| 687 | process_readdir(void) |
| 688 | { |
| 689 | DIR *dirp; |
| 690 | struct dirent *dp; |
| 691 | char *path; |
| 692 | int handle; |
| 693 | u_int32_t id; |
| 694 | |
| 695 | id = get_int(); |
| 696 | handle = get_handle(); |
| 697 | TRACE("readdir id %d handle %d", id, handle); |
| 698 | dirp = handle_to_dir(handle); |
| 699 | path = handle_to_name(handle); |
| 700 | if (dirp == NULL || path == NULL) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 701 | send_status(id, SSH2_FX_FAILURE); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 702 | } else { |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 703 | struct stat st; |
| 704 | char pathname[1024]; |
| 705 | Stat *stats; |
| 706 | int nstats = 10, count = 0, i; |
| 707 | stats = xmalloc(nstats * sizeof(Stat)); |
| 708 | while ((dp = readdir(dirp)) != NULL) { |
| 709 | if (count >= nstats) { |
| 710 | nstats *= 2; |
| 711 | stats = xrealloc(stats, nstats * sizeof(Stat)); |
| 712 | } |
| 713 | /* XXX OVERFLOW ? */ |
| 714 | snprintf(pathname, sizeof pathname, |
| 715 | "%s/%s", path, dp->d_name); |
| 716 | if (lstat(pathname, &st) < 0) |
| 717 | continue; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 718 | stat_to_attrib(&st, &(stats[count].attrib)); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 719 | stats[count].name = xstrdup(dp->d_name); |
| 720 | stats[count].long_name = ls_file(dp->d_name, &st); |
| 721 | count++; |
| 722 | /* send up to 100 entries in one message */ |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 723 | /* XXX check packet size instead */ |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 724 | if (count == 100) |
| 725 | break; |
| 726 | } |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 727 | if (count > 0) { |
| 728 | send_names(id, count, stats); |
| 729 | for(i = 0; i < count; i++) { |
| 730 | xfree(stats[i].name); |
| 731 | xfree(stats[i].long_name); |
| 732 | } |
| 733 | } else { |
| 734 | send_status(id, SSH2_FX_EOF); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 735 | } |
| 736 | xfree(stats); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | void |
| 741 | process_remove(void) |
| 742 | { |
| 743 | char *name; |
| 744 | u_int32_t id; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 745 | int status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 746 | int ret; |
| 747 | |
| 748 | id = get_int(); |
| 749 | name = get_string(NULL); |
| 750 | TRACE("remove id %d name %s", id, name); |
Kevin Steves | a074feb | 2000-12-21 22:33:45 +0000 | [diff] [blame] | 751 | ret = unlink(name); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 752 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 753 | send_status(id, status); |
| 754 | xfree(name); |
| 755 | } |
| 756 | |
| 757 | void |
| 758 | process_mkdir(void) |
| 759 | { |
| 760 | Attrib *a; |
| 761 | u_int32_t id; |
| 762 | char *name; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 763 | int ret, mode, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 764 | |
| 765 | id = get_int(); |
| 766 | name = get_string(NULL); |
| 767 | a = get_attrib(); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 768 | mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? |
| 769 | a->perm & 0777 : 0777; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 770 | TRACE("mkdir id %d name %s mode 0%o", id, name, mode); |
| 771 | ret = mkdir(name, mode); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 772 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 773 | send_status(id, status); |
| 774 | xfree(name); |
| 775 | } |
| 776 | |
| 777 | void |
| 778 | process_rmdir(void) |
| 779 | { |
| 780 | u_int32_t id; |
| 781 | char *name; |
| 782 | int ret, status; |
| 783 | |
| 784 | id = get_int(); |
| 785 | name = get_string(NULL); |
| 786 | TRACE("rmdir id %d name %s", id, name); |
| 787 | ret = rmdir(name); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 788 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 789 | send_status(id, status); |
| 790 | xfree(name); |
| 791 | } |
| 792 | |
| 793 | void |
| 794 | process_realpath(void) |
| 795 | { |
| 796 | char resolvedname[MAXPATHLEN]; |
| 797 | u_int32_t id; |
| 798 | char *path; |
| 799 | |
| 800 | id = get_int(); |
| 801 | path = get_string(NULL); |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 802 | if (path[0] == '\0') { |
| 803 | xfree(path); |
| 804 | path = xstrdup("."); |
| 805 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 806 | TRACE("realpath id %d path %s", id, path); |
| 807 | if (realpath(path, resolvedname) == NULL) { |
| 808 | send_status(id, errno_to_portable(errno)); |
| 809 | } else { |
| 810 | Stat s; |
| 811 | attrib_clear(&s.attrib); |
| 812 | s.name = s.long_name = resolvedname; |
| 813 | send_names(id, 1, &s); |
| 814 | } |
| 815 | xfree(path); |
| 816 | } |
| 817 | |
| 818 | void |
| 819 | process_rename(void) |
| 820 | { |
| 821 | u_int32_t id; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 822 | struct stat st; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 823 | char *oldpath, *newpath; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 824 | int ret, status = SSH2_FX_FAILURE; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 825 | |
| 826 | id = get_int(); |
| 827 | oldpath = get_string(NULL); |
| 828 | newpath = get_string(NULL); |
| 829 | TRACE("rename id %d old %s new %s", id, oldpath, newpath); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 830 | /* fail if 'newpath' exists */ |
| 831 | if (stat(newpath, &st) == -1) { |
| 832 | ret = rename(oldpath, newpath); |
| 833 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
| 834 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 835 | send_status(id, status); |
| 836 | xfree(oldpath); |
| 837 | xfree(newpath); |
| 838 | } |
| 839 | |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 840 | void |
| 841 | process_extended(void) |
| 842 | { |
| 843 | u_int32_t id; |
| 844 | char *request; |
| 845 | |
| 846 | id = get_int(); |
| 847 | request = get_string(NULL); |
| 848 | send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */ |
| 849 | xfree(request); |
| 850 | } |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 851 | |
| 852 | /* stolen from ssh-agent */ |
| 853 | |
| 854 | void |
| 855 | process(void) |
| 856 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 857 | u_int msg_len; |
| 858 | u_int type; |
| 859 | u_char *cp; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 860 | |
| 861 | if (buffer_len(&iqueue) < 5) |
| 862 | return; /* Incomplete message. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 863 | cp = (u_char *) buffer_ptr(&iqueue); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 864 | msg_len = GET_32BIT(cp); |
| 865 | if (msg_len > 256 * 1024) { |
| 866 | error("bad message "); |
| 867 | exit(11); |
| 868 | } |
| 869 | if (buffer_len(&iqueue) < msg_len + 4) |
| 870 | return; |
| 871 | buffer_consume(&iqueue, 4); |
| 872 | type = buffer_get_char(&iqueue); |
| 873 | switch (type) { |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 874 | case SSH2_FXP_INIT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 875 | process_init(); |
| 876 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 877 | case SSH2_FXP_OPEN: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 878 | process_open(); |
| 879 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 880 | case SSH2_FXP_CLOSE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 881 | process_close(); |
| 882 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 883 | case SSH2_FXP_READ: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 884 | process_read(); |
| 885 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 886 | case SSH2_FXP_WRITE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 887 | process_write(); |
| 888 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 889 | case SSH2_FXP_LSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 890 | process_lstat(); |
| 891 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 892 | case SSH2_FXP_FSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 893 | process_fstat(); |
| 894 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 895 | case SSH2_FXP_SETSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 896 | process_setstat(); |
| 897 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 898 | case SSH2_FXP_FSETSTAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 899 | process_fsetstat(); |
| 900 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 901 | case SSH2_FXP_OPENDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 902 | process_opendir(); |
| 903 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 904 | case SSH2_FXP_READDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 905 | process_readdir(); |
| 906 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 907 | case SSH2_FXP_REMOVE: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 908 | process_remove(); |
| 909 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 910 | case SSH2_FXP_MKDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 911 | process_mkdir(); |
| 912 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 913 | case SSH2_FXP_RMDIR: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 914 | process_rmdir(); |
| 915 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 916 | case SSH2_FXP_REALPATH: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 917 | process_realpath(); |
| 918 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 919 | case SSH2_FXP_STAT: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 920 | process_stat(); |
| 921 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 922 | case SSH2_FXP_RENAME: |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 923 | process_rename(); |
| 924 | break; |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 925 | case SSH2_FXP_EXTENDED: |
| 926 | process_extended(); |
| 927 | break; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 928 | default: |
| 929 | error("Unknown message %d", type); |
| 930 | break; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | int |
| 935 | main(int ac, char **av) |
| 936 | { |
| 937 | fd_set rset, wset; |
| 938 | int in, out, max; |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 939 | ssize_t len, olen; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 940 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 941 | __progname = get_progname(av[0]); |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 942 | handle_init(); |
| 943 | |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 944 | #ifdef DEBUG_SFTP_SERVER |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 945 | log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 946 | #endif |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 947 | |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 948 | in = dup(STDIN_FILENO); |
| 949 | out = dup(STDOUT_FILENO); |
| 950 | |
| 951 | max = 0; |
| 952 | if (in > max) |
| 953 | max = in; |
| 954 | if (out > max) |
| 955 | max = out; |
| 956 | |
| 957 | buffer_init(&iqueue); |
| 958 | buffer_init(&oqueue); |
| 959 | |
| 960 | for (;;) { |
| 961 | FD_ZERO(&rset); |
| 962 | FD_ZERO(&wset); |
| 963 | |
| 964 | FD_SET(in, &rset); |
| 965 | olen = buffer_len(&oqueue); |
| 966 | if (olen > 0) |
| 967 | FD_SET(out, &wset); |
| 968 | |
| 969 | if (select(max+1, &rset, &wset, NULL, NULL) < 0) { |
| 970 | if (errno == EINTR) |
| 971 | continue; |
| 972 | exit(2); |
| 973 | } |
| 974 | |
| 975 | /* copy stdin to iqueue */ |
| 976 | if (FD_ISSET(in, &rset)) { |
| 977 | char buf[4*4096]; |
| 978 | len = read(in, buf, sizeof buf); |
| 979 | if (len == 0) { |
| 980 | debug("read eof"); |
| 981 | exit(0); |
| 982 | } else if (len < 0) { |
| 983 | error("read error"); |
| 984 | exit(1); |
| 985 | } else { |
| 986 | buffer_append(&iqueue, buf, len); |
| 987 | } |
| 988 | } |
| 989 | /* send oqueue to stdout */ |
| 990 | if (FD_ISSET(out, &wset)) { |
| 991 | len = write(out, buffer_ptr(&oqueue), olen); |
| 992 | if (len < 0) { |
| 993 | error("write error"); |
| 994 | exit(1); |
| 995 | } else { |
| 996 | buffer_consume(&oqueue, len); |
| 997 | } |
| 998 | } |
| 999 | /* process requests from client */ |
| 1000 | process(); |
| 1001 | } |
| 1002 | } |