sftp-server.c 33.2 KB
Newer Older
1
/* $OpenBSD: sftp-server.c,v 1.97 2013/05/17 00:13:14 djm Exp $ */
Damien Miller's avatar
Damien Miller committed
2
/*
3
 * Copyright (c) 2000-2004 Markus Friedl.  All rights reserved.
Damien Miller's avatar
Damien Miller committed
4
 *
5 6 7
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
Damien Miller's avatar
Damien Miller committed
8
 *
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller's avatar
Damien Miller committed
16
 */
17

Damien Miller's avatar
Damien Miller committed
18
#include "includes.h"
19 20

#include <sys/types.h>
21
#include <sys/param.h>
22
#include <sys/stat.h>
23 24 25
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
26
#ifdef HAVE_SYS_MOUNT_H
27
#include <sys/mount.h>
28 29
#endif
#ifdef HAVE_SYS_STATVFS_H
30
#include <sys/statvfs.h>
31
#endif
32 33

#include <dirent.h>
34
#include <errno.h>
35
#include <fcntl.h>
36
#include <pwd.h>
37
#include <stdlib.h>
38
#include <stdio.h>
39
#include <string.h>
40
#include <pwd.h>
41
#include <time.h>
42
#include <unistd.h>
43
#include <stdarg.h>
Damien Miller's avatar
Damien Miller committed
44

45
#include "xmalloc.h"
Damien Miller's avatar
Damien Miller committed
46
#include "buffer.h"
47
#include "log.h"
48
#include "misc.h"
49
#include "uidswap.h"
Damien Miller's avatar
Damien Miller committed
50

Ben Lindstrom's avatar
Ben Lindstrom committed
51
#include "sftp.h"
52
#include "sftp-common.h"
Damien Miller's avatar
Damien Miller committed
53 54

/* helper */
Ben Lindstrom's avatar
Ben Lindstrom committed
55
#define get_int64()			buffer_get_int64(&iqueue);
Damien Miller's avatar
Damien Miller committed
56 57 58
#define get_int()			buffer_get_int(&iqueue);
#define get_string(lenp)		buffer_get_string(&iqueue, lenp);

59 60 61 62 63 64
/* Our verbosity */
LogLevel log_level = SYSLOG_LEVEL_ERROR;

/* Our client */
struct passwd *pw = NULL;
char *client_addr = NULL;
65

Damien Miller's avatar
Damien Miller committed
66 67 68 69
/* input and output queue */
Buffer iqueue;
Buffer oqueue;

Damien Miller's avatar
Damien Miller committed
70
/* Version of client */
71
u_int version;
Damien Miller's avatar
Damien Miller committed
72

73 74 75
/* Disable writes */
int readonly;

76
/* portable attributes, etc. */
Damien Miller's avatar
Damien Miller committed
77 78 79

typedef struct Stat Stat;

80
struct Stat {
Damien Miller's avatar
Damien Miller committed
81 82 83 84 85
	char *name;
	char *long_name;
	Attrib attrib;
};

86
static int
Damien Miller's avatar
Damien Miller committed
87 88 89
errno_to_portable(int unixerrno)
{
	int ret = 0;
90

Damien Miller's avatar
Damien Miller committed
91 92
	switch (unixerrno) {
	case 0:
Ben Lindstrom's avatar
Ben Lindstrom committed
93
		ret = SSH2_FX_OK;
Damien Miller's avatar
Damien Miller committed
94 95 96 97 98
		break;
	case ENOENT:
	case ENOTDIR:
	case EBADF:
	case ELOOP:
Ben Lindstrom's avatar
Ben Lindstrom committed
99
		ret = SSH2_FX_NO_SUCH_FILE;
Damien Miller's avatar
Damien Miller committed
100 101 102 103
		break;
	case EPERM:
	case EACCES:
	case EFAULT:
Ben Lindstrom's avatar
Ben Lindstrom committed
104
		ret = SSH2_FX_PERMISSION_DENIED;
Damien Miller's avatar
Damien Miller committed
105 106 107
		break;
	case ENAMETOOLONG:
	case EINVAL:
Ben Lindstrom's avatar
Ben Lindstrom committed
108
		ret = SSH2_FX_BAD_MESSAGE;
Damien Miller's avatar
Damien Miller committed
109
		break;
110 111 112
	case ENOSYS:
		ret = SSH2_FX_OP_UNSUPPORTED;
		break;
Damien Miller's avatar
Damien Miller committed
113
	default:
Ben Lindstrom's avatar
Ben Lindstrom committed
114
		ret = SSH2_FX_FAILURE;
Damien Miller's avatar
Damien Miller committed
115 116 117 118 119
		break;
	}
	return ret;
}

120
static int
Damien Miller's avatar
Damien Miller committed
121 122 123
flags_from_portable(int pflags)
{
	int flags = 0;
124

125 126
	if ((pflags & SSH2_FXF_READ) &&
	    (pflags & SSH2_FXF_WRITE)) {
Damien Miller's avatar
Damien Miller committed
127
		flags = O_RDWR;
Ben Lindstrom's avatar
Ben Lindstrom committed
128
	} else if (pflags & SSH2_FXF_READ) {
Damien Miller's avatar
Damien Miller committed
129
		flags = O_RDONLY;
Ben Lindstrom's avatar
Ben Lindstrom committed
130
	} else if (pflags & SSH2_FXF_WRITE) {
Damien Miller's avatar
Damien Miller committed
131 132
		flags = O_WRONLY;
	}
Ben Lindstrom's avatar
Ben Lindstrom committed
133
	if (pflags & SSH2_FXF_CREAT)
Damien Miller's avatar
Damien Miller committed
134
		flags |= O_CREAT;
Ben Lindstrom's avatar
Ben Lindstrom committed
135
	if (pflags & SSH2_FXF_TRUNC)
Damien Miller's avatar
Damien Miller committed
136
		flags |= O_TRUNC;
Ben Lindstrom's avatar
Ben Lindstrom committed
137
	if (pflags & SSH2_FXF_EXCL)
Damien Miller's avatar
Damien Miller committed
138 139 140 141
		flags |= O_EXCL;
	return flags;
}

142 143 144 145 146 147 148 149 150 151
static const char *
string_from_portable(int pflags)
{
	static char ret[128];

	*ret = '\0';

#define PAPPEND(str)	{				\
		if (*ret != '\0')			\
			strlcat(ret, ",", sizeof(ret));	\
152
		strlcat(ret, str, sizeof(ret));		\
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	}

	if (pflags & SSH2_FXF_READ)
		PAPPEND("READ")
	if (pflags & SSH2_FXF_WRITE)
		PAPPEND("WRITE")
	if (pflags & SSH2_FXF_CREAT)
		PAPPEND("CREATE")
	if (pflags & SSH2_FXF_TRUNC)
		PAPPEND("TRUNCATE")
	if (pflags & SSH2_FXF_EXCL)
		PAPPEND("EXCL")

	return ret;
}

