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