]> git.saurik.com Git - apple/network_cmds.git/blob - rcp.tproj/rcp.c
e78621f9819640386171e1dd969e08d85bb1553a
[apple/network_cmds.git] / rcp.tproj / rcp.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1983, 1990, 1992, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56
57 #include <sys/param.h>
58 #include <sys/stat.h>
59 #include <sys/time.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64
65 #include <ctype.h>
66 #include <dirent.h>
67 #include <err.h>
68 #include <errno.h>
69 #include <fcntl.h>
70 #include <netdb.h>
71 #include <pwd.h>
72 #include <signal.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <string.h>
77 #include <unistd.h>
78
79 #include "pathnames.h"
80 #include "extern.h"
81
82 #ifdef KERBEROS
83 #include <kerberosIV/des.h>
84 #include <kerberosIV/krb.h>
85
86 char dst_realm_buf[REALM_SZ];
87 char *dest_realm = NULL;
88 int use_kerberos = 1;
89 CREDENTIALS cred;
90 Key_schedule schedule;
91 extern char *krb_realmofhost();
92 #ifdef CRYPT
93 int doencrypt = 0;
94 #define OPTIONS "dfKk:prtx"
95 #else
96 #define OPTIONS "dfKk:prt"
97 #endif
98 #else
99 #define OPTIONS "dfprt"
100 #endif
101
102 struct passwd *pwd;
103 u_short port;
104 uid_t userid;
105 int errs, rem;
106 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
107
108 #define CMDNEEDS 64
109 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
110
111 #ifdef KERBEROS
112 int kerberos __P((char **, char *, char *, char *));
113 void oldw __P((const char *, ...));
114 #endif
115 int response __P((void));
116 void rsource __P((char *, struct stat *));
117 void sink __P((int, char *[]));
118 void source __P((int, char *[]));
119 void tolocal __P((int, char *[]));
120 void toremote __P((char *, int, char *[]));
121 void usage __P((void));
122
123 int
124 main(argc, argv)
125 int argc;
126 char *argv[];
127 {
128 struct servent *sp;
129 int ch, fflag, tflag;
130 char *targ, *shell;
131
132 fflag = tflag = 0;
133 while ((ch = getopt(argc, argv, OPTIONS)) != EOF)
134 switch(ch) { /* User-visible flags. */
135 case 'K':
136 #ifdef KERBEROS
137 use_kerberos = 0;
138 #endif
139 break;
140 #ifdef KERBEROS
141 case 'k':
142 dest_realm = dst_realm_buf;
143 (void)strncpy(dst_realm_buf, optarg, REALM_SZ);
144 break;
145 #ifdef CRYPT
146 case 'x':
147 doencrypt = 1;
148 /* des_set_key(cred.session, schedule); */
149 break;
150 #endif
151 #endif
152 case 'p':
153 pflag = 1;
154 break;
155 case 'r':
156 iamrecursive = 1;
157 break;
158 /* Server options. */
159 case 'd':
160 targetshouldbedirectory = 1;
161 break;
162 case 'f': /* "from" */
163 iamremote = 1;
164 fflag = 1;
165 break;
166 case 't': /* "to" */
167 iamremote = 1;
168 tflag = 1;
169 break;
170 case '?':
171 default:
172 usage();
173 }
174 argc -= optind;
175 argv += optind;
176
177 #ifdef KERBEROS
178 if (use_kerberos) {
179 #ifdef CRYPT
180 shell = doencrypt ? "ekshell" : "kshell";
181 #else
182 shell = "kshell";
183 #endif
184 if ((sp = getservbyname(shell, "tcp")) == NULL) {
185 use_kerberos = 0;
186 oldw("can't get entry for %s/tcp service", shell);
187 sp = getservbyname(shell = "shell", "tcp");
188 }
189 } else
190 sp = getservbyname(shell = "shell", "tcp");
191 #else
192 sp = getservbyname(shell = "shell", "tcp");
193 #endif
194 if (sp == NULL)
195 errx(1, "%s/tcp: unknown service", shell);
196 port = sp->s_port;
197
198 if ((pwd = getpwuid(userid = getuid())) == NULL)
199 errx(1, "unknown user %d", (int)userid);
200
201 rem = STDIN_FILENO; /* XXX */
202
203 if (fflag) { /* Follow "protocol", send data. */
204 (void)response();
205 (void)setuid(userid);
206 source(argc, argv);
207 exit(errs);
208 }
209
210 if (tflag) { /* Receive data. */
211 (void)setuid(userid);
212 sink(argc, argv);
213 exit(errs);
214 }
215
216 if (argc < 2)
217 usage();
218 if (argc > 2)
219 targetshouldbedirectory = 1;
220
221 rem = -1;
222 /* Command to be executed on remote system using "rsh". */
223 #ifdef KERBEROS
224 (void)snprintf(cmd, sizeof(cmd),
225 "rcp%s%s%s%s", iamrecursive ? " -r" : "",
226 #ifdef CRYPT
227 (doencrypt && use_kerberos ? " -x" : ""),
228 #else
229 "",
230 #endif
231 pflag ? " -p" : "", targetshouldbedirectory ? " -d" : "");
232 #else
233 (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s",
234 iamrecursive ? " -r" : "", pflag ? " -p" : "",
235 targetshouldbedirectory ? " -d" : "");
236 #endif
237
238 (void)signal(SIGPIPE, lostconn);
239
240 if (targ = colon(argv[argc - 1])) /* Dest is remote host. */
241 toremote(targ, argc, argv);
242 else {
243 tolocal(argc, argv); /* Dest is local host. */
244 if (targetshouldbedirectory)
245 verifydir(argv[argc - 1]);
246 }
247 exit(errs);
248 }
249
250 void
251 toremote(targ, argc, argv)
252 char *targ, *argv[];
253 int argc;
254 {
255 int i, len, tos;
256 char *bp, *host, *src, *suser, *thost, *tuser;
257
258 *targ++ = 0;
259 if (*targ == 0)
260 targ = ".";
261
262 if (thost = strchr(argv[argc - 1], '@')) {
263 /* user@host */
264 *thost++ = 0;
265 tuser = argv[argc - 1];
266 if (*tuser == '\0')
267 tuser = NULL;
268 else if (!okname(tuser))
269 exit(1);
270 } else {
271 thost = argv[argc - 1];
272 tuser = NULL;
273 }
274
275 for (i = 0; i < argc - 1; i++) {
276 src = colon(argv[i]);
277 if (src) { /* remote to remote */
278 *src++ = 0;
279 if (*src == 0)
280 src = ".";
281 host = strchr(argv[i], '@');
282 len = strlen(_PATH_RSH) + strlen(argv[i]) +
283 strlen(src) + (tuser ? strlen(tuser) : 0) +
284 strlen(thost) + strlen(targ) + CMDNEEDS + 20;
285 if (!(bp = malloc(len)))
286 err(1, NULL);
287 if (host) {
288 *host++ = 0;
289 suser = argv[i];
290 if (*suser == '\0')
291 suser = pwd->pw_name;
292 else if (!okname(suser))
293 continue;
294 (void)snprintf(bp, len,
295 "%s %s -l %s -n %s %s '%s%s%s:%s'",
296 _PATH_RSH, host, suser, cmd, src,
297 tuser ? tuser : "", tuser ? "@" : "",
298 thost, targ);
299 } else
300 (void)snprintf(bp, len,
301 "exec %s %s -n %s %s '%s%s%s:%s'",
302 _PATH_RSH, argv[i], cmd, src,
303 tuser ? tuser : "", tuser ? "@" : "",
304 thost, targ);
305 (void)susystem(bp, userid);
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,
317 pwd->pw_name,
318 tuser ? tuser : pwd->pw_name);
319 else
320 #endif
321 rem = rcmd(&host, port, pwd->pw_name,
322 tuser ? tuser : pwd->pw_name,
323 bp, 0);
324 if (rem < 0)
325 exit(1);
326 tos = IPTOS_THROUGHPUT;
327 if (setsockopt(rem, IPPROTO_IP, IP_TOS,
328 &tos, sizeof(int)) < 0)
329 warn("TOS (ignored)");
330 if (response() < 0)
331 exit(1);
332 (void)free(bp);
333 (void)setuid(userid);
334 }
335 source(1, argv+i);
336 }
337 }
338 }
339
340 void
341 tolocal(argc, argv)
342 int argc;
343 char *argv[];
344 {
345 int i, len, tos;
346 char *bp, *host, *src, *suser;
347
348 for (i = 0; i < argc - 1; i++) {
349 if (!(src = colon(argv[i]))) { /* Local to local. */
350 len = strlen(_PATH_CP) + strlen(argv[i]) +
351 strlen(argv[argc - 1]) + 20;
352 if (!(bp = malloc(len)))
353 err(1, NULL);
354 (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
355 iamrecursive ? " -r" : "", pflag ? " -p" : "",
356 argv[i], argv[argc - 1]);
357 if (susystem(bp, userid))
358 ++errs;
359 (void)free(bp);
360 continue;
361 }
362 *src++ = 0;
363 if (*src == 0)
364 src = ".";
365 if ((host = strchr(argv[i], '@')) == NULL) {
366 host = argv[i];
367 suser = pwd->pw_name;
368 } else {
369 *host++ = 0;
370 suser = argv[i];
371 if (*suser == '\0')
372 suser = pwd->pw_name;
373 else if (!okname(suser))
374 continue;
375 }
376 len = strlen(src) + CMDNEEDS + 20;
377 if ((bp = malloc(len)) == NULL)
378 err(1, NULL);
379 (void)snprintf(bp, len, "%s -f %s", cmd, src);
380 rem =
381 #ifdef KERBEROS
382 use_kerberos ?
383 kerberos(&host, bp, pwd->pw_name, suser) :
384 #endif
385 rcmd(&host, port, pwd->pw_name, suser, bp, 0);
386 (void)free(bp);
387 if (rem < 0) {
388 ++errs;
389 continue;
390 }
391 (void)seteuid(userid);
392 tos = IPTOS_THROUGHPUT;
393 if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
394 warn("TOS (ignored)");
395 sink(1, argv + argc - 1);
396 (void)seteuid(0);
397 (void)close(rem);
398 rem = -1;
399 }
400 }
401
402 void
403 source(argc, argv)
404 int argc;
405 char *argv[];
406 {
407 struct stat stb;
408 static BUF buffer;
409 BUF *bp;
410 off_t i;
411 int amt, fd, haderr, indx, result;
412 char *last, *name, buf[BUFSIZ];
413
414 for (indx = 0; indx < argc; ++indx) {
415 name = argv[indx];
416 if ((fd = open(name, O_RDONLY, 0)) < 0)
417 goto syserr;
418 if (fstat(fd, &stb)) {
419 syserr: run_err("%s: %s", name, strerror(errno));
420 goto next;
421 }
422 switch (stb.st_mode & S_IFMT) {
423 case S_IFREG:
424 break;
425 case S_IFDIR:
426 if (iamrecursive) {
427 rsource(name, &stb);
428 goto next;
429 }
430 /* FALLTHROUGH */
431 default:
432 run_err("%s: not a regular file", name);
433 goto next;
434 }
435 if ((last = strrchr(name, '/')) == NULL)
436 last = name;
437 else
438 ++last;
439 if (pflag) {
440 /*
441 * Make it compatible with possible future
442 * versions expecting microseconds.
443 */
444 (void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n",
445 stb.st_mtimespec.tv_sec, stb.st_atimespec.tv_sec);
446 (void)write(rem, buf, strlen(buf));
447 if (response() < 0)
448 goto next;
449 }
450 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
451 (void)snprintf(buf, sizeof(buf), "C%04o %qd %s\n",
452 stb.st_mode & MODEMASK, stb.st_size, last);
453 (void)write(rem, buf, strlen(buf));
454 if (response() < 0)
455 goto next;
456 if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) {
457 next: (void)close(fd);
458 continue;
459 }
460
461 /* Keep writing after an error so that we stay sync'd up. */
462 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
463 amt = bp->cnt;
464 if (i + amt > stb.st_size)
465 amt = stb.st_size - i;
466 if (!haderr) {
467 result = read(fd, bp->buf, amt);
468 if (result != amt)
469 haderr = result >= 0 ? EIO : errno;
470 }
471 if (haderr)
472 (void)write(rem, bp->buf, amt);
473 else {
474 result = write(rem, bp->buf, amt);
475 if (result != amt)
476 haderr = result >= 0 ? EIO : errno;
477 }
478 }
479 if (close(fd) && !haderr)
480 haderr = errno;
481 if (!haderr)
482 (void)write(rem, "", 1);
483 else
484 run_err("%s: %s", name, strerror(haderr));
485 (void)response();
486 }
487 }
488
489 void
490 rsource(name, statp)
491 char *name;
492 struct stat *statp;
493 {
494 DIR *dirp;
495 struct dirent *dp;
496 char *last, *vect[1], path[MAXPATHLEN];
497
498 if (!(dirp = opendir(name))) {
499 run_err("%s: %s", name, strerror(errno));
500 return;
501 }
502 last = strrchr(name, '/');
503 if (last == 0)
504 last = name;
505 else
506 last++;
507 if (pflag) {
508 (void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n",
509 statp->st_mtimespec.tv_sec, statp->st_atimespec.tv_sec);
510 (void)write(rem, path, strlen(path));
511 if (response() < 0) {
512 closedir(dirp);
513 return;
514 }
515 }
516 (void)snprintf(path, sizeof(path),
517 "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last);
518 (void)write(rem, path, strlen(path));
519 if (response() < 0) {
520 closedir(dirp);
521 return;
522 }
523 while (dp = readdir(dirp)) {
524 if (dp->d_ino == 0)
525 continue;
526 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
527 continue;
528 if (strlen(name) + 1 + strlen(dp->d_name) >= MAXPATHLEN - 1) {
529 run_err("%s/%s: name too long", name, dp->d_name);
530 continue;
531 }
532 (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name);
533 vect[0] = path;
534 source(1, vect);
535 }
536 (void)closedir(dirp);
537 (void)write(rem, "E\n", 2);
538 (void)response();
539 }
540
541 void
542 sink(argc, argv)
543 int argc;
544 char *argv[];
545 {
546 static BUF buffer;
547 struct stat stb;
548 struct timeval tv[2];
549 enum { YES, NO, DISPLAYED } wrerr;
550 BUF *bp;
551 off_t i, j;
552 int amt, count, exists, first, mask, mode, ofd, omode;
553 int setimes, size, targisdir, wrerrno;
554 char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ];
555
556 #define atime tv[0]
557 #define mtime tv[1]
558 #define SCREWUP(str) { why = str; goto screwup; }
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(*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(*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 (void)close(ofd);
756 (void)response();
757 if (setimes && wrerr == NO) {
758 setimes = 0;
759 if (utimes(np, tv) < 0) {
760 run_err("%s: set times: %s",
761 np, strerror(errno));
762 wrerr = DISPLAYED;
763 }
764 }
765 switch(wrerr) {
766 case YES:
767 run_err("%s: %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 }
780
781 #ifdef KERBEROS
782 int
783 kerberos(host, bp, locuser, user)
784 char **host, *bp, *locuser, *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(host, port, locuser, user, bp, 0);
820 }
821 return (rem);
822 }
823 #endif /* KERBEROS */
824
825 int
826 response()
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()
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 }
877
878 #if __STDC__
879 #include <stdarg.h>
880 #else
881 #include <varargs.h>
882 #endif
883
884 #ifdef KERBEROS
885 void
886 #if __STDC__
887 oldw(const char *fmt, ...)
888 #else
889 oldw(fmt, va_alist)
890 char *fmt;
891 va_dcl
892 #endif
893 {
894 va_list ap;
895 #if __STDC__
896 va_start(ap, fmt);
897 #else
898 va_start(ap);
899 #endif
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 #if __STDC__
909 run_err(const char *fmt, ...)
910 #else
911 run_err(fmt, va_alist)
912 char *fmt;
913 va_dcl
914 #endif
915 {
916 static FILE *fp;
917 va_list ap;
918 #if __STDC__
919 va_start(ap, fmt);
920 #else
921 va_start(ap);
922 #endif
923
924 ++errs;
925 if (fp == NULL && !(fp = fdopen(rem, "w")))
926 return;
927 (void)fprintf(fp, "%c", 0x01);
928 (void)fprintf(fp, "rcp: ");
929 (void)vfprintf(fp, fmt, ap);
930 (void)fprintf(fp, "\n");
931 (void)fflush(fp);
932
933 if (!iamremote)
934 vwarnx(fmt, ap);
935
936 va_end(ap);
937 }