blob: 13ba8e6257882e57e8a8f81638dd9d70d75dffb8 [file] [log] [blame]
Damien Miller9aec9192006-08-05 10:57:45 +10001/* $OpenBSD: sftp-server.c,v 1.66 2006/07/25 02:59:21 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 Miller9aec9192006-08-05 10:57:45 +100021#ifdef HAVE_SYS_TIME_H
22# include <sys/time.h>
23#endif
Damien Miller88f254b2006-03-15 11:25:13 +110024
25#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100027#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100028#include <pwd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100029#include <string.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100030#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100031#include <unistd.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110032
Damien Miller7b28dc52000-09-05 13:34:53 +110033#include "buffer.h"
34#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "log.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110036#include "xmalloc.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100037#include "misc.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100038#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110039
Ben Lindstrom2f959b42001-01-11 06:20:23 +000040#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110041#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110042
43/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000044#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110045#define get_int() buffer_get_int(&iqueue);
46#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Damien Miller7b28dc52000-09-05 13:34:53 +110047
Damien Millerfef95ad2006-07-10 20:46:55 +100048/* Our verbosity */
49LogLevel log_level = SYSLOG_LEVEL_ERROR;
50
51/* Our client */
52struct passwd *pw = NULL;
53char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000054
Damien Miller7b28dc52000-09-05 13:34:53 +110055/* input and output queue */
56Buffer iqueue;
57Buffer oqueue;
58
Damien Miller058316f2001-03-08 10:08:49 +110059/* Version of client */
60int version;
61
Darren Tuckera6612d42003-06-28 12:39:03 +100062/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110063
Damien Miller7b28dc52000-09-05 13:34:53 +110064typedef struct Stat Stat;
65
Damien Miller33804262001-02-04 23:20:18 +110066struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110067 char *name;
68 char *long_name;
69 Attrib attrib;
70};
71
Ben Lindstrombba81212001-06-25 05:01:22 +000072static int
Damien Miller7b28dc52000-09-05 13:34:53 +110073errno_to_portable(int unixerrno)
74{
75 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000076
Damien Miller7b28dc52000-09-05 13:34:53 +110077 switch (unixerrno) {
78 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000079 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110080 break;
81 case ENOENT:
82 case ENOTDIR:
83 case EBADF:
84 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000085 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110086 break;
87 case EPERM:
88 case EACCES:
89 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000090 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110091 break;
92 case ENAMETOOLONG:
93 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000094 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +110095 break;
96 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000097 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +110098 break;
99 }
100 return ret;
101}
102
Ben Lindstrombba81212001-06-25 05:01:22 +0000103static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100104flags_from_portable(int pflags)
105{
106 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000107
Ben Lindstrom36592512001-03-05 05:02:08 +0000108 if ((pflags & SSH2_FXF_READ) &&
109 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100110 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000111 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100112 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000113 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100114 flags = O_WRONLY;
115 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000116 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100117 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000118 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100119 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000120 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100121 flags |= O_EXCL;
122 return flags;
123}
124
Damien Millerfef95ad2006-07-10 20:46:55 +1000125static const char *
126string_from_portable(int pflags)
127{
128 static char ret[128];
129
130 *ret = '\0';
131
132#define PAPPEND(str) { \
133 if (*ret != '\0') \
134 strlcat(ret, ",", sizeof(ret)); \
135 strlcat(ret, str, sizeof(ret)); \
136 }
137
138 if (pflags & SSH2_FXF_READ)
139 PAPPEND("READ")
140 if (pflags & SSH2_FXF_WRITE)
141 PAPPEND("WRITE")
142 if (pflags & SSH2_FXF_CREAT)
143 PAPPEND("CREATE")
144 if (pflags & SSH2_FXF_TRUNC)
145 PAPPEND("TRUNCATE")
146 if (pflags & SSH2_FXF_EXCL)
147 PAPPEND("EXCL")
148
149 return ret;
150}
151
Ben Lindstrombba81212001-06-25 05:01:22 +0000152static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100153get_attrib(void)
154{
155 return decode_attrib(&iqueue);
156}
157
158/* handle handles */
159
160typedef struct Handle Handle;
161struct Handle {
162 int use;
163 DIR *dirp;
164 int fd;
165 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000166 u_int64_t bytes_read, bytes_write;
Damien Miller7b28dc52000-09-05 13:34:53 +1100167};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000168
Damien Miller7b28dc52000-09-05 13:34:53 +1100169enum {
170 HANDLE_UNUSED,
171 HANDLE_DIR,
172 HANDLE_FILE
173};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000174
Damien Miller7b28dc52000-09-05 13:34:53 +1100175Handle handles[100];
176
Ben Lindstrombba81212001-06-25 05:01:22 +0000177static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100178handle_init(void)
179{
Damien Millereccb9de2005-06-17 12:59:34 +1000180 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000181
Damien Miller9f0f5c62001-12-21 14:45:46 +1100182 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
Damien Miller7b28dc52000-09-05 13:34:53 +1100183 handles[i].use = HANDLE_UNUSED;
184}
185
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100187handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100188{
Damien Millereccb9de2005-06-17 12:59:34 +1000189 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000190
Damien Miller9f0f5c62001-12-21 14:45:46 +1100191 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100192 if (handles[i].use == HANDLE_UNUSED) {
193 handles[i].use = use;
194 handles[i].dirp = dirp;
195 handles[i].fd = fd;
Damien Miller00111382003-03-10 11:21:17 +1100196 handles[i].name = xstrdup(name);
Damien Millerfef95ad2006-07-10 20:46:55 +1000197 handles[i].bytes_read = handles[i].bytes_write = 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100198 return i;
199 }
200 }
201 return -1;
202}
203
Ben Lindstrombba81212001-06-25 05:01:22 +0000204static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100205handle_is_ok(int i, int type)
206{
Damien Millereccb9de2005-06-17 12:59:34 +1000207 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000208 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100209}
210
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100212handle_to_string(int handle, char **stringp, int *hlenp)
213{
Damien Miller7b28dc52000-09-05 13:34:53 +1100214 if (stringp == NULL || hlenp == NULL)
215 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000216 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100217 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000218 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100219 return 0;
220}
221
Ben Lindstrombba81212001-06-25 05:01:22 +0000222static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100223handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100224{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000225 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000226
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000227 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100228 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100229 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100230 if (handle_is_ok(val, HANDLE_FILE) ||
231 handle_is_ok(val, HANDLE_DIR))
232 return val;
233 return -1;
234}
235
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100237handle_to_name(int handle)
238{
239 if (handle_is_ok(handle, HANDLE_DIR)||
240 handle_is_ok(handle, HANDLE_FILE))
241 return handles[handle].name;
242 return NULL;
243}
244
Ben Lindstrombba81212001-06-25 05:01:22 +0000245static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100246handle_to_dir(int handle)
247{
248 if (handle_is_ok(handle, HANDLE_DIR))
249 return handles[handle].dirp;
250 return NULL;
251}
252
Ben Lindstrombba81212001-06-25 05:01:22 +0000253static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100254handle_to_fd(int handle)
255{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000256 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100257 return handles[handle].fd;
258 return -1;
259}
260
Damien Millerfef95ad2006-07-10 20:46:55 +1000261static void
262handle_update_read(int handle, ssize_t bytes)
263{
264 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
265 handles[handle].bytes_read += bytes;
266}
267
268static void
269handle_update_write(int handle, ssize_t bytes)
270{
271 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
272 handles[handle].bytes_write += bytes;
273}
274
275static u_int64_t
276handle_bytes_read(int handle)
277{
278 if (handle_is_ok(handle, HANDLE_FILE))
279 return (handles[handle].bytes_read);
280 return 0;
281}
282
283static u_int64_t
284handle_bytes_write(int handle)
285{
286 if (handle_is_ok(handle, HANDLE_FILE))
287 return (handles[handle].bytes_write);
288 return 0;
289}
290
Ben Lindstrombba81212001-06-25 05:01:22 +0000291static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100292handle_close(int handle)
293{
294 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000295
Damien Miller7b28dc52000-09-05 13:34:53 +1100296 if (handle_is_ok(handle, HANDLE_FILE)) {
297 ret = close(handles[handle].fd);
298 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100299 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100300 } else if (handle_is_ok(handle, HANDLE_DIR)) {
301 ret = closedir(handles[handle].dirp);
302 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100303 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100304 } else {
305 errno = ENOENT;
306 }
307 return ret;
308}
309
Damien Millerfef95ad2006-07-10 20:46:55 +1000310static void
311handle_log_close(int handle, char *emsg)
312{
313 if (handle_is_ok(handle, HANDLE_FILE)) {
314 logit("%s%sclose \"%s\" bytes read %llu written %llu",
315 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
316 handle_to_name(handle),
317 handle_bytes_read(handle), handle_bytes_write(handle));
318 } else {
319 logit("%s%sclosedir \"%s\"",
320 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
321 handle_to_name(handle));
322 }
323}
324
325static void
326handle_log_exit(void)
327{
328 u_int i;
329
330 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
331 if (handles[i].use != HANDLE_UNUSED)
332 handle_log_close(i, "forced");
333}
334
Ben Lindstrombba81212001-06-25 05:01:22 +0000335static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100336get_handle(void)
337{
338 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000339 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100340 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000341
Damien Miller7b28dc52000-09-05 13:34:53 +1100342 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000343 if (hlen < 256)
344 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100345 xfree(handle);
346 return val;
347}
348
349/* send replies */
350
Ben Lindstrombba81212001-06-25 05:01:22 +0000351static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100352send_msg(Buffer *m)
353{
354 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000355
Damien Miller7b28dc52000-09-05 13:34:53 +1100356 buffer_put_int(&oqueue, mlen);
357 buffer_append(&oqueue, buffer_ptr(m), mlen);
358 buffer_consume(m, mlen);
359}
360
Damien Millerfef95ad2006-07-10 20:46:55 +1000361static const char *
362status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100363{
Damien Miller058316f2001-03-08 10:08:49 +1100364 const char *status_messages[] = {
365 "Success", /* SSH_FX_OK */
366 "End of file", /* SSH_FX_EOF */
367 "No such file", /* SSH_FX_NO_SUCH_FILE */
368 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
369 "Failure", /* SSH_FX_FAILURE */
370 "Bad message", /* SSH_FX_BAD_MESSAGE */
371 "No connection", /* SSH_FX_NO_CONNECTION */
372 "Connection lost", /* SSH_FX_CONNECTION_LOST */
373 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
374 "Unknown error" /* Others */
375 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000376 return (status_messages[MIN(status,SSH2_FX_MAX)]);
377}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000378
Damien Millerfef95ad2006-07-10 20:46:55 +1000379static void
380send_status(u_int32_t id, u_int32_t status)
381{
382 Buffer msg;
383
384 debug3("request %u: sent status %u", id, status);
385 if (log_level > SYSLOG_LEVEL_VERBOSE ||
386 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
387 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100388 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000389 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100390 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000391 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100392 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000393 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100394 buffer_put_cstring(&msg, "");
395 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100396 send_msg(&msg);
397 buffer_free(&msg);
398}
Ben Lindstrombba81212001-06-25 05:01:22 +0000399static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100400send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100401{
402 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000403
Damien Miller7b28dc52000-09-05 13:34:53 +1100404 buffer_init(&msg);
405 buffer_put_char(&msg, type);
406 buffer_put_int(&msg, id);
407 buffer_put_string(&msg, data, dlen);
408 send_msg(&msg);
409 buffer_free(&msg);
410}
411
Ben Lindstrombba81212001-06-25 05:01:22 +0000412static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100413send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100414{
Damien Millerfef95ad2006-07-10 20:46:55 +1000415 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000416 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100417}
418
Ben Lindstrombba81212001-06-25 05:01:22 +0000419static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100420send_handle(u_int32_t id, int handle)
421{
422 char *string;
423 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000424
Damien Miller7b28dc52000-09-05 13:34:53 +1100425 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000426 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000427 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100428 xfree(string);
429}
430
Ben Lindstrombba81212001-06-25 05:01:22 +0000431static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100432send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100433{
434 Buffer msg;
435 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000436
Damien Miller7b28dc52000-09-05 13:34:53 +1100437 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000438 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100439 buffer_put_int(&msg, id);
440 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000441 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100442 for (i = 0; i < count; i++) {
443 buffer_put_cstring(&msg, stats[i].name);
444 buffer_put_cstring(&msg, stats[i].long_name);
445 encode_attrib(&msg, &stats[i].attrib);
446 }
447 send_msg(&msg);
448 buffer_free(&msg);
449}
450
Ben Lindstrombba81212001-06-25 05:01:22 +0000451static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100452send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100453{
454 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000455
Damien Millerfef95ad2006-07-10 20:46:55 +1000456 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100457 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000458 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100459 buffer_put_int(&msg, id);
460 encode_attrib(&msg, a);
461 send_msg(&msg);
462 buffer_free(&msg);
463}
464
465/* parse incoming */
466
Ben Lindstrombba81212001-06-25 05:01:22 +0000467static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100468process_init(void)
469{
470 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100471
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000472 version = get_int();
Damien Millerfef95ad2006-07-10 20:46:55 +1000473 verbose("received client version %d", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100474 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000475 buffer_put_char(&msg, SSH2_FXP_VERSION);
476 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100477 send_msg(&msg);
478 buffer_free(&msg);
479}
480
Ben Lindstrombba81212001-06-25 05:01:22 +0000481static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100482process_open(void)
483{
484 u_int32_t id, pflags;
485 Attrib *a;
486 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000487 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100488
489 id = get_int();
490 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000491 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000492 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100493 a = get_attrib();
494 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000495 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000496 logit("open \"%s\" flags %s mode 0%o",
497 name, string_from_portable(pflags), mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100498 fd = open(name, flags, mode);
499 if (fd < 0) {
500 status = errno_to_portable(errno);
501 } else {
Damien Miller00111382003-03-10 11:21:17 +1100502 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100503 if (handle < 0) {
504 close(fd);
505 } else {
506 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000507 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100508 }
509 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000510 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100511 send_status(id, status);
512 xfree(name);
513}
514
Ben Lindstrombba81212001-06-25 05:01:22 +0000515static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100516process_close(void)
517{
518 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000519 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100520
521 id = get_int();
522 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000523 debug3("request %u: close handle %u", id, handle);
524 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100525 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000526 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100527 send_status(id, status);
528}
529
Ben Lindstrombba81212001-06-25 05:01:22 +0000530static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100531process_read(void)
532{
533 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000534 u_int32_t id, len;
535 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100536 u_int64_t off;
537
538 id = get_int();
539 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000540 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100541 len = get_int();
542
Damien Millerfef95ad2006-07-10 20:46:55 +1000543 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
544 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100545 if (len > sizeof buf) {
546 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000547 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100548 }
549 fd = handle_to_fd(handle);
550 if (fd >= 0) {
551 if (lseek(fd, off, SEEK_SET) < 0) {
552 error("process_read: seek failed");
553 status = errno_to_portable(errno);
554 } else {
555 ret = read(fd, buf, len);
556 if (ret < 0) {
557 status = errno_to_portable(errno);
558 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000559 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100560 } else {
561 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000562 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000563 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100564 }
565 }
566 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000567 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100568 send_status(id, status);
569}
570
Ben Lindstrombba81212001-06-25 05:01:22 +0000571static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100572process_write(void)
573{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000574 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100575 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100576 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000577 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100578 char *data;
579
580 id = get_int();
581 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000582 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100583 data = get_string(&len);
584
Damien Millerfef95ad2006-07-10 20:46:55 +1000585 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
586 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100587 fd = handle_to_fd(handle);
588 if (fd >= 0) {
589 if (lseek(fd, off, SEEK_SET) < 0) {
590 status = errno_to_portable(errno);
591 error("process_write: seek failed");
592 } else {
593/* XXX ATOMICIO ? */
594 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000595 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100596 error("process_write: write failed");
597 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000598 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000599 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000600 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100601 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000602 debug2("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100603 }
604 }
605 }
606 send_status(id, status);
607 xfree(data);
608}
609
Ben Lindstrombba81212001-06-25 05:01:22 +0000610static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100611process_do_stat(int do_lstat)
612{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000613 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100614 struct stat st;
615 u_int32_t id;
616 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000617 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100618
619 id = get_int();
620 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000621 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
622 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100623 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
624 if (ret < 0) {
625 status = errno_to_portable(errno);
626 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000627 stat_to_attrib(&st, &a);
628 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000629 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100630 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000631 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100632 send_status(id, status);
633 xfree(name);
634}
635
Ben Lindstrombba81212001-06-25 05:01:22 +0000636static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100637process_stat(void)
638{
639 process_do_stat(0);
640}
641
Ben Lindstrombba81212001-06-25 05:01:22 +0000642static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100643process_lstat(void)
644{
645 process_do_stat(1);
646}
647
Ben Lindstrombba81212001-06-25 05:01:22 +0000648static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100649process_fstat(void)
650{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000651 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100652 struct stat st;
653 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000654 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100655
656 id = get_int();
657 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000658 debug("request %u: fstat \"%s\" (handle %u)",
659 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100660 fd = handle_to_fd(handle);
661 if (fd >= 0) {
662 ret = fstat(fd, &st);
663 if (ret < 0) {
664 status = errno_to_portable(errno);
665 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000666 stat_to_attrib(&st, &a);
667 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000668 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100669 }
670 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000671 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100672 send_status(id, status);
673}
674
Ben Lindstrombba81212001-06-25 05:01:22 +0000675static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100676attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100677{
678 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000679
Damien Miller7b28dc52000-09-05 13:34:53 +1100680 tv[0].tv_sec = a->atime;
681 tv[0].tv_usec = 0;
682 tv[1].tv_sec = a->mtime;
683 tv[1].tv_usec = 0;
684 return tv;
685}
686
Ben Lindstrombba81212001-06-25 05:01:22 +0000687static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100688process_setstat(void)
689{
690 Attrib *a;
691 u_int32_t id;
692 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000693 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100694
695 id = get_int();
696 name = get_string(NULL);
697 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000698 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100699 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000700 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100701 ret = truncate(name, a->size);
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_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000706 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller7b28dc52000-09-05 13:34:53 +1100707 ret = chmod(name, a->perm & 0777);
708 if (ret == -1)
709 status = errno_to_portable(errno);
710 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000711 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000712 char buf[64];
713 time_t t = a->mtime;
714
715 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
716 localtime(&t));
717 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100718 ret = utimes(name, attrib_to_tv(a));
719 if (ret == -1)
720 status = errno_to_portable(errno);
721 }
Kevin Steves8e743932001-02-05 13:24:35 +0000722 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000723 logit("set \"%s\" owner %lu group %lu", name,
724 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000725 ret = chown(name, a->uid, a->gid);
726 if (ret == -1)
727 status = errno_to_portable(errno);
728 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100729 send_status(id, status);
730 xfree(name);
731}
732
Ben Lindstrombba81212001-06-25 05:01:22 +0000733static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100734process_fsetstat(void)
735{
736 Attrib *a;
737 u_int32_t id;
738 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000739 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000740
Damien Miller7b28dc52000-09-05 13:34:53 +1100741 id = get_int();
742 handle = get_handle();
743 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000744 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100745 fd = handle_to_fd(handle);
Damien Millerfef95ad2006-07-10 20:46:55 +1000746 if (fd < 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000747 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100748 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000749 char *name = handle_to_name(handle);
750
Damien Miller00c92172002-02-13 14:05:00 +1100751 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000752 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100753 ret = ftruncate(fd, a->size);
754 if (ret == -1)
755 status = errno_to_portable(errno);
756 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000757 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000758 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000759#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100760 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000761#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000762 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000763#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100764 if (ret == -1)
765 status = errno_to_portable(errno);
766 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000767 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000768 char buf[64];
769 time_t t = a->mtime;
770
771 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
772 localtime(&t));
773 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100774#ifdef HAVE_FUTIMES
775 ret = futimes(fd, attrib_to_tv(a));
776#else
777 ret = utimes(name, attrib_to_tv(a));
778#endif
779 if (ret == -1)
780 status = errno_to_portable(errno);
781 }
Kevin Steves8e743932001-02-05 13:24:35 +0000782 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000783 logit("set \"%s\" owner %lu group %lu", name,
784 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000785#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000786 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000787#else
788 ret = chown(name, a->uid, a->gid);
789#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000790 if (ret == -1)
791 status = errno_to_portable(errno);
792 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100793 }
794 send_status(id, status);
795}
796
Ben Lindstrombba81212001-06-25 05:01:22 +0000797static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100798process_opendir(void)
799{
800 DIR *dirp = NULL;
801 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000802 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100803 u_int32_t id;
804
805 id = get_int();
806 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000807 debug3("request %u: opendir", id);
808 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000809 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100810 if (dirp == NULL) {
811 status = errno_to_portable(errno);
812 } else {
Damien Miller00111382003-03-10 11:21:17 +1100813 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100814 if (handle < 0) {
815 closedir(dirp);
816 } else {
817 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000818 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100819 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000820
Damien Miller7b28dc52000-09-05 13:34:53 +1100821 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000822 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100823 send_status(id, status);
824 xfree(path);
825}
826
Ben Lindstrombba81212001-06-25 05:01:22 +0000827static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100828process_readdir(void)
829{
830 DIR *dirp;
831 struct dirent *dp;
832 char *path;
833 int handle;
834 u_int32_t id;
835
836 id = get_int();
837 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000838 debug("request %u: readdir \"%s\" (handle %d)", id,
839 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100840 dirp = handle_to_dir(handle);
841 path = handle_to_name(handle);
842 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000843 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100844 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100845 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +1000846 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +1100847 Stat *stats;
848 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000849
Damien Miller07d86be2006-03-26 14:19:21 +1100850 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100851 while ((dp = readdir(dirp)) != NULL) {
852 if (count >= nstats) {
853 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +1100854 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100855 }
856/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000857 snprintf(pathname, sizeof pathname, "%s%s%s", path,
858 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100859 if (lstat(pathname, &st) < 0)
860 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000861 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100862 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000863 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100864 count++;
865 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000866 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100867 if (count == 100)
868 break;
869 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000870 if (count > 0) {
871 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100872 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000873 xfree(stats[i].name);
874 xfree(stats[i].long_name);
875 }
876 } else {
877 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100878 }
879 xfree(stats);
880 }
881}
882
Ben Lindstrombba81212001-06-25 05:01:22 +0000883static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100884process_remove(void)
885{
886 char *name;
887 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000888 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100889 int ret;
890
891 id = get_int();
892 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000893 debug3("request %u: remove", id);
894 logit("remove name \"%s\"", name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000895 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000896 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100897 send_status(id, status);
898 xfree(name);
899}
900
Ben Lindstrombba81212001-06-25 05:01:22 +0000901static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100902process_mkdir(void)
903{
904 Attrib *a;
905 u_int32_t id;
906 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000907 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100908
909 id = get_int();
910 name = get_string(NULL);
911 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000912 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
913 a->perm & 0777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +1000914 debug3("request %u: mkdir", id);
915 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100916 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000917 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100918 send_status(id, status);
919 xfree(name);
920}
921
Ben Lindstrombba81212001-06-25 05:01:22 +0000922static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100923process_rmdir(void)
924{
925 u_int32_t id;
926 char *name;
927 int ret, status;
928
929 id = get_int();
930 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000931 debug3("request %u: rmdir", id);
932 logit("rmdir name \"%s\"", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100933 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000934 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100935 send_status(id, status);
936 xfree(name);
937}
938
Ben Lindstrombba81212001-06-25 05:01:22 +0000939static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100940process_realpath(void)
941{
942 char resolvedname[MAXPATHLEN];
943 u_int32_t id;
944 char *path;
945
946 id = get_int();
947 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000948 if (path[0] == '\0') {
949 xfree(path);
950 path = xstrdup(".");
951 }
Damien Millerfef95ad2006-07-10 20:46:55 +1000952 debug3("request %u: realpath", id);
953 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100954 if (realpath(path, resolvedname) == NULL) {
955 send_status(id, errno_to_portable(errno));
956 } else {
957 Stat s;
958 attrib_clear(&s.attrib);
959 s.name = s.long_name = resolvedname;
960 send_names(id, 1, &s);
961 }
962 xfree(path);
963}
964
Ben Lindstrombba81212001-06-25 05:01:22 +0000965static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100966process_rename(void)
967{
968 u_int32_t id;
969 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100970 int status;
Damien Millerb3207e82003-03-26 16:01:11 +1100971 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +1100972
973 id = get_int();
974 oldpath = get_string(NULL);
975 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000976 debug3("request %u: rename", id);
977 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +1100978 status = SSH2_FX_FAILURE;
979 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +1100980 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +1100981 else if (S_ISREG(sb.st_mode)) {
982 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000983 if (link(oldpath, newpath) == -1) {
Darren Tuckere59b5082004-06-28 16:01:19 +1000984 if (errno == EOPNOTSUPP
985#ifdef LINK_OPNOTSUPP_ERRNO
986 || errno == LINK_OPNOTSUPP_ERRNO
987#endif
988 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000989 struct stat st;
990
991 /*
992 * fs doesn't support links, so fall back to
993 * stat+rename. This is racy.
994 */
995 if (stat(newpath, &st) == -1) {
996 if (rename(oldpath, newpath) == -1)
997 status =
998 errno_to_portable(errno);
999 else
1000 status = SSH2_FX_OK;
1001 }
1002 } else {
1003 status = errno_to_portable(errno);
1004 }
1005 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001006 status = errno_to_portable(errno);
1007 /* clean spare link */
1008 unlink(newpath);
1009 } else
1010 status = SSH2_FX_OK;
1011 } else if (stat(newpath, &sb) == -1) {
1012 if (rename(oldpath, newpath) == -1)
1013 status = errno_to_portable(errno);
1014 else
1015 status = SSH2_FX_OK;
1016 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001017 send_status(id, status);
1018 xfree(oldpath);
1019 xfree(newpath);
1020}
1021
Ben Lindstrombba81212001-06-25 05:01:22 +00001022static void
Damien Miller058316f2001-03-08 10:08:49 +11001023process_readlink(void)
1024{
1025 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001026 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001027 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001028 char *path;
1029
1030 id = get_int();
1031 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001032 debug3("request %u: readlink", id);
1033 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001034 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001035 send_status(id, errno_to_portable(errno));
1036 else {
1037 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001038
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001039 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001040 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001041 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001042 send_names(id, 1, &s);
1043 }
1044 xfree(path);
1045}
1046
Ben Lindstrombba81212001-06-25 05:01:22 +00001047static void
Damien Miller058316f2001-03-08 10:08:49 +11001048process_symlink(void)
1049{
1050 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +11001051 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001052 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001053
1054 id = get_int();
1055 oldpath = get_string(NULL);
1056 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001057 debug3("request %u: symlink", id);
1058 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001059 /* this will fail if 'newpath' exists */
1060 ret = symlink(oldpath, newpath);
1061 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001062 send_status(id, status);
1063 xfree(oldpath);
1064 xfree(newpath);
1065}
1066
Ben Lindstrombba81212001-06-25 05:01:22 +00001067static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001068process_extended(void)
1069{
1070 u_int32_t id;
1071 char *request;
1072
1073 id = get_int();
1074 request = get_string(NULL);
1075 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1076 xfree(request);
1077}
Damien Miller7b28dc52000-09-05 13:34:53 +11001078
1079/* stolen from ssh-agent */
1080
Ben Lindstrombba81212001-06-25 05:01:22 +00001081static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001082process(void)
1083{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001084 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +00001085 u_int buf_len;
1086 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001087 u_int type;
1088 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +11001089
Ben Lindstrom2c140472002-06-06 21:57:54 +00001090 buf_len = buffer_len(&iqueue);
1091 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001092 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001093 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001094 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001095 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001096 error("bad message from %s local user %s",
1097 client_addr, pw->pw_name);
1098 cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001099 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001100 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001101 return;
1102 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001103 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001104 type = buffer_get_char(&iqueue);
1105 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001106 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001107 process_init();
1108 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001109 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +11001110 process_open();
1111 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001112 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001113 process_close();
1114 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001115 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +11001116 process_read();
1117 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001118 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001119 process_write();
1120 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001121 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001122 process_lstat();
1123 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001124 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001125 process_fstat();
1126 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001127 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001128 process_setstat();
1129 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001130 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001131 process_fsetstat();
1132 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001133 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001134 process_opendir();
1135 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001136 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001137 process_readdir();
1138 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001139 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001140 process_remove();
1141 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001142 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001143 process_mkdir();
1144 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001145 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001146 process_rmdir();
1147 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001148 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001149 process_realpath();
1150 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001151 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001152 process_stat();
1153 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001154 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001155 process_rename();
1156 break;
Damien Miller058316f2001-03-08 10:08:49 +11001157 case SSH2_FXP_READLINK:
1158 process_readlink();
1159 break;
1160 case SSH2_FXP_SYMLINK:
1161 process_symlink();
1162 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001163 case SSH2_FXP_EXTENDED:
1164 process_extended();
1165 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001166 default:
1167 error("Unknown message %d", type);
1168 break;
1169 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001170 /* discard the remaining bytes from the current packet */
1171 if (buf_len < buffer_len(&iqueue))
Damien Millerfef95ad2006-07-10 20:46:55 +10001172 fatal("iqueue grew unexpectedly");
Ben Lindstrom2c140472002-06-06 21:57:54 +00001173 consumed = buf_len - buffer_len(&iqueue);
1174 if (msg_len < consumed)
1175 fatal("msg_len %d < consumed %d", msg_len, consumed);
1176 if (msg_len > consumed)
1177 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001178}
1179
Damien Millerfef95ad2006-07-10 20:46:55 +10001180/* Cleanup handler that logs active handles upon normal exit */
1181void
1182cleanup_exit(int i)
1183{
1184 if (pw != NULL && client_addr != NULL) {
1185 handle_log_exit();
1186 logit("session closed for local user %s from [%s]",
1187 pw->pw_name, client_addr);
1188 }
1189 _exit(i);
1190}
1191
1192static void
1193usage(void)
1194{
1195 extern char *__progname;
1196
1197 fprintf(stderr,
1198 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1199 exit(1);
1200}
1201
Damien Miller7b28dc52000-09-05 13:34:53 +11001202int
Damien Millerfef95ad2006-07-10 20:46:55 +10001203main(int argc, char **argv)
Damien Miller7b28dc52000-09-05 13:34:53 +11001204{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001205 fd_set *rset, *wset;
Damien Millerfef95ad2006-07-10 20:46:55 +10001206 int in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001207 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001208 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1209 char *cp;
1210
Damien Millerfef95ad2006-07-10 20:46:55 +10001211 extern char *optarg;
1212 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001213
Darren Tuckerce321d82005-10-03 18:11:24 +10001214 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1215 sanitise_stdfd();
1216
Damien Millerfef95ad2006-07-10 20:46:55 +10001217 __progname = ssh_get_progname(argv[0]);
1218 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001219
Damien Millerfef95ad2006-07-10 20:46:55 +10001220 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1221 switch (ch) {
1222 case 'c':
1223 /*
1224 * Ignore all arguments if we are invoked as a
1225 * shell using "sftp-server -c command"
1226 */
1227 skipargs = 1;
1228 break;
1229 case 'e':
1230 log_stderr = 1;
1231 break;
1232 case 'l':
1233 log_level = log_level_number(optarg);
1234 if (log_level == SYSLOG_LEVEL_NOT_SET)
1235 error("Invalid log level \"%s\"", optarg);
1236 break;
1237 case 'f':
1238 log_facility = log_facility_number(optarg);
1239 if (log_level == SYSLOG_FACILITY_NOT_SET)
1240 error("Invalid log facility \"%s\"", optarg);
1241 break;
1242 case 'h':
1243 default:
1244 usage();
1245 }
1246 }
1247
1248 log_init(__progname, log_level, log_facility, log_stderr);
1249
1250 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1251 client_addr = xstrdup(cp);
1252 if ((cp = strchr(client_addr, ' ')) == NULL)
1253 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1254 getenv("SSH_CONNECTION"));
1255 *cp = '\0';
1256 } else
1257 client_addr = xstrdup("UNKNOWN");
1258
1259 if ((pw = getpwuid(getuid())) == NULL)
1260 fatal("No user found for uid %lu", (u_long)getuid());
1261 pw = pwcopy(pw);
1262
1263 logit("session opened for local user %s from [%s]",
1264 pw->pw_name, client_addr);
1265
Damien Miller7b28dc52000-09-05 13:34:53 +11001266 handle_init();
1267
Damien Miller7b28dc52000-09-05 13:34:53 +11001268 in = dup(STDIN_FILENO);
1269 out = dup(STDOUT_FILENO);
1270
Damien Miller402b3312001-04-14 00:28:42 +10001271#ifdef HAVE_CYGWIN
1272 setmode(in, O_BINARY);
1273 setmode(out, O_BINARY);
1274#endif
1275
Damien Miller7b28dc52000-09-05 13:34:53 +11001276 max = 0;
1277 if (in > max)
1278 max = in;
1279 if (out > max)
1280 max = out;
1281
1282 buffer_init(&iqueue);
1283 buffer_init(&oqueue);
1284
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001285 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1286 rset = (fd_set *)xmalloc(set_size);
1287 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001288
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001289 for (;;) {
1290 memset(rset, 0, set_size);
1291 memset(wset, 0, set_size);
1292
1293 FD_SET(in, rset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001294 olen = buffer_len(&oqueue);
1295 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001296 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001297
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001298 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001299 if (errno == EINTR)
1300 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001301 error("select: %s", strerror(errno));
1302 cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001303 }
1304
1305 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001306 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001307 char buf[4*4096];
1308 len = read(in, buf, sizeof buf);
1309 if (len == 0) {
1310 debug("read eof");
Damien Millerfef95ad2006-07-10 20:46:55 +10001311 cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001312 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001313 error("read: %s", strerror(errno));
1314 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001315 } else {
1316 buffer_append(&iqueue, buf, len);
1317 }
1318 }
1319 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001320 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001321 len = write(out, buffer_ptr(&oqueue), olen);
1322 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001323 error("write: %s", strerror(errno));
1324 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001325 } else {
1326 buffer_consume(&oqueue, len);
1327 }
1328 }
1329 /* process requests from client */
1330 process();
1331 }
1332}