blob: b62bd3510a74a11b6d6b125b17c3abc3c30ca399 [file] [log] [blame]
Damien Miller6efab272013-10-15 12:07:05 +11001/* $OpenBSD: sftp-server.c,v 1.100 2013/10/14 14:18:56 jmc 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 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000233 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100234 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000235 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100236 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000237 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100238 flags |= O_EXCL;
239 return flags;
240}
241
Damien Millerfef95ad2006-07-10 20:46:55 +1000242static const char *
243string_from_portable(int pflags)
244{
245 static char ret[128];
246
247 *ret = '\0';
248
249#define PAPPEND(str) { \
250 if (*ret != '\0') \
251 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000252 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000253 }
254
255 if (pflags & SSH2_FXF_READ)
256 PAPPEND("READ")
257 if (pflags & SSH2_FXF_WRITE)
258 PAPPEND("WRITE")
259 if (pflags & SSH2_FXF_CREAT)
260 PAPPEND("CREATE")
261 if (pflags & SSH2_FXF_TRUNC)
262 PAPPEND("TRUNCATE")
263 if (pflags & SSH2_FXF_EXCL)
264 PAPPEND("EXCL")
265
266 return ret;
267}
268
Ben Lindstrombba81212001-06-25 05:01:22 +0000269static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100270get_attrib(void)
271{
272 return decode_attrib(&iqueue);
273}
274
275/* handle handles */
276
277typedef struct Handle Handle;
278struct Handle {
279 int use;
280 DIR *dirp;
281 int fd;
282 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000283 u_int64_t bytes_read, bytes_write;
Damien Miller3397d0e2008-02-10 22:26:51 +1100284 int next_unused;
Damien Miller7b28dc52000-09-05 13:34:53 +1100285};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000286
Damien Miller7b28dc52000-09-05 13:34:53 +1100287enum {
288 HANDLE_UNUSED,
289 HANDLE_DIR,
290 HANDLE_FILE
291};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000292
Damien Miller3397d0e2008-02-10 22:26:51 +1100293Handle *handles = NULL;
294u_int num_handles = 0;
295int first_unused_handle = -1;
Damien Miller7b28dc52000-09-05 13:34:53 +1100296
Damien Miller3397d0e2008-02-10 22:26:51 +1100297static void handle_unused(int i)
Damien Miller7b28dc52000-09-05 13:34:53 +1100298{
Damien Miller3397d0e2008-02-10 22:26:51 +1100299 handles[i].use = HANDLE_UNUSED;
300 handles[i].next_unused = first_unused_handle;
301 first_unused_handle = i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100302}
303
Ben Lindstrombba81212001-06-25 05:01:22 +0000304static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100305handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100306{
Damien Miller3397d0e2008-02-10 22:26:51 +1100307 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000308
Damien Miller3397d0e2008-02-10 22:26:51 +1100309 if (first_unused_handle == -1) {
310 if (num_handles + 1 <= num_handles)
311 return -1;
312 num_handles++;
313 handles = xrealloc(handles, num_handles, sizeof(Handle));
314 handle_unused(num_handles - 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100315 }
Damien Miller3397d0e2008-02-10 22:26:51 +1100316
317 i = first_unused_handle;
318 first_unused_handle = handles[i].next_unused;
319
320 handles[i].use = use;
321 handles[i].dirp = dirp;
322 handles[i].fd = fd;
323 handles[i].name = xstrdup(name);
324 handles[i].bytes_read = handles[i].bytes_write = 0;
325
326 return i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100327}
328
Ben Lindstrombba81212001-06-25 05:01:22 +0000329static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100330handle_is_ok(int i, int type)
331{
Damien Miller3397d0e2008-02-10 22:26:51 +1100332 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
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_to_string(int handle, char **stringp, int *hlenp)
337{
Damien Miller7b28dc52000-09-05 13:34:53 +1100338 if (stringp == NULL || hlenp == NULL)
339 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000340 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100341 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000342 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100343 return 0;
344}
345
Ben Lindstrombba81212001-06-25 05:01:22 +0000346static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100347handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100348{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000349 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000350
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000351 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100352 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100353 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100354 if (handle_is_ok(val, HANDLE_FILE) ||
355 handle_is_ok(val, HANDLE_DIR))
356 return val;
357 return -1;
358}
359
Ben Lindstrombba81212001-06-25 05:01:22 +0000360static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100361handle_to_name(int handle)
362{
363 if (handle_is_ok(handle, HANDLE_DIR)||
364 handle_is_ok(handle, HANDLE_FILE))
365 return handles[handle].name;
366 return NULL;
367}
368
Ben Lindstrombba81212001-06-25 05:01:22 +0000369static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100370handle_to_dir(int handle)
371{
372 if (handle_is_ok(handle, HANDLE_DIR))
373 return handles[handle].dirp;
374 return NULL;
375}
376
Ben Lindstrombba81212001-06-25 05:01:22 +0000377static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100378handle_to_fd(int handle)
379{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000380 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100381 return handles[handle].fd;
382 return -1;
383}
384
Damien Millerfef95ad2006-07-10 20:46:55 +1000385static void
386handle_update_read(int handle, ssize_t bytes)
387{
388 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
389 handles[handle].bytes_read += bytes;
390}
391
392static void
393handle_update_write(int handle, ssize_t bytes)
394{
395 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
396 handles[handle].bytes_write += bytes;
397}
398
399static u_int64_t
400handle_bytes_read(int handle)
401{
402 if (handle_is_ok(handle, HANDLE_FILE))
403 return (handles[handle].bytes_read);
404 return 0;
405}
406
407static u_int64_t
408handle_bytes_write(int handle)
409{
410 if (handle_is_ok(handle, HANDLE_FILE))
411 return (handles[handle].bytes_write);
412 return 0;
413}
414
Ben Lindstrombba81212001-06-25 05:01:22 +0000415static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100416handle_close(int handle)
417{
418 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000419
Damien Miller7b28dc52000-09-05 13:34:53 +1100420 if (handle_is_ok(handle, HANDLE_FILE)) {
421 ret = close(handles[handle].fd);
Darren Tuckera627d422013-06-02 07:31:17 +1000422 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100423 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100424 } else if (handle_is_ok(handle, HANDLE_DIR)) {
425 ret = closedir(handles[handle].dirp);
Darren Tuckera627d422013-06-02 07:31:17 +1000426 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100427 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100428 } else {
429 errno = ENOENT;
430 }
431 return ret;
432}
433
Damien Millerfef95ad2006-07-10 20:46:55 +1000434static void
435handle_log_close(int handle, char *emsg)
436{
437 if (handle_is_ok(handle, HANDLE_FILE)) {
438 logit("%s%sclose \"%s\" bytes read %llu written %llu",
439 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
440 handle_to_name(handle),
Darren Tucker86473c52007-05-20 14:59:32 +1000441 (unsigned long long)handle_bytes_read(handle),
442 (unsigned long long)handle_bytes_write(handle));
Damien Millerfef95ad2006-07-10 20:46:55 +1000443 } else {
444 logit("%s%sclosedir \"%s\"",
445 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
446 handle_to_name(handle));
447 }
448}
449
450static void
451handle_log_exit(void)
452{
453 u_int i;
454
Damien Miller3397d0e2008-02-10 22:26:51 +1100455 for (i = 0; i < num_handles; i++)
Damien Millerfef95ad2006-07-10 20:46:55 +1000456 if (handles[i].use != HANDLE_UNUSED)
457 handle_log_close(i, "forced");
458}
459
Ben Lindstrombba81212001-06-25 05:01:22 +0000460static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100461get_handle(void)
462{
463 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000464 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100465 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000466
Damien Miller7b28dc52000-09-05 13:34:53 +1100467 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000468 if (hlen < 256)
469 val = handle_from_string(handle, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000470 free(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100471 return val;
472}
473
474/* send replies */
475
Ben Lindstrombba81212001-06-25 05:01:22 +0000476static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100477send_msg(Buffer *m)
478{
479 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000480
Damien Miller7b28dc52000-09-05 13:34:53 +1100481 buffer_put_int(&oqueue, mlen);
482 buffer_append(&oqueue, buffer_ptr(m), mlen);
483 buffer_consume(m, mlen);
484}
485
Damien Millerfef95ad2006-07-10 20:46:55 +1000486static const char *
487status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100488{
Damien Miller058316f2001-03-08 10:08:49 +1100489 const char *status_messages[] = {
490 "Success", /* SSH_FX_OK */
491 "End of file", /* SSH_FX_EOF */
492 "No such file", /* SSH_FX_NO_SUCH_FILE */
493 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
494 "Failure", /* SSH_FX_FAILURE */
495 "Bad message", /* SSH_FX_BAD_MESSAGE */
496 "No connection", /* SSH_FX_NO_CONNECTION */
497 "Connection lost", /* SSH_FX_CONNECTION_LOST */
498 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
499 "Unknown error" /* Others */
500 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000501 return (status_messages[MIN(status,SSH2_FX_MAX)]);
502}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000503
Damien Millerfef95ad2006-07-10 20:46:55 +1000504static void
505send_status(u_int32_t id, u_int32_t status)
506{
507 Buffer msg;
508
509 debug3("request %u: sent status %u", id, status);
510 if (log_level > SYSLOG_LEVEL_VERBOSE ||
511 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
512 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100513 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000514 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100515 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000516 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100517 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000518 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100519 buffer_put_cstring(&msg, "");
520 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100521 send_msg(&msg);
522 buffer_free(&msg);
523}
Ben Lindstrombba81212001-06-25 05:01:22 +0000524static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100525send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100526{
527 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000528
Damien Miller7b28dc52000-09-05 13:34:53 +1100529 buffer_init(&msg);
530 buffer_put_char(&msg, type);
531 buffer_put_int(&msg, id);
532 buffer_put_string(&msg, data, dlen);
533 send_msg(&msg);
534 buffer_free(&msg);
535}
536
Ben Lindstrombba81212001-06-25 05:01:22 +0000537static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100538send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100539{
Damien Millerfef95ad2006-07-10 20:46:55 +1000540 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000541 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100542}
543
Ben Lindstrombba81212001-06-25 05:01:22 +0000544static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100545send_handle(u_int32_t id, int handle)
546{
547 char *string;
548 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000549
Damien Miller7b28dc52000-09-05 13:34:53 +1100550 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000551 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000552 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000553 free(string);
Damien Miller7b28dc52000-09-05 13:34:53 +1100554}
555
Ben Lindstrombba81212001-06-25 05:01:22 +0000556static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100557send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100558{
559 Buffer msg;
560 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000561
Damien Miller7b28dc52000-09-05 13:34:53 +1100562 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000563 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100564 buffer_put_int(&msg, id);
565 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000566 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100567 for (i = 0; i < count; i++) {
568 buffer_put_cstring(&msg, stats[i].name);
569 buffer_put_cstring(&msg, stats[i].long_name);
570 encode_attrib(&msg, &stats[i].attrib);
571 }
572 send_msg(&msg);
573 buffer_free(&msg);
574}
575
Ben Lindstrombba81212001-06-25 05:01:22 +0000576static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100577send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100578{
579 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000580
Damien Millerfef95ad2006-07-10 20:46:55 +1000581 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100582 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000583 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100584 buffer_put_int(&msg, id);
585 encode_attrib(&msg, a);
586 send_msg(&msg);
587 buffer_free(&msg);
588}
589
Damien Millerd671e5a2008-05-19 14:53:33 +1000590static void
591send_statvfs(u_int32_t id, struct statvfs *st)
592{
593 Buffer msg;
594 u_int64_t flag;
595
596 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
597 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
598
599 buffer_init(&msg);
600 buffer_put_char(&msg, SSH2_FXP_EXTENDED_REPLY);
601 buffer_put_int(&msg, id);
Darren Tucker3463aca2008-06-09 23:06:55 +1000602 buffer_put_int64(&msg, st->f_bsize);
603 buffer_put_int64(&msg, st->f_frsize);
Damien Millerd671e5a2008-05-19 14:53:33 +1000604 buffer_put_int64(&msg, st->f_blocks);
605 buffer_put_int64(&msg, st->f_bfree);
606 buffer_put_int64(&msg, st->f_bavail);
607 buffer_put_int64(&msg, st->f_files);
608 buffer_put_int64(&msg, st->f_ffree);
609 buffer_put_int64(&msg, st->f_favail);
Darren Tucker77001382008-06-09 06:17:53 +1000610 buffer_put_int64(&msg, FSID_TO_ULONG(st->f_fsid));
Darren Tucker3463aca2008-06-09 23:06:55 +1000611 buffer_put_int64(&msg, flag);
612 buffer_put_int64(&msg, st->f_namemax);
Damien Millerd671e5a2008-05-19 14:53:33 +1000613 send_msg(&msg);
614 buffer_free(&msg);
615}
616
Damien Miller7b28dc52000-09-05 13:34:53 +1100617/* parse incoming */
618
Ben Lindstrombba81212001-06-25 05:01:22 +0000619static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100620process_init(void)
621{
622 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100623
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000624 version = get_int();
Damien Millerf145a5b2011-06-20 14:42:51 +1000625 verbose("received client version %u", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100626 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000627 buffer_put_char(&msg, SSH2_FXP_VERSION);
628 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7c296612008-03-07 18:33:53 +1100629 /* POSIX rename extension */
630 buffer_put_cstring(&msg, "posix-rename@openssh.com");
631 buffer_put_cstring(&msg, "1"); /* version */
Damien Millera7e0d5a2008-05-19 16:08:41 +1000632 /* statvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000633 buffer_put_cstring(&msg, "statvfs@openssh.com");
Darren Tucker294b8412008-06-08 12:57:08 +1000634 buffer_put_cstring(&msg, "2"); /* version */
Damien Millera7e0d5a2008-05-19 16:08:41 +1000635 /* fstatvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000636 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
Darren Tucker294b8412008-06-08 12:57:08 +1000637 buffer_put_cstring(&msg, "2"); /* version */
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100638 /* hardlink extension */
639 buffer_put_cstring(&msg, "hardlink@openssh.com");
640 buffer_put_cstring(&msg, "1"); /* version */
Damien Miller7b28dc52000-09-05 13:34:53 +1100641 send_msg(&msg);
642 buffer_free(&msg);
643}
644
Ben Lindstrombba81212001-06-25 05:01:22 +0000645static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100646process_open(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100647{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100648 u_int32_t pflags;
Damien Miller7b28dc52000-09-05 13:34:53 +1100649 Attrib *a;
650 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000651 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100652
Damien Miller7b28dc52000-09-05 13:34:53 +1100653 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000654 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000655 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100656 a = get_attrib();
657 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000658 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000659 logit("open \"%s\" flags %s mode 0%o",
660 name, string_from_portable(pflags), mode);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100661 if (readonly &&
Damien Miller6eaeebf2013-10-15 11:55:57 +1100662 ((flags & O_ACCMODE) == O_WRONLY ||
663 (flags & O_ACCMODE) == O_RDWR)) {
664 verbose("Refusing open request in read-only mode");
665 status = SSH2_FX_PERMISSION_DENIED;
666 } else {
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100667 fd = open(name, flags, mode);
668 if (fd < 0) {
669 status = errno_to_portable(errno);
Damien Miller7b28dc52000-09-05 13:34:53 +1100670 } else {
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100671 handle = handle_new(HANDLE_FILE, name, fd, NULL);
672 if (handle < 0) {
673 close(fd);
674 } else {
675 send_handle(id, handle);
676 status = SSH2_FX_OK;
677 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100678 }
679 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000680 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100681 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000682 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100683}
684
Ben Lindstrombba81212001-06-25 05:01:22 +0000685static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100686process_close(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100687{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000688 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100689
Damien Miller7b28dc52000-09-05 13:34:53 +1100690 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000691 debug3("request %u: close handle %u", id, handle);
692 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100693 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000694 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100695 send_status(id, status);
696}
697
Ben Lindstrombba81212001-06-25 05:01:22 +0000698static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100699process_read(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100700{
701 char buf[64*1024];
Damien Miller6eaeebf2013-10-15 11:55:57 +1100702 u_int32_t len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000703 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100704 u_int64_t off;
705
Damien Miller7b28dc52000-09-05 13:34:53 +1100706 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000707 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100708 len = get_int();
709
Damien Millerfef95ad2006-07-10 20:46:55 +1000710 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
711 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100712 if (len > sizeof buf) {
713 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000714 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100715 }
716 fd = handle_to_fd(handle);
717 if (fd >= 0) {
718 if (lseek(fd, off, SEEK_SET) < 0) {
719 error("process_read: seek failed");
720 status = errno_to_portable(errno);
721 } else {
722 ret = read(fd, buf, len);
723 if (ret < 0) {
724 status = errno_to_portable(errno);
725 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000726 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100727 } else {
728 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000729 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000730 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100731 }
732 }
733 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000734 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100735 send_status(id, status);
736}
737
Ben Lindstrombba81212001-06-25 05:01:22 +0000738static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100739process_write(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100740{
Damien Miller7b28dc52000-09-05 13:34:53 +1100741 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100742 u_int len;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100743 int handle, fd, ret, status;
Damien Miller7b28dc52000-09-05 13:34:53 +1100744 char *data;
745
Damien Miller7b28dc52000-09-05 13:34:53 +1100746 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000747 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100748 data = get_string(&len);
749
Damien Millerfef95ad2006-07-10 20:46:55 +1000750 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
751 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100752 fd = handle_to_fd(handle);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100753
754 if (fd < 0)
755 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100756 else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100757 if (lseek(fd, off, SEEK_SET) < 0) {
758 status = errno_to_portable(errno);
759 error("process_write: seek failed");
760 } else {
761/* XXX ATOMICIO ? */
762 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000763 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100764 error("process_write: write failed");
765 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000766 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000767 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000768 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100769 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000770 debug2("nothing at all written");
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100771 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100772 }
773 }
774 }
775 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000776 free(data);
Damien Miller7b28dc52000-09-05 13:34:53 +1100777}
778
Ben Lindstrombba81212001-06-25 05:01:22 +0000779static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100780process_do_stat(u_int32_t id, int do_lstat)
Damien Miller7b28dc52000-09-05 13:34:53 +1100781{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000782 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100783 struct stat st;
Damien Miller7b28dc52000-09-05 13:34:53 +1100784 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000785 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100786
Damien Miller7b28dc52000-09-05 13:34:53 +1100787 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000788 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
789 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100790 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
791 if (ret < 0) {
792 status = errno_to_portable(errno);
793 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000794 stat_to_attrib(&st, &a);
795 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000796 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100797 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000798 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100799 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000800 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100801}
802
Ben Lindstrombba81212001-06-25 05:01:22 +0000803static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100804process_stat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100805{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100806 process_do_stat(id, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100807}
808
Ben Lindstrombba81212001-06-25 05:01:22 +0000809static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100810process_lstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100811{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100812 process_do_stat(id, 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100813}
814
Ben Lindstrombba81212001-06-25 05:01:22 +0000815static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100816process_fstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100817{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000818 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100819 struct stat st;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000820 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100821
Damien Miller7b28dc52000-09-05 13:34:53 +1100822 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000823 debug("request %u: fstat \"%s\" (handle %u)",
824 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100825 fd = handle_to_fd(handle);
Damien Millere2334d62007-01-05 16:31:02 +1100826 if (fd >= 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100827 ret = fstat(fd, &st);
828 if (ret < 0) {
829 status = errno_to_portable(errno);
830 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000831 stat_to_attrib(&st, &a);
832 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000833 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100834 }
835 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000836 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100837 send_status(id, status);
838}
839
Ben Lindstrombba81212001-06-25 05:01:22 +0000840static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100841attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100842{
843 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000844
Damien Miller7b28dc52000-09-05 13:34:53 +1100845 tv[0].tv_sec = a->atime;
846 tv[0].tv_usec = 0;
847 tv[1].tv_sec = a->mtime;
848 tv[1].tv_usec = 0;
849 return tv;
850}
851
Ben Lindstrombba81212001-06-25 05:01:22 +0000852static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100853process_setstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100854{
855 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100856 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000857 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100858
Damien Miller7b28dc52000-09-05 13:34:53 +1100859 name = get_string(NULL);
860 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000861 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100862 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000863 logit("set \"%s\" size %llu",
864 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100865 ret = truncate(name, a->size);
866 if (ret == -1)
867 status = errno_to_portable(errno);
868 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000869 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000870 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller9e720282008-06-29 22:46:35 +1000871 ret = chmod(name, a->perm & 07777);
Damien Miller7b28dc52000-09-05 13:34:53 +1100872 if (ret == -1)
873 status = errno_to_portable(errno);
874 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000875 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000876 char buf[64];
877 time_t t = a->mtime;
878
879 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
880 localtime(&t));
881 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100882 ret = utimes(name, attrib_to_tv(a));
883 if (ret == -1)
884 status = errno_to_portable(errno);
885 }
Kevin Steves8e743932001-02-05 13:24:35 +0000886 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000887 logit("set \"%s\" owner %lu group %lu", name,
888 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000889 ret = chown(name, a->uid, a->gid);
890 if (ret == -1)
891 status = errno_to_portable(errno);
892 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100893 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000894 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100895}
896
Ben Lindstrombba81212001-06-25 05:01:22 +0000897static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100898process_fsetstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100899{
900 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100901 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000902 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000903
Damien Miller7b28dc52000-09-05 13:34:53 +1100904 handle = get_handle();
905 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000906 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100907 fd = handle_to_fd(handle);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100908 if (fd < 0)
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000909 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100910 else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000911 char *name = handle_to_name(handle);
912
Damien Miller00c92172002-02-13 14:05:00 +1100913 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000914 logit("set \"%s\" size %llu",
915 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100916 ret = ftruncate(fd, a->size);
917 if (ret == -1)
918 status = errno_to_portable(errno);
919 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000920 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000921 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000922#ifdef HAVE_FCHMOD
Damien Miller9e720282008-06-29 22:46:35 +1000923 ret = fchmod(fd, a->perm & 07777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000924#else
Damien Miller9e720282008-06-29 22:46:35 +1000925 ret = chmod(name, a->perm & 07777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000926#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100927 if (ret == -1)
928 status = errno_to_portable(errno);
929 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000930 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000931 char buf[64];
932 time_t t = a->mtime;
933
934 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
935 localtime(&t));
936 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100937#ifdef HAVE_FUTIMES
938 ret = futimes(fd, attrib_to_tv(a));
939#else
940 ret = utimes(name, attrib_to_tv(a));
941#endif
942 if (ret == -1)
943 status = errno_to_portable(errno);
944 }
Kevin Steves8e743932001-02-05 13:24:35 +0000945 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000946 logit("set \"%s\" owner %lu group %lu", name,
947 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000948#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000949 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000950#else
951 ret = chown(name, a->uid, a->gid);
952#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000953 if (ret == -1)
954 status = errno_to_portable(errno);
955 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100956 }
957 send_status(id, status);
958}
959
Ben Lindstrombba81212001-06-25 05:01:22 +0000960static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100961process_opendir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100962{
963 DIR *dirp = NULL;
964 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000965 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100966
Damien Miller7b28dc52000-09-05 13:34:53 +1100967 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000968 debug3("request %u: opendir", id);
969 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000970 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100971 if (dirp == NULL) {
972 status = errno_to_portable(errno);
973 } else {
Damien Miller00111382003-03-10 11:21:17 +1100974 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100975 if (handle < 0) {
976 closedir(dirp);
977 } else {
978 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000979 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100980 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000981
Damien Miller7b28dc52000-09-05 13:34:53 +1100982 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000983 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100984 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000985 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100986}
987
Ben Lindstrombba81212001-06-25 05:01:22 +0000988static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100989process_readdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100990{
991 DIR *dirp;
992 struct dirent *dp;
993 char *path;
994 int handle;
Damien Miller7b28dc52000-09-05 13:34:53 +1100995
Damien Miller7b28dc52000-09-05 13:34:53 +1100996 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000997 debug("request %u: readdir \"%s\" (handle %d)", id,
998 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100999 dirp = handle_to_dir(handle);
1000 path = handle_to_name(handle);
1001 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001002 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +11001003 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +11001004 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +10001005 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +11001006 Stat *stats;
1007 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001008
Damien Miller07d86be2006-03-26 14:19:21 +11001009 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001010 while ((dp = readdir(dirp)) != NULL) {
1011 if (count >= nstats) {
1012 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +11001013 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001014 }
1015/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +00001016 snprintf(pathname, sizeof pathname, "%s%s%s", path,
1017 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001018 if (lstat(pathname, &st) < 0)
1019 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001020 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +11001021 stats[count].name = xstrdup(dp->d_name);
Darren Tucker2901e2d2010-01-13 22:44:06 +11001022 stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001023 count++;
1024 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001025 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +11001026 if (count == 100)
1027 break;
1028 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001029 if (count > 0) {
1030 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001031 for (i = 0; i < count; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001032 free(stats[i].name);
1033 free(stats[i].long_name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001034 }
1035 } else {
1036 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +11001037 }
Darren Tuckera627d422013-06-02 07:31:17 +10001038 free(stats);
Damien Miller7b28dc52000-09-05 13:34:53 +11001039 }
1040}
1041
Ben Lindstrombba81212001-06-25 05:01:22 +00001042static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001043process_remove(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001044{
1045 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001046 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001047 int ret;
1048
Damien Miller7b28dc52000-09-05 13:34:53 +11001049 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001050 debug3("request %u: remove", id);
1051 logit("remove name \"%s\"", name);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001052 ret = unlink(name);
1053 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001054 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001055 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001056}
1057
Ben Lindstrombba81212001-06-25 05:01:22 +00001058static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001059process_mkdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001060{
1061 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +11001062 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001063 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001064
Damien Miller7b28dc52000-09-05 13:34:53 +11001065 name = get_string(NULL);
1066 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001067 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
Damien Miller9e720282008-06-29 22:46:35 +10001068 a->perm & 07777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +10001069 debug3("request %u: mkdir", id);
1070 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001071 ret = mkdir(name, mode);
1072 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001073 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001074 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001075}
1076
Ben Lindstrombba81212001-06-25 05:01:22 +00001077static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001078process_rmdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001079{
Damien Miller7b28dc52000-09-05 13:34:53 +11001080 char *name;
1081 int ret, status;
1082
Damien Miller7b28dc52000-09-05 13:34:53 +11001083 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001084 debug3("request %u: rmdir", id);
1085 logit("rmdir name \"%s\"", name);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001086 ret = rmdir(name);
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_realpath(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001094{
1095 char resolvedname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +11001096 char *path;
1097
Damien Miller7b28dc52000-09-05 13:34:53 +11001098 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001099 if (path[0] == '\0') {
Darren Tuckera627d422013-06-02 07:31:17 +10001100 free(path);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001101 path = xstrdup(".");
1102 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001103 debug3("request %u: realpath", id);
1104 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001105 if (realpath(path, resolvedname) == NULL) {
1106 send_status(id, errno_to_portable(errno));
1107 } else {
1108 Stat s;
1109 attrib_clear(&s.attrib);
1110 s.name = s.long_name = resolvedname;
1111 send_names(id, 1, &s);
1112 }
Darren Tuckera627d422013-06-02 07:31:17 +10001113 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001114}
1115
Ben Lindstrombba81212001-06-25 05:01:22 +00001116static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001117process_rename(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001118{
Damien Miller7b28dc52000-09-05 13:34:53 +11001119 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001120 int status;
Damien Millerb3207e82003-03-26 16:01:11 +11001121 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +11001122
Damien Miller7b28dc52000-09-05 13:34:53 +11001123 oldpath = get_string(NULL);
1124 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001125 debug3("request %u: rename", id);
1126 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +11001127 status = SSH2_FX_FAILURE;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001128 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +11001129 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +11001130 else if (S_ISREG(sb.st_mode)) {
1131 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001132 if (link(oldpath, newpath) == -1) {
Damien Miller0e265512009-08-28 10:43:13 +10001133 if (errno == EOPNOTSUPP || errno == ENOSYS
Darren Tuckerf7fa7062008-07-04 14:10:19 +10001134#ifdef EXDEV
1135 || errno == EXDEV
1136#endif
Darren Tuckere59b5082004-06-28 16:01:19 +10001137#ifdef LINK_OPNOTSUPP_ERRNO
1138 || errno == LINK_OPNOTSUPP_ERRNO
1139#endif
1140 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001141 struct stat st;
1142
1143 /*
1144 * fs doesn't support links, so fall back to
1145 * stat+rename. This is racy.
1146 */
1147 if (stat(newpath, &st) == -1) {
1148 if (rename(oldpath, newpath) == -1)
1149 status =
1150 errno_to_portable(errno);
1151 else
1152 status = SSH2_FX_OK;
1153 }
1154 } else {
1155 status = errno_to_portable(errno);
1156 }
1157 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001158 status = errno_to_portable(errno);
1159 /* clean spare link */
1160 unlink(newpath);
1161 } else
1162 status = SSH2_FX_OK;
1163 } else if (stat(newpath, &sb) == -1) {
1164 if (rename(oldpath, newpath) == -1)
1165 status = errno_to_portable(errno);
1166 else
1167 status = SSH2_FX_OK;
1168 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001169 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001170 free(oldpath);
1171 free(newpath);
Damien Miller7b28dc52000-09-05 13:34:53 +11001172}
1173
Ben Lindstrombba81212001-06-25 05:01:22 +00001174static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001175process_readlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001176{
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001177 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001178 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001179 char *path;
1180
Damien Miller058316f2001-03-08 10:08:49 +11001181 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001182 debug3("request %u: readlink", id);
1183 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001184 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001185 send_status(id, errno_to_portable(errno));
1186 else {
1187 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001188
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001189 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001190 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001191 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001192 send_names(id, 1, &s);
1193 }
Darren Tuckera627d422013-06-02 07:31:17 +10001194 free(path);
Damien Miller058316f2001-03-08 10:08:49 +11001195}
1196
Ben Lindstrombba81212001-06-25 05:01:22 +00001197static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001198process_symlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001199{
Damien Miller058316f2001-03-08 10:08:49 +11001200 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001201 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001202
Damien Miller058316f2001-03-08 10:08:49 +11001203 oldpath = get_string(NULL);
1204 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001205 debug3("request %u: symlink", id);
1206 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001207 /* this will fail if 'newpath' exists */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001208 ret = symlink(oldpath, newpath);
1209 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001210 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001211 free(oldpath);
1212 free(newpath);
Damien Miller058316f2001-03-08 10:08:49 +11001213}
1214
Ben Lindstrombba81212001-06-25 05:01:22 +00001215static void
Damien Miller7c296612008-03-07 18:33:53 +11001216process_extended_posix_rename(u_int32_t id)
1217{
1218 char *oldpath, *newpath;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001219 int ret, status;
Damien Miller7c296612008-03-07 18:33:53 +11001220
1221 oldpath = get_string(NULL);
1222 newpath = get_string(NULL);
1223 debug3("request %u: posix-rename", id);
1224 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001225 ret = rename(oldpath, newpath);
1226 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001227 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001228 free(oldpath);
1229 free(newpath);
Damien Miller7c296612008-03-07 18:33:53 +11001230}
1231
1232static void
Damien Millerd671e5a2008-05-19 14:53:33 +10001233process_extended_statvfs(u_int32_t id)
1234{
1235 char *path;
1236 struct statvfs st;
1237
1238 path = get_string(NULL);
1239 debug3("request %u: statfs", id);
1240 logit("statfs \"%s\"", path);
1241
1242 if (statvfs(path, &st) != 0)
1243 send_status(id, errno_to_portable(errno));
1244 else
1245 send_statvfs(id, &st);
Darren Tuckera627d422013-06-02 07:31:17 +10001246 free(path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001247}
1248
1249static void
1250process_extended_fstatvfs(u_int32_t id)
1251{
1252 int handle, fd;
1253 struct statvfs st;
1254
1255 handle = get_handle();
1256 debug("request %u: fstatvfs \"%s\" (handle %u)",
1257 id, handle_to_name(handle), handle);
1258 if ((fd = handle_to_fd(handle)) < 0) {
1259 send_status(id, SSH2_FX_FAILURE);
1260 return;
1261 }
1262 if (fstatvfs(fd, &st) != 0)
1263 send_status(id, errno_to_portable(errno));
1264 else
1265 send_statvfs(id, &st);
1266}
1267
1268static void
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001269process_extended_hardlink(u_int32_t id)
1270{
1271 char *oldpath, *newpath;
1272 int ret, status;
1273
1274 oldpath = get_string(NULL);
1275 newpath = get_string(NULL);
1276 debug3("request %u: hardlink", id);
1277 logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001278 ret = link(oldpath, newpath);
1279 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001280 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001281 free(oldpath);
1282 free(newpath);
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001283}
1284
1285static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001286process_extended(u_int32_t id)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001287{
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001288 char *request;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001289 u_int i;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001290
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001291 request = get_string(NULL);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001292 for (i = 0; extended_handlers[i].handler != NULL; i++) {
1293 if (strcmp(request, extended_handlers[i].ext_name) == 0) {
1294 if (!request_permitted(&extended_handlers[i]))
1295 send_status(id, SSH2_FX_PERMISSION_DENIED);
1296 else
1297 extended_handlers[i].handler(id);
1298 break;
1299 }
1300 }
1301 if (extended_handlers[i].handler == NULL) {
1302 error("Unknown extended request \"%.100s\"", request);
Damien Miller7c296612008-03-07 18:33:53 +11001303 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001304 }
Darren Tuckera627d422013-06-02 07:31:17 +10001305 free(request);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001306}
Damien Miller7b28dc52000-09-05 13:34:53 +11001307
1308/* stolen from ssh-agent */
1309
Ben Lindstrombba81212001-06-25 05:01:22 +00001310static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001311process(void)
1312{
Damien Miller6eaeebf2013-10-15 11:55:57 +11001313 u_int msg_len, buf_len, consumed, type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001314 u_char *cp;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001315 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +11001316
Ben Lindstrom2c140472002-06-06 21:57:54 +00001317 buf_len = buffer_len(&iqueue);
1318 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001319 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001320 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001321 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001322 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001323 error("bad message from %s local user %s",
1324 client_addr, pw->pw_name);
Damien Millerdfc24252008-02-10 22:29:40 +11001325 sftp_server_cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001326 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001327 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001328 return;
1329 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001330 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001331 type = buffer_get_char(&iqueue);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001332
Damien Miller7b28dc52000-09-05 13:34:53 +11001333 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001334 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001335 process_init();
Damien Miller6eaeebf2013-10-15 11:55:57 +11001336 init_done = 1;
Damien Miller058316f2001-03-08 10:08:49 +11001337 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001338 case SSH2_FXP_EXTENDED:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001339 if (!init_done)
1340 fatal("Received extended request before init");
1341 id = get_int();
1342 process_extended(id);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001343 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001344 default:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001345 if (!init_done)
1346 fatal("Received %u request before init", type);
1347 id = get_int();
1348 for (i = 0; handlers[i].handler != NULL; i++) {
1349 if (type == handlers[i].type) {
1350 if (!request_permitted(&handlers[i])) {
1351 send_status(id,
1352 SSH2_FX_PERMISSION_DENIED);
1353 } else {
1354 handlers[i].handler(id);
1355 }
1356 break;
1357 }
1358 }
1359 if (handlers[i].handler == NULL)
1360 error("Unknown message %u", type);
Damien Miller7b28dc52000-09-05 13:34:53 +11001361 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001362 /* discard the remaining bytes from the current packet */
Damien Millerdfc24252008-02-10 22:29:40 +11001363 if (buf_len < buffer_len(&iqueue)) {
1364 error("iqueue grew unexpectedly");
1365 sftp_server_cleanup_exit(255);
1366 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001367 consumed = buf_len - buffer_len(&iqueue);
Damien Millerdfc24252008-02-10 22:29:40 +11001368 if (msg_len < consumed) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001369 error("msg_len %u < consumed %u", msg_len, consumed);
Damien Millerdfc24252008-02-10 22:29:40 +11001370 sftp_server_cleanup_exit(255);
1371 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001372 if (msg_len > consumed)
1373 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001374}
1375
Damien Millerfef95ad2006-07-10 20:46:55 +10001376/* Cleanup handler that logs active handles upon normal exit */
1377void
Damien Millerdfc24252008-02-10 22:29:40 +11001378sftp_server_cleanup_exit(int i)
Damien Millerfef95ad2006-07-10 20:46:55 +10001379{
1380 if (pw != NULL && client_addr != NULL) {
1381 handle_log_exit();
1382 logit("session closed for local user %s from [%s]",
1383 pw->pw_name, client_addr);
1384 }
1385 _exit(i);
1386}
1387
1388static void
Damien Millerdfc24252008-02-10 22:29:40 +11001389sftp_server_usage(void)
Damien Millerfef95ad2006-07-10 20:46:55 +10001390{
1391 extern char *__progname;
1392
1393 fprintf(stderr,
Damien Milleraa7ad302013-01-09 15:58:21 +11001394 "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
Damien Miller6efab272013-10-15 12:07:05 +11001395 "[-l log_level]\n\t[-P blacklisted_requests] "
1396 "[-p whitelisted_requests] [-u umask]\n"
1397 " %s -Q protocol_feature\n",
1398 __progname, __progname);
Damien Millerfef95ad2006-07-10 20:46:55 +10001399 exit(1);
1400}
1401
Damien Miller7b28dc52000-09-05 13:34:53 +11001402int
Damien Millerd8cb1f12008-02-10 22:40:12 +11001403sftp_server_main(int argc, char **argv, struct passwd *user_pw)
Damien Miller7b28dc52000-09-05 13:34:53 +11001404{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001405 fd_set *rset, *wset;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001406 int i, in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001407 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001408 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
Damien Miller502ab0e2013-01-09 15:57:36 +11001409 char *cp, *homedir = NULL, buf[4*4096];
Damien Miller07331212010-11-05 10:20:31 +11001410 long mask;
Damien Millerfef95ad2006-07-10 20:46:55 +10001411
Damien Millerfef95ad2006-07-10 20:46:55 +10001412 extern char *optarg;
1413 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001414
Damien Millerfef95ad2006-07-10 20:46:55 +10001415 __progname = ssh_get_progname(argv[0]);
1416 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001417
Damien Miller502ab0e2013-01-09 15:57:36 +11001418 pw = pwcopy(user_pw);
1419
Damien Miller6eaeebf2013-10-15 11:55:57 +11001420 while (!skipargs && (ch = getopt(argc, argv,
1421 "d:f:l:P:p:Q:u:cehR")) != -1) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001422 switch (ch) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001423 case 'Q':
1424 if (strcasecmp(optarg, "requests") != 0) {
1425 fprintf(stderr, "Invalid query type\n");
1426 exit(1);
1427 }
1428 for (i = 0; handlers[i].handler != NULL; i++)
1429 printf("%s\n", handlers[i].name);
1430 for (i = 0; extended_handlers[i].handler != NULL; i++)
1431 printf("%s\n", extended_handlers[i].name);
1432 exit(0);
1433 break;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001434 case 'R':
1435 readonly = 1;
1436 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001437 case 'c':
1438 /*
1439 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001440 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001441 */
1442 skipargs = 1;
1443 break;
1444 case 'e':
1445 log_stderr = 1;
1446 break;
1447 case 'l':
1448 log_level = log_level_number(optarg);
1449 if (log_level == SYSLOG_LEVEL_NOT_SET)
1450 error("Invalid log level \"%s\"", optarg);
1451 break;
1452 case 'f':
1453 log_facility = log_facility_number(optarg);
Damien Miller35e18db2007-09-17 16:11:33 +10001454 if (log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Millerfef95ad2006-07-10 20:46:55 +10001455 error("Invalid log facility \"%s\"", optarg);
1456 break;
Damien Miller502ab0e2013-01-09 15:57:36 +11001457 case 'd':
1458 cp = tilde_expand_filename(optarg, user_pw->pw_uid);
1459 homedir = percent_expand(cp, "d", user_pw->pw_dir,
1460 "u", user_pw->pw_name, (char *)NULL);
1461 free(cp);
1462 break;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001463 case 'p':
1464 if (request_whitelist != NULL)
1465 fatal("Permitted requests already set");
1466 request_whitelist = xstrdup(optarg);
1467 break;
1468 case 'P':
1469 if (request_blacklist != NULL)
1470 fatal("Refused requests already set");
1471 request_blacklist = xstrdup(optarg);
1472 break;
Darren Tucker7dc48502009-10-07 08:44:42 +11001473 case 'u':
Damien Miller07331212010-11-05 10:20:31 +11001474 errno = 0;
1475 mask = strtol(optarg, &cp, 8);
1476 if (mask < 0 || mask > 0777 || *cp != '\0' ||
1477 cp == optarg || (mask == 0 && errno != 0))
1478 fatal("Invalid umask \"%s\"", optarg);
1479 (void)umask((mode_t)mask);
Darren Tucker7dc48502009-10-07 08:44:42 +11001480 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001481 case 'h':
1482 default:
Damien Millerdfc24252008-02-10 22:29:40 +11001483 sftp_server_usage();
Damien Millerfef95ad2006-07-10 20:46:55 +10001484 }
1485 }
1486
1487 log_init(__progname, log_level, log_facility, log_stderr);
1488
1489 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1490 client_addr = xstrdup(cp);
Damien Millerdfc24252008-02-10 22:29:40 +11001491 if ((cp = strchr(client_addr, ' ')) == NULL) {
1492 error("Malformed SSH_CONNECTION variable: \"%s\"",
Damien Millerfef95ad2006-07-10 20:46:55 +10001493 getenv("SSH_CONNECTION"));
Damien Millerdfc24252008-02-10 22:29:40 +11001494 sftp_server_cleanup_exit(255);
1495 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001496 *cp = '\0';
1497 } else
1498 client_addr = xstrdup("UNKNOWN");
1499
Damien Millerfef95ad2006-07-10 20:46:55 +10001500 logit("session opened for local user %s from [%s]",
1501 pw->pw_name, client_addr);
1502
Darren Tuckeraaf51d22010-01-08 19:04:49 +11001503 in = STDIN_FILENO;
1504 out = STDOUT_FILENO;
Damien Miller7b28dc52000-09-05 13:34:53 +11001505
Damien Miller402b3312001-04-14 00:28:42 +10001506#ifdef HAVE_CYGWIN
1507 setmode(in, O_BINARY);
1508 setmode(out, O_BINARY);
1509#endif
1510
Damien Miller7b28dc52000-09-05 13:34:53 +11001511 max = 0;
1512 if (in > max)
1513 max = in;
1514 if (out > max)
1515 max = out;
1516
1517 buffer_init(&iqueue);
1518 buffer_init(&oqueue);
1519
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001520 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1521 rset = (fd_set *)xmalloc(set_size);
1522 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001523
Damien Miller502ab0e2013-01-09 15:57:36 +11001524 if (homedir != NULL) {
1525 if (chdir(homedir) != 0) {
1526 error("chdir to \"%s\" failed: %s", homedir,
1527 strerror(errno));
1528 }
1529 }
1530
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001531 for (;;) {
1532 memset(rset, 0, set_size);
1533 memset(wset, 0, set_size);
1534
Darren Tuckere9405982007-05-20 15:09:04 +10001535 /*
1536 * Ensure that we can read a full buffer and handle
1537 * the worst-case length packet it can generate,
1538 * otherwise apply backpressure by stopping reads.
1539 */
1540 if (buffer_check_alloc(&iqueue, sizeof(buf)) &&
1541 buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1542 FD_SET(in, rset);
1543
Damien Miller7b28dc52000-09-05 13:34:53 +11001544 olen = buffer_len(&oqueue);
1545 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001546 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001547
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001548 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001549 if (errno == EINTR)
1550 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001551 error("select: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001552 sftp_server_cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001553 }
1554
1555 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001556 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001557 len = read(in, buf, sizeof buf);
1558 if (len == 0) {
1559 debug("read eof");
Damien Millerdfc24252008-02-10 22:29:40 +11001560 sftp_server_cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001561 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001562 error("read: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001563 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001564 } else {
1565 buffer_append(&iqueue, buf, len);
1566 }
1567 }
1568 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001569 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001570 len = write(out, buffer_ptr(&oqueue), olen);
1571 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001572 error("write: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001573 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001574 } else {
1575 buffer_consume(&oqueue, len);
1576 }
1577 }
Darren Tuckere9405982007-05-20 15:09:04 +10001578
1579 /*
1580 * Process requests from client if we can fit the results
1581 * into the output buffer, otherwise stop processing input
1582 * and let the output queue drain.
1583 */
1584 if (buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1585 process();
Damien Miller7b28dc52000-09-05 13:34:53 +11001586 }
1587}