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