]>
Commit | Line | Data |
---|---|---|
b5e300c2 | 1 | /* |
cb476289 | 2 | * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved. |
b5e300c2 | 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. | |
b5e300c2 | 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" | |
00b3ad3e | 25 | RCSID("$OpenBSD: sftp-server.c,v 1.38 2002/09/11 22:41:50 djm Exp $"); |
b5e300c2 | 26 | |
b5e300c2 | 27 | #include "buffer.h" |
28 | #include "bufaux.h" | |
29 | #include "getput.h" | |
42f11eb2 | 30 | #include "log.h" |
b5e300c2 | 31 | #include "xmalloc.h" |
32 | ||
f546c780 | 33 | #include "sftp.h" |
61e96248 | 34 | #include "sftp-common.h" |
b5e300c2 | 35 | |
36 | /* helper */ | |
f546c780 | 37 | #define get_int64() buffer_get_int64(&iqueue); |
b5e300c2 | 38 | #define get_int() buffer_get_int(&iqueue); |
39 | #define get_string(lenp) buffer_get_string(&iqueue, lenp); | |
f546c780 | 40 | #define TRACE debug |
b5e300c2 | 41 | |
260d427b | 42 | #ifdef HAVE___PROGNAME |
43 | extern char *__progname; | |
44 | #else | |
45 | char *__progname; | |
46 | #endif | |
47 | ||
b5e300c2 | 48 | /* input and output queue */ |
49 | Buffer iqueue; | |
50 | Buffer oqueue; | |
51 | ||
3a7fe5ba | 52 | /* Version of client */ |
53 | int version; | |
54 | ||
b5e300c2 | 55 | /* portable attibutes, etc. */ |
56 | ||
b5e300c2 | 57 | typedef struct Stat Stat; |
58 | ||
61e96248 | 59 | struct Stat { |
b5e300c2 | 60 | char *name; |
61 | char *long_name; | |
62 | Attrib attrib; | |
63 | }; | |
64 | ||
396c147e | 65 | static int |
b5e300c2 | 66 | errno_to_portable(int unixerrno) |
67 | { | |
68 | int ret = 0; | |
dce9bac5 | 69 | |
b5e300c2 | 70 | switch (unixerrno) { |
71 | case 0: | |
f546c780 | 72 | ret = SSH2_FX_OK; |
b5e300c2 | 73 | break; |
74 | case ENOENT: | |
75 | case ENOTDIR: | |
76 | case EBADF: | |
77 | case ELOOP: | |
f546c780 | 78 | ret = SSH2_FX_NO_SUCH_FILE; |
b5e300c2 | 79 | break; |
80 | case EPERM: | |
81 | case EACCES: | |
82 | case EFAULT: | |
f546c780 | 83 | ret = SSH2_FX_PERMISSION_DENIED; |
b5e300c2 | 84 | break; |
85 | case ENAMETOOLONG: | |
86 | case EINVAL: | |
f546c780 | 87 | ret = SSH2_FX_BAD_MESSAGE; |
b5e300c2 | 88 | break; |
89 | default: | |
f546c780 | 90 | ret = SSH2_FX_FAILURE; |
b5e300c2 | 91 | break; |
92 | } | |
93 | return ret; | |
94 | } | |
95 | ||
396c147e | 96 | static int |
b5e300c2 | 97 | flags_from_portable(int pflags) |
98 | { | |
99 | int flags = 0; | |
dce9bac5 | 100 | |
79ddf6db | 101 | if ((pflags & SSH2_FXF_READ) && |
102 | (pflags & SSH2_FXF_WRITE)) { | |
b5e300c2 | 103 | flags = O_RDWR; |
f546c780 | 104 | } else if (pflags & SSH2_FXF_READ) { |
b5e300c2 | 105 | flags = O_RDONLY; |
f546c780 | 106 | } else if (pflags & SSH2_FXF_WRITE) { |
b5e300c2 | 107 | flags = O_WRONLY; |
108 | } | |
f546c780 | 109 | if (pflags & SSH2_FXF_CREAT) |
b5e300c2 | 110 | flags |= O_CREAT; |
f546c780 | 111 | if (pflags & SSH2_FXF_TRUNC) |
b5e300c2 | 112 | flags |= O_TRUNC; |
f546c780 | 113 | if (pflags & SSH2_FXF_EXCL) |
b5e300c2 | 114 | flags |= O_EXCL; |
115 | return flags; | |
116 | } | |
117 | ||
396c147e | 118 | static Attrib * |
b5e300c2 | 119 | get_attrib(void) |
120 | { | |
121 | return decode_attrib(&iqueue); | |
122 | } | |
123 | ||
124 | /* handle handles */ | |
125 | ||
126 | typedef struct Handle Handle; | |
127 | struct Handle { | |
128 | int use; | |
129 | DIR *dirp; | |
130 | int fd; | |
131 | char *name; | |
132 | }; | |
dce9bac5 | 133 | |
b5e300c2 | 134 | enum { |
135 | HANDLE_UNUSED, | |
136 | HANDLE_DIR, | |
137 | HANDLE_FILE | |
138 | }; | |
dce9bac5 | 139 | |
b5e300c2 | 140 | Handle handles[100]; |
141 | ||
396c147e | 142 | static void |
b5e300c2 | 143 | handle_init(void) |
144 | { | |
145 | int i; | |
dce9bac5 | 146 | |
184eed6a | 147 | for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) |
b5e300c2 | 148 | handles[i].use = HANDLE_UNUSED; |
149 | } | |
150 | ||
396c147e | 151 | static int |
b5e300c2 | 152 | handle_new(int use, char *name, int fd, DIR *dirp) |
153 | { | |
154 | int i; | |
dce9bac5 | 155 | |
184eed6a | 156 | for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) { |
b5e300c2 | 157 | if (handles[i].use == HANDLE_UNUSED) { |
158 | handles[i].use = use; | |
159 | handles[i].dirp = dirp; | |
160 | handles[i].fd = fd; | |
161 | handles[i].name = name; | |
162 | return i; | |
163 | } | |
164 | } | |
165 | return -1; | |
166 | } | |
167 | ||
396c147e | 168 | static int |
b5e300c2 | 169 | handle_is_ok(int i, int type) |
170 | { | |
f546c780 | 171 | return i >= 0 && i < sizeof(handles)/sizeof(Handle) && |
172 | handles[i].use == type; | |
b5e300c2 | 173 | } |
174 | ||
396c147e | 175 | static int |
b5e300c2 | 176 | handle_to_string(int handle, char **stringp, int *hlenp) |
177 | { | |
b5e300c2 | 178 | if (stringp == NULL || hlenp == NULL) |
179 | return -1; | |
b5c334cc | 180 | *stringp = xmalloc(sizeof(int32_t)); |
181 | PUT_32BIT(*stringp, handle); | |
182 | *hlenp = sizeof(int32_t); | |
b5e300c2 | 183 | return 0; |
184 | } | |
185 | ||
396c147e | 186 | static int |
bcbf86ec | 187 | handle_from_string(char *handle, u_int hlen) |
b5e300c2 | 188 | { |
b5c334cc | 189 | int val; |
dce9bac5 | 190 | |
b5c334cc | 191 | if (hlen != sizeof(int32_t)) |
b5e300c2 | 192 | return -1; |
b5c334cc | 193 | val = GET_32BIT(handle); |
b5e300c2 | 194 | if (handle_is_ok(val, HANDLE_FILE) || |
195 | handle_is_ok(val, HANDLE_DIR)) | |
196 | return val; | |
197 | return -1; | |
198 | } | |
199 | ||
396c147e | 200 | static char * |
b5e300c2 | 201 | handle_to_name(int handle) |
202 | { | |
203 | if (handle_is_ok(handle, HANDLE_DIR)|| | |
204 | handle_is_ok(handle, HANDLE_FILE)) | |
205 | return handles[handle].name; | |
206 | return NULL; | |
207 | } | |
208 | ||
396c147e | 209 | static DIR * |
b5e300c2 | 210 | handle_to_dir(int handle) |
211 | { | |
212 | if (handle_is_ok(handle, HANDLE_DIR)) | |
213 | return handles[handle].dirp; | |
214 | return NULL; | |
215 | } | |
216 | ||
396c147e | 217 | static int |
b5e300c2 | 218 | handle_to_fd(int handle) |
219 | { | |
2b87da3b | 220 | if (handle_is_ok(handle, HANDLE_FILE)) |
b5e300c2 | 221 | return handles[handle].fd; |
222 | return -1; | |
223 | } | |
224 | ||
396c147e | 225 | static int |
b5e300c2 | 226 | handle_close(int handle) |
227 | { | |
228 | int ret = -1; | |
dce9bac5 | 229 | |
b5e300c2 | 230 | if (handle_is_ok(handle, HANDLE_FILE)) { |
231 | ret = close(handles[handle].fd); | |
232 | handles[handle].use = HANDLE_UNUSED; | |
233 | } else if (handle_is_ok(handle, HANDLE_DIR)) { | |
234 | ret = closedir(handles[handle].dirp); | |
235 | handles[handle].use = HANDLE_UNUSED; | |
236 | } else { | |
237 | errno = ENOENT; | |
238 | } | |
239 | return ret; | |
240 | } | |
241 | ||
396c147e | 242 | static int |
b5e300c2 | 243 | get_handle(void) |
244 | { | |
245 | char *handle; | |
f546c780 | 246 | int val = -1; |
bcbf86ec | 247 | u_int hlen; |
dce9bac5 | 248 | |
b5e300c2 | 249 | handle = get_string(&hlen); |
f546c780 | 250 | if (hlen < 256) |
251 | val = handle_from_string(handle, hlen); | |
b5e300c2 | 252 | xfree(handle); |
253 | return val; | |
254 | } | |
255 | ||
256 | /* send replies */ | |
257 | ||
396c147e | 258 | static void |
b5e300c2 | 259 | send_msg(Buffer *m) |
260 | { | |
261 | int mlen = buffer_len(m); | |
dce9bac5 | 262 | |
b5e300c2 | 263 | buffer_put_int(&oqueue, mlen); |
264 | buffer_append(&oqueue, buffer_ptr(m), mlen); | |
265 | buffer_consume(m, mlen); | |
266 | } | |
267 | ||
396c147e | 268 | static void |
b5e300c2 | 269 | send_status(u_int32_t id, u_int32_t error) |
270 | { | |
271 | Buffer msg; | |
3a7fe5ba | 272 | const char *status_messages[] = { |
273 | "Success", /* SSH_FX_OK */ | |
274 | "End of file", /* SSH_FX_EOF */ | |
275 | "No such file", /* SSH_FX_NO_SUCH_FILE */ | |
276 | "Permission denied", /* SSH_FX_PERMISSION_DENIED */ | |
277 | "Failure", /* SSH_FX_FAILURE */ | |
278 | "Bad message", /* SSH_FX_BAD_MESSAGE */ | |
279 | "No connection", /* SSH_FX_NO_CONNECTION */ | |
280 | "Connection lost", /* SSH_FX_CONNECTION_LOST */ | |
281 | "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */ | |
282 | "Unknown error" /* Others */ | |
283 | }; | |
dce9bac5 | 284 | |
9906a836 | 285 | TRACE("sent status id %u error %u", id, error); |
b5e300c2 | 286 | buffer_init(&msg); |
f546c780 | 287 | buffer_put_char(&msg, SSH2_FXP_STATUS); |
b5e300c2 | 288 | buffer_put_int(&msg, id); |
289 | buffer_put_int(&msg, error); | |
3a7fe5ba | 290 | if (version >= 3) { |
cd332296 | 291 | buffer_put_cstring(&msg, |
3a7fe5ba | 292 | status_messages[MIN(error,SSH2_FX_MAX)]); |
293 | buffer_put_cstring(&msg, ""); | |
294 | } | |
b5e300c2 | 295 | send_msg(&msg); |
296 | buffer_free(&msg); | |
297 | } | |
396c147e | 298 | static void |
b5e300c2 | 299 | send_data_or_handle(char type, u_int32_t id, char *data, int dlen) |
300 | { | |
301 | Buffer msg; | |
dce9bac5 | 302 | |
b5e300c2 | 303 | buffer_init(&msg); |
304 | buffer_put_char(&msg, type); | |
305 | buffer_put_int(&msg, id); | |
306 | buffer_put_string(&msg, data, dlen); | |
307 | send_msg(&msg); | |
308 | buffer_free(&msg); | |
309 | } | |
310 | ||
396c147e | 311 | static void |
b5e300c2 | 312 | send_data(u_int32_t id, char *data, int dlen) |
313 | { | |
9906a836 | 314 | TRACE("sent data id %u len %d", id, dlen); |
f546c780 | 315 | send_data_or_handle(SSH2_FXP_DATA, id, data, dlen); |
b5e300c2 | 316 | } |
317 | ||
396c147e | 318 | static void |
b5e300c2 | 319 | send_handle(u_int32_t id, int handle) |
320 | { | |
321 | char *string; | |
322 | int hlen; | |
dce9bac5 | 323 | |
b5e300c2 | 324 | handle_to_string(handle, &string, &hlen); |
9906a836 | 325 | TRACE("sent handle id %u handle %d", id, handle); |
f546c780 | 326 | send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen); |
b5e300c2 | 327 | xfree(string); |
328 | } | |
329 | ||
396c147e | 330 | static void |
b5e300c2 | 331 | send_names(u_int32_t id, int count, Stat *stats) |
332 | { | |
333 | Buffer msg; | |
334 | int i; | |
dce9bac5 | 335 | |
b5e300c2 | 336 | buffer_init(&msg); |
f546c780 | 337 | buffer_put_char(&msg, SSH2_FXP_NAME); |
b5e300c2 | 338 | buffer_put_int(&msg, id); |
339 | buffer_put_int(&msg, count); | |
9906a836 | 340 | TRACE("sent names id %u count %d", id, count); |
b5e300c2 | 341 | for (i = 0; i < count; i++) { |
342 | buffer_put_cstring(&msg, stats[i].name); | |
343 | buffer_put_cstring(&msg, stats[i].long_name); | |
344 | encode_attrib(&msg, &stats[i].attrib); | |
345 | } | |
346 | send_msg(&msg); | |
347 | buffer_free(&msg); | |
348 | } | |
349 | ||
396c147e | 350 | static void |
b5e300c2 | 351 | send_attrib(u_int32_t id, Attrib *a) |
352 | { | |
353 | Buffer msg; | |
dce9bac5 | 354 | |
9906a836 | 355 | TRACE("sent attrib id %u have 0x%x", id, a->flags); |
b5e300c2 | 356 | buffer_init(&msg); |
f546c780 | 357 | buffer_put_char(&msg, SSH2_FXP_ATTRS); |
b5e300c2 | 358 | buffer_put_int(&msg, id); |
359 | encode_attrib(&msg, a); | |
360 | send_msg(&msg); | |
361 | buffer_free(&msg); | |
362 | } | |
363 | ||
364 | /* parse incoming */ | |
365 | ||
396c147e | 366 | static void |
b5e300c2 | 367 | process_init(void) |
368 | { | |
369 | Buffer msg; | |
b5e300c2 | 370 | |
802d93bb | 371 | version = get_int(); |
b5e300c2 | 372 | TRACE("client version %d", version); |
373 | buffer_init(&msg); | |
f546c780 | 374 | buffer_put_char(&msg, SSH2_FXP_VERSION); |
375 | buffer_put_int(&msg, SSH2_FILEXFER_VERSION); | |
b5e300c2 | 376 | send_msg(&msg); |
377 | buffer_free(&msg); | |
378 | } | |
379 | ||
396c147e | 380 | static void |
b5e300c2 | 381 | process_open(void) |
382 | { | |
383 | u_int32_t id, pflags; | |
384 | Attrib *a; | |
385 | char *name; | |
f546c780 | 386 | int handle, fd, flags, mode, status = SSH2_FX_FAILURE; |
b5e300c2 | 387 | |
388 | id = get_int(); | |
389 | name = get_string(NULL); | |
f546c780 | 390 | pflags = get_int(); /* portable flags */ |
b5e300c2 | 391 | a = get_attrib(); |
392 | flags = flags_from_portable(pflags); | |
f546c780 | 393 | mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666; |
9906a836 | 394 | TRACE("open id %u name %s flags %d mode 0%o", id, name, pflags, mode); |
b5e300c2 | 395 | fd = open(name, flags, mode); |
396 | if (fd < 0) { | |
397 | status = errno_to_portable(errno); | |
398 | } else { | |
399 | handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL); | |
400 | if (handle < 0) { | |
401 | close(fd); | |
402 | } else { | |
403 | send_handle(id, handle); | |
f546c780 | 404 | status = SSH2_FX_OK; |
b5e300c2 | 405 | } |
406 | } | |
f546c780 | 407 | if (status != SSH2_FX_OK) |
b5e300c2 | 408 | send_status(id, status); |
409 | xfree(name); | |
410 | } | |
411 | ||
396c147e | 412 | static void |
b5e300c2 | 413 | process_close(void) |
414 | { | |
415 | u_int32_t id; | |
f546c780 | 416 | int handle, ret, status = SSH2_FX_FAILURE; |
b5e300c2 | 417 | |
418 | id = get_int(); | |
419 | handle = get_handle(); | |
9906a836 | 420 | TRACE("close id %u handle %d", id, handle); |
b5e300c2 | 421 | ret = handle_close(handle); |
f546c780 | 422 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
b5e300c2 | 423 | send_status(id, status); |
424 | } | |
425 | ||
396c147e | 426 | static void |
b5e300c2 | 427 | process_read(void) |
428 | { | |
429 | char buf[64*1024]; | |
f546c780 | 430 | u_int32_t id, len; |
431 | int handle, fd, ret, status = SSH2_FX_FAILURE; | |
b5e300c2 | 432 | u_int64_t off; |
433 | ||
434 | id = get_int(); | |
435 | handle = get_handle(); | |
f546c780 | 436 | off = get_int64(); |
b5e300c2 | 437 | len = get_int(); |
438 | ||
9906a836 | 439 | TRACE("read id %u handle %d off %llu len %d", id, handle, |
490cad94 | 440 | (u_int64_t)off, len); |
b5e300c2 | 441 | if (len > sizeof buf) { |
442 | len = sizeof buf; | |
443 | log("read change len %d", len); | |
444 | } | |
445 | fd = handle_to_fd(handle); | |
446 | if (fd >= 0) { | |
447 | if (lseek(fd, off, SEEK_SET) < 0) { | |
448 | error("process_read: seek failed"); | |
449 | status = errno_to_portable(errno); | |
450 | } else { | |
451 | ret = read(fd, buf, len); | |
452 | if (ret < 0) { | |
453 | status = errno_to_portable(errno); | |
454 | } else if (ret == 0) { | |
f546c780 | 455 | status = SSH2_FX_EOF; |
b5e300c2 | 456 | } else { |
457 | send_data(id, buf, ret); | |
f546c780 | 458 | status = SSH2_FX_OK; |
b5e300c2 | 459 | } |
460 | } | |
461 | } | |
f546c780 | 462 | if (status != SSH2_FX_OK) |
b5e300c2 | 463 | send_status(id, status); |
464 | } | |
465 | ||
396c147e | 466 | static void |
b5e300c2 | 467 | process_write(void) |
468 | { | |
f546c780 | 469 | u_int32_t id; |
b5e300c2 | 470 | u_int64_t off; |
bcbf86ec | 471 | u_int len; |
f546c780 | 472 | int handle, fd, ret, status = SSH2_FX_FAILURE; |
b5e300c2 | 473 | char *data; |
474 | ||
475 | id = get_int(); | |
476 | handle = get_handle(); | |
f546c780 | 477 | off = get_int64(); |
b5e300c2 | 478 | data = get_string(&len); |
479 | ||
9906a836 | 480 | TRACE("write id %u handle %d off %llu len %d", id, handle, |
490cad94 | 481 | (u_int64_t)off, len); |
b5e300c2 | 482 | fd = handle_to_fd(handle); |
483 | if (fd >= 0) { | |
484 | if (lseek(fd, off, SEEK_SET) < 0) { | |
485 | status = errno_to_portable(errno); | |
486 | error("process_write: seek failed"); | |
487 | } else { | |
488 | /* XXX ATOMICIO ? */ | |
489 | ret = write(fd, data, len); | |
490 | if (ret == -1) { | |
491 | error("process_write: write failed"); | |
492 | status = errno_to_portable(errno); | |
493 | } else if (ret == len) { | |
f546c780 | 494 | status = SSH2_FX_OK; |
b5e300c2 | 495 | } else { |
496 | log("nothing at all written"); | |
497 | } | |
498 | } | |
499 | } | |
500 | send_status(id, status); | |
501 | xfree(data); | |
502 | } | |
503 | ||
396c147e | 504 | static void |
b5e300c2 | 505 | process_do_stat(int do_lstat) |
506 | { | |
b5c334cc | 507 | Attrib a; |
b5e300c2 | 508 | struct stat st; |
509 | u_int32_t id; | |
510 | char *name; | |
f546c780 | 511 | int ret, status = SSH2_FX_FAILURE; |
b5e300c2 | 512 | |
513 | id = get_int(); | |
514 | name = get_string(NULL); | |
9906a836 | 515 | TRACE("%sstat id %u name %s", do_lstat ? "l" : "", id, name); |
b5e300c2 | 516 | ret = do_lstat ? lstat(name, &st) : stat(name, &st); |
517 | if (ret < 0) { | |
518 | status = errno_to_portable(errno); | |
519 | } else { | |
b5c334cc | 520 | stat_to_attrib(&st, &a); |
521 | send_attrib(id, &a); | |
f546c780 | 522 | status = SSH2_FX_OK; |
b5e300c2 | 523 | } |
f546c780 | 524 | if (status != SSH2_FX_OK) |
b5e300c2 | 525 | send_status(id, status); |
526 | xfree(name); | |
527 | } | |
528 | ||
396c147e | 529 | static void |
b5e300c2 | 530 | process_stat(void) |
531 | { | |
532 | process_do_stat(0); | |
533 | } | |
534 | ||
396c147e | 535 | static void |
b5e300c2 | 536 | process_lstat(void) |
537 | { | |
538 | process_do_stat(1); | |
539 | } | |
540 | ||
396c147e | 541 | static void |
b5e300c2 | 542 | process_fstat(void) |
543 | { | |
b5c334cc | 544 | Attrib a; |
b5e300c2 | 545 | struct stat st; |
546 | u_int32_t id; | |
f546c780 | 547 | int fd, ret, handle, status = SSH2_FX_FAILURE; |
b5e300c2 | 548 | |
549 | id = get_int(); | |
550 | handle = get_handle(); | |
9906a836 | 551 | TRACE("fstat id %u handle %d", id, handle); |
b5e300c2 | 552 | fd = handle_to_fd(handle); |
553 | if (fd >= 0) { | |
554 | ret = fstat(fd, &st); | |
555 | if (ret < 0) { | |
556 | status = errno_to_portable(errno); | |
557 | } else { | |
b5c334cc | 558 | stat_to_attrib(&st, &a); |
559 | send_attrib(id, &a); | |
f546c780 | 560 | status = SSH2_FX_OK; |
b5e300c2 | 561 | } |
562 | } | |
f546c780 | 563 | if (status != SSH2_FX_OK) |
b5e300c2 | 564 | send_status(id, status); |
565 | } | |
566 | ||
396c147e | 567 | static struct timeval * |
b5e300c2 | 568 | attrib_to_tv(Attrib *a) |
569 | { | |
570 | static struct timeval tv[2]; | |
dce9bac5 | 571 | |
b5e300c2 | 572 | tv[0].tv_sec = a->atime; |
573 | tv[0].tv_usec = 0; | |
574 | tv[1].tv_sec = a->mtime; | |
575 | tv[1].tv_usec = 0; | |
576 | return tv; | |
577 | } | |
578 | ||
396c147e | 579 | static void |
b5e300c2 | 580 | process_setstat(void) |
581 | { | |
582 | Attrib *a; | |
583 | u_int32_t id; | |
584 | char *name; | |
9906a836 | 585 | int status = SSH2_FX_OK, ret; |
b5e300c2 | 586 | |
587 | id = get_int(); | |
588 | name = get_string(NULL); | |
589 | a = get_attrib(); | |
9906a836 | 590 | TRACE("setstat id %u name %s", id, name); |
cb476289 | 591 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { |
592 | ret = truncate(name, a->size); | |
593 | if (ret == -1) | |
594 | status = errno_to_portable(errno); | |
595 | } | |
f546c780 | 596 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
b5e300c2 | 597 | ret = chmod(name, a->perm & 0777); |
598 | if (ret == -1) | |
599 | status = errno_to_portable(errno); | |
600 | } | |
f546c780 | 601 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
b5e300c2 | 602 | ret = utimes(name, attrib_to_tv(a)); |
603 | if (ret == -1) | |
604 | status = errno_to_portable(errno); | |
605 | } | |
408ba72f | 606 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
607 | ret = chown(name, a->uid, a->gid); | |
608 | if (ret == -1) | |
609 | status = errno_to_portable(errno); | |
610 | } | |
b5e300c2 | 611 | send_status(id, status); |
612 | xfree(name); | |
613 | } | |
614 | ||
396c147e | 615 | static void |
b5e300c2 | 616 | process_fsetstat(void) |
617 | { | |
618 | Attrib *a; | |
619 | u_int32_t id; | |
620 | int handle, fd, ret; | |
f546c780 | 621 | int status = SSH2_FX_OK; |
bcbf86ec | 622 | char *name; |
16e538d4 | 623 | |
b5e300c2 | 624 | id = get_int(); |
625 | handle = get_handle(); | |
626 | a = get_attrib(); | |
9906a836 | 627 | TRACE("fsetstat id %u handle %d", id, handle); |
b5e300c2 | 628 | fd = handle_to_fd(handle); |
629 | name = handle_to_name(handle); | |
16e538d4 | 630 | if (fd < 0 || name == NULL) { |
f546c780 | 631 | status = SSH2_FX_FAILURE; |
b5e300c2 | 632 | } else { |
cb476289 | 633 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { |
634 | ret = ftruncate(fd, a->size); | |
635 | if (ret == -1) | |
636 | status = errno_to_portable(errno); | |
637 | } | |
f546c780 | 638 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
2fd3c144 | 639 | #ifdef HAVE_FCHMOD |
b5e300c2 | 640 | ret = fchmod(fd, a->perm & 0777); |
2fd3c144 | 641 | #else |
c33f0b36 | 642 | ret = chmod(name, a->perm & 0777); |
2fd3c144 | 643 | #endif |
b5e300c2 | 644 | if (ret == -1) |
645 | status = errno_to_portable(errno); | |
646 | } | |
f546c780 | 647 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
b5e300c2 | 648 | #ifdef HAVE_FUTIMES |
649 | ret = futimes(fd, attrib_to_tv(a)); | |
650 | #else | |
651 | ret = utimes(name, attrib_to_tv(a)); | |
652 | #endif | |
653 | if (ret == -1) | |
654 | status = errno_to_portable(errno); | |
655 | } | |
408ba72f | 656 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
01f13020 | 657 | #ifdef HAVE_FCHOWN |
408ba72f | 658 | ret = fchown(fd, a->uid, a->gid); |
01f13020 | 659 | #else |
660 | ret = chown(name, a->uid, a->gid); | |
661 | #endif | |
408ba72f | 662 | if (ret == -1) |
663 | status = errno_to_portable(errno); | |
664 | } | |
b5e300c2 | 665 | } |
666 | send_status(id, status); | |
667 | } | |
668 | ||
396c147e | 669 | static void |
b5e300c2 | 670 | process_opendir(void) |
671 | { | |
672 | DIR *dirp = NULL; | |
673 | char *path; | |
f546c780 | 674 | int handle, status = SSH2_FX_FAILURE; |
b5e300c2 | 675 | u_int32_t id; |
676 | ||
677 | id = get_int(); | |
678 | path = get_string(NULL); | |
9906a836 | 679 | TRACE("opendir id %u path %s", id, path); |
2b87da3b | 680 | dirp = opendir(path); |
b5e300c2 | 681 | if (dirp == NULL) { |
682 | status = errno_to_portable(errno); | |
683 | } else { | |
684 | handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp); | |
685 | if (handle < 0) { | |
686 | closedir(dirp); | |
687 | } else { | |
688 | send_handle(id, handle); | |
f546c780 | 689 | status = SSH2_FX_OK; |
b5e300c2 | 690 | } |
2b87da3b | 691 | |
b5e300c2 | 692 | } |
f546c780 | 693 | if (status != SSH2_FX_OK) |
b5e300c2 | 694 | send_status(id, status); |
695 | xfree(path); | |
696 | } | |
697 | ||
396c147e | 698 | static void |
b5e300c2 | 699 | process_readdir(void) |
700 | { | |
701 | DIR *dirp; | |
702 | struct dirent *dp; | |
703 | char *path; | |
704 | int handle; | |
705 | u_int32_t id; | |
706 | ||
707 | id = get_int(); | |
708 | handle = get_handle(); | |
9906a836 | 709 | TRACE("readdir id %u handle %d", id, handle); |
b5e300c2 | 710 | dirp = handle_to_dir(handle); |
711 | path = handle_to_name(handle); | |
712 | if (dirp == NULL || path == NULL) { | |
f546c780 | 713 | send_status(id, SSH2_FX_FAILURE); |
b5e300c2 | 714 | } else { |
b5e300c2 | 715 | struct stat st; |
716 | char pathname[1024]; | |
717 | Stat *stats; | |
718 | int nstats = 10, count = 0, i; | |
9906a836 | 719 | |
b5e300c2 | 720 | stats = xmalloc(nstats * sizeof(Stat)); |
721 | while ((dp = readdir(dirp)) != NULL) { | |
722 | if (count >= nstats) { | |
723 | nstats *= 2; | |
724 | stats = xrealloc(stats, nstats * sizeof(Stat)); | |
725 | } | |
726 | /* XXX OVERFLOW ? */ | |
88690211 | 727 | snprintf(pathname, sizeof pathname, "%s%s%s", path, |
728 | strcmp(path, "/") ? "/" : "", dp->d_name); | |
b5e300c2 | 729 | if (lstat(pathname, &st) < 0) |
730 | continue; | |
b5c334cc | 731 | stat_to_attrib(&st, &(stats[count].attrib)); |
b5e300c2 | 732 | stats[count].name = xstrdup(dp->d_name); |
00b3ad3e | 733 | stats[count].long_name = ls_file(dp->d_name, &st, 0); |
b5e300c2 | 734 | count++; |
735 | /* send up to 100 entries in one message */ | |
b5c334cc | 736 | /* XXX check packet size instead */ |
b5e300c2 | 737 | if (count == 100) |
738 | break; | |
739 | } | |
f546c780 | 740 | if (count > 0) { |
741 | send_names(id, count, stats); | |
184eed6a | 742 | for (i = 0; i < count; i++) { |
f546c780 | 743 | xfree(stats[i].name); |
744 | xfree(stats[i].long_name); | |
745 | } | |
746 | } else { | |
747 | send_status(id, SSH2_FX_EOF); | |
b5e300c2 | 748 | } |
749 | xfree(stats); | |
750 | } | |
751 | } | |
752 | ||
396c147e | 753 | static void |
b5e300c2 | 754 | process_remove(void) |
755 | { | |
756 | char *name; | |
757 | u_int32_t id; | |
f546c780 | 758 | int status = SSH2_FX_FAILURE; |
b5e300c2 | 759 | int ret; |
760 | ||
761 | id = get_int(); | |
762 | name = get_string(NULL); | |
9906a836 | 763 | TRACE("remove id %u name %s", id, name); |
67b0facb | 764 | ret = unlink(name); |
f546c780 | 765 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
b5e300c2 | 766 | send_status(id, status); |
767 | xfree(name); | |
768 | } | |
769 | ||
396c147e | 770 | static void |
b5e300c2 | 771 | process_mkdir(void) |
772 | { | |
773 | Attrib *a; | |
774 | u_int32_t id; | |
775 | char *name; | |
f546c780 | 776 | int ret, mode, status = SSH2_FX_FAILURE; |
b5e300c2 | 777 | |
778 | id = get_int(); | |
779 | name = get_string(NULL); | |
780 | a = get_attrib(); | |
f546c780 | 781 | mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? |
782 | a->perm & 0777 : 0777; | |
9906a836 | 783 | TRACE("mkdir id %u name %s mode 0%o", id, name, mode); |
b5e300c2 | 784 | ret = mkdir(name, mode); |
f546c780 | 785 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
b5e300c2 | 786 | send_status(id, status); |
787 | xfree(name); | |
788 | } | |
789 | ||
396c147e | 790 | static void |
b5e300c2 | 791 | process_rmdir(void) |
792 | { | |
793 | u_int32_t id; | |
794 | char *name; | |
795 | int ret, status; | |
796 | ||
797 | id = get_int(); | |
798 | name = get_string(NULL); | |
9906a836 | 799 | TRACE("rmdir id %u name %s", id, name); |
b5e300c2 | 800 | ret = rmdir(name); |
f546c780 | 801 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; |
b5e300c2 | 802 | send_status(id, status); |
803 | xfree(name); | |
804 | } | |
805 | ||
396c147e | 806 | static void |
b5e300c2 | 807 | process_realpath(void) |
808 | { | |
809 | char resolvedname[MAXPATHLEN]; | |
810 | u_int32_t id; | |
811 | char *path; | |
812 | ||
813 | id = get_int(); | |
814 | path = get_string(NULL); | |
6b523bae | 815 | if (path[0] == '\0') { |
816 | xfree(path); | |
817 | path = xstrdup("."); | |
818 | } | |
9906a836 | 819 | TRACE("realpath id %u path %s", id, path); |
b5e300c2 | 820 | if (realpath(path, resolvedname) == NULL) { |
821 | send_status(id, errno_to_portable(errno)); | |
822 | } else { | |
823 | Stat s; | |
824 | attrib_clear(&s.attrib); | |
825 | s.name = s.long_name = resolvedname; | |
826 | send_names(id, 1, &s); | |
827 | } | |
828 | xfree(path); | |
829 | } | |
830 | ||
396c147e | 831 | static void |
b5e300c2 | 832 | process_rename(void) |
833 | { | |
834 | u_int32_t id; | |
b5c334cc | 835 | struct stat st; |
b5e300c2 | 836 | char *oldpath, *newpath; |
b5c334cc | 837 | int ret, status = SSH2_FX_FAILURE; |
b5e300c2 | 838 | |
839 | id = get_int(); | |
840 | oldpath = get_string(NULL); | |
841 | newpath = get_string(NULL); | |
9906a836 | 842 | TRACE("rename id %u old %s new %s", id, oldpath, newpath); |
b5c334cc | 843 | /* fail if 'newpath' exists */ |
844 | if (stat(newpath, &st) == -1) { | |
845 | ret = rename(oldpath, newpath); | |
846 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; | |
847 | } | |
b5e300c2 | 848 | send_status(id, status); |
849 | xfree(oldpath); | |
850 | xfree(newpath); | |
851 | } | |
852 | ||
396c147e | 853 | static void |
3a7fe5ba | 854 | process_readlink(void) |
855 | { | |
856 | u_int32_t id; | |
362df52e | 857 | int len; |
3a7fe5ba | 858 | char link[MAXPATHLEN]; |
859 | char *path; | |
860 | ||
861 | id = get_int(); | |
862 | path = get_string(NULL); | |
9906a836 | 863 | TRACE("readlink id %u path %s", id, path); |
362df52e | 864 | if ((len = readlink(path, link, sizeof(link) - 1)) == -1) |
3a7fe5ba | 865 | send_status(id, errno_to_portable(errno)); |
866 | else { | |
867 | Stat s; | |
184eed6a | 868 | |
362df52e | 869 | link[len] = '\0'; |
3a7fe5ba | 870 | attrib_clear(&s.attrib); |
871 | s.name = s.long_name = link; | |
872 | send_names(id, 1, &s); | |
873 | } | |
874 | xfree(path); | |
875 | } | |
876 | ||
396c147e | 877 | static void |
3a7fe5ba | 878 | process_symlink(void) |
879 | { | |
880 | u_int32_t id; | |
881 | struct stat st; | |
882 | char *oldpath, *newpath; | |
883 | int ret, status = SSH2_FX_FAILURE; | |
884 | ||
885 | id = get_int(); | |
886 | oldpath = get_string(NULL); | |
887 | newpath = get_string(NULL); | |
9906a836 | 888 | TRACE("symlink id %u old %s new %s", id, oldpath, newpath); |
3a7fe5ba | 889 | /* fail if 'newpath' exists */ |
890 | if (stat(newpath, &st) == -1) { | |
891 | ret = symlink(oldpath, newpath); | |
892 | status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK; | |
893 | } | |
894 | send_status(id, status); | |
895 | xfree(oldpath); | |
896 | xfree(newpath); | |
897 | } | |
898 | ||
396c147e | 899 | static void |
f546c780 | 900 | process_extended(void) |
901 | { | |
902 | u_int32_t id; | |
903 | char *request; | |
904 | ||
905 | id = get_int(); | |
906 | request = get_string(NULL); | |
907 | send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */ | |
908 | xfree(request); | |
909 | } | |
b5e300c2 | 910 | |
911 | /* stolen from ssh-agent */ | |
912 | ||
396c147e | 913 | static void |
b5e300c2 | 914 | process(void) |
915 | { | |
1e3b8b07 | 916 | u_int msg_len; |
9b4ac641 | 917 | u_int buf_len; |
918 | u_int consumed; | |
1e3b8b07 | 919 | u_int type; |
920 | u_char *cp; | |
b5e300c2 | 921 | |
9b4ac641 | 922 | buf_len = buffer_len(&iqueue); |
923 | if (buf_len < 5) | |
b5e300c2 | 924 | return; /* Incomplete message. */ |
20905a8e | 925 | cp = buffer_ptr(&iqueue); |
b5e300c2 | 926 | msg_len = GET_32BIT(cp); |
927 | if (msg_len > 256 * 1024) { | |
928 | error("bad message "); | |
929 | exit(11); | |
930 | } | |
9b4ac641 | 931 | if (buf_len < msg_len + 4) |
b5e300c2 | 932 | return; |
933 | buffer_consume(&iqueue, 4); | |
9b4ac641 | 934 | buf_len -= 4; |
b5e300c2 | 935 | type = buffer_get_char(&iqueue); |
936 | switch (type) { | |
f546c780 | 937 | case SSH2_FXP_INIT: |
b5e300c2 | 938 | process_init(); |
939 | break; | |
f546c780 | 940 | case SSH2_FXP_OPEN: |
b5e300c2 | 941 | process_open(); |
942 | break; | |
f546c780 | 943 | case SSH2_FXP_CLOSE: |
b5e300c2 | 944 | process_close(); |
945 | break; | |
f546c780 | 946 | case SSH2_FXP_READ: |
b5e300c2 | 947 | process_read(); |
948 | break; | |
f546c780 | 949 | case SSH2_FXP_WRITE: |
b5e300c2 | 950 | process_write(); |
951 | break; | |
f546c780 | 952 | case SSH2_FXP_LSTAT: |
b5e300c2 | 953 | process_lstat(); |
954 | break; | |
f546c780 | 955 | case SSH2_FXP_FSTAT: |
b5e300c2 | 956 | process_fstat(); |
957 | break; | |
f546c780 | 958 | case SSH2_FXP_SETSTAT: |
b5e300c2 | 959 | process_setstat(); |
960 | break; | |
f546c780 | 961 | case SSH2_FXP_FSETSTAT: |
b5e300c2 | 962 | process_fsetstat(); |
963 | break; | |
f546c780 | 964 | case SSH2_FXP_OPENDIR: |
b5e300c2 | 965 | process_opendir(); |
966 | break; | |
f546c780 | 967 | case SSH2_FXP_READDIR: |
b5e300c2 | 968 | process_readdir(); |
969 | break; | |
f546c780 | 970 | case SSH2_FXP_REMOVE: |
b5e300c2 | 971 | process_remove(); |
972 | break; | |
f546c780 | 973 | case SSH2_FXP_MKDIR: |
b5e300c2 | 974 | process_mkdir(); |
975 | break; | |
f546c780 | 976 | case SSH2_FXP_RMDIR: |
b5e300c2 | 977 | process_rmdir(); |
978 | break; | |
f546c780 | 979 | case SSH2_FXP_REALPATH: |
b5e300c2 | 980 | process_realpath(); |
981 | break; | |
f546c780 | 982 | case SSH2_FXP_STAT: |
b5e300c2 | 983 | process_stat(); |
984 | break; | |
f546c780 | 985 | case SSH2_FXP_RENAME: |
b5e300c2 | 986 | process_rename(); |
987 | break; | |
3a7fe5ba | 988 | case SSH2_FXP_READLINK: |
989 | process_readlink(); | |
990 | break; | |
991 | case SSH2_FXP_SYMLINK: | |
992 | process_symlink(); | |
993 | break; | |
f546c780 | 994 | case SSH2_FXP_EXTENDED: |
995 | process_extended(); | |
996 | break; | |
b5e300c2 | 997 | default: |
998 | error("Unknown message %d", type); | |
999 | break; | |
1000 | } | |
9b4ac641 | 1001 | /* discard the remaining bytes from the current packet */ |
1002 | if (buf_len < buffer_len(&iqueue)) | |
1003 | fatal("iqueue grows"); | |
1004 | consumed = buf_len - buffer_len(&iqueue); | |
1005 | if (msg_len < consumed) | |
1006 | fatal("msg_len %d < consumed %d", msg_len, consumed); | |
1007 | if (msg_len > consumed) | |
1008 | buffer_consume(&iqueue, msg_len - consumed); | |
b5e300c2 | 1009 | } |
1010 | ||
1011 | int | |
1012 | main(int ac, char **av) | |
1013 | { | |
c8d75031 | 1014 | fd_set *rset, *wset; |
b5e300c2 | 1015 | int in, out, max; |
c8d75031 | 1016 | ssize_t len, olen, set_size; |
b5e300c2 | 1017 | |
61b3a2bc | 1018 | /* XXX should use getopt */ |
1019 | ||
260d427b | 1020 | __progname = get_progname(av[0]); |
b5e300c2 | 1021 | handle_init(); |
1022 | ||
b5c334cc | 1023 | #ifdef DEBUG_SFTP_SERVER |
2b87da3b | 1024 | log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0); |
b5c334cc | 1025 | #endif |
f546c780 | 1026 | |
b5e300c2 | 1027 | in = dup(STDIN_FILENO); |
1028 | out = dup(STDOUT_FILENO); | |
1029 | ||
fe56c12b | 1030 | #ifdef HAVE_CYGWIN |
1031 | setmode(in, O_BINARY); | |
1032 | setmode(out, O_BINARY); | |
1033 | #endif | |
1034 | ||
b5e300c2 | 1035 | max = 0; |
1036 | if (in > max) | |
1037 | max = in; | |
1038 | if (out > max) | |
1039 | max = out; | |
1040 | ||
1041 | buffer_init(&iqueue); | |
1042 | buffer_init(&oqueue); | |
1043 | ||
c8d75031 | 1044 | set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask); |
1045 | rset = (fd_set *)xmalloc(set_size); | |
1046 | wset = (fd_set *)xmalloc(set_size); | |
1047 | ||
b5e300c2 | 1048 | for (;;) { |
c8d75031 | 1049 | memset(rset, 0, set_size); |
1050 | memset(wset, 0, set_size); | |
b5e300c2 | 1051 | |
c8d75031 | 1052 | FD_SET(in, rset); |
b5e300c2 | 1053 | olen = buffer_len(&oqueue); |
1054 | if (olen > 0) | |
c8d75031 | 1055 | FD_SET(out, wset); |
b5e300c2 | 1056 | |
c8d75031 | 1057 | if (select(max+1, rset, wset, NULL, NULL) < 0) { |
b5e300c2 | 1058 | if (errno == EINTR) |
1059 | continue; | |
1060 | exit(2); | |
1061 | } | |
1062 | ||
1063 | /* copy stdin to iqueue */ | |
c8d75031 | 1064 | if (FD_ISSET(in, rset)) { |
b5e300c2 | 1065 | char buf[4*4096]; |
1066 | len = read(in, buf, sizeof buf); | |
1067 | if (len == 0) { | |
1068 | debug("read eof"); | |
1069 | exit(0); | |
1070 | } else if (len < 0) { | |
1071 | error("read error"); | |
1072 | exit(1); | |
1073 | } else { | |
1074 | buffer_append(&iqueue, buf, len); | |
1075 | } | |
1076 | } | |
1077 | /* send oqueue to stdout */ | |
c8d75031 | 1078 | if (FD_ISSET(out, wset)) { |
b5e300c2 | 1079 | len = write(out, buffer_ptr(&oqueue), olen); |
1080 | if (len < 0) { | |
1081 | error("write error"); | |
1082 | exit(1); | |
1083 | } else { | |
1084 | buffer_consume(&oqueue, len); | |
1085 | } | |
1086 | } | |
1087 | /* process requests from client */ | |
1088 | process(); | |
1089 | } | |
1090 | } |