Deleted Added
sdiff udiff text old ( 74462 ) new ( 90271 )
full compact
1#pragma ident "@(#)auth_time.c 1.4 92/11/10 SMI"
2
3/*
4 * auth_time.c
5 *
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.
13 *
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.
19 *
20 * Copyright (c) 1992 Sun Microsystems Inc.
21 * All rights reserved.
22 *
23 * Side effects :
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.
28 *
29 * NOTE: This code has had the crap beaten out it in order to convert
30 * it from TI-RPC back to TD-RPC for use on FreeBSD.
31 *
32 * $FreeBSD: head/lib/libc/rpc/auth_time.c 74462 2001-03-19 12:50:13Z alfred $
33 */
34#include "namespace.h"
35#include <stdio.h>
36#include <syslog.h>
37#include <string.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <netdb.h>
41#include <sys/signal.h>
42#include <sys/errno.h>
43#include <sys/socket.h>
44#include <netinet/in.h>
45#include <arpa/inet.h>
46#include <rpc/rpc.h>
47#include <rpc/rpc_com.h>
48#include <rpc/rpcb_prot.h>
49#undef NIS
50#include <rpcsvc/nis.h>
51#include "un-namespace.h"
52
53#ifdef TESTING
54#define msg(x) printf("ERROR: %s\n", x)
55/* #define msg(x) syslog(LOG_ERR, "%s", x) */
56#else
57#define msg(x)
58#endif
59
60static int saw_alarm = 0;
61
62static void
63alarm_hndler(s)
64 int s;
65{
66 saw_alarm = 1;
67 return;
68}
69
70/*
71 * The internet time server defines the epoch to be Jan 1, 1900
72 * whereas UNIX defines it to be Jan 1, 1970. To adjust the result
73 * from internet time-service time, into UNIX time we subtract the
74 * following offset :
75 */
76#define NYEARS (1970 - 1900)
77#define TOFFSET ((u_long)60*60*24*(365*NYEARS + (NYEARS/4)))
78
79
80/*
81 * Stolen from rpc.nisd:
82 * Turn a 'universal address' into a struct sockaddr_in.
83 * Bletch.
84 */
85static int uaddr_to_sockaddr(uaddr, sin)
86#ifdef foo
87 endpoint *endpt;
88#endif
89 char *uaddr;
90 struct sockaddr_in *sin;
91{
92 unsigned char p_bytes[2];
93 int i;
94 unsigned long a[6];
95
96 i = sscanf(uaddr, "%lu.%lu.%lu.%lu.%lu.%lu", &a[0], &a[1], &a[2],
97 &a[3], &a[4], &a[5]);
98
99 if (i < 6)
100 return(1);
101
102 for (i = 0; i < 4; i++)
103 sin->sin_addr.s_addr |= (a[i] & 0x000000FF) << (8 * i);
104
105 p_bytes[0] = (unsigned char)a[4] & 0x000000FF;
106 p_bytes[1] = (unsigned char)a[5] & 0x000000FF;
107
108 sin->sin_family = AF_INET; /* always */
109 bcopy((char *)&p_bytes, (char *)&sin->sin_port, 2);
110
111 return (0);
112}
113
114/*
115 * free_eps()
116 *
117 * Free the strings that were strduped into the eps structure.
118 */
119static void
120free_eps(eps, num)
121 endpoint eps[];
122 int num;
123{
124 int i;
125
126 for (i = 0; i < num; i++) {
127 free(eps[i].uaddr);
128 free(eps[i].proto);
129 free(eps[i].family);
130 }
131 return;
132}
133
134/*
135 * get_server()
136 *
137 * This function constructs a nis_server structure description for the
138 * indicated hostname.
139 *
140 * NOTE: There is a chance we may end up recursing here due to the
141 * fact that gethostbyname() could do an NIS search. Ideally, the
142 * NIS+ server will call __rpc_get_time_offset() with the nis_server
143 * structure already populated.
144 */
145static nis_server *
146get_server(sin, host, srv, eps, maxep)
147 struct sockaddr_in *sin;
148 char *host; /* name of the time host */
149 nis_server *srv; /* nis_server struct to use. */
150 endpoint eps[]; /* array of endpoints */
151 int maxep; /* max array size */
152{
153 char hname[256];
154 int num_ep = 0, i;
155 struct hostent *he;
156 struct hostent dummy;
157 char *ptr[2];
158
159 if (host == NULL && sin == NULL)
160 return (NULL);
161
162 if (sin == NULL) {
163 he = gethostbyname(host);
164 if (he == NULL)
165 return(NULL);
166 } else {
167 he = &dummy;
168 ptr[0] = (char *)&sin->sin_addr.s_addr;
169 ptr[1] = NULL;
170 dummy.h_addr_list = ptr;
171 }
172
173 /*
174 * This is lame. We go around once for TCP, then again
175 * for UDP.
176 */
177 for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep);
178 i++, num_ep++) {
179 struct in_addr *a;
180
181 a = (struct in_addr *)he->h_addr_list[i];
182 snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a));
183 eps[num_ep].uaddr = strdup(hname);
184 eps[num_ep].family = strdup("inet");
185 eps[num_ep].proto = strdup("tcp");
186 }
187
188 for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep);
189 i++, num_ep++) {
190 struct in_addr *a;
191
192 a = (struct in_addr *)he->h_addr_list[i];
193 snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a));
194 eps[num_ep].uaddr = strdup(hname);
195 eps[num_ep].family = strdup("inet");
196 eps[num_ep].proto = strdup("udp");
197 }
198
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;
204 srv->pkey.n_len = 0;
205 return (srv);
206}
207
208/*
209 * __rpc_get_time_offset()
210 *
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.
215 *
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
220 * time service.
221 *
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.
227 *
228 * td = "server" - "client"
229 */
230int
231__rpc_get_time_offset(td, srv, thost, uaddr, netid)
232 struct timeval *td; /* Time difference */
233 nis_server *srv; /* NIS Server description */
234 char *thost; /* if no server, this is the timehost */
235 char **uaddr; /* known universal address */
236 struct sockaddr_in *netid; /* known network identifier */
237{
238 CLIENT *clnt; /* Client handle */
239 endpoint *ep, /* useful endpoints */
240 *useep = NULL; /* endpoint of xp */
241 char *useua = NULL; /* uaddr of selected xp */
242 int epl, i; /* counters */
243 enum clnt_stat status; /* result of clnt_call */
244 u_long thetime, delta;
245 int needfree = 0;
246 struct timeval tv;
247 int time_valid;
248 int udp_ep = -1, tcp_ep = -1;
249 int a1, a2, a3, a4;
250 char ut[64], ipuaddr[64];
251 endpoint teps[32];
252 nis_server tsrv;
253 void (*oldsig)() = NULL; /* old alarm handler */
254 struct sockaddr_in sin;
255 int s = RPC_ANYSOCK, len;
256 int type = 0;
257
258 td->tv_sec = 0;
259 td->tv_usec = 0;
260
261 /*
262 * First check to see if we need to find and address for this
263 * server.
264 */
265 if (*uaddr == NULL) {
266 if ((srv != NULL) && (thost != NULL)) {
267 msg("both timehost and srv pointer used!");
268 return (0);
269 }
270 if (! srv) {
271 srv = get_server(netid, thost, &tsrv, teps, 32);
272 if (srv == NULL) {
273 msg("unable to contruct server data.");
274 return (0);
275 }
276 needfree = 1; /* need to free data in endpoints */
277 }
278
279 ep = srv->ep.ep_val;
280 epl = srv->ep.ep_len;
281
282 /* Identify the TCP and UDP endpoints */
283 for (i = 0;
284 (i < epl) && ((udp_ep == -1) || (tcp_ep == -1)); i++) {
285 if (strcasecmp(ep[i].proto, "udp") == 0)
286 udp_ep = i;
287 if (strcasecmp(ep[i].proto, "tcp") == 0)
288 tcp_ep = i;
289 }
290
291 /* Check to see if it is UDP or TCP */
292 if (tcp_ep > -1) {
293 useep = &ep[tcp_ep];
294 useua = ep[tcp_ep].uaddr;
295 type = SOCK_STREAM;
296 } else if (udp_ep > -1) {
297 useep = &ep[udp_ep];
298 useua = ep[udp_ep].uaddr;
299 type = SOCK_DGRAM;
300 }
301
302 if (useep == NULL) {
303 msg("no acceptable transport endpoints.");
304 if (needfree)
305 free_eps(teps, tsrv.ep.ep_len);
306 return (0);
307 }
308 }
309
310 /*
311 * Create a sockaddr from the uaddr.
312 */
313 if (*uaddr != NULL)
314 useua = *uaddr;
315
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);
319 useua = &ipuaddr[0];
320
321 bzero((char *)&sin, sizeof(sin));
322 if (uaddr_to_sockaddr(useua, &sin)) {
323 msg("unable to translate uaddr to sockaddr.");
324 if (needfree)
325 free_eps(teps, tsrv.ep.ep_len);
326 return (0);
327 }
328
329 /*
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.
335 */
336 clnt = clnttcp_create(&sin, RPCBPROG, RPCBVERS, &s, 0, 0);
337 if (clnt == NULL) {
338 msg("unable to create client handle to rpcbind.");
339 if (needfree)
340 free_eps(teps, tsrv.ep.ep_len);
341 return (0);
342 }
343
344 tv.tv_sec = 5;
345 tv.tv_usec = 0;
346 time_valid = 0;
347 status = clnt_call(clnt, RPCBPROC_GETTIME, xdr_void, NULL,
348 xdr_u_long, (char *)&thetime, tv);
349 /*
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.
353 */
354 if (status == RPC_SUCCESS)
355 time_valid = 1;
356 else {
357 int save;
358
359 /* Blow away possible stale CLNT handle. */
360 if (clnt != NULL) {
361 clnt_destroy(clnt);
362 clnt = NULL;
363 }
364
365 /*
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.
369 *
370 * We also know that the internet timeservice is always
371 * listening on port 37.
372 */
373 sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4);
374 sprintf(ut, "%d.%d.%d.%d.0.37", a1, a2, a3, a4);
375
376 if (uaddr_to_sockaddr(ut, &sin)) {
377 msg("cannot convert timeservice uaddr to sockaddr.");
378 goto error;
379 }
380
381 s = _socket(AF_INET, type, 0);
382 if (s == -1) {
383 msg("unable to open fd to network.");
384 goto error;
385 }
386
387 /*
388 * Now depending on whether or not we're talking to
389 * UDP we set a timeout or not.
390 */
391 if (type == SOCK_DGRAM) {
392 struct timeval timeout = { 20, 0 };
393 struct sockaddr_in from;
394 fd_set readfds;
395 int res;
396
397 if (_sendto(s, &thetime, sizeof(thetime), 0,
398 (struct sockaddr *)&sin, sizeof(sin)) == -1) {
399 msg("udp : sendto failed.");
400 goto error;
401 }
402 do {
403 FD_ZERO(&readfds);
404 FD_SET(s, &readfds);
405 res = _select(_rpc_dtablesize(), &readfds,
406 (fd_set *)NULL, (fd_set *)NULL, &timeout);
407 } while (res < 0 && errno == EINTR);
408 if (res <= 0)
409 goto error;
410 len = sizeof(from);
411 res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0,
412 (struct sockaddr *)&from, &len);
413 if (res == -1) {
414 msg("recvfrom failed on udp transport.");
415 goto error;
416 }
417 time_valid = 1;
418 } else {
419 int res;
420
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));
425 if (res == -1) {
426 msg("failed to connect to tcp endpoint.");
427 goto error;
428 }
429 if (saw_alarm) {
430 msg("alarm caught it, must be unreachable.");
431 goto error;
432 }
433 res = _read(s, (char *)&thetime, sizeof(thetime));
434 if (res != sizeof(thetime)) {
435 if (saw_alarm)
436 msg("timed out TCP call.");
437 else
438 msg("wrong size of results returned");
439
440 goto error;
441 }
442 time_valid = 1;
443 }
444 save = errno;
445 (void)_close(s);
446 errno = save;
447 s = RPC_ANYSOCK;
448
449 if (time_valid) {
450 thetime = ntohl(thetime);
451 thetime = thetime - TOFFSET; /* adjust to UNIX time */
452 } else
453 thetime = 0;
454 }
455
456 gettimeofday(&tv, 0);
457
458error:
459 /*
460 * clean up our allocated data structures.
461 */
462
463 if (s != RPC_ANYSOCK)
464 (void)_close(s);
465
466 if (clnt != NULL)
467 clnt_destroy(clnt);
468
469 alarm(0); /* reset that alarm if its outstanding */
470 if (oldsig) {
471 signal(SIGALRM, oldsig);
472 }
473
474 /*
475 * note, don't free uaddr strings until after we've made a
476 * copy of them.
477 */
478 if (time_valid) {
479 if (*uaddr == NULL)
480 *uaddr = strdup(useua);
481
482 /* Round to the nearest second */
483 tv.tv_sec += (tv.tv_sec > 500000) ? 1 : 0;
484 delta = (thetime > tv.tv_sec) ? thetime - tv.tv_sec :
485 tv.tv_sec - thetime;
486 td->tv_sec = (thetime < tv.tv_sec) ? - delta : delta;
487 td->tv_usec = 0;
488 } else {
489 msg("unable to get the server's time.");
490 }
491
492 if (needfree)
493 free_eps(teps, tsrv.ep.ep_len);
494
495 return (time_valid);
496}