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