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