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