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