1 /* #pragma ident "@(#)auth_time.c 1.4 92/11/10 SMI" */
6 * This module contains the private function __rpc_get_time_offset()
7 * which will return the difference in seconds between the local system's
8 * notion of time and a remote server's notion of time. This must be
9 * possible without calling any functions that may invoke the name
10 * service. (netdir_getbyxxx, getXbyY, etc). The function is used in the
11 * synchronize call of the authdes code to synchronize clocks between
12 * NIS+ clients and their servers.
14 * Note to minimize the amount of duplicate code, portions of the
15 * synchronize() function were folded into this code, and the synchronize
16 * call becomes simply a wrapper around this function. Further, if this
17 * function is called with a timehost it *DOES* recurse to the name
18 * server so don't use it in that mode if you are doing name service code.
20 * Copyright (c) 1992 Sun Microsystems Inc.
21 * All rights reserved.
24 * When called a client handle to a RPCBIND process is created
25 * and destroyed. Two strings "netid" and "uaddr" are malloc'd
26 * and returned. The SIGALRM processing is modified only if
27 * needed to deal with TCP connections.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "namespace.h"
40 #include <sys/signal.h>
41 #include <sys/errno.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
46 #include <rpc/rpc_com.h>
47 #include <rpc/rpcb_prot.h>
49 #include <rpcsvc/nis.h>
50 #include "un-namespace.h"
52 extern int _rpc_dtablesize( void );
55 #define msg(x) printf("ERROR: %s\n", x)
56 /* #define msg(x) syslog(LOG_ERR, "%s", x) */
61 static int saw_alarm = 0;
72 * The internet time server defines the epoch to be Jan 1, 1900
73 * whereas UNIX defines it to be Jan 1, 1970. To adjust the result
74 * from internet time-service time, into UNIX time we subtract the
77 #define NYEARS (1970 - 1900)
78 #define TOFFSET ((u_long)60*60*24*(365*NYEARS + (NYEARS/4)))
82 * Stolen from rpc.nisd:
83 * Turn a 'universal address' into a struct sockaddr_in.
86 static int uaddr_to_sockaddr(uaddr, sin)
91 struct sockaddr_in *sin;
93 unsigned char p_bytes[2];
97 i = sscanf(uaddr, "%lu.%lu.%lu.%lu.%lu.%lu", &a[0], &a[1], &a[2],
103 for (i = 0; i < 4; i++)
104 sin->sin_addr.s_addr |= (a[i] & 0x000000FF) << (8 * i);
106 p_bytes[0] = (unsigned char)a[4] & 0x000000FF;
107 p_bytes[1] = (unsigned char)a[5] & 0x000000FF;
109 sin->sin_family = AF_INET; /* always */
110 bcopy((char *)&p_bytes, (char *)&sin->sin_port, 2);
118 * Free the strings that were strduped into the eps structure.
127 for (i = 0; i < num; i++) {
138 * This function constructs a nis_server structure description for the
139 * indicated hostname.
141 * NOTE: There is a chance we may end up recursing here due to the
142 * fact that gethostbyname() could do an NIS search. Ideally, the
143 * NIS+ server will call __rpc_get_time_offset() with the nis_server
144 * structure already populated.
147 get_server(sin, host, srv, eps, maxep)
148 struct sockaddr_in *sin;
149 char *host; /* name of the time host */
150 nis_server *srv; /* nis_server struct to use. */
151 endpoint eps[]; /* array of endpoints */
152 int maxep; /* max array size */
157 struct hostent dummy;
160 if (host == NULL && sin == NULL)
164 he = gethostbyname(host);
169 ptr[0] = (char *)&sin->sin_addr.s_addr;
171 dummy.h_addr_list = ptr;
175 * This is lame. We go around once for TCP, then again
178 for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep);
182 a = (struct in_addr *)he->h_addr_list[i];
183 snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a));
184 eps[num_ep].uaddr = strdup(hname);
185 eps[num_ep].family = strdup("inet");
186 eps[num_ep].proto = strdup("tcp");
189 for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep);
193 a = (struct in_addr *)he->h_addr_list[i];
194 snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a));
195 eps[num_ep].uaddr = strdup(hname);
196 eps[num_ep].family = strdup("inet");
197 eps[num_ep].proto = strdup("udp");
200 srv->name = (nis_name) host;
201 srv->ep.ep_len = num_ep;
202 srv->ep.ep_val = eps;
203 srv->key_type = NIS_PK_NONE;
204 srv->pkey.n_bytes = NULL;
210 * __rpc_get_time_offset()
212 * This function uses a nis_server structure to contact the a remote
213 * machine (as named in that structure) and returns the offset in time
214 * between that machine and this one. This offset is returned in seconds
215 * and may be positive or negative.
217 * The first time through, a lot of fiddling is done with the netconfig
218 * stuff to find a suitable transport. The function is very aggressive
219 * about choosing UDP or at worst TCP if it can. This is because
220 * those transports support both the RCPBIND call and the internet
223 * Once through, *uaddr is set to the universal address of
224 * the machine and *netid is set to the local netid for the transport
225 * that uaddr goes with. On the second call, the netconfig stuff
226 * is skipped and the uaddr/netid pair are used to fetch the netconfig
227 * structure and to then contact the machine for the time.
229 * td = "server" - "client"
232 __rpc_get_time_offset(td, srv, thost, uaddr, netid)
233 struct timeval *td; /* Time difference */
234 nis_server *srv; /* NIS Server description */
235 char *thost; /* if no server, this is the timehost */
236 char **uaddr; /* known universal address */
237 struct sockaddr_in *netid; /* known network identifier */
239 CLIENT *clnt; /* Client handle */
240 endpoint *ep, /* useful endpoints */
241 *useep = NULL; /* endpoint of xp */
242 char *useua = NULL; /* uaddr of selected xp */
243 int epl, i; /* counters */
244 enum clnt_stat status; /* result of clnt_call */
245 u_long thetime, delta;
249 int udp_ep = -1, tcp_ep = -1;
251 char ut[64], ipuaddr[64];
254 void (*oldsig)() = NULL; /* old alarm handler */
255 struct sockaddr_in sin;
264 * First check to see if we need to find and address for this
267 if (*uaddr == NULL) {
268 if ((srv != NULL) && (thost != NULL)) {
269 msg("both timehost and srv pointer used!");
273 srv = get_server(netid, thost, &tsrv, teps, 32);
275 msg("unable to contruct server data.");
278 needfree = 1; /* need to free data in endpoints */
282 epl = srv->ep.ep_len;
284 /* Identify the TCP and UDP endpoints */
286 (i < epl) && ((udp_ep == -1) || (tcp_ep == -1)); i++) {
287 if (strcasecmp(ep[i].proto, "udp") == 0)
289 if (strcasecmp(ep[i].proto, "tcp") == 0)
293 /* Check to see if it is UDP or TCP */
296 useua = ep[tcp_ep].uaddr;
298 } else if (udp_ep > -1) {
300 useua = ep[udp_ep].uaddr;
305 msg("no acceptable transport endpoints.");
307 free_eps(teps, tsrv.ep.ep_len);
313 * Create a sockaddr from the uaddr.
318 /* Fixup test for NIS+ */
319 sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4);
320 sprintf(ipuaddr, "%d.%d.%d.%d.0.111", a1, a2, a3, a4);
323 bzero((char *)&sin, sizeof(sin));
324 if (uaddr_to_sockaddr(useua, &sin)) {
325 msg("unable to translate uaddr to sockaddr.");
327 free_eps(teps, tsrv.ep.ep_len);
332 * Create the client handle to rpcbind. Note we always try
333 * version 3 since that is the earliest version that supports
334 * the RPCB_GETTIME call. Also it is the version that comes
335 * standard with SVR4. Since most everyone supports TCP/IP
336 * we could consider trying the rtime call first.
338 clnt = clnttcp_create(&sin, RPCBPROG, RPCBVERS, &s, 0, 0);
340 msg("unable to create client handle to rpcbind.");
342 free_eps(teps, tsrv.ep.ep_len);
349 status = clnt_call(clnt, RPCBPROC_GETTIME, (xdrproc_t)xdr_void, NULL,
350 (xdrproc_t)xdr_u_long, &thetime, tv);
352 * The only error we check for is anything but success. In
353 * fact we could have seen PROGMISMATCH if talking to a 4.1
354 * machine (pmap v2) or TIMEDOUT if the net was busy.
356 if (status == RPC_SUCCESS)
361 /* Blow away possible stale CLNT handle. */
368 * Convert PMAP address into timeservice address
369 * We take advantage of the fact that we "know" what
370 * the universal address looks like for inet transports.
372 * We also know that the internet timeservice is always
373 * listening on port 37.
375 sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4);
376 sprintf(ut, "%d.%d.%d.%d.0.37", a1, a2, a3, a4);
378 if (uaddr_to_sockaddr(ut, &sin)) {
379 msg("cannot convert timeservice uaddr to sockaddr.");
383 s = _socket(AF_INET, type, 0);
385 msg("unable to open fd to network.");
390 * Now depending on whether or not we're talking to
391 * UDP we set a timeout or not.
393 if (type == SOCK_DGRAM) {
394 struct timeval timeout = { 20, 0 };
395 struct sockaddr_in from;
399 if (_sendto(s, &thetime, sizeof(thetime), 0,
400 (struct sockaddr *)&sin, sizeof(sin)) == -1) {
401 msg("udp : sendto failed.");
407 res = _select(_rpc_dtablesize(), &readfds,
408 (fd_set *)NULL, (fd_set *)NULL, &timeout);
409 } while (res < 0 && errno == EINTR);
413 res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0,
414 (struct sockaddr *)&from, &len);
416 msg("recvfrom failed on udp transport.");
423 oldsig = (void (*)())signal(SIGALRM, alarm_hndler);
424 saw_alarm = 0; /* global tracking the alarm */
425 alarm(20); /* only wait 20 seconds */
426 res = _connect(s, (struct sockaddr *)&sin, sizeof(sin));
428 msg("failed to connect to tcp endpoint.");
432 msg("alarm caught it, must be unreachable.");
435 res = _read(s, (char *)&thetime, sizeof(thetime));
436 if (res != sizeof(thetime)) {
438 msg("timed out TCP call.");
440 msg("wrong size of results returned");
452 thetime = ntohl(thetime);
453 thetime = thetime - TOFFSET; /* adjust to UNIX time */
458 gettimeofday(&tv, 0);
462 * clean up our allocated data structures.
465 if (s != RPC_ANYSOCK)
471 alarm(0); /* reset that alarm if its outstanding */
473 signal(SIGALRM, oldsig);
477 * note, don't free uaddr strings until after we've made a
482 *uaddr = strdup(useua);
484 /* Round to the nearest second */
485 tv.tv_sec += (tv.tv_sec > 500000) ? 1 : 0;
486 delta = (thetime > tv.tv_sec) ? thetime - tv.tv_sec :
488 td->tv_sec = (thetime < tv.tv_sec) ? - delta : delta;
491 msg("unable to get the server's time.");
495 free_eps(teps, tsrv.ep.ep_len);