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