blob: 285f21aafe706e05e5ad7b268e600b0fb93b7b6b [file] [log] [blame]
Darren Tuckera627d422013-06-02 07:31:17 +10001/* $OpenBSD: sftp-server.c,v 1.97 2013/05/17 00:13:14 djm Exp $ */
Damien Miller7b28dc52000-09-05 13:34:53 +11002/*
Darren Tucker37bd3662004-02-24 09:19:15 +11003 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
Damien Miller7b28dc52000-09-05 13:34:53 +11004 *
Darren Tucker37bd3662004-02-24 09:19:15 +11005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Miller7b28dc52000-09-05 13:34:53 +11008 *
Darren Tucker37bd3662004-02-24 09:19:15 +11009 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller7b28dc52000-09-05 13:34:53 +110016 */
Damien Millerd7834352006-08-05 12:39:39 +100017
Damien Miller7b28dc52000-09-05 13:34:53 +110018#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110019
20#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100021#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110022#include <sys/stat.h>
Damien Miller9aec9192006-08-05 10:57:45 +100023#ifdef HAVE_SYS_TIME_H
24# include <sys/time.h>
25#endif
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100026#ifdef HAVE_SYS_MOUNT_H
Damien Millerd671e5a2008-05-19 14:53:33 +100027#include <sys/mount.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100028#endif
29#ifdef HAVE_SYS_STATVFS_H
Damien Millerd671e5a2008-05-19 14:53:33 +100030#include <sys/statvfs.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100031#endif
Damien Miller88f254b2006-03-15 11:25:13 +110032
33#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100035#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100036#include <pwd.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100037#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100040#include <pwd.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100041#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100042#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100043#include <stdarg.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110044
Damien Miller7b28dc52000-09-05 13:34:53 +110045#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100046#include "buffer.h"
47#include "log.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100048#include "misc.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100049#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110050
Ben Lindstrom2f959b42001-01-11 06:20:23 +000051#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110052#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110053
54/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000055#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110056#define get_int() buffer_get_int(&iqueue);
57#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Damien Miller7b28dc52000-09-05 13:34:53 +110058
Damien Millerfef95ad2006-07-10 20:46:55 +100059/* Our verbosity */
60LogLevel log_level = SYSLOG_LEVEL_ERROR;
61
62/* Our client */
63struct passwd *pw = NULL;
64char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000065
Damien Miller7b28dc52000-09-05 13:34:53 +110066/* input and output queue */
67Buffer iqueue;
68Buffer oqueue;
69
Damien Miller058316f2001-03-08 10:08:49 +110070/* Version of client */
Damien Millerf145a5b2011-06-20 14:42:51 +100071u_int version;
Damien Miller058316f2001-03-08 10:08:49 +110072
Darren Tuckerdb7bf822010-01-09 22:24:33 +110073/* Disable writes */
74int readonly;
75
Darren Tuckera6612d42003-06-28 12:39:03 +100076/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110077
Damien Miller7b28dc52000-09-05 13:34:53 +110078typedef struct Stat Stat;
79
Damien Miller33804262001-02-04 23:20:18 +110080struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110081 char *name;
82 char *long_name;
83 Attrib attrib;
84};
85
Ben Lindstrombba81212001-06-25 05:01:22 +000086static int
Damien Miller7b28dc52000-09-05 13:34:53 +110087errno_to_portable(int unixerrno)
88{
89 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000090
Damien Miller7b28dc52000-09-05 13:34:53 +110091 switch (unixerrno) {
92 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000093 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110094 break;
95 case ENOENT:
96 case ENOTDIR:
97 case EBADF:
98 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000099 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100100 break;
101 case EPERM:
102 case EACCES:
103 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000104 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +1100105 break;
106 case ENAMETOOLONG:
107 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000108 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100109 break;
Darren Tucker422c34c2008-06-09 22:48:31 +1000110 case ENOSYS:
111 ret = SSH2_FX_OP_UNSUPPORTED;
112 break;
Damien Miller7b28dc52000-09-05 13:34:53 +1100113 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000114 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100115 break;
116 }
117 return ret;
118}
119
Ben Lindstrombba81212001-06-25 05:01:22 +0000120static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100121flags_from_portable(int pflags)
122{
123 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000124
Ben Lindstrom36592512001-03-05 05:02:08 +0000125 if ((pflags & SSH2_FXF_READ) &&
126 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100127 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000128 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100129 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000130 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100131 flags = O_WRONLY;
132 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000133 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100134 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000135 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100136 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000137 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100138 flags |= O_EXCL;
139 return flags;
140}
141
Damien Millerfef95ad2006-07-10 20:46:55 +1000142static const char *
143string_from_portable(int pflags)
144{
145 static char ret[128];
146
147 *ret = '\0';
148
149#define PAPPEND(str) { \
150 if (*ret != '\0') \
151 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000152 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000153 }
154
155 if (pflags & SSH2_FXF_READ)
156 PAPPEND("READ")
157 if (pflags & SSH2_FXF_WRITE)
158 PAPPEND("WRITE")
159 if (pflags & SSH2_FXF_CREAT)
160 PAPPEND("CREATE")
161 if (pflags & SSH2_FXF_TRUNC)
162 PAPPEND("TRUNCATE")
163 if (pflags & SSH2_FXF_EXCL)
164 PAPPEND("EXCL")
165
166 return ret;
167}
168
Ben Lindstrombba81212001-06-25 05:01:22 +0000169static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100170get_attrib(void)
171{
172 return decode_attrib(&iqueue);
173}
174
175/* handle handles */
176
177typedef struct Handle Handle;
178struct Handle {
179 int use;
180 DIR *dirp;
181 int fd;
182 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000183 u_int64_t bytes_read, bytes_write;
Damien Miller3397d0e2008-02-10 22:26:51 +1100184 int next_unused;
Damien Miller7b28dc52000-09-05 13:34:53 +1100185};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000186
Damien Miller7b28dc52000-09-05 13:34:53 +1100187enum {
188 HANDLE_UNUSED,
189 HANDLE_DIR,
190 HANDLE_FILE
191};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000192
Damien Miller3397d0e2008-02-10 22:26:51 +1100193Handle *handles = NULL;
194u_int num_handles = 0;
195int first_unused_handle = -1;
Damien Miller7b28dc52000-09-05 13:34:53 +1100196
Damien Miller3397d0e2008-02-10 22:26:51 +1100197static void handle_unused(int i)
Damien Miller7b28dc52000-09-05 13:34:53 +1100198{
Damien Miller3397d0e2008-02-10 22:26:51 +1100199 handles[i].use = HANDLE_UNUSED;
200 handles[i].next_unused = first_unused_handle;
201 first_unused_handle = i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100202}
203
Ben Lindstrombba81212001-06-25 05:01:22 +0000204static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100205handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100206{
Damien Miller3397d0e2008-02-10 22:26:51 +1100207 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000208
Damien Miller3397d0e2008-02-10 22:26:51 +1100209 if (first_unused_handle == -1) {
210 if (num_handles + 1 <= num_handles)
211 return -1;
212 num_handles++;
213 handles = xrealloc(handles, num_handles, sizeof(Handle));
214 handle_unused(num_handles - 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100215 }
Damien Miller3397d0e2008-02-10 22:26:51 +1100216
217 i = first_unused_handle;
218 first_unused_handle = handles[i].next_unused;
219
220 handles[i].use = use;
221 handles[i].dirp = dirp;
222 handles[i].fd = fd;
223 handles[i].name = xstrdup(name);
224 handles[i].bytes_read = handles[i].bytes_write = 0;
225
226 return i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100227}
228
Ben Lindstrombba81212001-06-25 05:01:22 +0000229static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100230handle_is_ok(int i, int type)
231{
Damien Miller3397d0e2008-02-10 22:26:51 +1100232 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
Damien Miller7b28dc52000-09-05 13:34:53 +1100233}
234
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100236handle_to_string(int handle, char **stringp, int *hlenp)
237{
Damien Miller7b28dc52000-09-05 13:34:53 +1100238 if (stringp == NULL || hlenp == NULL)
239 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000240 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100241 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000242 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100243 return 0;
244}
245
Ben Lindstrombba81212001-06-25 05:01:22 +0000246static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100247handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100248{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000249 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000250
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000251 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100252 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100253 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100254 if (handle_is_ok(val, HANDLE_FILE) ||
255 handle_is_ok(val, HANDLE_DIR))
256 return val;
257 return -1;
258}
259
Ben Lindstrombba81212001-06-25 05:01:22 +0000260static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100261handle_to_name(int handle)
262{
263 if (handle_is_ok(handle, HANDLE_DIR)||
264 handle_is_ok(handle, HANDLE_FILE))
265 return handles[handle].name;
266 return NULL;
267}
268
Ben Lindstrombba81212001-06-25 05:01:22 +0000269static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100270handle_to_dir(int handle)
271{
272 if (handle_is_ok(handle, HANDLE_DIR))
273 return handles[handle].dirp;
274 return NULL;
275}
276
Ben Lindstrombba81212001-06-25 05:01:22 +0000277static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100278handle_to_fd(int handle)
279{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000280 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100281 return handles[handle].fd;
282 return -1;
283}
284
Damien Millerfef95ad2006-07-10 20:46:55 +1000285static void
286handle_update_read(int handle, ssize_t bytes)
287{
288 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
289 handles[handle].bytes_read += bytes;
290}
291
292static void
293handle_update_write(int handle, ssize_t bytes)
294{
295 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
296 handles[handle].bytes_write += bytes;
297}
298
299static u_int64_t
300handle_bytes_read(int handle)
301{
302 if (handle_is_ok(handle, HANDLE_FILE))
303 return (handles[handle].bytes_read);
304 return 0;
305}
306
307static u_int64_t
308handle_bytes_write(int handle)
309{
310 if (handle_is_ok(handle, HANDLE_FILE))
311 return (handles[handle].bytes_write);
312 return 0;
313}
314
Ben Lindstrombba81212001-06-25 05:01:22 +0000315static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100316handle_close(int handle)
317{
318 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000319
Damien Miller7b28dc52000-09-05 13:34:53 +1100320 if (handle_is_ok(handle, HANDLE_FILE)) {
321 ret = close(handles[handle].fd);
Darren Tuckera627d422013-06-02 07:31:17 +1000322 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100323 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100324 } else if (handle_is_ok(handle, HANDLE_DIR)) {
325 ret = closedir(handles[handle].dirp);
Darren Tuckera627d422013-06-02 07:31:17 +1000326 free(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100327 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100328 } else {
329 errno = ENOENT;
330 }
331 return ret;
332}
333
Damien Millerfef95ad2006-07-10 20:46:55 +1000334static void
335handle_log_close(int handle, char *emsg)
336{
337 if (handle_is_ok(handle, HANDLE_FILE)) {
338 logit("%s%sclose \"%s\" bytes read %llu written %llu",
339 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
340 handle_to_name(handle),
Darren Tucker86473c52007-05-20 14:59:32 +1000341 (unsigned long long)handle_bytes_read(handle),
342 (unsigned long long)handle_bytes_write(handle));
Damien Millerfef95ad2006-07-10 20:46:55 +1000343 } else {
344 logit("%s%sclosedir \"%s\"",
345 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
346 handle_to_name(handle));
347 }
348}
349
350static void
351handle_log_exit(void)
352{
353 u_int i;
354
Damien Miller3397d0e2008-02-10 22:26:51 +1100355 for (i = 0; i < num_handles; i++)
Damien Millerfef95ad2006-07-10 20:46:55 +1000356 if (handles[i].use != HANDLE_UNUSED)
357 handle_log_close(i, "forced");
358}
359
Ben Lindstrombba81212001-06-25 05:01:22 +0000360static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100361get_handle(void)
362{
363 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000364 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100365 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000366
Damien Miller7b28dc52000-09-05 13:34:53 +1100367 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000368 if (hlen < 256)
369 val = handle_from_string(handle, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000370 free(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100371 return val;
372}
373
374/* send replies */
375
Ben Lindstrombba81212001-06-25 05:01:22 +0000376static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100377send_msg(Buffer *m)
378{
379 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000380
Damien Miller7b28dc52000-09-05 13:34:53 +1100381 buffer_put_int(&oqueue, mlen);
382 buffer_append(&oqueue, buffer_ptr(m), mlen);
383 buffer_consume(m, mlen);
384}
385
Damien Millerfef95ad2006-07-10 20:46:55 +1000386static const char *
387status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100388{
Damien Miller058316f2001-03-08 10:08:49 +1100389 const char *status_messages[] = {
390 "Success", /* SSH_FX_OK */
391 "End of file", /* SSH_FX_EOF */
392 "No such file", /* SSH_FX_NO_SUCH_FILE */
393 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
394 "Failure", /* SSH_FX_FAILURE */
395 "Bad message", /* SSH_FX_BAD_MESSAGE */
396 "No connection", /* SSH_FX_NO_CONNECTION */
397 "Connection lost", /* SSH_FX_CONNECTION_LOST */
398 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
399 "Unknown error" /* Others */
400 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000401 return (status_messages[MIN(status,SSH2_FX_MAX)]);
402}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000403
Damien Millerfef95ad2006-07-10 20:46:55 +1000404static void
405send_status(u_int32_t id, u_int32_t status)
406{
407 Buffer msg;
408
409 debug3("request %u: sent status %u", id, status);
410 if (log_level > SYSLOG_LEVEL_VERBOSE ||
411 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
412 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100413 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000414 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100415 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000416 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100417 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000418 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100419 buffer_put_cstring(&msg, "");
420 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100421 send_msg(&msg);
422 buffer_free(&msg);
423}
Ben Lindstrombba81212001-06-25 05:01:22 +0000424static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100425send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100426{
427 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000428
Damien Miller7b28dc52000-09-05 13:34:53 +1100429 buffer_init(&msg);
430 buffer_put_char(&msg, type);
431 buffer_put_int(&msg, id);
432 buffer_put_string(&msg, data, dlen);
433 send_msg(&msg);
434 buffer_free(&msg);
435}
436
Ben Lindstrombba81212001-06-25 05:01:22 +0000437static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100438send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100439{
Damien Millerfef95ad2006-07-10 20:46:55 +1000440 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000441 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100442}
443
Ben Lindstrombba81212001-06-25 05:01:22 +0000444static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100445send_handle(u_int32_t id, int handle)
446{
447 char *string;
448 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000449
Damien Miller7b28dc52000-09-05 13:34:53 +1100450 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000451 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000452 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000453 free(string);
Damien Miller7b28dc52000-09-05 13:34:53 +1100454}
455
Ben Lindstrombba81212001-06-25 05:01:22 +0000456static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100457send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100458{
459 Buffer msg;
460 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000461
Damien Miller7b28dc52000-09-05 13:34:53 +1100462 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000463 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100464 buffer_put_int(&msg, id);
465 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000466 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100467 for (i = 0; i < count; i++) {
468 buffer_put_cstring(&msg, stats[i].name);
469 buffer_put_cstring(&msg, stats[i].long_name);
470 encode_attrib(&msg, &stats[i].attrib);
471 }
472 send_msg(&msg);
473 buffer_free(&msg);
474}
475
Ben Lindstrombba81212001-06-25 05:01:22 +0000476static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100477send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100478{
479 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000480
Damien Millerfef95ad2006-07-10 20:46:55 +1000481 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100482 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000483 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100484 buffer_put_int(&msg, id);
485 encode_attrib(&msg, a);
486 send_msg(&msg);
487 buffer_free(&msg);
488}
489
Damien Millerd671e5a2008-05-19 14:53:33 +1000490static void
491send_statvfs(u_int32_t id, struct statvfs *st)
492{
493 Buffer msg;
494 u_int64_t flag;
495
496 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
497 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
498
499 buffer_init(&msg);
500 buffer_put_char(&msg, SSH2_FXP_EXTENDED_REPLY);
501 buffer_put_int(&msg, id);
Darren Tucker3463aca2008-06-09 23:06:55 +1000502 buffer_put_int64(&msg, st->f_bsize);
503 buffer_put_int64(&msg, st->f_frsize);
Damien Millerd671e5a2008-05-19 14:53:33 +1000504 buffer_put_int64(&msg, st->f_blocks);
505 buffer_put_int64(&msg, st->f_bfree);
506 buffer_put_int64(&msg, st->f_bavail);
507 buffer_put_int64(&msg, st->f_files);
508 buffer_put_int64(&msg, st->f_ffree);
509 buffer_put_int64(&msg, st->f_favail);
Darren Tucker77001382008-06-09 06:17:53 +1000510 buffer_put_int64(&msg, FSID_TO_ULONG(st->f_fsid));
Darren Tucker3463aca2008-06-09 23:06:55 +1000511 buffer_put_int64(&msg, flag);
512 buffer_put_int64(&msg, st->f_namemax);
Damien Millerd671e5a2008-05-19 14:53:33 +1000513 send_msg(&msg);
514 buffer_free(&msg);
515}
516
Damien Miller7b28dc52000-09-05 13:34:53 +1100517/* parse incoming */
518
Ben Lindstrombba81212001-06-25 05:01:22 +0000519static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100520process_init(void)
521{
522 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100523
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000524 version = get_int();
Damien Millerf145a5b2011-06-20 14:42:51 +1000525 verbose("received client version %u", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100526 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000527 buffer_put_char(&msg, SSH2_FXP_VERSION);
528 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7c296612008-03-07 18:33:53 +1100529 /* POSIX rename extension */
530 buffer_put_cstring(&msg, "posix-rename@openssh.com");
531 buffer_put_cstring(&msg, "1"); /* version */
Damien Millera7e0d5a2008-05-19 16:08:41 +1000532 /* statvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000533 buffer_put_cstring(&msg, "statvfs@openssh.com");
Darren Tucker294b8412008-06-08 12:57:08 +1000534 buffer_put_cstring(&msg, "2"); /* version */
Damien Millera7e0d5a2008-05-19 16:08:41 +1000535 /* fstatvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000536 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
Darren Tucker294b8412008-06-08 12:57:08 +1000537 buffer_put_cstring(&msg, "2"); /* version */
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100538 /* hardlink extension */
539 buffer_put_cstring(&msg, "hardlink@openssh.com");
540 buffer_put_cstring(&msg, "1"); /* version */
Damien Miller7b28dc52000-09-05 13:34:53 +1100541 send_msg(&msg);
542 buffer_free(&msg);
543}
544
Ben Lindstrombba81212001-06-25 05:01:22 +0000545static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100546process_open(void)
547{
548 u_int32_t id, pflags;
549 Attrib *a;
550 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000551 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100552
553 id = get_int();
554 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000555 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000556 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100557 a = get_attrib();
558 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000559 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000560 logit("open \"%s\" flags %s mode 0%o",
561 name, string_from_portable(pflags), mode);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100562 if (readonly &&
563 ((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR))
564 status = SSH2_FX_PERMISSION_DENIED;
565 else {
566 fd = open(name, flags, mode);
567 if (fd < 0) {
568 status = errno_to_portable(errno);
Damien Miller7b28dc52000-09-05 13:34:53 +1100569 } else {
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100570 handle = handle_new(HANDLE_FILE, name, fd, NULL);
571 if (handle < 0) {
572 close(fd);
573 } else {
574 send_handle(id, handle);
575 status = SSH2_FX_OK;
576 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100577 }
578 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000579 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100580 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000581 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100582}
583
Ben Lindstrombba81212001-06-25 05:01:22 +0000584static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100585process_close(void)
586{
587 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000588 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100589
590 id = get_int();
591 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000592 debug3("request %u: close handle %u", id, handle);
593 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100594 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000595 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100596 send_status(id, status);
597}
598
Ben Lindstrombba81212001-06-25 05:01:22 +0000599static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100600process_read(void)
601{
602 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000603 u_int32_t id, len;
604 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100605 u_int64_t off;
606
607 id = get_int();
608 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000609 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100610 len = get_int();
611
Damien Millerfef95ad2006-07-10 20:46:55 +1000612 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
613 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100614 if (len > sizeof buf) {
615 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000616 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100617 }
618 fd = handle_to_fd(handle);
619 if (fd >= 0) {
620 if (lseek(fd, off, SEEK_SET) < 0) {
621 error("process_read: seek failed");
622 status = errno_to_portable(errno);
623 } else {
624 ret = read(fd, buf, len);
625 if (ret < 0) {
626 status = errno_to_portable(errno);
627 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000628 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100629 } else {
630 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000631 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000632 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100633 }
634 }
635 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000636 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100637 send_status(id, status);
638}
639
Ben Lindstrombba81212001-06-25 05:01:22 +0000640static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100641process_write(void)
642{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000643 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100644 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100645 u_int len;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100646 int handle, fd, ret, status;
Damien Miller7b28dc52000-09-05 13:34:53 +1100647 char *data;
648
649 id = get_int();
650 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000651 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100652 data = get_string(&len);
653
Damien Millerfef95ad2006-07-10 20:46:55 +1000654 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
655 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100656 fd = handle_to_fd(handle);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100657
658 if (fd < 0)
659 status = SSH2_FX_FAILURE;
660 else if (readonly)
661 status = SSH2_FX_PERMISSION_DENIED;
662 else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100663 if (lseek(fd, off, SEEK_SET) < 0) {
664 status = errno_to_portable(errno);
665 error("process_write: seek failed");
666 } else {
667/* XXX ATOMICIO ? */
668 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000669 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100670 error("process_write: write failed");
671 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000672 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000673 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000674 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100675 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000676 debug2("nothing at all written");
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100677 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100678 }
679 }
680 }
681 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000682 free(data);
Damien Miller7b28dc52000-09-05 13:34:53 +1100683}
684
Ben Lindstrombba81212001-06-25 05:01:22 +0000685static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100686process_do_stat(int do_lstat)
687{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000688 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100689 struct stat st;
690 u_int32_t id;
691 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000692 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100693
694 id = get_int();
695 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000696 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
697 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100698 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
699 if (ret < 0) {
700 status = errno_to_portable(errno);
701 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000702 stat_to_attrib(&st, &a);
703 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000704 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100705 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000706 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100707 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000708 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100709}
710
Ben Lindstrombba81212001-06-25 05:01:22 +0000711static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100712process_stat(void)
713{
714 process_do_stat(0);
715}
716
Ben Lindstrombba81212001-06-25 05:01:22 +0000717static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100718process_lstat(void)
719{
720 process_do_stat(1);
721}
722
Ben Lindstrombba81212001-06-25 05:01:22 +0000723static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100724process_fstat(void)
725{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000726 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100727 struct stat st;
728 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000729 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100730
731 id = get_int();
732 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000733 debug("request %u: fstat \"%s\" (handle %u)",
734 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100735 fd = handle_to_fd(handle);
Damien Millere2334d62007-01-05 16:31:02 +1100736 if (fd >= 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100737 ret = fstat(fd, &st);
738 if (ret < 0) {
739 status = errno_to_portable(errno);
740 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000741 stat_to_attrib(&st, &a);
742 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000743 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100744 }
745 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000746 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100747 send_status(id, status);
748}
749
Ben Lindstrombba81212001-06-25 05:01:22 +0000750static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100751attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100752{
753 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000754
Damien Miller7b28dc52000-09-05 13:34:53 +1100755 tv[0].tv_sec = a->atime;
756 tv[0].tv_usec = 0;
757 tv[1].tv_sec = a->mtime;
758 tv[1].tv_usec = 0;
759 return tv;
760}
761
Ben Lindstrombba81212001-06-25 05:01:22 +0000762static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100763process_setstat(void)
764{
765 Attrib *a;
766 u_int32_t id;
767 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000768 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100769
770 id = get_int();
771 name = get_string(NULL);
772 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000773 debug("request %u: setstat name \"%s\"", id, name);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100774 if (readonly) {
775 status = SSH2_FX_PERMISSION_DENIED;
776 a->flags = 0;
777 }
Damien Miller00c92172002-02-13 14:05:00 +1100778 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000779 logit("set \"%s\" size %llu",
780 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100781 ret = truncate(name, a->size);
782 if (ret == -1)
783 status = errno_to_portable(errno);
784 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000785 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000786 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller9e720282008-06-29 22:46:35 +1000787 ret = chmod(name, a->perm & 07777);
Damien Miller7b28dc52000-09-05 13:34:53 +1100788 if (ret == -1)
789 status = errno_to_portable(errno);
790 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000791 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000792 char buf[64];
793 time_t t = a->mtime;
794
795 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
796 localtime(&t));
797 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100798 ret = utimes(name, attrib_to_tv(a));
799 if (ret == -1)
800 status = errno_to_portable(errno);
801 }
Kevin Steves8e743932001-02-05 13:24:35 +0000802 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000803 logit("set \"%s\" owner %lu group %lu", name,
804 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000805 ret = chown(name, a->uid, a->gid);
806 if (ret == -1)
807 status = errno_to_portable(errno);
808 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100809 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000810 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100811}
812
Ben Lindstrombba81212001-06-25 05:01:22 +0000813static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100814process_fsetstat(void)
815{
816 Attrib *a;
817 u_int32_t id;
818 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000819 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000820
Damien Miller7b28dc52000-09-05 13:34:53 +1100821 id = get_int();
822 handle = get_handle();
823 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000824 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100825 fd = handle_to_fd(handle);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100826 if (fd < 0)
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000827 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100828 else if (readonly)
829 status = SSH2_FX_PERMISSION_DENIED;
830 else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000831 char *name = handle_to_name(handle);
832
Damien Miller00c92172002-02-13 14:05:00 +1100833 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000834 logit("set \"%s\" size %llu",
835 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100836 ret = ftruncate(fd, a->size);
837 if (ret == -1)
838 status = errno_to_portable(errno);
839 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000840 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000841 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000842#ifdef HAVE_FCHMOD
Damien Miller9e720282008-06-29 22:46:35 +1000843 ret = fchmod(fd, a->perm & 07777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000844#else
Damien Miller9e720282008-06-29 22:46:35 +1000845 ret = chmod(name, a->perm & 07777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000846#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100847 if (ret == -1)
848 status = errno_to_portable(errno);
849 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000850 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000851 char buf[64];
852 time_t t = a->mtime;
853
854 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
855 localtime(&t));
856 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100857#ifdef HAVE_FUTIMES
858 ret = futimes(fd, attrib_to_tv(a));
859#else
860 ret = utimes(name, attrib_to_tv(a));
861#endif
862 if (ret == -1)
863 status = errno_to_portable(errno);
864 }
Kevin Steves8e743932001-02-05 13:24:35 +0000865 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000866 logit("set \"%s\" owner %lu group %lu", name,
867 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000868#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000869 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000870#else
871 ret = chown(name, a->uid, a->gid);
872#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000873 if (ret == -1)
874 status = errno_to_portable(errno);
875 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100876 }
877 send_status(id, status);
878}
879
Ben Lindstrombba81212001-06-25 05:01:22 +0000880static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100881process_opendir(void)
882{
883 DIR *dirp = NULL;
884 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000885 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100886 u_int32_t id;
887
888 id = get_int();
889 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000890 debug3("request %u: opendir", id);
891 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000892 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100893 if (dirp == NULL) {
894 status = errno_to_portable(errno);
895 } else {
Damien Miller00111382003-03-10 11:21:17 +1100896 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100897 if (handle < 0) {
898 closedir(dirp);
899 } else {
900 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000901 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100902 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000903
Damien Miller7b28dc52000-09-05 13:34:53 +1100904 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000905 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100906 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000907 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100908}
909
Ben Lindstrombba81212001-06-25 05:01:22 +0000910static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100911process_readdir(void)
912{
913 DIR *dirp;
914 struct dirent *dp;
915 char *path;
916 int handle;
917 u_int32_t id;
918
919 id = get_int();
920 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000921 debug("request %u: readdir \"%s\" (handle %d)", id,
922 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100923 dirp = handle_to_dir(handle);
924 path = handle_to_name(handle);
925 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000926 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100927 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100928 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +1000929 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +1100930 Stat *stats;
931 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000932
Damien Miller07d86be2006-03-26 14:19:21 +1100933 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100934 while ((dp = readdir(dirp)) != NULL) {
935 if (count >= nstats) {
936 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +1100937 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100938 }
939/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000940 snprintf(pathname, sizeof pathname, "%s%s%s", path,
941 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100942 if (lstat(pathname, &st) < 0)
943 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000944 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100945 stats[count].name = xstrdup(dp->d_name);
Darren Tucker2901e2d2010-01-13 22:44:06 +1100946 stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100947 count++;
948 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000949 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100950 if (count == 100)
951 break;
952 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000953 if (count > 0) {
954 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100955 for (i = 0; i < count; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000956 free(stats[i].name);
957 free(stats[i].long_name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000958 }
959 } else {
960 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100961 }
Darren Tuckera627d422013-06-02 07:31:17 +1000962 free(stats);
Damien Miller7b28dc52000-09-05 13:34:53 +1100963 }
964}
965
Ben Lindstrombba81212001-06-25 05:01:22 +0000966static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100967process_remove(void)
968{
969 char *name;
970 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000971 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100972 int ret;
973
974 id = get_int();
975 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000976 debug3("request %u: remove", id);
977 logit("remove name \"%s\"", name);
Darren Tuckerdb7bf822010-01-09 22:24:33 +1100978 if (readonly)
979 status = SSH2_FX_PERMISSION_DENIED;
980 else {
981 ret = unlink(name);
982 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
983 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100984 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +1000985 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100986}
987
Ben Lindstrombba81212001-06-25 05:01:22 +0000988static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100989process_mkdir(void)
990{
991 Attrib *a;
992 u_int32_t id;
993 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000994 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100995
996 id = get_int();
997 name = get_string(NULL);
998 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000999 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
Damien Miller9e720282008-06-29 22:46:35 +10001000 a->perm & 07777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +10001001 debug3("request %u: mkdir", id);
1002 logit("mkdir name \"%s\" mode 0%o", name, mode);
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001003 if (readonly)
1004 status = SSH2_FX_PERMISSION_DENIED;
1005 else {
1006 ret = mkdir(name, mode);
1007 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1008 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001009 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001010 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001011}
1012
Ben Lindstrombba81212001-06-25 05:01:22 +00001013static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001014process_rmdir(void)
1015{
1016 u_int32_t id;
1017 char *name;
1018 int ret, status;
1019
1020 id = get_int();
1021 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001022 debug3("request %u: rmdir", id);
1023 logit("rmdir name \"%s\"", name);
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001024 if (readonly)
1025 status = SSH2_FX_PERMISSION_DENIED;
1026 else {
1027 ret = rmdir(name);
1028 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1029 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001030 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001031 free(name);
Damien Miller7b28dc52000-09-05 13:34:53 +11001032}
1033
Ben Lindstrombba81212001-06-25 05:01:22 +00001034static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001035process_realpath(void)
1036{
1037 char resolvedname[MAXPATHLEN];
1038 u_int32_t id;
1039 char *path;
1040
1041 id = get_int();
1042 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001043 if (path[0] == '\0') {
Darren Tuckera627d422013-06-02 07:31:17 +10001044 free(path);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001045 path = xstrdup(".");
1046 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001047 debug3("request %u: realpath", id);
1048 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001049 if (realpath(path, resolvedname) == NULL) {
1050 send_status(id, errno_to_portable(errno));
1051 } else {
1052 Stat s;
1053 attrib_clear(&s.attrib);
1054 s.name = s.long_name = resolvedname;
1055 send_names(id, 1, &s);
1056 }
Darren Tuckera627d422013-06-02 07:31:17 +10001057 free(path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001058}
1059
Ben Lindstrombba81212001-06-25 05:01:22 +00001060static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001061process_rename(void)
1062{
1063 u_int32_t id;
1064 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001065 int status;
Damien Millerb3207e82003-03-26 16:01:11 +11001066 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +11001067
1068 id = get_int();
1069 oldpath = get_string(NULL);
1070 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001071 debug3("request %u: rename", id);
1072 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +11001073 status = SSH2_FX_FAILURE;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001074 if (readonly)
1075 status = SSH2_FX_PERMISSION_DENIED;
1076 else if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +11001077 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +11001078 else if (S_ISREG(sb.st_mode)) {
1079 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001080 if (link(oldpath, newpath) == -1) {
Damien Miller0e265512009-08-28 10:43:13 +10001081 if (errno == EOPNOTSUPP || errno == ENOSYS
Darren Tuckerf7fa7062008-07-04 14:10:19 +10001082#ifdef EXDEV
1083 || errno == EXDEV
1084#endif
Darren Tuckere59b5082004-06-28 16:01:19 +10001085#ifdef LINK_OPNOTSUPP_ERRNO
1086 || errno == LINK_OPNOTSUPP_ERRNO
1087#endif
1088 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001089 struct stat st;
1090
1091 /*
1092 * fs doesn't support links, so fall back to
1093 * stat+rename. This is racy.
1094 */
1095 if (stat(newpath, &st) == -1) {
1096 if (rename(oldpath, newpath) == -1)
1097 status =
1098 errno_to_portable(errno);
1099 else
1100 status = SSH2_FX_OK;
1101 }
1102 } else {
1103 status = errno_to_portable(errno);
1104 }
1105 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001106 status = errno_to_portable(errno);
1107 /* clean spare link */
1108 unlink(newpath);
1109 } else
1110 status = SSH2_FX_OK;
1111 } else if (stat(newpath, &sb) == -1) {
1112 if (rename(oldpath, newpath) == -1)
1113 status = errno_to_portable(errno);
1114 else
1115 status = SSH2_FX_OK;
1116 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001117 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001118 free(oldpath);
1119 free(newpath);
Damien Miller7b28dc52000-09-05 13:34:53 +11001120}
1121
Ben Lindstrombba81212001-06-25 05:01:22 +00001122static void
Damien Miller058316f2001-03-08 10:08:49 +11001123process_readlink(void)
1124{
1125 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001126 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001127 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001128 char *path;
1129
1130 id = get_int();
1131 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001132 debug3("request %u: readlink", id);
1133 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001134 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001135 send_status(id, errno_to_portable(errno));
1136 else {
1137 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001138
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001139 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001140 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001141 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001142 send_names(id, 1, &s);
1143 }
Darren Tuckera627d422013-06-02 07:31:17 +10001144 free(path);
Damien Miller058316f2001-03-08 10:08:49 +11001145}
1146
Ben Lindstrombba81212001-06-25 05:01:22 +00001147static void
Damien Miller058316f2001-03-08 10:08:49 +11001148process_symlink(void)
1149{
1150 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +11001151 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001152 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001153
1154 id = get_int();
1155 oldpath = get_string(NULL);
1156 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001157 debug3("request %u: symlink", id);
1158 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001159 /* this will fail if 'newpath' exists */
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001160 if (readonly)
1161 status = SSH2_FX_PERMISSION_DENIED;
1162 else {
1163 ret = symlink(oldpath, newpath);
1164 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1165 }
Damien Miller058316f2001-03-08 10:08:49 +11001166 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001167 free(oldpath);
1168 free(newpath);
Damien Miller058316f2001-03-08 10:08:49 +11001169}
1170
Ben Lindstrombba81212001-06-25 05:01:22 +00001171static void
Damien Miller7c296612008-03-07 18:33:53 +11001172process_extended_posix_rename(u_int32_t id)
1173{
1174 char *oldpath, *newpath;
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001175 int ret, status;
Damien Miller7c296612008-03-07 18:33:53 +11001176
1177 oldpath = get_string(NULL);
1178 newpath = get_string(NULL);
1179 debug3("request %u: posix-rename", id);
1180 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001181 if (readonly)
1182 status = SSH2_FX_PERMISSION_DENIED;
1183 else {
1184 ret = rename(oldpath, newpath);
1185 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1186 }
1187 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001188 free(oldpath);
1189 free(newpath);
Damien Miller7c296612008-03-07 18:33:53 +11001190}
1191
1192static void
Damien Millerd671e5a2008-05-19 14:53:33 +10001193process_extended_statvfs(u_int32_t id)
1194{
1195 char *path;
1196 struct statvfs st;
1197
1198 path = get_string(NULL);
1199 debug3("request %u: statfs", id);
1200 logit("statfs \"%s\"", path);
1201
1202 if (statvfs(path, &st) != 0)
1203 send_status(id, errno_to_portable(errno));
1204 else
1205 send_statvfs(id, &st);
Darren Tuckera627d422013-06-02 07:31:17 +10001206 free(path);
Damien Millerd671e5a2008-05-19 14:53:33 +10001207}
1208
1209static void
1210process_extended_fstatvfs(u_int32_t id)
1211{
1212 int handle, fd;
1213 struct statvfs st;
1214
1215 handle = get_handle();
1216 debug("request %u: fstatvfs \"%s\" (handle %u)",
1217 id, handle_to_name(handle), handle);
1218 if ((fd = handle_to_fd(handle)) < 0) {
1219 send_status(id, SSH2_FX_FAILURE);
1220 return;
1221 }
1222 if (fstatvfs(fd, &st) != 0)
1223 send_status(id, errno_to_portable(errno));
1224 else
1225 send_statvfs(id, &st);
1226}
1227
1228static void
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001229process_extended_hardlink(u_int32_t id)
1230{
1231 char *oldpath, *newpath;
1232 int ret, status;
1233
1234 oldpath = get_string(NULL);
1235 newpath = get_string(NULL);
1236 debug3("request %u: hardlink", id);
1237 logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
1238 if (readonly)
1239 status = SSH2_FX_PERMISSION_DENIED;
1240 else {
1241 ret = link(oldpath, newpath);
1242 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1243 }
1244 send_status(id, status);
Darren Tuckera627d422013-06-02 07:31:17 +10001245 free(oldpath);
1246 free(newpath);
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001247}
1248
1249static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001250process_extended(void)
1251{
1252 u_int32_t id;
1253 char *request;
1254
1255 id = get_int();
1256 request = get_string(NULL);
Damien Miller7c296612008-03-07 18:33:53 +11001257 if (strcmp(request, "posix-rename@openssh.com") == 0)
1258 process_extended_posix_rename(id);
Damien Millerd671e5a2008-05-19 14:53:33 +10001259 else if (strcmp(request, "statvfs@openssh.com") == 0)
1260 process_extended_statvfs(id);
1261 else if (strcmp(request, "fstatvfs@openssh.com") == 0)
1262 process_extended_fstatvfs(id);
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001263 else if (strcmp(request, "hardlink@openssh.com") == 0)
1264 process_extended_hardlink(id);
Damien Miller7c296612008-03-07 18:33:53 +11001265 else
1266 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
Darren Tuckera627d422013-06-02 07:31:17 +10001267 free(request);
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001268}
Damien Miller7b28dc52000-09-05 13:34:53 +11001269
1270/* stolen from ssh-agent */
1271
Ben Lindstrombba81212001-06-25 05:01:22 +00001272static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001273process(void)
1274{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001275 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +00001276 u_int buf_len;
1277 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001278 u_int type;
1279 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +11001280
Ben Lindstrom2c140472002-06-06 21:57:54 +00001281 buf_len = buffer_len(&iqueue);
1282 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001283 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001284 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001285 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001286 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001287 error("bad message from %s local user %s",
1288 client_addr, pw->pw_name);
Damien Millerdfc24252008-02-10 22:29:40 +11001289 sftp_server_cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001290 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001291 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001292 return;
1293 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001294 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001295 type = buffer_get_char(&iqueue);
1296 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001297 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001298 process_init();
1299 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001300 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +11001301 process_open();
1302 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001303 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001304 process_close();
1305 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001306 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +11001307 process_read();
1308 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001309 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001310 process_write();
1311 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001312 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001313 process_lstat();
1314 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001315 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001316 process_fstat();
1317 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001318 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001319 process_setstat();
1320 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001321 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001322 process_fsetstat();
1323 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001324 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001325 process_opendir();
1326 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001327 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001328 process_readdir();
1329 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001330 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001331 process_remove();
1332 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001333 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001334 process_mkdir();
1335 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001336 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001337 process_rmdir();
1338 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001339 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001340 process_realpath();
1341 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001342 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001343 process_stat();
1344 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001345 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001346 process_rename();
1347 break;
Damien Miller058316f2001-03-08 10:08:49 +11001348 case SSH2_FXP_READLINK:
1349 process_readlink();
1350 break;
1351 case SSH2_FXP_SYMLINK:
1352 process_symlink();
1353 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001354 case SSH2_FXP_EXTENDED:
1355 process_extended();
1356 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001357 default:
1358 error("Unknown message %d", type);
1359 break;
1360 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001361 /* discard the remaining bytes from the current packet */
Damien Millerdfc24252008-02-10 22:29:40 +11001362 if (buf_len < buffer_len(&iqueue)) {
1363 error("iqueue grew unexpectedly");
1364 sftp_server_cleanup_exit(255);
1365 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001366 consumed = buf_len - buffer_len(&iqueue);
Damien Millerdfc24252008-02-10 22:29:40 +11001367 if (msg_len < consumed) {
1368 error("msg_len %d < consumed %d", msg_len, consumed);
1369 sftp_server_cleanup_exit(255);
1370 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001371 if (msg_len > consumed)
1372 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001373}
1374
Damien Millerfef95ad2006-07-10 20:46:55 +10001375/* Cleanup handler that logs active handles upon normal exit */
1376void
Damien Millerdfc24252008-02-10 22:29:40 +11001377sftp_server_cleanup_exit(int i)
Damien Millerfef95ad2006-07-10 20:46:55 +10001378{
1379 if (pw != NULL && client_addr != NULL) {
1380 handle_log_exit();
1381 logit("session closed for local user %s from [%s]",
1382 pw->pw_name, client_addr);
1383 }
1384 _exit(i);
1385}
1386
1387static void
Damien Millerdfc24252008-02-10 22:29:40 +11001388sftp_server_usage(void)
Damien Millerfef95ad2006-07-10 20:46:55 +10001389{
1390 extern char *__progname;
1391
1392 fprintf(stderr,
Damien Milleraa7ad302013-01-09 15:58:21 +11001393 "usage: %s [-ehR] [-d start_directory] [-f log_facility] "
1394 "[-l log_level]\n\t[-u umask]\n",
Darren Tucker7dc48502009-10-07 08:44:42 +11001395 __progname);
Damien Millerfef95ad2006-07-10 20:46:55 +10001396 exit(1);
1397}
1398
Damien Miller7b28dc52000-09-05 13:34:53 +11001399int
Damien Millerd8cb1f12008-02-10 22:40:12 +11001400sftp_server_main(int argc, char **argv, struct passwd *user_pw)
Damien Miller7b28dc52000-09-05 13:34:53 +11001401{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001402 fd_set *rset, *wset;
Damien Millerfef95ad2006-07-10 20:46:55 +10001403 int in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001404 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001405 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
Damien Miller502ab0e2013-01-09 15:57:36 +11001406 char *cp, *homedir = NULL, buf[4*4096];
Damien Miller07331212010-11-05 10:20:31 +11001407 long mask;
Damien Millerfef95ad2006-07-10 20:46:55 +10001408
Damien Millerfef95ad2006-07-10 20:46:55 +10001409 extern char *optarg;
1410 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001411
Damien Millerfef95ad2006-07-10 20:46:55 +10001412 __progname = ssh_get_progname(argv[0]);
1413 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001414
Damien Miller502ab0e2013-01-09 15:57:36 +11001415 pw = pwcopy(user_pw);
1416
1417 while (!skipargs && (ch = getopt(argc, argv, "d:f:l:u:cehR")) != -1) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001418 switch (ch) {
Darren Tuckerdb7bf822010-01-09 22:24:33 +11001419 case 'R':
1420 readonly = 1;
1421 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001422 case 'c':
1423 /*
1424 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001425 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001426 */
1427 skipargs = 1;
1428 break;
1429 case 'e':
1430 log_stderr = 1;
1431 break;
1432 case 'l':
1433 log_level = log_level_number(optarg);
1434 if (log_level == SYSLOG_LEVEL_NOT_SET)
1435 error("Invalid log level \"%s\"", optarg);
1436 break;
1437 case 'f':
1438 log_facility = log_facility_number(optarg);
Damien Miller35e18db2007-09-17 16:11:33 +10001439 if (log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Millerfef95ad2006-07-10 20:46:55 +10001440 error("Invalid log facility \"%s\"", optarg);
1441 break;
Damien Miller502ab0e2013-01-09 15:57:36 +11001442 case 'd':
1443 cp = tilde_expand_filename(optarg, user_pw->pw_uid);
1444 homedir = percent_expand(cp, "d", user_pw->pw_dir,
1445 "u", user_pw->pw_name, (char *)NULL);
1446 free(cp);
1447 break;
Darren Tucker7dc48502009-10-07 08:44:42 +11001448 case 'u':
Damien Miller07331212010-11-05 10:20:31 +11001449 errno = 0;
1450 mask = strtol(optarg, &cp, 8);
1451 if (mask < 0 || mask > 0777 || *cp != '\0' ||
1452 cp == optarg || (mask == 0 && errno != 0))
1453 fatal("Invalid umask \"%s\"", optarg);
1454 (void)umask((mode_t)mask);
Darren Tucker7dc48502009-10-07 08:44:42 +11001455 break;
Damien Millerfef95ad2006-07-10 20:46:55 +10001456 case 'h':
1457 default:
Damien Millerdfc24252008-02-10 22:29:40 +11001458 sftp_server_usage();
Damien Millerfef95ad2006-07-10 20:46:55 +10001459 }
1460 }
1461
1462 log_init(__progname, log_level, log_facility, log_stderr);
1463
1464 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1465 client_addr = xstrdup(cp);
Damien Millerdfc24252008-02-10 22:29:40 +11001466 if ((cp = strchr(client_addr, ' ')) == NULL) {
1467 error("Malformed SSH_CONNECTION variable: \"%s\"",
Damien Millerfef95ad2006-07-10 20:46:55 +10001468 getenv("SSH_CONNECTION"));
Damien Millerdfc24252008-02-10 22:29:40 +11001469 sftp_server_cleanup_exit(255);
1470 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001471 *cp = '\0';
1472 } else
1473 client_addr = xstrdup("UNKNOWN");
1474
Damien Millerfef95ad2006-07-10 20:46:55 +10001475 logit("session opened for local user %s from [%s]",
1476 pw->pw_name, client_addr);
1477
Darren Tuckeraaf51d22010-01-08 19:04:49 +11001478 in = STDIN_FILENO;
1479 out = STDOUT_FILENO;
Damien Miller7b28dc52000-09-05 13:34:53 +11001480
Damien Miller402b3312001-04-14 00:28:42 +10001481#ifdef HAVE_CYGWIN
1482 setmode(in, O_BINARY);
1483 setmode(out, O_BINARY);
1484#endif
1485
Damien Miller7b28dc52000-09-05 13:34:53 +11001486 max = 0;
1487 if (in > max)
1488 max = in;
1489 if (out > max)
1490 max = out;
1491
1492 buffer_init(&iqueue);
1493 buffer_init(&oqueue);
1494
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001495 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1496 rset = (fd_set *)xmalloc(set_size);
1497 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001498
Damien Miller502ab0e2013-01-09 15:57:36 +11001499 if (homedir != NULL) {
1500 if (chdir(homedir) != 0) {
1501 error("chdir to \"%s\" failed: %s", homedir,
1502 strerror(errno));
1503 }
1504 }
1505
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001506 for (;;) {
1507 memset(rset, 0, set_size);
1508 memset(wset, 0, set_size);
1509
Darren Tuckere9405982007-05-20 15:09:04 +10001510 /*
1511 * Ensure that we can read a full buffer and handle
1512 * the worst-case length packet it can generate,
1513 * otherwise apply backpressure by stopping reads.
1514 */
1515 if (buffer_check_alloc(&iqueue, sizeof(buf)) &&
1516 buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1517 FD_SET(in, rset);
1518
Damien Miller7b28dc52000-09-05 13:34:53 +11001519 olen = buffer_len(&oqueue);
1520 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001521 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001522
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001523 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001524 if (errno == EINTR)
1525 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001526 error("select: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001527 sftp_server_cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001528 }
1529
1530 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001531 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001532 len = read(in, buf, sizeof buf);
1533 if (len == 0) {
1534 debug("read eof");
Damien Millerdfc24252008-02-10 22:29:40 +11001535 sftp_server_cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001536 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001537 error("read: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001538 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001539 } else {
1540 buffer_append(&iqueue, buf, len);
1541 }
1542 }
1543 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001544 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001545 len = write(out, buffer_ptr(&oqueue), olen);
1546 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001547 error("write: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001548 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001549 } else {
1550 buffer_consume(&oqueue, len);
1551 }
1552 }
Darren Tuckere9405982007-05-20 15:09:04 +10001553
1554 /*
1555 * Process requests from client if we can fit the results
1556 * into the output buffer, otherwise stop processing input
1557 * and let the output queue drain.
1558 */
1559 if (buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1560 process();
Damien Miller7b28dc52000-09-05 13:34:53 +11001561 }
1562}