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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* This module contains the private function __rpc_get_time_offset()
* which will return the difference in seconds between the local system's
* notion of time and a remote server's notion of time. This must be
* possible without calling any functions that may invoke the name
* service. (netdir_getbyxxx, getXbyY, etc). The function is used in the
* synchronize call of the authdes code to synchronize clocks between
* NIS+ clients and their servers.
*
* Note to minimize the amount of duplicate code, portions of the
* synchronize() function were folded into this code, and the synchronize
* call becomes simply a wrapper around this function. Further, if this
* function is called with a timehost it *DOES* recurse to the name
* server so don't use it in that mode if you are doing name service code.
*
* Side effects :
* When called a client handle to a RPCBIND process is created
* and destroyed. Two strings "netid" and "uaddr" are malloc'd
* and returned. The SIGALRM processing is modified only if
* needed to deal with TCP connections.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "mt.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <netdir.h>
#include <string.h>
#include <strings.h>
#include <netconfig.h>
#include <netdb.h>
#include <signal.h>
#include <sys/errno.h>
#include <sys/poll.h>
#include <rpc/rpc.h>
#include <rpc/nettype.h>
#undef NIS
#include <rpcsvc/nis.h>
extern void __nis_netconfig2ep(struct netconfig *, endpoint *);
extern bool_t __nis_netconfig_matches_ep(struct netconfig *, endpoint *);
#ifdef TESTING
#define msg(x) printf("ERROR: %s\n", x)
/* #define msg(x) syslog(LOG_ERR, "%s", x) */
#else
#define msg(x)
#endif
static int saw_alarm = 0;
/* ARGSUSED */
static void
alarm_hndler(int s)
{
saw_alarm = 1;
}
/*
* The internet time server defines the epoch to be Jan 1, 1900
* whereas UNIX defines it to be Jan 1, 1970. To adjust the result
* from internet time-service time, into UNIX time we subtract the
* following offset :
*/
#define NYEARS (1970 - 1900)
#define TOFFSET ((uint_t)60*60*24*(365*NYEARS + (NYEARS/4)))
/*
* free_eps()
*
* Free the strings that were strduped into the eps structure.
*/
static void
free_eps(endpoint eps[], int num)
{
int i;
for (i = 0; i < num; i++) {
free(eps[i].uaddr);
free(eps[i].proto);
free(eps[i].family);
}
}
/*
* get_server()
*
* This function constructs a nis_server structure description for the
* indicated hostname.
*/
static nis_server *
get_server(char *host, nis_server *srv, endpoint eps[], int maxep)
{
int num_ep = 0, i;
struct netconfig *nc;
void *nch;
struct nd_hostserv hs;
struct nd_addrlist *addrs;
if (! host)
return (NULL);
hs.h_host = host;
hs.h_serv = "rpcbind";
nch = setnetconfig();
while (nc = getnetconfig(nch)) {
if ((nc->nc_flag & NC_VISIBLE) == 0)
continue;
if (! netdir_getbyname(nc, &hs, &addrs)) {
for (i = 0; (i < (addrs->n_cnt)) && (num_ep < maxep);
i++, num_ep++) {
eps[num_ep].uaddr =
taddr2uaddr(nc, &(addrs->n_addrs[i]));
__nis_netconfig2ep(nc, &(eps[num_ep]));
}
netdir_free((char *)addrs, ND_ADDRLIST);
}
}
(void) endnetconfig(nch);
srv->name = (nis_name) host;
srv->ep.ep_len = num_ep;
srv->ep.ep_val = eps;
srv->key_type = NIS_PK_NONE;
srv->pkey.n_bytes = NULL;
srv->pkey.n_len = 0;
return (srv);
}
#define MEP(ep, prot) (strcasecmp(ep.proto, prot) == 0)
#define MAX_ENDPOINTS 32
/*
* __rpc_get_time_offset()
*
* This function uses a nis_server structure to contact the a remote
* machine (as named in that structure) and returns the offset in time
* between that machine and this one. This offset is returned in seconds
* and may be positive or negative.
*
* The first time through, a lot of fiddling is done with the netconfig
* stuff to find a suitable transport. The function is very aggressive
* about choosing UDP or at worst TCP if it can. This is because
* those transports support both the RCPBIND call and the internet
* time service.
*
* Once through, *uaddr is set to the universal address of
* the machine and *netid is set to the local netid for the transport
* that uaddr goes with. On the second call, the netconfig stuff
* is skipped and the uaddr/netid pair are used to fetch the netconfig
* structure and to then contact the machine for the time.
*
* td = "server" - "client"
*/
int
__rpc_get_time_offset(struct timeval *td, nis_server *srv,
char *thost, char **uaddr, char **netid)
{
CLIENT *clnt; /* Client handle */
struct netbuf *addr = 0; /* address */
void *nc_handle; /* Netconfig "state" */
struct netconfig *nc; /* Various handles */
endpoint *ep; /* useful endpoints */
char *useua = NULL, /* uaddr of selected xp */
*useid = NULL; /* netid of selected xp */
int epl, i; /* counters */
enum clnt_stat status; /* result of clnt_call */
uint_t thetime;
ulong_t delta;
int needfree = 0;
struct timeval tv;
int rtime_fd = -1, time_valid, flag = 0;
int a1, a2, a3, a4;
char ut[INET6_ADDRSTRLEN];
char ipuaddr[INET6_ADDRSTRLEN];
endpoint teps[MAX_ENDPOINTS],
*epcand[MAX_ENDPOINTS],
*nonipcand[MAX_ENDPOINTS],
supplied;
uint32_t epc, nonip;
nis_server tsrv;
void (*oldsig)() = NULL; /* old alarm handler */
char *dot = NULL; /* tmp pointer */
nc = NULL;
td->tv_sec = 0;
td->tv_usec = 0;
/*
* First check to see if we need to find and address for this
* server.
*/
if (*uaddr == NULL) {
if ((srv != NULL) && (thost != NULL)) {
msg("both timehost and srv pointer used!");
return (0);
}
if (! srv) {
srv = get_server(thost, &tsrv, teps, 32);
if (! srv) {
msg("unable to contruct server data.");
return (0);
}
needfree = 1; /* need to free data in endpoints */
}
nc_handle = (void *) setnetconfig();
if (! nc_handle) {
msg("unable to get netconfig info.");
if (needfree)
free_eps(teps, tsrv.ep.ep_len);
return (0);
}
ep = srv->ep.ep_val;
epl = srv->ep.ep_len;
for (i = 0; i < sizeof (epcand)/sizeof (epcand[0]); i++) {
epcand[i] = 0;
nonipcand[i] = 0;
}
epc = 0;
nonip = 0;
/*
* Build the list of endpoint candidates. We prefer transports
* that we know are IP, but let /etc/netconfig determine the
* ordering among the IP transports.
*
* Note: We assume that the endpoint 'proto' field contains
* the netid of the transport.
*/
while ((nc = getnetconfig(nc_handle)) != NULL) {
/* Is it a visible transport ? */
if ((nc->nc_flag & NC_VISIBLE) == 0)
continue;
/* Check against the end points */
for (i = 0; i < epl; i++) {
if (__nis_netconfig_matches_ep(nc, &(ep[i]))) {
if (MEP(ep[i], "udp") ||
MEP(ep[i], "udp6") ||
MEP(ep[i], "tcp") ||
MEP(ep[i], "tcp6")) {
epcand[epc++] = &(ep[i]);
} else {
nonipcand[nonip++] = &ep[i];
}
break;
}
}
}
(void) endnetconfig(nc_handle);
/*
* epcand[] now contains the candidate transports. If there
* were non-IP transports as well, add them to the end of the
* candidate list.
*/
for (i = 0; i < nonip; i++) {
epcand[epc++] = nonipcand[i];
}
if (epc == 0) {
msg("no acceptable transport endpoints.");
if (needfree)
free_eps(teps, tsrv.ep.ep_len);
return (0);
}
} else {
/* Caller supplied a uaddr. Fake an endpoint. */
if (*netid != 0) {
supplied.proto = *netid;
/* Is it one of the known IP transports ? */
if (strcmp("udp", supplied.proto) &&
strcmp("udp6", supplied.proto) &&
strcmp("tcp", supplied.proto) &&
strcmp("tcp6", supplied.proto)) {
/* No, it's not */
nonip = 1;
} else {
nonip = 0;
}
} else {
supplied.proto = (strchr(*uaddr, ':') != 0) ?
"udp6" : "udp";
nonip = 0;
}
supplied.uaddr = *uaddr;
supplied.family = (strchr(*uaddr, ':') != 0) ?
"inet6" : "inet";
epcand[0] = &supplied;
epc = 1;
nonip = 0;
}
nc = 0;
clnt = 0;
status = RPC_FAILED; /* Anything except RPC_SUCCESS */
/*
* Loop over the endpoint candidates. Defer error reporting (except
* for the netconfig entry) until we've looked at all candidates.
*/
for (i = 0; i < epc; i++) {
if (nc != 0)
freenetconfigent(nc);
nc = getnetconfigent(epcand[i]->proto);
if (nc == 0) {
msg("unable to locate netconfig info for netid.");
if (needfree)
free_eps(teps, tsrv.ep.ep_len);
return (0);
}
/*
* Add the appropriate port number to the uaddr
*/
useua = epcand[i]->uaddr;
useid = epcand[i]->proto;
if (strcasecmp(nc->nc_protofmly, NC_INET) == 0) {
(void) sscanf(useua,
"%d.%d.%d.%d.", &a1, &a2, &a3, &a4);
(void) sprintf(ipuaddr, "%d.%d.%d.%d.0.111",
a1, a2, a3, a4);
useua = &ipuaddr[0];
} else if (strcasecmp(nc->nc_protofmly, NC_INET6) == 0) {
size_t len;
char *port = ".0.111";
if (strlen(useua) >= sizeof (ipuaddr)) {
freenetconfigent(nc);
if (needfree)
free_eps(teps, tsrv.ep.ep_len);
return (0);
}
(void) strcpy(ipuaddr, useua);
/* get the IPv6 address out of the uaddr */
if ((dot = strrchr(ipuaddr, '.')) != 0) {
*dot = '\0';
if ((dot = strrchr(ipuaddr, '.')) != 0)
*dot = '\0';
}
if (dot == 0 ||
(len = strlen(ipuaddr))+strlen(port) >=
sizeof (ipuaddr)) {
freenetconfigent(nc);
if (needfree)
free_eps(teps, tsrv.ep.ep_len);
return (0);
}
/* now put in 0.111 */
(void) strcat(ipuaddr + len, port);
useua = ipuaddr;
}
/*
* Create the client handle to rpcbind. Note we always try
* version 3 since that is the earliest version that supports
* the RPCB_GETTIME call. Also it is the version that comes
* standard with SVR4. Since most everyone supports TCP/IP
* we could consider trying the rtime call first.
*/
if (clnt != 0)
clnt_destroy(clnt);
clnt = __nis_clnt_create(RPC_ANYFD, nc, useua, 0, 0, RPCBPROG,
RPCBVERS, 0, 0);
if (! clnt)
continue;
tv.tv_sec = 5;
tv.tv_usec = 0;
time_valid = 0;
status = clnt_call(clnt, RPCBPROC_GETTIME, xdr_void, NULL,
xdr_u_int, (char *)&thetime, tv);
/*
* The only error we check for is anything but success. In
* fact we could have seen PROGMISMATCH if talking to a 4.1
* machine (pmap v2) or TIMEDOUT if the net was busy.
*/
if (status == RPC_SUCCESS)
break;
}
if (status == RPC_SUCCESS) {
time_valid = 1;
} else if (clnt == 0) {
msg("unable to create client handle to rpcbind.");
freenetconfigent(nc);
if (needfree)
free_eps(teps, tsrv.ep.ep_len);
return (0);
} else {
/*
* Try the timeservice port. This presumably only exists
* for IP transports, so we ignore the non-IP ones.
*/
for (i = 0; i < epc-nonip; i++) {
/*
* Convert PMAP address into timeservice address
* We take advantage of the fact that we "know" what
* a universal address looks like for inet transports.
*
* We also know that the internet timeservice is always
* listening on port 37.
*/
if (nc != 0)
freenetconfigent(nc);
nc = getnetconfigent(epcand[i]->proto);
if (nc == 0) {
msg("no netconfig info for netid.");
if (needfree)
free_eps(teps, tsrv.ep.ep_len);
return (0);
}
useua = epcand[i]->uaddr;
useid = epcand[i]->proto;
if (strcasecmp(nc->nc_protofmly, NC_INET) == 0) {
(void) sscanf(useua,
"%d.%d.%d.%d.", &a1, &a2, &a3, &a4);
(void) sprintf(ut, "%d.%d.%d.%d.0.37",
a1, a2, a3, a4);
} else if (strcasecmp(nc->nc_protofmly, NC_INET6) ==
0) {
size_t len;
char *port = ".0.37";
if (strlen(useua) >= sizeof (ut)) {
goto error;
}
(void) strcpy(ut, useua);
/* get the IPv6 address out of the uaddr */
if ((dot = strrchr(ut, '.')) != 0) {
*dot = '\0';
if ((dot = strrchr(ut, '.')) != 0)
*dot = '\0';
}
if (dot == 0) {
goto error;
}
if ((len = strlen(ut))+strlen(port) >=
sizeof (ut)) {
goto error;
}
(void) strcat(ut + len, port);
}
addr = uaddr2taddr(nc, ut);
if (! addr) {
msg("timeservice uaddr to taddr failed.");
goto error;
}
rtime_fd = t_open(nc->nc_device, O_RDWR, NULL);
if (rtime_fd == -1) {
msg("unable to open fd to network.");
goto error;
}
if (t_bind(rtime_fd, NULL, NULL) < 0) {
msg("unable to bind an endpoint to fd.");
goto error;
}
/*
* Now depending on whether or not we're talking to
* UDP we set a timeout or not.
*/
if (nc->nc_semantics == NC_TPI_CLTS) {
struct t_unitdata tu_data;
struct pollfd pfd;
int res;
tu_data.addr = *addr;
tu_data.udata.buf = (char *)&thetime;
tu_data.udata.len = (uint_t)sizeof (thetime);
tu_data.udata.maxlen = tu_data.udata.len;
tu_data.opt.len = 0;
tu_data.opt.maxlen = 0;
if (t_sndudata(rtime_fd, &tu_data) == -1) {
msg("udp : t_sndudata failed.");
goto error;
}
pfd.fd = rtime_fd;
pfd.events =
POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
do {
res = poll(&pfd, 1, 10000);
} while (res < 0);
if ((res <= 0) || (pfd.revents & POLLNVAL))
goto error;
if (t_rcvudata(rtime_fd, &tu_data, &flag) <
0) {
msg("t_rvcdata failed on udp trpt.");
goto error;
}
time_valid = 1;
} else {
struct t_call sndcall;
sndcall.addr = *addr;
sndcall.opt.len = sndcall.opt.maxlen = 0;
sndcall.udata.len = sndcall.udata.maxlen = 0;
oldsig = (void (*)())signal(SIGALRM,
alarm_hndler);
saw_alarm = 0; /* global tracking the alarm */
(void) alarm(20); /* only wait 20 seconds */
if (t_connect(rtime_fd, &sndcall, NULL) ==
-1) {
msg("connect tcp endpoint failedd.");
goto error;
}
if (saw_alarm) {
msg("alarm caught it; unreachable.");
goto error;
}
if (t_rcv(rtime_fd, (char *)&thetime,
(uint_t)sizeof (thetime), &flag) !=
(uint_t)sizeof (thetime)) {
if (saw_alarm) {
/*EMPTY*/
msg("timed out TCP call.");
} else {
/*EMPTY*/
msg("wrong size results");
}
goto error;
}
time_valid = 1;
}
if (time_valid) {
thetime = ntohl(thetime);
/* adjust to UNIX time */
thetime = thetime - TOFFSET;
} else
thetime = 0;
}
}
error:
/*
* clean up our allocated data structures.
*/
if (addr)
netdir_free((char *)(addr), ND_ADDR);
if (rtime_fd != -1)
(void) t_close(rtime_fd);
if (clnt)
clnt_destroy(clnt);
if (nc)
freenetconfigent(nc);
if (oldsig) {
(void) alarm(0); /* reset that alarm if its outstanding */
(void) signal(SIGALRM, oldsig);
}
/*
* note, don't free uaddr strings until after we've made a
* copy of them.
*/
if (time_valid) {
if (! *netid) {
*netid = strdup(useid);
if (! *netid) {
msg("__rpc_get_time_offset: strdup failed.");
if (needfree)
free_eps(teps, tsrv.ep.ep_len);
return (0);
}
*uaddr = strdup(useua);
if (! *uaddr) {
msg("__rpc_get_time_offset: strdup failed.");
if (*netid)
free(*netid);
if (needfree)
free_eps(teps, tsrv.ep.ep_len);
return (0);
}
}
(void) gettimeofday(&tv, 0);
/* Round to the nearest second */
tv.tv_sec += (tv.tv_sec > 500000) ? 1 : 0;
delta = (thetime > tv.tv_sec) ? thetime - tv.tv_sec :
tv.tv_sec - thetime;
td->tv_sec = (thetime < tv.tv_sec) ? - delta : delta;
td->tv_usec = 0;
} else {
/*EMPTY*/
msg("unable to get the server's time.");
}
if (needfree)
free_eps(teps, tsrv.ep.ep_len);
return (time_valid);
}
|