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