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" |
Ben Lindstrom | 59e1249 | 2001-08-15 23:22:56 +0000 | [diff] [blame] | 29 | RCSID("$OpenBSD: sftp-int.c,v 1.40 2001/08/14 09:23:02 markus Exp $"); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 30 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 31 | #include "buffer.h" |
| 32 | #include "xmalloc.h" |
| 33 | #include "log.h" |
| 34 | #include "pathnames.h" |
| 35 | |
| 36 | #include "sftp.h" |
| 37 | #include "sftp-common.h" |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 38 | #include "sftp-glob.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 39 | #include "sftp-client.h" |
| 40 | #include "sftp-int.h" |
| 41 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 42 | /* File to read commands from */ |
| 43 | extern FILE *infile; |
| 44 | |
| 45 | /* Version of server we are speaking to */ |
| 46 | int version; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 47 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 48 | /* Seperators for interactive commands */ |
| 49 | #define WHITESPACE " \t\r\n" |
| 50 | |
| 51 | /* Commands for interactive mode */ |
| 52 | #define I_CHDIR 1 |
| 53 | #define I_CHGRP 2 |
| 54 | #define I_CHMOD 3 |
| 55 | #define I_CHOWN 4 |
| 56 | #define I_GET 5 |
| 57 | #define I_HELP 6 |
| 58 | #define I_LCHDIR 7 |
| 59 | #define I_LLS 8 |
| 60 | #define I_LMKDIR 9 |
| 61 | #define I_LPWD 10 |
| 62 | #define I_LS 11 |
| 63 | #define I_LUMASK 12 |
| 64 | #define I_MKDIR 13 |
| 65 | #define I_PUT 14 |
| 66 | #define I_PWD 15 |
| 67 | #define I_QUIT 16 |
| 68 | #define I_RENAME 17 |
| 69 | #define I_RM 18 |
| 70 | #define I_RMDIR 19 |
| 71 | #define I_SHELL 20 |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 72 | #define I_SYMLINK 21 |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 73 | #define I_VERSION 22 |
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[] = { |
Ben Lindstrom | 59e1249 | 2001-08-15 23:22:56 +0000 | [diff] [blame] | 81 | { "bye", I_QUIT }, |
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 }, |
Ben Lindstrom | 23d9a6d | 2001-04-11 23:05:17 +0000 | [diff] [blame] | 90 | { "mget", I_GET }, |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 91 | { "help", I_HELP }, |
| 92 | { "lcd", I_LCHDIR }, |
| 93 | { "lchdir", I_LCHDIR }, |
| 94 | { "lls", I_LLS }, |
| 95 | { "lmkdir", I_LMKDIR }, |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 96 | { "ln", I_SYMLINK }, |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 97 | { "lpwd", I_LPWD }, |
| 98 | { "ls", I_LS }, |
| 99 | { "lumask", I_LUMASK }, |
| 100 | { "mkdir", I_MKDIR }, |
| 101 | { "put", I_PUT }, |
Ben Lindstrom | 23d9a6d | 2001-04-11 23:05:17 +0000 | [diff] [blame] | 102 | { "mput", I_PUT }, |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 103 | { "pwd", I_PWD }, |
| 104 | { "quit", I_QUIT }, |
| 105 | { "rename", I_RENAME }, |
| 106 | { "rm", I_RM }, |
| 107 | { "rmdir", I_RMDIR }, |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 108 | { "symlink", I_SYMLINK }, |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 109 | { "version", I_VERSION }, |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 110 | { "!", I_SHELL }, |
| 111 | { "?", I_HELP }, |
| 112 | { NULL, -1} |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 113 | }; |
| 114 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 115 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 116 | help(void) |
| 117 | { |
| 118 | printf("Available commands:\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 119 | printf("cd path Change remote directory to 'path'\n"); |
| 120 | printf("lcd path Change local directory to 'path'\n"); |
| 121 | printf("chgrp grp path Change group of file 'path' to 'grp'\n"); |
| 122 | printf("chmod mode path Change permissions of file 'path' to 'mode'\n"); |
| 123 | printf("chown own path Change owner of file 'path' to 'own'\n"); |
| 124 | printf("help Display this help text\n"); |
| 125 | printf("get remote-path [local-path] Download file\n"); |
| 126 | printf("lls [ls-options [path]] Display local directory listing\n"); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 127 | printf("ln oldpath newpath Symlink remote file\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 128 | printf("lmkdir path Create local directory\n"); |
| 129 | printf("lpwd Print local working directory\n"); |
| 130 | printf("ls [path] Display remote directory listing\n"); |
| 131 | printf("lumask umask Set local umask to 'umask'\n"); |
| 132 | printf("mkdir path Create remote directory\n"); |
| 133 | printf("put local-path [remote-path] Upload file\n"); |
| 134 | printf("pwd Display remote working directory\n"); |
| 135 | printf("exit Quit sftp\n"); |
| 136 | printf("quit Quit sftp\n"); |
| 137 | printf("rename oldpath newpath Rename remote file\n"); |
| 138 | printf("rmdir path Remove remote directory\n"); |
| 139 | printf("rm path Delete remote file\n"); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 140 | printf("symlink oldpath newpath Symlink remote file\n"); |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 141 | printf("version Show SFTP version\n"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 142 | printf("!command Execute 'command' in local shell\n"); |
| 143 | printf("! Escape to local shell\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 144 | printf("? Synonym for help\n"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 145 | } |
| 146 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 147 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 148 | local_do_shell(const char *args) |
| 149 | { |
Ben Lindstrom | 206941f | 2001-04-15 14:27:16 +0000 | [diff] [blame] | 150 | int status; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 151 | char *shell; |
| 152 | pid_t pid; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 153 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 154 | if (!*args) |
| 155 | args = NULL; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 156 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 157 | if ((shell = getenv("SHELL")) == NULL) |
| 158 | shell = _PATH_BSHELL; |
| 159 | |
| 160 | if ((pid = fork()) == -1) |
| 161 | fatal("Couldn't fork: %s", strerror(errno)); |
| 162 | |
| 163 | if (pid == 0) { |
| 164 | /* XXX: child has pipe fds to ssh subproc open - issue? */ |
| 165 | if (args) { |
| 166 | debug3("Executing %s -c \"%s\"", shell, args); |
Damien Miller | efb1edf | 2001-07-14 12:19:36 +1000 | [diff] [blame] | 167 | execl(shell, shell, "-c", args, (char *)NULL); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 168 | } else { |
| 169 | debug3("Executing %s", shell); |
Damien Miller | efb1edf | 2001-07-14 12:19:36 +1000 | [diff] [blame] | 170 | execl(shell, shell, (char *)NULL); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 171 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 172 | fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 173 | strerror(errno)); |
| 174 | _exit(1); |
| 175 | } |
| 176 | if (waitpid(pid, &status, 0) == -1) |
| 177 | fatal("Couldn't wait for child: %s", strerror(errno)); |
| 178 | if (!WIFEXITED(status)) |
| 179 | error("Shell exited abormally"); |
| 180 | else if (WEXITSTATUS(status)) |
| 181 | error("Shell exited with status %d", WEXITSTATUS(status)); |
| 182 | } |
| 183 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 184 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 185 | local_do_ls(const char *args) |
| 186 | { |
| 187 | if (!args || !*args) |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 188 | local_do_shell(_PATH_LS); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 189 | else { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 190 | int len = strlen(_PATH_LS " ") + strlen(args) + 1; |
| 191 | char *buf = xmalloc(len); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 192 | |
| 193 | /* XXX: quoting - rip quoting code from ftp? */ |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 194 | snprintf(buf, len, _PATH_LS " %s", args); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 195 | local_do_shell(buf); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 196 | xfree(buf); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 200 | static char * |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 201 | path_append(char *p1, char *p2) |
| 202 | { |
| 203 | char *ret; |
Ben Lindstrom | cf00df6 | 2001-03-17 00:37:31 +0000 | [diff] [blame] | 204 | int len = strlen(p1) + strlen(p2) + 2; |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 205 | |
Ben Lindstrom | cf00df6 | 2001-03-17 00:37:31 +0000 | [diff] [blame] | 206 | ret = xmalloc(len); |
| 207 | strlcpy(ret, p1, len); |
Ben Lindstrom | 95148e3 | 2001-08-06 21:30:53 +0000 | [diff] [blame] | 208 | if (strcmp(p1, "/") != 0) |
| 209 | strlcat(ret, "/", len); |
Ben Lindstrom | cf00df6 | 2001-03-17 00:37:31 +0000 | [diff] [blame] | 210 | strlcat(ret, p2, len); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 211 | |
| 212 | return(ret); |
| 213 | } |
| 214 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 215 | static char * |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 216 | make_absolute(char *p, char *pwd) |
| 217 | { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 218 | char *abs; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 219 | |
| 220 | /* Derelativise */ |
| 221 | if (p && p[0] != '/') { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 222 | abs = path_append(pwd, p); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 223 | xfree(p); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 224 | return(abs); |
| 225 | } else |
| 226 | return(p); |
| 227 | } |
| 228 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 229 | static int |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 230 | infer_path(const char *p, char **ifp) |
| 231 | { |
| 232 | char *cp; |
| 233 | |
| 234 | cp = strrchr(p, '/'); |
| 235 | if (cp == NULL) { |
| 236 | *ifp = xstrdup(p); |
| 237 | return(0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 238 | } |
| 239 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 240 | if (!cp[1]) { |
| 241 | error("Invalid path"); |
| 242 | return(-1); |
| 243 | } |
| 244 | |
| 245 | *ifp = xstrdup(cp + 1); |
| 246 | return(0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 247 | } |
| 248 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 249 | static int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 250 | parse_getput_flags(const char **cpp, int *pflag) |
| 251 | { |
| 252 | const char *cp = *cpp; |
| 253 | |
| 254 | /* Check for flags */ |
| 255 | if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 256 | switch (cp[1]) { |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 257 | case 'p': |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 258 | case 'P': |
| 259 | *pflag = 1; |
| 260 | break; |
| 261 | default: |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 262 | error("Invalid flag -%c", cp[1]); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 263 | return(-1); |
| 264 | } |
| 265 | cp += 2; |
| 266 | *cpp = cp + strspn(cp, WHITESPACE); |
| 267 | } |
| 268 | |
| 269 | return(0); |
| 270 | } |
| 271 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 272 | static int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 273 | get_pathname(const char **cpp, char **path) |
| 274 | { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 275 | const char *cp = *cpp, *end; |
| 276 | char quot; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 277 | int i; |
| 278 | |
| 279 | cp += strspn(cp, WHITESPACE); |
| 280 | if (!*cp) { |
| 281 | *cpp = cp; |
| 282 | *path = NULL; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 283 | return (0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* Check for quoted filenames */ |
| 287 | if (*cp == '\"' || *cp == '\'') { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 288 | quot = *cp++; |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 289 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 290 | end = strchr(cp, quot); |
| 291 | if (end == NULL) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 292 | error("Unterminated quote"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 293 | goto fail; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 294 | } |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 295 | if (cp == end) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 296 | error("Empty quotes"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 297 | goto fail; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 298 | } |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 299 | *cpp = end + 1 + strspn(end + 1, WHITESPACE); |
| 300 | } else { |
| 301 | /* Read to end of filename */ |
| 302 | end = strpbrk(cp, WHITESPACE); |
| 303 | if (end == NULL) |
| 304 | end = strchr(cp, '\0'); |
| 305 | *cpp = end + strspn(end, WHITESPACE); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 306 | } |
| 307 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 308 | i = end - cp; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 309 | |
| 310 | *path = xmalloc(i + 1); |
| 311 | memcpy(*path, cp, i); |
| 312 | (*path)[i] = '\0'; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 313 | return(0); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 314 | |
| 315 | fail: |
| 316 | *path = NULL; |
| 317 | return (-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 318 | } |
| 319 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 320 | static int |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 321 | is_dir(char *path) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 322 | { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 323 | struct stat sb; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 324 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 325 | /* XXX: report errors? */ |
| 326 | if (stat(path, &sb) == -1) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 327 | return(0); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 328 | |
| 329 | return(sb.st_mode & S_IFDIR); |
| 330 | } |
| 331 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 332 | static int |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 333 | remote_is_dir(int in, int out, char *path) |
| 334 | { |
| 335 | Attrib *a; |
| 336 | |
| 337 | /* XXX: report errors? */ |
| 338 | if ((a = do_stat(in, out, path, 1)) == NULL) |
| 339 | return(0); |
| 340 | if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) |
| 341 | return(0); |
| 342 | return(a->perm & S_IFDIR); |
| 343 | } |
| 344 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 345 | static int |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 346 | process_get(int in, int out, char *src, char *dst, char *pwd, int pflag) |
| 347 | { |
| 348 | char *abs_src = NULL; |
| 349 | char *abs_dst = NULL; |
| 350 | char *tmp; |
| 351 | glob_t g; |
| 352 | int err = 0; |
| 353 | int i; |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 354 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 355 | abs_src = xstrdup(src); |
| 356 | abs_src = make_absolute(abs_src, pwd); |
| 357 | |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 358 | memset(&g, 0, sizeof(g)); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 359 | debug3("Looking up %s", abs_src); |
| 360 | if (remote_glob(in, out, abs_src, 0, NULL, &g)) { |
| 361 | error("File \"%s\" not found.", abs_src); |
| 362 | err = -1; |
| 363 | goto out; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 364 | } |
| 365 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 366 | /* Only one match, dst may be file, directory or unspecified */ |
| 367 | if (g.gl_pathv[0] && g.gl_matchc == 1) { |
| 368 | if (dst) { |
| 369 | /* If directory specified, append filename */ |
| 370 | if (is_dir(dst)) { |
| 371 | if (infer_path(g.gl_pathv[0], &tmp)) { |
| 372 | err = 1; |
| 373 | goto out; |
| 374 | } |
| 375 | abs_dst = path_append(dst, tmp); |
| 376 | xfree(tmp); |
| 377 | } else |
| 378 | abs_dst = xstrdup(dst); |
| 379 | } else if (infer_path(g.gl_pathv[0], &abs_dst)) { |
| 380 | err = -1; |
| 381 | goto out; |
| 382 | } |
| 383 | printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst); |
| 384 | err = do_download(in, out, g.gl_pathv[0], abs_dst, pflag); |
| 385 | goto out; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 386 | } |
| 387 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 388 | /* Multiple matches, dst may be directory or unspecified */ |
| 389 | if (dst && !is_dir(dst)) { |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 390 | error("Multiple files match, but \"%s\" is not a directory", |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 391 | dst); |
| 392 | err = -1; |
| 393 | goto out; |
| 394 | } |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 395 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 396 | for(i = 0; g.gl_pathv[i]; i++) { |
| 397 | if (infer_path(g.gl_pathv[i], &tmp)) { |
| 398 | err = -1; |
| 399 | goto out; |
| 400 | } |
| 401 | if (dst) { |
| 402 | abs_dst = path_append(dst, tmp); |
| 403 | xfree(tmp); |
| 404 | } else |
| 405 | abs_dst = tmp; |
| 406 | |
| 407 | printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst); |
| 408 | if (do_download(in, out, g.gl_pathv[i], abs_dst, pflag) == -1) |
| 409 | err = -1; |
| 410 | xfree(abs_dst); |
| 411 | abs_dst = NULL; |
| 412 | } |
| 413 | |
| 414 | out: |
| 415 | xfree(abs_src); |
| 416 | if (abs_dst) |
| 417 | xfree(abs_dst); |
| 418 | globfree(&g); |
| 419 | return(err); |
| 420 | } |
| 421 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 422 | static int |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 423 | process_put(int in, int out, char *src, char *dst, char *pwd, int pflag) |
| 424 | { |
| 425 | char *tmp_dst = NULL; |
| 426 | char *abs_dst = NULL; |
| 427 | char *tmp; |
| 428 | glob_t g; |
| 429 | int err = 0; |
| 430 | int i; |
| 431 | |
| 432 | if (dst) { |
| 433 | tmp_dst = xstrdup(dst); |
| 434 | tmp_dst = make_absolute(tmp_dst, pwd); |
| 435 | } |
| 436 | |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 437 | memset(&g, 0, sizeof(g)); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 438 | debug3("Looking up %s", src); |
| 439 | if (glob(src, 0, NULL, &g)) { |
| 440 | error("File \"%s\" not found.", src); |
| 441 | err = -1; |
| 442 | goto out; |
| 443 | } |
| 444 | |
| 445 | /* Only one match, dst may be file, directory or unspecified */ |
| 446 | if (g.gl_pathv[0] && g.gl_matchc == 1) { |
| 447 | if (tmp_dst) { |
| 448 | /* If directory specified, append filename */ |
| 449 | if (remote_is_dir(in, out, tmp_dst)) { |
| 450 | if (infer_path(g.gl_pathv[0], &tmp)) { |
| 451 | err = 1; |
| 452 | goto out; |
| 453 | } |
| 454 | abs_dst = path_append(tmp_dst, tmp); |
| 455 | xfree(tmp); |
| 456 | } else |
| 457 | abs_dst = xstrdup(tmp_dst); |
Ben Lindstrom | 7527f8b | 2001-03-24 00:39:12 +0000 | [diff] [blame] | 458 | } else { |
| 459 | if (infer_path(g.gl_pathv[0], &abs_dst)) { |
| 460 | err = -1; |
| 461 | goto out; |
| 462 | } |
| 463 | abs_dst = make_absolute(abs_dst, pwd); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 464 | } |
| 465 | printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst); |
| 466 | err = do_upload(in, out, g.gl_pathv[0], abs_dst, pflag); |
| 467 | goto out; |
| 468 | } |
| 469 | |
| 470 | /* Multiple matches, dst may be directory or unspecified */ |
| 471 | if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) { |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 472 | error("Multiple files match, but \"%s\" is not a directory", |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 473 | tmp_dst); |
| 474 | err = -1; |
| 475 | goto out; |
| 476 | } |
| 477 | |
| 478 | for(i = 0; g.gl_pathv[i]; i++) { |
| 479 | if (infer_path(g.gl_pathv[i], &tmp)) { |
| 480 | err = -1; |
| 481 | goto out; |
| 482 | } |
| 483 | if (tmp_dst) { |
| 484 | abs_dst = path_append(tmp_dst, tmp); |
| 485 | xfree(tmp); |
| 486 | } else |
| 487 | abs_dst = make_absolute(tmp, pwd); |
| 488 | |
| 489 | printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst); |
| 490 | if (do_upload(in, out, g.gl_pathv[i], abs_dst, pflag) == -1) |
| 491 | err = -1; |
| 492 | } |
| 493 | |
| 494 | out: |
| 495 | if (abs_dst) |
| 496 | xfree(abs_dst); |
| 497 | if (tmp_dst) |
| 498 | xfree(tmp_dst); |
| 499 | return(err); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 500 | } |
| 501 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 502 | static int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 503 | parse_args(const char **cpp, int *pflag, unsigned long *n_arg, |
| 504 | char **path1, char **path2) |
| 505 | { |
| 506 | const char *cmd, *cp = *cpp; |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 507 | char *cp2; |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 508 | int base = 0; |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 509 | long l; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 510 | int i, cmdnum; |
| 511 | |
| 512 | /* Skip leading whitespace */ |
| 513 | cp = cp + strspn(cp, WHITESPACE); |
| 514 | |
| 515 | /* Ignore blank lines */ |
| 516 | if (!*cp) |
| 517 | return(-1); |
| 518 | |
| 519 | /* Figure out which command we have */ |
| 520 | for(i = 0; cmds[i].c; i++) { |
| 521 | int cmdlen = strlen(cmds[i].c); |
| 522 | |
| 523 | /* Check for command followed by whitespace */ |
| 524 | if (!strncasecmp(cp, cmds[i].c, cmdlen) && |
| 525 | strchr(WHITESPACE, cp[cmdlen])) { |
| 526 | cp += cmdlen; |
| 527 | cp = cp + strspn(cp, WHITESPACE); |
| 528 | break; |
| 529 | } |
| 530 | } |
| 531 | cmdnum = cmds[i].n; |
| 532 | cmd = cmds[i].c; |
| 533 | |
| 534 | /* Special case */ |
| 535 | if (*cp == '!') { |
| 536 | cp++; |
| 537 | cmdnum = I_SHELL; |
| 538 | } else if (cmdnum == -1) { |
| 539 | error("Invalid command."); |
| 540 | return(-1); |
| 541 | } |
| 542 | |
| 543 | /* Get arguments and parse flags */ |
| 544 | *pflag = *n_arg = 0; |
| 545 | *path1 = *path2 = NULL; |
| 546 | switch (cmdnum) { |
| 547 | case I_GET: |
| 548 | case I_PUT: |
| 549 | if (parse_getput_flags(&cp, pflag)) |
| 550 | return(-1); |
| 551 | /* Get first pathname (mandatory) */ |
| 552 | if (get_pathname(&cp, path1)) |
| 553 | return(-1); |
| 554 | if (*path1 == NULL) { |
| 555 | error("You must specify at least one path after a " |
| 556 | "%s command.", cmd); |
| 557 | return(-1); |
| 558 | } |
| 559 | /* Try to get second pathname (optional) */ |
| 560 | if (get_pathname(&cp, path2)) |
| 561 | return(-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 562 | break; |
| 563 | case I_RENAME: |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 564 | case I_SYMLINK: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 565 | if (get_pathname(&cp, path1)) |
| 566 | return(-1); |
| 567 | if (get_pathname(&cp, path2)) |
| 568 | return(-1); |
| 569 | if (!*path1 || !*path2) { |
| 570 | error("You must specify two paths after a %s " |
| 571 | "command.", cmd); |
| 572 | return(-1); |
| 573 | } |
| 574 | break; |
| 575 | case I_RM: |
| 576 | case I_MKDIR: |
| 577 | case I_RMDIR: |
| 578 | case I_CHDIR: |
| 579 | case I_LCHDIR: |
| 580 | case I_LMKDIR: |
| 581 | /* Get pathname (mandatory) */ |
| 582 | if (get_pathname(&cp, path1)) |
| 583 | return(-1); |
| 584 | if (*path1 == NULL) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 585 | error("You must specify a path after a %s command.", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 586 | cmd); |
| 587 | return(-1); |
| 588 | } |
| 589 | break; |
| 590 | case I_LS: |
| 591 | /* Path is optional */ |
| 592 | if (get_pathname(&cp, path1)) |
| 593 | return(-1); |
| 594 | break; |
| 595 | case I_LLS: |
| 596 | case I_SHELL: |
| 597 | /* Uses the rest of the line */ |
| 598 | break; |
| 599 | case I_LUMASK: |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 600 | base = 8; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 601 | case I_CHMOD: |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 602 | base = 8; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 603 | case I_CHOWN: |
| 604 | case I_CHGRP: |
| 605 | /* Get numeric arg (mandatory) */ |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 606 | l = strtol(cp, &cp2, base); |
| 607 | if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) && |
| 608 | errno == ERANGE) || l < 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 609 | error("You must supply a numeric argument " |
| 610 | "to the %s command.", cmd); |
| 611 | return(-1); |
| 612 | } |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 613 | cp = cp2; |
| 614 | *n_arg = l; |
| 615 | if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp)) |
| 616 | break; |
| 617 | if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 618 | error("You must supply a numeric argument " |
| 619 | "to the %s command.", cmd); |
| 620 | return(-1); |
| 621 | } |
| 622 | cp += strspn(cp, WHITESPACE); |
| 623 | |
| 624 | /* Get pathname (mandatory) */ |
| 625 | if (get_pathname(&cp, path1)) |
| 626 | return(-1); |
| 627 | if (*path1 == NULL) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 628 | error("You must specify a path after a %s command.", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 629 | cmd); |
| 630 | return(-1); |
| 631 | } |
| 632 | break; |
| 633 | case I_QUIT: |
| 634 | case I_PWD: |
| 635 | case I_LPWD: |
| 636 | case I_HELP: |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 637 | case I_VERSION: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 638 | break; |
| 639 | default: |
| 640 | fatal("Command not implemented"); |
| 641 | } |
| 642 | |
| 643 | *cpp = cp; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 644 | return(cmdnum); |
| 645 | } |
| 646 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 647 | static int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 648 | parse_dispatch_command(int in, int out, const char *cmd, char **pwd) |
| 649 | { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 650 | char *path1, *path2, *tmp; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 651 | int pflag, cmdnum, i; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 652 | unsigned long n_arg; |
| 653 | Attrib a, *aa; |
Ben Lindstrom | 0a7e354 | 2001-02-15 03:50:49 +0000 | [diff] [blame] | 654 | char path_buf[MAXPATHLEN]; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 655 | int err = 0; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 656 | glob_t g; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 657 | |
| 658 | path1 = path2 = NULL; |
| 659 | cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2); |
| 660 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 661 | memset(&g, 0, sizeof(g)); |
| 662 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 663 | /* Perform command */ |
| 664 | switch (cmdnum) { |
| 665 | case -1: |
| 666 | break; |
| 667 | case I_GET: |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 668 | err = process_get(in, out, path1, path2, *pwd, pflag); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 669 | break; |
| 670 | case I_PUT: |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 671 | err = process_put(in, out, path1, path2, *pwd, pflag); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 672 | break; |
| 673 | case I_RENAME: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 674 | path1 = make_absolute(path1, *pwd); |
| 675 | path2 = make_absolute(path2, *pwd); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 676 | err = do_rename(in, out, path1, path2); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 677 | break; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 678 | case I_SYMLINK: |
| 679 | if (version < 3) { |
| 680 | error("The server (version %d) does not support " |
| 681 | "this operation", version); |
| 682 | err = -1; |
| 683 | } else { |
| 684 | path2 = make_absolute(path2, *pwd); |
| 685 | err = do_symlink(in, out, path1, path2); |
| 686 | } |
| 687 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 688 | case I_RM: |
| 689 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 690 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
| 691 | for(i = 0; g.gl_pathv[i]; i++) { |
| 692 | printf("Removing %s\n", g.gl_pathv[i]); |
| 693 | if (do_rm(in, out, g.gl_pathv[i]) == -1) |
| 694 | err = -1; |
| 695 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 696 | break; |
| 697 | case I_MKDIR: |
| 698 | path1 = make_absolute(path1, *pwd); |
| 699 | attrib_clear(&a); |
| 700 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 701 | a.perm = 0777; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 702 | err = do_mkdir(in, out, path1, &a); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 703 | break; |
| 704 | case I_RMDIR: |
| 705 | path1 = make_absolute(path1, *pwd); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 706 | err = do_rmdir(in, out, path1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 707 | break; |
| 708 | case I_CHDIR: |
| 709 | path1 = make_absolute(path1, *pwd); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 710 | if ((tmp = do_realpath(in, out, path1)) == NULL) { |
| 711 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 712 | break; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 713 | } |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 714 | if ((aa = do_stat(in, out, tmp, 0)) == NULL) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 715 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 716 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 717 | break; |
| 718 | } |
| 719 | if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { |
| 720 | error("Can't change directory: Can't check target"); |
| 721 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 722 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 723 | break; |
| 724 | } |
| 725 | if (!S_ISDIR(aa->perm)) { |
| 726 | error("Can't change directory: \"%s\" is not " |
| 727 | "a directory", tmp); |
| 728 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 729 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 730 | break; |
| 731 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 732 | xfree(*pwd); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 733 | *pwd = tmp; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 734 | break; |
| 735 | case I_LS: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 736 | if (!path1) { |
| 737 | do_ls(in, out, *pwd); |
| 738 | break; |
| 739 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 740 | path1 = make_absolute(path1, *pwd); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 741 | if ((tmp = do_realpath(in, out, path1)) == NULL) |
| 742 | break; |
| 743 | xfree(path1); |
| 744 | path1 = tmp; |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 745 | if ((aa = do_stat(in, out, path1, 0)) == NULL) |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 746 | break; |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 747 | if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 748 | !S_ISDIR(aa->perm)) { |
| 749 | error("Can't ls: \"%s\" is not a directory", path1); |
| 750 | break; |
| 751 | } |
| 752 | do_ls(in, out, path1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 753 | break; |
| 754 | case I_LCHDIR: |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 755 | if (chdir(path1) == -1) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 756 | error("Couldn't change local directory to " |
| 757 | "\"%s\": %s", path1, strerror(errno)); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 758 | err = 1; |
| 759 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 760 | break; |
| 761 | case I_LMKDIR: |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 762 | if (mkdir(path1, 0777) == -1) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 763 | error("Couldn't create local directory " |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 764 | "\"%s\": %s", path1, strerror(errno)); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 765 | err = 1; |
| 766 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 767 | break; |
| 768 | case I_LLS: |
| 769 | local_do_ls(cmd); |
| 770 | break; |
| 771 | case I_SHELL: |
| 772 | local_do_shell(cmd); |
| 773 | break; |
| 774 | case I_LUMASK: |
| 775 | umask(n_arg); |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 776 | printf("Local umask: %03lo\n", n_arg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 777 | break; |
| 778 | case I_CHMOD: |
| 779 | path1 = make_absolute(path1, *pwd); |
| 780 | attrib_clear(&a); |
| 781 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 782 | a.perm = n_arg; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 783 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
| 784 | for(i = 0; g.gl_pathv[i]; i++) { |
| 785 | printf("Changing mode on %s\n", g.gl_pathv[i]); |
| 786 | do_setstat(in, out, g.gl_pathv[i], &a); |
| 787 | } |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 788 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 789 | case I_CHOWN: |
| 790 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 791 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
| 792 | for(i = 0; g.gl_pathv[i]; i++) { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 793 | if (!(aa = do_stat(in, out, g.gl_pathv[i], 0))) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 794 | continue; |
| 795 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { |
| 796 | error("Can't get current ownership of " |
| 797 | "remote file \"%s\"", g.gl_pathv[i]); |
| 798 | continue; |
| 799 | } |
| 800 | printf("Changing owner on %s\n", g.gl_pathv[i]); |
| 801 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; |
| 802 | aa->uid = n_arg; |
| 803 | do_setstat(in, out, g.gl_pathv[i], aa); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 804 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 805 | break; |
| 806 | case I_CHGRP: |
| 807 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 808 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
| 809 | for(i = 0; g.gl_pathv[i]; i++) { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 810 | if (!(aa = do_stat(in, out, g.gl_pathv[i], 0))) |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 811 | continue; |
| 812 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { |
| 813 | error("Can't get current ownership of " |
| 814 | "remote file \"%s\"", g.gl_pathv[i]); |
| 815 | continue; |
| 816 | } |
| 817 | printf("Changing group on %s\n", g.gl_pathv[i]); |
| 818 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; |
| 819 | aa->gid = n_arg; |
| 820 | do_setstat(in, out, g.gl_pathv[i], aa); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 821 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 822 | break; |
| 823 | case I_PWD: |
| 824 | printf("Remote working directory: %s\n", *pwd); |
| 825 | break; |
| 826 | case I_LPWD: |
| 827 | if (!getcwd(path_buf, sizeof(path_buf))) |
Ben Lindstrom | 6df8ef4 | 2001-03-05 07:47:23 +0000 | [diff] [blame] | 828 | error("Couldn't get local cwd: %s", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 829 | strerror(errno)); |
| 830 | else |
| 831 | printf("Local working directory: %s\n", |
| 832 | path_buf); |
| 833 | break; |
| 834 | case I_QUIT: |
| 835 | return(-1); |
| 836 | case I_HELP: |
| 837 | help(); |
| 838 | break; |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 839 | case I_VERSION: |
| 840 | printf("SFTP protocol version %d\n", version); |
| 841 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 842 | default: |
| 843 | fatal("%d is not implemented", cmdnum); |
| 844 | } |
| 845 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 846 | if (g.gl_pathc) |
| 847 | globfree(&g); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 848 | if (path1) |
| 849 | xfree(path1); |
| 850 | if (path2) |
| 851 | xfree(path2); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 852 | |
| 853 | /* If an error occurs in batch mode we should abort. */ |
| 854 | if (infile != stdin && err > 0) |
| 855 | return -1; |
| 856 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 857 | return(0); |
| 858 | } |
| 859 | |
| 860 | void |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 861 | interactive_loop(int fd_in, int fd_out, char *file1, char *file2) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 862 | { |
| 863 | char *pwd; |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 864 | char *dir = NULL; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 865 | char cmd[2048]; |
| 866 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 867 | version = do_init(fd_in, fd_out); |
| 868 | if (version == -1) |
| 869 | fatal("Couldn't initialise connection to server"); |
| 870 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 871 | pwd = do_realpath(fd_in, fd_out, "."); |
| 872 | if (pwd == NULL) |
| 873 | fatal("Need cwd"); |
| 874 | |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 875 | if (file1 != NULL) { |
| 876 | dir = xstrdup(file1); |
| 877 | dir = make_absolute(dir, pwd); |
| 878 | |
| 879 | if (remote_is_dir(fd_in, fd_out, dir) && file2 == NULL) { |
| 880 | printf("Changing to: %s\n", dir); |
| 881 | snprintf(cmd, sizeof cmd, "cd \"%s\"", dir); |
| 882 | parse_dispatch_command(fd_in, fd_out, cmd, &pwd); |
| 883 | } else { |
| 884 | if (file2 == NULL) |
| 885 | snprintf(cmd, sizeof cmd, "get %s", dir); |
| 886 | else |
| 887 | snprintf(cmd, sizeof cmd, "get %s %s", dir, |
| 888 | file2); |
| 889 | |
| 890 | parse_dispatch_command(fd_in, fd_out, cmd, &pwd); |
| 891 | return; |
| 892 | } |
| 893 | } |
Ben Lindstrom | 6aebb34 | 2001-05-09 00:38:19 +0000 | [diff] [blame] | 894 | #if HAVE_SETVBUF |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 895 | setvbuf(stdout, NULL, _IOLBF, 0); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 896 | setvbuf(infile, NULL, _IOLBF, 0); |
Ben Lindstrom | 6aebb34 | 2001-05-09 00:38:19 +0000 | [diff] [blame] | 897 | #else |
| 898 | setlinebuf(stdout); |
| 899 | setlinebuf(infile); |
| 900 | #endif |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 901 | |
| 902 | for(;;) { |
| 903 | char *cp; |
| 904 | |
| 905 | printf("sftp> "); |
| 906 | |
| 907 | /* XXX: use libedit */ |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 908 | if (fgets(cmd, sizeof(cmd), infile) == NULL) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 909 | printf("\n"); |
| 910 | break; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 911 | } else if (infile != stdin) /* Bluff typing */ |
| 912 | printf("%s", cmd); |
| 913 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 914 | cp = strrchr(cmd, '\n'); |
| 915 | if (cp) |
| 916 | *cp = '\0'; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 917 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 918 | if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd)) |
| 919 | break; |
| 920 | } |
| 921 | xfree(pwd); |
| 922 | } |