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