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