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