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