169
static Attrib *
Damien Miller's avatar
Damien Miller committed
170 171 172 173 174 175 176 177 178 179 180 181 182
get_attrib(void)
{
	return decode_attrib(&iqueue);
}

/* handle handles */

typedef struct Handle Handle;
struct Handle {
	int use;
	DIR *dirp;
	int fd;
	char *name;
183
	u_int64_t bytes_read, bytes_write;
184
	int next_unused;
Damien Miller's avatar
Damien Miller committed
185
};
186

Damien Miller's avatar
Damien Miller committed
187 188 189 190 191
enum {
	HANDLE_UNUSED,
	HANDLE_DIR,
	HANDLE_FILE
};
192

193 194 195
Handle *handles = NULL;
u_int num_handles = 0;
int first_unused_handle = -1;
Damien Miller's avatar
Damien Miller committed
196

197
static void handle_unused(int i)
Damien Miller's avatar
Damien Miller committed
198
{
199 200 201
	handles[i].use = HANDLE_UNUSED;
	handles[i].next_unused = first_unused_handle;
	first_unused_handle = i;
Damien Miller's avatar
Damien Miller committed
202 203
}

204
static int
205
handle_new(int use, const char *name, int fd, DIR *dirp)
Damien Miller's avatar
Damien Miller committed
206
{
207
	int i;
208

209 210 211 212 213 214
	if (first_unused_handle == -1) {
		if (num_handles + 1 <= num_handles)
			return -1;
		num_handles++;
		handles = xrealloc(handles, num_handles, sizeof(Handle));
		handle_unused(num_handles - 1);
Damien Miller's avatar
Damien Miller committed
215
	}
216 217 218 219 220 221 222 223 224 225 226

	i = first_unused_handle;
	first_unused_handle = handles[i].next_unused;

	handles[i].use = use;
	handles[i].dirp = dirp;
	handles[i].fd = fd;
	handles[i].name = xstrdup(name);
	handles[i].bytes_read = handles[i].bytes_write = 0;

	return i;
Damien Miller's avatar
Damien Miller committed
227 228
}

229
static int
Damien Miller's avatar
Damien Miller committed
230 231
handle_is_ok(int i, int type)
{
232
	return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
Damien Miller's avatar
Damien Miller committed
233 234
}

235
static int
Damien Miller's avatar
Damien Miller committed
236 237 238 239
handle_to_string(int handle, char **stringp, int *hlenp)
{
	if (stringp == NULL || hlenp == NULL)
		return -1;
240
	*stringp = xmalloc(sizeof(int32_t));
241
	put_u32(*stringp, handle);
242
	*hlenp = sizeof(int32_t);
Damien Miller's avatar
Damien Miller committed
243 244 245
	return 0;
}

246
static int
247
handle_from_string(const char *handle, u_int hlen)
Damien Miller's avatar
Damien Miller committed
248
{
249
	int val;
250

251
	if (hlen != sizeof(int32_t))
Damien Miller's avatar
Damien Miller committed
252
		return -1;
253
	val = get_u32(handle);
Damien Miller's avatar
Damien Miller committed
254 255 256 257 258 259
	if (handle_is_ok(val, HANDLE_FILE) ||
	    handle_is_ok(val, HANDLE_DIR))
		return val;
	return -1;
}

260
static char *
Damien Miller's avatar
Damien Miller committed
261 262 263 264 265 266 267 268
handle_to_name(int handle)
{
	if (handle_is_ok(handle, HANDLE_DIR)||
	    handle_is_ok(handle, HANDLE_FILE))
		return handles[handle].name;
	return NULL;
}

269
static DIR *
Damien Miller's avatar
Damien Miller committed
270 271 272 273 274 275 276
handle_to_dir(int handle)
{
	if (handle_is_ok(handle, HANDLE_DIR))
		return handles[handle].dirp;
	return NULL;
}

