Damien Miller | bc35d92 | 2013-07-18 16:11:25 +1000 | [diff] [blame] | 1 | /* $OpenBSD: scp.c,v 1.178 2013/06/22 06:31:57 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 3 | * scp - secure remote copy. This is basically patched BSD rcp which |
| 4 | * uses ssh to do the data transfer (instead of using rcmd). |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 5 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 6 | * NOTE: This version should NOT be suid root. (This uses ssh to |
| 7 | * do the transfer and ssh has the necessary privileges.) |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 8 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 9 | * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 10 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 11 | * As far as I am concerned, the code I have written for this software |
| 12 | * can be used freely for any purpose. Any derived versions of this |
| 13 | * software must be clearly marked as such, and if the derived work is |
| 14 | * incompatible with the protocol description in the RFC file, it must be |
| 15 | * called by a name other than "ssh" or "Secure Shell". |
| 16 | */ |
| 17 | /* |
Ben Lindstrom | 92a2e38 | 2001-03-05 06:59:27 +0000 | [diff] [blame] | 18 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
| 19 | * Copyright (c) 1999 Aaron Campbell. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 20 | * |
| 21 | * Redistribution and use in source and binary forms, with or without |
| 22 | * modification, are permitted provided that the following conditions |
| 23 | * are met: |
| 24 | * 1. Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * 2. Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in the |
| 28 | * documentation and/or other materials provided with the distribution. |
| 29 | * |
| 30 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 31 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 32 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 33 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 34 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 35 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 36 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 37 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 38 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 39 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 40 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
| 42 | /* |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 43 | * Parts from: |
| 44 | * |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | * Copyright (c) 1983, 1990, 1992, 1993, 1995 |
| 46 | * The Regents of the University of California. All rights reserved. |
| 47 | * |
| 48 | * Redistribution and use in source and binary forms, with or without |
| 49 | * modification, are permitted provided that the following conditions |
| 50 | * are met: |
| 51 | * 1. Redistributions of source code must retain the above copyright |
| 52 | * notice, this list of conditions and the following disclaimer. |
| 53 | * 2. Redistributions in binary form must reproduce the above copyright |
| 54 | * notice, this list of conditions and the following disclaimer in the |
| 55 | * documentation and/or other materials provided with the distribution. |
Damien Miller | dafb12e | 2003-06-03 13:06:18 +1000 | [diff] [blame] | 56 | * 3. Neither the name of the University nor the names of its contributors |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | * may be used to endorse or promote products derived from this software |
| 58 | * without specific prior written permission. |
| 59 | * |
| 60 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 61 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 62 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 63 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 64 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 65 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 66 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 67 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 68 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 69 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 70 | * SUCH DAMAGE. |
| 71 | * |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | */ |
| 73 | |
| 74 | #include "includes.h" |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 75 | |
| 76 | #include <sys/types.h> |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 77 | #include <sys/param.h> |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 78 | #ifdef HAVE_SYS_STAT_H |
| 79 | # include <sys/stat.h> |
| 80 | #endif |
Tim Rice | 3eb1449 | 2008-03-13 17:59:50 -0700 | [diff] [blame] | 81 | #ifdef HAVE_POLL_H |
| 82 | #include <poll.h> |
| 83 | #else |
| 84 | # ifdef HAVE_SYS_POLL_H |
| 85 | # include <sys/poll.h> |
| 86 | # endif |
Darren Tucker | ccb13ee | 2008-02-29 15:07:01 +1100 | [diff] [blame] | 87 | #endif |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 88 | #ifdef HAVE_SYS_TIME_H |
| 89 | # include <sys/time.h> |
| 90 | #endif |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 91 | #include <sys/wait.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 92 | #include <sys/uio.h> |
Damien Miller | 88f254b | 2006-03-15 11:25:13 +1100 | [diff] [blame] | 93 | |
Damien Miller | c7b0636 | 2006-03-15 11:53:45 +1100 | [diff] [blame] | 94 | #include <ctype.h> |
Damien Miller | 88f254b | 2006-03-15 11:25:13 +1100 | [diff] [blame] | 95 | #include <dirent.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 96 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 97 | #include <fcntl.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 98 | #include <pwd.h> |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 99 | #include <signal.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 100 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 101 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 102 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 103 | #include <string.h> |
Damien Miller | 5598b4f | 2006-07-24 14:09:40 +1000 | [diff] [blame] | 104 | #include <time.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 105 | #include <unistd.h> |
Damien Miller | 63b4bcd | 2013-03-20 12:55:14 +1100 | [diff] [blame] | 106 | #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS) |
Darren Tucker | bed6311 | 2007-06-13 00:02:07 +1000 | [diff] [blame] | 107 | #include <vis.h> |
| 108 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 109 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 110 | #include "xmalloc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 111 | #include "atomicio.h" |
| 112 | #include "pathnames.h" |
| 113 | #include "log.h" |
Kevin Steves | 93c17d9 | 2001-02-18 03:55:16 +0000 | [diff] [blame] | 114 | #include "misc.h" |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 115 | #include "progressmeter.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 116 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 117 | extern char *__progname; |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 118 | |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 119 | #define COPY_BUFLEN 16384 |
| 120 | |
Damien Miller | 07aa132 | 2006-04-23 12:04:27 +1000 | [diff] [blame] | 121 | int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout); |
Damien Miller | f121143 | 2011-01-06 22:40:30 +1100 | [diff] [blame] | 122 | int do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout); |
Damien Miller | 07aa132 | 2006-04-23 12:04:27 +1000 | [diff] [blame] | 123 | |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 124 | /* Struct for addargs */ |
| 125 | arglist args; |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 126 | arglist remote_remote_args; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 127 | |
Damien Miller | ff74d74 | 2003-01-24 11:36:58 +1100 | [diff] [blame] | 128 | /* Bandwidth limit */ |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 129 | long long limit_kbps = 0; |
| 130 | struct bwlimit bwlimit; |
Damien Miller | ff74d74 | 2003-01-24 11:36:58 +1100 | [diff] [blame] | 131 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | /* Name of current file being transferred. */ |
| 133 | char *curfile; |
| 134 | |
| 135 | /* This is set to non-zero to enable verbose mode. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 136 | int verbose_mode = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 137 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 138 | /* This is set to zero if the progressmeter is not desired. */ |
| 139 | int showprogress = 1; |
| 140 | |
Damien Miller | f121143 | 2011-01-06 22:40:30 +1100 | [diff] [blame] | 141 | /* |
| 142 | * This is set to non-zero if remote-remote copy should be piped |
| 143 | * through this process. |
| 144 | */ |
| 145 | int throughlocal = 0; |
| 146 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 147 | /* This is the program to execute for the secured connection. ("ssh" or -S) */ |
Darren Tucker | e566230 | 2007-12-02 22:48:40 +1100 | [diff] [blame] | 148 | char *ssh_program = _PATH_SSH_PROGRAM; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 149 | |
Ben Lindstrom | 007eb91 | 2002-11-09 15:54:08 +0000 | [diff] [blame] | 150 | /* This is used to store the pid of ssh_program */ |
Damien Miller | b69aaa8 | 2003-06-04 22:51:24 +1000 | [diff] [blame] | 151 | pid_t do_cmd_pid = -1; |
| 152 | |
| 153 | static void |
| 154 | killchild(int signo) |
| 155 | { |
Darren Tucker | ba66df8 | 2005-01-24 21:57:40 +1100 | [diff] [blame] | 156 | if (do_cmd_pid > 1) { |
Darren Tucker | fc4f2dd | 2005-06-01 23:01:12 +1000 | [diff] [blame] | 157 | kill(do_cmd_pid, signo ? signo : SIGTERM); |
Darren Tucker | ba66df8 | 2005-01-24 21:57:40 +1100 | [diff] [blame] | 158 | waitpid(do_cmd_pid, NULL, 0); |
| 159 | } |
Damien Miller | b69aaa8 | 2003-06-04 22:51:24 +1000 | [diff] [blame] | 160 | |
Darren Tucker | fc4f2dd | 2005-06-01 23:01:12 +1000 | [diff] [blame] | 161 | if (signo) |
| 162 | _exit(1); |
| 163 | exit(1); |
Damien Miller | b69aaa8 | 2003-06-04 22:51:24 +1000 | [diff] [blame] | 164 | } |
Ben Lindstrom | 007eb91 | 2002-11-09 15:54:08 +0000 | [diff] [blame] | 165 | |
Damien Miller | 0979b40 | 2010-07-02 13:37:33 +1000 | [diff] [blame] | 166 | static void |
| 167 | suspchild(int signo) |
| 168 | { |
| 169 | int status; |
| 170 | |
| 171 | if (do_cmd_pid > 1) { |
| 172 | kill(do_cmd_pid, signo); |
| 173 | while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 && |
| 174 | errno == EINTR) |
| 175 | ; |
| 176 | kill(getpid(), SIGSTOP); |
| 177 | } |
| 178 | } |
| 179 | |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 180 | static int |
| 181 | do_local_cmd(arglist *a) |
| 182 | { |
| 183 | u_int i; |
| 184 | int status; |
| 185 | pid_t pid; |
| 186 | |
| 187 | if (a->num == 0) |
| 188 | fatal("do_local_cmd: no arguments"); |
| 189 | |
| 190 | if (verbose_mode) { |
| 191 | fprintf(stderr, "Executing:"); |
| 192 | for (i = 0; i < a->num; i++) |
| 193 | fprintf(stderr, " %s", a->list[i]); |
| 194 | fprintf(stderr, "\n"); |
| 195 | } |
| 196 | if ((pid = fork()) == -1) |
| 197 | fatal("do_local_cmd: fork: %s", strerror(errno)); |
| 198 | |
| 199 | if (pid == 0) { |
| 200 | execvp(a->list[0], a->list); |
| 201 | perror(a->list[0]); |
| 202 | exit(1); |
| 203 | } |
| 204 | |
| 205 | do_cmd_pid = pid; |
| 206 | signal(SIGTERM, killchild); |
| 207 | signal(SIGINT, killchild); |
| 208 | signal(SIGHUP, killchild); |
| 209 | |
| 210 | while (waitpid(pid, &status, 0) == -1) |
| 211 | if (errno != EINTR) |
| 212 | fatal("do_local_cmd: waitpid: %s", strerror(errno)); |
| 213 | |
| 214 | do_cmd_pid = -1; |
| 215 | |
| 216 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
| 217 | return (-1); |
| 218 | |
| 219 | return (0); |
| 220 | } |
| 221 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 222 | /* |
| 223 | * This function executes the given command as the specified user on the |
| 224 | * given host. This returns < 0 if execution fails, and >= 0 otherwise. This |
| 225 | * assigns the input and output file descriptors on success. |
| 226 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 227 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 228 | int |
Damien Miller | 07aa132 | 2006-04-23 12:04:27 +1000 | [diff] [blame] | 229 | do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 230 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 231 | int pin[2], pout[2], reserved[2]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 232 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 233 | if (verbose_mode) |
Ben Lindstrom | 2772a3f | 2001-08-06 21:17:12 +0000 | [diff] [blame] | 234 | fprintf(stderr, |
| 235 | "Executing: program %s host %s, user %s, command %s\n", |
| 236 | ssh_program, host, |
| 237 | remuser ? remuser : "(unspecified)", cmd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 238 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 239 | /* |
| 240 | * Reserve two descriptors so that the real pipes won't get |
| 241 | * descriptors 0 and 1 because that will screw up dup2 below. |
| 242 | */ |
Damien Miller | 40b5985 | 2006-06-13 13:00:25 +1000 | [diff] [blame] | 243 | if (pipe(reserved) < 0) |
| 244 | fatal("pipe: %s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 245 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 246 | /* Create a socket pair for communicating with ssh. */ |
| 247 | if (pipe(pin) < 0) |
| 248 | fatal("pipe: %s", strerror(errno)); |
| 249 | if (pipe(pout) < 0) |
| 250 | fatal("pipe: %s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 251 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 252 | /* Free the reserved descriptors. */ |
| 253 | close(reserved[0]); |
| 254 | close(reserved[1]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 255 | |
Damien Miller | 0979b40 | 2010-07-02 13:37:33 +1000 | [diff] [blame] | 256 | signal(SIGTSTP, suspchild); |
| 257 | signal(SIGTTIN, suspchild); |
| 258 | signal(SIGTTOU, suspchild); |
| 259 | |
Damien Miller | b2cdcb5 | 2003-06-18 20:26:34 +1000 | [diff] [blame] | 260 | /* Fork a child to execute the command on the remote host using ssh. */ |
Ben Lindstrom | 007eb91 | 2002-11-09 15:54:08 +0000 | [diff] [blame] | 261 | do_cmd_pid = fork(); |
| 262 | if (do_cmd_pid == 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 263 | /* Child. */ |
| 264 | close(pin[1]); |
| 265 | close(pout[0]); |
| 266 | dup2(pin[0], 0); |
| 267 | dup2(pout[1], 1); |
| 268 | close(pin[0]); |
| 269 | close(pout[1]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 270 | |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 271 | replacearg(&args, 0, "%s", ssh_program); |
Darren Tucker | b8c884a | 2010-01-08 18:53:43 +1100 | [diff] [blame] | 272 | if (remuser != NULL) { |
| 273 | addargs(&args, "-l"); |
| 274 | addargs(&args, "%s", remuser); |
| 275 | } |
| 276 | addargs(&args, "--"); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 277 | addargs(&args, "%s", host); |
| 278 | addargs(&args, "%s", cmd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 279 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 280 | execvp(ssh_program, args.list); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 281 | perror(ssh_program); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 282 | exit(1); |
Ben Lindstrom | 007eb91 | 2002-11-09 15:54:08 +0000 | [diff] [blame] | 283 | } else if (do_cmd_pid == -1) { |
| 284 | fatal("fork: %s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 285 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 286 | /* Parent. Close the other side, and return the local side. */ |
| 287 | close(pin[0]); |
| 288 | *fdout = pin[1]; |
| 289 | close(pout[1]); |
| 290 | *fdin = pout[0]; |
Damien Miller | b69aaa8 | 2003-06-04 22:51:24 +1000 | [diff] [blame] | 291 | signal(SIGTERM, killchild); |
| 292 | signal(SIGINT, killchild); |
| 293 | signal(SIGHUP, killchild); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 294 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 295 | } |
| 296 | |
Damien Miller | f121143 | 2011-01-06 22:40:30 +1100 | [diff] [blame] | 297 | /* |
| 298 | * This functions executes a command simlar to do_cmd(), but expects the |
| 299 | * input and output descriptors to be setup by a previous call to do_cmd(). |
| 300 | * This way the input and output of two commands can be connected. |
| 301 | */ |
| 302 | int |
| 303 | do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout) |
| 304 | { |
| 305 | pid_t pid; |
| 306 | int status; |
| 307 | |
| 308 | if (verbose_mode) |
| 309 | fprintf(stderr, |
| 310 | "Executing: 2nd program %s host %s, user %s, command %s\n", |
| 311 | ssh_program, host, |
| 312 | remuser ? remuser : "(unspecified)", cmd); |
| 313 | |
| 314 | /* Fork a child to execute the command on the remote host using ssh. */ |
| 315 | pid = fork(); |
| 316 | if (pid == 0) { |
| 317 | dup2(fdin, 0); |
| 318 | dup2(fdout, 1); |
| 319 | |
| 320 | replacearg(&args, 0, "%s", ssh_program); |
| 321 | if (remuser != NULL) { |
| 322 | addargs(&args, "-l"); |
| 323 | addargs(&args, "%s", remuser); |
| 324 | } |
| 325 | addargs(&args, "--"); |
| 326 | addargs(&args, "%s", host); |
| 327 | addargs(&args, "%s", cmd); |
| 328 | |
| 329 | execvp(ssh_program, args.list); |
| 330 | perror(ssh_program); |
| 331 | exit(1); |
| 332 | } else if (pid == -1) { |
| 333 | fatal("fork: %s", strerror(errno)); |
| 334 | } |
| 335 | while (waitpid(pid, &status, 0) == -1) |
| 336 | if (errno != EINTR) |
| 337 | fatal("do_cmd2: waitpid: %s", strerror(errno)); |
| 338 | return 0; |
| 339 | } |
| 340 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 341 | typedef struct { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 342 | size_t cnt; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 343 | char *buf; |
| 344 | } BUF; |
| 345 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 346 | BUF *allocbuf(BUF *, int, int); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 347 | void lostconn(int); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 348 | int okname(char *); |
| 349 | void run_err(const char *,...); |
| 350 | void verifydir(char *); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 351 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 352 | struct passwd *pwd; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 353 | uid_t userid; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 354 | int errs, remin, remout; |
| 355 | int pflag, iamremote, iamrecursive, targetshouldbedirectory; |
| 356 | |
| 357 | #define CMDNEEDS 64 |
| 358 | char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ |
| 359 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 360 | int response(void); |
| 361 | void rsource(char *, struct stat *); |
| 362 | void sink(int, char *[]); |
| 363 | void source(int, char *[]); |
| 364 | void tolocal(int, char *[]); |
| 365 | void toremote(char *, int, char *[]); |
| 366 | void usage(void); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 367 | |
| 368 | int |
Damien Miller | 65d1f57 | 2003-06-04 22:51:08 +1000 | [diff] [blame] | 369 | main(int argc, char **argv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 370 | { |
Damien Miller | 3d1a9f4 | 2006-07-10 22:19:53 +1000 | [diff] [blame] | 371 | int ch, fflag, tflag, status, n; |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 372 | char *targ, **newargv; |
| 373 | const char *errstr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 374 | extern char *optarg; |
| 375 | extern int optind; |
| 376 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 377 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 378 | sanitise_stdfd(); |
| 379 | |
Damien Miller | 3d1a9f4 | 2006-07-10 22:19:53 +1000 | [diff] [blame] | 380 | /* Copy argv, because we modify it */ |
| 381 | newargv = xcalloc(MAX(argc + 1, 1), sizeof(*newargv)); |
| 382 | for (n = 0; n < argc; n++) |
| 383 | newargv[n] = xstrdup(argv[n]); |
| 384 | argv = newargv; |
| 385 | |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 386 | __progname = ssh_get_progname(argv[0]); |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 387 | |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 388 | memset(&args, '\0', sizeof(args)); |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 389 | memset(&remote_remote_args, '\0', sizeof(remote_remote_args)); |
| 390 | args.list = remote_remote_args.list = NULL; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 391 | addargs(&args, "%s", ssh_program); |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 392 | addargs(&args, "-x"); |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 393 | addargs(&args, "-oForwardAgent=no"); |
| 394 | addargs(&args, "-oPermitLocalCommand=no"); |
| 395 | addargs(&args, "-oClearAllForwardings=yes"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 396 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 397 | fflag = tflag = 0; |
Damien Miller | f121143 | 2011-01-06 22:40:30 +1100 | [diff] [blame] | 398 | while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 399 | switch (ch) { |
| 400 | /* User-visible flags. */ |
Damien Miller | 8e12147 | 2003-01-24 11:37:38 +1100 | [diff] [blame] | 401 | case '1': |
| 402 | case '2': |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 403 | case '4': |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 404 | case '6': |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 405 | case 'C': |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 406 | addargs(&args, "-%c", ch); |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 407 | addargs(&remote_remote_args, "-%c", ch); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 408 | break; |
Damien Miller | f121143 | 2011-01-06 22:40:30 +1100 | [diff] [blame] | 409 | case '3': |
| 410 | throughlocal = 1; |
| 411 | break; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 412 | case 'o': |
| 413 | case 'c': |
| 414 | case 'i': |
Ben Lindstrom | 1e24324 | 2001-09-18 05:38:44 +0000 | [diff] [blame] | 415 | case 'F': |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 416 | addargs(&remote_remote_args, "-%c", ch); |
| 417 | addargs(&remote_remote_args, "%s", optarg); |
Darren Tucker | b8c884a | 2010-01-08 18:53:43 +1100 | [diff] [blame] | 418 | addargs(&args, "-%c", ch); |
| 419 | addargs(&args, "%s", optarg); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 420 | break; |
| 421 | case 'P': |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 422 | addargs(&remote_remote_args, "-p"); |
| 423 | addargs(&remote_remote_args, "%s", optarg); |
Darren Tucker | b8c884a | 2010-01-08 18:53:43 +1100 | [diff] [blame] | 424 | addargs(&args, "-p"); |
| 425 | addargs(&args, "%s", optarg); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 426 | break; |
| 427 | case 'B': |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 428 | addargs(&remote_remote_args, "-oBatchmode=yes"); |
| 429 | addargs(&args, "-oBatchmode=yes"); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 430 | break; |
Damien Miller | ff74d74 | 2003-01-24 11:36:58 +1100 | [diff] [blame] | 431 | case 'l': |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 432 | limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024, |
| 433 | &errstr); |
| 434 | if (errstr != NULL) |
Damien Miller | ff74d74 | 2003-01-24 11:36:58 +1100 | [diff] [blame] | 435 | usage(); |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 436 | limit_kbps *= 1024; /* kbps */ |
| 437 | bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN); |
Damien Miller | ff74d74 | 2003-01-24 11:36:58 +1100 | [diff] [blame] | 438 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 439 | case 'p': |
| 440 | pflag = 1; |
| 441 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 442 | case 'r': |
| 443 | iamrecursive = 1; |
| 444 | break; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 445 | case 'S': |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 446 | ssh_program = xstrdup(optarg); |
| 447 | break; |
| 448 | case 'v': |
Ben Lindstrom | 9cc9464 | 2001-06-09 01:15:11 +0000 | [diff] [blame] | 449 | addargs(&args, "-v"); |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 450 | addargs(&remote_remote_args, "-v"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 451 | verbose_mode = 1; |
| 452 | break; |
| 453 | case 'q': |
Damien Miller | c1f2792 | 2003-11-17 21:19:05 +1100 | [diff] [blame] | 454 | addargs(&args, "-q"); |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 455 | addargs(&remote_remote_args, "-q"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 456 | showprogress = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 457 | break; |
| 458 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 459 | /* Server options. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 460 | case 'd': |
| 461 | targetshouldbedirectory = 1; |
| 462 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 463 | case 'f': /* "from" */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 464 | iamremote = 1; |
| 465 | fflag = 1; |
| 466 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 467 | case 't': /* "to" */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 468 | iamremote = 1; |
| 469 | tflag = 1; |
Damien Miller | 402b331 | 2001-04-14 00:28:42 +1000 | [diff] [blame] | 470 | #ifdef HAVE_CYGWIN |
| 471 | setmode(0, O_BINARY); |
| 472 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 473 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 474 | default: |
| 475 | usage(); |
| 476 | } |
| 477 | argc -= optind; |
| 478 | argv += optind; |
| 479 | |
| 480 | if ((pwd = getpwuid(userid = getuid())) == NULL) |
Darren Tucker | 03a29ba | 2003-07-19 20:07:45 +1000 | [diff] [blame] | 481 | fatal("unknown user %u", (u_int) userid); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 482 | |
Darren Tucker | 6ec2fbe | 2007-02-19 22:14:11 +1100 | [diff] [blame] | 483 | if (!isatty(STDOUT_FILENO)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 484 | showprogress = 0; |
| 485 | |
| 486 | remin = STDIN_FILENO; |
| 487 | remout = STDOUT_FILENO; |
| 488 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 489 | if (fflag) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 490 | /* Follow "protocol", send data. */ |
| 491 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 492 | source(argc, argv); |
| 493 | exit(errs != 0); |
| 494 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 495 | if (tflag) { |
| 496 | /* Receive data. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 497 | sink(argc, argv); |
| 498 | exit(errs != 0); |
| 499 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 500 | if (argc < 2) |
| 501 | usage(); |
| 502 | if (argc > 2) |
| 503 | targetshouldbedirectory = 1; |
| 504 | |
| 505 | remin = remout = -1; |
Ben Lindstrom | 007eb91 | 2002-11-09 15:54:08 +0000 | [diff] [blame] | 506 | do_cmd_pid = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 507 | /* Command to be executed on remote system using "ssh". */ |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 508 | (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s", |
| 509 | verbose_mode ? " -v" : "", |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 510 | iamrecursive ? " -r" : "", pflag ? " -p" : "", |
| 511 | targetshouldbedirectory ? " -d" : ""); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 512 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 513 | (void) signal(SIGPIPE, lostconn); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 514 | |
| 515 | if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */ |
| 516 | toremote(targ, argc, argv); |
| 517 | else { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 518 | if (targetshouldbedirectory) |
| 519 | verifydir(argv[argc - 1]); |
Damien Miller | 923f1ce | 2006-01-31 22:11:37 +1100 | [diff] [blame] | 520 | tolocal(argc, argv); /* Dest is local host. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 521 | } |
Ben Lindstrom | 007eb91 | 2002-11-09 15:54:08 +0000 | [diff] [blame] | 522 | /* |
| 523 | * Finally check the exit status of the ssh process, if one was forked |
Damien Miller | b4acb47 | 2008-11-03 19:23:45 +1100 | [diff] [blame] | 524 | * and no error has occurred yet |
Ben Lindstrom | 007eb91 | 2002-11-09 15:54:08 +0000 | [diff] [blame] | 525 | */ |
| 526 | if (do_cmd_pid != -1 && errs == 0) { |
| 527 | if (remin != -1) |
| 528 | (void) close(remin); |
| 529 | if (remout != -1) |
| 530 | (void) close(remout); |
| 531 | if (waitpid(do_cmd_pid, &status, 0) == -1) |
| 532 | errs = 1; |
| 533 | else { |
| 534 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
| 535 | errs = 1; |
| 536 | } |
| 537 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 538 | exit(errs != 0); |
| 539 | } |
| 540 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 541 | /* Callback from atomicio6 to update progress meter and limit bandwidth */ |
| 542 | static int |
| 543 | scpio(void *_cnt, size_t s) |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 544 | { |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 545 | off_t *cnt = (off_t *)_cnt; |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 546 | |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 547 | *cnt += s; |
| 548 | if (limit_kbps > 0) |
| 549 | bandwidth_limit(&bwlimit, s); |
| 550 | return 0; |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 551 | } |
| 552 | |
Damien Miller | 044bd2a | 2013-07-18 16:09:25 +1000 | [diff] [blame] | 553 | static int |
| 554 | do_times(int fd, int verb, const struct stat *sb) |
| 555 | { |
| 556 | /* strlen(2^64) == 20; strlen(10^6) == 7 */ |
| 557 | char buf[(20 + 7 + 2) * 2 + 2]; |
| 558 | |
| 559 | (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n", |
| 560 | (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime), |
| 561 | (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime)); |
| 562 | if (verb) { |
| 563 | fprintf(stderr, "File mtime %lld atime %lld\n", |
| 564 | (long long)sb->st_mtime, (long long)sb->st_atime); |
| 565 | fprintf(stderr, "Sending file timestamps: %s", buf); |
| 566 | } |
| 567 | (void) atomicio(vwrite, fd, buf, strlen(buf)); |
| 568 | return (response()); |
| 569 | } |
| 570 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 571 | void |
Damien Miller | 65d1f57 | 2003-06-04 22:51:08 +1000 | [diff] [blame] | 572 | toremote(char *targ, int argc, char **argv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 573 | { |
Damien Miller | 3dae15c | 2005-04-03 10:16:11 +1000 | [diff] [blame] | 574 | char *bp, *host, *src, *suser, *thost, *tuser, *arg; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 575 | arglist alist; |
Damien Miller | 07aa132 | 2006-04-23 12:04:27 +1000 | [diff] [blame] | 576 | int i; |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 577 | u_int j; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 578 | |
| 579 | memset(&alist, '\0', sizeof(alist)); |
| 580 | alist.list = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 581 | |
| 582 | *targ++ = 0; |
| 583 | if (*targ == 0) |
| 584 | targ = "."; |
| 585 | |
Damien Miller | 4942de5 | 2005-04-03 10:16:39 +1000 | [diff] [blame] | 586 | arg = xstrdup(argv[argc - 1]); |
Damien Miller | 3dae15c | 2005-04-03 10:16:11 +1000 | [diff] [blame] | 587 | if ((thost = strrchr(arg, '@'))) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 588 | /* user@host */ |
| 589 | *thost++ = 0; |
Damien Miller | 3dae15c | 2005-04-03 10:16:11 +1000 | [diff] [blame] | 590 | tuser = arg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 591 | if (*tuser == '\0') |
| 592 | tuser = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 593 | } else { |
Damien Miller | 3dae15c | 2005-04-03 10:16:11 +1000 | [diff] [blame] | 594 | thost = arg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 595 | tuser = NULL; |
| 596 | } |
| 597 | |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 598 | if (tuser != NULL && !okname(tuser)) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 599 | free(arg); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 600 | return; |
| 601 | } |
| 602 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 603 | for (i = 0; i < argc - 1; i++) { |
| 604 | src = colon(argv[i]); |
Damien Miller | f121143 | 2011-01-06 22:40:30 +1100 | [diff] [blame] | 605 | if (src && throughlocal) { /* extended remote to remote */ |
| 606 | *src++ = 0; |
| 607 | if (*src == 0) |
| 608 | src = "."; |
| 609 | host = strrchr(argv[i], '@'); |
| 610 | if (host) { |
| 611 | *host++ = 0; |
| 612 | host = cleanhostname(host); |
| 613 | suser = argv[i]; |
| 614 | if (*suser == '\0') |
| 615 | suser = pwd->pw_name; |
| 616 | else if (!okname(suser)) |
| 617 | continue; |
| 618 | } else { |
| 619 | host = cleanhostname(argv[i]); |
| 620 | suser = NULL; |
| 621 | } |
Damien Miller | 0603d98 | 2011-09-22 21:38:00 +1000 | [diff] [blame] | 622 | xasprintf(&bp, "%s -f %s%s", cmd, |
| 623 | *src == '-' ? "-- " : "", src); |
Damien Miller | f121143 | 2011-01-06 22:40:30 +1100 | [diff] [blame] | 624 | if (do_cmd(host, suser, bp, &remin, &remout) < 0) |
| 625 | exit(1); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 626 | free(bp); |
Damien Miller | f121143 | 2011-01-06 22:40:30 +1100 | [diff] [blame] | 627 | host = cleanhostname(thost); |
Damien Miller | 0603d98 | 2011-09-22 21:38:00 +1000 | [diff] [blame] | 628 | xasprintf(&bp, "%s -t %s%s", cmd, |
| 629 | *targ == '-' ? "-- " : "", targ); |
Damien Miller | f121143 | 2011-01-06 22:40:30 +1100 | [diff] [blame] | 630 | if (do_cmd2(host, tuser, bp, remin, remout) < 0) |
| 631 | exit(1); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 632 | free(bp); |
Damien Miller | f121143 | 2011-01-06 22:40:30 +1100 | [diff] [blame] | 633 | (void) close(remin); |
| 634 | (void) close(remout); |
| 635 | remin = remout = -1; |
| 636 | } else if (src) { /* standard remote to remote */ |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 637 | freeargs(&alist); |
| 638 | addargs(&alist, "%s", ssh_program); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 639 | addargs(&alist, "-x"); |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 640 | addargs(&alist, "-oClearAllForwardings=yes"); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 641 | addargs(&alist, "-n"); |
Damien Miller | 87dc0a4 | 2010-12-01 12:03:19 +1100 | [diff] [blame] | 642 | for (j = 0; j < remote_remote_args.num; j++) { |
| 643 | addargs(&alist, "%s", |
| 644 | remote_remote_args.list[j]); |
| 645 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 646 | *src++ = 0; |
| 647 | if (*src == 0) |
| 648 | src = "."; |
Ben Lindstrom | c276c12 | 2002-12-23 02:14:51 +0000 | [diff] [blame] | 649 | host = strrchr(argv[i], '@'); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 650 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 651 | if (host) { |
| 652 | *host++ = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 653 | host = cleanhostname(host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 654 | suser = argv[i]; |
| 655 | if (*suser == '\0') |
| 656 | suser = pwd->pw_name; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 657 | else if (!okname(suser)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 658 | continue; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 659 | addargs(&alist, "-l"); |
| 660 | addargs(&alist, "%s", suser); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 661 | } else { |
| 662 | host = cleanhostname(argv[i]); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 663 | } |
Darren Tucker | b8c884a | 2010-01-08 18:53:43 +1100 | [diff] [blame] | 664 | addargs(&alist, "--"); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 665 | addargs(&alist, "%s", host); |
| 666 | addargs(&alist, "%s", cmd); |
| 667 | addargs(&alist, "%s", src); |
| 668 | addargs(&alist, "%s%s%s:%s", |
| 669 | tuser ? tuser : "", tuser ? "@" : "", |
| 670 | thost, targ); |
| 671 | if (do_local_cmd(&alist) != 0) |
Darren Tucker | 8654d16 | 2003-09-22 21:14:55 +1000 | [diff] [blame] | 672 | errs = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 673 | } else { /* local to remote */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 674 | if (remin == -1) { |
Damien Miller | 0603d98 | 2011-09-22 21:38:00 +1000 | [diff] [blame] | 675 | xasprintf(&bp, "%s -t %s%s", cmd, |
| 676 | *targ == '-' ? "-- " : "", targ); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 677 | host = cleanhostname(thost); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 678 | if (do_cmd(host, tuser, bp, &remin, |
Damien Miller | 07aa132 | 2006-04-23 12:04:27 +1000 | [diff] [blame] | 679 | &remout) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 680 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 681 | if (response() < 0) |
| 682 | exit(1); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 683 | free(bp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 684 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 685 | source(1, argv + i); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 686 | } |
| 687 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 688 | free(arg); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | void |
Damien Miller | 65d1f57 | 2003-06-04 22:51:08 +1000 | [diff] [blame] | 692 | tolocal(int argc, char **argv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 693 | { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 694 | char *bp, *host, *src, *suser; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 695 | arglist alist; |
Damien Miller | 07aa132 | 2006-04-23 12:04:27 +1000 | [diff] [blame] | 696 | int i; |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 697 | |
| 698 | memset(&alist, '\0', sizeof(alist)); |
| 699 | alist.list = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 700 | |
| 701 | for (i = 0; i < argc - 1; i++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 702 | if (!(src = colon(argv[i]))) { /* Local to local. */ |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 703 | freeargs(&alist); |
| 704 | addargs(&alist, "%s", _PATH_CP); |
| 705 | if (iamrecursive) |
| 706 | addargs(&alist, "-r"); |
| 707 | if (pflag) |
| 708 | addargs(&alist, "-p"); |
Darren Tucker | b8c884a | 2010-01-08 18:53:43 +1100 | [diff] [blame] | 709 | addargs(&alist, "--"); |
Damien Miller | 3eec6b7 | 2006-01-31 21:49:27 +1100 | [diff] [blame] | 710 | addargs(&alist, "%s", argv[i]); |
| 711 | addargs(&alist, "%s", argv[argc-1]); |
| 712 | if (do_local_cmd(&alist)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 713 | ++errs; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 714 | continue; |
| 715 | } |
| 716 | *src++ = 0; |
| 717 | if (*src == 0) |
| 718 | src = "."; |
Ben Lindstrom | c276c12 | 2002-12-23 02:14:51 +0000 | [diff] [blame] | 719 | if ((host = strrchr(argv[i], '@')) == NULL) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 720 | host = argv[i]; |
| 721 | suser = NULL; |
| 722 | } else { |
| 723 | *host++ = 0; |
| 724 | suser = argv[i]; |
| 725 | if (*suser == '\0') |
| 726 | suser = pwd->pw_name; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 727 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 728 | host = cleanhostname(host); |
Damien Miller | 0603d98 | 2011-09-22 21:38:00 +1000 | [diff] [blame] | 729 | xasprintf(&bp, "%s -f %s%s", |
| 730 | cmd, *src == '-' ? "-- " : "", src); |
Damien Miller | 07aa132 | 2006-04-23 12:04:27 +1000 | [diff] [blame] | 731 | if (do_cmd(host, suser, bp, &remin, &remout) < 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 732 | free(bp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 733 | ++errs; |
| 734 | continue; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 735 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 736 | free(bp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 737 | sink(1, argv + argc - 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 738 | (void) close(remin); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 739 | remin = remout = -1; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | void |
Damien Miller | 65d1f57 | 2003-06-04 22:51:08 +1000 | [diff] [blame] | 744 | source(int argc, char **argv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 745 | { |
| 746 | struct stat stb; |
| 747 | static BUF buffer; |
| 748 | BUF *bp; |
Darren Tucker | 03ccc9b | 2008-06-14 09:02:25 +1000 | [diff] [blame] | 749 | off_t i, statbytes; |
| 750 | size_t amt; |
Darren Tucker | 4085853 | 2005-08-02 17:07:07 +1000 | [diff] [blame] | 751 | int fd = -1, haderr, indx; |
Darren Tucker | bed6311 | 2007-06-13 00:02:07 +1000 | [diff] [blame] | 752 | char *last, *name, buf[2048], encname[MAXPATHLEN]; |
Ben Lindstrom | d47cf4d | 2001-04-07 01:14:38 +0000 | [diff] [blame] | 753 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 754 | |
| 755 | for (indx = 0; indx < argc; ++indx) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 756 | name = argv[indx]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 757 | statbytes = 0; |
Ben Lindstrom | d47cf4d | 2001-04-07 01:14:38 +0000 | [diff] [blame] | 758 | len = strlen(name); |
| 759 | while (len > 1 && name[len-1] == '/') |
| 760 | name[--len] = '\0'; |
Darren Tucker | 43ce902 | 2007-06-12 23:41:06 +1000 | [diff] [blame] | 761 | if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 762 | goto syserr; |
Darren Tucker | bed6311 | 2007-06-13 00:02:07 +1000 | [diff] [blame] | 763 | if (strchr(name, '\n') != NULL) { |
| 764 | strnvis(encname, name, sizeof(encname), VIS_NL); |
| 765 | name = encname; |
| 766 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 767 | if (fstat(fd, &stb) < 0) { |
| 768 | syserr: run_err("%s: %s", name, strerror(errno)); |
| 769 | goto next; |
| 770 | } |
Darren Tucker | 03ccc9b | 2008-06-14 09:02:25 +1000 | [diff] [blame] | 771 | if (stb.st_size < 0) { |
| 772 | run_err("%s: %s", name, "Negative file size"); |
| 773 | goto next; |
| 774 | } |
Darren Tucker | 43ce902 | 2007-06-12 23:41:06 +1000 | [diff] [blame] | 775 | unset_nonblock(fd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 776 | switch (stb.st_mode & S_IFMT) { |
| 777 | case S_IFREG: |
| 778 | break; |
| 779 | case S_IFDIR: |
| 780 | if (iamrecursive) { |
| 781 | rsource(name, &stb); |
| 782 | goto next; |
| 783 | } |
| 784 | /* FALLTHROUGH */ |
| 785 | default: |
| 786 | run_err("%s: not a regular file", name); |
| 787 | goto next; |
| 788 | } |
| 789 | if ((last = strrchr(name, '/')) == NULL) |
| 790 | last = name; |
| 791 | else |
| 792 | ++last; |
| 793 | curfile = last; |
| 794 | if (pflag) { |
Damien Miller | 044bd2a | 2013-07-18 16:09:25 +1000 | [diff] [blame] | 795 | if (do_times(remout, verbose_mode, &stb) < 0) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 796 | goto next; |
| 797 | } |
| 798 | #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) |
Ben Lindstrom | a4c5766 | 2001-03-17 00:10:20 +0000 | [diff] [blame] | 799 | snprintf(buf, sizeof buf, "C%04o %lld %s\n", |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 800 | (u_int) (stb.st_mode & FILEMODEMASK), |
Darren Tucker | e0be304 | 2005-11-25 14:44:55 +1100 | [diff] [blame] | 801 | (long long)stb.st_size, last); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 802 | if (verbose_mode) { |
| 803 | fprintf(stderr, "Sending file modes: %s", buf); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 804 | } |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 805 | (void) atomicio(vwrite, remout, buf, strlen(buf)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 806 | if (response() < 0) |
| 807 | goto next; |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 808 | if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) { |
Darren Tucker | 33f86bc | 2005-11-22 19:38:06 +1100 | [diff] [blame] | 809 | next: if (fd != -1) { |
| 810 | (void) close(fd); |
| 811 | fd = -1; |
| 812 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 813 | continue; |
| 814 | } |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 815 | if (showprogress) |
| 816 | start_progress_meter(curfile, stb.st_size, &statbytes); |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 817 | set_nonblock(remout); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 818 | for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { |
| 819 | amt = bp->cnt; |
Darren Tucker | 03ccc9b | 2008-06-14 09:02:25 +1000 | [diff] [blame] | 820 | if (i + (off_t)amt > stb.st_size) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 821 | amt = stb.st_size - i; |
| 822 | if (!haderr) { |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 823 | if (atomicio(read, fd, bp->buf, amt) != amt) |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 824 | haderr = errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 825 | } |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 826 | /* Keep writing after error to retain sync */ |
| 827 | if (haderr) { |
| 828 | (void)atomicio(vwrite, remout, bp->buf, amt); |
| 829 | continue; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 830 | } |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 831 | if (atomicio6(vwrite, remout, bp->buf, amt, scpio, |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 832 | &statbytes) != amt) |
| 833 | haderr = errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 834 | } |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 835 | unset_nonblock(remout); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 836 | if (showprogress) |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 837 | stop_progress_meter(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 838 | |
Darren Tucker | 33f86bc | 2005-11-22 19:38:06 +1100 | [diff] [blame] | 839 | if (fd != -1) { |
| 840 | if (close(fd) < 0 && !haderr) |
| 841 | haderr = errno; |
| 842 | fd = -1; |
| 843 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 844 | if (!haderr) |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 845 | (void) atomicio(vwrite, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 846 | else |
| 847 | run_err("%s: %s", name, strerror(haderr)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 848 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
| 852 | void |
Damien Miller | 65d1f57 | 2003-06-04 22:51:08 +1000 | [diff] [blame] | 853 | rsource(char *name, struct stat *statp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 854 | { |
| 855 | DIR *dirp; |
| 856 | struct dirent *dp; |
Darren Tucker | 194454d | 2013-06-06 08:16:04 +1000 | [diff] [blame] | 857 | char *last, *vect[1], path[MAXPATHLEN]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 858 | |
| 859 | if (!(dirp = opendir(name))) { |
| 860 | run_err("%s: %s", name, strerror(errno)); |
| 861 | return; |
| 862 | } |
| 863 | last = strrchr(name, '/'); |
| 864 | if (last == 0) |
| 865 | last = name; |
| 866 | else |
| 867 | last++; |
| 868 | if (pflag) { |
Damien Miller | 044bd2a | 2013-07-18 16:09:25 +1000 | [diff] [blame] | 869 | if (do_times(remout, verbose_mode, statp) < 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 870 | closedir(dirp); |
| 871 | return; |
| 872 | } |
| 873 | } |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 874 | (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n", |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 875 | (u_int) (statp->st_mode & FILEMODEMASK), 0, last); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 876 | if (verbose_mode) |
| 877 | fprintf(stderr, "Entering directory: %s", path); |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 878 | (void) atomicio(vwrite, remout, path, strlen(path)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 879 | if (response() < 0) { |
| 880 | closedir(dirp); |
| 881 | return; |
| 882 | } |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 883 | while ((dp = readdir(dirp)) != NULL) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 884 | if (dp->d_ino == 0) |
| 885 | continue; |
| 886 | if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) |
| 887 | continue; |
| 888 | if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) { |
| 889 | run_err("%s/%s: name too long", name, dp->d_name); |
| 890 | continue; |
| 891 | } |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 892 | (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 893 | vect[0] = path; |
| 894 | source(1, vect); |
| 895 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 896 | (void) closedir(dirp); |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 897 | (void) atomicio(vwrite, remout, "E\n", 2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 898 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | void |
Damien Miller | 65d1f57 | 2003-06-04 22:51:08 +1000 | [diff] [blame] | 902 | sink(int argc, char **argv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 903 | { |
| 904 | static BUF buffer; |
| 905 | struct stat stb; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 906 | enum { |
| 907 | YES, NO, DISPLAYED |
| 908 | } wrerr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 909 | BUF *bp; |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 910 | off_t i; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 911 | size_t j, count; |
Damien Miller | 07aa132 | 2006-04-23 12:04:27 +1000 | [diff] [blame] | 912 | int amt, exists, first, ofd; |
| 913 | mode_t mode, omode, mask; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 914 | off_t size, statbytes; |
Damien Miller | 044bd2a | 2013-07-18 16:09:25 +1000 | [diff] [blame] | 915 | unsigned long long ull; |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 916 | int setimes, targisdir, wrerrno = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 917 | char ch, *cp, *np, *targ, *why, *vect[1], buf[2048]; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 918 | struct timeval tv[2]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 919 | |
Ben Lindstrom | f719a20 | 2001-04-14 23:14:22 +0000 | [diff] [blame] | 920 | #define atime tv[0] |
| 921 | #define mtime tv[1] |
Darren Tucker | f30e1ac | 2004-11-05 20:10:02 +1100 | [diff] [blame] | 922 | #define SCREWUP(str) { why = str; goto screwup; } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 923 | |
| 924 | setimes = targisdir = 0; |
| 925 | mask = umask(0); |
| 926 | if (!pflag) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 927 | (void) umask(mask); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 928 | if (argc != 1) { |
| 929 | run_err("ambiguous target"); |
| 930 | exit(1); |
| 931 | } |
| 932 | targ = *argv; |
| 933 | if (targetshouldbedirectory) |
| 934 | verifydir(targ); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 935 | |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 936 | (void) atomicio(vwrite, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 937 | if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) |
| 938 | targisdir = 1; |
| 939 | for (first = 1;; first = 0) { |
| 940 | cp = buf; |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 941 | if (atomicio(read, remin, cp, 1) != 1) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 942 | return; |
| 943 | if (*cp++ == '\n') |
| 944 | SCREWUP("unexpected <newline>"); |
| 945 | do { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 946 | if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 947 | SCREWUP("lost connection"); |
| 948 | *cp++ = ch; |
| 949 | } while (cp < &buf[sizeof(buf) - 1] && ch != '\n'); |
| 950 | *cp = 0; |
Darren Tucker | bddc2b0 | 2004-04-19 23:50:16 +1000 | [diff] [blame] | 951 | if (verbose_mode) |
| 952 | fprintf(stderr, "Sink: %s", buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 953 | |
| 954 | if (buf[0] == '\01' || buf[0] == '\02') { |
| 955 | if (iamremote == 0) |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 956 | (void) atomicio(vwrite, STDERR_FILENO, |
Kevin Steves | 7d00ba4 | 2000-12-15 23:03:10 +0000 | [diff] [blame] | 957 | buf + 1, strlen(buf + 1)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 958 | if (buf[0] == '\02') |
| 959 | exit(1); |
| 960 | ++errs; |
| 961 | continue; |
| 962 | } |
| 963 | if (buf[0] == 'E') { |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 964 | (void) atomicio(vwrite, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 965 | return; |
| 966 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 967 | if (ch == '\n') |
| 968 | *--cp = 0; |
| 969 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 970 | cp = buf; |
| 971 | if (*cp == 'T') { |
| 972 | setimes++; |
| 973 | cp++; |
Damien Miller | 044bd2a | 2013-07-18 16:09:25 +1000 | [diff] [blame] | 974 | if (!isdigit((unsigned char)*cp)) |
| 975 | SCREWUP("mtime.sec not present"); |
| 976 | ull = strtoull(cp, &cp, 10); |
Ben Lindstrom | f719a20 | 2001-04-14 23:14:22 +0000 | [diff] [blame] | 977 | if (!cp || *cp++ != ' ') |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 978 | SCREWUP("mtime.sec not delimited"); |
Damien Miller | bc35d92 | 2013-07-18 16:11:25 +1000 | [diff] [blame] | 979 | if ((time_t)ull < 0 || |
| 980 | (unsigned long long)(time_t)ull != ull) |
Damien Miller | 044bd2a | 2013-07-18 16:09:25 +1000 | [diff] [blame] | 981 | setimes = 0; /* out of range */ |
| 982 | mtime.tv_sec = ull; |
Ben Lindstrom | f719a20 | 2001-04-14 23:14:22 +0000 | [diff] [blame] | 983 | mtime.tv_usec = strtol(cp, &cp, 10); |
Damien Miller | 044bd2a | 2013-07-18 16:09:25 +1000 | [diff] [blame] | 984 | if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 || |
| 985 | mtime.tv_usec > 999999) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 986 | SCREWUP("mtime.usec not delimited"); |
Damien Miller | 044bd2a | 2013-07-18 16:09:25 +1000 | [diff] [blame] | 987 | if (!isdigit((unsigned char)*cp)) |
| 988 | SCREWUP("atime.sec not present"); |
| 989 | ull = strtoull(cp, &cp, 10); |
Ben Lindstrom | f719a20 | 2001-04-14 23:14:22 +0000 | [diff] [blame] | 990 | if (!cp || *cp++ != ' ') |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 991 | SCREWUP("atime.sec not delimited"); |
Damien Miller | bc35d92 | 2013-07-18 16:11:25 +1000 | [diff] [blame] | 992 | if ((time_t)ull < 0 || |
| 993 | (unsigned long long)(time_t)ull != ull) |
Damien Miller | 044bd2a | 2013-07-18 16:09:25 +1000 | [diff] [blame] | 994 | setimes = 0; /* out of range */ |
| 995 | atime.tv_sec = ull; |
Ben Lindstrom | f719a20 | 2001-04-14 23:14:22 +0000 | [diff] [blame] | 996 | atime.tv_usec = strtol(cp, &cp, 10); |
Damien Miller | 044bd2a | 2013-07-18 16:09:25 +1000 | [diff] [blame] | 997 | if (!cp || *cp++ != '\0' || atime.tv_usec < 0 || |
| 998 | atime.tv_usec > 999999) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 999 | SCREWUP("atime.usec not delimited"); |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 1000 | (void) atomicio(vwrite, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1001 | continue; |
| 1002 | } |
| 1003 | if (*cp != 'C' && *cp != 'D') { |
| 1004 | /* |
| 1005 | * Check for the case "rcp remote:foo\* local:bar". |
| 1006 | * In this case, the line "No match." can be returned |
| 1007 | * by the shell before the rcp command on the remote is |
| 1008 | * executed so the ^Aerror_message convention isn't |
| 1009 | * followed. |
| 1010 | */ |
| 1011 | if (first) { |
| 1012 | run_err("%s", cp); |
| 1013 | exit(1); |
| 1014 | } |
| 1015 | SCREWUP("expected control record"); |
| 1016 | } |
| 1017 | mode = 0; |
| 1018 | for (++cp; cp < buf + 5; cp++) { |
| 1019 | if (*cp < '0' || *cp > '7') |
| 1020 | SCREWUP("bad mode"); |
| 1021 | mode = (mode << 3) | (*cp - '0'); |
| 1022 | } |
| 1023 | if (*cp++ != ' ') |
| 1024 | SCREWUP("mode not delimited"); |
| 1025 | |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 1026 | for (size = 0; isdigit(*cp);) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1027 | size = size * 10 + (*cp++ - '0'); |
| 1028 | if (*cp++ != ' ') |
| 1029 | SCREWUP("size not delimited"); |
Darren Tucker | bddc2b0 | 2004-04-19 23:50:16 +1000 | [diff] [blame] | 1030 | if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) { |
| 1031 | run_err("error: unexpected filename: %s", cp); |
| 1032 | exit(1); |
| 1033 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1034 | if (targisdir) { |
| 1035 | static char *namebuf; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1036 | static size_t cursize; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1037 | size_t need; |
| 1038 | |
| 1039 | need = strlen(targ) + strlen(cp) + 250; |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 1040 | if (need > cursize) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1041 | free(namebuf); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1042 | namebuf = xmalloc(need); |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 1043 | cursize = need; |
| 1044 | } |
| 1045 | (void) snprintf(namebuf, need, "%s%s%s", targ, |
Ben Lindstrom | de3895d | 2002-04-06 18:29:59 +0000 | [diff] [blame] | 1046 | strcmp(targ, "/") ? "/" : "", cp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1047 | np = namebuf; |
| 1048 | } else |
| 1049 | np = targ; |
| 1050 | curfile = cp; |
| 1051 | exists = stat(np, &stb) == 0; |
| 1052 | if (buf[0] == 'D') { |
| 1053 | int mod_flag = pflag; |
Darren Tucker | bddc2b0 | 2004-04-19 23:50:16 +1000 | [diff] [blame] | 1054 | if (!iamrecursive) |
| 1055 | SCREWUP("received directory without -r"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1056 | if (exists) { |
| 1057 | if (!S_ISDIR(stb.st_mode)) { |
| 1058 | errno = ENOTDIR; |
| 1059 | goto bad; |
| 1060 | } |
| 1061 | if (pflag) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1062 | (void) chmod(np, mode); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1063 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1064 | /* Handle copying from a read-only |
| 1065 | directory */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1066 | mod_flag = 1; |
| 1067 | if (mkdir(np, mode | S_IRWXU) < 0) |
| 1068 | goto bad; |
| 1069 | } |
Ben Lindstrom | 550bc54 | 2001-02-10 21:50:00 +0000 | [diff] [blame] | 1070 | vect[0] = xstrdup(np); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1071 | sink(1, vect); |
| 1072 | if (setimes) { |
| 1073 | setimes = 0; |
Damien Miller | 225736c | 2001-02-19 21:51:08 +1100 | [diff] [blame] | 1074 | if (utimes(vect[0], tv) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1075 | run_err("%s: set times: %s", |
Damien Miller | 225736c | 2001-02-19 21:51:08 +1100 | [diff] [blame] | 1076 | vect[0], strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1077 | } |
| 1078 | if (mod_flag) |
Damien Miller | 225736c | 2001-02-19 21:51:08 +1100 | [diff] [blame] | 1079 | (void) chmod(vect[0], mode); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1080 | free(vect[0]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1081 | continue; |
| 1082 | } |
| 1083 | omode = mode; |
Darren Tucker | 3750fce | 2013-06-02 07:52:21 +1000 | [diff] [blame] | 1084 | mode |= S_IWUSR; |
Ben Lindstrom | 7bad55b | 2001-06-05 19:31:41 +0000 | [diff] [blame] | 1085 | if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1086 | bad: run_err("%s: %s", np, strerror(errno)); |
| 1087 | continue; |
| 1088 | } |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 1089 | (void) atomicio(vwrite, remout, "", 1); |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 1090 | if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1091 | (void) close(ofd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1092 | continue; |
| 1093 | } |
| 1094 | cp = bp->buf; |
| 1095 | wrerr = NO; |
| 1096 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1097 | statbytes = 0; |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1098 | if (showprogress) |
| 1099 | start_progress_meter(curfile, size, &statbytes); |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 1100 | set_nonblock(remin); |
| 1101 | for (count = i = 0; i < size; i += bp->cnt) { |
| 1102 | amt = bp->cnt; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1103 | if (i + amt > size) |
| 1104 | amt = size - i; |
| 1105 | count += amt; |
| 1106 | do { |
Damien Miller | 65e42f8 | 2010-09-24 22:15:11 +1000 | [diff] [blame] | 1107 | j = atomicio6(read, remin, cp, amt, |
| 1108 | scpio, &statbytes); |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 1109 | if (j == 0) { |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 1110 | run_err("%s", j != EPIPE ? |
| 1111 | strerror(errno) : |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 1112 | "dropped connection"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1113 | exit(1); |
| 1114 | } |
| 1115 | amt -= j; |
| 1116 | cp += j; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1117 | } while (amt > 0); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1118 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1119 | if (count == bp->cnt) { |
| 1120 | /* Keep reading so we stay sync'd up. */ |
| 1121 | if (wrerr == NO) { |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 1122 | if (atomicio(vwrite, ofd, bp->buf, |
| 1123 | count) != count) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1124 | wrerr = YES; |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 1125 | wrerrno = errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | count = 0; |
| 1129 | cp = bp->buf; |
| 1130 | } |
| 1131 | } |
Damien Miller | c77cadb | 2007-10-26 15:39:15 +1000 | [diff] [blame] | 1132 | unset_nonblock(remin); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1133 | if (showprogress) |
Damien Miller | 62d57f6 | 2003-01-10 21:43:24 +1100 | [diff] [blame] | 1134 | stop_progress_meter(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1135 | if (count != 0 && wrerr == NO && |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 1136 | atomicio(vwrite, ofd, bp->buf, count) != count) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1137 | wrerr = YES; |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 1138 | wrerrno = errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1139 | } |
Darren Tucker | 067263e | 2007-06-25 18:32:33 +1000 | [diff] [blame] | 1140 | if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) && |
| 1141 | ftruncate(ofd, size) != 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1142 | run_err("%s: truncate: %s", np, strerror(errno)); |
| 1143 | wrerr = DISPLAYED; |
| 1144 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1145 | if (pflag) { |
| 1146 | if (exists || omode != mode) |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1147 | #ifdef HAVE_FCHMOD |
Darren Tucker | e1f1705 | 2004-07-08 23:11:44 +1000 | [diff] [blame] | 1148 | if (fchmod(ofd, omode)) { |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1149 | #else /* HAVE_FCHMOD */ |
Darren Tucker | e1f1705 | 2004-07-08 23:11:44 +1000 | [diff] [blame] | 1150 | if (chmod(np, omode)) { |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1151 | #endif /* HAVE_FCHMOD */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1152 | run_err("%s: set mode: %s", |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 1153 | np, strerror(errno)); |
Darren Tucker | e1f1705 | 2004-07-08 23:11:44 +1000 | [diff] [blame] | 1154 | wrerr = DISPLAYED; |
| 1155 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1156 | } else { |
| 1157 | if (!exists && omode != mode) |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1158 | #ifdef HAVE_FCHMOD |
Darren Tucker | e1f1705 | 2004-07-08 23:11:44 +1000 | [diff] [blame] | 1159 | if (fchmod(ofd, omode & ~mask)) { |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1160 | #else /* HAVE_FCHMOD */ |
Darren Tucker | e1f1705 | 2004-07-08 23:11:44 +1000 | [diff] [blame] | 1161 | if (chmod(np, omode & ~mask)) { |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1162 | #endif /* HAVE_FCHMOD */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1163 | run_err("%s: set mode: %s", |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 1164 | np, strerror(errno)); |
Darren Tucker | e1f1705 | 2004-07-08 23:11:44 +1000 | [diff] [blame] | 1165 | wrerr = DISPLAYED; |
| 1166 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1167 | } |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1168 | if (close(ofd) == -1) { |
| 1169 | wrerr = YES; |
| 1170 | wrerrno = errno; |
| 1171 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1172 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1173 | if (setimes && wrerr == NO) { |
| 1174 | setimes = 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1175 | if (utimes(np, tv) < 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1176 | run_err("%s: set times: %s", |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 1177 | np, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1178 | wrerr = DISPLAYED; |
| 1179 | } |
| 1180 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1181 | switch (wrerr) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1182 | case YES: |
| 1183 | run_err("%s: %s", np, strerror(wrerrno)); |
| 1184 | break; |
| 1185 | case NO: |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 1186 | (void) atomicio(vwrite, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1187 | break; |
| 1188 | case DISPLAYED: |
| 1189 | break; |
| 1190 | } |
| 1191 | } |
| 1192 | screwup: |
| 1193 | run_err("protocol error: %s", why); |
| 1194 | exit(1); |
| 1195 | } |
| 1196 | |
| 1197 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1198 | response(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1199 | { |
| 1200 | char ch, *cp, resp, rbuf[2048]; |
| 1201 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1202 | if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1203 | lostconn(0); |
| 1204 | |
| 1205 | cp = rbuf; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1206 | switch (resp) { |
| 1207 | case 0: /* ok */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1208 | return (0); |
| 1209 | default: |
| 1210 | *cp++ = resp; |
| 1211 | /* FALLTHROUGH */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1212 | case 1: /* error, followed by error msg */ |
| 1213 | case 2: /* fatal error, "" */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1214 | do { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1215 | if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1216 | lostconn(0); |
| 1217 | *cp++ = ch; |
| 1218 | } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n'); |
| 1219 | |
| 1220 | if (!iamremote) |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 1221 | (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1222 | ++errs; |
| 1223 | if (resp == 1) |
| 1224 | return (-1); |
| 1225 | exit(1); |
| 1226 | } |
| 1227 | /* NOTREACHED */ |
| 1228 | } |
| 1229 | |
| 1230 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1231 | usage(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1232 | { |
Ben Lindstrom | 1e24324 | 2001-09-18 05:38:44 +0000 | [diff] [blame] | 1233 | (void) fprintf(stderr, |
Damien Miller | 907998d | 2011-01-06 22:41:21 +1100 | [diff] [blame] | 1234 | "usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n" |
Darren Tucker | 1f20394 | 2003-10-15 15:50:42 +1000 | [diff] [blame] | 1235 | " [-l limit] [-o ssh_option] [-P port] [-S program]\n" |
Damien Miller | 647d97b | 2007-08-08 14:29:58 +1000 | [diff] [blame] | 1236 | " [[user@]host1:]file1 ... [[user@]host2:]file2\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1237 | exit(1); |
| 1238 | } |
| 1239 | |
| 1240 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1241 | run_err(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1242 | { |
| 1243 | static FILE *fp; |
| 1244 | va_list ap; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1245 | |
| 1246 | ++errs; |
Damien Miller | 745570c | 2006-03-26 13:49:43 +1100 | [diff] [blame] | 1247 | if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) { |
| 1248 | (void) fprintf(fp, "%c", 0x01); |
| 1249 | (void) fprintf(fp, "scp: "); |
| 1250 | va_start(ap, fmt); |
| 1251 | (void) vfprintf(fp, fmt, ap); |
| 1252 | va_end(ap); |
| 1253 | (void) fprintf(fp, "\n"); |
| 1254 | (void) fflush(fp); |
| 1255 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1256 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1257 | if (!iamremote) { |
Damien Miller | 03783f0 | 1999-12-31 09:16:40 +1100 | [diff] [blame] | 1258 | va_start(ap, fmt); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1259 | vfprintf(stderr, fmt, ap); |
Damien Miller | 03783f0 | 1999-12-31 09:16:40 +1100 | [diff] [blame] | 1260 | va_end(ap); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1261 | fprintf(stderr, "\n"); |
| 1262 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1263 | } |
| 1264 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1265 | void |
Damien Miller | 65d1f57 | 2003-06-04 22:51:08 +1000 | [diff] [blame] | 1266 | verifydir(char *cp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1267 | { |
| 1268 | struct stat stb; |
| 1269 | |
| 1270 | if (!stat(cp, &stb)) { |
| 1271 | if (S_ISDIR(stb.st_mode)) |
| 1272 | return; |
| 1273 | errno = ENOTDIR; |
| 1274 | } |
| 1275 | run_err("%s: %s", cp, strerror(errno)); |
Darren Tucker | fc4f2dd | 2005-06-01 23:01:12 +1000 | [diff] [blame] | 1276 | killchild(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | int |
Damien Miller | 65d1f57 | 2003-06-04 22:51:08 +1000 | [diff] [blame] | 1280 | okname(char *cp0) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1281 | { |
| 1282 | int c; |
| 1283 | char *cp; |
| 1284 | |
| 1285 | cp = cp0; |
| 1286 | do { |
Ben Lindstrom | 7d5ed3a | 2001-06-25 04:28:30 +0000 | [diff] [blame] | 1287 | c = (int)*cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1288 | if (c & 0200) |
| 1289 | goto bad; |
Damien Miller | eeeeb35 | 2003-02-24 11:52:58 +1100 | [diff] [blame] | 1290 | if (!isalpha(c) && !isdigit(c)) { |
| 1291 | switch (c) { |
| 1292 | case '\'': |
| 1293 | case '"': |
| 1294 | case '`': |
| 1295 | case ' ': |
| 1296 | case '#': |
| 1297 | goto bad; |
| 1298 | default: |
| 1299 | break; |
| 1300 | } |
| 1301 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1302 | } while (*++cp); |
| 1303 | return (1); |
| 1304 | |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 1305 | bad: fprintf(stderr, "%s: invalid user name\n", cp0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1306 | return (0); |
| 1307 | } |
| 1308 | |
| 1309 | BUF * |
Damien Miller | 65d1f57 | 2003-06-04 22:51:08 +1000 | [diff] [blame] | 1310 | allocbuf(BUF *bp, int fd, int blksize) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1311 | { |
| 1312 | size_t size; |
Tim Rice | 13aae5e | 2001-10-21 17:53:58 -0700 | [diff] [blame] | 1313 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1314 | struct stat stb; |
| 1315 | |
| 1316 | if (fstat(fd, &stb) < 0) { |
| 1317 | run_err("fstat: %s", strerror(errno)); |
| 1318 | return (0); |
| 1319 | } |
Ben Lindstrom | 418e078 | 2002-12-23 02:22:09 +0000 | [diff] [blame] | 1320 | size = roundup(stb.st_blksize, blksize); |
| 1321 | if (size == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1322 | size = blksize; |
Tim Rice | 13aae5e | 2001-10-21 17:53:58 -0700 | [diff] [blame] | 1323 | #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */ |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1324 | size = blksize; |
Tim Rice | 13aae5e | 2001-10-21 17:53:58 -0700 | [diff] [blame] | 1325 | #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1326 | if (bp->cnt >= size) |
| 1327 | return (bp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1328 | if (bp->buf == NULL) |
| 1329 | bp->buf = xmalloc(size); |
| 1330 | else |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 1331 | bp->buf = xrealloc(bp->buf, 1, size); |
Ben Lindstrom | 5fccbc2 | 2001-09-12 17:49:48 +0000 | [diff] [blame] | 1332 | memset(bp->buf, 0, size); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1333 | bp->cnt = size; |
| 1334 | return (bp); |
| 1335 | } |
| 1336 | |
| 1337 | void |
Damien Miller | 65d1f57 | 2003-06-04 22:51:08 +1000 | [diff] [blame] | 1338 | lostconn(int signo) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1339 | { |
| 1340 | if (!iamremote) |
Darren Tucker | dbee308 | 2013-05-16 20:32:29 +1000 | [diff] [blame] | 1341 | (void)write(STDERR_FILENO, "lost connection\n", 16); |
Ben Lindstrom | 738f51e | 2001-06-21 03:08:58 +0000 | [diff] [blame] | 1342 | if (signo) |
| 1343 | _exit(1); |
| 1344 | else |
| 1345 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1346 | } |