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 | /* |
Ben Lindstrom | 92a2e38 | 2001-03-05 06:59:27 +0000 | [diff] [blame] | 17 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
| 18 | * Copyright (c) 1999 Aaron Campbell. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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 | ff2618c | 2001-03-29 00:43:54 +0000 | [diff] [blame] | 78 | RCSID("$OpenBSD: scp.c,v 1.64 2001/03/28 20:04:38 stevesk Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | #include "xmalloc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 81 | #include "atomicio.h" |
| 82 | #include "pathnames.h" |
| 83 | #include "log.h" |
Kevin Steves | 93c17d9 | 2001-02-18 03:55:16 +0000 | [diff] [blame] | 84 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 85 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 86 | #ifdef HAVE___PROGNAME |
| 87 | extern char *__progname; |
| 88 | #else |
| 89 | char *__progname; |
| 90 | #endif |
| 91 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 92 | /* For progressmeter() -- number of seconds before xfer considered "stalled" */ |
| 93 | #define STALLTIME 5 |
| 94 | |
Damien Miller | 484118e | 2000-07-02 19:13:56 +1000 | [diff] [blame] | 95 | /* Progress meter bar */ |
| 96 | #define BAR \ |
| 97 | "************************************************************"\ |
| 98 | "************************************************************"\ |
| 99 | "************************************************************"\ |
| 100 | "************************************************************" |
| 101 | #define MAX_BARLENGTH (sizeof(BAR) - 1) |
| 102 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | /* Visual statistics about files as they are transferred. */ |
| 104 | void progressmeter(int); |
| 105 | |
| 106 | /* Returns width of the terminal (for progress meter calculations). */ |
| 107 | int getttywidth(void); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 108 | 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] | 109 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 110 | /* setup arguments for the call to ssh */ |
| 111 | void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2))); |
| 112 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 113 | /* Time a transfer started. */ |
| 114 | static struct timeval start; |
| 115 | |
| 116 | /* Number of bytes of current file transferred so far. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 117 | volatile u_long statbytes; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | |
| 119 | /* Total size of current file. */ |
Damien Miller | a2d6efe | 1999-11-13 13:22:46 +1100 | [diff] [blame] | 120 | off_t totalbytes = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 121 | |
| 122 | /* Name of current file being transferred. */ |
| 123 | char *curfile; |
| 124 | |
| 125 | /* This is set to non-zero to enable verbose mode. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 126 | int verbose_mode = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 127 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 128 | /* This is set to zero if the progressmeter is not desired. */ |
| 129 | int showprogress = 1; |
| 130 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 131 | /* This is the program to execute for the secured connection. ("ssh" or -S) */ |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 132 | char *ssh_program = _PATH_SSH_PROGRAM; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 133 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 134 | /* This is the list of arguments that scp passes to ssh */ |
| 135 | struct { |
| 136 | char **list; |
| 137 | int num; |
| 138 | int nalloc; |
| 139 | } args; |
| 140 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 141 | /* |
| 142 | * This function executes the given command as the specified user on the |
| 143 | * given host. This returns < 0 if execution fails, and >= 0 otherwise. This |
| 144 | * assigns the input and output file descriptors on success. |
| 145 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 146 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 147 | int |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 148 | 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] | 149 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 150 | int pin[2], pout[2], reserved[2]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 152 | if (verbose_mode) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 153 | fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n", |
| 154 | ssh_program, host, remuser ? remuser : "(unspecified)", cmd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 155 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 156 | /* |
| 157 | * Reserve two descriptors so that the real pipes won't get |
| 158 | * descriptors 0 and 1 because that will screw up dup2 below. |
| 159 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 160 | pipe(reserved); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 162 | /* Create a socket pair for communicating with ssh. */ |
| 163 | if (pipe(pin) < 0) |
| 164 | fatal("pipe: %s", strerror(errno)); |
| 165 | if (pipe(pout) < 0) |
| 166 | fatal("pipe: %s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 167 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 168 | /* Free the reserved descriptors. */ |
| 169 | close(reserved[0]); |
| 170 | close(reserved[1]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 171 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 172 | /* 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] | 173 | if (fork() == 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 174 | /* Child. */ |
| 175 | close(pin[1]); |
| 176 | close(pout[0]); |
| 177 | dup2(pin[0], 0); |
| 178 | dup2(pout[1], 1); |
| 179 | close(pin[0]); |
| 180 | close(pout[1]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 181 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 182 | args.list[0] = ssh_program; |
| 183 | if (remuser != NULL) |
Damien Miller | e4041c9 | 2000-10-14 17:45:58 +1100 | [diff] [blame] | 184 | addargs("-l%s", remuser); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 185 | addargs("%s", host); |
| 186 | addargs("%s", cmd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 187 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 188 | execvp(ssh_program, args.list); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 189 | perror(ssh_program); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 190 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 191 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 192 | /* Parent. Close the other side, and return the local side. */ |
| 193 | close(pin[0]); |
| 194 | *fdout = pin[1]; |
| 195 | close(pout[1]); |
| 196 | *fdin = pout[0]; |
| 197 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 198 | } |
| 199 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 200 | typedef struct { |
| 201 | int cnt; |
| 202 | char *buf; |
| 203 | } BUF; |
| 204 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 205 | BUF *allocbuf(BUF *, int, int); |
| 206 | char *colon(char *); |
| 207 | void lostconn(int); |
| 208 | void nospace(void); |
| 209 | int okname(char *); |
| 210 | void run_err(const char *,...); |
| 211 | void verifydir(char *); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 212 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 213 | struct passwd *pwd; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 214 | uid_t userid; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 215 | int errs, remin, remout; |
| 216 | int pflag, iamremote, iamrecursive, targetshouldbedirectory; |
| 217 | |
| 218 | #define CMDNEEDS 64 |
| 219 | char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ |
| 220 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 221 | int response(void); |
| 222 | void rsource(char *, struct stat *); |
| 223 | void sink(int, char *[]); |
| 224 | void source(int, char *[]); |
| 225 | void tolocal(int, char *[]); |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 226 | char *cleanhostname(char *); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 227 | void toremote(char *, int, char *[]); |
| 228 | void usage(void); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 229 | |
| 230 | int |
| 231 | main(argc, argv) |
| 232 | int argc; |
| 233 | char *argv[]; |
| 234 | { |
| 235 | int ch, fflag, tflag; |
| 236 | char *targ; |
| 237 | extern char *optarg; |
| 238 | extern int optind; |
| 239 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 240 | __progname = get_progname(argv[0]); |
| 241 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 242 | args.list = NULL; |
| 243 | addargs("ssh"); /* overwritten with ssh_program */ |
| 244 | addargs("-x"); |
| 245 | addargs("-oFallBackToRsh no"); |
| 246 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 247 | fflag = tflag = 0; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 248 | while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 249 | switch (ch) { |
| 250 | /* User-visible flags. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 251 | case '4': |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 252 | case '6': |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 253 | case 'C': |
| 254 | addargs("-%c", ch); |
| 255 | break; |
| 256 | case 'o': |
| 257 | case 'c': |
| 258 | case 'i': |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 259 | addargs("-%c%s", ch, optarg); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 260 | break; |
| 261 | case 'P': |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 262 | addargs("-p%s", optarg); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 263 | break; |
| 264 | case 'B': |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 265 | addargs("-oBatchmode yes"); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 266 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 267 | case 'p': |
| 268 | pflag = 1; |
| 269 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 270 | case 'r': |
| 271 | iamrecursive = 1; |
| 272 | break; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 273 | case 'S': |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 274 | ssh_program = xstrdup(optarg); |
| 275 | break; |
| 276 | case 'v': |
| 277 | verbose_mode = 1; |
| 278 | break; |
| 279 | case 'q': |
| 280 | showprogress = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 281 | break; |
| 282 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 283 | /* Server options. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 284 | case 'd': |
| 285 | targetshouldbedirectory = 1; |
| 286 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 287 | case 'f': /* "from" */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 288 | iamremote = 1; |
| 289 | fflag = 1; |
| 290 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 291 | case 't': /* "to" */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 292 | iamremote = 1; |
| 293 | tflag = 1; |
| 294 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 295 | default: |
| 296 | usage(); |
| 297 | } |
| 298 | argc -= optind; |
| 299 | argv += optind; |
| 300 | |
| 301 | if ((pwd = getpwuid(userid = getuid())) == NULL) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 302 | fatal("unknown user %d", (int) userid); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 303 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 304 | if (!isatty(STDERR_FILENO)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 305 | showprogress = 0; |
| 306 | |
| 307 | remin = STDIN_FILENO; |
| 308 | remout = STDOUT_FILENO; |
| 309 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 310 | if (fflag) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 311 | /* Follow "protocol", send data. */ |
| 312 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 313 | source(argc, argv); |
| 314 | exit(errs != 0); |
| 315 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 316 | if (tflag) { |
| 317 | /* Receive data. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 318 | sink(argc, argv); |
| 319 | exit(errs != 0); |
| 320 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 321 | if (argc < 2) |
| 322 | usage(); |
| 323 | if (argc > 2) |
| 324 | targetshouldbedirectory = 1; |
| 325 | |
| 326 | remin = remout = -1; |
| 327 | /* Command to be executed on remote system using "ssh". */ |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 328 | (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s", |
| 329 | verbose_mode ? " -v" : "", |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 330 | iamrecursive ? " -r" : "", pflag ? " -p" : "", |
| 331 | targetshouldbedirectory ? " -d" : ""); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 332 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 333 | (void) signal(SIGPIPE, lostconn); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 334 | |
| 335 | if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */ |
| 336 | toremote(targ, argc, argv); |
| 337 | else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 338 | tolocal(argc, argv); /* Dest is local host. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 339 | if (targetshouldbedirectory) |
| 340 | verifydir(argv[argc - 1]); |
| 341 | } |
| 342 | exit(errs != 0); |
| 343 | } |
| 344 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 345 | char * |
| 346 | cleanhostname(host) |
| 347 | char *host; |
| 348 | { |
| 349 | if (*host == '[' && host[strlen(host) - 1] == ']') { |
| 350 | host[strlen(host) - 1] = '\0'; |
| 351 | return (host + 1); |
| 352 | } else |
| 353 | return host; |
| 354 | } |
| 355 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 356 | void |
| 357 | toremote(targ, argc, argv) |
| 358 | char *targ, *argv[]; |
| 359 | int argc; |
| 360 | { |
| 361 | int i, len; |
| 362 | char *bp, *host, *src, *suser, *thost, *tuser; |
| 363 | |
| 364 | *targ++ = 0; |
| 365 | if (*targ == 0) |
| 366 | targ = "."; |
| 367 | |
| 368 | if ((thost = strchr(argv[argc - 1], '@'))) { |
| 369 | /* user@host */ |
| 370 | *thost++ = 0; |
| 371 | tuser = argv[argc - 1]; |
| 372 | if (*tuser == '\0') |
| 373 | tuser = NULL; |
| 374 | else if (!okname(tuser)) |
| 375 | exit(1); |
| 376 | } else { |
| 377 | thost = argv[argc - 1]; |
| 378 | tuser = NULL; |
| 379 | } |
| 380 | |
| 381 | for (i = 0; i < argc - 1; i++) { |
| 382 | src = colon(argv[i]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 383 | if (src) { /* remote to remote */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 384 | *src++ = 0; |
| 385 | if (*src == 0) |
| 386 | src = "."; |
| 387 | host = strchr(argv[i], '@'); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 388 | len = strlen(ssh_program) + strlen(argv[i]) + |
| 389 | strlen(src) + (tuser ? strlen(tuser) : 0) + |
| 390 | strlen(thost) + strlen(targ) + CMDNEEDS + 32; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 391 | bp = xmalloc(len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 392 | if (host) { |
| 393 | *host++ = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 394 | host = cleanhostname(host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 395 | suser = argv[i]; |
| 396 | if (*suser == '\0') |
| 397 | suser = pwd->pw_name; |
| 398 | else if (!okname(suser)) |
| 399 | continue; |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 400 | snprintf(bp, len, |
| 401 | "%s%s -x -o'FallBackToRsh no' -n " |
| 402 | "-l %s %s %s %s '%s%s%s:%s'", |
Kevin Steves | 7d00ba4 | 2000-12-15 23:03:10 +0000 | [diff] [blame] | 403 | ssh_program, verbose_mode ? " -v" : "", |
| 404 | suser, host, cmd, src, |
| 405 | tuser ? tuser : "", tuser ? "@" : "", |
| 406 | thost, targ); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 407 | } else { |
| 408 | host = cleanhostname(argv[i]); |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 409 | snprintf(bp, len, |
| 410 | "exec %s%s -x -o'FallBackToRsh no' -n %s " |
| 411 | "%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); |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 425 | (void) snprintf(bp, len, "%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); |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 452 | (void) snprintf(bp, len, "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); |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 479 | (void) snprintf(bp, len, "%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 | */ |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 536 | (void) snprintf(buf, sizeof 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) |
Damien Miller | 753b1c0 | 2001-03-19 12:56:14 +1100 | [diff] [blame] | 544 | #ifdef HAVE_LONG_LONG_INT |
Ben Lindstrom | a4c5766 | 2001-03-17 00:10:20 +0000 | [diff] [blame] | 545 | snprintf(buf, sizeof buf, "C%04o %lld %s\n", |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 546 | (u_int) (stb.st_mode & FILEMODEMASK), |
Ben Lindstrom | fea7278 | 2001-03-17 18:07:46 +0000 | [diff] [blame] | 547 | (long long) stb.st_size, last); |
Damien Miller | ffd0e10 | 2001-03-19 12:45:02 +1100 | [diff] [blame] | 548 | #else |
| 549 | /* XXX: Handle integer overflow? */ |
Ben Lindstrom | 8feff45 | 2001-03-19 03:09:40 +0000 | [diff] [blame] | 550 | snprintf(buf, sizeof buf, "C%04o %lu %s\n", |
Damien Miller | ffd0e10 | 2001-03-19 12:45:02 +1100 | [diff] [blame] | 551 | (u_int) (stb.st_mode & FILEMODEMASK), |
Ben Lindstrom | 8feff45 | 2001-03-19 03:09:40 +0000 | [diff] [blame] | 552 | (u_long) stb.st_size, last); |
Damien Miller | ffd0e10 | 2001-03-19 12:45:02 +1100 | [diff] [blame] | 553 | #endif |
| 554 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 555 | if (verbose_mode) { |
| 556 | fprintf(stderr, "Sending file modes: %s", buf); |
| 557 | fflush(stderr); |
| 558 | } |
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 | if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 563 | next: (void) close(fd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 564 | continue; |
| 565 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 566 | if (showprogress) { |
| 567 | totalbytes = stb.st_size; |
| 568 | progressmeter(-1); |
| 569 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 570 | /* Keep writing after an error so that we stay sync'd up. */ |
| 571 | for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { |
| 572 | amt = bp->cnt; |
| 573 | if (i + amt > stb.st_size) |
| 574 | amt = stb.st_size - i; |
| 575 | if (!haderr) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 576 | result = atomicio(read, fd, bp->buf, amt); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 577 | if (result != amt) |
| 578 | haderr = result >= 0 ? EIO : errno; |
| 579 | } |
| 580 | if (haderr) |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 581 | (void) atomicio(write, remout, bp->buf, amt); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 582 | else { |
Damien Miller | 864ea59 | 1999-12-15 11:04:25 +1100 | [diff] [blame] | 583 | result = atomicio(write, remout, bp->buf, amt); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 584 | if (result != amt) |
| 585 | haderr = result >= 0 ? EIO : errno; |
| 586 | statbytes += result; |
| 587 | } |
| 588 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 589 | if (showprogress) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 590 | progressmeter(1); |
| 591 | |
| 592 | if (close(fd) < 0 && !haderr) |
| 593 | haderr = errno; |
| 594 | if (!haderr) |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 595 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 596 | else |
| 597 | run_err("%s: %s", name, strerror(haderr)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 598 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | void |
| 603 | rsource(name, statp) |
| 604 | char *name; |
| 605 | struct stat *statp; |
| 606 | { |
| 607 | DIR *dirp; |
| 608 | struct dirent *dp; |
| 609 | char *last, *vect[1], path[1100]; |
| 610 | |
| 611 | if (!(dirp = opendir(name))) { |
| 612 | run_err("%s: %s", name, strerror(errno)); |
| 613 | return; |
| 614 | } |
| 615 | last = strrchr(name, '/'); |
| 616 | if (last == 0) |
| 617 | last = name; |
| 618 | else |
| 619 | last++; |
| 620 | if (pflag) { |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 621 | (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n", |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 622 | (u_long) statp->st_mtime, |
| 623 | (u_long) statp->st_atime); |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 624 | (void) atomicio(write, remout, path, strlen(path)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 625 | if (response() < 0) { |
| 626 | closedir(dirp); |
| 627 | return; |
| 628 | } |
| 629 | } |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 630 | (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n", |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 631 | (u_int) (statp->st_mode & FILEMODEMASK), 0, last); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 632 | if (verbose_mode) |
| 633 | fprintf(stderr, "Entering directory: %s", path); |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 634 | (void) atomicio(write, remout, path, strlen(path)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 635 | if (response() < 0) { |
| 636 | closedir(dirp); |
| 637 | return; |
| 638 | } |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 639 | while ((dp = readdir(dirp)) != NULL) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 640 | if (dp->d_ino == 0) |
| 641 | continue; |
| 642 | if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) |
| 643 | continue; |
| 644 | if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) { |
| 645 | run_err("%s/%s: name too long", name, dp->d_name); |
| 646 | continue; |
| 647 | } |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 648 | (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 649 | vect[0] = path; |
| 650 | source(1, vect); |
| 651 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 652 | (void) closedir(dirp); |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 653 | (void) atomicio(write, remout, "E\n", 2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 654 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void |
| 658 | sink(argc, argv) |
| 659 | int argc; |
| 660 | char *argv[]; |
| 661 | { |
| 662 | static BUF buffer; |
| 663 | struct stat stb; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 664 | enum { |
| 665 | YES, NO, DISPLAYED |
| 666 | } wrerr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 667 | BUF *bp; |
| 668 | off_t i, j; |
| 669 | int amt, count, exists, first, mask, mode, ofd, omode; |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 670 | off_t size; |
| 671 | int setimes, targisdir, wrerrno = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 672 | char ch, *cp, *np, *targ, *why, *vect[1], buf[2048]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 673 | int dummy_usec; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 674 | struct timeval tv[2]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 675 | |
| 676 | #define SCREWUP(str) { why = str; goto screwup; } |
| 677 | |
| 678 | setimes = targisdir = 0; |
| 679 | mask = umask(0); |
| 680 | if (!pflag) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 681 | (void) umask(mask); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 682 | if (argc != 1) { |
| 683 | run_err("ambiguous target"); |
| 684 | exit(1); |
| 685 | } |
| 686 | targ = *argv; |
| 687 | if (targetshouldbedirectory) |
| 688 | verifydir(targ); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 689 | |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 690 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 691 | if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) |
| 692 | targisdir = 1; |
| 693 | for (first = 1;; first = 0) { |
| 694 | cp = buf; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 695 | if (atomicio(read, remin, cp, 1) <= 0) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 696 | return; |
| 697 | if (*cp++ == '\n') |
| 698 | SCREWUP("unexpected <newline>"); |
| 699 | do { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 700 | if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 701 | SCREWUP("lost connection"); |
| 702 | *cp++ = ch; |
| 703 | } while (cp < &buf[sizeof(buf) - 1] && ch != '\n'); |
| 704 | *cp = 0; |
| 705 | |
| 706 | if (buf[0] == '\01' || buf[0] == '\02') { |
| 707 | if (iamremote == 0) |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 708 | (void) atomicio(write, STDERR_FILENO, |
Kevin Steves | 7d00ba4 | 2000-12-15 23:03:10 +0000 | [diff] [blame] | 709 | buf + 1, strlen(buf + 1)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 710 | if (buf[0] == '\02') |
| 711 | exit(1); |
| 712 | ++errs; |
| 713 | continue; |
| 714 | } |
| 715 | if (buf[0] == 'E') { |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 716 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 717 | return; |
| 718 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 719 | if (ch == '\n') |
| 720 | *--cp = 0; |
| 721 | |
| 722 | #define getnum(t) (t) = 0; \ |
| 723 | while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0'); |
| 724 | cp = buf; |
| 725 | if (*cp == 'T') { |
| 726 | setimes++; |
| 727 | cp++; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 728 | getnum(tv[1].tv_sec); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 729 | if (*cp++ != ' ') |
| 730 | SCREWUP("mtime.sec not delimited"); |
| 731 | getnum(dummy_usec); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 732 | tv[1].tv_usec = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 733 | if (*cp++ != ' ') |
| 734 | SCREWUP("mtime.usec not delimited"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 735 | getnum(tv[0].tv_sec); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 736 | if (*cp++ != ' ') |
| 737 | SCREWUP("atime.sec not delimited"); |
| 738 | getnum(dummy_usec); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 739 | tv[0].tv_usec = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 740 | if (*cp++ != '\0') |
| 741 | SCREWUP("atime.usec not delimited"); |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 742 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 743 | continue; |
| 744 | } |
| 745 | if (*cp != 'C' && *cp != 'D') { |
| 746 | /* |
| 747 | * Check for the case "rcp remote:foo\* local:bar". |
| 748 | * In this case, the line "No match." can be returned |
| 749 | * by the shell before the rcp command on the remote is |
| 750 | * executed so the ^Aerror_message convention isn't |
| 751 | * followed. |
| 752 | */ |
| 753 | if (first) { |
| 754 | run_err("%s", cp); |
| 755 | exit(1); |
| 756 | } |
| 757 | SCREWUP("expected control record"); |
| 758 | } |
| 759 | mode = 0; |
| 760 | for (++cp; cp < buf + 5; cp++) { |
| 761 | if (*cp < '0' || *cp > '7') |
| 762 | SCREWUP("bad mode"); |
| 763 | mode = (mode << 3) | (*cp - '0'); |
| 764 | } |
| 765 | if (*cp++ != ' ') |
| 766 | SCREWUP("mode not delimited"); |
| 767 | |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 768 | for (size = 0; isdigit(*cp);) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 769 | size = size * 10 + (*cp++ - '0'); |
| 770 | if (*cp++ != ' ') |
| 771 | SCREWUP("size not delimited"); |
| 772 | if (targisdir) { |
| 773 | static char *namebuf; |
| 774 | static int cursize; |
| 775 | size_t need; |
| 776 | |
| 777 | need = strlen(targ) + strlen(cp) + 250; |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 778 | if (need > cursize) { |
| 779 | if (namebuf) |
| 780 | xfree(namebuf); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 781 | namebuf = xmalloc(need); |
Ben Lindstrom | f6b7b09 | 2001-02-09 01:23:39 +0000 | [diff] [blame] | 782 | cursize = need; |
| 783 | } |
| 784 | (void) snprintf(namebuf, need, "%s%s%s", targ, |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 785 | *targ ? "/" : "", cp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 786 | np = namebuf; |
| 787 | } else |
| 788 | np = targ; |
| 789 | curfile = cp; |
| 790 | exists = stat(np, &stb) == 0; |
| 791 | if (buf[0] == 'D') { |
| 792 | int mod_flag = pflag; |
| 793 | if (exists) { |
| 794 | if (!S_ISDIR(stb.st_mode)) { |
| 795 | errno = ENOTDIR; |
| 796 | goto bad; |
| 797 | } |
| 798 | if (pflag) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 799 | (void) chmod(np, mode); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 800 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 801 | /* Handle copying from a read-only |
| 802 | directory */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 803 | mod_flag = 1; |
| 804 | if (mkdir(np, mode | S_IRWXU) < 0) |
| 805 | goto bad; |
| 806 | } |
Ben Lindstrom | 550bc54 | 2001-02-10 21:50:00 +0000 | [diff] [blame] | 807 | vect[0] = xstrdup(np); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 808 | sink(1, vect); |
| 809 | if (setimes) { |
| 810 | setimes = 0; |
Damien Miller | 225736c | 2001-02-19 21:51:08 +1100 | [diff] [blame] | 811 | if (utimes(vect[0], tv) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 812 | run_err("%s: set times: %s", |
Damien Miller | 225736c | 2001-02-19 21:51:08 +1100 | [diff] [blame] | 813 | vect[0], strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 814 | } |
| 815 | if (mod_flag) |
Damien Miller | 225736c | 2001-02-19 21:51:08 +1100 | [diff] [blame] | 816 | (void) chmod(vect[0], mode); |
| 817 | if (vect[0]) |
| 818 | xfree(vect[0]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 819 | continue; |
| 820 | } |
| 821 | omode = mode; |
| 822 | mode |= S_IWRITE; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 823 | if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 824 | bad: run_err("%s: %s", np, strerror(errno)); |
| 825 | continue; |
| 826 | } |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 827 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 828 | if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 829 | (void) close(ofd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 830 | continue; |
| 831 | } |
| 832 | cp = bp->buf; |
| 833 | wrerr = NO; |
| 834 | |
| 835 | if (showprogress) { |
| 836 | totalbytes = size; |
| 837 | progressmeter(-1); |
| 838 | } |
| 839 | statbytes = 0; |
| 840 | for (count = i = 0; i < size; i += 4096) { |
| 841 | amt = 4096; |
| 842 | if (i + amt > size) |
| 843 | amt = size - i; |
| 844 | count += amt; |
| 845 | do { |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 846 | j = read(remin, cp, amt); |
| 847 | if (j == -1 && (errno == EINTR || errno == EAGAIN)) { |
| 848 | continue; |
| 849 | } else if (j <= 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 850 | run_err("%s", j ? strerror(errno) : |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 851 | "dropped connection"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 852 | exit(1); |
| 853 | } |
| 854 | amt -= j; |
| 855 | cp += j; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 856 | statbytes += j; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 857 | } while (amt > 0); |
| 858 | if (count == bp->cnt) { |
| 859 | /* Keep reading so we stay sync'd up. */ |
| 860 | if (wrerr == NO) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 861 | j = atomicio(write, ofd, bp->buf, count); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 862 | if (j != count) { |
| 863 | wrerr = YES; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 864 | wrerrno = j >= 0 ? EIO : errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | count = 0; |
| 868 | cp = bp->buf; |
| 869 | } |
| 870 | } |
| 871 | if (showprogress) |
| 872 | progressmeter(1); |
| 873 | if (count != 0 && wrerr == NO && |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 874 | (j = atomicio(write, ofd, bp->buf, count)) != count) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 875 | wrerr = YES; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 876 | wrerrno = j >= 0 ? EIO : errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 877 | } |
| 878 | #if 0 |
| 879 | if (ftruncate(ofd, size)) { |
| 880 | run_err("%s: truncate: %s", np, strerror(errno)); |
| 881 | wrerr = DISPLAYED; |
| 882 | } |
| 883 | #endif |
| 884 | if (pflag) { |
| 885 | if (exists || omode != mode) |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 886 | #ifdef HAVE_FCHMOD |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 887 | if (fchmod(ofd, omode)) |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 888 | #else /* HAVE_FCHMOD */ |
| 889 | if (chmod(np, omode)) |
| 890 | #endif /* HAVE_FCHMOD */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 891 | run_err("%s: set mode: %s", |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 892 | np, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 893 | } else { |
| 894 | if (!exists && omode != mode) |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 895 | #ifdef HAVE_FCHMOD |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 896 | if (fchmod(ofd, omode & ~mask)) |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 897 | #else /* HAVE_FCHMOD */ |
| 898 | if (chmod(np, omode & ~mask)) |
| 899 | #endif /* HAVE_FCHMOD */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 900 | run_err("%s: set mode: %s", |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 901 | np, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 902 | } |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 903 | if (close(ofd) == -1) { |
| 904 | wrerr = YES; |
| 905 | wrerrno = errno; |
| 906 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 907 | (void) response(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 908 | if (setimes && wrerr == NO) { |
| 909 | setimes = 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 910 | if (utimes(np, tv) < 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 911 | run_err("%s: set times: %s", |
Ben Lindstrom | bd47226 | 2001-03-29 00:39:55 +0000 | [diff] [blame] | 912 | np, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 913 | wrerr = DISPLAYED; |
| 914 | } |
| 915 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 916 | switch (wrerr) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 917 | case YES: |
| 918 | run_err("%s: %s", np, strerror(wrerrno)); |
| 919 | break; |
| 920 | case NO: |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 921 | (void) atomicio(write, remout, "", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 922 | break; |
| 923 | case DISPLAYED: |
| 924 | break; |
| 925 | } |
| 926 | } |
| 927 | screwup: |
| 928 | run_err("protocol error: %s", why); |
| 929 | exit(1); |
| 930 | } |
| 931 | |
| 932 | int |
| 933 | response() |
| 934 | { |
| 935 | char ch, *cp, resp, rbuf[2048]; |
| 936 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 937 | if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 938 | lostconn(0); |
| 939 | |
| 940 | cp = rbuf; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 941 | switch (resp) { |
| 942 | case 0: /* ok */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 943 | return (0); |
| 944 | default: |
| 945 | *cp++ = resp; |
| 946 | /* FALLTHROUGH */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 947 | case 1: /* error, followed by error msg */ |
| 948 | case 2: /* fatal error, "" */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 949 | do { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 950 | if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 951 | lostconn(0); |
| 952 | *cp++ = ch; |
| 953 | } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n'); |
| 954 | |
| 955 | if (!iamremote) |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 956 | (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 957 | ++errs; |
| 958 | if (resp == 1) |
| 959 | return (-1); |
| 960 | exit(1); |
| 961 | } |
| 962 | /* NOTREACHED */ |
| 963 | } |
| 964 | |
| 965 | void |
| 966 | usage() |
| 967 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 968 | (void) fprintf(stderr, "usage: scp " |
Ben Lindstrom | ff2618c | 2001-03-29 00:43:54 +0000 | [diff] [blame] | 969 | "[-pqrvBC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2\n" |
| 970 | " or: scp [options] f1 ... fn directory\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 971 | exit(1); |
| 972 | } |
| 973 | |
| 974 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 975 | run_err(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 976 | { |
| 977 | static FILE *fp; |
| 978 | va_list ap; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 979 | |
| 980 | ++errs; |
| 981 | if (fp == NULL && !(fp = fdopen(remout, "w"))) |
| 982 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 983 | (void) fprintf(fp, "%c", 0x01); |
| 984 | (void) fprintf(fp, "scp: "); |
Damien Miller | 03783f0 | 1999-12-31 09:16:40 +1100 | [diff] [blame] | 985 | va_start(ap, fmt); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 986 | (void) vfprintf(fp, fmt, ap); |
Damien Miller | 03783f0 | 1999-12-31 09:16:40 +1100 | [diff] [blame] | 987 | va_end(ap); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 988 | (void) fprintf(fp, "\n"); |
| 989 | (void) fflush(fp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 990 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 991 | if (!iamremote) { |
Damien Miller | 03783f0 | 1999-12-31 09:16:40 +1100 | [diff] [blame] | 992 | va_start(ap, fmt); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 993 | vfprintf(stderr, fmt, ap); |
Damien Miller | 03783f0 | 1999-12-31 09:16:40 +1100 | [diff] [blame] | 994 | va_end(ap); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 995 | fprintf(stderr, "\n"); |
| 996 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 997 | } |
| 998 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 999 | char * |
| 1000 | colon(cp) |
| 1001 | char *cp; |
| 1002 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1003 | int flag = 0; |
| 1004 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1005 | if (*cp == ':') /* Leading colon is part of file name. */ |
| 1006 | return (0); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1007 | if (*cp == '[') |
| 1008 | flag = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1009 | |
| 1010 | for (; *cp; ++cp) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1011 | if (*cp == '@' && *(cp+1) == '[') |
| 1012 | flag = 1; |
| 1013 | if (*cp == ']' && *(cp+1) == ':' && flag) |
| 1014 | return (cp+1); |
| 1015 | if (*cp == ':' && !flag) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1016 | return (cp); |
| 1017 | if (*cp == '/') |
| 1018 | return (0); |
| 1019 | } |
| 1020 | return (0); |
| 1021 | } |
| 1022 | |
| 1023 | void |
| 1024 | verifydir(cp) |
| 1025 | char *cp; |
| 1026 | { |
| 1027 | struct stat stb; |
| 1028 | |
| 1029 | if (!stat(cp, &stb)) { |
| 1030 | if (S_ISDIR(stb.st_mode)) |
| 1031 | return; |
| 1032 | errno = ENOTDIR; |
| 1033 | } |
| 1034 | run_err("%s: %s", cp, strerror(errno)); |
| 1035 | exit(1); |
| 1036 | } |
| 1037 | |
| 1038 | int |
| 1039 | okname(cp0) |
| 1040 | char *cp0; |
| 1041 | { |
| 1042 | int c; |
| 1043 | char *cp; |
| 1044 | |
| 1045 | cp = cp0; |
| 1046 | do { |
| 1047 | c = *cp; |
| 1048 | if (c & 0200) |
| 1049 | goto bad; |
Kevin Steves | 8daed18 | 2000-12-16 19:21:03 +0000 | [diff] [blame] | 1050 | if (!isalpha(c) && !isdigit(c) && |
| 1051 | c != '_' && c != '-' && c != '.' && c != '+') |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1052 | goto bad; |
| 1053 | } while (*++cp); |
| 1054 | return (1); |
| 1055 | |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 1056 | bad: fprintf(stderr, "%s: invalid user name\n", cp0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1057 | return (0); |
| 1058 | } |
| 1059 | |
| 1060 | BUF * |
| 1061 | allocbuf(bp, fd, blksize) |
| 1062 | BUF *bp; |
| 1063 | int fd, blksize; |
| 1064 | { |
| 1065 | size_t size; |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1066 | #ifdef HAVE_ST_BLKSIZE |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1067 | struct stat stb; |
| 1068 | |
| 1069 | if (fstat(fd, &stb) < 0) { |
| 1070 | run_err("fstat: %s", strerror(errno)); |
| 1071 | return (0); |
| 1072 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1073 | if (stb.st_blksize == 0) |
| 1074 | size = blksize; |
| 1075 | else |
| 1076 | size = blksize + (stb.st_blksize - blksize % stb.st_blksize) % |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1077 | stb.st_blksize; |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1078 | #else /* HAVE_ST_BLKSIZE */ |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1079 | size = blksize; |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1080 | #endif /* HAVE_ST_BLKSIZE */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1081 | if (bp->cnt >= size) |
| 1082 | return (bp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1083 | if (bp->buf == NULL) |
| 1084 | bp->buf = xmalloc(size); |
| 1085 | else |
| 1086 | bp->buf = xrealloc(bp->buf, size); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1087 | bp->cnt = size; |
| 1088 | return (bp); |
| 1089 | } |
| 1090 | |
| 1091 | void |
| 1092 | lostconn(signo) |
| 1093 | int signo; |
| 1094 | { |
| 1095 | if (!iamremote) |
| 1096 | fprintf(stderr, "lost connection\n"); |
| 1097 | exit(1); |
| 1098 | } |
| 1099 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1100 | |
| 1101 | void |
| 1102 | alarmtimer(int wait) |
| 1103 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1104 | struct itimerval itv; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1105 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1106 | itv.it_value.tv_sec = wait; |
| 1107 | itv.it_value.tv_usec = 0; |
| 1108 | itv.it_interval = itv.it_value; |
| 1109 | setitimer(ITIMER_REAL, &itv, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | void |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 1113 | updateprogressmeter(int ignore) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1114 | { |
| 1115 | int save_errno = errno; |
| 1116 | |
| 1117 | progressmeter(0); |
| 1118 | errno = save_errno; |
| 1119 | } |
| 1120 | |
Damien Miller | 81428f9 | 1999-11-18 09:28:11 +1100 | [diff] [blame] | 1121 | int |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 1122 | foregroundproc(void) |
Damien Miller | 81428f9 | 1999-11-18 09:28:11 +1100 | [diff] [blame] | 1123 | { |
| 1124 | static pid_t pgrp = -1; |
| 1125 | int ctty_pgrp; |
| 1126 | |
| 1127 | if (pgrp == -1) |
| 1128 | pgrp = getpgrp(); |
| 1129 | |
Ben Lindstrom | dd5c5a3 | 2001-02-02 18:58:33 +0000 | [diff] [blame] | 1130 | #ifdef HAVE_TCGETPGRP |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1131 | return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 && |
| 1132 | ctty_pgrp == pgrp); |
| 1133 | #else |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1134 | return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 && |
| 1135 | ctty_pgrp == pgrp)); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1136 | #endif |
Damien Miller | 81428f9 | 1999-11-18 09:28:11 +1100 | [diff] [blame] | 1137 | } |
| 1138 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1139 | void |
| 1140 | progressmeter(int flag) |
| 1141 | { |
| 1142 | static const char prefixes[] = " KMGTP"; |
| 1143 | static struct timeval lastupdate; |
| 1144 | static off_t lastsize; |
| 1145 | struct timeval now, td, wait; |
| 1146 | off_t cursize, abbrevsize; |
| 1147 | double elapsed; |
Damien Miller | 7c64ba3 | 1999-11-11 21:39:50 +1100 | [diff] [blame] | 1148 | int ratio, barlength, i, remaining; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1149 | char buf[256]; |
| 1150 | |
| 1151 | if (flag == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1152 | (void) gettimeofday(&start, (struct timezone *) 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1153 | lastupdate = start; |
| 1154 | lastsize = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1155 | } |
Damien Miller | 81428f9 | 1999-11-18 09:28:11 +1100 | [diff] [blame] | 1156 | if (foregroundproc() == 0) |
| 1157 | return; |
| 1158 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1159 | (void) gettimeofday(&now, (struct timezone *) 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1160 | cursize = statbytes; |
Damien Miller | a2d6efe | 1999-11-13 13:22:46 +1100 | [diff] [blame] | 1161 | if (totalbytes != 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1162 | ratio = 100.0 * cursize / totalbytes; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1163 | ratio = MAX(ratio, 0); |
| 1164 | ratio = MIN(ratio, 100); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1165 | } else |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1166 | ratio = 100; |
| 1167 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1168 | snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1169 | |
| 1170 | barlength = getttywidth() - 51; |
Damien Miller | 484118e | 2000-07-02 19:13:56 +1000 | [diff] [blame] | 1171 | barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1172 | if (barlength > 0) { |
| 1173 | i = barlength * ratio / 100; |
| 1174 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Damien Miller | 484118e | 2000-07-02 19:13:56 +1000 | [diff] [blame] | 1175 | "|%.*s%*s|", i, BAR, barlength - i, ""); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1176 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1177 | i = 0; |
| 1178 | abbrevsize = cursize; |
| 1179 | while (abbrevsize >= 100000 && i < sizeof(prefixes)) { |
| 1180 | i++; |
| 1181 | abbrevsize >>= 10; |
| 1182 | } |
Kevin Steves | adf74cd | 2001-02-05 14:22:50 +0000 | [diff] [blame] | 1183 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ", |
Kevin Steves | d2e0d7d | 2001-02-11 14:19:40 +0000 | [diff] [blame] | 1184 | (unsigned long) abbrevsize, prefixes[i], |
| 1185 | prefixes[i] == ' ' ? ' ' : 'B'); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1186 | |
| 1187 | timersub(&now, &lastupdate, &wait); |
| 1188 | if (cursize > lastsize) { |
| 1189 | lastupdate = now; |
| 1190 | lastsize = cursize; |
| 1191 | if (wait.tv_sec >= STALLTIME) { |
| 1192 | start.tv_sec += wait.tv_sec; |
| 1193 | start.tv_usec += wait.tv_usec; |
| 1194 | } |
| 1195 | wait.tv_sec = 0; |
| 1196 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1197 | timersub(&now, &start, &td); |
| 1198 | elapsed = td.tv_sec + (td.tv_usec / 1000000.0); |
| 1199 | |
Kevin Steves | 7d00ba4 | 2000-12-15 23:03:10 +0000 | [diff] [blame] | 1200 | if (flag != 1 && |
| 1201 | (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1202 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Kevin Steves | 7d00ba4 | 2000-12-15 23:03:10 +0000 | [diff] [blame] | 1203 | " --:-- ETA"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1204 | } else if (wait.tv_sec >= STALLTIME) { |
| 1205 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Kevin Steves | 7d00ba4 | 2000-12-15 23:03:10 +0000 | [diff] [blame] | 1206 | " - stalled -"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1207 | } else { |
Damien Miller | 8bb73be | 2000-04-19 16:26:12 +1000 | [diff] [blame] | 1208 | if (flag != 1) |
Kevin Steves | 7d00ba4 | 2000-12-15 23:03:10 +0000 | [diff] [blame] | 1209 | remaining = (int)(totalbytes / (statbytes / elapsed) - |
| 1210 | elapsed); |
Damien Miller | 8bb73be | 2000-04-19 16:26:12 +1000 | [diff] [blame] | 1211 | else |
| 1212 | remaining = elapsed; |
| 1213 | |
Damien Miller | 01ab4a2 | 1999-10-28 15:23:30 +1000 | [diff] [blame] | 1214 | i = remaining / 3600; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1215 | if (i) |
| 1216 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1217 | "%2d:", i); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1218 | else |
| 1219 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1220 | " "); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1221 | i = remaining % 3600; |
| 1222 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1223 | "%02d:%02d%s", i / 60, i % 60, |
| 1224 | (flag != 1) ? " ETA" : " "); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1225 | } |
| 1226 | atomicio(write, fileno(stdout), buf, strlen(buf)); |
| 1227 | |
| 1228 | if (flag == -1) { |
Kevin Steves | 93c17d9 | 2001-02-18 03:55:16 +0000 | [diff] [blame] | 1229 | mysignal(SIGALRM, updateprogressmeter); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1230 | alarmtimer(1); |
| 1231 | } else if (flag == 1) { |
| 1232 | alarmtimer(0); |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 1233 | atomicio(write, fileno(stdout), "\n", 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1234 | statbytes = 0; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | int |
| 1239 | getttywidth(void) |
| 1240 | { |
| 1241 | struct winsize winsize; |
| 1242 | |
| 1243 | if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1244 | return (winsize.ws_col ? winsize.ws_col : 80); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1245 | else |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1246 | return (80); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1247 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1248 | |
| 1249 | void |
| 1250 | addargs(char *fmt, ...) |
| 1251 | { |
| 1252 | va_list ap; |
| 1253 | char buf[1024]; |
| 1254 | |
| 1255 | va_start(ap, fmt); |
| 1256 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 1257 | va_end(ap); |
| 1258 | |
| 1259 | if (args.list == NULL) { |
| 1260 | args.nalloc = 32; |
| 1261 | args.num = 0; |
| 1262 | args.list = xmalloc(args.nalloc * sizeof(char *)); |
| 1263 | } else if (args.num+2 >= args.nalloc) { |
| 1264 | args.nalloc *= 2; |
| 1265 | args.list = xrealloc(args.list, args.nalloc * sizeof(char *)); |
| 1266 | } |
| 1267 | args.list[args.num++] = xstrdup(buf); |
| 1268 | args.list[args.num] = NULL; |
| 1269 | } |