blob: d9549f5bc66c36be0141cd194c41948014662be5 [file] [log] [blame]
Damien Miller7c296612008-03-07 18:33:53 +11001/* $OpenBSD: sftp-server.c,v 1.78 2008/02/27 20:21:15 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
Damien Miller88f254b2006-03-15 11:25:13 +110026
27#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100028#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100029#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100030#include <pwd.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100031#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100032#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100033#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#include <pwd.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100035#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <stdarg.h>
Damien Miller7b28dc52000-09-05 13:34:53 +110038
Damien Miller7b28dc52000-09-05 13:34:53 +110039#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100040#include "buffer.h"
41#include "log.h"
Darren Tuckerce321d82005-10-03 18:11:24 +100042#include "misc.h"
Damien Millerfef95ad2006-07-10 20:46:55 +100043#include "uidswap.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110044
Ben Lindstrom2f959b42001-01-11 06:20:23 +000045#include "sftp.h"
Damien Miller33804262001-02-04 23:20:18 +110046#include "sftp-common.h"
Damien Miller7b28dc52000-09-05 13:34:53 +110047
48/* helper */
Ben Lindstrom2f959b42001-01-11 06:20:23 +000049#define get_int64() buffer_get_int64(&iqueue);
Damien Miller7b28dc52000-09-05 13:34:53 +110050#define get_int() buffer_get_int(&iqueue);
51#define get_string(lenp) buffer_get_string(&iqueue, lenp);
Damien Miller7b28dc52000-09-05 13:34:53 +110052
Damien Millerfef95ad2006-07-10 20:46:55 +100053/* Our verbosity */
54LogLevel log_level = SYSLOG_LEVEL_ERROR;
55
56/* Our client */
57struct passwd *pw = NULL;
58char *client_addr = NULL;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000059
Damien Miller7b28dc52000-09-05 13:34:53 +110060/* input and output queue */
61Buffer iqueue;
62Buffer oqueue;
63
Damien Miller058316f2001-03-08 10:08:49 +110064/* Version of client */
65int version;
66
Darren Tuckera6612d42003-06-28 12:39:03 +100067/* portable attributes, etc. */
Damien Miller7b28dc52000-09-05 13:34:53 +110068
Damien Miller7b28dc52000-09-05 13:34:53 +110069typedef struct Stat Stat;
70
Damien Miller33804262001-02-04 23:20:18 +110071struct Stat {
Damien Miller7b28dc52000-09-05 13:34:53 +110072 char *name;
73 char *long_name;
74 Attrib attrib;
75};
76
Ben Lindstrombba81212001-06-25 05:01:22 +000077static int
Damien Miller7b28dc52000-09-05 13:34:53 +110078errno_to_portable(int unixerrno)
79{
80 int ret = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +000081
Damien Miller7b28dc52000-09-05 13:34:53 +110082 switch (unixerrno) {
83 case 0:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000084 ret = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +110085 break;
86 case ENOENT:
87 case ENOTDIR:
88 case EBADF:
89 case ELOOP:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000090 ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller7b28dc52000-09-05 13:34:53 +110091 break;
92 case EPERM:
93 case EACCES:
94 case EFAULT:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000095 ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller7b28dc52000-09-05 13:34:53 +110096 break;
97 case ENAMETOOLONG:
98 case EINVAL:
Ben Lindstrom2f959b42001-01-11 06:20:23 +000099 ret = SSH2_FX_BAD_MESSAGE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100100 break;
101 default:
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000102 ret = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100103 break;
104 }
105 return ret;
106}
107
Ben Lindstrombba81212001-06-25 05:01:22 +0000108static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100109flags_from_portable(int pflags)
110{
111 int flags = 0;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000112
Ben Lindstrom36592512001-03-05 05:02:08 +0000113 if ((pflags & SSH2_FXF_READ) &&
114 (pflags & SSH2_FXF_WRITE)) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100115 flags = O_RDWR;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000116 } else if (pflags & SSH2_FXF_READ) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100117 flags = O_RDONLY;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000118 } else if (pflags & SSH2_FXF_WRITE) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100119 flags = O_WRONLY;
120 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000121 if (pflags & SSH2_FXF_CREAT)
Damien Miller7b28dc52000-09-05 13:34:53 +1100122 flags |= O_CREAT;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000123 if (pflags & SSH2_FXF_TRUNC)
Damien Miller7b28dc52000-09-05 13:34:53 +1100124 flags |= O_TRUNC;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000125 if (pflags & SSH2_FXF_EXCL)
Damien Miller7b28dc52000-09-05 13:34:53 +1100126 flags |= O_EXCL;
127 return flags;
128}
129
Damien Millerfef95ad2006-07-10 20:46:55 +1000130static const char *
131string_from_portable(int pflags)
132{
133 static char ret[128];
134
135 *ret = '\0';
136
137#define PAPPEND(str) { \
138 if (*ret != '\0') \
139 strlcat(ret, ",", sizeof(ret)); \
Damien Millerd7834352006-08-05 12:39:39 +1000140 strlcat(ret, str, sizeof(ret)); \
Damien Millerfef95ad2006-07-10 20:46:55 +1000141 }
142
143 if (pflags & SSH2_FXF_READ)
144 PAPPEND("READ")
145 if (pflags & SSH2_FXF_WRITE)
146 PAPPEND("WRITE")
147 if (pflags & SSH2_FXF_CREAT)
148 PAPPEND("CREATE")
149 if (pflags & SSH2_FXF_TRUNC)
150 PAPPEND("TRUNCATE")
151 if (pflags & SSH2_FXF_EXCL)
152 PAPPEND("EXCL")
153
154 return ret;
155}
156
Ben Lindstrombba81212001-06-25 05:01:22 +0000157static Attrib *
Damien Miller7b28dc52000-09-05 13:34:53 +1100158get_attrib(void)
159{
160 return decode_attrib(&iqueue);
161}
162
163/* handle handles */
164
165typedef struct Handle Handle;
166struct Handle {
167 int use;
168 DIR *dirp;
169 int fd;
170 char *name;
Damien Millerfef95ad2006-07-10 20:46:55 +1000171 u_int64_t bytes_read, bytes_write;
Damien Miller3397d0e2008-02-10 22:26:51 +1100172 int next_unused;
Damien Miller7b28dc52000-09-05 13:34:53 +1100173};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000174
Damien Miller7b28dc52000-09-05 13:34:53 +1100175enum {
176 HANDLE_UNUSED,
177 HANDLE_DIR,
178 HANDLE_FILE
179};
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000180
Damien Miller3397d0e2008-02-10 22:26:51 +1100181Handle *handles = NULL;
182u_int num_handles = 0;
183int first_unused_handle = -1;
Damien Miller7b28dc52000-09-05 13:34:53 +1100184
Damien Miller3397d0e2008-02-10 22:26:51 +1100185static void handle_unused(int i)
Damien Miller7b28dc52000-09-05 13:34:53 +1100186{
Damien Miller3397d0e2008-02-10 22:26:51 +1100187 handles[i].use = HANDLE_UNUSED;
188 handles[i].next_unused = first_unused_handle;
189 first_unused_handle = i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100190}
191
Ben Lindstrombba81212001-06-25 05:01:22 +0000192static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100193handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller7b28dc52000-09-05 13:34:53 +1100194{
Damien Miller3397d0e2008-02-10 22:26:51 +1100195 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000196
Damien Miller3397d0e2008-02-10 22:26:51 +1100197 if (first_unused_handle == -1) {
198 if (num_handles + 1 <= num_handles)
199 return -1;
200 num_handles++;
201 handles = xrealloc(handles, num_handles, sizeof(Handle));
202 handle_unused(num_handles - 1);
Damien Miller7b28dc52000-09-05 13:34:53 +1100203 }
Damien Miller3397d0e2008-02-10 22:26:51 +1100204
205 i = first_unused_handle;
206 first_unused_handle = handles[i].next_unused;
207
208 handles[i].use = use;
209 handles[i].dirp = dirp;
210 handles[i].fd = fd;
211 handles[i].name = xstrdup(name);
212 handles[i].bytes_read = handles[i].bytes_write = 0;
213
214 return i;
Damien Miller7b28dc52000-09-05 13:34:53 +1100215}
216
Ben Lindstrombba81212001-06-25 05:01:22 +0000217static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100218handle_is_ok(int i, int type)
219{
Damien Miller3397d0e2008-02-10 22:26:51 +1100220 return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
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_to_string(int handle, char **stringp, int *hlenp)
225{
Damien Miller7b28dc52000-09-05 13:34:53 +1100226 if (stringp == NULL || hlenp == NULL)
227 return -1;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000228 *stringp = xmalloc(sizeof(int32_t));
Damien Miller3f941882006-03-31 23:13:02 +1100229 put_u32(*stringp, handle);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000230 *hlenp = sizeof(int32_t);
Damien Miller7b28dc52000-09-05 13:34:53 +1100231 return 0;
232}
233
Ben Lindstrombba81212001-06-25 05:01:22 +0000234static int
Damien Millerf58b58c2003-11-17 21:18:23 +1100235handle_from_string(const char *handle, u_int hlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100236{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000237 int val;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000238
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000239 if (hlen != sizeof(int32_t))
Damien Miller7b28dc52000-09-05 13:34:53 +1100240 return -1;
Damien Miller3f941882006-03-31 23:13:02 +1100241 val = get_u32(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100242 if (handle_is_ok(val, HANDLE_FILE) ||
243 handle_is_ok(val, HANDLE_DIR))
244 return val;
245 return -1;
246}
247
Ben Lindstrombba81212001-06-25 05:01:22 +0000248static char *
Damien Miller7b28dc52000-09-05 13:34:53 +1100249handle_to_name(int handle)
250{
251 if (handle_is_ok(handle, HANDLE_DIR)||
252 handle_is_ok(handle, HANDLE_FILE))
253 return handles[handle].name;
254 return NULL;
255}
256
Ben Lindstrombba81212001-06-25 05:01:22 +0000257static DIR *
Damien Miller7b28dc52000-09-05 13:34:53 +1100258handle_to_dir(int handle)
259{
260 if (handle_is_ok(handle, HANDLE_DIR))
261 return handles[handle].dirp;
262 return NULL;
263}
264
Ben Lindstrombba81212001-06-25 05:01:22 +0000265static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100266handle_to_fd(int handle)
267{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000268 if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller7b28dc52000-09-05 13:34:53 +1100269 return handles[handle].fd;
270 return -1;
271}
272
Damien Millerfef95ad2006-07-10 20:46:55 +1000273static void
274handle_update_read(int handle, ssize_t bytes)
275{
276 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
277 handles[handle].bytes_read += bytes;
278}
279
280static void
281handle_update_write(int handle, ssize_t bytes)
282{
283 if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
284 handles[handle].bytes_write += bytes;
285}
286
287static u_int64_t
288handle_bytes_read(int handle)
289{
290 if (handle_is_ok(handle, HANDLE_FILE))
291 return (handles[handle].bytes_read);
292 return 0;
293}
294
295static u_int64_t
296handle_bytes_write(int handle)
297{
298 if (handle_is_ok(handle, HANDLE_FILE))
299 return (handles[handle].bytes_write);
300 return 0;
301}
302
Ben Lindstrombba81212001-06-25 05:01:22 +0000303static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100304handle_close(int handle)
305{
306 int ret = -1;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000307
Damien Miller7b28dc52000-09-05 13:34:53 +1100308 if (handle_is_ok(handle, HANDLE_FILE)) {
309 ret = close(handles[handle].fd);
Damien Miller00111382003-03-10 11:21:17 +1100310 xfree(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100311 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100312 } else if (handle_is_ok(handle, HANDLE_DIR)) {
313 ret = closedir(handles[handle].dirp);
Damien Miller00111382003-03-10 11:21:17 +1100314 xfree(handles[handle].name);
Damien Miller3397d0e2008-02-10 22:26:51 +1100315 handle_unused(handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100316 } else {
317 errno = ENOENT;
318 }
319 return ret;
320}
321
Damien Millerfef95ad2006-07-10 20:46:55 +1000322static void
323handle_log_close(int handle, char *emsg)
324{
325 if (handle_is_ok(handle, HANDLE_FILE)) {
326 logit("%s%sclose \"%s\" bytes read %llu written %llu",
327 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
328 handle_to_name(handle),
Darren Tucker86473c52007-05-20 14:59:32 +1000329 (unsigned long long)handle_bytes_read(handle),
330 (unsigned long long)handle_bytes_write(handle));
Damien Millerfef95ad2006-07-10 20:46:55 +1000331 } else {
332 logit("%s%sclosedir \"%s\"",
333 emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
334 handle_to_name(handle));
335 }
336}
337
338static void
339handle_log_exit(void)
340{
341 u_int i;
342
Damien Miller3397d0e2008-02-10 22:26:51 +1100343 for (i = 0; i < num_handles; i++)
Damien Millerfef95ad2006-07-10 20:46:55 +1000344 if (handles[i].use != HANDLE_UNUSED)
345 handle_log_close(i, "forced");
346}
347
Ben Lindstrombba81212001-06-25 05:01:22 +0000348static int
Damien Miller7b28dc52000-09-05 13:34:53 +1100349get_handle(void)
350{
351 char *handle;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000352 int val = -1;
Damien Millere4340be2000-09-16 13:29:08 +1100353 u_int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000354
Damien Miller7b28dc52000-09-05 13:34:53 +1100355 handle = get_string(&hlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000356 if (hlen < 256)
357 val = handle_from_string(handle, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100358 xfree(handle);
359 return val;
360}
361
362/* send replies */
363
Ben Lindstrombba81212001-06-25 05:01:22 +0000364static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100365send_msg(Buffer *m)
366{
367 int mlen = buffer_len(m);
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000368
Damien Miller7b28dc52000-09-05 13:34:53 +1100369 buffer_put_int(&oqueue, mlen);
370 buffer_append(&oqueue, buffer_ptr(m), mlen);
371 buffer_consume(m, mlen);
372}
373
Damien Millerfef95ad2006-07-10 20:46:55 +1000374static const char *
375status_to_message(u_int32_t status)
Damien Miller7b28dc52000-09-05 13:34:53 +1100376{
Damien Miller058316f2001-03-08 10:08:49 +1100377 const char *status_messages[] = {
378 "Success", /* SSH_FX_OK */
379 "End of file", /* SSH_FX_EOF */
380 "No such file", /* SSH_FX_NO_SUCH_FILE */
381 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
382 "Failure", /* SSH_FX_FAILURE */
383 "Bad message", /* SSH_FX_BAD_MESSAGE */
384 "No connection", /* SSH_FX_NO_CONNECTION */
385 "Connection lost", /* SSH_FX_CONNECTION_LOST */
386 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
387 "Unknown error" /* Others */
388 };
Damien Millerfef95ad2006-07-10 20:46:55 +1000389 return (status_messages[MIN(status,SSH2_FX_MAX)]);
390}
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000391
Damien Millerfef95ad2006-07-10 20:46:55 +1000392static void
393send_status(u_int32_t id, u_int32_t status)
394{
395 Buffer msg;
396
397 debug3("request %u: sent status %u", id, status);
398 if (log_level > SYSLOG_LEVEL_VERBOSE ||
399 (status != SSH2_FX_OK && status != SSH2_FX_EOF))
400 logit("sent status %s", status_to_message(status));
Damien Miller7b28dc52000-09-05 13:34:53 +1100401 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000402 buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100403 buffer_put_int(&msg, id);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000404 buffer_put_int(&msg, status);
Damien Miller058316f2001-03-08 10:08:49 +1100405 if (version >= 3) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000406 buffer_put_cstring(&msg, status_to_message(status));
Damien Miller058316f2001-03-08 10:08:49 +1100407 buffer_put_cstring(&msg, "");
408 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100409 send_msg(&msg);
410 buffer_free(&msg);
411}
Ben Lindstrombba81212001-06-25 05:01:22 +0000412static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100413send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100414{
415 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000416
Damien Miller7b28dc52000-09-05 13:34:53 +1100417 buffer_init(&msg);
418 buffer_put_char(&msg, type);
419 buffer_put_int(&msg, id);
420 buffer_put_string(&msg, data, dlen);
421 send_msg(&msg);
422 buffer_free(&msg);
423}
424
Ben Lindstrombba81212001-06-25 05:01:22 +0000425static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100426send_data(u_int32_t id, const char *data, int dlen)
Damien Miller7b28dc52000-09-05 13:34:53 +1100427{
Damien Millerfef95ad2006-07-10 20:46:55 +1000428 debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000429 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100430}
431
Ben Lindstrombba81212001-06-25 05:01:22 +0000432static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100433send_handle(u_int32_t id, int handle)
434{
435 char *string;
436 int hlen;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000437
Damien Miller7b28dc52000-09-05 13:34:53 +1100438 handle_to_string(handle, &string, &hlen);
Damien Millerfef95ad2006-07-10 20:46:55 +1000439 debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000440 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
Damien Miller7b28dc52000-09-05 13:34:53 +1100441 xfree(string);
442}
443
Ben Lindstrombba81212001-06-25 05:01:22 +0000444static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100445send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller7b28dc52000-09-05 13:34:53 +1100446{
447 Buffer msg;
448 int i;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000449
Damien Miller7b28dc52000-09-05 13:34:53 +1100450 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000451 buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller7b28dc52000-09-05 13:34:53 +1100452 buffer_put_int(&msg, id);
453 buffer_put_int(&msg, count);
Damien Millerfef95ad2006-07-10 20:46:55 +1000454 debug("request %u: sent names count %d", id, count);
Damien Miller7b28dc52000-09-05 13:34:53 +1100455 for (i = 0; i < count; i++) {
456 buffer_put_cstring(&msg, stats[i].name);
457 buffer_put_cstring(&msg, stats[i].long_name);
458 encode_attrib(&msg, &stats[i].attrib);
459 }
460 send_msg(&msg);
461 buffer_free(&msg);
462}
463
Ben Lindstrombba81212001-06-25 05:01:22 +0000464static void
Damien Millerf58b58c2003-11-17 21:18:23 +1100465send_attrib(u_int32_t id, const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100466{
467 Buffer msg;
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000468
Damien Millerfef95ad2006-07-10 20:46:55 +1000469 debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100470 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000471 buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller7b28dc52000-09-05 13:34:53 +1100472 buffer_put_int(&msg, id);
473 encode_attrib(&msg, a);
474 send_msg(&msg);
475 buffer_free(&msg);
476}
477
478/* parse incoming */
479
Ben Lindstrombba81212001-06-25 05:01:22 +0000480static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100481process_init(void)
482{
483 Buffer msg;
Damien Miller7b28dc52000-09-05 13:34:53 +1100484
Ben Lindstrom937df1d2002-06-06 21:58:35 +0000485 version = get_int();
Damien Millerfef95ad2006-07-10 20:46:55 +1000486 verbose("received client version %d", version);
Damien Miller7b28dc52000-09-05 13:34:53 +1100487 buffer_init(&msg);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000488 buffer_put_char(&msg, SSH2_FXP_VERSION);
489 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller7c296612008-03-07 18:33:53 +1100490 /* POSIX rename extension */
491 buffer_put_cstring(&msg, "posix-rename@openssh.com");
492 buffer_put_cstring(&msg, "1"); /* version */
Damien Miller7b28dc52000-09-05 13:34:53 +1100493 send_msg(&msg);
494 buffer_free(&msg);
495}
496
Ben Lindstrombba81212001-06-25 05:01:22 +0000497static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100498process_open(void)
499{
500 u_int32_t id, pflags;
501 Attrib *a;
502 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000503 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100504
505 id = get_int();
506 name = get_string(NULL);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000507 pflags = get_int(); /* portable flags */
Damien Miller6444fe92006-07-10 21:31:27 +1000508 debug3("request %u: open flags %d", id, pflags);
Damien Miller7b28dc52000-09-05 13:34:53 +1100509 a = get_attrib();
510 flags = flags_from_portable(pflags);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000511 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
Damien Millerfef95ad2006-07-10 20:46:55 +1000512 logit("open \"%s\" flags %s mode 0%o",
513 name, string_from_portable(pflags), mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100514 fd = open(name, flags, mode);
515 if (fd < 0) {
516 status = errno_to_portable(errno);
517 } else {
Damien Miller00111382003-03-10 11:21:17 +1100518 handle = handle_new(HANDLE_FILE, name, fd, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100519 if (handle < 0) {
520 close(fd);
521 } else {
522 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000523 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100524 }
525 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000526 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100527 send_status(id, status);
528 xfree(name);
529}
530
Ben Lindstrombba81212001-06-25 05:01:22 +0000531static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100532process_close(void)
533{
534 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000535 int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100536
537 id = get_int();
538 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000539 debug3("request %u: close handle %u", id, handle);
540 handle_log_close(handle, NULL);
Damien Miller7b28dc52000-09-05 13:34:53 +1100541 ret = handle_close(handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000542 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100543 send_status(id, status);
544}
545
Ben Lindstrombba81212001-06-25 05:01:22 +0000546static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100547process_read(void)
548{
549 char buf[64*1024];
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000550 u_int32_t id, len;
551 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100552 u_int64_t off;
553
554 id = get_int();
555 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000556 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100557 len = get_int();
558
Damien Millerfef95ad2006-07-10 20:46:55 +1000559 debug("request %u: read \"%s\" (handle %d) off %llu len %d",
560 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100561 if (len > sizeof buf) {
562 len = sizeof buf;
Damien Millerfef95ad2006-07-10 20:46:55 +1000563 debug2("read change len %d", len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100564 }
565 fd = handle_to_fd(handle);
566 if (fd >= 0) {
567 if (lseek(fd, off, SEEK_SET) < 0) {
568 error("process_read: seek failed");
569 status = errno_to_portable(errno);
570 } else {
571 ret = read(fd, buf, len);
572 if (ret < 0) {
573 status = errno_to_portable(errno);
574 } else if (ret == 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000575 status = SSH2_FX_EOF;
Damien Miller7b28dc52000-09-05 13:34:53 +1100576 } else {
577 send_data(id, buf, ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000578 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000579 handle_update_read(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100580 }
581 }
582 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000583 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100584 send_status(id, status);
585}
586
Ben Lindstrombba81212001-06-25 05:01:22 +0000587static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100588process_write(void)
589{
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000590 u_int32_t id;
Damien Miller7b28dc52000-09-05 13:34:53 +1100591 u_int64_t off;
Damien Millere4340be2000-09-16 13:29:08 +1100592 u_int len;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000593 int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100594 char *data;
595
596 id = get_int();
597 handle = get_handle();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000598 off = get_int64();
Damien Miller7b28dc52000-09-05 13:34:53 +1100599 data = get_string(&len);
600
Damien Millerfef95ad2006-07-10 20:46:55 +1000601 debug("request %u: write \"%s\" (handle %d) off %llu len %d",
602 id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller7b28dc52000-09-05 13:34:53 +1100603 fd = handle_to_fd(handle);
604 if (fd >= 0) {
605 if (lseek(fd, off, SEEK_SET) < 0) {
606 status = errno_to_portable(errno);
607 error("process_write: seek failed");
608 } else {
609/* XXX ATOMICIO ? */
610 ret = write(fd, data, len);
Damien Millereccb9de2005-06-17 12:59:34 +1000611 if (ret < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100612 error("process_write: write failed");
613 status = errno_to_portable(errno);
Damien Millereccb9de2005-06-17 12:59:34 +1000614 } else if ((size_t)ret == len) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000615 status = SSH2_FX_OK;
Damien Millerfef95ad2006-07-10 20:46:55 +1000616 handle_update_write(handle, ret);
Damien Miller7b28dc52000-09-05 13:34:53 +1100617 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000618 debug2("nothing at all written");
Damien Miller7b28dc52000-09-05 13:34:53 +1100619 }
620 }
621 }
622 send_status(id, status);
623 xfree(data);
624}
625
Ben Lindstrombba81212001-06-25 05:01:22 +0000626static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100627process_do_stat(int do_lstat)
628{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000629 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100630 struct stat st;
631 u_int32_t id;
632 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000633 int ret, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100634
635 id = get_int();
636 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000637 debug3("request %u: %sstat", id, do_lstat ? "l" : "");
638 verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100639 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
640 if (ret < 0) {
641 status = errno_to_portable(errno);
642 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000643 stat_to_attrib(&st, &a);
644 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000645 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100646 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000647 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100648 send_status(id, status);
649 xfree(name);
650}
651
Ben Lindstrombba81212001-06-25 05:01:22 +0000652static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100653process_stat(void)
654{
655 process_do_stat(0);
656}
657
Ben Lindstrombba81212001-06-25 05:01:22 +0000658static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100659process_lstat(void)
660{
661 process_do_stat(1);
662}
663
Ben Lindstrombba81212001-06-25 05:01:22 +0000664static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100665process_fstat(void)
666{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000667 Attrib a;
Damien Miller7b28dc52000-09-05 13:34:53 +1100668 struct stat st;
669 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000670 int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100671
672 id = get_int();
673 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000674 debug("request %u: fstat \"%s\" (handle %u)",
675 id, handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100676 fd = handle_to_fd(handle);
Damien Millere2334d62007-01-05 16:31:02 +1100677 if (fd >= 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100678 ret = fstat(fd, &st);
679 if (ret < 0) {
680 status = errno_to_portable(errno);
681 } else {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000682 stat_to_attrib(&st, &a);
683 send_attrib(id, &a);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000684 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100685 }
686 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000687 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100688 send_status(id, status);
689}
690
Ben Lindstrombba81212001-06-25 05:01:22 +0000691static struct timeval *
Damien Millerf58b58c2003-11-17 21:18:23 +1100692attrib_to_tv(const Attrib *a)
Damien Miller7b28dc52000-09-05 13:34:53 +1100693{
694 static struct timeval tv[2];
Ben Lindstrom1addabd2001-03-05 07:09:11 +0000695
Damien Miller7b28dc52000-09-05 13:34:53 +1100696 tv[0].tv_sec = a->atime;
697 tv[0].tv_usec = 0;
698 tv[1].tv_sec = a->mtime;
699 tv[1].tv_usec = 0;
700 return tv;
701}
702
Ben Lindstrombba81212001-06-25 05:01:22 +0000703static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100704process_setstat(void)
705{
706 Attrib *a;
707 u_int32_t id;
708 char *name;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000709 int status = SSH2_FX_OK, ret;
Damien Miller7b28dc52000-09-05 13:34:53 +1100710
711 id = get_int();
712 name = get_string(NULL);
713 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000714 debug("request %u: setstat name \"%s\"", id, name);
Damien Miller00c92172002-02-13 14:05:00 +1100715 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000716 logit("set \"%s\" size %llu",
717 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100718 ret = truncate(name, a->size);
719 if (ret == -1)
720 status = errno_to_portable(errno);
721 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000722 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000723 logit("set \"%s\" mode %04o", name, a->perm);
Damien Miller7b28dc52000-09-05 13:34:53 +1100724 ret = chmod(name, a->perm & 0777);
725 if (ret == -1)
726 status = errno_to_portable(errno);
727 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000728 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000729 char buf[64];
730 time_t t = a->mtime;
731
732 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
733 localtime(&t));
734 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100735 ret = utimes(name, attrib_to_tv(a));
736 if (ret == -1)
737 status = errno_to_portable(errno);
738 }
Kevin Steves8e743932001-02-05 13:24:35 +0000739 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000740 logit("set \"%s\" owner %lu group %lu", name,
741 (u_long)a->uid, (u_long)a->gid);
Kevin Steves8e743932001-02-05 13:24:35 +0000742 ret = chown(name, a->uid, a->gid);
743 if (ret == -1)
744 status = errno_to_portable(errno);
745 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100746 send_status(id, status);
747 xfree(name);
748}
749
Ben Lindstrombba81212001-06-25 05:01:22 +0000750static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100751process_fsetstat(void)
752{
753 Attrib *a;
754 u_int32_t id;
755 int handle, fd, ret;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000756 int status = SSH2_FX_OK;
Kevin Stevesf7ffab32001-01-24 20:11:06 +0000757
Damien Miller7b28dc52000-09-05 13:34:53 +1100758 id = get_int();
759 handle = get_handle();
760 a = get_attrib();
Damien Millerfef95ad2006-07-10 20:46:55 +1000761 debug("request %u: fsetstat handle %d", id, handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100762 fd = handle_to_fd(handle);
Damien Millerfef95ad2006-07-10 20:46:55 +1000763 if (fd < 0) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000764 status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100765 } else {
Damien Millerfef95ad2006-07-10 20:46:55 +1000766 char *name = handle_to_name(handle);
767
Damien Miller00c92172002-02-13 14:05:00 +1100768 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
Darren Tucker86473c52007-05-20 14:59:32 +1000769 logit("set \"%s\" size %llu",
770 name, (unsigned long long)a->size);
Damien Miller00c92172002-02-13 14:05:00 +1100771 ret = ftruncate(fd, a->size);
772 if (ret == -1)
773 status = errno_to_portable(errno);
774 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000775 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000776 logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000777#ifdef HAVE_FCHMOD
Damien Miller7b28dc52000-09-05 13:34:53 +1100778 ret = fchmod(fd, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000779#else
Kevin Stevesb6b37ba2001-01-24 20:01:44 +0000780 ret = chmod(name, a->perm & 0777);
Ben Lindstrom200e3c92001-01-15 01:56:46 +0000781#endif
Damien Miller7b28dc52000-09-05 13:34:53 +1100782 if (ret == -1)
783 status = errno_to_portable(errno);
784 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000785 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000786 char buf[64];
787 time_t t = a->mtime;
788
789 strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
790 localtime(&t));
791 logit("set \"%s\" modtime %s", name, buf);
Damien Miller7b28dc52000-09-05 13:34:53 +1100792#ifdef HAVE_FUTIMES
793 ret = futimes(fd, attrib_to_tv(a));
794#else
795 ret = utimes(name, attrib_to_tv(a));
796#endif
797 if (ret == -1)
798 status = errno_to_portable(errno);
799 }
Kevin Steves8e743932001-02-05 13:24:35 +0000800 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
Damien Millerfef95ad2006-07-10 20:46:55 +1000801 logit("set \"%s\" owner %lu group %lu", name,
802 (u_long)a->uid, (u_long)a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000803#ifdef HAVE_FCHOWN
Kevin Steves8e743932001-02-05 13:24:35 +0000804 ret = fchown(fd, a->uid, a->gid);
Ben Lindstrom34bb0c72001-02-13 02:40:56 +0000805#else
806 ret = chown(name, a->uid, a->gid);
807#endif
Kevin Steves8e743932001-02-05 13:24:35 +0000808 if (ret == -1)
809 status = errno_to_portable(errno);
810 }
Damien Miller7b28dc52000-09-05 13:34:53 +1100811 }
812 send_status(id, status);
813}
814
Ben Lindstrombba81212001-06-25 05:01:22 +0000815static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100816process_opendir(void)
817{
818 DIR *dirp = NULL;
819 char *path;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000820 int handle, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100821 u_int32_t id;
822
823 id = get_int();
824 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000825 debug3("request %u: opendir", id);
826 logit("opendir \"%s\"", path);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000827 dirp = opendir(path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100828 if (dirp == NULL) {
829 status = errno_to_portable(errno);
830 } else {
Damien Miller00111382003-03-10 11:21:17 +1100831 handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller7b28dc52000-09-05 13:34:53 +1100832 if (handle < 0) {
833 closedir(dirp);
834 } else {
835 send_handle(id, handle);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000836 status = SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100837 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000838
Damien Miller7b28dc52000-09-05 13:34:53 +1100839 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000840 if (status != SSH2_FX_OK)
Damien Miller7b28dc52000-09-05 13:34:53 +1100841 send_status(id, status);
842 xfree(path);
843}
844
Ben Lindstrombba81212001-06-25 05:01:22 +0000845static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100846process_readdir(void)
847{
848 DIR *dirp;
849 struct dirent *dp;
850 char *path;
851 int handle;
852 u_int32_t id;
853
854 id = get_int();
855 handle = get_handle();
Damien Millerfef95ad2006-07-10 20:46:55 +1000856 debug("request %u: readdir \"%s\" (handle %d)", id,
857 handle_to_name(handle), handle);
Damien Miller7b28dc52000-09-05 13:34:53 +1100858 dirp = handle_to_dir(handle);
859 path = handle_to_name(handle);
860 if (dirp == NULL || path == NULL) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000861 send_status(id, SSH2_FX_FAILURE);
Damien Miller7b28dc52000-09-05 13:34:53 +1100862 } else {
Damien Miller7b28dc52000-09-05 13:34:53 +1100863 struct stat st;
Damien Millerfef95ad2006-07-10 20:46:55 +1000864 char pathname[MAXPATHLEN];
Damien Miller7b28dc52000-09-05 13:34:53 +1100865 Stat *stats;
866 int nstats = 10, count = 0, i;
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000867
Damien Miller07d86be2006-03-26 14:19:21 +1100868 stats = xcalloc(nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100869 while ((dp = readdir(dirp)) != NULL) {
870 if (count >= nstats) {
871 nstats *= 2;
Damien Miller36812092006-03-26 14:22:47 +1100872 stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller7b28dc52000-09-05 13:34:53 +1100873 }
874/* XXX OVERFLOW ? */
Ben Lindstrom95148e32001-08-06 21:30:53 +0000875 snprintf(pathname, sizeof pathname, "%s%s%s", path,
876 strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100877 if (lstat(pathname, &st) < 0)
878 continue;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000879 stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller7b28dc52000-09-05 13:34:53 +1100880 stats[count].name = xstrdup(dp->d_name);
Damien Millere1a49812002-09-12 09:54:25 +1000881 stats[count].long_name = ls_file(dp->d_name, &st, 0);
Damien Miller7b28dc52000-09-05 13:34:53 +1100882 count++;
883 /* send up to 100 entries in one message */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000884 /* XXX check packet size instead */
Damien Miller7b28dc52000-09-05 13:34:53 +1100885 if (count == 100)
886 break;
887 }
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000888 if (count > 0) {
889 send_names(id, count, stats);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100890 for (i = 0; i < count; i++) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000891 xfree(stats[i].name);
892 xfree(stats[i].long_name);
893 }
894 } else {
895 send_status(id, SSH2_FX_EOF);
Damien Miller7b28dc52000-09-05 13:34:53 +1100896 }
897 xfree(stats);
898 }
899}
900
Ben Lindstrombba81212001-06-25 05:01:22 +0000901static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100902process_remove(void)
903{
904 char *name;
905 u_int32_t id;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000906 int status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100907 int ret;
908
909 id = get_int();
910 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000911 debug3("request %u: remove", id);
912 logit("remove name \"%s\"", name);
Kevin Stevesa074feb2000-12-21 22:33:45 +0000913 ret = unlink(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000914 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100915 send_status(id, status);
916 xfree(name);
917}
918
Ben Lindstrombba81212001-06-25 05:01:22 +0000919static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100920process_mkdir(void)
921{
922 Attrib *a;
923 u_int32_t id;
924 char *name;
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000925 int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller7b28dc52000-09-05 13:34:53 +1100926
927 id = get_int();
928 name = get_string(NULL);
929 a = get_attrib();
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000930 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
931 a->perm & 0777 : 0777;
Damien Millerfef95ad2006-07-10 20:46:55 +1000932 debug3("request %u: mkdir", id);
933 logit("mkdir name \"%s\" mode 0%o", name, mode);
Damien Miller7b28dc52000-09-05 13:34:53 +1100934 ret = mkdir(name, mode);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000935 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100936 send_status(id, status);
937 xfree(name);
938}
939
Ben Lindstrombba81212001-06-25 05:01:22 +0000940static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100941process_rmdir(void)
942{
943 u_int32_t id;
944 char *name;
945 int ret, status;
946
947 id = get_int();
948 name = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000949 debug3("request %u: rmdir", id);
950 logit("rmdir name \"%s\"", name);
Damien Miller7b28dc52000-09-05 13:34:53 +1100951 ret = rmdir(name);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000952 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller7b28dc52000-09-05 13:34:53 +1100953 send_status(id, status);
954 xfree(name);
955}
956
Ben Lindstrombba81212001-06-25 05:01:22 +0000957static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100958process_realpath(void)
959{
960 char resolvedname[MAXPATHLEN];
961 u_int32_t id;
962 char *path;
963
964 id = get_int();
965 path = get_string(NULL);
Ben Lindstromfa1b3d02000-12-10 01:55:37 +0000966 if (path[0] == '\0') {
967 xfree(path);
968 path = xstrdup(".");
969 }
Damien Millerfef95ad2006-07-10 20:46:55 +1000970 debug3("request %u: realpath", id);
971 verbose("realpath \"%s\"", path);
Damien Miller7b28dc52000-09-05 13:34:53 +1100972 if (realpath(path, resolvedname) == NULL) {
973 send_status(id, errno_to_portable(errno));
974 } else {
975 Stat s;
976 attrib_clear(&s.attrib);
977 s.name = s.long_name = resolvedname;
978 send_names(id, 1, &s);
979 }
980 xfree(path);
981}
982
Ben Lindstrombba81212001-06-25 05:01:22 +0000983static void
Damien Miller7b28dc52000-09-05 13:34:53 +1100984process_rename(void)
985{
986 u_int32_t id;
987 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +1100988 int status;
Damien Millerb3207e82003-03-26 16:01:11 +1100989 struct stat sb;
Damien Miller7b28dc52000-09-05 13:34:53 +1100990
991 id = get_int();
992 oldpath = get_string(NULL);
993 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +1000994 debug3("request %u: rename", id);
995 logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
Damien Millerb3207e82003-03-26 16:01:11 +1100996 status = SSH2_FX_FAILURE;
997 if (lstat(oldpath, &sb) == -1)
Damien Miller9e51a732003-02-24 11:58:44 +1100998 status = errno_to_portable(errno);
Damien Millerb3207e82003-03-26 16:01:11 +1100999 else if (S_ISREG(sb.st_mode)) {
1000 /* Race-free rename of regular files */
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001001 if (link(oldpath, newpath) == -1) {
Darren Tuckere59b5082004-06-28 16:01:19 +10001002 if (errno == EOPNOTSUPP
1003#ifdef LINK_OPNOTSUPP_ERRNO
1004 || errno == LINK_OPNOTSUPP_ERRNO
1005#endif
1006 ) {
Darren Tuckeraedc1d62004-06-25 17:06:02 +10001007 struct stat st;
1008
1009 /*
1010 * fs doesn't support links, so fall back to
1011 * stat+rename. This is racy.
1012 */
1013 if (stat(newpath, &st) == -1) {
1014 if (rename(oldpath, newpath) == -1)
1015 status =
1016 errno_to_portable(errno);
1017 else
1018 status = SSH2_FX_OK;
1019 }
1020 } else {
1021 status = errno_to_portable(errno);
1022 }
1023 } else if (unlink(oldpath) == -1) {
Damien Millerb3207e82003-03-26 16:01:11 +11001024 status = errno_to_portable(errno);
1025 /* clean spare link */
1026 unlink(newpath);
1027 } else
1028 status = SSH2_FX_OK;
1029 } else if (stat(newpath, &sb) == -1) {
1030 if (rename(oldpath, newpath) == -1)
1031 status = errno_to_portable(errno);
1032 else
1033 status = SSH2_FX_OK;
1034 }
Damien Miller7b28dc52000-09-05 13:34:53 +11001035 send_status(id, status);
1036 xfree(oldpath);
1037 xfree(newpath);
1038}
1039
Ben Lindstrombba81212001-06-25 05:01:22 +00001040static void
Damien Miller058316f2001-03-08 10:08:49 +11001041process_readlink(void)
1042{
1043 u_int32_t id;
Ben Lindstromabbb73d2001-05-17 03:14:57 +00001044 int len;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001045 char buf[MAXPATHLEN];
Damien Miller058316f2001-03-08 10:08:49 +11001046 char *path;
1047
1048 id = get_int();
1049 path = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001050 debug3("request %u: readlink", id);
1051 verbose("readlink \"%s\"", path);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001052 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller058316f2001-03-08 10:08:49 +11001053 send_status(id, errno_to_portable(errno));
1054 else {
1055 Stat s;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001056
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001057 buf[len] = '\0';
Damien Miller058316f2001-03-08 10:08:49 +11001058 attrib_clear(&s.attrib);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001059 s.name = s.long_name = buf;
Damien Miller058316f2001-03-08 10:08:49 +11001060 send_names(id, 1, &s);
1061 }
1062 xfree(path);
1063}
1064
Ben Lindstrombba81212001-06-25 05:01:22 +00001065static void
Damien Miller058316f2001-03-08 10:08:49 +11001066process_symlink(void)
1067{
1068 u_int32_t id;
Damien Miller058316f2001-03-08 10:08:49 +11001069 char *oldpath, *newpath;
Damien Miller9e51a732003-02-24 11:58:44 +11001070 int ret, status;
Damien Miller058316f2001-03-08 10:08:49 +11001071
1072 id = get_int();
1073 oldpath = get_string(NULL);
1074 newpath = get_string(NULL);
Damien Millerfef95ad2006-07-10 20:46:55 +10001075 debug3("request %u: symlink", id);
1076 logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
Damien Miller9e51a732003-02-24 11:58:44 +11001077 /* this will fail if 'newpath' exists */
1078 ret = symlink(oldpath, newpath);
1079 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller058316f2001-03-08 10:08:49 +11001080 send_status(id, status);
1081 xfree(oldpath);
1082 xfree(newpath);
1083}
1084
Ben Lindstrombba81212001-06-25 05:01:22 +00001085static void
Damien Miller7c296612008-03-07 18:33:53 +11001086process_extended_posix_rename(u_int32_t id)
1087{
1088 char *oldpath, *newpath;
1089
1090 oldpath = get_string(NULL);
1091 newpath = get_string(NULL);
1092 debug3("request %u: posix-rename", id);
1093 logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
1094 if (rename(oldpath, newpath) == -1)
1095 send_status(id, errno_to_portable(errno));
1096 else
1097 send_status(id, SSH2_FX_OK);
1098 xfree(oldpath);
1099 xfree(newpath);
1100}
1101
1102static void
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001103process_extended(void)
1104{
1105 u_int32_t id;
1106 char *request;
1107
1108 id = get_int();
1109 request = get_string(NULL);
Damien Miller7c296612008-03-07 18:33:53 +11001110 if (strcmp(request, "posix-rename@openssh.com") == 0)
1111 process_extended_posix_rename(id);
1112 else
1113 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001114 xfree(request);
1115}
Damien Miller7b28dc52000-09-05 13:34:53 +11001116
1117/* stolen from ssh-agent */
1118
Ben Lindstrombba81212001-06-25 05:01:22 +00001119static void
Damien Miller7b28dc52000-09-05 13:34:53 +11001120process(void)
1121{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001122 u_int msg_len;
Ben Lindstrom2c140472002-06-06 21:57:54 +00001123 u_int buf_len;
1124 u_int consumed;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001125 u_int type;
1126 u_char *cp;
Damien Miller7b28dc52000-09-05 13:34:53 +11001127
Ben Lindstrom2c140472002-06-06 21:57:54 +00001128 buf_len = buffer_len(&iqueue);
1129 if (buf_len < 5)
Damien Miller7b28dc52000-09-05 13:34:53 +11001130 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +11001131 cp = buffer_ptr(&iqueue);
Damien Miller3f941882006-03-31 23:13:02 +11001132 msg_len = get_u32(cp);
Damien Miller54446182006-01-02 23:40:50 +11001133 if (msg_len > SFTP_MAX_MSG_LENGTH) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001134 error("bad message from %s local user %s",
1135 client_addr, pw->pw_name);
Damien Millerdfc24252008-02-10 22:29:40 +11001136 sftp_server_cleanup_exit(11);
Damien Miller7b28dc52000-09-05 13:34:53 +11001137 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001138 if (buf_len < msg_len + 4)
Damien Miller7b28dc52000-09-05 13:34:53 +11001139 return;
1140 buffer_consume(&iqueue, 4);
Ben Lindstrom2c140472002-06-06 21:57:54 +00001141 buf_len -= 4;
Damien Miller7b28dc52000-09-05 13:34:53 +11001142 type = buffer_get_char(&iqueue);
1143 switch (type) {
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001144 case SSH2_FXP_INIT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001145 process_init();
1146 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001147 case SSH2_FXP_OPEN:
Damien Miller7b28dc52000-09-05 13:34:53 +11001148 process_open();
1149 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001150 case SSH2_FXP_CLOSE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001151 process_close();
1152 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001153 case SSH2_FXP_READ:
Damien Miller7b28dc52000-09-05 13:34:53 +11001154 process_read();
1155 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001156 case SSH2_FXP_WRITE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001157 process_write();
1158 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001159 case SSH2_FXP_LSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001160 process_lstat();
1161 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001162 case SSH2_FXP_FSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001163 process_fstat();
1164 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001165 case SSH2_FXP_SETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001166 process_setstat();
1167 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001168 case SSH2_FXP_FSETSTAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001169 process_fsetstat();
1170 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001171 case SSH2_FXP_OPENDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001172 process_opendir();
1173 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001174 case SSH2_FXP_READDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001175 process_readdir();
1176 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001177 case SSH2_FXP_REMOVE:
Damien Miller7b28dc52000-09-05 13:34:53 +11001178 process_remove();
1179 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001180 case SSH2_FXP_MKDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001181 process_mkdir();
1182 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001183 case SSH2_FXP_RMDIR:
Damien Miller7b28dc52000-09-05 13:34:53 +11001184 process_rmdir();
1185 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001186 case SSH2_FXP_REALPATH:
Damien Miller7b28dc52000-09-05 13:34:53 +11001187 process_realpath();
1188 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001189 case SSH2_FXP_STAT:
Damien Miller7b28dc52000-09-05 13:34:53 +11001190 process_stat();
1191 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001192 case SSH2_FXP_RENAME:
Damien Miller7b28dc52000-09-05 13:34:53 +11001193 process_rename();
1194 break;
Damien Miller058316f2001-03-08 10:08:49 +11001195 case SSH2_FXP_READLINK:
1196 process_readlink();
1197 break;
1198 case SSH2_FXP_SYMLINK:
1199 process_symlink();
1200 break;
Ben Lindstrom2f959b42001-01-11 06:20:23 +00001201 case SSH2_FXP_EXTENDED:
1202 process_extended();
1203 break;
Damien Miller7b28dc52000-09-05 13:34:53 +11001204 default:
1205 error("Unknown message %d", type);
1206 break;
1207 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001208 /* discard the remaining bytes from the current packet */
Damien Millerdfc24252008-02-10 22:29:40 +11001209 if (buf_len < buffer_len(&iqueue)) {
1210 error("iqueue grew unexpectedly");
1211 sftp_server_cleanup_exit(255);
1212 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001213 consumed = buf_len - buffer_len(&iqueue);
Damien Millerdfc24252008-02-10 22:29:40 +11001214 if (msg_len < consumed) {
1215 error("msg_len %d < consumed %d", msg_len, consumed);
1216 sftp_server_cleanup_exit(255);
1217 }
Ben Lindstrom2c140472002-06-06 21:57:54 +00001218 if (msg_len > consumed)
1219 buffer_consume(&iqueue, msg_len - consumed);
Damien Miller7b28dc52000-09-05 13:34:53 +11001220}
1221
Damien Millerfef95ad2006-07-10 20:46:55 +10001222/* Cleanup handler that logs active handles upon normal exit */
1223void
Damien Millerdfc24252008-02-10 22:29:40 +11001224sftp_server_cleanup_exit(int i)
Damien Millerfef95ad2006-07-10 20:46:55 +10001225{
1226 if (pw != NULL && client_addr != NULL) {
1227 handle_log_exit();
1228 logit("session closed for local user %s from [%s]",
1229 pw->pw_name, client_addr);
1230 }
1231 _exit(i);
1232}
1233
1234static void
Damien Millerdfc24252008-02-10 22:29:40 +11001235sftp_server_usage(void)
Damien Millerfef95ad2006-07-10 20:46:55 +10001236{
1237 extern char *__progname;
1238
1239 fprintf(stderr,
1240 "usage: %s [-he] [-l log_level] [-f log_facility]\n", __progname);
1241 exit(1);
1242}
1243
Damien Miller7b28dc52000-09-05 13:34:53 +11001244int
Damien Millerd8cb1f12008-02-10 22:40:12 +11001245sftp_server_main(int argc, char **argv, struct passwd *user_pw)
Damien Miller7b28dc52000-09-05 13:34:53 +11001246{
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001247 fd_set *rset, *wset;
Damien Millerfef95ad2006-07-10 20:46:55 +10001248 int in, out, max, ch, skipargs = 0, log_stderr = 0;
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001249 ssize_t len, olen, set_size;
Damien Millerfef95ad2006-07-10 20:46:55 +10001250 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
Darren Tuckere9405982007-05-20 15:09:04 +10001251 char *cp, buf[4*4096];
Damien Millerfef95ad2006-07-10 20:46:55 +10001252
Damien Millerfef95ad2006-07-10 20:46:55 +10001253 extern char *optarg;
1254 extern char *__progname;
Damien Miller7b28dc52000-09-05 13:34:53 +11001255
Damien Millerfef95ad2006-07-10 20:46:55 +10001256 __progname = ssh_get_progname(argv[0]);
1257 log_init(__progname, log_level, log_facility, log_stderr);
Ben Lindstromc7f4ccd2001-03-15 00:09:15 +00001258
Damien Millerfef95ad2006-07-10 20:46:55 +10001259 while (!skipargs && (ch = getopt(argc, argv, "C:f:l:che")) != -1) {
1260 switch (ch) {
1261 case 'c':
1262 /*
1263 * Ignore all arguments if we are invoked as a
Damien Millerd7834352006-08-05 12:39:39 +10001264 * shell using "sftp-server -c command"
Damien Millerfef95ad2006-07-10 20:46:55 +10001265 */
1266 skipargs = 1;
1267 break;
1268 case 'e':
1269 log_stderr = 1;
1270 break;
1271 case 'l':
1272 log_level = log_level_number(optarg);
1273 if (log_level == SYSLOG_LEVEL_NOT_SET)
1274 error("Invalid log level \"%s\"", optarg);
1275 break;
1276 case 'f':
1277 log_facility = log_facility_number(optarg);
Damien Miller35e18db2007-09-17 16:11:33 +10001278 if (log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Millerfef95ad2006-07-10 20:46:55 +10001279 error("Invalid log facility \"%s\"", optarg);
1280 break;
1281 case 'h':
1282 default:
Damien Millerdfc24252008-02-10 22:29:40 +11001283 sftp_server_usage();
Damien Millerfef95ad2006-07-10 20:46:55 +10001284 }
1285 }
1286
1287 log_init(__progname, log_level, log_facility, log_stderr);
1288
1289 if ((cp = getenv("SSH_CONNECTION")) != NULL) {
1290 client_addr = xstrdup(cp);
Damien Millerdfc24252008-02-10 22:29:40 +11001291 if ((cp = strchr(client_addr, ' ')) == NULL) {
1292 error("Malformed SSH_CONNECTION variable: \"%s\"",
Damien Millerfef95ad2006-07-10 20:46:55 +10001293 getenv("SSH_CONNECTION"));
Damien Millerdfc24252008-02-10 22:29:40 +11001294 sftp_server_cleanup_exit(255);
1295 }
Damien Millerfef95ad2006-07-10 20:46:55 +10001296 *cp = '\0';
1297 } else
1298 client_addr = xstrdup("UNKNOWN");
1299
Damien Millerd8cb1f12008-02-10 22:40:12 +11001300 pw = pwcopy(user_pw);
Damien Millerfef95ad2006-07-10 20:46:55 +10001301
1302 logit("session opened for local user %s from [%s]",
1303 pw->pw_name, client_addr);
1304
Damien Miller7b28dc52000-09-05 13:34:53 +11001305 in = dup(STDIN_FILENO);
1306 out = dup(STDOUT_FILENO);
1307
Damien Miller402b3312001-04-14 00:28:42 +10001308#ifdef HAVE_CYGWIN
1309 setmode(in, O_BINARY);
1310 setmode(out, O_BINARY);
1311#endif
1312
Damien Miller7b28dc52000-09-05 13:34:53 +11001313 max = 0;
1314 if (in > max)
1315 max = in;
1316 if (out > max)
1317 max = out;
1318
1319 buffer_init(&iqueue);
1320 buffer_init(&oqueue);
1321
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001322 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1323 rset = (fd_set *)xmalloc(set_size);
1324 wset = (fd_set *)xmalloc(set_size);
Damien Miller7b28dc52000-09-05 13:34:53 +11001325
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001326 for (;;) {
1327 memset(rset, 0, set_size);
1328 memset(wset, 0, set_size);
1329
Darren Tuckere9405982007-05-20 15:09:04 +10001330 /*
1331 * Ensure that we can read a full buffer and handle
1332 * the worst-case length packet it can generate,
1333 * otherwise apply backpressure by stopping reads.
1334 */
1335 if (buffer_check_alloc(&iqueue, sizeof(buf)) &&
1336 buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1337 FD_SET(in, rset);
1338
Damien Miller7b28dc52000-09-05 13:34:53 +11001339 olen = buffer_len(&oqueue);
1340 if (olen > 0)
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001341 FD_SET(out, wset);
Damien Miller7b28dc52000-09-05 13:34:53 +11001342
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001343 if (select(max+1, rset, wset, NULL, NULL) < 0) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001344 if (errno == EINTR)
1345 continue;
Damien Millerfef95ad2006-07-10 20:46:55 +10001346 error("select: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001347 sftp_server_cleanup_exit(2);
Damien Miller7b28dc52000-09-05 13:34:53 +11001348 }
1349
1350 /* copy stdin to iqueue */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001351 if (FD_ISSET(in, rset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001352 len = read(in, buf, sizeof buf);
1353 if (len == 0) {
1354 debug("read eof");
Damien Millerdfc24252008-02-10 22:29:40 +11001355 sftp_server_cleanup_exit(0);
Damien Miller7b28dc52000-09-05 13:34:53 +11001356 } else if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001357 error("read: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001358 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001359 } else {
1360 buffer_append(&iqueue, buf, len);
1361 }
1362 }
1363 /* send oqueue to stdout */
Ben Lindstromcb80bdf2001-03-05 07:06:12 +00001364 if (FD_ISSET(out, wset)) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001365 len = write(out, buffer_ptr(&oqueue), olen);
1366 if (len < 0) {
Damien Millerfef95ad2006-07-10 20:46:55 +10001367 error("write: %s", strerror(errno));
Damien Millerdfc24252008-02-10 22:29:40 +11001368 sftp_server_cleanup_exit(1);
Damien Miller7b28dc52000-09-05 13:34:53 +11001369 } else {
1370 buffer_consume(&oqueue, len);
1371 }
1372 }
Darren Tuckere9405982007-05-20 15:09:04 +10001373
1374 /*
1375 * Process requests from client if we can fit the results
1376 * into the output buffer, otherwise stop processing input
1377 * and let the output queue drain.
1378 */
1379 if (buffer_check_alloc(&oqueue, SFTP_MAX_MSG_LENGTH))
1380 process();
Damien Miller7b28dc52000-09-05 13:34:53 +11001381 }
1382}