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