Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2001 Damien Miller. All rights reserved. |
| 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 | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 25 | /* XXX: globbed ls */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 26 | /* XXX: recursive operations */ |
| 27 | |
| 28 | #include "includes.h" |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame^] | 29 | RCSID("$OpenBSD: sftp-int.c,v 1.27 2001/03/13 22:42:54 djm Exp $"); |
| 30 | |
| 31 | #include <glob.h> |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 32 | |
| 33 | #include "buffer.h" |
| 34 | #include "xmalloc.h" |
| 35 | #include "log.h" |
| 36 | #include "pathnames.h" |
| 37 | |
| 38 | #include "sftp.h" |
| 39 | #include "sftp-common.h" |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame^] | 40 | #include "sftp-glob.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 41 | #include "sftp-client.h" |
| 42 | #include "sftp-int.h" |
| 43 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 44 | /* File to read commands from */ |
| 45 | extern FILE *infile; |
| 46 | |
| 47 | /* Version of server we are speaking to */ |
| 48 | int version; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 49 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 50 | /* Seperators for interactive commands */ |
| 51 | #define WHITESPACE " \t\r\n" |
| 52 | |
| 53 | /* Commands for interactive mode */ |
| 54 | #define I_CHDIR 1 |
| 55 | #define I_CHGRP 2 |
| 56 | #define I_CHMOD 3 |
| 57 | #define I_CHOWN 4 |
| 58 | #define I_GET 5 |
| 59 | #define I_HELP 6 |
| 60 | #define I_LCHDIR 7 |
| 61 | #define I_LLS 8 |
| 62 | #define I_LMKDIR 9 |
| 63 | #define I_LPWD 10 |
| 64 | #define I_LS 11 |
| 65 | #define I_LUMASK 12 |
| 66 | #define I_MKDIR 13 |
| 67 | #define I_PUT 14 |
| 68 | #define I_PWD 15 |
| 69 | #define I_QUIT 16 |
| 70 | #define I_RENAME 17 |
| 71 | #define I_RM 18 |
| 72 | #define I_RMDIR 19 |
| 73 | #define I_SHELL 20 |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 74 | #define I_SYMLINK 21 |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 75 | |
| 76 | struct CMD { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 77 | const char *c; |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 78 | const int n; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | const struct CMD cmds[] = { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 82 | { "cd", I_CHDIR }, |
| 83 | { "chdir", I_CHDIR }, |
| 84 | { "chgrp", I_CHGRP }, |
| 85 | { "chmod", I_CHMOD }, |
| 86 | { "chown", I_CHOWN }, |
| 87 | { "dir", I_LS }, |
| 88 | { "exit", I_QUIT }, |
| 89 | { "get", I_GET }, |
| 90 | { "help", I_HELP }, |
| 91 | { "lcd", I_LCHDIR }, |
| 92 | { "lchdir", I_LCHDIR }, |
| 93 | { "lls", I_LLS }, |
| 94 | { "lmkdir", I_LMKDIR }, |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 95 | { "ln", I_SYMLINK }, |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 96 | { "lpwd", I_LPWD }, |
| 97 | { "ls", I_LS }, |
| 98 | { "lumask", I_LUMASK }, |
| 99 | { "mkdir", I_MKDIR }, |
| 100 | { "put", I_PUT }, |
| 101 | { "pwd", I_PWD }, |
| 102 | { "quit", I_QUIT }, |
| 103 | { "rename", I_RENAME }, |
| 104 | { "rm", I_RM }, |
| 105 | { "rmdir", I_RMDIR }, |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 106 | { "symlink", I_SYMLINK }, |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 107 | { "!", I_SHELL }, |
| 108 | { "?", I_HELP }, |
| 109 | { NULL, -1} |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | void |
| 113 | help(void) |
| 114 | { |
| 115 | printf("Available commands:\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 116 | printf("cd path Change remote directory to 'path'\n"); |
| 117 | printf("lcd path Change local directory to 'path'\n"); |
| 118 | printf("chgrp grp path Change group of file 'path' to 'grp'\n"); |
| 119 | printf("chmod mode path Change permissions of file 'path' to 'mode'\n"); |
| 120 | printf("chown own path Change owner of file 'path' to 'own'\n"); |
| 121 | printf("help Display this help text\n"); |
| 122 | printf("get remote-path [local-path] Download file\n"); |
| 123 | printf("lls [ls-options [path]] Display local directory listing\n"); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 124 | printf("ln oldpath newpath Symlink remote file\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 125 | printf("lmkdir path Create local directory\n"); |
| 126 | printf("lpwd Print local working directory\n"); |
| 127 | printf("ls [path] Display remote directory listing\n"); |
| 128 | printf("lumask umask Set local umask to 'umask'\n"); |
| 129 | printf("mkdir path Create remote directory\n"); |
| 130 | printf("put local-path [remote-path] Upload file\n"); |
| 131 | printf("pwd Display remote working directory\n"); |
| 132 | printf("exit Quit sftp\n"); |
| 133 | printf("quit Quit sftp\n"); |
| 134 | printf("rename oldpath newpath Rename remote file\n"); |
| 135 | printf("rmdir path Remove remote directory\n"); |
| 136 | printf("rm path Delete remote file\n"); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 137 | printf("symlink oldpath newpath Symlink remote file\n"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 138 | printf("!command Execute 'command' in local shell\n"); |
| 139 | printf("! Escape to local shell\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 140 | printf("? Synonym for help\n"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
| 144 | local_do_shell(const char *args) |
| 145 | { |
| 146 | int ret, status; |
| 147 | char *shell; |
| 148 | pid_t pid; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 149 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 150 | if (!*args) |
| 151 | args = NULL; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 152 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 153 | if ((shell = getenv("SHELL")) == NULL) |
| 154 | shell = _PATH_BSHELL; |
| 155 | |
| 156 | if ((pid = fork()) == -1) |
| 157 | fatal("Couldn't fork: %s", strerror(errno)); |
| 158 | |
| 159 | if (pid == 0) { |
| 160 | /* XXX: child has pipe fds to ssh subproc open - issue? */ |
| 161 | if (args) { |
| 162 | debug3("Executing %s -c \"%s\"", shell, args); |
| 163 | ret = execl(shell, shell, "-c", args, NULL); |
| 164 | } else { |
| 165 | debug3("Executing %s", shell); |
| 166 | ret = execl(shell, shell, NULL); |
| 167 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 168 | fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 169 | strerror(errno)); |
| 170 | _exit(1); |
| 171 | } |
| 172 | if (waitpid(pid, &status, 0) == -1) |
| 173 | fatal("Couldn't wait for child: %s", strerror(errno)); |
| 174 | if (!WIFEXITED(status)) |
| 175 | error("Shell exited abormally"); |
| 176 | else if (WEXITSTATUS(status)) |
| 177 | error("Shell exited with status %d", WEXITSTATUS(status)); |
| 178 | } |
| 179 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 180 | void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 181 | local_do_ls(const char *args) |
| 182 | { |
| 183 | if (!args || !*args) |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 184 | local_do_shell(_PATH_LS); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 185 | else { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 186 | int len = strlen(_PATH_LS " ") + strlen(args) + 1; |
| 187 | char *buf = xmalloc(len); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 188 | |
| 189 | /* XXX: quoting - rip quoting code from ftp? */ |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 190 | snprintf(buf, len, _PATH_LS " %s", args); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 191 | local_do_shell(buf); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 192 | xfree(buf); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | char * |
| 197 | make_absolute(char *p, char *pwd) |
| 198 | { |
| 199 | char buf[2048]; |
| 200 | |
| 201 | /* Derelativise */ |
| 202 | if (p && p[0] != '/') { |
| 203 | snprintf(buf, sizeof(buf), "%s/%s", pwd, p); |
| 204 | xfree(p); |
| 205 | p = xstrdup(buf); |
| 206 | } |
| 207 | |
| 208 | return(p); |
| 209 | } |
| 210 | |
| 211 | int |
| 212 | parse_getput_flags(const char **cpp, int *pflag) |
| 213 | { |
| 214 | const char *cp = *cpp; |
| 215 | |
| 216 | /* Check for flags */ |
| 217 | if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 218 | switch (cp[1]) { |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 219 | case 'p': |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 220 | case 'P': |
| 221 | *pflag = 1; |
| 222 | break; |
| 223 | default: |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 224 | error("Invalid flag -%c", cp[1]); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 225 | return(-1); |
| 226 | } |
| 227 | cp += 2; |
| 228 | *cpp = cp + strspn(cp, WHITESPACE); |
| 229 | } |
| 230 | |
| 231 | return(0); |
| 232 | } |
| 233 | |
| 234 | int |
| 235 | get_pathname(const char **cpp, char **path) |
| 236 | { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 237 | const char *cp = *cpp, *end; |
| 238 | char quot; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 239 | int i; |
| 240 | |
| 241 | cp += strspn(cp, WHITESPACE); |
| 242 | if (!*cp) { |
| 243 | *cpp = cp; |
| 244 | *path = NULL; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 245 | return (0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /* Check for quoted filenames */ |
| 249 | if (*cp == '\"' || *cp == '\'') { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 250 | quot = *cp++; |
| 251 | |
| 252 | end = strchr(cp, quot); |
| 253 | if (end == NULL) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 254 | error("Unterminated quote"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 255 | goto fail; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 256 | } |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 257 | if (cp == end) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 258 | error("Empty quotes"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 259 | goto fail; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 260 | } |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 261 | *cpp = end + 1 + strspn(end + 1, WHITESPACE); |
| 262 | } else { |
| 263 | /* Read to end of filename */ |
| 264 | end = strpbrk(cp, WHITESPACE); |
| 265 | if (end == NULL) |
| 266 | end = strchr(cp, '\0'); |
| 267 | *cpp = end + strspn(end, WHITESPACE); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 268 | } |
| 269 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 270 | i = end - cp; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 271 | |
| 272 | *path = xmalloc(i + 1); |
| 273 | memcpy(*path, cp, i); |
| 274 | (*path)[i] = '\0'; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 275 | return(0); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 276 | |
| 277 | fail: |
| 278 | *path = NULL; |
| 279 | return (-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | int |
| 283 | infer_path(const char *p, char **ifp) |
| 284 | { |
| 285 | char *cp; |
| 286 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 287 | cp = strrchr(p, '/'); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 288 | if (cp == NULL) { |
| 289 | *ifp = xstrdup(p); |
| 290 | return(0); |
| 291 | } |
| 292 | |
| 293 | if (!cp[1]) { |
| 294 | error("Invalid path"); |
| 295 | return(-1); |
| 296 | } |
| 297 | |
| 298 | *ifp = xstrdup(cp + 1); |
| 299 | return(0); |
| 300 | } |
| 301 | |
| 302 | int |
| 303 | parse_args(const char **cpp, int *pflag, unsigned long *n_arg, |
| 304 | char **path1, char **path2) |
| 305 | { |
| 306 | const char *cmd, *cp = *cpp; |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 307 | char *cp2; |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 308 | int base = 0; |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 309 | long l; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 310 | int i, cmdnum; |
| 311 | |
| 312 | /* Skip leading whitespace */ |
| 313 | cp = cp + strspn(cp, WHITESPACE); |
| 314 | |
| 315 | /* Ignore blank lines */ |
| 316 | if (!*cp) |
| 317 | return(-1); |
| 318 | |
| 319 | /* Figure out which command we have */ |
| 320 | for(i = 0; cmds[i].c; i++) { |
| 321 | int cmdlen = strlen(cmds[i].c); |
| 322 | |
| 323 | /* Check for command followed by whitespace */ |
| 324 | if (!strncasecmp(cp, cmds[i].c, cmdlen) && |
| 325 | strchr(WHITESPACE, cp[cmdlen])) { |
| 326 | cp += cmdlen; |
| 327 | cp = cp + strspn(cp, WHITESPACE); |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | cmdnum = cmds[i].n; |
| 332 | cmd = cmds[i].c; |
| 333 | |
| 334 | /* Special case */ |
| 335 | if (*cp == '!') { |
| 336 | cp++; |
| 337 | cmdnum = I_SHELL; |
| 338 | } else if (cmdnum == -1) { |
| 339 | error("Invalid command."); |
| 340 | return(-1); |
| 341 | } |
| 342 | |
| 343 | /* Get arguments and parse flags */ |
| 344 | *pflag = *n_arg = 0; |
| 345 | *path1 = *path2 = NULL; |
| 346 | switch (cmdnum) { |
| 347 | case I_GET: |
| 348 | case I_PUT: |
| 349 | if (parse_getput_flags(&cp, pflag)) |
| 350 | return(-1); |
| 351 | /* Get first pathname (mandatory) */ |
| 352 | if (get_pathname(&cp, path1)) |
| 353 | return(-1); |
| 354 | if (*path1 == NULL) { |
| 355 | error("You must specify at least one path after a " |
| 356 | "%s command.", cmd); |
| 357 | return(-1); |
| 358 | } |
| 359 | /* Try to get second pathname (optional) */ |
| 360 | if (get_pathname(&cp, path2)) |
| 361 | return(-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 362 | break; |
| 363 | case I_RENAME: |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 364 | case I_SYMLINK: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 365 | if (get_pathname(&cp, path1)) |
| 366 | return(-1); |
| 367 | if (get_pathname(&cp, path2)) |
| 368 | return(-1); |
| 369 | if (!*path1 || !*path2) { |
| 370 | error("You must specify two paths after a %s " |
| 371 | "command.", cmd); |
| 372 | return(-1); |
| 373 | } |
| 374 | break; |
| 375 | case I_RM: |
| 376 | case I_MKDIR: |
| 377 | case I_RMDIR: |
| 378 | case I_CHDIR: |
| 379 | case I_LCHDIR: |
| 380 | case I_LMKDIR: |
| 381 | /* Get pathname (mandatory) */ |
| 382 | if (get_pathname(&cp, path1)) |
| 383 | return(-1); |
| 384 | if (*path1 == NULL) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 385 | error("You must specify a path after a %s command.", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 386 | cmd); |
| 387 | return(-1); |
| 388 | } |
| 389 | break; |
| 390 | case I_LS: |
| 391 | /* Path is optional */ |
| 392 | if (get_pathname(&cp, path1)) |
| 393 | return(-1); |
| 394 | break; |
| 395 | case I_LLS: |
| 396 | case I_SHELL: |
| 397 | /* Uses the rest of the line */ |
| 398 | break; |
| 399 | case I_LUMASK: |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 400 | base = 8; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 401 | case I_CHMOD: |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 402 | base = 8; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 403 | case I_CHOWN: |
| 404 | case I_CHGRP: |
| 405 | /* Get numeric arg (mandatory) */ |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 406 | l = strtol(cp, &cp2, base); |
| 407 | if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) && |
| 408 | errno == ERANGE) || l < 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 409 | error("You must supply a numeric argument " |
| 410 | "to the %s command.", cmd); |
| 411 | return(-1); |
| 412 | } |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 413 | cp = cp2; |
| 414 | *n_arg = l; |
| 415 | if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp)) |
| 416 | break; |
| 417 | if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 418 | error("You must supply a numeric argument " |
| 419 | "to the %s command.", cmd); |
| 420 | return(-1); |
| 421 | } |
| 422 | cp += strspn(cp, WHITESPACE); |
| 423 | |
| 424 | /* Get pathname (mandatory) */ |
| 425 | if (get_pathname(&cp, path1)) |
| 426 | return(-1); |
| 427 | if (*path1 == NULL) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 428 | error("You must specify a path after a %s command.", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 429 | cmd); |
| 430 | return(-1); |
| 431 | } |
| 432 | break; |
| 433 | case I_QUIT: |
| 434 | case I_PWD: |
| 435 | case I_LPWD: |
| 436 | case I_HELP: |
| 437 | break; |
| 438 | default: |
| 439 | fatal("Command not implemented"); |
| 440 | } |
| 441 | |
| 442 | *cpp = cp; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 443 | return(cmdnum); |
| 444 | } |
| 445 | |
| 446 | int |
| 447 | parse_dispatch_command(int in, int out, const char *cmd, char **pwd) |
| 448 | { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 449 | char *path1, *path2, *tmp; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame^] | 450 | int pflag, cmdnum, i; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 451 | unsigned long n_arg; |
| 452 | Attrib a, *aa; |
Ben Lindstrom | 0a7e354 | 2001-02-15 03:50:49 +0000 | [diff] [blame] | 453 | char path_buf[MAXPATHLEN]; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 454 | int err = 0; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame^] | 455 | glob_t g; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 456 | |
| 457 | path1 = path2 = NULL; |
| 458 | cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2); |
| 459 | |
| 460 | /* Perform command */ |
| 461 | switch (cmdnum) { |
| 462 | case -1: |
| 463 | break; |
| 464 | case I_GET: |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame^] | 465 | memset(&g, 0, sizeof(g)); |
| 466 | if (!remote_glob(in, out, path1, 0, NULL, &g)) { |
| 467 | if (path2) { |
| 468 | /* XXX: target should be directory */ |
| 469 | error("You cannot specify a target when " |
| 470 | "downloading multiple files"); |
| 471 | err = -1; |
| 472 | break; |
| 473 | } |
| 474 | for(i = 0; g.gl_pathv[i]; i++) { |
| 475 | if (!infer_path(g.gl_pathv[i], &path2)) { |
| 476 | printf("Fetching %s\n", g.gl_pathv[i]); |
| 477 | if (do_download(in, out, g.gl_pathv[i], |
| 478 | path2, pflag) == -1) |
| 479 | err = -1; |
| 480 | free(path2); |
| 481 | path2 = NULL; |
| 482 | } else |
| 483 | err = -1; |
| 484 | } |
| 485 | } else { |
| 486 | if (!path2 && infer_path(path1, &path2)) { |
| 487 | err = -1; |
| 488 | break; |
| 489 | } |
| 490 | err = do_download(in, out, path1, path2, pflag); |
| 491 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 492 | break; |
| 493 | case I_PUT: |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame^] | 494 | if (!glob(path1, 0, NULL, &g)) { |
| 495 | if (path2) { |
| 496 | error("You cannot specify a target when " |
| 497 | "uploading multiple files"); |
| 498 | err = -1; |
| 499 | break; |
| 500 | } |
| 501 | for(i = 0; g.gl_pathv[i]; i++) { |
| 502 | if (!infer_path(g.gl_pathv[i], &path2)) { |
| 503 | path2 = make_absolute(path2, *pwd); |
| 504 | printf("Uploading %s\n", g.gl_pathv[i]); |
| 505 | if (do_upload(in, out, g.gl_pathv[i], |
| 506 | path2, pflag) == -1) |
| 507 | err = -1; |
| 508 | free(path2); |
| 509 | path2 = NULL; |
| 510 | } else |
| 511 | err = -1; |
| 512 | } |
| 513 | } else { |
| 514 | if (!path2 && infer_path(path1, &path2)) { |
| 515 | err = -1; |
| 516 | break; |
| 517 | } |
| 518 | err = do_upload(in, out, path1, path2, pflag); |
| 519 | } |
| 520 | break; |
| 521 | case I_RENAME: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 522 | path1 = make_absolute(path1, *pwd); |
| 523 | path2 = make_absolute(path2, *pwd); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 524 | err = do_rename(in, out, path1, path2); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 525 | break; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 526 | case I_SYMLINK: |
| 527 | if (version < 3) { |
| 528 | error("The server (version %d) does not support " |
| 529 | "this operation", version); |
| 530 | err = -1; |
| 531 | } else { |
| 532 | path2 = make_absolute(path2, *pwd); |
| 533 | err = do_symlink(in, out, path1, path2); |
| 534 | } |
| 535 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 536 | case I_RM: |
| 537 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame^] | 538 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
| 539 | for(i = 0; g.gl_pathv[i]; i++) { |
| 540 | printf("Removing %s\n", g.gl_pathv[i]); |
| 541 | if (do_rm(in, out, g.gl_pathv[i]) == -1) |
| 542 | err = -1; |
| 543 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 544 | break; |
| 545 | case I_MKDIR: |
| 546 | path1 = make_absolute(path1, *pwd); |
| 547 | attrib_clear(&a); |
| 548 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 549 | a.perm = 0777; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 550 | err = do_mkdir(in, out, path1, &a); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 551 | break; |
| 552 | case I_RMDIR: |
| 553 | path1 = make_absolute(path1, *pwd); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 554 | err = do_rmdir(in, out, path1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 555 | break; |
| 556 | case I_CHDIR: |
| 557 | path1 = make_absolute(path1, *pwd); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 558 | if ((tmp = do_realpath(in, out, path1)) == NULL) { |
| 559 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 560 | break; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 561 | } |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 562 | if ((aa = do_stat(in, out, tmp)) == NULL) { |
| 563 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 564 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 565 | break; |
| 566 | } |
| 567 | if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { |
| 568 | error("Can't change directory: Can't check target"); |
| 569 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 570 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 571 | break; |
| 572 | } |
| 573 | if (!S_ISDIR(aa->perm)) { |
| 574 | error("Can't change directory: \"%s\" is not " |
| 575 | "a directory", tmp); |
| 576 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 577 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 578 | break; |
| 579 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 580 | xfree(*pwd); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 581 | *pwd = tmp; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 582 | break; |
| 583 | case I_LS: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 584 | if (!path1) { |
| 585 | do_ls(in, out, *pwd); |
| 586 | break; |
| 587 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 588 | path1 = make_absolute(path1, *pwd); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 589 | if ((tmp = do_realpath(in, out, path1)) == NULL) |
| 590 | break; |
| 591 | xfree(path1); |
| 592 | path1 = tmp; |
| 593 | if ((aa = do_stat(in, out, path1)) == NULL) |
| 594 | break; |
| 595 | if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
| 596 | !S_ISDIR(aa->perm)) { |
| 597 | error("Can't ls: \"%s\" is not a directory", path1); |
| 598 | break; |
| 599 | } |
| 600 | do_ls(in, out, path1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 601 | break; |
| 602 | case I_LCHDIR: |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 603 | if (chdir(path1) == -1) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 604 | error("Couldn't change local directory to " |
| 605 | "\"%s\": %s", path1, strerror(errno)); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 606 | err = 1; |
| 607 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 608 | break; |
| 609 | case I_LMKDIR: |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 610 | if (mkdir(path1, 0777) == -1) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 611 | error("Couldn't create local directory " |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 612 | "\"%s\": %s", path1, strerror(errno)); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 613 | err = 1; |
| 614 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 615 | break; |
| 616 | case I_LLS: |
| 617 | local_do_ls(cmd); |
| 618 | break; |
| 619 | case I_SHELL: |
| 620 | local_do_shell(cmd); |
| 621 | break; |
| 622 | case I_LUMASK: |
| 623 | umask(n_arg); |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 624 | printf("Local umask: %03lo\n", n_arg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 625 | break; |
| 626 | case I_CHMOD: |
| 627 | path1 = make_absolute(path1, *pwd); |
| 628 | attrib_clear(&a); |
| 629 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 630 | a.perm = n_arg; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame^] | 631 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
| 632 | for(i = 0; g.gl_pathv[i]; i++) { |
| 633 | printf("Changing mode on %s\n", g.gl_pathv[i]); |
| 634 | do_setstat(in, out, g.gl_pathv[i], &a); |
| 635 | } |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 636 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 637 | case I_CHOWN: |
| 638 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame^] | 639 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
| 640 | for(i = 0; g.gl_pathv[i]; i++) { |
| 641 | if (!(aa = do_stat(in, out, g.gl_pathv[i]))) |
| 642 | continue; |
| 643 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { |
| 644 | error("Can't get current ownership of " |
| 645 | "remote file \"%s\"", g.gl_pathv[i]); |
| 646 | continue; |
| 647 | } |
| 648 | printf("Changing owner on %s\n", g.gl_pathv[i]); |
| 649 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; |
| 650 | aa->uid = n_arg; |
| 651 | do_setstat(in, out, g.gl_pathv[i], aa); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 652 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 653 | break; |
| 654 | case I_CHGRP: |
| 655 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame^] | 656 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
| 657 | for(i = 0; g.gl_pathv[i]; i++) { |
| 658 | if (!(aa = do_stat(in, out, g.gl_pathv[i]))) |
| 659 | continue; |
| 660 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { |
| 661 | error("Can't get current ownership of " |
| 662 | "remote file \"%s\"", g.gl_pathv[i]); |
| 663 | continue; |
| 664 | } |
| 665 | printf("Changing group on %s\n", g.gl_pathv[i]); |
| 666 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; |
| 667 | aa->gid = n_arg; |
| 668 | do_setstat(in, out, g.gl_pathv[i], aa); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 669 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 670 | break; |
| 671 | case I_PWD: |
| 672 | printf("Remote working directory: %s\n", *pwd); |
| 673 | break; |
| 674 | case I_LPWD: |
| 675 | if (!getcwd(path_buf, sizeof(path_buf))) |
Ben Lindstrom | 6df8ef4 | 2001-03-05 07:47:23 +0000 | [diff] [blame] | 676 | error("Couldn't get local cwd: %s", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 677 | strerror(errno)); |
| 678 | else |
| 679 | printf("Local working directory: %s\n", |
| 680 | path_buf); |
| 681 | break; |
| 682 | case I_QUIT: |
| 683 | return(-1); |
| 684 | case I_HELP: |
| 685 | help(); |
| 686 | break; |
| 687 | default: |
| 688 | fatal("%d is not implemented", cmdnum); |
| 689 | } |
| 690 | |
| 691 | if (path1) |
| 692 | xfree(path1); |
| 693 | if (path2) |
| 694 | xfree(path2); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 695 | |
| 696 | /* If an error occurs in batch mode we should abort. */ |
| 697 | if (infile != stdin && err > 0) |
| 698 | return -1; |
| 699 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 700 | return(0); |
| 701 | } |
| 702 | |
| 703 | void |
| 704 | interactive_loop(int fd_in, int fd_out) |
| 705 | { |
| 706 | char *pwd; |
| 707 | char cmd[2048]; |
| 708 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 709 | version = do_init(fd_in, fd_out); |
| 710 | if (version == -1) |
| 711 | fatal("Couldn't initialise connection to server"); |
| 712 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 713 | pwd = do_realpath(fd_in, fd_out, "."); |
| 714 | if (pwd == NULL) |
| 715 | fatal("Need cwd"); |
| 716 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 717 | setvbuf(stdout, NULL, _IOLBF, 0); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 718 | setvbuf(infile, NULL, _IOLBF, 0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 719 | |
| 720 | for(;;) { |
| 721 | char *cp; |
| 722 | |
| 723 | printf("sftp> "); |
| 724 | |
| 725 | /* XXX: use libedit */ |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 726 | if (fgets(cmd, sizeof(cmd), infile) == NULL) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 727 | printf("\n"); |
| 728 | break; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 729 | } else if (infile != stdin) /* Bluff typing */ |
| 730 | printf("%s", cmd); |
| 731 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 732 | cp = strrchr(cmd, '\n'); |
| 733 | if (cp) |
| 734 | *cp = '\0'; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 735 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 736 | if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd)) |
| 737 | break; |
| 738 | } |
| 739 | xfree(pwd); |
| 740 | } |