import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / nsl / auth_time.c
blob20fb6fe342a7dd3409cddba505c15ba404845181
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
29 * This module contains the private function __rpc_get_time_offset()
30 * which will return the difference in seconds between the local system's
31 * notion of time and a remote server's notion of time. This must be
32 * possible without calling any functions that may invoke the name
33 * service. (netdir_getbyxxx, getXbyY, etc). The function is used in the
34 * synchronize call of the authdes code to synchronize clocks between
35 * NIS+ clients and their servers.
37 * Note to minimize the amount of duplicate code, portions of the
38 * synchronize() function were folded into this code, and the synchronize
39 * call becomes simply a wrapper around this function. Further, if this
40 * function is called with a timehost it *DOES* recurse to the name
41 * server so don't use it in that mode if you are doing name service code.
43 * Side effects :
44 * When called a client handle to a RPCBIND process is created
45 * and destroyed. Two strings "netid" and "uaddr" are malloc'd
46 * and returned. The SIGALRM processing is modified only if
47 * needed to deal with TCP connections.
50 #include "mt.h"
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <syslog.h>
55 #include <netdir.h>
56 #include <string.h>
57 #include <strings.h>
58 #include <netconfig.h>
59 #include <netdb.h>
60 #include <signal.h>
61 #include <sys/errno.h>
62 #include <sys/poll.h>
63 #include <rpc/rpc.h>
64 #include <rpc/nettype.h>
65 #include <rpcsvc/nis.h>
68 extern void __nis_netconfig2ep(struct netconfig *, endpoint *);
69 extern bool_t __nis_netconfig_matches_ep(struct netconfig *, endpoint *);
71 #ifdef TESTING
72 #define msg(x) printf("ERROR: %s\n", x)
73 /* #define msg(x) syslog(LOG_ERR, "%s", x) */
74 #else
75 #define msg(x)
76 #endif
78 static int saw_alarm = 0;
80 /* ARGSUSED */
81 static void
82 alarm_hndler(int s)
84 saw_alarm = 1;
88 * The internet time server defines the epoch to be Jan 1, 1900
89 * whereas UNIX defines it to be Jan 1, 1970. To adjust the result
90 * from internet time-service time, into UNIX time we subtract the
91 * following offset :
93 #define NYEARS (1970 - 1900)
94 #define TOFFSET ((uint_t)60*60*24*(365*NYEARS + (NYEARS/4)))
97 * free_eps()
99 * Free the strings that were strduped into the eps structure.
101 static void
102 free_eps(endpoint eps[], int num)
104 int i;
106 for (i = 0; i < num; i++) {
107 free(eps[i].uaddr);
108 free(eps[i].proto);
109 free(eps[i].family);
114 * get_server()
116 * This function constructs a nis_server structure description for the
117 * indicated hostname.
119 static nis_server *
120 get_server(char *host, nis_server *srv, endpoint eps[], int maxep)
122 int num_ep = 0, i;
123 struct netconfig *nc;
124 void *nch;
125 struct nd_hostserv hs;
126 struct nd_addrlist *addrs;
128 if (! host)
129 return (NULL);
130 hs.h_host = host;
131 hs.h_serv = "rpcbind";
132 nch = setnetconfig();
133 while (nc = getnetconfig(nch)) {
134 if ((nc->nc_flag & NC_VISIBLE) == 0)
135 continue;
136 if (! netdir_getbyname(nc, &hs, &addrs)) {
137 for (i = 0; (i < (addrs->n_cnt)) && (num_ep < maxep);
138 i++, num_ep++) {
139 eps[num_ep].uaddr =
140 taddr2uaddr(nc, &(addrs->n_addrs[i]));
141 __nis_netconfig2ep(nc, &(eps[num_ep]));
143 netdir_free((char *)addrs, ND_ADDRLIST);
146 (void) endnetconfig(nch);
148 srv->name = (nis_name) host;
149 srv->ep.ep_len = num_ep;
150 srv->ep.ep_val = eps;
151 srv->key_type = NIS_PK_NONE;
152 srv->pkey.n_bytes = NULL;
153 srv->pkey.n_len = 0;
154 return (srv);
157 #define MEP(ep, prot) (strcasecmp(ep.proto, prot) == 0)
158 #define MAX_ENDPOINTS 32
161 * __rpc_get_time_offset()
163 * This function uses a nis_server structure to contact the a remote
164 * machine (as named in that structure) and returns the offset in time
165 * between that machine and this one. This offset is returned in seconds
166 * and may be positive or negative.
168 * The first time through, a lot of fiddling is done with the netconfig
169 * stuff to find a suitable transport. The function is very aggressive
170 * about choosing UDP or at worst TCP if it can. This is because
171 * those transports support both the RCPBIND call and the internet
172 * time service.
174 * Once through, *uaddr is set to the universal address of
175 * the machine and *netid is set to the local netid for the transport
176 * that uaddr goes with. On the second call, the netconfig stuff
177 * is skipped and the uaddr/netid pair are used to fetch the netconfig
178 * structure and to then contact the machine for the time.
180 * td = "server" - "client"
183 __rpc_get_time_offset(struct timeval *td, nis_server *srv,
184 char *thost, char **uaddr, char **netid)
186 CLIENT *clnt; /* Client handle */
187 struct netbuf *addr = 0; /* address */
188 void *nc_handle; /* Netconfig "state" */
189 struct netconfig *nc; /* Various handles */
190 endpoint *ep; /* useful endpoints */
191 char *useua = NULL, /* uaddr of selected xp */
192 *useid = NULL; /* netid of selected xp */
193 int epl, i; /* counters */
194 enum clnt_stat status; /* result of clnt_call */
195 uint_t thetime;
196 ulong_t delta;
197 int needfree = 0;
198 struct timeval tv;
199 int rtime_fd = -1, time_valid, flag = 0;
200 int a1, a2, a3, a4;
201 char ut[INET6_ADDRSTRLEN];
202 char ipuaddr[INET6_ADDRSTRLEN];
203 endpoint teps[MAX_ENDPOINTS],
204 *epcand[MAX_ENDPOINTS],
205 *nonipcand[MAX_ENDPOINTS],
206 supplied;
207 uint32_t epc, nonip;
208 nis_server tsrv;
209 void (*oldsig)() = NULL; /* old alarm handler */
210 char *dot = NULL; /* tmp pointer */
214 nc = NULL;
215 td->tv_sec = 0;
216 td->tv_usec = 0;
219 * First check to see if we need to find and address for this
220 * server.
222 if (*uaddr == NULL) {
223 if ((srv != NULL) && (thost != NULL)) {
224 msg("both timehost and srv pointer used!");
225 return (0);
227 if (! srv) {
228 srv = get_server(thost, &tsrv, teps, 32);
229 if (! srv) {
230 msg("unable to contruct server data.");
231 return (0);
233 needfree = 1; /* need to free data in endpoints */
236 nc_handle = (void *) setnetconfig();
237 if (! nc_handle) {
238 msg("unable to get netconfig info.");
239 if (needfree)
240 free_eps(teps, tsrv.ep.ep_len);
241 return (0);
244 ep = srv->ep.ep_val;
245 epl = srv->ep.ep_len;
246 for (i = 0; i < sizeof (epcand)/sizeof (epcand[0]); i++) {
247 epcand[i] = 0;
248 nonipcand[i] = 0;
250 epc = 0;
251 nonip = 0;
254 * Build the list of endpoint candidates. We prefer transports
255 * that we know are IP, but let /etc/netconfig determine the
256 * ordering among the IP transports.
258 * Note: We assume that the endpoint 'proto' field contains
259 * the netid of the transport.
261 while ((nc = getnetconfig(nc_handle)) != NULL) {
263 /* Is it a visible transport ? */
264 if ((nc->nc_flag & NC_VISIBLE) == 0)
265 continue;
267 /* Check against the end points */
268 for (i = 0; i < epl; i++) {
269 if (__nis_netconfig_matches_ep(nc, &(ep[i]))) {
270 if (MEP(ep[i], "udp") ||
271 MEP(ep[i], "udp6") ||
272 MEP(ep[i], "tcp") ||
273 MEP(ep[i], "tcp6")) {
274 epcand[epc++] = &(ep[i]);
275 } else {
276 nonipcand[nonip++] = &ep[i];
278 break;
283 (void) endnetconfig(nc_handle);
286 * epcand[] now contains the candidate transports. If there
287 * were non-IP transports as well, add them to the end of the
288 * candidate list.
290 for (i = 0; i < nonip; i++) {
291 epcand[epc++] = nonipcand[i];
294 if (epc == 0) {
295 msg("no acceptable transport endpoints.");
296 if (needfree)
297 free_eps(teps, tsrv.ep.ep_len);
298 return (0);
300 } else {
301 /* Caller supplied a uaddr. Fake an endpoint. */
302 if (*netid != 0) {
303 supplied.proto = *netid;
304 /* Is it one of the known IP transports ? */
305 if (strcmp("udp", supplied.proto) &&
306 strcmp("udp6", supplied.proto) &&
307 strcmp("tcp", supplied.proto) &&
308 strcmp("tcp6", supplied.proto)) {
309 /* No, it's not */
310 nonip = 1;
311 } else {
312 nonip = 0;
314 } else {
315 supplied.proto = (strchr(*uaddr, ':') != 0) ?
316 "udp6" : "udp";
317 nonip = 0;
319 supplied.uaddr = *uaddr;
320 supplied.family = (strchr(*uaddr, ':') != 0) ?
321 "inet6" : "inet";
322 epcand[0] = &supplied;
323 epc = 1;
324 nonip = 0;
327 nc = 0;
328 clnt = 0;
329 status = RPC_FAILED; /* Anything except RPC_SUCCESS */
332 * Loop over the endpoint candidates. Defer error reporting (except
333 * for the netconfig entry) until we've looked at all candidates.
335 for (i = 0; i < epc; i++) {
337 if (nc != 0)
338 freenetconfigent(nc);
339 nc = getnetconfigent(epcand[i]->proto);
341 if (nc == 0) {
342 msg("unable to locate netconfig info for netid.");
343 if (needfree)
344 free_eps(teps, tsrv.ep.ep_len);
345 return (0);
349 * Add the appropriate port number to the uaddr
351 useua = epcand[i]->uaddr;
352 useid = epcand[i]->proto;
353 if (strcasecmp(nc->nc_protofmly, NC_INET) == 0) {
354 (void) sscanf(useua,
355 "%d.%d.%d.%d.", &a1, &a2, &a3, &a4);
356 (void) sprintf(ipuaddr, "%d.%d.%d.%d.0.111",
357 a1, a2, a3, a4);
358 useua = &ipuaddr[0];
359 } else if (strcasecmp(nc->nc_protofmly, NC_INET6) == 0) {
360 size_t len;
361 char *port = ".0.111";
363 if (strlen(useua) >= sizeof (ipuaddr)) {
364 freenetconfigent(nc);
365 if (needfree)
366 free_eps(teps, tsrv.ep.ep_len);
367 return (0);
370 (void) strcpy(ipuaddr, useua);
372 /* get the IPv6 address out of the uaddr */
373 if ((dot = strrchr(ipuaddr, '.')) != 0) {
374 *dot = '\0';
375 if ((dot = strrchr(ipuaddr, '.')) != 0)
376 *dot = '\0';
379 if (dot == 0 ||
380 (len = strlen(ipuaddr))+strlen(port) >=
381 sizeof (ipuaddr)) {
382 freenetconfigent(nc);
383 if (needfree)
384 free_eps(teps, tsrv.ep.ep_len);
385 return (0);
388 /* now put in 0.111 */
389 (void) strcat(ipuaddr + len, port);
390 useua = ipuaddr;
394 * Create the client handle to rpcbind. Note we always try
395 * version 3 since that is the earliest version that supports
396 * the RPCB_GETTIME call. Also it is the version that comes
397 * standard with SVR4. Since most everyone supports TCP/IP
398 * we could consider trying the rtime call first.
400 if (clnt != 0)
401 clnt_destroy(clnt);
402 clnt = __nis_clnt_create(RPC_ANYFD, nc, useua, 0, 0, RPCBPROG,
403 RPCBVERS, 0, 0);
404 if (! clnt)
405 continue;
407 tv.tv_sec = 5;
408 tv.tv_usec = 0;
409 time_valid = 0;
411 status = clnt_call(clnt, RPCBPROC_GETTIME, xdr_void, NULL,
412 xdr_u_int, (char *)&thetime, tv);
414 * The only error we check for is anything but success. In
415 * fact we could have seen PROGMISMATCH if talking to a 4.1
416 * machine (pmap v2) or TIMEDOUT if the net was busy.
418 if (status == RPC_SUCCESS)
419 break;
423 if (status == RPC_SUCCESS) {
424 time_valid = 1;
425 } else if (clnt == 0) {
426 msg("unable to create client handle to rpcbind.");
427 freenetconfigent(nc);
428 if (needfree)
429 free_eps(teps, tsrv.ep.ep_len);
430 return (0);
431 } else {
434 * Try the timeservice port. This presumably only exists
435 * for IP transports, so we ignore the non-IP ones.
438 for (i = 0; i < epc-nonip; i++) {
441 * Convert PMAP address into timeservice address
442 * We take advantage of the fact that we "know" what
443 * a universal address looks like for inet transports.
445 * We also know that the internet timeservice is always
446 * listening on port 37.
449 if (nc != 0)
450 freenetconfigent(nc);
451 nc = getnetconfigent(epcand[i]->proto);
453 if (nc == 0) {
454 msg("no netconfig info for netid.");
455 if (needfree)
456 free_eps(teps, tsrv.ep.ep_len);
457 return (0);
460 useua = epcand[i]->uaddr;
461 useid = epcand[i]->proto;
463 if (strcasecmp(nc->nc_protofmly, NC_INET) == 0) {
464 (void) sscanf(useua,
465 "%d.%d.%d.%d.", &a1, &a2, &a3, &a4);
466 (void) sprintf(ut, "%d.%d.%d.%d.0.37",
467 a1, a2, a3, a4);
468 } else if (strcasecmp(nc->nc_protofmly, NC_INET6) ==
469 0) {
470 size_t len;
471 char *port = ".0.37";
473 if (strlen(useua) >= sizeof (ut)) {
474 goto error;
477 (void) strcpy(ut, useua);
479 /* get the IPv6 address out of the uaddr */
480 if ((dot = strrchr(ut, '.')) != 0) {
481 *dot = '\0';
482 if ((dot = strrchr(ut, '.')) != 0)
483 *dot = '\0';
486 if (dot == 0) {
487 goto error;
490 if ((len = strlen(ut))+strlen(port) >=
491 sizeof (ut)) {
492 goto error;
495 (void) strcat(ut + len, port);
499 addr = uaddr2taddr(nc, ut);
500 if (! addr) {
501 msg("timeservice uaddr to taddr failed.");
502 goto error;
505 rtime_fd = t_open(nc->nc_device, O_RDWR, NULL);
506 if (rtime_fd == -1) {
507 msg("unable to open fd to network.");
508 goto error;
511 if (t_bind(rtime_fd, NULL, NULL) < 0) {
512 msg("unable to bind an endpoint to fd.");
513 goto error;
517 * Now depending on whether or not we're talking to
518 * UDP we set a timeout or not.
520 if (nc->nc_semantics == NC_TPI_CLTS) {
521 struct t_unitdata tu_data;
522 struct pollfd pfd;
523 int res;
525 tu_data.addr = *addr;
526 tu_data.udata.buf = (char *)&thetime;
527 tu_data.udata.len = (uint_t)sizeof (thetime);
528 tu_data.udata.maxlen = tu_data.udata.len;
529 tu_data.opt.len = 0;
530 tu_data.opt.maxlen = 0;
531 if (t_sndudata(rtime_fd, &tu_data) == -1) {
532 msg("udp : t_sndudata failed.");
533 goto error;
535 pfd.fd = rtime_fd;
536 pfd.events =
537 POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
539 do {
540 res = poll(&pfd, 1, 10000);
541 } while (res < 0);
542 if ((res <= 0) || (pfd.revents & POLLNVAL))
543 goto error;
544 if (t_rcvudata(rtime_fd, &tu_data, &flag) <
545 0) {
546 msg("t_rvcdata failed on udp trpt.");
547 goto error;
549 time_valid = 1;
550 } else {
551 struct t_call sndcall;
553 sndcall.addr = *addr;
554 sndcall.opt.len = sndcall.opt.maxlen = 0;
555 sndcall.udata.len = sndcall.udata.maxlen = 0;
557 oldsig = (void (*)())signal(SIGALRM,
558 alarm_hndler);
559 saw_alarm = 0; /* global tracking the alarm */
560 (void) alarm(20); /* only wait 20 seconds */
561 if (t_connect(rtime_fd, &sndcall, NULL) ==
562 -1) {
563 msg("connect tcp endpoint failedd.");
564 goto error;
566 if (saw_alarm) {
567 msg("alarm caught it; unreachable.");
568 goto error;
570 if (t_rcv(rtime_fd, (char *)&thetime,
571 (uint_t)sizeof (thetime), &flag) !=
572 (uint_t)sizeof (thetime)) {
573 if (saw_alarm) {
574 /*EMPTY*/
575 msg("timed out TCP call.");
576 } else {
577 /*EMPTY*/
578 msg("wrong size results");
580 goto error;
582 time_valid = 1;
584 if (time_valid) {
585 thetime = ntohl(thetime);
586 /* adjust to UNIX time */
587 thetime = thetime - TOFFSET;
588 } else
589 thetime = 0;
593 error:
595 * clean up our allocated data structures.
597 if (addr)
598 netdir_free((char *)(addr), ND_ADDR);
600 if (rtime_fd != -1)
601 (void) t_close(rtime_fd);
603 if (clnt)
604 clnt_destroy(clnt);
606 if (nc)
607 freenetconfigent(nc);
609 if (oldsig) {
610 (void) alarm(0); /* reset that alarm if its outstanding */
611 (void) signal(SIGALRM, oldsig);
615 * note, don't free uaddr strings until after we've made a
616 * copy of them.
618 if (time_valid) {
619 if (! *netid) {
620 *netid = strdup(useid);
621 if (! *netid) {
622 msg("__rpc_get_time_offset: strdup failed.");
623 if (needfree)
624 free_eps(teps, tsrv.ep.ep_len);
625 return (0);
628 *uaddr = strdup(useua);
629 if (! *uaddr) {
630 msg("__rpc_get_time_offset: strdup failed.");
631 free(*netid);
632 if (needfree)
633 free_eps(teps, tsrv.ep.ep_len);
634 return (0);
638 (void) gettimeofday(&tv, 0);
640 /* Round to the nearest second */
641 tv.tv_sec += (tv.tv_sec > 500000) ? 1 : 0;
642 delta = (thetime > tv.tv_sec) ? thetime - tv.tv_sec :
643 tv.tv_sec - thetime;
644 td->tv_sec = (thetime < tv.tv_sec) ? - delta : delta;
645 td->tv_usec = 0;
646 } else {
647 /*EMPTY*/
648 msg("unable to get the server's time.");
651 if (needfree)
652 free_eps(teps, tsrv.ep.ep_len);
654 return (time_valid);