277
static int
Damien Miller's avatar
Damien Miller committed
278 279
handle_to_fd(int handle)
{
280
	if (handle_is_ok(handle, HANDLE_FILE))
Damien Miller's avatar
Damien Miller committed
281 282 283 284
		return handles[handle].fd;
	return -1;
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
static void
handle_update_read(int handle, ssize_t bytes)
{
	if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
		handles[handle].bytes_read += bytes;
}

static void
handle_update_write(int handle, ssize_t bytes)
{
	if (handle_is_ok(handle, HANDLE_FILE) && bytes > 0)
		handles[handle].bytes_write += bytes;
}

static u_int64_t
handle_bytes_read(int handle)
{
	if (handle_is_ok(handle, HANDLE_FILE))
		return (handles[handle].bytes_read);
	return 0;
}

static u_int64_t
handle_bytes_write(int handle)
{
	if (handle_is_ok(handle, HANDLE_FILE))
		return (handles[handle].bytes_write);
	return 0;
}

315
static int
Damien Miller's avatar
Damien Miller committed
316 317 318
handle_close(int handle)
{
	int ret = -1;
319

Damien Miller's avatar
Damien Miller committed
320 321
	if (handle_is_ok(handle, HANDLE_FILE)) {
		ret = close(handles[handle].fd);
322
		free(handles[handle].name);
323
		handle_unused(handle);
Damien Miller's avatar
Damien Miller committed
324 325
	} else if (handle_is_ok(handle, HANDLE_DIR)) {
		ret = closedir(handles[handle].dirp);
326
		free(handles[handle].name);
327
		handle_unused(handle);
Damien Miller's avatar
Damien Miller committed
328 329 330 331 332 333
	} else {
		errno = ENOENT;
	}
	return ret;
}

334 335 336 337 338 339 340
static void
handle_log_close(int handle, char *emsg)
{
	if (handle_is_ok(handle, HANDLE_FILE)) {
		logit("%s%sclose \"%s\" bytes read %llu written %llu",
		    emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
		    handle_to_name(handle),
341 342
		    (unsigned long long)handle_bytes_read(handle),
		    (unsigned long long)handle_bytes_write(handle));
343 344 345 346 347 348 349 350 351 352 353 354
	} else {
		logit("%s%sclosedir \"%s\"",
		    emsg == NULL ? "" : emsg, emsg == NULL ? "" : " ",
		    handle_to_name(handle));
	}
}

static void
handle_log_exit(void)
{
	u_int i;

355
	for (i = 0; i < num_handles; i++)
356 357 358 359
		if (handles[i].use != HANDLE_UNUSED)
			handle_log_close(i, "forced");
}

360
static int
Damien Miller's avatar
Damien Miller committed
361 362 363
get_handle(void)
{
	char *handle;
Ben Lindstrom's avatar
Ben Lindstrom committed
364
	int val = -1;
365
	u_int hlen;
366

Damien Miller's avatar
Damien Miller committed
367
	handle = get_string(&hlen);
Ben Lindstrom's avatar
Ben Lindstrom committed
368 369
	if (hlen < 256)
		val = handle_from_string(handle, hlen);
370
	free(handle);
Damien Miller's avatar
Damien Miller committed
371 372 373 374 375
	return val;
}

/* send replies */

376
static void
Damien Miller's avatar
Damien Miller committed
377 378 379
send_msg(Buffer *m)
{
	int mlen = buffer_len(m);
380

Damien Miller's avatar
Damien Miller committed
381 382 383 384 385
	buffer_put_int(&oqueue, mlen);
	buffer_append(&oqueue, buffer_ptr(m), mlen);
	buffer_consume(m, mlen);
}

386 387
static const char *
status_to_message(u_int32_t status)
Damien Miller's avatar
Damien Miller committed
388
{
Damien Miller's avatar
Damien Miller committed
389 390 391 392 393 394 395 396 397 398 399 400
	const char *status_messages[] = {
		"Success",			/* SSH_FX_OK */
		"End of file",			/* SSH_FX_EOF */
		"No such file",			/* SSH_FX_NO_SUCH_FILE */
		"Permission denied",		/* SSH_FX_PERMISSION_DENIED */
		"Failure",			/* SSH_FX_FAILURE */
		"Bad message",			/* SSH_FX_BAD_MESSAGE */
		"No connection",		/* SSH_FX_NO_CONNECTION */
		"Connection lost",		/* SSH_FX_CONNECTION_LOST */
		"Operation unsupported",	/* SSH_FX_OP_UNSUPPORTED */
		"Unknown error"			/* Others */
	};
401 402
	return (status_messages[MIN(status,SSH2_FX_MAX)]);
}
403

404 405 406 407 408 409 410 411 412
static void
send_status(u_int32_t id, u_int32_t status)
{
	Buffer msg;

	debug3("request %u: sent status %u", id, status);
	if (log_level > SYSLOG_LEVEL_VERBOSE ||
	    (status != SSH2_FX_OK && status != SSH2_FX_EOF))
		logit("sent status %s", status_to_message(status));
Damien Miller's avatar
Damien Miller committed
413
	buffer_init(&msg);
Ben Lindstrom's avatar
Ben Lindstrom committed
414
	buffer_put_char(&msg, SSH2_FXP_STATUS);
Damien Miller's avatar
Damien Miller committed
415
	buffer_put_int(&msg, id);
416
	buffer_put_int(&msg, status);
Damien Miller's avatar
Damien Miller committed
417
	if (version >= 3) {
418
		buffer_put_cstring(&msg, status_to_message(status));
Damien Miller's avatar
Damien Miller committed
419 420
		buffer_put_cstring(&msg, "");
	}
Damien Miller's avatar
Damien Miller committed
421 422 423
	send_msg(&msg);
	buffer_free(&msg);
}
424
static void
425
send_data_or_handle(char type, u_int32_t id, const char *data, int dlen)
Damien Miller's avatar
Damien Miller committed
426 427
{
	Buffer msg;
428

Damien Miller's avatar
Damien Miller committed
429 430 431 432 433 434 435 436
	buffer_init(&msg);
	buffer_put_char(&msg, type);
	buffer_put_int(&msg, id);
	buffer_put_string(&msg, data, dlen);
	send_msg(&msg);
	buffer_free(&msg);
}

437
static void
438
send_data(u_int32_t id, const char *data, int dlen)
Damien Miller's avatar
Damien Miller committed
439
{
440
	debug("request %u: sent data len %d", id, dlen);
Ben Lindstrom's avatar
Ben Lindstrom committed
441
	send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
Damien Miller's avatar
Damien Miller committed
442 443
}

444
static void
Damien Miller's avatar
Damien Miller committed
445 446 447 448
send_handle(u_int32_t id, int handle)
{
	char *string;
	int hlen;
449

Damien Miller's avatar
Damien Miller committed
450
	handle_to_string(handle, &string, &hlen);
451
	debug("request %u: sent handle handle %d", id, handle);
Ben Lindstrom's avatar
Ben Lindstrom committed
452
	send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
453
	free(string);
Damien Miller's avatar
Damien Miller committed
454 455
}

456
static void
457
send_names(u_int32_t id, int count, const Stat *stats)
Damien Miller's avatar
Damien Miller committed
458 459 460
{
	Buffer msg;
	int i;
461

Damien Miller's avatar
Damien Miller committed
462
	buffer_init(&msg);
Ben Lindstrom's avatar
Ben Lindstrom committed
463
	buffer_put_char(&msg, SSH2_FXP_NAME);
Damien Miller's avatar
Damien Miller committed
464 465
	buffer_put_int(&msg, id);
	buffer_put_int(&msg, count);
466
	debug("request %u: sent names count %d", id, count);
Damien Miller's avatar
Damien Miller committed
467 468 469 470 471 472 473 474 475
	for (i = 0; i < count; i++) {
		buffer_put_cstring(&msg, stats[i].name);
		buffer_put_cstring(&msg, stats[i].long_name);
		encode_attrib(&msg, &stats[i].attrib);
	}
	send_msg(&msg);
	buffer_free(&msg);
}

476
static void
477
send_attrib(u_int32_t id, const Attrib *a)
Damien Miller's avatar
Damien Miller committed
478 479
{
	Buffer msg;
480

481
	debug("request %u: sent attrib have 0x%x", id, a->flags);
Damien Miller's avatar
Damien Miller committed
482
	buffer_init(&msg);
Ben Lindstrom's avatar
Ben Lindstrom committed
483
	buffer_put_char(&msg, SSH2_FXP_ATTRS);
Damien Miller's avatar
Damien Miller committed
484 485 486 487 488 489
	buffer_put_int(&msg, id);
	encode_attrib(&msg, a);
	send_msg(&msg);
	buffer_free(&msg);
}

490 491 492 493 494 495 496 497 498 499 500 501
static void
send_statvfs(u_int32_t id, struct statvfs *st)
{
	Buffer msg;
	u_int64_t flag;

	flag = (st->f_flag & ST_RDONLY) ? SSH2_FXE_STATVFS_ST_RDONLY : 0;
	flag |= (st->f_flag & ST_NOSUID) ? SSH2_FXE_STATVFS_ST_NOSUID : 0;

	buffer_init(&msg);
	buffer_put_char(&msg, SSH2_FXP_EXTENDED_REPLY);
	buffer_put_int(&msg, id);
502 503
	buffer_put_int64(&msg, st->f_bsize);
	buffer_put_int64(&msg, st->f_frsize);
504 505 506 507 508 509
	buffer_put_int64(&msg, st->f_blocks);
	buffer_put_int64(&msg, st->f_bfree);
	buffer_put_int64(&msg, st->f_bavail);
	buffer_put_int64(&msg, st->f_files);
	buffer_put_int64(&msg, st->f_ffree);
	buffer_put_int64(&msg, st->f_favail);
510
	buffer_put_int64(&msg, FSID_TO_ULONG(st->f_fsid));
511 512
	buffer_put_int64(&msg, flag);
	buffer_put_int64(&msg, st->f_namemax);
513 514 515 516
	send_msg(&msg);
	buffer_free(&msg);
}

Damien Miller's avatar
Damien Miller committed
517 518
/* parse incoming */

519
static void
Damien Miller's avatar
Damien Miller committed
520 521 522 523
process_init(void)
{
	Buffer msg;

524
	version = get_int();
525
	verbose("received client version %u", version);
Damien Miller's avatar
Damien Miller committed
526
	buffer_init(&msg);
Ben Lindstrom's avatar
Ben Lindstrom committed
527 528
	buffer_put_char(&msg, SSH2_FXP_VERSION);
	buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
529 530 531
	/* POSIX rename extension */
	buffer_put_cstring(&msg, "posix-rename@openssh.com");
	buffer_put_cstring(&msg, "1"); /* version */
532
	/* statvfs extension */
533
	buffer_put_cstring(&msg, "statvfs@openssh.com");
534
	buffer_put_cstring(&msg, "2"); /* version */
535
	/* fstatvfs extension */
536
	buffer_put_cstring(&msg, "fstatvfs@openssh.com");
537
	buffer_put_cstring(&msg, "2"); /* version */
538 539 540
	/* hardlink extension */
	buffer_put_cstring(&msg, "hardlink@openssh.com");
	buffer_put_cstring(&msg, "1"); /* version */
Damien Miller's avatar
Damien Miller committed
541 542 543 544
	send_msg(&msg);
	buffer_free(&msg);
}

545
static void
Damien Miller's avatar
Damien Miller committed
546 547 548 549 550
process_open(void)
{
	u_int32_t id, pflags;
	Attrib *a;
	char *name;
Ben Lindstrom's avatar
Ben Lindstrom committed
551
	int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
Damien Miller's avatar
Damien Miller committed
552 553 554

	id = get_int();
	name = get_string(NULL);
Ben Lindstrom's avatar
Ben Lindstrom committed
555
	pflags = get_int();		/* portable flags */
556
	debug3("request %u: open flags %d", id, pflags);
Damien Miller's avatar
Damien Miller committed
557 558
	a = get_attrib();
	flags = flags_from_portable(pflags);
Ben Lindstrom's avatar
Ben Lindstrom committed
559
	mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
560 561
	logit("open \"%s\" flags %s mode 0%o",
	    name, string_from_portable(pflags), mode);
562 563 564 565 566 567 568
	if (readonly &&
	    ((flags & O_ACCMODE) == O_WRONLY || (flags & O_ACCMODE) == O_RDWR))
		status = SSH2_FX_PERMISSION_DENIED;
	else {
		fd = open(name, flags, mode);
		if (fd < 0) {
			status = errno_to_portable(errno);
Damien Miller's avatar
Damien Miller committed
569
		} else {
570 571 572 573 574 575 576
			handle = handle_new(HANDLE_FILE, name, fd, NULL);
			if (handle < 0) {
				close(fd);
			} else {
				send_handle(id, handle);
				status = SSH2_FX_OK;
			}
Damien Miller's avatar
Damien Miller committed
577 578
		}
	}
Ben Lindstrom's avatar
Ben Lindstrom committed
579
	if (status != SSH2_FX_OK)
Damien Miller's avatar
Damien Miller committed
580
		send_status(id, status);
581
	free(name);
Damien Miller's avatar
Damien Miller committed
582 583
}

584
static void
Damien Miller's avatar
Damien Miller committed
585 586 587
process_close(void)
{
	u_int32_t id;
Ben Lindstrom's avatar
Ben Lindstrom committed
588
	int handle, ret, status = SSH2_FX_FAILURE;
Damien Miller's avatar
Damien Miller committed
589 590 591

	id = get_int();
	handle = get_handle();
592 593
	debug3("request %u: close handle %u", id, handle);
	handle_log_close(handle, NULL);
Damien Miller's avatar
Damien Miller committed
594
	ret = handle_close(handle);
Ben Lindstrom's avatar
Ben Lindstrom committed
595
	status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
Damien Miller's avatar
Damien Miller committed
596 597 598
	send_status(id, status);
}

599
static void
Damien Miller's avatar
Damien Miller committed
600 601 602
process_read(void)
{
	char buf[64*1024];
Ben Lindstrom's avatar
Ben Lindstrom committed
603 604
	u_int32_t id, len;
	int handle, fd, ret, status = SSH2_FX_FAILURE;
Damien Miller's avatar
Damien Miller committed
605 606 607 608
	u_int64_t off;

	id = get_int();
	handle = get_handle();
Ben Lindstrom's avatar
Ben Lindstrom committed
609
	off = get_int64();
Damien Miller's avatar
Damien Miller committed
610 611
	len = get_int();

612 613
	debug("request %u: read \"%s\" (handle %d) off %llu len %d",
	    id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller's avatar
Damien Miller committed
614 615
	if (len > sizeof buf) {
		len = sizeof buf;
616
		debug2("read change len %d", len);
Damien Miller's avatar
Damien Miller committed
617 618 619 620 621 622 623 624 625 626 627
	}
	fd = handle_to_fd(handle);
	if (fd >= 0) {
		if (lseek(fd, off, SEEK_SET) < 0) {
			error("process_read: seek failed");
			status = errno_to_portable(errno);
		} else {
			ret = read(fd, buf, len);
			if (ret < 0) {
				status = errno_to_portable(errno);
			} else if (ret == 0) {
Ben Lindstrom's avatar
Ben Lindstrom committed
628
				status = SSH2_FX_EOF;
Damien Miller's avatar
Damien Miller committed
629 630
			} else {
				send_data(id, buf, ret);
Ben Lindstrom's avatar
Ben Lindstrom committed
631
				status = SSH2_FX_OK;
632
				handle_update_read(handle, ret);
Damien Miller's avatar
Damien Miller committed
633 634 635
			}
		}
	}
Ben Lindstrom's avatar
Ben Lindstrom committed
636
	if (status != SSH2_FX_OK)
Damien Miller's avatar
Damien Miller committed
637 638 639
		send_status(id, status);
}

640
static void
Damien Miller's avatar
Damien Miller committed
641 642
process_write(void)
{
Ben Lindstrom's avatar
Ben Lindstrom committed
643
	u_int32_t id;
Damien Miller's avatar
Damien Miller committed
644
	u_int64_t off;
645
	u_int len;
646
	int handle, fd, ret, status;
Damien Miller's avatar
Damien Miller committed
647 648 649 650
	char *data;

	id = get_int();
	handle = get_handle();
Ben Lindstrom's avatar
Ben Lindstrom committed
651
	off = get_int64();
Damien Miller's avatar
Damien Miller committed
652 653
	data = get_string(&len);

654 655
	debug("request %u: write \"%s\" (handle %d) off %llu len %d",
	    id, handle_to_name(handle), handle, (unsigned long long)off, len);
Damien Miller's avatar
Damien Miller committed
656
	fd = handle_to_fd(handle);
657 658 659 660 661 662
	
	if (fd < 0)
		status = SSH2_FX_FAILURE;
	else if (readonly)
		status = SSH2_FX_PERMISSION_DENIED;
	else {
Damien Miller's avatar
Damien Miller committed
663 664 665 666 667 668
		if (lseek(fd, off, SEEK_SET) < 0) {
			status = errno_to_portable(errno);
			error("process_write: seek failed");
		} else {
/* XXX ATOMICIO ? */
			ret = write(fd, data, len);
669
			if (ret < 0) {
Damien Miller's avatar
Damien Miller committed
670 671
				error("process_write: write failed");
				status = errno_to_portable(errno);
672
			} else if ((size_t)ret == len) {
Ben Lindstrom's avatar
Ben Lindstrom committed
673
				status = SSH2_FX_OK;
674
				handle_update_write(handle, ret);
Damien Miller's avatar
Damien Miller committed
675
			} else {
676
				debug2("nothing at all written");
677
				status = SSH2_FX_FAILURE;
Damien Miller's avatar
Damien Miller committed
678 679 680 681
			}
		}
	}
	send_status(id, status);
682
	free(data);
Damien Miller's avatar
Damien Miller committed
683 684
}

685
static void
Damien Miller's avatar
Damien Miller committed
686 687
process_do_stat(int do_lstat)
{
688
	Attrib a;
Damien Miller's avatar
Damien Miller committed
689 690 691
	struct stat st;
	u_int32_t id;
	char *name;
Ben Lindstrom's avatar
Ben Lindstrom committed
692
	int ret, status = SSH2_FX_FAILURE;
Damien Miller's avatar
Damien Miller committed
693 694 695

	id = get_int();
	name = get_string(NULL);
696 697
	debug3("request %u: %sstat", id, do_lstat ? "l" : "");
	verbose("%sstat name \"%s\"", do_lstat ? "l" : "", name);
Damien Miller's avatar
Damien Miller committed
698 699 700 701
	ret = do_lstat ? lstat(name, &st) : stat(name, &st);
	if (ret < 0) {
		status = errno_to_portable(errno);
	} else {
702 703
		stat_to_attrib(&st, &a);
		send_attrib(id, &a);
Ben Lindstrom's avatar
Ben Lindstrom committed
704
		status = SSH2_FX_OK;
Damien Miller's avatar
Damien Miller committed
705
	}
Ben Lindstrom's avatar
Ben Lindstrom committed
706
	if (status != SSH2_FX_OK)
Damien Miller's avatar
Damien Miller committed
707
		send_status(id, status);
708
	free(name);
Damien Miller's avatar
Damien Miller committed
709 710
}

711
static void
Damien Miller's avatar
Damien Miller committed
712 713 714 715 716
process_stat(void)
{
	process_do_stat(0);
}

717
static void
Damien Miller's avatar
Damien Miller committed
718 719 720 721 722
process_lstat(void)
{
	process_do_stat(1);
}

723
static void
Damien Miller's avatar
Damien Miller committed
724 725
process_fstat(void)
{
726
	Attrib a;
Damien Miller's avatar
Damien Miller committed
727 728
	struct stat st;
	u_int32_t id;
Ben Lindstrom's avatar
Ben Lindstrom committed
729
	int fd, ret, handle, status = SSH2_FX_FAILURE;
Damien Miller's avatar
Damien Miller committed
730 731 732

	id = get_int();
	handle = get_handle();
733 734
	debug("request %u: fstat \"%s\" (handle %u)",
	    id, handle_to_name(handle), handle);
Damien Miller's avatar
Damien Miller committed
735
	fd = handle_to_fd(handle);
736
	if (fd >= 0) {
Damien Miller's avatar
Damien Miller committed
737 738 739 740
		ret = fstat(fd, &st);
		if (ret < 0) {
			status = errno_to_portable(errno);
		} else {
741 742
			stat_to_attrib(&st, &a);
			send_attrib(id, &a);
Ben Lindstrom's avatar
Ben Lindstrom committed
743
			status = SSH2_FX_OK;
Damien Miller's avatar
Damien Miller committed
744 745
		}
	}
Ben Lindstrom's avatar
Ben Lindstrom committed
746
	if (status != SSH2_FX_OK)
Damien Miller's avatar
Damien Miller committed
747 748 749
		send_status(id, status);
}

750
static struct timeval *
751
attrib_to_tv(const Attrib *a)
Damien Miller's avatar
Damien Miller committed
752 753
{
	static struct timeval tv[2];
754

Damien Miller's avatar
Damien Miller committed
755 756 757 758 759 760 761
	tv[0].tv_sec = a->atime;
	tv[0].tv_usec = 0;
	tv[1].tv_sec = a->mtime;
	tv[1].tv_usec = 0;
	return tv;
}

762
static void
Damien Miller's avatar
Damien Miller committed
763 764 765 766 767
process_setstat(void)
{
	Attrib *a;
	u_int32_t id;
	char *name;
768
	int status = SSH2_FX_OK, ret;
Damien Miller's avatar
Damien Miller committed
769 770 771 772

	id = get_int();
	name = get_string(NULL);
	a = get_attrib();
773
	debug("request %u: setstat name \"%s\"", id, name);
774 775 776 777
	if (readonly) {
		status = SSH2_FX_PERMISSION_DENIED;
		a->flags = 0;
	}
778
	if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
779 780
		logit("set \"%s\" size %llu",
		    name, (unsigned long long)a->size);
781 782 783 784
		ret = truncate(name, a->size);
		if (ret == -1)
			status = errno_to_portable(errno);
	}
Ben Lindstrom's avatar
Ben Lindstrom committed
785
	if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
786
		logit("set \"%s\" mode %04o", name, a->perm);
787
		ret = chmod(name, a->perm & 07777);
Damien Miller's avatar
Damien Miller committed
788 789 790
		if (ret == -1)
			status = errno_to_portable(errno);
	}
Ben Lindstrom's avatar
Ben Lindstrom committed
791
	if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
792 793 794 795 796 797
		char buf[64];
		time_t t = a->mtime;

		strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
		    localtime(&t));
		logit("set \"%s\" modtime %s", name, buf);
