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