rcp.c revision 1.18 1 /* $NetBSD: rcp.c,v 1.18 1997/10/19 13:12:04 mycroft 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.18 1997/10/19 13:12:04 mycroft 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)) != -1)
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 %ld %ld %ld\n",
429 (long)stb.st_mtimespec.tv_sec,
430 (long)stb.st_mtimespec.tv_nsec / 1000,
431 (long)stb.st_atimespec.tv_sec,
432 (long)stb.st_atimespec.tv_nsec / 1000);
433 (void)write(rem, buf, strlen(buf));
434 if (response() < 0)
435 goto next;
436 }
437 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
438 (void)snprintf(buf, sizeof(buf), "C%04o %qd %s\n",
439 stb.st_mode & MODEMASK, (long long)stb.st_size, last);
440 (void)write(rem, buf, strlen(buf));
441 if (response() < 0)
442 goto next;
443 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
444 next: (void)close(fd);
445 continue;
446 }
447
448 /* Keep writing after an error so that we stay sync'd up. */
449 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
450 amt = bp->cnt;
451 if (i + amt > stb.st_size)
452 amt = stb.st_size - i;
453 if (!haderr) {
454 result = read(fd, bp->buf, amt);
455 if (result != amt)
456 haderr = result >= 0 ? EIO : errno;
457 }
458 if (haderr)
459 (void)write(rem, bp->buf, amt);
460 else {
461 result = write(rem, bp->buf, amt);
462 if (result != amt)
463 haderr = result >= 0 ? EIO : errno;
464 }
465 }
466 if (close(fd) && !haderr)
467 haderr = errno;
468 if (!haderr)
469 (void)write(rem, "", 1);
470 else
471 run_err("%s: %s", name, strerror(haderr));
472 (void)response();
473 }
474 }
475
476 void
477 rsource(name, statp)
478 char *name;
479 struct stat *statp;
480 {
481 DIR *dirp;
482 struct dirent *dp;
483 char *last, *vect[1], path[MAXPATHLEN];
484
485 if (!(dirp = opendir(name))) {
486 run_err("%s: %s", name, strerror(errno));
487 return;
488 }
489 last = strrchr(name, '/');
490 if (last == 0)
491 last = name;
492 else
493 last++;
494 if (pflag) {
495 (void)snprintf(path, sizeof(path), "T%ld %ld %ld %ld\n",
496 (long)statp->st_mtimespec.tv_sec,
497 (long)statp->st_mtimespec.tv_nsec / 1000,
498 (long)statp->st_atimespec.tv_sec,
499 (long)statp->st_atimespec.tv_nsec / 1000);
500 (void)write(rem, path, strlen(path));
501 if (response() < 0) {
502 closedir(dirp);
503 return;
504 }
505 }
506 (void)snprintf(path, sizeof(path),
507 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
508 (void)write(rem, path, strlen(path));
509 if (response() < 0) {
510 closedir(dirp);
511 return;
512 }
513 while ((dp = readdir(dirp)) != NULL) {
514 if (dp->d_ino == 0)
515 continue;
516 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
517 continue;
518 if (strlen(name) + 1 + strlen(dp->d_name) >= MAXPATHLEN - 1) {
519 run_err("%s/%s: name too long", name, dp->d_name);
520 continue;
521 }
522 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
523 vect[0] = path;
524 source(1, vect);
525 }
526 (void)closedir(dirp);
527 (void)write(rem, "E\n", 2);
528 (void)response();
529 }
530
531 void
532 sink(argc, argv)
533 int argc;
534 char *argv[];
535 {
536 static BUF buffer;
537 struct stat stb;
538 struct timeval tv[2];
539 enum { YES, NO, DISPLAYED } wrerr;
540 BUF *bp;
541 off_t i, j;
542 int amt, count, exists, first, mask, mode, ofd, omode;
543 int setimes, size, targisdir;
544 int wrerrno = 0; /* pacify gcc */
545 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ];
546
547 #define atime tv[0]
548 #define mtime tv[1]
549 #define SCREWUP(str) { why = str; goto screwup; }
550
551 setimes = targisdir = 0;
552 mask = umask(0);
553 if (!pflag)
554 (void)umask(mask);
555 if (argc != 1) {
556 run_err("ambiguous target");
557 exit(1);
558 }
559 targ = *argv;
560 if (targetshouldbedirectory)
561 verifydir(targ);
562 (void)write(rem, "", 1);
563 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
564 targisdir = 1;
565 for (first = 1;; first = 0) {
566 cp = buf;
567 if (read(rem, cp, 1) <= 0)
568 return;
569 if (*cp++ == '\n')
570 SCREWUP("unexpected <newline>");
571 do {
572 if (read(rem, &ch, sizeof(ch)) != sizeof(ch))
573 SCREWUP("lost connection");
574 *cp++ = ch;
575 } while (cp < &buf[BUFSIZ - 1] && ch != '\n');
576 *cp = 0;
577
578 if (buf[0] == '\01' || buf[0] == '\02') {
579 if (iamremote == 0)
580 (void)write(STDERR_FILENO,
581 buf + 1, strlen(buf + 1));
582 if (buf[0] == '\02')
583 exit(1);
584 ++errs;
585 continue;
586 }
587 if (buf[0] == 'E') {
588 (void)write(rem, "", 1);
589 return;
590 }
591
592 if (ch == '\n')
593 *--cp = 0;
594
595 #define getnum(t) (t) = 0; while (isdigit(*cp)) (t) = (t) * 10 + (*cp++ - '0');
596 cp = buf;
597 if (*cp == 'T') {
598 setimes++;
599 cp++;
600 getnum(mtime.tv_sec);
601 if (*cp++ != ' ')
602 SCREWUP("mtime.sec not delimited");
603 getnum(mtime.tv_usec);
604 if (*cp++ != ' ')
605 SCREWUP("mtime.usec not delimited");
606 getnum(atime.tv_sec);
607 if (*cp++ != ' ')
608 SCREWUP("atime.sec not delimited");
609 getnum(atime.tv_usec);
610 if (*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) {
688 bad: 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 if (setimes && wrerr == NO) {
747 setimes = 0;
748 if (futimes(ofd, tv) < 0) {
749 run_err("%s: set times: %s",
750 np, strerror(errno));
751 wrerr = DISPLAYED;
752 }
753 }
754 (void)close(ofd);
755 (void)response();
756 switch(wrerr) {
757 case YES:
758 run_err("%s: write: %s", np, strerror(wrerrno));
759 break;
760 case NO:
761 (void)write(rem, "", 1);
762 break;
763 case DISPLAYED:
764 break;
765 }
766 }
767 screwup:
768 run_err("protocol error: %s", why);
769 exit(1);
770 }
771
772 #ifdef KERBEROS
773 int
774 kerberos(host, bp, locuser, user)
775 char **host, *bp, *locuser, *user;
776 {
777 struct servent *sp;
778
779 again:
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
816 int
817 response()
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
849 void
850 usage()
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
876 void
877 #if __STDC__
878 oldw(const char *fmt, ...)
879 #else
880 oldw(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
898 void
899 #if __STDC__
900 run_err(const char *fmt, ...)
901 #else
902 run_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 }
929