Damien Miller's avatar
Damien Miller committed
798 799 800 801
		ret = utimes(name, attrib_to_tv(a));
		if (ret == -1)
			status = errno_to_portable(errno);
	}
802
	if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
803 804
		logit("set \"%s\" owner %lu group %lu", name,
		    (u_long)a->uid, (u_long)a->gid);
805 806 807 808
		ret = chown(name, a->uid, a->gid);
		if (ret == -1)
			status = errno_to_portable(errno);
	}
Damien Miller's avatar
Damien Miller committed
809
	send_status(id, status);
810
	free(name);
Damien Miller's avatar
Damien Miller committed
811 812
}

813
static void
Damien Miller's avatar
Damien Miller committed
814 815 816 817 818
process_fsetstat(void)
{
	Attrib *a;
	u_int32_t id;
	int handle, fd, ret;
Ben Lindstrom's avatar
Ben Lindstrom committed
819
	int status = SSH2_FX_OK;
820

Damien Miller's avatar
Damien Miller committed
821 822 823
	id = get_int();
	handle = get_handle();
	a = get_attrib();
824
	debug("request %u: fsetstat handle %d", id, handle);
Damien Miller's avatar
Damien Miller committed
825
	fd = handle_to_fd(handle);
826
	if (fd < 0)
Ben Lindstrom's avatar
Ben Lindstrom committed
827
		status = SSH2_FX_FAILURE;
828 829 830
	else if (readonly)
		status = SSH2_FX_PERMISSION_DENIED;
	else {
831 832
		char *name = handle_to_name(handle);

833
		if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
834 835
			logit("set \"%s\" size %llu",
			    name, (unsigned long long)a->size);
836 837 838 839
			ret = ftruncate(fd, a->size);
			if (ret == -1)
				status = errno_to_portable(errno);
		}
Ben Lindstrom's avatar
Ben Lindstrom committed
840
		if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
841
			logit("set \"%s\" mode %04o", name, a->perm);
Ben Lindstrom's avatar
Ben Lindstrom committed
842
#ifdef HAVE_FCHMOD
843
			ret = fchmod(fd, a->perm & 07777);
Ben Lindstrom's avatar
Ben Lindstrom committed
844
#else
845
			ret = chmod(name, a->perm & 07777);
Ben Lindstrom's avatar
Ben Lindstrom committed
846
#endif
Damien Miller's avatar
Damien Miller committed
847 848 849
			if (ret == -1)
				status = errno_to_portable(errno);
		}
