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