]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
bcbf86ec | 2 | * scp - secure remote copy. This is basically patched BSD rcp which |
3 | * uses ssh to do the data transfer (instead of using rcmd). | |
6ae2364d | 4 | * |
bcbf86ec | 5 | * NOTE: This version should NOT be suid root. (This uses ssh to |
6 | * do the transfer and ssh has the necessary privileges.) | |
6ae2364d | 7 | * |
5260325f | 8 | * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi> |
6ae2364d | 9 | * |
bcbf86ec | 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 | /* | |
f3c7c613 | 17 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
18 | * Copyright (c) 1999 Aaron Campbell. All rights reserved. | |
bcbf86ec | 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 | */ | |
8efc0c15 | 40 | |
41 | /* | |
2e73a022 | 42 | * Parts from: |
43 | * | |
8efc0c15 | 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. | |
d981089c | 55 | * 3. Neither the name of the University nor the names of its contributors |
8efc0c15 | 56 | * may be used to endorse or promote products derived from this software |
57 | * without specific prior written permission. | |
58 | * | |
59 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
60 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
61 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
62 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
63 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
64 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
65 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
66 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
67 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
68 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
69 | * SUCH DAMAGE. | |
70 | * | |
8efc0c15 | 71 | */ |
72 | ||
73 | #include "includes.h" | |
2ceb8101 | 74 | RCSID("$OpenBSD: scp.c,v 1.124 2005/06/17 02:44:33 djm Exp $"); |
8efc0c15 | 75 | |
8efc0c15 | 76 | #include "xmalloc.h" |
42f11eb2 | 77 | #include "atomicio.h" |
78 | #include "pathnames.h" | |
79 | #include "log.h" | |
36a358ca | 80 | #include "misc.h" |
b65c3807 | 81 | #include "progressmeter.h" |
8efc0c15 | 82 | |
260d427b | 83 | extern char *__progname; |
260d427b | 84 | |
6ea3c52a | 85 | void bwlimit(int); |
86 | ||
8a624ebf | 87 | /* Struct for addargs */ |
88 | arglist args; | |
94ec8c6b | 89 | |
6ea3c52a | 90 | /* Bandwidth limit */ |
10adbb52 | 91 | off_t limit_rate = 0; |
6ea3c52a | 92 | |
8efc0c15 | 93 | /* Name of current file being transferred. */ |
94 | char *curfile; | |
95 | ||
96 | /* This is set to non-zero to enable verbose mode. */ | |
5260325f | 97 | int verbose_mode = 0; |
8efc0c15 | 98 | |
8efc0c15 | 99 | /* This is set to zero if the progressmeter is not desired. */ |
100 | int showprogress = 1; | |
101 | ||
2e73a022 | 102 | /* This is the program to execute for the secured connection. ("ssh" or -S) */ |
42f11eb2 | 103 | char *ssh_program = _PATH_SSH_PROGRAM; |
2e73a022 | 104 | |
252d1a24 | 105 | /* This is used to store the pid of ssh_program */ |
0f764b2f | 106 | pid_t do_cmd_pid = -1; |
107 | ||
108 | static void | |
109 | killchild(int signo) | |
110 | { | |
3c03ad3f | 111 | if (do_cmd_pid > 1) { |
8f4ab41b | 112 | kill(do_cmd_pid, signo ? signo : SIGTERM); |
3c03ad3f | 113 | waitpid(do_cmd_pid, NULL, 0); |
114 | } | |
0f764b2f | 115 | |
8f4ab41b | 116 | if (signo) |
117 | _exit(1); | |
118 | exit(1); | |
0f764b2f | 119 | } |
252d1a24 | 120 | |
aa3378df | 121 | /* |
122 | * This function executes the given command as the specified user on the | |
123 | * given host. This returns < 0 if execution fails, and >= 0 otherwise. This | |
124 | * assigns the input and output file descriptors on success. | |
125 | */ | |
8efc0c15 | 126 | |
6ae2364d | 127 | int |
2e73a022 | 128 | do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc) |
8efc0c15 | 129 | { |
5260325f | 130 | int pin[2], pout[2], reserved[2]; |
131 | ||
132 | if (verbose_mode) | |
0b2988ca | 133 | fprintf(stderr, |
134 | "Executing: program %s host %s, user %s, command %s\n", | |
135 | ssh_program, host, | |
136 | remuser ? remuser : "(unspecified)", cmd); | |
5260325f | 137 | |
aa3378df | 138 | /* |
139 | * Reserve two descriptors so that the real pipes won't get | |
140 | * descriptors 0 and 1 because that will screw up dup2 below. | |
141 | */ | |
5260325f | 142 | pipe(reserved); |
143 | ||
144 | /* Create a socket pair for communicating with ssh. */ | |
145 | if (pipe(pin) < 0) | |
146 | fatal("pipe: %s", strerror(errno)); | |
147 | if (pipe(pout) < 0) | |
148 | fatal("pipe: %s", strerror(errno)); | |
149 | ||
150 | /* Free the reserved descriptors. */ | |
151 | close(reserved[0]); | |
152 | close(reserved[1]); | |
153 | ||
1fb23629 | 154 | /* Fork a child to execute the command on the remote host using ssh. */ |
252d1a24 | 155 | do_cmd_pid = fork(); |
156 | if (do_cmd_pid == 0) { | |
5260325f | 157 | /* Child. */ |
158 | close(pin[1]); | |
159 | close(pout[0]); | |
160 | dup2(pin[0], 0); | |
161 | dup2(pout[1], 1); | |
162 | close(pin[0]); | |
163 | close(pout[1]); | |
164 | ||
94ec8c6b | 165 | args.list[0] = ssh_program; |
166 | if (remuser != NULL) | |
8a624ebf | 167 | addargs(&args, "-l%s", remuser); |
168 | addargs(&args, "%s", host); | |
169 | addargs(&args, "%s", cmd); | |
5260325f | 170 | |
94ec8c6b | 171 | execvp(ssh_program, args.list); |
2e73a022 | 172 | perror(ssh_program); |
5260325f | 173 | exit(1); |
252d1a24 | 174 | } else if (do_cmd_pid == -1) { |
175 | fatal("fork: %s", strerror(errno)); | |
8efc0c15 | 176 | } |
5260325f | 177 | /* Parent. Close the other side, and return the local side. */ |
178 | close(pin[0]); | |
179 | *fdout = pin[1]; | |
180 | close(pout[1]); | |
181 | *fdin = pout[0]; | |
0f764b2f | 182 | signal(SIGTERM, killchild); |
183 | signal(SIGINT, killchild); | |
184 | signal(SIGHUP, killchild); | |
5260325f | 185 | return 0; |
8efc0c15 | 186 | } |
187 | ||
8efc0c15 | 188 | typedef struct { |
2ceb8101 | 189 | size_t cnt; |
8efc0c15 | 190 | char *buf; |
191 | } BUF; | |
192 | ||
5260325f | 193 | BUF *allocbuf(BUF *, int, int); |
5260325f | 194 | void lostconn(int); |
195 | void nospace(void); | |
196 | int okname(char *); | |
197 | void run_err(const char *,...); | |
198 | void verifydir(char *); | |
8efc0c15 | 199 | |
8efc0c15 | 200 | struct passwd *pwd; |
5260325f | 201 | uid_t userid; |
8efc0c15 | 202 | int errs, remin, remout; |
203 | int pflag, iamremote, iamrecursive, targetshouldbedirectory; | |
204 | ||
205 | #define CMDNEEDS 64 | |
206 | char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ | |
207 | ||
5260325f | 208 | int response(void); |
209 | void rsource(char *, struct stat *); | |
210 | void sink(int, char *[]); | |
211 | void source(int, char *[]); | |
212 | void tolocal(int, char *[]); | |
213 | void toremote(char *, int, char *[]); | |
214 | void usage(void); | |
8efc0c15 | 215 | |
216 | int | |
cf3248b8 | 217 | main(int argc, char **argv) |
8efc0c15 | 218 | { |
252d1a24 | 219 | int ch, fflag, tflag, status; |
6ea3c52a | 220 | double speed; |
221 | char *targ, *endp; | |
8efc0c15 | 222 | extern char *optarg; |
223 | extern int optind; | |
224 | ||
fda04d7d | 225 | __progname = ssh_get_progname(argv[0]); |
260d427b | 226 | |
94ec8c6b | 227 | args.list = NULL; |
7203d6bb | 228 | addargs(&args, "ssh"); /* overwritten with ssh_program */ |
8a624ebf | 229 | addargs(&args, "-x"); |
3a222388 | 230 | addargs(&args, "-oForwardAgent no"); |
e1c5bfaf | 231 | addargs(&args, "-oClearAllForwardings yes"); |
94ec8c6b | 232 | |
8efc0c15 | 233 | fflag = tflag = 0; |
e6b15f23 | 234 | while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1) |
5260325f | 235 | switch (ch) { |
236 | /* User-visible flags. */ | |
e6b15f23 | 237 | case '1': |
238 | case '2': | |
48e671d5 | 239 | case '4': |
48e671d5 | 240 | case '6': |
94ec8c6b | 241 | case 'C': |
8a624ebf | 242 | addargs(&args, "-%c", ch); |
48e671d5 | 243 | break; |
94ec8c6b | 244 | case 'o': |
245 | case 'c': | |
246 | case 'i': | |
f1278af7 | 247 | case 'F': |
8a624ebf | 248 | addargs(&args, "-%c%s", ch, optarg); |
8efc0c15 | 249 | break; |
250 | case 'P': | |
8a624ebf | 251 | addargs(&args, "-p%s", optarg); |
94ec8c6b | 252 | break; |
253 | case 'B': | |
8a624ebf | 254 | addargs(&args, "-oBatchmode yes"); |
94ec8c6b | 255 | break; |
6ea3c52a | 256 | case 'l': |
257 | speed = strtod(optarg, &endp); | |
258 | if (speed <= 0 || *endp != '\0') | |
259 | usage(); | |
10adbb52 | 260 | limit_rate = speed * 1024; |
6ea3c52a | 261 | break; |
94ec8c6b | 262 | case 'p': |
263 | pflag = 1; | |
5260325f | 264 | break; |
8efc0c15 | 265 | case 'r': |
266 | iamrecursive = 1; | |
267 | break; | |
2e73a022 | 268 | case 'S': |
94ec8c6b | 269 | ssh_program = xstrdup(optarg); |
270 | break; | |
271 | case 'v': | |
4fc334a2 | 272 | addargs(&args, "-v"); |
94ec8c6b | 273 | verbose_mode = 1; |
274 | break; | |
275 | case 'q': | |
15c8e3fd | 276 | addargs(&args, "-q"); |
94ec8c6b | 277 | showprogress = 0; |
2e73a022 | 278 | break; |
279 | ||
5260325f | 280 | /* Server options. */ |
8efc0c15 | 281 | case 'd': |
282 | targetshouldbedirectory = 1; | |
283 | break; | |
5260325f | 284 | case 'f': /* "from" */ |
8efc0c15 | 285 | iamremote = 1; |
286 | fflag = 1; | |
287 | break; | |
5260325f | 288 | case 't': /* "to" */ |
8efc0c15 | 289 | iamremote = 1; |
290 | tflag = 1; | |
fe56c12b | 291 | #ifdef HAVE_CYGWIN |
292 | setmode(0, O_BINARY); | |
293 | #endif | |
8efc0c15 | 294 | break; |
8efc0c15 | 295 | default: |
296 | usage(); | |
297 | } | |
298 | argc -= optind; | |
299 | argv += optind; | |
300 | ||
301 | if ((pwd = getpwuid(userid = getuid())) == NULL) | |
261bd618 | 302 | fatal("unknown user %u", (u_int) userid); |
8efc0c15 | 303 | |
5260325f | 304 | if (!isatty(STDERR_FILENO)) |
8efc0c15 | 305 | showprogress = 0; |
306 | ||
307 | remin = STDIN_FILENO; | |
308 | remout = STDOUT_FILENO; | |
309 | ||
2b87da3b | 310 | if (fflag) { |
5260325f | 311 | /* Follow "protocol", send data. */ |
312 | (void) response(); | |
8efc0c15 | 313 | source(argc, argv); |
314 | exit(errs != 0); | |
315 | } | |
5260325f | 316 | if (tflag) { |
317 | /* Receive data. */ | |
8efc0c15 | 318 | sink(argc, argv); |
319 | exit(errs != 0); | |
320 | } | |
8efc0c15 | 321 | if (argc < 2) |
322 | usage(); | |
323 | if (argc > 2) | |
324 | targetshouldbedirectory = 1; | |
325 | ||
326 | remin = remout = -1; | |
252d1a24 | 327 | do_cmd_pid = -1; |
8efc0c15 | 328 | /* Command to be executed on remote system using "ssh". */ |
0ed28836 | 329 | (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s", |
330 | verbose_mode ? " -v" : "", | |
2e73a022 | 331 | iamrecursive ? " -r" : "", pflag ? " -p" : "", |
332 | targetshouldbedirectory ? " -d" : ""); | |
8efc0c15 | 333 | |
5260325f | 334 | (void) signal(SIGPIPE, lostconn); |
8efc0c15 | 335 | |
336 | if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */ | |
337 | toremote(targ, argc, argv); | |
338 | else { | |
5260325f | 339 | tolocal(argc, argv); /* Dest is local host. */ |
8efc0c15 | 340 | if (targetshouldbedirectory) |
341 | verifydir(argv[argc - 1]); | |
342 | } | |
252d1a24 | 343 | /* |
344 | * Finally check the exit status of the ssh process, if one was forked | |
345 | * and no error has occured yet | |
346 | */ | |
347 | if (do_cmd_pid != -1 && errs == 0) { | |
348 | if (remin != -1) | |
349 | (void) close(remin); | |
350 | if (remout != -1) | |
351 | (void) close(remout); | |
352 | if (waitpid(do_cmd_pid, &status, 0) == -1) | |
353 | errs = 1; | |
354 | else { | |
355 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) | |
356 | errs = 1; | |
357 | } | |
358 | } | |
8efc0c15 | 359 | exit(errs != 0); |
360 | } | |
361 | ||
362 | void | |
cf3248b8 | 363 | toremote(char *targ, int argc, char **argv) |
8efc0c15 | 364 | { |
365 | int i, len; | |
99dfaccc | 366 | char *bp, *host, *src, *suser, *thost, *tuser, *arg; |
8efc0c15 | 367 | |
368 | *targ++ = 0; | |
369 | if (*targ == 0) | |
370 | targ = "."; | |
371 | ||
ae0d2f42 | 372 | arg = xstrdup(argv[argc - 1]); |
99dfaccc | 373 | if ((thost = strrchr(arg, '@'))) { |
8efc0c15 | 374 | /* user@host */ |
375 | *thost++ = 0; | |
99dfaccc | 376 | tuser = arg; |
8efc0c15 | 377 | if (*tuser == '\0') |
378 | tuser = NULL; | |
8efc0c15 | 379 | } else { |
99dfaccc | 380 | thost = arg; |
8efc0c15 | 381 | tuser = NULL; |
382 | } | |
383 | ||
384 | for (i = 0; i < argc - 1; i++) { | |
385 | src = colon(argv[i]); | |
5260325f | 386 | if (src) { /* remote to remote */ |
e1c5bfaf | 387 | static char *ssh_options = |
61eb812e | 388 | "-x -o'ClearAllForwardings yes'"; |
8efc0c15 | 389 | *src++ = 0; |
390 | if (*src == 0) | |
391 | src = "."; | |
15748b4d | 392 | host = strrchr(argv[i], '@'); |
2e73a022 | 393 | len = strlen(ssh_program) + strlen(argv[i]) + |
394 | strlen(src) + (tuser ? strlen(tuser) : 0) + | |
e1c5bfaf | 395 | strlen(thost) + strlen(targ) + |
396 | strlen(ssh_options) + CMDNEEDS + 20; | |
5260325f | 397 | bp = xmalloc(len); |
8efc0c15 | 398 | if (host) { |
399 | *host++ = 0; | |
48e671d5 | 400 | host = cleanhostname(host); |
8efc0c15 | 401 | suser = argv[i]; |
402 | if (*suser == '\0') | |
403 | suser = pwd->pw_name; | |
3e2f2431 | 404 | else if (!okname(suser)) { |
405 | xfree(bp); | |
8efc0c15 | 406 | continue; |
3e2f2431 | 407 | } |
408 | if (tuser && !okname(tuser)) { | |
409 | xfree(bp); | |
0ec7661a | 410 | continue; |
3e2f2431 | 411 | } |
0ed28836 | 412 | snprintf(bp, len, |
e1c5bfaf | 413 | "%s%s %s -n " |
0ed28836 | 414 | "-l %s %s %s %s '%s%s%s:%s'", |
71c0d06a | 415 | ssh_program, verbose_mode ? " -v" : "", |
e1c5bfaf | 416 | ssh_options, suser, host, cmd, src, |
71c0d06a | 417 | tuser ? tuser : "", tuser ? "@" : "", |
418 | thost, targ); | |
48e671d5 | 419 | } else { |
420 | host = cleanhostname(argv[i]); | |
0ed28836 | 421 | snprintf(bp, len, |
e1c5bfaf | 422 | "exec %s%s %s -n %s " |
0ed28836 | 423 | "%s %s '%s%s%s:%s'", |
71c0d06a | 424 | ssh_program, verbose_mode ? " -v" : "", |
e1c5bfaf | 425 | ssh_options, host, cmd, src, |
71c0d06a | 426 | tuser ? tuser : "", tuser ? "@" : "", |
427 | thost, targ); | |
48e671d5 | 428 | } |
5260325f | 429 | if (verbose_mode) |
430 | fprintf(stderr, "Executing: %s\n", bp); | |
815a8407 | 431 | if (system(bp) != 0) |
432 | errs = 1; | |
5260325f | 433 | (void) xfree(bp); |
434 | } else { /* local to remote */ | |
8efc0c15 | 435 | if (remin == -1) { |
436 | len = strlen(targ) + CMDNEEDS + 20; | |
5260325f | 437 | bp = xmalloc(len); |
0ed28836 | 438 | (void) snprintf(bp, len, "%s -t %s", cmd, targ); |
48e671d5 | 439 | host = cleanhostname(thost); |
2e73a022 | 440 | if (do_cmd(host, tuser, bp, &remin, |
441 | &remout, argc) < 0) | |
5260325f | 442 | exit(1); |
8efc0c15 | 443 | if (response() < 0) |
444 | exit(1); | |
5260325f | 445 | (void) xfree(bp); |
8efc0c15 | 446 | } |
5260325f | 447 | source(1, argv + i); |
8efc0c15 | 448 | } |
449 | } | |
450 | } | |
451 | ||
452 | void | |
cf3248b8 | 453 | tolocal(int argc, char **argv) |
8efc0c15 | 454 | { |
455 | int i, len; | |
456 | char *bp, *host, *src, *suser; | |
457 | ||
458 | for (i = 0; i < argc - 1; i++) { | |
5260325f | 459 | if (!(src = colon(argv[i]))) { /* Local to local. */ |
8efc0c15 | 460 | len = strlen(_PATH_CP) + strlen(argv[i]) + |
2e73a022 | 461 | strlen(argv[argc - 1]) + 20; |
8efc0c15 | 462 | bp = xmalloc(len); |
0ed28836 | 463 | (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP, |
2e73a022 | 464 | iamrecursive ? " -r" : "", pflag ? " -p" : "", |
465 | argv[i], argv[argc - 1]); | |
5260325f | 466 | if (verbose_mode) |
467 | fprintf(stderr, "Executing: %s\n", bp); | |
8efc0c15 | 468 | if (system(bp)) |
469 | ++errs; | |
5260325f | 470 | (void) xfree(bp); |
8efc0c15 | 471 | continue; |
472 | } | |
473 | *src++ = 0; | |
474 | if (*src == 0) | |
475 | src = "."; | |
15748b4d | 476 | if ((host = strrchr(argv[i], '@')) == NULL) { |
8efc0c15 | 477 | host = argv[i]; |
478 | suser = NULL; | |
479 | } else { | |
480 | *host++ = 0; | |
481 | suser = argv[i]; | |
482 | if (*suser == '\0') | |
483 | suser = pwd->pw_name; | |
8efc0c15 | 484 | } |
48e671d5 | 485 | host = cleanhostname(host); |
8efc0c15 | 486 | len = strlen(src) + CMDNEEDS + 20; |
5260325f | 487 | bp = xmalloc(len); |
0ed28836 | 488 | (void) snprintf(bp, len, "%s -f %s", cmd, src); |
2e73a022 | 489 | if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) { |
5260325f | 490 | (void) xfree(bp); |
491 | ++errs; | |
492 | continue; | |
8efc0c15 | 493 | } |
5260325f | 494 | xfree(bp); |
8efc0c15 | 495 | sink(1, argv + argc - 1); |
5260325f | 496 | (void) close(remin); |
8efc0c15 | 497 | remin = remout = -1; |
498 | } | |
499 | } | |
500 | ||
501 | void | |
cf3248b8 | 502 | source(int argc, char **argv) |
8efc0c15 | 503 | { |
504 | struct stat stb; | |
505 | static BUF buffer; | |
506 | BUF *bp; | |
05624c18 | 507 | off_t i, amt, statbytes; |
508 | size_t result; | |
ba917921 | 509 | int fd, haderr, indx; |
8efc0c15 | 510 | char *last, *name, buf[2048]; |
0fc791ba | 511 | int len; |
8efc0c15 | 512 | |
513 | for (indx = 0; indx < argc; ++indx) { | |
5260325f | 514 | name = argv[indx]; |
8efc0c15 | 515 | statbytes = 0; |
0fc791ba | 516 | len = strlen(name); |
517 | while (len > 1 && name[len-1] == '/') | |
518 | name[--len] = '\0'; | |
5720c10e | 519 | if (strchr(name, '\n') != NULL) { |
520 | run_err("%s: skipping, filename contains a newline", | |
521 | name); | |
522 | goto next; | |
523 | } | |
8efc0c15 | 524 | if ((fd = open(name, O_RDONLY, 0)) < 0) |
525 | goto syserr; | |
526 | if (fstat(fd, &stb) < 0) { | |
527 | syserr: run_err("%s: %s", name, strerror(errno)); | |
528 | goto next; | |
529 | } | |
530 | switch (stb.st_mode & S_IFMT) { | |
531 | case S_IFREG: | |
532 | break; | |
533 | case S_IFDIR: | |
534 | if (iamrecursive) { | |
535 | rsource(name, &stb); | |
536 | goto next; | |
537 | } | |
538 | /* FALLTHROUGH */ | |
539 | default: | |
540 | run_err("%s: not a regular file", name); | |
541 | goto next; | |
542 | } | |
543 | if ((last = strrchr(name, '/')) == NULL) | |
544 | last = name; | |
545 | else | |
546 | ++last; | |
547 | curfile = last; | |
548 | if (pflag) { | |
549 | /* | |
550 | * Make it compatible with possible future | |
551 | * versions expecting microseconds. | |
552 | */ | |
0ed28836 | 553 | (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n", |
1e3b8b07 | 554 | (u_long) stb.st_mtime, |
555 | (u_long) stb.st_atime); | |
dc54438a | 556 | (void) atomicio(vwrite, remout, buf, strlen(buf)); |
8efc0c15 | 557 | if (response() < 0) |
558 | goto next; | |
559 | } | |
560 | #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) | |
bf1d27bd | 561 | snprintf(buf, sizeof buf, "C%04o %lld %s\n", |
1e3b8b07 | 562 | (u_int) (stb.st_mode & FILEMODEMASK), |
ded9dd18 | 563 | (int64_t)stb.st_size, last); |
5260325f | 564 | if (verbose_mode) { |
565 | fprintf(stderr, "Sending file modes: %s", buf); | |
5260325f | 566 | } |
dc54438a | 567 | (void) atomicio(vwrite, remout, buf, strlen(buf)); |
8efc0c15 | 568 | if (response() < 0) |
569 | goto next; | |
570 | if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) { | |
5260325f | 571 | next: (void) close(fd); |
8efc0c15 | 572 | continue; |
573 | } | |
b65c3807 | 574 | if (showprogress) |
575 | start_progress_meter(curfile, stb.st_size, &statbytes); | |
8efc0c15 | 576 | /* Keep writing after an error so that we stay sync'd up. */ |
577 | for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { | |
578 | amt = bp->cnt; | |
579 | if (i + amt > stb.st_size) | |
580 | amt = stb.st_size - i; | |
581 | if (!haderr) { | |
1d1ffb87 | 582 | result = atomicio(read, fd, bp->buf, amt); |
8efc0c15 | 583 | if (result != amt) |
05624c18 | 584 | haderr = errno; |
8efc0c15 | 585 | } |
586 | if (haderr) | |
dc54438a | 587 | (void) atomicio(vwrite, remout, bp->buf, amt); |
8efc0c15 | 588 | else { |
dc54438a | 589 | result = atomicio(vwrite, remout, bp->buf, amt); |
8efc0c15 | 590 | if (result != amt) |
05624c18 | 591 | haderr = errno; |
8efc0c15 | 592 | statbytes += result; |
593 | } | |
10adbb52 | 594 | if (limit_rate) |
6ea3c52a | 595 | bwlimit(amt); |
8efc0c15 | 596 | } |
5260325f | 597 | if (showprogress) |
b65c3807 | 598 | stop_progress_meter(); |
8efc0c15 | 599 | |
600 | if (close(fd) < 0 && !haderr) | |
601 | haderr = errno; | |
602 | if (!haderr) | |
dc54438a | 603 | (void) atomicio(vwrite, remout, "", 1); |
8efc0c15 | 604 | else |
605 | run_err("%s: %s", name, strerror(haderr)); | |
5260325f | 606 | (void) response(); |
8efc0c15 | 607 | } |
608 | } | |
609 | ||
610 | void | |
cf3248b8 | 611 | rsource(char *name, struct stat *statp) |
8efc0c15 | 612 | { |
613 | DIR *dirp; | |
614 | struct dirent *dp; | |
615 | char *last, *vect[1], path[1100]; | |
616 | ||
617 | if (!(dirp = opendir(name))) { | |
618 | run_err("%s: %s", name, strerror(errno)); | |
619 | return; | |
620 | } | |
621 | last = strrchr(name, '/'); | |
622 | if (last == 0) | |
623 | last = name; | |
624 | else | |
625 | last++; | |
626 | if (pflag) { | |
0ed28836 | 627 | (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n", |
1e3b8b07 | 628 | (u_long) statp->st_mtime, |
629 | (u_long) statp->st_atime); | |
dc54438a | 630 | (void) atomicio(vwrite, remout, path, strlen(path)); |
8efc0c15 | 631 | if (response() < 0) { |
632 | closedir(dirp); | |
633 | return; | |
634 | } | |
635 | } | |
0ed28836 | 636 | (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n", |
1e3b8b07 | 637 | (u_int) (statp->st_mode & FILEMODEMASK), 0, last); |
5260325f | 638 | if (verbose_mode) |
639 | fprintf(stderr, "Entering directory: %s", path); | |
dc54438a | 640 | (void) atomicio(vwrite, remout, path, strlen(path)); |
8efc0c15 | 641 | if (response() < 0) { |
642 | closedir(dirp); | |
643 | return; | |
644 | } | |
e5ff6ecf | 645 | while ((dp = readdir(dirp)) != NULL) { |
8efc0c15 | 646 | if (dp->d_ino == 0) |
647 | continue; | |
648 | if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) | |
649 | continue; | |
650 | if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) { | |
651 | run_err("%s/%s: name too long", name, dp->d_name); | |
652 | continue; | |
653 | } | |
0ed28836 | 654 | (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name); |
8efc0c15 | 655 | vect[0] = path; |
656 | source(1, vect); | |
657 | } | |
5260325f | 658 | (void) closedir(dirp); |
dc54438a | 659 | (void) atomicio(vwrite, remout, "E\n", 2); |
5260325f | 660 | (void) response(); |
8efc0c15 | 661 | } |
662 | ||
6ea3c52a | 663 | void |
664 | bwlimit(int amount) | |
665 | { | |
666 | static struct timeval bwstart, bwend; | |
667 | static int lamt, thresh = 16384; | |
ca75d7de | 668 | u_int64_t waitlen; |
6ea3c52a | 669 | struct timespec ts, rm; |
670 | ||
671 | if (!timerisset(&bwstart)) { | |
672 | gettimeofday(&bwstart, NULL); | |
673 | return; | |
674 | } | |
675 | ||
676 | lamt += amount; | |
677 | if (lamt < thresh) | |
678 | return; | |
679 | ||
680 | gettimeofday(&bwend, NULL); | |
681 | timersub(&bwend, &bwstart, &bwend); | |
682 | if (!timerisset(&bwend)) | |
683 | return; | |
684 | ||
685 | lamt *= 8; | |
ca75d7de | 686 | waitlen = (double)1000000L * lamt / limit_rate; |
6ea3c52a | 687 | |
ca75d7de | 688 | bwstart.tv_sec = waitlen / 1000000L; |
689 | bwstart.tv_usec = waitlen % 1000000L; | |
6ea3c52a | 690 | |
691 | if (timercmp(&bwstart, &bwend, >)) { | |
692 | timersub(&bwstart, &bwend, &bwend); | |
693 | ||
694 | /* Adjust the wait time */ | |
695 | if (bwend.tv_sec) { | |
696 | thresh /= 2; | |
697 | if (thresh < 2048) | |
698 | thresh = 2048; | |
699 | } else if (bwend.tv_usec < 100) { | |
700 | thresh *= 2; | |
701 | if (thresh > 32768) | |
702 | thresh = 32768; | |
703 | } | |
704 | ||
705 | TIMEVAL_TO_TIMESPEC(&bwend, &ts); | |
706 | while (nanosleep(&ts, &rm) == -1) { | |
707 | if (errno != EINTR) | |
708 | break; | |
709 | ts = rm; | |
710 | } | |
711 | } | |
712 | ||
713 | lamt = 0; | |
714 | gettimeofday(&bwstart, NULL); | |
715 | } | |
716 | ||
8efc0c15 | 717 | void |
cf3248b8 | 718 | sink(int argc, char **argv) |
8efc0c15 | 719 | { |
720 | static BUF buffer; | |
721 | struct stat stb; | |
5260325f | 722 | enum { |
723 | YES, NO, DISPLAYED | |
724 | } wrerr; | |
8efc0c15 | 725 | BUF *bp; |
05624c18 | 726 | off_t i; |
2ceb8101 | 727 | size_t j, count; |
728 | int amt, exists, first, mask, mode, ofd, omode; | |
b65c3807 | 729 | off_t size, statbytes; |
14a9a859 | 730 | int setimes, targisdir, wrerrno = 0; |
8efc0c15 | 731 | char ch, *cp, *np, *targ, *why, *vect[1], buf[2048]; |
188adeb2 | 732 | struct timeval tv[2]; |
8efc0c15 | 733 | |
36967a16 | 734 | #define atime tv[0] |
735 | #define mtime tv[1] | |
007607ab | 736 | #define SCREWUP(str) { why = str; goto screwup; } |
8efc0c15 | 737 | |
738 | setimes = targisdir = 0; | |
739 | mask = umask(0); | |
740 | if (!pflag) | |
5260325f | 741 | (void) umask(mask); |
8efc0c15 | 742 | if (argc != 1) { |
743 | run_err("ambiguous target"); | |
744 | exit(1); | |
745 | } | |
746 | targ = *argv; | |
747 | if (targetshouldbedirectory) | |
748 | verifydir(targ); | |
5260325f | 749 | |
dc54438a | 750 | (void) atomicio(vwrite, remout, "", 1); |
8efc0c15 | 751 | if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) |
752 | targisdir = 1; | |
753 | for (first = 1;; first = 0) { | |
754 | cp = buf; | |
05624c18 | 755 | if (atomicio(read, remin, cp, 1) != 1) |
8efc0c15 | 756 | return; |
757 | if (*cp++ == '\n') | |
758 | SCREWUP("unexpected <newline>"); | |
759 | do { | |
1d1ffb87 | 760 | if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) |
8efc0c15 | 761 | SCREWUP("lost connection"); |
762 | *cp++ = ch; | |
763 | } while (cp < &buf[sizeof(buf) - 1] && ch != '\n'); | |
764 | *cp = 0; | |
f09aa22c | 765 | if (verbose_mode) |
766 | fprintf(stderr, "Sink: %s", buf); | |
8efc0c15 | 767 | |
768 | if (buf[0] == '\01' || buf[0] == '\02') { | |
769 | if (iamremote == 0) | |
dc54438a | 770 | (void) atomicio(vwrite, STDERR_FILENO, |
71c0d06a | 771 | buf + 1, strlen(buf + 1)); |
8efc0c15 | 772 | if (buf[0] == '\02') |
773 | exit(1); | |
774 | ++errs; | |
775 | continue; | |
776 | } | |
777 | if (buf[0] == 'E') { | |
dc54438a | 778 | (void) atomicio(vwrite, remout, "", 1); |
8efc0c15 | 779 | return; |
780 | } | |
8efc0c15 | 781 | if (ch == '\n') |
782 | *--cp = 0; | |
783 | ||
8efc0c15 | 784 | cp = buf; |
785 | if (*cp == 'T') { | |
786 | setimes++; | |
787 | cp++; | |
36967a16 | 788 | mtime.tv_sec = strtol(cp, &cp, 10); |
789 | if (!cp || *cp++ != ' ') | |
8efc0c15 | 790 | SCREWUP("mtime.sec not delimited"); |
36967a16 | 791 | mtime.tv_usec = strtol(cp, &cp, 10); |
792 | if (!cp || *cp++ != ' ') | |
8efc0c15 | 793 | SCREWUP("mtime.usec not delimited"); |
36967a16 | 794 | atime.tv_sec = strtol(cp, &cp, 10); |
795 | if (!cp || *cp++ != ' ') | |
8efc0c15 | 796 | SCREWUP("atime.sec not delimited"); |
36967a16 | 797 | atime.tv_usec = strtol(cp, &cp, 10); |
798 | if (!cp || *cp++ != '\0') | |
8efc0c15 | 799 | SCREWUP("atime.usec not delimited"); |
dc54438a | 800 | (void) atomicio(vwrite, remout, "", 1); |
8efc0c15 | 801 | continue; |
802 | } | |
803 | if (*cp != 'C' && *cp != 'D') { | |
804 | /* | |
805 | * Check for the case "rcp remote:foo\* local:bar". | |
806 | * In this case, the line "No match." can be returned | |
807 | * by the shell before the rcp command on the remote is | |
808 | * executed so the ^Aerror_message convention isn't | |
809 | * followed. | |
810 | */ | |
811 | if (first) { | |
812 | run_err("%s", cp); | |
813 | exit(1); | |
814 | } | |
815 | SCREWUP("expected control record"); | |
816 | } | |
817 | mode = 0; | |
818 | for (++cp; cp < buf + 5; cp++) { | |
819 | if (*cp < '0' || *cp > '7') | |
820 | SCREWUP("bad mode"); | |
821 | mode = (mode << 3) | (*cp - '0'); | |
822 | } | |
823 | if (*cp++ != ' ') | |
824 | SCREWUP("mode not delimited"); | |
825 | ||
e5ff6ecf | 826 | for (size = 0; isdigit(*cp);) |
8efc0c15 | 827 | size = size * 10 + (*cp++ - '0'); |
828 | if (*cp++ != ' ') | |
829 | SCREWUP("size not delimited"); | |
f09aa22c | 830 | if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) { |
831 | run_err("error: unexpected filename: %s", cp); | |
832 | exit(1); | |
833 | } | |
8efc0c15 | 834 | if (targisdir) { |
835 | static char *namebuf; | |
2ceb8101 | 836 | static size_t cursize; |
8efc0c15 | 837 | size_t need; |
838 | ||
839 | need = strlen(targ) + strlen(cp) + 250; | |
0ed28836 | 840 | if (need > cursize) { |
841 | if (namebuf) | |
842 | xfree(namebuf); | |
5260325f | 843 | namebuf = xmalloc(need); |
0ed28836 | 844 | cursize = need; |
845 | } | |
846 | (void) snprintf(namebuf, need, "%s%s%s", targ, | |
39f9599a | 847 | strcmp(targ, "/") ? "/" : "", cp); |
8efc0c15 | 848 | np = namebuf; |
849 | } else | |
850 | np = targ; | |
851 | curfile = cp; | |
852 | exists = stat(np, &stb) == 0; | |
853 | if (buf[0] == 'D') { | |
854 | int mod_flag = pflag; | |
f09aa22c | 855 | if (!iamrecursive) |
856 | SCREWUP("received directory without -r"); | |
8efc0c15 | 857 | if (exists) { |
858 | if (!S_ISDIR(stb.st_mode)) { | |
859 | errno = ENOTDIR; | |
860 | goto bad; | |
861 | } | |
862 | if (pflag) | |
5260325f | 863 | (void) chmod(np, mode); |
8efc0c15 | 864 | } else { |
5260325f | 865 | /* Handle copying from a read-only |
866 | directory */ | |
8efc0c15 | 867 | mod_flag = 1; |
868 | if (mkdir(np, mode | S_IRWXU) < 0) | |
869 | goto bad; | |
870 | } | |
5c470997 | 871 | vect[0] = xstrdup(np); |
8efc0c15 | 872 | sink(1, vect); |
873 | if (setimes) { | |
874 | setimes = 0; | |
e68225b2 | 875 | if (utimes(vect[0], tv) < 0) |
5260325f | 876 | run_err("%s: set times: %s", |
e68225b2 | 877 | vect[0], strerror(errno)); |
8efc0c15 | 878 | } |
879 | if (mod_flag) | |
e68225b2 | 880 | (void) chmod(vect[0], mode); |
881 | if (vect[0]) | |
882 | xfree(vect[0]); | |
8efc0c15 | 883 | continue; |
884 | } | |
885 | omode = mode; | |
886 | mode |= S_IWRITE; | |
3e4fc5f9 | 887 | if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { |
8efc0c15 | 888 | bad: run_err("%s: %s", np, strerror(errno)); |
889 | continue; | |
890 | } | |
dc54438a | 891 | (void) atomicio(vwrite, remout, "", 1); |
8efc0c15 | 892 | if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) { |
5260325f | 893 | (void) close(ofd); |
8efc0c15 | 894 | continue; |
895 | } | |
896 | cp = bp->buf; | |
897 | wrerr = NO; | |
898 | ||
8efc0c15 | 899 | statbytes = 0; |
b65c3807 | 900 | if (showprogress) |
901 | start_progress_meter(curfile, size, &statbytes); | |
8efc0c15 | 902 | for (count = i = 0; i < size; i += 4096) { |
903 | amt = 4096; | |
904 | if (i + amt > size) | |
905 | amt = size - i; | |
906 | count += amt; | |
907 | do { | |
f581b6e8 | 908 | j = atomicio(read, remin, cp, amt); |
05624c18 | 909 | if (j == 0) { |
8efc0c15 | 910 | run_err("%s", j ? strerror(errno) : |
e5ff6ecf | 911 | "dropped connection"); |
8efc0c15 | 912 | exit(1); |
913 | } | |
914 | amt -= j; | |
915 | cp += j; | |
5260325f | 916 | statbytes += j; |
8efc0c15 | 917 | } while (amt > 0); |
b6453d99 | 918 | |
10adbb52 | 919 | if (limit_rate) |
6ea3c52a | 920 | bwlimit(4096); |
921 | ||
8efc0c15 | 922 | if (count == bp->cnt) { |
923 | /* Keep reading so we stay sync'd up. */ | |
924 | if (wrerr == NO) { | |
05624c18 | 925 | if (atomicio(vwrite, ofd, bp->buf, |
926 | count) != count) { | |
8efc0c15 | 927 | wrerr = YES; |
05624c18 | 928 | wrerrno = errno; |
8efc0c15 | 929 | } |
930 | } | |
931 | count = 0; | |
932 | cp = bp->buf; | |
933 | } | |
934 | } | |
935 | if (showprogress) | |
b65c3807 | 936 | stop_progress_meter(); |
8efc0c15 | 937 | if (count != 0 && wrerr == NO && |
05624c18 | 938 | atomicio(vwrite, ofd, bp->buf, count) != count) { |
8efc0c15 | 939 | wrerr = YES; |
05624c18 | 940 | wrerrno = errno; |
8efc0c15 | 941 | } |
25a6efd8 | 942 | if (wrerr == NO && ftruncate(ofd, size) != 0) { |
8efc0c15 | 943 | run_err("%s: truncate: %s", np, strerror(errno)); |
944 | wrerr = DISPLAYED; | |
945 | } | |
8efc0c15 | 946 | if (pflag) { |
947 | if (exists || omode != mode) | |
77bb0bca | 948 | #ifdef HAVE_FCHMOD |
7e693c81 | 949 | if (fchmod(ofd, omode)) { |
77bb0bca | 950 | #else /* HAVE_FCHMOD */ |
7e693c81 | 951 | if (chmod(np, omode)) { |
77bb0bca | 952 | #endif /* HAVE_FCHMOD */ |
8efc0c15 | 953 | run_err("%s: set mode: %s", |
e5ff6ecf | 954 | np, strerror(errno)); |
7e693c81 | 955 | wrerr = DISPLAYED; |
956 | } | |
8efc0c15 | 957 | } else { |
958 | if (!exists && omode != mode) | |
77bb0bca | 959 | #ifdef HAVE_FCHMOD |
7e693c81 | 960 | if (fchmod(ofd, omode & ~mask)) { |
77bb0bca | 961 | #else /* HAVE_FCHMOD */ |
7e693c81 | 962 | if (chmod(np, omode & ~mask)) { |
77bb0bca | 963 | #endif /* HAVE_FCHMOD */ |
8efc0c15 | 964 | run_err("%s: set mode: %s", |
e5ff6ecf | 965 | np, strerror(errno)); |
7e693c81 | 966 | wrerr = DISPLAYED; |
967 | } | |
8efc0c15 | 968 | } |
704b1659 | 969 | if (close(ofd) == -1) { |
970 | wrerr = YES; | |
971 | wrerrno = errno; | |
972 | } | |
5260325f | 973 | (void) response(); |
8efc0c15 | 974 | if (setimes && wrerr == NO) { |
975 | setimes = 0; | |
188adeb2 | 976 | if (utimes(np, tv) < 0) { |
8efc0c15 | 977 | run_err("%s: set times: %s", |
e5ff6ecf | 978 | np, strerror(errno)); |
8efc0c15 | 979 | wrerr = DISPLAYED; |
980 | } | |
981 | } | |
5260325f | 982 | switch (wrerr) { |
8efc0c15 | 983 | case YES: |
984 | run_err("%s: %s", np, strerror(wrerrno)); | |
985 | break; | |
986 | case NO: | |
dc54438a | 987 | (void) atomicio(vwrite, remout, "", 1); |
8efc0c15 | 988 | break; |
989 | case DISPLAYED: | |
990 | break; | |
991 | } | |
992 | } | |
993 | screwup: | |
994 | run_err("protocol error: %s", why); | |
995 | exit(1); | |
996 | } | |
997 | ||
998 | int | |
d5bb9418 | 999 | response(void) |
8efc0c15 | 1000 | { |
1001 | char ch, *cp, resp, rbuf[2048]; | |
1002 | ||
1d1ffb87 | 1003 | if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp)) |
8efc0c15 | 1004 | lostconn(0); |
1005 | ||
1006 | cp = rbuf; | |
5260325f | 1007 | switch (resp) { |
1008 | case 0: /* ok */ | |
8efc0c15 | 1009 | return (0); |
1010 | default: | |
1011 | *cp++ = resp; | |
1012 | /* FALLTHROUGH */ | |
5260325f | 1013 | case 1: /* error, followed by error msg */ |
1014 | case 2: /* fatal error, "" */ | |
8efc0c15 | 1015 | do { |
1d1ffb87 | 1016 | if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) |
8efc0c15 | 1017 | lostconn(0); |
1018 | *cp++ = ch; | |
1019 | } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n'); | |
1020 | ||
1021 | if (!iamremote) | |
dc54438a | 1022 | (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf); |
8efc0c15 | 1023 | ++errs; |
1024 | if (resp == 1) | |
1025 | return (-1); | |
1026 | exit(1); | |
1027 | } | |
1028 | /* NOTREACHED */ | |
1029 | } | |
1030 | ||
1031 | void | |
d5bb9418 | 1032 | usage(void) |
8efc0c15 | 1033 | { |
f1278af7 | 1034 | (void) fprintf(stderr, |
433e60ac | 1035 | "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n" |
1036 | " [-l limit] [-o ssh_option] [-P port] [-S program]\n" | |
3015d321 | 1037 | " [[user@]host1:]file1 [...] [[user@]host2:]file2\n"); |
8efc0c15 | 1038 | exit(1); |
1039 | } | |
1040 | ||
1041 | void | |
5260325f | 1042 | run_err(const char *fmt,...) |
8efc0c15 | 1043 | { |
1044 | static FILE *fp; | |
1045 | va_list ap; | |
8efc0c15 | 1046 | |
1047 | ++errs; | |
1048 | if (fp == NULL && !(fp = fdopen(remout, "w"))) | |
1049 | return; | |
5260325f | 1050 | (void) fprintf(fp, "%c", 0x01); |
1051 | (void) fprintf(fp, "scp: "); | |
b92964b7 | 1052 | va_start(ap, fmt); |
5260325f | 1053 | (void) vfprintf(fp, fmt, ap); |
b92964b7 | 1054 | va_end(ap); |
5260325f | 1055 | (void) fprintf(fp, "\n"); |
1056 | (void) fflush(fp); | |
1057 | ||
1058 | if (!iamremote) { | |
b92964b7 | 1059 | va_start(ap, fmt); |
5260325f | 1060 | vfprintf(stderr, fmt, ap); |
b92964b7 | 1061 | va_end(ap); |
5260325f | 1062 | fprintf(stderr, "\n"); |
1063 | } | |
8efc0c15 | 1064 | } |
1065 | ||
8efc0c15 | 1066 | void |
cf3248b8 | 1067 | verifydir(char *cp) |
8efc0c15 | 1068 | { |
1069 | struct stat stb; | |
1070 | ||
1071 | if (!stat(cp, &stb)) { | |
1072 | if (S_ISDIR(stb.st_mode)) | |
1073 | return; | |
1074 | errno = ENOTDIR; | |
1075 | } | |
1076 | run_err("%s: %s", cp, strerror(errno)); | |
8f4ab41b | 1077 | killchild(0); |
8efc0c15 | 1078 | } |
1079 | ||
1080 | int | |
cf3248b8 | 1081 | okname(char *cp0) |
8efc0c15 | 1082 | { |
1083 | int c; | |
1084 | char *cp; | |
1085 | ||
1086 | cp = cp0; | |
1087 | do { | |
cb220a93 | 1088 | c = (int)*cp; |
8efc0c15 | 1089 | if (c & 0200) |
1090 | goto bad; | |
0ec7661a | 1091 | if (!isalpha(c) && !isdigit(c)) { |
1092 | switch (c) { | |
1093 | case '\'': | |
1094 | case '"': | |
1095 | case '`': | |
1096 | case ' ': | |
1097 | case '#': | |
1098 | goto bad; | |
1099 | default: | |
1100 | break; | |
1101 | } | |
1102 | } | |
8efc0c15 | 1103 | } while (*++cp); |
1104 | return (1); | |
1105 | ||
c8d54615 | 1106 | bad: fprintf(stderr, "%s: invalid user name\n", cp0); |
8efc0c15 | 1107 | return (0); |
1108 | } | |
1109 | ||
1110 | BUF * | |
cf3248b8 | 1111 | allocbuf(BUF *bp, int fd, int blksize) |
8efc0c15 | 1112 | { |
1113 | size_t size; | |
98a7c37b | 1114 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
8efc0c15 | 1115 | struct stat stb; |
1116 | ||
1117 | if (fstat(fd, &stb) < 0) { | |
1118 | run_err("fstat: %s", strerror(errno)); | |
1119 | return (0); | |
1120 | } | |
a0a14bcd | 1121 | size = roundup(stb.st_blksize, blksize); |
1122 | if (size == 0) | |
5260325f | 1123 | size = blksize; |
98a7c37b | 1124 | #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */ |
2b87da3b | 1125 | size = blksize; |
98a7c37b | 1126 | #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */ |
8efc0c15 | 1127 | if (bp->cnt >= size) |
1128 | return (bp); | |
5260325f | 1129 | if (bp->buf == NULL) |
1130 | bp->buf = xmalloc(size); | |
1131 | else | |
1132 | bp->buf = xrealloc(bp->buf, size); | |
46660a9e | 1133 | memset(bp->buf, 0, size); |
8efc0c15 | 1134 | bp->cnt = size; |
1135 | return (bp); | |
1136 | } | |
1137 | ||
1138 | void | |
cf3248b8 | 1139 | lostconn(int signo) |
8efc0c15 | 1140 | { |
1141 | if (!iamremote) | |
08c260ea | 1142 | write(STDERR_FILENO, "lost connection\n", 16); |
1143 | if (signo) | |
1144 | _exit(1); | |
1145 | else | |
1146 | exit(1); | |
8efc0c15 | 1147 | } |