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