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