Damien Miller | aec5cf8 | 2008-02-10 22:26:24 +1100 | [diff] [blame] | 1 | /* $OpenBSD: sftp.c,v 1.99 2008/01/20 00:38:30 djm Exp $ */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 2 | /* |
Damien Miller | 4e60ed7 | 2004-02-17 17:07:59 +1100 | [diff] [blame] | 3 | * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 4 | * |
Damien Miller | 4e60ed7 | 2004-02-17 17:07:59 +1100 | [diff] [blame] | 5 | * Permission to use, copy, modify, and distribute this software for any |
| 6 | * purpose with or without fee is hereby granted, provided that the above |
| 7 | * copyright notice and this permission notice appear in all copies. |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 8 | * |
Damien Miller | 4e60ed7 | 2004-02-17 17:07:59 +1100 | [diff] [blame] | 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include "includes.h" |
| 19 | |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 20 | #include <sys/types.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 21 | #include <sys/ioctl.h> |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 22 | #ifdef HAVE_SYS_STAT_H |
| 23 | # include <sys/stat.h> |
| 24 | #endif |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 25 | #include <sys/param.h> |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 26 | #include <sys/socket.h> |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 27 | #include <sys/wait.h> |
Darren Tucker | 2d963d8 | 2004-11-07 20:04:10 +1100 | [diff] [blame] | 28 | |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 29 | #include <ctype.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 30 | #include <errno.h> |
| 31 | |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 32 | #ifdef HAVE_PATHS_H |
Damien Miller | a9263d0 | 2006-03-15 11:18:26 +1100 | [diff] [blame] | 33 | # include <paths.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 34 | #endif |
Darren Tucker | 2d963d8 | 2004-11-07 20:04:10 +1100 | [diff] [blame] | 35 | #ifdef USE_LIBEDIT |
| 36 | #include <histedit.h> |
| 37 | #else |
| 38 | typedef void EditLine; |
| 39 | #endif |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 40 | #include <signal.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 41 | #include <stdlib.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 42 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 43 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 44 | #include <unistd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 45 | #include <stdarg.h> |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 46 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 47 | #include "xmalloc.h" |
| 48 | #include "log.h" |
| 49 | #include "pathnames.h" |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 50 | #include "misc.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 51 | |
| 52 | #include "sftp.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 53 | #include "buffer.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 54 | #include "sftp-common.h" |
| 55 | #include "sftp-client.h" |
Damien Miller | d7d46bb | 2004-02-18 14:11:13 +1100 | [diff] [blame] | 56 | |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 57 | /* File to read commands from */ |
| 58 | FILE* infile; |
| 59 | |
| 60 | /* Are we in batchfile mode? */ |
| 61 | int batchmode = 0; |
| 62 | |
| 63 | /* Size of buffer used when copying files */ |
| 64 | size_t copy_buffer_len = 32768; |
| 65 | |
| 66 | /* Number of concurrent outstanding requests */ |
| 67 | size_t num_requests = 16; |
| 68 | |
| 69 | /* PID of ssh transport process */ |
| 70 | static pid_t sshpid = -1; |
| 71 | |
| 72 | /* This is set to 0 if the progressmeter is not desired. */ |
Damien Miller | c0f27d8 | 2004-03-08 23:12:19 +1100 | [diff] [blame] | 73 | int showprogress = 1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 74 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 75 | /* SIGINT received during command processing */ |
| 76 | volatile sig_atomic_t interrupted = 0; |
| 77 | |
Darren Tucker | b912345 | 2004-06-22 13:06:45 +1000 | [diff] [blame] | 78 | /* I wish qsort() took a separate ctx for the comparison function...*/ |
| 79 | int sort_flag; |
| 80 | |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 81 | int remote_glob(struct sftp_conn *, const char *, int, |
| 82 | int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 83 | |
Kevin Steves | 12888d1 | 2001-03-05 19:50:57 +0000 | [diff] [blame] | 84 | extern char *__progname; |
Kevin Steves | 12888d1 | 2001-03-05 19:50:57 +0000 | [diff] [blame] | 85 | |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 86 | /* Separators for interactive commands */ |
| 87 | #define WHITESPACE " \t\r\n" |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 88 | |
Darren Tucker | b912345 | 2004-06-22 13:06:45 +1000 | [diff] [blame] | 89 | /* ls flags */ |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 90 | #define LS_LONG_VIEW 0x01 /* Full view ala ls -l */ |
| 91 | #define LS_SHORT_VIEW 0x02 /* Single row view ala ls -1 */ |
| 92 | #define LS_NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */ |
| 93 | #define LS_NAME_SORT 0x08 /* Sort by name (default) */ |
| 94 | #define LS_TIME_SORT 0x10 /* Sort by mtime */ |
| 95 | #define LS_SIZE_SORT 0x20 /* Sort by file size */ |
| 96 | #define LS_REVERSE_SORT 0x40 /* Reverse sort order */ |
Darren Tucker | 9a52645 | 2004-06-22 13:09:55 +1000 | [diff] [blame] | 97 | #define LS_SHOW_ALL 0x80 /* Don't skip filenames starting with '.' */ |
Darren Tucker | b912345 | 2004-06-22 13:06:45 +1000 | [diff] [blame] | 98 | |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 99 | #define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW) |
| 100 | #define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT) |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 101 | |
| 102 | /* Commands for interactive mode */ |
| 103 | #define I_CHDIR 1 |
| 104 | #define I_CHGRP 2 |
| 105 | #define I_CHMOD 3 |
| 106 | #define I_CHOWN 4 |
| 107 | #define I_GET 5 |
| 108 | #define I_HELP 6 |
| 109 | #define I_LCHDIR 7 |
| 110 | #define I_LLS 8 |
| 111 | #define I_LMKDIR 9 |
| 112 | #define I_LPWD 10 |
| 113 | #define I_LS 11 |
| 114 | #define I_LUMASK 12 |
| 115 | #define I_MKDIR 13 |
| 116 | #define I_PUT 14 |
| 117 | #define I_PWD 15 |
| 118 | #define I_QUIT 16 |
| 119 | #define I_RENAME 17 |
| 120 | #define I_RM 18 |
| 121 | #define I_RMDIR 19 |
| 122 | #define I_SHELL 20 |
| 123 | #define I_SYMLINK 21 |
| 124 | #define I_VERSION 22 |
| 125 | #define I_PROGRESS 23 |
| 126 | |
| 127 | struct CMD { |
| 128 | const char *c; |
| 129 | const int n; |
| 130 | }; |
| 131 | |
| 132 | static const struct CMD cmds[] = { |
| 133 | { "bye", I_QUIT }, |
| 134 | { "cd", I_CHDIR }, |
| 135 | { "chdir", I_CHDIR }, |
| 136 | { "chgrp", I_CHGRP }, |
| 137 | { "chmod", I_CHMOD }, |
| 138 | { "chown", I_CHOWN }, |
| 139 | { "dir", I_LS }, |
| 140 | { "exit", I_QUIT }, |
| 141 | { "get", I_GET }, |
| 142 | { "mget", I_GET }, |
| 143 | { "help", I_HELP }, |
| 144 | { "lcd", I_LCHDIR }, |
| 145 | { "lchdir", I_LCHDIR }, |
| 146 | { "lls", I_LLS }, |
| 147 | { "lmkdir", I_LMKDIR }, |
| 148 | { "ln", I_SYMLINK }, |
| 149 | { "lpwd", I_LPWD }, |
| 150 | { "ls", I_LS }, |
| 151 | { "lumask", I_LUMASK }, |
| 152 | { "mkdir", I_MKDIR }, |
| 153 | { "progress", I_PROGRESS }, |
| 154 | { "put", I_PUT }, |
| 155 | { "mput", I_PUT }, |
| 156 | { "pwd", I_PWD }, |
| 157 | { "quit", I_QUIT }, |
| 158 | { "rename", I_RENAME }, |
| 159 | { "rm", I_RM }, |
| 160 | { "rmdir", I_RMDIR }, |
| 161 | { "symlink", I_SYMLINK }, |
| 162 | { "version", I_VERSION }, |
| 163 | { "!", I_SHELL }, |
| 164 | { "?", I_HELP }, |
| 165 | { NULL, -1} |
| 166 | }; |
| 167 | |
| 168 | int interactive_loop(int fd_in, int fd_out, char *file1, char *file2); |
| 169 | |
Damien Miller | b6c85fc | 2007-01-05 16:30:41 +1100 | [diff] [blame] | 170 | /* ARGSUSED */ |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 171 | static void |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 172 | killchild(int signo) |
| 173 | { |
Darren Tucker | ba66df8 | 2005-01-24 21:57:40 +1100 | [diff] [blame] | 174 | if (sshpid > 1) { |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 175 | kill(sshpid, SIGTERM); |
Darren Tucker | ba66df8 | 2005-01-24 21:57:40 +1100 | [diff] [blame] | 176 | waitpid(sshpid, NULL, 0); |
| 177 | } |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 178 | |
| 179 | _exit(1); |
| 180 | } |
| 181 | |
Damien Miller | b6c85fc | 2007-01-05 16:30:41 +1100 | [diff] [blame] | 182 | /* ARGSUSED */ |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 183 | static void |
| 184 | cmd_interrupt(int signo) |
| 185 | { |
| 186 | const char msg[] = "\rInterrupt \n"; |
Darren Tucker | e2f189a | 2004-12-06 22:45:53 +1100 | [diff] [blame] | 187 | int olderrno = errno; |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 188 | |
| 189 | write(STDERR_FILENO, msg, sizeof(msg) - 1); |
| 190 | interrupted = 1; |
Darren Tucker | e2f189a | 2004-12-06 22:45:53 +1100 | [diff] [blame] | 191 | errno = olderrno; |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static void |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 195 | help(void) |
| 196 | { |
| 197 | printf("Available commands:\n"); |
| 198 | printf("cd path Change remote directory to 'path'\n"); |
| 199 | printf("lcd path Change local directory to 'path'\n"); |
| 200 | printf("chgrp grp path Change group of file 'path' to 'grp'\n"); |
| 201 | printf("chmod mode path Change permissions of file 'path' to 'mode'\n"); |
| 202 | printf("chown own path Change owner of file 'path' to 'own'\n"); |
| 203 | printf("help Display this help text\n"); |
| 204 | printf("get remote-path [local-path] Download file\n"); |
| 205 | printf("lls [ls-options [path]] Display local directory listing\n"); |
| 206 | printf("ln oldpath newpath Symlink remote file\n"); |
| 207 | printf("lmkdir path Create local directory\n"); |
| 208 | printf("lpwd Print local working directory\n"); |
| 209 | printf("ls [path] Display remote directory listing\n"); |
| 210 | printf("lumask umask Set local umask to 'umask'\n"); |
| 211 | printf("mkdir path Create remote directory\n"); |
| 212 | printf("progress Toggle display of progress meter\n"); |
| 213 | printf("put local-path [remote-path] Upload file\n"); |
| 214 | printf("pwd Display remote working directory\n"); |
| 215 | printf("exit Quit sftp\n"); |
| 216 | printf("quit Quit sftp\n"); |
| 217 | printf("rename oldpath newpath Rename remote file\n"); |
| 218 | printf("rmdir path Remove remote directory\n"); |
| 219 | printf("rm path Delete remote file\n"); |
| 220 | printf("symlink oldpath newpath Symlink remote file\n"); |
| 221 | printf("version Show SFTP version\n"); |
| 222 | printf("!command Execute 'command' in local shell\n"); |
| 223 | printf("! Escape to local shell\n"); |
| 224 | printf("? Synonym for help\n"); |
| 225 | } |
| 226 | |
| 227 | static void |
| 228 | local_do_shell(const char *args) |
| 229 | { |
| 230 | int status; |
| 231 | char *shell; |
| 232 | pid_t pid; |
| 233 | |
| 234 | if (!*args) |
| 235 | args = NULL; |
| 236 | |
| 237 | if ((shell = getenv("SHELL")) == NULL) |
| 238 | shell = _PATH_BSHELL; |
| 239 | |
| 240 | if ((pid = fork()) == -1) |
| 241 | fatal("Couldn't fork: %s", strerror(errno)); |
| 242 | |
| 243 | if (pid == 0) { |
| 244 | /* XXX: child has pipe fds to ssh subproc open - issue? */ |
| 245 | if (args) { |
| 246 | debug3("Executing %s -c \"%s\"", shell, args); |
| 247 | execl(shell, shell, "-c", args, (char *)NULL); |
| 248 | } else { |
| 249 | debug3("Executing %s", shell); |
| 250 | execl(shell, shell, (char *)NULL); |
| 251 | } |
| 252 | fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell, |
| 253 | strerror(errno)); |
| 254 | _exit(1); |
| 255 | } |
| 256 | while (waitpid(pid, &status, 0) == -1) |
| 257 | if (errno != EINTR) |
| 258 | fatal("Couldn't wait for child: %s", strerror(errno)); |
| 259 | if (!WIFEXITED(status)) |
Damien Miller | 55b04f1 | 2006-03-26 14:23:17 +1100 | [diff] [blame] | 260 | error("Shell exited abnormally"); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 261 | else if (WEXITSTATUS(status)) |
| 262 | error("Shell exited with status %d", WEXITSTATUS(status)); |
| 263 | } |
| 264 | |
| 265 | static void |
| 266 | local_do_ls(const char *args) |
| 267 | { |
| 268 | if (!args || !*args) |
| 269 | local_do_shell(_PATH_LS); |
| 270 | else { |
| 271 | int len = strlen(_PATH_LS " ") + strlen(args) + 1; |
| 272 | char *buf = xmalloc(len); |
| 273 | |
| 274 | /* XXX: quoting - rip quoting code from ftp? */ |
| 275 | snprintf(buf, len, _PATH_LS " %s", args); |
| 276 | local_do_shell(buf); |
| 277 | xfree(buf); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* Strip one path (usually the pwd) from the start of another */ |
| 282 | static char * |
| 283 | path_strip(char *path, char *strip) |
| 284 | { |
| 285 | size_t len; |
| 286 | |
| 287 | if (strip == NULL) |
| 288 | return (xstrdup(path)); |
| 289 | |
| 290 | len = strlen(strip); |
Darren Tucker | e2f189a | 2004-12-06 22:45:53 +1100 | [diff] [blame] | 291 | if (strncmp(path, strip, len) == 0) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 292 | if (strip[len - 1] != '/' && path[len] == '/') |
| 293 | len++; |
| 294 | return (xstrdup(path + len)); |
| 295 | } |
| 296 | |
| 297 | return (xstrdup(path)); |
| 298 | } |
| 299 | |
| 300 | static char * |
| 301 | path_append(char *p1, char *p2) |
| 302 | { |
| 303 | char *ret; |
Damien Miller | 3ca8b77 | 2007-01-05 16:24:47 +1100 | [diff] [blame] | 304 | size_t len = strlen(p1) + strlen(p2) + 2; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 305 | |
| 306 | ret = xmalloc(len); |
| 307 | strlcpy(ret, p1, len); |
Damien Miller | 3ca8b77 | 2007-01-05 16:24:47 +1100 | [diff] [blame] | 308 | if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/') |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 309 | strlcat(ret, "/", len); |
| 310 | strlcat(ret, p2, len); |
| 311 | |
| 312 | return(ret); |
| 313 | } |
| 314 | |
| 315 | static char * |
| 316 | make_absolute(char *p, char *pwd) |
| 317 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 318 | char *abs_str; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 319 | |
| 320 | /* Derelativise */ |
| 321 | if (p && p[0] != '/') { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 322 | abs_str = path_append(pwd, p); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 323 | xfree(p); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 324 | return(abs_str); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 325 | } else |
| 326 | return(p); |
| 327 | } |
| 328 | |
| 329 | static int |
| 330 | infer_path(const char *p, char **ifp) |
| 331 | { |
| 332 | char *cp; |
| 333 | |
| 334 | cp = strrchr(p, '/'); |
| 335 | if (cp == NULL) { |
| 336 | *ifp = xstrdup(p); |
| 337 | return(0); |
| 338 | } |
| 339 | |
| 340 | if (!cp[1]) { |
| 341 | error("Invalid path"); |
| 342 | return(-1); |
| 343 | } |
| 344 | |
| 345 | *ifp = xstrdup(cp + 1); |
| 346 | return(0); |
| 347 | } |
| 348 | |
| 349 | static int |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 350 | parse_getput_flags(const char *cmd, char **argv, int argc, int *pflag) |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 351 | { |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 352 | extern int optind, optreset, opterr; |
| 353 | int ch; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 354 | |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 355 | optind = optreset = 1; |
| 356 | opterr = 0; |
| 357 | |
| 358 | *pflag = 0; |
| 359 | while ((ch = getopt(argc, argv, "Pp")) != -1) { |
| 360 | switch (ch) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 361 | case 'p': |
| 362 | case 'P': |
| 363 | *pflag = 1; |
| 364 | break; |
| 365 | default: |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 366 | error("%s: Invalid flag -%c", cmd, ch); |
| 367 | return -1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 368 | } |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 369 | } |
| 370 | |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 371 | return optind; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static int |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 375 | parse_ls_flags(char **argv, int argc, int *lflag) |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 376 | { |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 377 | extern int optind, optreset, opterr; |
| 378 | int ch; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 379 | |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 380 | optind = optreset = 1; |
| 381 | opterr = 0; |
| 382 | |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 383 | *lflag = LS_NAME_SORT; |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 384 | while ((ch = getopt(argc, argv, "1Saflnrt")) != -1) { |
| 385 | switch (ch) { |
| 386 | case '1': |
| 387 | *lflag &= ~VIEW_FLAGS; |
| 388 | *lflag |= LS_SHORT_VIEW; |
| 389 | break; |
| 390 | case 'S': |
| 391 | *lflag &= ~SORT_FLAGS; |
| 392 | *lflag |= LS_SIZE_SORT; |
| 393 | break; |
| 394 | case 'a': |
| 395 | *lflag |= LS_SHOW_ALL; |
| 396 | break; |
| 397 | case 'f': |
| 398 | *lflag &= ~SORT_FLAGS; |
| 399 | break; |
| 400 | case 'l': |
| 401 | *lflag &= ~VIEW_FLAGS; |
| 402 | *lflag |= LS_LONG_VIEW; |
| 403 | break; |
| 404 | case 'n': |
| 405 | *lflag &= ~VIEW_FLAGS; |
| 406 | *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW; |
| 407 | break; |
| 408 | case 'r': |
| 409 | *lflag |= LS_REVERSE_SORT; |
| 410 | break; |
| 411 | case 't': |
| 412 | *lflag &= ~SORT_FLAGS; |
| 413 | *lflag |= LS_TIME_SORT; |
| 414 | break; |
| 415 | default: |
| 416 | error("ls: Invalid flag -%c", ch); |
| 417 | return -1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 418 | } |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 419 | } |
| 420 | |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 421 | return optind; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static int |
| 425 | is_dir(char *path) |
| 426 | { |
| 427 | struct stat sb; |
| 428 | |
| 429 | /* XXX: report errors? */ |
| 430 | if (stat(path, &sb) == -1) |
| 431 | return(0); |
| 432 | |
Darren Tucker | 1e80e40 | 2006-09-21 12:59:33 +1000 | [diff] [blame] | 433 | return(S_ISDIR(sb.st_mode)); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static int |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 437 | remote_is_dir(struct sftp_conn *conn, char *path) |
| 438 | { |
| 439 | Attrib *a; |
| 440 | |
| 441 | /* XXX: report errors? */ |
| 442 | if ((a = do_stat(conn, path, 1)) == NULL) |
| 443 | return(0); |
| 444 | if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) |
| 445 | return(0); |
Darren Tucker | 1e80e40 | 2006-09-21 12:59:33 +1000 | [diff] [blame] | 446 | return(S_ISDIR(a->perm)); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | static int |
| 450 | process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) |
| 451 | { |
| 452 | char *abs_src = NULL; |
| 453 | char *abs_dst = NULL; |
| 454 | char *tmp; |
| 455 | glob_t g; |
| 456 | int err = 0; |
| 457 | int i; |
| 458 | |
| 459 | abs_src = xstrdup(src); |
| 460 | abs_src = make_absolute(abs_src, pwd); |
| 461 | |
| 462 | memset(&g, 0, sizeof(g)); |
| 463 | debug3("Looking up %s", abs_src); |
| 464 | if (remote_glob(conn, abs_src, 0, NULL, &g)) { |
| 465 | error("File \"%s\" not found.", abs_src); |
| 466 | err = -1; |
| 467 | goto out; |
| 468 | } |
| 469 | |
| 470 | /* If multiple matches, dst must be a directory or unspecified */ |
| 471 | if (g.gl_matchc > 1 && dst && !is_dir(dst)) { |
| 472 | error("Multiple files match, but \"%s\" is not a directory", |
| 473 | dst); |
| 474 | err = -1; |
| 475 | goto out; |
| 476 | } |
| 477 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 478 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 479 | if (infer_path(g.gl_pathv[i], &tmp)) { |
| 480 | err = -1; |
| 481 | goto out; |
| 482 | } |
| 483 | |
| 484 | if (g.gl_matchc == 1 && dst) { |
| 485 | /* If directory specified, append filename */ |
Damien Miller | 40b5985 | 2006-06-13 13:00:25 +1000 | [diff] [blame] | 486 | xfree(tmp); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 487 | if (is_dir(dst)) { |
| 488 | if (infer_path(g.gl_pathv[0], &tmp)) { |
| 489 | err = 1; |
| 490 | goto out; |
| 491 | } |
| 492 | abs_dst = path_append(dst, tmp); |
| 493 | xfree(tmp); |
| 494 | } else |
| 495 | abs_dst = xstrdup(dst); |
| 496 | } else if (dst) { |
| 497 | abs_dst = path_append(dst, tmp); |
| 498 | xfree(tmp); |
| 499 | } else |
| 500 | abs_dst = tmp; |
| 501 | |
| 502 | printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst); |
| 503 | if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1) |
| 504 | err = -1; |
| 505 | xfree(abs_dst); |
| 506 | abs_dst = NULL; |
| 507 | } |
| 508 | |
| 509 | out: |
| 510 | xfree(abs_src); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 511 | globfree(&g); |
| 512 | return(err); |
| 513 | } |
| 514 | |
| 515 | static int |
| 516 | process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) |
| 517 | { |
| 518 | char *tmp_dst = NULL; |
| 519 | char *abs_dst = NULL; |
| 520 | char *tmp; |
| 521 | glob_t g; |
| 522 | int err = 0; |
| 523 | int i; |
Damien Miller | aec5cf8 | 2008-02-10 22:26:24 +1100 | [diff] [blame] | 524 | struct stat sb; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 525 | |
| 526 | if (dst) { |
| 527 | tmp_dst = xstrdup(dst); |
| 528 | tmp_dst = make_absolute(tmp_dst, pwd); |
| 529 | } |
| 530 | |
| 531 | memset(&g, 0, sizeof(g)); |
| 532 | debug3("Looking up %s", src); |
Damien Miller | aec5cf8 | 2008-02-10 22:26:24 +1100 | [diff] [blame] | 533 | if (glob(src, GLOB_NOCHECK, NULL, &g)) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 534 | error("File \"%s\" not found.", src); |
| 535 | err = -1; |
| 536 | goto out; |
| 537 | } |
| 538 | |
| 539 | /* If multiple matches, dst may be directory or unspecified */ |
| 540 | if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) { |
| 541 | error("Multiple files match, but \"%s\" is not a directory", |
| 542 | tmp_dst); |
| 543 | err = -1; |
| 544 | goto out; |
| 545 | } |
| 546 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 547 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
Damien Miller | aec5cf8 | 2008-02-10 22:26:24 +1100 | [diff] [blame] | 548 | if (stat(g.gl_pathv[i], &sb) == -1) { |
| 549 | err = -1; |
| 550 | error("stat %s: %s", g.gl_pathv[i], strerror(errno)); |
| 551 | continue; |
| 552 | } |
| 553 | |
| 554 | if (!S_ISREG(sb.st_mode)) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 555 | error("skipping non-regular file %s", |
| 556 | g.gl_pathv[i]); |
| 557 | continue; |
| 558 | } |
| 559 | if (infer_path(g.gl_pathv[i], &tmp)) { |
| 560 | err = -1; |
| 561 | goto out; |
| 562 | } |
| 563 | |
| 564 | if (g.gl_matchc == 1 && tmp_dst) { |
| 565 | /* If directory specified, append filename */ |
| 566 | if (remote_is_dir(conn, tmp_dst)) { |
| 567 | if (infer_path(g.gl_pathv[0], &tmp)) { |
| 568 | err = 1; |
| 569 | goto out; |
| 570 | } |
| 571 | abs_dst = path_append(tmp_dst, tmp); |
| 572 | xfree(tmp); |
| 573 | } else |
| 574 | abs_dst = xstrdup(tmp_dst); |
| 575 | |
| 576 | } else if (tmp_dst) { |
| 577 | abs_dst = path_append(tmp_dst, tmp); |
| 578 | xfree(tmp); |
| 579 | } else |
| 580 | abs_dst = make_absolute(tmp, pwd); |
| 581 | |
| 582 | printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst); |
| 583 | if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1) |
| 584 | err = -1; |
| 585 | } |
| 586 | |
| 587 | out: |
| 588 | if (abs_dst) |
| 589 | xfree(abs_dst); |
| 590 | if (tmp_dst) |
| 591 | xfree(tmp_dst); |
| 592 | globfree(&g); |
| 593 | return(err); |
| 594 | } |
| 595 | |
| 596 | static int |
| 597 | sdirent_comp(const void *aa, const void *bb) |
| 598 | { |
| 599 | SFTP_DIRENT *a = *(SFTP_DIRENT **)aa; |
| 600 | SFTP_DIRENT *b = *(SFTP_DIRENT **)bb; |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 601 | int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 602 | |
Darren Tucker | b912345 | 2004-06-22 13:06:45 +1000 | [diff] [blame] | 603 | #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1)) |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 604 | if (sort_flag & LS_NAME_SORT) |
Darren Tucker | b912345 | 2004-06-22 13:06:45 +1000 | [diff] [blame] | 605 | return (rmul * strcmp(a->filename, b->filename)); |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 606 | else if (sort_flag & LS_TIME_SORT) |
Darren Tucker | b912345 | 2004-06-22 13:06:45 +1000 | [diff] [blame] | 607 | return (rmul * NCMP(a->a.mtime, b->a.mtime)); |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 608 | else if (sort_flag & LS_SIZE_SORT) |
Darren Tucker | b912345 | 2004-06-22 13:06:45 +1000 | [diff] [blame] | 609 | return (rmul * NCMP(a->a.size, b->a.size)); |
| 610 | |
| 611 | fatal("Unknown ls sort type"); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* sftp ls.1 replacement for directories */ |
| 615 | static int |
| 616 | do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) |
| 617 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 618 | int n; |
| 619 | u_int c = 1, colspace = 0, columns = 1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 620 | SFTP_DIRENT **d; |
| 621 | |
| 622 | if ((n = do_readdir(conn, path, &d)) != 0) |
| 623 | return (n); |
| 624 | |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 625 | if (!(lflag & LS_SHORT_VIEW)) { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 626 | u_int m = 0, width = 80; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 627 | struct winsize ws; |
| 628 | char *tmp; |
| 629 | |
| 630 | /* Count entries for sort and find longest filename */ |
Darren Tucker | 9a52645 | 2004-06-22 13:09:55 +1000 | [diff] [blame] | 631 | for (n = 0; d[n] != NULL; n++) { |
| 632 | if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL)) |
| 633 | m = MAX(m, strlen(d[n]->filename)); |
| 634 | } |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 635 | |
| 636 | /* Add any subpath that also needs to be counted */ |
| 637 | tmp = path_strip(path, strip_path); |
| 638 | m += strlen(tmp); |
| 639 | xfree(tmp); |
| 640 | |
| 641 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) |
| 642 | width = ws.ws_col; |
| 643 | |
| 644 | columns = width / (m + 2); |
| 645 | columns = MAX(columns, 1); |
| 646 | colspace = width / columns; |
| 647 | colspace = MIN(colspace, width); |
| 648 | } |
| 649 | |
Darren Tucker | b912345 | 2004-06-22 13:06:45 +1000 | [diff] [blame] | 650 | if (lflag & SORT_FLAGS) { |
Damien Miller | 653b93b | 2005-11-05 15:15:23 +1100 | [diff] [blame] | 651 | for (n = 0; d[n] != NULL; n++) |
| 652 | ; /* count entries */ |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 653 | sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT); |
Darren Tucker | b912345 | 2004-06-22 13:06:45 +1000 | [diff] [blame] | 654 | qsort(d, n, sizeof(*d), sdirent_comp); |
| 655 | } |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 656 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 657 | for (n = 0; d[n] != NULL && !interrupted; n++) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 658 | char *tmp, *fname; |
| 659 | |
Darren Tucker | 9a52645 | 2004-06-22 13:09:55 +1000 | [diff] [blame] | 660 | if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL)) |
| 661 | continue; |
| 662 | |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 663 | tmp = path_append(path, d[n]->filename); |
| 664 | fname = path_strip(tmp, strip_path); |
| 665 | xfree(tmp); |
| 666 | |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 667 | if (lflag & LS_LONG_VIEW) { |
| 668 | if (lflag & LS_NUMERIC_VIEW) { |
Darren Tucker | b215c5d | 2004-06-22 12:30:53 +1000 | [diff] [blame] | 669 | char *lname; |
| 670 | struct stat sb; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 671 | |
Darren Tucker | b215c5d | 2004-06-22 12:30:53 +1000 | [diff] [blame] | 672 | memset(&sb, 0, sizeof(sb)); |
| 673 | attrib_to_stat(&d[n]->a, &sb); |
| 674 | lname = ls_file(fname, &sb, 1); |
| 675 | printf("%s\n", lname); |
| 676 | xfree(lname); |
| 677 | } else |
| 678 | printf("%s\n", d[n]->longname); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 679 | } else { |
| 680 | printf("%-*s", colspace, fname); |
| 681 | if (c >= columns) { |
| 682 | printf("\n"); |
| 683 | c = 1; |
| 684 | } else |
| 685 | c++; |
| 686 | } |
| 687 | |
| 688 | xfree(fname); |
| 689 | } |
| 690 | |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 691 | if (!(lflag & LS_LONG_VIEW) && (c != 1)) |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 692 | printf("\n"); |
| 693 | |
| 694 | free_sftp_dirents(d); |
| 695 | return (0); |
| 696 | } |
| 697 | |
| 698 | /* sftp ls.1 replacement which handles path globs */ |
| 699 | static int |
| 700 | do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, |
| 701 | int lflag) |
| 702 | { |
| 703 | glob_t g; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 704 | u_int i, c = 1, colspace = 0, columns = 1; |
Darren Tucker | 596dcfa | 2004-12-11 13:37:22 +1100 | [diff] [blame] | 705 | Attrib *a = NULL; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 706 | |
| 707 | memset(&g, 0, sizeof(g)); |
| 708 | |
| 709 | if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE, |
Darren Tucker | 596dcfa | 2004-12-11 13:37:22 +1100 | [diff] [blame] | 710 | NULL, &g) || (g.gl_pathc && !g.gl_matchc)) { |
| 711 | if (g.gl_pathc) |
| 712 | globfree(&g); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 713 | error("Can't ls: \"%s\" not found", path); |
| 714 | return (-1); |
| 715 | } |
| 716 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 717 | if (interrupted) |
| 718 | goto out; |
| 719 | |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 720 | /* |
Darren Tucker | 596dcfa | 2004-12-11 13:37:22 +1100 | [diff] [blame] | 721 | * If the glob returns a single match and it is a directory, |
| 722 | * then just list its contents. |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 723 | */ |
Darren Tucker | 596dcfa | 2004-12-11 13:37:22 +1100 | [diff] [blame] | 724 | if (g.gl_matchc == 1) { |
| 725 | if ((a = do_lstat(conn, g.gl_pathv[0], 1)) == NULL) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 726 | globfree(&g); |
| 727 | return (-1); |
| 728 | } |
| 729 | if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
| 730 | S_ISDIR(a->perm)) { |
Darren Tucker | 596dcfa | 2004-12-11 13:37:22 +1100 | [diff] [blame] | 731 | int err; |
| 732 | |
| 733 | err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 734 | globfree(&g); |
Darren Tucker | 596dcfa | 2004-12-11 13:37:22 +1100 | [diff] [blame] | 735 | return (err); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 739 | if (!(lflag & LS_SHORT_VIEW)) { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 740 | u_int m = 0, width = 80; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 741 | struct winsize ws; |
| 742 | |
| 743 | /* Count entries for sort and find longest filename */ |
| 744 | for (i = 0; g.gl_pathv[i]; i++) |
| 745 | m = MAX(m, strlen(g.gl_pathv[i])); |
| 746 | |
| 747 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) |
| 748 | width = ws.ws_col; |
| 749 | |
| 750 | columns = width / (m + 2); |
| 751 | columns = MAX(columns, 1); |
| 752 | colspace = width / columns; |
| 753 | } |
| 754 | |
Darren Tucker | 596dcfa | 2004-12-11 13:37:22 +1100 | [diff] [blame] | 755 | for (i = 0; g.gl_pathv[i] && !interrupted; i++, a = NULL) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 756 | char *fname; |
| 757 | |
| 758 | fname = path_strip(g.gl_pathv[i], strip_path); |
| 759 | |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 760 | if (lflag & LS_LONG_VIEW) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 761 | char *lname; |
| 762 | struct stat sb; |
| 763 | |
| 764 | /* |
| 765 | * XXX: this is slow - 1 roundtrip per path |
| 766 | * A solution to this is to fork glob() and |
| 767 | * build a sftp specific version which keeps the |
| 768 | * attribs (which currently get thrown away) |
| 769 | * that the server returns as well as the filenames. |
| 770 | */ |
| 771 | memset(&sb, 0, sizeof(sb)); |
Darren Tucker | 596dcfa | 2004-12-11 13:37:22 +1100 | [diff] [blame] | 772 | if (a == NULL) |
| 773 | a = do_lstat(conn, g.gl_pathv[i], 1); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 774 | if (a != NULL) |
| 775 | attrib_to_stat(a, &sb); |
| 776 | lname = ls_file(fname, &sb, 1); |
| 777 | printf("%s\n", lname); |
| 778 | xfree(lname); |
| 779 | } else { |
| 780 | printf("%-*s", colspace, fname); |
| 781 | if (c >= columns) { |
| 782 | printf("\n"); |
| 783 | c = 1; |
| 784 | } else |
| 785 | c++; |
| 786 | } |
| 787 | xfree(fname); |
| 788 | } |
| 789 | |
Darren Tucker | a4e9ffa | 2004-06-22 13:07:58 +1000 | [diff] [blame] | 790 | if (!(lflag & LS_LONG_VIEW) && (c != 1)) |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 791 | printf("\n"); |
| 792 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 793 | out: |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 794 | if (g.gl_pathc) |
| 795 | globfree(&g); |
| 796 | |
| 797 | return (0); |
| 798 | } |
| 799 | |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 800 | /* |
| 801 | * Undo escaping of glob sequences in place. Used to undo extra escaping |
| 802 | * applied in makeargv() when the string is destined for a function that |
| 803 | * does not glob it. |
| 804 | */ |
| 805 | static void |
| 806 | undo_glob_escape(char *s) |
| 807 | { |
| 808 | size_t i, j; |
| 809 | |
| 810 | for (i = j = 0;;) { |
| 811 | if (s[i] == '\0') { |
| 812 | s[j] = '\0'; |
| 813 | return; |
| 814 | } |
| 815 | if (s[i] != '\\') { |
| 816 | s[j++] = s[i++]; |
| 817 | continue; |
| 818 | } |
| 819 | /* s[i] == '\\' */ |
| 820 | ++i; |
| 821 | switch (s[i]) { |
| 822 | case '?': |
| 823 | case '[': |
| 824 | case '*': |
| 825 | case '\\': |
| 826 | s[j++] = s[i++]; |
| 827 | break; |
| 828 | case '\0': |
| 829 | s[j++] = '\\'; |
| 830 | s[j] = '\0'; |
| 831 | return; |
| 832 | default: |
| 833 | s[j++] = '\\'; |
| 834 | s[j++] = s[i++]; |
| 835 | break; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Split a string into an argument vector using sh(1)-style quoting, |
| 842 | * comment and escaping rules, but with some tweaks to handle glob(3) |
| 843 | * wildcards. |
| 844 | * Returns NULL on error or a NULL-terminated array of arguments. |
| 845 | */ |
| 846 | #define MAXARGS 128 |
| 847 | #define MAXARGLEN 8192 |
| 848 | static char ** |
| 849 | makeargv(const char *arg, int *argcp) |
| 850 | { |
| 851 | int argc, quot; |
| 852 | size_t i, j; |
| 853 | static char argvs[MAXARGLEN]; |
| 854 | static char *argv[MAXARGS + 1]; |
| 855 | enum { MA_START, MA_SQUOTE, MA_DQUOTE, MA_UNQUOTED } state, q; |
| 856 | |
| 857 | *argcp = argc = 0; |
| 858 | if (strlen(arg) > sizeof(argvs) - 1) { |
| 859 | args_too_longs: |
| 860 | error("string too long"); |
| 861 | return NULL; |
| 862 | } |
| 863 | state = MA_START; |
| 864 | i = j = 0; |
| 865 | for (;;) { |
| 866 | if (isspace(arg[i])) { |
| 867 | if (state == MA_UNQUOTED) { |
| 868 | /* Terminate current argument */ |
| 869 | argvs[j++] = '\0'; |
| 870 | argc++; |
| 871 | state = MA_START; |
| 872 | } else if (state != MA_START) |
| 873 | argvs[j++] = arg[i]; |
| 874 | } else if (arg[i] == '"' || arg[i] == '\'') { |
| 875 | q = arg[i] == '"' ? MA_DQUOTE : MA_SQUOTE; |
| 876 | if (state == MA_START) { |
| 877 | argv[argc] = argvs + j; |
| 878 | state = q; |
| 879 | } else if (state == MA_UNQUOTED) |
| 880 | state = q; |
| 881 | else if (state == q) |
| 882 | state = MA_UNQUOTED; |
| 883 | else |
| 884 | argvs[j++] = arg[i]; |
| 885 | } else if (arg[i] == '\\') { |
| 886 | if (state == MA_SQUOTE || state == MA_DQUOTE) { |
| 887 | quot = state == MA_SQUOTE ? '\'' : '"'; |
| 888 | /* Unescape quote we are in */ |
| 889 | /* XXX support \n and friends? */ |
| 890 | if (arg[i + 1] == quot) { |
| 891 | i++; |
| 892 | argvs[j++] = arg[i]; |
| 893 | } else if (arg[i + 1] == '?' || |
| 894 | arg[i + 1] == '[' || arg[i + 1] == '*') { |
| 895 | /* |
| 896 | * Special case for sftp: append |
| 897 | * double-escaped glob sequence - |
| 898 | * glob will undo one level of |
| 899 | * escaping. NB. string can grow here. |
| 900 | */ |
| 901 | if (j >= sizeof(argvs) - 5) |
| 902 | goto args_too_longs; |
| 903 | argvs[j++] = '\\'; |
| 904 | argvs[j++] = arg[i++]; |
| 905 | argvs[j++] = '\\'; |
| 906 | argvs[j++] = arg[i]; |
| 907 | } else { |
| 908 | argvs[j++] = arg[i++]; |
| 909 | argvs[j++] = arg[i]; |
| 910 | } |
| 911 | } else { |
| 912 | if (state == MA_START) { |
| 913 | argv[argc] = argvs + j; |
| 914 | state = MA_UNQUOTED; |
| 915 | } |
| 916 | if (arg[i + 1] == '?' || arg[i + 1] == '[' || |
| 917 | arg[i + 1] == '*' || arg[i + 1] == '\\') { |
| 918 | /* |
| 919 | * Special case for sftp: append |
| 920 | * escaped glob sequence - |
| 921 | * glob will undo one level of |
| 922 | * escaping. |
| 923 | */ |
| 924 | argvs[j++] = arg[i++]; |
| 925 | argvs[j++] = arg[i]; |
| 926 | } else { |
| 927 | /* Unescape everything */ |
| 928 | /* XXX support \n and friends? */ |
| 929 | i++; |
| 930 | argvs[j++] = arg[i]; |
| 931 | } |
| 932 | } |
| 933 | } else if (arg[i] == '#') { |
| 934 | if (state == MA_SQUOTE || state == MA_DQUOTE) |
| 935 | argvs[j++] = arg[i]; |
| 936 | else |
| 937 | goto string_done; |
| 938 | } else if (arg[i] == '\0') { |
| 939 | if (state == MA_SQUOTE || state == MA_DQUOTE) { |
| 940 | error("Unterminated quoted argument"); |
| 941 | return NULL; |
| 942 | } |
| 943 | string_done: |
| 944 | if (state == MA_UNQUOTED) { |
| 945 | argvs[j++] = '\0'; |
| 946 | argc++; |
| 947 | } |
| 948 | break; |
| 949 | } else { |
| 950 | if (state == MA_START) { |
| 951 | argv[argc] = argvs + j; |
| 952 | state = MA_UNQUOTED; |
| 953 | } |
| 954 | if ((state == MA_SQUOTE || state == MA_DQUOTE) && |
| 955 | (arg[i] == '?' || arg[i] == '[' || arg[i] == '*')) { |
| 956 | /* |
| 957 | * Special case for sftp: escape quoted |
| 958 | * glob(3) wildcards. NB. string can grow |
| 959 | * here. |
| 960 | */ |
| 961 | if (j >= sizeof(argvs) - 3) |
| 962 | goto args_too_longs; |
| 963 | argvs[j++] = '\\'; |
| 964 | argvs[j++] = arg[i]; |
| 965 | } else |
| 966 | argvs[j++] = arg[i]; |
| 967 | } |
| 968 | i++; |
| 969 | } |
| 970 | *argcp = argc; |
| 971 | return argv; |
| 972 | } |
| 973 | |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 974 | static int |
| 975 | parse_args(const char **cpp, int *pflag, int *lflag, int *iflag, |
| 976 | unsigned long *n_arg, char **path1, char **path2) |
| 977 | { |
| 978 | const char *cmd, *cp = *cpp; |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 979 | char *cp2, **argv; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 980 | int base = 0; |
| 981 | long l; |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 982 | int i, cmdnum, optidx, argc; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 983 | |
| 984 | /* Skip leading whitespace */ |
| 985 | cp = cp + strspn(cp, WHITESPACE); |
| 986 | |
| 987 | /* Ignore blank lines and lines which begin with comment '#' char */ |
| 988 | if (*cp == '\0' || *cp == '#') |
| 989 | return (0); |
| 990 | |
| 991 | /* Check for leading '-' (disable error processing) */ |
| 992 | *iflag = 0; |
| 993 | if (*cp == '-') { |
| 994 | *iflag = 1; |
| 995 | cp++; |
| 996 | } |
| 997 | |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 998 | if ((argv = makeargv(cp, &argc)) == NULL) |
| 999 | return -1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1000 | |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1001 | /* Figure out which command we have */ |
| 1002 | for (i = 0; cmds[i].c != NULL; i++) { |
| 1003 | if (strcasecmp(cmds[i].c, argv[0]) == 0) |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1004 | break; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1005 | } |
| 1006 | cmdnum = cmds[i].n; |
| 1007 | cmd = cmds[i].c; |
| 1008 | |
| 1009 | /* Special case */ |
| 1010 | if (*cp == '!') { |
| 1011 | cp++; |
| 1012 | cmdnum = I_SHELL; |
| 1013 | } else if (cmdnum == -1) { |
| 1014 | error("Invalid command."); |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1015 | return -1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | /* Get arguments and parse flags */ |
| 1019 | *lflag = *pflag = *n_arg = 0; |
| 1020 | *path1 = *path2 = NULL; |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1021 | optidx = 1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1022 | switch (cmdnum) { |
| 1023 | case I_GET: |
| 1024 | case I_PUT: |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1025 | if ((optidx = parse_getput_flags(cmd, argv, argc, pflag)) == -1) |
| 1026 | return -1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1027 | /* Get first pathname (mandatory) */ |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1028 | if (argc - optidx < 1) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1029 | error("You must specify at least one path after a " |
| 1030 | "%s command.", cmd); |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1031 | return -1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1032 | } |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1033 | *path1 = xstrdup(argv[optidx]); |
| 1034 | /* Get second pathname (optional) */ |
| 1035 | if (argc - optidx > 1) { |
| 1036 | *path2 = xstrdup(argv[optidx + 1]); |
| 1037 | /* Destination is not globbed */ |
| 1038 | undo_glob_escape(*path2); |
| 1039 | } |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1040 | break; |
| 1041 | case I_RENAME: |
| 1042 | case I_SYMLINK: |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1043 | if (argc - optidx < 2) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1044 | error("You must specify two paths after a %s " |
| 1045 | "command.", cmd); |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1046 | return -1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1047 | } |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1048 | *path1 = xstrdup(argv[optidx]); |
| 1049 | *path2 = xstrdup(argv[optidx + 1]); |
| 1050 | /* Paths are not globbed */ |
| 1051 | undo_glob_escape(*path1); |
| 1052 | undo_glob_escape(*path2); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1053 | break; |
| 1054 | case I_RM: |
| 1055 | case I_MKDIR: |
| 1056 | case I_RMDIR: |
| 1057 | case I_CHDIR: |
| 1058 | case I_LCHDIR: |
| 1059 | case I_LMKDIR: |
| 1060 | /* Get pathname (mandatory) */ |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1061 | if (argc - optidx < 1) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1062 | error("You must specify a path after a %s command.", |
| 1063 | cmd); |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1064 | return -1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1065 | } |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1066 | *path1 = xstrdup(argv[optidx]); |
| 1067 | /* Only "rm" globs */ |
| 1068 | if (cmdnum != I_RM) |
| 1069 | undo_glob_escape(*path1); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1070 | break; |
| 1071 | case I_LS: |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1072 | if ((optidx = parse_ls_flags(argv, argc, lflag)) == -1) |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1073 | return(-1); |
| 1074 | /* Path is optional */ |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1075 | if (argc - optidx > 0) |
| 1076 | *path1 = xstrdup(argv[optidx]); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1077 | break; |
| 1078 | case I_LLS: |
Darren Tucker | 88b976f | 2007-12-29 02:40:43 +1100 | [diff] [blame] | 1079 | /* Skip ls command and following whitespace */ |
| 1080 | cp = cp + strlen(cmd) + strspn(cp, WHITESPACE); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1081 | case I_SHELL: |
| 1082 | /* Uses the rest of the line */ |
| 1083 | break; |
| 1084 | case I_LUMASK: |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1085 | case I_CHMOD: |
| 1086 | base = 8; |
| 1087 | case I_CHOWN: |
| 1088 | case I_CHGRP: |
| 1089 | /* Get numeric arg (mandatory) */ |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1090 | if (argc - optidx < 1) |
| 1091 | goto need_num_arg; |
Damien Miller | e7658a5 | 2006-10-24 03:00:12 +1000 | [diff] [blame] | 1092 | errno = 0; |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1093 | l = strtol(argv[optidx], &cp2, base); |
| 1094 | if (cp2 == argv[optidx] || *cp2 != '\0' || |
| 1095 | ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) || |
| 1096 | l < 0) { |
| 1097 | need_num_arg: |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1098 | error("You must supply a numeric argument " |
| 1099 | "to the %s command.", cmd); |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1100 | return -1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1101 | } |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1102 | *n_arg = l; |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1103 | if (cmdnum == I_LUMASK) |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1104 | break; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1105 | /* Get pathname (mandatory) */ |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1106 | if (argc - optidx < 2) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1107 | error("You must specify a path after a %s command.", |
| 1108 | cmd); |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1109 | return -1; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1110 | } |
Damien Miller | 1cbc292 | 2007-10-26 14:27:45 +1000 | [diff] [blame] | 1111 | *path1 = xstrdup(argv[optidx + 1]); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1112 | break; |
| 1113 | case I_QUIT: |
| 1114 | case I_PWD: |
| 1115 | case I_LPWD: |
| 1116 | case I_HELP: |
| 1117 | case I_VERSION: |
| 1118 | case I_PROGRESS: |
| 1119 | break; |
| 1120 | default: |
| 1121 | fatal("Command not implemented"); |
| 1122 | } |
| 1123 | |
| 1124 | *cpp = cp; |
| 1125 | return(cmdnum); |
| 1126 | } |
| 1127 | |
| 1128 | static int |
| 1129 | parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, |
| 1130 | int err_abort) |
| 1131 | { |
| 1132 | char *path1, *path2, *tmp; |
| 1133 | int pflag, lflag, iflag, cmdnum, i; |
| 1134 | unsigned long n_arg; |
| 1135 | Attrib a, *aa; |
| 1136 | char path_buf[MAXPATHLEN]; |
| 1137 | int err = 0; |
| 1138 | glob_t g; |
| 1139 | |
| 1140 | path1 = path2 = NULL; |
| 1141 | cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg, |
| 1142 | &path1, &path2); |
| 1143 | |
| 1144 | if (iflag != 0) |
| 1145 | err_abort = 0; |
| 1146 | |
| 1147 | memset(&g, 0, sizeof(g)); |
| 1148 | |
| 1149 | /* Perform command */ |
| 1150 | switch (cmdnum) { |
| 1151 | case 0: |
| 1152 | /* Blank line */ |
| 1153 | break; |
| 1154 | case -1: |
| 1155 | /* Unrecognized command */ |
| 1156 | err = -1; |
| 1157 | break; |
| 1158 | case I_GET: |
| 1159 | err = process_get(conn, path1, path2, *pwd, pflag); |
| 1160 | break; |
| 1161 | case I_PUT: |
| 1162 | err = process_put(conn, path1, path2, *pwd, pflag); |
| 1163 | break; |
| 1164 | case I_RENAME: |
| 1165 | path1 = make_absolute(path1, *pwd); |
| 1166 | path2 = make_absolute(path2, *pwd); |
| 1167 | err = do_rename(conn, path1, path2); |
| 1168 | break; |
| 1169 | case I_SYMLINK: |
| 1170 | path2 = make_absolute(path2, *pwd); |
| 1171 | err = do_symlink(conn, path1, path2); |
| 1172 | break; |
| 1173 | case I_RM: |
| 1174 | path1 = make_absolute(path1, *pwd); |
| 1175 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1176 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1177 | printf("Removing %s\n", g.gl_pathv[i]); |
| 1178 | err = do_rm(conn, g.gl_pathv[i]); |
| 1179 | if (err != 0 && err_abort) |
| 1180 | break; |
| 1181 | } |
| 1182 | break; |
| 1183 | case I_MKDIR: |
| 1184 | path1 = make_absolute(path1, *pwd); |
| 1185 | attrib_clear(&a); |
| 1186 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 1187 | a.perm = 0777; |
| 1188 | err = do_mkdir(conn, path1, &a); |
| 1189 | break; |
| 1190 | case I_RMDIR: |
| 1191 | path1 = make_absolute(path1, *pwd); |
| 1192 | err = do_rmdir(conn, path1); |
| 1193 | break; |
| 1194 | case I_CHDIR: |
| 1195 | path1 = make_absolute(path1, *pwd); |
| 1196 | if ((tmp = do_realpath(conn, path1)) == NULL) { |
| 1197 | err = 1; |
| 1198 | break; |
| 1199 | } |
| 1200 | if ((aa = do_stat(conn, tmp, 0)) == NULL) { |
| 1201 | xfree(tmp); |
| 1202 | err = 1; |
| 1203 | break; |
| 1204 | } |
| 1205 | if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { |
| 1206 | error("Can't change directory: Can't check target"); |
| 1207 | xfree(tmp); |
| 1208 | err = 1; |
| 1209 | break; |
| 1210 | } |
| 1211 | if (!S_ISDIR(aa->perm)) { |
| 1212 | error("Can't change directory: \"%s\" is not " |
| 1213 | "a directory", tmp); |
| 1214 | xfree(tmp); |
| 1215 | err = 1; |
| 1216 | break; |
| 1217 | } |
| 1218 | xfree(*pwd); |
| 1219 | *pwd = tmp; |
| 1220 | break; |
| 1221 | case I_LS: |
| 1222 | if (!path1) { |
| 1223 | do_globbed_ls(conn, *pwd, *pwd, lflag); |
| 1224 | break; |
| 1225 | } |
| 1226 | |
| 1227 | /* Strip pwd off beginning of non-absolute paths */ |
| 1228 | tmp = NULL; |
| 1229 | if (*path1 != '/') |
| 1230 | tmp = *pwd; |
| 1231 | |
| 1232 | path1 = make_absolute(path1, *pwd); |
| 1233 | err = do_globbed_ls(conn, path1, tmp, lflag); |
| 1234 | break; |
| 1235 | case I_LCHDIR: |
| 1236 | if (chdir(path1) == -1) { |
| 1237 | error("Couldn't change local directory to " |
| 1238 | "\"%s\": %s", path1, strerror(errno)); |
| 1239 | err = 1; |
| 1240 | } |
| 1241 | break; |
| 1242 | case I_LMKDIR: |
| 1243 | if (mkdir(path1, 0777) == -1) { |
| 1244 | error("Couldn't create local directory " |
| 1245 | "\"%s\": %s", path1, strerror(errno)); |
| 1246 | err = 1; |
| 1247 | } |
| 1248 | break; |
| 1249 | case I_LLS: |
| 1250 | local_do_ls(cmd); |
| 1251 | break; |
| 1252 | case I_SHELL: |
| 1253 | local_do_shell(cmd); |
| 1254 | break; |
| 1255 | case I_LUMASK: |
| 1256 | umask(n_arg); |
| 1257 | printf("Local umask: %03lo\n", n_arg); |
| 1258 | break; |
| 1259 | case I_CHMOD: |
| 1260 | path1 = make_absolute(path1, *pwd); |
| 1261 | attrib_clear(&a); |
| 1262 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 1263 | a.perm = n_arg; |
| 1264 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1265 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1266 | printf("Changing mode on %s\n", g.gl_pathv[i]); |
| 1267 | err = do_setstat(conn, g.gl_pathv[i], &a); |
| 1268 | if (err != 0 && err_abort) |
| 1269 | break; |
| 1270 | } |
| 1271 | break; |
| 1272 | case I_CHOWN: |
| 1273 | case I_CHGRP: |
| 1274 | path1 = make_absolute(path1, *pwd); |
| 1275 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1276 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1277 | if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) { |
| 1278 | if (err != 0 && err_abort) |
| 1279 | break; |
| 1280 | else |
| 1281 | continue; |
| 1282 | } |
| 1283 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { |
| 1284 | error("Can't get current ownership of " |
| 1285 | "remote file \"%s\"", g.gl_pathv[i]); |
| 1286 | if (err != 0 && err_abort) |
| 1287 | break; |
| 1288 | else |
| 1289 | continue; |
| 1290 | } |
| 1291 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; |
| 1292 | if (cmdnum == I_CHOWN) { |
| 1293 | printf("Changing owner on %s\n", g.gl_pathv[i]); |
| 1294 | aa->uid = n_arg; |
| 1295 | } else { |
| 1296 | printf("Changing group on %s\n", g.gl_pathv[i]); |
| 1297 | aa->gid = n_arg; |
| 1298 | } |
| 1299 | err = do_setstat(conn, g.gl_pathv[i], aa); |
| 1300 | if (err != 0 && err_abort) |
| 1301 | break; |
| 1302 | } |
| 1303 | break; |
| 1304 | case I_PWD: |
| 1305 | printf("Remote working directory: %s\n", *pwd); |
| 1306 | break; |
| 1307 | case I_LPWD: |
| 1308 | if (!getcwd(path_buf, sizeof(path_buf))) { |
| 1309 | error("Couldn't get local cwd: %s", strerror(errno)); |
| 1310 | err = -1; |
| 1311 | break; |
| 1312 | } |
| 1313 | printf("Local working directory: %s\n", path_buf); |
| 1314 | break; |
| 1315 | case I_QUIT: |
| 1316 | /* Processed below */ |
| 1317 | break; |
| 1318 | case I_HELP: |
| 1319 | help(); |
| 1320 | break; |
| 1321 | case I_VERSION: |
| 1322 | printf("SFTP protocol version %u\n", sftp_proto_version(conn)); |
| 1323 | break; |
| 1324 | case I_PROGRESS: |
| 1325 | showprogress = !showprogress; |
| 1326 | if (showprogress) |
| 1327 | printf("Progress meter enabled\n"); |
| 1328 | else |
| 1329 | printf("Progress meter disabled\n"); |
| 1330 | break; |
| 1331 | default: |
| 1332 | fatal("%d is not implemented", cmdnum); |
| 1333 | } |
| 1334 | |
| 1335 | if (g.gl_pathc) |
| 1336 | globfree(&g); |
| 1337 | if (path1) |
| 1338 | xfree(path1); |
| 1339 | if (path2) |
| 1340 | xfree(path2); |
| 1341 | |
| 1342 | /* If an unignored error occurs in batch mode we should abort. */ |
| 1343 | if (err_abort && err != 0) |
| 1344 | return (-1); |
| 1345 | else if (cmdnum == I_QUIT) |
| 1346 | return (1); |
| 1347 | |
| 1348 | return (0); |
| 1349 | } |
| 1350 | |
Darren Tucker | 2d963d8 | 2004-11-07 20:04:10 +1100 | [diff] [blame] | 1351 | #ifdef USE_LIBEDIT |
| 1352 | static char * |
| 1353 | prompt(EditLine *el) |
| 1354 | { |
| 1355 | return ("sftp> "); |
| 1356 | } |
| 1357 | #endif |
| 1358 | |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1359 | int |
| 1360 | interactive_loop(int fd_in, int fd_out, char *file1, char *file2) |
| 1361 | { |
| 1362 | char *pwd; |
| 1363 | char *dir = NULL; |
| 1364 | char cmd[2048]; |
| 1365 | struct sftp_conn *conn; |
Damien Miller | 0e2c102 | 2005-08-12 22:16:22 +1000 | [diff] [blame] | 1366 | int err, interactive; |
Darren Tucker | 2d963d8 | 2004-11-07 20:04:10 +1100 | [diff] [blame] | 1367 | EditLine *el = NULL; |
| 1368 | #ifdef USE_LIBEDIT |
| 1369 | History *hl = NULL; |
| 1370 | HistEvent hev; |
| 1371 | extern char *__progname; |
| 1372 | |
| 1373 | if (!batchmode && isatty(STDIN_FILENO)) { |
| 1374 | if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL) |
| 1375 | fatal("Couldn't initialise editline"); |
| 1376 | if ((hl = history_init()) == NULL) |
| 1377 | fatal("Couldn't initialise editline history"); |
| 1378 | history(hl, &hev, H_SETSIZE, 100); |
| 1379 | el_set(el, EL_HIST, history, hl); |
| 1380 | |
| 1381 | el_set(el, EL_PROMPT, prompt); |
| 1382 | el_set(el, EL_EDITOR, "emacs"); |
| 1383 | el_set(el, EL_TERMINAL, NULL); |
| 1384 | el_set(el, EL_SIGNAL, 1); |
| 1385 | el_source(el, NULL); |
| 1386 | } |
| 1387 | #endif /* USE_LIBEDIT */ |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1388 | |
| 1389 | conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests); |
| 1390 | if (conn == NULL) |
| 1391 | fatal("Couldn't initialise connection to server"); |
| 1392 | |
| 1393 | pwd = do_realpath(conn, "."); |
| 1394 | if (pwd == NULL) |
| 1395 | fatal("Need cwd"); |
| 1396 | |
| 1397 | if (file1 != NULL) { |
| 1398 | dir = xstrdup(file1); |
| 1399 | dir = make_absolute(dir, pwd); |
| 1400 | |
| 1401 | if (remote_is_dir(conn, dir) && file2 == NULL) { |
| 1402 | printf("Changing to: %s\n", dir); |
| 1403 | snprintf(cmd, sizeof cmd, "cd \"%s\"", dir); |
Darren Tucker | cd516ef | 2004-12-06 22:43:43 +1100 | [diff] [blame] | 1404 | if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) { |
| 1405 | xfree(dir); |
| 1406 | xfree(pwd); |
Damien Miller | e0b90a6 | 2006-03-26 13:51:44 +1100 | [diff] [blame] | 1407 | xfree(conn); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1408 | return (-1); |
Darren Tucker | cd516ef | 2004-12-06 22:43:43 +1100 | [diff] [blame] | 1409 | } |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1410 | } else { |
| 1411 | if (file2 == NULL) |
| 1412 | snprintf(cmd, sizeof cmd, "get %s", dir); |
| 1413 | else |
| 1414 | snprintf(cmd, sizeof cmd, "get %s %s", dir, |
| 1415 | file2); |
| 1416 | |
| 1417 | err = parse_dispatch_command(conn, cmd, &pwd, 1); |
| 1418 | xfree(dir); |
| 1419 | xfree(pwd); |
Damien Miller | e0b90a6 | 2006-03-26 13:51:44 +1100 | [diff] [blame] | 1420 | xfree(conn); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1421 | return (err); |
| 1422 | } |
| 1423 | xfree(dir); |
| 1424 | } |
| 1425 | |
Darren Tucker | 93e7e8f | 2005-08-23 08:06:55 +1000 | [diff] [blame] | 1426 | #if defined(HAVE_SETVBUF) && !defined(BROKEN_SETVBUF) |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1427 | setvbuf(stdout, NULL, _IOLBF, 0); |
| 1428 | setvbuf(infile, NULL, _IOLBF, 0); |
| 1429 | #else |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 1430 | setlinebuf(stdout); |
| 1431 | setlinebuf(infile); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1432 | #endif |
| 1433 | |
Damien Miller | 0e2c102 | 2005-08-12 22:16:22 +1000 | [diff] [blame] | 1434 | interactive = !batchmode && isatty(STDIN_FILENO); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1435 | err = 0; |
| 1436 | for (;;) { |
| 1437 | char *cp; |
| 1438 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1439 | signal(SIGINT, SIG_IGN); |
| 1440 | |
Darren Tucker | 2d963d8 | 2004-11-07 20:04:10 +1100 | [diff] [blame] | 1441 | if (el == NULL) { |
Damien Miller | 0e2c102 | 2005-08-12 22:16:22 +1000 | [diff] [blame] | 1442 | if (interactive) |
| 1443 | printf("sftp> "); |
Darren Tucker | 2d963d8 | 2004-11-07 20:04:10 +1100 | [diff] [blame] | 1444 | if (fgets(cmd, sizeof(cmd), infile) == NULL) { |
Damien Miller | 0e2c102 | 2005-08-12 22:16:22 +1000 | [diff] [blame] | 1445 | if (interactive) |
| 1446 | printf("\n"); |
Darren Tucker | 2d963d8 | 2004-11-07 20:04:10 +1100 | [diff] [blame] | 1447 | break; |
| 1448 | } |
Damien Miller | 0e2c102 | 2005-08-12 22:16:22 +1000 | [diff] [blame] | 1449 | if (!interactive) { /* Echo command */ |
| 1450 | printf("sftp> %s", cmd); |
| 1451 | if (strlen(cmd) > 0 && |
| 1452 | cmd[strlen(cmd) - 1] != '\n') |
| 1453 | printf("\n"); |
| 1454 | } |
Darren Tucker | 2d963d8 | 2004-11-07 20:04:10 +1100 | [diff] [blame] | 1455 | } else { |
| 1456 | #ifdef USE_LIBEDIT |
| 1457 | const char *line; |
| 1458 | int count = 0; |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1459 | |
Damien Miller | 0e2c102 | 2005-08-12 22:16:22 +1000 | [diff] [blame] | 1460 | if ((line = el_gets(el, &count)) == NULL || count <= 0) { |
| 1461 | printf("\n"); |
| 1462 | break; |
| 1463 | } |
Darren Tucker | 2d963d8 | 2004-11-07 20:04:10 +1100 | [diff] [blame] | 1464 | history(hl, &hev, H_ENTER, line); |
| 1465 | if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) { |
| 1466 | fprintf(stderr, "Error: input line too long\n"); |
| 1467 | continue; |
| 1468 | } |
| 1469 | #endif /* USE_LIBEDIT */ |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1470 | } |
| 1471 | |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1472 | cp = strrchr(cmd, '\n'); |
| 1473 | if (cp) |
| 1474 | *cp = '\0'; |
| 1475 | |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1476 | /* Handle user interrupts gracefully during commands */ |
| 1477 | interrupted = 0; |
| 1478 | signal(SIGINT, cmd_interrupt); |
| 1479 | |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1480 | err = parse_dispatch_command(conn, cmd, &pwd, batchmode); |
| 1481 | if (err != 0) |
| 1482 | break; |
| 1483 | } |
| 1484 | xfree(pwd); |
Damien Miller | e0b90a6 | 2006-03-26 13:51:44 +1100 | [diff] [blame] | 1485 | xfree(conn); |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1486 | |
Tim Rice | 027e8b1 | 2005-08-15 14:52:50 -0700 | [diff] [blame] | 1487 | #ifdef USE_LIBEDIT |
Damien Miller | 0e2c102 | 2005-08-12 22:16:22 +1000 | [diff] [blame] | 1488 | if (el != NULL) |
| 1489 | el_end(el); |
Tim Rice | 027e8b1 | 2005-08-15 14:52:50 -0700 | [diff] [blame] | 1490 | #endif /* USE_LIBEDIT */ |
Damien Miller | 0e2c102 | 2005-08-12 22:16:22 +1000 | [diff] [blame] | 1491 | |
Damien Miller | 20e1fab | 2004-02-18 14:30:55 +1100 | [diff] [blame] | 1492 | /* err == 1 signifies normal "quit" exit */ |
| 1493 | return (err >= 0 ? 0 : -1); |
| 1494 | } |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1495 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1496 | static void |
Damien Miller | cc685c1 | 2003-06-04 22:51:38 +1000 | [diff] [blame] | 1497 | connect_to_server(char *path, char **args, int *in, int *out) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1498 | { |
| 1499 | int c_in, c_out; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1500 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1501 | #ifdef USE_PIPES |
| 1502 | int pin[2], pout[2]; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1503 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1504 | if ((pipe(pin) == -1) || (pipe(pout) == -1)) |
| 1505 | fatal("pipe: %s", strerror(errno)); |
| 1506 | *in = pin[0]; |
| 1507 | *out = pout[1]; |
| 1508 | c_in = pout[0]; |
| 1509 | c_out = pin[1]; |
| 1510 | #else /* USE_PIPES */ |
| 1511 | int inout[2]; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 1512 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1513 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1) |
| 1514 | fatal("socketpair: %s", strerror(errno)); |
| 1515 | *in = *out = inout[0]; |
| 1516 | c_in = c_out = inout[1]; |
| 1517 | #endif /* USE_PIPES */ |
| 1518 | |
Damien Miller | cc685c1 | 2003-06-04 22:51:38 +1000 | [diff] [blame] | 1519 | if ((sshpid = fork()) == -1) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1520 | fatal("fork: %s", strerror(errno)); |
Damien Miller | cc685c1 | 2003-06-04 22:51:38 +1000 | [diff] [blame] | 1521 | else if (sshpid == 0) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1522 | if ((dup2(c_in, STDIN_FILENO) == -1) || |
| 1523 | (dup2(c_out, STDOUT_FILENO) == -1)) { |
| 1524 | fprintf(stderr, "dup2: %s\n", strerror(errno)); |
Damien Miller | 350327c | 2004-06-15 10:24:13 +1000 | [diff] [blame] | 1525 | _exit(1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1526 | } |
| 1527 | close(*in); |
| 1528 | close(*out); |
| 1529 | close(c_in); |
| 1530 | close(c_out); |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1531 | |
| 1532 | /* |
| 1533 | * The underlying ssh is in the same process group, so we must |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1534 | * ignore SIGINT if we want to gracefully abort commands, |
| 1535 | * otherwise the signal will make it to the ssh process and |
Darren Tucker | cdf547a | 2004-05-24 10:12:19 +1000 | [diff] [blame] | 1536 | * kill it too |
| 1537 | */ |
| 1538 | signal(SIGINT, SIG_IGN); |
Darren Tucker | bd12f17 | 2004-06-18 16:23:43 +1000 | [diff] [blame] | 1539 | execvp(path, args); |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1540 | fprintf(stderr, "exec: %s: %s\n", path, strerror(errno)); |
Damien Miller | 350327c | 2004-06-15 10:24:13 +1000 | [diff] [blame] | 1541 | _exit(1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1542 | } |
| 1543 | |
Damien Miller | cc685c1 | 2003-06-04 22:51:38 +1000 | [diff] [blame] | 1544 | signal(SIGTERM, killchild); |
| 1545 | signal(SIGINT, killchild); |
| 1546 | signal(SIGHUP, killchild); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1547 | close(c_in); |
| 1548 | close(c_out); |
| 1549 | } |
| 1550 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1551 | static void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1552 | usage(void) |
| 1553 | { |
Damien Miller | 025e01c | 2002-02-08 22:06:29 +1100 | [diff] [blame] | 1554 | extern char *__progname; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1555 | |
Ben Lindstrom | 1e24324 | 2001-09-18 05:38:44 +0000 | [diff] [blame] | 1556 | fprintf(stderr, |
Darren Tucker | 1f20394 | 2003-10-15 15:50:42 +1000 | [diff] [blame] | 1557 | "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n" |
| 1558 | " [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n" |
| 1559 | " [-S program] [-s subsystem | sftp_server] host\n" |
| 1560 | " %s [[user@]host[:file [file]]]\n" |
| 1561 | " %s [[user@]host[:dir[/]]]\n" |
| 1562 | " %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1563 | exit(1); |
| 1564 | } |
| 1565 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1566 | int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1567 | main(int argc, char **argv) |
| 1568 | { |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1569 | int in, out, ch, err; |
Damien Miller | 7cf17eb | 2004-06-15 10:28:56 +1000 | [diff] [blame] | 1570 | char *host, *userhost, *cp, *file2 = NULL; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 1571 | int debug_level = 0, sshver = 2; |
| 1572 | char *file1 = NULL, *sftp_server = NULL; |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1573 | char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL; |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 1574 | LogLevel ll = SYSLOG_LEVEL_INFO; |
| 1575 | arglist args; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1576 | extern int optind; |
| 1577 | extern char *optarg; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1578 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1579 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 1580 | sanitise_stdfd(); |
| 1581 | |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 1582 | __progname = ssh_get_progname(argv[0]); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 1583 | memset(&args, '\0', sizeof(args)); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 1584 | args.list = NULL; |
Damien Miller | 2b5a0de | 2006-03-31 23:10:31 +1100 | [diff] [blame] | 1585 | addargs(&args, "%s", ssh_program); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 1586 | addargs(&args, "-oForwardX11 no"); |
| 1587 | addargs(&args, "-oForwardAgent no"); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1588 | addargs(&args, "-oPermitLocalCommand no"); |
Ben Lindstrom | 2b7a0e9 | 2001-09-20 00:57:55 +0000 | [diff] [blame] | 1589 | addargs(&args, "-oClearAllForwardings yes"); |
Damien Miller | e4f5a82 | 2004-01-21 14:11:05 +1100 | [diff] [blame] | 1590 | |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 1591 | ll = SYSLOG_LEVEL_INFO; |
Damien Miller | e4f5a82 | 2004-01-21 14:11:05 +1100 | [diff] [blame] | 1592 | infile = stdin; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1593 | |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1594 | while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1595 | switch (ch) { |
| 1596 | case 'C': |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 1597 | addargs(&args, "-C"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1598 | break; |
| 1599 | case 'v': |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 1600 | if (debug_level < 3) { |
| 1601 | addargs(&args, "-v"); |
| 1602 | ll = SYSLOG_LEVEL_DEBUG1 + debug_level; |
| 1603 | } |
| 1604 | debug_level++; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1605 | break; |
Ben Lindstrom | 1e24324 | 2001-09-18 05:38:44 +0000 | [diff] [blame] | 1606 | case 'F': |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1607 | case 'o': |
Ben Lindstrom | 1e24324 | 2001-09-18 05:38:44 +0000 | [diff] [blame] | 1608 | addargs(&args, "-%c%s", ch, optarg); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1609 | break; |
| 1610 | case '1': |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 1611 | sshver = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1612 | if (sftp_server == NULL) |
| 1613 | sftp_server = _PATH_SFTP_SERVER; |
| 1614 | break; |
| 1615 | case 's': |
| 1616 | sftp_server = optarg; |
| 1617 | break; |
| 1618 | case 'S': |
| 1619 | ssh_program = optarg; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 1620 | replacearg(&args, 0, "%s", ssh_program); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1621 | break; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1622 | case 'b': |
Damien Miller | 44f75c1 | 2004-01-21 10:58:47 +1100 | [diff] [blame] | 1623 | if (batchmode) |
| 1624 | fatal("Batch file already specified."); |
| 1625 | |
| 1626 | /* Allow "-" as stdin */ |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1627 | if (strcmp(optarg, "-") != 0 && |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 1628 | (infile = fopen(optarg, "r")) == NULL) |
Damien Miller | 44f75c1 | 2004-01-21 10:58:47 +1100 | [diff] [blame] | 1629 | fatal("%s (%s).", strerror(errno), optarg); |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1630 | showprogress = 0; |
Damien Miller | 44f75c1 | 2004-01-21 10:58:47 +1100 | [diff] [blame] | 1631 | batchmode = 1; |
Damien Miller | 64e8d44 | 2005-03-01 21:16:47 +1100 | [diff] [blame] | 1632 | addargs(&args, "-obatchmode yes"); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1633 | break; |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1634 | case 'P': |
| 1635 | sftp_direct = optarg; |
| 1636 | break; |
Damien Miller | 8829d36 | 2002-02-08 22:04:05 +1100 | [diff] [blame] | 1637 | case 'B': |
| 1638 | copy_buffer_len = strtol(optarg, &cp, 10); |
| 1639 | if (copy_buffer_len == 0 || *cp != '\0') |
| 1640 | fatal("Invalid buffer size \"%s\"", optarg); |
| 1641 | break; |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1642 | case 'R': |
| 1643 | num_requests = strtol(optarg, &cp, 10); |
| 1644 | if (num_requests == 0 || *cp != '\0') |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1645 | fatal("Invalid number of requests \"%s\"", |
Damien Miller | 16a1333 | 2002-02-13 14:03:56 +1100 | [diff] [blame] | 1646 | optarg); |
| 1647 | break; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 1648 | case 'h': |
| 1649 | default: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1650 | usage(); |
| 1651 | } |
| 1652 | } |
| 1653 | |
Damien Miller | c0f27d8 | 2004-03-08 23:12:19 +1100 | [diff] [blame] | 1654 | if (!isatty(STDERR_FILENO)) |
| 1655 | showprogress = 0; |
| 1656 | |
Ben Lindstrom | 2f3d52a | 2002-04-02 21:06:18 +0000 | [diff] [blame] | 1657 | log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); |
| 1658 | |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1659 | if (sftp_direct == NULL) { |
| 1660 | if (optind == argc || argc > (optind + 2)) |
| 1661 | usage(); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1662 | |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1663 | userhost = xstrdup(argv[optind]); |
| 1664 | file2 = argv[optind+1]; |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 1665 | |
Ben Lindstrom | c276c12 | 2002-12-23 02:14:51 +0000 | [diff] [blame] | 1666 | if ((host = strrchr(userhost, '@')) == NULL) |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1667 | host = userhost; |
| 1668 | else { |
| 1669 | *host++ = '\0'; |
| 1670 | if (!userhost[0]) { |
| 1671 | fprintf(stderr, "Missing username\n"); |
| 1672 | usage(); |
| 1673 | } |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 1674 | addargs(&args, "-l%s", userhost); |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1675 | } |
| 1676 | |
Damien Miller | ec69203 | 2004-01-27 21:22:00 +1100 | [diff] [blame] | 1677 | if ((cp = colon(host)) != NULL) { |
| 1678 | *cp++ = '\0'; |
| 1679 | file1 = cp; |
| 1680 | } |
| 1681 | |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1682 | host = cleanhostname(host); |
| 1683 | if (!*host) { |
| 1684 | fprintf(stderr, "Missing hostname\n"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1685 | usage(); |
| 1686 | } |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1687 | |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1688 | addargs(&args, "-oProtocol %d", sshver); |
| 1689 | |
| 1690 | /* no subsystem if the server-spec contains a '/' */ |
| 1691 | if (sftp_server == NULL || strchr(sftp_server, '/') == NULL) |
| 1692 | addargs(&args, "-s"); |
| 1693 | |
| 1694 | addargs(&args, "%s", host); |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 1695 | addargs(&args, "%s", (sftp_server != NULL ? |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1696 | sftp_server : "sftp")); |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1697 | |
Damien Miller | 44f75c1 | 2004-01-21 10:58:47 +1100 | [diff] [blame] | 1698 | if (!batchmode) |
| 1699 | fprintf(stderr, "Connecting to %s...\n", host); |
Damien Miller | cc685c1 | 2003-06-04 22:51:38 +1000 | [diff] [blame] | 1700 | connect_to_server(ssh_program, args.list, &in, &out); |
Damien Miller | d14ee1e | 2002-02-05 12:27:31 +1100 | [diff] [blame] | 1701 | } else { |
| 1702 | args.list = NULL; |
| 1703 | addargs(&args, "sftp-server"); |
| 1704 | |
Damien Miller | 44f75c1 | 2004-01-21 10:58:47 +1100 | [diff] [blame] | 1705 | if (!batchmode) |
| 1706 | fprintf(stderr, "Attaching to %s...\n", sftp_direct); |
Damien Miller | cc685c1 | 2003-06-04 22:51:38 +1000 | [diff] [blame] | 1707 | connect_to_server(sftp_direct, args.list, &in, &out); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1708 | } |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 1709 | freeargs(&args); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1710 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1711 | err = interactive_loop(in, out, file1, file2); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1712 | |
Ben Lindstrom | 10b9bf9 | 2001-02-26 20:04:45 +0000 | [diff] [blame] | 1713 | #if !defined(USE_PIPES) |
Damien Miller | 37294fb | 2005-07-17 17:18:49 +1000 | [diff] [blame] | 1714 | shutdown(in, SHUT_RDWR); |
| 1715 | shutdown(out, SHUT_RDWR); |
Ben Lindstrom | 10b9bf9 | 2001-02-26 20:04:45 +0000 | [diff] [blame] | 1716 | #endif |
| 1717 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1718 | close(in); |
| 1719 | close(out); |
Damien Miller | 44f75c1 | 2004-01-21 10:58:47 +1100 | [diff] [blame] | 1720 | if (batchmode) |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 1721 | fclose(infile); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1722 | |
Ben Lindstrom | 47fd811 | 2002-04-02 20:48:19 +0000 | [diff] [blame] | 1723 | while (waitpid(sshpid, NULL, 0) == -1) |
| 1724 | if (errno != EINTR) |
| 1725 | fatal("Couldn't wait for ssh process: %s", |
| 1726 | strerror(errno)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1727 | |
Damien Miller | 956f3fb | 2003-01-10 21:40:00 +1100 | [diff] [blame] | 1728 | exit(err == 0 ? 0 : 1); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1729 | } |