xref: /onnv-gate/usr/src/lib/libnisdb/nis_ldap.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include <poll.h>
31*0Sstevel@tonic-gate #include <sys/time.h>
32*0Sstevel@tonic-gate #include <stdlib.h>
33*0Sstevel@tonic-gate #include "nis_ldap.h"
34*0Sstevel@tonic-gate #include "nis_hashitem.h"
35*0Sstevel@tonic-gate #include "ldap_map.h"
36*0Sstevel@tonic-gate #include "ldap_parse.h"
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * Global structure keeping config state. Since it's created and modified
41*0Sstevel@tonic-gate  * while the rpc.nisd still is single-threaded, and only read in MT mode,
42*0Sstevel@tonic-gate  * no locking is needed.
43*0Sstevel@tonic-gate  */
44*0Sstevel@tonic-gate __nis_config_t	ldapConfig = {
45*0Sstevel@tonic-gate 	ini_none,			/* nisplusLDAPinitialUpdate */
46*0Sstevel@tonic-gate 	pass_error,			/* nisplusLDAPthreadCreationError */
47*0Sstevel@tonic-gate 	{
48*0Sstevel@tonic-gate 		-1,			/* Try forever */
49*0Sstevel@tonic-gate 		15			/* 15 second timeout */
50*0Sstevel@tonic-gate 	},
51*0Sstevel@tonic-gate 	de_retry,			/* nisplusLDAPdumpError */
52*0Sstevel@tonic-gate 	{
53*0Sstevel@tonic-gate 		-1,			/* Try forever */
54*0Sstevel@tonic-gate 		200			/* 200 second timeout */
55*0Sstevel@tonic-gate 	},
56*0Sstevel@tonic-gate 	directory_locked,		/* nisplusLDAPresyncService */
57*0Sstevel@tonic-gate 	accumulate,			/* nisplusLDAPupdateBatching */
58*0Sstevel@tonic-gate 	{
59*0Sstevel@tonic-gate 		-1,			/* Not used */
60*0Sstevel@tonic-gate 		120			/* Accumulate for 120 seconds */
61*0Sstevel@tonic-gate 	},
62*0Sstevel@tonic-gate 	block				/* nisplusLDAPexclusiveWaitMOde */
63*0Sstevel@tonic-gate };
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate  * Utility function that accepts a (__nisdb_retry_t *), decrements the
68*0Sstevel@tonic-gate  * 'attempts' counter, and sleeps for 'timeout' seconds.
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  * NOTE:	Don't pass a pointer into the 'ldapConfig' structure to
71*0Sstevel@tonic-gate  *		this function. Instead, initialize a private copy to the
72*0Sstevel@tonic-gate  *		value from 'ldapConfig'.
73*0Sstevel@tonic-gate  *
74*0Sstevel@tonic-gate  * The value of 'attempts' upon entry determines action as follows:
75*0Sstevel@tonic-gate  *
76*0Sstevel@tonic-gate  *	< 0	Don't change 'attempts', sleep as indicated, return 1
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  *	  0	Don't change 'attempts', only sleep if forceSleep is set,
79*0Sstevel@tonic-gate  *		return 0 if we didn't sleep, 1 if we slept.
80*0Sstevel@tonic-gate  *
81*0Sstevel@tonic-gate  *	> 0	Decrement 'attempts', sleep as indicated, return 1
82*0Sstevel@tonic-gate  */
83*0Sstevel@tonic-gate int
__nis_retry_sleep(__nisdb_retry_t * retry,int forceSleep)84*0Sstevel@tonic-gate __nis_retry_sleep(__nisdb_retry_t *retry, int forceSleep) {
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	if (retry == NULL)
87*0Sstevel@tonic-gate 		return (0);
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	if (retry->attempts > 0) {
90*0Sstevel@tonic-gate 		retry->attempts -= 1;
91*0Sstevel@tonic-gate 	} else if (retry->attempts == 0 && !forceSleep) {
92*0Sstevel@tonic-gate 		return (0);
93*0Sstevel@tonic-gate 	}
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	(void) poll(NULL, 0, retry->timeout*1000);
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	return (1);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /*
101*0Sstevel@tonic-gate  * The root directory is special in NIS+; it's the only directory that
102*0Sstevel@tonic-gate  * doesn't appear as an entry in another directory. Hence, our method
103*0Sstevel@tonic-gate  * of keeping the directory/table entry expiration time in the
104*0Sstevel@tonic-gate  * directory/table doesn't work, and we instead implement the following
105*0Sstevel@tonic-gate  * interface.
106*0Sstevel@tonic-gate  */
107*0Sstevel@tonic-gate static time_t	rootDirExpire = 0;
108*0Sstevel@tonic-gate static int	rootDirTtl = 0;
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate /*
111*0Sstevel@tonic-gate  * Return 1 if the root dir has expired, 0 otherwise.
112*0Sstevel@tonic-gate  */
113*0Sstevel@tonic-gate int
rootDirExpired(void)114*0Sstevel@tonic-gate rootDirExpired(void) {
115*0Sstevel@tonic-gate 	struct timeval	now;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	(void) gettimeofday(&now, 0);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	if (rootDirExpire >= now.tv_sec)
120*0Sstevel@tonic-gate 		return (1);
121*0Sstevel@tonic-gate 	else
122*0Sstevel@tonic-gate 		return (0);
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate  * Update the expiration time of the root dir to be now plus the TTL.
127*0Sstevel@tonic-gate  * Also establishes the TTL if not set.
128*0Sstevel@tonic-gate  */
129*0Sstevel@tonic-gate int
touchRootDir(void)130*0Sstevel@tonic-gate touchRootDir(void) {
131*0Sstevel@tonic-gate 	struct timeval	now;
132*0Sstevel@tonic-gate 	int		ttl;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	(void) gettimeofday(&now, 0);
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	/* Do we need to initialize the TTL ? */
137*0Sstevel@tonic-gate 	if (rootDirTtl == 0) {
138*0Sstevel@tonic-gate 		__nis_table_mapping_t	*t;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 		t = __nis_find_item_mt(ROOTDIRFILE, &ldapMappingList, 0, 0);
141*0Sstevel@tonic-gate 		if (t != 0) {
142*0Sstevel@tonic-gate 			int	interval;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 			interval = t->initTtlHi - t->initTtlLo + 1;
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 			if (interval > 1) {
147*0Sstevel@tonic-gate 				srand48(now.tv_sec);
148*0Sstevel@tonic-gate 				ttl = (lrand48() % interval);
149*0Sstevel@tonic-gate 			} else {
150*0Sstevel@tonic-gate 				ttl = t->initTtlLo;
151*0Sstevel@tonic-gate 			}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 			rootDirTtl = t->ttl;
154*0Sstevel@tonic-gate 		} else {
155*0Sstevel@tonic-gate 			ttl = rootDirTtl = 3600;
156*0Sstevel@tonic-gate 		}
157*0Sstevel@tonic-gate 	} else {
158*0Sstevel@tonic-gate 		ttl = rootDirTtl;
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	rootDirExpire = now.tv_sec + ttl;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	return (0);
164*0Sstevel@tonic-gate }
165