blob: 29503246ee9a3c0c9dbea20fd5a861e295280519 [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: sftp-server.c,v 1.62 2006/07/11 20:07:25 stevesk 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>
Darren Tucker39972492006-07-12 22:22:46 +100023#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100024#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100025#include <pwd.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110026
Damien Miller7b28dc52000-09-05 13:34:53 +110027#include "buffer.h"
28#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000029#include "log.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110030#include "xmalloc.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100031#include "misc.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100032#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110033
Ben Lindstrom2f959b42001-01-11 06:20:23 +000034#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110035#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110036
37/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000038#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110039#define get_int() buffer_get_int(&iqueue);
40#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Damien Miller7b28dc52000-09-05 13:34:53 +110041
Damien Millerfef95ad2006-07-10 20:46:55 +100042/* Our verbosity */
43LogLevel log_level = SYSLOG_LEVEL_ERROR;
44
45/* Our client */
46struct passwd *pw = NULL;
47char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000048
Damien Miller7b28dc52000-09-05 13:34:53 +110049/* input and output queue */
50Buffer iqueue;
51Buffer oqueue;
52
Damien Miller058316f2001-03-08 10:08:49 +110053/* Version of client */
54int version;
55
Darren Tuckera6612d42003-06-28 12:39:03 +100056/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110057
Damien Miller7b28dc52000-09-05 13:34:53 +110058typedef struct Stat Stat;
59
Damien Miller33804262001-02-04 23:20:18 +110060struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110061 char *name;
62 char *long_name;
63 Attrib attrib;
64};
65
Ben Lindstrombba81212001-06-25 05:01:22 +000066static int
Damien Miller7b28dc52000-09-05 13:34:53 +110067errno_to_portable(int unixerrno)
68{
69 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000070
Damien Miller7b28dc52000-09-05 13:34:53 +110071 switch (unixerrno) {
72 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000073 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110074 break;
75 case ENOENT:
76 case ENOTDIR:
77 case EBADF:
78 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000079 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110080 break;
81 case EPERM:
82 case EACCES:
83 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000084 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110085 break;
86 case ENAMETOOLONG:
87 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000088 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +110089 break;
90 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000091 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +110092 break;
93 }
94 return ret;
95}
96
Ben Lindstrombba81212001-06-25 05:01:22 +000097static int
Damien Miller7b28dc52000-09-05 13:34:53 +110098flags_from_portable(int pflags)
99{
100 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000101
Ben Lindstrom36592512001-03-05 05:02:08 +0000102 if ((pflags & SSH2_FXF_READ) &&
103 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100104 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000105 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100106 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000107 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100108 flags = O_WRONLY;
109 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000110 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100111 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000112 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100113 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000114 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100115 flags |= O_EXCL;
116 return flags;
117}
118
Damien Millerfef95ad2006-07-10 20:46:55 +1000119static const char *
120string_from_portable(int pflags)
121{
122 static char ret[128];
123
124 *ret = '\0';
125
126#define PAPPEND(str) { \
127 if (*ret != '\0') \
128 strlcat(ret, ",", sizeof(ret)); \
129 strlcat(ret, str, sizeof(ret)); \
130 }
131
132 if (pflags & SSH2_FXF_READ)
133 PAPPEND("READ")
134 if (pflags & SSH2_FXF_WRITE)
135 PAPPEND("WRITE")
136 if (pflags & SSH2_FXF_CREAT)
137 PAPPEND("CREATE")
138 if (pflags & SSH2_FXF_TRUNC)
139 PAPPEND("TRUNCATE")
140 if (pflags & SSH2_FXF_EXCL)
141 PAPPEND("EXCL")
142
143 return ret;
144}
145
Ben Lindstrombba81212001-06-25 05:01:22 +0000146static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100147get_attrib(void)
148{
149 return decode_attrib(&iqueue);
150}
151
152/* handle handles */
153
154typedef struct Handle Handle;
155struct Handle {
156 int use;
157 DIR *dirp;
158 int fd;
159 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000160 u_int64_t bytes_read, bytes_write;
Damien Miller7b28dc52000-09-05 13:34:53 +1100161};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000162
Damien Miller7b28dc52000-09-05 13:34:53 +1100163enum {
164 HANDLE_UNUSED,
165 HANDLE_DIR,
166 HANDLE_FILE
167};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000168
Damien Miller7b28dc52000-09-05 13:34:53 +1100169Handle handles[100];
170
Ben Lindstrombba81212001-06-25 05:01:22 +0000171static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100172handle_init(void)
173{
Damien Millereccb9de2005-06-17 12:59:34 +1000174 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000175
Damien Miller9f0f5c62001-12-21 14:45:46 +1100176 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
Damien Miller7b28dc52000-09-05 13:34:53 +1100177 handles[i].use = HANDLE_UNUSED;
178}
179
Ben Lindstrombba81212001-06-25 05:01:22 +0000180static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100181handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100182{
Damien Millereccb9de2005-06-17 12:59:34 +1000183 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000184
Damien Miller9f0f5c62001-12-21 14:45:46 +1100185 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100186 if (handles[i].use == HANDLE_UNUSED) {
187 handles[i].use = use;
188 handles[i].dirp = dirp;
189 handles[i].fd = fd;
Damien Miller00111382003-03-10 11:21:17 +1100190 handles[i].name = xstrdup(name);
Damien Millerfef95ad2006-07-10 20:46:55 +1000191 handles[i].bytes_read = handles[i].bytes_write = 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100192 return i;
193 }
194 }
195 return -1;
196}
197
Ben Lindstrombba81212001-06-25 05:01:22 +0000198static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100199handle_is_ok(int i, int type)
200{
Damien Millereccb9de2005-06-17 12:59:34 +1000201 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000202 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100203}
204
Ben Lindstrombba81212001-06-25 05:01:22 +0000205static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100206handle_to_string(int handle, char **stringp, int *hlenp)
207{
Damien Miller7b28dc52000-09-05 13:34:53 +1100208 if (stringp == NULL || hlenp == NULL)
209 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000210 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100211 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000212 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100213 return 0;
214}
215
Ben Lindstrombba81212001-06-25 05:01:22 +0000216static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100217handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100218{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000219 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000220
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000221 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100222 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100223 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100224 if (handle_is_ok(val, HANDLE_FILE) ||
225 handle_is_ok(val, HANDLE_DIR))
226 return val;
227 return -1;
228}
229
Ben Lindstrombba81212001-06-25 05:01:22 +0000230static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100231handle_to_name(int handle)
232{
233 if (handle_is_ok(handle, HANDLE_DIR)||
234 handle_is_ok(handle, HANDLE_FILE))
235 return handles[handle].name;
236 return NULL;
237}
238
Ben Lindstrombba81212001-06-25 05:01:22 +0000239static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100240handle_to_dir(int handle)
241{
242 if (handle_is_ok(handle, HANDLE_DIR))
243 return handles[handle].dirp;
244 return NULL;
245}
246
Ben Lindstrombba81212001-06-25 05:01:22 +0000247static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100248handle_to_fd(int handle)
249{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000250 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100251 return handles[handle].fd;
252 return -1;
253}
254
Damien Millerfef95ad2006-07-10 20:46:55 +1000255static void
256handle_update_read(int handle, ssize_t bytes)
257{
258 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
259 handles[handle].bytes_read += bytes;
260}
261
262static void
263handle_update_write(int handle, ssize_t bytes)
264{
265 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
266 handles[handle].bytes_write += bytes;
267}
268
269static u_int64_t
270handle_bytes_read(int handle)
271{
272 if (handle_is_ok(handle, HANDLE_FILE))
273 return (handles[handle].bytes_read);
274 return 0;
275}
276
277static u_int64_t
278handle_bytes_write(int handle)
279{
280 if (handle_is_ok(handle, HANDLE_FILE))
281 return (handles[handle].bytes_write);
282 return 0;
283}
284
Ben Lindstrombba81212001-06-25 05:01:22 +0000285static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100286handle_close(int handle)
287{
288 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000289
Damien Miller7b28dc52000-09-05 13:34:53 +1100290 if (handle_is_ok(handle, HANDLE_FILE)) {
291 ret = close(handles[handle].fd);
292 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100293 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100294 } else if (handle_is_ok(handle, HANDLE_DIR)) {
295 ret = closedir(handles[handle].dirp);
296 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100297 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100298 } else {
299 errno = ENOENT;
300 }
301 return ret;
302}
303
Damien Millerfef95ad2006-07-10 20:46:55 +1000304static void
305handle_log_close(int handle, char *emsg)
306{
307 if (handle_is_ok(handle, HANDLE_FILE)) {
308 logit("%s%sclose \"%s\" bytes read %llu written %llu",
309 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
310 handle_to_name(handle),
311 handle_bytes_read(handle), handle_bytes_write(handle));
312 } else {
313 logit("%s%sclosedir \"%s\"",
314 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
315 handle_to_name(handle));
316 }
317}
318
319static void
320handle_log_exit(void)
321{
322 u_int i;
323
324 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
325 if (handles[i].use != HANDLE_UNUSED)
326 handle_log_close(i, "forced");
327}
328
Ben Lindstrombba81212001-06-25 05:01:22 +0000329static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100330get_handle(void)
331{
332 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000333 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100334 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000335
Damien Miller7b28dc52000-09-05 13:34:53 +1100336 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000337 if (hlen < 256)
338 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100339 xfree(handle);
340 return val;
341}
342
343/* send replies */
344
Ben Lindstrombba81212001-06-25 05:01:22 +0000345static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100346send_msg(Buffer *m)
347{
348 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000349
Damien Miller7b28dc52000-09-05 13:34:53 +1100350 buffer_put_int(&oqueue, mlen);
351 buffer_append(&oqueue, buffer_ptr(m), mlen);
352 buffer_consume(m, mlen);
353}
354
Damien Millerfef95ad2006-07-10 20:46:55 +1000355static const char *
356status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100357{
Damien Miller058316f2001-03-08 10:08:49 +1100358 const char *status_messages[] = {
359 "Success", /* SSH_FX_OK */
360 "End of file", /* SSH_FX_EOF */
361 "No such file", /* SSH_FX_NO_SUCH_FILE */
362 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
363 "Failure", /* SSH_FX_FAILURE */
364 "Bad message", /* SSH_FX_BAD_MESSAGE */
365 "No connection", /* SSH_FX_NO_CONNECTION */
366 "Connection lost", /* SSH_FX_CONNECTION_LOST */
367 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
368 "Unknown error" /* Others */
369 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000370 return (status_messages[MIN(status,SSH2_FX_MAX)]);
371}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000372
Damien Millerfef95ad2006-07-10 20:46:55 +1000373static void
374send_status(u_int32_t id, u_int32_t status)
375{
376 Buffer msg;
377
378 debug3("request %u: sent status %u", id, status);
379 if (log_level > SYSLOG_LEVEL_VERBOSE ||
380 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
381 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100382 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000383 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100384 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000385 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100386 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000387 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100388 buffer_put_cstring(&msg, "");
389 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100390 send_msg(&msg);
391 buffer_free(&msg);
392}
Ben Lindstrombba81212001-06-25 05:01:22 +0000393static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100394send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100395{
396 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000397
Damien Miller7b28dc52000-09-05 13:34:53 +1100398 buffer_init(&msg);
399 buffer_put_char(&msg, type);
400 buffer_put_int(&msg, id);
401 buffer_put_string(&msg, data, dlen);
402 send_msg(&msg);
403 buffer_free(&msg);
404}
405
Ben Lindstrombba81212001-06-25 05:01:22 +0000406static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100407send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100408{
Damien Millerfef95ad2006-07-10 20:46:55 +1000409 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000410 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100411}
412
Ben Lindstrombba81212001-06-25 05:01:22 +0000413static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100414send_handle(u_int32_t id, int handle)
415{
416 char *string;
417 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000418
Damien Miller7b28dc52000-09-05 13:34:53 +1100419 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000420 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000421 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100422 xfree(string);
423}
424
Ben Lindstrombba81212001-06-25 05:01:22 +0000425static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100426send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100427{
428 Buffer msg;
429 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000430
Damien Miller7b28dc52000-09-05 13:34:53 +1100431 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000432 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100433 buffer_put_int(&msg, id);
434 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000435 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100436 for (i = 0; i < count; i++) {
437 buffer_put_cstring(&msg, stats[i].name);
438 buffer_put_cstring(&msg, stats[i].long_name);
439 encode_attrib(&msg, &stats[i].attrib);
440 }
441 send_msg(&msg);
442 buffer_free(&msg);
443}
444
Ben Lindstrombba81212001-06-25 05:01:22 +0000445static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100446send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100447{
448 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000449
Damien Millerfef95ad2006-07-10 20:46:55 +1000450 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100451 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000452 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100453 buffer_put_int(&msg, id);
454 encode_attrib(&msg, a);
455 send_msg(&msg);
456 buffer_free(&msg);
457}
458
459/* parse incoming */
460
Ben Lindstrombba81212001-06-25 05:01:22 +0000461static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100462process_init(void)
463{
464 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100465
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000466 version = get_int();
Damien Millerfef95ad2006-07-10 20:46:55 +1000467 verbose("received client version %d", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100468 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000469 buffer_put_char(&msg, SSH2_FXP_VERSION);
470 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100471 send_msg(&msg);
472 buffer_free(&msg);
473}
474
Ben Lindstrombba81212001-06-25 05:01:22 +0000475static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100476process_open(void)
477{
478 u_int32_t id, pflags;
479 Attrib *a;
480 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000481 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100482
483 id = get_int();
484 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000485 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000486 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100487 a = get_attrib();
488 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000489 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000490 logit("open \"%s\" flags %s mode 0%o",
491 name, string_from_portable(pflags), mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100492 fd = open(name, flags, mode);
493 if (fd < 0) {
494 status = errno_to_portable(errno);
495 } else {
Damien Miller00111382003-03-10 11:21:17 +1100496 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100497 if (handle < 0) {
498 close(fd);
499 } else {
500 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000501 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100502 }
503 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000504 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100505 send_status(id, status);
506 xfree(name);
507}
508
Ben Lindstrombba81212001-06-25 05:01:22 +0000509static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100510process_close(void)
511{
512 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000513 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100514
515 id = get_int();
516 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000517 debug3("request %u: close handle %u", id, handle);
518 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100519 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000520 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100521 send_status(id, status);
522}
523
Ben Lindstrombba81212001-06-25 05:01:22 +0000524static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100525process_read(void)
526{
527 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000528 u_int32_t id, len;
529 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100530 u_int64_t off;
531
532 id = get_int();
533 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000534 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100535 len = get_int();
536
Damien Millerfef95ad2006-07-10 20:46:55 +1000537 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
538 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100539 if (len > sizeof buf) {
540 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000541 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100542 }
543 fd = handle_to_fd(handle);
544 if (fd >= 0) {
545 if (lseek(fd, off, SEEK_SET) < 0) {
546 error("process_read: seek failed");
547 status = errno_to_portable(errno);
548 } else {
549 ret = read(fd, buf, len);
550 if (ret < 0) {
551 status = errno_to_portable(errno);
552 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000553 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100554 } else {
555 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000556 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000557 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100558 }
559 }
560 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000561 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100562 send_status(id, status);
563}
564
Ben Lindstrombba81212001-06-25 05:01:22 +0000565static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100566process_write(void)
567{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000568 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100569 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100570 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000571 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100572 char *data;
573
574 id = get_int();
575 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000576 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100577 data = get_string(&len);
578
Damien Millerfef95ad2006-07-10 20:46:55 +1000579 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
580 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100581 fd = handle_to_fd(handle);
582 if (fd >= 0) {
583 if (lseek(fd, off, SEEK_SET) < 0) {
584 status = errno_to_portable(errno);
585 error("process_write: seek failed");
586 } else {
587/* XXX ATOMICIO ? */
588 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000589 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100590 error("process_write: write failed");
591 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000592 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000593 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000594 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100595 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000596 debug2("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100597 }
598 }
599 }
600 send_status(id, status);
601 xfree(data);
602}
603
Ben Lindstrombba81212001-06-25 05:01:22 +0000604static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100605process_do_stat(int do_lstat)
606{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000607 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100608 struct stat st;
609 u_int32_t id;
610 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000611 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100612
613 id = get_int();
614 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000615 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
616 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100617 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
618 if (ret < 0) {
619 status = errno_to_portable(errno);
620 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000621 stat_to_attrib(&st, &a);
622 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000623 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100624 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000625 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100626 send_status(id, status);
627 xfree(name);
628}
629
Ben Lindstrombba81212001-06-25 05:01:22 +0000630static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100631process_stat(void)
632{
633 process_do_stat(0);
634}
635
Ben Lindstrombba81212001-06-25 05:01:22 +0000636static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100637process_lstat(void)
638{
639 process_do_stat(1);
640}
641
Ben Lindstrombba81212001-06-25 05:01:22 +0000642static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100643process_fstat(void)
644{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000645 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100646 struct stat st;
647 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000648 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100649
650 id = get_int();
651 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000652 debug("request %u: fstat \"%s\" (handle %u)",
653 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100654 fd = handle_to_fd(handle);
655 if (fd >= 0) {
656 ret = fstat(fd, &st);
657 if (ret < 0) {
658 status = errno_to_portable(errno);
659 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000660 stat_to_attrib(&st, &a);
661 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000662 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100663 }
664 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000665 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100666 send_status(id, status);
667}
668
Ben Lindstrombba81212001-06-25 05:01:22 +0000669static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100670attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100671{
672 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000673
Damien Miller7b28dc52000-09-05 13:34:53 +1100674 tv[0].tv_sec = a->atime;
675 tv[0].tv_usec = 0;
676 tv[1].tv_sec = a->mtime;
677 tv[1].tv_usec = 0;
678 return tv;
679}
680
Ben Lindstrombba81212001-06-25 05:01:22 +0000681static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100682process_setstat(void)
683{
684 Attrib *a;
685 u_int32_t id;
686 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000687 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100688
689 id = get_int();
690 name = get_string(NULL);
691 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000692 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100693 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000694 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100695 ret = truncate(name, a->size);
696 if (ret == -1)
697 status = errno_to_portable(errno);
698 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000699 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000700 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller7b28dc52000-09-05 13:34:53 +1100701 ret = chmod(name, a->perm & 0777);
702 if (ret == -1)
703 status = errno_to_portable(errno);
704 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000705 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000706 char buf[64];
707 time_t t = a->mtime;
708
709 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
710 localtime(&t));
711 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100712 ret = utimes(name, attrib_to_tv(a));
713 if (ret == -1)
714 status = errno_to_portable(errno);
715 }
Kevin Steves8e743932001-02-05 13:24:35 +0000716 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000717 logit("set \"%s\" owner %lu group %lu", name,
718 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000719 ret = chown(name, a->uid, a->gid);
720 if (ret == -1)
721 status = errno_to_portable(errno);
722 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100723 send_status(id, status);
724 xfree(name);
725}
726
Ben Lindstrombba81212001-06-25 05:01:22 +0000727static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100728process_fsetstat(void)
729{
730 Attrib *a;
731 u_int32_t id;
732 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000733 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000734
Damien Miller7b28dc52000-09-05 13:34:53 +1100735 id = get_int();
736 handle = get_handle();
737 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000738 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100739 fd = handle_to_fd(handle);
Damien Millerfef95ad2006-07-10 20:46:55 +1000740 if (fd < 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000741 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100742 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000743 char *name = handle_to_name(handle);
744
Damien Miller00c92172002-02-13 14:05:00 +1100745 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000746 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100747 ret = ftruncate(fd, a->size);
748 if (ret == -1)
749 status = errno_to_portable(errno);
750 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000751 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000752 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000753#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100754 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000755#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000756 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000757#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100758 if (ret == -1)
759 status = errno_to_portable(errno);
760 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000761 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000762 char buf[64];
763 time_t t = a->mtime;
764
765 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
766 localtime(&t));
767 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100768#ifdef HAVE_FUTIMES
769 ret = futimes(fd, attrib_to_tv(a));
770#else
771 ret = utimes(name, attrib_to_tv(a));
772#endif
773 if (ret == -1)
774 status = errno_to_portable(errno);
775 }
Kevin Steves8e743932001-02-05 13:24:35 +0000776 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000777 logit("set \"%s\" owner %lu group %lu", name,
778 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000779#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000780 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000781#else
782 ret = chown(name, a->uid, a->gid);
783#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000784 if (ret == -1)
785 status = errno_to_portable(errno);
786 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100787 }
788 send_status(id, status);
789}
790
Ben Lindstrombba81212001-06-25 05:01:22 +0000791static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100792process_opendir(void)
793{
794 DIR *dirp = NULL;
795 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000796 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100797 u_int32_t id;
798
799 id = get_int();
800 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000801 debug3("request %u: opendir", id);
802 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000803 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100804 if (dirp == NULL) {
805 status = errno_to_portable(errno);
806 } else {
Damien Miller00111382003-03-10 11:21:17 +1100807 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100808 if (handle < 0) {
809 closedir(dirp);
810 } else {
811 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000812 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100813 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000814
Damien Miller7b28dc52000-09-05 13:34:53 +1100815 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000816 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100817 send_status(id, status);
818 xfree(path);
819}
820
Ben Lindstrombba81212001-06-25 05:01:22 +0000821static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100822process_readdir(void)
823{
824 DIR *dirp;
825 struct dirent *dp;
826 char *path;
827 int handle;
828 u_int32_t id;
829
830 id = get_int();
831 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000832 debug("request %u: readdir \"%s\" (handle %d)", id,
833 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100834 dirp = handle_to_dir(handle);
835 path = handle_to_name(handle);
836 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000837 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100838 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100839 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +1000840 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +1100841 Stat *stats;
842 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000843
Damien Miller07d86be2006-03-26 14:19:21 +1100844 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100845 while ((dp = readdir(dirp)) != NULL) {
846 if (count >= nstats) {
847 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +1100848 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100849 }
850/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000851 snprintf(pathname, sizeof pathname, "%s%s%s", path,
852 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100853 if (lstat(pathname, &st) < 0)
854 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000855 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100856 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000857 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100858 count++;
859 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000860 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100861 if (count == 100)
862 break;
863 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000864 if (count > 0) {
865 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100866 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000867 xfree(stats[i].name);
868 xfree(stats[i].long_name);
869 }
870 } else {
871 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100872 }
873 xfree(stats);
874 }
875}
876
Ben Lindstrombba81212001-06-25 05:01:22 +0000877static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100878process_remove(void)
879{
880 char *name;
881 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000882 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100883 int ret;
884
885 id = get_int();
886 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000887 debug3("request %u: remove", id);
888 logit("remove name \"%s\"", name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000889 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000890 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100891 send_status(id, status);
892 xfree(name);
893}
894
Ben Lindstrombba81212001-06-25 05:01:22 +0000895static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100896process_mkdir(void)
897{
898 Attrib *a;
899 u_int32_t id;
900 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000901 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100902
903 id = get_int();
904 name = get_string(NULL);
905 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000906 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
907 a->perm & 0777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +1000908 debug3("request %u: mkdir", id);
909 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100910 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000911 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100912 send_status(id, status);
913 xfree(name);
914}
915
Ben Lindstrombba81212001-06-25 05:01:22 +0000916static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100917process_rmdir(void)
918{
919 u_int32_t id;
920 char *name;
921 int ret, status;
922
923 id = get_int();
924 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000925 debug3("request %u: rmdir", id);
926 logit("rmdir name \"%s\"", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100927 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000928 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100929 send_status(id, status);
930 xfree(name);
931}
932
Ben Lindstrombba81212001-06-25 05:01:22 +0000933static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100934process_realpath(void)
935{
936 char resolvedname[MAXPATHLEN];
937 u_int32_t id;
938 char *path;
939
940 id = get_int();
941 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000942 if (path[0] == '\0') {
943 xfree(path);
944 path = xstrdup(".");
945 }
Damien Millerfef95ad2006-07-10 20:46:55 +1000946 debug3("request %u: realpath", id);
947 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100948 if (realpath(path, resolvedname) == NULL) {
949 send_status(id, errno_to_portable(errno));
950 } else {
951 Stat s;
952 attrib_clear(&s.attrib);
953 s.name = s.long_name = resolvedname;
954 send_names(id, 1, &s);
955 }
956 xfree(path);
957}
958
Ben Lindstrombba81212001-06-25 05:01:22 +0000959static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100960process_rename(void)
961{
962 u_int32_t id;
963 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100964 int status;
Damien Millerb3207e82003-03-26 16:01:11 +1100965 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +1100966
967 id = get_int();
968 oldpath = get_string(NULL);
969 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000970 debug3("request %u: rename", id);
971 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +1100972 status = SSH2_FX_FAILURE;
973 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +1100974 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +1100975 else if (S_ISREG(sb.st_mode)) {
976 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000977 if (link(oldpath, newpath) == -1) {
Darren Tuckere59b5082004-06-28 16:01:19 +1000978 if (errno == EOPNOTSUPP
979#ifdef LINK_OPNOTSUPP_ERRNO
980 || errno == LINK_OPNOTSUPP_ERRNO
981#endif
982 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000983 struct stat st;
984
985 /*
986 * fs doesn't support links, so fall back to
987 * stat+rename. This is racy.
988 */
989 if (stat(newpath, &st) == -1) {
990 if (rename(oldpath, newpath) == -1)
991 status =
992 errno_to_portable(errno);
993 else
994 status = SSH2_FX_OK;
995 }
996 } else {
997 status = errno_to_portable(errno);
998 }
999 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001000 status = errno_to_portable(errno);
1001 /* clean spare link */
1002 unlink(newpath);
1003 } else
1004 status = SSH2_FX_OK;
1005 } else if (stat(newpath, &sb) == -1) {
1006 if (rename(oldpath, newpath) == -1)
1007 status = errno_to_portable(errno);
1008 else
1009 status = SSH2_FX_OK;
1010 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001011 send_status(id, status);
1012 xfree(oldpath);
1013 xfree(newpath);
1014}
1015
Ben Lindstrombba81212001-06-25 05:01:22 +00001016static void
Damien Miller058316f2001-03-08 10:08:49 +11001017process_readlink(void)
1018{
1019 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001020 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001021 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001022 char *path;
1023
1024 id = get_int();
1025 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001026 debug3("request %u: readlink", id);
1027 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001028 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001029 send_status(id, errno_to_portable(errno));
1030 else {
1031 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001032
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001033 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001034 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001035 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001036 send_names(id, 1, &s);
1037 }
1038 xfree(path);
1039}
1040
Ben Lindstrombba81212001-06-25 05:01:22 +00001041static void
Damien Miller058316f2001-03-08 10:08:49 +11001042process_symlink(void)
1043{
1044 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +11001045 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001046 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001047
1048 id = get_int();
1049 oldpath = get_string(NULL);
1050 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001051 debug3("request %u: symlink", id);
1052 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001053 /* this will fail if 'newpath' exists */
1054 ret = symlink(oldpath, newpath);
1055 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001056 send_status(id, status);
1057 xfree(oldpath);
1058 xfree(newpath);
1059}
1060
Ben Lindstrombba81212001-06-25 05:01:22 +00001061static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001062process_extended(void)
1063{
1064 u_int32_t id;
1065 char *request;
1066
1067 id = get_int();
1068 request = get_string(NULL);
1069 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1070 xfree(request);
1071}
Damien Miller7b28dc52000-09-05 13:34:53 +11001072
1073/* stolen from ssh-agent */
1074
Ben Lindstrombba81212001-06-25 05:01:22 +00001075static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001076process(void)
1077{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001078 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +00001079 u_int buf_len;
1080 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001081 u_int type;
1082 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +11001083
Ben Lindstrom2c140472002-06-06 21:57:54 +00001084 buf_len = buffer_len(&iqueue);
1085 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001086 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001087 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001088 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001089 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001090 error("bad message from %s local user %s",
1091 client_addr, pw->pw_name);
1092 cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001093 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001094 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001095 return;
1096 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001097 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001098 type = buffer_get_char(&iqueue);
1099 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001100 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001101 process_init();
1102 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001103 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +11001104 process_open();
1105 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001106 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001107 process_close();
1108 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001109 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +11001110 process_read();
1111 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001112 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001113 process_write();
1114 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001115 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001116 process_lstat();
1117 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001118 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001119 process_fstat();
1120 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001121 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001122 process_setstat();
1123 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001124 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001125 process_fsetstat();
1126 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001127 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001128 process_opendir();
1129 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001130 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001131 process_readdir();
1132 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001133 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001134 process_remove();
1135 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001136 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001137 process_mkdir();
1138 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001139 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001140 process_rmdir();
1141 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001142 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001143 process_realpath();
1144 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001145 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001146 process_stat();
1147 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001148 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001149 process_rename();
1150 break;
Damien Miller058316f2001-03-08 10:08:49 +11001151 case SSH2_FXP_READLINK:
1152 process_readlink();
1153 break;
1154 case SSH2_FXP_SYMLINK:
1155 process_symlink();
1156 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001157 case SSH2_FXP_EXTENDED:
1158 process_extended();
1159 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001160 default:
1161 error("Unknown message %d", type);
1162 break;
1163 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001164 /* discard the remaining bytes from the current packet */
1165 if (buf_len < buffer_len(&iqueue))
Damien Millerfef95ad2006-07-10 20:46:55 +10001166 fatal("iqueue grew unexpectedly");
Ben Lindstrom2c140472002-06-06 21:57:54 +00001167 consumed = buf_len - buffer_len(&iqueue);
1168 if (msg_len < consumed)
1169 fatal("msg_len %d < consumed %d", msg_len, consumed);
1170 if (msg_len > consumed)
1171 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001172}
1173
Damien Millerfef95ad2006-07-10 20:46:55 +10001174/* Cleanup handler that logs active handles upon normal exit */
1175void
1176cleanup_exit(int i)
1177{
1178 if (pw != NULL && client_addr != NULL) {
1179 handle_log_exit();
1180 logit("session closed for local user %s from [%s]",
1181 pw->pw_name, client_addr);
1182 }
1183 _exit(i);
1184}
1185
1186static void
1187usage(void)
1188{
1189 extern char *__progname;
1190
1191 fprintf(stderr,
1192 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1193 exit(1);
1194}
1195
Damien Miller7b28dc52000-09-05 13:34:53 +11001196int
Damien Millerfef95ad2006-07-10 20:46:55 +10001197main(int argc, char **argv)
Damien Miller7b28dc52000-09-05 13:34:53 +11001198{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001199 fd_set *rset, *wset;
Damien Millerfef95ad2006-07-10 20:46:55 +10001200 int in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001201 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001202 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1203 char *cp;
1204
Damien Millerfef95ad2006-07-10 20:46:55 +10001205 extern char *optarg;
1206 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001207
Darren Tuckerce321d82005-10-03 18:11:24 +10001208 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1209 sanitise_stdfd();
1210
Damien Millerfef95ad2006-07-10 20:46:55 +10001211 __progname = ssh_get_progname(argv[0]);
1212 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001213
Damien Millerfef95ad2006-07-10 20:46:55 +10001214 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1215 switch (ch) {
1216 case 'c':
1217 /*
1218 * Ignore all arguments if we are invoked as a
1219 * shell using "sftp-server -c command"
1220 */
1221 skipargs = 1;
1222 break;
1223 case 'e':
1224 log_stderr = 1;
1225 break;
1226 case 'l':
1227 log_level = log_level_number(optarg);
1228 if (log_level == SYSLOG_LEVEL_NOT_SET)
1229 error("Invalid log level \"%s\"", optarg);
1230 break;
1231 case 'f':
1232 log_facility = log_facility_number(optarg);
1233 if (log_level == SYSLOG_FACILITY_NOT_SET)
1234 error("Invalid log facility \"%s\"", optarg);
1235 break;
1236 case 'h':
1237 default:
1238 usage();
1239 }
1240 }
1241
1242 log_init(__progname, log_level, log_facility, log_stderr);
1243
1244 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1245 client_addr = xstrdup(cp);
1246 if ((cp = strchr(client_addr, ' ')) == NULL)
1247 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1248 getenv("SSH_CONNECTION"));
1249 *cp = '\0';
1250 } else
1251 client_addr = xstrdup("UNKNOWN");
1252
1253 if ((pw = getpwuid(getuid())) == NULL)
1254 fatal("No user found for uid %lu", (u_long)getuid());
1255 pw = pwcopy(pw);
1256
1257 logit("session opened for local user %s from [%s]",
1258 pw->pw_name, client_addr);
1259
Damien Miller7b28dc52000-09-05 13:34:53 +11001260 handle_init();
1261
Damien Miller7b28dc52000-09-05 13:34:53 +11001262 in = dup(STDIN_FILENO);
1263 out = dup(STDOUT_FILENO);
1264
Damien Miller402b3312001-04-14 00:28:42 +10001265#ifdef HAVE_CYGWIN
1266 setmode(in, O_BINARY);
1267 setmode(out, O_BINARY);
1268#endif
1269
Damien Miller7b28dc52000-09-05 13:34:53 +11001270 max = 0;
1271 if (in > max)
1272 max = in;
1273 if (out > max)
1274 max = out;
1275
1276 buffer_init(&iqueue);
1277 buffer_init(&oqueue);
1278
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001279 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1280 rset = (fd_set *)xmalloc(set_size);
1281 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001282
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001283 for (;;) {
1284 memset(rset, 0, set_size);
1285 memset(wset, 0, set_size);
1286
1287 FD_SET(in, rset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001288 olen = buffer_len(&oqueue);
1289 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001290 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001291
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001292 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001293 if (errno == EINTR)
1294 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001295 error("select: %s", strerror(errno));
1296 cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001297 }
1298
1299 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001300 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001301 char buf[4*4096];
1302 len = read(in, buf, sizeof buf);
1303 if (len == 0) {
1304 debug("read eof");
Damien Millerfef95ad2006-07-10 20:46:55 +10001305 cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001306 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001307 error("read: %s", strerror(errno));
1308 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001309 } else {
1310 buffer_append(&iqueue, buf, len);
1311 }
1312 }
1313 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001314 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001315 len = write(out, buffer_ptr(&oqueue), olen);
1316 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001317 error("write: %s", strerror(errno));
1318 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001319 } else {
1320 buffer_consume(&oqueue, len);
1321 }
1322 }
1323 /* process requests from client */
1324 process();
1325 }
1326}