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