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