1e1dd0a2fSth /*
2e1dd0a2fSth  * CDDL HEADER START
3e1dd0a2fSth  *
4e1dd0a2fSth  * The contents of this file are subject to the terms of the
5e1dd0a2fSth  * Common Development and Distribution License (the "License").
6e1dd0a2fSth  * You may not use this file except in compliance with the License.
7e1dd0a2fSth  *
8e1dd0a2fSth  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9e1dd0a2fSth  * or http://www.opensolaris.org/os/licensing.
10e1dd0a2fSth  * See the License for the specific language governing permissions
11e1dd0a2fSth  * and limitations under the License.
12e1dd0a2fSth  *
13e1dd0a2fSth  * When distributing Covered Code, include this CDDL HEADER in each
14e1dd0a2fSth  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15e1dd0a2fSth  * If applicable, add the following below this CDDL HEADER, with the
16e1dd0a2fSth  * fields enclosed by brackets "[]" replaced with your own identifying
17e1dd0a2fSth  * information: Portions Copyright [yyyy] [name of copyright owner]
18e1dd0a2fSth  *
19e1dd0a2fSth  * CDDL HEADER END
20e1dd0a2fSth  */
21e1dd0a2fSth /*
22e1dd0a2fSth  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23e1dd0a2fSth  * Use is subject to license terms.
24695ef821SGordon Ross  *
25695ef821SGordon Ross  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
26e1dd0a2fSth  */
27e1dd0a2fSth 
28e1dd0a2fSth #include <string.h>
29e1dd0a2fSth #include <errno.h>
30e1dd0a2fSth #include <syslog.h>
31e1dd0a2fSth #include <procfs.h>
32e1dd0a2fSth #include <unistd.h>
33e1dd0a2fSth #include <fcntl.h>
34e1dd0a2fSth #include <libintl.h>
35e1dd0a2fSth #include <atomic.h>
36e1dd0a2fSth #include <pthread.h>
37e1dd0a2fSth #include <sys/mman.h>
38e1dd0a2fSth #include <time.h>
39e1dd0a2fSth #include "solaris-int.h"
40e1dd0a2fSth #include "ns_connmgmt.h"
41e1dd0a2fSth #include "ns_cache_door.h"
42e1dd0a2fSth #include "ns_internal.h"
43e1dd0a2fSth 
44e1dd0a2fSth /*
45e1dd0a2fSth  * Access (reference, shutdown, or reload) the current connection
46e1dd0a2fSth  * management control structure conn_mgmt_t.
47e1dd0a2fSth  */
48e1dd0a2fSth #define	NS_CONN_MGMT_OP_REF		1
49e1dd0a2fSth #define	NS_CONN_MGMT_OP_SHUTDOWN	2
50e1dd0a2fSth #define	NS_CONN_MGMT_OP_RELOAD_CONFIG	3
51e1dd0a2fSth #define	NS_CONN_MGMT_OP_NEW_CONFIG	4
52e1dd0a2fSth #define	NS_CONN_MGMT_OP_LIB_INIT	5
53e1dd0a2fSth 
54e1dd0a2fSth static ns_conn_mgmt_t *access_conn_mgmt(int);
55e1dd0a2fSth static ns_conn_mgmt_t *release_conn_mgmt(ns_conn_mgmt_t *, boolean_t);
56e1dd0a2fSth static int close_conn_mt(ns_conn_mt_t *, int, ns_ldap_error_t **,
57e1dd0a2fSth 	ns_conn_user_t *);
58e1dd0a2fSth static int close_conn_mt_when_nouser(ns_conn_mt_t *cm);
59e1dd0a2fSth void shutdown_all_conn_mt(ns_conn_mgmt_t *cmg);
60e1dd0a2fSth static int conn_signal(ns_conn_mt_t *);
61e1dd0a2fSth static int conn_wait(ns_conn_mt_t *, ns_conn_user_t *);
62e1dd0a2fSth static void close_conn_mt_by_procchg(ns_conn_mt_t *cm, int rc, char *errmsg);
63e1dd0a2fSth static ns_conn_mgmt_t *proc_server_change(ns_server_status_change_t *chg,
64e1dd0a2fSth 	ns_conn_mgmt_t  *cmg);
65e1dd0a2fSth static void get_preferred_servers(boolean_t, boolean_t, ns_conn_mgmt_t *);
66e1dd0a2fSth static void start_thread();
67e1dd0a2fSth 
68e1dd0a2fSth static ns_conn_mgmt_t	*ns_connmgmt = NULL;
69e1dd0a2fSth static ns_conn_mgmt_t	*ns_connmgmt_parent = NULL;
70e1dd0a2fSth static mutex_t		ns_connmgmt_lock = DEFAULTMUTEX;
71e1dd0a2fSth static boolean_t	ns_connmgmt_shutting_down = B_FALSE;
72e1dd0a2fSth 
73e1dd0a2fSth #define	NS_CONN_MSG_NO_CONN_MGMT gettext( \
74e1dd0a2fSth 	"libsldap: unable to allocate the connection management control")
75e1dd0a2fSth #define	NS_CONN_MSG_NO_MTC_KEY gettext( \
76e1dd0a2fSth 	"libsldap: unable to allocate the TSD key for per-thread ldap error")
77e1dd0a2fSth #define	NS_CONN_MSG_NO_CMG_KEY gettext( \
78e1dd0a2fSth 	"libsldap: unable to allocate the TSD key for connection management")
79e1dd0a2fSth #define	NS_CONN_MSG_SHUTDOWN gettext("libsldap: library is being unloaded")
80e1dd0a2fSth #define	NS_CONN_MSG_RELOADED gettext( \
81e1dd0a2fSth 	"libsldap: configuration has been reloaded")
82e1dd0a2fSth #define	NS_CONN_MSG_SHUTDOWN_RELOADED gettext( \
83e1dd0a2fSth 	"libsldap: library unloaded or configuration has been reloaded")
84e1dd0a2fSth #define	NS_CONN_MSG_BAD_CACHEMGR_DATA gettext( \
85e1dd0a2fSth 	"libsldap: received incorrect data from ldap_cachemgr")
86e1dd0a2fSth #define	NS_CONN_MSG_MEMORY_ERROR gettext( \
87e1dd0a2fSth 	"libsldap: unable to allocate memory")
88e1dd0a2fSth #define	NS_CONN_MSG_NO_PROCCHG_THREAD gettext( \
89e1dd0a2fSth 	"libsldap: unable to start the server monitor thread (%s)")
90e1dd0a2fSth #define	NS_CONN_MSG_DOWN_FROM_CACHEMGR gettext( \
91e1dd0a2fSth 	"libsldap: server down reported by ldap_cachemgr")
92e1dd0a2fSth 
93e1dd0a2fSth static int ns_conn_free = 1;
94e1dd0a2fSth #define	NS_CONN_UNLOCK_AND_FREE(free, cm, cmg)  \
95e1dd0a2fSth { \
96e1dd0a2fSth 	(void) mutex_unlock(&(cm)->lock);	\
97e1dd0a2fSth 	if (free == 1)	\
98e1dd0a2fSth 		cmg = free_conn_mt((cm), 1); \
99e1dd0a2fSth 	if (cmg != NULL) \
100e1dd0a2fSth 		(void) mutex_unlock(&(cmg)->lock); \
101e1dd0a2fSth }
102e1dd0a2fSth 
103e1dd0a2fSth #define	NS_CONN_CHECK_ABORT_AND_LOCK(cmg, cu, errp) \
104e1dd0a2fSth { \
105e1dd0a2fSth 	char *msg = NULL; \
106e1dd0a2fSth 	(void) mutex_lock(&(cmg)->lock); \
107e1dd0a2fSth 	if ((cmg)->shutting_down == B_TRUE) \
108e1dd0a2fSth 		msg = NS_CONN_MSG_SHUTDOWN; \
109e1dd0a2fSth 	else if ((cmg)->cfg_reloaded == B_TRUE)  \
110e1dd0a2fSth 		msg = NS_CONN_MSG_RELOADED; \
111e1dd0a2fSth 	if (msg != NULL) { \
112e1dd0a2fSth 		(*errp) = __s_api_make_error(NS_LDAP_OP_FAILED, msg); \
113e1dd0a2fSth 		(void) mutex_unlock(&(cmg)->lock); \
114e1dd0a2fSth 		return (NS_LDAP_OP_FAILED); \
115e1dd0a2fSth 	} \
116e1dd0a2fSth }
117e1dd0a2fSth 
118e1dd0a2fSth /*
119e1dd0a2fSth  * TSD keys ns_mtckey and ns_cmgkey are for sharing ldap connections
120e1dd0a2fSth  * and their associated connection management structure among
121e1dd0a2fSth  * multiple threads. The pointers to the per-thread ldap error
122e1dd0a2fSth  * information and the connection management structure are
123e1dd0a2fSth  * saved in ns_mtckey and ns_cmgkey.
124e1dd0a2fSth  */
125e1dd0a2fSth thread_key_t ns_mtckey = THR_ONCE_KEY;
126e1dd0a2fSth thread_key_t ns_cmgkey = THR_ONCE_KEY;
127e1dd0a2fSth 
128e1dd0a2fSth /* Per thread LDAP error resides in thread-specific data (ns_mtckey) */
129e1dd0a2fSth struct ldap_error {
130e1dd0a2fSth 	int	le_errno;
131e1dd0a2fSth 	char	*le_matched;
132e1dd0a2fSth 	char	*le_errmsg;
133e1dd0a2fSth };
134e1dd0a2fSth 
135e1dd0a2fSth /* NULL struct ldap_error */
136e1dd0a2fSth static struct ldap_error ldap_error_NULL = { LDAP_SUCCESS, NULL, NULL};
137e1dd0a2fSth 
138e1dd0a2fSth /* destructor: free the ldap error data in the thread specific area */
139e1dd0a2fSth static void
ns_mtckey_cleanup(void * key)140*cc98049dSToomas Soome ns_mtckey_cleanup(void *key)
141*cc98049dSToomas Soome {
142e1dd0a2fSth 	struct ldap_error *le = (struct ldap_error *)key;
143e1dd0a2fSth 
144e1dd0a2fSth 	if (le == NULL)
145e1dd0a2fSth 		return;
146e1dd0a2fSth 	if (le->le_matched != NULL) {
147e1dd0a2fSth 		ldap_memfree(le->le_matched);
148e1dd0a2fSth 	}
149e1dd0a2fSth 	if (le->le_errmsg != NULL) {
150e1dd0a2fSth 		ldap_memfree(le->le_errmsg);
151e1dd0a2fSth 	}
152e1dd0a2fSth 	free(le);
153e1dd0a2fSth }
154e1dd0a2fSth 
155e1dd0a2fSth /* Free/detach the thread specific data structures */
156e1dd0a2fSth static void
conn_tsd_free(void)157*cc98049dSToomas Soome conn_tsd_free(void)
158*cc98049dSToomas Soome {
159e1dd0a2fSth 	void	*tsd = NULL;
160e1dd0a2fSth 	int	rc;
161e1dd0a2fSth 
162e1dd0a2fSth 	/* free the per-thread ldap error info */
163e1dd0a2fSth 	rc = thr_getspecific(ns_mtckey, &tsd);
164e1dd0a2fSth 	if (rc == 0 && tsd != NULL)
165e1dd0a2fSth 		ns_mtckey_cleanup(tsd);
166e1dd0a2fSth 	(void) thr_setspecific(ns_mtckey, NULL);
167e1dd0a2fSth 
168e1dd0a2fSth 	/* detach the connection management control */
169e1dd0a2fSth 	(void) thr_setspecific(ns_cmgkey, NULL);
170e1dd0a2fSth }
171e1dd0a2fSth 
172e1dd0a2fSth /* per-thread callback function for allocating a mutex */
173e1dd0a2fSth static void *
ns_mutex_alloc(void)174e1dd0a2fSth ns_mutex_alloc(void)
175e1dd0a2fSth {
176e1dd0a2fSth 	mutex_t *mutexp = NULL;
177e1dd0a2fSth 
178e1dd0a2fSth 	if ((mutexp = malloc(sizeof (mutex_t))) != NULL) {
179e1dd0a2fSth 		if (mutex_init(mutexp, USYNC_THREAD, NULL) != 0) {
180e1dd0a2fSth 			free(mutexp);
181e1dd0a2fSth 			mutexp = NULL;
182e1dd0a2fSth 		}
183e1dd0a2fSth 	}
184e1dd0a2fSth 	return (mutexp);
185e1dd0a2fSth }
186e1dd0a2fSth 
187e1dd0a2fSth /* per-thread callback function for freeing a mutex */
188e1dd0a2fSth static void
ns_mutex_free(void * mutexp)189e1dd0a2fSth ns_mutex_free(void *mutexp)
190e1dd0a2fSth {
191e1dd0a2fSth 	(void) mutex_destroy((mutex_t *)mutexp);
192e1dd0a2fSth 	free(mutexp);
193e1dd0a2fSth }
194e1dd0a2fSth 
195e1dd0a2fSth /*
196e1dd0a2fSth  * Function for setting up thread-specific data
197e1dd0a2fSth  * where per thread LDAP error and the pointer
198e1dd0a2fSth  * to the active connection management control
199e1dd0a2fSth  * are stored.
200e1dd0a2fSth  */
201e1dd0a2fSth static int
conn_tsd_setup(ns_conn_mgmt_t * cmg)202e1dd0a2fSth conn_tsd_setup(ns_conn_mgmt_t *cmg)
203e1dd0a2fSth {
204e1dd0a2fSth 	void	*tsd;
205e1dd0a2fSth 	int	rc;
206e1dd0a2fSth 
207e1dd0a2fSth 	rc = thr_setspecific(ns_cmgkey, cmg);
208e1dd0a2fSth 	if (rc != 0) /* must be ENOMEM */
209e1dd0a2fSth 		return (-1);
210e1dd0a2fSth 
211e1dd0a2fSth 	/* return success if the ns_mtckey TSD is already set */
212e1dd0a2fSth 	rc = thr_getspecific(ns_mtckey, &tsd);
213e1dd0a2fSth 	if (rc == 0 && tsd != NULL)
214e1dd0a2fSth 		return (0);
215e1dd0a2fSth 
216e1dd0a2fSth 	/* allocate and set the ns_mtckey TSD */
217e1dd0a2fSth 	tsd = (void *) calloc(1, sizeof (struct ldap_error));
218e1dd0a2fSth 	if (tsd == NULL)
219e1dd0a2fSth 		return (-1);
220e1dd0a2fSth 	rc = thr_setspecific(ns_mtckey, tsd);
221e1dd0a2fSth 	if (rc != 0) { /* must be ENOMEM */
222e1dd0a2fSth 		free(tsd);
223e1dd0a2fSth 		return (-1);
224e1dd0a2fSth 	}
225e1dd0a2fSth 	return (0);
226e1dd0a2fSth }
227e1dd0a2fSth 
228e1dd0a2fSth /* Callback function for setting the per thread LDAP error */
229e1dd0a2fSth /*ARGSUSED*/
230e1dd0a2fSth static void
set_ld_error(int err,char * matched,char * errmsg,void * dummy)231e1dd0a2fSth set_ld_error(int err, char *matched, char *errmsg, void *dummy)
232e1dd0a2fSth {
233e1dd0a2fSth 	struct ldap_error	*le;
234e1dd0a2fSth 	int			eno;
235e1dd0a2fSth 
236e1dd0a2fSth 	if ((eno = thr_getspecific(ns_mtckey, (void **)&le)) != 0) {
237e1dd0a2fSth 		syslog(LOG_ERR, gettext(
238e1dd0a2fSth 		    "libsldap: set_ld_error: thr_getspecific failed (%s)."),
239e1dd0a2fSth 		    strerror(eno));
240e1dd0a2fSth 		return;
241e1dd0a2fSth 	}
242e1dd0a2fSth 
243e1dd0a2fSth 	/* play safe, do nothing if TSD pointer is NULL */
244e1dd0a2fSth 	if (le == NULL) {
245e1dd0a2fSth 		syslog(LOG_INFO, gettext(
246e1dd0a2fSth 		    "libsldap: set_ld_error: TSD pointer is NULL."));
247e1dd0a2fSth 		return;
248e1dd0a2fSth 	}
249e1dd0a2fSth 
250e1dd0a2fSth 	le->le_errno = err;
251e1dd0a2fSth 
252e1dd0a2fSth 	if (le->le_matched != NULL) {
253e1dd0a2fSth 		ldap_memfree(le->le_matched);
254e1dd0a2fSth 		le->le_matched = NULL;
255e1dd0a2fSth 	}
256e1dd0a2fSth 	le->le_matched = matched;
257e1dd0a2fSth 
258e1dd0a2fSth 	if (le->le_errmsg != NULL) {
259e1dd0a2fSth 		ldap_memfree(le->le_errmsg);
260e1dd0a2fSth 		le->le_errmsg = NULL;
261e1dd0a2fSth 	}
262e1dd0a2fSth 	le->le_errmsg = errmsg;
263e1dd0a2fSth }
264e1dd0a2fSth 
265e1dd0a2fSth /* check and allocate the thread-specific data for using a MT connection */
266e1dd0a2fSth static int
conn_tsd_check(ns_conn_mgmt_t * cmg)267e1dd0a2fSth conn_tsd_check(ns_conn_mgmt_t *cmg)
268e1dd0a2fSth {
269e1dd0a2fSth 	if (conn_tsd_setup(cmg) != 0)
270e1dd0a2fSth 		return (NS_LDAP_MEMORY);
271e1dd0a2fSth 
272e1dd0a2fSth 	return (NS_LDAP_SUCCESS);
273e1dd0a2fSth }
274e1dd0a2fSth 
275e1dd0a2fSth /* Callback function for getting the per thread LDAP error */
276e1dd0a2fSth /*ARGSUSED*/
277e1dd0a2fSth static int
get_ld_error(char ** matched,char ** errmsg,void * dummy)278e1dd0a2fSth get_ld_error(char **matched, char **errmsg, void *dummy)
279e1dd0a2fSth {
280e1dd0a2fSth 	struct ldap_error	*le;
281e1dd0a2fSth 	int			eno;
282e1dd0a2fSth 
283e1dd0a2fSth 	if ((eno = thr_getspecific(ns_mtckey, (void **)&le)) != 0) {
284e1dd0a2fSth 		syslog(LOG_ERR, gettext(
285e1dd0a2fSth 		    "libsldap: get_ld_error: thr_getspecific failed (%s)"),
286e1dd0a2fSth 		    strerror(eno));
287e1dd0a2fSth 		return (eno);
288e1dd0a2fSth 	}
289e1dd0a2fSth 
290e1dd0a2fSth 	/* play safe, return NULL error data, if TSD pointer is NULL */
291e1dd0a2fSth 	if (le == NULL)
292e1dd0a2fSth 		le = &ldap_error_NULL;
293e1dd0a2fSth 
294e1dd0a2fSth 	if (matched != NULL) {
295e1dd0a2fSth 		*matched = le->le_matched;
296e1dd0a2fSth 	}
297e1dd0a2fSth 	if (errmsg != NULL) {
298e1dd0a2fSth 		*errmsg = le->le_errmsg;
299e1dd0a2fSth 	}
300e1dd0a2fSth 	return (le->le_errno);
301e1dd0a2fSth }
302e1dd0a2fSth 
303e1dd0a2fSth /* Callback function for setting per thread errno */
304e1dd0a2fSth static void
set_errno(int err)305e1dd0a2fSth set_errno(int err)
306e1dd0a2fSth {
307e1dd0a2fSth 	errno = err;
308e1dd0a2fSth }
309e1dd0a2fSth 
310e1dd0a2fSth /* Callback function for getting per thread errno */
311e1dd0a2fSth static int
get_errno(void)312e1dd0a2fSth get_errno(void)
313e1dd0a2fSth {
314e1dd0a2fSth 	return (errno);
315e1dd0a2fSth }
316e1dd0a2fSth 
317*cc98049dSToomas Soome static void *
ltf_threadid(void)318*cc98049dSToomas Soome ltf_threadid(void)
319*cc98049dSToomas Soome {
320*cc98049dSToomas Soome 	return ((void *)(uintptr_t)thr_self());
321*cc98049dSToomas Soome }
322*cc98049dSToomas Soome 
323e1dd0a2fSth /* set up an ldap session 'ld' for sharing among multiple threads */
324e1dd0a2fSth static int
setup_mt_conn(LDAP * ld)325e1dd0a2fSth setup_mt_conn(LDAP *ld)
326e1dd0a2fSth {
327e1dd0a2fSth 
328e1dd0a2fSth 	struct ldap_thread_fns		tfns;
329e1dd0a2fSth 	struct ldap_extra_thread_fns	extrafns;
330e1dd0a2fSth 	int				rc;
331e1dd0a2fSth 
332e1dd0a2fSth 	/*
333e1dd0a2fSth 	 * Set the function pointers for dealing with mutexes
334e1dd0a2fSth 	 * and error information
335e1dd0a2fSth 	 */
336e1dd0a2fSth 	(void) memset(&tfns, '\0', sizeof (struct ldap_thread_fns));
337e1dd0a2fSth 	tfns.ltf_mutex_alloc = (void *(*)(void)) ns_mutex_alloc;
338e1dd0a2fSth 	tfns.ltf_mutex_free = (void (*)(void *)) ns_mutex_free;
339e1dd0a2fSth 	tfns.ltf_mutex_lock = (int (*)(void *)) mutex_lock;
340e1dd0a2fSth 	tfns.ltf_mutex_unlock = (int (*)(void *)) mutex_unlock;
341e1dd0a2fSth 	tfns.ltf_get_errno = get_errno;
342e1dd0a2fSth 	tfns.ltf_set_errno = set_errno;
343e1dd0a2fSth 	tfns.ltf_get_lderrno = get_ld_error;
344e1dd0a2fSth 	tfns.ltf_set_lderrno = set_ld_error;
345e1dd0a2fSth 	tfns.ltf_lderrno_arg = NULL;
346e1dd0a2fSth 
347e1dd0a2fSth 	/*
348e1dd0a2fSth 	 * Set up the ld to use those function pointers
349e1dd0a2fSth 	 */
350e1dd0a2fSth 	rc = ldap_set_option(ld, LDAP_OPT_THREAD_FN_PTRS,
351e1dd0a2fSth 	    (void *) &tfns);
352e1dd0a2fSth 	if (rc < 0) {
353e1dd0a2fSth 		syslog(LOG_INFO, gettext("libsldap: ldap_set_option "
354e1dd0a2fSth 		"(LDAP_OPT_THREAD_FN_PTRS)"));
355e1dd0a2fSth 		return (0);
356e1dd0a2fSth 	}
357e1dd0a2fSth 
358e1dd0a2fSth 	/*
359e1dd0a2fSth 	 * Set the function pointers for working with semaphores
360e1dd0a2fSth 	 */
361e1dd0a2fSth 	(void) memset(&extrafns, '\0',
362e1dd0a2fSth 	    sizeof (struct ldap_extra_thread_fns));
363*cc98049dSToomas Soome 	extrafns.ltf_threadid_fn = ltf_threadid;
364e1dd0a2fSth 	extrafns.ltf_mutex_trylock = NULL;
365e1dd0a2fSth 	extrafns.ltf_sema_alloc = NULL;
366e1dd0a2fSth 	extrafns.ltf_sema_free = NULL;
367e1dd0a2fSth 	extrafns.ltf_sema_wait = NULL;
368e1dd0a2fSth 	extrafns.ltf_sema_post = NULL;
369e1dd0a2fSth 
370e1dd0a2fSth 	/* Set up the ld to use those function pointers */
371e1dd0a2fSth 	rc = ldap_set_option(ld, LDAP_OPT_EXTRA_THREAD_FN_PTRS,
372e1dd0a2fSth 	    (void *) &extrafns);
373e1dd0a2fSth 	if (rc < 0) {
374e1dd0a2fSth 		syslog(LOG_INFO, gettext("libsldap: ldap_set_option "
375e1dd0a2fSth 		"(LDAP_OPT_EXTRA_THREAD_FN_PTRS)"));
376e1dd0a2fSth 		return (0);
377e1dd0a2fSth 	}
378e1dd0a2fSth 
379e1dd0a2fSth 	return (1);
380e1dd0a2fSth }
381e1dd0a2fSth 
382e1dd0a2fSth /* set up an MT connection for sharing among multiple threads */
383e1dd0a2fSth static int
setup_mt_ld(LDAP * ld,ns_conn_mgmt_t * cmg)384e1dd0a2fSth setup_mt_ld(LDAP *ld, ns_conn_mgmt_t *cmg)
385e1dd0a2fSth {
386e1dd0a2fSth 	thread_t	t = thr_self();
387e1dd0a2fSth 
388e1dd0a2fSth 	/* set up the per-thread data for using the MT connection */
389e1dd0a2fSth 	if (conn_tsd_setup(cmg) == -1) {
390e1dd0a2fSth 		syslog(LOG_WARNING,
391e1dd0a2fSth 		    gettext("libsldap: tid= %d: unable to set up TSD\n"), t);
392e1dd0a2fSth 		return (-1);
393e1dd0a2fSth 	}
394e1dd0a2fSth 
395e1dd0a2fSth 	if (setup_mt_conn(ld) == 0) {
396e1dd0a2fSth 		/* multiple threads per connection not supported */
397e1dd0a2fSth 		syslog(LOG_WARNING, gettext("libsldap: tid= %d: multiple "
398e1dd0a2fSth 		    "threads per connection not supported\n"), t);
399e1dd0a2fSth 		conn_tsd_free();
400e1dd0a2fSth 		return (-1);
401e1dd0a2fSth 	}
402e1dd0a2fSth 	return (0);
403e1dd0a2fSth }
404e1dd0a2fSth 
405e1dd0a2fSth /*
406e1dd0a2fSth  * Check name and UID of process, if it is nscd.
407e1dd0a2fSth  *
408e1dd0a2fSth  * Input:
409e1dd0a2fSth  *   pid	: PID of checked process
410e1dd0a2fSth  *   check_uid	: check if UID == 0
411e1dd0a2fSth  * Output:
412e1dd0a2fSth  *   B_TRUE	: nscd detected
413e1dd0a2fSth  *   B_FALSE	: nscd not confirmed
414e1dd0a2fSth  */
415e1dd0a2fSth static boolean_t
check_nscd_proc(pid_t pid,boolean_t check_uid)416e1dd0a2fSth check_nscd_proc(pid_t pid, boolean_t check_uid)
417e1dd0a2fSth {
418e1dd0a2fSth 	psinfo_t	pinfo;
419e1dd0a2fSth 	char		fname[MAXPATHLEN];
420e1dd0a2fSth 	ssize_t		ret;
421e1dd0a2fSth 	int		fd;
422e1dd0a2fSth 
423e1dd0a2fSth 	if (snprintf(fname, MAXPATHLEN, "/proc/%d/psinfo", pid) > 0) {
424e1dd0a2fSth 		if ((fd = open(fname,  O_RDONLY)) >= 0) {
425e1dd0a2fSth 			ret = read(fd, &pinfo, sizeof (psinfo_t));
426e1dd0a2fSth 			(void) close(fd);
427e1dd0a2fSth 			if ((ret == sizeof (psinfo_t)) &&
428e1dd0a2fSth 			    (strcmp(pinfo.pr_fname, "nscd") == 0)) {
429e1dd0a2fSth 				if (check_uid && (pinfo.pr_uid != 0))
430e1dd0a2fSth 					return (B_FALSE);
431e1dd0a2fSth 				return (B_TRUE);
432e1dd0a2fSth 			}
433e1dd0a2fSth 		}
434e1dd0a2fSth 	}
435e1dd0a2fSth 	return (B_FALSE);
436e1dd0a2fSth }
437e1dd0a2fSth 
438e1dd0a2fSth /*
439e1dd0a2fSth  * Check if this process is peruser nscd.
440e1dd0a2fSth  */
441e1dd0a2fSth boolean_t
__s_api_peruser_proc(void)442e1dd0a2fSth __s_api_peruser_proc(void)
443e1dd0a2fSth {
444e1dd0a2fSth 	pid_t		my_ppid;
445e1dd0a2fSth 	static mutex_t	nscdLock = DEFAULTMUTEX;
446e1dd0a2fSth 	static pid_t	checkedPpid = (pid_t)-1;
447e1dd0a2fSth 	static boolean_t isPeruserNscd = B_FALSE;
448e1dd0a2fSth 
449e1dd0a2fSth 	my_ppid = getppid();
450e1dd0a2fSth 
451e1dd0a2fSth 	/*
452e1dd0a2fSth 	 * Already checked before for this process? If yes, return cached
453e1dd0a2fSth 	 * response.
454e1dd0a2fSth 	 */
455e1dd0a2fSth 	if (my_ppid == checkedPpid) {
456e1dd0a2fSth 		return (isPeruserNscd);
457e1dd0a2fSth 	}
458e1dd0a2fSth 
459e1dd0a2fSth 	(void) mutex_lock(&nscdLock);
460e1dd0a2fSth 
461e1dd0a2fSth 	/* Check once more incase another thread has just complete this. */
462e1dd0a2fSth 	if (my_ppid == checkedPpid) {
463e1dd0a2fSth 		(void) mutex_unlock(&nscdLock);
464e1dd0a2fSth 		return (isPeruserNscd);
465e1dd0a2fSth 	}
466e1dd0a2fSth 
467e1dd0a2fSth 	/* Reinitialize to be sure there is no residue after fork. */
468e1dd0a2fSth 	isPeruserNscd = B_FALSE;
469e1dd0a2fSth 
470e1dd0a2fSth 	/* Am I the nscd process? */
471e1dd0a2fSth 	if (check_nscd_proc(getpid(), B_FALSE)) {
472e1dd0a2fSth 		/* Is my parent the nscd process with UID == 0. */
473e1dd0a2fSth 		isPeruserNscd = check_nscd_proc(my_ppid, B_TRUE);
474e1dd0a2fSth 	}
475e1dd0a2fSth 
476ca190d8dSmichen 	/* Remember for whom isPeruserNscd is. */
477e1dd0a2fSth 	checkedPpid = my_ppid;
478e1dd0a2fSth 
479e1dd0a2fSth 	(void) mutex_unlock(&nscdLock);
480e1dd0a2fSth 	return (isPeruserNscd);
481e1dd0a2fSth }
482e1dd0a2fSth 
483e1dd0a2fSth /*
484e1dd0a2fSth  * Check if this process is main nscd.
485e1dd0a2fSth  */
486e1dd0a2fSth boolean_t
__s_api_nscd_proc(void)487e1dd0a2fSth __s_api_nscd_proc(void)
488e1dd0a2fSth {
489e1dd0a2fSth 	pid_t		my_pid;
490e1dd0a2fSth 	static mutex_t	nscdLock = DEFAULTMUTEX;
491e1dd0a2fSth 	static pid_t	checkedPid = (pid_t)-1;
492e1dd0a2fSth 	static boolean_t isMainNscd = B_FALSE;
493e1dd0a2fSth 
494e1dd0a2fSth 	/*
495e1dd0a2fSth 	 * Don't bother checking if this process isn't root, this cannot
496e1dd0a2fSth 	 * be main nscd.
497e1dd0a2fSth 	 */
498e1dd0a2fSth 	if (getuid() != 0)
499e1dd0a2fSth 		return (B_FALSE);
500e1dd0a2fSth 
501e1dd0a2fSth 	my_pid = getpid();
502e1dd0a2fSth 
503e1dd0a2fSth 	/*
504e1dd0a2fSth 	 * Already checked before for this process? If yes, return cached
505e1dd0a2fSth 	 * response.
506e1dd0a2fSth 	 */
507e1dd0a2fSth 	if (my_pid == checkedPid) {
508e1dd0a2fSth 		return (isMainNscd);
509e1dd0a2fSth 	}
510e1dd0a2fSth 
511e1dd0a2fSth 	(void) mutex_lock(&nscdLock);
512e1dd0a2fSth 
513e1dd0a2fSth 	/* Check once more incase another thread has just done this. */
514e1dd0a2fSth 	if (my_pid == checkedPid) {
515e1dd0a2fSth 		(void) mutex_unlock(&nscdLock);
516e1dd0a2fSth 		return (isMainNscd);
517e1dd0a2fSth 	}
518e1dd0a2fSth 
519e1dd0a2fSth 	/*
520e1dd0a2fSth 	 * Am I the nscd process? UID is already checked, not needed from
521e1dd0a2fSth 	 * psinfo.
522e1dd0a2fSth 	 */
523e1dd0a2fSth 	isMainNscd = check_nscd_proc(my_pid, B_FALSE);
524e1dd0a2fSth 
525ca190d8dSmichen 	/* Remember for whom isMainNscd is. */
526e1dd0a2fSth 	checkedPid = my_pid;
527e1dd0a2fSth 
528e1dd0a2fSth 	(void) mutex_unlock(&nscdLock);
529e1dd0a2fSth 	return (isMainNscd);
530e1dd0a2fSth }
531e1dd0a2fSth 
532e1dd0a2fSth /*
533e1dd0a2fSth  * initialize a connection management control structure conn_mgmt_t
534e1dd0a2fSth  */
535e1dd0a2fSth ns_conn_mgmt_t *
init_conn_mgmt()536e1dd0a2fSth init_conn_mgmt()
537e1dd0a2fSth {
538e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
539e1dd0a2fSth 
540e1dd0a2fSth 	cmg = (ns_conn_mgmt_t *)calloc(1, sizeof (*cmg));
541e1dd0a2fSth 	if (cmg == NULL) {
542e1dd0a2fSth 		syslog(LOG_ERR, NS_CONN_MSG_NO_CONN_MGMT);
543e1dd0a2fSth 		return (NULL);
544e1dd0a2fSth 	}
545e1dd0a2fSth 
546e1dd0a2fSth 	/* is this process nscd or peruser nscd ? */
547e1dd0a2fSth 	cmg->is_nscd = __s_api_nscd_proc();
548e1dd0a2fSth 	cmg->is_peruser_nscd = __s_api_peruser_proc();
549e1dd0a2fSth 
550e1dd0a2fSth 	/*
551e1dd0a2fSth 	 * assume the underlying libldap allows multiple threads sharing
552e1dd0a2fSth 	 * the same ldap connection (MT connection)
553e1dd0a2fSth 	 */
554e1dd0a2fSth 	cmg->ldap_mt = B_TRUE;
555e1dd0a2fSth 	/* state is inactive until MT connection is required/requested */
556e1dd0a2fSth 	cmg->state = NS_CONN_MGMT_INACTIVE;
557e1dd0a2fSth 
558e1dd0a2fSth 	(void) mutex_init(&cmg->lock, USYNC_THREAD, NULL);
559e1dd0a2fSth 	(void) mutex_init(&cmg->cfg_lock, USYNC_THREAD, NULL);
560e1dd0a2fSth 	cmg->pid = getpid();
561e1dd0a2fSth 
562e1dd0a2fSth 	/* for nscd or peruser nscd, MT connection is required */
563e1dd0a2fSth 	if (cmg->is_nscd == B_TRUE || cmg->is_peruser_nscd == B_TRUE)
564e1dd0a2fSth 		cmg->state = NS_CONN_MGMT_ACTIVE;
565e1dd0a2fSth 
566e1dd0a2fSth 	/*
567e1dd0a2fSth 	 * reference (or initialize) the current Native LDAP configuration and
568e1dd0a2fSth 	 * if in nscd process, make it never refreshed
569e1dd0a2fSth 	 */
570e1dd0a2fSth 	cmg->config = __s_api_get_default_config_global();
571e1dd0a2fSth 	if (cmg->config == NULL)
572e1dd0a2fSth 		cmg->config = __s_api_loadrefresh_config_global();
573e1dd0a2fSth 	if (cmg->config != NULL) {
574e1dd0a2fSth 		/*
575e1dd0a2fSth 		 * main nscd get config change notice from ldap_cachemgr
576e1dd0a2fSth 		 * so won't times out and refresh the config
577e1dd0a2fSth 		 */
578e1dd0a2fSth 		if (cmg->is_nscd  == B_TRUE)
579e1dd0a2fSth 			(cmg->config)->paramList[NS_LDAP_EXP_P].ns_tm = 0;
580e1dd0a2fSth 		cmg->cfg_cookie = cmg->config->config_cookie;
581e1dd0a2fSth 	}
582e1dd0a2fSth 
583e1dd0a2fSth 	return (cmg);
584e1dd0a2fSth }
585e1dd0a2fSth 
586e1dd0a2fSth static void
mark_shutdown_or_reloaded(int op)587ca190d8dSmichen mark_shutdown_or_reloaded(int op)
588e1dd0a2fSth {
589e1dd0a2fSth 	ns_conn_mgmt_t	*cmg = ns_connmgmt;
590e1dd0a2fSth 
591e1dd0a2fSth 	(void) mutex_lock(&cmg->lock);
592e1dd0a2fSth 	if (op == NS_CONN_MGMT_OP_SHUTDOWN)
593e1dd0a2fSth 		cmg->shutting_down = B_TRUE;
594e1dd0a2fSth 	else
595e1dd0a2fSth 		cmg->cfg_reloaded = B_TRUE;
596e1dd0a2fSth 	atomic_inc_uint(&cmg->ref_cnt);
597e1dd0a2fSth 	cmg->state = NS_CONN_MGMT_DETACHED;
598e1dd0a2fSth 
599e1dd0a2fSth 	if (op == NS_CONN_MGMT_OP_RELOAD_CONFIG)
600e1dd0a2fSth 		__s_api_init_config_global(NULL);
601e1dd0a2fSth 
602e1dd0a2fSth 	(void) mutex_unlock(&cmg->lock);
603e1dd0a2fSth }
604e1dd0a2fSth 
605e1dd0a2fSth /*
606e1dd0a2fSth  * Return a pointer to the current connection management. If
607e1dd0a2fSth  * it has not been created, or is requested to recreate, then
608e1dd0a2fSth  * create and return the pointer. It is possible, the current
609e1dd0a2fSth  * one is created by the parent before fork, create a new
610e1dd0a2fSth  * one too in such a case.
611e1dd0a2fSth  */
612e1dd0a2fSth static ns_conn_mgmt_t *
get_current_conn_mgmt(int op)613e1dd0a2fSth get_current_conn_mgmt(int op)
614e1dd0a2fSth {
615e1dd0a2fSth 	ns_conn_mgmt_t	*cmg = ns_connmgmt;
616e1dd0a2fSth 	static pid_t	checked_pid = (pid_t)-1;
617e1dd0a2fSth 	pid_t		mypid;
618e1dd0a2fSth 
619e1dd0a2fSth 	mypid = getpid();
620e1dd0a2fSth 	if (cmg == NULL || checked_pid != mypid) {
621e1dd0a2fSth 		checked_pid = mypid;
622e1dd0a2fSth 
623e1dd0a2fSth 		/*
624e1dd0a2fSth 		 * if current conn_mgmt not created yet or is from parent
625e1dd0a2fSth 		 * or is requested to recreate, create it
626e1dd0a2fSth 		 */
627e1dd0a2fSth 		if (cmg == NULL || cmg->pid != mypid) {
628e1dd0a2fSth 			if (cmg != NULL) {
629e1dd0a2fSth 				/*
630e1dd0a2fSth 				 * We don't want to free the conn_mgmt
631e1dd0a2fSth 				 * allocated by the parent, since
632e1dd0a2fSth 				 * there may be ldap connections
633e1dd0a2fSth 				 * still being used. So leave it
634e1dd0a2fSth 				 * alone but keep it referenced,
635e1dd0a2fSth 				 * so that it will not be flagged
636e1dd0a2fSth 				 * as a piece of leaked memory.
637e1dd0a2fSth 				 */
638e1dd0a2fSth 				ns_connmgmt_parent = cmg;
639e1dd0a2fSth 				/*
640e1dd0a2fSth 				 * avoid lint warning; does not
641e1dd0a2fSth 				 * change the conn_mgmt in parent
642e1dd0a2fSth 				 */
643e1dd0a2fSth 				ns_connmgmt_parent->state =
644e1dd0a2fSth 				    NS_CONN_MGMT_DETACHED;
645e1dd0a2fSth 			}
646e1dd0a2fSth 			ns_connmgmt = init_conn_mgmt();
647e1dd0a2fSth 			cmg = ns_connmgmt;
648e1dd0a2fSth 			/*
649e1dd0a2fSth 			 * ensure it will not be destroyed until explicitly
650e1dd0a2fSth 			 * shut down or reloaded
651e1dd0a2fSth 			 */
652e1dd0a2fSth 			if (op == NS_CONN_MGMT_OP_REF)
653e1dd0a2fSth 				atomic_inc_uint(&cmg->ref_cnt);
654e1dd0a2fSth 		}
655e1dd0a2fSth 	}
656e1dd0a2fSth 
657e1dd0a2fSth 	return (cmg);
658e1dd0a2fSth }
659e1dd0a2fSth 
660e1dd0a2fSth static ns_conn_mgmt_t *
access_conn_mgmt(int op)661e1dd0a2fSth access_conn_mgmt(int op)
662e1dd0a2fSth {
663e1dd0a2fSth 	ns_conn_mgmt_t	*cmg = NULL;
664e1dd0a2fSth 	ns_conn_mgmt_t	*cmg_prev;
665e1dd0a2fSth 
666e1dd0a2fSth 	(void) mutex_lock(&ns_connmgmt_lock);
667e1dd0a2fSth 
668e1dd0a2fSth 	/*
669e1dd0a2fSth 	 * connection management is not available when the libsldap is being
670e1dd0a2fSth 	 * unloaded or shut down
671e1dd0a2fSth 	 */
672e1dd0a2fSth 	if (ns_connmgmt_shutting_down == B_TRUE) {
673e1dd0a2fSth 		(void) mutex_unlock(&ns_connmgmt_lock);
674e1dd0a2fSth 		return (NULL);
675e1dd0a2fSth 	}
676e1dd0a2fSth 
677e1dd0a2fSth 	if (op == NS_CONN_MGMT_OP_SHUTDOWN) {
678e1dd0a2fSth 		ns_connmgmt_shutting_down = B_TRUE;
679e1dd0a2fSth 		if (ns_connmgmt != NULL) {
680e1dd0a2fSth 			cmg = ns_connmgmt;
681ca190d8dSmichen 			mark_shutdown_or_reloaded(op);
682e1dd0a2fSth 			ns_connmgmt = NULL;
683e1dd0a2fSth 		}
684e1dd0a2fSth 		(void) mutex_unlock(&ns_connmgmt_lock);
685e1dd0a2fSth 		return (cmg);
686e1dd0a2fSth 	}
687e1dd0a2fSth 
688e1dd0a2fSth 	if (op == NS_CONN_MGMT_OP_RELOAD_CONFIG ||
689e1dd0a2fSth 	    op == NS_CONN_MGMT_OP_NEW_CONFIG) {
690e1dd0a2fSth 		cmg_prev = ns_connmgmt;
691ca190d8dSmichen 		mark_shutdown_or_reloaded(op);
692e1dd0a2fSth 		/*
693e1dd0a2fSth 		 * the previous cmg (cmg_prev) will be freed later
694e1dd0a2fSth 		 * when its ref count reaches zero
695e1dd0a2fSth 		 */
696e1dd0a2fSth 		ns_connmgmt = NULL;
697e1dd0a2fSth 	}
698e1dd0a2fSth 
699e1dd0a2fSth 	cmg = get_current_conn_mgmt(op);
700e1dd0a2fSth 	if (cmg == NULL) {
701e1dd0a2fSth 		(void) mutex_unlock(&ns_connmgmt_lock);
702e1dd0a2fSth 		return (NULL);
703e1dd0a2fSth 	}
704e1dd0a2fSth 
705e1dd0a2fSth 	atomic_inc_uint(&cmg->ref_cnt);
706e1dd0a2fSth 	if (op == NS_CONN_MGMT_OP_RELOAD_CONFIG ||
707e1dd0a2fSth 	    op == NS_CONN_MGMT_OP_NEW_CONFIG)
708e1dd0a2fSth 		cmg = cmg_prev;
709e1dd0a2fSth 	else { /* op is  NS_CONN_MGMT_OP_REF  or NS_CONN_MGMT_OP_LIB_INIT */
710e1dd0a2fSth 		if (cmg->config == NULL)
711e1dd0a2fSth 			cmg->config = __s_api_get_default_config();
712e1dd0a2fSth 	}
713e1dd0a2fSth 
714e1dd0a2fSth 	(void) mutex_unlock(&ns_connmgmt_lock);
715e1dd0a2fSth 	return (cmg);
716e1dd0a2fSth }
717e1dd0a2fSth 
718e1dd0a2fSth /*
719e1dd0a2fSth  * free a connection management control
720e1dd0a2fSth  */
721e1dd0a2fSth static void
free_conn_mgmt(ns_conn_mgmt_t * cmg)722e1dd0a2fSth free_conn_mgmt(ns_conn_mgmt_t *cmg)
723e1dd0a2fSth {
724e1dd0a2fSth 	union {
725e1dd0a2fSth 		ldap_data_t	s_d;
726e1dd0a2fSth 		char		s_b[1024];
727e1dd0a2fSth 	} space;
728e1dd0a2fSth 	ldap_data_t	*sptr;
729e1dd0a2fSth 	int		ndata;
730e1dd0a2fSth 	int		adata;
731e1dd0a2fSth 	int		rc;
732e1dd0a2fSth 	ldap_get_chg_cookie_t cookie;
733e1dd0a2fSth 
734e1dd0a2fSth 	if (cmg == NULL)
735e1dd0a2fSth 		return;
736e1dd0a2fSth 	cookie = cmg->cfg_cookie;
737e1dd0a2fSth 
738e1dd0a2fSth 	__s_api_free2dArray(cmg->pservers);
739e1dd0a2fSth 	/* destroy the previous config or release the current one */
740e1dd0a2fSth 	if (cmg->config != NULL) {
741e1dd0a2fSth 		if (cmg->state == NS_CONN_MGMT_DETACHED)
742e1dd0a2fSth 			__s_api_destroy_config(cmg->config);
743e1dd0a2fSth 		else
744e1dd0a2fSth 			__s_api_release_config(cmg->config);
745e1dd0a2fSth 	}
746e1dd0a2fSth 
747e1dd0a2fSth 	/* stop the server status/config-change monitor thread */
748e1dd0a2fSth 	if (cmg->procchg_started == B_TRUE) {
749e1dd0a2fSth 		if (cmg->procchg_tid != thr_self()) {
750e1dd0a2fSth 			if (cmg->procchg_door_call == B_TRUE) {
751e1dd0a2fSth 				adata = sizeof (ldap_call_t) + 1;
752e1dd0a2fSth 				ndata = sizeof (space);
753e1dd0a2fSth 				space.s_d.ldap_call.ldap_callnumber =
754e1dd0a2fSth 				    GETSTATUSCHANGE;
755e1dd0a2fSth 				space.s_d.ldap_call.ldap_u.get_change.op =
756e1dd0a2fSth 				    NS_STATUS_CHANGE_OP_STOP;
757e1dd0a2fSth 				space.s_d.ldap_call.ldap_u.get_change.cookie =
758e1dd0a2fSth 				    cookie;
759e1dd0a2fSth 				sptr = &space.s_d;
760e1dd0a2fSth 				rc = __ns_ldap_trydoorcall(&sptr, &ndata,
761e1dd0a2fSth 				    &adata);
762e1dd0a2fSth 				if (rc != NS_CACHE_SUCCESS)
763e1dd0a2fSth 					syslog(LOG_INFO,
764e1dd0a2fSth 					    gettext("libsldap: "
765e1dd0a2fSth 					    "free_conn_mgmt():"
766e1dd0a2fSth 					    " stopping door call "
767e1dd0a2fSth 					    " GETSTATUSCHANGE failed "
768e1dd0a2fSth 					    " (rc = %d)"), rc);
769e1dd0a2fSth 			}
770e1dd0a2fSth 			(void) pthread_cancel(cmg->procchg_tid);
771e1dd0a2fSth 			cmg->procchg_started = B_FALSE;
772e1dd0a2fSth 		}
773e1dd0a2fSth 	}
774e1dd0a2fSth 
775e1dd0a2fSth 	free(cmg);
776e1dd0a2fSth }
777e1dd0a2fSth 
778e1dd0a2fSth static ns_conn_mgmt_t *
release_conn_mgmt(ns_conn_mgmt_t * cmg,boolean_t unlock_cmg)779e1dd0a2fSth release_conn_mgmt(ns_conn_mgmt_t *cmg, boolean_t unlock_cmg)
780e1dd0a2fSth {
781e1dd0a2fSth 	if (cmg == NULL)
782e1dd0a2fSth 		return (NULL);
783e1dd0a2fSth 	if (atomic_dec_uint_nv(&cmg->ref_cnt) == 0) {
784e1dd0a2fSth 		if (cmg->state == NS_CONN_MGMT_DETACHED) {
785e1dd0a2fSth 			if (unlock_cmg == B_TRUE)
786e1dd0a2fSth 				(void) mutex_unlock(&cmg->lock);
787e1dd0a2fSth 			free_conn_mgmt(cmg);
78829836b19Smichen 			__s_api_free_sessionPool();
789e1dd0a2fSth 			return (NULL);
790e1dd0a2fSth 		} else {
791e1dd0a2fSth 			syslog(LOG_WARNING,
792e1dd0a2fSth 			    gettext("libsldap: connection management "
793e1dd0a2fSth 			    " has a refcount of zero but the state "
794e1dd0a2fSth 			    " is not DETACHED (%d)"), cmg->state);
795e1dd0a2fSth 			cmg = NULL;
796e1dd0a2fSth 		}
797e1dd0a2fSth 	}
798e1dd0a2fSth 	return (cmg);
799e1dd0a2fSth }
800e1dd0a2fSth 
801e1dd0a2fSth /*
802e1dd0a2fSth  * exposed function for initializing a connection management control structure
803e1dd0a2fSth  */
804e1dd0a2fSth ns_conn_mgmt_t *
__s_api_conn_mgmt_init()805e1dd0a2fSth __s_api_conn_mgmt_init()
806e1dd0a2fSth {
807e1dd0a2fSth 	if (thr_keycreate_once(&ns_mtckey, ns_mtckey_cleanup) != 0) {
808e1dd0a2fSth 		syslog(LOG_WARNING, NS_CONN_MSG_NO_MTC_KEY);
809e1dd0a2fSth 		return (NULL);
810e1dd0a2fSth 	}
811e1dd0a2fSth 
812e1dd0a2fSth 	if (thr_keycreate_once(&ns_cmgkey, NULL) != 0) {
813e1dd0a2fSth 		syslog(LOG_WARNING, NS_CONN_MSG_NO_CMG_KEY);
814e1dd0a2fSth 		return (NULL);
815e1dd0a2fSth 	}
816e1dd0a2fSth 
817e1dd0a2fSth 	return (access_conn_mgmt(NS_CONN_MGMT_OP_LIB_INIT));
818e1dd0a2fSth }
819e1dd0a2fSth 
820e1dd0a2fSth /* initialize a connection user */
821e1dd0a2fSth ns_conn_user_t *
__s_api_conn_user_init(int type,void * userinfo,boolean_t referral)822e1dd0a2fSth __s_api_conn_user_init(int type, void *userinfo, boolean_t referral)
823e1dd0a2fSth {
824e1dd0a2fSth 	ns_conn_user_t	*cu;
825e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
826e1dd0a2fSth 
827e1dd0a2fSth 	/* delete the reference to the previously used conn_mgmt */
828e1dd0a2fSth 	(void) thr_setspecific(ns_cmgkey, NULL);
829e1dd0a2fSth 
830e1dd0a2fSth 	cmg = access_conn_mgmt(NS_CONN_MGMT_OP_REF);
831e1dd0a2fSth 	if (cmg == NULL)
832e1dd0a2fSth 		return (NULL);
833e1dd0a2fSth 
834e1dd0a2fSth 	if (cmg->state != NS_CONN_MGMT_ACTIVE &&
835e1dd0a2fSth 	    cmg->state != NS_CONN_MGMT_INACTIVE) {
836e1dd0a2fSth 		atomic_dec_uint(&cmg->ref_cnt);
837e1dd0a2fSth 		return (NULL);
838e1dd0a2fSth 	}
839e1dd0a2fSth 
840e1dd0a2fSth 	cu = (ns_conn_user_t *)calloc(1, sizeof (*cu));
841e1dd0a2fSth 	if (cu == NULL) {
842e1dd0a2fSth 		atomic_dec_uint(&cmg->ref_cnt);
843e1dd0a2fSth 		return (NULL);
844e1dd0a2fSth 	}
845e1dd0a2fSth 
846e1dd0a2fSth 	cu->type = type;
847e1dd0a2fSth 	cu->state = NS_CONN_USER_ALLOCATED;
848e1dd0a2fSth 	cu->tid = thr_self();
849e1dd0a2fSth 	cu->userinfo = userinfo;
850e1dd0a2fSth 	cu->referral = referral;
851e1dd0a2fSth 	cu->ns_rc = NS_LDAP_SUCCESS;
852e1dd0a2fSth 	cu->conn_mgmt = cmg;
853e1dd0a2fSth 
854e1dd0a2fSth 	(void) conn_tsd_setup(cmg);
855e1dd0a2fSth 
856e1dd0a2fSth 	return (cu);
857e1dd0a2fSth }
858e1dd0a2fSth 
859e1dd0a2fSth /*
860e1dd0a2fSth  * Free the resources used by a connection user.
861e1dd0a2fSth  * The caller should ensure this conn_user is
862e1dd0a2fSth  * not associated with any conn_mt, i.e.,
863e1dd0a2fSth  * not in any conn_mt's linked list of conn_users.
864e1dd0a2fSth  * The caller needs to free the userinfo member
865e1dd0a2fSth  * as well.
866e1dd0a2fSth  */
867e1dd0a2fSth void
__s_api_conn_user_free(ns_conn_user_t * cu)868e1dd0a2fSth __s_api_conn_user_free(ns_conn_user_t *cu)
869e1dd0a2fSth {
870e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
871e1dd0a2fSth 
872e1dd0a2fSth 	if (cu == NULL)
873e1dd0a2fSth 		return;
874e1dd0a2fSth 
875e1dd0a2fSth 	cu->state = NS_CONN_USER_FREED;
876e1dd0a2fSth 	if (cu->ns_error != NULL)
877e1dd0a2fSth 		(void) __ns_ldap_freeError(&cu->ns_error);
878e1dd0a2fSth 
879e1dd0a2fSth 	cmg = cu->conn_mgmt;
880e1dd0a2fSth 	conn_tsd_free();
881e1dd0a2fSth 	(void) release_conn_mgmt(cmg, B_FALSE);
882e1dd0a2fSth 	(void) free(cu);
883e1dd0a2fSth }
884e1dd0a2fSth 
885e1dd0a2fSth /*
886e1dd0a2fSth  * Initialize an MT connection control structure
887e1dd0a2fSth  * that will be used to represent an ldap connection
888e1dd0a2fSth  * to be shared among multiple threads and to hold
889e1dd0a2fSth  * and manage all the conn_users using the ldap
890e1dd0a2fSth  * connection.
891e1dd0a2fSth  */
892e1dd0a2fSth static ns_conn_mt_t *
init_conn_mt(ns_conn_mgmt_t * cmg,ns_ldap_error_t ** ep)893e1dd0a2fSth init_conn_mt(ns_conn_mgmt_t *cmg, ns_ldap_error_t **ep)
894e1dd0a2fSth {
895e1dd0a2fSth 	ns_conn_mt_t	*cm;
896e1dd0a2fSth 	ns_conn_mgmt_t	*cmg_a;
897e1dd0a2fSth 
898e1dd0a2fSth 	cm = (ns_conn_mt_t *)calloc(1, sizeof (*cm));
899e1dd0a2fSth 	if (cm == NULL) {
900e1dd0a2fSth 		if (ep != NULL)
901e1dd0a2fSth 			*ep = __s_api_make_error(NS_LDAP_MEMORY, NULL);
902e1dd0a2fSth 		return (NULL);
903e1dd0a2fSth 	}
904e1dd0a2fSth 
905e1dd0a2fSth 	cmg_a = access_conn_mgmt(NS_CONN_MGMT_OP_REF);
906e1dd0a2fSth 	if (cmg_a != cmg) {
907e1dd0a2fSth 		if (cmg_a != NULL) {
908e1dd0a2fSth 			(void) release_conn_mgmt(cmg_a, B_FALSE);
909e1dd0a2fSth 			if (ep != NULL)
910e1dd0a2fSth 				*ep = __s_api_make_error(NS_LDAP_OP_FAILED,
911e1dd0a2fSth 				    NS_CONN_MSG_SHUTDOWN_RELOADED);
912e1dd0a2fSth 		}
913e1dd0a2fSth 		return (NULL);
914e1dd0a2fSth 	}
915e1dd0a2fSth 
916e1dd0a2fSth 	(void) mutex_init(&cm->lock, USYNC_THREAD, NULL);
917e1dd0a2fSth 	cm->state = NS_CONN_MT_CONNECTING;
918e1dd0a2fSth 	cm->tid = thr_self();
919e1dd0a2fSth 	cm->pid = getpid();
920e1dd0a2fSth 	cm->next = NULL;
921e1dd0a2fSth 	cm->cu_head = NULL;
922e1dd0a2fSth 	cm->cu_tail = NULL;
923e1dd0a2fSth 	cm->conn = NULL;
924e1dd0a2fSth 	cm->conn_mgmt = cmg;
925e1dd0a2fSth 
926e1dd0a2fSth 	return (cm);
927e1dd0a2fSth }
928e1dd0a2fSth 
929e1dd0a2fSth /*
930e1dd0a2fSth  * Free an MT connection control structure, assume conn_mgmt is locked.
931e1dd0a2fSth  * 'unlock_cmg' is passed to release_conn_mgmt() to indicate the
932e1dd0a2fSth  * cmg needs to be unlocked or not.
933e1dd0a2fSth  */
934e1dd0a2fSth static ns_conn_mgmt_t *
free_conn_mt(ns_conn_mt_t * cm,int unlock_cmg)935e1dd0a2fSth free_conn_mt(ns_conn_mt_t *cm, int unlock_cmg)
936e1dd0a2fSth {
937e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
938e1dd0a2fSth 
939e1dd0a2fSth 	if (cm == NULL)
940e1dd0a2fSth 		return (NULL);
941e1dd0a2fSth 	if (cm->ns_error != NULL)
942e1dd0a2fSth 		(void) __ns_ldap_freeError(&cm->ns_error);
943e1dd0a2fSth 	if (cm->conn != NULL) {
944e1dd0a2fSth 		if (cm->conn->ld != NULL)
945e1dd0a2fSth 			(void) ldap_unbind(cm->conn->ld);
946e1dd0a2fSth 		__s_api_freeConnection(cm->conn);
947e1dd0a2fSth 	}
948e1dd0a2fSth 	cmg = cm->conn_mgmt;
949e1dd0a2fSth 	free(cm);
950e1dd0a2fSth 	return (release_conn_mgmt(cmg, unlock_cmg));
951e1dd0a2fSth }
952e1dd0a2fSth 
953e1dd0a2fSth /* add a connection user to an MT connection */
954e1dd0a2fSth static void
add_cu2cm(ns_conn_user_t * cu,ns_conn_mt_t * cm)955e1dd0a2fSth add_cu2cm(ns_conn_user_t *cu, ns_conn_mt_t *cm)
956e1dd0a2fSth {
957e1dd0a2fSth 
958e1dd0a2fSth 	if (cm->cu_head == NULL) {
959e1dd0a2fSth 		cm->cu_head = cu;
960e1dd0a2fSth 		cm->cu_tail = cu;
961e1dd0a2fSth 	} else {
962e1dd0a2fSth 		cm->cu_tail->next = cu;
963e1dd0a2fSth 		cm->cu_tail = cu;
964e1dd0a2fSth 	}
965e1dd0a2fSth 	cm->cu_cnt++;
966e1dd0a2fSth }
967e1dd0a2fSth 
968e1dd0a2fSth /* add an MT connection to the connection management */
969e1dd0a2fSth static void
add_cm2cmg(ns_conn_mt_t * cm,ns_conn_mgmt_t * cmg)970e1dd0a2fSth add_cm2cmg(ns_conn_mt_t *cm, ns_conn_mgmt_t *cmg)
971e1dd0a2fSth {
972e1dd0a2fSth 	/*
973e1dd0a2fSth 	 * add connection opened for WRITE to top of list
974e1dd0a2fSth 	 * for garbage collection purpose. This is to
975e1dd0a2fSth 	 * ensure the connection will be closed after a
976e1dd0a2fSth 	 * certain amount of time (60 seconds).
977e1dd0a2fSth 	 */
978e1dd0a2fSth 	if (cmg->cm_head == NULL) {
979e1dd0a2fSth 		cmg->cm_head = cm;
980e1dd0a2fSth 		cmg->cm_tail = cm;
981e1dd0a2fSth 	} else {
982e1dd0a2fSth 		if (cm->opened_for == NS_CONN_USER_WRITE) {
983e1dd0a2fSth 			cm->next = cmg->cm_head;
984e1dd0a2fSth 			cmg->cm_head = cm;
985e1dd0a2fSth 		} else {
986e1dd0a2fSth 			cmg->cm_tail->next = cm;
987e1dd0a2fSth 			cmg->cm_tail = cm;
988e1dd0a2fSth 		}
989e1dd0a2fSth 	}
990e1dd0a2fSth 	cmg->cm_cnt++;
991e1dd0a2fSth }
992e1dd0a2fSth 
993e1dd0a2fSth /* delete a connection user from an MT connection */
994e1dd0a2fSth static void
del_cu4cm(ns_conn_user_t * cu,ns_conn_mt_t * cm)995e1dd0a2fSth del_cu4cm(ns_conn_user_t *cu, ns_conn_mt_t *cm)
996e1dd0a2fSth {
997e1dd0a2fSth 	ns_conn_user_t *pu, *u;
998e1dd0a2fSth 
999e1dd0a2fSth 	if (cu == NULL || cm->cu_head == NULL || cm->cu_cnt == 0)
1000e1dd0a2fSth 		return;
1001e1dd0a2fSth 
1002e1dd0a2fSth 	/* only one conn_user on list */
1003e1dd0a2fSth 	if (cm->cu_head == cm->cu_tail) {
1004e1dd0a2fSth 		if (cu == cm->cu_head) {
1005e1dd0a2fSth 			cm->cu_head = cm->cu_tail = NULL;
1006e1dd0a2fSth 			cm->cu_cnt = 0;
1007e1dd0a2fSth 			cu->next = NULL;
1008e1dd0a2fSth 		}
1009e1dd0a2fSth 		return;
1010e1dd0a2fSth 	}
1011e1dd0a2fSth 
1012e1dd0a2fSth 	/* more than one and cu is the first one */
1013e1dd0a2fSth 	if (cu == cm->cu_head) {
1014e1dd0a2fSth 		cm->cu_head = cu->next;
1015e1dd0a2fSth 		cm->cu_cnt--;
1016e1dd0a2fSth 		cu->next = NULL;
1017e1dd0a2fSth 		return;
1018e1dd0a2fSth 	}
1019e1dd0a2fSth 
1020e1dd0a2fSth 	pu = cm->cu_head;
1021e1dd0a2fSth 	for (u = cm->cu_head->next; u; u = u->next) {
1022e1dd0a2fSth 		if (cu == u)
1023e1dd0a2fSth 			break;
1024e1dd0a2fSth 		pu = u;
1025e1dd0a2fSth 	}
1026e1dd0a2fSth 	if (pu != cm->cu_tail) {
1027e1dd0a2fSth 		pu->next = cu->next;
1028e1dd0a2fSth 		if (pu->next == NULL)
1029e1dd0a2fSth 			cm->cu_tail = pu;
1030e1dd0a2fSth 		cm->cu_cnt--;
1031e1dd0a2fSth 		cu->next = NULL;
1032e1dd0a2fSth 	} else {
1033e1dd0a2fSth 		syslog(LOG_INFO, gettext(
1034e1dd0a2fSth 		    "libsldap: del_cu4cm(): connection user not found"));
1035e1dd0a2fSth 	}
1036e1dd0a2fSth }
1037e1dd0a2fSth 
1038e1dd0a2fSth /* delete an MT connection from the connection management control structure */
1039e1dd0a2fSth static void
del_cm4cmg(ns_conn_mt_t * cm,ns_conn_mgmt_t * cmg)1040e1dd0a2fSth del_cm4cmg(ns_conn_mt_t *cm, ns_conn_mgmt_t *cmg)
1041e1dd0a2fSth {
1042e1dd0a2fSth 	ns_conn_mt_t *pm, *m;
1043e1dd0a2fSth 
1044e1dd0a2fSth 	if (cm == NULL || cmg->cm_head == NULL || cmg->cm_cnt == 0)
1045e1dd0a2fSth 		return;
1046e1dd0a2fSth 
1047e1dd0a2fSth 	/* only one conn_mt on list */
1048e1dd0a2fSth 	if (cmg->cm_head == cmg->cm_tail) {
1049e1dd0a2fSth 		if (cm == cmg->cm_head) {
1050e1dd0a2fSth 			cmg->cm_head = cmg->cm_tail = NULL;
1051e1dd0a2fSth 			cmg->cm_cnt = 0;
1052e1dd0a2fSth 			cm->next = NULL;
1053e1dd0a2fSth 		}
1054e1dd0a2fSth 		return;
1055e1dd0a2fSth 	}
1056e1dd0a2fSth 
1057e1dd0a2fSth 	/* more than one and cm is the first one */
1058e1dd0a2fSth 	if (cm == cmg->cm_head) {
1059e1dd0a2fSth 		cmg->cm_head = cm->next;
1060e1dd0a2fSth 		cmg->cm_cnt--;
1061e1dd0a2fSth 		cm->next = NULL;
1062e1dd0a2fSth 		return;
1063e1dd0a2fSth 	}
1064e1dd0a2fSth 
1065e1dd0a2fSth 	pm = cmg->cm_head;
1066e1dd0a2fSth 	for (m = cmg->cm_head->next; m; m = m->next) {
1067e1dd0a2fSth 		if (cm == m)
1068e1dd0a2fSth 			break;
1069e1dd0a2fSth 		pm = m;
1070e1dd0a2fSth 	}
1071e1dd0a2fSth 	if (pm != cmg->cm_tail) {
1072e1dd0a2fSth 		pm->next = cm->next;
1073e1dd0a2fSth 		if (pm->next == NULL)
1074e1dd0a2fSth 			cmg->cm_tail = pm;
1075e1dd0a2fSth 		cmg->cm_cnt--;
1076e1dd0a2fSth 		cm->next = NULL;
1077e1dd0a2fSth 	} else {
1078e1dd0a2fSth 		syslog(LOG_INFO, gettext(
1079e1dd0a2fSth 		    "libsldap: del_cm4cmg(): MT connection not found"));
1080e1dd0a2fSth 	}
1081e1dd0a2fSth }
1082e1dd0a2fSth 
1083e1dd0a2fSth /*
1084e1dd0a2fSth  * compare to see if the server and credential for authentication match
1085e1dd0a2fSth  * those used by an MT connection
1086e1dd0a2fSth  */
1087e1dd0a2fSth static boolean_t
is_server_cred_matched(const char * server,const ns_cred_t * cred,ns_conn_mt_t * cm)1088e1dd0a2fSth is_server_cred_matched(const char *server, const ns_cred_t *cred,
1089*cc98049dSToomas Soome     ns_conn_mt_t *cm)
1090e1dd0a2fSth {
1091e1dd0a2fSth 	Connection	*cp = cm->conn;
1092e1dd0a2fSth 
1093e1dd0a2fSth 	/* check server first */
1094e1dd0a2fSth 	if (server != NULL && *server != 0) {
1095e1dd0a2fSth 		if (strcasecmp(server, cp->serverAddr) != 0)
1096e1dd0a2fSth 			return (B_FALSE);
1097e1dd0a2fSth 	}
1098e1dd0a2fSth 
1099e1dd0a2fSth 	if (cred == NULL)
1100e1dd0a2fSth 		return (B_TRUE);
1101e1dd0a2fSth 
1102e1dd0a2fSth 	/* then check cred */
1103e1dd0a2fSth 	return (__s_api_is_auth_matched(cp->auth, cred));
1104e1dd0a2fSth }
1105e1dd0a2fSth 
1106e1dd0a2fSth /*
1107e1dd0a2fSth  * Wait until a pending MT connection becomes available.
1108e1dd0a2fSth  * Return 1 if so, 0 if error.
1109e1dd0a2fSth  *
1110e1dd0a2fSth  * Assume the current conn_mgmt and the input conn_mt
1111e1dd0a2fSth  * are locked.
1112e1dd0a2fSth  */
1113e1dd0a2fSth static int
wait_for_conn_mt(ns_conn_user_t * cu,ns_conn_mt_t * cm)1114e1dd0a2fSth wait_for_conn_mt(ns_conn_user_t *cu, ns_conn_mt_t *cm)
1115e1dd0a2fSth {
1116e1dd0a2fSth 
1117e1dd0a2fSth 	cu->state = NS_CONN_USER_WAITING;
1118e1dd0a2fSth 	add_cu2cm(cu, cm);
1119e1dd0a2fSth 	cu->conn_mt = cm;
1120e1dd0a2fSth 
1121e1dd0a2fSth 	(void) mutex_unlock(&cm->lock);
1122e1dd0a2fSth 	/*
1123e1dd0a2fSth 	 * It could take some time so we don't want to hold
1124e1dd0a2fSth 	 * cm->conn_mgmt across the wait
1125e1dd0a2fSth 	 */
1126e1dd0a2fSth 	(void) mutex_unlock(&(cm->conn_mgmt)->lock);
1127e1dd0a2fSth 
1128e1dd0a2fSth 	(void) mutex_lock(&cm->lock);
1129e1dd0a2fSth 	/* check one more time see if need to wait */
1130e1dd0a2fSth 	if (cm->state == NS_CONN_MT_CONNECTING) {
1131e1dd0a2fSth 		(void) conn_wait(cm, cu);
1132e1dd0a2fSth 
1133e1dd0a2fSth 		/* cm->lock is locked again at this point */
1134e1dd0a2fSth 
1135e1dd0a2fSth 		cu->state = NS_CONN_USER_WOKEUP;
1136e1dd0a2fSth 	}
1137e1dd0a2fSth 
1138e1dd0a2fSth 	if (cm->state == NS_CONN_MT_CONNECTED)
1139e1dd0a2fSth 		return (1);
1140e1dd0a2fSth 	else {
1141e1dd0a2fSth 		del_cu4cm(cu, cm);
1142e1dd0a2fSth 		cu->conn_mt = NULL;
1143e1dd0a2fSth 		cu->bad_mt_conn = B_FALSE;
1144e1dd0a2fSth 		return (0);
1145e1dd0a2fSth 	}
1146e1dd0a2fSth }
1147e1dd0a2fSth 
1148e1dd0a2fSth /*
1149e1dd0a2fSth  * Check and see if the input MT connection '*cm' should be closed.
1150e1dd0a2fSth  * In two cases, it should be closed. If a preferred server is
1151e1dd0a2fSth  * found to be up when ldap_cachemgr is queried and reported back.
1152e1dd0a2fSth  * Or when the server being used for the connection is found to
1153e1dd0a2fSth  * be down. Return B_FALSE if the connection is not closed (or not marked
1154e1dd0a2fSth  * to be closed), otherwise unlock mutex (*cm)->lock and return B_TRUE.
1155e1dd0a2fSth  * This function assumes conn_mgmt cmg and conn_mt *cm are locked.
1156e1dd0a2fSth  */
1157e1dd0a2fSth static boolean_t
check_and_close_conn(ns_conn_mgmt_t * cmg,ns_conn_mt_t ** cm,ns_conn_user_t * cu)1158e1dd0a2fSth check_and_close_conn(ns_conn_mgmt_t *cmg, ns_conn_mt_t **cm,
1159*cc98049dSToomas Soome     ns_conn_user_t *cu)
1160*cc98049dSToomas Soome {
1161e1dd0a2fSth 
1162e1dd0a2fSth 	int rc;
1163e1dd0a2fSth 	int j;
1164e1dd0a2fSth 	int svridx = -1;
1165e1dd0a2fSth 	int upidx = -1;
1166e1dd0a2fSth 	int free_cm;
1167e1dd0a2fSth 	ns_server_info_t sinfo;
1168e1dd0a2fSth 	ns_ldap_error_t *errorp = NULL;
1169e1dd0a2fSth 
1170e1dd0a2fSth 	/*
1171e1dd0a2fSth 	 * check only if preferred servers are defined
1172e1dd0a2fSth 	 */
1173e1dd0a2fSth 	if (cmg->pservers_loaded == B_FALSE)
1174e1dd0a2fSth 		get_preferred_servers(B_FALSE, B_FALSE, cmg);
1175e1dd0a2fSth 	if (cmg->pservers == NULL)
1176e1dd0a2fSth 		return (B_FALSE);
1177e1dd0a2fSth 
1178e1dd0a2fSth 	/*
1179e1dd0a2fSth 	 * ask ldap_cachemgr for the first available server
1180e1dd0a2fSth 	 */
1181e1dd0a2fSth 	rc = __s_api_requestServer(NS_CACHE_NEW, NULL,
1182e1dd0a2fSth 	    &sinfo, &errorp, NS_CACHE_ADDR_IP);
1183e1dd0a2fSth 	if (rc != NS_LDAP_SUCCESS || sinfo.server == NULL) {
1184e1dd0a2fSth 		(void) __ns_ldap_freeError(&errorp);
1185e1dd0a2fSth 		return (B_FALSE);
1186e1dd0a2fSth 	}
1187e1dd0a2fSth 
1188e1dd0a2fSth 	/*
1189e1dd0a2fSth 	 * Did ldap_cachemgr return a preferred server ?
1190e1dd0a2fSth 	 */
1191e1dd0a2fSth 	for (j = 0; cmg->pservers[j] != NULL; j++) {
1192e1dd0a2fSth 		if (strcasecmp(sinfo.server, cmg->pservers[j]) != 0)
1193e1dd0a2fSth 			continue;
1194e1dd0a2fSth 		upidx = j;
1195e1dd0a2fSth 		break;
1196e1dd0a2fSth 	}
1197e1dd0a2fSth 
1198e1dd0a2fSth 	/*
1199e1dd0a2fSth 	 * Is the server being used a preferred one ?
1200e1dd0a2fSth 	 */
1201e1dd0a2fSth 	for (j = 0; cmg->pservers[j] != NULL; j++) {
1202e1dd0a2fSth 		if (strcasecmp(cmg->pservers[j], (*cm)->conn->serverAddr) != 0)
1203e1dd0a2fSth 			continue;
1204e1dd0a2fSth 		svridx = j;
1205e1dd0a2fSth 		break;
1206e1dd0a2fSth 	}
1207e1dd0a2fSth 
1208e1dd0a2fSth 	/*
1209e1dd0a2fSth 	 * Need to fall back to a down-but-now-up preferred server ?
1210e1dd0a2fSth 	 * A preferred server falls back to a more preferred one.
1211e1dd0a2fSth 	 * A regular one falls back to any preferred ones. So if
1212e1dd0a2fSth 	 * both are preferred ones and same index, or both
1213e1dd0a2fSth 	 * are not preferred ones, then no need to close the
1214e1dd0a2fSth 	 * connection.
1215e1dd0a2fSth 	 */
1216e1dd0a2fSth 	if ((upidx == -1 && svridx == -1) ||
1217e1dd0a2fSth 	    (upidx != -1 && svridx != -1 && upidx == svridx)) {
1218e1dd0a2fSth 		__s_api_free_server_info(&sinfo);
1219e1dd0a2fSth 		return (B_FALSE);
1220e1dd0a2fSth 	}
1221e1dd0a2fSth 
1222e1dd0a2fSth 	/*
1223e1dd0a2fSth 	 * otherwise, 4 cases, all may need to close the connection:
1224e1dd0a2fSth 	 * For case 1 and 2, both servers are preferred ones:
1225e1dd0a2fSth 	 * 1. ldap_cachemgr returned a better one to use (upidx < svridx)
1226e1dd0a2fSth 	 * 2. the server being used is down (upidx > svridx)
1227e1dd0a2fSth 	 * 3. ldap_cachemgr returned a preferred one, but the server
1228e1dd0a2fSth 	 *    being used is not, so need to fall back to the preferred server
1229e1dd0a2fSth 	 * 4. ldap_cachemgr returned a non-preferred one, but the server
1230e1dd0a2fSth 	 *    being used is a preferred one, so it must be down (since
1231e1dd0a2fSth 	 *    ldap_cachemgr always returns a preferred one when possible).
1232e1dd0a2fSth 	 * For case 1 & 3, close the READ connection when no user uses it.
1233e1dd0a2fSth 	 * For 2 and 4, close the connection with error rc, LDAP_SERVER_DOWN.
1234e1dd0a2fSth 	 */
1235e1dd0a2fSth 	if (upidx != -1 && (svridx == -1 || upidx < svridx)) { /* case 1 & 3 */
1236e1dd0a2fSth 		/* fallback does not make sense for WRITE/referred connection */
1237e1dd0a2fSth 		if ((*cm)->opened_for == NS_CONN_USER_WRITE ||
1238e1dd0a2fSth 		    (*cm)->referral == B_TRUE) {
1239e1dd0a2fSth 			__s_api_free_server_info(&sinfo);
1240e1dd0a2fSth 			return (B_FALSE);
1241e1dd0a2fSth 		}
1242e1dd0a2fSth 		free_cm = close_conn_mt_when_nouser(*cm);
1243e1dd0a2fSth 		if (cmg->shutting_down == B_FALSE)
1244e1dd0a2fSth 			cu->retry = B_TRUE;
1245e1dd0a2fSth 	} else {
1246e1dd0a2fSth 		ns_ldap_error_t *ep;
1247e1dd0a2fSth 		ep = __s_api_make_error(LDAP_SERVER_DOWN,
1248e1dd0a2fSth 		    NS_CONN_MSG_DOWN_FROM_CACHEMGR);
1249e1dd0a2fSth 		/* cu has not been attached to cm yet, use NULL as cu pointer */
1250e1dd0a2fSth 		free_cm = close_conn_mt(*cm, LDAP_SERVER_DOWN, &ep, NULL);
1251e1dd0a2fSth 		if (cmg->shutting_down == B_FALSE)
1252e1dd0a2fSth 			cu->retry = B_TRUE;
1253e1dd0a2fSth 		(void) __ns_ldap_freeError(&ep);
1254e1dd0a2fSth 	}
1255e1dd0a2fSth 
1256e1dd0a2fSth 	(void) mutex_unlock(&(*cm)->lock);
1257e1dd0a2fSth 	if (free_cm == 1) {
1258e1dd0a2fSth 		(void) free_conn_mt(*cm, 0);
1259e1dd0a2fSth 		*cm = NULL;
1260e1dd0a2fSth 	}
1261e1dd0a2fSth 
1262e1dd0a2fSth 	__s_api_free_server_info(&sinfo);
1263e1dd0a2fSth 
1264e1dd0a2fSth 	return (B_TRUE);
1265e1dd0a2fSth }
1266e1dd0a2fSth 
1267e1dd0a2fSth /*
1268e1dd0a2fSth  * Check to see if a conn_mt matches the connection criteria from
1269e1dd0a2fSth  * a conn_user. Return B_TRUE if yes, B_FALSE, otherwise. The input
1270e1dd0a2fSth  * conn_mt pointer (*cmt) may be freed and *cmt will be set to NULL
1271e1dd0a2fSth  * to indicate so.
1272e1dd0a2fSth  * conn_mt *cmt and conn_mgmt cm->conn_mgmt are assumed locked.
1273e1dd0a2fSth  * cm->lock is unlocked at exit if rc is B_FALSE.
1274e1dd0a2fSth  */
1275e1dd0a2fSth static boolean_t
match_conn_mt(ns_conn_user_t * cu,ns_conn_mt_t ** cmt,ns_conn_mt_state_t st,const char * server,const ns_cred_t * cred)1276e1dd0a2fSth match_conn_mt(ns_conn_user_t *cu, ns_conn_mt_t **cmt,
1277*cc98049dSToomas Soome     ns_conn_mt_state_t st, const char *server,
1278*cc98049dSToomas Soome     const ns_cred_t *cred)
1279e1dd0a2fSth {
1280e1dd0a2fSth 	boolean_t	matched = B_FALSE;
1281e1dd0a2fSth 	boolean_t	drop_conn;
1282e1dd0a2fSth 	int		free_cm = 0;
1283e1dd0a2fSth 	ns_conn_mt_t	*cm = *cmt;
1284e1dd0a2fSth 	ns_conn_mgmt_t	*cmg = cm->conn_mgmt;
1285e1dd0a2fSth 
1286e1dd0a2fSth 	if (cm->state != st || cm->close_when_nouser  == B_TRUE ||
1287e1dd0a2fSth 	    cm->detached == B_TRUE || cm->pid != getpid() ||
1288e1dd0a2fSth 	    cm->referral != cu->referral) {
1289e1dd0a2fSth 		(void) mutex_unlock(&cm->lock);
1290e1dd0a2fSth 		return (B_FALSE);
1291e1dd0a2fSth 	}
1292e1dd0a2fSth 
1293e1dd0a2fSth 	/*
1294e1dd0a2fSth 	 * if a conn_mt opened for WRITE is idle
1295e1dd0a2fSth 	 * long enough, then close it. To improve
1296e1dd0a2fSth 	 * the performance of applications, such
1297e1dd0a2fSth 	 * as ldapaddent, a WRITE connection is
1298e1dd0a2fSth 	 * given a short time to live in the
1299e1dd0a2fSth 	 * connection pool, expecting the write
1300e1dd0a2fSth 	 * requests to come in a quick succession.
1301e1dd0a2fSth 	 * To save resource, the connection will
1302e1dd0a2fSth 	 * be closed if idle more than 60 seconds.
1303e1dd0a2fSth 	 */
1304e1dd0a2fSth 	if (cm->opened_for == NS_CONN_USER_WRITE &&
1305e1dd0a2fSth 	    cu->type != NS_CONN_USER_WRITE && cm->cu_cnt == 0 &&
1306e1dd0a2fSth 	    ((time(NULL) - cm->access_time) > 60)) {
1307e1dd0a2fSth 		/*
1308e1dd0a2fSth 		 * NS_LDAP_INTERNAL is irrelevant here. There no
1309e1dd0a2fSth 		 * conn_user to consume the rc
1310e1dd0a2fSth 		 */
1311e1dd0a2fSth 		free_cm = close_conn_mt(cm, NS_LDAP_INTERNAL, NULL, NULL);
1312e1dd0a2fSth 		(void) mutex_unlock(&cm->lock);
1313e1dd0a2fSth 		if (free_cm == 1) {
1314e1dd0a2fSth 			(void) free_conn_mt(cm, 0);
1315e1dd0a2fSth 			*cmt = NULL;
1316e1dd0a2fSth 		}
1317e1dd0a2fSth 		return (B_FALSE);
1318e1dd0a2fSth 	}
1319e1dd0a2fSth 
1320e1dd0a2fSth 	switch (cu->type) {
1321e1dd0a2fSth 	case NS_CONN_USER_SEARCH:
1322e1dd0a2fSth 	case NS_CONN_USER_GETENT:
1323e1dd0a2fSth 		if (cm->opened_for == NS_CONN_USER_SEARCH ||
1324e1dd0a2fSth 		    cm->opened_for == NS_CONN_USER_GETENT)
1325e1dd0a2fSth 			matched = B_TRUE;
1326e1dd0a2fSth 		break;
1327e1dd0a2fSth 
1328e1dd0a2fSth 	case NS_CONN_USER_WRITE:
1329e1dd0a2fSth 		if (cm->opened_for == NS_CONN_USER_WRITE)
1330e1dd0a2fSth 			matched = B_TRUE;
1331e1dd0a2fSth 		break;
1332e1dd0a2fSth 
1333e1dd0a2fSth 	default:
1334e1dd0a2fSth 		matched = B_FALSE;
1335e1dd0a2fSth 		break;
1336e1dd0a2fSth 	}
1337e1dd0a2fSth 
1338e1dd0a2fSth 	if (matched == B_TRUE && ((server != NULL || cred != NULL) &&
1339e1dd0a2fSth 	    is_server_cred_matched(server, cred, cm) == B_FALSE))
1340e1dd0a2fSth 		matched = B_FALSE;
1341e1dd0a2fSth 
1342e1dd0a2fSth 	if (matched != B_FALSE) {
1343e1dd0a2fSth 		/*
1344e1dd0a2fSth 		 * Check and drop the 'connected' connection if
1345e1dd0a2fSth 		 * necessary. Main nscd gets status changes from
1346e1dd0a2fSth 		 * the ldap_cachemgr daemon directly via the
1347e1dd0a2fSth 		 * GETSTATUSCHANGE door call, the standalone
1348e1dd0a2fSth 		 * function works in a no ldap_cachemgr environment,
1349e1dd0a2fSth 		 * so no need to check and drop connections.
1350e1dd0a2fSth 		 */
1351e1dd0a2fSth 		if (cm->state == NS_CONN_MT_CONNECTED &&
1352e1dd0a2fSth 		    cmg->is_nscd == B_FALSE && !__s_api_isStandalone()) {
1353e1dd0a2fSth 			drop_conn = check_and_close_conn(cmg, &cm, cu);
1354e1dd0a2fSth 			if (drop_conn == B_TRUE) {
1355e1dd0a2fSth 				if (cm == NULL)
1356e1dd0a2fSth 					*cmt = NULL;
1357e1dd0a2fSth 				return (B_FALSE);
1358e1dd0a2fSth 			}
1359e1dd0a2fSth 		}
1360e1dd0a2fSth 
1361e1dd0a2fSth 		/* check if max. users using or waiting for the connection */
1362e1dd0a2fSth 		if ((cm->state == NS_CONN_MT_CONNECTED &&
1363e1dd0a2fSth 		    cm->cu_max != NS_CONN_MT_USER_NO_MAX &&
1364e1dd0a2fSth 		    cm->cu_cnt >= cm->cu_max) ||
1365e1dd0a2fSth 		    (cm->state == NS_CONN_MT_CONNECTING &&
1366e1dd0a2fSth 		    cm->cu_max != NS_CONN_MT_USER_NO_MAX &&
1367e1dd0a2fSth 		    cm->waiter_cnt >= cm->cu_max - 1))
1368e1dd0a2fSth 			matched = B_FALSE;
1369e1dd0a2fSth 	}
1370e1dd0a2fSth 
1371e1dd0a2fSth 	if (matched == B_FALSE)
1372e1dd0a2fSth 		(void) mutex_unlock(&cm->lock);
1373e1dd0a2fSth 
1374e1dd0a2fSth 	return (matched);
1375e1dd0a2fSth }
1376e1dd0a2fSth 
1377e1dd0a2fSth /*
1378e1dd0a2fSth  * obtain an MT connection from the connection management for a conn_user
1379e1dd0a2fSth  *
1380e1dd0a2fSth  * Input:
1381e1dd0a2fSth  *   server	: server name or IP address
1382e1dd0a2fSth  *   flags	: libsldap API flags
1383e1dd0a2fSth  *   cred	: pointer to the user credential
1384e1dd0a2fSth  *   cu		: pointer to the conn_user structure
1385e1dd0a2fSth  * Output:
1386e1dd0a2fSth  *   session	: hold pointer to the Connection structure
1387e1dd0a2fSth  *   errorp	: hold pointer to error info (ns_ldap_error_t)
1388e1dd0a2fSth  */
1389e1dd0a2fSth int
__s_api_conn_mt_get(const char * server,const int flags,const ns_cred_t * cred,Connection ** session,ns_ldap_error_t ** errorp,ns_conn_user_t * cu)1390e1dd0a2fSth __s_api_conn_mt_get(const char *server, const int flags, const ns_cred_t *cred,
1391*cc98049dSToomas Soome     Connection **session, ns_ldap_error_t **errorp, ns_conn_user_t *cu)
1392e1dd0a2fSth {
1393e1dd0a2fSth 	int		rc;
1394e1dd0a2fSth 	int		i;
1395e1dd0a2fSth 	ns_conn_mt_t	*cn;
1396e1dd0a2fSth 	ns_conn_mt_state_t st;
1397e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
1398e1dd0a2fSth 
1399e1dd0a2fSth 	if (errorp == NULL || cu == NULL || session == NULL)
1400e1dd0a2fSth 		return (NS_LDAP_INVALID_PARAM);
1401e1dd0a2fSth 
1402e1dd0a2fSth 	*session = NULL;
1403e1dd0a2fSth 	cmg = cu->conn_mgmt;
1404e1dd0a2fSth 
1405e1dd0a2fSth 	/*
1406e1dd0a2fSth 	 * for pam_ldap, always try opening a new connection
1407e1dd0a2fSth 	 */
1408e1dd0a2fSth 	if (cu->type == NS_CONN_USER_AUTH)
1409e1dd0a2fSth 		return (NS_LDAP_NOTFOUND);
1410e1dd0a2fSth 
1411e1dd0a2fSth 	/* if need a new conn, then don't reuse */
1412e1dd0a2fSth 	if (flags & NS_LDAP_NEW_CONN)
1413e1dd0a2fSth 		return (NS_LDAP_NOTFOUND);
1414e1dd0a2fSth 
1415e1dd0a2fSth 	if (flags & NS_LDAP_KEEP_CONN)
1416e1dd0a2fSth 		cu->keep_conn = B_TRUE;
1417e1dd0a2fSth 
1418e1dd0a2fSth 	/*
1419e1dd0a2fSth 	 * We want to use MT connection only if keep-connection flag is
1420e1dd0a2fSth 	 * set or if MT was requested (or active)
1421e1dd0a2fSth 	 */
1422e1dd0a2fSth 	if (!((cmg->state == NS_CONN_MGMT_INACTIVE &&
1423e1dd0a2fSth 	    cu->keep_conn == B_TRUE) || cmg->state == NS_CONN_MGMT_ACTIVE))
1424e1dd0a2fSth 		return (NS_LDAP_NOTFOUND);
1425e1dd0a2fSth 
1426e1dd0a2fSth 	/* MT connection will be used now (if possible/available) */
1427e1dd0a2fSth 	cu->use_mt_conn = B_TRUE;
1428e1dd0a2fSth 
1429e1dd0a2fSth 	NS_CONN_CHECK_ABORT_AND_LOCK(cmg, cu, errorp);
1430e1dd0a2fSth 
1431e1dd0a2fSth 	/* first look for a connection already open */
1432e1dd0a2fSth 	st = NS_CONN_MT_CONNECTED;
1433e1dd0a2fSth 	cu->state = NS_CONN_USER_FINDING;
1434e1dd0a2fSth 	for (i = 0; i < 2; i++) {
1435e1dd0a2fSth 		for (cn = cmg->cm_head; cn; cn = cn->next) {
1436e1dd0a2fSth 			(void) mutex_lock(&cn->lock);
1437e1dd0a2fSth 			rc = match_conn_mt(cu, &cn, st, server, cred);
1438e1dd0a2fSth 			if (rc == B_FALSE && cn != NULL) /* not found */
1439e1dd0a2fSth 				continue;
1440e1dd0a2fSth 			if (cn == NULL) { /* not found and cn freed */
1441e1dd0a2fSth 				/*
1442e1dd0a2fSth 				 * as the conn_mt list could
1443e1dd0a2fSth 				 * be different due to cn's
1444e1dd0a2fSth 				 * deletion, scan the entire
1445e1dd0a2fSth 				 * conn_mt list again
1446e1dd0a2fSth 				 */
1447e1dd0a2fSth 				st = NS_CONN_MT_CONNECTED;
1448e1dd0a2fSth 				i = -1;
1449e1dd0a2fSth 				break;
1450e1dd0a2fSth 			}
1451e1dd0a2fSth 
1452e1dd0a2fSth 			/* return a connected one if found */
1453e1dd0a2fSth 			if (cn->state == NS_CONN_MT_CONNECTED) {
1454e1dd0a2fSth 				*session = cn->conn;
1455e1dd0a2fSth 				add_cu2cm(cu, cn);
1456e1dd0a2fSth 				cu->conn_mt = cn;
1457e1dd0a2fSth 				cu->state = NS_CONN_USER_CONNECTED;
1458e1dd0a2fSth 				(void) mutex_unlock(&cn->lock);
1459e1dd0a2fSth 				(void) mutex_unlock(&cmg->lock);
1460e1dd0a2fSth 				return (NS_LDAP_SUCCESS);
1461e1dd0a2fSth 			}
1462e1dd0a2fSth 
1463e1dd0a2fSth 			/*
1464e1dd0a2fSth 			 * if cn is not connecting, or allow only
1465e1dd0a2fSth 			 * one user, skip it
1466e1dd0a2fSth 			 */
1467e1dd0a2fSth 			if (cn->state != NS_CONN_MT_CONNECTING ||
1468e1dd0a2fSth 			    cn->cu_max == 1) {
1469e1dd0a2fSth 				(void) mutex_unlock(&cn->lock);
1470e1dd0a2fSth 				continue;
1471e1dd0a2fSth 			}
1472e1dd0a2fSth 
1473e1dd0a2fSth 			/* wait for the connecting conn_mt */
1474e1dd0a2fSth 			if (wait_for_conn_mt(cu, cn) != 1) {
1475e1dd0a2fSth 				/*
1476e1dd0a2fSth 				 * NS_LDAP_NOTFOUND signals that the function
1477e1dd0a2fSth 				 * __s_api_check_libldap_MT_conn_support()
1478e1dd0a2fSth 				 * detected that the lower libldap library
1479e1dd0a2fSth 				 * does not support MT connection, so return
1480e1dd0a2fSth 				 * NS_LDAP_NOTFOUND to let the caller to
1481e1dd0a2fSth 				 * open a non-MT conneciton. Otherwise,
1482e1dd0a2fSth 				 * connect error occurred, return
1483e1dd0a2fSth 				 * NS_CONN_USER_CONNECT_ERROR
1484e1dd0a2fSth 				 */
1485e1dd0a2fSth 				if (cn->ns_rc != NS_LDAP_NOTFOUND)
1486e1dd0a2fSth 					cu->state = NS_CONN_USER_CONNECT_ERROR;
1487e1dd0a2fSth 				else {
1488e1dd0a2fSth 					cu->state = NS_CONN_USER_FINDING;
1489e1dd0a2fSth 					cu->use_mt_conn = B_FALSE;
1490e1dd0a2fSth 				}
1491e1dd0a2fSth 				(void) mutex_unlock(&cn->lock);
1492e1dd0a2fSth 
1493e1dd0a2fSth 				/* cmg->lock unlocked by wait_for_conn_mt() */
1494e1dd0a2fSth 
1495e1dd0a2fSth 				return (cn->ns_rc);
1496e1dd0a2fSth 			}
1497e1dd0a2fSth 
1498e1dd0a2fSth 			/* return the newly available conn_mt */
1499e1dd0a2fSth 			*session = cn->conn;
1500e1dd0a2fSth 			cu->state = NS_CONN_USER_CONNECTED;
1501e1dd0a2fSth 			(void) mutex_unlock(&cn->lock);
1502e1dd0a2fSth 
1503e1dd0a2fSth 			/* cmg->lock unlocked by wait_for_conn_mt() */
1504e1dd0a2fSth 
1505e1dd0a2fSth 			return (NS_LDAP_SUCCESS);
1506e1dd0a2fSth 		}
1507e1dd0a2fSth 
1508e1dd0a2fSth 		/* next, look for a connecting conn_mt */
1509e1dd0a2fSth 		if (i == 0)
1510e1dd0a2fSth 			st = NS_CONN_MT_CONNECTING;
1511e1dd0a2fSth 	}
1512e1dd0a2fSth 
1513e1dd0a2fSth 	/* no connection found, start opening one */
1514e1dd0a2fSth 	cn = init_conn_mt(cmg, errorp);
1515e1dd0a2fSth 	if (cn == NULL) {
1516e1dd0a2fSth 		(void) mutex_unlock(&cmg->lock);
1517e1dd0a2fSth 		return ((*errorp)->status);
1518e1dd0a2fSth 	}
1519e1dd0a2fSth 	cu->conn_mt = cn;
1520e1dd0a2fSth 	cn->opened_for = cu->type;
1521e1dd0a2fSth 	cn->referral = cu->referral;
1522e1dd0a2fSth 	if (cmg->ldap_mt == B_TRUE)
1523e1dd0a2fSth 		cn->cu_max = NS_CONN_MT_USER_MAX;
1524e1dd0a2fSth 	else
1525e1dd0a2fSth 		cn->cu_max = 1;
1526e1dd0a2fSth 	add_cm2cmg(cn, cmg);
1527e1dd0a2fSth 	(void) mutex_unlock(&cmg->lock);
1528e1dd0a2fSth 
1529e1dd0a2fSth 	return (NS_LDAP_NOTFOUND);
1530e1dd0a2fSth }
1531e1dd0a2fSth 
1532e1dd0a2fSth 
1533e1dd0a2fSth /*
1534e1dd0a2fSth  * add an MT connection to the connection management
1535e1dd0a2fSth  *
1536e1dd0a2fSth  * Input:
1537e1dd0a2fSth  *   con	: pointer to the Connection info
1538e1dd0a2fSth  *   cu		: pointer to the conn_user structure
1539e1dd0a2fSth  * Output:
1540e1dd0a2fSth  *   ep		: hold pointer to error info (ns_ldap_error_t)
1541e1dd0a2fSth  */
1542e1dd0a2fSth int
__s_api_conn_mt_add(Connection * con,ns_conn_user_t * cu,ns_ldap_error_t ** ep)1543e1dd0a2fSth __s_api_conn_mt_add(Connection *con, ns_conn_user_t *cu, ns_ldap_error_t **ep)
1544e1dd0a2fSth {
1545e1dd0a2fSth 	ns_conn_mgmt_t	*cmg = cu->conn_mgmt;
1546e1dd0a2fSth 	ns_conn_mt_t	*cm = cu->conn_mt;
1547e1dd0a2fSth 
1548e1dd0a2fSth 	/* if the conn_mgmt is being shut down, return error */
1549e1dd0a2fSth 	NS_CONN_CHECK_ABORT_AND_LOCK(cmg, cu, ep);
1550e1dd0a2fSth 
1551e1dd0a2fSth 	/*
1552e1dd0a2fSth 	 * start the change monitor thread only if it
1553e1dd0a2fSth 	 * hasn't been started and the process is the
1554e1dd0a2fSth 	 * main nscd (not peruser nscd)
1555e1dd0a2fSth 	 */
1556e1dd0a2fSth 	if (cmg->procchg_started == B_FALSE && cmg->is_nscd == B_TRUE) {
1557e1dd0a2fSth 		start_thread(cmg);
1558e1dd0a2fSth 		cmg->procchg_started = B_TRUE;
1559e1dd0a2fSth 	}
1560e1dd0a2fSth 	(void) mutex_lock(&cm->lock);
1561e1dd0a2fSth 	cm->conn = con;
1562e1dd0a2fSth 	cm->state = NS_CONN_MT_CONNECTED;
1563e1dd0a2fSth 	cm->pid = getpid();
1564e1dd0a2fSth 	cm->create_time = time(NULL);
1565e1dd0a2fSth 	cm->access_time = cm->create_time;
1566e1dd0a2fSth 	cm->opened_for = cu->type;
1567e1dd0a2fSth 	add_cu2cm(cu, cm);
1568e1dd0a2fSth 	cu->conn_mt = cm;
1569e1dd0a2fSth 	cu->state = NS_CONN_USER_CONNECTED;
1570e1dd0a2fSth 	if (cmg->ldap_mt == B_TRUE)
1571e1dd0a2fSth 		cm->cu_max = NS_CONN_MT_USER_MAX;
1572e1dd0a2fSth 	else
1573e1dd0a2fSth 		cm->cu_max = 1;
1574e1dd0a2fSth 
1575e1dd0a2fSth 	/* wake up the waiters if any */
1576e1dd0a2fSth 	(void) conn_signal(cm);
1577e1dd0a2fSth 
1578e1dd0a2fSth 	(void) mutex_unlock(&cm->lock);
1579e1dd0a2fSth 	(void) mutex_unlock(&cmg->lock);
1580e1dd0a2fSth 
1581e1dd0a2fSth 	return (NS_LDAP_SUCCESS);
1582e1dd0a2fSth }
1583e1dd0a2fSth 
1584e1dd0a2fSth /*
1585ca190d8dSmichen  * return an MT connection to the pool when a conn user is done using it
1586e1dd0a2fSth  *
1587e1dd0a2fSth  * Input:
1588e1dd0a2fSth  *   cu		: pointer to the conn_user structure
1589e1dd0a2fSth  * Output:	NONE
1590e1dd0a2fSth  */
1591e1dd0a2fSth void
__s_api_conn_mt_return(ns_conn_user_t * cu)1592e1dd0a2fSth __s_api_conn_mt_return(ns_conn_user_t *cu)
1593e1dd0a2fSth {
1594e1dd0a2fSth 	ns_conn_mt_t	*cm;
1595e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
1596e1dd0a2fSth 
1597e1dd0a2fSth 	if (cu == NULL || cu->use_mt_conn == B_FALSE)
1598e1dd0a2fSth 		return;
1599e1dd0a2fSth 	cm = cu->conn_mt;
1600e1dd0a2fSth 	if (cm == NULL)
1601e1dd0a2fSth 		return;
1602e1dd0a2fSth 	cmg = cu->conn_mgmt;
1603e1dd0a2fSth 
1604e1dd0a2fSth 	(void) mutex_lock(&cm->lock);
1605e1dd0a2fSth 	del_cu4cm(cu, cm);
1606e1dd0a2fSth 	cu->state = NS_CONN_USER_DISCONNECTED;
1607e1dd0a2fSth 	cu->conn_mt = NULL;
1608e1dd0a2fSth 	cu->bad_mt_conn = B_FALSE;
1609e1dd0a2fSth 
1610e1dd0a2fSth 	/*
1611e1dd0a2fSth 	 *  if this MT connection is no longer needed, or not usable, and
1612e1dd0a2fSth 	 * no more conn_user uses it, then close it.
1613e1dd0a2fSth 	 */
1614e1dd0a2fSth 
1615e1dd0a2fSth 	if ((cm->close_when_nouser == B_TRUE ||
1616e1dd0a2fSth 	    cm->state != NS_CONN_MT_CONNECTED) && cm->cu_cnt == 0) {
1617e1dd0a2fSth 		(void) mutex_unlock(&cm->lock);
1618e1dd0a2fSth 		(void) mutex_lock(&cmg->lock);
1619e1dd0a2fSth 		(void) mutex_lock(&cm->lock);
1620e1dd0a2fSth 		del_cm4cmg(cm, cmg);
1621e1dd0a2fSth 		/* use ns_conn_free (instead of 1) to avoid lint warning */
1622e1dd0a2fSth 		NS_CONN_UNLOCK_AND_FREE(ns_conn_free, cm, cmg);
1623e1dd0a2fSth 	} else {
1624e1dd0a2fSth 		if (cm->state == NS_CONN_MT_CONNECTED && cm->cu_cnt == 0 &&
1625e1dd0a2fSth 		    cm->conn != NULL && cm->conn->ld != NULL) {
1626e1dd0a2fSth 			struct timeval	zerotime;
1627e1dd0a2fSth 			LDAPMessage	*res;
1628e1dd0a2fSth 
1629e1dd0a2fSth 			zerotime.tv_sec = zerotime.tv_usec = 0L;
1630e1dd0a2fSth 			/* clean up remaining results just in case */
1631e1dd0a2fSth 			while (ldap_result(cm->conn->ld, LDAP_RES_ANY,
1632e1dd0a2fSth 			    LDAP_MSG_ALL, &zerotime, &res) > 0) {
1633e1dd0a2fSth 				if (res != NULL)
1634e1dd0a2fSth 					(void) ldap_msgfree(res);
1635e1dd0a2fSth 			}
1636e1dd0a2fSth 		}
1637e1dd0a2fSth 		(void) mutex_unlock(&cm->lock);
1638e1dd0a2fSth 	}
1639e1dd0a2fSth }
1640e1dd0a2fSth 
1641e1dd0a2fSth /* save error info (rc and ns_ldap_error_t) in the conn_mt */
1642e1dd0a2fSth static void
err2cm(ns_conn_mt_t * cm,int rc,ns_ldap_error_t ** errorp)1643*cc98049dSToomas Soome err2cm(ns_conn_mt_t *cm, int rc, ns_ldap_error_t **errorp)
1644*cc98049dSToomas Soome {
1645e1dd0a2fSth 	ns_ldap_error_t	*ep;
1646e1dd0a2fSth 
1647e1dd0a2fSth 	cm->ns_rc = rc;
1648e1dd0a2fSth 	cm->ns_error = NULL;
1649e1dd0a2fSth 	if (errorp != NULL && *errorp != NULL) {
1650e1dd0a2fSth 		ep = __s_api_copy_error(*errorp);
1651e1dd0a2fSth 		if (ep == NULL)
1652e1dd0a2fSth 			cm->ns_rc = NS_LDAP_MEMORY;
1653e1dd0a2fSth 		else
1654e1dd0a2fSth 			cm->ns_error = ep;
1655e1dd0a2fSth 	}
1656e1dd0a2fSth }
1657e1dd0a2fSth 
1658e1dd0a2fSth /* copy error info (rc and ns_ldap_error_t) from conn_mt to conn_user */
1659e1dd0a2fSth static void
err_from_cm(ns_conn_user_t * cu,ns_conn_mt_t * cm)1660*cc98049dSToomas Soome err_from_cm(ns_conn_user_t *cu, ns_conn_mt_t *cm)
1661*cc98049dSToomas Soome {
1662e1dd0a2fSth 	ns_ldap_error_t	*ep;
1663e1dd0a2fSth 
1664e1dd0a2fSth 	cu->ns_rc = cm->ns_rc;
1665e1dd0a2fSth 	if (cu->ns_error != NULL)
1666e1dd0a2fSth 		(void) __ns_ldap_freeError(&cu->ns_error);
1667e1dd0a2fSth 	cu->ns_error = NULL;
1668e1dd0a2fSth 	if (cm->ns_rc != NS_LDAP_SUCCESS && cm->ns_error != NULL) {
1669e1dd0a2fSth 		ep = __s_api_copy_error(cm->ns_error);
1670e1dd0a2fSth 		if (ep == NULL)
1671e1dd0a2fSth 			cu->ns_rc = NS_LDAP_MEMORY;
1672e1dd0a2fSth 		else
1673e1dd0a2fSth 			cu->ns_error = ep;
1674e1dd0a2fSth 	}
1675e1dd0a2fSth }
1676e1dd0a2fSth 
1677e1dd0a2fSth /* copy error info (rc and ns_ldap_error_t) from caller to conn_user */
1678e1dd0a2fSth static void
err_from_caller(ns_conn_user_t * cu,int rc,ns_ldap_error_t ** errorp)1679*cc98049dSToomas Soome err_from_caller(ns_conn_user_t *cu, int rc, ns_ldap_error_t **errorp)
1680*cc98049dSToomas Soome {
1681e1dd0a2fSth 
1682e1dd0a2fSth 	cu->ns_rc = rc;
1683e1dd0a2fSth 	if (errorp != NULL) {
1684e1dd0a2fSth 		if (cu->ns_error != NULL)
1685e1dd0a2fSth 			(void) __ns_ldap_freeError(&cu->ns_error);
1686e1dd0a2fSth 		cu->ns_error = *errorp;
1687e1dd0a2fSth 		*errorp = NULL;
1688e1dd0a2fSth 	} else
1689e1dd0a2fSth 		cu->ns_error = NULL;
1690e1dd0a2fSth }
1691e1dd0a2fSth 
1692e1dd0a2fSth /*
1693e1dd0a2fSth  * remove an MT connection from the connection management when failed to open
1694e1dd0a2fSth  *
1695e1dd0a2fSth  * Input:
1696e1dd0a2fSth  *   cu		: pointer to the conn_user structure
1697e1dd0a2fSth  *   rc		: error code
1698e1dd0a2fSth  *   errorp	: pointer to pointer to error info (ns_ldap_error_t)
1699e1dd0a2fSth  * Output:
1700e1dd0a2fSth  *   errorp	: set to NULL, if none NULL cm, callers do not need to free it
1701e1dd0a2fSth  */
1702e1dd0a2fSth void
__s_api_conn_mt_remove(ns_conn_user_t * cu,int rc,ns_ldap_error_t ** errorp)1703e1dd0a2fSth __s_api_conn_mt_remove(ns_conn_user_t *cu, int rc, ns_ldap_error_t **errorp)
1704e1dd0a2fSth {
1705e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
1706e1dd0a2fSth 	ns_conn_mt_t	*cm;
1707e1dd0a2fSth 	int		free_cm = 0;
1708e1dd0a2fSth 
1709e1dd0a2fSth 	if (cu == NULL || cu->use_mt_conn == B_FALSE)
1710e1dd0a2fSth 		return;
1711e1dd0a2fSth 	if ((cm = cu->conn_mt) == NULL)
1712e1dd0a2fSth 		return;
1713e1dd0a2fSth 	cmg = cu->conn_mgmt;
1714e1dd0a2fSth 
1715e1dd0a2fSth 	(void) mutex_lock(&cmg->lock);
1716e1dd0a2fSth 	(void) mutex_lock(&cm->lock);
1717e1dd0a2fSth 	if (cm->state != NS_CONN_MT_CONNECT_ERROR) {
1718e1dd0a2fSth 		cm->state = NS_CONN_MT_CONNECT_ERROR;
1719e1dd0a2fSth 		cm->ns_rc = rc;
1720e1dd0a2fSth 		if (errorp != NULL) {
1721e1dd0a2fSth 			cm->ns_error = *errorp;
1722e1dd0a2fSth 			*errorp = NULL;
1723e1dd0a2fSth 		}
1724e1dd0a2fSth 	}
1725e1dd0a2fSth 
1726e1dd0a2fSth 	/* all the conn_users share the same error rc and ns_ldap_error_t */
1727e1dd0a2fSth 	err_from_cm(cu, cm);
1728e1dd0a2fSth 	/* wake up the waiters if any */
1729e1dd0a2fSth 	(void) conn_signal(cm);
1730e1dd0a2fSth 
1731e1dd0a2fSth 	del_cu4cm(cu, cm);
1732e1dd0a2fSth 	cu->conn_mt = NULL;
1733e1dd0a2fSth 	cu->bad_mt_conn = B_FALSE;
1734e1dd0a2fSth 	if (cm->cu_cnt == 0) {
1735e1dd0a2fSth 		del_cm4cmg(cm, cmg);
1736e1dd0a2fSth 		free_cm = 1;
1737e1dd0a2fSth 	}
1738e1dd0a2fSth 
1739e1dd0a2fSth 	NS_CONN_UNLOCK_AND_FREE(free_cm, cm, cmg);
1740e1dd0a2fSth }
1741e1dd0a2fSth 
1742e1dd0a2fSth /*
1743e1dd0a2fSth  * check to see if the underlying libldap supports multi-threaded client
1744e1dd0a2fSth  * (MT connections)
1745e1dd0a2fSth  */
1746e1dd0a2fSth int
__s_api_check_libldap_MT_conn_support(ns_conn_user_t * cu,LDAP * ld,ns_ldap_error_t ** ep)1747e1dd0a2fSth __s_api_check_libldap_MT_conn_support(ns_conn_user_t *cu, LDAP *ld,
1748*cc98049dSToomas Soome     ns_ldap_error_t **ep)
1749e1dd0a2fSth {
1750e1dd0a2fSth 	int		rc;
1751e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
1752e1dd0a2fSth 
1753e1dd0a2fSth 	/* if no need to check, just return success */
1754e1dd0a2fSth 	if (cu->conn_mt == NULL || cu->use_mt_conn == B_FALSE)
1755e1dd0a2fSth 		return (NS_LDAP_SUCCESS);
1756e1dd0a2fSth 
1757e1dd0a2fSth 	cmg = cu->conn_mgmt;
1758e1dd0a2fSth 	rc = setup_mt_ld(ld, cmg);
1759e1dd0a2fSth 
1760e1dd0a2fSth 	if (cmg->do_mt_conn == B_FALSE) {
1761e1dd0a2fSth 		/*
1762e1dd0a2fSth 		 * If the conn_mgmt is being shut down, return error.
1763e1dd0a2fSth 		 * if cmg is usable, cmg->lock will be locked. Otherwise,
1764e1dd0a2fSth 		 * this function will return with rc NS_LDAP_OP_FAILED.
1765e1dd0a2fSth 		 */
1766e1dd0a2fSth 		NS_CONN_CHECK_ABORT_AND_LOCK(cmg, cu, ep);
1767e1dd0a2fSth 		if (cmg->do_mt_conn == B_FALSE) {
1768e1dd0a2fSth 			if (rc < 0)
1769e1dd0a2fSth 				cmg->ldap_mt = B_FALSE;
1770e1dd0a2fSth 			else {
1771e1dd0a2fSth 				cmg->ldap_mt = B_TRUE;
1772e1dd0a2fSth 				if (cmg->is_nscd  == B_TRUE ||
1773e1dd0a2fSth 				    cmg->is_peruser_nscd == B_TRUE) {
1774e1dd0a2fSth 					cmg->do_mt_conn = B_TRUE;
1775e1dd0a2fSth 					cmg->state = NS_CONN_MGMT_ACTIVE;
1776e1dd0a2fSth 				}
1777e1dd0a2fSth 			}
1778e1dd0a2fSth 		}
1779e1dd0a2fSth 		(void) mutex_unlock(&cmg->lock);
1780e1dd0a2fSth 	}
1781e1dd0a2fSth 
1782e1dd0a2fSth 	if (rc < 0)
1783e1dd0a2fSth 		__s_api_conn_mt_remove(cu, NS_LDAP_NOTFOUND, NULL);
1784e1dd0a2fSth 	return (NS_LDAP_SUCCESS);
1785e1dd0a2fSth }
1786e1dd0a2fSth 
1787e1dd0a2fSth /*
1788e1dd0a2fSth  * Close an MT connection.
1789e1dd0a2fSth  * Assume cm not null and locked, assume conn_mgmt is also locked.
1790e1dd0a2fSth  * Return -1 if error, 1 if the cm should be freed, otherwise 0.
1791e1dd0a2fSth  */
1792e1dd0a2fSth static int
close_conn_mt(ns_conn_mt_t * cm,int rc,ns_ldap_error_t ** errorp,ns_conn_user_t * cu)1793e1dd0a2fSth close_conn_mt(ns_conn_mt_t *cm, int rc, ns_ldap_error_t **errorp,
1794*cc98049dSToomas Soome     ns_conn_user_t *cu)
1795e1dd0a2fSth {
1796e1dd0a2fSth 	ns_conn_mgmt_t	*cmg = cm->conn_mgmt;
1797e1dd0a2fSth 	ns_conn_mt_t	*m;
1798e1dd0a2fSth 	ns_conn_user_t	*u;
1799e1dd0a2fSth 
1800e1dd0a2fSth 	if ((cm->state != NS_CONN_MT_CONNECTED && cm->state !=
1801e1dd0a2fSth 	    NS_CONN_MT_CLOSING) || cmg->cm_head == NULL || cmg->cm_cnt == 0)
1802e1dd0a2fSth 		return (-1);
1803e1dd0a2fSth 
1804e1dd0a2fSth 	/* if the conn_mt is not in the MT connection pool, nothing to do */
1805e1dd0a2fSth 	for (m = cmg->cm_head; m; m = m->next) {
1806e1dd0a2fSth 		if (cm == m)
1807e1dd0a2fSth 			break;
1808e1dd0a2fSth 	}
1809e1dd0a2fSth 	if (m == NULL)
1810e1dd0a2fSth 		return (-1);
1811e1dd0a2fSth 
1812e1dd0a2fSth 	if (cm->state == NS_CONN_MT_CONNECTED) { /* first time in here */
1813e1dd0a2fSth 		cm->state = NS_CONN_MT_CLOSING;
1814e1dd0a2fSth 		/*
1815e1dd0a2fSth 		 * If more cu exist to consume the error info, copy
1816e1dd0a2fSth 		 * it to the cm. If the caller calls on behalf of
1817e1dd0a2fSth 		 * a cu, cu won't be NULL. Check to see if there's
1818e1dd0a2fSth 		 * more cu that needs the error info. If caller does
1819e1dd0a2fSth 		 * not have a specific cu attached to it (e.g.,
1820e1dd0a2fSth 		 * shutdown_all_conn_mt()), cu is NULL, check if at
1821e1dd0a2fSth 		 * least one cu exists.
1822e1dd0a2fSth 		 */
1823e1dd0a2fSth 		if ((cu != NULL && cm->cu_cnt > 1) ||
1824e1dd0a2fSth 		    (cu == NULL && cm->cu_cnt > 0)) {
1825e1dd0a2fSth 			err2cm(cm, rc, errorp);
1826e1dd0a2fSth 			/* wake up waiter (conn_user) if any */
1827e1dd0a2fSth 			(void) conn_signal(cm);
1828e1dd0a2fSth 		}
1829e1dd0a2fSth 
1830e1dd0a2fSth 		/* for each conn_user using the conn_mt, set bad_mt_conn flag */
1831e1dd0a2fSth 		if (cm->cu_head != NULL) {
1832e1dd0a2fSth 			for (u = cm->cu_head; u; u = u->next) {
1833e1dd0a2fSth 				u->bad_mt_conn = B_TRUE;
1834e1dd0a2fSth 				if (cmg->shutting_down == B_FALSE)
1835e1dd0a2fSth 					u->retry = B_TRUE;
1836e1dd0a2fSth 			}
1837e1dd0a2fSth 		}
1838e1dd0a2fSth 	}
1839e1dd0a2fSth 
1840e1dd0a2fSth 	/* detach the conn_mt if no more conn_user left */
1841e1dd0a2fSth 	if ((cu != NULL && cm->cu_cnt == 1) ||
1842e1dd0a2fSth 	    (cu == NULL && cm->cu_cnt ==  0)) {
1843e1dd0a2fSth 		del_cm4cmg(cm, cmg);
1844e1dd0a2fSth 		cm->detached = B_TRUE;
1845e1dd0a2fSth 		return (1);
1846e1dd0a2fSth 	}
1847e1dd0a2fSth 
1848e1dd0a2fSth 	return (0);
1849e1dd0a2fSth }
1850e1dd0a2fSth 
1851e1dd0a2fSth /*
1852e1dd0a2fSth  * An MT connection becomes bad, close it and free resources.
1853e1dd0a2fSth  * This function is called with a ns_conn_user_t representing
1854e1dd0a2fSth  * a user of the MT connection.
1855e1dd0a2fSth  *
1856e1dd0a2fSth  * Input:
1857e1dd0a2fSth  *   cu		: pointer to the conn_user structure
1858e1dd0a2fSth  *   rc		: error code
1859e1dd0a2fSth  *   errorp	: pointer to pointer to error info (ns_ldap_error_t)
1860e1dd0a2fSth  * Output:
1861e1dd0a2fSth  *   errorp	: set to NULL (if no error), callers do not need to free it
1862e1dd0a2fSth  */
1863e1dd0a2fSth void
__s_api_conn_mt_close(ns_conn_user_t * cu,int rc,ns_ldap_error_t ** errorp)1864e1dd0a2fSth __s_api_conn_mt_close(ns_conn_user_t *cu, int rc, ns_ldap_error_t **errorp)
1865e1dd0a2fSth {
1866e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
1867e1dd0a2fSth 	ns_conn_mt_t	*cm;
1868e1dd0a2fSth 	int		free_cm = 0;
1869e1dd0a2fSth 
1870e1dd0a2fSth 	if (cu == NULL || cu->use_mt_conn == B_FALSE)
1871e1dd0a2fSth 		return;
1872e1dd0a2fSth 
1873e1dd0a2fSth 	if (cu->state != NS_CONN_USER_CONNECTED || (cm = cu->conn_mt) == NULL)
1874e1dd0a2fSth 		return;
1875e1dd0a2fSth 	cmg = cu->conn_mgmt;
1876e1dd0a2fSth 
1877e1dd0a2fSth 	(void) mutex_lock(&cmg->lock);
1878e1dd0a2fSth 	(void) mutex_lock(&cm->lock);
1879e1dd0a2fSth 
1880e1dd0a2fSth 	/* close the MT connection if possible */
1881e1dd0a2fSth 	free_cm = close_conn_mt(cm, rc, errorp, cu);
1882e1dd0a2fSth 	if (free_cm == -1) { /* error case */
1883e1dd0a2fSth 		(void) mutex_unlock(&cm->lock);
1884e1dd0a2fSth 		(void) mutex_unlock(&cmg->lock);
1885e1dd0a2fSth 		return;
1886e1dd0a2fSth 	}
1887e1dd0a2fSth 
1888e1dd0a2fSth 	if (rc != NS_LDAP_SUCCESS) { /* error info passed in, use it */
1889e1dd0a2fSth 		err_from_caller(cu, rc, errorp);
1890e1dd0a2fSth 	} else { /* error not passed in, use those saved in the conn_mt */
1891e1dd0a2fSth 		err_from_cm(cu, cm);
1892e1dd0a2fSth 	}
1893e1dd0a2fSth 
1894e1dd0a2fSth 	/* detach the conn_user from the conn_mt */
1895e1dd0a2fSth 	del_cu4cm(cu, cm);
1896e1dd0a2fSth 	cu->conn_mt = NULL;
1897e1dd0a2fSth 	cu->bad_mt_conn = B_FALSE;
1898e1dd0a2fSth 	if (cmg->shutting_down == B_FALSE)
1899e1dd0a2fSth 		cu->retry = B_TRUE;
1900e1dd0a2fSth 	NS_CONN_UNLOCK_AND_FREE(free_cm, cm, cmg);
1901e1dd0a2fSth }
1902e1dd0a2fSth 
1903e1dd0a2fSth /*
1904e1dd0a2fSth  * Close an MT connection when the associated server is known to be
1905e1dd0a2fSth  * down. This function is called with a ns_conn_mt_t representing
1906e1dd0a2fSth  * the MT connection. That is, the caller is not a conn_user
1907e1dd0a2fSth  * thread but rather the procchg thread.
1908e1dd0a2fSth  */
1909e1dd0a2fSth static void
close_conn_mt_by_procchg(ns_conn_mt_t * cm,int rc,char * errmsg)1910e1dd0a2fSth close_conn_mt_by_procchg(ns_conn_mt_t *cm, int rc, char *errmsg)
1911e1dd0a2fSth {
1912e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
1913e1dd0a2fSth 	int		free_cm = 0;
1914e1dd0a2fSth 	ns_ldap_error_t	*ep;
1915e1dd0a2fSth 
1916e1dd0a2fSth 	if (cm == NULL)
1917e1dd0a2fSth 		return;
1918e1dd0a2fSth 	cmg = cm->conn_mgmt;
1919e1dd0a2fSth 
1920e1dd0a2fSth 	ep = (ns_ldap_error_t *)calloc(1, sizeof (*ep));
1921e1dd0a2fSth 	if (ep != NULL) {
1922e1dd0a2fSth 		ep->status = rc;
1923e1dd0a2fSth 		if (errmsg != NULL)
1924e1dd0a2fSth 			ep->message =  strdup(errmsg); /* OK if returns NULL */
1925e1dd0a2fSth 	}
1926e1dd0a2fSth 
1927e1dd0a2fSth 	(void) mutex_lock(&cmg->lock);
1928e1dd0a2fSth 	(void) mutex_lock(&cm->lock);
1929e1dd0a2fSth 
1930e1dd0a2fSth 	/* close the MT connection if possible */
1931e1dd0a2fSth 	free_cm = close_conn_mt(cm, LDAP_SERVER_DOWN, &ep, NULL);
1932e1dd0a2fSth 	if (free_cm == -1) { /* error case */
1933e1dd0a2fSth 		(void) mutex_unlock(&cm->lock);
1934e1dd0a2fSth 		(void) mutex_unlock(&cmg->lock);
1935e1dd0a2fSth 		return;
1936e1dd0a2fSth 	}
1937e1dd0a2fSth 	(void) __ns_ldap_freeError(&ep);
1938e1dd0a2fSth 
1939e1dd0a2fSth 	NS_CONN_UNLOCK_AND_FREE(free_cm, cm, cmg);
1940e1dd0a2fSth }
1941e1dd0a2fSth 
1942e1dd0a2fSth /*
1943e1dd0a2fSth  * Close an MT connection when there is a better server to connect to.
1944e1dd0a2fSth  * Mark the connection as to-be-closed-when-no-one-using so that
1945e1dd0a2fSth  * any outstanding ldap operations can run to completion.
1946e1dd0a2fSth  * Assume that both the conn_mt and conn_mgmt are locked.
1947e1dd0a2fSth  * Return 1 if the conn_mt should be freed.
1948e1dd0a2fSth  */
1949e1dd0a2fSth static int
close_conn_mt_when_nouser(ns_conn_mt_t * cm)1950e1dd0a2fSth close_conn_mt_when_nouser(ns_conn_mt_t *cm)
1951e1dd0a2fSth {
1952e1dd0a2fSth 	int		free_cm = 0;
1953e1dd0a2fSth 
1954e1dd0a2fSth 	if (cm->cu_cnt == 0) {
1955e1dd0a2fSth 		del_cm4cmg(cm, cm->conn_mgmt);
1956e1dd0a2fSth 		free_cm = 1;
1957e1dd0a2fSth 	} else {
1958e1dd0a2fSth 		cm->close_when_nouser = B_TRUE;
1959e1dd0a2fSth 	}
1960e1dd0a2fSth 
1961e1dd0a2fSth 	return (free_cm);
1962e1dd0a2fSth }
1963e1dd0a2fSth 
1964e1dd0a2fSth /*
1965e1dd0a2fSth  * Retrieve the configured preferred server list.
1966e1dd0a2fSth  * This function locked the conn_mgmt and does not
1967e1dd0a2fSth  * unlock at exit.
1968e1dd0a2fSth  */
1969e1dd0a2fSth static void
get_preferred_servers(boolean_t lock,boolean_t reload,ns_conn_mgmt_t * cmg)1970e1dd0a2fSth get_preferred_servers(boolean_t lock, boolean_t reload, ns_conn_mgmt_t *cmg)
1971e1dd0a2fSth {
1972e1dd0a2fSth 	ns_ldap_error_t *errorp = NULL;
1973e1dd0a2fSth 	void		**pservers = NULL;
1974e1dd0a2fSth 
1975e1dd0a2fSth 	if (lock == B_TRUE)
1976e1dd0a2fSth 		(void) mutex_lock(&cmg->lock);
1977e1dd0a2fSth 
1978e1dd0a2fSth 	/* if already done, and no reload, then return */
1979e1dd0a2fSth 	if (cmg->pservers_loaded == B_TRUE && reload == B_FALSE)
1980e1dd0a2fSth 		return;
1981e1dd0a2fSth 
1982e1dd0a2fSth 	if (cmg->pservers != NULL) {
1983e1dd0a2fSth 		(void) __ns_ldap_freeParam((void ***)&cmg->pservers);
1984e1dd0a2fSth 		cmg->pservers = NULL;
1985e1dd0a2fSth 	}
1986e1dd0a2fSth 
1987e1dd0a2fSth 	if (__ns_ldap_getParam(NS_LDAP_SERVER_PREF_P,
1988e1dd0a2fSth 	    &pservers, &errorp) == NS_LDAP_SUCCESS) {
1989e1dd0a2fSth 		cmg->pservers = (char **)pservers;
1990e1dd0a2fSth 		cmg->pservers_loaded = B_TRUE;
1991e1dd0a2fSth 	} else {
1992e1dd0a2fSth 		(void) __ns_ldap_freeError(&errorp);
1993e1dd0a2fSth 		(void) __ns_ldap_freeParam(&pservers);
1994e1dd0a2fSth 	}
1995e1dd0a2fSth }
1996e1dd0a2fSth 
1997e1dd0a2fSth /*
1998e1dd0a2fSth  * This function handles the config or server status change notification
1999e1dd0a2fSth  * from the ldap_cachemgr.
2000e1dd0a2fSth  */
2001e1dd0a2fSth static ns_conn_mgmt_t *
proc_server_change(ns_server_status_change_t * chg,ns_conn_mgmt_t * cmg)2002e1dd0a2fSth proc_server_change(ns_server_status_change_t *chg, ns_conn_mgmt_t  *cmg)
2003e1dd0a2fSth {
2004e1dd0a2fSth 	int		cnt, i, j, k, n;
2005e1dd0a2fSth 	boolean_t	loop = B_TRUE;
2006e1dd0a2fSth 	boolean_t	cmg_locked = B_FALSE;
2007*cc98049dSToomas Soome 	char		*s;
2008e1dd0a2fSth 	ns_conn_mt_t	*cm;
2009e1dd0a2fSth 	ns_conn_mgmt_t	*ocmg;
2010e1dd0a2fSth 
2011e1dd0a2fSth 	/* if config changed, reload the configuration */
2012e1dd0a2fSth 	if (chg->config_changed == B_TRUE) {
2013e1dd0a2fSth 		/* reload the conn_mgmt and Native LDAP config */
2014e1dd0a2fSth 		ocmg = access_conn_mgmt(NS_CONN_MGMT_OP_RELOAD_CONFIG);
2015e1dd0a2fSth 		shutdown_all_conn_mt(ocmg);
2016e1dd0a2fSth 		/* release the one obtained from access_conn_mgmt(RELOAD) */
2017e1dd0a2fSth 		(void) release_conn_mgmt(ocmg, B_FALSE);
2018e1dd0a2fSth 		/* release the one obtained when ocmg was created */
2019e1dd0a2fSth 		(void) release_conn_mgmt(ocmg, B_FALSE);
2020e1dd0a2fSth 		return (ocmg);
2021e1dd0a2fSth 	}
2022e1dd0a2fSth 
2023e1dd0a2fSth 	if ((cnt = chg->num_server) == 0)
2024e1dd0a2fSth 		return (cmg);
2025e1dd0a2fSth 
2026e1dd0a2fSth 	/* handle down servers first */
2027e1dd0a2fSth 	for (i = 0; i < cnt; i++) {
2028e1dd0a2fSth 
2029e1dd0a2fSth 		if (chg->changes[i] != NS_SERVER_DOWN)
2030e1dd0a2fSth 			continue;
2031e1dd0a2fSth 		s = chg->servers[i];
2032e1dd0a2fSth 
2033e1dd0a2fSth 		/*
2034e1dd0a2fSth 		 * look for a CONNECTED MT connection using
2035e1dd0a2fSth 		 * the same server s, and close it
2036e1dd0a2fSth 		 */
2037e1dd0a2fSth 		while (loop) {
2038e1dd0a2fSth 			if (cmg_locked == B_FALSE) {
2039e1dd0a2fSth 				(void) mutex_lock(&cmg->lock);
2040e1dd0a2fSth 				cmg_locked = B_TRUE;
2041e1dd0a2fSth 			}
2042e1dd0a2fSth 			for (cm = cmg->cm_head; cm; cm = cm->next) {
2043e1dd0a2fSth 				(void) mutex_lock(&cm->lock);
2044e1dd0a2fSth 
2045e1dd0a2fSth 				if (cm->state == NS_CONN_MT_CONNECTED &&
2046e1dd0a2fSth 				    cm->conn != NULL &&
2047e1dd0a2fSth 				    strcasecmp(cm->conn->serverAddr, s) == 0) {
2048e1dd0a2fSth 					(void) mutex_unlock(&cm->lock);
2049e1dd0a2fSth 					break;
2050e1dd0a2fSth 				}
2051e1dd0a2fSth 
2052e1dd0a2fSth 				(void) mutex_unlock(&cm->lock);
2053e1dd0a2fSth 			}
2054e1dd0a2fSth 			if (cm != NULL) {
2055e1dd0a2fSth 				(void) mutex_unlock(&cmg->lock);
2056e1dd0a2fSth 				cmg_locked = B_FALSE;
2057e1dd0a2fSth 				close_conn_mt_by_procchg(cm, LDAP_SERVER_DOWN,
2058e1dd0a2fSth 				    NS_CONN_MSG_DOWN_FROM_CACHEMGR);
2059e1dd0a2fSth 				/*
2060e1dd0a2fSth 				 * Process the next cm using server s.
2061e1dd0a2fSth 				 * Start from the head of the cm linked
2062e1dd0a2fSth 				 * list again, as the cm list may change
2063e1dd0a2fSth 				 * after close_conn_mt_by_procchg() is done.
2064e1dd0a2fSth 				 */
2065e1dd0a2fSth 				continue;
2066e1dd0a2fSth 			}
2067e1dd0a2fSth 
2068e1dd0a2fSth 			/*
2069e1dd0a2fSth 			 * No (more) MT connection using the down server s.
2070e1dd0a2fSth 			 * Process the next server on the list.
2071e1dd0a2fSth 			 */
2072e1dd0a2fSth 			break;
2073e1dd0a2fSth 		} /* while loop */
2074e1dd0a2fSth 	}
2075e1dd0a2fSth 
2076e1dd0a2fSth 	/*
2077e1dd0a2fSth 	 * Next handle servers whose status changed to up.
2078e1dd0a2fSth 	 * Get the preferred server list first if not done yet.
2079e1dd0a2fSth 	 * get_preferred_servers() leaves conn_mgmt locked.
2080e1dd0a2fSth 	 */
2081e1dd0a2fSth 	get_preferred_servers(cmg_locked == B_FALSE ? B_TRUE : B_FALSE,
2082e1dd0a2fSth 	    B_FALSE, cmg);
2083e1dd0a2fSth 	cmg_locked = B_TRUE;
2084e1dd0a2fSth 	/*
2085e1dd0a2fSth 	 * if no preferred server configured, we don't switch MT connection
2086e1dd0a2fSth 	 * to a more preferred server (i.e., fallback), so just return
2087e1dd0a2fSth 	 */
2088e1dd0a2fSth 	if (cmg->pservers == NULL) {
2089e1dd0a2fSth 		(void) mutex_unlock(&cmg->lock);
2090e1dd0a2fSth 		return (cmg);
2091e1dd0a2fSth 	}
2092e1dd0a2fSth 
2093e1dd0a2fSth 	/* for each server that is up now */
2094e1dd0a2fSth 	for (i = 0; i < cnt; i++) {
2095e1dd0a2fSth 		if (chg->changes[i] != NS_SERVER_UP)
2096e1dd0a2fSth 			continue;
2097e1dd0a2fSth 		s = chg->servers[i];
2098e1dd0a2fSth 
2099e1dd0a2fSth 		/*
2100e1dd0a2fSth 		 * look for a CONNECTED MT connection which uses
2101e1dd0a2fSth 		 * a server less preferred than s, and treat it
2102e1dd0a2fSth 		 * as 'fallback needed' by calling
2103e1dd0a2fSth 		 * close_conn_mt_when_nouser()
2104e1dd0a2fSth 		 */
2105e1dd0a2fSth 		k = -1;
2106e1dd0a2fSth 		loop = B_TRUE;
2107e1dd0a2fSth 		while (loop) {
2108e1dd0a2fSth 			if (cmg_locked == B_FALSE) {
2109e1dd0a2fSth 				(void) mutex_lock(&cmg->lock);
2110e1dd0a2fSth 				cmg_locked = B_TRUE;
2111e1dd0a2fSth 			}
2112e1dd0a2fSth 
2113e1dd0a2fSth 			/* Is s a preferred server ? */
2114e1dd0a2fSth 			if (k == -1) {
2115e1dd0a2fSth 				for (j = 0; cmg->pservers[j] != NULL; j++) {
2116e1dd0a2fSth 					if (strcasecmp(cmg->pservers[j],
2117e1dd0a2fSth 					    s) == 0) {
2118e1dd0a2fSth 						k = j;
2119e1dd0a2fSth 						break;
2120e1dd0a2fSth 					}
2121e1dd0a2fSth 				}
2122e1dd0a2fSth 			}
2123e1dd0a2fSth 			/* skip s if not a preferred server */
2124e1dd0a2fSth 			if (k == -1) {
2125e1dd0a2fSth 				break;
2126e1dd0a2fSth 			}
2127e1dd0a2fSth 
2128e1dd0a2fSth 			/* check each MT connection */
2129e1dd0a2fSth 			for (cm = cmg->cm_head; cm; cm = cm->next) {
2130e1dd0a2fSth 				(void) mutex_lock(&cm->lock);
2131e1dd0a2fSth 				/*
2132e1dd0a2fSth 				 * Find an MT connection that is connected and
2133e1dd0a2fSth 				 * not marked, but leave WRITE or REFERRAL
2134e1dd0a2fSth 				 * connections alone, since fallback does not
2135e1dd0a2fSth 				 * make sense for them.
2136e1dd0a2fSth 				 */
2137e1dd0a2fSth 				if (cm->state == NS_CONN_MT_CONNECTED &&
2138e1dd0a2fSth 				    cm->close_when_nouser == B_FALSE &&
2139e1dd0a2fSth 				    cm->conn != NULL && cm->opened_for !=
2140e1dd0a2fSth 				    NS_CONN_USER_WRITE &&
2141e1dd0a2fSth 				    cm->referral == B_FALSE) {
2142e1dd0a2fSth 					n = -1;
2143e1dd0a2fSth 					/*
2144e1dd0a2fSth 					 * j < k ??? should we close
2145e1dd0a2fSth 					 * an active MT that is using s ?
2146e1dd0a2fSth 					 * ie could s went down and up
2147e1dd0a2fSth 					 * again, but cm is bound prior to
2148e1dd0a2fSth 					 * the down ? Play safe here,
2149e1dd0a2fSth 					 * and check j <= k.
2150e1dd0a2fSth 					 */
2151e1dd0a2fSth 					for (j = 0; j <= k; j++) {
2152e1dd0a2fSth 						if (strcasecmp(
2153e1dd0a2fSth 						    cm->conn->serverAddr,
2154e1dd0a2fSth 						    cmg->pservers[j]) == 0) {
2155e1dd0a2fSth 							n = j;
2156e1dd0a2fSth 							break;
2157e1dd0a2fSth 						}
2158e1dd0a2fSth 					}
2159e1dd0a2fSth 					/*
2160e1dd0a2fSth 					 * s is preferred, if its location
2161e1dd0a2fSth 					 * in the preferred server list is
2162e1dd0a2fSth 					 * ahead of that of the server
2163e1dd0a2fSth 					 * used by the cm (i.e., no match
2164e1dd0a2fSth 					 * found before s)
2165e1dd0a2fSth 					 */
2166e1dd0a2fSth 					if (n == -1) { /* s is preferred */
2167e1dd0a2fSth 						int fr = 0;
2168e1dd0a2fSth 						fr = close_conn_mt_when_nouser(
2169e1dd0a2fSth 						    cm);
2170e1dd0a2fSth 						NS_CONN_UNLOCK_AND_FREE(fr,
2171e1dd0a2fSth 						    cm, cmg);
2172e1dd0a2fSth 						cmg_locked = B_FALSE;
2173e1dd0a2fSth 						/*
2174e1dd0a2fSth 						 * break, not continue,
2175e1dd0a2fSth 						 * because we need to
2176e1dd0a2fSth 						 * check the entire cm
2177e1dd0a2fSth 						 * list again. The call
2178e1dd0a2fSth 						 * above may change the
2179e1dd0a2fSth 						 * cm list.
2180e1dd0a2fSth 						 */
2181e1dd0a2fSth 						break;
2182e1dd0a2fSth 					}
2183e1dd0a2fSth 				}
2184e1dd0a2fSth 				(void) mutex_unlock(&cm->lock);
2185e1dd0a2fSth 			}
2186e1dd0a2fSth 			/* if no (more) cm using s, check next server */
2187e1dd0a2fSth 			if (cm == NULL)
2188e1dd0a2fSth 				loop = B_FALSE;
2189e1dd0a2fSth 		} /* while loop */
2190e1dd0a2fSth 	}
2191e1dd0a2fSth 	if (cmg_locked == B_TRUE)
2192e1dd0a2fSth 		(void) mutex_unlock(&cmg->lock);
2193e1dd0a2fSth 	return (cmg);
2194e1dd0a2fSth }
2195e1dd0a2fSth 
2196e1dd0a2fSth /* Shut down all MT connection managed by the connection management */
2197e1dd0a2fSth void
shutdown_all_conn_mt(ns_conn_mgmt_t * cmg)2198e1dd0a2fSth shutdown_all_conn_mt(ns_conn_mgmt_t  *cmg)
2199e1dd0a2fSth {
2200e1dd0a2fSth 	ns_ldap_error_t	*ep;
2201e1dd0a2fSth 	ns_conn_mt_t	*cm;
2202e1dd0a2fSth 	int		free_cm = 0;
2203e1dd0a2fSth 	boolean_t	done = B_FALSE;
2204e1dd0a2fSth 
2205e1dd0a2fSth 	ep = (ns_ldap_error_t *)calloc(1, sizeof (*ep));
2206e1dd0a2fSth 	if (ep != NULL) { /* if NULL, not a problem */
2207e1dd0a2fSth 		/* OK if returns NULL */
2208e1dd0a2fSth 		ep->message = strdup(NS_CONN_MSG_SHUTDOWN_RELOADED);
2209e1dd0a2fSth 	}
2210e1dd0a2fSth 
2211e1dd0a2fSth 	(void) mutex_lock(&cmg->lock);
2212e1dd0a2fSth 	while (cmg->cm_head != NULL && done == B_FALSE) {
2213e1dd0a2fSth 		for (cm = cmg->cm_head; cm; cm = cm->next) {
2214e1dd0a2fSth 			(void) mutex_lock(&cm->lock);
2215e1dd0a2fSth 			if (cm->next == NULL)
2216e1dd0a2fSth 				done = B_TRUE;
2217e1dd0a2fSth 			/* shut down each conn_mt, ignore errors */
2218e1dd0a2fSth 			free_cm = close_conn_mt(cm, LDAP_OTHER, &ep, NULL);
2219e1dd0a2fSth 			(void) mutex_unlock(&cm->lock);
2220e1dd0a2fSth 			if (free_cm == 1) {
2221e1dd0a2fSth 				(void) free_conn_mt(cm, 0);
2222e1dd0a2fSth 				/*
2223e1dd0a2fSth 				 * conn_mt may change, so start from
2224e1dd0a2fSth 				 * top of list again
2225e1dd0a2fSth 				 */
2226e1dd0a2fSth 				break;
2227e1dd0a2fSth 			}
2228e1dd0a2fSth 		}
2229e1dd0a2fSth 	}
2230e1dd0a2fSth 	(void) mutex_unlock(&cmg->lock);
2231e1dd0a2fSth 	(void) __ns_ldap_freeError(&ep);
2232e1dd0a2fSth }
2233e1dd0a2fSth 
2234e1dd0a2fSth /* free all the resources used by the connection management */
2235e1dd0a2fSth void
__s_api_shutdown_conn_mgmt()2236e1dd0a2fSth __s_api_shutdown_conn_mgmt()
2237e1dd0a2fSth {
2238e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
2239e1dd0a2fSth 
2240e1dd0a2fSth 	cmg = access_conn_mgmt(NS_CONN_MGMT_OP_SHUTDOWN);
2241e1dd0a2fSth 	if (cmg == NULL) /* already being SHUT done */
2242e1dd0a2fSth 		return;
2243e1dd0a2fSth 
2244e1dd0a2fSth 	(void) shutdown_all_conn_mt(cmg);
2245e1dd0a2fSth 	(void) release_conn_mgmt(cmg, B_FALSE);
2246e1dd0a2fSth 
2247e1dd0a2fSth 	/* then destroy the conn_mgmt */
2248e1dd0a2fSth 	(void) release_conn_mgmt(cmg, B_FALSE);
2249e1dd0a2fSth }
2250e1dd0a2fSth 
2251e1dd0a2fSth 
2252e1dd0a2fSth /*
2253ca190d8dSmichen  * Reinitialize the libsldap connection management after
2254ca190d8dSmichen  * a new native LDAP configuration is received.
2255e1dd0a2fSth  */
2256e1dd0a2fSth void
__s_api_reinit_conn_mgmt_new_config(ns_config_t * new_cfg)2257e1dd0a2fSth __s_api_reinit_conn_mgmt_new_config(ns_config_t *new_cfg)
2258e1dd0a2fSth {
2259e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
2260e1dd0a2fSth 	ns_conn_mgmt_t	*ocmg;
2261e1dd0a2fSth 
2262e1dd0a2fSth 	cmg = access_conn_mgmt(NS_CONN_MGMT_OP_REF);
2263e1dd0a2fSth 	if (cmg == NULL)
2264e1dd0a2fSth 		return;
2265e1dd0a2fSth 	if (cmg->config == new_cfg || cmg->state == NS_CONN_MGMT_DETACHED) {
2266e1dd0a2fSth 		(void) release_conn_mgmt(cmg, B_FALSE);
2267e1dd0a2fSth 		return;
2268e1dd0a2fSth 	}
2269e1dd0a2fSth 
2270e1dd0a2fSth 	/* reload the conn_mgmt and native LDAP config */
2271e1dd0a2fSth 	ocmg = access_conn_mgmt(NS_CONN_MGMT_OP_NEW_CONFIG);
2272e1dd0a2fSth 	if (ocmg == cmg)
2273e1dd0a2fSth 		shutdown_all_conn_mt(ocmg);
2274e1dd0a2fSth 	/* release the one obtained from access_conn_mgmt(RELOAD) */
2275e1dd0a2fSth 	(void) release_conn_mgmt(ocmg, B_FALSE);
2276e1dd0a2fSth 	/* release the one obtained when ocmg was created */
2277e1dd0a2fSth 	(void) release_conn_mgmt(ocmg, B_FALSE);
2278e1dd0a2fSth 	/* release the one obtained when this function is entered */
2279e1dd0a2fSth 	(void) release_conn_mgmt(cmg, B_FALSE);
2280e1dd0a2fSth }
2281e1dd0a2fSth 
2282e1dd0a2fSth /*
2283e1dd0a2fSth  * Prepare to retry ldap search operation if needed.
2284e1dd0a2fSth  * Return 1 if retry is needed, otherwise 0.
2285e1dd0a2fSth  * If first time in, return 1. If not, return 1 if:
2286e1dd0a2fSth  * - not a NS_CONN_USER_GETENT conn_user AND
2287e1dd0a2fSth  * - have not retried 3 times yet AND
2288e1dd0a2fSth  * - previous search failed AND
2289e1dd0a2fSth  * - the retry flag is set in the ns_conn_user_t or config was reloaded
2290e1dd0a2fSth  */
2291e1dd0a2fSth int
__s_api_setup_retry_search(ns_conn_user_t ** conn_user,ns_conn_user_type_t type,int * try_cnt,int * rc,ns_ldap_error_t ** errorp)2292e1dd0a2fSth __s_api_setup_retry_search(ns_conn_user_t **conn_user,
2293*cc98049dSToomas Soome     ns_conn_user_type_t type, int *try_cnt, int *rc,
2294*cc98049dSToomas Soome     ns_ldap_error_t **errorp)
2295e1dd0a2fSth {
2296e1dd0a2fSth 	boolean_t	retry;
2297e1dd0a2fSth 	ns_conn_user_t	*cu = *conn_user;
2298e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
2299e1dd0a2fSth 
2300e1dd0a2fSth 	if (*try_cnt > 0 && cu != NULL) {
2301e1dd0a2fSth 		/*
2302e1dd0a2fSth 		 * if called from firstEntry(), keep conn_mt for
2303e1dd0a2fSth 		 * the subsequent getnext requests
2304e1dd0a2fSth 		 */
2305e1dd0a2fSth 		if (cu->type == NS_CONN_USER_GETENT && *rc == NS_LDAP_SUCCESS)
2306e1dd0a2fSth 			return (0);
2307e1dd0a2fSth 		cmg = cu->conn_mgmt;
2308e1dd0a2fSth 		retry = cu->retry;
2309e1dd0a2fSth 		if (cu->conn_mt != NULL)
2310e1dd0a2fSth 			__s_api_conn_mt_return(cu);
2311e1dd0a2fSth 		if (cmg != NULL && cmg->cfg_reloaded == B_TRUE)
2312e1dd0a2fSth 			retry = B_TRUE;
2313e1dd0a2fSth 		__s_api_conn_user_free(cu);
2314e1dd0a2fSth 		*conn_user = NULL;
2315e1dd0a2fSth 
2316e1dd0a2fSth 		if (*rc == NS_LDAP_SUCCESS || retry != B_TRUE)
2317e1dd0a2fSth 			return (0);
2318e1dd0a2fSth 	}
2319e1dd0a2fSth 
2320e1dd0a2fSth 	*try_cnt = *try_cnt + 1;
2321e1dd0a2fSth 	if (*try_cnt > NS_LIST_TRY_MAX)
2322e1dd0a2fSth 		return (0);
2323e1dd0a2fSth 
2324e1dd0a2fSth 	*conn_user = __s_api_conn_user_init(type, NULL, B_FALSE);
2325e1dd0a2fSth 	if (*conn_user == NULL) {
2326e1dd0a2fSth 		if (*try_cnt == 1) { /* first call before any retry */
2327e1dd0a2fSth 			*rc = NS_LDAP_MEMORY;
2328e1dd0a2fSth 			*errorp = NULL;
2329e1dd0a2fSth 		}
2330e1dd0a2fSth 		/* for 1+ try, use previous rc and errorp */
2331e1dd0a2fSth 		return (0);
2332e1dd0a2fSth 	}
2333e1dd0a2fSth 
2334e1dd0a2fSth 	/* free ldap_error_t from previous search */
2335e1dd0a2fSth 	if (*try_cnt > 1 && rc != NS_LDAP_SUCCESS && *errorp != NULL)
2336e1dd0a2fSth 		(void) __ns_ldap_freeError(errorp);
2337e1dd0a2fSth 
2338e1dd0a2fSth 	return (1);
2339e1dd0a2fSth }
2340e1dd0a2fSth 
2341e1dd0a2fSth /* prepare to get the next entry for an enumeration */
2342e1dd0a2fSth int
__s_api_setup_getnext(ns_conn_user_t * cu,int * ns_err,ns_ldap_error_t ** errorp)2343e1dd0a2fSth __s_api_setup_getnext(ns_conn_user_t *cu, int *ns_err,
2344*cc98049dSToomas Soome     ns_ldap_error_t **errorp)
2345e1dd0a2fSth {
2346e1dd0a2fSth 	int rc;
2347e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
2348e1dd0a2fSth 
2349e1dd0a2fSth 	/*
2350e1dd0a2fSth 	 * if using an MT connection, ensure the thread-specific data are set,
2351e1dd0a2fSth 	 * but if the MT connection is no longer good, return the error saved.
2352e1dd0a2fSth 	 */
2353e1dd0a2fSth 	if (cu->conn_mt != NULL && (cmg = cu->conn_mgmt) != NULL) {
2354e1dd0a2fSth 
2355e1dd0a2fSth 		if (cu->bad_mt_conn ==  B_TRUE) {
2356e1dd0a2fSth 			__s_api_conn_mt_close(cu, 0, NULL);
2357e1dd0a2fSth 			*ns_err = cu->ns_rc;
2358e1dd0a2fSth 			*errorp = cu->ns_error;
2359e1dd0a2fSth 			cu->ns_error = NULL;
2360e1dd0a2fSth 			return (*ns_err);
2361e1dd0a2fSth 		}
2362e1dd0a2fSth 
2363e1dd0a2fSth 		rc = conn_tsd_check(cmg);
2364e1dd0a2fSth 		if (rc != NS_LDAP_SUCCESS) {
2365e1dd0a2fSth 			*errorp = NULL;
2366e1dd0a2fSth 			return (rc);
2367e1dd0a2fSth 		}
2368e1dd0a2fSth 	}
2369e1dd0a2fSth 
2370e1dd0a2fSth 	return (NS_LDAP_SUCCESS);
2371e1dd0a2fSth }
2372e1dd0a2fSth 
2373e1dd0a2fSth /* wait for an MT connection to become available */
2374e1dd0a2fSth static int
conn_wait(ns_conn_mt_t * conn_mt,ns_conn_user_t * conn_user)2375e1dd0a2fSth conn_wait(ns_conn_mt_t *conn_mt, ns_conn_user_t *conn_user)
2376e1dd0a2fSth {
2377e1dd0a2fSth 	ns_conn_waiter_t	mywait;
2378e1dd0a2fSth 	ns_conn_waiter_t	*head = &conn_mt->waiter;
2379e1dd0a2fSth 
2380e1dd0a2fSth 	(void) cond_init(&(mywait.waitcv), USYNC_THREAD, 0);
2381e1dd0a2fSth 	mywait.key = conn_user;
2382e1dd0a2fSth 	mywait.signaled = 0;
2383e1dd0a2fSth 	mywait.next = head->next;
2384e1dd0a2fSth 	mywait.prev = head;
2385e1dd0a2fSth 	if (mywait.next)
2386e1dd0a2fSth 		mywait.next->prev = &mywait;
2387e1dd0a2fSth 	head->next = &mywait;
2388e1dd0a2fSth 	atomic_inc_uint(&conn_mt->waiter_cnt);
2389e1dd0a2fSth 
2390e1dd0a2fSth 	while (!mywait.signaled)
2391e1dd0a2fSth 		(void) cond_wait(&(mywait.waitcv), &conn_mt->lock);
2392e1dd0a2fSth 	if (mywait.prev)
2393e1dd0a2fSth 		mywait.prev->next = mywait.next;
2394e1dd0a2fSth 	if (mywait.next)
2395e1dd0a2fSth 		mywait.next->prev = mywait.prev;
2396e1dd0a2fSth 	return (0);
2397e1dd0a2fSth }
2398e1dd0a2fSth 
2399e1dd0a2fSth /* signal that an MT connection is now available */
2400e1dd0a2fSth static int
conn_signal(ns_conn_mt_t * conn_mt)2401e1dd0a2fSth conn_signal(ns_conn_mt_t *conn_mt)
2402e1dd0a2fSth {
2403e1dd0a2fSth 	int			c = 0;
2404e1dd0a2fSth 	ns_conn_waiter_t	*head = &conn_mt->waiter;
2405e1dd0a2fSth 	ns_conn_waiter_t	*tmp = head->next;
2406e1dd0a2fSth 
2407e1dd0a2fSth 	while (tmp) {
2408e1dd0a2fSth 		(void) cond_signal(&(tmp->waitcv));
2409e1dd0a2fSth 		tmp->signaled = 1;
2410e1dd0a2fSth 		atomic_dec_uint(&conn_mt->waiter_cnt);
2411e1dd0a2fSth 		c++;
2412e1dd0a2fSth 		tmp = tmp->next;
2413e1dd0a2fSth 	}
2414e1dd0a2fSth 
2415e1dd0a2fSth 	return (c);
2416e1dd0a2fSth }
2417e1dd0a2fSth 
2418e1dd0a2fSth /*
2419e1dd0a2fSth  * wait and process the server status and/or config change notification
2420e1dd0a2fSth  * from ldap_cachemgr
2421e1dd0a2fSth  */
2422e1dd0a2fSth static void *
get_server_change(void * arg)2423e1dd0a2fSth get_server_change(void *arg)
2424e1dd0a2fSth {
2425e1dd0a2fSth 	union {
2426e1dd0a2fSth 		ldap_data_t	s_d;
2427e1dd0a2fSth 		char		s_b[DOORBUFFERSIZE];
2428e1dd0a2fSth 	} space;
2429e1dd0a2fSth 	ldap_data_t	*sptr = &space.s_d;
2430e1dd0a2fSth 	int		ndata;
2431e1dd0a2fSth 	int		adata;
2432e1dd0a2fSth 	char		*ptr;
2433e1dd0a2fSth 	int		ds_cnt;
2434e1dd0a2fSth 	int		door_rc;
2435e1dd0a2fSth 	int		which;
2436e1dd0a2fSth 	int		retry = 0;
2437e1dd0a2fSth 	boolean_t	loop = B_TRUE;
2438e1dd0a2fSth 	char		*c, *oc;
2439e1dd0a2fSth 	int		dslen = strlen(DOORLINESEP);
2440e1dd0a2fSth 	char		dsep = DOORLINESEP_CHR;
2441e1dd0a2fSth 	char		chg_data[DOORBUFFERSIZE];
2442e1dd0a2fSth 	char		**servers = NULL;
2443e1dd0a2fSth 	boolean_t	getchg_not_supported = B_FALSE;
2444e1dd0a2fSth 	ns_conn_mgmt_t	*ocmg = (ns_conn_mgmt_t *)arg;
2445e1dd0a2fSth 	ns_conn_mgmt_t	*cmg;
2446e1dd0a2fSth 	ns_server_status_t *status = NULL;
2447e1dd0a2fSth 	ns_server_status_change_t chg = { 0 };
2448e1dd0a2fSth 	ldap_get_change_out_t *get_chg;
2449e1dd0a2fSth 	ldap_get_chg_cookie_t cookie;
2450e1dd0a2fSth 	ldap_get_chg_cookie_t new_cookie;
2451e1dd0a2fSth 
2452e1dd0a2fSth 	cmg = access_conn_mgmt(NS_CONN_MGMT_OP_REF);
2453e1dd0a2fSth 	if (cmg != ocmg)
2454e1dd0a2fSth 		thr_exit(NULL);
2455e1dd0a2fSth 	/* cmg is locked before called */
2456e1dd0a2fSth 	cmg->procchg_tid = thr_self();
2457e1dd0a2fSth 
2458e1dd0a2fSth 	/* make sure the thread specific data are set */
2459e1dd0a2fSth 	(void) conn_tsd_setup(cmg);
2460e1dd0a2fSth 	cookie = cmg->cfg_cookie;
2461e1dd0a2fSth 
2462e1dd0a2fSth 	while (loop) {
2463e1dd0a2fSth 
2464e1dd0a2fSth 		if (chg.servers != NULL)
2465e1dd0a2fSth 			free(chg.servers);
2466e1dd0a2fSth 		if (chg.changes != NULL)
2467e1dd0a2fSth 			free(chg.changes);
2468e1dd0a2fSth 		if (sptr != &space.s_d)
2469e1dd0a2fSth 			(void) munmap((char *)sptr, sizeof (space));
2470e1dd0a2fSth 
2471e1dd0a2fSth 		/*
2472e1dd0a2fSth 		 * If the attached conn_mgmt has been deleted,
2473e1dd0a2fSth 		 * then exit. The new conn_mgmt will starts it
2474e1dd0a2fSth 		 * own monitor thread later. If libsldap is being
2475e1dd0a2fSth 		 * unloaded or configuration reloaded, OR
2476e1dd0a2fSth 		 * ldap_cachemgr rejected the GETSTATUSCHANGE door
2477e1dd0a2fSth 		 * call, then exit as well.
2478e1dd0a2fSth 		 */
2479e1dd0a2fSth 		if (cmg == NULL || cmg->state == NS_CONN_MGMT_DETACHED ||
2480e1dd0a2fSth 		    getchg_not_supported == B_TRUE) {
2481e1dd0a2fSth 
2482e1dd0a2fSth 			if (cmg != NULL) {
2483e1dd0a2fSth 				cmg->procchg_started = B_FALSE;
2484e1dd0a2fSth 				(void) release_conn_mgmt(cmg, B_FALSE);
2485e1dd0a2fSth 			}
2486e1dd0a2fSth 
2487e1dd0a2fSth 			conn_tsd_free();
2488e1dd0a2fSth 			thr_exit(NULL);
2489e1dd0a2fSth 		}
2490e1dd0a2fSth 
2491e1dd0a2fSth 		(void) memset(space.s_b, 0, DOORBUFFERSIZE);
2492e1dd0a2fSth 		(void) memset(&chg, 0, sizeof (chg));
2493e1dd0a2fSth 		adata = sizeof (ldap_call_t) + 1;
2494e1dd0a2fSth 		ndata = sizeof (space);
2495e1dd0a2fSth 		space.s_d.ldap_call.ldap_callnumber = GETSTATUSCHANGE;
2496e1dd0a2fSth 		space.s_d.ldap_call.ldap_u.get_change.op =
2497e1dd0a2fSth 		    NS_STATUS_CHANGE_OP_START;
2498e1dd0a2fSth 		space.s_d.ldap_call.ldap_u.get_change.cookie = cookie;
2499e1dd0a2fSth 		sptr = &space.s_d;
2500e1dd0a2fSth 		door_rc = __ns_ldap_trydoorcall_getfd();
2501e1dd0a2fSth 		cmg->procchg_door_call = B_TRUE;
2502e1dd0a2fSth 		if (release_conn_mgmt(cmg, B_FALSE) == NULL) {
2503e1dd0a2fSth 			conn_tsd_free();
2504e1dd0a2fSth 			thr_exit(NULL);
2505e1dd0a2fSth 		}
2506e1dd0a2fSth 
2507e1dd0a2fSth 		if (door_rc == NS_CACHE_SUCCESS)
2508e1dd0a2fSth 			door_rc = __ns_ldap_trydoorcall_send(&sptr, &ndata,
2509e1dd0a2fSth 			    &adata);
2510e1dd0a2fSth 
2511e1dd0a2fSth 		/*
2512e1dd0a2fSth 		 * Check and see if the conn_mgmt is still current.
2513e1dd0a2fSth 		 * If not, no need to continue.
2514e1dd0a2fSth 		 */
2515e1dd0a2fSth 		cmg = access_conn_mgmt(NS_CONN_MGMT_OP_REF);
2516e1dd0a2fSth 		if (cmg != NULL)
2517e1dd0a2fSth 			cmg->procchg_door_call = B_FALSE;
2518e1dd0a2fSth 		if (cmg != ocmg) {
2519e1dd0a2fSth 			if (cmg != NULL) {
2520e1dd0a2fSth 				cmg->procchg_started = B_FALSE;
2521e1dd0a2fSth 				(void) release_conn_mgmt(cmg, B_FALSE);
2522e1dd0a2fSth 			}
2523e1dd0a2fSth 			conn_tsd_free();
2524e1dd0a2fSth 			thr_exit(NULL);
2525e1dd0a2fSth 		}
2526e1dd0a2fSth 
2527e1dd0a2fSth 		if (door_rc != NS_CACHE_SUCCESS) {
2528e1dd0a2fSth 			if (door_rc == NS_CACHE_NOSERVER) {
2529e1dd0a2fSth 				if (retry++ > 10)
2530e1dd0a2fSth 					getchg_not_supported = B_TRUE;
2531e1dd0a2fSth 				else {
2532e1dd0a2fSth 					/*
2533e1dd0a2fSth 					 * ldap_cachemgr may be down, give
2534e1dd0a2fSth 					 * it time to restart
2535e1dd0a2fSth 					 */
2536e1dd0a2fSth 					(void) sleep(2);
2537e1dd0a2fSth 				}
2538e1dd0a2fSth 			} else if (door_rc == NS_CACHE_NOTFOUND)
2539e1dd0a2fSth 				getchg_not_supported = B_TRUE;
2540e1dd0a2fSth 			continue;
2541e1dd0a2fSth 		} else
2542e1dd0a2fSth 			retry = 0;
2543e1dd0a2fSth 
2544e1dd0a2fSth 		/* copy info from door call return structure */
2545e1dd0a2fSth 		get_chg =  &sptr->ldap_ret.ldap_u.changes;
2546e1dd0a2fSth 		ptr = get_chg->data;
2547e1dd0a2fSth 		/* configuration change ? */
2548e1dd0a2fSth 		if (get_chg->type == NS_STATUS_CHANGE_TYPE_CONFIG) {
2549e1dd0a2fSth 			chg.config_changed = B_TRUE;
2550e1dd0a2fSth 			cmg = proc_server_change(&chg, cmg);
2551e1dd0a2fSth 			continue;
2552e1dd0a2fSth 		}
2553e1dd0a2fSth 
2554e1dd0a2fSth 		/* server status changes ? */
2555e1dd0a2fSth 		if (get_chg->type == NS_STATUS_CHANGE_TYPE_SERVER) {
2556e1dd0a2fSth 			/*
2557e1dd0a2fSth 			 * first check cookies, if don't match, config
2558e1dd0a2fSth 			 * has changed
2559e1dd0a2fSth 			 */
2560e1dd0a2fSth 			new_cookie = get_chg->cookie;
2561e1dd0a2fSth 			if (new_cookie.mgr_pid != cookie.mgr_pid ||
2562e1dd0a2fSth 			    new_cookie.seq_num != cookie.seq_num) {
2563e1dd0a2fSth 				chg.config_changed = B_TRUE;
2564e1dd0a2fSth 				cmg = proc_server_change(&chg, cmg);
2565e1dd0a2fSth 				continue;
2566e1dd0a2fSth 			}
2567e1dd0a2fSth 
2568e1dd0a2fSth 			(void) strlcpy(chg_data, ptr, sizeof (chg_data));
2569e1dd0a2fSth 			chg.num_server = get_chg->server_count;
2570e1dd0a2fSth 
2571e1dd0a2fSth 			servers = (char **)calloc(chg.num_server,
2572e1dd0a2fSth 			    sizeof (char *));
2573e1dd0a2fSth 			if (servers == NULL) {
2574e1dd0a2fSth 				syslog(LOG_INFO, NS_CONN_MSG_MEMORY_ERROR);
2575e1dd0a2fSth 				continue;
2576e1dd0a2fSth 			}
2577e1dd0a2fSth 			status = (ns_server_status_t *)calloc(chg.num_server,
2578e1dd0a2fSth 			    sizeof (int));
2579e1dd0a2fSth 			if (status == NULL) {
2580e1dd0a2fSth 				syslog(LOG_INFO, NS_CONN_MSG_MEMORY_ERROR);
2581e1dd0a2fSth 				free(servers);
2582e1dd0a2fSth 				continue;
2583e1dd0a2fSth 			}
2584e1dd0a2fSth 			ds_cnt = 0;
2585e1dd0a2fSth 			which = 0;
2586e1dd0a2fSth 			oc = ptr;
2587e1dd0a2fSth 			for (c = ptr; which != 2; c++) {
2588e1dd0a2fSth 				/* look for DOORLINESEP or end of string */
2589e1dd0a2fSth 				if (*c != dsep && *c != '\0')
2590e1dd0a2fSth 					continue;
2591e1dd0a2fSth 				if (*c == dsep) { /* DOORLINESEP */
2592e1dd0a2fSth 					*c = '\0'; /* current value */
2593e1dd0a2fSth 					c += dslen; /* skip to next value */
2594e1dd0a2fSth 				}
2595e1dd0a2fSth 				if (which == 0) { /* get server info */
2596e1dd0a2fSth 					servers[ds_cnt] = oc;
2597e1dd0a2fSth 					oc = c;
2598e1dd0a2fSth 					which = 1; /* get status next */
2599e1dd0a2fSth 					continue;
2600e1dd0a2fSth 				}
2601e1dd0a2fSth 				/* which == 1, get up/down status */
2602e1dd0a2fSth 				if (strcmp(NS_SERVER_CHANGE_UP, oc) == 0) {
2603e1dd0a2fSth 					status[ds_cnt] = NS_SERVER_UP;
2604e1dd0a2fSth 				} else if (strcmp(NS_SERVER_CHANGE_DOWN,
2605e1dd0a2fSth 				    oc) == 0)
2606e1dd0a2fSth 					status[ds_cnt] = NS_SERVER_DOWN;
2607e1dd0a2fSth 				else {
2608e1dd0a2fSth 					syslog(LOG_INFO,
2609e1dd0a2fSth 					    NS_CONN_MSG_BAD_CACHEMGR_DATA);
2610e1dd0a2fSth 					continue;
2611e1dd0a2fSth 				}
2612e1dd0a2fSth 				oc = c;
2613e1dd0a2fSth 				ds_cnt++;
2614e1dd0a2fSth 				if (*c == '\0')
2615e1dd0a2fSth 					which = 2; /* exit the loop */
2616e1dd0a2fSth 				else
2617e1dd0a2fSth 					which = 0; /* get server info next */
2618e1dd0a2fSth 			}
2619e1dd0a2fSth 			chg.servers = servers;
2620e1dd0a2fSth 			chg.changes = status;
2621e1dd0a2fSth 			cmg = proc_server_change(&chg, cmg);
2622e1dd0a2fSth 			continue;
2623e1dd0a2fSth 		}
2624e1dd0a2fSth 	}
2625e1dd0a2fSth 
2626e1dd0a2fSth 	return (NULL);
2627e1dd0a2fSth }
2628e1dd0a2fSth 
2629e1dd0a2fSth /* start the thread handling the change notification from ldap_cachemgr */
2630e1dd0a2fSth static void
start_thread(ns_conn_mgmt_t * cmg)2631*cc98049dSToomas Soome start_thread(ns_conn_mgmt_t *cmg)
2632*cc98049dSToomas Soome {
2633e1dd0a2fSth 
2634e1dd0a2fSth 	int		errnum;
2635e1dd0a2fSth 
2636e1dd0a2fSth 	/*
2637e1dd0a2fSth 	 * start a thread to get and process config and server status changes
2638e1dd0a2fSth 	 */
263951b02b29SToomas Soome 	if (thr_create(NULL, 0, get_server_change,
2640e1dd0a2fSth 	    (void *)cmg, THR_DETACHED, NULL) != 0) {
2641e1dd0a2fSth 		errnum = errno;
2642e1dd0a2fSth 		syslog(LOG_WARNING, NS_CONN_MSG_NO_PROCCHG_THREAD,
2643e1dd0a2fSth 		    strerror(errnum));
2644e1dd0a2fSth 	}
2645e1dd0a2fSth }
2646