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