xref: /freebsd-src/lib/libc/rpc/auth_time.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
1e8636dfdSBill Paul /*
2e8636dfdSBill Paul  *	auth_time.c
3e8636dfdSBill Paul  *
4e8636dfdSBill Paul  * This module contains the private function __rpc_get_time_offset()
5e8636dfdSBill Paul  * which will return the difference in seconds between the local system's
6e8636dfdSBill Paul  * notion of time and a remote server's notion of time. This must be
7e8636dfdSBill Paul  * possible without calling any functions that may invoke the name
8e8636dfdSBill Paul  * service. (netdir_getbyxxx, getXbyY, etc). The function is used in the
9e8636dfdSBill Paul  * synchronize call of the authdes code to synchronize clocks between
10e8636dfdSBill Paul  * NIS+ clients and their servers.
11e8636dfdSBill Paul  *
12e8636dfdSBill Paul  * Note to minimize the amount of duplicate code, portions of the
13e8636dfdSBill Paul  * synchronize() function were folded into this code, and the synchronize
14e8636dfdSBill Paul  * call becomes simply a wrapper around this function. Further, if this
15e8636dfdSBill Paul  * function is called with a timehost it *DOES* recurse to the name
16e8636dfdSBill Paul  * server so don't use it in that mode if you are doing name service code.
17e8636dfdSBill Paul  *
18e8636dfdSBill Paul  *	Copyright (c) 1992 Sun Microsystems Inc.
19e8636dfdSBill Paul  *	All rights reserved.
20e8636dfdSBill Paul  *
21e8636dfdSBill Paul  * Side effects :
22e8636dfdSBill Paul  *	When called a client handle to a RPCBIND process is created
23e8636dfdSBill Paul  *	and destroyed. Two strings "netid" and "uaddr" are malloc'd
24e8636dfdSBill Paul  *	and returned. The SIGALRM processing is modified only if
25e8636dfdSBill Paul  *	needed to deal with TCP connections.
26e8636dfdSBill Paul  */
27d3d20c82SDavid E. O'Brien 
28d201fe46SDaniel Eischen #include "namespace.h"
29e8636dfdSBill Paul #include <stdio.h>
30e8636dfdSBill Paul #include <syslog.h>
31e8636dfdSBill Paul #include <string.h>
32e8636dfdSBill Paul #include <stdlib.h>
33e8636dfdSBill Paul #include <unistd.h>
34e8636dfdSBill Paul #include <netdb.h>
35e8636dfdSBill Paul #include <sys/signal.h>
36e8636dfdSBill Paul #include <sys/errno.h>
37e8636dfdSBill Paul #include <sys/socket.h>
38e8636dfdSBill Paul #include <netinet/in.h>
39e8636dfdSBill Paul #include <arpa/inet.h>
40e8636dfdSBill Paul #include <rpc/rpc.h>
41e8636dfdSBill Paul #include <rpc/rpc_com.h>
428360efbdSAlfred Perlstein #include <rpc/rpcb_prot.h>
43e8636dfdSBill Paul #undef NIS
44e8636dfdSBill Paul #include <rpcsvc/nis.h>
45d201fe46SDaniel Eischen #include "un-namespace.h"
46e8636dfdSBill Paul 
47a587d2f7SJacques Vidrine extern int _rpc_dtablesize( void );
48a587d2f7SJacques Vidrine 
49e8636dfdSBill Paul #ifdef TESTING
50e8636dfdSBill Paul #define	msg(x)	printf("ERROR: %s\n", x)
51e8636dfdSBill Paul /* #define msg(x) syslog(LOG_ERR, "%s", x) */
52e8636dfdSBill Paul #else
53e8636dfdSBill Paul #define	msg(x)
54e8636dfdSBill Paul #endif
55e8636dfdSBill Paul 
56e8636dfdSBill Paul static int saw_alarm = 0;
57e8636dfdSBill Paul 
58e8636dfdSBill Paul static void
alarm_hndler(int s)5968895e38SCraig Rodrigues alarm_hndler(int s)
60e8636dfdSBill Paul {
61e8636dfdSBill Paul 	saw_alarm = 1;
62e8636dfdSBill Paul 	return;
63e8636dfdSBill Paul }
64e8636dfdSBill Paul 
65e8636dfdSBill Paul /*
66e8636dfdSBill Paul  * The internet time server defines the epoch to be Jan 1, 1900
67e8636dfdSBill Paul  * whereas UNIX defines it to be Jan 1, 1970. To adjust the result
68e8636dfdSBill Paul  * from internet time-service time, into UNIX time we subtract the
69e8636dfdSBill Paul  * following offset :
70e8636dfdSBill Paul  */
71e8636dfdSBill Paul #define	NYEARS	(1970 - 1900)
72e8636dfdSBill Paul #define	TOFFSET ((u_long)60*60*24*(365*NYEARS + (NYEARS/4)))
73e8636dfdSBill Paul 
74e8636dfdSBill Paul 
75e8636dfdSBill Paul /*
76e8636dfdSBill Paul  * Stolen from rpc.nisd:
77e8636dfdSBill Paul  * Turn a 'universal address' into a struct sockaddr_in.
78e8636dfdSBill Paul  * Bletch.
79e8636dfdSBill Paul  */
uaddr_to_sockaddr(char * uaddr,struct sockaddr_in * sin)8068895e38SCraig Rodrigues static int uaddr_to_sockaddr(char *uaddr, struct sockaddr_in *sin)
81e8636dfdSBill Paul {
82e8636dfdSBill Paul 	unsigned char		p_bytes[2];
83e8636dfdSBill Paul 	int			i;
84e8636dfdSBill Paul 	unsigned long		a[6];
85e8636dfdSBill Paul 
86e8636dfdSBill Paul 	i = sscanf(uaddr, "%lu.%lu.%lu.%lu.%lu.%lu", &a[0], &a[1], &a[2],
87e8636dfdSBill Paul 						&a[3], &a[4], &a[5]);
88e8636dfdSBill Paul 
89e8636dfdSBill Paul 	if (i < 6)
90e8636dfdSBill Paul 		return(1);
91e8636dfdSBill Paul 
92e8636dfdSBill Paul 	for (i = 0; i < 4; i++)
93e8636dfdSBill Paul 		sin->sin_addr.s_addr |= (a[i] & 0x000000FF) << (8 * i);
94e8636dfdSBill Paul 
95e8636dfdSBill Paul 	p_bytes[0] = (unsigned char)a[4] & 0x000000FF;
96e8636dfdSBill Paul 	p_bytes[1] = (unsigned char)a[5] & 0x000000FF;
97e8636dfdSBill Paul 
98e8636dfdSBill Paul 	sin->sin_family = AF_INET; /* always */
99e8636dfdSBill Paul 	bcopy((char *)&p_bytes, (char *)&sin->sin_port, 2);
100e8636dfdSBill Paul 
101e8636dfdSBill Paul 	return (0);
102e8636dfdSBill Paul }
103e8636dfdSBill Paul 
104e8636dfdSBill Paul /*
105e8636dfdSBill Paul  * free_eps()
106e8636dfdSBill Paul  *
107e8636dfdSBill Paul  * Free the strings that were strduped into the eps structure.
108e8636dfdSBill Paul  */
109e8636dfdSBill Paul static void
free_eps(endpoint eps[],int num)11068895e38SCraig Rodrigues free_eps(endpoint eps[], int num)
111e8636dfdSBill Paul {
112e8636dfdSBill Paul 	int		i;
113e8636dfdSBill Paul 
114e8636dfdSBill Paul 	for (i = 0; i < num; i++) {
115e8636dfdSBill Paul 		free(eps[i].uaddr);
116e8636dfdSBill Paul 		free(eps[i].proto);
117e8636dfdSBill Paul 		free(eps[i].family);
118e8636dfdSBill Paul 	}
119e8636dfdSBill Paul 	return;
120e8636dfdSBill Paul }
121e8636dfdSBill Paul 
122e8636dfdSBill Paul /*
123e8636dfdSBill Paul  * get_server()
124e8636dfdSBill Paul  *
125e8636dfdSBill Paul  * This function constructs a nis_server structure description for the
126e8636dfdSBill Paul  * indicated hostname.
127e8636dfdSBill Paul  *
128e8636dfdSBill Paul  * NOTE: There is a chance we may end up recursing here due to the
129e8636dfdSBill Paul  * fact that gethostbyname() could do an NIS search. Ideally, the
130e8636dfdSBill Paul  * NIS+ server will call __rpc_get_time_offset() with the nis_server
131e8636dfdSBill Paul  * structure already populated.
13268895e38SCraig Rodrigues  *
13368895e38SCraig Rodrigues  * host  - name of the time host
13468895e38SCraig Rodrigues  * srv   - nis_server struct to use.
13568895e38SCraig Rodrigues  * eps[] - array of endpoints
13668895e38SCraig Rodrigues  * maxep - max array size
137e8636dfdSBill Paul  */
138e8636dfdSBill Paul static nis_server *
get_server(struct sockaddr_in * sin,char * host,nis_server * srv,endpoint eps[],int maxep)13968895e38SCraig Rodrigues get_server(struct sockaddr_in *sin, char *host, nis_server *srv,
14068895e38SCraig Rodrigues     endpoint eps[], int maxep)
141e8636dfdSBill Paul {
142e8636dfdSBill Paul 	char			hname[256];
143e8636dfdSBill Paul 	int			num_ep = 0, i;
144e8636dfdSBill Paul 	struct hostent		*he;
145e8636dfdSBill Paul 	struct hostent		dummy;
146e8636dfdSBill Paul 	char			*ptr[2];
147330e445cSMatteo Riondato 	endpoint		*ep;
148e8636dfdSBill Paul 
149e8636dfdSBill Paul 	if (host == NULL && sin == NULL)
150e8636dfdSBill Paul 		return (NULL);
151e8636dfdSBill Paul 
152e8636dfdSBill Paul 	if (sin == NULL) {
153e8636dfdSBill Paul 		he = gethostbyname(host);
154e8636dfdSBill Paul 		if (he == NULL)
155e8636dfdSBill Paul 			return(NULL);
156e8636dfdSBill Paul 	} else {
157e8636dfdSBill Paul 		he = &dummy;
158e8636dfdSBill Paul 		ptr[0] = (char *)&sin->sin_addr.s_addr;
159e8636dfdSBill Paul 		ptr[1] = NULL;
160e8636dfdSBill Paul 		dummy.h_addr_list = ptr;
161e8636dfdSBill Paul 	}
162e8636dfdSBill Paul 
163e8636dfdSBill Paul 	/*
164e8636dfdSBill Paul 	 * This is lame. We go around once for TCP, then again
165e8636dfdSBill Paul 	 * for UDP.
166e8636dfdSBill Paul 	 */
167330e445cSMatteo Riondato 	for (i = 0, ep = eps; (he->h_addr_list[i] != NULL) && (num_ep < maxep);
168330e445cSMatteo Riondato 	    i++, ep++, num_ep++) {
169e8636dfdSBill Paul 		struct in_addr *a;
170e8636dfdSBill Paul 
171e8636dfdSBill Paul 		a = (struct in_addr *)he->h_addr_list[i];
172e8636dfdSBill Paul 		snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a));
173330e445cSMatteo Riondato 		ep->uaddr = strdup(hname);
174330e445cSMatteo Riondato 		ep->family = strdup("inet");
175330e445cSMatteo Riondato 		ep->proto =  strdup("tcp");
176330e445cSMatteo Riondato 		if (ep->uaddr == NULL || ep->family == NULL || ep->proto == NULL) {
177330e445cSMatteo Riondato 			free_eps(eps, num_ep + 1);
178330e445cSMatteo Riondato 			return (NULL);
179330e445cSMatteo Riondato 		}
180e8636dfdSBill Paul 	}
181e8636dfdSBill Paul 
182e8636dfdSBill Paul 	for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep);
183330e445cSMatteo Riondato 	    i++, ep++, num_ep++) {
184e8636dfdSBill Paul 		struct in_addr *a;
185e8636dfdSBill Paul 
186e8636dfdSBill Paul 		a = (struct in_addr *)he->h_addr_list[i];
187e8636dfdSBill Paul 		snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a));
188330e445cSMatteo Riondato 		ep->uaddr = strdup(hname);
189330e445cSMatteo Riondato 		ep->family = strdup("inet");
190330e445cSMatteo Riondato 		ep->proto =  strdup("udp");
191330e445cSMatteo Riondato 		if (ep->uaddr == NULL || ep->family == NULL || ep->proto == NULL) {
192330e445cSMatteo Riondato 			free_eps(eps, num_ep + 1);
193330e445cSMatteo Riondato 			return (NULL);
194330e445cSMatteo Riondato 		}
195e8636dfdSBill Paul 	}
196e8636dfdSBill Paul 
197e8636dfdSBill Paul 	srv->name = (nis_name) host;
198e8636dfdSBill Paul 	srv->ep.ep_len = num_ep;
199e8636dfdSBill Paul 	srv->ep.ep_val = eps;
200e8636dfdSBill Paul 	srv->key_type = NIS_PK_NONE;
201e8636dfdSBill Paul 	srv->pkey.n_bytes = NULL;
202e8636dfdSBill Paul 	srv->pkey.n_len = 0;
203e8636dfdSBill Paul 	return (srv);
204e8636dfdSBill Paul }
205e8636dfdSBill Paul 
206e8636dfdSBill Paul /*
207e8636dfdSBill Paul  * __rpc_get_time_offset()
208e8636dfdSBill Paul  *
209e8636dfdSBill Paul  * This function uses a nis_server structure to contact the a remote
210e8636dfdSBill Paul  * machine (as named in that structure) and returns the offset in time
211e8636dfdSBill Paul  * between that machine and this one. This offset is returned in seconds
212e8636dfdSBill Paul  * and may be positive or negative.
213e8636dfdSBill Paul  *
214e8636dfdSBill Paul  * The first time through, a lot of fiddling is done with the netconfig
215e8636dfdSBill Paul  * stuff to find a suitable transport. The function is very aggressive
216e8636dfdSBill Paul  * about choosing UDP or at worst TCP if it can. This is because
217e8636dfdSBill Paul  * those transports support both the RCPBIND call and the internet
218e8636dfdSBill Paul  * time service.
219e8636dfdSBill Paul  *
220e8636dfdSBill Paul  * Once through, *uaddr is set to the universal address of
221e8636dfdSBill Paul  * the machine and *netid is set to the local netid for the transport
222e8636dfdSBill Paul  * that uaddr goes with. On the second call, the netconfig stuff
223e8636dfdSBill Paul  * is skipped and the uaddr/netid pair are used to fetch the netconfig
224e8636dfdSBill Paul  * structure and to then contact the machine for the time.
225e8636dfdSBill Paul  *
226e8636dfdSBill Paul  * td = "server" - "client"
22768895e38SCraig Rodrigues  *
22868895e38SCraig Rodrigues  * td    - Time difference
22968895e38SCraig Rodrigues  * srv   - NIS Server description
23068895e38SCraig Rodrigues  * thost - if no server, this is the timehost
23168895e38SCraig Rodrigues  * uaddr - known universal address
23268895e38SCraig Rodrigues  * netid - known network identifier
233e8636dfdSBill Paul  */
234e8636dfdSBill Paul int
__rpc_get_time_offset(struct timeval * td,nis_server * srv,char * thost,char ** uaddr,struct sockaddr_in * netid)23568895e38SCraig Rodrigues __rpc_get_time_offset(struct timeval *td, nis_server *srv, char *thost,
23668895e38SCraig Rodrigues     char **uaddr, struct sockaddr_in *netid)
237e8636dfdSBill Paul {
238e8636dfdSBill Paul 	CLIENT			*clnt; 		/* Client handle 	*/
239e8636dfdSBill Paul 	endpoint		*ep,		/* useful endpoints	*/
240e8636dfdSBill Paul 				*useep = NULL;	/* endpoint of xp	*/
241e8636dfdSBill Paul 	char			*useua = NULL;	/* uaddr of selected xp	*/
242e8636dfdSBill Paul 	int			epl, i;		/* counters		*/
243e8636dfdSBill Paul 	enum clnt_stat		status;		/* result of clnt_call	*/
244e8636dfdSBill Paul 	u_long			thetime, delta;
245e8636dfdSBill Paul 	int			needfree = 0;
246e8636dfdSBill Paul 	struct timeval		tv;
247e8636dfdSBill Paul 	int			time_valid;
248e8636dfdSBill Paul 	int			udp_ep = -1, tcp_ep = -1;
249e8636dfdSBill Paul 	int			a1, a2, a3, a4;
250e8636dfdSBill Paul 	char			ut[64], ipuaddr[64];
251e8636dfdSBill Paul 	endpoint		teps[32];
252e8636dfdSBill Paul 	nis_server		tsrv;
253*929d5af5SCraig Rodrigues 	void			(*oldsig)(int) = NULL; /* old alarm handler */
254e8636dfdSBill Paul 	struct sockaddr_in	sin;
255720138bbSStefan Farfeleder 	socklen_t		len;
256720138bbSStefan Farfeleder 	int			s = RPC_ANYSOCK;
257e8636dfdSBill Paul 	int			type = 0;
258e8636dfdSBill Paul 
259e8636dfdSBill Paul 	td->tv_sec = 0;
260e8636dfdSBill Paul 	td->tv_usec = 0;
261e8636dfdSBill Paul 
262e8636dfdSBill Paul 	/*
263e8636dfdSBill Paul 	 * First check to see if we need to find and address for this
264e8636dfdSBill Paul 	 * server.
265e8636dfdSBill Paul 	 */
266e8636dfdSBill Paul 	if (*uaddr == NULL) {
267e8636dfdSBill Paul 		if ((srv != NULL) && (thost != NULL)) {
268e8636dfdSBill Paul 			msg("both timehost and srv pointer used!");
269e8636dfdSBill Paul 			return (0);
270e8636dfdSBill Paul 		}
271e8636dfdSBill Paul 		if (! srv) {
272e8636dfdSBill Paul 			srv = get_server(netid, thost, &tsrv, teps, 32);
273e8636dfdSBill Paul 			if (srv == NULL) {
274e8636dfdSBill Paul 				msg("unable to contruct server data.");
275e8636dfdSBill Paul 				return (0);
276e8636dfdSBill Paul 			}
277e8636dfdSBill Paul 			needfree = 1;	/* need to free data in endpoints */
278e8636dfdSBill Paul 		}
279e8636dfdSBill Paul 
280e8636dfdSBill Paul 		ep = srv->ep.ep_val;
281e8636dfdSBill Paul 		epl = srv->ep.ep_len;
282e8636dfdSBill Paul 
283e8636dfdSBill Paul 		/* Identify the TCP and UDP endpoints */
284e8636dfdSBill Paul 		for (i = 0;
285e8636dfdSBill Paul 			(i < epl) && ((udp_ep == -1) || (tcp_ep == -1)); i++) {
286e8636dfdSBill Paul 			if (strcasecmp(ep[i].proto, "udp") == 0)
287e8636dfdSBill Paul 				udp_ep = i;
288e8636dfdSBill Paul 			if (strcasecmp(ep[i].proto, "tcp") == 0)
289e8636dfdSBill Paul 				tcp_ep = i;
290e8636dfdSBill Paul 		}
291e8636dfdSBill Paul 
292e8636dfdSBill Paul 		/* Check to see if it is UDP or TCP */
293e8636dfdSBill Paul 		if (tcp_ep > -1) {
294e8636dfdSBill Paul 			useep = &ep[tcp_ep];
295e8636dfdSBill Paul 			useua = ep[tcp_ep].uaddr;
296e8636dfdSBill Paul 			type = SOCK_STREAM;
297e8636dfdSBill Paul 		} else if (udp_ep > -1) {
298e8636dfdSBill Paul 			useep = &ep[udp_ep];
299e8636dfdSBill Paul 			useua = ep[udp_ep].uaddr;
300e8636dfdSBill Paul 			type = SOCK_DGRAM;
301e8636dfdSBill Paul 		}
302e8636dfdSBill Paul 
303e8636dfdSBill Paul 		if (useep == NULL) {
304e8636dfdSBill Paul 			msg("no acceptable transport endpoints.");
305e8636dfdSBill Paul 			if (needfree)
306e8636dfdSBill Paul 				free_eps(teps, tsrv.ep.ep_len);
307e8636dfdSBill Paul 			return (0);
308e8636dfdSBill Paul 		}
309e8636dfdSBill Paul 	}
310e8636dfdSBill Paul 
311e8636dfdSBill Paul 	/*
312e8636dfdSBill Paul 	 * Create a sockaddr from the uaddr.
313e8636dfdSBill Paul 	 */
314e8636dfdSBill Paul 	if (*uaddr != NULL)
315e8636dfdSBill Paul 		useua = *uaddr;
316e8636dfdSBill Paul 
317e8636dfdSBill Paul 	/* Fixup test for NIS+ */
318e8636dfdSBill Paul 	sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4);
319e8636dfdSBill Paul 	sprintf(ipuaddr, "%d.%d.%d.%d.0.111", a1, a2, a3, a4);
320e8636dfdSBill Paul 	useua = &ipuaddr[0];
321e8636dfdSBill Paul 
322c88fdb1dSBill Paul 	bzero((char *)&sin, sizeof(sin));
323e8636dfdSBill Paul 	if (uaddr_to_sockaddr(useua, &sin)) {
324e8636dfdSBill Paul 		msg("unable to translate uaddr to sockaddr.");
325e8636dfdSBill Paul 		if (needfree)
326e8636dfdSBill Paul 			free_eps(teps, tsrv.ep.ep_len);
327e8636dfdSBill Paul 		return (0);
328e8636dfdSBill Paul 	}
329e8636dfdSBill Paul 
330e8636dfdSBill Paul 	/*
331e8636dfdSBill Paul 	 * Create the client handle to rpcbind. Note we always try
332e8636dfdSBill Paul 	 * version 3 since that is the earliest version that supports
333e8636dfdSBill Paul 	 * the RPCB_GETTIME call. Also it is the version that comes
334e8636dfdSBill Paul 	 * standard with SVR4. Since most everyone supports TCP/IP
335e8636dfdSBill Paul 	 * we could consider trying the rtime call first.
336e8636dfdSBill Paul 	 */
337e8636dfdSBill Paul 	clnt = clnttcp_create(&sin, RPCBPROG, RPCBVERS, &s, 0, 0);
338e8636dfdSBill Paul 	if (clnt == NULL) {
339e8636dfdSBill Paul 		msg("unable to create client handle to rpcbind.");
340e8636dfdSBill Paul 		if (needfree)
341e8636dfdSBill Paul 			free_eps(teps, tsrv.ep.ep_len);
342e8636dfdSBill Paul 		return (0);
343e8636dfdSBill Paul 	}
344e8636dfdSBill Paul 
345e8636dfdSBill Paul 	tv.tv_sec = 5;
346e8636dfdSBill Paul 	tv.tv_usec = 0;
347e8636dfdSBill Paul 	time_valid = 0;
348f249dbccSDag-Erling Smørgrav 	status = clnt_call(clnt, RPCBPROC_GETTIME, (xdrproc_t)xdr_void, NULL,
349f249dbccSDag-Erling Smørgrav 					(xdrproc_t)xdr_u_long, &thetime, tv);
350e8636dfdSBill Paul 	/*
351e8636dfdSBill Paul 	 * The only error we check for is anything but success. In
352e8636dfdSBill Paul 	 * fact we could have seen PROGMISMATCH if talking to a 4.1
353e8636dfdSBill Paul 	 * machine (pmap v2) or TIMEDOUT if the net was busy.
354e8636dfdSBill Paul 	 */
355e8636dfdSBill Paul 	if (status == RPC_SUCCESS)
356e8636dfdSBill Paul 		time_valid = 1;
357e8636dfdSBill Paul 	else {
358e8636dfdSBill Paul 		int save;
359e8636dfdSBill Paul 
360e8636dfdSBill Paul 		/* Blow away possible stale CLNT handle. */
361e8636dfdSBill Paul 		if (clnt != NULL) {
362e8636dfdSBill Paul 			clnt_destroy(clnt);
363e8636dfdSBill Paul 			clnt = NULL;
364e8636dfdSBill Paul 		}
365e8636dfdSBill Paul 
366e8636dfdSBill Paul 		/*
367e8636dfdSBill Paul 		 * Convert PMAP address into timeservice address
368e8636dfdSBill Paul 		 * We take advantage of the fact that we "know" what
369e8636dfdSBill Paul 		 * the universal address looks like for inet transports.
370e8636dfdSBill Paul 		 *
371e8636dfdSBill Paul 		 * We also know that the internet timeservice is always
372e8636dfdSBill Paul 		 * listening on port 37.
373e8636dfdSBill Paul 		 */
374e8636dfdSBill Paul 		sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4);
375e8636dfdSBill Paul 		sprintf(ut, "%d.%d.%d.%d.0.37", a1, a2, a3, a4);
376e8636dfdSBill Paul 
377e8636dfdSBill Paul 		if (uaddr_to_sockaddr(ut, &sin)) {
378e8636dfdSBill Paul 			msg("cannot convert timeservice uaddr to sockaddr.");
379e8636dfdSBill Paul 			goto error;
380e8636dfdSBill Paul 		}
381e8636dfdSBill Paul 
382d201fe46SDaniel Eischen 		s = _socket(AF_INET, type, 0);
383e8636dfdSBill Paul 		if (s == -1) {
384e8636dfdSBill Paul 			msg("unable to open fd to network.");
385e8636dfdSBill Paul 			goto error;
386e8636dfdSBill Paul 		}
387e8636dfdSBill Paul 
388e8636dfdSBill Paul 		/*
389e8636dfdSBill Paul 		 * Now depending on whether or not we're talking to
390e8636dfdSBill Paul 		 * UDP we set a timeout or not.
391e8636dfdSBill Paul 		 */
392e8636dfdSBill Paul 		if (type == SOCK_DGRAM) {
393e8636dfdSBill Paul 			struct timeval timeout = { 20, 0 };
394e8636dfdSBill Paul 			struct sockaddr_in from;
395e8636dfdSBill Paul 			fd_set readfds;
396e8636dfdSBill Paul 			int res;
397e8636dfdSBill Paul 
398d201fe46SDaniel Eischen 			if (_sendto(s, &thetime, sizeof(thetime), 0,
399e8636dfdSBill Paul 				(struct sockaddr *)&sin, sizeof(sin)) == -1) {
400e8636dfdSBill Paul 				msg("udp : sendto failed.");
401e8636dfdSBill Paul 				goto error;
402e8636dfdSBill Paul 			}
403e8636dfdSBill Paul 			do {
404e8636dfdSBill Paul 				FD_ZERO(&readfds);
405e8636dfdSBill Paul 				FD_SET(s, &readfds);
406d201fe46SDaniel Eischen 				res = _select(_rpc_dtablesize(), &readfds,
407e8636dfdSBill Paul 				     (fd_set *)NULL, (fd_set *)NULL, &timeout);
408e8636dfdSBill Paul 			} while (res < 0 && errno == EINTR);
409e8636dfdSBill Paul 			if (res <= 0)
410e8636dfdSBill Paul 				goto error;
411e8636dfdSBill Paul 			len = sizeof(from);
412d201fe46SDaniel Eischen 			res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0,
413e8636dfdSBill Paul 				       (struct sockaddr *)&from, &len);
414e8636dfdSBill Paul 			if (res == -1) {
415e8636dfdSBill Paul 				msg("recvfrom failed on udp transport.");
416e8636dfdSBill Paul 				goto error;
417e8636dfdSBill Paul 			}
418e8636dfdSBill Paul 			time_valid = 1;
419e8636dfdSBill Paul 		} else {
420e8636dfdSBill Paul 			int res;
421e8636dfdSBill Paul 
422*929d5af5SCraig Rodrigues 			oldsig = (void (*)(int))signal(SIGALRM, alarm_hndler);
423e8636dfdSBill Paul 			saw_alarm = 0; /* global tracking the alarm */
424e8636dfdSBill Paul 			alarm(20); /* only wait 20 seconds */
425d201fe46SDaniel Eischen 			res = _connect(s, (struct sockaddr *)&sin, sizeof(sin));
426e8636dfdSBill Paul 			if (res == -1) {
427e8636dfdSBill Paul 				msg("failed to connect to tcp endpoint.");
428e8636dfdSBill Paul 				goto error;
429e8636dfdSBill Paul 			}
430e8636dfdSBill Paul 			if (saw_alarm) {
431e8636dfdSBill Paul 				msg("alarm caught it, must be unreachable.");
432e8636dfdSBill Paul 				goto error;
433e8636dfdSBill Paul 			}
4349233c4d9SJason Evans 			res = _read(s, (char *)&thetime, sizeof(thetime));
435e8636dfdSBill Paul 			if (res != sizeof(thetime)) {
436e8636dfdSBill Paul 				if (saw_alarm)
437e8636dfdSBill Paul 					msg("timed out TCP call.");
438e8636dfdSBill Paul 				else
439e8636dfdSBill Paul 					msg("wrong size of results returned");
440e8636dfdSBill Paul 
441e8636dfdSBill Paul 				goto error;
442e8636dfdSBill Paul 			}
443e8636dfdSBill Paul 			time_valid = 1;
444e8636dfdSBill Paul 		}
445e8636dfdSBill Paul 		save = errno;
4469233c4d9SJason Evans 		(void)_close(s);
447e8636dfdSBill Paul 		errno = save;
448e8636dfdSBill Paul 		s = RPC_ANYSOCK;
449e8636dfdSBill Paul 
450e8636dfdSBill Paul 		if (time_valid) {
451e8636dfdSBill Paul 			thetime = ntohl(thetime);
452e8636dfdSBill Paul 			thetime = thetime - TOFFSET; /* adjust to UNIX time */
453e8636dfdSBill Paul 		} else
454e8636dfdSBill Paul 			thetime = 0;
455e8636dfdSBill Paul 	}
456e8636dfdSBill Paul 
457e8636dfdSBill Paul 	gettimeofday(&tv, 0);
458e8636dfdSBill Paul 
459e8636dfdSBill Paul error:
460e8636dfdSBill Paul 	/*
461e8636dfdSBill Paul 	 * clean up our allocated data structures.
462e8636dfdSBill Paul 	 */
463e8636dfdSBill Paul 
464e8636dfdSBill Paul 	if (s != RPC_ANYSOCK)
4659233c4d9SJason Evans 		(void)_close(s);
466e8636dfdSBill Paul 
467e8636dfdSBill Paul 	if (clnt != NULL)
468e8636dfdSBill Paul 		clnt_destroy(clnt);
469e8636dfdSBill Paul 
470e8636dfdSBill Paul 	alarm(0);	/* reset that alarm if its outstanding */
471e8636dfdSBill Paul 	if (oldsig) {
472e8636dfdSBill Paul 		signal(SIGALRM, oldsig);
473e8636dfdSBill Paul 	}
474e8636dfdSBill Paul 
475e8636dfdSBill Paul 	/*
476e8636dfdSBill Paul 	 * note, don't free uaddr strings until after we've made a
477e8636dfdSBill Paul 	 * copy of them.
478e8636dfdSBill Paul 	 */
479e8636dfdSBill Paul 	if (time_valid) {
480e8636dfdSBill Paul 		if (*uaddr == NULL)
481e8636dfdSBill Paul 			*uaddr = strdup(useua);
482e8636dfdSBill Paul 
483e8636dfdSBill Paul 		/* Round to the nearest second */
484e8636dfdSBill Paul 		tv.tv_sec += (tv.tv_sec > 500000) ? 1 : 0;
485e8636dfdSBill Paul 		delta = (thetime > tv.tv_sec) ? thetime - tv.tv_sec :
486e8636dfdSBill Paul 						tv.tv_sec - thetime;
487e8636dfdSBill Paul 		td->tv_sec = (thetime < tv.tv_sec) ? - delta : delta;
488e8636dfdSBill Paul 		td->tv_usec = 0;
489e8636dfdSBill Paul 	} else {
490e8636dfdSBill Paul 		msg("unable to get the server's time.");
491e8636dfdSBill Paul 	}
492e8636dfdSBill Paul 
493e8636dfdSBill Paul 	if (needfree)
494e8636dfdSBill Paul 		free_eps(teps, tsrv.ep.ep_len);
495e8636dfdSBill Paul 
496e8636dfdSBill Paul 	return (time_valid);
497e8636dfdSBill Paul }
498