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