Annotation of src/usr.bin/ssh/sftp.c, Revision 1.195
1.195 ! djm 1: /* $OpenBSD: sftp.c,v 1.194 2019/07/10 07:04:27 tb 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:
1.91 deraadt 18: #include <sys/types.h>
1.72 stevesk 19: #include <sys/ioctl.h>
1.73 stevesk 20: #include <sys/wait.h>
1.75 stevesk 21: #include <sys/stat.h>
1.83 stevesk 22: #include <sys/socket.h>
1.100 djm 23: #include <sys/statvfs.h>
1.44 djm 24:
1.97 djm 25: #include <ctype.h>
1.85 stevesk 26: #include <errno.h>
1.44 djm 27: #include <glob.h>
1.57 djm 28: #include <histedit.h>
1.71 stevesk 29: #include <paths.h>
1.111 djm 30: #include <libgen.h>
1.146 dtucker 31: #include <locale.h>
1.74 stevesk 32: #include <signal.h>
1.174 schwarze 33: #include <stdarg.h>
1.89 stevesk 34: #include <stdlib.h>
1.90 stevesk 35: #include <stdio.h>
1.87 stevesk 36: #include <string.h>
1.86 stevesk 37: #include <unistd.h>
1.170 deraadt 38: #include <limits.h>
1.100 djm 39: #include <util.h>
1.1 djm 40:
41: #include "xmalloc.h"
42: #include "log.h"
43: #include "pathnames.h"
1.16 mouring 44: #include "misc.h"
1.174 schwarze 45: #include "utf8.h"
1.1 djm 46:
47: #include "sftp.h"
1.169 djm 48: #include "ssherr.h"
49: #include "sshbuf.h"
1.1 djm 50: #include "sftp-common.h"
51: #include "sftp-client.h"
1.43 djm 52:
1.112 djm 53: #define DEFAULT_COPY_BUFLEN 32768 /* Size of buffer for up/download */
54: #define DEFAULT_NUM_REQUESTS 64 /* # concurrent outstanding requests */
55:
1.44 djm 56: /* File to read commands from */
57: FILE* infile;
1.15 mouring 58:
1.44 djm 59: /* Are we in batchfile mode? */
1.39 djm 60: int batchmode = 0;
1.44 djm 61:
62: /* PID of ssh transport process */
1.185 bluhm 63: static volatile pid_t sshpid = -1;
1.7 markus 64:
1.143 djm 65: /* Suppress diagnositic messages */
66: int quiet = 0;
67:
1.44 djm 68: /* This is set to 0 if the progressmeter is not desired. */
1.45 djm 69: int showprogress = 1;
1.44 djm 70:
1.111 djm 71: /* When this option is set, we always recursively download/upload directories */
72: int global_rflag = 0;
73:
1.159 logan 74: /* When this option is set, we resume download or upload if possible */
1.148 djm 75: int global_aflag = 0;
76:
1.111 djm 77: /* When this option is set, the file transfers will always preserve times */
78: int global_pflag = 0;
79:
1.156 djm 80: /* When this option is set, transfers will have fsync() called on each file */
81: int global_fflag = 0;
82:
1.46 djm 83: /* SIGINT received during command processing */
84: volatile sig_atomic_t interrupted = 0;
85:
1.52 djm 86: /* I wish qsort() took a separate ctx for the comparison function...*/
87: int sort_flag;
1.180 djm 88: glob_t *sort_glob;
1.52 djm 89:
1.116 djm 90: /* Context used for commandline completion */
91: struct complete_ctx {
92: struct sftp_conn *conn;
93: char **remote_pathp;
94: };
95:
1.44 djm 96: int remote_glob(struct sftp_conn *, const char *, int,
97: int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
98:
99: /* Separators for interactive commands */
100: #define WHITESPACE " \t\r\n"
101:
1.52 djm 102: /* ls flags */
1.119 djm 103: #define LS_LONG_VIEW 0x0001 /* Full view ala ls -l */
104: #define LS_SHORT_VIEW 0x0002 /* Single row view ala ls -1 */
105: #define LS_NUMERIC_VIEW 0x0004 /* Long view with numeric uid/gid */
106: #define LS_NAME_SORT 0x0008 /* Sort by name (default) */
107: #define LS_TIME_SORT 0x0010 /* Sort by mtime */
108: #define LS_SIZE_SORT 0x0020 /* Sort by file size */
109: #define LS_REVERSE_SORT 0x0040 /* Reverse sort order */
110: #define LS_SHOW_ALL 0x0080 /* Don't skip filenames starting with '.' */
111: #define LS_SI_UNITS 0x0100 /* Display sizes as K, M, G, etc. */
1.52 djm 112:
1.119 djm 113: #define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW|LS_SI_UNITS)
1.53 djm 114: #define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
1.44 djm 115:
116: /* Commands for interactive mode */
1.149 djm 117: enum sftp_command {
118: I_CHDIR = 1,
119: I_CHGRP,
120: I_CHMOD,
121: I_CHOWN,
122: I_DF,
123: I_GET,
124: I_HELP,
125: I_LCHDIR,
126: I_LINK,
127: I_LLS,
128: I_LMKDIR,
129: I_LPWD,
130: I_LS,
131: I_LUMASK,
132: I_MKDIR,
133: I_PUT,
134: I_PWD,
135: I_QUIT,
1.160 logan 136: I_REGET,
1.149 djm 137: I_RENAME,
1.160 logan 138: I_REPUT,
1.149 djm 139: I_RM,
140: I_RMDIR,
141: I_SHELL,
142: I_SYMLINK,
143: I_VERSION,
144: I_PROGRESS,
145: };
1.44 djm 146:
147: struct CMD {
148: const char *c;
149: const int n;
1.116 djm 150: const int t;
1.44 djm 151: };
152:
1.116 djm 153: /* Type of completion */
154: #define NOARGS 0
155: #define REMOTE 1
156: #define LOCAL 2
157:
1.44 djm 158: static const struct CMD cmds[] = {
1.116 djm 159: { "bye", I_QUIT, NOARGS },
160: { "cd", I_CHDIR, REMOTE },
161: { "chdir", I_CHDIR, REMOTE },
162: { "chgrp", I_CHGRP, REMOTE },
163: { "chmod", I_CHMOD, REMOTE },
164: { "chown", I_CHOWN, REMOTE },
165: { "df", I_DF, REMOTE },
166: { "dir", I_LS, REMOTE },
167: { "exit", I_QUIT, NOARGS },
168: { "get", I_GET, REMOTE },
169: { "help", I_HELP, NOARGS },
170: { "lcd", I_LCHDIR, LOCAL },
171: { "lchdir", I_LCHDIR, LOCAL },
172: { "lls", I_LLS, LOCAL },
173: { "lmkdir", I_LMKDIR, LOCAL },
1.132 djm 174: { "ln", I_LINK, REMOTE },
1.116 djm 175: { "lpwd", I_LPWD, LOCAL },
176: { "ls", I_LS, REMOTE },
177: { "lumask", I_LUMASK, NOARGS },
178: { "mkdir", I_MKDIR, REMOTE },
1.124 dtucker 179: { "mget", I_GET, REMOTE },
180: { "mput", I_PUT, LOCAL },
1.116 djm 181: { "progress", I_PROGRESS, NOARGS },
182: { "put", I_PUT, LOCAL },
183: { "pwd", I_PWD, REMOTE },
184: { "quit", I_QUIT, NOARGS },
1.148 djm 185: { "reget", I_REGET, REMOTE },
1.116 djm 186: { "rename", I_RENAME, REMOTE },
1.167 djm 187: { "reput", I_REPUT, LOCAL },
1.116 djm 188: { "rm", I_RM, REMOTE },
189: { "rmdir", I_RMDIR, REMOTE },
190: { "symlink", I_SYMLINK, REMOTE },
191: { "version", I_VERSION, NOARGS },
192: { "!", I_SHELL, NOARGS },
193: { "?", I_HELP, NOARGS },
194: { NULL, -1, -1 }
1.44 djm 195: };
196:
1.96 stevesk 197: /* ARGSUSED */
1.44 djm 198: static void
1.46 djm 199: killchild(int signo)
200: {
1.61 dtucker 201: if (sshpid > 1) {
1.46 djm 202: kill(sshpid, SIGTERM);
1.61 dtucker 203: waitpid(sshpid, NULL, 0);
204: }
1.46 djm 205:
206: _exit(1);
207: }
208:
1.96 stevesk 209: /* ARGSUSED */
1.46 djm 210: static void
1.177 millert 211: suspchild(int signo)
212: {
213: if (sshpid > 1) {
214: kill(sshpid, signo);
215: while (waitpid(sshpid, NULL, WUNTRACED) == -1 && errno == EINTR)
216: continue;
217: }
218: kill(getpid(), SIGSTOP);
219: }
220:
221: /* ARGSUSED */
222: static void
1.46 djm 223: cmd_interrupt(int signo)
224: {
225: const char msg[] = "\rInterrupt \n";
1.59 djm 226: int olderrno = errno;
1.46 djm 227:
1.144 dtucker 228: (void)write(STDERR_FILENO, msg, sizeof(msg) - 1);
1.46 djm 229: interrupted = 1;
1.59 djm 230: errno = olderrno;
1.46 djm 231: }
232:
1.184 djm 233: /*ARGSUSED*/
234: static void
235: sigchld_handler(int sig)
236: {
237: int save_errno = errno;
238: pid_t pid;
239: const char msg[] = "\rConnection closed. \n";
240:
241: /* Report if ssh transport process dies. */
242: while ((pid = waitpid(sshpid, NULL, WNOHANG)) == -1 && errno == EINTR)
243: continue;
1.185 bluhm 244: if (pid == sshpid) {
1.184 djm 245: (void)write(STDERR_FILENO, msg, sizeof(msg) - 1);
1.185 bluhm 246: sshpid = -1;
247: }
1.184 djm 248:
249: errno = save_errno;
250: }
251:
1.46 djm 252: static void
1.44 djm 253: help(void)
254: {
1.106 sobrado 255: printf("Available commands:\n"
256: "bye Quit sftp\n"
257: "cd path Change remote directory to 'path'\n"
1.189 djm 258: "chgrp [-h] grp path Change group of file 'path' to 'grp'\n"
259: "chmod [-h] mode path Change permissions of file 'path' to 'mode'\n"
260: "chown [-h] own path Change owner of file 'path' to 'own'\n"
1.106 sobrado 261: "df [-hi] [path] Display statistics for current directory or\n"
262: " filesystem containing 'path'\n"
263: "exit Quit sftp\n"
1.193 jmc 264: "get [-afpR] remote [local] Download file\n"
1.106 sobrado 265: "help Display this help text\n"
266: "lcd path Change local directory to 'path'\n"
267: "lls [ls-options [path]] Display local directory listing\n"
268: "lmkdir path Create local directory\n"
1.132 djm 269: "ln [-s] oldpath newpath Link remote file (-s for symlink)\n"
1.106 sobrado 270: "lpwd Print local working directory\n"
1.121 jmc 271: "ls [-1afhlnrSt] [path] Display remote directory listing\n"
1.106 sobrado 272: "lumask umask Set local umask to 'umask'\n"
273: "mkdir path Create remote directory\n"
274: "progress Toggle display of progress meter\n"
1.193 jmc 275: "put [-afpR] local [remote] Upload file\n"
1.106 sobrado 276: "pwd Display remote working directory\n"
277: "quit Quit sftp\n"
1.193 jmc 278: "reget [-fpR] remote [local] Resume download file\n"
1.106 sobrado 279: "rename oldpath newpath Rename remote file\n"
1.193 jmc 280: "reput [-fpR] local [remote] Resume upload file\n"
1.106 sobrado 281: "rm path Delete remote file\n"
282: "rmdir path Remove remote directory\n"
283: "symlink oldpath newpath Symlink remote file\n"
284: "version Show SFTP version\n"
285: "!command Execute 'command' in local shell\n"
286: "! Escape to local shell\n"
287: "? Synonym for help\n");
1.44 djm 288: }
289:
290: static void
291: local_do_shell(const char *args)
292: {
293: int status;
294: char *shell;
295: pid_t pid;
296:
297: if (!*args)
298: args = NULL;
299:
1.130 djm 300: if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
1.44 djm 301: shell = _PATH_BSHELL;
302:
303: if ((pid = fork()) == -1)
304: fatal("Couldn't fork: %s", strerror(errno));
305:
306: if (pid == 0) {
307: /* XXX: child has pipe fds to ssh subproc open - issue? */
308: if (args) {
309: debug3("Executing %s -c \"%s\"", shell, args);
310: execl(shell, shell, "-c", args, (char *)NULL);
311: } else {
312: debug3("Executing %s", shell);
313: execl(shell, shell, (char *)NULL);
314: }
315: fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
316: strerror(errno));
317: _exit(1);
318: }
319: while (waitpid(pid, &status, 0) == -1)
320: if (errno != EINTR)
321: fatal("Couldn't wait for child: %s", strerror(errno));
322: if (!WIFEXITED(status))
1.78 djm 323: error("Shell exited abnormally");
1.44 djm 324: else if (WEXITSTATUS(status))
325: error("Shell exited with status %d", WEXITSTATUS(status));
326: }
327:
328: static void
329: local_do_ls(const char *args)
330: {
331: if (!args || !*args)
332: local_do_shell(_PATH_LS);
333: else {
334: int len = strlen(_PATH_LS " ") + strlen(args) + 1;
335: char *buf = xmalloc(len);
336:
337: /* XXX: quoting - rip quoting code from ftp? */
338: snprintf(buf, len, _PATH_LS " %s", args);
339: local_do_shell(buf);
1.145 djm 340: free(buf);
1.44 djm 341: }
342: }
343:
344: /* Strip one path (usually the pwd) from the start of another */
345: static char *
1.175 djm 346: path_strip(const char *path, const char *strip)
1.44 djm 347: {
348: size_t len;
349:
350: if (strip == NULL)
351: return (xstrdup(path));
352:
353: len = strlen(strip);
1.59 djm 354: if (strncmp(path, strip, len) == 0) {
1.44 djm 355: if (strip[len - 1] != '/' && path[len] == '/')
356: len++;
357: return (xstrdup(path + len));
358: }
359:
360: return (xstrdup(path));
361: }
362:
363: static char *
1.175 djm 364: make_absolute(char *p, const char *pwd)
1.44 djm 365: {
1.51 avsm 366: char *abs_str;
1.44 djm 367:
368: /* Derelativise */
1.188 djm 369: if (p && !path_absolute(p)) {
1.51 avsm 370: abs_str = path_append(pwd, p);
1.145 djm 371: free(p);
1.51 avsm 372: return(abs_str);
1.44 djm 373: } else
374: return(p);
375: }
376:
377: static int
1.148 djm 378: parse_getput_flags(const char *cmd, char **argv, int argc,
1.156 djm 379: int *aflag, int *fflag, int *pflag, int *rflag)
1.44 djm 380: {
1.102 martynas 381: extern int opterr, optind, optopt, optreset;
1.97 djm 382: int ch;
1.44 djm 383:
1.97 djm 384: optind = optreset = 1;
385: opterr = 0;
386:
1.156 djm 387: *aflag = *fflag = *rflag = *pflag = 0;
388: while ((ch = getopt(argc, argv, "afPpRr")) != -1) {
1.97 djm 389: switch (ch) {
1.148 djm 390: case 'a':
391: *aflag = 1;
392: break;
1.156 djm 393: case 'f':
394: *fflag = 1;
395: break;
1.44 djm 396: case 'p':
397: case 'P':
398: *pflag = 1;
399: break;
1.111 djm 400: case 'r':
401: case 'R':
402: *rflag = 1;
403: break;
1.44 djm 404: default:
1.102 martynas 405: error("%s: Invalid flag -%c", cmd, optopt);
1.97 djm 406: return -1;
1.44 djm 407: }
408: }
409:
1.97 djm 410: return optind;
1.44 djm 411: }
412:
413: static int
1.132 djm 414: parse_link_flags(const char *cmd, char **argv, int argc, int *sflag)
415: {
416: extern int opterr, optind, optopt, optreset;
417: int ch;
418:
419: optind = optreset = 1;
420: opterr = 0;
421:
422: *sflag = 0;
423: while ((ch = getopt(argc, argv, "s")) != -1) {
424: switch (ch) {
425: case 's':
426: *sflag = 1;
427: break;
428: default:
429: error("%s: Invalid flag -%c", cmd, optopt);
430: return -1;
431: }
432: }
433:
434: return optind;
435: }
436:
437: static int
1.152 djm 438: parse_rename_flags(const char *cmd, char **argv, int argc, int *lflag)
439: {
440: extern int opterr, optind, optopt, optreset;
441: int ch;
442:
443: optind = optreset = 1;
444: opterr = 0;
445:
446: *lflag = 0;
447: while ((ch = getopt(argc, argv, "l")) != -1) {
448: switch (ch) {
449: case 'l':
450: *lflag = 1;
451: break;
452: default:
453: error("%s: Invalid flag -%c", cmd, optopt);
454: return -1;
455: }
456: }
457:
458: return optind;
459: }
460:
461: static int
1.97 djm 462: parse_ls_flags(char **argv, int argc, int *lflag)
1.44 djm 463: {
1.102 martynas 464: extern int opterr, optind, optopt, optreset;
1.97 djm 465: int ch;
466:
467: optind = optreset = 1;
468: opterr = 0;
1.44 djm 469:
1.53 djm 470: *lflag = LS_NAME_SORT;
1.119 djm 471: while ((ch = getopt(argc, argv, "1Safhlnrt")) != -1) {
1.97 djm 472: switch (ch) {
473: case '1':
474: *lflag &= ~VIEW_FLAGS;
475: *lflag |= LS_SHORT_VIEW;
476: break;
477: case 'S':
478: *lflag &= ~SORT_FLAGS;
479: *lflag |= LS_SIZE_SORT;
480: break;
481: case 'a':
482: *lflag |= LS_SHOW_ALL;
483: break;
484: case 'f':
485: *lflag &= ~SORT_FLAGS;
486: break;
1.119 djm 487: case 'h':
488: *lflag |= LS_SI_UNITS;
489: break;
1.97 djm 490: case 'l':
1.119 djm 491: *lflag &= ~LS_SHORT_VIEW;
1.97 djm 492: *lflag |= LS_LONG_VIEW;
493: break;
494: case 'n':
1.119 djm 495: *lflag &= ~LS_SHORT_VIEW;
1.97 djm 496: *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
497: break;
498: case 'r':
499: *lflag |= LS_REVERSE_SORT;
500: break;
501: case 't':
502: *lflag &= ~SORT_FLAGS;
503: *lflag |= LS_TIME_SORT;
504: break;
505: default:
1.102 martynas 506: error("ls: Invalid flag -%c", optopt);
1.97 djm 507: return -1;
1.44 djm 508: }
509: }
510:
1.97 djm 511: return optind;
1.44 djm 512: }
513:
514: static int
1.100 djm 515: parse_df_flags(const char *cmd, char **argv, int argc, int *hflag, int *iflag)
516: {
1.102 martynas 517: extern int opterr, optind, optopt, optreset;
1.100 djm 518: int ch;
519:
520: optind = optreset = 1;
521: opterr = 0;
522:
523: *hflag = *iflag = 0;
524: while ((ch = getopt(argc, argv, "hi")) != -1) {
525: switch (ch) {
526: case 'h':
527: *hflag = 1;
528: break;
529: case 'i':
530: *iflag = 1;
531: break;
532: default:
1.102 martynas 533: error("%s: Invalid flag -%c", cmd, optopt);
1.100 djm 534: return -1;
535: }
536: }
537:
538: return optind;
539: }
540:
541: static int
1.189 djm 542: parse_ch_flags(const char *cmd, char **argv, int argc, int *hflag)
543: {
544: extern int opterr, optind, optopt, optreset;
545: int ch;
546:
547: optind = optreset = 1;
548: opterr = 0;
549:
550: *hflag = 0;
551: while ((ch = getopt(argc, argv, "h")) != -1) {
552: switch (ch) {
553: case 'h':
554: *hflag = 1;
555: break;
556: default:
557: error("%s: Invalid flag -%c", cmd, optopt);
558: return -1;
559: }
560: }
561:
562: return optind;
563: }
564:
565: static int
1.153 djm 566: parse_no_flags(const char *cmd, char **argv, int argc)
567: {
568: extern int opterr, optind, optopt, optreset;
569: int ch;
570:
571: optind = optreset = 1;
572: opterr = 0;
573:
574: while ((ch = getopt(argc, argv, "")) != -1) {
575: switch (ch) {
576: default:
577: error("%s: Invalid flag -%c", cmd, optopt);
578: return -1;
579: }
580: }
581:
582: return optind;
583: }
584:
585: static int
1.175 djm 586: is_dir(const char *path)
1.44 djm 587: {
588: struct stat sb;
589:
590: /* XXX: report errors? */
591: if (stat(path, &sb) == -1)
592: return(0);
593:
1.92 otto 594: return(S_ISDIR(sb.st_mode));
1.44 djm 595: }
596:
597: static int
1.175 djm 598: remote_is_dir(struct sftp_conn *conn, const char *path)
1.44 djm 599: {
600: Attrib *a;
601:
602: /* XXX: report errors? */
603: if ((a = do_stat(conn, path, 1)) == NULL)
604: return(0);
605: if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
606: return(0);
1.92 otto 607: return(S_ISDIR(a->perm));
1.44 djm 608: }
609:
1.111 djm 610: /* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
611: static int
1.175 djm 612: pathname_is_dir(const char *pathname)
1.111 djm 613: {
614: size_t l = strlen(pathname);
615:
616: return l > 0 && pathname[l - 1] == '/';
617: }
618:
1.44 djm 619: static int
1.175 djm 620: process_get(struct sftp_conn *conn, const char *src, const char *dst,
621: const char *pwd, int pflag, int rflag, int resume, int fflag)
1.44 djm 622: {
623: char *abs_src = NULL;
624: char *abs_dst = NULL;
625: glob_t g;
1.111 djm 626: char *filename, *tmp=NULL;
1.164 djm 627: int i, r, err = 0;
1.44 djm 628:
629: abs_src = xstrdup(src);
630: abs_src = make_absolute(abs_src, pwd);
1.111 djm 631: memset(&g, 0, sizeof(g));
1.44 djm 632:
633: debug3("Looking up %s", abs_src);
1.164 djm 634: if ((r = remote_glob(conn, abs_src, GLOB_MARK, NULL, &g)) != 0) {
635: if (r == GLOB_NOSPACE) {
636: error("Too many matches for \"%s\".", abs_src);
637: } else {
638: error("File \"%s\" not found.", abs_src);
639: }
1.44 djm 640: err = -1;
641: goto out;
642: }
643:
1.111 djm 644: /*
645: * If multiple matches then dst must be a directory or
646: * unspecified.
647: */
648: if (g.gl_matchc > 1 && dst != NULL && !is_dir(dst)) {
649: error("Multiple source paths, but destination "
650: "\"%s\" is not a directory", dst);
1.44 djm 651: err = -1;
652: goto out;
653: }
654:
1.46 djm 655: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.111 djm 656: tmp = xstrdup(g.gl_pathv[i]);
657: if ((filename = basename(tmp)) == NULL) {
658: error("basename %s: %s", tmp, strerror(errno));
1.145 djm 659: free(tmp);
1.44 djm 660: err = -1;
661: goto out;
662: }
663:
664: if (g.gl_matchc == 1 && dst) {
665: if (is_dir(dst)) {
1.111 djm 666: abs_dst = path_append(dst, filename);
667: } else {
1.44 djm 668: abs_dst = xstrdup(dst);
1.111 djm 669: }
1.44 djm 670: } else if (dst) {
1.111 djm 671: abs_dst = path_append(dst, filename);
672: } else {
673: abs_dst = xstrdup(filename);
674: }
1.145 djm 675: free(tmp);
1.44 djm 676:
1.148 djm 677: resume |= global_aflag;
678: if (!quiet && resume)
1.174 schwarze 679: mprintf("Resuming %s to %s\n",
680: g.gl_pathv[i], abs_dst);
1.148 djm 681: else if (!quiet && !resume)
1.174 schwarze 682: mprintf("Fetching %s to %s\n",
683: g.gl_pathv[i], abs_dst);
1.111 djm 684: if (pathname_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
1.148 djm 685: if (download_dir(conn, g.gl_pathv[i], abs_dst, NULL,
1.156 djm 686: pflag || global_pflag, 1, resume,
687: fflag || global_fflag) == -1)
1.111 djm 688: err = -1;
689: } else {
690: if (do_download(conn, g.gl_pathv[i], abs_dst, NULL,
1.156 djm 691: pflag || global_pflag, resume,
692: fflag || global_fflag) == -1)
1.111 djm 693: err = -1;
694: }
1.145 djm 695: free(abs_dst);
1.44 djm 696: abs_dst = NULL;
697: }
698:
699: out:
1.145 djm 700: free(abs_src);
1.44 djm 701: globfree(&g);
702: return(err);
703: }
704:
705: static int
1.175 djm 706: process_put(struct sftp_conn *conn, const char *src, const char *dst,
707: const char *pwd, int pflag, int rflag, int resume, int fflag)
1.44 djm 708: {
709: char *tmp_dst = NULL;
710: char *abs_dst = NULL;
1.111 djm 711: char *tmp = NULL, *filename = NULL;
1.44 djm 712: glob_t g;
713: int err = 0;
1.111 djm 714: int i, dst_is_dir = 1;
1.99 djm 715: struct stat sb;
1.44 djm 716:
717: if (dst) {
718: tmp_dst = xstrdup(dst);
719: tmp_dst = make_absolute(tmp_dst, pwd);
720: }
721:
722: memset(&g, 0, sizeof(g));
723: debug3("Looking up %s", src);
1.111 djm 724: if (glob(src, GLOB_NOCHECK | GLOB_MARK, NULL, &g)) {
1.44 djm 725: error("File \"%s\" not found.", src);
726: err = -1;
727: goto out;
728: }
729:
1.111 djm 730: /* If we aren't fetching to pwd then stash this status for later */
731: if (tmp_dst != NULL)
732: dst_is_dir = remote_is_dir(conn, tmp_dst);
733:
1.44 djm 734: /* If multiple matches, dst may be directory or unspecified */
1.111 djm 735: if (g.gl_matchc > 1 && tmp_dst && !dst_is_dir) {
736: error("Multiple paths match, but destination "
737: "\"%s\" is not a directory", tmp_dst);
1.44 djm 738: err = -1;
739: goto out;
740: }
741:
1.46 djm 742: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.99 djm 743: if (stat(g.gl_pathv[i], &sb) == -1) {
744: err = -1;
745: error("stat %s: %s", g.gl_pathv[i], strerror(errno));
746: continue;
747: }
1.149 djm 748:
1.111 djm 749: tmp = xstrdup(g.gl_pathv[i]);
750: if ((filename = basename(tmp)) == NULL) {
751: error("basename %s: %s", tmp, strerror(errno));
1.145 djm 752: free(tmp);
1.44 djm 753: err = -1;
754: goto out;
755: }
756:
757: if (g.gl_matchc == 1 && tmp_dst) {
758: /* If directory specified, append filename */
1.111 djm 759: if (dst_is_dir)
760: abs_dst = path_append(tmp_dst, filename);
761: else
1.44 djm 762: abs_dst = xstrdup(tmp_dst);
763: } else if (tmp_dst) {
1.111 djm 764: abs_dst = path_append(tmp_dst, filename);
765: } else {
766: abs_dst = make_absolute(xstrdup(filename), pwd);
767: }
1.145 djm 768: free(tmp);
1.44 djm 769:
1.159 logan 770: resume |= global_aflag;
771: if (!quiet && resume)
1.174 schwarze 772: mprintf("Resuming upload of %s to %s\n",
773: g.gl_pathv[i], abs_dst);
1.159 logan 774: else if (!quiet && !resume)
1.174 schwarze 775: mprintf("Uploading %s to %s\n",
776: g.gl_pathv[i], abs_dst);
1.111 djm 777: if (pathname_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
778: if (upload_dir(conn, g.gl_pathv[i], abs_dst,
1.159 logan 779: pflag || global_pflag, 1, resume,
1.156 djm 780: fflag || global_fflag) == -1)
1.111 djm 781: err = -1;
782: } else {
783: if (do_upload(conn, g.gl_pathv[i], abs_dst,
1.159 logan 784: pflag || global_pflag, resume,
1.156 djm 785: fflag || global_fflag) == -1)
1.111 djm 786: err = -1;
787: }
1.44 djm 788: }
789:
790: out:
1.145 djm 791: free(abs_dst);
792: free(tmp_dst);
1.44 djm 793: globfree(&g);
794: return(err);
795: }
796:
797: static int
798: sdirent_comp(const void *aa, const void *bb)
799: {
800: SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
801: SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
1.53 djm 802: int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
1.44 djm 803:
1.52 djm 804: #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
1.53 djm 805: if (sort_flag & LS_NAME_SORT)
1.52 djm 806: return (rmul * strcmp(a->filename, b->filename));
1.53 djm 807: else if (sort_flag & LS_TIME_SORT)
1.52 djm 808: return (rmul * NCMP(a->a.mtime, b->a.mtime));
1.53 djm 809: else if (sort_flag & LS_SIZE_SORT)
1.52 djm 810: return (rmul * NCMP(a->a.size, b->a.size));
811:
812: fatal("Unknown ls sort type");
1.44 djm 813: }
814:
815: /* sftp ls.1 replacement for directories */
816: static int
1.175 djm 817: do_ls_dir(struct sftp_conn *conn, const char *path,
818: const char *strip_path, int lflag)
1.44 djm 819: {
1.64 djm 820: int n;
821: u_int c = 1, colspace = 0, columns = 1;
1.44 djm 822: SFTP_DIRENT **d;
823:
824: if ((n = do_readdir(conn, path, &d)) != 0)
825: return (n);
826:
1.53 djm 827: if (!(lflag & LS_SHORT_VIEW)) {
1.64 djm 828: u_int m = 0, width = 80;
1.44 djm 829: struct winsize ws;
830: char *tmp;
831:
832: /* Count entries for sort and find longest filename */
1.54 djm 833: for (n = 0; d[n] != NULL; n++) {
834: if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL))
1.176 deraadt 835: m = MAXIMUM(m, strlen(d[n]->filename));
1.54 djm 836: }
1.44 djm 837:
838: /* Add any subpath that also needs to be counted */
839: tmp = path_strip(path, strip_path);
840: m += strlen(tmp);
1.145 djm 841: free(tmp);
1.44 djm 842:
843: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
844: width = ws.ws_col;
845:
846: columns = width / (m + 2);
1.176 deraadt 847: columns = MAXIMUM(columns, 1);
1.44 djm 848: colspace = width / columns;
1.176 deraadt 849: colspace = MINIMUM(colspace, width);
1.44 djm 850: }
851:
1.52 djm 852: if (lflag & SORT_FLAGS) {
1.68 dtucker 853: for (n = 0; d[n] != NULL; n++)
854: ; /* count entries */
1.53 djm 855: sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
1.52 djm 856: qsort(d, n, sizeof(*d), sdirent_comp);
857: }
1.44 djm 858:
1.46 djm 859: for (n = 0; d[n] != NULL && !interrupted; n++) {
1.44 djm 860: char *tmp, *fname;
1.54 djm 861:
862: if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL))
863: continue;
1.44 djm 864:
865: tmp = path_append(path, d[n]->filename);
866: fname = path_strip(tmp, strip_path);
1.145 djm 867: free(tmp);
1.44 djm 868:
1.53 djm 869: if (lflag & LS_LONG_VIEW) {
1.119 djm 870: if (lflag & (LS_NUMERIC_VIEW|LS_SI_UNITS)) {
1.50 djm 871: char *lname;
872: struct stat sb;
873:
874: memset(&sb, 0, sizeof(sb));
875: attrib_to_stat(&d[n]->a, &sb);
1.119 djm 876: lname = ls_file(fname, &sb, 1,
877: (lflag & LS_SI_UNITS));
1.174 schwarze 878: mprintf("%s\n", lname);
1.145 djm 879: free(lname);
1.50 djm 880: } else
1.174 schwarze 881: mprintf("%s\n", d[n]->longname);
1.44 djm 882: } else {
1.174 schwarze 883: mprintf("%-*s", colspace, fname);
1.44 djm 884: if (c >= columns) {
885: printf("\n");
886: c = 1;
887: } else
888: c++;
889: }
890:
1.145 djm 891: free(fname);
1.44 djm 892: }
893:
1.53 djm 894: if (!(lflag & LS_LONG_VIEW) && (c != 1))
1.44 djm 895: printf("\n");
896:
897: free_sftp_dirents(d);
898: return (0);
899: }
900:
1.180 djm 901: static int
902: sglob_comp(const void *aa, const void *bb)
903: {
904: u_int a = *(const u_int *)aa;
905: u_int b = *(const u_int *)bb;
906: const char *ap = sort_glob->gl_pathv[a];
907: const char *bp = sort_glob->gl_pathv[b];
908: const struct stat *as = sort_glob->gl_statv[a];
909: const struct stat *bs = sort_glob->gl_statv[b];
910: int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
911:
912: #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
913: if (sort_flag & LS_NAME_SORT)
914: return (rmul * strcmp(ap, bp));
915: else if (sort_flag & LS_TIME_SORT)
916: return (rmul * timespeccmp(&as->st_mtim, &bs->st_mtim, <));
917: else if (sort_flag & LS_SIZE_SORT)
918: return (rmul * NCMP(as->st_size, bs->st_size));
919:
920: fatal("Unknown ls sort type");
921: }
922:
1.44 djm 923: /* sftp ls.1 replacement which handles path globs */
924: static int
1.175 djm 925: do_globbed_ls(struct sftp_conn *conn, const char *path,
926: const char *strip_path, int lflag)
1.44 djm 927: {
1.129 djm 928: char *fname, *lname;
1.44 djm 929: glob_t g;
1.164 djm 930: int err, r;
1.129 djm 931: struct winsize ws;
1.180 djm 932: u_int i, j, nentries, *indices = NULL, c = 1;
933: u_int colspace = 0, columns = 1, m = 0, width = 80;
1.44 djm 934:
935: memset(&g, 0, sizeof(g));
936:
1.164 djm 937: if ((r = remote_glob(conn, path,
1.133 djm 938: GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE|GLOB_KEEPSTAT|GLOB_NOSORT,
1.164 djm 939: NULL, &g)) != 0 ||
1.128 djm 940: (g.gl_pathc && !g.gl_matchc)) {
1.60 fgsch 941: if (g.gl_pathc)
942: globfree(&g);
1.164 djm 943: if (r == GLOB_NOSPACE) {
944: error("Can't ls: Too many matches for \"%s\"", path);
945: } else {
946: error("Can't ls: \"%s\" not found", path);
947: }
1.128 djm 948: return -1;
1.44 djm 949: }
950:
1.46 djm 951: if (interrupted)
952: goto out;
953:
1.44 djm 954: /*
1.60 fgsch 955: * If the glob returns a single match and it is a directory,
956: * then just list its contents.
1.44 djm 957: */
1.128 djm 958: if (g.gl_matchc == 1 && g.gl_statv[0] != NULL &&
959: S_ISDIR(g.gl_statv[0]->st_mode)) {
960: err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag);
961: globfree(&g);
962: return err;
1.44 djm 963: }
964:
1.129 djm 965: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
966: width = ws.ws_col;
967:
1.53 djm 968: if (!(lflag & LS_SHORT_VIEW)) {
1.44 djm 969: /* Count entries for sort and find longest filename */
970: for (i = 0; g.gl_pathv[i]; i++)
1.176 deraadt 971: m = MAXIMUM(m, strlen(g.gl_pathv[i]));
1.44 djm 972:
973: columns = width / (m + 2);
1.176 deraadt 974: columns = MAXIMUM(columns, 1);
1.44 djm 975: colspace = width / columns;
976: }
977:
1.180 djm 978: /*
979: * Sorting: rather than mess with the contents of glob_t, prepare
980: * an array of indices into it and sort that. For the usual
981: * unsorted case, the indices are just the identity 1=1, 2=2, etc.
982: */
983: for (nentries = 0; g.gl_pathv[nentries] != NULL; nentries++)
984: ; /* count entries */
985: indices = calloc(nentries, sizeof(*indices));
986: for (i = 0; i < nentries; i++)
987: indices[i] = i;
988:
989: if (lflag & SORT_FLAGS) {
990: sort_glob = &g;
991: sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
992: qsort(indices, nentries, sizeof(*indices), sglob_comp);
993: sort_glob = NULL;
994: }
995:
996: for (j = 0; j < nentries && !interrupted; j++) {
997: i = indices[j];
1.44 djm 998: fname = path_strip(g.gl_pathv[i], strip_path);
1.53 djm 999: if (lflag & LS_LONG_VIEW) {
1.128 djm 1000: if (g.gl_statv[i] == NULL) {
1001: error("no stat information for %s", fname);
1002: continue;
1003: }
1004: lname = ls_file(fname, g.gl_statv[i], 1,
1005: (lflag & LS_SI_UNITS));
1.174 schwarze 1006: mprintf("%s\n", lname);
1.145 djm 1007: free(lname);
1.44 djm 1008: } else {
1.174 schwarze 1009: mprintf("%-*s", colspace, fname);
1.44 djm 1010: if (c >= columns) {
1011: printf("\n");
1012: c = 1;
1013: } else
1014: c++;
1015: }
1.145 djm 1016: free(fname);
1.44 djm 1017: }
1018:
1.53 djm 1019: if (!(lflag & LS_LONG_VIEW) && (c != 1))
1.44 djm 1020: printf("\n");
1021:
1.46 djm 1022: out:
1.44 djm 1023: if (g.gl_pathc)
1024: globfree(&g);
1.180 djm 1025: free(indices);
1.44 djm 1026:
1.128 djm 1027: return 0;
1.44 djm 1028: }
1029:
1.100 djm 1030: static int
1.175 djm 1031: do_df(struct sftp_conn *conn, const char *path, int hflag, int iflag)
1.100 djm 1032: {
1.101 dtucker 1033: struct sftp_statvfs st;
1.178 djm 1034: char s_used[FMT_SCALED_STRSIZE], s_avail[FMT_SCALED_STRSIZE];
1035: char s_root[FMT_SCALED_STRSIZE], s_total[FMT_SCALED_STRSIZE];
1036: char s_icapacity[16], s_dcapacity[16];
1.100 djm 1037:
1038: if (do_statvfs(conn, path, &st, 1) == -1)
1039: return -1;
1.178 djm 1040: if (st.f_files == 0)
1041: strlcpy(s_icapacity, "ERR", sizeof(s_icapacity));
1042: else {
1043: snprintf(s_icapacity, sizeof(s_icapacity), "%3llu%%",
1044: (unsigned long long)(100 * (st.f_files - st.f_ffree) /
1045: st.f_files));
1046: }
1047: if (st.f_blocks == 0)
1048: strlcpy(s_dcapacity, "ERR", sizeof(s_dcapacity));
1049: else {
1050: snprintf(s_dcapacity, sizeof(s_dcapacity), "%3llu%%",
1051: (unsigned long long)(100 * (st.f_blocks - st.f_bfree) /
1052: st.f_blocks));
1053: }
1.100 djm 1054: if (iflag) {
1055: printf(" Inodes Used Avail "
1056: "(root) %%Capacity\n");
1.178 djm 1057: printf("%11llu %11llu %11llu %11llu %s\n",
1.100 djm 1058: (unsigned long long)st.f_files,
1059: (unsigned long long)(st.f_files - st.f_ffree),
1060: (unsigned long long)st.f_favail,
1.178 djm 1061: (unsigned long long)st.f_ffree, s_icapacity);
1.100 djm 1062: } else if (hflag) {
1063: strlcpy(s_used, "error", sizeof(s_used));
1064: strlcpy(s_avail, "error", sizeof(s_avail));
1065: strlcpy(s_root, "error", sizeof(s_root));
1066: strlcpy(s_total, "error", sizeof(s_total));
1067: fmt_scaled((st.f_blocks - st.f_bfree) * st.f_frsize, s_used);
1068: fmt_scaled(st.f_bavail * st.f_frsize, s_avail);
1069: fmt_scaled(st.f_bfree * st.f_frsize, s_root);
1070: fmt_scaled(st.f_blocks * st.f_frsize, s_total);
1071: printf(" Size Used Avail (root) %%Capacity\n");
1.178 djm 1072: printf("%7sB %7sB %7sB %7sB %s\n",
1073: s_total, s_used, s_avail, s_root, s_dcapacity);
1.100 djm 1074: } else {
1075: printf(" Size Used Avail "
1076: "(root) %%Capacity\n");
1.178 djm 1077: printf("%12llu %12llu %12llu %12llu %s\n",
1.100 djm 1078: (unsigned long long)(st.f_frsize * st.f_blocks / 1024),
1079: (unsigned long long)(st.f_frsize *
1080: (st.f_blocks - st.f_bfree) / 1024),
1081: (unsigned long long)(st.f_frsize * st.f_bavail / 1024),
1082: (unsigned long long)(st.f_frsize * st.f_bfree / 1024),
1.178 djm 1083: s_dcapacity);
1.100 djm 1084: }
1085: return 0;
1086: }
1087:
1.97 djm 1088: /*
1089: * Undo escaping of glob sequences in place. Used to undo extra escaping
1090: * applied in makeargv() when the string is destined for a function that
1091: * does not glob it.
1092: */
1093: static void
1094: undo_glob_escape(char *s)
1095: {
1096: size_t i, j;
1097:
1098: for (i = j = 0;;) {
1099: if (s[i] == '\0') {
1100: s[j] = '\0';
1101: return;
1102: }
1103: if (s[i] != '\\') {
1104: s[j++] = s[i++];
1105: continue;
1106: }
1107: /* s[i] == '\\' */
1108: ++i;
1109: switch (s[i]) {
1110: case '?':
1111: case '[':
1112: case '*':
1113: case '\\':
1114: s[j++] = s[i++];
1115: break;
1116: case '\0':
1117: s[j++] = '\\';
1118: s[j] = '\0';
1119: return;
1120: default:
1121: s[j++] = '\\';
1122: s[j++] = s[i++];
1123: break;
1124: }
1125: }
1126: }
1127:
1128: /*
1129: * Split a string into an argument vector using sh(1)-style quoting,
1130: * comment and escaping rules, but with some tweaks to handle glob(3)
1131: * wildcards.
1.116 djm 1132: * The "sloppy" flag allows for recovery from missing terminating quote, for
1133: * use in parsing incomplete commandlines during tab autocompletion.
1134: *
1.97 djm 1135: * Returns NULL on error or a NULL-terminated array of arguments.
1.116 djm 1136: *
1137: * If "lastquote" is not NULL, the quoting character used for the last
1138: * argument is placed in *lastquote ("\0", "'" or "\"").
1.149 djm 1139: *
1.116 djm 1140: * If "terminated" is not NULL, *terminated will be set to 1 when the
1141: * last argument's quote has been properly terminated or 0 otherwise.
1142: * This parameter is only of use if "sloppy" is set.
1.97 djm 1143: */
1144: #define MAXARGS 128
1145: #define MAXARGLEN 8192
1146: static char **
1.116 djm 1147: makeargv(const char *arg, int *argcp, int sloppy, char *lastquote,
1148: u_int *terminated)
1.97 djm 1149: {
1150: int argc, quot;
1151: size_t i, j;
1152: static char argvs[MAXARGLEN];
1153: static char *argv[MAXARGS + 1];
1154: enum { MA_START, MA_SQUOTE, MA_DQUOTE, MA_UNQUOTED } state, q;
1155:
1156: *argcp = argc = 0;
1157: if (strlen(arg) > sizeof(argvs) - 1) {
1158: args_too_longs:
1159: error("string too long");
1160: return NULL;
1161: }
1.116 djm 1162: if (terminated != NULL)
1163: *terminated = 1;
1164: if (lastquote != NULL)
1165: *lastquote = '\0';
1.97 djm 1166: state = MA_START;
1167: i = j = 0;
1168: for (;;) {
1.141 markus 1169: if ((size_t)argc >= sizeof(argv) / sizeof(*argv)){
1.138 dtucker 1170: error("Too many arguments.");
1171: return NULL;
1172: }
1.158 deraadt 1173: if (isspace((unsigned char)arg[i])) {
1.97 djm 1174: if (state == MA_UNQUOTED) {
1175: /* Terminate current argument */
1176: argvs[j++] = '\0';
1177: argc++;
1178: state = MA_START;
1179: } else if (state != MA_START)
1180: argvs[j++] = arg[i];
1181: } else if (arg[i] == '"' || arg[i] == '\'') {
1182: q = arg[i] == '"' ? MA_DQUOTE : MA_SQUOTE;
1183: if (state == MA_START) {
1184: argv[argc] = argvs + j;
1185: state = q;
1.116 djm 1186: if (lastquote != NULL)
1187: *lastquote = arg[i];
1.149 djm 1188: } else if (state == MA_UNQUOTED)
1.97 djm 1189: state = q;
1190: else if (state == q)
1191: state = MA_UNQUOTED;
1192: else
1193: argvs[j++] = arg[i];
1194: } else if (arg[i] == '\\') {
1195: if (state == MA_SQUOTE || state == MA_DQUOTE) {
1196: quot = state == MA_SQUOTE ? '\'' : '"';
1197: /* Unescape quote we are in */
1198: /* XXX support \n and friends? */
1199: if (arg[i + 1] == quot) {
1200: i++;
1201: argvs[j++] = arg[i];
1202: } else if (arg[i + 1] == '?' ||
1203: arg[i + 1] == '[' || arg[i + 1] == '*') {
1204: /*
1205: * Special case for sftp: append
1206: * double-escaped glob sequence -
1207: * glob will undo one level of
1208: * escaping. NB. string can grow here.
1209: */
1210: if (j >= sizeof(argvs) - 5)
1211: goto args_too_longs;
1212: argvs[j++] = '\\';
1213: argvs[j++] = arg[i++];
1214: argvs[j++] = '\\';
1215: argvs[j++] = arg[i];
1216: } else {
1217: argvs[j++] = arg[i++];
1218: argvs[j++] = arg[i];
1219: }
1220: } else {
1221: if (state == MA_START) {
1222: argv[argc] = argvs + j;
1223: state = MA_UNQUOTED;
1.116 djm 1224: if (lastquote != NULL)
1225: *lastquote = '\0';
1.97 djm 1226: }
1227: if (arg[i + 1] == '?' || arg[i + 1] == '[' ||
1228: arg[i + 1] == '*' || arg[i + 1] == '\\') {
1229: /*
1230: * Special case for sftp: append
1231: * escaped glob sequence -
1232: * glob will undo one level of
1233: * escaping.
1234: */
1235: argvs[j++] = arg[i++];
1236: argvs[j++] = arg[i];
1237: } else {
1238: /* Unescape everything */
1239: /* XXX support \n and friends? */
1240: i++;
1241: argvs[j++] = arg[i];
1242: }
1243: }
1244: } else if (arg[i] == '#') {
1245: if (state == MA_SQUOTE || state == MA_DQUOTE)
1246: argvs[j++] = arg[i];
1247: else
1248: goto string_done;
1249: } else if (arg[i] == '\0') {
1250: if (state == MA_SQUOTE || state == MA_DQUOTE) {
1.116 djm 1251: if (sloppy) {
1252: state = MA_UNQUOTED;
1253: if (terminated != NULL)
1254: *terminated = 0;
1255: goto string_done;
1256: }
1.97 djm 1257: error("Unterminated quoted argument");
1258: return NULL;
1259: }
1260: string_done:
1261: if (state == MA_UNQUOTED) {
1262: argvs[j++] = '\0';
1263: argc++;
1264: }
1265: break;
1266: } else {
1267: if (state == MA_START) {
1268: argv[argc] = argvs + j;
1269: state = MA_UNQUOTED;
1.116 djm 1270: if (lastquote != NULL)
1271: *lastquote = '\0';
1.97 djm 1272: }
1273: if ((state == MA_SQUOTE || state == MA_DQUOTE) &&
1274: (arg[i] == '?' || arg[i] == '[' || arg[i] == '*')) {
1275: /*
1276: * Special case for sftp: escape quoted
1277: * glob(3) wildcards. NB. string can grow
1278: * here.
1279: */
1280: if (j >= sizeof(argvs) - 3)
1281: goto args_too_longs;
1282: argvs[j++] = '\\';
1283: argvs[j++] = arg[i];
1284: } else
1285: argvs[j++] = arg[i];
1286: }
1287: i++;
1288: }
1289: *argcp = argc;
1290: return argv;
1291: }
1292:
1.44 djm 1293: static int
1.187 djm 1294: parse_args(const char **cpp, int *ignore_errors, int *disable_echo, int *aflag,
1.173 djm 1295: int *fflag, int *hflag, int *iflag, int *lflag, int *pflag,
1.159 logan 1296: int *rflag, int *sflag,
1.156 djm 1297: unsigned long *n_arg, char **path1, char **path2)
1.44 djm 1298: {
1299: const char *cmd, *cp = *cpp;
1.97 djm 1300: char *cp2, **argv;
1.44 djm 1301: int base = 0;
1302: long l;
1.182 djm 1303: int path1_mandatory = 0, i, cmdnum, optidx, argc;
1.44 djm 1304:
1305: /* Skip leading whitespace */
1306: cp = cp + strspn(cp, WHITESPACE);
1307:
1.187 djm 1308: /*
1309: * Check for leading '-' (disable error processing) and '@' (suppress
1310: * command echo)
1311: */
1.156 djm 1312: *ignore_errors = 0;
1.187 djm 1313: *disable_echo = 0;
1314: for (;*cp != '\0'; cp++) {
1315: if (*cp == '-') {
1316: *ignore_errors = 1;
1317: } else if (*cp == '@') {
1318: *disable_echo = 1;
1319: } else {
1320: /* all other characters terminate prefix processing */
1321: break;
1322: }
1.44 djm 1323: }
1.187 djm 1324: cp = cp + strspn(cp, WHITESPACE);
1.118 dtucker 1325:
1326: /* Ignore blank lines and lines which begin with comment '#' char */
1327: if (*cp == '\0' || *cp == '#')
1328: return (0);
1.44 djm 1329:
1.116 djm 1330: if ((argv = makeargv(cp, &argc, 0, NULL, NULL)) == NULL)
1.97 djm 1331: return -1;
1332:
1.44 djm 1333: /* Figure out which command we have */
1.97 djm 1334: for (i = 0; cmds[i].c != NULL; i++) {
1.142 djm 1335: if (argv[0] != NULL && strcasecmp(cmds[i].c, argv[0]) == 0)
1.44 djm 1336: break;
1337: }
1338: cmdnum = cmds[i].n;
1339: cmd = cmds[i].c;
1340:
1341: /* Special case */
1342: if (*cp == '!') {
1343: cp++;
1344: cmdnum = I_SHELL;
1345: } else if (cmdnum == -1) {
1346: error("Invalid command.");
1.97 djm 1347: return -1;
1.44 djm 1348: }
1349:
1350: /* Get arguments and parse flags */
1.156 djm 1351: *aflag = *fflag = *hflag = *iflag = *lflag = *pflag = 0;
1352: *rflag = *sflag = 0;
1.44 djm 1353: *path1 = *path2 = NULL;
1.97 djm 1354: optidx = 1;
1.44 djm 1355: switch (cmdnum) {
1356: case I_GET:
1.148 djm 1357: case I_REGET:
1.159 logan 1358: case I_REPUT:
1.44 djm 1359: case I_PUT:
1.132 djm 1360: if ((optidx = parse_getput_flags(cmd, argv, argc,
1.156 djm 1361: aflag, fflag, pflag, rflag)) == -1)
1.97 djm 1362: return -1;
1.44 djm 1363: /* Get first pathname (mandatory) */
1.97 djm 1364: if (argc - optidx < 1) {
1.44 djm 1365: error("You must specify at least one path after a "
1366: "%s command.", cmd);
1.97 djm 1367: return -1;
1368: }
1369: *path1 = xstrdup(argv[optidx]);
1370: /* Get second pathname (optional) */
1371: if (argc - optidx > 1) {
1372: *path2 = xstrdup(argv[optidx + 1]);
1373: /* Destination is not globbed */
1374: undo_glob_escape(*path2);
1.44 djm 1375: }
1376: break;
1.132 djm 1377: case I_LINK:
1378: if ((optidx = parse_link_flags(cmd, argv, argc, sflag)) == -1)
1379: return -1;
1.152 djm 1380: goto parse_two_paths;
1381: case I_RENAME:
1382: if ((optidx = parse_rename_flags(cmd, argv, argc, lflag)) == -1)
1383: return -1;
1384: goto parse_two_paths;
1.132 djm 1385: case I_SYMLINK:
1.153 djm 1386: if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
1387: return -1;
1.152 djm 1388: parse_two_paths:
1.97 djm 1389: if (argc - optidx < 2) {
1.44 djm 1390: error("You must specify two paths after a %s "
1391: "command.", cmd);
1.97 djm 1392: return -1;
1.44 djm 1393: }
1.97 djm 1394: *path1 = xstrdup(argv[optidx]);
1395: *path2 = xstrdup(argv[optidx + 1]);
1396: /* Paths are not globbed */
1397: undo_glob_escape(*path1);
1398: undo_glob_escape(*path2);
1.44 djm 1399: break;
1400: case I_RM:
1401: case I_MKDIR:
1402: case I_RMDIR:
1.182 djm 1403: case I_LMKDIR:
1404: path1_mandatory = 1;
1405: /* FALLTHROUGH */
1.44 djm 1406: case I_CHDIR:
1407: case I_LCHDIR:
1.153 djm 1408: if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
1409: return -1;
1.44 djm 1410: /* Get pathname (mandatory) */
1.97 djm 1411: if (argc - optidx < 1) {
1.182 djm 1412: if (!path1_mandatory)
1413: break; /* return a NULL path1 */
1.44 djm 1414: error("You must specify a path after a %s command.",
1415: cmd);
1.97 djm 1416: return -1;
1.44 djm 1417: }
1.97 djm 1418: *path1 = xstrdup(argv[optidx]);
1419: /* Only "rm" globs */
1420: if (cmdnum != I_RM)
1421: undo_glob_escape(*path1);
1.44 djm 1422: break;
1.100 djm 1423: case I_DF:
1424: if ((optidx = parse_df_flags(cmd, argv, argc, hflag,
1425: iflag)) == -1)
1426: return -1;
1427: /* Default to current directory if no path specified */
1428: if (argc - optidx < 1)
1429: *path1 = NULL;
1430: else {
1431: *path1 = xstrdup(argv[optidx]);
1432: undo_glob_escape(*path1);
1433: }
1434: break;
1.44 djm 1435: case I_LS:
1.97 djm 1436: if ((optidx = parse_ls_flags(argv, argc, lflag)) == -1)
1.44 djm 1437: return(-1);
1438: /* Path is optional */
1.97 djm 1439: if (argc - optidx > 0)
1440: *path1 = xstrdup(argv[optidx]);
1.44 djm 1441: break;
1442: case I_LLS:
1.98 djm 1443: /* Skip ls command and following whitespace */
1444: cp = cp + strlen(cmd) + strspn(cp, WHITESPACE);
1.44 djm 1445: case I_SHELL:
1446: /* Uses the rest of the line */
1447: break;
1448: case I_LUMASK:
1449: case I_CHMOD:
1450: base = 8;
1.186 dtucker 1451: /* FALLTHROUGH */
1.44 djm 1452: case I_CHOWN:
1453: case I_CHGRP:
1.189 djm 1454: if ((optidx = parse_ch_flags(cmd, argv, argc, hflag)) == -1)
1.153 djm 1455: return -1;
1.44 djm 1456: /* Get numeric arg (mandatory) */
1.97 djm 1457: if (argc - optidx < 1)
1458: goto need_num_arg;
1.93 ray 1459: errno = 0;
1.97 djm 1460: l = strtol(argv[optidx], &cp2, base);
1461: if (cp2 == argv[optidx] || *cp2 != '\0' ||
1462: ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
1463: l < 0) {
1464: need_num_arg:
1.44 djm 1465: error("You must supply a numeric argument "
1466: "to the %s command.", cmd);
1.97 djm 1467: return -1;
1.44 djm 1468: }
1469: *n_arg = l;
1.97 djm 1470: if (cmdnum == I_LUMASK)
1.44 djm 1471: break;
1472: /* Get pathname (mandatory) */
1.97 djm 1473: if (argc - optidx < 2) {
1.44 djm 1474: error("You must specify a path after a %s command.",
1475: cmd);
1.97 djm 1476: return -1;
1.44 djm 1477: }
1.97 djm 1478: *path1 = xstrdup(argv[optidx + 1]);
1.44 djm 1479: break;
1480: case I_QUIT:
1481: case I_PWD:
1482: case I_LPWD:
1483: case I_HELP:
1484: case I_VERSION:
1485: case I_PROGRESS:
1.153 djm 1486: if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
1487: return -1;
1.44 djm 1488: break;
1489: default:
1490: fatal("Command not implemented");
1491: }
1492:
1493: *cpp = cp;
1494: return(cmdnum);
1495: }
1496:
1497: static int
1498: parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
1.187 djm 1499: const char *startdir, int err_abort, int echo_command)
1.44 djm 1500: {
1.187 djm 1501: const char *ocmd = cmd;
1.44 djm 1502: char *path1, *path2, *tmp;
1.187 djm 1503: int ignore_errors = 0, disable_echo = 1;
1504: int aflag = 0, fflag = 0, hflag = 0, iflag = 0;
1.156 djm 1505: int lflag = 0, pflag = 0, rflag = 0, sflag = 0;
1.132 djm 1506: int cmdnum, i;
1.107 dtucker 1507: unsigned long n_arg = 0;
1.44 djm 1508: Attrib a, *aa;
1.170 deraadt 1509: char path_buf[PATH_MAX];
1.44 djm 1510: int err = 0;
1511: glob_t g;
1512:
1513: path1 = path2 = NULL;
1.187 djm 1514: cmdnum = parse_args(&cmd, &ignore_errors, &disable_echo, &aflag, &fflag,
1515: &hflag, &iflag, &lflag, &pflag, &rflag, &sflag, &n_arg,
1516: &path1, &path2);
1.156 djm 1517: if (ignore_errors != 0)
1.44 djm 1518: err_abort = 0;
1519:
1.187 djm 1520: if (echo_command && !disable_echo)
1521: mprintf("sftp> %s\n", ocmd);
1522:
1.44 djm 1523: memset(&g, 0, sizeof(g));
1524:
1525: /* Perform command */
1526: switch (cmdnum) {
1527: case 0:
1528: /* Blank line */
1529: break;
1530: case -1:
1531: /* Unrecognized command */
1532: err = -1;
1533: break;
1.148 djm 1534: case I_REGET:
1535: aflag = 1;
1536: /* FALLTHROUGH */
1.44 djm 1537: case I_GET:
1.148 djm 1538: err = process_get(conn, path1, path2, *pwd, pflag,
1.156 djm 1539: rflag, aflag, fflag);
1.44 djm 1540: break;
1.159 logan 1541: case I_REPUT:
1542: aflag = 1;
1543: /* FALLTHROUGH */
1.44 djm 1544: case I_PUT:
1.156 djm 1545: err = process_put(conn, path1, path2, *pwd, pflag,
1.159 logan 1546: rflag, aflag, fflag);
1.44 djm 1547: break;
1548: case I_RENAME:
1549: path1 = make_absolute(path1, *pwd);
1550: path2 = make_absolute(path2, *pwd);
1.152 djm 1551: err = do_rename(conn, path1, path2, lflag);
1.44 djm 1552: break;
1553: case I_SYMLINK:
1.132 djm 1554: sflag = 1;
1.186 dtucker 1555: /* FALLTHROUGH */
1.132 djm 1556: case I_LINK:
1.151 djm 1557: if (!sflag)
1558: path1 = make_absolute(path1, *pwd);
1.44 djm 1559: path2 = make_absolute(path2, *pwd);
1.132 djm 1560: err = (sflag ? do_symlink : do_hardlink)(conn, path1, path2);
1.44 djm 1561: break;
1562: case I_RM:
1563: path1 = make_absolute(path1, *pwd);
1564: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46 djm 1565: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.143 djm 1566: if (!quiet)
1.174 schwarze 1567: mprintf("Removing %s\n", g.gl_pathv[i]);
1.44 djm 1568: err = do_rm(conn, g.gl_pathv[i]);
1569: if (err != 0 && err_abort)
1570: break;
1571: }
1572: break;
1573: case I_MKDIR:
1574: path1 = make_absolute(path1, *pwd);
1575: attrib_clear(&a);
1576: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1577: a.perm = 0777;
1.111 djm 1578: err = do_mkdir(conn, path1, &a, 1);
1.44 djm 1579: break;
1580: case I_RMDIR:
1581: path1 = make_absolute(path1, *pwd);
1582: err = do_rmdir(conn, path1);
1583: break;
1584: case I_CHDIR:
1.182 djm 1585: if (path1 == NULL || *path1 == '\0')
1586: path1 = xstrdup(startdir);
1.44 djm 1587: path1 = make_absolute(path1, *pwd);
1588: if ((tmp = do_realpath(conn, path1)) == NULL) {
1589: err = 1;
1590: break;
1591: }
1592: if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1.145 djm 1593: free(tmp);
1.44 djm 1594: err = 1;
1595: break;
1596: }
1597: if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
1598: error("Can't change directory: Can't check target");
1.145 djm 1599: free(tmp);
1.44 djm 1600: err = 1;
1601: break;
1602: }
1603: if (!S_ISDIR(aa->perm)) {
1604: error("Can't change directory: \"%s\" is not "
1605: "a directory", tmp);
1.145 djm 1606: free(tmp);
1.44 djm 1607: err = 1;
1608: break;
1609: }
1.145 djm 1610: free(*pwd);
1.44 djm 1611: *pwd = tmp;
1612: break;
1613: case I_LS:
1614: if (!path1) {
1.125 djm 1615: do_ls_dir(conn, *pwd, *pwd, lflag);
1.44 djm 1616: break;
1617: }
1618:
1619: /* Strip pwd off beginning of non-absolute paths */
1620: tmp = NULL;
1.188 djm 1621: if (!path_absolute(path1))
1.44 djm 1622: tmp = *pwd;
1623:
1624: path1 = make_absolute(path1, *pwd);
1625: err = do_globbed_ls(conn, path1, tmp, lflag);
1.100 djm 1626: break;
1627: case I_DF:
1628: /* Default to current directory if no path specified */
1629: if (path1 == NULL)
1630: path1 = xstrdup(*pwd);
1631: path1 = make_absolute(path1, *pwd);
1632: err = do_df(conn, path1, hflag, iflag);
1.44 djm 1633: break;
1634: case I_LCHDIR:
1.182 djm 1635: if (path1 == NULL || *path1 == '\0')
1636: path1 = xstrdup("~");
1.166 deraadt 1637: tmp = tilde_expand_filename(path1, getuid());
1.165 djm 1638: free(path1);
1639: path1 = tmp;
1.44 djm 1640: if (chdir(path1) == -1) {
1641: error("Couldn't change local directory to "
1642: "\"%s\": %s", path1, strerror(errno));
1643: err = 1;
1644: }
1645: break;
1646: case I_LMKDIR:
1647: if (mkdir(path1, 0777) == -1) {
1648: error("Couldn't create local directory "
1649: "\"%s\": %s", path1, strerror(errno));
1650: err = 1;
1651: }
1652: break;
1653: case I_LLS:
1654: local_do_ls(cmd);
1655: break;
1656: case I_SHELL:
1657: local_do_shell(cmd);
1658: break;
1659: case I_LUMASK:
1660: umask(n_arg);
1661: printf("Local umask: %03lo\n", n_arg);
1662: break;
1663: case I_CHMOD:
1664: path1 = make_absolute(path1, *pwd);
1665: attrib_clear(&a);
1666: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1667: a.perm = n_arg;
1668: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46 djm 1669: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.143 djm 1670: if (!quiet)
1.174 schwarze 1671: mprintf("Changing mode on %s\n",
1672: g.gl_pathv[i]);
1.189 djm 1673: err = (hflag ? do_lsetstat : do_setstat)(conn,
1674: g.gl_pathv[i], &a);
1.44 djm 1675: if (err != 0 && err_abort)
1676: break;
1677: }
1678: break;
1679: case I_CHOWN:
1680: case I_CHGRP:
1681: path1 = make_absolute(path1, *pwd);
1682: remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46 djm 1683: for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.189 djm 1684: if (!(aa = (hflag ? do_lstat : do_stat)(conn,
1685: g.gl_pathv[i], 0))) {
1.104 djm 1686: if (err_abort) {
1687: err = -1;
1.44 djm 1688: break;
1.104 djm 1689: } else
1.44 djm 1690: continue;
1691: }
1692: if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1693: error("Can't get current ownership of "
1694: "remote file \"%s\"", g.gl_pathv[i]);
1.104 djm 1695: if (err_abort) {
1696: err = -1;
1.44 djm 1697: break;
1.104 djm 1698: } else
1.44 djm 1699: continue;
1700: }
1701: aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1702: if (cmdnum == I_CHOWN) {
1.143 djm 1703: if (!quiet)
1.174 schwarze 1704: mprintf("Changing owner on %s\n",
1.143 djm 1705: g.gl_pathv[i]);
1.44 djm 1706: aa->uid = n_arg;
1707: } else {
1.143 djm 1708: if (!quiet)
1.174 schwarze 1709: mprintf("Changing group on %s\n",
1.143 djm 1710: g.gl_pathv[i]);
1.44 djm 1711: aa->gid = n_arg;
1712: }
1.189 djm 1713: err = (hflag ? do_lsetstat : do_setstat)(conn,
1714: g.gl_pathv[i], aa);
1.44 djm 1715: if (err != 0 && err_abort)
1716: break;
1717: }
1718: break;
1719: case I_PWD:
1.174 schwarze 1720: mprintf("Remote working directory: %s\n", *pwd);
1.44 djm 1721: break;
1722: case I_LPWD:
1723: if (!getcwd(path_buf, sizeof(path_buf))) {
1724: error("Couldn't get local cwd: %s", strerror(errno));
1725: err = -1;
1726: break;
1727: }
1.174 schwarze 1728: mprintf("Local working directory: %s\n", path_buf);
1.44 djm 1729: break;
1730: case I_QUIT:
1731: /* Processed below */
1732: break;
1733: case I_HELP:
1734: help();
1735: break;
1736: case I_VERSION:
1737: printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1738: break;
1739: case I_PROGRESS:
1740: showprogress = !showprogress;
1741: if (showprogress)
1742: printf("Progress meter enabled\n");
1743: else
1744: printf("Progress meter disabled\n");
1745: break;
1746: default:
1747: fatal("%d is not implemented", cmdnum);
1748: }
1749:
1750: if (g.gl_pathc)
1751: globfree(&g);
1.145 djm 1752: free(path1);
1753: free(path2);
1.44 djm 1754:
1755: /* If an unignored error occurs in batch mode we should abort. */
1756: if (err_abort && err != 0)
1757: return (-1);
1758: else if (cmdnum == I_QUIT)
1759: return (1);
1760:
1761: return (0);
1762: }
1763:
1.57 djm 1764: static char *
1765: prompt(EditLine *el)
1766: {
1767: return ("sftp> ");
1768: }
1769:
1.116 djm 1770: /* Display entries in 'list' after skipping the first 'len' chars */
1771: static void
1772: complete_display(char **list, u_int len)
1773: {
1774: u_int y, m = 0, width = 80, columns = 1, colspace = 0, llen;
1775: struct winsize ws;
1776: char *tmp;
1777:
1778: /* Count entries for sort and find longest */
1.149 djm 1779: for (y = 0; list[y]; y++)
1.176 deraadt 1780: m = MAXIMUM(m, strlen(list[y]));
1.116 djm 1781:
1782: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
1783: width = ws.ws_col;
1784:
1785: m = m > len ? m - len : 0;
1786: columns = width / (m + 2);
1.176 deraadt 1787: columns = MAXIMUM(columns, 1);
1.116 djm 1788: colspace = width / columns;
1.176 deraadt 1789: colspace = MINIMUM(colspace, width);
1.116 djm 1790:
1791: printf("\n");
1792: m = 1;
1793: for (y = 0; list[y]; y++) {
1794: llen = strlen(list[y]);
1795: tmp = llen > len ? list[y] + len : "";
1.174 schwarze 1796: mprintf("%-*s", colspace, tmp);
1.116 djm 1797: if (m >= columns) {
1798: printf("\n");
1799: m = 1;
1800: } else
1801: m++;
1802: }
1803: printf("\n");
1804: }
1805:
1806: /*
1807: * Given a "list" of words that begin with a common prefix of "word",
1808: * attempt to find an autocompletion to extends "word" by the next
1809: * characters common to all entries in "list".
1810: */
1811: static char *
1812: complete_ambiguous(const char *word, char **list, size_t count)
1813: {
1814: if (word == NULL)
1815: return NULL;
1816:
1817: if (count > 0) {
1818: u_int y, matchlen = strlen(list[0]);
1819:
1820: /* Find length of common stem */
1821: for (y = 1; list[y]; y++) {
1822: u_int x;
1823:
1.149 djm 1824: for (x = 0; x < matchlen; x++)
1825: if (list[0][x] != list[y][x])
1.116 djm 1826: break;
1827:
1828: matchlen = x;
1829: }
1830:
1831: if (matchlen > strlen(word)) {
1832: char *tmp = xstrdup(list[0]);
1833:
1.117 dtucker 1834: tmp[matchlen] = '\0';
1.116 djm 1835: return tmp;
1836: }
1.149 djm 1837: }
1.116 djm 1838:
1839: return xstrdup(word);
1840: }
1841:
1842: /* Autocomplete a sftp command */
1843: static int
1844: complete_cmd_parse(EditLine *el, char *cmd, int lastarg, char quote,
1845: int terminated)
1846: {
1847: u_int y, count = 0, cmdlen, tmplen;
1848: char *tmp, **list, argterm[3];
1849: const LineInfo *lf;
1850:
1851: list = xcalloc((sizeof(cmds) / sizeof(*cmds)) + 1, sizeof(char *));
1852:
1853: /* No command specified: display all available commands */
1854: if (cmd == NULL) {
1855: for (y = 0; cmds[y].c; y++)
1856: list[count++] = xstrdup(cmds[y].c);
1.149 djm 1857:
1.116 djm 1858: list[count] = NULL;
1859: complete_display(list, 0);
1860:
1.149 djm 1861: for (y = 0; list[y] != NULL; y++)
1862: free(list[y]);
1.145 djm 1863: free(list);
1.116 djm 1864: return count;
1865: }
1866:
1867: /* Prepare subset of commands that start with "cmd" */
1868: cmdlen = strlen(cmd);
1869: for (y = 0; cmds[y].c; y++) {
1.149 djm 1870: if (!strncasecmp(cmd, cmds[y].c, cmdlen))
1.116 djm 1871: list[count++] = xstrdup(cmds[y].c);
1872: }
1873: list[count] = NULL;
1874:
1.134 oga 1875: if (count == 0) {
1.145 djm 1876: free(list);
1.116 djm 1877: return 0;
1.134 oga 1878: }
1.116 djm 1879:
1.183 djm 1880: /* Complete ambiguous command */
1.116 djm 1881: tmp = complete_ambiguous(cmd, list, count);
1882: if (count > 1)
1883: complete_display(list, 0);
1884:
1.149 djm 1885: for (y = 0; list[y]; y++)
1886: free(list[y]);
1.145 djm 1887: free(list);
1.116 djm 1888:
1889: if (tmp != NULL) {
1890: tmplen = strlen(tmp);
1891: cmdlen = strlen(cmd);
1892: /* If cmd may be extended then do so */
1893: if (tmplen > cmdlen)
1894: if (el_insertstr(el, tmp + cmdlen) == -1)
1895: fatal("el_insertstr failed.");
1896: lf = el_line(el);
1897: /* Terminate argument cleanly */
1898: if (count == 1) {
1899: y = 0;
1900: if (!terminated)
1901: argterm[y++] = quote;
1902: if (lastarg || *(lf->cursor) != ' ')
1903: argterm[y++] = ' ';
1904: argterm[y] = '\0';
1905: if (y > 0 && el_insertstr(el, argterm) == -1)
1906: fatal("el_insertstr failed.");
1907: }
1.145 djm 1908: free(tmp);
1.116 djm 1909: }
1910:
1911: return count;
1912: }
1913:
1914: /*
1915: * Determine whether a particular sftp command's arguments (if any)
1916: * represent local or remote files.
1917: */
1918: static int
1919: complete_is_remote(char *cmd) {
1920: int i;
1921:
1922: if (cmd == NULL)
1923: return -1;
1924:
1925: for (i = 0; cmds[i].c; i++) {
1.149 djm 1926: if (!strncasecmp(cmd, cmds[i].c, strlen(cmds[i].c)))
1.116 djm 1927: return cmds[i].t;
1928: }
1929:
1930: return -1;
1931: }
1932:
1933: /* Autocomplete a filename "file" */
1934: static int
1935: complete_match(EditLine *el, struct sftp_conn *conn, char *remote_path,
1936: char *file, int remote, int lastarg, char quote, int terminated)
1937: {
1938: glob_t g;
1.146 dtucker 1939: char *tmp, *tmp2, ins[8];
1.140 dtucker 1940: u_int i, hadglob, pwdlen, len, tmplen, filelen, cesc, isesc, isabs;
1.146 dtucker 1941: int clen;
1.116 djm 1942: const LineInfo *lf;
1.149 djm 1943:
1.116 djm 1944: /* Glob from "file" location */
1945: if (file == NULL)
1946: tmp = xstrdup("*");
1947: else
1948: xasprintf(&tmp, "%s*", file);
1949:
1.139 dtucker 1950: /* Check if the path is absolute. */
1.188 djm 1951: isabs = path_absolute(tmp);
1.139 dtucker 1952:
1.116 djm 1953: memset(&g, 0, sizeof(g));
1954: if (remote != LOCAL) {
1955: tmp = make_absolute(tmp, remote_path);
1956: remote_glob(conn, tmp, GLOB_DOOFFS|GLOB_MARK, NULL, &g);
1.149 djm 1957: } else
1.116 djm 1958: glob(tmp, GLOB_DOOFFS|GLOB_MARK, NULL, &g);
1.149 djm 1959:
1.116 djm 1960: /* Determine length of pwd so we can trim completion display */
1961: for (hadglob = tmplen = pwdlen = 0; tmp[tmplen] != 0; tmplen++) {
1962: /* Terminate counting on first unescaped glob metacharacter */
1963: if (tmp[tmplen] == '*' || tmp[tmplen] == '?') {
1964: if (tmp[tmplen] != '*' || tmp[tmplen + 1] != '\0')
1965: hadglob = 1;
1966: break;
1967: }
1968: if (tmp[tmplen] == '\\' && tmp[tmplen + 1] != '\0')
1969: tmplen++;
1970: if (tmp[tmplen] == '/')
1971: pwdlen = tmplen + 1; /* track last seen '/' */
1972: }
1.145 djm 1973: free(tmp);
1.161 dtucker 1974: tmp = NULL;
1.116 djm 1975:
1.149 djm 1976: if (g.gl_matchc == 0)
1.116 djm 1977: goto out;
1978:
1979: if (g.gl_matchc > 1)
1980: complete_display(g.gl_pathv, pwdlen);
1981:
1982: /* Don't try to extend globs */
1983: if (file == NULL || hadglob)
1984: goto out;
1985:
1986: tmp2 = complete_ambiguous(file, g.gl_pathv, g.gl_matchc);
1.139 dtucker 1987: tmp = path_strip(tmp2, isabs ? NULL : remote_path);
1.145 djm 1988: free(tmp2);
1.116 djm 1989:
1990: if (tmp == NULL)
1991: goto out;
1992:
1993: tmplen = strlen(tmp);
1994: filelen = strlen(file);
1995:
1.140 dtucker 1996: /* Count the number of escaped characters in the input string. */
1997: cesc = isesc = 0;
1998: for (i = 0; i < filelen; i++) {
1999: if (!isesc && file[i] == '\\' && i + 1 < filelen){
2000: isesc = 1;
2001: cesc++;
2002: } else
2003: isesc = 0;
2004: }
2005:
2006: if (tmplen > (filelen - cesc)) {
2007: tmp2 = tmp + filelen - cesc;
1.149 djm 2008: len = strlen(tmp2);
1.116 djm 2009: /* quote argument on way out */
1.146 dtucker 2010: for (i = 0; i < len; i += clen) {
2011: if ((clen = mblen(tmp2 + i, len - i)) < 0 ||
2012: (size_t)clen > sizeof(ins) - 2)
2013: fatal("invalid multibyte character");
1.116 djm 2014: ins[0] = '\\';
1.146 dtucker 2015: memcpy(ins + 1, tmp2 + i, clen);
2016: ins[clen + 1] = '\0';
1.116 djm 2017: switch (tmp2[i]) {
2018: case '\'':
2019: case '"':
2020: case '\\':
2021: case '\t':
1.131 sthen 2022: case '[':
1.116 djm 2023: case ' ':
1.140 dtucker 2024: case '#':
2025: case '*':
1.116 djm 2026: if (quote == '\0' || tmp2[i] == quote) {
2027: if (el_insertstr(el, ins) == -1)
2028: fatal("el_insertstr "
2029: "failed.");
2030: break;
2031: }
2032: /* FALLTHROUGH */
2033: default:
2034: if (el_insertstr(el, ins + 1) == -1)
2035: fatal("el_insertstr failed.");
2036: break;
2037: }
2038: }
2039: }
2040:
2041: lf = el_line(el);
2042: if (g.gl_matchc == 1) {
2043: i = 0;
1.162 dtucker 2044: if (!terminated && quote != '\0')
1.116 djm 2045: ins[i++] = quote;
1.120 djm 2046: if (*(lf->cursor - 1) != '/' &&
2047: (lastarg || *(lf->cursor) != ' '))
1.116 djm 2048: ins[i++] = ' ';
2049: ins[i] = '\0';
2050: if (i > 0 && el_insertstr(el, ins) == -1)
2051: fatal("el_insertstr failed.");
2052: }
1.145 djm 2053: free(tmp);
1.116 djm 2054:
2055: out:
2056: globfree(&g);
2057: return g.gl_matchc;
2058: }
2059:
2060: /* tab-completion hook function, called via libedit */
2061: static unsigned char
2062: complete(EditLine *el, int ch)
2063: {
1.149 djm 2064: char **argv, *line, quote;
1.147 djm 2065: int argc, carg;
2066: u_int cursor, len, terminated, ret = CC_ERROR;
1.116 djm 2067: const LineInfo *lf;
2068: struct complete_ctx *complete_ctx;
2069:
2070: lf = el_line(el);
2071: if (el_get(el, EL_CLIENTDATA, (void**)&complete_ctx) != 0)
2072: fatal("%s: el_get failed", __func__);
2073:
2074: /* Figure out which argument the cursor points to */
2075: cursor = lf->cursor - lf->buffer;
1.171 deraadt 2076: line = xmalloc(cursor + 1);
1.116 djm 2077: memcpy(line, lf->buffer, cursor);
2078: line[cursor] = '\0';
2079: argv = makeargv(line, &carg, 1, "e, &terminated);
1.145 djm 2080: free(line);
1.116 djm 2081:
2082: /* Get all the arguments on the line */
2083: len = lf->lastchar - lf->buffer;
1.171 deraadt 2084: line = xmalloc(len + 1);
1.116 djm 2085: memcpy(line, lf->buffer, len);
2086: line[len] = '\0';
2087: argv = makeargv(line, &argc, 1, NULL, NULL);
2088:
2089: /* Ensure cursor is at EOL or a argument boundary */
2090: if (line[cursor] != ' ' && line[cursor] != '\0' &&
2091: line[cursor] != '\n') {
1.145 djm 2092: free(line);
1.116 djm 2093: return ret;
2094: }
2095:
2096: if (carg == 0) {
2097: /* Show all available commands */
2098: complete_cmd_parse(el, NULL, argc == carg, '\0', 1);
2099: ret = CC_REDISPLAY;
2100: } else if (carg == 1 && cursor > 0 && line[cursor - 1] != ' ') {
2101: /* Handle the command parsing */
2102: if (complete_cmd_parse(el, argv[0], argc == carg,
1.149 djm 2103: quote, terminated) != 0)
1.116 djm 2104: ret = CC_REDISPLAY;
2105: } else if (carg >= 1) {
2106: /* Handle file parsing */
2107: int remote = complete_is_remote(argv[0]);
2108: char *filematch = NULL;
2109:
2110: if (carg > 1 && line[cursor-1] != ' ')
2111: filematch = argv[carg - 1];
2112:
2113: if (remote != 0 &&
2114: complete_match(el, complete_ctx->conn,
2115: *complete_ctx->remote_pathp, filematch,
1.149 djm 2116: remote, carg == argc, quote, terminated) != 0)
1.116 djm 2117: ret = CC_REDISPLAY;
2118: }
2119:
1.149 djm 2120: free(line);
1.116 djm 2121: return ret;
2122: }
2123:
1.182 djm 2124: static int
1.112 djm 2125: interactive_loop(struct sftp_conn *conn, char *file1, char *file2)
1.44 djm 2126: {
1.116 djm 2127: char *remote_path;
1.182 djm 2128: char *dir = NULL, *startdir = NULL;
1.44 djm 2129: char cmd[2048];
1.66 jaredy 2130: int err, interactive;
1.57 djm 2131: EditLine *el = NULL;
2132: History *hl = NULL;
2133: HistEvent hev;
2134: extern char *__progname;
1.116 djm 2135: struct complete_ctx complete_ctx;
1.57 djm 2136:
2137: if (!batchmode && isatty(STDIN_FILENO)) {
2138: if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL)
2139: fatal("Couldn't initialise editline");
2140: if ((hl = history_init()) == NULL)
2141: fatal("Couldn't initialise editline history");
2142: history(hl, &hev, H_SETSIZE, 100);
2143: el_set(el, EL_HIST, history, hl);
2144:
2145: el_set(el, EL_PROMPT, prompt);
2146: el_set(el, EL_EDITOR, "emacs");
2147: el_set(el, EL_TERMINAL, NULL);
2148: el_set(el, EL_SIGNAL, 1);
2149: el_source(el, NULL);
1.116 djm 2150:
2151: /* Tab Completion */
1.149 djm 2152: el_set(el, EL_ADDFN, "ftp-complete",
1.131 sthen 2153: "Context sensitive argument completion", complete);
1.116 djm 2154: complete_ctx.conn = conn;
2155: complete_ctx.remote_pathp = &remote_path;
2156: el_set(el, EL_CLIENTDATA, (void*)&complete_ctx);
2157: el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
1.154 djm 2158: /* enable ctrl-left-arrow and ctrl-right-arrow */
2159: el_set(el, EL_BIND, "\\e[1;5C", "em-next-word", NULL);
1.194 tb 2160: el_set(el, EL_BIND, "\\e\\e[C", "em-next-word", NULL);
1.154 djm 2161: el_set(el, EL_BIND, "\\e[1;5D", "ed-prev-word", NULL);
2162: el_set(el, EL_BIND, "\\e\\e[D", "ed-prev-word", NULL);
1.155 djm 2163: /* make ^w match ksh behaviour */
2164: el_set(el, EL_BIND, "^w", "ed-delete-prev-word", NULL);
1.57 djm 2165: }
1.44 djm 2166:
1.116 djm 2167: remote_path = do_realpath(conn, ".");
2168: if (remote_path == NULL)
1.44 djm 2169: fatal("Need cwd");
1.182 djm 2170: startdir = xstrdup(remote_path);
1.44 djm 2171:
2172: if (file1 != NULL) {
2173: dir = xstrdup(file1);
1.116 djm 2174: dir = make_absolute(dir, remote_path);
1.44 djm 2175:
2176: if (remote_is_dir(conn, dir) && file2 == NULL) {
1.143 djm 2177: if (!quiet)
1.174 schwarze 2178: mprintf("Changing to: %s\n", dir);
1.44 djm 2179: snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
1.116 djm 2180: if (parse_dispatch_command(conn, cmd,
1.187 djm 2181: &remote_path, startdir, 1, 0) != 0) {
1.145 djm 2182: free(dir);
1.182 djm 2183: free(startdir);
1.145 djm 2184: free(remote_path);
2185: free(conn);
1.44 djm 2186: return (-1);
1.58 markus 2187: }
1.44 djm 2188: } else {
1.137 djm 2189: /* XXX this is wrong wrt quoting */
1.148 djm 2190: snprintf(cmd, sizeof cmd, "get%s %s%s%s",
2191: global_aflag ? " -a" : "", dir,
2192: file2 == NULL ? "" : " ",
2193: file2 == NULL ? "" : file2);
1.116 djm 2194: err = parse_dispatch_command(conn, cmd,
1.187 djm 2195: &remote_path, startdir, 1, 0);
1.145 djm 2196: free(dir);
1.182 djm 2197: free(startdir);
1.145 djm 2198: free(remote_path);
2199: free(conn);
1.44 djm 2200: return (err);
2201: }
1.145 djm 2202: free(dir);
1.44 djm 2203: }
2204:
1.168 millert 2205: setvbuf(stdout, NULL, _IOLBF, 0);
2206: setvbuf(infile, NULL, _IOLBF, 0);
1.44 djm 2207:
1.66 jaredy 2208: interactive = !batchmode && isatty(STDIN_FILENO);
1.44 djm 2209: err = 0;
2210: for (;;) {
1.57 djm 2211: const char *line;
2212: int count = 0;
1.44 djm 2213:
1.46 djm 2214: signal(SIGINT, SIG_IGN);
2215:
1.57 djm 2216: if (el == NULL) {
1.66 jaredy 2217: if (interactive)
2218: printf("sftp> ");
1.57 djm 2219: if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1.66 jaredy 2220: if (interactive)
2221: printf("\n");
1.57 djm 2222: break;
2223: }
2224: } else {
1.116 djm 2225: if ((line = el_gets(el, &count)) == NULL ||
2226: count <= 0) {
1.66 jaredy 2227: printf("\n");
1.57 djm 2228: break;
1.66 jaredy 2229: }
1.57 djm 2230: history(hl, &hev, H_ENTER, line);
2231: if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
2232: fprintf(stderr, "Error: input line too long\n");
2233: continue;
2234: }
1.44 djm 2235: }
2236:
1.187 djm 2237: cmd[strcspn(cmd, "\n")] = '\0';
1.44 djm 2238:
1.46 djm 2239: /* Handle user interrupts gracefully during commands */
2240: interrupted = 0;
2241: signal(SIGINT, cmd_interrupt);
2242:
1.116 djm 2243: err = parse_dispatch_command(conn, cmd, &remote_path,
1.187 djm 2244: startdir, batchmode, !interactive && el == NULL);
1.44 djm 2245: if (err != 0)
2246: break;
2247: }
1.184 djm 2248: signal(SIGCHLD, SIG_DFL);
1.145 djm 2249: free(remote_path);
1.182 djm 2250: free(startdir);
1.145 djm 2251: free(conn);
1.66 jaredy 2252:
2253: if (el != NULL)
2254: el_end(el);
1.44 djm 2255:
2256: /* err == 1 signifies normal "quit" exit */
2257: return (err >= 0 ? 0 : -1);
2258: }
1.34 fgsch 2259:
1.18 itojun 2260: static void
1.36 djm 2261: connect_to_server(char *path, char **args, int *in, int *out)
1.1 djm 2262: {
2263: int c_in, c_out;
1.30 deraadt 2264:
1.1 djm 2265: int inout[2];
1.30 deraadt 2266:
1.1 djm 2267: if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
2268: fatal("socketpair: %s", strerror(errno));
2269: *in = *out = inout[0];
2270: c_in = c_out = inout[1];
2271:
1.36 djm 2272: if ((sshpid = fork()) == -1)
1.1 djm 2273: fatal("fork: %s", strerror(errno));
1.36 djm 2274: else if (sshpid == 0) {
1.1 djm 2275: if ((dup2(c_in, STDIN_FILENO) == -1) ||
2276: (dup2(c_out, STDOUT_FILENO) == -1)) {
2277: fprintf(stderr, "dup2: %s\n", strerror(errno));
1.47 djm 2278: _exit(1);
1.1 djm 2279: }
2280: close(*in);
2281: close(*out);
2282: close(c_in);
2283: close(c_out);
1.46 djm 2284:
2285: /*
2286: * The underlying ssh is in the same process group, so we must
1.56 deraadt 2287: * ignore SIGINT if we want to gracefully abort commands,
2288: * otherwise the signal will make it to the ssh process and
1.122 guenther 2289: * kill it too. Contrawise, since sftp sends SIGTERMs to the
2290: * underlying ssh, it must *not* ignore that signal.
1.46 djm 2291: */
2292: signal(SIGINT, SIG_IGN);
1.122 guenther 2293: signal(SIGTERM, SIG_DFL);
1.49 dtucker 2294: execvp(path, args);
1.23 djm 2295: fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
1.47 djm 2296: _exit(1);
1.1 djm 2297: }
2298:
1.36 djm 2299: signal(SIGTERM, killchild);
2300: signal(SIGINT, killchild);
2301: signal(SIGHUP, killchild);
1.177 millert 2302: signal(SIGTSTP, suspchild);
2303: signal(SIGTTIN, suspchild);
2304: signal(SIGTTOU, suspchild);
1.184 djm 2305: signal(SIGCHLD, sigchld_handler);
1.1 djm 2306: close(c_in);
2307: close(c_out);
2308: }
2309:
1.18 itojun 2310: static void
1.1 djm 2311: usage(void)
2312: {
1.25 mpech 2313: extern char *__progname;
1.27 markus 2314:
1.19 stevesk 2315: fprintf(stderr,
1.179 djm 2316: "usage: %s [-46aCfpqrv] [-B buffer_size] [-b batchfile] [-c cipher]\n"
1.190 tb 2317: " [-D sftp_server_path] [-F ssh_config] [-i identity_file]\n"
2318: " [-J destination] [-l limit] [-o ssh_option] [-P port]\n"
2319: " [-R num_requests] [-S program] [-s subsystem | sftp_server]\n"
2320: " destination\n",
1.181 millert 2321: __progname);
1.1 djm 2322: exit(1);
2323: }
2324:
1.2 stevesk 2325: int
1.1 djm 2326: main(int argc, char **argv)
2327: {
1.181 millert 2328: int in, out, ch, err, tmp, port = -1;
2329: char *host = NULL, *user, *cp, *file2 = NULL;
1.17 mouring 2330: int debug_level = 0, sshver = 2;
2331: char *file1 = NULL, *sftp_server = NULL;
1.23 djm 2332: char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
1.126 djm 2333: const char *errstr;
1.17 mouring 2334: LogLevel ll = SYSLOG_LEVEL_INFO;
2335: arglist args;
1.3 djm 2336: extern int optind;
2337: extern char *optarg;
1.112 djm 2338: struct sftp_conn *conn;
2339: size_t copy_buffer_len = DEFAULT_COPY_BUFLEN;
2340: size_t num_requests = DEFAULT_NUM_REQUESTS;
1.126 djm 2341: long long limit_kbps = 0;
1.67 djm 2342:
2343: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2344: sanitise_stdfd();
1.146 dtucker 2345: setlocale(LC_CTYPE, "");
1.1 djm 2346:
1.70 djm 2347: memset(&args, '\0', sizeof(args));
1.17 mouring 2348: args.list = NULL;
1.80 djm 2349: addargs(&args, "%s", ssh_program);
1.17 mouring 2350: addargs(&args, "-oForwardX11 no");
2351: addargs(&args, "-oForwardAgent no");
1.69 reyk 2352: addargs(&args, "-oPermitLocalCommand no");
1.21 stevesk 2353: addargs(&args, "-oClearAllForwardings yes");
1.40 djm 2354:
1.17 mouring 2355: ll = SYSLOG_LEVEL_INFO;
1.40 djm 2356: infile = stdin;
1.3 djm 2357:
1.109 djm 2358: while ((ch = getopt(argc, argv,
1.190 tb 2359: "1246afhpqrvCc:D:i:l:o:s:S:b:B:F:J:P:R:")) != -1) {
1.3 djm 2360: switch (ch) {
1.108 djm 2361: /* Passed through to ssh(1) */
2362: case '4':
2363: case '6':
1.3 djm 2364: case 'C':
1.108 djm 2365: addargs(&args, "-%c", ch);
2366: break;
2367: /* Passed through to ssh(1) with argument */
2368: case 'F':
1.190 tb 2369: case 'J':
1.108 djm 2370: case 'c':
2371: case 'i':
2372: case 'o':
1.113 halex 2373: addargs(&args, "-%c", ch);
2374: addargs(&args, "%s", optarg);
1.108 djm 2375: break;
2376: case 'q':
1.143 djm 2377: ll = SYSLOG_LEVEL_ERROR;
2378: quiet = 1;
1.108 djm 2379: showprogress = 0;
2380: addargs(&args, "-%c", ch);
1.3 djm 2381: break;
1.109 djm 2382: case 'P':
1.181 millert 2383: port = a2port(optarg);
2384: if (port <= 0)
2385: fatal("Bad port \"%s\"\n", optarg);
1.109 djm 2386: break;
1.3 djm 2387: case 'v':
1.17 mouring 2388: if (debug_level < 3) {
2389: addargs(&args, "-v");
2390: ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
2391: }
2392: debug_level++;
1.3 djm 2393: break;
1.7 markus 2394: case '1':
1.17 mouring 2395: sshver = 1;
1.7 markus 2396: if (sftp_server == NULL)
2397: sftp_server = _PATH_SFTP_SERVER;
2398: break;
1.108 djm 2399: case '2':
2400: sshver = 2;
1.148 djm 2401: break;
2402: case 'a':
2403: global_aflag = 1;
1.7 markus 2404: break;
1.108 djm 2405: case 'B':
2406: copy_buffer_len = strtol(optarg, &cp, 10);
2407: if (copy_buffer_len == 0 || *cp != '\0')
2408: fatal("Invalid buffer size \"%s\"", optarg);
1.3 djm 2409: break;
1.10 deraadt 2410: case 'b':
1.39 djm 2411: if (batchmode)
2412: fatal("Batch file already specified.");
2413:
2414: /* Allow "-" as stdin */
1.56 deraadt 2415: if (strcmp(optarg, "-") != 0 &&
1.65 djm 2416: (infile = fopen(optarg, "r")) == NULL)
1.39 djm 2417: fatal("%s (%s).", strerror(errno), optarg);
1.34 fgsch 2418: showprogress = 0;
1.143 djm 2419: quiet = batchmode = 1;
1.62 djm 2420: addargs(&args, "-obatchmode yes");
1.156 djm 2421: break;
2422: case 'f':
2423: global_fflag = 1;
1.10 deraadt 2424: break;
1.111 djm 2425: case 'p':
2426: global_pflag = 1;
2427: break;
1.109 djm 2428: case 'D':
1.23 djm 2429: sftp_direct = optarg;
1.111 djm 2430: break;
1.126 djm 2431: case 'l':
2432: limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
2433: &errstr);
2434: if (errstr != NULL)
2435: usage();
2436: limit_kbps *= 1024; /* kbps */
2437: break;
1.111 djm 2438: case 'r':
2439: global_rflag = 1;
1.24 djm 2440: break;
1.26 djm 2441: case 'R':
2442: num_requests = strtol(optarg, &cp, 10);
2443: if (num_requests == 0 || *cp != '\0')
1.27 markus 2444: fatal("Invalid number of requests \"%s\"",
1.26 djm 2445: optarg);
1.108 djm 2446: break;
2447: case 's':
2448: sftp_server = optarg;
2449: break;
2450: case 'S':
2451: ssh_program = optarg;
2452: replacearg(&args, 0, "%s", ssh_program);
1.23 djm 2453: break;
1.3 djm 2454: case 'h':
2455: default:
1.1 djm 2456: usage();
2457: }
2458: }
1.45 djm 2459:
2460: if (!isatty(STDERR_FILENO))
2461: showprogress = 0;
1.1 djm 2462:
1.29 markus 2463: log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
2464:
1.23 djm 2465: if (sftp_direct == NULL) {
2466: if (optind == argc || argc > (optind + 2))
2467: usage();
1.181 millert 2468: argv += optind;
1.23 djm 2469:
1.181 millert 2470: switch (parse_uri("sftp", *argv, &user, &host, &tmp, &file1)) {
2471: case -1:
2472: usage();
2473: break;
2474: case 0:
2475: if (tmp != -1)
2476: port = tmp;
2477: break;
2478: default:
1.192 dtucker 2479: /* Try with user, host and path. */
1.181 millert 2480: if (parse_user_host_path(*argv, &user, &host,
1.192 dtucker 2481: &file1) == 0)
2482: break;
2483: /* Try with user and host. */
2484: if (parse_user_host_port(*argv, &user, &host, NULL)
2485: == 0)
2486: break;
2487: /* Treat as a plain hostname. */
2488: host = xstrdup(*argv);
2489: host = cleanhostname(host);
1.181 millert 2490: break;
1.23 djm 2491: }
1.181 millert 2492: file2 = *(argv + 1);
1.3 djm 2493:
1.23 djm 2494: if (!*host) {
2495: fprintf(stderr, "Missing hostname\n");
1.1 djm 2496: usage();
2497: }
2498:
1.181 millert 2499: if (port != -1)
2500: addargs(&args, "-oPort %d", port);
2501: if (user != NULL) {
2502: addargs(&args, "-l");
2503: addargs(&args, "%s", user);
2504: }
1.23 djm 2505: addargs(&args, "-oProtocol %d", sshver);
2506:
2507: /* no subsystem if the server-spec contains a '/' */
2508: if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
2509: addargs(&args, "-s");
2510:
1.115 guenther 2511: addargs(&args, "--");
1.23 djm 2512: addargs(&args, "%s", host);
1.27 markus 2513: addargs(&args, "%s", (sftp_server != NULL ?
1.23 djm 2514: sftp_server : "sftp"));
2515:
1.36 djm 2516: connect_to_server(ssh_program, args.list, &in, &out);
1.23 djm 2517: } else {
2518: args.list = NULL;
2519: addargs(&args, "sftp-server");
2520:
1.36 djm 2521: connect_to_server(sftp_direct, args.list, &in, &out);
1.1 djm 2522: }
1.70 djm 2523: freeargs(&args);
1.1 djm 2524:
1.126 djm 2525: conn = do_init(in, out, copy_buffer_len, num_requests, limit_kbps);
1.112 djm 2526: if (conn == NULL)
2527: fatal("Couldn't initialise connection to server");
2528:
1.143 djm 2529: if (!quiet) {
1.112 djm 2530: if (sftp_direct == NULL)
2531: fprintf(stderr, "Connected to %s.\n", host);
2532: else
2533: fprintf(stderr, "Attached to %s.\n", sftp_direct);
2534: }
2535:
2536: err = interactive_loop(conn, file1, file2);
1.1 djm 2537:
2538: close(in);
2539: close(out);
1.39 djm 2540: if (batchmode)
1.10 deraadt 2541: fclose(infile);
1.1 djm 2542:
1.185 bluhm 2543: while (waitpid(sshpid, NULL, 0) == -1 && sshpid > 1)
1.28 markus 2544: if (errno != EINTR)
2545: fatal("Couldn't wait for ssh process: %s",
2546: strerror(errno));
1.1 djm 2547:
1.33 djm 2548: exit(err == 0 ? 0 : 1);
1.1 djm 2549: }