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