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