blob: 9661224525d3047ac981a4fde9aa5f110aac58ef [file] [log] [blame]
Damien Miller7b28dc52000-09-05 13:34:53 +11001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Miller7b28dc52000-09-05 13:34:53 +110012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24#include "includes.h"
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +000025RCSID("$OpenBSD: sftp-server.c,v 1.24 2001/03/14 22:50:25 deraadt Exp $");
Damien Miller7b28dc52000-09-05 13:34:53 +110026
Damien Miller7b28dc52000-09-05 13:34:53 +110027#include "buffer.h"
28#include "bufaux.h"
29#include "getput.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include "log.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110031#include "xmalloc.h"
32
Ben Lindstrom2f959b42001-01-11 06:20:23 +000033#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110034#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110035
36/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000037#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110038#define get_int() buffer_get_int(&iqueue);
39#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Ben Lindstrom2f959b42001-01-11 06:20:23 +000040#define TRACE debug
Damien Miller7b28dc52000-09-05 13:34:53 +110041
Ben Lindstrom49a79c02000-11-17 03:47:20 +000042#ifdef HAVE___PROGNAME
43extern char *__progname;
44#else
45char *__progname;
46#endif
47
Damien Miller7b28dc52000-09-05 13:34:53 +110048/* input and output queue */
49Buffer iqueue;
50Buffer oqueue;
51
Damien Miller058316f2001-03-08 10:08:49 +110052/* Version of client */
53int version;
54
Damien Miller7b28dc52000-09-05 13:34:53 +110055/* portable attibutes, etc. */
56
Damien Miller7b28dc52000-09-05 13:34:53 +110057typedef struct Stat Stat;
58
Damien Miller33804262001-02-04 23:20:18 +110059struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110060 char *name;
61 char *long_name;
62 Attrib attrib;
63};
64
65int
66errno_to_portable(int unixerrno)
67{
68 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000069
Damien Miller7b28dc52000-09-05 13:34:53 +110070 switch (unixerrno) {
71 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000072 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110073 break;
74 case ENOENT:
75 case ENOTDIR:
76 case EBADF:
77 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000078 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110079 break;
80 case EPERM:
81 case EACCES:
82 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000083 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110084 break;
85 case ENAMETOOLONG:
86 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000087 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +110088 break;
89 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000090 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +110091 break;
92 }
93 return ret;
94}
95
96int
97flags_from_portable(int pflags)
98{
99 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000100
Ben Lindstrom36592512001-03-05 05:02:08 +0000101 if ((pflags & SSH2_FXF_READ) &&
102 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100103 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000104 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100105 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000106 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100107 flags = O_WRONLY;
108 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000109 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100110 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000111 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100112 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000113 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100114 flags |= O_EXCL;
115 return flags;
116}
117
Damien Miller7b28dc52000-09-05 13:34:53 +1100118Attrib *
119get_attrib(void)
120{
121 return decode_attrib(&iqueue);
122}
123
124/* handle handles */
125
126typedef struct Handle Handle;
127struct Handle {
128 int use;
129 DIR *dirp;
130 int fd;
131 char *name;
132};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000133
Damien Miller7b28dc52000-09-05 13:34:53 +1100134enum {
135 HANDLE_UNUSED,
136 HANDLE_DIR,
137 HANDLE_FILE
138};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000139
Damien Miller7b28dc52000-09-05 13:34:53 +1100140Handle handles[100];
141
142void
143handle_init(void)
144{
145 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000146
Damien Miller7b28dc52000-09-05 13:34:53 +1100147 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++)
148 handles[i].use = HANDLE_UNUSED;
149}
150
151int
152handle_new(int use, char *name, int fd, DIR *dirp)
153{
154 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000155
Damien Miller7b28dc52000-09-05 13:34:53 +1100156 for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
157 if (handles[i].use == HANDLE_UNUSED) {
158 handles[i].use = use;
159 handles[i].dirp = dirp;
160 handles[i].fd = fd;
161 handles[i].name = name;
162 return i;
163 }
164 }
165 return -1;
166}
167
168int
169handle_is_ok(int i, int type)
170{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000171 return i >= 0 && i < sizeof(handles)/sizeof(Handle) &&
172 handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100173}
174
175int
176handle_to_string(int handle, char **stringp, int *hlenp)
177{
Damien Miller7b28dc52000-09-05 13:34:53 +1100178 if (stringp == NULL || hlenp == NULL)
179 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000180 *stringp = xmalloc(sizeof(int32_t));
181 PUT_32BIT(*stringp, handle);
182 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100183 return 0;
184}
185
186int
Damien Millere4340be2000-09-16 13:29:08 +1100187handle_from_string(char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100188{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000189 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000190
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000191 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100192 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000193 val = GET_32BIT(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100194 if (handle_is_ok(val, HANDLE_FILE) ||
195 handle_is_ok(val, HANDLE_DIR))
196 return val;
197 return -1;
198}
199
200char *
201handle_to_name(int handle)
202{
203 if (handle_is_ok(handle, HANDLE_DIR)||
204 handle_is_ok(handle, HANDLE_FILE))
205 return handles[handle].name;
206 return NULL;
207}
208
209DIR *
210handle_to_dir(int handle)
211{
212 if (handle_is_ok(handle, HANDLE_DIR))
213 return handles[handle].dirp;
214 return NULL;
215}
216
217int
218handle_to_fd(int handle)
219{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000220 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100221 return handles[handle].fd;
222 return -1;
223}
224
225int
226handle_close(int handle)
227{
228 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000229
Damien Miller7b28dc52000-09-05 13:34:53 +1100230 if (handle_is_ok(handle, HANDLE_FILE)) {
231 ret = close(handles[handle].fd);
232 handles[handle].use = HANDLE_UNUSED;
233 } else if (handle_is_ok(handle, HANDLE_DIR)) {
234 ret = closedir(handles[handle].dirp);
235 handles[handle].use = HANDLE_UNUSED;
236 } else {
237 errno = ENOENT;
238 }
239 return ret;
240}
241
242int
243get_handle(void)
244{
245 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000246 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100247 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000248
Damien Miller7b28dc52000-09-05 13:34:53 +1100249 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000250 if (hlen < 256)
251 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100252 xfree(handle);
253 return val;
254}
255
256/* send replies */
257
258void
259send_msg(Buffer *m)
260{
261 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000262
Damien Miller7b28dc52000-09-05 13:34:53 +1100263 buffer_put_int(&oqueue, mlen);
264 buffer_append(&oqueue, buffer_ptr(m), mlen);
265 buffer_consume(m, mlen);
266}
267
268void
269send_status(u_int32_t id, u_int32_t error)
270{
271 Buffer msg;
Damien Miller058316f2001-03-08 10:08:49 +1100272 const char *status_messages[] = {
273 "Success", /* SSH_FX_OK */
274 "End of file", /* SSH_FX_EOF */
275 "No such file", /* SSH_FX_NO_SUCH_FILE */
276 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
277 "Failure", /* SSH_FX_FAILURE */
278 "Bad message", /* SSH_FX_BAD_MESSAGE */
279 "No connection", /* SSH_FX_NO_CONNECTION */
280 "Connection lost", /* SSH_FX_CONNECTION_LOST */
281 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
282 "Unknown error" /* Others */
283 };
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000284
Damien Miller7b28dc52000-09-05 13:34:53 +1100285 TRACE("sent status id %d error %d", id, error);
286 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000287 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100288 buffer_put_int(&msg, id);
289 buffer_put_int(&msg, error);
Damien Miller058316f2001-03-08 10:08:49 +1100290 if (version >= 3) {
291 buffer_put_cstring(&msg,
292 status_messages[MIN(error,SSH2_FX_MAX)]);
293 buffer_put_cstring(&msg, "");
294 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100295 send_msg(&msg);
296 buffer_free(&msg);
297}
298void
299send_data_or_handle(char type, u_int32_t id, char *data, int dlen)
300{
301 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000302
Damien Miller7b28dc52000-09-05 13:34:53 +1100303 buffer_init(&msg);
304 buffer_put_char(&msg, type);
305 buffer_put_int(&msg, id);
306 buffer_put_string(&msg, data, dlen);
307 send_msg(&msg);
308 buffer_free(&msg);
309}
310
311void
312send_data(u_int32_t id, char *data, int dlen)
313{
314 TRACE("sent data id %d len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000315 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100316}
317
318void
319send_handle(u_int32_t id, int handle)
320{
321 char *string;
322 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000323
Damien Miller7b28dc52000-09-05 13:34:53 +1100324 handle_to_string(handle, &string, &hlen);
325 TRACE("sent handle id %d handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000326 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100327 xfree(string);
328}
329
330void
331send_names(u_int32_t id, int count, Stat *stats)
332{
333 Buffer msg;
334 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000335
Damien Miller7b28dc52000-09-05 13:34:53 +1100336 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000337 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100338 buffer_put_int(&msg, id);
339 buffer_put_int(&msg, count);
340 TRACE("sent names id %d count %d", id, count);
341 for (i = 0; i < count; i++) {
342 buffer_put_cstring(&msg, stats[i].name);
343 buffer_put_cstring(&msg, stats[i].long_name);
344 encode_attrib(&msg, &stats[i].attrib);
345 }
346 send_msg(&msg);
347 buffer_free(&msg);
348}
349
350void
351send_attrib(u_int32_t id, Attrib *a)
352{
353 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000354
Damien Miller7b28dc52000-09-05 13:34:53 +1100355 TRACE("sent attrib id %d have 0x%x", id, a->flags);
356 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000357 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100358 buffer_put_int(&msg, id);
359 encode_attrib(&msg, a);
360 send_msg(&msg);
361 buffer_free(&msg);
362}
363
364/* parse incoming */
365
366void
367process_init(void)
368{
369 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100370
Damien Miller058316f2001-03-08 10:08:49 +1100371 version = buffer_get_int(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +1100372 TRACE("client version %d", version);
373 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000374 buffer_put_char(&msg, SSH2_FXP_VERSION);
375 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7b28dc52000-09-05 13:34:53 +1100376 send_msg(&msg);
377 buffer_free(&msg);
378}
379
380void
381process_open(void)
382{
383 u_int32_t id, pflags;
384 Attrib *a;
385 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000386 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100387
388 id = get_int();
389 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000390 pflags = get_int(); /* portable flags */
Damien Miller7b28dc52000-09-05 13:34:53 +1100391 a = get_attrib();
392 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000393 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Miller7b28dc52000-09-05 13:34:53 +1100394 TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode);
395 fd = open(name, flags, mode);
396 if (fd < 0) {
397 status = errno_to_portable(errno);
398 } else {
399 handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL);
400 if (handle < 0) {
401 close(fd);
402 } else {
403 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000404 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100405 }
406 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000407 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100408 send_status(id, status);
409 xfree(name);
410}
411
412void
413process_close(void)
414{
415 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000416 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100417
418 id = get_int();
419 handle = get_handle();
420 TRACE("close id %d handle %d", id, handle);
421 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000422 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100423 send_status(id, status);
424}
425
426void
427process_read(void)
428{
429 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000430 u_int32_t id, len;
431 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100432 u_int64_t off;
433
434 id = get_int();
435 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000436 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100437 len = get_int();
438
Kevin Stevesec1c1402001-02-05 15:39:22 +0000439 TRACE("read id %d handle %d off %llu len %d", id, handle,
Ben Lindstrom416d8742001-02-25 02:02:43 +0000440 (u_int64_t)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100441 if (len > sizeof buf) {
442 len = sizeof buf;
443 log("read change len %d", len);
444 }
445 fd = handle_to_fd(handle);
446 if (fd >= 0) {
447 if (lseek(fd, off, SEEK_SET) < 0) {
448 error("process_read: seek failed");
449 status = errno_to_portable(errno);
450 } else {
451 ret = read(fd, buf, len);
452 if (ret < 0) {
453 status = errno_to_portable(errno);
454 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000455 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100456 } else {
457 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000458 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100459 }
460 }
461 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000462 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100463 send_status(id, status);
464}
465
466void
467process_write(void)
468{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000469 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100470 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100471 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000472 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100473 char *data;
474
475 id = get_int();
476 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000477 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100478 data = get_string(&len);
479
Kevin Stevesec1c1402001-02-05 15:39:22 +0000480 TRACE("write id %d handle %d off %llu len %d", id, handle,
Ben Lindstrom416d8742001-02-25 02:02:43 +0000481 (u_int64_t)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100482 fd = handle_to_fd(handle);
483 if (fd >= 0) {
484 if (lseek(fd, off, SEEK_SET) < 0) {
485 status = errno_to_portable(errno);
486 error("process_write: seek failed");
487 } else {
488/* XXX ATOMICIO ? */
489 ret = write(fd, data, len);
490 if (ret == -1) {
491 error("process_write: write failed");
492 status = errno_to_portable(errno);
493 } else if (ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000494 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100495 } else {
496 log("nothing at all written");
497 }
498 }
499 }
500 send_status(id, status);
501 xfree(data);
502}
503
504void
505process_do_stat(int do_lstat)
506{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000507 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100508 struct stat st;
509 u_int32_t id;
510 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000511 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100512
513 id = get_int();
514 name = get_string(NULL);
515 TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name);
516 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
517 if (ret < 0) {
518 status = errno_to_portable(errno);
519 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000520 stat_to_attrib(&st, &a);
521 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000522 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100523 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000524 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100525 send_status(id, status);
526 xfree(name);
527}
528
529void
530process_stat(void)
531{
532 process_do_stat(0);
533}
534
535void
536process_lstat(void)
537{
538 process_do_stat(1);
539}
540
541void
542process_fstat(void)
543{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000544 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100545 struct stat st;
546 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000547 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100548
549 id = get_int();
550 handle = get_handle();
551 TRACE("fstat id %d handle %d", id, handle);
552 fd = handle_to_fd(handle);
553 if (fd >= 0) {
554 ret = fstat(fd, &st);
555 if (ret < 0) {
556 status = errno_to_portable(errno);
557 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000558 stat_to_attrib(&st, &a);
559 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000560 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100561 }
562 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000563 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100564 send_status(id, status);
565}
566
567struct timeval *
568attrib_to_tv(Attrib *a)
569{
570 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000571
Damien Miller7b28dc52000-09-05 13:34:53 +1100572 tv[0].tv_sec = a->atime;
573 tv[0].tv_usec = 0;
574 tv[1].tv_sec = a->mtime;
575 tv[1].tv_usec = 0;
576 return tv;
577}
578
579void
580process_setstat(void)
581{
582 Attrib *a;
583 u_int32_t id;
584 char *name;
585 int ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000586 int status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100587
588 id = get_int();
589 name = get_string(NULL);
590 a = get_attrib();
591 TRACE("setstat id %d name %s", id, name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000592 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100593 ret = chmod(name, a->perm & 0777);
594 if (ret == -1)
595 status = errno_to_portable(errno);
596 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000597 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100598 ret = utimes(name, attrib_to_tv(a));
599 if (ret == -1)
600 status = errno_to_portable(errno);
601 }
Kevin Steves8e743932001-02-05 13:24:35 +0000602 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
603 ret = chown(name, a->uid, a->gid);
604 if (ret == -1)
605 status = errno_to_portable(errno);
606 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100607 send_status(id, status);
608 xfree(name);
609}
610
611void
612process_fsetstat(void)
613{
614 Attrib *a;
615 u_int32_t id;
616 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000617 int status = SSH2_FX_OK;
Damien Millere4340be2000-09-16 13:29:08 +1100618 char *name;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000619
Damien Miller7b28dc52000-09-05 13:34:53 +1100620 id = get_int();
621 handle = get_handle();
622 a = get_attrib();
623 TRACE("fsetstat id %d handle %d", id, handle);
624 fd = handle_to_fd(handle);
625 name = handle_to_name(handle);
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000626 if (fd < 0 || name == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000627 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100628 } else {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000629 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000630#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100631 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000632#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000633 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000634#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100635 if (ret == -1)
636 status = errno_to_portable(errno);
637 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000638 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100639#ifdef HAVE_FUTIMES
640 ret = futimes(fd, attrib_to_tv(a));
641#else
642 ret = utimes(name, attrib_to_tv(a));
643#endif
644 if (ret == -1)
645 status = errno_to_portable(errno);
646 }
Kevin Steves8e743932001-02-05 13:24:35 +0000647 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000648#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000649 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000650#else
651 ret = chown(name, a->uid, a->gid);
652#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000653 if (ret == -1)
654 status = errno_to_portable(errno);
655 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100656 }
657 send_status(id, status);
658}
659
660void
661process_opendir(void)
662{
663 DIR *dirp = NULL;
664 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000665 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100666 u_int32_t id;
667
668 id = get_int();
669 path = get_string(NULL);
670 TRACE("opendir id %d path %s", id, path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000671 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100672 if (dirp == NULL) {
673 status = errno_to_portable(errno);
674 } else {
675 handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
676 if (handle < 0) {
677 closedir(dirp);
678 } else {
679 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000680 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100681 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000682
Damien Miller7b28dc52000-09-05 13:34:53 +1100683 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000684 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100685 send_status(id, status);
686 xfree(path);
687}
688
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000689/*
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000690 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000691 */
Damien Miller7b28dc52000-09-05 13:34:53 +1100692char *
693ls_file(char *name, struct stat *st)
694{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000695 int sz = 0;
696 struct passwd *pw;
697 struct group *gr;
698 struct tm *ltime = localtime(&st->st_mtime);
699 char *user, *group;
700 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
701
702 strmode(st->st_mode, mode);
703 if ((pw = getpwuid(st->st_uid)) != NULL) {
704 user = pw->pw_name;
705 } else {
706 snprintf(ubuf, sizeof ubuf, "%d", st->st_uid);
707 user = ubuf;
708 }
709 if ((gr = getgrgid(st->st_gid)) != NULL) {
710 group = gr->gr_name;
711 } else {
712 snprintf(gbuf, sizeof gbuf, "%d", st->st_gid);
713 group = gbuf;
714 }
715 if (ltime != NULL) {
716 if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
717 sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
718 else
719 sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
720 }
721 if (sz == 0)
722 tbuf[0] = '\0';
Ben Lindstrom7e9aff52001-02-10 23:00:22 +0000723 snprintf(buf, sizeof buf, "%s %3d %-8.8s %-8.8s %8llu %s %s", mode,
Ben Lindstrom416d8742001-02-25 02:02:43 +0000724 st->st_nlink, user, group, (u_int64_t)st->st_size, tbuf, name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100725 return xstrdup(buf);
726}
727
728void
729process_readdir(void)
730{
731 DIR *dirp;
732 struct dirent *dp;
733 char *path;
734 int handle;
735 u_int32_t id;
736
737 id = get_int();
738 handle = get_handle();
739 TRACE("readdir id %d handle %d", id, handle);
740 dirp = handle_to_dir(handle);
741 path = handle_to_name(handle);
742 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000743 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100744 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100745 struct stat st;
746 char pathname[1024];
747 Stat *stats;
748 int nstats = 10, count = 0, i;
749 stats = xmalloc(nstats * sizeof(Stat));
750 while ((dp = readdir(dirp)) != NULL) {
751 if (count >= nstats) {
752 nstats *= 2;
753 stats = xrealloc(stats, nstats * sizeof(Stat));
754 }
755/* XXX OVERFLOW ? */
756 snprintf(pathname, sizeof pathname,
757 "%s/%s", path, dp->d_name);
758 if (lstat(pathname, &st) < 0)
759 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000760 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100761 stats[count].name = xstrdup(dp->d_name);
762 stats[count].long_name = ls_file(dp->d_name, &st);
763 count++;
764 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000765 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100766 if (count == 100)
767 break;
768 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000769 if (count > 0) {
770 send_names(id, count, stats);
771 for(i = 0; i < count; i++) {
772 xfree(stats[i].name);
773 xfree(stats[i].long_name);
774 }
775 } else {
776 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100777 }
778 xfree(stats);
779 }
780}
781
782void
783process_remove(void)
784{
785 char *name;
786 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000787 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100788 int ret;
789
790 id = get_int();
791 name = get_string(NULL);
792 TRACE("remove id %d name %s", id, name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000793 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000794 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100795 send_status(id, status);
796 xfree(name);
797}
798
799void
800process_mkdir(void)
801{
802 Attrib *a;
803 u_int32_t id;
804 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000805 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100806
807 id = get_int();
808 name = get_string(NULL);
809 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000810 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
811 a->perm & 0777 : 0777;
Damien Miller7b28dc52000-09-05 13:34:53 +1100812 TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
813 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000814 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100815 send_status(id, status);
816 xfree(name);
817}
818
819void
820process_rmdir(void)
821{
822 u_int32_t id;
823 char *name;
824 int ret, status;
825
826 id = get_int();
827 name = get_string(NULL);
828 TRACE("rmdir id %d name %s", id, name);
829 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000830 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100831 send_status(id, status);
832 xfree(name);
833}
834
835void
836process_realpath(void)
837{
838 char resolvedname[MAXPATHLEN];
839 u_int32_t id;
840 char *path;
841
842 id = get_int();
843 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000844 if (path[0] == '\0') {
845 xfree(path);
846 path = xstrdup(".");
847 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100848 TRACE("realpath id %d path %s", id, path);
849 if (realpath(path, resolvedname) == NULL) {
850 send_status(id, errno_to_portable(errno));
851 } else {
852 Stat s;
853 attrib_clear(&s.attrib);
854 s.name = s.long_name = resolvedname;
855 send_names(id, 1, &s);
856 }
857 xfree(path);
858}
859
860void
861process_rename(void)
862{
863 u_int32_t id;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000864 struct stat st;
Damien Miller7b28dc52000-09-05 13:34:53 +1100865 char *oldpath, *newpath;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000866 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100867
868 id = get_int();
869 oldpath = get_string(NULL);
870 newpath = get_string(NULL);
871 TRACE("rename id %d old %s new %s", id, oldpath, newpath);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000872 /* fail if 'newpath' exists */
873 if (stat(newpath, &st) == -1) {
874 ret = rename(oldpath, newpath);
875 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
876 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100877 send_status(id, status);
878 xfree(oldpath);
879 xfree(newpath);
880}
881
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000882void
Damien Miller058316f2001-03-08 10:08:49 +1100883process_readlink(void)
884{
885 u_int32_t id;
886 char link[MAXPATHLEN];
887 char *path;
888
889 id = get_int();
890 path = get_string(NULL);
891 TRACE("readlink id %d path %s", id, path);
892 if (readlink(path, link, sizeof(link) - 1) == -1)
893 send_status(id, errno_to_portable(errno));
894 else {
895 Stat s;
896
897 link[sizeof(link) - 1] = '\0';
898 attrib_clear(&s.attrib);
899 s.name = s.long_name = link;
900 send_names(id, 1, &s);
901 }
902 xfree(path);
903}
904
905void
906process_symlink(void)
907{
908 u_int32_t id;
909 struct stat st;
910 char *oldpath, *newpath;
911 int ret, status = SSH2_FX_FAILURE;
912
913 id = get_int();
914 oldpath = get_string(NULL);
915 newpath = get_string(NULL);
916 TRACE("symlink id %d old %s new %s", id, oldpath, newpath);
917 /* fail if 'newpath' exists */
918 if (stat(newpath, &st) == -1) {
919 ret = symlink(oldpath, newpath);
920 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
921 }
922 send_status(id, status);
923 xfree(oldpath);
924 xfree(newpath);
925}
926
927void
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000928process_extended(void)
929{
930 u_int32_t id;
931 char *request;
932
933 id = get_int();
934 request = get_string(NULL);
935 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
936 xfree(request);
937}
Damien Miller7b28dc52000-09-05 13:34:53 +1100938
939/* stolen from ssh-agent */
940
941void
942process(void)
943{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000944 u_int msg_len;
945 u_int type;
946 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +1100947
948 if (buffer_len(&iqueue) < 5)
949 return; /* Incomplete message. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000950 cp = (u_char *) buffer_ptr(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +1100951 msg_len = GET_32BIT(cp);
952 if (msg_len > 256 * 1024) {
953 error("bad message ");
954 exit(11);
955 }
956 if (buffer_len(&iqueue) < msg_len + 4)
957 return;
958 buffer_consume(&iqueue, 4);
959 type = buffer_get_char(&iqueue);
960 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000961 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100962 process_init();
963 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000964 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +1100965 process_open();
966 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000967 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100968 process_close();
969 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000970 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +1100971 process_read();
972 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000973 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100974 process_write();
975 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000976 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100977 process_lstat();
978 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000979 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100980 process_fstat();
981 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000982 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100983 process_setstat();
984 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000985 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +1100986 process_fsetstat();
987 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000988 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100989 process_opendir();
990 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000991 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100992 process_readdir();
993 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000994 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +1100995 process_remove();
996 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000997 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +1100998 process_mkdir();
999 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001000 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001001 process_rmdir();
1002 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001003 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001004 process_realpath();
1005 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001006 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001007 process_stat();
1008 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001009 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001010 process_rename();
1011 break;
Damien Miller058316f2001-03-08 10:08:49 +11001012 case SSH2_FXP_READLINK:
1013 process_readlink();
1014 break;
1015 case SSH2_FXP_SYMLINK:
1016 process_symlink();
1017 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001018 case SSH2_FXP_EXTENDED:
1019 process_extended();
1020 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001021 default:
1022 error("Unknown message %d", type);
1023 break;
1024 }
1025}
1026
1027int
1028main(int ac, char **av)
1029{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001030 fd_set *rset, *wset;
Damien Miller7b28dc52000-09-05 13:34:53 +11001031 int in, out, max;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001032 ssize_t len, olen, set_size;
Damien Miller7b28dc52000-09-05 13:34:53 +11001033
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001034 /* XXX should use getopt */
1035
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001036 __progname = get_progname(av[0]);
Damien Miller7b28dc52000-09-05 13:34:53 +11001037 handle_init();
1038
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001039#ifdef DEBUG_SFTP_SERVER
Kevin Stevesef4eea92001-02-05 12:42:17 +00001040 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001041#endif
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001042
Damien Miller7b28dc52000-09-05 13:34:53 +11001043 in = dup(STDIN_FILENO);
1044 out = dup(STDOUT_FILENO);
1045
1046 max = 0;
1047 if (in > max)
1048 max = in;
1049 if (out > max)
1050 max = out;
1051
1052 buffer_init(&iqueue);
1053 buffer_init(&oqueue);
1054
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001055 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1056 rset = (fd_set *)xmalloc(set_size);
1057 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001058
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001059 for (;;) {
1060 memset(rset, 0, set_size);
1061 memset(wset, 0, set_size);
1062
1063 FD_SET(in, rset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001064 olen = buffer_len(&oqueue);
1065 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001066 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001067
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001068 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001069 if (errno == EINTR)
1070 continue;
1071 exit(2);
1072 }
1073
1074 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001075 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001076 char buf[4*4096];
1077 len = read(in, buf, sizeof buf);
1078 if (len == 0) {
1079 debug("read eof");
1080 exit(0);
1081 } else if (len < 0) {
1082 error("read error");
1083 exit(1);
1084 } else {
1085 buffer_append(&iqueue, buf, len);
1086 }
1087 }
1088 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001089 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001090 len = write(out, buffer_ptr(&oqueue), olen);
1091 if (len < 0) {
1092 error("write error");
1093 exit(1);
1094 } else {
1095 buffer_consume(&oqueue, len);
1096 }
1097 }
1098 /* process requests from client */
1099 process();
1100 }
1101}