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