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