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