blob: 3056c454e02c87e4275997a8fc8ad3afd8a08c7a [file] [log] [blame]
Damien Millere9fc72e2013-10-15 12:14:12 +11001/* $OpenBSD: sftp-server.c,v 1.101 2013/10/14 23:28:23 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);
115static void process_extended(u_int32_t id);
116
117struct sftp_handler {
118 const char *name; /* user-visible name for fine-grained perms */
119 const char *ext_name; /* extended request name */
120 u_int type; /* packet type, for non extended packets */
121 void (*handler)(u_int32_t);
122 int does_write; /* if nonzero, banned for readonly mode */
123};
124
125struct sftp_handler handlers[] = {
126 /* NB. SSH2_FXP_OPEN does the readonly check in the handler itself */
127 { "open", NULL, SSH2_FXP_OPEN, process_open, 0 },
128 { "close", NULL, SSH2_FXP_CLOSE, process_close, 0 },
129 { "read", NULL, SSH2_FXP_READ, process_read, 0 },
130 { "write", NULL, SSH2_FXP_WRITE, process_write, 1 },
131 { "lstat", NULL, SSH2_FXP_LSTAT, process_lstat, 0 },
132 { "fstat", NULL, SSH2_FXP_FSTAT, process_fstat, 0 },
133 { "setstat", NULL, SSH2_FXP_SETSTAT, process_setstat, 1 },
134 { "fsetstat", NULL, SSH2_FXP_FSETSTAT, process_fsetstat, 1 },
135 { "opendir", NULL, SSH2_FXP_OPENDIR, process_opendir, 0 },
136 { "readdir", NULL, SSH2_FXP_READDIR, process_readdir, 0 },
137 { "remove", NULL, SSH2_FXP_REMOVE, process_remove, 1 },
138 { "mkdir", NULL, SSH2_FXP_MKDIR, process_mkdir, 1 },
139 { "rmdir", NULL, SSH2_FXP_RMDIR, process_rmdir, 1 },
140 { "realpath", NULL, SSH2_FXP_REALPATH, process_realpath, 0 },
141 { "stat", NULL, SSH2_FXP_STAT, process_stat, 0 },
142 { "rename", NULL, SSH2_FXP_RENAME, process_rename, 1 },
143 { "readlink", NULL, SSH2_FXP_READLINK, process_readlink, 0 },
144 { "symlink", NULL, SSH2_FXP_SYMLINK, process_symlink, 1 },
145 { NULL, NULL, 0, NULL, 0 }
146};
147
148/* SSH2_FXP_EXTENDED submessages */
149struct sftp_handler extended_handlers[] = {
150 { "posix-rename", "posix-rename@openssh.com", 0,
151 process_extended_posix_rename, 1 },
152 { "statvfs", "statvfs@openssh.com", 0, process_extended_statvfs, 0 },
153 { "fstatvfs", "fstatvfs@openssh.com", 0, process_extended_fstatvfs, 0 },
154 { "hardlink", "hardlink@openssh.com", 0, process_extended_hardlink, 1 },
155 { NULL, NULL, 0, NULL, 0 }
156};
157
158static int
159request_permitted(struct sftp_handler *h)
160{
161 char *result;
162
163 if (readonly && h->does_write) {
164 verbose("Refusing %s request in read-only mode", h->name);
165 return 0;
166 }
167 if (request_blacklist != NULL &&
168 ((result = match_list(h->name, request_blacklist, NULL))) != NULL) {
169 free(result);
170 verbose("Refusing blacklisted %s request", h->name);
171 return 0;
172 }
173 if (request_whitelist != NULL &&
174 ((result = match_list(h->name, request_whitelist, NULL))) != NULL) {
175 free(result);
176 debug2("Permitting whitelisted %s request", h->name);
177 return 1;
178 }
179 if (request_whitelist != NULL) {
180 verbose("Refusing non-whitelisted %s request", h->name);
181 return 0;
182 }
183 return 1;
184}
185
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100187errno_to_portable(int unixerrno)
188{
189 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000190
Damien Miller7b28dc52000-09-05 13:34:53 +1100191 switch (unixerrno) {
192 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000193 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100194 break;
195 case ENOENT:
196 case ENOTDIR:
197 case EBADF:
198 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000199 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100200 break;
201 case EPERM:
202 case EACCES:
203 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000204 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +1100205 break;
206 case ENAMETOOLONG:
207 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000208 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100209 break;
Darren Tucker422c34c2008-06-09 22:48:31 +1000210 case ENOSYS:
211 ret = SSH2_FX_OP_UNSUPPORTED;
212 break;
Damien Miller7b28dc52000-09-05 13:34:53 +1100213 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000214 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100215 break;
216 }
217 return ret;
218}
219
Ben Lindstrombba81212001-06-25 05:01:22 +0000220static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100221flags_from_portable(int pflags)
222{
223 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000224
Ben Lindstrom36592512001-03-05 05:02:08 +0000225 if ((pflags & SSH2_FXF_READ) &&
226 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100227 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000228 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100229 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000230 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100231 flags = O_WRONLY;
232 }
Damien Millere9fc72e2013-10-15 12:14:12 +1100233 if (pflags & SSH2_FXF_APPEND)
234 flags |= O_APPEND;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000235 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100236 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000237 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100238 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000239 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100240 flags |= O_EXCL;
241 return flags;
242}
243
Damien Millerfef95ad2006-07-10 20:46:55 +1000244static const char *
245string_from_portable(int pflags)
246{
247 static char ret[128];
248
249 *ret = '\0';
250
251#define PAPPEND(str) { \
252 if (*ret != '\0') \
253 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000254 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000255 }
256
257 if (pflags & SSH2_FXF_READ)
258 PAPPEND("READ")
259 if (pflags & SSH2_FXF_WRITE)
260 PAPPEND("WRITE")
Damien Millere9fc72e2013-10-15 12:14:12 +1100261 if (pflags & SSH2_FXF_APPEND)
262 PAPPEND("APPEND")
Damien Millerfef95ad2006-07-10 20:46:55 +1000263 if (pflags & SSH2_FXF_CREAT)
264 PAPPEND("CREATE")
265 if (pflags & SSH2_FXF_TRUNC)
266 PAPPEND("TRUNCATE")
267 if (pflags & SSH2_FXF_EXCL)
268 PAPPEND("EXCL")
269
270 return ret;
271}
272
Ben Lindstrombba81212001-06-25 05:01:22 +0000273static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100274get_attrib(void)
275{
276 return decode_attrib(&iqueue);
277}
278
279/* handle handles */
280
281typedef struct Handle Handle;
282struct Handle {
283 int use;
284 DIR *dirp;
285 int fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100286 int flags;
Damien Miller7b28dc52000-09-05 13:34:53 +1100287 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000288 u_int64_t bytes_read, bytes_write;
Damien Miller3397d0e2008-02-10 22:26:51 +1100289 int next_unused;
Damien Miller7b28dc52000-09-05 13:34:53 +1100290};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000291
Damien Miller7b28dc52000-09-05 13:34:53 +1100292enum {
293 HANDLE_UNUSED,
294 HANDLE_DIR,
295 HANDLE_FILE
296};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000297
Damien Miller3397d0e2008-02-10 22:26:51 +1100298Handle *handles = NULL;
299u_int num_handles = 0;
300int first_unused_handle = -1;
Damien Miller7b28dc52000-09-05 13:34:53 +1100301
Damien Miller3397d0e2008-02-10 22:26:51 +1100302static void handle_unused(int i)
Damien Miller7b28dc52000-09-05 13:34:53 +1100303{
Damien Miller3397d0e2008-02-10 22:26:51 +1100304 handles[i].use = HANDLE_UNUSED;
305 handles[i].next_unused = first_unused_handle;
306 first_unused_handle = i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100307}
308
Ben Lindstrombba81212001-06-25 05:01:22 +0000309static int
Damien Millere9fc72e2013-10-15 12:14:12 +1100310handle_new(int use, const char *name, int fd, int flags, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100311{
Damien Miller3397d0e2008-02-10 22:26:51 +1100312 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000313
Damien Miller3397d0e2008-02-10 22:26:51 +1100314 if (first_unused_handle == -1) {
315 if (num_handles + 1 <= num_handles)
316 return -1;
317 num_handles++;
318 handles = xrealloc(handles, num_handles, sizeof(Handle));
319 handle_unused(num_handles - 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100320 }
Damien Miller3397d0e2008-02-10 22:26:51 +1100321
322 i = first_unused_handle;
323 first_unused_handle = handles[i].next_unused;
324
325 handles[i].use = use;
326 handles[i].dirp = dirp;
327 handles[i].fd = fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100328 handles[i].flags = flags;
Damien Miller3397d0e2008-02-10 22:26:51 +1100329 handles[i].name = xstrdup(name);
330 handles[i].bytes_read = handles[i].bytes_write = 0;
331
332 return i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100333}
334
Ben Lindstrombba81212001-06-25 05:01:22 +0000335static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100336handle_is_ok(int i, int type)
337{
Damien Miller3397d0e2008-02-10 22:26:51 +1100338 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100339}
340
Ben Lindstrombba81212001-06-25 05:01:22 +0000341static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100342handle_to_string(int handle, char **stringp, int *hlenp)
343{
Damien Miller7b28dc52000-09-05 13:34:53 +1100344 if (stringp == NULL || hlenp == NULL)
345 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000346 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100347 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000348 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100349 return 0;
350}
351
Ben Lindstrombba81212001-06-25 05:01:22 +0000352static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100353handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100354{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000355 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000356
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000357 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100358 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100359 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100360 if (handle_is_ok(val, HANDLE_FILE) ||
361 handle_is_ok(val, HANDLE_DIR))
362 return val;
363 return -1;
364}
365
Ben Lindstrombba81212001-06-25 05:01:22 +0000366static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100367handle_to_name(int handle)
368{
369 if (handle_is_ok(handle, HANDLE_DIR)||
370 handle_is_ok(handle, HANDLE_FILE))
371 return handles[handle].name;
372 return NULL;
373}
374
Ben Lindstrombba81212001-06-25 05:01:22 +0000375static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100376handle_to_dir(int handle)
377{
378 if (handle_is_ok(handle, HANDLE_DIR))
379 return handles[handle].dirp;
380 return NULL;
381}
382
Ben Lindstrombba81212001-06-25 05:01:22 +0000383static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100384handle_to_fd(int handle)
385{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000386 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100387 return handles[handle].fd;
388 return -1;
389}
390
Damien Millere9fc72e2013-10-15 12:14:12 +1100391static int
392handle_to_flags(int handle)
393{
394 if (handle_is_ok(handle, HANDLE_FILE))
395 return handles[handle].flags;
396 return 0;
397}
398
Damien Millerfef95ad2006-07-10 20:46:55 +1000399static void
400handle_update_read(int handle, ssize_t bytes)
401{
402 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
403 handles[handle].bytes_read += bytes;
404}
405
406static void
407handle_update_write(int handle, ssize_t bytes)
408{
409 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
410 handles[handle].bytes_write += bytes;
411}
412
413static u_int64_t
414handle_bytes_read(int handle)
415{
416 if (handle_is_ok(handle, HANDLE_FILE))
417 return (handles[handle].bytes_read);
418 return 0;
419}
420
421static u_int64_t
422handle_bytes_write(int handle)
423{
424 if (handle_is_ok(handle, HANDLE_FILE))
425 return (handles[handle].bytes_write);
426 return 0;
427}
428
Ben Lindstrombba81212001-06-25 05:01:22 +0000429static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100430handle_close(int handle)
431{
432 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000433
Damien Miller7b28dc52000-09-05 13:34:53 +1100434 if (handle_is_ok(handle, HANDLE_FILE)) {
435 ret = close(handles[handle].fd);
Darren Tuckera627d422013-06-02 07:31:17 +1000436 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100437 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100438 } else if (handle_is_ok(handle, HANDLE_DIR)) {
439 ret = closedir(handles[handle].dirp);
Darren Tuckera627d422013-06-02 07:31:17 +1000440 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100441 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100442 } else {
443 errno = ENOENT;
444 }
445 return ret;
446}
447
Damien Millerfef95ad2006-07-10 20:46:55 +1000448static void
449handle_log_close(int handle, char *emsg)
450{
451 if (handle_is_ok(handle, HANDLE_FILE)) {
452 logit("%s%sclose \"%s\" bytes read %llu written %llu",
453 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
454 handle_to_name(handle),
Darren Tucker86473c52007-05-20 14:59:32 +1000455 (unsigned long long)handle_bytes_read(handle),
456 (unsigned long long)handle_bytes_write(handle));
Damien Millerfef95ad2006-07-10 20:46:55 +1000457 } else {
458 logit("%s%sclosedir \"%s\"",
459 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
460 handle_to_name(handle));
461 }
462}
463
464static void
465handle_log_exit(void)
466{
467 u_int i;
468
Damien Miller3397d0e2008-02-10 22:26:51 +1100469 for (i = 0; i < num_handles; i++)
Damien Millerfef95ad2006-07-10 20:46:55 +1000470 if (handles[i].use != HANDLE_UNUSED)
471 handle_log_close(i, "forced");
472}
473
Ben Lindstrombba81212001-06-25 05:01:22 +0000474static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100475get_handle(void)
476{
477 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000478 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100479 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000480
Damien Miller7b28dc52000-09-05 13:34:53 +1100481 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000482 if (hlen < 256)
483 val = handle_from_string(handle, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000484 free(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100485 return val;
486}
487
488/* send replies */
489
Ben Lindstrombba81212001-06-25 05:01:22 +0000490static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100491send_msg(Buffer *m)
492{
493 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000494
Damien Miller7b28dc52000-09-05 13:34:53 +1100495 buffer_put_int(&oqueue, mlen);
496 buffer_append(&oqueue, buffer_ptr(m), mlen);
497 buffer_consume(m, mlen);
498}
499
Damien Millerfef95ad2006-07-10 20:46:55 +1000500static const char *
501status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100502{
Damien Miller058316f2001-03-08 10:08:49 +1100503 const char *status_messages[] = {
504 "Success", /* SSH_FX_OK */
505 "End of file", /* SSH_FX_EOF */
506 "No such file", /* SSH_FX_NO_SUCH_FILE */
507 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
508 "Failure", /* SSH_FX_FAILURE */
509 "Bad message", /* SSH_FX_BAD_MESSAGE */
510 "No connection", /* SSH_FX_NO_CONNECTION */
511 "Connection lost", /* SSH_FX_CONNECTION_LOST */
512 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
513 "Unknown error" /* Others */
514 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000515 return (status_messages[MIN(status,SSH2_FX_MAX)]);
516}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000517
Damien Millerfef95ad2006-07-10 20:46:55 +1000518static void
519send_status(u_int32_t id, u_int32_t status)
520{
521 Buffer msg;
522
523 debug3("request %u: sent status %u", id, status);
524 if (log_level > SYSLOG_LEVEL_VERBOSE ||
525 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
526 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100527 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000528 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100529 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000530 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100531 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000532 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100533 buffer_put_cstring(&msg, "");
534 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100535 send_msg(&msg);
536 buffer_free(&msg);
537}
Ben Lindstrombba81212001-06-25 05:01:22 +0000538static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100539send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100540{
541 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000542
Damien Miller7b28dc52000-09-05 13:34:53 +1100543 buffer_init(&msg);
544 buffer_put_char(&msg, type);
545 buffer_put_int(&msg, id);
546 buffer_put_string(&msg, data, dlen);
547 send_msg(&msg);
548 buffer_free(&msg);
549}
550
Ben Lindstrombba81212001-06-25 05:01:22 +0000551static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100552send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100553{
Damien Millerfef95ad2006-07-10 20:46:55 +1000554 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000555 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100556}
557
Ben Lindstrombba81212001-06-25 05:01:22 +0000558static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100559send_handle(u_int32_t id, int handle)
560{
561 char *string;
562 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000563
Damien Miller7b28dc52000-09-05 13:34:53 +1100564 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000565 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000566 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000567 free(string);
Damien Miller7b28dc52000-09-05 13:34:53 +1100568}
569
Ben Lindstrombba81212001-06-25 05:01:22 +0000570static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100571send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100572{
573 Buffer msg;
574 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000575
Damien Miller7b28dc52000-09-05 13:34:53 +1100576 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000577 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100578 buffer_put_int(&msg, id);
579 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000580 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100581 for (i = 0; i < count; i++) {
582 buffer_put_cstring(&msg, stats[i].name);
583 buffer_put_cstring(&msg, stats[i].long_name);
584 encode_attrib(&msg, &stats[i].attrib);
585 }
586 send_msg(&msg);
587 buffer_free(&msg);
588}
589
Ben Lindstrombba81212001-06-25 05:01:22 +0000590static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100591send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100592{
593 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000594
Damien Millerfef95ad2006-07-10 20:46:55 +1000595 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100596 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000597 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100598 buffer_put_int(&msg, id);
599 encode_attrib(&msg, a);
600 send_msg(&msg);
601 buffer_free(&msg);
602}
603
Damien Millerd671e5a2008-05-19 14:53:33 +1000604static void
605send_statvfs(u_int32_t id, struct statvfs *st)
606{
607 Buffer msg;
608 u_int64_t flag;
609
610 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
611 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
612
613 buffer_init(&msg);
614 buffer_put_char(&msg, SSH2_FXP_EXTENDED_REPLY);
615 buffer_put_int(&msg, id);
Darren Tucker3463aca2008-06-09 23:06:55 +1000616 buffer_put_int64(&msg, st->f_bsize);
617 buffer_put_int64(&msg, st->f_frsize);
Damien Millerd671e5a2008-05-19 14:53:33 +1000618 buffer_put_int64(&msg, st->f_blocks);
619 buffer_put_int64(&msg, st->f_bfree);
620 buffer_put_int64(&msg, st->f_bavail);
621 buffer_put_int64(&msg, st->f_files);
622 buffer_put_int64(&msg, st->f_ffree);
623 buffer_put_int64(&msg, st->f_favail);
Darren Tucker77001382008-06-09 06:17:53 +1000624 buffer_put_int64(&msg, FSID_TO_ULONG(st->f_fsid));
Darren Tucker3463aca2008-06-09 23:06:55 +1000625 buffer_put_int64(&msg, flag);
626 buffer_put_int64(&msg, st->f_namemax);
Damien Millerd671e5a2008-05-19 14:53:33 +1000627 send_msg(&msg);
628 buffer_free(&msg);
629}
630
Damien Miller7b28dc52000-09-05 13:34:53 +1100631/* parse incoming */
632
Ben Lindstrombba81212001-06-25 05:01:22 +0000633static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100634process_init(void)
635{
636 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100637
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000638 version = get_int();
Damien Millerf145a5b2011-06-20 14:42:51 +1000639 verbose("received client version %u", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100640 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000641 buffer_put_char(&msg, SSH2_FXP_VERSION);
642 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7c296612008-03-07 18:33:53 +1100643 /* POSIX rename extension */
644 buffer_put_cstring(&msg, "posix-rename@openssh.com");
645 buffer_put_cstring(&msg, "1"); /* version */
Damien Millera7e0d5a2008-05-19 16:08:41 +1000646 /* statvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000647 buffer_put_cstring(&msg, "statvfs@openssh.com");
Darren Tucker294b8412008-06-08 12:57:08 +1000648 buffer_put_cstring(&msg, "2"); /* version */
Damien Millera7e0d5a2008-05-19 16:08:41 +1000649 /* fstatvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000650 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
Darren Tucker294b8412008-06-08 12:57:08 +1000651 buffer_put_cstring(&msg, "2"); /* version */
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100652 /* hardlink extension */
653 buffer_put_cstring(&msg, "hardlink@openssh.com");
654 buffer_put_cstring(&msg, "1"); /* version */
Damien Miller7b28dc52000-09-05 13:34:53 +1100655 send_msg(&msg);
656 buffer_free(&msg);
657}
658
Ben Lindstrombba81212001-06-25 05:01:22 +0000659static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100660process_open(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100661{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100662 u_int32_t pflags;
Damien Miller7b28dc52000-09-05 13:34:53 +1100663 Attrib *a;
664 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000665 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100666
Damien Miller7b28dc52000-09-05 13:34:53 +1100667 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000668 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000669 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100670 a = get_attrib();
671 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000672 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000673 logit("open \"%s\" flags %s mode 0%o",
674 name, string_from_portable(pflags), mode);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100675 if (readonly &&
Damien Miller6eaeebf2013-10-15 11:55:57 +1100676 ((flags & O_ACCMODE) == O_WRONLY ||
677 (flags & O_ACCMODE) == O_RDWR)) {
678 verbose("Refusing open request in read-only mode");
679 status = SSH2_FX_PERMISSION_DENIED;
680 } else {
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100681 fd = open(name, flags, mode);
682 if (fd < 0) {
683 status = errno_to_portable(errno);
Damien Miller7b28dc52000-09-05 13:34:53 +1100684 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100685 handle = handle_new(HANDLE_FILE, name, fd, flags, NULL);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100686 if (handle < 0) {
687 close(fd);
688 } else {
689 send_handle(id, handle);
690 status = SSH2_FX_OK;
691 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100692 }
693 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000694 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100695 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000696 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100697}
698
Ben Lindstrombba81212001-06-25 05:01:22 +0000699static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100700process_close(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100701{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000702 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100703
Damien Miller7b28dc52000-09-05 13:34:53 +1100704 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000705 debug3("request %u: close handle %u", id, handle);
706 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100707 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000708 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100709 send_status(id, status);
710}
711
Ben Lindstrombba81212001-06-25 05:01:22 +0000712static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100713process_read(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100714{
715 char buf[64*1024];
Damien Miller6eaeebf2013-10-15 11:55:57 +1100716 u_int32_t len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000717 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100718 u_int64_t off;
719
Damien Miller7b28dc52000-09-05 13:34:53 +1100720 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000721 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100722 len = get_int();
723
Damien Millerfef95ad2006-07-10 20:46:55 +1000724 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
725 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100726 if (len > sizeof buf) {
727 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000728 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100729 }
730 fd = handle_to_fd(handle);
731 if (fd >= 0) {
732 if (lseek(fd, off, SEEK_SET) < 0) {
733 error("process_read: seek failed");
734 status = errno_to_portable(errno);
735 } else {
736 ret = read(fd, buf, len);
737 if (ret < 0) {
738 status = errno_to_portable(errno);
739 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000740 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100741 } else {
742 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000743 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000744 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100745 }
746 }
747 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000748 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100749 send_status(id, status);
750}
751
Ben Lindstrombba81212001-06-25 05:01:22 +0000752static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100753process_write(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100754{
Damien Miller7b28dc52000-09-05 13:34:53 +1100755 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100756 u_int len;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100757 int handle, fd, ret, status;
Damien Miller7b28dc52000-09-05 13:34:53 +1100758 char *data;
759
Damien Miller7b28dc52000-09-05 13:34:53 +1100760 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000761 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100762 data = get_string(&len);
763
Damien Millerfef95ad2006-07-10 20:46:55 +1000764 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
765 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100766 fd = handle_to_fd(handle);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100767
768 if (fd < 0)
769 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100770 else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100771 if (!(handle_to_flags(handle) & O_APPEND) &&
772 lseek(fd, off, SEEK_SET) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100773 status = errno_to_portable(errno);
774 error("process_write: seek failed");
775 } else {
776/* XXX ATOMICIO ? */
777 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000778 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100779 error("process_write: write failed");
780 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000781 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000782 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000783 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100784 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000785 debug2("nothing at all written");
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100786 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100787 }
788 }
789 }
790 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000791 free(data);
Damien Miller7b28dc52000-09-05 13:34:53 +1100792}
793
Ben Lindstrombba81212001-06-25 05:01:22 +0000794static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100795process_do_stat(u_int32_t id, int do_lstat)
Damien Miller7b28dc52000-09-05 13:34:53 +1100796{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000797 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100798 struct stat st;
Damien Miller7b28dc52000-09-05 13:34:53 +1100799 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000800 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100801
Damien Miller7b28dc52000-09-05 13:34:53 +1100802 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000803 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
804 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100805 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
806 if (ret < 0) {
807 status = errno_to_portable(errno);
808 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000809 stat_to_attrib(&st, &a);
810 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000811 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100812 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000813 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100814 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000815 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100816}
817
Ben Lindstrombba81212001-06-25 05:01:22 +0000818static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100819process_stat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100820{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100821 process_do_stat(id, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100822}
823
Ben Lindstrombba81212001-06-25 05:01:22 +0000824static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100825process_lstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100826{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100827 process_do_stat(id, 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100828}
829
Ben Lindstrombba81212001-06-25 05:01:22 +0000830static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100831process_fstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100832{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000833 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100834 struct stat st;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000835 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100836
Damien Miller7b28dc52000-09-05 13:34:53 +1100837 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000838 debug("request %u: fstat \"%s\" (handle %u)",
839 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100840 fd = handle_to_fd(handle);
Damien Millere2334d62007-01-05 16:31:02 +1100841 if (fd >= 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100842 ret = fstat(fd, &st);
843 if (ret < 0) {
844 status = errno_to_portable(errno);
845 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000846 stat_to_attrib(&st, &a);
847 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000848 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100849 }
850 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000851 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100852 send_status(id, status);
853}
854
Ben Lindstrombba81212001-06-25 05:01:22 +0000855static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100856attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100857{
858 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000859
Damien Miller7b28dc52000-09-05 13:34:53 +1100860 tv[0].tv_sec = a->atime;
861 tv[0].tv_usec = 0;
862 tv[1].tv_sec = a->mtime;
863 tv[1].tv_usec = 0;
864 return tv;
865}
866
Ben Lindstrombba81212001-06-25 05:01:22 +0000867static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100868process_setstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100869{
870 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100871 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000872 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100873
Damien Miller7b28dc52000-09-05 13:34:53 +1100874 name = get_string(NULL);
875 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000876 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100877 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000878 logit("set \"%s\" size %llu",
879 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100880 ret = truncate(name, a->size);
881 if (ret == -1)
882 status = errno_to_portable(errno);
883 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000884 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000885 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller9e720282008-06-29 22:46:35 +1000886 ret = chmod(name, a->perm & 07777);
Damien Miller7b28dc52000-09-05 13:34:53 +1100887 if (ret == -1)
888 status = errno_to_portable(errno);
889 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000890 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000891 char buf[64];
892 time_t t = a->mtime;
893
894 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
895 localtime(&t));
896 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100897 ret = utimes(name, attrib_to_tv(a));
898 if (ret == -1)
899 status = errno_to_portable(errno);
900 }
Kevin Steves8e743932001-02-05 13:24:35 +0000901 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000902 logit("set \"%s\" owner %lu group %lu", name,
903 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000904 ret = chown(name, a->uid, a->gid);
905 if (ret == -1)
906 status = errno_to_portable(errno);
907 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100908 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000909 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100910}
911
Ben Lindstrombba81212001-06-25 05:01:22 +0000912static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100913process_fsetstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100914{
915 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100916 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000917 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000918
Damien Miller7b28dc52000-09-05 13:34:53 +1100919 handle = get_handle();
920 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000921 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100922 fd = handle_to_fd(handle);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100923 if (fd < 0)
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000924 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100925 else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000926 char *name = handle_to_name(handle);
927
Damien Miller00c92172002-02-13 14:05:00 +1100928 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000929 logit("set \"%s\" size %llu",
930 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100931 ret = ftruncate(fd, a->size);
932 if (ret == -1)
933 status = errno_to_portable(errno);
934 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000935 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000936 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000937#ifdef HAVE_FCHMOD
Damien Miller9e720282008-06-29 22:46:35 +1000938 ret = fchmod(fd, a->perm & 07777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000939#else
Damien Miller9e720282008-06-29 22:46:35 +1000940 ret = chmod(name, a->perm & 07777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000941#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100942 if (ret == -1)
943 status = errno_to_portable(errno);
944 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000945 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000946 char buf[64];
947 time_t t = a->mtime;
948
949 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
950 localtime(&t));
951 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100952#ifdef HAVE_FUTIMES
953 ret = futimes(fd, attrib_to_tv(a));
954#else
955 ret = utimes(name, attrib_to_tv(a));
956#endif
957 if (ret == -1)
958 status = errno_to_portable(errno);
959 }
Kevin Steves8e743932001-02-05 13:24:35 +0000960 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000961 logit("set \"%s\" owner %lu group %lu", name,
962 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000963#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000964 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000965#else
966 ret = chown(name, a->uid, a->gid);
967#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000968 if (ret == -1)
969 status = errno_to_portable(errno);
970 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100971 }
972 send_status(id, status);
973}
974
Ben Lindstrombba81212001-06-25 05:01:22 +0000975static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100976process_opendir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100977{
978 DIR *dirp = NULL;
979 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000980 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100981
Damien Miller7b28dc52000-09-05 13:34:53 +1100982 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000983 debug3("request %u: opendir", id);
984 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000985 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100986 if (dirp == NULL) {
987 status = errno_to_portable(errno);
988 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100989 handle = handle_new(HANDLE_DIR, path, 0, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100990 if (handle < 0) {
991 closedir(dirp);
992 } else {
993 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000994 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100995 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000996
Damien Miller7b28dc52000-09-05 13:34:53 +1100997 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000998 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100999 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001000 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001001}
1002
Ben Lindstrombba81212001-06-25 05:01:22 +00001003static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001004process_readdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001005{
1006 DIR *dirp;
1007 struct dirent *dp;
1008 char *path;
1009 int handle;
Damien Miller7b28dc52000-09-05 13:34:53 +11001010
Damien Miller7b28dc52000-09-05 13:34:53 +11001011 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +10001012 debug("request %u: readdir \"%s\" (handle %d)", id,
1013 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +11001014 dirp = handle_to_dir(handle);
1015 path = handle_to_name(handle);
1016 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001017 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +11001018 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +11001019 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +10001020 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +11001021 Stat *stats;
1022 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001023
Damien Miller07d86be2006-03-26 14:19:21 +11001024 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001025 while ((dp = readdir(dirp)) != NULL) {
1026 if (count >= nstats) {
1027 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +11001028 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001029 }
1030/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +00001031 snprintf(pathname, sizeof pathname, "%s%s%s", path,
1032 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001033 if (lstat(pathname, &st) < 0)
1034 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001035 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +11001036 stats[count].name = xstrdup(dp->d_name);
Darren Tucker2901e2d2010-01-13 22:44:06 +11001037 stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001038 count++;
1039 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001040 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +11001041 if (count == 100)
1042 break;
1043 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001044 if (count > 0) {
1045 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001046 for (i = 0; i < count; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001047 free(stats[i].name);
1048 free(stats[i].long_name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001049 }
1050 } else {
1051 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +11001052 }
Darren Tuckera627d422013-06-02 07:31:17 +10001053 free(stats);
Damien Miller7b28dc52000-09-05 13:34:53 +11001054 }
1055}
1056
Ben Lindstrombba81212001-06-25 05:01:22 +00001057static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001058process_remove(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001059{
1060 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001061 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001062 int ret;
1063
Damien Miller7b28dc52000-09-05 13:34:53 +11001064 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001065 debug3("request %u: remove", id);
1066 logit("remove name \"%s\"", name);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001067 ret = unlink(name);
1068 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001069 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001070 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001071}
1072
Ben Lindstrombba81212001-06-25 05:01:22 +00001073static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001074process_mkdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001075{
1076 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +11001077 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001078 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001079
Damien Miller7b28dc52000-09-05 13:34:53 +11001080 name = get_string(NULL);
1081 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001082 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
Damien Miller9e720282008-06-29 22:46:35 +10001083 a->perm & 07777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +10001084 debug3("request %u: mkdir", id);
1085 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001086 ret = mkdir(name, mode);
1087 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001088 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001089 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001090}
1091
Ben Lindstrombba81212001-06-25 05:01:22 +00001092static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001093process_rmdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001094{
Damien Miller7b28dc52000-09-05 13:34:53 +11001095 char *name;
1096 int ret, status;
1097
Damien Miller7b28dc52000-09-05 13:34:53 +11001098 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001099 debug3("request %u: rmdir", id);
1100 logit("rmdir name \"%s\"", name);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001101 ret = rmdir(name);
1102 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001103 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001104 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001105}
1106
Ben Lindstrombba81212001-06-25 05:01:22 +00001107static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001108process_realpath(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001109{
1110 char resolvedname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +11001111 char *path;
1112
Damien Miller7b28dc52000-09-05 13:34:53 +11001113 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001114 if (path[0] == '\0') {
Darren Tuckera627d422013-06-02 07:31:17 +10001115 free(path);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001116 path = xstrdup(".");
1117 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001118 debug3("request %u: realpath", id);
1119 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001120 if (realpath(path, resolvedname) == NULL) {
1121 send_status(id, errno_to_portable(errno));
1122 } else {
1123 Stat s;
1124 attrib_clear(&s.attrib);
1125 s.name = s.long_name = resolvedname;
1126 send_names(id, 1, &s);
1127 }
Darren Tuckera627d422013-06-02 07:31:17 +10001128 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001129}
1130
Ben Lindstrombba81212001-06-25 05:01:22 +00001131static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001132process_rename(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001133{
Damien Miller7b28dc52000-09-05 13:34:53 +11001134 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001135 int status;
Damien Millerb3207e82003-03-26 16:01:11 +11001136 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +11001137
Damien Miller7b28dc52000-09-05 13:34:53 +11001138 oldpath = get_string(NULL);
1139 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001140 debug3("request %u: rename", id);
1141 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +11001142 status = SSH2_FX_FAILURE;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001143 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +11001144 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +11001145 else if (S_ISREG(sb.st_mode)) {
1146 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001147 if (link(oldpath, newpath) == -1) {
Damien Miller0e265512009-08-28 10:43:13 +10001148 if (errno == EOPNOTSUPP || errno == ENOSYS
Darren Tuckerf7fa7062008-07-04 14:10:19 +10001149#ifdef EXDEV
1150 || errno == EXDEV
1151#endif
Darren Tuckere59b5082004-06-28 16:01:19 +10001152#ifdef LINK_OPNOTSUPP_ERRNO
1153 || errno == LINK_OPNOTSUPP_ERRNO
1154#endif
1155 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001156 struct stat st;
1157
1158 /*
1159 * fs doesn't support links, so fall back to
1160 * stat+rename. This is racy.
1161 */
1162 if (stat(newpath, &st) == -1) {
1163 if (rename(oldpath, newpath) == -1)
1164 status =
1165 errno_to_portable(errno);
1166 else
1167 status = SSH2_FX_OK;
1168 }
1169 } else {
1170 status = errno_to_portable(errno);
1171 }
1172 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001173 status = errno_to_portable(errno);
1174 /* clean spare link */
1175 unlink(newpath);
1176 } else
1177 status = SSH2_FX_OK;
1178 } else if (stat(newpath, &sb) == -1) {
1179 if (rename(oldpath, newpath) == -1)
1180 status = errno_to_portable(errno);
1181 else
1182 status = SSH2_FX_OK;
1183 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001184 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001185 free(oldpath);
1186 free(newpath);
Damien Miller7b28dc52000-09-05 13:34:53 +11001187}
1188
Ben Lindstrombba81212001-06-25 05:01:22 +00001189static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001190process_readlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001191{
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001192 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001193 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001194 char *path;
1195
Damien Miller058316f2001-03-08 10:08:49 +11001196 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001197 debug3("request %u: readlink", id);
1198 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001199 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001200 send_status(id, errno_to_portable(errno));
1201 else {
1202 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001203
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001204 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001205 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001206 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001207 send_names(id, 1, &s);
1208 }
Darren Tuckera627d422013-06-02 07:31:17 +10001209 free(path);
Damien Miller058316f2001-03-08 10:08:49 +11001210}
1211
Ben Lindstrombba81212001-06-25 05:01:22 +00001212static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001213process_symlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001214{
Damien Miller058316f2001-03-08 10:08:49 +11001215 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001216 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001217
Damien Miller058316f2001-03-08 10:08:49 +11001218 oldpath = get_string(NULL);
1219 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001220 debug3("request %u: symlink", id);
1221 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001222 /* this will fail if 'newpath' exists */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001223 ret = symlink(oldpath, newpath);
1224 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001225 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001226 free(oldpath);
1227 free(newpath);
Damien Miller058316f2001-03-08 10:08:49 +11001228}
1229
Ben Lindstrombba81212001-06-25 05:01:22 +00001230static void
Damien Miller7c296612008-03-07 18:33:53 +11001231process_extended_posix_rename(u_int32_t id)
1232{
1233 char *oldpath, *newpath;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001234 int ret, status;
Damien Miller7c296612008-03-07 18:33:53 +11001235
1236 oldpath = get_string(NULL);
1237 newpath = get_string(NULL);
1238 debug3("request %u: posix-rename", id);
1239 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001240 ret = rename(oldpath, newpath);
1241 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001242 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001243 free(oldpath);
1244 free(newpath);
Damien Miller7c296612008-03-07 18:33:53 +11001245}
1246
1247static void
Damien Millerd671e5a2008-05-19 14:53:33 +10001248process_extended_statvfs(u_int32_t id)
1249{
1250 char *path;
1251 struct statvfs st;
1252
1253 path = get_string(NULL);
1254 debug3("request %u: statfs", id);
1255 logit("statfs \"%s\"", path);
1256
1257 if (statvfs(path, &st) != 0)
1258 send_status(id, errno_to_portable(errno));
1259 else
1260 send_statvfs(id, &st);
Darren Tuckera627d422013-06-02 07:31:17 +10001261 free(path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001262}
1263
1264static void
1265process_extended_fstatvfs(u_int32_t id)
1266{
1267 int handle, fd;
1268 struct statvfs st;
1269
1270 handle = get_handle();
1271 debug("request %u: fstatvfs \"%s\" (handle %u)",
1272 id, handle_to_name(handle), handle);
1273 if ((fd = handle_to_fd(handle)) < 0) {
1274 send_status(id, SSH2_FX_FAILURE);
1275 return;
1276 }
1277 if (fstatvfs(fd, &st) != 0)
1278 send_status(id, errno_to_portable(errno));
1279 else
1280 send_statvfs(id, &st);
1281}
1282
1283static void
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001284process_extended_hardlink(u_int32_t id)
1285{
1286 char *oldpath, *newpath;
1287 int ret, status;
1288
1289 oldpath = get_string(NULL);
1290 newpath = get_string(NULL);
1291 debug3("request %u: hardlink", id);
1292 logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001293 ret = link(oldpath, newpath);
1294 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001295 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001296 free(oldpath);
1297 free(newpath);
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001298}
1299
1300static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001301process_extended(u_int32_t id)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001302{
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001303 char *request;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001304 u_int i;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001305
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001306 request = get_string(NULL);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001307 for (i = 0; extended_handlers[i].handler != NULL; i++) {
1308 if (strcmp(request, extended_handlers[i].ext_name) == 0) {
1309 if (!request_permitted(&extended_handlers[i]))
1310 send_status(id, SSH2_FX_PERMISSION_DENIED);
1311 else
1312 extended_handlers[i].handler(id);
1313 break;
1314 }
1315 }
1316 if (extended_handlers[i].handler == NULL) {
1317 error("Unknown extended request \"%.100s\"", request);
Damien Miller7c296612008-03-07 18:33:53 +11001318 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001319 }
Darren Tuckera627d422013-06-02 07:31:17 +10001320 free(request);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001321}
Damien Miller7b28dc52000-09-05 13:34:53 +11001322
1323/* stolen from ssh-agent */
1324
Ben Lindstrombba81212001-06-25 05:01:22 +00001325static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001326process(void)
1327{
Damien Miller6eaeebf2013-10-15 11:55:57 +11001328 u_int msg_len, buf_len, consumed, type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001329 u_char *cp;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001330 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +11001331
Ben Lindstrom2c140472002-06-06 21:57:54 +00001332 buf_len = buffer_len(&iqueue);
1333 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001334 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001335 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001336 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001337 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001338 error("bad message from %s local user %s",
1339 client_addr, pw->pw_name);
Damien Millerdfc24252008-02-10 22:29:40 +11001340 sftp_server_cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001341 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001342 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001343 return;
1344 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001345 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001346 type = buffer_get_char(&iqueue);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001347
Damien Miller7b28dc52000-09-05 13:34:53 +11001348 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001349 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001350 process_init();
Damien Miller6eaeebf2013-10-15 11:55:57 +11001351 init_done = 1;
Damien Miller058316f2001-03-08 10:08:49 +11001352 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001353 case SSH2_FXP_EXTENDED:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001354 if (!init_done)
1355 fatal("Received extended request before init");
1356 id = get_int();
1357 process_extended(id);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001358 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001359 default:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001360 if (!init_done)
1361 fatal("Received %u request before init", type);
1362 id = get_int();
1363 for (i = 0; handlers[i].handler != NULL; i++) {
1364 if (type == handlers[i].type) {
1365 if (!request_permitted(&handlers[i])) {
1366 send_status(id,
1367 SSH2_FX_PERMISSION_DENIED);
1368 } else {
1369 handlers[i].handler(id);
1370 }
1371 break;
1372 }
1373 }
1374 if (handlers[i].handler == NULL)
1375 error("Unknown message %u", type);
Damien Miller7b28dc52000-09-05 13:34:53 +11001376 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001377 /* discard the remaining bytes from the current packet */
Damien Millerdfc24252008-02-10 22:29:40 +11001378 if (buf_len < buffer_len(&iqueue)) {
1379 error("iqueue grew unexpectedly");
1380 sftp_server_cleanup_exit(255);
1381 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001382 consumed = buf_len - buffer_len(&iqueue);
Damien Millerdfc24252008-02-10 22:29:40 +11001383 if (msg_len < consumed) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001384 error("msg_len %u < consumed %u", msg_len, consumed);
Damien Millerdfc24252008-02-10 22:29:40 +11001385 sftp_server_cleanup_exit(255);
1386 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001387 if (msg_len > consumed)
1388 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001389}
1390
Damien Millerfef95ad2006-07-10 20:46:55 +10001391/* Cleanup handler that logs active handles upon normal exit */
1392void
Damien Millerdfc24252008-02-10 22:29:40 +11001393sftp_server_cleanup_exit(int i)
Damien Millerfef95ad2006-07-10 20:46:55 +10001394{
1395 if (pw != NULL && client_addr != NULL) {
1396 handle_log_exit();
1397 logit("session closed for local user %s from [%s]",
1398 pw->pw_name, client_addr);
1399 }
1400 _exit(i);
1401}
1402
1403static void
Damien Millerdfc24252008-02-10 22:29:40 +11001404sftp_server_usage(void)
Damien Millerfef95ad2006-07-10 20:46:55 +10001405{
1406 extern char *__progname;
1407
1408 fprintf(stderr,
Damien Milleraa7ad302013-01-09 15:58:21 +11001409 "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
Damien Miller6efab272013-10-15 12:07:05 +11001410 "[-l log_level]\n\t[-P blacklisted_requests] "
1411 "[-p whitelisted_requests] [-u umask]\n"
1412 " %s -Q protocol_feature\n",
1413 __progname, __progname);
Damien Millerfef95ad2006-07-10 20:46:55 +10001414 exit(1);
1415}
1416
Damien Miller7b28dc52000-09-05 13:34:53 +11001417int
Damien Millerd8cb1f12008-02-10 22:40:12 +11001418sftp_server_main(int argc, char **argv, struct passwd *user_pw)
Damien Miller7b28dc52000-09-05 13:34:53 +11001419{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001420 fd_set *rset, *wset;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001421 int i, in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001422 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001423 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
Damien Miller502ab0e2013-01-09 15:57:36 +11001424 char *cp, *homedir = NULL, buf[4*4096];
Damien Miller07331212010-11-05 10:20:31 +11001425 long mask;
Damien Millerfef95ad2006-07-10 20:46:55 +10001426
Damien Millerfef95ad2006-07-10 20:46:55 +10001427 extern char *optarg;
1428 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001429
Damien Millerfef95ad2006-07-10 20:46:55 +10001430 __progname = ssh_get_progname(argv[0]);
1431 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001432
Damien Miller502ab0e2013-01-09 15:57:36 +11001433 pw = pwcopy(user_pw);
1434
Damien Miller6eaeebf2013-10-15 11:55:57 +11001435 while (!skipargs && (ch = getopt(argc, argv,
1436 "d:f:l:P:p:Q:u:cehR")) != -1) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001437 switch (ch) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001438 case 'Q':
1439 if (strcasecmp(optarg, "requests") != 0) {
1440 fprintf(stderr, "Invalid query type\n");
1441 exit(1);
1442 }
1443 for (i = 0; handlers[i].handler != NULL; i++)
1444 printf("%s\n", handlers[i].name);
1445 for (i = 0; extended_handlers[i].handler != NULL; i++)
1446 printf("%s\n", extended_handlers[i].name);
1447 exit(0);
1448 break;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001449 case 'R':
1450 readonly = 1;
1451 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001452 case 'c':
1453 /*
1454 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001455 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001456 */
1457 skipargs = 1;
1458 break;
1459 case 'e':
1460 log_stderr = 1;
1461 break;
1462 case 'l':
1463 log_level = log_level_number(optarg);
1464 if (log_level == SYSLOG_LEVEL_NOT_SET)
1465 error("Invalid log level \"%s\"", optarg);
1466 break;
1467 case 'f':
1468 log_facility = log_facility_number(optarg);
Damien Miller35e18db2007-09-17 16:11:33 +10001469 if (log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Millerfef95ad2006-07-10 20:46:55 +10001470 error("Invalid log facility \"%s\"", optarg);
1471 break;
Damien Miller502ab0e2013-01-09 15:57:36 +11001472 case 'd':
1473 cp = tilde_expand_filename(optarg, user_pw->pw_uid);
1474 homedir = percent_expand(cp, "d", user_pw->pw_dir,
1475 "u", user_pw->pw_name, (char *)NULL);
1476 free(cp);
1477 break;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001478 case 'p':
1479 if (request_whitelist != NULL)
1480 fatal("Permitted requests already set");
1481 request_whitelist = xstrdup(optarg);
1482 break;
1483 case 'P':
1484 if (request_blacklist != NULL)
1485 fatal("Refused requests already set");
1486 request_blacklist = xstrdup(optarg);
1487 break;
Darren Tucker7dc48502009-10-07 08:44:42 +11001488 case 'u':
Damien Miller07331212010-11-05 10:20:31 +11001489 errno = 0;
1490 mask = strtol(optarg, &cp, 8);
1491 if (mask < 0 || mask > 0777 || *cp != '\0' ||
1492 cp == optarg || (mask == 0 && errno != 0))
1493 fatal("Invalid umask \"%s\"", optarg);
1494 (void)umask((mode_t)mask);
Darren Tucker7dc48502009-10-07 08:44:42 +11001495 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001496 case 'h':
1497 default:
Damien Millerdfc24252008-02-10 22:29:40 +11001498 sftp_server_usage();
Damien Millerfef95ad2006-07-10 20:46:55 +10001499 }
1500 }
1501
1502 log_init(__progname, log_level, log_facility, log_stderr);
1503
1504 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1505 client_addr = xstrdup(cp);
Damien Millerdfc24252008-02-10 22:29:40 +11001506 if ((cp = strchr(client_addr, ' ')) == NULL) {
1507 error("Malformed SSH_CONNECTION variable: \"%s\"",
Damien Millerfef95ad2006-07-10 20:46:55 +10001508 getenv("SSH_CONNECTION"));
Damien Millerdfc24252008-02-10 22:29:40 +11001509 sftp_server_cleanup_exit(255);
1510 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001511 *cp = '\0';
1512 } else
1513 client_addr = xstrdup("UNKNOWN");
1514
Damien Millerfef95ad2006-07-10 20:46:55 +10001515 logit("session opened for local user %s from [%s]",
1516 pw->pw_name, client_addr);
1517
Darren Tuckeraaf51d22010-01-08 19:04:49 +11001518 in = STDIN_FILENO;
1519 out = STDOUT_FILENO;
Damien Miller7b28dc52000-09-05 13:34:53 +11001520
Damien Miller402b3312001-04-14 00:28:42 +10001521#ifdef HAVE_CYGWIN
1522 setmode(in, O_BINARY);
1523 setmode(out, O_BINARY);
1524#endif
1525
Damien Miller7b28dc52000-09-05 13:34:53 +11001526 max = 0;
1527 if (in > max)
1528 max = in;
1529 if (out > max)
1530 max = out;
1531
1532 buffer_init(&iqueue);
1533 buffer_init(&oqueue);
1534
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001535 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1536 rset = (fd_set *)xmalloc(set_size);
1537 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001538
Damien Miller502ab0e2013-01-09 15:57:36 +11001539 if (homedir != NULL) {
1540 if (chdir(homedir) != 0) {
1541 error("chdir to \"%s\" failed: %s", homedir,
1542 strerror(errno));
1543 }
1544 }
1545
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001546 for (;;) {
1547 memset(rset, 0, set_size);
1548 memset(wset, 0, set_size);
1549
Darren Tuckere9405982007-05-20 15:09:04 +10001550 /*
1551 * Ensure that we can read a full buffer and handle
1552 * the worst-case length packet it can generate,
1553 * otherwise apply backpressure by stopping reads.
1554 */
1555 if (buffer_check_alloc(&iqueue, sizeof(buf)) &&
1556 buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1557 FD_SET(in, rset);
1558
Damien Miller7b28dc52000-09-05 13:34:53 +11001559 olen = buffer_len(&oqueue);
1560 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001561 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001562
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001563 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001564 if (errno == EINTR)
1565 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001566 error("select: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001567 sftp_server_cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001568 }
1569
1570 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001571 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001572 len = read(in, buf, sizeof buf);
1573 if (len == 0) {
1574 debug("read eof");
Damien Millerdfc24252008-02-10 22:29:40 +11001575 sftp_server_cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001576 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001577 error("read: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001578 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001579 } else {
1580 buffer_append(&iqueue, buf, len);
1581 }
1582 }
1583 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001584 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001585 len = write(out, buffer_ptr(&oqueue), olen);
1586 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001587 error("write: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001588 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001589 } else {
1590 buffer_consume(&oqueue, len);
1591 }
1592 }
Darren Tuckere9405982007-05-20 15:09:04 +10001593
1594 /*
1595 * Process requests from client if we can fit the results
1596 * into the output buffer, otherwise stop processing input
1597 * and let the output queue drain.
1598 */
1599 if (buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1600 process();
Damien Miller7b28dc52000-09-05 13:34:53 +11001601 }
1602}