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$"); 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 6426219Swpaulalarm_hndler(s) 6526219Swpaul int s; 6626219Swpaul{ 6726219Swpaul saw_alarm = 1; 6826219Swpaul return; 6926219Swpaul} 7026219Swpaul 7126219Swpaul/* 7226219Swpaul * The internet time server defines the epoch to be Jan 1, 1900 7326219Swpaul * whereas UNIX defines it to be Jan 1, 1970. To adjust the result 7426219Swpaul * from internet time-service time, into UNIX time we subtract the 7526219Swpaul * following offset : 7626219Swpaul */ 7726219Swpaul#define NYEARS (1970 - 1900) 7826219Swpaul#define TOFFSET ((u_long)60*60*24*(365*NYEARS + (NYEARS/4))) 7926219Swpaul 8026219Swpaul 8126219Swpaul/* 8226219Swpaul * Stolen from rpc.nisd: 8326219Swpaul * Turn a 'universal address' into a struct sockaddr_in. 8426219Swpaul * Bletch. 8526219Swpaul */ 8626219Swpaulstatic int uaddr_to_sockaddr(uaddr, sin) 8726219Swpaul#ifdef foo 8826219Swpaul endpoint *endpt; 8926219Swpaul#endif 9026219Swpaul char *uaddr; 9126219Swpaul struct sockaddr_in *sin; 9226219Swpaul{ 9326219Swpaul unsigned char p_bytes[2]; 9426219Swpaul int i; 9526219Swpaul unsigned long a[6]; 9626219Swpaul 9726219Swpaul i = sscanf(uaddr, "%lu.%lu.%lu.%lu.%lu.%lu", &a[0], &a[1], &a[2], 9826219Swpaul &a[3], &a[4], &a[5]); 9926219Swpaul 10026219Swpaul if (i < 6) 10126219Swpaul return(1); 10226219Swpaul 10326219Swpaul for (i = 0; i < 4; i++) 10426219Swpaul sin->sin_addr.s_addr |= (a[i] & 0x000000FF) << (8 * i); 10526219Swpaul 10626219Swpaul p_bytes[0] = (unsigned char)a[4] & 0x000000FF; 10726219Swpaul p_bytes[1] = (unsigned char)a[5] & 0x000000FF; 10826219Swpaul 10926219Swpaul sin->sin_family = AF_INET; /* always */ 11026219Swpaul bcopy((char *)&p_bytes, (char *)&sin->sin_port, 2); 11126219Swpaul 11226219Swpaul return (0); 11326219Swpaul} 11426219Swpaul 11526219Swpaul/* 11626219Swpaul * free_eps() 11726219Swpaul * 11826219Swpaul * Free the strings that were strduped into the eps structure. 11926219Swpaul */ 12026219Swpaulstatic void 12126219Swpaulfree_eps(eps, num) 12226219Swpaul endpoint eps[]; 12326219Swpaul int num; 12426219Swpaul{ 12526219Swpaul int i; 12626219Swpaul 12726219Swpaul for (i = 0; i < num; i++) { 12826219Swpaul free(eps[i].uaddr); 12926219Swpaul free(eps[i].proto); 13026219Swpaul free(eps[i].family); 13126219Swpaul } 13226219Swpaul return; 13326219Swpaul} 13426219Swpaul 13526219Swpaul/* 13626219Swpaul * get_server() 13726219Swpaul * 13826219Swpaul * This function constructs a nis_server structure description for the 13926219Swpaul * indicated hostname. 14026219Swpaul * 14126219Swpaul * NOTE: There is a chance we may end up recursing here due to the 14226219Swpaul * fact that gethostbyname() could do an NIS search. Ideally, the 14326219Swpaul * NIS+ server will call __rpc_get_time_offset() with the nis_server 14426219Swpaul * structure already populated. 14526219Swpaul */ 14626219Swpaulstatic nis_server * 14726219Swpaulget_server(sin, host, srv, eps, maxep) 14826219Swpaul struct sockaddr_in *sin; 14926219Swpaul char *host; /* name of the time host */ 15026219Swpaul nis_server *srv; /* nis_server struct to use. */ 15126219Swpaul endpoint eps[]; /* array of endpoints */ 15226219Swpaul int maxep; /* max array size */ 15326219Swpaul{ 15426219Swpaul char hname[256]; 15526219Swpaul int num_ep = 0, i; 15626219Swpaul struct hostent *he; 15726219Swpaul struct hostent dummy; 15826219Swpaul char *ptr[2]; 159172259Smatteo endpoint *ep; 16026219Swpaul 16126219Swpaul if (host == NULL && sin == NULL) 16226219Swpaul return (NULL); 16326219Swpaul 16426219Swpaul if (sin == NULL) { 16526219Swpaul he = gethostbyname(host); 16626219Swpaul if (he == NULL) 16726219Swpaul return(NULL); 16826219Swpaul } else { 16926219Swpaul he = &dummy; 17026219Swpaul ptr[0] = (char *)&sin->sin_addr.s_addr; 17126219Swpaul ptr[1] = NULL; 17226219Swpaul dummy.h_addr_list = ptr; 17326219Swpaul } 17426219Swpaul 17526219Swpaul /* 17626219Swpaul * This is lame. We go around once for TCP, then again 17726219Swpaul * for UDP. 17826219Swpaul */ 179172259Smatteo for (i = 0, ep = eps; (he->h_addr_list[i] != NULL) && (num_ep < maxep); 180172259Smatteo i++, ep++, num_ep++) { 18126219Swpaul struct in_addr *a; 18226219Swpaul 18326219Swpaul a = (struct in_addr *)he->h_addr_list[i]; 18426219Swpaul snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a)); 185172259Smatteo ep->uaddr = strdup(hname); 186172259Smatteo ep->family = strdup("inet"); 187172259Smatteo ep->proto = strdup("tcp"); 188172259Smatteo if (ep->uaddr == NULL || ep->family == NULL || ep->proto == NULL) { 189172259Smatteo free_eps(eps, num_ep + 1); 190172259Smatteo return (NULL); 191172259Smatteo } 19226219Swpaul } 19326219Swpaul 19426219Swpaul for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep); 195172259Smatteo i++, ep++, num_ep++) { 19626219Swpaul struct in_addr *a; 19726219Swpaul 19826219Swpaul a = (struct in_addr *)he->h_addr_list[i]; 19926219Swpaul snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a)); 200172259Smatteo ep->uaddr = strdup(hname); 201172259Smatteo ep->family = strdup("inet"); 202172259Smatteo ep->proto = strdup("udp"); 203172259Smatteo if (ep->uaddr == NULL || ep->family == NULL || ep->proto == NULL) { 204172259Smatteo free_eps(eps, num_ep + 1); 205172259Smatteo return (NULL); 206172259Smatteo } 20726219Swpaul } 20826219Swpaul 20926219Swpaul srv->name = (nis_name) host; 21026219Swpaul srv->ep.ep_len = num_ep; 21126219Swpaul srv->ep.ep_val = eps; 21226219Swpaul srv->key_type = NIS_PK_NONE; 21326219Swpaul srv->pkey.n_bytes = NULL; 21426219Swpaul srv->pkey.n_len = 0; 21526219Swpaul return (srv); 21626219Swpaul} 21726219Swpaul 21826219Swpaul/* 21926219Swpaul * __rpc_get_time_offset() 22026219Swpaul * 22126219Swpaul * This function uses a nis_server structure to contact the a remote 22226219Swpaul * machine (as named in that structure) and returns the offset in time 22326219Swpaul * between that machine and this one. This offset is returned in seconds 22426219Swpaul * and may be positive or negative. 22526219Swpaul * 22626219Swpaul * The first time through, a lot of fiddling is done with the netconfig 22726219Swpaul * stuff to find a suitable transport. The function is very aggressive 22826219Swpaul * about choosing UDP or at worst TCP if it can. This is because 22926219Swpaul * those transports support both the RCPBIND call and the internet 23026219Swpaul * time service. 23126219Swpaul * 23226219Swpaul * Once through, *uaddr is set to the universal address of 23326219Swpaul * the machine and *netid is set to the local netid for the transport 23426219Swpaul * that uaddr goes with. On the second call, the netconfig stuff 23526219Swpaul * is skipped and the uaddr/netid pair are used to fetch the netconfig 23626219Swpaul * structure and to then contact the machine for the time. 23726219Swpaul * 23826219Swpaul * td = "server" - "client" 23926219Swpaul */ 24026219Swpaulint 24126219Swpaul__rpc_get_time_offset(td, srv, thost, uaddr, netid) 24226219Swpaul struct timeval *td; /* Time difference */ 24326219Swpaul nis_server *srv; /* NIS Server description */ 24426219Swpaul char *thost; /* if no server, this is the timehost */ 24526219Swpaul char **uaddr; /* known universal address */ 24626219Swpaul struct sockaddr_in *netid; /* known network identifier */ 24726219Swpaul{ 24826219Swpaul CLIENT *clnt; /* Client handle */ 24926219Swpaul endpoint *ep, /* useful endpoints */ 25026219Swpaul *useep = NULL; /* endpoint of xp */ 25126219Swpaul char *useua = NULL; /* uaddr of selected xp */ 25226219Swpaul int epl, i; /* counters */ 25326219Swpaul enum clnt_stat status; /* result of clnt_call */ 25426219Swpaul u_long thetime, delta; 25526219Swpaul int needfree = 0; 25626219Swpaul struct timeval tv; 25726219Swpaul int time_valid; 25826219Swpaul int udp_ep = -1, tcp_ep = -1; 25926219Swpaul int a1, a2, a3, a4; 26026219Swpaul char ut[64], ipuaddr[64]; 26126219Swpaul endpoint teps[32]; 26226219Swpaul nis_server tsrv; 26326219Swpaul void (*oldsig)() = NULL; /* old alarm handler */ 26426219Swpaul struct sockaddr_in sin; 265143344Sstefanf socklen_t len; 266143344Sstefanf int s = RPC_ANYSOCK; 26726219Swpaul int type = 0; 26826219Swpaul 26926219Swpaul td->tv_sec = 0; 27026219Swpaul td->tv_usec = 0; 27126219Swpaul 27226219Swpaul /* 27326219Swpaul * First check to see if we need to find and address for this 27426219Swpaul * server. 27526219Swpaul */ 27626219Swpaul if (*uaddr == NULL) { 27726219Swpaul if ((srv != NULL) && (thost != NULL)) { 27826219Swpaul msg("both timehost and srv pointer used!"); 27926219Swpaul return (0); 28026219Swpaul } 28126219Swpaul if (! srv) { 28226219Swpaul srv = get_server(netid, thost, &tsrv, teps, 32); 28326219Swpaul if (srv == NULL) { 28426219Swpaul msg("unable to contruct server data."); 28526219Swpaul return (0); 28626219Swpaul } 28726219Swpaul needfree = 1; /* need to free data in endpoints */ 28826219Swpaul } 28926219Swpaul 29026219Swpaul ep = srv->ep.ep_val; 29126219Swpaul epl = srv->ep.ep_len; 29226219Swpaul 29326219Swpaul /* Identify the TCP and UDP endpoints */ 29426219Swpaul for (i = 0; 29526219Swpaul (i < epl) && ((udp_ep == -1) || (tcp_ep == -1)); i++) { 29626219Swpaul if (strcasecmp(ep[i].proto, "udp") == 0) 29726219Swpaul udp_ep = i; 29826219Swpaul if (strcasecmp(ep[i].proto, "tcp") == 0) 29926219Swpaul tcp_ep = i; 30026219Swpaul } 30126219Swpaul 30226219Swpaul /* Check to see if it is UDP or TCP */ 30326219Swpaul if (tcp_ep > -1) { 30426219Swpaul useep = &ep[tcp_ep]; 30526219Swpaul useua = ep[tcp_ep].uaddr; 30626219Swpaul type = SOCK_STREAM; 30726219Swpaul } else if (udp_ep > -1) { 30826219Swpaul useep = &ep[udp_ep]; 30926219Swpaul useua = ep[udp_ep].uaddr; 31026219Swpaul type = SOCK_DGRAM; 31126219Swpaul } 31226219Swpaul 31326219Swpaul if (useep == NULL) { 31426219Swpaul msg("no acceptable transport endpoints."); 31526219Swpaul if (needfree) 31626219Swpaul free_eps(teps, tsrv.ep.ep_len); 31726219Swpaul return (0); 31826219Swpaul } 31926219Swpaul } 32026219Swpaul 32126219Swpaul /* 32226219Swpaul * Create a sockaddr from the uaddr. 32326219Swpaul */ 32426219Swpaul if (*uaddr != NULL) 32526219Swpaul useua = *uaddr; 32626219Swpaul 32726219Swpaul /* Fixup test for NIS+ */ 32826219Swpaul sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4); 32926219Swpaul sprintf(ipuaddr, "%d.%d.%d.%d.0.111", a1, a2, a3, a4); 33026219Swpaul useua = &ipuaddr[0]; 33126219Swpaul 33226666Swpaul bzero((char *)&sin, sizeof(sin)); 33326219Swpaul if (uaddr_to_sockaddr(useua, &sin)) { 33426219Swpaul msg("unable to translate uaddr to sockaddr."); 33526219Swpaul if (needfree) 33626219Swpaul free_eps(teps, tsrv.ep.ep_len); 33726219Swpaul return (0); 33826219Swpaul } 33926219Swpaul 34026219Swpaul /* 34126219Swpaul * Create the client handle to rpcbind. Note we always try 34226219Swpaul * version 3 since that is the earliest version that supports 34326219Swpaul * the RPCB_GETTIME call. Also it is the version that comes 34426219Swpaul * standard with SVR4. Since most everyone supports TCP/IP 34526219Swpaul * we could consider trying the rtime call first. 34626219Swpaul */ 34726219Swpaul clnt = clnttcp_create(&sin, RPCBPROG, RPCBVERS, &s, 0, 0); 34826219Swpaul if (clnt == NULL) { 34926219Swpaul msg("unable to create client handle to rpcbind."); 35026219Swpaul if (needfree) 35126219Swpaul free_eps(teps, tsrv.ep.ep_len); 35226219Swpaul return (0); 35326219Swpaul } 35426219Swpaul 35526219Swpaul tv.tv_sec = 5; 35626219Swpaul tv.tv_usec = 0; 35726219Swpaul time_valid = 0; 35895658Sdes status = clnt_call(clnt, RPCBPROC_GETTIME, (xdrproc_t)xdr_void, NULL, 35995658Sdes (xdrproc_t)xdr_u_long, &thetime, tv); 36026219Swpaul /* 36126219Swpaul * The only error we check for is anything but success. In 36226219Swpaul * fact we could have seen PROGMISMATCH if talking to a 4.1 36326219Swpaul * machine (pmap v2) or TIMEDOUT if the net was busy. 36426219Swpaul */ 36526219Swpaul if (status == RPC_SUCCESS) 36626219Swpaul time_valid = 1; 36726219Swpaul else { 36826219Swpaul int save; 36926219Swpaul 37026219Swpaul /* Blow away possible stale CLNT handle. */ 37126219Swpaul if (clnt != NULL) { 37226219Swpaul clnt_destroy(clnt); 37326219Swpaul clnt = NULL; 37426219Swpaul } 37526219Swpaul 37626219Swpaul /* 37726219Swpaul * Convert PMAP address into timeservice address 37826219Swpaul * We take advantage of the fact that we "know" what 37926219Swpaul * the universal address looks like for inet transports. 38026219Swpaul * 38126219Swpaul * We also know that the internet timeservice is always 38226219Swpaul * listening on port 37. 38326219Swpaul */ 38426219Swpaul sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4); 38526219Swpaul sprintf(ut, "%d.%d.%d.%d.0.37", a1, a2, a3, a4); 38626219Swpaul 38726219Swpaul if (uaddr_to_sockaddr(ut, &sin)) { 38826219Swpaul msg("cannot convert timeservice uaddr to sockaddr."); 38926219Swpaul goto error; 39026219Swpaul } 39126219Swpaul 39271579Sdeischen s = _socket(AF_INET, type, 0); 39326219Swpaul if (s == -1) { 39426219Swpaul msg("unable to open fd to network."); 39526219Swpaul goto error; 39626219Swpaul } 39726219Swpaul 39826219Swpaul /* 39926219Swpaul * Now depending on whether or not we're talking to 40026219Swpaul * UDP we set a timeout or not. 40126219Swpaul */ 40226219Swpaul if (type == SOCK_DGRAM) { 40326219Swpaul struct timeval timeout = { 20, 0 }; 40426219Swpaul struct sockaddr_in from; 40526219Swpaul fd_set readfds; 40626219Swpaul int res; 40726219Swpaul 40871579Sdeischen if (_sendto(s, &thetime, sizeof(thetime), 0, 40926219Swpaul (struct sockaddr *)&sin, sizeof(sin)) == -1) { 41026219Swpaul msg("udp : sendto failed."); 41126219Swpaul goto error; 41226219Swpaul } 41326219Swpaul do { 41426219Swpaul FD_ZERO(&readfds); 41526219Swpaul FD_SET(s, &readfds); 41671579Sdeischen res = _select(_rpc_dtablesize(), &readfds, 41726219Swpaul (fd_set *)NULL, (fd_set *)NULL, &timeout); 41826219Swpaul } while (res < 0 && errno == EINTR); 41926219Swpaul if (res <= 0) 42026219Swpaul goto error; 42126219Swpaul len = sizeof(from); 42271579Sdeischen res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0, 42326219Swpaul (struct sockaddr *)&from, &len); 42426219Swpaul if (res == -1) { 42526219Swpaul msg("recvfrom failed on udp transport."); 42626219Swpaul goto error; 42726219Swpaul } 42826219Swpaul time_valid = 1; 42926219Swpaul } else { 43026219Swpaul int res; 43126219Swpaul 43226219Swpaul oldsig = (void (*)())signal(SIGALRM, alarm_hndler); 43326219Swpaul saw_alarm = 0; /* global tracking the alarm */ 43426219Swpaul alarm(20); /* only wait 20 seconds */ 43571579Sdeischen res = _connect(s, (struct sockaddr *)&sin, sizeof(sin)); 43626219Swpaul if (res == -1) { 43726219Swpaul msg("failed to connect to tcp endpoint."); 43826219Swpaul goto error; 43926219Swpaul } 44026219Swpaul if (saw_alarm) { 44126219Swpaul msg("alarm caught it, must be unreachable."); 44226219Swpaul goto error; 44326219Swpaul } 44456698Sjasone res = _read(s, (char *)&thetime, sizeof(thetime)); 44526219Swpaul if (res != sizeof(thetime)) { 44626219Swpaul if (saw_alarm) 44726219Swpaul msg("timed out TCP call."); 44826219Swpaul else 44926219Swpaul msg("wrong size of results returned"); 45026219Swpaul 45126219Swpaul goto error; 45226219Swpaul } 45326219Swpaul time_valid = 1; 45426219Swpaul } 45526219Swpaul save = errno; 45656698Sjasone (void)_close(s); 45726219Swpaul errno = save; 45826219Swpaul s = RPC_ANYSOCK; 45926219Swpaul 46026219Swpaul if (time_valid) { 46126219Swpaul thetime = ntohl(thetime); 46226219Swpaul thetime = thetime - TOFFSET; /* adjust to UNIX time */ 46326219Swpaul } else 46426219Swpaul thetime = 0; 46526219Swpaul } 46626219Swpaul 46726219Swpaul gettimeofday(&tv, 0); 46826219Swpaul 46926219Swpaulerror: 47026219Swpaul /* 47126219Swpaul * clean up our allocated data structures. 47226219Swpaul */ 47326219Swpaul 47426219Swpaul if (s != RPC_ANYSOCK) 47556698Sjasone (void)_close(s); 47626219Swpaul 47726219Swpaul if (clnt != NULL) 47826219Swpaul clnt_destroy(clnt); 47926219Swpaul 48026219Swpaul alarm(0); /* reset that alarm if its outstanding */ 48126219Swpaul if (oldsig) { 48226219Swpaul signal(SIGALRM, oldsig); 48326219Swpaul } 48426219Swpaul 48526219Swpaul /* 48626219Swpaul * note, don't free uaddr strings until after we've made a 48726219Swpaul * copy of them. 48826219Swpaul */ 48926219Swpaul if (time_valid) { 49026219Swpaul if (*uaddr == NULL) 49126219Swpaul *uaddr = strdup(useua); 49226219Swpaul 49326219Swpaul /* Round to the nearest second */ 49426219Swpaul tv.tv_sec += (tv.tv_sec > 500000) ? 1 : 0; 49526219Swpaul delta = (thetime > tv.tv_sec) ? thetime - tv.tv_sec : 49626219Swpaul tv.tv_sec - thetime; 49726219Swpaul td->tv_sec = (thetime < tv.tv_sec) ? - delta : delta; 49826219Swpaul td->tv_usec = 0; 49926219Swpaul } else { 50026219Swpaul msg("unable to get the server's time."); 50126219Swpaul } 50226219Swpaul 50326219Swpaul if (needfree) 50426219Swpaul free_eps(teps, tsrv.ep.ep_len); 50526219Swpaul 50626219Swpaul return (time_valid); 50726219Swpaul} 508