10Sstevel@tonic-gate /*
25087Sjp161948 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
35087Sjp161948 *
45087Sjp161948 * Permission to use, copy, modify, and distribute this software for any
55087Sjp161948 * purpose with or without fee is hereby granted, provided that the above
65087Sjp161948 * copyright notice and this permission notice appear in all copies.
75087Sjp161948 *
85087Sjp161948 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
95087Sjp161948 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
105087Sjp161948 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
115087Sjp161948 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
125087Sjp161948 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
135087Sjp161948 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
145087Sjp161948 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
150Sstevel@tonic-gate */
160Sstevel@tonic-gate
175087Sjp161948 /* $OpenBSD: sftp-server.c,v 1.71 2007/01/03 07:22:36 stevesk Exp $ */
180Sstevel@tonic-gate
195087Sjp161948 #include "includes.h"
205087Sjp161948
215087Sjp161948 #include <sys/types.h>
225087Sjp161948 #include <sys/param.h>
235087Sjp161948 #include <sys/stat.h>
245087Sjp161948 #ifdef HAVE_SYS_TIME_H
255087Sjp161948 # include <sys/time.h>
265087Sjp161948 #endif
275087Sjp161948
285087Sjp161948 #include <dirent.h>
295087Sjp161948 #include <errno.h>
305087Sjp161948 #include <fcntl.h>
315087Sjp161948 #include <pwd.h>
325087Sjp161948 #include <stdlib.h>
335087Sjp161948 #include <stdio.h>
345087Sjp161948 #include <string.h>
355087Sjp161948 #include <pwd.h>
365087Sjp161948 #include <time.h>
375087Sjp161948 #include <unistd.h>
385087Sjp161948 #include <stdarg.h>
395087Sjp161948
405087Sjp161948 #include "xmalloc.h"
410Sstevel@tonic-gate #include "buffer.h"
420Sstevel@tonic-gate #include "bufaux.h"
430Sstevel@tonic-gate #include "log.h"
445087Sjp161948 #include "misc.h"
455087Sjp161948 #include "uidswap.h"
460Sstevel@tonic-gate
470Sstevel@tonic-gate #include "sftp.h"
480Sstevel@tonic-gate #include "sftp-common.h"
490Sstevel@tonic-gate
500Sstevel@tonic-gate #ifdef HAVE___PROGNAME
510Sstevel@tonic-gate extern char *__progname;
520Sstevel@tonic-gate #else
530Sstevel@tonic-gate char *__progname;
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate
565087Sjp161948 /* helper */
575087Sjp161948 #define get_int64() buffer_get_int64(&iqueue);
585087Sjp161948 #define get_int() buffer_get_int(&iqueue);
595087Sjp161948 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
605087Sjp161948
61*9139SJan.Pechanec@Sun.COM void cleanup_exit(int i);
625087Sjp161948
635087Sjp161948 /* Our verbosity */
645087Sjp161948 LogLevel log_level = SYSLOG_LEVEL_ERROR;
655087Sjp161948
665087Sjp161948 /* Our client */
675087Sjp161948 struct passwd *pw = NULL;
685087Sjp161948 char *client_addr = NULL;
695087Sjp161948
700Sstevel@tonic-gate /* input and output queue */
710Sstevel@tonic-gate Buffer iqueue;
720Sstevel@tonic-gate Buffer oqueue;
730Sstevel@tonic-gate
740Sstevel@tonic-gate /* Version of client */
750Sstevel@tonic-gate int version;
760Sstevel@tonic-gate
775087Sjp161948 /* portable attributes, etc. */
780Sstevel@tonic-gate
790Sstevel@tonic-gate typedef struct Stat Stat;
800Sstevel@tonic-gate
810Sstevel@tonic-gate struct Stat {
820Sstevel@tonic-gate char *name;
830Sstevel@tonic-gate char *long_name;
840Sstevel@tonic-gate Attrib attrib;
850Sstevel@tonic-gate };
860Sstevel@tonic-gate
870Sstevel@tonic-gate static int
errno_to_portable(int unixerrno)880Sstevel@tonic-gate errno_to_portable(int unixerrno)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate int ret = 0;
910Sstevel@tonic-gate
920Sstevel@tonic-gate switch (unixerrno) {
930Sstevel@tonic-gate case 0:
940Sstevel@tonic-gate ret = SSH2_FX_OK;
950Sstevel@tonic-gate break;
960Sstevel@tonic-gate case ENOENT:
970Sstevel@tonic-gate case ENOTDIR:
980Sstevel@tonic-gate case EBADF:
990Sstevel@tonic-gate case ELOOP:
1000Sstevel@tonic-gate ret = SSH2_FX_NO_SUCH_FILE;
1010Sstevel@tonic-gate break;
1020Sstevel@tonic-gate case EPERM:
1030Sstevel@tonic-gate case EACCES:
1040Sstevel@tonic-gate case EFAULT:
1050Sstevel@tonic-gate ret = SSH2_FX_PERMISSION_DENIED;
1060Sstevel@tonic-gate break;
1070Sstevel@tonic-gate case ENAMETOOLONG:
1080Sstevel@tonic-gate case EINVAL:
1090Sstevel@tonic-gate ret = SSH2_FX_BAD_MESSAGE;
1100Sstevel@tonic-gate break;
1110Sstevel@tonic-gate default:
1120Sstevel@tonic-gate ret = SSH2_FX_FAILURE;
1130Sstevel@tonic-gate break;
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate return ret;
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate static int
flags_from_portable(int pflags)1190Sstevel@tonic-gate flags_from_portable(int pflags)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate int flags = 0;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if ((pflags & SSH2_FXF_READ) &&
1240Sstevel@tonic-gate (pflags & SSH2_FXF_WRITE)) {
1250Sstevel@tonic-gate flags = O_RDWR;
1260Sstevel@tonic-gate } else if (pflags & SSH2_FXF_READ) {
1270Sstevel@tonic-gate flags = O_RDONLY;
1280Sstevel@tonic-gate } else if (pflags & SSH2_FXF_WRITE) {
1290Sstevel@tonic-gate flags = O_WRONLY;
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate if (pflags & SSH2_FXF_CREAT)
1320Sstevel@tonic-gate flags |= O_CREAT;
1330Sstevel@tonic-gate if (pflags & SSH2_FXF_TRUNC)
1340Sstevel@tonic-gate flags |= O_TRUNC;
1350Sstevel@tonic-gate if (pflags & SSH2_FXF_EXCL)
1360Sstevel@tonic-gate flags |= O_EXCL;
1370Sstevel@tonic-gate return flags;
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1405087Sjp161948 static const char *
string_from_portable(int pflags)1415087Sjp161948 string_from_portable(int pflags)
1425087Sjp161948 {
1435087Sjp161948 static char ret[128];
1445087Sjp161948
1455087Sjp161948 *ret = '\0';
1465087Sjp161948
1475087Sjp161948 #define PAPPEND(str) { \
1485087Sjp161948 if (*ret != '\0') \
1495087Sjp161948 strlcat(ret, ",", sizeof(ret)); \
1505087Sjp161948 strlcat(ret, str, sizeof(ret)); \
1515087Sjp161948 }
1525087Sjp161948
1535087Sjp161948 if (pflags & SSH2_FXF_READ)
1545087Sjp161948 PAPPEND("READ")
1555087Sjp161948 if (pflags & SSH2_FXF_WRITE)
1565087Sjp161948 PAPPEND("WRITE")
1575087Sjp161948 if (pflags & SSH2_FXF_CREAT)
1585087Sjp161948 PAPPEND("CREATE")
1595087Sjp161948 if (pflags & SSH2_FXF_TRUNC)
1605087Sjp161948 PAPPEND("TRUNCATE")
1615087Sjp161948 if (pflags & SSH2_FXF_EXCL)
1625087Sjp161948 PAPPEND("EXCL")
1635087Sjp161948
1645087Sjp161948 return ret;
1655087Sjp161948 }
1665087Sjp161948
1670Sstevel@tonic-gate static Attrib *
get_attrib(void)1680Sstevel@tonic-gate get_attrib(void)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate return decode_attrib(&iqueue);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /* handle handles */
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate typedef struct Handle Handle;
1760Sstevel@tonic-gate struct Handle {
1770Sstevel@tonic-gate int use;
1780Sstevel@tonic-gate DIR *dirp;
1790Sstevel@tonic-gate int fd;
1800Sstevel@tonic-gate char *name;
1815087Sjp161948 u_int64_t bytes_read, bytes_write;
1820Sstevel@tonic-gate };
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate enum {
1850Sstevel@tonic-gate HANDLE_UNUSED,
1860Sstevel@tonic-gate HANDLE_DIR,
1870Sstevel@tonic-gate HANDLE_FILE
1880Sstevel@tonic-gate };
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate Handle handles[100];
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate static void
handle_init(void)1930Sstevel@tonic-gate handle_init(void)
1940Sstevel@tonic-gate {
1955087Sjp161948 u_int i;
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
1980Sstevel@tonic-gate handles[i].use = HANDLE_UNUSED;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate static int
handle_new(int use,const char * name,int fd,DIR * dirp)2025087Sjp161948 handle_new(int use, const char *name, int fd, DIR *dirp)
2030Sstevel@tonic-gate {
2045087Sjp161948 u_int i;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
2070Sstevel@tonic-gate if (handles[i].use == HANDLE_UNUSED) {
2080Sstevel@tonic-gate handles[i].use = use;
2090Sstevel@tonic-gate handles[i].dirp = dirp;
2100Sstevel@tonic-gate handles[i].fd = fd;
2115087Sjp161948 handles[i].name = xstrdup(name);
2125087Sjp161948 handles[i].bytes_read = handles[i].bytes_write = 0;
2130Sstevel@tonic-gate return i;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate return -1;
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate static int
handle_is_ok(int i,int type)2200Sstevel@tonic-gate handle_is_ok(int i, int type)
2210Sstevel@tonic-gate {
2225087Sjp161948 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
2230Sstevel@tonic-gate handles[i].use == type;
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate static int
handle_to_string(int handle,char ** stringp,int * hlenp)2270Sstevel@tonic-gate handle_to_string(int handle, char **stringp, int *hlenp)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate if (stringp == NULL || hlenp == NULL)
2300Sstevel@tonic-gate return -1;
2310Sstevel@tonic-gate *stringp = xmalloc(sizeof(int32_t));
2325087Sjp161948 put_u32(*stringp, handle);
2330Sstevel@tonic-gate *hlenp = sizeof(int32_t);
2340Sstevel@tonic-gate return 0;
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate static int
handle_from_string(const char * handle,u_int hlen)2385087Sjp161948 handle_from_string(const char *handle, u_int hlen)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate int val;
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate if (hlen != sizeof(int32_t))
2430Sstevel@tonic-gate return -1;
2445087Sjp161948 val = get_u32(handle);
2450Sstevel@tonic-gate if (handle_is_ok(val, HANDLE_FILE) ||
2460Sstevel@tonic-gate handle_is_ok(val, HANDLE_DIR))
2470Sstevel@tonic-gate return val;
2480Sstevel@tonic-gate return -1;
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate static char *
handle_to_name(int handle)2520Sstevel@tonic-gate handle_to_name(int handle)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate if (handle_is_ok(handle, HANDLE_DIR)||
2550Sstevel@tonic-gate handle_is_ok(handle, HANDLE_FILE))
2560Sstevel@tonic-gate return handles[handle].name;
2570Sstevel@tonic-gate return NULL;
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate static DIR *
handle_to_dir(int handle)2610Sstevel@tonic-gate handle_to_dir(int handle)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate if (handle_is_ok(handle, HANDLE_DIR))
2640Sstevel@tonic-gate return handles[handle].dirp;
2650Sstevel@tonic-gate return NULL;
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate static int
handle_to_fd(int handle)2690Sstevel@tonic-gate handle_to_fd(int handle)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate if (handle_is_ok(handle, HANDLE_FILE))
2720Sstevel@tonic-gate return handles[handle].fd;
2730Sstevel@tonic-gate return -1;
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate
2765087Sjp161948 static void
handle_update_read(int handle,ssize_t bytes)2775087Sjp161948 handle_update_read(int handle, ssize_t bytes)
2785087Sjp161948 {
2795087Sjp161948 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
2805087Sjp161948 handles[handle].bytes_read += bytes;
2815087Sjp161948 }
2825087Sjp161948
2835087Sjp161948 static void
handle_update_write(int handle,ssize_t bytes)2845087Sjp161948 handle_update_write(int handle, ssize_t bytes)
2855087Sjp161948 {
2865087Sjp161948 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
2875087Sjp161948 handles[handle].bytes_write += bytes;
2885087Sjp161948 }
2895087Sjp161948
2905087Sjp161948 static u_int64_t
handle_bytes_read(int handle)2915087Sjp161948 handle_bytes_read(int handle)
2925087Sjp161948 {
2935087Sjp161948 if (handle_is_ok(handle, HANDLE_FILE))
2945087Sjp161948 return (handles[handle].bytes_read);
2955087Sjp161948 return 0;
2965087Sjp161948 }
2975087Sjp161948
2985087Sjp161948 static u_int64_t
handle_bytes_write(int handle)2995087Sjp161948 handle_bytes_write(int handle)
3005087Sjp161948 {
3015087Sjp161948 if (handle_is_ok(handle, HANDLE_FILE))
3025087Sjp161948 return (handles[handle].bytes_write);
3035087Sjp161948 return 0;
3045087Sjp161948 }
3055087Sjp161948
3060Sstevel@tonic-gate static int
handle_close(int handle)3070Sstevel@tonic-gate handle_close(int handle)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate int ret = -1;
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate if (handle_is_ok(handle, HANDLE_FILE)) {
3120Sstevel@tonic-gate ret = close(handles[handle].fd);
3130Sstevel@tonic-gate handles[handle].use = HANDLE_UNUSED;
3145087Sjp161948 xfree(handles[handle].name);
3150Sstevel@tonic-gate } else if (handle_is_ok(handle, HANDLE_DIR)) {
3160Sstevel@tonic-gate ret = closedir(handles[handle].dirp);
3170Sstevel@tonic-gate handles[handle].use = HANDLE_UNUSED;
3185087Sjp161948 xfree(handles[handle].name);
3190Sstevel@tonic-gate } else {
3200Sstevel@tonic-gate errno = ENOENT;
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate return ret;
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3255087Sjp161948 static void
handle_log_close(int handle,char * emsg)3265087Sjp161948 handle_log_close(int handle, char *emsg)
3275087Sjp161948 {
3285087Sjp161948 if (handle_is_ok(handle, HANDLE_FILE)) {
3295087Sjp161948 log("%s%sclose \"%s\" bytes read %llu written %llu",
3305087Sjp161948 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
3315087Sjp161948 handle_to_name(handle),
3325087Sjp161948 (unsigned long long)handle_bytes_read(handle),
3335087Sjp161948 (unsigned long long)handle_bytes_write(handle));
3345087Sjp161948 } else {
3355087Sjp161948 log("%s%sclosedir \"%s\"",
3365087Sjp161948 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
3375087Sjp161948 handle_to_name(handle));
3385087Sjp161948 }
3395087Sjp161948 }
3405087Sjp161948
3415087Sjp161948 static void
handle_log_exit(void)3425087Sjp161948 handle_log_exit(void)
3435087Sjp161948 {
3445087Sjp161948 u_int i;
3455087Sjp161948
3465087Sjp161948 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
3475087Sjp161948 if (handles[i].use != HANDLE_UNUSED)
3485087Sjp161948 handle_log_close(i, "forced");
3495087Sjp161948 }
3505087Sjp161948
3510Sstevel@tonic-gate static int
get_handle(void)3520Sstevel@tonic-gate get_handle(void)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate char *handle;
3550Sstevel@tonic-gate int val = -1;
3560Sstevel@tonic-gate u_int hlen;
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate handle = get_string(&hlen);
3590Sstevel@tonic-gate if (hlen < 256)
3600Sstevel@tonic-gate val = handle_from_string(handle, hlen);
3610Sstevel@tonic-gate xfree(handle);
3620Sstevel@tonic-gate return val;
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate /* send replies */
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate static void
send_msg(Buffer * m)3680Sstevel@tonic-gate send_msg(Buffer *m)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate int mlen = buffer_len(m);
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate buffer_put_int(&oqueue, mlen);
3730Sstevel@tonic-gate buffer_append(&oqueue, buffer_ptr(m), mlen);
3740Sstevel@tonic-gate buffer_consume(m, mlen);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate
3775087Sjp161948 static const char *
status_to_message(u_int32_t status)3785087Sjp161948 status_to_message(u_int32_t status)
3790Sstevel@tonic-gate {
3800Sstevel@tonic-gate const char *status_messages[] = {
3810Sstevel@tonic-gate "Success", /* SSH_FX_OK */
3820Sstevel@tonic-gate "End of file", /* SSH_FX_EOF */
3830Sstevel@tonic-gate "No such file", /* SSH_FX_NO_SUCH_FILE */
3840Sstevel@tonic-gate "Permission denied", /* SSH_FX_PERMISSION_DENIED */
3850Sstevel@tonic-gate "Failure", /* SSH_FX_FAILURE */
3860Sstevel@tonic-gate "Bad message", /* SSH_FX_BAD_MESSAGE */
3870Sstevel@tonic-gate "No connection", /* SSH_FX_NO_CONNECTION */
3880Sstevel@tonic-gate "Connection lost", /* SSH_FX_CONNECTION_LOST */
3890Sstevel@tonic-gate "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
3900Sstevel@tonic-gate "Unknown error" /* Others */
3910Sstevel@tonic-gate };
3925087Sjp161948 return (status_messages[MIN(status,SSH2_FX_MAX)]);
3935087Sjp161948 }
3940Sstevel@tonic-gate
3955087Sjp161948 static void
send_status(u_int32_t id,u_int32_t status)3965087Sjp161948 send_status(u_int32_t id, u_int32_t status)
3975087Sjp161948 {
3985087Sjp161948 Buffer msg;
3995087Sjp161948
4005087Sjp161948 debug3("request %u: sent status %u", id, status);
4015087Sjp161948 if (log_level > SYSLOG_LEVEL_VERBOSE ||
4025087Sjp161948 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
4035087Sjp161948 log("sent status %s", status_to_message(status));
4040Sstevel@tonic-gate buffer_init(&msg);
4050Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_STATUS);
4060Sstevel@tonic-gate buffer_put_int(&msg, id);
4075087Sjp161948 buffer_put_int(&msg, status);
4080Sstevel@tonic-gate if (version >= 3) {
4095087Sjp161948 buffer_put_cstring(&msg, status_to_message(status));
4100Sstevel@tonic-gate buffer_put_cstring(&msg, "");
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate send_msg(&msg);
4130Sstevel@tonic-gate buffer_free(&msg);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate static void
send_data_or_handle(char type,u_int32_t id,const char * data,int dlen)4165087Sjp161948 send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate Buffer msg;
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate buffer_init(&msg);
4210Sstevel@tonic-gate buffer_put_char(&msg, type);
4220Sstevel@tonic-gate buffer_put_int(&msg, id);
4230Sstevel@tonic-gate buffer_put_string(&msg, data, dlen);
4240Sstevel@tonic-gate send_msg(&msg);
4250Sstevel@tonic-gate buffer_free(&msg);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate static void
send_data(u_int32_t id,const char * data,int dlen)4295087Sjp161948 send_data(u_int32_t id, const char *data, int dlen)
4300Sstevel@tonic-gate {
4315087Sjp161948 debug("request %u: sent data len %d", id, dlen);
4320Sstevel@tonic-gate send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate static void
send_handle(u_int32_t id,int handle)4360Sstevel@tonic-gate send_handle(u_int32_t id, int handle)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate char *string;
4390Sstevel@tonic-gate int hlen;
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate handle_to_string(handle, &string, &hlen);
4425087Sjp161948 debug("request %u: sent handle handle %d", id, handle);
4430Sstevel@tonic-gate send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
4440Sstevel@tonic-gate xfree(string);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate static void
send_names(u_int32_t id,int count,const Stat * stats)4485087Sjp161948 send_names(u_int32_t id, int count, const Stat *stats)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate Buffer msg;
4510Sstevel@tonic-gate int i;
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate buffer_init(&msg);
4540Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_NAME);
4550Sstevel@tonic-gate buffer_put_int(&msg, id);
4560Sstevel@tonic-gate buffer_put_int(&msg, count);
4575087Sjp161948 debug("request %u: sent names count %d", id, count);
4580Sstevel@tonic-gate for (i = 0; i < count; i++) {
4590Sstevel@tonic-gate buffer_put_cstring(&msg, stats[i].name);
4600Sstevel@tonic-gate buffer_put_cstring(&msg, stats[i].long_name);
4610Sstevel@tonic-gate encode_attrib(&msg, &stats[i].attrib);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate send_msg(&msg);
4640Sstevel@tonic-gate buffer_free(&msg);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate static void
send_attrib(u_int32_t id,const Attrib * a)4685087Sjp161948 send_attrib(u_int32_t id, const Attrib *a)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate Buffer msg;
4710Sstevel@tonic-gate
4725087Sjp161948 debug("request %u: sent attrib have 0x%x", id, a->flags);
4730Sstevel@tonic-gate buffer_init(&msg);
4740Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_ATTRS);
4750Sstevel@tonic-gate buffer_put_int(&msg, id);
4760Sstevel@tonic-gate encode_attrib(&msg, a);
4770Sstevel@tonic-gate send_msg(&msg);
4780Sstevel@tonic-gate buffer_free(&msg);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate /* parse incoming */
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate static void
process_init(void)4840Sstevel@tonic-gate process_init(void)
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate Buffer msg;
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate version = get_int();
4895087Sjp161948 verbose("received client version %d", version);
4900Sstevel@tonic-gate buffer_init(&msg);
4910Sstevel@tonic-gate buffer_put_char(&msg, SSH2_FXP_VERSION);
4920Sstevel@tonic-gate buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
4930Sstevel@tonic-gate send_msg(&msg);
4940Sstevel@tonic-gate buffer_free(&msg);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate static void
process_open(void)4980Sstevel@tonic-gate process_open(void)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate u_int32_t id, pflags;
5010Sstevel@tonic-gate Attrib *a;
5020Sstevel@tonic-gate char *name;
5030Sstevel@tonic-gate int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate id = get_int();
5060Sstevel@tonic-gate name = get_string(NULL);
5070Sstevel@tonic-gate pflags = get_int(); /* portable flags */
5085087Sjp161948 debug3("request %u: open flags %d", id, pflags);
5090Sstevel@tonic-gate a = get_attrib();
5100Sstevel@tonic-gate flags = flags_from_portable(pflags);
5110Sstevel@tonic-gate mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
5125087Sjp161948 log("open \"%s\" flags %s mode 0%o",
5135087Sjp161948 name, string_from_portable(pflags), mode);
5140Sstevel@tonic-gate fd = open(name, flags, mode);
5150Sstevel@tonic-gate if (fd < 0) {
5160Sstevel@tonic-gate status = errno_to_portable(errno);
5170Sstevel@tonic-gate } else {
5185087Sjp161948 handle = handle_new(HANDLE_FILE, name, fd, NULL);
5190Sstevel@tonic-gate if (handle < 0) {
5200Sstevel@tonic-gate close(fd);
5210Sstevel@tonic-gate } else {
5220Sstevel@tonic-gate send_handle(id, handle);
5230Sstevel@tonic-gate status = SSH2_FX_OK;
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate if (status != SSH2_FX_OK)
5270Sstevel@tonic-gate send_status(id, status);
5280Sstevel@tonic-gate xfree(name);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate static void
process_close(void)5320Sstevel@tonic-gate process_close(void)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate u_int32_t id;
5350Sstevel@tonic-gate int handle, ret, status = SSH2_FX_FAILURE;
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate id = get_int();
5380Sstevel@tonic-gate handle = get_handle();
5395087Sjp161948 debug3("request %u: close handle %u", id, handle);
5405087Sjp161948 handle_log_close(handle, NULL);
5410Sstevel@tonic-gate ret = handle_close(handle);
5420Sstevel@tonic-gate status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
5430Sstevel@tonic-gate send_status(id, status);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate static void
process_read(void)5470Sstevel@tonic-gate process_read(void)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate char buf[64*1024];
5500Sstevel@tonic-gate u_int32_t id, len;
5510Sstevel@tonic-gate int handle, fd, ret, status = SSH2_FX_FAILURE;
5520Sstevel@tonic-gate u_int64_t off;
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate id = get_int();
5550Sstevel@tonic-gate handle = get_handle();
5560Sstevel@tonic-gate off = get_int64();
5570Sstevel@tonic-gate len = get_int();
5580Sstevel@tonic-gate
5595087Sjp161948 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
5605087Sjp161948 id, handle_to_name(handle), handle, (unsigned long long)off, len);
5610Sstevel@tonic-gate if (len > sizeof buf) {
5620Sstevel@tonic-gate len = sizeof buf;
5635087Sjp161948 debug2("read change len %d", len);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate fd = handle_to_fd(handle);
5660Sstevel@tonic-gate if (fd >= 0) {
5670Sstevel@tonic-gate if (lseek(fd, off, SEEK_SET) < 0) {
5680Sstevel@tonic-gate error("process_read: seek failed");
5690Sstevel@tonic-gate status = errno_to_portable(errno);
5700Sstevel@tonic-gate } else {
5710Sstevel@tonic-gate ret = read(fd, buf, len);
5720Sstevel@tonic-gate if (ret < 0) {
5730Sstevel@tonic-gate status = errno_to_portable(errno);
5740Sstevel@tonic-gate } else if (ret == 0) {
5750Sstevel@tonic-gate status = SSH2_FX_EOF;
5760Sstevel@tonic-gate } else {
5770Sstevel@tonic-gate send_data(id, buf, ret);
5780Sstevel@tonic-gate status = SSH2_FX_OK;
5795087Sjp161948 handle_update_read(handle, ret);
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate if (status != SSH2_FX_OK)
5840Sstevel@tonic-gate send_status(id, status);
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate
5870Sstevel@tonic-gate static void
process_write(void)5880Sstevel@tonic-gate process_write(void)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate u_int32_t id;
5910Sstevel@tonic-gate u_int64_t off;
5920Sstevel@tonic-gate u_int len;
5930Sstevel@tonic-gate int handle, fd, ret, status = SSH2_FX_FAILURE;
5940Sstevel@tonic-gate char *data;
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate id = get_int();
5970Sstevel@tonic-gate handle = get_handle();
5980Sstevel@tonic-gate off = get_int64();
5990Sstevel@tonic-gate data = get_string(&len);
6000Sstevel@tonic-gate
6015087Sjp161948 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
6025087Sjp161948 id, handle_to_name(handle), handle, (unsigned long long)off, len);
6030Sstevel@tonic-gate fd = handle_to_fd(handle);
6040Sstevel@tonic-gate if (fd >= 0) {
6050Sstevel@tonic-gate if (lseek(fd, off, SEEK_SET) < 0) {
6060Sstevel@tonic-gate status = errno_to_portable(errno);
6070Sstevel@tonic-gate error("process_write: seek failed");
6080Sstevel@tonic-gate } else {
6090Sstevel@tonic-gate /* XXX ATOMICIO ? */
6100Sstevel@tonic-gate ret = write(fd, data, len);
6115087Sjp161948 if (ret < 0) {
6120Sstevel@tonic-gate error("process_write: write failed");
6130Sstevel@tonic-gate status = errno_to_portable(errno);
6145087Sjp161948 } else if ((size_t)ret == len) {
6150Sstevel@tonic-gate status = SSH2_FX_OK;
6165087Sjp161948 handle_update_write(handle, ret);
6170Sstevel@tonic-gate } else {
6185087Sjp161948 debug2("nothing at all written");
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate send_status(id, status);
6230Sstevel@tonic-gate xfree(data);
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate
6260Sstevel@tonic-gate static void
process_do_stat(int do_lstat)6270Sstevel@tonic-gate process_do_stat(int do_lstat)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate Attrib a;
6300Sstevel@tonic-gate struct stat st;
6310Sstevel@tonic-gate u_int32_t id;
6320Sstevel@tonic-gate char *name;
6330Sstevel@tonic-gate int ret, status = SSH2_FX_FAILURE;
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate id = get_int();
6360Sstevel@tonic-gate name = get_string(NULL);
6375087Sjp161948 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
6385087Sjp161948 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
6390Sstevel@tonic-gate ret = do_lstat ? lstat(name, &st) : stat(name, &st);
6400Sstevel@tonic-gate if (ret < 0) {
6410Sstevel@tonic-gate status = errno_to_portable(errno);
6420Sstevel@tonic-gate } else {
6430Sstevel@tonic-gate stat_to_attrib(&st, &a);
6440Sstevel@tonic-gate send_attrib(id, &a);
6450Sstevel@tonic-gate status = SSH2_FX_OK;
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate if (status != SSH2_FX_OK)
6480Sstevel@tonic-gate send_status(id, status);
6490Sstevel@tonic-gate xfree(name);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate static void
process_stat(void)6530Sstevel@tonic-gate process_stat(void)
6540Sstevel@tonic-gate {
6550Sstevel@tonic-gate process_do_stat(0);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate static void
process_lstat(void)6590Sstevel@tonic-gate process_lstat(void)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate process_do_stat(1);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate static void
process_fstat(void)6650Sstevel@tonic-gate process_fstat(void)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate Attrib a;
6680Sstevel@tonic-gate struct stat st;
6690Sstevel@tonic-gate u_int32_t id;
6700Sstevel@tonic-gate int fd, ret, handle, status = SSH2_FX_FAILURE;
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate id = get_int();
6730Sstevel@tonic-gate handle = get_handle();
6745087Sjp161948 debug("request %u: fstat \"%s\" (handle %u)",
6755087Sjp161948 id, handle_to_name(handle), handle);
6760Sstevel@tonic-gate fd = handle_to_fd(handle);
6775087Sjp161948 if (fd >= 0) {
6780Sstevel@tonic-gate ret = fstat(fd, &st);
6790Sstevel@tonic-gate if (ret < 0) {
6800Sstevel@tonic-gate status = errno_to_portable(errno);
6810Sstevel@tonic-gate } else {
6820Sstevel@tonic-gate stat_to_attrib(&st, &a);
6830Sstevel@tonic-gate send_attrib(id, &a);
6840Sstevel@tonic-gate status = SSH2_FX_OK;
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate if (status != SSH2_FX_OK)
6880Sstevel@tonic-gate send_status(id, status);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate static struct timeval *
attrib_to_tv(const Attrib * a)6925087Sjp161948 attrib_to_tv(const Attrib *a)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate static struct timeval tv[2];
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate tv[0].tv_sec = a->atime;
6970Sstevel@tonic-gate tv[0].tv_usec = 0;
6980Sstevel@tonic-gate tv[1].tv_sec = a->mtime;
6990Sstevel@tonic-gate tv[1].tv_usec = 0;
7000Sstevel@tonic-gate return tv;
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate
7030Sstevel@tonic-gate static void
process_setstat(void)7040Sstevel@tonic-gate process_setstat(void)
7050Sstevel@tonic-gate {
7060Sstevel@tonic-gate Attrib *a;
7070Sstevel@tonic-gate u_int32_t id;
7080Sstevel@tonic-gate char *name;
7090Sstevel@tonic-gate int status = SSH2_FX_OK, ret;
7100Sstevel@tonic-gate
7110Sstevel@tonic-gate id = get_int();
7120Sstevel@tonic-gate name = get_string(NULL);
7130Sstevel@tonic-gate a = get_attrib();
7145087Sjp161948 debug("request %u: setstat name \"%s\"", id, name);
7150Sstevel@tonic-gate if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
7165087Sjp161948 log("set \"%s\" size %llu",
7175087Sjp161948 name, (unsigned long long)a->size);
7180Sstevel@tonic-gate ret = truncate(name, a->size);
7190Sstevel@tonic-gate if (ret == -1)
7200Sstevel@tonic-gate status = errno_to_portable(errno);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
7235087Sjp161948 log("set \"%s\" mode %04o", name, a->perm);
7248304SVladimir.Kotal@Sun.COM ret = chmod(name, a->perm & 07777);
7250Sstevel@tonic-gate if (ret == -1)
7260Sstevel@tonic-gate status = errno_to_portable(errno);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
7295087Sjp161948 char buf[64];
7305087Sjp161948 time_t t = a->mtime;
7315087Sjp161948
7325087Sjp161948 strftime(buf, sizeof(buf), "%Y" "%m%d-%H:%M:%S",
7335087Sjp161948 localtime(&t));
7345087Sjp161948 log("set \"%s\" modtime %s", name, buf);
7350Sstevel@tonic-gate ret = utimes(name, attrib_to_tv(a));
7360Sstevel@tonic-gate if (ret == -1)
7370Sstevel@tonic-gate status = errno_to_portable(errno);
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
7405087Sjp161948 log("set \"%s\" owner %lu group %lu", name,
7415087Sjp161948 (u_long)a->uid, (u_long)a->gid);
7420Sstevel@tonic-gate ret = chown(name, a->uid, a->gid);
7430Sstevel@tonic-gate if (ret == -1)
7440Sstevel@tonic-gate status = errno_to_portable(errno);
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate send_status(id, status);
7470Sstevel@tonic-gate xfree(name);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate static void
process_fsetstat(void)7510Sstevel@tonic-gate process_fsetstat(void)
7520Sstevel@tonic-gate {
7530Sstevel@tonic-gate Attrib *a;
7540Sstevel@tonic-gate u_int32_t id;
7550Sstevel@tonic-gate int handle, fd, ret;
7560Sstevel@tonic-gate int status = SSH2_FX_OK;
7570Sstevel@tonic-gate
7580Sstevel@tonic-gate id = get_int();
7590Sstevel@tonic-gate handle = get_handle();
7600Sstevel@tonic-gate a = get_attrib();
7615087Sjp161948 debug("request %u: fsetstat handle %d", id, handle);
7620Sstevel@tonic-gate fd = handle_to_fd(handle);
7635087Sjp161948 if (fd < 0) {
7640Sstevel@tonic-gate status = SSH2_FX_FAILURE;
7650Sstevel@tonic-gate } else {
7665087Sjp161948 char *name = handle_to_name(handle);
7675087Sjp161948
7680Sstevel@tonic-gate if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
7695087Sjp161948 log("set \"%s\" size %llu",
7705087Sjp161948 name, (unsigned long long)a->size);
7710Sstevel@tonic-gate ret = ftruncate(fd, a->size);
7720Sstevel@tonic-gate if (ret == -1)
7730Sstevel@tonic-gate status = errno_to_portable(errno);
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
7765087Sjp161948 log("set \"%s\" mode %04o", name, a->perm);
7770Sstevel@tonic-gate #ifdef HAVE_FCHMOD
7788304SVladimir.Kotal@Sun.COM ret = fchmod(fd, a->perm & 07777);
7790Sstevel@tonic-gate #else
7808304SVladimir.Kotal@Sun.COM ret = chmod(name, a->perm & 07777);
7810Sstevel@tonic-gate #endif
7820Sstevel@tonic-gate if (ret == -1)
7830Sstevel@tonic-gate status = errno_to_portable(errno);
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
7865087Sjp161948 char buf[64];
7875087Sjp161948 time_t t = a->mtime;
7885087Sjp161948
7895087Sjp161948 strftime(buf, sizeof(buf), "%Y" "%m%d-%H:%M:%S",
7905087Sjp161948 localtime(&t));
7915087Sjp161948 log("set \"%s\" modtime %s", name, buf);
7920Sstevel@tonic-gate #ifdef HAVE_FUTIMES
7930Sstevel@tonic-gate ret = futimes(fd, attrib_to_tv(a));
7940Sstevel@tonic-gate #else
7950Sstevel@tonic-gate ret = utimes(name, attrib_to_tv(a));
7960Sstevel@tonic-gate #endif
7970Sstevel@tonic-gate if (ret == -1)
7980Sstevel@tonic-gate status = errno_to_portable(errno);
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
8015087Sjp161948 log("set \"%s\" owner %lu group %lu", name,
8025087Sjp161948 (u_long)a->uid, (u_long)a->gid);
8030Sstevel@tonic-gate #ifdef HAVE_FCHOWN
8040Sstevel@tonic-gate ret = fchown(fd, a->uid, a->gid);
8050Sstevel@tonic-gate #else
8060Sstevel@tonic-gate ret = chown(name, a->uid, a->gid);
8070Sstevel@tonic-gate #endif
8080Sstevel@tonic-gate if (ret == -1)
8090Sstevel@tonic-gate status = errno_to_portable(errno);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate }
8120Sstevel@tonic-gate send_status(id, status);
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate
8150Sstevel@tonic-gate static void
process_opendir(void)8160Sstevel@tonic-gate process_opendir(void)
8170Sstevel@tonic-gate {
8180Sstevel@tonic-gate DIR *dirp = NULL;
8190Sstevel@tonic-gate char *path;
8200Sstevel@tonic-gate int handle, status = SSH2_FX_FAILURE;
8210Sstevel@tonic-gate u_int32_t id;
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate id = get_int();
8240Sstevel@tonic-gate path = get_string(NULL);
8255087Sjp161948 debug3("request %u: opendir", id);
8265087Sjp161948 log("opendir \"%s\"", path);
8270Sstevel@tonic-gate dirp = opendir(path);
8280Sstevel@tonic-gate if (dirp == NULL) {
8290Sstevel@tonic-gate status = errno_to_portable(errno);
8300Sstevel@tonic-gate } else {
8315087Sjp161948 handle = handle_new(HANDLE_DIR, path, 0, dirp);
8320Sstevel@tonic-gate if (handle < 0) {
8330Sstevel@tonic-gate closedir(dirp);
8340Sstevel@tonic-gate } else {
8350Sstevel@tonic-gate send_handle(id, handle);
8360Sstevel@tonic-gate status = SSH2_FX_OK;
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate if (status != SSH2_FX_OK)
8410Sstevel@tonic-gate send_status(id, status);
8420Sstevel@tonic-gate xfree(path);
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate static void
process_readdir(void)8460Sstevel@tonic-gate process_readdir(void)
8470Sstevel@tonic-gate {
8480Sstevel@tonic-gate DIR *dirp;
8490Sstevel@tonic-gate struct dirent *dp;
8500Sstevel@tonic-gate char *path;
8510Sstevel@tonic-gate int handle;
8520Sstevel@tonic-gate u_int32_t id;
8530Sstevel@tonic-gate
8540Sstevel@tonic-gate id = get_int();
8550Sstevel@tonic-gate handle = get_handle();
8565087Sjp161948 debug("request %u: readdir \"%s\" (handle %d)", id,
8575087Sjp161948 handle_to_name(handle), handle);
8580Sstevel@tonic-gate dirp = handle_to_dir(handle);
8590Sstevel@tonic-gate path = handle_to_name(handle);
8600Sstevel@tonic-gate if (dirp == NULL || path == NULL) {
8610Sstevel@tonic-gate send_status(id, SSH2_FX_FAILURE);
8620Sstevel@tonic-gate } else {
8630Sstevel@tonic-gate struct stat st;
8645087Sjp161948 char pathname[MAXPATHLEN];
8650Sstevel@tonic-gate Stat *stats;
8660Sstevel@tonic-gate int nstats = 10, count = 0, i;
8670Sstevel@tonic-gate
8685087Sjp161948 stats = xcalloc(nstats, sizeof(Stat));
8690Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) {
8700Sstevel@tonic-gate if (count >= nstats) {
8710Sstevel@tonic-gate nstats *= 2;
8720Sstevel@tonic-gate stats = xrealloc(stats, nstats * sizeof(Stat));
8730Sstevel@tonic-gate }
8740Sstevel@tonic-gate /* XXX OVERFLOW ? */
8750Sstevel@tonic-gate snprintf(pathname, sizeof pathname, "%s%s%s", path,
8760Sstevel@tonic-gate strcmp(path, "/") ? "/" : "", dp->d_name);
8770Sstevel@tonic-gate if (lstat(pathname, &st) < 0)
8780Sstevel@tonic-gate continue;
8790Sstevel@tonic-gate stat_to_attrib(&st, &(stats[count].attrib));
8800Sstevel@tonic-gate stats[count].name = xstrdup(dp->d_name);
8810Sstevel@tonic-gate stats[count].long_name = ls_file(dp->d_name, &st, 0);
8820Sstevel@tonic-gate count++;
8830Sstevel@tonic-gate /* send up to 100 entries in one message */
8840Sstevel@tonic-gate /* XXX check packet size instead */
8850Sstevel@tonic-gate if (count == 100)
8860Sstevel@tonic-gate break;
8870Sstevel@tonic-gate }
8880Sstevel@tonic-gate if (count > 0) {
8890Sstevel@tonic-gate send_names(id, count, stats);
8900Sstevel@tonic-gate for (i = 0; i < count; i++) {
8910Sstevel@tonic-gate xfree(stats[i].name);
8920Sstevel@tonic-gate xfree(stats[i].long_name);
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate } else {
8950Sstevel@tonic-gate send_status(id, SSH2_FX_EOF);
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate xfree(stats);
8980Sstevel@tonic-gate }
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate
9010Sstevel@tonic-gate static void
process_remove(void)9020Sstevel@tonic-gate process_remove(void)
9030Sstevel@tonic-gate {
9040Sstevel@tonic-gate char *name;
9050Sstevel@tonic-gate u_int32_t id;
9060Sstevel@tonic-gate int status = SSH2_FX_FAILURE;
9070Sstevel@tonic-gate int ret;
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate id = get_int();
9100Sstevel@tonic-gate name = get_string(NULL);
9115087Sjp161948 debug3("request %u: remove", id);
9125087Sjp161948 log("remove name \"%s\"", name);
9130Sstevel@tonic-gate ret = unlink(name);
9140Sstevel@tonic-gate status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
9150Sstevel@tonic-gate send_status(id, status);
9160Sstevel@tonic-gate xfree(name);
9170Sstevel@tonic-gate }
9180Sstevel@tonic-gate
9190Sstevel@tonic-gate static void
process_mkdir(void)9200Sstevel@tonic-gate process_mkdir(void)
9210Sstevel@tonic-gate {
9220Sstevel@tonic-gate Attrib *a;
9230Sstevel@tonic-gate u_int32_t id;
9240Sstevel@tonic-gate char *name;
9250Sstevel@tonic-gate int ret, mode, status = SSH2_FX_FAILURE;
9260Sstevel@tonic-gate
9270Sstevel@tonic-gate id = get_int();
9280Sstevel@tonic-gate name = get_string(NULL);
9290Sstevel@tonic-gate a = get_attrib();
9300Sstevel@tonic-gate mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
9310Sstevel@tonic-gate a->perm & 0777 : 0777;
9325087Sjp161948 debug3("request %u: mkdir", id);
9335087Sjp161948 log("mkdir name \"%s\" mode 0%o", name, mode);
9340Sstevel@tonic-gate ret = mkdir(name, mode);
9350Sstevel@tonic-gate status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
9360Sstevel@tonic-gate send_status(id, status);
9370Sstevel@tonic-gate xfree(name);
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate
9400Sstevel@tonic-gate static void
process_rmdir(void)9410Sstevel@tonic-gate process_rmdir(void)
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate u_int32_t id;
9440Sstevel@tonic-gate char *name;
9450Sstevel@tonic-gate int ret, status;
9460Sstevel@tonic-gate
9470Sstevel@tonic-gate id = get_int();
9480Sstevel@tonic-gate name = get_string(NULL);
9495087Sjp161948 debug3("request %u: rmdir", id);
9505087Sjp161948 log("rmdir name \"%s\"", name);
9510Sstevel@tonic-gate ret = rmdir(name);
9520Sstevel@tonic-gate status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
9530Sstevel@tonic-gate send_status(id, status);
9540Sstevel@tonic-gate xfree(name);
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate static void
process_realpath(void)9580Sstevel@tonic-gate process_realpath(void)
9590Sstevel@tonic-gate {
9600Sstevel@tonic-gate char resolvedname[MAXPATHLEN];
9610Sstevel@tonic-gate u_int32_t id;
9620Sstevel@tonic-gate char *path;
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate id = get_int();
9650Sstevel@tonic-gate path = get_string(NULL);
9660Sstevel@tonic-gate if (path[0] == '\0') {
9670Sstevel@tonic-gate xfree(path);
9680Sstevel@tonic-gate path = xstrdup(".");
9690Sstevel@tonic-gate }
9705087Sjp161948 debug3("request %u: realpath", id);
9715087Sjp161948 verbose("realpath \"%s\"", path);
9720Sstevel@tonic-gate if (realpath(path, resolvedname) == NULL) {
9730Sstevel@tonic-gate send_status(id, errno_to_portable(errno));
9740Sstevel@tonic-gate } else {
9750Sstevel@tonic-gate Stat s;
9760Sstevel@tonic-gate attrib_clear(&s.attrib);
9770Sstevel@tonic-gate s.name = s.long_name = resolvedname;
9780Sstevel@tonic-gate send_names(id, 1, &s);
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate xfree(path);
9810Sstevel@tonic-gate }
9820Sstevel@tonic-gate
9830Sstevel@tonic-gate static void
process_rename(void)9840Sstevel@tonic-gate process_rename(void)
9850Sstevel@tonic-gate {
9860Sstevel@tonic-gate u_int32_t id;
9870Sstevel@tonic-gate char *oldpath, *newpath;
9885087Sjp161948 int status;
9895087Sjp161948 struct stat sb;
9900Sstevel@tonic-gate
9910Sstevel@tonic-gate id = get_int();
9920Sstevel@tonic-gate oldpath = get_string(NULL);
9930Sstevel@tonic-gate newpath = get_string(NULL);
9945087Sjp161948 debug3("request %u: rename", id);
9955087Sjp161948 log("rename old \"%s\" new \"%s\"", oldpath, newpath);
9965087Sjp161948 status = SSH2_FX_FAILURE;
9975087Sjp161948 if (lstat(oldpath, &sb) == -1)
9985087Sjp161948 status = errno_to_portable(errno);
9995087Sjp161948 else if (S_ISREG(sb.st_mode)) {
10005087Sjp161948 /* Race-free rename of regular files */
10015087Sjp161948 if (link(oldpath, newpath) == -1) {
10025087Sjp161948 if (errno == EOPNOTSUPP
10035087Sjp161948 #ifdef LINK_OPNOTSUPP_ERRNO
10045087Sjp161948 || errno == LINK_OPNOTSUPP_ERRNO
10055087Sjp161948 #endif
10065087Sjp161948 ) {
10075087Sjp161948 struct stat st;
10085087Sjp161948
10095087Sjp161948 /*
10105087Sjp161948 * fs doesn't support links, so fall back to
10115087Sjp161948 * stat+rename. This is racy.
10125087Sjp161948 */
10135087Sjp161948 if (stat(newpath, &st) == -1) {
10145087Sjp161948 if (rename(oldpath, newpath) == -1)
10155087Sjp161948 status =
10165087Sjp161948 errno_to_portable(errno);
10175087Sjp161948 else
10185087Sjp161948 status = SSH2_FX_OK;
10195087Sjp161948 }
10205087Sjp161948 } else {
10215087Sjp161948 status = errno_to_portable(errno);
10225087Sjp161948 }
10235087Sjp161948 } else if (unlink(oldpath) == -1) {
10245087Sjp161948 status = errno_to_portable(errno);
10255087Sjp161948 /* clean spare link */
10265087Sjp161948 unlink(newpath);
10275087Sjp161948 } else
10285087Sjp161948 status = SSH2_FX_OK;
10295087Sjp161948 } else if (stat(newpath, &sb) == -1) {
10305087Sjp161948 if (rename(oldpath, newpath) == -1)
10315087Sjp161948 status = errno_to_portable(errno);
10325087Sjp161948 else
10335087Sjp161948 status = SSH2_FX_OK;
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate send_status(id, status);
10360Sstevel@tonic-gate xfree(oldpath);
10370Sstevel@tonic-gate xfree(newpath);
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate static void
process_readlink(void)10410Sstevel@tonic-gate process_readlink(void)
10420Sstevel@tonic-gate {
10430Sstevel@tonic-gate u_int32_t id;
10440Sstevel@tonic-gate int len;
10455087Sjp161948 char buf[MAXPATHLEN];
10460Sstevel@tonic-gate char *path;
10470Sstevel@tonic-gate
10480Sstevel@tonic-gate id = get_int();
10490Sstevel@tonic-gate path = get_string(NULL);
10505087Sjp161948 debug3("request %u: readlink", id);
10515087Sjp161948 verbose("readlink \"%s\"", path);
10525087Sjp161948 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
10530Sstevel@tonic-gate send_status(id, errno_to_portable(errno));
10540Sstevel@tonic-gate else {
10550Sstevel@tonic-gate Stat s;
10560Sstevel@tonic-gate
10575087Sjp161948 buf[len] = '\0';
10580Sstevel@tonic-gate attrib_clear(&s.attrib);
10595087Sjp161948 s.name = s.long_name = buf;
10600Sstevel@tonic-gate send_names(id, 1, &s);
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate xfree(path);
10630Sstevel@tonic-gate }
10640Sstevel@tonic-gate
10650Sstevel@tonic-gate static void
process_symlink(void)10660Sstevel@tonic-gate process_symlink(void)
10670Sstevel@tonic-gate {
10680Sstevel@tonic-gate u_int32_t id;
10690Sstevel@tonic-gate char *oldpath, *newpath;
10705087Sjp161948 int ret, status;
10710Sstevel@tonic-gate
10720Sstevel@tonic-gate id = get_int();
10730Sstevel@tonic-gate oldpath = get_string(NULL);
10740Sstevel@tonic-gate newpath = get_string(NULL);
10755087Sjp161948 debug3("request %u: symlink", id);
10765087Sjp161948 log("symlink old \"%s\" new \"%s\"", oldpath, newpath);
10775087Sjp161948 /* this will fail if 'newpath' exists */
10785087Sjp161948 ret = symlink(oldpath, newpath);
10795087Sjp161948 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
10800Sstevel@tonic-gate send_status(id, status);
10810Sstevel@tonic-gate xfree(oldpath);
10820Sstevel@tonic-gate xfree(newpath);
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate static void
process_extended(void)10860Sstevel@tonic-gate process_extended(void)
10870Sstevel@tonic-gate {
10880Sstevel@tonic-gate u_int32_t id;
10890Sstevel@tonic-gate char *request;
10900Sstevel@tonic-gate
10910Sstevel@tonic-gate id = get_int();
10920Sstevel@tonic-gate request = get_string(NULL);
10930Sstevel@tonic-gate send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
10940Sstevel@tonic-gate xfree(request);
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate
10970Sstevel@tonic-gate /* stolen from ssh-agent */
10980Sstevel@tonic-gate
10990Sstevel@tonic-gate static void
process(void)11000Sstevel@tonic-gate process(void)
11010Sstevel@tonic-gate {
11020Sstevel@tonic-gate u_int msg_len;
11030Sstevel@tonic-gate u_int buf_len;
11040Sstevel@tonic-gate u_int consumed;
11050Sstevel@tonic-gate u_int type;
11060Sstevel@tonic-gate u_char *cp;
11070Sstevel@tonic-gate
11080Sstevel@tonic-gate buf_len = buffer_len(&iqueue);
11090Sstevel@tonic-gate if (buf_len < 5)
11100Sstevel@tonic-gate return; /* Incomplete message. */
11110Sstevel@tonic-gate cp = buffer_ptr(&iqueue);
11125087Sjp161948 msg_len = get_u32(cp);
11135087Sjp161948 if (msg_len > SFTP_MAX_MSG_LENGTH) {
11145087Sjp161948 error("bad message from %s local user %s",
11155087Sjp161948 client_addr, pw->pw_name);
11165087Sjp161948 cleanup_exit(11);
11170Sstevel@tonic-gate }
11180Sstevel@tonic-gate if (buf_len < msg_len + 4)
11190Sstevel@tonic-gate return;
11200Sstevel@tonic-gate buffer_consume(&iqueue, 4);
11210Sstevel@tonic-gate buf_len -= 4;
11220Sstevel@tonic-gate type = buffer_get_char(&iqueue);
11230Sstevel@tonic-gate switch (type) {
11240Sstevel@tonic-gate case SSH2_FXP_INIT:
11250Sstevel@tonic-gate process_init();
11260Sstevel@tonic-gate break;
11270Sstevel@tonic-gate case SSH2_FXP_OPEN:
11280Sstevel@tonic-gate process_open();
11290Sstevel@tonic-gate break;
11300Sstevel@tonic-gate case SSH2_FXP_CLOSE:
11310Sstevel@tonic-gate process_close();
11320Sstevel@tonic-gate break;
11330Sstevel@tonic-gate case SSH2_FXP_READ:
11340Sstevel@tonic-gate process_read();
11350Sstevel@tonic-gate break;
11360Sstevel@tonic-gate case SSH2_FXP_WRITE:
11370Sstevel@tonic-gate process_write();
11380Sstevel@tonic-gate break;
11390Sstevel@tonic-gate case SSH2_FXP_LSTAT:
11400Sstevel@tonic-gate process_lstat();
11410Sstevel@tonic-gate break;
11420Sstevel@tonic-gate case SSH2_FXP_FSTAT:
11430Sstevel@tonic-gate process_fstat();
11440Sstevel@tonic-gate break;
11450Sstevel@tonic-gate case SSH2_FXP_SETSTAT:
11460Sstevel@tonic-gate process_setstat();
11470Sstevel@tonic-gate break;
11480Sstevel@tonic-gate case SSH2_FXP_FSETSTAT:
11490Sstevel@tonic-gate process_fsetstat();
11500Sstevel@tonic-gate break;
11510Sstevel@tonic-gate case SSH2_FXP_OPENDIR:
11520Sstevel@tonic-gate process_opendir();
11530Sstevel@tonic-gate break;
11540Sstevel@tonic-gate case SSH2_FXP_READDIR:
11550Sstevel@tonic-gate process_readdir();
11560Sstevel@tonic-gate break;
11570Sstevel@tonic-gate case SSH2_FXP_REMOVE:
11580Sstevel@tonic-gate process_remove();
11590Sstevel@tonic-gate break;
11600Sstevel@tonic-gate case SSH2_FXP_MKDIR:
11610Sstevel@tonic-gate process_mkdir();
11620Sstevel@tonic-gate break;
11630Sstevel@tonic-gate case SSH2_FXP_RMDIR:
11640Sstevel@tonic-gate process_rmdir();
11650Sstevel@tonic-gate break;
11660Sstevel@tonic-gate case SSH2_FXP_REALPATH:
11670Sstevel@tonic-gate process_realpath();
11680Sstevel@tonic-gate break;
11690Sstevel@tonic-gate case SSH2_FXP_STAT:
11700Sstevel@tonic-gate process_stat();
11710Sstevel@tonic-gate break;
11720Sstevel@tonic-gate case SSH2_FXP_RENAME:
11730Sstevel@tonic-gate process_rename();
11740Sstevel@tonic-gate break;
11750Sstevel@tonic-gate case SSH2_FXP_READLINK:
11760Sstevel@tonic-gate process_readlink();
11770Sstevel@tonic-gate break;
11780Sstevel@tonic-gate case SSH2_FXP_SYMLINK:
11790Sstevel@tonic-gate process_symlink();
11800Sstevel@tonic-gate break;
11810Sstevel@tonic-gate case SSH2_FXP_EXTENDED:
11820Sstevel@tonic-gate process_extended();
11830Sstevel@tonic-gate break;
11840Sstevel@tonic-gate default:
11850Sstevel@tonic-gate error("Unknown message %d", type);
11860Sstevel@tonic-gate break;
11870Sstevel@tonic-gate }
11880Sstevel@tonic-gate /* discard the remaining bytes from the current packet */
11890Sstevel@tonic-gate if (buf_len < buffer_len(&iqueue))
11905087Sjp161948 fatal("iqueue grew unexpectedly");
11910Sstevel@tonic-gate consumed = buf_len - buffer_len(&iqueue);
11920Sstevel@tonic-gate if (msg_len < consumed)
11930Sstevel@tonic-gate fatal("msg_len %d < consumed %d", msg_len, consumed);
11940Sstevel@tonic-gate if (msg_len > consumed)
11950Sstevel@tonic-gate buffer_consume(&iqueue, msg_len - consumed);
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate
1198*9139SJan.Pechanec@Sun.COM /*
1199*9139SJan.Pechanec@Sun.COM * Cleanup handler that logs active handles upon normal exit. Not static since
1200*9139SJan.Pechanec@Sun.COM * sftp-server-main.c file needs that as well.
1201*9139SJan.Pechanec@Sun.COM */
1202*9139SJan.Pechanec@Sun.COM void
cleanup_exit(int i)12035087Sjp161948 cleanup_exit(int i)
12045087Sjp161948 {
12055087Sjp161948 if (pw != NULL && client_addr != NULL) {
12065087Sjp161948 handle_log_exit();
12075087Sjp161948 log("session closed for local user %s from [%s]",
12085087Sjp161948 pw->pw_name, client_addr);
12095087Sjp161948 }
12105087Sjp161948 _exit(i);
12115087Sjp161948 }
12125087Sjp161948
12135087Sjp161948 static void
usage(void)12145087Sjp161948 usage(void)
12155087Sjp161948 {
12165087Sjp161948 fprintf(stderr,
12175087Sjp161948 "Usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
12185087Sjp161948 exit(1);
12195087Sjp161948 }
12205087Sjp161948
12210Sstevel@tonic-gate int
sftp_server_main(int argc,char ** argv,struct passwd * user_pw)1222*9139SJan.Pechanec@Sun.COM sftp_server_main(int argc, char **argv, struct passwd *user_pw)
12230Sstevel@tonic-gate {
12240Sstevel@tonic-gate fd_set *rset, *wset;
12255087Sjp161948 int in, out, max, ch, skipargs = 0, log_stderr = 0;
12260Sstevel@tonic-gate ssize_t len, olen, set_size;
12275087Sjp161948 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
12285087Sjp161948 char *cp, buf[4*4096];
12290Sstevel@tonic-gate
12305087Sjp161948 extern char *optarg;
12310Sstevel@tonic-gate
12325087Sjp161948 __progname = get_progname(argv[0]);
12330Sstevel@tonic-gate
12340Sstevel@tonic-gate (void) g11n_setlocale(LC_ALL, "");
12350Sstevel@tonic-gate
12365087Sjp161948 log_init(__progname, log_level, log_facility, log_stderr);
12370Sstevel@tonic-gate
12385087Sjp161948 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
12395087Sjp161948 switch (ch) {
12405087Sjp161948 case 'c':
12415087Sjp161948 /*
12425087Sjp161948 * Ignore all arguments if we are invoked as a
12435087Sjp161948 * shell using "sftp-server -c command"
12445087Sjp161948 */
12455087Sjp161948 skipargs = 1;
12465087Sjp161948 break;
12475087Sjp161948 case 'e':
12485087Sjp161948 log_stderr = 1;
12495087Sjp161948 break;
12505087Sjp161948 case 'l':
12515087Sjp161948 log_level = log_level_number(optarg);
12525087Sjp161948 if (log_level == SYSLOG_LEVEL_NOT_SET)
12535087Sjp161948 error("Invalid log level \"%s\"", optarg);
12545087Sjp161948 break;
12555087Sjp161948 case 'f':
12565087Sjp161948 log_facility = log_facility_number(optarg);
12575087Sjp161948 if (log_facility == SYSLOG_FACILITY_NOT_SET)
12585087Sjp161948 error("Invalid log facility \"%s\"", optarg);
12595087Sjp161948 break;
12605087Sjp161948 case 'h':
12615087Sjp161948 default:
12625087Sjp161948 usage();
12635087Sjp161948 }
12645087Sjp161948 }
12655087Sjp161948
12665087Sjp161948 log_init(__progname, log_level, log_facility, log_stderr);
12675087Sjp161948
12685087Sjp161948 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
12695087Sjp161948 client_addr = xstrdup(cp);
12705087Sjp161948 if ((cp = strchr(client_addr, ' ')) == NULL)
12715087Sjp161948 fatal("Malformed SSH_CONNECTION variable: \"%s\"",
12725087Sjp161948 getenv("SSH_CONNECTION"));
12735087Sjp161948 *cp = '\0';
12745087Sjp161948 } else
12755087Sjp161948 client_addr = xstrdup("UNKNOWN");
12765087Sjp161948
1277*9139SJan.Pechanec@Sun.COM pw = pwcopy(user_pw);
12785087Sjp161948
12795087Sjp161948 log("session opened for local user %s from [%s]",
12805087Sjp161948 pw->pw_name, client_addr);
12815087Sjp161948
12825087Sjp161948 handle_init();
12830Sstevel@tonic-gate
12840Sstevel@tonic-gate in = dup(STDIN_FILENO);
12850Sstevel@tonic-gate out = dup(STDOUT_FILENO);
12860Sstevel@tonic-gate
12870Sstevel@tonic-gate #ifdef HAVE_CYGWIN
12880Sstevel@tonic-gate setmode(in, O_BINARY);
12890Sstevel@tonic-gate setmode(out, O_BINARY);
12900Sstevel@tonic-gate #endif
12910Sstevel@tonic-gate
12920Sstevel@tonic-gate max = 0;
12930Sstevel@tonic-gate if (in > max)
12940Sstevel@tonic-gate max = in;
12950Sstevel@tonic-gate if (out > max)
12960Sstevel@tonic-gate max = out;
12970Sstevel@tonic-gate
12980Sstevel@tonic-gate buffer_init(&iqueue);
12990Sstevel@tonic-gate buffer_init(&oqueue);
13000Sstevel@tonic-gate
13010Sstevel@tonic-gate set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
13020Sstevel@tonic-gate rset = (fd_set *)xmalloc(set_size);
13030Sstevel@tonic-gate wset = (fd_set *)xmalloc(set_size);
13040Sstevel@tonic-gate
13050Sstevel@tonic-gate for (;;) {
13060Sstevel@tonic-gate memset(rset, 0, set_size);
13070Sstevel@tonic-gate memset(wset, 0, set_size);
13080Sstevel@tonic-gate
13095087Sjp161948 /*
13105087Sjp161948 * Ensure that we can read a full buffer and handle
13115087Sjp161948 * the worst-case length packet it can generate,
13125087Sjp161948 * otherwise apply backpressure by stopping reads.
13135087Sjp161948 */
13145087Sjp161948 if (buffer_check_alloc(&iqueue, sizeof(buf)) &&
13155087Sjp161948 buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
13165087Sjp161948 FD_SET(in, rset);
13175087Sjp161948
13180Sstevel@tonic-gate olen = buffer_len(&oqueue);
13190Sstevel@tonic-gate if (olen > 0)
13200Sstevel@tonic-gate FD_SET(out, wset);
13210Sstevel@tonic-gate
13220Sstevel@tonic-gate if (select(max+1, rset, wset, NULL, NULL) < 0) {
13230Sstevel@tonic-gate if (errno == EINTR)
13240Sstevel@tonic-gate continue;
13255087Sjp161948 error("select: %s", strerror(errno));
13265087Sjp161948 cleanup_exit(2);
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate
13290Sstevel@tonic-gate /* copy stdin to iqueue */
13300Sstevel@tonic-gate if (FD_ISSET(in, rset)) {
13310Sstevel@tonic-gate len = read(in, buf, sizeof buf);
13320Sstevel@tonic-gate if (len == 0) {
13330Sstevel@tonic-gate debug("read eof");
13345087Sjp161948 cleanup_exit(0);
13350Sstevel@tonic-gate } else if (len < 0) {
13365087Sjp161948 error("read: %s", strerror(errno));
13375087Sjp161948 cleanup_exit(1);
13380Sstevel@tonic-gate } else {
13390Sstevel@tonic-gate buffer_append(&iqueue, buf, len);
13400Sstevel@tonic-gate }
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate /* send oqueue to stdout */
13430Sstevel@tonic-gate if (FD_ISSET(out, wset)) {
13440Sstevel@tonic-gate len = write(out, buffer_ptr(&oqueue), olen);
13450Sstevel@tonic-gate if (len < 0) {
13465087Sjp161948 error("write: %s", strerror(errno));
13475087Sjp161948 cleanup_exit(1);
13480Sstevel@tonic-gate } else {
13490Sstevel@tonic-gate buffer_consume(&oqueue, len);
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate }
13525087Sjp161948
13535087Sjp161948 /*
13545087Sjp161948 * Process requests from client if we can fit the results
13555087Sjp161948 * into the output buffer, otherwise stop processing input
13565087Sjp161948 * and let the output queue drain.
13575087Sjp161948 */
13585087Sjp161948 if (buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
13595087Sjp161948 process();
13600Sstevel@tonic-gate }
13615087Sjp161948
13625087Sjp161948 /* NOTREACHED */
13635087Sjp161948 return (0);
13640Sstevel@tonic-gate }
1365