blob: eac11d7e695d2a3c8919b23bb822646f50ef2134 [file] [log] [blame]
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001/* $OpenBSD: sftp-server.c,v 1.107 2015/08/20 22:32:42 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 Miller5598b4f2006-07-24 14:09:40 +100043#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100044#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100045#include <stdarg.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110046
Damien Miller7b28dc52000-09-05 13:34:53 +110047#include "xmalloc.h"
djm@openbsd.org7d845f42015-01-14 13:54:13 +000048#include "sshbuf.h"
49#include "ssherr.h"
Damien Millerd7834352006-08-05 12:39:39 +100050#include "log.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100051#include "misc.h"
Damien Miller6eaeebf2013-10-15 11:55:57 +110052#include "match.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100053#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110054
Ben Lindstrom2f959b42001-01-11 06:20:23 +000055#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110056#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110057
Damien Millerfef95ad2006-07-10 20:46:55 +100058/* Our verbosity */
Damien Miller6eaeebf2013-10-15 11:55:57 +110059static LogLevel log_level = SYSLOG_LEVEL_ERROR;
Damien Millerfef95ad2006-07-10 20:46:55 +100060
61/* Our client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110062static struct passwd *pw = NULL;
63static char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000064
Damien Miller7b28dc52000-09-05 13:34:53 +110065/* input and output queue */
djm@openbsd.org7d845f42015-01-14 13:54:13 +000066struct sshbuf *iqueue;
67struct sshbuf *oqueue;
Damien Miller7b28dc52000-09-05 13:34:53 +110068
Damien Miller058316f2001-03-08 10:08:49 +110069/* Version of client */
Damien Miller6eaeebf2013-10-15 11:55:57 +110070static u_int version;
71
72/* SSH2_FXP_INIT received */
73static int init_done;
Damien Miller058316f2001-03-08 10:08:49 +110074
Darren Tuckerdb7bf822010-01-09 22:24:33 +110075/* Disable writes */
Damien Miller6eaeebf2013-10-15 11:55:57 +110076static int readonly;
77
78/* Requests that are allowed/denied */
79static char *request_whitelist, *request_blacklist;
Darren Tuckerdb7bf822010-01-09 22:24:33 +110080
Darren Tuckera6612d42003-06-28 12:39:03 +100081/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110082typedef struct Stat Stat;
83
Damien Miller33804262001-02-04 23:20:18 +110084struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110085 char *name;
86 char *long_name;
87 Attrib attrib;
88};
89
Damien Miller6eaeebf2013-10-15 11:55:57 +110090/* Packet handlers */
91static void process_open(u_int32_t id);
92static void process_close(u_int32_t id);
93static void process_read(u_int32_t id);
94static void process_write(u_int32_t id);
95static void process_stat(u_int32_t id);
96static void process_lstat(u_int32_t id);
97static void process_fstat(u_int32_t id);
98static void process_setstat(u_int32_t id);
99static void process_fsetstat(u_int32_t id);
100static void process_opendir(u_int32_t id);
101static void process_readdir(u_int32_t id);
102static void process_remove(u_int32_t id);
103static void process_mkdir(u_int32_t id);
104static void process_rmdir(u_int32_t id);
105static void process_realpath(u_int32_t id);
106static void process_rename(u_int32_t id);
107static void process_readlink(u_int32_t id);
108static void process_symlink(u_int32_t id);
109static void process_extended_posix_rename(u_int32_t id);
110static void process_extended_statvfs(u_int32_t id);
111static void process_extended_fstatvfs(u_int32_t id);
112static void process_extended_hardlink(u_int32_t id);
Damien Millerf29238e2013-10-17 11:48:52 +1100113static void process_extended_fsync(u_int32_t id);
Damien Miller6eaeebf2013-10-15 11:55:57 +1100114static void process_extended(u_int32_t id);
115
116struct sftp_handler {
117 const char *name; /* user-visible name for fine-grained perms */
118 const char *ext_name; /* extended request name */
119 u_int type; /* packet type, for non extended packets */
120 void (*handler)(u_int32_t);
121 int does_write; /* if nonzero, banned for readonly mode */
122};
123
124struct sftp_handler handlers[] = {
125 /* NB. SSH2_FXP_OPEN does the readonly check in the handler itself */
126 { "open", NULL, SSH2_FXP_OPEN, process_open, 0 },
127 { "close", NULL, SSH2_FXP_CLOSE, process_close, 0 },
128 { "read", NULL, SSH2_FXP_READ, process_read, 0 },
129 { "write", NULL, SSH2_FXP_WRITE, process_write, 1 },
130 { "lstat", NULL, SSH2_FXP_LSTAT, process_lstat, 0 },
131 { "fstat", NULL, SSH2_FXP_FSTAT, process_fstat, 0 },
132 { "setstat", NULL, SSH2_FXP_SETSTAT, process_setstat, 1 },
133 { "fsetstat", NULL, SSH2_FXP_FSETSTAT, process_fsetstat, 1 },
134 { "opendir", NULL, SSH2_FXP_OPENDIR, process_opendir, 0 },
135 { "readdir", NULL, SSH2_FXP_READDIR, process_readdir, 0 },
136 { "remove", NULL, SSH2_FXP_REMOVE, process_remove, 1 },
137 { "mkdir", NULL, SSH2_FXP_MKDIR, process_mkdir, 1 },
138 { "rmdir", NULL, SSH2_FXP_RMDIR, process_rmdir, 1 },
139 { "realpath", NULL, SSH2_FXP_REALPATH, process_realpath, 0 },
140 { "stat", NULL, SSH2_FXP_STAT, process_stat, 0 },
141 { "rename", NULL, SSH2_FXP_RENAME, process_rename, 1 },
142 { "readlink", NULL, SSH2_FXP_READLINK, process_readlink, 0 },
143 { "symlink", NULL, SSH2_FXP_SYMLINK, process_symlink, 1 },
144 { NULL, NULL, 0, NULL, 0 }
145};
146
147/* SSH2_FXP_EXTENDED submessages */
148struct sftp_handler extended_handlers[] = {
149 { "posix-rename", "posix-rename@openssh.com", 0,
150 process_extended_posix_rename, 1 },
151 { "statvfs", "statvfs@openssh.com", 0, process_extended_statvfs, 0 },
152 { "fstatvfs", "fstatvfs@openssh.com", 0, process_extended_fstatvfs, 0 },
153 { "hardlink", "hardlink@openssh.com", 0, process_extended_hardlink, 1 },
Damien Millerf29238e2013-10-17 11:48:52 +1100154 { "fsync", "fsync@openssh.com", 0, process_extended_fsync, 1 },
Damien Miller6eaeebf2013-10-15 11:55:57 +1100155 { NULL, NULL, 0, NULL, 0 }
156};
157
158static int
159request_permitted(struct sftp_handler *h)
160{
161 char *result;
162
163 if (readonly && h->does_write) {
164 verbose("Refusing %s request in read-only mode", h->name);
165 return 0;
166 }
167 if (request_blacklist != NULL &&
168 ((result = match_list(h->name, request_blacklist, NULL))) != NULL) {
169 free(result);
170 verbose("Refusing blacklisted %s request", h->name);
171 return 0;
172 }
173 if (request_whitelist != NULL &&
174 ((result = match_list(h->name, request_whitelist, NULL))) != NULL) {
175 free(result);
176 debug2("Permitting whitelisted %s request", h->name);
177 return 1;
178 }
179 if (request_whitelist != NULL) {
180 verbose("Refusing non-whitelisted %s request", h->name);
181 return 0;
182 }
183 return 1;
184}
185
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100187errno_to_portable(int unixerrno)
188{
189 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000190
Damien Miller7b28dc52000-09-05 13:34:53 +1100191 switch (unixerrno) {
192 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000193 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100194 break;
195 case ENOENT:
196 case ENOTDIR:
197 case EBADF:
198 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000199 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100200 break;
201 case EPERM:
202 case EACCES:
203 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000204 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +1100205 break;
206 case ENAMETOOLONG:
207 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000208 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100209 break;
Darren Tucker422c34c2008-06-09 22:48:31 +1000210 case ENOSYS:
211 ret = SSH2_FX_OP_UNSUPPORTED;
212 break;
Damien Miller7b28dc52000-09-05 13:34:53 +1100213 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000214 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100215 break;
216 }
217 return ret;
218}
219
Ben Lindstrombba81212001-06-25 05:01:22 +0000220static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100221flags_from_portable(int pflags)
222{
223 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000224
Ben Lindstrom36592512001-03-05 05:02:08 +0000225 if ((pflags & SSH2_FXF_READ) &&
226 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100227 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000228 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100229 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000230 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100231 flags = O_WRONLY;
232 }
Damien Millere9fc72e2013-10-15 12:14:12 +1100233 if (pflags & SSH2_FXF_APPEND)
234 flags |= O_APPEND;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000235 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100236 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000237 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100238 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000239 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100240 flags |= O_EXCL;
241 return flags;
242}
243
Damien Millerfef95ad2006-07-10 20:46:55 +1000244static const char *
245string_from_portable(int pflags)
246{
247 static char ret[128];
248
249 *ret = '\0';
250
251#define PAPPEND(str) { \
252 if (*ret != '\0') \
253 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000254 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000255 }
256
257 if (pflags & SSH2_FXF_READ)
258 PAPPEND("READ")
259 if (pflags & SSH2_FXF_WRITE)
260 PAPPEND("WRITE")
Damien Millere9fc72e2013-10-15 12:14:12 +1100261 if (pflags & SSH2_FXF_APPEND)
262 PAPPEND("APPEND")
Damien Millerfef95ad2006-07-10 20:46:55 +1000263 if (pflags & SSH2_FXF_CREAT)
264 PAPPEND("CREATE")
265 if (pflags & SSH2_FXF_TRUNC)
266 PAPPEND("TRUNCATE")
267 if (pflags & SSH2_FXF_EXCL)
268 PAPPEND("EXCL")
269
270 return ret;
271}
272
Damien Miller7b28dc52000-09-05 13:34:53 +1100273/* handle handles */
274
275typedef struct Handle Handle;
276struct Handle {
277 int use;
278 DIR *dirp;
279 int fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100280 int flags;
Damien Miller7b28dc52000-09-05 13:34:53 +1100281 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000282 u_int64_t bytes_read, bytes_write;
Damien Miller3397d0e2008-02-10 22:26:51 +1100283 int next_unused;
Damien Miller7b28dc52000-09-05 13:34:53 +1100284};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000285
Damien Miller7b28dc52000-09-05 13:34:53 +1100286enum {
287 HANDLE_UNUSED,
288 HANDLE_DIR,
289 HANDLE_FILE
290};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000291
Damien Miller3397d0e2008-02-10 22:26:51 +1100292Handle *handles = NULL;
293u_int num_handles = 0;
294int first_unused_handle = -1;
Damien Miller7b28dc52000-09-05 13:34:53 +1100295
Damien Miller3397d0e2008-02-10 22:26:51 +1100296static void handle_unused(int i)
Damien Miller7b28dc52000-09-05 13:34:53 +1100297{
Damien Miller3397d0e2008-02-10 22:26:51 +1100298 handles[i].use = HANDLE_UNUSED;
299 handles[i].next_unused = first_unused_handle;
300 first_unused_handle = i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100301}
302
Ben Lindstrombba81212001-06-25 05:01:22 +0000303static int
Damien Millere9fc72e2013-10-15 12:14:12 +1100304handle_new(int use, const char *name, int fd, int flags, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100305{
Damien Miller3397d0e2008-02-10 22:26:51 +1100306 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000307
Damien Miller3397d0e2008-02-10 22:26:51 +1100308 if (first_unused_handle == -1) {
309 if (num_handles + 1 <= num_handles)
310 return -1;
311 num_handles++;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000312 handles = xreallocarray(handles, num_handles, sizeof(Handle));
Damien Miller3397d0e2008-02-10 22:26:51 +1100313 handle_unused(num_handles - 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100314 }
Damien Miller3397d0e2008-02-10 22:26:51 +1100315
316 i = first_unused_handle;
317 first_unused_handle = handles[i].next_unused;
318
319 handles[i].use = use;
320 handles[i].dirp = dirp;
321 handles[i].fd = fd;
Damien Millere9fc72e2013-10-15 12:14:12 +1100322 handles[i].flags = flags;
Damien Miller3397d0e2008-02-10 22:26:51 +1100323 handles[i].name = xstrdup(name);
324 handles[i].bytes_read = handles[i].bytes_write = 0;
325
326 return i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100327}
328
Ben Lindstrombba81212001-06-25 05:01:22 +0000329static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100330handle_is_ok(int i, int type)
331{
Damien Miller3397d0e2008-02-10 22:26:51 +1100332 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100333}
334
Ben Lindstrombba81212001-06-25 05:01:22 +0000335static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000336handle_to_string(int handle, u_char **stringp, int *hlenp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100337{
Damien Miller7b28dc52000-09-05 13:34:53 +1100338 if (stringp == NULL || hlenp == NULL)
339 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000340 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100341 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000342 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100343 return 0;
344}
345
Ben Lindstrombba81212001-06-25 05:01:22 +0000346static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000347handle_from_string(const u_char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100348{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000349 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000350
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000351 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100352 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100353 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100354 if (handle_is_ok(val, HANDLE_FILE) ||
355 handle_is_ok(val, HANDLE_DIR))
356 return val;
357 return -1;
358}
359
Ben Lindstrombba81212001-06-25 05:01:22 +0000360static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100361handle_to_name(int handle)
362{
363 if (handle_is_ok(handle, HANDLE_DIR)||
364 handle_is_ok(handle, HANDLE_FILE))
365 return handles[handle].name;
366 return NULL;
367}
368
Ben Lindstrombba81212001-06-25 05:01:22 +0000369static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100370handle_to_dir(int handle)
371{
372 if (handle_is_ok(handle, HANDLE_DIR))
373 return handles[handle].dirp;
374 return NULL;
375}
376
Ben Lindstrombba81212001-06-25 05:01:22 +0000377static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100378handle_to_fd(int handle)
379{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000380 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100381 return handles[handle].fd;
382 return -1;
383}
384
Damien Millere9fc72e2013-10-15 12:14:12 +1100385static int
386handle_to_flags(int handle)
387{
388 if (handle_is_ok(handle, HANDLE_FILE))
389 return handles[handle].flags;
390 return 0;
391}
392
Damien Millerfef95ad2006-07-10 20:46:55 +1000393static void
394handle_update_read(int handle, ssize_t bytes)
395{
396 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
397 handles[handle].bytes_read += bytes;
398}
399
400static void
401handle_update_write(int handle, ssize_t bytes)
402{
403 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
404 handles[handle].bytes_write += bytes;
405}
406
407static u_int64_t
408handle_bytes_read(int handle)
409{
410 if (handle_is_ok(handle, HANDLE_FILE))
411 return (handles[handle].bytes_read);
412 return 0;
413}
414
415static u_int64_t
416handle_bytes_write(int handle)
417{
418 if (handle_is_ok(handle, HANDLE_FILE))
419 return (handles[handle].bytes_write);
420 return 0;
421}
422
Ben Lindstrombba81212001-06-25 05:01:22 +0000423static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100424handle_close(int handle)
425{
426 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000427
Damien Miller7b28dc52000-09-05 13:34:53 +1100428 if (handle_is_ok(handle, HANDLE_FILE)) {
429 ret = close(handles[handle].fd);
Darren Tuckera627d422013-06-02 07:31:17 +1000430 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100431 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100432 } else if (handle_is_ok(handle, HANDLE_DIR)) {
433 ret = closedir(handles[handle].dirp);
Darren Tuckera627d422013-06-02 07:31:17 +1000434 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100435 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100436 } else {
437 errno = ENOENT;
438 }
439 return ret;
440}
441
Damien Millerfef95ad2006-07-10 20:46:55 +1000442static void
443handle_log_close(int handle, char *emsg)
444{
445 if (handle_is_ok(handle, HANDLE_FILE)) {
446 logit("%s%sclose \"%s\" bytes read %llu written %llu",
447 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
448 handle_to_name(handle),
Darren Tucker86473c52007-05-20 14:59:32 +1000449 (unsigned long long)handle_bytes_read(handle),
450 (unsigned long long)handle_bytes_write(handle));
Damien Millerfef95ad2006-07-10 20:46:55 +1000451 } else {
452 logit("%s%sclosedir \"%s\"",
453 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
454 handle_to_name(handle));
455 }
456}
457
458static void
459handle_log_exit(void)
460{
461 u_int i;
462
Damien Miller3397d0e2008-02-10 22:26:51 +1100463 for (i = 0; i < num_handles; i++)
Damien Millerfef95ad2006-07-10 20:46:55 +1000464 if (handles[i].use != HANDLE_UNUSED)
465 handle_log_close(i, "forced");
466}
467
Ben Lindstrombba81212001-06-25 05:01:22 +0000468static int
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000469get_handle(struct sshbuf *queue, int *hp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100470{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000471 u_char *handle;
472 int r;
473 size_t hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000474
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000475 *hp = -1;
476 if ((r = sshbuf_get_string(queue, &handle, &hlen)) != 0)
477 return r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000478 if (hlen < 256)
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000479 *hp = handle_from_string(handle, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000480 free(handle);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000481 return 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100482}
483
484/* send replies */
485
Ben Lindstrombba81212001-06-25 05:01:22 +0000486static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000487send_msg(struct sshbuf *m)
Damien Miller7b28dc52000-09-05 13:34:53 +1100488{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000489 int r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000490
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000491 if ((r = sshbuf_put_stringb(oqueue, m)) != 0)
492 fatal("%s: buffer error: %s", __func__, ssh_err(r));
493 sshbuf_reset(m);
Damien Miller7b28dc52000-09-05 13:34:53 +1100494}
495
Damien Millerfef95ad2006-07-10 20:46:55 +1000496static const char *
497status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100498{
Damien Miller058316f2001-03-08 10:08:49 +1100499 const char *status_messages[] = {
500 "Success", /* SSH_FX_OK */
501 "End of file", /* SSH_FX_EOF */
502 "No such file", /* SSH_FX_NO_SUCH_FILE */
503 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
504 "Failure", /* SSH_FX_FAILURE */
505 "Bad message", /* SSH_FX_BAD_MESSAGE */
506 "No connection", /* SSH_FX_NO_CONNECTION */
507 "Connection lost", /* SSH_FX_CONNECTION_LOST */
508 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
509 "Unknown error" /* Others */
510 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000511 return (status_messages[MIN(status,SSH2_FX_MAX)]);
512}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000513
Damien Millerfef95ad2006-07-10 20:46:55 +1000514static void
515send_status(u_int32_t id, u_int32_t status)
516{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000517 struct sshbuf *msg;
518 int r;
Damien Millerfef95ad2006-07-10 20:46:55 +1000519
520 debug3("request %u: sent status %u", id, status);
521 if (log_level > SYSLOG_LEVEL_VERBOSE ||
522 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
523 logit("sent status %s", status_to_message(status));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000524 if ((msg = sshbuf_new()) == NULL)
525 fatal("%s: sshbuf_new failed", __func__);
526 if ((r = sshbuf_put_u8(msg, SSH2_FXP_STATUS)) != 0 ||
527 (r = sshbuf_put_u32(msg, id)) != 0 ||
528 (r = sshbuf_put_u32(msg, status)) != 0)
529 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +1100530 if (version >= 3) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000531 if ((r = sshbuf_put_cstring(msg,
532 status_to_message(status))) != 0 ||
533 (r = sshbuf_put_cstring(msg, "")) != 0)
534 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller058316f2001-03-08 10:08:49 +1100535 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000536 send_msg(msg);
537 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100538}
Ben Lindstrombba81212001-06-25 05:01:22 +0000539static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000540send_data_or_handle(char type, u_int32_t id, const u_char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100541{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000542 struct sshbuf *msg;
543 int r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000544
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000545 if ((msg = sshbuf_new()) == NULL)
546 fatal("%s: sshbuf_new failed", __func__);
547 if ((r = sshbuf_put_u8(msg, type)) != 0 ||
548 (r = sshbuf_put_u32(msg, id)) != 0 ||
549 (r = sshbuf_put_string(msg, data, dlen)) != 0)
550 fatal("%s: buffer error: %s", __func__, ssh_err(r));
551 send_msg(msg);
552 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100553}
554
Ben Lindstrombba81212001-06-25 05:01:22 +0000555static void
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000556send_data(u_int32_t id, const u_char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100557{
Damien Millerfef95ad2006-07-10 20:46:55 +1000558 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000559 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100560}
561
Ben Lindstrombba81212001-06-25 05:01:22 +0000562static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100563send_handle(u_int32_t id, int handle)
564{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000565 u_char *string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100566 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000567
Damien Miller7b28dc52000-09-05 13:34:53 +1100568 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000569 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000570 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000571 free(string);
Damien Miller7b28dc52000-09-05 13:34:53 +1100572}
573
Ben Lindstrombba81212001-06-25 05:01:22 +0000574static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100575send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100576{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000577 struct sshbuf *msg;
578 int i, r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000579
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000580 if ((msg = sshbuf_new()) == NULL)
581 fatal("%s: sshbuf_new failed", __func__);
582 if ((r = sshbuf_put_u8(msg, SSH2_FXP_NAME)) != 0 ||
583 (r = sshbuf_put_u32(msg, id)) != 0 ||
584 (r = sshbuf_put_u32(msg, count)) != 0)
585 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerfef95ad2006-07-10 20:46:55 +1000586 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100587 for (i = 0; i < count; i++) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000588 if ((r = sshbuf_put_cstring(msg, stats[i].name)) != 0 ||
589 (r = sshbuf_put_cstring(msg, stats[i].long_name)) != 0 ||
590 (r = encode_attrib(msg, &stats[i].attrib)) != 0)
591 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100592 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000593 send_msg(msg);
594 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100595}
596
Ben Lindstrombba81212001-06-25 05:01:22 +0000597static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100598send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100599{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000600 struct sshbuf *msg;
601 int r;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000602
Damien Millerfef95ad2006-07-10 20:46:55 +1000603 debug("request %u: sent attrib have 0x%x", id, a->flags);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000604 if ((msg = sshbuf_new()) == NULL)
605 fatal("%s: sshbuf_new failed", __func__);
606 if ((r = sshbuf_put_u8(msg, SSH2_FXP_ATTRS)) != 0 ||
607 (r = sshbuf_put_u32(msg, id)) != 0 ||
608 (r = encode_attrib(msg, a)) != 0)
609 fatal("%s: buffer error: %s", __func__, ssh_err(r));
610 send_msg(msg);
611 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100612}
613
Damien Millerd671e5a2008-05-19 14:53:33 +1000614static void
615send_statvfs(u_int32_t id, struct statvfs *st)
616{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000617 struct sshbuf *msg;
Damien Millerd671e5a2008-05-19 14:53:33 +1000618 u_int64_t flag;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000619 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +1000620
621 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
622 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
623
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000624 if ((msg = sshbuf_new()) == NULL)
625 fatal("%s: sshbuf_new failed", __func__);
626 if ((r = sshbuf_put_u8(msg, SSH2_FXP_EXTENDED_REPLY)) != 0 ||
627 (r = sshbuf_put_u32(msg, id)) != 0 ||
628 (r = sshbuf_put_u64(msg, st->f_bsize)) != 0 ||
629 (r = sshbuf_put_u64(msg, st->f_frsize)) != 0 ||
630 (r = sshbuf_put_u64(msg, st->f_blocks)) != 0 ||
631 (r = sshbuf_put_u64(msg, st->f_bfree)) != 0 ||
632 (r = sshbuf_put_u64(msg, st->f_bavail)) != 0 ||
633 (r = sshbuf_put_u64(msg, st->f_files)) != 0 ||
634 (r = sshbuf_put_u64(msg, st->f_ffree)) != 0 ||
635 (r = sshbuf_put_u64(msg, st->f_favail)) != 0 ||
636 (r = sshbuf_put_u64(msg, FSID_TO_ULONG(st->f_fsid))) != 0 ||
637 (r = sshbuf_put_u64(msg, flag)) != 0 ||
638 (r = sshbuf_put_u64(msg, st->f_namemax)) != 0)
639 fatal("%s: buffer error: %s", __func__, ssh_err(r));
640 send_msg(msg);
641 sshbuf_free(msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000642}
643
Damien Miller7b28dc52000-09-05 13:34:53 +1100644/* parse incoming */
645
Ben Lindstrombba81212001-06-25 05:01:22 +0000646static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100647process_init(void)
648{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000649 struct sshbuf *msg;
650 int r;
Damien Miller7b28dc52000-09-05 13:34:53 +1100651
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000652 if ((r = sshbuf_get_u32(iqueue, &version)) != 0)
653 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf145a5b2011-06-20 14:42:51 +1000654 verbose("received client version %u", version);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000655 if ((msg = sshbuf_new()) == NULL)
656 fatal("%s: sshbuf_new failed", __func__);
657 if ((r = sshbuf_put_u8(msg, SSH2_FXP_VERSION)) != 0 ||
658 (r = sshbuf_put_u32(msg, SSH2_FILEXFER_VERSION)) != 0 ||
659 /* POSIX rename extension */
660 (r = sshbuf_put_cstring(msg, "posix-rename@openssh.com")) != 0 ||
661 (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
662 /* statvfs extension */
663 (r = sshbuf_put_cstring(msg, "statvfs@openssh.com")) != 0 ||
664 (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
665 /* fstatvfs extension */
666 (r = sshbuf_put_cstring(msg, "fstatvfs@openssh.com")) != 0 ||
667 (r = sshbuf_put_cstring(msg, "2")) != 0 || /* version */
668 /* hardlink extension */
669 (r = sshbuf_put_cstring(msg, "hardlink@openssh.com")) != 0 ||
670 (r = sshbuf_put_cstring(msg, "1")) != 0 || /* version */
671 /* fsync extension */
672 (r = sshbuf_put_cstring(msg, "fsync@openssh.com")) != 0 ||
673 (r = sshbuf_put_cstring(msg, "1")) != 0) /* version */
674 fatal("%s: buffer error: %s", __func__, ssh_err(r));
675 send_msg(msg);
676 sshbuf_free(msg);
Damien Miller7b28dc52000-09-05 13:34:53 +1100677}
678
Ben Lindstrombba81212001-06-25 05:01:22 +0000679static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100680process_open(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100681{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100682 u_int32_t pflags;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000683 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100684 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000685 int r, handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100686
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000687 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
688 (r = sshbuf_get_u32(iqueue, &pflags)) != 0 || /* portable flags */
689 (r = decode_attrib(iqueue, &a)) != 0)
690 fatal("%s: buffer error: %s", __func__, ssh_err(r));
691
Damien Miller6444fe92006-07-10 21:31:27 +1000692 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100693 flags = flags_from_portable(pflags);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000694 mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a.perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000695 logit("open \"%s\" flags %s mode 0%o",
696 name, string_from_portable(pflags), mode);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100697 if (readonly &&
Damien Miller6eaeebf2013-10-15 11:55:57 +1100698 ((flags & O_ACCMODE) == O_WRONLY ||
699 (flags & O_ACCMODE) == O_RDWR)) {
700 verbose("Refusing open request in read-only mode");
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000701 status = SSH2_FX_PERMISSION_DENIED;
Damien Miller6eaeebf2013-10-15 11:55:57 +1100702 } else {
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100703 fd = open(name, flags, mode);
704 if (fd < 0) {
705 status = errno_to_portable(errno);
Damien Miller7b28dc52000-09-05 13:34:53 +1100706 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100707 handle = handle_new(HANDLE_FILE, name, fd, flags, NULL);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100708 if (handle < 0) {
709 close(fd);
710 } else {
711 send_handle(id, handle);
712 status = SSH2_FX_OK;
713 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100714 }
715 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000716 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100717 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000718 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100719}
720
Ben Lindstrombba81212001-06-25 05:01:22 +0000721static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100722process_close(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100723{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000724 int r, handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100725
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000726 if ((r = get_handle(iqueue, &handle)) != 0)
727 fatal("%s: buffer error: %s", __func__, ssh_err(r));
728
Damien Millerfef95ad2006-07-10 20:46:55 +1000729 debug3("request %u: close handle %u", id, handle);
730 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100731 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000732 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100733 send_status(id, status);
734}
735
Ben Lindstrombba81212001-06-25 05:01:22 +0000736static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100737process_read(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100738{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000739 u_char buf[64*1024];
Damien Miller6eaeebf2013-10-15 11:55:57 +1100740 u_int32_t len;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000741 int r, handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100742 u_int64_t off;
743
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000744 if ((r = get_handle(iqueue, &handle)) != 0 ||
745 (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
746 (r = sshbuf_get_u32(iqueue, &len)) != 0)
747 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100748
Damien Millerfef95ad2006-07-10 20:46:55 +1000749 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
750 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100751 if (len > sizeof buf) {
752 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000753 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100754 }
755 fd = handle_to_fd(handle);
756 if (fd >= 0) {
757 if (lseek(fd, off, SEEK_SET) < 0) {
758 error("process_read: seek failed");
759 status = errno_to_portable(errno);
760 } else {
761 ret = read(fd, buf, len);
762 if (ret < 0) {
763 status = errno_to_portable(errno);
764 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000765 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100766 } else {
767 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000768 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000769 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100770 }
771 }
772 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000773 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100774 send_status(id, status);
775}
776
Ben Lindstrombba81212001-06-25 05:01:22 +0000777static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100778process_write(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100779{
Damien Miller7b28dc52000-09-05 13:34:53 +1100780 u_int64_t off;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000781 size_t len;
782 int r, handle, fd, ret, status;
783 u_char *data;
Damien Miller7b28dc52000-09-05 13:34:53 +1100784
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000785 if ((r = get_handle(iqueue, &handle)) != 0 ||
786 (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
787 (r = sshbuf_get_string(iqueue, &data, &len)) != 0)
788 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +1100789
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000790 debug("request %u: write \"%s\" (handle %d) off %llu len %zu",
Damien Millerfef95ad2006-07-10 20:46:55 +1000791 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100792 fd = handle_to_fd(handle);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000793
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100794 if (fd < 0)
795 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100796 else {
Damien Millere9fc72e2013-10-15 12:14:12 +1100797 if (!(handle_to_flags(handle) & O_APPEND) &&
798 lseek(fd, off, SEEK_SET) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100799 status = errno_to_portable(errno);
800 error("process_write: seek failed");
801 } else {
802/* XXX ATOMICIO ? */
803 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000804 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100805 error("process_write: write failed");
806 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000807 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000808 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000809 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100810 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000811 debug2("nothing at all written");
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100812 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100813 }
814 }
815 }
816 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000817 free(data);
Damien Miller7b28dc52000-09-05 13:34:53 +1100818}
819
Ben Lindstrombba81212001-06-25 05:01:22 +0000820static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100821process_do_stat(u_int32_t id, int do_lstat)
Damien Miller7b28dc52000-09-05 13:34:53 +1100822{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000823 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100824 struct stat st;
Damien Miller7b28dc52000-09-05 13:34:53 +1100825 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000826 int r, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100827
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000828 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
829 fatal("%s: buffer error: %s", __func__, ssh_err(r));
830
Damien Millerfef95ad2006-07-10 20:46:55 +1000831 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
832 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000833 r = do_lstat ? lstat(name, &st) : stat(name, &st);
834 if (r < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100835 status = errno_to_portable(errno);
836 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000837 stat_to_attrib(&st, &a);
838 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000839 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100840 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000841 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100842 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000843 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100844}
845
Ben Lindstrombba81212001-06-25 05:01:22 +0000846static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100847process_stat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100848{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100849 process_do_stat(id, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100850}
851
Ben Lindstrombba81212001-06-25 05:01:22 +0000852static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100853process_lstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100854{
Damien Miller6eaeebf2013-10-15 11:55:57 +1100855 process_do_stat(id, 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100856}
857
Ben Lindstrombba81212001-06-25 05:01:22 +0000858static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100859process_fstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100860{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000861 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100862 struct stat st;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000863 int fd, r, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100864
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000865 if ((r = get_handle(iqueue, &handle)) != 0)
866 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerfef95ad2006-07-10 20:46:55 +1000867 debug("request %u: fstat \"%s\" (handle %u)",
868 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100869 fd = handle_to_fd(handle);
Damien Millere2334d62007-01-05 16:31:02 +1100870 if (fd >= 0) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000871 r = fstat(fd, &st);
872 if (r < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100873 status = errno_to_portable(errno);
874 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000875 stat_to_attrib(&st, &a);
876 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000877 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100878 }
879 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000880 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100881 send_status(id, status);
882}
883
Ben Lindstrombba81212001-06-25 05:01:22 +0000884static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100885attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100886{
887 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000888
Damien Miller7b28dc52000-09-05 13:34:53 +1100889 tv[0].tv_sec = a->atime;
890 tv[0].tv_usec = 0;
891 tv[1].tv_sec = a->mtime;
892 tv[1].tv_usec = 0;
893 return tv;
894}
895
Ben Lindstrombba81212001-06-25 05:01:22 +0000896static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100897process_setstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100898{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000899 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100900 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000901 int r, status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100902
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000903 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
904 (r = decode_attrib(iqueue, &a)) != 0)
905 fatal("%s: buffer error: %s", __func__, ssh_err(r));
906
Damien Millerfef95ad2006-07-10 20:46:55 +1000907 debug("request %u: setstat name \"%s\"", id, name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000908 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000909 logit("set \"%s\" size %llu",
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000910 name, (unsigned long long)a.size);
911 r = truncate(name, a.size);
912 if (r == -1)
Damien Miller00c92172002-02-13 14:05:00 +1100913 status = errno_to_portable(errno);
914 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000915 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
916 logit("set \"%s\" mode %04o", name, a.perm);
917 r = chmod(name, a.perm & 07777);
918 if (r == -1)
Damien Miller7b28dc52000-09-05 13:34:53 +1100919 status = errno_to_portable(errno);
920 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000921 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000922 char buf[64];
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000923 time_t t = a.mtime;
Damien Millerfef95ad2006-07-10 20:46:55 +1000924
925 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
926 localtime(&t));
927 logit("set \"%s\" modtime %s", name, buf);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000928 r = utimes(name, attrib_to_tv(&a));
929 if (r == -1)
Damien Miller7b28dc52000-09-05 13:34:53 +1100930 status = errno_to_portable(errno);
931 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000932 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000933 logit("set \"%s\" owner %lu group %lu", name,
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000934 (u_long)a.uid, (u_long)a.gid);
935 r = chown(name, a.uid, a.gid);
936 if (r == -1)
Kevin Steves8e743932001-02-05 13:24:35 +0000937 status = errno_to_portable(errno);
938 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100939 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000940 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100941}
942
Ben Lindstrombba81212001-06-25 05:01:22 +0000943static void
Damien Miller6eaeebf2013-10-15 11:55:57 +1100944process_fsetstat(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +1100945{
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000946 Attrib a;
947 int handle, fd, r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000948 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000949
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000950 if ((r = get_handle(iqueue, &handle)) != 0 ||
951 (r = decode_attrib(iqueue, &a)) != 0)
952 fatal("%s: buffer error: %s", __func__, ssh_err(r));
953
954 debug("request %u: fsetstat handle %d", id, handle);
955 fd = handle_to_fd(handle);
956 if (fd < 0)
957 status = SSH2_FX_FAILURE;
958 else {
959 char *name = handle_to_name(handle);
960
961 if (a.flags & SSH2_FILEXFER_ATTR_SIZE) {
962 logit("set \"%s\" size %llu",
963 name, (unsigned long long)a.size);
964 r = ftruncate(fd, a.size);
965 if (r == -1)
966 status = errno_to_portable(errno);
967 }
968 if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
969 logit("set \"%s\" mode %04o", name, a.perm);
970#ifdef HAVE_FCHMOD
Damien Miller83b96782015-01-15 02:35:50 +1100971 r = fchmod(fd, a.perm & 07777);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000972#else
Damien Miller83b96782015-01-15 02:35:50 +1100973 r = chmod(name, a.perm & 07777);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000974#endif
975 if (r == -1)
976 status = errno_to_portable(errno);
977 }
978 if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
979 char buf[64];
980 time_t t = a.mtime;
981
982 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
983 localtime(&t));
984 logit("set \"%s\" modtime %s", name, buf);
985#ifdef HAVE_FUTIMES
Damien Miller83b96782015-01-15 02:35:50 +1100986 r = futimes(fd, attrib_to_tv(&a));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000987#else
Damien Miller83b96782015-01-15 02:35:50 +1100988 r = utimes(name, attrib_to_tv(&a));
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000989#endif
990 if (r == -1)
991 status = errno_to_portable(errno);
992 }
993 if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) {
994 logit("set \"%s\" owner %lu group %lu", name,
995 (u_long)a.uid, (u_long)a.gid);
996#ifdef HAVE_FCHOWN
Damien Miller83b96782015-01-15 02:35:50 +1100997 r = fchown(fd, a.uid, a.gid);
djm@openbsd.org7d845f42015-01-14 13:54:13 +0000998#else
Damien Miller83b96782015-01-15 02:35:50 +1100999 r = chown(name, a.uid, a.gid);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001000#endif
1001 if (r == -1)
1002 status = errno_to_portable(errno);
1003 }
1004 }
1005 send_status(id, status);
1006}
Damien Miller7b28dc52000-09-05 13:34:53 +11001007
Ben Lindstrombba81212001-06-25 05:01:22 +00001008static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001009process_opendir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001010{
1011 DIR *dirp = NULL;
1012 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001013 int r, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001014
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001015 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1016 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1017
Damien Millerfef95ad2006-07-10 20:46:55 +10001018 debug3("request %u: opendir", id);
1019 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001020 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001021 if (dirp == NULL) {
1022 status = errno_to_portable(errno);
1023 } else {
Damien Millere9fc72e2013-10-15 12:14:12 +11001024 handle = handle_new(HANDLE_DIR, path, 0, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +11001025 if (handle < 0) {
1026 closedir(dirp);
1027 } else {
1028 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001029 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001030 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001031
Damien Miller7b28dc52000-09-05 13:34:53 +11001032 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001033 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +11001034 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001035 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001036}
1037
Ben Lindstrombba81212001-06-25 05:01:22 +00001038static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001039process_readdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001040{
1041 DIR *dirp;
1042 struct dirent *dp;
1043 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001044 int r, handle;
Damien Miller7b28dc52000-09-05 13:34:53 +11001045
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001046 if ((r = get_handle(iqueue, &handle)) != 0)
1047 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1048
Damien Millerfef95ad2006-07-10 20:46:55 +10001049 debug("request %u: readdir \"%s\" (handle %d)", id,
1050 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +11001051 dirp = handle_to_dir(handle);
1052 path = handle_to_name(handle);
1053 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001054 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +11001055 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +11001056 struct stat st;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001057 char pathname[PATH_MAX];
Damien Miller7b28dc52000-09-05 13:34:53 +11001058 Stat *stats;
1059 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001060
Damien Miller07d86be2006-03-26 14:19:21 +11001061 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001062 while ((dp = readdir(dirp)) != NULL) {
1063 if (count >= nstats) {
1064 nstats *= 2;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001065 stats = xreallocarray(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +11001066 }
1067/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +00001068 snprintf(pathname, sizeof pathname, "%s%s%s", path,
1069 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001070 if (lstat(pathname, &st) < 0)
1071 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001072 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +11001073 stats[count].name = xstrdup(dp->d_name);
Darren Tucker2901e2d2010-01-13 22:44:06 +11001074 stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001075 count++;
1076 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001077 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +11001078 if (count == 100)
1079 break;
1080 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001081 if (count > 0) {
1082 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001083 for (i = 0; i < count; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001084 free(stats[i].name);
1085 free(stats[i].long_name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001086 }
1087 } else {
1088 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +11001089 }
Darren Tuckera627d422013-06-02 07:31:17 +10001090 free(stats);
Damien Miller7b28dc52000-09-05 13:34:53 +11001091 }
1092}
1093
Ben Lindstrombba81212001-06-25 05:01:22 +00001094static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001095process_remove(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001096{
1097 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001098 int r, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001099
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001100 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1101 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1102
Damien Millerfef95ad2006-07-10 20:46:55 +10001103 debug3("request %u: remove", id);
1104 logit("remove name \"%s\"", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001105 r = unlink(name);
1106 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001107 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001108 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001109}
1110
Ben Lindstrombba81212001-06-25 05:01:22 +00001111static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001112process_mkdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001113{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001114 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +11001115 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001116 int r, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +11001117
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001118 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
1119 (r = decode_attrib(iqueue, &a)) != 0)
1120 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1121
1122 mode = (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
1123 a.perm & 07777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +10001124 debug3("request %u: mkdir", id);
1125 logit("mkdir name \"%s\" mode 0%o", name, mode);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001126 r = mkdir(name, mode);
1127 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001128 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001129 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001130}
1131
Ben Lindstrombba81212001-06-25 05:01:22 +00001132static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001133process_rmdir(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001134{
Damien Miller7b28dc52000-09-05 13:34:53 +11001135 char *name;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001136 int r, status;
Damien Miller7b28dc52000-09-05 13:34:53 +11001137
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001138 if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0)
1139 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1140
Damien Millerfef95ad2006-07-10 20:46:55 +10001141 debug3("request %u: rmdir", id);
1142 logit("rmdir name \"%s\"", name);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001143 r = rmdir(name);
1144 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +11001145 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001146 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001147}
1148
Ben Lindstrombba81212001-06-25 05:01:22 +00001149static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001150process_realpath(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001151{
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001152 char resolvedname[PATH_MAX];
Damien Miller7b28dc52000-09-05 13:34:53 +11001153 char *path;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001154 int r;
Damien Miller7b28dc52000-09-05 13:34:53 +11001155
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001156 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1157 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1158
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001159 if (path[0] == '\0') {
Darren Tuckera627d422013-06-02 07:31:17 +10001160 free(path);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001161 path = xstrdup(".");
1162 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001163 debug3("request %u: realpath", id);
1164 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001165 if (realpath(path, resolvedname) == NULL) {
1166 send_status(id, errno_to_portable(errno));
1167 } else {
1168 Stat s;
1169 attrib_clear(&s.attrib);
1170 s.name = s.long_name = resolvedname;
1171 send_names(id, 1, &s);
1172 }
Darren Tuckera627d422013-06-02 07:31:17 +10001173 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001174}
1175
Ben Lindstrombba81212001-06-25 05:01:22 +00001176static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001177process_rename(u_int32_t id)
Damien Miller7b28dc52000-09-05 13:34:53 +11001178{
Damien Miller7b28dc52000-09-05 13:34:53 +11001179 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001180 int r, status;
Damien Millerb3207e82003-03-26 16:01:11 +11001181 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +11001182
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001183 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1184 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1185 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1186
Damien Millerfef95ad2006-07-10 20:46:55 +10001187 debug3("request %u: rename", id);
1188 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +11001189 status = SSH2_FX_FAILURE;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001190 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +11001191 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +11001192 else if (S_ISREG(sb.st_mode)) {
1193 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001194 if (link(oldpath, newpath) == -1) {
Damien Miller0e265512009-08-28 10:43:13 +10001195 if (errno == EOPNOTSUPP || errno == ENOSYS
Darren Tuckerf7fa7062008-07-04 14:10:19 +10001196#ifdef EXDEV
1197 || errno == EXDEV
1198#endif
Darren Tuckere59b5082004-06-28 16:01:19 +10001199#ifdef LINK_OPNOTSUPP_ERRNO
1200 || errno == LINK_OPNOTSUPP_ERRNO
1201#endif
1202 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001203 struct stat st;
1204
1205 /*
1206 * fs doesn't support links, so fall back to
1207 * stat+rename. This is racy.
1208 */
1209 if (stat(newpath, &st) == -1) {
1210 if (rename(oldpath, newpath) == -1)
1211 status =
1212 errno_to_portable(errno);
1213 else
1214 status = SSH2_FX_OK;
1215 }
1216 } else {
1217 status = errno_to_portable(errno);
1218 }
1219 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001220 status = errno_to_portable(errno);
1221 /* clean spare link */
1222 unlink(newpath);
1223 } else
1224 status = SSH2_FX_OK;
1225 } else if (stat(newpath, &sb) == -1) {
1226 if (rename(oldpath, newpath) == -1)
1227 status = errno_to_portable(errno);
1228 else
1229 status = SSH2_FX_OK;
1230 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001231 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001232 free(oldpath);
1233 free(newpath);
Damien Miller7b28dc52000-09-05 13:34:53 +11001234}
1235
Ben Lindstrombba81212001-06-25 05:01:22 +00001236static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001237process_readlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001238{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001239 int r, len;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001240 char buf[PATH_MAX];
Damien Miller058316f2001-03-08 10:08:49 +11001241 char *path;
1242
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001243 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1244 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1245
Damien Millerfef95ad2006-07-10 20:46:55 +10001246 debug3("request %u: readlink", id);
1247 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001248 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001249 send_status(id, errno_to_portable(errno));
1250 else {
1251 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001252
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001253 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001254 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001255 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001256 send_names(id, 1, &s);
1257 }
Darren Tuckera627d422013-06-02 07:31:17 +10001258 free(path);
Damien Miller058316f2001-03-08 10:08:49 +11001259}
1260
Ben Lindstrombba81212001-06-25 05:01:22 +00001261static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001262process_symlink(u_int32_t id)
Damien Miller058316f2001-03-08 10:08:49 +11001263{
Damien Miller058316f2001-03-08 10:08:49 +11001264 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001265 int r, status;
Damien Miller058316f2001-03-08 10:08:49 +11001266
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001267 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1268 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1269 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1270
Damien Millerfef95ad2006-07-10 20:46:55 +10001271 debug3("request %u: symlink", id);
1272 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001273 /* this will fail if 'newpath' exists */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001274 r = symlink(oldpath, newpath);
1275 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001276 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001277 free(oldpath);
1278 free(newpath);
Damien Miller058316f2001-03-08 10:08:49 +11001279}
1280
Ben Lindstrombba81212001-06-25 05:01:22 +00001281static void
Damien Miller7c296612008-03-07 18:33:53 +11001282process_extended_posix_rename(u_int32_t id)
1283{
1284 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001285 int r, status;
Damien Miller7c296612008-03-07 18:33:53 +11001286
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001287 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1288 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1289 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1290
Damien Miller7c296612008-03-07 18:33:53 +11001291 debug3("request %u: posix-rename", id);
1292 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001293 r = rename(oldpath, newpath);
1294 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001295 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001296 free(oldpath);
1297 free(newpath);
Damien Miller7c296612008-03-07 18:33:53 +11001298}
1299
1300static void
Damien Millerd671e5a2008-05-19 14:53:33 +10001301process_extended_statvfs(u_int32_t id)
1302{
1303 char *path;
1304 struct statvfs st;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001305 int r;
Damien Millerd671e5a2008-05-19 14:53:33 +10001306
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001307 if ((r = sshbuf_get_cstring(iqueue, &path, NULL)) != 0)
1308 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker2aca1592014-01-19 15:25:34 +11001309 debug3("request %u: statvfs", id);
1310 logit("statvfs \"%s\"", path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001311
1312 if (statvfs(path, &st) != 0)
1313 send_status(id, errno_to_portable(errno));
1314 else
1315 send_statvfs(id, &st);
Darren Tuckera627d422013-06-02 07:31:17 +10001316 free(path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001317}
1318
1319static void
1320process_extended_fstatvfs(u_int32_t id)
1321{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001322 int r, handle, fd;
Damien Millerd671e5a2008-05-19 14:53:33 +10001323 struct statvfs st;
1324
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001325 if ((r = get_handle(iqueue, &handle)) != 0)
1326 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd671e5a2008-05-19 14:53:33 +10001327 debug("request %u: fstatvfs \"%s\" (handle %u)",
1328 id, handle_to_name(handle), handle);
1329 if ((fd = handle_to_fd(handle)) < 0) {
1330 send_status(id, SSH2_FX_FAILURE);
1331 return;
1332 }
1333 if (fstatvfs(fd, &st) != 0)
1334 send_status(id, errno_to_portable(errno));
1335 else
1336 send_statvfs(id, &st);
1337}
1338
1339static void
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001340process_extended_hardlink(u_int32_t id)
1341{
1342 char *oldpath, *newpath;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001343 int r, status;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001344
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001345 if ((r = sshbuf_get_cstring(iqueue, &oldpath, NULL)) != 0 ||
1346 (r = sshbuf_get_cstring(iqueue, &newpath, NULL)) != 0)
1347 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1348
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001349 debug3("request %u: hardlink", id);
1350 logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001351 r = link(oldpath, newpath);
1352 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001353 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001354 free(oldpath);
1355 free(newpath);
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001356}
1357
1358static void
Damien Millerf29238e2013-10-17 11:48:52 +11001359process_extended_fsync(u_int32_t id)
1360{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001361 int handle, fd, r, status = SSH2_FX_OP_UNSUPPORTED;
Damien Millerf29238e2013-10-17 11:48:52 +11001362
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001363 if ((r = get_handle(iqueue, &handle)) != 0)
1364 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf29238e2013-10-17 11:48:52 +11001365 debug3("request %u: fsync (handle %u)", id, handle);
1366 verbose("fsync \"%s\"", handle_to_name(handle));
1367 if ((fd = handle_to_fd(handle)) < 0)
1368 status = SSH2_FX_NO_SUCH_FILE;
1369 else if (handle_is_ok(handle, HANDLE_FILE)) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001370 r = fsync(fd);
1371 status = (r == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Millerf29238e2013-10-17 11:48:52 +11001372 }
1373 send_status(id, status);
1374}
1375
1376static void
Damien Miller6eaeebf2013-10-15 11:55:57 +11001377process_extended(u_int32_t id)
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001378{
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001379 char *request;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001380 int i, r;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001381
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001382 if ((r = sshbuf_get_cstring(iqueue, &request, NULL)) != 0)
1383 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001384 for (i = 0; extended_handlers[i].handler != NULL; i++) {
1385 if (strcmp(request, extended_handlers[i].ext_name) == 0) {
1386 if (!request_permitted(&extended_handlers[i]))
1387 send_status(id, SSH2_FX_PERMISSION_DENIED);
1388 else
1389 extended_handlers[i].handler(id);
1390 break;
1391 }
1392 }
1393 if (extended_handlers[i].handler == NULL) {
1394 error("Unknown extended request \"%.100s\"", request);
Damien Miller7c296612008-03-07 18:33:53 +11001395 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
Damien Miller6eaeebf2013-10-15 11:55:57 +11001396 }
Darren Tuckera627d422013-06-02 07:31:17 +10001397 free(request);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001398}
Damien Miller7b28dc52000-09-05 13:34:53 +11001399
1400/* stolen from ssh-agent */
1401
Ben Lindstrombba81212001-06-25 05:01:22 +00001402static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001403process(void)
1404{
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001405 u_int msg_len;
1406 u_int buf_len;
1407 u_int consumed;
1408 u_char type;
1409 const u_char *cp;
1410 int i, r;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001411 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +11001412
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001413 buf_len = sshbuf_len(iqueue);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001414 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001415 return; /* Incomplete message. */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001416 cp = sshbuf_ptr(iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001417 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001418 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001419 error("bad message from %s local user %s",
1420 client_addr, pw->pw_name);
Damien Millerdfc24252008-02-10 22:29:40 +11001421 sftp_server_cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001422 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001423 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001424 return;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001425 if ((r = sshbuf_consume(iqueue, 4)) != 0)
1426 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom2c140472002-06-06 21:57:54 +00001427 buf_len -= 4;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001428 if ((r = sshbuf_get_u8(iqueue, &type)) != 0)
1429 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001430
Damien Miller7b28dc52000-09-05 13:34:53 +11001431 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001432 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001433 process_init();
Damien Miller6eaeebf2013-10-15 11:55:57 +11001434 init_done = 1;
Damien Miller058316f2001-03-08 10:08:49 +11001435 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001436 case SSH2_FXP_EXTENDED:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001437 if (!init_done)
1438 fatal("Received extended request before init");
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001439 if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1440 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001441 process_extended(id);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001442 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001443 default:
Damien Miller6eaeebf2013-10-15 11:55:57 +11001444 if (!init_done)
1445 fatal("Received %u request before init", type);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001446 if ((r = sshbuf_get_u32(iqueue, &id)) != 0)
1447 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller6eaeebf2013-10-15 11:55:57 +11001448 for (i = 0; handlers[i].handler != NULL; i++) {
1449 if (type == handlers[i].type) {
1450 if (!request_permitted(&handlers[i])) {
1451 send_status(id,
1452 SSH2_FX_PERMISSION_DENIED);
1453 } else {
1454 handlers[i].handler(id);
1455 }
1456 break;
1457 }
1458 }
1459 if (handlers[i].handler == NULL)
1460 error("Unknown message %u", type);
Damien Miller7b28dc52000-09-05 13:34:53 +11001461 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001462 /* discard the remaining bytes from the current packet */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001463 if (buf_len < sshbuf_len(iqueue)) {
Damien Millerdfc24252008-02-10 22:29:40 +11001464 error("iqueue grew unexpectedly");
1465 sftp_server_cleanup_exit(255);
1466 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001467 consumed = buf_len - sshbuf_len(iqueue);
Damien Millerdfc24252008-02-10 22:29:40 +11001468 if (msg_len < consumed) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001469 error("msg_len %u < consumed %u", msg_len, consumed);
Damien Millerdfc24252008-02-10 22:29:40 +11001470 sftp_server_cleanup_exit(255);
1471 }
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001472 if (msg_len > consumed &&
1473 (r = sshbuf_consume(iqueue, msg_len - consumed)) != 0)
1474 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001475}
1476
Damien Millerfef95ad2006-07-10 20:46:55 +10001477/* Cleanup handler that logs active handles upon normal exit */
1478void
Damien Millerdfc24252008-02-10 22:29:40 +11001479sftp_server_cleanup_exit(int i)
Damien Millerfef95ad2006-07-10 20:46:55 +10001480{
1481 if (pw != NULL && client_addr != NULL) {
1482 handle_log_exit();
1483 logit("session closed for local user %s from [%s]",
1484 pw->pw_name, client_addr);
1485 }
1486 _exit(i);
1487}
1488
1489static void
Damien Millerdfc24252008-02-10 22:29:40 +11001490sftp_server_usage(void)
Damien Millerfef95ad2006-07-10 20:46:55 +10001491{
1492 extern char *__progname;
1493
1494 fprintf(stderr,
Damien Milleraa7ad302013-01-09 15:58:21 +11001495 "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
Damien Miller6efab272013-10-15 12:07:05 +11001496 "[-l log_level]\n\t[-P blacklisted_requests] "
1497 "[-p whitelisted_requests] [-u umask]\n"
1498 " %s -Q protocol_feature\n",
1499 __progname, __progname);
Damien Millerfef95ad2006-07-10 20:46:55 +10001500 exit(1);
1501}
1502
Damien Miller7b28dc52000-09-05 13:34:53 +11001503int
Damien Millerd8cb1f12008-02-10 22:40:12 +11001504sftp_server_main(int argc, char **argv, struct passwd *user_pw)
Damien Miller7b28dc52000-09-05 13:34:53 +11001505{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001506 fd_set *rset, *wset;
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001507 int i, r, in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001508 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001509 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
Damien Miller502ab0e2013-01-09 15:57:36 +11001510 char *cp, *homedir = NULL, buf[4*4096];
Damien Miller07331212010-11-05 10:20:31 +11001511 long mask;
Damien Millerfef95ad2006-07-10 20:46:55 +10001512
Damien Millerfef95ad2006-07-10 20:46:55 +10001513 extern char *optarg;
1514 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001515
Damien Millerfef95ad2006-07-10 20:46:55 +10001516 __progname = ssh_get_progname(argv[0]);
1517 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001518
Damien Miller502ab0e2013-01-09 15:57:36 +11001519 pw = pwcopy(user_pw);
1520
Damien Miller6eaeebf2013-10-15 11:55:57 +11001521 while (!skipargs && (ch = getopt(argc, argv,
1522 "d:f:l:P:p:Q:u:cehR")) != -1) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001523 switch (ch) {
Damien Miller6eaeebf2013-10-15 11:55:57 +11001524 case 'Q':
1525 if (strcasecmp(optarg, "requests") != 0) {
1526 fprintf(stderr, "Invalid query type\n");
1527 exit(1);
1528 }
1529 for (i = 0; handlers[i].handler != NULL; i++)
1530 printf("%s\n", handlers[i].name);
1531 for (i = 0; extended_handlers[i].handler != NULL; i++)
1532 printf("%s\n", extended_handlers[i].name);
1533 exit(0);
1534 break;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001535 case 'R':
1536 readonly = 1;
1537 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001538 case 'c':
1539 /*
1540 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001541 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001542 */
1543 skipargs = 1;
1544 break;
1545 case 'e':
1546 log_stderr = 1;
1547 break;
1548 case 'l':
1549 log_level = log_level_number(optarg);
1550 if (log_level == SYSLOG_LEVEL_NOT_SET)
1551 error("Invalid log level \"%s\"", optarg);
1552 break;
1553 case 'f':
1554 log_facility = log_facility_number(optarg);
Damien Miller35e18db2007-09-17 16:11:33 +10001555 if (log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Millerfef95ad2006-07-10 20:46:55 +10001556 error("Invalid log facility \"%s\"", optarg);
1557 break;
Damien Miller502ab0e2013-01-09 15:57:36 +11001558 case 'd':
1559 cp = tilde_expand_filename(optarg, user_pw->pw_uid);
1560 homedir = percent_expand(cp, "d", user_pw->pw_dir,
1561 "u", user_pw->pw_name, (char *)NULL);
1562 free(cp);
1563 break;
Damien Miller6eaeebf2013-10-15 11:55:57 +11001564 case 'p':
1565 if (request_whitelist != NULL)
1566 fatal("Permitted requests already set");
1567 request_whitelist = xstrdup(optarg);
1568 break;
1569 case 'P':
1570 if (request_blacklist != NULL)
1571 fatal("Refused requests already set");
1572 request_blacklist = xstrdup(optarg);
1573 break;
Darren Tucker7dc48502009-10-07 08:44:42 +11001574 case 'u':
Damien Miller07331212010-11-05 10:20:31 +11001575 errno = 0;
1576 mask = strtol(optarg, &cp, 8);
1577 if (mask < 0 || mask > 0777 || *cp != '\0' ||
1578 cp == optarg || (mask == 0 && errno != 0))
1579 fatal("Invalid umask \"%s\"", optarg);
1580 (void)umask((mode_t)mask);
Darren Tucker7dc48502009-10-07 08:44:42 +11001581 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001582 case 'h':
1583 default:
Damien Millerdfc24252008-02-10 22:29:40 +11001584 sftp_server_usage();
Damien Millerfef95ad2006-07-10 20:46:55 +10001585 }
1586 }
1587
1588 log_init(__progname, log_level, log_facility, log_stderr);
1589
Damien Miller9c1dede2014-08-24 03:01:06 +10001590#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
Damien Miller14928b72014-04-01 14:38:07 +11001591 /*
1592 * On Linux, we should try to avoid making /proc/self/{mem,maps}
1593 * available to the user so that sftp access doesn't automatically
1594 * imply arbitrary code execution access that will break
1595 * restricted configurations.
1596 */
1597 if (prctl(PR_SET_DUMPABLE, 0) != 0)
1598 fatal("unable to make the process undumpable");
Damien Miller9c1dede2014-08-24 03:01:06 +10001599#endif /* defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE) */
Damien Miller14928b72014-04-01 14:38:07 +11001600
Damien Millerfef95ad2006-07-10 20:46:55 +10001601 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1602 client_addr = xstrdup(cp);
Damien Millerdfc24252008-02-10 22:29:40 +11001603 if ((cp = strchr(client_addr, ' ')) == NULL) {
1604 error("Malformed SSH_CONNECTION variable: \"%s\"",
Damien Millerfef95ad2006-07-10 20:46:55 +10001605 getenv("SSH_CONNECTION"));
Damien Millerdfc24252008-02-10 22:29:40 +11001606 sftp_server_cleanup_exit(255);
1607 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001608 *cp = '\0';
1609 } else
1610 client_addr = xstrdup("UNKNOWN");
1611
Damien Millerfef95ad2006-07-10 20:46:55 +10001612 logit("session opened for local user %s from [%s]",
1613 pw->pw_name, client_addr);
1614
Darren Tuckeraaf51d22010-01-08 19:04:49 +11001615 in = STDIN_FILENO;
1616 out = STDOUT_FILENO;
Damien Miller7b28dc52000-09-05 13:34:53 +11001617
Damien Miller402b3312001-04-14 00:28:42 +10001618#ifdef HAVE_CYGWIN
1619 setmode(in, O_BINARY);
1620 setmode(out, O_BINARY);
1621#endif
1622
Damien Miller7b28dc52000-09-05 13:34:53 +11001623 max = 0;
1624 if (in > max)
1625 max = in;
1626 if (out > max)
1627 max = out;
1628
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001629 if ((iqueue = sshbuf_new()) == NULL)
1630 fatal("%s: sshbuf_new failed", __func__);
1631 if ((oqueue = sshbuf_new()) == NULL)
1632 fatal("%s: sshbuf_new failed", __func__);
Damien Miller7b28dc52000-09-05 13:34:53 +11001633
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001634 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001635 rset = xmalloc(set_size);
1636 wset = xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001637
Damien Miller502ab0e2013-01-09 15:57:36 +11001638 if (homedir != NULL) {
1639 if (chdir(homedir) != 0) {
1640 error("chdir to \"%s\" failed: %s", homedir,
1641 strerror(errno));
1642 }
1643 }
1644
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001645 for (;;) {
1646 memset(rset, 0, set_size);
1647 memset(wset, 0, set_size);
1648
Darren Tuckere9405982007-05-20 15:09:04 +10001649 /*
1650 * Ensure that we can read a full buffer and handle
1651 * the worst-case length packet it can generate,
1652 * otherwise apply backpressure by stopping reads.
1653 */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001654 if ((r = sshbuf_check_reserve(iqueue, sizeof(buf))) == 0 &&
1655 (r = sshbuf_check_reserve(oqueue,
1656 SFTP_MAX_MSG_LENGTH)) == 0)
Darren Tuckere9405982007-05-20 15:09:04 +10001657 FD_SET(in, rset);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001658 else if (r != SSH_ERR_NO_BUFFER_SPACE)
1659 fatal("%s: sshbuf_check_reserve failed: %s",
1660 __func__, ssh_err(r));
Darren Tuckere9405982007-05-20 15:09:04 +10001661
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001662 olen = sshbuf_len(oqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +11001663 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001664 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001665
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001666 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001667 if (errno == EINTR)
1668 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001669 error("select: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001670 sftp_server_cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001671 }
1672
1673 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001674 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001675 len = read(in, buf, sizeof buf);
1676 if (len == 0) {
1677 debug("read eof");
Damien Millerdfc24252008-02-10 22:29:40 +11001678 sftp_server_cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001679 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001680 error("read: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001681 sftp_server_cleanup_exit(1);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001682 } else if ((r = sshbuf_put(iqueue, buf, len)) != 0) {
1683 fatal("%s: buffer error: %s",
1684 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001685 }
1686 }
1687 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001688 if (FD_ISSET(out, wset)) {
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001689 len = write(out, sshbuf_ptr(oqueue), olen);
Damien Miller7b28dc52000-09-05 13:34:53 +11001690 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001691 error("write: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001692 sftp_server_cleanup_exit(1);
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001693 } else if ((r = sshbuf_consume(oqueue, len)) != 0) {
1694 fatal("%s: buffer error: %s",
1695 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001696 }
1697 }
Darren Tuckere9405982007-05-20 15:09:04 +10001698
1699 /*
1700 * Process requests from client if we can fit the results
1701 * into the output buffer, otherwise stop processing input
1702 * and let the output queue drain.
1703 */
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001704 r = sshbuf_check_reserve(oqueue, SFTP_MAX_MSG_LENGTH);
1705 if (r == 0)
Darren Tuckere9405982007-05-20 15:09:04 +10001706 process();
djm@openbsd.org7d845f42015-01-14 13:54:13 +00001707 else if (r != SSH_ERR_NO_BUFFER_SPACE)
1708 fatal("%s: sshbuf_check_reserve: %s",
1709 __func__, ssh_err(r));
Damien Miller7b28dc52000-09-05 13:34:53 +11001710 }
1711}