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