Deleted Added
sdiff udiff text old ( 24348 ) new ( 25612 )
full compact
1/*
2 * Copyright (c) 1983, 1990, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $Id: rcp.c,v 1.11 1997/02/22 14:05:22 peter Exp $
34 */
35
36#ifndef lint
37static char const copyright[] =
38"@(#) Copyright (c) 1983, 1990, 1992, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40#endif /* not lint */
41
42#ifndef lint
43static char const sccsid[] = "@(#)rcp.c 8.2 (Berkeley) 4/2/94";
44#endif /* not lint */
45
46#include <sys/param.h>
47#include <sys/stat.h>
48#include <sys/time.h>
49#include <sys/socket.h>
50#include <netinet/in.h>
51#include <netinet/in_systm.h>
52#include <netinet/ip.h>
53
54#include <ctype.h>
55#include <dirent.h>
56#include <err.h>
57#include <errno.h>
58#include <fcntl.h>
59#include <netdb.h>
60#include <pwd.h>
61#include <signal.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <string.h>
66#include <unistd.h>
67
68#include "pathnames.h"
69#include "extern.h"
70
71#ifdef KERBEROS
72#include <des.h>
73#include <kerberosIV/krb.h>
74
75#include "../../usr.bin/rlogin/krb.h"
76
77char dst_realm_buf[REALM_SZ];
78char *dest_realm = NULL;
79int use_kerberos = 1;
80CREDENTIALS cred;
81Key_schedule schedule;
82extern char *krb_realmofhost();
83#ifdef CRYPT
84int doencrypt = 0;
85#define OPTIONS "dfKk:prtx"
86#else
87#define OPTIONS "dfKk:prt"
88#endif
89#else
90#define OPTIONS "dfprt"
91#endif
92
93struct passwd *pwd;
94u_short port;
95uid_t userid;
96int errs, rem;
97int pflag, iamremote, iamrecursive, targetshouldbedirectory;
98
99#define CMDNEEDS 64
100char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
101
102#ifdef KERBEROS
103int kerberos __P((char **, char *, char *, char *));
104void oldw __P((const char *, ...));
105#endif
106int response __P((void));
107void rsource __P((char *, struct stat *));
108void sink __P((int, char *[]));
109void source __P((int, char *[]));
110void tolocal __P((int, char *[]));
111void toremote __P((char *, int, char *[]));
112void usage __P((void));
113
114int
115main(argc, argv)
116 int argc;
117 char *argv[];
118{
119 struct servent *sp;
120 int ch, fflag, tflag;
121 char *targ, *shell;
122
123 fflag = tflag = 0;
124 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
125 switch(ch) { /* User-visible flags. */
126 case 'K':
127#ifdef KERBEROS
128 use_kerberos = 0;
129#endif
130 break;
131#ifdef KERBEROS
132 case 'k':
133 dest_realm = dst_realm_buf;
134 (void)strncpy(dst_realm_buf, optarg, REALM_SZ);
135 break;
136#ifdef CRYPT
137 case 'x':
138 doencrypt = 1;
139 /* des_set_key(cred.session, schedule); */
140 break;
141#endif
142#endif
143 case 'p':
144 pflag = 1;
145 break;
146 case 'r':
147 iamrecursive = 1;
148 break;
149 /* Server options. */
150 case 'd':
151 targetshouldbedirectory = 1;
152 break;
153 case 'f': /* "from" */
154 iamremote = 1;
155 fflag = 1;
156 break;
157 case 't': /* "to" */
158 iamremote = 1;
159 tflag = 1;
160 break;
161 case '?':
162 default:
163 usage();
164 }
165 argc -= optind;
166 argv += optind;
167
168#ifdef KERBEROS
169 if (use_kerberos) {
170#ifdef CRYPT
171 shell = doencrypt ? "ekshell" : "kshell";
172#else
173 shell = "kshell";
174#endif
175 if ((sp = getservbyname(shell, "tcp")) == NULL) {
176 use_kerberos = 0;
177 oldw("can't get entry for %s/tcp service", shell);
178 sp = getservbyname(shell = "shell", "tcp");
179 }
180 } else
181 sp = getservbyname(shell = "shell", "tcp");
182#else
183 sp = getservbyname(shell = "shell", "tcp");
184#endif
185 if (sp == NULL)
186 errx(1, "%s/tcp: unknown service", shell);
187 port = sp->s_port;
188
189 if ((pwd = getpwuid(userid = getuid())) == NULL)
190 errx(1, "unknown user %d", (int)userid);
191
192 rem = STDIN_FILENO; /* XXX */
193
194 if (fflag) { /* Follow "protocol", send data. */
195 (void)response();
196 (void)setuid(userid);
197 source(argc, argv);
198 exit(errs);
199 }
200
201 if (tflag) { /* Receive data. */
202 (void)setuid(userid);
203 sink(argc, argv);
204 exit(errs);
205 }
206
207 if (argc < 2)
208 usage();
209 if (argc > 2)
210 targetshouldbedirectory = 1;
211
212 rem = -1;
213 /* Command to be executed on remote system using "rsh". */
214#ifdef KERBEROS
215 (void)snprintf(cmd, sizeof(cmd),
216 "rcp%s%s%s%s", iamrecursive ? " -r" : "",
217#ifdef CRYPT
218 (doencrypt && use_kerberos ? " -x" : ""),
219#else
220 "",
221#endif
222 pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
223#else
224 (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
225 iamrecursive ? " -r" : "", pflag ? " -p" : "",
226 targetshouldbedirectory ? " -d" : "");
227#endif
228
229 (void)signal(SIGPIPE, lostconn);
230
231 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
232 toremote(targ, argc, argv);
233 else {
234 tolocal(argc, argv); /* Dest is local host. */
235 if (targetshouldbedirectory)
236 verifydir(argv[argc - 1]);
237 }
238 exit(errs);
239}
240
241void
242toremote(targ, argc, argv)
243 char *targ, *argv[];
244 int argc;
245{
246 int i, len, tos;
247 char *bp, *host, *src, *suser, *thost, *tuser;
248
249 *targ++ = 0;
250 if (*targ == 0)
251 targ = ".";
252
253 if ((thost = strchr(argv[argc - 1], '@'))) {
254 /* user@host */
255 *thost++ = 0;
256 tuser = argv[argc - 1];
257 if (*tuser == '\0')
258 tuser = NULL;
259 else if (!okname(tuser))
260 exit(1);
261 } else {
262 thost = argv[argc - 1];
263 tuser = NULL;
264 }
265
266 for (i = 0; i < argc - 1; i++) {
267 src = colon(argv[i]);
268 if (src) { /* remote to remote */
269 *src++ = 0;
270 if (*src == 0)
271 src = ".";
272 host = strchr(argv[i], '@');
273 len = strlen(_PATH_RSH) + strlen(argv[i]) +
274 strlen(src) + (tuser ? strlen(tuser) : 0) +
275 strlen(thost) + strlen(targ) + CMDNEEDS + 20;
276 if (!(bp = malloc(len)))
277 err(1, NULL);
278 if (host) {
279 *host++ = 0;
280 suser = argv[i];
281 if (*suser == '\0')
282 suser = pwd->pw_name;
283 else if (!okname(suser))
284 continue;
285 (void)snprintf(bp, len,
286 "%s %s -l %s -n %s %s '%s%s%s:%s'",
287 _PATH_RSH, host, suser, cmd, src,
288 tuser ? tuser : "", tuser ? "@" : "",
289 thost, targ);
290 } else
291 (void)snprintf(bp, len,
292 "exec %s %s -n %s %s '%s%s%s:%s'",
293 _PATH_RSH, argv[i], cmd, src,
294 tuser ? tuser : "", tuser ? "@" : "",
295 thost, targ);
296 (void)susystem(bp, userid);
297 (void)free(bp);
298 } else { /* local to remote */
299 if (rem == -1) {
300 len = strlen(targ) + CMDNEEDS + 20;
301 if (!(bp = malloc(len)))
302 err(1, NULL);
303 (void)snprintf(bp, len, "%s -t %s", cmd, targ);
304 host = thost;
305#ifdef KERBEROS
306 if (use_kerberos)
307 rem = kerberos(&host, bp,
308 pwd->pw_name,
309 tuser ? tuser : pwd->pw_name);
310 else
311#endif
312 rem = rcmd(&host, port, pwd->pw_name,
313 tuser ? tuser : pwd->pw_name,
314 bp, 0);
315 if (rem < 0)
316 exit(1);
317 tos = IPTOS_THROUGHPUT;
318 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
319 &tos, sizeof(int)) < 0)
320 warn("TOS (ignored)");
321 if (response() < 0)
322 exit(1);
323 (void)free(bp);
324 (void)setuid(userid);
325 }
326 source(1, argv+i);
327 }
328 }
329}
330
331void
332tolocal(argc, argv)
333 int argc;
334 char *argv[];
335{
336 int i, len, tos;
337 char *bp, *host, *src, *suser;
338
339 for (i = 0; i < argc - 1; i++) {
340 if (!(src = colon(argv[i]))) { /* Local to local. */
341 len = strlen(_PATH_CP) + strlen(argv[i]) +
342 strlen(argv[argc - 1]) + 20;
343 if (!(bp = malloc(len)))
344 err(1, NULL);
345 (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
346 iamrecursive ? " -r" : "", pflag ? " -p" : "",
347 argv[i], argv[argc - 1]);
348 if (susystem(bp, userid))
349 ++errs;
350 (void)free(bp);
351 continue;
352 }
353 *src++ = 0;
354 if (*src == 0)
355 src = ".";
356 if ((host = strchr(argv[i], '@')) == NULL) {
357 host = argv[i];
358 suser = pwd->pw_name;
359 } else {
360 *host++ = 0;
361 suser = argv[i];
362 if (*suser == '\0')
363 suser = pwd->pw_name;
364 else if (!okname(suser))
365 continue;
366 }
367 len = strlen(src) + CMDNEEDS + 20;
368 if ((bp = malloc(len)) == NULL)
369 err(1, NULL);
370 (void)snprintf(bp, len, "%s -f %s", cmd, src);
371 rem =
372#ifdef KERBEROS
373 use_kerberos ?
374 kerberos(&host, bp, pwd->pw_name, suser) :
375#endif
376 rcmd(&host, port, pwd->pw_name, suser, bp, 0);
377 (void)free(bp);
378 if (rem < 0) {
379 ++errs;
380 continue;
381 }
382 (void)seteuid(userid);
383 tos = IPTOS_THROUGHPUT;
384 if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
385 warn("TOS (ignored)");
386 sink(1, argv + argc - 1);
387 (void)seteuid(0);
388 (void)close(rem);
389 rem = -1;
390 }
391}
392
393void
394source(argc, argv)
395 int argc;
396 char *argv[];
397{
398 struct stat stb;
399 static BUF buffer;
400 BUF *bp;
401 off_t i;
402 int amt, fd, haderr, indx, result;
403 char *last, *name, buf[BUFSIZ];
404
405 for (indx = 0; indx < argc; ++indx) {
406 name = argv[indx];
407 if ((fd = open(name, O_RDONLY, 0)) < 0)
408 goto syserr;
409 if (fstat(fd, &stb)) {
410syserr: run_err("%s: %s", name, strerror(errno));
411 goto next;
412 }
413 switch (stb.st_mode & S_IFMT) {
414 case S_IFREG:
415 break;
416 case S_IFDIR:
417 if (iamrecursive) {
418 rsource(name, &stb);
419 goto next;
420 }
421 /* FALLTHROUGH */
422 default:
423 run_err("%s: not a regular file", name);
424 goto next;
425 }
426 if ((last = strrchr(name, '/')) == NULL)
427 last = name;
428 else
429 ++last;
430 if (pflag) {
431 /*
432 * Make it compatible with possible future
433 * versions expecting microseconds.
434 */
435 (void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
436 stb.st_mtimespec.tv_sec, stb.st_atimespec.tv_sec);
437 (void)write(rem, buf, strlen(buf));
438 if (response() < 0)
439 goto next;
440 }
441#define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
442 (void)snprintf(buf, sizeof(buf), "C%04o %qd %s\n",
443 stb.st_mode & MODEMASK, stb.st_size, last);
444 (void)write(rem, buf, strlen(buf));
445 if (response() < 0)
446 goto next;
447 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
448next: (void)close(fd);
449 continue;
450 }
451
452 /* Keep writing after an error so that we stay sync'd up. */
453 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
454 amt = bp->cnt;
455 if (i + amt > stb.st_size)
456 amt = stb.st_size - i;
457 if (!haderr) {
458 result = read(fd, bp->buf, amt);
459 if (result != amt)
460 haderr = result >= 0 ? EIO : errno;
461 }
462 if (haderr)
463 (void)write(rem, bp->buf, amt);
464 else {
465 result = write(rem, bp->buf, amt);
466 if (result != amt)
467 haderr = result >= 0 ? EIO : errno;
468 }
469 }
470 if (close(fd) && !haderr)
471 haderr = errno;
472 if (!haderr)
473 (void)write(rem, "", 1);
474 else
475 run_err("%s: %s", name, strerror(haderr));
476 (void)response();
477 }
478}
479
480void
481rsource(name, statp)
482 char *name;
483 struct stat *statp;
484{
485 DIR *dirp;
486 struct dirent *dp;
487 char *last, *vect[1], path[MAXPATHLEN];
488
489 if (!(dirp = opendir(name))) {
490 run_err("%s: %s", name, strerror(errno));
491 return;
492 }
493 last = strrchr(name, '/');
494 if (last == 0)
495 last = name;
496 else
497 last++;
498 if (pflag) {
499 (void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
500 statp->st_mtimespec.tv_sec, statp->st_atimespec.tv_sec);
501 (void)write(rem, path, strlen(path));
502 if (response() < 0) {
503 closedir(dirp);
504 return;
505 }
506 }
507 (void)snprintf(path, sizeof(path),
508 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
509 (void)write(rem, path, strlen(path));
510 if (response() < 0) {
511 closedir(dirp);
512 return;
513 }
514 while ((dp = readdir(dirp))) {
515 if (dp->d_ino == 0)
516 continue;
517 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
518 continue;
519 if (strlen(name) + 1 + strlen(dp->d_name) >= MAXPATHLEN - 1) {
520 run_err("%s/%s: name too long", name, dp->d_name);
521 continue;
522 }
523 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
524 vect[0] = path;
525 source(1, vect);
526 }
527 (void)closedir(dirp);
528 (void)write(rem, "E\n", 2);
529 (void)response();
530}
531
532void
533sink(argc, argv)
534 int argc;
535 char *argv[];
536{
537 static BUF buffer;
538 struct stat stb;
539 struct timeval tv[2];
540 enum { YES, NO, DISPLAYED } wrerr;
541 BUF *bp;
542 off_t i, j;
543 int amt, count, exists, first, mask, mode, ofd, omode;
544 int setimes, size, targisdir, wrerrno = 0;
545 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ];
546
547#define atime tv[0]
548#define mtime tv[1]
549#define SCREWUP(str) { why = str; goto screwup; }
550
551 setimes = targisdir = 0;
552 mask = umask(0);
553 if (!pflag)
554 (void)umask(mask);
555 if (argc != 1) {
556 run_err("ambiguous target");
557 exit(1);
558 }
559 targ = *argv;
560 if (targetshouldbedirectory)
561 verifydir(targ);
562 (void)write(rem, "", 1);
563 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
564 targisdir = 1;
565 for (first = 1;; first = 0) {
566 cp = buf;
567 if (read(rem, cp, 1) <= 0)
568 return;
569 if (*cp++ == '\n')
570 SCREWUP("unexpected <newline>");
571 do {
572 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
573 SCREWUP("lost connection");
574 *cp++ = ch;
575 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
576 *cp = 0;
577
578 if (buf[0] == '\01' || buf[0] == '\02') {
579 if (iamremote == 0)
580 (void)write(STDERR_FILENO,
581 buf + 1, strlen(buf + 1));
582 if (buf[0] == '\02')
583 exit(1);
584 ++errs;
585 continue;
586 }
587 if (buf[0] == 'E') {
588 (void)write(rem, "", 1);
589 return;
590 }
591
592 if (ch == '\n')
593 *--cp = 0;
594
595 cp = buf;
596 if (*cp == 'T') {
597 setimes++;
598 cp++;
599 mtime.tv_sec = strtol(cp, &cp, 10);
600 if (!cp || *cp++ != ' ')
601 SCREWUP("mtime.sec not delimited");
602 mtime.tv_usec = strtol(cp, &cp, 10);
603 if (!cp || *cp++ != ' ')
604 SCREWUP("mtime.usec not delimited");
605 atime.tv_sec = strtol(cp, &cp, 10);
606 if (!cp || *cp++ != ' ')
607 SCREWUP("atime.sec not delimited");
608 atime.tv_usec = strtol(cp, &cp, 10);
609 if (!cp || *cp++ != '\0')
610 SCREWUP("atime.usec not delimited");
611 (void)write(rem, "", 1);
612 continue;
613 }
614 if (*cp != 'C' && *cp != 'D') {
615 /*
616 * Check for the case "rcp remote:foo\* local:bar".
617 * In this case, the line "No match." can be returned
618 * by the shell before the rcp command on the remote is
619 * executed so the ^Aerror_message convention isn't
620 * followed.
621 */
622 if (first) {
623 run_err("%s", cp);
624 exit(1);
625 }
626 SCREWUP("expected control record");
627 }
628 mode = 0;
629 for (++cp; cp < buf + 5; cp++) {
630 if (*cp < '0' || *cp > '7')
631 SCREWUP("bad mode");
632 mode = (mode << 3) | (*cp - '0');
633 }
634 if (*cp++ != ' ')
635 SCREWUP("mode not delimited");
636
637 for (size = 0; isdigit(*cp);)
638 size = size * 10 + (*cp++ - '0');
639 if (*cp++ != ' ')
640 SCREWUP("size not delimited");
641 if (targisdir) {
642 static char *namebuf;
643 static int cursize;
644 size_t need;
645
646 need = strlen(targ) + strlen(cp) + 250;
647 if (need > cursize) {
648 if (!(namebuf = malloc(need)))
649 run_err("%s", strerror(errno));
650 }
651 (void)snprintf(namebuf, need, "%s%s%s", targ,
652 *targ ? "/" : "", cp);
653 np = namebuf;
654 } else
655 np = targ;
656 exists = stat(np, &stb) == 0;
657 if (buf[0] == 'D') {
658 int mod_flag = pflag;
659 if (exists) {
660 if (!S_ISDIR(stb.st_mode)) {
661 errno = ENOTDIR;
662 goto bad;
663 }
664 if (pflag)
665 (void)chmod(np, mode);
666 } else {
667 /* Handle copying from a read-only directory */
668 mod_flag = 1;
669 if (mkdir(np, mode | S_IRWXU) < 0)
670 goto bad;
671 }
672 vect[0] = np;
673 sink(1, vect);
674 if (setimes) {
675 setimes = 0;
676 if (utimes(np, tv) < 0)
677 run_err("%s: set times: %s",
678 np, strerror(errno));
679 }
680 if (mod_flag)
681 (void)chmod(np, mode);
682 continue;
683 }
684 omode = mode;
685 mode |= S_IWRITE;
686 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
687bad: run_err("%s: %s", np, strerror(errno));
688 continue;
689 }
690 (void)write(rem, "", 1);
691 if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) {
692 (void)close(ofd);
693 continue;
694 }
695 cp = bp->buf;
696 wrerr = NO;
697 for (count = i = 0; i < size; i += BUFSIZ) {
698 amt = BUFSIZ;
699 if (i + amt > size)
700 amt = size - i;
701 count += amt;
702 do {
703 j = read(rem, cp, amt);
704 if (j <= 0) {
705 run_err("%s", j ? strerror(errno) :
706 "dropped connection");
707 exit(1);
708 }
709 amt -= j;
710 cp += j;
711 } while (amt > 0);
712 if (count == bp->cnt) {
713 /* Keep reading so we stay sync'd up. */
714 if (wrerr == NO) {
715 j = write(ofd, bp->buf, count);
716 if (j != count) {
717 wrerr = YES;
718 wrerrno = j >= 0 ? EIO : errno;
719 }
720 }
721 count = 0;
722 cp = bp->buf;
723 }
724 }
725 if (count != 0 && wrerr == NO &&
726 (j = write(ofd, bp->buf, count)) != count) {
727 wrerr = YES;
728 wrerrno = j >= 0 ? EIO : errno;
729 }
730 if (ftruncate(ofd, size)) {
731 run_err("%s: truncate: %s", np, strerror(errno));
732 wrerr = DISPLAYED;
733 }
734 if (pflag) {
735 if (exists || omode != mode)
736 if (fchmod(ofd, omode))
737 run_err("%s: set mode: %s",
738 np, strerror(errno));
739 } else {
740 if (!exists && omode != mode)
741 if (fchmod(ofd, omode & ~mask))
742 run_err("%s: set mode: %s",
743 np, strerror(errno));
744 }
745 (void)close(ofd);
746 (void)response();
747 if (setimes && wrerr == NO) {
748 setimes = 0;
749 if (utimes(np, tv) < 0) {
750 run_err("%s: set times: %s",
751 np, strerror(errno));
752 wrerr = DISPLAYED;
753 }
754 }
755 switch(wrerr) {
756 case YES:
757 run_err("%s: %s", np, strerror(wrerrno));
758 break;
759 case NO:
760 (void)write(rem, "", 1);
761 break;
762 case DISPLAYED:
763 break;
764 }
765 }
766screwup:
767 run_err("protocol error: %s", why);
768 exit(1);
769}
770
771#ifdef KERBEROS
772int
773kerberos(host, bp, locuser, user)
774 char **host, *bp, *locuser, *user;
775{
776 struct servent *sp;
777
778again:
779 if (use_kerberos) {
780 rem = KSUCCESS;
781 errno = 0;
782 if (dest_realm == NULL)
783 dest_realm = krb_realmofhost(*host);
784 rem =
785#ifdef CRYPT
786 doencrypt ?
787 krcmd_mutual(host,
788 port, user, bp, 0, dest_realm, &cred, schedule) :
789#endif
790 krcmd(host, port, user, bp, 0, dest_realm);
791
792 if (rem < 0) {
793 use_kerberos = 0;
794 if ((sp = getservbyname("shell", "tcp")) == NULL)
795 errx(1, "unknown service shell/tcp");
796 if (errno == ECONNREFUSED)
797 oldw("remote host doesn't support Kerberos");
798 else if (errno == ENOENT)
799 oldw("can't provide Kerberos authentication data");
800 port = sp->s_port;
801 goto again;
802 }
803 } else {
804#ifdef CRYPT
805 if (doencrypt)
806 errx(1,
807 "the -x option requires Kerberos authentication");
808#endif
809 rem = rcmd(host, port, locuser, user, bp, 0);
810 }
811 return (rem);
812}
813#endif /* KERBEROS */
814
815int
816response()
817{
818 char ch, *cp, resp, rbuf[BUFSIZ];
819
820 if (read(rem, &resp, sizeof(resp)) != sizeof(resp))
821 lostconn(0);
822
823 cp = rbuf;
824 switch(resp) {
825 case 0: /* ok */
826 return (0);
827 default:
828 *cp++ = resp;
829 /* FALLTHROUGH */
830 case 1: /* error, followed by error msg */
831 case 2: /* fatal error, "" */
832 do {
833 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
834 lostconn(0);
835 *cp++ = ch;
836 } while (cp < &rbuf[BUFSIZ] && ch != '\n');
837
838 if (!iamremote)
839 (void)write(STDERR_FILENO, rbuf, cp - rbuf);
840 ++errs;
841 if (resp == 1)
842 return (-1);
843 exit(1);
844 }
845 /* NOTREACHED */
846}
847
848void
849usage()
850{
851#ifdef KERBEROS
852#ifdef CRYPT
853 (void)fprintf(stderr, "%s\n\t%s\n",
854 "usage: rcp [-Kpx] [-k realm] f1 f2",
855 "or: rcp [-Kprx] [-k realm] f1 ... fn directory");
856#else
857 (void)fprintf(stderr, "%s\n\t%s\n",
858 "usage: rcp [-Kp] [-k realm] f1 f2",
859 "or: rcp [-Kpr] [-k realm] f1 ... fn directory");
860#endif
861#else
862 (void)fprintf(stderr,
863 "usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n");
864#endif
865 exit(1);
866}
867
868#if __STDC__
869#include <stdarg.h>
870#else
871#include <varargs.h>
872#endif
873
874#ifdef KERBEROS
875void
876#if __STDC__
877oldw(const char *fmt, ...)
878#else
879oldw(fmt, va_alist)
880 char *fmt;
881 va_dcl
882#endif
883{
884 va_list ap;
885#if __STDC__
886 va_start(ap, fmt);
887#else
888 va_start(ap);
889#endif
890 (void)fprintf(stderr, "rcp: ");
891 (void)vfprintf(stderr, fmt, ap);
892 (void)fprintf(stderr, ", using standard rcp\n");
893 va_end(ap);
894}
895#endif
896
897void
898#if __STDC__
899run_err(const char *fmt, ...)
900#else
901run_err(fmt, va_alist)
902 char *fmt;
903 va_dcl
904#endif
905{
906 static FILE *fp;
907 va_list ap;
908#if __STDC__
909 va_start(ap, fmt);
910#else
911 va_start(ap);
912#endif
913
914 ++errs;
915 if (fp == NULL && !(fp = fdopen(rem, "w")))
916 return;
917 (void)fprintf(fp, "%c", 0x01);
918 (void)fprintf(fp, "rcp: ");
919 (void)vfprintf(fp, fmt, ap);
920 (void)fprintf(fp, "\n");
921 (void)fflush(fp);
922
923 if (!iamremote)
924 vwarnx(fmt, ap);
925
926 va_end(ap);
927}