Annotation of src/usr.bin/ssh/sftp.c, Revision 1.64
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.64 ! djm 19: RCSID("$OpenBSD: sftp.c,v 1.63 2005/03/10 22:01:05 deraadt 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] == '-') {
1.63 deraadt 355: for (; strchr(WHITESPACE, *cp) == NULL; cp++) {
1.44 djm 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;
1.64 ! djm 402: u_int i, j;
1.44 djm 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: {
1.64 ! djm 662: int n;
! 663: u_int c = 1, colspace = 0, columns = 1;
1.44 djm 664: SFTP_DIRENT **d;
665:
666: if ((n = do_readdir(conn, path, &d)) != 0)
667: return (n);
668:
1.53 djm 669: if (!(lflag & LS_SHORT_VIEW)) {
1.64 ! djm 670: u_int m = 0, width = 80;
1.44 djm 671: struct winsize ws;
672: char *tmp;
673:
674: /* Count entries for sort and find longest filename */
1.54 djm 675: for (n = 0; d[n] != NULL; n++) {
676: if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL))
677: m = MAX(m, strlen(d[n]->filename));
678: }
1.44 djm 679:
680: /* Add any subpath that also needs to be counted */
681: tmp = path_strip(path, strip_path);
682: m += strlen(tmp);
683: xfree(tmp);
684:
685: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
686: width = ws.ws_col;
687:
688: columns = width / (m + 2);
689: columns = MAX(columns, 1);
690: colspace = width / columns;
691: colspace = MIN(colspace, width);
692: }
693:
1.52 djm 694: if (lflag & SORT_FLAGS) {
1.53 djm 695: sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
1.52 djm 696: qsort(d, n, sizeof(*d), sdirent_comp);
697: }
1.44 djm 698:
1.46 djm 699: for (n = 0; d[n] != NULL && !interrupted; n++) {
1.44 djm 700: char *tmp, *fname;
1.54 djm 701:
702: if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL))
703: continue;
1.44 djm 704:
705: tmp = path_append(path, d[n]->filename);
706: fname = path_strip(tmp, strip_path);
707: xfree(tmp);
708:
1.53 djm 709: if (lflag & LS_LONG_VIEW) {
710: if (lflag & LS_NUMERIC_VIEW) {
1.50 djm 711: char *lname;
712: struct stat sb;
713:
714: memset(&sb, 0, sizeof(sb));
715: attrib_to_stat(&d[n]->a, &sb);
716: lname = ls_file(fname, &sb, 1);
717: printf("%s\n", lname);
718: xfree(lname);
719: } else
720: printf("%s\n", d[n]->longname);
1.44 djm 721: } else {
722: printf("%-*s", colspace, fname);
723: if (c >= columns) {
724: printf("\n");
725: c = 1;
726: } else
727: c++;
728: }
729:
730: xfree(fname);
731: }
732:
1.53 djm 733: if (!(lflag & LS_LONG_VIEW) && (c != 1))
1.44 djm 734: printf("\n");
735:
736: free_sftp_dirents(d);
737: return (0);
738: }
739:
740: /* sftp ls.1 replacement which handles path globs */
741: static int
742: do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
743: int lflag)
744: {
745: glob_t g;
1.64 ! djm 746: u_int i, c = 1, colspace = 0, columns = 1;
1.60 fgsch 747: Attrib *a = NULL;
1.44 djm 748:
749: memset(&g, 0, sizeof(g));
750:
751: if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
1.60 fgsch 752: NULL, &g) || (g.gl_pathc && !g.gl_matchc)) {
753: if (g.gl_pathc)
754: globfree(&g);
1.44 djm 755: error("Can't ls: \"%s\" not found", path);
756: return (-1);
757: }
758:
1.46 djm 759: if (interrupted)
760: goto out;
761:
1.44 djm 762: /*
1.60 fgsch 763: * If the glob returns a single match and it is a directory,
764: * then just list its contents.
1.44 djm 765: */
1.60 fgsch 766: if (g.gl_matchc == 1) {
767: if ((a = do_lstat(conn, g.gl_pathv[0], 1)) == NULL) {
1.44 djm 768: globfree(&g);
769: return (-1);
770: }
771: if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
772: S_ISDIR(a->perm)) {
1.60 fgsch 773: int err;
774:
775: err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag);
1.44 djm 776: globfree(&g);
1.60 fgsch 777: return (err);
1.44 djm 778: }
779: }
780:
1.53 djm 781: if (!(lflag & LS_SHORT_VIEW)) {
1.64 ! djm 782: u_int m = 0, width = 80;
1.44 djm 783: struct winsize ws;
784:
785: /* Count entries for sort and find longest filename */
786: for (i = 0; g.gl_pathv[i]; i++)
787: m = MAX(m, strlen(g.gl_pathv[i]));
788:
789: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
790: width = ws.ws_col;
791:
792: columns = width / (m + 2);
793: columns = MAX(columns, 1);
794: colspace = width / columns;
795: }
796:
1.60 fgsch 797: for (i = 0; g.gl_pathv[i] && !interrupted; i++, a = NULL) {
1.44 djm 798: char *fname;
799:
800: fname = path_strip(g.gl_pathv[i], strip_path);
801:
1.53 djm 802: if (lflag & LS_LONG_VIEW) {
1.44 djm 803: char *lname;
804: struct stat sb;
805:
806: /*
807: * XXX: this is slow - 1 roundtrip per path
808: * A solution to this is to fork glob() and
809: * build a sftp specific version which keeps the
810: * attribs (which currently get thrown away)
811: * that the server returns as well as the filenames.
812: */
813: memset(&sb, 0, sizeof(sb));
1.60 fgsch 814: if (a == NULL)
815: a = do_lstat(conn, g.gl_pathv[i], 1);
1.44 djm 816: if (a != NULL)
817: attrib_to_stat(a, &sb);
818: lname = ls_file(fname, &sb, 1);
819: printf("%s\n", lname);
820: xfree(lname);
821: } else {
822: printf("%-*s", colspace, fname);
823: if (c >= columns) {
824: printf("\n");
825: c = 1;
826: } else
827: c++;
828: }
829: xfree(fname);
830: }
831:
1.53 djm 832: if (!(lflag & LS_LONG_VIEW) && (c != 1))
1.44 djm 833: printf("\n");
834:
1.46 djm 835: out:
1.44 djm 836: if (g.gl_pathc)
837: globfree(&g);
838:
839: return (0);
840: }
841:
842: static int
843: parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
844: unsigned long *n_arg, char **path1, char **path2)
845: {
846: const char *cmd, *cp = *cpp;
847: char *cp2;
848: int base = 0;
849: long l;
850: int i, cmdnum;
851:
852: /* Skip leading whitespace */
853: cp = cp + strspn(cp, WHITESPACE);
854:
855: /* Ignore blank lines and lines which begin with comment '#' char */
856: if (*cp == '\0' || *cp == '#')
857: return (0);
858:
859: /* Check for leading '-' (disable error processing) */
860: *iflag = 0;
861: if (*cp == '-') {
862: *iflag = 1;
863: cp++;
864: }
865:
866: /* Figure out which command we have */
867: for (i = 0; cmds[i].c; i++) {
868: int cmdlen = strlen(cmds[i].c);
869:
870: /* Check for command followed by whitespace */
871: if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
872: strchr(WHITESPACE, cp[cmdlen])) {
873: cp += cmdlen;
874: cp = cp + strspn(cp, WHITESPACE);
875: break;
876: }
877: }
878: cmdnum = cmds[i].n;
879: cmd = cmds[i].c;
880:
881: /* Special case */
882: if (*cp == '!') {
883: cp++;
884: cmdnum = I_SHELL;
885: } else if (cmdnum == -1) {
886: error("Invalid command.");
887: return (-1);
888: }
889:
890: /* Get arguments and parse flags */
891: *lflag = *pflag = *n_arg = 0;
892: *path1 = *path2 = NULL;
893: switch (cmdnum) {
894: case I_GET:
895: case I_PUT:
896: if (parse_getput_flags(&cp, pflag))
897: return(-1);
898: /* Get first pathname (mandatory) */
899: if (get_pathname(&cp, path1))
900: return(-1);
901: if (*path1 == NULL) {
902: error("You must specify at least one path after a "
903: "%s command.", cmd);
904: return(-1);
905: }
906: /* Try to get second pathname (optional) */
907: if (get_pathname(&cp, path2))
908: return(-1);
909: break;
910: case I_RENAME:
911: case I_SYMLINK:
912: if (get_pathname(&cp, path1))
913: return(-1);
914: if (get_pathname(&cp, path2))
915: return(-1);
916: if (!*path1 || !*path2) {
917: error("You must specify two paths after a %s "
918: "command.", cmd);
919: return(-1);
920: }
921: break;
922: case I_RM:
923: case I_MKDIR:
924: case I_RMDIR:
925: case I_CHDIR:
926: case I_LCHDIR:
927: case I_LMKDIR:
928: /* Get pathname (mandatory) */
929: if (get_pathname(&cp, path1))
930: return(-1);
931: if (*path1 == NULL) {
932: error("You must specify a path after a %s command.",
933: cmd);
934: return(-1);
935: }
936: break;
937: case I_LS:
938: if (parse_ls_flags(&cp, lflag))
939: return(-1);
940: /* Path is optional */
941: if (get_pathname(&cp, path1))
942: return(-1);
943: break;
944: case I_LLS:
945: case I_SHELL:
946: /* Uses the rest of the line */
947: break;
948: case I_LUMASK:
949: base = 8;
950: case I_CHMOD:
951: base = 8;
952: case I_CHOWN:
953: case I_CHGRP:
954: /* Get numeric arg (mandatory) */
955: l = strtol(cp, &cp2, base);
956: if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
957: errno == ERANGE) || l < 0) {
958: error("You must supply a numeric argument "
959: "to the %s command.", cmd);
960: return(-1);
961: }
962: cp = cp2;
963: *n_arg = l;
964: if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
965: break;
966: if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
967: error("You must supply a numeric argument "
968: "to the %s command.", cmd);
969: return(-1);
970: }
971: cp += strspn(cp, WHITESPACE);
972:
973: /* Get pathname (mandatory) */
974: if (get_pathname(&cp, path1))
975: return(-1);
976: if (*path1 == NULL) {
977: error("You must specify a path after a %s command.",
978: cmd);
979: return(-1);
980: }
981: break;
982: case I_QUIT:
983: case I_PWD:
984: case I_LPWD:
985: case I_HELP:
986: case I_VERSION:
987: case I_PROGRESS:
988: break;
989: default:
990: fatal("Command not implemented");
991: }
992:
993: *cpp = cp;
994: return(cmdnum);
995: }
996:
997: static int
998: parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
999: int err_abort)
1000: {
1001: char *path1, *path2, *tmp;
1002: int pflag, lflag, iflag, cmdnum, i;
1003: unsigned long n_arg;
1004: Attrib a, *aa;
1005: char path_buf[MAXPATHLEN];
1006: int err = 0;
1007: glob_t g;
1008:
1009: path1 = path2 = NULL;
1010: cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
1011: &path1, &path2);
1012:
1013: if (iflag != 0)
1014: err_abort = 0;
1015:
1016: memset(&g, 0, sizeof(g));
1017:
1018: /* Perform command */
1019: switch (cmdnum) {
1020: case 0:
1021: /* Blank line */
1022: break;
1023: case -1:
1024: /* Unrecognized command */
1025: err = -1;
1026: break;
1027: case I_GET:
1028: err = process_get(conn, path1, path2, *pwd, pflag);
1029: break;
1030: case I_PUT:
1031: err = process_put(conn, path1, path2, *pwd, pflag);
1032: break;
1033: case I_RENAME:
1034: path1 = make_absolute(path1, *pwd);
1035: path2 = make_absolute(path2, *pwd);
1036: err = do_rename(conn, path1, path2);
1037: break;
1038: case I_SYMLINK:
1039: path2 = make_absolute(path2, *pwd);
1040: err = do_symlink(conn, path1, path2);
1041: break;
1042: case I_RM:
1043: path1 = make_absolute(path1, *pwd);
1044: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46 djm 1045: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.44 djm 1046: printf("Removing %s\n", g.gl_pathv[i]);
1047: err = do_rm(conn, g.gl_pathv[i]);
1048: if (err != 0 && err_abort)
1049: break;
1050: }
1051: break;
1052: case I_MKDIR:
1053: path1 = make_absolute(path1, *pwd);
1054: attrib_clear(&a);
1055: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1056: a.perm = 0777;
1057: err = do_mkdir(conn, path1, &a);
1058: break;
1059: case I_RMDIR:
1060: path1 = make_absolute(path1, *pwd);
1061: err = do_rmdir(conn, path1);
1062: break;
1063: case I_CHDIR:
1064: path1 = make_absolute(path1, *pwd);
1065: if ((tmp = do_realpath(conn, path1)) == NULL) {
1066: err = 1;
1067: break;
1068: }
1069: if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1070: xfree(tmp);
1071: err = 1;
1072: break;
1073: }
1074: if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
1075: error("Can't change directory: Can't check target");
1076: xfree(tmp);
1077: err = 1;
1078: break;
1079: }
1080: if (!S_ISDIR(aa->perm)) {
1081: error("Can't change directory: \"%s\" is not "
1082: "a directory", tmp);
1083: xfree(tmp);
1084: err = 1;
1085: break;
1086: }
1087: xfree(*pwd);
1088: *pwd = tmp;
1089: break;
1090: case I_LS:
1091: if (!path1) {
1092: do_globbed_ls(conn, *pwd, *pwd, lflag);
1093: break;
1094: }
1095:
1096: /* Strip pwd off beginning of non-absolute paths */
1097: tmp = NULL;
1098: if (*path1 != '/')
1099: tmp = *pwd;
1100:
1101: path1 = make_absolute(path1, *pwd);
1102: err = do_globbed_ls(conn, path1, tmp, lflag);
1103: break;
1104: case I_LCHDIR:
1105: if (chdir(path1) == -1) {
1106: error("Couldn't change local directory to "
1107: "\"%s\": %s", path1, strerror(errno));
1108: err = 1;
1109: }
1110: break;
1111: case I_LMKDIR:
1112: if (mkdir(path1, 0777) == -1) {
1113: error("Couldn't create local directory "
1114: "\"%s\": %s", path1, strerror(errno));
1115: err = 1;
1116: }
1117: break;
1118: case I_LLS:
1119: local_do_ls(cmd);
1120: break;
1121: case I_SHELL:
1122: local_do_shell(cmd);
1123: break;
1124: case I_LUMASK:
1125: umask(n_arg);
1126: printf("Local umask: %03lo\n", n_arg);
1127: break;
1128: case I_CHMOD:
1129: path1 = make_absolute(path1, *pwd);
1130: attrib_clear(&a);
1131: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1132: a.perm = n_arg;
1133: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46 djm 1134: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.44 djm 1135: printf("Changing mode on %s\n", g.gl_pathv[i]);
1136: err = do_setstat(conn, g.gl_pathv[i], &a);
1137: if (err != 0 && err_abort)
1138: break;
1139: }
1140: break;
1141: case I_CHOWN:
1142: case I_CHGRP:
1143: path1 = make_absolute(path1, *pwd);
1144: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46 djm 1145: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.44 djm 1146: if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
1147: if (err != 0 && err_abort)
1148: break;
1149: else
1150: continue;
1151: }
1152: if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1153: error("Can't get current ownership of "
1154: "remote file \"%s\"", g.gl_pathv[i]);
1155: if (err != 0 && err_abort)
1156: break;
1157: else
1158: continue;
1159: }
1160: aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1161: if (cmdnum == I_CHOWN) {
1162: printf("Changing owner on %s\n", g.gl_pathv[i]);
1163: aa->uid = n_arg;
1164: } else {
1165: printf("Changing group on %s\n", g.gl_pathv[i]);
1166: aa->gid = n_arg;
1167: }
1168: err = do_setstat(conn, g.gl_pathv[i], aa);
1169: if (err != 0 && err_abort)
1170: break;
1171: }
1172: break;
1173: case I_PWD:
1174: printf("Remote working directory: %s\n", *pwd);
1175: break;
1176: case I_LPWD:
1177: if (!getcwd(path_buf, sizeof(path_buf))) {
1178: error("Couldn't get local cwd: %s", strerror(errno));
1179: err = -1;
1180: break;
1181: }
1182: printf("Local working directory: %s\n", path_buf);
1183: break;
1184: case I_QUIT:
1185: /* Processed below */
1186: break;
1187: case I_HELP:
1188: help();
1189: break;
1190: case I_VERSION:
1191: printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1192: break;
1193: case I_PROGRESS:
1194: showprogress = !showprogress;
1195: if (showprogress)
1196: printf("Progress meter enabled\n");
1197: else
1198: printf("Progress meter disabled\n");
1199: break;
1200: default:
1201: fatal("%d is not implemented", cmdnum);
1202: }
1203:
1204: if (g.gl_pathc)
1205: globfree(&g);
1206: if (path1)
1207: xfree(path1);
1208: if (path2)
1209: xfree(path2);
1210:
1211: /* If an unignored error occurs in batch mode we should abort. */
1212: if (err_abort && err != 0)
1213: return (-1);
1214: else if (cmdnum == I_QUIT)
1215: return (1);
1216:
1217: return (0);
1218: }
1219:
1.57 djm 1220: static char *
1221: prompt(EditLine *el)
1222: {
1223: return ("sftp> ");
1224: }
1225:
1.44 djm 1226: int
1227: interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1228: {
1229: char *pwd;
1230: char *dir = NULL;
1231: char cmd[2048];
1232: struct sftp_conn *conn;
1233: int err;
1.57 djm 1234: EditLine *el = NULL;
1235: History *hl = NULL;
1236: HistEvent hev;
1237: extern char *__progname;
1238:
1239: if (!batchmode && isatty(STDIN_FILENO)) {
1240: if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL)
1241: fatal("Couldn't initialise editline");
1242: if ((hl = history_init()) == NULL)
1243: fatal("Couldn't initialise editline history");
1244: history(hl, &hev, H_SETSIZE, 100);
1245: el_set(el, EL_HIST, history, hl);
1246:
1247: el_set(el, EL_PROMPT, prompt);
1248: el_set(el, EL_EDITOR, "emacs");
1249: el_set(el, EL_TERMINAL, NULL);
1250: el_set(el, EL_SIGNAL, 1);
1251: el_source(el, NULL);
1252: }
1.44 djm 1253:
1254: conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1255: if (conn == NULL)
1256: fatal("Couldn't initialise connection to server");
1257:
1258: pwd = do_realpath(conn, ".");
1259: if (pwd == NULL)
1260: fatal("Need cwd");
1261:
1262: if (file1 != NULL) {
1263: dir = xstrdup(file1);
1264: dir = make_absolute(dir, pwd);
1265:
1266: if (remote_is_dir(conn, dir) && file2 == NULL) {
1267: printf("Changing to: %s\n", dir);
1268: snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
1.58 markus 1269: if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) {
1270: xfree(dir);
1271: xfree(pwd);
1.44 djm 1272: return (-1);
1.58 markus 1273: }
1.44 djm 1274: } else {
1275: if (file2 == NULL)
1276: snprintf(cmd, sizeof cmd, "get %s", dir);
1277: else
1278: snprintf(cmd, sizeof cmd, "get %s %s", dir,
1279: file2);
1280:
1281: err = parse_dispatch_command(conn, cmd, &pwd, 1);
1282: xfree(dir);
1283: xfree(pwd);
1284: return (err);
1285: }
1286: xfree(dir);
1287: }
1288:
1289: setvbuf(stdout, NULL, _IOLBF, 0);
1290: setvbuf(infile, NULL, _IOLBF, 0);
1291:
1292: err = 0;
1293: for (;;) {
1294: char *cp;
1.57 djm 1295: const char *line;
1296: int count = 0;
1.44 djm 1297:
1.46 djm 1298: signal(SIGINT, SIG_IGN);
1299:
1.57 djm 1300: if (el == NULL) {
1301: printf("sftp> ");
1302: if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1303: printf("\n");
1304: break;
1305: }
1306: if (batchmode) /* Echo command */
1307: printf("%s", cmd);
1308: } else {
1309: if ((line = el_gets(el, &count)) == NULL || count <= 0)
1310: break;
1311: history(hl, &hev, H_ENTER, line);
1312: if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
1313: fprintf(stderr, "Error: input line too long\n");
1314: continue;
1315: }
1.44 djm 1316: }
1317:
1318: cp = strrchr(cmd, '\n');
1319: if (cp)
1320: *cp = '\0';
1321:
1.46 djm 1322: /* Handle user interrupts gracefully during commands */
1323: interrupted = 0;
1324: signal(SIGINT, cmd_interrupt);
1325:
1.44 djm 1326: err = parse_dispatch_command(conn, cmd, &pwd, batchmode);
1327: if (err != 0)
1328: break;
1329: }
1330: xfree(pwd);
1331:
1332: /* err == 1 signifies normal "quit" exit */
1333: return (err >= 0 ? 0 : -1);
1334: }
1.34 fgsch 1335:
1.18 itojun 1336: static void
1.36 djm 1337: connect_to_server(char *path, char **args, int *in, int *out)
1.1 djm 1338: {
1339: int c_in, c_out;
1.30 deraadt 1340:
1.1 djm 1341: #ifdef USE_PIPES
1342: int pin[2], pout[2];
1.30 deraadt 1343:
1.1 djm 1344: if ((pipe(pin) == -1) || (pipe(pout) == -1))
1345: fatal("pipe: %s", strerror(errno));
1346: *in = pin[0];
1347: *out = pout[1];
1348: c_in = pout[0];
1349: c_out = pin[1];
1350: #else /* USE_PIPES */
1351: int inout[2];
1.30 deraadt 1352:
1.1 djm 1353: if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
1354: fatal("socketpair: %s", strerror(errno));
1355: *in = *out = inout[0];
1356: c_in = c_out = inout[1];
1357: #endif /* USE_PIPES */
1358:
1.36 djm 1359: if ((sshpid = fork()) == -1)
1.1 djm 1360: fatal("fork: %s", strerror(errno));
1.36 djm 1361: else if (sshpid == 0) {
1.1 djm 1362: if ((dup2(c_in, STDIN_FILENO) == -1) ||
1363: (dup2(c_out, STDOUT_FILENO) == -1)) {
1364: fprintf(stderr, "dup2: %s\n", strerror(errno));
1.47 djm 1365: _exit(1);
1.1 djm 1366: }
1367: close(*in);
1368: close(*out);
1369: close(c_in);
1370: close(c_out);
1.46 djm 1371:
1372: /*
1373: * The underlying ssh is in the same process group, so we must
1.56 deraadt 1374: * ignore SIGINT if we want to gracefully abort commands,
1375: * otherwise the signal will make it to the ssh process and
1.46 djm 1376: * kill it too
1377: */
1378: signal(SIGINT, SIG_IGN);
1.49 dtucker 1379: execvp(path, args);
1.23 djm 1380: fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
1.47 djm 1381: _exit(1);
1.1 djm 1382: }
1383:
1.36 djm 1384: signal(SIGTERM, killchild);
1385: signal(SIGINT, killchild);
1386: signal(SIGHUP, killchild);
1.1 djm 1387: close(c_in);
1388: close(c_out);
1389: }
1390:
1.18 itojun 1391: static void
1.1 djm 1392: usage(void)
1393: {
1.25 mpech 1394: extern char *__progname;
1.27 markus 1395:
1.19 stevesk 1396: fprintf(stderr,
1.38 jmc 1397: "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n"
1398: " [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n"
1399: " [-S program] [-s subsystem | sftp_server] host\n"
1400: " %s [[user@]host[:file [file]]]\n"
1401: " %s [[user@]host[:dir[/]]]\n"
1402: " %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname);
1.1 djm 1403: exit(1);
1404: }
1405:
1.2 stevesk 1406: int
1.1 djm 1407: main(int argc, char **argv)
1408: {
1.33 djm 1409: int in, out, ch, err;
1.48 pedro 1410: char *host, *userhost, *cp, *file2 = NULL;
1.17 mouring 1411: int debug_level = 0, sshver = 2;
1412: char *file1 = NULL, *sftp_server = NULL;
1.23 djm 1413: char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
1.17 mouring 1414: LogLevel ll = SYSLOG_LEVEL_INFO;
1415: arglist args;
1.3 djm 1416: extern int optind;
1417: extern char *optarg;
1.1 djm 1418:
1.17 mouring 1419: args.list = NULL;
1.22 deraadt 1420: addargs(&args, "ssh"); /* overwritten with ssh_program */
1.17 mouring 1421: addargs(&args, "-oForwardX11 no");
1422: addargs(&args, "-oForwardAgent no");
1.21 stevesk 1423: addargs(&args, "-oClearAllForwardings yes");
1.40 djm 1424:
1.17 mouring 1425: ll = SYSLOG_LEVEL_INFO;
1.40 djm 1426: infile = stdin;
1.3 djm 1427:
1.26 djm 1428: while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
1.3 djm 1429: switch (ch) {
1430: case 'C':
1.17 mouring 1431: addargs(&args, "-C");
1.3 djm 1432: break;
1433: case 'v':
1.17 mouring 1434: if (debug_level < 3) {
1435: addargs(&args, "-v");
1436: ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
1437: }
1438: debug_level++;
1.3 djm 1439: break;
1.19 stevesk 1440: case 'F':
1.3 djm 1441: case 'o':
1.19 stevesk 1442: addargs(&args, "-%c%s", ch, optarg);
1.7 markus 1443: break;
1444: case '1':
1.17 mouring 1445: sshver = 1;
1.7 markus 1446: if (sftp_server == NULL)
1447: sftp_server = _PATH_SFTP_SERVER;
1448: break;
1449: case 's':
1450: sftp_server = optarg;
1451: break;
1452: case 'S':
1453: ssh_program = optarg;
1.3 djm 1454: break;
1.10 deraadt 1455: case 'b':
1.39 djm 1456: if (batchmode)
1457: fatal("Batch file already specified.");
1458:
1459: /* Allow "-" as stdin */
1.56 deraadt 1460: if (strcmp(optarg, "-") != 0 &&
1.39 djm 1461: (infile = fopen(optarg, "r")) == NULL)
1462: fatal("%s (%s).", strerror(errno), optarg);
1.34 fgsch 1463: showprogress = 0;
1.39 djm 1464: batchmode = 1;
1.62 djm 1465: addargs(&args, "-obatchmode yes");
1.10 deraadt 1466: break;
1.23 djm 1467: case 'P':
1468: sftp_direct = optarg;
1.24 djm 1469: break;
1470: case 'B':
1471: copy_buffer_len = strtol(optarg, &cp, 10);
1472: if (copy_buffer_len == 0 || *cp != '\0')
1473: fatal("Invalid buffer size \"%s\"", optarg);
1.26 djm 1474: break;
1475: case 'R':
1476: num_requests = strtol(optarg, &cp, 10);
1477: if (num_requests == 0 || *cp != '\0')
1.27 markus 1478: fatal("Invalid number of requests \"%s\"",
1.26 djm 1479: optarg);
1.23 djm 1480: break;
1.3 djm 1481: case 'h':
1482: default:
1.1 djm 1483: usage();
1484: }
1485: }
1.45 djm 1486:
1487: if (!isatty(STDERR_FILENO))
1488: showprogress = 0;
1.1 djm 1489:
1.29 markus 1490: log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
1491:
1.23 djm 1492: if (sftp_direct == NULL) {
1493: if (optind == argc || argc > (optind + 2))
1494: usage();
1495:
1496: userhost = xstrdup(argv[optind]);
1497: file2 = argv[optind+1];
1.1 djm 1498:
1.32 markus 1499: if ((host = strrchr(userhost, '@')) == NULL)
1.23 djm 1500: host = userhost;
1501: else {
1502: *host++ = '\0';
1503: if (!userhost[0]) {
1504: fprintf(stderr, "Missing username\n");
1505: usage();
1506: }
1507: addargs(&args, "-l%s",userhost);
1.41 djm 1508: }
1509:
1510: if ((cp = colon(host)) != NULL) {
1511: *cp++ = '\0';
1512: file1 = cp;
1.23 djm 1513: }
1.3 djm 1514:
1.23 djm 1515: host = cleanhostname(host);
1516: if (!*host) {
1517: fprintf(stderr, "Missing hostname\n");
1.1 djm 1518: usage();
1519: }
1520:
1.23 djm 1521: addargs(&args, "-oProtocol %d", sshver);
1522:
1523: /* no subsystem if the server-spec contains a '/' */
1524: if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
1525: addargs(&args, "-s");
1526:
1527: addargs(&args, "%s", host);
1.27 markus 1528: addargs(&args, "%s", (sftp_server != NULL ?
1.23 djm 1529: sftp_server : "sftp"));
1530: args.list[0] = ssh_program;
1531:
1.39 djm 1532: if (!batchmode)
1533: fprintf(stderr, "Connecting to %s...\n", host);
1.36 djm 1534: connect_to_server(ssh_program, args.list, &in, &out);
1.23 djm 1535: } else {
1536: args.list = NULL;
1537: addargs(&args, "sftp-server");
1538:
1.39 djm 1539: if (!batchmode)
1540: fprintf(stderr, "Attaching to %s...\n", sftp_direct);
1.36 djm 1541: connect_to_server(sftp_direct, args.list, &in, &out);
1.1 djm 1542: }
1543:
1.33 djm 1544: err = interactive_loop(in, out, file1, file2);
1.1 djm 1545:
1546: close(in);
1547: close(out);
1.39 djm 1548: if (batchmode)
1.10 deraadt 1549: fclose(infile);
1.1 djm 1550:
1.28 markus 1551: while (waitpid(sshpid, NULL, 0) == -1)
1552: if (errno != EINTR)
1553: fatal("Couldn't wait for ssh process: %s",
1554: strerror(errno));
1.1 djm 1555:
1.33 djm 1556: exit(err == 0 ? 0 : 1);
1.1 djm 1557: }