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