Annotation of src/usr.bin/ssh/sftp-int.c, Revision 1.43
1.1 djm 1: /*
2: * Copyright (c) 2001 Damien Miller. 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:
1.12 djm 25: /* XXX: globbed ls */
1.1 djm 26: /* XXX: recursive operations */
27:
28: #include "includes.h"
1.43 ! djm 29: RCSID("$OpenBSD: sftp-int.c,v 1.42 2002/02/05 00:00:46 djm Exp $");
1.27 djm 30:
31: #include <glob.h>
1.1 djm 32:
33: #include "buffer.h"
34: #include "xmalloc.h"
35: #include "log.h"
36: #include "pathnames.h"
37:
38: #include "sftp.h"
39: #include "sftp-common.h"
1.27 djm 40: #include "sftp-glob.h"
1.1 djm 41: #include "sftp-client.h"
42: #include "sftp-int.h"
43:
1.26 djm 44: /* File to read commands from */
45: extern FILE *infile;
46:
1.42 djm 47: /* Size of buffer used when copying files */
48: extern size_t copy_buffer_len;
49:
1.43 ! djm 50: /* Number of concurrent outstanding requests */
! 51: extern int num_requests;
! 52:
1.26 djm 53: /* Version of server we are speaking to */
54: int version;
1.25 deraadt 55:
1.1 djm 56: /* Seperators for interactive commands */
57: #define WHITESPACE " \t\r\n"
58:
59: /* Commands for interactive mode */
60: #define I_CHDIR 1
61: #define I_CHGRP 2
62: #define I_CHMOD 3
63: #define I_CHOWN 4
64: #define I_GET 5
65: #define I_HELP 6
66: #define I_LCHDIR 7
67: #define I_LLS 8
68: #define I_LMKDIR 9
69: #define I_LPWD 10
70: #define I_LS 11
71: #define I_LUMASK 12
72: #define I_MKDIR 13
73: #define I_PUT 14
74: #define I_PWD 15
75: #define I_QUIT 16
76: #define I_RENAME 17
77: #define I_RM 18
78: #define I_RMDIR 19
79: #define I_SHELL 20
1.26 djm 80: #define I_SYMLINK 21
1.28 markus 81: #define I_VERSION 22
1.1 djm 82:
83: struct CMD {
1.6 deraadt 84: const char *c;
1.1 djm 85: const int n;
86: };
87:
88: const struct CMD cmds[] = {
1.40 markus 89: { "bye", I_QUIT },
1.15 stevesk 90: { "cd", I_CHDIR },
91: { "chdir", I_CHDIR },
92: { "chgrp", I_CHGRP },
93: { "chmod", I_CHMOD },
94: { "chown", I_CHOWN },
95: { "dir", I_LS },
96: { "exit", I_QUIT },
97: { "get", I_GET },
1.34 djm 98: { "mget", I_GET },
1.15 stevesk 99: { "help", I_HELP },
100: { "lcd", I_LCHDIR },
101: { "lchdir", I_LCHDIR },
102: { "lls", I_LLS },
103: { "lmkdir", I_LMKDIR },
1.26 djm 104: { "ln", I_SYMLINK },
1.15 stevesk 105: { "lpwd", I_LPWD },
106: { "ls", I_LS },
107: { "lumask", I_LUMASK },
108: { "mkdir", I_MKDIR },
109: { "put", I_PUT },
1.34 djm 110: { "mput", I_PUT },
1.15 stevesk 111: { "pwd", I_PWD },
112: { "quit", I_QUIT },
113: { "rename", I_RENAME },
114: { "rm", I_RM },
115: { "rmdir", I_RMDIR },
1.26 djm 116: { "symlink", I_SYMLINK },
1.28 markus 117: { "version", I_VERSION },
1.6 deraadt 118: { "!", I_SHELL },
1.7 deraadt 119: { "?", I_HELP },
1.6 deraadt 120: { NULL, -1}
1.1 djm 121: };
122:
1.37 itojun 123: static void
1.1 djm 124: help(void)
125: {
126: printf("Available commands:\n");
1.13 stevesk 127: printf("cd path Change remote directory to 'path'\n");
128: printf("lcd path Change local directory to 'path'\n");
129: printf("chgrp grp path Change group of file 'path' to 'grp'\n");
130: printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
131: printf("chown own path Change owner of file 'path' to 'own'\n");
132: printf("help Display this help text\n");
133: printf("get remote-path [local-path] Download file\n");
134: printf("lls [ls-options [path]] Display local directory listing\n");
1.26 djm 135: printf("ln oldpath newpath Symlink remote file\n");
1.13 stevesk 136: printf("lmkdir path Create local directory\n");
137: printf("lpwd Print local working directory\n");
138: printf("ls [path] Display remote directory listing\n");
139: printf("lumask umask Set local umask to 'umask'\n");
140: printf("mkdir path Create remote directory\n");
141: printf("put local-path [remote-path] Upload file\n");
142: printf("pwd Display remote working directory\n");
143: printf("exit Quit sftp\n");
144: printf("quit Quit sftp\n");
145: printf("rename oldpath newpath Rename remote file\n");
146: printf("rmdir path Remove remote directory\n");
147: printf("rm path Delete remote file\n");
1.26 djm 148: printf("symlink oldpath newpath Symlink remote file\n");
1.28 markus 149: printf("version Show SFTP version\n");
1.1 djm 150: printf("!command Execute 'command' in local shell\n");
151: printf("! Escape to local shell\n");
1.13 stevesk 152: printf("? Synonym for help\n");
1.1 djm 153: }
154:
1.37 itojun 155: static void
1.1 djm 156: local_do_shell(const char *args)
157: {
1.36 markus 158: int status;
1.1 djm 159: char *shell;
160: pid_t pid;
1.3 stevesk 161:
1.1 djm 162: if (!*args)
163: args = NULL;
1.3 stevesk 164:
1.1 djm 165: if ((shell = getenv("SHELL")) == NULL)
166: shell = _PATH_BSHELL;
167:
168: if ((pid = fork()) == -1)
169: fatal("Couldn't fork: %s", strerror(errno));
170:
171: if (pid == 0) {
172: /* XXX: child has pipe fds to ssh subproc open - issue? */
173: if (args) {
174: debug3("Executing %s -c \"%s\"", shell, args);
1.38 deraadt 175: execl(shell, shell, "-c", args, (char *)NULL);
1.1 djm 176: } else {
177: debug3("Executing %s", shell);
1.38 deraadt 178: execl(shell, shell, (char *)NULL);
1.1 djm 179: }
1.3 stevesk 180: fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
1.1 djm 181: strerror(errno));
182: _exit(1);
183: }
184: if (waitpid(pid, &status, 0) == -1)
185: fatal("Couldn't wait for child: %s", strerror(errno));
186: if (!WIFEXITED(status))
187: error("Shell exited abormally");
188: else if (WEXITSTATUS(status))
189: error("Shell exited with status %d", WEXITSTATUS(status));
190: }
191:
1.37 itojun 192: static void
1.1 djm 193: local_do_ls(const char *args)
194: {
195: if (!args || !*args)
1.18 stevesk 196: local_do_shell(_PATH_LS);
1.1 djm 197: else {
1.18 stevesk 198: int len = strlen(_PATH_LS " ") + strlen(args) + 1;
1.16 deraadt 199: char *buf = xmalloc(len);
1.1 djm 200:
201: /* XXX: quoting - rip quoting code from ftp? */
1.18 stevesk 202: snprintf(buf, len, _PATH_LS " %s", args);
1.1 djm 203: local_do_shell(buf);
1.16 deraadt 204: xfree(buf);
1.1 djm 205: }
206: }
207:
1.37 itojun 208: static char *
1.29 djm 209: path_append(char *p1, char *p2)
210: {
211: char *ret;
1.31 markus 212: int len = strlen(p1) + strlen(p2) + 2;
1.29 djm 213:
1.31 markus 214: ret = xmalloc(len);
215: strlcpy(ret, p1, len);
1.41 deraadt 216: if (strcmp(p1, "/") != 0)
1.39 jakob 217: strlcat(ret, "/", len);
1.31 markus 218: strlcat(ret, p2, len);
1.29 djm 219:
220: return(ret);
221: }
222:
1.37 itojun 223: static char *
1.1 djm 224: make_absolute(char *p, char *pwd)
225: {
1.29 djm 226: char *abs;
1.1 djm 227:
228: /* Derelativise */
229: if (p && p[0] != '/') {
1.29 djm 230: abs = path_append(pwd, p);
1.1 djm 231: xfree(p);
1.29 djm 232: return(abs);
233: } else
234: return(p);
235: }
236:
1.37 itojun 237: static int
1.29 djm 238: infer_path(const char *p, char **ifp)
239: {
240: char *cp;
241:
242: cp = strrchr(p, '/');
243: if (cp == NULL) {
244: *ifp = xstrdup(p);
245: return(0);
1.1 djm 246: }
247:
1.29 djm 248: if (!cp[1]) {
249: error("Invalid path");
250: return(-1);
251: }
252:
253: *ifp = xstrdup(cp + 1);
254: return(0);
1.1 djm 255: }
256:
1.37 itojun 257: static int
1.1 djm 258: parse_getput_flags(const char **cpp, int *pflag)
259: {
260: const char *cp = *cpp;
261:
262: /* Check for flags */
263: if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
1.20 djm 264: switch (cp[1]) {
1.22 djm 265: case 'p':
1.1 djm 266: case 'P':
267: *pflag = 1;
268: break;
269: default:
1.22 djm 270: error("Invalid flag -%c", cp[1]);
1.1 djm 271: return(-1);
272: }
273: cp += 2;
274: *cpp = cp + strspn(cp, WHITESPACE);
275: }
276:
277: return(0);
278: }
279:
1.37 itojun 280: static int
1.1 djm 281: get_pathname(const char **cpp, char **path)
282: {
1.8 provos 283: const char *cp = *cpp, *end;
284: char quot;
1.1 djm 285: int i;
286:
287: cp += strspn(cp, WHITESPACE);
288: if (!*cp) {
289: *cpp = cp;
290: *path = NULL;
1.8 provos 291: return (0);
1.1 djm 292: }
293:
294: /* Check for quoted filenames */
295: if (*cp == '\"' || *cp == '\'') {
1.8 provos 296: quot = *cp++;
1.30 markus 297:
1.8 provos 298: end = strchr(cp, quot);
299: if (end == NULL) {
1.1 djm 300: error("Unterminated quote");
1.8 provos 301: goto fail;
1.1 djm 302: }
1.8 provos 303: if (cp == end) {
1.1 djm 304: error("Empty quotes");
1.8 provos 305: goto fail;
1.1 djm 306: }
1.8 provos 307: *cpp = end + 1 + strspn(end + 1, WHITESPACE);
308: } else {
309: /* Read to end of filename */
310: end = strpbrk(cp, WHITESPACE);
311: if (end == NULL)
312: end = strchr(cp, '\0');
313: *cpp = end + strspn(end, WHITESPACE);
1.1 djm 314: }
315:
1.8 provos 316: i = end - cp;
1.1 djm 317:
318: *path = xmalloc(i + 1);
319: memcpy(*path, cp, i);
320: (*path)[i] = '\0';
321: return(0);
1.8 provos 322:
323: fail:
324: *path = NULL;
325: return (-1);
1.1 djm 326: }
327:
1.37 itojun 328: static int
1.29 djm 329: is_dir(char *path)
330: {
331: struct stat sb;
332:
333: /* XXX: report errors? */
334: if (stat(path, &sb) == -1)
335: return(0);
336:
337: return(sb.st_mode & S_IFDIR);
338: }
339:
1.37 itojun 340: static int
1.29 djm 341: remote_is_dir(int in, int out, char *path)
1.1 djm 342: {
1.29 djm 343: Attrib *a;
1.1 djm 344:
1.29 djm 345: /* XXX: report errors? */
346: if ((a = do_stat(in, out, path, 1)) == NULL)
347: return(0);
348: if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
1.1 djm 349: return(0);
1.29 djm 350: return(a->perm & S_IFDIR);
351: }
352:
1.37 itojun 353: static int
1.29 djm 354: process_get(int in, int out, char *src, char *dst, char *pwd, int pflag)
355: {
356: char *abs_src = NULL;
357: char *abs_dst = NULL;
358: char *tmp;
359: glob_t g;
360: int err = 0;
361: int i;
1.30 markus 362:
1.29 djm 363: abs_src = xstrdup(src);
364: abs_src = make_absolute(abs_src, pwd);
365:
1.30 markus 366: memset(&g, 0, sizeof(g));
1.29 djm 367: debug3("Looking up %s", abs_src);
368: if (remote_glob(in, out, abs_src, 0, NULL, &g)) {
369: error("File \"%s\" not found.", abs_src);
370: err = -1;
371: goto out;
372: }
373:
374: /* Only one match, dst may be file, directory or unspecified */
375: if (g.gl_pathv[0] && g.gl_matchc == 1) {
376: if (dst) {
377: /* If directory specified, append filename */
378: if (is_dir(dst)) {
379: if (infer_path(g.gl_pathv[0], &tmp)) {
380: err = 1;
381: goto out;
382: }
383: abs_dst = path_append(dst, tmp);
384: xfree(tmp);
385: } else
386: abs_dst = xstrdup(dst);
387: } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
388: err = -1;
389: goto out;
390: }
391: printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
1.42 djm 392: err = do_download(in, out, g.gl_pathv[0], abs_dst, pflag,
1.43 ! djm 393: copy_buffer_len, num_requests);
1.29 djm 394: goto out;
395: }
396:
397: /* Multiple matches, dst may be directory or unspecified */
398: if (dst && !is_dir(dst)) {
1.30 markus 399: error("Multiple files match, but \"%s\" is not a directory",
1.29 djm 400: dst);
401: err = -1;
402: goto out;
403: }
1.30 markus 404:
1.41 deraadt 405: for (i = 0; g.gl_pathv[i]; i++) {
1.29 djm 406: if (infer_path(g.gl_pathv[i], &tmp)) {
407: err = -1;
408: goto out;
409: }
410: if (dst) {
411: abs_dst = path_append(dst, tmp);
412: xfree(tmp);
413: } else
414: abs_dst = tmp;
415:
416: printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
1.42 djm 417: if (do_download(in, out, g.gl_pathv[i], abs_dst, pflag,
1.43 ! djm 418: copy_buffer_len, num_requests) == -1)
1.29 djm 419: err = -1;
420: xfree(abs_dst);
421: abs_dst = NULL;
1.1 djm 422: }
423:
1.29 djm 424: out:
425: xfree(abs_src);
426: if (abs_dst)
427: xfree(abs_dst);
428: globfree(&g);
429: return(err);
430: }
431:
1.37 itojun 432: static int
1.29 djm 433: process_put(int in, int out, char *src, char *dst, char *pwd, int pflag)
434: {
435: char *tmp_dst = NULL;
436: char *abs_dst = NULL;
437: char *tmp;
438: glob_t g;
439: int err = 0;
440: int i;
441:
442: if (dst) {
443: tmp_dst = xstrdup(dst);
444: tmp_dst = make_absolute(tmp_dst, pwd);
445: }
446:
1.30 markus 447: memset(&g, 0, sizeof(g));
1.29 djm 448: debug3("Looking up %s", src);
449: if (glob(src, 0, NULL, &g)) {
450: error("File \"%s\" not found.", src);
451: err = -1;
452: goto out;
453: }
454:
455: /* Only one match, dst may be file, directory or unspecified */
456: if (g.gl_pathv[0] && g.gl_matchc == 1) {
457: if (tmp_dst) {
458: /* If directory specified, append filename */
459: if (remote_is_dir(in, out, tmp_dst)) {
460: if (infer_path(g.gl_pathv[0], &tmp)) {
461: err = 1;
462: goto out;
463: }
464: abs_dst = path_append(tmp_dst, tmp);
465: xfree(tmp);
466: } else
467: abs_dst = xstrdup(tmp_dst);
1.32 markus 468: } else {
469: if (infer_path(g.gl_pathv[0], &abs_dst)) {
470: err = -1;
471: goto out;
472: }
473: abs_dst = make_absolute(abs_dst, pwd);
1.29 djm 474: }
475: printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
1.42 djm 476: err = do_upload(in, out, g.gl_pathv[0], abs_dst, pflag,
1.43 ! djm 477: copy_buffer_len, num_requests);
1.29 djm 478: goto out;
479: }
480:
481: /* Multiple matches, dst may be directory or unspecified */
482: if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) {
1.30 markus 483: error("Multiple files match, but \"%s\" is not a directory",
1.29 djm 484: tmp_dst);
485: err = -1;
486: goto out;
487: }
488:
1.41 deraadt 489: for (i = 0; g.gl_pathv[i]; i++) {
1.29 djm 490: if (infer_path(g.gl_pathv[i], &tmp)) {
491: err = -1;
492: goto out;
493: }
494: if (tmp_dst) {
495: abs_dst = path_append(tmp_dst, tmp);
496: xfree(tmp);
497: } else
498: abs_dst = make_absolute(tmp, pwd);
499:
500: printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
1.42 djm 501: if (do_upload(in, out, g.gl_pathv[i], abs_dst, pflag,
1.43 ! djm 502: copy_buffer_len, num_requests) == -1)
1.29 djm 503: err = -1;
1.1 djm 504: }
505:
1.29 djm 506: out:
507: if (abs_dst)
508: xfree(abs_dst);
509: if (tmp_dst)
510: xfree(tmp_dst);
511: return(err);
1.1 djm 512: }
513:
1.37 itojun 514: static int
1.1 djm 515: parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
516: char **path1, char **path2)
517: {
518: const char *cmd, *cp = *cpp;
1.21 stevesk 519: char *cp2;
1.4 markus 520: int base = 0;
1.21 stevesk 521: long l;
1.1 djm 522: int i, cmdnum;
523:
524: /* Skip leading whitespace */
525: cp = cp + strspn(cp, WHITESPACE);
526:
527: /* Ignore blank lines */
528: if (!*cp)
529: return(-1);
530:
531: /* Figure out which command we have */
1.41 deraadt 532: for (i = 0; cmds[i].c; i++) {
1.1 djm 533: int cmdlen = strlen(cmds[i].c);
534:
535: /* Check for command followed by whitespace */
536: if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
537: strchr(WHITESPACE, cp[cmdlen])) {
538: cp += cmdlen;
539: cp = cp + strspn(cp, WHITESPACE);
540: break;
541: }
542: }
543: cmdnum = cmds[i].n;
544: cmd = cmds[i].c;
545:
546: /* Special case */
547: if (*cp == '!') {
548: cp++;
549: cmdnum = I_SHELL;
550: } else if (cmdnum == -1) {
551: error("Invalid command.");
552: return(-1);
553: }
554:
555: /* Get arguments and parse flags */
556: *pflag = *n_arg = 0;
557: *path1 = *path2 = NULL;
558: switch (cmdnum) {
559: case I_GET:
560: case I_PUT:
561: if (parse_getput_flags(&cp, pflag))
562: return(-1);
563: /* Get first pathname (mandatory) */
564: if (get_pathname(&cp, path1))
565: return(-1);
566: if (*path1 == NULL) {
567: error("You must specify at least one path after a "
568: "%s command.", cmd);
569: return(-1);
570: }
571: /* Try to get second pathname (optional) */
572: if (get_pathname(&cp, path2))
573: return(-1);
574: break;
575: case I_RENAME:
1.26 djm 576: case I_SYMLINK:
1.1 djm 577: if (get_pathname(&cp, path1))
578: return(-1);
579: if (get_pathname(&cp, path2))
580: return(-1);
581: if (!*path1 || !*path2) {
582: error("You must specify two paths after a %s "
583: "command.", cmd);
584: return(-1);
585: }
586: break;
587: case I_RM:
588: case I_MKDIR:
589: case I_RMDIR:
590: case I_CHDIR:
591: case I_LCHDIR:
592: case I_LMKDIR:
593: /* Get pathname (mandatory) */
594: if (get_pathname(&cp, path1))
595: return(-1);
596: if (*path1 == NULL) {
1.3 stevesk 597: error("You must specify a path after a %s command.",
1.1 djm 598: cmd);
599: return(-1);
600: }
601: break;
602: case I_LS:
603: /* Path is optional */
604: if (get_pathname(&cp, path1))
605: return(-1);
606: break;
607: case I_LLS:
608: case I_SHELL:
609: /* Uses the rest of the line */
610: break;
611: case I_LUMASK:
1.21 stevesk 612: base = 8;
1.1 djm 613: case I_CHMOD:
1.4 markus 614: base = 8;
1.1 djm 615: case I_CHOWN:
616: case I_CHGRP:
617: /* Get numeric arg (mandatory) */
1.21 stevesk 618: l = strtol(cp, &cp2, base);
619: if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
620: errno == ERANGE) || l < 0) {
1.1 djm 621: error("You must supply a numeric argument "
622: "to the %s command.", cmd);
623: return(-1);
624: }
1.21 stevesk 625: cp = cp2;
626: *n_arg = l;
627: if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
628: break;
629: if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
1.1 djm 630: error("You must supply a numeric argument "
631: "to the %s command.", cmd);
632: return(-1);
633: }
634: cp += strspn(cp, WHITESPACE);
635:
636: /* Get pathname (mandatory) */
637: if (get_pathname(&cp, path1))
638: return(-1);
639: if (*path1 == NULL) {
1.3 stevesk 640: error("You must specify a path after a %s command.",
1.1 djm 641: cmd);
642: return(-1);
643: }
644: break;
645: case I_QUIT:
646: case I_PWD:
647: case I_LPWD:
648: case I_HELP:
1.28 markus 649: case I_VERSION:
1.1 djm 650: break;
651: default:
652: fatal("Command not implemented");
653: }
654:
655: *cpp = cp;
656: return(cmdnum);
657: }
658:
1.37 itojun 659: static int
1.1 djm 660: parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
661: {
1.8 provos 662: char *path1, *path2, *tmp;
1.27 djm 663: int pflag, cmdnum, i;
1.1 djm 664: unsigned long n_arg;
665: Attrib a, *aa;
1.23 deraadt 666: char path_buf[MAXPATHLEN];
1.25 deraadt 667: int err = 0;
1.27 djm 668: glob_t g;
1.1 djm 669:
670: path1 = path2 = NULL;
671: cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
672:
1.29 djm 673: memset(&g, 0, sizeof(g));
674:
1.1 djm 675: /* Perform command */
676: switch (cmdnum) {
677: case -1:
678: break;
679: case I_GET:
1.29 djm 680: err = process_get(in, out, path1, path2, *pwd, pflag);
1.1 djm 681: break;
682: case I_PUT:
1.29 djm 683: err = process_put(in, out, path1, path2, *pwd, pflag);
1.33 markus 684: break;
685: case I_RENAME:
1.1 djm 686: path1 = make_absolute(path1, *pwd);
687: path2 = make_absolute(path2, *pwd);
1.25 deraadt 688: err = do_rename(in, out, path1, path2);
1.1 djm 689: break;
1.26 djm 690: case I_SYMLINK:
691: if (version < 3) {
692: error("The server (version %d) does not support "
693: "this operation", version);
694: err = -1;
695: } else {
696: path2 = make_absolute(path2, *pwd);
697: err = do_symlink(in, out, path1, path2);
698: }
699: break;
1.1 djm 700: case I_RM:
701: path1 = make_absolute(path1, *pwd);
1.27 djm 702: remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
1.41 deraadt 703: for (i = 0; g.gl_pathv[i]; i++) {
1.27 djm 704: printf("Removing %s\n", g.gl_pathv[i]);
705: if (do_rm(in, out, g.gl_pathv[i]) == -1)
706: err = -1;
707: }
1.1 djm 708: break;
709: case I_MKDIR:
710: path1 = make_absolute(path1, *pwd);
711: attrib_clear(&a);
712: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
713: a.perm = 0777;
1.25 deraadt 714: err = do_mkdir(in, out, path1, &a);
1.1 djm 715: break;
716: case I_RMDIR:
717: path1 = make_absolute(path1, *pwd);
1.25 deraadt 718: err = do_rmdir(in, out, path1);
1.1 djm 719: break;
720: case I_CHDIR:
721: path1 = make_absolute(path1, *pwd);
1.25 deraadt 722: if ((tmp = do_realpath(in, out, path1)) == NULL) {
723: err = 1;
1.11 markus 724: break;
1.25 deraadt 725: }
1.29 djm 726: if ((aa = do_stat(in, out, tmp, 0)) == NULL) {
1.11 markus 727: xfree(tmp);
1.25 deraadt 728: err = 1;
1.11 markus 729: break;
730: }
1.9 djm 731: if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
732: error("Can't change directory: Can't check target");
733: xfree(tmp);
1.25 deraadt 734: err = 1;
1.9 djm 735: break;
736: }
737: if (!S_ISDIR(aa->perm)) {
738: error("Can't change directory: \"%s\" is not "
739: "a directory", tmp);
740: xfree(tmp);
1.25 deraadt 741: err = 1;
1.9 djm 742: break;
743: }
1.11 markus 744: xfree(*pwd);
745: *pwd = tmp;
1.1 djm 746: break;
747: case I_LS:
1.12 djm 748: if (!path1) {
749: do_ls(in, out, *pwd);
750: break;
751: }
1.1 djm 752: path1 = make_absolute(path1, *pwd);
1.12 djm 753: if ((tmp = do_realpath(in, out, path1)) == NULL)
754: break;
755: xfree(path1);
756: path1 = tmp;
1.29 djm 757: if ((aa = do_stat(in, out, path1, 0)) == NULL)
1.12 djm 758: break;
1.30 markus 759: if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.12 djm 760: !S_ISDIR(aa->perm)) {
761: error("Can't ls: \"%s\" is not a directory", path1);
762: break;
763: }
764: do_ls(in, out, path1);
1.1 djm 765: break;
766: case I_LCHDIR:
1.25 deraadt 767: if (chdir(path1) == -1) {
1.1 djm 768: error("Couldn't change local directory to "
769: "\"%s\": %s", path1, strerror(errno));
1.25 deraadt 770: err = 1;
771: }
1.1 djm 772: break;
773: case I_LMKDIR:
1.25 deraadt 774: if (mkdir(path1, 0777) == -1) {
1.17 stevesk 775: error("Couldn't create local directory "
1.1 djm 776: "\"%s\": %s", path1, strerror(errno));
1.25 deraadt 777: err = 1;
778: }
1.1 djm 779: break;
780: case I_LLS:
781: local_do_ls(cmd);
782: break;
783: case I_SHELL:
784: local_do_shell(cmd);
785: break;
786: case I_LUMASK:
787: umask(n_arg);
1.21 stevesk 788: printf("Local umask: %03lo\n", n_arg);
1.1 djm 789: break;
790: case I_CHMOD:
791: path1 = make_absolute(path1, *pwd);
792: attrib_clear(&a);
793: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
794: a.perm = n_arg;
1.27 djm 795: remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
1.41 deraadt 796: for (i = 0; g.gl_pathv[i]; i++) {
1.27 djm 797: printf("Changing mode on %s\n", g.gl_pathv[i]);
798: do_setstat(in, out, g.gl_pathv[i], &a);
799: }
1.5 stevesk 800: break;
1.1 djm 801: case I_CHOWN:
802: path1 = make_absolute(path1, *pwd);
1.27 djm 803: remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
1.41 deraadt 804: for (i = 0; g.gl_pathv[i]; i++) {
1.29 djm 805: if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
1.27 djm 806: continue;
807: if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
808: error("Can't get current ownership of "
809: "remote file \"%s\"", g.gl_pathv[i]);
810: continue;
811: }
812: printf("Changing owner on %s\n", g.gl_pathv[i]);
813: aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
814: aa->uid = n_arg;
815: do_setstat(in, out, g.gl_pathv[i], aa);
1.1 djm 816: }
817: break;
818: case I_CHGRP:
819: path1 = make_absolute(path1, *pwd);
1.27 djm 820: remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
1.41 deraadt 821: for (i = 0; g.gl_pathv[i]; i++) {
1.29 djm 822: if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
1.27 djm 823: continue;
824: if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
825: error("Can't get current ownership of "
826: "remote file \"%s\"", g.gl_pathv[i]);
827: continue;
828: }
829: printf("Changing group on %s\n", g.gl_pathv[i]);
830: aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
831: aa->gid = n_arg;
832: do_setstat(in, out, g.gl_pathv[i], aa);
1.1 djm 833: }
834: break;
835: case I_PWD:
836: printf("Remote working directory: %s\n", *pwd);
837: break;
838: case I_LPWD:
839: if (!getcwd(path_buf, sizeof(path_buf)))
1.24 millert 840: error("Couldn't get local cwd: %s",
1.1 djm 841: strerror(errno));
842: else
843: printf("Local working directory: %s\n",
844: path_buf);
845: break;
846: case I_QUIT:
847: return(-1);
848: case I_HELP:
849: help();
1.28 markus 850: break;
851: case I_VERSION:
852: printf("SFTP protocol version %d\n", version);
1.1 djm 853: break;
854: default:
855: fatal("%d is not implemented", cmdnum);
856: }
857:
1.29 djm 858: if (g.gl_pathc)
859: globfree(&g);
1.1 djm 860: if (path1)
861: xfree(path1);
862: if (path2)
863: xfree(path2);
1.25 deraadt 864:
865: /* If an error occurs in batch mode we should abort. */
866: if (infile != stdin && err > 0)
867: return -1;
868:
1.1 djm 869: return(0);
870: }
871:
872: void
1.35 mouring 873: interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1.1 djm 874: {
875: char *pwd;
1.35 mouring 876: char *dir = NULL;
1.1 djm 877: char cmd[2048];
1.26 djm 878:
879: version = do_init(fd_in, fd_out);
880: if (version == -1)
881: fatal("Couldn't initialise connection to server");
1.1 djm 882:
883: pwd = do_realpath(fd_in, fd_out, ".");
884: if (pwd == NULL)
885: fatal("Need cwd");
886:
1.35 mouring 887: if (file1 != NULL) {
888: dir = xstrdup(file1);
889: dir = make_absolute(dir, pwd);
890:
891: if (remote_is_dir(fd_in, fd_out, dir) && file2 == NULL) {
892: printf("Changing to: %s\n", dir);
893: snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
894: parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
895: } else {
896: if (file2 == NULL)
897: snprintf(cmd, sizeof cmd, "get %s", dir);
898: else
899: snprintf(cmd, sizeof cmd, "get %s %s", dir,
900: file2);
901:
902: parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
903: return;
904: }
905: }
1.14 stevesk 906: setvbuf(stdout, NULL, _IOLBF, 0);
1.25 deraadt 907: setvbuf(infile, NULL, _IOLBF, 0);
1.1 djm 908:
1.41 deraadt 909: for (;;) {
1.1 djm 910: char *cp;
911:
912: printf("sftp> ");
913:
914: /* XXX: use libedit */
1.25 deraadt 915: if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1.1 djm 916: printf("\n");
917: break;
1.25 deraadt 918: } else if (infile != stdin) /* Bluff typing */
919: printf("%s", cmd);
920:
1.1 djm 921: cp = strrchr(cmd, '\n');
922: if (cp)
923: *cp = '\0';
1.25 deraadt 924:
1.1 djm 925: if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
926: break;
927: }
928: xfree(pwd);
929: }