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