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