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