Ben Lindstrom's avatar
Ben Lindstrom committed
850
		if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
851 852 853 854 855 856
			char buf[64];
			time_t t = a->mtime;

			strftime(buf, sizeof(buf), "%Y%m%d-%H:%M:%S",
			    localtime(&t));
			logit("set \"%s\" modtime %s", name, buf);
Damien Miller's avatar
Damien Miller committed
857 858 859 860 861 862 863 864
#ifdef HAVE_FUTIMES
			ret = futimes(fd, attrib_to_tv(a));
#else
			ret = utimes(name, attrib_to_tv(a));
#endif
			if (ret == -1)
				status = errno_to_portable(errno);
		}
865
		if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
866 867
			logit("set \"%s\" owner %lu group %lu", name,
			    (u_long)a->uid, (u_long)a->gid);
868
#ifdef HAVE_FCHOWN
869
			ret = fchown(fd, a->uid, a->gid);
870 871 872
#else
			ret = chown(name, a->uid, a->gid);
#endif
873 874 875
			if (ret == -1)
				status = errno_to_portable(errno);
		}
Damien Miller's avatar
Damien Miller committed
876 877 878 879
	}
	send_status(id, status);
}

880
static void
Damien Miller's avatar
Damien Miller committed
881 882 883 884
process_opendir(void)
{
	DIR *dirp = NULL;
	char *path;
Ben Lindstrom's avatar
Ben Lindstrom committed
885
	int handle, status = SSH2_FX_FAILURE;
Damien Miller's avatar
Damien Miller committed
886 887 888 889
	u_int32_t id;

	id = get_int();
	path = get_string(NULL);
890 891
	debug3("request %u: opendir", id);
	logit("opendir \"%s\"", path);
892
	dirp = opendir(path);
Damien Miller's avatar
Damien Miller committed
893 894 895
	if (dirp == NULL) {
		status = errno_to_portable(errno);
	} else {
Damien Miller's avatar
Damien Miller committed
896
		handle = handle_new(HANDLE_DIR, path, 0, dirp);
Damien Miller's avatar
Damien Miller committed
897 898 899 900
		if (handle < 0) {
			closedir(dirp);
		} else {
			send_handle(id, handle);
Ben Lindstrom's avatar
Ben Lindstrom committed
901
			status = SSH2_FX_OK;
Damien Miller's avatar
Damien Miller committed
902
		}
903

Damien Miller's avatar
Damien Miller committed
904
	}
Ben Lindstrom's avatar
Ben Lindstrom committed
905
	if (status != SSH2_FX_OK)
Damien Miller's avatar
Damien Miller committed
906
		send_status(id, status);
907
	free(path);
Damien Miller's avatar
Damien Miller committed
908 909
}

