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