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