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