timed.c revision 239991
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 239991 2012-09-01 14:45:15Z ed $");
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
1131553Srgrimesmain(argc, argv)
1141553Srgrimes	int argc;
1151553Srgrimes	char *argv[];
1161553Srgrimes{
1171553Srgrimes	int on;
1181553Srgrimes	int ret;
1191553Srgrimes	int nflag, iflag;
1201553Srgrimes	struct timeval ntime;
1211553Srgrimes	struct servent *srvp;
1221553Srgrimes	char buf[BUFSIZ], *cp, *cplim;
1231553Srgrimes	struct ifconf ifc;
1241553Srgrimes	struct ifreq ifreq, ifreqf, *ifr;
1251553Srgrimes	register struct netinfo *ntp;
1261553Srgrimes	struct netinfo *ntip;
1271553Srgrimes	struct netinfo *savefromnet;
1281553Srgrimes	struct netent *nentp;
1291553Srgrimes	struct nets *nt;
1301553Srgrimes	struct sockaddr_in server;
1311553Srgrimes	u_short port;
132179485Simp	int c;
1331553Srgrimes
1341553Srgrimes#ifdef lint
1351553Srgrimes	ntip = NULL;
1361553Srgrimes#endif
1371553Srgrimes
1381553Srgrimes	on = 1;
1391553Srgrimes	nflag = OFF;
1401553Srgrimes	iflag = OFF;
1411553Srgrimes
1421553Srgrimes
1431553Srgrimes	opterr = 0;
14424428Simp	while ((c = getopt(argc, argv, "Mtdn:i:F:G:P:")) != -1) {
1451553Srgrimes		switch (c) {
1461553Srgrimes		case 'M':
1471553Srgrimes			Mflag = 1;
1481553Srgrimes			break;
1491553Srgrimes
1501553Srgrimes		case 't':
1511553Srgrimes			trace = 1;
1521553Srgrimes			break;
1531553Srgrimes
1541553Srgrimes		case 'n':
1551553Srgrimes			if (iflag) {
15630642Scharnier				errx(1, "-i and -n make no sense together");
1571553Srgrimes			} else {
1581553Srgrimes				nflag = ON;
1591553Srgrimes				addnetname(optarg);
1601553Srgrimes			}
1611553Srgrimes			break;
1621553Srgrimes
1631553Srgrimes		case 'i':
1641553Srgrimes			if (nflag) {
16530642Scharnier				errx(1, "-i and -n make no sense together");
1661553Srgrimes			} else {
1671553Srgrimes				iflag = ON;
1681553Srgrimes				addnetname(optarg);
1691553Srgrimes			}
1701553Srgrimes			break;
1711553Srgrimes
1721553Srgrimes		case 'F':
1731553Srgrimes			add_good_host(optarg,1);
1741553Srgrimes			while (optind < argc && argv[optind][0] != '-')
1751553Srgrimes				add_good_host(argv[optind++], 1);
1761553Srgrimes			break;
1771553Srgrimes
1781553Srgrimes		case 'd':
1791553Srgrimes			debug = 1;
1801553Srgrimes			break;
1811553Srgrimes		case 'G':
18230642Scharnier			if (goodgroup != 0)
18330642Scharnier				errx(1, "only one net group");
1841553Srgrimes			goodgroup = optarg;
1851553Srgrimes			break;
1861553Srgrimes
1871553Srgrimes		default:
18830642Scharnier			usage();
1891553Srgrimes			break;
1901553Srgrimes		}
1911553Srgrimes	}
19230642Scharnier	if (optind < argc)
19330642Scharnier		usage();
1941553Srgrimes
1951553Srgrimes	/* If we care about which machine is the master, then we must
1961553Srgrimes	 *	be willing to be a master
1971553Srgrimes	 */
1981553Srgrimes	if (0 != goodgroup || 0 != goodhosts)
1991553Srgrimes		Mflag = 1;
2001553Srgrimes
20130642Scharnier	if (gethostname(hostname, sizeof(hostname) - 1) < 0)
20230642Scharnier		err(1, "gethostname");
2031553Srgrimes	self.l_bak = &self;
2041553Srgrimes	self.l_fwd = &self;
2051553Srgrimes	self.h_bak = &self;
2061553Srgrimes	self.h_fwd = &self;
2071553Srgrimes	self.head = 1;
2081553Srgrimes	self.good = 1;
2091553Srgrimes
2101553Srgrimes	if (goodhosts != 0)		/* trust ourself */
2111553Srgrimes		add_good_host(hostname,1);
2121553Srgrimes
2131553Srgrimes	srvp = getservbyname("timed", "udp");
21430642Scharnier	if (srvp == 0)
215117278Scharnier		errx(1, "timed/udp: unknown service");
2161553Srgrimes	port = srvp->s_port;
2178532Sdg	bzero(&server, sizeof(struct sockaddr_in));
2181553Srgrimes	server.sin_port = srvp->s_port;
2191553Srgrimes	server.sin_family = AF_INET;
2201553Srgrimes	sock = socket(AF_INET, SOCK_DGRAM, 0);
22130642Scharnier	if (sock < 0)
22230642Scharnier		err(1, "socket");
2231553Srgrimes	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&on,
22430642Scharnier							sizeof(on)) < 0)
22530642Scharnier		err(1, "setsockopt");
2261553Srgrimes	if (bind(sock, (struct sockaddr*)&server, sizeof(server))) {
2271553Srgrimes		if (errno == EADDRINUSE)
22830642Scharnier			warnx("time daemon already running");
2291553Srgrimes		else
23030642Scharnier			warn("bind");
2311553Srgrimes		exit(1);
2321553Srgrimes	}
2331553Srgrimes
2341553Srgrimes	/* choose a unique seed for random number generation */
235239991Sed	(void)gettimeofday(&ntime, NULL);
2361553Srgrimes	srandom(ntime.tv_sec + ntime.tv_usec);
2371553Srgrimes
2381553Srgrimes	sequence = random();     /* initial seq number */
2391553Srgrimes
2401553Srgrimes	/* rounds kernel variable time to multiple of 5 ms. */
2411553Srgrimes	ntime.tv_sec = 0;
2421553Srgrimes	ntime.tv_usec = -((ntime.tv_usec/1000) % 5) * 1000;
2431553Srgrimes	(void)adjtime(&ntime, (struct timeval *)0);
2441553Srgrimes
2451553Srgrimes	for (nt = nets; nt; nt = nt->next) {
2461553Srgrimes		nentp = getnetbyname(nt->name);
2471553Srgrimes		if (nentp == 0) {
2481553Srgrimes			nt->net = inet_network(nt->name);
2491553Srgrimes			if (nt->net != INADDR_NONE)
2501553Srgrimes				nentp = getnetbyaddr(nt->net, AF_INET);
2511553Srgrimes		}
2521553Srgrimes		if (nentp != 0) {
2531553Srgrimes			nt->net = nentp->n_net;
2541553Srgrimes		} else if (nt->net == INADDR_NONE) {
25530642Scharnier			errx(1, "unknown net %s", nt->name);
2561553Srgrimes		} else if (nt->net == INADDR_ANY) {
25730642Scharnier			errx(1, "bad net %s", nt->name);
2581553Srgrimes		} else {
25930642Scharnier			warnx("warning: %s unknown in /etc/networks",
2601553Srgrimes				nt->name);
2611553Srgrimes		}
2621553Srgrimes
2631553Srgrimes		if (0 == (nt->net & 0xff000000))
2641553Srgrimes		    nt->net <<= 8;
2651553Srgrimes		if (0 == (nt->net & 0xff000000))
2661553Srgrimes		    nt->net <<= 8;
2671553Srgrimes		if (0 == (nt->net & 0xff000000))
2681553Srgrimes		    nt->net <<= 8;
2691553Srgrimes	}
2701553Srgrimes	ifc.ifc_len = sizeof(buf);
2711553Srgrimes	ifc.ifc_buf = buf;
27230642Scharnier	if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0)
27330642Scharnier		err(1, "get interface configuration");
2741553Srgrimes	ntp = NULL;
2751553Srgrimes#define size(p)	max((p).sa_len, sizeof(p))
2761553Srgrimes	cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
2771553Srgrimes	for (cp = buf; cp < cplim;
2781553Srgrimes			cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
2791553Srgrimes		ifr = (struct ifreq *)cp;
2801553Srgrimes		if (ifr->ifr_addr.sa_family != AF_INET)
2811553Srgrimes			continue;
2821553Srgrimes		if (!ntp)
2831553Srgrimes			ntp = (struct netinfo*)malloc(sizeof(struct netinfo));
2841553Srgrimes		bzero(ntp,sizeof(*ntp));
2851553Srgrimes		ntp->my_addr=((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
2861553Srgrimes		ntp->status = NOMASTER;
2871553Srgrimes		ifreq = *ifr;
2881553Srgrimes		ifreqf = *ifr;
2891553Srgrimes
2901553Srgrimes		if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreqf) < 0) {
29130642Scharnier			warn("get interface flags");
2921553Srgrimes			continue;
2931553Srgrimes		}
2941553Srgrimes		if ((ifreqf.ifr_flags & IFF_UP) == 0)
2951553Srgrimes			continue;
2961553Srgrimes		if ((ifreqf.ifr_flags & IFF_BROADCAST) == 0 &&
2971553Srgrimes		    (ifreqf.ifr_flags & IFF_POINTOPOINT) == 0) {
2981553Srgrimes			continue;
2991553Srgrimes		}
3001553Srgrimes
3011553Srgrimes
3021553Srgrimes		if (ioctl(sock, SIOCGIFNETMASK, (char *)&ifreq) < 0) {
30330642Scharnier			warn("get netmask");
3041553Srgrimes			continue;
3051553Srgrimes		}
3061553Srgrimes		ntp->mask = ((struct sockaddr_in *)
3071553Srgrimes			&ifreq.ifr_addr)->sin_addr.s_addr;
3081553Srgrimes
3091553Srgrimes		if (ifreqf.ifr_flags & IFF_BROADCAST) {
3101553Srgrimes			if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
31130642Scharnier				warn("get broadaddr");
3121553Srgrimes				continue;
3131553Srgrimes			}
3141553Srgrimes			ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_broadaddr;
3151553Srgrimes			/* What if the broadcast address is all ones?
3161553Srgrimes			 * So we cannot just mask ntp->dest_addr.  */
3171553Srgrimes			ntp->net = ntp->my_addr;
3181553Srgrimes			ntp->net.s_addr &= ntp->mask;
3191553Srgrimes		} else {
3201553Srgrimes			if (ioctl(sock, SIOCGIFDSTADDR,
3211553Srgrimes						(char *)&ifreq) < 0) {
32230642Scharnier				warn("get destaddr");
3231553Srgrimes				continue;
3241553Srgrimes			}
3251553Srgrimes			ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_dstaddr;
3261553Srgrimes			ntp->net = ntp->dest_addr.sin_addr;
3271553Srgrimes		}
3281553Srgrimes
3291553Srgrimes		ntp->dest_addr.sin_port = port;
3301553Srgrimes
3311553Srgrimes		for (nt = nets; nt; nt = nt->next) {
33228547Sjlemon			if (ntp->net.s_addr == htonl(nt->net))
3331553Srgrimes				break;
3341553Srgrimes		}
33530642Scharnier		if ((nflag && !nt) || (iflag && nt))
3361553Srgrimes			continue;
3371553Srgrimes
3381553Srgrimes		ntp->next = NULL;
3391553Srgrimes		if (nettab == NULL) {
3401553Srgrimes			nettab = ntp;
3411553Srgrimes		} else {
3421553Srgrimes			ntip->next = ntp;
3431553Srgrimes		}
3441553Srgrimes		ntip = ntp;
3451553Srgrimes		ntp = NULL;
3461553Srgrimes	}
3471553Srgrimes	if (ntp)
3481553Srgrimes		(void) free((char *)ntp);
34930642Scharnier	if (nettab == NULL)
35030642Scharnier		errx(1, "no network usable");
3511553Srgrimes
3521553Srgrimes	/* microseconds to delay before responding to a broadcast */
3531553Srgrimes	delay1 = casual(1, 100*1000);
3541553Srgrimes
3551553Srgrimes	/* election timer delay in secs. */
3561553Srgrimes	delay2 = casual(MINTOUT, MAXTOUT);
3571553Srgrimes
3581553Srgrimes	if (!debug)
3591553Srgrimes		daemon(debug, 0);
3601553Srgrimes
3611553Srgrimes	if (trace)
3621553Srgrimes		traceon();
3631553Srgrimes	openlog("timed", LOG_CONS|LOG_PID, LOG_DAEMON);
3641553Srgrimes
3651553Srgrimes	/*
3661553Srgrimes	 * keep returning here
3671553Srgrimes	 */
3681553Srgrimes	ret = setjmp(jmpenv);
3691553Srgrimes	savefromnet = fromnet;
3701553Srgrimes	setstatus();
3711553Srgrimes
3721553Srgrimes	if (Mflag) {
3731553Srgrimes		switch (ret) {
3741553Srgrimes
3751553Srgrimes		case 0:
3761553Srgrimes			checkignorednets();
3771553Srgrimes			pickslavenet(0);
3781553Srgrimes			break;
3791553Srgrimes		case 1:
3801553Srgrimes			/* Just lost our master */
3811553Srgrimes			if (slavenet != 0)
3821553Srgrimes				slavenet->status = election(slavenet);
3831553Srgrimes			if (!slavenet || slavenet->status == MASTER) {
3841553Srgrimes				checkignorednets();
3851553Srgrimes				pickslavenet(0);
3861553Srgrimes			} else {
3871553Srgrimes				makeslave(slavenet);	/* prune extras */
3881553Srgrimes			}
3891553Srgrimes			break;
3901553Srgrimes
3911553Srgrimes		case 2:
3921553Srgrimes			/* Just been told to quit */
3931553Srgrimes			justquit = 1;
3941553Srgrimes			pickslavenet(savefromnet);
3951553Srgrimes			break;
3961553Srgrimes		}
3971553Srgrimes
3981553Srgrimes		setstatus();
3991553Srgrimes		if (!(status & MASTER) && sock_raw != -1) {
4001553Srgrimes			/* sock_raw is not being used now */
4011553Srgrimes			(void)close(sock_raw);
4021553Srgrimes			sock_raw = -1;
4031553Srgrimes		}
4041553Srgrimes
4051553Srgrimes		if (status == MASTER)
4061553Srgrimes			master();
4071553Srgrimes		else
4081553Srgrimes			slave();
4091553Srgrimes
4101553Srgrimes	} else {
4111553Srgrimes		if (sock_raw != -1) {
4121553Srgrimes			(void)close(sock_raw);
4131553Srgrimes			sock_raw = -1;
4141553Srgrimes		}
4151553Srgrimes
4161553Srgrimes		if (ret) {
4171553Srgrimes			/* we just lost our master or were told to quit */
4181553Srgrimes			justquit = 1;
4191553Srgrimes		}
4201553Srgrimes		for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
4211553Srgrimes			if (ntp->status == MASTER)
4221553Srgrimes				rmnetmachs(ntp);
4231553Srgrimes				ntp->status = NOMASTER;
4241553Srgrimes		}
4251553Srgrimes		checkignorednets();
4261553Srgrimes		pickslavenet(0);
4271553Srgrimes		setstatus();
4281553Srgrimes
4291553Srgrimes		slave();
4301553Srgrimes	}
4311553Srgrimes	/* NOTREACHED */
4321553Srgrimes	return(0);
4331553Srgrimes}
4341553Srgrimes
43530642Scharnierstatic void
43630642Scharnierusage()
43730642Scharnier{
43830642Scharnier#ifdef HAVENIS
43930642Scharnier	fprintf(stderr,
44030642Scharnier"usage: timed [-dtM] [-i net|-n net] [-F host1 host2 ...] [-G netgp]\n");
44130642Scharnier#else
44230642Scharnier	fprintf(stderr,
44330642Scharnier"usage: timed [-dtM] [-i net|-n net] [-F host1 host2 ...]\n");
44430642Scharnier#endif /* HAVENIS */
44530642Scharnier	exit(1);
44630642Scharnier}
44730642Scharnier
4481553Srgrimes/*
4491553Srgrimes * suppress an upstart, untrustworthy, self-appointed master
4501553Srgrimes */
4511553Srgrimesvoid
4521553Srgrimessuppress(addr, name,net)
4531553Srgrimes	struct sockaddr_in *addr;
4541553Srgrimes	char *name;
4551553Srgrimes	struct netinfo *net;
4561553Srgrimes{
4571553Srgrimes	struct sockaddr_in tgt;
4581553Srgrimes	char tname[MAXHOSTNAMELEN];
4591553Srgrimes	struct tsp msg;
4601553Srgrimes	static struct timeval wait;
4611553Srgrimes
4621553Srgrimes	if (trace)
4631553Srgrimes		fprintf(fd, "suppress: %s\n", name);
4641553Srgrimes	tgt = *addr;
46530830Scharnier	(void)strcpy(tname, name);
4661553Srgrimes
4671553Srgrimes	while (0 != readmsg(TSP_ANY, ANYADDR, &wait, net)) {
4681553Srgrimes		if (trace)
4691553Srgrimes			fprintf(fd, "suppress:\tdiscarded packet from %s\n",
4701553Srgrimes				    name);
4711553Srgrimes	}
4721553Srgrimes
4731553Srgrimes	syslog(LOG_NOTICE, "suppressing false master %s", tname);
4741553Srgrimes	msg.tsp_type = TSP_QUIT;
47530830Scharnier	(void)strcpy(msg.tsp_name, hostname);
4761553Srgrimes	(void)acksend(&msg, &tgt, tname, TSP_ACK, 0, 1);
4771553Srgrimes}
4781553Srgrimes
4791553Srgrimesvoid
4801553Srgrimeslookformaster(ntp)
4811553Srgrimes	struct netinfo *ntp;
4821553Srgrimes{
4831553Srgrimes	struct tsp resp, conflict, *answer;
4841553Srgrimes	struct timeval ntime;
4851553Srgrimes	char mastername[MAXHOSTNAMELEN];
4861553Srgrimes	struct sockaddr_in masteraddr;
4871553Srgrimes
4881553Srgrimes	get_goodgroup(0);
4891553Srgrimes	ntp->status = SLAVE;
4901553Srgrimes
4911553Srgrimes	/* look for master */
4921553Srgrimes	resp.tsp_type = TSP_MASTERREQ;
49330830Scharnier	(void)strcpy(resp.tsp_name, hostname);
4941553Srgrimes	answer = acksend(&resp, &ntp->dest_addr, ANYADDR,
4951553Srgrimes			 TSP_MASTERACK, ntp, 0);
4961553Srgrimes	if (answer != 0 && !good_host_name(answer->tsp_name)) {
4971553Srgrimes		suppress(&from, answer->tsp_name, ntp);
4981553Srgrimes		ntp->status = NOMASTER;
4991553Srgrimes		answer = 0;
5001553Srgrimes	}
5011553Srgrimes	if (answer == 0) {
5021553Srgrimes		/*
5031553Srgrimes		 * Various conditions can cause conflict: races between
5041553Srgrimes		 * two just started timedaemons when no master is
5051553Srgrimes		 * present, or timedaemons started during an election.
5061553Srgrimes		 * A conservative approach is taken.  Give up and became a
5071553Srgrimes		 * slave, postponing election of a master until first
5081553Srgrimes		 * timer expires.
5091553Srgrimes		 */
5101553Srgrimes		ntime.tv_sec = ntime.tv_usec = 0;
5111553Srgrimes		answer = readmsg(TSP_MASTERREQ, ANYADDR, &ntime, ntp);
5121553Srgrimes		if (answer != 0) {
5131553Srgrimes			if (!good_host_name(answer->tsp_name)) {
5141553Srgrimes				suppress(&from, answer->tsp_name, ntp);
5151553Srgrimes				ntp->status = NOMASTER;
5161553Srgrimes			}
5171553Srgrimes			return;
5181553Srgrimes		}
5191553Srgrimes
5201553Srgrimes		ntime.tv_sec = ntime.tv_usec = 0;
5211553Srgrimes		answer = readmsg(TSP_MASTERUP, ANYADDR, &ntime, ntp);
5221553Srgrimes		if (answer != 0) {
5231553Srgrimes			if (!good_host_name(answer->tsp_name)) {
5241553Srgrimes				suppress(&from, answer->tsp_name, ntp);
5251553Srgrimes				ntp->status = NOMASTER;
5261553Srgrimes			}
5271553Srgrimes			return;
5281553Srgrimes		}
5291553Srgrimes
5301553Srgrimes		ntime.tv_sec = ntime.tv_usec = 0;
5311553Srgrimes		answer = readmsg(TSP_ELECTION, ANYADDR, &ntime, ntp);
5321553Srgrimes		if (answer != 0) {
5331553Srgrimes			if (!good_host_name(answer->tsp_name)) {
5341553Srgrimes				suppress(&from, answer->tsp_name, ntp);
5351553Srgrimes				ntp->status = NOMASTER;
5361553Srgrimes			}
5371553Srgrimes			return;
5381553Srgrimes		}
5391553Srgrimes
5401553Srgrimes		if (Mflag)
5411553Srgrimes			ntp->status = MASTER;
5421553Srgrimes		else
5431553Srgrimes			ntp->status = NOMASTER;
5441553Srgrimes		return;
5451553Srgrimes	}
5461553Srgrimes
5471553Srgrimes	ntp->status = SLAVE;
54830830Scharnier	(void)strcpy(mastername, answer->tsp_name);
5491553Srgrimes	masteraddr = from;
5501553Srgrimes
5511553Srgrimes	/*
5521553Srgrimes	 * If network has been partitioned, there might be other
5531553Srgrimes	 * masters; tell the one we have just acknowledged that
5541553Srgrimes	 * it has to gain control over the others.
5551553Srgrimes	 */
5561553Srgrimes	ntime.tv_sec = 0;
5571553Srgrimes	ntime.tv_usec = 300000;
5581553Srgrimes	answer = readmsg(TSP_MASTERACK, ANYADDR, &ntime, ntp);
5591553Srgrimes	/*
5601553Srgrimes	 * checking also not to send CONFLICT to ack'ed master
5611553Srgrimes	 * due to duplicated MASTERACKs
5621553Srgrimes	 */
5631553Srgrimes	if (answer != NULL &&
5641553Srgrimes	    strcmp(answer->tsp_name, mastername) != 0) {
5651553Srgrimes		conflict.tsp_type = TSP_CONFLICT;
56630830Scharnier		(void)strcpy(conflict.tsp_name, hostname);
5671553Srgrimes		if (!acksend(&conflict, &masteraddr, mastername,
5681553Srgrimes			     TSP_ACK, 0, 0)) {
5691553Srgrimes			syslog(LOG_ERR,
5701553Srgrimes			       "error on sending TSP_CONFLICT");
5711553Srgrimes		}
5721553Srgrimes	}
5731553Srgrimes}
5741553Srgrimes
5751553Srgrimes/*
5761553Srgrimes * based on the current network configuration, set the status, and count
5771553Srgrimes * networks;
5781553Srgrimes */
5791553Srgrimesvoid
5801553Srgrimessetstatus()
5811553Srgrimes{
5821553Srgrimes	struct netinfo *ntp;
5831553Srgrimes
5841553Srgrimes	status = 0;
5851553Srgrimes	nmasternets = nslavenets = nnets = nignorednets = 0;
5861553Srgrimes	if (trace)
5871553Srgrimes		fprintf(fd, "Net status:\n");
5881553Srgrimes	for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
5891553Srgrimes		switch ((int)ntp->status) {
5901553Srgrimes		case MASTER:
5911553Srgrimes			nmasternets++;
5921553Srgrimes			break;
5931553Srgrimes		case SLAVE:
5941553Srgrimes			nslavenets++;
5951553Srgrimes			break;
5961553Srgrimes		case NOMASTER:
5971553Srgrimes		case IGNORE:
5981553Srgrimes			nignorednets++;
5991553Srgrimes			break;
6001553Srgrimes		}
6011553Srgrimes		if (trace) {
6021553Srgrimes			fprintf(fd, "\t%-16s", inet_ntoa(ntp->net));
6031553Srgrimes			switch ((int)ntp->status) {
6041553Srgrimes			case NOMASTER:
6051553Srgrimes				fprintf(fd, "NOMASTER\n");
6061553Srgrimes				break;
6071553Srgrimes			case MASTER:
6081553Srgrimes				fprintf(fd, "MASTER\n");
6091553Srgrimes				break;
6101553Srgrimes			case SLAVE:
6111553Srgrimes				fprintf(fd, "SLAVE\n");
6121553Srgrimes				break;
6131553Srgrimes			case IGNORE:
6141553Srgrimes				fprintf(fd, "IGNORE\n");
6151553Srgrimes				break;
6161553Srgrimes			default:
6171553Srgrimes				fprintf(fd, "invalid state %d\n",
6181553Srgrimes					(int)ntp->status);
6191553Srgrimes				break;
6201553Srgrimes			}
6211553Srgrimes		}
6221553Srgrimes		nnets++;
6231553Srgrimes		status |= ntp->status;
6241553Srgrimes	}
6251553Srgrimes	status &= ~IGNORE;
6261553Srgrimes	if (trace)
6271553Srgrimes		fprintf(fd,
62837268Sbde		    "\tnets=%d masters=%d slaves=%d ignored=%d delay2=%ld\n",
62937268Sbde		    nnets, nmasternets, nslavenets, nignorednets, delay2);
6301553Srgrimes}
6311553Srgrimes
6321553Srgrimesvoid
6331553Srgrimesmakeslave(net)
6341553Srgrimes	struct netinfo *net;
6351553Srgrimes{
6361553Srgrimes	register struct netinfo *ntp;
6371553Srgrimes
6381553Srgrimes	for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
6391553Srgrimes		if (ntp->status == SLAVE && ntp != net)
6401553Srgrimes			ntp->status = IGNORE;
6411553Srgrimes	}
6421553Srgrimes	slavenet = net;
6431553Srgrimes}
6441553Srgrimes
6451553Srgrimes/*
6461553Srgrimes * Try to become master over ignored nets..
6471553Srgrimes */
6481553Srgrimesstatic void
6491553Srgrimescheckignorednets()
6501553Srgrimes{
6511553Srgrimes	register struct netinfo *ntp;
6521553Srgrimes
6531553Srgrimes	for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
6541553Srgrimes		if (!Mflag && ntp->status == SLAVE)
6551553Srgrimes			break;
6561553Srgrimes
6571553Srgrimes		if (ntp->status == IGNORE || ntp->status == NOMASTER) {
6581553Srgrimes			lookformaster(ntp);
6591553Srgrimes			if (!Mflag && ntp->status == SLAVE)
6601553Srgrimes				break;
6611553Srgrimes		}
6621553Srgrimes	}
6631553Srgrimes}
6641553Srgrimes
6651553Srgrimes/*
6661553Srgrimes * choose a good network on which to be a slave
6671553Srgrimes *	The ignored networks must have already been checked.
6681553Srgrimes *	Take a hint about for a good network.
6691553Srgrimes */
6701553Srgrimesstatic void
6711553Srgrimespickslavenet(ntp)
6721553Srgrimes	struct netinfo *ntp;
6731553Srgrimes{
6741553Srgrimes	if (slavenet != 0 && slavenet->status == SLAVE) {
6751553Srgrimes		makeslave(slavenet);		/* prune extras */
6761553Srgrimes		return;
6771553Srgrimes	}
6781553Srgrimes
6791553Srgrimes	if (ntp == 0 || ntp->status != SLAVE) {
6801553Srgrimes		for (ntp = nettab; ntp != 0; ntp = ntp->next) {
6811553Srgrimes			if (ntp->status == SLAVE)
6821553Srgrimes				break;
6831553Srgrimes		}
6841553Srgrimes	}
6851553Srgrimes	makeslave(ntp);
6861553Srgrimes}
6871553Srgrimes
6881553Srgrimes/*
6891553Srgrimes * returns a random number in the range [inf, sup]
6901553Srgrimes */
6911553Srgrimeslong
6921553Srgrimescasual(inf, sup)
6931553Srgrimes	long inf, sup;
6941553Srgrimes{
6951553Srgrimes	double value;
6961553Srgrimes
6971553Srgrimes	value = ((double)(random() & 0x7fffffff)) / (0x7fffffff*1.0);
6981553Srgrimes	return(inf + (sup - inf)*value);
6991553Srgrimes}
7001553Srgrimes
7011553Srgrimeschar *
7021553Srgrimesdate()
7031553Srgrimes{
70437268Sbde	time_t	tv_sec;
7051553Srgrimes
706239991Sed	tv_sec = time(NULL);
70737268Sbde	return (ctime(&tv_sec));
7081553Srgrimes}
7091553Srgrimes
7101553Srgrimesvoid
7111553Srgrimesaddnetname(name)
7121553Srgrimes	char *name;
7131553Srgrimes{
7141553Srgrimes	register struct nets **netlist = &nets;
7151553Srgrimes
7161553Srgrimes	while (*netlist)
7171553Srgrimes		netlist = &((*netlist)->next);
7181553Srgrimes	*netlist = (struct nets *)malloc(sizeof **netlist);
71930642Scharnier	if (*netlist == 0)
72030642Scharnier		errx(1, "malloc failed");
7211553Srgrimes	bzero((char *)*netlist, sizeof(**netlist));
7221553Srgrimes	(*netlist)->name = name;
7231553Srgrimes}
7241553Srgrimes
7251553Srgrimes/* note a host as trustworthy */
7261553Srgrimesstatic void
7271553Srgrimesadd_good_host(name, perm)
7281553Srgrimes	char *name;
7291553Srgrimes	int perm;			/* 1=not part of the netgroup */
7301553Srgrimes{
7311553Srgrimes	register struct goodhost *ghp;
7321553Srgrimes	register struct hostent *hentp;
7331553Srgrimes
7341553Srgrimes	ghp = (struct goodhost*)malloc(sizeof(*ghp));
7351553Srgrimes	if (!ghp) {
7361553Srgrimes		syslog(LOG_ERR, "malloc failed");
7371553Srgrimes		exit(1);
7381553Srgrimes	}
7391553Srgrimes
7401553Srgrimes	bzero((char*)ghp, sizeof(*ghp));
7411553Srgrimes	(void)strncpy(&ghp->name[0], name, sizeof(ghp->name));
7421553Srgrimes	ghp->next = goodhosts;
7431553Srgrimes	ghp->perm = perm;
7441553Srgrimes	goodhosts = ghp;
7451553Srgrimes
7461553Srgrimes	hentp = gethostbyname(name);
7471553Srgrimes	if (0 == hentp && perm)
74830642Scharnier		warnx("unknown host %s", name);
7491553Srgrimes}
7501553Srgrimes
7511553Srgrimes
7521553Srgrimes/* update our image of the net-group of trustworthy hosts
7531553Srgrimes */
7541553Srgrimesvoid
7551553Srgrimesget_goodgroup(force)
7561553Srgrimes	int force;
7571553Srgrimes{
7581553Srgrimes# define NG_DELAY (30*60*CLK_TCK)	/* 30 minutes */
7591553Srgrimes	static unsigned long last_update = -NG_DELAY;
7601553Srgrimes	unsigned long new_update;
76130642Scharnier	struct goodhost *ghp, **ghpp;
76230642Scharnier#ifdef HAVENIS
7631553Srgrimes	struct hosttbl *htp;
7641553Srgrimes	char *mach, *usr, *dom;
76530642Scharnier#endif /* HAVENIS */
7661553Srgrimes	struct tms tm;
7671553Srgrimes
7681553Srgrimes
7691553Srgrimes	/* if no netgroup, then we are finished */
7701553Srgrimes	if (goodgroup == 0 || !Mflag)
7711553Srgrimes		return;
7721553Srgrimes
7731553Srgrimes	/* Do not chatter with the netgroup master too often.
7741553Srgrimes	 */
7751553Srgrimes	new_update = times(&tm);
7761553Srgrimes	if (new_update < last_update + NG_DELAY
7771553Srgrimes	    && !force)
7781553Srgrimes		return;
7791553Srgrimes	last_update = new_update;
7801553Srgrimes
7811553Srgrimes	/* forget the old temporary entries */
7821553Srgrimes	ghpp = &goodhosts;
7831553Srgrimes	while (0 != (ghp = *ghpp)) {
7841553Srgrimes		if (!ghp->perm) {
7851553Srgrimes			*ghpp = ghp->next;
7861553Srgrimes			free((char*)ghp);
7871553Srgrimes		} else {
7881553Srgrimes			ghpp = &ghp->next;
7891553Srgrimes		}
7901553Srgrimes	}
7911553Srgrimes
7921553Srgrimes#ifdef HAVENIS
7931553Srgrimes	/* quit now if we are not one of the trusted masters
7941553Srgrimes	 */
7951553Srgrimes	if (!innetgr(goodgroup, &hostname[0], 0,0)) {
7961553Srgrimes		if (trace)
7971553Srgrimes			(void)fprintf(fd, "get_goodgroup: %s not in %s\n",
7981553Srgrimes				      &hostname[0], goodgroup);
7991553Srgrimes		return;
8001553Srgrimes	}
8011553Srgrimes	if (trace)
8021553Srgrimes		(void)fprintf(fd, "get_goodgroup: %s in %s\n",
8031553Srgrimes				  &hostname[0], goodgroup);
8041553Srgrimes
8051553Srgrimes	/* mark the entire netgroup as trusted */
8061553Srgrimes	(void)setnetgrent(goodgroup);
8071553Srgrimes	while (getnetgrent(&mach,&usr,&dom)) {
8081553Srgrimes		if (0 != mach)
8091553Srgrimes			add_good_host(mach,0);
8101553Srgrimes	}
8111553Srgrimes	(void)endnetgrent();
8121553Srgrimes
8131553Srgrimes	/* update list of slaves */
8141553Srgrimes	for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
8151553Srgrimes		htp->good = good_host_name(&htp->name[0]);
8161553Srgrimes	}
8171553Srgrimes#endif /* HAVENIS */
8181553Srgrimes}
8191553Srgrimes
8201553Srgrimes
8211553Srgrimes/* see if a machine is trustworthy
8221553Srgrimes */
8231553Srgrimesint					/* 1=trust hp to change our date */
8241553Srgrimesgood_host_name(name)
8251553Srgrimes	char *name;
8261553Srgrimes{
8271553Srgrimes	register struct goodhost *ghp = goodhosts;
8281553Srgrimes	register char c;
8291553Srgrimes
8301553Srgrimes	if (!ghp || !Mflag)		/* trust everyone if no one named */
8311553Srgrimes		return 1;
8321553Srgrimes
8331553Srgrimes	c = *name;
8341553Srgrimes	do {
8351553Srgrimes		if (c == ghp->name[0]
8361553Srgrimes		    && !strcasecmp(name, ghp->name))
8371553Srgrimes			return 1;	/* found him, so say so */
8381553Srgrimes	} while (0 != (ghp = ghp->next));
8391553Srgrimes
8401553Srgrimes	if (!strcasecmp(name,hostname))	/* trust ourself */
8411553Srgrimes		return 1;
8421553Srgrimes
8431553Srgrimes	return 0;			/* did not find him */
8441553Srgrimes}
845