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