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 | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 25 | /* XXX: recursive operations */ |
| 26 | |
| 27 | #include "includes.h" |
Damien Miller | 5d421c0 | 2003-05-14 13:42:40 +1000 | [diff] [blame] | 28 | RCSID("$OpenBSD: sftp-int.c,v 1.58 2003/04/14 21:31:27 mouring Exp $"); |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 29 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 30 | #include "buffer.h" |
| 31 | #include "xmalloc.h" |
| 32 | #include "log.h" |
| 33 | #include "pathnames.h" |
| 34 | |
| 35 | #include "sftp.h" |
| 36 | #include "sftp-common.h" |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 37 | #include "sftp-glob.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 38 | #include "sftp-client.h" |
| 39 | #include "sftp-int.h" |
| 40 | |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 41 | /* File to read commands from */ |
| 42 | extern FILE *infile; |
| 43 | |
Damien Miller | 8829d36 | 2002-02-08 22:04:05 +1100 | [diff] [blame] | 44 | /* Size of buffer used when copying files */ |
| 45 | extern size_t copy_buffer_len; |
| 46 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 47 | /* Number of concurrent outstanding requests */ |
| 48 | extern int num_requests; |
| 49 | |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 50 | /* This is set to 0 if the progressmeter is not desired. */ |
| 51 | int showprogress = 1; |
| 52 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 53 | /* Seperators for interactive commands */ |
| 54 | #define WHITESPACE " \t\r\n" |
| 55 | |
| 56 | /* Commands for interactive mode */ |
| 57 | #define I_CHDIR 1 |
| 58 | #define I_CHGRP 2 |
| 59 | #define I_CHMOD 3 |
| 60 | #define I_CHOWN 4 |
| 61 | #define I_GET 5 |
| 62 | #define I_HELP 6 |
| 63 | #define I_LCHDIR 7 |
| 64 | #define I_LLS 8 |
| 65 | #define I_LMKDIR 9 |
| 66 | #define I_LPWD 10 |
| 67 | #define I_LS 11 |
| 68 | #define I_LUMASK 12 |
| 69 | #define I_MKDIR 13 |
| 70 | #define I_PUT 14 |
| 71 | #define I_PWD 15 |
| 72 | #define I_QUIT 16 |
| 73 | #define I_RENAME 17 |
| 74 | #define I_RM 18 |
| 75 | #define I_RMDIR 19 |
| 76 | #define I_SHELL 20 |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 77 | #define I_SYMLINK 21 |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 78 | #define I_VERSION 22 |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 79 | #define I_PROGRESS 23 |
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 | |
Damien Miller | dc70857 | 2003-01-14 22:24:05 +1100 | [diff] [blame] | 86 | static 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 }, |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 107 | { "progress", I_PROGRESS }, |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 108 | { "put", I_PUT }, |
Ben Lindstrom | 23d9a6d | 2001-04-11 23:05:17 +0000 | [diff] [blame] | 109 | { "mput", I_PUT }, |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 110 | { "pwd", I_PWD }, |
| 111 | { "quit", I_QUIT }, |
| 112 | { "rename", I_RENAME }, |
| 113 | { "rm", I_RM }, |
| 114 | { "rmdir", I_RMDIR }, |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 115 | { "symlink", I_SYMLINK }, |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 116 | { "version", I_VERSION }, |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 117 | { "!", I_SHELL }, |
| 118 | { "?", I_HELP }, |
| 119 | { NULL, -1} |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 120 | }; |
| 121 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 122 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 123 | help(void) |
| 124 | { |
| 125 | printf("Available commands:\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 126 | printf("cd path Change remote directory to 'path'\n"); |
| 127 | printf("lcd path Change local directory to 'path'\n"); |
| 128 | printf("chgrp grp path Change group of file 'path' to 'grp'\n"); |
| 129 | printf("chmod mode path Change permissions of file 'path' to 'mode'\n"); |
| 130 | printf("chown own path Change owner of file 'path' to 'own'\n"); |
| 131 | printf("help Display this help text\n"); |
| 132 | printf("get remote-path [local-path] Download file\n"); |
| 133 | printf("lls [ls-options [path]] Display local directory listing\n"); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 134 | printf("ln oldpath newpath Symlink remote file\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 135 | printf("lmkdir path Create local directory\n"); |
| 136 | printf("lpwd Print local working directory\n"); |
| 137 | printf("ls [path] Display remote directory listing\n"); |
| 138 | printf("lumask umask Set local umask to 'umask'\n"); |
| 139 | printf("mkdir path Create remote directory\n"); |
Damien Miller | 0141319 | 2003-01-14 22:22:11 +1100 | [diff] [blame] | 140 | printf("progress Toggle display of progress meter\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 141 | printf("put local-path [remote-path] Upload file\n"); |
| 142 | printf("pwd Display remote working directory\n"); |
| 143 | printf("exit Quit sftp\n"); |
| 144 | printf("quit Quit sftp\n"); |
| 145 | printf("rename oldpath newpath Rename remote file\n"); |
| 146 | printf("rmdir path Remove remote directory\n"); |
| 147 | printf("rm path Delete remote file\n"); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 148 | printf("symlink oldpath newpath Symlink remote file\n"); |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 149 | printf("version Show SFTP version\n"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 150 | printf("!command Execute 'command' in local shell\n"); |
| 151 | printf("! Escape to local shell\n"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 152 | printf("? Synonym for help\n"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 153 | } |
| 154 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 155 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 156 | local_do_shell(const char *args) |
| 157 | { |
Ben Lindstrom | 206941f | 2001-04-15 14:27:16 +0000 | [diff] [blame] | 158 | int status; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 159 | char *shell; |
| 160 | pid_t pid; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 161 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 162 | if (!*args) |
| 163 | args = NULL; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 164 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 165 | if ((shell = getenv("SHELL")) == NULL) |
| 166 | shell = _PATH_BSHELL; |
| 167 | |
| 168 | if ((pid = fork()) == -1) |
| 169 | fatal("Couldn't fork: %s", strerror(errno)); |
| 170 | |
| 171 | if (pid == 0) { |
| 172 | /* XXX: child has pipe fds to ssh subproc open - issue? */ |
| 173 | if (args) { |
| 174 | debug3("Executing %s -c \"%s\"", shell, args); |
Damien Miller | efb1edf | 2001-07-14 12:19:36 +1000 | [diff] [blame] | 175 | execl(shell, shell, "-c", args, (char *)NULL); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 176 | } else { |
| 177 | debug3("Executing %s", shell); |
Damien Miller | efb1edf | 2001-07-14 12:19:36 +1000 | [diff] [blame] | 178 | execl(shell, shell, (char *)NULL); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 179 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 180 | fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 181 | strerror(errno)); |
| 182 | _exit(1); |
| 183 | } |
Ben Lindstrom | 47fd811 | 2002-04-02 20:48:19 +0000 | [diff] [blame] | 184 | while (waitpid(pid, &status, 0) == -1) |
| 185 | if (errno != EINTR) |
| 186 | fatal("Couldn't wait for child: %s", strerror(errno)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 187 | if (!WIFEXITED(status)) |
| 188 | error("Shell exited abormally"); |
| 189 | else if (WEXITSTATUS(status)) |
| 190 | error("Shell exited with status %d", WEXITSTATUS(status)); |
| 191 | } |
| 192 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 193 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 194 | local_do_ls(const char *args) |
| 195 | { |
| 196 | if (!args || !*args) |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 197 | local_do_shell(_PATH_LS); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 198 | else { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 199 | int len = strlen(_PATH_LS " ") + strlen(args) + 1; |
| 200 | char *buf = xmalloc(len); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 201 | |
| 202 | /* XXX: quoting - rip quoting code from ftp? */ |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 203 | snprintf(buf, len, _PATH_LS " %s", args); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 204 | local_do_shell(buf); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 205 | xfree(buf); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 209 | /* Strip one path (usually the pwd) from the start of another */ |
| 210 | static char * |
| 211 | path_strip(char *path, char *strip) |
| 212 | { |
| 213 | size_t len; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 214 | |
| 215 | if (strip == NULL) |
| 216 | return (xstrdup(path)); |
| 217 | |
| 218 | len = strlen(strip); |
| 219 | if (strip != NULL && strncmp(path, strip, len) == 0) { |
| 220 | if (strip[len - 1] != '/' && path[len] == '/') |
| 221 | len++; |
| 222 | return (xstrdup(path + len)); |
| 223 | } |
| 224 | |
| 225 | return (xstrdup(path)); |
| 226 | } |
| 227 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 228 | static char * |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 229 | path_append(char *p1, char *p2) |
| 230 | { |
| 231 | char *ret; |
Ben Lindstrom | cf00df6 | 2001-03-17 00:37:31 +0000 | [diff] [blame] | 232 | int len = strlen(p1) + strlen(p2) + 2; |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 233 | |
Ben Lindstrom | cf00df6 | 2001-03-17 00:37:31 +0000 | [diff] [blame] | 234 | ret = xmalloc(len); |
| 235 | strlcpy(ret, p1, len); |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 236 | if (p1[strlen(p1) - 1] != '/') |
Ben Lindstrom | 95148e3 | 2001-08-06 21:30:53 +0000 | [diff] [blame] | 237 | strlcat(ret, "/", len); |
Ben Lindstrom | cf00df6 | 2001-03-17 00:37:31 +0000 | [diff] [blame] | 238 | strlcat(ret, p2, len); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 239 | |
| 240 | return(ret); |
| 241 | } |
| 242 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 243 | static char * |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 244 | make_absolute(char *p, char *pwd) |
| 245 | { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 246 | char *abs; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 247 | |
| 248 | /* Derelativise */ |
| 249 | if (p && p[0] != '/') { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 250 | abs = path_append(pwd, p); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 251 | xfree(p); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 252 | return(abs); |
| 253 | } else |
| 254 | return(p); |
| 255 | } |
| 256 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 257 | static int |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 258 | infer_path(const char *p, char **ifp) |
| 259 | { |
| 260 | char *cp; |
| 261 | |
| 262 | cp = strrchr(p, '/'); |
| 263 | if (cp == NULL) { |
| 264 | *ifp = xstrdup(p); |
| 265 | return(0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 266 | } |
| 267 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 268 | if (!cp[1]) { |
| 269 | error("Invalid path"); |
| 270 | return(-1); |
| 271 | } |
| 272 | |
| 273 | *ifp = xstrdup(cp + 1); |
| 274 | return(0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 275 | } |
| 276 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 277 | static int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 278 | parse_getput_flags(const char **cpp, int *pflag) |
| 279 | { |
| 280 | const char *cp = *cpp; |
| 281 | |
| 282 | /* Check for flags */ |
| 283 | if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 284 | switch (cp[1]) { |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 285 | case 'p': |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 286 | case 'P': |
| 287 | *pflag = 1; |
| 288 | break; |
| 289 | default: |
Ben Lindstrom | 9d4f2c8 | 2001-02-15 03:22:45 +0000 | [diff] [blame] | 290 | error("Invalid flag -%c", cp[1]); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 291 | return(-1); |
| 292 | } |
| 293 | cp += 2; |
| 294 | *cpp = cp + strspn(cp, WHITESPACE); |
| 295 | } |
| 296 | |
| 297 | return(0); |
| 298 | } |
| 299 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 300 | static int |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 301 | parse_ls_flags(const char **cpp, int *lflag) |
| 302 | { |
| 303 | const char *cp = *cpp; |
| 304 | |
| 305 | /* Check for flags */ |
| 306 | if (cp++[0] == '-') { |
| 307 | for(; strchr(WHITESPACE, *cp) == NULL; cp++) { |
| 308 | switch (*cp) { |
| 309 | case 'l': |
| 310 | *lflag = 1; |
| 311 | break; |
| 312 | default: |
| 313 | error("Invalid flag -%c", *cp); |
| 314 | return(-1); |
| 315 | } |
| 316 | } |
| 317 | *cpp = cp + strspn(cp, WHITESPACE); |
| 318 | } |
| 319 | |
| 320 | return(0); |
| 321 | } |
| 322 | |
| 323 | static int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 324 | get_pathname(const char **cpp, char **path) |
| 325 | { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 326 | const char *cp = *cpp, *end; |
| 327 | char quot; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 328 | int i; |
| 329 | |
| 330 | cp += strspn(cp, WHITESPACE); |
| 331 | if (!*cp) { |
| 332 | *cpp = cp; |
| 333 | *path = NULL; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 334 | return (0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /* Check for quoted filenames */ |
| 338 | if (*cp == '\"' || *cp == '\'') { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 339 | quot = *cp++; |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 340 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 341 | end = strchr(cp, quot); |
| 342 | if (end == NULL) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 343 | error("Unterminated quote"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 344 | goto fail; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 345 | } |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 346 | if (cp == end) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 347 | error("Empty quotes"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 348 | goto fail; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 349 | } |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 350 | *cpp = end + 1 + strspn(end + 1, WHITESPACE); |
| 351 | } else { |
| 352 | /* Read to end of filename */ |
| 353 | end = strpbrk(cp, WHITESPACE); |
| 354 | if (end == NULL) |
| 355 | end = strchr(cp, '\0'); |
| 356 | *cpp = end + strspn(end, WHITESPACE); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 357 | } |
| 358 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 359 | i = end - cp; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 360 | |
| 361 | *path = xmalloc(i + 1); |
| 362 | memcpy(*path, cp, i); |
| 363 | (*path)[i] = '\0'; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 364 | return(0); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 365 | |
| 366 | fail: |
| 367 | *path = NULL; |
| 368 | return (-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 369 | } |
| 370 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 371 | static int |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 372 | is_dir(char *path) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 373 | { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 374 | struct stat sb; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 375 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 376 | /* XXX: report errors? */ |
| 377 | if (stat(path, &sb) == -1) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 378 | return(0); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 379 | |
| 380 | return(sb.st_mode & S_IFDIR); |
| 381 | } |
| 382 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 383 | static int |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 384 | is_reg(char *path) |
| 385 | { |
| 386 | struct stat sb; |
| 387 | |
| 388 | if (stat(path, &sb) == -1) |
| 389 | fatal("stat %s: %s", path, strerror(errno)); |
| 390 | |
| 391 | return(S_ISREG(sb.st_mode)); |
| 392 | } |
| 393 | |
| 394 | static int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 395 | remote_is_dir(struct sftp_conn *conn, char *path) |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 396 | { |
| 397 | Attrib *a; |
| 398 | |
| 399 | /* XXX: report errors? */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 400 | if ((a = do_stat(conn, path, 1)) == NULL) |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 401 | return(0); |
| 402 | if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) |
| 403 | return(0); |
| 404 | return(a->perm & S_IFDIR); |
| 405 | } |
| 406 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 407 | static int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 408 | 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] | 409 | { |
| 410 | char *abs_src = NULL; |
| 411 | char *abs_dst = NULL; |
| 412 | char *tmp; |
| 413 | glob_t g; |
| 414 | int err = 0; |
| 415 | int i; |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 416 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 417 | abs_src = xstrdup(src); |
| 418 | abs_src = make_absolute(abs_src, pwd); |
| 419 | |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 420 | memset(&g, 0, sizeof(g)); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 421 | debug3("Looking up %s", abs_src); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 422 | if (remote_glob(conn, abs_src, 0, NULL, &g)) { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 423 | error("File \"%s\" not found.", abs_src); |
| 424 | err = -1; |
| 425 | goto out; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 426 | } |
| 427 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 428 | /* Only one match, dst may be file, directory or unspecified */ |
| 429 | if (g.gl_pathv[0] && g.gl_matchc == 1) { |
| 430 | if (dst) { |
| 431 | /* If directory specified, append filename */ |
| 432 | if (is_dir(dst)) { |
| 433 | if (infer_path(g.gl_pathv[0], &tmp)) { |
| 434 | err = 1; |
| 435 | goto out; |
| 436 | } |
| 437 | abs_dst = path_append(dst, tmp); |
| 438 | xfree(tmp); |
| 439 | } else |
| 440 | abs_dst = xstrdup(dst); |
| 441 | } else if (infer_path(g.gl_pathv[0], &abs_dst)) { |
| 442 | err = -1; |
| 443 | goto out; |
| 444 | } |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 445 | err = do_download(conn, g.gl_pathv[0], abs_dst, pflag); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 446 | goto out; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 447 | } |
| 448 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 449 | /* Multiple matches, dst may be directory or unspecified */ |
| 450 | if (dst && !is_dir(dst)) { |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 451 | error("Multiple files match, but \"%s\" is not a directory", |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 452 | dst); |
| 453 | err = -1; |
| 454 | goto out; |
| 455 | } |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 456 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 457 | for (i = 0; g.gl_pathv[i]; i++) { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 458 | if (infer_path(g.gl_pathv[i], &tmp)) { |
| 459 | err = -1; |
| 460 | goto out; |
| 461 | } |
| 462 | if (dst) { |
| 463 | abs_dst = path_append(dst, tmp); |
| 464 | xfree(tmp); |
| 465 | } else |
| 466 | abs_dst = tmp; |
| 467 | |
| 468 | printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 469 | if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1) |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 470 | err = -1; |
| 471 | xfree(abs_dst); |
| 472 | abs_dst = NULL; |
| 473 | } |
| 474 | |
| 475 | out: |
| 476 | xfree(abs_src); |
| 477 | if (abs_dst) |
| 478 | xfree(abs_dst); |
| 479 | globfree(&g); |
| 480 | return(err); |
| 481 | } |
| 482 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 483 | static int |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 484 | 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] | 485 | { |
| 486 | char *tmp_dst = NULL; |
| 487 | char *abs_dst = NULL; |
| 488 | char *tmp; |
| 489 | glob_t g; |
| 490 | int err = 0; |
| 491 | int i; |
| 492 | |
| 493 | if (dst) { |
| 494 | tmp_dst = xstrdup(dst); |
| 495 | tmp_dst = make_absolute(tmp_dst, pwd); |
| 496 | } |
| 497 | |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 498 | memset(&g, 0, sizeof(g)); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 499 | debug3("Looking up %s", src); |
| 500 | if (glob(src, 0, NULL, &g)) { |
| 501 | error("File \"%s\" not found.", src); |
| 502 | err = -1; |
| 503 | goto out; |
| 504 | } |
| 505 | |
| 506 | /* Only one match, dst may be file, directory or unspecified */ |
| 507 | if (g.gl_pathv[0] && g.gl_matchc == 1) { |
Damien Miller | cb3e3c8 | 2003-01-16 15:39:13 +1100 | [diff] [blame] | 508 | if (!is_reg(g.gl_pathv[0])) { |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 509 | error("Can't upload %s: not a regular file", |
| 510 | g.gl_pathv[0]); |
| 511 | err = 1; |
| 512 | goto out; |
| 513 | } |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 514 | if (tmp_dst) { |
| 515 | /* If directory specified, append filename */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 516 | if (remote_is_dir(conn, tmp_dst)) { |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 517 | if (infer_path(g.gl_pathv[0], &tmp)) { |
| 518 | err = 1; |
| 519 | goto out; |
| 520 | } |
| 521 | abs_dst = path_append(tmp_dst, tmp); |
| 522 | xfree(tmp); |
| 523 | } else |
| 524 | abs_dst = xstrdup(tmp_dst); |
Ben Lindstrom | 7527f8b | 2001-03-24 00:39:12 +0000 | [diff] [blame] | 525 | } else { |
| 526 | if (infer_path(g.gl_pathv[0], &abs_dst)) { |
| 527 | err = -1; |
| 528 | goto out; |
| 529 | } |
| 530 | abs_dst = make_absolute(abs_dst, pwd); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 531 | } |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 532 | err = do_upload(conn, g.gl_pathv[0], abs_dst, pflag); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 533 | goto out; |
| 534 | } |
| 535 | |
| 536 | /* Multiple matches, dst may be directory or unspecified */ |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 537 | if (tmp_dst && !remote_is_dir(conn, tmp_dst)) { |
Ben Lindstrom | 5df2ffa | 2001-03-17 00:36:17 +0000 | [diff] [blame] | 538 | error("Multiple files match, but \"%s\" is not a directory", |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 539 | tmp_dst); |
| 540 | err = -1; |
| 541 | goto out; |
| 542 | } |
| 543 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 544 | for (i = 0; g.gl_pathv[i]; i++) { |
Damien Miller | 5fa01fd | 2003-01-14 22:24:47 +1100 | [diff] [blame] | 545 | if (!is_reg(g.gl_pathv[i])) { |
| 546 | error("skipping non-regular file %s", |
| 547 | g.gl_pathv[i]); |
| 548 | continue; |
| 549 | } |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 550 | if (infer_path(g.gl_pathv[i], &tmp)) { |
| 551 | err = -1; |
| 552 | goto out; |
| 553 | } |
| 554 | if (tmp_dst) { |
| 555 | abs_dst = path_append(tmp_dst, tmp); |
| 556 | xfree(tmp); |
| 557 | } else |
| 558 | abs_dst = make_absolute(tmp, pwd); |
| 559 | |
| 560 | printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 561 | if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1) |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 562 | err = -1; |
| 563 | } |
| 564 | |
| 565 | out: |
| 566 | if (abs_dst) |
| 567 | xfree(abs_dst); |
| 568 | if (tmp_dst) |
| 569 | xfree(tmp_dst); |
Damien Miller | 5d421c0 | 2003-05-14 13:42:40 +1000 | [diff] [blame] | 570 | globfree(&g); |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 571 | return(err); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 572 | } |
| 573 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 574 | static int |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 575 | sdirent_comp(const void *aa, const void *bb) |
| 576 | { |
| 577 | SFTP_DIRENT *a = *(SFTP_DIRENT **)aa; |
| 578 | SFTP_DIRENT *b = *(SFTP_DIRENT **)bb; |
| 579 | |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 580 | return (strcmp(a->filename, b->filename)); |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* sftp ls.1 replacement for directories */ |
| 584 | static int |
| 585 | do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) |
| 586 | { |
| 587 | int n; |
| 588 | SFTP_DIRENT **d; |
| 589 | |
| 590 | if ((n = do_readdir(conn, path, &d)) != 0) |
| 591 | return (n); |
| 592 | |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 593 | /* Count entries for sort */ |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 594 | for (n = 0; d[n] != NULL; n++) |
| 595 | ; |
| 596 | |
| 597 | qsort(d, n, sizeof(*d), sdirent_comp); |
| 598 | |
| 599 | for (n = 0; d[n] != NULL; n++) { |
| 600 | char *tmp, *fname; |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 601 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 602 | tmp = path_append(path, d[n]->filename); |
| 603 | fname = path_strip(tmp, strip_path); |
| 604 | xfree(tmp); |
| 605 | |
| 606 | if (lflag) { |
| 607 | char *lname; |
| 608 | struct stat sb; |
| 609 | |
| 610 | memset(&sb, 0, sizeof(sb)); |
| 611 | attrib_to_stat(&d[n]->a, &sb); |
| 612 | lname = ls_file(fname, &sb, 1); |
| 613 | printf("%s\n", lname); |
| 614 | xfree(lname); |
| 615 | } else { |
| 616 | /* XXX - multicolumn display would be nice here */ |
| 617 | printf("%s\n", fname); |
| 618 | } |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 619 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 620 | xfree(fname); |
| 621 | } |
| 622 | |
| 623 | free_sftp_dirents(d); |
| 624 | return (0); |
| 625 | } |
| 626 | |
| 627 | /* sftp ls.1 replacement which handles path globs */ |
| 628 | static int |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 629 | do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 630 | int lflag) |
| 631 | { |
| 632 | glob_t g; |
| 633 | int i; |
| 634 | Attrib *a; |
| 635 | struct stat sb; |
| 636 | |
| 637 | memset(&g, 0, sizeof(g)); |
| 638 | |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 639 | if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE, |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 640 | NULL, &g)) { |
| 641 | error("Can't ls: \"%s\" not found", path); |
| 642 | return (-1); |
| 643 | } |
| 644 | |
| 645 | /* |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 646 | * If the glob returns a single match, which is the same as the |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 647 | * input glob, and it is a directory, then just list its contents |
| 648 | */ |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 649 | if (g.gl_pathc == 1 && |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 650 | strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) { |
| 651 | if ((a = do_lstat(conn, path, 1)) == NULL) { |
| 652 | globfree(&g); |
| 653 | return (-1); |
| 654 | } |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 655 | if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 656 | S_ISDIR(a->perm)) { |
| 657 | globfree(&g); |
| 658 | return (do_ls_dir(conn, path, strip_path, lflag)); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | for (i = 0; g.gl_pathv[i]; i++) { |
| 663 | char *fname, *lname; |
| 664 | |
| 665 | fname = path_strip(g.gl_pathv[i], strip_path); |
| 666 | |
| 667 | if (lflag) { |
| 668 | /* |
| 669 | * XXX: this is slow - 1 roundtrip per path |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 670 | * A solution to this is to fork glob() and |
| 671 | * build a sftp specific version which keeps the |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 672 | * attribs (which currently get thrown away) |
| 673 | * that the server returns as well as the filenames. |
| 674 | */ |
| 675 | memset(&sb, 0, sizeof(sb)); |
| 676 | a = do_lstat(conn, g.gl_pathv[i], 1); |
| 677 | if (a != NULL) |
| 678 | attrib_to_stat(a, &sb); |
| 679 | lname = ls_file(fname, &sb, 1); |
| 680 | printf("%s\n", lname); |
| 681 | xfree(lname); |
| 682 | } else { |
| 683 | /* XXX - multicolumn display would be nice here */ |
| 684 | printf("%s\n", fname); |
| 685 | } |
| 686 | xfree(fname); |
| 687 | } |
| 688 | |
| 689 | if (g.gl_pathc) |
| 690 | globfree(&g); |
| 691 | |
| 692 | return (0); |
| 693 | } |
| 694 | |
| 695 | static int |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 696 | parse_args(const char **cpp, int *pflag, int *lflag, int *iflag, |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 697 | unsigned long *n_arg, char **path1, char **path2) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 698 | { |
| 699 | const char *cmd, *cp = *cpp; |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 700 | char *cp2; |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 701 | int base = 0; |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 702 | long l; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 703 | int i, cmdnum; |
| 704 | |
| 705 | /* Skip leading whitespace */ |
| 706 | cp = cp + strspn(cp, WHITESPACE); |
| 707 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 708 | /* Ignore blank lines and lines which begin with comment '#' char */ |
| 709 | if (*cp == '\0' || *cp == '#') |
| 710 | return (0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 711 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 712 | /* Check for leading '-' (disable error processing) */ |
| 713 | *iflag = 0; |
| 714 | if (*cp == '-') { |
| 715 | *iflag = 1; |
| 716 | cp++; |
| 717 | } |
| 718 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 719 | /* Figure out which command we have */ |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 720 | for (i = 0; cmds[i].c; i++) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 721 | int cmdlen = strlen(cmds[i].c); |
| 722 | |
| 723 | /* Check for command followed by whitespace */ |
| 724 | if (!strncasecmp(cp, cmds[i].c, cmdlen) && |
| 725 | strchr(WHITESPACE, cp[cmdlen])) { |
| 726 | cp += cmdlen; |
| 727 | cp = cp + strspn(cp, WHITESPACE); |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | cmdnum = cmds[i].n; |
| 732 | cmd = cmds[i].c; |
| 733 | |
| 734 | /* Special case */ |
| 735 | if (*cp == '!') { |
| 736 | cp++; |
| 737 | cmdnum = I_SHELL; |
| 738 | } else if (cmdnum == -1) { |
| 739 | error("Invalid command."); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 740 | return (-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /* Get arguments and parse flags */ |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 744 | *lflag = *pflag = *n_arg = 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 745 | *path1 = *path2 = NULL; |
| 746 | switch (cmdnum) { |
| 747 | case I_GET: |
| 748 | case I_PUT: |
| 749 | if (parse_getput_flags(&cp, pflag)) |
| 750 | return(-1); |
| 751 | /* Get first pathname (mandatory) */ |
| 752 | if (get_pathname(&cp, path1)) |
| 753 | return(-1); |
| 754 | if (*path1 == NULL) { |
| 755 | error("You must specify at least one path after a " |
| 756 | "%s command.", cmd); |
| 757 | return(-1); |
| 758 | } |
| 759 | /* Try to get second pathname (optional) */ |
| 760 | if (get_pathname(&cp, path2)) |
| 761 | return(-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 762 | break; |
| 763 | case I_RENAME: |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 764 | case I_SYMLINK: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 765 | if (get_pathname(&cp, path1)) |
| 766 | return(-1); |
| 767 | if (get_pathname(&cp, path2)) |
| 768 | return(-1); |
| 769 | if (!*path1 || !*path2) { |
| 770 | error("You must specify two paths after a %s " |
| 771 | "command.", cmd); |
| 772 | return(-1); |
| 773 | } |
| 774 | break; |
| 775 | case I_RM: |
| 776 | case I_MKDIR: |
| 777 | case I_RMDIR: |
| 778 | case I_CHDIR: |
| 779 | case I_LCHDIR: |
| 780 | case I_LMKDIR: |
| 781 | /* Get pathname (mandatory) */ |
| 782 | if (get_pathname(&cp, path1)) |
| 783 | return(-1); |
| 784 | if (*path1 == NULL) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 785 | error("You must specify a path after a %s command.", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 786 | cmd); |
| 787 | return(-1); |
| 788 | } |
| 789 | break; |
| 790 | case I_LS: |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 791 | if (parse_ls_flags(&cp, lflag)) |
| 792 | return(-1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 793 | /* Path is optional */ |
| 794 | if (get_pathname(&cp, path1)) |
| 795 | return(-1); |
| 796 | break; |
| 797 | case I_LLS: |
| 798 | case I_SHELL: |
| 799 | /* Uses the rest of the line */ |
| 800 | break; |
| 801 | case I_LUMASK: |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 802 | base = 8; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 803 | case I_CHMOD: |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 804 | base = 8; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 805 | case I_CHOWN: |
| 806 | case I_CHGRP: |
| 807 | /* Get numeric arg (mandatory) */ |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 808 | l = strtol(cp, &cp2, base); |
| 809 | if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) && |
| 810 | errno == ERANGE) || l < 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 811 | error("You must supply a numeric argument " |
| 812 | "to the %s command.", cmd); |
| 813 | return(-1); |
| 814 | } |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 815 | cp = cp2; |
| 816 | *n_arg = l; |
| 817 | if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp)) |
| 818 | break; |
| 819 | if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 820 | error("You must supply a numeric argument " |
| 821 | "to the %s command.", cmd); |
| 822 | return(-1); |
| 823 | } |
| 824 | cp += strspn(cp, WHITESPACE); |
| 825 | |
| 826 | /* Get pathname (mandatory) */ |
| 827 | if (get_pathname(&cp, path1)) |
| 828 | return(-1); |
| 829 | if (*path1 == NULL) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 830 | error("You must specify a path after a %s command.", |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 831 | cmd); |
| 832 | return(-1); |
| 833 | } |
| 834 | break; |
| 835 | case I_QUIT: |
| 836 | case I_PWD: |
| 837 | case I_LPWD: |
| 838 | case I_HELP: |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 839 | case I_VERSION: |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 840 | case I_PROGRESS: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 841 | break; |
| 842 | default: |
| 843 | fatal("Command not implemented"); |
| 844 | } |
| 845 | |
| 846 | *cpp = cp; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 847 | return(cmdnum); |
| 848 | } |
| 849 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 850 | static int |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 851 | parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, |
| 852 | int err_abort) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 853 | { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 854 | char *path1, *path2, *tmp; |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 855 | int pflag, lflag, iflag, cmdnum, i; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 856 | unsigned long n_arg; |
| 857 | Attrib a, *aa; |
Ben Lindstrom | 0a7e354 | 2001-02-15 03:50:49 +0000 | [diff] [blame] | 858 | char path_buf[MAXPATHLEN]; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 859 | int err = 0; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 860 | glob_t g; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 861 | |
| 862 | path1 = path2 = NULL; |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 863 | cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg, |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 864 | &path1, &path2); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 865 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 866 | if (iflag != 0) |
| 867 | err_abort = 0; |
| 868 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 869 | memset(&g, 0, sizeof(g)); |
| 870 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 871 | /* Perform command */ |
| 872 | switch (cmdnum) { |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 873 | case 0: |
| 874 | /* Blank line */ |
| 875 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 876 | case -1: |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 877 | /* Unrecognized command */ |
| 878 | err = -1; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 879 | break; |
| 880 | case I_GET: |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 881 | err = process_get(conn, path1, path2, *pwd, pflag); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 882 | break; |
| 883 | case I_PUT: |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 884 | err = process_put(conn, path1, path2, *pwd, pflag); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 885 | break; |
| 886 | case I_RENAME: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 887 | path1 = make_absolute(path1, *pwd); |
| 888 | path2 = make_absolute(path2, *pwd); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 889 | err = do_rename(conn, path1, path2); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 890 | break; |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 891 | case I_SYMLINK: |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 892 | path2 = make_absolute(path2, *pwd); |
| 893 | err = do_symlink(conn, path1, path2); |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 894 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 895 | case I_RM: |
| 896 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 897 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 898 | for (i = 0; g.gl_pathv[i]; i++) { |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 899 | printf("Removing %s\n", g.gl_pathv[i]); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 900 | err = do_rm(conn, g.gl_pathv[i]); |
| 901 | if (err != 0 && err_abort) |
| 902 | break; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 903 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 904 | break; |
| 905 | case I_MKDIR: |
| 906 | path1 = make_absolute(path1, *pwd); |
| 907 | attrib_clear(&a); |
| 908 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 909 | a.perm = 0777; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 910 | err = do_mkdir(conn, path1, &a); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 911 | break; |
| 912 | case I_RMDIR: |
| 913 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 914 | err = do_rmdir(conn, path1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 915 | break; |
| 916 | case I_CHDIR: |
| 917 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 918 | if ((tmp = do_realpath(conn, path1)) == NULL) { |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 919 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 920 | break; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 921 | } |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 922 | if ((aa = do_stat(conn, tmp, 0)) == NULL) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 923 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 924 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 925 | break; |
| 926 | } |
| 927 | if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { |
| 928 | error("Can't change directory: Can't check target"); |
| 929 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 930 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 931 | break; |
| 932 | } |
| 933 | if (!S_ISDIR(aa->perm)) { |
| 934 | error("Can't change directory: \"%s\" is not " |
| 935 | "a directory", tmp); |
| 936 | xfree(tmp); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 937 | err = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 938 | break; |
| 939 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 940 | xfree(*pwd); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 941 | *pwd = tmp; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 942 | break; |
| 943 | case I_LS: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 944 | if (!path1) { |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 945 | do_globbed_ls(conn, *pwd, *pwd, lflag); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 946 | break; |
| 947 | } |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 948 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 949 | /* Strip pwd off beginning of non-absolute paths */ |
| 950 | tmp = NULL; |
| 951 | if (*path1 != '/') |
| 952 | tmp = *pwd; |
| 953 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 954 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 955 | err = do_globbed_ls(conn, path1, tmp, lflag); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 956 | break; |
| 957 | case I_LCHDIR: |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 958 | if (chdir(path1) == -1) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 959 | error("Couldn't change local directory to " |
| 960 | "\"%s\": %s", path1, strerror(errno)); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 961 | err = 1; |
| 962 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 963 | break; |
| 964 | case I_LMKDIR: |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 965 | if (mkdir(path1, 0777) == -1) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 966 | error("Couldn't create local directory " |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 967 | "\"%s\": %s", path1, strerror(errno)); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 968 | err = 1; |
| 969 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 970 | break; |
| 971 | case I_LLS: |
| 972 | local_do_ls(cmd); |
| 973 | break; |
| 974 | case I_SHELL: |
| 975 | local_do_shell(cmd); |
| 976 | break; |
| 977 | case I_LUMASK: |
| 978 | umask(n_arg); |
Ben Lindstrom | 6690494 | 2001-02-15 03:19:56 +0000 | [diff] [blame] | 979 | printf("Local umask: %03lo\n", n_arg); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 980 | break; |
| 981 | case I_CHMOD: |
| 982 | path1 = make_absolute(path1, *pwd); |
| 983 | attrib_clear(&a); |
| 984 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 985 | a.perm = n_arg; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 986 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 987 | for (i = 0; g.gl_pathv[i]; i++) { |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 988 | printf("Changing mode on %s\n", g.gl_pathv[i]); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 989 | err = do_setstat(conn, g.gl_pathv[i], &a); |
| 990 | if (err != 0 && err_abort) |
| 991 | break; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 992 | } |
Kevin Steves | 62c45db | 2001-02-05 13:42:43 +0000 | [diff] [blame] | 993 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 994 | case I_CHOWN: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 995 | case I_CHGRP: |
| 996 | path1 = make_absolute(path1, *pwd); |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 997 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 998 | for (i = 0; g.gl_pathv[i]; i++) { |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 999 | if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) { |
| 1000 | if (err != 0 && err_abort) |
| 1001 | break; |
| 1002 | else |
| 1003 | continue; |
| 1004 | } |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 1005 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { |
| 1006 | error("Can't get current ownership of " |
| 1007 | "remote file \"%s\"", g.gl_pathv[i]); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1008 | if (err != 0 && err_abort) |
| 1009 | break; |
| 1010 | else |
| 1011 | continue; |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 1012 | } |
Damien Miller | 4870afd | 2001-03-14 10:27:09 +1100 | [diff] [blame] | 1013 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1014 | if (cmdnum == I_CHOWN) { |
| 1015 | printf("Changing owner on %s\n", g.gl_pathv[i]); |
| 1016 | aa->uid = n_arg; |
| 1017 | } else { |
| 1018 | printf("Changing group on %s\n", g.gl_pathv[i]); |
| 1019 | aa->gid = n_arg; |
| 1020 | } |
| 1021 | err = do_setstat(conn, g.gl_pathv[i], aa); |
| 1022 | if (err != 0 && err_abort) |
| 1023 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1024 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1025 | break; |
| 1026 | case I_PWD: |
| 1027 | printf("Remote working directory: %s\n", *pwd); |
| 1028 | break; |
| 1029 | case I_LPWD: |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1030 | if (!getcwd(path_buf, sizeof(path_buf))) { |
| 1031 | error("Couldn't get local cwd: %s", strerror(errno)); |
| 1032 | err = -1; |
| 1033 | break; |
| 1034 | } |
| 1035 | printf("Local working directory: %s\n", path_buf); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1036 | break; |
| 1037 | case I_QUIT: |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1038 | /* Processed below */ |
| 1039 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1040 | case I_HELP: |
| 1041 | help(); |
| 1042 | break; |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 1043 | case I_VERSION: |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1044 | printf("SFTP protocol version %u\n", sftp_proto_version(conn)); |
Ben Lindstrom | f78682d | 2001-03-14 21:26:27 +0000 | [diff] [blame] | 1045 | break; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1046 | case I_PROGRESS: |
| 1047 | showprogress = !showprogress; |
| 1048 | if (showprogress) |
| 1049 | printf("Progress meter enabled\n"); |
| 1050 | else |
| 1051 | printf("Progress meter disabled\n"); |
| 1052 | break; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1053 | default: |
| 1054 | fatal("%d is not implemented", cmdnum); |
| 1055 | } |
| 1056 | |
Ben Lindstrom | c8d1c30 | 2001-03-17 00:34:46 +0000 | [diff] [blame] | 1057 | if (g.gl_pathc) |
| 1058 | globfree(&g); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1059 | if (path1) |
| 1060 | xfree(path1); |
| 1061 | if (path2) |
| 1062 | xfree(path2); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1063 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1064 | /* If an unignored error occurs in batch mode we should abort. */ |
| 1065 | if (err_abort && err != 0) |
| 1066 | return (-1); |
| 1067 | else if (cmdnum == I_QUIT) |
| 1068 | return (1); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1069 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1070 | return (0); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1071 | } |
| 1072 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1073 | int |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1074 | interactive_loop(int fd_in, int fd_out, char *file1, char *file2) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1075 | { |
| 1076 | char *pwd; |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1077 | char *dir = NULL; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1078 | char cmd[2048]; |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1079 | struct sftp_conn *conn; |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1080 | int err; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1081 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1082 | conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests); |
| 1083 | if (conn == NULL) |
Damien Miller | 058316f | 2001-03-08 10:08:49 +1100 | [diff] [blame] | 1084 | fatal("Couldn't initialise connection to server"); |
| 1085 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1086 | pwd = do_realpath(conn, "."); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1087 | if (pwd == NULL) |
| 1088 | fatal("Need cwd"); |
| 1089 | |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1090 | if (file1 != NULL) { |
| 1091 | dir = xstrdup(file1); |
| 1092 | dir = make_absolute(dir, pwd); |
| 1093 | |
Damien Miller | 3db5f53 | 2002-02-13 14:10:32 +1100 | [diff] [blame] | 1094 | if (remote_is_dir(conn, dir) && file2 == NULL) { |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1095 | printf("Changing to: %s\n", dir); |
| 1096 | snprintf(cmd, sizeof cmd, "cd \"%s\"", dir); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1097 | if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) |
| 1098 | return (-1); |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1099 | } else { |
| 1100 | if (file2 == NULL) |
| 1101 | snprintf(cmd, sizeof cmd, "get %s", dir); |
| 1102 | else |
| 1103 | snprintf(cmd, sizeof cmd, "get %s %s", dir, |
| 1104 | file2); |
| 1105 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1106 | err = parse_dispatch_command(conn, cmd, &pwd, 1); |
Ben Lindstrom | cb1f60e | 2002-03-22 02:47:28 +0000 | [diff] [blame] | 1107 | xfree(dir); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1108 | xfree(pwd); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1109 | return (err); |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1110 | } |
Ben Lindstrom | cb1f60e | 2002-03-22 02:47:28 +0000 | [diff] [blame] | 1111 | xfree(dir); |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1112 | } |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1113 | |
Ben Lindstrom | 6aebb34 | 2001-05-09 00:38:19 +0000 | [diff] [blame] | 1114 | #if HAVE_SETVBUF |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1115 | setvbuf(stdout, NULL, _IOLBF, 0); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1116 | setvbuf(infile, NULL, _IOLBF, 0); |
Ben Lindstrom | 6aebb34 | 2001-05-09 00:38:19 +0000 | [diff] [blame] | 1117 | #else |
| 1118 | setlinebuf(stdout); |
| 1119 | setlinebuf(infile); |
| 1120 | #endif |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1121 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1122 | err = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1123 | for (;;) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1124 | char *cp; |
| 1125 | |
| 1126 | printf("sftp> "); |
| 1127 | |
| 1128 | /* XXX: use libedit */ |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1129 | if (fgets(cmd, sizeof(cmd), infile) == NULL) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1130 | printf("\n"); |
| 1131 | break; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1132 | } else if (infile != stdin) /* Bluff typing */ |
| 1133 | printf("%s", cmd); |
| 1134 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1135 | cp = strrchr(cmd, '\n'); |
| 1136 | if (cp) |
| 1137 | *cp = '\0'; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1138 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1139 | err = parse_dispatch_command(conn, cmd, &pwd, infile != stdin); |
| 1140 | if (err != 0) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1141 | break; |
| 1142 | } |
| 1143 | xfree(pwd); |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1144 | |
| 1145 | /* err == 1 signifies normal "quit" exit */ |
| 1146 | return (err >= 0 ? 0 : -1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1147 | } |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1148 | |