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