4 * This module contains the private function __rpc_get_time_offset()
5 * which will return the difference in seconds between the local system's
6 * notion of time and a remote server's notion of time. This must be
7 * possible without calling any functions that may invoke the name
8 * service. (netdir_getbyxxx, getXbyY, etc). The function is used in the
9 * synchronize call of the authdes code to synchronize clocks between
10 * NIS+ clients and their servers.
12 * Note to minimize the amount of duplicate code, portions of the
13 * synchronize() function were folded into this code, and the synchronize
14 * call becomes simply a wrapper around this function. Further, if this
15 * function is called with a timehost it *DOES* recurse to the name
16 * server so don't use it in that mode if you are doing name service code.
18 * Copyright (c) 1992 Sun Microsystems Inc.
19 * All rights reserved.
22 * When called a client handle to a RPCBIND process is created
23 * and destroyed. Two strings "netid" and "uaddr" are malloc'd
24 * and returned. The SIGALRM processing is modified only if
25 * needed to deal with TCP connections.
28 //#include <sys/cdefs.h>
35 //#include <sys/signal.h>
37 //#include <sys/socket.h>
38 //#include <netinet/in.h>
39 //#include <arpa/inet.h>
42 #include <rpc/rpc_com.h>
43 #include <rpc/rpcb_prot.h>
44 //#include <clnt_soc.h>
45 //#include <sys/select.h>
47 #include <rpcsvc/nis.h>
51 #define msg(x) printf("ERROR: %s\n", x)
52 /* #define msg(x) syslog(LOG_ERR, "%s", x) */
57 static int saw_alarm
= 0;
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
73 #define NYEARS (1970 - 1900)
74 #define TOFFSET ((u_long)60*60*24*(365*NYEARS + (NYEARS/4)))
78 * Stolen from rpc.nisd:
79 * Turn a 'universal address' into a struct sockaddr_in.
82 static int uaddr_to_sockaddr(uaddr
, sin
)
87 struct sockaddr_in
*sin
;
89 unsigned char p_bytes
[2];
93 i
= sscanf(uaddr
, "%lu.%lu.%lu.%lu.%lu.%lu", &a
[0], &a
[1], &a
[2],
99 for (i
= 0; i
< 4; i
++)
100 sin
->sin_addr
.s_addr
|= (a
[i
] & 0x000000FF) << (8 * i
);
102 p_bytes
[0] = (unsigned char)a
[4] & 0x000000FF;
103 p_bytes
[1] = (unsigned char)a
[5] & 0x000000FF;
105 sin
->sin_family
= AF_INET
; /* always */
106 bcopy((char *)&p_bytes
, (char *)&sin
->sin_port
, 2);
114 * Free the strings that were strduped into the eps structure.
123 for (i
= 0; i
< num
; i
++) {
134 * This function constructs a nis_server structure description for the
135 * indicated hostname.
137 * NOTE: There is a chance we may end up recursing here due to the
138 * fact that gethostbyname() could do an NIS search. Ideally, the
139 * NIS+ server will call __rpc_get_time_offset() with the nis_server
140 * structure already populated.
143 get_server(sin
, host
, srv
, eps
, maxep
)
144 struct sockaddr_in
*sin
;
145 char *host
; /* name of the time host */
146 nis_server
*srv
; /* nis_server struct to use. */
147 endpoint eps
[]; /* array of endpoints */
148 int maxep
; /* max array size */
153 struct hostent dummy
;
156 if (host
== NULL
&& sin
== NULL
)
160 he
= gethostbyname(host
);
165 ptr
[0] = (char *)&sin
->sin_addr
.s_addr
;
167 dummy
.h_addr_list
= ptr
;
171 * This is lame. We go around once for TCP, then again
174 for (i
= 0; (he
->h_addr_list
[i
] != NULL
) && (num_ep
< maxep
);
178 a
= (struct in_addr
*)he
->h_addr_list
[i
];
179 snprintf(hname
, sizeof(hname
), "%s.0.111", inet_ntoa(*a
));
180 eps
[num_ep
].uaddr
= strdup(hname
);
181 eps
[num_ep
].family
= strdup("inet");
182 eps
[num_ep
].proto
= strdup("tcp");
185 for (i
= 0; (he
->h_addr_list
[i
] != NULL
) && (num_ep
< maxep
);
189 a
= (struct in_addr
*)he
->h_addr_list
[i
];
190 snprintf(hname
, sizeof(hname
), "%s.0.111", inet_ntoa(*a
));
191 eps
[num_ep
].uaddr
= strdup(hname
);
192 eps
[num_ep
].family
= strdup("inet");
193 eps
[num_ep
].proto
= strdup("udp");
196 srv
->name
= (nis_name
) host
;
197 srv
->ep
.ep_len
= num_ep
;
198 srv
->ep
.ep_val
= eps
;
199 srv
->key_type
= NIS_PK_NONE
;
200 srv
->pkey
.n_bytes
= NULL
;
206 * __rpc_get_time_offset()
208 * This function uses a nis_server structure to contact the a remote
209 * machine (as named in that structure) and returns the offset in time
210 * between that machine and this one. This offset is returned in seconds
211 * and may be positive or negative.
213 * The first time through, a lot of fiddling is done with the netconfig
214 * stuff to find a suitable transport. The function is very aggressive
215 * about choosing UDP or at worst TCP if it can. This is because
216 * those transports support both the RCPBIND call and the internet
219 * Once through, *uaddr is set to the universal address of
220 * the machine and *netid is set to the local netid for the transport
221 * that uaddr goes with. On the second call, the netconfig stuff
222 * is skipped and the uaddr/netid pair are used to fetch the netconfig
223 * structure and to then contact the machine for the time.
225 * td = "server" - "client"
228 __rpc_get_time_offset(td
, srv
, thost
, uaddr
, netid
)
229 struct timeval
*td
; /* Time difference */
230 nis_server
*srv
; /* NIS Server description */
231 char *thost
; /* if no server, this is the timehost */
232 char **uaddr
; /* known universal address */
233 struct sockaddr_in
*netid
; /* known network identifier */
235 CLIENT
*clnt
; /* Client handle */
236 endpoint
*ep
, /* useful endpoints */
237 *useep
= NULL
; /* endpoint of xp */
238 char *useua
= NULL
; /* uaddr of selected xp */
239 int epl
, i
; /* counters */
240 enum clnt_stat status
; /* result of clnt_call */
246 int udp_ep
= -1, tcp_ep
= -1;
248 char ut
[64], ipuaddr
[64];
252 void (*oldsig
)() = NULL
; /* old alarm handler */
254 struct sockaddr_in sin
;
255 SOCKET s
= RPC_ANYSOCK
;
263 * First check to see if we need to find and address for this
266 if (*uaddr
== NULL
) {
267 if ((srv
!= NULL
) && (thost
!= NULL
)) {
268 msg("both timehost and srv pointer used!");
272 srv
= get_server(netid
, thost
, &tsrv
, teps
, 32);
274 msg("unable to contruct server data.");
277 needfree
= 1; /* need to free data in endpoints */
281 epl
= srv
->ep
.ep_len
;
283 /* Identify the TCP and UDP endpoints */
285 (i
< epl
) && ((udp_ep
== -1) || (tcp_ep
== -1)); i
++) {
286 if (strcasecmp(ep
[i
].proto
, "udp") == 0)
288 if (strcasecmp(ep
[i
].proto
, "tcp") == 0)
292 /* Check to see if it is UDP or TCP */
295 useua
= ep
[tcp_ep
].uaddr
;
297 } else if (udp_ep
> -1) {
299 useua
= ep
[udp_ep
].uaddr
;
304 msg("no acceptable transport endpoints.");
306 free_eps(teps
, tsrv
.ep
.ep_len
);
312 * Create a sockaddr from the uaddr.
317 /* Fixup test for NIS+ */
318 sscanf(useua
, "%d.%d.%d.%d.", &a1
, &a2
, &a3
, &a4
);
319 sprintf(ipuaddr
, "%d.%d.%d.%d.0.111", a1
, a2
, a3
, a4
);
322 bzero((char *)&sin
, sizeof(sin
));
323 if (uaddr_to_sockaddr(useua
, &sin
)) {
324 msg("unable to translate uaddr to sockaddr.");
326 free_eps(teps
, tsrv
.ep
.ep_len
);
331 * Create the client handle to rpcbind. Note we always try
332 * version 3 since that is the earliest version that supports
333 * the RPCB_GETTIME call. Also it is the version that comes
334 * standard with SVR4. Since most everyone supports TCP/IP
335 * we could consider trying the rtime call first.
337 clnt
= clnttcp_create(&sin
, RPCBPROG
, RPCBVERS
, &s
, 0, 0);
339 msg("unable to create client handle to rpcbind.");
341 free_eps(teps
, tsrv
.ep
.ep_len
);
348 status
= clnt_call(clnt
, RPCBPROC_GETTIME
, (xdrproc_t
)xdr_void
, NULL
,
349 (xdrproc_t
)xdr_u_long
, &thetime
, tv
);
351 * The only error we check for is anything but success. In
352 * fact we could have seen PROGMISMATCH if talking to a 4.1
353 * machine (pmap v2) or TIMEDOUT if the net was busy.
355 if (status
== RPC_SUCCESS
)
360 /* Blow away possible stale CLNT handle. */
367 * Convert PMAP address into timeservice address
368 * We take advantage of the fact that we "know" what
369 * the universal address looks like for inet transports.
371 * We also know that the internet timeservice is always
372 * listening on port 37.
374 sscanf(useua
, "%d.%d.%d.%d.", &a1
, &a2
, &a3
, &a4
);
375 sprintf(ut
, "%d.%d.%d.%d.0.37", a1
, a2
, a3
, a4
);
377 if (uaddr_to_sockaddr(ut
, &sin
)) {
378 msg("cannot convert timeservice uaddr to sockaddr.");
382 s
= socket(AF_INET
, type
, 0);
383 if (s
== INVALID_SOCKET
) {
384 msg("unable to open fd to network.");
389 * Now depending on whether or not we're talking to
390 * UDP we set a timeout or not.
392 if (type
== SOCK_DGRAM
) {
393 struct timeval timeout
= { 20, 0 };
394 struct sockaddr_in from
;
398 if (sendto(s
, (const char *)&thetime
, sizeof(thetime
), 0,
399 (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
400 msg("udp : sendto failed.");
406 res
= select(_rpc_dtablesize(), &readfds
,
407 (fd_set
*)NULL
, (fd_set
*)NULL
, &timeout
);
408 } while (res
== SOCKET_ERROR
&& WSAGetLastError() == WSAEINTR
);
409 if (res
== SOCKET_ERROR
)
412 res
= recvfrom(s
, (char *)&thetime
, sizeof(thetime
), 0,
413 (struct sockaddr
*)&from
, &len
);
414 if (res
== SOCKET_ERROR
) {
415 msg("recvfrom failed on udp transport.");
422 oldsig
= (void (*)())signal(SIGALRM
, alarm_hndler
);
423 saw_alarm
= 0; /* global tracking the alarm */
424 alarm(20); /* only wait 20 seconds */
427 /* XXX Need Windows signal/alarm stuff here XXX */
429 res
= connect(s
, (struct sockaddr
*)&sin
, sizeof(sin
));
430 if (res
== SOCKET_ERROR
) {
431 msg("failed to connect to tcp endpoint.");
435 msg("alarm caught it, must be unreachable.");
438 // res = read(s, (char *)&thetime, sizeof(thetime));
439 res
= recv(s
, (char *)&thetime
, sizeof(thetime
), 0);
440 if (res
!= sizeof(thetime
)) {
442 msg("timed out TCP call.");
444 msg("wrong size of results returned");
450 save
= WSAGetLastError();
451 (void)closesocket(s
);
456 thetime
= ntohl(thetime
);
457 thetime
= thetime
- TOFFSET
; /* adjust to UNIX time */
462 gettimeofday(&tv
, 0);
466 * clean up our allocated data structures.
469 if (s
!= RPC_ANYSOCK
)
470 (void)closesocket(s
);
476 /* XXX Need Windows signal/alarm stuff here XXX */
478 alarm(0); /* reset that alarm if its outstanding */
480 signal(SIGALRM
, oldsig
);
485 * note, don't free uaddr strings until after we've made a
490 *uaddr
= strdup(useua
);
492 /* Round to the nearest second */
493 tv
.tv_sec
+= (tv
.tv_sec
> 500000) ? 1 : 0;
494 delta
= (thetime
> tv
.tv_sec
) ? thetime
- tv
.tv_sec
:
496 td
->tv_sec
= (thetime
< tv
.tv_sec
) ? - delta
: delta
;
499 msg("unable to get the server's time.");
503 free_eps(teps
, tsrv
.ep
.ep_len
);