Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1 | /* |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 2 | * Copyright (c) 2001,2002 Damien Miller. All rights reserved. |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 25 | /* XXX: recursive operations */ |
| 26 | |
| 27 | #include "includes.h" |
Darren Tucker | 7858726 | 2003-08-26 12:12:56 +1000 | [diff] [blame] | 28 | RCSID("$OpenBSD: sftp-int.c,v 1.62 2003/08/25 08:13:09 fgsch Exp $"); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 29 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 30 | #include "buffer.h" |
| 31 | #include "xmalloc.h" |
| 32 | #include "log.h" |
| 33 | #include "pathnames.h" |
| 34 | |
| 35 | #include "sftp.h" |
| 36 | #include "sftp-common.h" |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 37 | #include "sftp-glob.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 38 | #include "sftp-client.h" |
| 39 | #include "sftp-int.h" |
| 40 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 41 | /* File to read commands from */ |
| 42 | extern FILE *infile; |
| 43 | |
Damien Miller | 8829d36 | 2002-02-08 22:04:05 +1100 | [diff] [blame] | 44 | /* Size of buffer used when copying files */ |
| 45 | extern size_t copy_buffer_len; |
| 46 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 47 | /* Number of concurrent outstanding requests */ |
| 48 | extern int num_requests; |
| 49 | |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 50 | /* This is set to 0 if the progressmeter is not desired. */ |
| 51 | int showprogress = 1; |
| 52 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 53 | /* Seperators for interactive commands */ |
| 54 | #define WHITESPACE " \t\r\n" |
| 55 | |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 56 | /* Define what type of ls view (0 - multi-column) */ |
| 57 | #define LONG_VIEW 1 /* Full view ala ls -l */ |
| 58 | #define SHORT_VIEW 2 /* Single row view ala ls -1 */ |
| 59 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 60 | /* Commands for interactive mode */ |
| 61 | #define I_CHDIR 1 |
| 62 | #define I_CHGRP 2 |
| 63 | #define I_CHMOD 3 |
| 64 | #define I_CHOWN 4 |
| 65 | #define I_GET 5 |
| 66 | #define I_HELP 6 |
| 67 | #define I_LCHDIR 7 |
| 68 | #define I_LLS 8 |
| 69 | #define I_LMKDIR 9 |
| 70 | #define I_LPWD 10 |
| 71 | #define I_LS 11 |
| 72 | #define I_LUMASK 12 |
| 73 | #define I_MKDIR 13 |
| 74 | #define I_PUT 14 |
| 75 | #define I_PWD 15 |
| 76 | #define I_QUIT 16 |
| 77 | #define I_RENAME 17 |
| 78 | #define I_RM 18 |
| 79 | #define I_RMDIR 19 |
| 80 | #define I_SHELL 20 |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 81 | #define I_SYMLINK 21 |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 82 | #define I_VERSION 22 |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 83 | #define I_PROGRESS 23 |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 84 | |
| 85 | struct CMD { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 86 | const char *c; |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 87 | const int n; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 88 | }; |
| 89 | |
Damien Miller | dc70857 | 2003-01-14 22:24:05 +1100 | [diff] [blame] | 90 | static const struct CMD cmds[] = { |
Ben Lindstrom | 59e1249 | 2001-08-15 23:22:56 +0000 | [diff] [blame] | 91 | { "bye", I_QUIT }, |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 92 | { "cd", I_CHDIR }, |
| 93 | { "chdir", I_CHDIR }, |
| 94 | { "chgrp", I_CHGRP }, |
| 95 | { "chmod", I_CHMOD }, |
| 96 | { "chown", I_CHOWN }, |
| 97 | { "dir", I_LS }, |
| 98 | { "exit", I_QUIT }, |
| 99 | { "get", I_GET }, |
Ben Lindstrom | 23d9a6d | 2001-04-11 23:05:17 +0000 | [diff] [blame] | 100 | { "mget", I_GET }, |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 101 | { "help", I_HELP }, |
| 102 | { "lcd", I_LCHDIR }, |
| 103 | { "lchdir", I_LCHDIR }, |
| 104 | { "lls", I_LLS }, |
| 105 | { "lmkdir", I_LMKDIR }, |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 106 | { "ln", I_SYMLINK }, |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 107 | { "lpwd", I_LPWD }, |
| 108 | { "ls", I_LS }, |
| 109 | { "lumask", I_LUMASK }, |
| 110 | { "mkdir", I_MKDIR }, |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 111 | { "progress", I_PROGRESS }, |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 112 | { "put", I_PUT }, |
Ben Lindstrom | 23d9a6d | 2001-04-11 23:05:17 +0000 | [diff] [blame] | 113 | { "mput", I_PUT }, |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 114 | { "pwd", I_PWD }, |
| 115 | { "quit", I_QUIT }, |
| 116 | { "rename", I_RENAME }, |
| 117 | { "rm", I_RM }, |
| 118 | { "rmdir", I_RMDIR }, |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 119 | { "symlink", I_SYMLINK }, |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 120 | { "version", I_VERSION }, |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 121 | { "!", I_SHELL }, |
| 122 | { "?", I_HELP }, |
| 123 | { NULL, -1} |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 124 | }; |
| 125 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 126 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 127 | help(void) |
| 128 | { |
| 129 | printf("Available commands:\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 130 | printf("cd path Change remote directory to 'path'\n"); |
| 131 | printf("lcd path Change local directory to 'path'\n"); |
| 132 | printf("chgrp grp path Change group of file 'path' to 'grp'\n"); |
| 133 | printf("chmod mode path Change permissions of file 'path' to 'mode'\n"); |
| 134 | printf("chown own path Change owner of file 'path' to 'own'\n"); |
| 135 | printf("help Display this help text\n"); |
| 136 | printf("get remote-path [local-path] Download file\n"); |
| 137 | printf("lls [ls-options [path]] Display local directory listing\n"); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 138 | printf("ln oldpath newpath Symlink remote file\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 139 | printf("lmkdir path Create local directory\n"); |
| 140 | printf("lpwd Print local working directory\n"); |
| 141 | printf("ls [path] Display remote directory listing\n"); |
| 142 | printf("lumask umask Set local umask to 'umask'\n"); |
| 143 | printf("mkdir path Create remote directory\n"); |
Damien Miller | 0141319 | 2003-01-14 22:22:11 +1100 | [diff] [blame] | 144 | printf("progress Toggle display of progress meter\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 145 | printf("put local-path [remote-path] Upload file\n"); |
| 146 | printf("pwd Display remote working directory\n"); |
| 147 | printf("exit Quit sftp\n"); |
| 148 | printf("quit Quit sftp\n"); |
| 149 | printf("rename oldpath newpath Rename remote file\n"); |
| 150 | printf("rmdir path Remove remote directory\n"); |
| 151 | printf("rm path Delete remote file\n"); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 152 | printf("symlink oldpath newpath Symlink remote file\n"); |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 153 | printf("version Show SFTP version\n"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 154 | printf("!command Execute 'command' in local shell\n"); |
| 155 | printf("! Escape to local shell\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 156 | printf("? Synonym for help\n"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 157 | } |
| 158 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 159 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 160 | local_do_shell(const char *args) |
| 161 | { |
Ben Lindstrom | 206941f | 2001-04-15 14:27:16 +0000 | [diff] [blame] | 162 | int status; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 163 | char *shell; |
| 164 | pid_t pid; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 165 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 166 | if (!*args) |
| 167 | args = NULL; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 168 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 169 | if ((shell = getenv("SHELL")) == NULL) |
| 170 | shell = _PATH_BSHELL; |
| 171 | |
| 172 | if ((pid = fork()) == -1) |
| 173 | fatal("Couldn't fork: %s", strerror(errno)); |
| 174 | |
| 175 | if (pid == 0) { |
| 176 | /* XXX: child has pipe fds to ssh subproc open - issue? */ |
| 177 | if (args) { |
| 178 | debug3("Executing %s -c \"%s\"", shell, args); |
Damien Miller | efb1edf | 2001-07-14 12:19:36 +1000 | [diff] [blame] | 179 | execl(shell, shell, "-c", args, (char *)NULL); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 180 | } else { |
| 181 | debug3("Executing %s", shell); |
Damien Miller | efb1edf | 2001-07-14 12:19:36 +1000 | [diff] [blame] | 182 | execl(shell, shell, (char *)NULL); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 183 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 184 | fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 185 | strerror(errno)); |
| 186 | _exit(1); |
| 187 | } |
Ben Lindstrom | 47fd811 | 2002-04-02 20:48:19 +0000 | [diff] [blame] | 188 | while (waitpid(pid, &status, 0) == -1) |
| 189 | if (errno != EINTR) |
| 190 | fatal("Couldn't wait for child: %s", strerror(errno)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 191 | if (!WIFEXITED(status)) |
| 192 | error("Shell exited abormally"); |
| 193 | else if (WEXITSTATUS(status)) |
| 194 | error("Shell exited with status %d", WEXITSTATUS(status)); |
| 195 | } |
| 196 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 197 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 198 | local_do_ls(const char *args) |
| 199 | { |
| 200 | if (!args || !*args) |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 201 | local_do_shell(_PATH_LS); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 202 | else { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 203 | int len = strlen(_PATH_LS " ") + strlen(args) + 1; |
| 204 | char *buf = xmalloc(len); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 205 | |
| 206 | /* XXX: quoting - rip quoting code from ftp? */ |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 207 | snprintf(buf, len, _PATH_LS " %s", args); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 208 | local_do_shell(buf); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 209 | xfree(buf); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 213 | /* Strip one path (usually the pwd) from the start of another */ |
| 214 | static char * |
| 215 | path_strip(char *path, char *strip) |
| 216 | { |
| 217 | size_t len; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 218 | |
| 219 | if (strip == NULL) |
| 220 | return (xstrdup(path)); |
| 221 | |
| 222 | len = strlen(strip); |
| 223 | if (strip != NULL && strncmp(path, strip, len) == 0) { |
| 224 | if (strip[len - 1] != '/' && path[len] == '/') |
| 225 | len++; |
| 226 | return (xstrdup(path + len)); |
| 227 | } |
| 228 | |
| 229 | return (xstrdup(path)); |
| 230 | } |
| 231 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 232 | static char * |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 233 | path_append(char *p1, char *p2) |
| 234 | { |
| 235 | char *ret; |
Ben Lindstrom | cf00df6 | 2001-03-17 00:37:31 +0000 | [diff] [blame] | 236 | int len = strlen(p1) + strlen(p2) + 2; |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 237 | |
Ben Lindstrom | cf00df6 | 2001-03-17 00:37:31 +0000 | [diff] [blame] | 238 | ret = xmalloc(len); |
| 239 | strlcpy(ret, p1, len); |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 240 | if (p1[strlen(p1) - 1] != '/') |
Ben Lindstrom | 95148e3 | 2001-08-06 21:30:53 +0000 | [diff] [blame] | 241 | strlcat(ret, "/", len); |
Ben Lindstrom | cf00df6 | 2001-03-17 00:37:31 +0000 | [diff] [blame] | 242 | strlcat(ret, p2, len); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 243 | |
| 244 | return(ret); |
| 245 | } |
| 246 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 247 | static char * |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 248 | make_absolute(char *p, char *pwd) |
| 249 | { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 250 | char *abs; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 251 | |
| 252 | /* Derelativise */ |
| 253 | if (p && p[0] != '/') { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 254 | abs = path_append(pwd, p); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 255 | xfree(p); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 256 | return(abs); |
| 257 | } else |
| 258 | return(p); |
| 259 | } |
| 260 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 261 | static int |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 262 | infer_path(const char *p, char **ifp) |
| 263 | { |
| 264 | char *cp; |
| 265 | |
| 266 | cp = strrchr(p, '/'); |
| 267 | if (cp == NULL) { |
| 268 | *ifp = xstrdup(p); |
| 269 | return(0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 270 | } |
| 271 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 272 | if (!cp[1]) { |
| 273 | error("Invalid path"); |
| 274 | return(-1); |
| 275 | } |
| 276 | |
| 277 | *ifp = xstrdup(cp + 1); |
| 278 | return(0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 279 | } |
| 280 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 281 | static int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 282 | parse_getput_flags(const char **cpp, int *pflag) |
| 283 | { |
| 284 | const char *cp = *cpp; |
| 285 | |
| 286 | /* Check for flags */ |
| 287 | if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 288 | switch (cp[1]) { |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 289 | case 'p': |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 290 | case 'P': |
| 291 | *pflag = 1; |
| 292 | break; |
| 293 | default: |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 294 | error("Invalid flag -%c", cp[1]); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 295 | return(-1); |
| 296 | } |
| 297 | cp += 2; |
| 298 | *cpp = cp + strspn(cp, WHITESPACE); |
| 299 | } |
| 300 | |
| 301 | return(0); |
| 302 | } |
| 303 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 304 | static int |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 305 | parse_ls_flags(const char **cpp, int *lflag) |
| 306 | { |
| 307 | const char *cp = *cpp; |
| 308 | |
| 309 | /* Check for flags */ |
| 310 | if (cp++[0] == '-') { |
| 311 | for(; strchr(WHITESPACE, *cp) == NULL; cp++) { |
| 312 | switch (*cp) { |
| 313 | case 'l': |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 314 | *lflag = LONG_VIEW; |
| 315 | break; |
| 316 | case '1': |
| 317 | *lflag = SHORT_VIEW; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 318 | break; |
| 319 | default: |
| 320 | error("Invalid flag -%c", *cp); |
| 321 | return(-1); |
| 322 | } |
| 323 | } |
| 324 | *cpp = cp + strspn(cp, WHITESPACE); |
| 325 | } |
| 326 | |
| 327 | return(0); |
| 328 | } |
| 329 | |
| 330 | static int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 331 | get_pathname(const char **cpp, char **path) |
| 332 | { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 333 | const char *cp = *cpp, *end; |
| 334 | char quot; |
Darren Tucker | 554d5b5 | 2003-07-19 20:09:21 +1000 | [diff] [blame] | 335 | int i, j; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 336 | |
| 337 | cp += strspn(cp, WHITESPACE); |
| 338 | if (!*cp) { |
| 339 | *cpp = cp; |
| 340 | *path = NULL; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 341 | return (0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 342 | } |
| 343 | |
Darren Tucker | 554d5b5 | 2003-07-19 20:09:21 +1000 | [diff] [blame] | 344 | *path = xmalloc(strlen(cp) + 1); |
| 345 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 346 | /* Check for quoted filenames */ |
| 347 | if (*cp == '\"' || *cp == '\'') { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 348 | quot = *cp++; |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 349 | |
Darren Tucker | 554d5b5 | 2003-07-19 20:09:21 +1000 | [diff] [blame] | 350 | /* Search for terminating quote, unescape some chars */ |
| 351 | for (i = j = 0; i <= strlen(cp); i++) { |
| 352 | if (cp[i] == quot) { /* Found quote */ |
| 353 | (*path)[j] = '\0'; |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 354 | i++; |
Darren Tucker | 554d5b5 | 2003-07-19 20:09:21 +1000 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | if (cp[i] == '\0') { /* End of string */ |
| 358 | error("Unterminated quote"); |
| 359 | goto fail; |
| 360 | } |
| 361 | if (cp[i] == '\\') { /* Escaped characters */ |
| 362 | i++; |
| 363 | if (cp[i] != '\'' && cp[i] != '\"' && |
| 364 | cp[i] != '\\') { |
| 365 | error("Bad escaped character '\%c'", |
| 366 | cp[i]); |
| 367 | goto fail; |
| 368 | } |
| 369 | } |
| 370 | (*path)[j++] = cp[i]; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 371 | } |
Darren Tucker | 554d5b5 | 2003-07-19 20:09:21 +1000 | [diff] [blame] | 372 | |
| 373 | if (j == 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 374 | error("Empty quotes"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 375 | goto fail; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 376 | } |
Darren Tucker | 554d5b5 | 2003-07-19 20:09:21 +1000 | [diff] [blame] | 377 | *cpp = cp + i + strspn(cp + i, WHITESPACE); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 378 | } else { |
| 379 | /* Read to end of filename */ |
| 380 | end = strpbrk(cp, WHITESPACE); |
| 381 | if (end == NULL) |
| 382 | end = strchr(cp, '\0'); |
| 383 | *cpp = end + strspn(end, WHITESPACE); |
Darren Tucker | 554d5b5 | 2003-07-19 20:09:21 +1000 | [diff] [blame] | 384 | |
| 385 | memcpy(*path, cp, end - cp); |
| 386 | (*path)[end - cp] = '\0'; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 387 | } |
Darren Tucker | 554d5b5 | 2003-07-19 20:09:21 +1000 | [diff] [blame] | 388 | return (0); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 389 | |
| 390 | fail: |
Darren Tucker | 554d5b5 | 2003-07-19 20:09:21 +1000 | [diff] [blame] | 391 | xfree(*path); |
| 392 | *path = NULL; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 393 | return (-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 394 | } |
| 395 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 396 | static int |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 397 | is_dir(char *path) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 398 | { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 399 | struct stat sb; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 400 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 401 | /* XXX: report errors? */ |
| 402 | if (stat(path, &sb) == -1) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 403 | return(0); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 404 | |
| 405 | return(sb.st_mode & S_IFDIR); |
| 406 | } |
| 407 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 408 | static int |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 409 | is_reg(char *path) |
| 410 | { |
| 411 | struct stat sb; |
| 412 | |
| 413 | if (stat(path, &sb) == -1) |
| 414 | fatal("stat %s: %s", path, strerror(errno)); |
| 415 | |
| 416 | return(S_ISREG(sb.st_mode)); |
| 417 | } |
| 418 | |
| 419 | static int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 420 | remote_is_dir(struct sftp_conn *conn, char *path) |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 421 | { |
| 422 | Attrib *a; |
| 423 | |
| 424 | /* XXX: report errors? */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 425 | if ((a = do_stat(conn, path, 1)) == NULL) |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 426 | return(0); |
| 427 | if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) |
| 428 | return(0); |
| 429 | return(a->perm & S_IFDIR); |
| 430 | } |
| 431 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 432 | static int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 433 | process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 434 | { |
| 435 | char *abs_src = NULL; |
| 436 | char *abs_dst = NULL; |
| 437 | char *tmp; |
| 438 | glob_t g; |
| 439 | int err = 0; |
| 440 | int i; |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 441 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 442 | abs_src = xstrdup(src); |
| 443 | abs_src = make_absolute(abs_src, pwd); |
| 444 | |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 445 | memset(&g, 0, sizeof(g)); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 446 | debug3("Looking up %s", abs_src); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 447 | if (remote_glob(conn, abs_src, 0, NULL, &g)) { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 448 | error("File \"%s\" not found.", abs_src); |
| 449 | err = -1; |
| 450 | goto out; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 451 | } |
| 452 | |
Damien Miller | 4962ed6 | 2003-05-15 13:48:59 +1000 | [diff] [blame] | 453 | /* If multiple matches, dst must be a directory or unspecified */ |
| 454 | if (g.gl_matchc > 1 && dst && !is_dir(dst)) { |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 455 | error("Multiple files match, but \"%s\" is not a directory", |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 456 | dst); |
| 457 | err = -1; |
| 458 | goto out; |
| 459 | } |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 460 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 461 | for (i = 0; g.gl_pathv[i]; i++) { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 462 | if (infer_path(g.gl_pathv[i], &tmp)) { |
| 463 | err = -1; |
| 464 | goto out; |
| 465 | } |
Damien Miller | 4962ed6 | 2003-05-15 13:48:59 +1000 | [diff] [blame] | 466 | |
| 467 | if (g.gl_matchc == 1 && dst) { |
| 468 | /* If directory specified, append filename */ |
| 469 | if (is_dir(dst)) { |
| 470 | if (infer_path(g.gl_pathv[0], &tmp)) { |
| 471 | err = 1; |
| 472 | goto out; |
| 473 | } |
| 474 | abs_dst = path_append(dst, tmp); |
| 475 | xfree(tmp); |
| 476 | } else |
| 477 | abs_dst = xstrdup(dst); |
| 478 | } else if (dst) { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 479 | abs_dst = path_append(dst, tmp); |
| 480 | xfree(tmp); |
| 481 | } else |
| 482 | abs_dst = tmp; |
| 483 | |
| 484 | printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 485 | if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1) |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 486 | err = -1; |
| 487 | xfree(abs_dst); |
| 488 | abs_dst = NULL; |
| 489 | } |
| 490 | |
| 491 | out: |
| 492 | xfree(abs_src); |
| 493 | if (abs_dst) |
| 494 | xfree(abs_dst); |
| 495 | globfree(&g); |
| 496 | return(err); |
| 497 | } |
| 498 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 499 | static int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 500 | process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 501 | { |
| 502 | char *tmp_dst = NULL; |
| 503 | char *abs_dst = NULL; |
| 504 | char *tmp; |
| 505 | glob_t g; |
| 506 | int err = 0; |
| 507 | int i; |
| 508 | |
| 509 | if (dst) { |
| 510 | tmp_dst = xstrdup(dst); |
| 511 | tmp_dst = make_absolute(tmp_dst, pwd); |
| 512 | } |
| 513 | |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 514 | memset(&g, 0, sizeof(g)); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 515 | debug3("Looking up %s", src); |
| 516 | if (glob(src, 0, NULL, &g)) { |
| 517 | error("File \"%s\" not found.", src); |
| 518 | err = -1; |
| 519 | goto out; |
| 520 | } |
| 521 | |
Damien Miller | 4962ed6 | 2003-05-15 13:48:59 +1000 | [diff] [blame] | 522 | /* If multiple matches, dst may be directory or unspecified */ |
| 523 | if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) { |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 524 | error("Multiple files match, but \"%s\" is not a directory", |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 525 | tmp_dst); |
| 526 | err = -1; |
| 527 | goto out; |
| 528 | } |
| 529 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 530 | for (i = 0; g.gl_pathv[i]; i++) { |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 531 | if (!is_reg(g.gl_pathv[i])) { |
| 532 | error("skipping non-regular file %s", |
| 533 | g.gl_pathv[i]); |
| 534 | continue; |
| 535 | } |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 536 | if (infer_path(g.gl_pathv[i], &tmp)) { |
| 537 | err = -1; |
| 538 | goto out; |
| 539 | } |
Damien Miller | 4962ed6 | 2003-05-15 13:48:59 +1000 | [diff] [blame] | 540 | |
| 541 | if (g.gl_matchc == 1 && tmp_dst) { |
| 542 | /* If directory specified, append filename */ |
| 543 | if (remote_is_dir(conn, tmp_dst)) { |
| 544 | if (infer_path(g.gl_pathv[0], &tmp)) { |
| 545 | err = 1; |
| 546 | goto out; |
| 547 | } |
| 548 | abs_dst = path_append(tmp_dst, tmp); |
| 549 | xfree(tmp); |
| 550 | } else |
| 551 | abs_dst = xstrdup(tmp_dst); |
| 552 | |
| 553 | } else if (tmp_dst) { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 554 | abs_dst = path_append(tmp_dst, tmp); |
| 555 | xfree(tmp); |
| 556 | } else |
| 557 | abs_dst = make_absolute(tmp, pwd); |
| 558 | |
| 559 | printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 560 | if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1) |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 561 | err = -1; |
| 562 | } |
| 563 | |
| 564 | out: |
| 565 | if (abs_dst) |
| 566 | xfree(abs_dst); |
| 567 | if (tmp_dst) |
| 568 | xfree(tmp_dst); |
Damien Miller | 5d421c0 | 2003-05-14 13:42:40 +1000 | [diff] [blame] | 569 | globfree(&g); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 570 | return(err); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 571 | } |
| 572 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 573 | static int |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 574 | sdirent_comp(const void *aa, const void *bb) |
| 575 | { |
| 576 | SFTP_DIRENT *a = *(SFTP_DIRENT **)aa; |
| 577 | SFTP_DIRENT *b = *(SFTP_DIRENT **)bb; |
| 578 | |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 579 | return (strcmp(a->filename, b->filename)); |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* sftp ls.1 replacement for directories */ |
| 583 | static int |
| 584 | do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) |
| 585 | { |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 586 | int n, c = 1, colspace = 0, columns = 1; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 587 | SFTP_DIRENT **d; |
| 588 | |
| 589 | if ((n = do_readdir(conn, path, &d)) != 0) |
| 590 | return (n); |
| 591 | |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 592 | if (!(lflag & SHORT_VIEW)) { |
| 593 | int m = 0, width = 80; |
| 594 | struct winsize ws; |
| 595 | |
| 596 | /* Count entries for sort and find longest filename */ |
| 597 | for (n = 0; d[n] != NULL; n++) |
| 598 | m = MAX(m, strlen(d[n]->filename)); |
| 599 | |
| 600 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) |
| 601 | width = ws.ws_col; |
| 602 | |
| 603 | columns = width / (m + 2); |
Darren Tucker | 7858726 | 2003-08-26 12:12:56 +1000 | [diff] [blame] | 604 | columns = MAX(columns, 1); |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 605 | colspace = width / columns; |
| 606 | } |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 607 | |
| 608 | qsort(d, n, sizeof(*d), sdirent_comp); |
| 609 | |
| 610 | for (n = 0; d[n] != NULL; n++) { |
| 611 | char *tmp, *fname; |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 612 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 613 | tmp = path_append(path, d[n]->filename); |
| 614 | fname = path_strip(tmp, strip_path); |
| 615 | xfree(tmp); |
| 616 | |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 617 | if (lflag & LONG_VIEW) { |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 618 | char *lname; |
| 619 | struct stat sb; |
| 620 | |
| 621 | memset(&sb, 0, sizeof(sb)); |
| 622 | attrib_to_stat(&d[n]->a, &sb); |
| 623 | lname = ls_file(fname, &sb, 1); |
| 624 | printf("%s\n", lname); |
| 625 | xfree(lname); |
| 626 | } else { |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 627 | printf("%-*s", colspace, fname); |
| 628 | if (c >= columns) { |
| 629 | printf("\n"); |
| 630 | c = 1; |
| 631 | } else |
| 632 | c++; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 633 | } |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 634 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 635 | xfree(fname); |
| 636 | } |
| 637 | |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 638 | if (!(lflag & LONG_VIEW) && (c != 1)) |
| 639 | printf("\n"); |
| 640 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 641 | free_sftp_dirents(d); |
| 642 | return (0); |
| 643 | } |
| 644 | |
| 645 | /* sftp ls.1 replacement which handles path globs */ |
| 646 | static int |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 647 | do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 648 | int lflag) |
| 649 | { |
| 650 | glob_t g; |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 651 | int i, c = 1, colspace = 0, columns = 1; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 652 | Attrib *a; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 653 | |
| 654 | memset(&g, 0, sizeof(g)); |
| 655 | |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 656 | if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE, |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 657 | NULL, &g)) { |
| 658 | error("Can't ls: \"%s\" not found", path); |
| 659 | return (-1); |
| 660 | } |
| 661 | |
| 662 | /* |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 663 | * If the glob returns a single match, which is the same as the |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 664 | * input glob, and it is a directory, then just list its contents |
| 665 | */ |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 666 | if (g.gl_pathc == 1 && |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 667 | strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) { |
| 668 | if ((a = do_lstat(conn, path, 1)) == NULL) { |
| 669 | globfree(&g); |
| 670 | return (-1); |
| 671 | } |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 672 | if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 673 | S_ISDIR(a->perm)) { |
| 674 | globfree(&g); |
| 675 | return (do_ls_dir(conn, path, strip_path, lflag)); |
| 676 | } |
| 677 | } |
| 678 | |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 679 | if (!(lflag & SHORT_VIEW)) { |
| 680 | int m = 0, width = 80; |
| 681 | struct winsize ws; |
| 682 | |
| 683 | /* Count entries for sort and find longest filename */ |
| 684 | for (i = 0; g.gl_pathv[i]; i++) |
| 685 | m = MAX(m, strlen(g.gl_pathv[i])); |
| 686 | |
| 687 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) |
| 688 | width = ws.ws_col; |
| 689 | |
| 690 | columns = width / (m + 2); |
Darren Tucker | 7858726 | 2003-08-26 12:12:56 +1000 | [diff] [blame] | 691 | columns = MAX(columns, 1); |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 692 | colspace = width / columns; |
| 693 | } |
| 694 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 695 | for (i = 0; g.gl_pathv[i]; i++) { |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 696 | char *fname; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 697 | |
| 698 | fname = path_strip(g.gl_pathv[i], strip_path); |
| 699 | |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 700 | if (lflag & LONG_VIEW) { |
| 701 | char *lname; |
| 702 | struct stat sb; |
| 703 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 704 | /* |
| 705 | * XXX: this is slow - 1 roundtrip per path |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 706 | * A solution to this is to fork glob() and |
| 707 | * build a sftp specific version which keeps the |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 708 | * attribs (which currently get thrown away) |
| 709 | * that the server returns as well as the filenames. |
| 710 | */ |
| 711 | memset(&sb, 0, sizeof(sb)); |
| 712 | a = do_lstat(conn, g.gl_pathv[i], 1); |
| 713 | if (a != NULL) |
| 714 | attrib_to_stat(a, &sb); |
| 715 | lname = ls_file(fname, &sb, 1); |
| 716 | printf("%s\n", lname); |
| 717 | xfree(lname); |
| 718 | } else { |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 719 | printf("%-*s", colspace, fname); |
| 720 | if (c >= columns) { |
| 721 | printf("\n"); |
| 722 | c = 1; |
| 723 | } else |
| 724 | c++; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 725 | } |
| 726 | xfree(fname); |
| 727 | } |
| 728 | |
Damien Miller | 19c8f2b | 2003-05-15 13:49:21 +1000 | [diff] [blame] | 729 | if (!(lflag & LONG_VIEW) && (c != 1)) |
| 730 | printf("\n"); |
| 731 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 732 | if (g.gl_pathc) |
| 733 | globfree(&g); |
| 734 | |
| 735 | return (0); |
| 736 | } |
| 737 | |
| 738 | static int |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 739 | parse_args(const char **cpp, int *pflag, int *lflag, int *iflag, |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 740 | unsigned long *n_arg, char **path1, char **path2) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 741 | { |
| 742 | const char *cmd, *cp = *cpp; |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 743 | char *cp2; |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 744 | int base = 0; |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 745 | long l; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 746 | int i, cmdnum; |
| 747 | |
| 748 | /* Skip leading whitespace */ |
| 749 | cp = cp + strspn(cp, WHITESPACE); |
| 750 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 751 | /* Ignore blank lines and lines which begin with comment '#' char */ |
| 752 | if (*cp == '\0' || *cp == '#') |
| 753 | return (0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 754 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 755 | /* Check for leading '-' (disable error processing) */ |
| 756 | *iflag = 0; |
| 757 | if (*cp == '-') { |
| 758 | *iflag = 1; |
| 759 | cp++; |
| 760 | } |
| 761 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 762 | /* Figure out which command we have */ |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 763 | for (i = 0; cmds[i].c; i++) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 764 | int cmdlen = strlen(cmds[i].c); |
| 765 | |
| 766 | /* Check for command followed by whitespace */ |
| 767 | if (!strncasecmp(cp, cmds[i].c, cmdlen) && |
| 768 | strchr(WHITESPACE, cp[cmdlen])) { |
| 769 | cp += cmdlen; |
| 770 | cp = cp + strspn(cp, WHITESPACE); |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | cmdnum = cmds[i].n; |
| 775 | cmd = cmds[i].c; |
| 776 | |
| 777 | /* Special case */ |
| 778 | if (*cp == '!') { |
| 779 | cp++; |
| 780 | cmdnum = I_SHELL; |
| 781 | } else if (cmdnum == -1) { |
| 782 | error("Invalid command."); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 783 | return (-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /* Get arguments and parse flags */ |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 787 | *lflag = *pflag = *n_arg = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 788 | *path1 = *path2 = NULL; |
| 789 | switch (cmdnum) { |
| 790 | case I_GET: |
| 791 | case I_PUT: |
| 792 | if (parse_getput_flags(&cp, pflag)) |
| 793 | return(-1); |
| 794 | /* Get first pathname (mandatory) */ |
| 795 | if (get_pathname(&cp, path1)) |
| 796 | return(-1); |
| 797 | if (*path1 == NULL) { |
| 798 | error("You must specify at least one path after a " |
| 799 | "%s command.", cmd); |
| 800 | return(-1); |
| 801 | } |
| 802 | /* Try to get second pathname (optional) */ |
| 803 | if (get_pathname(&cp, path2)) |
| 804 | return(-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 805 | break; |
| 806 | case I_RENAME: |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 807 | case I_SYMLINK: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 808 | if (get_pathname(&cp, path1)) |
| 809 | return(-1); |
| 810 | if (get_pathname(&cp, path2)) |
| 811 | return(-1); |
| 812 | if (!*path1 || !*path2) { |
| 813 | error("You must specify two paths after a %s " |
| 814 | "command.", cmd); |
| 815 | return(-1); |
| 816 | } |
| 817 | break; |
| 818 | case I_RM: |
| 819 | case I_MKDIR: |
| 820 | case I_RMDIR: |
| 821 | case I_CHDIR: |
| 822 | case I_LCHDIR: |
| 823 | case I_LMKDIR: |
| 824 | /* Get pathname (mandatory) */ |
| 825 | if (get_pathname(&cp, path1)) |
| 826 | return(-1); |
| 827 | if (*path1 == NULL) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 828 | error("You must specify a path after a %s command.", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 829 | cmd); |
| 830 | return(-1); |
| 831 | } |
| 832 | break; |
| 833 | case I_LS: |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 834 | if (parse_ls_flags(&cp, lflag)) |
| 835 | return(-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 836 | /* Path is optional */ |
| 837 | if (get_pathname(&cp, path1)) |
| 838 | return(-1); |
| 839 | break; |
| 840 | case I_LLS: |
| 841 | case I_SHELL: |
| 842 | /* Uses the rest of the line */ |
| 843 | break; |
| 844 | case I_LUMASK: |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 845 | base = 8; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 846 | case I_CHMOD: |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 847 | base = 8; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 848 | case I_CHOWN: |
| 849 | case I_CHGRP: |
| 850 | /* Get numeric arg (mandatory) */ |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 851 | l = strtol(cp, &cp2, base); |
| 852 | if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) && |
| 853 | errno == ERANGE) || l < 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 854 | error("You must supply a numeric argument " |
| 855 | "to the %s command.", cmd); |
| 856 | return(-1); |
| 857 | } |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 858 | cp = cp2; |
| 859 | *n_arg = l; |
| 860 | if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp)) |
| 861 | break; |
| 862 | if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 863 | error("You must supply a numeric argument " |
| 864 | "to the %s command.", cmd); |
| 865 | return(-1); |
| 866 | } |
| 867 | cp += strspn(cp, WHITESPACE); |
| 868 | |
| 869 | /* Get pathname (mandatory) */ |
| 870 | if (get_pathname(&cp, path1)) |
| 871 | return(-1); |
| 872 | if (*path1 == NULL) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 873 | error("You must specify a path after a %s command.", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 874 | cmd); |
| 875 | return(-1); |
| 876 | } |
| 877 | break; |
| 878 | case I_QUIT: |
| 879 | case I_PWD: |
| 880 | case I_LPWD: |
| 881 | case I_HELP: |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 882 | case I_VERSION: |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 883 | case I_PROGRESS: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 884 | break; |
| 885 | default: |
| 886 | fatal("Command not implemented"); |
| 887 | } |
| 888 | |
| 889 | *cpp = cp; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 890 | return(cmdnum); |
| 891 | } |
| 892 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 893 | static int |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 894 | parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, |
| 895 | int err_abort) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 896 | { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 897 | char *path1, *path2, *tmp; |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 898 | int pflag, lflag, iflag, cmdnum, i; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 899 | unsigned long n_arg; |
| 900 | Attrib a, *aa; |
Ben Lindstrom | 0a7e354 | 2001-02-15 03:50:49 +0000 | [diff] [blame] | 901 | char path_buf[MAXPATHLEN]; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 902 | int err = 0; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 903 | glob_t g; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 904 | |
| 905 | path1 = path2 = NULL; |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 906 | cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg, |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 907 | &path1, &path2); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 908 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 909 | if (iflag != 0) |
| 910 | err_abort = 0; |
| 911 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 912 | memset(&g, 0, sizeof(g)); |
| 913 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 914 | /* Perform command */ |
| 915 | switch (cmdnum) { |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 916 | case 0: |
| 917 | /* Blank line */ |
| 918 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 919 | case -1: |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 920 | /* Unrecognized command */ |
| 921 | err = -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 922 | break; |
| 923 | case I_GET: |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 924 | err = process_get(conn, path1, path2, *pwd, pflag); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 925 | break; |
| 926 | case I_PUT: |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 927 | err = process_put(conn, path1, path2, *pwd, pflag); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 928 | break; |
| 929 | case I_RENAME: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 930 | path1 = make_absolute(path1, *pwd); |
| 931 | path2 = make_absolute(path2, *pwd); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 932 | err = do_rename(conn, path1, path2); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 933 | break; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 934 | case I_SYMLINK: |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 935 | path2 = make_absolute(path2, *pwd); |
| 936 | err = do_symlink(conn, path1, path2); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 937 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 938 | case I_RM: |
| 939 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 940 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 941 | for (i = 0; g.gl_pathv[i]; i++) { |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 942 | printf("Removing %s\n", g.gl_pathv[i]); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 943 | err = do_rm(conn, g.gl_pathv[i]); |
| 944 | if (err != 0 && err_abort) |
| 945 | break; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 946 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 947 | break; |
| 948 | case I_MKDIR: |
| 949 | path1 = make_absolute(path1, *pwd); |
| 950 | attrib_clear(&a); |
| 951 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 952 | a.perm = 0777; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 953 | err = do_mkdir(conn, path1, &a); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 954 | break; |
| 955 | case I_RMDIR: |
| 956 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 957 | err = do_rmdir(conn, path1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 958 | break; |
| 959 | case I_CHDIR: |
| 960 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 961 | if ((tmp = do_realpath(conn, path1)) == NULL) { |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 962 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 963 | break; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 964 | } |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 965 | if ((aa = do_stat(conn, tmp, 0)) == NULL) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 966 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 967 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 968 | break; |
| 969 | } |
| 970 | if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { |
| 971 | error("Can't change directory: Can't check target"); |
| 972 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 973 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 974 | break; |
| 975 | } |
| 976 | if (!S_ISDIR(aa->perm)) { |
| 977 | error("Can't change directory: \"%s\" is not " |
| 978 | "a directory", tmp); |
| 979 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 980 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 981 | break; |
| 982 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 983 | xfree(*pwd); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 984 | *pwd = tmp; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 985 | break; |
| 986 | case I_LS: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 987 | if (!path1) { |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 988 | do_globbed_ls(conn, *pwd, *pwd, lflag); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 989 | break; |
| 990 | } |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 991 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 992 | /* Strip pwd off beginning of non-absolute paths */ |
| 993 | tmp = NULL; |
| 994 | if (*path1 != '/') |
| 995 | tmp = *pwd; |
| 996 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 997 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 998 | err = do_globbed_ls(conn, path1, tmp, lflag); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 999 | break; |
| 1000 | case I_LCHDIR: |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1001 | if (chdir(path1) == -1) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1002 | error("Couldn't change local directory to " |
| 1003 | "\"%s\": %s", path1, strerror(errno)); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1004 | err = 1; |
| 1005 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1006 | break; |
| 1007 | case I_LMKDIR: |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1008 | if (mkdir(path1, 0777) == -1) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1009 | error("Couldn't create local directory " |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1010 | "\"%s\": %s", path1, strerror(errno)); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1011 | err = 1; |
| 1012 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1013 | break; |
| 1014 | case I_LLS: |
| 1015 | local_do_ls(cmd); |
| 1016 | break; |
| 1017 | case I_SHELL: |
| 1018 | local_do_shell(cmd); |
| 1019 | break; |
| 1020 | case I_LUMASK: |
| 1021 | umask(n_arg); |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 1022 | printf("Local umask: %03lo\n", n_arg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1023 | break; |
| 1024 | case I_CHMOD: |
| 1025 | path1 = make_absolute(path1, *pwd); |
| 1026 | attrib_clear(&a); |
| 1027 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 1028 | a.perm = n_arg; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1029 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1030 | for (i = 0; g.gl_pathv[i]; i++) { |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 1031 | printf("Changing mode on %s\n", g.gl_pathv[i]); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1032 | err = do_setstat(conn, g.gl_pathv[i], &a); |
| 1033 | if (err != 0 && err_abort) |
| 1034 | break; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 1035 | } |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 1036 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1037 | case I_CHOWN: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1038 | case I_CHGRP: |
| 1039 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1040 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1041 | for (i = 0; g.gl_pathv[i]; i++) { |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1042 | if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) { |
| 1043 | if (err != 0 && err_abort) |
| 1044 | break; |
| 1045 | else |
| 1046 | continue; |
| 1047 | } |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 1048 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { |
| 1049 | error("Can't get current ownership of " |
| 1050 | "remote file \"%s\"", g.gl_pathv[i]); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1051 | if (err != 0 && err_abort) |
| 1052 | break; |
| 1053 | else |
| 1054 | continue; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 1055 | } |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 1056 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1057 | if (cmdnum == I_CHOWN) { |
| 1058 | printf("Changing owner on %s\n", g.gl_pathv[i]); |
| 1059 | aa->uid = n_arg; |
| 1060 | } else { |
| 1061 | printf("Changing group on %s\n", g.gl_pathv[i]); |
| 1062 | aa->gid = n_arg; |
| 1063 | } |
| 1064 | err = do_setstat(conn, g.gl_pathv[i], aa); |
| 1065 | if (err != 0 && err_abort) |
| 1066 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1067 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1068 | break; |
| 1069 | case I_PWD: |
| 1070 | printf("Remote working directory: %s\n", *pwd); |
| 1071 | break; |
| 1072 | case I_LPWD: |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1073 | if (!getcwd(path_buf, sizeof(path_buf))) { |
| 1074 | error("Couldn't get local cwd: %s", strerror(errno)); |
| 1075 | err = -1; |
| 1076 | break; |
| 1077 | } |
| 1078 | printf("Local working directory: %s\n", path_buf); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1079 | break; |
| 1080 | case I_QUIT: |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1081 | /* Processed below */ |
| 1082 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1083 | case I_HELP: |
| 1084 | help(); |
| 1085 | break; |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 1086 | case I_VERSION: |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1087 | printf("SFTP protocol version %u\n", sftp_proto_version(conn)); |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 1088 | break; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1089 | case I_PROGRESS: |
| 1090 | showprogress = !showprogress; |
| 1091 | if (showprogress) |
| 1092 | printf("Progress meter enabled\n"); |
| 1093 | else |
| 1094 | printf("Progress meter disabled\n"); |
| 1095 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1096 | default: |
| 1097 | fatal("%d is not implemented", cmdnum); |
| 1098 | } |
| 1099 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 1100 | if (g.gl_pathc) |
| 1101 | globfree(&g); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1102 | if (path1) |
| 1103 | xfree(path1); |
| 1104 | if (path2) |
| 1105 | xfree(path2); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1106 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1107 | /* If an unignored error occurs in batch mode we should abort. */ |
| 1108 | if (err_abort && err != 0) |
| 1109 | return (-1); |
| 1110 | else if (cmdnum == I_QUIT) |
| 1111 | return (1); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1112 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1113 | return (0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1114 | } |
| 1115 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1116 | int |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1117 | interactive_loop(int fd_in, int fd_out, char *file1, char *file2) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1118 | { |
| 1119 | char *pwd; |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1120 | char *dir = NULL; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1121 | char cmd[2048]; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1122 | struct sftp_conn *conn; |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1123 | int err; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1124 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1125 | conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests); |
| 1126 | if (conn == NULL) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1127 | fatal("Couldn't initialise connection to server"); |
| 1128 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1129 | pwd = do_realpath(conn, "."); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1130 | if (pwd == NULL) |
| 1131 | fatal("Need cwd"); |
| 1132 | |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1133 | if (file1 != NULL) { |
| 1134 | dir = xstrdup(file1); |
| 1135 | dir = make_absolute(dir, pwd); |
| 1136 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1137 | if (remote_is_dir(conn, dir) && file2 == NULL) { |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1138 | printf("Changing to: %s\n", dir); |
| 1139 | snprintf(cmd, sizeof cmd, "cd \"%s\"", dir); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1140 | if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) |
| 1141 | return (-1); |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1142 | } else { |
| 1143 | if (file2 == NULL) |
| 1144 | snprintf(cmd, sizeof cmd, "get %s", dir); |
| 1145 | else |
| 1146 | snprintf(cmd, sizeof cmd, "get %s %s", dir, |
| 1147 | file2); |
| 1148 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1149 | err = parse_dispatch_command(conn, cmd, &pwd, 1); |
Ben Lindstrom | cb1f60e | 2002-03-22 02:47:28 +0000 | [diff] [blame] | 1150 | xfree(dir); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1151 | xfree(pwd); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1152 | return (err); |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1153 | } |
Ben Lindstrom | cb1f60e | 2002-03-22 02:47:28 +0000 | [diff] [blame] | 1154 | xfree(dir); |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1155 | } |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1156 | |
Ben Lindstrom | 6aebb34 | 2001-05-09 00:38:19 +0000 | [diff] [blame] | 1157 | #if HAVE_SETVBUF |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1158 | setvbuf(stdout, NULL, _IOLBF, 0); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1159 | setvbuf(infile, NULL, _IOLBF, 0); |
Ben Lindstrom | 6aebb34 | 2001-05-09 00:38:19 +0000 | [diff] [blame] | 1160 | #else |
| 1161 | setlinebuf(stdout); |
| 1162 | setlinebuf(infile); |
| 1163 | #endif |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1164 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1165 | err = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1166 | for (;;) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1167 | char *cp; |
| 1168 | |
| 1169 | printf("sftp> "); |
| 1170 | |
| 1171 | /* XXX: use libedit */ |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1172 | if (fgets(cmd, sizeof(cmd), infile) == NULL) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1173 | printf("\n"); |
| 1174 | break; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1175 | } else if (infile != stdin) /* Bluff typing */ |
| 1176 | printf("%s", cmd); |
| 1177 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1178 | cp = strrchr(cmd, '\n'); |
| 1179 | if (cp) |
| 1180 | *cp = '\0'; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1181 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1182 | err = parse_dispatch_command(conn, cmd, &pwd, infile != stdin); |
| 1183 | if (err != 0) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1184 | break; |
| 1185 | } |
| 1186 | xfree(pwd); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1187 | |
| 1188 | /* err == 1 signifies normal "quit" exit */ |
| 1189 | return (err >= 0 ? 0 : -1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1190 | } |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1191 | |