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