910
static void
Damien Miller's avatar
Damien Miller committed
911 912 913 914 915 916 917 918 919 920
process_readdir(void)
{
	DIR *dirp;
	struct dirent *dp;
	char *path;
	int handle;
	u_int32_t id;

	id = get_int();
	handle = get_handle();
921 922
	debug("request %u: readdir \"%s\" (handle %d)", id,
	    handle_to_name(handle), handle);
Damien Miller's avatar
Damien Miller committed
923 924 925
	dirp = handle_to_dir(handle);
	path = handle_to_name(handle);
	if (dirp == NULL || path == NULL) {
Ben Lindstrom's avatar
Ben Lindstrom committed
926
		send_status(id, SSH2_FX_FAILURE);
Damien Miller's avatar
Damien Miller committed
927 928
	} else {
		struct stat st;
929
		char pathname[MAXPATHLEN];
Damien Miller's avatar
Damien Miller committed
930 931
		Stat *stats;
		int nstats = 10, count = 0, i;
932

933
		stats = xcalloc(nstats, sizeof(Stat));
Damien Miller's avatar
Damien Miller committed
934 935 936
		while ((dp = readdir(dirp)) != NULL) {
			if (count >= nstats) {
				nstats *= 2;
937
				stats = xrealloc(stats, nstats, sizeof(Stat));
Damien Miller's avatar
Damien Miller committed
938 939
			}
/* XXX OVERFLOW ? */
940 941
			snprintf(pathname, sizeof pathname, "%s%s%s", path,
			    strcmp(path, "/") ? "/" : "", dp->d_name);
Damien Miller's avatar
Damien Miller committed
942 943
			if (lstat(pathname, &st) < 0)
				continue;
944
			stat_to_attrib(&st, &(stats[count].attrib));
Damien Miller's avatar
Damien Miller committed
945
			stats[count].name = xstrdup(dp->d_name);
946
			stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
Damien Miller's avatar
Damien Miller committed
947 948
			count++;
			/* send up to 100 entries in one message */
949
			/* XXX check packet size instead */
Damien Miller's avatar
Damien Miller committed
950 951 952
			if (count == 100)
				break;
		}
Ben Lindstrom's avatar
Ben Lindstrom committed
953 954
		if (count > 0) {
			send_names(id, count, stats);
955
			for (i = 0; i < count; i++) {
956 957
				free(stats[i].name);
				free(stats[i].long_name);
Ben Lindstrom's avatar
Ben Lindstrom committed
958 959 960
			}
		} else {
			send_status(id, SSH2_FX_EOF);
Damien Miller's avatar
Damien Miller committed
961
		}
962
		free(stats);
Damien Miller's avatar
Damien Miller committed
963 964 965
	}
}

