]> andersk Git - openssh.git/blame - sftp-server.c
- jmc@cvs.openbsd.org 2008/01/31 20:06:50
[openssh.git] / sftp-server.c
CommitLineData
b4bbe43c 1/* $OpenBSD: sftp-server.c,v 1.75 2008/01/21 17:24:30 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);
b5e300c2 490 send_msg(&msg);
491 buffer_free(&msg);
492}
493
396c147e 494static void
b5e300c2 495process_open(void)
496{
497 u_int32_t id, pflags;
498 Attrib *a;
499 char *name;
f546c780 500 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
b5e300c2 501
502 id = get_int();
503 name = get_string(NULL);
f546c780 504 pflags = get_int(); /* portable flags */
26ddd377 505 debug3("request %u: open flags %d", id, pflags);
b5e300c2 506 a = get_attrib();
507 flags = flags_from_portable(pflags);
f546c780 508 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
a13880bb 509 logit("open \"%s\" flags %s mode 0%o",
510 name, string_from_portable(pflags), mode);
b5e300c2 511 fd = open(name, flags, mode);
512 if (fd < 0) {
513 status = errno_to_portable(errno);
514 } else {
3e2f2431 515 handle = handle_new(HANDLE_FILE, name, fd, NULL);
b5e300c2 516 if (handle < 0) {
517 close(fd);
518 } else {
519 send_handle(id, handle);
f546c780 520 status = SSH2_FX_OK;
b5e300c2 521 }
522 }
f546c780 523 if (status != SSH2_FX_OK)
b5e300c2 524 send_status(id, status);
525 xfree(name);
526}
527
396c147e 528static void
b5e300c2 529process_close(void)
530{
531 u_int32_t id;
f546c780 532 int handle, ret, status = SSH2_FX_FAILURE;
b5e300c2 533
534 id = get_int();
535 handle = get_handle();
a13880bb 536 debug3("request %u: close handle %u", id, handle);
537 handle_log_close(handle, NULL);
b5e300c2 538 ret = handle_close(handle);
f546c780 539 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 540 send_status(id, status);
541}
542
396c147e 543static void
b5e300c2 544process_read(void)
545{
546 char buf[64*1024];
f546c780 547 u_int32_t id, len;
548 int handle, fd, ret, status = SSH2_FX_FAILURE;
b5e300c2 549 u_int64_t off;
550
551 id = get_int();
552 handle = get_handle();
f546c780 553 off = get_int64();
b5e300c2 554 len = get_int();
555
a13880bb 556 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
557 id, handle_to_name(handle), handle, (unsigned long long)off, len);
b5e300c2 558 if (len > sizeof buf) {
559 len = sizeof buf;
a13880bb 560 debug2("read change len %d", len);
b5e300c2 561 }
562 fd = handle_to_fd(handle);
563 if (fd >= 0) {
564 if (lseek(fd, off, SEEK_SET) < 0) {
565 error("process_read: seek failed");
566 status = errno_to_portable(errno);
567 } else {
568 ret = read(fd, buf, len);
569 if (ret < 0) {
570 status = errno_to_portable(errno);
571 } else if (ret == 0) {
f546c780 572 status = SSH2_FX_EOF;
b5e300c2 573 } else {
574 send_data(id, buf, ret);
f546c780 575 status = SSH2_FX_OK;
a13880bb 576 handle_update_read(handle, ret);
b5e300c2 577 }
578 }
579 }
f546c780 580 if (status != SSH2_FX_OK)
b5e300c2 581 send_status(id, status);
582}
583
396c147e 584static void
b5e300c2 585process_write(void)
586{
f546c780 587 u_int32_t id;
b5e300c2 588 u_int64_t off;
bcbf86ec 589 u_int len;
f546c780 590 int handle, fd, ret, status = SSH2_FX_FAILURE;
b5e300c2 591 char *data;
592
593 id = get_int();
594 handle = get_handle();
f546c780 595 off = get_int64();
b5e300c2 596 data = get_string(&len);
597
a13880bb 598 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
599 id, handle_to_name(handle), handle, (unsigned long long)off, len);
b5e300c2 600 fd = handle_to_fd(handle);
601 if (fd >= 0) {
602 if (lseek(fd, off, SEEK_SET) < 0) {
603 status = errno_to_portable(errno);
604 error("process_write: seek failed");
605 } else {
606/* XXX ATOMICIO ? */
607 ret = write(fd, data, len);
2ceb8101 608 if (ret < 0) {
b5e300c2 609 error("process_write: write failed");
610 status = errno_to_portable(errno);
2ceb8101 611 } else if ((size_t)ret == len) {
f546c780 612 status = SSH2_FX_OK;
a13880bb 613 handle_update_write(handle, ret);
b5e300c2 614 } else {
a13880bb 615 debug2("nothing at all written");
b5e300c2 616 }
617 }
618 }
619 send_status(id, status);
620 xfree(data);
621}
622
396c147e 623static void
b5e300c2 624process_do_stat(int do_lstat)
625{
b5c334cc 626 Attrib a;
b5e300c2 627 struct stat st;
628 u_int32_t id;
629 char *name;
f546c780 630 int ret, status = SSH2_FX_FAILURE;
b5e300c2 631
632 id = get_int();
633 name = get_string(NULL);
a13880bb 634 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
635 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
b5e300c2 636 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
637 if (ret < 0) {
638 status = errno_to_portable(errno);
639 } else {
b5c334cc 640 stat_to_attrib(&st, &a);
641 send_attrib(id, &a);
f546c780 642 status = SSH2_FX_OK;
b5e300c2 643 }
f546c780 644 if (status != SSH2_FX_OK)
b5e300c2 645 send_status(id, status);
646 xfree(name);
647}
648
396c147e 649static void
b5e300c2 650process_stat(void)
651{
652 process_do_stat(0);
653}
654
396c147e 655static void
b5e300c2 656process_lstat(void)
657{
658 process_do_stat(1);
659}
660
396c147e 661static void
b5e300c2 662process_fstat(void)
663{
b5c334cc 664 Attrib a;
b5e300c2 665 struct stat st;
666 u_int32_t id;
f546c780 667 int fd, ret, handle, status = SSH2_FX_FAILURE;
b5e300c2 668
669 id = get_int();
670 handle = get_handle();
a13880bb 671 debug("request %u: fstat \"%s\" (handle %u)",
672 id, handle_to_name(handle), handle);
b5e300c2 673 fd = handle_to_fd(handle);
9a24ac07 674 if (fd >= 0) {
b5e300c2 675 ret = fstat(fd, &st);
676 if (ret < 0) {
677 status = errno_to_portable(errno);
678 } else {
b5c334cc 679 stat_to_attrib(&st, &a);
680 send_attrib(id, &a);
f546c780 681 status = SSH2_FX_OK;
b5e300c2 682 }
683 }
f546c780 684 if (status != SSH2_FX_OK)
b5e300c2 685 send_status(id, status);
686}
687
396c147e 688static struct timeval *
b6c7b7b7 689attrib_to_tv(const Attrib *a)
b5e300c2 690{
691 static struct timeval tv[2];
dce9bac5 692
b5e300c2 693 tv[0].tv_sec = a->atime;
694 tv[0].tv_usec = 0;
695 tv[1].tv_sec = a->mtime;
696 tv[1].tv_usec = 0;
697 return tv;
698}
699
396c147e 700static void
b5e300c2 701process_setstat(void)
702{
703 Attrib *a;
704 u_int32_t id;
705 char *name;
9906a836 706 int status = SSH2_FX_OK, ret;
b5e300c2 707
708 id = get_int();
709 name = get_string(NULL);
710 a = get_attrib();
a13880bb 711 debug("request %u: setstat name \"%s\"", id, name);
cb476289 712 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
23983bf9 713 logit("set \"%s\" size %llu",
714 name, (unsigned long long)a->size);
cb476289 715 ret = truncate(name, a->size);
716 if (ret == -1)
717 status = errno_to_portable(errno);
718 }
f546c780 719 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
a13880bb 720 logit("set \"%s\" mode %04o", name, a->perm);
b5e300c2 721 ret = chmod(name, a->perm & 0777);
722 if (ret == -1)
723 status = errno_to_portable(errno);
724 }
f546c780 725 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
a13880bb 726 char buf[64];
727 time_t t = a->mtime;
728
729 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
730 localtime(&t));
731 logit("set \"%s\" modtime %s", name, buf);
b5e300c2 732 ret = utimes(name, attrib_to_tv(a));
733 if (ret == -1)
734 status = errno_to_portable(errno);
735 }
408ba72f 736 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
a13880bb 737 logit("set \"%s\" owner %lu group %lu", name,
738 (u_long)a->uid, (u_long)a->gid);
408ba72f 739 ret = chown(name, a->uid, a->gid);
740 if (ret == -1)
741 status = errno_to_portable(errno);
742 }
b5e300c2 743 send_status(id, status);
744 xfree(name);
745}
746
396c147e 747static void
b5e300c2 748process_fsetstat(void)
749{
750 Attrib *a;
751 u_int32_t id;
752 int handle, fd, ret;
f546c780 753 int status = SSH2_FX_OK;
16e538d4 754
b5e300c2 755 id = get_int();
756 handle = get_handle();
757 a = get_attrib();
a13880bb 758 debug("request %u: fsetstat handle %d", id, handle);
b5e300c2 759 fd = handle_to_fd(handle);
a13880bb 760 if (fd < 0) {
f546c780 761 status = SSH2_FX_FAILURE;
b5e300c2 762 } else {
a13880bb 763 char *name = handle_to_name(handle);
764
cb476289 765 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
23983bf9 766 logit("set \"%s\" size %llu",
767 name, (unsigned long long)a->size);
cb476289 768 ret = ftruncate(fd, a->size);
769 if (ret == -1)
770 status = errno_to_portable(errno);
771 }
f546c780 772 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
a13880bb 773 logit("set \"%s\" mode %04o", name, a->perm);
2fd3c144 774#ifdef HAVE_FCHMOD
b5e300c2 775 ret = fchmod(fd, a->perm & 0777);
2fd3c144 776#else
c33f0b36 777 ret = chmod(name, a->perm & 0777);
2fd3c144 778#endif
b5e300c2 779 if (ret == -1)
780 status = errno_to_portable(errno);
781 }
f546c780 782 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
a13880bb 783 char buf[64];
784 time_t t = a->mtime;
785
786 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
787 localtime(&t));
788 logit("set \"%s\" modtime %s", name, buf);
b5e300c2 789#ifdef HAVE_FUTIMES
790 ret = futimes(fd, attrib_to_tv(a));
791#else
792 ret = utimes(name, attrib_to_tv(a));
793#endif
794 if (ret == -1)
795 status = errno_to_portable(errno);
796 }
408ba72f 797 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
a13880bb 798 logit("set \"%s\" owner %lu group %lu", name,
799 (u_long)a->uid, (u_long)a->gid);
01f13020 800#ifdef HAVE_FCHOWN
408ba72f 801 ret = fchown(fd, a->uid, a->gid);
01f13020 802#else
803 ret = chown(name, a->uid, a->gid);
804#endif
408ba72f 805 if (ret == -1)
806 status = errno_to_portable(errno);
807 }
b5e300c2 808 }
809 send_status(id, status);
810}
811
396c147e 812static void
b5e300c2 813process_opendir(void)
814{
815 DIR *dirp = NULL;
816 char *path;
f546c780 817 int handle, status = SSH2_FX_FAILURE;
b5e300c2 818 u_int32_t id;
819
820 id = get_int();
821 path = get_string(NULL);
a13880bb 822 debug3("request %u: opendir", id);
823 logit("opendir \"%s\"", path);
2b87da3b 824 dirp = opendir(path);
b5e300c2 825 if (dirp == NULL) {
826 status = errno_to_portable(errno);
827 } else {
3e2f2431 828 handle = handle_new(HANDLE_DIR, path, 0, dirp);
b5e300c2 829 if (handle < 0) {
830 closedir(dirp);
831 } else {
832 send_handle(id, handle);
f546c780 833 status = SSH2_FX_OK;
b5e300c2 834 }
2b87da3b 835
b5e300c2 836 }
f546c780 837 if (status != SSH2_FX_OK)
b5e300c2 838 send_status(id, status);
839 xfree(path);
840}
841
396c147e 842static void
b5e300c2 843process_readdir(void)
844{
845 DIR *dirp;
846 struct dirent *dp;
847 char *path;
848 int handle;
849 u_int32_t id;
850
851 id = get_int();
852 handle = get_handle();
a13880bb 853 debug("request %u: readdir \"%s\" (handle %d)", id,
854 handle_to_name(handle), handle);
b5e300c2 855 dirp = handle_to_dir(handle);
856 path = handle_to_name(handle);
857 if (dirp == NULL || path == NULL) {
f546c780 858 send_status(id, SSH2_FX_FAILURE);
b5e300c2 859 } else {
b5e300c2 860 struct stat st;
a13880bb 861 char pathname[MAXPATHLEN];
b5e300c2 862 Stat *stats;
863 int nstats = 10, count = 0, i;
9906a836 864
52e3daed 865 stats = xcalloc(nstats, sizeof(Stat));
b5e300c2 866 while ((dp = readdir(dirp)) != NULL) {
867 if (count >= nstats) {
868 nstats *= 2;
c5d10563 869 stats = xrealloc(stats, nstats, sizeof(Stat));
b5e300c2 870 }
871/* XXX OVERFLOW ? */
88690211 872 snprintf(pathname, sizeof pathname, "%s%s%s", path,
873 strcmp(path, "/") ? "/" : "", dp->d_name);
b5e300c2 874 if (lstat(pathname, &st) < 0)
875 continue;
b5c334cc 876 stat_to_attrib(&st, &(stats[count].attrib));
b5e300c2 877 stats[count].name = xstrdup(dp->d_name);
00b3ad3e 878 stats[count].long_name = ls_file(dp->d_name, &st, 0);
b5e300c2 879 count++;
880 /* send up to 100 entries in one message */
b5c334cc 881 /* XXX check packet size instead */
b5e300c2 882 if (count == 100)
883 break;
884 }
f546c780 885 if (count > 0) {
886 send_names(id, count, stats);
184eed6a 887 for (i = 0; i < count; i++) {
f546c780 888 xfree(stats[i].name);
889 xfree(stats[i].long_name);
890 }
891 } else {
892 send_status(id, SSH2_FX_EOF);
b5e300c2 893 }
894 xfree(stats);
895 }
896}
897
396c147e 898static void
b5e300c2 899process_remove(void)
900{
901 char *name;
902 u_int32_t id;
f546c780 903 int status = SSH2_FX_FAILURE;
b5e300c2 904 int ret;
905
906 id = get_int();
907 name = get_string(NULL);
a13880bb 908 debug3("request %u: remove", id);
909 logit("remove name \"%s\"", name);
67b0facb 910 ret = unlink(name);
f546c780 911 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 912 send_status(id, status);
913 xfree(name);
914}
915
396c147e 916static void
b5e300c2 917process_mkdir(void)
918{
919 Attrib *a;
920 u_int32_t id;
921 char *name;
f546c780 922 int ret, mode, status = SSH2_FX_FAILURE;
b5e300c2 923
924 id = get_int();
925 name = get_string(NULL);
926 a = get_attrib();
f546c780 927 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
928 a->perm & 0777 : 0777;
a13880bb 929 debug3("request %u: mkdir", id);
930 logit("mkdir name \"%s\" mode 0%o", name, mode);
b5e300c2 931 ret = mkdir(name, mode);
f546c780 932 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 933 send_status(id, status);
934 xfree(name);
935}
936
396c147e 937static void
b5e300c2 938process_rmdir(void)
939{
940 u_int32_t id;
941 char *name;
942 int ret, status;
943
944 id = get_int();
945 name = get_string(NULL);
a13880bb 946 debug3("request %u: rmdir", id);
947 logit("rmdir name \"%s\"", name);
b5e300c2 948 ret = rmdir(name);
f546c780 949 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 950 send_status(id, status);
951 xfree(name);
952}
953
396c147e 954static void
b5e300c2 955process_realpath(void)
956{
957 char resolvedname[MAXPATHLEN];
958 u_int32_t id;
959 char *path;
960
961 id = get_int();
962 path = get_string(NULL);
6b523bae 963 if (path[0] == '\0') {
964 xfree(path);
965 path = xstrdup(".");
966 }
a13880bb 967 debug3("request %u: realpath", id);
968 verbose("realpath \"%s\"", path);
b5e300c2 969 if (realpath(path, resolvedname) == NULL) {
970 send_status(id, errno_to_portable(errno));
971 } else {
972 Stat s;
973 attrib_clear(&s.attrib);
974 s.name = s.long_name = resolvedname;
975 send_names(id, 1, &s);
976 }
977 xfree(path);
978}
979
396c147e 980static void
b5e300c2 981process_rename(void)
982{
983 u_int32_t id;
984 char *oldpath, *newpath;
f2f28f1f 985 int status;
fd77a40f 986 struct stat sb;
b5e300c2 987
988 id = get_int();
989 oldpath = get_string(NULL);
990 newpath = get_string(NULL);
a13880bb 991 debug3("request %u: rename", id);
992 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
fd77a40f 993 status = SSH2_FX_FAILURE;
994 if (lstat(oldpath, &sb) == -1)
f2f28f1f 995 status = errno_to_portable(errno);
fd77a40f 996 else if (S_ISREG(sb.st_mode)) {
997 /* Race-free rename of regular files */
dc5888bf 998 if (link(oldpath, newpath) == -1) {
cd698186 999 if (errno == EOPNOTSUPP
1000#ifdef LINK_OPNOTSUPP_ERRNO
1001 || errno == LINK_OPNOTSUPP_ERRNO
1002#endif
1003 ) {
dc5888bf 1004 struct stat st;
1005
1006 /*
1007 * fs doesn't support links, so fall back to
1008 * stat+rename. This is racy.
1009 */
1010 if (stat(newpath, &st) == -1) {
1011 if (rename(oldpath, newpath) == -1)
1012 status =
1013 errno_to_portable(errno);
1014 else
1015 status = SSH2_FX_OK;
1016 }
1017 } else {
1018 status = errno_to_portable(errno);
1019 }
1020 } else if (unlink(oldpath) == -1) {
fd77a40f 1021 status = errno_to_portable(errno);
1022 /* clean spare link */
1023 unlink(newpath);
1024 } else
1025 status = SSH2_FX_OK;
1026 } else if (stat(newpath, &sb) == -1) {
1027 if (rename(oldpath, newpath) == -1)
1028 status = errno_to_portable(errno);
1029 else
1030 status = SSH2_FX_OK;
1031 }
b5e300c2 1032 send_status(id, status);
1033 xfree(oldpath);
1034 xfree(newpath);
1035}
1036
396c147e 1037static void
3a7fe5ba 1038process_readlink(void)
1039{
1040 u_int32_t id;
362df52e 1041 int len;
ca75d7de 1042 char buf[MAXPATHLEN];
3a7fe5ba 1043 char *path;
1044
1045 id = get_int();
1046 path = get_string(NULL);
a13880bb 1047 debug3("request %u: readlink", id);
1048 verbose("readlink \"%s\"", path);
ca75d7de 1049 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
3a7fe5ba 1050 send_status(id, errno_to_portable(errno));
1051 else {
1052 Stat s;
184eed6a 1053
ca75d7de 1054 buf[len] = '\0';
3a7fe5ba 1055 attrib_clear(&s.attrib);
ca75d7de 1056 s.name = s.long_name = buf;
3a7fe5ba 1057 send_names(id, 1, &s);
1058 }
1059 xfree(path);
1060}
1061
396c147e 1062static void
3a7fe5ba 1063process_symlink(void)
1064{
1065 u_int32_t id;
3a7fe5ba 1066 char *oldpath, *newpath;
f2f28f1f 1067 int ret, status;
3a7fe5ba 1068
1069 id = get_int();
1070 oldpath = get_string(NULL);
1071 newpath = get_string(NULL);
a13880bb 1072 debug3("request %u: symlink", id);
1073 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
f2f28f1f 1074 /* this will fail if 'newpath' exists */
1075 ret = symlink(oldpath, newpath);
1076 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
3a7fe5ba 1077 send_status(id, status);
1078 xfree(oldpath);
1079 xfree(newpath);
1080}
1081
396c147e 1082static void
f546c780 1083process_extended(void)
1084{
1085 u_int32_t id;
1086 char *request;
1087
1088 id = get_int();
1089 request = get_string(NULL);
1090 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1091 xfree(request);
1092}
b5e300c2 1093
1094/* stolen from ssh-agent */
1095
396c147e 1096static void
b5e300c2 1097process(void)
1098{
1e3b8b07 1099 u_int msg_len;
9b4ac641 1100 u_int buf_len;
1101 u_int consumed;
1e3b8b07 1102 u_int type;
1103 u_char *cp;
b5e300c2 1104
9b4ac641 1105 buf_len = buffer_len(&iqueue);
1106 if (buf_len < 5)
b5e300c2 1107 return; /* Incomplete message. */
20905a8e 1108 cp = buffer_ptr(&iqueue);
51e7a012 1109 msg_len = get_u32(cp);
3eee3b86 1110 if (msg_len > SFTP_MAX_MSG_LENGTH) {
a13880bb 1111 error("bad message from %s local user %s",
1112 client_addr, pw->pw_name);
1113 cleanup_exit(11);
b5e300c2 1114 }
9b4ac641 1115 if (buf_len < msg_len + 4)
b5e300c2 1116 return;
1117 buffer_consume(&iqueue, 4);
9b4ac641 1118 buf_len -= 4;
b5e300c2 1119 type = buffer_get_char(&iqueue);
1120 switch (type) {
f546c780 1121 case SSH2_FXP_INIT:
b5e300c2 1122 process_init();
1123 break;
f546c780 1124 case SSH2_FXP_OPEN:
b5e300c2 1125 process_open();
1126 break;
f546c780 1127 case SSH2_FXP_CLOSE:
b5e300c2 1128 process_close();
1129 break;
f546c780 1130 case SSH2_FXP_READ:
b5e300c2 1131 process_read();
1132 break;
f546c780 1133 case SSH2_FXP_WRITE:
b5e300c2 1134 process_write();
1135 break;
f546c780 1136 case SSH2_FXP_LSTAT:
b5e300c2 1137 process_lstat();
1138 break;
f546c780 1139 case SSH2_FXP_FSTAT:
b5e300c2 1140 process_fstat();
1141 break;
f546c780 1142 case SSH2_FXP_SETSTAT:
b5e300c2 1143 process_setstat();
1144 break;
f546c780 1145 case SSH2_FXP_FSETSTAT:
b5e300c2 1146 process_fsetstat();
1147 break;
f546c780 1148 case SSH2_FXP_OPENDIR:
b5e300c2 1149 process_opendir();
1150 break;
f546c780 1151 case SSH2_FXP_READDIR:
b5e300c2 1152 process_readdir();
1153 break;
f546c780 1154 case SSH2_FXP_REMOVE:
b5e300c2 1155 process_remove();
1156 break;
f546c780 1157 case SSH2_FXP_MKDIR:
b5e300c2 1158 process_mkdir();
1159 break;
f546c780 1160 case SSH2_FXP_RMDIR:
b5e300c2 1161 process_rmdir();
1162 break;
f546c780 1163 case SSH2_FXP_REALPATH:
b5e300c2 1164 process_realpath();
1165 break;
f546c780 1166 case SSH2_FXP_STAT:
b5e300c2 1167 process_stat();
1168 break;
f546c780 1169 case SSH2_FXP_RENAME:
b5e300c2 1170 process_rename();
1171 break;
3a7fe5ba 1172 case SSH2_FXP_READLINK:
1173 process_readlink();
1174 break;
1175 case SSH2_FXP_SYMLINK:
1176 process_symlink();
1177 break;
f546c780 1178 case SSH2_FXP_EXTENDED:
1179 process_extended();
1180 break;
b5e300c2 1181 default:
1182 error("Unknown message %d", type);
1183 break;
1184 }
9b4ac641 1185 /* discard the remaining bytes from the current packet */
1186 if (buf_len < buffer_len(&iqueue))
a13880bb 1187 fatal("iqueue grew unexpectedly");
9b4ac641 1188 consumed = buf_len - buffer_len(&iqueue);
1189 if (msg_len < consumed)
1190 fatal("msg_len %d < consumed %d", msg_len, consumed);
1191 if (msg_len > consumed)
1192 buffer_consume(&iqueue, msg_len - consumed);
b5e300c2 1193}
1194
a13880bb 1195/* Cleanup handler that logs active handles upon normal exit */
1196void
1197cleanup_exit(int i)
1198{
1199 if (pw != NULL && client_addr != NULL) {
1200 handle_log_exit();
1201 logit("session closed for local user %s from [%s]",
1202 pw->pw_name, client_addr);
1203 }
1204 _exit(i);
1205}
1206
1207static void
1208usage(void)
1209{
1210 extern char *__progname;
1211
1212 fprintf(stderr,
1213 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1214 exit(1);
1215}
1216
b5e300c2 1217int
a13880bb 1218main(int argc, char **argv)
b5e300c2 1219{
c8d75031 1220 fd_set *rset, *wset;
a13880bb 1221 int in, out, max, ch, skipargs = 0, log_stderr = 0;
c8d75031 1222 ssize_t len, olen, set_size;
a13880bb 1223 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
72dea2d9 1224 char *cp, buf[4*4096];
a13880bb 1225
a13880bb 1226 extern char *optarg;
1227 extern char *__progname;
b5e300c2 1228
fd6168c1 1229 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1230 sanitise_stdfd();
1231
a13880bb 1232 __progname = ssh_get_progname(argv[0]);
1233 log_init(__progname, log_level, log_facility, log_stderr);
1234
1235 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1236 switch (ch) {
1237 case 'c':
1238 /*
1239 * Ignore all arguments if we are invoked as a
31652869 1240 * shell using "sftp-server -c command"
a13880bb 1241 */
1242 skipargs = 1;
1243 break;
1244 case 'e':
1245 log_stderr = 1;
1246 break;
1247 case 'l':
1248 log_level = log_level_number(optarg);
1249 if (log_level == SYSLOG_LEVEL_NOT_SET)
1250 error("Invalid log level \"%s\"", optarg);
1251 break;
1252 case 'f':
1253 log_facility = log_facility_number(optarg);
f8f7ecf5 1254 if (log_facility == SYSLOG_FACILITY_NOT_SET)
a13880bb 1255 error("Invalid log facility \"%s\"", optarg);
1256 break;
1257 case 'h':
1258 default:
1259 usage();
1260 }
1261 }
61b3a2bc 1262
a13880bb 1263 log_init(__progname, log_level, log_facility, log_stderr);
b5e300c2 1264
a13880bb 1265 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1266 client_addr = xstrdup(cp);
1267 if ((cp = strchr(client_addr, ' ')) == NULL)
1268 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1269 getenv("SSH_CONNECTION"));
1270 *cp = '\0';
1271 } else
1272 client_addr = xstrdup("UNKNOWN");
1273
1274 if ((pw = getpwuid(getuid())) == NULL)
1275 fatal("No user found for uid %lu", (u_long)getuid());
1276 pw = pwcopy(pw);
1277
1278 logit("session opened for local user %s from [%s]",
1279 pw->pw_name, client_addr);
1280
b5e300c2 1281 in = dup(STDIN_FILENO);
1282 out = dup(STDOUT_FILENO);
1283
fe56c12b 1284#ifdef HAVE_CYGWIN
1285 setmode(in, O_BINARY);
1286 setmode(out, O_BINARY);
1287#endif
1288
b5e300c2 1289 max = 0;
1290 if (in > max)
1291 max = in;
1292 if (out > max)
1293 max = out;
1294
1295 buffer_init(&iqueue);
1296 buffer_init(&oqueue);
1297
c8d75031 1298 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1299 rset = (fd_set *)xmalloc(set_size);
1300 wset = (fd_set *)xmalloc(set_size);
1301
b5e300c2 1302 for (;;) {
c8d75031 1303 memset(rset, 0, set_size);
1304 memset(wset, 0, set_size);
b5e300c2 1305
72dea2d9 1306 /*
1307 * Ensure that we can read a full buffer and handle
1308 * the worst-case length packet it can generate,
1309 * otherwise apply backpressure by stopping reads.
1310 */
1311 if (buffer_check_alloc(&iqueue, sizeof(buf)) &&
1312 buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1313 FD_SET(in, rset);
1314
b5e300c2 1315 olen = buffer_len(&oqueue);
1316 if (olen > 0)
c8d75031 1317 FD_SET(out, wset);
b5e300c2 1318
c8d75031 1319 if (select(max+1, rset, wset, NULL, NULL) < 0) {
b5e300c2 1320 if (errno == EINTR)
1321 continue;
a13880bb 1322 error("select: %s", strerror(errno));
1323 cleanup_exit(2);
b5e300c2 1324 }
1325
1326 /* copy stdin to iqueue */
c8d75031 1327 if (FD_ISSET(in, rset)) {
b5e300c2 1328 len = read(in, buf, sizeof buf);
1329 if (len == 0) {
1330 debug("read eof");
a13880bb 1331 cleanup_exit(0);
b5e300c2 1332 } else if (len < 0) {
a13880bb 1333 error("read: %s", strerror(errno));
1334 cleanup_exit(1);
b5e300c2 1335 } else {
1336 buffer_append(&iqueue, buf, len);
1337 }
1338 }
1339 /* send oqueue to stdout */
c8d75031 1340 if (FD_ISSET(out, wset)) {
b5e300c2 1341 len = write(out, buffer_ptr(&oqueue), olen);
1342 if (len < 0) {
a13880bb 1343 error("write: %s", strerror(errno));
1344 cleanup_exit(1);
b5e300c2 1345 } else {
1346 buffer_consume(&oqueue, len);
1347 }
1348 }
72dea2d9 1349
1350 /*
1351 * Process requests from client if we can fit the results
1352 * into the output buffer, otherwise stop processing input
1353 * and let the output queue drain.
1354 */
1355 if (buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1356 process();
b5e300c2 1357 }
1358}
This page took 1.42825 seconds and 5 git commands to generate.