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