966
static void
Damien Miller's avatar
Damien Miller committed
967 968 969 970
process_remove(void)
{
	char *name;
	u_int32_t id;
Ben Lindstrom's avatar
Ben Lindstrom committed
971
	int status = SSH2_FX_FAILURE;
Damien Miller's avatar
Damien Miller committed
972 973 974 975
	int ret;

	id = get_int();
	name = get_string(NULL);
976 977
	debug3("request %u: remove", id);
	logit("remove name \"%s\"", name);
978 979 980 981 982 983
	if (readonly)
		status = SSH2_FX_PERMISSION_DENIED;
	else {
		ret = unlink(name);
		status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
	}
Damien Miller's avatar
Damien Miller committed
984
	send_status(id, status);
985
	free(name);
Damien Miller's avatar
Damien Miller committed
986 987
}

988
static void
Damien Miller's avatar
Damien Miller committed
989 990 991 992 993
process_mkdir(void)
{
	Attrib *a;
	u_int32_t id;
	char *name;
Ben Lindstrom's avatar
Ben Lindstrom committed
994
	int ret, mode, status = SSH2_FX_FAILURE;
Damien Miller's avatar
Damien Miller committed
995 996 997 998

	id = get_int();
	name = get_string(NULL);
	a = get_attrib();
Ben Lindstrom's avatar
Ben Lindstrom committed
999
	mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
1000
	    a->perm & 07777 : 0777;
1001 1002
	debug3("request %u: mkdir", id);
	logit("mkdir name \"%s\" mode 0%o", name, mode);
1003 1004 1005 1006 1007 1008
	if (readonly)
		status = SSH2_FX_PERMISSION_DENIED;
	else {
		ret = mkdir(name, mode);
		status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
	}
Damien Miller's avatar
Damien Miller committed
1009
	send_status(id, status);
1010
	free(name);
Damien Miller's avatar
Damien Miller committed
1011 1012
}

1013
static void
Damien Miller's avatar
Damien Miller committed
1014 1015 1016 1017 1018 1019 1020 1021
process_rmdir(void)
{
	u_int32_t id;
	char *name;
	int ret, status;

	id = get_int();
	name = get_string(NULL);
1022 1023
	debug3("request %u: rmdir", id);
	logit("rmdir name \"%s\"", name);
1024 1025 1026 1027 1028 1029
	if (readonly)
		status = SSH2_FX_PERMISSION_DENIED;
	else {
		ret = rmdir(name);
		status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
	}
Damien Miller's avatar
Damien Miller committed
1030
	send_status(id, status);
1031
	free(name);
Damien Miller's avatar
Damien Miller committed
1032 1033
}

1034
static void
Damien Miller's avatar
Damien Miller committed
1035 1036 1037 1038 1039 1040 1041 1042
process_realpath(void)
{
	char resolvedname[MAXPATHLEN];
	u_int32_t id;
	char *path;

	id = get_int();
	path = get_string(NULL);
Ben Lindstrom's avatar
Ben Lindstrom committed
1043
	if (path[0] == '\0') {
1044
		free(path);
Ben Lindstrom's avatar
Ben Lindstrom committed
1045 1046
		path = xstrdup(".");
	}
1047 1048
	debug3("request %u: realpath", id);
	verbose("realpath \"%s\"", path);
Damien Miller's avatar
Damien Miller committed
1049 1050 1051 1052 1053 1054 1055 1056
	if (realpath(path, resolvedname) == NULL) {
		send_status(id, errno_to_portable(errno));
	} else {
		Stat s;
		attrib_clear(&s.attrib);
		s.name = s.long_name = resolvedname;
		send_names(id, 1, &s);
	}
1057
	free(path);
Damien Miller's avatar
Damien Miller committed
1058 1059
}

1060
static void
Damien Miller's avatar
Damien Miller committed
1061 1062 1063 1064
process_rename(void)
{
	u_int32_t id;
	char *oldpath, *newpath;
1065
	int status;
1066
	struct stat sb;
Damien Miller's avatar
Damien Miller committed
1067 1068 1069 1070

	id = get_int();
	oldpath = get_string(NULL);
	newpath = get_string(NULL);
1071 1072
	debug3("request %u: rename", id);
	logit("rename old \"%s\" new \"%s\"", oldpath, newpath);
1073
	status = SSH2_FX_FAILURE;
1074 1075 1076
	if (readonly)
		status = SSH2_FX_PERMISSION_DENIED;
	else if (lstat(oldpath, &sb) == -1)
1077
		status = errno_to_portable(errno);
1078 1079
	else if (S_ISREG(sb.st_mode)) {
		/* Race-free rename of regular files */
1080
		if (link(oldpath, newpath) == -1) {
1081
			if (errno == EOPNOTSUPP || errno == ENOSYS
1082 1083 1084
#ifdef EXDEV
			    || errno == EXDEV
#endif
1085 1086 1087 1088
#ifdef LINK_OPNOTSUPP_ERRNO
			    || errno == LINK_OPNOTSUPP_ERRNO
#endif
			    ) {
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
				struct stat st;

				/*
				 * fs doesn't support links, so fall back to
				 * stat+rename.  This is racy.
				 */
				if (stat(newpath, &st) == -1) {
					if (rename(oldpath, newpath) == -1)
						status =
						    errno_to_portable(errno);
					else
						status = SSH2_FX_OK;
				}
			} else {
				status = errno_to_portable(errno);
			}
		} else if (unlink(oldpath) == -1) {
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
			status = errno_to_portable(errno);
			/* clean spare link */
			unlink(newpath);
		} else
			status = SSH2_FX_OK;
	} else if (stat(newpath, &sb) == -1) {
		if (rename(oldpath, newpath) == -1)
			status = errno_to_portable(errno);
		else
			status = SSH2_FX_OK;
	}
Damien Miller's avatar
Damien Miller committed
1117
	send_status(id, status);
1118 1119
	free(oldpath);
	free(newpath);
Damien Miller's avatar
Damien Miller committed
1120 1121
}

