Annotation of src/usr.bin/ssh/sftp-server.c, Revision 1.6.4.2
1.1 markus 1: /*
2: * Copyright (c) 2000 Markus Friedl. All rights reserved.
3: *
4: * Redistribution and use in source and binary forms, with or without
5: * modification, are permitted provided that the following conditions
6: * are met:
7: * 1. Redistributions of source code must retain the above copyright
8: * notice, this list of conditions and the following disclaimer.
9: * 2. Redistributions in binary form must reproduce the above copyright
10: * notice, this list of conditions and the following disclaimer in the
11: * documentation and/or other materials provided with the distribution.
12: *
13: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: */
24: #include "includes.h"
1.6.4.2 ! jason 25: RCSID("$OpenBSD: sftp-server.c,v 1.19 2001/02/07 18:01:18 itojun Exp $");
1.1 markus 26:
27: #include "buffer.h"
28: #include "bufaux.h"
29: #include "getput.h"
1.6.4.2 ! jason 30: #include "log.h"
1.1 markus 31: #include "xmalloc.h"
32:
1.6.4.2 ! jason 33: #include "sftp.h"
! 34: #include "sftp-common.h"
1.1 markus 35:
36: /* helper */
1.6.4.2 ! jason 37: #define get_int64() buffer_get_int64(&iqueue);
1.1 markus 38: #define get_int() buffer_get_int(&iqueue);
39: #define get_string(lenp) buffer_get_string(&iqueue, lenp);
1.6.4.2 ! jason 40: #define TRACE debug
1.1 markus 41:
42: /* input and output queue */
43: Buffer iqueue;
44: Buffer oqueue;
45:
46: /* portable attibutes, etc. */
47:
48: typedef struct Stat Stat;
49:
1.6.4.2 ! jason 50: struct Stat {
1.1 markus 51: char *name;
52: char *long_name;
53: Attrib attrib;
54: };
55:
56: int
1.2 markus 57: errno_to_portable(int unixerrno)
1.1 markus 58: {
59: int ret = 0;
1.2 markus 60: switch (unixerrno) {
1.1 markus 61: case 0:
1.6.4.2 ! jason 62: ret = SSH2_FX_OK;
1.1 markus 63: break;
64: case ENOENT:
65: case ENOTDIR:
66: case EBADF:
67: case ELOOP:
1.6.4.2 ! jason 68: ret = SSH2_FX_NO_SUCH_FILE;
1.1 markus 69: break;
70: case EPERM:
71: case EACCES:
72: case EFAULT:
1.6.4.2 ! jason 73: ret = SSH2_FX_PERMISSION_DENIED;
1.1 markus 74: break;
75: case ENAMETOOLONG:
76: case EINVAL:
1.6.4.2 ! jason 77: ret = SSH2_FX_BAD_MESSAGE;
1.1 markus 78: break;
79: default:
1.6.4.2 ! jason 80: ret = SSH2_FX_FAILURE;
1.1 markus 81: break;
82: }
83: return ret;
84: }
85:
86: int
87: flags_from_portable(int pflags)
88: {
89: int flags = 0;
1.6.4.2 ! jason 90: if (pflags & SSH2_FXF_READ &&
! 91: pflags & SSH2_FXF_WRITE) {
1.1 markus 92: flags = O_RDWR;
1.6.4.2 ! jason 93: } else if (pflags & SSH2_FXF_READ) {
1.1 markus 94: flags = O_RDONLY;
1.6.4.2 ! jason 95: } else if (pflags & SSH2_FXF_WRITE) {
1.1 markus 96: flags = O_WRONLY;
97: }
1.6.4.2 ! jason 98: if (pflags & SSH2_FXF_CREAT)
1.1 markus 99: flags |= O_CREAT;
1.6.4.2 ! jason 100: if (pflags & SSH2_FXF_TRUNC)
1.1 markus 101: flags |= O_TRUNC;
1.6.4.2 ! jason 102: if (pflags & SSH2_FXF_EXCL)
1.1 markus 103: flags |= O_EXCL;
104: return flags;
105: }
106:
107: Attrib *
108: get_attrib(void)
109: {
110: return decode_attrib(&iqueue);
111: }
112:
113: /* handle handles */
114:
115: typedef struct Handle Handle;
116: struct Handle {
117: int use;
118: DIR *dirp;
119: int fd;
120: char *name;
121: };
122: enum {
123: HANDLE_UNUSED,
124: HANDLE_DIR,
125: HANDLE_FILE
126: };
127: Handle handles[100];
128:
129: void
130: handle_init(void)
131: {
132: int i;
133: for(i = 0; i < sizeof(handles)/sizeof(Handle); i++)
134: handles[i].use = HANDLE_UNUSED;
135: }
136:
137: int
138: handle_new(int use, char *name, int fd, DIR *dirp)
139: {
140: int i;
141: for(i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
142: if (handles[i].use == HANDLE_UNUSED) {
143: handles[i].use = use;
144: handles[i].dirp = dirp;
145: handles[i].fd = fd;
146: handles[i].name = name;
147: return i;
148: }
149: }
150: return -1;
151: }
152:
153: int
154: handle_is_ok(int i, int type)
155: {
1.6.4.2 ! jason 156: return i >= 0 && i < sizeof(handles)/sizeof(Handle) &&
! 157: handles[i].use == type;
1.1 markus 158: }
159:
160: int
161: handle_to_string(int handle, char **stringp, int *hlenp)
162: {
163: if (stringp == NULL || hlenp == NULL)
164: return -1;
1.6.4.2 ! jason 165: *stringp = xmalloc(sizeof(int32_t));
! 166: PUT_32BIT(*stringp, handle);
! 167: *hlenp = sizeof(int32_t);
1.1 markus 168: return 0;
169: }
170:
171: int
1.5 markus 172: handle_from_string(char *handle, u_int hlen)
1.1 markus 173: {
1.6.4.2 ! jason 174: int val;
! 175: if (hlen != sizeof(int32_t))
1.1 markus 176: return -1;
1.6.4.2 ! jason 177: val = GET_32BIT(handle);
1.1 markus 178: if (handle_is_ok(val, HANDLE_FILE) ||
179: handle_is_ok(val, HANDLE_DIR))
180: return val;
181: return -1;
182: }
183:
184: char *
185: handle_to_name(int handle)
186: {
187: if (handle_is_ok(handle, HANDLE_DIR)||
188: handle_is_ok(handle, HANDLE_FILE))
189: return handles[handle].name;
190: return NULL;
191: }
192:
193: DIR *
194: handle_to_dir(int handle)
195: {
196: if (handle_is_ok(handle, HANDLE_DIR))
197: return handles[handle].dirp;
198: return NULL;
199: }
200:
201: int
202: handle_to_fd(int handle)
203: {
1.6.4.2 ! jason 204: if (handle_is_ok(handle, HANDLE_FILE))
1.1 markus 205: return handles[handle].fd;
206: return -1;
207: }
208:
209: int
210: handle_close(int handle)
211: {
212: int ret = -1;
213: if (handle_is_ok(handle, HANDLE_FILE)) {
214: ret = close(handles[handle].fd);
215: handles[handle].use = HANDLE_UNUSED;
216: } else if (handle_is_ok(handle, HANDLE_DIR)) {
217: ret = closedir(handles[handle].dirp);
218: handles[handle].use = HANDLE_UNUSED;
219: } else {
220: errno = ENOENT;
221: }
222: return ret;
223: }
224:
225: int
226: get_handle(void)
227: {
228: char *handle;
1.6.4.2 ! jason 229: int val = -1;
1.5 markus 230: u_int hlen;
1.1 markus 231: handle = get_string(&hlen);
1.6.4.2 ! jason 232: if (hlen < 256)
! 233: val = handle_from_string(handle, hlen);
1.1 markus 234: xfree(handle);
235: return val;
236: }
237:
238: /* send replies */
239:
240: void
241: send_msg(Buffer *m)
242: {
243: int mlen = buffer_len(m);
244: buffer_put_int(&oqueue, mlen);
245: buffer_append(&oqueue, buffer_ptr(m), mlen);
246: buffer_consume(m, mlen);
247: }
248:
249: void
250: send_status(u_int32_t id, u_int32_t error)
251: {
252: Buffer msg;
253: TRACE("sent status id %d error %d", id, error);
254: buffer_init(&msg);
1.6.4.2 ! jason 255: buffer_put_char(&msg, SSH2_FXP_STATUS);
1.1 markus 256: buffer_put_int(&msg, id);
257: buffer_put_int(&msg, error);
258: send_msg(&msg);
259: buffer_free(&msg);
260: }
261: void
262: send_data_or_handle(char type, u_int32_t id, char *data, int dlen)
263: {
264: Buffer msg;
265: buffer_init(&msg);
266: buffer_put_char(&msg, type);
267: buffer_put_int(&msg, id);
268: buffer_put_string(&msg, data, dlen);
269: send_msg(&msg);
270: buffer_free(&msg);
271: }
272:
273: void
274: send_data(u_int32_t id, char *data, int dlen)
275: {
276: TRACE("sent data id %d len %d", id, dlen);
1.6.4.2 ! jason 277: send_data_or_handle(SSH2_FXP_DATA, id, data, dlen);
1.1 markus 278: }
279:
280: void
281: send_handle(u_int32_t id, int handle)
282: {
283: char *string;
284: int hlen;
285: handle_to_string(handle, &string, &hlen);
286: TRACE("sent handle id %d handle %d", id, handle);
1.6.4.2 ! jason 287: send_data_or_handle(SSH2_FXP_HANDLE, id, string, hlen);
1.1 markus 288: xfree(string);
289: }
290:
291: void
292: send_names(u_int32_t id, int count, Stat *stats)
293: {
294: Buffer msg;
295: int i;
296: buffer_init(&msg);
1.6.4.2 ! jason 297: buffer_put_char(&msg, SSH2_FXP_NAME);
1.1 markus 298: buffer_put_int(&msg, id);
299: buffer_put_int(&msg, count);
300: TRACE("sent names id %d count %d", id, count);
301: for (i = 0; i < count; i++) {
302: buffer_put_cstring(&msg, stats[i].name);
303: buffer_put_cstring(&msg, stats[i].long_name);
304: encode_attrib(&msg, &stats[i].attrib);
305: }
306: send_msg(&msg);
307: buffer_free(&msg);
308: }
309:
310: void
311: send_attrib(u_int32_t id, Attrib *a)
312: {
313: Buffer msg;
314: TRACE("sent attrib id %d have 0x%x", id, a->flags);
315: buffer_init(&msg);
1.6.4.2 ! jason 316: buffer_put_char(&msg, SSH2_FXP_ATTRS);
1.1 markus 317: buffer_put_int(&msg, id);
318: encode_attrib(&msg, a);
319: send_msg(&msg);
320: buffer_free(&msg);
321: }
322:
323: /* parse incoming */
324:
325: void
326: process_init(void)
327: {
328: Buffer msg;
329: int version = buffer_get_int(&iqueue);
330:
331: TRACE("client version %d", version);
332: buffer_init(&msg);
1.6.4.2 ! jason 333: buffer_put_char(&msg, SSH2_FXP_VERSION);
! 334: buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
1.1 markus 335: send_msg(&msg);
336: buffer_free(&msg);
337: }
338:
339: void
340: process_open(void)
341: {
342: u_int32_t id, pflags;
343: Attrib *a;
344: char *name;
1.6.4.2 ! jason 345: int handle, fd, flags, mode, status = SSH2_FX_FAILURE;
1.1 markus 346:
347: id = get_int();
348: name = get_string(NULL);
1.6.4.2 ! jason 349: pflags = get_int(); /* portable flags */
1.1 markus 350: a = get_attrib();
351: flags = flags_from_portable(pflags);
1.6.4.2 ! jason 352: mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ? a->perm : 0666;
1.1 markus 353: TRACE("open id %d name %s flags %d mode 0%o", id, name, pflags, mode);
354: fd = open(name, flags, mode);
355: if (fd < 0) {
356: status = errno_to_portable(errno);
357: } else {
358: handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL);
359: if (handle < 0) {
360: close(fd);
361: } else {
362: send_handle(id, handle);
1.6.4.2 ! jason 363: status = SSH2_FX_OK;
1.1 markus 364: }
365: }
1.6.4.2 ! jason 366: if (status != SSH2_FX_OK)
1.1 markus 367: send_status(id, status);
368: xfree(name);
369: }
370:
371: void
372: process_close(void)
373: {
374: u_int32_t id;
1.6.4.2 ! jason 375: int handle, ret, status = SSH2_FX_FAILURE;
1.1 markus 376:
377: id = get_int();
378: handle = get_handle();
379: TRACE("close id %d handle %d", id, handle);
380: ret = handle_close(handle);
1.6.4.2 ! jason 381: status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.1 markus 382: send_status(id, status);
383: }
384:
385: void
386: process_read(void)
387: {
388: char buf[64*1024];
1.6.4.2 ! jason 389: u_int32_t id, len;
! 390: int handle, fd, ret, status = SSH2_FX_FAILURE;
1.1 markus 391: u_int64_t off;
392:
393: id = get_int();
394: handle = get_handle();
1.6.4.2 ! jason 395: off = get_int64();
1.1 markus 396: len = get_int();
397:
1.6.4.2 ! jason 398: TRACE("read id %d handle %d off %llu len %d", id, handle,
! 399: (unsigned long long)off, len);
1.1 markus 400: if (len > sizeof buf) {
401: len = sizeof buf;
402: log("read change len %d", len);
403: }
404: fd = handle_to_fd(handle);
405: if (fd >= 0) {
406: if (lseek(fd, off, SEEK_SET) < 0) {
407: error("process_read: seek failed");
408: status = errno_to_portable(errno);
409: } else {
410: ret = read(fd, buf, len);
411: if (ret < 0) {
412: status = errno_to_portable(errno);
413: } else if (ret == 0) {
1.6.4.2 ! jason 414: status = SSH2_FX_EOF;
1.1 markus 415: } else {
416: send_data(id, buf, ret);
1.6.4.2 ! jason 417: status = SSH2_FX_OK;
1.1 markus 418: }
419: }
420: }
1.6.4.2 ! jason 421: if (status != SSH2_FX_OK)
1.1 markus 422: send_status(id, status);
423: }
424:
425: void
426: process_write(void)
427: {
1.6.4.2 ! jason 428: u_int32_t id;
1.1 markus 429: u_int64_t off;
1.5 markus 430: u_int len;
1.6.4.2 ! jason 431: int handle, fd, ret, status = SSH2_FX_FAILURE;
1.1 markus 432: char *data;
433:
434: id = get_int();
435: handle = get_handle();
1.6.4.2 ! jason 436: off = get_int64();
1.1 markus 437: data = get_string(&len);
438:
1.6.4.2 ! jason 439: TRACE("write id %d handle %d off %llu len %d", id, handle,
! 440: (unsigned long long)off, len);
1.1 markus 441: fd = handle_to_fd(handle);
442: if (fd >= 0) {
443: if (lseek(fd, off, SEEK_SET) < 0) {
444: status = errno_to_portable(errno);
445: error("process_write: seek failed");
446: } else {
447: /* XXX ATOMICIO ? */
448: ret = write(fd, data, len);
449: if (ret == -1) {
450: error("process_write: write failed");
451: status = errno_to_portable(errno);
452: } else if (ret == len) {
1.6.4.2 ! jason 453: status = SSH2_FX_OK;
1.1 markus 454: } else {
455: log("nothing at all written");
456: }
457: }
458: }
459: send_status(id, status);
460: xfree(data);
461: }
462:
463: void
464: process_do_stat(int do_lstat)
465: {
1.6.4.2 ! jason 466: Attrib a;
1.1 markus 467: struct stat st;
468: u_int32_t id;
469: char *name;
1.6.4.2 ! jason 470: int ret, status = SSH2_FX_FAILURE;
1.1 markus 471:
472: id = get_int();
473: name = get_string(NULL);
474: TRACE("%sstat id %d name %s", do_lstat ? "l" : "", id, name);
475: ret = do_lstat ? lstat(name, &st) : stat(name, &st);
476: if (ret < 0) {
477: status = errno_to_portable(errno);
478: } else {
1.6.4.2 ! jason 479: stat_to_attrib(&st, &a);
! 480: send_attrib(id, &a);
! 481: status = SSH2_FX_OK;
1.1 markus 482: }
1.6.4.2 ! jason 483: if (status != SSH2_FX_OK)
1.1 markus 484: send_status(id, status);
485: xfree(name);
486: }
487:
488: void
489: process_stat(void)
490: {
491: process_do_stat(0);
492: }
493:
494: void
495: process_lstat(void)
496: {
497: process_do_stat(1);
498: }
499:
500: void
501: process_fstat(void)
502: {
1.6.4.2 ! jason 503: Attrib a;
1.1 markus 504: struct stat st;
505: u_int32_t id;
1.6.4.2 ! jason 506: int fd, ret, handle, status = SSH2_FX_FAILURE;
1.1 markus 507:
508: id = get_int();
509: handle = get_handle();
510: TRACE("fstat id %d handle %d", id, handle);
511: fd = handle_to_fd(handle);
512: if (fd >= 0) {
513: ret = fstat(fd, &st);
514: if (ret < 0) {
515: status = errno_to_portable(errno);
516: } else {
1.6.4.2 ! jason 517: stat_to_attrib(&st, &a);
! 518: send_attrib(id, &a);
! 519: status = SSH2_FX_OK;
1.1 markus 520: }
521: }
1.6.4.2 ! jason 522: if (status != SSH2_FX_OK)
1.1 markus 523: send_status(id, status);
524: }
525:
526: struct timeval *
527: attrib_to_tv(Attrib *a)
528: {
529: static struct timeval tv[2];
530: tv[0].tv_sec = a->atime;
531: tv[0].tv_usec = 0;
532: tv[1].tv_sec = a->mtime;
533: tv[1].tv_usec = 0;
534: return tv;
535: }
536:
537: void
538: process_setstat(void)
539: {
540: Attrib *a;
541: u_int32_t id;
542: char *name;
543: int ret;
1.6.4.2 ! jason 544: int status = SSH2_FX_OK;
1.1 markus 545:
546: id = get_int();
547: name = get_string(NULL);
548: a = get_attrib();
549: TRACE("setstat id %d name %s", id, name);
1.6.4.2 ! jason 550: if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1.1 markus 551: ret = chmod(name, a->perm & 0777);
552: if (ret == -1)
553: status = errno_to_portable(errno);
554: }
1.6.4.2 ! jason 555: if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1.1 markus 556: ret = utimes(name, attrib_to_tv(a));
557: if (ret == -1)
558: status = errno_to_portable(errno);
559: }
1.6.4.2 ! jason 560: if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
! 561: ret = chown(name, a->uid, a->gid);
! 562: if (ret == -1)
! 563: status = errno_to_portable(errno);
! 564: }
1.1 markus 565: send_status(id, status);
566: xfree(name);
567: }
568:
569: void
570: process_fsetstat(void)
571: {
572: Attrib *a;
573: u_int32_t id;
574: int handle, fd, ret;
1.6.4.2 ! jason 575: int status = SSH2_FX_OK;
1.1 markus 576:
577: id = get_int();
578: handle = get_handle();
579: a = get_attrib();
580: TRACE("fsetstat id %d handle %d", id, handle);
581: fd = handle_to_fd(handle);
582: if (fd < 0) {
1.6.4.2 ! jason 583: status = SSH2_FX_FAILURE;
1.1 markus 584: } else {
1.6.4.2 ! jason 585: if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
1.1 markus 586: ret = fchmod(fd, a->perm & 0777);
587: if (ret == -1)
588: status = errno_to_portable(errno);
589: }
1.6.4.2 ! jason 590: if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1.1 markus 591: ret = futimes(fd, attrib_to_tv(a));
592: if (ret == -1)
593: status = errno_to_portable(errno);
594: }
1.6.4.2 ! jason 595: if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
! 596: ret = fchown(fd, a->uid, a->gid);
! 597: if (ret == -1)
! 598: status = errno_to_portable(errno);
! 599: }
1.1 markus 600: }
601: send_status(id, status);
602: }
603:
604: void
605: process_opendir(void)
606: {
607: DIR *dirp = NULL;
608: char *path;
1.6.4.2 ! jason 609: int handle, status = SSH2_FX_FAILURE;
1.1 markus 610: u_int32_t id;
611:
612: id = get_int();
613: path = get_string(NULL);
614: TRACE("opendir id %d path %s", id, path);
1.6.4.2 ! jason 615: dirp = opendir(path);
1.1 markus 616: if (dirp == NULL) {
617: status = errno_to_portable(errno);
618: } else {
619: handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
620: if (handle < 0) {
621: closedir(dirp);
622: } else {
623: send_handle(id, handle);
1.6.4.2 ! jason 624: status = SSH2_FX_OK;
1.1 markus 625: }
1.6.4.2 ! jason 626:
1.1 markus 627: }
1.6.4.2 ! jason 628: if (status != SSH2_FX_OK)
1.1 markus 629: send_status(id, status);
630: xfree(path);
631: }
632:
1.6.4.2 ! jason 633: /*
! 634: * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
! 635: */
1.1 markus 636: char *
637: ls_file(char *name, struct stat *st)
638: {
1.6.4.2 ! jason 639: int sz = 0;
! 640: struct passwd *pw;
! 641: struct group *gr;
! 642: struct tm *ltime = localtime(&st->st_mtime);
! 643: char *user, *group;
! 644: char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
! 645:
! 646: strmode(st->st_mode, mode);
! 647: if ((pw = getpwuid(st->st_uid)) != NULL) {
! 648: user = pw->pw_name;
! 649: } else {
! 650: snprintf(ubuf, sizeof ubuf, "%d", st->st_uid);
! 651: user = ubuf;
! 652: }
! 653: if ((gr = getgrgid(st->st_gid)) != NULL) {
! 654: group = gr->gr_name;
! 655: } else {
! 656: snprintf(gbuf, sizeof gbuf, "%d", st->st_gid);
! 657: group = gbuf;
! 658: }
! 659: if (ltime != NULL) {
! 660: if (time(NULL) - st->st_mtime < (365*24*60*60)/2)
! 661: sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
! 662: else
! 663: sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime);
! 664: }
! 665: if (sz == 0)
! 666: tbuf[0] = '\0';
! 667: snprintf(buf, sizeof buf, "%s %3d %-8.8s %-8.8s %8llu %s %s", mode,
! 668: st->st_nlink, user, group, (unsigned long long)st->st_size, tbuf, name);
1.1 markus 669: return xstrdup(buf);
670: }
671:
672: void
673: process_readdir(void)
674: {
675: DIR *dirp;
676: struct dirent *dp;
677: char *path;
678: int handle;
679: u_int32_t id;
680:
681: id = get_int();
682: handle = get_handle();
683: TRACE("readdir id %d handle %d", id, handle);
684: dirp = handle_to_dir(handle);
685: path = handle_to_name(handle);
686: if (dirp == NULL || path == NULL) {
1.6.4.2 ! jason 687: send_status(id, SSH2_FX_FAILURE);
1.1 markus 688: } else {
689: struct stat st;
690: char pathname[1024];
691: Stat *stats;
692: int nstats = 10, count = 0, i;
693: stats = xmalloc(nstats * sizeof(Stat));
694: while ((dp = readdir(dirp)) != NULL) {
695: if (count >= nstats) {
696: nstats *= 2;
697: stats = xrealloc(stats, nstats * sizeof(Stat));
698: }
699: /* XXX OVERFLOW ? */
700: snprintf(pathname, sizeof pathname,
701: "%s/%s", path, dp->d_name);
702: if (lstat(pathname, &st) < 0)
703: continue;
1.6.4.2 ! jason 704: stat_to_attrib(&st, &(stats[count].attrib));
1.1 markus 705: stats[count].name = xstrdup(dp->d_name);
706: stats[count].long_name = ls_file(dp->d_name, &st);
707: count++;
708: /* send up to 100 entries in one message */
1.6.4.2 ! jason 709: /* XXX check packet size instead */
1.1 markus 710: if (count == 100)
711: break;
712: }
1.6.4.2 ! jason 713: if (count > 0) {
! 714: send_names(id, count, stats);
! 715: for(i = 0; i < count; i++) {
! 716: xfree(stats[i].name);
! 717: xfree(stats[i].long_name);
! 718: }
! 719: } else {
! 720: send_status(id, SSH2_FX_EOF);
1.1 markus 721: }
722: xfree(stats);
723: }
724: }
725:
726: void
727: process_remove(void)
728: {
729: char *name;
730: u_int32_t id;
1.6.4.2 ! jason 731: int status = SSH2_FX_FAILURE;
1.1 markus 732: int ret;
733:
734: id = get_int();
735: name = get_string(NULL);
736: TRACE("remove id %d name %s", id, name);
1.6.4.2 ! jason 737: ret = unlink(name);
! 738: status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.1 markus 739: send_status(id, status);
740: xfree(name);
741: }
742:
743: void
744: process_mkdir(void)
745: {
746: Attrib *a;
747: u_int32_t id;
748: char *name;
1.6.4.2 ! jason 749: int ret, mode, status = SSH2_FX_FAILURE;
1.1 markus 750:
751: id = get_int();
752: name = get_string(NULL);
753: a = get_attrib();
1.6.4.2 ! jason 754: mode = (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) ?
! 755: a->perm & 0777 : 0777;
1.1 markus 756: TRACE("mkdir id %d name %s mode 0%o", id, name, mode);
757: ret = mkdir(name, mode);
1.6.4.2 ! jason 758: status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.1 markus 759: send_status(id, status);
760: xfree(name);
761: }
762:
763: void
764: process_rmdir(void)
765: {
766: u_int32_t id;
767: char *name;
768: int ret, status;
769:
770: id = get_int();
771: name = get_string(NULL);
772: TRACE("rmdir id %d name %s", id, name);
773: ret = rmdir(name);
1.6.4.2 ! jason 774: status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
1.1 markus 775: send_status(id, status);
776: xfree(name);
777: }
778:
779: void
780: process_realpath(void)
781: {
782: char resolvedname[MAXPATHLEN];
783: u_int32_t id;
784: char *path;
785:
786: id = get_int();
787: path = get_string(NULL);
1.6.4.2 ! jason 788: if (path[0] == '\0') {
! 789: xfree(path);
! 790: path = xstrdup(".");
! 791: }
1.1 markus 792: TRACE("realpath id %d path %s", id, path);
793: if (realpath(path, resolvedname) == NULL) {
794: send_status(id, errno_to_portable(errno));
795: } else {
796: Stat s;
797: attrib_clear(&s.attrib);
798: s.name = s.long_name = resolvedname;
799: send_names(id, 1, &s);
800: }
801: xfree(path);
802: }
803:
804: void
805: process_rename(void)
806: {
807: u_int32_t id;
1.6.4.2 ! jason 808: struct stat st;
1.1 markus 809: char *oldpath, *newpath;
1.6.4.2 ! jason 810: int ret, status = SSH2_FX_FAILURE;
1.1 markus 811:
812: id = get_int();
813: oldpath = get_string(NULL);
814: newpath = get_string(NULL);
815: TRACE("rename id %d old %s new %s", id, oldpath, newpath);
1.6.4.2 ! jason 816: /* fail if 'newpath' exists */
! 817: if (stat(newpath, &st) == -1) {
! 818: ret = rename(oldpath, newpath);
! 819: status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
! 820: }
1.1 markus 821: send_status(id, status);
822: xfree(oldpath);
823: xfree(newpath);
824: }
825:
1.6.4.2 ! jason 826: void
! 827: process_extended(void)
! 828: {
! 829: u_int32_t id;
! 830: char *request;
! 831:
! 832: id = get_int();
! 833: request = get_string(NULL);
! 834: send_status(id, SSH2_FX_OP_UNSUPPORTED); /* MUST */
! 835: xfree(request);
! 836: }
1.1 markus 837:
838: /* stolen from ssh-agent */
839:
840: void
841: process(void)
842: {
1.6.4.2 ! jason 843: u_int msg_len;
! 844: u_int type;
! 845: u_char *cp;
1.1 markus 846:
847: if (buffer_len(&iqueue) < 5)
848: return; /* Incomplete message. */
1.6.4.2 ! jason 849: cp = (u_char *) buffer_ptr(&iqueue);
1.1 markus 850: msg_len = GET_32BIT(cp);
851: if (msg_len > 256 * 1024) {
852: error("bad message ");
853: exit(11);
854: }
855: if (buffer_len(&iqueue) < msg_len + 4)
856: return;
857: buffer_consume(&iqueue, 4);
858: type = buffer_get_char(&iqueue);
859: switch (type) {
1.6.4.2 ! jason 860: case SSH2_FXP_INIT:
1.1 markus 861: process_init();
862: break;
1.6.4.2 ! jason 863: case SSH2_FXP_OPEN:
1.1 markus 864: process_open();
865: break;
1.6.4.2 ! jason 866: case SSH2_FXP_CLOSE:
1.1 markus 867: process_close();
868: break;
1.6.4.2 ! jason 869: case SSH2_FXP_READ:
1.1 markus 870: process_read();
871: break;
1.6.4.2 ! jason 872: case SSH2_FXP_WRITE:
1.1 markus 873: process_write();
874: break;
1.6.4.2 ! jason 875: case SSH2_FXP_LSTAT:
1.1 markus 876: process_lstat();
877: break;
1.6.4.2 ! jason 878: case SSH2_FXP_FSTAT:
1.1 markus 879: process_fstat();
880: break;
1.6.4.2 ! jason 881: case SSH2_FXP_SETSTAT:
1.1 markus 882: process_setstat();
883: break;
1.6.4.2 ! jason 884: case SSH2_FXP_FSETSTAT:
1.1 markus 885: process_fsetstat();
886: break;
1.6.4.2 ! jason 887: case SSH2_FXP_OPENDIR:
1.1 markus 888: process_opendir();
889: break;
1.6.4.2 ! jason 890: case SSH2_FXP_READDIR:
1.1 markus 891: process_readdir();
892: break;
1.6.4.2 ! jason 893: case SSH2_FXP_REMOVE:
1.1 markus 894: process_remove();
895: break;
1.6.4.2 ! jason 896: case SSH2_FXP_MKDIR:
1.1 markus 897: process_mkdir();
898: break;
1.6.4.2 ! jason 899: case SSH2_FXP_RMDIR:
1.1 markus 900: process_rmdir();
901: break;
1.6.4.2 ! jason 902: case SSH2_FXP_REALPATH:
1.1 markus 903: process_realpath();
904: break;
1.6.4.2 ! jason 905: case SSH2_FXP_STAT:
1.1 markus 906: process_stat();
907: break;
1.6.4.2 ! jason 908: case SSH2_FXP_RENAME:
1.1 markus 909: process_rename();
910: break;
1.6.4.2 ! jason 911: case SSH2_FXP_EXTENDED:
! 912: process_extended();
! 913: break;
1.1 markus 914: default:
915: error("Unknown message %d", type);
916: break;
917: }
918: }
919:
920: int
921: main(int ac, char **av)
922: {
923: fd_set rset, wset;
924: int in, out, max;
1.5 markus 925: ssize_t len, olen;
1.1 markus 926:
927: handle_init();
1.6.4.2 ! jason 928:
! 929: #ifdef DEBUG_SFTP_SERVER
! 930: log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
! 931: #endif
1.1 markus 932:
933: in = dup(STDIN_FILENO);
934: out = dup(STDOUT_FILENO);
935:
936: max = 0;
937: if (in > max)
938: max = in;
939: if (out > max)
940: max = out;
941:
942: buffer_init(&iqueue);
943: buffer_init(&oqueue);
944:
945: for (;;) {
946: FD_ZERO(&rset);
947: FD_ZERO(&wset);
948:
949: FD_SET(in, &rset);
950: olen = buffer_len(&oqueue);
951: if (olen > 0)
952: FD_SET(out, &wset);
953:
954: if (select(max+1, &rset, &wset, NULL, NULL) < 0) {
955: if (errno == EINTR)
956: continue;
957: exit(2);
958: }
959:
960: /* copy stdin to iqueue */
961: if (FD_ISSET(in, &rset)) {
962: char buf[4*4096];
963: len = read(in, buf, sizeof buf);
964: if (len == 0) {
965: debug("read eof");
966: exit(0);
967: } else if (len < 0) {
968: error("read error");
969: exit(1);
970: } else {
971: buffer_append(&iqueue, buf, len);
972: }
973: }
974: /* send oqueue to stdout */
975: if (FD_ISSET(out, &wset)) {
976: len = write(out, buffer_ptr(&oqueue), olen);
977: if (len < 0) {
978: error("write error");
979: exit(1);
980: } else {
981: buffer_consume(&oqueue, len);
982: }
983: }
984: /* process requests from client */
985: process();
986: }
987: }