Annotation of src/usr.bin/ssh/sftp-int.c, Revision 1.65
1.1 djm 1: /*
1.44 djm 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:
25: /* XXX: recursive operations */
26:
27: #include "includes.h"
1.65 ! djm 28: RCSID("$OpenBSD: sftp-int.c,v 1.64 2003/11/08 19:17:29 jmc Exp $");
1.27 djm 29:
30: #include <glob.h>
1.1 djm 31:
32: #include "buffer.h"
33: #include "xmalloc.h"
34: #include "log.h"
35: #include "pathnames.h"
36:
37: #include "sftp.h"
38: #include "sftp-common.h"
1.27 djm 39: #include "sftp-glob.h"
1.1 djm 40: #include "sftp-client.h"
41: #include "sftp-int.h"
42:
1.26 djm 43: /* File to read commands from */
44: extern FILE *infile;
45:
1.42 djm 46: /* Size of buffer used when copying files */
47: extern size_t copy_buffer_len;
48:
1.43 djm 49: /* Number of concurrent outstanding requests */
50: extern int num_requests;
51:
1.52 fgsch 52: /* This is set to 0 if the progressmeter is not desired. */
53: int showprogress = 1;
54:
1.64 jmc 55: /* Separators for interactive commands */
1.1 djm 56: #define WHITESPACE " \t\r\n"
57:
1.60 mouring 58: /* Define what type of ls view (0 - multi-column) */
59: #define LONG_VIEW 1 /* Full view ala ls -l */
60: #define SHORT_VIEW 2 /* Single row view ala ls -1 */
61:
1.1 djm 62: /* Commands for interactive mode */
63: #define I_CHDIR 1
64: #define I_CHGRP 2
65: #define I_CHMOD 3
66: #define I_CHOWN 4
67: #define I_GET 5
68: #define I_HELP 6
69: #define I_LCHDIR 7
70: #define I_LLS 8
71: #define I_LMKDIR 9
72: #define I_LPWD 10
73: #define I_LS 11
74: #define I_LUMASK 12
75: #define I_MKDIR 13
76: #define I_PUT 14
77: #define I_PWD 15
78: #define I_QUIT 16
79: #define I_RENAME 17
80: #define I_RM 18
81: #define I_RMDIR 19
82: #define I_SHELL 20
1.26 djm 83: #define I_SYMLINK 21
1.28 markus 84: #define I_VERSION 22
1.52 fgsch 85: #define I_PROGRESS 23
1.1 djm 86:
87: struct CMD {
1.6 deraadt 88: const char *c;
1.1 djm 89: const int n;
90: };
91:
1.54 djm 92: static const struct CMD cmds[] = {
1.40 markus 93: { "bye", I_QUIT },
1.15 stevesk 94: { "cd", I_CHDIR },
95: { "chdir", I_CHDIR },
96: { "chgrp", I_CHGRP },
97: { "chmod", I_CHMOD },
98: { "chown", I_CHOWN },
99: { "dir", I_LS },
100: { "exit", I_QUIT },
101: { "get", I_GET },
1.34 djm 102: { "mget", I_GET },
1.15 stevesk 103: { "help", I_HELP },
104: { "lcd", I_LCHDIR },
105: { "lchdir", I_LCHDIR },
106: { "lls", I_LLS },
107: { "lmkdir", I_LMKDIR },
1.26 djm 108: { "ln", I_SYMLINK },
1.15 stevesk 109: { "lpwd", I_LPWD },
110: { "ls", I_LS },
111: { "lumask", I_LUMASK },
112: { "mkdir", I_MKDIR },
1.52 fgsch 113: { "progress", I_PROGRESS },
1.15 stevesk 114: { "put", I_PUT },
1.34 djm 115: { "mput", I_PUT },
1.15 stevesk 116: { "pwd", I_PWD },
117: { "quit", I_QUIT },
118: { "rename", I_RENAME },
119: { "rm", I_RM },
120: { "rmdir", I_RMDIR },
1.26 djm 121: { "symlink", I_SYMLINK },
1.28 markus 122: { "version", I_VERSION },
1.6 deraadt 123: { "!", I_SHELL },
1.7 deraadt 124: { "?", I_HELP },
1.6 deraadt 125: { NULL, -1}
1.1 djm 126: };
127:
1.37 itojun 128: static void
1.1 djm 129: help(void)
130: {
131: printf("Available commands:\n");
1.13 stevesk 132: printf("cd path Change remote directory to 'path'\n");
133: printf("lcd path Change local directory to 'path'\n");
134: printf("chgrp grp path Change group of file 'path' to 'grp'\n");
135: printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
136: printf("chown own path Change owner of file 'path' to 'own'\n");
137: printf("help Display this help text\n");
138: printf("get remote-path [local-path] Download file\n");
139: printf("lls [ls-options [path]] Display local directory listing\n");
1.26 djm 140: printf("ln oldpath newpath Symlink remote file\n");
1.13 stevesk 141: printf("lmkdir path Create local directory\n");
142: printf("lpwd Print local working directory\n");
143: printf("ls [path] Display remote directory listing\n");
144: printf("lumask umask Set local umask to 'umask'\n");
145: printf("mkdir path Create remote directory\n");
1.53 fgsch 146: printf("progress Toggle display of progress meter\n");
1.13 stevesk 147: printf("put local-path [remote-path] Upload file\n");
148: printf("pwd Display remote working directory\n");
149: printf("exit Quit sftp\n");
150: printf("quit Quit sftp\n");
151: printf("rename oldpath newpath Rename remote file\n");
152: printf("rmdir path Remove remote directory\n");
153: printf("rm path Delete remote file\n");
1.26 djm 154: printf("symlink oldpath newpath Symlink remote file\n");
1.28 markus 155: printf("version Show SFTP version\n");
1.1 djm 156: printf("!command Execute 'command' in local shell\n");
157: printf("! Escape to local shell\n");
1.13 stevesk 158: printf("? Synonym for help\n");
1.1 djm 159: }
160:
1.37 itojun 161: static void
1.1 djm 162: local_do_shell(const char *args)
163: {
1.36 markus 164: int status;
1.1 djm 165: char *shell;
166: pid_t pid;
1.3 stevesk 167:
1.1 djm 168: if (!*args)
169: args = NULL;
1.3 stevesk 170:
1.1 djm 171: if ((shell = getenv("SHELL")) == NULL)
172: shell = _PATH_BSHELL;
173:
174: if ((pid = fork()) == -1)
175: fatal("Couldn't fork: %s", strerror(errno));
176:
177: if (pid == 0) {
178: /* XXX: child has pipe fds to ssh subproc open - issue? */
179: if (args) {
180: debug3("Executing %s -c \"%s\"", shell, args);
1.38 deraadt 181: execl(shell, shell, "-c", args, (char *)NULL);
1.1 djm 182: } else {
183: debug3("Executing %s", shell);
1.38 deraadt 184: execl(shell, shell, (char *)NULL);
1.1 djm 185: }
1.3 stevesk 186: fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
1.1 djm 187: strerror(errno));
188: _exit(1);
189: }
1.46 markus 190: while (waitpid(pid, &status, 0) == -1)
191: if (errno != EINTR)
192: fatal("Couldn't wait for child: %s", strerror(errno));
1.1 djm 193: if (!WIFEXITED(status))
194: error("Shell exited abormally");
195: else if (WEXITSTATUS(status))
196: error("Shell exited with status %d", WEXITSTATUS(status));
197: }
198:
1.37 itojun 199: static void
1.1 djm 200: local_do_ls(const char *args)
201: {
202: if (!args || !*args)
1.18 stevesk 203: local_do_shell(_PATH_LS);
1.1 djm 204: else {
1.18 stevesk 205: int len = strlen(_PATH_LS " ") + strlen(args) + 1;
1.16 deraadt 206: char *buf = xmalloc(len);
1.1 djm 207:
208: /* XXX: quoting - rip quoting code from ftp? */
1.18 stevesk 209: snprintf(buf, len, _PATH_LS " %s", args);
1.1 djm 210: local_do_shell(buf);
1.16 deraadt 211: xfree(buf);
1.1 djm 212: }
213: }
214:
1.48 djm 215: /* Strip one path (usually the pwd) from the start of another */
216: static char *
217: path_strip(char *path, char *strip)
218: {
219: size_t len;
220:
221: if (strip == NULL)
222: return (xstrdup(path));
223:
224: len = strlen(strip);
225: if (strip != NULL && strncmp(path, strip, len) == 0) {
226: if (strip[len - 1] != '/' && path[len] == '/')
227: len++;
228: return (xstrdup(path + len));
229: }
230:
231: return (xstrdup(path));
232: }
233:
1.37 itojun 234: static char *
1.29 djm 235: path_append(char *p1, char *p2)
236: {
237: char *ret;
1.31 markus 238: int len = strlen(p1) + strlen(p2) + 2;
1.29 djm 239:
1.31 markus 240: ret = xmalloc(len);
241: strlcpy(ret, p1, len);
1.48 djm 242: if (p1[strlen(p1) - 1] != '/')
1.39 jakob 243: strlcat(ret, "/", len);
1.31 markus 244: strlcat(ret, p2, len);
1.29 djm 245:
246: return(ret);
247: }
248:
1.37 itojun 249: static char *
1.1 djm 250: make_absolute(char *p, char *pwd)
251: {
1.29 djm 252: char *abs;
1.1 djm 253:
254: /* Derelativise */
255: if (p && p[0] != '/') {
1.29 djm 256: abs = path_append(pwd, p);
1.1 djm 257: xfree(p);
1.29 djm 258: return(abs);
259: } else
260: return(p);
261: }
262:
1.37 itojun 263: static int
1.29 djm 264: infer_path(const char *p, char **ifp)
265: {
266: char *cp;
267:
268: cp = strrchr(p, '/');
269: if (cp == NULL) {
270: *ifp = xstrdup(p);
271: return(0);
1.1 djm 272: }
273:
1.29 djm 274: if (!cp[1]) {
275: error("Invalid path");
276: return(-1);
277: }
278:
279: *ifp = xstrdup(cp + 1);
280: return(0);
1.1 djm 281: }
282:
1.37 itojun 283: static int
1.1 djm 284: parse_getput_flags(const char **cpp, int *pflag)
285: {
286: const char *cp = *cpp;
287:
288: /* Check for flags */
289: if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
1.20 djm 290: switch (cp[1]) {
1.22 djm 291: case 'p':
1.1 djm 292: case 'P':
293: *pflag = 1;
294: break;
295: default:
1.22 djm 296: error("Invalid flag -%c", cp[1]);
1.1 djm 297: return(-1);
298: }
299: cp += 2;
300: *cpp = cp + strspn(cp, WHITESPACE);
301: }
302:
303: return(0);
304: }
305:
1.37 itojun 306: static int
1.48 djm 307: parse_ls_flags(const char **cpp, int *lflag)
308: {
309: const char *cp = *cpp;
310:
311: /* Check for flags */
312: if (cp++[0] == '-') {
313: for(; strchr(WHITESPACE, *cp) == NULL; cp++) {
314: switch (*cp) {
315: case 'l':
1.60 mouring 316: *lflag = LONG_VIEW;
317: break;
318: case '1':
319: *lflag = SHORT_VIEW;
1.48 djm 320: break;
321: default:
322: error("Invalid flag -%c", *cp);
323: return(-1);
324: }
325: }
326: *cpp = cp + strspn(cp, WHITESPACE);
327: }
328:
329: return(0);
330: }
331:
332: static int
1.1 djm 333: get_pathname(const char **cpp, char **path)
334: {
1.8 provos 335: const char *cp = *cpp, *end;
336: char quot;
1.61 djm 337: int i, j;
1.1 djm 338:
339: cp += strspn(cp, WHITESPACE);
340: if (!*cp) {
341: *cpp = cp;
342: *path = NULL;
1.8 provos 343: return (0);
1.1 djm 344: }
345:
1.61 djm 346: *path = xmalloc(strlen(cp) + 1);
347:
1.1 djm 348: /* Check for quoted filenames */
349: if (*cp == '\"' || *cp == '\'') {
1.8 provos 350: quot = *cp++;
1.30 markus 351:
1.61 djm 352: /* Search for terminating quote, unescape some chars */
353: for (i = j = 0; i <= strlen(cp); i++) {
354: if (cp[i] == quot) { /* Found quote */
1.63 djm 355: i++;
1.61 djm 356: (*path)[j] = '\0';
357: break;
358: }
359: if (cp[i] == '\0') { /* End of string */
360: error("Unterminated quote");
361: goto fail;
362: }
363: if (cp[i] == '\\') { /* Escaped characters */
364: i++;
1.65 ! djm 365: if (cp[i] != '\'' && cp[i] != '\"' &&
1.61 djm 366: cp[i] != '\\') {
367: error("Bad escaped character '\%c'",
368: cp[i]);
369: goto fail;
370: }
371: }
372: (*path)[j++] = cp[i];
1.1 djm 373: }
1.61 djm 374:
375: if (j == 0) {
1.1 djm 376: error("Empty quotes");
1.8 provos 377: goto fail;
1.1 djm 378: }
1.61 djm 379: *cpp = cp + i + strspn(cp + i, WHITESPACE);
1.8 provos 380: } else {
381: /* Read to end of filename */
382: end = strpbrk(cp, WHITESPACE);
383: if (end == NULL)
384: end = strchr(cp, '\0');
385: *cpp = end + strspn(end, WHITESPACE);
1.61 djm 386:
387: memcpy(*path, cp, end - cp);
388: (*path)[end - cp] = '\0';
1.1 djm 389: }
1.61 djm 390: return (0);
1.8 provos 391:
392: fail:
1.65 ! djm 393: xfree(*path);
! 394: *path = NULL;
1.8 provos 395: return (-1);
1.1 djm 396: }
397:
1.37 itojun 398: static int
1.29 djm 399: is_dir(char *path)
400: {
401: struct stat sb;
402:
403: /* XXX: report errors? */
404: if (stat(path, &sb) == -1)
405: return(0);
406:
407: return(sb.st_mode & S_IFDIR);
408: }
409:
1.37 itojun 410: static int
1.55 djm 411: is_reg(char *path)
412: {
413: struct stat sb;
414:
415: if (stat(path, &sb) == -1)
416: fatal("stat %s: %s", path, strerror(errno));
417:
418: return(S_ISREG(sb.st_mode));
419: }
420:
421: static int
1.44 djm 422: remote_is_dir(struct sftp_conn *conn, char *path)
1.1 djm 423: {
1.29 djm 424: Attrib *a;
1.1 djm 425:
1.29 djm 426: /* XXX: report errors? */
1.44 djm 427: if ((a = do_stat(conn, path, 1)) == NULL)
1.29 djm 428: return(0);
429: if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
1.1 djm 430: return(0);
1.29 djm 431: return(a->perm & S_IFDIR);
432: }
433:
1.37 itojun 434: static int
1.44 djm 435: process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
1.29 djm 436: {
437: char *abs_src = NULL;
438: char *abs_dst = NULL;
439: char *tmp;
440: glob_t g;
441: int err = 0;
442: int i;
1.30 markus 443:
1.29 djm 444: abs_src = xstrdup(src);
445: abs_src = make_absolute(abs_src, pwd);
446:
1.30 markus 447: memset(&g, 0, sizeof(g));
1.29 djm 448: debug3("Looking up %s", abs_src);
1.44 djm 449: if (remote_glob(conn, abs_src, 0, NULL, &g)) {
1.29 djm 450: error("File \"%s\" not found.", abs_src);
451: err = -1;
452: goto out;
453: }
454:
1.59 mouring 455: /* If multiple matches, dst must be a directory or unspecified */
456: if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
1.30 markus 457: error("Multiple files match, but \"%s\" is not a directory",
1.29 djm 458: dst);
459: err = -1;
460: goto out;
461: }
1.30 markus 462:
1.41 deraadt 463: for (i = 0; g.gl_pathv[i]; i++) {
1.29 djm 464: if (infer_path(g.gl_pathv[i], &tmp)) {
465: err = -1;
466: goto out;
467: }
1.59 mouring 468:
469: if (g.gl_matchc == 1 && dst) {
470: /* If directory specified, append filename */
471: if (is_dir(dst)) {
472: if (infer_path(g.gl_pathv[0], &tmp)) {
473: err = 1;
474: goto out;
475: }
476: abs_dst = path_append(dst, tmp);
477: xfree(tmp);
478: } else
479: abs_dst = xstrdup(dst);
480: } else if (dst) {
1.29 djm 481: abs_dst = path_append(dst, tmp);
482: xfree(tmp);
483: } else
484: abs_dst = tmp;
485:
486: printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
1.44 djm 487: if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
1.29 djm 488: err = -1;
489: xfree(abs_dst);
490: abs_dst = NULL;
1.1 djm 491: }
492:
1.29 djm 493: out:
494: xfree(abs_src);
495: if (abs_dst)
496: xfree(abs_dst);
497: globfree(&g);
498: return(err);
499: }
500:
1.37 itojun 501: static int
1.44 djm 502: process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
1.29 djm 503: {
504: char *tmp_dst = NULL;
505: char *abs_dst = NULL;
506: char *tmp;
507: glob_t g;
508: int err = 0;
509: int i;
510:
511: if (dst) {
512: tmp_dst = xstrdup(dst);
513: tmp_dst = make_absolute(tmp_dst, pwd);
514: }
515:
1.30 markus 516: memset(&g, 0, sizeof(g));
1.29 djm 517: debug3("Looking up %s", src);
518: if (glob(src, 0, NULL, &g)) {
519: error("File \"%s\" not found.", src);
520: err = -1;
521: goto out;
522: }
523:
1.59 mouring 524: /* If multiple matches, dst may be directory or unspecified */
525: if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
1.30 markus 526: error("Multiple files match, but \"%s\" is not a directory",
1.29 djm 527: tmp_dst);
528: err = -1;
529: goto out;
530: }
531:
1.41 deraadt 532: for (i = 0; g.gl_pathv[i]; i++) {
1.55 djm 533: if (!is_reg(g.gl_pathv[i])) {
1.65 ! djm 534: error("skipping non-regular file %s",
1.55 djm 535: g.gl_pathv[i]);
536: continue;
537: }
1.29 djm 538: if (infer_path(g.gl_pathv[i], &tmp)) {
539: err = -1;
540: goto out;
541: }
1.59 mouring 542:
543: if (g.gl_matchc == 1 && tmp_dst) {
544: /* If directory specified, append filename */
545: if (remote_is_dir(conn, tmp_dst)) {
546: if (infer_path(g.gl_pathv[0], &tmp)) {
547: err = 1;
548: goto out;
549: }
550: abs_dst = path_append(tmp_dst, tmp);
551: xfree(tmp);
552: } else
553: abs_dst = xstrdup(tmp_dst);
554:
555: } else if (tmp_dst) {
1.29 djm 556: abs_dst = path_append(tmp_dst, tmp);
557: xfree(tmp);
558: } else
559: abs_dst = make_absolute(tmp, pwd);
560:
561: printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
1.44 djm 562: if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
1.29 djm 563: err = -1;
1.1 djm 564: }
565:
1.29 djm 566: out:
567: if (abs_dst)
568: xfree(abs_dst);
569: if (tmp_dst)
570: xfree(tmp_dst);
1.58 mouring 571: globfree(&g);
1.29 djm 572: return(err);
1.1 djm 573: }
574:
1.37 itojun 575: static int
1.48 djm 576: sdirent_comp(const void *aa, const void *bb)
577: {
578: SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
579: SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
580:
1.50 deraadt 581: return (strcmp(a->filename, b->filename));
1.48 djm 582: }
583:
584: /* sftp ls.1 replacement for directories */
585: static int
586: do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
587: {
1.60 mouring 588: int n, c = 1, colspace = 0, columns = 1;
1.48 djm 589: SFTP_DIRENT **d;
590:
591: if ((n = do_readdir(conn, path, &d)) != 0)
592: return (n);
593:
1.60 mouring 594: if (!(lflag & SHORT_VIEW)) {
595: int m = 0, width = 80;
596: struct winsize ws;
597:
598: /* Count entries for sort and find longest filename */
599: for (n = 0; d[n] != NULL; n++)
600: m = MAX(m, strlen(d[n]->filename));
601:
1.65 ! djm 602: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
1.60 mouring 603: width = ws.ws_col;
604:
605: columns = width / (m + 2);
1.62 fgsch 606: columns = MAX(columns, 1);
1.60 mouring 607: colspace = width / columns;
608: }
1.48 djm 609:
610: qsort(d, n, sizeof(*d), sdirent_comp);
611:
612: for (n = 0; d[n] != NULL; n++) {
613: char *tmp, *fname;
1.50 deraadt 614:
1.48 djm 615: tmp = path_append(path, d[n]->filename);
616: fname = path_strip(tmp, strip_path);
617: xfree(tmp);
618:
1.60 mouring 619: if (lflag & LONG_VIEW) {
1.48 djm 620: char *lname;
621: struct stat sb;
622:
623: memset(&sb, 0, sizeof(sb));
624: attrib_to_stat(&d[n]->a, &sb);
625: lname = ls_file(fname, &sb, 1);
626: printf("%s\n", lname);
627: xfree(lname);
628: } else {
1.60 mouring 629: printf("%-*s", colspace, fname);
630: if (c >= columns) {
631: printf("\n");
632: c = 1;
633: } else
634: c++;
1.48 djm 635: }
1.50 deraadt 636:
1.48 djm 637: xfree(fname);
638: }
639:
1.60 mouring 640: if (!(lflag & LONG_VIEW) && (c != 1))
641: printf("\n");
642:
1.48 djm 643: free_sftp_dirents(d);
644: return (0);
645: }
646:
647: /* sftp ls.1 replacement which handles path globs */
648: static int
1.50 deraadt 649: do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
1.48 djm 650: int lflag)
651: {
652: glob_t g;
1.60 mouring 653: int i, c = 1, colspace = 0, columns = 1;
1.48 djm 654: Attrib *a;
655:
656: memset(&g, 0, sizeof(g));
657:
1.50 deraadt 658: if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
1.48 djm 659: NULL, &g)) {
660: error("Can't ls: \"%s\" not found", path);
661: return (-1);
662: }
663:
664: /*
1.50 deraadt 665: * If the glob returns a single match, which is the same as the
1.48 djm 666: * input glob, and it is a directory, then just list its contents
667: */
1.50 deraadt 668: if (g.gl_pathc == 1 &&
1.48 djm 669: strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) {
670: if ((a = do_lstat(conn, path, 1)) == NULL) {
671: globfree(&g);
1.65 ! djm 672: return (-1);
1.48 djm 673: }
1.50 deraadt 674: if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.48 djm 675: S_ISDIR(a->perm)) {
676: globfree(&g);
677: return (do_ls_dir(conn, path, strip_path, lflag));
678: }
679: }
680:
1.60 mouring 681: if (!(lflag & SHORT_VIEW)) {
682: int m = 0, width = 80;
1.65 ! djm 683: struct winsize ws;
1.60 mouring 684:
685: /* Count entries for sort and find longest filename */
1.65 ! djm 686: for (i = 0; g.gl_pathv[i]; i++)
1.60 mouring 687: m = MAX(m, strlen(g.gl_pathv[i]));
688:
689: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
690: width = ws.ws_col;
691:
692: columns = width / (m + 2);
1.62 fgsch 693: columns = MAX(columns, 1);
1.60 mouring 694: colspace = width / columns;
695: }
696:
1.48 djm 697: for (i = 0; g.gl_pathv[i]; i++) {
1.60 mouring 698: char *fname;
1.48 djm 699:
700: fname = path_strip(g.gl_pathv[i], strip_path);
701:
1.60 mouring 702: if (lflag & LONG_VIEW) {
703: char *lname;
704: struct stat sb;
705:
1.48 djm 706: /*
707: * XXX: this is slow - 1 roundtrip per path
1.50 deraadt 708: * A solution to this is to fork glob() and
709: * build a sftp specific version which keeps the
1.48 djm 710: * attribs (which currently get thrown away)
711: * that the server returns as well as the filenames.
712: */
713: memset(&sb, 0, sizeof(sb));
714: a = do_lstat(conn, g.gl_pathv[i], 1);
715: if (a != NULL)
716: attrib_to_stat(a, &sb);
717: lname = ls_file(fname, &sb, 1);
718: printf("%s\n", lname);
719: xfree(lname);
720: } else {
1.60 mouring 721: printf("%-*s", colspace, fname);
722: if (c >= columns) {
723: printf("\n");
724: c = 1;
725: } else
726: c++;
1.48 djm 727: }
728: xfree(fname);
729: }
1.60 mouring 730:
731: if (!(lflag & LONG_VIEW) && (c != 1))
732: printf("\n");
1.48 djm 733:
734: if (g.gl_pathc)
735: globfree(&g);
736:
737: return (0);
738: }
739:
740: static int
1.51 djm 741: parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
1.48 djm 742: unsigned long *n_arg, char **path1, char **path2)
1.1 djm 743: {
744: const char *cmd, *cp = *cpp;
1.21 stevesk 745: char *cp2;
1.4 markus 746: int base = 0;
1.21 stevesk 747: long l;
1.1 djm 748: int i, cmdnum;
749:
750: /* Skip leading whitespace */
751: cp = cp + strspn(cp, WHITESPACE);
752:
1.51 djm 753: /* Ignore blank lines and lines which begin with comment '#' char */
754: if (*cp == '\0' || *cp == '#')
755: return (0);
1.1 djm 756:
1.51 djm 757: /* Check for leading '-' (disable error processing) */
758: *iflag = 0;
759: if (*cp == '-') {
760: *iflag = 1;
761: cp++;
762: }
1.65 ! djm 763:
1.1 djm 764: /* Figure out which command we have */
1.41 deraadt 765: for (i = 0; cmds[i].c; i++) {
1.1 djm 766: int cmdlen = strlen(cmds[i].c);
767:
768: /* Check for command followed by whitespace */
769: if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
770: strchr(WHITESPACE, cp[cmdlen])) {
771: cp += cmdlen;
772: cp = cp + strspn(cp, WHITESPACE);
773: break;
774: }
775: }
776: cmdnum = cmds[i].n;
777: cmd = cmds[i].c;
778:
779: /* Special case */
780: if (*cp == '!') {
781: cp++;
782: cmdnum = I_SHELL;
783: } else if (cmdnum == -1) {
784: error("Invalid command.");
1.51 djm 785: return (-1);
1.1 djm 786: }
787:
788: /* Get arguments and parse flags */
1.48 djm 789: *lflag = *pflag = *n_arg = 0;
1.1 djm 790: *path1 = *path2 = NULL;
791: switch (cmdnum) {
792: case I_GET:
793: case I_PUT:
794: if (parse_getput_flags(&cp, pflag))
795: return(-1);
796: /* Get first pathname (mandatory) */
797: if (get_pathname(&cp, path1))
798: return(-1);
799: if (*path1 == NULL) {
800: error("You must specify at least one path after a "
801: "%s command.", cmd);
802: return(-1);
803: }
804: /* Try to get second pathname (optional) */
805: if (get_pathname(&cp, path2))
806: return(-1);
807: break;
808: case I_RENAME:
1.26 djm 809: case I_SYMLINK:
1.1 djm 810: if (get_pathname(&cp, path1))
811: return(-1);
812: if (get_pathname(&cp, path2))
813: return(-1);
814: if (!*path1 || !*path2) {
815: error("You must specify two paths after a %s "
816: "command.", cmd);
817: return(-1);
818: }
819: break;
820: case I_RM:
821: case I_MKDIR:
822: case I_RMDIR:
823: case I_CHDIR:
824: case I_LCHDIR:
825: case I_LMKDIR:
826: /* Get pathname (mandatory) */
827: if (get_pathname(&cp, path1))
828: return(-1);
829: if (*path1 == NULL) {
1.3 stevesk 830: error("You must specify a path after a %s command.",
1.1 djm 831: cmd);
832: return(-1);
833: }
834: break;
835: case I_LS:
1.48 djm 836: if (parse_ls_flags(&cp, lflag))
837: return(-1);
1.1 djm 838: /* Path is optional */
839: if (get_pathname(&cp, path1))
840: return(-1);
841: break;
842: case I_LLS:
843: case I_SHELL:
844: /* Uses the rest of the line */
845: break;
846: case I_LUMASK:
1.21 stevesk 847: base = 8;
1.1 djm 848: case I_CHMOD:
1.4 markus 849: base = 8;
1.1 djm 850: case I_CHOWN:
851: case I_CHGRP:
852: /* Get numeric arg (mandatory) */
1.21 stevesk 853: l = strtol(cp, &cp2, base);
854: if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
855: errno == ERANGE) || l < 0) {
1.1 djm 856: error("You must supply a numeric argument "
857: "to the %s command.", cmd);
858: return(-1);
859: }
1.21 stevesk 860: cp = cp2;
861: *n_arg = l;
862: if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
863: break;
864: if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
1.1 djm 865: error("You must supply a numeric argument "
866: "to the %s command.", cmd);
867: return(-1);
868: }
869: cp += strspn(cp, WHITESPACE);
870:
871: /* Get pathname (mandatory) */
872: if (get_pathname(&cp, path1))
873: return(-1);
874: if (*path1 == NULL) {
1.3 stevesk 875: error("You must specify a path after a %s command.",
1.1 djm 876: cmd);
877: return(-1);
878: }
879: break;
880: case I_QUIT:
881: case I_PWD:
882: case I_LPWD:
883: case I_HELP:
1.28 markus 884: case I_VERSION:
1.52 fgsch 885: case I_PROGRESS:
1.1 djm 886: break;
887: default:
888: fatal("Command not implemented");
889: }
890:
891: *cpp = cp;
892: return(cmdnum);
893: }
894:
1.37 itojun 895: static int
1.51 djm 896: parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
897: int err_abort)
1.1 djm 898: {
1.8 provos 899: char *path1, *path2, *tmp;
1.51 djm 900: int pflag, lflag, iflag, cmdnum, i;
1.1 djm 901: unsigned long n_arg;
902: Attrib a, *aa;
1.23 deraadt 903: char path_buf[MAXPATHLEN];
1.25 deraadt 904: int err = 0;
1.27 djm 905: glob_t g;
1.1 djm 906:
907: path1 = path2 = NULL;
1.51 djm 908: cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
1.48 djm 909: &path1, &path2);
1.1 djm 910:
1.51 djm 911: if (iflag != 0)
912: err_abort = 0;
913:
1.29 djm 914: memset(&g, 0, sizeof(g));
915:
1.1 djm 916: /* Perform command */
917: switch (cmdnum) {
1.51 djm 918: case 0:
919: /* Blank line */
920: break;
1.1 djm 921: case -1:
1.51 djm 922: /* Unrecognized command */
923: err = -1;
1.1 djm 924: break;
925: case I_GET:
1.44 djm 926: err = process_get(conn, path1, path2, *pwd, pflag);
1.1 djm 927: break;
928: case I_PUT:
1.44 djm 929: err = process_put(conn, path1, path2, *pwd, pflag);
1.33 markus 930: break;
931: case I_RENAME:
1.1 djm 932: path1 = make_absolute(path1, *pwd);
933: path2 = make_absolute(path2, *pwd);
1.44 djm 934: err = do_rename(conn, path1, path2);
1.1 djm 935: break;
1.26 djm 936: case I_SYMLINK:
1.44 djm 937: path2 = make_absolute(path2, *pwd);
938: err = do_symlink(conn, path1, path2);
1.26 djm 939: break;
1.1 djm 940: case I_RM:
941: path1 = make_absolute(path1, *pwd);
1.44 djm 942: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41 deraadt 943: for (i = 0; g.gl_pathv[i]; i++) {
1.27 djm 944: printf("Removing %s\n", g.gl_pathv[i]);
1.51 djm 945: err = do_rm(conn, g.gl_pathv[i]);
946: if (err != 0 && err_abort)
947: break;
1.27 djm 948: }
1.1 djm 949: break;
950: case I_MKDIR:
951: path1 = make_absolute(path1, *pwd);
952: attrib_clear(&a);
953: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
954: a.perm = 0777;
1.44 djm 955: err = do_mkdir(conn, path1, &a);
1.1 djm 956: break;
957: case I_RMDIR:
958: path1 = make_absolute(path1, *pwd);
1.44 djm 959: err = do_rmdir(conn, path1);
1.1 djm 960: break;
961: case I_CHDIR:
962: path1 = make_absolute(path1, *pwd);
1.44 djm 963: if ((tmp = do_realpath(conn, path1)) == NULL) {
1.25 deraadt 964: err = 1;
1.11 markus 965: break;
1.25 deraadt 966: }
1.44 djm 967: if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1.11 markus 968: xfree(tmp);
1.25 deraadt 969: err = 1;
1.11 markus 970: break;
971: }
1.9 djm 972: if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
973: error("Can't change directory: Can't check target");
974: xfree(tmp);
1.25 deraadt 975: err = 1;
1.9 djm 976: break;
977: }
978: if (!S_ISDIR(aa->perm)) {
979: error("Can't change directory: \"%s\" is not "
980: "a directory", tmp);
981: xfree(tmp);
1.25 deraadt 982: err = 1;
1.9 djm 983: break;
984: }
1.11 markus 985: xfree(*pwd);
986: *pwd = tmp;
1.1 djm 987: break;
988: case I_LS:
1.12 djm 989: if (!path1) {
1.48 djm 990: do_globbed_ls(conn, *pwd, *pwd, lflag);
1.12 djm 991: break;
992: }
1.50 deraadt 993:
1.48 djm 994: /* Strip pwd off beginning of non-absolute paths */
995: tmp = NULL;
996: if (*path1 != '/')
997: tmp = *pwd;
998:
1.1 djm 999: path1 = make_absolute(path1, *pwd);
1.51 djm 1000: err = do_globbed_ls(conn, path1, tmp, lflag);
1.1 djm 1001: break;
1002: case I_LCHDIR:
1.25 deraadt 1003: if (chdir(path1) == -1) {
1.1 djm 1004: error("Couldn't change local directory to "
1005: "\"%s\": %s", path1, strerror(errno));
1.25 deraadt 1006: err = 1;
1007: }
1.1 djm 1008: break;
1009: case I_LMKDIR:
1.25 deraadt 1010: if (mkdir(path1, 0777) == -1) {
1.17 stevesk 1011: error("Couldn't create local directory "
1.1 djm 1012: "\"%s\": %s", path1, strerror(errno));
1.25 deraadt 1013: err = 1;
1014: }
1.1 djm 1015: break;
1016: case I_LLS:
1017: local_do_ls(cmd);
1018: break;
1019: case I_SHELL:
1020: local_do_shell(cmd);
1021: break;
1022: case I_LUMASK:
1023: umask(n_arg);
1.21 stevesk 1024: printf("Local umask: %03lo\n", n_arg);
1.1 djm 1025: break;
1026: case I_CHMOD:
1027: path1 = make_absolute(path1, *pwd);
1028: attrib_clear(&a);
1029: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1030: a.perm = n_arg;
1.44 djm 1031: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41 deraadt 1032: for (i = 0; g.gl_pathv[i]; i++) {
1.27 djm 1033: printf("Changing mode on %s\n", g.gl_pathv[i]);
1.51 djm 1034: err = do_setstat(conn, g.gl_pathv[i], &a);
1035: if (err != 0 && err_abort)
1036: break;
1.27 djm 1037: }
1.5 stevesk 1038: break;
1.1 djm 1039: case I_CHOWN:
1.51 djm 1040: case I_CHGRP:
1.1 djm 1041: path1 = make_absolute(path1, *pwd);
1.44 djm 1042: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41 deraadt 1043: for (i = 0; g.gl_pathv[i]; i++) {
1.51 djm 1044: if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
1045: if (err != 0 && err_abort)
1046: break;
1047: else
1048: continue;
1049: }
1.27 djm 1050: if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1051: error("Can't get current ownership of "
1052: "remote file \"%s\"", g.gl_pathv[i]);
1.51 djm 1053: if (err != 0 && err_abort)
1054: break;
1055: else
1056: continue;
1.27 djm 1057: }
1058: aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1.51 djm 1059: if (cmdnum == I_CHOWN) {
1060: printf("Changing owner on %s\n", g.gl_pathv[i]);
1061: aa->uid = n_arg;
1062: } else {
1063: printf("Changing group on %s\n", g.gl_pathv[i]);
1064: aa->gid = n_arg;
1.27 djm 1065: }
1.51 djm 1066: err = do_setstat(conn, g.gl_pathv[i], aa);
1067: if (err != 0 && err_abort)
1068: break;
1.1 djm 1069: }
1070: break;
1071: case I_PWD:
1072: printf("Remote working directory: %s\n", *pwd);
1073: break;
1074: case I_LPWD:
1.51 djm 1075: if (!getcwd(path_buf, sizeof(path_buf))) {
1076: error("Couldn't get local cwd: %s", strerror(errno));
1077: err = -1;
1078: break;
1079: }
1080: printf("Local working directory: %s\n", path_buf);
1.1 djm 1081: break;
1082: case I_QUIT:
1.51 djm 1083: /* Processed below */
1084: break;
1.1 djm 1085: case I_HELP:
1086: help();
1.28 markus 1087: break;
1088: case I_VERSION:
1.47 deraadt 1089: printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1.52 fgsch 1090: break;
1091: case I_PROGRESS:
1092: showprogress = !showprogress;
1093: if (showprogress)
1094: printf("Progress meter enabled\n");
1095: else
1096: printf("Progress meter disabled\n");
1.1 djm 1097: break;
1098: default:
1099: fatal("%d is not implemented", cmdnum);
1100: }
1101:
1.29 djm 1102: if (g.gl_pathc)
1103: globfree(&g);
1.1 djm 1104: if (path1)
1105: xfree(path1);
1106: if (path2)
1107: xfree(path2);
1.25 deraadt 1108:
1.51 djm 1109: /* If an unignored error occurs in batch mode we should abort. */
1110: if (err_abort && err != 0)
1111: return (-1);
1112: else if (cmdnum == I_QUIT)
1113: return (1);
1.25 deraadt 1114:
1.51 djm 1115: return (0);
1.1 djm 1116: }
1117:
1.51 djm 1118: int
1.35 mouring 1119: interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1.1 djm 1120: {
1121: char *pwd;
1.35 mouring 1122: char *dir = NULL;
1.1 djm 1123: char cmd[2048];
1.44 djm 1124: struct sftp_conn *conn;
1.51 djm 1125: int err;
1.26 djm 1126:
1.44 djm 1127: conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1128: if (conn == NULL)
1.26 djm 1129: fatal("Couldn't initialise connection to server");
1.1 djm 1130:
1.44 djm 1131: pwd = do_realpath(conn, ".");
1.1 djm 1132: if (pwd == NULL)
1133: fatal("Need cwd");
1134:
1.35 mouring 1135: if (file1 != NULL) {
1136: dir = xstrdup(file1);
1137: dir = make_absolute(dir, pwd);
1138:
1.44 djm 1139: if (remote_is_dir(conn, dir) && file2 == NULL) {
1.35 mouring 1140: printf("Changing to: %s\n", dir);
1141: snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
1.51 djm 1142: if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0)
1143: return (-1);
1.35 mouring 1144: } else {
1145: if (file2 == NULL)
1146: snprintf(cmd, sizeof cmd, "get %s", dir);
1147: else
1148: snprintf(cmd, sizeof cmd, "get %s %s", dir,
1149: file2);
1150:
1.51 djm 1151: err = parse_dispatch_command(conn, cmd, &pwd, 1);
1.45 mpech 1152: xfree(dir);
1.57 markus 1153: xfree(pwd);
1.51 djm 1154: return (err);
1.35 mouring 1155: }
1.45 mpech 1156: xfree(dir);
1.35 mouring 1157: }
1.51 djm 1158:
1.14 stevesk 1159: setvbuf(stdout, NULL, _IOLBF, 0);
1.25 deraadt 1160: setvbuf(infile, NULL, _IOLBF, 0);
1.1 djm 1161:
1.51 djm 1162: err = 0;
1.41 deraadt 1163: for (;;) {
1.1 djm 1164: char *cp;
1165:
1166: printf("sftp> ");
1167:
1168: /* XXX: use libedit */
1.25 deraadt 1169: if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1.1 djm 1170: printf("\n");
1171: break;
1.25 deraadt 1172: } else if (infile != stdin) /* Bluff typing */
1173: printf("%s", cmd);
1174:
1.1 djm 1175: cp = strrchr(cmd, '\n');
1176: if (cp)
1177: *cp = '\0';
1.25 deraadt 1178:
1.51 djm 1179: err = parse_dispatch_command(conn, cmd, &pwd, infile != stdin);
1180: if (err != 0)
1.1 djm 1181: break;
1182: }
1183: xfree(pwd);
1.51 djm 1184:
1185: /* err == 1 signifies normal "quit" exit */
1186: return (err >= 0 ? 0 : -1);
1.1 djm 1187: }
1.51 djm 1188: