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