Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 2 | * scp - secure remote copy. This is basically patched BSD rcp which |
| 3 | * uses ssh to do the data transfer (instead of using rcmd). |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 5 | * NOTE: This version should NOT be suid root. (This uses ssh to |
| 6 | * do the transfer and ssh has the necessary privileges.) |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 7 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 8 | * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 10 | * As far as I am concerned, the code I have written for this software |
| 11 | * can be used freely for any purpose. Any derived versions of this |
| 12 | * software must be clearly marked as such, and if the derived work is |
| 13 | * incompatible with the protocol description in the RFC file, it must be |
| 14 | * called by a name other than "ssh" or "Secure Shell". |
| 15 | */ |
| 16 | /* |
| 17 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
| 18 | * Copyright (c) 1999 Aaron Campbell. All rights reserved. |
| 19 | * |
| 20 | * Redistribution and use in source and binary forms, with or without |
| 21 | * modification, are permitted provided that the following conditions |
| 22 | * are met: |
| 23 | * 1. Redistributions of source code must retain the above copyright |
| 24 | * notice, this list of conditions and the following disclaimer. |
| 25 | * 2. Redistributions in binary form must reproduce the above copyright |
| 26 | * notice, this list of conditions and the following disclaimer in the |
| 27 | * documentation and/or other materials provided with the distribution. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 30 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 31 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 32 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 33 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 34 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 35 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 36 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 38 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 39 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | |
| 41 | /* |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 42 | * Parts from: |
| 43 | * |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | * Copyright (c) 1983, 1990, 1992, 1993, 1995 |
| 45 | * The Regents of the University of California. All rights reserved. |
| 46 | * |
| 47 | * Redistribution and use in source and binary forms, with or without |
| 48 | * modification, are permitted provided that the following conditions |
| 49 | * are met: |
| 50 | * 1. Redistributions of source code must retain the above copyright |
| 51 | * notice, this list of conditions and the following disclaimer. |
| 52 | * 2. Redistributions in binary form must reproduce the above copyright |
| 53 | * notice, this list of conditions and the following disclaimer in the |
| 54 | * documentation and/or other materials provided with the distribution. |
| 55 | * 3. All advertising materials mentioning features or use of this software |
| 56 | * must display the following acknowledgement: |
| 57 | * This product includes software developed by the University of |
| 58 | * California, Berkeley and its contributors. |
| 59 | * 4. Neither the name of the University nor the names of its contributors |
| 60 | * may be used to endorse or promote products derived from this software |
| 61 | * without specific prior written permission. |
| 62 | * |
| 63 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 64 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 65 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 66 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 67 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 68 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 69 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 70 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 71 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 72 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 73 | * SUCH DAMAGE. |
| 74 | * |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 75 | */ |
| 76 | |
| 77 | #include "includes.h" |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 78 | RCSID("$OpenBSD: scp.c,v 1.40 2000/09/21 11:11:42 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | |
| 80 | #include "ssh.h" |
| 81 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 82 | |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 83 | #ifndef _PATH_CP |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 84 | #define _PATH_CP "cp" |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 85 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 86 | |
| 87 | /* For progressmeter() -- number of seconds before xfer considered "stalled" */ |
| 88 | #define STALLTIME 5 |
| 89 | |
Damien Miller | 484118e | 2000-07-02 19:13:56 +1000 | [diff] [blame] | 90 | /* Progress meter bar */ |
| 91 | #define BAR \ |
| 92 | "************************************************************"\ |
| 93 | "************************************************************"\ |
| 94 | "************************************************************"\ |
| 95 | "************************************************************" |
| 96 | #define MAX_BARLENGTH (sizeof(BAR) - 1) |
| 97 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 98 | /* Visual statistics about files as they are transferred. */ |
| 99 | void progressmeter(int); |
| 100 | |
| 101 | /* Returns width of the terminal (for progress meter calculations). */ |
| 102 | int getttywidth(void); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 103 | int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 104 | |
| 105 | /* Time a transfer started. */ |
| 106 | static struct timeval start; |
| 107 | |
| 108 | /* Number of bytes of current file transferred so far. */ |
| 109 | volatile unsigned long statbytes; |
| 110 | |
| 111 | /* Total size of current file. */ |
Damien Miller | a2d6efe | 1999-11-13 13:22:46 +1100 | [diff] [blame] | 112 | off_t totalbytes = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 113 | |
| 114 | /* Name of current file being transferred. */ |
| 115 | char *curfile; |
| 116 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 117 | /* This is set to non-zero if IPv4 is desired. */ |
| 118 | int IPv4 = 0; |
| 119 | |
| 120 | /* This is set to non-zero if IPv6 is desired. */ |
| 121 | int IPv6 = 0; |
| 122 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 123 | /* This is set to non-zero to enable verbose mode. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 124 | int verbose_mode = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 125 | |
| 126 | /* This is set to non-zero if compression is desired. */ |
Damien Miller | d8087f6 | 1999-11-25 12:31:26 +1100 | [diff] [blame] | 127 | int compress_flag = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 128 | |
| 129 | /* This is set to zero if the progressmeter is not desired. */ |
| 130 | int showprogress = 1; |
| 131 | |
| 132 | /* This is set to non-zero if running in batch mode (that is, password |
| 133 | and passphrase queries are not allowed). */ |
| 134 | int batchmode = 0; |
| 135 | |
| 136 | /* This is set to the cipher type string if given on the command line. */ |
| 137 | char *cipher = NULL; |
| 138 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 139 | /* This is set to the RSA authentication identity file name if given on |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 140 | the command line. */ |
| 141 | char *identity = NULL; |
| 142 | |
| 143 | /* This is the port to use in contacting the remote site (is non-NULL). */ |
| 144 | char *port = NULL; |
| 145 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 146 | /* This is the program to execute for the secured connection. ("ssh" or -S) */ |
| 147 | char *ssh_program = SSH_PROGRAM; |
| 148 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 149 | /* |
| 150 | * This function executes the given command as the specified user on the |
| 151 | * given host. This returns < 0 if execution fails, and >= 0 otherwise. This |
| 152 | * assigns the input and output file descriptors on success. |
| 153 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 154 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 155 | int |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 156 | do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 157 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | int pin[2], pout[2], reserved[2]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 159 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 160 | if (verbose_mode) |
| 161 | fprintf(stderr, "Executing: host %s, user %s, command %s\n", |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 162 | host, remuser ? remuser : "(unspecified)", cmd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 163 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 164 | /* |
| 165 | * Reserve two descriptors so that the real pipes won't get |
| 166 | * descriptors 0 and 1 because that will screw up dup2 below. |
| 167 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 168 | pipe(reserved); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 169 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 170 | /* Create a socket pair for communicating with ssh. */ |
| 171 | if (pipe(pin) < 0) |
| 172 | fatal("pipe: %s", strerror(errno)); |
| 173 | if (pipe(pout) < 0) |
| 174 | fatal("pipe: %s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 175 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 176 | /* Free the reserved descriptors. */ |
| 177 | close(reserved[0]); |
| 178 | close(reserved[1]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 179 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 180 | /* For a child to execute the command on the remote host using ssh. */ |
| 181 | if (fork() == 0) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 182 | char *args[100]; /* XXX careful */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 183 | unsigned int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 184 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 185 | /* Child. */ |
| 186 | close(pin[1]); |
| 187 | close(pout[0]); |
| 188 | dup2(pin[0], 0); |
| 189 | dup2(pout[1], 1); |
| 190 | close(pin[0]); |
| 191 | close(pout[1]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 192 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 193 | i = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 194 | args[i++] = ssh_program; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 195 | args[i++] = "-x"; |
| 196 | args[i++] = "-oFallBackToRsh no"; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 197 | if (IPv4) |
| 198 | args[i++] = "-4"; |
| 199 | if (IPv6) |
| 200 | args[i++] = "-6"; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 201 | if (verbose_mode) |
| 202 | args[i++] = "-v"; |
Damien Miller | d8087f6 | 1999-11-25 12:31:26 +1100 | [diff] [blame] | 203 | if (compress_flag) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 204 | args[i++] = "-C"; |
| 205 | if (batchmode) |
| 206 | args[i++] = "-oBatchMode yes"; |
| 207 | if (cipher != NULL) { |
| 208 | args[i++] = "-c"; |
| 209 | args[i++] = cipher; |
| 210 | } |
| 211 | if (identity != NULL) { |
| 212 | args[i++] = "-i"; |
| 213 | args[i++] = identity; |
| 214 | } |
| 215 | if (port != NULL) { |
| 216 | args[i++] = "-p"; |
| 217 | args[i++] = port; |
| 218 | } |
| 219 | if (remuser != NULL) { |
| 220 | args[i++] = "-l"; |
| 221 | args[i++] = remuser; |
| 222 | } |
| 223 | args[i++] = host; |
| 224 | args[i++] = cmd; |
| 225 | args[i++] = NULL; |
| 226 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 227 | execvp(ssh_program, args); |
| 228 | perror(ssh_program); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 229 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 230 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 231 | /* Parent. Close the other side, and return the local side. */ |
| 232 | close(pin[0]); |
| 233 | *fdout = pin[1]; |
| 234 | close(pout[1]); |
| 235 | *fdin = pout[0]; |
| 236 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 237 | } |
| 238 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 239 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 240 | fatal(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 241 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 242 | va_list ap; |
| 243 | char buf[1024]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 244 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 245 | va_start(ap, fmt); |
| 246 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 247 | va_end(ap); |
| 248 | fprintf(stderr, "%s\n", buf); |
| 249 | exit(255); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 250 | } |
| 251 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 252 | typedef struct { |
| 253 | int cnt; |
| 254 | char *buf; |
| 255 | } BUF; |
| 256 | |
| 257 | extern int iamremote; |
| 258 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 259 | BUF *allocbuf(BUF *, int, int); |
| 260 | char *colon(char *); |
| 261 | void lostconn(int); |
| 262 | void nospace(void); |
| 263 | int okname(char *); |
| 264 | void run_err(const char *,...); |
| 265 | void verifydir(char *); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 266 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 267 | struct passwd *pwd; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 268 | uid_t userid; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 269 | int errs, remin, remout; |
| 270 | int pflag, iamremote, iamrecursive, targetshouldbedirectory; |
| 271 | |
| 272 | #define CMDNEEDS 64 |
| 273 | char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ |
| 274 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 275 | int response(void); |
| 276 | void rsource(char *, struct stat *); |
| 277 | void sink(int, char *[]); |
| 278 | void source(int, char *[]); |
| 279 | void tolocal(int, char *[]); |
| 280 | void toremote(char *, int, char *[]); |
| 281 | void usage(void); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 282 | |
| 283 | int |
| 284 | main(argc, argv) |
| 285 | int argc; |
| 286 | char *argv[]; |
| 287 | { |
| 288 | int ch, fflag, tflag; |
| 289 | char *targ; |
| 290 | extern char *optarg; |
| 291 | extern int optind; |
| 292 | |
| 293 | fflag = tflag = 0; |
Damien Miller | 7b28dc5 | 2000-09-05 13:34:53 +1100 | [diff] [blame] | 294 | while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:")) != EOF) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 295 | switch (ch) { |
| 296 | /* User-visible flags. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 297 | case '4': |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 298 | IPv4 = 1; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 299 | break; |
| 300 | case '6': |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 301 | IPv6 = 1; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 302 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 303 | case 'p': |
| 304 | pflag = 1; |
| 305 | break; |
| 306 | case 'P': |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 307 | port = optarg; |
| 308 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 309 | case 'r': |
| 310 | iamrecursive = 1; |
| 311 | break; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 312 | case 'S': |
| 313 | ssh_program = optarg; |
| 314 | break; |
| 315 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 316 | /* Server options. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 317 | case 'd': |
| 318 | targetshouldbedirectory = 1; |
| 319 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 320 | case 'f': /* "from" */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 321 | iamremote = 1; |
| 322 | fflag = 1; |
| 323 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 324 | case 't': /* "to" */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 325 | iamremote = 1; |
| 326 | tflag = 1; |
| 327 | break; |
| 328 | case 'c': |
| 329 | cipher = optarg; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 330 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 331 | case 'i': |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 332 | identity = optarg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 333 | break; |
| 334 | case 'v': |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 335 | verbose_mode = 1; |
| 336 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 337 | case 'B': |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 338 | batchmode = 1; |
| 339 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 340 | case 'C': |
Damien Miller | d8087f6 | 1999-11-25 12:31:26 +1100 | [diff] [blame] | 341 | compress_flag = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 342 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 343 | case 'q': |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 344 | showprogress = 0; |
| 345 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 346 | case '?': |
| 347 | default: |
| 348 | usage(); |
| 349 | } |
| 350 | argc -= optind; |
| 351 | argv += optind; |
| 352 | |
| 353 | if ((pwd = getpwuid(userid = getuid())) == NULL) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 354 | fatal("unknown user %d", (int) userid); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 355 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 356 | if (!isatty(STDERR_FILENO)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 357 | showprogress = 0; |
| 358 | |
| 359 | remin = STDIN_FILENO; |
| 360 | remout = STDOUT_FILENO; |
| 361 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 362 | if (fflag) { |
| 363 | /* Follow "protocol", send data. */ |
| 364 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 365 | source(argc, argv); |
| 366 | exit(errs != 0); |
| 367 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 368 | if (tflag) { |
| 369 | /* Receive data. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 370 | sink(argc, argv); |
| 371 | exit(errs != 0); |
| 372 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 373 | if (argc < 2) |
| 374 | usage(); |
| 375 | if (argc > 2) |
| 376 | targetshouldbedirectory = 1; |
| 377 | |
| 378 | remin = remout = -1; |
| 379 | /* Command to be executed on remote system using "ssh". */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 380 | (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "", |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 381 | iamrecursive ? " -r" : "", pflag ? " -p" : "", |
| 382 | targetshouldbedirectory ? " -d" : ""); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 383 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 384 | (void) signal(SIGPIPE, lostconn); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 385 | |
| 386 | if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */ |
| 387 | toremote(targ, argc, argv); |
| 388 | else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 389 | tolocal(argc, argv); /* Dest is local host. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 390 | if (targetshouldbedirectory) |
| 391 | verifydir(argv[argc - 1]); |
| 392 | } |
| 393 | exit(errs != 0); |
| 394 | } |
| 395 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 396 | char * |
| 397 | cleanhostname(host) |
| 398 | char *host; |
| 399 | { |
| 400 | if (*host == '[' && host[strlen(host) - 1] == ']') { |
| 401 | host[strlen(host) - 1] = '\0'; |
| 402 | return (host + 1); |
| 403 | } else |
| 404 | return host; |
| 405 | } |
| 406 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 407 | void |
| 408 | toremote(targ, argc, argv) |
| 409 | char *targ, *argv[]; |
| 410 | int argc; |
| 411 | { |
| 412 | int i, len; |
| 413 | char *bp, *host, *src, *suser, *thost, *tuser; |
| 414 | |
| 415 | *targ++ = 0; |
| 416 | if (*targ == 0) |
| 417 | targ = "."; |
| 418 | |
| 419 | if ((thost = strchr(argv[argc - 1], '@'))) { |
| 420 | /* user@host */ |
| 421 | *thost++ = 0; |
| 422 | tuser = argv[argc - 1]; |
| 423 | if (*tuser == '\0') |
| 424 | tuser = NULL; |
| 425 | else if (!okname(tuser)) |
| 426 | exit(1); |
| 427 | } else { |
| 428 | thost = argv[argc - 1]; |
| 429 | tuser = NULL; |
| 430 | } |
| 431 | |
| 432 | for (i = 0; i < argc - 1; i++) { |
| 433 | src = colon(argv[i]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 434 | if (src) { /* remote to remote */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 435 | *src++ = 0; |
| 436 | if (*src == 0) |
| 437 | src = "."; |
| 438 | host = strchr(argv[i], '@'); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 439 | len = strlen(ssh_program) + strlen(argv[i]) + |
| 440 | strlen(src) + (tuser ? strlen(tuser) : 0) + |
| 441 | strlen(thost) + strlen(targ) + CMDNEEDS + 32; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 442 | bp = xmalloc(len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 443 | if (host) { |
| 444 | *host++ = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 445 | host = cleanhostname(host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 446 | suser = argv[i]; |
| 447 | if (*suser == '\0') |
| 448 | suser = pwd->pw_name; |
| 449 | else if (!okname(suser)) |
| 450 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 451 | (void) sprintf(bp, |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 452 | "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'", |
| 453 | ssh_program, verbose_mode ? " -v" : "", |
| 454 | suser, host, cmd, src, |
| 455 | tuser ? tuser : "", tuser ? "@" : "", |
| 456 | thost, targ); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 457 | } else { |
| 458 | host = cleanhostname(argv[i]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 459 | (void) sprintf(bp, |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 460 | "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'", |
| 461 | ssh_program, verbose_mode ? " -v" : "", |
| 462 | host, cmd, src, |
| 463 | tuser ? tuser : "", tuser ? "@" : "", |
| 464 | thost, targ); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 465 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 466 | if (verbose_mode) |
| 467 | fprintf(stderr, "Executing: %s\n", bp); |
| 468 | (void) system(bp); |
| 469 | (void) xfree(bp); |
| 470 | } else { /* local to remote */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 471 | if (remin == -1) { |
| 472 | len = strlen(targ) + CMDNEEDS + 20; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 473 | bp = xmalloc(len); |
| 474 | (void) sprintf(bp, "%s -t %s", cmd, targ); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 475 | host = cleanhostname(thost); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 476 | if (do_cmd(host, tuser, bp, &remin, |
| 477 | &remout, argc) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 478 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 479 | if (response() < 0) |
| 480 | exit(1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 481 | (void) xfree(bp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 482 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 483 | source(1, argv + i); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | void |
| 489 | tolocal(argc, argv) |
| 490 | int argc; |
| 491 | char *argv[]; |
| 492 | { |
| 493 | int i, len; |
| 494 | char *bp, *host, *src, *suser; |
| 495 | |
| 496 | for (i = 0; i < argc - 1; i++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 497 | if (!(src = colon(argv[i]))) { /* Local to local. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 498 | len = strlen(_PATH_CP) + strlen(argv[i]) + |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 499 | strlen(argv[argc - 1]) + 20; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 500 | bp = xmalloc(len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 501 | (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP, |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 502 | iamrecursive ? " -r" : "", pflag ? " -p" : "", |
| 503 | argv[i], argv[argc - 1]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 504 | if (verbose_mode) |
| 505 | fprintf(stderr, "Executing: %s\n", bp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 506 | if (system(bp)) |
| 507 | ++errs; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 508 | (void) xfree(bp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 509 | continue; |
| 510 | } |
| 511 | *src++ = 0; |
| 512 | if (*src == 0) |
| 513 | src = "."; |
| 514 | if ((host = strchr(argv[i], '@')) == NULL) { |
| 515 | host = argv[i]; |
| 516 | suser = NULL; |
| 517 | } else { |
| 518 | *host++ = 0; |
| 519 | suser = argv[i]; |
| 520 | if (*suser == '\0') |
| 521 | suser = pwd->pw_name; |
| 522 | else if (!okname(suser)) |
| 523 | continue; |
| 524 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 525 | host = cleanhostname(host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 526 | len = strlen(src) + CMDNEEDS + 20; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 527 | bp = xmalloc(len); |
| 528 | (void) sprintf(bp, "%s -f %s", cmd, src); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 529 | if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 530 | (void) xfree(bp); |
| 531 | ++errs; |
| 532 | continue; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 533 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 534 | xfree(bp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 535 | sink(1, argv + argc - 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 536 | (void) close(remin); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 537 | remin = remout = -1; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | void |
| 542 | source(argc, argv) |
| 543 | int argc; |
| 544 | char *argv[]; |
| 545 | { |
| 546 | struct stat stb; |
| 547 | static BUF buffer; |
| 548 | BUF *bp; |
| 549 | off_t i; |
| 550 | int amt, fd, haderr, indx, result; |
| 551 | char *last, *name, buf[2048]; |
| 552 | |
| 553 | for (indx = 0; indx < argc; ++indx) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 554 | name = argv[indx]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 555 | statbytes = 0; |
| 556 | if ((fd = open(name, O_RDONLY, 0)) < 0) |
| 557 | goto syserr; |
| 558 | if (fstat(fd, &stb) < 0) { |
| 559 | syserr: run_err("%s: %s", name, strerror(errno)); |
| 560 | goto next; |
| 561 | } |
| 562 | switch (stb.st_mode & S_IFMT) { |
| 563 | case S_IFREG: |
| 564 | break; |
| 565 | case S_IFDIR: |
| 566 | if (iamrecursive) { |
| 567 | rsource(name, &stb); |
| 568 | goto next; |
| 569 | } |
| 570 | /* FALLTHROUGH */ |
| 571 | default: |
| 572 | run_err("%s: not a regular file", name); |
| 573 | goto next; |
| 574 | } |
| 575 | if ((last = strrchr(name, '/')) == NULL) |
| 576 | last = name; |
| 577 | else |
| 578 | ++last; |
| 579 | curfile = last; |
| 580 | if (pflag) { |
| 581 | /* |
| 582 | * Make it compatible with possible future |
| 583 | * versions expecting microseconds. |
| 584 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 585 | (void) sprintf(buf, "T%lu 0 %lu 0\n", |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 586 | (unsigned long) stb.st_mtime, |
| 587 | (unsigned long) stb.st_atime); |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 588 | (void) atomicio(write, remout, buf, strlen(buf)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 589 | if (response() < 0) |
| 590 | goto next; |
| 591 | } |
| 592 | #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 593 | (void) sprintf(buf, "C%04o %lu %s\n", |
| 594 | (unsigned int) (stb.st_mode & FILEMODEMASK), |
| 595 | (unsigned long) stb.st_size, |
| 596 | last); |
| 597 | if (verbose_mode) { |
| 598 | fprintf(stderr, "Sending file modes: %s", buf); |
| 599 | fflush(stderr); |
| 600 | } |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 601 | (void) atomicio(write, remout, buf, strlen(buf)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 602 | if (response() < 0) |
| 603 | goto next; |
| 604 | if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 605 | next: (void) close(fd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 606 | continue; |
| 607 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 608 | if (showprogress) { |
| 609 | totalbytes = stb.st_size; |
| 610 | progressmeter(-1); |
| 611 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 612 | /* Keep writing after an error so that we stay sync'd up. */ |
| 613 | for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { |
| 614 | amt = bp->cnt; |
| 615 | if (i + amt > stb.st_size) |
| 616 | amt = stb.st_size - i; |
| 617 | if (!haderr) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 618 | result = atomicio(read, fd, bp->buf, amt); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 619 | if (result != amt) |
| 620 | haderr = result >= 0 ? EIO : errno; |
| 621 | } |
| 622 | if (haderr) |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 623 | (void) atomicio(write, remout, bp->buf, amt); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 624 | else { |
Damien Miller | 864ea59 | 1999-12-15 11:04:25 +1100 | [diff] [blame] | 625 | result = atomicio(write, remout, bp->buf, amt); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 626 | if (result != amt) |
| 627 | haderr = result >= 0 ? EIO : errno; |
| 628 | statbytes += result; |
| 629 | } |
| 630 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 631 | if (showprogress) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 632 | progressmeter(1); |
| 633 | |
| 634 | if (close(fd) < 0 && !haderr) |
| 635 | haderr = errno; |
| 636 | if (!haderr) |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 637 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 638 | else |
| 639 | run_err("%s: %s", name, strerror(haderr)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 640 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
| 644 | void |
| 645 | rsource(name, statp) |
| 646 | char *name; |
| 647 | struct stat *statp; |
| 648 | { |
| 649 | DIR *dirp; |
| 650 | struct dirent *dp; |
| 651 | char *last, *vect[1], path[1100]; |
| 652 | |
| 653 | if (!(dirp = opendir(name))) { |
| 654 | run_err("%s: %s", name, strerror(errno)); |
| 655 | return; |
| 656 | } |
| 657 | last = strrchr(name, '/'); |
| 658 | if (last == 0) |
| 659 | last = name; |
| 660 | else |
| 661 | last++; |
| 662 | if (pflag) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 663 | (void) sprintf(path, "T%lu 0 %lu 0\n", |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 664 | (unsigned long) statp->st_mtime, |
| 665 | (unsigned long) statp->st_atime); |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 666 | (void) atomicio(write, remout, path, strlen(path)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 667 | if (response() < 0) { |
| 668 | closedir(dirp); |
| 669 | return; |
| 670 | } |
| 671 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 672 | (void) sprintf(path, "D%04o %d %.1024s\n", |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 673 | (unsigned int) (statp->st_mode & FILEMODEMASK), 0, last); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 674 | if (verbose_mode) |
| 675 | fprintf(stderr, "Entering directory: %s", path); |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 676 | (void) atomicio(write, remout, path, strlen(path)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 677 | if (response() < 0) { |
| 678 | closedir(dirp); |
| 679 | return; |
| 680 | } |
| 681 | while ((dp = readdir(dirp))) { |
| 682 | if (dp->d_ino == 0) |
| 683 | continue; |
| 684 | if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) |
| 685 | continue; |
| 686 | if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) { |
| 687 | run_err("%s/%s: name too long", name, dp->d_name); |
| 688 | continue; |
| 689 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 690 | (void) sprintf(path, "%s/%s", name, dp->d_name); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 691 | vect[0] = path; |
| 692 | source(1, vect); |
| 693 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 694 | (void) closedir(dirp); |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 695 | (void) atomicio(write, remout, "E\n", 2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 696 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | void |
| 700 | sink(argc, argv) |
| 701 | int argc; |
| 702 | char *argv[]; |
| 703 | { |
| 704 | static BUF buffer; |
| 705 | struct stat stb; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 706 | enum { |
| 707 | YES, NO, DISPLAYED |
| 708 | } wrerr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 709 | BUF *bp; |
| 710 | off_t i, j; |
| 711 | int amt, count, exists, first, mask, mode, ofd, omode; |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 712 | off_t size; |
| 713 | int setimes, targisdir, wrerrno = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 714 | char ch, *cp, *np, *targ, *why, *vect[1], buf[2048]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 715 | int dummy_usec; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 716 | struct timeval tv[2]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 717 | |
| 718 | #define SCREWUP(str) { why = str; goto screwup; } |
| 719 | |
| 720 | setimes = targisdir = 0; |
| 721 | mask = umask(0); |
| 722 | if (!pflag) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 723 | (void) umask(mask); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 724 | if (argc != 1) { |
| 725 | run_err("ambiguous target"); |
| 726 | exit(1); |
| 727 | } |
| 728 | targ = *argv; |
| 729 | if (targetshouldbedirectory) |
| 730 | verifydir(targ); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 731 | |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 732 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 733 | if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) |
| 734 | targisdir = 1; |
| 735 | for (first = 1;; first = 0) { |
| 736 | cp = buf; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 737 | if (atomicio(read, remin, cp, 1) <= 0) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 738 | return; |
| 739 | if (*cp++ == '\n') |
| 740 | SCREWUP("unexpected <newline>"); |
| 741 | do { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 742 | if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 743 | SCREWUP("lost connection"); |
| 744 | *cp++ = ch; |
| 745 | } while (cp < &buf[sizeof(buf) - 1] && ch != '\n'); |
| 746 | *cp = 0; |
| 747 | |
| 748 | if (buf[0] == '\01' || buf[0] == '\02') { |
| 749 | if (iamremote == 0) |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 750 | (void) atomicio(write, STDERR_FILENO, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 751 | buf + 1, strlen(buf + 1)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 752 | if (buf[0] == '\02') |
| 753 | exit(1); |
| 754 | ++errs; |
| 755 | continue; |
| 756 | } |
| 757 | if (buf[0] == 'E') { |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 758 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 759 | return; |
| 760 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 761 | if (ch == '\n') |
| 762 | *--cp = 0; |
| 763 | |
| 764 | #define getnum(t) (t) = 0; \ |
| 765 | while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0'); |
| 766 | cp = buf; |
| 767 | if (*cp == 'T') { |
| 768 | setimes++; |
| 769 | cp++; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 770 | getnum(tv[1].tv_sec); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 771 | if (*cp++ != ' ') |
| 772 | SCREWUP("mtime.sec not delimited"); |
| 773 | getnum(dummy_usec); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 774 | tv[1].tv_usec = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 775 | if (*cp++ != ' ') |
| 776 | SCREWUP("mtime.usec not delimited"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 777 | getnum(tv[0].tv_sec); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 778 | if (*cp++ != ' ') |
| 779 | SCREWUP("atime.sec not delimited"); |
| 780 | getnum(dummy_usec); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 781 | tv[0].tv_usec = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 782 | if (*cp++ != '\0') |
| 783 | SCREWUP("atime.usec not delimited"); |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 784 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 785 | continue; |
| 786 | } |
| 787 | if (*cp != 'C' && *cp != 'D') { |
| 788 | /* |
| 789 | * Check for the case "rcp remote:foo\* local:bar". |
| 790 | * In this case, the line "No match." can be returned |
| 791 | * by the shell before the rcp command on the remote is |
| 792 | * executed so the ^Aerror_message convention isn't |
| 793 | * followed. |
| 794 | */ |
| 795 | if (first) { |
| 796 | run_err("%s", cp); |
| 797 | exit(1); |
| 798 | } |
| 799 | SCREWUP("expected control record"); |
| 800 | } |
| 801 | mode = 0; |
| 802 | for (++cp; cp < buf + 5; cp++) { |
| 803 | if (*cp < '0' || *cp > '7') |
| 804 | SCREWUP("bad mode"); |
| 805 | mode = (mode << 3) | (*cp - '0'); |
| 806 | } |
| 807 | if (*cp++ != ' ') |
| 808 | SCREWUP("mode not delimited"); |
| 809 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 810 | for (size = 0; *cp >= '0' && *cp <= '9';) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 811 | size = size * 10 + (*cp++ - '0'); |
| 812 | if (*cp++ != ' ') |
| 813 | SCREWUP("size not delimited"); |
| 814 | if (targisdir) { |
| 815 | static char *namebuf; |
| 816 | static int cursize; |
| 817 | size_t need; |
| 818 | |
| 819 | need = strlen(targ) + strlen(cp) + 250; |
| 820 | if (need > cursize) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 821 | namebuf = xmalloc(need); |
| 822 | (void) sprintf(namebuf, "%s%s%s", targ, |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 823 | *targ ? "/" : "", cp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 824 | np = namebuf; |
| 825 | } else |
| 826 | np = targ; |
| 827 | curfile = cp; |
| 828 | exists = stat(np, &stb) == 0; |
| 829 | if (buf[0] == 'D') { |
| 830 | int mod_flag = pflag; |
| 831 | if (exists) { |
| 832 | if (!S_ISDIR(stb.st_mode)) { |
| 833 | errno = ENOTDIR; |
| 834 | goto bad; |
| 835 | } |
| 836 | if (pflag) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 837 | (void) chmod(np, mode); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 838 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 839 | /* Handle copying from a read-only |
| 840 | directory */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 841 | mod_flag = 1; |
| 842 | if (mkdir(np, mode | S_IRWXU) < 0) |
| 843 | goto bad; |
| 844 | } |
| 845 | vect[0] = np; |
| 846 | sink(1, vect); |
| 847 | if (setimes) { |
| 848 | setimes = 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 849 | if (utimes(np, tv) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 850 | run_err("%s: set times: %s", |
| 851 | np, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 852 | } |
| 853 | if (mod_flag) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 854 | (void) chmod(np, mode); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 855 | continue; |
| 856 | } |
| 857 | omode = mode; |
| 858 | mode |= S_IWRITE; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 859 | if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 860 | bad: run_err("%s: %s", np, strerror(errno)); |
| 861 | continue; |
| 862 | } |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 863 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 864 | if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 865 | (void) close(ofd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 866 | continue; |
| 867 | } |
| 868 | cp = bp->buf; |
| 869 | wrerr = NO; |
| 870 | |
| 871 | if (showprogress) { |
| 872 | totalbytes = size; |
| 873 | progressmeter(-1); |
| 874 | } |
| 875 | statbytes = 0; |
| 876 | for (count = i = 0; i < size; i += 4096) { |
| 877 | amt = 4096; |
| 878 | if (i + amt > size) |
| 879 | amt = size - i; |
| 880 | count += amt; |
| 881 | do { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 882 | j = atomicio(read, remin, cp, amt); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 883 | if (j <= 0) { |
| 884 | run_err("%s", j ? strerror(errno) : |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 885 | "dropped connection"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 886 | exit(1); |
| 887 | } |
| 888 | amt -= j; |
| 889 | cp += j; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 890 | statbytes += j; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 891 | } while (amt > 0); |
| 892 | if (count == bp->cnt) { |
| 893 | /* Keep reading so we stay sync'd up. */ |
| 894 | if (wrerr == NO) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 895 | j = atomicio(write, ofd, bp->buf, count); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 896 | if (j != count) { |
| 897 | wrerr = YES; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 898 | wrerrno = j >= 0 ? EIO : errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | count = 0; |
| 902 | cp = bp->buf; |
| 903 | } |
| 904 | } |
| 905 | if (showprogress) |
| 906 | progressmeter(1); |
| 907 | if (count != 0 && wrerr == NO && |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 908 | (j = atomicio(write, ofd, bp->buf, count)) != count) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 909 | wrerr = YES; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 910 | wrerrno = j >= 0 ? EIO : errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 911 | } |
| 912 | #if 0 |
| 913 | if (ftruncate(ofd, size)) { |
| 914 | run_err("%s: truncate: %s", np, strerror(errno)); |
| 915 | wrerr = DISPLAYED; |
| 916 | } |
| 917 | #endif |
| 918 | if (pflag) { |
| 919 | if (exists || omode != mode) |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 920 | #ifdef HAVE_FCHMOD |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 921 | if (fchmod(ofd, omode)) |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 922 | #else /* HAVE_FCHMOD */ |
| 923 | if (chmod(np, omode)) |
| 924 | #endif /* HAVE_FCHMOD */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 925 | run_err("%s: set mode: %s", |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 926 | np, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 927 | } else { |
| 928 | if (!exists && omode != mode) |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 929 | #ifdef HAVE_FCHMOD |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 930 | if (fchmod(ofd, omode & ~mask)) |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 931 | #else /* HAVE_FCHMOD */ |
| 932 | if (chmod(np, omode & ~mask)) |
| 933 | #endif /* HAVE_FCHMOD */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 934 | run_err("%s: set mode: %s", |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 935 | np, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 936 | } |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 937 | if (close(ofd) == -1) { |
| 938 | wrerr = YES; |
| 939 | wrerrno = errno; |
| 940 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 941 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 942 | if (setimes && wrerr == NO) { |
| 943 | setimes = 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 944 | if (utimes(np, tv) < 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 945 | run_err("%s: set times: %s", |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 946 | np, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 947 | wrerr = DISPLAYED; |
| 948 | } |
| 949 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 950 | switch (wrerr) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 951 | case YES: |
| 952 | run_err("%s: %s", np, strerror(wrerrno)); |
| 953 | break; |
| 954 | case NO: |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 955 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 956 | break; |
| 957 | case DISPLAYED: |
| 958 | break; |
| 959 | } |
| 960 | } |
| 961 | screwup: |
| 962 | run_err("protocol error: %s", why); |
| 963 | exit(1); |
| 964 | } |
| 965 | |
| 966 | int |
| 967 | response() |
| 968 | { |
| 969 | char ch, *cp, resp, rbuf[2048]; |
| 970 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 971 | if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 972 | lostconn(0); |
| 973 | |
| 974 | cp = rbuf; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 975 | switch (resp) { |
| 976 | case 0: /* ok */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 977 | return (0); |
| 978 | default: |
| 979 | *cp++ = resp; |
| 980 | /* FALLTHROUGH */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 981 | case 1: /* error, followed by error msg */ |
| 982 | case 2: /* fatal error, "" */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 983 | do { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 984 | if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 985 | lostconn(0); |
| 986 | *cp++ = ch; |
| 987 | } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n'); |
| 988 | |
| 989 | if (!iamremote) |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 990 | (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 991 | ++errs; |
| 992 | if (resp == 1) |
| 993 | return (-1); |
| 994 | exit(1); |
| 995 | } |
| 996 | /* NOTREACHED */ |
| 997 | } |
| 998 | |
| 999 | void |
| 1000 | usage() |
| 1001 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1002 | (void) fprintf(stderr, "usage: scp " |
| 1003 | "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n" |
| 1004 | " scp [options] f1 ... fn directory\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1005 | exit(1); |
| 1006 | } |
| 1007 | |
| 1008 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1009 | run_err(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1010 | { |
| 1011 | static FILE *fp; |
| 1012 | va_list ap; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1013 | |
| 1014 | ++errs; |
| 1015 | if (fp == NULL && !(fp = fdopen(remout, "w"))) |
| 1016 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1017 | (void) fprintf(fp, "%c", 0x01); |
| 1018 | (void) fprintf(fp, "scp: "); |
Damien Miller | 03783f0 | 1999-12-31 09:16:40 +1100 | [diff] [blame] | 1019 | va_start(ap, fmt); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1020 | (void) vfprintf(fp, fmt, ap); |
Damien Miller | 03783f0 | 1999-12-31 09:16:40 +1100 | [diff] [blame] | 1021 | va_end(ap); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1022 | (void) fprintf(fp, "\n"); |
| 1023 | (void) fflush(fp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1024 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1025 | if (!iamremote) { |
Damien Miller | 03783f0 | 1999-12-31 09:16:40 +1100 | [diff] [blame] | 1026 | va_start(ap, fmt); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1027 | vfprintf(stderr, fmt, ap); |
Damien Miller | 03783f0 | 1999-12-31 09:16:40 +1100 | [diff] [blame] | 1028 | va_end(ap); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1029 | fprintf(stderr, "\n"); |
| 1030 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1031 | } |
| 1032 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1033 | char * |
| 1034 | colon(cp) |
| 1035 | char *cp; |
| 1036 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1037 | int flag = 0; |
| 1038 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1039 | if (*cp == ':') /* Leading colon is part of file name. */ |
| 1040 | return (0); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1041 | if (*cp == '[') |
| 1042 | flag = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1043 | |
| 1044 | for (; *cp; ++cp) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1045 | if (*cp == '@' && *(cp+1) == '[') |
| 1046 | flag = 1; |
| 1047 | if (*cp == ']' && *(cp+1) == ':' && flag) |
| 1048 | return (cp+1); |
| 1049 | if (*cp == ':' && !flag) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1050 | return (cp); |
| 1051 | if (*cp == '/') |
| 1052 | return (0); |
| 1053 | } |
| 1054 | return (0); |
| 1055 | } |
| 1056 | |
| 1057 | void |
| 1058 | verifydir(cp) |
| 1059 | char *cp; |
| 1060 | { |
| 1061 | struct stat stb; |
| 1062 | |
| 1063 | if (!stat(cp, &stb)) { |
| 1064 | if (S_ISDIR(stb.st_mode)) |
| 1065 | return; |
| 1066 | errno = ENOTDIR; |
| 1067 | } |
| 1068 | run_err("%s: %s", cp, strerror(errno)); |
| 1069 | exit(1); |
| 1070 | } |
| 1071 | |
| 1072 | int |
| 1073 | okname(cp0) |
| 1074 | char *cp0; |
| 1075 | { |
| 1076 | int c; |
| 1077 | char *cp; |
| 1078 | |
| 1079 | cp = cp0; |
| 1080 | do { |
| 1081 | c = *cp; |
| 1082 | if (c & 0200) |
| 1083 | goto bad; |
| 1084 | if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') |
| 1085 | goto bad; |
| 1086 | } while (*++cp); |
| 1087 | return (1); |
| 1088 | |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 1089 | bad: fprintf(stderr, "%s: invalid user name\n", cp0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1090 | return (0); |
| 1091 | } |
| 1092 | |
| 1093 | BUF * |
| 1094 | allocbuf(bp, fd, blksize) |
| 1095 | BUF *bp; |
| 1096 | int fd, blksize; |
| 1097 | { |
| 1098 | size_t size; |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1099 | #ifdef HAVE_ST_BLKSIZE |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1100 | struct stat stb; |
| 1101 | |
| 1102 | if (fstat(fd, &stb) < 0) { |
| 1103 | run_err("fstat: %s", strerror(errno)); |
| 1104 | return (0); |
| 1105 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1106 | if (stb.st_blksize == 0) |
| 1107 | size = blksize; |
| 1108 | else |
| 1109 | size = blksize + (stb.st_blksize - blksize % stb.st_blksize) % |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1110 | stb.st_blksize; |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1111 | #else /* HAVE_ST_BLKSIZE */ |
| 1112 | size = blksize; |
| 1113 | #endif /* HAVE_ST_BLKSIZE */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1114 | if (bp->cnt >= size) |
| 1115 | return (bp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1116 | if (bp->buf == NULL) |
| 1117 | bp->buf = xmalloc(size); |
| 1118 | else |
| 1119 | bp->buf = xrealloc(bp->buf, size); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1120 | bp->cnt = size; |
| 1121 | return (bp); |
| 1122 | } |
| 1123 | |
| 1124 | void |
| 1125 | lostconn(signo) |
| 1126 | int signo; |
| 1127 | { |
| 1128 | if (!iamremote) |
| 1129 | fprintf(stderr, "lost connection\n"); |
| 1130 | exit(1); |
| 1131 | } |
| 1132 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1133 | |
| 1134 | void |
| 1135 | alarmtimer(int wait) |
| 1136 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1137 | struct itimerval itv; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1138 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1139 | itv.it_value.tv_sec = wait; |
| 1140 | itv.it_value.tv_usec = 0; |
| 1141 | itv.it_interval = itv.it_value; |
| 1142 | setitimer(ITIMER_REAL, &itv, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | void |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 1146 | updateprogressmeter(int ignore) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1147 | { |
| 1148 | int save_errno = errno; |
| 1149 | |
| 1150 | progressmeter(0); |
| 1151 | errno = save_errno; |
| 1152 | } |
| 1153 | |
Damien Miller | 81428f9 | 1999-11-18 09:28:11 +1100 | [diff] [blame] | 1154 | int |
| 1155 | foregroundproc() |
| 1156 | { |
| 1157 | static pid_t pgrp = -1; |
| 1158 | int ctty_pgrp; |
| 1159 | |
| 1160 | if (pgrp == -1) |
| 1161 | pgrp = getpgrp(); |
| 1162 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1163 | #ifdef HAVE_CYGWIN |
| 1164 | /* |
| 1165 | * Cygwin only supports tcgetpgrp() for getting the controlling tty |
| 1166 | * currently. |
| 1167 | */ |
| 1168 | return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 && |
| 1169 | ctty_pgrp == pgrp); |
| 1170 | #else |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1171 | return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 && |
| 1172 | ctty_pgrp == pgrp)); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1173 | #endif |
Damien Miller | 81428f9 | 1999-11-18 09:28:11 +1100 | [diff] [blame] | 1174 | } |
| 1175 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1176 | void |
| 1177 | progressmeter(int flag) |
| 1178 | { |
| 1179 | static const char prefixes[] = " KMGTP"; |
| 1180 | static struct timeval lastupdate; |
| 1181 | static off_t lastsize; |
| 1182 | struct timeval now, td, wait; |
| 1183 | off_t cursize, abbrevsize; |
| 1184 | double elapsed; |
Damien Miller | 7c64ba3 | 1999-11-11 21:39:50 +1100 | [diff] [blame] | 1185 | int ratio, barlength, i, remaining; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1186 | char buf[256]; |
| 1187 | |
| 1188 | if (flag == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1189 | (void) gettimeofday(&start, (struct timezone *) 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1190 | lastupdate = start; |
| 1191 | lastsize = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1192 | } |
Damien Miller | 81428f9 | 1999-11-18 09:28:11 +1100 | [diff] [blame] | 1193 | if (foregroundproc() == 0) |
| 1194 | return; |
| 1195 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1196 | (void) gettimeofday(&now, (struct timezone *) 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1197 | cursize = statbytes; |
Damien Miller | a2d6efe | 1999-11-13 13:22:46 +1100 | [diff] [blame] | 1198 | if (totalbytes != 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1199 | ratio = 100.0 * cursize / totalbytes; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1200 | ratio = MAX(ratio, 0); |
| 1201 | ratio = MIN(ratio, 100); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1202 | } else |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1203 | ratio = 100; |
| 1204 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1205 | snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1206 | |
| 1207 | barlength = getttywidth() - 51; |
Damien Miller | 484118e | 2000-07-02 19:13:56 +1000 | [diff] [blame] | 1208 | barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1209 | if (barlength > 0) { |
| 1210 | i = barlength * ratio / 100; |
| 1211 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Damien Miller | 484118e | 2000-07-02 19:13:56 +1000 | [diff] [blame] | 1212 | "|%.*s%*s|", i, BAR, barlength - i, ""); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1213 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1214 | i = 0; |
| 1215 | abbrevsize = cursize; |
| 1216 | while (abbrevsize >= 100000 && i < sizeof(prefixes)) { |
| 1217 | i++; |
| 1218 | abbrevsize >>= 10; |
| 1219 | } |
Damien Miller | 864ea59 | 1999-12-15 11:04:25 +1100 | [diff] [blame] | 1220 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ", |
| 1221 | (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1222 | 'B'); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1223 | |
| 1224 | timersub(&now, &lastupdate, &wait); |
| 1225 | if (cursize > lastsize) { |
| 1226 | lastupdate = now; |
| 1227 | lastsize = cursize; |
| 1228 | if (wait.tv_sec >= STALLTIME) { |
| 1229 | start.tv_sec += wait.tv_sec; |
| 1230 | start.tv_usec += wait.tv_usec; |
| 1231 | } |
| 1232 | wait.tv_sec = 0; |
| 1233 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1234 | timersub(&now, &start, &td); |
| 1235 | elapsed = td.tv_sec + (td.tv_usec / 1000000.0); |
| 1236 | |
| 1237 | if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) { |
| 1238 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1239 | " --:-- ETA"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1240 | } else if (wait.tv_sec >= STALLTIME) { |
| 1241 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1242 | " - stalled -"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1243 | } else { |
Damien Miller | 8bb73be | 2000-04-19 16:26:12 +1000 | [diff] [blame] | 1244 | if (flag != 1) |
| 1245 | remaining = |
| 1246 | (int)(totalbytes / (statbytes / elapsed) - elapsed); |
| 1247 | else |
| 1248 | remaining = elapsed; |
| 1249 | |
Damien Miller | 01ab4a2 | 1999-10-28 15:23:30 +1000 | [diff] [blame] | 1250 | i = remaining / 3600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1251 | if (i) |
| 1252 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1253 | "%2d:", i); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1254 | else |
| 1255 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1256 | " "); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1257 | i = remaining % 3600; |
| 1258 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1259 | "%02d:%02d%s", i / 60, i % 60, |
| 1260 | (flag != 1) ? " ETA" : " "); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1261 | } |
| 1262 | atomicio(write, fileno(stdout), buf, strlen(buf)); |
| 1263 | |
| 1264 | if (flag == -1) { |
Damien Miller | 864ea59 | 1999-12-15 11:04:25 +1100 | [diff] [blame] | 1265 | struct sigaction sa; |
| 1266 | sa.sa_handler = updateprogressmeter; |
Damien Miller | 77aba9d | 2000-08-30 10:11:30 +1100 | [diff] [blame] | 1267 | sigemptyset((sigset_t *)&sa.sa_mask); |
Damien Miller | 615f939 | 2000-05-17 22:53:33 +1000 | [diff] [blame] | 1268 | #ifdef SA_RESTART |
Damien Miller | 864ea59 | 1999-12-15 11:04:25 +1100 | [diff] [blame] | 1269 | sa.sa_flags = SA_RESTART; |
Damien Miller | 615f939 | 2000-05-17 22:53:33 +1000 | [diff] [blame] | 1270 | #endif |
Damien Miller | 864ea59 | 1999-12-15 11:04:25 +1100 | [diff] [blame] | 1271 | sigaction(SIGALRM, &sa, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1272 | alarmtimer(1); |
| 1273 | } else if (flag == 1) { |
| 1274 | alarmtimer(0); |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 1275 | atomicio(write, fileno(stdout), "\n", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1276 | statbytes = 0; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | int |
| 1281 | getttywidth(void) |
| 1282 | { |
| 1283 | struct winsize winsize; |
| 1284 | |
| 1285 | if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1286 | return (winsize.ws_col ? winsize.ws_col : 80); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1287 | else |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1288 | return (80); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1289 | } |