blob: e882216b108457c4a5dc7f4fdf5768e1a60fe053 [file] [log] [blame]
Damien Millerfef95ad2006-07-10 20:46:55 +10001/* $OpenBSD: sftp-server.c,v 1.58 2006/07/06 10:47:57 djm Exp $ */
Damien Miller7b28dc52000-09-05 13:34:53 +11002/*
Darren Tucker37bd3662004-02-24 09:19:15 +11003 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
Damien Miller7b28dc52000-09-05 13:34:53 +11004 *
Darren Tucker37bd3662004-02-24 09:19:15 +11005 * 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.
Damien Miller7b28dc52000-09-05 13:34:53 +11008 *
Darren Tucker37bd3662004-02-24 09:19:15 +11009 * 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.
Damien Miller7b28dc52000-09-05 13:34:53 +110016 */
17#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110018
19#include <sys/types.h>
20#include <sys/stat.h>
Damien Miller88f254b2006-03-15 11:25:13 +110021
22#include <dirent.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110023
Damien Miller7b28dc52000-09-05 13:34:53 +110024#include "buffer.h"
25#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "log.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110027#include "xmalloc.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100028#include "misc.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100029#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110030
Ben Lindstrom2f959b42001-01-11 06:20:23 +000031#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110032#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110033
34/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000035#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110036#define get_int() buffer_get_int(&iqueue);
37#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Damien Miller7b28dc52000-09-05 13:34:53 +110038
Damien Millerfef95ad2006-07-10 20:46:55 +100039/* Our verbosity */
40LogLevel log_level = SYSLOG_LEVEL_ERROR;
41
42/* Our client */
43struct passwd *pw = NULL;
44char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000045
Damien Miller7b28dc52000-09-05 13:34:53 +110046/* input and output queue */
47Buffer iqueue;
48Buffer oqueue;
49
Damien Miller058316f2001-03-08 10:08:49 +110050/* Version of client */
51int version;
52
Darren Tuckera6612d42003-06-28 12:39:03 +100053/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110054
Damien Miller7b28dc52000-09-05 13:34:53 +110055typedef struct Stat Stat;
56
Damien Miller33804262001-02-04 23:20:18 +110057struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110058 char *name;
59 char *long_name;
60 Attrib attrib;
61};
62
Ben Lindstrombba81212001-06-25 05:01:22 +000063static int
Damien Miller7b28dc52000-09-05 13:34:53 +110064errno_to_portable(int unixerrno)
65{
66 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000067
Damien Miller7b28dc52000-09-05 13:34:53 +110068 switch (unixerrno) {
69 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000070 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110071 break;
72 case ENOENT:
73 case ENOTDIR:
74 case EBADF:
75 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000076 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110077 break;
78 case EPERM:
79 case EACCES:
80 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000081 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110082 break;
83 case ENAMETOOLONG:
84 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000085 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +110086 break;
87 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000088 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +110089 break;
90 }
91 return ret;
92}
93
Ben Lindstrombba81212001-06-25 05:01:22 +000094static int
Damien Miller7b28dc52000-09-05 13:34:53 +110095flags_from_portable(int pflags)
96{
97 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000098
Ben Lindstrom36592512001-03-05 05:02:08 +000099 if ((pflags & SSH2_FXF_READ) &&
100 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100101 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000102 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100103 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000104 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100105 flags = O_WRONLY;
106 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000107 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100108 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000109 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100110 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000111 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100112 flags |= O_EXCL;
113 return flags;
114}
115
Damien Millerfef95ad2006-07-10 20:46:55 +1000116static const char *
117string_from_portable(int pflags)
118{
119 static char ret[128];
120
121 *ret = '\0';
122
123#define PAPPEND(str) { \
124 if (*ret != '\0') \
125 strlcat(ret, ",", sizeof(ret)); \
126 strlcat(ret, str, sizeof(ret)); \
127 }
128
129 if (pflags & SSH2_FXF_READ)
130 PAPPEND("READ")
131 if (pflags & SSH2_FXF_WRITE)
132 PAPPEND("WRITE")
133 if (pflags & SSH2_FXF_CREAT)
134 PAPPEND("CREATE")
135 if (pflags & SSH2_FXF_TRUNC)
136 PAPPEND("TRUNCATE")
137 if (pflags & SSH2_FXF_EXCL)
138 PAPPEND("EXCL")
139
140 return ret;
141}
142
Ben Lindstrombba81212001-06-25 05:01:22 +0000143static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100144get_attrib(void)
145{
146 return decode_attrib(&iqueue);
147}
148
149/* handle handles */
150
151typedef struct Handle Handle;
152struct Handle {
153 int use;
154 DIR *dirp;
155 int fd;
156 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000157 u_int64_t bytes_read, bytes_write;
Damien Miller7b28dc52000-09-05 13:34:53 +1100158};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000159
Damien Miller7b28dc52000-09-05 13:34:53 +1100160enum {
161 HANDLE_UNUSED,
162 HANDLE_DIR,
163 HANDLE_FILE
164};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000165
Damien Miller7b28dc52000-09-05 13:34:53 +1100166Handle handles[100];
167
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100169handle_init(void)
170{
Damien Millereccb9de2005-06-17 12:59:34 +1000171 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000172
Damien Miller9f0f5c62001-12-21 14:45:46 +1100173 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
Damien Miller7b28dc52000-09-05 13:34:53 +1100174 handles[i].use = HANDLE_UNUSED;
175}
176
Ben Lindstrombba81212001-06-25 05:01:22 +0000177static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100178handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100179{
Damien Millereccb9de2005-06-17 12:59:34 +1000180 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000181
Damien Miller9f0f5c62001-12-21 14:45:46 +1100182 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100183 if (handles[i].use == HANDLE_UNUSED) {
184 handles[i].use = use;
185 handles[i].dirp = dirp;
186 handles[i].fd = fd;
Damien Miller00111382003-03-10 11:21:17 +1100187 handles[i].name = xstrdup(name);
Damien Millerfef95ad2006-07-10 20:46:55 +1000188 handles[i].bytes_read = handles[i].bytes_write = 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100189 return i;
190 }
191 }
192 return -1;
193}
194
Ben Lindstrombba81212001-06-25 05:01:22 +0000195static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100196handle_is_ok(int i, int type)
197{
Damien Millereccb9de2005-06-17 12:59:34 +1000198 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000199 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100200}
201
Ben Lindstrombba81212001-06-25 05:01:22 +0000202static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100203handle_to_string(int handle, char **stringp, int *hlenp)
204{
Damien Miller7b28dc52000-09-05 13:34:53 +1100205 if (stringp == NULL || hlenp == NULL)
206 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000207 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100208 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000209 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100210 return 0;
211}
212
Ben Lindstrombba81212001-06-25 05:01:22 +0000213static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100214handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100215{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000216 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000217
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000218 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100219 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100220 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100221 if (handle_is_ok(val, HANDLE_FILE) ||
222 handle_is_ok(val, HANDLE_DIR))
223 return val;
224 return -1;
225}
226
Ben Lindstrombba81212001-06-25 05:01:22 +0000227static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100228handle_to_name(int handle)
229{
230 if (handle_is_ok(handle, HANDLE_DIR)||
231 handle_is_ok(handle, HANDLE_FILE))
232 return handles[handle].name;
233 return NULL;
234}
235
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100237handle_to_dir(int handle)
238{
239 if (handle_is_ok(handle, HANDLE_DIR))
240 return handles[handle].dirp;
241 return NULL;
242}
243
Ben Lindstrombba81212001-06-25 05:01:22 +0000244static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100245handle_to_fd(int handle)
246{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000247 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100248 return handles[handle].fd;
249 return -1;
250}
251
Damien Millerfef95ad2006-07-10 20:46:55 +1000252static void
253handle_update_read(int handle, ssize_t bytes)
254{
255 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
256 handles[handle].bytes_read += bytes;
257}
258
259static void
260handle_update_write(int handle, ssize_t bytes)
261{
262 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
263 handles[handle].bytes_write += bytes;
264}
265
266static u_int64_t
267handle_bytes_read(int handle)
268{
269 if (handle_is_ok(handle, HANDLE_FILE))
270 return (handles[handle].bytes_read);
271 return 0;
272}
273
274static u_int64_t
275handle_bytes_write(int handle)
276{
277 if (handle_is_ok(handle, HANDLE_FILE))
278 return (handles[handle].bytes_write);
279 return 0;
280}
281
Ben Lindstrombba81212001-06-25 05:01:22 +0000282static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100283handle_close(int handle)
284{
285 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000286
Damien Miller7b28dc52000-09-05 13:34:53 +1100287 if (handle_is_ok(handle, HANDLE_FILE)) {
288 ret = close(handles[handle].fd);
289 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100290 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100291 } else if (handle_is_ok(handle, HANDLE_DIR)) {
292 ret = closedir(handles[handle].dirp);
293 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100294 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100295 } else {
296 errno = ENOENT;
297 }
298 return ret;
299}
300
Damien Millerfef95ad2006-07-10 20:46:55 +1000301static void
302handle_log_close(int handle, char *emsg)
303{
304 if (handle_is_ok(handle, HANDLE_FILE)) {
305 logit("%s%sclose \"%s\" bytes read %llu written %llu",
306 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
307 handle_to_name(handle),
308 handle_bytes_read(handle), handle_bytes_write(handle));
309 } else {
310 logit("%s%sclosedir \"%s\"",
311 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
312 handle_to_name(handle));
313 }
314}
315
316static void
317handle_log_exit(void)
318{
319 u_int i;
320
321 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
322 if (handles[i].use != HANDLE_UNUSED)
323 handle_log_close(i, "forced");
324}
325
Ben Lindstrombba81212001-06-25 05:01:22 +0000326static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100327get_handle(void)
328{
329 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000330 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100331 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000332
Damien Miller7b28dc52000-09-05 13:34:53 +1100333 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000334 if (hlen < 256)
335 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100336 xfree(handle);
337 return val;
338}
339
340/* send replies */
341
Ben Lindstrombba81212001-06-25 05:01:22 +0000342static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100343send_msg(Buffer *m)
344{
345 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000346
Damien Miller7b28dc52000-09-05 13:34:53 +1100347 buffer_put_int(&oqueue, mlen);
348 buffer_append(&oqueue, buffer_ptr(m), mlen);
349 buffer_consume(m, mlen);
350}
351
Damien Millerfef95ad2006-07-10 20:46:55 +1000352static const char *
353status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100354{
Damien Miller058316f2001-03-08 10:08:49 +1100355 const char *status_messages[] = {
356 "Success", /* SSH_FX_OK */
357 "End of file", /* SSH_FX_EOF */
358 "No such file", /* SSH_FX_NO_SUCH_FILE */
359 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
360 "Failure", /* SSH_FX_FAILURE */
361 "Bad message", /* SSH_FX_BAD_MESSAGE */
362 "No connection", /* SSH_FX_NO_CONNECTION */
363 "Connection lost", /* SSH_FX_CONNECTION_LOST */
364 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
365 "Unknown error" /* Others */
366 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000367 return (status_messages[MIN(status,SSH2_FX_MAX)]);
368}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000369
Damien Millerfef95ad2006-07-10 20:46:55 +1000370static void
371send_status(u_int32_t id, u_int32_t status)
372{
373 Buffer msg;
374
375 debug3("request %u: sent status %u", id, status);
376 if (log_level > SYSLOG_LEVEL_VERBOSE ||
377 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
378 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100379 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000380 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100381 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000382 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100383 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000384 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100385 buffer_put_cstring(&msg, "");
386 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100387 send_msg(&msg);
388 buffer_free(&msg);
389}
Ben Lindstrombba81212001-06-25 05:01:22 +0000390static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100391send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100392{
393 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000394
Damien Miller7b28dc52000-09-05 13:34:53 +1100395 buffer_init(&msg);
396 buffer_put_char(&msg, type);
397 buffer_put_int(&msg, id);
398 buffer_put_string(&msg, data, dlen);
399 send_msg(&msg);
400 buffer_free(&msg);
401}
402
Ben Lindstrombba81212001-06-25 05:01:22 +0000403static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100404send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100405{
Damien Millerfef95ad2006-07-10 20:46:55 +1000406 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000407 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100408}
409
Ben Lindstrombba81212001-06-25 05:01:22 +0000410static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100411send_handle(u_int32_t id, int handle)
412{
413 char *string;
414 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000415
Damien Miller7b28dc52000-09-05 13:34:53 +1100416 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000417 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000418 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100419 xfree(string);
420}
421
Ben Lindstrombba81212001-06-25 05:01:22 +0000422static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100423send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100424{
425 Buffer msg;
426 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000427
Damien Miller7b28dc52000-09-05 13:34:53 +1100428 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000429 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100430 buffer_put_int(&msg, id);
431 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000432 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100433 for (i = 0; i < count; i++) {
434 buffer_put_cstring(&msg, stats[i].name);
435 buffer_put_cstring(&msg, stats[i].long_name);
436 encode_attrib(&msg, &stats[i].attrib);
437 }
438 send_msg(&msg);
439 buffer_free(&msg);
440}
441
Ben Lindstrombba81212001-06-25 05:01:22 +0000442static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100443send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100444{
445 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000446
Damien Millerfef95ad2006-07-10 20:46:55 +1000447 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100448 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000449 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100450 buffer_put_int(&msg, id);
451 encode_attrib(&msg, a);
452 send_msg(&msg);
453 buffer_free(&msg);
454}
455
456/* parse incoming */
457
Ben Lindstrombba81212001-06-25 05:01:22 +0000458static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100459process_init(void)
460{
461 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100462
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000463 version = get_int();
Damien Millerfef95ad2006-07-10 20:46:55 +1000464 verbose("received client version %d", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100465 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000466 buffer_put_char(&msg, SSH2_FXP_VERSION);
467 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100468 send_msg(&msg);
469 buffer_free(&msg);
470}
471
Ben Lindstrombba81212001-06-25 05:01:22 +0000472static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100473process_open(void)
474{
475 u_int32_t id, pflags;
476 Attrib *a;
477 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000478 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100479
480 id = get_int();
Damien Millerfef95ad2006-07-10 20:46:55 +1000481 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100482 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000483 pflags = get_int(); /* portable flags */
Damien Miller7b28dc52000-09-05 13:34:53 +1100484 a = get_attrib();
485 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000486 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000487 logit("open \"%s\" flags %s mode 0%o",
488 name, string_from_portable(pflags), mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100489 fd = open(name, flags, mode);
490 if (fd < 0) {
491 status = errno_to_portable(errno);
492 } else {
Damien Miller00111382003-03-10 11:21:17 +1100493 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100494 if (handle < 0) {
495 close(fd);
496 } else {
497 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000498 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100499 }
500 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000501 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100502 send_status(id, status);
503 xfree(name);
504}
505
Ben Lindstrombba81212001-06-25 05:01:22 +0000506static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100507process_close(void)
508{
509 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000510 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100511
512 id = get_int();
513 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000514 debug3("request %u: close handle %u", id, handle);
515 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100516 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000517 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100518 send_status(id, status);
519}
520
Ben Lindstrombba81212001-06-25 05:01:22 +0000521static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100522process_read(void)
523{
524 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000525 u_int32_t id, len;
526 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100527 u_int64_t off;
528
529 id = get_int();
530 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000531 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100532 len = get_int();
533
Damien Millerfef95ad2006-07-10 20:46:55 +1000534 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
535 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100536 if (len > sizeof buf) {
537 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000538 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100539 }
540 fd = handle_to_fd(handle);
541 if (fd >= 0) {
542 if (lseek(fd, off, SEEK_SET) < 0) {
543 error("process_read: seek failed");
544 status = errno_to_portable(errno);
545 } else {
546 ret = read(fd, buf, len);
547 if (ret < 0) {
548 status = errno_to_portable(errno);
549 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000550 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100551 } else {
552 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000553 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000554 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100555 }
556 }
557 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000558 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100559 send_status(id, status);
560}
561
Ben Lindstrombba81212001-06-25 05:01:22 +0000562static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100563process_write(void)
564{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000565 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100566 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100567 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000568 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100569 char *data;
570
571 id = get_int();
572 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000573 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100574 data = get_string(&len);
575
Damien Millerfef95ad2006-07-10 20:46:55 +1000576 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
577 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100578 fd = handle_to_fd(handle);
579 if (fd >= 0) {
580 if (lseek(fd, off, SEEK_SET) < 0) {
581 status = errno_to_portable(errno);
582 error("process_write: seek failed");
583 } else {
584/* XXX ATOMICIO ? */
585 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000586 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100587 error("process_write: write failed");
588 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000589 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000590 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000591 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100592 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000593 debug2("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100594 }
595 }
596 }
597 send_status(id, status);
598 xfree(data);
599}
600
Ben Lindstrombba81212001-06-25 05:01:22 +0000601static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100602process_do_stat(int do_lstat)
603{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000604 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100605 struct stat st;
606 u_int32_t id;
607 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000608 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100609
610 id = get_int();
611 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000612 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
613 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100614 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
615 if (ret < 0) {
616 status = errno_to_portable(errno);
617 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000618 stat_to_attrib(&st, &a);
619 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000620 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100621 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000622 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100623 send_status(id, status);
624 xfree(name);
625}
626
Ben Lindstrombba81212001-06-25 05:01:22 +0000627static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100628process_stat(void)
629{
630 process_do_stat(0);
631}
632
Ben Lindstrombba81212001-06-25 05:01:22 +0000633static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100634process_lstat(void)
635{
636 process_do_stat(1);
637}
638
Ben Lindstrombba81212001-06-25 05:01:22 +0000639static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100640process_fstat(void)
641{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000642 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100643 struct stat st;
644 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000645 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100646
647 id = get_int();
648 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000649 debug("request %u: fstat \"%s\" (handle %u)",
650 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100651 fd = handle_to_fd(handle);
652 if (fd >= 0) {
653 ret = fstat(fd, &st);
654 if (ret < 0) {
655 status = errno_to_portable(errno);
656 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000657 stat_to_attrib(&st, &a);
658 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000659 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100660 }
661 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000662 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100663 send_status(id, status);
664}
665
Ben Lindstrombba81212001-06-25 05:01:22 +0000666static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100667attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100668{
669 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000670
Damien Miller7b28dc52000-09-05 13:34:53 +1100671 tv[0].tv_sec = a->atime;
672 tv[0].tv_usec = 0;
673 tv[1].tv_sec = a->mtime;
674 tv[1].tv_usec = 0;
675 return tv;
676}
677
Ben Lindstrombba81212001-06-25 05:01:22 +0000678static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100679process_setstat(void)
680{
681 Attrib *a;
682 u_int32_t id;
683 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000684 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100685
686 id = get_int();
687 name = get_string(NULL);
688 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000689 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100690 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000691 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100692 ret = truncate(name, a->size);
693 if (ret == -1)
694 status = errno_to_portable(errno);
695 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000696 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000697 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller7b28dc52000-09-05 13:34:53 +1100698 ret = chmod(name, a->perm & 0777);
699 if (ret == -1)
700 status = errno_to_portable(errno);
701 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000702 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000703 char buf[64];
704 time_t t = a->mtime;
705
706 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
707 localtime(&t));
708 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100709 ret = utimes(name, attrib_to_tv(a));
710 if (ret == -1)
711 status = errno_to_portable(errno);
712 }
Kevin Steves8e743932001-02-05 13:24:35 +0000713 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000714 logit("set \"%s\" owner %lu group %lu", name,
715 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000716 ret = chown(name, a->uid, a->gid);
717 if (ret == -1)
718 status = errno_to_portable(errno);
719 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100720 send_status(id, status);
721 xfree(name);
722}
723
Ben Lindstrombba81212001-06-25 05:01:22 +0000724static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100725process_fsetstat(void)
726{
727 Attrib *a;
728 u_int32_t id;
729 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000730 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000731
Damien Miller7b28dc52000-09-05 13:34:53 +1100732 id = get_int();
733 handle = get_handle();
734 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000735 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100736 fd = handle_to_fd(handle);
Damien Millerfef95ad2006-07-10 20:46:55 +1000737 if (fd < 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000738 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100739 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000740 char *name = handle_to_name(handle);
741
Damien Miller00c92172002-02-13 14:05:00 +1100742 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000743 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100744 ret = ftruncate(fd, a->size);
745 if (ret == -1)
746 status = errno_to_portable(errno);
747 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000748 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000749 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000750#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100751 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000752#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000753 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000754#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100755 if (ret == -1)
756 status = errno_to_portable(errno);
757 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000758 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000759 char buf[64];
760 time_t t = a->mtime;
761
762 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
763 localtime(&t));
764 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100765#ifdef HAVE_FUTIMES
766 ret = futimes(fd, attrib_to_tv(a));
767#else
768 ret = utimes(name, attrib_to_tv(a));
769#endif
770 if (ret == -1)
771 status = errno_to_portable(errno);
772 }
Kevin Steves8e743932001-02-05 13:24:35 +0000773 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000774 logit("set \"%s\" owner %lu group %lu", name,
775 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000776#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000777 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000778#else
779 ret = chown(name, a->uid, a->gid);
780#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000781 if (ret == -1)
782 status = errno_to_portable(errno);
783 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100784 }
785 send_status(id, status);
786}
787
Ben Lindstrombba81212001-06-25 05:01:22 +0000788static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100789process_opendir(void)
790{
791 DIR *dirp = NULL;
792 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000793 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100794 u_int32_t id;
795
796 id = get_int();
797 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000798 debug3("request %u: opendir", id);
799 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000800 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100801 if (dirp == NULL) {
802 status = errno_to_portable(errno);
803 } else {
Damien Miller00111382003-03-10 11:21:17 +1100804 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100805 if (handle < 0) {
806 closedir(dirp);
807 } else {
808 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000809 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100810 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000811
Damien Miller7b28dc52000-09-05 13:34:53 +1100812 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000813 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100814 send_status(id, status);
815 xfree(path);
816}
817
Ben Lindstrombba81212001-06-25 05:01:22 +0000818static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100819process_readdir(void)
820{
821 DIR *dirp;
822 struct dirent *dp;
823 char *path;
824 int handle;
825 u_int32_t id;
826
827 id = get_int();
828 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000829 debug("request %u: readdir \"%s\" (handle %d)", id,
830 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100831 dirp = handle_to_dir(handle);
832 path = handle_to_name(handle);
833 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000834 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100835 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100836 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +1000837 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +1100838 Stat *stats;
839 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000840
Damien Miller07d86be2006-03-26 14:19:21 +1100841 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100842 while ((dp = readdir(dirp)) != NULL) {
843 if (count >= nstats) {
844 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +1100845 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100846 }
847/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000848 snprintf(pathname, sizeof pathname, "%s%s%s", path,
849 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100850 if (lstat(pathname, &st) < 0)
851 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000852 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100853 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000854 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100855 count++;
856 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000857 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100858 if (count == 100)
859 break;
860 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000861 if (count > 0) {
862 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100863 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000864 xfree(stats[i].name);
865 xfree(stats[i].long_name);
866 }
867 } else {
868 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100869 }
870 xfree(stats);
871 }
872}
873
Ben Lindstrombba81212001-06-25 05:01:22 +0000874static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100875process_remove(void)
876{
877 char *name;
878 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000879 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100880 int ret;
881
882 id = get_int();
883 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000884 debug3("request %u: remove", id);
885 logit("remove name \"%s\"", name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000886 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000887 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100888 send_status(id, status);
889 xfree(name);
890}
891
Ben Lindstrombba81212001-06-25 05:01:22 +0000892static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100893process_mkdir(void)
894{
895 Attrib *a;
896 u_int32_t id;
897 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000898 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100899
900 id = get_int();
901 name = get_string(NULL);
902 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000903 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
904 a->perm & 0777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +1000905 debug3("request %u: mkdir", id);
906 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100907 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000908 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100909 send_status(id, status);
910 xfree(name);
911}
912
Ben Lindstrombba81212001-06-25 05:01:22 +0000913static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100914process_rmdir(void)
915{
916 u_int32_t id;
917 char *name;
918 int ret, status;
919
920 id = get_int();
921 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000922 debug3("request %u: rmdir", id);
923 logit("rmdir name \"%s\"", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100924 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000925 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100926 send_status(id, status);
927 xfree(name);
928}
929
Ben Lindstrombba81212001-06-25 05:01:22 +0000930static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100931process_realpath(void)
932{
933 char resolvedname[MAXPATHLEN];
934 u_int32_t id;
935 char *path;
936
937 id = get_int();
938 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000939 if (path[0] == '\0') {
940 xfree(path);
941 path = xstrdup(".");
942 }
Damien Millerfef95ad2006-07-10 20:46:55 +1000943 debug3("request %u: realpath", id);
944 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100945 if (realpath(path, resolvedname) == NULL) {
946 send_status(id, errno_to_portable(errno));
947 } else {
948 Stat s;
949 attrib_clear(&s.attrib);
950 s.name = s.long_name = resolvedname;
951 send_names(id, 1, &s);
952 }
953 xfree(path);
954}
955
Ben Lindstrombba81212001-06-25 05:01:22 +0000956static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100957process_rename(void)
958{
959 u_int32_t id;
960 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100961 int status;
Damien Millerb3207e82003-03-26 16:01:11 +1100962 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +1100963
964 id = get_int();
965 oldpath = get_string(NULL);
966 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000967 debug3("request %u: rename", id);
968 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +1100969 status = SSH2_FX_FAILURE;
970 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +1100971 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +1100972 else if (S_ISREG(sb.st_mode)) {
973 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000974 if (link(oldpath, newpath) == -1) {
Darren Tuckere59b5082004-06-28 16:01:19 +1000975 if (errno == EOPNOTSUPP
976#ifdef LINK_OPNOTSUPP_ERRNO
977 || errno == LINK_OPNOTSUPP_ERRNO
978#endif
979 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000980 struct stat st;
981
982 /*
983 * fs doesn't support links, so fall back to
984 * stat+rename. This is racy.
985 */
986 if (stat(newpath, &st) == -1) {
987 if (rename(oldpath, newpath) == -1)
988 status =
989 errno_to_portable(errno);
990 else
991 status = SSH2_FX_OK;
992 }
993 } else {
994 status = errno_to_portable(errno);
995 }
996 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +1100997 status = errno_to_portable(errno);
998 /* clean spare link */
999 unlink(newpath);
1000 } else
1001 status = SSH2_FX_OK;
1002 } else if (stat(newpath, &sb) == -1) {
1003 if (rename(oldpath, newpath) == -1)
1004 status = errno_to_portable(errno);
1005 else
1006 status = SSH2_FX_OK;
1007 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001008 send_status(id, status);
1009 xfree(oldpath);
1010 xfree(newpath);
1011}
1012
Ben Lindstrombba81212001-06-25 05:01:22 +00001013static void
Damien Miller058316f2001-03-08 10:08:49 +11001014process_readlink(void)
1015{
1016 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001017 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001018 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001019 char *path;
1020
1021 id = get_int();
1022 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001023 debug3("request %u: readlink", id);
1024 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001025 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001026 send_status(id, errno_to_portable(errno));
1027 else {
1028 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001029
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001030 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001031 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001032 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001033 send_names(id, 1, &s);
1034 }
1035 xfree(path);
1036}
1037
Ben Lindstrombba81212001-06-25 05:01:22 +00001038static void
Damien Miller058316f2001-03-08 10:08:49 +11001039process_symlink(void)
1040{
1041 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +11001042 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001043 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001044
1045 id = get_int();
1046 oldpath = get_string(NULL);
1047 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001048 debug3("request %u: symlink", id);
1049 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001050 /* this will fail if 'newpath' exists */
1051 ret = symlink(oldpath, newpath);
1052 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001053 send_status(id, status);
1054 xfree(oldpath);
1055 xfree(newpath);
1056}
1057
Ben Lindstrombba81212001-06-25 05:01:22 +00001058static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001059process_extended(void)
1060{
1061 u_int32_t id;
1062 char *request;
1063
1064 id = get_int();
1065 request = get_string(NULL);
1066 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1067 xfree(request);
1068}
Damien Miller7b28dc52000-09-05 13:34:53 +11001069
1070/* stolen from ssh-agent */
1071
Ben Lindstrombba81212001-06-25 05:01:22 +00001072static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001073process(void)
1074{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001075 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +00001076 u_int buf_len;
1077 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001078 u_int type;
1079 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +11001080
Ben Lindstrom2c140472002-06-06 21:57:54 +00001081 buf_len = buffer_len(&iqueue);
1082 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001083 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001084 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001085 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001086 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001087 error("bad message from %s local user %s",
1088 client_addr, pw->pw_name);
1089 cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001090 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001091 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001092 return;
1093 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001094 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001095 type = buffer_get_char(&iqueue);
1096 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001097 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001098 process_init();
1099 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001100 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +11001101 process_open();
1102 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001103 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001104 process_close();
1105 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001106 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +11001107 process_read();
1108 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001109 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001110 process_write();
1111 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001112 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001113 process_lstat();
1114 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001115 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001116 process_fstat();
1117 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001118 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001119 process_setstat();
1120 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001121 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001122 process_fsetstat();
1123 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001124 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001125 process_opendir();
1126 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001127 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001128 process_readdir();
1129 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001130 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001131 process_remove();
1132 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001133 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001134 process_mkdir();
1135 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001136 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001137 process_rmdir();
1138 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001139 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001140 process_realpath();
1141 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001142 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001143 process_stat();
1144 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001145 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001146 process_rename();
1147 break;
Damien Miller058316f2001-03-08 10:08:49 +11001148 case SSH2_FXP_READLINK:
1149 process_readlink();
1150 break;
1151 case SSH2_FXP_SYMLINK:
1152 process_symlink();
1153 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001154 case SSH2_FXP_EXTENDED:
1155 process_extended();
1156 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001157 default:
1158 error("Unknown message %d", type);
1159 break;
1160 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001161 /* discard the remaining bytes from the current packet */
1162 if (buf_len < buffer_len(&iqueue))
Damien Millerfef95ad2006-07-10 20:46:55 +10001163 fatal("iqueue grew unexpectedly");
Ben Lindstrom2c140472002-06-06 21:57:54 +00001164 consumed = buf_len - buffer_len(&iqueue);
1165 if (msg_len < consumed)
1166 fatal("msg_len %d < consumed %d", msg_len, consumed);
1167 if (msg_len > consumed)
1168 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001169}
1170
Damien Millerfef95ad2006-07-10 20:46:55 +10001171/* Cleanup handler that logs active handles upon normal exit */
1172void
1173cleanup_exit(int i)
1174{
1175 if (pw != NULL && client_addr != NULL) {
1176 handle_log_exit();
1177 logit("session closed for local user %s from [%s]",
1178 pw->pw_name, client_addr);
1179 }
1180 _exit(i);
1181}
1182
1183static void
1184usage(void)
1185{
1186 extern char *__progname;
1187
1188 fprintf(stderr,
1189 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1190 exit(1);
1191}
1192
Damien Miller7b28dc52000-09-05 13:34:53 +11001193int
Damien Millerfef95ad2006-07-10 20:46:55 +10001194main(int argc, char **argv)
Damien Miller7b28dc52000-09-05 13:34:53 +11001195{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001196 fd_set *rset, *wset;
Damien Millerfef95ad2006-07-10 20:46:55 +10001197 int in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001198 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001199 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1200 char *cp;
1201
1202 extern int optind;
1203 extern char *optarg;
1204 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001205
Darren Tuckerce321d82005-10-03 18:11:24 +10001206 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1207 sanitise_stdfd();
1208
Damien Millerfef95ad2006-07-10 20:46:55 +10001209 __progname = ssh_get_progname(argv[0]);
1210 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001211
Damien Millerfef95ad2006-07-10 20:46:55 +10001212 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1213 switch (ch) {
1214 case 'c':
1215 /*
1216 * Ignore all arguments if we are invoked as a
1217 * shell using "sftp-server -c command"
1218 */
1219 skipargs = 1;
1220 break;
1221 case 'e':
1222 log_stderr = 1;
1223 break;
1224 case 'l':
1225 log_level = log_level_number(optarg);
1226 if (log_level == SYSLOG_LEVEL_NOT_SET)
1227 error("Invalid log level \"%s\"", optarg);
1228 break;
1229 case 'f':
1230 log_facility = log_facility_number(optarg);
1231 if (log_level == SYSLOG_FACILITY_NOT_SET)
1232 error("Invalid log facility \"%s\"", optarg);
1233 break;
1234 case 'h':
1235 default:
1236 usage();
1237 }
1238 }
1239
1240 log_init(__progname, log_level, log_facility, log_stderr);
1241
1242 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1243 client_addr = xstrdup(cp);
1244 if ((cp = strchr(client_addr, ' ')) == NULL)
1245 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1246 getenv("SSH_CONNECTION"));
1247 *cp = '\0';
1248 } else
1249 client_addr = xstrdup("UNKNOWN");
1250
1251 if ((pw = getpwuid(getuid())) == NULL)
1252 fatal("No user found for uid %lu", (u_long)getuid());
1253 pw = pwcopy(pw);
1254
1255 logit("session opened for local user %s from [%s]",
1256 pw->pw_name, client_addr);
1257
Damien Miller7b28dc52000-09-05 13:34:53 +11001258 handle_init();
1259
Damien Miller7b28dc52000-09-05 13:34:53 +11001260 in = dup(STDIN_FILENO);
1261 out = dup(STDOUT_FILENO);
1262
Damien Miller402b3312001-04-14 00:28:42 +10001263#ifdef HAVE_CYGWIN
1264 setmode(in, O_BINARY);
1265 setmode(out, O_BINARY);
1266#endif
1267
Damien Miller7b28dc52000-09-05 13:34:53 +11001268 max = 0;
1269 if (in > max)
1270 max = in;
1271 if (out > max)
1272 max = out;
1273
1274 buffer_init(&iqueue);
1275 buffer_init(&oqueue);
1276
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001277 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1278 rset = (fd_set *)xmalloc(set_size);
1279 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001280
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001281 for (;;) {
1282 memset(rset, 0, set_size);
1283 memset(wset, 0, set_size);
1284
1285 FD_SET(in, rset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001286 olen = buffer_len(&oqueue);
1287 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001288 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001289
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001290 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001291 if (errno == EINTR)
1292 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001293 error("select: %s", strerror(errno));
1294 cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001295 }
1296
1297 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001298 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001299 char buf[4*4096];
1300 len = read(in, buf, sizeof buf);
1301 if (len == 0) {
1302 debug("read eof");
Damien Millerfef95ad2006-07-10 20:46:55 +10001303 cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001304 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001305 error("read: %s", strerror(errno));
1306 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001307 } else {
1308 buffer_append(&iqueue, buf, len);
1309 }
1310 }
1311 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001312 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001313 len = write(out, buffer_ptr(&oqueue), olen);
1314 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001315 error("write: %s", strerror(errno));
1316 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001317 } else {
1318 buffer_consume(&oqueue, len);
1319 }
1320 }
1321 /* process requests from client */
1322 process();
1323 }
1324}