blob: b95cb96c2e082cf85c93d0c6b806db5d1836be44 [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 Miller9f2abc42006-07-10 20:53:08 +100023#include <pwd.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110024
Damien Miller7b28dc52000-09-05 13:34:53 +110025#include "buffer.h"
26#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "log.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110028#include "xmalloc.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100029#include "misc.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100030#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110031
Ben Lindstrom2f959b42001-01-11 06:20:23 +000032#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110033#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110034
35/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000036#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110037#define get_int() buffer_get_int(&iqueue);
38#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Damien Miller7b28dc52000-09-05 13:34:53 +110039
Damien Millerfef95ad2006-07-10 20:46:55 +100040/* Our verbosity */
41LogLevel log_level = SYSLOG_LEVEL_ERROR;
42
43/* Our client */
44struct passwd *pw = NULL;
45char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000046
Damien Miller7b28dc52000-09-05 13:34:53 +110047/* input and output queue */
48Buffer iqueue;
49Buffer oqueue;
50
Damien Miller058316f2001-03-08 10:08:49 +110051/* Version of client */
52int version;
53
Darren Tuckera6612d42003-06-28 12:39:03 +100054/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110055
Damien Miller7b28dc52000-09-05 13:34:53 +110056typedef struct Stat Stat;
57
Damien Miller33804262001-02-04 23:20:18 +110058struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110059 char *name;
60 char *long_name;
61 Attrib attrib;
62};
63
Ben Lindstrombba81212001-06-25 05:01:22 +000064static int
Damien Miller7b28dc52000-09-05 13:34:53 +110065errno_to_portable(int unixerrno)
66{
67 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000068
Damien Miller7b28dc52000-09-05 13:34:53 +110069 switch (unixerrno) {
70 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000071 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110072 break;
73 case ENOENT:
74 case ENOTDIR:
75 case EBADF:
76 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000077 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110078 break;
79 case EPERM:
80 case EACCES:
81 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000082 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110083 break;
84 case ENAMETOOLONG:
85 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000086 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +110087 break;
88 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000089 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +110090 break;
91 }
92 return ret;
93}
94
Ben Lindstrombba81212001-06-25 05:01:22 +000095static int
Damien Miller7b28dc52000-09-05 13:34:53 +110096flags_from_portable(int pflags)
97{
98 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000099
Ben Lindstrom36592512001-03-05 05:02:08 +0000100 if ((pflags & SSH2_FXF_READ) &&
101 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100102 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000103 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100104 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000105 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100106 flags = O_WRONLY;
107 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000108 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100109 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000110 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100111 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000112 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100113 flags |= O_EXCL;
114 return flags;
115}
116
Damien Millerfef95ad2006-07-10 20:46:55 +1000117static const char *
118string_from_portable(int pflags)
119{
120 static char ret[128];
121
122 *ret = '\0';
123
124#define PAPPEND(str) { \
125 if (*ret != '\0') \
126 strlcat(ret, ",", sizeof(ret)); \
127 strlcat(ret, str, sizeof(ret)); \
128 }
129
130 if (pflags & SSH2_FXF_READ)
131 PAPPEND("READ")
132 if (pflags & SSH2_FXF_WRITE)
133 PAPPEND("WRITE")
134 if (pflags & SSH2_FXF_CREAT)
135 PAPPEND("CREATE")
136 if (pflags & SSH2_FXF_TRUNC)
137 PAPPEND("TRUNCATE")
138 if (pflags & SSH2_FXF_EXCL)
139 PAPPEND("EXCL")
140
141 return ret;
142}
143
Ben Lindstrombba81212001-06-25 05:01:22 +0000144static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100145get_attrib(void)
146{
147 return decode_attrib(&iqueue);
148}
149
150/* handle handles */
151
152typedef struct Handle Handle;
153struct Handle {
154 int use;
155 DIR *dirp;
156 int fd;
157 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000158 u_int64_t bytes_read, bytes_write;
Damien Miller7b28dc52000-09-05 13:34:53 +1100159};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000160
Damien Miller7b28dc52000-09-05 13:34:53 +1100161enum {
162 HANDLE_UNUSED,
163 HANDLE_DIR,
164 HANDLE_FILE
165};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000166
Damien Miller7b28dc52000-09-05 13:34:53 +1100167Handle handles[100];
168
Ben Lindstrombba81212001-06-25 05:01:22 +0000169static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100170handle_init(void)
171{
Damien Millereccb9de2005-06-17 12:59:34 +1000172 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000173
Damien Miller9f0f5c62001-12-21 14:45:46 +1100174 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
Damien Miller7b28dc52000-09-05 13:34:53 +1100175 handles[i].use = HANDLE_UNUSED;
176}
177
Ben Lindstrombba81212001-06-25 05:01:22 +0000178static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100179handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100180{
Damien Millereccb9de2005-06-17 12:59:34 +1000181 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000182
Damien Miller9f0f5c62001-12-21 14:45:46 +1100183 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100184 if (handles[i].use == HANDLE_UNUSED) {
185 handles[i].use = use;
186 handles[i].dirp = dirp;
187 handles[i].fd = fd;
Damien Miller00111382003-03-10 11:21:17 +1100188 handles[i].name = xstrdup(name);
Damien Millerfef95ad2006-07-10 20:46:55 +1000189 handles[i].bytes_read = handles[i].bytes_write = 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100190 return i;
191 }
192 }
193 return -1;
194}
195
Ben Lindstrombba81212001-06-25 05:01:22 +0000196static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100197handle_is_ok(int i, int type)
198{
Damien Millereccb9de2005-06-17 12:59:34 +1000199 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000200 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100201}
202
Ben Lindstrombba81212001-06-25 05:01:22 +0000203static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100204handle_to_string(int handle, char **stringp, int *hlenp)
205{
Damien Miller7b28dc52000-09-05 13:34:53 +1100206 if (stringp == NULL || hlenp == NULL)
207 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000208 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100209 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000210 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100211 return 0;
212}
213
Ben Lindstrombba81212001-06-25 05:01:22 +0000214static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100215handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100216{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000217 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000218
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000219 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100220 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100221 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100222 if (handle_is_ok(val, HANDLE_FILE) ||
223 handle_is_ok(val, HANDLE_DIR))
224 return val;
225 return -1;
226}
227
Ben Lindstrombba81212001-06-25 05:01:22 +0000228static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100229handle_to_name(int handle)
230{
231 if (handle_is_ok(handle, HANDLE_DIR)||
232 handle_is_ok(handle, HANDLE_FILE))
233 return handles[handle].name;
234 return NULL;
235}
236
Ben Lindstrombba81212001-06-25 05:01:22 +0000237static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100238handle_to_dir(int handle)
239{
240 if (handle_is_ok(handle, HANDLE_DIR))
241 return handles[handle].dirp;
242 return NULL;
243}
244
Ben Lindstrombba81212001-06-25 05:01:22 +0000245static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100246handle_to_fd(int handle)
247{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000248 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100249 return handles[handle].fd;
250 return -1;
251}
252
Damien Millerfef95ad2006-07-10 20:46:55 +1000253static void
254handle_update_read(int handle, ssize_t bytes)
255{
256 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
257 handles[handle].bytes_read += bytes;
258}
259
260static void
261handle_update_write(int handle, ssize_t bytes)
262{
263 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
264 handles[handle].bytes_write += bytes;
265}
266
267static u_int64_t
268handle_bytes_read(int handle)
269{
270 if (handle_is_ok(handle, HANDLE_FILE))
271 return (handles[handle].bytes_read);
272 return 0;
273}
274
275static u_int64_t
276handle_bytes_write(int handle)
277{
278 if (handle_is_ok(handle, HANDLE_FILE))
279 return (handles[handle].bytes_write);
280 return 0;
281}
282
Ben Lindstrombba81212001-06-25 05:01:22 +0000283static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100284handle_close(int handle)
285{
286 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000287
Damien Miller7b28dc52000-09-05 13:34:53 +1100288 if (handle_is_ok(handle, HANDLE_FILE)) {
289 ret = close(handles[handle].fd);
290 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100291 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100292 } else if (handle_is_ok(handle, HANDLE_DIR)) {
293 ret = closedir(handles[handle].dirp);
294 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100295 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100296 } else {
297 errno = ENOENT;
298 }
299 return ret;
300}
301
Damien Millerfef95ad2006-07-10 20:46:55 +1000302static void
303handle_log_close(int handle, char *emsg)
304{
305 if (handle_is_ok(handle, HANDLE_FILE)) {
306 logit("%s%sclose \"%s\" bytes read %llu written %llu",
307 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
308 handle_to_name(handle),
309 handle_bytes_read(handle), handle_bytes_write(handle));
310 } else {
311 logit("%s%sclosedir \"%s\"",
312 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
313 handle_to_name(handle));
314 }
315}
316
317static void
318handle_log_exit(void)
319{
320 u_int i;
321
322 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
323 if (handles[i].use != HANDLE_UNUSED)
324 handle_log_close(i, "forced");
325}
326
Ben Lindstrombba81212001-06-25 05:01:22 +0000327static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100328get_handle(void)
329{
330 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000331 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100332 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000333
Damien Miller7b28dc52000-09-05 13:34:53 +1100334 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000335 if (hlen < 256)
336 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100337 xfree(handle);
338 return val;
339}
340
341/* send replies */
342
Ben Lindstrombba81212001-06-25 05:01:22 +0000343static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100344send_msg(Buffer *m)
345{
346 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000347
Damien Miller7b28dc52000-09-05 13:34:53 +1100348 buffer_put_int(&oqueue, mlen);
349 buffer_append(&oqueue, buffer_ptr(m), mlen);
350 buffer_consume(m, mlen);
351}
352
Damien Millerfef95ad2006-07-10 20:46:55 +1000353static const char *
354status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100355{
Damien Miller058316f2001-03-08 10:08:49 +1100356 const char *status_messages[] = {
357 "Success", /* SSH_FX_OK */
358 "End of file", /* SSH_FX_EOF */
359 "No such file", /* SSH_FX_NO_SUCH_FILE */
360 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
361 "Failure", /* SSH_FX_FAILURE */
362 "Bad message", /* SSH_FX_BAD_MESSAGE */
363 "No connection", /* SSH_FX_NO_CONNECTION */
364 "Connection lost", /* SSH_FX_CONNECTION_LOST */
365 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
366 "Unknown error" /* Others */
367 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000368 return (status_messages[MIN(status,SSH2_FX_MAX)]);
369}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000370
Damien Millerfef95ad2006-07-10 20:46:55 +1000371static void
372send_status(u_int32_t id, u_int32_t status)
373{
374 Buffer msg;
375
376 debug3("request %u: sent status %u", id, status);
377 if (log_level > SYSLOG_LEVEL_VERBOSE ||
378 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
379 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100380 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000381 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100382 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000383 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100384 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000385 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100386 buffer_put_cstring(&msg, "");
387 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100388 send_msg(&msg);
389 buffer_free(&msg);
390}
Ben Lindstrombba81212001-06-25 05:01:22 +0000391static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100392send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100393{
394 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000395
Damien Miller7b28dc52000-09-05 13:34:53 +1100396 buffer_init(&msg);
397 buffer_put_char(&msg, type);
398 buffer_put_int(&msg, id);
399 buffer_put_string(&msg, data, dlen);
400 send_msg(&msg);
401 buffer_free(&msg);
402}
403
Ben Lindstrombba81212001-06-25 05:01:22 +0000404static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100405send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100406{
Damien Millerfef95ad2006-07-10 20:46:55 +1000407 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000408 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100409}
410
Ben Lindstrombba81212001-06-25 05:01:22 +0000411static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100412send_handle(u_int32_t id, int handle)
413{
414 char *string;
415 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000416
Damien Miller7b28dc52000-09-05 13:34:53 +1100417 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000418 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000419 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100420 xfree(string);
421}
422
Ben Lindstrombba81212001-06-25 05:01:22 +0000423static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100424send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100425{
426 Buffer msg;
427 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000428
Damien Miller7b28dc52000-09-05 13:34:53 +1100429 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000430 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100431 buffer_put_int(&msg, id);
432 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000433 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100434 for (i = 0; i < count; i++) {
435 buffer_put_cstring(&msg, stats[i].name);
436 buffer_put_cstring(&msg, stats[i].long_name);
437 encode_attrib(&msg, &stats[i].attrib);
438 }
439 send_msg(&msg);
440 buffer_free(&msg);
441}
442
Ben Lindstrombba81212001-06-25 05:01:22 +0000443static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100444send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100445{
446 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000447
Damien Millerfef95ad2006-07-10 20:46:55 +1000448 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100449 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000450 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100451 buffer_put_int(&msg, id);
452 encode_attrib(&msg, a);
453 send_msg(&msg);
454 buffer_free(&msg);
455}
456
457/* parse incoming */
458
Ben Lindstrombba81212001-06-25 05:01:22 +0000459static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100460process_init(void)
461{
462 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100463
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000464 version = get_int();
Damien Millerfef95ad2006-07-10 20:46:55 +1000465 verbose("received client version %d", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100466 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000467 buffer_put_char(&msg, SSH2_FXP_VERSION);
468 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100469 send_msg(&msg);
470 buffer_free(&msg);
471}
472
Ben Lindstrombba81212001-06-25 05:01:22 +0000473static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100474process_open(void)
475{
476 u_int32_t id, pflags;
477 Attrib *a;
478 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000479 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100480
481 id = get_int();
Damien Millerfef95ad2006-07-10 20:46:55 +1000482 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100483 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000484 pflags = get_int(); /* portable flags */
Damien Miller7b28dc52000-09-05 13:34:53 +1100485 a = get_attrib();
486 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000487 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000488 logit("open \"%s\" flags %s mode 0%o",
489 name, string_from_portable(pflags), mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100490 fd = open(name, flags, mode);
491 if (fd < 0) {
492 status = errno_to_portable(errno);
493 } else {
Damien Miller00111382003-03-10 11:21:17 +1100494 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100495 if (handle < 0) {
496 close(fd);
497 } else {
498 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000499 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100500 }
501 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000502 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100503 send_status(id, status);
504 xfree(name);
505}
506
Ben Lindstrombba81212001-06-25 05:01:22 +0000507static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100508process_close(void)
509{
510 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000511 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100512
513 id = get_int();
514 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000515 debug3("request %u: close handle %u", id, handle);
516 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100517 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000518 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100519 send_status(id, status);
520}
521
Ben Lindstrombba81212001-06-25 05:01:22 +0000522static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100523process_read(void)
524{
525 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000526 u_int32_t id, len;
527 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100528 u_int64_t off;
529
530 id = get_int();
531 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000532 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100533 len = get_int();
534
Damien Millerfef95ad2006-07-10 20:46:55 +1000535 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
536 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100537 if (len > sizeof buf) {
538 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000539 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100540 }
541 fd = handle_to_fd(handle);
542 if (fd >= 0) {
543 if (lseek(fd, off, SEEK_SET) < 0) {
544 error("process_read: seek failed");
545 status = errno_to_portable(errno);
546 } else {
547 ret = read(fd, buf, len);
548 if (ret < 0) {
549 status = errno_to_portable(errno);
550 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000551 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100552 } else {
553 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000554 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000555 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100556 }
557 }
558 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000559 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100560 send_status(id, status);
561}
562
Ben Lindstrombba81212001-06-25 05:01:22 +0000563static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100564process_write(void)
565{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000566 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100567 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100568 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000569 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100570 char *data;
571
572 id = get_int();
573 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000574 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100575 data = get_string(&len);
576
Damien Millerfef95ad2006-07-10 20:46:55 +1000577 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
578 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100579 fd = handle_to_fd(handle);
580 if (fd >= 0) {
581 if (lseek(fd, off, SEEK_SET) < 0) {
582 status = errno_to_portable(errno);
583 error("process_write: seek failed");
584 } else {
585/* XXX ATOMICIO ? */
586 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000587 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100588 error("process_write: write failed");
589 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000590 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000591 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000592 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100593 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000594 debug2("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100595 }
596 }
597 }
598 send_status(id, status);
599 xfree(data);
600}
601
Ben Lindstrombba81212001-06-25 05:01:22 +0000602static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100603process_do_stat(int do_lstat)
604{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000605 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100606 struct stat st;
607 u_int32_t id;
608 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000609 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100610
611 id = get_int();
612 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000613 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
614 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100615 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
616 if (ret < 0) {
617 status = errno_to_portable(errno);
618 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000619 stat_to_attrib(&st, &a);
620 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000621 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100622 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000623 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100624 send_status(id, status);
625 xfree(name);
626}
627
Ben Lindstrombba81212001-06-25 05:01:22 +0000628static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100629process_stat(void)
630{
631 process_do_stat(0);
632}
633
Ben Lindstrombba81212001-06-25 05:01:22 +0000634static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100635process_lstat(void)
636{
637 process_do_stat(1);
638}
639
Ben Lindstrombba81212001-06-25 05:01:22 +0000640static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100641process_fstat(void)
642{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000643 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100644 struct stat st;
645 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000646 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100647
648 id = get_int();
649 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000650 debug("request %u: fstat \"%s\" (handle %u)",
651 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100652 fd = handle_to_fd(handle);
653 if (fd >= 0) {
654 ret = fstat(fd, &st);
655 if (ret < 0) {
656 status = errno_to_portable(errno);
657 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000658 stat_to_attrib(&st, &a);
659 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000660 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100661 }
662 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000663 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100664 send_status(id, status);
665}
666
Ben Lindstrombba81212001-06-25 05:01:22 +0000667static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100668attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100669{
670 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000671
Damien Miller7b28dc52000-09-05 13:34:53 +1100672 tv[0].tv_sec = a->atime;
673 tv[0].tv_usec = 0;
674 tv[1].tv_sec = a->mtime;
675 tv[1].tv_usec = 0;
676 return tv;
677}
678
Ben Lindstrombba81212001-06-25 05:01:22 +0000679static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100680process_setstat(void)
681{
682 Attrib *a;
683 u_int32_t id;
684 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000685 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100686
687 id = get_int();
688 name = get_string(NULL);
689 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000690 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100691 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000692 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100693 ret = truncate(name, a->size);
694 if (ret == -1)
695 status = errno_to_portable(errno);
696 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000697 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000698 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller7b28dc52000-09-05 13:34:53 +1100699 ret = chmod(name, a->perm & 0777);
700 if (ret == -1)
701 status = errno_to_portable(errno);
702 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000703 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000704 char buf[64];
705 time_t t = a->mtime;
706
707 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
708 localtime(&t));
709 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100710 ret = utimes(name, attrib_to_tv(a));
711 if (ret == -1)
712 status = errno_to_portable(errno);
713 }
Kevin Steves8e743932001-02-05 13:24:35 +0000714 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000715 logit("set \"%s\" owner %lu group %lu", name,
716 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000717 ret = chown(name, a->uid, a->gid);
718 if (ret == -1)
719 status = errno_to_portable(errno);
720 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100721 send_status(id, status);
722 xfree(name);
723}
724
Ben Lindstrombba81212001-06-25 05:01:22 +0000725static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100726process_fsetstat(void)
727{
728 Attrib *a;
729 u_int32_t id;
730 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000731 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000732
Damien Miller7b28dc52000-09-05 13:34:53 +1100733 id = get_int();
734 handle = get_handle();
735 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000736 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100737 fd = handle_to_fd(handle);
Damien Millerfef95ad2006-07-10 20:46:55 +1000738 if (fd < 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000739 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100740 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000741 char *name = handle_to_name(handle);
742
Damien Miller00c92172002-02-13 14:05:00 +1100743 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000744 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100745 ret = ftruncate(fd, a->size);
746 if (ret == -1)
747 status = errno_to_portable(errno);
748 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000749 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000750 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000751#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100752 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000753#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000754 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000755#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100756 if (ret == -1)
757 status = errno_to_portable(errno);
758 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000759 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000760 char buf[64];
761 time_t t = a->mtime;
762
763 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
764 localtime(&t));
765 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100766#ifdef HAVE_FUTIMES
767 ret = futimes(fd, attrib_to_tv(a));
768#else
769 ret = utimes(name, attrib_to_tv(a));
770#endif
771 if (ret == -1)
772 status = errno_to_portable(errno);
773 }
Kevin Steves8e743932001-02-05 13:24:35 +0000774 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000775 logit("set \"%s\" owner %lu group %lu", name,
776 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000777#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000778 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000779#else
780 ret = chown(name, a->uid, a->gid);
781#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000782 if (ret == -1)
783 status = errno_to_portable(errno);
784 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100785 }
786 send_status(id, status);
787}
788
Ben Lindstrombba81212001-06-25 05:01:22 +0000789static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100790process_opendir(void)
791{
792 DIR *dirp = NULL;
793 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000794 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100795 u_int32_t id;
796
797 id = get_int();
798 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000799 debug3("request %u: opendir", id);
800 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000801 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100802 if (dirp == NULL) {
803 status = errno_to_portable(errno);
804 } else {
Damien Miller00111382003-03-10 11:21:17 +1100805 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100806 if (handle < 0) {
807 closedir(dirp);
808 } else {
809 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000810 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100811 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000812
Damien Miller7b28dc52000-09-05 13:34:53 +1100813 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000814 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100815 send_status(id, status);
816 xfree(path);
817}
818
Ben Lindstrombba81212001-06-25 05:01:22 +0000819static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100820process_readdir(void)
821{
822 DIR *dirp;
823 struct dirent *dp;
824 char *path;
825 int handle;
826 u_int32_t id;
827
828 id = get_int();
829 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000830 debug("request %u: readdir \"%s\" (handle %d)", id,
831 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100832 dirp = handle_to_dir(handle);
833 path = handle_to_name(handle);
834 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000835 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100836 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100837 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +1000838 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +1100839 Stat *stats;
840 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000841
Damien Miller07d86be2006-03-26 14:19:21 +1100842 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100843 while ((dp = readdir(dirp)) != NULL) {
844 if (count >= nstats) {
845 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +1100846 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100847 }
848/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000849 snprintf(pathname, sizeof pathname, "%s%s%s", path,
850 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100851 if (lstat(pathname, &st) < 0)
852 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000853 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100854 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000855 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100856 count++;
857 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000858 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100859 if (count == 100)
860 break;
861 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000862 if (count > 0) {
863 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100864 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000865 xfree(stats[i].name);
866 xfree(stats[i].long_name);
867 }
868 } else {
869 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100870 }
871 xfree(stats);
872 }
873}
874
Ben Lindstrombba81212001-06-25 05:01:22 +0000875static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100876process_remove(void)
877{
878 char *name;
879 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000880 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100881 int ret;
882
883 id = get_int();
884 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000885 debug3("request %u: remove", id);
886 logit("remove name \"%s\"", name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000887 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000888 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100889 send_status(id, status);
890 xfree(name);
891}
892
Ben Lindstrombba81212001-06-25 05:01:22 +0000893static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100894process_mkdir(void)
895{
896 Attrib *a;
897 u_int32_t id;
898 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000899 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100900
901 id = get_int();
902 name = get_string(NULL);
903 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000904 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
905 a->perm & 0777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +1000906 debug3("request %u: mkdir", id);
907 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100908 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000909 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100910 send_status(id, status);
911 xfree(name);
912}
913
Ben Lindstrombba81212001-06-25 05:01:22 +0000914static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100915process_rmdir(void)
916{
917 u_int32_t id;
918 char *name;
919 int ret, status;
920
921 id = get_int();
922 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000923 debug3("request %u: rmdir", id);
924 logit("rmdir name \"%s\"", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100925 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000926 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100927 send_status(id, status);
928 xfree(name);
929}
930
Ben Lindstrombba81212001-06-25 05:01:22 +0000931static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100932process_realpath(void)
933{
934 char resolvedname[MAXPATHLEN];
935 u_int32_t id;
936 char *path;
937
938 id = get_int();
939 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000940 if (path[0] == '\0') {
941 xfree(path);
942 path = xstrdup(".");
943 }
Damien Millerfef95ad2006-07-10 20:46:55 +1000944 debug3("request %u: realpath", id);
945 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100946 if (realpath(path, resolvedname) == NULL) {
947 send_status(id, errno_to_portable(errno));
948 } else {
949 Stat s;
950 attrib_clear(&s.attrib);
951 s.name = s.long_name = resolvedname;
952 send_names(id, 1, &s);
953 }
954 xfree(path);
955}
956
Ben Lindstrombba81212001-06-25 05:01:22 +0000957static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100958process_rename(void)
959{
960 u_int32_t id;
961 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100962 int status;
Damien Millerb3207e82003-03-26 16:01:11 +1100963 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +1100964
965 id = get_int();
966 oldpath = get_string(NULL);
967 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000968 debug3("request %u: rename", id);
969 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +1100970 status = SSH2_FX_FAILURE;
971 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +1100972 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +1100973 else if (S_ISREG(sb.st_mode)) {
974 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000975 if (link(oldpath, newpath) == -1) {
Darren Tuckere59b5082004-06-28 16:01:19 +1000976 if (errno == EOPNOTSUPP
977#ifdef LINK_OPNOTSUPP_ERRNO
978 || errno == LINK_OPNOTSUPP_ERRNO
979#endif
980 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000981 struct stat st;
982
983 /*
984 * fs doesn't support links, so fall back to
985 * stat+rename. This is racy.
986 */
987 if (stat(newpath, &st) == -1) {
988 if (rename(oldpath, newpath) == -1)
989 status =
990 errno_to_portable(errno);
991 else
992 status = SSH2_FX_OK;
993 }
994 } else {
995 status = errno_to_portable(errno);
996 }
997 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +1100998 status = errno_to_portable(errno);
999 /* clean spare link */
1000 unlink(newpath);
1001 } else
1002 status = SSH2_FX_OK;
1003 } else if (stat(newpath, &sb) == -1) {
1004 if (rename(oldpath, newpath) == -1)
1005 status = errno_to_portable(errno);
1006 else
1007 status = SSH2_FX_OK;
1008 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001009 send_status(id, status);
1010 xfree(oldpath);
1011 xfree(newpath);
1012}
1013
Ben Lindstrombba81212001-06-25 05:01:22 +00001014static void
Damien Miller058316f2001-03-08 10:08:49 +11001015process_readlink(void)
1016{
1017 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001018 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001019 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001020 char *path;
1021
1022 id = get_int();
1023 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001024 debug3("request %u: readlink", id);
1025 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001026 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001027 send_status(id, errno_to_portable(errno));
1028 else {
1029 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001030
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001031 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001032 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001033 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001034 send_names(id, 1, &s);
1035 }
1036 xfree(path);
1037}
1038
Ben Lindstrombba81212001-06-25 05:01:22 +00001039static void
Damien Miller058316f2001-03-08 10:08:49 +11001040process_symlink(void)
1041{
1042 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +11001043 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001044 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001045
1046 id = get_int();
1047 oldpath = get_string(NULL);
1048 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001049 debug3("request %u: symlink", id);
1050 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001051 /* this will fail if 'newpath' exists */
1052 ret = symlink(oldpath, newpath);
1053 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001054 send_status(id, status);
1055 xfree(oldpath);
1056 xfree(newpath);
1057}
1058
Ben Lindstrombba81212001-06-25 05:01:22 +00001059static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001060process_extended(void)
1061{
1062 u_int32_t id;
1063 char *request;
1064
1065 id = get_int();
1066 request = get_string(NULL);
1067 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1068 xfree(request);
1069}
Damien Miller7b28dc52000-09-05 13:34:53 +11001070
1071/* stolen from ssh-agent */
1072
Ben Lindstrombba81212001-06-25 05:01:22 +00001073static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001074process(void)
1075{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001076 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +00001077 u_int buf_len;
1078 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001079 u_int type;
1080 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +11001081
Ben Lindstrom2c140472002-06-06 21:57:54 +00001082 buf_len = buffer_len(&iqueue);
1083 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001084 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001085 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001086 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001087 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001088 error("bad message from %s local user %s",
1089 client_addr, pw->pw_name);
1090 cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001091 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001092 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001093 return;
1094 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001095 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001096 type = buffer_get_char(&iqueue);
1097 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001098 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001099 process_init();
1100 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001101 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +11001102 process_open();
1103 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001104 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001105 process_close();
1106 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001107 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +11001108 process_read();
1109 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001110 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001111 process_write();
1112 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001113 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001114 process_lstat();
1115 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001116 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001117 process_fstat();
1118 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001119 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001120 process_setstat();
1121 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001122 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001123 process_fsetstat();
1124 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001125 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001126 process_opendir();
1127 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001128 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001129 process_readdir();
1130 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001131 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001132 process_remove();
1133 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001134 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001135 process_mkdir();
1136 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001137 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001138 process_rmdir();
1139 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001140 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001141 process_realpath();
1142 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001143 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001144 process_stat();
1145 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001146 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001147 process_rename();
1148 break;
Damien Miller058316f2001-03-08 10:08:49 +11001149 case SSH2_FXP_READLINK:
1150 process_readlink();
1151 break;
1152 case SSH2_FXP_SYMLINK:
1153 process_symlink();
1154 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001155 case SSH2_FXP_EXTENDED:
1156 process_extended();
1157 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001158 default:
1159 error("Unknown message %d", type);
1160 break;
1161 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001162 /* discard the remaining bytes from the current packet */
1163 if (buf_len < buffer_len(&iqueue))
Damien Millerfef95ad2006-07-10 20:46:55 +10001164 fatal("iqueue grew unexpectedly");
Ben Lindstrom2c140472002-06-06 21:57:54 +00001165 consumed = buf_len - buffer_len(&iqueue);
1166 if (msg_len < consumed)
1167 fatal("msg_len %d < consumed %d", msg_len, consumed);
1168 if (msg_len > consumed)
1169 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001170}
1171
Damien Millerfef95ad2006-07-10 20:46:55 +10001172/* Cleanup handler that logs active handles upon normal exit */
1173void
1174cleanup_exit(int i)
1175{
1176 if (pw != NULL && client_addr != NULL) {
1177 handle_log_exit();
1178 logit("session closed for local user %s from [%s]",
1179 pw->pw_name, client_addr);
1180 }
1181 _exit(i);
1182}
1183
1184static void
1185usage(void)
1186{
1187 extern char *__progname;
1188
1189 fprintf(stderr,
1190 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1191 exit(1);
1192}
1193
Damien Miller7b28dc52000-09-05 13:34:53 +11001194int
Damien Millerfef95ad2006-07-10 20:46:55 +10001195main(int argc, char **argv)
Damien Miller7b28dc52000-09-05 13:34:53 +11001196{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001197 fd_set *rset, *wset;
Damien Millerfef95ad2006-07-10 20:46:55 +10001198 int in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001199 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001200 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1201 char *cp;
1202
1203 extern int optind;
1204 extern char *optarg;
1205 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001206
Darren Tuckerce321d82005-10-03 18:11:24 +10001207 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1208 sanitise_stdfd();
1209
Damien Millerfef95ad2006-07-10 20:46:55 +10001210 __progname = ssh_get_progname(argv[0]);
1211 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001212
Damien Millerfef95ad2006-07-10 20:46:55 +10001213 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1214 switch (ch) {
1215 case 'c':
1216 /*
1217 * Ignore all arguments if we are invoked as a
1218 * shell using "sftp-server -c command"
1219 */
1220 skipargs = 1;
1221 break;
1222 case 'e':
1223 log_stderr = 1;
1224 break;
1225 case 'l':
1226 log_level = log_level_number(optarg);
1227 if (log_level == SYSLOG_LEVEL_NOT_SET)
1228 error("Invalid log level \"%s\"", optarg);
1229 break;
1230 case 'f':
1231 log_facility = log_facility_number(optarg);
1232 if (log_level == SYSLOG_FACILITY_NOT_SET)
1233 error("Invalid log facility \"%s\"", optarg);
1234 break;
1235 case 'h':
1236 default:
1237 usage();
1238 }
1239 }
1240
1241 log_init(__progname, log_level, log_facility, log_stderr);
1242
1243 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1244 client_addr = xstrdup(cp);
1245 if ((cp = strchr(client_addr, ' ')) == NULL)
1246 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1247 getenv("SSH_CONNECTION"));
1248 *cp = '\0';
1249 } else
1250 client_addr = xstrdup("UNKNOWN");
1251
1252 if ((pw = getpwuid(getuid())) == NULL)
1253 fatal("No user found for uid %lu", (u_long)getuid());
1254 pw = pwcopy(pw);
1255
1256 logit("session opened for local user %s from [%s]",
1257 pw->pw_name, client_addr);
1258
Damien Miller7b28dc52000-09-05 13:34:53 +11001259 handle_init();
1260
Damien Miller7b28dc52000-09-05 13:34:53 +11001261 in = dup(STDIN_FILENO);
1262 out = dup(STDOUT_FILENO);
1263
Damien Miller402b3312001-04-14 00:28:42 +10001264#ifdef HAVE_CYGWIN
1265 setmode(in, O_BINARY);
1266 setmode(out, O_BINARY);
1267#endif
1268
Damien Miller7b28dc52000-09-05 13:34:53 +11001269 max = 0;
1270 if (in > max)
1271 max = in;
1272 if (out > max)
1273 max = out;
1274
1275 buffer_init(&iqueue);
1276 buffer_init(&oqueue);
1277
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001278 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1279 rset = (fd_set *)xmalloc(set_size);
1280 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001281
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001282 for (;;) {
1283 memset(rset, 0, set_size);
1284 memset(wset, 0, set_size);
1285
1286 FD_SET(in, rset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001287 olen = buffer_len(&oqueue);
1288 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001289 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001290
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001291 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001292 if (errno == EINTR)
1293 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001294 error("select: %s", strerror(errno));
1295 cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001296 }
1297
1298 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001299 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001300 char buf[4*4096];
1301 len = read(in, buf, sizeof buf);
1302 if (len == 0) {
1303 debug("read eof");
Damien Millerfef95ad2006-07-10 20:46:55 +10001304 cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001305 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001306 error("read: %s", strerror(errno));
1307 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001308 } else {
1309 buffer_append(&iqueue, buf, len);
1310 }
1311 }
1312 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001313 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001314 len = write(out, buffer_ptr(&oqueue), olen);
1315 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001316 error("write: %s", strerror(errno));
1317 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001318 } else {
1319 buffer_consume(&oqueue, len);
1320 }
1321 }
1322 /* process requests from client */
1323 process();
1324 }
1325}