blob: c57958b0f327fe4cf955ac90efe917b8d36434de [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: sftp-server.c,v 1.70 2006/08/03 03:34:42 deraadt 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 */
Damien Millerd7834352006-08-05 12:39:39 +100017
Damien Miller7b28dc52000-09-05 13:34:53 +110018#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110019
20#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100021#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110022#include <sys/stat.h>
Damien Miller9aec9192006-08-05 10:57:45 +100023#ifdef HAVE_SYS_TIME_H
24# include <sys/time.h>
25#endif
Damien Miller88f254b2006-03-15 11:25:13 +110026
27#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100028#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100029#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100030#include <pwd.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100031#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100032#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100033#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#include <pwd.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100035#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <stdarg.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110038
Damien Miller7b28dc52000-09-05 13:34:53 +110039#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100040#include "buffer.h"
41#include "log.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100042#include "misc.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100043#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110044
Ben Lindstrom2f959b42001-01-11 06:20:23 +000045#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110046#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110047
48/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000049#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110050#define get_int() buffer_get_int(&iqueue);
51#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Damien Miller7b28dc52000-09-05 13:34:53 +110052
Damien Millerfef95ad2006-07-10 20:46:55 +100053/* Our verbosity */
54LogLevel log_level = SYSLOG_LEVEL_ERROR;
55
56/* Our client */
57struct passwd *pw = NULL;
58char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000059
Damien Miller7b28dc52000-09-05 13:34:53 +110060/* input and output queue */
61Buffer iqueue;
62Buffer oqueue;
63
Damien Miller058316f2001-03-08 10:08:49 +110064/* Version of client */
65int version;
66
Darren Tuckera6612d42003-06-28 12:39:03 +100067/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110068
Damien Miller7b28dc52000-09-05 13:34:53 +110069typedef struct Stat Stat;
70
Damien Miller33804262001-02-04 23:20:18 +110071struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110072 char *name;
73 char *long_name;
74 Attrib attrib;
75};
76
Ben Lindstrombba81212001-06-25 05:01:22 +000077static int
Damien Miller7b28dc52000-09-05 13:34:53 +110078errno_to_portable(int unixerrno)
79{
80 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000081
Damien Miller7b28dc52000-09-05 13:34:53 +110082 switch (unixerrno) {
83 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000084 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110085 break;
86 case ENOENT:
87 case ENOTDIR:
88 case EBADF:
89 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000090 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110091 break;
92 case EPERM:
93 case EACCES:
94 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000095 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110096 break;
97 case ENAMETOOLONG:
98 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000099 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100100 break;
101 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000102 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100103 break;
104 }
105 return ret;
106}
107
Ben Lindstrombba81212001-06-25 05:01:22 +0000108static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100109flags_from_portable(int pflags)
110{
111 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000112
Ben Lindstrom36592512001-03-05 05:02:08 +0000113 if ((pflags & SSH2_FXF_READ) &&
114 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100115 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000116 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100117 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000118 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100119 flags = O_WRONLY;
120 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000121 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100122 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000123 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100124 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000125 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100126 flags |= O_EXCL;
127 return flags;
128}
129
Damien Millerfef95ad2006-07-10 20:46:55 +1000130static const char *
131string_from_portable(int pflags)
132{
133 static char ret[128];
134
135 *ret = '\0';
136
137#define PAPPEND(str) { \
138 if (*ret != '\0') \
139 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000140 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000141 }
142
143 if (pflags & SSH2_FXF_READ)
144 PAPPEND("READ")
145 if (pflags & SSH2_FXF_WRITE)
146 PAPPEND("WRITE")
147 if (pflags & SSH2_FXF_CREAT)
148 PAPPEND("CREATE")
149 if (pflags & SSH2_FXF_TRUNC)
150 PAPPEND("TRUNCATE")
151 if (pflags & SSH2_FXF_EXCL)
152 PAPPEND("EXCL")
153
154 return ret;
155}
156
Ben Lindstrombba81212001-06-25 05:01:22 +0000157static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100158get_attrib(void)
159{
160 return decode_attrib(&iqueue);
161}
162
163/* handle handles */
164
165typedef struct Handle Handle;
166struct Handle {
167 int use;
168 DIR *dirp;
169 int fd;
170 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000171 u_int64_t bytes_read, bytes_write;
Damien Miller7b28dc52000-09-05 13:34:53 +1100172};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000173
Damien Miller7b28dc52000-09-05 13:34:53 +1100174enum {
175 HANDLE_UNUSED,
176 HANDLE_DIR,
177 HANDLE_FILE
178};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000179
Damien Miller7b28dc52000-09-05 13:34:53 +1100180Handle handles[100];
181
Ben Lindstrombba81212001-06-25 05:01:22 +0000182static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100183handle_init(void)
184{
Damien Millereccb9de2005-06-17 12:59:34 +1000185 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000186
Damien Miller9f0f5c62001-12-21 14:45:46 +1100187 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
Damien Miller7b28dc52000-09-05 13:34:53 +1100188 handles[i].use = HANDLE_UNUSED;
189}
190
Ben Lindstrombba81212001-06-25 05:01:22 +0000191static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100192handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100193{
Damien Millereccb9de2005-06-17 12:59:34 +1000194 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000195
Damien Miller9f0f5c62001-12-21 14:45:46 +1100196 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100197 if (handles[i].use == HANDLE_UNUSED) {
198 handles[i].use = use;
199 handles[i].dirp = dirp;
200 handles[i].fd = fd;
Damien Miller00111382003-03-10 11:21:17 +1100201 handles[i].name = xstrdup(name);
Damien Millerfef95ad2006-07-10 20:46:55 +1000202 handles[i].bytes_read = handles[i].bytes_write = 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100203 return i;
204 }
205 }
206 return -1;
207}
208
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100210handle_is_ok(int i, int type)
211{
Damien Millereccb9de2005-06-17 12:59:34 +1000212 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000213 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100214}
215
Ben Lindstrombba81212001-06-25 05:01:22 +0000216static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100217handle_to_string(int handle, char **stringp, int *hlenp)
218{
Damien Miller7b28dc52000-09-05 13:34:53 +1100219 if (stringp == NULL || hlenp == NULL)
220 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000221 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100222 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000223 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100224 return 0;
225}
226
Ben Lindstrombba81212001-06-25 05:01:22 +0000227static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100228handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100229{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000230 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000231
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000232 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100233 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100234 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100235 if (handle_is_ok(val, HANDLE_FILE) ||
236 handle_is_ok(val, HANDLE_DIR))
237 return val;
238 return -1;
239}
240
Ben Lindstrombba81212001-06-25 05:01:22 +0000241static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100242handle_to_name(int handle)
243{
244 if (handle_is_ok(handle, HANDLE_DIR)||
245 handle_is_ok(handle, HANDLE_FILE))
246 return handles[handle].name;
247 return NULL;
248}
249
Ben Lindstrombba81212001-06-25 05:01:22 +0000250static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100251handle_to_dir(int handle)
252{
253 if (handle_is_ok(handle, HANDLE_DIR))
254 return handles[handle].dirp;
255 return NULL;
256}
257
Ben Lindstrombba81212001-06-25 05:01:22 +0000258static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100259handle_to_fd(int handle)
260{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000261 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100262 return handles[handle].fd;
263 return -1;
264}
265
Damien Millerfef95ad2006-07-10 20:46:55 +1000266static void
267handle_update_read(int handle, ssize_t bytes)
268{
269 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
270 handles[handle].bytes_read += bytes;
271}
272
273static void
274handle_update_write(int handle, ssize_t bytes)
275{
276 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
277 handles[handle].bytes_write += bytes;
278}
279
280static u_int64_t
281handle_bytes_read(int handle)
282{
283 if (handle_is_ok(handle, HANDLE_FILE))
284 return (handles[handle].bytes_read);
285 return 0;
286}
287
288static u_int64_t
289handle_bytes_write(int handle)
290{
291 if (handle_is_ok(handle, HANDLE_FILE))
292 return (handles[handle].bytes_write);
293 return 0;
294}
295
Ben Lindstrombba81212001-06-25 05:01:22 +0000296static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100297handle_close(int handle)
298{
299 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000300
Damien Miller7b28dc52000-09-05 13:34:53 +1100301 if (handle_is_ok(handle, HANDLE_FILE)) {
302 ret = close(handles[handle].fd);
303 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100304 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100305 } else if (handle_is_ok(handle, HANDLE_DIR)) {
306 ret = closedir(handles[handle].dirp);
307 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100308 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100309 } else {
310 errno = ENOENT;
311 }
312 return ret;
313}
314
Damien Millerfef95ad2006-07-10 20:46:55 +1000315static void
316handle_log_close(int handle, char *emsg)
317{
318 if (handle_is_ok(handle, HANDLE_FILE)) {
319 logit("%s%sclose \"%s\" bytes read %llu written %llu",
320 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
321 handle_to_name(handle),
322 handle_bytes_read(handle), handle_bytes_write(handle));
323 } else {
324 logit("%s%sclosedir \"%s\"",
325 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
326 handle_to_name(handle));
327 }
328}
329
330static void
331handle_log_exit(void)
332{
333 u_int i;
334
335 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
336 if (handles[i].use != HANDLE_UNUSED)
337 handle_log_close(i, "forced");
338}
339
Ben Lindstrombba81212001-06-25 05:01:22 +0000340static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100341get_handle(void)
342{
343 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000344 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100345 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000346
Damien Miller7b28dc52000-09-05 13:34:53 +1100347 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000348 if (hlen < 256)
349 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100350 xfree(handle);
351 return val;
352}
353
354/* send replies */
355
Ben Lindstrombba81212001-06-25 05:01:22 +0000356static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100357send_msg(Buffer *m)
358{
359 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000360
Damien Miller7b28dc52000-09-05 13:34:53 +1100361 buffer_put_int(&oqueue, mlen);
362 buffer_append(&oqueue, buffer_ptr(m), mlen);
363 buffer_consume(m, mlen);
364}
365
Damien Millerfef95ad2006-07-10 20:46:55 +1000366static const char *
367status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100368{
Damien Miller058316f2001-03-08 10:08:49 +1100369 const char *status_messages[] = {
370 "Success", /* SSH_FX_OK */
371 "End of file", /* SSH_FX_EOF */
372 "No such file", /* SSH_FX_NO_SUCH_FILE */
373 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
374 "Failure", /* SSH_FX_FAILURE */
375 "Bad message", /* SSH_FX_BAD_MESSAGE */
376 "No connection", /* SSH_FX_NO_CONNECTION */
377 "Connection lost", /* SSH_FX_CONNECTION_LOST */
378 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
379 "Unknown error" /* Others */
380 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000381 return (status_messages[MIN(status,SSH2_FX_MAX)]);
382}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000383
Damien Millerfef95ad2006-07-10 20:46:55 +1000384static void
385send_status(u_int32_t id, u_int32_t status)
386{
387 Buffer msg;
388
389 debug3("request %u: sent status %u", id, status);
390 if (log_level > SYSLOG_LEVEL_VERBOSE ||
391 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
392 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100393 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000394 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100395 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000396 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100397 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000398 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100399 buffer_put_cstring(&msg, "");
400 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100401 send_msg(&msg);
402 buffer_free(&msg);
403}
Ben Lindstrombba81212001-06-25 05:01:22 +0000404static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100405send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100406{
407 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000408
Damien Miller7b28dc52000-09-05 13:34:53 +1100409 buffer_init(&msg);
410 buffer_put_char(&msg, type);
411 buffer_put_int(&msg, id);
412 buffer_put_string(&msg, data, dlen);
413 send_msg(&msg);
414 buffer_free(&msg);
415}
416
Ben Lindstrombba81212001-06-25 05:01:22 +0000417static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100418send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100419{
Damien Millerfef95ad2006-07-10 20:46:55 +1000420 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000421 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100422}
423
Ben Lindstrombba81212001-06-25 05:01:22 +0000424static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100425send_handle(u_int32_t id, int handle)
426{
427 char *string;
428 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000429
Damien Miller7b28dc52000-09-05 13:34:53 +1100430 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000431 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000432 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100433 xfree(string);
434}
435
Ben Lindstrombba81212001-06-25 05:01:22 +0000436static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100437send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100438{
439 Buffer msg;
440 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000441
Damien Miller7b28dc52000-09-05 13:34:53 +1100442 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000443 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100444 buffer_put_int(&msg, id);
445 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000446 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100447 for (i = 0; i < count; i++) {
448 buffer_put_cstring(&msg, stats[i].name);
449 buffer_put_cstring(&msg, stats[i].long_name);
450 encode_attrib(&msg, &stats[i].attrib);
451 }
452 send_msg(&msg);
453 buffer_free(&msg);
454}
455
Ben Lindstrombba81212001-06-25 05:01:22 +0000456static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100457send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100458{
459 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000460
Damien Millerfef95ad2006-07-10 20:46:55 +1000461 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100462 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000463 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100464 buffer_put_int(&msg, id);
465 encode_attrib(&msg, a);
466 send_msg(&msg);
467 buffer_free(&msg);
468}
469
470/* parse incoming */
471
Ben Lindstrombba81212001-06-25 05:01:22 +0000472static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100473process_init(void)
474{
475 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100476
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000477 version = get_int();
Damien Millerfef95ad2006-07-10 20:46:55 +1000478 verbose("received client version %d", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100479 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000480 buffer_put_char(&msg, SSH2_FXP_VERSION);
481 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100482 send_msg(&msg);
483 buffer_free(&msg);
484}
485
Ben Lindstrombba81212001-06-25 05:01:22 +0000486static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100487process_open(void)
488{
489 u_int32_t id, pflags;
490 Attrib *a;
491 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000492 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100493
494 id = get_int();
495 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000496 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000497 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100498 a = get_attrib();
499 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000500 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000501 logit("open \"%s\" flags %s mode 0%o",
502 name, string_from_portable(pflags), mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100503 fd = open(name, flags, mode);
504 if (fd < 0) {
505 status = errno_to_portable(errno);
506 } else {
Damien Miller00111382003-03-10 11:21:17 +1100507 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100508 if (handle < 0) {
509 close(fd);
510 } else {
511 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000512 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100513 }
514 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000515 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100516 send_status(id, status);
517 xfree(name);
518}
519
Ben Lindstrombba81212001-06-25 05:01:22 +0000520static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100521process_close(void)
522{
523 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000524 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100525
526 id = get_int();
527 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000528 debug3("request %u: close handle %u", id, handle);
529 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100530 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000531 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100532 send_status(id, status);
533}
534
Ben Lindstrombba81212001-06-25 05:01:22 +0000535static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100536process_read(void)
537{
538 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000539 u_int32_t id, len;
540 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100541 u_int64_t off;
542
543 id = get_int();
544 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000545 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100546 len = get_int();
547
Damien Millerfef95ad2006-07-10 20:46:55 +1000548 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
549 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100550 if (len > sizeof buf) {
551 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000552 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100553 }
554 fd = handle_to_fd(handle);
555 if (fd >= 0) {
556 if (lseek(fd, off, SEEK_SET) < 0) {
557 error("process_read: seek failed");
558 status = errno_to_portable(errno);
559 } else {
560 ret = read(fd, buf, len);
561 if (ret < 0) {
562 status = errno_to_portable(errno);
563 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000564 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100565 } else {
566 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000567 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000568 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100569 }
570 }
571 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000572 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100573 send_status(id, status);
574}
575
Ben Lindstrombba81212001-06-25 05:01:22 +0000576static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100577process_write(void)
578{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000579 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100580 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100581 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000582 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100583 char *data;
584
585 id = get_int();
586 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000587 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100588 data = get_string(&len);
589
Damien Millerfef95ad2006-07-10 20:46:55 +1000590 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
591 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100592 fd = handle_to_fd(handle);
593 if (fd >= 0) {
594 if (lseek(fd, off, SEEK_SET) < 0) {
595 status = errno_to_portable(errno);
596 error("process_write: seek failed");
597 } else {
598/* XXX ATOMICIO ? */
599 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000600 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100601 error("process_write: write failed");
602 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000603 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000604 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000605 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100606 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000607 debug2("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100608 }
609 }
610 }
611 send_status(id, status);
612 xfree(data);
613}
614
Ben Lindstrombba81212001-06-25 05:01:22 +0000615static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100616process_do_stat(int do_lstat)
617{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000618 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100619 struct stat st;
620 u_int32_t id;
621 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000622 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100623
624 id = get_int();
625 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000626 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
627 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100628 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
629 if (ret < 0) {
630 status = errno_to_portable(errno);
631 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000632 stat_to_attrib(&st, &a);
633 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000634 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100635 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000636 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100637 send_status(id, status);
638 xfree(name);
639}
640
Ben Lindstrombba81212001-06-25 05:01:22 +0000641static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100642process_stat(void)
643{
644 process_do_stat(0);
645}
646
Ben Lindstrombba81212001-06-25 05:01:22 +0000647static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100648process_lstat(void)
649{
650 process_do_stat(1);
651}
652
Ben Lindstrombba81212001-06-25 05:01:22 +0000653static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100654process_fstat(void)
655{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000656 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100657 struct stat st;
658 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000659 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100660
661 id = get_int();
662 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000663 debug("request %u: fstat \"%s\" (handle %u)",
664 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100665 fd = handle_to_fd(handle);
666 if (fd >= 0) {
667 ret = fstat(fd, &st);
668 if (ret < 0) {
669 status = errno_to_portable(errno);
670 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000671 stat_to_attrib(&st, &a);
672 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000673 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100674 }
675 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000676 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100677 send_status(id, status);
678}
679
Ben Lindstrombba81212001-06-25 05:01:22 +0000680static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100681attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100682{
683 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000684
Damien Miller7b28dc52000-09-05 13:34:53 +1100685 tv[0].tv_sec = a->atime;
686 tv[0].tv_usec = 0;
687 tv[1].tv_sec = a->mtime;
688 tv[1].tv_usec = 0;
689 return tv;
690}
691
Ben Lindstrombba81212001-06-25 05:01:22 +0000692static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100693process_setstat(void)
694{
695 Attrib *a;
696 u_int32_t id;
697 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000698 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100699
700 id = get_int();
701 name = get_string(NULL);
702 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000703 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100704 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000705 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100706 ret = truncate(name, a->size);
707 if (ret == -1)
708 status = errno_to_portable(errno);
709 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000710 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000711 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller7b28dc52000-09-05 13:34:53 +1100712 ret = chmod(name, a->perm & 0777);
713 if (ret == -1)
714 status = errno_to_portable(errno);
715 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000716 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000717 char buf[64];
718 time_t t = a->mtime;
719
720 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
721 localtime(&t));
722 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100723 ret = utimes(name, attrib_to_tv(a));
724 if (ret == -1)
725 status = errno_to_portable(errno);
726 }
Kevin Steves8e743932001-02-05 13:24:35 +0000727 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000728 logit("set \"%s\" owner %lu group %lu", name,
729 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000730 ret = chown(name, a->uid, a->gid);
731 if (ret == -1)
732 status = errno_to_portable(errno);
733 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100734 send_status(id, status);
735 xfree(name);
736}
737
Ben Lindstrombba81212001-06-25 05:01:22 +0000738static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100739process_fsetstat(void)
740{
741 Attrib *a;
742 u_int32_t id;
743 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000744 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000745
Damien Miller7b28dc52000-09-05 13:34:53 +1100746 id = get_int();
747 handle = get_handle();
748 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000749 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100750 fd = handle_to_fd(handle);
Damien Millerfef95ad2006-07-10 20:46:55 +1000751 if (fd < 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000752 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100753 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000754 char *name = handle_to_name(handle);
755
Damien Miller00c92172002-02-13 14:05:00 +1100756 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000757 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100758 ret = ftruncate(fd, a->size);
759 if (ret == -1)
760 status = errno_to_portable(errno);
761 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000762 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000763 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000764#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100765 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000766#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000767 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000768#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100769 if (ret == -1)
770 status = errno_to_portable(errno);
771 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000772 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000773 char buf[64];
774 time_t t = a->mtime;
775
776 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
777 localtime(&t));
778 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100779#ifdef HAVE_FUTIMES
780 ret = futimes(fd, attrib_to_tv(a));
781#else
782 ret = utimes(name, attrib_to_tv(a));
783#endif
784 if (ret == -1)
785 status = errno_to_portable(errno);
786 }
Kevin Steves8e743932001-02-05 13:24:35 +0000787 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000788 logit("set \"%s\" owner %lu group %lu", name,
789 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000790#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000791 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000792#else
793 ret = chown(name, a->uid, a->gid);
794#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000795 if (ret == -1)
796 status = errno_to_portable(errno);
797 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100798 }
799 send_status(id, status);
800}
801
Ben Lindstrombba81212001-06-25 05:01:22 +0000802static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100803process_opendir(void)
804{
805 DIR *dirp = NULL;
806 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000807 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100808 u_int32_t id;
809
810 id = get_int();
811 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000812 debug3("request %u: opendir", id);
813 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000814 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100815 if (dirp == NULL) {
816 status = errno_to_portable(errno);
817 } else {
Damien Miller00111382003-03-10 11:21:17 +1100818 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100819 if (handle < 0) {
820 closedir(dirp);
821 } else {
822 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000823 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100824 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000825
Damien Miller7b28dc52000-09-05 13:34:53 +1100826 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000827 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100828 send_status(id, status);
829 xfree(path);
830}
831
Ben Lindstrombba81212001-06-25 05:01:22 +0000832static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100833process_readdir(void)
834{
835 DIR *dirp;
836 struct dirent *dp;
837 char *path;
838 int handle;
839 u_int32_t id;
840
841 id = get_int();
842 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000843 debug("request %u: readdir \"%s\" (handle %d)", id,
844 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100845 dirp = handle_to_dir(handle);
846 path = handle_to_name(handle);
847 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000848 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100849 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100850 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +1000851 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +1100852 Stat *stats;
853 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000854
Damien Miller07d86be2006-03-26 14:19:21 +1100855 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100856 while ((dp = readdir(dirp)) != NULL) {
857 if (count >= nstats) {
858 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +1100859 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100860 }
861/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000862 snprintf(pathname, sizeof pathname, "%s%s%s", path,
863 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100864 if (lstat(pathname, &st) < 0)
865 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000866 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100867 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000868 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100869 count++;
870 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000871 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100872 if (count == 100)
873 break;
874 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000875 if (count > 0) {
876 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100877 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000878 xfree(stats[i].name);
879 xfree(stats[i].long_name);
880 }
881 } else {
882 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100883 }
884 xfree(stats);
885 }
886}
887
Ben Lindstrombba81212001-06-25 05:01:22 +0000888static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100889process_remove(void)
890{
891 char *name;
892 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000893 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100894 int ret;
895
896 id = get_int();
897 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000898 debug3("request %u: remove", id);
899 logit("remove name \"%s\"", name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000900 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000901 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100902 send_status(id, status);
903 xfree(name);
904}
905
Ben Lindstrombba81212001-06-25 05:01:22 +0000906static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100907process_mkdir(void)
908{
909 Attrib *a;
910 u_int32_t id;
911 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000912 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100913
914 id = get_int();
915 name = get_string(NULL);
916 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000917 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
918 a->perm & 0777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +1000919 debug3("request %u: mkdir", id);
920 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100921 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000922 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100923 send_status(id, status);
924 xfree(name);
925}
926
Ben Lindstrombba81212001-06-25 05:01:22 +0000927static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100928process_rmdir(void)
929{
930 u_int32_t id;
931 char *name;
932 int ret, status;
933
934 id = get_int();
935 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000936 debug3("request %u: rmdir", id);
937 logit("rmdir name \"%s\"", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100938 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000939 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100940 send_status(id, status);
941 xfree(name);
942}
943
Ben Lindstrombba81212001-06-25 05:01:22 +0000944static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100945process_realpath(void)
946{
947 char resolvedname[MAXPATHLEN];
948 u_int32_t id;
949 char *path;
950
951 id = get_int();
952 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000953 if (path[0] == '\0') {
954 xfree(path);
955 path = xstrdup(".");
956 }
Damien Millerfef95ad2006-07-10 20:46:55 +1000957 debug3("request %u: realpath", id);
958 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100959 if (realpath(path, resolvedname) == NULL) {
960 send_status(id, errno_to_portable(errno));
961 } else {
962 Stat s;
963 attrib_clear(&s.attrib);
964 s.name = s.long_name = resolvedname;
965 send_names(id, 1, &s);
966 }
967 xfree(path);
968}
969
Ben Lindstrombba81212001-06-25 05:01:22 +0000970static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100971process_rename(void)
972{
973 u_int32_t id;
974 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100975 int status;
Damien Millerb3207e82003-03-26 16:01:11 +1100976 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +1100977
978 id = get_int();
979 oldpath = get_string(NULL);
980 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000981 debug3("request %u: rename", id);
982 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +1100983 status = SSH2_FX_FAILURE;
984 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +1100985 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +1100986 else if (S_ISREG(sb.st_mode)) {
987 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000988 if (link(oldpath, newpath) == -1) {
Darren Tuckere59b5082004-06-28 16:01:19 +1000989 if (errno == EOPNOTSUPP
990#ifdef LINK_OPNOTSUPP_ERRNO
991 || errno == LINK_OPNOTSUPP_ERRNO
992#endif
993 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000994 struct stat st;
995
996 /*
997 * fs doesn't support links, so fall back to
998 * stat+rename. This is racy.
999 */
1000 if (stat(newpath, &st) == -1) {
1001 if (rename(oldpath, newpath) == -1)
1002 status =
1003 errno_to_portable(errno);
1004 else
1005 status = SSH2_FX_OK;
1006 }
1007 } else {
1008 status = errno_to_portable(errno);
1009 }
1010 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001011 status = errno_to_portable(errno);
1012 /* clean spare link */
1013 unlink(newpath);
1014 } else
1015 status = SSH2_FX_OK;
1016 } else if (stat(newpath, &sb) == -1) {
1017 if (rename(oldpath, newpath) == -1)
1018 status = errno_to_portable(errno);
1019 else
1020 status = SSH2_FX_OK;
1021 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001022 send_status(id, status);
1023 xfree(oldpath);
1024 xfree(newpath);
1025}
1026
Ben Lindstrombba81212001-06-25 05:01:22 +00001027static void
Damien Miller058316f2001-03-08 10:08:49 +11001028process_readlink(void)
1029{
1030 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001031 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001032 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001033 char *path;
1034
1035 id = get_int();
1036 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001037 debug3("request %u: readlink", id);
1038 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001039 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001040 send_status(id, errno_to_portable(errno));
1041 else {
1042 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001043
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001044 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001045 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001046 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001047 send_names(id, 1, &s);
1048 }
1049 xfree(path);
1050}
1051
Ben Lindstrombba81212001-06-25 05:01:22 +00001052static void
Damien Miller058316f2001-03-08 10:08:49 +11001053process_symlink(void)
1054{
1055 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +11001056 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001057 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001058
1059 id = get_int();
1060 oldpath = get_string(NULL);
1061 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001062 debug3("request %u: symlink", id);
1063 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001064 /* this will fail if 'newpath' exists */
1065 ret = symlink(oldpath, newpath);
1066 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001067 send_status(id, status);
1068 xfree(oldpath);
1069 xfree(newpath);
1070}
1071
Ben Lindstrombba81212001-06-25 05:01:22 +00001072static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001073process_extended(void)
1074{
1075 u_int32_t id;
1076 char *request;
1077
1078 id = get_int();
1079 request = get_string(NULL);
1080 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1081 xfree(request);
1082}
Damien Miller7b28dc52000-09-05 13:34:53 +11001083
1084/* stolen from ssh-agent */
1085
Ben Lindstrombba81212001-06-25 05:01:22 +00001086static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001087process(void)
1088{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001089 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +00001090 u_int buf_len;
1091 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001092 u_int type;
1093 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +11001094
Ben Lindstrom2c140472002-06-06 21:57:54 +00001095 buf_len = buffer_len(&iqueue);
1096 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001097 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001098 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001099 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001100 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001101 error("bad message from %s local user %s",
1102 client_addr, pw->pw_name);
1103 cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001104 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001105 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001106 return;
1107 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001108 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001109 type = buffer_get_char(&iqueue);
1110 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001111 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001112 process_init();
1113 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001114 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +11001115 process_open();
1116 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001117 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001118 process_close();
1119 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001120 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +11001121 process_read();
1122 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001123 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001124 process_write();
1125 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001126 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001127 process_lstat();
1128 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001129 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001130 process_fstat();
1131 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001132 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001133 process_setstat();
1134 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001135 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001136 process_fsetstat();
1137 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001138 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001139 process_opendir();
1140 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001141 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001142 process_readdir();
1143 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001144 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001145 process_remove();
1146 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001147 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001148 process_mkdir();
1149 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001150 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001151 process_rmdir();
1152 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001153 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001154 process_realpath();
1155 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001156 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001157 process_stat();
1158 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001159 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001160 process_rename();
1161 break;
Damien Miller058316f2001-03-08 10:08:49 +11001162 case SSH2_FXP_READLINK:
1163 process_readlink();
1164 break;
1165 case SSH2_FXP_SYMLINK:
1166 process_symlink();
1167 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001168 case SSH2_FXP_EXTENDED:
1169 process_extended();
1170 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001171 default:
1172 error("Unknown message %d", type);
1173 break;
1174 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001175 /* discard the remaining bytes from the current packet */
1176 if (buf_len < buffer_len(&iqueue))
Damien Millerfef95ad2006-07-10 20:46:55 +10001177 fatal("iqueue grew unexpectedly");
Ben Lindstrom2c140472002-06-06 21:57:54 +00001178 consumed = buf_len - buffer_len(&iqueue);
1179 if (msg_len < consumed)
1180 fatal("msg_len %d < consumed %d", msg_len, consumed);
1181 if (msg_len > consumed)
1182 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001183}
1184
Damien Millerfef95ad2006-07-10 20:46:55 +10001185/* Cleanup handler that logs active handles upon normal exit */
1186void
1187cleanup_exit(int i)
1188{
1189 if (pw != NULL && client_addr != NULL) {
1190 handle_log_exit();
1191 logit("session closed for local user %s from [%s]",
1192 pw->pw_name, client_addr);
1193 }
1194 _exit(i);
1195}
1196
1197static void
1198usage(void)
1199{
1200 extern char *__progname;
1201
1202 fprintf(stderr,
1203 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1204 exit(1);
1205}
1206
Damien Miller7b28dc52000-09-05 13:34:53 +11001207int
Damien Millerfef95ad2006-07-10 20:46:55 +10001208main(int argc, char **argv)
Damien Miller7b28dc52000-09-05 13:34:53 +11001209{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001210 fd_set *rset, *wset;
Damien Millerfef95ad2006-07-10 20:46:55 +10001211 int in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001212 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001213 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1214 char *cp;
1215
Damien Millerfef95ad2006-07-10 20:46:55 +10001216 extern char *optarg;
1217 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001218
Darren Tuckerce321d82005-10-03 18:11:24 +10001219 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1220 sanitise_stdfd();
1221
Damien Millerfef95ad2006-07-10 20:46:55 +10001222 __progname = ssh_get_progname(argv[0]);
1223 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001224
Damien Millerfef95ad2006-07-10 20:46:55 +10001225 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1226 switch (ch) {
1227 case 'c':
1228 /*
1229 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001230 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001231 */
1232 skipargs = 1;
1233 break;
1234 case 'e':
1235 log_stderr = 1;
1236 break;
1237 case 'l':
1238 log_level = log_level_number(optarg);
1239 if (log_level == SYSLOG_LEVEL_NOT_SET)
1240 error("Invalid log level \"%s\"", optarg);
1241 break;
1242 case 'f':
1243 log_facility = log_facility_number(optarg);
1244 if (log_level == SYSLOG_FACILITY_NOT_SET)
1245 error("Invalid log facility \"%s\"", optarg);
1246 break;
1247 case 'h':
1248 default:
1249 usage();
1250 }
1251 }
1252
1253 log_init(__progname, log_level, log_facility, log_stderr);
1254
1255 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1256 client_addr = xstrdup(cp);
1257 if ((cp = strchr(client_addr, ' ')) == NULL)
1258 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1259 getenv("SSH_CONNECTION"));
1260 *cp = '\0';
1261 } else
1262 client_addr = xstrdup("UNKNOWN");
1263
1264 if ((pw = getpwuid(getuid())) == NULL)
1265 fatal("No user found for uid %lu", (u_long)getuid());
1266 pw = pwcopy(pw);
1267
1268 logit("session opened for local user %s from [%s]",
1269 pw->pw_name, client_addr);
1270
Damien Miller7b28dc52000-09-05 13:34:53 +11001271 handle_init();
1272
Damien Miller7b28dc52000-09-05 13:34:53 +11001273 in = dup(STDIN_FILENO);
1274 out = dup(STDOUT_FILENO);
1275
Damien Miller402b3312001-04-14 00:28:42 +10001276#ifdef HAVE_CYGWIN
1277 setmode(in, O_BINARY);
1278 setmode(out, O_BINARY);
1279#endif
1280
Damien Miller7b28dc52000-09-05 13:34:53 +11001281 max = 0;
1282 if (in > max)
1283 max = in;
1284 if (out > max)
1285 max = out;
1286
1287 buffer_init(&iqueue);
1288 buffer_init(&oqueue);
1289
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001290 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1291 rset = (fd_set *)xmalloc(set_size);
1292 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001293
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001294 for (;;) {
1295 memset(rset, 0, set_size);
1296 memset(wset, 0, set_size);
1297
1298 FD_SET(in, rset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001299 olen = buffer_len(&oqueue);
1300 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001301 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001302
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001303 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001304 if (errno == EINTR)
1305 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001306 error("select: %s", strerror(errno));
1307 cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001308 }
1309
1310 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001311 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001312 char buf[4*4096];
1313 len = read(in, buf, sizeof buf);
1314 if (len == 0) {
1315 debug("read eof");
Damien Millerfef95ad2006-07-10 20:46:55 +10001316 cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001317 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001318 error("read: %s", strerror(errno));
1319 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001320 } else {
1321 buffer_append(&iqueue, buf, len);
1322 }
1323 }
1324 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001325 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001326 len = write(out, buffer_ptr(&oqueue), olen);
1327 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001328 error("write: %s", strerror(errno));
1329 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001330 } else {
1331 buffer_consume(&oqueue, len);
1332 }
1333 }
1334 /* process requests from client */
1335 process();
1336 }
1337}