xref: /titanic_44/usr/src/cmd/cmd-inet/usr.bin/rdist/docmd.c (revision 740638c8fa5a1c03618a9c75c3161dba57259a17)
17c478bd9Sstevel@tonic-gate /*
2*740638c8Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
117c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
127c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
137c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
147c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
157c478bd9Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
167c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
177c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
187c478bd9Sstevel@tonic-gate  */
197c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate #include "defs.h"
227c478bd9Sstevel@tonic-gate #include <string.h>
237c478bd9Sstevel@tonic-gate #include <setjmp.h>
247c478bd9Sstevel@tonic-gate #include <netdb.h>
257c478bd9Sstevel@tonic-gate #include <signal.h>
267c478bd9Sstevel@tonic-gate #include <krb5defs.h>
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #ifndef RDIST
297c478bd9Sstevel@tonic-gate #ifdef SYSV
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Historically, the rdist program has had the following hard-coded
327c478bd9Sstevel@tonic-gate  * pathname.  Some operating systems attempt to "improve" the
337c478bd9Sstevel@tonic-gate  * directory layout, in the process re-locating the rdist binary
347c478bd9Sstevel@tonic-gate  * to some other location.  However, the first original implementation
357c478bd9Sstevel@tonic-gate  * sets a standard of sorts.  In order to interoperate with other
367c478bd9Sstevel@tonic-gate  * systems, our implementation must do two things: It must provide
377c478bd9Sstevel@tonic-gate  * the an rdist binary at the pathname below, and it must use this
387c478bd9Sstevel@tonic-gate  * pathname when executing rdist on remote systems via the rcmd()
397c478bd9Sstevel@tonic-gate  * library.  Thus the hard-coded path name below can never be changed.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate #endif /* SYSV */
427c478bd9Sstevel@tonic-gate #define	RDIST "/usr/ucb/rdist"
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate FILE	*lfp;			/* log file for recording files updated */
467c478bd9Sstevel@tonic-gate struct	subcmd *subcmds;	/* list of sub-commands for current cmd */
477c478bd9Sstevel@tonic-gate jmp_buf	env;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate void	cleanup();
507c478bd9Sstevel@tonic-gate void	lostconn();
517c478bd9Sstevel@tonic-gate static int	init_service(int);
527c478bd9Sstevel@tonic-gate static struct servent *sp;
537c478bd9Sstevel@tonic-gate 
54*740638c8Sbw static void notify(char *file, char *rhost, struct namelist *to, time_t lmod);
55*740638c8Sbw static void rcmptime(struct stat *st);
56*740638c8Sbw static void cmptime(char *name);
57*740638c8Sbw static void dodcolon(char **filev, struct namelist *files, char *stamp,
58*740638c8Sbw     struct subcmd *cmds);
59*740638c8Sbw static void closeconn(void);
60*740638c8Sbw static void doarrow(char **filev, struct namelist *files, char *rhost,
61*740638c8Sbw     struct subcmd *cmds);
62*740638c8Sbw static int makeconn(char *rhost);
63*740638c8Sbw static int okname(register char *name);
64*740638c8Sbw 
657c478bd9Sstevel@tonic-gate #ifdef SYSV
667c478bd9Sstevel@tonic-gate #include <libgen.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static char *recomp;
697c478bd9Sstevel@tonic-gate static char *errstring = "regcmp failed for some unknown reason";
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate char *
re_comp(s)727c478bd9Sstevel@tonic-gate re_comp(s)
737c478bd9Sstevel@tonic-gate char *s;
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	if ((int)recomp != 0)
767c478bd9Sstevel@tonic-gate 		free(recomp);
777c478bd9Sstevel@tonic-gate 	recomp = regcmp(s, (char *)0);
787c478bd9Sstevel@tonic-gate 	if (recomp == NULL)
797c478bd9Sstevel@tonic-gate 		return (errstring);
807c478bd9Sstevel@tonic-gate 	else
817c478bd9Sstevel@tonic-gate 		return ((char *)0);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 
85*740638c8Sbw static int
re_exec(s)867c478bd9Sstevel@tonic-gate re_exec(s)
877c478bd9Sstevel@tonic-gate char *s;
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	if ((int)recomp == 0)
907c478bd9Sstevel@tonic-gate 		return (-1);
917c478bd9Sstevel@tonic-gate 	if (regex(recomp, s) == NULL)
927c478bd9Sstevel@tonic-gate 		return (0);
937c478bd9Sstevel@tonic-gate 	else
947c478bd9Sstevel@tonic-gate 		return (1);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate #endif /* SYSV */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * Do the commands in cmds (initialized by yyparse).
1007c478bd9Sstevel@tonic-gate  */
101*740638c8Sbw void
docmds(dhosts,argc,argv)1027c478bd9Sstevel@tonic-gate docmds(dhosts, argc, argv)
1037c478bd9Sstevel@tonic-gate 	char **dhosts;
1047c478bd9Sstevel@tonic-gate 	int argc;
1057c478bd9Sstevel@tonic-gate 	char **argv;
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	register struct cmd *c;
1087c478bd9Sstevel@tonic-gate 	register struct namelist *f;
1097c478bd9Sstevel@tonic-gate 	register char **cpp;
1107c478bd9Sstevel@tonic-gate 	extern struct cmd *cmds;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/* protect backgrounded rdist */
1137c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1147c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, cleanup);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/* ... and running via nohup(1) */
1177c478bd9Sstevel@tonic-gate 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1187c478bd9Sstevel@tonic-gate 		(void) signal(SIGHUP, cleanup);
1197c478bd9Sstevel@tonic-gate 	if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1207c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, cleanup);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	(void) signal(SIGTERM, cleanup);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate if (debug)
1257c478bd9Sstevel@tonic-gate 	if (!cmds)
1267c478bd9Sstevel@tonic-gate 		printf("docmds:  cmds == NULL\n");
1277c478bd9Sstevel@tonic-gate 	else {
1287c478bd9Sstevel@tonic-gate 		printf("docmds:  cmds ");
1297c478bd9Sstevel@tonic-gate 		prcmd(cmds);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	for (c = cmds; c != NULL; c = c->c_next) {
1327c478bd9Sstevel@tonic-gate 		if (dhosts != NULL && *dhosts != NULL) {
1337c478bd9Sstevel@tonic-gate 			for (cpp = dhosts; *cpp; cpp++)
1347c478bd9Sstevel@tonic-gate 				if (strcmp(c->c_name, *cpp) == 0)
1357c478bd9Sstevel@tonic-gate 					goto fndhost;
1367c478bd9Sstevel@tonic-gate 			continue;
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 	fndhost:
1397c478bd9Sstevel@tonic-gate 		if (argc) {
1407c478bd9Sstevel@tonic-gate 			for (cpp = argv; *cpp; cpp++) {
1417c478bd9Sstevel@tonic-gate 				if (c->c_label != NULL &&
1427c478bd9Sstevel@tonic-gate 				    strcmp(c->c_label, *cpp) == 0) {
1437c478bd9Sstevel@tonic-gate 					cpp = NULL;
1447c478bd9Sstevel@tonic-gate 					goto found;
1457c478bd9Sstevel@tonic-gate 				}
1467c478bd9Sstevel@tonic-gate 				for (f = c->c_files; f != NULL; f = f->n_next)
1477c478bd9Sstevel@tonic-gate 					if (strcmp(f->n_name, *cpp) == 0)
1487c478bd9Sstevel@tonic-gate 						goto found;
1497c478bd9Sstevel@tonic-gate 			}
1507c478bd9Sstevel@tonic-gate 			continue;
1517c478bd9Sstevel@tonic-gate 		} else
1527c478bd9Sstevel@tonic-gate 			cpp = NULL;
1537c478bd9Sstevel@tonic-gate 	found:
1547c478bd9Sstevel@tonic-gate 		switch (c->c_type) {
1557c478bd9Sstevel@tonic-gate 		case ARROW:
1567c478bd9Sstevel@tonic-gate 			doarrow(cpp, c->c_files, c->c_name, c->c_cmds);
1577c478bd9Sstevel@tonic-gate 			break;
1587c478bd9Sstevel@tonic-gate 		case DCOLON:
1597c478bd9Sstevel@tonic-gate 			dodcolon(cpp, c->c_files, c->c_name, c->c_cmds);
1607c478bd9Sstevel@tonic-gate 			break;
1617c478bd9Sstevel@tonic-gate 		default:
1627c478bd9Sstevel@tonic-gate 			fatal("illegal command type %d\n", c->c_type);
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	closeconn();
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Process commands for sending files to other machines.
1707c478bd9Sstevel@tonic-gate  */
171*740638c8Sbw static void
doarrow(filev,files,rhost,cmds)1727c478bd9Sstevel@tonic-gate doarrow(filev, files, rhost, cmds)
1737c478bd9Sstevel@tonic-gate 	char **filev;
1747c478bd9Sstevel@tonic-gate 	struct namelist *files;
1757c478bd9Sstevel@tonic-gate 	char *rhost;
1767c478bd9Sstevel@tonic-gate 	struct subcmd *cmds;
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	register struct namelist *f;
1797c478bd9Sstevel@tonic-gate 	register struct subcmd *sc;
1807c478bd9Sstevel@tonic-gate 	register char **cpp;
1817c478bd9Sstevel@tonic-gate 	int n, ddir, opts = options;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (debug)
1847c478bd9Sstevel@tonic-gate 		printf("doarrow(%x, %s, %x)\n", files, rhost, cmds);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (files == NULL) {
1877c478bd9Sstevel@tonic-gate 		error("no files to be updated\n");
1887c478bd9Sstevel@tonic-gate 		return;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	subcmds = cmds;
1927c478bd9Sstevel@tonic-gate 	ddir = files->n_next != NULL;	/* destination is a directory */
1937c478bd9Sstevel@tonic-gate 	if (nflag)
1947c478bd9Sstevel@tonic-gate 		printf("updating host %s\n", rhost);
1957c478bd9Sstevel@tonic-gate 	else {
1967c478bd9Sstevel@tonic-gate 		if (setjmp(env))
1977c478bd9Sstevel@tonic-gate 			goto done;
1987c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, lostconn);
1997c478bd9Sstevel@tonic-gate 		if (!makeconn(rhost))
2007c478bd9Sstevel@tonic-gate 			return;
2017c478bd9Sstevel@tonic-gate 		if (!nflag)
2027c478bd9Sstevel@tonic-gate 			if ((lfp = fopen(Tmpfile, "w")) == NULL) {
2037c478bd9Sstevel@tonic-gate 				fatal("cannot open %s\n", Tmpfile);
2047c478bd9Sstevel@tonic-gate 				exit(1);
2057c478bd9Sstevel@tonic-gate 			}
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	for (f = files; f != NULL; f = f->n_next) {
2087c478bd9Sstevel@tonic-gate 		if (filev) {
2097c478bd9Sstevel@tonic-gate 			for (cpp = filev; *cpp; cpp++)
2107c478bd9Sstevel@tonic-gate 				if (strcmp(f->n_name, *cpp) == 0)
2117c478bd9Sstevel@tonic-gate 					goto found;
2127c478bd9Sstevel@tonic-gate 			continue;
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 	found:
2157c478bd9Sstevel@tonic-gate 		n = 0;
2167c478bd9Sstevel@tonic-gate 		for (sc = cmds; sc != NULL; sc = sc->sc_next) {
2177c478bd9Sstevel@tonic-gate 			if (sc->sc_type != INSTALL)
2187c478bd9Sstevel@tonic-gate 				continue;
2197c478bd9Sstevel@tonic-gate 			n++;
2207c478bd9Sstevel@tonic-gate 			install(f->n_name, sc->sc_name,
2217c478bd9Sstevel@tonic-gate 				sc->sc_name == NULL ? 0 : ddir, sc->sc_options);
2227c478bd9Sstevel@tonic-gate 			opts = sc->sc_options;
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 		if (n == 0)
2257c478bd9Sstevel@tonic-gate 			install(f->n_name, NULL, 0, options);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate done:
2287c478bd9Sstevel@tonic-gate 	if (!nflag) {
2297c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, cleanup);
2307c478bd9Sstevel@tonic-gate 		(void) fclose(lfp);
2317c478bd9Sstevel@tonic-gate 		lfp = NULL;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	for (sc = cmds; sc != NULL; sc = sc->sc_next)
2347c478bd9Sstevel@tonic-gate 		if (sc->sc_type == NOTIFY)
2357c478bd9Sstevel@tonic-gate 			notify(Tmpfile, rhost, sc->sc_args, 0);
2367c478bd9Sstevel@tonic-gate 	if (!nflag) {
2377c478bd9Sstevel@tonic-gate 		(void) unlink(Tmpfile);
2387c478bd9Sstevel@tonic-gate 		for (; ihead != NULL; ihead = ihead->nextp) {
2397c478bd9Sstevel@tonic-gate 			free(ihead);
2407c478bd9Sstevel@tonic-gate 			if ((opts & IGNLNKS) || ihead->count == 0)
2417c478bd9Sstevel@tonic-gate 				continue;
2427c478bd9Sstevel@tonic-gate 			log(lfp, "%s: Warning: missing links\n",
2437c478bd9Sstevel@tonic-gate 				ihead->pathname);
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate static int
init_service(int krb5flag)2497c478bd9Sstevel@tonic-gate init_service(int krb5flag)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	boolean_t success = B_FALSE;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (krb5flag > 0) {
2547c478bd9Sstevel@tonic-gate 		if ((sp = getservbyname("kshell", "tcp")) == NULL) {
2557c478bd9Sstevel@tonic-gate 			fatal("kshell/tcp: unknown service");
2567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2577c478bd9Sstevel@tonic-gate 				gettext("trying shell/tcp service...\n"));
2587c478bd9Sstevel@tonic-gate 		} else {
2597c478bd9Sstevel@tonic-gate 			success = B_TRUE;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 	} else {
2627c478bd9Sstevel@tonic-gate 		if ((sp = getservbyname("shell", "tcp")) == NULL) {
2637c478bd9Sstevel@tonic-gate 			fatal("shell/tcp: unknown service");
2647c478bd9Sstevel@tonic-gate 			exit(1);
2657c478bd9Sstevel@tonic-gate 		} else {
2667c478bd9Sstevel@tonic-gate 			success = B_TRUE;
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 	return (success);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate  * Create a connection to the rdist server on the machine rhost.
2737c478bd9Sstevel@tonic-gate  */
274*740638c8Sbw static int
makeconn(rhost)2757c478bd9Sstevel@tonic-gate makeconn(rhost)
2767c478bd9Sstevel@tonic-gate 	char *rhost;
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	register char *ruser, *cp;
2797c478bd9Sstevel@tonic-gate 	static char *cur_host = NULL;
2807c478bd9Sstevel@tonic-gate 	static int port = -1;
2817c478bd9Sstevel@tonic-gate 	char tuser[20];
2827c478bd9Sstevel@tonic-gate 	int n;
2837c478bd9Sstevel@tonic-gate 	extern char user[];
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (debug)
2867c478bd9Sstevel@tonic-gate 		printf("makeconn(%s)\n", rhost);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (cur_host != NULL && rem >= 0) {
2897c478bd9Sstevel@tonic-gate 		if (strcmp(cur_host, rhost) == 0)
2907c478bd9Sstevel@tonic-gate 			return (1);
2917c478bd9Sstevel@tonic-gate 		closeconn();
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	cur_host = rhost;
2947c478bd9Sstevel@tonic-gate 	cp = index(rhost, '@');
2957c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
2967c478bd9Sstevel@tonic-gate 		char c = *cp;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		*cp = '\0';
2997c478bd9Sstevel@tonic-gate 		strncpy(tuser, rhost, sizeof (tuser)-1);
3007c478bd9Sstevel@tonic-gate 		*cp = c;
3017c478bd9Sstevel@tonic-gate 		rhost = cp + 1;
3027c478bd9Sstevel@tonic-gate 		ruser = tuser;
3037c478bd9Sstevel@tonic-gate 		if (*ruser == '\0')
3047c478bd9Sstevel@tonic-gate 			ruser = user;
3057c478bd9Sstevel@tonic-gate 		else if (!okname(ruser))
3067c478bd9Sstevel@tonic-gate 			return (0);
3077c478bd9Sstevel@tonic-gate 	} else
3087c478bd9Sstevel@tonic-gate 		ruser = user;
3097c478bd9Sstevel@tonic-gate 	if (!qflag)
3107c478bd9Sstevel@tonic-gate 		printf("updating host %s\n", rhost);
3117c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, RDIST_BUFSIZ, "%s%s -Server%s",
3127c478bd9Sstevel@tonic-gate 			encrypt_flag ? "-x " : "", RDIST, qflag ? " -q" : "");
3137c478bd9Sstevel@tonic-gate 	if (port < 0) {
3147c478bd9Sstevel@tonic-gate 		if (debug_port == 0) {
3157c478bd9Sstevel@tonic-gate 			if ((retval = (int)init_service(krb5auth_flag)) == 0) {
3167c478bd9Sstevel@tonic-gate 				krb5auth_flag = encrypt_flag = 0;
3177c478bd9Sstevel@tonic-gate 				(void) init_service(krb5auth_flag);
3187c478bd9Sstevel@tonic-gate 			}
3197c478bd9Sstevel@tonic-gate 			port = sp->s_port;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 		} else {
3227c478bd9Sstevel@tonic-gate 			port = debug_port;
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (debug) {
3277c478bd9Sstevel@tonic-gate 		printf("port = %d, luser = %s, ruser = %s\n", ntohs(port),
3287c478bd9Sstevel@tonic-gate 			user, ruser);
3297c478bd9Sstevel@tonic-gate 		printf("buf = %s\n", buf);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	fflush(stdout);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
3357c478bd9Sstevel@tonic-gate 		if ((encrypt_flag > 0) && (!krb5_privacy_allowed())) {
3367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rdist: Encryption "
3377c478bd9Sstevel@tonic-gate 					" not supported.\n"));
3387c478bd9Sstevel@tonic-gate 			exit(1);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		authopts = AP_OPTS_MUTUAL_REQUIRED;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		status = kcmd(&rem, &rhost, port,
3447c478bd9Sstevel@tonic-gate 				user, ruser,
3457c478bd9Sstevel@tonic-gate 				buf, 0, "host", krb_realm,
3467c478bd9Sstevel@tonic-gate 				bsd_context,
3477c478bd9Sstevel@tonic-gate 				&auth_context,
3487c478bd9Sstevel@tonic-gate 				&cred,
3497c478bd9Sstevel@tonic-gate 				0,	/* No need for sequence number */
3507c478bd9Sstevel@tonic-gate 				0,	/* No need for server seq # */
3517c478bd9Sstevel@tonic-gate 				authopts,
3527c478bd9Sstevel@tonic-gate 				1,	/* Always set anyport */
3537c478bd9Sstevel@tonic-gate 				&kcmd_proto);
3547c478bd9Sstevel@tonic-gate 		if (status) {
3557c478bd9Sstevel@tonic-gate 			/*
3567c478bd9Sstevel@tonic-gate 			 * If new protocol requested, we dont
3577c478bd9Sstevel@tonic-gate 			 * fallback to less secure ones.
3587c478bd9Sstevel@tonic-gate 			 */
3597c478bd9Sstevel@tonic-gate 			if (kcmd_proto == KCMD_NEW_PROTOCOL) {
3607c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rdist: kcmdv2 "
3617c478bd9Sstevel@tonic-gate 					"to host %s failed - %s\n"
3627c478bd9Sstevel@tonic-gate 					"Fallback to normal rdist denied."),
3637c478bd9Sstevel@tonic-gate 					host, error_message(status));
3647c478bd9Sstevel@tonic-gate 				exit(1);
3657c478bd9Sstevel@tonic-gate 			}
3667c478bd9Sstevel@tonic-gate 			/* check NO_TKT_FILE or equivalent... */
3677c478bd9Sstevel@tonic-gate 			if (status != -1) {
3687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rdist: "
3697c478bd9Sstevel@tonic-gate 				"kcmd to host %s failed - %s\n"
3707c478bd9Sstevel@tonic-gate 				"trying normal rdist...\n\n"),
3717c478bd9Sstevel@tonic-gate 				host, error_message(status));
3727c478bd9Sstevel@tonic-gate 			} else {
3737c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3747c478bd9Sstevel@tonic-gate 					gettext("trying normal rdist...\n"));
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 			/*
3777c478bd9Sstevel@tonic-gate 			 * kcmd() failed, so we now fallback to normal rdist
3787c478bd9Sstevel@tonic-gate 			 */
3797c478bd9Sstevel@tonic-gate 			krb5auth_flag = encrypt_flag = 0;
3807c478bd9Sstevel@tonic-gate 			(void) init_service(krb5auth_flag);
3817c478bd9Sstevel@tonic-gate 			port = sp->s_port;
3827c478bd9Sstevel@tonic-gate 			goto do_rcmd;
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate #ifdef DEBUG
3857c478bd9Sstevel@tonic-gate 		else {
3867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Kerberized rdist "
3877c478bd9Sstevel@tonic-gate 					"session, port %d in use "), port);
3887c478bd9Sstevel@tonic-gate 			if (kcmd_proto == KCMD_OLD_PROTOCOL)
3897c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3907c478bd9Sstevel@tonic-gate 						gettext("[kcmd ver.1].\n"));
3917c478bd9Sstevel@tonic-gate 			else
3927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3937c478bd9Sstevel@tonic-gate 						gettext("[kcmd ver.2].\n"));
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3967c478bd9Sstevel@tonic-gate 		session_key = &cred->keyblock;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		if (kcmd_proto == KCMD_NEW_PROTOCOL) {
3997c478bd9Sstevel@tonic-gate 			status = krb5_auth_con_getlocalsubkey(bsd_context,
4007c478bd9Sstevel@tonic-gate 							    auth_context,
4017c478bd9Sstevel@tonic-gate 							    &session_key);
4027c478bd9Sstevel@tonic-gate 			if (status) {
4037c478bd9Sstevel@tonic-gate 				com_err("rdist", status,
4047c478bd9Sstevel@tonic-gate 					"determining subkey for session");
4057c478bd9Sstevel@tonic-gate 				exit(1);
4067c478bd9Sstevel@tonic-gate 			}
4077c478bd9Sstevel@tonic-gate 			if (!session_key) {
4087c478bd9Sstevel@tonic-gate 				com_err("rdist", 0,
4097c478bd9Sstevel@tonic-gate 				"no subkey negotiated for connection");
4107c478bd9Sstevel@tonic-gate 				exit(1);
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		eblock.crypto_entry = session_key->enctype;
4157c478bd9Sstevel@tonic-gate 		eblock.key = (krb5_keyblock *)session_key;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		init_encrypt(encrypt_flag, bsd_context, kcmd_proto, &desinbuf,
4187c478bd9Sstevel@tonic-gate 				&desoutbuf, CLIENT, &eblock);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		if (encrypt_flag > 0) {
4227c478bd9Sstevel@tonic-gate 			char *s = gettext("This rdist session is using "
4237c478bd9Sstevel@tonic-gate 				"encryption for all data transmissions.\r\n");
4247c478bd9Sstevel@tonic-gate 			(void) write(2, s, strlen(s));
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 	else
4297c478bd9Sstevel@tonic-gate do_rcmd:
4307c478bd9Sstevel@tonic-gate 	{
4317c478bd9Sstevel@tonic-gate 		rem = rcmd_af(&rhost, port, user, ruser, buf, 0, AF_INET6);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if (rem < 0)
4357c478bd9Sstevel@tonic-gate 		return (0);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	cp = buf;
4387c478bd9Sstevel@tonic-gate 	if (desread(rem, cp, 1, 0) != 1)
4397c478bd9Sstevel@tonic-gate 		lostconn();
4407c478bd9Sstevel@tonic-gate 	if (*cp == 'V') {
4417c478bd9Sstevel@tonic-gate 		do {
4427c478bd9Sstevel@tonic-gate 			if (desread(rem, cp, 1, 0) != 1)
4437c478bd9Sstevel@tonic-gate 				lostconn();
4447c478bd9Sstevel@tonic-gate 		} while (*cp++ != '\n' && cp < &buf[RDIST_BUFSIZ]);
4457c478bd9Sstevel@tonic-gate 		*--cp = '\0';
4467c478bd9Sstevel@tonic-gate 		cp = buf;
4477c478bd9Sstevel@tonic-gate 		n = 0;
4487c478bd9Sstevel@tonic-gate 		while (*cp >= '0' && *cp <= '9')
4497c478bd9Sstevel@tonic-gate 			n = (n * 10) + (*cp++ - '0');
4507c478bd9Sstevel@tonic-gate 		if (*cp == '\0' && n == VERSION)
4517c478bd9Sstevel@tonic-gate 			return (1);
4527c478bd9Sstevel@tonic-gate 		error("connection failed: version numbers don't match"
4537c478bd9Sstevel@tonic-gate 		    " (local %d, remote %d)\n", VERSION, n);
4547c478bd9Sstevel@tonic-gate 	} else {
4557c478bd9Sstevel@tonic-gate 		error("connection failed: version numbers don't match\n");
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 	closeconn();
4587c478bd9Sstevel@tonic-gate 	return (0);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate /*
4627c478bd9Sstevel@tonic-gate  * Signal end of previous connection.
4637c478bd9Sstevel@tonic-gate  */
464*740638c8Sbw static void
closeconn(void)465*740638c8Sbw closeconn(void)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	if (debug)
4687c478bd9Sstevel@tonic-gate 		printf("closeconn()\n");
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (rem >= 0) {
4717c478bd9Sstevel@tonic-gate 		(void) deswrite(rem, "\2\n", 2, 0);
4727c478bd9Sstevel@tonic-gate 		(void) close(rem);
4737c478bd9Sstevel@tonic-gate 		rem = -1;
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate void
lostconn()4787c478bd9Sstevel@tonic-gate lostconn()
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	if (iamremote)
4817c478bd9Sstevel@tonic-gate 		cleanup();
4827c478bd9Sstevel@tonic-gate 	log(lfp, "rdist: lost connection\n");
4837c478bd9Sstevel@tonic-gate 	longjmp(env, 1);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
486*740638c8Sbw static int
okname(name)4877c478bd9Sstevel@tonic-gate okname(name)
4887c478bd9Sstevel@tonic-gate 	register char *name;
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	register char *cp = name;
4917c478bd9Sstevel@tonic-gate 	register int c;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	do {
4947c478bd9Sstevel@tonic-gate 		c = *cp;
4957c478bd9Sstevel@tonic-gate 		if (c & 0200)
4967c478bd9Sstevel@tonic-gate 			goto bad;
4977c478bd9Sstevel@tonic-gate 		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
4987c478bd9Sstevel@tonic-gate 			goto bad;
4997c478bd9Sstevel@tonic-gate 		cp++;
5007c478bd9Sstevel@tonic-gate 	} while (*cp);
5017c478bd9Sstevel@tonic-gate 	return (1);
5027c478bd9Sstevel@tonic-gate bad:
5037c478bd9Sstevel@tonic-gate 	error("invalid user name %s\n", name);
5047c478bd9Sstevel@tonic-gate 	return (0);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate time_t	lastmod;
5087c478bd9Sstevel@tonic-gate FILE	*tfp;
5097c478bd9Sstevel@tonic-gate extern	char target[], *tp;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate /*
5127c478bd9Sstevel@tonic-gate  * Process commands for comparing files to time stamp files.
5137c478bd9Sstevel@tonic-gate  */
514*740638c8Sbw static void
dodcolon(filev,files,stamp,cmds)5157c478bd9Sstevel@tonic-gate dodcolon(filev, files, stamp, cmds)
5167c478bd9Sstevel@tonic-gate 	char **filev;
5177c478bd9Sstevel@tonic-gate 	struct namelist *files;
5187c478bd9Sstevel@tonic-gate 	char *stamp;
5197c478bd9Sstevel@tonic-gate 	struct subcmd *cmds;
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	register struct subcmd *sc;
5227c478bd9Sstevel@tonic-gate 	register struct namelist *f;
5237c478bd9Sstevel@tonic-gate 	register char **cpp;
5247c478bd9Sstevel@tonic-gate 	struct timeval tv[2];
5257c478bd9Sstevel@tonic-gate 	struct stat stb;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if (debug)
5287c478bd9Sstevel@tonic-gate 		printf("dodcolon()\n");
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (files == NULL) {
5317c478bd9Sstevel@tonic-gate 		error("no files to be updated\n");
5327c478bd9Sstevel@tonic-gate 		return;
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 	if (stat(stamp, &stb) < 0) {
5357c478bd9Sstevel@tonic-gate 		error("%s: %s\n", stamp, strerror(errno));
5367c478bd9Sstevel@tonic-gate 		return;
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 	if (debug)
5397c478bd9Sstevel@tonic-gate 		printf("%s: %d\n", stamp, stb.st_mtime);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	subcmds = cmds;
5427c478bd9Sstevel@tonic-gate 	lastmod = stb.st_mtime;
5437c478bd9Sstevel@tonic-gate 	if (nflag || (options & VERIFY))
5447c478bd9Sstevel@tonic-gate 		tfp = NULL;
5457c478bd9Sstevel@tonic-gate 	else {
5467c478bd9Sstevel@tonic-gate 		if ((tfp = fopen(Tmpfile, "w")) == NULL) {
5477c478bd9Sstevel@tonic-gate 			error("%s: %s\n", stamp, strerror(errno));
5487c478bd9Sstevel@tonic-gate 			return;
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tv[0], (struct timezone *)NULL);
5517c478bd9Sstevel@tonic-gate 		tv[1] = tv[0];
5527c478bd9Sstevel@tonic-gate 		(void) utimes(stamp, tv);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	for (f = files; f != NULL; f = f->n_next) {
5567c478bd9Sstevel@tonic-gate 		if (filev) {
5577c478bd9Sstevel@tonic-gate 			for (cpp = filev; *cpp; cpp++)
5587c478bd9Sstevel@tonic-gate 				if (strcmp(f->n_name, *cpp) == 0)
5597c478bd9Sstevel@tonic-gate 					goto found;
5607c478bd9Sstevel@tonic-gate 			continue;
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate 	found:
5637c478bd9Sstevel@tonic-gate 		tp = NULL;
5647c478bd9Sstevel@tonic-gate 		cmptime(f->n_name);
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if (tfp != NULL)
5687c478bd9Sstevel@tonic-gate 		(void) fclose(tfp);
5697c478bd9Sstevel@tonic-gate 	for (sc = cmds; sc != NULL; sc = sc->sc_next)
5707c478bd9Sstevel@tonic-gate 		if (sc->sc_type == NOTIFY)
5717c478bd9Sstevel@tonic-gate 			notify(Tmpfile, NULL, sc->sc_args, lastmod);
5727c478bd9Sstevel@tonic-gate 	if (!nflag && !(options & VERIFY))
5737c478bd9Sstevel@tonic-gate 		(void) unlink(Tmpfile);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /*
5777c478bd9Sstevel@tonic-gate  * Compare the mtime of file to the list of time stamps.
5787c478bd9Sstevel@tonic-gate  */
579*740638c8Sbw static void
cmptime(name)5807c478bd9Sstevel@tonic-gate cmptime(name)
5817c478bd9Sstevel@tonic-gate 	char *name;
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate 	struct stat stb;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (debug)
5867c478bd9Sstevel@tonic-gate 		printf("cmptime(%s)\n", name);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	if (except(name))
5897c478bd9Sstevel@tonic-gate 		return;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	if (nflag) {
5927c478bd9Sstevel@tonic-gate 		printf("comparing dates: %s\n", name);
5937c478bd9Sstevel@tonic-gate 		return;
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	/*
5977c478bd9Sstevel@tonic-gate 	 * first time cmptime() is called?
5987c478bd9Sstevel@tonic-gate 	 */
5997c478bd9Sstevel@tonic-gate 	if (tp == NULL) {
6007c478bd9Sstevel@tonic-gate 		if (exptilde(target, RDIST_BUFSIZ, name) == NULL)
6017c478bd9Sstevel@tonic-gate 			return;
6027c478bd9Sstevel@tonic-gate 		tp = name = target;
6037c478bd9Sstevel@tonic-gate 		while (*tp)
6047c478bd9Sstevel@tonic-gate 			tp++;
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 	if (access(name, 4) < 0 || stat(name, &stb) < 0) {
6077c478bd9Sstevel@tonic-gate 		error("%s: %s\n", name, strerror(errno));
6087c478bd9Sstevel@tonic-gate 		return;
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	switch (stb.st_mode & S_IFMT) {
6127c478bd9Sstevel@tonic-gate 	case S_IFREG:
6137c478bd9Sstevel@tonic-gate 		break;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	case S_IFDIR:
6167c478bd9Sstevel@tonic-gate 		rcmptime(&stb);
6177c478bd9Sstevel@tonic-gate 		return;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	default:
6207c478bd9Sstevel@tonic-gate 		error("%s: not a plain file\n", name);
6217c478bd9Sstevel@tonic-gate 		return;
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	if (stb.st_mtime > lastmod)
6257c478bd9Sstevel@tonic-gate 		log(tfp, "new: %s\n", name);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate 
628*740638c8Sbw static void
rcmptime(st)6297c478bd9Sstevel@tonic-gate rcmptime(st)
6307c478bd9Sstevel@tonic-gate 	struct stat *st;
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	register DIR *d;
6337c478bd9Sstevel@tonic-gate 	register struct dirent *dp;
6347c478bd9Sstevel@tonic-gate 	register char *cp;
6357c478bd9Sstevel@tonic-gate 	char *otp;
6367c478bd9Sstevel@tonic-gate 	int len;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	if (debug)
6397c478bd9Sstevel@tonic-gate 		printf("rcmptime(%x)\n", st);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	if ((d = opendir(target)) == NULL) {
6427c478bd9Sstevel@tonic-gate 		error("%s: %s\n", target, strerror(errno));
6437c478bd9Sstevel@tonic-gate 		return;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	otp = tp;
6467c478bd9Sstevel@tonic-gate 	len = tp - target;
6477c478bd9Sstevel@tonic-gate 	while (dp = readdir(d)) {
6487c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
6497c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
6507c478bd9Sstevel@tonic-gate 			continue;
6517c478bd9Sstevel@tonic-gate 		if (len + 1 + strlen(dp->d_name) >= RDIST_BUFSIZ - 1) {
6527c478bd9Sstevel@tonic-gate 			error("%s/%s: Name too long\n", target, dp->d_name);
6537c478bd9Sstevel@tonic-gate 			continue;
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 		tp = otp;
6567c478bd9Sstevel@tonic-gate 		*tp++ = '/';
6577c478bd9Sstevel@tonic-gate 		cp = dp->d_name;
6587c478bd9Sstevel@tonic-gate 		while (*tp++ = *cp++)
6597c478bd9Sstevel@tonic-gate 			;
6607c478bd9Sstevel@tonic-gate 		tp--;
6617c478bd9Sstevel@tonic-gate 		cmptime(target);
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 	closedir(d);
6647c478bd9Sstevel@tonic-gate 	tp = otp;
6657c478bd9Sstevel@tonic-gate 	*tp = '\0';
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate /*
6697c478bd9Sstevel@tonic-gate  * Notify the list of people the changes that were made.
6707c478bd9Sstevel@tonic-gate  * rhost == NULL if we are mailing a list of changes compared to at time
6717c478bd9Sstevel@tonic-gate  * stamp file.
6727c478bd9Sstevel@tonic-gate  */
673*740638c8Sbw static void
notify(file,rhost,to,lmod)6747c478bd9Sstevel@tonic-gate notify(file, rhost, to, lmod)
6757c478bd9Sstevel@tonic-gate 	char *file, *rhost;
6767c478bd9Sstevel@tonic-gate 	register struct namelist *to;
6777c478bd9Sstevel@tonic-gate 	time_t lmod;
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	register int fd, len;
6807c478bd9Sstevel@tonic-gate 	FILE *pf, *popen();
6817c478bd9Sstevel@tonic-gate 	struct stat stb;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	if ((options & VERIFY) || to == NULL)
6847c478bd9Sstevel@tonic-gate 		return;
6857c478bd9Sstevel@tonic-gate 	if (!qflag) {
6867c478bd9Sstevel@tonic-gate 		printf("notify ");
6877c478bd9Sstevel@tonic-gate 		if (rhost)
6887c478bd9Sstevel@tonic-gate 			printf("@%s ", rhost);
6897c478bd9Sstevel@tonic-gate 		prnames(to);
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 	if (nflag)
6927c478bd9Sstevel@tonic-gate 		return;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	if ((fd = open(file, 0)) < 0) {
6957c478bd9Sstevel@tonic-gate 		error("%s: %s\n", file, strerror(errno));
6967c478bd9Sstevel@tonic-gate 		return;
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 	if (fstat(fd, &stb) < 0) {
6997c478bd9Sstevel@tonic-gate 		error("%s: %s\n", file, strerror(errno));
7007c478bd9Sstevel@tonic-gate 		(void) close(fd);
7017c478bd9Sstevel@tonic-gate 		return;
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 	if (stb.st_size == 0) {
7047c478bd9Sstevel@tonic-gate 		(void) close(fd);
7057c478bd9Sstevel@tonic-gate 		return;
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * Create a pipe to mailling program.
7097c478bd9Sstevel@tonic-gate 	 */
7107c478bd9Sstevel@tonic-gate 	pf = popen(MAILCMD, "w");
7117c478bd9Sstevel@tonic-gate 	if (pf == NULL) {
7127c478bd9Sstevel@tonic-gate 		error("notify: \"%s\" failed\n", MAILCMD);
7137c478bd9Sstevel@tonic-gate 		(void) close(fd);
7147c478bd9Sstevel@tonic-gate 		return;
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 	/*
7177c478bd9Sstevel@tonic-gate 	 * Output the proper header information.
7187c478bd9Sstevel@tonic-gate 	 */
7197c478bd9Sstevel@tonic-gate 	fprintf(pf, "From: rdist (Remote distribution program)\n");
7207c478bd9Sstevel@tonic-gate 	fprintf(pf, "To:");
7217c478bd9Sstevel@tonic-gate 	if (!any('@', to->n_name) && rhost != NULL)
7227c478bd9Sstevel@tonic-gate 		fprintf(pf, " %s@%s", to->n_name, rhost);
7237c478bd9Sstevel@tonic-gate 	else
7247c478bd9Sstevel@tonic-gate 		fprintf(pf, " %s", to->n_name);
7257c478bd9Sstevel@tonic-gate 	to = to->n_next;
7267c478bd9Sstevel@tonic-gate 	while (to != NULL) {
7277c478bd9Sstevel@tonic-gate 		if (!any('@', to->n_name) && rhost != NULL)
7287c478bd9Sstevel@tonic-gate 			fprintf(pf, ", %s@%s", to->n_name, rhost);
7297c478bd9Sstevel@tonic-gate 		else
7307c478bd9Sstevel@tonic-gate 			fprintf(pf, ", %s", to->n_name);
7317c478bd9Sstevel@tonic-gate 		to = to->n_next;
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	putc('\n', pf);
7347c478bd9Sstevel@tonic-gate 	if (rhost != NULL)
7357c478bd9Sstevel@tonic-gate 		fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
7367c478bd9Sstevel@tonic-gate 			host, rhost);
7377c478bd9Sstevel@tonic-gate 	else
7387c478bd9Sstevel@tonic-gate 		fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod));
7397c478bd9Sstevel@tonic-gate 	putc('\n', pf);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	while ((len = read(fd, buf, RDIST_BUFSIZ)) > 0)
7427c478bd9Sstevel@tonic-gate 		(void) fwrite(buf, 1, len, pf);
7437c478bd9Sstevel@tonic-gate 	(void) close(fd);
7447c478bd9Sstevel@tonic-gate 	(void) pclose(pf);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate /*
7487c478bd9Sstevel@tonic-gate  * Return true if name is in the list.
7497c478bd9Sstevel@tonic-gate  */
750*740638c8Sbw int
inlist(list,file)7517c478bd9Sstevel@tonic-gate inlist(list, file)
7527c478bd9Sstevel@tonic-gate 	struct namelist *list;
7537c478bd9Sstevel@tonic-gate 	char *file;
7547c478bd9Sstevel@tonic-gate {
7557c478bd9Sstevel@tonic-gate 	register struct namelist *nl;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	for (nl = list; nl != NULL; nl = nl->n_next)
7587c478bd9Sstevel@tonic-gate 		if (strcmp(file, nl->n_name) == 0)
7597c478bd9Sstevel@tonic-gate 			return (1);
7607c478bd9Sstevel@tonic-gate 	return (0);
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate /*
7647c478bd9Sstevel@tonic-gate  * Return TRUE if file is in the exception list.
7657c478bd9Sstevel@tonic-gate  */
766*740638c8Sbw int
except(file)7677c478bd9Sstevel@tonic-gate except(file)
7687c478bd9Sstevel@tonic-gate 	char *file;
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate 	register struct	subcmd *sc;
7717c478bd9Sstevel@tonic-gate 	register struct	namelist *nl;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (debug)
7747c478bd9Sstevel@tonic-gate 		printf("except(%s)\n", file);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
7777c478bd9Sstevel@tonic-gate 		if (sc->sc_type != EXCEPT && sc->sc_type != PATTERN)
7787c478bd9Sstevel@tonic-gate 			continue;
7797c478bd9Sstevel@tonic-gate 		for (nl = sc->sc_args; nl != NULL; nl = nl->n_next) {
7807c478bd9Sstevel@tonic-gate 			if (sc->sc_type == EXCEPT) {
7817c478bd9Sstevel@tonic-gate 				if (strcmp(file, nl->n_name) == 0)
7827c478bd9Sstevel@tonic-gate 					return (1);
7837c478bd9Sstevel@tonic-gate 				continue;
7847c478bd9Sstevel@tonic-gate 			}
7857c478bd9Sstevel@tonic-gate 			re_comp(nl->n_name);
7867c478bd9Sstevel@tonic-gate 			if (re_exec(file) > 0)
7877c478bd9Sstevel@tonic-gate 				return (1);
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 	return (0);
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate char *
colon(cp)7947c478bd9Sstevel@tonic-gate colon(cp)
7957c478bd9Sstevel@tonic-gate 	register char *cp;
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	while (*cp) {
7997c478bd9Sstevel@tonic-gate 		if (*cp == ':')
8007c478bd9Sstevel@tonic-gate 			return (cp);
8017c478bd9Sstevel@tonic-gate 		if (*cp == '/')
8027c478bd9Sstevel@tonic-gate 			return (0);
8037c478bd9Sstevel@tonic-gate 		cp++;
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 	return (0);
8067c478bd9Sstevel@tonic-gate }
807