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 /*
22434c5a06SMilan Jurik  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23e1dd0a2fSth  * Use is subject to license terms.
2433f5ff17SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
25b3b48d8eSHans Rosenfeld  * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
26e1dd0a2fSth  */
27e1dd0a2fSth 
28e1dd0a2fSth #define	__STANDALONE_MODULE__
29e1dd0a2fSth 
30e1dd0a2fSth #include <stdio.h>
31e1dd0a2fSth #include <sys/types.h>
32e1dd0a2fSth #include <stdlib.h>
33e1dd0a2fSth #include <libintl.h>
34e1dd0a2fSth #include <string.h>
35e1dd0a2fSth #include <ctype.h>
36e1dd0a2fSth 
37e1dd0a2fSth #include <sys/stat.h>
38e1dd0a2fSth #include <fcntl.h>
39e1dd0a2fSth #include <unistd.h>
40e1dd0a2fSth #include <syslog.h>
41e1dd0a2fSth #include <locale.h>
42e1dd0a2fSth #include <errno.h>
43e1dd0a2fSth #include <sys/time.h>
44e1dd0a2fSth 
45e1dd0a2fSth #include <arpa/inet.h>
46e1dd0a2fSth #include <netdb.h>
47e1dd0a2fSth #include <strings.h>
48e1dd0a2fSth 
49e1dd0a2fSth #include <thread.h>
50e1dd0a2fSth 
51e1dd0a2fSth #include <nsswitch.h>
52e1dd0a2fSth #include <nss_dbdefs.h>
53e1dd0a2fSth #include <nss.h>
54e1dd0a2fSth 
55e1dd0a2fSth #include "ns_cache_door.h"
56e1dd0a2fSth #include "ns_internal.h"
57ca190d8dSmichen #include "ns_connmgmt.h"
58e1dd0a2fSth 
59e1dd0a2fSth typedef enum {
60e1dd0a2fSth 	INFO_SERVER_JUST_INITED	= -1,
61e1dd0a2fSth 	INFO_SERVER_UNKNOWN	= 0,
62e1dd0a2fSth 	INFO_SERVER_CONNECTING	= 1,
63e1dd0a2fSth 	INFO_SERVER_UP		= 2,
64e1dd0a2fSth 	INFO_SERVER_ERROR 	= 3,
65e1dd0a2fSth 	INFO_SERVER_REMOVED	= 4
66e1dd0a2fSth } dir_server_status_t;
67e1dd0a2fSth 
68e1dd0a2fSth typedef enum {
69e1dd0a2fSth 	INFO_STATUS_NEW   	= 2,
70e1dd0a2fSth 	INFO_STATUS_OLD		= 3
71e1dd0a2fSth } dir_server_info_t;
72e1dd0a2fSth 
73e1dd0a2fSth typedef struct dir_server {
74e1dd0a2fSth 	char			*ip;
75e1dd0a2fSth 	char			**controls;
76e1dd0a2fSth 	char			**saslMech;
77e1dd0a2fSth 	dir_server_status_t	status;
78e1dd0a2fSth 	mutex_t			updateStatus;
79e1dd0a2fSth 	dir_server_info_t	info;
80e1dd0a2fSth } dir_server_t;
81e1dd0a2fSth 
82e1dd0a2fSth typedef struct dir_server_list {
83e1dd0a2fSth 	dir_server_t	**nsServers;
84e1dd0a2fSth 
85e1dd0a2fSth 	rwlock_t	listDestroyLock;
86e1dd0a2fSth } dir_server_list_t;
87e1dd0a2fSth 
88e1dd0a2fSth struct {
89e1dd0a2fSth 	/* The local list of the directory servers' root DSEs. */
90e1dd0a2fSth 	dir_server_list_t	*list;
91e1dd0a2fSth 	/* The flag indicating if libsldap is in the 'Standalone' mode. */
92e1dd0a2fSth 	int			standalone;
93e1dd0a2fSth 	/*
94e1dd0a2fSth 	 * The mutex ensuring that only one thread performs
95e1dd0a2fSth 	 * the initialization of the list.
96e1dd0a2fSth 	 */
97e1dd0a2fSth 	mutex_t			listReplaceLock;
98e1dd0a2fSth 	/*
99e1dd0a2fSth 	 * A flag indicating that a particular thread is
100e1dd0a2fSth 	 * in the 'ldap_cachemgr' mode. It is stored by thread as
101e1dd0a2fSth 	 * a thread specific data.
102e1dd0a2fSth 	 */
103e1dd0a2fSth 	const int		initFlag;
104e1dd0a2fSth 	/*
105e1dd0a2fSth 	 * A thread specific key storing
106e1dd0a2fSth 	 * the the 'ldap_cachemgr' mode indicator.
107e1dd0a2fSth 	 */
108e1dd0a2fSth 	thread_key_t		standaloneInitKey;
109e1dd0a2fSth } dir_servers = {NULL, 0, DEFAULTMUTEX, '1'};
110e1dd0a2fSth 
111e1dd0a2fSth typedef struct switchDatabase {
112e1dd0a2fSth 	char *conf;
113e1dd0a2fSth 	uint32_t alloced;
114e1dd0a2fSth } switch_database_t;
115e1dd0a2fSth 
116e1dd0a2fSth static thread_key_t switchConfigKey;
117e1dd0a2fSth 
118e1dd0a2fSth #pragma init(createStandaloneKey)
119e1dd0a2fSth 
120e1dd0a2fSth #define	DONT_INCLUDE_ATTR_NAMES	0
121e1dd0a2fSth #define	INCLUDE_ATTR_NAMES	1
122e1dd0a2fSth #define	IS_PROFILE		1
123e1dd0a2fSth #define	NOT_PROFILE		0
124e1dd0a2fSth /* INET6_ADDRSTRLEN + ":" + <5-digit port> + some round-up */
125e1dd0a2fSth #define	MAX_HOSTADDR_LEN (INET6_ADDRSTRLEN + 6 + 12)
126e1dd0a2fSth 
127e1dd0a2fSth static
128e1dd0a2fSth void
switch_conf_disposer(void * data)129e1dd0a2fSth switch_conf_disposer(void *data)
130e1dd0a2fSth {
131e1dd0a2fSth 	switch_database_t *localData = (switch_database_t *)data;
132e1dd0a2fSth 
133e1dd0a2fSth 	free(localData->conf);
134e1dd0a2fSth 	free(localData);
135e1dd0a2fSth }
136e1dd0a2fSth 
137e1dd0a2fSth /*
138e1dd0a2fSth  * This function initializes an indication that a thread obtaining a root DSE
139e1dd0a2fSth  * will be switched to the 'ldap_cachemgr' mode. Within the thread libsldap
140e1dd0a2fSth  * will not invoke the __s_api_requestServer function. Instead, the library
141e1dd0a2fSth  * will establish a connection to the server specified by
142e1dd0a2fSth  * the __ns_ldap_getRootDSE function.
143e1dd0a2fSth  * Since  ldap_cachmgr can obtain a DUAProfile and root DSEs at the same time
144e1dd0a2fSth  * and we do not want to affect a thread obtaining a DUAProfile,
145e1dd0a2fSth  * the 'ldap_cachemgr' mode is thread private.
146e1dd0a2fSth  * In addition, this function creates a key holding temporary configuration
147e1dd0a2fSth  * for the "hosts" and "ipnodes" databases which is used by the "SKIPDB"
148e1dd0a2fSth  * mechanism (__s_api_ip2hostname() & _s_api_hostname2ip()).
149e1dd0a2fSth  */
150e1dd0a2fSth static
151e1dd0a2fSth void
createStandaloneKey()152e1dd0a2fSth createStandaloneKey()
153e1dd0a2fSth {
154e1dd0a2fSth 	if (thr_keycreate(&dir_servers.standaloneInitKey, NULL) != 0) {
155e1dd0a2fSth 		syslog(LOG_ERR, gettext("libsldap: unable to create a thread "
156e1dd0a2fSth 		"key needed for sharing ldap connections"));
157e1dd0a2fSth 	}
158e1dd0a2fSth 	if (thr_keycreate(&switchConfigKey, switch_conf_disposer) != 0) {
159e1dd0a2fSth 		syslog(LOG_ERR, gettext("libsldap: unable to create a thread "
160e1dd0a2fSth 		    "key containing current nsswitch configuration"));
161e1dd0a2fSth 	}
162e1dd0a2fSth }
163e1dd0a2fSth 
164e1dd0a2fSth /*
165e1dd0a2fSth  * This function sets the 'ldap_cachemgr' mode indication.
166e1dd0a2fSth  */
167e1dd0a2fSth void
__s_api_setInitMode()168e1dd0a2fSth __s_api_setInitMode()
169e1dd0a2fSth {
170e1dd0a2fSth 	(void) thr_setspecific(dir_servers.standaloneInitKey,
171e1dd0a2fSth 	    (void *) &dir_servers.initFlag);
172e1dd0a2fSth }
173e1dd0a2fSth 
174e1dd0a2fSth /*
175e1dd0a2fSth  * This function unset the 'ldap_cachemgr' mode indication.
176e1dd0a2fSth  */
177e1dd0a2fSth void
__s_api_unsetInitMode()178e1dd0a2fSth __s_api_unsetInitMode()
179e1dd0a2fSth {
180e1dd0a2fSth 	(void) thr_setspecific(dir_servers.standaloneInitKey, NULL);
181e1dd0a2fSth }
182e1dd0a2fSth 
183e1dd0a2fSth /*
184e1dd0a2fSth  * This function checks if the 'ldap_cachemgr' mode indication is set.
185e1dd0a2fSth  */
186e1dd0a2fSth int
__s_api_isInitializing()187e1dd0a2fSth __s_api_isInitializing() {
188e1dd0a2fSth 	int *flag = NULL;
189e1dd0a2fSth 
190e1dd0a2fSth 	(void) thr_getspecific(dir_servers.standaloneInitKey, (void **) &flag);
191e1dd0a2fSth 
192e1dd0a2fSth 	return (flag != NULL && *flag == dir_servers.initFlag);
193e1dd0a2fSth }
194e1dd0a2fSth 
195e1dd0a2fSth /*
196e1dd0a2fSth  * This function checks if the process runs in the 'Standalone' mode.
197e1dd0a2fSth  * In this mode libsldap will check the local, process private list of root DSEs
198e1dd0a2fSth  * instead of requesting them via a door call to ldap_cachemgr.
199e1dd0a2fSth  */
200e1dd0a2fSth int
__s_api_isStandalone()201e1dd0a2fSth __s_api_isStandalone()
202e1dd0a2fSth {
203e1dd0a2fSth 	int	mode;
204e1dd0a2fSth 
205e1dd0a2fSth 	(void) mutex_lock(&dir_servers.listReplaceLock);
206e1dd0a2fSth 	mode = dir_servers.standalone;
207e1dd0a2fSth 	(void) mutex_unlock(&dir_servers.listReplaceLock);
208e1dd0a2fSth 
209e1dd0a2fSth 	return (mode);
210e1dd0a2fSth }
211e1dd0a2fSth 
212e1dd0a2fSth 
213e1dd0a2fSth static
214e1dd0a2fSth int
remove_ldap(char * dst,char * src,int dst_buf_len)215e1dd0a2fSth remove_ldap(char *dst, char *src, int dst_buf_len)
216e1dd0a2fSth {
217e1dd0a2fSth 	int i = 0;
218e1dd0a2fSth 
219e1dd0a2fSth 	if (strlen(src) >= dst_buf_len)
220e1dd0a2fSth 		return (0);
221e1dd0a2fSth 
222e1dd0a2fSth 	while (*src != '\0') {
223e1dd0a2fSth 		/* Copy up to one space from source. */
224e1dd0a2fSth 		if (isspace(*src)) {
225e1dd0a2fSth 			dst[i++] = *src;
226e1dd0a2fSth 			while (isspace(*src))
227e1dd0a2fSth 				src++;
228e1dd0a2fSth 		}
229e1dd0a2fSth 
230e1dd0a2fSth 		/* If not "ldap", just copy. */
231e1dd0a2fSth 		if (strncmp(src, "ldap", 4) != 0) {
232e1dd0a2fSth 			while (!isspace(*src)) {
233e1dd0a2fSth 				dst[i++] = *src++;
234e1dd0a2fSth 				/* At the end of string? */
235e1dd0a2fSth 				if (dst[i-1] == '\0')
236e1dd0a2fSth 					return (1);
237e1dd0a2fSth 			}
238e1dd0a2fSth 			/* Copy up to one space from source. */
239e1dd0a2fSth 			if (isspace(*src)) {
240e1dd0a2fSth 				dst[i++] = *src;
241e1dd0a2fSth 				while (isspace(*src))
242e1dd0a2fSth 					src++;
243e1dd0a2fSth 			}
244e1dd0a2fSth 			/* Copy also the criteria section */
245e1dd0a2fSth 			if (*src == '[')
246e1dd0a2fSth 				while (*src != ']') {
247e1dd0a2fSth 					dst[i++] = *src++;
248e1dd0a2fSth 					/* Shouln't happen if format is right */
249e1dd0a2fSth 					if (dst[i-1] == '\0')
250e1dd0a2fSth 						return (1);
251e1dd0a2fSth 				}
252e1dd0a2fSth 		}
253e1dd0a2fSth 
254e1dd0a2fSth 		/* If next part is ldap, skip over it ... */
255e1dd0a2fSth 		if (strncmp(src, "ldap", 4) == 0) {
256e1dd0a2fSth 			if (isspace(*(src+4)) || *(src+4) == '\0') {
257e1dd0a2fSth 				src += 4;
258e1dd0a2fSth 				while (isspace(*src))
259e1dd0a2fSth 					src++;
260e1dd0a2fSth 				if (*src == '[') {
261e1dd0a2fSth 					while (*src++ != ']') {
262e1dd0a2fSth 						/*
263e1dd0a2fSth 						 * See comment above about
264e1dd0a2fSth 						 * correct format.
265e1dd0a2fSth 						 */
266e1dd0a2fSth 						if (*src == '\0') {
267e1dd0a2fSth 							dst[i++] = '\0';
268e1dd0a2fSth 							return (1);
269e1dd0a2fSth 						}
270e1dd0a2fSth 					}
271e1dd0a2fSth 				}
272e1dd0a2fSth 				while (isspace(*src))
273e1dd0a2fSth 					src++;
274e1dd0a2fSth 			}
275e1dd0a2fSth 		}
276e1dd0a2fSth 		if (*src == '\0')
277e1dd0a2fSth 			dst[i++] = '\0';
278e1dd0a2fSth 	}
279e1dd0a2fSth 
280e1dd0a2fSth 	return (1);
281e1dd0a2fSth }
282e1dd0a2fSth 
283e1dd0a2fSth static
284e1dd0a2fSth char *
get_db(const char * db_name)285e1dd0a2fSth get_db(const char *db_name)
286e1dd0a2fSth {
287e1dd0a2fSth 	char			*ptr;
288e1dd0a2fSth 	switch_database_t	*hostService = NULL;
289e1dd0a2fSth 	FILE			*fp = fopen(__NSW_CONFIG_FILE, "rF");
290e1dd0a2fSth 	char			*linep, line[NSS_BUFSIZ];
291e1dd0a2fSth 
292e1dd0a2fSth 	if (fp == NULL) {
293e1dd0a2fSth 		syslog(LOG_WARNING, gettext("libsldap: can not read %s"),
294e1dd0a2fSth 		    __NSW_CONFIG_FILE);
295e1dd0a2fSth 		return (NULL);
296e1dd0a2fSth 	}
297e1dd0a2fSth 
298e1dd0a2fSth 	while ((linep = fgets(line, NSS_BUFSIZ, fp)) != NULL) {
299e1dd0a2fSth 		while (isspace(*linep)) {
300e1dd0a2fSth 			++linep;
301e1dd0a2fSth 		}
302e1dd0a2fSth 		if (*linep == '#') {
303e1dd0a2fSth 			continue;
304e1dd0a2fSth 		}
305e1dd0a2fSth 		if (strncmp(linep, db_name, strlen(db_name)) != 0) {
306e1dd0a2fSth 			continue;
307e1dd0a2fSth 		}
308e1dd0a2fSth 		if ((linep = strchr(linep, ':')) != NULL) {
309e1dd0a2fSth 			if (linep[strlen(linep) - 1] == '\n') {
310e1dd0a2fSth 				linep[strlen(linep) - 1] = '\0';
311e1dd0a2fSth 			}
312e1dd0a2fSth 
313e1dd0a2fSth 			while (isspace(*++linep))
314e1dd0a2fSth 				;
315e1dd0a2fSth 
316e1dd0a2fSth 			if ((ptr = strchr(linep, '#')) != NULL) {
317e1dd0a2fSth 				while (--ptr >= linep && isspace(*ptr))
318e1dd0a2fSth 					;
319e1dd0a2fSth 				*(ptr + 1) = '\0';
320e1dd0a2fSth 			}
321e1dd0a2fSth 
322e1dd0a2fSth 			if (strlen(linep) == 0) {
323e1dd0a2fSth 				continue;
324e1dd0a2fSth 			}
325e1dd0a2fSth 			break;
326e1dd0a2fSth 		}
327e1dd0a2fSth 	}
328e1dd0a2fSth 
329e1dd0a2fSth 	(void) fclose(fp);
330e1dd0a2fSth 
331e1dd0a2fSth 	if (linep == NULL) {
332e1dd0a2fSth 		syslog(LOG_WARNING,
333e1dd0a2fSth 		    gettext("libsldap: the %s database "
334e1dd0a2fSth 		    "is missing from %s"),
335e1dd0a2fSth 		    db_name,
336e1dd0a2fSth 		    __NSW_CONFIG_FILE);
337e1dd0a2fSth 		return (NULL);
338e1dd0a2fSth 	}
339e1dd0a2fSth 
340e1dd0a2fSth 	(void) thr_getspecific(switchConfigKey, (void **) &hostService);
341e1dd0a2fSth 	if (hostService == NULL) {
342e1dd0a2fSth 		hostService = calloc(1, sizeof (switch_database_t));
343e1dd0a2fSth 		if (hostService == NULL) {
344e1dd0a2fSth 			return (NULL);
345e1dd0a2fSth 		}
346e1dd0a2fSth 		(void) thr_setspecific(switchConfigKey, hostService);
347e1dd0a2fSth 	}
348e1dd0a2fSth 
349e1dd0a2fSth 	/*
350e1dd0a2fSth 	 * In a long-living process threads can perform several
351e1dd0a2fSth 	 * getXbyY requests. And the windows between those requests
352e1dd0a2fSth 	 * can be long. The nsswitch configuration can change from time
353e1dd0a2fSth 	 * to time. So instead of allocating/freeing memory every time
354e1dd0a2fSth 	 * the API is called, reallocate memory only when the current
355e1dd0a2fSth 	 * configuration for the database being used is longer than
356e1dd0a2fSth 	 * the previous one.
357e1dd0a2fSth 	 */
358e1dd0a2fSth 	if (strlen(linep) >= hostService->alloced) {
359e1dd0a2fSth 		ptr = (char *)realloc((void *)hostService->conf,
360e1dd0a2fSth 		    strlen(linep) + 1);
361e1dd0a2fSth 		if (ptr == NULL) {
362e1dd0a2fSth 			free((void *)hostService->conf);
363e1dd0a2fSth 			hostService->conf = NULL;
364e1dd0a2fSth 			hostService->alloced = 0;
365e1dd0a2fSth 			return (NULL);
366e1dd0a2fSth 		}
367e1dd0a2fSth 		bzero(ptr, strlen(linep) + 1);
368e1dd0a2fSth 		hostService->conf = ptr;
369e1dd0a2fSth 		hostService->alloced = strlen(linep) + 1;
370e1dd0a2fSth 	}
371e1dd0a2fSth 
372e1dd0a2fSth 	if (remove_ldap(hostService->conf, linep, hostService->alloced))
373e1dd0a2fSth 		return (hostService->conf);
374e1dd0a2fSth 	else
375e1dd0a2fSth 		return (NULL);
376e1dd0a2fSth }
377e1dd0a2fSth 
378e1dd0a2fSth static
379e1dd0a2fSth void
_initf_ipnodes(nss_db_params_t * p)380e1dd0a2fSth _initf_ipnodes(nss_db_params_t *p)
381e1dd0a2fSth {
382e1dd0a2fSth 	char *services = get_db("ipnodes");
383e1dd0a2fSth 
384e1dd0a2fSth 	p->name = NSS_DBNAM_IPNODES;
385e1dd0a2fSth 	p->flags |= NSS_USE_DEFAULT_CONFIG;
386e1dd0a2fSth 	p->default_config = services == NULL ? "" : services;
387e1dd0a2fSth }
388e1dd0a2fSth 
389e1dd0a2fSth static
390e1dd0a2fSth void
_initf_hosts(nss_db_params_t * p)391e1dd0a2fSth _initf_hosts(nss_db_params_t *p)
392e1dd0a2fSth {
393e1dd0a2fSth 	char *services = get_db("hosts");
394e1dd0a2fSth 
395e1dd0a2fSth 	p->name = NSS_DBNAM_HOSTS;
396e1dd0a2fSth 	p->flags |= NSS_USE_DEFAULT_CONFIG;
397e1dd0a2fSth 	p->default_config = services == NULL ? "" : services;
398e1dd0a2fSth }
399e1dd0a2fSth 
400e1dd0a2fSth /*
401e1dd0a2fSth  * This function is an analog of the standard gethostbyaddr_r()
402e1dd0a2fSth  * function with an exception that it removes the 'ldap' back-end
403e1dd0a2fSth  * (if any) from the host/ipnodes nsswitch's databases and then
404e1dd0a2fSth  * looks up using remaining back-ends.
405e1dd0a2fSth  */
406e1dd0a2fSth static
407e1dd0a2fSth struct hostent *
_filter_gethostbyaddr_r(const char * addr,int len,int type,struct hostent * result,char * buffer,int buflen,int * h_errnop)408e1dd0a2fSth _filter_gethostbyaddr_r(const char *addr, int len, int type,
409e1dd0a2fSth 	struct hostent *result, char *buffer, int buflen,
410e1dd0a2fSth 	int *h_errnop)
411e1dd0a2fSth {
412e1dd0a2fSth 	DEFINE_NSS_DB_ROOT(db_root_hosts);
413e1dd0a2fSth 	DEFINE_NSS_DB_ROOT(db_root_ipnodes);
414e1dd0a2fSth 	nss_XbyY_args_t arg;
415e1dd0a2fSth 	nss_status_t    res;
416e1dd0a2fSth 	int		(*str2ent)();
417e1dd0a2fSth 	void		(*nss_initf)();
418e1dd0a2fSth 	nss_db_root_t	*nss_db_root;
419e1dd0a2fSth 	int		dbop;
420e1dd0a2fSth 
421e1dd0a2fSth 	switch (type) {
422e1dd0a2fSth 	case AF_INET:
423e1dd0a2fSth 		str2ent		= str2hostent;
424e1dd0a2fSth 		nss_initf	= _initf_hosts;
425e1dd0a2fSth 		nss_db_root	= &db_root_hosts;
426e1dd0a2fSth 		dbop		= NSS_DBOP_HOSTS_BYADDR;
427e1dd0a2fSth 		break;
428e1dd0a2fSth 	case AF_INET6:
429e1dd0a2fSth 		str2ent		= str2hostent6;
430e1dd0a2fSth 		nss_initf	= _initf_ipnodes;
431e1dd0a2fSth 		nss_db_root	= &db_root_ipnodes;
432e1dd0a2fSth 		dbop		= NSS_DBOP_IPNODES_BYADDR;
433a17ca6b1SToomas Soome 		break;
434e1dd0a2fSth 	default:
435e1dd0a2fSth 		return (NULL);
436e1dd0a2fSth 	}
437e1dd0a2fSth 
438e1dd0a2fSth 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2ent);
439e1dd0a2fSth 
440e1dd0a2fSth 	arg.key.hostaddr.addr	= addr;
441e1dd0a2fSth 	arg.key.hostaddr.len	= len;
442e1dd0a2fSth 	arg.key.hostaddr.type	= type;
443e1dd0a2fSth 	arg.stayopen		= 0;
444e1dd0a2fSth 	arg.h_errno		= NETDB_SUCCESS;
445e1dd0a2fSth 
446e1dd0a2fSth 	res = nss_search(nss_db_root, nss_initf, dbop, &arg);
447e1dd0a2fSth 	arg.status = res;
448e1dd0a2fSth 	*h_errnop = arg.h_errno;
449e1dd0a2fSth 	return (struct hostent *)NSS_XbyY_FINI(&arg);
450e1dd0a2fSth }
451e1dd0a2fSth 
452e1dd0a2fSth /*
453e1dd0a2fSth  * This routine is an analog of gethostbyaddr_r().
454e1dd0a2fSth  * But in addition __s_api_hostname2ip() performs the "LDAP SKIPDB" activity
455e1dd0a2fSth  * prior to querying the name services.
456e1dd0a2fSth  * If the buffer is not big enough to accommodate a returning data,
457e1dd0a2fSth  * NULL is returned and h_errnop is set to TRY_AGAIN.
458e1dd0a2fSth  */
459e1dd0a2fSth struct hostent *
__s_api_hostname2ip(const char * name,struct hostent * result,char * buffer,int buflen,int * h_errnop)460e1dd0a2fSth __s_api_hostname2ip(const char *name,
461e1dd0a2fSth 	struct hostent *result, char *buffer, int buflen,
462e1dd0a2fSth 	int *h_errnop)
463e1dd0a2fSth {
464e1dd0a2fSth 	DEFINE_NSS_DB_ROOT(db_root_ipnodes);
465e1dd0a2fSth 	DEFINE_NSS_DB_ROOT(db_root_hosts);
466e1dd0a2fSth 	nss_XbyY_args_t	arg;
467e1dd0a2fSth 	nss_status_t	res;
468e1dd0a2fSth 	struct in_addr	addr;
469e1dd0a2fSth 	struct in6_addr	addr6;
470e1dd0a2fSth 
471e1dd0a2fSth 	if (inet_pton(AF_INET, name, &addr) > 0) {
472e1dd0a2fSth 		if (buflen < strlen(name) + 1 +
473e1dd0a2fSth 		    sizeof (char *) * 2 + /* The h_aliases member */
474e1dd0a2fSth 		    sizeof (struct in_addr) +
475e1dd0a2fSth 		    sizeof (struct in_addr *) * 2) {
476e1dd0a2fSth 			*h_errnop = TRY_AGAIN;
477e1dd0a2fSth 			return (NULL);
478e1dd0a2fSth 		}
479e1dd0a2fSth 
480e1dd0a2fSth 		result->h_addrtype = AF_INET;
481e1dd0a2fSth 		result->h_length = sizeof (struct in_addr);
482e1dd0a2fSth 		(void) strncpy(buffer, name, buflen);
483e1dd0a2fSth 
484e1dd0a2fSth 		result->h_addr_list = (char **)ROUND_UP(
485e1dd0a2fSth 		    buffer + strlen(name) + 1,
486e1dd0a2fSth 		    sizeof (char *));
487e1dd0a2fSth 		result->h_aliases = (char **)ROUND_UP(result->h_addr_list,
488e1dd0a2fSth 		    sizeof (char *));
489e1dd0a2fSth 		result->h_aliases[0] = buffer;
490e1dd0a2fSth 		result->h_aliases[1] = NULL;
491e1dd0a2fSth 		bcopy(&addr,
492e1dd0a2fSth 		    buffer + buflen - sizeof (struct in_addr),
493e1dd0a2fSth 		    sizeof (struct in_addr));
494e1dd0a2fSth 		result->h_addr_list[0] = buffer + buflen -
495e1dd0a2fSth 		    sizeof (struct in_addr);
496e1dd0a2fSth 		result->h_addr_list[1] = NULL;
497e1dd0a2fSth 		result->h_aliases = result->h_addr_list;
498e1dd0a2fSth 		result->h_name = buffer;
499e1dd0a2fSth 
500e1dd0a2fSth 		*h_errnop = NETDB_SUCCESS;
501e1dd0a2fSth 		return (result);
502e1dd0a2fSth 	}
503e1dd0a2fSth 	if (inet_pton(AF_INET6, name, &addr6) > 0) {
504e1dd0a2fSth 		if (buflen < strlen(name) + 1 +
505e1dd0a2fSth 		    sizeof (char *) * 2 + /* The h_aliases member */
506e1dd0a2fSth 		    sizeof (struct in6_addr) +
507e1dd0a2fSth 		    sizeof (struct in6_addr *) * 2) {
508e1dd0a2fSth 			*h_errnop = TRY_AGAIN;
509e1dd0a2fSth 			return (NULL);
510e1dd0a2fSth 		}
511e1dd0a2fSth 
512e1dd0a2fSth 		result->h_addrtype = AF_INET6;
513e1dd0a2fSth 		result->h_length = sizeof (struct in6_addr);
514e1dd0a2fSth 		(void) strncpy(buffer, name, buflen);
515e1dd0a2fSth 
516e1dd0a2fSth 		result->h_addr_list = (char **)ROUND_UP(
517e1dd0a2fSth 		    buffer + strlen(name) + 1,
518e1dd0a2fSth 		    sizeof (char *));
519e1dd0a2fSth 		result->h_aliases = (char **)ROUND_UP(result->h_addr_list,
520e1dd0a2fSth 		    sizeof (char *));
521e1dd0a2fSth 		result->h_aliases[0] = buffer;
522e1dd0a2fSth 		result->h_aliases[1] = NULL;
523e1dd0a2fSth 		bcopy(&addr6,
524e1dd0a2fSth 		    buffer + buflen - sizeof (struct in6_addr),
525e1dd0a2fSth 		    sizeof (struct in6_addr));
526e1dd0a2fSth 		result->h_addr_list[0] = buffer + buflen -
527e1dd0a2fSth 		    sizeof (struct in6_addr);
528e1dd0a2fSth 		result->h_addr_list[1] = NULL;
529e1dd0a2fSth 		result->h_aliases = result->h_addr_list;
530e1dd0a2fSth 		result->h_name = buffer;
531e1dd0a2fSth 
532e1dd0a2fSth 		*h_errnop = NETDB_SUCCESS;
533e1dd0a2fSth 		return (result);
534e1dd0a2fSth 	}
535e1dd0a2fSth 
536e1dd0a2fSth 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent);
537e1dd0a2fSth 
538e1dd0a2fSth 	arg.key.name = name;
539e1dd0a2fSth 	arg.stayopen = 0;
540e1dd0a2fSth 	arg.h_errno = NETDB_SUCCESS;
541e1dd0a2fSth 
542e1dd0a2fSth 	res = nss_search(&db_root_ipnodes, _initf_ipnodes,
543e1dd0a2fSth 	    NSS_DBOP_IPNODES_BYNAME, &arg);
544e1dd0a2fSth 	if (res == NSS_NOTFOUND || res == NSS_UNAVAIL) {
545e1dd0a2fSth 		arg.h_errno = NETDB_SUCCESS;
546e1dd0a2fSth 		res = nss_search(&db_root_hosts, _initf_hosts,
547e1dd0a2fSth 		    NSS_DBOP_HOSTS_BYNAME, &arg);
548e1dd0a2fSth 	}
549e1dd0a2fSth 	arg.status = res;
550e1dd0a2fSth 	*h_errnop = arg.h_errno;
551e1dd0a2fSth 	return ((struct hostent *)NSS_XbyY_FINI(&arg));
552e1dd0a2fSth }
553e1dd0a2fSth 
554e1dd0a2fSth /*
555e1dd0a2fSth  * Convert an IP to a host name.
556e1dd0a2fSth  */
557e1dd0a2fSth ns_ldap_return_code
__s_api_ip2hostname(char * ipaddr,char ** hostname)558e1dd0a2fSth __s_api_ip2hostname(char *ipaddr, char **hostname) {
559e1dd0a2fSth 	struct in_addr	in;
560e1dd0a2fSth 	struct in6_addr	in6;
561e1dd0a2fSth 	struct hostent	*hp = NULL, hostEnt;
562e1dd0a2fSth 	char		buffer[NSS_BUFLEN_HOSTS];
563e1dd0a2fSth 	int		buflen = NSS_BUFLEN_HOSTS;
564e1dd0a2fSth 	char		*start = NULL,
565e1dd0a2fSth 			*end = NULL,
566e1dd0a2fSth 			delim = '\0';
567e1dd0a2fSth 	char		*port = NULL,
568e1dd0a2fSth 			*addr = NULL;
569e1dd0a2fSth 	int		errorNum = 0,
570e1dd0a2fSth 			len = 0;
571e1dd0a2fSth 
572e1dd0a2fSth 	if (ipaddr == NULL || hostname == NULL)
573e1dd0a2fSth 		return (NS_LDAP_INVALID_PARAM);
574e1dd0a2fSth 	*hostname = NULL;
575e1dd0a2fSth 	if ((addr = strdup(ipaddr)) == NULL)
576e1dd0a2fSth 		return (NS_LDAP_MEMORY);
577e1dd0a2fSth 
578e1dd0a2fSth 	if (addr[0] == '[') {
579e1dd0a2fSth 		/*
580e1dd0a2fSth 		 * Assume it's [ipv6]:port
581e1dd0a2fSth 		 * Extract ipv6 IP
582e1dd0a2fSth 		 */
583e1dd0a2fSth 		start = &addr[1];
584e1dd0a2fSth 		if ((end = strchr(addr, ']')) != NULL) {
585e1dd0a2fSth 			*end = '\0';
586e1dd0a2fSth 			delim = ']';
587e1dd0a2fSth 			if (*(end + 1) == ':')
588e1dd0a2fSth 				/* extract port */
589e1dd0a2fSth 				port = end + 2;
590e1dd0a2fSth 		} else {
591e1dd0a2fSth 			free(addr);
592e1dd0a2fSth 			return (NS_LDAP_INVALID_PARAM);
593e1dd0a2fSth 		}
594e1dd0a2fSth 	} else if ((end = strchr(addr, ':')) != NULL) {
595e1dd0a2fSth 		/* assume it's ipv4:port */
596e1dd0a2fSth 		*end = '\0';
597e1dd0a2fSth 		delim = ':';
598e1dd0a2fSth 		start = addr;
599e1dd0a2fSth 		port = end + 1;
600e1dd0a2fSth 	} else
601e1dd0a2fSth 		/* No port */
602e1dd0a2fSth 		start = addr;
603e1dd0a2fSth 
604e1dd0a2fSth 
605e1dd0a2fSth 	if (inet_pton(AF_INET, start, &in) == 1) {
606e1dd0a2fSth 		/* IPv4 */
607e1dd0a2fSth 		hp = _filter_gethostbyaddr_r((char *)&in,
608e1dd0a2fSth 		    sizeof (in.s_addr),
609e1dd0a2fSth 		    AF_INET,
610e1dd0a2fSth 		    &hostEnt,
611e1dd0a2fSth 		    buffer,
612e1dd0a2fSth 		    buflen,
613e1dd0a2fSth 		    &errorNum);
614e1dd0a2fSth 		if (hp && hp->h_name) {
615e1dd0a2fSth 			/* hostname + '\0' */
616e1dd0a2fSth 			len = strlen(hp->h_name) + 1;
617e1dd0a2fSth 			if (port)
618e1dd0a2fSth 				/* ':' + port */
619e1dd0a2fSth 				len += strlen(port) + 1;
620e1dd0a2fSth 			if ((*hostname = malloc(len)) == NULL) {
621e1dd0a2fSth 				free(addr);
622e1dd0a2fSth 				return (NS_LDAP_MEMORY);
623e1dd0a2fSth 			}
624e1dd0a2fSth 
625e1dd0a2fSth 			if (port)
626e1dd0a2fSth 				(void) snprintf(*hostname, len, "%s:%s",
627e1dd0a2fSth 						hp->h_name, port);
628e1dd0a2fSth 			else
629e1dd0a2fSth 				(void) strlcpy(*hostname, hp->h_name, len);
630e1dd0a2fSth 
631e1dd0a2fSth 			free(addr);
632e1dd0a2fSth 			return (NS_LDAP_SUCCESS);
633e1dd0a2fSth 		} else {
634e1dd0a2fSth 			free(addr);
635e1dd0a2fSth 			return (NS_LDAP_NOTFOUND);
636e1dd0a2fSth 		}
637e1dd0a2fSth 	} else if (inet_pton(AF_INET6, start, &in6) == 1) {
638e1dd0a2fSth 		/* IPv6 */
639e1dd0a2fSth 		hp = _filter_gethostbyaddr_r((char *)&in6,
640e1dd0a2fSth 		    sizeof (in6.s6_addr),
641e1dd0a2fSth 		    AF_INET6,
642e1dd0a2fSth 		    &hostEnt,
643e1dd0a2fSth 		    buffer,
644e1dd0a2fSth 		    buflen,
645e1dd0a2fSth 		    &errorNum);
646e1dd0a2fSth 		if (hp && hp->h_name) {
647e1dd0a2fSth 			/* hostname + '\0' */
648e1dd0a2fSth 			len = strlen(hp->h_name) + 1;
649e1dd0a2fSth 			if (port)
650e1dd0a2fSth 				/* ':' + port */
651e1dd0a2fSth 				len += strlen(port) + 1;
652e1dd0a2fSth 			if ((*hostname = malloc(len)) == NULL) {
653e1dd0a2fSth 				free(addr);
654e1dd0a2fSth 				return (NS_LDAP_MEMORY);
655e1dd0a2fSth 			}
656e1dd0a2fSth 
657e1dd0a2fSth 			if (port)
658e1dd0a2fSth 				(void) snprintf(*hostname, len, "%s:%s",
659e1dd0a2fSth 						hp->h_name, port);
660e1dd0a2fSth 			else
661e1dd0a2fSth 				(void) strlcpy(*hostname, hp->h_name, len);
662e1dd0a2fSth 
663e1dd0a2fSth 			free(addr);
664e1dd0a2fSth 			return (NS_LDAP_SUCCESS);
665e1dd0a2fSth 		} else {
666e1dd0a2fSth 			free(addr);
667e1dd0a2fSth 			return (NS_LDAP_NOTFOUND);
668e1dd0a2fSth 		}
669e1dd0a2fSth 	} else {
670e1dd0a2fSth 		/*
671e1dd0a2fSth 		 * A hostname
672e1dd0a2fSth 		 * Return it as is
673e1dd0a2fSth 		 */
674e1dd0a2fSth 		if (end)
675e1dd0a2fSth 			*end = delim;
676e1dd0a2fSth 		*hostname = addr;
677e1dd0a2fSth 		return (NS_LDAP_SUCCESS);
678e1dd0a2fSth 	}
679e1dd0a2fSth }
680e1dd0a2fSth 
681e1dd0a2fSth /*
682e1dd0a2fSth  * This function obtains data returned by an LDAP search request and puts it
683e1dd0a2fSth  * in a string in the ldap_cachmgr(1) door call format.
684e1dd0a2fSth  *
685e1dd0a2fSth  * INPUT:
686e1dd0a2fSth  *     ld - a pointer to an LDAP structure used for a search operation,
687e1dd0a2fSth  *     result_msg - a pointer to an LDAPMessage returned by the search,
688e1dd0a2fSth  *     include_names - if set to INCLUDE_ATTR_NAMES, the output buffer will
689e1dd0a2fSth  *                     contain attribute names.
690e1dd0a2fSth  *                     Otherwise, only values will be return.
691e1dd0a2fSth  *
692e1dd0a2fSth  * OUTPUT:
693e1dd0a2fSth  *      a buffer containing server info in the following format:
694e1dd0a2fSth  *         [<attribute name>=]value [DOORLINESEP [<attribute name>=]value ]...]
695e1dd0a2fSth  *      Should be free'ed by the caller.
696e1dd0a2fSth  */
697e1dd0a2fSth static
698e1dd0a2fSth ns_ldap_return_code
convert_to_door_line(LDAP * ld,LDAPMessage * result_msg,int include_names,int is_profile,char ** door_line)699e1dd0a2fSth convert_to_door_line(LDAP* ld,
700e1dd0a2fSth 		LDAPMessage *result_msg,
701e1dd0a2fSth 		int include_names,
702e1dd0a2fSth 		int is_profile,
703e1dd0a2fSth 		char **door_line)
704e1dd0a2fSth {
705e1dd0a2fSth 	uint32_t	total_length = 0, attr_len = 0, i;
706e1dd0a2fSth 	LDAPMessage	*e;
707e1dd0a2fSth 	char		*a, **vals;
708e1dd0a2fSth 	BerElement	*ber;
709e1dd0a2fSth 	int		seen_objectclass = 0, rewind = 0;
710e1dd0a2fSth 
711e1dd0a2fSth 	if (!door_line) {
712e1dd0a2fSth 		return (NS_LDAP_INVALID_PARAM);
713e1dd0a2fSth 	}
714e1dd0a2fSth 	*door_line = NULL;
715e1dd0a2fSth 
716e1dd0a2fSth 	if ((e = ldap_first_entry(ld, result_msg)) == NULL) {
717e1dd0a2fSth 		return (NS_LDAP_NOTFOUND);
718e1dd0a2fSth 	}
719e1dd0a2fSth 
720e1dd0a2fSth 	/* calculate length of received data */
721e1dd0a2fSth 	for (a = ldap_first_attribute(ld, e, &ber);
722e1dd0a2fSth 	    a != NULL;
723e1dd0a2fSth 	    a = ldap_next_attribute(ld, e, ber)) {
724e1dd0a2fSth 
725e1dd0a2fSth 		if ((vals = ldap_get_values(ld, e, a)) != NULL) {
726e1dd0a2fSth 			for (i = 0; vals[i] != NULL; i++) {
727e1dd0a2fSth 				total_length += (include_names ?
728e1dd0a2fSth 				    strlen(a) : 0) +
729e1dd0a2fSth 				    strlen(vals[i]) +
730e1dd0a2fSth 				    strlen(DOORLINESEP) +1;
731e1dd0a2fSth 			}
732e1dd0a2fSth 			ldap_value_free(vals);
733e1dd0a2fSth 		}
734e1dd0a2fSth 		ldap_memfree(a);
735e1dd0a2fSth 	}
736e1dd0a2fSth 	if (ber != NULL) {
737e1dd0a2fSth 		ber_free(ber, 0);
738e1dd0a2fSth 	}
739e1dd0a2fSth 
740e1dd0a2fSth 	if (total_length == 0) {
741e1dd0a2fSth 		return (NS_LDAP_NOTFOUND);
742e1dd0a2fSth 	}
743e1dd0a2fSth 
744e1dd0a2fSth 	/* copy the data */
745e1dd0a2fSth 	/* add 1 for the last '\0' */
746e1dd0a2fSth 	*door_line  = (char *)malloc(total_length + 1);
747e1dd0a2fSth 	if (*door_line == NULL) {
748e1dd0a2fSth 		return (NS_LDAP_MEMORY);
749e1dd0a2fSth 	}
750e1dd0a2fSth 
751e1dd0a2fSth 	/* make it an empty string first */
752e1dd0a2fSth 	**door_line = '\0';
753e1dd0a2fSth 	a = ldap_first_attribute(ld, e, &ber);
754e1dd0a2fSth 	while (a != NULL) {
755e1dd0a2fSth 		if (is_profile) {
756e1dd0a2fSth 			/*
757e1dd0a2fSth 			 * If we're processing DUAConfigProfile, we need to make
758e1dd0a2fSth 			 * sure we put objectclass attribute first.
759e1dd0a2fSth 			 * __s_api_create_config_door_str depends on that.
760e1dd0a2fSth 			 */
761e1dd0a2fSth 			if (seen_objectclass) {
762e1dd0a2fSth 				if (strcasecmp(a, "objectclass") == 0) {
763e1dd0a2fSth 					/* Skip objectclass now. */
764e1dd0a2fSth 					a = ldap_next_attribute(ld, e, ber);
765e1dd0a2fSth 					continue;
766e1dd0a2fSth 				}
767e1dd0a2fSth 			} else {
768e1dd0a2fSth 				if (strcasecmp(a, "objectclass") == 0) {
769e1dd0a2fSth 					seen_objectclass = 1;
770e1dd0a2fSth 					rewind = 1;
771e1dd0a2fSth 				} else {
772e1dd0a2fSth 					/* Skip all but objectclass first. */
773e1dd0a2fSth 					a = ldap_next_attribute(ld, e, ber);
774e1dd0a2fSth 					continue;
775e1dd0a2fSth 				}
776e1dd0a2fSth 			}
777e1dd0a2fSth 		}
778e1dd0a2fSth 
779e1dd0a2fSth 		if ((vals = ldap_get_values(ld, e, a)) != NULL) {
780e1dd0a2fSth 			for (i = 0; vals[i] != NULL; i++) {
781e1dd0a2fSth 				if (include_names) {
782e1dd0a2fSth 					attr_len += strlen(a);
783e1dd0a2fSth 				}
784e1dd0a2fSth 				attr_len += strlen(vals[i]) +
785e1dd0a2fSth 				    strlen(DOORLINESEP) + 2;
786e1dd0a2fSth 				if (include_names) {
787e1dd0a2fSth 					(void) snprintf(*door_line +
788e1dd0a2fSth 					    strlen(*door_line),
789e1dd0a2fSth 					    attr_len,
790e1dd0a2fSth 					    "%s=%s%s",
791e1dd0a2fSth 					    a, vals[i],
792e1dd0a2fSth 					    DOORLINESEP);
793e1dd0a2fSth 				} else {
794e1dd0a2fSth 					(void) snprintf(*door_line +
795e1dd0a2fSth 					    strlen(*door_line),
796e1dd0a2fSth 					    attr_len,
797e1dd0a2fSth 					    "%s%s",
798e1dd0a2fSth 					    vals[i],
799e1dd0a2fSth 					    DOORLINESEP);
800e1dd0a2fSth 				}
801e1dd0a2fSth 			}
802e1dd0a2fSth 			ldap_value_free(vals);
803e1dd0a2fSth 		}
804e1dd0a2fSth 		ldap_memfree(a);
805e1dd0a2fSth 
806e1dd0a2fSth 		/* Rewind */
807e1dd0a2fSth 		if (rewind) {
808e1dd0a2fSth 			if (ber != NULL) {
809e1dd0a2fSth 				ber_free(ber, 0);
810e1dd0a2fSth 			}
811e1dd0a2fSth 			a = ldap_first_attribute(ld, e, &ber);
812e1dd0a2fSth 			rewind = 0;
813e1dd0a2fSth 		} else {
814e1dd0a2fSth 			a = ldap_next_attribute(ld, e, ber);
815e1dd0a2fSth 		}
816e1dd0a2fSth 	}
817e1dd0a2fSth 	if (ber != NULL) {
818e1dd0a2fSth 		ber_free(ber, 0);
819e1dd0a2fSth 	}
820e1dd0a2fSth 
821e1dd0a2fSth 	if (e != result_msg) {
822e1dd0a2fSth 		(void) ldap_msgfree(e);
823e1dd0a2fSth 	}
824e1dd0a2fSth 
825e1dd0a2fSth 	return (NS_LDAP_SUCCESS);
826e1dd0a2fSth }
827e1dd0a2fSth 
828e1dd0a2fSth /*
829e1dd0a2fSth  * This function looks up the base DN of a directory serving
830e1dd0a2fSth  * a specified domain name.
831e1dd0a2fSth  *
832e1dd0a2fSth  * INPUT:
833e1dd0a2fSth  *     ld - a pointer to an LDAP structure used for the search operation,
834e1dd0a2fSth  *     domain_name - the name of a domain.
835e1dd0a2fSth  *
836e1dd0a2fSth  * OUTPUT:
837e1dd0a2fSth  *     a buffer containing a directory's base DN found.
838e1dd0a2fSth  *     Should be free'ed by the caller.
839e1dd0a2fSth  */
840e1dd0a2fSth static
841e1dd0a2fSth ns_ldap_return_code
getDirBaseDN(LDAP * ld,const char * domain_name,char ** dir_base_dn)842e1dd0a2fSth getDirBaseDN(LDAP *ld, const char *domain_name, char **dir_base_dn)
843e1dd0a2fSth {
844e1dd0a2fSth 	struct timeval		tv = {NS_DEFAULT_SEARCH_TIMEOUT, 0};
845e1dd0a2fSth 	char			*attrs[2], *DNlist, *rest, *ptr;
846e1dd0a2fSth 	char			filter[BUFSIZ], *a = NULL;
847e1dd0a2fSth 	int			ldap_rc;
848e1dd0a2fSth 	LDAPMessage		*resultMsg = NULL;
849e1dd0a2fSth 	ns_ldap_return_code	ret_code;
850e1dd0a2fSth 
851e1dd0a2fSth 	/* Get the whole list of naming contexts residing on the server */
852e1dd0a2fSth 	attrs[0] = "namingcontexts";
853e1dd0a2fSth 	attrs[1] = NULL;
854e1dd0a2fSth 	ldap_rc = ldap_search_ext_s(ld, "", LDAP_SCOPE_BASE, "(objectclass=*)",
855e1dd0a2fSth 	    attrs, 0, NULL, NULL, &tv, 0, &resultMsg);
856e1dd0a2fSth 	switch (ldap_rc) {
857e1dd0a2fSth 		/* If successful, the root DSE was found. */
858e1dd0a2fSth 		case LDAP_SUCCESS:
859e1dd0a2fSth 			break;
860e1dd0a2fSth 		/*
861e1dd0a2fSth 		 * If the root DSE was not found, the server does
862e1dd0a2fSth 		 * not comply with the LDAP v3 protocol.
863e1dd0a2fSth 		 */
864e1dd0a2fSth 		default:
865e1dd0a2fSth 			if (resultMsg) {
866e1dd0a2fSth 				(void) ldap_msgfree(resultMsg);
867e1dd0a2fSth 				resultMsg = NULL;
868e1dd0a2fSth 			}
869e1dd0a2fSth 
870e1dd0a2fSth 			return (NS_LDAP_OP_FAILED);
871e1dd0a2fSth 	}
872e1dd0a2fSth 
873e1dd0a2fSth 	if ((ret_code = convert_to_door_line(ld,
874e1dd0a2fSth 	    resultMsg,
875e1dd0a2fSth 	    DONT_INCLUDE_ATTR_NAMES,
876e1dd0a2fSth 	    NOT_PROFILE,
877e1dd0a2fSth 	    &DNlist)) != NS_LDAP_SUCCESS) {
878e1dd0a2fSth 		if (resultMsg) {
879e1dd0a2fSth 			(void) ldap_msgfree(resultMsg);
880e1dd0a2fSth 			resultMsg = NULL;
881e1dd0a2fSth 		}
882e1dd0a2fSth 		return (ret_code);
883e1dd0a2fSth 	}
884e1dd0a2fSth 
885e1dd0a2fSth 	if (resultMsg) {
886e1dd0a2fSth 		(void) ldap_msgfree(resultMsg);
887e1dd0a2fSth 		resultMsg = NULL;
888e1dd0a2fSth 	}
889e1dd0a2fSth 
890e1dd0a2fSth 	if (DNlist == NULL ||
891e1dd0a2fSth 	    (ptr = strtok_r(DNlist, DOORLINESEP, &rest)) == NULL) {
892e1dd0a2fSth 		return (NS_LDAP_NOTFOUND);
893e1dd0a2fSth 	}
894e1dd0a2fSth 	attrs[0] = "dn";
895e1dd0a2fSth 	do {
896e1dd0a2fSth 		/*
897e1dd0a2fSth 		 * For each context try to find a NIS domain object
898e1dd0a2fSth 		 * which 'nisdomain' attribute's value matches the domain name
899e1dd0a2fSth 		 */
900e1dd0a2fSth 		(void) snprintf(filter,
901e1dd0a2fSth 		    BUFSIZ,
902e1dd0a2fSth 		    "(&(objectclass=nisDomainObject)"
903e1dd0a2fSth 		    "(nisdomain=%s))",
904e1dd0a2fSth 		    domain_name);
905e1dd0a2fSth 		ldap_rc = ldap_search_ext_s(ld,
906e1dd0a2fSth 		    ptr,
907e1dd0a2fSth 		    LDAP_SCOPE_SUBTREE,
908e1dd0a2fSth 		    filter,
909e1dd0a2fSth 		    attrs,
910e1dd0a2fSth 		    0,
911e1dd0a2fSth 		    NULL,
912e1dd0a2fSth 		    NULL,
913e1dd0a2fSth 		    &tv,
914e1dd0a2fSth 		    0,
915e1dd0a2fSth 		    &resultMsg);
916e1dd0a2fSth 		if (ldap_rc != LDAP_SUCCESS) {
917e1dd0a2fSth 			if (resultMsg) {
918e1dd0a2fSth 				(void) ldap_msgfree(resultMsg);
919e1dd0a2fSth 				resultMsg = NULL;
920e1dd0a2fSth 			}
921e1dd0a2fSth 			continue;
922e1dd0a2fSth 		}
923e1dd0a2fSth 		if ((a = ldap_get_dn(ld, resultMsg)) != NULL) {
924e1dd0a2fSth 			*dir_base_dn = strdup(a);
925e1dd0a2fSth 			ldap_memfree(a);
926e1dd0a2fSth 
927e1dd0a2fSth 			if (resultMsg) {
928e1dd0a2fSth 				(void) ldap_msgfree(resultMsg);
929e1dd0a2fSth 				resultMsg = NULL;
930e1dd0a2fSth 			}
931e1dd0a2fSth 
932e1dd0a2fSth 			if (!*dir_base_dn) {
933e1dd0a2fSth 				free(DNlist);
934e1dd0a2fSth 				return (NS_LDAP_MEMORY);
935e1dd0a2fSth 			}
936e1dd0a2fSth 			break;
937e1dd0a2fSth 		}
938e1dd0a2fSth 
939e1dd0a2fSth 		if (resultMsg) {
940e1dd0a2fSth 			(void) ldap_msgfree(resultMsg);
941e1dd0a2fSth 			resultMsg = NULL;
942e1dd0a2fSth 		}
943e1dd0a2fSth 	} while (ptr = strtok_r(NULL, DOORLINESEP, &rest));
944e1dd0a2fSth 
945e1dd0a2fSth 	free(DNlist);
946e1dd0a2fSth 
947e1dd0a2fSth 	if (!*dir_base_dn) {
948e1dd0a2fSth 		return (NS_LDAP_NOTFOUND);
949e1dd0a2fSth 	}
950e1dd0a2fSth 
951e1dd0a2fSth 	return (NS_LDAP_SUCCESS);
952e1dd0a2fSth }
953e1dd0a2fSth 
954e1dd0a2fSth /*
955e1dd0a2fSth  * This function parses the results of a search operation
956e1dd0a2fSth  * requesting a DUAProfile.
957e1dd0a2fSth  *
958e1dd0a2fSth  * INPUT:
959e1dd0a2fSth  *     ld - a pointer to an LDAP structure used for the search operation,
960e1dd0a2fSth  *     dir_base_dn - the name of a directory's base DN,
961e1dd0a2fSth  *     profile_name - the name of a DUAProfile to be obtained.
962e1dd0a2fSth  *
963e1dd0a2fSth  * OUTPUT:
964e1dd0a2fSth  *      a buffer containing the DUAProfile in the following format:
965e1dd0a2fSth  *        [<attribute name>=]value [DOORLINESEP [<attribute name>=]value ]...]
966e1dd0a2fSth  *      Should be free'ed by the caller.
967e1dd0a2fSth  */
968e1dd0a2fSth static
969e1dd0a2fSth ns_ldap_return_code
getDUAProfile(LDAP * ld,const char * dir_base_dn,const char * profile_name,char ** profile)970e1dd0a2fSth getDUAProfile(LDAP *ld,
971e1dd0a2fSth 		const char *dir_base_dn,
972e1dd0a2fSth 		const char *profile_name,
973e1dd0a2fSth 		char **profile)
974e1dd0a2fSth {
975e1dd0a2fSth 	char			searchBaseDN[BUFSIZ], filter[BUFSIZ];
976e1dd0a2fSth 	LDAPMessage		*resultMsg = NULL;
977e1dd0a2fSth 	struct timeval		tv = {NS_DEFAULT_SEARCH_TIMEOUT, 0};
978e1dd0a2fSth 	int			ldap_rc;
979e1dd0a2fSth 	ns_ldap_return_code	ret_code;
980e1dd0a2fSth 
981e1dd0a2fSth 	(void) snprintf(searchBaseDN, BUFSIZ, "ou=profile,%s", dir_base_dn);
982e1dd0a2fSth 	(void) snprintf(filter,
983e1dd0a2fSth 	    BUFSIZ,
984e1dd0a2fSth 	    _PROFILE_FILTER,
985e1dd0a2fSth 	    _PROFILE1_OBJECTCLASS,
986e1dd0a2fSth 	    _PROFILE2_OBJECTCLASS,
987e1dd0a2fSth 	    profile_name);
988e1dd0a2fSth 	ldap_rc = ldap_search_ext_s(ld,
989e1dd0a2fSth 	    searchBaseDN,
990e1dd0a2fSth 	    LDAP_SCOPE_SUBTREE,
991e1dd0a2fSth 	    filter,
992e1dd0a2fSth 	    NULL,
993e1dd0a2fSth 	    0,
994e1dd0a2fSth 	    NULL,
995e1dd0a2fSth 	    NULL,
996e1dd0a2fSth 	    &tv,
997e1dd0a2fSth 	    0,
998e1dd0a2fSth 	    &resultMsg);
999e1dd0a2fSth 
1000e1dd0a2fSth 	switch (ldap_rc) {
1001e1dd0a2fSth 		/* If successful, the DUA profile was found. */
1002e1dd0a2fSth 		case LDAP_SUCCESS:
1003e1dd0a2fSth 			break;
1004e1dd0a2fSth 		/*
1005e1dd0a2fSth 		 * If the root DSE was not found, the server does
1006e1dd0a2fSth 		 * not comply with the LDAP v3 protocol.
1007e1dd0a2fSth 		 */
1008e1dd0a2fSth 		default:
1009e1dd0a2fSth 			if (resultMsg) {
1010e1dd0a2fSth 				(void) ldap_msgfree(resultMsg);
1011e1dd0a2fSth 				resultMsg = NULL;
1012e1dd0a2fSth 			}
1013e1dd0a2fSth 
1014e1dd0a2fSth 			return (NS_LDAP_OP_FAILED);
1015e1dd0a2fSth 	}
1016e1dd0a2fSth 
1017e1dd0a2fSth 	ret_code = convert_to_door_line(ld,
1018e1dd0a2fSth 	    resultMsg,
1019e1dd0a2fSth 	    INCLUDE_ATTR_NAMES,
1020e1dd0a2fSth 	    IS_PROFILE,
1021e1dd0a2fSth 	    profile);
1022e1dd0a2fSth 	if (resultMsg) {
1023e1dd0a2fSth 		(void) ldap_msgfree(resultMsg);
1024e1dd0a2fSth 		resultMsg = NULL;
1025e1dd0a2fSth 	}
1026e1dd0a2fSth 	return (ret_code);
1027e1dd0a2fSth }
1028e1dd0a2fSth 
1029e1dd0a2fSth /*
1030e1dd0a2fSth  * This function derives the directory's base DN from a provided domain name.
1031e1dd0a2fSth  *
1032e1dd0a2fSth  * INPUT:
1033e1dd0a2fSth  *     domain_name - the name of a domain to be converted into a base DN,
1034e1dd0a2fSth  *     buffer - contains the derived base DN,
1035e1dd0a2fSth  *     buf_len - the length of the buffer.
1036e1dd0a2fSth  *
1037e1dd0a2fSth  * OUTPUT:
1038e1dd0a2fSth  *     The function returns the address of the buffer or NULL.
1039e1dd0a2fSth  */
1040e1dd0a2fSth static
1041e1dd0a2fSth char *
domainname2baseDN(char * domain_name,char * buffer,uint16_t buf_len)1042e1dd0a2fSth domainname2baseDN(char *domain_name, char *buffer, uint16_t buf_len)
1043e1dd0a2fSth {
1044e1dd0a2fSth 	char		*nextDC, *chr;
1045e1dd0a2fSth 	uint16_t	i, length;
1046e1dd0a2fSth 
1047e1dd0a2fSth 	if (!domain_name || !buffer || buf_len == 0) {
1048e1dd0a2fSth 		return (NULL);
1049e1dd0a2fSth 	}
1050e1dd0a2fSth 
1051e1dd0a2fSth 	buffer[0] = '\0';
1052e1dd0a2fSth 	nextDC = chr = domain_name;
1053e1dd0a2fSth 	length = strlen(domain_name);
1054e1dd0a2fSth 	for (i = 0; i < length + 1; ++i, ++chr) {
1055e1dd0a2fSth 		/* Simply replace dots with "dc=" */
1056e1dd0a2fSth 		if (*chr != '.' && *chr != '\0') {
1057e1dd0a2fSth 			continue;
1058e1dd0a2fSth 		}
1059e1dd0a2fSth 		*chr = '\0';
1060e1dd0a2fSth 		if (strlcat(buffer, "dc=", buf_len) >= buf_len)
1061e1dd0a2fSth 			return (NULL);
1062e1dd0a2fSth 		if (strlcat(buffer, nextDC, buf_len) >= buf_len)
1063e1dd0a2fSth 			return (NULL);
1064e1dd0a2fSth 		if (i < length) {
1065e1dd0a2fSth 			/*
1066e1dd0a2fSth 			 * The end of the domain name
1067e1dd0a2fSth 			 * has not been reached yet
1068e1dd0a2fSth 			 */
1069e1dd0a2fSth 			if (strlcat(buffer, ",", buf_len) >= buf_len)
1070e1dd0a2fSth 				return (NULL);
1071e1dd0a2fSth 			nextDC = chr + 1;
1072e1dd0a2fSth 			*chr = '.';
1073e1dd0a2fSth 		}
1074e1dd0a2fSth 	}
1075e1dd0a2fSth 
1076e1dd0a2fSth 	return (buffer);
1077e1dd0a2fSth }
1078e1dd0a2fSth 
1079e1dd0a2fSth /*
1080e1dd0a2fSth  * This function obtains the directory's base DN and a DUAProfile
1081e1dd0a2fSth  * from a specified server.
1082e1dd0a2fSth  *
1083e1dd0a2fSth  * INPUT:
1084e1dd0a2fSth  *     server - a structure describing a server to connect to and
1085e1dd0a2fSth  *              a DUAProfile to be obtained from the server,
1086e1dd0a2fSth  *     cred - credentials to be used during establishing connections to
1087e1dd0a2fSth  *            the server.
1088e1dd0a2fSth  *
1089e1dd0a2fSth  * OUTPUT:
1090e1dd0a2fSth  *     dua_profile - a buffer containing the DUAProfile in the following format:
1091e1dd0a2fSth  *        [<attribute name>=]value [DOORLINESEP [<attribute name>=]value ]...]
1092e1dd0a2fSth  *     dir_base_dn - a buffer containing the base DN,
1093e1dd0a2fSth  *     errorp - an error object describing an error, if any.
1094e1dd0a2fSth  *
1095e1dd0a2fSth  *     All the output data structures should be free'ed by the caller.
1096e1dd0a2fSth  */
1097e1dd0a2fSth ns_ldap_return_code
__ns_ldap_getConnectionInfoFromDUA(const ns_dir_server_t * server,const ns_cred_t * cred,char ** dua_profile,char ** dir_base_dn,ns_ldap_error_t ** errorp)1098e1dd0a2fSth __ns_ldap_getConnectionInfoFromDUA(const ns_dir_server_t *server,
1099e1dd0a2fSth 	const ns_cred_t *cred,
1100e1dd0a2fSth 	char **dua_profile,
1101e1dd0a2fSth 	char **dir_base_dn,
1102e1dd0a2fSth 	ns_ldap_error_t **errorp)
1103e1dd0a2fSth {
1104e1dd0a2fSth 	char			serverAddr[MAX_HOSTADDR_LEN];
1105e1dd0a2fSth 	char			*dirBaseDN = NULL, *duaProfile = NULL;
1106e1dd0a2fSth 	ns_cred_t		default_cred;
1107e1dd0a2fSth 	ns_ldap_return_code	ret_code;
1108e1dd0a2fSth 
1109e1dd0a2fSth 	ns_config_t		*config_struct = __s_api_create_config();
1110e1dd0a2fSth 	ConnectionID		sessionId = 0;
1111e1dd0a2fSth 	Connection		*session = NULL;
1112e1dd0a2fSth 	char			errmsg[MAXERROR];
1113e1dd0a2fSth 	char			buffer[NSS_BUFLEN_HOSTS];
111429836b19Smichen 	ns_conn_user_t		*cu = NULL;
1115e1dd0a2fSth 
1116e1dd0a2fSth 	if (errorp == NULL) {
1117e1dd0a2fSth 		__s_api_destroy_config(config_struct);
1118e1dd0a2fSth 		return (NS_LDAP_INVALID_PARAM);
1119e1dd0a2fSth 	}
1120e1dd0a2fSth 
1121e1dd0a2fSth 	*errorp = NULL;
1122e1dd0a2fSth 
1123e1dd0a2fSth 	if (server == NULL) {
1124e1dd0a2fSth 		__s_api_destroy_config(config_struct);
1125e1dd0a2fSth 		return (NS_LDAP_INVALID_PARAM);
1126e1dd0a2fSth 	}
1127e1dd0a2fSth 
1128e1dd0a2fSth 	if (config_struct == NULL) {
1129e1dd0a2fSth 		return (NS_LDAP_MEMORY);
1130e1dd0a2fSth 	}
1131e1dd0a2fSth 
1132e1dd0a2fSth 	/*
1133e1dd0a2fSth 	 * If no credentials are specified, try to establish a connection
1134e1dd0a2fSth 	 * as anonymous.
1135e1dd0a2fSth 	 */
1136e1dd0a2fSth 	if (!cred) {
1137e1dd0a2fSth 		default_cred.cred.unix_cred.passwd = NULL;
1138e1dd0a2fSth 		default_cred.cred.unix_cred.userID = NULL;
1139e1dd0a2fSth 		default_cred.auth.type = NS_LDAP_AUTH_NONE;
1140e1dd0a2fSth 	}
1141e1dd0a2fSth 
1142e1dd0a2fSth 	/* Now create a default LDAP configuration */
1143e1dd0a2fSth 
1144e1dd0a2fSth 	(void) strncpy(buffer, server->server, sizeof (buffer));
1145e1dd0a2fSth 	if (__ns_ldap_setParamValue(config_struct, NS_LDAP_SERVERS_P, buffer,
1146e1dd0a2fSth 	    errorp) != NS_LDAP_SUCCESS) {
1147e1dd0a2fSth 		__s_api_destroy_config(config_struct);
1148e1dd0a2fSth 		return (NS_LDAP_CONFIG);
1149e1dd0a2fSth 	}
1150e1dd0a2fSth 
1151e1dd0a2fSth 	/* Put together the address and the port specified by the user app. */
1152e1dd0a2fSth 	if (server->port > 0) {
1153e1dd0a2fSth 		(void) snprintf(serverAddr,
1154e1dd0a2fSth 		    sizeof (serverAddr),
1155e1dd0a2fSth 		    "%s:%hu",
1156e1dd0a2fSth 		    buffer,
1157e1dd0a2fSth 		    server->port);
1158e1dd0a2fSth 	} else {
1159e1dd0a2fSth 		(void) strncpy(serverAddr, buffer, sizeof (serverAddr));
1160e1dd0a2fSth 	}
1161e1dd0a2fSth 
1162e1dd0a2fSth 	/*
1163e1dd0a2fSth 	 * There is no default value for the 'Default Search Base DN' attribute.
1164e1dd0a2fSth 	 * Derive one from the domain name to make __s_api_crosscheck() happy.
1165e1dd0a2fSth 	 */
1166e1dd0a2fSth 	if (domainname2baseDN(server->domainName ?
1167e1dd0a2fSth 	    server->domainName : config_struct->domainName,
1168e1dd0a2fSth 	    buffer, NSS_BUFLEN_HOSTS) == NULL) {
1169e1dd0a2fSth 		(void) snprintf(errmsg,
1170e1dd0a2fSth 		    sizeof (errmsg),
1171e1dd0a2fSth 		    gettext("Can not convert %s into a base DN name"),
1172e1dd0a2fSth 		    server->domainName ?
1173e1dd0a2fSth 		    server->domainName : config_struct->domainName);
1174e1dd0a2fSth 		MKERROR(LOG_ERR,
1175e1dd0a2fSth 		    *errorp,
1176e1dd0a2fSth 		    NS_LDAP_INTERNAL,
1177e1dd0a2fSth 		    strdup(errmsg),
1178e1dd0a2fSth 		    NS_LDAP_MEMORY);
1179e1dd0a2fSth 		__s_api_destroy_config(config_struct);
1180e1dd0a2fSth 		return (NS_LDAP_INTERNAL);
1181e1dd0a2fSth 	}
1182e1dd0a2fSth 	if (__ns_ldap_setParamValue(config_struct, NS_LDAP_SEARCH_BASEDN_P,
1183e1dd0a2fSth 	    buffer, errorp) != NS_LDAP_SUCCESS) {
1184e1dd0a2fSth 		__s_api_destroy_config(config_struct);
1185e1dd0a2fSth 		return (NS_LDAP_CONFIG);
1186e1dd0a2fSth 	}
1187e1dd0a2fSth 
1188e1dd0a2fSth 	if (__s_api_crosscheck(config_struct, errmsg, B_FALSE) != NS_SUCCESS) {
1189e1dd0a2fSth 		__s_api_destroy_config(config_struct);
1190e1dd0a2fSth 		return (NS_LDAP_CONFIG);
1191e1dd0a2fSth 	}
1192e1dd0a2fSth 
1193e1dd0a2fSth 	__s_api_init_config(config_struct);
1194e1dd0a2fSth 
1195e1dd0a2fSth 	__s_api_setInitMode();
1196e1dd0a2fSth 
119729836b19Smichen 	cu = __s_api_conn_user_init(NS_CONN_USER_SEARCH, NULL, B_FALSE);
119829836b19Smichen 	if (cu == NULL) {
119929836b19Smichen 		return (NS_LDAP_INTERNAL);
120029836b19Smichen 	}
120129836b19Smichen 
1202e1dd0a2fSth 	if ((ret_code = __s_api_getConnection(serverAddr,
1203e1dd0a2fSth 	    NS_LDAP_NEW_CONN,
1204e1dd0a2fSth 	    cred ? cred : &default_cred,
1205e1dd0a2fSth 	    &sessionId,
1206e1dd0a2fSth 	    &session,
1207e1dd0a2fSth 	    errorp,
1208e1dd0a2fSth 	    0,
1209e1dd0a2fSth 	    0,
121029836b19Smichen 	    cu)) != NS_LDAP_SUCCESS) {
121129836b19Smichen 		__s_api_conn_user_free(cu);
1212e1dd0a2fSth 		__s_api_unsetInitMode();
1213e1dd0a2fSth 		return (ret_code);
1214e1dd0a2fSth 	}
1215e1dd0a2fSth 
1216e1dd0a2fSth 	__s_api_unsetInitMode();
1217e1dd0a2fSth 
1218e1dd0a2fSth 	if ((ret_code = getDirBaseDN(session->ld,
1219e1dd0a2fSth 	    server->domainName ?
1220e1dd0a2fSth 	    server->domainName :
1221e1dd0a2fSth 	    config_struct->domainName,
1222e1dd0a2fSth 	    &dirBaseDN)) != NS_LDAP_SUCCESS) {
1223e1dd0a2fSth 		(void) snprintf(errmsg,
1224e1dd0a2fSth 		    sizeof (errmsg),
1225e1dd0a2fSth 		    gettext("Can not find the "
1226e1dd0a2fSth 		    "nisDomainObject for domain %s\n"),
1227e1dd0a2fSth 		    server->domainName ?
1228e1dd0a2fSth 		    server->domainName : config_struct->domainName);
1229e1dd0a2fSth 		MKERROR(LOG_ERR,
1230e1dd0a2fSth 		    *errorp,
1231e1dd0a2fSth 		    ret_code,
1232e1dd0a2fSth 		    strdup(errmsg),
1233e1dd0a2fSth 		    NS_LDAP_MEMORY);
123429836b19Smichen 		__s_api_conn_user_free(cu);
1235e1dd0a2fSth 		DropConnection(sessionId, NS_LDAP_NEW_CONN);
1236e1dd0a2fSth 		return (ret_code);
1237e1dd0a2fSth 	}
1238e1dd0a2fSth 
1239e1dd0a2fSth 	/*
1240e1dd0a2fSth 	 * And here obtain a DUAProfile which will be used
1241e1dd0a2fSth 	 * as a real configuration.
1242e1dd0a2fSth 	 */
1243e1dd0a2fSth 	if ((ret_code = getDUAProfile(session->ld,
1244e1dd0a2fSth 	    dirBaseDN,
1245e1dd0a2fSth 	    server->profileName ?
1246e1dd0a2fSth 	    server->profileName : "default",
1247e1dd0a2fSth 	    &duaProfile)) != NS_LDAP_SUCCESS) {
1248e1dd0a2fSth 		(void) snprintf(errmsg,
1249e1dd0a2fSth 		    sizeof (errmsg),
1250e1dd0a2fSth 		    gettext("Can not find the "
1251e1dd0a2fSth 		    "%s DUAProfile\n"),
1252e1dd0a2fSth 		    server->profileName ?
1253e1dd0a2fSth 		    server->profileName : "default");
1254e1dd0a2fSth 		MKERROR(LOG_ERR,
1255e1dd0a2fSth 		    *errorp,
1256e1dd0a2fSth 		    ret_code,
1257e1dd0a2fSth 		    strdup(errmsg),
1258e1dd0a2fSth 		    NS_LDAP_MEMORY);
125929836b19Smichen 		__s_api_conn_user_free(cu);
1260e1dd0a2fSth 		DropConnection(sessionId, NS_LDAP_NEW_CONN);
1261e1dd0a2fSth 		return (ret_code);
1262e1dd0a2fSth 	}
1263e1dd0a2fSth 
1264e1dd0a2fSth 	if (dir_base_dn) {
1265e1dd0a2fSth 		*dir_base_dn = dirBaseDN;
1266e1dd0a2fSth 	} else {
1267e1dd0a2fSth 		free(dirBaseDN);
1268e1dd0a2fSth 	}
1269e1dd0a2fSth 
1270e1dd0a2fSth 	if (dua_profile) {
1271e1dd0a2fSth 		*dua_profile = duaProfile;
1272e1dd0a2fSth 	} else {
1273e1dd0a2fSth 		free(duaProfile);
1274e1dd0a2fSth 	}
1275e1dd0a2fSth 
127629836b19Smichen 	__s_api_conn_user_free(cu);
1277e1dd0a2fSth 	DropConnection(sessionId, NS_LDAP_NEW_CONN);
1278e1dd0a2fSth 
1279e1dd0a2fSth 	return (NS_LDAP_SUCCESS);
1280e1dd0a2fSth }
1281e1dd0a2fSth 
1282e1dd0a2fSth /*
1283e1dd0a2fSth  * This function obtains the root DSE from a specified server.
1284e1dd0a2fSth  *
1285e1dd0a2fSth  * INPUT:
1286e1dd0a2fSth  *     server_addr - an adress of a server to be connected to.
1287e1dd0a2fSth  *
1288e1dd0a2fSth  * OUTPUT:
1289e1dd0a2fSth  *     root_dse - a buffer containing the root DSE in the following format:
1290e1dd0a2fSth  *          [<attribute name>=]value [DOORLINESEP [<attribute name>=]value ]...]
1291e1dd0a2fSth  *        For example: ( here | used as DOORLINESEP for visual purposes)
1292e1dd0a2fSth  *          supportedControl=1.1.1.1|supportedSASLmechanisms=EXTERNAL
1293e1dd0a2fSth  *        Should be free'ed by the caller.
1294e1dd0a2fSth  */
1295e1dd0a2fSth ns_ldap_return_code
__ns_ldap_getRootDSE(const char * server_addr,char ** root_dse,ns_ldap_error_t ** errorp,int anon_fallback)1296e1dd0a2fSth __ns_ldap_getRootDSE(const char *server_addr,
1297e1dd0a2fSth 		char **root_dse,
1298e1dd0a2fSth 		ns_ldap_error_t **errorp,
1299e1dd0a2fSth 		int anon_fallback)
1300e1dd0a2fSth {
1301e1dd0a2fSth 	char			errmsg[MAXERROR];
1302e1dd0a2fSth 	ns_ldap_return_code	ret_code;
1303e1dd0a2fSth 
1304e1dd0a2fSth 	ConnectionID		sessionId = 0;
1305e1dd0a2fSth 	Connection		*session = NULL;
1306e1dd0a2fSth 
1307e1dd0a2fSth 	struct timeval		tv = {NS_DEFAULT_SEARCH_TIMEOUT, 0};
1308e1dd0a2fSth 	char			*attrs[3];
1309e1dd0a2fSth 	int			ldap_rc, ldaperrno = 0;
1310e1dd0a2fSth 	LDAPMessage		*resultMsg = NULL;
1311e1dd0a2fSth 	void			**paramVal = NULL;
1312e1dd0a2fSth 
1313e1dd0a2fSth 	ns_cred_t		anon;
131429836b19Smichen 	ns_conn_user_t		*cu = NULL;
1315e1dd0a2fSth 
1316e1dd0a2fSth 	if (errorp == NULL) {
1317e1dd0a2fSth 		return (NS_LDAP_INVALID_PARAM);
1318e1dd0a2fSth 	}
1319e1dd0a2fSth 
1320e1dd0a2fSth 	*errorp = NULL;
1321e1dd0a2fSth 
1322e1dd0a2fSth 	if (!root_dse) {
1323e1dd0a2fSth 		return (NS_LDAP_INVALID_PARAM);
1324e1dd0a2fSth 	}
1325e1dd0a2fSth 
1326e1dd0a2fSth 	if (!server_addr) {
1327e1dd0a2fSth 		return (NS_LDAP_INVALID_PARAM);
1328e1dd0a2fSth 	}
1329e1dd0a2fSth 
1330e1dd0a2fSth 	__s_api_setInitMode();
1331e1dd0a2fSth 
133229836b19Smichen 	cu = __s_api_conn_user_init(NS_CONN_USER_SEARCH, NULL, B_FALSE);
133329836b19Smichen 	if (cu == NULL) {
133429836b19Smichen 		return (NS_LDAP_INTERNAL);
133529836b19Smichen 	}
133629836b19Smichen 
1337e1dd0a2fSth 	/*
1338e1dd0a2fSth 	 * All the credentials will be taken from the current
1339e1dd0a2fSth 	 * libsldap configuration.
1340e1dd0a2fSth 	 */
1341e1dd0a2fSth 	if ((ret_code = __s_api_getConnection(server_addr,
1342e1dd0a2fSth 	    NS_LDAP_NEW_CONN,
1343e1dd0a2fSth 	    NULL,
1344e1dd0a2fSth 	    &sessionId,
1345e1dd0a2fSth 	    &session,
1346e1dd0a2fSth 	    errorp,
1347e1dd0a2fSth 	    0,
1348e1dd0a2fSth 	    0,
134929836b19Smichen 	    cu)) != NS_LDAP_SUCCESS) {
1350e1dd0a2fSth 		/* Fallback to anonymous mode is disabled. Stop. */
1351e1dd0a2fSth 		if (anon_fallback == 0) {
1352e1dd0a2fSth 			syslog(LOG_WARNING,
1353e1dd0a2fSth 			    gettext("libsldap: can not get the root DSE from "
1354e1dd0a2fSth 			    " the %s server: %s. "
1355e1dd0a2fSth 			    "Falling back to anonymous disabled.\n"),
1356e1dd0a2fSth 			    server_addr,
1357e1dd0a2fSth 			    errorp && *errorp && (*errorp)->message ?
1358e1dd0a2fSth 			    (*errorp)->message : "");
1359e1dd0a2fSth 			if (errorp != NULL && *errorp != NULL) {
1360e1dd0a2fSth 				(void) __ns_ldap_freeError(errorp);
1361e1dd0a2fSth 			}
1362e1dd0a2fSth 			__s_api_unsetInitMode();
1363e1dd0a2fSth 			return (ret_code);
1364e1dd0a2fSth 		}
1365e1dd0a2fSth 
1366e1dd0a2fSth 		/*
1367e1dd0a2fSth 		 * Fallback to anonymous, non-SSL mode for backward
1368e1dd0a2fSth 		 * compatibility reasons. This mode should only be used when
1369e1dd0a2fSth 		 * this function (__ns_ldap_getRootDSE) is called from
1370*bbf21555SRichard Lowe 		 * ldap_cachemgr(8).
1371e1dd0a2fSth 		 */
1372e1dd0a2fSth 		syslog(LOG_WARNING,
1373e1dd0a2fSth 		    gettext("libsldap: Falling back to anonymous, non-SSL"
1374e1dd0a2fSth 		    " mode for __ns_ldap_getRootDSE. %s\n"),
1375e1dd0a2fSth 		    errorp && *errorp && (*errorp)->message ?
1376e1dd0a2fSth 		    (*errorp)->message : "");
1377e1dd0a2fSth 
1378e1dd0a2fSth 		/* Setup the anon credential for anonymous connection. */
1379e1dd0a2fSth 		(void) memset(&anon, 0, sizeof (ns_cred_t));
1380e1dd0a2fSth 		anon.auth.type = NS_LDAP_AUTH_NONE;
1381e1dd0a2fSth 
1382e1dd0a2fSth 		if (*errorp != NULL) {
1383e1dd0a2fSth 			(void) __ns_ldap_freeError(errorp);
1384e1dd0a2fSth 		}
1385e1dd0a2fSth 		*errorp = NULL;
1386e1dd0a2fSth 
1387e1dd0a2fSth 		ret_code = __s_api_getConnection(server_addr,
1388e1dd0a2fSth 		    NS_LDAP_NEW_CONN,
1389e1dd0a2fSth 		    &anon,
1390e1dd0a2fSth 		    &sessionId,
1391e1dd0a2fSth 		    &session,
1392e1dd0a2fSth 		    errorp,
1393e1dd0a2fSth 		    0,
1394e1dd0a2fSth 		    0,
139529836b19Smichen 		    cu);
1396e1dd0a2fSth 
1397e1dd0a2fSth 		if (ret_code != NS_LDAP_SUCCESS) {
139829836b19Smichen 			__s_api_conn_user_free(cu);
1399e1dd0a2fSth 			__s_api_unsetInitMode();
1400e1dd0a2fSth 			return (ret_code);
1401e1dd0a2fSth 		}
1402e1dd0a2fSth 	}
1403e1dd0a2fSth 
1404e1dd0a2fSth 	__s_api_unsetInitMode();
1405e1dd0a2fSth 
1406e1dd0a2fSth 	/* get search timeout value */
1407e1dd0a2fSth 	(void) __ns_ldap_getParam(NS_LDAP_SEARCH_TIME_P, &paramVal, errorp);
1408e1dd0a2fSth 	if (paramVal != NULL && *paramVal != NULL) {
1409e1dd0a2fSth 		tv.tv_sec = **((int **)paramVal);
1410e1dd0a2fSth 		(void) __ns_ldap_freeParam(&paramVal);
1411e1dd0a2fSth 	}
1412e1dd0a2fSth 	if (*errorp != NULL) {
1413e1dd0a2fSth 		(void) __ns_ldap_freeError(errorp);
1414e1dd0a2fSth 	}
1415e1dd0a2fSth 
1416e1dd0a2fSth 	/* Get root DSE from the server specified by the caller. */
1417e1dd0a2fSth 	attrs[0] = "supportedControl";
1418e1dd0a2fSth 	attrs[1] = "supportedsaslmechanisms";
1419e1dd0a2fSth 	attrs[2] = NULL;
1420e1dd0a2fSth 	ldap_rc = ldap_search_ext_s(session->ld,
1421e1dd0a2fSth 	    "",
1422e1dd0a2fSth 	    LDAP_SCOPE_BASE,
1423e1dd0a2fSth 	    "(objectclass=*)",
1424e1dd0a2fSth 	    attrs,
1425e1dd0a2fSth 	    0,
1426e1dd0a2fSth 	    NULL,
1427e1dd0a2fSth 	    NULL,
1428e1dd0a2fSth 	    &tv,
1429e1dd0a2fSth 	    0,
1430e1dd0a2fSth 	    &resultMsg);
1431e1dd0a2fSth 
1432e1dd0a2fSth 	if (ldap_rc != LDAP_SUCCESS) {
1433e1dd0a2fSth 		/*
1434e1dd0a2fSth 		 * If the root DSE was not found, the server does
1435e1dd0a2fSth 		 * not comply with the LDAP v3 protocol.
1436e1dd0a2fSth 		 */
1437e1dd0a2fSth 		(void) ldap_get_option(session->ld,
1438e1dd0a2fSth 		    LDAP_OPT_ERROR_NUMBER,
1439e1dd0a2fSth 		    &ldaperrno);
1440e1dd0a2fSth 		(void) snprintf(errmsg,
1441e1dd0a2fSth 		    sizeof (errmsg),
1442e1dd0a2fSth 		    gettext(ldap_err2string(ldaperrno)));
1443e1dd0a2fSth 		MKERROR(LOG_ERR,
1444e1dd0a2fSth 		    *errorp,
1445e1dd0a2fSth 		    NS_LDAP_OP_FAILED,
1446e1dd0a2fSth 		    strdup(errmsg),
1447e1dd0a2fSth 		    NS_LDAP_MEMORY);
1448e1dd0a2fSth 
1449e1dd0a2fSth 		if (resultMsg) {
1450e1dd0a2fSth 			(void) ldap_msgfree(resultMsg);
1451e1dd0a2fSth 			resultMsg = NULL;
1452e1dd0a2fSth 		}
1453e1dd0a2fSth 
145429836b19Smichen 		__s_api_conn_user_free(cu);
1455b3b48d8eSHans Rosenfeld 		DropConnection(sessionId, NS_LDAP_NEW_CONN);
1456e1dd0a2fSth 		return (NS_LDAP_OP_FAILED);
1457e1dd0a2fSth 	}
145829836b19Smichen 	__s_api_conn_user_free(cu);
1459e1dd0a2fSth 
1460e1dd0a2fSth 	ret_code = convert_to_door_line(session->ld,
1461e1dd0a2fSth 	    resultMsg,
1462e1dd0a2fSth 	    INCLUDE_ATTR_NAMES,
1463e1dd0a2fSth 	    NOT_PROFILE,
1464e1dd0a2fSth 	    root_dse);
1465e1dd0a2fSth 	if (ret_code == NS_LDAP_NOTFOUND) {
1466e1dd0a2fSth 		(void) snprintf(errmsg,
1467e1dd0a2fSth 		    sizeof (errmsg),
1468e1dd0a2fSth 		    gettext("No root DSE data "
1469e1dd0a2fSth 		    "for server %s returned."),
1470e1dd0a2fSth 		    server_addr);
1471e1dd0a2fSth 		MKERROR(LOG_ERR,
1472e1dd0a2fSth 		    *errorp,
1473e1dd0a2fSth 		    NS_LDAP_NOTFOUND,
1474e1dd0a2fSth 		    strdup(errmsg),
1475e1dd0a2fSth 		    NS_LDAP_MEMORY);
1476e1dd0a2fSth 	}
1477e1dd0a2fSth 
1478e1dd0a2fSth 	if (resultMsg) {
1479e1dd0a2fSth 		(void) ldap_msgfree(resultMsg);
1480e1dd0a2fSth 		resultMsg = NULL;
1481e1dd0a2fSth 	}
1482e1dd0a2fSth 
1483e1dd0a2fSth 	DropConnection(sessionId, NS_LDAP_NEW_CONN);
1484e1dd0a2fSth 
1485e1dd0a2fSth 	return (ret_code);
1486e1dd0a2fSth }
1487e1dd0a2fSth 
1488e1dd0a2fSth /*
1489e1dd0a2fSth  * This function destroys the local list of root DSEs. The input parameter is
1490e1dd0a2fSth  * a pointer to the list to be erased.
1491e1dd0a2fSth  * The type of the pointer passed to this function should be
1492e1dd0a2fSth  * (dir_server_list_t *).
1493e1dd0a2fSth  */
1494e1dd0a2fSth static
1495e1dd0a2fSth void *
disposeOfOldList(void * param)1496e1dd0a2fSth disposeOfOldList(void *param)
1497e1dd0a2fSth {
1498e1dd0a2fSth 	dir_server_list_t	*old_list = (dir_server_list_t *)param;
1499e1dd0a2fSth 	long			i = 0, j;
1500e1dd0a2fSth 
1501e1dd0a2fSth 	(void) rw_wrlock(&old_list->listDestroyLock);
1502e1dd0a2fSth 	/* Destroy the old list */
1503e1dd0a2fSth 	while (old_list->nsServers[i]) {
1504e1dd0a2fSth 		free(old_list->nsServers[i]->ip);
1505e1dd0a2fSth 		j = 0;
1506e1dd0a2fSth 		while (old_list->nsServers[i]->controls &&
1507e1dd0a2fSth 		    old_list->nsServers[i]->controls[j]) {
1508e1dd0a2fSth 			free(old_list->nsServers[i]->controls[j]);
1509e1dd0a2fSth 			++j;
1510e1dd0a2fSth 		}
1511e1dd0a2fSth 		free(old_list->nsServers[i]->controls);
1512e1dd0a2fSth 		j = 0;
1513e1dd0a2fSth 		while (old_list->nsServers[i]->saslMech &&
1514e1dd0a2fSth 		    old_list->nsServers[i]->saslMech[j]) {
1515e1dd0a2fSth 			free(old_list->nsServers[i]->saslMech[j]);
1516e1dd0a2fSth 			++j;
1517e1dd0a2fSth 		}
1518e1dd0a2fSth 		free(old_list->nsServers[i]->saslMech);
1519e1dd0a2fSth 		++i;
1520e1dd0a2fSth 	}
1521e1dd0a2fSth 	/*
1522e1dd0a2fSth 	 * All the structures pointed by old_list->nsServers were allocated
1523e1dd0a2fSth 	 * in one chunck. The nsServers[0] pointer points to the beginning
1524e1dd0a2fSth 	 * of that chunck.
1525e1dd0a2fSth 	 */
1526e1dd0a2fSth 	free(old_list->nsServers[0]);
1527e1dd0a2fSth 	free(old_list->nsServers);
1528e1dd0a2fSth 	(void) rw_unlock(&old_list->listDestroyLock);
1529e1dd0a2fSth 	(void) rwlock_destroy(&old_list->listDestroyLock);
1530e1dd0a2fSth 	free(old_list);
1531e1dd0a2fSth 
1532e1dd0a2fSth 	return (NULL);
1533e1dd0a2fSth }
1534e1dd0a2fSth 
1535e1dd0a2fSth /*
1536e1dd0a2fSth  * This function cancels the Standalone mode and destroys the list of root DSEs.
1537e1dd0a2fSth  */
1538e1dd0a2fSth void
__ns_ldap_cancelStandalone(void)1539e1dd0a2fSth __ns_ldap_cancelStandalone(void)
1540e1dd0a2fSth {
1541e1dd0a2fSth 	dir_server_list_t	*old_list;
1542e1dd0a2fSth 
1543e1dd0a2fSth 	(void) mutex_lock(&dir_servers.listReplaceLock);
1544e1dd0a2fSth 	dir_servers.standalone = 0;
1545e1dd0a2fSth 	if (!dir_servers.list) {
1546e1dd0a2fSth 		(void) mutex_unlock(&dir_servers.listReplaceLock);
1547e1dd0a2fSth 		return;
1548e1dd0a2fSth 	}
1549e1dd0a2fSth 	old_list = dir_servers.list;
1550e1dd0a2fSth 	dir_servers.list = NULL;
1551e1dd0a2fSth 	(void) mutex_unlock(&dir_servers.listReplaceLock);
1552e1dd0a2fSth 
1553e1dd0a2fSth 	(void) disposeOfOldList(old_list);
1554e1dd0a2fSth }
1555e1dd0a2fSth 
1556e1dd0a2fSth 
1557e1dd0a2fSth static
1558e1dd0a2fSth void*
create_ns_servers_entry(void * param)1559e1dd0a2fSth create_ns_servers_entry(void *param)
1560e1dd0a2fSth {
1561e1dd0a2fSth #define	CHUNK_SIZE 16
1562e1dd0a2fSth 
1563e1dd0a2fSth 	dir_server_t		*server = (dir_server_t *)param;
1564e1dd0a2fSth 	ns_ldap_return_code	*retCode = calloc(1,
1565e1dd0a2fSth 	    sizeof (ns_ldap_return_code));
1566e1dd0a2fSth 	uint32_t		sc_counter = 0, sm_counter = 0;
1567e1dd0a2fSth 	uint32_t		sc_mem_blocks = 1, sm_mem_blocks = 1;
1568e1dd0a2fSth 	char			*rootDSE = NULL, *attr, *val, *rest, **ptr;
1569e1dd0a2fSth 	ns_ldap_error_t		*error = NULL;
1570e1dd0a2fSth 
1571e1dd0a2fSth 	if (retCode == NULL) {
1572e1dd0a2fSth 		return (NULL);
1573e1dd0a2fSth 	}
1574e1dd0a2fSth 
1575e1dd0a2fSth 	/*
1576e1dd0a2fSth 	 * We call this function in non anon-fallback mode because we
1577e1dd0a2fSth 	 * want the whole procedure to fail as soon as possible to
1578e1dd0a2fSth 	 * indicate there are problems with connecting to the server.
1579e1dd0a2fSth 	 */
1580e1dd0a2fSth 	*retCode = __ns_ldap_getRootDSE(server->ip,
1581e1dd0a2fSth 	    &rootDSE,
1582e1dd0a2fSth 	    &error,
1583e1dd0a2fSth 	    SA_ALLOW_FALLBACK);
1584e1dd0a2fSth 
1585e1dd0a2fSth 	if (*retCode == NS_LDAP_MEMORY) {
1586e1dd0a2fSth 		free(retCode);
1587e1dd0a2fSth 		return (NULL);
1588e1dd0a2fSth 	}
1589e1dd0a2fSth 
1590e1dd0a2fSth 	/*
1591e1dd0a2fSth 	 * If the root DSE can not be obtained, log an error and keep the
1592e1dd0a2fSth 	 * server.
1593e1dd0a2fSth 	 */
1594e1dd0a2fSth 	if (*retCode != NS_LDAP_SUCCESS) {
1595e1dd0a2fSth 		server->status = INFO_SERVER_ERROR;
1596e1dd0a2fSth 		syslog(LOG_WARNING,
1597e1dd0a2fSth 		    gettext("libsldap (\"standalone\" mode): "
1598e1dd0a2fSth 		    "can not obtain the root DSE from %s. %s"),
1599e1dd0a2fSth 		    server->ip,
1600e1dd0a2fSth 		    error && error->message ? error->message : "");
1601e1dd0a2fSth 		if (error) {
1602e1dd0a2fSth 			(void) __ns_ldap_freeError(&error);
1603e1dd0a2fSth 		}
1604e1dd0a2fSth 		return (retCode);
1605e1dd0a2fSth 	}
1606e1dd0a2fSth 
1607e1dd0a2fSth 	/* Get the first attribute of the root DSE. */
1608e1dd0a2fSth 	attr = strtok_r(rootDSE, DOORLINESEP, &rest);
1609e1dd0a2fSth 	if (attr == NULL) {
1610e1dd0a2fSth 		free(rootDSE);
1611e1dd0a2fSth 		server->status = INFO_SERVER_ERROR;
1612e1dd0a2fSth 		syslog(LOG_WARNING,
1613e1dd0a2fSth 		    gettext("libsldap (\"standalone\" mode): "
1614e1dd0a2fSth 		    "the root DSE from %s is empty or corrupted."),
1615e1dd0a2fSth 		    server->ip);
1616e1dd0a2fSth 		*retCode = NS_LDAP_INTERNAL;
1617e1dd0a2fSth 		return (retCode);
1618e1dd0a2fSth 	}
1619e1dd0a2fSth 
1620e1dd0a2fSth 	server->controls = (char **)calloc(CHUNK_SIZE, sizeof (char *));
1621e1dd0a2fSth 	server->saslMech = (char **)calloc(CHUNK_SIZE, sizeof (char *));
1622e1dd0a2fSth 	if (server->controls == NULL || server->saslMech == NULL) {
1623e1dd0a2fSth 		free(rootDSE);
1624e1dd0a2fSth 		free(retCode);
1625e1dd0a2fSth 		return (NULL);
1626e1dd0a2fSth 	}
1627e1dd0a2fSth 
1628e1dd0a2fSth 	do {
1629e1dd0a2fSth 		if ((val = strchr(attr, '=')) == NULL) {
1630e1dd0a2fSth 			continue;
1631e1dd0a2fSth 		}
1632e1dd0a2fSth 		++val;
1633e1dd0a2fSth 
1634e1dd0a2fSth 		if (strncasecmp(attr,
1635e1dd0a2fSth 		    _SASLMECHANISM,
1636e1dd0a2fSth 		    _SASLMECHANISM_LEN) == 0) {
1637e1dd0a2fSth 			if (sm_counter == CHUNK_SIZE * sm_mem_blocks - 1) {
1638e1dd0a2fSth 				ptr = (char **)realloc(server->saslMech,
1639e1dd0a2fSth 				    CHUNK_SIZE *
1640e1dd0a2fSth 				    ++sm_mem_blocks *
1641e1dd0a2fSth 				    sizeof (char *));
1642e1dd0a2fSth 				if (ptr == NULL) {
1643e1dd0a2fSth 					*retCode = NS_LDAP_MEMORY;
1644e1dd0a2fSth 					break;
1645e1dd0a2fSth 				}
1646e1dd0a2fSth 				bzero((char *)ptr +
1647e1dd0a2fSth 				    (sm_counter + 1) *
1648e1dd0a2fSth 				    sizeof (char *),
1649e1dd0a2fSth 				    CHUNK_SIZE *
1650e1dd0a2fSth 				    sm_mem_blocks *
1651e1dd0a2fSth 				    sizeof (char *) -
1652e1dd0a2fSth 				    (sm_counter + 1) *
1653e1dd0a2fSth 				    sizeof (char *));
1654e1dd0a2fSth 				server->saslMech = ptr;
1655e1dd0a2fSth 			}
1656e1dd0a2fSth 			server->saslMech[sm_counter] = strdup(val);
1657e1dd0a2fSth 			if (server->saslMech[sm_counter] == NULL) {
1658e1dd0a2fSth 				*retCode = NS_LDAP_MEMORY;
1659e1dd0a2fSth 				break;
1660e1dd0a2fSth 			}
1661e1dd0a2fSth 			++sm_counter;
1662e1dd0a2fSth 			continue;
1663e1dd0a2fSth 		}
1664e1dd0a2fSth 		if (strncasecmp(attr,
1665e1dd0a2fSth 		    _SUPPORTEDCONTROL,
1666e1dd0a2fSth 		    _SUPPORTEDCONTROL_LEN) == 0) {
1667e1dd0a2fSth 			if (sc_counter == CHUNK_SIZE * sc_mem_blocks - 1) {
1668e1dd0a2fSth 				ptr = (char **)realloc(server->controls,
1669e1dd0a2fSth 				    CHUNK_SIZE *
1670e1dd0a2fSth 				    ++sc_mem_blocks *
1671e1dd0a2fSth 				    sizeof (char *));
1672e1dd0a2fSth 				if (ptr == NULL) {
1673e1dd0a2fSth 					*retCode = NS_LDAP_MEMORY;
1674e1dd0a2fSth 					break;
1675e1dd0a2fSth 				}
1676e1dd0a2fSth 				bzero((char *)ptr +
1677e1dd0a2fSth 				    (sc_counter + 1) *
1678e1dd0a2fSth 				    sizeof (char *),
1679e1dd0a2fSth 				    CHUNK_SIZE *
1680e1dd0a2fSth 				    sc_mem_blocks *
1681e1dd0a2fSth 				    sizeof (char *) -
1682e1dd0a2fSth 				    (sc_counter + 1) *
1683e1dd0a2fSth 				    sizeof (char *));
1684e1dd0a2fSth 				server->controls = ptr;
1685e1dd0a2fSth 			}
1686e1dd0a2fSth 
1687e1dd0a2fSth 			server->controls[sc_counter] = strdup(val);
1688e1dd0a2fSth 			if (server->controls[sc_counter] == NULL) {
1689e1dd0a2fSth 				*retCode = NS_LDAP_MEMORY;
1690e1dd0a2fSth 				break;
1691e1dd0a2fSth 			}
1692e1dd0a2fSth 			++sc_counter;
1693e1dd0a2fSth 			continue;
1694e1dd0a2fSth 		}
1695e1dd0a2fSth 
1696e1dd0a2fSth 	} while (attr = strtok_r(NULL, DOORLINESEP, &rest));
1697e1dd0a2fSth 
1698e1dd0a2fSth 	free(rootDSE);
1699e1dd0a2fSth 
1700e1dd0a2fSth 	if (*retCode == NS_LDAP_MEMORY) {
1701e1dd0a2fSth 		free(retCode);
1702e1dd0a2fSth 		return (NULL);
1703e1dd0a2fSth 	}
1704e1dd0a2fSth 
1705e1dd0a2fSth 	server->controls[sc_counter] = NULL;
1706e1dd0a2fSth 	server->saslMech[sm_counter] = NULL;
1707e1dd0a2fSth 
1708e1dd0a2fSth 	server->status = INFO_SERVER_UP;
1709e1dd0a2fSth 
1710e1dd0a2fSth 	return (retCode);
1711e1dd0a2fSth #undef CHUNK_SIZE
1712e1dd0a2fSth }
1713e1dd0a2fSth 
1714e1dd0a2fSth 
1715e1dd0a2fSth /*
1716e1dd0a2fSth  * This function creates a new local list of root DSEs from all the servers
1717e1dd0a2fSth  * mentioned in the DUAProfile (or local NS BEC) and returns
1718e1dd0a2fSth  * a pointer to the list.
1719e1dd0a2fSth  */
1720e1dd0a2fSth static
1721e1dd0a2fSth ns_ldap_return_code
createDirServerList(dir_server_list_t ** new_list,ns_ldap_error_t ** errorp)1722e1dd0a2fSth createDirServerList(dir_server_list_t **new_list,
1723e1dd0a2fSth 		ns_ldap_error_t **errorp)
1724e1dd0a2fSth {
1725e1dd0a2fSth 	char			**serverList;
1726e1dd0a2fSth 	ns_ldap_return_code	retCode = NS_LDAP_SUCCESS;
1727e1dd0a2fSth 	dir_server_t		*tmpSrvArray;
1728e1dd0a2fSth 	long			srvListLength, i;
1729e1dd0a2fSth 	thread_t		*thrPool, thrID;
1730e1dd0a2fSth 	void			*status = NULL;
1731e1dd0a2fSth 
1732e1dd0a2fSth 	if (errorp == NULL) {
1733e1dd0a2fSth 		return (NS_LDAP_INVALID_PARAM);
1734e1dd0a2fSth 	}
1735e1dd0a2fSth 
1736e1dd0a2fSth 	*errorp = NULL;
1737e1dd0a2fSth 
1738e1dd0a2fSth 	if (new_list == NULL) {
1739e1dd0a2fSth 		return (NS_LDAP_INVALID_PARAM);
1740e1dd0a2fSth 	}
1741e1dd0a2fSth 
1742e1dd0a2fSth 	retCode = __s_api_getServers(&serverList, errorp);
1743e1dd0a2fSth 	if (retCode != NS_LDAP_SUCCESS || serverList == NULL) {
1744e1dd0a2fSth 		return (retCode);
1745e1dd0a2fSth 	}
1746e1dd0a2fSth 
1747e1dd0a2fSth 	for (i = 0; serverList[i]; ++i) {
1748e1dd0a2fSth 		;
1749e1dd0a2fSth 	}
1750e1dd0a2fSth 	srvListLength = i;
1751e1dd0a2fSth 
1752e1dd0a2fSth 	thrPool = calloc(srvListLength, sizeof (thread_t));
1753e1dd0a2fSth 	if (thrPool == NULL) {
1754e1dd0a2fSth 		__s_api_free2dArray(serverList);
1755e1dd0a2fSth 		return (NS_LDAP_MEMORY);
1756e1dd0a2fSth 	}
1757e1dd0a2fSth 
1758e1dd0a2fSth 	*new_list = (dir_server_list_t *)calloc(1,
1759e1dd0a2fSth 	    sizeof (dir_server_list_t));
1760e1dd0a2fSth 	if (*new_list == NULL) {
1761e1dd0a2fSth 		__s_api_free2dArray(serverList);
1762e1dd0a2fSth 		free(thrPool);
1763e1dd0a2fSth 		return (NS_LDAP_MEMORY);
1764e1dd0a2fSth 	}
1765e1dd0a2fSth 	(void) rwlock_init(&(*new_list)->listDestroyLock, USYNC_THREAD, NULL);
1766e1dd0a2fSth 
1767e1dd0a2fSth 	(*new_list)->nsServers = (dir_server_t **)calloc(srvListLength + 1,
1768e1dd0a2fSth 	    sizeof (dir_server_t *));
1769e1dd0a2fSth 	if ((*new_list)->nsServers == NULL) {
1770e1dd0a2fSth 		free(*new_list);
1771e1dd0a2fSth 		*new_list = NULL;
1772e1dd0a2fSth 		__s_api_free2dArray(serverList);
1773e1dd0a2fSth 		free(thrPool);
1774e1dd0a2fSth 		return (NS_LDAP_MEMORY);
1775e1dd0a2fSth 	}
1776e1dd0a2fSth 
1777e1dd0a2fSth 	/*
1778e1dd0a2fSth 	 * Allocate a set of dir_server_t structures as an array,
1779e1dd0a2fSth 	 * with one alloc call and then initialize the nsServers pointers
1780e1dd0a2fSth 	 * with the addresses of the array's members.
1781e1dd0a2fSth 	 */
1782e1dd0a2fSth 	tmpSrvArray = (dir_server_t *)calloc(srvListLength,
1783e1dd0a2fSth 	    sizeof (dir_server_t));
1784e1dd0a2fSth 	for (i = 0; i < srvListLength; ++i) {
1785e1dd0a2fSth 		(*new_list)->nsServers[i] = &tmpSrvArray[i];
1786e1dd0a2fSth 
1787e1dd0a2fSth 		(*new_list)->nsServers[i]->info = INFO_STATUS_NEW;
1788e1dd0a2fSth 		(void) mutex_init(&(*new_list)->nsServers[i]->updateStatus,
1789e1dd0a2fSth 		    USYNC_THREAD,
1790e1dd0a2fSth 		    NULL);
1791e1dd0a2fSth 
1792e1dd0a2fSth 		(*new_list)->nsServers[i]->ip = strdup(serverList[i]);
1793e1dd0a2fSth 		if ((*new_list)->nsServers[i]->ip == NULL) {
1794e1dd0a2fSth 			retCode = NS_LDAP_MEMORY;
1795e1dd0a2fSth 			break;
1796e1dd0a2fSth 		}
1797e1dd0a2fSth 
1798e1dd0a2fSth 		(*new_list)->nsServers[i]->status = INFO_SERVER_CONNECTING;
1799e1dd0a2fSth 
1800e1dd0a2fSth 		switch (thr_create(NULL,
1801e1dd0a2fSth 		    0,
1802e1dd0a2fSth 		    create_ns_servers_entry,
1803e1dd0a2fSth 		    (*new_list)->nsServers[i],
1804e1dd0a2fSth 		    0,
1805e1dd0a2fSth 		    &thrID)) {
1806e1dd0a2fSth 		case EAGAIN:
1807e1dd0a2fSth 			(*new_list)->nsServers[i]->status =
1808e1dd0a2fSth 			    INFO_SERVER_ERROR;
1809e1dd0a2fSth 			continue;
1810e1dd0a2fSth 		case ENOMEM:
1811e1dd0a2fSth 			(*new_list)->nsServers[i]->status =
1812e1dd0a2fSth 			    INFO_SERVER_ERROR;
1813e1dd0a2fSth 			continue;
1814e1dd0a2fSth 		default:
1815e1dd0a2fSth 			thrPool[i] = thrID;
1816e1dd0a2fSth 			continue;
1817e1dd0a2fSth 		}
1818e1dd0a2fSth 	}
1819e1dd0a2fSth 
1820e1dd0a2fSth 	for (i = 0; i < srvListLength; ++i) {
1821e1dd0a2fSth 		if (thrPool[i] != 0 &&
1822e1dd0a2fSth 		    thr_join(thrPool[i], NULL, &status) == 0) {
1823e1dd0a2fSth 			if (status == NULL) {
1824e1dd0a2fSth 				/*
1825e1dd0a2fSth 				 * Some memory allocation problems occured. Just
1826e1dd0a2fSth 				 * ignore the server and hope there will be some
1827e1dd0a2fSth 				 * other good ones.
1828e1dd0a2fSth 				 */
1829e1dd0a2fSth 				(*new_list)->nsServers[i]->status =
1830e1dd0a2fSth 				    INFO_SERVER_ERROR;
1831e1dd0a2fSth 			}
1832e1dd0a2fSth 			free(status);
1833e1dd0a2fSth 		}
1834e1dd0a2fSth 	}
1835e1dd0a2fSth 
1836e1dd0a2fSth 	__s_api_free2dArray(serverList);
1837e1dd0a2fSth 	free(thrPool);
1838e1dd0a2fSth 
1839e1dd0a2fSth 	if (retCode == NS_LDAP_MEMORY) {
1840e1dd0a2fSth 		(void) disposeOfOldList(*new_list);
1841e1dd0a2fSth 		return (NS_LDAP_MEMORY);
1842e1dd0a2fSth 	}
1843e1dd0a2fSth 
1844e1dd0a2fSth 	return (NS_LDAP_SUCCESS);
1845e1dd0a2fSth }
1846e1dd0a2fSth 
1847e1dd0a2fSth /*
1848e1dd0a2fSth  * This functions replaces the local list of root DSEs with a new one and starts
1849e1dd0a2fSth  * a thread destroying the old list. There is no need for other threads to wait
1850e1dd0a2fSth  * until the old list will be destroyed.
1851e1dd0a2fSth  * Since it is possible that more than one thread can start creating the list,
1852e1dd0a2fSth  * this function should be protected by mutexes to be sure that only one thread
1853e1dd0a2fSth  * performs the initialization.
1854e1dd0a2fSth  */
1855e1dd0a2fSth static
1856e1dd0a2fSth ns_ldap_return_code
initGlobalList(ns_ldap_error_t ** error)1857e1dd0a2fSth initGlobalList(ns_ldap_error_t **error)
1858e1dd0a2fSth {
1859e1dd0a2fSth 	dir_server_list_t	*new_list, *old_list;
1860e1dd0a2fSth 	ns_ldap_return_code	ret_code;
1861e1dd0a2fSth 	thread_t		tid;
1862e1dd0a2fSth 
1863e1dd0a2fSth 	ret_code = createDirServerList(&new_list, error);
1864e1dd0a2fSth 	if (ret_code != NS_LDAP_SUCCESS) {
1865e1dd0a2fSth 		return (ret_code);
1866e1dd0a2fSth 	}
1867e1dd0a2fSth 
1868e1dd0a2fSth 	old_list = dir_servers.list;
1869e1dd0a2fSth 	dir_servers.list = new_list;
1870e1dd0a2fSth 
1871e1dd0a2fSth 	if (old_list) {
1872e1dd0a2fSth 		(void) thr_create(NULL,
1873e1dd0a2fSth 		    0,
1874e1dd0a2fSth 		    disposeOfOldList,
1875e1dd0a2fSth 		    old_list,
1876e1dd0a2fSth 		    THR_DETACHED,
1877e1dd0a2fSth 		    &tid);
1878e1dd0a2fSth 	}
1879e1dd0a2fSth 
1880e1dd0a2fSth 	return (NS_LDAP_SUCCESS);
1881e1dd0a2fSth }
1882e1dd0a2fSth 
1883e1dd0a2fSth static
1884e1dd0a2fSth struct {
1885e1dd0a2fSth 	char *authMech;
1886e1dd0a2fSth 	ns_auth_t auth;
1887e1dd0a2fSth } authArray[] = {{"none", {NS_LDAP_AUTH_NONE,
1888e1dd0a2fSth 			NS_LDAP_TLS_NONE,
1889e1dd0a2fSth 			NS_LDAP_SASL_NONE,
1890e1dd0a2fSth 			NS_LDAP_SASLOPT_NONE}},
1891e1dd0a2fSth 		{"simple", {NS_LDAP_AUTH_SIMPLE,
1892e1dd0a2fSth 			NS_LDAP_TLS_NONE,
1893e1dd0a2fSth 			NS_LDAP_SASL_NONE,
1894e1dd0a2fSth 			NS_LDAP_SASLOPT_NONE}},
1895e1dd0a2fSth 		{"tls:simple", {NS_LDAP_AUTH_TLS,
1896e1dd0a2fSth 			NS_LDAP_TLS_SIMPLE,
1897e1dd0a2fSth 			NS_LDAP_SASL_NONE,
1898e1dd0a2fSth 			NS_LDAP_SASLOPT_NONE}},
1899e1dd0a2fSth 		{"tls:sasl/CRAM-MD5", {NS_LDAP_AUTH_TLS,
1900e1dd0a2fSth 			NS_LDAP_TLS_SASL,
1901e1dd0a2fSth 			NS_LDAP_SASL_CRAM_MD5,
1902e1dd0a2fSth 			NS_LDAP_SASLOPT_NONE}},
1903e1dd0a2fSth 		{"tls:sasl/DIGEST-MD5", {NS_LDAP_AUTH_TLS,
1904e1dd0a2fSth 			NS_LDAP_TLS_SASL,
1905e1dd0a2fSth 			NS_LDAP_SASL_DIGEST_MD5,
1906e1dd0a2fSth 			NS_LDAP_SASLOPT_NONE}},
1907e1dd0a2fSth 		{"sasl/CRAM-MD5", {NS_LDAP_AUTH_SASL,
1908e1dd0a2fSth 			NS_LDAP_TLS_SASL,
1909e1dd0a2fSth 			NS_LDAP_SASL_CRAM_MD5,
1910e1dd0a2fSth 			NS_LDAP_SASLOPT_NONE}},
1911e1dd0a2fSth 		{"sasl/DIGEST-MD5", {NS_LDAP_AUTH_SASL,
1912e1dd0a2fSth 			NS_LDAP_TLS_SASL,
1913e1dd0a2fSth 			NS_LDAP_SASL_DIGEST_MD5,
1914e1dd0a2fSth 			NS_LDAP_SASLOPT_NONE}},
1915e1dd0a2fSth 		{"sasl/GSSAPI", {NS_LDAP_AUTH_SASL,
1916e1dd0a2fSth 			NS_LDAP_TLS_SASL,
1917e1dd0a2fSth 			NS_LDAP_SASL_GSSAPI,
1918e1dd0a2fSth 			NS_LDAP_SASLOPT_PRIV | NS_LDAP_SASLOPT_INT}},
1919e1dd0a2fSth 		{NULL, {NS_LDAP_AUTH_NONE,
1920e1dd0a2fSth 			NS_LDAP_TLS_NONE,
1921e1dd0a2fSth 			NS_LDAP_SASL_NONE,
1922e1dd0a2fSth 			NS_LDAP_SASLOPT_NONE}}};
1923e1dd0a2fSth 
1924e1dd0a2fSth ns_ldap_return_code
__ns_ldap_initAuth(const char * auth_mech,ns_auth_t * auth,ns_ldap_error_t ** errorp)1925e1dd0a2fSth __ns_ldap_initAuth(const char *auth_mech,
1926e1dd0a2fSth 		ns_auth_t *auth,
1927e1dd0a2fSth 		ns_ldap_error_t **errorp)
1928e1dd0a2fSth {
1929e1dd0a2fSth 	uint32_t	i;
1930e1dd0a2fSth 	char		errmsg[MAXERROR];
1931e1dd0a2fSth 
1932e1dd0a2fSth 	if (auth_mech == NULL) {
1933e1dd0a2fSth 		(void) snprintf(errmsg,
1934e1dd0a2fSth 		    sizeof (errmsg),
1935e1dd0a2fSth 		    gettext("Invalid authentication method specified\n"));
1936e1dd0a2fSth 		MKERROR(LOG_WARNING,
1937e1dd0a2fSth 		    *errorp,
1938e1dd0a2fSth 		    NS_LDAP_INTERNAL,
1939e1dd0a2fSth 		    strdup(errmsg),
1940e1dd0a2fSth 		    NS_LDAP_MEMORY);
1941e1dd0a2fSth 		return (NS_LDAP_INTERNAL);
1942e1dd0a2fSth 	}
1943e1dd0a2fSth 
1944e1dd0a2fSth 	for (i = 0; authArray[i].authMech != NULL; ++i) {
1945e1dd0a2fSth 		if (strcasecmp(auth_mech, authArray[i].authMech) == 0) {
1946e1dd0a2fSth 			*auth = authArray[i].auth;
1947e1dd0a2fSth 			return (NS_LDAP_SUCCESS);
1948e1dd0a2fSth 		}
1949e1dd0a2fSth 	}
1950e1dd0a2fSth 
1951e1dd0a2fSth 	(void) snprintf(errmsg,
1952e1dd0a2fSth 	    sizeof (errmsg),
1953e1dd0a2fSth 	    gettext("Invalid authentication method specified\n"));
1954e1dd0a2fSth 	MKERROR(LOG_WARNING,
1955e1dd0a2fSth 	    *errorp,
1956e1dd0a2fSth 	    NS_LDAP_INTERNAL,
1957e1dd0a2fSth 	    strdup(errmsg),
1958e1dd0a2fSth 	    NS_LDAP_MEMORY);
1959e1dd0a2fSth 	return (NS_LDAP_INTERNAL);
1960e1dd0a2fSth }
1961e1dd0a2fSth 
1962e1dd0a2fSth /*
1963e1dd0a2fSth  * This function "informs" libsldap that a client application has specified
1964e1dd0a2fSth  * a directory to use. The function obtains a DUAProfile, credentials,
1965e1dd0a2fSth  * and naming context. During all further operations on behalf
1966e1dd0a2fSth  * of the application requested a standalone schema libsldap will use
1967e1dd0a2fSth  * the information obtained by __ns_ldap_initStandalone() instead of
1968*bbf21555SRichard Lowe  * door_call(3C)ing ldap_cachemgr(8).
1969e1dd0a2fSth  *
1970e1dd0a2fSth  * INPUT:
1971e1dd0a2fSth  *     sa_conf - a structure describing where and in which way to obtain all
1972e1dd0a2fSth  *               the configuration describing how to communicate to
1973e1dd0a2fSth  *               a choosen LDAP directory,
1974e1dd0a2fSth  *     errorp - an error object describing an error occured.
1975e1dd0a2fSth  */
1976e1dd0a2fSth ns_ldap_return_code
__ns_ldap_initStandalone(const ns_standalone_conf_t * sa_conf,ns_ldap_error_t ** errorp)1977e1dd0a2fSth __ns_ldap_initStandalone(const ns_standalone_conf_t *sa_conf,
1978e1dd0a2fSth 			ns_ldap_error_t	**errorp) {
1979e1dd0a2fSth 
1980e1dd0a2fSth 	ns_cred_t	user_cred = {{NS_LDAP_AUTH_NONE,
1981e1dd0a2fSth 					NS_LDAP_TLS_NONE,
1982e1dd0a2fSth 					NS_LDAP_SASL_NONE,
1983e1dd0a2fSth 					NS_LDAP_SASLOPT_NONE},
1984e1dd0a2fSth 					NULL,
1985e1dd0a2fSth 					{NULL, NULL}};
1986e1dd0a2fSth 	char		*dua_profile = NULL;
1987e1dd0a2fSth 	char		errmsg[MAXERROR];
1988e1dd0a2fSth 	ns_config_t 	*cfg;
1989e1dd0a2fSth 	int		ret_code;
1990e1dd0a2fSth 
1991e1dd0a2fSth 	if (sa_conf->SA_BIND_DN == NULL && sa_conf->SA_BIND_PWD != NULL ||
1992e1dd0a2fSth 	    sa_conf->SA_BIND_DN != NULL && sa_conf->SA_BIND_PWD == NULL) {
1993e1dd0a2fSth 		(void) snprintf(errmsg,
1994e1dd0a2fSth 		    sizeof (errmsg),
1995e1dd0a2fSth 		    gettext("Bind DN and bind password"
1996e1dd0a2fSth 		    " must both be provided\n"));
1997e1dd0a2fSth 		MKERROR(LOG_ERR,
1998e1dd0a2fSth 		    *errorp,
1999e1dd0a2fSth 		    NS_CONFIG_NOTLOADED,
2000e1dd0a2fSth 		    strdup(errmsg),
2001e1dd0a2fSth 		    NS_LDAP_MEMORY);
2002e1dd0a2fSth 		return (NS_LDAP_INTERNAL);
2003e1dd0a2fSth 	}
2004e1dd0a2fSth 
2005e1dd0a2fSth 	switch (sa_conf->type) {
2006e1dd0a2fSth 	case NS_LDAP_SERVER:
2007e1dd0a2fSth 		if (sa_conf->SA_BIND_DN != NULL) {
2008e1dd0a2fSth 			user_cred.cred.unix_cred.userID = sa_conf->SA_BIND_DN;
2009e1dd0a2fSth 			user_cred.auth.type = NS_LDAP_AUTH_SIMPLE;
2010e1dd0a2fSth 		}
2011e1dd0a2fSth 
2012e1dd0a2fSth 		if (sa_conf->SA_BIND_PWD != NULL) {
2013e1dd0a2fSth 			user_cred.cred.unix_cred.passwd = sa_conf->SA_BIND_PWD;
2014e1dd0a2fSth 		}
2015e1dd0a2fSth 
2016e1dd0a2fSth 		if (sa_conf->SA_AUTH != NULL) {
2017e1dd0a2fSth 			user_cred.auth.type = sa_conf->SA_AUTH->type;
2018e1dd0a2fSth 			user_cred.auth.tlstype = sa_conf->SA_AUTH->tlstype;
2019e1dd0a2fSth 			user_cred.auth.saslmech = sa_conf->SA_AUTH->saslmech;
2020e1dd0a2fSth 			user_cred.auth.saslopt = sa_conf->SA_AUTH->saslopt;
2021e1dd0a2fSth 		}
2022e1dd0a2fSth 
2023e1dd0a2fSth 		if (sa_conf->SA_CERT_PATH != NULL) {
2024e1dd0a2fSth 			user_cred.hostcertpath = sa_conf->SA_CERT_PATH;
2025e1dd0a2fSth 		}
2026e1dd0a2fSth 
2027e1dd0a2fSth 		ret_code = __ns_ldap_getConnectionInfoFromDUA(
2028e1dd0a2fSth 		    &sa_conf->ds_profile.server,
2029e1dd0a2fSth 		    &user_cred,
2030e1dd0a2fSth 		    &dua_profile,
2031e1dd0a2fSth 		    NULL,
2032e1dd0a2fSth 		    errorp);
2033e1dd0a2fSth 		if (ret_code != NS_LDAP_SUCCESS) {
2034e1dd0a2fSth 			return (ret_code);
2035e1dd0a2fSth 		}
2036e1dd0a2fSth 
2037e1dd0a2fSth 		cfg = __s_api_create_config_door_str(dua_profile, errorp);
2038e1dd0a2fSth 		if (cfg == NULL) {
2039e1dd0a2fSth 			free(dua_profile);
2040e1dd0a2fSth 			return (NS_LDAP_CONFIG);
2041e1dd0a2fSth 		}
2042e1dd0a2fSth 
2043e1dd0a2fSth 		if (sa_conf->SA_CERT_PATH != NULL) {
2044e1dd0a2fSth 			char		*certPathAttr;
2045e1dd0a2fSth 			ParamIndexType	type;
2046e1dd0a2fSth 
2047e1dd0a2fSth 			switch (cfg->version) {
2048e1dd0a2fSth 			case NS_LDAP_V1:
2049e1dd0a2fSth 				certPathAttr = "NS_LDAP_CERT_PATH";
2050e1dd0a2fSth 				break;
2051e1dd0a2fSth 			default:	/* Version 2 */
2052e1dd0a2fSth 				certPathAttr = "NS_LDAP_HOST_CERTPATH";
2053e1dd0a2fSth 				break;
2054e1dd0a2fSth 			}
2055e1dd0a2fSth 
2056e1dd0a2fSth 			if (__s_api_get_versiontype(cfg,
2057e1dd0a2fSth 						certPathAttr,
2058e1dd0a2fSth 						&type) == 0 &&
2059e1dd0a2fSth 			    (ret_code = __ns_ldap_setParamValue(cfg,
2060e1dd0a2fSth 						type,
2061e1dd0a2fSth 						sa_conf->SA_CERT_PATH,
2062e1dd0a2fSth 						errorp)) != NS_LDAP_SUCCESS) {
2063e1dd0a2fSth 				__s_api_destroy_config(cfg);
2064e1dd0a2fSth 				return (ret_code);
2065e1dd0a2fSth 			}
2066e1dd0a2fSth 		}
2067e1dd0a2fSth 
2068e1dd0a2fSth 		if (sa_conf->SA_BIND_DN != NULL &&
2069e1dd0a2fSth 		    sa_conf->SA_BIND_PWD != NULL) {
2070434c5a06SMilan Jurik 			char *authMethods;
2071e1dd0a2fSth 
2072434c5a06SMilan Jurik 			authMethods = __s_api_strValue(cfg, NS_LDAP_AUTH_P,
2073e1dd0a2fSth 			    NS_FILE_FMT);
2074e1dd0a2fSth 			if (authMethods != NULL &&
2075e1dd0a2fSth 			    strstr(authMethods, "sasl/GSSAPI") != NULL) {
2076e1dd0a2fSth 				/*
2077e1dd0a2fSth 				 * The received DUAProfile specifies
2078e1dd0a2fSth 				 * sasl/GSSAPI as an auth. mechanism.
2079e1dd0a2fSth 				 * The bind DN and password will be
2080e1dd0a2fSth 				 * ignored.
2081e1dd0a2fSth 				 */
2082e1dd0a2fSth 				syslog(LOG_INFO, gettext("sasl/GSSAPI will be "
2083e1dd0a2fSth 				    "used as an authentication method. "
2084e1dd0a2fSth 				    "The bind DN and password will "
2085e1dd0a2fSth 				    "be ignored.\n"));
2086434c5a06SMilan Jurik 				free(authMethods);
2087e1dd0a2fSth 				break;
2088e1dd0a2fSth 			}
2089e1dd0a2fSth 
2090434c5a06SMilan Jurik 			if (authMethods != NULL)
2091434c5a06SMilan Jurik 				free(authMethods);
2092434c5a06SMilan Jurik 
2093e1dd0a2fSth 			if (__ns_ldap_setParamValue(cfg,
2094e1dd0a2fSth 						NS_LDAP_BINDDN_P,
2095e1dd0a2fSth 						sa_conf->SA_BIND_DN,
2096e1dd0a2fSth 						errorp) != NS_LDAP_SUCCESS) {
2097e1dd0a2fSth 				__s_api_destroy_config(cfg);
2098e1dd0a2fSth 				return (NS_LDAP_CONFIG);
2099e1dd0a2fSth 			}
2100e1dd0a2fSth 
2101e1dd0a2fSth 			if (__ns_ldap_setParamValue(cfg,
2102e1dd0a2fSth 			    NS_LDAP_BINDPASSWD_P,
2103e1dd0a2fSth 			    sa_conf->SA_BIND_PWD,
2104e1dd0a2fSth 			    errorp) != NS_LDAP_SUCCESS) {
2105e1dd0a2fSth 				__s_api_destroy_config(cfg);
2106e1dd0a2fSth 				return (NS_LDAP_CONFIG);
2107e1dd0a2fSth 			}
2108e1dd0a2fSth 		}
2109e1dd0a2fSth 
2110e1dd0a2fSth 		break;
2111e1dd0a2fSth 	default:	/* NS_CACHEMGR */
2112e1dd0a2fSth 		return (NS_LDAP_SUCCESS);
2113e1dd0a2fSth 	}
2114e1dd0a2fSth 
2115e1dd0a2fSth 	__s_api_init_config(cfg);
2116ca190d8dSmichen 	/* Connection management should use the new config now. */
2117ca190d8dSmichen 	__s_api_reinit_conn_mgmt_new_config(cfg);
2118e1dd0a2fSth 	__ns_ldap_setServer(TRUE);
2119e1dd0a2fSth 
2120e1dd0a2fSth 	(void) mutex_lock(&dir_servers.listReplaceLock);
2121e1dd0a2fSth 	if ((ret_code = initGlobalList(errorp)) != NS_SUCCESS) {
2122e1dd0a2fSth 		(void) mutex_unlock(&dir_servers.listReplaceLock);
2123e1dd0a2fSth 		return (ret_code);
2124e1dd0a2fSth 	}
2125e1dd0a2fSth 	dir_servers.standalone = 1;
2126e1dd0a2fSth 	(void) mutex_unlock(&dir_servers.listReplaceLock);
2127e1dd0a2fSth 
2128e1dd0a2fSth 	return (NS_LDAP_SUCCESS);
2129e1dd0a2fSth }
2130e1dd0a2fSth 
2131e1dd0a2fSth /*
2132e1dd0a2fSth  * INPUT:
2133e1dd0a2fSth  *     serverAddr is the address of a server and
2134e1dd0a2fSth  *     request is one of the following:
2135e1dd0a2fSth  *     NS_CACHE_NEW:    get a new server address, addr is ignored.
2136e1dd0a2fSth  *     NS_CACHE_NORESP: get the next one, remove addr from list.
2137e1dd0a2fSth  *     NS_CACHE_NEXT:   get the next one, keep addr on list.
2138e1dd0a2fSth  *     NS_CACHE_WRITE:  get a non-replica server, if possible, if not, same
2139e1dd0a2fSth  *                      as NS_CACHE_NEXT.
2140e1dd0a2fSth  *     addrType:
2141e1dd0a2fSth  *     NS_CACHE_ADDR_IP: return server address as is, this is default.
2142e1dd0a2fSth  *     NS_CACHE_ADDR_HOSTNAME: return server addess as FQDN format, only
2143e1dd0a2fSth  *                             self credential case requires such format.
2144e1dd0a2fSth  * OUTPUT:
2145e1dd0a2fSth  *     ret
2146e1dd0a2fSth  *
2147e1dd0a2fSth  *     a structure of type ns_server_info_t containing the server address
2148e1dd0a2fSth  *     or name, server controls and supported SASL mechanisms.
2149e1dd0a2fSth  *     NOTE: Caller should allocate space for the structure and free
2150e1dd0a2fSth  *     all the space allocated by the function for the information contained
2151e1dd0a2fSth  *     in the structure.
2152e1dd0a2fSth  *
2153e1dd0a2fSth  *     error - an error object describing an error, if any.
2154e1dd0a2fSth  */
2155e1dd0a2fSth ns_ldap_return_code
__s_api_findRootDSE(const char * request,const char * serverAddr,const char * addrType,ns_server_info_t * ret,ns_ldap_error_t ** error)2156e1dd0a2fSth __s_api_findRootDSE(const char *request,
2157e1dd0a2fSth 		const char *serverAddr,
2158e1dd0a2fSth 		const char *addrType,
2159e1dd0a2fSth 		ns_server_info_t *ret,
2160e1dd0a2fSth 		ns_ldap_error_t	**error)
2161e1dd0a2fSth {
2162e1dd0a2fSth 	dir_server_list_t	*current_list = NULL;
2163e1dd0a2fSth 	ns_ldap_return_code	ret_code;
2164e1dd0a2fSth 	long			i = 0;
2165e1dd0a2fSth 	int			matched = FALSE;
2166e1dd0a2fSth 	dir_server_t		*server = NULL;
2167e1dd0a2fSth 	char			errmsg[MAXERROR];
2168e1dd0a2fSth 
2169e1dd0a2fSth 	(void) mutex_lock(&dir_servers.listReplaceLock);
2170e1dd0a2fSth 	if (dir_servers.list == NULL) {
2171e1dd0a2fSth 		(void) mutex_unlock(&dir_servers.listReplaceLock);
2172e1dd0a2fSth 		(void) snprintf(errmsg,
2173e1dd0a2fSth 		    sizeof (errmsg),
2174e1dd0a2fSth 		    gettext("The list of root DSEs is empty: "
2175e1dd0a2fSth 		    "the Standalone mode was not properly initialized"));
2176e1dd0a2fSth 		MKERROR(LOG_ERR,
2177e1dd0a2fSth 		    *error,
2178e1dd0a2fSth 		    NS_CONFIG_NOTLOADED,
2179e1dd0a2fSth 		    strdup(errmsg),
2180e1dd0a2fSth 		    NS_LDAP_MEMORY);
2181e1dd0a2fSth 		return (NS_LDAP_INTERNAL);
2182e1dd0a2fSth 	}
2183e1dd0a2fSth 
2184e1dd0a2fSth 	current_list = dir_servers.list;
2185e1dd0a2fSth 	(void) rw_rdlock(&current_list->listDestroyLock);
2186e1dd0a2fSth 	(void) mutex_unlock(&dir_servers.listReplaceLock);
2187e1dd0a2fSth 
2188e1dd0a2fSth 	/*
2189e1dd0a2fSth 	 * The code below is mostly the clone of the
2190e1dd0a2fSth 	 * ldap_cachemgr::cachemgr_getldap.c::getldap_get_serverInfo() function.
2191e1dd0a2fSth 	 * Currently we have two different server lists: one is maintained
2192e1dd0a2fSth 	 * by libsldap ('standalone' mode), the other is in ldap_cachemgr
2193e1dd0a2fSth 	 * (a part of its standard functionality).
2194e1dd0a2fSth 	 */
2195e1dd0a2fSth 
2196e1dd0a2fSth 	/*
2197e1dd0a2fSth 	 * If NS_CACHE_NEW, or the server info is new,
2198e1dd0a2fSth 	 * starts from the beginning of the list.
2199e1dd0a2fSth 	 */
2200e1dd0a2fSth 	(void) mutex_lock(&current_list->nsServers[0]->updateStatus);
2201e1dd0a2fSth 	if (strcmp(request, NS_CACHE_NEW) == 0 ||
2202e1dd0a2fSth 	    current_list->nsServers[0]->info == INFO_STATUS_NEW) {
2203e1dd0a2fSth 		matched = TRUE;
2204e1dd0a2fSth 	}
2205e1dd0a2fSth 	(void) mutex_unlock(&current_list->nsServers[i]->updateStatus);
2206e1dd0a2fSth 
2207e1dd0a2fSth 	for (i = 0; current_list->nsServers[i]; ++i) {
2208e1dd0a2fSth 		/*
2209e1dd0a2fSth 		 * Lock the updateStatus mutex to
2210e1dd0a2fSth 		 * make sure the server status stays the same
2211e1dd0a2fSth 		 * while the data is being processed.
2212e1dd0a2fSth 		 */
2213e1dd0a2fSth 		if (matched == FALSE &&
2214e1dd0a2fSth 		    strcmp(current_list->nsServers[i]->ip,
2215e1dd0a2fSth 		    serverAddr) == 0) {
2216e1dd0a2fSth 			matched = TRUE;
2217e1dd0a2fSth 			if (strcmp(request, NS_CACHE_NORESP) == 0) {
2218e1dd0a2fSth 
2219e1dd0a2fSth 				/*
2220e1dd0a2fSth 				 * if the server has already been removed,
2221e1dd0a2fSth 				 * don't bother.
2222e1dd0a2fSth 				 */
2223e1dd0a2fSth 				(void) mutex_lock(&current_list->
2224e1dd0a2fSth 				    nsServers[i]->updateStatus);
2225e1dd0a2fSth 				if (current_list->nsServers[i]->status ==
2226e1dd0a2fSth 				    INFO_SERVER_REMOVED) {
2227e1dd0a2fSth 					(void) mutex_unlock(&current_list->
2228e1dd0a2fSth 					    nsServers[i]->
2229e1dd0a2fSth 					    updateStatus);
2230e1dd0a2fSth 					continue;
2231e1dd0a2fSth 				}
2232e1dd0a2fSth 				(void) mutex_unlock(&current_list->
2233e1dd0a2fSth 				    nsServers[i]->
2234e1dd0a2fSth 				    updateStatus);
2235e1dd0a2fSth 
2236e1dd0a2fSth 				/*
2237e1dd0a2fSth 				 * if the information is new,
2238e1dd0a2fSth 				 * give this server one more chance.
2239e1dd0a2fSth 				 */
2240e1dd0a2fSth 				(void) mutex_lock(&current_list->
2241e1dd0a2fSth 				    nsServers[i]->
2242e1dd0a2fSth 				    updateStatus);
2243e1dd0a2fSth 				if (current_list->nsServers[i]->info ==
2244e1dd0a2fSth 				    INFO_STATUS_NEW &&
2245e1dd0a2fSth 				    current_list->nsServers[i]->status  ==
2246e1dd0a2fSth 				    INFO_SERVER_UP) {
2247e1dd0a2fSth 					server = current_list->nsServers[i];
2248e1dd0a2fSth 					(void) mutex_unlock(&current_list->
2249e1dd0a2fSth 					    nsServers[i]->
2250e1dd0a2fSth 					    updateStatus);
2251e1dd0a2fSth 					break;
2252e1dd0a2fSth 				} else {
2253e1dd0a2fSth 					/*
2254e1dd0a2fSth 					 * it is recommended that
2255e1dd0a2fSth 					 * before removing the
2256e1dd0a2fSth 					 * server from the list,
2257e1dd0a2fSth 					 * the server should be
2258e1dd0a2fSth 					 * contacted one more time
2259e1dd0a2fSth 					 * to make sure that it is
2260e1dd0a2fSth 					 * really unavailable.
2261e1dd0a2fSth 					 * For now, just trust the client
2262e1dd0a2fSth 					 * (i.e., the sldap library)
2263e1dd0a2fSth 					 * that it knows what it is
2264e1dd0a2fSth 					 * doing and would not try
2265e1dd0a2fSth 					 * to mess up the server
2266e1dd0a2fSth 					 * list.
2267e1dd0a2fSth 					 */
2268e1dd0a2fSth 					current_list->nsServers[i]->status =
2269e1dd0a2fSth 					    INFO_SERVER_REMOVED;
2270e1dd0a2fSth 					(void) mutex_unlock(&current_list->
2271e1dd0a2fSth 					    nsServers[i]->
2272e1dd0a2fSth 					    updateStatus);
2273e1dd0a2fSth 					continue;
2274e1dd0a2fSth 				}
2275e1dd0a2fSth 			} else {
2276e1dd0a2fSth 				/*
2277e1dd0a2fSth 				 * req == NS_CACHE_NEXT or NS_CACHE_WRITE
2278e1dd0a2fSth 				 */
2279e1dd0a2fSth 				continue;
2280e1dd0a2fSth 			}
2281e1dd0a2fSth 		}
2282e1dd0a2fSth 
2283e1dd0a2fSth 		if (matched) {
2284e1dd0a2fSth 			if (strcmp(request, NS_CACHE_WRITE) == 0) {
2285e1dd0a2fSth 				/*
2286e1dd0a2fSth 				 * ldap_cachemgr checks here if the server
2287e1dd0a2fSth 				 * is not a non-replica server (a server
2288e1dd0a2fSth 				 * of type INFO_RW_WRITEABLE). But currently
2289e1dd0a2fSth 				 * it considers all the servers in its list
2290e1dd0a2fSth 				 * as those.
2291e1dd0a2fSth 				 */
2292e1dd0a2fSth 				(void) mutex_lock(&current_list->
2293e1dd0a2fSth 				    nsServers[i]->
2294e1dd0a2fSth 				    updateStatus);
2295e1dd0a2fSth 				if (current_list->nsServers[i]->status  ==
2296e1dd0a2fSth 				    INFO_SERVER_UP) {
2297e1dd0a2fSth 					(void) mutex_unlock(&current_list->
2298e1dd0a2fSth 					    nsServers[i]->
2299e1dd0a2fSth 					    updateStatus);
2300e1dd0a2fSth 					server = current_list->nsServers[i];
2301e1dd0a2fSth 					break;
2302e1dd0a2fSth 				}
2303e1dd0a2fSth 			} else {
2304e1dd0a2fSth 				(void) mutex_lock(&current_list->
2305e1dd0a2fSth 				    nsServers[i]->
2306e1dd0a2fSth 				    updateStatus);
2307e1dd0a2fSth 				if (current_list->nsServers[i]->status ==
2308e1dd0a2fSth 				    INFO_SERVER_UP) {
2309e1dd0a2fSth 					(void) mutex_unlock(&current_list->
2310e1dd0a2fSth 					    nsServers[i]->
2311e1dd0a2fSth 					    updateStatus);
2312e1dd0a2fSth 					server = current_list->nsServers[i];
2313e1dd0a2fSth 					break;
2314e1dd0a2fSth 				}
2315e1dd0a2fSth 			}
2316e1dd0a2fSth 
2317e1dd0a2fSth 			(void) mutex_unlock(&current_list->
2318e1dd0a2fSth 			    nsServers[i]->
2319e1dd0a2fSth 			    updateStatus);
2320e1dd0a2fSth 		}
2321e1dd0a2fSth 	}
2322e1dd0a2fSth 
2323e1dd0a2fSth 	if (server == NULL) {
2324e1dd0a2fSth 		(void) rw_unlock(&current_list->listDestroyLock);
2325e1dd0a2fSth 		(void) snprintf(errmsg,
2326e1dd0a2fSth 		    sizeof (errmsg),
2327e1dd0a2fSth 		    gettext("No servers are available"));
2328e1dd0a2fSth 		MKERROR(LOG_ERR,
2329e1dd0a2fSth 		    *error,
2330e1dd0a2fSth 		    NS_CONFIG_NOTLOADED,
2331e1dd0a2fSth 		    strdup(errmsg),
2332e1dd0a2fSth 		    NS_LDAP_MEMORY);
2333e1dd0a2fSth 		return (NS_LDAP_NOTFOUND);
2334e1dd0a2fSth 	}
2335e1dd0a2fSth 
2336e1dd0a2fSth 	(void) mutex_lock(&server->updateStatus);
2337e1dd0a2fSth 	server->info = INFO_STATUS_OLD;
2338e1dd0a2fSth 	(void) mutex_unlock(&server->updateStatus);
2339e1dd0a2fSth 
2340e1dd0a2fSth 	if (ret == NULL) {
2341e1dd0a2fSth 		(void) rw_unlock(&current_list->listDestroyLock);
2342e1dd0a2fSth 		return (NS_LDAP_SUCCESS);
2343e1dd0a2fSth 	}
2344e1dd0a2fSth 
2345e1dd0a2fSth 	if (strcmp(addrType, NS_CACHE_ADDR_HOSTNAME) == 0) {
2346e1dd0a2fSth 		ret_code = __s_api_ip2hostname(server->ip, &ret->serverFQDN);
2347e1dd0a2fSth 		if (ret_code != NS_LDAP_SUCCESS) {
2348e1dd0a2fSth 			(void) snprintf(errmsg,
2349e1dd0a2fSth 			    sizeof (errmsg),
2350e1dd0a2fSth 			    gettext("The %s address "
2351e1dd0a2fSth 			    "can not be resolved into "
2352e1dd0a2fSth 			    "a host name. Returning "
2353e1dd0a2fSth 			    "the address as it is."),
2354e1dd0a2fSth 			    server->ip);
2355e1dd0a2fSth 			MKERROR(LOG_ERR,
2356e1dd0a2fSth 			    *error,
2357e1dd0a2fSth 			    NS_CONFIG_NOTLOADED,
2358e1dd0a2fSth 			    strdup(errmsg),
2359e1dd0a2fSth 			    NS_LDAP_MEMORY);
2360e1dd0a2fSth 			return (NS_LDAP_INTERNAL);
2361e1dd0a2fSth 		}
2362e1dd0a2fSth 	}
2363e1dd0a2fSth 
2364e1dd0a2fSth 	ret->server = strdup(server->ip);
2365e1dd0a2fSth 
2366e1dd0a2fSth 	ret->controls = __s_api_cp2dArray(server->controls);
2367e1dd0a2fSth 	ret->saslMechanisms = __s_api_cp2dArray(server->saslMech);
2368e1dd0a2fSth 
2369e1dd0a2fSth 	(void) rw_unlock(&current_list->listDestroyLock);
2370e1dd0a2fSth 
2371e1dd0a2fSth 	return (NS_LDAP_SUCCESS);
2372e1dd0a2fSth }
2373e1dd0a2fSth 
2374e1dd0a2fSth /*
2375e1dd0a2fSth  * This function iterates through the list of the configured LDAP servers
2376e1dd0a2fSth  * and "pings" those which are marked as removed or if any error occurred
2377e1dd0a2fSth  * during the previous receiving of the server's root DSE. If the
2378e1dd0a2fSth  * function is able to reach such a server and get its root DSE, it
2379e1dd0a2fSth  * marks the server as on-line. Otherwise, the server's status is set
2380e1dd0a2fSth  * to "Error".
2381e1dd0a2fSth  * For each server the function tries to connect to, it fires up
2382e1dd0a2fSth  * a separate thread and then waits until all the treads finish.
2383e1dd0a2fSth  * The function returns NS_LDAP_INTERNAL if the Standalone mode was not
2384e1dd0a2fSth  * initialized or was canceled prior to an invocation of
2385e1dd0a2fSth  * __ns_ldap_pingOfflineServers().
2386e1dd0a2fSth  */
2387e1dd0a2fSth ns_ldap_return_code
__ns_ldap_pingOfflineServers(void)2388e1dd0a2fSth __ns_ldap_pingOfflineServers(void)
2389e1dd0a2fSth {
2390e1dd0a2fSth 	dir_server_list_t	*current_list = NULL;
2391e1dd0a2fSth 	ns_ldap_return_code	retCode = NS_LDAP_SUCCESS;
2392e1dd0a2fSth 	long			srvListLength, i = 0;
2393e1dd0a2fSth 	thread_t		*thrPool, thrID;
2394e1dd0a2fSth 	void			*status = NULL;
2395e1dd0a2fSth 
2396e1dd0a2fSth 	(void) mutex_lock(&dir_servers.listReplaceLock);
2397e1dd0a2fSth 	if (dir_servers.list == NULL) {
2398e1dd0a2fSth 		(void) mutex_unlock(&dir_servers.listReplaceLock);
2399e1dd0a2fSth 		return (NS_LDAP_INTERNAL);
2400e1dd0a2fSth 	}
2401e1dd0a2fSth 
2402e1dd0a2fSth 	current_list = dir_servers.list;
2403e1dd0a2fSth 	(void) rw_wrlock(&current_list->listDestroyLock);
2404e1dd0a2fSth 	(void) mutex_unlock(&dir_servers.listReplaceLock);
2405e1dd0a2fSth 
2406e1dd0a2fSth 	while (current_list->nsServers[i] != NULL) {
2407e1dd0a2fSth 		++i;
2408e1dd0a2fSth 	}
2409e1dd0a2fSth 	srvListLength = i;
2410e1dd0a2fSth 
2411e1dd0a2fSth 	thrPool = calloc(srvListLength, sizeof (thread_t));
2412e1dd0a2fSth 	if (thrPool == NULL) {
2413e1dd0a2fSth 		(void) rw_unlock(&current_list->listDestroyLock);
2414e1dd0a2fSth 		return (NS_LDAP_MEMORY);
2415e1dd0a2fSth 	}
2416e1dd0a2fSth 
2417e1dd0a2fSth 	for (i = 0; i < srvListLength; ++i) {
2418e1dd0a2fSth 		if (current_list->nsServers[i]->status != INFO_SERVER_REMOVED &&
2419e1dd0a2fSth 		    current_list->nsServers[i]->status != INFO_SERVER_ERROR) {
2420e1dd0a2fSth 			continue;
2421e1dd0a2fSth 		}
2422e1dd0a2fSth 		current_list->nsServers[i]->status = INFO_SERVER_CONNECTING;
2423e1dd0a2fSth 		current_list->nsServers[i]->info = INFO_STATUS_NEW;
2424e1dd0a2fSth 
2425e1dd0a2fSth 		__s_api_free2dArray(current_list->nsServers[i]->controls);
2426e1dd0a2fSth 		current_list->nsServers[i]->controls = NULL;
2427e1dd0a2fSth 		__s_api_free2dArray(current_list->nsServers[i]->saslMech);
2428e1dd0a2fSth 		current_list->nsServers[i]->saslMech = NULL;
2429e1dd0a2fSth 
2430e1dd0a2fSth 		switch (thr_create(NULL,
2431e1dd0a2fSth 		    0,
2432e1dd0a2fSth 		    create_ns_servers_entry,
2433e1dd0a2fSth 		    current_list->nsServers[i],
2434e1dd0a2fSth 		    0,
2435e1dd0a2fSth 		    &thrID)) {
2436e1dd0a2fSth 		case EAGAIN:
2437e1dd0a2fSth 			current_list->nsServers[i]->status = INFO_SERVER_ERROR;
2438e1dd0a2fSth 			continue;
2439e1dd0a2fSth 		case ENOMEM:
2440e1dd0a2fSth 			current_list->nsServers[i]->status = INFO_SERVER_ERROR;
2441e1dd0a2fSth 			retCode = NS_LDAP_MEMORY;
2442e1dd0a2fSth 			break;
2443e1dd0a2fSth 		default:
2444e1dd0a2fSth 			thrPool[i] = thrID;
2445e1dd0a2fSth 			continue;
2446e1dd0a2fSth 		}
2447e1dd0a2fSth 		/* A memory allocation error has occured */
2448e1dd0a2fSth 		break;
2449e1dd0a2fSth 
2450e1dd0a2fSth 	}
2451e1dd0a2fSth 
2452e1dd0a2fSth 	for (i = 0; i < srvListLength; ++i) {
2453e1dd0a2fSth 		if (thrPool[i] != 0 &&
2454e1dd0a2fSth 		    thr_join(thrPool[i], NULL, &status) == 0) {
2455e1dd0a2fSth 			if (status == NULL) {
2456e1dd0a2fSth 				current_list->nsServers[i]->status =
2457e1dd0a2fSth 				    INFO_SERVER_ERROR;
2458e1dd0a2fSth 				retCode = NS_LDAP_MEMORY;
2459e1dd0a2fSth 			}
2460e1dd0a2fSth 			free(status);
2461e1dd0a2fSth 		}
2462e1dd0a2fSth 	}
2463e1dd0a2fSth 
2464e1dd0a2fSth 	(void) rw_unlock(&current_list->listDestroyLock);
2465e1dd0a2fSth 
2466e1dd0a2fSth 	free(thrPool);
2467e1dd0a2fSth 
2468e1dd0a2fSth 	return (retCode);
2469e1dd0a2fSth }
2470