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