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