1122
static void
Damien Miller's avatar
Damien Miller committed
1123 1124 1125
process_readlink(void)
{
	u_int32_t id;
1126
	int len;
1127
	char buf[MAXPATHLEN];
Damien Miller's avatar
Damien Miller committed
1128 1129 1130 1131
	char *path;

	id = get_int();
	path = get_string(NULL);
1132 1133
	debug3("request %u: readlink", id);
	verbose("readlink \"%s\"", path);
1134
	if ((len = readlink(path, buf, sizeof(buf) - 1)) == -1)
Damien Miller's avatar
Damien Miller committed
1135 1136 1137
		send_status(id, errno_to_portable(errno));
	else {
		Stat s;
1138

1139
		buf[len] = '\0';
Damien Miller's avatar
Damien Miller committed
1140
		attrib_clear(&s.attrib);
1141
		s.name = s.long_name = buf;
Damien Miller's avatar
Damien Miller committed
1142 1143
		send_names(id, 1, &s);
	}
1144
	free(path);
Damien Miller's avatar
Damien Miller committed
1145 1146
}

1147
static void
Damien Miller's avatar
Damien Miller committed
1148 1149 1150 1151
process_symlink(void)
{
	u_int32_t id;
	char *oldpath, *newpath;
1152
	int ret, status;
Damien Miller's avatar
Damien Miller committed
1153 1154 1155 1156

	id = get_int();
	oldpath = get_string(NULL);
	newpath = get_string(NULL);
1157 1158
	debug3("request %u: symlink", id);
	logit("symlink old \"%s\" new \"%s\"", oldpath, newpath);
1159
	/* this will fail if 'newpath' exists */
1160 1161 1162 1163 1164 1165
	if (readonly)
		status = SSH2_FX_PERMISSION_DENIED;
	else {
		ret = symlink(oldpath, newpath);
		status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
	}
Damien Miller's avatar
Damien Miller committed
1166
	send_status(id, status);
1167 1168
	free(oldpath);
	free(newpath);
Damien Miller's avatar
Damien Miller committed
1169 1170
}

1171 1172 1173 1174
static void
process_extended_posix_rename(u_int32_t id)
{
	char *oldpath, *newpath;
1175
	int ret, status;
1176 1177 1178 1179 1180

	oldpath = get_string(NULL);
	newpath = get_string(NULL);
	debug3("request %u: posix-rename", id);
	logit("posix-rename old \"%s\" new \"%s\"", oldpath, newpath);
1181 1182 1183 1184 1185 1186 1187
	if (readonly)
		status = SSH2_FX_PERMISSION_DENIED;
	else {
		ret = rename(oldpath, newpath);
		status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
	}
	send_status(id, status);
1188 1189
	free(oldpath);
	free(newpath);
1190 1191
}

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
static void
process_extended_statvfs(u_int32_t id)
{
	char *path;
	struct statvfs st;

	path = get_string(NULL);
	debug3("request %u: statfs", id);
	logit("statfs \"%s\"", path);

	if (statvfs(path, &st) != 0)
		send_status(id, errno_to_portable(errno));
	else
		send_statvfs(id, &st);
1206
        free(path);
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
}

static void
process_extended_fstatvfs(u_int32_t id)
{
	int handle, fd;
	struct statvfs st;

	handle = get_handle();
	debug("request %u: fstatvfs \"%s\" (handle %u)",
	    id, handle_to_name(handle), handle);
	if ((fd = handle_to_fd(handle)) < 0) {
		send_status(id, SSH2_FX_FAILURE);
		return;
	}
	if (fstatvfs(fd, &st) != 0)
		send_status(id, errno_to_portable(errno));
	else
		send_statvfs(id, &st);
}

1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
static void
process_extended_hardlink(u_int32_t id)
{
	char *oldpath, *newpath;
	int ret, status;

	oldpath = get_string(NULL);
	newpath = get_string(NULL);
	debug3("request %u: hardlink", id);
	logit("hardlink old \"%s\" new \"%s\"", oldpath, newpath);
	if (readonly)
		status = SSH2_FX_PERMISSION_DENIED;
	else {
		ret = link(oldpath, newpath);
		status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
	}
	send_status(id, status);
1245 1246
	free(oldpath);
	free(newpath);
1247 1248
}

1249
static void
Ben Lindstrom's avatar
Ben Lindstrom committed
1250 1251 1252 1253 1254 1255 1256
process_extended(void)
{
	u_int32_t id;
	char *request;

	id = get_int();
	request = get_string(NULL);
1257 1258
	if (strcmp(request, "posix-rename@openssh.com") == 0)
		process_extended_posix_rename(id);
1259 1260 1261 1262
	else if (strcmp(request, "statvfs@openssh.com") == 0)
		process_extended_statvfs(id);
	else if (strcmp(request, "fstatvfs@openssh.com") == 0)
		process_extended_fstatvfs(id);
1263 1264
	else if (strcmp(request, "hardlink@openssh.com") == 0)
		process_extended_hardlink(id);
1265 1266
	else
		send_status(id, SSH2_FX_OP_UNSUPPORTED);	/* MUST */
1267
	free(request);
Ben Lindstrom's avatar
Ben Lindstrom committed
1268
}
Damien Miller's avatar
Damien Miller committed
1269 1270 1271

/* stolen from ssh-agent */

1272
static void
Damien Miller's avatar
Damien Miller committed
1273 1274
process(void)
{
1275
	u_int msg_len;
1276 1277
	u_int buf_len;
	u_int consumed;
1278 1279
	u_int type;
	u_char *cp;