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