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