blob: a9cc9408af1f4495a473b40a844edc452e986e25 [file] [log] [blame]
Damien Millera7e0d5a2008-05-19 16:08:41 +10001/* $OpenBSD: sftp-server.c,v 1.80 2008/05/18 21:29:05 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 */
71int version;
72
Darren Tuckera6612d42003-06-28 12:39:03 +100073/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110074
Damien Miller7b28dc52000-09-05 13:34:53 +110075typedef struct Stat Stat;
76
Damien Miller33804262001-02-04 23:20:18 +110077struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110078 char *name;
79 char *long_name;
80 Attrib attrib;
81};
82
Ben Lindstrombba81212001-06-25 05:01:22 +000083static int
Damien Miller7b28dc52000-09-05 13:34:53 +110084errno_to_portable(int unixerrno)
85{
86 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000087
Damien Miller7b28dc52000-09-05 13:34:53 +110088 switch (unixerrno) {
89 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000090 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110091 break;
92 case ENOENT:
93 case ENOTDIR:
94 case EBADF:
95 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000096 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110097 break;
98 case EPERM:
99 case EACCES:
100 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000101 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +1100102 break;
103 case ENAMETOOLONG:
104 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000105 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100106 break;
107 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000108 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100109 break;
110 }
111 return ret;
112}
113
Ben Lindstrombba81212001-06-25 05:01:22 +0000114static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100115flags_from_portable(int pflags)
116{
117 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000118
Ben Lindstrom36592512001-03-05 05:02:08 +0000119 if ((pflags & SSH2_FXF_READ) &&
120 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100121 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000122 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100123 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000124 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100125 flags = O_WRONLY;
126 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000127 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100128 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000129 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100130 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000131 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100132 flags |= O_EXCL;
133 return flags;
134}
135
Damien Millerfef95ad2006-07-10 20:46:55 +1000136static const char *
137string_from_portable(int pflags)
138{
139 static char ret[128];
140
141 *ret = '\0';
142
143#define PAPPEND(str) { \
144 if (*ret != '\0') \
145 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000146 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000147 }
148
149 if (pflags & SSH2_FXF_READ)
150 PAPPEND("READ")
151 if (pflags & SSH2_FXF_WRITE)
152 PAPPEND("WRITE")
153 if (pflags & SSH2_FXF_CREAT)
154 PAPPEND("CREATE")
155 if (pflags & SSH2_FXF_TRUNC)
156 PAPPEND("TRUNCATE")
157 if (pflags & SSH2_FXF_EXCL)
158 PAPPEND("EXCL")
159
160 return ret;
161}
162
Ben Lindstrombba81212001-06-25 05:01:22 +0000163static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100164get_attrib(void)
165{
166 return decode_attrib(&iqueue);
167}
168
169/* handle handles */
170
171typedef struct Handle Handle;
172struct Handle {
173 int use;
174 DIR *dirp;
175 int fd;
176 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000177 u_int64_t bytes_read, bytes_write;
Damien Miller3397d0e2008-02-10 22:26:51 +1100178 int next_unused;
Damien Miller7b28dc52000-09-05 13:34:53 +1100179};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000180
Damien Miller7b28dc52000-09-05 13:34:53 +1100181enum {
182 HANDLE_UNUSED,
183 HANDLE_DIR,
184 HANDLE_FILE
185};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000186
Damien Miller3397d0e2008-02-10 22:26:51 +1100187Handle *handles = NULL;
188u_int num_handles = 0;
189int first_unused_handle = -1;
Damien Miller7b28dc52000-09-05 13:34:53 +1100190
Damien Miller3397d0e2008-02-10 22:26:51 +1100191static void handle_unused(int i)
Damien Miller7b28dc52000-09-05 13:34:53 +1100192{
Damien Miller3397d0e2008-02-10 22:26:51 +1100193 handles[i].use = HANDLE_UNUSED;
194 handles[i].next_unused = first_unused_handle;
195 first_unused_handle = i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100196}
197
Ben Lindstrombba81212001-06-25 05:01:22 +0000198static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100199handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100200{
Damien Miller3397d0e2008-02-10 22:26:51 +1100201 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000202
Damien Miller3397d0e2008-02-10 22:26:51 +1100203 if (first_unused_handle == -1) {
204 if (num_handles + 1 <= num_handles)
205 return -1;
206 num_handles++;
207 handles = xrealloc(handles, num_handles, sizeof(Handle));
208 handle_unused(num_handles - 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100209 }
Damien Miller3397d0e2008-02-10 22:26:51 +1100210
211 i = first_unused_handle;
212 first_unused_handle = handles[i].next_unused;
213
214 handles[i].use = use;
215 handles[i].dirp = dirp;
216 handles[i].fd = fd;
217 handles[i].name = xstrdup(name);
218 handles[i].bytes_read = handles[i].bytes_write = 0;
219
220 return i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100221}
222
Ben Lindstrombba81212001-06-25 05:01:22 +0000223static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100224handle_is_ok(int i, int type)
225{
Damien Miller3397d0e2008-02-10 22:26:51 +1100226 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
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_to_string(int handle, char **stringp, int *hlenp)
231{
Damien Miller7b28dc52000-09-05 13:34:53 +1100232 if (stringp == NULL || hlenp == NULL)
233 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000234 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100235 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000236 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100237 return 0;
238}
239
Ben Lindstrombba81212001-06-25 05:01:22 +0000240static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100241handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100242{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000243 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000244
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000245 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100246 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100247 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100248 if (handle_is_ok(val, HANDLE_FILE) ||
249 handle_is_ok(val, HANDLE_DIR))
250 return val;
251 return -1;
252}
253
Ben Lindstrombba81212001-06-25 05:01:22 +0000254static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100255handle_to_name(int handle)
256{
257 if (handle_is_ok(handle, HANDLE_DIR)||
258 handle_is_ok(handle, HANDLE_FILE))
259 return handles[handle].name;
260 return NULL;
261}
262
Ben Lindstrombba81212001-06-25 05:01:22 +0000263static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100264handle_to_dir(int handle)
265{
266 if (handle_is_ok(handle, HANDLE_DIR))
267 return handles[handle].dirp;
268 return NULL;
269}
270
Ben Lindstrombba81212001-06-25 05:01:22 +0000271static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100272handle_to_fd(int handle)
273{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000274 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100275 return handles[handle].fd;
276 return -1;
277}
278
Damien Millerfef95ad2006-07-10 20:46:55 +1000279static void
280handle_update_read(int handle, ssize_t bytes)
281{
282 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
283 handles[handle].bytes_read += bytes;
284}
285
286static void
287handle_update_write(int handle, ssize_t bytes)
288{
289 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
290 handles[handle].bytes_write += bytes;
291}
292
293static u_int64_t
294handle_bytes_read(int handle)
295{
296 if (handle_is_ok(handle, HANDLE_FILE))
297 return (handles[handle].bytes_read);
298 return 0;
299}
300
301static u_int64_t
302handle_bytes_write(int handle)
303{
304 if (handle_is_ok(handle, HANDLE_FILE))
305 return (handles[handle].bytes_write);
306 return 0;
307}
308
Ben Lindstrombba81212001-06-25 05:01:22 +0000309static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100310handle_close(int handle)
311{
312 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000313
Damien Miller7b28dc52000-09-05 13:34:53 +1100314 if (handle_is_ok(handle, HANDLE_FILE)) {
315 ret = close(handles[handle].fd);
Damien Miller00111382003-03-10 11:21:17 +1100316 xfree(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100317 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100318 } else if (handle_is_ok(handle, HANDLE_DIR)) {
319 ret = closedir(handles[handle].dirp);
Damien Miller00111382003-03-10 11:21:17 +1100320 xfree(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100321 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100322 } else {
323 errno = ENOENT;
324 }
325 return ret;
326}
327
Damien Millerfef95ad2006-07-10 20:46:55 +1000328static void
329handle_log_close(int handle, char *emsg)
330{
331 if (handle_is_ok(handle, HANDLE_FILE)) {
332 logit("%s%sclose \"%s\" bytes read %llu written %llu",
333 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
334 handle_to_name(handle),
Darren Tucker86473c52007-05-20 14:59:32 +1000335 (unsigned long long)handle_bytes_read(handle),
336 (unsigned long long)handle_bytes_write(handle));
Damien Millerfef95ad2006-07-10 20:46:55 +1000337 } else {
338 logit("%s%sclosedir \"%s\"",
339 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
340 handle_to_name(handle));
341 }
342}
343
344static void
345handle_log_exit(void)
346{
347 u_int i;
348
Damien Miller3397d0e2008-02-10 22:26:51 +1100349 for (i = 0; i < num_handles; i++)
Damien Millerfef95ad2006-07-10 20:46:55 +1000350 if (handles[i].use != HANDLE_UNUSED)
351 handle_log_close(i, "forced");
352}
353
Ben Lindstrombba81212001-06-25 05:01:22 +0000354static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100355get_handle(void)
356{
357 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000358 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100359 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000360
Damien Miller7b28dc52000-09-05 13:34:53 +1100361 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000362 if (hlen < 256)
363 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100364 xfree(handle);
365 return val;
366}
367
368/* send replies */
369
Ben Lindstrombba81212001-06-25 05:01:22 +0000370static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100371send_msg(Buffer *m)
372{
373 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000374
Damien Miller7b28dc52000-09-05 13:34:53 +1100375 buffer_put_int(&oqueue, mlen);
376 buffer_append(&oqueue, buffer_ptr(m), mlen);
377 buffer_consume(m, mlen);
378}
379
Damien Millerfef95ad2006-07-10 20:46:55 +1000380static const char *
381status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100382{
Damien Miller058316f2001-03-08 10:08:49 +1100383 const char *status_messages[] = {
384 "Success", /* SSH_FX_OK */
385 "End of file", /* SSH_FX_EOF */
386 "No such file", /* SSH_FX_NO_SUCH_FILE */
387 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
388 "Failure", /* SSH_FX_FAILURE */
389 "Bad message", /* SSH_FX_BAD_MESSAGE */
390 "No connection", /* SSH_FX_NO_CONNECTION */
391 "Connection lost", /* SSH_FX_CONNECTION_LOST */
392 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
393 "Unknown error" /* Others */
394 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000395 return (status_messages[MIN(status,SSH2_FX_MAX)]);
396}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000397
Damien Millerfef95ad2006-07-10 20:46:55 +1000398static void
399send_status(u_int32_t id, u_int32_t status)
400{
401 Buffer msg;
402
403 debug3("request %u: sent status %u", id, status);
404 if (log_level > SYSLOG_LEVEL_VERBOSE ||
405 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
406 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100407 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000408 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100409 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000410 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100411 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000412 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100413 buffer_put_cstring(&msg, "");
414 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100415 send_msg(&msg);
416 buffer_free(&msg);
417}
Ben Lindstrombba81212001-06-25 05:01:22 +0000418static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100419send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100420{
421 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000422
Damien Miller7b28dc52000-09-05 13:34:53 +1100423 buffer_init(&msg);
424 buffer_put_char(&msg, type);
425 buffer_put_int(&msg, id);
426 buffer_put_string(&msg, data, dlen);
427 send_msg(&msg);
428 buffer_free(&msg);
429}
430
Ben Lindstrombba81212001-06-25 05:01:22 +0000431static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100432send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100433{
Damien Millerfef95ad2006-07-10 20:46:55 +1000434 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000435 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100436}
437
Ben Lindstrombba81212001-06-25 05:01:22 +0000438static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100439send_handle(u_int32_t id, int handle)
440{
441 char *string;
442 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000443
Damien Miller7b28dc52000-09-05 13:34:53 +1100444 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000445 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000446 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100447 xfree(string);
448}
449
Ben Lindstrombba81212001-06-25 05:01:22 +0000450static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100451send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100452{
453 Buffer msg;
454 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000455
Damien Miller7b28dc52000-09-05 13:34:53 +1100456 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000457 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100458 buffer_put_int(&msg, id);
459 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000460 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100461 for (i = 0; i < count; i++) {
462 buffer_put_cstring(&msg, stats[i].name);
463 buffer_put_cstring(&msg, stats[i].long_name);
464 encode_attrib(&msg, &stats[i].attrib);
465 }
466 send_msg(&msg);
467 buffer_free(&msg);
468}
469
Ben Lindstrombba81212001-06-25 05:01:22 +0000470static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100471send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100472{
473 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000474
Damien Millerfef95ad2006-07-10 20:46:55 +1000475 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100476 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000477 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100478 buffer_put_int(&msg, id);
479 encode_attrib(&msg, a);
480 send_msg(&msg);
481 buffer_free(&msg);
482}
483
Darren Tucker5b2e2ba2008-06-08 09:25:28 +1000484#ifdef USE_STATVFS
Damien Millerd671e5a2008-05-19 14:53:33 +1000485static void
486send_statvfs(u_int32_t id, struct statvfs *st)
487{
488 Buffer msg;
489 u_int64_t flag;
490
491 flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
492 flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;
493
494 buffer_init(&msg);
495 buffer_put_char(&msg, SSH2_FXP_EXTENDED_REPLY);
496 buffer_put_int(&msg, id);
497 buffer_put_int(&msg, st->f_bsize);
498 buffer_put_int(&msg, st->f_frsize);
499 buffer_put_int64(&msg, st->f_blocks);
500 buffer_put_int64(&msg, st->f_bfree);
501 buffer_put_int64(&msg, st->f_bavail);
502 buffer_put_int64(&msg, st->f_files);
503 buffer_put_int64(&msg, st->f_ffree);
504 buffer_put_int64(&msg, st->f_favail);
505 buffer_put_int(&msg, st->f_fsid);
506 buffer_put_int(&msg, flag);
507 buffer_put_int(&msg, st->f_namemax);
508 send_msg(&msg);
509 buffer_free(&msg);
510}
Darren Tucker5b2e2ba2008-06-08 09:25:28 +1000511#endif
Damien Millerd671e5a2008-05-19 14:53:33 +1000512
Damien Miller7b28dc52000-09-05 13:34:53 +1100513/* parse incoming */
514
Ben Lindstrombba81212001-06-25 05:01:22 +0000515static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100516process_init(void)
517{
518 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100519
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000520 version = get_int();
Damien Millerfef95ad2006-07-10 20:46:55 +1000521 verbose("received client version %d", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100522 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000523 buffer_put_char(&msg, SSH2_FXP_VERSION);
524 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7c296612008-03-07 18:33:53 +1100525 /* POSIX rename extension */
526 buffer_put_cstring(&msg, "posix-rename@openssh.com");
527 buffer_put_cstring(&msg, "1"); /* version */
Darren Tucker5b2e2ba2008-06-08 09:25:28 +1000528#ifdef USEE_STATVFS
Damien Millera7e0d5a2008-05-19 16:08:41 +1000529 /* statvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000530 buffer_put_cstring(&msg, "statvfs@openssh.com");
531 buffer_put_cstring(&msg, "1"); /* version */
Damien Millera7e0d5a2008-05-19 16:08:41 +1000532 /* fstatvfs extension */
Damien Millerd671e5a2008-05-19 14:53:33 +1000533 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
534 buffer_put_cstring(&msg, "1"); /* version */
Darren Tucker5b2e2ba2008-06-08 09:25:28 +1000535#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100536 send_msg(&msg);
537 buffer_free(&msg);
538}
539
Ben Lindstrombba81212001-06-25 05:01:22 +0000540static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100541process_open(void)
542{
543 u_int32_t id, pflags;
544 Attrib *a;
545 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000546 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100547
548 id = get_int();
549 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000550 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000551 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100552 a = get_attrib();
553 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000554 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000555 logit("open \"%s\" flags %s mode 0%o",
556 name, string_from_portable(pflags), mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100557 fd = open(name, flags, mode);
558 if (fd < 0) {
559 status = errno_to_portable(errno);
560 } else {
Damien Miller00111382003-03-10 11:21:17 +1100561 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100562 if (handle < 0) {
563 close(fd);
564 } else {
565 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000566 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100567 }
568 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000569 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100570 send_status(id, status);
571 xfree(name);
572}
573
Ben Lindstrombba81212001-06-25 05:01:22 +0000574static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100575process_close(void)
576{
577 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000578 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100579
580 id = get_int();
581 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000582 debug3("request %u: close handle %u", id, handle);
583 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100584 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000585 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100586 send_status(id, status);
587}
588
Ben Lindstrombba81212001-06-25 05:01:22 +0000589static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100590process_read(void)
591{
592 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000593 u_int32_t id, len;
594 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100595 u_int64_t off;
596
597 id = get_int();
598 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000599 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100600 len = get_int();
601
Damien Millerfef95ad2006-07-10 20:46:55 +1000602 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
603 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100604 if (len > sizeof buf) {
605 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000606 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100607 }
608 fd = handle_to_fd(handle);
609 if (fd >= 0) {
610 if (lseek(fd, off, SEEK_SET) < 0) {
611 error("process_read: seek failed");
612 status = errno_to_portable(errno);
613 } else {
614 ret = read(fd, buf, len);
615 if (ret < 0) {
616 status = errno_to_portable(errno);
617 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000618 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100619 } else {
620 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000621 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000622 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100623 }
624 }
625 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000626 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100627 send_status(id, status);
628}
629
Ben Lindstrombba81212001-06-25 05:01:22 +0000630static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100631process_write(void)
632{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000633 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100634 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100635 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000636 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100637 char *data;
638
639 id = get_int();
640 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000641 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100642 data = get_string(&len);
643
Damien Millerfef95ad2006-07-10 20:46:55 +1000644 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
645 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100646 fd = handle_to_fd(handle);
647 if (fd >= 0) {
648 if (lseek(fd, off, SEEK_SET) < 0) {
649 status = errno_to_portable(errno);
650 error("process_write: seek failed");
651 } else {
652/* XXX ATOMICIO ? */
653 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000654 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100655 error("process_write: write failed");
656 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000657 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000658 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000659 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100660 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000661 debug2("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100662 }
663 }
664 }
665 send_status(id, status);
666 xfree(data);
667}
668
Ben Lindstrombba81212001-06-25 05:01:22 +0000669static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100670process_do_stat(int do_lstat)
671{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000672 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100673 struct stat st;
674 u_int32_t id;
675 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000676 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100677
678 id = get_int();
679 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000680 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
681 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100682 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
683 if (ret < 0) {
684 status = errno_to_portable(errno);
685 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000686 stat_to_attrib(&st, &a);
687 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000688 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100689 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000690 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100691 send_status(id, status);
692 xfree(name);
693}
694
Ben Lindstrombba81212001-06-25 05:01:22 +0000695static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100696process_stat(void)
697{
698 process_do_stat(0);
699}
700
Ben Lindstrombba81212001-06-25 05:01:22 +0000701static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100702process_lstat(void)
703{
704 process_do_stat(1);
705}
706
Ben Lindstrombba81212001-06-25 05:01:22 +0000707static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100708process_fstat(void)
709{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000710 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100711 struct stat st;
712 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000713 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100714
715 id = get_int();
716 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000717 debug("request %u: fstat \"%s\" (handle %u)",
718 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100719 fd = handle_to_fd(handle);
Damien Millere2334d62007-01-05 16:31:02 +1100720 if (fd >= 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100721 ret = fstat(fd, &st);
722 if (ret < 0) {
723 status = errno_to_portable(errno);
724 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000725 stat_to_attrib(&st, &a);
726 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000727 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100728 }
729 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000730 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100731 send_status(id, status);
732}
733
Ben Lindstrombba81212001-06-25 05:01:22 +0000734static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100735attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100736{
737 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000738
Damien Miller7b28dc52000-09-05 13:34:53 +1100739 tv[0].tv_sec = a->atime;
740 tv[0].tv_usec = 0;
741 tv[1].tv_sec = a->mtime;
742 tv[1].tv_usec = 0;
743 return tv;
744}
745
Ben Lindstrombba81212001-06-25 05:01:22 +0000746static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100747process_setstat(void)
748{
749 Attrib *a;
750 u_int32_t id;
751 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000752 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100753
754 id = get_int();
755 name = get_string(NULL);
756 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000757 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100758 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000759 logit("set \"%s\" size %llu",
760 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100761 ret = truncate(name, a->size);
762 if (ret == -1)
763 status = errno_to_portable(errno);
764 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000765 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000766 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller7b28dc52000-09-05 13:34:53 +1100767 ret = chmod(name, a->perm & 0777);
768 if (ret == -1)
769 status = errno_to_portable(errno);
770 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000771 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000772 char buf[64];
773 time_t t = a->mtime;
774
775 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
776 localtime(&t));
777 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100778 ret = utimes(name, attrib_to_tv(a));
779 if (ret == -1)
780 status = errno_to_portable(errno);
781 }
Kevin Steves8e743932001-02-05 13:24:35 +0000782 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000783 logit("set \"%s\" owner %lu group %lu", name,
784 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000785 ret = chown(name, a->uid, a->gid);
786 if (ret == -1)
787 status = errno_to_portable(errno);
788 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100789 send_status(id, status);
790 xfree(name);
791}
792
Ben Lindstrombba81212001-06-25 05:01:22 +0000793static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100794process_fsetstat(void)
795{
796 Attrib *a;
797 u_int32_t id;
798 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000799 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000800
Damien Miller7b28dc52000-09-05 13:34:53 +1100801 id = get_int();
802 handle = get_handle();
803 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000804 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100805 fd = handle_to_fd(handle);
Damien Millerfef95ad2006-07-10 20:46:55 +1000806 if (fd < 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000807 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100808 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000809 char *name = handle_to_name(handle);
810
Damien Miller00c92172002-02-13 14:05:00 +1100811 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000812 logit("set \"%s\" size %llu",
813 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100814 ret = ftruncate(fd, a->size);
815 if (ret == -1)
816 status = errno_to_portable(errno);
817 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000818 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000819 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000820#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100821 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000822#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000823 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000824#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100825 if (ret == -1)
826 status = errno_to_portable(errno);
827 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000828 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000829 char buf[64];
830 time_t t = a->mtime;
831
832 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
833 localtime(&t));
834 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100835#ifdef HAVE_FUTIMES
836 ret = futimes(fd, attrib_to_tv(a));
837#else
838 ret = utimes(name, attrib_to_tv(a));
839#endif
840 if (ret == -1)
841 status = errno_to_portable(errno);
842 }
Kevin Steves8e743932001-02-05 13:24:35 +0000843 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000844 logit("set \"%s\" owner %lu group %lu", name,
845 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000846#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000847 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000848#else
849 ret = chown(name, a->uid, a->gid);
850#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000851 if (ret == -1)
852 status = errno_to_portable(errno);
853 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100854 }
855 send_status(id, status);
856}
857
Ben Lindstrombba81212001-06-25 05:01:22 +0000858static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100859process_opendir(void)
860{
861 DIR *dirp = NULL;
862 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000863 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100864 u_int32_t id;
865
866 id = get_int();
867 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000868 debug3("request %u: opendir", id);
869 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000870 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100871 if (dirp == NULL) {
872 status = errno_to_portable(errno);
873 } else {
Damien Miller00111382003-03-10 11:21:17 +1100874 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100875 if (handle < 0) {
876 closedir(dirp);
877 } else {
878 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000879 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100880 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000881
Damien Miller7b28dc52000-09-05 13:34:53 +1100882 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000883 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100884 send_status(id, status);
885 xfree(path);
886}
887
Ben Lindstrombba81212001-06-25 05:01:22 +0000888static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100889process_readdir(void)
890{
891 DIR *dirp;
892 struct dirent *dp;
893 char *path;
894 int handle;
895 u_int32_t id;
896
897 id = get_int();
898 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000899 debug("request %u: readdir \"%s\" (handle %d)", id,
900 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100901 dirp = handle_to_dir(handle);
902 path = handle_to_name(handle);
903 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000904 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100905 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100906 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +1000907 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +1100908 Stat *stats;
909 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000910
Damien Miller07d86be2006-03-26 14:19:21 +1100911 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100912 while ((dp = readdir(dirp)) != NULL) {
913 if (count >= nstats) {
914 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +1100915 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100916 }
917/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000918 snprintf(pathname, sizeof pathname, "%s%s%s", path,
919 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100920 if (lstat(pathname, &st) < 0)
921 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000922 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100923 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000924 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100925 count++;
926 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000927 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100928 if (count == 100)
929 break;
930 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000931 if (count > 0) {
932 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100933 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000934 xfree(stats[i].name);
935 xfree(stats[i].long_name);
936 }
937 } else {
938 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100939 }
940 xfree(stats);
941 }
942}
943
Ben Lindstrombba81212001-06-25 05:01:22 +0000944static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100945process_remove(void)
946{
947 char *name;
948 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000949 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100950 int ret;
951
952 id = get_int();
953 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000954 debug3("request %u: remove", id);
955 logit("remove name \"%s\"", name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000956 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000957 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100958 send_status(id, status);
959 xfree(name);
960}
961
Ben Lindstrombba81212001-06-25 05:01:22 +0000962static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100963process_mkdir(void)
964{
965 Attrib *a;
966 u_int32_t id;
967 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000968 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100969
970 id = get_int();
971 name = get_string(NULL);
972 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000973 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
974 a->perm & 0777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +1000975 debug3("request %u: mkdir", id);
976 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100977 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000978 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100979 send_status(id, status);
980 xfree(name);
981}
982
Ben Lindstrombba81212001-06-25 05:01:22 +0000983static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100984process_rmdir(void)
985{
986 u_int32_t id;
987 char *name;
988 int ret, status;
989
990 id = get_int();
991 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000992 debug3("request %u: rmdir", id);
993 logit("rmdir name \"%s\"", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100994 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000995 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100996 send_status(id, status);
997 xfree(name);
998}
999
Ben Lindstrombba81212001-06-25 05:01:22 +00001000static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001001process_realpath(void)
1002{
1003 char resolvedname[MAXPATHLEN];
1004 u_int32_t id;
1005 char *path;
1006
1007 id = get_int();
1008 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +00001009 if (path[0] == '\0') {
1010 xfree(path);
1011 path = xstrdup(".");
1012 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001013 debug3("request %u: realpath", id);
1014 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +11001015 if (realpath(path, resolvedname) == NULL) {
1016 send_status(id, errno_to_portable(errno));
1017 } else {
1018 Stat s;
1019 attrib_clear(&s.attrib);
1020 s.name = s.long_name = resolvedname;
1021 send_names(id, 1, &s);
1022 }
1023 xfree(path);
1024}
1025
Ben Lindstrombba81212001-06-25 05:01:22 +00001026static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001027process_rename(void)
1028{
1029 u_int32_t id;
1030 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001031 int status;
Damien Millerb3207e82003-03-26 16:01:11 +11001032 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +11001033
1034 id = get_int();
1035 oldpath = get_string(NULL);
1036 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001037 debug3("request %u: rename", id);
1038 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +11001039 status = SSH2_FX_FAILURE;
1040 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +11001041 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +11001042 else if (S_ISREG(sb.st_mode)) {
1043 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001044 if (link(oldpath, newpath) == -1) {
Darren Tuckere59b5082004-06-28 16:01:19 +10001045 if (errno == EOPNOTSUPP
1046#ifdef LINK_OPNOTSUPP_ERRNO
1047 || errno == LINK_OPNOTSUPP_ERRNO
1048#endif
1049 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001050 struct stat st;
1051
1052 /*
1053 * fs doesn't support links, so fall back to
1054 * stat+rename. This is racy.
1055 */
1056 if (stat(newpath, &st) == -1) {
1057 if (rename(oldpath, newpath) == -1)
1058 status =
1059 errno_to_portable(errno);
1060 else
1061 status = SSH2_FX_OK;
1062 }
1063 } else {
1064 status = errno_to_portable(errno);
1065 }
1066 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001067 status = errno_to_portable(errno);
1068 /* clean spare link */
1069 unlink(newpath);
1070 } else
1071 status = SSH2_FX_OK;
1072 } else if (stat(newpath, &sb) == -1) {
1073 if (rename(oldpath, newpath) == -1)
1074 status = errno_to_portable(errno);
1075 else
1076 status = SSH2_FX_OK;
1077 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001078 send_status(id, status);
1079 xfree(oldpath);
1080 xfree(newpath);
1081}
1082
Ben Lindstrombba81212001-06-25 05:01:22 +00001083static void
Damien Miller058316f2001-03-08 10:08:49 +11001084process_readlink(void)
1085{
1086 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001087 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001088 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001089 char *path;
1090
1091 id = get_int();
1092 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001093 debug3("request %u: readlink", id);
1094 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001095 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001096 send_status(id, errno_to_portable(errno));
1097 else {
1098 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001099
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001100 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001101 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001102 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001103 send_names(id, 1, &s);
1104 }
1105 xfree(path);
1106}
1107
Ben Lindstrombba81212001-06-25 05:01:22 +00001108static void
Damien Miller058316f2001-03-08 10:08:49 +11001109process_symlink(void)
1110{
1111 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +11001112 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001113 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001114
1115 id = get_int();
1116 oldpath = get_string(NULL);
1117 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001118 debug3("request %u: symlink", id);
1119 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001120 /* this will fail if 'newpath' exists */
1121 ret = symlink(oldpath, newpath);
1122 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001123 send_status(id, status);
1124 xfree(oldpath);
1125 xfree(newpath);
1126}
1127
Ben Lindstrombba81212001-06-25 05:01:22 +00001128static void
Damien Miller7c296612008-03-07 18:33:53 +11001129process_extended_posix_rename(u_int32_t id)
1130{
1131 char *oldpath, *newpath;
1132
1133 oldpath = get_string(NULL);
1134 newpath = get_string(NULL);
1135 debug3("request %u: posix-rename", id);
1136 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
1137 if (rename(oldpath, newpath) == -1)
1138 send_status(id, errno_to_portable(errno));
1139 else
1140 send_status(id, SSH2_FX_OK);
1141 xfree(oldpath);
1142 xfree(newpath);
1143}
1144
Darren Tucker5b2e2ba2008-06-08 09:25:28 +10001145#ifdef USE_STATVFS
Damien Miller7c296612008-03-07 18:33:53 +11001146static void
Damien Millerd671e5a2008-05-19 14:53:33 +10001147process_extended_statvfs(u_int32_t id)
1148{
1149 char *path;
1150 struct statvfs st;
1151
1152 path = get_string(NULL);
1153 debug3("request %u: statfs", id);
1154 logit("statfs \"%s\"", path);
1155
1156 if (statvfs(path, &st) != 0)
1157 send_status(id, errno_to_portable(errno));
1158 else
1159 send_statvfs(id, &st);
1160 xfree(path);
1161}
1162
1163static void
1164process_extended_fstatvfs(u_int32_t id)
1165{
1166 int handle, fd;
1167 struct statvfs st;
1168
1169 handle = get_handle();
1170 debug("request %u: fstatvfs \"%s\" (handle %u)",
1171 id, handle_to_name(handle), handle);
1172 if ((fd = handle_to_fd(handle)) < 0) {
1173 send_status(id, SSH2_FX_FAILURE);
1174 return;
1175 }
1176 if (fstatvfs(fd, &st) != 0)
1177 send_status(id, errno_to_portable(errno));
1178 else
1179 send_statvfs(id, &st);
1180}
Darren Tucker5b2e2ba2008-06-08 09:25:28 +10001181#endif
Damien Millerd671e5a2008-05-19 14:53:33 +10001182
1183static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001184process_extended(void)
1185{
1186 u_int32_t id;
1187 char *request;
1188
1189 id = get_int();
1190 request = get_string(NULL);
Damien Miller7c296612008-03-07 18:33:53 +11001191 if (strcmp(request, "posix-rename@openssh.com") == 0)
1192 process_extended_posix_rename(id);
Darren Tucker5b2e2ba2008-06-08 09:25:28 +10001193#ifdef USE_STATVFS
Damien Millerd671e5a2008-05-19 14:53:33 +10001194 else if (strcmp(request, "statvfs@openssh.com") == 0)
1195 process_extended_statvfs(id);
1196 else if (strcmp(request, "fstatvfs@openssh.com") == 0)
1197 process_extended_fstatvfs(id);
Darren Tucker5b2e2ba2008-06-08 09:25:28 +10001198#endif
Damien Miller7c296612008-03-07 18:33:53 +11001199 else
1200 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001201 xfree(request);
1202}
Damien Miller7b28dc52000-09-05 13:34:53 +11001203
1204/* stolen from ssh-agent */
1205
Ben Lindstrombba81212001-06-25 05:01:22 +00001206static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001207process(void)
1208{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001209 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +00001210 u_int buf_len;
1211 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001212 u_int type;
1213 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +11001214
Ben Lindstrom2c140472002-06-06 21:57:54 +00001215 buf_len = buffer_len(&iqueue);
1216 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001217 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001218 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001219 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001220 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001221 error("bad message from %s local user %s",
1222 client_addr, pw->pw_name);
Damien Millerdfc24252008-02-10 22:29:40 +11001223 sftp_server_cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001224 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001225 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001226 return;
1227 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001228 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001229 type = buffer_get_char(&iqueue);
1230 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001231 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001232 process_init();
1233 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001234 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +11001235 process_open();
1236 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001237 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001238 process_close();
1239 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001240 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +11001241 process_read();
1242 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001243 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001244 process_write();
1245 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001246 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001247 process_lstat();
1248 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001249 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001250 process_fstat();
1251 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001252 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001253 process_setstat();
1254 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001255 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001256 process_fsetstat();
1257 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001258 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001259 process_opendir();
1260 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001261 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001262 process_readdir();
1263 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001264 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001265 process_remove();
1266 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001267 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001268 process_mkdir();
1269 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001270 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001271 process_rmdir();
1272 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001273 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001274 process_realpath();
1275 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001276 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001277 process_stat();
1278 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001279 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001280 process_rename();
1281 break;
Damien Miller058316f2001-03-08 10:08:49 +11001282 case SSH2_FXP_READLINK:
1283 process_readlink();
1284 break;
1285 case SSH2_FXP_SYMLINK:
1286 process_symlink();
1287 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001288 case SSH2_FXP_EXTENDED:
1289 process_extended();
1290 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001291 default:
1292 error("Unknown message %d", type);
1293 break;
1294 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001295 /* discard the remaining bytes from the current packet */
Damien Millerdfc24252008-02-10 22:29:40 +11001296 if (buf_len < buffer_len(&iqueue)) {
1297 error("iqueue grew unexpectedly");
1298 sftp_server_cleanup_exit(255);
1299 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001300 consumed = buf_len - buffer_len(&iqueue);
Damien Millerdfc24252008-02-10 22:29:40 +11001301 if (msg_len < consumed) {
1302 error("msg_len %d < consumed %d", msg_len, consumed);
1303 sftp_server_cleanup_exit(255);
1304 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001305 if (msg_len > consumed)
1306 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001307}
1308
Damien Millerfef95ad2006-07-10 20:46:55 +10001309/* Cleanup handler that logs active handles upon normal exit */
1310void
Damien Millerdfc24252008-02-10 22:29:40 +11001311sftp_server_cleanup_exit(int i)
Damien Millerfef95ad2006-07-10 20:46:55 +10001312{
1313 if (pw != NULL && client_addr != NULL) {
1314 handle_log_exit();
1315 logit("session closed for local user %s from [%s]",
1316 pw->pw_name, client_addr);
1317 }
1318 _exit(i);
1319}
1320
1321static void
Damien Millerdfc24252008-02-10 22:29:40 +11001322sftp_server_usage(void)
Damien Millerfef95ad2006-07-10 20:46:55 +10001323{
1324 extern char *__progname;
1325
1326 fprintf(stderr,
1327 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1328 exit(1);
1329}
1330
Damien Miller7b28dc52000-09-05 13:34:53 +11001331int
Damien Millerd8cb1f12008-02-10 22:40:12 +11001332sftp_server_main(int argc, char **argv, struct passwd *user_pw)
Damien Miller7b28dc52000-09-05 13:34:53 +11001333{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001334 fd_set *rset, *wset;
Damien Millerfef95ad2006-07-10 20:46:55 +10001335 int in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001336 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001337 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
Darren Tuckere9405982007-05-20 15:09:04 +10001338 char *cp, buf[4*4096];
Damien Millerfef95ad2006-07-10 20:46:55 +10001339
Damien Millerfef95ad2006-07-10 20:46:55 +10001340 extern char *optarg;
1341 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001342
Damien Millerfef95ad2006-07-10 20:46:55 +10001343 __progname = ssh_get_progname(argv[0]);
1344 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001345
Damien Millerfef95ad2006-07-10 20:46:55 +10001346 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1347 switch (ch) {
1348 case 'c':
1349 /*
1350 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001351 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001352 */
1353 skipargs = 1;
1354 break;
1355 case 'e':
1356 log_stderr = 1;
1357 break;
1358 case 'l':
1359 log_level = log_level_number(optarg);
1360 if (log_level == SYSLOG_LEVEL_NOT_SET)
1361 error("Invalid log level \"%s\"", optarg);
1362 break;
1363 case 'f':
1364 log_facility = log_facility_number(optarg);
Damien Miller35e18db2007-09-17 16:11:33 +10001365 if (log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Millerfef95ad2006-07-10 20:46:55 +10001366 error("Invalid log facility \"%s\"", optarg);
1367 break;
1368 case 'h':
1369 default:
Damien Millerdfc24252008-02-10 22:29:40 +11001370 sftp_server_usage();
Damien Millerfef95ad2006-07-10 20:46:55 +10001371 }
1372 }
1373
1374 log_init(__progname, log_level, log_facility, log_stderr);
1375
1376 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1377 client_addr = xstrdup(cp);
Damien Millerdfc24252008-02-10 22:29:40 +11001378 if ((cp = strchr(client_addr, ' ')) == NULL) {
1379 error("Malformed SSH_CONNECTION variable: \"%s\"",
Damien Millerfef95ad2006-07-10 20:46:55 +10001380 getenv("SSH_CONNECTION"));
Damien Millerdfc24252008-02-10 22:29:40 +11001381 sftp_server_cleanup_exit(255);
1382 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001383 *cp = '\0';
1384 } else
1385 client_addr = xstrdup("UNKNOWN");
1386
Damien Millerd8cb1f12008-02-10 22:40:12 +11001387 pw = pwcopy(user_pw);
Damien Millerfef95ad2006-07-10 20:46:55 +10001388
1389 logit("session opened for local user %s from [%s]",
1390 pw->pw_name, client_addr);
1391
Damien Miller7b28dc52000-09-05 13:34:53 +11001392 in = dup(STDIN_FILENO);
1393 out = dup(STDOUT_FILENO);
1394
Damien Miller402b3312001-04-14 00:28:42 +10001395#ifdef HAVE_CYGWIN
1396 setmode(in, O_BINARY);
1397 setmode(out, O_BINARY);
1398#endif
1399
Damien Miller7b28dc52000-09-05 13:34:53 +11001400 max = 0;
1401 if (in > max)
1402 max = in;
1403 if (out > max)
1404 max = out;
1405
1406 buffer_init(&iqueue);
1407 buffer_init(&oqueue);
1408
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001409 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1410 rset = (fd_set *)xmalloc(set_size);
1411 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001412
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001413 for (;;) {
1414 memset(rset, 0, set_size);
1415 memset(wset, 0, set_size);
1416
Darren Tuckere9405982007-05-20 15:09:04 +10001417 /*
1418 * Ensure that we can read a full buffer and handle
1419 * the worst-case length packet it can generate,
1420 * otherwise apply backpressure by stopping reads.
1421 */
1422 if (buffer_check_alloc(&iqueue, sizeof(buf)) &&
1423 buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1424 FD_SET(in, rset);
1425
Damien Miller7b28dc52000-09-05 13:34:53 +11001426 olen = buffer_len(&oqueue);
1427 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001428 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001429
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001430 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001431 if (errno == EINTR)
1432 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001433 error("select: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001434 sftp_server_cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001435 }
1436
1437 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001438 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001439 len = read(in, buf, sizeof buf);
1440 if (len == 0) {
1441 debug("read eof");
Damien Millerdfc24252008-02-10 22:29:40 +11001442 sftp_server_cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001443 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001444 error("read: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001445 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001446 } else {
1447 buffer_append(&iqueue, buf, len);
1448 }
1449 }
1450 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001451 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001452 len = write(out, buffer_ptr(&oqueue), olen);
1453 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001454 error("write: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001455 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001456 } else {
1457 buffer_consume(&oqueue, len);
1458 }
1459 }
Darren Tuckere9405982007-05-20 15:09:04 +10001460
1461 /*
1462 * Process requests from client if we can fit the results
1463 * into the output buffer, otherwise stop processing input
1464 * and let the output queue drain.
1465 */
1466 if (buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1467 process();
Damien Miller7b28dc52000-09-05 13:34:53 +11001468 }
1469}