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