1 /* $OpenBSD: netdate.c,v 1.15 2003/06/02 23:32:07 millert Exp $ */
2 /* $NetBSD: netdate.c,v 1.10 1995/09/07 06:21:06 jtc Exp $ */
3
4 /*-
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36
37 #include <netinet/in.h>
38 #include <netdb.h>
39 #define TSPTYPES
40 #include <protocols/timed.h>
41
42 #include <err.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <rpc/rpc.h> /* bindresvport() proto */
49
50 #include "extern.h"
51
52 __SCCSID("@(#)netdate.c 8.2 (Berkeley) 4/28/95");
53 __RCSID("$MirOS: src/bin/date/netdate.c,v 1.2 2006/10/17 18:44:08 tg Exp $");
54
55 #define WAITACK 2 /* seconds */
56 #define WAITDATEACK 5 /* seconds */
57
58 extern int retval;
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
netsettime(time_t tval)68 netsettime(time_t tval)
69 {
70 struct timeval tout;
71 struct servent *sp;
72 struct tsp msg;
73 struct sockaddr_in sin, dest, from;
74 int fdsn;
75 fd_set *fdsp = NULL;
76 long waittime;
77 int s, timed_ack, found, error;
78 socklen_t length;
79 char hostname[MAXHOSTNAMELEN];
80
81 if ((sp = getservbyname("timed", "udp")) == NULL) {
82 warnx("udp/timed: unknown service");
83 return (retval = 2);
84 }
85
86 memset(&dest, 0, sizeof(dest));
87 dest.sin_len = sizeof(struct sockaddr_in);
88 dest.sin_family = AF_INET;
89 dest.sin_port = sp->s_port;
90 dest.sin_addr.s_addr = htonl(INADDR_ANY);
91 s = socket(AF_INET, SOCK_DGRAM, 0);
92 if (s < 0) {
93 if (errno != EPROTONOSUPPORT)
94 warn("timed");
95 return (retval = 2);
96 }
97
98 memset(&sin, 0, sizeof(sin));
99 sin.sin_len = sizeof(struct sockaddr_in);
100 sin.sin_family = AF_INET;
101 if (bindresvport(s, &sin) < 0) {
102 warnx("all ports in use");
103 goto bad;
104 }
105 msg.tsp_type = TSP_SETDATE;
106 msg.tsp_vers = TSPVERSION;
107 if (gethostname(hostname, sizeof(hostname))) {
108 warn("gethostname");
109 goto bad;
110 }
111 (void)strlcpy(msg.tsp_name, hostname, sizeof(msg.tsp_name));
112 msg.tsp_seq = htons((u_short)0);
113 msg.tsp_time.tv_sec = htonl((u_long)tval);
114 msg.tsp_time.tv_usec = htonl((u_long)0);
115 length = sizeof(struct sockaddr_in);
116 if (connect(s, (struct sockaddr *)&dest, length) < 0) {
117 warn("connect");
118 goto bad;
119 }
120 if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
121 if (errno != ECONNREFUSED)
122 warn("send");
123 goto bad;
124 }
125
126 timed_ack = -1;
127 waittime = WAITACK;
128
129 fdsn = howmany(s+1, NFDBITS) * sizeof(fd_mask);
130 if ((fdsp = (fd_set *)malloc(fdsn)) == NULL)
131 err(1, "malloc");
132 loop:
133 tout.tv_sec = waittime;
134 tout.tv_usec = 0;
135
136 memset(fdsp, 0, fdsn);
137 FD_SET(s, fdsp);
138 found = select(s+1, fdsp, NULL, NULL, &tout);
139
140 length = sizeof(error);
141 if (!getsockopt(s,
142 SOL_SOCKET, SO_ERROR, (char *)&error, &length) && error) {
143 if (error != ECONNREFUSED)
144 warn("send (delayed error)");
145 goto bad;
146 }
147
148 if (found > 0 && FD_ISSET(s, fdsp)) {
149 length = sizeof(struct sockaddr_in);
150 if (recvfrom(s, &msg, sizeof(struct tsp), 0,
151 (struct sockaddr *)&from, &length) < 0) {
152 if (errno != ECONNREFUSED)
153 warn("recvfrom");
154 goto bad;
155 }
156 msg.tsp_seq = ntohs(msg.tsp_seq);
157 msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
158 msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
159 switch (msg.tsp_type) {
160 case TSP_ACK:
161 timed_ack = TSP_ACK;
162 waittime = WAITDATEACK;
163 goto loop;
164 case TSP_DATEACK:
165 (void)close(s);
166 free(fdsp);
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
178 bad:
179 if (fdsp)
180 free(fdsp);
181 (void)close(s);
182 return (retval = 2);
183 }
184