blob: df0fb50680fe32219f3555620bc5a008209e613e [file] [log] [blame]
djm@openbsd.org4d827f02017-04-04 00:24:56 +00001/* $OpenBSD: sftp-server.c,v 1.111 2017/04/04 00:24:56 djm Exp $ */
Damien Miller7b28dc52000-09-05 13:34:53 +11002/*
Darren Tucker37bd3662004-02-24 09:19:15 +11003 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
Damien Miller7b28dc52000-09-05 13:34:53 +11004 *
Darren Tucker37bd3662004-02-24 09:19:15 +11005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Miller7b28dc52000-09-05 13:34:53 +11008 *
Darren Tucker37bd3662004-02-24 09:19:15 +11009 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller7b28dc52000-09-05 13:34:53 +110016 */
Damien Millerd7834352006-08-05 12:39:39 +100017
Damien Miller7b28dc52000-09-05 13:34:53 +110018#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110019
20#include <sys/types.h>
21#include <sys/stat.h>
Damien Miller9aec9192006-08-05 10:57:45 +100022#ifdef HAVE_SYS_TIME_H
23# include <sys/time.h>
24#endif
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100025#ifdef HAVE_SYS_MOUNT_H
Damien Millerd671e5a2008-05-19 14:53:33 +100026#include <sys/mount.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100027#endif
28#ifdef HAVE_SYS_STATVFS_H
Damien Millerd671e5a2008-05-19 14:53:33 +100029#include <sys/statvfs.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100030#endif
Damien Miller88f254b2006-03-15 11:25:13 +110031
32#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100033#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100034#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100035#include <pwd.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100036#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100037#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100038#include <string.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100039#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100041#include <stdarg.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110042
Damien Miller7b28dc52000-09-05 13:34:53 +110043#include "xmalloc.h"
djm@openbsd.org7d845f42015-01-14 13:54:13 +000044#include "sshbuf.h"
45#include "ssherr.h"
Damien Millerd7834352006-08-05 12:39:39 +100046#include "log.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100047#include "misc.h"
Damien Miller6eaeebf2013-10-15 11:55:57 +110048#include "match.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100049#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110050
Ben Lindstrom2f959b42001-01-11 06:20:23 +000051#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110052#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110053
Damien Millerfef95ad2006-07-10 20:46:55 +100054/* Our verbosity */
Damien Miller6eaeebf2013-10-15 11:55:57 +110055static LogLevel log_level = SYSLOG_LEVEL_ERROR;
Damien Millerfef95ad2006-07-10 20:46:55 +100056
57/* Our client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110058static struct passwd *pw = NULL;
59static char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000060
Damien Miller7b28dc52000-09-05 13:34:53 +110061/* input and output queue */
djm@openbsd.org7d845f42015-01-14 13:54:13 +000062struct sshbuf *iqueue;
63struct sshbuf *oqueue;
Damien Miller7b28dc52000-09-05 13:34:53 +110064
Damien Miller058316f2001-03-08 10:08:49 +110065/* Version of client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110066static u_int version;
67
68/* SSH2_FXP_INIT received */
69static int init_done;
Damien Miller058316f2001-03-08 10:08:49 +110070
Darren Tuckerdb7bf822010-01-09 22:24:33 +110071/* Disable writes */
Damien Miller6eaeebf2013-10-15 11:55:57 +110072static int readonly;
73
74/* Requests that are allowed/denied */
75static char *request_whitelist, *request_blacklist;
Darren Tuckerdb7bf822010-01-09 22:24:33 +110076
Darren Tuckera6612d42003-06-28 12:39:03 +100077/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110078typedef struct Stat Stat;
79
Damien Miller33804262001-02-04 23:20:18 +110080struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110081 char *name;
82 char *long_name;
83 Attrib attrib;
84};
85
Damien Miller6eaeebf2013-10-15 11:55:57 +110086/* Packet handlers */
87static void process_open(u_int32_t id);
88static void process_close(u_int32_t id);
89static void process_read(u_int32_t id);
90static void process_write(u_int32_t id);
91static void process_stat(u_int32_t id);
92static void process_lstat(u_int32_t id);
93static void process_fstat(u_int32_t id);
94static void process_setstat(u_int32_t id);
95static void process_fsetstat(u_int32_t id);
96static void process_opendir(u_int32_t id);
97static void process_readdir(u_int32_t id);
98static void process_remove(u_int32_t id);
99static void process_mkdir(u_int32_t id);
100static void process_rmdir(u_int32_t id);
101static void process_realpath(u_int32_t id);
102static void process_rename(u_int32_t id);
103static void process_readlink(u_int32_t id);
104static void process_symlink(u_int32_t id);
105static void process_extended_posix_rename(u_int32_t id);
106static void process_extended_statvfs(u_int32_t id);
107static void process_extended_fstatvfs(u_int32_t id);
108static void process_extended_hardlink(u_int32_t id);
Damien Millerf29238e2013-10-17 11:48:52 +1100109static void process_extended_fsync(u_int32_t id);
Damien Miller6eaeebf2013-10-15 11:55:57 +1100110static void process_extended(u_int32_t id);
111
112struct sftp_handler {
113 const char *name; /* user-visible name for fine-grained perms */
114 const char *ext_name; /* extended request name */
115 u_int type; /* packet type, for non extended packets */
116 void (*handler)(u_int32_t);
117 int does_write; /* if nonzero, banned for readonly mode */
118};
119
120struct sftp_handler handlers[] = {
121 /* NB. SSH2_FXP_OPEN does the readonly check in the handler itself */
122 { "open", NULL, SSH2_FXP_OPEN, process_open, 0 },
123 { "close", NULL, SSH2_FXP_CLOSE, process_close, 0 },
124 { "read", NULL, SSH2_FXP_READ, process_read, 0 },
125 { "write", NULL, SSH2_FXP_WRITE, process_write, 1 },
126 { "lstat", NULL, SSH2_FXP_LSTAT, process_lstat, 0 },
127 { "fstat", NULL, SSH2_FXP_FSTAT, process_fstat, 0 },
128 { "setstat", NULL, SSH2_FXP_SETSTAT, process_setstat, 1 },
129 { "fsetstat", NULL, SSH2_FXP_FSETSTAT, process_fsetstat, 1 },
130 { "opendir", NULL, SSH2_FXP_OPENDIR, process_opendir, 0 },
131 { "readdir", NULL, SSH2_FXP_READDIR, process_readdir, 0 },
132 { "remove", NULL, SSH2_FXP_REMOVE, process_remove, 1 },
133 { "mkdir", NULL, SSH2_FXP_MKDIR, process_mkdir, 1 },
134 { "rmdir", NULL, SSH2_FXP_RMDIR, process_rmdir, 1 },
135 { "realpath", NULL, SSH2_FXP_REALPATH, process_realpath, 0 },
136 { "stat", NULL, SSH2_FXP_STAT, process_stat, 0 },
137 { "rename", NULL, SSH2_FXP_RENAME, process_rename, 1 },
138 { "readlink", NULL, SSH2_FXP_READLINK, process_readlink, 0 },
139 { "symlink", NULL, SSH2_FXP_SYMLINK, process_symlink, 1 },
140 { NULL, NULL, 0, NULL, 0 }
141};
142
143/* SSH2_FXP_EXTENDED submessages */
144struct sftp_handler extended_handlers[] = {
145 { "posix-rename", "posix-rename@openssh.com", 0,
146 process_extended_posix_rename, 1 },
147 { "statvfs", "statvfs@openssh.com", 0, process_extended_statvfs, 0 },
148 { "fstatvfs", "fstatvfs@openssh.com", 0, process_extended_fstatvfs, 0 },
149 { "hardlink", "hardlink@openssh.com", 0, process_extended_hardlink, 1 },
Damien Millerf29238e2013-10-17 11:48:52 +1100150 { "fsync", "fsync@openssh.com", 0, process_extended_fsync, 1 },
Damien Miller6eaeebf2013-10-15 11:55:57 +1100151 { NULL, NULL, 0, NULL, 0 }
152};
153
154static int
155request_permitted(struct sftp_handler *h)
156{
157 char *result;
158
159 if (readonly && h->does_write) {
160 verbose("Refusing %s request in read-only mode", h->name);
161 return 0;
162 }
163 if (request_blacklist != NULL &&
164 ((result = match_list(h->name, request_blacklist, NULL))) != NULL) {
165 free(result);
166 verbose("Refusing blacklisted %s request", h->name);
167 return 0;
168 }
169 if (request_whitelist != NULL &&
170 ((result = match_list(h->name, request_whitelist, NULL))) != NULL) {
171 free(result);
172 debug2("Permitting whitelisted %s request", h->name);
173 return 1;
174 }
175 if (request_whitelist != NULL) {
176 verbose("Refusing non-whitelisted %s request", h->name);
177 return 0;
178 }
179 return 1;
180}
181
Ben Lindstrombba81212001-06-25 05:01:22 +0000182static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100183errno_to_portable(int unixerrno)
184{
185 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000186
Damien Miller7b28dc52000-09-05 13:34:53 +1100187 switch (unixerrno) {
188 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000189 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100190 break;
191 case ENOENT:
192 case ENOTDIR:
193 case EBADF:
194 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000195 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100196 break;
197 case EPERM:
198 case EACCES:
199 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000200 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +1100201 break;
202 case ENAMETOOLONG:
203 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000204 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100205 break;
Darren Tucker422c34c2008-06-09 22:48:31 +1000206 case ENOSYS:
207 ret = SSH2_FX_OP_UNSUPPORTED;
208 break;
Damien Miller7b28dc52000-09-05 13:34:53 +1100209 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000210 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100211 break;
212 }
213 return ret;
214}
215
Ben Lindstrombba81212001-06-25 05:01:22 +0000216static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100217flags_from_portable(int pflags)
218{
219 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000220
Ben Lindstrom36592512001-03-05 05:02:08 +0000221 if ((pflags & SSH2_FXF_READ) &&
222 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100223 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000224 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100225 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000226 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100227 flags = O_WRONLY;
228 }
Damien Millere9fc72e2013-10-15 12:14:12 +1100229 if (pflags & SSH2_FXF_APPEND)
230 flags |= O_APPEND;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000231 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100232 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000233 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100234 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000235 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100236 flags |= O_EXCL;
237 return flags;
238}
239
Damien Millerfef95ad2006-07-10 20:46:55 +1000240static const char *
241string_from_portable(int pflags)
242{
243 static char ret[128];
244
245 *ret = '\0';
246
247#define PAPPEND(str) { \
248 if (*ret != '\0') \
249 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000250 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000251 }
252
253 if (pflags & SSH2_FXF_READ)
254 PAPPEND("READ")
255 if (pflags & SSH2_FXF_WRITE)
256 PAPPEND("WRITE")
Damien Millere9fc72e2013-10-15 12:14:12 +1100257 if (pflags & SSH2_FXF_APPEND)
258 PAPPEND("APPEND")
Damien Millerfef95ad2006-07-10 20:46:55 +1000259 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
Damien Miller7b28dc52000-09-05 13:34:53 +1100269/* handle handles */
270
271typedef struct Handle Handle;
272struct Handle {
273 int use;
274 DIR *dirp;
275 int fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100276 int flags;
Damien Miller7b28dc52000-09-05 13:34:53 +1100277 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000278 u_int64_t bytes_read, bytes_write;
Damien Miller3397d0e2008-02-10 22:26:51 +1100279 int next_unused;
Damien Miller7b28dc52000-09-05 13:34:53 +1100280};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000281
Damien Miller7b28dc52000-09-05 13:34:53 +1100282enum {
283 HANDLE_UNUSED,
284 HANDLE_DIR,
285 HANDLE_FILE
286};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000287
Damien Miller3397d0e2008-02-10 22:26:51 +1100288Handle *handles = NULL;
289u_int num_handles = 0;
290int first_unused_handle = -1;
Damien Miller7b28dc52000-09-05 13:34:53 +1100291
Damien Miller3397d0e2008-02-10 22:26:51 +1100292static void handle_unused(int i)
Damien Miller7b28dc52000-09-05 13:34:53 +1100293{
Damien Miller3397d0e2008-02-10 22:26:51 +1100294 handles[i].use = HANDLE_UNUSED;
295 handles[i].next_unused = first_unused_handle;
296 first_unused_handle = i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100297}
298
Ben Lindstrombba81212001-06-25 05:01:22 +0000299static int
Damien Millere9fc72e2013-10-15 12:14:12 +1100300handle_new(int use, const char *name, int fd, int flags, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100301{
Damien Miller3397d0e2008-02-10 22:26:51 +1100302 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000303
Damien Miller3397d0e2008-02-10 22:26:51 +1100304 if (first_unused_handle == -1) {
305 if (num_handles + 1 <= num_handles)
306 return -1;
307 num_handles++;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000308 handles = xreallocarray(handles, num_handles, sizeof(Handle));
Damien Miller3397d0e2008-02-10 22:26:51 +1100309 handle_unused(num_handles - 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100310 }
Damien Miller3397d0e2008-02-10 22:26:51 +1100311
312 i = first_unused_handle;
313 first_unused_handle = handles[i].next_unused;
314
315 handles[i].use = use;
316 handles[i].dirp = dirp;
317 handles[i].fd = fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100318 handles[i].flags = flags;
Damien Miller3397d0e2008-02-10 22:26:51 +1100319 handles[i].name = xstrdup(name);
320 handles[i].bytes_read = handles[i].bytes_write = 0;
321
322 return i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100323}
324
Ben Lindstrombba81212001-06-25 05:01:22 +0000325static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100326handle_is_ok(int i, int type)
327{
Damien Miller3397d0e2008-02-10 22:26:51 +1100328 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100329}
330
Ben Lindstrombba81212001-06-25 05:01:22 +0000331static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000332handle_to_string(int handle, u_char **stringp, int *hlenp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100333{
Damien Miller7b28dc52000-09-05 13:34:53 +1100334 if (stringp == NULL || hlenp == NULL)
335 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000336 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100337 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000338 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100339 return 0;
340}
341
Ben Lindstrombba81212001-06-25 05:01:22 +0000342static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000343handle_from_string(const u_char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100344{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000345 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000346
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000347 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100348 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100349 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100350 if (handle_is_ok(val, HANDLE_FILE) ||
351 handle_is_ok(val, HANDLE_DIR))
352 return val;
353 return -1;
354}
355
Ben Lindstrombba81212001-06-25 05:01:22 +0000356static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100357handle_to_name(int handle)
358{
359 if (handle_is_ok(handle, HANDLE_DIR)||
360 handle_is_ok(handle, HANDLE_FILE))
361 return handles[handle].name;
362 return NULL;
363}
364
Ben Lindstrombba81212001-06-25 05:01:22 +0000365static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100366handle_to_dir(int handle)
367{
368 if (handle_is_ok(handle, HANDLE_DIR))
369 return handles[handle].dirp;
370 return NULL;
371}
372
Ben Lindstrombba81212001-06-25 05:01:22 +0000373static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100374handle_to_fd(int handle)
375{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000376 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100377 return handles[handle].fd;
378 return -1;
379}
380
Damien Millere9fc72e2013-10-15 12:14:12 +1100381static int
382handle_to_flags(int handle)
383{
384 if (handle_is_ok(handle, HANDLE_FILE))
385 return handles[handle].flags;
386 return 0;
387}
388
Damien Millerfef95ad2006-07-10 20:46:55 +1000389static void
390handle_update_read(int handle, ssize_t bytes)
391{
392 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
393 handles[handle].bytes_read += bytes;
394}
395
396static void
397handle_update_write(int handle, ssize_t bytes)
398{
399 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
400 handles[handle].bytes_write += bytes;
401}
402
403static u_int64_t
404handle_bytes_read(int handle)
405{
406 if (handle_is_ok(handle, HANDLE_FILE))
407 return (handles[handle].bytes_read);
408 return 0;
409}
410
411static u_int64_t
412handle_bytes_write(int handle)
413{
414 if (handle_is_ok(handle, HANDLE_FILE))
415 return (handles[handle].bytes_write);
416 return 0;
417}
418
Ben Lindstrombba81212001-06-25 05:01:22 +0000419static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100420handle_close(int handle)
421{
422 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000423
Damien Miller7b28dc52000-09-05 13:34:53 +1100424 if (handle_is_ok(handle, HANDLE_FILE)) {
425 ret = close(handles[handle].fd);
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 if (handle_is_ok(handle, HANDLE_DIR)) {
429 ret = closedir(handles[handle].dirp);
Darren Tuckera627d422013-06-02 07:31:17 +1000430 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100431 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100432 } else {
433 errno = ENOENT;
434 }
435 return ret;
436}
437
Damien Millerfef95ad2006-07-10 20:46:55 +1000438static void
439handle_log_close(int handle, char *emsg)
440{
441 if (handle_is_ok(handle, HANDLE_FILE)) {
442 logit("%s%sclose \"%s\" bytes read %llu written %llu",
443 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
444 handle_to_name(handle),
Darren Tucker86473c52007-05-20 14:59:32 +1000445 (unsigned long long)handle_bytes_read(handle),
446 (unsigned long long)handle_bytes_write(handle));
Damien Millerfef95ad2006-07-10 20:46:55 +1000447 } else {
448 logit("%s%sclosedir \"%s\"",
449 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
450 handle_to_name(handle));
451 }
452}
453
454static void
455handle_log_exit(void)
456{
457 u_int i;
458
Damien Miller3397d0e2008-02-10 22:26:51 +1100459 for (i = 0; i < num_handles; i++)
Damien Millerfef95ad2006-07-10 20:46:55 +1000460 if (handles[i].use != HANDLE_UNUSED)
461 handle_log_close(i, "forced");
462}
463
Ben Lindstrombba81212001-06-25 05:01:22 +0000464static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000465get_handle(struct sshbuf *queue, int *hp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100466{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000467 u_char *handle;
468 int r;
469 size_t hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000470
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000471 *hp = -1;
472 if ((r = sshbuf_get_string(queue, &handle, &hlen)) != 0)
473 return r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000474 if (hlen < 256)
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000475 *hp = handle_from_string(handle, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000476 free(handle);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000477 return 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100478}
479
480/* send replies */
481
Ben Lindstrombba81212001-06-25 05:01:22 +0000482static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000483send_msg(struct sshbuf *m)
Damien Miller7b28dc52000-09-05 13:34:53 +1100484{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000485 int r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000486
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000487 if ((r = sshbuf_put_stringb(oqueue, m)) != 0)
488 fatal("%s: buffer error: %s", __func__, ssh_err(r));
489 sshbuf_reset(m);
Damien Miller7b28dc52000-09-05 13:34:53 +1100490}
491
Damien Millerfef95ad2006-07-10 20:46:55 +1000492static const char *
493status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100494{
Damien Miller058316f2001-03-08 10:08:49 +1100495 const char *status_messages[] = {
496 "Success", /* SSH_FX_OK */
497 "End of file", /* SSH_FX_EOF */
498 "No such file", /* SSH_FX_NO_SUCH_FILE */
499 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
500 "Failure", /* SSH_FX_FAILURE */
501 "Bad message", /* SSH_FX_BAD_MESSAGE */
502 "No connection", /* SSH_FX_NO_CONNECTION */
503 "Connection lost", /* SSH_FX_CONNECTION_LOST */
504 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
505 "Unknown error" /* Others */
506 };
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000507 return (status_messages[MINIMUM(status,SSH2_FX_MAX)]);
Damien Millerfef95ad2006-07-10 20:46:55 +1000508}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000509
Damien Millerfef95ad2006-07-10 20:46:55 +1000510static void
511send_status(u_int32_t id, u_int32_t status)
512{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000513 struct sshbuf *msg;
514 int r;
Damien Millerfef95ad2006-07-10 20:46:55 +1000515
516 debug3("request %u: sent status %u", id, status);
517 if (log_level > SYSLOG_LEVEL_VERBOSE ||
518 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
519 logit("sent status %s", status_to_message(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000520 if ((msg = sshbuf_new()) == NULL)
521 fatal("%s: sshbuf_new failed", __func__);
522 if ((r = sshbuf_put_u8(msg, SSH2_FXP_STATUS)) != 0 ||
523 (r = sshbuf_put_u32(msg, id)) != 0 ||
524 (r = sshbuf_put_u32(msg, status)) != 0)
525 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +1100526 if (version >= 3) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000527 if ((r = sshbuf_put_cstring(msg,
528 status_to_message(status))) != 0 ||
529 (r = sshbuf_put_cstring(msg, "")) != 0)
530 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +1100531 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000532 send_msg(msg);
533 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100534}
Ben Lindstrombba81212001-06-25 05:01:22 +0000535static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000536send_data_or_handle(char type, u_int32_t id, const u_char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100537{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000538 struct sshbuf *msg;
539 int r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000540
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000541 if ((msg = sshbuf_new()) == NULL)
542 fatal("%s: sshbuf_new failed", __func__);
543 if ((r = sshbuf_put_u8(msg, type)) != 0 ||
544 (r = sshbuf_put_u32(msg, id)) != 0 ||
545 (r = sshbuf_put_string(msg, data, dlen)) != 0)
546 fatal("%s: buffer error: %s", __func__, ssh_err(r));
547 send_msg(msg);
548 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100549}
550
Ben Lindstrombba81212001-06-25 05:01:22 +0000551static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000552send_data(u_int32_t id, const u_char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100553{
Damien Millerfef95ad2006-07-10 20:46:55 +1000554 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000555 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100556}
557
Ben Lindstrombba81212001-06-25 05:01:22 +0000558static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100559send_handle(u_int32_t id, int handle)
560{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000561 u_char *string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100562 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000563
Damien Miller7b28dc52000-09-05 13:34:53 +1100564 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000565 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000566 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000567 free(string);
Damien Miller7b28dc52000-09-05 13:34:53 +1100568}
569
Ben Lindstrombba81212001-06-25 05:01:22 +0000570static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100571send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100572{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000573 struct sshbuf *msg;
574 int i, r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000575
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000576 if ((msg = sshbuf_new()) == NULL)
577 fatal("%s: sshbuf_new failed", __func__);
578 if ((r = sshbuf_put_u8(msg, SSH2_FXP_NAME)) != 0 ||
579 (r = sshbuf_put_u32(msg, id)) != 0 ||
580 (r = sshbuf_put_u32(msg, count)) != 0)
581 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerfef95ad2006-07-10 20:46:55 +1000582 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100583 for (i = 0; i < count; i++) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000584 if ((r = sshbuf_put_cstring(msg, stats[i].name)) != 0 ||
585 (r = sshbuf_put_cstring(msg, stats[i].long_name)) != 0 ||
586 (r = encode_attrib(msg, &stats[i].attrib)) != 0)
587 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100588 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000589 send_msg(msg);
590 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100591}
592
Ben Lindstrombba81212001-06-25 05:01:22 +0000593static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100594send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100595{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000596 struct sshbuf *msg;
597 int r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000598
Damien Millerfef95ad2006-07-10 20:46:55 +1000599 debug("request %u: sent attrib have 0x%x", id, a->flags);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000600 if ((msg = sshbuf_new()) == NULL)
601 fatal("%s: sshbuf_new failed", __func__);
602 if ((r = sshbuf_put_u8(msg, SSH2_FXP_ATTRS)) != 0 ||
603 (r = sshbuf_put_u32(msg, id)) != 0 ||
604 (r = encode_attrib(msg, a)) != 0)
605 fatal("%s: buffer error: %s", __func__, ssh_err(r));
606 send_msg(msg);
607 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100608}
609
Damien Millerd671e5a2008-05-19 14:53:33 +1000610static void
611send_statvfs(u_int32_t id, struct statvfs *st)
612{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000613 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +1000614 u_int64_t flag;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000615 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +1000616
617 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
618 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
619
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000620 if ((msg = sshbuf_new()) == NULL)
621 fatal("%s: sshbuf_new failed", __func__);
622 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED_REPLY)) != 0 ||
623 (r = sshbuf_put_u32(msg, id)) != 0 ||
624 (r = sshbuf_put_u64(msg, st->f_bsize)) != 0 ||
625 (r = sshbuf_put_u64(msg, st->f_frsize)) != 0 ||
626 (r = sshbuf_put_u64(msg, st->f_blocks)) != 0 ||
627 (r = sshbuf_put_u64(msg, st->f_bfree)) != 0 ||
628 (r = sshbuf_put_u64(msg, st->f_bavail)) != 0 ||
629 (r = sshbuf_put_u64(msg, st->f_files)) != 0 ||
630 (r = sshbuf_put_u64(msg, st->f_ffree)) != 0 ||
631 (r = sshbuf_put_u64(msg, st->f_favail)) != 0 ||
632 (r = sshbuf_put_u64(msg, FSID_TO_ULONG(st->f_fsid))) != 0 ||
633 (r = sshbuf_put_u64(msg, flag)) != 0 ||
634 (r = sshbuf_put_u64(msg, st->f_namemax)) != 0)
635 fatal("%s: buffer error: %s", __func__, ssh_err(r));
636 send_msg(msg);
637 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000638}
639
Damien Miller7b28dc52000-09-05 13:34:53 +1100640/* parse incoming */
641
Ben Lindstrombba81212001-06-25 05:01:22 +0000642static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100643process_init(void)
644{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000645 struct sshbuf *msg;
646 int r;
Damien Miller7b28dc52000-09-05 13:34:53 +1100647
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000648 if ((r = sshbuf_get_u32(iqueue, &version)) != 0)
649 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf145a5b2011-06-20 14:42:51 +1000650 verbose("received client version %u", version);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000651 if ((msg = sshbuf_new()) == NULL)
652 fatal("%s: sshbuf_new failed", __func__);
653 if ((r = sshbuf_put_u8(msg, SSH2_FXP_VERSION)) != 0 ||
654 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0 ||
655 /* POSIX rename extension */
656 (r = sshbuf_put_cstring(msg, "posix-rename@openssh.com")) != 0 ||
657 (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
658 /* statvfs extension */
659 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
660 (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
661 /* fstatvfs extension */
662 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
663 (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
664 /* hardlink extension */
665 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
666 (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
667 /* fsync extension */
668 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
669 (r = sshbuf_put_cstring(msg, "1")) != 0) /* version */
670 fatal("%s: buffer error: %s", __func__, ssh_err(r));
671 send_msg(msg);
672 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100673}
674
Ben Lindstrombba81212001-06-25 05:01:22 +0000675static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100676process_open(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100677{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100678 u_int32_t pflags;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000679 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100680 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000681 int r, handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100682
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000683 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
684 (r = sshbuf_get_u32(iqueue, &pflags)) != 0 || /* portable flags */
685 (r = decode_attrib(iqueue, &a)) != 0)
686 fatal("%s: buffer error: %s", __func__, ssh_err(r));
687
Damien Miller6444fe92006-07-10 21:31:27 +1000688 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100689 flags = flags_from_portable(pflags);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000690 mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a.perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000691 logit("open \"%s\" flags %s mode 0%o",
692 name, string_from_portable(pflags), mode);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100693 if (readonly &&
djm@openbsd.org4d827f02017-04-04 00:24:56 +0000694 ((flags & O_ACCMODE) != O_RDONLY ||
695 (flags & (O_CREAT|O_TRUNC)) != 0)) {
Damien Miller6eaeebf2013-10-15 11:55:57 +1100696 verbose("Refusing open request in read-only mode");
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000697 status = SSH2_FX_PERMISSION_DENIED;
Damien Miller6eaeebf2013-10-15 11:55:57 +1100698 } else {
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100699 fd = open(name, flags, mode);
700 if (fd < 0) {
701 status = errno_to_portable(errno);
Damien Miller7b28dc52000-09-05 13:34:53 +1100702 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100703 handle = handle_new(HANDLE_FILE, name, fd, flags, NULL);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100704 if (handle < 0) {
705 close(fd);
706 } else {
707 send_handle(id, handle);
708 status = SSH2_FX_OK;
709 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100710 }
711 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000712 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100713 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000714 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100715}
716
Ben Lindstrombba81212001-06-25 05:01:22 +0000717static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100718process_close(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100719{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000720 int r, handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100721
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000722 if ((r = get_handle(iqueue, &handle)) != 0)
723 fatal("%s: buffer error: %s", __func__, ssh_err(r));
724
Damien Millerfef95ad2006-07-10 20:46:55 +1000725 debug3("request %u: close handle %u", id, handle);
726 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100727 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000728 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100729 send_status(id, status);
730}
731
Ben Lindstrombba81212001-06-25 05:01:22 +0000732static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100733process_read(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100734{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000735 u_char buf[64*1024];
Damien Miller6eaeebf2013-10-15 11:55:57 +1100736 u_int32_t len;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000737 int r, handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100738 u_int64_t off;
739
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000740 if ((r = get_handle(iqueue, &handle)) != 0 ||
741 (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
742 (r = sshbuf_get_u32(iqueue, &len)) != 0)
743 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100744
Damien Millerfef95ad2006-07-10 20:46:55 +1000745 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
746 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100747 if (len > sizeof buf) {
748 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000749 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100750 }
751 fd = handle_to_fd(handle);
752 if (fd >= 0) {
753 if (lseek(fd, off, SEEK_SET) < 0) {
754 error("process_read: seek failed");
755 status = errno_to_portable(errno);
756 } else {
757 ret = read(fd, buf, len);
758 if (ret < 0) {
759 status = errno_to_portable(errno);
760 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000761 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100762 } else {
763 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000764 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000765 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100766 }
767 }
768 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000769 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100770 send_status(id, status);
771}
772
Ben Lindstrombba81212001-06-25 05:01:22 +0000773static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100774process_write(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100775{
Damien Miller7b28dc52000-09-05 13:34:53 +1100776 u_int64_t off;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000777 size_t len;
778 int r, handle, fd, ret, status;
779 u_char *data;
Damien Miller7b28dc52000-09-05 13:34:53 +1100780
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000781 if ((r = get_handle(iqueue, &handle)) != 0 ||
782 (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
783 (r = sshbuf_get_string(iqueue, &data, &len)) != 0)
784 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100785
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000786 debug("request %u: write \"%s\" (handle %d) off %llu len %zu",
Damien Millerfef95ad2006-07-10 20:46:55 +1000787 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100788 fd = handle_to_fd(handle);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000789
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100790 if (fd < 0)
791 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100792 else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100793 if (!(handle_to_flags(handle) & O_APPEND) &&
794 lseek(fd, off, SEEK_SET) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100795 status = errno_to_portable(errno);
796 error("process_write: seek failed");
797 } else {
798/* XXX ATOMICIO ? */
799 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000800 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100801 error("process_write: write failed");
802 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000803 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000804 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000805 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100806 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000807 debug2("nothing at all written");
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100808 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100809 }
810 }
811 }
812 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000813 free(data);
Damien Miller7b28dc52000-09-05 13:34:53 +1100814}
815
Ben Lindstrombba81212001-06-25 05:01:22 +0000816static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100817process_do_stat(u_int32_t id, int do_lstat)
Damien Miller7b28dc52000-09-05 13:34:53 +1100818{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000819 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100820 struct stat st;
Damien Miller7b28dc52000-09-05 13:34:53 +1100821 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000822 int r, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100823
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000824 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
825 fatal("%s: buffer error: %s", __func__, ssh_err(r));
826
Damien Millerfef95ad2006-07-10 20:46:55 +1000827 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
828 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000829 r = do_lstat ? lstat(name, &st) : stat(name, &st);
830 if (r < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100831 status = errno_to_portable(errno);
832 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000833 stat_to_attrib(&st, &a);
834 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000835 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100836 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000837 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100838 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000839 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100840}
841
Ben Lindstrombba81212001-06-25 05:01:22 +0000842static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100843process_stat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100844{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100845 process_do_stat(id, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100846}
847
Ben Lindstrombba81212001-06-25 05:01:22 +0000848static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100849process_lstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100850{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100851 process_do_stat(id, 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100852}
853
Ben Lindstrombba81212001-06-25 05:01:22 +0000854static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100855process_fstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100856{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000857 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100858 struct stat st;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000859 int fd, r, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100860
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000861 if ((r = get_handle(iqueue, &handle)) != 0)
862 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerfef95ad2006-07-10 20:46:55 +1000863 debug("request %u: fstat \"%s\" (handle %u)",
864 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100865 fd = handle_to_fd(handle);
Damien Millere2334d62007-01-05 16:31:02 +1100866 if (fd >= 0) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000867 r = fstat(fd, &st);
868 if (r < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100869 status = errno_to_portable(errno);
870 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000871 stat_to_attrib(&st, &a);
872 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000873 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100874 }
875 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000876 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100877 send_status(id, status);
878}
879
Ben Lindstrombba81212001-06-25 05:01:22 +0000880static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100881attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100882{
883 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000884
Damien Miller7b28dc52000-09-05 13:34:53 +1100885 tv[0].tv_sec = a->atime;
886 tv[0].tv_usec = 0;
887 tv[1].tv_sec = a->mtime;
888 tv[1].tv_usec = 0;
889 return tv;
890}
891
Ben Lindstrombba81212001-06-25 05:01:22 +0000892static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100893process_setstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100894{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000895 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100896 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000897 int r, status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100898
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000899 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
900 (r = decode_attrib(iqueue, &a)) != 0)
901 fatal("%s: buffer error: %s", __func__, ssh_err(r));
902
Damien Millerfef95ad2006-07-10 20:46:55 +1000903 debug("request %u: setstat name \"%s\"", id, name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000904 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000905 logit("set \"%s\" size %llu",
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000906 name, (unsigned long long)a.size);
907 r = truncate(name, a.size);
908 if (r == -1)
Damien Miller00c92172002-02-13 14:05:00 +1100909 status = errno_to_portable(errno);
910 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000911 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
912 logit("set \"%s\" mode %04o", name, a.perm);
913 r = chmod(name, a.perm & 07777);
914 if (r == -1)
Damien Miller7b28dc52000-09-05 13:34:53 +1100915 status = errno_to_portable(errno);
916 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000917 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000918 char buf[64];
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000919 time_t t = a.mtime;
Damien Millerfef95ad2006-07-10 20:46:55 +1000920
921 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
922 localtime(&t));
923 logit("set \"%s\" modtime %s", name, buf);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000924 r = utimes(name, attrib_to_tv(&a));
925 if (r == -1)
Damien Miller7b28dc52000-09-05 13:34:53 +1100926 status = errno_to_portable(errno);
927 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000928 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000929 logit("set \"%s\" owner %lu group %lu", name,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000930 (u_long)a.uid, (u_long)a.gid);
931 r = chown(name, a.uid, a.gid);
932 if (r == -1)
Kevin Steves8e743932001-02-05 13:24:35 +0000933 status = errno_to_portable(errno);
934 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100935 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000936 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100937}
938
Ben Lindstrombba81212001-06-25 05:01:22 +0000939static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100940process_fsetstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100941{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000942 Attrib a;
943 int handle, fd, r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000944 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000945
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000946 if ((r = get_handle(iqueue, &handle)) != 0 ||
947 (r = decode_attrib(iqueue, &a)) != 0)
948 fatal("%s: buffer error: %s", __func__, ssh_err(r));
949
950 debug("request %u: fsetstat handle %d", id, handle);
951 fd = handle_to_fd(handle);
952 if (fd < 0)
953 status = SSH2_FX_FAILURE;
954 else {
955 char *name = handle_to_name(handle);
956
957 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
958 logit("set \"%s\" size %llu",
959 name, (unsigned long long)a.size);
960 r = ftruncate(fd, a.size);
961 if (r == -1)
962 status = errno_to_portable(errno);
963 }
964 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
965 logit("set \"%s\" mode %04o", name, a.perm);
966#ifdef HAVE_FCHMOD
Damien Miller83b96782015-01-15 02:35:50 +1100967 r = fchmod(fd, a.perm & 07777);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000968#else
Damien Miller83b96782015-01-15 02:35:50 +1100969 r = chmod(name, a.perm & 07777);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000970#endif
971 if (r == -1)
972 status = errno_to_portable(errno);
973 }
974 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
975 char buf[64];
976 time_t t = a.mtime;
977
978 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
979 localtime(&t));
980 logit("set \"%s\" modtime %s", name, buf);
981#ifdef HAVE_FUTIMES
Damien Miller83b96782015-01-15 02:35:50 +1100982 r = futimes(fd, attrib_to_tv(&a));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000983#else
Damien Miller83b96782015-01-15 02:35:50 +1100984 r = utimes(name, attrib_to_tv(&a));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000985#endif
986 if (r == -1)
987 status = errno_to_portable(errno);
988 }
989 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
990 logit("set \"%s\" owner %lu group %lu", name,
991 (u_long)a.uid, (u_long)a.gid);
992#ifdef HAVE_FCHOWN
Damien Miller83b96782015-01-15 02:35:50 +1100993 r = fchown(fd, a.uid, a.gid);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000994#else
Damien Miller83b96782015-01-15 02:35:50 +1100995 r = chown(name, a.uid, a.gid);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000996#endif
997 if (r == -1)
998 status = errno_to_portable(errno);
999 }
1000 }
1001 send_status(id, status);
1002}
Damien Miller7b28dc52000-09-05 13:34:53 +11001003
Ben Lindstrombba81212001-06-25 05:01:22 +00001004static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001005process_opendir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001006{
1007 DIR *dirp = NULL;
1008 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001009 int r, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001010
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001011 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1012 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1013
Damien Millerfef95ad2006-07-10 20:46:55 +10001014 debug3("request %u: opendir", id);
1015 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001016 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001017 if (dirp == NULL) {
1018 status = errno_to_portable(errno);
1019 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +11001020 handle = handle_new(HANDLE_DIR, path, 0, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +11001021 if (handle < 0) {
1022 closedir(dirp);
1023 } else {
1024 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001025 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001026 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001027
Damien Miller7b28dc52000-09-05 13:34:53 +11001028 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001029 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +11001030 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001031 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001032}
1033
Ben Lindstrombba81212001-06-25 05:01:22 +00001034static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001035process_readdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001036{
1037 DIR *dirp;
1038 struct dirent *dp;
1039 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001040 int r, handle;
Damien Miller7b28dc52000-09-05 13:34:53 +11001041
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001042 if ((r = get_handle(iqueue, &handle)) != 0)
1043 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1044
Damien Millerfef95ad2006-07-10 20:46:55 +10001045 debug("request %u: readdir \"%s\" (handle %d)", id,
1046 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +11001047 dirp = handle_to_dir(handle);
1048 path = handle_to_name(handle);
1049 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001050 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +11001051 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +11001052 struct stat st;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001053 char pathname[PATH_MAX];
Damien Miller7b28dc52000-09-05 13:34:53 +11001054 Stat *stats;
1055 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001056
Damien Miller07d86be2006-03-26 14:19:21 +11001057 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001058 while ((dp = readdir(dirp)) != NULL) {
1059 if (count >= nstats) {
1060 nstats *= 2;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001061 stats = xreallocarray(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001062 }
1063/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +00001064 snprintf(pathname, sizeof pathname, "%s%s%s", path,
1065 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001066 if (lstat(pathname, &st) < 0)
1067 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001068 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +11001069 stats[count].name = xstrdup(dp->d_name);
Darren Tucker2901e2d2010-01-13 22:44:06 +11001070 stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001071 count++;
1072 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001073 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +11001074 if (count == 100)
1075 break;
1076 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001077 if (count > 0) {
1078 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001079 for (i = 0; i < count; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001080 free(stats[i].name);
1081 free(stats[i].long_name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001082 }
1083 } else {
1084 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +11001085 }
Darren Tuckera627d422013-06-02 07:31:17 +10001086 free(stats);
Damien Miller7b28dc52000-09-05 13:34:53 +11001087 }
1088}
1089
Ben Lindstrombba81212001-06-25 05:01:22 +00001090static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001091process_remove(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001092{
1093 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001094 int r, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001095
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001096 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1097 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1098
Damien Millerfef95ad2006-07-10 20:46:55 +10001099 debug3("request %u: remove", id);
1100 logit("remove name \"%s\"", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001101 r = unlink(name);
1102 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001103 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001104 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001105}
1106
Ben Lindstrombba81212001-06-25 05:01:22 +00001107static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001108process_mkdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001109{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001110 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +11001111 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001112 int r, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001113
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001114 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
1115 (r = decode_attrib(iqueue, &a)) != 0)
1116 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1117
1118 mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
1119 a.perm & 07777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +10001120 debug3("request %u: mkdir", id);
1121 logit("mkdir name \"%s\" mode 0%o", name, mode);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001122 r = mkdir(name, mode);
1123 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001124 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001125 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001126}
1127
Ben Lindstrombba81212001-06-25 05:01:22 +00001128static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001129process_rmdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001130{
Damien Miller7b28dc52000-09-05 13:34:53 +11001131 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001132 int r, status;
Damien Miller7b28dc52000-09-05 13:34:53 +11001133
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001134 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1135 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1136
Damien Millerfef95ad2006-07-10 20:46:55 +10001137 debug3("request %u: rmdir", id);
1138 logit("rmdir name \"%s\"", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001139 r = rmdir(name);
1140 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001141 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001142 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001143}
1144
Ben Lindstrombba81212001-06-25 05:01:22 +00001145static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001146process_realpath(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001147{
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001148 char resolvedname[PATH_MAX];
Damien Miller7b28dc52000-09-05 13:34:53 +11001149 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001150 int r;
Damien Miller7b28dc52000-09-05 13:34:53 +11001151
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001152 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1153 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1154
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001155 if (path[0] == '\0') {
Darren Tuckera627d422013-06-02 07:31:17 +10001156 free(path);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001157 path = xstrdup(".");
1158 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001159 debug3("request %u: realpath", id);
1160 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001161 if (realpath(path, resolvedname) == NULL) {
1162 send_status(id, errno_to_portable(errno));
1163 } else {
1164 Stat s;
1165 attrib_clear(&s.attrib);
1166 s.name = s.long_name = resolvedname;
1167 send_names(id, 1, &s);
1168 }
Darren Tuckera627d422013-06-02 07:31:17 +10001169 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001170}
1171
Ben Lindstrombba81212001-06-25 05:01:22 +00001172static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001173process_rename(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001174{
Damien Miller7b28dc52000-09-05 13:34:53 +11001175 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001176 int r, status;
Damien Millerb3207e82003-03-26 16:01:11 +11001177 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +11001178
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001179 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1180 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1181 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1182
Damien Millerfef95ad2006-07-10 20:46:55 +10001183 debug3("request %u: rename", id);
1184 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +11001185 status = SSH2_FX_FAILURE;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001186 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +11001187 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +11001188 else if (S_ISREG(sb.st_mode)) {
1189 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001190 if (link(oldpath, newpath) == -1) {
Damien Miller0e265512009-08-28 10:43:13 +10001191 if (errno == EOPNOTSUPP || errno == ENOSYS
Darren Tuckerf7fa7062008-07-04 14:10:19 +10001192#ifdef EXDEV
1193 || errno == EXDEV
1194#endif
Darren Tuckere59b5082004-06-28 16:01:19 +10001195#ifdef LINK_OPNOTSUPP_ERRNO
1196 || errno == LINK_OPNOTSUPP_ERRNO
1197#endif
1198 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001199 struct stat st;
1200
1201 /*
1202 * fs doesn't support links, so fall back to
1203 * stat+rename. This is racy.
1204 */
1205 if (stat(newpath, &st) == -1) {
1206 if (rename(oldpath, newpath) == -1)
1207 status =
1208 errno_to_portable(errno);
1209 else
1210 status = SSH2_FX_OK;
1211 }
1212 } else {
1213 status = errno_to_portable(errno);
1214 }
1215 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001216 status = errno_to_portable(errno);
1217 /* clean spare link */
1218 unlink(newpath);
1219 } else
1220 status = SSH2_FX_OK;
1221 } else if (stat(newpath, &sb) == -1) {
1222 if (rename(oldpath, newpath) == -1)
1223 status = errno_to_portable(errno);
1224 else
1225 status = SSH2_FX_OK;
1226 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001227 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001228 free(oldpath);
1229 free(newpath);
Damien Miller7b28dc52000-09-05 13:34:53 +11001230}
1231
Ben Lindstrombba81212001-06-25 05:01:22 +00001232static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001233process_readlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001234{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001235 int r, len;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001236 char buf[PATH_MAX];
Damien Miller058316f2001-03-08 10:08:49 +11001237 char *path;
1238
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001239 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1240 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1241
Damien Millerfef95ad2006-07-10 20:46:55 +10001242 debug3("request %u: readlink", id);
1243 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001244 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001245 send_status(id, errno_to_portable(errno));
1246 else {
1247 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001248
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001249 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001250 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001251 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001252 send_names(id, 1, &s);
1253 }
Darren Tuckera627d422013-06-02 07:31:17 +10001254 free(path);
Damien Miller058316f2001-03-08 10:08:49 +11001255}
1256
Ben Lindstrombba81212001-06-25 05:01:22 +00001257static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001258process_symlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001259{
Damien Miller058316f2001-03-08 10:08:49 +11001260 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001261 int r, status;
Damien Miller058316f2001-03-08 10:08:49 +11001262
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001263 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1264 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1265 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1266
Damien Millerfef95ad2006-07-10 20:46:55 +10001267 debug3("request %u: symlink", id);
1268 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001269 /* this will fail if 'newpath' exists */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001270 r = symlink(oldpath, newpath);
1271 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001272 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001273 free(oldpath);
1274 free(newpath);
Damien Miller058316f2001-03-08 10:08:49 +11001275}
1276
Ben Lindstrombba81212001-06-25 05:01:22 +00001277static void
Damien Miller7c296612008-03-07 18:33:53 +11001278process_extended_posix_rename(u_int32_t id)
1279{
1280 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001281 int r, status;
Damien Miller7c296612008-03-07 18:33:53 +11001282
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001283 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1284 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1285 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1286
Damien Miller7c296612008-03-07 18:33:53 +11001287 debug3("request %u: posix-rename", id);
1288 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001289 r = rename(oldpath, newpath);
1290 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001291 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001292 free(oldpath);
1293 free(newpath);
Damien Miller7c296612008-03-07 18:33:53 +11001294}
1295
1296static void
Damien Millerd671e5a2008-05-19 14:53:33 +10001297process_extended_statvfs(u_int32_t id)
1298{
1299 char *path;
1300 struct statvfs st;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001301 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +10001302
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001303 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1304 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker2aca1592014-01-19 15:25:34 +11001305 debug3("request %u: statvfs", id);
1306 logit("statvfs \"%s\"", path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001307
1308 if (statvfs(path, &st) != 0)
1309 send_status(id, errno_to_portable(errno));
1310 else
1311 send_statvfs(id, &st);
Darren Tuckera627d422013-06-02 07:31:17 +10001312 free(path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001313}
1314
1315static void
1316process_extended_fstatvfs(u_int32_t id)
1317{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001318 int r, handle, fd;
Damien Millerd671e5a2008-05-19 14:53:33 +10001319 struct statvfs st;
1320
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001321 if ((r = get_handle(iqueue, &handle)) != 0)
1322 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +10001323 debug("request %u: fstatvfs \"%s\" (handle %u)",
1324 id, handle_to_name(handle), handle);
1325 if ((fd = handle_to_fd(handle)) < 0) {
1326 send_status(id, SSH2_FX_FAILURE);
1327 return;
1328 }
1329 if (fstatvfs(fd, &st) != 0)
1330 send_status(id, errno_to_portable(errno));
1331 else
1332 send_statvfs(id, &st);
1333}
1334
1335static void
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001336process_extended_hardlink(u_int32_t id)
1337{
1338 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001339 int r, status;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001340
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001341 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1342 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1343 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1344
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001345 debug3("request %u: hardlink", id);
1346 logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001347 r = link(oldpath, newpath);
1348 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001349 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001350 free(oldpath);
1351 free(newpath);
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001352}
1353
1354static void
Damien Millerf29238e2013-10-17 11:48:52 +11001355process_extended_fsync(u_int32_t id)
1356{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001357 int handle, fd, r, status = SSH2_FX_OP_UNSUPPORTED;
Damien Millerf29238e2013-10-17 11:48:52 +11001358
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001359 if ((r = get_handle(iqueue, &handle)) != 0)
1360 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf29238e2013-10-17 11:48:52 +11001361 debug3("request %u: fsync (handle %u)", id, handle);
1362 verbose("fsync \"%s\"", handle_to_name(handle));
1363 if ((fd = handle_to_fd(handle)) < 0)
1364 status = SSH2_FX_NO_SUCH_FILE;
1365 else if (handle_is_ok(handle, HANDLE_FILE)) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001366 r = fsync(fd);
1367 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Millerf29238e2013-10-17 11:48:52 +11001368 }
1369 send_status(id, status);
1370}
1371
1372static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001373process_extended(u_int32_t id)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001374{
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001375 char *request;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001376 int i, r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001377
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001378 if ((r = sshbuf_get_cstring(iqueue, &request, NULL)) != 0)
1379 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001380 for (i = 0; extended_handlers[i].handler != NULL; i++) {
1381 if (strcmp(request, extended_handlers[i].ext_name) == 0) {
1382 if (!request_permitted(&extended_handlers[i]))
1383 send_status(id, SSH2_FX_PERMISSION_DENIED);
1384 else
1385 extended_handlers[i].handler(id);
1386 break;
1387 }
1388 }
1389 if (extended_handlers[i].handler == NULL) {
1390 error("Unknown extended request \"%.100s\"", request);
Damien Miller7c296612008-03-07 18:33:53 +11001391 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001392 }
Darren Tuckera627d422013-06-02 07:31:17 +10001393 free(request);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001394}
Damien Miller7b28dc52000-09-05 13:34:53 +11001395
1396/* stolen from ssh-agent */
1397
Ben Lindstrombba81212001-06-25 05:01:22 +00001398static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001399process(void)
1400{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001401 u_int msg_len;
1402 u_int buf_len;
1403 u_int consumed;
1404 u_char type;
1405 const u_char *cp;
1406 int i, r;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001407 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +11001408
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001409 buf_len = sshbuf_len(iqueue);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001410 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001411 return; /* Incomplete message. */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001412 cp = sshbuf_ptr(iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001413 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001414 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001415 error("bad message from %s local user %s",
1416 client_addr, pw->pw_name);
Damien Millerdfc24252008-02-10 22:29:40 +11001417 sftp_server_cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001418 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001419 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001420 return;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001421 if ((r = sshbuf_consume(iqueue, 4)) != 0)
1422 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom2c140472002-06-06 21:57:54 +00001423 buf_len -= 4;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001424 if ((r = sshbuf_get_u8(iqueue, &type)) != 0)
1425 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001426
Damien Miller7b28dc52000-09-05 13:34:53 +11001427 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001428 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001429 process_init();
Damien Miller6eaeebf2013-10-15 11:55:57 +11001430 init_done = 1;
Damien Miller058316f2001-03-08 10:08:49 +11001431 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001432 case SSH2_FXP_EXTENDED:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001433 if (!init_done)
1434 fatal("Received extended request before init");
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001435 if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1436 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001437 process_extended(id);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001438 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001439 default:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001440 if (!init_done)
1441 fatal("Received %u request before init", type);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001442 if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1443 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001444 for (i = 0; handlers[i].handler != NULL; i++) {
1445 if (type == handlers[i].type) {
1446 if (!request_permitted(&handlers[i])) {
1447 send_status(id,
1448 SSH2_FX_PERMISSION_DENIED);
1449 } else {
1450 handlers[i].handler(id);
1451 }
1452 break;
1453 }
1454 }
1455 if (handlers[i].handler == NULL)
1456 error("Unknown message %u", type);
Damien Miller7b28dc52000-09-05 13:34:53 +11001457 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001458 /* discard the remaining bytes from the current packet */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001459 if (buf_len < sshbuf_len(iqueue)) {
Damien Millerdfc24252008-02-10 22:29:40 +11001460 error("iqueue grew unexpectedly");
1461 sftp_server_cleanup_exit(255);
1462 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001463 consumed = buf_len - sshbuf_len(iqueue);
Damien Millerdfc24252008-02-10 22:29:40 +11001464 if (msg_len < consumed) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001465 error("msg_len %u < consumed %u", msg_len, consumed);
Damien Millerdfc24252008-02-10 22:29:40 +11001466 sftp_server_cleanup_exit(255);
1467 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001468 if (msg_len > consumed &&
1469 (r = sshbuf_consume(iqueue, msg_len - consumed)) != 0)
1470 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001471}
1472
Damien Millerfef95ad2006-07-10 20:46:55 +10001473/* Cleanup handler that logs active handles upon normal exit */
1474void
Damien Millerdfc24252008-02-10 22:29:40 +11001475sftp_server_cleanup_exit(int i)
Damien Millerfef95ad2006-07-10 20:46:55 +10001476{
1477 if (pw != NULL && client_addr != NULL) {
1478 handle_log_exit();
1479 logit("session closed for local user %s from [%s]",
1480 pw->pw_name, client_addr);
1481 }
1482 _exit(i);
1483}
1484
1485static void
Damien Millerdfc24252008-02-10 22:29:40 +11001486sftp_server_usage(void)
Damien Millerfef95ad2006-07-10 20:46:55 +10001487{
1488 extern char *__progname;
1489
1490 fprintf(stderr,
Damien Milleraa7ad302013-01-09 15:58:21 +11001491 "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
Damien Miller6efab272013-10-15 12:07:05 +11001492 "[-l log_level]\n\t[-P blacklisted_requests] "
1493 "[-p whitelisted_requests] [-u umask]\n"
1494 " %s -Q protocol_feature\n",
1495 __progname, __progname);
Damien Millerfef95ad2006-07-10 20:46:55 +10001496 exit(1);
1497}
1498
Damien Miller7b28dc52000-09-05 13:34:53 +11001499int
Damien Millerd8cb1f12008-02-10 22:40:12 +11001500sftp_server_main(int argc, char **argv, struct passwd *user_pw)
Damien Miller7b28dc52000-09-05 13:34:53 +11001501{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001502 fd_set *rset, *wset;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001503 int i, r, in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001504 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001505 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
Damien Miller502ab0e2013-01-09 15:57:36 +11001506 char *cp, *homedir = NULL, buf[4*4096];
Damien Miller07331212010-11-05 10:20:31 +11001507 long mask;
Damien Millerfef95ad2006-07-10 20:46:55 +10001508
Damien Millerfef95ad2006-07-10 20:46:55 +10001509 extern char *optarg;
1510 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001511
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +00001512 ssh_malloc_init(); /* must be called before any mallocs */
Damien Millerfef95ad2006-07-10 20:46:55 +10001513 __progname = ssh_get_progname(argv[0]);
1514 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001515
Damien Miller502ab0e2013-01-09 15:57:36 +11001516 pw = pwcopy(user_pw);
1517
Damien Miller6eaeebf2013-10-15 11:55:57 +11001518 while (!skipargs && (ch = getopt(argc, argv,
1519 "d:f:l:P:p:Q:u:cehR")) != -1) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001520 switch (ch) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001521 case 'Q':
1522 if (strcasecmp(optarg, "requests") != 0) {
1523 fprintf(stderr, "Invalid query type\n");
1524 exit(1);
1525 }
1526 for (i = 0; handlers[i].handler != NULL; i++)
1527 printf("%s\n", handlers[i].name);
1528 for (i = 0; extended_handlers[i].handler != NULL; i++)
1529 printf("%s\n", extended_handlers[i].name);
1530 exit(0);
1531 break;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001532 case 'R':
1533 readonly = 1;
1534 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001535 case 'c':
1536 /*
1537 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001538 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001539 */
1540 skipargs = 1;
1541 break;
1542 case 'e':
1543 log_stderr = 1;
1544 break;
1545 case 'l':
1546 log_level = log_level_number(optarg);
1547 if (log_level == SYSLOG_LEVEL_NOT_SET)
1548 error("Invalid log level \"%s\"", optarg);
1549 break;
1550 case 'f':
1551 log_facility = log_facility_number(optarg);
Damien Miller35e18db2007-09-17 16:11:33 +10001552 if (log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Millerfef95ad2006-07-10 20:46:55 +10001553 error("Invalid log facility \"%s\"", optarg);
1554 break;
Damien Miller502ab0e2013-01-09 15:57:36 +11001555 case 'd':
1556 cp = tilde_expand_filename(optarg, user_pw->pw_uid);
1557 homedir = percent_expand(cp, "d", user_pw->pw_dir,
1558 "u", user_pw->pw_name, (char *)NULL);
1559 free(cp);
1560 break;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001561 case 'p':
1562 if (request_whitelist != NULL)
1563 fatal("Permitted requests already set");
1564 request_whitelist = xstrdup(optarg);
1565 break;
1566 case 'P':
1567 if (request_blacklist != NULL)
1568 fatal("Refused requests already set");
1569 request_blacklist = xstrdup(optarg);
1570 break;
Darren Tucker7dc48502009-10-07 08:44:42 +11001571 case 'u':
Damien Miller07331212010-11-05 10:20:31 +11001572 errno = 0;
1573 mask = strtol(optarg, &cp, 8);
1574 if (mask < 0 || mask > 0777 || *cp != '\0' ||
1575 cp == optarg || (mask == 0 && errno != 0))
1576 fatal("Invalid umask \"%s\"", optarg);
1577 (void)umask((mode_t)mask);
Darren Tucker7dc48502009-10-07 08:44:42 +11001578 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001579 case 'h':
1580 default:
Damien Millerdfc24252008-02-10 22:29:40 +11001581 sftp_server_usage();
Damien Millerfef95ad2006-07-10 20:46:55 +10001582 }
1583 }
1584
1585 log_init(__progname, log_level, log_facility, log_stderr);
1586
Damien Miller14928b72014-04-01 14:38:07 +11001587 /*
Darren Tucker0fb7f592016-06-09 16:23:07 +10001588 * On platforms where we can, avoid making /proc/self/{mem,maps}
Damien Miller14928b72014-04-01 14:38:07 +11001589 * available to the user so that sftp access doesn't automatically
1590 * imply arbitrary code execution access that will break
1591 * restricted configurations.
1592 */
Darren Tucker0fb7f592016-06-09 16:23:07 +10001593 platform_disable_tracing(1); /* strict */
Damien Miller14928b72014-04-01 14:38:07 +11001594
Damien Miller4626cba2016-01-08 14:24:56 +11001595 /* Drop any fine-grained privileges we don't need */
1596 platform_pledge_sftp_server();
1597
Damien Millerfef95ad2006-07-10 20:46:55 +10001598 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1599 client_addr = xstrdup(cp);
Damien Millerdfc24252008-02-10 22:29:40 +11001600 if ((cp = strchr(client_addr, ' ')) == NULL) {
1601 error("Malformed SSH_CONNECTION variable: \"%s\"",
Damien Millerfef95ad2006-07-10 20:46:55 +10001602 getenv("SSH_CONNECTION"));
Damien Millerdfc24252008-02-10 22:29:40 +11001603 sftp_server_cleanup_exit(255);
1604 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001605 *cp = '\0';
1606 } else
1607 client_addr = xstrdup("UNKNOWN");
1608
Damien Millerfef95ad2006-07-10 20:46:55 +10001609 logit("session opened for local user %s from [%s]",
1610 pw->pw_name, client_addr);
1611
Darren Tuckeraaf51d22010-01-08 19:04:49 +11001612 in = STDIN_FILENO;
1613 out = STDOUT_FILENO;
Damien Miller7b28dc52000-09-05 13:34:53 +11001614
Damien Miller402b3312001-04-14 00:28:42 +10001615#ifdef HAVE_CYGWIN
1616 setmode(in, O_BINARY);
1617 setmode(out, O_BINARY);
1618#endif
1619
Damien Miller7b28dc52000-09-05 13:34:53 +11001620 max = 0;
1621 if (in > max)
1622 max = in;
1623 if (out > max)
1624 max = out;
1625
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001626 if ((iqueue = sshbuf_new()) == NULL)
1627 fatal("%s: sshbuf_new failed", __func__);
1628 if ((oqueue = sshbuf_new()) == NULL)
1629 fatal("%s: sshbuf_new failed", __func__);
Damien Miller7b28dc52000-09-05 13:34:53 +11001630
logan@openbsd.orgdb6f8dc2015-11-16 06:13:04 +00001631 rset = xcalloc(howmany(max + 1, NFDBITS), sizeof(fd_mask));
1632 wset = xcalloc(howmany(max + 1, NFDBITS), sizeof(fd_mask));
Damien Miller7b28dc52000-09-05 13:34:53 +11001633
Damien Miller502ab0e2013-01-09 15:57:36 +11001634 if (homedir != NULL) {
1635 if (chdir(homedir) != 0) {
1636 error("chdir to \"%s\" failed: %s", homedir,
1637 strerror(errno));
1638 }
1639 }
1640
logan@openbsd.orgdb6f8dc2015-11-16 06:13:04 +00001641 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001642 for (;;) {
1643 memset(rset, 0, set_size);
1644 memset(wset, 0, set_size);
1645
Darren Tuckere9405982007-05-20 15:09:04 +10001646 /*
1647 * Ensure that we can read a full buffer and handle
1648 * the worst-case length packet it can generate,
1649 * otherwise apply backpressure by stopping reads.
1650 */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001651 if ((r = sshbuf_check_reserve(iqueue, sizeof(buf))) == 0 &&
1652 (r = sshbuf_check_reserve(oqueue,
1653 SFTP_MAX_MSG_LENGTH)) == 0)
Darren Tuckere9405982007-05-20 15:09:04 +10001654 FD_SET(in, rset);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001655 else if (r != SSH_ERR_NO_BUFFER_SPACE)
1656 fatal("%s: sshbuf_check_reserve failed: %s",
1657 __func__, ssh_err(r));
Darren Tuckere9405982007-05-20 15:09:04 +10001658
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001659 olen = sshbuf_len(oqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +11001660 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001661 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001662
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001663 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001664 if (errno == EINTR)
1665 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001666 error("select: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001667 sftp_server_cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001668 }
1669
1670 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001671 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001672 len = read(in, buf, sizeof buf);
1673 if (len == 0) {
1674 debug("read eof");
Damien Millerdfc24252008-02-10 22:29:40 +11001675 sftp_server_cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001676 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001677 error("read: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001678 sftp_server_cleanup_exit(1);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001679 } else if ((r = sshbuf_put(iqueue, buf, len)) != 0) {
1680 fatal("%s: buffer error: %s",
1681 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001682 }
1683 }
1684 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001685 if (FD_ISSET(out, wset)) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001686 len = write(out, sshbuf_ptr(oqueue), olen);
Damien Miller7b28dc52000-09-05 13:34:53 +11001687 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001688 error("write: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001689 sftp_server_cleanup_exit(1);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001690 } else if ((r = sshbuf_consume(oqueue, len)) != 0) {
1691 fatal("%s: buffer error: %s",
1692 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001693 }
1694 }
Darren Tuckere9405982007-05-20 15:09:04 +10001695
1696 /*
1697 * Process requests from client if we can fit the results
1698 * into the output buffer, otherwise stop processing input
1699 * and let the output queue drain.
1700 */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001701 r = sshbuf_check_reserve(oqueue, SFTP_MAX_MSG_LENGTH);
1702 if (r == 0)
Darren Tuckere9405982007-05-20 15:09:04 +10001703 process();
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001704 else if (r != SSH_ERR_NO_BUFFER_SPACE)
1705 fatal("%s: sshbuf_check_reserve: %s",
1706 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001707 }
1708}