1/*-
2 * Copyright (c) 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static char sccsid[] = "@(#)netdate.c	8.1 (Berkeley) 5/31/93";
33#endif /* not lint */
34#endif
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD$");
38
39#include <sys/param.h>
40#include <sys/time.h>
41#include <sys/socket.h>
42
43#include <netinet/in.h>
44#include <netdb.h>
45#define TSPTYPES
46#include <protocols/timed.h>
47
48#include <err.h>
49#include <errno.h>
50#include <string.h>
51#include <unistd.h>
52
53#include "extern.h"
54
55#define	WAITACK		2	/* seconds */
56#define	WAITDATEACK	5	/* seconds */
57
58/*
59 * Set the date in the machines controlled by timedaemons by communicating the
60 * new date to the local timedaemon.  If the timedaemon is in the master state,
61 * it performs the correction on all slaves.  If it is in the slave state, it
62 * notifies the master that a correction is needed.
63 * Returns 0 on success.  Returns > 0 on failure, setting retval to 2;
64 */
65int
66netsettime(time_t tval)
67{
68	struct timeval tout;
69	struct servent *sp;
70	struct tsp msg;
71	struct sockaddr_in lsin, dest, from;
72	fd_set ready;
73	long waittime;
74	int s, port, timed_ack, found, lerr;
75	socklen_t length;
76	char hostname[MAXHOSTNAMELEN];
77
78	if ((sp = getservbyname("timed", "udp")) == NULL) {
79		warnx("timed/udp: unknown service");
80		return (retval = 2);
81	}
82
83	dest.sin_port = sp->s_port;
84	dest.sin_family = AF_INET;
85	dest.sin_addr.s_addr = htonl((u_long)INADDR_ANY);
86	s = socket(AF_INET, SOCK_DGRAM, 0);
87	if (s < 0) {
88		if (errno != EAFNOSUPPORT)
89			warn("timed");
90		return (retval = 2);
91	}
92
93	memset(&lsin, 0, sizeof(lsin));
94	lsin.sin_family = AF_INET;
95	for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
96		lsin.sin_port = htons((u_short)port);
97		if (bind(s, (struct sockaddr *)&lsin, sizeof(lsin)) >= 0)
98			break;
99		if (errno == EADDRINUSE)
100			continue;
101		if (errno != EADDRNOTAVAIL)
102			warn("bind");
103		goto bad;
104	}
105	if (port == IPPORT_RESERVED / 2) {
106		warnx("all ports in use");
107		goto bad;
108	}
109	memset(&msg, 0, sizeof(msg));
110	msg.tsp_type = TSP_SETDATE;
111	msg.tsp_vers = TSPVERSION;
112	if (gethostname(hostname, sizeof(hostname))) {
113		warn("gethostname");
114		goto bad;
115	}
116	(void)strlcpy(msg.tsp_name, hostname, sizeof(msg.tsp_name));
117	msg.tsp_seq = htons((u_short)0);
118	msg.tsp_time.tv_sec = htonl((u_long)tval);
119	msg.tsp_time.tv_usec = htonl((u_long)0);
120	length = sizeof(struct sockaddr_in);
121	if (connect(s, (struct sockaddr *)&dest, length) < 0) {
122		warn("connect");
123		goto bad;
124	}
125	if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
126		if (errno != ECONNREFUSED)
127			warn("send");
128		goto bad;
129	}
130
131	timed_ack = -1;
132	waittime = WAITACK;
133loop:
134	tout.tv_sec = waittime;
135	tout.tv_usec = 0;
136
137	FD_ZERO(&ready);
138	FD_SET(s, &ready);
139	found = select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout);
140
141	length = sizeof(lerr);
142	if (!getsockopt(s,
143	    SOL_SOCKET, SO_ERROR, (char *)&lerr, &length) && lerr) {
144		if (lerr != ECONNREFUSED)
145			warnc(lerr, "send (delayed error)");
146		goto bad;
147	}
148
149	if (found > 0 && FD_ISSET(s, &ready)) {
150		length = sizeof(struct sockaddr_in);
151		if (recvfrom(s, &msg, sizeof(struct tsp), 0,
152		    (struct sockaddr *)&from, &length) < 0) {
153			if (errno != ECONNREFUSED)
154				warn("recvfrom");
155			goto bad;
156		}
157		msg.tsp_seq = ntohs(msg.tsp_seq);
158		msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
159		msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
160		switch (msg.tsp_type) {
161		case TSP_ACK:
162			timed_ack = TSP_ACK;
163			waittime = WAITDATEACK;
164			goto loop;
165		case TSP_DATEACK:
166			(void)close(s);
167			return (0);
168		default:
169			warnx("wrong ack received from timed: %s",
170			    tsptype[msg.tsp_type]);
171			timed_ack = -1;
172			break;
173		}
174	}
175	if (timed_ack == -1)
176		warnx("can't reach time daemon, time set locally");
177
178bad:
179	(void)close(s);
180	return (retval = 2);
181}
182