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