Annotation of src/usr.bin/ssh/sftp.c, Revision 1.105
1.105 ! djm 1: /* $OpenBSD: sftp.c,v 1.104 2008/12/09 02:39:59 djm Exp $ */
1.1 djm 2: /*
1.42 djm 3: * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
1.1 djm 4: *
1.42 djm 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 djm 8: *
1.42 djm 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 djm 16: */
17:
1.91 deraadt 18: #include <sys/types.h>
1.72 stevesk 19: #include <sys/ioctl.h>
1.73 stevesk 20: #include <sys/wait.h>
1.75 stevesk 21: #include <sys/stat.h>
1.83 stevesk 22: #include <sys/socket.h>
1.88 stevesk 23: #include <sys/param.h>
1.100 djm 24: #include <sys/statvfs.h>
1.44 djm 25:
1.97 djm 26: #include <ctype.h>
1.85 stevesk 27: #include <errno.h>
1.44 djm 28: #include <glob.h>
1.57 djm 29: #include <histedit.h>
1.71 stevesk 30: #include <paths.h>
1.74 stevesk 31: #include <signal.h>
1.89 stevesk 32: #include <stdlib.h>
1.90 stevesk 33: #include <stdio.h>
1.87 stevesk 34: #include <string.h>
1.86 stevesk 35: #include <unistd.h>
1.100 djm 36: #include <util.h>
1.91 deraadt 37: #include <stdarg.h>
1.1 djm 38:
39: #include "xmalloc.h"
40: #include "log.h"
41: #include "pathnames.h"
1.16 mouring 42: #include "misc.h"
1.1 djm 43:
44: #include "sftp.h"
1.91 deraadt 45: #include "buffer.h"
1.1 djm 46: #include "sftp-common.h"
47: #include "sftp-client.h"
1.43 djm 48:
1.44 djm 49: /* File to read commands from */
50: FILE* infile;
1.15 mouring 51:
1.44 djm 52: /* Are we in batchfile mode? */
1.39 djm 53: int batchmode = 0;
1.44 djm 54:
55: /* Size of buffer used when copying files */
1.24 djm 56: size_t copy_buffer_len = 32768;
1.44 djm 57:
58: /* Number of concurrent outstanding requests */
1.103 djm 59: size_t num_requests = 64;
1.44 djm 60:
61: /* PID of ssh transport process */
1.36 djm 62: static pid_t sshpid = -1;
1.7 markus 63:
1.44 djm 64: /* This is set to 0 if the progressmeter is not desired. */
1.45 djm 65: int showprogress = 1;
1.44 djm 66:
1.46 djm 67: /* SIGINT received during command processing */
68: volatile sig_atomic_t interrupted = 0;
69:
1.52 djm 70: /* I wish qsort() took a separate ctx for the comparison function...*/
71: int sort_flag;
72:
1.44 djm 73: int remote_glob(struct sftp_conn *, const char *, int,
74: int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
75:
76: /* Separators for interactive commands */
77: #define WHITESPACE " \t\r\n"
78:
1.52 djm 79: /* ls flags */
1.53 djm 80: #define LS_LONG_VIEW 0x01 /* Full view ala ls -l */
81: #define LS_SHORT_VIEW 0x02 /* Single row view ala ls -1 */
82: #define LS_NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */
83: #define LS_NAME_SORT 0x08 /* Sort by name (default) */
84: #define LS_TIME_SORT 0x10 /* Sort by mtime */
85: #define LS_SIZE_SORT 0x20 /* Sort by file size */
86: #define LS_REVERSE_SORT 0x40 /* Reverse sort order */
1.54 djm 87: #define LS_SHOW_ALL 0x80 /* Don't skip filenames starting with '.' */
1.52 djm 88:
1.53 djm 89: #define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW)
90: #define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
1.44 djm 91:
92: /* Commands for interactive mode */
93: #define I_CHDIR 1
94: #define I_CHGRP 2
95: #define I_CHMOD 3
96: #define I_CHOWN 4
1.100 djm 97: #define I_DF 24
1.44 djm 98: #define I_GET 5
99: #define I_HELP 6
100: #define I_LCHDIR 7
101: #define I_LLS 8
102: #define I_LMKDIR 9
103: #define I_LPWD 10
104: #define I_LS 11
105: #define I_LUMASK 12
106: #define I_MKDIR 13
107: #define I_PUT 14
108: #define I_PWD 15
109: #define I_QUIT 16
110: #define I_RENAME 17
111: #define I_RM 18
112: #define I_RMDIR 19
113: #define I_SHELL 20
114: #define I_SYMLINK 21
115: #define I_VERSION 22
116: #define I_PROGRESS 23
117:
118: struct CMD {
119: const char *c;
120: const int n;
121: };
122:
123: static const struct CMD cmds[] = {
124: { "bye", I_QUIT },
125: { "cd", I_CHDIR },
126: { "chdir", I_CHDIR },
127: { "chgrp", I_CHGRP },
128: { "chmod", I_CHMOD },
129: { "chown", I_CHOWN },
1.100 djm 130: { "df", I_DF },
1.44 djm 131: { "dir", I_LS },
132: { "exit", I_QUIT },
133: { "get", I_GET },
134: { "mget", I_GET },
135: { "help", I_HELP },
136: { "lcd", I_LCHDIR },
137: { "lchdir", I_LCHDIR },
138: { "lls", I_LLS },
139: { "lmkdir", I_LMKDIR },
140: { "ln", I_SYMLINK },
141: { "lpwd", I_LPWD },
142: { "ls", I_LS },
143: { "lumask", I_LUMASK },
144: { "mkdir", I_MKDIR },
145: { "progress", I_PROGRESS },
146: { "put", I_PUT },
147: { "mput", I_PUT },
148: { "pwd", I_PWD },
149: { "quit", I_QUIT },
150: { "rename", I_RENAME },
151: { "rm", I_RM },
152: { "rmdir", I_RMDIR },
153: { "symlink", I_SYMLINK },
154: { "version", I_VERSION },
155: { "!", I_SHELL },
156: { "?", I_HELP },
157: { NULL, -1}
158: };
159:
160: int interactive_loop(int fd_in, int fd_out, char *file1, char *file2);
161:
1.96 stevesk 162: /* ARGSUSED */
1.44 djm 163: static void
1.46 djm 164: killchild(int signo)
165: {
1.61 dtucker 166: if (sshpid > 1) {
1.46 djm 167: kill(sshpid, SIGTERM);
1.61 dtucker 168: waitpid(sshpid, NULL, 0);
169: }
1.46 djm 170:
171: _exit(1);
172: }
173:
1.96 stevesk 174: /* ARGSUSED */
1.46 djm 175: static void
176: cmd_interrupt(int signo)
177: {
178: const char msg[] = "\rInterrupt \n";
1.59 djm 179: int olderrno = errno;
1.46 djm 180:
181: write(STDERR_FILENO, msg, sizeof(msg) - 1);
182: interrupted = 1;
1.59 djm 183: errno = olderrno;
1.46 djm 184: }
185:
186: static void
1.44 djm 187: help(void)
188: {
189: printf("Available commands:\n");
190: printf("cd path Change remote directory to 'path'\n");
191: printf("lcd path Change local directory to 'path'\n");
192: printf("chgrp grp path Change group of file 'path' to 'grp'\n");
193: printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
194: printf("chown own path Change owner of file 'path' to 'own'\n");
1.100 djm 195: printf("df [path] Display statistics for current directory or\n");
196: printf(" filesystem containing 'path'\n");
1.44 djm 197: printf("help Display this help text\n");
198: printf("get remote-path [local-path] Download file\n");
199: printf("lls [ls-options [path]] Display local directory listing\n");
200: printf("ln oldpath newpath Symlink remote file\n");
201: printf("lmkdir path Create local directory\n");
202: printf("lpwd Print local working directory\n");
203: printf("ls [path] Display remote directory listing\n");
204: printf("lumask umask Set local umask to 'umask'\n");
205: printf("mkdir path Create remote directory\n");
206: printf("progress Toggle display of progress meter\n");
207: printf("put local-path [remote-path] Upload file\n");
208: printf("pwd Display remote working directory\n");
209: printf("exit Quit sftp\n");
210: printf("quit Quit sftp\n");
211: printf("rename oldpath newpath Rename remote file\n");
212: printf("rmdir path Remove remote directory\n");
213: printf("rm path Delete remote file\n");
214: printf("symlink oldpath newpath Symlink remote file\n");
215: printf("version Show SFTP version\n");
216: printf("!command Execute 'command' in local shell\n");
217: printf("! Escape to local shell\n");
218: printf("? Synonym for help\n");
219: }
220:
221: static void
222: local_do_shell(const char *args)
223: {
224: int status;
225: char *shell;
226: pid_t pid;
227:
228: if (!*args)
229: args = NULL;
230:
231: if ((shell = getenv("SHELL")) == NULL)
232: shell = _PATH_BSHELL;
233:
234: if ((pid = fork()) == -1)
235: fatal("Couldn't fork: %s", strerror(errno));
236:
237: if (pid == 0) {
238: /* XXX: child has pipe fds to ssh subproc open - issue? */
239: if (args) {
240: debug3("Executing %s -c \"%s\"", shell, args);
241: execl(shell, shell, "-c", args, (char *)NULL);
242: } else {
243: debug3("Executing %s", shell);
244: execl(shell, shell, (char *)NULL);
245: }
246: fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
247: strerror(errno));
248: _exit(1);
249: }
250: while (waitpid(pid, &status, 0) == -1)
251: if (errno != EINTR)
252: fatal("Couldn't wait for child: %s", strerror(errno));
253: if (!WIFEXITED(status))
1.78 djm 254: error("Shell exited abnormally");
1.44 djm 255: else if (WEXITSTATUS(status))
256: error("Shell exited with status %d", WEXITSTATUS(status));
257: }
258:
259: static void
260: local_do_ls(const char *args)
261: {
262: if (!args || !*args)
263: local_do_shell(_PATH_LS);
264: else {
265: int len = strlen(_PATH_LS " ") + strlen(args) + 1;
266: char *buf = xmalloc(len);
267:
268: /* XXX: quoting - rip quoting code from ftp? */
269: snprintf(buf, len, _PATH_LS " %s", args);
270: local_do_shell(buf);
271: xfree(buf);
272: }
273: }
274:
275: /* Strip one path (usually the pwd) from the start of another */
276: static char *
277: path_strip(char *path, char *strip)
278: {
279: size_t len;
280:
281: if (strip == NULL)
282: return (xstrdup(path));
283:
284: len = strlen(strip);
1.59 djm 285: if (strncmp(path, strip, len) == 0) {
1.44 djm 286: if (strip[len - 1] != '/' && path[len] == '/')
287: len++;
288: return (xstrdup(path + len));
289: }
290:
291: return (xstrdup(path));
292: }
293:
294: static char *
295: path_append(char *p1, char *p2)
296: {
297: char *ret;
1.94 ray 298: size_t len = strlen(p1) + strlen(p2) + 2;
1.44 djm 299:
300: ret = xmalloc(len);
301: strlcpy(ret, p1, len);
1.94 ray 302: if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1.44 djm 303: strlcat(ret, "/", len);
304: strlcat(ret, p2, len);
305:
306: return(ret);
307: }
308:
309: static char *
310: make_absolute(char *p, char *pwd)
311: {
1.51 avsm 312: char *abs_str;
1.44 djm 313:
314: /* Derelativise */
315: if (p && p[0] != '/') {
1.51 avsm 316: abs_str = path_append(pwd, p);
1.44 djm 317: xfree(p);
1.51 avsm 318: return(abs_str);
1.44 djm 319: } else
320: return(p);
321: }
322:
323: static int
324: infer_path(const char *p, char **ifp)
325: {
326: char *cp;
327:
328: cp = strrchr(p, '/');
329: if (cp == NULL) {
330: *ifp = xstrdup(p);
331: return(0);
332: }
333:
334: if (!cp[1]) {
335: error("Invalid path");
336: return(-1);
337: }
338:
339: *ifp = xstrdup(cp + 1);
340: return(0);
341: }
342:
343: static int
1.97 djm 344: parse_getput_flags(const char *cmd, char **argv, int argc, int *pflag)
1.44 djm 345: {
1.102 martynas 346: extern int opterr, optind, optopt, optreset;
1.97 djm 347: int ch;
1.44 djm 348:
1.97 djm 349: optind = optreset = 1;
350: opterr = 0;
351:
352: *pflag = 0;
353: while ((ch = getopt(argc, argv, "Pp")) != -1) {
354: switch (ch) {
1.44 djm 355: case 'p':
356: case 'P':
357: *pflag = 1;
358: break;
359: default:
1.102 martynas 360: error("%s: Invalid flag -%c", cmd, optopt);
1.97 djm 361: return -1;
1.44 djm 362: }
363: }
364:
1.97 djm 365: return optind;
1.44 djm 366: }
367:
368: static int
1.97 djm 369: parse_ls_flags(char **argv, int argc, int *lflag)
1.44 djm 370: {
1.102 martynas 371: extern int opterr, optind, optopt, optreset;
1.97 djm 372: int ch;
373:
374: optind = optreset = 1;
375: opterr = 0;
1.44 djm 376:
1.53 djm 377: *lflag = LS_NAME_SORT;
1.97 djm 378: while ((ch = getopt(argc, argv, "1Saflnrt")) != -1) {
379: switch (ch) {
380: case '1':
381: *lflag &= ~VIEW_FLAGS;
382: *lflag |= LS_SHORT_VIEW;
383: break;
384: case 'S':
385: *lflag &= ~SORT_FLAGS;
386: *lflag |= LS_SIZE_SORT;
387: break;
388: case 'a':
389: *lflag |= LS_SHOW_ALL;
390: break;
391: case 'f':
392: *lflag &= ~SORT_FLAGS;
393: break;
394: case 'l':
395: *lflag &= ~VIEW_FLAGS;
396: *lflag |= LS_LONG_VIEW;
397: break;
398: case 'n':
399: *lflag &= ~VIEW_FLAGS;
400: *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
401: break;
402: case 'r':
403: *lflag |= LS_REVERSE_SORT;
404: break;
405: case 't':
406: *lflag &= ~SORT_FLAGS;
407: *lflag |= LS_TIME_SORT;
408: break;
409: default:
1.102 martynas 410: error("ls: Invalid flag -%c", optopt);
1.97 djm 411: return -1;
1.44 djm 412: }
413: }
414:
1.97 djm 415: return optind;
1.44 djm 416: }
417:
418: static int
1.100 djm 419: parse_df_flags(const char *cmd, char **argv, int argc, int *hflag, int *iflag)
420: {
1.102 martynas 421: extern int opterr, optind, optopt, optreset;
1.100 djm 422: int ch;
423:
424: optind = optreset = 1;
425: opterr = 0;
426:
427: *hflag = *iflag = 0;
428: while ((ch = getopt(argc, argv, "hi")) != -1) {
429: switch (ch) {
430: case 'h':
431: *hflag = 1;
432: break;
433: case 'i':
434: *iflag = 1;
435: break;
436: default:
1.102 martynas 437: error("%s: Invalid flag -%c", cmd, optopt);
1.100 djm 438: return -1;
439: }
440: }
441:
442: return optind;
443: }
444:
445: static int
1.44 djm 446: is_dir(char *path)
447: {
448: struct stat sb;
449:
450: /* XXX: report errors? */
451: if (stat(path, &sb) == -1)
452: return(0);
453:
1.92 otto 454: return(S_ISDIR(sb.st_mode));
1.44 djm 455: }
456:
457: static int
458: remote_is_dir(struct sftp_conn *conn, char *path)
459: {
460: Attrib *a;
461:
462: /* XXX: report errors? */
463: if ((a = do_stat(conn, path, 1)) == NULL)
464: return(0);
465: if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
466: return(0);
1.92 otto 467: return(S_ISDIR(a->perm));
1.44 djm 468: }
469:
470: static int
471: process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
472: {
473: char *abs_src = NULL;
474: char *abs_dst = NULL;
475: char *tmp;
476: glob_t g;
477: int err = 0;
478: int i;
479:
480: abs_src = xstrdup(src);
481: abs_src = make_absolute(abs_src, pwd);
482:
483: memset(&g, 0, sizeof(g));
484: debug3("Looking up %s", abs_src);
485: if (remote_glob(conn, abs_src, 0, NULL, &g)) {
486: error("File \"%s\" not found.", abs_src);
487: err = -1;
488: goto out;
489: }
490:
491: /* If multiple matches, dst must be a directory or unspecified */
492: if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
493: error("Multiple files match, but \"%s\" is not a directory",
494: dst);
495: err = -1;
496: goto out;
497: }
498:
1.46 djm 499: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.44 djm 500: if (infer_path(g.gl_pathv[i], &tmp)) {
501: err = -1;
502: goto out;
503: }
504:
505: if (g.gl_matchc == 1 && dst) {
506: /* If directory specified, append filename */
1.82 markus 507: xfree(tmp);
1.44 djm 508: if (is_dir(dst)) {
509: if (infer_path(g.gl_pathv[0], &tmp)) {
510: err = 1;
511: goto out;
512: }
513: abs_dst = path_append(dst, tmp);
514: xfree(tmp);
515: } else
516: abs_dst = xstrdup(dst);
517: } else if (dst) {
518: abs_dst = path_append(dst, tmp);
519: xfree(tmp);
520: } else
521: abs_dst = tmp;
522:
523: printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
524: if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
525: err = -1;
526: xfree(abs_dst);
527: abs_dst = NULL;
528: }
529:
530: out:
531: xfree(abs_src);
532: globfree(&g);
533: return(err);
534: }
535:
536: static int
537: process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
538: {
539: char *tmp_dst = NULL;
540: char *abs_dst = NULL;
541: char *tmp;
542: glob_t g;
543: int err = 0;
544: int i;
1.99 djm 545: struct stat sb;
1.44 djm 546:
547: if (dst) {
548: tmp_dst = xstrdup(dst);
549: tmp_dst = make_absolute(tmp_dst, pwd);
550: }
551:
552: memset(&g, 0, sizeof(g));
553: debug3("Looking up %s", src);
1.99 djm 554: if (glob(src, GLOB_NOCHECK, NULL, &g)) {
1.44 djm 555: error("File \"%s\" not found.", src);
556: err = -1;
557: goto out;
558: }
559:
560: /* If multiple matches, dst may be directory or unspecified */
561: if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
562: error("Multiple files match, but \"%s\" is not a directory",
563: tmp_dst);
564: err = -1;
565: goto out;
566: }
567:
1.46 djm 568: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.99 djm 569: if (stat(g.gl_pathv[i], &sb) == -1) {
570: err = -1;
571: error("stat %s: %s", g.gl_pathv[i], strerror(errno));
572: continue;
573: }
574:
575: if (!S_ISREG(sb.st_mode)) {
1.44 djm 576: error("skipping non-regular file %s",
577: g.gl_pathv[i]);
578: continue;
579: }
580: if (infer_path(g.gl_pathv[i], &tmp)) {
581: err = -1;
582: goto out;
583: }
584:
585: if (g.gl_matchc == 1 && tmp_dst) {
586: /* If directory specified, append filename */
587: if (remote_is_dir(conn, tmp_dst)) {
588: if (infer_path(g.gl_pathv[0], &tmp)) {
589: err = 1;
590: goto out;
591: }
592: abs_dst = path_append(tmp_dst, tmp);
593: xfree(tmp);
594: } else
595: abs_dst = xstrdup(tmp_dst);
596:
597: } else if (tmp_dst) {
598: abs_dst = path_append(tmp_dst, tmp);
599: xfree(tmp);
600: } else
601: abs_dst = make_absolute(tmp, pwd);
602:
603: printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
604: if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
605: err = -1;
606: }
607:
608: out:
609: if (abs_dst)
610: xfree(abs_dst);
611: if (tmp_dst)
612: xfree(tmp_dst);
613: globfree(&g);
614: return(err);
615: }
616:
617: static int
618: sdirent_comp(const void *aa, const void *bb)
619: {
620: SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
621: SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
1.53 djm 622: int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
1.44 djm 623:
1.52 djm 624: #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
1.53 djm 625: if (sort_flag & LS_NAME_SORT)
1.52 djm 626: return (rmul * strcmp(a->filename, b->filename));
1.53 djm 627: else if (sort_flag & LS_TIME_SORT)
1.52 djm 628: return (rmul * NCMP(a->a.mtime, b->a.mtime));
1.53 djm 629: else if (sort_flag & LS_SIZE_SORT)
1.52 djm 630: return (rmul * NCMP(a->a.size, b->a.size));
631:
632: fatal("Unknown ls sort type");
1.44 djm 633: }
634:
635: /* sftp ls.1 replacement for directories */
636: static int
637: do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
638: {
1.64 djm 639: int n;
640: u_int c = 1, colspace = 0, columns = 1;
1.44 djm 641: SFTP_DIRENT **d;
642:
643: if ((n = do_readdir(conn, path, &d)) != 0)
644: return (n);
645:
1.53 djm 646: if (!(lflag & LS_SHORT_VIEW)) {
1.64 djm 647: u_int m = 0, width = 80;
1.44 djm 648: struct winsize ws;
649: char *tmp;
650:
651: /* Count entries for sort and find longest filename */
1.54 djm 652: for (n = 0; d[n] != NULL; n++) {
653: if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL))
654: m = MAX(m, strlen(d[n]->filename));
655: }
1.44 djm 656:
657: /* Add any subpath that also needs to be counted */
658: tmp = path_strip(path, strip_path);
659: m += strlen(tmp);
660: xfree(tmp);
661:
662: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
663: width = ws.ws_col;
664:
665: columns = width / (m + 2);
666: columns = MAX(columns, 1);
667: colspace = width / columns;
668: colspace = MIN(colspace, width);
669: }
670:
1.52 djm 671: if (lflag & SORT_FLAGS) {
1.68 dtucker 672: for (n = 0; d[n] != NULL; n++)
673: ; /* count entries */
1.53 djm 674: sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
1.52 djm 675: qsort(d, n, sizeof(*d), sdirent_comp);
676: }
1.44 djm 677:
1.46 djm 678: for (n = 0; d[n] != NULL && !interrupted; n++) {
1.44 djm 679: char *tmp, *fname;
1.54 djm 680:
681: if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL))
682: continue;
1.44 djm 683:
684: tmp = path_append(path, d[n]->filename);
685: fname = path_strip(tmp, strip_path);
686: xfree(tmp);
687:
1.53 djm 688: if (lflag & LS_LONG_VIEW) {
689: if (lflag & LS_NUMERIC_VIEW) {
1.50 djm 690: char *lname;
691: struct stat sb;
692:
693: memset(&sb, 0, sizeof(sb));
694: attrib_to_stat(&d[n]->a, &sb);
695: lname = ls_file(fname, &sb, 1);
696: printf("%s\n", lname);
697: xfree(lname);
698: } else
699: printf("%s\n", d[n]->longname);
1.44 djm 700: } else {
701: printf("%-*s", colspace, fname);
702: if (c >= columns) {
703: printf("\n");
704: c = 1;
705: } else
706: c++;
707: }
708:
709: xfree(fname);
710: }
711:
1.53 djm 712: if (!(lflag & LS_LONG_VIEW) && (c != 1))
1.44 djm 713: printf("\n");
714:
715: free_sftp_dirents(d);
716: return (0);
717: }
718:
719: /* sftp ls.1 replacement which handles path globs */
720: static int
721: do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
722: int lflag)
723: {
724: glob_t g;
1.64 djm 725: u_int i, c = 1, colspace = 0, columns = 1;
1.60 fgsch 726: Attrib *a = NULL;
1.44 djm 727:
728: memset(&g, 0, sizeof(g));
729:
730: if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
1.60 fgsch 731: NULL, &g) || (g.gl_pathc && !g.gl_matchc)) {
732: if (g.gl_pathc)
733: globfree(&g);
1.44 djm 734: error("Can't ls: \"%s\" not found", path);
735: return (-1);
736: }
737:
1.46 djm 738: if (interrupted)
739: goto out;
740:
1.44 djm 741: /*
1.60 fgsch 742: * If the glob returns a single match and it is a directory,
743: * then just list its contents.
1.44 djm 744: */
1.60 fgsch 745: if (g.gl_matchc == 1) {
746: if ((a = do_lstat(conn, g.gl_pathv[0], 1)) == NULL) {
1.44 djm 747: globfree(&g);
748: return (-1);
749: }
750: if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
751: S_ISDIR(a->perm)) {
1.60 fgsch 752: int err;
753:
754: err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag);
1.44 djm 755: globfree(&g);
1.60 fgsch 756: return (err);
1.44 djm 757: }
758: }
759:
1.53 djm 760: if (!(lflag & LS_SHORT_VIEW)) {
1.64 djm 761: u_int m = 0, width = 80;
1.44 djm 762: struct winsize ws;
763:
764: /* Count entries for sort and find longest filename */
765: for (i = 0; g.gl_pathv[i]; i++)
766: m = MAX(m, strlen(g.gl_pathv[i]));
767:
768: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
769: width = ws.ws_col;
770:
771: columns = width / (m + 2);
772: columns = MAX(columns, 1);
773: colspace = width / columns;
774: }
775:
1.60 fgsch 776: for (i = 0; g.gl_pathv[i] && !interrupted; i++, a = NULL) {
1.44 djm 777: char *fname;
778:
779: fname = path_strip(g.gl_pathv[i], strip_path);
780:
1.53 djm 781: if (lflag & LS_LONG_VIEW) {
1.44 djm 782: char *lname;
783: struct stat sb;
784:
785: /*
786: * XXX: this is slow - 1 roundtrip per path
787: * A solution to this is to fork glob() and
788: * build a sftp specific version which keeps the
789: * attribs (which currently get thrown away)
790: * that the server returns as well as the filenames.
791: */
792: memset(&sb, 0, sizeof(sb));
1.60 fgsch 793: if (a == NULL)
794: a = do_lstat(conn, g.gl_pathv[i], 1);
1.44 djm 795: if (a != NULL)
796: attrib_to_stat(a, &sb);
797: lname = ls_file(fname, &sb, 1);
798: printf("%s\n", lname);
799: xfree(lname);
800: } else {
801: printf("%-*s", colspace, fname);
802: if (c >= columns) {
803: printf("\n");
804: c = 1;
805: } else
806: c++;
807: }
808: xfree(fname);
809: }
810:
1.53 djm 811: if (!(lflag & LS_LONG_VIEW) && (c != 1))
1.44 djm 812: printf("\n");
813:
1.46 djm 814: out:
1.44 djm 815: if (g.gl_pathc)
816: globfree(&g);
817:
818: return (0);
819: }
820:
1.100 djm 821: static int
822: do_df(struct sftp_conn *conn, char *path, int hflag, int iflag)
823: {
1.101 dtucker 824: struct sftp_statvfs st;
1.100 djm 825: char s_used[FMT_SCALED_STRSIZE];
826: char s_avail[FMT_SCALED_STRSIZE];
827: char s_root[FMT_SCALED_STRSIZE];
828: char s_total[FMT_SCALED_STRSIZE];
829:
830: if (do_statvfs(conn, path, &st, 1) == -1)
831: return -1;
832: if (iflag) {
833: printf(" Inodes Used Avail "
834: "(root) %%Capacity\n");
835: printf("%11llu %11llu %11llu %11llu %3llu%%\n",
836: (unsigned long long)st.f_files,
837: (unsigned long long)(st.f_files - st.f_ffree),
838: (unsigned long long)st.f_favail,
839: (unsigned long long)st.f_ffree,
840: (unsigned long long)(100 * (st.f_files - st.f_ffree) /
841: st.f_files));
842: } else if (hflag) {
843: strlcpy(s_used, "error", sizeof(s_used));
844: strlcpy(s_avail, "error", sizeof(s_avail));
845: strlcpy(s_root, "error", sizeof(s_root));
846: strlcpy(s_total, "error", sizeof(s_total));
847: fmt_scaled((st.f_blocks - st.f_bfree) * st.f_frsize, s_used);
848: fmt_scaled(st.f_bavail * st.f_frsize, s_avail);
849: fmt_scaled(st.f_bfree * st.f_frsize, s_root);
850: fmt_scaled(st.f_blocks * st.f_frsize, s_total);
851: printf(" Size Used Avail (root) %%Capacity\n");
852: printf("%7sB %7sB %7sB %7sB %3llu%%\n",
853: s_total, s_used, s_avail, s_root,
854: (unsigned long long)(100 * (st.f_blocks - st.f_bfree) /
855: st.f_blocks));
856: } else {
857: printf(" Size Used Avail "
858: "(root) %%Capacity\n");
859: printf("%12llu %12llu %12llu %12llu %3llu%%\n",
860: (unsigned long long)(st.f_frsize * st.f_blocks / 1024),
861: (unsigned long long)(st.f_frsize *
862: (st.f_blocks - st.f_bfree) / 1024),
863: (unsigned long long)(st.f_frsize * st.f_bavail / 1024),
864: (unsigned long long)(st.f_frsize * st.f_bfree / 1024),
865: (unsigned long long)(100 * (st.f_blocks - st.f_bfree) /
866: st.f_blocks));
867: }
868: return 0;
869: }
870:
1.97 djm 871: /*
872: * Undo escaping of glob sequences in place. Used to undo extra escaping
873: * applied in makeargv() when the string is destined for a function that
874: * does not glob it.
875: */
876: static void
877: undo_glob_escape(char *s)
878: {
879: size_t i, j;
880:
881: for (i = j = 0;;) {
882: if (s[i] == '\0') {
883: s[j] = '\0';
884: return;
885: }
886: if (s[i] != '\\') {
887: s[j++] = s[i++];
888: continue;
889: }
890: /* s[i] == '\\' */
891: ++i;
892: switch (s[i]) {
893: case '?':
894: case '[':
895: case '*':
896: case '\\':
897: s[j++] = s[i++];
898: break;
899: case '\0':
900: s[j++] = '\\';
901: s[j] = '\0';
902: return;
903: default:
904: s[j++] = '\\';
905: s[j++] = s[i++];
906: break;
907: }
908: }
909: }
910:
911: /*
912: * Split a string into an argument vector using sh(1)-style quoting,
913: * comment and escaping rules, but with some tweaks to handle glob(3)
914: * wildcards.
915: * Returns NULL on error or a NULL-terminated array of arguments.
916: */
917: #define MAXARGS 128
918: #define MAXARGLEN 8192
919: static char **
920: makeargv(const char *arg, int *argcp)
921: {
922: int argc, quot;
923: size_t i, j;
924: static char argvs[MAXARGLEN];
925: static char *argv[MAXARGS + 1];
926: enum { MA_START, MA_SQUOTE, MA_DQUOTE, MA_UNQUOTED } state, q;
927:
928: *argcp = argc = 0;
929: if (strlen(arg) > sizeof(argvs) - 1) {
930: args_too_longs:
931: error("string too long");
932: return NULL;
933: }
934: state = MA_START;
935: i = j = 0;
936: for (;;) {
937: if (isspace(arg[i])) {
938: if (state == MA_UNQUOTED) {
939: /* Terminate current argument */
940: argvs[j++] = '\0';
941: argc++;
942: state = MA_START;
943: } else if (state != MA_START)
944: argvs[j++] = arg[i];
945: } else if (arg[i] == '"' || arg[i] == '\'') {
946: q = arg[i] == '"' ? MA_DQUOTE : MA_SQUOTE;
947: if (state == MA_START) {
948: argv[argc] = argvs + j;
949: state = q;
950: } else if (state == MA_UNQUOTED)
951: state = q;
952: else if (state == q)
953: state = MA_UNQUOTED;
954: else
955: argvs[j++] = arg[i];
956: } else if (arg[i] == '\\') {
957: if (state == MA_SQUOTE || state == MA_DQUOTE) {
958: quot = state == MA_SQUOTE ? '\'' : '"';
959: /* Unescape quote we are in */
960: /* XXX support \n and friends? */
961: if (arg[i + 1] == quot) {
962: i++;
963: argvs[j++] = arg[i];
964: } else if (arg[i + 1] == '?' ||
965: arg[i + 1] == '[' || arg[i + 1] == '*') {
966: /*
967: * Special case for sftp: append
968: * double-escaped glob sequence -
969: * glob will undo one level of
970: * escaping. NB. string can grow here.
971: */
972: if (j >= sizeof(argvs) - 5)
973: goto args_too_longs;
974: argvs[j++] = '\\';
975: argvs[j++] = arg[i++];
976: argvs[j++] = '\\';
977: argvs[j++] = arg[i];
978: } else {
979: argvs[j++] = arg[i++];
980: argvs[j++] = arg[i];
981: }
982: } else {
983: if (state == MA_START) {
984: argv[argc] = argvs + j;
985: state = MA_UNQUOTED;
986: }
987: if (arg[i + 1] == '?' || arg[i + 1] == '[' ||
988: arg[i + 1] == '*' || arg[i + 1] == '\\') {
989: /*
990: * Special case for sftp: append
991: * escaped glob sequence -
992: * glob will undo one level of
993: * escaping.
994: */
995: argvs[j++] = arg[i++];
996: argvs[j++] = arg[i];
997: } else {
998: /* Unescape everything */
999: /* XXX support \n and friends? */
1000: i++;
1001: argvs[j++] = arg[i];
1002: }
1003: }
1004: } else if (arg[i] == '#') {
1005: if (state == MA_SQUOTE || state == MA_DQUOTE)
1006: argvs[j++] = arg[i];
1007: else
1008: goto string_done;
1009: } else if (arg[i] == '\0') {
1010: if (state == MA_SQUOTE || state == MA_DQUOTE) {
1011: error("Unterminated quoted argument");
1012: return NULL;
1013: }
1014: string_done:
1015: if (state == MA_UNQUOTED) {
1016: argvs[j++] = '\0';
1017: argc++;
1018: }
1019: break;
1020: } else {
1021: if (state == MA_START) {
1022: argv[argc] = argvs + j;
1023: state = MA_UNQUOTED;
1024: }
1025: if ((state == MA_SQUOTE || state == MA_DQUOTE) &&
1026: (arg[i] == '?' || arg[i] == '[' || arg[i] == '*')) {
1027: /*
1028: * Special case for sftp: escape quoted
1029: * glob(3) wildcards. NB. string can grow
1030: * here.
1031: */
1032: if (j >= sizeof(argvs) - 3)
1033: goto args_too_longs;
1034: argvs[j++] = '\\';
1035: argvs[j++] = arg[i];
1036: } else
1037: argvs[j++] = arg[i];
1038: }
1039: i++;
1040: }
1041: *argcp = argc;
1042: return argv;
1043: }
1044:
1.44 djm 1045: static int
1.100 djm 1046: parse_args(const char **cpp, int *pflag, int *lflag, int *iflag, int *hflag,
1.44 djm 1047: unsigned long *n_arg, char **path1, char **path2)
1048: {
1049: const char *cmd, *cp = *cpp;
1.97 djm 1050: char *cp2, **argv;
1.44 djm 1051: int base = 0;
1052: long l;
1.97 djm 1053: int i, cmdnum, optidx, argc;
1.44 djm 1054:
1055: /* Skip leading whitespace */
1056: cp = cp + strspn(cp, WHITESPACE);
1057:
1058: /* Ignore blank lines and lines which begin with comment '#' char */
1059: if (*cp == '\0' || *cp == '#')
1060: return (0);
1061:
1062: /* Check for leading '-' (disable error processing) */
1063: *iflag = 0;
1064: if (*cp == '-') {
1065: *iflag = 1;
1066: cp++;
1067: }
1068:
1.97 djm 1069: if ((argv = makeargv(cp, &argc)) == NULL)
1070: return -1;
1071:
1.44 djm 1072: /* Figure out which command we have */
1.97 djm 1073: for (i = 0; cmds[i].c != NULL; i++) {
1074: if (strcasecmp(cmds[i].c, argv[0]) == 0)
1.44 djm 1075: break;
1076: }
1077: cmdnum = cmds[i].n;
1078: cmd = cmds[i].c;
1079:
1080: /* Special case */
1081: if (*cp == '!') {
1082: cp++;
1083: cmdnum = I_SHELL;
1084: } else if (cmdnum == -1) {
1085: error("Invalid command.");
1.97 djm 1086: return -1;
1.44 djm 1087: }
1088:
1089: /* Get arguments and parse flags */
1.100 djm 1090: *lflag = *pflag = *hflag = *n_arg = 0;
1.44 djm 1091: *path1 = *path2 = NULL;
1.97 djm 1092: optidx = 1;
1.44 djm 1093: switch (cmdnum) {
1094: case I_GET:
1095: case I_PUT:
1.97 djm 1096: if ((optidx = parse_getput_flags(cmd, argv, argc, pflag)) == -1)
1097: return -1;
1.44 djm 1098: /* Get first pathname (mandatory) */
1.97 djm 1099: if (argc - optidx < 1) {
1.44 djm 1100: error("You must specify at least one path after a "
1101: "%s command.", cmd);
1.97 djm 1102: return -1;
1103: }
1104: *path1 = xstrdup(argv[optidx]);
1105: /* Get second pathname (optional) */
1106: if (argc - optidx > 1) {
1107: *path2 = xstrdup(argv[optidx + 1]);
1108: /* Destination is not globbed */
1109: undo_glob_escape(*path2);
1.44 djm 1110: }
1111: break;
1112: case I_RENAME:
1113: case I_SYMLINK:
1.97 djm 1114: if (argc - optidx < 2) {
1.44 djm 1115: error("You must specify two paths after a %s "
1116: "command.", cmd);
1.97 djm 1117: return -1;
1.44 djm 1118: }
1.97 djm 1119: *path1 = xstrdup(argv[optidx]);
1120: *path2 = xstrdup(argv[optidx + 1]);
1121: /* Paths are not globbed */
1122: undo_glob_escape(*path1);
1123: undo_glob_escape(*path2);
1.44 djm 1124: break;
1125: case I_RM:
1126: case I_MKDIR:
1127: case I_RMDIR:
1128: case I_CHDIR:
1129: case I_LCHDIR:
1130: case I_LMKDIR:
1131: /* Get pathname (mandatory) */
1.97 djm 1132: if (argc - optidx < 1) {
1.44 djm 1133: error("You must specify a path after a %s command.",
1134: cmd);
1.97 djm 1135: return -1;
1.44 djm 1136: }
1.97 djm 1137: *path1 = xstrdup(argv[optidx]);
1138: /* Only "rm" globs */
1139: if (cmdnum != I_RM)
1140: undo_glob_escape(*path1);
1.44 djm 1141: break;
1.100 djm 1142: case I_DF:
1143: if ((optidx = parse_df_flags(cmd, argv, argc, hflag,
1144: iflag)) == -1)
1145: return -1;
1146: /* Default to current directory if no path specified */
1147: if (argc - optidx < 1)
1148: *path1 = NULL;
1149: else {
1150: *path1 = xstrdup(argv[optidx]);
1151: undo_glob_escape(*path1);
1152: }
1153: break;
1.44 djm 1154: case I_LS:
1.97 djm 1155: if ((optidx = parse_ls_flags(argv, argc, lflag)) == -1)
1.44 djm 1156: return(-1);
1157: /* Path is optional */
1.97 djm 1158: if (argc - optidx > 0)
1159: *path1 = xstrdup(argv[optidx]);
1.44 djm 1160: break;
1161: case I_LLS:
1.98 djm 1162: /* Skip ls command and following whitespace */
1163: cp = cp + strlen(cmd) + strspn(cp, WHITESPACE);
1.44 djm 1164: case I_SHELL:
1165: /* Uses the rest of the line */
1166: break;
1167: case I_LUMASK:
1168: case I_CHMOD:
1169: base = 8;
1170: case I_CHOWN:
1171: case I_CHGRP:
1172: /* Get numeric arg (mandatory) */
1.97 djm 1173: if (argc - optidx < 1)
1174: goto need_num_arg;
1.93 ray 1175: errno = 0;
1.97 djm 1176: l = strtol(argv[optidx], &cp2, base);
1177: if (cp2 == argv[optidx] || *cp2 != '\0' ||
1178: ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
1179: l < 0) {
1180: need_num_arg:
1.44 djm 1181: error("You must supply a numeric argument "
1182: "to the %s command.", cmd);
1.97 djm 1183: return -1;
1.44 djm 1184: }
1185: *n_arg = l;
1.97 djm 1186: if (cmdnum == I_LUMASK)
1.44 djm 1187: break;
1188: /* Get pathname (mandatory) */
1.97 djm 1189: if (argc - optidx < 2) {
1.44 djm 1190: error("You must specify a path after a %s command.",
1191: cmd);
1.97 djm 1192: return -1;
1.44 djm 1193: }
1.97 djm 1194: *path1 = xstrdup(argv[optidx + 1]);
1.44 djm 1195: break;
1196: case I_QUIT:
1197: case I_PWD:
1198: case I_LPWD:
1199: case I_HELP:
1200: case I_VERSION:
1201: case I_PROGRESS:
1202: break;
1203: default:
1204: fatal("Command not implemented");
1205: }
1206:
1207: *cpp = cp;
1208: return(cmdnum);
1209: }
1210:
1211: static int
1212: parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
1213: int err_abort)
1214: {
1215: char *path1, *path2, *tmp;
1.100 djm 1216: int pflag, lflag, iflag, hflag, cmdnum, i;
1.44 djm 1217: unsigned long n_arg;
1218: Attrib a, *aa;
1219: char path_buf[MAXPATHLEN];
1220: int err = 0;
1221: glob_t g;
1222:
1223: path1 = path2 = NULL;
1.100 djm 1224: cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &hflag, &n_arg,
1.44 djm 1225: &path1, &path2);
1226:
1227: if (iflag != 0)
1228: err_abort = 0;
1229:
1230: memset(&g, 0, sizeof(g));
1231:
1232: /* Perform command */
1233: switch (cmdnum) {
1234: case 0:
1235: /* Blank line */
1236: break;
1237: case -1:
1238: /* Unrecognized command */
1239: err = -1;
1240: break;
1241: case I_GET:
1242: err = process_get(conn, path1, path2, *pwd, pflag);
1243: break;
1244: case I_PUT:
1245: err = process_put(conn, path1, path2, *pwd, pflag);
1246: break;
1247: case I_RENAME:
1248: path1 = make_absolute(path1, *pwd);
1249: path2 = make_absolute(path2, *pwd);
1250: err = do_rename(conn, path1, path2);
1251: break;
1252: case I_SYMLINK:
1253: path2 = make_absolute(path2, *pwd);
1254: err = do_symlink(conn, path1, path2);
1255: break;
1256: case I_RM:
1257: path1 = make_absolute(path1, *pwd);
1258: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46 djm 1259: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.44 djm 1260: printf("Removing %s\n", g.gl_pathv[i]);
1261: err = do_rm(conn, g.gl_pathv[i]);
1262: if (err != 0 && err_abort)
1263: break;
1264: }
1265: break;
1266: case I_MKDIR:
1267: path1 = make_absolute(path1, *pwd);
1268: attrib_clear(&a);
1269: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1270: a.perm = 0777;
1271: err = do_mkdir(conn, path1, &a);
1272: break;
1273: case I_RMDIR:
1274: path1 = make_absolute(path1, *pwd);
1275: err = do_rmdir(conn, path1);
1276: break;
1277: case I_CHDIR:
1278: path1 = make_absolute(path1, *pwd);
1279: if ((tmp = do_realpath(conn, path1)) == NULL) {
1280: err = 1;
1281: break;
1282: }
1283: if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1284: xfree(tmp);
1285: err = 1;
1286: break;
1287: }
1288: if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
1289: error("Can't change directory: Can't check target");
1290: xfree(tmp);
1291: err = 1;
1292: break;
1293: }
1294: if (!S_ISDIR(aa->perm)) {
1295: error("Can't change directory: \"%s\" is not "
1296: "a directory", tmp);
1297: xfree(tmp);
1298: err = 1;
1299: break;
1300: }
1301: xfree(*pwd);
1302: *pwd = tmp;
1303: break;
1304: case I_LS:
1305: if (!path1) {
1306: do_globbed_ls(conn, *pwd, *pwd, lflag);
1307: break;
1308: }
1309:
1310: /* Strip pwd off beginning of non-absolute paths */
1311: tmp = NULL;
1312: if (*path1 != '/')
1313: tmp = *pwd;
1314:
1315: path1 = make_absolute(path1, *pwd);
1316: err = do_globbed_ls(conn, path1, tmp, lflag);
1.100 djm 1317: break;
1318: case I_DF:
1319: /* Default to current directory if no path specified */
1320: if (path1 == NULL)
1321: path1 = xstrdup(*pwd);
1322: path1 = make_absolute(path1, *pwd);
1323: err = do_df(conn, path1, hflag, iflag);
1.44 djm 1324: break;
1325: case I_LCHDIR:
1326: if (chdir(path1) == -1) {
1327: error("Couldn't change local directory to "
1328: "\"%s\": %s", path1, strerror(errno));
1329: err = 1;
1330: }
1331: break;
1332: case I_LMKDIR:
1333: if (mkdir(path1, 0777) == -1) {
1334: error("Couldn't create local directory "
1335: "\"%s\": %s", path1, strerror(errno));
1336: err = 1;
1337: }
1338: break;
1339: case I_LLS:
1340: local_do_ls(cmd);
1341: break;
1342: case I_SHELL:
1343: local_do_shell(cmd);
1344: break;
1345: case I_LUMASK:
1346: umask(n_arg);
1347: printf("Local umask: %03lo\n", n_arg);
1348: break;
1349: case I_CHMOD:
1350: path1 = make_absolute(path1, *pwd);
1351: attrib_clear(&a);
1352: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1353: a.perm = n_arg;
1354: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46 djm 1355: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.44 djm 1356: printf("Changing mode on %s\n", g.gl_pathv[i]);
1357: err = do_setstat(conn, g.gl_pathv[i], &a);
1358: if (err != 0 && err_abort)
1359: break;
1360: }
1361: break;
1362: case I_CHOWN:
1363: case I_CHGRP:
1364: path1 = make_absolute(path1, *pwd);
1365: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46 djm 1366: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.44 djm 1367: if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
1.104 djm 1368: if (err_abort) {
1369: err = -1;
1.44 djm 1370: break;
1.104 djm 1371: } else
1.44 djm 1372: continue;
1373: }
1374: if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1375: error("Can't get current ownership of "
1376: "remote file \"%s\"", g.gl_pathv[i]);
1.104 djm 1377: if (err_abort) {
1378: err = -1;
1.44 djm 1379: break;
1.104 djm 1380: } else
1.44 djm 1381: continue;
1382: }
1383: aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1384: if (cmdnum == I_CHOWN) {
1385: printf("Changing owner on %s\n", g.gl_pathv[i]);
1386: aa->uid = n_arg;
1387: } else {
1388: printf("Changing group on %s\n", g.gl_pathv[i]);
1389: aa->gid = n_arg;
1390: }
1391: err = do_setstat(conn, g.gl_pathv[i], aa);
1392: if (err != 0 && err_abort)
1393: break;
1394: }
1395: break;
1396: case I_PWD:
1397: printf("Remote working directory: %s\n", *pwd);
1398: break;
1399: case I_LPWD:
1400: if (!getcwd(path_buf, sizeof(path_buf))) {
1401: error("Couldn't get local cwd: %s", strerror(errno));
1402: err = -1;
1403: break;
1404: }
1405: printf("Local working directory: %s\n", path_buf);
1406: break;
1407: case I_QUIT:
1408: /* Processed below */
1409: break;
1410: case I_HELP:
1411: help();
1412: break;
1413: case I_VERSION:
1414: printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1415: break;
1416: case I_PROGRESS:
1417: showprogress = !showprogress;
1418: if (showprogress)
1419: printf("Progress meter enabled\n");
1420: else
1421: printf("Progress meter disabled\n");
1422: break;
1423: default:
1424: fatal("%d is not implemented", cmdnum);
1425: }
1426:
1427: if (g.gl_pathc)
1428: globfree(&g);
1429: if (path1)
1430: xfree(path1);
1431: if (path2)
1432: xfree(path2);
1433:
1434: /* If an unignored error occurs in batch mode we should abort. */
1435: if (err_abort && err != 0)
1436: return (-1);
1437: else if (cmdnum == I_QUIT)
1438: return (1);
1439:
1440: return (0);
1441: }
1442:
1.57 djm 1443: static char *
1444: prompt(EditLine *el)
1445: {
1446: return ("sftp> ");
1447: }
1448:
1.44 djm 1449: int
1450: interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1451: {
1452: char *pwd;
1453: char *dir = NULL;
1454: char cmd[2048];
1455: struct sftp_conn *conn;
1.66 jaredy 1456: int err, interactive;
1.57 djm 1457: EditLine *el = NULL;
1458: History *hl = NULL;
1459: HistEvent hev;
1460: extern char *__progname;
1461:
1462: if (!batchmode && isatty(STDIN_FILENO)) {
1463: if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL)
1464: fatal("Couldn't initialise editline");
1465: if ((hl = history_init()) == NULL)
1466: fatal("Couldn't initialise editline history");
1467: history(hl, &hev, H_SETSIZE, 100);
1468: el_set(el, EL_HIST, history, hl);
1469:
1470: el_set(el, EL_PROMPT, prompt);
1471: el_set(el, EL_EDITOR, "emacs");
1472: el_set(el, EL_TERMINAL, NULL);
1473: el_set(el, EL_SIGNAL, 1);
1474: el_source(el, NULL);
1475: }
1.44 djm 1476:
1477: conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1478: if (conn == NULL)
1479: fatal("Couldn't initialise connection to server");
1480:
1481: pwd = do_realpath(conn, ".");
1482: if (pwd == NULL)
1483: fatal("Need cwd");
1484:
1485: if (file1 != NULL) {
1486: dir = xstrdup(file1);
1487: dir = make_absolute(dir, pwd);
1488:
1489: if (remote_is_dir(conn, dir) && file2 == NULL) {
1490: printf("Changing to: %s\n", dir);
1491: snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
1.58 markus 1492: if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) {
1493: xfree(dir);
1494: xfree(pwd);
1.76 djm 1495: xfree(conn);
1.44 djm 1496: return (-1);
1.58 markus 1497: }
1.44 djm 1498: } else {
1499: if (file2 == NULL)
1500: snprintf(cmd, sizeof cmd, "get %s", dir);
1501: else
1502: snprintf(cmd, sizeof cmd, "get %s %s", dir,
1503: file2);
1504:
1505: err = parse_dispatch_command(conn, cmd, &pwd, 1);
1506: xfree(dir);
1507: xfree(pwd);
1.76 djm 1508: xfree(conn);
1.44 djm 1509: return (err);
1510: }
1511: xfree(dir);
1512: }
1513:
1514: setvbuf(stdout, NULL, _IOLBF, 0);
1515: setvbuf(infile, NULL, _IOLBF, 0);
1516:
1.66 jaredy 1517: interactive = !batchmode && isatty(STDIN_FILENO);
1.44 djm 1518: err = 0;
1519: for (;;) {
1520: char *cp;
1.57 djm 1521: const char *line;
1522: int count = 0;
1.44 djm 1523:
1.46 djm 1524: signal(SIGINT, SIG_IGN);
1525:
1.57 djm 1526: if (el == NULL) {
1.66 jaredy 1527: if (interactive)
1528: printf("sftp> ");
1.57 djm 1529: if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1.66 jaredy 1530: if (interactive)
1531: printf("\n");
1.57 djm 1532: break;
1533: }
1.66 jaredy 1534: if (!interactive) { /* Echo command */
1535: printf("sftp> %s", cmd);
1536: if (strlen(cmd) > 0 &&
1537: cmd[strlen(cmd) - 1] != '\n')
1538: printf("\n");
1539: }
1.57 djm 1540: } else {
1.66 jaredy 1541: if ((line = el_gets(el, &count)) == NULL || count <= 0) {
1542: printf("\n");
1.57 djm 1543: break;
1.66 jaredy 1544: }
1.57 djm 1545: history(hl, &hev, H_ENTER, line);
1546: if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
1547: fprintf(stderr, "Error: input line too long\n");
1548: continue;
1549: }
1.44 djm 1550: }
1551:
1552: cp = strrchr(cmd, '\n');
1553: if (cp)
1554: *cp = '\0';
1555:
1.46 djm 1556: /* Handle user interrupts gracefully during commands */
1557: interrupted = 0;
1558: signal(SIGINT, cmd_interrupt);
1559:
1.44 djm 1560: err = parse_dispatch_command(conn, cmd, &pwd, batchmode);
1561: if (err != 0)
1562: break;
1563: }
1564: xfree(pwd);
1.76 djm 1565: xfree(conn);
1.66 jaredy 1566:
1567: if (el != NULL)
1568: el_end(el);
1.44 djm 1569:
1570: /* err == 1 signifies normal "quit" exit */
1571: return (err >= 0 ? 0 : -1);
1572: }
1.34 fgsch 1573:
1.18 itojun 1574: static void
1.36 djm 1575: connect_to_server(char *path, char **args, int *in, int *out)
1.1 djm 1576: {
1577: int c_in, c_out;
1.30 deraadt 1578:
1.1 djm 1579: int inout[2];
1.30 deraadt 1580:
1.1 djm 1581: if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
1582: fatal("socketpair: %s", strerror(errno));
1583: *in = *out = inout[0];
1584: c_in = c_out = inout[1];
1585:
1.36 djm 1586: if ((sshpid = fork()) == -1)
1.1 djm 1587: fatal("fork: %s", strerror(errno));
1.36 djm 1588: else if (sshpid == 0) {
1.1 djm 1589: if ((dup2(c_in, STDIN_FILENO) == -1) ||
1590: (dup2(c_out, STDOUT_FILENO) == -1)) {
1591: fprintf(stderr, "dup2: %s\n", strerror(errno));
1.47 djm 1592: _exit(1);
1.1 djm 1593: }
1594: close(*in);
1595: close(*out);
1596: close(c_in);
1597: close(c_out);
1.46 djm 1598:
1599: /*
1600: * The underlying ssh is in the same process group, so we must
1.56 deraadt 1601: * ignore SIGINT if we want to gracefully abort commands,
1602: * otherwise the signal will make it to the ssh process and
1.46 djm 1603: * kill it too
1604: */
1605: signal(SIGINT, SIG_IGN);
1.49 dtucker 1606: execvp(path, args);
1.23 djm 1607: fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
1.47 djm 1608: _exit(1);
1.1 djm 1609: }
1610:
1.36 djm 1611: signal(SIGTERM, killchild);
1612: signal(SIGINT, killchild);
1613: signal(SIGHUP, killchild);
1.1 djm 1614: close(c_in);
1615: close(c_out);
1616: }
1617:
1.18 itojun 1618: static void
1.1 djm 1619: usage(void)
1620: {
1.25 mpech 1621: extern char *__progname;
1.27 markus 1622:
1.19 stevesk 1623: fprintf(stderr,
1.38 jmc 1624: "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n"
1625: " [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n"
1626: " [-S program] [-s subsystem | sftp_server] host\n"
1.105 ! djm 1627: " %s [user@]host[:file ...]\n"
! 1628: " %s [user@]host[:dir[/]]\n"
1.38 jmc 1629: " %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname);
1.1 djm 1630: exit(1);
1631: }
1632:
1.2 stevesk 1633: int
1.1 djm 1634: main(int argc, char **argv)
1635: {
1.33 djm 1636: int in, out, ch, err;
1.48 pedro 1637: char *host, *userhost, *cp, *file2 = NULL;
1.17 mouring 1638: int debug_level = 0, sshver = 2;
1639: char *file1 = NULL, *sftp_server = NULL;
1.23 djm 1640: char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
1.17 mouring 1641: LogLevel ll = SYSLOG_LEVEL_INFO;
1642: arglist args;
1.3 djm 1643: extern int optind;
1644: extern char *optarg;
1.67 djm 1645:
1646: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1647: sanitise_stdfd();
1.1 djm 1648:
1.70 djm 1649: memset(&args, '\0', sizeof(args));
1.17 mouring 1650: args.list = NULL;
1.80 djm 1651: addargs(&args, "%s", ssh_program);
1.17 mouring 1652: addargs(&args, "-oForwardX11 no");
1653: addargs(&args, "-oForwardAgent no");
1.69 reyk 1654: addargs(&args, "-oPermitLocalCommand no");
1.21 stevesk 1655: addargs(&args, "-oClearAllForwardings yes");
1.40 djm 1656:
1.17 mouring 1657: ll = SYSLOG_LEVEL_INFO;
1.40 djm 1658: infile = stdin;
1.3 djm 1659:
1.26 djm 1660: while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
1.3 djm 1661: switch (ch) {
1662: case 'C':
1.17 mouring 1663: addargs(&args, "-C");
1.3 djm 1664: break;
1665: case 'v':
1.17 mouring 1666: if (debug_level < 3) {
1667: addargs(&args, "-v");
1668: ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
1669: }
1670: debug_level++;
1.3 djm 1671: break;
1.19 stevesk 1672: case 'F':
1.3 djm 1673: case 'o':
1.19 stevesk 1674: addargs(&args, "-%c%s", ch, optarg);
1.7 markus 1675: break;
1676: case '1':
1.17 mouring 1677: sshver = 1;
1.7 markus 1678: if (sftp_server == NULL)
1679: sftp_server = _PATH_SFTP_SERVER;
1680: break;
1681: case 's':
1682: sftp_server = optarg;
1683: break;
1684: case 'S':
1685: ssh_program = optarg;
1.70 djm 1686: replacearg(&args, 0, "%s", ssh_program);
1.3 djm 1687: break;
1.10 deraadt 1688: case 'b':
1.39 djm 1689: if (batchmode)
1690: fatal("Batch file already specified.");
1691:
1692: /* Allow "-" as stdin */
1.56 deraadt 1693: if (strcmp(optarg, "-") != 0 &&
1.65 djm 1694: (infile = fopen(optarg, "r")) == NULL)
1.39 djm 1695: fatal("%s (%s).", strerror(errno), optarg);
1.34 fgsch 1696: showprogress = 0;
1.39 djm 1697: batchmode = 1;
1.62 djm 1698: addargs(&args, "-obatchmode yes");
1.10 deraadt 1699: break;
1.23 djm 1700: case 'P':
1701: sftp_direct = optarg;
1.24 djm 1702: break;
1703: case 'B':
1704: copy_buffer_len = strtol(optarg, &cp, 10);
1705: if (copy_buffer_len == 0 || *cp != '\0')
1706: fatal("Invalid buffer size \"%s\"", optarg);
1.26 djm 1707: break;
1708: case 'R':
1709: num_requests = strtol(optarg, &cp, 10);
1710: if (num_requests == 0 || *cp != '\0')
1.27 markus 1711: fatal("Invalid number of requests \"%s\"",
1.26 djm 1712: optarg);
1.23 djm 1713: break;
1.3 djm 1714: case 'h':
1715: default:
1.1 djm 1716: usage();
1717: }
1718: }
1.45 djm 1719:
1720: if (!isatty(STDERR_FILENO))
1721: showprogress = 0;
1.1 djm 1722:
1.29 markus 1723: log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
1724:
1.23 djm 1725: if (sftp_direct == NULL) {
1726: if (optind == argc || argc > (optind + 2))
1727: usage();
1728:
1729: userhost = xstrdup(argv[optind]);
1730: file2 = argv[optind+1];
1.1 djm 1731:
1.32 markus 1732: if ((host = strrchr(userhost, '@')) == NULL)
1.23 djm 1733: host = userhost;
1734: else {
1735: *host++ = '\0';
1736: if (!userhost[0]) {
1737: fprintf(stderr, "Missing username\n");
1738: usage();
1739: }
1.95 stevesk 1740: addargs(&args, "-l%s", userhost);
1.41 djm 1741: }
1742:
1743: if ((cp = colon(host)) != NULL) {
1744: *cp++ = '\0';
1745: file1 = cp;
1.23 djm 1746: }
1.3 djm 1747:
1.23 djm 1748: host = cleanhostname(host);
1749: if (!*host) {
1750: fprintf(stderr, "Missing hostname\n");
1.1 djm 1751: usage();
1752: }
1753:
1.23 djm 1754: addargs(&args, "-oProtocol %d", sshver);
1755:
1756: /* no subsystem if the server-spec contains a '/' */
1757: if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
1758: addargs(&args, "-s");
1759:
1760: addargs(&args, "%s", host);
1.27 markus 1761: addargs(&args, "%s", (sftp_server != NULL ?
1.23 djm 1762: sftp_server : "sftp"));
1763:
1.39 djm 1764: if (!batchmode)
1765: fprintf(stderr, "Connecting to %s...\n", host);
1.36 djm 1766: connect_to_server(ssh_program, args.list, &in, &out);
1.23 djm 1767: } else {
1768: args.list = NULL;
1769: addargs(&args, "sftp-server");
1770:
1.39 djm 1771: if (!batchmode)
1772: fprintf(stderr, "Attaching to %s...\n", sftp_direct);
1.36 djm 1773: connect_to_server(sftp_direct, args.list, &in, &out);
1.1 djm 1774: }
1.70 djm 1775: freeargs(&args);
1.1 djm 1776:
1.33 djm 1777: err = interactive_loop(in, out, file1, file2);
1.1 djm 1778:
1779: close(in);
1780: close(out);
1.39 djm 1781: if (batchmode)
1.10 deraadt 1782: fclose(infile);
1.1 djm 1783:
1.28 markus 1784: while (waitpid(sshpid, NULL, 0) == -1)
1785: if (errno != EINTR)
1786: fatal("Couldn't wait for ssh process: %s",
1787: strerror(errno));
1.1 djm 1788:
1.33 djm 1789: exit(err == 0 ? 0 : 1);
1.1 djm 1790: }