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