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