blob: ad158f8e2fd390e00beac80816ac859c67c8c411 [file] [log] [blame]
Damien Millerf29238e2013-10-17 11:48:52 +11001/* $OpenBSD: sftp-server.c,v 1.102 2013/10/17 00:30:13 djm Exp $ */
Damien Miller7b28dc52000-09-05 13:34:53 +11002/*
Darren Tucker37bd3662004-02-24 09:19:15 +11003 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
Damien Miller7b28dc52000-09-05 13:34:53 +11004 *
Darren Tucker37bd3662004-02-24 09:19:15 +11005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Miller7b28dc52000-09-05 13:34:53 +11008 *
Darren Tucker37bd3662004-02-24 09:19:15 +11009 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller7b28dc52000-09-05 13:34:53 +110016 */
Damien Millerd7834352006-08-05 12:39:39 +100017
Damien Miller7b28dc52000-09-05 13:34:53 +110018#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110019
20#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100021#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110022#include <sys/stat.h>
Damien Miller9aec9192006-08-05 10:57:45 +100023#ifdef HAVE_SYS_TIME_H
24# include <sys/time.h>
25#endif
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100026#ifdef HAVE_SYS_MOUNT_H
Damien Millerd671e5a2008-05-19 14:53:33 +100027#include <sys/mount.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100028#endif
29#ifdef HAVE_SYS_STATVFS_H
Damien Millerd671e5a2008-05-19 14:53:33 +100030#include <sys/statvfs.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100031#endif
Damien Miller88f254b2006-03-15 11:25:13 +110032
33#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100035#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100036#include <pwd.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100037#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100040#include <pwd.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100041#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100042#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100043#include <stdarg.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110044
Damien Miller7b28dc52000-09-05 13:34:53 +110045#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100046#include "buffer.h"
47#include "log.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100048#include "misc.h"
Damien Miller6eaeebf2013-10-15 11:55:57 +110049#include "match.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100050#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110051
Ben Lindstrom2f959b42001-01-11 06:20:23 +000052#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110053#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110054
55/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000056#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110057#define get_int() buffer_get_int(&iqueue);
58#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Damien Miller7b28dc52000-09-05 13:34:53 +110059
Damien Millerfef95ad2006-07-10 20:46:55 +100060/* Our verbosity */
Damien Miller6eaeebf2013-10-15 11:55:57 +110061static LogLevel log_level = SYSLOG_LEVEL_ERROR;
Damien Millerfef95ad2006-07-10 20:46:55 +100062
63/* Our client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110064static struct passwd *pw = NULL;
65static char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000066
Damien Miller7b28dc52000-09-05 13:34:53 +110067/* input and output queue */
Damien Miller6eaeebf2013-10-15 11:55:57 +110068static Buffer iqueue;
69static Buffer oqueue;
Damien Miller7b28dc52000-09-05 13:34:53 +110070
Damien Miller058316f2001-03-08 10:08:49 +110071/* Version of client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110072static u_int version;
73
74/* SSH2_FXP_INIT received */
75static int init_done;
Damien Miller058316f2001-03-08 10:08:49 +110076
Darren Tuckerdb7bf822010-01-09 22:24:33 +110077/* Disable writes */
Damien Miller6eaeebf2013-10-15 11:55:57 +110078static int readonly;
79
80/* Requests that are allowed/denied */
81static char *request_whitelist, *request_blacklist;
Darren Tuckerdb7bf822010-01-09 22:24:33 +110082
Darren Tuckera6612d42003-06-28 12:39:03 +100083/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110084typedef struct Stat Stat;
85
Damien Miller33804262001-02-04 23:20:18 +110086struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110087 char *name;
88 char *long_name;
89 Attrib attrib;
90};
91
Damien Miller6eaeebf2013-10-15 11:55:57 +110092/* Packet handlers */
93static void process_open(u_int32_t id);
94static void process_close(u_int32_t id);
95static void process_read(u_int32_t id);
96static void process_write(u_int32_t id);
97static void process_stat(u_int32_t id);
98static void process_lstat(u_int32_t id);
99static void process_fstat(u_int32_t id);
100static void process_setstat(u_int32_t id);
101static void process_fsetstat(u_int32_t id);
102static void process_opendir(u_int32_t id);
103static void process_readdir(u_int32_t id);
104static void process_remove(u_int32_t id);
105static void process_mkdir(u_int32_t id);
106static void process_rmdir(u_int32_t id);
107static void process_realpath(u_int32_t id);
108static void process_rename(u_int32_t id);
109static void process_readlink(u_int32_t id);
110static void process_symlink(u_int32_t id);
111static void process_extended_posix_rename(u_int32_t id);
112static void process_extended_statvfs(u_int32_t id);
113static void process_extended_fstatvfs(u_int32_t id);
114static void process_extended_hardlink(u_int32_t id);
Damien Millerf29238e2013-10-17 11:48:52 +1100115static void process_extended_fsync(u_int32_t id);
Damien Miller6eaeebf2013-10-15 11:55:57 +1100116static void process_extended(u_int32_t id);
117
118struct sftp_handler {
119 const char *name; /* user-visible name for fine-grained perms */
120 const char *ext_name; /* extended request name */
121 u_int type; /* packet type, for non extended packets */
122 void (*handler)(u_int32_t);
123 int does_write; /* if nonzero, banned for readonly mode */
124};
125
126struct sftp_handler handlers[] = {
127 /* NB. SSH2_FXP_OPEN does the readonly check in the handler itself */
128 { "open", NULL, SSH2_FXP_OPEN, process_open, 0 },
129 { "close", NULL, SSH2_FXP_CLOSE, process_close, 0 },
130 { "read", NULL, SSH2_FXP_READ, process_read, 0 },
131 { "write", NULL, SSH2_FXP_WRITE, process_write, 1 },
132 { "lstat", NULL, SSH2_FXP_LSTAT, process_lstat, 0 },
133 { "fstat", NULL, SSH2_FXP_FSTAT, process_fstat, 0 },
134 { "setstat", NULL, SSH2_FXP_SETSTAT, process_setstat, 1 },
135 { "fsetstat", NULL, SSH2_FXP_FSETSTAT, process_fsetstat, 1 },
136 { "opendir", NULL, SSH2_FXP_OPENDIR, process_opendir, 0 },
137 { "readdir", NULL, SSH2_FXP_READDIR, process_readdir, 0 },
138 { "remove", NULL, SSH2_FXP_REMOVE, process_remove, 1 },
139 { "mkdir", NULL, SSH2_FXP_MKDIR, process_mkdir, 1 },
140 { "rmdir", NULL, SSH2_FXP_RMDIR, process_rmdir, 1 },
141 { "realpath", NULL, SSH2_FXP_REALPATH, process_realpath, 0 },
142 { "stat", NULL, SSH2_FXP_STAT, process_stat, 0 },
143 { "rename", NULL, SSH2_FXP_RENAME, process_rename, 1 },
144 { "readlink", NULL, SSH2_FXP_READLINK, process_readlink, 0 },
145 { "symlink", NULL, SSH2_FXP_SYMLINK, process_symlink, 1 },
146 { NULL, NULL, 0, NULL, 0 }
147};
148
149/* SSH2_FXP_EXTENDED submessages */
150struct sftp_handler extended_handlers[] = {
151 { "posix-rename", "posix-rename@openssh.com", 0,
152 process_extended_posix_rename, 1 },
153 { "statvfs", "statvfs@openssh.com", 0, process_extended_statvfs, 0 },
154 { "fstatvfs", "fstatvfs@openssh.com", 0, process_extended_fstatvfs, 0 },
155 { "hardlink", "hardlink@openssh.com", 0, process_extended_hardlink, 1 },
Damien Millerf29238e2013-10-17 11:48:52 +1100156 { "fsync", "fsync@openssh.com", 0, process_extended_fsync, 1 },
Damien Miller6eaeebf2013-10-15 11:55:57 +1100157 { NULL, NULL, 0, NULL, 0 }
158};
159
160static int
161request_permitted(struct sftp_handler *h)
162{
163 char *result;
164
165 if (readonly && h->does_write) {
166 verbose("Refusing %s request in read-only mode", h->name);
167 return 0;
168 }
169 if (request_blacklist != NULL &&
170 ((result = match_list(h->name, request_blacklist, NULL))) != NULL) {
171 free(result);
172 verbose("Refusing blacklisted %s request", h->name);
173 return 0;
174 }
175 if (request_whitelist != NULL &&
176 ((result = match_list(h->name, request_whitelist, NULL))) != NULL) {
177 free(result);
178 debug2("Permitting whitelisted %s request", h->name);
179 return 1;
180 }
181 if (request_whitelist != NULL) {
182 verbose("Refusing non-whitelisted %s request", h->name);
183 return 0;
184 }
185 return 1;
186}
187
Ben Lindstrombba81212001-06-25 05:01:22 +0000188static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100189errno_to_portable(int unixerrno)
190{
191 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000192
Damien Miller7b28dc52000-09-05 13:34:53 +1100193 switch (unixerrno) {
194 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000195 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100196 break;
197 case ENOENT:
198 case ENOTDIR:
199 case EBADF:
200 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000201 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100202 break;
203 case EPERM:
204 case EACCES:
205 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000206 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +1100207 break;
208 case ENAMETOOLONG:
209 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000210 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100211 break;
Darren Tucker422c34c2008-06-09 22:48:31 +1000212 case ENOSYS:
213 ret = SSH2_FX_OP_UNSUPPORTED;
214 break;
Damien Miller7b28dc52000-09-05 13:34:53 +1100215 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000216 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100217 break;
218 }
219 return ret;
220}
221
Ben Lindstrombba81212001-06-25 05:01:22 +0000222static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100223flags_from_portable(int pflags)
224{
225 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000226
Ben Lindstrom36592512001-03-05 05:02:08 +0000227 if ((pflags & SSH2_FXF_READ) &&
228 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100229 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000230 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100231 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000232 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100233 flags = O_WRONLY;
234 }
Damien Millere9fc72e2013-10-15 12:14:12 +1100235 if (pflags & SSH2_FXF_APPEND)
236 flags |= O_APPEND;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000237 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100238 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000239 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100240 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000241 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100242 flags |= O_EXCL;
243 return flags;
244}
245
Damien Millerfef95ad2006-07-10 20:46:55 +1000246static const char *
247string_from_portable(int pflags)
248{
249 static char ret[128];
250
251 *ret = '\0';
252
253#define PAPPEND(str) { \
254 if (*ret != '\0') \
255 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000256 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000257 }
258
259 if (pflags & SSH2_FXF_READ)
260 PAPPEND("READ")
261 if (pflags & SSH2_FXF_WRITE)
262 PAPPEND("WRITE")
Damien Millere9fc72e2013-10-15 12:14:12 +1100263 if (pflags & SSH2_FXF_APPEND)
264 PAPPEND("APPEND")
Damien Millerfef95ad2006-07-10 20:46:55 +1000265 if (pflags & SSH2_FXF_CREAT)
266 PAPPEND("CREATE")
267 if (pflags & SSH2_FXF_TRUNC)
268 PAPPEND("TRUNCATE")
269 if (pflags & SSH2_FXF_EXCL)
270 PAPPEND("EXCL")
271
272 return ret;
273}
274
Ben Lindstrombba81212001-06-25 05:01:22 +0000275static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100276get_attrib(void)
277{
278 return decode_attrib(&iqueue);
279}
280
281/* handle handles */
282
283typedef struct Handle Handle;
284struct Handle {
285 int use;
286 DIR *dirp;
287 int fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100288 int flags;
Damien Miller7b28dc52000-09-05 13:34:53 +1100289 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000290 u_int64_t bytes_read, bytes_write;
Damien Miller3397d0e2008-02-10 22:26:51 +1100291 int next_unused;
Damien Miller7b28dc52000-09-05 13:34:53 +1100292};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000293
Damien Miller7b28dc52000-09-05 13:34:53 +1100294enum {
295 HANDLE_UNUSED,
296 HANDLE_DIR,
297 HANDLE_FILE
298};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000299
Damien Miller3397d0e2008-02-10 22:26:51 +1100300Handle *handles = NULL;
301u_int num_handles = 0;
302int first_unused_handle = -1;
Damien Miller7b28dc52000-09-05 13:34:53 +1100303
Damien Miller3397d0e2008-02-10 22:26:51 +1100304static void handle_unused(int i)
Damien Miller7b28dc52000-09-05 13:34:53 +1100305{
Damien Miller3397d0e2008-02-10 22:26:51 +1100306 handles[i].use = HANDLE_UNUSED;
307 handles[i].next_unused = first_unused_handle;
308 first_unused_handle = i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100309}
310
Ben Lindstrombba81212001-06-25 05:01:22 +0000311static int
Damien Millere9fc72e2013-10-15 12:14:12 +1100312handle_new(int use, const char *name, int fd, int flags, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100313{
Damien Miller3397d0e2008-02-10 22:26:51 +1100314 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000315
Damien Miller3397d0e2008-02-10 22:26:51 +1100316 if (first_unused_handle == -1) {
317 if (num_handles + 1 <= num_handles)
318 return -1;
319 num_handles++;
320 handles = xrealloc(handles, num_handles, sizeof(Handle));
321 handle_unused(num_handles - 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100322 }
Damien Miller3397d0e2008-02-10 22:26:51 +1100323
324 i = first_unused_handle;
325 first_unused_handle = handles[i].next_unused;
326
327 handles[i].use = use;
328 handles[i].dirp = dirp;
329 handles[i].fd = fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100330 handles[i].flags = flags;
Damien Miller3397d0e2008-02-10 22:26:51 +1100331 handles[i].name = xstrdup(name);
332 handles[i].bytes_read = handles[i].bytes_write = 0;
333
334 return i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100335}
336
Ben Lindstrombba81212001-06-25 05:01:22 +0000337static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100338handle_is_ok(int i, int type)
339{
Damien Miller3397d0e2008-02-10 22:26:51 +1100340 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100341}
342
Ben Lindstrombba81212001-06-25 05:01:22 +0000343static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100344handle_to_string(int handle, char **stringp, int *hlenp)
345{
Damien Miller7b28dc52000-09-05 13:34:53 +1100346 if (stringp == NULL || hlenp == NULL)
347 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000348 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100349 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000350 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100351 return 0;
352}
353
Ben Lindstrombba81212001-06-25 05:01:22 +0000354static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100355handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100356{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000357 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000358
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000359 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100360 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100361 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100362 if (handle_is_ok(val, HANDLE_FILE) ||
363 handle_is_ok(val, HANDLE_DIR))
364 return val;
365 return -1;
366}
367
Ben Lindstrombba81212001-06-25 05:01:22 +0000368static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100369handle_to_name(int handle)
370{
371 if (handle_is_ok(handle, HANDLE_DIR)||
372 handle_is_ok(handle, HANDLE_FILE))
373 return handles[handle].name;
374 return NULL;
375}
376
Ben Lindstrombba81212001-06-25 05:01:22 +0000377static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100378handle_to_dir(int handle)
379{
380 if (handle_is_ok(handle, HANDLE_DIR))
381 return handles[handle].dirp;
382 return NULL;
383}
384
Ben Lindstrombba81212001-06-25 05:01:22 +0000385static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100386handle_to_fd(int handle)
387{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000388 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100389 return handles[handle].fd;
390 return -1;
391}
392
Damien Millere9fc72e2013-10-15 12:14:12 +1100393static int
394handle_to_flags(int handle)
395{
396 if (handle_is_ok(handle, HANDLE_FILE))
397 return handles[handle].flags;
398 return 0;
399}
400
Damien Millerfef95ad2006-07-10 20:46:55 +1000401static void
402handle_update_read(int handle, ssize_t bytes)
403{
404 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
405 handles[handle].bytes_read += bytes;
406}
407
408static void
409handle_update_write(int handle, ssize_t bytes)
410{
411 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
412 handles[handle].bytes_write += bytes;
413}
414
415static u_int64_t
416handle_bytes_read(int handle)
417{
418 if (handle_is_ok(handle, HANDLE_FILE))
419 return (handles[handle].bytes_read);
420 return 0;
421}
422
423static u_int64_t
424handle_bytes_write(int handle)
425{
426 if (handle_is_ok(handle, HANDLE_FILE))
427 return (handles[handle].bytes_write);
428 return 0;
429}
430
Ben Lindstrombba81212001-06-25 05:01:22 +0000431static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100432handle_close(int handle)
433{
434 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000435
Damien Miller7b28dc52000-09-05 13:34:53 +1100436 if (handle_is_ok(handle, HANDLE_FILE)) {
437 ret = close(handles[handle].fd);
Darren Tuckera627d422013-06-02 07:31:17 +1000438 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100439 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100440 } else if (handle_is_ok(handle, HANDLE_DIR)) {
441 ret = closedir(handles[handle].dirp);
Darren Tuckera627d422013-06-02 07:31:17 +1000442 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100443 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100444 } else {
445 errno = ENOENT;
446 }
447 return ret;
448}
449
Damien Millerfef95ad2006-07-10 20:46:55 +1000450static void
451handle_log_close(int handle, char *emsg)
452{
453 if (handle_is_ok(handle, HANDLE_FILE)) {
454 logit("%s%sclose \"%s\" bytes read %llu written %llu",
455 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
456 handle_to_name(handle),
Darren Tucker86473c52007-05-20 14:59:32 +1000457 (unsigned long long)handle_bytes_read(handle),
458 (unsigned long long)handle_bytes_write(handle));
Damien Millerfef95ad2006-07-10 20:46:55 +1000459 } else {
460 logit("%s%sclosedir \"%s\"",
461 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
462 handle_to_name(handle));
463 }
464}
465
466static void
467handle_log_exit(void)
468{
469 u_int i;
470
Damien Miller3397d0e2008-02-10 22:26:51 +1100471 for (i = 0; i < num_handles; i++)
Damien Millerfef95ad2006-07-10 20:46:55 +1000472 if (handles[i].use != HANDLE_UNUSED)
473 handle_log_close(i, "forced");
474}
475
Ben Lindstrombba81212001-06-25 05:01:22 +0000476static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100477get_handle(void)
478{
479 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000480 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100481 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000482
Damien Miller7b28dc52000-09-05 13:34:53 +1100483 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000484 if (hlen < 256)
485 val = handle_from_string(handle, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000486 free(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100487 return val;
488}
489
490/* send replies */
491
Ben Lindstrombba81212001-06-25 05:01:22 +0000492static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100493send_msg(Buffer *m)
494{
495 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000496
Damien Miller7b28dc52000-09-05 13:34:53 +1100497 buffer_put_int(&oqueue, mlen);
498 buffer_append(&oqueue, buffer_ptr(m), mlen);
499 buffer_consume(m, mlen);
500}
501
Damien Millerfef95ad2006-07-10 20:46:55 +1000502static const char *
503status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100504{
Damien Miller058316f2001-03-08 10:08:49 +1100505 const char *status_messages[] = {
506 "Success", /* SSH_FX_OK */
507 "End of file", /* SSH_FX_EOF */
508 "No such file", /* SSH_FX_NO_SUCH_FILE */
509 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
510 "Failure", /* SSH_FX_FAILURE */
511 "Bad message", /* SSH_FX_BAD_MESSAGE */
512 "No connection", /* SSH_FX_NO_CONNECTION */
513 "Connection lost", /* SSH_FX_CONNECTION_LOST */
514 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
515 "Unknown error" /* Others */
516 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000517 return (status_messages[MIN(status,SSH2_FX_MAX)]);
518}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000519
Damien Millerfef95ad2006-07-10 20:46:55 +1000520static void
521send_status(u_int32_t id, u_int32_t status)
522{
523 Buffer msg;
524
525 debug3("request %u: sent status %u", id, status);
526 if (log_level > SYSLOG_LEVEL_VERBOSE ||
527 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
528 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100529 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000530 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100531 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000532 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100533 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000534 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100535 buffer_put_cstring(&msg, "");
536 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100537 send_msg(&msg);
538 buffer_free(&msg);
539}
Ben Lindstrombba81212001-06-25 05:01:22 +0000540static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100541send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100542{
543 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000544
Damien Miller7b28dc52000-09-05 13:34:53 +1100545 buffer_init(&msg);
546 buffer_put_char(&msg, type);
547 buffer_put_int(&msg, id);
548 buffer_put_string(&msg, data, dlen);
549 send_msg(&msg);
550 buffer_free(&msg);
551}
552
Ben Lindstrombba81212001-06-25 05:01:22 +0000553static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100554send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100555{
Damien Millerfef95ad2006-07-10 20:46:55 +1000556 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000557 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100558}
559
Ben Lindstrombba81212001-06-25 05:01:22 +0000560static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100561send_handle(u_int32_t id, int handle)
562{
563 char *string;
564 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000565
Damien Miller7b28dc52000-09-05 13:34:53 +1100566 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000567 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000568 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000569 free(string);
Damien Miller7b28dc52000-09-05 13:34:53 +1100570}
571
Ben Lindstrombba81212001-06-25 05:01:22 +0000572static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100573send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100574{
575 Buffer msg;
576 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000577
Damien Miller7b28dc52000-09-05 13:34:53 +1100578 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000579 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100580 buffer_put_int(&msg, id);
581 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000582 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100583 for (i = 0; i < count; i++) {
584 buffer_put_cstring(&msg, stats[i].name);
585 buffer_put_cstring(&msg, stats[i].long_name);
586 encode_attrib(&msg, &stats[i].attrib);
587 }
588 send_msg(&msg);
589 buffer_free(&msg);
590}
591
Ben Lindstrombba81212001-06-25 05:01:22 +0000592static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100593send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100594{
595 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000596
Damien Millerfef95ad2006-07-10 20:46:55 +1000597 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100598 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000599 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100600 buffer_put_int(&msg, id);
601 encode_attrib(&msg, a);
602 send_msg(&msg);
603 buffer_free(&msg);
604}
605
Damien Millerd671e5a2008-05-19 14:53:33 +1000606static void
607send_statvfs(u_int32_t id, struct statvfs *st)
608{
609 Buffer msg;
610 u_int64_t flag;
611
612 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
613 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
614
615 buffer_init(&msg);
616 buffer_put_char(&msg, SSH2_FXP_EXTENDED_REPLY);
617 buffer_put_int(&msg, id);
Darren Tucker3463aca2008-06-09 23:06:55 +1000618 buffer_put_int64(&msg, st->f_bsize);
619 buffer_put_int64(&msg, st->f_frsize);
Damien Millerd671e5a2008-05-19 14:53:33 +1000620 buffer_put_int64(&msg, st->f_blocks);
621 buffer_put_int64(&msg, st->f_bfree);
622 buffer_put_int64(&msg, st->f_bavail);
623 buffer_put_int64(&msg, st->f_files);
624 buffer_put_int64(&msg, st->f_ffree);
625 buffer_put_int64(&msg, st->f_favail);
Darren Tucker77001382008-06-09 06:17:53 +1000626 buffer_put_int64(&msg, FSID_TO_ULONG(st->f_fsid));
Darren Tucker3463aca2008-06-09 23:06:55 +1000627 buffer_put_int64(&msg, flag);
628 buffer_put_int64(&msg, st->f_namemax);
Damien Millerd671e5a2008-05-19 14:53:33 +1000629 send_msg(&msg);
630 buffer_free(&msg);
631}
632
Damien Miller7b28dc52000-09-05 13:34:53 +1100633/* parse incoming */
634
Ben Lindstrombba81212001-06-25 05:01:22 +0000635static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100636process_init(void)
637{
638 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100639
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000640 version = get_int();
Damien Millerf145a5b2011-06-20 14:42:51 +1000641 verbose("received client version %u", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100642 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000643 buffer_put_char(&msg, SSH2_FXP_VERSION);
644 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7c296612008-03-07 18:33:53 +1100645 /* POSIX rename extension */
646 buffer_put_cstring(&msg, "posix-rename@openssh.com");
647 buffer_put_cstring(&msg, "1"); /* version */
Damien Millera7e0d5a2008-05-19 16:08:41 +1000648 /* statvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000649 buffer_put_cstring(&msg, "statvfs@openssh.com");
Darren Tucker294b8412008-06-08 12:57:08 +1000650 buffer_put_cstring(&msg, "2"); /* version */
Damien Millera7e0d5a2008-05-19 16:08:41 +1000651 /* fstatvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000652 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
Darren Tucker294b8412008-06-08 12:57:08 +1000653 buffer_put_cstring(&msg, "2"); /* version */
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100654 /* hardlink extension */
655 buffer_put_cstring(&msg, "hardlink@openssh.com");
656 buffer_put_cstring(&msg, "1"); /* version */
Damien Millerf29238e2013-10-17 11:48:52 +1100657 /* fsync extension */
658 buffer_put_cstring(&msg, "fsync@openssh.com");
659 buffer_put_cstring(&msg, "1"); /* version */
Damien Miller7b28dc52000-09-05 13:34:53 +1100660 send_msg(&msg);
661 buffer_free(&msg);
662}
663
Ben Lindstrombba81212001-06-25 05:01:22 +0000664static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100665process_open(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100666{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100667 u_int32_t pflags;
Damien Miller7b28dc52000-09-05 13:34:53 +1100668 Attrib *a;
669 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000670 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100671
Damien Miller7b28dc52000-09-05 13:34:53 +1100672 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000673 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000674 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100675 a = get_attrib();
676 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000677 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000678 logit("open \"%s\" flags %s mode 0%o",
679 name, string_from_portable(pflags), mode);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100680 if (readonly &&
Damien Miller6eaeebf2013-10-15 11:55:57 +1100681 ((flags & O_ACCMODE) == O_WRONLY ||
682 (flags & O_ACCMODE) == O_RDWR)) {
683 verbose("Refusing open request in read-only mode");
684 status = SSH2_FX_PERMISSION_DENIED;
685 } else {
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100686 fd = open(name, flags, mode);
687 if (fd < 0) {
688 status = errno_to_portable(errno);
Damien Miller7b28dc52000-09-05 13:34:53 +1100689 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100690 handle = handle_new(HANDLE_FILE, name, fd, flags, NULL);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100691 if (handle < 0) {
692 close(fd);
693 } else {
694 send_handle(id, handle);
695 status = SSH2_FX_OK;
696 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100697 }
698 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000699 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100700 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000701 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100702}
703
Ben Lindstrombba81212001-06-25 05:01:22 +0000704static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100705process_close(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100706{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000707 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100708
Damien Miller7b28dc52000-09-05 13:34:53 +1100709 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000710 debug3("request %u: close handle %u", id, handle);
711 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100712 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000713 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100714 send_status(id, status);
715}
716
Ben Lindstrombba81212001-06-25 05:01:22 +0000717static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100718process_read(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100719{
720 char buf[64*1024];
Damien Miller6eaeebf2013-10-15 11:55:57 +1100721 u_int32_t len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000722 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100723 u_int64_t off;
724
Damien Miller7b28dc52000-09-05 13:34:53 +1100725 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000726 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100727 len = get_int();
728
Damien Millerfef95ad2006-07-10 20:46:55 +1000729 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
730 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100731 if (len > sizeof buf) {
732 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000733 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100734 }
735 fd = handle_to_fd(handle);
736 if (fd >= 0) {
737 if (lseek(fd, off, SEEK_SET) < 0) {
738 error("process_read: seek failed");
739 status = errno_to_portable(errno);
740 } else {
741 ret = read(fd, buf, len);
742 if (ret < 0) {
743 status = errno_to_portable(errno);
744 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000745 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100746 } else {
747 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000748 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000749 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100750 }
751 }
752 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000753 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100754 send_status(id, status);
755}
756
Ben Lindstrombba81212001-06-25 05:01:22 +0000757static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100758process_write(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100759{
Damien Miller7b28dc52000-09-05 13:34:53 +1100760 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100761 u_int len;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100762 int handle, fd, ret, status;
Damien Miller7b28dc52000-09-05 13:34:53 +1100763 char *data;
764
Damien Miller7b28dc52000-09-05 13:34:53 +1100765 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000766 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100767 data = get_string(&len);
768
Damien Millerfef95ad2006-07-10 20:46:55 +1000769 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
770 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100771 fd = handle_to_fd(handle);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100772
773 if (fd < 0)
774 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100775 else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100776 if (!(handle_to_flags(handle) & O_APPEND) &&
777 lseek(fd, off, SEEK_SET) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100778 status = errno_to_portable(errno);
779 error("process_write: seek failed");
780 } else {
781/* XXX ATOMICIO ? */
782 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000783 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100784 error("process_write: write failed");
785 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000786 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000787 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000788 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100789 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000790 debug2("nothing at all written");
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100791 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100792 }
793 }
794 }
795 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000796 free(data);
Damien Miller7b28dc52000-09-05 13:34:53 +1100797}
798
Ben Lindstrombba81212001-06-25 05:01:22 +0000799static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100800process_do_stat(u_int32_t id, int do_lstat)
Damien Miller7b28dc52000-09-05 13:34:53 +1100801{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000802 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100803 struct stat st;
Damien Miller7b28dc52000-09-05 13:34:53 +1100804 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000805 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100806
Damien Miller7b28dc52000-09-05 13:34:53 +1100807 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000808 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
809 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100810 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
811 if (ret < 0) {
812 status = errno_to_portable(errno);
813 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000814 stat_to_attrib(&st, &a);
815 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000816 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100817 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000818 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100819 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000820 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100821}
822
Ben Lindstrombba81212001-06-25 05:01:22 +0000823static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100824process_stat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100825{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100826 process_do_stat(id, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100827}
828
Ben Lindstrombba81212001-06-25 05:01:22 +0000829static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100830process_lstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100831{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100832 process_do_stat(id, 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100833}
834
Ben Lindstrombba81212001-06-25 05:01:22 +0000835static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100836process_fstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100837{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000838 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100839 struct stat st;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000840 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100841
Damien Miller7b28dc52000-09-05 13:34:53 +1100842 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000843 debug("request %u: fstat \"%s\" (handle %u)",
844 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100845 fd = handle_to_fd(handle);
Damien Millere2334d62007-01-05 16:31:02 +1100846 if (fd >= 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100847 ret = fstat(fd, &st);
848 if (ret < 0) {
849 status = errno_to_portable(errno);
850 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000851 stat_to_attrib(&st, &a);
852 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000853 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100854 }
855 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000856 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100857 send_status(id, status);
858}
859
Ben Lindstrombba81212001-06-25 05:01:22 +0000860static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100861attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100862{
863 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000864
Damien Miller7b28dc52000-09-05 13:34:53 +1100865 tv[0].tv_sec = a->atime;
866 tv[0].tv_usec = 0;
867 tv[1].tv_sec = a->mtime;
868 tv[1].tv_usec = 0;
869 return tv;
870}
871
Ben Lindstrombba81212001-06-25 05:01:22 +0000872static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100873process_setstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100874{
875 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100876 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000877 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100878
Damien Miller7b28dc52000-09-05 13:34:53 +1100879 name = get_string(NULL);
880 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000881 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100882 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000883 logit("set \"%s\" size %llu",
884 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100885 ret = truncate(name, a->size);
886 if (ret == -1)
887 status = errno_to_portable(errno);
888 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000889 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000890 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller9e720282008-06-29 22:46:35 +1000891 ret = chmod(name, a->perm & 07777);
Damien Miller7b28dc52000-09-05 13:34:53 +1100892 if (ret == -1)
893 status = errno_to_portable(errno);
894 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000895 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000896 char buf[64];
897 time_t t = a->mtime;
898
899 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
900 localtime(&t));
901 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100902 ret = utimes(name, attrib_to_tv(a));
903 if (ret == -1)
904 status = errno_to_portable(errno);
905 }
Kevin Steves8e743932001-02-05 13:24:35 +0000906 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000907 logit("set \"%s\" owner %lu group %lu", name,
908 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000909 ret = chown(name, a->uid, a->gid);
910 if (ret == -1)
911 status = errno_to_portable(errno);
912 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100913 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000914 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100915}
916
Ben Lindstrombba81212001-06-25 05:01:22 +0000917static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100918process_fsetstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100919{
920 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100921 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000922 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000923
Damien Miller7b28dc52000-09-05 13:34:53 +1100924 handle = get_handle();
925 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000926 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100927 fd = handle_to_fd(handle);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100928 if (fd < 0)
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000929 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100930 else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000931 char *name = handle_to_name(handle);
932
Damien Miller00c92172002-02-13 14:05:00 +1100933 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000934 logit("set \"%s\" size %llu",
935 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100936 ret = ftruncate(fd, a->size);
937 if (ret == -1)
938 status = errno_to_portable(errno);
939 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000940 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000941 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000942#ifdef HAVE_FCHMOD
Damien Miller9e720282008-06-29 22:46:35 +1000943 ret = fchmod(fd, a->perm & 07777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000944#else
Damien Miller9e720282008-06-29 22:46:35 +1000945 ret = chmod(name, a->perm & 07777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000946#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100947 if (ret == -1)
948 status = errno_to_portable(errno);
949 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000950 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000951 char buf[64];
952 time_t t = a->mtime;
953
954 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
955 localtime(&t));
956 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100957#ifdef HAVE_FUTIMES
958 ret = futimes(fd, attrib_to_tv(a));
959#else
960 ret = utimes(name, attrib_to_tv(a));
961#endif
962 if (ret == -1)
963 status = errno_to_portable(errno);
964 }
Kevin Steves8e743932001-02-05 13:24:35 +0000965 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000966 logit("set \"%s\" owner %lu group %lu", name,
967 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000968#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000969 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000970#else
971 ret = chown(name, a->uid, a->gid);
972#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000973 if (ret == -1)
974 status = errno_to_portable(errno);
975 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100976 }
977 send_status(id, status);
978}
979
Ben Lindstrombba81212001-06-25 05:01:22 +0000980static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100981process_opendir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100982{
983 DIR *dirp = NULL;
984 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000985 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100986
Damien Miller7b28dc52000-09-05 13:34:53 +1100987 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000988 debug3("request %u: opendir", id);
989 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000990 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100991 if (dirp == NULL) {
992 status = errno_to_portable(errno);
993 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100994 handle = handle_new(HANDLE_DIR, path, 0, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100995 if (handle < 0) {
996 closedir(dirp);
997 } else {
998 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000999 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001000 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001001
Damien Miller7b28dc52000-09-05 13:34:53 +11001002 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001003 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +11001004 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001005 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001006}
1007
Ben Lindstrombba81212001-06-25 05:01:22 +00001008static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001009process_readdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001010{
1011 DIR *dirp;
1012 struct dirent *dp;
1013 char *path;
1014 int handle;
Damien Miller7b28dc52000-09-05 13:34:53 +11001015
Damien Miller7b28dc52000-09-05 13:34:53 +11001016 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +10001017 debug("request %u: readdir \"%s\" (handle %d)", id,
1018 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +11001019 dirp = handle_to_dir(handle);
1020 path = handle_to_name(handle);
1021 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001022 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +11001023 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +11001024 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +10001025 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +11001026 Stat *stats;
1027 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001028
Damien Miller07d86be2006-03-26 14:19:21 +11001029 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001030 while ((dp = readdir(dirp)) != NULL) {
1031 if (count >= nstats) {
1032 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +11001033 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001034 }
1035/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +00001036 snprintf(pathname, sizeof pathname, "%s%s%s", path,
1037 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001038 if (lstat(pathname, &st) < 0)
1039 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001040 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +11001041 stats[count].name = xstrdup(dp->d_name);
Darren Tucker2901e2d2010-01-13 22:44:06 +11001042 stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001043 count++;
1044 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001045 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +11001046 if (count == 100)
1047 break;
1048 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001049 if (count > 0) {
1050 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001051 for (i = 0; i < count; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001052 free(stats[i].name);
1053 free(stats[i].long_name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001054 }
1055 } else {
1056 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +11001057 }
Darren Tuckera627d422013-06-02 07:31:17 +10001058 free(stats);
Damien Miller7b28dc52000-09-05 13:34:53 +11001059 }
1060}
1061
Ben Lindstrombba81212001-06-25 05:01:22 +00001062static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001063process_remove(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001064{
1065 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001066 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001067 int ret;
1068
Damien Miller7b28dc52000-09-05 13:34:53 +11001069 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001070 debug3("request %u: remove", id);
1071 logit("remove name \"%s\"", name);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001072 ret = unlink(name);
1073 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001074 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001075 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001076}
1077
Ben Lindstrombba81212001-06-25 05:01:22 +00001078static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001079process_mkdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001080{
1081 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +11001082 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001083 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001084
Damien Miller7b28dc52000-09-05 13:34:53 +11001085 name = get_string(NULL);
1086 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001087 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
Damien Miller9e720282008-06-29 22:46:35 +10001088 a->perm & 07777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +10001089 debug3("request %u: mkdir", id);
1090 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001091 ret = mkdir(name, mode);
1092 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001093 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001094 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001095}
1096
Ben Lindstrombba81212001-06-25 05:01:22 +00001097static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001098process_rmdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001099{
Damien Miller7b28dc52000-09-05 13:34:53 +11001100 char *name;
1101 int ret, status;
1102
Damien Miller7b28dc52000-09-05 13:34:53 +11001103 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001104 debug3("request %u: rmdir", id);
1105 logit("rmdir name \"%s\"", name);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001106 ret = rmdir(name);
1107 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001108 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001109 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001110}
1111
Ben Lindstrombba81212001-06-25 05:01:22 +00001112static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001113process_realpath(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001114{
1115 char resolvedname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +11001116 char *path;
1117
Damien Miller7b28dc52000-09-05 13:34:53 +11001118 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001119 if (path[0] == '\0') {
Darren Tuckera627d422013-06-02 07:31:17 +10001120 free(path);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001121 path = xstrdup(".");
1122 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001123 debug3("request %u: realpath", id);
1124 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001125 if (realpath(path, resolvedname) == NULL) {
1126 send_status(id, errno_to_portable(errno));
1127 } else {
1128 Stat s;
1129 attrib_clear(&s.attrib);
1130 s.name = s.long_name = resolvedname;
1131 send_names(id, 1, &s);
1132 }
Darren Tuckera627d422013-06-02 07:31:17 +10001133 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001134}
1135
Ben Lindstrombba81212001-06-25 05:01:22 +00001136static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001137process_rename(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001138{
Damien Miller7b28dc52000-09-05 13:34:53 +11001139 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001140 int status;
Damien Millerb3207e82003-03-26 16:01:11 +11001141 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +11001142
Damien Miller7b28dc52000-09-05 13:34:53 +11001143 oldpath = get_string(NULL);
1144 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001145 debug3("request %u: rename", id);
1146 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +11001147 status = SSH2_FX_FAILURE;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001148 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +11001149 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +11001150 else if (S_ISREG(sb.st_mode)) {
1151 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001152 if (link(oldpath, newpath) == -1) {
Damien Miller0e265512009-08-28 10:43:13 +10001153 if (errno == EOPNOTSUPP || errno == ENOSYS
Darren Tuckerf7fa7062008-07-04 14:10:19 +10001154#ifdef EXDEV
1155 || errno == EXDEV
1156#endif
Darren Tuckere59b5082004-06-28 16:01:19 +10001157#ifdef LINK_OPNOTSUPP_ERRNO
1158 || errno == LINK_OPNOTSUPP_ERRNO
1159#endif
1160 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001161 struct stat st;
1162
1163 /*
1164 * fs doesn't support links, so fall back to
1165 * stat+rename. This is racy.
1166 */
1167 if (stat(newpath, &st) == -1) {
1168 if (rename(oldpath, newpath) == -1)
1169 status =
1170 errno_to_portable(errno);
1171 else
1172 status = SSH2_FX_OK;
1173 }
1174 } else {
1175 status = errno_to_portable(errno);
1176 }
1177 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001178 status = errno_to_portable(errno);
1179 /* clean spare link */
1180 unlink(newpath);
1181 } else
1182 status = SSH2_FX_OK;
1183 } else if (stat(newpath, &sb) == -1) {
1184 if (rename(oldpath, newpath) == -1)
1185 status = errno_to_portable(errno);
1186 else
1187 status = SSH2_FX_OK;
1188 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001189 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001190 free(oldpath);
1191 free(newpath);
Damien Miller7b28dc52000-09-05 13:34:53 +11001192}
1193
Ben Lindstrombba81212001-06-25 05:01:22 +00001194static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001195process_readlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001196{
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001197 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001198 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001199 char *path;
1200
Damien Miller058316f2001-03-08 10:08:49 +11001201 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001202 debug3("request %u: readlink", id);
1203 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001204 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001205 send_status(id, errno_to_portable(errno));
1206 else {
1207 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001208
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001209 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001210 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001211 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001212 send_names(id, 1, &s);
1213 }
Darren Tuckera627d422013-06-02 07:31:17 +10001214 free(path);
Damien Miller058316f2001-03-08 10:08:49 +11001215}
1216
Ben Lindstrombba81212001-06-25 05:01:22 +00001217static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001218process_symlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001219{
Damien Miller058316f2001-03-08 10:08:49 +11001220 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001221 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001222
Damien Miller058316f2001-03-08 10:08:49 +11001223 oldpath = get_string(NULL);
1224 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001225 debug3("request %u: symlink", id);
1226 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001227 /* this will fail if 'newpath' exists */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001228 ret = symlink(oldpath, newpath);
1229 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001230 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001231 free(oldpath);
1232 free(newpath);
Damien Miller058316f2001-03-08 10:08:49 +11001233}
1234
Ben Lindstrombba81212001-06-25 05:01:22 +00001235static void
Damien Miller7c296612008-03-07 18:33:53 +11001236process_extended_posix_rename(u_int32_t id)
1237{
1238 char *oldpath, *newpath;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001239 int ret, status;
Damien Miller7c296612008-03-07 18:33:53 +11001240
1241 oldpath = get_string(NULL);
1242 newpath = get_string(NULL);
1243 debug3("request %u: posix-rename", id);
1244 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001245 ret = rename(oldpath, newpath);
1246 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001247 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001248 free(oldpath);
1249 free(newpath);
Damien Miller7c296612008-03-07 18:33:53 +11001250}
1251
1252static void
Damien Millerd671e5a2008-05-19 14:53:33 +10001253process_extended_statvfs(u_int32_t id)
1254{
1255 char *path;
1256 struct statvfs st;
1257
1258 path = get_string(NULL);
1259 debug3("request %u: statfs", id);
1260 logit("statfs \"%s\"", path);
1261
1262 if (statvfs(path, &st) != 0)
1263 send_status(id, errno_to_portable(errno));
1264 else
1265 send_statvfs(id, &st);
Darren Tuckera627d422013-06-02 07:31:17 +10001266 free(path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001267}
1268
1269static void
1270process_extended_fstatvfs(u_int32_t id)
1271{
1272 int handle, fd;
1273 struct statvfs st;
1274
1275 handle = get_handle();
1276 debug("request %u: fstatvfs \"%s\" (handle %u)",
1277 id, handle_to_name(handle), handle);
1278 if ((fd = handle_to_fd(handle)) < 0) {
1279 send_status(id, SSH2_FX_FAILURE);
1280 return;
1281 }
1282 if (fstatvfs(fd, &st) != 0)
1283 send_status(id, errno_to_portable(errno));
1284 else
1285 send_statvfs(id, &st);
1286}
1287
1288static void
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001289process_extended_hardlink(u_int32_t id)
1290{
1291 char *oldpath, *newpath;
1292 int ret, status;
1293
1294 oldpath = get_string(NULL);
1295 newpath = get_string(NULL);
1296 debug3("request %u: hardlink", id);
1297 logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001298 ret = link(oldpath, newpath);
1299 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001300 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001301 free(oldpath);
1302 free(newpath);
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001303}
1304
1305static void
Damien Millerf29238e2013-10-17 11:48:52 +11001306process_extended_fsync(u_int32_t id)
1307{
1308 int handle, fd, ret, status = SSH2_FX_OP_UNSUPPORTED;
1309
1310 handle = get_handle();
1311 debug3("request %u: fsync (handle %u)", id, handle);
1312 verbose("fsync \"%s\"", handle_to_name(handle));
1313 if ((fd = handle_to_fd(handle)) < 0)
1314 status = SSH2_FX_NO_SUCH_FILE;
1315 else if (handle_is_ok(handle, HANDLE_FILE)) {
1316 ret = fsync(fd);
1317 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1318 }
1319 send_status(id, status);
1320}
1321
1322static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001323process_extended(u_int32_t id)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001324{
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001325 char *request;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001326 u_int i;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001327
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001328 request = get_string(NULL);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001329 for (i = 0; extended_handlers[i].handler != NULL; i++) {
1330 if (strcmp(request, extended_handlers[i].ext_name) == 0) {
1331 if (!request_permitted(&extended_handlers[i]))
1332 send_status(id, SSH2_FX_PERMISSION_DENIED);
1333 else
1334 extended_handlers[i].handler(id);
1335 break;
1336 }
1337 }
1338 if (extended_handlers[i].handler == NULL) {
1339 error("Unknown extended request \"%.100s\"", request);
Damien Miller7c296612008-03-07 18:33:53 +11001340 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001341 }
Darren Tuckera627d422013-06-02 07:31:17 +10001342 free(request);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001343}
Damien Miller7b28dc52000-09-05 13:34:53 +11001344
1345/* stolen from ssh-agent */
1346
Ben Lindstrombba81212001-06-25 05:01:22 +00001347static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001348process(void)
1349{
Damien Miller6eaeebf2013-10-15 11:55:57 +11001350 u_int msg_len, buf_len, consumed, type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001351 u_char *cp;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001352 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +11001353
Ben Lindstrom2c140472002-06-06 21:57:54 +00001354 buf_len = buffer_len(&iqueue);
1355 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001356 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001357 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001358 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001359 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001360 error("bad message from %s local user %s",
1361 client_addr, pw->pw_name);
Damien Millerdfc24252008-02-10 22:29:40 +11001362 sftp_server_cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001363 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001364 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001365 return;
1366 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001367 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001368 type = buffer_get_char(&iqueue);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001369
Damien Miller7b28dc52000-09-05 13:34:53 +11001370 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001371 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001372 process_init();
Damien Miller6eaeebf2013-10-15 11:55:57 +11001373 init_done = 1;
Damien Miller058316f2001-03-08 10:08:49 +11001374 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001375 case SSH2_FXP_EXTENDED:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001376 if (!init_done)
1377 fatal("Received extended request before init");
1378 id = get_int();
1379 process_extended(id);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001380 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001381 default:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001382 if (!init_done)
1383 fatal("Received %u request before init", type);
1384 id = get_int();
1385 for (i = 0; handlers[i].handler != NULL; i++) {
1386 if (type == handlers[i].type) {
1387 if (!request_permitted(&handlers[i])) {
1388 send_status(id,
1389 SSH2_FX_PERMISSION_DENIED);
1390 } else {
1391 handlers[i].handler(id);
1392 }
1393 break;
1394 }
1395 }
1396 if (handlers[i].handler == NULL)
1397 error("Unknown message %u", type);
Damien Miller7b28dc52000-09-05 13:34:53 +11001398 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001399 /* discard the remaining bytes from the current packet */
Damien Millerdfc24252008-02-10 22:29:40 +11001400 if (buf_len < buffer_len(&iqueue)) {
1401 error("iqueue grew unexpectedly");
1402 sftp_server_cleanup_exit(255);
1403 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001404 consumed = buf_len - buffer_len(&iqueue);
Damien Millerdfc24252008-02-10 22:29:40 +11001405 if (msg_len < consumed) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001406 error("msg_len %u < consumed %u", msg_len, consumed);
Damien Millerdfc24252008-02-10 22:29:40 +11001407 sftp_server_cleanup_exit(255);
1408 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001409 if (msg_len > consumed)
1410 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001411}
1412
Damien Millerfef95ad2006-07-10 20:46:55 +10001413/* Cleanup handler that logs active handles upon normal exit */
1414void
Damien Millerdfc24252008-02-10 22:29:40 +11001415sftp_server_cleanup_exit(int i)
Damien Millerfef95ad2006-07-10 20:46:55 +10001416{
1417 if (pw != NULL && client_addr != NULL) {
1418 handle_log_exit();
1419 logit("session closed for local user %s from [%s]",
1420 pw->pw_name, client_addr);
1421 }
1422 _exit(i);
1423}
1424
1425static void
Damien Millerdfc24252008-02-10 22:29:40 +11001426sftp_server_usage(void)
Damien Millerfef95ad2006-07-10 20:46:55 +10001427{
1428 extern char *__progname;
1429
1430 fprintf(stderr,
Damien Milleraa7ad302013-01-09 15:58:21 +11001431 "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
Damien Miller6efab272013-10-15 12:07:05 +11001432 "[-l log_level]\n\t[-P blacklisted_requests] "
1433 "[-p whitelisted_requests] [-u umask]\n"
1434 " %s -Q protocol_feature\n",
1435 __progname, __progname);
Damien Millerfef95ad2006-07-10 20:46:55 +10001436 exit(1);
1437}
1438
Damien Miller7b28dc52000-09-05 13:34:53 +11001439int
Damien Millerd8cb1f12008-02-10 22:40:12 +11001440sftp_server_main(int argc, char **argv, struct passwd *user_pw)
Damien Miller7b28dc52000-09-05 13:34:53 +11001441{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001442 fd_set *rset, *wset;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001443 int i, in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001444 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001445 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
Damien Miller502ab0e2013-01-09 15:57:36 +11001446 char *cp, *homedir = NULL, buf[4*4096];
Damien Miller07331212010-11-05 10:20:31 +11001447 long mask;
Damien Millerfef95ad2006-07-10 20:46:55 +10001448
Damien Millerfef95ad2006-07-10 20:46:55 +10001449 extern char *optarg;
1450 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001451
Damien Millerfef95ad2006-07-10 20:46:55 +10001452 __progname = ssh_get_progname(argv[0]);
1453 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001454
Damien Miller502ab0e2013-01-09 15:57:36 +11001455 pw = pwcopy(user_pw);
1456
Damien Miller6eaeebf2013-10-15 11:55:57 +11001457 while (!skipargs && (ch = getopt(argc, argv,
1458 "d:f:l:P:p:Q:u:cehR")) != -1) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001459 switch (ch) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001460 case 'Q':
1461 if (strcasecmp(optarg, "requests") != 0) {
1462 fprintf(stderr, "Invalid query type\n");
1463 exit(1);
1464 }
1465 for (i = 0; handlers[i].handler != NULL; i++)
1466 printf("%s\n", handlers[i].name);
1467 for (i = 0; extended_handlers[i].handler != NULL; i++)
1468 printf("%s\n", extended_handlers[i].name);
1469 exit(0);
1470 break;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001471 case 'R':
1472 readonly = 1;
1473 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001474 case 'c':
1475 /*
1476 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001477 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001478 */
1479 skipargs = 1;
1480 break;
1481 case 'e':
1482 log_stderr = 1;
1483 break;
1484 case 'l':
1485 log_level = log_level_number(optarg);
1486 if (log_level == SYSLOG_LEVEL_NOT_SET)
1487 error("Invalid log level \"%s\"", optarg);
1488 break;
1489 case 'f':
1490 log_facility = log_facility_number(optarg);
Damien Miller35e18db2007-09-17 16:11:33 +10001491 if (log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Millerfef95ad2006-07-10 20:46:55 +10001492 error("Invalid log facility \"%s\"", optarg);
1493 break;
Damien Miller502ab0e2013-01-09 15:57:36 +11001494 case 'd':
1495 cp = tilde_expand_filename(optarg, user_pw->pw_uid);
1496 homedir = percent_expand(cp, "d", user_pw->pw_dir,
1497 "u", user_pw->pw_name, (char *)NULL);
1498 free(cp);
1499 break;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001500 case 'p':
1501 if (request_whitelist != NULL)
1502 fatal("Permitted requests already set");
1503 request_whitelist = xstrdup(optarg);
1504 break;
1505 case 'P':
1506 if (request_blacklist != NULL)
1507 fatal("Refused requests already set");
1508 request_blacklist = xstrdup(optarg);
1509 break;
Darren Tucker7dc48502009-10-07 08:44:42 +11001510 case 'u':
Damien Miller07331212010-11-05 10:20:31 +11001511 errno = 0;
1512 mask = strtol(optarg, &cp, 8);
1513 if (mask < 0 || mask > 0777 || *cp != '\0' ||
1514 cp == optarg || (mask == 0 && errno != 0))
1515 fatal("Invalid umask \"%s\"", optarg);
1516 (void)umask((mode_t)mask);
Darren Tucker7dc48502009-10-07 08:44:42 +11001517 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001518 case 'h':
1519 default:
Damien Millerdfc24252008-02-10 22:29:40 +11001520 sftp_server_usage();
Damien Millerfef95ad2006-07-10 20:46:55 +10001521 }
1522 }
1523
1524 log_init(__progname, log_level, log_facility, log_stderr);
1525
1526 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1527 client_addr = xstrdup(cp);
Damien Millerdfc24252008-02-10 22:29:40 +11001528 if ((cp = strchr(client_addr, ' ')) == NULL) {
1529 error("Malformed SSH_CONNECTION variable: \"%s\"",
Damien Millerfef95ad2006-07-10 20:46:55 +10001530 getenv("SSH_CONNECTION"));
Damien Millerdfc24252008-02-10 22:29:40 +11001531 sftp_server_cleanup_exit(255);
1532 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001533 *cp = '\0';
1534 } else
1535 client_addr = xstrdup("UNKNOWN");
1536
Damien Millerfef95ad2006-07-10 20:46:55 +10001537 logit("session opened for local user %s from [%s]",
1538 pw->pw_name, client_addr);
1539
Darren Tuckeraaf51d22010-01-08 19:04:49 +11001540 in = STDIN_FILENO;
1541 out = STDOUT_FILENO;
Damien Miller7b28dc52000-09-05 13:34:53 +11001542
Damien Miller402b3312001-04-14 00:28:42 +10001543#ifdef HAVE_CYGWIN
1544 setmode(in, O_BINARY);
1545 setmode(out, O_BINARY);
1546#endif
1547
Damien Miller7b28dc52000-09-05 13:34:53 +11001548 max = 0;
1549 if (in > max)
1550 max = in;
1551 if (out > max)
1552 max = out;
1553
1554 buffer_init(&iqueue);
1555 buffer_init(&oqueue);
1556
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001557 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1558 rset = (fd_set *)xmalloc(set_size);
1559 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001560
Damien Miller502ab0e2013-01-09 15:57:36 +11001561 if (homedir != NULL) {
1562 if (chdir(homedir) != 0) {
1563 error("chdir to \"%s\" failed: %s", homedir,
1564 strerror(errno));
1565 }
1566 }
1567
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001568 for (;;) {
1569 memset(rset, 0, set_size);
1570 memset(wset, 0, set_size);
1571
Darren Tuckere9405982007-05-20 15:09:04 +10001572 /*
1573 * Ensure that we can read a full buffer and handle
1574 * the worst-case length packet it can generate,
1575 * otherwise apply backpressure by stopping reads.
1576 */
1577 if (buffer_check_alloc(&iqueue, sizeof(buf)) &&
1578 buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1579 FD_SET(in, rset);
1580
Damien Miller7b28dc52000-09-05 13:34:53 +11001581 olen = buffer_len(&oqueue);
1582 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001583 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001584
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001585 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001586 if (errno == EINTR)
1587 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001588 error("select: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001589 sftp_server_cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001590 }
1591
1592 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001593 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001594 len = read(in, buf, sizeof buf);
1595 if (len == 0) {
1596 debug("read eof");
Damien Millerdfc24252008-02-10 22:29:40 +11001597 sftp_server_cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001598 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001599 error("read: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001600 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001601 } else {
1602 buffer_append(&iqueue, buf, len);
1603 }
1604 }
1605 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001606 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001607 len = write(out, buffer_ptr(&oqueue), olen);
1608 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001609 error("write: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001610 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001611 } else {
1612 buffer_consume(&oqueue, len);
1613 }
1614 }
Darren Tuckere9405982007-05-20 15:09:04 +10001615
1616 /*
1617 * Process requests from client if we can fit the results
1618 * into the output buffer, otherwise stop processing input
1619 * and let the output queue drain.
1620 */
1621 if (buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1622 process();
Damien Miller7b28dc52000-09-05 13:34:53 +11001623 }
1624}