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