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