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