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