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