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