4 * This module contains the private function __rpc_get_time_offset()
5 * which will return the difference in seconds between the local system's
6 * notion of time and a remote server's notion of time. This must be
7 * possible without calling any functions that may invoke the name
8 * service. (netdir_getbyxxx, getXbyY, etc). The function is used in the
9 * synchronize call of the authdes code to synchronize clocks between
10 * NIS+ clients and their servers.
12 * Note to minimize the amount of duplicate code, portions of the
13 * synchronize() function were folded into this code, and the synchronize
14 * call becomes simply a wrapper around this function. Further, if this
15 * function is called with a timehost it *DOES* recurse to the name
16 * server so don't use it in that mode if you are doing name service code.
18 * Copyright (c) 1992 Sun Microsystems Inc.
19 * All rights reserved.
22 * When called a client handle to a RPCBIND process is created
23 * and destroyed. Two strings "netid" and "uaddr" are malloc'd
24 * and returned. The SIGALRM processing is modified only if
25 * needed to deal with TCP connections.
27 * @(#)auth_time.c 1.4 92/11/10 SMI
28 * $FreeBSD: src/lib/libc/rpc/auth_time.c,v 1.12 2007/09/20 22:35:24 matteo Exp $
31 #include "namespace.h"
38 #include <sys/signal.h>
39 #include <sys/errno.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
44 #include <rpc/rpc_com.h>
45 #include <rpc/rpcb_prot.h>
47 #include <rpcsvc/nis.h>
48 #include "un-namespace.h"
50 extern int _rpc_dtablesize(void);
53 #define msg(x) printf("ERROR: %s\n", x)
54 /* #define msg(x) syslog(LOG_ERR, "%s", x) */
59 static int saw_alarm = 0;
69 * The internet time server defines the epoch to be Jan 1, 1900
70 * whereas UNIX defines it to be Jan 1, 1970. To adjust the result
71 * from internet time-service time, into UNIX time we subtract the
74 #define NYEARS (1970 - 1900)
75 #define TOFFSET ((u_long)60*60*24*(365*NYEARS + (NYEARS/4)))
79 * Stolen from rpc.nisd:
80 * Turn a 'universal address' into a struct sockaddr_in.
84 uaddr_to_sockaddr(char *uaddr, struct sockaddr_in *sin)
86 unsigned char p_bytes[2];
90 i = sscanf(uaddr, "%lu.%lu.%lu.%lu.%lu.%lu", &a[0], &a[1], &a[2],
96 for (i = 0; i < 4; i++)
97 sin->sin_addr.s_addr |= (a[i] & 0x000000FF) << (8 * i);
99 p_bytes[0] = (unsigned char)a[4] & 0x000000FF;
100 p_bytes[1] = (unsigned char)a[5] & 0x000000FF;
102 sin->sin_family = AF_INET; /* always */
103 bcopy((char *)&p_bytes, (char *)&sin->sin_port, 2);
111 * Free the strings that were strduped into the eps structure.
114 free_eps(endpoint eps[], int num)
118 for (i = 0; i < num; i++) {
129 * This function constructs a nis_server structure description for the
130 * indicated hostname.
132 * NOTE: There is a chance we may end up recursing here due to the
133 * fact that gethostbyname() could do an NIS search. Ideally, the
134 * NIS+ server will call __rpc_get_time_offset() with the nis_server
135 * structure already populated.
138 get_server(struct sockaddr_in *sin,
139 char *host, /* name of the time host */
140 nis_server *srv, /* nis_server struct to use. */
141 endpoint eps[], /* array of endpoints */
142 int maxep) /* max array size */
147 struct hostent dummy;
151 if (host == NULL && sin == NULL)
155 he = gethostbyname(host);
160 ptr[0] = (char *)&sin->sin_addr.s_addr;
162 dummy.h_addr_list = ptr;
166 * This is lame. We go around once for TCP, then again
169 for (i = 0, ep = eps; (he->h_addr_list[i] != NULL) && (num_ep < maxep);
170 i++, ep++, num_ep++) {
173 a = (struct in_addr *)he->h_addr_list[i];
174 snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a));
175 ep->uaddr = strdup(hname);
176 ep->family = strdup("inet");
177 ep->proto = strdup("tcp");
178 if (ep->uaddr == NULL || ep->family == NULL || ep->proto == NULL) {
179 free_eps(eps, num_ep + 1);
184 for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep);
185 i++, ep++, num_ep++) {
188 a = (struct in_addr *)he->h_addr_list[i];
189 snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a));
190 ep->uaddr = strdup(hname);
191 ep->family = strdup("inet");
192 ep->proto = strdup("udp");
193 if (ep->uaddr == NULL || ep->family == NULL || ep->proto == NULL) {
194 free_eps(eps, num_ep + 1);
199 srv->name = (nis_name) host;
200 srv->ep.ep_len = num_ep;
201 srv->ep.ep_val = eps;
202 srv->key_type = NIS_PK_NONE;
203 srv->pkey.n_bytes = NULL;
209 * __rpc_get_time_offset()
211 * This function uses a nis_server structure to contact the a remote
212 * machine (as named in that structure) and returns the offset in time
213 * between that machine and this one. This offset is returned in seconds
214 * and may be positive or negative.
216 * The first time through, a lot of fiddling is done with the netconfig
217 * stuff to find a suitable transport. The function is very aggressive
218 * about choosing UDP or at worst TCP if it can. This is because
219 * those transports support both the RCPBIND call and the internet
222 * Once through, *uaddr is set to the universal address of
223 * the machine and *netid is set to the local netid for the transport
224 * that uaddr goes with. On the second call, the netconfig stuff
225 * is skipped and the uaddr/netid pair are used to fetch the netconfig
226 * structure and to then contact the machine for the time.
228 * td = "server" - "client"
231 __rpc_get_time_offset(struct timeval *td, /* Time difference */
232 nis_server *srv, /* NIS Server description */
233 char *thost, /* if no server, this is the timehost */
234 char **uaddr, /* known universal address */
235 struct sockaddr_in *netid)/* known network identifier */
237 CLIENT *clnt; /* Client handle */
238 endpoint *ep, /* useful endpoints */
239 *useep = NULL; /* endpoint of xp */
240 char *useua = NULL; /* uaddr of selected xp */
241 int epl, i; /* counters */
242 enum clnt_stat status; /* result of clnt_call */
243 u_long thetime, delta;
247 int udp_ep = -1, tcp_ep = -1;
249 char ut[64], ipuaddr[64];
252 void (*oldsig)() = NULL; /* old alarm handler */
253 struct sockaddr_in sin;
262 * First check to see if we need to find and address for this
265 if (*uaddr == NULL) {
266 if ((srv != NULL) && (thost != NULL)) {
267 msg("both timehost and srv pointer used!");
271 srv = get_server(netid, thost, &tsrv, teps, 32);
273 msg("unable to contruct server data.");
276 needfree = 1; /* need to free data in endpoints */
280 epl = srv->ep.ep_len;
282 /* Identify the TCP and UDP endpoints */
284 (i < epl) && ((udp_ep == -1) || (tcp_ep == -1)); i++) {
285 if (strcasecmp(ep[i].proto, "udp") == 0)
287 if (strcasecmp(ep[i].proto, "tcp") == 0)
291 /* Check to see if it is UDP or TCP */
294 useua = ep[tcp_ep].uaddr;
296 } else if (udp_ep > -1) {
298 useua = ep[udp_ep].uaddr;
303 msg("no acceptable transport endpoints.");
305 free_eps(teps, tsrv.ep.ep_len);
311 * Create a sockaddr from the uaddr.
316 /* Fixup test for NIS+ */
317 sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4);
318 sprintf(ipuaddr, "%d.%d.%d.%d.0.111", a1, a2, a3, a4);
321 bzero((char *)&sin, sizeof(sin));
322 if (uaddr_to_sockaddr(useua, &sin)) {
323 msg("unable to translate uaddr to sockaddr.");
325 free_eps(teps, tsrv.ep.ep_len);
330 * Create the client handle to rpcbind. Note we always try
331 * version 3 since that is the earliest version that supports
332 * the RPCB_GETTIME call. Also it is the version that comes
333 * standard with SVR4. Since most everyone supports TCP/IP
334 * we could consider trying the rtime call first.
336 clnt = clnttcp_create(&sin, RPCBPROG, RPCBVERS, &s, 0, 0);
338 msg("unable to create client handle to rpcbind.");
340 free_eps(teps, tsrv.ep.ep_len);
347 status = clnt_call(clnt, RPCBPROC_GETTIME, (xdrproc_t)xdr_void, NULL,
348 (xdrproc_t)xdr_u_long, &thetime, tv);
350 * The only error we check for is anything but success. In
351 * fact we could have seen PROGMISMATCH if talking to a 4.1
352 * machine (pmap v2) or TIMEDOUT if the net was busy.
354 if (status == RPC_SUCCESS)
359 /* Blow away possible stale CLNT handle. */
366 * Convert PMAP address into timeservice address
367 * We take advantage of the fact that we "know" what
368 * the universal address looks like for inet transports.
370 * We also know that the internet timeservice is always
371 * listening on port 37.
373 sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4);
374 sprintf(ut, "%d.%d.%d.%d.0.37", a1, a2, a3, a4);
376 if (uaddr_to_sockaddr(ut, &sin)) {
377 msg("cannot convert timeservice uaddr to sockaddr.");
381 s = _socket(AF_INET, type, 0);
383 msg("unable to open fd to network.");
388 * Now depending on whether or not we're talking to
389 * UDP we set a timeout or not.
391 if (type == SOCK_DGRAM) {
392 struct timeval timeout = { 20, 0 };
393 struct sockaddr_in from;
397 if (_sendto(s, &thetime, sizeof(thetime), 0,
398 (struct sockaddr *)&sin, sizeof(sin)) == -1) {
399 msg("udp : sendto failed.");
405 res = _select(_rpc_dtablesize(), &readfds,
406 NULL, NULL, &timeout);
407 } while (res < 0 && errno == EINTR);
411 res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0,
412 (struct sockaddr *)&from, &len);
414 msg("recvfrom failed on udp transport.");
421 oldsig = (void (*)())signal(SIGALRM, alarm_hndler);
422 saw_alarm = 0; /* global tracking the alarm */
423 alarm(20); /* only wait 20 seconds */
424 res = _connect(s, (struct sockaddr *)&sin, sizeof(sin));
426 msg("failed to connect to tcp endpoint.");
430 msg("alarm caught it, must be unreachable.");
433 res = _read(s, (char *)&thetime, sizeof(thetime));
434 if (res != sizeof(thetime)) {
436 msg("timed out TCP call.");
438 msg("wrong size of results returned");
451 thetime = ntohl(thetime);
452 thetime = thetime - TOFFSET; /* adjust to UNIX time */
457 gettimeofday(&tv, 0);
461 * clean up our allocated data structures.
464 if (s != RPC_ANYSOCK)
470 alarm(0); /* reset that alarm if its outstanding */
472 signal(SIGALRM, oldsig);
476 * note, don't free uaddr strings until after we've made a
481 *uaddr = strdup(useua);
483 /* Round to the nearest second */
484 tv.tv_sec += (tv.tv_sec > 500000) ? 1 : 0;
485 delta = (thetime > tv.tv_sec) ? thetime - tv.tv_sec :
487 td->tv_sec = (thetime < tv.tv_sec) ? - delta : delta;
490 msg("unable to get the server's time.");
494 free_eps(teps, tsrv.ep.ep_len);