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