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