| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#ifdef HAVE_CONFIG_H |
| 29 |
#include "config.h" |
| 30 |
#endif |
| 31 |
#include <stdio.h> |
| 32 |
#include <syslog.h> |
| 33 |
#include <string.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <unistd.h> |
| 36 |
#include <netdb.h> |
| 37 |
#include <signal.h> |
| 38 |
#include <errno.h> |
| 39 |
#include <sys/socket.h> |
| 40 |
#include <netinet/in.h> |
| 41 |
#include <arpa/inet.h> |
| 42 |
#include <rpc/rpc.h> |
| 43 |
#include <rpc/rpc_com.h> |
| 44 |
#include <rpc/rpcb_prot.h> |
| 45 |
|
| 46 |
#include <sys/select.h> |
| 47 |
|
| 48 |
#include "nis.h" |
| 49 |
|
| 50 |
|
| 51 |
#ifdef TESTING |
| 52 |
#define msg(x) printf("ERROR: %s\n", x) |
| 53 |
|
| 54 |
#else |
| 55 |
#define msg(x) |
| 56 |
#endif |
| 57 |
|
| 58 |
static int saw_alarm = 0; |
| 59 |
|
| 60 |
static void |
| 61 |
alarm_hndler(s) |
| 62 |
int s; |
| 63 |
{ |
| 64 |
saw_alarm = 1; |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#define NYEARS (1970 - 1900) |
| 75 |
#define TOFFSET ((u_long)60*60*24*(365*NYEARS + (NYEARS/4))) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
static int uaddr_to_sockaddr(uaddr, sin) |
| 84 |
#ifdef foo |
| 85 |
endpoint *endpt; |
| 86 |
#endif |
| 87 |
char *uaddr; |
| 88 |
struct sockaddr_in *sin; |
| 89 |
{ |
| 90 |
unsigned char p_bytes[2]; |
| 91 |
int i; |
| 92 |
unsigned long a[6]; |
| 93 |
|
| 94 |
i = sscanf(uaddr, "%lu.%lu.%lu.%lu.%lu.%lu", &a[0], &a[1], &a[2], |
| 95 |
&a[3], &a[4], &a[5]); |
| 96 |
|
| 97 |
if (i < 6) |
| 98 |
return(1); |
| 99 |
|
| 100 |
for (i = 0; i < 4; i++) |
| 101 |
sin->sin_addr.s_addr |= (a[i] & 0x000000FF) << (8 * i); |
| 102 |
|
| 103 |
p_bytes[0] = (unsigned char)a[4] & 0x000000FF; |
| 104 |
p_bytes[1] = (unsigned char)a[5] & 0x000000FF; |
| 105 |
|
| 106 |
sin->sin_family = AF_INET; |
| 107 |
memcpy((char *)&sin->sin_port, (char *)&p_bytes, 2); |
| 108 |
|
| 109 |
return (0); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static void |
| 118 |
free_eps(eps, num) |
| 119 |
endpoint eps[]; |
| 120 |
int num; |
| 121 |
{ |
| 122 |
int i; |
| 123 |
|
| 124 |
for (i = 0; i < num; i++) { |
| 125 |
free(eps[i].uaddr); |
| 126 |
free(eps[i].proto); |
| 127 |
free(eps[i].family); |
| 128 |
} |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
static nis_server * |
| 144 |
get_server(sin, host, srv, eps, maxep) |
| 145 |
struct sockaddr_in *sin; |
| 146 |
char *host; |
| 147 |
nis_server *srv; |
| 148 |
endpoint eps[]; |
| 149 |
int maxep; |
| 150 |
{ |
| 151 |
char hname[256]; |
| 152 |
int num_ep = 0, i; |
| 153 |
struct hostent *he; |
| 154 |
struct hostent dummy; |
| 155 |
char *ptr[2]; |
| 156 |
|
| 157 |
if (host == NULL && sin == NULL) |
| 158 |
return (NULL); |
| 159 |
|
| 160 |
if (sin == NULL) { |
| 161 |
he = gethostbyname(host); |
| 162 |
if (he == NULL) |
| 163 |
return(NULL); |
| 164 |
} else { |
| 165 |
he = &dummy; |
| 166 |
ptr[0] = (char *)&sin->sin_addr.s_addr; |
| 167 |
ptr[1] = NULL; |
| 168 |
dummy.h_addr_list = ptr; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep); |
| 176 |
i++, num_ep++) { |
| 177 |
struct in_addr *a; |
| 178 |
|
| 179 |
a = (struct in_addr *)he->h_addr_list[i]; |
| 180 |
snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a)); |
| 181 |
eps[num_ep].uaddr = strdup(hname); |
| 182 |
eps[num_ep].family = strdup("inet"); |
| 183 |
eps[num_ep].proto = strdup("tcp"); |
| 184 |
} |
| 185 |
|
| 186 |
for (i = 0; (he->h_addr_list[i] != NULL) && (num_ep < maxep); |
| 187 |
i++, num_ep++) { |
| 188 |
struct in_addr *a; |
| 189 |
|
| 190 |
a = (struct in_addr *)he->h_addr_list[i]; |
| 191 |
snprintf(hname, sizeof(hname), "%s.0.111", inet_ntoa(*a)); |
| 192 |
eps[num_ep].uaddr = strdup(hname); |
| 193 |
eps[num_ep].family = strdup("inet"); |
| 194 |
eps[num_ep].proto = strdup("udp"); |
| 195 |
} |
| 196 |
|
| 197 |
srv->name = (nis_name) host; |
| 198 |
srv->ep.ep_len = num_ep; |
| 199 |
srv->ep.ep_val = eps; |
| 200 |
srv->key_type = NIS_PK_NONE; |
| 201 |
srv->pkey.n_bytes = NULL; |
| 202 |
srv->pkey.n_len = 0; |
| 203 |
return (srv); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
int |
| 229 |
__rpc_get_time_offset(td, srv, thost, uaddr, netid) |
| 230 |
struct timeval *td; |
| 231 |
nis_server *srv; |
| 232 |
char *thost; |
| 233 |
char **uaddr; |
| 234 |
struct sockaddr_in *netid; |
| 235 |
{ |
| 236 |
CLIENT *clnt; |
| 237 |
endpoint *ep, |
| 238 |
*useep = NULL; |
| 239 |
char *useua = NULL; |
| 240 |
int epl, i; |
| 241 |
enum clnt_stat status; |
| 242 |
u_long thetime, delta; |
| 243 |
int needfree = 0; |
| 244 |
struct timeval tv; |
| 245 |
int time_valid; |
| 246 |
int udp_ep = -1, tcp_ep = -1; |
| 247 |
int a1, a2, a3, a4; |
| 248 |
char ut[64], ipuaddr[64]; |
| 249 |
endpoint teps[32]; |
| 250 |
nis_server tsrv; |
| 251 |
void (*oldsig)() = NULL; |
| 252 |
struct sockaddr_in sin; |
| 253 |
int s = RPC_ANYSOCK; |
| 254 |
socklen_t len; |
| 255 |
int type = 0; |
| 256 |
|
| 257 |
td->tv_sec = 0; |
| 258 |
td->tv_usec = 0; |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
if (*uaddr == NULL) { |
| 265 |
if ((srv != NULL) && (thost != NULL)) { |
| 266 |
msg("both timehost and srv pointer used!"); |
| 267 |
return (0); |
| 268 |
} |
| 269 |
if (! srv) { |
| 270 |
srv = get_server(netid, thost, &tsrv, teps, 32); |
| 271 |
if (srv == NULL) { |
| 272 |
msg("unable to contruct server data."); |
| 273 |
return (0); |
| 274 |
} |
| 275 |
needfree = 1; |
| 276 |
} |
| 277 |
|
| 278 |
ep = srv->ep.ep_val; |
| 279 |
epl = srv->ep.ep_len; |
| 280 |
|
| 281 |
|
| 282 |
for (i = 0; |
| 283 |
(i < epl) && ((udp_ep == -1) || (tcp_ep == -1)); i++) { |
| 284 |
if (strcasecmp(ep[i].proto, "udp") == 0) |
| 285 |
udp_ep = i; |
| 286 |
if (strcasecmp(ep[i].proto, "tcp") == 0) |
| 287 |
tcp_ep = i; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
if (tcp_ep > -1) { |
| 292 |
useep = &ep[tcp_ep]; |
| 293 |
useua = ep[tcp_ep].uaddr; |
| 294 |
type = SOCK_STREAM; |
| 295 |
} else if (udp_ep > -1) { |
| 296 |
useep = &ep[udp_ep]; |
| 297 |
useua = ep[udp_ep].uaddr; |
| 298 |
type = SOCK_DGRAM; |
| 299 |
} |
| 300 |
|
| 301 |
if (useep == NULL) { |
| 302 |
msg("no acceptable transport endpoints."); |
| 303 |
if (needfree) |
| 304 |
free_eps(teps, tsrv.ep.ep_len); |
| 305 |
return (0); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
if (*uaddr != NULL) |
| 313 |
useua = *uaddr; |
| 314 |
|
| 315 |
|
| 316 |
sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4); |
| 317 |
sprintf(ipuaddr, "%d.%d.%d.%d.0.111", a1, a2, a3, a4); |
| 318 |
useua = &ipuaddr[0]; |
| 319 |
|
| 320 |
memset(&sin, 0, sizeof(sin)); |
| 321 |
if (uaddr_to_sockaddr(useua, &sin)) { |
| 322 |
msg("unable to translate uaddr to sockaddr."); |
| 323 |
if (needfree) |
| 324 |
free_eps(teps, tsrv.ep.ep_len); |
| 325 |
return (0); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
clnt = clnttcp_create(&sin, RPCBPROG, RPCBVERS, &s, 0, 0); |
| 336 |
if (clnt == NULL) { |
| 337 |
msg("unable to create client handle to rpcbind."); |
| 338 |
if (needfree) |
| 339 |
free_eps(teps, tsrv.ep.ep_len); |
| 340 |
return (0); |
| 341 |
} |
| 342 |
|
| 343 |
tv.tv_sec = 5; |
| 344 |
tv.tv_usec = 0; |
| 345 |
time_valid = 0; |
| 346 |
status = clnt_call(clnt, RPCBPROC_GETTIME, (xdrproc_t)xdr_void, NULL, |
| 347 |
(xdrproc_t)xdr_u_long, &thetime, tv); |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
if (status == RPC_SUCCESS) |
| 354 |
time_valid = 1; |
| 355 |
else { |
| 356 |
int save; |
| 357 |
|
| 358 |
|
| 359 |
if (clnt != NULL) { |
| 360 |
clnt_destroy(clnt); |
| 361 |
clnt = NULL; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
sscanf(useua, "%d.%d.%d.%d.", &a1, &a2, &a3, &a4); |
| 373 |
sprintf(ut, "%d.%d.%d.%d.0.37", a1, a2, a3, a4); |
| 374 |
|
| 375 |
if (uaddr_to_sockaddr(ut, &sin)) { |
| 376 |
msg("cannot convert timeservice uaddr to sockaddr."); |
| 377 |
goto error; |
| 378 |
} |
| 379 |
|
| 380 |
s = socket(AF_INET, type, 0); |
| 381 |
if (s == -1) { |
| 382 |
msg("unable to open fd to network."); |
| 383 |
goto error; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
if (type == SOCK_DGRAM) { |
| 391 |
struct timeval timeout = { 20, 0 }; |
| 392 |
struct sockaddr_in from; |
| 393 |
fd_set readfds; |
| 394 |
int res; |
| 395 |
|
| 396 |
if (sendto(s, &thetime, sizeof(thetime), 0, |
| 397 |
(struct sockaddr *)&sin, sizeof(sin)) == -1) { |
| 398 |
msg("udp : sendto failed."); |
| 399 |
goto error; |
| 400 |
} |
| 401 |
do { |
| 402 |
FD_ZERO(&readfds); |
| 403 |
FD_SET(s, &readfds); |
| 404 |
res = select(_rpc_dtablesize(), &readfds, |
| 405 |
(fd_set *)NULL, (fd_set *)NULL, &timeout); |
| 406 |
} while (res < 0 && errno == EINTR); |
| 407 |
if (res <= 0) |
| 408 |
goto error; |
| 409 |
len = sizeof(from); |
| 410 |
res = recvfrom(s, (char *)&thetime, sizeof(thetime), 0, |
| 411 |
(struct sockaddr *)&from, &len); |
| 412 |
if (res == -1) { |
| 413 |
msg("recvfrom failed on udp transport."); |
| 414 |
goto error; |
| 415 |
} |
| 416 |
time_valid = 1; |
| 417 |
} else { |
| 418 |
int res; |
| 419 |
|
| 420 |
oldsig = (void (*)())signal(SIGALRM, alarm_hndler); |
| 421 |
saw_alarm = 0; |
| 422 |
alarm(20); |
| 423 |
res = connect(s, (struct sockaddr *)&sin, sizeof(sin)); |
| 424 |
if (res == -1) { |
| 425 |
msg("failed to connect to tcp endpoint."); |
| 426 |
goto error; |
| 427 |
} |
| 428 |
if (saw_alarm) { |
| 429 |
msg("alarm caught it, must be unreachable."); |
| 430 |
goto error; |
| 431 |
} |
| 432 |
res = read(s, (char *)&thetime, sizeof(thetime)); |
| 433 |
if (res != sizeof(thetime)) { |
| 434 |
if (saw_alarm) |
| 435 |
msg("timed out TCP call."); |
| 436 |
else |
| 437 |
msg("wrong size of results returned"); |
| 438 |
|
| 439 |
goto error; |
| 440 |
} |
| 441 |
time_valid = 1; |
| 442 |
} |
| 443 |
save = errno; |
| 444 |
(void)close(s); |
| 445 |
errno = save; |
| 446 |
s = RPC_ANYSOCK; |
| 447 |
|
| 448 |
if (time_valid) { |
| 449 |
thetime = ntohl(thetime); |
| 450 |
thetime = thetime - TOFFSET; |
| 451 |
} else |
| 452 |
thetime = 0; |
| 453 |
} |
| 454 |
|
| 455 |
gettimeofday(&tv, 0); |
| 456 |
|
| 457 |
error: |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
if (s != RPC_ANYSOCK) |
| 463 |
(void)close(s); |
| 464 |
|
| 465 |
if (clnt != NULL) |
| 466 |
clnt_destroy(clnt); |
| 467 |
|
| 468 |
alarm(0); |
| 469 |
if (oldsig) { |
| 470 |
signal(SIGALRM, oldsig); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
if (time_valid) { |
| 478 |
if (*uaddr == NULL) |
| 479 |
*uaddr = strdup(useua); |
| 480 |
|
| 481 |
|
| 482 |
tv.tv_sec += (tv.tv_sec > 500000) ? 1 : 0; |
| 483 |
delta = (thetime > tv.tv_sec) ? thetime - tv.tv_sec : |
| 484 |
tv.tv_sec - thetime; |
| 485 |
td->tv_sec = (thetime < tv.tv_sec) ? - delta : delta; |
| 486 |
td->tv_usec = 0; |
| 487 |
} else { |
| 488 |
msg("unable to get the server's time."); |
| 489 |
} |
| 490 |
|
| 491 |
if (needfree) |
| 492 |
free_eps(teps, tsrv.ep.ep_len); |
| 493 |
|
| 494 |
return (time_valid); |
| 495 |
} |