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