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