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