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