17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 22*61961e0fSrobinson 237c478bd9Sstevel@tonic-gate /* 24*61961e0fSrobinson * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 25*61961e0fSrobinson * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate * 277c478bd9Sstevel@tonic-gate * auth_time.c 287c478bd9Sstevel@tonic-gate * 297c478bd9Sstevel@tonic-gate * This module contains the private function __rpc_get_time_offset() 307c478bd9Sstevel@tonic-gate * which will return the difference in seconds between the local system's 317c478bd9Sstevel@tonic-gate * notion of time and a remote server's notion of time. This must be 327c478bd9Sstevel@tonic-gate * possible without calling any functions that may invoke the name 337c478bd9Sstevel@tonic-gate * service. (netdir_getbyxxx, getXbyY, etc). The function is used in the 347c478bd9Sstevel@tonic-gate * synchronize call of the authdes code to synchronize clocks between 357c478bd9Sstevel@tonic-gate * NIS+ clients and their servers. 367c478bd9Sstevel@tonic-gate * 377c478bd9Sstevel@tonic-gate * Note to minimize the amount of duplicate code, portions of the 387c478bd9Sstevel@tonic-gate * synchronize() function were folded into this code, and the synchronize 397c478bd9Sstevel@tonic-gate * call becomes simply a wrapper around this function. Further, if this 407c478bd9Sstevel@tonic-gate * function is called with a timehost it *DOES* recurse to the name 417c478bd9Sstevel@tonic-gate * server so don't use it in that mode if you are doing name service code. 427c478bd9Sstevel@tonic-gate * 437c478bd9Sstevel@tonic-gate * Side effects : 447c478bd9Sstevel@tonic-gate * When called a client handle to a RPCBIND process is created 457c478bd9Sstevel@tonic-gate * and destroyed. Two strings "netid" and "uaddr" are malloc'd 467c478bd9Sstevel@tonic-gate * and returned. The SIGALRM processing is modified only if 477c478bd9Sstevel@tonic-gate * needed to deal with TCP connections. 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate #include <stdio.h> 537c478bd9Sstevel@tonic-gate #include <stdlib.h> 547c478bd9Sstevel@tonic-gate #include <unistd.h> 557c478bd9Sstevel@tonic-gate #include <syslog.h> 567c478bd9Sstevel@tonic-gate #include <netdir.h> 577c478bd9Sstevel@tonic-gate #include <string.h> 58*61961e0fSrobinson #include <strings.h> 597c478bd9Sstevel@tonic-gate #include <netconfig.h> 607c478bd9Sstevel@tonic-gate #include <netdb.h> 617c478bd9Sstevel@tonic-gate #include <signal.h> 627c478bd9Sstevel@tonic-gate #include <sys/errno.h> 637c478bd9Sstevel@tonic-gate #include <sys/poll.h> 647c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 657c478bd9Sstevel@tonic-gate #include <rpc/nettype.h> 667c478bd9Sstevel@tonic-gate #undef NIS 677c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h> 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate extern void __nis_netconfig2ep(struct netconfig *, endpoint *); 717c478bd9Sstevel@tonic-gate extern bool_t __nis_netconfig_matches_ep(struct netconfig *, endpoint *); 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate #ifdef TESTING 747c478bd9Sstevel@tonic-gate #define msg(x) printf("ERROR: %s\n", x) 757c478bd9Sstevel@tonic-gate /* #define msg(x) syslog(LOG_ERR, "%s", x) */ 767c478bd9Sstevel@tonic-gate #else 777c478bd9Sstevel@tonic-gate #define msg(x) 787c478bd9Sstevel@tonic-gate #endif 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate static int saw_alarm = 0; 817c478bd9Sstevel@tonic-gate 82*61961e0fSrobinson /* ARGSUSED */ 837c478bd9Sstevel@tonic-gate static void 84*61961e0fSrobinson alarm_hndler(int s) 857c478bd9Sstevel@tonic-gate { 867c478bd9Sstevel@tonic-gate saw_alarm = 1; 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * The internet time server defines the epoch to be Jan 1, 1900 917c478bd9Sstevel@tonic-gate * whereas UNIX defines it to be Jan 1, 1970. To adjust the result 927c478bd9Sstevel@tonic-gate * from internet time-service time, into UNIX time we subtract the 937c478bd9Sstevel@tonic-gate * following offset : 947c478bd9Sstevel@tonic-gate */ 957c478bd9Sstevel@tonic-gate #define NYEARS (1970 - 1900) 967c478bd9Sstevel@tonic-gate #define TOFFSET ((uint_t)60*60*24*(365*NYEARS + (NYEARS/4))) 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate /* 997c478bd9Sstevel@tonic-gate * free_eps() 1007c478bd9Sstevel@tonic-gate * 1017c478bd9Sstevel@tonic-gate * Free the strings that were strduped into the eps structure. 1027c478bd9Sstevel@tonic-gate */ 1037c478bd9Sstevel@tonic-gate static void 1047c478bd9Sstevel@tonic-gate free_eps(endpoint eps[], int num) 1057c478bd9Sstevel@tonic-gate { 1067c478bd9Sstevel@tonic-gate int i; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate for (i = 0; i < num; i++) { 1097c478bd9Sstevel@tonic-gate free(eps[i].uaddr); 1107c478bd9Sstevel@tonic-gate free(eps[i].proto); 1117c478bd9Sstevel@tonic-gate free(eps[i].family); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate /* 1167c478bd9Sstevel@tonic-gate * get_server() 1177c478bd9Sstevel@tonic-gate * 1187c478bd9Sstevel@tonic-gate * This function constructs a nis_server structure description for the 1197c478bd9Sstevel@tonic-gate * indicated hostname. 1207c478bd9Sstevel@tonic-gate */ 1217c478bd9Sstevel@tonic-gate static nis_server * 1227c478bd9Sstevel@tonic-gate get_server(char *host, nis_server *srv, endpoint eps[], int maxep) 1237c478bd9Sstevel@tonic-gate { 1247c478bd9Sstevel@tonic-gate int num_ep = 0, i; 1257c478bd9Sstevel@tonic-gate struct netconfig *nc; 1267c478bd9Sstevel@tonic-gate void *nch; 1277c478bd9Sstevel@tonic-gate struct nd_hostserv hs; 1287c478bd9Sstevel@tonic-gate struct nd_addrlist *addrs; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate if (! host) 1317c478bd9Sstevel@tonic-gate return (NULL); 1327c478bd9Sstevel@tonic-gate hs.h_host = host; 1337c478bd9Sstevel@tonic-gate hs.h_serv = "rpcbind"; 1347c478bd9Sstevel@tonic-gate nch = setnetconfig(); 1357c478bd9Sstevel@tonic-gate while (nc = getnetconfig(nch)) { 1367c478bd9Sstevel@tonic-gate if ((nc->nc_flag & NC_VISIBLE) == 0) 1377c478bd9Sstevel@tonic-gate continue; 1387c478bd9Sstevel@tonic-gate if (! netdir_getbyname(nc, &hs, &addrs)) { 1397c478bd9Sstevel@tonic-gate for (i = 0; (i < (addrs->n_cnt)) && (num_ep < maxep); 1407c478bd9Sstevel@tonic-gate i++, num_ep++) { 1417c478bd9Sstevel@tonic-gate eps[num_ep].uaddr = 1427c478bd9Sstevel@tonic-gate taddr2uaddr(nc, &(addrs->n_addrs[i])); 1437c478bd9Sstevel@tonic-gate __nis_netconfig2ep(nc, &(eps[num_ep])); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate netdir_free((char *)addrs, ND_ADDRLIST); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate } 148*61961e0fSrobinson (void) endnetconfig(nch); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate srv->name = (nis_name) host; 1517c478bd9Sstevel@tonic-gate srv->ep.ep_len = num_ep; 1527c478bd9Sstevel@tonic-gate srv->ep.ep_val = eps; 1537c478bd9Sstevel@tonic-gate srv->key_type = NIS_PK_NONE; 1547c478bd9Sstevel@tonic-gate srv->pkey.n_bytes = NULL; 1557c478bd9Sstevel@tonic-gate srv->pkey.n_len = 0; 1567c478bd9Sstevel@tonic-gate return (srv); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate #define MEP(ep, prot) (strcasecmp(ep.proto, prot) == 0) 1607c478bd9Sstevel@tonic-gate #define MAX_ENDPOINTS 32 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * __rpc_get_time_offset() 1647c478bd9Sstevel@tonic-gate * 1657c478bd9Sstevel@tonic-gate * This function uses a nis_server structure to contact the a remote 1667c478bd9Sstevel@tonic-gate * machine (as named in that structure) and returns the offset in time 1677c478bd9Sstevel@tonic-gate * between that machine and this one. This offset is returned in seconds 1687c478bd9Sstevel@tonic-gate * and may be positive or negative. 1697c478bd9Sstevel@tonic-gate * 1707c478bd9Sstevel@tonic-gate * The first time through, a lot of fiddling is done with the netconfig 1717c478bd9Sstevel@tonic-gate * stuff to find a suitable transport. The function is very aggressive 1727c478bd9Sstevel@tonic-gate * about choosing UDP or at worst TCP if it can. This is because 1737c478bd9Sstevel@tonic-gate * those transports support both the RCPBIND call and the internet 1747c478bd9Sstevel@tonic-gate * time service. 1757c478bd9Sstevel@tonic-gate * 1767c478bd9Sstevel@tonic-gate * Once through, *uaddr is set to the universal address of 1777c478bd9Sstevel@tonic-gate * the machine and *netid is set to the local netid for the transport 1787c478bd9Sstevel@tonic-gate * that uaddr goes with. On the second call, the netconfig stuff 1797c478bd9Sstevel@tonic-gate * is skipped and the uaddr/netid pair are used to fetch the netconfig 1807c478bd9Sstevel@tonic-gate * structure and to then contact the machine for the time. 1817c478bd9Sstevel@tonic-gate * 1827c478bd9Sstevel@tonic-gate * td = "server" - "client" 1837c478bd9Sstevel@tonic-gate */ 1847c478bd9Sstevel@tonic-gate int 1857c478bd9Sstevel@tonic-gate __rpc_get_time_offset(struct timeval *td, nis_server *srv, 1867c478bd9Sstevel@tonic-gate char *thost, char **uaddr, char **netid) 1877c478bd9Sstevel@tonic-gate { 1887c478bd9Sstevel@tonic-gate CLIENT *clnt; /* Client handle */ 1897c478bd9Sstevel@tonic-gate struct netbuf *addr = 0; /* address */ 1907c478bd9Sstevel@tonic-gate void *nc_handle; /* Netconfig "state" */ 1917c478bd9Sstevel@tonic-gate struct netconfig *nc; /* Various handles */ 1927c478bd9Sstevel@tonic-gate endpoint *ep; /* useful endpoints */ 1937c478bd9Sstevel@tonic-gate char *useua = NULL, /* uaddr of selected xp */ 1947c478bd9Sstevel@tonic-gate *useid = NULL; /* netid of selected xp */ 1957c478bd9Sstevel@tonic-gate int epl, i; /* counters */ 1967c478bd9Sstevel@tonic-gate enum clnt_stat status; /* result of clnt_call */ 1977c478bd9Sstevel@tonic-gate uint_t thetime; 1987c478bd9Sstevel@tonic-gate ulong_t delta; 1997c478bd9Sstevel@tonic-gate int needfree = 0; 2007c478bd9Sstevel@tonic-gate struct timeval tv; 2017c478bd9Sstevel@tonic-gate int rtime_fd = -1, time_valid, flag = 0; 2027c478bd9Sstevel@tonic-gate int a1, a2, a3, a4; 2037c478bd9Sstevel@tonic-gate char ut[INET6_ADDRSTRLEN]; 2047c478bd9Sstevel@tonic-gate char ipuaddr[INET6_ADDRSTRLEN]; 2057c478bd9Sstevel@tonic-gate endpoint teps[MAX_ENDPOINTS], 2067c478bd9Sstevel@tonic-gate *epcand[MAX_ENDPOINTS], 2077c478bd9Sstevel@tonic-gate *nonipcand[MAX_ENDPOINTS], 2087c478bd9Sstevel@tonic-gate supplied; 2097c478bd9Sstevel@tonic-gate uint32_t epc, nonip; 2107c478bd9Sstevel@tonic-gate nis_server tsrv; 2117c478bd9Sstevel@tonic-gate void (*oldsig)() = NULL; /* old alarm handler */ 2127c478bd9Sstevel@tonic-gate char *dot = NULL; /* tmp pointer */ 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate nc = NULL; 2177c478bd9Sstevel@tonic-gate td->tv_sec = 0; 2187c478bd9Sstevel@tonic-gate td->tv_usec = 0; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate /* 2217c478bd9Sstevel@tonic-gate * First check to see if we need to find and address for this 2227c478bd9Sstevel@tonic-gate * server. 2237c478bd9Sstevel@tonic-gate */ 2247c478bd9Sstevel@tonic-gate if (*uaddr == NULL) { 2257c478bd9Sstevel@tonic-gate if ((srv != NULL) && (thost != NULL)) { 2267c478bd9Sstevel@tonic-gate msg("both timehost and srv pointer used!"); 2277c478bd9Sstevel@tonic-gate return (0); 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate if (! srv) { 2307c478bd9Sstevel@tonic-gate srv = get_server(thost, &tsrv, teps, 32); 2317c478bd9Sstevel@tonic-gate if (! srv) { 2327c478bd9Sstevel@tonic-gate msg("unable to contruct server data."); 2337c478bd9Sstevel@tonic-gate return (0); 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate needfree = 1; /* need to free data in endpoints */ 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate nc_handle = (void *) setnetconfig(); 2397c478bd9Sstevel@tonic-gate if (! nc_handle) { 2407c478bd9Sstevel@tonic-gate msg("unable to get netconfig info."); 2417c478bd9Sstevel@tonic-gate if (needfree) 2427c478bd9Sstevel@tonic-gate free_eps(teps, tsrv.ep.ep_len); 2437c478bd9Sstevel@tonic-gate return (0); 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate ep = srv->ep.ep_val; 2477c478bd9Sstevel@tonic-gate epl = srv->ep.ep_len; 2487c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (epcand)/sizeof (epcand[0]); i++) { 2497c478bd9Sstevel@tonic-gate epcand[i] = 0; 2507c478bd9Sstevel@tonic-gate nonipcand[i] = 0; 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate epc = 0; 2537c478bd9Sstevel@tonic-gate nonip = 0; 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate /* 2567c478bd9Sstevel@tonic-gate * Build the list of endpoint candidates. We prefer transports 2577c478bd9Sstevel@tonic-gate * that we know are IP, but let /etc/netconfig determine the 2587c478bd9Sstevel@tonic-gate * ordering among the IP transports. 2597c478bd9Sstevel@tonic-gate * 2607c478bd9Sstevel@tonic-gate * Note: We assume that the endpoint 'proto' field contains 2617c478bd9Sstevel@tonic-gate * the netid of the transport. 2627c478bd9Sstevel@tonic-gate */ 2637c478bd9Sstevel@tonic-gate while ((nc = getnetconfig(nc_handle)) != NULL) { 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate /* Is it a visible transport ? */ 2667c478bd9Sstevel@tonic-gate if ((nc->nc_flag & NC_VISIBLE) == 0) 2677c478bd9Sstevel@tonic-gate continue; 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* Check against the end points */ 2707c478bd9Sstevel@tonic-gate for (i = 0; i < epl; i++) { 2717c478bd9Sstevel@tonic-gate if (__nis_netconfig_matches_ep(nc, &(ep[i]))) { 2727c478bd9Sstevel@tonic-gate if (MEP(ep[i], "udp") || 2737c478bd9Sstevel@tonic-gate MEP(ep[i], "udp6") || 2747c478bd9Sstevel@tonic-gate MEP(ep[i], "tcp") || 2757c478bd9Sstevel@tonic-gate MEP(ep[i], "tcp6")) { 2767c478bd9Sstevel@tonic-gate epcand[epc++] = &(ep[i]); 2777c478bd9Sstevel@tonic-gate } else { 2787c478bd9Sstevel@tonic-gate nonipcand[nonip++] = &ep[i]; 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate break; 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate 285*61961e0fSrobinson (void) endnetconfig(nc_handle); 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate /* 2887c478bd9Sstevel@tonic-gate * epcand[] now contains the candidate transports. If there 2897c478bd9Sstevel@tonic-gate * were non-IP transports as well, add them to the end of the 2907c478bd9Sstevel@tonic-gate * candidate list. 2917c478bd9Sstevel@tonic-gate */ 2927c478bd9Sstevel@tonic-gate for (i = 0; i < nonip; i++) { 2937c478bd9Sstevel@tonic-gate epcand[epc++] = nonipcand[i]; 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate if (epc == 0) { 2977c478bd9Sstevel@tonic-gate msg("no acceptable transport endpoints."); 2987c478bd9Sstevel@tonic-gate if (needfree) 2997c478bd9Sstevel@tonic-gate free_eps(teps, tsrv.ep.ep_len); 3007c478bd9Sstevel@tonic-gate return (0); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate } else { 3037c478bd9Sstevel@tonic-gate /* Caller supplied a uaddr. Fake an endpoint. */ 3047c478bd9Sstevel@tonic-gate if (*netid != 0) { 3057c478bd9Sstevel@tonic-gate supplied.proto = *netid; 3067c478bd9Sstevel@tonic-gate /* Is it one of the known IP transports ? */ 3077c478bd9Sstevel@tonic-gate if (strcmp("udp", supplied.proto) && 3087c478bd9Sstevel@tonic-gate strcmp("udp6", supplied.proto) && 3097c478bd9Sstevel@tonic-gate strcmp("tcp", supplied.proto) && 3107c478bd9Sstevel@tonic-gate strcmp("tcp6", supplied.proto)) { 3117c478bd9Sstevel@tonic-gate /* No, it's not */ 3127c478bd9Sstevel@tonic-gate nonip = 1; 3137c478bd9Sstevel@tonic-gate } else { 3147c478bd9Sstevel@tonic-gate nonip = 0; 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate } else { 3177c478bd9Sstevel@tonic-gate supplied.proto = (strchr(*uaddr, ':') != 0) ? 3187c478bd9Sstevel@tonic-gate "udp6" : "udp"; 3197c478bd9Sstevel@tonic-gate nonip = 0; 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate supplied.uaddr = *uaddr; 3227c478bd9Sstevel@tonic-gate supplied.family = (strchr(*uaddr, ':') != 0) ? 3237c478bd9Sstevel@tonic-gate "inet6" : "inet"; 3247c478bd9Sstevel@tonic-gate epcand[0] = &supplied; 3257c478bd9Sstevel@tonic-gate epc = 1; 3267c478bd9Sstevel@tonic-gate nonip = 0; 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate nc = 0; 3307c478bd9Sstevel@tonic-gate clnt = 0; 3317c478bd9Sstevel@tonic-gate status = RPC_FAILED; /* Anything except RPC_SUCCESS */ 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* 3347c478bd9Sstevel@tonic-gate * Loop over the endpoint candidates. Defer error reporting (except 3357c478bd9Sstevel@tonic-gate * for the netconfig entry) until we've looked at all candidates. 3367c478bd9Sstevel@tonic-gate */ 3377c478bd9Sstevel@tonic-gate for (i = 0; i < epc; i++) { 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate if (nc != 0) 3407c478bd9Sstevel@tonic-gate freenetconfigent(nc); 3417c478bd9Sstevel@tonic-gate nc = getnetconfigent(epcand[i]->proto); 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate if (nc == 0) { 3447c478bd9Sstevel@tonic-gate msg("unable to locate netconfig info for netid."); 3457c478bd9Sstevel@tonic-gate if (needfree) 3467c478bd9Sstevel@tonic-gate free_eps(teps, tsrv.ep.ep_len); 3477c478bd9Sstevel@tonic-gate return (0); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate /* 3517c478bd9Sstevel@tonic-gate * Add the appropriate port number to the uaddr 3527c478bd9Sstevel@tonic-gate */ 3537c478bd9Sstevel@tonic-gate useua = epcand[i]->uaddr; 3547c478bd9Sstevel@tonic-gate useid = epcand[i]->proto; 3557c478bd9Sstevel@tonic-gate if (strcasecmp(nc->nc_protofmly, NC_INET) == 0) { 356*61961e0fSrobinson (void) sscanf(useua, 357*61961e0fSrobinson "%d.%d.%d.%d.", &a1, &a2, &a3, &a4); 358*61961e0fSrobinson (void) sprintf(ipuaddr, "%d.%d.%d.%d.0.111", 359*61961e0fSrobinson a1, a2, a3, a4); 3607c478bd9Sstevel@tonic-gate useua = &ipuaddr[0]; 3617c478bd9Sstevel@tonic-gate } else if (strcasecmp(nc->nc_protofmly, NC_INET6) == 0) { 3627c478bd9Sstevel@tonic-gate size_t len; 3637c478bd9Sstevel@tonic-gate char *port = ".0.111"; 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate if (strlen(useua) >= sizeof (ipuaddr)) { 3667c478bd9Sstevel@tonic-gate freenetconfigent(nc); 3677c478bd9Sstevel@tonic-gate if (needfree) 3687c478bd9Sstevel@tonic-gate free_eps(teps, tsrv.ep.ep_len); 3697c478bd9Sstevel@tonic-gate return (0); 3707c478bd9Sstevel@tonic-gate } 3717c478bd9Sstevel@tonic-gate 372*61961e0fSrobinson (void) strcpy(ipuaddr, useua); 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate /* get the IPv6 address out of the uaddr */ 3757c478bd9Sstevel@tonic-gate if ((dot = strrchr(ipuaddr, '.')) != 0) { 3767c478bd9Sstevel@tonic-gate *dot = '\0'; 3777c478bd9Sstevel@tonic-gate if ((dot = strrchr(ipuaddr, '.')) != 0) 3787c478bd9Sstevel@tonic-gate *dot = '\0'; 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate if (dot == 0 || 3827c478bd9Sstevel@tonic-gate (len = strlen(ipuaddr))+strlen(port) >= 3837c478bd9Sstevel@tonic-gate sizeof (ipuaddr)) { 3847c478bd9Sstevel@tonic-gate freenetconfigent(nc); 3857c478bd9Sstevel@tonic-gate if (needfree) 3867c478bd9Sstevel@tonic-gate free_eps(teps, tsrv.ep.ep_len); 3877c478bd9Sstevel@tonic-gate return (0); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate /* now put in 0.111 */ 391*61961e0fSrobinson (void) strcat(ipuaddr + len, port); 3927c478bd9Sstevel@tonic-gate useua = ipuaddr; 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate /* 3967c478bd9Sstevel@tonic-gate * Create the client handle to rpcbind. Note we always try 3977c478bd9Sstevel@tonic-gate * version 3 since that is the earliest version that supports 3987c478bd9Sstevel@tonic-gate * the RPCB_GETTIME call. Also it is the version that comes 3997c478bd9Sstevel@tonic-gate * standard with SVR4. Since most everyone supports TCP/IP 4007c478bd9Sstevel@tonic-gate * we could consider trying the rtime call first. 4017c478bd9Sstevel@tonic-gate */ 4027c478bd9Sstevel@tonic-gate if (clnt != 0) 4037c478bd9Sstevel@tonic-gate clnt_destroy(clnt); 4047c478bd9Sstevel@tonic-gate clnt = __nis_clnt_create(RPC_ANYFD, nc, useua, 0, 0, RPCBPROG, 4057c478bd9Sstevel@tonic-gate RPCBVERS, 0, 0); 4067c478bd9Sstevel@tonic-gate if (! clnt) 4077c478bd9Sstevel@tonic-gate continue; 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate tv.tv_sec = 5; 4107c478bd9Sstevel@tonic-gate tv.tv_usec = 0; 4117c478bd9Sstevel@tonic-gate time_valid = 0; 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate status = clnt_call(clnt, RPCBPROC_GETTIME, xdr_void, NULL, 4147c478bd9Sstevel@tonic-gate xdr_u_int, (char *)&thetime, tv); 4157c478bd9Sstevel@tonic-gate /* 4167c478bd9Sstevel@tonic-gate * The only error we check for is anything but success. In 4177c478bd9Sstevel@tonic-gate * fact we could have seen PROGMISMATCH if talking to a 4.1 4187c478bd9Sstevel@tonic-gate * machine (pmap v2) or TIMEDOUT if the net was busy. 4197c478bd9Sstevel@tonic-gate */ 4207c478bd9Sstevel@tonic-gate if (status == RPC_SUCCESS) 4217c478bd9Sstevel@tonic-gate break; 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate if (status == RPC_SUCCESS) { 4267c478bd9Sstevel@tonic-gate time_valid = 1; 4277c478bd9Sstevel@tonic-gate } else if (clnt == 0) { 4287c478bd9Sstevel@tonic-gate msg("unable to create client handle to rpcbind."); 4297c478bd9Sstevel@tonic-gate freenetconfigent(nc); 4307c478bd9Sstevel@tonic-gate if (needfree) 4317c478bd9Sstevel@tonic-gate free_eps(teps, tsrv.ep.ep_len); 4327c478bd9Sstevel@tonic-gate return (0); 4337c478bd9Sstevel@tonic-gate } else { 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate /* 4367c478bd9Sstevel@tonic-gate * Try the timeservice port. This presumably only exists 4377c478bd9Sstevel@tonic-gate * for IP transports, so we ignore the non-IP ones. 4387c478bd9Sstevel@tonic-gate */ 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate for (i = 0; i < epc-nonip; i++) { 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate /* 4437c478bd9Sstevel@tonic-gate * Convert PMAP address into timeservice address 4447c478bd9Sstevel@tonic-gate * We take advantage of the fact that we "know" what 4457c478bd9Sstevel@tonic-gate * a universal address looks like for inet transports. 4467c478bd9Sstevel@tonic-gate * 4477c478bd9Sstevel@tonic-gate * We also know that the internet timeservice is always 4487c478bd9Sstevel@tonic-gate * listening on port 37. 4497c478bd9Sstevel@tonic-gate */ 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate if (nc != 0) 4527c478bd9Sstevel@tonic-gate freenetconfigent(nc); 4537c478bd9Sstevel@tonic-gate nc = getnetconfigent(epcand[i]->proto); 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate if (nc == 0) { 4567c478bd9Sstevel@tonic-gate msg("no netconfig info for netid."); 4577c478bd9Sstevel@tonic-gate if (needfree) 4587c478bd9Sstevel@tonic-gate free_eps(teps, tsrv.ep.ep_len); 4597c478bd9Sstevel@tonic-gate return (0); 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate useua = epcand[i]->uaddr; 4637c478bd9Sstevel@tonic-gate useid = epcand[i]->proto; 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate if (strcasecmp(nc->nc_protofmly, NC_INET) == 0) { 466*61961e0fSrobinson (void) sscanf(useua, 4677c478bd9Sstevel@tonic-gate "%d.%d.%d.%d.", &a1, &a2, &a3, &a4); 468*61961e0fSrobinson (void) sprintf(ut, "%d.%d.%d.%d.0.37", 4697c478bd9Sstevel@tonic-gate a1, a2, a3, a4); 4707c478bd9Sstevel@tonic-gate } else if (strcasecmp(nc->nc_protofmly, NC_INET6) == 4717c478bd9Sstevel@tonic-gate 0) { 4727c478bd9Sstevel@tonic-gate size_t len; 4737c478bd9Sstevel@tonic-gate char *port = ".0.37"; 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate if (strlen(useua) >= sizeof (ut)) { 4767c478bd9Sstevel@tonic-gate goto error; 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate 479*61961e0fSrobinson (void) strcpy(ut, useua); 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate /* get the IPv6 address out of the uaddr */ 4827c478bd9Sstevel@tonic-gate if ((dot = strrchr(ut, '.')) != 0) { 4837c478bd9Sstevel@tonic-gate *dot = '\0'; 4847c478bd9Sstevel@tonic-gate if ((dot = strrchr(ut, '.')) != 0) 4857c478bd9Sstevel@tonic-gate *dot = '\0'; 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate if (dot == 0) { 4897c478bd9Sstevel@tonic-gate goto error; 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate if ((len = strlen(ut))+strlen(port) >= 4937c478bd9Sstevel@tonic-gate sizeof (ut)) { 4947c478bd9Sstevel@tonic-gate goto error; 4957c478bd9Sstevel@tonic-gate } 4967c478bd9Sstevel@tonic-gate 497*61961e0fSrobinson (void) strcat(ut + len, port); 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate addr = uaddr2taddr(nc, ut); 5027c478bd9Sstevel@tonic-gate if (! addr) { 5037c478bd9Sstevel@tonic-gate msg("timeservice uaddr to taddr failed."); 5047c478bd9Sstevel@tonic-gate goto error; 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate rtime_fd = t_open(nc->nc_device, O_RDWR, NULL); 5087c478bd9Sstevel@tonic-gate if (rtime_fd == -1) { 5097c478bd9Sstevel@tonic-gate msg("unable to open fd to network."); 5107c478bd9Sstevel@tonic-gate goto error; 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate if (t_bind(rtime_fd, NULL, NULL) < 0) { 5147c478bd9Sstevel@tonic-gate msg("unable to bind an endpoint to fd."); 5157c478bd9Sstevel@tonic-gate goto error; 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate /* 5197c478bd9Sstevel@tonic-gate * Now depending on whether or not we're talking to 5207c478bd9Sstevel@tonic-gate * UDP we set a timeout or not. 5217c478bd9Sstevel@tonic-gate */ 5227c478bd9Sstevel@tonic-gate if (nc->nc_semantics == NC_TPI_CLTS) { 5237c478bd9Sstevel@tonic-gate struct t_unitdata tu_data; 5247c478bd9Sstevel@tonic-gate struct pollfd pfd; 5257c478bd9Sstevel@tonic-gate int res; 5267c478bd9Sstevel@tonic-gate 5277c478bd9Sstevel@tonic-gate tu_data.addr = *addr; 5287c478bd9Sstevel@tonic-gate tu_data.udata.buf = (char *)&thetime; 5297c478bd9Sstevel@tonic-gate tu_data.udata.len = (uint_t)sizeof (thetime); 5307c478bd9Sstevel@tonic-gate tu_data.udata.maxlen = tu_data.udata.len; 5317c478bd9Sstevel@tonic-gate tu_data.opt.len = 0; 5327c478bd9Sstevel@tonic-gate tu_data.opt.maxlen = 0; 5337c478bd9Sstevel@tonic-gate if (t_sndudata(rtime_fd, &tu_data) == -1) { 5347c478bd9Sstevel@tonic-gate msg("udp : t_sndudata failed."); 5357c478bd9Sstevel@tonic-gate goto error; 5367c478bd9Sstevel@tonic-gate } 5377c478bd9Sstevel@tonic-gate pfd.fd = rtime_fd; 5387c478bd9Sstevel@tonic-gate pfd.events = 5397c478bd9Sstevel@tonic-gate POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND; 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate do { 5427c478bd9Sstevel@tonic-gate res = poll(&pfd, 1, 10000); 5437c478bd9Sstevel@tonic-gate } while (res < 0); 5447c478bd9Sstevel@tonic-gate if ((res <= 0) || (pfd.revents & POLLNVAL)) 5457c478bd9Sstevel@tonic-gate goto error; 5467c478bd9Sstevel@tonic-gate if (t_rcvudata(rtime_fd, &tu_data, &flag) < 5477c478bd9Sstevel@tonic-gate 0) { 5487c478bd9Sstevel@tonic-gate msg("t_rvcdata failed on udp trpt."); 5497c478bd9Sstevel@tonic-gate goto error; 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate time_valid = 1; 5527c478bd9Sstevel@tonic-gate } else { 5537c478bd9Sstevel@tonic-gate struct t_call sndcall; 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate sndcall.addr = *addr; 5567c478bd9Sstevel@tonic-gate sndcall.opt.len = sndcall.opt.maxlen = 0; 5577c478bd9Sstevel@tonic-gate sndcall.udata.len = sndcall.udata.maxlen = 0; 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate oldsig = (void (*)())signal(SIGALRM, 5607c478bd9Sstevel@tonic-gate alarm_hndler); 5617c478bd9Sstevel@tonic-gate saw_alarm = 0; /* global tracking the alarm */ 562*61961e0fSrobinson (void) alarm(20); /* only wait 20 seconds */ 5637c478bd9Sstevel@tonic-gate if (t_connect(rtime_fd, &sndcall, NULL) == 5647c478bd9Sstevel@tonic-gate -1) { 5657c478bd9Sstevel@tonic-gate msg("connect tcp endpoint failedd."); 5667c478bd9Sstevel@tonic-gate goto error; 5677c478bd9Sstevel@tonic-gate } 5687c478bd9Sstevel@tonic-gate if (saw_alarm) { 5697c478bd9Sstevel@tonic-gate msg("alarm caught it; unreachable."); 5707c478bd9Sstevel@tonic-gate goto error; 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate if (t_rcv(rtime_fd, (char *)&thetime, 5737c478bd9Sstevel@tonic-gate (uint_t)sizeof (thetime), &flag) != 5747c478bd9Sstevel@tonic-gate (uint_t)sizeof (thetime)) { 5757c478bd9Sstevel@tonic-gate if (saw_alarm) { 5767c478bd9Sstevel@tonic-gate /*EMPTY*/ 5777c478bd9Sstevel@tonic-gate msg("timed out TCP call."); 5787c478bd9Sstevel@tonic-gate } else { 5797c478bd9Sstevel@tonic-gate /*EMPTY*/ 5807c478bd9Sstevel@tonic-gate msg("wrong size results"); 5817c478bd9Sstevel@tonic-gate } 5827c478bd9Sstevel@tonic-gate goto error; 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate time_valid = 1; 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate if (time_valid) { 5877c478bd9Sstevel@tonic-gate thetime = ntohl(thetime); 5887c478bd9Sstevel@tonic-gate /* adjust to UNIX time */ 5897c478bd9Sstevel@tonic-gate thetime = thetime - TOFFSET; 5907c478bd9Sstevel@tonic-gate } else 5917c478bd9Sstevel@tonic-gate thetime = 0; 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate error: 5967c478bd9Sstevel@tonic-gate /* 5977c478bd9Sstevel@tonic-gate * clean up our allocated data structures. 5987c478bd9Sstevel@tonic-gate */ 5997c478bd9Sstevel@tonic-gate if (addr) 6007c478bd9Sstevel@tonic-gate netdir_free((char *)(addr), ND_ADDR); 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate if (rtime_fd != -1) 6037c478bd9Sstevel@tonic-gate (void) t_close(rtime_fd); 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate if (clnt) 6067c478bd9Sstevel@tonic-gate clnt_destroy(clnt); 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate if (nc) 6097c478bd9Sstevel@tonic-gate freenetconfigent(nc); 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate if (oldsig) { 612*61961e0fSrobinson (void) alarm(0); /* reset that alarm if its outstanding */ 613*61961e0fSrobinson (void) signal(SIGALRM, oldsig); 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate /* 6177c478bd9Sstevel@tonic-gate * note, don't free uaddr strings until after we've made a 6187c478bd9Sstevel@tonic-gate * copy of them. 6197c478bd9Sstevel@tonic-gate */ 6207c478bd9Sstevel@tonic-gate if (time_valid) { 6217c478bd9Sstevel@tonic-gate if (! *netid) { 6227c478bd9Sstevel@tonic-gate *netid = strdup(useid); 6237c478bd9Sstevel@tonic-gate if (! *netid) { 6247c478bd9Sstevel@tonic-gate msg("__rpc_get_time_offset: strdup failed."); 6257c478bd9Sstevel@tonic-gate if (needfree) 6267c478bd9Sstevel@tonic-gate free_eps(teps, tsrv.ep.ep_len); 6277c478bd9Sstevel@tonic-gate return (0); 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate *uaddr = strdup(useua); 6317c478bd9Sstevel@tonic-gate if (! *uaddr) { 6327c478bd9Sstevel@tonic-gate msg("__rpc_get_time_offset: strdup failed."); 6337c478bd9Sstevel@tonic-gate if (*netid) 6347c478bd9Sstevel@tonic-gate free(*netid); 6357c478bd9Sstevel@tonic-gate if (needfree) 6367c478bd9Sstevel@tonic-gate free_eps(teps, tsrv.ep.ep_len); 6377c478bd9Sstevel@tonic-gate return (0); 6387c478bd9Sstevel@tonic-gate } 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate 641*61961e0fSrobinson (void) gettimeofday(&tv, 0); 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate /* Round to the nearest second */ 6447c478bd9Sstevel@tonic-gate tv.tv_sec += (tv.tv_sec > 500000) ? 1 : 0; 6457c478bd9Sstevel@tonic-gate delta = (thetime > tv.tv_sec) ? thetime - tv.tv_sec : 6467c478bd9Sstevel@tonic-gate tv.tv_sec - thetime; 6477c478bd9Sstevel@tonic-gate td->tv_sec = (thetime < tv.tv_sec) ? - delta : delta; 6487c478bd9Sstevel@tonic-gate td->tv_usec = 0; 6497c478bd9Sstevel@tonic-gate } else { 6507c478bd9Sstevel@tonic-gate /*EMPTY*/ 6517c478bd9Sstevel@tonic-gate msg("unable to get the server's time."); 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate if (needfree) 6557c478bd9Sstevel@tonic-gate free_eps(teps, tsrv.ep.ep_len); 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate return (time_valid); 6587c478bd9Sstevel@tonic-gate } 659