timed.c revision 246209
11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1985, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 4. Neither the name of the University nor the names of its contributors
141553Srgrimes *    may be used to endorse or promote products derived from this software
151553Srgrimes *    without specific prior written permission.
161553Srgrimes *
171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271553Srgrimes * SUCH DAMAGE.
281553Srgrimes */
291553Srgrimes
301553Srgrimes#ifndef lint
3130642Scharnierstatic const char copyright[] =
321553Srgrimes"@(#) Copyright (c) 1985, 1993\n\
331553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
341553Srgrimes#endif /* not lint */
351553Srgrimes
36117278Scharnier#if 0
371553Srgrimes#ifndef lint
381553Srgrimesstatic char sccsid[] = "@(#)timed.c	8.1 (Berkeley) 6/6/93";
39117278Scharnier#endif /* not lint */
4030642Scharnier#endif
411553Srgrimes
42117278Scharnier#include <sys/cdefs.h>
43117278Scharnier__FBSDID("$FreeBSD: head/usr.sbin/timed/timed/timed.c 246209 2013-02-01 14:26:54Z charnier $");
44117278Scharnier
451553Srgrimes#include "globals.h"
461553Srgrimes#include <net/if.h>
471553Srgrimes#include <sys/file.h>
481553Srgrimes#include <sys/ioctl.h>
491553Srgrimes#include <setjmp.h>
501553Srgrimes#include "pathnames.h"
511553Srgrimes#include <math.h>
521553Srgrimes#include <sys/types.h>
531553Srgrimes#include <sys/times.h>
541553Srgrimes
551553Srgrimesint trace = 0;
561553Srgrimesint sock, sock_raw = -1;
571553Srgrimesint status = 0;
581553Srgrimesu_short sequence;			/* sequence number */
591553Srgrimeslong delay1;
601553Srgrimeslong delay2;
611553Srgrimes
621553Srgrimesint nslavenets;				/* nets were I could be a slave */
631553Srgrimesint nmasternets;			/* nets were I could be a master */
641553Srgrimesint nignorednets;			/* ignored nets */
651553Srgrimesint nnets;				/* nets I am connected to */
661553Srgrimes
671553SrgrimesFILE *fd;				/* trace file FD */
681553Srgrimes
691553Srgrimesjmp_buf jmpenv;
701553Srgrimes
711553Srgrimesstruct netinfo *nettab = 0;
721553Srgrimesstruct netinfo *slavenet;
731553Srgrimesint Mflag;
741553Srgrimesint justquit = 0;
751553Srgrimesint debug;
761553Srgrimes
771553Srgrimesstatic struct nets {
781553Srgrimes	char	*name;
791553Srgrimes	long	net;
801553Srgrimes	struct nets *next;
811553Srgrimes} *nets = 0;
821553Srgrimes
831553Srgrimesstruct hosttbl hosttbl[NHOSTS+1];	/* known hosts */
841553Srgrimes
851553Srgrimesstatic struct goodhost {		/* hosts that we trust */
8630872Scharnier	char	name[MAXHOSTNAMELEN];
871553Srgrimes	struct goodhost *next;
881553Srgrimes	char	perm;
891553Srgrimes} *goodhosts;
901553Srgrimes
911553Srgrimesstatic char *goodgroup;			/* net group of trusted hosts */
92117278Scharnierstatic void checkignorednets(void);
93117278Scharnierstatic void pickslavenet(struct netinfo *);
94117278Scharnierstatic void add_good_host(char *, int);
95117278Scharnierstatic void usage(void);
961553Srgrimes
971553Srgrimes/*
981553Srgrimes * The timedaemons synchronize the clocks of hosts in a local area network.
991553Srgrimes * One daemon runs as master, all the others as slaves. The master
1001553Srgrimes * performs the task of computing clock differences and sends correction
1011553Srgrimes * values to the slaves.
1021553Srgrimes * Slaves start an election to choose a new master when the latter disappears
1031553Srgrimes * because of a machine crash, network partition, or when killed.
1041553Srgrimes * A resolution protocol is used to kill all but one of the masters
1051553Srgrimes * that happen to exist in segments of a partitioned network when the
1061553Srgrimes * network partition is fixed.
1071553Srgrimes *
1081553Srgrimes * Authors: Riccardo Gusella & Stefano Zatti
1091553Srgrimes *
1101553Srgrimes * overhauled at Silicon Graphics
1111553Srgrimes */
1121553Srgrimesint
113246209Scharniermain(int argc, char *argv[])
1141553Srgrimes{
1151553Srgrimes	int on;
1161553Srgrimes	int ret;
1171553Srgrimes	int nflag, iflag;
1181553Srgrimes	struct timeval ntime;
1191553Srgrimes	struct servent *srvp;
1201553Srgrimes	char buf[BUFSIZ], *cp, *cplim;
1211553Srgrimes	struct ifconf ifc;
1221553Srgrimes	struct ifreq ifreq, ifreqf, *ifr;
1231553Srgrimes	register struct netinfo *ntp;
1241553Srgrimes	struct netinfo *ntip;
1251553Srgrimes	struct netinfo *savefromnet;
1261553Srgrimes	struct netent *nentp;
1271553Srgrimes	struct nets *nt;
1281553Srgrimes	struct sockaddr_in server;
1291553Srgrimes	u_short port;
130179485Simp	int c;
1311553Srgrimes
1321553Srgrimes#ifdef lint
1331553Srgrimes	ntip = NULL;
1341553Srgrimes#endif
1351553Srgrimes
1361553Srgrimes	on = 1;
1371553Srgrimes	nflag = OFF;
1381553Srgrimes	iflag = OFF;
1391553Srgrimes
1401553Srgrimes
1411553Srgrimes	opterr = 0;
14224428Simp	while ((c = getopt(argc, argv, "Mtdn:i:F:G:P:")) != -1) {
1431553Srgrimes		switch (c) {
1441553Srgrimes		case 'M':
1451553Srgrimes			Mflag = 1;
1461553Srgrimes			break;
1471553Srgrimes
1481553Srgrimes		case 't':
1491553Srgrimes			trace = 1;
1501553Srgrimes			break;
1511553Srgrimes
1521553Srgrimes		case 'n':
1531553Srgrimes			if (iflag) {
15430642Scharnier				errx(1, "-i and -n make no sense together");
1551553Srgrimes			} else {
1561553Srgrimes				nflag = ON;
1571553Srgrimes				addnetname(optarg);
1581553Srgrimes			}
1591553Srgrimes			break;
1601553Srgrimes
1611553Srgrimes		case 'i':
1621553Srgrimes			if (nflag) {
16330642Scharnier				errx(1, "-i and -n make no sense together");
1641553Srgrimes			} else {
1651553Srgrimes				iflag = ON;
1661553Srgrimes				addnetname(optarg);
1671553Srgrimes			}
1681553Srgrimes			break;
1691553Srgrimes
1701553Srgrimes		case 'F':
1711553Srgrimes			add_good_host(optarg,1);
1721553Srgrimes			while (optind < argc && argv[optind][0] != '-')
1731553Srgrimes				add_good_host(argv[optind++], 1);
1741553Srgrimes			break;
1751553Srgrimes
1761553Srgrimes		case 'd':
1771553Srgrimes			debug = 1;
1781553Srgrimes			break;
1791553Srgrimes		case 'G':
18030642Scharnier			if (goodgroup != 0)
18130642Scharnier				errx(1, "only one net group");
1821553Srgrimes			goodgroup = optarg;
1831553Srgrimes			break;
1841553Srgrimes
1851553Srgrimes		default:
18630642Scharnier			usage();
1871553Srgrimes			break;
1881553Srgrimes		}
1891553Srgrimes	}
19030642Scharnier	if (optind < argc)
19130642Scharnier		usage();
1921553Srgrimes
1931553Srgrimes	/* If we care about which machine is the master, then we must
1941553Srgrimes	 *	be willing to be a master
1951553Srgrimes	 */
1961553Srgrimes	if (0 != goodgroup || 0 != goodhosts)
1971553Srgrimes		Mflag = 1;
1981553Srgrimes
19930642Scharnier	if (gethostname(hostname, sizeof(hostname) - 1) < 0)
20030642Scharnier		err(1, "gethostname");
2011553Srgrimes	self.l_bak = &self;
2021553Srgrimes	self.l_fwd = &self;
2031553Srgrimes	self.h_bak = &self;
2041553Srgrimes	self.h_fwd = &self;
2051553Srgrimes	self.head = 1;
2061553Srgrimes	self.good = 1;
2071553Srgrimes
2081553Srgrimes	if (goodhosts != 0)		/* trust ourself */
2091553Srgrimes		add_good_host(hostname,1);
2101553Srgrimes
2111553Srgrimes	srvp = getservbyname("timed", "udp");
21230642Scharnier	if (srvp == 0)
213117278Scharnier		errx(1, "timed/udp: unknown service");
2141553Srgrimes	port = srvp->s_port;
2158532Sdg	bzero(&server, sizeof(struct sockaddr_in));
2161553Srgrimes	server.sin_port = srvp->s_port;
2171553Srgrimes	server.sin_family = AF_INET;
2181553Srgrimes	sock = socket(AF_INET, SOCK_DGRAM, 0);
21930642Scharnier	if (sock < 0)
22030642Scharnier		err(1, "socket");
2211553Srgrimes	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&on,
22230642Scharnier							sizeof(on)) < 0)
22330642Scharnier		err(1, "setsockopt");
2241553Srgrimes	if (bind(sock, (struct sockaddr*)&server, sizeof(server))) {
2251553Srgrimes		if (errno == EADDRINUSE)
22630642Scharnier			warnx("time daemon already running");
2271553Srgrimes		else
22830642Scharnier			warn("bind");
2291553Srgrimes		exit(1);
2301553Srgrimes	}
2311553Srgrimes
2321553Srgrimes	/* choose a unique seed for random number generation */
233239991Sed	(void)gettimeofday(&ntime, NULL);
2341553Srgrimes	srandom(ntime.tv_sec + ntime.tv_usec);
2351553Srgrimes
2361553Srgrimes	sequence = random();     /* initial seq number */
2371553Srgrimes
2381553Srgrimes	/* rounds kernel variable time to multiple of 5 ms. */
2391553Srgrimes	ntime.tv_sec = 0;
2401553Srgrimes	ntime.tv_usec = -((ntime.tv_usec/1000) % 5) * 1000;
2411553Srgrimes	(void)adjtime(&ntime, (struct timeval *)0);
2421553Srgrimes
2431553Srgrimes	for (nt = nets; nt; nt = nt->next) {
2441553Srgrimes		nentp = getnetbyname(nt->name);
2451553Srgrimes		if (nentp == 0) {
2461553Srgrimes			nt->net = inet_network(nt->name);
2471553Srgrimes			if (nt->net != INADDR_NONE)
2481553Srgrimes				nentp = getnetbyaddr(nt->net, AF_INET);
2491553Srgrimes		}
2501553Srgrimes		if (nentp != 0) {
2511553Srgrimes			nt->net = nentp->n_net;
2521553Srgrimes		} else if (nt->net == INADDR_NONE) {
25330642Scharnier			errx(1, "unknown net %s", nt->name);
2541553Srgrimes		} else if (nt->net == INADDR_ANY) {
25530642Scharnier			errx(1, "bad net %s", nt->name);
2561553Srgrimes		} else {
25730642Scharnier			warnx("warning: %s unknown in /etc/networks",
2581553Srgrimes				nt->name);
2591553Srgrimes		}
2601553Srgrimes
2611553Srgrimes		if (0 == (nt->net & 0xff000000))
2621553Srgrimes		    nt->net <<= 8;
2631553Srgrimes		if (0 == (nt->net & 0xff000000))
2641553Srgrimes		    nt->net <<= 8;
2651553Srgrimes		if (0 == (nt->net & 0xff000000))
2661553Srgrimes		    nt->net <<= 8;
2671553Srgrimes	}
2681553Srgrimes	ifc.ifc_len = sizeof(buf);
2691553Srgrimes	ifc.ifc_buf = buf;
27030642Scharnier	if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0)
27130642Scharnier		err(1, "get interface configuration");
2721553Srgrimes	ntp = NULL;
2731553Srgrimes#define size(p)	max((p).sa_len, sizeof(p))
2741553Srgrimes	cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
2751553Srgrimes	for (cp = buf; cp < cplim;
2761553Srgrimes			cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
2771553Srgrimes		ifr = (struct ifreq *)cp;
2781553Srgrimes		if (ifr->ifr_addr.sa_family != AF_INET)
2791553Srgrimes			continue;
2801553Srgrimes		if (!ntp)
2811553Srgrimes			ntp = (struct netinfo*)malloc(sizeof(struct netinfo));
2821553Srgrimes		bzero(ntp,sizeof(*ntp));
2831553Srgrimes		ntp->my_addr=((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
2841553Srgrimes		ntp->status = NOMASTER;
2851553Srgrimes		ifreq = *ifr;
2861553Srgrimes		ifreqf = *ifr;
2871553Srgrimes
2881553Srgrimes		if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreqf) < 0) {
28930642Scharnier			warn("get interface flags");
2901553Srgrimes			continue;
2911553Srgrimes		}
2921553Srgrimes		if ((ifreqf.ifr_flags & IFF_UP) == 0)
2931553Srgrimes			continue;
2941553Srgrimes		if ((ifreqf.ifr_flags & IFF_BROADCAST) == 0 &&
2951553Srgrimes		    (ifreqf.ifr_flags & IFF_POINTOPOINT) == 0) {
2961553Srgrimes			continue;
2971553Srgrimes		}
2981553Srgrimes
2991553Srgrimes
3001553Srgrimes		if (ioctl(sock, SIOCGIFNETMASK, (char *)&ifreq) < 0) {
30130642Scharnier			warn("get netmask");
3021553Srgrimes			continue;
3031553Srgrimes		}
3041553Srgrimes		ntp->mask = ((struct sockaddr_in *)
3051553Srgrimes			&ifreq.ifr_addr)->sin_addr.s_addr;
3061553Srgrimes
3071553Srgrimes		if (ifreqf.ifr_flags & IFF_BROADCAST) {
3081553Srgrimes			if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
30930642Scharnier				warn("get broadaddr");
3101553Srgrimes				continue;
3111553Srgrimes			}
3121553Srgrimes			ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_broadaddr;
3131553Srgrimes			/* What if the broadcast address is all ones?
3141553Srgrimes			 * So we cannot just mask ntp->dest_addr.  */
3151553Srgrimes			ntp->net = ntp->my_addr;
3161553Srgrimes			ntp->net.s_addr &= ntp->mask;
3171553Srgrimes		} else {
3181553Srgrimes			if (ioctl(sock, SIOCGIFDSTADDR,
3191553Srgrimes						(char *)&ifreq) < 0) {
32030642Scharnier				warn("get destaddr");
3211553Srgrimes				continue;
3221553Srgrimes			}
3231553Srgrimes			ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_dstaddr;
3241553Srgrimes			ntp->net = ntp->dest_addr.sin_addr;
3251553Srgrimes		}
3261553Srgrimes
3271553Srgrimes		ntp->dest_addr.sin_port = port;
3281553Srgrimes
3291553Srgrimes		for (nt = nets; nt; nt = nt->next) {
33028547Sjlemon			if (ntp->net.s_addr == htonl(nt->net))
3311553Srgrimes				break;
3321553Srgrimes		}
33330642Scharnier		if ((nflag && !nt) || (iflag && nt))
3341553Srgrimes			continue;
3351553Srgrimes
3361553Srgrimes		ntp->next = NULL;
3371553Srgrimes		if (nettab == NULL) {
3381553Srgrimes			nettab = ntp;
3391553Srgrimes		} else {
3401553Srgrimes			ntip->next = ntp;
3411553Srgrimes		}
3421553Srgrimes		ntip = ntp;
3431553Srgrimes		ntp = NULL;
3441553Srgrimes	}
3451553Srgrimes	if (ntp)
3461553Srgrimes		(void) free((char *)ntp);
34730642Scharnier	if (nettab == NULL)
34830642Scharnier		errx(1, "no network usable");
3491553Srgrimes
3501553Srgrimes	/* microseconds to delay before responding to a broadcast */
3511553Srgrimes	delay1 = casual(1, 100*1000);
3521553Srgrimes
3531553Srgrimes	/* election timer delay in secs. */
3541553Srgrimes	delay2 = casual(MINTOUT, MAXTOUT);
3551553Srgrimes
3561553Srgrimes	if (!debug)
3571553Srgrimes		daemon(debug, 0);
3581553Srgrimes
3591553Srgrimes	if (trace)
3601553Srgrimes		traceon();
3611553Srgrimes	openlog("timed", LOG_CONS|LOG_PID, LOG_DAEMON);
3621553Srgrimes
3631553Srgrimes	/*
3641553Srgrimes	 * keep returning here
3651553Srgrimes	 */
3661553Srgrimes	ret = setjmp(jmpenv);
3671553Srgrimes	savefromnet = fromnet;
3681553Srgrimes	setstatus();
3691553Srgrimes
3701553Srgrimes	if (Mflag) {
3711553Srgrimes		switch (ret) {
3721553Srgrimes
3731553Srgrimes		case 0:
3741553Srgrimes			checkignorednets();
3751553Srgrimes			pickslavenet(0);
3761553Srgrimes			break;
3771553Srgrimes		case 1:
3781553Srgrimes			/* Just lost our master */
3791553Srgrimes			if (slavenet != 0)
3801553Srgrimes				slavenet->status = election(slavenet);
3811553Srgrimes			if (!slavenet || slavenet->status == MASTER) {
3821553Srgrimes				checkignorednets();
3831553Srgrimes				pickslavenet(0);
3841553Srgrimes			} else {
3851553Srgrimes				makeslave(slavenet);	/* prune extras */
3861553Srgrimes			}
3871553Srgrimes			break;
3881553Srgrimes
3891553Srgrimes		case 2:
3901553Srgrimes			/* Just been told to quit */
3911553Srgrimes			justquit = 1;
3921553Srgrimes			pickslavenet(savefromnet);
3931553Srgrimes			break;
3941553Srgrimes		}
3951553Srgrimes
3961553Srgrimes		setstatus();
3971553Srgrimes		if (!(status & MASTER) && sock_raw != -1) {
3981553Srgrimes			/* sock_raw is not being used now */
3991553Srgrimes			(void)close(sock_raw);
4001553Srgrimes			sock_raw = -1;
4011553Srgrimes		}
4021553Srgrimes
4031553Srgrimes		if (status == MASTER)
4041553Srgrimes			master();
4051553Srgrimes		else
4061553Srgrimes			slave();
4071553Srgrimes
4081553Srgrimes	} else {
4091553Srgrimes		if (sock_raw != -1) {
4101553Srgrimes			(void)close(sock_raw);
4111553Srgrimes			sock_raw = -1;
4121553Srgrimes		}
4131553Srgrimes
4141553Srgrimes		if (ret) {
4151553Srgrimes			/* we just lost our master or were told to quit */
4161553Srgrimes			justquit = 1;
4171553Srgrimes		}
4181553Srgrimes		for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
419240388Skevlo			if (ntp->status == MASTER) {
4201553Srgrimes				rmnetmachs(ntp);
4211553Srgrimes				ntp->status = NOMASTER;
422240388Skevlo			}
4231553Srgrimes		}
4241553Srgrimes		checkignorednets();
4251553Srgrimes		pickslavenet(0);
4261553Srgrimes		setstatus();
4271553Srgrimes
4281553Srgrimes		slave();
4291553Srgrimes	}
4301553Srgrimes	/* NOTREACHED */
4311553Srgrimes	return(0);
4321553Srgrimes}
4331553Srgrimes
43430642Scharnierstatic void
435246209Scharnierusage(void)
43630642Scharnier{
43730642Scharnier#ifdef HAVENIS
43830642Scharnier	fprintf(stderr,
43930642Scharnier"usage: timed [-dtM] [-i net|-n net] [-F host1 host2 ...] [-G netgp]\n");
44030642Scharnier#else
44130642Scharnier	fprintf(stderr,
44230642Scharnier"usage: timed [-dtM] [-i net|-n net] [-F host1 host2 ...]\n");
44330642Scharnier#endif /* HAVENIS */
44430642Scharnier	exit(1);
44530642Scharnier}
44630642Scharnier
4471553Srgrimes/*
4481553Srgrimes * suppress an upstart, untrustworthy, self-appointed master
4491553Srgrimes */
4501553Srgrimesvoid
451246209Scharniersuppress(struct sockaddr_in *addr, char *name, struct netinfo *net)
4521553Srgrimes{
4531553Srgrimes	struct sockaddr_in tgt;
4541553Srgrimes	char tname[MAXHOSTNAMELEN];
4551553Srgrimes	struct tsp msg;
4561553Srgrimes	static struct timeval wait;
4571553Srgrimes
4581553Srgrimes	if (trace)
4591553Srgrimes		fprintf(fd, "suppress: %s\n", name);
4601553Srgrimes	tgt = *addr;
46130830Scharnier	(void)strcpy(tname, name);
4621553Srgrimes
4631553Srgrimes	while (0 != readmsg(TSP_ANY, ANYADDR, &wait, net)) {
4641553Srgrimes		if (trace)
4651553Srgrimes			fprintf(fd, "suppress:\tdiscarded packet from %s\n",
4661553Srgrimes				    name);
4671553Srgrimes	}
4681553Srgrimes
4691553Srgrimes	syslog(LOG_NOTICE, "suppressing false master %s", tname);
4701553Srgrimes	msg.tsp_type = TSP_QUIT;
47130830Scharnier	(void)strcpy(msg.tsp_name, hostname);
4721553Srgrimes	(void)acksend(&msg, &tgt, tname, TSP_ACK, 0, 1);
4731553Srgrimes}
4741553Srgrimes
4751553Srgrimesvoid
476246209Scharnierlookformaster(struct netinfo *ntp)
4771553Srgrimes{
4781553Srgrimes	struct tsp resp, conflict, *answer;
4791553Srgrimes	struct timeval ntime;
4801553Srgrimes	char mastername[MAXHOSTNAMELEN];
4811553Srgrimes	struct sockaddr_in masteraddr;
4821553Srgrimes
4831553Srgrimes	get_goodgroup(0);
4841553Srgrimes	ntp->status = SLAVE;
4851553Srgrimes
4861553Srgrimes	/* look for master */
4871553Srgrimes	resp.tsp_type = TSP_MASTERREQ;
48830830Scharnier	(void)strcpy(resp.tsp_name, hostname);
4891553Srgrimes	answer = acksend(&resp, &ntp->dest_addr, ANYADDR,
4901553Srgrimes			 TSP_MASTERACK, ntp, 0);
4911553Srgrimes	if (answer != 0 && !good_host_name(answer->tsp_name)) {
4921553Srgrimes		suppress(&from, answer->tsp_name, ntp);
4931553Srgrimes		ntp->status = NOMASTER;
4941553Srgrimes		answer = 0;
4951553Srgrimes	}
4961553Srgrimes	if (answer == 0) {
4971553Srgrimes		/*
4981553Srgrimes		 * Various conditions can cause conflict: races between
4991553Srgrimes		 * two just started timedaemons when no master is
5001553Srgrimes		 * present, or timedaemons started during an election.
5011553Srgrimes		 * A conservative approach is taken.  Give up and became a
5021553Srgrimes		 * slave, postponing election of a master until first
5031553Srgrimes		 * timer expires.
5041553Srgrimes		 */
5051553Srgrimes		ntime.tv_sec = ntime.tv_usec = 0;
5061553Srgrimes		answer = readmsg(TSP_MASTERREQ, ANYADDR, &ntime, ntp);
5071553Srgrimes		if (answer != 0) {
5081553Srgrimes			if (!good_host_name(answer->tsp_name)) {
5091553Srgrimes				suppress(&from, answer->tsp_name, ntp);
5101553Srgrimes				ntp->status = NOMASTER;
5111553Srgrimes			}
5121553Srgrimes			return;
5131553Srgrimes		}
5141553Srgrimes
5151553Srgrimes		ntime.tv_sec = ntime.tv_usec = 0;
5161553Srgrimes		answer = readmsg(TSP_MASTERUP, ANYADDR, &ntime, ntp);
5171553Srgrimes		if (answer != 0) {
5181553Srgrimes			if (!good_host_name(answer->tsp_name)) {
5191553Srgrimes				suppress(&from, answer->tsp_name, ntp);
5201553Srgrimes				ntp->status = NOMASTER;
5211553Srgrimes			}
5221553Srgrimes			return;
5231553Srgrimes		}
5241553Srgrimes
5251553Srgrimes		ntime.tv_sec = ntime.tv_usec = 0;
5261553Srgrimes		answer = readmsg(TSP_ELECTION, ANYADDR, &ntime, ntp);
5271553Srgrimes		if (answer != 0) {
5281553Srgrimes			if (!good_host_name(answer->tsp_name)) {
5291553Srgrimes				suppress(&from, answer->tsp_name, ntp);
5301553Srgrimes				ntp->status = NOMASTER;
5311553Srgrimes			}
5321553Srgrimes			return;
5331553Srgrimes		}
5341553Srgrimes
5351553Srgrimes		if (Mflag)
5361553Srgrimes			ntp->status = MASTER;
5371553Srgrimes		else
5381553Srgrimes			ntp->status = NOMASTER;
5391553Srgrimes		return;
5401553Srgrimes	}
5411553Srgrimes
5421553Srgrimes	ntp->status = SLAVE;
54330830Scharnier	(void)strcpy(mastername, answer->tsp_name);
5441553Srgrimes	masteraddr = from;
5451553Srgrimes
5461553Srgrimes	/*
5471553Srgrimes	 * If network has been partitioned, there might be other
5481553Srgrimes	 * masters; tell the one we have just acknowledged that
5491553Srgrimes	 * it has to gain control over the others.
5501553Srgrimes	 */
5511553Srgrimes	ntime.tv_sec = 0;
5521553Srgrimes	ntime.tv_usec = 300000;
5531553Srgrimes	answer = readmsg(TSP_MASTERACK, ANYADDR, &ntime, ntp);
5541553Srgrimes	/*
5551553Srgrimes	 * checking also not to send CONFLICT to ack'ed master
5561553Srgrimes	 * due to duplicated MASTERACKs
5571553Srgrimes	 */
5581553Srgrimes	if (answer != NULL &&
5591553Srgrimes	    strcmp(answer->tsp_name, mastername) != 0) {
5601553Srgrimes		conflict.tsp_type = TSP_CONFLICT;
56130830Scharnier		(void)strcpy(conflict.tsp_name, hostname);
5621553Srgrimes		if (!acksend(&conflict, &masteraddr, mastername,
5631553Srgrimes			     TSP_ACK, 0, 0)) {
5641553Srgrimes			syslog(LOG_ERR,
5651553Srgrimes			       "error on sending TSP_CONFLICT");
5661553Srgrimes		}
5671553Srgrimes	}
5681553Srgrimes}
5691553Srgrimes
5701553Srgrimes/*
5711553Srgrimes * based on the current network configuration, set the status, and count
5721553Srgrimes * networks;
5731553Srgrimes */
5741553Srgrimesvoid
575246209Scharniersetstatus(void)
5761553Srgrimes{
5771553Srgrimes	struct netinfo *ntp;
5781553Srgrimes
5791553Srgrimes	status = 0;
5801553Srgrimes	nmasternets = nslavenets = nnets = nignorednets = 0;
5811553Srgrimes	if (trace)
5821553Srgrimes		fprintf(fd, "Net status:\n");
5831553Srgrimes	for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
5841553Srgrimes		switch ((int)ntp->status) {
5851553Srgrimes		case MASTER:
5861553Srgrimes			nmasternets++;
5871553Srgrimes			break;
5881553Srgrimes		case SLAVE:
5891553Srgrimes			nslavenets++;
5901553Srgrimes			break;
5911553Srgrimes		case NOMASTER:
5921553Srgrimes		case IGNORE:
5931553Srgrimes			nignorednets++;
5941553Srgrimes			break;
5951553Srgrimes		}
5961553Srgrimes		if (trace) {
5971553Srgrimes			fprintf(fd, "\t%-16s", inet_ntoa(ntp->net));
5981553Srgrimes			switch ((int)ntp->status) {
5991553Srgrimes			case NOMASTER:
6001553Srgrimes				fprintf(fd, "NOMASTER\n");
6011553Srgrimes				break;
6021553Srgrimes			case MASTER:
6031553Srgrimes				fprintf(fd, "MASTER\n");
6041553Srgrimes				break;
6051553Srgrimes			case SLAVE:
6061553Srgrimes				fprintf(fd, "SLAVE\n");
6071553Srgrimes				break;
6081553Srgrimes			case IGNORE:
6091553Srgrimes				fprintf(fd, "IGNORE\n");
6101553Srgrimes				break;
6111553Srgrimes			default:
6121553Srgrimes				fprintf(fd, "invalid state %d\n",
6131553Srgrimes					(int)ntp->status);
6141553Srgrimes				break;
6151553Srgrimes			}
6161553Srgrimes		}
6171553Srgrimes		nnets++;
6181553Srgrimes		status |= ntp->status;
6191553Srgrimes	}
6201553Srgrimes	status &= ~IGNORE;
6211553Srgrimes	if (trace)
6221553Srgrimes		fprintf(fd,
62337268Sbde		    "\tnets=%d masters=%d slaves=%d ignored=%d delay2=%ld\n",
62437268Sbde		    nnets, nmasternets, nslavenets, nignorednets, delay2);
6251553Srgrimes}
6261553Srgrimes
6271553Srgrimesvoid
628246209Scharniermakeslave(struct netinfo *net)
6291553Srgrimes{
6301553Srgrimes	register struct netinfo *ntp;
6311553Srgrimes
6321553Srgrimes	for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
6331553Srgrimes		if (ntp->status == SLAVE && ntp != net)
6341553Srgrimes			ntp->status = IGNORE;
6351553Srgrimes	}
6361553Srgrimes	slavenet = net;
6371553Srgrimes}
6381553Srgrimes
6391553Srgrimes/*
6401553Srgrimes * Try to become master over ignored nets..
6411553Srgrimes */
6421553Srgrimesstatic void
643246209Scharniercheckignorednets(void)
6441553Srgrimes{
6451553Srgrimes	register struct netinfo *ntp;
6461553Srgrimes
6471553Srgrimes	for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
6481553Srgrimes		if (!Mflag && ntp->status == SLAVE)
6491553Srgrimes			break;
6501553Srgrimes
6511553Srgrimes		if (ntp->status == IGNORE || ntp->status == NOMASTER) {
6521553Srgrimes			lookformaster(ntp);
6531553Srgrimes			if (!Mflag && ntp->status == SLAVE)
6541553Srgrimes				break;
6551553Srgrimes		}
6561553Srgrimes	}
6571553Srgrimes}
6581553Srgrimes
6591553Srgrimes/*
6601553Srgrimes * choose a good network on which to be a slave
6611553Srgrimes *	The ignored networks must have already been checked.
6621553Srgrimes *	Take a hint about for a good network.
6631553Srgrimes */
6641553Srgrimesstatic void
665246209Scharnierpickslavenet(struct netinfo *ntp)
6661553Srgrimes{
6671553Srgrimes	if (slavenet != 0 && slavenet->status == SLAVE) {
6681553Srgrimes		makeslave(slavenet);		/* prune extras */
6691553Srgrimes		return;
6701553Srgrimes	}
6711553Srgrimes
6721553Srgrimes	if (ntp == 0 || ntp->status != SLAVE) {
6731553Srgrimes		for (ntp = nettab; ntp != 0; ntp = ntp->next) {
6741553Srgrimes			if (ntp->status == SLAVE)
6751553Srgrimes				break;
6761553Srgrimes		}
6771553Srgrimes	}
6781553Srgrimes	makeslave(ntp);
6791553Srgrimes}
6801553Srgrimes
6811553Srgrimes/*
6821553Srgrimes * returns a random number in the range [inf, sup]
6831553Srgrimes */
6841553Srgrimeslong
685246209Scharniercasual(long inf, long sup)
6861553Srgrimes{
6871553Srgrimes	double value;
6881553Srgrimes
6891553Srgrimes	value = ((double)(random() & 0x7fffffff)) / (0x7fffffff*1.0);
6901553Srgrimes	return(inf + (sup - inf)*value);
6911553Srgrimes}
6921553Srgrimes
6931553Srgrimeschar *
694246209Scharnierdate(void)
6951553Srgrimes{
69637268Sbde	time_t	tv_sec;
6971553Srgrimes
698239991Sed	tv_sec = time(NULL);
69937268Sbde	return (ctime(&tv_sec));
7001553Srgrimes}
7011553Srgrimes
7021553Srgrimesvoid
703246209Scharnieraddnetname(char *name)
7041553Srgrimes{
7051553Srgrimes	register struct nets **netlist = &nets;
7061553Srgrimes
7071553Srgrimes	while (*netlist)
7081553Srgrimes		netlist = &((*netlist)->next);
7091553Srgrimes	*netlist = (struct nets *)malloc(sizeof **netlist);
71030642Scharnier	if (*netlist == 0)
71130642Scharnier		errx(1, "malloc failed");
7121553Srgrimes	bzero((char *)*netlist, sizeof(**netlist));
7131553Srgrimes	(*netlist)->name = name;
7141553Srgrimes}
7151553Srgrimes
716246209Scharnier/* note a host as trustworthy
717246209Scharnier * perm		1=not part of the netgroup
718246209Scharnier */
7191553Srgrimesstatic void
720246209Scharnieradd_good_host(char *name, int perm)
7211553Srgrimes{
7221553Srgrimes	register struct goodhost *ghp;
7231553Srgrimes	register struct hostent *hentp;
7241553Srgrimes
7251553Srgrimes	ghp = (struct goodhost*)malloc(sizeof(*ghp));
7261553Srgrimes	if (!ghp) {
7271553Srgrimes		syslog(LOG_ERR, "malloc failed");
7281553Srgrimes		exit(1);
7291553Srgrimes	}
7301553Srgrimes
7311553Srgrimes	bzero((char*)ghp, sizeof(*ghp));
7321553Srgrimes	(void)strncpy(&ghp->name[0], name, sizeof(ghp->name));
7331553Srgrimes	ghp->next = goodhosts;
7341553Srgrimes	ghp->perm = perm;
7351553Srgrimes	goodhosts = ghp;
7361553Srgrimes
7371553Srgrimes	hentp = gethostbyname(name);
7381553Srgrimes	if (0 == hentp && perm)
73930642Scharnier		warnx("unknown host %s", name);
7401553Srgrimes}
7411553Srgrimes
7421553Srgrimes
7431553Srgrimes/* update our image of the net-group of trustworthy hosts
7441553Srgrimes */
7451553Srgrimesvoid
746246209Scharnierget_goodgroup(int force)
7471553Srgrimes{
7481553Srgrimes# define NG_DELAY (30*60*CLK_TCK)	/* 30 minutes */
7491553Srgrimes	static unsigned long last_update = -NG_DELAY;
7501553Srgrimes	unsigned long new_update;
75130642Scharnier	struct goodhost *ghp, **ghpp;
75230642Scharnier#ifdef HAVENIS
7531553Srgrimes	struct hosttbl *htp;
7541553Srgrimes	char *mach, *usr, *dom;
75530642Scharnier#endif /* HAVENIS */
7561553Srgrimes	struct tms tm;
7571553Srgrimes
7581553Srgrimes
7591553Srgrimes	/* if no netgroup, then we are finished */
7601553Srgrimes	if (goodgroup == 0 || !Mflag)
7611553Srgrimes		return;
7621553Srgrimes
7631553Srgrimes	/* Do not chatter with the netgroup master too often.
7641553Srgrimes	 */
7651553Srgrimes	new_update = times(&tm);
7661553Srgrimes	if (new_update < last_update + NG_DELAY
7671553Srgrimes	    && !force)
7681553Srgrimes		return;
7691553Srgrimes	last_update = new_update;
7701553Srgrimes
7711553Srgrimes	/* forget the old temporary entries */
7721553Srgrimes	ghpp = &goodhosts;
7731553Srgrimes	while (0 != (ghp = *ghpp)) {
7741553Srgrimes		if (!ghp->perm) {
7751553Srgrimes			*ghpp = ghp->next;
7761553Srgrimes			free((char*)ghp);
7771553Srgrimes		} else {
7781553Srgrimes			ghpp = &ghp->next;
7791553Srgrimes		}
7801553Srgrimes	}
7811553Srgrimes
7821553Srgrimes#ifdef HAVENIS
7831553Srgrimes	/* quit now if we are not one of the trusted masters
7841553Srgrimes	 */
7851553Srgrimes	if (!innetgr(goodgroup, &hostname[0], 0,0)) {
7861553Srgrimes		if (trace)
7871553Srgrimes			(void)fprintf(fd, "get_goodgroup: %s not in %s\n",
7881553Srgrimes				      &hostname[0], goodgroup);
7891553Srgrimes		return;
7901553Srgrimes	}
7911553Srgrimes	if (trace)
7921553Srgrimes		(void)fprintf(fd, "get_goodgroup: %s in %s\n",
7931553Srgrimes				  &hostname[0], goodgroup);
7941553Srgrimes
7951553Srgrimes	/* mark the entire netgroup as trusted */
7961553Srgrimes	(void)setnetgrent(goodgroup);
7971553Srgrimes	while (getnetgrent(&mach,&usr,&dom)) {
7981553Srgrimes		if (0 != mach)
7991553Srgrimes			add_good_host(mach,0);
8001553Srgrimes	}
8011553Srgrimes	(void)endnetgrent();
8021553Srgrimes
8031553Srgrimes	/* update list of slaves */
8041553Srgrimes	for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
8051553Srgrimes		htp->good = good_host_name(&htp->name[0]);
8061553Srgrimes	}
8071553Srgrimes#endif /* HAVENIS */
8081553Srgrimes}
8091553Srgrimes
8101553Srgrimes
8111553Srgrimes/* see if a machine is trustworthy
8121553Srgrimes */
8131553Srgrimesint					/* 1=trust hp to change our date */
814246209Scharniergood_host_name(char *name)
8151553Srgrimes{
8161553Srgrimes	register struct goodhost *ghp = goodhosts;
8171553Srgrimes	register char c;
8181553Srgrimes
8191553Srgrimes	if (!ghp || !Mflag)		/* trust everyone if no one named */
8201553Srgrimes		return 1;
8211553Srgrimes
8221553Srgrimes	c = *name;
8231553Srgrimes	do {
8241553Srgrimes		if (c == ghp->name[0]
8251553Srgrimes		    && !strcasecmp(name, ghp->name))
8261553Srgrimes			return 1;	/* found him, so say so */
8271553Srgrimes	} while (0 != (ghp = ghp->next));
8281553Srgrimes
8291553Srgrimes	if (!strcasecmp(name,hostname))	/* trust ourself */
8301553Srgrimes		return 1;
8311553Srgrimes
8321553Srgrimes	return 0;			/* did not find him */
8331553Srgrimes}
834