blob: 4f735cd9397c37d76b3bd2cc019aacbe9db976c5 [file] [log] [blame]
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001/* $OpenBSD: sftp-server.c,v 1.105 2015/01/20 23:14:00 deraadt 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 Miller14928b72014-04-01 14:38:07 +110032#ifdef HAVE_SYS_PRCTL_H
33#include <sys/prctl.h>
34#endif
Damien Miller88f254b2006-03-15 11:25:13 +110035
36#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100037#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100038#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100039#include <pwd.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100040#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100041#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100042#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100043#include <pwd.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100044#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100046#include <stdarg.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110047
Damien Miller7b28dc52000-09-05 13:34:53 +110048#include "xmalloc.h"
djm@openbsd.org7d845f42015-01-14 13:54:13 +000049#include "sshbuf.h"
50#include "ssherr.h"
Damien Millerd7834352006-08-05 12:39:39 +100051#include "log.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100052#include "misc.h"
Damien Miller6eaeebf2013-10-15 11:55:57 +110053#include "match.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100054#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110055
Ben Lindstrom2f959b42001-01-11 06:20:23 +000056#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110057#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110058
Damien Millerfef95ad2006-07-10 20:46:55 +100059/* Our verbosity */
Damien Miller6eaeebf2013-10-15 11:55:57 +110060static LogLevel log_level = SYSLOG_LEVEL_ERROR;
Damien Millerfef95ad2006-07-10 20:46:55 +100061
62/* Our client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110063static struct passwd *pw = NULL;
64static char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000065
Damien Miller7b28dc52000-09-05 13:34:53 +110066/* input and output queue */
djm@openbsd.org7d845f42015-01-14 13:54:13 +000067struct sshbuf *iqueue;
68struct sshbuf *oqueue;
Damien Miller7b28dc52000-09-05 13:34:53 +110069
Damien Miller058316f2001-03-08 10:08:49 +110070/* Version of client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110071static u_int version;
72
73/* SSH2_FXP_INIT received */
74static int init_done;
Damien Miller058316f2001-03-08 10:08:49 +110075
Darren Tuckerdb7bf822010-01-09 22:24:33 +110076/* Disable writes */
Damien Miller6eaeebf2013-10-15 11:55:57 +110077static int readonly;
78
79/* Requests that are allowed/denied */
80static char *request_whitelist, *request_blacklist;
Darren Tuckerdb7bf822010-01-09 22:24:33 +110081
Darren Tuckera6612d42003-06-28 12:39:03 +100082/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110083typedef struct Stat Stat;
84
Damien Miller33804262001-02-04 23:20:18 +110085struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110086 char *name;
87 char *long_name;
88 Attrib attrib;
89};
90
Damien Miller6eaeebf2013-10-15 11:55:57 +110091/* Packet handlers */
92static void process_open(u_int32_t id);
93static void process_close(u_int32_t id);
94static void process_read(u_int32_t id);
95static void process_write(u_int32_t id);
96static void process_stat(u_int32_t id);
97static void process_lstat(u_int32_t id);
98static void process_fstat(u_int32_t id);
99static void process_setstat(u_int32_t id);
100static void process_fsetstat(u_int32_t id);
101static void process_opendir(u_int32_t id);
102static void process_readdir(u_int32_t id);
103static void process_remove(u_int32_t id);
104static void process_mkdir(u_int32_t id);
105static void process_rmdir(u_int32_t id);
106static void process_realpath(u_int32_t id);
107static void process_rename(u_int32_t id);
108static void process_readlink(u_int32_t id);
109static void process_symlink(u_int32_t id);
110static void process_extended_posix_rename(u_int32_t id);
111static void process_extended_statvfs(u_int32_t id);
112static void process_extended_fstatvfs(u_int32_t id);
113static void process_extended_hardlink(u_int32_t id);
Damien Millerf29238e2013-10-17 11:48:52 +1100114static void process_extended_fsync(u_int32_t id);
Damien Miller6eaeebf2013-10-15 11:55:57 +1100115static void process_extended(u_int32_t id);
116
117struct sftp_handler {
118 const char *name; /* user-visible name for fine-grained perms */
119 const char *ext_name; /* extended request name */
120 u_int type; /* packet type, for non extended packets */
121 void (*handler)(u_int32_t);
122 int does_write; /* if nonzero, banned for readonly mode */
123};
124
125struct sftp_handler handlers[] = {
126 /* NB. SSH2_FXP_OPEN does the readonly check in the handler itself */
127 { "open", NULL, SSH2_FXP_OPEN, process_open, 0 },
128 { "close", NULL, SSH2_FXP_CLOSE, process_close, 0 },
129 { "read", NULL, SSH2_FXP_READ, process_read, 0 },
130 { "write", NULL, SSH2_FXP_WRITE, process_write, 1 },
131 { "lstat", NULL, SSH2_FXP_LSTAT, process_lstat, 0 },
132 { "fstat", NULL, SSH2_FXP_FSTAT, process_fstat, 0 },
133 { "setstat", NULL, SSH2_FXP_SETSTAT, process_setstat, 1 },
134 { "fsetstat", NULL, SSH2_FXP_FSETSTAT, process_fsetstat, 1 },
135 { "opendir", NULL, SSH2_FXP_OPENDIR, process_opendir, 0 },
136 { "readdir", NULL, SSH2_FXP_READDIR, process_readdir, 0 },
137 { "remove", NULL, SSH2_FXP_REMOVE, process_remove, 1 },
138 { "mkdir", NULL, SSH2_FXP_MKDIR, process_mkdir, 1 },
139 { "rmdir", NULL, SSH2_FXP_RMDIR, process_rmdir, 1 },
140 { "realpath", NULL, SSH2_FXP_REALPATH, process_realpath, 0 },
141 { "stat", NULL, SSH2_FXP_STAT, process_stat, 0 },
142 { "rename", NULL, SSH2_FXP_RENAME, process_rename, 1 },
143 { "readlink", NULL, SSH2_FXP_READLINK, process_readlink, 0 },
144 { "symlink", NULL, SSH2_FXP_SYMLINK, process_symlink, 1 },
145 { NULL, NULL, 0, NULL, 0 }
146};
147
148/* SSH2_FXP_EXTENDED submessages */
149struct sftp_handler extended_handlers[] = {
150 { "posix-rename", "posix-rename@openssh.com", 0,
151 process_extended_posix_rename, 1 },
152 { "statvfs", "statvfs@openssh.com", 0, process_extended_statvfs, 0 },
153 { "fstatvfs", "fstatvfs@openssh.com", 0, process_extended_fstatvfs, 0 },
154 { "hardlink", "hardlink@openssh.com", 0, process_extended_hardlink, 1 },
Damien Millerf29238e2013-10-17 11:48:52 +1100155 { "fsync", "fsync@openssh.com", 0, process_extended_fsync, 1 },
Damien Miller6eaeebf2013-10-15 11:55:57 +1100156 { NULL, NULL, 0, NULL, 0 }
157};
158
159static int
160request_permitted(struct sftp_handler *h)
161{
162 char *result;
163
164 if (readonly && h->does_write) {
165 verbose("Refusing %s request in read-only mode", h->name);
166 return 0;
167 }
168 if (request_blacklist != NULL &&
169 ((result = match_list(h->name, request_blacklist, NULL))) != NULL) {
170 free(result);
171 verbose("Refusing blacklisted %s request", h->name);
172 return 0;
173 }
174 if (request_whitelist != NULL &&
175 ((result = match_list(h->name, request_whitelist, NULL))) != NULL) {
176 free(result);
177 debug2("Permitting whitelisted %s request", h->name);
178 return 1;
179 }
180 if (request_whitelist != NULL) {
181 verbose("Refusing non-whitelisted %s request", h->name);
182 return 0;
183 }
184 return 1;
185}
186
Ben Lindstrombba81212001-06-25 05:01:22 +0000187static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100188errno_to_portable(int unixerrno)
189{
190 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000191
Damien Miller7b28dc52000-09-05 13:34:53 +1100192 switch (unixerrno) {
193 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000194 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100195 break;
196 case ENOENT:
197 case ENOTDIR:
198 case EBADF:
199 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000200 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100201 break;
202 case EPERM:
203 case EACCES:
204 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000205 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +1100206 break;
207 case ENAMETOOLONG:
208 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000209 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100210 break;
Darren Tucker422c34c2008-06-09 22:48:31 +1000211 case ENOSYS:
212 ret = SSH2_FX_OP_UNSUPPORTED;
213 break;
Damien Miller7b28dc52000-09-05 13:34:53 +1100214 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000215 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100216 break;
217 }
218 return ret;
219}
220
Ben Lindstrombba81212001-06-25 05:01:22 +0000221static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100222flags_from_portable(int pflags)
223{
224 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000225
Ben Lindstrom36592512001-03-05 05:02:08 +0000226 if ((pflags & SSH2_FXF_READ) &&
227 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100228 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000229 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100230 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000231 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100232 flags = O_WRONLY;
233 }
Damien Millere9fc72e2013-10-15 12:14:12 +1100234 if (pflags & SSH2_FXF_APPEND)
235 flags |= O_APPEND;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000236 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100237 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000238 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100239 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000240 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100241 flags |= O_EXCL;
242 return flags;
243}
244
Damien Millerfef95ad2006-07-10 20:46:55 +1000245static const char *
246string_from_portable(int pflags)
247{
248 static char ret[128];
249
250 *ret = '\0';
251
252#define PAPPEND(str) { \
253 if (*ret != '\0') \
254 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000255 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000256 }
257
258 if (pflags & SSH2_FXF_READ)
259 PAPPEND("READ")
260 if (pflags & SSH2_FXF_WRITE)
261 PAPPEND("WRITE")
Damien Millere9fc72e2013-10-15 12:14:12 +1100262 if (pflags & SSH2_FXF_APPEND)
263 PAPPEND("APPEND")
Damien Millerfef95ad2006-07-10 20:46:55 +1000264 if (pflags & SSH2_FXF_CREAT)
265 PAPPEND("CREATE")
266 if (pflags & SSH2_FXF_TRUNC)
267 PAPPEND("TRUNCATE")
268 if (pflags & SSH2_FXF_EXCL)
269 PAPPEND("EXCL")
270
271 return ret;
272}
273
Damien Miller7b28dc52000-09-05 13:34:53 +1100274/* handle handles */
275
276typedef struct Handle Handle;
277struct Handle {
278 int use;
279 DIR *dirp;
280 int fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100281 int flags;
Damien Miller7b28dc52000-09-05 13:34:53 +1100282 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000283 u_int64_t bytes_read, bytes_write;
Damien Miller3397d0e2008-02-10 22:26:51 +1100284 int next_unused;
Damien Miller7b28dc52000-09-05 13:34:53 +1100285};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000286
Damien Miller7b28dc52000-09-05 13:34:53 +1100287enum {
288 HANDLE_UNUSED,
289 HANDLE_DIR,
290 HANDLE_FILE
291};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000292
Damien Miller3397d0e2008-02-10 22:26:51 +1100293Handle *handles = NULL;
294u_int num_handles = 0;
295int first_unused_handle = -1;
Damien Miller7b28dc52000-09-05 13:34:53 +1100296
Damien Miller3397d0e2008-02-10 22:26:51 +1100297static void handle_unused(int i)
Damien Miller7b28dc52000-09-05 13:34:53 +1100298{
Damien Miller3397d0e2008-02-10 22:26:51 +1100299 handles[i].use = HANDLE_UNUSED;
300 handles[i].next_unused = first_unused_handle;
301 first_unused_handle = i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100302}
303
Ben Lindstrombba81212001-06-25 05:01:22 +0000304static int
Damien Millere9fc72e2013-10-15 12:14:12 +1100305handle_new(int use, const char *name, int fd, int flags, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100306{
Damien Miller3397d0e2008-02-10 22:26:51 +1100307 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000308
Damien Miller3397d0e2008-02-10 22:26:51 +1100309 if (first_unused_handle == -1) {
310 if (num_handles + 1 <= num_handles)
311 return -1;
312 num_handles++;
313 handles = xrealloc(handles, num_handles, sizeof(Handle));
314 handle_unused(num_handles - 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100315 }
Damien Miller3397d0e2008-02-10 22:26:51 +1100316
317 i = first_unused_handle;
318 first_unused_handle = handles[i].next_unused;
319
320 handles[i].use = use;
321 handles[i].dirp = dirp;
322 handles[i].fd = fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100323 handles[i].flags = flags;
Damien Miller3397d0e2008-02-10 22:26:51 +1100324 handles[i].name = xstrdup(name);
325 handles[i].bytes_read = handles[i].bytes_write = 0;
326
327 return i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100328}
329
Ben Lindstrombba81212001-06-25 05:01:22 +0000330static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100331handle_is_ok(int i, int type)
332{
Damien Miller3397d0e2008-02-10 22:26:51 +1100333 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100334}
335
Ben Lindstrombba81212001-06-25 05:01:22 +0000336static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000337handle_to_string(int handle, u_char **stringp, int *hlenp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100338{
Damien Miller7b28dc52000-09-05 13:34:53 +1100339 if (stringp == NULL || hlenp == NULL)
340 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000341 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100342 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000343 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100344 return 0;
345}
346
Ben Lindstrombba81212001-06-25 05:01:22 +0000347static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000348handle_from_string(const u_char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100349{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000350 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000351
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000352 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100353 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100354 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100355 if (handle_is_ok(val, HANDLE_FILE) ||
356 handle_is_ok(val, HANDLE_DIR))
357 return val;
358 return -1;
359}
360
Ben Lindstrombba81212001-06-25 05:01:22 +0000361static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100362handle_to_name(int handle)
363{
364 if (handle_is_ok(handle, HANDLE_DIR)||
365 handle_is_ok(handle, HANDLE_FILE))
366 return handles[handle].name;
367 return NULL;
368}
369
Ben Lindstrombba81212001-06-25 05:01:22 +0000370static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100371handle_to_dir(int handle)
372{
373 if (handle_is_ok(handle, HANDLE_DIR))
374 return handles[handle].dirp;
375 return NULL;
376}
377
Ben Lindstrombba81212001-06-25 05:01:22 +0000378static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100379handle_to_fd(int handle)
380{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000381 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100382 return handles[handle].fd;
383 return -1;
384}
385
Damien Millere9fc72e2013-10-15 12:14:12 +1100386static int
387handle_to_flags(int handle)
388{
389 if (handle_is_ok(handle, HANDLE_FILE))
390 return handles[handle].flags;
391 return 0;
392}
393
Damien Millerfef95ad2006-07-10 20:46:55 +1000394static void
395handle_update_read(int handle, ssize_t bytes)
396{
397 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
398 handles[handle].bytes_read += bytes;
399}
400
401static void
402handle_update_write(int handle, ssize_t bytes)
403{
404 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
405 handles[handle].bytes_write += bytes;
406}
407
408static u_int64_t
409handle_bytes_read(int handle)
410{
411 if (handle_is_ok(handle, HANDLE_FILE))
412 return (handles[handle].bytes_read);
413 return 0;
414}
415
416static u_int64_t
417handle_bytes_write(int handle)
418{
419 if (handle_is_ok(handle, HANDLE_FILE))
420 return (handles[handle].bytes_write);
421 return 0;
422}
423
Ben Lindstrombba81212001-06-25 05:01:22 +0000424static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100425handle_close(int handle)
426{
427 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000428
Damien Miller7b28dc52000-09-05 13:34:53 +1100429 if (handle_is_ok(handle, HANDLE_FILE)) {
430 ret = close(handles[handle].fd);
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 if (handle_is_ok(handle, HANDLE_DIR)) {
434 ret = closedir(handles[handle].dirp);
Darren Tuckera627d422013-06-02 07:31:17 +1000435 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100436 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100437 } else {
438 errno = ENOENT;
439 }
440 return ret;
441}
442
Damien Millerfef95ad2006-07-10 20:46:55 +1000443static void
444handle_log_close(int handle, char *emsg)
445{
446 if (handle_is_ok(handle, HANDLE_FILE)) {
447 logit("%s%sclose \"%s\" bytes read %llu written %llu",
448 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
449 handle_to_name(handle),
Darren Tucker86473c52007-05-20 14:59:32 +1000450 (unsigned long long)handle_bytes_read(handle),
451 (unsigned long long)handle_bytes_write(handle));
Damien Millerfef95ad2006-07-10 20:46:55 +1000452 } else {
453 logit("%s%sclosedir \"%s\"",
454 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
455 handle_to_name(handle));
456 }
457}
458
459static void
460handle_log_exit(void)
461{
462 u_int i;
463
Damien Miller3397d0e2008-02-10 22:26:51 +1100464 for (i = 0; i < num_handles; i++)
Damien Millerfef95ad2006-07-10 20:46:55 +1000465 if (handles[i].use != HANDLE_UNUSED)
466 handle_log_close(i, "forced");
467}
468
Ben Lindstrombba81212001-06-25 05:01:22 +0000469static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000470get_handle(struct sshbuf *queue, int *hp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100471{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000472 u_char *handle;
473 int r;
474 size_t hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000475
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000476 *hp = -1;
477 if ((r = sshbuf_get_string(queue, &handle, &hlen)) != 0)
478 return r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000479 if (hlen < 256)
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000480 *hp = handle_from_string(handle, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000481 free(handle);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000482 return 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100483}
484
485/* send replies */
486
Ben Lindstrombba81212001-06-25 05:01:22 +0000487static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000488send_msg(struct sshbuf *m)
Damien Miller7b28dc52000-09-05 13:34:53 +1100489{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000490 int r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000491
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000492 if ((r = sshbuf_put_stringb(oqueue, m)) != 0)
493 fatal("%s: buffer error: %s", __func__, ssh_err(r));
494 sshbuf_reset(m);
Damien Miller7b28dc52000-09-05 13:34:53 +1100495}
496
Damien Millerfef95ad2006-07-10 20:46:55 +1000497static const char *
498status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100499{
Damien Miller058316f2001-03-08 10:08:49 +1100500 const char *status_messages[] = {
501 "Success", /* SSH_FX_OK */
502 "End of file", /* SSH_FX_EOF */
503 "No such file", /* SSH_FX_NO_SUCH_FILE */
504 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
505 "Failure", /* SSH_FX_FAILURE */
506 "Bad message", /* SSH_FX_BAD_MESSAGE */
507 "No connection", /* SSH_FX_NO_CONNECTION */
508 "Connection lost", /* SSH_FX_CONNECTION_LOST */
509 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
510 "Unknown error" /* Others */
511 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000512 return (status_messages[MIN(status,SSH2_FX_MAX)]);
513}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000514
Damien Millerfef95ad2006-07-10 20:46:55 +1000515static void
516send_status(u_int32_t id, u_int32_t status)
517{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000518 struct sshbuf *msg;
519 int r;
Damien Millerfef95ad2006-07-10 20:46:55 +1000520
521 debug3("request %u: sent status %u", id, status);
522 if (log_level > SYSLOG_LEVEL_VERBOSE ||
523 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
524 logit("sent status %s", status_to_message(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000525 if ((msg = sshbuf_new()) == NULL)
526 fatal("%s: sshbuf_new failed", __func__);
527 if ((r = sshbuf_put_u8(msg, SSH2_FXP_STATUS)) != 0 ||
528 (r = sshbuf_put_u32(msg, id)) != 0 ||
529 (r = sshbuf_put_u32(msg, status)) != 0)
530 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +1100531 if (version >= 3) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000532 if ((r = sshbuf_put_cstring(msg,
533 status_to_message(status))) != 0 ||
534 (r = sshbuf_put_cstring(msg, "")) != 0)
535 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +1100536 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000537 send_msg(msg);
538 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100539}
Ben Lindstrombba81212001-06-25 05:01:22 +0000540static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000541send_data_or_handle(char type, u_int32_t id, const u_char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100542{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000543 struct sshbuf *msg;
544 int r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000545
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000546 if ((msg = sshbuf_new()) == NULL)
547 fatal("%s: sshbuf_new failed", __func__);
548 if ((r = sshbuf_put_u8(msg, type)) != 0 ||
549 (r = sshbuf_put_u32(msg, id)) != 0 ||
550 (r = sshbuf_put_string(msg, data, dlen)) != 0)
551 fatal("%s: buffer error: %s", __func__, ssh_err(r));
552 send_msg(msg);
553 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100554}
555
Ben Lindstrombba81212001-06-25 05:01:22 +0000556static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000557send_data(u_int32_t id, const u_char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100558{
Damien Millerfef95ad2006-07-10 20:46:55 +1000559 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000560 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100561}
562
Ben Lindstrombba81212001-06-25 05:01:22 +0000563static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100564send_handle(u_int32_t id, int handle)
565{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000566 u_char *string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100567 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000568
Damien Miller7b28dc52000-09-05 13:34:53 +1100569 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000570 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000571 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000572 free(string);
Damien Miller7b28dc52000-09-05 13:34:53 +1100573}
574
Ben Lindstrombba81212001-06-25 05:01:22 +0000575static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100576send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100577{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000578 struct sshbuf *msg;
579 int i, r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000580
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000581 if ((msg = sshbuf_new()) == NULL)
582 fatal("%s: sshbuf_new failed", __func__);
583 if ((r = sshbuf_put_u8(msg, SSH2_FXP_NAME)) != 0 ||
584 (r = sshbuf_put_u32(msg, id)) != 0 ||
585 (r = sshbuf_put_u32(msg, count)) != 0)
586 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerfef95ad2006-07-10 20:46:55 +1000587 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100588 for (i = 0; i < count; i++) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000589 if ((r = sshbuf_put_cstring(msg, stats[i].name)) != 0 ||
590 (r = sshbuf_put_cstring(msg, stats[i].long_name)) != 0 ||
591 (r = encode_attrib(msg, &stats[i].attrib)) != 0)
592 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100593 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000594 send_msg(msg);
595 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100596}
597
Ben Lindstrombba81212001-06-25 05:01:22 +0000598static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100599send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100600{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000601 struct sshbuf *msg;
602 int r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000603
Damien Millerfef95ad2006-07-10 20:46:55 +1000604 debug("request %u: sent attrib have 0x%x", id, a->flags);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000605 if ((msg = sshbuf_new()) == NULL)
606 fatal("%s: sshbuf_new failed", __func__);
607 if ((r = sshbuf_put_u8(msg, SSH2_FXP_ATTRS)) != 0 ||
608 (r = sshbuf_put_u32(msg, id)) != 0 ||
609 (r = encode_attrib(msg, a)) != 0)
610 fatal("%s: buffer error: %s", __func__, ssh_err(r));
611 send_msg(msg);
612 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100613}
614
Damien Millerd671e5a2008-05-19 14:53:33 +1000615static void
616send_statvfs(u_int32_t id, struct statvfs *st)
617{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000618 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +1000619 u_int64_t flag;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000620 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +1000621
622 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
623 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
624
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000625 if ((msg = sshbuf_new()) == NULL)
626 fatal("%s: sshbuf_new failed", __func__);
627 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED_REPLY)) != 0 ||
628 (r = sshbuf_put_u32(msg, id)) != 0 ||
629 (r = sshbuf_put_u64(msg, st->f_bsize)) != 0 ||
630 (r = sshbuf_put_u64(msg, st->f_frsize)) != 0 ||
631 (r = sshbuf_put_u64(msg, st->f_blocks)) != 0 ||
632 (r = sshbuf_put_u64(msg, st->f_bfree)) != 0 ||
633 (r = sshbuf_put_u64(msg, st->f_bavail)) != 0 ||
634 (r = sshbuf_put_u64(msg, st->f_files)) != 0 ||
635 (r = sshbuf_put_u64(msg, st->f_ffree)) != 0 ||
636 (r = sshbuf_put_u64(msg, st->f_favail)) != 0 ||
637 (r = sshbuf_put_u64(msg, FSID_TO_ULONG(st->f_fsid))) != 0 ||
638 (r = sshbuf_put_u64(msg, flag)) != 0 ||
639 (r = sshbuf_put_u64(msg, st->f_namemax)) != 0)
640 fatal("%s: buffer error: %s", __func__, ssh_err(r));
641 send_msg(msg);
642 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000643}
644
Damien Miller7b28dc52000-09-05 13:34:53 +1100645/* parse incoming */
646
Ben Lindstrombba81212001-06-25 05:01:22 +0000647static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100648process_init(void)
649{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000650 struct sshbuf *msg;
651 int r;
Damien Miller7b28dc52000-09-05 13:34:53 +1100652
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000653 if ((r = sshbuf_get_u32(iqueue, &version)) != 0)
654 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf145a5b2011-06-20 14:42:51 +1000655 verbose("received client version %u", version);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000656 if ((msg = sshbuf_new()) == NULL)
657 fatal("%s: sshbuf_new failed", __func__);
658 if ((r = sshbuf_put_u8(msg, SSH2_FXP_VERSION)) != 0 ||
659 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0 ||
660 /* POSIX rename extension */
661 (r = sshbuf_put_cstring(msg, "posix-rename@openssh.com")) != 0 ||
662 (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
663 /* statvfs extension */
664 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
665 (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
666 /* fstatvfs extension */
667 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
668 (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
669 /* hardlink extension */
670 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
671 (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
672 /* fsync extension */
673 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
674 (r = sshbuf_put_cstring(msg, "1")) != 0) /* version */
675 fatal("%s: buffer error: %s", __func__, ssh_err(r));
676 send_msg(msg);
677 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100678}
679
Ben Lindstrombba81212001-06-25 05:01:22 +0000680static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100681process_open(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100682{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100683 u_int32_t pflags;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000684 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100685 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000686 int r, handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100687
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000688 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
689 (r = sshbuf_get_u32(iqueue, &pflags)) != 0 || /* portable flags */
690 (r = decode_attrib(iqueue, &a)) != 0)
691 fatal("%s: buffer error: %s", __func__, ssh_err(r));
692
Damien Miller6444fe92006-07-10 21:31:27 +1000693 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100694 flags = flags_from_portable(pflags);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000695 mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a.perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000696 logit("open \"%s\" flags %s mode 0%o",
697 name, string_from_portable(pflags), mode);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100698 if (readonly &&
Damien Miller6eaeebf2013-10-15 11:55:57 +1100699 ((flags & O_ACCMODE) == O_WRONLY ||
700 (flags & O_ACCMODE) == O_RDWR)) {
701 verbose("Refusing open request in read-only mode");
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000702 status = SSH2_FX_PERMISSION_DENIED;
Damien Miller6eaeebf2013-10-15 11:55:57 +1100703 } else {
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100704 fd = open(name, flags, mode);
705 if (fd < 0) {
706 status = errno_to_portable(errno);
Damien Miller7b28dc52000-09-05 13:34:53 +1100707 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100708 handle = handle_new(HANDLE_FILE, name, fd, flags, NULL);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100709 if (handle < 0) {
710 close(fd);
711 } else {
712 send_handle(id, handle);
713 status = SSH2_FX_OK;
714 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100715 }
716 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000717 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100718 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000719 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100720}
721
Ben Lindstrombba81212001-06-25 05:01:22 +0000722static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100723process_close(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100724{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000725 int r, handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100726
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000727 if ((r = get_handle(iqueue, &handle)) != 0)
728 fatal("%s: buffer error: %s", __func__, ssh_err(r));
729
Damien Millerfef95ad2006-07-10 20:46:55 +1000730 debug3("request %u: close handle %u", id, handle);
731 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100732 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000733 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100734 send_status(id, status);
735}
736
Ben Lindstrombba81212001-06-25 05:01:22 +0000737static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100738process_read(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100739{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000740 u_char buf[64*1024];
Damien Miller6eaeebf2013-10-15 11:55:57 +1100741 u_int32_t len;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000742 int r, handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100743 u_int64_t off;
744
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000745 if ((r = get_handle(iqueue, &handle)) != 0 ||
746 (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
747 (r = sshbuf_get_u32(iqueue, &len)) != 0)
748 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100749
Damien Millerfef95ad2006-07-10 20:46:55 +1000750 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
751 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100752 if (len > sizeof buf) {
753 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000754 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100755 }
756 fd = handle_to_fd(handle);
757 if (fd >= 0) {
758 if (lseek(fd, off, SEEK_SET) < 0) {
759 error("process_read: seek failed");
760 status = errno_to_portable(errno);
761 } else {
762 ret = read(fd, buf, len);
763 if (ret < 0) {
764 status = errno_to_portable(errno);
765 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000766 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100767 } else {
768 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000769 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000770 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100771 }
772 }
773 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000774 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100775 send_status(id, status);
776}
777
Ben Lindstrombba81212001-06-25 05:01:22 +0000778static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100779process_write(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100780{
Damien Miller7b28dc52000-09-05 13:34:53 +1100781 u_int64_t off;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000782 size_t len;
783 int r, handle, fd, ret, status;
784 u_char *data;
Damien Miller7b28dc52000-09-05 13:34:53 +1100785
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000786 if ((r = get_handle(iqueue, &handle)) != 0 ||
787 (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
788 (r = sshbuf_get_string(iqueue, &data, &len)) != 0)
789 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100790
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000791 debug("request %u: write \"%s\" (handle %d) off %llu len %zu",
Damien Millerfef95ad2006-07-10 20:46:55 +1000792 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100793 fd = handle_to_fd(handle);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000794
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100795 if (fd < 0)
796 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100797 else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100798 if (!(handle_to_flags(handle) & O_APPEND) &&
799 lseek(fd, off, SEEK_SET) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100800 status = errno_to_portable(errno);
801 error("process_write: seek failed");
802 } else {
803/* XXX ATOMICIO ? */
804 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000805 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100806 error("process_write: write failed");
807 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000808 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000809 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000810 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100811 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000812 debug2("nothing at all written");
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100813 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100814 }
815 }
816 }
817 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000818 free(data);
Damien Miller7b28dc52000-09-05 13:34:53 +1100819}
820
Ben Lindstrombba81212001-06-25 05:01:22 +0000821static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100822process_do_stat(u_int32_t id, int do_lstat)
Damien Miller7b28dc52000-09-05 13:34:53 +1100823{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000824 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100825 struct stat st;
Damien Miller7b28dc52000-09-05 13:34:53 +1100826 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000827 int r, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100828
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000829 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
830 fatal("%s: buffer error: %s", __func__, ssh_err(r));
831
Damien Millerfef95ad2006-07-10 20:46:55 +1000832 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
833 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000834 r = do_lstat ? lstat(name, &st) : stat(name, &st);
835 if (r < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100836 status = errno_to_portable(errno);
837 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000838 stat_to_attrib(&st, &a);
839 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000840 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100841 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000842 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100843 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000844 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100845}
846
Ben Lindstrombba81212001-06-25 05:01:22 +0000847static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100848process_stat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100849{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100850 process_do_stat(id, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100851}
852
Ben Lindstrombba81212001-06-25 05:01:22 +0000853static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100854process_lstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100855{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100856 process_do_stat(id, 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100857}
858
Ben Lindstrombba81212001-06-25 05:01:22 +0000859static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100860process_fstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100861{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000862 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100863 struct stat st;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000864 int fd, r, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100865
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000866 if ((r = get_handle(iqueue, &handle)) != 0)
867 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerfef95ad2006-07-10 20:46:55 +1000868 debug("request %u: fstat \"%s\" (handle %u)",
869 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100870 fd = handle_to_fd(handle);
Damien Millere2334d62007-01-05 16:31:02 +1100871 if (fd >= 0) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000872 r = fstat(fd, &st);
873 if (r < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100874 status = errno_to_portable(errno);
875 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000876 stat_to_attrib(&st, &a);
877 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000878 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100879 }
880 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000881 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100882 send_status(id, status);
883}
884
Ben Lindstrombba81212001-06-25 05:01:22 +0000885static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100886attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100887{
888 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000889
Damien Miller7b28dc52000-09-05 13:34:53 +1100890 tv[0].tv_sec = a->atime;
891 tv[0].tv_usec = 0;
892 tv[1].tv_sec = a->mtime;
893 tv[1].tv_usec = 0;
894 return tv;
895}
896
Ben Lindstrombba81212001-06-25 05:01:22 +0000897static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100898process_setstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100899{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000900 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100901 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000902 int r, status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100903
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000904 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
905 (r = decode_attrib(iqueue, &a)) != 0)
906 fatal("%s: buffer error: %s", __func__, ssh_err(r));
907
Damien Millerfef95ad2006-07-10 20:46:55 +1000908 debug("request %u: setstat name \"%s\"", id, name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000909 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000910 logit("set \"%s\" size %llu",
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000911 name, (unsigned long long)a.size);
912 r = truncate(name, a.size);
913 if (r == -1)
Damien Miller00c92172002-02-13 14:05:00 +1100914 status = errno_to_portable(errno);
915 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000916 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
917 logit("set \"%s\" mode %04o", name, a.perm);
918 r = chmod(name, a.perm & 07777);
919 if (r == -1)
Damien Miller7b28dc52000-09-05 13:34:53 +1100920 status = errno_to_portable(errno);
921 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000922 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000923 char buf[64];
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000924 time_t t = a.mtime;
Damien Millerfef95ad2006-07-10 20:46:55 +1000925
926 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
927 localtime(&t));
928 logit("set \"%s\" modtime %s", name, buf);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000929 r = utimes(name, attrib_to_tv(&a));
930 if (r == -1)
Damien Miller7b28dc52000-09-05 13:34:53 +1100931 status = errno_to_portable(errno);
932 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000933 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000934 logit("set \"%s\" owner %lu group %lu", name,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000935 (u_long)a.uid, (u_long)a.gid);
936 r = chown(name, a.uid, a.gid);
937 if (r == -1)
Kevin Steves8e743932001-02-05 13:24:35 +0000938 status = errno_to_portable(errno);
939 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100940 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000941 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100942}
943
Ben Lindstrombba81212001-06-25 05:01:22 +0000944static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100945process_fsetstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100946{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000947 Attrib a;
948 int handle, fd, r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000949 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000950
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000951 if ((r = get_handle(iqueue, &handle)) != 0 ||
952 (r = decode_attrib(iqueue, &a)) != 0)
953 fatal("%s: buffer error: %s", __func__, ssh_err(r));
954
955 debug("request %u: fsetstat handle %d", id, handle);
956 fd = handle_to_fd(handle);
957 if (fd < 0)
958 status = SSH2_FX_FAILURE;
959 else {
960 char *name = handle_to_name(handle);
961
962 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
963 logit("set \"%s\" size %llu",
964 name, (unsigned long long)a.size);
965 r = ftruncate(fd, a.size);
966 if (r == -1)
967 status = errno_to_portable(errno);
968 }
969 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
970 logit("set \"%s\" mode %04o", name, a.perm);
971#ifdef HAVE_FCHMOD
Damien Miller83b96782015-01-15 02:35:50 +1100972 r = fchmod(fd, a.perm & 07777);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000973#else
Damien Miller83b96782015-01-15 02:35:50 +1100974 r = chmod(name, a.perm & 07777);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000975#endif
976 if (r == -1)
977 status = errno_to_portable(errno);
978 }
979 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
980 char buf[64];
981 time_t t = a.mtime;
982
983 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
984 localtime(&t));
985 logit("set \"%s\" modtime %s", name, buf);
986#ifdef HAVE_FUTIMES
Damien Miller83b96782015-01-15 02:35:50 +1100987 r = futimes(fd, attrib_to_tv(&a));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000988#else
Damien Miller83b96782015-01-15 02:35:50 +1100989 r = utimes(name, attrib_to_tv(&a));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000990#endif
991 if (r == -1)
992 status = errno_to_portable(errno);
993 }
994 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
995 logit("set \"%s\" owner %lu group %lu", name,
996 (u_long)a.uid, (u_long)a.gid);
997#ifdef HAVE_FCHOWN
Damien Miller83b96782015-01-15 02:35:50 +1100998 r = fchown(fd, a.uid, a.gid);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000999#else
Damien Miller83b96782015-01-15 02:35:50 +11001000 r = chown(name, a.uid, a.gid);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001001#endif
1002 if (r == -1)
1003 status = errno_to_portable(errno);
1004 }
1005 }
1006 send_status(id, status);
1007}
Damien Miller7b28dc52000-09-05 13:34:53 +11001008
Ben Lindstrombba81212001-06-25 05:01:22 +00001009static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001010process_opendir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001011{
1012 DIR *dirp = NULL;
1013 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001014 int r, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001015
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001016 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1017 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1018
Damien Millerfef95ad2006-07-10 20:46:55 +10001019 debug3("request %u: opendir", id);
1020 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001021 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001022 if (dirp == NULL) {
1023 status = errno_to_portable(errno);
1024 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +11001025 handle = handle_new(HANDLE_DIR, path, 0, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +11001026 if (handle < 0) {
1027 closedir(dirp);
1028 } else {
1029 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001030 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001031 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001032
Damien Miller7b28dc52000-09-05 13:34:53 +11001033 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001034 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +11001035 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001036 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001037}
1038
Ben Lindstrombba81212001-06-25 05:01:22 +00001039static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001040process_readdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001041{
1042 DIR *dirp;
1043 struct dirent *dp;
1044 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001045 int r, handle;
Damien Miller7b28dc52000-09-05 13:34:53 +11001046
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001047 if ((r = get_handle(iqueue, &handle)) != 0)
1048 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1049
Damien Millerfef95ad2006-07-10 20:46:55 +10001050 debug("request %u: readdir \"%s\" (handle %d)", id,
1051 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +11001052 dirp = handle_to_dir(handle);
1053 path = handle_to_name(handle);
1054 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001055 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +11001056 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +11001057 struct stat st;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001058 char pathname[PATH_MAX];
Damien Miller7b28dc52000-09-05 13:34:53 +11001059 Stat *stats;
1060 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001061
Damien Miller07d86be2006-03-26 14:19:21 +11001062 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001063 while ((dp = readdir(dirp)) != NULL) {
1064 if (count >= nstats) {
1065 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +11001066 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001067 }
1068/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +00001069 snprintf(pathname, sizeof pathname, "%s%s%s", path,
1070 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001071 if (lstat(pathname, &st) < 0)
1072 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001073 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +11001074 stats[count].name = xstrdup(dp->d_name);
Darren Tucker2901e2d2010-01-13 22:44:06 +11001075 stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001076 count++;
1077 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001078 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +11001079 if (count == 100)
1080 break;
1081 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001082 if (count > 0) {
1083 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001084 for (i = 0; i < count; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001085 free(stats[i].name);
1086 free(stats[i].long_name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001087 }
1088 } else {
1089 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +11001090 }
Darren Tuckera627d422013-06-02 07:31:17 +10001091 free(stats);
Damien Miller7b28dc52000-09-05 13:34:53 +11001092 }
1093}
1094
Ben Lindstrombba81212001-06-25 05:01:22 +00001095static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001096process_remove(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001097{
1098 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001099 int r, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001100
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001101 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1102 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1103
Damien Millerfef95ad2006-07-10 20:46:55 +10001104 debug3("request %u: remove", id);
1105 logit("remove name \"%s\"", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001106 r = unlink(name);
1107 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001108 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001109 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001110}
1111
Ben Lindstrombba81212001-06-25 05:01:22 +00001112static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001113process_mkdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001114{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001115 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +11001116 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001117 int r, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001118
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001119 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
1120 (r = decode_attrib(iqueue, &a)) != 0)
1121 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1122
1123 mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
1124 a.perm & 07777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +10001125 debug3("request %u: mkdir", id);
1126 logit("mkdir name \"%s\" mode 0%o", name, mode);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001127 r = mkdir(name, mode);
1128 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001129 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001130 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001131}
1132
Ben Lindstrombba81212001-06-25 05:01:22 +00001133static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001134process_rmdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001135{
Damien Miller7b28dc52000-09-05 13:34:53 +11001136 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001137 int r, status;
Damien Miller7b28dc52000-09-05 13:34:53 +11001138
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001139 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1140 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1141
Damien Millerfef95ad2006-07-10 20:46:55 +10001142 debug3("request %u: rmdir", id);
1143 logit("rmdir name \"%s\"", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001144 r = rmdir(name);
1145 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001146 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001147 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001148}
1149
Ben Lindstrombba81212001-06-25 05:01:22 +00001150static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001151process_realpath(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001152{
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001153 char resolvedname[PATH_MAX];
Damien Miller7b28dc52000-09-05 13:34:53 +11001154 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001155 int r;
Damien Miller7b28dc52000-09-05 13:34:53 +11001156
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001157 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1158 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1159
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001160 if (path[0] == '\0') {
Darren Tuckera627d422013-06-02 07:31:17 +10001161 free(path);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001162 path = xstrdup(".");
1163 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001164 debug3("request %u: realpath", id);
1165 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001166 if (realpath(path, resolvedname) == NULL) {
1167 send_status(id, errno_to_portable(errno));
1168 } else {
1169 Stat s;
1170 attrib_clear(&s.attrib);
1171 s.name = s.long_name = resolvedname;
1172 send_names(id, 1, &s);
1173 }
Darren Tuckera627d422013-06-02 07:31:17 +10001174 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001175}
1176
Ben Lindstrombba81212001-06-25 05:01:22 +00001177static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001178process_rename(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001179{
Damien Miller7b28dc52000-09-05 13:34:53 +11001180 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001181 int r, status;
Damien Millerb3207e82003-03-26 16:01:11 +11001182 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +11001183
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001184 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1185 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1186 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1187
Damien Millerfef95ad2006-07-10 20:46:55 +10001188 debug3("request %u: rename", id);
1189 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +11001190 status = SSH2_FX_FAILURE;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001191 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +11001192 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +11001193 else if (S_ISREG(sb.st_mode)) {
1194 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001195 if (link(oldpath, newpath) == -1) {
Damien Miller0e265512009-08-28 10:43:13 +10001196 if (errno == EOPNOTSUPP || errno == ENOSYS
Darren Tuckerf7fa7062008-07-04 14:10:19 +10001197#ifdef EXDEV
1198 || errno == EXDEV
1199#endif
Darren Tuckere59b5082004-06-28 16:01:19 +10001200#ifdef LINK_OPNOTSUPP_ERRNO
1201 || errno == LINK_OPNOTSUPP_ERRNO
1202#endif
1203 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001204 struct stat st;
1205
1206 /*
1207 * fs doesn't support links, so fall back to
1208 * stat+rename. This is racy.
1209 */
1210 if (stat(newpath, &st) == -1) {
1211 if (rename(oldpath, newpath) == -1)
1212 status =
1213 errno_to_portable(errno);
1214 else
1215 status = SSH2_FX_OK;
1216 }
1217 } else {
1218 status = errno_to_portable(errno);
1219 }
1220 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001221 status = errno_to_portable(errno);
1222 /* clean spare link */
1223 unlink(newpath);
1224 } else
1225 status = SSH2_FX_OK;
1226 } else if (stat(newpath, &sb) == -1) {
1227 if (rename(oldpath, newpath) == -1)
1228 status = errno_to_portable(errno);
1229 else
1230 status = SSH2_FX_OK;
1231 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001232 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001233 free(oldpath);
1234 free(newpath);
Damien Miller7b28dc52000-09-05 13:34:53 +11001235}
1236
Ben Lindstrombba81212001-06-25 05:01:22 +00001237static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001238process_readlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001239{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001240 int r, len;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001241 char buf[PATH_MAX];
Damien Miller058316f2001-03-08 10:08:49 +11001242 char *path;
1243
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001244 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1245 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1246
Damien Millerfef95ad2006-07-10 20:46:55 +10001247 debug3("request %u: readlink", id);
1248 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001249 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001250 send_status(id, errno_to_portable(errno));
1251 else {
1252 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001253
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001254 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001255 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001256 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001257 send_names(id, 1, &s);
1258 }
Darren Tuckera627d422013-06-02 07:31:17 +10001259 free(path);
Damien Miller058316f2001-03-08 10:08:49 +11001260}
1261
Ben Lindstrombba81212001-06-25 05:01:22 +00001262static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001263process_symlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001264{
Damien Miller058316f2001-03-08 10:08:49 +11001265 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001266 int r, status;
Damien Miller058316f2001-03-08 10:08:49 +11001267
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001268 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1269 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1270 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1271
Damien Millerfef95ad2006-07-10 20:46:55 +10001272 debug3("request %u: symlink", id);
1273 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001274 /* this will fail if 'newpath' exists */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001275 r = symlink(oldpath, newpath);
1276 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001277 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001278 free(oldpath);
1279 free(newpath);
Damien Miller058316f2001-03-08 10:08:49 +11001280}
1281
Ben Lindstrombba81212001-06-25 05:01:22 +00001282static void
Damien Miller7c296612008-03-07 18:33:53 +11001283process_extended_posix_rename(u_int32_t id)
1284{
1285 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001286 int r, status;
Damien Miller7c296612008-03-07 18:33:53 +11001287
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001288 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1289 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1290 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1291
Damien Miller7c296612008-03-07 18:33:53 +11001292 debug3("request %u: posix-rename", id);
1293 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001294 r = rename(oldpath, newpath);
1295 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001296 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001297 free(oldpath);
1298 free(newpath);
Damien Miller7c296612008-03-07 18:33:53 +11001299}
1300
1301static void
Damien Millerd671e5a2008-05-19 14:53:33 +10001302process_extended_statvfs(u_int32_t id)
1303{
1304 char *path;
1305 struct statvfs st;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001306 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +10001307
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001308 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1309 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker2aca1592014-01-19 15:25:34 +11001310 debug3("request %u: statvfs", id);
1311 logit("statvfs \"%s\"", path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001312
1313 if (statvfs(path, &st) != 0)
1314 send_status(id, errno_to_portable(errno));
1315 else
1316 send_statvfs(id, &st);
Darren Tuckera627d422013-06-02 07:31:17 +10001317 free(path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001318}
1319
1320static void
1321process_extended_fstatvfs(u_int32_t id)
1322{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001323 int r, handle, fd;
Damien Millerd671e5a2008-05-19 14:53:33 +10001324 struct statvfs st;
1325
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001326 if ((r = get_handle(iqueue, &handle)) != 0)
1327 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +10001328 debug("request %u: fstatvfs \"%s\" (handle %u)",
1329 id, handle_to_name(handle), handle);
1330 if ((fd = handle_to_fd(handle)) < 0) {
1331 send_status(id, SSH2_FX_FAILURE);
1332 return;
1333 }
1334 if (fstatvfs(fd, &st) != 0)
1335 send_status(id, errno_to_portable(errno));
1336 else
1337 send_statvfs(id, &st);
1338}
1339
1340static void
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001341process_extended_hardlink(u_int32_t id)
1342{
1343 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001344 int r, status;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001345
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001346 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1347 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1348 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1349
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001350 debug3("request %u: hardlink", id);
1351 logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001352 r = link(oldpath, newpath);
1353 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001354 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001355 free(oldpath);
1356 free(newpath);
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001357}
1358
1359static void
Damien Millerf29238e2013-10-17 11:48:52 +11001360process_extended_fsync(u_int32_t id)
1361{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001362 int handle, fd, r, status = SSH2_FX_OP_UNSUPPORTED;
Damien Millerf29238e2013-10-17 11:48:52 +11001363
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001364 if ((r = get_handle(iqueue, &handle)) != 0)
1365 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf29238e2013-10-17 11:48:52 +11001366 debug3("request %u: fsync (handle %u)", id, handle);
1367 verbose("fsync \"%s\"", handle_to_name(handle));
1368 if ((fd = handle_to_fd(handle)) < 0)
1369 status = SSH2_FX_NO_SUCH_FILE;
1370 else if (handle_is_ok(handle, HANDLE_FILE)) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001371 r = fsync(fd);
1372 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Millerf29238e2013-10-17 11:48:52 +11001373 }
1374 send_status(id, status);
1375}
1376
1377static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001378process_extended(u_int32_t id)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001379{
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001380 char *request;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001381 int i, r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001382
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001383 if ((r = sshbuf_get_cstring(iqueue, &request, NULL)) != 0)
1384 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001385 for (i = 0; extended_handlers[i].handler != NULL; i++) {
1386 if (strcmp(request, extended_handlers[i].ext_name) == 0) {
1387 if (!request_permitted(&extended_handlers[i]))
1388 send_status(id, SSH2_FX_PERMISSION_DENIED);
1389 else
1390 extended_handlers[i].handler(id);
1391 break;
1392 }
1393 }
1394 if (extended_handlers[i].handler == NULL) {
1395 error("Unknown extended request \"%.100s\"", request);
Damien Miller7c296612008-03-07 18:33:53 +11001396 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001397 }
Darren Tuckera627d422013-06-02 07:31:17 +10001398 free(request);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001399}
Damien Miller7b28dc52000-09-05 13:34:53 +11001400
1401/* stolen from ssh-agent */
1402
Ben Lindstrombba81212001-06-25 05:01:22 +00001403static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001404process(void)
1405{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001406 u_int msg_len;
1407 u_int buf_len;
1408 u_int consumed;
1409 u_char type;
1410 const u_char *cp;
1411 int i, r;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001412 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +11001413
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001414 buf_len = sshbuf_len(iqueue);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001415 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001416 return; /* Incomplete message. */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001417 cp = sshbuf_ptr(iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001418 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001419 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001420 error("bad message from %s local user %s",
1421 client_addr, pw->pw_name);
Damien Millerdfc24252008-02-10 22:29:40 +11001422 sftp_server_cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001423 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001424 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001425 return;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001426 if ((r = sshbuf_consume(iqueue, 4)) != 0)
1427 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom2c140472002-06-06 21:57:54 +00001428 buf_len -= 4;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001429 if ((r = sshbuf_get_u8(iqueue, &type)) != 0)
1430 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001431
Damien Miller7b28dc52000-09-05 13:34:53 +11001432 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001433 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001434 process_init();
Damien Miller6eaeebf2013-10-15 11:55:57 +11001435 init_done = 1;
Damien Miller058316f2001-03-08 10:08:49 +11001436 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001437 case SSH2_FXP_EXTENDED:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001438 if (!init_done)
1439 fatal("Received extended request before init");
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001440 if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1441 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001442 process_extended(id);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001443 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001444 default:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001445 if (!init_done)
1446 fatal("Received %u request before init", type);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001447 if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1448 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001449 for (i = 0; handlers[i].handler != NULL; i++) {
1450 if (type == handlers[i].type) {
1451 if (!request_permitted(&handlers[i])) {
1452 send_status(id,
1453 SSH2_FX_PERMISSION_DENIED);
1454 } else {
1455 handlers[i].handler(id);
1456 }
1457 break;
1458 }
1459 }
1460 if (handlers[i].handler == NULL)
1461 error("Unknown message %u", type);
Damien Miller7b28dc52000-09-05 13:34:53 +11001462 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001463 /* discard the remaining bytes from the current packet */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001464 if (buf_len < sshbuf_len(iqueue)) {
Damien Millerdfc24252008-02-10 22:29:40 +11001465 error("iqueue grew unexpectedly");
1466 sftp_server_cleanup_exit(255);
1467 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001468 consumed = buf_len - sshbuf_len(iqueue);
Damien Millerdfc24252008-02-10 22:29:40 +11001469 if (msg_len < consumed) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001470 error("msg_len %u < consumed %u", msg_len, consumed);
Damien Millerdfc24252008-02-10 22:29:40 +11001471 sftp_server_cleanup_exit(255);
1472 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001473 if (msg_len > consumed &&
1474 (r = sshbuf_consume(iqueue, msg_len - consumed)) != 0)
1475 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001476}
1477
Damien Millerfef95ad2006-07-10 20:46:55 +10001478/* Cleanup handler that logs active handles upon normal exit */
1479void
Damien Millerdfc24252008-02-10 22:29:40 +11001480sftp_server_cleanup_exit(int i)
Damien Millerfef95ad2006-07-10 20:46:55 +10001481{
1482 if (pw != NULL && client_addr != NULL) {
1483 handle_log_exit();
1484 logit("session closed for local user %s from [%s]",
1485 pw->pw_name, client_addr);
1486 }
1487 _exit(i);
1488}
1489
1490static void
Damien Millerdfc24252008-02-10 22:29:40 +11001491sftp_server_usage(void)
Damien Millerfef95ad2006-07-10 20:46:55 +10001492{
1493 extern char *__progname;
1494
1495 fprintf(stderr,
Damien Milleraa7ad302013-01-09 15:58:21 +11001496 "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
Damien Miller6efab272013-10-15 12:07:05 +11001497 "[-l log_level]\n\t[-P blacklisted_requests] "
1498 "[-p whitelisted_requests] [-u umask]\n"
1499 " %s -Q protocol_feature\n",
1500 __progname, __progname);
Damien Millerfef95ad2006-07-10 20:46:55 +10001501 exit(1);
1502}
1503
Damien Miller7b28dc52000-09-05 13:34:53 +11001504int
Damien Millerd8cb1f12008-02-10 22:40:12 +11001505sftp_server_main(int argc, char **argv, struct passwd *user_pw)
Damien Miller7b28dc52000-09-05 13:34:53 +11001506{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001507 fd_set *rset, *wset;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001508 int i, r, in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001509 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001510 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
Damien Miller502ab0e2013-01-09 15:57:36 +11001511 char *cp, *homedir = NULL, buf[4*4096];
Damien Miller07331212010-11-05 10:20:31 +11001512 long mask;
Damien Millerfef95ad2006-07-10 20:46:55 +10001513
Damien Millerfef95ad2006-07-10 20:46:55 +10001514 extern char *optarg;
1515 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001516
Damien Millerfef95ad2006-07-10 20:46:55 +10001517 __progname = ssh_get_progname(argv[0]);
1518 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001519
Damien Miller502ab0e2013-01-09 15:57:36 +11001520 pw = pwcopy(user_pw);
1521
Damien Miller6eaeebf2013-10-15 11:55:57 +11001522 while (!skipargs && (ch = getopt(argc, argv,
1523 "d:f:l:P:p:Q:u:cehR")) != -1) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001524 switch (ch) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001525 case 'Q':
1526 if (strcasecmp(optarg, "requests") != 0) {
1527 fprintf(stderr, "Invalid query type\n");
1528 exit(1);
1529 }
1530 for (i = 0; handlers[i].handler != NULL; i++)
1531 printf("%s\n", handlers[i].name);
1532 for (i = 0; extended_handlers[i].handler != NULL; i++)
1533 printf("%s\n", extended_handlers[i].name);
1534 exit(0);
1535 break;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001536 case 'R':
1537 readonly = 1;
1538 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001539 case 'c':
1540 /*
1541 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001542 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001543 */
1544 skipargs = 1;
1545 break;
1546 case 'e':
1547 log_stderr = 1;
1548 break;
1549 case 'l':
1550 log_level = log_level_number(optarg);
1551 if (log_level == SYSLOG_LEVEL_NOT_SET)
1552 error("Invalid log level \"%s\"", optarg);
1553 break;
1554 case 'f':
1555 log_facility = log_facility_number(optarg);
Damien Miller35e18db2007-09-17 16:11:33 +10001556 if (log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Millerfef95ad2006-07-10 20:46:55 +10001557 error("Invalid log facility \"%s\"", optarg);
1558 break;
Damien Miller502ab0e2013-01-09 15:57:36 +11001559 case 'd':
1560 cp = tilde_expand_filename(optarg, user_pw->pw_uid);
1561 homedir = percent_expand(cp, "d", user_pw->pw_dir,
1562 "u", user_pw->pw_name, (char *)NULL);
1563 free(cp);
1564 break;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001565 case 'p':
1566 if (request_whitelist != NULL)
1567 fatal("Permitted requests already set");
1568 request_whitelist = xstrdup(optarg);
1569 break;
1570 case 'P':
1571 if (request_blacklist != NULL)
1572 fatal("Refused requests already set");
1573 request_blacklist = xstrdup(optarg);
1574 break;
Darren Tucker7dc48502009-10-07 08:44:42 +11001575 case 'u':
Damien Miller07331212010-11-05 10:20:31 +11001576 errno = 0;
1577 mask = strtol(optarg, &cp, 8);
1578 if (mask < 0 || mask > 0777 || *cp != '\0' ||
1579 cp == optarg || (mask == 0 && errno != 0))
1580 fatal("Invalid umask \"%s\"", optarg);
1581 (void)umask((mode_t)mask);
Darren Tucker7dc48502009-10-07 08:44:42 +11001582 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001583 case 'h':
1584 default:
Damien Millerdfc24252008-02-10 22:29:40 +11001585 sftp_server_usage();
Damien Millerfef95ad2006-07-10 20:46:55 +10001586 }
1587 }
1588
1589 log_init(__progname, log_level, log_facility, log_stderr);
1590
Damien Miller9c1dede2014-08-24 03:01:06 +10001591#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
Damien Miller14928b72014-04-01 14:38:07 +11001592 /*
1593 * On Linux, we should try to avoid making /proc/self/{mem,maps}
1594 * available to the user so that sftp access doesn't automatically
1595 * imply arbitrary code execution access that will break
1596 * restricted configurations.
1597 */
1598 if (prctl(PR_SET_DUMPABLE, 0) != 0)
1599 fatal("unable to make the process undumpable");
Damien Miller9c1dede2014-08-24 03:01:06 +10001600#endif /* defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE) */
Damien Miller14928b72014-04-01 14:38:07 +11001601
Damien Millerfef95ad2006-07-10 20:46:55 +10001602 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1603 client_addr = xstrdup(cp);
Damien Millerdfc24252008-02-10 22:29:40 +11001604 if ((cp = strchr(client_addr, ' ')) == NULL) {
1605 error("Malformed SSH_CONNECTION variable: \"%s\"",
Damien Millerfef95ad2006-07-10 20:46:55 +10001606 getenv("SSH_CONNECTION"));
Damien Millerdfc24252008-02-10 22:29:40 +11001607 sftp_server_cleanup_exit(255);
1608 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001609 *cp = '\0';
1610 } else
1611 client_addr = xstrdup("UNKNOWN");
1612
Damien Millerfef95ad2006-07-10 20:46:55 +10001613 logit("session opened for local user %s from [%s]",
1614 pw->pw_name, client_addr);
1615
Darren Tuckeraaf51d22010-01-08 19:04:49 +11001616 in = STDIN_FILENO;
1617 out = STDOUT_FILENO;
Damien Miller7b28dc52000-09-05 13:34:53 +11001618
Damien Miller402b3312001-04-14 00:28:42 +10001619#ifdef HAVE_CYGWIN
1620 setmode(in, O_BINARY);
1621 setmode(out, O_BINARY);
1622#endif
1623
Damien Miller7b28dc52000-09-05 13:34:53 +11001624 max = 0;
1625 if (in > max)
1626 max = in;
1627 if (out > max)
1628 max = out;
1629
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001630 if ((iqueue = sshbuf_new()) == NULL)
1631 fatal("%s: sshbuf_new failed", __func__);
1632 if ((oqueue = sshbuf_new()) == NULL)
1633 fatal("%s: sshbuf_new failed", __func__);
Damien Miller7b28dc52000-09-05 13:34:53 +11001634
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001635 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1636 rset = (fd_set *)xmalloc(set_size);
1637 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001638
Damien Miller502ab0e2013-01-09 15:57:36 +11001639 if (homedir != NULL) {
1640 if (chdir(homedir) != 0) {
1641 error("chdir to \"%s\" failed: %s", homedir,
1642 strerror(errno));
1643 }
1644 }
1645
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001646 for (;;) {
1647 memset(rset, 0, set_size);
1648 memset(wset, 0, set_size);
1649
Darren Tuckere9405982007-05-20 15:09:04 +10001650 /*
1651 * Ensure that we can read a full buffer and handle
1652 * the worst-case length packet it can generate,
1653 * otherwise apply backpressure by stopping reads.
1654 */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001655 if ((r = sshbuf_check_reserve(iqueue, sizeof(buf))) == 0 &&
1656 (r = sshbuf_check_reserve(oqueue,
1657 SFTP_MAX_MSG_LENGTH)) == 0)
Darren Tuckere9405982007-05-20 15:09:04 +10001658 FD_SET(in, rset);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001659 else if (r != SSH_ERR_NO_BUFFER_SPACE)
1660 fatal("%s: sshbuf_check_reserve failed: %s",
1661 __func__, ssh_err(r));
Darren Tuckere9405982007-05-20 15:09:04 +10001662
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001663 olen = sshbuf_len(oqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +11001664 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001665 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001666
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001667 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001668 if (errno == EINTR)
1669 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001670 error("select: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001671 sftp_server_cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001672 }
1673
1674 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001675 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001676 len = read(in, buf, sizeof buf);
1677 if (len == 0) {
1678 debug("read eof");
Damien Millerdfc24252008-02-10 22:29:40 +11001679 sftp_server_cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001680 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001681 error("read: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001682 sftp_server_cleanup_exit(1);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001683 } else if ((r = sshbuf_put(iqueue, buf, len)) != 0) {
1684 fatal("%s: buffer error: %s",
1685 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001686 }
1687 }
1688 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001689 if (FD_ISSET(out, wset)) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001690 len = write(out, sshbuf_ptr(oqueue), olen);
Damien Miller7b28dc52000-09-05 13:34:53 +11001691 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001692 error("write: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001693 sftp_server_cleanup_exit(1);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001694 } else if ((r = sshbuf_consume(oqueue, len)) != 0) {
1695 fatal("%s: buffer error: %s",
1696 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001697 }
1698 }
Darren Tuckere9405982007-05-20 15:09:04 +10001699
1700 /*
1701 * Process requests from client if we can fit the results
1702 * into the output buffer, otherwise stop processing input
1703 * and let the output queue drain.
1704 */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001705 r = sshbuf_check_reserve(oqueue, SFTP_MAX_MSG_LENGTH);
1706 if (r == 0)
Darren Tuckere9405982007-05-20 15:09:04 +10001707 process();
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001708 else if (r != SSH_ERR_NO_BUFFER_SPACE)
1709 fatal("%s: sshbuf_check_reserve: %s",
1710 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001711 }
1712}