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