blob: 6067f0203402c7ed10c380ad0be7f35af2128ca1 [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: sftp-server.c,v 1.65 2006/07/22 20:48:23 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 Millere3476ed2006-07-24 14:13:33 +100026#include <string.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100027#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100028#include <unistd.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110029
Damien Miller7b28dc52000-09-05 13:34:53 +110030#include "buffer.h"
31#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000032#include "log.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110033#include "xmalloc.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100034#include "misc.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100035#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110036
Ben Lindstrom2f959b42001-01-11 06:20:23 +000037#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110038#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110039
40/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000041#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110042#define get_int() buffer_get_int(&iqueue);
43#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Damien Miller7b28dc52000-09-05 13:34:53 +110044
Damien Millerfef95ad2006-07-10 20:46:55 +100045/* Our verbosity */
46LogLevel log_level = SYSLOG_LEVEL_ERROR;
47
48/* Our client */
49struct passwd *pw = NULL;
50char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000051
Damien Miller7b28dc52000-09-05 13:34:53 +110052/* input and output queue */
53Buffer iqueue;
54Buffer oqueue;
55
Damien Miller058316f2001-03-08 10:08:49 +110056/* Version of client */
57int version;
58
Darren Tuckera6612d42003-06-28 12:39:03 +100059/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110060
Damien Miller7b28dc52000-09-05 13:34:53 +110061typedef struct Stat Stat;
62
Damien Miller33804262001-02-04 23:20:18 +110063struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110064 char *name;
65 char *long_name;
66 Attrib attrib;
67};
68
Ben Lindstrombba81212001-06-25 05:01:22 +000069static int
Damien Miller7b28dc52000-09-05 13:34:53 +110070errno_to_portable(int unixerrno)
71{
72 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000073
Damien Miller7b28dc52000-09-05 13:34:53 +110074 switch (unixerrno) {
75 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000076 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110077 break;
78 case ENOENT:
79 case ENOTDIR:
80 case EBADF:
81 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000082 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110083 break;
84 case EPERM:
85 case EACCES:
86 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000087 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110088 break;
89 case ENAMETOOLONG:
90 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000091 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +110092 break;
93 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000094 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +110095 break;
96 }
97 return ret;
98}
99
Ben Lindstrombba81212001-06-25 05:01:22 +0000100static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100101flags_from_portable(int pflags)
102{
103 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000104
Ben Lindstrom36592512001-03-05 05:02:08 +0000105 if ((pflags & SSH2_FXF_READ) &&
106 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100107 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000108 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100109 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000110 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100111 flags = O_WRONLY;
112 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000113 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100114 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000115 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100116 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000117 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100118 flags |= O_EXCL;
119 return flags;
120}
121
Damien Millerfef95ad2006-07-10 20:46:55 +1000122static const char *
123string_from_portable(int pflags)
124{
125 static char ret[128];
126
127 *ret = '\0';
128
129#define PAPPEND(str) { \
130 if (*ret != '\0') \
131 strlcat(ret, ",", sizeof(ret)); \
132 strlcat(ret, str, sizeof(ret)); \
133 }
134
135 if (pflags & SSH2_FXF_READ)
136 PAPPEND("READ")
137 if (pflags & SSH2_FXF_WRITE)
138 PAPPEND("WRITE")
139 if (pflags & SSH2_FXF_CREAT)
140 PAPPEND("CREATE")
141 if (pflags & SSH2_FXF_TRUNC)
142 PAPPEND("TRUNCATE")
143 if (pflags & SSH2_FXF_EXCL)
144 PAPPEND("EXCL")
145
146 return ret;
147}
148
Ben Lindstrombba81212001-06-25 05:01:22 +0000149static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100150get_attrib(void)
151{
152 return decode_attrib(&iqueue);
153}
154
155/* handle handles */
156
157typedef struct Handle Handle;
158struct Handle {
159 int use;
160 DIR *dirp;
161 int fd;
162 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000163 u_int64_t bytes_read, bytes_write;
Damien Miller7b28dc52000-09-05 13:34:53 +1100164};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000165
Damien Miller7b28dc52000-09-05 13:34:53 +1100166enum {
167 HANDLE_UNUSED,
168 HANDLE_DIR,
169 HANDLE_FILE
170};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000171
Damien Miller7b28dc52000-09-05 13:34:53 +1100172Handle handles[100];
173
Ben Lindstrombba81212001-06-25 05:01:22 +0000174static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100175handle_init(void)
176{
Damien Millereccb9de2005-06-17 12:59:34 +1000177 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000178
Damien Miller9f0f5c62001-12-21 14:45:46 +1100179 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
Damien Miller7b28dc52000-09-05 13:34:53 +1100180 handles[i].use = HANDLE_UNUSED;
181}
182
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100184handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100185{
Damien Millereccb9de2005-06-17 12:59:34 +1000186 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000187
Damien Miller9f0f5c62001-12-21 14:45:46 +1100188 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100189 if (handles[i].use == HANDLE_UNUSED) {
190 handles[i].use = use;
191 handles[i].dirp = dirp;
192 handles[i].fd = fd;
Damien Miller00111382003-03-10 11:21:17 +1100193 handles[i].name = xstrdup(name);
Damien Millerfef95ad2006-07-10 20:46:55 +1000194 handles[i].bytes_read = handles[i].bytes_write = 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100195 return i;
196 }
197 }
198 return -1;
199}
200
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100202handle_is_ok(int i, int type)
203{
Damien Millereccb9de2005-06-17 12:59:34 +1000204 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000205 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100206}
207
Ben Lindstrombba81212001-06-25 05:01:22 +0000208static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100209handle_to_string(int handle, char **stringp, int *hlenp)
210{
Damien Miller7b28dc52000-09-05 13:34:53 +1100211 if (stringp == NULL || hlenp == NULL)
212 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000213 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100214 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000215 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100216 return 0;
217}
218
Ben Lindstrombba81212001-06-25 05:01:22 +0000219static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100220handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100221{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000222 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000223
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000224 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100225 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100226 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100227 if (handle_is_ok(val, HANDLE_FILE) ||
228 handle_is_ok(val, HANDLE_DIR))
229 return val;
230 return -1;
231}
232
Ben Lindstrombba81212001-06-25 05:01:22 +0000233static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100234handle_to_name(int handle)
235{
236 if (handle_is_ok(handle, HANDLE_DIR)||
237 handle_is_ok(handle, HANDLE_FILE))
238 return handles[handle].name;
239 return NULL;
240}
241
Ben Lindstrombba81212001-06-25 05:01:22 +0000242static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100243handle_to_dir(int handle)
244{
245 if (handle_is_ok(handle, HANDLE_DIR))
246 return handles[handle].dirp;
247 return NULL;
248}
249
Ben Lindstrombba81212001-06-25 05:01:22 +0000250static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100251handle_to_fd(int handle)
252{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000253 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100254 return handles[handle].fd;
255 return -1;
256}
257
Damien Millerfef95ad2006-07-10 20:46:55 +1000258static void
259handle_update_read(int handle, ssize_t bytes)
260{
261 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
262 handles[handle].bytes_read += bytes;
263}
264
265static void
266handle_update_write(int handle, ssize_t bytes)
267{
268 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
269 handles[handle].bytes_write += bytes;
270}
271
272static u_int64_t
273handle_bytes_read(int handle)
274{
275 if (handle_is_ok(handle, HANDLE_FILE))
276 return (handles[handle].bytes_read);
277 return 0;
278}
279
280static u_int64_t
281handle_bytes_write(int handle)
282{
283 if (handle_is_ok(handle, HANDLE_FILE))
284 return (handles[handle].bytes_write);
285 return 0;
286}
287
Ben Lindstrombba81212001-06-25 05:01:22 +0000288static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100289handle_close(int handle)
290{
291 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000292
Damien Miller7b28dc52000-09-05 13:34:53 +1100293 if (handle_is_ok(handle, HANDLE_FILE)) {
294 ret = close(handles[handle].fd);
295 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100296 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100297 } else if (handle_is_ok(handle, HANDLE_DIR)) {
298 ret = closedir(handles[handle].dirp);
299 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100300 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100301 } else {
302 errno = ENOENT;
303 }
304 return ret;
305}
306
Damien Millerfef95ad2006-07-10 20:46:55 +1000307static void
308handle_log_close(int handle, char *emsg)
309{
310 if (handle_is_ok(handle, HANDLE_FILE)) {
311 logit("%s%sclose \"%s\" bytes read %llu written %llu",
312 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
313 handle_to_name(handle),
314 handle_bytes_read(handle), handle_bytes_write(handle));
315 } else {
316 logit("%s%sclosedir \"%s\"",
317 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
318 handle_to_name(handle));
319 }
320}
321
322static void
323handle_log_exit(void)
324{
325 u_int i;
326
327 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
328 if (handles[i].use != HANDLE_UNUSED)
329 handle_log_close(i, "forced");
330}
331
Ben Lindstrombba81212001-06-25 05:01:22 +0000332static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100333get_handle(void)
334{
335 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000336 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100337 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000338
Damien Miller7b28dc52000-09-05 13:34:53 +1100339 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000340 if (hlen < 256)
341 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100342 xfree(handle);
343 return val;
344}
345
346/* send replies */
347
Ben Lindstrombba81212001-06-25 05:01:22 +0000348static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100349send_msg(Buffer *m)
350{
351 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000352
Damien Miller7b28dc52000-09-05 13:34:53 +1100353 buffer_put_int(&oqueue, mlen);
354 buffer_append(&oqueue, buffer_ptr(m), mlen);
355 buffer_consume(m, mlen);
356}
357
Damien Millerfef95ad2006-07-10 20:46:55 +1000358static const char *
359status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100360{
Damien Miller058316f2001-03-08 10:08:49 +1100361 const char *status_messages[] = {
362 "Success", /* SSH_FX_OK */
363 "End of file", /* SSH_FX_EOF */
364 "No such file", /* SSH_FX_NO_SUCH_FILE */
365 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
366 "Failure", /* SSH_FX_FAILURE */
367 "Bad message", /* SSH_FX_BAD_MESSAGE */
368 "No connection", /* SSH_FX_NO_CONNECTION */
369 "Connection lost", /* SSH_FX_CONNECTION_LOST */
370 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
371 "Unknown error" /* Others */
372 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000373 return (status_messages[MIN(status,SSH2_FX_MAX)]);
374}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000375
Damien Millerfef95ad2006-07-10 20:46:55 +1000376static void
377send_status(u_int32_t id, u_int32_t status)
378{
379 Buffer msg;
380
381 debug3("request %u: sent status %u", id, status);
382 if (log_level > SYSLOG_LEVEL_VERBOSE ||
383 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
384 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100385 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000386 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100387 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000388 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100389 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000390 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100391 buffer_put_cstring(&msg, "");
392 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100393 send_msg(&msg);
394 buffer_free(&msg);
395}
Ben Lindstrombba81212001-06-25 05:01:22 +0000396static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100397send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100398{
399 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000400
Damien Miller7b28dc52000-09-05 13:34:53 +1100401 buffer_init(&msg);
402 buffer_put_char(&msg, type);
403 buffer_put_int(&msg, id);
404 buffer_put_string(&msg, data, dlen);
405 send_msg(&msg);
406 buffer_free(&msg);
407}
408
Ben Lindstrombba81212001-06-25 05:01:22 +0000409static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100410send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100411{
Damien Millerfef95ad2006-07-10 20:46:55 +1000412 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000413 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100414}
415
Ben Lindstrombba81212001-06-25 05:01:22 +0000416static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100417send_handle(u_int32_t id, int handle)
418{
419 char *string;
420 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000421
Damien Miller7b28dc52000-09-05 13:34:53 +1100422 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000423 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000424 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100425 xfree(string);
426}
427
Ben Lindstrombba81212001-06-25 05:01:22 +0000428static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100429send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100430{
431 Buffer msg;
432 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000433
Damien Miller7b28dc52000-09-05 13:34:53 +1100434 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000435 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100436 buffer_put_int(&msg, id);
437 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000438 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100439 for (i = 0; i < count; i++) {
440 buffer_put_cstring(&msg, stats[i].name);
441 buffer_put_cstring(&msg, stats[i].long_name);
442 encode_attrib(&msg, &stats[i].attrib);
443 }
444 send_msg(&msg);
445 buffer_free(&msg);
446}
447
Ben Lindstrombba81212001-06-25 05:01:22 +0000448static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100449send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100450{
451 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000452
Damien Millerfef95ad2006-07-10 20:46:55 +1000453 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100454 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000455 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100456 buffer_put_int(&msg, id);
457 encode_attrib(&msg, a);
458 send_msg(&msg);
459 buffer_free(&msg);
460}
461
462/* parse incoming */
463
Ben Lindstrombba81212001-06-25 05:01:22 +0000464static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100465process_init(void)
466{
467 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100468
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000469 version = get_int();
Damien Millerfef95ad2006-07-10 20:46:55 +1000470 verbose("received client version %d", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100471 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000472 buffer_put_char(&msg, SSH2_FXP_VERSION);
473 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100474 send_msg(&msg);
475 buffer_free(&msg);
476}
477
Ben Lindstrombba81212001-06-25 05:01:22 +0000478static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100479process_open(void)
480{
481 u_int32_t id, pflags;
482 Attrib *a;
483 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000484 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100485
486 id = get_int();
487 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000488 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000489 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100490 a = get_attrib();
491 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000492 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000493 logit("open \"%s\" flags %s mode 0%o",
494 name, string_from_portable(pflags), mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100495 fd = open(name, flags, mode);
496 if (fd < 0) {
497 status = errno_to_portable(errno);
498 } else {
Damien Miller00111382003-03-10 11:21:17 +1100499 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100500 if (handle < 0) {
501 close(fd);
502 } else {
503 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000504 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100505 }
506 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000507 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100508 send_status(id, status);
509 xfree(name);
510}
511
Ben Lindstrombba81212001-06-25 05:01:22 +0000512static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100513process_close(void)
514{
515 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000516 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100517
518 id = get_int();
519 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000520 debug3("request %u: close handle %u", id, handle);
521 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100522 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000523 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100524 send_status(id, status);
525}
526
Ben Lindstrombba81212001-06-25 05:01:22 +0000527static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100528process_read(void)
529{
530 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000531 u_int32_t id, len;
532 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100533 u_int64_t off;
534
535 id = get_int();
536 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000537 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100538 len = get_int();
539
Damien Millerfef95ad2006-07-10 20:46:55 +1000540 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
541 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100542 if (len > sizeof buf) {
543 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000544 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100545 }
546 fd = handle_to_fd(handle);
547 if (fd >= 0) {
548 if (lseek(fd, off, SEEK_SET) < 0) {
549 error("process_read: seek failed");
550 status = errno_to_portable(errno);
551 } else {
552 ret = read(fd, buf, len);
553 if (ret < 0) {
554 status = errno_to_portable(errno);
555 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000556 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100557 } else {
558 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000559 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000560 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100561 }
562 }
563 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000564 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100565 send_status(id, status);
566}
567
Ben Lindstrombba81212001-06-25 05:01:22 +0000568static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100569process_write(void)
570{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000571 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100572 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100573 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000574 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100575 char *data;
576
577 id = get_int();
578 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000579 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100580 data = get_string(&len);
581
Damien Millerfef95ad2006-07-10 20:46:55 +1000582 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
583 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100584 fd = handle_to_fd(handle);
585 if (fd >= 0) {
586 if (lseek(fd, off, SEEK_SET) < 0) {
587 status = errno_to_portable(errno);
588 error("process_write: seek failed");
589 } else {
590/* XXX ATOMICIO ? */
591 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000592 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100593 error("process_write: write failed");
594 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000595 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000596 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000597 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100598 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000599 debug2("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100600 }
601 }
602 }
603 send_status(id, status);
604 xfree(data);
605}
606
Ben Lindstrombba81212001-06-25 05:01:22 +0000607static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100608process_do_stat(int do_lstat)
609{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000610 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100611 struct stat st;
612 u_int32_t id;
613 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000614 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100615
616 id = get_int();
617 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000618 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
619 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100620 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
621 if (ret < 0) {
622 status = errno_to_portable(errno);
623 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000624 stat_to_attrib(&st, &a);
625 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000626 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100627 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000628 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100629 send_status(id, status);
630 xfree(name);
631}
632
Ben Lindstrombba81212001-06-25 05:01:22 +0000633static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100634process_stat(void)
635{
636 process_do_stat(0);
637}
638
Ben Lindstrombba81212001-06-25 05:01:22 +0000639static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100640process_lstat(void)
641{
642 process_do_stat(1);
643}
644
Ben Lindstrombba81212001-06-25 05:01:22 +0000645static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100646process_fstat(void)
647{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000648 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100649 struct stat st;
650 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000651 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100652
653 id = get_int();
654 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000655 debug("request %u: fstat \"%s\" (handle %u)",
656 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100657 fd = handle_to_fd(handle);
658 if (fd >= 0) {
659 ret = fstat(fd, &st);
660 if (ret < 0) {
661 status = errno_to_portable(errno);
662 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000663 stat_to_attrib(&st, &a);
664 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000665 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100666 }
667 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000668 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100669 send_status(id, status);
670}
671
Ben Lindstrombba81212001-06-25 05:01:22 +0000672static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100673attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100674{
675 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000676
Damien Miller7b28dc52000-09-05 13:34:53 +1100677 tv[0].tv_sec = a->atime;
678 tv[0].tv_usec = 0;
679 tv[1].tv_sec = a->mtime;
680 tv[1].tv_usec = 0;
681 return tv;
682}
683
Ben Lindstrombba81212001-06-25 05:01:22 +0000684static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100685process_setstat(void)
686{
687 Attrib *a;
688 u_int32_t id;
689 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000690 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100691
692 id = get_int();
693 name = get_string(NULL);
694 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000695 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100696 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000697 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100698 ret = truncate(name, a->size);
699 if (ret == -1)
700 status = errno_to_portable(errno);
701 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000702 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000703 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller7b28dc52000-09-05 13:34:53 +1100704 ret = chmod(name, a->perm & 0777);
705 if (ret == -1)
706 status = errno_to_portable(errno);
707 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000708 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000709 char buf[64];
710 time_t t = a->mtime;
711
712 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
713 localtime(&t));
714 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100715 ret = utimes(name, attrib_to_tv(a));
716 if (ret == -1)
717 status = errno_to_portable(errno);
718 }
Kevin Steves8e743932001-02-05 13:24:35 +0000719 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000720 logit("set \"%s\" owner %lu group %lu", name,
721 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000722 ret = chown(name, a->uid, a->gid);
723 if (ret == -1)
724 status = errno_to_portable(errno);
725 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100726 send_status(id, status);
727 xfree(name);
728}
729
Ben Lindstrombba81212001-06-25 05:01:22 +0000730static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100731process_fsetstat(void)
732{
733 Attrib *a;
734 u_int32_t id;
735 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000736 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000737
Damien Miller7b28dc52000-09-05 13:34:53 +1100738 id = get_int();
739 handle = get_handle();
740 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000741 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100742 fd = handle_to_fd(handle);
Damien Millerfef95ad2006-07-10 20:46:55 +1000743 if (fd < 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000744 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100745 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000746 char *name = handle_to_name(handle);
747
Damien Miller00c92172002-02-13 14:05:00 +1100748 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000749 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100750 ret = ftruncate(fd, a->size);
751 if (ret == -1)
752 status = errno_to_portable(errno);
753 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000754 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000755 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000756#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100757 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000758#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000759 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000760#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100761 if (ret == -1)
762 status = errno_to_portable(errno);
763 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000764 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000765 char buf[64];
766 time_t t = a->mtime;
767
768 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
769 localtime(&t));
770 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100771#ifdef HAVE_FUTIMES
772 ret = futimes(fd, attrib_to_tv(a));
773#else
774 ret = utimes(name, attrib_to_tv(a));
775#endif
776 if (ret == -1)
777 status = errno_to_portable(errno);
778 }
Kevin Steves8e743932001-02-05 13:24:35 +0000779 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000780 logit("set \"%s\" owner %lu group %lu", name,
781 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000782#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000783 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000784#else
785 ret = chown(name, a->uid, a->gid);
786#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000787 if (ret == -1)
788 status = errno_to_portable(errno);
789 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100790 }
791 send_status(id, status);
792}
793
Ben Lindstrombba81212001-06-25 05:01:22 +0000794static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100795process_opendir(void)
796{
797 DIR *dirp = NULL;
798 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000799 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100800 u_int32_t id;
801
802 id = get_int();
803 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000804 debug3("request %u: opendir", id);
805 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000806 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100807 if (dirp == NULL) {
808 status = errno_to_portable(errno);
809 } else {
Damien Miller00111382003-03-10 11:21:17 +1100810 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100811 if (handle < 0) {
812 closedir(dirp);
813 } else {
814 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000815 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100816 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000817
Damien Miller7b28dc52000-09-05 13:34:53 +1100818 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000819 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100820 send_status(id, status);
821 xfree(path);
822}
823
Ben Lindstrombba81212001-06-25 05:01:22 +0000824static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100825process_readdir(void)
826{
827 DIR *dirp;
828 struct dirent *dp;
829 char *path;
830 int handle;
831 u_int32_t id;
832
833 id = get_int();
834 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000835 debug("request %u: readdir \"%s\" (handle %d)", id,
836 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100837 dirp = handle_to_dir(handle);
838 path = handle_to_name(handle);
839 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000840 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100841 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100842 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +1000843 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +1100844 Stat *stats;
845 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000846
Damien Miller07d86be2006-03-26 14:19:21 +1100847 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100848 while ((dp = readdir(dirp)) != NULL) {
849 if (count >= nstats) {
850 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +1100851 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100852 }
853/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000854 snprintf(pathname, sizeof pathname, "%s%s%s", path,
855 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100856 if (lstat(pathname, &st) < 0)
857 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000858 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100859 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000860 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100861 count++;
862 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000863 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100864 if (count == 100)
865 break;
866 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000867 if (count > 0) {
868 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100869 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000870 xfree(stats[i].name);
871 xfree(stats[i].long_name);
872 }
873 } else {
874 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100875 }
876 xfree(stats);
877 }
878}
879
Ben Lindstrombba81212001-06-25 05:01:22 +0000880static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100881process_remove(void)
882{
883 char *name;
884 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000885 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100886 int ret;
887
888 id = get_int();
889 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000890 debug3("request %u: remove", id);
891 logit("remove name \"%s\"", name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000892 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000893 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100894 send_status(id, status);
895 xfree(name);
896}
897
Ben Lindstrombba81212001-06-25 05:01:22 +0000898static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100899process_mkdir(void)
900{
901 Attrib *a;
902 u_int32_t id;
903 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000904 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100905
906 id = get_int();
907 name = get_string(NULL);
908 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000909 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
910 a->perm & 0777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +1000911 debug3("request %u: mkdir", id);
912 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100913 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000914 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100915 send_status(id, status);
916 xfree(name);
917}
918
Ben Lindstrombba81212001-06-25 05:01:22 +0000919static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100920process_rmdir(void)
921{
922 u_int32_t id;
923 char *name;
924 int ret, status;
925
926 id = get_int();
927 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000928 debug3("request %u: rmdir", id);
929 logit("rmdir name \"%s\"", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100930 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000931 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100932 send_status(id, status);
933 xfree(name);
934}
935
Ben Lindstrombba81212001-06-25 05:01:22 +0000936static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100937process_realpath(void)
938{
939 char resolvedname[MAXPATHLEN];
940 u_int32_t id;
941 char *path;
942
943 id = get_int();
944 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000945 if (path[0] == '\0') {
946 xfree(path);
947 path = xstrdup(".");
948 }
Damien Millerfef95ad2006-07-10 20:46:55 +1000949 debug3("request %u: realpath", id);
950 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100951 if (realpath(path, resolvedname) == NULL) {
952 send_status(id, errno_to_portable(errno));
953 } else {
954 Stat s;
955 attrib_clear(&s.attrib);
956 s.name = s.long_name = resolvedname;
957 send_names(id, 1, &s);
958 }
959 xfree(path);
960}
961
Ben Lindstrombba81212001-06-25 05:01:22 +0000962static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100963process_rename(void)
964{
965 u_int32_t id;
966 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100967 int status;
Damien Millerb3207e82003-03-26 16:01:11 +1100968 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +1100969
970 id = get_int();
971 oldpath = get_string(NULL);
972 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000973 debug3("request %u: rename", id);
974 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +1100975 status = SSH2_FX_FAILURE;
976 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +1100977 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +1100978 else if (S_ISREG(sb.st_mode)) {
979 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000980 if (link(oldpath, newpath) == -1) {
Darren Tuckere59b5082004-06-28 16:01:19 +1000981 if (errno == EOPNOTSUPP
982#ifdef LINK_OPNOTSUPP_ERRNO
983 || errno == LINK_OPNOTSUPP_ERRNO
984#endif
985 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000986 struct stat st;
987
988 /*
989 * fs doesn't support links, so fall back to
990 * stat+rename. This is racy.
991 */
992 if (stat(newpath, &st) == -1) {
993 if (rename(oldpath, newpath) == -1)
994 status =
995 errno_to_portable(errno);
996 else
997 status = SSH2_FX_OK;
998 }
999 } else {
1000 status = errno_to_portable(errno);
1001 }
1002 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001003 status = errno_to_portable(errno);
1004 /* clean spare link */
1005 unlink(newpath);
1006 } else
1007 status = SSH2_FX_OK;
1008 } else if (stat(newpath, &sb) == -1) {
1009 if (rename(oldpath, newpath) == -1)
1010 status = errno_to_portable(errno);
1011 else
1012 status = SSH2_FX_OK;
1013 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001014 send_status(id, status);
1015 xfree(oldpath);
1016 xfree(newpath);
1017}
1018
Ben Lindstrombba81212001-06-25 05:01:22 +00001019static void
Damien Miller058316f2001-03-08 10:08:49 +11001020process_readlink(void)
1021{
1022 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001023 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001024 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001025 char *path;
1026
1027 id = get_int();
1028 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001029 debug3("request %u: readlink", id);
1030 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001031 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001032 send_status(id, errno_to_portable(errno));
1033 else {
1034 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001035
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001036 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001037 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001038 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001039 send_names(id, 1, &s);
1040 }
1041 xfree(path);
1042}
1043
Ben Lindstrombba81212001-06-25 05:01:22 +00001044static void
Damien Miller058316f2001-03-08 10:08:49 +11001045process_symlink(void)
1046{
1047 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +11001048 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001049 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001050
1051 id = get_int();
1052 oldpath = get_string(NULL);
1053 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001054 debug3("request %u: symlink", id);
1055 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001056 /* this will fail if 'newpath' exists */
1057 ret = symlink(oldpath, newpath);
1058 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001059 send_status(id, status);
1060 xfree(oldpath);
1061 xfree(newpath);
1062}
1063
Ben Lindstrombba81212001-06-25 05:01:22 +00001064static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001065process_extended(void)
1066{
1067 u_int32_t id;
1068 char *request;
1069
1070 id = get_int();
1071 request = get_string(NULL);
1072 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1073 xfree(request);
1074}
Damien Miller7b28dc52000-09-05 13:34:53 +11001075
1076/* stolen from ssh-agent */
1077
Ben Lindstrombba81212001-06-25 05:01:22 +00001078static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001079process(void)
1080{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001081 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +00001082 u_int buf_len;
1083 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001084 u_int type;
1085 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +11001086
Ben Lindstrom2c140472002-06-06 21:57:54 +00001087 buf_len = buffer_len(&iqueue);
1088 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001089 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001090 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001091 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001092 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001093 error("bad message from %s local user %s",
1094 client_addr, pw->pw_name);
1095 cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001096 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001097 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001098 return;
1099 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001100 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001101 type = buffer_get_char(&iqueue);
1102 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001103 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001104 process_init();
1105 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001106 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +11001107 process_open();
1108 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001109 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001110 process_close();
1111 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001112 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +11001113 process_read();
1114 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001115 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001116 process_write();
1117 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001118 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001119 process_lstat();
1120 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001121 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001122 process_fstat();
1123 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001124 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001125 process_setstat();
1126 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001127 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001128 process_fsetstat();
1129 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001130 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001131 process_opendir();
1132 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001133 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001134 process_readdir();
1135 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001136 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001137 process_remove();
1138 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001139 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001140 process_mkdir();
1141 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001142 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001143 process_rmdir();
1144 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001145 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001146 process_realpath();
1147 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001148 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001149 process_stat();
1150 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001151 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001152 process_rename();
1153 break;
Damien Miller058316f2001-03-08 10:08:49 +11001154 case SSH2_FXP_READLINK:
1155 process_readlink();
1156 break;
1157 case SSH2_FXP_SYMLINK:
1158 process_symlink();
1159 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001160 case SSH2_FXP_EXTENDED:
1161 process_extended();
1162 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001163 default:
1164 error("Unknown message %d", type);
1165 break;
1166 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001167 /* discard the remaining bytes from the current packet */
1168 if (buf_len < buffer_len(&iqueue))
Damien Millerfef95ad2006-07-10 20:46:55 +10001169 fatal("iqueue grew unexpectedly");
Ben Lindstrom2c140472002-06-06 21:57:54 +00001170 consumed = buf_len - buffer_len(&iqueue);
1171 if (msg_len < consumed)
1172 fatal("msg_len %d < consumed %d", msg_len, consumed);
1173 if (msg_len > consumed)
1174 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001175}
1176
Damien Millerfef95ad2006-07-10 20:46:55 +10001177/* Cleanup handler that logs active handles upon normal exit */
1178void
1179cleanup_exit(int i)
1180{
1181 if (pw != NULL && client_addr != NULL) {
1182 handle_log_exit();
1183 logit("session closed for local user %s from [%s]",
1184 pw->pw_name, client_addr);
1185 }
1186 _exit(i);
1187}
1188
1189static void
1190usage(void)
1191{
1192 extern char *__progname;
1193
1194 fprintf(stderr,
1195 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1196 exit(1);
1197}
1198
Damien Miller7b28dc52000-09-05 13:34:53 +11001199int
Damien Millerfef95ad2006-07-10 20:46:55 +10001200main(int argc, char **argv)
Damien Miller7b28dc52000-09-05 13:34:53 +11001201{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001202 fd_set *rset, *wset;
Damien Millerfef95ad2006-07-10 20:46:55 +10001203 int in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001204 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001205 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1206 char *cp;
1207
Damien Millerfef95ad2006-07-10 20:46:55 +10001208 extern char *optarg;
1209 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001210
Darren Tuckerce321d82005-10-03 18:11:24 +10001211 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1212 sanitise_stdfd();
1213
Damien Millerfef95ad2006-07-10 20:46:55 +10001214 __progname = ssh_get_progname(argv[0]);
1215 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001216
Damien Millerfef95ad2006-07-10 20:46:55 +10001217 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1218 switch (ch) {
1219 case 'c':
1220 /*
1221 * Ignore all arguments if we are invoked as a
1222 * shell using "sftp-server -c command"
1223 */
1224 skipargs = 1;
1225 break;
1226 case 'e':
1227 log_stderr = 1;
1228 break;
1229 case 'l':
1230 log_level = log_level_number(optarg);
1231 if (log_level == SYSLOG_LEVEL_NOT_SET)
1232 error("Invalid log level \"%s\"", optarg);
1233 break;
1234 case 'f':
1235 log_facility = log_facility_number(optarg);
1236 if (log_level == SYSLOG_FACILITY_NOT_SET)
1237 error("Invalid log facility \"%s\"", optarg);
1238 break;
1239 case 'h':
1240 default:
1241 usage();
1242 }
1243 }
1244
1245 log_init(__progname, log_level, log_facility, log_stderr);
1246
1247 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1248 client_addr = xstrdup(cp);
1249 if ((cp = strchr(client_addr, ' ')) == NULL)
1250 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1251 getenv("SSH_CONNECTION"));
1252 *cp = '\0';
1253 } else
1254 client_addr = xstrdup("UNKNOWN");
1255
1256 if ((pw = getpwuid(getuid())) == NULL)
1257 fatal("No user found for uid %lu", (u_long)getuid());
1258 pw = pwcopy(pw);
1259
1260 logit("session opened for local user %s from [%s]",
1261 pw->pw_name, client_addr);
1262
Damien Miller7b28dc52000-09-05 13:34:53 +11001263 handle_init();
1264
Damien Miller7b28dc52000-09-05 13:34:53 +11001265 in = dup(STDIN_FILENO);
1266 out = dup(STDOUT_FILENO);
1267
Damien Miller402b3312001-04-14 00:28:42 +10001268#ifdef HAVE_CYGWIN
1269 setmode(in, O_BINARY);
1270 setmode(out, O_BINARY);
1271#endif
1272
Damien Miller7b28dc52000-09-05 13:34:53 +11001273 max = 0;
1274 if (in > max)
1275 max = in;
1276 if (out > max)
1277 max = out;
1278
1279 buffer_init(&iqueue);
1280 buffer_init(&oqueue);
1281
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001282 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1283 rset = (fd_set *)xmalloc(set_size);
1284 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001285
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001286 for (;;) {
1287 memset(rset, 0, set_size);
1288 memset(wset, 0, set_size);
1289
1290 FD_SET(in, rset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001291 olen = buffer_len(&oqueue);
1292 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001293 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001294
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001295 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001296 if (errno == EINTR)
1297 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001298 error("select: %s", strerror(errno));
1299 cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001300 }
1301
1302 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001303 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001304 char buf[4*4096];
1305 len = read(in, buf, sizeof buf);
1306 if (len == 0) {
1307 debug("read eof");
Damien Millerfef95ad2006-07-10 20:46:55 +10001308 cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001309 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001310 error("read: %s", strerror(errno));
1311 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001312 } else {
1313 buffer_append(&iqueue, buf, len);
1314 }
1315 }
1316 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001317 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001318 len = write(out, buffer_ptr(&oqueue), olen);
1319 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001320 error("write: %s", strerror(errno));
1321 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001322 } else {
1323 buffer_consume(&oqueue, len);
1324 }
1325 }
1326 /* process requests from client */
1327 process();
1328 }
1329}