190271Salfred/* #pragma ident "@(#)auth_time.c 1.4 92/11/10 SMI" */ 226219Swpaul 326219Swpaul/* 426219Swpaul * auth_time.c 526219Swpaul * 626219Swpaul * This module contains the private function __rpc_get_time_offset() 726219Swpaul * which will return the difference in seconds between the local system's 826219Swpaul * notion of time and a remote server's notion of time. This must be 926219Swpaul * possible without calling any functions that may invoke the name 1026219Swpaul * service. (netdir_getbyxxx, getXbyY, etc). The function is used in the 1126219Swpaul * synchronize call of the authdes code to synchronize clocks between 1226219Swpaul * NIS+ clients and their servers. 1326219Swpaul * 1426219Swpaul * Note to minimize the amount of duplicate code, portions of the 1526219Swpaul * synchronize() function were folded into this code, and the synchronize 1626219Swpaul * call becomes simply a wrapper around this function. Further, if this 1726219Swpaul * function is called with a timehost it *DOES* recurse to the name 1826219Swpaul * server so don't use it in that mode if you are doing name service code. 1926219Swpaul * 2026219Swpaul * Copyright (c) 1992 Sun Microsystems Inc. 2126219Swpaul * All rights reserved. 2226219Swpaul * 2326219Swpaul * Side effects : 2426219Swpaul * When called a client handle to a RPCBIND process is created 2526219Swpaul * and destroyed. Two strings "netid" and "uaddr" are malloc'd 2626219Swpaul * and returned. The SIGALRM processing is modified only if 2726219Swpaul * needed to deal with TCP connections. 2826219Swpaul */ 2992990Sobrien 3092990Sobrien#include <sys/cdefs.h> 3192990Sobrien__FBSDID("$FreeBSD: releng/11.0/lib/libc/rpc/auth_time.c 287348 2015-09-01 07:33:36Z rodrigc $"); 3292990Sobrien 3371579Sdeischen#include "namespace.h" 3426219Swpaul#include <stdio.h> 3526219Swpaul#include <syslog.h> 3626219Swpaul#include <string.h> 3726219Swpaul#include <stdlib.h> 3826219Swpaul#include <unistd.h> 3926219Swpaul#include <netdb.h> 4026219Swpaul#include <sys/signal.h> 4126219Swpaul#include <sys/errno.h> 4226219Swpaul#include <sys/socket.h> 4326219Swpaul#include <netinet/in.h> 4426219Swpaul#include <arpa/inet.h> 4526219Swpaul#include <rpc/rpc.h> 4626219Swpaul#include <rpc/rpc_com.h> 4774462Salfred#include <rpc/rpcb_prot.h> 4826219Swpaul#undef NIS 4926219Swpaul#include <rpcsvc/nis.h> 5071579Sdeischen#include "un-namespace.h" 5126219Swpaul 52124178Snectarextern int _rpc_dtablesize( void ); 53124178Snectar 5426219Swpaul#ifdef TESTING 5526219Swpaul#define msg(x) printf("ERROR: %s\n", x) 5626219Swpaul/* #define msg(x) syslog(LOG_ERR, "%s", x) */ 5726219Swpaul#else 5826219Swpaul#define msg(x) 5926219Swpaul#endif 6026219Swpaul 6126219Swpaulstatic int saw_alarm = 0; 6226219Swpaul 6326219Swpaulstatic void 64287341Srodrigcalarm_hndler(int s) 6526219Swpaul{ 6626219Swpaul saw_alarm = 1; 6726219Swpaul return; 6826219Swpaul} 6926219Swpaul 7026219Swpaul/* 7126219Swpaul * The internet time server defines the epoch to be Jan 1, 1900 7226219Swpaul * whereas UNIX defines it to be Jan 1, 1970. To adjust the result 7326219Swpaul * from internet time-service time, into UNIX time we subtract the 7426219Swpaul * following offset : 7526219Swpaul */ 7626219Swpaul#define NYEARS (1970 - 1900) 7726219Swpaul#define TOFFSET ((u_long)60*60*24*(365*NYEARS + (NYEARS/4))) 7826219Swpaul 7926219Swpaul 8026219Swpaul/* 8126219Swpaul * Stolen from rpc.nisd: 8226219Swpaul * Turn a 'universal address' into a struct sockaddr_in. 8326219Swpaul * Bletch. 8426219Swpaul */ 85287341Srodrigcstatic int uaddr_to_sockaddr(char *uaddr, struct sockaddr_in *sin) 8626219Swpaul{ 8726219Swpaul unsigned char p_bytes[2]; 8826219Swpaul int i; 8926219Swpaul unsigned long a[6]; 9026219Swpaul 9126219Swpaul i = sscanf(uaddr, "%lu.%lu.%lu.%lu.%lu.%lu", &a[0], &a[1], &a[2], 9226219Swpaul &a[3], &a[4], &a[5]); 9326219Swpaul 9426219Swpaul if (i < 6) 9526219Swpaul return(1); 9626219Swpaul 9726219Swpaul for (i = 0; i < 4; i++) 9826219Swpaul sin->sin_addr.s_addr |= (a[i] & 0x000000FF) << (8 * i); 9926219Swpaul 10026219Swpaul p_bytes[0] = (unsigned char)a[4] & 0x000000FF; 10126219Swpaul p_bytes[1] = (unsigned char)a[5] & 0x000000FF; 10226219Swpaul 10326219Swpaul sin->sin_family = AF_INET; /* always */ 10426219Swpaul bcopy((char *)&p_bytes, (char *)&sin->sin_port, 2); 10526219Swpaul 10626219Swpaul return (0); 10726219Swpaul} 10826219Swpaul 10926219Swpaul/* 11026219Swpaul * free_eps() 11126219Swpaul * 11226219Swpaul * Free the strings that were strduped into the eps structure. 11326219Swpaul */ 11426219Swpaulstatic void 115287341Srodrigcfree_eps(endpoint eps[], int num) 11626219Swpaul{ 11726219Swpaul int i; 11826219Swpaul 11926219Swpaul for (i = 0; i < num; i++) { 12026219Swpaul free(eps[i].uaddr); 12126219Swpaul free(eps[i].proto); 12226219Swpaul free(eps[i].family); 12326219Swpaul } 12426219Swpaul return; 12526219Swpaul} 12626219Swpaul 12726219Swpaul/* 12826219Swpaul * get_server() 12926219Swpaul * 13026219Swpaul * This function constructs a nis_server structure description for the 13126219Swpaul * indicated hostname. 13226219Swpaul * 13326219Swpaul * NOTE: There is a chance we may end up recursing here due to the 13426219Swpaul * fact that gethostbyname() could do an NIS search. Ideally, the 13526219Swpaul * NIS+ server will call __rpc_get_time_offset() with the nis_server 13626219Swpaul * structure already populated. 137287341Srodrigc * 138287341Srodrigc * host - name of the time host 139287341Srodrigc * srv - nis_server struct to use. 140287341Srodrigc * eps[] - array of endpoints 141287341Srodrigc * maxep - max array size 14226219Swpaul */ 14326219Swpaulstatic nis_server * 144287341Srodrigcget_server(struct sockaddr_in *sin, char *host, nis_server *srv, 145287341Srodrigc endpoint eps[], int maxep) 14626219Swpaul{ 14726219Swpaul char hname[256]; 14826219Swpaul int num_ep = 0, i; 14926219Swpaul struct hostent *he; 15026219Swpaul struct hostent dummy; 15126219Swpaul char *ptr[2]; 152172259Smatteo endpoint *ep; 15326219Swpaul 15426219Swpaul if (host == NULL && sin == NULL) 15526219Swpaul return (NULL); 15626219Swpaul 15726219Swpaul if (sin == NULL) { 15826219Swpaul he = gethostbyname(host); 15926219Swpaul if (he == NULL) 16026219Swpaul return(NULL); 16126219Swpaul } else { 16226219Swpaul he = &dummy; 16326219Swpaul ptr[0] = (char *)&sin->sin_addr.s_addr; 16426219Swpaul ptr[1] = NULL; 16526219Swpaul dummy.h_addr_list = ptr; 16626219Swpaul } 16726219Swpaul 16826219Swpaul /* 16926219Swpaul * This is lame. We go around once for TCP, then again 17026219Swpaul * for UDP. 17126219Swpaul */ 172172259Smatteo for (i = 0, ep = eps; (he->h_addr_list[i] != NULL) && (num_ep < maxep); 173172259Smatteo i++, ep++, num_ep++) { 17426219Swpaul struct in_addr *a; 17526219Swpaul 17626219Swpaul a = (struct in_addr *)he->h_addr_list[i]; 17726219Swpaul snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a)); 178172259Smatteo ep->uaddr = strdup(hname); 179172259Smatteo ep->family = strdup("inet"); 180172259Smatteo ep->proto = strdup("tcp"); 181172259Smatteo if (ep->uaddr == NULL || ep->family == NULL || ep->proto == NULL) { 182172259Smatteo free_eps(eps, num_ep + 1); 183172259Smatteo return (NULL); 184172259Smatteo } 18526219Swpaul } 18626219Swpaul 18726219Swpaul for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep); 188172259Smatteo i++, ep++, num_ep++) { 18926219Swpaul struct in_addr *a; 19026219Swpaul 19126219Swpaul a = (struct in_addr *)he->h_addr_list[i]; 19226219Swpaul snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a)); 193172259Smatteo ep->uaddr = strdup(hname); 194172259Smatteo ep->family = strdup("inet"); 195172259Smatteo ep->proto = strdup("udp"); 196172259Smatteo if (ep->uaddr == NULL || ep->family == NULL || ep->proto == NULL) { 197172259Smatteo free_eps(eps, num_ep + 1); 198172259Smatteo return (NULL); 199172259Smatteo } 20026219Swpaul } 20126219Swpaul 20226219Swpaul srv->name = (nis_name) host; 20326219Swpaul srv->ep.ep_len = num_ep; 20426219Swpaul srv->ep.ep_val = eps; 20526219Swpaul srv->key_type = NIS_PK_NONE; 20626219Swpaul srv->pkey.n_bytes = NULL; 20726219Swpaul srv->pkey.n_len = 0; 20826219Swpaul return (srv); 20926219Swpaul} 21026219Swpaul 21126219Swpaul/* 21226219Swpaul * __rpc_get_time_offset() 21326219Swpaul * 21426219Swpaul * This function uses a nis_server structure to contact the a remote 21526219Swpaul * machine (as named in that structure) and returns the offset in time 21626219Swpaul * between that machine and this one. This offset is returned in seconds 21726219Swpaul * and may be positive or negative. 21826219Swpaul * 21926219Swpaul * The first time through, a lot of fiddling is done with the netconfig 22026219Swpaul * stuff to find a suitable transport. The function is very aggressive 22126219Swpaul * about choosing UDP or at worst TCP if it can. This is because 22226219Swpaul * those transports support both the RCPBIND call and the internet 22326219Swpaul * time service. 22426219Swpaul * 22526219Swpaul * Once through, *uaddr is set to the universal address of 22626219Swpaul * the machine and *netid is set to the local netid for the transport 22726219Swpaul * that uaddr goes with. On the second call, the netconfig stuff 22826219Swpaul * is skipped and the uaddr/netid pair are used to fetch the netconfig 22926219Swpaul * structure and to then contact the machine for the time. 23026219Swpaul * 23126219Swpaul * td = "server" - "client" 232287341Srodrigc * 233287341Srodrigc * td - Time difference 234287341Srodrigc * srv - NIS Server description 235287341Srodrigc * thost - if no server, this is the timehost 236287341Srodrigc * uaddr - known universal address 237287341Srodrigc * netid - known network identifier 23826219Swpaul */ 23926219Swpaulint 240287341Srodrigc__rpc_get_time_offset(struct timeval *td, nis_server *srv, char *thost, 241287341Srodrigc char **uaddr, struct sockaddr_in *netid) 24226219Swpaul{ 24326219Swpaul CLIENT *clnt; /* Client handle */ 24426219Swpaul endpoint *ep, /* useful endpoints */ 24526219Swpaul *useep = NULL; /* endpoint of xp */ 24626219Swpaul char *useua = NULL; /* uaddr of selected xp */ 24726219Swpaul int epl, i; /* counters */ 24826219Swpaul enum clnt_stat status; /* result of clnt_call */ 24926219Swpaul u_long thetime, delta; 25026219Swpaul int needfree = 0; 25126219Swpaul struct timeval tv; 25226219Swpaul int time_valid; 25326219Swpaul int udp_ep = -1, tcp_ep = -1; 25426219Swpaul int a1, a2, a3, a4; 25526219Swpaul char ut[64], ipuaddr[64]; 25626219Swpaul endpoint teps[32]; 25726219Swpaul nis_server tsrv; 258287348Srodrigc void (*oldsig)(int) = NULL; /* old alarm handler */ 25926219Swpaul struct sockaddr_in sin; 260143344Sstefanf socklen_t len; 261143344Sstefanf int s = RPC_ANYSOCK; 26226219Swpaul int type = 0; 26326219Swpaul 26426219Swpaul td->tv_sec = 0; 26526219Swpaul td->tv_usec = 0; 26626219Swpaul 26726219Swpaul /* 26826219Swpaul * First check to see if we need to find and address for this 26926219Swpaul * server. 27026219Swpaul */ 27126219Swpaul if (*uaddr == NULL) { 27226219Swpaul if ((srv != NULL) && (thost != NULL)) { 27326219Swpaul msg("both timehost and srv pointer used!"); 27426219Swpaul return (0); 27526219Swpaul } 27626219Swpaul if (! srv) { 27726219Swpaul srv = get_server(netid, thost, &tsrv, teps, 32); 27826219Swpaul if (srv == NULL) { 27926219Swpaul msg("unable to contruct server data."); 28026219Swpaul return (0); 28126219Swpaul } 28226219Swpaul needfree = 1; /* need to free data in endpoints */ 28326219Swpaul } 28426219Swpaul 28526219Swpaul ep = srv->ep.ep_val; 28626219Swpaul epl = srv->ep.ep_len; 28726219Swpaul 28826219Swpaul /* Identify the TCP and UDP endpoints */ 28926219Swpaul for (i = 0; 29026219Swpaul (i < epl) && ((udp_ep == -1) || (tcp_ep == -1)); i++) { 29126219Swpaul if (strcasecmp(ep[i].proto, "udp") == 0) 29226219Swpaul udp_ep = i; 29326219Swpaul if (strcasecmp(ep[i].proto, "tcp") == 0) 29426219Swpaul tcp_ep = i; 29526219Swpaul } 29626219Swpaul 29726219Swpaul /* Check to see if it is UDP or TCP */ 29826219Swpaul if (tcp_ep > -1) { 29926219Swpaul useep = &ep[tcp_ep]; 30026219Swpaul useua = ep[tcp_ep].uaddr; 30126219Swpaul type = SOCK_STREAM; 30226219Swpaul } else if (udp_ep > -1) { 30326219Swpaul useep = &ep[udp_ep]; 30426219Swpaul useua = ep[udp_ep].uaddr; 30526219Swpaul type = SOCK_DGRAM; 30626219Swpaul } 30726219Swpaul 30826219Swpaul if (useep == NULL) { 30926219Swpaul msg("no acceptable transport endpoints."); 31026219Swpaul if (needfree) 31126219Swpaul free_eps(teps, tsrv.ep.ep_len); 31226219Swpaul return (0); 31326219Swpaul } 31426219Swpaul } 31526219Swpaul 31626219Swpaul /* 31726219Swpaul * Create a sockaddr from the uaddr. 31826219Swpaul */ 31926219Swpaul if (*uaddr != NULL) 32026219Swpaul useua = *uaddr; 32126219Swpaul 32226219Swpaul /* Fixup test for NIS+ */ 32326219Swpaul sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4); 32426219Swpaul sprintf(ipuaddr, "%d.%d.%d.%d.0.111", a1, a2, a3, a4); 32526219Swpaul useua = &ipuaddr[0]; 32626219Swpaul 32726666Swpaul bzero((char *)&sin, sizeof(sin)); 32826219Swpaul if (uaddr_to_sockaddr(useua, &sin)) { 32926219Swpaul msg("unable to translate uaddr to sockaddr."); 33026219Swpaul if (needfree) 33126219Swpaul free_eps(teps, tsrv.ep.ep_len); 33226219Swpaul return (0); 33326219Swpaul } 33426219Swpaul 33526219Swpaul /* 33626219Swpaul * Create the client handle to rpcbind. Note we always try 33726219Swpaul * version 3 since that is the earliest version that supports 33826219Swpaul * the RPCB_GETTIME call. Also it is the version that comes 33926219Swpaul * standard with SVR4. Since most everyone supports TCP/IP 34026219Swpaul * we could consider trying the rtime call first. 34126219Swpaul */ 34226219Swpaul clnt = clnttcp_create(&sin, RPCBPROG, RPCBVERS, &s, 0, 0); 34326219Swpaul if (clnt == NULL) { 34426219Swpaul msg("unable to create client handle to rpcbind."); 34526219Swpaul if (needfree) 34626219Swpaul free_eps(teps, tsrv.ep.ep_len); 34726219Swpaul return (0); 34826219Swpaul } 34926219Swpaul 35026219Swpaul tv.tv_sec = 5; 35126219Swpaul tv.tv_usec = 0; 35226219Swpaul time_valid = 0; 35395658Sdes status = clnt_call(clnt, RPCBPROC_GETTIME, (xdrproc_t)xdr_void, NULL, 35495658Sdes (xdrproc_t)xdr_u_long, &thetime, tv); 35526219Swpaul /* 35626219Swpaul * The only error we check for is anything but success. In 35726219Swpaul * fact we could have seen PROGMISMATCH if talking to a 4.1 35826219Swpaul * machine (pmap v2) or TIMEDOUT if the net was busy. 35926219Swpaul */ 36026219Swpaul if (status == RPC_SUCCESS) 36126219Swpaul time_valid = 1; 36226219Swpaul else { 36326219Swpaul int save; 36426219Swpaul 36526219Swpaul /* Blow away possible stale CLNT handle. */ 36626219Swpaul if (clnt != NULL) { 36726219Swpaul clnt_destroy(clnt); 36826219Swpaul clnt = NULL; 36926219Swpaul } 37026219Swpaul 37126219Swpaul /* 37226219Swpaul * Convert PMAP address into timeservice address 37326219Swpaul * We take advantage of the fact that we "know" what 37426219Swpaul * the universal address looks like for inet transports. 37526219Swpaul * 37626219Swpaul * We also know that the internet timeservice is always 37726219Swpaul * listening on port 37. 37826219Swpaul */ 37926219Swpaul sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4); 38026219Swpaul sprintf(ut, "%d.%d.%d.%d.0.37", a1, a2, a3, a4); 38126219Swpaul 38226219Swpaul if (uaddr_to_sockaddr(ut, &sin)) { 38326219Swpaul msg("cannot convert timeservice uaddr to sockaddr."); 38426219Swpaul goto error; 38526219Swpaul } 38626219Swpaul 38771579Sdeischen s = _socket(AF_INET, type, 0); 38826219Swpaul if (s == -1) { 38926219Swpaul msg("unable to open fd to network."); 39026219Swpaul goto error; 39126219Swpaul } 39226219Swpaul 39326219Swpaul /* 39426219Swpaul * Now depending on whether or not we're talking to 39526219Swpaul * UDP we set a timeout or not. 39626219Swpaul */ 39726219Swpaul if (type == SOCK_DGRAM) { 39826219Swpaul struct timeval timeout = { 20, 0 }; 39926219Swpaul struct sockaddr_in from; 40026219Swpaul fd_set readfds; 40126219Swpaul int res; 40226219Swpaul 40371579Sdeischen if (_sendto(s, &thetime, sizeof(thetime), 0, 40426219Swpaul (struct sockaddr *)&sin, sizeof(sin)) == -1) { 40526219Swpaul msg("udp : sendto failed."); 40626219Swpaul goto error; 40726219Swpaul } 40826219Swpaul do { 40926219Swpaul FD_ZERO(&readfds); 41026219Swpaul FD_SET(s, &readfds); 41171579Sdeischen res = _select(_rpc_dtablesize(), &readfds, 41226219Swpaul (fd_set *)NULL, (fd_set *)NULL, &timeout); 41326219Swpaul } while (res < 0 && errno == EINTR); 41426219Swpaul if (res <= 0) 41526219Swpaul goto error; 41626219Swpaul len = sizeof(from); 41771579Sdeischen res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0, 41826219Swpaul (struct sockaddr *)&from, &len); 41926219Swpaul if (res == -1) { 42026219Swpaul msg("recvfrom failed on udp transport."); 42126219Swpaul goto error; 42226219Swpaul } 42326219Swpaul time_valid = 1; 42426219Swpaul } else { 42526219Swpaul int res; 42626219Swpaul 427287348Srodrigc oldsig = (void (*)(int))signal(SIGALRM, alarm_hndler); 42826219Swpaul saw_alarm = 0; /* global tracking the alarm */ 42926219Swpaul alarm(20); /* only wait 20 seconds */ 43071579Sdeischen res = _connect(s, (struct sockaddr *)&sin, sizeof(sin)); 43126219Swpaul if (res == -1) { 43226219Swpaul msg("failed to connect to tcp endpoint."); 43326219Swpaul goto error; 43426219Swpaul } 43526219Swpaul if (saw_alarm) { 43626219Swpaul msg("alarm caught it, must be unreachable."); 43726219Swpaul goto error; 43826219Swpaul } 43956698Sjasone res = _read(s, (char *)&thetime, sizeof(thetime)); 44026219Swpaul if (res != sizeof(thetime)) { 44126219Swpaul if (saw_alarm) 44226219Swpaul msg("timed out TCP call."); 44326219Swpaul else 44426219Swpaul msg("wrong size of results returned"); 44526219Swpaul 44626219Swpaul goto error; 44726219Swpaul } 44826219Swpaul time_valid = 1; 44926219Swpaul } 45026219Swpaul save = errno; 45156698Sjasone (void)_close(s); 45226219Swpaul errno = save; 45326219Swpaul s = RPC_ANYSOCK; 45426219Swpaul 45526219Swpaul if (time_valid) { 45626219Swpaul thetime = ntohl(thetime); 45726219Swpaul thetime = thetime - TOFFSET; /* adjust to UNIX time */ 45826219Swpaul } else 45926219Swpaul thetime = 0; 46026219Swpaul } 46126219Swpaul 46226219Swpaul gettimeofday(&tv, 0); 46326219Swpaul 46426219Swpaulerror: 46526219Swpaul /* 46626219Swpaul * clean up our allocated data structures. 46726219Swpaul */ 46826219Swpaul 46926219Swpaul if (s != RPC_ANYSOCK) 47056698Sjasone (void)_close(s); 47126219Swpaul 47226219Swpaul if (clnt != NULL) 47326219Swpaul clnt_destroy(clnt); 47426219Swpaul 47526219Swpaul alarm(0); /* reset that alarm if its outstanding */ 47626219Swpaul if (oldsig) { 47726219Swpaul signal(SIGALRM, oldsig); 47826219Swpaul } 47926219Swpaul 48026219Swpaul /* 48126219Swpaul * note, don't free uaddr strings until after we've made a 48226219Swpaul * copy of them. 48326219Swpaul */ 48426219Swpaul if (time_valid) { 48526219Swpaul if (*uaddr == NULL) 48626219Swpaul *uaddr = strdup(useua); 48726219Swpaul 48826219Swpaul /* Round to the nearest second */ 48926219Swpaul tv.tv_sec += (tv.tv_sec > 500000) ? 1 : 0; 49026219Swpaul delta = (thetime > tv.tv_sec) ? thetime - tv.tv_sec : 49126219Swpaul tv.tv_sec - thetime; 49226219Swpaul td->tv_sec = (thetime < tv.tv_sec) ? - delta : delta; 49326219Swpaul td->tv_usec = 0; 49426219Swpaul } else { 49526219Swpaul msg("unable to get the server's time."); 49626219Swpaul } 49726219Swpaul 49826219Swpaul if (needfree) 49926219Swpaul free_eps(teps, tsrv.ep.ep_len); 50026219Swpaul 50126219Swpaul return (time_valid); 50226219Swpaul} 503