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