blob: c0839782dece03e3b9bc5f51f81d49d263bd5b61 [file] [log] [blame]
Damien Millera7a73ee2006-08-05 11:37:59 +10001/* $OpenBSD: sftp-server.c,v 1.69 2006/08/01 23:22:47 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>
Damien Miller8dbffe72006-08-05 11:02:17 +100020#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110021#include <sys/stat.h>
Damien Miller9aec9192006-08-05 10:57:45 +100022#ifdef HAVE_SYS_TIME_H
23# include <sys/time.h>
24#endif
Damien Miller88f254b2006-03-15 11:25:13 +110025
26#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100027#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100028#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100029#include <pwd.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100030#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100031#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100032#include <string.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100033#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100034#include <unistd.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110035
Damien Miller7b28dc52000-09-05 13:34:53 +110036#include "buffer.h"
37#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038#include "log.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110039#include "xmalloc.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100040#include "misc.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100041#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110042
Ben Lindstrom2f959b42001-01-11 06:20:23 +000043#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110044#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110045
46/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000047#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110048#define get_int() buffer_get_int(&iqueue);
49#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Damien Miller7b28dc52000-09-05 13:34:53 +110050
Damien Millerfef95ad2006-07-10 20:46:55 +100051/* Our verbosity */
52LogLevel log_level = SYSLOG_LEVEL_ERROR;
53
54/* Our client */
55struct passwd *pw = NULL;
56char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000057
Damien Miller7b28dc52000-09-05 13:34:53 +110058/* input and output queue */
59Buffer iqueue;
60Buffer oqueue;
61
Damien Miller058316f2001-03-08 10:08:49 +110062/* Version of client */
63int version;
64
Darren Tuckera6612d42003-06-28 12:39:03 +100065/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110066
Damien Miller7b28dc52000-09-05 13:34:53 +110067typedef struct Stat Stat;
68
Damien Miller33804262001-02-04 23:20:18 +110069struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110070 char *name;
71 char *long_name;
72 Attrib attrib;
73};
74
Ben Lindstrombba81212001-06-25 05:01:22 +000075static int
Damien Miller7b28dc52000-09-05 13:34:53 +110076errno_to_portable(int unixerrno)
77{
78 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000079
Damien Miller7b28dc52000-09-05 13:34:53 +110080 switch (unixerrno) {
81 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000082 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110083 break;
84 case ENOENT:
85 case ENOTDIR:
86 case EBADF:
87 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000088 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110089 break;
90 case EPERM:
91 case EACCES:
92 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000093 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110094 break;
95 case ENAMETOOLONG:
96 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000097 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +110098 break;
99 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000100 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100101 break;
102 }
103 return ret;
104}
105
Ben Lindstrombba81212001-06-25 05:01:22 +0000106static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100107flags_from_portable(int pflags)
108{
109 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000110
Ben Lindstrom36592512001-03-05 05:02:08 +0000111 if ((pflags & SSH2_FXF_READ) &&
112 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100113 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000114 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100115 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000116 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100117 flags = O_WRONLY;
118 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000119 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100120 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000121 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100122 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000123 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100124 flags |= O_EXCL;
125 return flags;
126}
127
Damien Millerfef95ad2006-07-10 20:46:55 +1000128static const char *
129string_from_portable(int pflags)
130{
131 static char ret[128];
132
133 *ret = '\0';
134
135#define PAPPEND(str) { \
136 if (*ret != '\0') \
137 strlcat(ret, ",", sizeof(ret)); \
138 strlcat(ret, str, sizeof(ret)); \
139 }
140
141 if (pflags & SSH2_FXF_READ)
142 PAPPEND("READ")
143 if (pflags & SSH2_FXF_WRITE)
144 PAPPEND("WRITE")
145 if (pflags & SSH2_FXF_CREAT)
146 PAPPEND("CREATE")
147 if (pflags & SSH2_FXF_TRUNC)
148 PAPPEND("TRUNCATE")
149 if (pflags & SSH2_FXF_EXCL)
150 PAPPEND("EXCL")
151
152 return ret;
153}
154
Ben Lindstrombba81212001-06-25 05:01:22 +0000155static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100156get_attrib(void)
157{
158 return decode_attrib(&iqueue);
159}
160
161/* handle handles */
162
163typedef struct Handle Handle;
164struct Handle {
165 int use;
166 DIR *dirp;
167 int fd;
168 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000169 u_int64_t bytes_read, bytes_write;
Damien Miller7b28dc52000-09-05 13:34:53 +1100170};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000171
Damien Miller7b28dc52000-09-05 13:34:53 +1100172enum {
173 HANDLE_UNUSED,
174 HANDLE_DIR,
175 HANDLE_FILE
176};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000177
Damien Miller7b28dc52000-09-05 13:34:53 +1100178Handle handles[100];
179
Ben Lindstrombba81212001-06-25 05:01:22 +0000180static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100181handle_init(void)
182{
Damien Millereccb9de2005-06-17 12:59:34 +1000183 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000184
Damien Miller9f0f5c62001-12-21 14:45:46 +1100185 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
Damien Miller7b28dc52000-09-05 13:34:53 +1100186 handles[i].use = HANDLE_UNUSED;
187}
188
Ben Lindstrombba81212001-06-25 05:01:22 +0000189static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100190handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100191{
Damien Millereccb9de2005-06-17 12:59:34 +1000192 u_int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000193
Damien Miller9f0f5c62001-12-21 14:45:46 +1100194 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100195 if (handles[i].use == HANDLE_UNUSED) {
196 handles[i].use = use;
197 handles[i].dirp = dirp;
198 handles[i].fd = fd;
Damien Miller00111382003-03-10 11:21:17 +1100199 handles[i].name = xstrdup(name);
Damien Millerfef95ad2006-07-10 20:46:55 +1000200 handles[i].bytes_read = handles[i].bytes_write = 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100201 return i;
202 }
203 }
204 return -1;
205}
206
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100208handle_is_ok(int i, int type)
209{
Damien Millereccb9de2005-06-17 12:59:34 +1000210 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000211 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100212}
213
Ben Lindstrombba81212001-06-25 05:01:22 +0000214static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100215handle_to_string(int handle, char **stringp, int *hlenp)
216{
Damien Miller7b28dc52000-09-05 13:34:53 +1100217 if (stringp == NULL || hlenp == NULL)
218 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000219 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100220 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000221 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100222 return 0;
223}
224
Ben Lindstrombba81212001-06-25 05:01:22 +0000225static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100226handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100227{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000228 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000229
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000230 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100231 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100232 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100233 if (handle_is_ok(val, HANDLE_FILE) ||
234 handle_is_ok(val, HANDLE_DIR))
235 return val;
236 return -1;
237}
238
Ben Lindstrombba81212001-06-25 05:01:22 +0000239static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100240handle_to_name(int handle)
241{
242 if (handle_is_ok(handle, HANDLE_DIR)||
243 handle_is_ok(handle, HANDLE_FILE))
244 return handles[handle].name;
245 return NULL;
246}
247
Ben Lindstrombba81212001-06-25 05:01:22 +0000248static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100249handle_to_dir(int handle)
250{
251 if (handle_is_ok(handle, HANDLE_DIR))
252 return handles[handle].dirp;
253 return NULL;
254}
255
Ben Lindstrombba81212001-06-25 05:01:22 +0000256static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100257handle_to_fd(int handle)
258{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000259 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100260 return handles[handle].fd;
261 return -1;
262}
263
Damien Millerfef95ad2006-07-10 20:46:55 +1000264static void
265handle_update_read(int handle, ssize_t bytes)
266{
267 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
268 handles[handle].bytes_read += bytes;
269}
270
271static void
272handle_update_write(int handle, ssize_t bytes)
273{
274 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
275 handles[handle].bytes_write += bytes;
276}
277
278static u_int64_t
279handle_bytes_read(int handle)
280{
281 if (handle_is_ok(handle, HANDLE_FILE))
282 return (handles[handle].bytes_read);
283 return 0;
284}
285
286static u_int64_t
287handle_bytes_write(int handle)
288{
289 if (handle_is_ok(handle, HANDLE_FILE))
290 return (handles[handle].bytes_write);
291 return 0;
292}
293
Ben Lindstrombba81212001-06-25 05:01:22 +0000294static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100295handle_close(int handle)
296{
297 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000298
Damien Miller7b28dc52000-09-05 13:34:53 +1100299 if (handle_is_ok(handle, HANDLE_FILE)) {
300 ret = close(handles[handle].fd);
301 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100302 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100303 } else if (handle_is_ok(handle, HANDLE_DIR)) {
304 ret = closedir(handles[handle].dirp);
305 handles[handle].use = HANDLE_UNUSED;
Damien Miller00111382003-03-10 11:21:17 +1100306 xfree(handles[handle].name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100307 } else {
308 errno = ENOENT;
309 }
310 return ret;
311}
312
Damien Millerfef95ad2006-07-10 20:46:55 +1000313static void
314handle_log_close(int handle, char *emsg)
315{
316 if (handle_is_ok(handle, HANDLE_FILE)) {
317 logit("%s%sclose \"%s\" bytes read %llu written %llu",
318 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
319 handle_to_name(handle),
320 handle_bytes_read(handle), handle_bytes_write(handle));
321 } else {
322 logit("%s%sclosedir \"%s\"",
323 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
324 handle_to_name(handle));
325 }
326}
327
328static void
329handle_log_exit(void)
330{
331 u_int i;
332
333 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
334 if (handles[i].use != HANDLE_UNUSED)
335 handle_log_close(i, "forced");
336}
337
Ben Lindstrombba81212001-06-25 05:01:22 +0000338static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100339get_handle(void)
340{
341 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000342 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100343 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000344
Damien Miller7b28dc52000-09-05 13:34:53 +1100345 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000346 if (hlen < 256)
347 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100348 xfree(handle);
349 return val;
350}
351
352/* send replies */
353
Ben Lindstrombba81212001-06-25 05:01:22 +0000354static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100355send_msg(Buffer *m)
356{
357 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000358
Damien Miller7b28dc52000-09-05 13:34:53 +1100359 buffer_put_int(&oqueue, mlen);
360 buffer_append(&oqueue, buffer_ptr(m), mlen);
361 buffer_consume(m, mlen);
362}
363
Damien Millerfef95ad2006-07-10 20:46:55 +1000364static const char *
365status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100366{
Damien Miller058316f2001-03-08 10:08:49 +1100367 const char *status_messages[] = {
368 "Success", /* SSH_FX_OK */
369 "End of file", /* SSH_FX_EOF */
370 "No such file", /* SSH_FX_NO_SUCH_FILE */
371 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
372 "Failure", /* SSH_FX_FAILURE */
373 "Bad message", /* SSH_FX_BAD_MESSAGE */
374 "No connection", /* SSH_FX_NO_CONNECTION */
375 "Connection lost", /* SSH_FX_CONNECTION_LOST */
376 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
377 "Unknown error" /* Others */
378 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000379 return (status_messages[MIN(status,SSH2_FX_MAX)]);
380}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000381
Damien Millerfef95ad2006-07-10 20:46:55 +1000382static void
383send_status(u_int32_t id, u_int32_t status)
384{
385 Buffer msg;
386
387 debug3("request %u: sent status %u", id, status);
388 if (log_level > SYSLOG_LEVEL_VERBOSE ||
389 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
390 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100391 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000392 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100393 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000394 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100395 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000396 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100397 buffer_put_cstring(&msg, "");
398 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100399 send_msg(&msg);
400 buffer_free(&msg);
401}
Ben Lindstrombba81212001-06-25 05:01:22 +0000402static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100403send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100404{
405 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000406
Damien Miller7b28dc52000-09-05 13:34:53 +1100407 buffer_init(&msg);
408 buffer_put_char(&msg, type);
409 buffer_put_int(&msg, id);
410 buffer_put_string(&msg, data, dlen);
411 send_msg(&msg);
412 buffer_free(&msg);
413}
414
Ben Lindstrombba81212001-06-25 05:01:22 +0000415static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100416send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100417{
Damien Millerfef95ad2006-07-10 20:46:55 +1000418 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000419 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100420}
421
Ben Lindstrombba81212001-06-25 05:01:22 +0000422static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100423send_handle(u_int32_t id, int handle)
424{
425 char *string;
426 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000427
Damien Miller7b28dc52000-09-05 13:34:53 +1100428 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000429 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000430 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100431 xfree(string);
432}
433
Ben Lindstrombba81212001-06-25 05:01:22 +0000434static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100435send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100436{
437 Buffer msg;
438 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000439
Damien Miller7b28dc52000-09-05 13:34:53 +1100440 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000441 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100442 buffer_put_int(&msg, id);
443 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000444 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100445 for (i = 0; i < count; i++) {
446 buffer_put_cstring(&msg, stats[i].name);
447 buffer_put_cstring(&msg, stats[i].long_name);
448 encode_attrib(&msg, &stats[i].attrib);
449 }
450 send_msg(&msg);
451 buffer_free(&msg);
452}
453
Ben Lindstrombba81212001-06-25 05:01:22 +0000454static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100455send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100456{
457 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000458
Damien Millerfef95ad2006-07-10 20:46:55 +1000459 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100460 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000461 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100462 buffer_put_int(&msg, id);
463 encode_attrib(&msg, a);
464 send_msg(&msg);
465 buffer_free(&msg);
466}
467
468/* parse incoming */
469
Ben Lindstrombba81212001-06-25 05:01:22 +0000470static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100471process_init(void)
472{
473 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100474
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000475 version = get_int();
Damien Millerfef95ad2006-07-10 20:46:55 +1000476 verbose("received client version %d", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100477 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000478 buffer_put_char(&msg, SSH2_FXP_VERSION);
479 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100480 send_msg(&msg);
481 buffer_free(&msg);
482}
483
Ben Lindstrombba81212001-06-25 05:01:22 +0000484static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100485process_open(void)
486{
487 u_int32_t id, pflags;
488 Attrib *a;
489 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000490 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100491
492 id = get_int();
493 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000494 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000495 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100496 a = get_attrib();
497 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000498 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000499 logit("open \"%s\" flags %s mode 0%o",
500 name, string_from_portable(pflags), mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100501 fd = open(name, flags, mode);
502 if (fd < 0) {
503 status = errno_to_portable(errno);
504 } else {
Damien Miller00111382003-03-10 11:21:17 +1100505 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100506 if (handle < 0) {
507 close(fd);
508 } else {
509 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000510 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100511 }
512 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000513 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100514 send_status(id, status);
515 xfree(name);
516}
517
Ben Lindstrombba81212001-06-25 05:01:22 +0000518static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100519process_close(void)
520{
521 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000522 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100523
524 id = get_int();
525 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000526 debug3("request %u: close handle %u", id, handle);
527 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100528 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000529 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100530 send_status(id, status);
531}
532
Ben Lindstrombba81212001-06-25 05:01:22 +0000533static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100534process_read(void)
535{
536 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000537 u_int32_t id, len;
538 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100539 u_int64_t off;
540
541 id = get_int();
542 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000543 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100544 len = get_int();
545
Damien Millerfef95ad2006-07-10 20:46:55 +1000546 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
547 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100548 if (len > sizeof buf) {
549 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000550 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100551 }
552 fd = handle_to_fd(handle);
553 if (fd >= 0) {
554 if (lseek(fd, off, SEEK_SET) < 0) {
555 error("process_read: seek failed");
556 status = errno_to_portable(errno);
557 } else {
558 ret = read(fd, buf, len);
559 if (ret < 0) {
560 status = errno_to_portable(errno);
561 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000562 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100563 } else {
564 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000565 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000566 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100567 }
568 }
569 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000570 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100571 send_status(id, status);
572}
573
Ben Lindstrombba81212001-06-25 05:01:22 +0000574static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100575process_write(void)
576{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000577 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100578 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100579 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000580 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100581 char *data;
582
583 id = get_int();
584 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000585 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100586 data = get_string(&len);
587
Damien Millerfef95ad2006-07-10 20:46:55 +1000588 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
589 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100590 fd = handle_to_fd(handle);
591 if (fd >= 0) {
592 if (lseek(fd, off, SEEK_SET) < 0) {
593 status = errno_to_portable(errno);
594 error("process_write: seek failed");
595 } else {
596/* XXX ATOMICIO ? */
597 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000598 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100599 error("process_write: write failed");
600 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000601 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000602 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000603 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100604 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000605 debug2("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100606 }
607 }
608 }
609 send_status(id, status);
610 xfree(data);
611}
612
Ben Lindstrombba81212001-06-25 05:01:22 +0000613static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100614process_do_stat(int do_lstat)
615{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000616 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100617 struct stat st;
618 u_int32_t id;
619 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000620 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100621
622 id = get_int();
623 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000624 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
625 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100626 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
627 if (ret < 0) {
628 status = errno_to_portable(errno);
629 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000630 stat_to_attrib(&st, &a);
631 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000632 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100633 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000634 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100635 send_status(id, status);
636 xfree(name);
637}
638
Ben Lindstrombba81212001-06-25 05:01:22 +0000639static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100640process_stat(void)
641{
642 process_do_stat(0);
643}
644
Ben Lindstrombba81212001-06-25 05:01:22 +0000645static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100646process_lstat(void)
647{
648 process_do_stat(1);
649}
650
Ben Lindstrombba81212001-06-25 05:01:22 +0000651static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100652process_fstat(void)
653{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000654 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100655 struct stat st;
656 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000657 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100658
659 id = get_int();
660 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000661 debug("request %u: fstat \"%s\" (handle %u)",
662 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100663 fd = handle_to_fd(handle);
664 if (fd >= 0) {
665 ret = fstat(fd, &st);
666 if (ret < 0) {
667 status = errno_to_portable(errno);
668 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000669 stat_to_attrib(&st, &a);
670 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000671 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100672 }
673 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000674 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100675 send_status(id, status);
676}
677
Ben Lindstrombba81212001-06-25 05:01:22 +0000678static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100679attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100680{
681 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000682
Damien Miller7b28dc52000-09-05 13:34:53 +1100683 tv[0].tv_sec = a->atime;
684 tv[0].tv_usec = 0;
685 tv[1].tv_sec = a->mtime;
686 tv[1].tv_usec = 0;
687 return tv;
688}
689
Ben Lindstrombba81212001-06-25 05:01:22 +0000690static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100691process_setstat(void)
692{
693 Attrib *a;
694 u_int32_t id;
695 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000696 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100697
698 id = get_int();
699 name = get_string(NULL);
700 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000701 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100702 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000703 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100704 ret = truncate(name, a->size);
705 if (ret == -1)
706 status = errno_to_portable(errno);
707 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000708 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000709 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller7b28dc52000-09-05 13:34:53 +1100710 ret = chmod(name, a->perm & 0777);
711 if (ret == -1)
712 status = errno_to_portable(errno);
713 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000714 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000715 char buf[64];
716 time_t t = a->mtime;
717
718 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
719 localtime(&t));
720 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100721 ret = utimes(name, attrib_to_tv(a));
722 if (ret == -1)
723 status = errno_to_portable(errno);
724 }
Kevin Steves8e743932001-02-05 13:24:35 +0000725 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000726 logit("set \"%s\" owner %lu group %lu", name,
727 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000728 ret = chown(name, a->uid, a->gid);
729 if (ret == -1)
730 status = errno_to_portable(errno);
731 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100732 send_status(id, status);
733 xfree(name);
734}
735
Ben Lindstrombba81212001-06-25 05:01:22 +0000736static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100737process_fsetstat(void)
738{
739 Attrib *a;
740 u_int32_t id;
741 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000742 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000743
Damien Miller7b28dc52000-09-05 13:34:53 +1100744 id = get_int();
745 handle = get_handle();
746 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000747 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100748 fd = handle_to_fd(handle);
Damien Millerfef95ad2006-07-10 20:46:55 +1000749 if (fd < 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000750 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100751 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000752 char *name = handle_to_name(handle);
753
Damien Miller00c92172002-02-13 14:05:00 +1100754 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000755 logit("set \"%s\" size %llu", name, a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100756 ret = ftruncate(fd, a->size);
757 if (ret == -1)
758 status = errno_to_portable(errno);
759 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000760 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000761 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000762#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100763 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000764#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000765 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000766#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100767 if (ret == -1)
768 status = errno_to_portable(errno);
769 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000770 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000771 char buf[64];
772 time_t t = a->mtime;
773
774 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
775 localtime(&t));
776 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100777#ifdef HAVE_FUTIMES
778 ret = futimes(fd, attrib_to_tv(a));
779#else
780 ret = utimes(name, attrib_to_tv(a));
781#endif
782 if (ret == -1)
783 status = errno_to_portable(errno);
784 }
Kevin Steves8e743932001-02-05 13:24:35 +0000785 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000786 logit("set \"%s\" owner %lu group %lu", name,
787 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000788#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000789 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000790#else
791 ret = chown(name, a->uid, a->gid);
792#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000793 if (ret == -1)
794 status = errno_to_portable(errno);
795 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100796 }
797 send_status(id, status);
798}
799
Ben Lindstrombba81212001-06-25 05:01:22 +0000800static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100801process_opendir(void)
802{
803 DIR *dirp = NULL;
804 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000805 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100806 u_int32_t id;
807
808 id = get_int();
809 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000810 debug3("request %u: opendir", id);
811 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000812 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100813 if (dirp == NULL) {
814 status = errno_to_portable(errno);
815 } else {
Damien Miller00111382003-03-10 11:21:17 +1100816 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100817 if (handle < 0) {
818 closedir(dirp);
819 } else {
820 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000821 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100822 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000823
Damien Miller7b28dc52000-09-05 13:34:53 +1100824 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000825 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100826 send_status(id, status);
827 xfree(path);
828}
829
Ben Lindstrombba81212001-06-25 05:01:22 +0000830static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100831process_readdir(void)
832{
833 DIR *dirp;
834 struct dirent *dp;
835 char *path;
836 int handle;
837 u_int32_t id;
838
839 id = get_int();
840 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000841 debug("request %u: readdir \"%s\" (handle %d)", id,
842 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100843 dirp = handle_to_dir(handle);
844 path = handle_to_name(handle);
845 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000846 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100847 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100848 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +1000849 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +1100850 Stat *stats;
851 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000852
Damien Miller07d86be2006-03-26 14:19:21 +1100853 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100854 while ((dp = readdir(dirp)) != NULL) {
855 if (count >= nstats) {
856 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +1100857 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100858 }
859/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000860 snprintf(pathname, sizeof pathname, "%s%s%s", path,
861 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100862 if (lstat(pathname, &st) < 0)
863 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000864 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100865 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000866 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100867 count++;
868 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000869 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100870 if (count == 100)
871 break;
872 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000873 if (count > 0) {
874 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100875 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000876 xfree(stats[i].name);
877 xfree(stats[i].long_name);
878 }
879 } else {
880 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100881 }
882 xfree(stats);
883 }
884}
885
Ben Lindstrombba81212001-06-25 05:01:22 +0000886static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100887process_remove(void)
888{
889 char *name;
890 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000891 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100892 int ret;
893
894 id = get_int();
895 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000896 debug3("request %u: remove", id);
897 logit("remove name \"%s\"", name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000898 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000899 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100900 send_status(id, status);
901 xfree(name);
902}
903
Ben Lindstrombba81212001-06-25 05:01:22 +0000904static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100905process_mkdir(void)
906{
907 Attrib *a;
908 u_int32_t id;
909 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000910 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100911
912 id = get_int();
913 name = get_string(NULL);
914 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000915 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
916 a->perm & 0777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +1000917 debug3("request %u: mkdir", id);
918 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100919 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000920 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100921 send_status(id, status);
922 xfree(name);
923}
924
Ben Lindstrombba81212001-06-25 05:01:22 +0000925static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100926process_rmdir(void)
927{
928 u_int32_t id;
929 char *name;
930 int ret, status;
931
932 id = get_int();
933 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000934 debug3("request %u: rmdir", id);
935 logit("rmdir name \"%s\"", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100936 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000937 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100938 send_status(id, status);
939 xfree(name);
940}
941
Ben Lindstrombba81212001-06-25 05:01:22 +0000942static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100943process_realpath(void)
944{
945 char resolvedname[MAXPATHLEN];
946 u_int32_t id;
947 char *path;
948
949 id = get_int();
950 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000951 if (path[0] == '\0') {
952 xfree(path);
953 path = xstrdup(".");
954 }
Damien Millerfef95ad2006-07-10 20:46:55 +1000955 debug3("request %u: realpath", id);
956 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100957 if (realpath(path, resolvedname) == NULL) {
958 send_status(id, errno_to_portable(errno));
959 } else {
960 Stat s;
961 attrib_clear(&s.attrib);
962 s.name = s.long_name = resolvedname;
963 send_names(id, 1, &s);
964 }
965 xfree(path);
966}
967
Ben Lindstrombba81212001-06-25 05:01:22 +0000968static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100969process_rename(void)
970{
971 u_int32_t id;
972 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100973 int status;
Damien Millerb3207e82003-03-26 16:01:11 +1100974 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +1100975
976 id = get_int();
977 oldpath = get_string(NULL);
978 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000979 debug3("request %u: rename", id);
980 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +1100981 status = SSH2_FX_FAILURE;
982 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +1100983 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +1100984 else if (S_ISREG(sb.st_mode)) {
985 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000986 if (link(oldpath, newpath) == -1) {
Darren Tuckere59b5082004-06-28 16:01:19 +1000987 if (errno == EOPNOTSUPP
988#ifdef LINK_OPNOTSUPP_ERRNO
989 || errno == LINK_OPNOTSUPP_ERRNO
990#endif
991 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +1000992 struct stat st;
993
994 /*
995 * fs doesn't support links, so fall back to
996 * stat+rename. This is racy.
997 */
998 if (stat(newpath, &st) == -1) {
999 if (rename(oldpath, newpath) == -1)
1000 status =
1001 errno_to_portable(errno);
1002 else
1003 status = SSH2_FX_OK;
1004 }
1005 } else {
1006 status = errno_to_portable(errno);
1007 }
1008 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001009 status = errno_to_portable(errno);
1010 /* clean spare link */
1011 unlink(newpath);
1012 } else
1013 status = SSH2_FX_OK;
1014 } else if (stat(newpath, &sb) == -1) {
1015 if (rename(oldpath, newpath) == -1)
1016 status = errno_to_portable(errno);
1017 else
1018 status = SSH2_FX_OK;
1019 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001020 send_status(id, status);
1021 xfree(oldpath);
1022 xfree(newpath);
1023}
1024
Ben Lindstrombba81212001-06-25 05:01:22 +00001025static void
Damien Miller058316f2001-03-08 10:08:49 +11001026process_readlink(void)
1027{
1028 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001029 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001030 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001031 char *path;
1032
1033 id = get_int();
1034 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001035 debug3("request %u: readlink", id);
1036 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001037 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001038 send_status(id, errno_to_portable(errno));
1039 else {
1040 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001041
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001042 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001043 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001044 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001045 send_names(id, 1, &s);
1046 }
1047 xfree(path);
1048}
1049
Ben Lindstrombba81212001-06-25 05:01:22 +00001050static void
Damien Miller058316f2001-03-08 10:08:49 +11001051process_symlink(void)
1052{
1053 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +11001054 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001055 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001056
1057 id = get_int();
1058 oldpath = get_string(NULL);
1059 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001060 debug3("request %u: symlink", id);
1061 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001062 /* this will fail if 'newpath' exists */
1063 ret = symlink(oldpath, newpath);
1064 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001065 send_status(id, status);
1066 xfree(oldpath);
1067 xfree(newpath);
1068}
1069
Ben Lindstrombba81212001-06-25 05:01:22 +00001070static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001071process_extended(void)
1072{
1073 u_int32_t id;
1074 char *request;
1075
1076 id = get_int();
1077 request = get_string(NULL);
1078 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
1079 xfree(request);
1080}
Damien Miller7b28dc52000-09-05 13:34:53 +11001081
1082/* stolen from ssh-agent */
1083
Ben Lindstrombba81212001-06-25 05:01:22 +00001084static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001085process(void)
1086{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001087 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +00001088 u_int buf_len;
1089 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001090 u_int type;
1091 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +11001092
Ben Lindstrom2c140472002-06-06 21:57:54 +00001093 buf_len = buffer_len(&iqueue);
1094 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001095 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001096 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001097 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001098 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001099 error("bad message from %s local user %s",
1100 client_addr, pw->pw_name);
1101 cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001102 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001103 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001104 return;
1105 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001106 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001107 type = buffer_get_char(&iqueue);
1108 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001109 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001110 process_init();
1111 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001112 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +11001113 process_open();
1114 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001115 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001116 process_close();
1117 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001118 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +11001119 process_read();
1120 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001121 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001122 process_write();
1123 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001124 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001125 process_lstat();
1126 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001127 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001128 process_fstat();
1129 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001130 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001131 process_setstat();
1132 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001133 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001134 process_fsetstat();
1135 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001136 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001137 process_opendir();
1138 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001139 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001140 process_readdir();
1141 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001142 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001143 process_remove();
1144 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001145 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001146 process_mkdir();
1147 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001148 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001149 process_rmdir();
1150 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001151 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001152 process_realpath();
1153 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001154 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001155 process_stat();
1156 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001157 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001158 process_rename();
1159 break;
Damien Miller058316f2001-03-08 10:08:49 +11001160 case SSH2_FXP_READLINK:
1161 process_readlink();
1162 break;
1163 case SSH2_FXP_SYMLINK:
1164 process_symlink();
1165 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001166 case SSH2_FXP_EXTENDED:
1167 process_extended();
1168 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001169 default:
1170 error("Unknown message %d", type);
1171 break;
1172 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001173 /* discard the remaining bytes from the current packet */
1174 if (buf_len < buffer_len(&iqueue))
Damien Millerfef95ad2006-07-10 20:46:55 +10001175 fatal("iqueue grew unexpectedly");
Ben Lindstrom2c140472002-06-06 21:57:54 +00001176 consumed = buf_len - buffer_len(&iqueue);
1177 if (msg_len < consumed)
1178 fatal("msg_len %d < consumed %d", msg_len, consumed);
1179 if (msg_len > consumed)
1180 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001181}
1182
Damien Millerfef95ad2006-07-10 20:46:55 +10001183/* Cleanup handler that logs active handles upon normal exit */
1184void
1185cleanup_exit(int i)
1186{
1187 if (pw != NULL && client_addr != NULL) {
1188 handle_log_exit();
1189 logit("session closed for local user %s from [%s]",
1190 pw->pw_name, client_addr);
1191 }
1192 _exit(i);
1193}
1194
1195static void
1196usage(void)
1197{
1198 extern char *__progname;
1199
1200 fprintf(stderr,
1201 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1202 exit(1);
1203}
1204
Damien Miller7b28dc52000-09-05 13:34:53 +11001205int
Damien Millerfef95ad2006-07-10 20:46:55 +10001206main(int argc, char **argv)
Damien Miller7b28dc52000-09-05 13:34:53 +11001207{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001208 fd_set *rset, *wset;
Damien Millerfef95ad2006-07-10 20:46:55 +10001209 int in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001210 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001211 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
1212 char *cp;
1213
Damien Millerfef95ad2006-07-10 20:46:55 +10001214 extern char *optarg;
1215 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001216
Darren Tuckerce321d82005-10-03 18:11:24 +10001217 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1218 sanitise_stdfd();
1219
Damien Millerfef95ad2006-07-10 20:46:55 +10001220 __progname = ssh_get_progname(argv[0]);
1221 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001222
Damien Millerfef95ad2006-07-10 20:46:55 +10001223 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1224 switch (ch) {
1225 case 'c':
1226 /*
1227 * Ignore all arguments if we are invoked as a
1228 * shell using "sftp-server -c command"
1229 */
1230 skipargs = 1;
1231 break;
1232 case 'e':
1233 log_stderr = 1;
1234 break;
1235 case 'l':
1236 log_level = log_level_number(optarg);
1237 if (log_level == SYSLOG_LEVEL_NOT_SET)
1238 error("Invalid log level \"%s\"", optarg);
1239 break;
1240 case 'f':
1241 log_facility = log_facility_number(optarg);
1242 if (log_level == SYSLOG_FACILITY_NOT_SET)
1243 error("Invalid log facility \"%s\"", optarg);
1244 break;
1245 case 'h':
1246 default:
1247 usage();
1248 }
1249 }
1250
1251 log_init(__progname, log_level, log_facility, log_stderr);
1252
1253 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1254 client_addr = xstrdup(cp);
1255 if ((cp = strchr(client_addr, ' ')) == NULL)
1256 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
1257 getenv("SSH_CONNECTION"));
1258 *cp = '\0';
1259 } else
1260 client_addr = xstrdup("UNKNOWN");
1261
1262 if ((pw = getpwuid(getuid())) == NULL)
1263 fatal("No user found for uid %lu", (u_long)getuid());
1264 pw = pwcopy(pw);
1265
1266 logit("session opened for local user %s from [%s]",
1267 pw->pw_name, client_addr);
1268
Damien Miller7b28dc52000-09-05 13:34:53 +11001269 handle_init();
1270
Damien Miller7b28dc52000-09-05 13:34:53 +11001271 in = dup(STDIN_FILENO);
1272 out = dup(STDOUT_FILENO);
1273
Damien Miller402b3312001-04-14 00:28:42 +10001274#ifdef HAVE_CYGWIN
1275 setmode(in, O_BINARY);
1276 setmode(out, O_BINARY);
1277#endif
1278
Damien Miller7b28dc52000-09-05 13:34:53 +11001279 max = 0;
1280 if (in > max)
1281 max = in;
1282 if (out > max)
1283 max = out;
1284
1285 buffer_init(&iqueue);
1286 buffer_init(&oqueue);
1287
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001288 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1289 rset = (fd_set *)xmalloc(set_size);
1290 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001291
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001292 for (;;) {
1293 memset(rset, 0, set_size);
1294 memset(wset, 0, set_size);
1295
1296 FD_SET(in, rset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001297 olen = buffer_len(&oqueue);
1298 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001299 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001300
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001301 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001302 if (errno == EINTR)
1303 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001304 error("select: %s", strerror(errno));
1305 cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001306 }
1307
1308 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001309 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001310 char buf[4*4096];
1311 len = read(in, buf, sizeof buf);
1312 if (len == 0) {
1313 debug("read eof");
Damien Millerfef95ad2006-07-10 20:46:55 +10001314 cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001315 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001316 error("read: %s", strerror(errno));
1317 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001318 } else {
1319 buffer_append(&iqueue, buf, len);
1320 }
1321 }
1322 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001323 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001324 len = write(out, buffer_ptr(&oqueue), olen);
1325 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001326 error("write: %s", strerror(errno));
1327 cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001328 } else {
1329 buffer_consume(&oqueue, len);
1330 }
1331 }
1332 /* process requests from client */
1333 process();
1334 }
1335}