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