blob: 646286a3cda577cf4da7e07fce0b45c5136809d8 [file] [log] [blame]
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +00001/* $OpenBSD: sftp-server.c,v 1.109 2016/02/15 09:47:49 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
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000020#include <sys/param.h> /* MIN */
Damien Millerf17883e2006-03-15 11:45:54 +110021#include <sys/types.h>
22#include <sys/stat.h>
Damien Miller9aec9192006-08-05 10:57:45 +100023#ifdef HAVE_SYS_TIME_H
24# include <sys/time.h>
25#endif
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100026#ifdef HAVE_SYS_MOUNT_H
Damien Millerd671e5a2008-05-19 14:53:33 +100027#include <sys/mount.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100028#endif
29#ifdef HAVE_SYS_STATVFS_H
Damien Millerd671e5a2008-05-19 14:53:33 +100030#include <sys/statvfs.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100031#endif
Damien Miller88f254b2006-03-15 11:25:13 +110032
33#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100035#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100036#include <pwd.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100037#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039#include <string.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100040#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100042#include <stdarg.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110043
Damien Miller7b28dc52000-09-05 13:34:53 +110044#include "xmalloc.h"
djm@openbsd.org7d845f42015-01-14 13:54:13 +000045#include "sshbuf.h"
46#include "ssherr.h"
Damien Millerd7834352006-08-05 12:39:39 +100047#include "log.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100048#include "misc.h"
Damien Miller6eaeebf2013-10-15 11:55:57 +110049#include "match.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100050#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110051
Ben Lindstrom2f959b42001-01-11 06:20:23 +000052#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110053#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110054
Damien Millerfef95ad2006-07-10 20:46:55 +100055/* Our verbosity */
Damien Miller6eaeebf2013-10-15 11:55:57 +110056static LogLevel log_level = SYSLOG_LEVEL_ERROR;
Damien Millerfef95ad2006-07-10 20:46:55 +100057
58/* Our client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110059static struct passwd *pw = NULL;
60static char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000061
Damien Miller7b28dc52000-09-05 13:34:53 +110062/* input and output queue */
djm@openbsd.org7d845f42015-01-14 13:54:13 +000063struct sshbuf *iqueue;
64struct sshbuf *oqueue;
Damien Miller7b28dc52000-09-05 13:34:53 +110065
Damien Miller058316f2001-03-08 10:08:49 +110066/* Version of client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110067static u_int version;
68
69/* SSH2_FXP_INIT received */
70static int init_done;
Damien Miller058316f2001-03-08 10:08:49 +110071
Darren Tuckerdb7bf822010-01-09 22:24:33 +110072/* Disable writes */
Damien Miller6eaeebf2013-10-15 11:55:57 +110073static int readonly;
74
75/* Requests that are allowed/denied */
76static char *request_whitelist, *request_blacklist;
Darren Tuckerdb7bf822010-01-09 22:24:33 +110077
Darren Tuckera6612d42003-06-28 12:39:03 +100078/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110079typedef struct Stat Stat;
80
Damien Miller33804262001-02-04 23:20:18 +110081struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110082 char *name;
83 char *long_name;
84 Attrib attrib;
85};
86
Damien Miller6eaeebf2013-10-15 11:55:57 +110087/* Packet handlers */
88static void process_open(u_int32_t id);
89static void process_close(u_int32_t id);
90static void process_read(u_int32_t id);
91static void process_write(u_int32_t id);
92static void process_stat(u_int32_t id);
93static void process_lstat(u_int32_t id);
94static void process_fstat(u_int32_t id);
95static void process_setstat(u_int32_t id);
96static void process_fsetstat(u_int32_t id);
97static void process_opendir(u_int32_t id);
98static void process_readdir(u_int32_t id);
99static void process_remove(u_int32_t id);
100static void process_mkdir(u_int32_t id);
101static void process_rmdir(u_int32_t id);
102static void process_realpath(u_int32_t id);
103static void process_rename(u_int32_t id);
104static void process_readlink(u_int32_t id);
105static void process_symlink(u_int32_t id);
106static void process_extended_posix_rename(u_int32_t id);
107static void process_extended_statvfs(u_int32_t id);
108static void process_extended_fstatvfs(u_int32_t id);
109static void process_extended_hardlink(u_int32_t id);
Damien Millerf29238e2013-10-17 11:48:52 +1100110static void process_extended_fsync(u_int32_t id);
Damien Miller6eaeebf2013-10-15 11:55:57 +1100111static void process_extended(u_int32_t id);
112
113struct sftp_handler {
114 const char *name; /* user-visible name for fine-grained perms */
115 const char *ext_name; /* extended request name */
116 u_int type; /* packet type, for non extended packets */
117 void (*handler)(u_int32_t);
118 int does_write; /* if nonzero, banned for readonly mode */
119};
120
121struct sftp_handler handlers[] = {
122 /* NB. SSH2_FXP_OPEN does the readonly check in the handler itself */
123 { "open", NULL, SSH2_FXP_OPEN, process_open, 0 },
124 { "close", NULL, SSH2_FXP_CLOSE, process_close, 0 },
125 { "read", NULL, SSH2_FXP_READ, process_read, 0 },
126 { "write", NULL, SSH2_FXP_WRITE, process_write, 1 },
127 { "lstat", NULL, SSH2_FXP_LSTAT, process_lstat, 0 },
128 { "fstat", NULL, SSH2_FXP_FSTAT, process_fstat, 0 },
129 { "setstat", NULL, SSH2_FXP_SETSTAT, process_setstat, 1 },
130 { "fsetstat", NULL, SSH2_FXP_FSETSTAT, process_fsetstat, 1 },
131 { "opendir", NULL, SSH2_FXP_OPENDIR, process_opendir, 0 },
132 { "readdir", NULL, SSH2_FXP_READDIR, process_readdir, 0 },
133 { "remove", NULL, SSH2_FXP_REMOVE, process_remove, 1 },
134 { "mkdir", NULL, SSH2_FXP_MKDIR, process_mkdir, 1 },
135 { "rmdir", NULL, SSH2_FXP_RMDIR, process_rmdir, 1 },
136 { "realpath", NULL, SSH2_FXP_REALPATH, process_realpath, 0 },
137 { "stat", NULL, SSH2_FXP_STAT, process_stat, 0 },
138 { "rename", NULL, SSH2_FXP_RENAME, process_rename, 1 },
139 { "readlink", NULL, SSH2_FXP_READLINK, process_readlink, 0 },
140 { "symlink", NULL, SSH2_FXP_SYMLINK, process_symlink, 1 },
141 { NULL, NULL, 0, NULL, 0 }
142};
143
144/* SSH2_FXP_EXTENDED submessages */
145struct sftp_handler extended_handlers[] = {
146 { "posix-rename", "posix-rename@openssh.com", 0,
147 process_extended_posix_rename, 1 },
148 { "statvfs", "statvfs@openssh.com", 0, process_extended_statvfs, 0 },
149 { "fstatvfs", "fstatvfs@openssh.com", 0, process_extended_fstatvfs, 0 },
150 { "hardlink", "hardlink@openssh.com", 0, process_extended_hardlink, 1 },
Damien Millerf29238e2013-10-17 11:48:52 +1100151 { "fsync", "fsync@openssh.com", 0, process_extended_fsync, 1 },
Damien Miller6eaeebf2013-10-15 11:55:57 +1100152 { NULL, NULL, 0, NULL, 0 }
153};
154
155static int
156request_permitted(struct sftp_handler *h)
157{
158 char *result;
159
160 if (readonly && h->does_write) {
161 verbose("Refusing %s request in read-only mode", h->name);
162 return 0;
163 }
164 if (request_blacklist != NULL &&
165 ((result = match_list(h->name, request_blacklist, NULL))) != NULL) {
166 free(result);
167 verbose("Refusing blacklisted %s request", h->name);
168 return 0;
169 }
170 if (request_whitelist != NULL &&
171 ((result = match_list(h->name, request_whitelist, NULL))) != NULL) {
172 free(result);
173 debug2("Permitting whitelisted %s request", h->name);
174 return 1;
175 }
176 if (request_whitelist != NULL) {
177 verbose("Refusing non-whitelisted %s request", h->name);
178 return 0;
179 }
180 return 1;
181}
182
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100184errno_to_portable(int unixerrno)
185{
186 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000187
Damien Miller7b28dc52000-09-05 13:34:53 +1100188 switch (unixerrno) {
189 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000190 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100191 break;
192 case ENOENT:
193 case ENOTDIR:
194 case EBADF:
195 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000196 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100197 break;
198 case EPERM:
199 case EACCES:
200 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000201 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +1100202 break;
203 case ENAMETOOLONG:
204 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000205 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100206 break;
Darren Tucker422c34c2008-06-09 22:48:31 +1000207 case ENOSYS:
208 ret = SSH2_FX_OP_UNSUPPORTED;
209 break;
Damien Miller7b28dc52000-09-05 13:34:53 +1100210 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000211 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100212 break;
213 }
214 return ret;
215}
216
Ben Lindstrombba81212001-06-25 05:01:22 +0000217static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100218flags_from_portable(int pflags)
219{
220 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000221
Ben Lindstrom36592512001-03-05 05:02:08 +0000222 if ((pflags & SSH2_FXF_READ) &&
223 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100224 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000225 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100226 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000227 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100228 flags = O_WRONLY;
229 }
Damien Millere9fc72e2013-10-15 12:14:12 +1100230 if (pflags & SSH2_FXF_APPEND)
231 flags |= O_APPEND;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000232 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100233 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000234 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100235 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000236 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100237 flags |= O_EXCL;
238 return flags;
239}
240
Damien Millerfef95ad2006-07-10 20:46:55 +1000241static const char *
242string_from_portable(int pflags)
243{
244 static char ret[128];
245
246 *ret = '\0';
247
248#define PAPPEND(str) { \
249 if (*ret != '\0') \
250 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000251 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000252 }
253
254 if (pflags & SSH2_FXF_READ)
255 PAPPEND("READ")
256 if (pflags & SSH2_FXF_WRITE)
257 PAPPEND("WRITE")
Damien Millere9fc72e2013-10-15 12:14:12 +1100258 if (pflags & SSH2_FXF_APPEND)
259 PAPPEND("APPEND")
Damien Millerfef95ad2006-07-10 20:46:55 +1000260 if (pflags & SSH2_FXF_CREAT)
261 PAPPEND("CREATE")
262 if (pflags & SSH2_FXF_TRUNC)
263 PAPPEND("TRUNCATE")
264 if (pflags & SSH2_FXF_EXCL)
265 PAPPEND("EXCL")
266
267 return ret;
268}
269
Damien Miller7b28dc52000-09-05 13:34:53 +1100270/* handle handles */
271
272typedef struct Handle Handle;
273struct Handle {
274 int use;
275 DIR *dirp;
276 int fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100277 int flags;
Damien Miller7b28dc52000-09-05 13:34:53 +1100278 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000279 u_int64_t bytes_read, bytes_write;
Damien Miller3397d0e2008-02-10 22:26:51 +1100280 int next_unused;
Damien Miller7b28dc52000-09-05 13:34:53 +1100281};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000282
Damien Miller7b28dc52000-09-05 13:34:53 +1100283enum {
284 HANDLE_UNUSED,
285 HANDLE_DIR,
286 HANDLE_FILE
287};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000288
Damien Miller3397d0e2008-02-10 22:26:51 +1100289Handle *handles = NULL;
290u_int num_handles = 0;
291int first_unused_handle = -1;
Damien Miller7b28dc52000-09-05 13:34:53 +1100292
Damien Miller3397d0e2008-02-10 22:26:51 +1100293static void handle_unused(int i)
Damien Miller7b28dc52000-09-05 13:34:53 +1100294{
Damien Miller3397d0e2008-02-10 22:26:51 +1100295 handles[i].use = HANDLE_UNUSED;
296 handles[i].next_unused = first_unused_handle;
297 first_unused_handle = i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100298}
299
Ben Lindstrombba81212001-06-25 05:01:22 +0000300static int
Damien Millere9fc72e2013-10-15 12:14:12 +1100301handle_new(int use, const char *name, int fd, int flags, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100302{
Damien Miller3397d0e2008-02-10 22:26:51 +1100303 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000304
Damien Miller3397d0e2008-02-10 22:26:51 +1100305 if (first_unused_handle == -1) {
306 if (num_handles + 1 <= num_handles)
307 return -1;
308 num_handles++;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000309 handles = xreallocarray(handles, num_handles, sizeof(Handle));
Damien Miller3397d0e2008-02-10 22:26:51 +1100310 handle_unused(num_handles - 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100311 }
Damien Miller3397d0e2008-02-10 22:26:51 +1100312
313 i = first_unused_handle;
314 first_unused_handle = handles[i].next_unused;
315
316 handles[i].use = use;
317 handles[i].dirp = dirp;
318 handles[i].fd = fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100319 handles[i].flags = flags;
Damien Miller3397d0e2008-02-10 22:26:51 +1100320 handles[i].name = xstrdup(name);
321 handles[i].bytes_read = handles[i].bytes_write = 0;
322
323 return i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100324}
325
Ben Lindstrombba81212001-06-25 05:01:22 +0000326static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100327handle_is_ok(int i, int type)
328{
Damien Miller3397d0e2008-02-10 22:26:51 +1100329 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100330}
331
Ben Lindstrombba81212001-06-25 05:01:22 +0000332static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000333handle_to_string(int handle, u_char **stringp, int *hlenp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100334{
Damien Miller7b28dc52000-09-05 13:34:53 +1100335 if (stringp == NULL || hlenp == NULL)
336 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000337 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100338 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000339 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100340 return 0;
341}
342
Ben Lindstrombba81212001-06-25 05:01:22 +0000343static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000344handle_from_string(const u_char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100345{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000346 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000347
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000348 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100349 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100350 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100351 if (handle_is_ok(val, HANDLE_FILE) ||
352 handle_is_ok(val, HANDLE_DIR))
353 return val;
354 return -1;
355}
356
Ben Lindstrombba81212001-06-25 05:01:22 +0000357static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100358handle_to_name(int handle)
359{
360 if (handle_is_ok(handle, HANDLE_DIR)||
361 handle_is_ok(handle, HANDLE_FILE))
362 return handles[handle].name;
363 return NULL;
364}
365
Ben Lindstrombba81212001-06-25 05:01:22 +0000366static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100367handle_to_dir(int handle)
368{
369 if (handle_is_ok(handle, HANDLE_DIR))
370 return handles[handle].dirp;
371 return NULL;
372}
373
Ben Lindstrombba81212001-06-25 05:01:22 +0000374static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100375handle_to_fd(int handle)
376{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000377 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100378 return handles[handle].fd;
379 return -1;
380}
381
Damien Millere9fc72e2013-10-15 12:14:12 +1100382static int
383handle_to_flags(int handle)
384{
385 if (handle_is_ok(handle, HANDLE_FILE))
386 return handles[handle].flags;
387 return 0;
388}
389
Damien Millerfef95ad2006-07-10 20:46:55 +1000390static void
391handle_update_read(int handle, ssize_t bytes)
392{
393 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
394 handles[handle].bytes_read += bytes;
395}
396
397static void
398handle_update_write(int handle, ssize_t bytes)
399{
400 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
401 handles[handle].bytes_write += bytes;
402}
403
404static u_int64_t
405handle_bytes_read(int handle)
406{
407 if (handle_is_ok(handle, HANDLE_FILE))
408 return (handles[handle].bytes_read);
409 return 0;
410}
411
412static u_int64_t
413handle_bytes_write(int handle)
414{
415 if (handle_is_ok(handle, HANDLE_FILE))
416 return (handles[handle].bytes_write);
417 return 0;
418}
419
Ben Lindstrombba81212001-06-25 05:01:22 +0000420static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100421handle_close(int handle)
422{
423 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000424
Damien Miller7b28dc52000-09-05 13:34:53 +1100425 if (handle_is_ok(handle, HANDLE_FILE)) {
426 ret = close(handles[handle].fd);
Darren Tuckera627d422013-06-02 07:31:17 +1000427 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100428 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100429 } else if (handle_is_ok(handle, HANDLE_DIR)) {
430 ret = closedir(handles[handle].dirp);
Darren Tuckera627d422013-06-02 07:31:17 +1000431 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100432 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100433 } else {
434 errno = ENOENT;
435 }
436 return ret;
437}
438
Damien Millerfef95ad2006-07-10 20:46:55 +1000439static void
440handle_log_close(int handle, char *emsg)
441{
442 if (handle_is_ok(handle, HANDLE_FILE)) {
443 logit("%s%sclose \"%s\" bytes read %llu written %llu",
444 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
445 handle_to_name(handle),
Darren Tucker86473c52007-05-20 14:59:32 +1000446 (unsigned long long)handle_bytes_read(handle),
447 (unsigned long long)handle_bytes_write(handle));
Damien Millerfef95ad2006-07-10 20:46:55 +1000448 } else {
449 logit("%s%sclosedir \"%s\"",
450 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
451 handle_to_name(handle));
452 }
453}
454
455static void
456handle_log_exit(void)
457{
458 u_int i;
459
Damien Miller3397d0e2008-02-10 22:26:51 +1100460 for (i = 0; i < num_handles; i++)
Damien Millerfef95ad2006-07-10 20:46:55 +1000461 if (handles[i].use != HANDLE_UNUSED)
462 handle_log_close(i, "forced");
463}
464
Ben Lindstrombba81212001-06-25 05:01:22 +0000465static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000466get_handle(struct sshbuf *queue, int *hp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100467{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000468 u_char *handle;
469 int r;
470 size_t hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000471
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000472 *hp = -1;
473 if ((r = sshbuf_get_string(queue, &handle, &hlen)) != 0)
474 return r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000475 if (hlen < 256)
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000476 *hp = handle_from_string(handle, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000477 free(handle);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000478 return 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100479}
480
481/* send replies */
482
Ben Lindstrombba81212001-06-25 05:01:22 +0000483static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000484send_msg(struct sshbuf *m)
Damien Miller7b28dc52000-09-05 13:34:53 +1100485{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000486 int r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000487
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000488 if ((r = sshbuf_put_stringb(oqueue, m)) != 0)
489 fatal("%s: buffer error: %s", __func__, ssh_err(r));
490 sshbuf_reset(m);
Damien Miller7b28dc52000-09-05 13:34:53 +1100491}
492
Damien Millerfef95ad2006-07-10 20:46:55 +1000493static const char *
494status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100495{
Damien Miller058316f2001-03-08 10:08:49 +1100496 const char *status_messages[] = {
497 "Success", /* SSH_FX_OK */
498 "End of file", /* SSH_FX_EOF */
499 "No such file", /* SSH_FX_NO_SUCH_FILE */
500 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
501 "Failure", /* SSH_FX_FAILURE */
502 "Bad message", /* SSH_FX_BAD_MESSAGE */
503 "No connection", /* SSH_FX_NO_CONNECTION */
504 "Connection lost", /* SSH_FX_CONNECTION_LOST */
505 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
506 "Unknown error" /* Others */
507 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000508 return (status_messages[MIN(status,SSH2_FX_MAX)]);
509}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000510
Damien Millerfef95ad2006-07-10 20:46:55 +1000511static void
512send_status(u_int32_t id, u_int32_t status)
513{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000514 struct sshbuf *msg;
515 int r;
Damien Millerfef95ad2006-07-10 20:46:55 +1000516
517 debug3("request %u: sent status %u", id, status);
518 if (log_level > SYSLOG_LEVEL_VERBOSE ||
519 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
520 logit("sent status %s", status_to_message(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000521 if ((msg = sshbuf_new()) == NULL)
522 fatal("%s: sshbuf_new failed", __func__);
523 if ((r = sshbuf_put_u8(msg, SSH2_FXP_STATUS)) != 0 ||
524 (r = sshbuf_put_u32(msg, id)) != 0 ||
525 (r = sshbuf_put_u32(msg, status)) != 0)
526 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +1100527 if (version >= 3) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000528 if ((r = sshbuf_put_cstring(msg,
529 status_to_message(status))) != 0 ||
530 (r = sshbuf_put_cstring(msg, "")) != 0)
531 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +1100532 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000533 send_msg(msg);
534 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100535}
Ben Lindstrombba81212001-06-25 05:01:22 +0000536static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000537send_data_or_handle(char type, u_int32_t id, const u_char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100538{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000539 struct sshbuf *msg;
540 int r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000541
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000542 if ((msg = sshbuf_new()) == NULL)
543 fatal("%s: sshbuf_new failed", __func__);
544 if ((r = sshbuf_put_u8(msg, type)) != 0 ||
545 (r = sshbuf_put_u32(msg, id)) != 0 ||
546 (r = sshbuf_put_string(msg, data, dlen)) != 0)
547 fatal("%s: buffer error: %s", __func__, ssh_err(r));
548 send_msg(msg);
549 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100550}
551
Ben Lindstrombba81212001-06-25 05:01:22 +0000552static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000553send_data(u_int32_t id, const u_char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100554{
Damien Millerfef95ad2006-07-10 20:46:55 +1000555 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000556 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100557}
558
Ben Lindstrombba81212001-06-25 05:01:22 +0000559static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100560send_handle(u_int32_t id, int handle)
561{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000562 u_char *string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100563 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000564
Damien Miller7b28dc52000-09-05 13:34:53 +1100565 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000566 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000567 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000568 free(string);
Damien Miller7b28dc52000-09-05 13:34:53 +1100569}
570
Ben Lindstrombba81212001-06-25 05:01:22 +0000571static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100572send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100573{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000574 struct sshbuf *msg;
575 int i, r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000576
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000577 if ((msg = sshbuf_new()) == NULL)
578 fatal("%s: sshbuf_new failed", __func__);
579 if ((r = sshbuf_put_u8(msg, SSH2_FXP_NAME)) != 0 ||
580 (r = sshbuf_put_u32(msg, id)) != 0 ||
581 (r = sshbuf_put_u32(msg, count)) != 0)
582 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerfef95ad2006-07-10 20:46:55 +1000583 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100584 for (i = 0; i < count; i++) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000585 if ((r = sshbuf_put_cstring(msg, stats[i].name)) != 0 ||
586 (r = sshbuf_put_cstring(msg, stats[i].long_name)) != 0 ||
587 (r = encode_attrib(msg, &stats[i].attrib)) != 0)
588 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100589 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000590 send_msg(msg);
591 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100592}
593
Ben Lindstrombba81212001-06-25 05:01:22 +0000594static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100595send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100596{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000597 struct sshbuf *msg;
598 int r;
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);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000601 if ((msg = sshbuf_new()) == NULL)
602 fatal("%s: sshbuf_new failed", __func__);
603 if ((r = sshbuf_put_u8(msg, SSH2_FXP_ATTRS)) != 0 ||
604 (r = sshbuf_put_u32(msg, id)) != 0 ||
605 (r = encode_attrib(msg, a)) != 0)
606 fatal("%s: buffer error: %s", __func__, ssh_err(r));
607 send_msg(msg);
608 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100609}
610
Damien Millerd671e5a2008-05-19 14:53:33 +1000611static void
612send_statvfs(u_int32_t id, struct statvfs *st)
613{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000614 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +1000615 u_int64_t flag;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000616 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +1000617
618 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
619 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
620
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000621 if ((msg = sshbuf_new()) == NULL)
622 fatal("%s: sshbuf_new failed", __func__);
623 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED_REPLY)) != 0 ||
624 (r = sshbuf_put_u32(msg, id)) != 0 ||
625 (r = sshbuf_put_u64(msg, st->f_bsize)) != 0 ||
626 (r = sshbuf_put_u64(msg, st->f_frsize)) != 0 ||
627 (r = sshbuf_put_u64(msg, st->f_blocks)) != 0 ||
628 (r = sshbuf_put_u64(msg, st->f_bfree)) != 0 ||
629 (r = sshbuf_put_u64(msg, st->f_bavail)) != 0 ||
630 (r = sshbuf_put_u64(msg, st->f_files)) != 0 ||
631 (r = sshbuf_put_u64(msg, st->f_ffree)) != 0 ||
632 (r = sshbuf_put_u64(msg, st->f_favail)) != 0 ||
633 (r = sshbuf_put_u64(msg, FSID_TO_ULONG(st->f_fsid))) != 0 ||
634 (r = sshbuf_put_u64(msg, flag)) != 0 ||
635 (r = sshbuf_put_u64(msg, st->f_namemax)) != 0)
636 fatal("%s: buffer error: %s", __func__, ssh_err(r));
637 send_msg(msg);
638 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000639}
640
Damien Miller7b28dc52000-09-05 13:34:53 +1100641/* parse incoming */
642
Ben Lindstrombba81212001-06-25 05:01:22 +0000643static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100644process_init(void)
645{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000646 struct sshbuf *msg;
647 int r;
Damien Miller7b28dc52000-09-05 13:34:53 +1100648
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000649 if ((r = sshbuf_get_u32(iqueue, &version)) != 0)
650 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf145a5b2011-06-20 14:42:51 +1000651 verbose("received client version %u", version);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000652 if ((msg = sshbuf_new()) == NULL)
653 fatal("%s: sshbuf_new failed", __func__);
654 if ((r = sshbuf_put_u8(msg, SSH2_FXP_VERSION)) != 0 ||
655 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0 ||
656 /* POSIX rename extension */
657 (r = sshbuf_put_cstring(msg, "posix-rename@openssh.com")) != 0 ||
658 (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
659 /* statvfs extension */
660 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
661 (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
662 /* fstatvfs extension */
663 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
664 (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
665 /* hardlink extension */
666 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
667 (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
668 /* fsync extension */
669 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
670 (r = sshbuf_put_cstring(msg, "1")) != 0) /* version */
671 fatal("%s: buffer error: %s", __func__, ssh_err(r));
672 send_msg(msg);
673 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100674}
675
Ben Lindstrombba81212001-06-25 05:01:22 +0000676static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100677process_open(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100678{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100679 u_int32_t pflags;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000680 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100681 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000682 int r, handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100683
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000684 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
685 (r = sshbuf_get_u32(iqueue, &pflags)) != 0 || /* portable flags */
686 (r = decode_attrib(iqueue, &a)) != 0)
687 fatal("%s: buffer error: %s", __func__, ssh_err(r));
688
Damien Miller6444fe92006-07-10 21:31:27 +1000689 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100690 flags = flags_from_portable(pflags);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000691 mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a.perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000692 logit("open \"%s\" flags %s mode 0%o",
693 name, string_from_portable(pflags), mode);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100694 if (readonly &&
Damien Miller6eaeebf2013-10-15 11:55:57 +1100695 ((flags & O_ACCMODE) == O_WRONLY ||
696 (flags & O_ACCMODE) == O_RDWR)) {
697 verbose("Refusing open request in read-only mode");
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000698 status = SSH2_FX_PERMISSION_DENIED;
Damien Miller6eaeebf2013-10-15 11:55:57 +1100699 } else {
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100700 fd = open(name, flags, mode);
701 if (fd < 0) {
702 status = errno_to_portable(errno);
Damien Miller7b28dc52000-09-05 13:34:53 +1100703 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100704 handle = handle_new(HANDLE_FILE, name, fd, flags, NULL);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100705 if (handle < 0) {
706 close(fd);
707 } else {
708 send_handle(id, handle);
709 status = SSH2_FX_OK;
710 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100711 }
712 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000713 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100714 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000715 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100716}
717
Ben Lindstrombba81212001-06-25 05:01:22 +0000718static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100719process_close(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100720{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000721 int r, handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100722
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000723 if ((r = get_handle(iqueue, &handle)) != 0)
724 fatal("%s: buffer error: %s", __func__, ssh_err(r));
725
Damien Millerfef95ad2006-07-10 20:46:55 +1000726 debug3("request %u: close handle %u", id, handle);
727 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100728 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000729 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100730 send_status(id, status);
731}
732
Ben Lindstrombba81212001-06-25 05:01:22 +0000733static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100734process_read(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100735{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000736 u_char buf[64*1024];
Damien Miller6eaeebf2013-10-15 11:55:57 +1100737 u_int32_t len;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000738 int r, handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100739 u_int64_t off;
740
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000741 if ((r = get_handle(iqueue, &handle)) != 0 ||
742 (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
743 (r = sshbuf_get_u32(iqueue, &len)) != 0)
744 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100745
Damien Millerfef95ad2006-07-10 20:46:55 +1000746 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
747 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100748 if (len > sizeof buf) {
749 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000750 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100751 }
752 fd = handle_to_fd(handle);
753 if (fd >= 0) {
754 if (lseek(fd, off, SEEK_SET) < 0) {
755 error("process_read: seek failed");
756 status = errno_to_portable(errno);
757 } else {
758 ret = read(fd, buf, len);
759 if (ret < 0) {
760 status = errno_to_portable(errno);
761 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000762 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100763 } else {
764 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000765 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000766 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100767 }
768 }
769 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000770 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100771 send_status(id, status);
772}
773
Ben Lindstrombba81212001-06-25 05:01:22 +0000774static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100775process_write(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100776{
Damien Miller7b28dc52000-09-05 13:34:53 +1100777 u_int64_t off;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000778 size_t len;
779 int r, handle, fd, ret, status;
780 u_char *data;
Damien Miller7b28dc52000-09-05 13:34:53 +1100781
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000782 if ((r = get_handle(iqueue, &handle)) != 0 ||
783 (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
784 (r = sshbuf_get_string(iqueue, &data, &len)) != 0)
785 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100786
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000787 debug("request %u: write \"%s\" (handle %d) off %llu len %zu",
Damien Millerfef95ad2006-07-10 20:46:55 +1000788 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100789 fd = handle_to_fd(handle);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000790
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100791 if (fd < 0)
792 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100793 else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100794 if (!(handle_to_flags(handle) & O_APPEND) &&
795 lseek(fd, off, SEEK_SET) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100796 status = errno_to_portable(errno);
797 error("process_write: seek failed");
798 } else {
799/* XXX ATOMICIO ? */
800 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000801 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100802 error("process_write: write failed");
803 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000804 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000805 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000806 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100807 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000808 debug2("nothing at all written");
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100809 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100810 }
811 }
812 }
813 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000814 free(data);
Damien Miller7b28dc52000-09-05 13:34:53 +1100815}
816
Ben Lindstrombba81212001-06-25 05:01:22 +0000817static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100818process_do_stat(u_int32_t id, int do_lstat)
Damien Miller7b28dc52000-09-05 13:34:53 +1100819{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000820 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100821 struct stat st;
Damien Miller7b28dc52000-09-05 13:34:53 +1100822 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000823 int r, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100824
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000825 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
826 fatal("%s: buffer error: %s", __func__, ssh_err(r));
827
Damien Millerfef95ad2006-07-10 20:46:55 +1000828 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
829 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000830 r = do_lstat ? lstat(name, &st) : stat(name, &st);
831 if (r < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100832 status = errno_to_portable(errno);
833 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000834 stat_to_attrib(&st, &a);
835 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000836 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100837 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000838 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100839 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000840 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100841}
842
Ben Lindstrombba81212001-06-25 05:01:22 +0000843static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100844process_stat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100845{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100846 process_do_stat(id, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100847}
848
Ben Lindstrombba81212001-06-25 05:01:22 +0000849static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100850process_lstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100851{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100852 process_do_stat(id, 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100853}
854
Ben Lindstrombba81212001-06-25 05:01:22 +0000855static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100856process_fstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100857{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000858 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100859 struct stat st;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000860 int fd, r, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100861
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000862 if ((r = get_handle(iqueue, &handle)) != 0)
863 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerfef95ad2006-07-10 20:46:55 +1000864 debug("request %u: fstat \"%s\" (handle %u)",
865 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100866 fd = handle_to_fd(handle);
Damien Millere2334d62007-01-05 16:31:02 +1100867 if (fd >= 0) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000868 r = fstat(fd, &st);
869 if (r < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100870 status = errno_to_portable(errno);
871 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000872 stat_to_attrib(&st, &a);
873 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000874 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100875 }
876 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000877 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100878 send_status(id, status);
879}
880
Ben Lindstrombba81212001-06-25 05:01:22 +0000881static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100882attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100883{
884 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000885
Damien Miller7b28dc52000-09-05 13:34:53 +1100886 tv[0].tv_sec = a->atime;
887 tv[0].tv_usec = 0;
888 tv[1].tv_sec = a->mtime;
889 tv[1].tv_usec = 0;
890 return tv;
891}
892
Ben Lindstrombba81212001-06-25 05:01:22 +0000893static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100894process_setstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100895{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000896 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100897 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000898 int r, status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100899
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000900 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
901 (r = decode_attrib(iqueue, &a)) != 0)
902 fatal("%s: buffer error: %s", __func__, ssh_err(r));
903
Damien Millerfef95ad2006-07-10 20:46:55 +1000904 debug("request %u: setstat name \"%s\"", id, name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000905 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000906 logit("set \"%s\" size %llu",
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000907 name, (unsigned long long)a.size);
908 r = truncate(name, a.size);
909 if (r == -1)
Damien Miller00c92172002-02-13 14:05:00 +1100910 status = errno_to_portable(errno);
911 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000912 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
913 logit("set \"%s\" mode %04o", name, a.perm);
914 r = chmod(name, a.perm & 07777);
915 if (r == -1)
Damien Miller7b28dc52000-09-05 13:34:53 +1100916 status = errno_to_portable(errno);
917 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000918 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000919 char buf[64];
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000920 time_t t = a.mtime;
Damien Millerfef95ad2006-07-10 20:46:55 +1000921
922 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
923 localtime(&t));
924 logit("set \"%s\" modtime %s", name, buf);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000925 r = utimes(name, attrib_to_tv(&a));
926 if (r == -1)
Damien Miller7b28dc52000-09-05 13:34:53 +1100927 status = errno_to_portable(errno);
928 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000929 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000930 logit("set \"%s\" owner %lu group %lu", name,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000931 (u_long)a.uid, (u_long)a.gid);
932 r = chown(name, a.uid, a.gid);
933 if (r == -1)
Kevin Steves8e743932001-02-05 13:24:35 +0000934 status = errno_to_portable(errno);
935 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100936 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000937 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100938}
939
Ben Lindstrombba81212001-06-25 05:01:22 +0000940static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100941process_fsetstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100942{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000943 Attrib a;
944 int handle, fd, r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000945 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000946
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000947 if ((r = get_handle(iqueue, &handle)) != 0 ||
948 (r = decode_attrib(iqueue, &a)) != 0)
949 fatal("%s: buffer error: %s", __func__, ssh_err(r));
950
951 debug("request %u: fsetstat handle %d", id, handle);
952 fd = handle_to_fd(handle);
953 if (fd < 0)
954 status = SSH2_FX_FAILURE;
955 else {
956 char *name = handle_to_name(handle);
957
958 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
959 logit("set \"%s\" size %llu",
960 name, (unsigned long long)a.size);
961 r = ftruncate(fd, a.size);
962 if (r == -1)
963 status = errno_to_portable(errno);
964 }
965 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
966 logit("set \"%s\" mode %04o", name, a.perm);
967#ifdef HAVE_FCHMOD
Damien Miller83b96782015-01-15 02:35:50 +1100968 r = fchmod(fd, a.perm & 07777);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000969#else
Damien Miller83b96782015-01-15 02:35:50 +1100970 r = chmod(name, a.perm & 07777);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000971#endif
972 if (r == -1)
973 status = errno_to_portable(errno);
974 }
975 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
976 char buf[64];
977 time_t t = a.mtime;
978
979 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
980 localtime(&t));
981 logit("set \"%s\" modtime %s", name, buf);
982#ifdef HAVE_FUTIMES
Damien Miller83b96782015-01-15 02:35:50 +1100983 r = futimes(fd, attrib_to_tv(&a));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000984#else
Damien Miller83b96782015-01-15 02:35:50 +1100985 r = utimes(name, attrib_to_tv(&a));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000986#endif
987 if (r == -1)
988 status = errno_to_portable(errno);
989 }
990 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
991 logit("set \"%s\" owner %lu group %lu", name,
992 (u_long)a.uid, (u_long)a.gid);
993#ifdef HAVE_FCHOWN
Damien Miller83b96782015-01-15 02:35:50 +1100994 r = fchown(fd, a.uid, a.gid);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000995#else
Damien Miller83b96782015-01-15 02:35:50 +1100996 r = chown(name, a.uid, a.gid);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000997#endif
998 if (r == -1)
999 status = errno_to_portable(errno);
1000 }
1001 }
1002 send_status(id, status);
1003}
Damien Miller7b28dc52000-09-05 13:34:53 +11001004
Ben Lindstrombba81212001-06-25 05:01:22 +00001005static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001006process_opendir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001007{
1008 DIR *dirp = NULL;
1009 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001010 int r, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001011
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001012 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1013 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1014
Damien Millerfef95ad2006-07-10 20:46:55 +10001015 debug3("request %u: opendir", id);
1016 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001017 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001018 if (dirp == NULL) {
1019 status = errno_to_portable(errno);
1020 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +11001021 handle = handle_new(HANDLE_DIR, path, 0, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +11001022 if (handle < 0) {
1023 closedir(dirp);
1024 } else {
1025 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001026 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001027 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001028
Damien Miller7b28dc52000-09-05 13:34:53 +11001029 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001030 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +11001031 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001032 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001033}
1034
Ben Lindstrombba81212001-06-25 05:01:22 +00001035static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001036process_readdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001037{
1038 DIR *dirp;
1039 struct dirent *dp;
1040 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001041 int r, handle;
Damien Miller7b28dc52000-09-05 13:34:53 +11001042
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001043 if ((r = get_handle(iqueue, &handle)) != 0)
1044 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1045
Damien Millerfef95ad2006-07-10 20:46:55 +10001046 debug("request %u: readdir \"%s\" (handle %d)", id,
1047 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +11001048 dirp = handle_to_dir(handle);
1049 path = handle_to_name(handle);
1050 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001051 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +11001052 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +11001053 struct stat st;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001054 char pathname[PATH_MAX];
Damien Miller7b28dc52000-09-05 13:34:53 +11001055 Stat *stats;
1056 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001057
Damien Miller07d86be2006-03-26 14:19:21 +11001058 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001059 while ((dp = readdir(dirp)) != NULL) {
1060 if (count >= nstats) {
1061 nstats *= 2;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001062 stats = xreallocarray(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001063 }
1064/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +00001065 snprintf(pathname, sizeof pathname, "%s%s%s", path,
1066 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001067 if (lstat(pathname, &st) < 0)
1068 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001069 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +11001070 stats[count].name = xstrdup(dp->d_name);
Darren Tucker2901e2d2010-01-13 22:44:06 +11001071 stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001072 count++;
1073 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001074 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +11001075 if (count == 100)
1076 break;
1077 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001078 if (count > 0) {
1079 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001080 for (i = 0; i < count; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001081 free(stats[i].name);
1082 free(stats[i].long_name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001083 }
1084 } else {
1085 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +11001086 }
Darren Tuckera627d422013-06-02 07:31:17 +10001087 free(stats);
Damien Miller7b28dc52000-09-05 13:34:53 +11001088 }
1089}
1090
Ben Lindstrombba81212001-06-25 05:01:22 +00001091static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001092process_remove(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001093{
1094 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001095 int r, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001096
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001097 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1098 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1099
Damien Millerfef95ad2006-07-10 20:46:55 +10001100 debug3("request %u: remove", id);
1101 logit("remove name \"%s\"", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001102 r = unlink(name);
1103 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001104 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001105 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001106}
1107
Ben Lindstrombba81212001-06-25 05:01:22 +00001108static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001109process_mkdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001110{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001111 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +11001112 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001113 int r, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001114
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001115 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
1116 (r = decode_attrib(iqueue, &a)) != 0)
1117 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1118
1119 mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
1120 a.perm & 07777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +10001121 debug3("request %u: mkdir", id);
1122 logit("mkdir name \"%s\" mode 0%o", name, mode);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001123 r = mkdir(name, mode);
1124 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001125 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001126 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001127}
1128
Ben Lindstrombba81212001-06-25 05:01:22 +00001129static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001130process_rmdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001131{
Damien Miller7b28dc52000-09-05 13:34:53 +11001132 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001133 int r, status;
Damien Miller7b28dc52000-09-05 13:34:53 +11001134
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001135 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1136 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1137
Damien Millerfef95ad2006-07-10 20:46:55 +10001138 debug3("request %u: rmdir", id);
1139 logit("rmdir name \"%s\"", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001140 r = rmdir(name);
1141 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001142 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001143 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001144}
1145
Ben Lindstrombba81212001-06-25 05:01:22 +00001146static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001147process_realpath(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001148{
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001149 char resolvedname[PATH_MAX];
Damien Miller7b28dc52000-09-05 13:34:53 +11001150 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001151 int r;
Damien Miller7b28dc52000-09-05 13:34:53 +11001152
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001153 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1154 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1155
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001156 if (path[0] == '\0') {
Darren Tuckera627d422013-06-02 07:31:17 +10001157 free(path);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001158 path = xstrdup(".");
1159 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001160 debug3("request %u: realpath", id);
1161 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001162 if (realpath(path, resolvedname) == NULL) {
1163 send_status(id, errno_to_portable(errno));
1164 } else {
1165 Stat s;
1166 attrib_clear(&s.attrib);
1167 s.name = s.long_name = resolvedname;
1168 send_names(id, 1, &s);
1169 }
Darren Tuckera627d422013-06-02 07:31:17 +10001170 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001171}
1172
Ben Lindstrombba81212001-06-25 05:01:22 +00001173static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001174process_rename(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001175{
Damien Miller7b28dc52000-09-05 13:34:53 +11001176 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001177 int r, status;
Damien Millerb3207e82003-03-26 16:01:11 +11001178 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +11001179
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001180 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1181 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1182 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1183
Damien Millerfef95ad2006-07-10 20:46:55 +10001184 debug3("request %u: rename", id);
1185 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +11001186 status = SSH2_FX_FAILURE;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001187 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +11001188 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +11001189 else if (S_ISREG(sb.st_mode)) {
1190 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001191 if (link(oldpath, newpath) == -1) {
Damien Miller0e265512009-08-28 10:43:13 +10001192 if (errno == EOPNOTSUPP || errno == ENOSYS
Darren Tuckerf7fa7062008-07-04 14:10:19 +10001193#ifdef EXDEV
1194 || errno == EXDEV
1195#endif
Darren Tuckere59b5082004-06-28 16:01:19 +10001196#ifdef LINK_OPNOTSUPP_ERRNO
1197 || errno == LINK_OPNOTSUPP_ERRNO
1198#endif
1199 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001200 struct stat st;
1201
1202 /*
1203 * fs doesn't support links, so fall back to
1204 * stat+rename. This is racy.
1205 */
1206 if (stat(newpath, &st) == -1) {
1207 if (rename(oldpath, newpath) == -1)
1208 status =
1209 errno_to_portable(errno);
1210 else
1211 status = SSH2_FX_OK;
1212 }
1213 } else {
1214 status = errno_to_portable(errno);
1215 }
1216 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001217 status = errno_to_portable(errno);
1218 /* clean spare link */
1219 unlink(newpath);
1220 } else
1221 status = SSH2_FX_OK;
1222 } else if (stat(newpath, &sb) == -1) {
1223 if (rename(oldpath, newpath) == -1)
1224 status = errno_to_portable(errno);
1225 else
1226 status = SSH2_FX_OK;
1227 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001228 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001229 free(oldpath);
1230 free(newpath);
Damien Miller7b28dc52000-09-05 13:34:53 +11001231}
1232
Ben Lindstrombba81212001-06-25 05:01:22 +00001233static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001234process_readlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001235{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001236 int r, len;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001237 char buf[PATH_MAX];
Damien Miller058316f2001-03-08 10:08:49 +11001238 char *path;
1239
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001240 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1241 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1242
Damien Millerfef95ad2006-07-10 20:46:55 +10001243 debug3("request %u: readlink", id);
1244 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001245 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001246 send_status(id, errno_to_portable(errno));
1247 else {
1248 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001249
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001250 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001251 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001252 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001253 send_names(id, 1, &s);
1254 }
Darren Tuckera627d422013-06-02 07:31:17 +10001255 free(path);
Damien Miller058316f2001-03-08 10:08:49 +11001256}
1257
Ben Lindstrombba81212001-06-25 05:01:22 +00001258static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001259process_symlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001260{
Damien Miller058316f2001-03-08 10:08:49 +11001261 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001262 int r, status;
Damien Miller058316f2001-03-08 10:08:49 +11001263
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001264 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1265 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1266 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1267
Damien Millerfef95ad2006-07-10 20:46:55 +10001268 debug3("request %u: symlink", id);
1269 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001270 /* this will fail if 'newpath' exists */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001271 r = symlink(oldpath, newpath);
1272 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001273 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001274 free(oldpath);
1275 free(newpath);
Damien Miller058316f2001-03-08 10:08:49 +11001276}
1277
Ben Lindstrombba81212001-06-25 05:01:22 +00001278static void
Damien Miller7c296612008-03-07 18:33:53 +11001279process_extended_posix_rename(u_int32_t id)
1280{
1281 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001282 int r, status;
Damien Miller7c296612008-03-07 18:33:53 +11001283
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001284 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1285 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1286 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1287
Damien Miller7c296612008-03-07 18:33:53 +11001288 debug3("request %u: posix-rename", id);
1289 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001290 r = rename(oldpath, newpath);
1291 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001292 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001293 free(oldpath);
1294 free(newpath);
Damien Miller7c296612008-03-07 18:33:53 +11001295}
1296
1297static void
Damien Millerd671e5a2008-05-19 14:53:33 +10001298process_extended_statvfs(u_int32_t id)
1299{
1300 char *path;
1301 struct statvfs st;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001302 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +10001303
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001304 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1305 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker2aca1592014-01-19 15:25:34 +11001306 debug3("request %u: statvfs", id);
1307 logit("statvfs \"%s\"", path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001308
1309 if (statvfs(path, &st) != 0)
1310 send_status(id, errno_to_portable(errno));
1311 else
1312 send_statvfs(id, &st);
Darren Tuckera627d422013-06-02 07:31:17 +10001313 free(path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001314}
1315
1316static void
1317process_extended_fstatvfs(u_int32_t id)
1318{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001319 int r, handle, fd;
Damien Millerd671e5a2008-05-19 14:53:33 +10001320 struct statvfs st;
1321
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001322 if ((r = get_handle(iqueue, &handle)) != 0)
1323 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +10001324 debug("request %u: fstatvfs \"%s\" (handle %u)",
1325 id, handle_to_name(handle), handle);
1326 if ((fd = handle_to_fd(handle)) < 0) {
1327 send_status(id, SSH2_FX_FAILURE);
1328 return;
1329 }
1330 if (fstatvfs(fd, &st) != 0)
1331 send_status(id, errno_to_portable(errno));
1332 else
1333 send_statvfs(id, &st);
1334}
1335
1336static void
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001337process_extended_hardlink(u_int32_t id)
1338{
1339 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001340 int r, status;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001341
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001342 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1343 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1344 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1345
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001346 debug3("request %u: hardlink", id);
1347 logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001348 r = link(oldpath, newpath);
1349 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001350 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001351 free(oldpath);
1352 free(newpath);
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001353}
1354
1355static void
Damien Millerf29238e2013-10-17 11:48:52 +11001356process_extended_fsync(u_int32_t id)
1357{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001358 int handle, fd, r, status = SSH2_FX_OP_UNSUPPORTED;
Damien Millerf29238e2013-10-17 11:48:52 +11001359
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001360 if ((r = get_handle(iqueue, &handle)) != 0)
1361 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf29238e2013-10-17 11:48:52 +11001362 debug3("request %u: fsync (handle %u)", id, handle);
1363 verbose("fsync \"%s\"", handle_to_name(handle));
1364 if ((fd = handle_to_fd(handle)) < 0)
1365 status = SSH2_FX_NO_SUCH_FILE;
1366 else if (handle_is_ok(handle, HANDLE_FILE)) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001367 r = fsync(fd);
1368 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Millerf29238e2013-10-17 11:48:52 +11001369 }
1370 send_status(id, status);
1371}
1372
1373static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001374process_extended(u_int32_t id)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001375{
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001376 char *request;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001377 int i, r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001378
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001379 if ((r = sshbuf_get_cstring(iqueue, &request, NULL)) != 0)
1380 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001381 for (i = 0; extended_handlers[i].handler != NULL; i++) {
1382 if (strcmp(request, extended_handlers[i].ext_name) == 0) {
1383 if (!request_permitted(&extended_handlers[i]))
1384 send_status(id, SSH2_FX_PERMISSION_DENIED);
1385 else
1386 extended_handlers[i].handler(id);
1387 break;
1388 }
1389 }
1390 if (extended_handlers[i].handler == NULL) {
1391 error("Unknown extended request \"%.100s\"", request);
Damien Miller7c296612008-03-07 18:33:53 +11001392 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001393 }
Darren Tuckera627d422013-06-02 07:31:17 +10001394 free(request);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001395}
Damien Miller7b28dc52000-09-05 13:34:53 +11001396
1397/* stolen from ssh-agent */
1398
Ben Lindstrombba81212001-06-25 05:01:22 +00001399static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001400process(void)
1401{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001402 u_int msg_len;
1403 u_int buf_len;
1404 u_int consumed;
1405 u_char type;
1406 const u_char *cp;
1407 int i, r;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001408 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +11001409
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001410 buf_len = sshbuf_len(iqueue);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001411 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001412 return; /* Incomplete message. */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001413 cp = sshbuf_ptr(iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001414 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001415 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001416 error("bad message from %s local user %s",
1417 client_addr, pw->pw_name);
Damien Millerdfc24252008-02-10 22:29:40 +11001418 sftp_server_cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001419 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001420 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001421 return;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001422 if ((r = sshbuf_consume(iqueue, 4)) != 0)
1423 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom2c140472002-06-06 21:57:54 +00001424 buf_len -= 4;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001425 if ((r = sshbuf_get_u8(iqueue, &type)) != 0)
1426 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001427
Damien Miller7b28dc52000-09-05 13:34:53 +11001428 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001429 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001430 process_init();
Damien Miller6eaeebf2013-10-15 11:55:57 +11001431 init_done = 1;
Damien Miller058316f2001-03-08 10:08:49 +11001432 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001433 case SSH2_FXP_EXTENDED:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001434 if (!init_done)
1435 fatal("Received extended request before init");
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001436 if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1437 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001438 process_extended(id);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001439 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001440 default:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001441 if (!init_done)
1442 fatal("Received %u request before init", type);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001443 if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1444 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001445 for (i = 0; handlers[i].handler != NULL; i++) {
1446 if (type == handlers[i].type) {
1447 if (!request_permitted(&handlers[i])) {
1448 send_status(id,
1449 SSH2_FX_PERMISSION_DENIED);
1450 } else {
1451 handlers[i].handler(id);
1452 }
1453 break;
1454 }
1455 }
1456 if (handlers[i].handler == NULL)
1457 error("Unknown message %u", type);
Damien Miller7b28dc52000-09-05 13:34:53 +11001458 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001459 /* discard the remaining bytes from the current packet */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001460 if (buf_len < sshbuf_len(iqueue)) {
Damien Millerdfc24252008-02-10 22:29:40 +11001461 error("iqueue grew unexpectedly");
1462 sftp_server_cleanup_exit(255);
1463 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001464 consumed = buf_len - sshbuf_len(iqueue);
Damien Millerdfc24252008-02-10 22:29:40 +11001465 if (msg_len < consumed) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001466 error("msg_len %u < consumed %u", msg_len, consumed);
Damien Millerdfc24252008-02-10 22:29:40 +11001467 sftp_server_cleanup_exit(255);
1468 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001469 if (msg_len > consumed &&
1470 (r = sshbuf_consume(iqueue, msg_len - consumed)) != 0)
1471 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001472}
1473
Damien Millerfef95ad2006-07-10 20:46:55 +10001474/* Cleanup handler that logs active handles upon normal exit */
1475void
Damien Millerdfc24252008-02-10 22:29:40 +11001476sftp_server_cleanup_exit(int i)
Damien Millerfef95ad2006-07-10 20:46:55 +10001477{
1478 if (pw != NULL && client_addr != NULL) {
1479 handle_log_exit();
1480 logit("session closed for local user %s from [%s]",
1481 pw->pw_name, client_addr);
1482 }
1483 _exit(i);
1484}
1485
1486static void
Damien Millerdfc24252008-02-10 22:29:40 +11001487sftp_server_usage(void)
Damien Millerfef95ad2006-07-10 20:46:55 +10001488{
1489 extern char *__progname;
1490
1491 fprintf(stderr,
Damien Milleraa7ad302013-01-09 15:58:21 +11001492 "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
Damien Miller6efab272013-10-15 12:07:05 +11001493 "[-l log_level]\n\t[-P blacklisted_requests] "
1494 "[-p whitelisted_requests] [-u umask]\n"
1495 " %s -Q protocol_feature\n",
1496 __progname, __progname);
Damien Millerfef95ad2006-07-10 20:46:55 +10001497 exit(1);
1498}
1499
Damien Miller7b28dc52000-09-05 13:34:53 +11001500int
Damien Millerd8cb1f12008-02-10 22:40:12 +11001501sftp_server_main(int argc, char **argv, struct passwd *user_pw)
Damien Miller7b28dc52000-09-05 13:34:53 +11001502{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001503 fd_set *rset, *wset;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001504 int i, r, in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001505 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001506 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
Damien Miller502ab0e2013-01-09 15:57:36 +11001507 char *cp, *homedir = NULL, buf[4*4096];
Damien Miller07331212010-11-05 10:20:31 +11001508 long mask;
Damien Millerfef95ad2006-07-10 20:46:55 +10001509
Damien Millerfef95ad2006-07-10 20:46:55 +10001510 extern char *optarg;
1511 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001512
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +00001513 ssh_malloc_init(); /* must be called before any mallocs */
Damien Millerfef95ad2006-07-10 20:46:55 +10001514 __progname = ssh_get_progname(argv[0]);
1515 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001516
Damien Miller502ab0e2013-01-09 15:57:36 +11001517 pw = pwcopy(user_pw);
1518
Damien Miller6eaeebf2013-10-15 11:55:57 +11001519 while (!skipargs && (ch = getopt(argc, argv,
1520 "d:f:l:P:p:Q:u:cehR")) != -1) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001521 switch (ch) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001522 case 'Q':
1523 if (strcasecmp(optarg, "requests") != 0) {
1524 fprintf(stderr, "Invalid query type\n");
1525 exit(1);
1526 }
1527 for (i = 0; handlers[i].handler != NULL; i++)
1528 printf("%s\n", handlers[i].name);
1529 for (i = 0; extended_handlers[i].handler != NULL; i++)
1530 printf("%s\n", extended_handlers[i].name);
1531 exit(0);
1532 break;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001533 case 'R':
1534 readonly = 1;
1535 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001536 case 'c':
1537 /*
1538 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001539 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001540 */
1541 skipargs = 1;
1542 break;
1543 case 'e':
1544 log_stderr = 1;
1545 break;
1546 case 'l':
1547 log_level = log_level_number(optarg);
1548 if (log_level == SYSLOG_LEVEL_NOT_SET)
1549 error("Invalid log level \"%s\"", optarg);
1550 break;
1551 case 'f':
1552 log_facility = log_facility_number(optarg);
Damien Miller35e18db2007-09-17 16:11:33 +10001553 if (log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Millerfef95ad2006-07-10 20:46:55 +10001554 error("Invalid log facility \"%s\"", optarg);
1555 break;
Damien Miller502ab0e2013-01-09 15:57:36 +11001556 case 'd':
1557 cp = tilde_expand_filename(optarg, user_pw->pw_uid);
1558 homedir = percent_expand(cp, "d", user_pw->pw_dir,
1559 "u", user_pw->pw_name, (char *)NULL);
1560 free(cp);
1561 break;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001562 case 'p':
1563 if (request_whitelist != NULL)
1564 fatal("Permitted requests already set");
1565 request_whitelist = xstrdup(optarg);
1566 break;
1567 case 'P':
1568 if (request_blacklist != NULL)
1569 fatal("Refused requests already set");
1570 request_blacklist = xstrdup(optarg);
1571 break;
Darren Tucker7dc48502009-10-07 08:44:42 +11001572 case 'u':
Damien Miller07331212010-11-05 10:20:31 +11001573 errno = 0;
1574 mask = strtol(optarg, &cp, 8);
1575 if (mask < 0 || mask > 0777 || *cp != '\0' ||
1576 cp == optarg || (mask == 0 && errno != 0))
1577 fatal("Invalid umask \"%s\"", optarg);
1578 (void)umask((mode_t)mask);
Darren Tucker7dc48502009-10-07 08:44:42 +11001579 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001580 case 'h':
1581 default:
Damien Millerdfc24252008-02-10 22:29:40 +11001582 sftp_server_usage();
Damien Millerfef95ad2006-07-10 20:46:55 +10001583 }
1584 }
1585
1586 log_init(__progname, log_level, log_facility, log_stderr);
1587
Damien Miller14928b72014-04-01 14:38:07 +11001588 /*
Darren Tucker0fb7f592016-06-09 16:23:07 +10001589 * On platforms where we can, avoid making /proc/self/{mem,maps}
Damien Miller14928b72014-04-01 14:38:07 +11001590 * available to the user so that sftp access doesn't automatically
1591 * imply arbitrary code execution access that will break
1592 * restricted configurations.
1593 */
Darren Tucker0fb7f592016-06-09 16:23:07 +10001594 platform_disable_tracing(1); /* strict */
Damien Miller14928b72014-04-01 14:38:07 +11001595
Damien Miller4626cba2016-01-08 14:24:56 +11001596 /* Drop any fine-grained privileges we don't need */
1597 platform_pledge_sftp_server();
1598
Damien Millerfef95ad2006-07-10 20:46:55 +10001599 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1600 client_addr = xstrdup(cp);
Damien Millerdfc24252008-02-10 22:29:40 +11001601 if ((cp = strchr(client_addr, ' ')) == NULL) {
1602 error("Malformed SSH_CONNECTION variable: \"%s\"",
Damien Millerfef95ad2006-07-10 20:46:55 +10001603 getenv("SSH_CONNECTION"));
Damien Millerdfc24252008-02-10 22:29:40 +11001604 sftp_server_cleanup_exit(255);
1605 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001606 *cp = '\0';
1607 } else
1608 client_addr = xstrdup("UNKNOWN");
1609
Damien Millerfef95ad2006-07-10 20:46:55 +10001610 logit("session opened for local user %s from [%s]",
1611 pw->pw_name, client_addr);
1612
Darren Tuckeraaf51d22010-01-08 19:04:49 +11001613 in = STDIN_FILENO;
1614 out = STDOUT_FILENO;
Damien Miller7b28dc52000-09-05 13:34:53 +11001615
Damien Miller402b3312001-04-14 00:28:42 +10001616#ifdef HAVE_CYGWIN
1617 setmode(in, O_BINARY);
1618 setmode(out, O_BINARY);
1619#endif
1620
Damien Miller7b28dc52000-09-05 13:34:53 +11001621 max = 0;
1622 if (in > max)
1623 max = in;
1624 if (out > max)
1625 max = out;
1626
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001627 if ((iqueue = sshbuf_new()) == NULL)
1628 fatal("%s: sshbuf_new failed", __func__);
1629 if ((oqueue = sshbuf_new()) == NULL)
1630 fatal("%s: sshbuf_new failed", __func__);
Damien Miller7b28dc52000-09-05 13:34:53 +11001631
logan@openbsd.orgdb6f8dc2015-11-16 06:13:04 +00001632 rset = xcalloc(howmany(max + 1, NFDBITS), sizeof(fd_mask));
1633 wset = xcalloc(howmany(max + 1, NFDBITS), sizeof(fd_mask));
Damien Miller7b28dc52000-09-05 13:34:53 +11001634
Damien Miller502ab0e2013-01-09 15:57:36 +11001635 if (homedir != NULL) {
1636 if (chdir(homedir) != 0) {
1637 error("chdir to \"%s\" failed: %s", homedir,
1638 strerror(errno));
1639 }
1640 }
1641
logan@openbsd.orgdb6f8dc2015-11-16 06:13:04 +00001642 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001643 for (;;) {
1644 memset(rset, 0, set_size);
1645 memset(wset, 0, set_size);
1646
Darren Tuckere9405982007-05-20 15:09:04 +10001647 /*
1648 * Ensure that we can read a full buffer and handle
1649 * the worst-case length packet it can generate,
1650 * otherwise apply backpressure by stopping reads.
1651 */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001652 if ((r = sshbuf_check_reserve(iqueue, sizeof(buf))) == 0 &&
1653 (r = sshbuf_check_reserve(oqueue,
1654 SFTP_MAX_MSG_LENGTH)) == 0)
Darren Tuckere9405982007-05-20 15:09:04 +10001655 FD_SET(in, rset);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001656 else if (r != SSH_ERR_NO_BUFFER_SPACE)
1657 fatal("%s: sshbuf_check_reserve failed: %s",
1658 __func__, ssh_err(r));
Darren Tuckere9405982007-05-20 15:09:04 +10001659
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001660 olen = sshbuf_len(oqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +11001661 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001662 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001663
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001664 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001665 if (errno == EINTR)
1666 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001667 error("select: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001668 sftp_server_cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001669 }
1670
1671 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001672 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001673 len = read(in, buf, sizeof buf);
1674 if (len == 0) {
1675 debug("read eof");
Damien Millerdfc24252008-02-10 22:29:40 +11001676 sftp_server_cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001677 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001678 error("read: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001679 sftp_server_cleanup_exit(1);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001680 } else if ((r = sshbuf_put(iqueue, buf, len)) != 0) {
1681 fatal("%s: buffer error: %s",
1682 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001683 }
1684 }
1685 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001686 if (FD_ISSET(out, wset)) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001687 len = write(out, sshbuf_ptr(oqueue), olen);
Damien Miller7b28dc52000-09-05 13:34:53 +11001688 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001689 error("write: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001690 sftp_server_cleanup_exit(1);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001691 } else if ((r = sshbuf_consume(oqueue, len)) != 0) {
1692 fatal("%s: buffer error: %s",
1693 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001694 }
1695 }
Darren Tuckere9405982007-05-20 15:09:04 +10001696
1697 /*
1698 * Process requests from client if we can fit the results
1699 * into the output buffer, otherwise stop processing input
1700 * and let the output queue drain.
1701 */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001702 r = sshbuf_check_reserve(oqueue, SFTP_MAX_MSG_LENGTH);
1703 if (r == 0)
Darren Tuckere9405982007-05-20 15:09:04 +10001704 process();
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001705 else if (r != SSH_ERR_NO_BUFFER_SPACE)
1706 fatal("%s: sshbuf_check_reserve: %s",
1707 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001708 }
1709}