]> andersk Git - openssh.git/blame - sftp-server.c
- markus@cvs.openbsd.org 2002/03/25 09:21:13
[openssh.git] / sftp-server.c
CommitLineData
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"
cb476289 25RCSID("$OpenBSD: sftp-server.c,v 1.33 2002/02/13 00:28:13 markus 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
43extern char *__progname;
44#else
45char *__progname;
46#endif
47
b5e300c2 48/* input and output queue */
49Buffer iqueue;
50Buffer oqueue;
51
3a7fe5ba 52/* Version of client */
53int version;
54
b5e300c2 55/* portable attibutes, etc. */
56
b5e300c2 57typedef struct Stat Stat;
58
61e96248 59struct Stat {
b5e300c2 60 char *name;
61 char *long_name;
62 Attrib attrib;
63};
64
396c147e 65static int
b5e300c2 66errno_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 96static int
b5e300c2 97flags_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 118static Attrib *
b5e300c2 119get_attrib(void)
120{
121 return decode_attrib(&iqueue);
122}
123
124/* handle handles */
125
126typedef struct Handle Handle;
127struct Handle {
128 int use;
129 DIR *dirp;
130 int fd;
131 char *name;
132};
dce9bac5 133
b5e300c2 134enum {
135 HANDLE_UNUSED,
136 HANDLE_DIR,
137 HANDLE_FILE
138};
dce9bac5 139
b5e300c2 140Handle handles[100];
141
396c147e 142static void
b5e300c2 143handle_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 151static int
b5e300c2 152handle_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 168static int
b5e300c2 169handle_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 175static int
b5e300c2 176handle_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 186static int
bcbf86ec 187handle_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 200static char *
b5e300c2 201handle_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 209static DIR *
b5e300c2 210handle_to_dir(int handle)
211{
212 if (handle_is_ok(handle, HANDLE_DIR))
213 return handles[handle].dirp;
214 return NULL;
215}
216
396c147e 217static int
b5e300c2 218handle_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 225static int
b5e300c2 226handle_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 242static int
b5e300c2 243get_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 258static void
b5e300c2 259send_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 268static void
b5e300c2 269send_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
b5e300c2 285 TRACE("sent status id %d error %d", id, error);
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 298static void
b5e300c2 299send_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 311static void
b5e300c2 312send_data(u_int32_t id, char *data, int dlen)
313{
314 TRACE("sent data id %d len %d", id, dlen);
f546c780 315 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
b5e300c2 316}
317
396c147e 318static void
b5e300c2 319send_handle(u_int32_t id, int handle)
320{
321 char *string;
322 int hlen;
dce9bac5 323
b5e300c2 324 handle_to_string(handle, &string, &hlen);
325 TRACE("sent handle id %d handle %d", id, handle);
f546c780 326 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
b5e300c2 327 xfree(string);
328}
329
396c147e 330static void
b5e300c2 331send_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);
340 TRACE("sent names id %d count %d", id, count);
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 350static void
b5e300c2 351send_attrib(u_int32_t id, Attrib *a)
352{
353 Buffer msg;
dce9bac5 354
b5e300c2 355 TRACE("sent attrib id %d have 0x%x", id, a->flags);
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 366static void
b5e300c2 367process_init(void)
368{
369 Buffer msg;
b5e300c2 370
3a7fe5ba 371 version = buffer_get_int(&iqueue);
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 380static void
b5e300c2 381process_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;
b5e300c2 394 TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode);
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 412static void
b5e300c2 413process_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();
420 TRACE("close id %d handle %d", id, handle);
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 426static void
b5e300c2 427process_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
9378f292 439 TRACE("read id %d 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 466static void
b5e300c2 467process_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
9378f292 480 TRACE("write id %d 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 504static void
b5e300c2 505process_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);
515 TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name);
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 529static void
b5e300c2 530process_stat(void)
531{
532 process_do_stat(0);
533}
534
396c147e 535static void
b5e300c2 536process_lstat(void)
537{
538 process_do_stat(1);
539}
540
396c147e 541static void
b5e300c2 542process_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();
551 TRACE("fstat id %d handle %d", id, handle);
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 567static struct timeval *
b5e300c2 568attrib_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 579static void
b5e300c2 580process_setstat(void)
581{
582 Attrib *a;
583 u_int32_t id;
584 char *name;
585 int ret;
f546c780 586 int status = SSH2_FX_OK;
b5e300c2 587
588 id = get_int();
589 name = get_string(NULL);
590 a = get_attrib();
591 TRACE("setstat id %d name %s", id, name);
cb476289 592 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
593 ret = truncate(name, a->size);
594 if (ret == -1)
595 status = errno_to_portable(errno);
596 }
f546c780 597 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
b5e300c2 598 ret = chmod(name, a->perm & 0777);
599 if (ret == -1)
600 status = errno_to_portable(errno);
601 }
f546c780 602 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
b5e300c2 603 ret = utimes(name, attrib_to_tv(a));
604 if (ret == -1)
605 status = errno_to_portable(errno);
606 }
408ba72f 607 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
608 ret = chown(name, a->uid, a->gid);
609 if (ret == -1)
610 status = errno_to_portable(errno);
611 }
b5e300c2 612 send_status(id, status);
613 xfree(name);
614}
615
396c147e 616static void
b5e300c2 617process_fsetstat(void)
618{
619 Attrib *a;
620 u_int32_t id;
621 int handle, fd, ret;
f546c780 622 int status = SSH2_FX_OK;
bcbf86ec 623 char *name;
16e538d4 624
b5e300c2 625 id = get_int();
626 handle = get_handle();
627 a = get_attrib();
628 TRACE("fsetstat id %d handle %d", id, handle);
629 fd = handle_to_fd(handle);
630 name = handle_to_name(handle);
16e538d4 631 if (fd < 0 || name == NULL) {
f546c780 632 status = SSH2_FX_FAILURE;
b5e300c2 633 } else {
cb476289 634 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
635 ret = ftruncate(fd, a->size);
636 if (ret == -1)
637 status = errno_to_portable(errno);
638 }
f546c780 639 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
2fd3c144 640#ifdef HAVE_FCHMOD
b5e300c2 641 ret = fchmod(fd, a->perm & 0777);
2fd3c144 642#else
c33f0b36 643 ret = chmod(name, a->perm & 0777);
2fd3c144 644#endif
b5e300c2 645 if (ret == -1)
646 status = errno_to_portable(errno);
647 }
f546c780 648 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
b5e300c2 649#ifdef HAVE_FUTIMES
650 ret = futimes(fd, attrib_to_tv(a));
651#else
652 ret = utimes(name, attrib_to_tv(a));
653#endif
654 if (ret == -1)
655 status = errno_to_portable(errno);
656 }
408ba72f 657 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
01f13020 658#ifdef HAVE_FCHOWN
408ba72f 659 ret = fchown(fd, a->uid, a->gid);
01f13020 660#else
661 ret = chown(name, a->uid, a->gid);
662#endif
408ba72f 663 if (ret == -1)
664 status = errno_to_portable(errno);
665 }
b5e300c2 666 }
667 send_status(id, status);
668}
669
396c147e 670static void
b5e300c2 671process_opendir(void)
672{
673 DIR *dirp = NULL;
674 char *path;
f546c780 675 int handle, status = SSH2_FX_FAILURE;
b5e300c2 676 u_int32_t id;
677
678 id = get_int();
679 path = get_string(NULL);
680 TRACE("opendir id %d path %s", id, path);
2b87da3b 681 dirp = opendir(path);
b5e300c2 682 if (dirp == NULL) {
683 status = errno_to_portable(errno);
684 } else {
685 handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
686 if (handle < 0) {
687 closedir(dirp);
688 } else {
689 send_handle(id, handle);
f546c780 690 status = SSH2_FX_OK;
b5e300c2 691 }
2b87da3b 692
b5e300c2 693 }
f546c780 694 if (status != SSH2_FX_OK)
b5e300c2 695 send_status(id, status);
696 xfree(path);
697}
698
f546c780 699/*
b5c334cc 700 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
f546c780 701 */
396c147e 702static char *
b5e300c2 703ls_file(char *name, struct stat *st)
704{
bc233fdf 705 int ulen, glen, sz = 0;
b5c334cc 706 struct passwd *pw;
707 struct group *gr;
708 struct tm *ltime = localtime(&st->st_mtime);
709 char *user, *group;
710 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
711
712 strmode(st->st_mode, mode);
713 if ((pw = getpwuid(st->st_uid)) != NULL) {
714 user = pw->pw_name;
715 } else {
716 snprintf(ubuf, sizeof ubuf, "%d", st->st_uid);
717 user = ubuf;
718 }
719 if ((gr = getgrgid(st->st_gid)) != NULL) {
720 group = gr->gr_name;
721 } else {
722 snprintf(gbuf, sizeof gbuf, "%d", st->st_gid);
723 group = gbuf;
724 }
725 if (ltime != NULL) {
726 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
727 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
728 else
729 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
730 }
731 if (sz == 0)
732 tbuf[0] = '\0';
bc233fdf 733 ulen = MAX(strlen(user), 8);
734 glen = MAX(strlen(group), 8);
735 snprintf(buf, sizeof buf, "%s %3d %-*s %-*s %8llu %s %s", mode,
736 st->st_nlink, ulen, user, glen, group,
737 (u_int64_t)st->st_size, tbuf, name);
b5e300c2 738 return xstrdup(buf);
739}
740
396c147e 741static void
b5e300c2 742process_readdir(void)
743{
744 DIR *dirp;
745 struct dirent *dp;
746 char *path;
747 int handle;
748 u_int32_t id;
749
750 id = get_int();
751 handle = get_handle();
752 TRACE("readdir id %d handle %d", id, handle);
753 dirp = handle_to_dir(handle);
754 path = handle_to_name(handle);
755 if (dirp == NULL || path == NULL) {
f546c780 756 send_status(id, SSH2_FX_FAILURE);
b5e300c2 757 } else {
b5e300c2 758 struct stat st;
759 char pathname[1024];
760 Stat *stats;
761 int nstats = 10, count = 0, i;
762 stats = xmalloc(nstats * sizeof(Stat));
763 while ((dp = readdir(dirp)) != NULL) {
764 if (count >= nstats) {
765 nstats *= 2;
766 stats = xrealloc(stats, nstats * sizeof(Stat));
767 }
768/* XXX OVERFLOW ? */
88690211 769 snprintf(pathname, sizeof pathname, "%s%s%s", path,
770 strcmp(path, "/") ? "/" : "", dp->d_name);
b5e300c2 771 if (lstat(pathname, &st) < 0)
772 continue;
b5c334cc 773 stat_to_attrib(&st, &(stats[count].attrib));
b5e300c2 774 stats[count].name = xstrdup(dp->d_name);
775 stats[count].long_name = ls_file(dp->d_name, &st);
776 count++;
777 /* send up to 100 entries in one message */
b5c334cc 778 /* XXX check packet size instead */
b5e300c2 779 if (count == 100)
780 break;
781 }
f546c780 782 if (count > 0) {
783 send_names(id, count, stats);
184eed6a 784 for (i = 0; i < count; i++) {
f546c780 785 xfree(stats[i].name);
786 xfree(stats[i].long_name);
787 }
788 } else {
789 send_status(id, SSH2_FX_EOF);
b5e300c2 790 }
791 xfree(stats);
792 }
793}
794
396c147e 795static void
b5e300c2 796process_remove(void)
797{
798 char *name;
799 u_int32_t id;
f546c780 800 int status = SSH2_FX_FAILURE;
b5e300c2 801 int ret;
802
803 id = get_int();
804 name = get_string(NULL);
805 TRACE("remove id %d name %s", id, name);
67b0facb 806 ret = unlink(name);
f546c780 807 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 808 send_status(id, status);
809 xfree(name);
810}
811
396c147e 812static void
b5e300c2 813process_mkdir(void)
814{
815 Attrib *a;
816 u_int32_t id;
817 char *name;
f546c780 818 int ret, mode, status = SSH2_FX_FAILURE;
b5e300c2 819
820 id = get_int();
821 name = get_string(NULL);
822 a = get_attrib();
f546c780 823 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
824 a->perm & 0777 : 0777;
b5e300c2 825 TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
826 ret = mkdir(name, mode);
f546c780 827 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 828 send_status(id, status);
829 xfree(name);
830}
831
396c147e 832static void
b5e300c2 833process_rmdir(void)
834{
835 u_int32_t id;
836 char *name;
837 int ret, status;
838
839 id = get_int();
840 name = get_string(NULL);
841 TRACE("rmdir id %d name %s", id, name);
842 ret = rmdir(name);
f546c780 843 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 844 send_status(id, status);
845 xfree(name);
846}
847
396c147e 848static void
b5e300c2 849process_realpath(void)
850{
851 char resolvedname[MAXPATHLEN];
852 u_int32_t id;
853 char *path;
854
855 id = get_int();
856 path = get_string(NULL);
6b523bae 857 if (path[0] == '\0') {
858 xfree(path);
859 path = xstrdup(".");
860 }
b5e300c2 861 TRACE("realpath id %d path %s", id, path);
862 if (realpath(path, resolvedname) == NULL) {
863 send_status(id, errno_to_portable(errno));
864 } else {
865 Stat s;
866 attrib_clear(&s.attrib);
867 s.name = s.long_name = resolvedname;
868 send_names(id, 1, &s);
869 }
870 xfree(path);
871}
872
396c147e 873static void
b5e300c2 874process_rename(void)
875{
876 u_int32_t id;
b5c334cc 877 struct stat st;
b5e300c2 878 char *oldpath, *newpath;
b5c334cc 879 int ret, status = SSH2_FX_FAILURE;
b5e300c2 880
881 id = get_int();
882 oldpath = get_string(NULL);
883 newpath = get_string(NULL);
884 TRACE("rename id %d old %s new %s", id, oldpath, newpath);
b5c334cc 885 /* fail if 'newpath' exists */
886 if (stat(newpath, &st) == -1) {
887 ret = rename(oldpath, newpath);
888 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
889 }
b5e300c2 890 send_status(id, status);
891 xfree(oldpath);
892 xfree(newpath);
893}
894
396c147e 895static void
3a7fe5ba 896process_readlink(void)
897{
898 u_int32_t id;
362df52e 899 int len;
3a7fe5ba 900 char link[MAXPATHLEN];
901 char *path;
902
903 id = get_int();
904 path = get_string(NULL);
905 TRACE("readlink id %d path %s", id, path);
362df52e 906 if ((len = readlink(path, link, sizeof(link) - 1)) == -1)
3a7fe5ba 907 send_status(id, errno_to_portable(errno));
908 else {
909 Stat s;
184eed6a 910
362df52e 911 link[len] = '\0';
3a7fe5ba 912 attrib_clear(&s.attrib);
913 s.name = s.long_name = link;
914 send_names(id, 1, &s);
915 }
916 xfree(path);
917}
918
396c147e 919static void
3a7fe5ba 920process_symlink(void)
921{
922 u_int32_t id;
923 struct stat st;
924 char *oldpath, *newpath;
925 int ret, status = SSH2_FX_FAILURE;
926
927 id = get_int();
928 oldpath = get_string(NULL);
929 newpath = get_string(NULL);
930 TRACE("symlink id %d old %s new %s", id, oldpath, newpath);
931 /* fail if 'newpath' exists */
932 if (stat(newpath, &st) == -1) {
933 ret = symlink(oldpath, newpath);
934 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
935 }
936 send_status(id, status);
937 xfree(oldpath);
938 xfree(newpath);
939}
940
396c147e 941static void
f546c780 942process_extended(void)
943{
944 u_int32_t id;
945 char *request;
946
947 id = get_int();
948 request = get_string(NULL);
949 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
950 xfree(request);
951}
b5e300c2 952
953/* stolen from ssh-agent */
954
396c147e 955static void
b5e300c2 956process(void)
957{
1e3b8b07 958 u_int msg_len;
959 u_int type;
960 u_char *cp;
b5e300c2 961
962 if (buffer_len(&iqueue) < 5)
963 return; /* Incomplete message. */
20905a8e 964 cp = buffer_ptr(&iqueue);
b5e300c2 965 msg_len = GET_32BIT(cp);
966 if (msg_len > 256 * 1024) {
967 error("bad message ");
968 exit(11);
969 }
970 if (buffer_len(&iqueue) < msg_len + 4)
971 return;
972 buffer_consume(&iqueue, 4);
973 type = buffer_get_char(&iqueue);
974 switch (type) {
f546c780 975 case SSH2_FXP_INIT:
b5e300c2 976 process_init();
977 break;
f546c780 978 case SSH2_FXP_OPEN:
b5e300c2 979 process_open();
980 break;
f546c780 981 case SSH2_FXP_CLOSE:
b5e300c2 982 process_close();
983 break;
f546c780 984 case SSH2_FXP_READ:
b5e300c2 985 process_read();
986 break;
f546c780 987 case SSH2_FXP_WRITE:
b5e300c2 988 process_write();
989 break;
f546c780 990 case SSH2_FXP_LSTAT:
b5e300c2 991 process_lstat();
992 break;
f546c780 993 case SSH2_FXP_FSTAT:
b5e300c2 994 process_fstat();
995 break;
f546c780 996 case SSH2_FXP_SETSTAT:
b5e300c2 997 process_setstat();
998 break;
f546c780 999 case SSH2_FXP_FSETSTAT:
b5e300c2 1000 process_fsetstat();
1001 break;
f546c780 1002 case SSH2_FXP_OPENDIR:
b5e300c2 1003 process_opendir();
1004 break;
f546c780 1005 case SSH2_FXP_READDIR:
b5e300c2 1006 process_readdir();
1007 break;
f546c780 1008 case SSH2_FXP_REMOVE:
b5e300c2 1009 process_remove();
1010 break;
f546c780 1011 case SSH2_FXP_MKDIR:
b5e300c2 1012 process_mkdir();
1013 break;
f546c780 1014 case SSH2_FXP_RMDIR:
b5e300c2 1015 process_rmdir();
1016 break;
f546c780 1017 case SSH2_FXP_REALPATH:
b5e300c2 1018 process_realpath();
1019 break;
f546c780 1020 case SSH2_FXP_STAT:
b5e300c2 1021 process_stat();
1022 break;
f546c780 1023 case SSH2_FXP_RENAME:
b5e300c2 1024 process_rename();
1025 break;
3a7fe5ba 1026 case SSH2_FXP_READLINK:
1027 process_readlink();
1028 break;
1029 case SSH2_FXP_SYMLINK:
1030 process_symlink();
1031 break;
f546c780 1032 case SSH2_FXP_EXTENDED:
1033 process_extended();
1034 break;
b5e300c2 1035 default:
1036 error("Unknown message %d", type);
1037 break;
1038 }
1039}
1040
1041int
1042main(int ac, char **av)
1043{
c8d75031 1044 fd_set *rset, *wset;
b5e300c2 1045 int in, out, max;
c8d75031 1046 ssize_t len, olen, set_size;
b5e300c2 1047
61b3a2bc 1048 /* XXX should use getopt */
1049
260d427b 1050 __progname = get_progname(av[0]);
b5e300c2 1051 handle_init();
1052
b5c334cc 1053#ifdef DEBUG_SFTP_SERVER
2b87da3b 1054 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
b5c334cc 1055#endif
f546c780 1056
b5e300c2 1057 in = dup(STDIN_FILENO);
1058 out = dup(STDOUT_FILENO);
1059
fe56c12b 1060#ifdef HAVE_CYGWIN
1061 setmode(in, O_BINARY);
1062 setmode(out, O_BINARY);
1063#endif
1064
b5e300c2 1065 max = 0;
1066 if (in > max)
1067 max = in;
1068 if (out > max)
1069 max = out;
1070
1071 buffer_init(&iqueue);
1072 buffer_init(&oqueue);
1073
c8d75031 1074 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1075 rset = (fd_set *)xmalloc(set_size);
1076 wset = (fd_set *)xmalloc(set_size);
1077
b5e300c2 1078 for (;;) {
c8d75031 1079 memset(rset, 0, set_size);
1080 memset(wset, 0, set_size);
b5e300c2 1081
c8d75031 1082 FD_SET(in, rset);
b5e300c2 1083 olen = buffer_len(&oqueue);
1084 if (olen > 0)
c8d75031 1085 FD_SET(out, wset);
b5e300c2 1086
c8d75031 1087 if (select(max+1, rset, wset, NULL, NULL) < 0) {
b5e300c2 1088 if (errno == EINTR)
1089 continue;
1090 exit(2);
1091 }
1092
1093 /* copy stdin to iqueue */
c8d75031 1094 if (FD_ISSET(in, rset)) {
b5e300c2 1095 char buf[4*4096];
1096 len = read(in, buf, sizeof buf);
1097 if (len == 0) {
1098 debug("read eof");
1099 exit(0);
1100 } else if (len < 0) {
1101 error("read error");
1102 exit(1);
1103 } else {
1104 buffer_append(&iqueue, buf, len);
1105 }
1106 }
1107 /* send oqueue to stdout */
c8d75031 1108 if (FD_ISSET(out, wset)) {
b5e300c2 1109 len = write(out, buffer_ptr(&oqueue), olen);
1110 if (len < 0) {
1111 error("write error");
1112 exit(1);
1113 } else {
1114 buffer_consume(&oqueue, len);
1115 }
1116 }
1117 /* process requests from client */
1118 process();
1119 }
1120}
This page took 0.545126 seconds and 5 git commands to generate.