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