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