auth_time.c revision 74462
126219Swpaul#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 * 2926219Swpaul * NOTE: This code has had the crap beaten out it in order to convert 3026219Swpaul * it from TI-RPC back to TD-RPC for use on FreeBSD. 3155837Sjasone * 3255837Sjasone * $FreeBSD: head/lib/libc/rpc/auth_time.c 74462 2001-03-19 12:50:13Z alfred $ 3326219Swpaul */ 3471579Sdeischen#include "namespace.h" 3526219Swpaul#include <stdio.h> 3626219Swpaul#include <syslog.h> 3726219Swpaul#include <string.h> 3826219Swpaul#include <stdlib.h> 3926219Swpaul#include <unistd.h> 4026219Swpaul#include <netdb.h> 4126219Swpaul#include <sys/signal.h> 4226219Swpaul#include <sys/errno.h> 4326219Swpaul#include <sys/socket.h> 4426219Swpaul#include <netinet/in.h> 4526219Swpaul#include <arpa/inet.h> 4626219Swpaul#include <rpc/rpc.h> 4726219Swpaul#include <rpc/rpc_com.h> 4874462Salfred#include <rpc/rpcb_prot.h> 4926219Swpaul#undef NIS 5026219Swpaul#include <rpcsvc/nis.h> 5171579Sdeischen#include "un-namespace.h" 5226219Swpaul 5326219Swpaul#ifdef TESTING 5426219Swpaul#define msg(x) printf("ERROR: %s\n", x) 5526219Swpaul/* #define msg(x) syslog(LOG_ERR, "%s", x) */ 5626219Swpaul#else 5726219Swpaul#define msg(x) 5826219Swpaul#endif 5926219Swpaul 6026219Swpaulstatic int saw_alarm = 0; 6126219Swpaul 6226219Swpaulstatic void 6326219Swpaulalarm_hndler(s) 6426219Swpaul 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 */ 8526219Swpaulstatic int uaddr_to_sockaddr(uaddr, sin) 8626219Swpaul#ifdef foo 8726219Swpaul endpoint *endpt; 8826219Swpaul#endif 8926219Swpaul char *uaddr; 9026219Swpaul struct sockaddr_in *sin; 9126219Swpaul{ 9226219Swpaul unsigned char p_bytes[2]; 9326219Swpaul int i; 9426219Swpaul unsigned long a[6]; 9526219Swpaul 9626219Swpaul i = sscanf(uaddr, "%lu.%lu.%lu.%lu.%lu.%lu", &a[0], &a[1], &a[2], 9726219Swpaul &a[3], &a[4], &a[5]); 9826219Swpaul 9926219Swpaul if (i < 6) 10026219Swpaul return(1); 10126219Swpaul 10226219Swpaul for (i = 0; i < 4; i++) 10326219Swpaul sin->sin_addr.s_addr |= (a[i] & 0x000000FF) << (8 * i); 10426219Swpaul 10526219Swpaul p_bytes[0] = (unsigned char)a[4] & 0x000000FF; 10626219Swpaul p_bytes[1] = (unsigned char)a[5] & 0x000000FF; 10726219Swpaul 10826219Swpaul sin->sin_family = AF_INET; /* always */ 10926219Swpaul bcopy((char *)&p_bytes, (char *)&sin->sin_port, 2); 11026219Swpaul 11126219Swpaul return (0); 11226219Swpaul} 11326219Swpaul 11426219Swpaul/* 11526219Swpaul * free_eps() 11626219Swpaul * 11726219Swpaul * Free the strings that were strduped into the eps structure. 11826219Swpaul */ 11926219Swpaulstatic void 12026219Swpaulfree_eps(eps, num) 12126219Swpaul endpoint eps[]; 12226219Swpaul int num; 12326219Swpaul{ 12426219Swpaul int i; 12526219Swpaul 12626219Swpaul for (i = 0; i < num; i++) { 12726219Swpaul free(eps[i].uaddr); 12826219Swpaul free(eps[i].proto); 12926219Swpaul free(eps[i].family); 13026219Swpaul } 13126219Swpaul return; 13226219Swpaul} 13326219Swpaul 13426219Swpaul/* 13526219Swpaul * get_server() 13626219Swpaul * 13726219Swpaul * This function constructs a nis_server structure description for the 13826219Swpaul * indicated hostname. 13926219Swpaul * 14026219Swpaul * NOTE: There is a chance we may end up recursing here due to the 14126219Swpaul * fact that gethostbyname() could do an NIS search. Ideally, the 14226219Swpaul * NIS+ server will call __rpc_get_time_offset() with the nis_server 14326219Swpaul * structure already populated. 14426219Swpaul */ 14526219Swpaulstatic nis_server * 14626219Swpaulget_server(sin, host, srv, eps, maxep) 14726219Swpaul struct sockaddr_in *sin; 14826219Swpaul char *host; /* name of the time host */ 14926219Swpaul nis_server *srv; /* nis_server struct to use. */ 15026219Swpaul endpoint eps[]; /* array of endpoints */ 15126219Swpaul int maxep; /* max array size */ 15226219Swpaul{ 15326219Swpaul char hname[256]; 15426219Swpaul int num_ep = 0, i; 15526219Swpaul struct hostent *he; 15626219Swpaul struct hostent dummy; 15726219Swpaul char *ptr[2]; 15826219Swpaul 15926219Swpaul if (host == NULL && sin == NULL) 16026219Swpaul return (NULL); 16126219Swpaul 16226219Swpaul if (sin == NULL) { 16326219Swpaul he = gethostbyname(host); 16426219Swpaul if (he == NULL) 16526219Swpaul return(NULL); 16626219Swpaul } else { 16726219Swpaul he = &dummy; 16826219Swpaul ptr[0] = (char *)&sin->sin_addr.s_addr; 16926219Swpaul ptr[1] = NULL; 17026219Swpaul dummy.h_addr_list = ptr; 17126219Swpaul } 17226219Swpaul 17326219Swpaul /* 17426219Swpaul * This is lame. We go around once for TCP, then again 17526219Swpaul * for UDP. 17626219Swpaul */ 17726219Swpaul for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep); 17826219Swpaul i++, num_ep++) { 17926219Swpaul struct in_addr *a; 18026219Swpaul 18126219Swpaul a = (struct in_addr *)he->h_addr_list[i]; 18226219Swpaul snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a)); 18326219Swpaul eps[num_ep].uaddr = strdup(hname); 18426219Swpaul eps[num_ep].family = strdup("inet"); 18526219Swpaul eps[num_ep].proto = strdup("tcp"); 18626219Swpaul } 18726219Swpaul 18826219Swpaul for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep); 18926219Swpaul i++, num_ep++) { 19026219Swpaul struct in_addr *a; 19126219Swpaul 19226219Swpaul a = (struct in_addr *)he->h_addr_list[i]; 19326219Swpaul snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a)); 19426219Swpaul eps[num_ep].uaddr = strdup(hname); 19526219Swpaul eps[num_ep].family = strdup("inet"); 19626219Swpaul eps[num_ep].proto = strdup("udp"); 19726219Swpaul } 19826219Swpaul 19926219Swpaul srv->name = (nis_name) host; 20026219Swpaul srv->ep.ep_len = num_ep; 20126219Swpaul srv->ep.ep_val = eps; 20226219Swpaul srv->key_type = NIS_PK_NONE; 20326219Swpaul srv->pkey.n_bytes = NULL; 20426219Swpaul srv->pkey.n_len = 0; 20526219Swpaul return (srv); 20626219Swpaul} 20726219Swpaul 20826219Swpaul/* 20926219Swpaul * __rpc_get_time_offset() 21026219Swpaul * 21126219Swpaul * This function uses a nis_server structure to contact the a remote 21226219Swpaul * machine (as named in that structure) and returns the offset in time 21326219Swpaul * between that machine and this one. This offset is returned in seconds 21426219Swpaul * and may be positive or negative. 21526219Swpaul * 21626219Swpaul * The first time through, a lot of fiddling is done with the netconfig 21726219Swpaul * stuff to find a suitable transport. The function is very aggressive 21826219Swpaul * about choosing UDP or at worst TCP if it can. This is because 21926219Swpaul * those transports support both the RCPBIND call and the internet 22026219Swpaul * time service. 22126219Swpaul * 22226219Swpaul * Once through, *uaddr is set to the universal address of 22326219Swpaul * the machine and *netid is set to the local netid for the transport 22426219Swpaul * that uaddr goes with. On the second call, the netconfig stuff 22526219Swpaul * is skipped and the uaddr/netid pair are used to fetch the netconfig 22626219Swpaul * structure and to then contact the machine for the time. 22726219Swpaul * 22826219Swpaul * td = "server" - "client" 22926219Swpaul */ 23026219Swpaulint 23126219Swpaul__rpc_get_time_offset(td, srv, thost, uaddr, netid) 23226219Swpaul struct timeval *td; /* Time difference */ 23326219Swpaul nis_server *srv; /* NIS Server description */ 23426219Swpaul char *thost; /* if no server, this is the timehost */ 23526219Swpaul char **uaddr; /* known universal address */ 23626219Swpaul struct sockaddr_in *netid; /* known network identifier */ 23726219Swpaul{ 23826219Swpaul CLIENT *clnt; /* Client handle */ 23926219Swpaul endpoint *ep, /* useful endpoints */ 24026219Swpaul *useep = NULL; /* endpoint of xp */ 24126219Swpaul char *useua = NULL; /* uaddr of selected xp */ 24226219Swpaul int epl, i; /* counters */ 24326219Swpaul enum clnt_stat status; /* result of clnt_call */ 24426219Swpaul u_long thetime, delta; 24526219Swpaul int needfree = 0; 24626219Swpaul struct timeval tv; 24726219Swpaul int time_valid; 24826219Swpaul int udp_ep = -1, tcp_ep = -1; 24926219Swpaul int a1, a2, a3, a4; 25026219Swpaul char ut[64], ipuaddr[64]; 25126219Swpaul endpoint teps[32]; 25226219Swpaul nis_server tsrv; 25326219Swpaul void (*oldsig)() = NULL; /* old alarm handler */ 25426219Swpaul struct sockaddr_in sin; 25526219Swpaul int s = RPC_ANYSOCK, len; 25626219Swpaul int type = 0; 25726219Swpaul 25826219Swpaul td->tv_sec = 0; 25926219Swpaul td->tv_usec = 0; 26026219Swpaul 26126219Swpaul /* 26226219Swpaul * First check to see if we need to find and address for this 26326219Swpaul * server. 26426219Swpaul */ 26526219Swpaul if (*uaddr == NULL) { 26626219Swpaul if ((srv != NULL) && (thost != NULL)) { 26726219Swpaul msg("both timehost and srv pointer used!"); 26826219Swpaul return (0); 26926219Swpaul } 27026219Swpaul if (! srv) { 27126219Swpaul srv = get_server(netid, thost, &tsrv, teps, 32); 27226219Swpaul if (srv == NULL) { 27326219Swpaul msg("unable to contruct server data."); 27426219Swpaul return (0); 27526219Swpaul } 27626219Swpaul needfree = 1; /* need to free data in endpoints */ 27726219Swpaul } 27826219Swpaul 27926219Swpaul ep = srv->ep.ep_val; 28026219Swpaul epl = srv->ep.ep_len; 28126219Swpaul 28226219Swpaul /* Identify the TCP and UDP endpoints */ 28326219Swpaul for (i = 0; 28426219Swpaul (i < epl) && ((udp_ep == -1) || (tcp_ep == -1)); i++) { 28526219Swpaul if (strcasecmp(ep[i].proto, "udp") == 0) 28626219Swpaul udp_ep = i; 28726219Swpaul if (strcasecmp(ep[i].proto, "tcp") == 0) 28826219Swpaul tcp_ep = i; 28926219Swpaul } 29026219Swpaul 29126219Swpaul /* Check to see if it is UDP or TCP */ 29226219Swpaul if (tcp_ep > -1) { 29326219Swpaul useep = &ep[tcp_ep]; 29426219Swpaul useua = ep[tcp_ep].uaddr; 29526219Swpaul type = SOCK_STREAM; 29626219Swpaul } else if (udp_ep > -1) { 29726219Swpaul useep = &ep[udp_ep]; 29826219Swpaul useua = ep[udp_ep].uaddr; 29926219Swpaul type = SOCK_DGRAM; 30026219Swpaul } 30126219Swpaul 30226219Swpaul if (useep == NULL) { 30326219Swpaul msg("no acceptable transport endpoints."); 30426219Swpaul if (needfree) 30526219Swpaul free_eps(teps, tsrv.ep.ep_len); 30626219Swpaul return (0); 30726219Swpaul } 30826219Swpaul } 30926219Swpaul 31026219Swpaul /* 31126219Swpaul * Create a sockaddr from the uaddr. 31226219Swpaul */ 31326219Swpaul if (*uaddr != NULL) 31426219Swpaul useua = *uaddr; 31526219Swpaul 31626219Swpaul /* Fixup test for NIS+ */ 31726219Swpaul sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4); 31826219Swpaul sprintf(ipuaddr, "%d.%d.%d.%d.0.111", a1, a2, a3, a4); 31926219Swpaul useua = &ipuaddr[0]; 32026219Swpaul 32126666Swpaul bzero((char *)&sin, sizeof(sin)); 32226219Swpaul if (uaddr_to_sockaddr(useua, &sin)) { 32326219Swpaul msg("unable to translate uaddr to sockaddr."); 32426219Swpaul if (needfree) 32526219Swpaul free_eps(teps, tsrv.ep.ep_len); 32626219Swpaul return (0); 32726219Swpaul } 32826219Swpaul 32926219Swpaul /* 33026219Swpaul * Create the client handle to rpcbind. Note we always try 33126219Swpaul * version 3 since that is the earliest version that supports 33226219Swpaul * the RPCB_GETTIME call. Also it is the version that comes 33326219Swpaul * standard with SVR4. Since most everyone supports TCP/IP 33426219Swpaul * we could consider trying the rtime call first. 33526219Swpaul */ 33626219Swpaul clnt = clnttcp_create(&sin, RPCBPROG, RPCBVERS, &s, 0, 0); 33726219Swpaul if (clnt == NULL) { 33826219Swpaul msg("unable to create client handle to rpcbind."); 33926219Swpaul if (needfree) 34026219Swpaul free_eps(teps, tsrv.ep.ep_len); 34126219Swpaul return (0); 34226219Swpaul } 34326219Swpaul 34426219Swpaul tv.tv_sec = 5; 34526219Swpaul tv.tv_usec = 0; 34626219Swpaul time_valid = 0; 34726219Swpaul status = clnt_call(clnt, RPCBPROC_GETTIME, xdr_void, NULL, 34826219Swpaul xdr_u_long, (char *)&thetime, tv); 34926219Swpaul /* 35026219Swpaul * The only error we check for is anything but success. In 35126219Swpaul * fact we could have seen PROGMISMATCH if talking to a 4.1 35226219Swpaul * machine (pmap v2) or TIMEDOUT if the net was busy. 35326219Swpaul */ 35426219Swpaul if (status == RPC_SUCCESS) 35526219Swpaul time_valid = 1; 35626219Swpaul else { 35726219Swpaul int save; 35826219Swpaul 35926219Swpaul /* Blow away possible stale CLNT handle. */ 36026219Swpaul if (clnt != NULL) { 36126219Swpaul clnt_destroy(clnt); 36226219Swpaul clnt = NULL; 36326219Swpaul } 36426219Swpaul 36526219Swpaul /* 36626219Swpaul * Convert PMAP address into timeservice address 36726219Swpaul * We take advantage of the fact that we "know" what 36826219Swpaul * the universal address looks like for inet transports. 36926219Swpaul * 37026219Swpaul * We also know that the internet timeservice is always 37126219Swpaul * listening on port 37. 37226219Swpaul */ 37326219Swpaul sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4); 37426219Swpaul sprintf(ut, "%d.%d.%d.%d.0.37", a1, a2, a3, a4); 37526219Swpaul 37626219Swpaul if (uaddr_to_sockaddr(ut, &sin)) { 37726219Swpaul msg("cannot convert timeservice uaddr to sockaddr."); 37826219Swpaul goto error; 37926219Swpaul } 38026219Swpaul 38171579Sdeischen s = _socket(AF_INET, type, 0); 38226219Swpaul if (s == -1) { 38326219Swpaul msg("unable to open fd to network."); 38426219Swpaul goto error; 38526219Swpaul } 38626219Swpaul 38726219Swpaul /* 38826219Swpaul * Now depending on whether or not we're talking to 38926219Swpaul * UDP we set a timeout or not. 39026219Swpaul */ 39126219Swpaul if (type == SOCK_DGRAM) { 39226219Swpaul struct timeval timeout = { 20, 0 }; 39326219Swpaul struct sockaddr_in from; 39426219Swpaul fd_set readfds; 39526219Swpaul int res; 39626219Swpaul 39771579Sdeischen if (_sendto(s, &thetime, sizeof(thetime), 0, 39826219Swpaul (struct sockaddr *)&sin, sizeof(sin)) == -1) { 39926219Swpaul msg("udp : sendto failed."); 40026219Swpaul goto error; 40126219Swpaul } 40226219Swpaul do { 40326219Swpaul FD_ZERO(&readfds); 40426219Swpaul FD_SET(s, &readfds); 40571579Sdeischen res = _select(_rpc_dtablesize(), &readfds, 40626219Swpaul (fd_set *)NULL, (fd_set *)NULL, &timeout); 40726219Swpaul } while (res < 0 && errno == EINTR); 40826219Swpaul if (res <= 0) 40926219Swpaul goto error; 41026219Swpaul len = sizeof(from); 41171579Sdeischen res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0, 41226219Swpaul (struct sockaddr *)&from, &len); 41326219Swpaul if (res == -1) { 41426219Swpaul msg("recvfrom failed on udp transport."); 41526219Swpaul goto error; 41626219Swpaul } 41726219Swpaul time_valid = 1; 41826219Swpaul } else { 41926219Swpaul int res; 42026219Swpaul 42126219Swpaul oldsig = (void (*)())signal(SIGALRM, alarm_hndler); 42226219Swpaul saw_alarm = 0; /* global tracking the alarm */ 42326219Swpaul alarm(20); /* only wait 20 seconds */ 42471579Sdeischen res = _connect(s, (struct sockaddr *)&sin, sizeof(sin)); 42526219Swpaul if (res == -1) { 42626219Swpaul msg("failed to connect to tcp endpoint."); 42726219Swpaul goto error; 42826219Swpaul } 42926219Swpaul if (saw_alarm) { 43026219Swpaul msg("alarm caught it, must be unreachable."); 43126219Swpaul goto error; 43226219Swpaul } 43356698Sjasone res = _read(s, (char *)&thetime, sizeof(thetime)); 43426219Swpaul if (res != sizeof(thetime)) { 43526219Swpaul if (saw_alarm) 43626219Swpaul msg("timed out TCP call."); 43726219Swpaul else 43826219Swpaul msg("wrong size of results returned"); 43926219Swpaul 44026219Swpaul goto error; 44126219Swpaul } 44226219Swpaul time_valid = 1; 44326219Swpaul } 44426219Swpaul save = errno; 44556698Sjasone (void)_close(s); 44626219Swpaul errno = save; 44726219Swpaul s = RPC_ANYSOCK; 44826219Swpaul 44926219Swpaul if (time_valid) { 45026219Swpaul thetime = ntohl(thetime); 45126219Swpaul thetime = thetime - TOFFSET; /* adjust to UNIX time */ 45226219Swpaul } else 45326219Swpaul thetime = 0; 45426219Swpaul } 45526219Swpaul 45626219Swpaul gettimeofday(&tv, 0); 45726219Swpaul 45826219Swpaulerror: 45926219Swpaul /* 46026219Swpaul * clean up our allocated data structures. 46126219Swpaul */ 46226219Swpaul 46326219Swpaul if (s != RPC_ANYSOCK) 46456698Sjasone (void)_close(s); 46526219Swpaul 46626219Swpaul if (clnt != NULL) 46726219Swpaul clnt_destroy(clnt); 46826219Swpaul 46926219Swpaul alarm(0); /* reset that alarm if its outstanding */ 47026219Swpaul if (oldsig) { 47126219Swpaul signal(SIGALRM, oldsig); 47226219Swpaul } 47326219Swpaul 47426219Swpaul /* 47526219Swpaul * note, don't free uaddr strings until after we've made a 47626219Swpaul * copy of them. 47726219Swpaul */ 47826219Swpaul if (time_valid) { 47926219Swpaul if (*uaddr == NULL) 48026219Swpaul *uaddr = strdup(useua); 48126219Swpaul 48226219Swpaul /* Round to the nearest second */ 48326219Swpaul tv.tv_sec += (tv.tv_sec > 500000) ? 1 : 0; 48426219Swpaul delta = (thetime > tv.tv_sec) ? thetime - tv.tv_sec : 48526219Swpaul tv.tv_sec - thetime; 48626219Swpaul td->tv_sec = (thetime < tv.tv_sec) ? - delta : delta; 48726219Swpaul td->tv_usec = 0; 48826219Swpaul } else { 48926219Swpaul msg("unable to get the server's time."); 49026219Swpaul } 49126219Swpaul 49226219Swpaul if (needfree) 49326219Swpaul free_eps(teps, tsrv.ep.ep_len); 49426219Swpaul 49526219Swpaul return (time_valid); 49626219Swpaul} 497