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