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