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