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
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]
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.
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.
58 #include <netconfig.h>
61 #include <sys/errno.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
*);
72 #define msg(x) printf("ERROR: %s\n", x)
73 /* #define msg(x) syslog(LOG_ERR, "%s", x) */
78 static int saw_alarm
= 0;
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
93 #define NYEARS (1970 - 1900)
94 #define TOFFSET ((uint_t)60*60*24*(365*NYEARS + (NYEARS/4)))
99 * Free the strings that were strduped into the eps structure.
102 free_eps(endpoint eps
[], int num
)
106 for (i
= 0; i
< num
; i
++) {
116 * This function constructs a nis_server structure description for the
117 * indicated hostname.
120 get_server(char *host
, nis_server
*srv
, endpoint eps
[], int maxep
)
123 struct netconfig
*nc
;
125 struct nd_hostserv hs
;
126 struct nd_addrlist
*addrs
;
131 hs
.h_serv
= "rpcbind";
132 nch
= setnetconfig();
133 while (nc
= getnetconfig(nch
)) {
134 if ((nc
->nc_flag
& NC_VISIBLE
) == 0)
136 if (! netdir_getbyname(nc
, &hs
, &addrs
)) {
137 for (i
= 0; (i
< (addrs
->n_cnt
)) && (num_ep
< maxep
);
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
;
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
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 */
199 int rtime_fd
= -1, time_valid
, flag
= 0;
201 char ut
[INET6_ADDRSTRLEN
];
202 char ipuaddr
[INET6_ADDRSTRLEN
];
203 endpoint teps
[MAX_ENDPOINTS
],
204 *epcand
[MAX_ENDPOINTS
],
205 *nonipcand
[MAX_ENDPOINTS
],
209 void (*oldsig
)() = NULL
; /* old alarm handler */
210 char *dot
= NULL
; /* tmp pointer */
219 * First check to see if we need to find and address for this
222 if (*uaddr
== NULL
) {
223 if ((srv
!= NULL
) && (thost
!= NULL
)) {
224 msg("both timehost and srv pointer used!");
228 srv
= get_server(thost
, &tsrv
, teps
, 32);
230 msg("unable to contruct server data.");
233 needfree
= 1; /* need to free data in endpoints */
236 nc_handle
= (void *) setnetconfig();
238 msg("unable to get netconfig info.");
240 free_eps(teps
, tsrv
.ep
.ep_len
);
245 epl
= srv
->ep
.ep_len
;
246 for (i
= 0; i
< sizeof (epcand
)/sizeof (epcand
[0]); i
++) {
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)
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") ||
273 MEP(ep
[i
], "tcp6")) {
274 epcand
[epc
++] = &(ep
[i
]);
276 nonipcand
[nonip
++] = &ep
[i
];
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
290 for (i
= 0; i
< nonip
; i
++) {
291 epcand
[epc
++] = nonipcand
[i
];
295 msg("no acceptable transport endpoints.");
297 free_eps(teps
, tsrv
.ep
.ep_len
);
301 /* Caller supplied a uaddr. Fake an endpoint. */
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
)) {
315 supplied
.proto
= (strchr(*uaddr
, ':') != 0) ?
319 supplied
.uaddr
= *uaddr
;
320 supplied
.family
= (strchr(*uaddr
, ':') != 0) ?
322 epcand
[0] = &supplied
;
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
++) {
338 freenetconfigent(nc
);
339 nc
= getnetconfigent(epcand
[i
]->proto
);
342 msg("unable to locate netconfig info for netid.");
344 free_eps(teps
, tsrv
.ep
.ep_len
);
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) {
355 "%d.%d.%d.%d.", &a1
, &a2
, &a3
, &a4
);
356 (void) sprintf(ipuaddr
, "%d.%d.%d.%d.0.111",
359 } else if (strcasecmp(nc
->nc_protofmly
, NC_INET6
) == 0) {
361 char *port
= ".0.111";
363 if (strlen(useua
) >= sizeof (ipuaddr
)) {
364 freenetconfigent(nc
);
366 free_eps(teps
, tsrv
.ep
.ep_len
);
370 (void) strcpy(ipuaddr
, useua
);
372 /* get the IPv6 address out of the uaddr */
373 if ((dot
= strrchr(ipuaddr
, '.')) != 0) {
375 if ((dot
= strrchr(ipuaddr
, '.')) != 0)
380 (len
= strlen(ipuaddr
))+strlen(port
) >=
382 freenetconfigent(nc
);
384 free_eps(teps
, tsrv
.ep
.ep_len
);
388 /* now put in 0.111 */
389 (void) strcat(ipuaddr
+ len
, port
);
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.
402 clnt
= __nis_clnt_create(RPC_ANYFD
, nc
, useua
, 0, 0, RPCBPROG
,
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
)
423 if (status
== RPC_SUCCESS
) {
425 } else if (clnt
== 0) {
426 msg("unable to create client handle to rpcbind.");
427 freenetconfigent(nc
);
429 free_eps(teps
, tsrv
.ep
.ep_len
);
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.
450 freenetconfigent(nc
);
451 nc
= getnetconfigent(epcand
[i
]->proto
);
454 msg("no netconfig info for netid.");
456 free_eps(teps
, tsrv
.ep
.ep_len
);
460 useua
= epcand
[i
]->uaddr
;
461 useid
= epcand
[i
]->proto
;
463 if (strcasecmp(nc
->nc_protofmly
, NC_INET
) == 0) {
465 "%d.%d.%d.%d.", &a1
, &a2
, &a3
, &a4
);
466 (void) sprintf(ut
, "%d.%d.%d.%d.0.37",
468 } else if (strcasecmp(nc
->nc_protofmly
, NC_INET6
) ==
471 char *port
= ".0.37";
473 if (strlen(useua
) >= sizeof (ut
)) {
477 (void) strcpy(ut
, useua
);
479 /* get the IPv6 address out of the uaddr */
480 if ((dot
= strrchr(ut
, '.')) != 0) {
482 if ((dot
= strrchr(ut
, '.')) != 0)
490 if ((len
= strlen(ut
))+strlen(port
) >=
495 (void) strcat(ut
+ len
, port
);
499 addr
= uaddr2taddr(nc
, ut
);
501 msg("timeservice uaddr to taddr failed.");
505 rtime_fd
= t_open(nc
->nc_device
, O_RDWR
, NULL
);
506 if (rtime_fd
== -1) {
507 msg("unable to open fd to network.");
511 if (t_bind(rtime_fd
, NULL
, NULL
) < 0) {
512 msg("unable to bind an endpoint to fd.");
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
;
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
;
530 tu_data
.opt
.maxlen
= 0;
531 if (t_sndudata(rtime_fd
, &tu_data
) == -1) {
532 msg("udp : t_sndudata failed.");
537 POLLIN
| POLLPRI
| POLLRDNORM
| POLLRDBAND
;
540 res
= poll(&pfd
, 1, 10000);
542 if ((res
<= 0) || (pfd
.revents
& POLLNVAL
))
544 if (t_rcvudata(rtime_fd
, &tu_data
, &flag
) <
546 msg("t_rvcdata failed on udp trpt.");
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
,
559 saw_alarm
= 0; /* global tracking the alarm */
560 (void) alarm(20); /* only wait 20 seconds */
561 if (t_connect(rtime_fd
, &sndcall
, NULL
) ==
563 msg("connect tcp endpoint failedd.");
567 msg("alarm caught it; unreachable.");
570 if (t_rcv(rtime_fd
, (char *)&thetime
,
571 (uint_t
)sizeof (thetime
), &flag
) !=
572 (uint_t
)sizeof (thetime
)) {
575 msg("timed out TCP call.");
578 msg("wrong size results");
585 thetime
= ntohl(thetime
);
586 /* adjust to UNIX time */
587 thetime
= thetime
- TOFFSET
;
595 * clean up our allocated data structures.
598 netdir_free((char *)(addr
), ND_ADDR
);
601 (void) t_close(rtime_fd
);
607 freenetconfigent(nc
);
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
620 *netid
= strdup(useid
);
622 msg("__rpc_get_time_offset: strdup failed.");
624 free_eps(teps
, tsrv
.ep
.ep_len
);
628 *uaddr
= strdup(useua
);
630 msg("__rpc_get_time_offset: strdup failed.");
633 free_eps(teps
, tsrv
.ep
.ep_len
);
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
:
644 td
->tv_sec
= (thetime
< tv
.tv_sec
) ? - delta
: delta
;
648 msg("unable to get the server's time.");
652 free_eps(teps
, tsrv
.ep
.ep_len
);