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