blob: 77834117c8075f065aeb7b8a2b3e41c753a88b7b [file] [log] [blame]
Darren Tucker2aca1592014-01-19 15:25:34 +11001/* $OpenBSD: sftp-server.c,v 1.103 2014/01/17 06:23:24 dtucker 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 Miller14928b72014-04-01 14:38:07 +110032#ifdef HAVE_SYS_PRCTL_H
33#include <sys/prctl.h>
34#endif
Damien Miller88f254b2006-03-15 11:25:13 +110035
36#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100037#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100038#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100039#include <pwd.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100040#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100041#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100042#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100043#include <pwd.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100044#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100046#include <stdarg.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110047
Damien Miller7b28dc52000-09-05 13:34:53 +110048#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100049#include "buffer.h"
50#include "log.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100051#include "misc.h"
Damien Miller6eaeebf2013-10-15 11:55:57 +110052#include "match.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100053#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110054
Ben Lindstrom2f959b42001-01-11 06:20:23 +000055#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110056#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110057
58/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000059#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110060#define get_int() buffer_get_int(&iqueue);
61#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Damien Miller7b28dc52000-09-05 13:34:53 +110062
Damien Millerfef95ad2006-07-10 20:46:55 +100063/* Our verbosity */
Damien Miller6eaeebf2013-10-15 11:55:57 +110064static LogLevel log_level = SYSLOG_LEVEL_ERROR;
Damien Millerfef95ad2006-07-10 20:46:55 +100065
66/* Our client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110067static struct passwd *pw = NULL;
68static char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000069
Damien Miller7b28dc52000-09-05 13:34:53 +110070/* input and output queue */
Damien Miller6eaeebf2013-10-15 11:55:57 +110071static Buffer iqueue;
72static Buffer oqueue;
Damien Miller7b28dc52000-09-05 13:34:53 +110073
Damien Miller058316f2001-03-08 10:08:49 +110074/* Version of client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110075static u_int version;
76
77/* SSH2_FXP_INIT received */
78static int init_done;
Damien Miller058316f2001-03-08 10:08:49 +110079
Darren Tuckerdb7bf822010-01-09 22:24:33 +110080/* Disable writes */
Damien Miller6eaeebf2013-10-15 11:55:57 +110081static int readonly;
82
83/* Requests that are allowed/denied */
84static char *request_whitelist, *request_blacklist;
Darren Tuckerdb7bf822010-01-09 22:24:33 +110085
Darren Tuckera6612d42003-06-28 12:39:03 +100086/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110087typedef struct Stat Stat;
88
Damien Miller33804262001-02-04 23:20:18 +110089struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110090 char *name;
91 char *long_name;
92 Attrib attrib;
93};
94
Damien Miller6eaeebf2013-10-15 11:55:57 +110095/* Packet handlers */
96static void process_open(u_int32_t id);
97static void process_close(u_int32_t id);
98static void process_read(u_int32_t id);
99static void process_write(u_int32_t id);
100static void process_stat(u_int32_t id);
101static void process_lstat(u_int32_t id);
102static void process_fstat(u_int32_t id);
103static void process_setstat(u_int32_t id);
104static void process_fsetstat(u_int32_t id);
105static void process_opendir(u_int32_t id);
106static void process_readdir(u_int32_t id);
107static void process_remove(u_int32_t id);
108static void process_mkdir(u_int32_t id);
109static void process_rmdir(u_int32_t id);
110static void process_realpath(u_int32_t id);
111static void process_rename(u_int32_t id);
112static void process_readlink(u_int32_t id);
113static void process_symlink(u_int32_t id);
114static void process_extended_posix_rename(u_int32_t id);
115static void process_extended_statvfs(u_int32_t id);
116static void process_extended_fstatvfs(u_int32_t id);
117static void process_extended_hardlink(u_int32_t id);
Damien Millerf29238e2013-10-17 11:48:52 +1100118static void process_extended_fsync(u_int32_t id);
Damien Miller6eaeebf2013-10-15 11:55:57 +1100119static void process_extended(u_int32_t id);
120
121struct sftp_handler {
122 const char *name; /* user-visible name for fine-grained perms */
123 const char *ext_name; /* extended request name */
124 u_int type; /* packet type, for non extended packets */
125 void (*handler)(u_int32_t);
126 int does_write; /* if nonzero, banned for readonly mode */
127};
128
129struct sftp_handler handlers[] = {
130 /* NB. SSH2_FXP_OPEN does the readonly check in the handler itself */
131 { "open", NULL, SSH2_FXP_OPEN, process_open, 0 },
132 { "close", NULL, SSH2_FXP_CLOSE, process_close, 0 },
133 { "read", NULL, SSH2_FXP_READ, process_read, 0 },
134 { "write", NULL, SSH2_FXP_WRITE, process_write, 1 },
135 { "lstat", NULL, SSH2_FXP_LSTAT, process_lstat, 0 },
136 { "fstat", NULL, SSH2_FXP_FSTAT, process_fstat, 0 },
137 { "setstat", NULL, SSH2_FXP_SETSTAT, process_setstat, 1 },
138 { "fsetstat", NULL, SSH2_FXP_FSETSTAT, process_fsetstat, 1 },
139 { "opendir", NULL, SSH2_FXP_OPENDIR, process_opendir, 0 },
140 { "readdir", NULL, SSH2_FXP_READDIR, process_readdir, 0 },
141 { "remove", NULL, SSH2_FXP_REMOVE, process_remove, 1 },
142 { "mkdir", NULL, SSH2_FXP_MKDIR, process_mkdir, 1 },
143 { "rmdir", NULL, SSH2_FXP_RMDIR, process_rmdir, 1 },
144 { "realpath", NULL, SSH2_FXP_REALPATH, process_realpath, 0 },
145 { "stat", NULL, SSH2_FXP_STAT, process_stat, 0 },
146 { "rename", NULL, SSH2_FXP_RENAME, process_rename, 1 },
147 { "readlink", NULL, SSH2_FXP_READLINK, process_readlink, 0 },
148 { "symlink", NULL, SSH2_FXP_SYMLINK, process_symlink, 1 },
149 { NULL, NULL, 0, NULL, 0 }
150};
151
152/* SSH2_FXP_EXTENDED submessages */
153struct sftp_handler extended_handlers[] = {
154 { "posix-rename", "posix-rename@openssh.com", 0,
155 process_extended_posix_rename, 1 },
156 { "statvfs", "statvfs@openssh.com", 0, process_extended_statvfs, 0 },
157 { "fstatvfs", "fstatvfs@openssh.com", 0, process_extended_fstatvfs, 0 },
158 { "hardlink", "hardlink@openssh.com", 0, process_extended_hardlink, 1 },
Damien Millerf29238e2013-10-17 11:48:52 +1100159 { "fsync", "fsync@openssh.com", 0, process_extended_fsync, 1 },
Damien Miller6eaeebf2013-10-15 11:55:57 +1100160 { NULL, NULL, 0, NULL, 0 }
161};
162
163static int
164request_permitted(struct sftp_handler *h)
165{
166 char *result;
167
168 if (readonly && h->does_write) {
169 verbose("Refusing %s request in read-only mode", h->name);
170 return 0;
171 }
172 if (request_blacklist != NULL &&
173 ((result = match_list(h->name, request_blacklist, NULL))) != NULL) {
174 free(result);
175 verbose("Refusing blacklisted %s request", h->name);
176 return 0;
177 }
178 if (request_whitelist != NULL &&
179 ((result = match_list(h->name, request_whitelist, NULL))) != NULL) {
180 free(result);
181 debug2("Permitting whitelisted %s request", h->name);
182 return 1;
183 }
184 if (request_whitelist != NULL) {
185 verbose("Refusing non-whitelisted %s request", h->name);
186 return 0;
187 }
188 return 1;
189}
190
Ben Lindstrombba81212001-06-25 05:01:22 +0000191static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100192errno_to_portable(int unixerrno)
193{
194 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000195
Damien Miller7b28dc52000-09-05 13:34:53 +1100196 switch (unixerrno) {
197 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000198 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100199 break;
200 case ENOENT:
201 case ENOTDIR:
202 case EBADF:
203 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000204 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100205 break;
206 case EPERM:
207 case EACCES:
208 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000209 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +1100210 break;
211 case ENAMETOOLONG:
212 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000213 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100214 break;
Darren Tucker422c34c2008-06-09 22:48:31 +1000215 case ENOSYS:
216 ret = SSH2_FX_OP_UNSUPPORTED;
217 break;
Damien Miller7b28dc52000-09-05 13:34:53 +1100218 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000219 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100220 break;
221 }
222 return ret;
223}
224
Ben Lindstrombba81212001-06-25 05:01:22 +0000225static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100226flags_from_portable(int pflags)
227{
228 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000229
Ben Lindstrom36592512001-03-05 05:02:08 +0000230 if ((pflags & SSH2_FXF_READ) &&
231 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100232 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000233 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100234 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000235 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100236 flags = O_WRONLY;
237 }
Damien Millere9fc72e2013-10-15 12:14:12 +1100238 if (pflags & SSH2_FXF_APPEND)
239 flags |= O_APPEND;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000240 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100241 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000242 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100243 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000244 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100245 flags |= O_EXCL;
246 return flags;
247}
248
Damien Millerfef95ad2006-07-10 20:46:55 +1000249static const char *
250string_from_portable(int pflags)
251{
252 static char ret[128];
253
254 *ret = '\0';
255
256#define PAPPEND(str) { \
257 if (*ret != '\0') \
258 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000259 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000260 }
261
262 if (pflags & SSH2_FXF_READ)
263 PAPPEND("READ")
264 if (pflags & SSH2_FXF_WRITE)
265 PAPPEND("WRITE")
Damien Millere9fc72e2013-10-15 12:14:12 +1100266 if (pflags & SSH2_FXF_APPEND)
267 PAPPEND("APPEND")
Damien Millerfef95ad2006-07-10 20:46:55 +1000268 if (pflags & SSH2_FXF_CREAT)
269 PAPPEND("CREATE")
270 if (pflags & SSH2_FXF_TRUNC)
271 PAPPEND("TRUNCATE")
272 if (pflags & SSH2_FXF_EXCL)
273 PAPPEND("EXCL")
274
275 return ret;
276}
277
Ben Lindstrombba81212001-06-25 05:01:22 +0000278static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100279get_attrib(void)
280{
281 return decode_attrib(&iqueue);
282}
283
284/* handle handles */
285
286typedef struct Handle Handle;
287struct Handle {
288 int use;
289 DIR *dirp;
290 int fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100291 int flags;
Damien Miller7b28dc52000-09-05 13:34:53 +1100292 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000293 u_int64_t bytes_read, bytes_write;
Damien Miller3397d0e2008-02-10 22:26:51 +1100294 int next_unused;
Damien Miller7b28dc52000-09-05 13:34:53 +1100295};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000296
Damien Miller7b28dc52000-09-05 13:34:53 +1100297enum {
298 HANDLE_UNUSED,
299 HANDLE_DIR,
300 HANDLE_FILE
301};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000302
Damien Miller3397d0e2008-02-10 22:26:51 +1100303Handle *handles = NULL;
304u_int num_handles = 0;
305int first_unused_handle = -1;
Damien Miller7b28dc52000-09-05 13:34:53 +1100306
Damien Miller3397d0e2008-02-10 22:26:51 +1100307static void handle_unused(int i)
Damien Miller7b28dc52000-09-05 13:34:53 +1100308{
Damien Miller3397d0e2008-02-10 22:26:51 +1100309 handles[i].use = HANDLE_UNUSED;
310 handles[i].next_unused = first_unused_handle;
311 first_unused_handle = i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100312}
313
Ben Lindstrombba81212001-06-25 05:01:22 +0000314static int
Damien Millere9fc72e2013-10-15 12:14:12 +1100315handle_new(int use, const char *name, int fd, int flags, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100316{
Damien Miller3397d0e2008-02-10 22:26:51 +1100317 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000318
Damien Miller3397d0e2008-02-10 22:26:51 +1100319 if (first_unused_handle == -1) {
320 if (num_handles + 1 <= num_handles)
321 return -1;
322 num_handles++;
323 handles = xrealloc(handles, num_handles, sizeof(Handle));
324 handle_unused(num_handles - 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100325 }
Damien Miller3397d0e2008-02-10 22:26:51 +1100326
327 i = first_unused_handle;
328 first_unused_handle = handles[i].next_unused;
329
330 handles[i].use = use;
331 handles[i].dirp = dirp;
332 handles[i].fd = fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100333 handles[i].flags = flags;
Damien Miller3397d0e2008-02-10 22:26:51 +1100334 handles[i].name = xstrdup(name);
335 handles[i].bytes_read = handles[i].bytes_write = 0;
336
337 return i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100338}
339
Ben Lindstrombba81212001-06-25 05:01:22 +0000340static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100341handle_is_ok(int i, int type)
342{
Damien Miller3397d0e2008-02-10 22:26:51 +1100343 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100344}
345
Ben Lindstrombba81212001-06-25 05:01:22 +0000346static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100347handle_to_string(int handle, char **stringp, int *hlenp)
348{
Damien Miller7b28dc52000-09-05 13:34:53 +1100349 if (stringp == NULL || hlenp == NULL)
350 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000351 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100352 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000353 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100354 return 0;
355}
356
Ben Lindstrombba81212001-06-25 05:01:22 +0000357static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100358handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100359{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000360 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000361
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000362 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100363 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100364 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100365 if (handle_is_ok(val, HANDLE_FILE) ||
366 handle_is_ok(val, HANDLE_DIR))
367 return val;
368 return -1;
369}
370
Ben Lindstrombba81212001-06-25 05:01:22 +0000371static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100372handle_to_name(int handle)
373{
374 if (handle_is_ok(handle, HANDLE_DIR)||
375 handle_is_ok(handle, HANDLE_FILE))
376 return handles[handle].name;
377 return NULL;
378}
379
Ben Lindstrombba81212001-06-25 05:01:22 +0000380static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100381handle_to_dir(int handle)
382{
383 if (handle_is_ok(handle, HANDLE_DIR))
384 return handles[handle].dirp;
385 return NULL;
386}
387
Ben Lindstrombba81212001-06-25 05:01:22 +0000388static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100389handle_to_fd(int handle)
390{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000391 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100392 return handles[handle].fd;
393 return -1;
394}
395
Damien Millere9fc72e2013-10-15 12:14:12 +1100396static int
397handle_to_flags(int handle)
398{
399 if (handle_is_ok(handle, HANDLE_FILE))
400 return handles[handle].flags;
401 return 0;
402}
403
Damien Millerfef95ad2006-07-10 20:46:55 +1000404static void
405handle_update_read(int handle, ssize_t bytes)
406{
407 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
408 handles[handle].bytes_read += bytes;
409}
410
411static void
412handle_update_write(int handle, ssize_t bytes)
413{
414 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
415 handles[handle].bytes_write += bytes;
416}
417
418static u_int64_t
419handle_bytes_read(int handle)
420{
421 if (handle_is_ok(handle, HANDLE_FILE))
422 return (handles[handle].bytes_read);
423 return 0;
424}
425
426static u_int64_t
427handle_bytes_write(int handle)
428{
429 if (handle_is_ok(handle, HANDLE_FILE))
430 return (handles[handle].bytes_write);
431 return 0;
432}
433
Ben Lindstrombba81212001-06-25 05:01:22 +0000434static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100435handle_close(int handle)
436{
437 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000438
Damien Miller7b28dc52000-09-05 13:34:53 +1100439 if (handle_is_ok(handle, HANDLE_FILE)) {
440 ret = close(handles[handle].fd);
Darren Tuckera627d422013-06-02 07:31:17 +1000441 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100442 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100443 } else if (handle_is_ok(handle, HANDLE_DIR)) {
444 ret = closedir(handles[handle].dirp);
Darren Tuckera627d422013-06-02 07:31:17 +1000445 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100446 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100447 } else {
448 errno = ENOENT;
449 }
450 return ret;
451}
452
Damien Millerfef95ad2006-07-10 20:46:55 +1000453static void
454handle_log_close(int handle, char *emsg)
455{
456 if (handle_is_ok(handle, HANDLE_FILE)) {
457 logit("%s%sclose \"%s\" bytes read %llu written %llu",
458 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
459 handle_to_name(handle),
Darren Tucker86473c52007-05-20 14:59:32 +1000460 (unsigned long long)handle_bytes_read(handle),
461 (unsigned long long)handle_bytes_write(handle));
Damien Millerfef95ad2006-07-10 20:46:55 +1000462 } else {
463 logit("%s%sclosedir \"%s\"",
464 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
465 handle_to_name(handle));
466 }
467}
468
469static void
470handle_log_exit(void)
471{
472 u_int i;
473
Damien Miller3397d0e2008-02-10 22:26:51 +1100474 for (i = 0; i < num_handles; i++)
Damien Millerfef95ad2006-07-10 20:46:55 +1000475 if (handles[i].use != HANDLE_UNUSED)
476 handle_log_close(i, "forced");
477}
478
Ben Lindstrombba81212001-06-25 05:01:22 +0000479static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100480get_handle(void)
481{
482 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000483 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100484 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000485
Damien Miller7b28dc52000-09-05 13:34:53 +1100486 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000487 if (hlen < 256)
488 val = handle_from_string(handle, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000489 free(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100490 return val;
491}
492
493/* send replies */
494
Ben Lindstrombba81212001-06-25 05:01:22 +0000495static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100496send_msg(Buffer *m)
497{
498 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000499
Damien Miller7b28dc52000-09-05 13:34:53 +1100500 buffer_put_int(&oqueue, mlen);
501 buffer_append(&oqueue, buffer_ptr(m), mlen);
502 buffer_consume(m, mlen);
503}
504
Damien Millerfef95ad2006-07-10 20:46:55 +1000505static const char *
506status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100507{
Damien Miller058316f2001-03-08 10:08:49 +1100508 const char *status_messages[] = {
509 "Success", /* SSH_FX_OK */
510 "End of file", /* SSH_FX_EOF */
511 "No such file", /* SSH_FX_NO_SUCH_FILE */
512 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
513 "Failure", /* SSH_FX_FAILURE */
514 "Bad message", /* SSH_FX_BAD_MESSAGE */
515 "No connection", /* SSH_FX_NO_CONNECTION */
516 "Connection lost", /* SSH_FX_CONNECTION_LOST */
517 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
518 "Unknown error" /* Others */
519 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000520 return (status_messages[MIN(status,SSH2_FX_MAX)]);
521}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000522
Damien Millerfef95ad2006-07-10 20:46:55 +1000523static void
524send_status(u_int32_t id, u_int32_t status)
525{
526 Buffer msg;
527
528 debug3("request %u: sent status %u", id, status);
529 if (log_level > SYSLOG_LEVEL_VERBOSE ||
530 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
531 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100532 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000533 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100534 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000535 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100536 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000537 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100538 buffer_put_cstring(&msg, "");
539 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100540 send_msg(&msg);
541 buffer_free(&msg);
542}
Ben Lindstrombba81212001-06-25 05:01:22 +0000543static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100544send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100545{
546 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000547
Damien Miller7b28dc52000-09-05 13:34:53 +1100548 buffer_init(&msg);
549 buffer_put_char(&msg, type);
550 buffer_put_int(&msg, id);
551 buffer_put_string(&msg, data, dlen);
552 send_msg(&msg);
553 buffer_free(&msg);
554}
555
Ben Lindstrombba81212001-06-25 05:01:22 +0000556static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100557send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100558{
Damien Millerfef95ad2006-07-10 20:46:55 +1000559 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000560 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100561}
562
Ben Lindstrombba81212001-06-25 05:01:22 +0000563static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100564send_handle(u_int32_t id, int handle)
565{
566 char *string;
567 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000568
Damien Miller7b28dc52000-09-05 13:34:53 +1100569 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000570 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000571 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000572 free(string);
Damien Miller7b28dc52000-09-05 13:34:53 +1100573}
574
Ben Lindstrombba81212001-06-25 05:01:22 +0000575static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100576send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100577{
578 Buffer msg;
579 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000580
Damien Miller7b28dc52000-09-05 13:34:53 +1100581 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000582 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100583 buffer_put_int(&msg, id);
584 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000585 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100586 for (i = 0; i < count; i++) {
587 buffer_put_cstring(&msg, stats[i].name);
588 buffer_put_cstring(&msg, stats[i].long_name);
589 encode_attrib(&msg, &stats[i].attrib);
590 }
591 send_msg(&msg);
592 buffer_free(&msg);
593}
594
Ben Lindstrombba81212001-06-25 05:01:22 +0000595static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100596send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100597{
598 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000599
Damien Millerfef95ad2006-07-10 20:46:55 +1000600 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100601 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000602 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100603 buffer_put_int(&msg, id);
604 encode_attrib(&msg, a);
605 send_msg(&msg);
606 buffer_free(&msg);
607}
608
Damien Millerd671e5a2008-05-19 14:53:33 +1000609static void
610send_statvfs(u_int32_t id, struct statvfs *st)
611{
612 Buffer msg;
613 u_int64_t flag;
614
615 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
616 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
617
618 buffer_init(&msg);
619 buffer_put_char(&msg, SSH2_FXP_EXTENDED_REPLY);
620 buffer_put_int(&msg, id);
Darren Tucker3463aca2008-06-09 23:06:55 +1000621 buffer_put_int64(&msg, st->f_bsize);
622 buffer_put_int64(&msg, st->f_frsize);
Damien Millerd671e5a2008-05-19 14:53:33 +1000623 buffer_put_int64(&msg, st->f_blocks);
624 buffer_put_int64(&msg, st->f_bfree);
625 buffer_put_int64(&msg, st->f_bavail);
626 buffer_put_int64(&msg, st->f_files);
627 buffer_put_int64(&msg, st->f_ffree);
628 buffer_put_int64(&msg, st->f_favail);
Darren Tucker77001382008-06-09 06:17:53 +1000629 buffer_put_int64(&msg, FSID_TO_ULONG(st->f_fsid));
Darren Tucker3463aca2008-06-09 23:06:55 +1000630 buffer_put_int64(&msg, flag);
631 buffer_put_int64(&msg, st->f_namemax);
Damien Millerd671e5a2008-05-19 14:53:33 +1000632 send_msg(&msg);
633 buffer_free(&msg);
634}
635
Damien Miller7b28dc52000-09-05 13:34:53 +1100636/* parse incoming */
637
Ben Lindstrombba81212001-06-25 05:01:22 +0000638static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100639process_init(void)
640{
641 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100642
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000643 version = get_int();
Damien Millerf145a5b2011-06-20 14:42:51 +1000644 verbose("received client version %u", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100645 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000646 buffer_put_char(&msg, SSH2_FXP_VERSION);
647 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7c296612008-03-07 18:33:53 +1100648 /* POSIX rename extension */
649 buffer_put_cstring(&msg, "posix-rename@openssh.com");
650 buffer_put_cstring(&msg, "1"); /* version */
Damien Millera7e0d5a2008-05-19 16:08:41 +1000651 /* statvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000652 buffer_put_cstring(&msg, "statvfs@openssh.com");
Darren Tucker294b8412008-06-08 12:57:08 +1000653 buffer_put_cstring(&msg, "2"); /* version */
Damien Millera7e0d5a2008-05-19 16:08:41 +1000654 /* fstatvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000655 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
Darren Tucker294b8412008-06-08 12:57:08 +1000656 buffer_put_cstring(&msg, "2"); /* version */
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100657 /* hardlink extension */
658 buffer_put_cstring(&msg, "hardlink@openssh.com");
659 buffer_put_cstring(&msg, "1"); /* version */
Damien Millerf29238e2013-10-17 11:48:52 +1100660 /* fsync extension */
661 buffer_put_cstring(&msg, "fsync@openssh.com");
662 buffer_put_cstring(&msg, "1"); /* version */
Damien Miller7b28dc52000-09-05 13:34:53 +1100663 send_msg(&msg);
664 buffer_free(&msg);
665}
666
Ben Lindstrombba81212001-06-25 05:01:22 +0000667static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100668process_open(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100669{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100670 u_int32_t pflags;
Damien Miller7b28dc52000-09-05 13:34:53 +1100671 Attrib *a;
672 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000673 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100674
Damien Miller7b28dc52000-09-05 13:34:53 +1100675 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000676 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000677 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100678 a = get_attrib();
679 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000680 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000681 logit("open \"%s\" flags %s mode 0%o",
682 name, string_from_portable(pflags), mode);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100683 if (readonly &&
Damien Miller6eaeebf2013-10-15 11:55:57 +1100684 ((flags & O_ACCMODE) == O_WRONLY ||
685 (flags & O_ACCMODE) == O_RDWR)) {
686 verbose("Refusing open request in read-only mode");
687 status = SSH2_FX_PERMISSION_DENIED;
688 } else {
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100689 fd = open(name, flags, mode);
690 if (fd < 0) {
691 status = errno_to_portable(errno);
Damien Miller7b28dc52000-09-05 13:34:53 +1100692 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100693 handle = handle_new(HANDLE_FILE, name, fd, flags, NULL);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100694 if (handle < 0) {
695 close(fd);
696 } else {
697 send_handle(id, handle);
698 status = SSH2_FX_OK;
699 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100700 }
701 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000702 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100703 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000704 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100705}
706
Ben Lindstrombba81212001-06-25 05:01:22 +0000707static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100708process_close(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100709{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000710 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100711
Damien Miller7b28dc52000-09-05 13:34:53 +1100712 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000713 debug3("request %u: close handle %u", id, handle);
714 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100715 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000716 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100717 send_status(id, status);
718}
719
Ben Lindstrombba81212001-06-25 05:01:22 +0000720static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100721process_read(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100722{
723 char buf[64*1024];
Damien Miller6eaeebf2013-10-15 11:55:57 +1100724 u_int32_t len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000725 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100726 u_int64_t off;
727
Damien Miller7b28dc52000-09-05 13:34:53 +1100728 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000729 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100730 len = get_int();
731
Damien Millerfef95ad2006-07-10 20:46:55 +1000732 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
733 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100734 if (len > sizeof buf) {
735 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000736 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100737 }
738 fd = handle_to_fd(handle);
739 if (fd >= 0) {
740 if (lseek(fd, off, SEEK_SET) < 0) {
741 error("process_read: seek failed");
742 status = errno_to_portable(errno);
743 } else {
744 ret = read(fd, buf, len);
745 if (ret < 0) {
746 status = errno_to_portable(errno);
747 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000748 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100749 } else {
750 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000751 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000752 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100753 }
754 }
755 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000756 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100757 send_status(id, status);
758}
759
Ben Lindstrombba81212001-06-25 05:01:22 +0000760static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100761process_write(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100762{
Damien Miller7b28dc52000-09-05 13:34:53 +1100763 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100764 u_int len;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100765 int handle, fd, ret, status;
Damien Miller7b28dc52000-09-05 13:34:53 +1100766 char *data;
767
Damien Miller7b28dc52000-09-05 13:34:53 +1100768 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000769 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100770 data = get_string(&len);
771
Damien Millerfef95ad2006-07-10 20:46:55 +1000772 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
773 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100774 fd = handle_to_fd(handle);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100775
776 if (fd < 0)
777 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100778 else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100779 if (!(handle_to_flags(handle) & O_APPEND) &&
780 lseek(fd, off, SEEK_SET) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100781 status = errno_to_portable(errno);
782 error("process_write: seek failed");
783 } else {
784/* XXX ATOMICIO ? */
785 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000786 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100787 error("process_write: write failed");
788 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000789 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000790 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000791 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100792 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000793 debug2("nothing at all written");
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100794 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100795 }
796 }
797 }
798 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000799 free(data);
Damien Miller7b28dc52000-09-05 13:34:53 +1100800}
801
Ben Lindstrombba81212001-06-25 05:01:22 +0000802static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100803process_do_stat(u_int32_t id, int do_lstat)
Damien Miller7b28dc52000-09-05 13:34:53 +1100804{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000805 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100806 struct stat st;
Damien Miller7b28dc52000-09-05 13:34:53 +1100807 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000808 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100809
Damien Miller7b28dc52000-09-05 13:34:53 +1100810 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000811 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
812 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100813 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
814 if (ret < 0) {
815 status = errno_to_portable(errno);
816 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000817 stat_to_attrib(&st, &a);
818 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000819 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100820 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000821 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100822 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000823 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100824}
825
Ben Lindstrombba81212001-06-25 05:01:22 +0000826static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100827process_stat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100828{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100829 process_do_stat(id, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100830}
831
Ben Lindstrombba81212001-06-25 05:01:22 +0000832static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100833process_lstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100834{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100835 process_do_stat(id, 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100836}
837
Ben Lindstrombba81212001-06-25 05:01:22 +0000838static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100839process_fstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100840{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000841 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100842 struct stat st;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000843 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100844
Damien Miller7b28dc52000-09-05 13:34:53 +1100845 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000846 debug("request %u: fstat \"%s\" (handle %u)",
847 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100848 fd = handle_to_fd(handle);
Damien Millere2334d62007-01-05 16:31:02 +1100849 if (fd >= 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100850 ret = fstat(fd, &st);
851 if (ret < 0) {
852 status = errno_to_portable(errno);
853 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000854 stat_to_attrib(&st, &a);
855 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000856 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100857 }
858 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000859 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100860 send_status(id, status);
861}
862
Ben Lindstrombba81212001-06-25 05:01:22 +0000863static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100864attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100865{
866 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000867
Damien Miller7b28dc52000-09-05 13:34:53 +1100868 tv[0].tv_sec = a->atime;
869 tv[0].tv_usec = 0;
870 tv[1].tv_sec = a->mtime;
871 tv[1].tv_usec = 0;
872 return tv;
873}
874
Ben Lindstrombba81212001-06-25 05:01:22 +0000875static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100876process_setstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100877{
878 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100879 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000880 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100881
Damien Miller7b28dc52000-09-05 13:34:53 +1100882 name = get_string(NULL);
883 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000884 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100885 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000886 logit("set \"%s\" size %llu",
887 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100888 ret = truncate(name, a->size);
889 if (ret == -1)
890 status = errno_to_portable(errno);
891 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000892 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000893 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller9e720282008-06-29 22:46:35 +1000894 ret = chmod(name, a->perm & 07777);
Damien Miller7b28dc52000-09-05 13:34:53 +1100895 if (ret == -1)
896 status = errno_to_portable(errno);
897 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000898 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000899 char buf[64];
900 time_t t = a->mtime;
901
902 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
903 localtime(&t));
904 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100905 ret = utimes(name, attrib_to_tv(a));
906 if (ret == -1)
907 status = errno_to_portable(errno);
908 }
Kevin Steves8e743932001-02-05 13:24:35 +0000909 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000910 logit("set \"%s\" owner %lu group %lu", name,
911 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000912 ret = chown(name, a->uid, a->gid);
913 if (ret == -1)
914 status = errno_to_portable(errno);
915 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100916 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000917 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100918}
919
Ben Lindstrombba81212001-06-25 05:01:22 +0000920static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100921process_fsetstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100922{
923 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100924 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000925 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000926
Damien Miller7b28dc52000-09-05 13:34:53 +1100927 handle = get_handle();
928 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000929 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100930 fd = handle_to_fd(handle);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100931 if (fd < 0)
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000932 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100933 else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000934 char *name = handle_to_name(handle);
935
Damien Miller00c92172002-02-13 14:05:00 +1100936 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000937 logit("set \"%s\" size %llu",
938 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100939 ret = ftruncate(fd, a->size);
940 if (ret == -1)
941 status = errno_to_portable(errno);
942 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000943 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000944 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000945#ifdef HAVE_FCHMOD
Damien Miller9e720282008-06-29 22:46:35 +1000946 ret = fchmod(fd, a->perm & 07777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000947#else
Damien Miller9e720282008-06-29 22:46:35 +1000948 ret = chmod(name, a->perm & 07777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000949#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100950 if (ret == -1)
951 status = errno_to_portable(errno);
952 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000953 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000954 char buf[64];
955 time_t t = a->mtime;
956
957 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
958 localtime(&t));
959 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100960#ifdef HAVE_FUTIMES
961 ret = futimes(fd, attrib_to_tv(a));
962#else
963 ret = utimes(name, attrib_to_tv(a));
964#endif
965 if (ret == -1)
966 status = errno_to_portable(errno);
967 }
Kevin Steves8e743932001-02-05 13:24:35 +0000968 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000969 logit("set \"%s\" owner %lu group %lu", name,
970 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000971#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000972 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000973#else
974 ret = chown(name, a->uid, a->gid);
975#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000976 if (ret == -1)
977 status = errno_to_portable(errno);
978 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100979 }
980 send_status(id, status);
981}
982
Ben Lindstrombba81212001-06-25 05:01:22 +0000983static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100984process_opendir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100985{
986 DIR *dirp = NULL;
987 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000988 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100989
Damien Miller7b28dc52000-09-05 13:34:53 +1100990 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000991 debug3("request %u: opendir", id);
992 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000993 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100994 if (dirp == NULL) {
995 status = errno_to_portable(errno);
996 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100997 handle = handle_new(HANDLE_DIR, path, 0, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100998 if (handle < 0) {
999 closedir(dirp);
1000 } else {
1001 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001002 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001003 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001004
Damien Miller7b28dc52000-09-05 13:34:53 +11001005 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001006 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +11001007 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001008 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001009}
1010
Ben Lindstrombba81212001-06-25 05:01:22 +00001011static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001012process_readdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001013{
1014 DIR *dirp;
1015 struct dirent *dp;
1016 char *path;
1017 int handle;
Damien Miller7b28dc52000-09-05 13:34:53 +11001018
Damien Miller7b28dc52000-09-05 13:34:53 +11001019 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +10001020 debug("request %u: readdir \"%s\" (handle %d)", id,
1021 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +11001022 dirp = handle_to_dir(handle);
1023 path = handle_to_name(handle);
1024 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001025 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +11001026 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +11001027 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +10001028 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +11001029 Stat *stats;
1030 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001031
Damien Miller07d86be2006-03-26 14:19:21 +11001032 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001033 while ((dp = readdir(dirp)) != NULL) {
1034 if (count >= nstats) {
1035 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +11001036 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001037 }
1038/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +00001039 snprintf(pathname, sizeof pathname, "%s%s%s", path,
1040 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001041 if (lstat(pathname, &st) < 0)
1042 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001043 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +11001044 stats[count].name = xstrdup(dp->d_name);
Darren Tucker2901e2d2010-01-13 22:44:06 +11001045 stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001046 count++;
1047 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001048 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +11001049 if (count == 100)
1050 break;
1051 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001052 if (count > 0) {
1053 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001054 for (i = 0; i < count; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001055 free(stats[i].name);
1056 free(stats[i].long_name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001057 }
1058 } else {
1059 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +11001060 }
Darren Tuckera627d422013-06-02 07:31:17 +10001061 free(stats);
Damien Miller7b28dc52000-09-05 13:34:53 +11001062 }
1063}
1064
Ben Lindstrombba81212001-06-25 05:01:22 +00001065static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001066process_remove(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001067{
1068 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001069 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001070 int ret;
1071
Damien Miller7b28dc52000-09-05 13:34:53 +11001072 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001073 debug3("request %u: remove", id);
1074 logit("remove name \"%s\"", name);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001075 ret = unlink(name);
1076 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001077 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001078 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001079}
1080
Ben Lindstrombba81212001-06-25 05:01:22 +00001081static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001082process_mkdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001083{
1084 Attrib *a;
Damien Miller7b28dc52000-09-05 13:34:53 +11001085 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001086 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001087
Damien Miller7b28dc52000-09-05 13:34:53 +11001088 name = get_string(NULL);
1089 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001090 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
Damien Miller9e720282008-06-29 22:46:35 +10001091 a->perm & 07777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +10001092 debug3("request %u: mkdir", id);
1093 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001094 ret = mkdir(name, mode);
1095 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001096 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001097 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001098}
1099
Ben Lindstrombba81212001-06-25 05:01:22 +00001100static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001101process_rmdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001102{
Damien Miller7b28dc52000-09-05 13:34:53 +11001103 char *name;
1104 int ret, status;
1105
Damien Miller7b28dc52000-09-05 13:34:53 +11001106 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001107 debug3("request %u: rmdir", id);
1108 logit("rmdir name \"%s\"", name);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001109 ret = rmdir(name);
1110 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001111 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001112 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001113}
1114
Ben Lindstrombba81212001-06-25 05:01:22 +00001115static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001116process_realpath(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001117{
1118 char resolvedname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +11001119 char *path;
1120
Damien Miller7b28dc52000-09-05 13:34:53 +11001121 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001122 if (path[0] == '\0') {
Darren Tuckera627d422013-06-02 07:31:17 +10001123 free(path);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001124 path = xstrdup(".");
1125 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001126 debug3("request %u: realpath", id);
1127 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001128 if (realpath(path, resolvedname) == NULL) {
1129 send_status(id, errno_to_portable(errno));
1130 } else {
1131 Stat s;
1132 attrib_clear(&s.attrib);
1133 s.name = s.long_name = resolvedname;
1134 send_names(id, 1, &s);
1135 }
Darren Tuckera627d422013-06-02 07:31:17 +10001136 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001137}
1138
Ben Lindstrombba81212001-06-25 05:01:22 +00001139static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001140process_rename(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001141{
Damien Miller7b28dc52000-09-05 13:34:53 +11001142 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001143 int status;
Damien Millerb3207e82003-03-26 16:01:11 +11001144 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +11001145
Damien Miller7b28dc52000-09-05 13:34:53 +11001146 oldpath = get_string(NULL);
1147 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001148 debug3("request %u: rename", id);
1149 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +11001150 status = SSH2_FX_FAILURE;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001151 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +11001152 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +11001153 else if (S_ISREG(sb.st_mode)) {
1154 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001155 if (link(oldpath, newpath) == -1) {
Damien Miller0e265512009-08-28 10:43:13 +10001156 if (errno == EOPNOTSUPP || errno == ENOSYS
Darren Tuckerf7fa7062008-07-04 14:10:19 +10001157#ifdef EXDEV
1158 || errno == EXDEV
1159#endif
Darren Tuckere59b5082004-06-28 16:01:19 +10001160#ifdef LINK_OPNOTSUPP_ERRNO
1161 || errno == LINK_OPNOTSUPP_ERRNO
1162#endif
1163 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001164 struct stat st;
1165
1166 /*
1167 * fs doesn't support links, so fall back to
1168 * stat+rename. This is racy.
1169 */
1170 if (stat(newpath, &st) == -1) {
1171 if (rename(oldpath, newpath) == -1)
1172 status =
1173 errno_to_portable(errno);
1174 else
1175 status = SSH2_FX_OK;
1176 }
1177 } else {
1178 status = errno_to_portable(errno);
1179 }
1180 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001181 status = errno_to_portable(errno);
1182 /* clean spare link */
1183 unlink(newpath);
1184 } else
1185 status = SSH2_FX_OK;
1186 } else if (stat(newpath, &sb) == -1) {
1187 if (rename(oldpath, newpath) == -1)
1188 status = errno_to_portable(errno);
1189 else
1190 status = SSH2_FX_OK;
1191 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001192 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001193 free(oldpath);
1194 free(newpath);
Damien Miller7b28dc52000-09-05 13:34:53 +11001195}
1196
Ben Lindstrombba81212001-06-25 05:01:22 +00001197static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001198process_readlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001199{
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001200 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001201 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001202 char *path;
1203
Damien Miller058316f2001-03-08 10:08:49 +11001204 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001205 debug3("request %u: readlink", id);
1206 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001207 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001208 send_status(id, errno_to_portable(errno));
1209 else {
1210 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001211
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001212 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001213 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001214 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001215 send_names(id, 1, &s);
1216 }
Darren Tuckera627d422013-06-02 07:31:17 +10001217 free(path);
Damien Miller058316f2001-03-08 10:08:49 +11001218}
1219
Ben Lindstrombba81212001-06-25 05:01:22 +00001220static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001221process_symlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001222{
Damien Miller058316f2001-03-08 10:08:49 +11001223 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001224 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001225
Damien Miller058316f2001-03-08 10:08:49 +11001226 oldpath = get_string(NULL);
1227 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001228 debug3("request %u: symlink", id);
1229 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001230 /* this will fail if 'newpath' exists */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001231 ret = symlink(oldpath, newpath);
1232 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001233 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001234 free(oldpath);
1235 free(newpath);
Damien Miller058316f2001-03-08 10:08:49 +11001236}
1237
Ben Lindstrombba81212001-06-25 05:01:22 +00001238static void
Damien Miller7c296612008-03-07 18:33:53 +11001239process_extended_posix_rename(u_int32_t id)
1240{
1241 char *oldpath, *newpath;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001242 int ret, status;
Damien Miller7c296612008-03-07 18:33:53 +11001243
1244 oldpath = get_string(NULL);
1245 newpath = get_string(NULL);
1246 debug3("request %u: posix-rename", id);
1247 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001248 ret = rename(oldpath, newpath);
1249 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001250 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001251 free(oldpath);
1252 free(newpath);
Damien Miller7c296612008-03-07 18:33:53 +11001253}
1254
1255static void
Damien Millerd671e5a2008-05-19 14:53:33 +10001256process_extended_statvfs(u_int32_t id)
1257{
1258 char *path;
1259 struct statvfs st;
1260
1261 path = get_string(NULL);
Darren Tucker2aca1592014-01-19 15:25:34 +11001262 debug3("request %u: statvfs", id);
1263 logit("statvfs \"%s\"", path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001264
1265 if (statvfs(path, &st) != 0)
1266 send_status(id, errno_to_portable(errno));
1267 else
1268 send_statvfs(id, &st);
Darren Tuckera627d422013-06-02 07:31:17 +10001269 free(path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001270}
1271
1272static void
1273process_extended_fstatvfs(u_int32_t id)
1274{
1275 int handle, fd;
1276 struct statvfs st;
1277
1278 handle = get_handle();
1279 debug("request %u: fstatvfs \"%s\" (handle %u)",
1280 id, handle_to_name(handle), handle);
1281 if ((fd = handle_to_fd(handle)) < 0) {
1282 send_status(id, SSH2_FX_FAILURE);
1283 return;
1284 }
1285 if (fstatvfs(fd, &st) != 0)
1286 send_status(id, errno_to_portable(errno));
1287 else
1288 send_statvfs(id, &st);
1289}
1290
1291static void
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001292process_extended_hardlink(u_int32_t id)
1293{
1294 char *oldpath, *newpath;
1295 int ret, status;
1296
1297 oldpath = get_string(NULL);
1298 newpath = get_string(NULL);
1299 debug3("request %u: hardlink", id);
1300 logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001301 ret = link(oldpath, newpath);
1302 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001303 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001304 free(oldpath);
1305 free(newpath);
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001306}
1307
1308static void
Damien Millerf29238e2013-10-17 11:48:52 +11001309process_extended_fsync(u_int32_t id)
1310{
1311 int handle, fd, ret, status = SSH2_FX_OP_UNSUPPORTED;
1312
1313 handle = get_handle();
1314 debug3("request %u: fsync (handle %u)", id, handle);
1315 verbose("fsync \"%s\"", handle_to_name(handle));
1316 if ((fd = handle_to_fd(handle)) < 0)
1317 status = SSH2_FX_NO_SUCH_FILE;
1318 else if (handle_is_ok(handle, HANDLE_FILE)) {
1319 ret = fsync(fd);
1320 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1321 }
1322 send_status(id, status);
1323}
1324
1325static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001326process_extended(u_int32_t id)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001327{
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001328 char *request;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001329 u_int i;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001330
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001331 request = get_string(NULL);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001332 for (i = 0; extended_handlers[i].handler != NULL; i++) {
1333 if (strcmp(request, extended_handlers[i].ext_name) == 0) {
1334 if (!request_permitted(&extended_handlers[i]))
1335 send_status(id, SSH2_FX_PERMISSION_DENIED);
1336 else
1337 extended_handlers[i].handler(id);
1338 break;
1339 }
1340 }
1341 if (extended_handlers[i].handler == NULL) {
1342 error("Unknown extended request \"%.100s\"", request);
Damien Miller7c296612008-03-07 18:33:53 +11001343 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001344 }
Darren Tuckera627d422013-06-02 07:31:17 +10001345 free(request);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001346}
Damien Miller7b28dc52000-09-05 13:34:53 +11001347
1348/* stolen from ssh-agent */
1349
Ben Lindstrombba81212001-06-25 05:01:22 +00001350static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001351process(void)
1352{
Damien Miller6eaeebf2013-10-15 11:55:57 +11001353 u_int msg_len, buf_len, consumed, type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001354 u_char *cp;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001355 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +11001356
Ben Lindstrom2c140472002-06-06 21:57:54 +00001357 buf_len = buffer_len(&iqueue);
1358 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001359 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001360 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001361 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001362 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001363 error("bad message from %s local user %s",
1364 client_addr, pw->pw_name);
Damien Millerdfc24252008-02-10 22:29:40 +11001365 sftp_server_cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001366 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001367 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001368 return;
1369 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001370 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001371 type = buffer_get_char(&iqueue);
Damien Miller6eaeebf2013-10-15 11:55:57 +11001372
Damien Miller7b28dc52000-09-05 13:34:53 +11001373 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001374 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001375 process_init();
Damien Miller6eaeebf2013-10-15 11:55:57 +11001376 init_done = 1;
Damien Miller058316f2001-03-08 10:08:49 +11001377 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001378 case SSH2_FXP_EXTENDED:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001379 if (!init_done)
1380 fatal("Received extended request before init");
1381 id = get_int();
1382 process_extended(id);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001383 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001384 default:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001385 if (!init_done)
1386 fatal("Received %u request before init", type);
1387 id = get_int();
1388 for (i = 0; handlers[i].handler != NULL; i++) {
1389 if (type == handlers[i].type) {
1390 if (!request_permitted(&handlers[i])) {
1391 send_status(id,
1392 SSH2_FX_PERMISSION_DENIED);
1393 } else {
1394 handlers[i].handler(id);
1395 }
1396 break;
1397 }
1398 }
1399 if (handlers[i].handler == NULL)
1400 error("Unknown message %u", type);
Damien Miller7b28dc52000-09-05 13:34:53 +11001401 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001402 /* discard the remaining bytes from the current packet */
Damien Millerdfc24252008-02-10 22:29:40 +11001403 if (buf_len < buffer_len(&iqueue)) {
1404 error("iqueue grew unexpectedly");
1405 sftp_server_cleanup_exit(255);
1406 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001407 consumed = buf_len - buffer_len(&iqueue);
Damien Millerdfc24252008-02-10 22:29:40 +11001408 if (msg_len < consumed) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001409 error("msg_len %u < consumed %u", msg_len, consumed);
Damien Millerdfc24252008-02-10 22:29:40 +11001410 sftp_server_cleanup_exit(255);
1411 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001412 if (msg_len > consumed)
1413 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001414}
1415
Damien Millerfef95ad2006-07-10 20:46:55 +10001416/* Cleanup handler that logs active handles upon normal exit */
1417void
Damien Millerdfc24252008-02-10 22:29:40 +11001418sftp_server_cleanup_exit(int i)
Damien Millerfef95ad2006-07-10 20:46:55 +10001419{
1420 if (pw != NULL && client_addr != NULL) {
1421 handle_log_exit();
1422 logit("session closed for local user %s from [%s]",
1423 pw->pw_name, client_addr);
1424 }
1425 _exit(i);
1426}
1427
1428static void
Damien Millerdfc24252008-02-10 22:29:40 +11001429sftp_server_usage(void)
Damien Millerfef95ad2006-07-10 20:46:55 +10001430{
1431 extern char *__progname;
1432
1433 fprintf(stderr,
Damien Milleraa7ad302013-01-09 15:58:21 +11001434 "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
Damien Miller6efab272013-10-15 12:07:05 +11001435 "[-l log_level]\n\t[-P blacklisted_requests] "
1436 "[-p whitelisted_requests] [-u umask]\n"
1437 " %s -Q protocol_feature\n",
1438 __progname, __progname);
Damien Millerfef95ad2006-07-10 20:46:55 +10001439 exit(1);
1440}
1441
Damien Miller7b28dc52000-09-05 13:34:53 +11001442int
Damien Millerd8cb1f12008-02-10 22:40:12 +11001443sftp_server_main(int argc, char **argv, struct passwd *user_pw)
Damien Miller7b28dc52000-09-05 13:34:53 +11001444{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001445 fd_set *rset, *wset;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001446 int i, in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001447 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001448 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
Damien Miller502ab0e2013-01-09 15:57:36 +11001449 char *cp, *homedir = NULL, buf[4*4096];
Damien Miller07331212010-11-05 10:20:31 +11001450 long mask;
Damien Millerfef95ad2006-07-10 20:46:55 +10001451
Damien Millerfef95ad2006-07-10 20:46:55 +10001452 extern char *optarg;
1453 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001454
Damien Millerfef95ad2006-07-10 20:46:55 +10001455 __progname = ssh_get_progname(argv[0]);
1456 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001457
Damien Miller502ab0e2013-01-09 15:57:36 +11001458 pw = pwcopy(user_pw);
1459
Damien Miller6eaeebf2013-10-15 11:55:57 +11001460 while (!skipargs && (ch = getopt(argc, argv,
1461 "d:f:l:P:p:Q:u:cehR")) != -1) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001462 switch (ch) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001463 case 'Q':
1464 if (strcasecmp(optarg, "requests") != 0) {
1465 fprintf(stderr, "Invalid query type\n");
1466 exit(1);
1467 }
1468 for (i = 0; handlers[i].handler != NULL; i++)
1469 printf("%s\n", handlers[i].name);
1470 for (i = 0; extended_handlers[i].handler != NULL; i++)
1471 printf("%s\n", extended_handlers[i].name);
1472 exit(0);
1473 break;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001474 case 'R':
1475 readonly = 1;
1476 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001477 case 'c':
1478 /*
1479 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001480 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001481 */
1482 skipargs = 1;
1483 break;
1484 case 'e':
1485 log_stderr = 1;
1486 break;
1487 case 'l':
1488 log_level = log_level_number(optarg);
1489 if (log_level == SYSLOG_LEVEL_NOT_SET)
1490 error("Invalid log level \"%s\"", optarg);
1491 break;
1492 case 'f':
1493 log_facility = log_facility_number(optarg);
Damien Miller35e18db2007-09-17 16:11:33 +10001494 if (log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Millerfef95ad2006-07-10 20:46:55 +10001495 error("Invalid log facility \"%s\"", optarg);
1496 break;
Damien Miller502ab0e2013-01-09 15:57:36 +11001497 case 'd':
1498 cp = tilde_expand_filename(optarg, user_pw->pw_uid);
1499 homedir = percent_expand(cp, "d", user_pw->pw_dir,
1500 "u", user_pw->pw_name, (char *)NULL);
1501 free(cp);
1502 break;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001503 case 'p':
1504 if (request_whitelist != NULL)
1505 fatal("Permitted requests already set");
1506 request_whitelist = xstrdup(optarg);
1507 break;
1508 case 'P':
1509 if (request_blacklist != NULL)
1510 fatal("Refused requests already set");
1511 request_blacklist = xstrdup(optarg);
1512 break;
Darren Tucker7dc48502009-10-07 08:44:42 +11001513 case 'u':
Damien Miller07331212010-11-05 10:20:31 +11001514 errno = 0;
1515 mask = strtol(optarg, &cp, 8);
1516 if (mask < 0 || mask > 0777 || *cp != '\0' ||
1517 cp == optarg || (mask == 0 && errno != 0))
1518 fatal("Invalid umask \"%s\"", optarg);
1519 (void)umask((mode_t)mask);
Darren Tucker7dc48502009-10-07 08:44:42 +11001520 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001521 case 'h':
1522 default:
Damien Millerdfc24252008-02-10 22:29:40 +11001523 sftp_server_usage();
Damien Millerfef95ad2006-07-10 20:46:55 +10001524 }
1525 }
1526
1527 log_init(__progname, log_level, log_facility, log_stderr);
1528
Damien Miller14928b72014-04-01 14:38:07 +11001529#ifdef HAVE_PRCTL
1530 /*
1531 * On Linux, we should try to avoid making /proc/self/{mem,maps}
1532 * available to the user so that sftp access doesn't automatically
1533 * imply arbitrary code execution access that will break
1534 * restricted configurations.
1535 */
1536 if (prctl(PR_SET_DUMPABLE, 0) != 0)
1537 fatal("unable to make the process undumpable");
1538#endif
1539
Damien Millerfef95ad2006-07-10 20:46:55 +10001540 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1541 client_addr = xstrdup(cp);
Damien Millerdfc24252008-02-10 22:29:40 +11001542 if ((cp = strchr(client_addr, ' ')) == NULL) {
1543 error("Malformed SSH_CONNECTION variable: \"%s\"",
Damien Millerfef95ad2006-07-10 20:46:55 +10001544 getenv("SSH_CONNECTION"));
Damien Millerdfc24252008-02-10 22:29:40 +11001545 sftp_server_cleanup_exit(255);
1546 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001547 *cp = '\0';
1548 } else
1549 client_addr = xstrdup("UNKNOWN");
1550
Damien Millerfef95ad2006-07-10 20:46:55 +10001551 logit("session opened for local user %s from [%s]",
1552 pw->pw_name, client_addr);
1553
Darren Tuckeraaf51d22010-01-08 19:04:49 +11001554 in = STDIN_FILENO;
1555 out = STDOUT_FILENO;
Damien Miller7b28dc52000-09-05 13:34:53 +11001556
Damien Miller402b3312001-04-14 00:28:42 +10001557#ifdef HAVE_CYGWIN
1558 setmode(in, O_BINARY);
1559 setmode(out, O_BINARY);
1560#endif
1561
Damien Miller7b28dc52000-09-05 13:34:53 +11001562 max = 0;
1563 if (in > max)
1564 max = in;
1565 if (out > max)
1566 max = out;
1567
1568 buffer_init(&iqueue);
1569 buffer_init(&oqueue);
1570
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001571 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1572 rset = (fd_set *)xmalloc(set_size);
1573 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001574
Damien Miller502ab0e2013-01-09 15:57:36 +11001575 if (homedir != NULL) {
1576 if (chdir(homedir) != 0) {
1577 error("chdir to \"%s\" failed: %s", homedir,
1578 strerror(errno));
1579 }
1580 }
1581
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001582 for (;;) {
1583 memset(rset, 0, set_size);
1584 memset(wset, 0, set_size);
1585
Darren Tuckere9405982007-05-20 15:09:04 +10001586 /*
1587 * Ensure that we can read a full buffer and handle
1588 * the worst-case length packet it can generate,
1589 * otherwise apply backpressure by stopping reads.
1590 */
1591 if (buffer_check_alloc(&iqueue, sizeof(buf)) &&
1592 buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1593 FD_SET(in, rset);
1594
Damien Miller7b28dc52000-09-05 13:34:53 +11001595 olen = buffer_len(&oqueue);
1596 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001597 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001598
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001599 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001600 if (errno == EINTR)
1601 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001602 error("select: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001603 sftp_server_cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001604 }
1605
1606 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001607 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001608 len = read(in, buf, sizeof buf);
1609 if (len == 0) {
1610 debug("read eof");
Damien Millerdfc24252008-02-10 22:29:40 +11001611 sftp_server_cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001612 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001613 error("read: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001614 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001615 } else {
1616 buffer_append(&iqueue, buf, len);
1617 }
1618 }
1619 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001620 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001621 len = write(out, buffer_ptr(&oqueue), olen);
1622 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001623 error("write: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001624 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001625 } else {
1626 buffer_consume(&oqueue, len);
1627 }
1628 }
Darren Tuckere9405982007-05-20 15:09:04 +10001629
1630 /*
1631 * Process requests from client if we can fit the results
1632 * into the output buffer, otherwise stop processing input
1633 * and let the output queue drain.
1634 */
1635 if (buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1636 process();
Damien Miller7b28dc52000-09-05 13:34:53 +11001637 }
1638}