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