]> andersk Git - openssh.git/blame - sftp-server.c
- (dtucker) [progressmeter.c scp.c sftp-server.c] Use correct casts for
[openssh.git] / sftp-server.c
CommitLineData
b5e300c2 1/*
71c1910f 2 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
b5e300c2 3 *
71c1910f 4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
b5e300c2 7 *
71c1910f 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
b5e300c2 15 */
16#include "includes.h"
fd6168c1 17RCSID("$OpenBSD: sftp-server.c,v 1.49 2005/09/13 23:40:07 djm Exp $");
b5e300c2 18
b5e300c2 19#include "buffer.h"
20#include "bufaux.h"
21#include "getput.h"
42f11eb2 22#include "log.h"
b5e300c2 23#include "xmalloc.h"
fd6168c1 24#include "misc.h"
b5e300c2 25
f546c780 26#include "sftp.h"
61e96248 27#include "sftp-common.h"
b5e300c2 28
29/* helper */
f546c780 30#define get_int64() buffer_get_int64(&iqueue);
b5e300c2 31#define get_int() buffer_get_int(&iqueue);
32#define get_string(lenp) buffer_get_string(&iqueue, lenp);
f546c780 33#define TRACE debug
b5e300c2 34
260d427b 35extern char *__progname;
260d427b 36
b5e300c2 37/* input and output queue */
38Buffer iqueue;
39Buffer oqueue;
40
3a7fe5ba 41/* Version of client */
42int version;
43
d7ded285 44/* portable attributes, etc. */
b5e300c2 45
b5e300c2 46typedef struct Stat Stat;
47
61e96248 48struct Stat {
b5e300c2 49 char *name;
50 char *long_name;
51 Attrib attrib;
52};
53
396c147e 54static int
b5e300c2 55errno_to_portable(int unixerrno)
56{
57 int ret = 0;
dce9bac5 58
b5e300c2 59 switch (unixerrno) {
60 case 0:
f546c780 61 ret = SSH2_FX_OK;
b5e300c2 62 break;
63 case ENOENT:
64 case ENOTDIR:
65 case EBADF:
66 case ELOOP:
f546c780 67 ret = SSH2_FX_NO_SUCH_FILE;
b5e300c2 68 break;
69 case EPERM:
70 case EACCES:
71 case EFAULT:
f546c780 72 ret = SSH2_FX_PERMISSION_DENIED;
b5e300c2 73 break;
74 case ENAMETOOLONG:
75 case EINVAL:
f546c780 76 ret = SSH2_FX_BAD_MESSAGE;
b5e300c2 77 break;
78 default:
f546c780 79 ret = SSH2_FX_FAILURE;
b5e300c2 80 break;
81 }
82 return ret;
83}
84
396c147e 85static int
b5e300c2 86flags_from_portable(int pflags)
87{
88 int flags = 0;
dce9bac5 89
79ddf6db 90 if ((pflags & SSH2_FXF_READ) &&
91 (pflags & SSH2_FXF_WRITE)) {
b5e300c2 92 flags = O_RDWR;
f546c780 93 } else if (pflags & SSH2_FXF_READ) {
b5e300c2 94 flags = O_RDONLY;
f546c780 95 } else if (pflags & SSH2_FXF_WRITE) {
b5e300c2 96 flags = O_WRONLY;
97 }
f546c780 98 if (pflags & SSH2_FXF_CREAT)
b5e300c2 99 flags |= O_CREAT;
f546c780 100 if (pflags & SSH2_FXF_TRUNC)
b5e300c2 101 flags |= O_TRUNC;
f546c780 102 if (pflags & SSH2_FXF_EXCL)
b5e300c2 103 flags |= O_EXCL;
104 return flags;
105}
106
396c147e 107static Attrib *
b5e300c2 108get_attrib(void)
109{
110 return decode_attrib(&iqueue);
111}
112
113/* handle handles */
114
115typedef struct Handle Handle;
116struct Handle {
117 int use;
118 DIR *dirp;
119 int fd;
120 char *name;
121};
dce9bac5 122
b5e300c2 123enum {
124 HANDLE_UNUSED,
125 HANDLE_DIR,
126 HANDLE_FILE
127};
dce9bac5 128
b5e300c2 129Handle handles[100];
130
396c147e 131static void
b5e300c2 132handle_init(void)
133{
2ceb8101 134 u_int i;
dce9bac5 135
184eed6a 136 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
b5e300c2 137 handles[i].use = HANDLE_UNUSED;
138}
139
396c147e 140static int
b6c7b7b7 141handle_new(int use, const char *name, int fd, DIR *dirp)
b5e300c2 142{
2ceb8101 143 u_int i;
dce9bac5 144
184eed6a 145 for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
b5e300c2 146 if (handles[i].use == HANDLE_UNUSED) {
147 handles[i].use = use;
148 handles[i].dirp = dirp;
149 handles[i].fd = fd;
3e2f2431 150 handles[i].name = xstrdup(name);
b5e300c2 151 return i;
152 }
153 }
154 return -1;
155}
156
396c147e 157static int
b5e300c2 158handle_is_ok(int i, int type)
159{
2ceb8101 160 return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
f546c780 161 handles[i].use == type;
b5e300c2 162}
163
396c147e 164static int
b5e300c2 165handle_to_string(int handle, char **stringp, int *hlenp)
166{
b5e300c2 167 if (stringp == NULL || hlenp == NULL)
168 return -1;
b5c334cc 169 *stringp = xmalloc(sizeof(int32_t));
170 PUT_32BIT(*stringp, handle);
171 *hlenp = sizeof(int32_t);
b5e300c2 172 return 0;
173}
174
396c147e 175static int
b6c7b7b7 176handle_from_string(const char *handle, u_int hlen)
b5e300c2 177{
b5c334cc 178 int val;
dce9bac5 179
b5c334cc 180 if (hlen != sizeof(int32_t))
b5e300c2 181 return -1;
b5c334cc 182 val = GET_32BIT(handle);
b5e300c2 183 if (handle_is_ok(val, HANDLE_FILE) ||
184 handle_is_ok(val, HANDLE_DIR))
185 return val;
186 return -1;
187}
188
396c147e 189static char *
b5e300c2 190handle_to_name(int handle)
191{
192 if (handle_is_ok(handle, HANDLE_DIR)||
193 handle_is_ok(handle, HANDLE_FILE))
194 return handles[handle].name;
195 return NULL;
196}
197
396c147e 198static DIR *
b5e300c2 199handle_to_dir(int handle)
200{
201 if (handle_is_ok(handle, HANDLE_DIR))
202 return handles[handle].dirp;
203 return NULL;
204}
205
396c147e 206static int
b5e300c2 207handle_to_fd(int handle)
208{
2b87da3b 209 if (handle_is_ok(handle, HANDLE_FILE))
b5e300c2 210 return handles[handle].fd;
211 return -1;
212}
213
396c147e 214static int
b5e300c2 215handle_close(int handle)
216{
217 int ret = -1;
dce9bac5 218
b5e300c2 219 if (handle_is_ok(handle, HANDLE_FILE)) {
220 ret = close(handles[handle].fd);
221 handles[handle].use = HANDLE_UNUSED;
3e2f2431 222 xfree(handles[handle].name);
b5e300c2 223 } else if (handle_is_ok(handle, HANDLE_DIR)) {
224 ret = closedir(handles[handle].dirp);
225 handles[handle].use = HANDLE_UNUSED;
3e2f2431 226 xfree(handles[handle].name);
b5e300c2 227 } else {
228 errno = ENOENT;
229 }
230 return ret;
231}
232
396c147e 233static int
b5e300c2 234get_handle(void)
235{
236 char *handle;
f546c780 237 int val = -1;
bcbf86ec 238 u_int hlen;
dce9bac5 239
b5e300c2 240 handle = get_string(&hlen);
f546c780 241 if (hlen < 256)
242 val = handle_from_string(handle, hlen);
b5e300c2 243 xfree(handle);
244 return val;
245}
246
247/* send replies */
248
396c147e 249static void
b5e300c2 250send_msg(Buffer *m)
251{
252 int mlen = buffer_len(m);
dce9bac5 253
b5e300c2 254 buffer_put_int(&oqueue, mlen);
255 buffer_append(&oqueue, buffer_ptr(m), mlen);
256 buffer_consume(m, mlen);
257}
258
396c147e 259static void
ca75d7de 260send_status(u_int32_t id, u_int32_t status)
b5e300c2 261{
262 Buffer msg;
3a7fe5ba 263 const char *status_messages[] = {
264 "Success", /* SSH_FX_OK */
265 "End of file", /* SSH_FX_EOF */
266 "No such file", /* SSH_FX_NO_SUCH_FILE */
267 "Permission denied", /* SSH_FX_PERMISSION_DENIED */
268 "Failure", /* SSH_FX_FAILURE */
269 "Bad message", /* SSH_FX_BAD_MESSAGE */
270 "No connection", /* SSH_FX_NO_CONNECTION */
271 "Connection lost", /* SSH_FX_CONNECTION_LOST */
272 "Operation unsupported", /* SSH_FX_OP_UNSUPPORTED */
273 "Unknown error" /* Others */
274 };
dce9bac5 275
ca75d7de 276 TRACE("sent status id %u error %u", id, status);
b5e300c2 277 buffer_init(&msg);
f546c780 278 buffer_put_char(&msg, SSH2_FXP_STATUS);
b5e300c2 279 buffer_put_int(&msg, id);
ca75d7de 280 buffer_put_int(&msg, status);
3a7fe5ba 281 if (version >= 3) {
cd332296 282 buffer_put_cstring(&msg,
ca75d7de 283 status_messages[MIN(status,SSH2_FX_MAX)]);
3a7fe5ba 284 buffer_put_cstring(&msg, "");
285 }
b5e300c2 286 send_msg(&msg);
287 buffer_free(&msg);
288}
396c147e 289static void
b6c7b7b7 290send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
b5e300c2 291{
292 Buffer msg;
dce9bac5 293
b5e300c2 294 buffer_init(&msg);
295 buffer_put_char(&msg, type);
296 buffer_put_int(&msg, id);
297 buffer_put_string(&msg, data, dlen);
298 send_msg(&msg);
299 buffer_free(&msg);
300}
301
396c147e 302static void
b6c7b7b7 303send_data(u_int32_t id, const char *data, int dlen)
b5e300c2 304{
9906a836 305 TRACE("sent data id %u len %d", id, dlen);
f546c780 306 send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
b5e300c2 307}
308
396c147e 309static void
b5e300c2 310send_handle(u_int32_t id, int handle)
311{
312 char *string;
313 int hlen;
dce9bac5 314
b5e300c2 315 handle_to_string(handle, &string, &hlen);
9906a836 316 TRACE("sent handle id %u handle %d", id, handle);
f546c780 317 send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
b5e300c2 318 xfree(string);
319}
320
396c147e 321static void
b6c7b7b7 322send_names(u_int32_t id, int count, const Stat *stats)
b5e300c2 323{
324 Buffer msg;
325 int i;
dce9bac5 326
b5e300c2 327 buffer_init(&msg);
f546c780 328 buffer_put_char(&msg, SSH2_FXP_NAME);
b5e300c2 329 buffer_put_int(&msg, id);
330 buffer_put_int(&msg, count);
9906a836 331 TRACE("sent names id %u count %d", id, count);
b5e300c2 332 for (i = 0; i < count; i++) {
333 buffer_put_cstring(&msg, stats[i].name);
334 buffer_put_cstring(&msg, stats[i].long_name);
335 encode_attrib(&msg, &stats[i].attrib);
336 }
337 send_msg(&msg);
338 buffer_free(&msg);
339}
340
396c147e 341static void
b6c7b7b7 342send_attrib(u_int32_t id, const Attrib *a)
b5e300c2 343{
344 Buffer msg;
dce9bac5 345
9906a836 346 TRACE("sent attrib id %u have 0x%x", id, a->flags);
b5e300c2 347 buffer_init(&msg);
f546c780 348 buffer_put_char(&msg, SSH2_FXP_ATTRS);
b5e300c2 349 buffer_put_int(&msg, id);
350 encode_attrib(&msg, a);
351 send_msg(&msg);
352 buffer_free(&msg);
353}
354
355/* parse incoming */
356
396c147e 357static void
b5e300c2 358process_init(void)
359{
360 Buffer msg;
b5e300c2 361
802d93bb 362 version = get_int();
b5e300c2 363 TRACE("client version %d", version);
364 buffer_init(&msg);
f546c780 365 buffer_put_char(&msg, SSH2_FXP_VERSION);
366 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
b5e300c2 367 send_msg(&msg);
368 buffer_free(&msg);
369}
370
396c147e 371static void
b5e300c2 372process_open(void)
373{
374 u_int32_t id, pflags;
375 Attrib *a;
376 char *name;
f546c780 377 int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
b5e300c2 378
379 id = get_int();
380 name = get_string(NULL);
f546c780 381 pflags = get_int(); /* portable flags */
b5e300c2 382 a = get_attrib();
383 flags = flags_from_portable(pflags);
f546c780 384 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
9906a836 385 TRACE("open id %u name %s flags %d mode 0%o", id, name, pflags, mode);
b5e300c2 386 fd = open(name, flags, mode);
387 if (fd < 0) {
388 status = errno_to_portable(errno);
389 } else {
3e2f2431 390 handle = handle_new(HANDLE_FILE, name, fd, NULL);
b5e300c2 391 if (handle < 0) {
392 close(fd);
393 } else {
394 send_handle(id, handle);
f546c780 395 status = SSH2_FX_OK;
b5e300c2 396 }
397 }
f546c780 398 if (status != SSH2_FX_OK)
b5e300c2 399 send_status(id, status);
400 xfree(name);
401}
402
396c147e 403static void
b5e300c2 404process_close(void)
405{
406 u_int32_t id;
f546c780 407 int handle, ret, status = SSH2_FX_FAILURE;
b5e300c2 408
409 id = get_int();
410 handle = get_handle();
9906a836 411 TRACE("close id %u handle %d", id, handle);
b5e300c2 412 ret = handle_close(handle);
f546c780 413 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 414 send_status(id, status);
415}
416
396c147e 417static void
b5e300c2 418process_read(void)
419{
420 char buf[64*1024];
f546c780 421 u_int32_t id, len;
422 int handle, fd, ret, status = SSH2_FX_FAILURE;
b5e300c2 423 u_int64_t off;
424
425 id = get_int();
426 handle = get_handle();
f546c780 427 off = get_int64();
b5e300c2 428 len = get_int();
429
9906a836 430 TRACE("read id %u handle %d off %llu len %d", id, handle,
419094c6 431 (unsigned long long)off, len);
b5e300c2 432 if (len > sizeof buf) {
433 len = sizeof buf;
bbe88b6d 434 logit("read change len %d", len);
b5e300c2 435 }
436 fd = handle_to_fd(handle);
437 if (fd >= 0) {
438 if (lseek(fd, off, SEEK_SET) < 0) {
439 error("process_read: seek failed");
440 status = errno_to_portable(errno);
441 } else {
442 ret = read(fd, buf, len);
443 if (ret < 0) {
444 status = errno_to_portable(errno);
445 } else if (ret == 0) {
f546c780 446 status = SSH2_FX_EOF;
b5e300c2 447 } else {
448 send_data(id, buf, ret);
f546c780 449 status = SSH2_FX_OK;
b5e300c2 450 }
451 }
452 }
f546c780 453 if (status != SSH2_FX_OK)
b5e300c2 454 send_status(id, status);
455}
456
396c147e 457static void
b5e300c2 458process_write(void)
459{
f546c780 460 u_int32_t id;
b5e300c2 461 u_int64_t off;
bcbf86ec 462 u_int len;
f546c780 463 int handle, fd, ret, status = SSH2_FX_FAILURE;
b5e300c2 464 char *data;
465
466 id = get_int();
467 handle = get_handle();
f546c780 468 off = get_int64();
b5e300c2 469 data = get_string(&len);
470
9906a836 471 TRACE("write id %u handle %d off %llu len %d", id, handle,
419094c6 472 (unsigned long long)off, len);
b5e300c2 473 fd = handle_to_fd(handle);
474 if (fd >= 0) {
475 if (lseek(fd, off, SEEK_SET) < 0) {
476 status = errno_to_portable(errno);
477 error("process_write: seek failed");
478 } else {
479/* XXX ATOMICIO ? */
480 ret = write(fd, data, len);
2ceb8101 481 if (ret < 0) {
b5e300c2 482 error("process_write: write failed");
483 status = errno_to_portable(errno);
2ceb8101 484 } else if ((size_t)ret == len) {
f546c780 485 status = SSH2_FX_OK;
b5e300c2 486 } else {
bbe88b6d 487 logit("nothing at all written");
b5e300c2 488 }
489 }
490 }
491 send_status(id, status);
492 xfree(data);
493}
494
396c147e 495static void
b5e300c2 496process_do_stat(int do_lstat)
497{
b5c334cc 498 Attrib a;
b5e300c2 499 struct stat st;
500 u_int32_t id;
501 char *name;
f546c780 502 int ret, status = SSH2_FX_FAILURE;
b5e300c2 503
504 id = get_int();
505 name = get_string(NULL);
9906a836 506 TRACE("%sstat id %u name %s", do_lstat ? "l" : "", id, name);
b5e300c2 507 ret = do_lstat ? lstat(name, &st) : stat(name, &st);
508 if (ret < 0) {
509 status = errno_to_portable(errno);
510 } else {
b5c334cc 511 stat_to_attrib(&st, &a);
512 send_attrib(id, &a);
f546c780 513 status = SSH2_FX_OK;
b5e300c2 514 }
f546c780 515 if (status != SSH2_FX_OK)
b5e300c2 516 send_status(id, status);
517 xfree(name);
518}
519
396c147e 520static void
b5e300c2 521process_stat(void)
522{
523 process_do_stat(0);
524}
525
396c147e 526static void
b5e300c2 527process_lstat(void)
528{
529 process_do_stat(1);
530}
531
396c147e 532static void
b5e300c2 533process_fstat(void)
534{
b5c334cc 535 Attrib a;
b5e300c2 536 struct stat st;
537 u_int32_t id;
f546c780 538 int fd, ret, handle, status = SSH2_FX_FAILURE;
b5e300c2 539
540 id = get_int();
541 handle = get_handle();
9906a836 542 TRACE("fstat id %u handle %d", id, handle);
b5e300c2 543 fd = handle_to_fd(handle);
544 if (fd >= 0) {
545 ret = fstat(fd, &st);
546 if (ret < 0) {
547 status = errno_to_portable(errno);
548 } else {
b5c334cc 549 stat_to_attrib(&st, &a);
550 send_attrib(id, &a);
f546c780 551 status = SSH2_FX_OK;
b5e300c2 552 }
553 }
f546c780 554 if (status != SSH2_FX_OK)
b5e300c2 555 send_status(id, status);
556}
557
396c147e 558static struct timeval *
b6c7b7b7 559attrib_to_tv(const Attrib *a)
b5e300c2 560{
561 static struct timeval tv[2];
dce9bac5 562
b5e300c2 563 tv[0].tv_sec = a->atime;
564 tv[0].tv_usec = 0;
565 tv[1].tv_sec = a->mtime;
566 tv[1].tv_usec = 0;
567 return tv;
568}
569
396c147e 570static void
b5e300c2 571process_setstat(void)
572{
573 Attrib *a;
574 u_int32_t id;
575 char *name;
9906a836 576 int status = SSH2_FX_OK, ret;
b5e300c2 577
578 id = get_int();
579 name = get_string(NULL);
580 a = get_attrib();
9906a836 581 TRACE("setstat id %u name %s", id, name);
cb476289 582 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
583 ret = truncate(name, a->size);
584 if (ret == -1)
585 status = errno_to_portable(errno);
586 }
f546c780 587 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
b5e300c2 588 ret = chmod(name, a->perm & 0777);
589 if (ret == -1)
590 status = errno_to_portable(errno);
591 }
f546c780 592 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
b5e300c2 593 ret = utimes(name, attrib_to_tv(a));
594 if (ret == -1)
595 status = errno_to_portable(errno);
596 }
408ba72f 597 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
598 ret = chown(name, a->uid, a->gid);
599 if (ret == -1)
600 status = errno_to_portable(errno);
601 }
b5e300c2 602 send_status(id, status);
603 xfree(name);
604}
605
396c147e 606static void
b5e300c2 607process_fsetstat(void)
608{
609 Attrib *a;
610 u_int32_t id;
611 int handle, fd, ret;
f546c780 612 int status = SSH2_FX_OK;
bcbf86ec 613 char *name;
16e538d4 614
b5e300c2 615 id = get_int();
616 handle = get_handle();
617 a = get_attrib();
9906a836 618 TRACE("fsetstat id %u handle %d", id, handle);
b5e300c2 619 fd = handle_to_fd(handle);
620 name = handle_to_name(handle);
16e538d4 621 if (fd < 0 || name == NULL) {
f546c780 622 status = SSH2_FX_FAILURE;
b5e300c2 623 } else {
cb476289 624 if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
625 ret = ftruncate(fd, a->size);
626 if (ret == -1)
627 status = errno_to_portable(errno);
628 }
f546c780 629 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
2fd3c144 630#ifdef HAVE_FCHMOD
b5e300c2 631 ret = fchmod(fd, a->perm & 0777);
2fd3c144 632#else
c33f0b36 633 ret = chmod(name, a->perm & 0777);
2fd3c144 634#endif
b5e300c2 635 if (ret == -1)
636 status = errno_to_portable(errno);
637 }
f546c780 638 if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
b5e300c2 639#ifdef HAVE_FUTIMES
640 ret = futimes(fd, attrib_to_tv(a));
641#else
642 ret = utimes(name, attrib_to_tv(a));
643#endif
644 if (ret == -1)
645 status = errno_to_portable(errno);
646 }
408ba72f 647 if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
01f13020 648#ifdef HAVE_FCHOWN
408ba72f 649 ret = fchown(fd, a->uid, a->gid);
01f13020 650#else
651 ret = chown(name, a->uid, a->gid);
652#endif
408ba72f 653 if (ret == -1)
654 status = errno_to_portable(errno);
655 }
b5e300c2 656 }
657 send_status(id, status);
658}
659
396c147e 660static void
b5e300c2 661process_opendir(void)
662{
663 DIR *dirp = NULL;
664 char *path;
f546c780 665 int handle, status = SSH2_FX_FAILURE;
b5e300c2 666 u_int32_t id;
667
668 id = get_int();
669 path = get_string(NULL);
9906a836 670 TRACE("opendir id %u path %s", id, path);
2b87da3b 671 dirp = opendir(path);
b5e300c2 672 if (dirp == NULL) {
673 status = errno_to_portable(errno);
674 } else {
3e2f2431 675 handle = handle_new(HANDLE_DIR, path, 0, dirp);
b5e300c2 676 if (handle < 0) {
677 closedir(dirp);
678 } else {
679 send_handle(id, handle);
f546c780 680 status = SSH2_FX_OK;
b5e300c2 681 }
2b87da3b 682
b5e300c2 683 }
f546c780 684 if (status != SSH2_FX_OK)
b5e300c2 685 send_status(id, status);
686 xfree(path);
687}
688
396c147e 689static void
b5e300c2 690process_readdir(void)
691{
692 DIR *dirp;
693 struct dirent *dp;
694 char *path;
695 int handle;
696 u_int32_t id;
697
698 id = get_int();
699 handle = get_handle();
9906a836 700 TRACE("readdir id %u handle %d", id, handle);
b5e300c2 701 dirp = handle_to_dir(handle);
702 path = handle_to_name(handle);
703 if (dirp == NULL || path == NULL) {
f546c780 704 send_status(id, SSH2_FX_FAILURE);
b5e300c2 705 } else {
b5e300c2 706 struct stat st;
707 char pathname[1024];
708 Stat *stats;
709 int nstats = 10, count = 0, i;
9906a836 710
b5e300c2 711 stats = xmalloc(nstats * sizeof(Stat));
712 while ((dp = readdir(dirp)) != NULL) {
713 if (count >= nstats) {
714 nstats *= 2;
715 stats = xrealloc(stats, nstats * sizeof(Stat));
716 }
717/* XXX OVERFLOW ? */
88690211 718 snprintf(pathname, sizeof pathname, "%s%s%s", path,
719 strcmp(path, "/") ? "/" : "", dp->d_name);
b5e300c2 720 if (lstat(pathname, &st) < 0)
721 continue;
b5c334cc 722 stat_to_attrib(&st, &(stats[count].attrib));
b5e300c2 723 stats[count].name = xstrdup(dp->d_name);
00b3ad3e 724 stats[count].long_name = ls_file(dp->d_name, &st, 0);
b5e300c2 725 count++;
726 /* send up to 100 entries in one message */
b5c334cc 727 /* XXX check packet size instead */
b5e300c2 728 if (count == 100)
729 break;
730 }
f546c780 731 if (count > 0) {
732 send_names(id, count, stats);
184eed6a 733 for (i = 0; i < count; i++) {
f546c780 734 xfree(stats[i].name);
735 xfree(stats[i].long_name);
736 }
737 } else {
738 send_status(id, SSH2_FX_EOF);
b5e300c2 739 }
740 xfree(stats);
741 }
742}
743
396c147e 744static void
b5e300c2 745process_remove(void)
746{
747 char *name;
748 u_int32_t id;
f546c780 749 int status = SSH2_FX_FAILURE;
b5e300c2 750 int ret;
751
752 id = get_int();
753 name = get_string(NULL);
9906a836 754 TRACE("remove id %u name %s", id, name);
67b0facb 755 ret = unlink(name);
f546c780 756 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 757 send_status(id, status);
758 xfree(name);
759}
760
396c147e 761static void
b5e300c2 762process_mkdir(void)
763{
764 Attrib *a;
765 u_int32_t id;
766 char *name;
f546c780 767 int ret, mode, status = SSH2_FX_FAILURE;
b5e300c2 768
769 id = get_int();
770 name = get_string(NULL);
771 a = get_attrib();
f546c780 772 mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
773 a->perm & 0777 : 0777;
9906a836 774 TRACE("mkdir id %u name %s mode 0%o", id, name, mode);
b5e300c2 775 ret = mkdir(name, mode);
f546c780 776 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 777 send_status(id, status);
778 xfree(name);
779}
780
396c147e 781static void
b5e300c2 782process_rmdir(void)
783{
784 u_int32_t id;
785 char *name;
786 int ret, status;
787
788 id = get_int();
789 name = get_string(NULL);
9906a836 790 TRACE("rmdir id %u name %s", id, name);
b5e300c2 791 ret = rmdir(name);
f546c780 792 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
b5e300c2 793 send_status(id, status);
794 xfree(name);
795}
796
396c147e 797static void
b5e300c2 798process_realpath(void)
799{
800 char resolvedname[MAXPATHLEN];
801 u_int32_t id;
802 char *path;
803
804 id = get_int();
805 path = get_string(NULL);
6b523bae 806 if (path[0] == '\0') {
807 xfree(path);
808 path = xstrdup(".");
809 }
9906a836 810 TRACE("realpath id %u path %s", id, path);
b5e300c2 811 if (realpath(path, resolvedname) == NULL) {
812 send_status(id, errno_to_portable(errno));
813 } else {
814 Stat s;
815 attrib_clear(&s.attrib);
816 s.name = s.long_name = resolvedname;
817 send_names(id, 1, &s);
818 }
819 xfree(path);
820}
821
396c147e 822static void
b5e300c2 823process_rename(void)
824{
825 u_int32_t id;
826 char *oldpath, *newpath;
f2f28f1f 827 int status;
fd77a40f 828 struct stat sb;
b5e300c2 829
830 id = get_int();
831 oldpath = get_string(NULL);
832 newpath = get_string(NULL);
9906a836 833 TRACE("rename id %u old %s new %s", id, oldpath, newpath);
fd77a40f 834 status = SSH2_FX_FAILURE;
835 if (lstat(oldpath, &sb) == -1)
f2f28f1f 836 status = errno_to_portable(errno);
fd77a40f 837 else if (S_ISREG(sb.st_mode)) {
838 /* Race-free rename of regular files */
dc5888bf 839 if (link(oldpath, newpath) == -1) {
cd698186 840 if (errno == EOPNOTSUPP
841#ifdef LINK_OPNOTSUPP_ERRNO
842 || errno == LINK_OPNOTSUPP_ERRNO
843#endif
844 ) {
dc5888bf 845 struct stat st;
846
847 /*
848 * fs doesn't support links, so fall back to
849 * stat+rename. This is racy.
850 */
851 if (stat(newpath, &st) == -1) {
852 if (rename(oldpath, newpath) == -1)
853 status =
854 errno_to_portable(errno);
855 else
856 status = SSH2_FX_OK;
857 }
858 } else {
859 status = errno_to_portable(errno);
860 }
861 } else if (unlink(oldpath) == -1) {
fd77a40f 862 status = errno_to_portable(errno);
863 /* clean spare link */
864 unlink(newpath);
865 } else
866 status = SSH2_FX_OK;
867 } else if (stat(newpath, &sb) == -1) {
868 if (rename(oldpath, newpath) == -1)
869 status = errno_to_portable(errno);
870 else
871 status = SSH2_FX_OK;
872 }
b5e300c2 873 send_status(id, status);
874 xfree(oldpath);
875 xfree(newpath);
876}
877
396c147e 878static void
3a7fe5ba 879process_readlink(void)
880{
881 u_int32_t id;
362df52e 882 int len;
ca75d7de 883 char buf[MAXPATHLEN];
3a7fe5ba 884 char *path;
885
886 id = get_int();
887 path = get_string(NULL);
9906a836 888 TRACE("readlink id %u path %s", id, path);
ca75d7de 889 if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
3a7fe5ba 890 send_status(id, errno_to_portable(errno));
891 else {
892 Stat s;
184eed6a 893
ca75d7de 894 buf[len] = '\0';
3a7fe5ba 895 attrib_clear(&s.attrib);
ca75d7de 896 s.name = s.long_name = buf;
3a7fe5ba 897 send_names(id, 1, &s);
898 }
899 xfree(path);
900}
901
396c147e 902static void
3a7fe5ba 903process_symlink(void)
904{
905 u_int32_t id;
3a7fe5ba 906 char *oldpath, *newpath;
f2f28f1f 907 int ret, status;
3a7fe5ba 908
909 id = get_int();
910 oldpath = get_string(NULL);
911 newpath = get_string(NULL);
9906a836 912 TRACE("symlink id %u old %s new %s", id, oldpath, newpath);
f2f28f1f 913 /* this will fail if 'newpath' exists */
914 ret = symlink(oldpath, newpath);
915 status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
3a7fe5ba 916 send_status(id, status);
917 xfree(oldpath);
918 xfree(newpath);
919}
920
396c147e 921static void
f546c780 922process_extended(void)
923{
924 u_int32_t id;
925 char *request;
926
927 id = get_int();
928 request = get_string(NULL);
929 send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
930 xfree(request);
931}
b5e300c2 932
933/* stolen from ssh-agent */
934
396c147e 935static void
b5e300c2 936process(void)
937{
1e3b8b07 938 u_int msg_len;
9b4ac641 939 u_int buf_len;
940 u_int consumed;
1e3b8b07 941 u_int type;
942 u_char *cp;
b5e300c2 943
9b4ac641 944 buf_len = buffer_len(&iqueue);
945 if (buf_len < 5)
b5e300c2 946 return; /* Incomplete message. */
20905a8e 947 cp = buffer_ptr(&iqueue);
b5e300c2 948 msg_len = GET_32BIT(cp);
949 if (msg_len > 256 * 1024) {
950 error("bad message ");
951 exit(11);
952 }
9b4ac641 953 if (buf_len < msg_len + 4)
b5e300c2 954 return;
955 buffer_consume(&iqueue, 4);
9b4ac641 956 buf_len -= 4;
b5e300c2 957 type = buffer_get_char(&iqueue);
958 switch (type) {
f546c780 959 case SSH2_FXP_INIT:
b5e300c2 960 process_init();
961 break;
f546c780 962 case SSH2_FXP_OPEN:
b5e300c2 963 process_open();
964 break;
f546c780 965 case SSH2_FXP_CLOSE:
b5e300c2 966 process_close();
967 break;
f546c780 968 case SSH2_FXP_READ:
b5e300c2 969 process_read();
970 break;
f546c780 971 case SSH2_FXP_WRITE:
b5e300c2 972 process_write();
973 break;
f546c780 974 case SSH2_FXP_LSTAT:
b5e300c2 975 process_lstat();
976 break;
f546c780 977 case SSH2_FXP_FSTAT:
b5e300c2 978 process_fstat();
979 break;
f546c780 980 case SSH2_FXP_SETSTAT:
b5e300c2 981 process_setstat();
982 break;
f546c780 983 case SSH2_FXP_FSETSTAT:
b5e300c2 984 process_fsetstat();
985 break;
f546c780 986 case SSH2_FXP_OPENDIR:
b5e300c2 987 process_opendir();
988 break;
f546c780 989 case SSH2_FXP_READDIR:
b5e300c2 990 process_readdir();
991 break;
f546c780 992 case SSH2_FXP_REMOVE:
b5e300c2 993 process_remove();
994 break;
f546c780 995 case SSH2_FXP_MKDIR:
b5e300c2 996 process_mkdir();
997 break;
f546c780 998 case SSH2_FXP_RMDIR:
b5e300c2 999 process_rmdir();
1000 break;
f546c780 1001 case SSH2_FXP_REALPATH:
b5e300c2 1002 process_realpath();
1003 break;
f546c780 1004 case SSH2_FXP_STAT:
b5e300c2 1005 process_stat();
1006 break;
f546c780 1007 case SSH2_FXP_RENAME:
b5e300c2 1008 process_rename();
1009 break;
3a7fe5ba 1010 case SSH2_FXP_READLINK:
1011 process_readlink();
1012 break;
1013 case SSH2_FXP_SYMLINK:
1014 process_symlink();
1015 break;
f546c780 1016 case SSH2_FXP_EXTENDED:
1017 process_extended();
1018 break;
b5e300c2 1019 default:
1020 error("Unknown message %d", type);
1021 break;
1022 }
9b4ac641 1023 /* discard the remaining bytes from the current packet */
1024 if (buf_len < buffer_len(&iqueue))
1025 fatal("iqueue grows");
1026 consumed = buf_len - buffer_len(&iqueue);
1027 if (msg_len < consumed)
1028 fatal("msg_len %d < consumed %d", msg_len, consumed);
1029 if (msg_len > consumed)
1030 buffer_consume(&iqueue, msg_len - consumed);
b5e300c2 1031}
1032
1033int
1034main(int ac, char **av)
1035{
c8d75031 1036 fd_set *rset, *wset;
b5e300c2 1037 int in, out, max;
c8d75031 1038 ssize_t len, olen, set_size;
b5e300c2 1039
fd6168c1 1040 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1041 sanitise_stdfd();
1042
61b3a2bc 1043 /* XXX should use getopt */
1044
fda04d7d 1045 __progname = ssh_get_progname(av[0]);
b5e300c2 1046 handle_init();
1047
b5c334cc 1048#ifdef DEBUG_SFTP_SERVER
2b87da3b 1049 log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
b5c334cc 1050#endif
f546c780 1051
b5e300c2 1052 in = dup(STDIN_FILENO);
1053 out = dup(STDOUT_FILENO);
1054
fe56c12b 1055#ifdef HAVE_CYGWIN
1056 setmode(in, O_BINARY);
1057 setmode(out, O_BINARY);
1058#endif
1059
b5e300c2 1060 max = 0;
1061 if (in > max)
1062 max = in;
1063 if (out > max)
1064 max = out;
1065
1066 buffer_init(&iqueue);
1067 buffer_init(&oqueue);
1068
c8d75031 1069 set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
1070 rset = (fd_set *)xmalloc(set_size);
1071 wset = (fd_set *)xmalloc(set_size);
1072
b5e300c2 1073 for (;;) {
c8d75031 1074 memset(rset, 0, set_size);
1075 memset(wset, 0, set_size);
b5e300c2 1076
c8d75031 1077 FD_SET(in, rset);
b5e300c2 1078 olen = buffer_len(&oqueue);
1079 if (olen > 0)
c8d75031 1080 FD_SET(out, wset);
b5e300c2 1081
c8d75031 1082 if (select(max+1, rset, wset, NULL, NULL) < 0) {
b5e300c2 1083 if (errno == EINTR)
1084 continue;
1085 exit(2);
1086 }
1087
1088 /* copy stdin to iqueue */
c8d75031 1089 if (FD_ISSET(in, rset)) {
b5e300c2 1090 char buf[4*4096];
1091 len = read(in, buf, sizeof buf);
1092 if (len == 0) {
1093 debug("read eof");
1094 exit(0);
1095 } else if (len < 0) {
1096 error("read error");
1097 exit(1);
1098 } else {
1099 buffer_append(&iqueue, buf, len);
1100 }
1101 }
1102 /* send oqueue to stdout */
c8d75031 1103 if (FD_ISSET(out, wset)) {
b5e300c2 1104 len = write(out, buffer_ptr(&oqueue), olen);
1105 if (len < 0) {
1106 error("write error");
1107 exit(1);
1108 } else {
1109 buffer_consume(&oqueue, len);
1110 }
1111 }
1112 /* process requests from client */
1113 process();
1114 }
1115}
This page took 0.382142 seconds and 5 git commands to generate.