xref: /netbsd-src/usr.bin/rdist/docmd.c (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
1 /*
2  * Copyright (c) 1983, 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 
34 #ifndef lint
35 static char sccsid[] = "@(#)docmd.c	8.1 (Berkeley) 6/9/93";
36 #endif /* not lint */
37 
38 #include "defs.h"
39 #include <setjmp.h>
40 #include <netdb.h>
41 
42 FILE	*lfp;			/* log file for recording files updated */
43 struct	subcmd *subcmds;	/* list of sub-commands for current cmd */
44 jmp_buf	env;
45 
46 static int	 makeconn __P((char *));
47 static int	 okname __P((char *));
48 static void	 closeconn __P((void));
49 static void	 cmptime __P((char *));
50 static void	 doarrow __P((char **,
51 		    struct namelist *, char *, struct subcmd *));
52 static void	 dodcolon __P((char **,
53 		    struct namelist *, char *, struct subcmd *));
54 static void	 notify __P((char *, char *, struct namelist *, time_t));
55 static void	 rcmptime __P((struct stat *));
56 
57 /*
58  * Do the commands in cmds (initialized by yyparse).
59  */
60 void
61 docmds(dhosts, argc, argv)
62 	char **dhosts;
63 	int argc;
64 	char **argv;
65 {
66 	register struct cmd *c;
67 	register struct namelist *f;
68 	register char **cpp;
69 	extern struct cmd *cmds;
70 
71 	signal(SIGHUP, cleanup);
72 	signal(SIGINT, cleanup);
73 	signal(SIGQUIT, cleanup);
74 	signal(SIGTERM, cleanup);
75 
76 	for (c = cmds; c != NULL; c = c->c_next) {
77 		if (dhosts != NULL && *dhosts != NULL) {
78 			for (cpp = dhosts; *cpp; cpp++)
79 				if (strcmp(c->c_name, *cpp) == 0)
80 					goto fndhost;
81 			continue;
82 		}
83 	fndhost:
84 		if (argc) {
85 			for (cpp = argv; *cpp; cpp++) {
86 				if (c->c_label != NULL &&
87 				    strcmp(c->c_label, *cpp) == 0) {
88 					cpp = NULL;
89 					goto found;
90 				}
91 				for (f = c->c_files; f != NULL; f = f->n_next)
92 					if (strcmp(f->n_name, *cpp) == 0)
93 						goto found;
94 			}
95 			continue;
96 		} else
97 			cpp = NULL;
98 	found:
99 		switch (c->c_type) {
100 		case ARROW:
101 			doarrow(cpp, c->c_files, c->c_name, c->c_cmds);
102 			break;
103 		case DCOLON:
104 			dodcolon(cpp, c->c_files, c->c_name, c->c_cmds);
105 			break;
106 		default:
107 			fatal("illegal command type %d\n", c->c_type);
108 		}
109 	}
110 	closeconn();
111 }
112 
113 /*
114  * Process commands for sending files to other machines.
115  */
116 static void
117 doarrow(filev, files, rhost, cmds)
118 	char **filev;
119 	struct namelist *files;
120 	char *rhost;
121 	struct subcmd *cmds;
122 {
123 	register struct namelist *f;
124 	register struct subcmd *sc;
125 	register char **cpp;
126 	int n, ddir, opts = options;
127 
128 	if (debug)
129 		printf("doarrow(%x, %s, %x)\n", files, rhost, cmds);
130 
131 	if (files == NULL) {
132 		error("no files to be updated\n");
133 		return;
134 	}
135 
136 	subcmds = cmds;
137 	ddir = files->n_next != NULL;	/* destination is a directory */
138 	if (nflag)
139 		printf("updating host %s\n", rhost);
140 	else {
141 		if (setjmp(env))
142 			goto done;
143 		signal(SIGPIPE, lostconn);
144 		if (!makeconn(rhost))
145 			return;
146 		if ((lfp = fopen(tempfile, "w")) == NULL) {
147 			fatal("cannot open %s\n", tempfile);
148 			exit(1);
149 		}
150 	}
151 	for (f = files; f != NULL; f = f->n_next) {
152 		if (filev) {
153 			for (cpp = filev; *cpp; cpp++)
154 				if (strcmp(f->n_name, *cpp) == 0)
155 					goto found;
156 			if (!nflag)
157 				(void) fclose(lfp);
158 			continue;
159 		}
160 	found:
161 		n = 0;
162 		for (sc = cmds; sc != NULL; sc = sc->sc_next) {
163 			if (sc->sc_type != INSTALL)
164 				continue;
165 			n++;
166 			install(f->n_name, sc->sc_name,
167 				sc->sc_name == NULL ? 0 : ddir, sc->sc_options);
168 			opts = sc->sc_options;
169 		}
170 		if (n == 0)
171 			install(f->n_name, NULL, 0, options);
172 	}
173 done:
174 	if (!nflag) {
175 		(void) signal(SIGPIPE, cleanup);
176 		(void) fclose(lfp);
177 		lfp = NULL;
178 	}
179 	for (sc = cmds; sc != NULL; sc = sc->sc_next)
180 		if (sc->sc_type == NOTIFY)
181 			notify(tempfile, rhost, sc->sc_args, 0);
182 	if (!nflag) {
183 		(void) unlink(tempfile);
184 		for (; ihead != NULL; ihead = ihead->nextp) {
185 			free(ihead);
186 			if ((opts & IGNLNKS) || ihead->count == 0)
187 				continue;
188 			log(lfp, "%s: Warning: missing links\n",
189 				ihead->pathname);
190 		}
191 	}
192 }
193 
194 /*
195  * Create a connection to the rdist server on the machine rhost.
196  */
197 static int
198 makeconn(rhost)
199 	char *rhost;
200 {
201 	register char *ruser, *cp;
202 	static char *cur_host = NULL;
203 	static int port = -1;
204 	char tuser[20];
205 	int n;
206 	extern char user[];
207 	extern int userid;
208 
209 	if (debug)
210 		printf("makeconn(%s)\n", rhost);
211 
212 	if (cur_host != NULL && rem >= 0) {
213 		if (strcmp(cur_host, rhost) == 0)
214 			return(1);
215 		closeconn();
216 	}
217 	cur_host = rhost;
218 	cp = index(rhost, '@');
219 	if (cp != NULL) {
220 		char c = *cp;
221 
222 		*cp = '\0';
223 		strncpy(tuser, rhost, sizeof(tuser)-1);
224 		*cp = c;
225 		rhost = cp + 1;
226 		ruser = tuser;
227 		if (*ruser == '\0')
228 			ruser = user;
229 		else if (!okname(ruser))
230 			return(0);
231 	} else
232 		ruser = user;
233 	if (!qflag)
234 		printf("updating host %s\n", rhost);
235 	(void) sprintf(buf, "%s -Server%s", _PATH_RDIST, qflag ? " -q" : "");
236 	if (port < 0) {
237 		struct servent *sp;
238 
239 		if ((sp = getservbyname("shell", "tcp")) == NULL)
240 			fatal("shell/tcp: unknown service");
241 		port = sp->s_port;
242 	}
243 
244 	if (debug) {
245 		printf("port = %d, luser = %s, ruser = %s\n", ntohs(port), user, ruser);
246 		printf("buf = %s\n", buf);
247 	}
248 
249 	fflush(stdout);
250 	seteuid(0);
251 	rem = rcmd(&rhost, port, user, ruser, buf, 0);
252 	seteuid(userid);
253 	if (rem < 0)
254 		return(0);
255 	cp = buf;
256 	if (read(rem, cp, 1) != 1)
257 		lostconn(0);
258 	if (*cp == 'V') {
259 		do {
260 			if (read(rem, cp, 1) != 1)
261 				lostconn(0);
262 		} while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
263 		*--cp = '\0';
264 		cp = buf;
265 		n = 0;
266 		while (*cp >= '0' && *cp <= '9')
267 			n = (n * 10) + (*cp++ - '0');
268 		if (*cp == '\0' && n == VERSION)
269 			return(1);
270 		error("connection failed: version numbers don't match (local %d, remote %d)\n", VERSION, n);
271 	} else {
272 		error("connection failed: version numbers don't match\n");
273 		error("got unexpected input:");
274 		do {
275 			error("%c", *cp);
276 		} while (*cp != '\n' && read(rem, cp, 1) == 1);
277 	}
278 	closeconn();
279 	return(0);
280 }
281 
282 /*
283  * Signal end of previous connection.
284  */
285 static void
286 closeconn()
287 {
288 	if (debug)
289 		printf("closeconn()\n");
290 
291 	if (rem >= 0) {
292 		(void) write(rem, "\2\n", 2);
293 		(void) close(rem);
294 		rem = -1;
295 	}
296 }
297 
298 void
299 lostconn(signo)
300 	int signo;
301 {
302 	if (iamremote)
303 		cleanup(0);
304 	log(lfp, "rdist: lost connection\n");
305 	longjmp(env, 1);
306 }
307 
308 static int
309 okname(name)
310 	register char *name;
311 {
312 	register char *cp = name;
313 	register int c;
314 
315 	do {
316 		c = *cp;
317 		if (c & 0200)
318 			goto bad;
319 		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
320 			goto bad;
321 		cp++;
322 	} while (*cp);
323 	return(1);
324 bad:
325 	error("invalid user name %s\n", name);
326 	return(0);
327 }
328 
329 time_t	lastmod;
330 FILE	*tfp;
331 extern	char target[], *tp;
332 
333 /*
334  * Process commands for comparing files to time stamp files.
335  */
336 static void
337 dodcolon(filev, files, stamp, cmds)
338 	char **filev;
339 	struct namelist *files;
340 	char *stamp;
341 	struct subcmd *cmds;
342 {
343 	register struct subcmd *sc;
344 	register struct namelist *f;
345 	register char **cpp;
346 	struct timeval tv[2];
347 	struct timezone tz;
348 	struct stat stb;
349 
350 	if (debug)
351 		printf("dodcolon()\n");
352 
353 	if (files == NULL) {
354 		error("no files to be updated\n");
355 		return;
356 	}
357 	if (stat(stamp, &stb) < 0) {
358 		error("%s: %s\n", stamp, strerror(errno));
359 		return;
360 	}
361 	if (debug)
362 		printf("%s: %ld\n", stamp, stb.st_mtime);
363 
364 	subcmds = cmds;
365 	lastmod = stb.st_mtime;
366 	if (nflag || (options & VERIFY))
367 		tfp = NULL;
368 	else {
369 		if ((tfp = fopen(tempfile, "w")) == NULL) {
370 			error("%s: %s\n", stamp, strerror(errno));
371 			return;
372 		}
373 		(void) gettimeofday(&tv[0], &tz);
374 		tv[1] = tv[0];
375 		(void) utimes(stamp, tv);
376 	}
377 
378 	for (f = files; f != NULL; f = f->n_next) {
379 		if (filev) {
380 			for (cpp = filev; *cpp; cpp++)
381 				if (strcmp(f->n_name, *cpp) == 0)
382 					goto found;
383 			continue;
384 		}
385 	found:
386 		tp = NULL;
387 		cmptime(f->n_name);
388 	}
389 
390 	if (tfp != NULL)
391 		(void) fclose(tfp);
392 	for (sc = cmds; sc != NULL; sc = sc->sc_next)
393 		if (sc->sc_type == NOTIFY)
394 			notify(tempfile, NULL, sc->sc_args, lastmod);
395 	if (!nflag && !(options & VERIFY))
396 		(void) unlink(tempfile);
397 }
398 
399 /*
400  * Compare the mtime of file to the list of time stamps.
401  */
402 static void
403 cmptime(name)
404 	char *name;
405 {
406 	struct stat stb;
407 
408 	if (debug)
409 		printf("cmptime(%s)\n", name);
410 
411 	if (except(name))
412 		return;
413 
414 	if (nflag) {
415 		printf("comparing dates: %s\n", name);
416 		return;
417 	}
418 
419 	/*
420 	 * first time cmptime() is called?
421 	 */
422 	if (tp == NULL) {
423 		if (exptilde(target, name) == NULL)
424 			return;
425 		tp = name = target;
426 		while (*tp)
427 			tp++;
428 	}
429 	if (access(name, 4) < 0 || stat(name, &stb) < 0) {
430 		error("%s: %s\n", name, strerror(errno));
431 		return;
432 	}
433 
434 	switch (stb.st_mode & S_IFMT) {
435 	case S_IFREG:
436 		break;
437 
438 	case S_IFDIR:
439 		rcmptime(&stb);
440 		return;
441 
442 	default:
443 		error("%s: not a plain file\n", name);
444 		return;
445 	}
446 
447 	if (stb.st_mtime > lastmod)
448 		log(tfp, "new: %s\n", name);
449 }
450 
451 static void
452 rcmptime(st)
453 	struct stat *st;
454 {
455 	register DIR *d;
456 	register struct direct *dp;
457 	register char *cp;
458 	char *otp;
459 	int len;
460 
461 	if (debug)
462 		printf("rcmptime(%x)\n", st);
463 
464 	if ((d = opendir(target)) == NULL) {
465 		error("%s: %s\n", target, strerror(errno));
466 		return;
467 	}
468 	otp = tp;
469 	len = tp - target;
470 	while (dp = readdir(d)) {
471 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
472 			continue;
473 		if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
474 			error("%s/%s: Name too long\n", target, dp->d_name);
475 			continue;
476 		}
477 		tp = otp;
478 		*tp++ = '/';
479 		cp = dp->d_name;
480 		while (*tp++ = *cp++)
481 			;
482 		tp--;
483 		cmptime(target);
484 	}
485 	closedir(d);
486 	tp = otp;
487 	*tp = '\0';
488 }
489 
490 /*
491  * Notify the list of people the changes that were made.
492  * rhost == NULL if we are mailing a list of changes compared to at time
493  * stamp file.
494  */
495 static void
496 notify(file, rhost, to, lmod)
497 	char *file, *rhost;
498 	register struct namelist *to;
499 	time_t lmod;
500 {
501 	register int fd, len;
502 	struct stat stb;
503 	FILE *pf;
504 
505 	if ((options & VERIFY) || to == NULL)
506 		return;
507 	if (!qflag) {
508 		printf("notify ");
509 		if (rhost)
510 			printf("@%s ", rhost);
511 		prnames(to);
512 	}
513 	if (nflag)
514 		return;
515 
516 	if ((fd = open(file, 0)) < 0) {
517 		error("%s: %s\n", file, strerror(errno));
518 		return;
519 	}
520 	if (fstat(fd, &stb) < 0) {
521 		error("%s: %s\n", file, strerror(errno));
522 		(void) close(fd);
523 		return;
524 	}
525 	if (stb.st_size == 0) {
526 		(void) close(fd);
527 		return;
528 	}
529 	/*
530 	 * Create a pipe to mailling program.
531 	 */
532 	(void)sprintf(buf, "%s -oi -t", _PATH_SENDMAIL);
533 	pf = popen(buf, "w");
534 	if (pf == NULL) {
535 		error("notify: \"%s\" failed\n", _PATH_SENDMAIL);
536 		(void) close(fd);
537 		return;
538 	}
539 	/*
540 	 * Output the proper header information.
541 	 */
542 	fprintf(pf, "From: rdist (Remote distribution program)\n");
543 	fprintf(pf, "To:");
544 	if (!any('@', to->n_name) && rhost != NULL)
545 		fprintf(pf, " %s@%s", to->n_name, rhost);
546 	else
547 		fprintf(pf, " %s", to->n_name);
548 	to = to->n_next;
549 	while (to != NULL) {
550 		if (!any('@', to->n_name) && rhost != NULL)
551 			fprintf(pf, ", %s@%s", to->n_name, rhost);
552 		else
553 			fprintf(pf, ", %s", to->n_name);
554 		to = to->n_next;
555 	}
556 	putc('\n', pf);
557 	if (rhost != NULL)
558 		fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
559 			host, rhost);
560 	else
561 		fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod));
562 	putc('\n', pf);
563 
564 	while ((len = read(fd, buf, BUFSIZ)) > 0)
565 		(void) fwrite(buf, 1, len, pf);
566 	(void) close(fd);
567 	(void) pclose(pf);
568 }
569 
570 /*
571  * Return true if name is in the list.
572  */
573 int
574 inlist(list, file)
575 	struct namelist *list;
576 	char *file;
577 {
578 	register struct namelist *nl;
579 
580 	for (nl = list; nl != NULL; nl = nl->n_next)
581 		if (!strcmp(file, nl->n_name))
582 			return(1);
583 	return(0);
584 }
585 
586 /*
587  * Return TRUE if file is in the exception list.
588  */
589 int
590 except(file)
591 	char *file;
592 {
593 	register struct	subcmd *sc;
594 	register struct	namelist *nl;
595 
596 	if (debug)
597 		printf("except(%s)\n", file);
598 
599 	for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
600 		if (sc->sc_type != EXCEPT && sc->sc_type != PATTERN)
601 			continue;
602 		for (nl = sc->sc_args; nl != NULL; nl = nl->n_next) {
603 			if (sc->sc_type == EXCEPT) {
604 				if (!strcmp(file, nl->n_name))
605 					return(1);
606 				continue;
607 			}
608 			re_comp(nl->n_name);
609 			if (re_exec(file) > 0)
610 				return(1);
611 		}
612 	}
613 	return(0);
614 }
615 
616 char *
617 colon(cp)
618 	register char *cp;
619 {
620 
621 	while (*cp) {
622 		if (*cp == ':')
623 			return(cp);
624 		if (*cp == '/')
625 			return(0);
626 		cp++;
627 	}
628 	return(0);
629 }
630