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