xref: /titanic_44/usr/src/cmd/ldapcachemgr/cachemgr_getldap.c (revision 4a6b6ac40d4a465eac9d0db6decbd3cf7fdf9349)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*4a6b6ac4Schinlong  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <assert.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <memory.h>
317c478bd9Sstevel@tonic-gate #include <signal.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <syslog.h>
377c478bd9Sstevel@tonic-gate #include <sys/door.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/time.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/wait.h>
427c478bd9Sstevel@tonic-gate #include <synch.h>
437c478bd9Sstevel@tonic-gate #include <pthread.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <lber.h>
467c478bd9Sstevel@tonic-gate #include <ldap.h>
477c478bd9Sstevel@tonic-gate #include <ctype.h>	/* tolower */
48cb5caa98Sdjl #include <sys/socket.h>
49cb5caa98Sdjl #include <netinet/in.h>
50cb5caa98Sdjl #include <arpa/inet.h>
517c478bd9Sstevel@tonic-gate #include "cachemgr.h"
527c478bd9Sstevel@tonic-gate #include "solaris-priv.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static rwlock_t	ldap_lock = DEFAULTRWLOCK;
557c478bd9Sstevel@tonic-gate static int	sighup_update = FALSE;
567c478bd9Sstevel@tonic-gate extern admin_t	current_admin;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /* variables used for SIGHUP wakeup on sleep */
597c478bd9Sstevel@tonic-gate static mutex_t			sighuplock;
607c478bd9Sstevel@tonic-gate static cond_t			cond;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /* refresh time statistics */
637c478bd9Sstevel@tonic-gate static time_t	prev_refresh_time = 0;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* variables used for signaling parent process */
667c478bd9Sstevel@tonic-gate static mutex_t	sig_mutex;
677c478bd9Sstevel@tonic-gate static int	signal_done = FALSE;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* TCP connection timeout (in milliseconds) */
707c478bd9Sstevel@tonic-gate static int tcptimeout = NS_DEFAULT_BIND_TIMEOUT * 1000;
717c478bd9Sstevel@tonic-gate /* search timeout (in seconds) */
727c478bd9Sstevel@tonic-gate static int search_timeout = NS_DEFAULT_SEARCH_TIMEOUT;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #ifdef SLP
757c478bd9Sstevel@tonic-gate extern int	use_slp;
767c478bd9Sstevel@tonic-gate #endif /* SLP */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /* nis domain information */
797c478bd9Sstevel@tonic-gate #define	_NIS_FILTER		"objectclass=nisDomainObject"
807c478bd9Sstevel@tonic-gate #define	_NIS_DOMAIN		"nisdomain"
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	CACHESLEEPTIME		600
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * server list refresh delay when in "no server" mode
857c478bd9Sstevel@tonic-gate  * (1 second)
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate #define	REFRESH_DELAY_WHEN_NO_SERVER	1
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate typedef enum {
907c478bd9Sstevel@tonic-gate 	INFO_OP_CREATE		= 0,
917c478bd9Sstevel@tonic-gate 	INFO_OP_DELETE		= 1,
927c478bd9Sstevel@tonic-gate 	INFO_OP_REFRESH		= 2,
937c478bd9Sstevel@tonic-gate 	INFO_OP_REFRESH_WAIT	= 3,
947c478bd9Sstevel@tonic-gate 	INFO_OP_GETSERVER	= 4,
957c478bd9Sstevel@tonic-gate 	INFO_OP_GETSTAT		= 5
967c478bd9Sstevel@tonic-gate } info_op_t;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate typedef enum {
997c478bd9Sstevel@tonic-gate 	INFO_RW_UNKNOWN		= 0,
1007c478bd9Sstevel@tonic-gate 	INFO_RW_READONLY	= 1,
1017c478bd9Sstevel@tonic-gate 	INFO_RW_WRITEABLE	= 2
1027c478bd9Sstevel@tonic-gate } info_rw_t;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate typedef enum {
1057c478bd9Sstevel@tonic-gate 	INFO_SERVER_JUST_INITED	= -1,
1067c478bd9Sstevel@tonic-gate 	INFO_SERVER_UNKNOWN	= 0,
1077c478bd9Sstevel@tonic-gate 	INFO_SERVER_CONNECTING	= 1,
1087c478bd9Sstevel@tonic-gate 	INFO_SERVER_UP		= 2,
1097c478bd9Sstevel@tonic-gate 	INFO_SERVER_ERROR 	= 3,
1107c478bd9Sstevel@tonic-gate 	INFO_SERVER_REMOVED	= 4
1117c478bd9Sstevel@tonic-gate } info_server_t;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate typedef enum {
1147c478bd9Sstevel@tonic-gate 	INFO_STATUS_UNKNOWN	= 0,
1157c478bd9Sstevel@tonic-gate 	INFO_STATUS_ERROR 	= 1,
1167c478bd9Sstevel@tonic-gate 	INFO_STATUS_NEW   	= 2,
1177c478bd9Sstevel@tonic-gate 	INFO_STATUS_OLD		= 3
1187c478bd9Sstevel@tonic-gate } info_status_t;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate typedef enum {
1217c478bd9Sstevel@tonic-gate 	CACHE_OP_CREATE		= 0,
1227c478bd9Sstevel@tonic-gate 	CACHE_OP_DELETE		= 1,
1237c478bd9Sstevel@tonic-gate 	CACHE_OP_FIND		= 2,
1247c478bd9Sstevel@tonic-gate 	CACHE_OP_ADD		= 3,
1257c478bd9Sstevel@tonic-gate 	CACHE_OP_GETSTAT	= 4
1267c478bd9Sstevel@tonic-gate } cache_op_t;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate typedef enum {
1297c478bd9Sstevel@tonic-gate 	CACHE_MAP_UNKNOWN	= 0,
1307c478bd9Sstevel@tonic-gate 	CACHE_MAP_DN2DOMAIN	= 1
1317c478bd9Sstevel@tonic-gate } cache_type_t;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate typedef struct server_info_ext {
1347c478bd9Sstevel@tonic-gate 	char			*addr;
135cb5caa98Sdjl 	char			*hostname;
1367c478bd9Sstevel@tonic-gate 	char			*rootDSE_data;
1377c478bd9Sstevel@tonic-gate 	char			*errormsg;
1387c478bd9Sstevel@tonic-gate 	info_rw_t		type;
1397c478bd9Sstevel@tonic-gate 	info_server_t		server_status;
1407c478bd9Sstevel@tonic-gate 	info_server_t		prev_server_status;
1417c478bd9Sstevel@tonic-gate 	info_status_t 		info_status;
1427c478bd9Sstevel@tonic-gate } server_info_ext_t;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate typedef struct server_info {
1457c478bd9Sstevel@tonic-gate 	struct server_info 	*next;
1467c478bd9Sstevel@tonic-gate 	mutex_t			mutex[2];	/* 0: current copy lock */
1477c478bd9Sstevel@tonic-gate 						/* 1: update copy lock */
1487c478bd9Sstevel@tonic-gate 	server_info_ext_t	sinfo[2]; /* 0: current, 1:  update copy */
1497c478bd9Sstevel@tonic-gate } server_info_t;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate typedef struct cache_hash {
1527c478bd9Sstevel@tonic-gate 	cache_type_t		type;
1537c478bd9Sstevel@tonic-gate 	char			*from;
1547c478bd9Sstevel@tonic-gate 	char			*to;
1557c478bd9Sstevel@tonic-gate 	struct cache_hash	*next;
1567c478bd9Sstevel@tonic-gate } cache_hash_t;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static int getldap_destroy_serverInfo(server_info_t *head);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Load configuration
1627c478bd9Sstevel@tonic-gate  * The code was in signal handler getldap_revalidate
1637c478bd9Sstevel@tonic-gate  * It's moved out of the handler because it could cause deadlock
1647c478bd9Sstevel@tonic-gate  * return: 1 SUCCESS
1657c478bd9Sstevel@tonic-gate  *         0 FAIL
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate static int
1687c478bd9Sstevel@tonic-gate load_config() {
1697c478bd9Sstevel@tonic-gate 	ns_ldap_error_t *error;
1707c478bd9Sstevel@tonic-gate 	int		rc = 1;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_setServer(TRUE);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&ldap_lock);
1757c478bd9Sstevel@tonic-gate 	if ((error = __ns_ldap_LoadConfiguration()) != NULL) {
1767c478bd9Sstevel@tonic-gate 		logit("Error: Unable to read '%s': %s\n",
1777c478bd9Sstevel@tonic-gate 			NSCONFIGFILE, error->message);
1787c478bd9Sstevel@tonic-gate 		__ns_ldap_freeError(&error);
1797c478bd9Sstevel@tonic-gate 		rc = 0; /* FAIL */
1807c478bd9Sstevel@tonic-gate 	} else
1817c478bd9Sstevel@tonic-gate 		sighup_update = TRUE;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&ldap_lock);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	return (rc);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * Calculate a hash for a string
1907c478bd9Sstevel@tonic-gate  * Based on elf_hash algorithm, hash is case insensitive
1917c478bd9Sstevel@tonic-gate  * Uses tolower instead of _tolower because of I18N
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static unsigned long
1957c478bd9Sstevel@tonic-gate getldap_hash(const char *str)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	unsigned int	hval = 0;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	while (*str) {
2007c478bd9Sstevel@tonic-gate 		unsigned int	g;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		hval = (hval << 4) + tolower(*str++);
2037c478bd9Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
2047c478bd9Sstevel@tonic-gate 			hval ^= g >> 24;
2057c478bd9Sstevel@tonic-gate 		hval &= ~g;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	return ((unsigned long)hval);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * Remove a hash table entry.
2127c478bd9Sstevel@tonic-gate  * This function expects a lock in place when called.
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate static cache_hash_t *
2167c478bd9Sstevel@tonic-gate getldap_free_hash(cache_hash_t *p)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	cache_hash_t	*next;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	p->type = CACHE_MAP_UNKNOWN;
2217c478bd9Sstevel@tonic-gate 	if (p->from)
2227c478bd9Sstevel@tonic-gate 		free(p->from);
2237c478bd9Sstevel@tonic-gate 	if (p->to)
2247c478bd9Sstevel@tonic-gate 		free(p->to);
2257c478bd9Sstevel@tonic-gate 	next = p->next;
2267c478bd9Sstevel@tonic-gate 	p->next = NULL;
2277c478bd9Sstevel@tonic-gate 	free(p);
2287c478bd9Sstevel@tonic-gate 	return (next);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * Scan a hash table hit for a matching hash entry.
2337c478bd9Sstevel@tonic-gate  * This function expects a lock in place when called.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate static cache_hash_t *
2367c478bd9Sstevel@tonic-gate getldap_scan_hash(cache_type_t type, char *from,
2377c478bd9Sstevel@tonic-gate 		cache_hash_t *idx)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	while (idx) {
2407c478bd9Sstevel@tonic-gate 		if (idx->type == type &&
2417c478bd9Sstevel@tonic-gate 		    strcasecmp(from, idx->from) == 0) {
2427c478bd9Sstevel@tonic-gate 			return (idx);
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		idx = idx->next;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	return ((cache_hash_t *)NULL);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * Format and return the cache data statistics
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate static int
2537c478bd9Sstevel@tonic-gate getldap_get_cacheData_stat(int max, int current, char **output)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate #define	C_HEADER0	"Cache data information: "
2567c478bd9Sstevel@tonic-gate #define	C_HEADER1	"  Maximum cache entries:   "
2577c478bd9Sstevel@tonic-gate #define	C_HEADER2	"  Number of cache entries: "
2587c478bd9Sstevel@tonic-gate 	int		hdr0_len = strlen(gettext(C_HEADER0));
2597c478bd9Sstevel@tonic-gate 	int		hdr1_len = strlen(gettext(C_HEADER1));
2607c478bd9Sstevel@tonic-gate 	int		hdr2_len = strlen(gettext(C_HEADER2));
2617c478bd9Sstevel@tonic-gate 	int		len;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
2647c478bd9Sstevel@tonic-gate 		logit("getldap_get_cacheData_stat()...\n");
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	*output = NULL;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	len = hdr0_len + hdr1_len + hdr2_len +
2707c478bd9Sstevel@tonic-gate 	    3 * strlen(DOORLINESEP) + 21;
2717c478bd9Sstevel@tonic-gate 	*output = malloc(len);
2727c478bd9Sstevel@tonic-gate 	if (*output == NULL)
2737c478bd9Sstevel@tonic-gate 		return (-1);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	(void) snprintf(*output, len, "%s%s%s%10d%s%s%10d%s",
2767c478bd9Sstevel@tonic-gate 	    gettext(C_HEADER0), DOORLINESEP,
2777c478bd9Sstevel@tonic-gate 	    gettext(C_HEADER1), max, DOORLINESEP,
2787c478bd9Sstevel@tonic-gate 	    gettext(C_HEADER2), current, DOORLINESEP);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate static int
2847c478bd9Sstevel@tonic-gate getldap_cache_op(cache_op_t op, cache_type_t type,
2857c478bd9Sstevel@tonic-gate 			char *from, char **to)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate #define	CACHE_HASH_MAX		257
2887c478bd9Sstevel@tonic-gate #define	CACHE_HASH_MAX_ENTRY	256
2897c478bd9Sstevel@tonic-gate 	static cache_hash_t	*hashTbl[CACHE_HASH_MAX];
2907c478bd9Sstevel@tonic-gate 	cache_hash_t		*next, *idx, *newp;
2917c478bd9Sstevel@tonic-gate 	unsigned long		hash;
2927c478bd9Sstevel@tonic-gate 	static rwlock_t 	cache_lock = DEFAULTRWLOCK;
2937c478bd9Sstevel@tonic-gate 	int 			i;
294a506a34cSth160488 	static int		entry_num = 0;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
2977c478bd9Sstevel@tonic-gate 		logit("getldap_cache_op()...\n");
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	switch (op) {
3007c478bd9Sstevel@tonic-gate 	case CACHE_OP_CREATE:
3017c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
3027c478bd9Sstevel@tonic-gate 			logit("operation is CACHE_OP_CREATE...\n");
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 		(void) rw_wrlock(&cache_lock);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		for (i = 0; i < CACHE_HASH_MAX; i++) {
3077c478bd9Sstevel@tonic-gate 			hashTbl[i] = NULL;
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 		entry_num = 0;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&cache_lock);
3127c478bd9Sstevel@tonic-gate 		break;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	case CACHE_OP_DELETE:
3157c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
3167c478bd9Sstevel@tonic-gate 			logit("operation is CACHE_OP_DELETE...\n");
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 		(void) rw_wrlock(&cache_lock);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		for (i = 0; i < CACHE_HASH_MAX; i++) {
3217c478bd9Sstevel@tonic-gate 			next = hashTbl[i];
3227c478bd9Sstevel@tonic-gate 			while (next != NULL) {
3237c478bd9Sstevel@tonic-gate 				next = getldap_free_hash(next);
3247c478bd9Sstevel@tonic-gate 			}
3257c478bd9Sstevel@tonic-gate 			hashTbl[i] = NULL;
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 		entry_num = 0;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&cache_lock);
3307c478bd9Sstevel@tonic-gate 		break;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	case CACHE_OP_ADD:
3337c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
3347c478bd9Sstevel@tonic-gate 			logit("operation is CACHE_OP_ADD...\n");
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 		if (from == NULL || to == NULL || *to == NULL)
3377c478bd9Sstevel@tonic-gate 			return (-1);
3387c478bd9Sstevel@tonic-gate 		hash = getldap_hash(from) % CACHE_HASH_MAX;
3397c478bd9Sstevel@tonic-gate 		(void) rw_wrlock(&cache_lock);
3407c478bd9Sstevel@tonic-gate 		idx = hashTbl[hash];
3417c478bd9Sstevel@tonic-gate 		/*
3427c478bd9Sstevel@tonic-gate 		 * replace old "to" value with new one
3437c478bd9Sstevel@tonic-gate 		 * if an entry with same "from"
3447c478bd9Sstevel@tonic-gate 		 * already exists
3457c478bd9Sstevel@tonic-gate 		 */
3467c478bd9Sstevel@tonic-gate 		if (idx) {
3477c478bd9Sstevel@tonic-gate 			newp = getldap_scan_hash(type, from, idx);
3487c478bd9Sstevel@tonic-gate 			if (newp) {
3497c478bd9Sstevel@tonic-gate 				free(newp->to);
3507c478bd9Sstevel@tonic-gate 				newp->to = strdup(*to);
3517c478bd9Sstevel@tonic-gate 				(void) rw_unlock(&cache_lock);
3527c478bd9Sstevel@tonic-gate 				return (NS_LDAP_SUCCESS);
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		if (entry_num > CACHE_HASH_MAX_ENTRY) {
3577c478bd9Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
3587c478bd9Sstevel@tonic-gate 			return (-1);
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		newp = (cache_hash_t *)malloc(sizeof (cache_hash_t));
3627c478bd9Sstevel@tonic-gate 		if (newp == NULL) {
3637c478bd9Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
3647c478bd9Sstevel@tonic-gate 			return (NS_LDAP_MEMORY);
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		newp->type = type;
3677c478bd9Sstevel@tonic-gate 		newp->from = strdup(from);
3687c478bd9Sstevel@tonic-gate 		newp->to = strdup(*to);
3697c478bd9Sstevel@tonic-gate 		newp->next = idx;
3707c478bd9Sstevel@tonic-gate 		hashTbl[hash] = newp;
3717c478bd9Sstevel@tonic-gate 		entry_num++;
3727c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&cache_lock);
3737c478bd9Sstevel@tonic-gate 		break;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	case CACHE_OP_FIND:
3767c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
3777c478bd9Sstevel@tonic-gate 			logit("operation is CACHE_OP_FIND...\n");
3787c478bd9Sstevel@tonic-gate 		}
3797c478bd9Sstevel@tonic-gate 		if (from == NULL || to == NULL)
3807c478bd9Sstevel@tonic-gate 			return (-1);
3817c478bd9Sstevel@tonic-gate 		*to = NULL;
3827c478bd9Sstevel@tonic-gate 		hash = getldap_hash(from) % CACHE_HASH_MAX;
3837c478bd9Sstevel@tonic-gate 		(void) rw_rdlock(&cache_lock);
3847c478bd9Sstevel@tonic-gate 		idx = hashTbl[hash];
3857c478bd9Sstevel@tonic-gate 		idx = getldap_scan_hash(type, from, idx);
3867c478bd9Sstevel@tonic-gate 		if (idx)
3877c478bd9Sstevel@tonic-gate 			*to = strdup(idx->to);
3887c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&cache_lock);
3897c478bd9Sstevel@tonic-gate 		if (idx == NULL)
3907c478bd9Sstevel@tonic-gate 			return (-1);
3917c478bd9Sstevel@tonic-gate 		break;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	case CACHE_OP_GETSTAT:
3947c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
3957c478bd9Sstevel@tonic-gate 			logit("operation is CACHE_OP_GETSTAT...\n");
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 		if (to == NULL)
3987c478bd9Sstevel@tonic-gate 			return (-1);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		return (getldap_get_cacheData_stat(CACHE_HASH_MAX_ENTRY,
4017c478bd9Sstevel@tonic-gate 		    entry_num, to));
4027c478bd9Sstevel@tonic-gate 		break;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	default:
4057c478bd9Sstevel@tonic-gate 		logit("getldap_cache_op(): "
4067c478bd9Sstevel@tonic-gate 		    "invalid operation code (%d).\n", op);
4077c478bd9Sstevel@tonic-gate 		return (-1);
4087c478bd9Sstevel@tonic-gate 		break;
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * Function: sync_current_with_update_copy
4147c478bd9Sstevel@tonic-gate  *
4157c478bd9Sstevel@tonic-gate  * This function syncs up the 2 sinfo copies in info.
4167c478bd9Sstevel@tonic-gate  *
4177c478bd9Sstevel@tonic-gate  * The 2 copies are identical most of time.
4187c478bd9Sstevel@tonic-gate  * The update copy(sinfo[1]) could be different when
4197c478bd9Sstevel@tonic-gate  * getldap_serverInfo_refresh thread is refreshing the server list
4207c478bd9Sstevel@tonic-gate  * and calls getldap_get_rootDSE to update info.  getldap_get_rootDSE
4217c478bd9Sstevel@tonic-gate  * calls sync_current_with_update_copy to sync up 2 copies before thr_exit.
4227c478bd9Sstevel@tonic-gate  * The calling sequence is
4237c478bd9Sstevel@tonic-gate  *  getldap_serverInfo_refresh->
4247c478bd9Sstevel@tonic-gate  *  getldap_get_serverInfo_op(INFO_OP_CREATE,...)->
4257c478bd9Sstevel@tonic-gate  *  getldap_set_serverInfo->
4267c478bd9Sstevel@tonic-gate  *  getldap_get_rootDSE
4277c478bd9Sstevel@tonic-gate  *
4287c478bd9Sstevel@tonic-gate  * The original server_info_t has one copy of server info. When libsldap
4297c478bd9Sstevel@tonic-gate  * makes door call GETLDAPSERVER to get the server info and getldap_get_rootDSE
4307c478bd9Sstevel@tonic-gate  * is updating the server info, it would hit a unprotected window in
4317c478bd9Sstevel@tonic-gate  * getldap_rootDSE. The door call  will not get server info and libsldap
4327c478bd9Sstevel@tonic-gate  * fails at making ldap connection.
4337c478bd9Sstevel@tonic-gate  *
4347c478bd9Sstevel@tonic-gate  * The new server_info_t provides GETLDAPSERVER thread with a current
4357c478bd9Sstevel@tonic-gate  * copy(sinfo[0]). getldap_get_rootDSE only works on the update copy(sinfo[1])
4367c478bd9Sstevel@tonic-gate  * and syncs up 2 copies before thr_exit. This will close the window in
4377c478bd9Sstevel@tonic-gate  * getldap_get_rootDSE.
4387c478bd9Sstevel@tonic-gate  *
4397c478bd9Sstevel@tonic-gate  */
4407c478bd9Sstevel@tonic-gate static void
4417c478bd9Sstevel@tonic-gate sync_current_with_update_copy(server_info_t *info)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
4447c478bd9Sstevel@tonic-gate 		logit("sync_current_with_update_copy()...\n");
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&info->mutex[1]);
4487c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&info->mutex[0]);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* free memory in current copy first */
4517c478bd9Sstevel@tonic-gate 	if (info->sinfo[0].addr)
4527c478bd9Sstevel@tonic-gate 		free(info->sinfo[0].addr);
4537c478bd9Sstevel@tonic-gate 	info->sinfo[0].addr = NULL;
4547c478bd9Sstevel@tonic-gate 
455cb5caa98Sdjl 	if (info->sinfo[0].hostname)
456cb5caa98Sdjl 		free(info->sinfo[0].hostname);
457cb5caa98Sdjl 	info->sinfo[0].hostname = NULL;
458cb5caa98Sdjl 
4597c478bd9Sstevel@tonic-gate 	if (info->sinfo[0].rootDSE_data)
4607c478bd9Sstevel@tonic-gate 		free(info->sinfo[0].rootDSE_data);
4617c478bd9Sstevel@tonic-gate 	info->sinfo[0].rootDSE_data = NULL;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if (info->sinfo[0].errormsg)
4647c478bd9Sstevel@tonic-gate 		free(info->sinfo[0].errormsg);
4657c478bd9Sstevel@tonic-gate 	info->sinfo[0].errormsg = NULL;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/*
4687c478bd9Sstevel@tonic-gate 	 * make current and update copy identical
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 	info->sinfo[0] = info->sinfo[1];
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	/*
4737c478bd9Sstevel@tonic-gate 	 * getldap_get_server_stat() reads the update copy sinfo[1]
4747c478bd9Sstevel@tonic-gate 	 * so it can't be freed or nullified yet at this point.
4757c478bd9Sstevel@tonic-gate 	 *
4767c478bd9Sstevel@tonic-gate 	 * The sinfo[0] and sinfo[1] have identical string pointers.
4777c478bd9Sstevel@tonic-gate 	 * strdup the strings to avoid the double free problem.
4787c478bd9Sstevel@tonic-gate 	 * The strings of sinfo[1] are freed in
4797c478bd9Sstevel@tonic-gate 	 * getldap_get_rootDSE() and the strings of sinfo[0]
4807c478bd9Sstevel@tonic-gate 	 * are freed earlier in this function. If the pointers are the
4817c478bd9Sstevel@tonic-gate 	 * same, they will be freed twice.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	if (info->sinfo[1].addr)
4847c478bd9Sstevel@tonic-gate 		info->sinfo[0].addr = strdup(info->sinfo[1].addr);
485cb5caa98Sdjl 	if (info->sinfo[1].hostname)
486cb5caa98Sdjl 		info->sinfo[0].hostname = strdup(info->sinfo[1].hostname);
4877c478bd9Sstevel@tonic-gate 	if (info->sinfo[1].rootDSE_data)
4887c478bd9Sstevel@tonic-gate 		info->sinfo[0].rootDSE_data =
4897c478bd9Sstevel@tonic-gate 		    strdup(info->sinfo[1].rootDSE_data);
4907c478bd9Sstevel@tonic-gate 	if (info->sinfo[1].errormsg)
4917c478bd9Sstevel@tonic-gate 		info->sinfo[0].errormsg = strdup(info->sinfo[1].errormsg);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&info->mutex[0]);
4947c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&info->mutex[1]);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate static void *
4997c478bd9Sstevel@tonic-gate getldap_get_rootDSE(void *arg)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	server_info_t	*serverInfo = (server_info_t *)arg;
5027c478bd9Sstevel@tonic-gate 	int 		ldapVersion = LDAP_VERSION3;
5037c478bd9Sstevel@tonic-gate 	LDAP		*ld;
5047c478bd9Sstevel@tonic-gate 	LDAPMessage	*resultMsg = NULL;
5057c478bd9Sstevel@tonic-gate 	LDAPMessage	*e;
5067c478bd9Sstevel@tonic-gate 	BerElement	*ber;
5077c478bd9Sstevel@tonic-gate 	char		errmsg[MAXERROR];
5087c478bd9Sstevel@tonic-gate 	char		*rootDSE;
5097c478bd9Sstevel@tonic-gate 	char		*attrs[3];
5107c478bd9Sstevel@tonic-gate 	char		*a;
5117c478bd9Sstevel@tonic-gate 	char		**vals;
5127c478bd9Sstevel@tonic-gate 	int		ldaperrno = 0;
5137c478bd9Sstevel@tonic-gate 	int		rc = 0, exitrc = NS_LDAP_SUCCESS;
5147c478bd9Sstevel@tonic-gate 	int		i = 0, len = 0;
5157c478bd9Sstevel@tonic-gate 	pid_t		ppid;
5167c478bd9Sstevel@tonic-gate 	struct timeval	tv;
5177c478bd9Sstevel@tonic-gate 	int		server_found = 0;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
5207c478bd9Sstevel@tonic-gate 		logit("getldap_get_rootDSE()....\n");
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/* initialize the server info element */
5247c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&serverInfo->mutex[1]);
5257c478bd9Sstevel@tonic-gate 	serverInfo->sinfo[1].type	= INFO_RW_UNKNOWN;
526*4a6b6ac4Schinlong 	serverInfo->sinfo[1].info_status =
527*4a6b6ac4Schinlong 	    INFO_STATUS_UNKNOWN;
5287c478bd9Sstevel@tonic-gate 	/*
5297c478bd9Sstevel@tonic-gate 	 * When the sever list is refreshed over and over,
5307c478bd9Sstevel@tonic-gate 	 * this function is called each time it is refreshed.
5317c478bd9Sstevel@tonic-gate 	 * The previous server status of the update copy(sinfo[1])
5327c478bd9Sstevel@tonic-gate 	 * is the status of the current copy
5337c478bd9Sstevel@tonic-gate 	 */
5347c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&serverInfo->mutex[0]);
5357c478bd9Sstevel@tonic-gate 	serverInfo->sinfo[1].prev_server_status =
5367c478bd9Sstevel@tonic-gate 	    serverInfo->sinfo[0].server_status;
5377c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&serverInfo->mutex[0]);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	serverInfo->sinfo[1].server_status =
5407c478bd9Sstevel@tonic-gate 	    INFO_SERVER_UNKNOWN;
5417c478bd9Sstevel@tonic-gate 	if (serverInfo->sinfo[1].rootDSE_data)
5427c478bd9Sstevel@tonic-gate 		free(serverInfo->sinfo[1].rootDSE_data);
5437c478bd9Sstevel@tonic-gate 	serverInfo->sinfo[1].rootDSE_data	= NULL;
5447c478bd9Sstevel@tonic-gate 	if (serverInfo->sinfo[1].errormsg)
5457c478bd9Sstevel@tonic-gate 		free(serverInfo->sinfo[1].errormsg);
5467c478bd9Sstevel@tonic-gate 	serverInfo->sinfo[1].errormsg 		= NULL;
5477c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&serverInfo->mutex[1]);
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if ((ld = ldap_init(serverInfo->sinfo[1].addr,
5507c478bd9Sstevel@tonic-gate 	    LDAP_PORT)) == NULL ||
5517c478bd9Sstevel@tonic-gate 		/* SKIP ldap data base to prevent recursion */
5527c478bd9Sstevel@tonic-gate 		/* in gethostbyname when resolving hostname */
5537c478bd9Sstevel@tonic-gate 	    0 != ldap_set_option(ld, LDAP_X_OPT_DNS_SKIPDB, "ldap")) {
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&serverInfo->mutex[1]);
5567c478bd9Sstevel@tonic-gate 		serverInfo->sinfo[1].server_status =
5577c478bd9Sstevel@tonic-gate 		    INFO_SERVER_ERROR;
5587c478bd9Sstevel@tonic-gate 		serverInfo->sinfo[1].info_status =
5597c478bd9Sstevel@tonic-gate 		    INFO_STATUS_ERROR;
5607c478bd9Sstevel@tonic-gate 		serverInfo->sinfo[1].errormsg =
5617c478bd9Sstevel@tonic-gate 		    strdup(gettext("ldap_init failed"));
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
5647c478bd9Sstevel@tonic-gate 			logit("getldap_get_rootDSE: %s.\n",
5657c478bd9Sstevel@tonic-gate 			    serverInfo->sinfo[1].errormsg);
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&serverInfo->mutex[1]);
5687c478bd9Sstevel@tonic-gate 		/*
5697c478bd9Sstevel@tonic-gate 		 * sync sinfo copies in the serverInfo.
5707c478bd9Sstevel@tonic-gate 		 * protected by mutex
5717c478bd9Sstevel@tonic-gate 		 */
5727c478bd9Sstevel@tonic-gate 		sync_current_with_update_copy(serverInfo);
5737c478bd9Sstevel@tonic-gate 		thr_exit((void *) -1);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 	ldap_set_option(ld,
5767c478bd9Sstevel@tonic-gate 	    LDAP_OPT_PROTOCOL_VERSION, &ldapVersion);
5777c478bd9Sstevel@tonic-gate 	ldap_set_option(ld,
5787c478bd9Sstevel@tonic-gate 	    LDAP_X_OPT_CONNECT_TIMEOUT, &tcptimeout);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/* currently, only interested in two attributes */
5817c478bd9Sstevel@tonic-gate 	attrs[0] = "supportedControl";
5827c478bd9Sstevel@tonic-gate 	attrs[1] = "supportedsaslmechanisms";
5837c478bd9Sstevel@tonic-gate 	attrs[2] = NULL;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&serverInfo->mutex[1]);
5867c478bd9Sstevel@tonic-gate 	serverInfo->sinfo[1].server_status = INFO_SERVER_CONNECTING;
5877c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&serverInfo->mutex[1]);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	tv.tv_sec = search_timeout;
5907c478bd9Sstevel@tonic-gate 	tv.tv_usec = 0;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	rc = ldap_search_ext_s(ld, "", LDAP_SCOPE_BASE,
5937c478bd9Sstevel@tonic-gate 	    "(objectclass=*)",
5947c478bd9Sstevel@tonic-gate 	    attrs, 0, NULL, NULL, &tv, 0, &resultMsg);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	switch (rc) {
5977c478bd9Sstevel@tonic-gate 		/* If successful, the root DSE was found. */
5987c478bd9Sstevel@tonic-gate 		case LDAP_SUCCESS:
5997c478bd9Sstevel@tonic-gate 			break;
6007c478bd9Sstevel@tonic-gate 		/*
6017c478bd9Sstevel@tonic-gate 		 * If the root DSE was not found, the server does
6027c478bd9Sstevel@tonic-gate 		 * not comply with the LDAP v3 protocol.
6037c478bd9Sstevel@tonic-gate 		 */
6047c478bd9Sstevel@tonic-gate 		default:
6057c478bd9Sstevel@tonic-gate 			ldap_get_option(ld,
6067c478bd9Sstevel@tonic-gate 			    LDAP_OPT_ERROR_NUMBER, &ldaperrno);
6077c478bd9Sstevel@tonic-gate 			(void) snprintf(errmsg, sizeof (errmsg),
6087c478bd9Sstevel@tonic-gate 			    gettext(ldap_err2string(ldaperrno)));
6097c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
6107c478bd9Sstevel@tonic-gate 			logit("getldap_get_rootDSE: Root DSE not found."
6117c478bd9Sstevel@tonic-gate 			    " %s is not an LDAPv3 server (%s).\n",
6127c478bd9Sstevel@tonic-gate 			    serverInfo->sinfo[1].addr, errmsg);
6137c478bd9Sstevel@tonic-gate 			}
6147c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&serverInfo->mutex[1]);
6157c478bd9Sstevel@tonic-gate 			serverInfo->sinfo[1].errormsg
6167c478bd9Sstevel@tonic-gate 			    = strdup(errmsg);
6177c478bd9Sstevel@tonic-gate 			serverInfo->sinfo[1].info_status
6187c478bd9Sstevel@tonic-gate 			    = INFO_STATUS_ERROR;
6197c478bd9Sstevel@tonic-gate 			serverInfo->sinfo[1].server_status
6207c478bd9Sstevel@tonic-gate 			    = INFO_SERVER_ERROR;
6217c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&serverInfo->mutex[1]);
6227c478bd9Sstevel@tonic-gate 			if (resultMsg)
6237c478bd9Sstevel@tonic-gate 				ldap_msgfree(resultMsg);
6247c478bd9Sstevel@tonic-gate 			ldap_unbind(ld);
6257c478bd9Sstevel@tonic-gate 			/*
6267c478bd9Sstevel@tonic-gate 			 * sync sinfo copies in the serverInfo.
6277c478bd9Sstevel@tonic-gate 			 * protected by mutex
6287c478bd9Sstevel@tonic-gate 			 */
6297c478bd9Sstevel@tonic-gate 			sync_current_with_update_copy(serverInfo);
6307c478bd9Sstevel@tonic-gate 			thr_exit((void *) -1);
6317c478bd9Sstevel@tonic-gate 			break;
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if ((e = ldap_first_entry(ld, resultMsg)) != NULL) {
6367c478bd9Sstevel@tonic-gate 		/* calculate length of root DSE data */
6377c478bd9Sstevel@tonic-gate 		for (a = ldap_first_attribute(ld, e, &ber);
6387c478bd9Sstevel@tonic-gate 		    a != NULL;
6397c478bd9Sstevel@tonic-gate 		    a = ldap_next_attribute(ld, e, ber)) {
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 			if ((vals = ldap_get_values(ld, e, a)) != NULL) {
6427c478bd9Sstevel@tonic-gate 				for (i = 0; vals[i] != NULL; i++) {
6437c478bd9Sstevel@tonic-gate 					len +=  strlen(a) +
6447c478bd9Sstevel@tonic-gate 					    strlen(vals[i]) +
6457c478bd9Sstevel@tonic-gate 					    strlen(DOORLINESEP) +1;
6467c478bd9Sstevel@tonic-gate 				}
6477c478bd9Sstevel@tonic-gate 				ldap_value_free(vals);
6487c478bd9Sstevel@tonic-gate 			}
6497c478bd9Sstevel@tonic-gate 			ldap_memfree(a);
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		if (ber != NULL)
6527c478bd9Sstevel@tonic-gate 			ber_free(ber, 0);
6537c478bd9Sstevel@tonic-gate 		/* copy root DSE data */
6547c478bd9Sstevel@tonic-gate 		if (len) {
6557c478bd9Sstevel@tonic-gate 			/* add 1 for the last '\0' */
6567c478bd9Sstevel@tonic-gate 			rootDSE  = (char *)malloc(len + 1);
6577c478bd9Sstevel@tonic-gate 			if (rootDSE != NULL) {
6587c478bd9Sstevel@tonic-gate 				/* make it an empty string first */
6597c478bd9Sstevel@tonic-gate 				*rootDSE = '\0';
6607c478bd9Sstevel@tonic-gate 				for (a = ldap_first_attribute(ld, e, &ber);
6617c478bd9Sstevel@tonic-gate 				    a != NULL;
6627c478bd9Sstevel@tonic-gate 				    a = ldap_next_attribute(
6637c478bd9Sstevel@tonic-gate 				    ld, e, ber)) {
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 					if ((vals = ldap_get_values(
6667c478bd9Sstevel@tonic-gate 					    ld, e, a)) != NULL) {
6677c478bd9Sstevel@tonic-gate 						for (i = 0; vals[i] != NULL;
6687c478bd9Sstevel@tonic-gate 						    i++) {
6697c478bd9Sstevel@tonic-gate 							int len;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 							len = strlen(a) +
6727c478bd9Sstevel@tonic-gate 							    strlen(vals[i]) +
673*4a6b6ac4Schinlong 							    strlen(DOORLINESEP)
674*4a6b6ac4Schinlong 							    + 2;
6757c478bd9Sstevel@tonic-gate 							(void) snprintf(
6767c478bd9Sstevel@tonic-gate 							    rootDSE +
6777c478bd9Sstevel@tonic-gate 							    strlen(rootDSE),
6787c478bd9Sstevel@tonic-gate 							    len, "%s=%s%s",
6797c478bd9Sstevel@tonic-gate 							    a, vals[i],
6807c478bd9Sstevel@tonic-gate 							    DOORLINESEP);
6817c478bd9Sstevel@tonic-gate 						}
6827c478bd9Sstevel@tonic-gate 						ldap_value_free(vals);
6837c478bd9Sstevel@tonic-gate 					}
6847c478bd9Sstevel@tonic-gate 					ldap_memfree(a);
6857c478bd9Sstevel@tonic-gate 				}
6867c478bd9Sstevel@tonic-gate 				if (ber != NULL)
6877c478bd9Sstevel@tonic-gate 					ber_free(ber, 0);
6887c478bd9Sstevel@tonic-gate 			} else
6897c478bd9Sstevel@tonic-gate 				len = 0;
6907c478bd9Sstevel@tonic-gate 		}
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/* error, if no root DSE data */
6947c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&serverInfo->mutex[1]);
6957c478bd9Sstevel@tonic-gate 	if (len == 0) {
6967c478bd9Sstevel@tonic-gate 		serverInfo->sinfo[1].errormsg =
6977c478bd9Sstevel@tonic-gate 		    strdup(gettext("No root DSE data returned."));
6987c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
6997c478bd9Sstevel@tonic-gate 			logit("getldap_get_rootDSE: %s.\n",
7007c478bd9Sstevel@tonic-gate 			    serverInfo->sinfo[1].errormsg);
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 		serverInfo->sinfo[1].type
7037c478bd9Sstevel@tonic-gate 		    = INFO_RW_UNKNOWN;
7047c478bd9Sstevel@tonic-gate 		serverInfo->sinfo[1].info_status
7057c478bd9Sstevel@tonic-gate 		    = INFO_STATUS_ERROR;
7067c478bd9Sstevel@tonic-gate 		serverInfo->sinfo[1].server_status 	= INFO_SERVER_ERROR;
7077c478bd9Sstevel@tonic-gate 		exitrc = -1;
7087c478bd9Sstevel@tonic-gate 	} else {
7097c478bd9Sstevel@tonic-gate 		/* assume writeable, i.e., can do modify */
7107c478bd9Sstevel@tonic-gate 		serverInfo->sinfo[1].type	= INFO_RW_WRITEABLE;
7117c478bd9Sstevel@tonic-gate 		serverInfo->sinfo[1].server_status
7127c478bd9Sstevel@tonic-gate 		    = INFO_SERVER_UP;
7137c478bd9Sstevel@tonic-gate 		serverInfo->sinfo[1].info_status	= INFO_STATUS_NEW;
7147c478bd9Sstevel@tonic-gate 		/* remove the last DOORLINESEP */
7157c478bd9Sstevel@tonic-gate 		*(rootDSE+strlen(rootDSE)-1) = '\0';
7167c478bd9Sstevel@tonic-gate 		serverInfo->sinfo[1].rootDSE_data = rootDSE;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 		server_found = 1;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 		exitrc = NS_LDAP_SUCCESS;
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&serverInfo->mutex[1]);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (resultMsg)
7257c478bd9Sstevel@tonic-gate 		ldap_msgfree(resultMsg);
7267c478bd9Sstevel@tonic-gate 	ldap_unbind(ld);
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * sync sinfo copies in the serverInfo.
7307c478bd9Sstevel@tonic-gate 	 * protected by mutex
7317c478bd9Sstevel@tonic-gate 	 */
7327c478bd9Sstevel@tonic-gate 	sync_current_with_update_copy(serverInfo);
7337c478bd9Sstevel@tonic-gate 	/*
7347c478bd9Sstevel@tonic-gate 	 * signal that the ldap_cachemgr parent process
7357c478bd9Sstevel@tonic-gate 	 * should exit now, if it is still waiting
7367c478bd9Sstevel@tonic-gate 	 */
7377c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&sig_mutex);
7387c478bd9Sstevel@tonic-gate 	if (signal_done == FALSE && server_found) {
7397c478bd9Sstevel@tonic-gate 		ppid = getppid();
7407c478bd9Sstevel@tonic-gate 		(void) kill(ppid, SIGUSR1);
7417c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
7427c478bd9Sstevel@tonic-gate 			logit("getldap_get_rootDSE(): "
7437c478bd9Sstevel@tonic-gate 			    "SIGUSR1 signal sent to "
7447c478bd9Sstevel@tonic-gate 			    "parent process(%ld).\n", ppid);
7457c478bd9Sstevel@tonic-gate 		}
7467c478bd9Sstevel@tonic-gate 		signal_done = TRUE;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&sig_mutex);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	thr_exit((void *) exitrc);
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	return ((void *) NULL);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate static int
7567c478bd9Sstevel@tonic-gate getldap_init_serverInfo(server_info_t **head)
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	char		**servers = NULL;
7597c478bd9Sstevel@tonic-gate 	int		rc = 0, i, exitrc = NS_LDAP_SUCCESS;
7607c478bd9Sstevel@tonic-gate 	ns_ldap_error_t *errorp = NULL;
7617c478bd9Sstevel@tonic-gate 	server_info_t	*info, *tail = NULL;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	*head = NULL;
7647c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
7657c478bd9Sstevel@tonic-gate 		logit("getldap_init_serverInfo()...\n");
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 	rc = __s_api_getServers(&servers, &errorp);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	if (rc != NS_LDAP_SUCCESS) {
7707c478bd9Sstevel@tonic-gate 		logit("getldap_init_serverInfo: "
7717c478bd9Sstevel@tonic-gate 		    "__s_api_getServers failed.\n");
7727c478bd9Sstevel@tonic-gate 		if (errorp)
7737c478bd9Sstevel@tonic-gate 			__ns_ldap_freeError(&errorp);
7747c478bd9Sstevel@tonic-gate 		return (-1);
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 	for (i = 0; servers[i] != NULL; i++) {
7777c478bd9Sstevel@tonic-gate 		info = (server_info_t *)calloc(1, sizeof (server_info_t));
7787c478bd9Sstevel@tonic-gate 		if (info == NULL) {
7797c478bd9Sstevel@tonic-gate 			logit("getldap_init_serverInfo: "
7807c478bd9Sstevel@tonic-gate 			    "not enough memory.\n");
7817c478bd9Sstevel@tonic-gate 			exitrc = NS_LDAP_MEMORY;
7827c478bd9Sstevel@tonic-gate 			break;
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 		if (i == 0) {
7857c478bd9Sstevel@tonic-gate 			*head = info;
7867c478bd9Sstevel@tonic-gate 			tail  = info;
7877c478bd9Sstevel@tonic-gate 		} else {
7887c478bd9Sstevel@tonic-gate 			tail->next = info;
7897c478bd9Sstevel@tonic-gate 			tail  = info;
7907c478bd9Sstevel@tonic-gate 		}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 		info->sinfo[0].addr		= strdup(servers[i]);
7937c478bd9Sstevel@tonic-gate 		if (info->sinfo[0].addr == NULL) {
7947c478bd9Sstevel@tonic-gate 			logit("getldap_init_serverInfo: "
7957c478bd9Sstevel@tonic-gate 			    "not enough memory.\n");
7967c478bd9Sstevel@tonic-gate 			exitrc = NS_LDAP_MEMORY;
7977c478bd9Sstevel@tonic-gate 			break;
7987c478bd9Sstevel@tonic-gate 		}
7997c478bd9Sstevel@tonic-gate 		info->sinfo[1].addr		= strdup(servers[i]);
8007c478bd9Sstevel@tonic-gate 		if (info->sinfo[1].addr == NULL) {
8017c478bd9Sstevel@tonic-gate 			logit("getldap_init_serverInfo: "
8027c478bd9Sstevel@tonic-gate 			    "not enough memory.\n");
8037c478bd9Sstevel@tonic-gate 			exitrc = NS_LDAP_MEMORY;
8047c478bd9Sstevel@tonic-gate 			break;
8057c478bd9Sstevel@tonic-gate 		}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		info->sinfo[0].type 		= INFO_RW_UNKNOWN;
8087c478bd9Sstevel@tonic-gate 		info->sinfo[1].type 		= INFO_RW_UNKNOWN;
8097c478bd9Sstevel@tonic-gate 		info->sinfo[0].info_status	= INFO_STATUS_UNKNOWN;
8107c478bd9Sstevel@tonic-gate 		info->sinfo[1].info_status	= INFO_STATUS_UNKNOWN;
8117c478bd9Sstevel@tonic-gate 		info->sinfo[0].server_status	= INFO_SERVER_UNKNOWN;
8127c478bd9Sstevel@tonic-gate 		info->sinfo[1].server_status	= INFO_SERVER_UNKNOWN;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 		/*
8157c478bd9Sstevel@tonic-gate 		 * Assume at startup or after the configuration
8167c478bd9Sstevel@tonic-gate 		 * profile is refreshed, all servers are good.
8177c478bd9Sstevel@tonic-gate 		 */
8187c478bd9Sstevel@tonic-gate 		info->sinfo[0].prev_server_status =
8197c478bd9Sstevel@tonic-gate 		    INFO_SERVER_UP;
8207c478bd9Sstevel@tonic-gate 		info->sinfo[1].prev_server_status =
8217c478bd9Sstevel@tonic-gate 		    INFO_SERVER_UP;
822cb5caa98Sdjl 		info->sinfo[0].hostname		= NULL;
823cb5caa98Sdjl 		info->sinfo[1].hostname		= NULL;
8247c478bd9Sstevel@tonic-gate 		info->sinfo[0].rootDSE_data	= NULL;
8257c478bd9Sstevel@tonic-gate 		info->sinfo[1].rootDSE_data	= NULL;
8267c478bd9Sstevel@tonic-gate 		info->sinfo[0].errormsg 	= NULL;
8277c478bd9Sstevel@tonic-gate 		info->sinfo[1].errormsg 	= NULL;
8287c478bd9Sstevel@tonic-gate 		info->next 		= NULL;
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 	__s_api_free2dArray(servers);
8317c478bd9Sstevel@tonic-gate 	if (exitrc != NS_LDAP_SUCCESS) {
8327c478bd9Sstevel@tonic-gate 		if (head && *head) {
8337c478bd9Sstevel@tonic-gate 			(void) getldap_destroy_serverInfo(*head);
8347c478bd9Sstevel@tonic-gate 			*head = NULL;
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 	return (exitrc);
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate static int
8417c478bd9Sstevel@tonic-gate getldap_destroy_serverInfo(server_info_t *head)
8427c478bd9Sstevel@tonic-gate {
8437c478bd9Sstevel@tonic-gate 	server_info_t	*info, *next;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
8467c478bd9Sstevel@tonic-gate 		logit("getldap_destroy_serverInfo()...\n");
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	if (head == NULL) {
8507c478bd9Sstevel@tonic-gate 		logit("getldap_destroy_serverInfo: "
8517c478bd9Sstevel@tonic-gate 		    "invalid serverInfo list.\n");
8527c478bd9Sstevel@tonic-gate 		return (-1);
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	for (info = head; info; info = next) {
8567c478bd9Sstevel@tonic-gate 		if (info->sinfo[0].addr)
8577c478bd9Sstevel@tonic-gate 			free(info->sinfo[0].addr);
8587c478bd9Sstevel@tonic-gate 		if (info->sinfo[1].addr)
8597c478bd9Sstevel@tonic-gate 			free(info->sinfo[1].addr);
860cb5caa98Sdjl 		if (info->sinfo[0].hostname)
861cb5caa98Sdjl 			free(info->sinfo[0].hostname);
862cb5caa98Sdjl 		if (info->sinfo[1].hostname)
863cb5caa98Sdjl 			free(info->sinfo[1].hostname);
8647c478bd9Sstevel@tonic-gate 		if (info->sinfo[0].rootDSE_data)
8657c478bd9Sstevel@tonic-gate 			free(info->sinfo[0].rootDSE_data);
8667c478bd9Sstevel@tonic-gate 		if (info->sinfo[1].rootDSE_data)
8677c478bd9Sstevel@tonic-gate 			free(info->sinfo[1].rootDSE_data);
8687c478bd9Sstevel@tonic-gate 		if (info->sinfo[0].errormsg)
8697c478bd9Sstevel@tonic-gate 			free(info->sinfo[0].errormsg);
8707c478bd9Sstevel@tonic-gate 		if (info->sinfo[1].errormsg)
8717c478bd9Sstevel@tonic-gate 			free(info->sinfo[1].errormsg);
8727c478bd9Sstevel@tonic-gate 		next = info->next;
8737c478bd9Sstevel@tonic-gate 		free(info);
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate static int
8797c478bd9Sstevel@tonic-gate getldap_set_serverInfo(server_info_t *head,
8807c478bd9Sstevel@tonic-gate 		int reset_bindtime)
8817c478bd9Sstevel@tonic-gate {
8827c478bd9Sstevel@tonic-gate 	server_info_t	*info;
8837c478bd9Sstevel@tonic-gate 	int 		atleast1 = 0;
8847c478bd9Sstevel@tonic-gate 	thread_t	*tid;
8857c478bd9Sstevel@tonic-gate 	int 		num_threads = 0, i, j;
8867c478bd9Sstevel@tonic-gate 	void		*status;
8877c478bd9Sstevel@tonic-gate 	void		**paramVal = NULL;
8887c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	*error = NULL;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
8917c478bd9Sstevel@tonic-gate 		logit("getldap_set_serverInfo()...\n");
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	if (head == NULL) {
8957c478bd9Sstevel@tonic-gate 		logit("getldap_set_serverInfo: "
8967c478bd9Sstevel@tonic-gate 		    "invalid serverInfo list.\n");
8977c478bd9Sstevel@tonic-gate 		return (-1);
8987c478bd9Sstevel@tonic-gate 	}
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	/* Get the bind timeout value */
9017c478bd9Sstevel@tonic-gate 	if (reset_bindtime == 1) {
9027c478bd9Sstevel@tonic-gate 		tcptimeout = NS_DEFAULT_BIND_TIMEOUT * 1000;
9037c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_getParam(NS_LDAP_BIND_TIME_P,
9047c478bd9Sstevel@tonic-gate 		    &paramVal, &error);
9057c478bd9Sstevel@tonic-gate 		if (paramVal != NULL && *paramVal != NULL) {
9067c478bd9Sstevel@tonic-gate 			/* convert to milliseconds */
9077c478bd9Sstevel@tonic-gate 			tcptimeout = **((int **)paramVal);
9087c478bd9Sstevel@tonic-gate 			tcptimeout *= 1000;
9097c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeParam(&paramVal);
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 		if (error)
9127c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&error);
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 		/* get search timeout value */
9157c478bd9Sstevel@tonic-gate 		search_timeout = NS_DEFAULT_SEARCH_TIMEOUT;
9167c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_getParam(NS_LDAP_SEARCH_TIME_P,
9177c478bd9Sstevel@tonic-gate 		    &paramVal, &error);
9187c478bd9Sstevel@tonic-gate 		if (paramVal != NULL && *paramVal != NULL) {
9197c478bd9Sstevel@tonic-gate 			search_timeout = **((int **)paramVal);
9207c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeParam(&paramVal);
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 		if (error)
9237c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&error);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	for (info = head; info; info = info->next)
9287c478bd9Sstevel@tonic-gate 		num_threads++;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	if (num_threads == 0) {
9317c478bd9Sstevel@tonic-gate 		logit("getldap_set_serverInfo: "
9327c478bd9Sstevel@tonic-gate 		    "empty serverInfo list.\n");
9337c478bd9Sstevel@tonic-gate 		return (-1);
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	tid = (thread_t *) calloc(1, sizeof (thread_t) * num_threads);
9377c478bd9Sstevel@tonic-gate 	if (tid == NULL) {
9387c478bd9Sstevel@tonic-gate 		logit("getldap_set_serverInfo: "
9397c478bd9Sstevel@tonic-gate 		    "No memory to create thread ID list.\n");
9407c478bd9Sstevel@tonic-gate 		return (-1);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	for (info = head, i = 0; info; info = info->next, i++) {
9447c478bd9Sstevel@tonic-gate 		if (thr_create(NULL, 0,
9457c478bd9Sstevel@tonic-gate 		    (void *(*)(void*))getldap_get_rootDSE,
9467c478bd9Sstevel@tonic-gate 		    (void *)info, 0, &tid[i])) {
9477c478bd9Sstevel@tonic-gate 			logit("getldap_set_serverInfo: "
9487c478bd9Sstevel@tonic-gate 			    "can not create thread %d.\n", i + 1);
9497c478bd9Sstevel@tonic-gate 			for (j = 0; j < i; j++)
9507c478bd9Sstevel@tonic-gate 				(void) thr_join(tid[j], NULL, NULL);
9517c478bd9Sstevel@tonic-gate 			free(tid);
9527c478bd9Sstevel@tonic-gate 			return (-1);
9537c478bd9Sstevel@tonic-gate 		}
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_threads; i++) {
9577c478bd9Sstevel@tonic-gate 		if (thr_join(tid[i], NULL, &status) == 0) {
9587c478bd9Sstevel@tonic-gate 			if ((int)status == NS_LDAP_SUCCESS)
9597c478bd9Sstevel@tonic-gate 				atleast1 = 1;
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	free(tid);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if (atleast1)
9667c478bd9Sstevel@tonic-gate 		return (NS_LDAP_SUCCESS);
9677c478bd9Sstevel@tonic-gate 	else
9687c478bd9Sstevel@tonic-gate 		return (-1);
9697c478bd9Sstevel@tonic-gate }
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate /*
972cb5caa98Sdjl  * Convert an IP to a host name
973cb5caa98Sdjl  */
974cb5caa98Sdjl static int
975cb5caa98Sdjl getldap_ip2hostname(char *ipaddr, char **hostname) {
976cb5caa98Sdjl 	struct in_addr	in;
977cb5caa98Sdjl 	struct in6_addr	in6;
978cb5caa98Sdjl 	struct hostent	*hp = NULL;
979cb5caa98Sdjl 	char	*start = NULL, *end = NULL, delim = '\0';
980cb5caa98Sdjl 	char	*port = NULL, *addr = NULL;
981cb5caa98Sdjl 	int	error_num = 0, len = 0;
982cb5caa98Sdjl 
983cb5caa98Sdjl 	if (ipaddr == NULL || hostname == NULL)
984cb5caa98Sdjl 		return (NS_LDAP_INVALID_PARAM);
985cb5caa98Sdjl 	*hostname = NULL;
986cb5caa98Sdjl 	if ((addr = strdup(ipaddr)) == NULL)
987cb5caa98Sdjl 		return (NS_LDAP_MEMORY);
988cb5caa98Sdjl 
989cb5caa98Sdjl 	if (addr[0] == '[') {
990cb5caa98Sdjl 		/*
991cb5caa98Sdjl 		 * Assume it's [ipv6]:port
992cb5caa98Sdjl 		 * Extract ipv6 IP
993cb5caa98Sdjl 		 */
994cb5caa98Sdjl 		start = &addr[1];
995cb5caa98Sdjl 		if ((end = strchr(addr, ']')) != NULL) {
996cb5caa98Sdjl 			*end = '\0';
997cb5caa98Sdjl 			delim = ']';
998cb5caa98Sdjl 			if (*(end + 1) == ':')
999cb5caa98Sdjl 				/* extract port */
1000cb5caa98Sdjl 				port = end + 2;
1001cb5caa98Sdjl 		} else {
1002cb5caa98Sdjl 			return (NS_LDAP_INVALID_PARAM);
1003cb5caa98Sdjl 		}
1004cb5caa98Sdjl 	} else if ((end = strchr(addr, ':')) != NULL) {
1005cb5caa98Sdjl 		/* assume it's ipv4:port */
1006cb5caa98Sdjl 		*end = '\0';
1007cb5caa98Sdjl 		delim = ':';
1008cb5caa98Sdjl 		start = addr;
1009cb5caa98Sdjl 		port = end + 1;
1010cb5caa98Sdjl 	} else
1011cb5caa98Sdjl 		/* No port */
1012cb5caa98Sdjl 		start = addr;
1013cb5caa98Sdjl 
1014cb5caa98Sdjl 
1015cb5caa98Sdjl 	if (inet_pton(AF_INET, start, &in) == 1) {
1016cb5caa98Sdjl 		/* IPv4 */
1017cb5caa98Sdjl 		hp = getipnodebyaddr((char *)&in,
1018cb5caa98Sdjl 			sizeof (struct in_addr), AF_INET, &error_num);
1019cb5caa98Sdjl 		if (hp && hp->h_name) {
1020cb5caa98Sdjl 			/* hostname + '\0' */
1021cb5caa98Sdjl 			len = strlen(hp->h_name) + 1;
1022cb5caa98Sdjl 			if (port)
1023cb5caa98Sdjl 				/* ':' + port */
1024cb5caa98Sdjl 				len += strlen(port) + 1;
1025cb5caa98Sdjl 			if ((*hostname = malloc(len)) == NULL) {
1026cb5caa98Sdjl 				free(addr);
1027cb5caa98Sdjl 				freehostent(hp);
1028cb5caa98Sdjl 				return (NS_LDAP_MEMORY);
1029cb5caa98Sdjl 			}
1030cb5caa98Sdjl 
1031cb5caa98Sdjl 			if (port)
1032cb5caa98Sdjl 				(void) snprintf(*hostname, len, "%s:%s",
1033cb5caa98Sdjl 						hp->h_name, port);
1034cb5caa98Sdjl 			else
1035cb5caa98Sdjl 				(void) strlcpy(*hostname, hp->h_name, len);
1036cb5caa98Sdjl 
1037cb5caa98Sdjl 			free(addr);
1038cb5caa98Sdjl 			freehostent(hp);
1039cb5caa98Sdjl 			return (NS_LDAP_SUCCESS);
1040cb5caa98Sdjl 		} else {
1041cb5caa98Sdjl 			return (NS_LDAP_NOTFOUND);
1042cb5caa98Sdjl 		}
1043cb5caa98Sdjl 	} else if (inet_pton(AF_INET6, start, &in6) == 1) {
1044cb5caa98Sdjl 		/* IPv6 */
1045cb5caa98Sdjl 		hp = getipnodebyaddr((char *)&in6,
1046cb5caa98Sdjl 			sizeof (struct in6_addr), AF_INET6, &error_num);
1047cb5caa98Sdjl 		if (hp && hp->h_name) {
1048cb5caa98Sdjl 			/* hostname + '\0' */
1049cb5caa98Sdjl 			len = strlen(hp->h_name) + 1;
1050cb5caa98Sdjl 			if (port)
1051cb5caa98Sdjl 				/* ':' + port */
1052cb5caa98Sdjl 				len += strlen(port) + 1;
1053cb5caa98Sdjl 			if ((*hostname = malloc(len)) == NULL) {
1054cb5caa98Sdjl 				free(addr);
1055cb5caa98Sdjl 				freehostent(hp);
1056cb5caa98Sdjl 				return (NS_LDAP_MEMORY);
1057cb5caa98Sdjl 			}
1058cb5caa98Sdjl 
1059cb5caa98Sdjl 			if (port)
1060cb5caa98Sdjl 				(void) snprintf(*hostname, len, "%s:%s",
1061cb5caa98Sdjl 						hp->h_name, port);
1062cb5caa98Sdjl 			else
1063cb5caa98Sdjl 				(void) strlcpy(*hostname, hp->h_name, len);
1064cb5caa98Sdjl 
1065cb5caa98Sdjl 			free(addr);
1066cb5caa98Sdjl 			freehostent(hp);
1067cb5caa98Sdjl 			return (NS_LDAP_SUCCESS);
1068cb5caa98Sdjl 		} else {
1069cb5caa98Sdjl 			return (NS_LDAP_NOTFOUND);
1070cb5caa98Sdjl 		}
1071cb5caa98Sdjl 	} else {
1072cb5caa98Sdjl 		/*
1073cb5caa98Sdjl 		 * A hostname
1074cb5caa98Sdjl 		 * Return it as is
1075cb5caa98Sdjl 		 */
1076cb5caa98Sdjl 		if (end)
1077cb5caa98Sdjl 			*end = delim;
1078cb5caa98Sdjl 		*hostname = addr;
1079cb5caa98Sdjl 		return (NS_LDAP_SUCCESS);
1080cb5caa98Sdjl 	}
1081cb5caa98Sdjl }
1082cb5caa98Sdjl /*
10837c478bd9Sstevel@tonic-gate  * getldap_get_serverInfo processes the GETLDAPSERVER door request passed
10847c478bd9Sstevel@tonic-gate  * to this function from getldap_serverInfo_op().
10857c478bd9Sstevel@tonic-gate  * input:
10867c478bd9Sstevel@tonic-gate  *   a buffer containing an empty string (e.g., input[0]='\0';) or a string
1087cb5caa98Sdjl  *   as the "input" in printf(input, "%s%s%s%s", req, addrtype, DOORLINESEP,
1088cb5caa98Sdjl  *   addr);
10897c478bd9Sstevel@tonic-gate  *   where addr is the address of a server and
10907c478bd9Sstevel@tonic-gate  *   req is one of the following:
10917c478bd9Sstevel@tonic-gate  *   NS_CACHE_NEW:    send a new server address, addr is ignored.
10927c478bd9Sstevel@tonic-gate  *   NS_CACHE_NORESP: send the next one, remove addr from list.
10937c478bd9Sstevel@tonic-gate  *   NS_CACHE_NEXT:   send the next one, keep addr on list.
10947c478bd9Sstevel@tonic-gate  *   NS_CACHE_WRITE:  send a non-replica server, if possible, if not, same
10957c478bd9Sstevel@tonic-gate  *                    as NS_CACHE_NEXT.
1096cb5caa98Sdjl  *   addrtype:
1097cb5caa98Sdjl  *   NS_CACHE_ADDR_IP: return server address as is, this is default.
1098*4a6b6ac4Schinlong  *   NS_CACHE_ADDR_HOSTNAME: return both server address and its FQDN format,
1099*4a6b6ac4Schinlong  *			only self credential case requires such format.
11007c478bd9Sstevel@tonic-gate  * output:
11017c478bd9Sstevel@tonic-gate  *   a buffer containing server info in the following format:
1102*4a6b6ac4Schinlong  *   serveraddress DOORLINESEP [ serveraddress FQDN DOORLINESEP ]
1103*4a6b6ac4Schinlong  *   [ attr=value [DOORLINESEP attr=value ]...]
1104*4a6b6ac4Schinlong  *   For example: ( here | used as DOORLINESEP for visual purposes)
1105*4a6b6ac4Schinlong  *   1) simple bind and sasl/DIGEST-MD5 bind :
1106*4a6b6ac4Schinlong  *   1.2.3.4|supportedControl=1.1.1.1|supportedSASLmechanisms=EXTERNAL|
1107*4a6b6ac4Schinlong  *   supportedSASLmechanisms=GSSAPI
1108*4a6b6ac4Schinlong  *   2) sasl/GSSAPI bind (self credential):
1109*4a6b6ac4Schinlong  *   1.2.3.4|foo.sun.com|supportedControl=1.1.1.1|
1110*4a6b6ac4Schinlong  *   supportedSASLmechanisms=EXTERNAL|supportedSASLmechanisms=GSSAPI
11117c478bd9Sstevel@tonic-gate  *   NOTE: caller should free this buffer when done using it
11127c478bd9Sstevel@tonic-gate  */
11137c478bd9Sstevel@tonic-gate static int
11147c478bd9Sstevel@tonic-gate getldap_get_serverInfo(server_info_t *head, char *input,
11157c478bd9Sstevel@tonic-gate 		char **output, int *svr_removed)
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate 	server_info_t	*info 	= NULL;
11187c478bd9Sstevel@tonic-gate 	server_info_t	*server	= NULL;
11197c478bd9Sstevel@tonic-gate 	char 		*addr	= NULL;
11207c478bd9Sstevel@tonic-gate 	char 		*req	= NULL;
11217c478bd9Sstevel@tonic-gate 	char 		req_new[] = NS_CACHE_NEW;
1122cb5caa98Sdjl 	char 		addr_type[] = NS_CACHE_ADDR_IP;
1123cb5caa98Sdjl 	int		matched = FALSE, len, rc = 0;
1124*4a6b6ac4Schinlong 	char		*ret_addr = NULL, *ret_addrFQDN = NULL;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
11277c478bd9Sstevel@tonic-gate 		logit("getldap_get_serverInfo()...\n");
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	if (input == NULL || output == NULL) {
11317c478bd9Sstevel@tonic-gate 		logit("getldap_get_serverInfo: "
11327c478bd9Sstevel@tonic-gate 		    "No input or output buffer.\n");
11337c478bd9Sstevel@tonic-gate 		return (-1);
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	*output = NULL;
11377c478bd9Sstevel@tonic-gate 	*svr_removed = FALSE;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	if (head == NULL) {
11407c478bd9Sstevel@tonic-gate 		logit("getldap_get_serverInfo: "
11417c478bd9Sstevel@tonic-gate 		    "invalid serverInfo list.\n");
11427c478bd9Sstevel@tonic-gate 		return (-1);
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 	/*
11457c478bd9Sstevel@tonic-gate 	 * parse the input string to get req and addr,
11467c478bd9Sstevel@tonic-gate 	 * if input is empty, i.e., input[0] == '\0',
11477c478bd9Sstevel@tonic-gate 	 * treat it as an NS_CACHE_NEW request
11487c478bd9Sstevel@tonic-gate 	 */
11497c478bd9Sstevel@tonic-gate 	req = req_new;
11507c478bd9Sstevel@tonic-gate 	if (input[0] != '\0') {
11517c478bd9Sstevel@tonic-gate 		req = input;
1152cb5caa98Sdjl 		/* Save addr type flag */
1153cb5caa98Sdjl 		addr_type[0] = input[1];
11547c478bd9Sstevel@tonic-gate 		input[strlen(NS_CACHE_NEW)] = '\0';
1155cb5caa98Sdjl 		/* skip acion type flag, addr type flag and DOORLINESEP */
1156cb5caa98Sdjl 		addr = input + strlen(DOORLINESEP) + strlen(NS_CACHE_NEW)
1157cb5caa98Sdjl 		    + strlen(NS_CACHE_ADDR_IP);
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 	/*
11607c478bd9Sstevel@tonic-gate 	 * if NS_CACHE_NEW,
11617c478bd9Sstevel@tonic-gate 	 * or the server info is new,
11627c478bd9Sstevel@tonic-gate 	 * starts from the
11637c478bd9Sstevel@tonic-gate 	 * beginning of the list
11647c478bd9Sstevel@tonic-gate 	 */
11657c478bd9Sstevel@tonic-gate 	if ((strcmp(req, NS_CACHE_NEW) == 0) ||
11667c478bd9Sstevel@tonic-gate 	    (head->sinfo[0].info_status == INFO_STATUS_NEW))
11677c478bd9Sstevel@tonic-gate 		matched = TRUE;
11687c478bd9Sstevel@tonic-gate 	for (info = head; info; info = info->next) {
11697c478bd9Sstevel@tonic-gate 		/*
11707c478bd9Sstevel@tonic-gate 		 * make sure the server info stays the same
11717c478bd9Sstevel@tonic-gate 		 * while the data is being processed
11727c478bd9Sstevel@tonic-gate 		 */
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 		/*
11757c478bd9Sstevel@tonic-gate 		 * This function is called to get server info list
11767c478bd9Sstevel@tonic-gate 		 * and pass it back to door call clients.
11777c478bd9Sstevel@tonic-gate 		 * Access the current copy (sinfo[0]) to get such
11787c478bd9Sstevel@tonic-gate 		 * information
11797c478bd9Sstevel@tonic-gate 		 */
11807c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&info->mutex[0]);
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 		if (matched == FALSE &&
11837c478bd9Sstevel@tonic-gate 		    strcmp(info->sinfo[0].addr, addr) == 0) {
11847c478bd9Sstevel@tonic-gate 			matched = TRUE;
11857c478bd9Sstevel@tonic-gate 			if (strcmp(req, NS_CACHE_NORESP) == 0) {
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 				/*
11887c478bd9Sstevel@tonic-gate 				 * if the server has already been removed,
11897c478bd9Sstevel@tonic-gate 				 * don't bother
11907c478bd9Sstevel@tonic-gate 				 */
11917c478bd9Sstevel@tonic-gate 				if (info->sinfo[0].server_status ==
11927c478bd9Sstevel@tonic-gate 				    INFO_SERVER_REMOVED) {
11937c478bd9Sstevel@tonic-gate 					(void) mutex_unlock(&info->mutex[0]);
11947c478bd9Sstevel@tonic-gate 					continue;
11957c478bd9Sstevel@tonic-gate 				}
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 				/*
11987c478bd9Sstevel@tonic-gate 				 * if the information is new,
11997c478bd9Sstevel@tonic-gate 				 * give this server one more chance
12007c478bd9Sstevel@tonic-gate 				 */
12017c478bd9Sstevel@tonic-gate 				if (info->sinfo[0].info_status ==
12027c478bd9Sstevel@tonic-gate 				    INFO_STATUS_NEW &&
12037c478bd9Sstevel@tonic-gate 				    info->sinfo[0].server_status  ==
12047c478bd9Sstevel@tonic-gate 				    INFO_SERVER_UP) {
12057c478bd9Sstevel@tonic-gate 					server = info;
12067c478bd9Sstevel@tonic-gate 					break;
12077c478bd9Sstevel@tonic-gate 				} else {
12087c478bd9Sstevel@tonic-gate 					/*
12097c478bd9Sstevel@tonic-gate 					 * it is recommended that
12107c478bd9Sstevel@tonic-gate 					 * before removing the
12117c478bd9Sstevel@tonic-gate 					 * server from the list,
12127c478bd9Sstevel@tonic-gate 					 * the server should be
12137c478bd9Sstevel@tonic-gate 					 * contacted one more time
12147c478bd9Sstevel@tonic-gate 					 * to make sure that it is
12157c478bd9Sstevel@tonic-gate 					 * really unavailable.
12167c478bd9Sstevel@tonic-gate 					 * For now, just trust the client
12177c478bd9Sstevel@tonic-gate 					 * (i.e., the sldap library)
12187c478bd9Sstevel@tonic-gate 					 * that it knows what it is
12197c478bd9Sstevel@tonic-gate 					 * doing and would not try
12207c478bd9Sstevel@tonic-gate 					 * to mess up the server
12217c478bd9Sstevel@tonic-gate 					 * list.
12227c478bd9Sstevel@tonic-gate 					 */
12237c478bd9Sstevel@tonic-gate 					info->sinfo[0].prev_server_status =
12247c478bd9Sstevel@tonic-gate 					    info->sinfo[0].server_status;
12257c478bd9Sstevel@tonic-gate 					info->sinfo[0].server_status  =
12267c478bd9Sstevel@tonic-gate 					    INFO_SERVER_REMOVED;
12277c478bd9Sstevel@tonic-gate 					/*
12287c478bd9Sstevel@tonic-gate 					 * make sure this will be seen
12297c478bd9Sstevel@tonic-gate 					 * if a user query the server
12307c478bd9Sstevel@tonic-gate 					 * status via the ldap_cachemgr's
12317c478bd9Sstevel@tonic-gate 					 * -g option
12327c478bd9Sstevel@tonic-gate 					 */
12337c478bd9Sstevel@tonic-gate 					info->sinfo[1].server_status  =
12347c478bd9Sstevel@tonic-gate 					    INFO_SERVER_REMOVED;
12357c478bd9Sstevel@tonic-gate 					*svr_removed = TRUE;
12367c478bd9Sstevel@tonic-gate 					(void) mutex_unlock(&info->mutex[0]);
12377c478bd9Sstevel@tonic-gate 					continue;
12387c478bd9Sstevel@tonic-gate 				}
12397c478bd9Sstevel@tonic-gate 			} else {
12407c478bd9Sstevel@tonic-gate 				/*
12417c478bd9Sstevel@tonic-gate 				 * req == NS_CACHE_NEXT or NS_CACHE_WRITE
12427c478bd9Sstevel@tonic-gate 				 */
12437c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&info->mutex[0]);
12447c478bd9Sstevel@tonic-gate 				continue;
12457c478bd9Sstevel@tonic-gate 			}
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 		if (matched) {
12497c478bd9Sstevel@tonic-gate 			if (strcmp(req, NS_CACHE_WRITE) == 0) {
12507c478bd9Sstevel@tonic-gate 				if (info->sinfo[0].type ==
12517c478bd9Sstevel@tonic-gate 				    INFO_RW_WRITEABLE &&
12527c478bd9Sstevel@tonic-gate 				    info->sinfo[0].server_status  ==
12537c478bd9Sstevel@tonic-gate 				    INFO_SERVER_UP) {
12547c478bd9Sstevel@tonic-gate 					server = info;
12557c478bd9Sstevel@tonic-gate 					break;
12567c478bd9Sstevel@tonic-gate 				}
12577c478bd9Sstevel@tonic-gate 			} else if (info->sinfo[0].server_status ==
12587c478bd9Sstevel@tonic-gate 			    INFO_SERVER_UP) {
12597c478bd9Sstevel@tonic-gate 				server = info;
12607c478bd9Sstevel@tonic-gate 				break;
12617c478bd9Sstevel@tonic-gate 			}
12627c478bd9Sstevel@tonic-gate 		}
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&info->mutex[0]);
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if (server) {
1268cb5caa98Sdjl 		if (strcmp(addr_type, NS_CACHE_ADDR_HOSTNAME) == 0) {
1269cb5caa98Sdjl 			/*
1270cb5caa98Sdjl 			 * In SASL/GSSAPI case, a hostname is required for
1271cb5caa98Sdjl 			 * Kerberos's service principal.
1272cb5caa98Sdjl 			 * e.g.
1273cb5caa98Sdjl 			 * ldap/foo.sun.com@SUN.COM
1274cb5caa98Sdjl 			 */
1275cb5caa98Sdjl 			if (server->sinfo[0].hostname == NULL) {
1276cb5caa98Sdjl 				rc = getldap_ip2hostname(server->sinfo[0].addr,
1277cb5caa98Sdjl 				    &server->sinfo[0].hostname);
1278cb5caa98Sdjl 				if (rc != NS_LDAP_SUCCESS) {
1279cb5caa98Sdjl 					(void) mutex_unlock(&info->mutex[0]);
1280cb5caa98Sdjl 					return (rc);
1281cb5caa98Sdjl 				}
1282cb5caa98Sdjl 				if (current_admin.debug_level >= DBG_ALL) {
1283cb5caa98Sdjl 					logit("getldap_get_serverInfo: "
1284cb5caa98Sdjl 					    "%s is converted to %s\n",
1285cb5caa98Sdjl 					    server->sinfo[0].addr,
1286cb5caa98Sdjl 					    server->sinfo[0].hostname);
1287cb5caa98Sdjl 				}
1288cb5caa98Sdjl 			}
1289*4a6b6ac4Schinlong 			ret_addr = server->sinfo[0].addr;
1290*4a6b6ac4Schinlong 			ret_addrFQDN = server->sinfo[0].hostname;
1291cb5caa98Sdjl 
1292cb5caa98Sdjl 		} else
1293cb5caa98Sdjl 			ret_addr = server->sinfo[0].addr;
1294cb5caa98Sdjl 
1295cb5caa98Sdjl 
1296cb5caa98Sdjl 		len = strlen(ret_addr) +
12977c478bd9Sstevel@tonic-gate 		    strlen(server->sinfo[0].rootDSE_data) +
12987c478bd9Sstevel@tonic-gate 		    strlen(DOORLINESEP) + 1;
1299*4a6b6ac4Schinlong 		if (ret_addrFQDN != NULL)
1300*4a6b6ac4Schinlong 			len += strlen(ret_addrFQDN) + strlen(DOORLINESEP);
13017c478bd9Sstevel@tonic-gate 		*output = (char *)malloc(len);
13027c478bd9Sstevel@tonic-gate 		if (*output == NULL) {
13037c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&info->mutex[0]);
13047c478bd9Sstevel@tonic-gate 			return (NS_LDAP_MEMORY);
13057c478bd9Sstevel@tonic-gate 		}
1306*4a6b6ac4Schinlong 		if (ret_addrFQDN == NULL)
13077c478bd9Sstevel@tonic-gate 			(void) snprintf(*output, len, "%s%s%s",
1308cb5caa98Sdjl 			    ret_addr, DOORLINESEP,
13097c478bd9Sstevel@tonic-gate 			    server->sinfo[0].rootDSE_data);
1310*4a6b6ac4Schinlong 		else
1311*4a6b6ac4Schinlong 			(void) snprintf(*output, len, "%s%s%s%s%s",
1312*4a6b6ac4Schinlong 			    ret_addr, DOORLINESEP,
1313*4a6b6ac4Schinlong 			    ret_addrFQDN, DOORLINESEP,
1314*4a6b6ac4Schinlong 			    server->sinfo[0].rootDSE_data);
13157c478bd9Sstevel@tonic-gate 		server->sinfo[0].info_status = INFO_STATUS_OLD;
13167c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&info->mutex[0]);
13177c478bd9Sstevel@tonic-gate 		return (NS_LDAP_SUCCESS);
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 	else
13207c478bd9Sstevel@tonic-gate 		return (-99);
13217c478bd9Sstevel@tonic-gate }
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate /*
13247c478bd9Sstevel@tonic-gate  * Format previous and next refresh time
13257c478bd9Sstevel@tonic-gate  */
13267c478bd9Sstevel@tonic-gate static int
13277c478bd9Sstevel@tonic-gate getldap_format_refresh_time(char **output, time_t *prev, time_t *next)
13287c478bd9Sstevel@tonic-gate {
13297c478bd9Sstevel@tonic-gate #define	TIME_FORMAT	"%Y/%m/%d %H:%M:%S"
13307c478bd9Sstevel@tonic-gate #define	TIME_HEADER1	"  Previous refresh time: "
13317c478bd9Sstevel@tonic-gate #define	TIME_HEADER2	"  Next refresh time:     "
13327c478bd9Sstevel@tonic-gate 	int		hdr1_len = strlen(gettext(TIME_HEADER1));
13337c478bd9Sstevel@tonic-gate 	int		hdr2_len = strlen(gettext(TIME_HEADER2));
13347c478bd9Sstevel@tonic-gate 	struct	tm 	tm;
13357c478bd9Sstevel@tonic-gate 	char		nbuf[256];
13367c478bd9Sstevel@tonic-gate 	char		pbuf[256];
13377c478bd9Sstevel@tonic-gate 	int		len;
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
13407c478bd9Sstevel@tonic-gate 		logit("getldap_format_refresh_time()...\n");
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	*output = NULL;
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	/* format the time of previous refresh  */
13467c478bd9Sstevel@tonic-gate 	if (*prev != 0) {
13477c478bd9Sstevel@tonic-gate 		(void) localtime_r(prev, &tm);
13487c478bd9Sstevel@tonic-gate 		(void) strftime(pbuf, sizeof (pbuf) - 1, TIME_FORMAT, &tm);
13497c478bd9Sstevel@tonic-gate 	} else {
13507c478bd9Sstevel@tonic-gate 		(void) strcpy(pbuf, gettext("NOT DONE"));
13517c478bd9Sstevel@tonic-gate 	}
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	/* format the time of next refresh  */
13547c478bd9Sstevel@tonic-gate 	if (*next != 0) {
13557c478bd9Sstevel@tonic-gate 		(void) localtime_r(next, &tm);
13567c478bd9Sstevel@tonic-gate 		(void) strftime(nbuf, sizeof (nbuf) - 1, TIME_FORMAT, &tm);
13577c478bd9Sstevel@tonic-gate 	} else {
13587c478bd9Sstevel@tonic-gate 		(void) strcpy(nbuf, gettext("NOT SET"));
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	len = hdr1_len + hdr2_len + strlen(nbuf) +
13627c478bd9Sstevel@tonic-gate 	    strlen(pbuf) + 2 * strlen(DOORLINESEP) + 1;
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	*output = malloc(len);
13657c478bd9Sstevel@tonic-gate 	if (*output == NULL)
13667c478bd9Sstevel@tonic-gate 		return (-1);
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	(void) snprintf(*output, len, "%s%s%s%s%s%s",
13697c478bd9Sstevel@tonic-gate 	    gettext(TIME_HEADER1), pbuf, DOORLINESEP,
13707c478bd9Sstevel@tonic-gate 	    gettext(TIME_HEADER2), nbuf, DOORLINESEP);
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate /*
13767c478bd9Sstevel@tonic-gate  * getldap_get_server_stat processes the GETSTAT request passed
13777c478bd9Sstevel@tonic-gate  * to this function from getldap_serverInfo_op().
13787c478bd9Sstevel@tonic-gate  * output:
13797c478bd9Sstevel@tonic-gate  *   a buffer containing info for all the servers.
13807c478bd9Sstevel@tonic-gate  *   For each server, the data is in the following format:
13817c478bd9Sstevel@tonic-gate  *   server: server address or name, status: unknown|up|down|removed DOORLINESEP
13827c478bd9Sstevel@tonic-gate  *   for example: ( here | used as DOORLINESEP for visual purposes)
13837c478bd9Sstevel@tonic-gate  *   server: 1.2.3.4, status: down|server: 2.2.2.2, status: up|
13847c478bd9Sstevel@tonic-gate  *   NOTE: caller should free this buffer when done using it
13857c478bd9Sstevel@tonic-gate  */
13867c478bd9Sstevel@tonic-gate static int
13877c478bd9Sstevel@tonic-gate getldap_get_server_stat(server_info_t *head, char **output,
13887c478bd9Sstevel@tonic-gate 		time_t *prev, time_t *next)
13897c478bd9Sstevel@tonic-gate {
13907c478bd9Sstevel@tonic-gate #define	S_HEADER	"Server information: "
13917c478bd9Sstevel@tonic-gate #define	S_FORMAT	"  server: %s, status: %s%s"
13927c478bd9Sstevel@tonic-gate #define	S_ERROR		"    error message: %s%s"
13937c478bd9Sstevel@tonic-gate 	server_info_t	*info 	= NULL;
13947c478bd9Sstevel@tonic-gate 	int	header_len = strlen(gettext(S_HEADER));
13957c478bd9Sstevel@tonic-gate 	int	format_len = strlen(gettext(S_FORMAT));
13967c478bd9Sstevel@tonic-gate 	int	error_len = strlen(gettext(S_ERROR));
13977c478bd9Sstevel@tonic-gate 	int	len = header_len + strlen(DOORLINESEP);
13987c478bd9Sstevel@tonic-gate 	int	len1 = 0;
13997c478bd9Sstevel@tonic-gate 	char	*status, *output1 = NULL, *tmpptr;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	*output = NULL;
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
14047c478bd9Sstevel@tonic-gate 		logit("getldap_get_server_stat()...\n");
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	if (head == NULL) {
14087c478bd9Sstevel@tonic-gate 		logit("getldap_get_server_stat: "
14097c478bd9Sstevel@tonic-gate 		    "invalid serverInfo list.\n");
14107c478bd9Sstevel@tonic-gate 		return (-1);
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	/* format previous and next refresh time */
14147c478bd9Sstevel@tonic-gate 	(void) getldap_format_refresh_time(&output1, prev, next);
14157c478bd9Sstevel@tonic-gate 	if (output1 == NULL)
14167c478bd9Sstevel@tonic-gate 		return (-1);
14177c478bd9Sstevel@tonic-gate 	len += strlen(output1);
14187c478bd9Sstevel@tonic-gate 	len1 = len + strlen(DOORLINESEP) + 1;
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	*output = (char *)calloc(1, len1);
14217c478bd9Sstevel@tonic-gate 	if (*output == NULL) {
14227c478bd9Sstevel@tonic-gate 		free(output1);
14237c478bd9Sstevel@tonic-gate 		return (-1);
14247c478bd9Sstevel@tonic-gate 	}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	/* insert header string and refresh time info */
14277c478bd9Sstevel@tonic-gate 	(void) snprintf(*output, len1, "%s%s%s",
14287c478bd9Sstevel@tonic-gate 	    gettext(S_HEADER), DOORLINESEP, output1);
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	for (info = head; info; info = info->next) {
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 		/*
14337c478bd9Sstevel@tonic-gate 		 * make sure the server info stays the same
14347c478bd9Sstevel@tonic-gate 		 * while the data is being processed
14357c478bd9Sstevel@tonic-gate 		 */
14367c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&info->mutex[1]);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 		/*
14397c478bd9Sstevel@tonic-gate 		 * When the updating process is under way(getldap_get_rootDSE)
14407c478bd9Sstevel@tonic-gate 		 * the update copy(sinfo[1] is the latest copy.
14417c478bd9Sstevel@tonic-gate 		 * When the updating process
14427c478bd9Sstevel@tonic-gate 		 * is done, the current copy (sinfo[0]) has the latest status,
14437c478bd9Sstevel@tonic-gate 		 * which is still identical to the update copy.
14447c478bd9Sstevel@tonic-gate 		 * So update copy has the latest status.
14457c478bd9Sstevel@tonic-gate 		 * Use the update copy(sinfo[1]) to show status
14467c478bd9Sstevel@tonic-gate 		 * (ldap_cachemgr -g).
14477c478bd9Sstevel@tonic-gate 		 *
14487c478bd9Sstevel@tonic-gate 		 */
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 		switch (info->sinfo[1].server_status) {
14517c478bd9Sstevel@tonic-gate 		case INFO_SERVER_UNKNOWN:
14527c478bd9Sstevel@tonic-gate 			status = gettext("UNKNOWN");
14537c478bd9Sstevel@tonic-gate 			break;
14547c478bd9Sstevel@tonic-gate 		case INFO_SERVER_CONNECTING:
14557c478bd9Sstevel@tonic-gate 			status = gettext("CONNECTING");
14567c478bd9Sstevel@tonic-gate 			break;
14577c478bd9Sstevel@tonic-gate 		case INFO_SERVER_UP:
14587c478bd9Sstevel@tonic-gate 			status = gettext("UP");
14597c478bd9Sstevel@tonic-gate 			break;
14607c478bd9Sstevel@tonic-gate 		case INFO_SERVER_ERROR:
14617c478bd9Sstevel@tonic-gate 			status = gettext("ERROR");
14627c478bd9Sstevel@tonic-gate 			break;
14637c478bd9Sstevel@tonic-gate 		case INFO_SERVER_REMOVED:
14647c478bd9Sstevel@tonic-gate 			status = gettext("REMOVED");
14657c478bd9Sstevel@tonic-gate 			break;
14667c478bd9Sstevel@tonic-gate 		}
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 		len += format_len + strlen(status) +
14697c478bd9Sstevel@tonic-gate 		    strlen(info->sinfo[1].addr) +
14707c478bd9Sstevel@tonic-gate 		    strlen(DOORLINESEP);
14717c478bd9Sstevel@tonic-gate 		if (info->sinfo[1].errormsg != NULL)
14727c478bd9Sstevel@tonic-gate 			len += error_len +
14737c478bd9Sstevel@tonic-gate 			    strlen(info->sinfo[1].errormsg) +
14747c478bd9Sstevel@tonic-gate 			    strlen(DOORLINESEP);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 		tmpptr = (char *)realloc(*output, len);
14777c478bd9Sstevel@tonic-gate 		if (tmpptr == NULL) {
14787c478bd9Sstevel@tonic-gate 			free(output1);
14797c478bd9Sstevel@tonic-gate 			free(*output);
14807c478bd9Sstevel@tonic-gate 			*output = NULL;
14817c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&info->mutex[1]);
14827c478bd9Sstevel@tonic-gate 			return (-1);
14837c478bd9Sstevel@tonic-gate 		} else
14847c478bd9Sstevel@tonic-gate 			*output = tmpptr;
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 		/* insert server IP addr or name and status */
14877c478bd9Sstevel@tonic-gate 		len1 = len - strlen(*output);
14887c478bd9Sstevel@tonic-gate 		(void) snprintf(*output + strlen(*output), len1,
14897c478bd9Sstevel@tonic-gate 		    gettext(S_FORMAT), info->sinfo[1].addr,
14907c478bd9Sstevel@tonic-gate 		    status, DOORLINESEP);
14917c478bd9Sstevel@tonic-gate 		/* insert error message if any */
14927c478bd9Sstevel@tonic-gate 		len1 = len - strlen(*output);
14937c478bd9Sstevel@tonic-gate 		if (info->sinfo[1].errormsg != NULL)
14947c478bd9Sstevel@tonic-gate 			(void) snprintf(*output + strlen(*output), len1,
14957c478bd9Sstevel@tonic-gate 			    gettext(S_ERROR),
14967c478bd9Sstevel@tonic-gate 			    info->sinfo[1].errormsg,
14977c478bd9Sstevel@tonic-gate 			    DOORLINESEP);
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&info->mutex[1]);
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	}
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	free(output1);
15047c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate /*
15087c478bd9Sstevel@tonic-gate  * Format and return the refresh time statistics
15097c478bd9Sstevel@tonic-gate  */
15107c478bd9Sstevel@tonic-gate static int
15117c478bd9Sstevel@tonic-gate getldap_get_refresh_stat(char **output)
15127c478bd9Sstevel@tonic-gate {
15137c478bd9Sstevel@tonic-gate #define	R_HEADER0	"Configuration refresh information: "
15147c478bd9Sstevel@tonic-gate #define	R_HEADER1	"  Configured to NO REFRESH."
15157c478bd9Sstevel@tonic-gate 	int		hdr0_len = strlen(gettext(R_HEADER0));
15167c478bd9Sstevel@tonic-gate 	int		hdr1_len = strlen(gettext(R_HEADER1));
15177c478bd9Sstevel@tonic-gate 	int		cache_ttl = -1, len = 0;
15187c478bd9Sstevel@tonic-gate 	time_t 		expire = 0;
15197c478bd9Sstevel@tonic-gate 	void		**paramVal = NULL;
15207c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	*errorp = NULL;
15217c478bd9Sstevel@tonic-gate 	char		*output1 = NULL;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
15247c478bd9Sstevel@tonic-gate 		logit("getldap_get_refresh_stat()...\n");
15257c478bd9Sstevel@tonic-gate 	}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	*output = NULL;
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	/* get configured cache TTL */
15307c478bd9Sstevel@tonic-gate 	if ((__ns_ldap_getParam(NS_LDAP_CACHETTL_P,
15317c478bd9Sstevel@tonic-gate 	    &paramVal, &errorp) == NS_LDAP_SUCCESS) &&
15327c478bd9Sstevel@tonic-gate 	    paramVal != NULL &&
15337c478bd9Sstevel@tonic-gate 	    (char *)*paramVal != NULL) {
15347c478bd9Sstevel@tonic-gate 			cache_ttl = atol((char *)*paramVal);
15357c478bd9Sstevel@tonic-gate 	} else {
15367c478bd9Sstevel@tonic-gate 		if (errorp)
15377c478bd9Sstevel@tonic-gate 			__ns_ldap_freeError(&errorp);
15387c478bd9Sstevel@tonic-gate 	}
15397c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeParam(&paramVal);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	/* cound not get cache TTL */
15427c478bd9Sstevel@tonic-gate 	if (cache_ttl == -1)
15437c478bd9Sstevel@tonic-gate 		return (-1);
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	if (cache_ttl == 0) {
15467c478bd9Sstevel@tonic-gate 		len = hdr0_len + hdr1_len +
15477c478bd9Sstevel@tonic-gate 		    2 * strlen(DOORLINESEP) + 1;
15487c478bd9Sstevel@tonic-gate 		*output = malloc(len);
15497c478bd9Sstevel@tonic-gate 		if (*output == NULL)
15507c478bd9Sstevel@tonic-gate 			return (-1);
15517c478bd9Sstevel@tonic-gate 		(void) snprintf(*output, len, "%s%s%s%s",
15527c478bd9Sstevel@tonic-gate 		    gettext(R_HEADER0), DOORLINESEP,
15537c478bd9Sstevel@tonic-gate 		    gettext(R_HEADER1), DOORLINESEP);
15547c478bd9Sstevel@tonic-gate 	} else {
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 		/* get configuration expiration time */
15577c478bd9Sstevel@tonic-gate 		if ((__ns_ldap_getParam(NS_LDAP_EXP_P,
15587c478bd9Sstevel@tonic-gate 		    &paramVal, &errorp) == NS_LDAP_SUCCESS) &&
15597c478bd9Sstevel@tonic-gate 		    paramVal != NULL &&
15607c478bd9Sstevel@tonic-gate 		    (char *)*paramVal != NULL) {
15617c478bd9Sstevel@tonic-gate 				expire = (time_t)atol((char *)*paramVal);
15627c478bd9Sstevel@tonic-gate 		} else {
15637c478bd9Sstevel@tonic-gate 			if (errorp)
15647c478bd9Sstevel@tonic-gate 				__ns_ldap_freeError(&errorp);
15657c478bd9Sstevel@tonic-gate 		}
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeParam(&paramVal);
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 		/* cound not get expiration time */
15707c478bd9Sstevel@tonic-gate 		if (expire == -1)
15717c478bd9Sstevel@tonic-gate 			return (-1);
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 		/* format previous and next refresh time */
15747c478bd9Sstevel@tonic-gate 		(void) getldap_format_refresh_time(&output1,
15757c478bd9Sstevel@tonic-gate 		    &prev_refresh_time, &expire);
15767c478bd9Sstevel@tonic-gate 		if (output1 == NULL)
15777c478bd9Sstevel@tonic-gate 			return (-1);
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 		len = hdr0_len + strlen(output1) +
15807c478bd9Sstevel@tonic-gate 		    2 * strlen(DOORLINESEP) + 1;
15817c478bd9Sstevel@tonic-gate 		*output = malloc(len);
15827c478bd9Sstevel@tonic-gate 		if (*output == NULL) {
15837c478bd9Sstevel@tonic-gate 			free(output1);
15847c478bd9Sstevel@tonic-gate 			return (-1);
15857c478bd9Sstevel@tonic-gate 		}
15867c478bd9Sstevel@tonic-gate 		(void) snprintf(*output, len, "%s%s%s%s",
15877c478bd9Sstevel@tonic-gate 		    gettext(R_HEADER0), DOORLINESEP,
15887c478bd9Sstevel@tonic-gate 		    output1, DOORLINESEP);
15897c478bd9Sstevel@tonic-gate 		free(output1);
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate static int
15967c478bd9Sstevel@tonic-gate getldap_get_cacheTTL()
15977c478bd9Sstevel@tonic-gate {
15987c478bd9Sstevel@tonic-gate 	void		**paramVal = NULL;
15997c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	*error;
16007c478bd9Sstevel@tonic-gate 	int		rc = 0, cachettl;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
16047c478bd9Sstevel@tonic-gate 		logit("getldap_get_cacheTTL()....\n");
16057c478bd9Sstevel@tonic-gate 	}
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	if ((rc = __ns_ldap_getParam(NS_LDAP_CACHETTL_P,
16087c478bd9Sstevel@tonic-gate 	    &paramVal, &error)) != NS_LDAP_SUCCESS) {
16097c478bd9Sstevel@tonic-gate 		if (error != NULL && error->message != NULL)
16107c478bd9Sstevel@tonic-gate 			logit("Error: Unable to get configuration "
16117c478bd9Sstevel@tonic-gate 			    "refresh TTL: %s\n",
16127c478bd9Sstevel@tonic-gate 			    error->message);
16137c478bd9Sstevel@tonic-gate 		else {
16147c478bd9Sstevel@tonic-gate 			char *tmp;
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 			__ns_ldap_err2str(rc, &tmp);
16177c478bd9Sstevel@tonic-gate 			logit("Error: Unable to get configuration "
16187c478bd9Sstevel@tonic-gate 			    "refresh TTL: %s\n", tmp);
16197c478bd9Sstevel@tonic-gate 		}
16207c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeParam(&paramVal);
16217c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&error);
16227c478bd9Sstevel@tonic-gate 		return (-1);
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate 	if (paramVal == NULL || (char *)*paramVal == NULL)
16257c478bd9Sstevel@tonic-gate 			return (-1);
16267c478bd9Sstevel@tonic-gate 	cachettl = atol((char *)*paramVal);
16277c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeParam(&paramVal);
16287c478bd9Sstevel@tonic-gate 	return (cachettl);
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate /*
16337c478bd9Sstevel@tonic-gate  * This function implements the adaptive server list refresh
16347c478bd9Sstevel@tonic-gate  * algorithm used by ldap_cachemgr. The idea is to have the
16357c478bd9Sstevel@tonic-gate  * refresh TTL adjust itself between maximum and minimum
16367c478bd9Sstevel@tonic-gate  * values. If the server list has been walked three times
16377c478bd9Sstevel@tonic-gate  * in a row without errors, the TTL will be doubled. This will
16387c478bd9Sstevel@tonic-gate  * be done repeatedly until the maximum value is reached
16397c478bd9Sstevel@tonic-gate  * or passed. If passed, the maximum value will be used.
16407c478bd9Sstevel@tonic-gate  * If any time a server is found to be down/bad, either
16417c478bd9Sstevel@tonic-gate  * after another server list walk or informed by libsldap via
16427c478bd9Sstevel@tonic-gate  * the GETLDAPSERVER door calls, the TTL will be set to half
16437c478bd9Sstevel@tonic-gate  * of its value, again repeatedly, but no less than the minimum
16447c478bd9Sstevel@tonic-gate  * value. Also, at any time, if all the servers on the list
16457c478bd9Sstevel@tonic-gate  * are found to be down/bad, the TTL will be set to minimum,
16467c478bd9Sstevel@tonic-gate  * so that a "no-server" refresh loop should be entered to try
16477c478bd9Sstevel@tonic-gate  * to find a good server as soon as possible. The caller
16487c478bd9Sstevel@tonic-gate  * could check the no_gd_server flag for this situation.
16497c478bd9Sstevel@tonic-gate  * The maximum and minimum values are initialized when the input
16507c478bd9Sstevel@tonic-gate  * refresh_ttl is set to zero, this should occur during
16517c478bd9Sstevel@tonic-gate  * ldap_cachemgr startup or every time the server list is
16527c478bd9Sstevel@tonic-gate  * recreated after the configuration profile is refreshed
16537c478bd9Sstevel@tonic-gate  * from an LDAP server. The maximum is set to the value of
16547c478bd9Sstevel@tonic-gate  * the NS_LDAP_CACHETTL parameter (configuration profile
16557c478bd9Sstevel@tonic-gate  * refresh TTL), but if it is zero (never refreshed) or can
16567c478bd9Sstevel@tonic-gate  * not be retrieved, the maximum is set to the macro
16577c478bd9Sstevel@tonic-gate  * REFRESHTTL_MAX (12 hours) defined below. The minimum is
16587c478bd9Sstevel@tonic-gate  * set to REFRESHTTL_MIN, which is the TCP connection timeout
16597c478bd9Sstevel@tonic-gate  * (tcptimeout) set via the LDAP API ldap_set_option()
16607c478bd9Sstevel@tonic-gate  * with the new LDAP_X_OPT_CONNECT_TIMEOUT option plus 10 seconds.
16617c478bd9Sstevel@tonic-gate  * This accounts for the maximum possible timeout value for an
16627c478bd9Sstevel@tonic-gate  * LDAP TCP connect call.The first refresh TTL, initial value of
16637c478bd9Sstevel@tonic-gate  * refresh_ttl, will be set to the smaller of the two,
16647c478bd9Sstevel@tonic-gate  * REFRESHTTL_REGULAR (10 minutes) or (REFRESHTTL_MAX + REFRESHTTL_MIN)/2.
16657c478bd9Sstevel@tonic-gate  * The idea is to have a low starting value and have the value
16667c478bd9Sstevel@tonic-gate  * stay low if the network/server is unstable, but eventually
16677c478bd9Sstevel@tonic-gate  * the value will move up to maximum and stay there if the
16687c478bd9Sstevel@tonic-gate  * network/server is stable.
16697c478bd9Sstevel@tonic-gate  */
16707c478bd9Sstevel@tonic-gate static int
16717c478bd9Sstevel@tonic-gate getldap_set_refresh_ttl(server_info_t *head, int *refresh_ttl,
16727c478bd9Sstevel@tonic-gate 		int *no_gd_server)
16737c478bd9Sstevel@tonic-gate {
16747c478bd9Sstevel@tonic-gate #define	REFRESHTTL_REGULAR	600
16757c478bd9Sstevel@tonic-gate #define	REFRESHTTL_MAX		43200
16767c478bd9Sstevel@tonic-gate /* tcptimeout is in milliseconds */
16777c478bd9Sstevel@tonic-gate #define	REFRESHTTL_MIN		(tcptimeout/1000) + 10
16787c478bd9Sstevel@tonic-gate #define	UP_REFRESH_TTL_NUM	2
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	static mutex_t		refresh_mutex;
16817c478bd9Sstevel@tonic-gate 	static int		refresh_ttl_max = 0;
16827c478bd9Sstevel@tonic-gate 	static int		refresh_ttl_min = 0;
16837c478bd9Sstevel@tonic-gate 	static int		num_walked_ok = 0;
16847c478bd9Sstevel@tonic-gate 	int			num_servers = 0;
16857c478bd9Sstevel@tonic-gate 	int			num_good_servers = 0;
16867c478bd9Sstevel@tonic-gate 	int			num_prev_good_servers = 0;
16877c478bd9Sstevel@tonic-gate 	server_info_t		*info;
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	/* allow one thread at a time */
16907c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&refresh_mutex);
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
16937c478bd9Sstevel@tonic-gate 		logit("getldap_set_refresh_ttl()...\n");
16947c478bd9Sstevel@tonic-gate 	}
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	if (!head || !refresh_ttl || !no_gd_server) {
16977c478bd9Sstevel@tonic-gate 		logit("getldap_set_refresh_ttl: head is "
16987c478bd9Sstevel@tonic-gate 		    "NULL or refresh_ttl is NULL or "
16997c478bd9Sstevel@tonic-gate 		    "no_gd_server is NULL");
17007c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&refresh_mutex);
17017c478bd9Sstevel@tonic-gate 		return (-1);
17027c478bd9Sstevel@tonic-gate 	}
17037c478bd9Sstevel@tonic-gate 	*no_gd_server = FALSE;
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	/*
17067c478bd9Sstevel@tonic-gate 	 * init max. min. TTLs if first time through or a fresh one
17077c478bd9Sstevel@tonic-gate 	 */
17087c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_SERVER_LIST_REFRESH) {
17097c478bd9Sstevel@tonic-gate 		logit("getldap_set_refresh_ttl:(1) refresh ttl is %d "
17107c478bd9Sstevel@tonic-gate 		    "seconds\n", *refresh_ttl);
17117c478bd9Sstevel@tonic-gate 	}
17127c478bd9Sstevel@tonic-gate 	if (*refresh_ttl == 0) {
17137c478bd9Sstevel@tonic-gate 		num_walked_ok = 0;
17147c478bd9Sstevel@tonic-gate 		/*
17157c478bd9Sstevel@tonic-gate 		 * init cache manager server list TTL:
17167c478bd9Sstevel@tonic-gate 		 *
17177c478bd9Sstevel@tonic-gate 		 * init the min. TTL to
17187c478bd9Sstevel@tonic-gate 		 * REFRESHTTL_MIN ( 2*(TCP MSL) + 10 seconds)
17197c478bd9Sstevel@tonic-gate 		 */
17207c478bd9Sstevel@tonic-gate 		refresh_ttl_min = REFRESHTTL_MIN;
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 		/*
17237c478bd9Sstevel@tonic-gate 		 * try to set the max. TTL to
17247c478bd9Sstevel@tonic-gate 		 * configuration refresh TTL (NS_LDAP_CACHETTL),
17257c478bd9Sstevel@tonic-gate 		 * if error (-1), or never refreshed (0),
17267c478bd9Sstevel@tonic-gate 		 * set it to REFRESHTTL_MAX (12 hours)
17277c478bd9Sstevel@tonic-gate 		 */
17287c478bd9Sstevel@tonic-gate 		refresh_ttl_max = getldap_get_cacheTTL();
17297c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_SERVER_LIST_REFRESH) {
17307c478bd9Sstevel@tonic-gate 			logit("getldap_set_refresh_ttl:(2) refresh ttl is %d "
17317c478bd9Sstevel@tonic-gate 			    "seconds\n", *refresh_ttl);
17327c478bd9Sstevel@tonic-gate 			logit("getldap_set_refresh_ttl:(2) max ttl is %d, "
17337c478bd9Sstevel@tonic-gate 			    "min ttl is %d seconds\n",
17347c478bd9Sstevel@tonic-gate 			    refresh_ttl_max, refresh_ttl_min);
17357c478bd9Sstevel@tonic-gate 		}
17367c478bd9Sstevel@tonic-gate 		if (refresh_ttl_max <= 0)
17377c478bd9Sstevel@tonic-gate 			refresh_ttl_max = REFRESHTTL_MAX;
17387c478bd9Sstevel@tonic-gate 		else if (refresh_ttl_max < refresh_ttl_min)
17397c478bd9Sstevel@tonic-gate 			refresh_ttl_max = refresh_ttl_min;
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 		/*
17427c478bd9Sstevel@tonic-gate 		 * init the first TTL to the smaller of the two:
17437c478bd9Sstevel@tonic-gate 		 * REFRESHTTL_REGULAR ( 10 minutes),
17447c478bd9Sstevel@tonic-gate 		 * (refresh_ttl_max + refresh_ttl_min)/2
17457c478bd9Sstevel@tonic-gate 		 */
17467c478bd9Sstevel@tonic-gate 		*refresh_ttl = REFRESHTTL_REGULAR;
17477c478bd9Sstevel@tonic-gate 		if (*refresh_ttl > (refresh_ttl_max + refresh_ttl_min) / 2)
17487c478bd9Sstevel@tonic-gate 			*refresh_ttl = (refresh_ttl_max + refresh_ttl_min) / 2;
17497c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_SERVER_LIST_REFRESH) {
17507c478bd9Sstevel@tonic-gate 			logit("getldap_set_refresh_ttl:(3) refresh ttl is %d "
17517c478bd9Sstevel@tonic-gate 			    "seconds\n", *refresh_ttl);
17527c478bd9Sstevel@tonic-gate 			logit("getldap_set_refresh_ttl:(3) max ttl is %d, "
17537c478bd9Sstevel@tonic-gate 			    "min ttl is %d seconds\n",
17547c478bd9Sstevel@tonic-gate 			    refresh_ttl_max, refresh_ttl_min);
17557c478bd9Sstevel@tonic-gate 		}
17567c478bd9Sstevel@tonic-gate 	}
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	/*
17597c478bd9Sstevel@tonic-gate 	 * get the servers statistics:
17607c478bd9Sstevel@tonic-gate 	 * number of servers on list
17617c478bd9Sstevel@tonic-gate 	 * number of good servers on list
17627c478bd9Sstevel@tonic-gate 	 * number of pevious good servers on list
17637c478bd9Sstevel@tonic-gate 	 */
17647c478bd9Sstevel@tonic-gate 	for (info = head; info; info = info->next) {
17657c478bd9Sstevel@tonic-gate 		num_servers++;
17667c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&info->mutex[0]);
17677c478bd9Sstevel@tonic-gate 		if (info->sinfo[0].server_status  == INFO_SERVER_UP)
17687c478bd9Sstevel@tonic-gate 			num_good_servers++;
17697c478bd9Sstevel@tonic-gate 		/*
17707c478bd9Sstevel@tonic-gate 		 * Server's previous status could be UNKNOWN
17717c478bd9Sstevel@tonic-gate 		 * only between the very first and second
17727c478bd9Sstevel@tonic-gate 		 * refresh. Treat that UNKNOWN status as up
17737c478bd9Sstevel@tonic-gate 		 */
17747c478bd9Sstevel@tonic-gate 		if (info->sinfo[0].prev_server_status
17757c478bd9Sstevel@tonic-gate 		    == INFO_SERVER_UP ||
17767c478bd9Sstevel@tonic-gate 		    info->sinfo[0].prev_server_status
17777c478bd9Sstevel@tonic-gate 		    == INFO_SERVER_UNKNOWN)
17787c478bd9Sstevel@tonic-gate 			num_prev_good_servers++;
17797c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&info->mutex[0]);
17807c478bd9Sstevel@tonic-gate 	}
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	/*
17837c478bd9Sstevel@tonic-gate 	 * if the server list is walked three times in a row
17847c478bd9Sstevel@tonic-gate 	 * without problems, double the refresh TTL but no more
17857c478bd9Sstevel@tonic-gate 	 * than the max. refresh TTL
17867c478bd9Sstevel@tonic-gate 	 */
17877c478bd9Sstevel@tonic-gate 	if (num_good_servers == num_servers) {
17887c478bd9Sstevel@tonic-gate 		num_walked_ok++;
17897c478bd9Sstevel@tonic-gate 		if (num_walked_ok > UP_REFRESH_TTL_NUM)  {
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 			*refresh_ttl = *refresh_ttl * 2;
17927c478bd9Sstevel@tonic-gate 			if (*refresh_ttl > refresh_ttl_max)
17937c478bd9Sstevel@tonic-gate 				*refresh_ttl = refresh_ttl_max;
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 			num_walked_ok = 0;
17967c478bd9Sstevel@tonic-gate 		}
17977c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_SERVER_LIST_REFRESH) {
17987c478bd9Sstevel@tonic-gate 			logit("getldap_set_refresh_ttl:(4) refresh ttl is %d "
17997c478bd9Sstevel@tonic-gate 			    "seconds\n", *refresh_ttl);
18007c478bd9Sstevel@tonic-gate 		}
18017c478bd9Sstevel@tonic-gate 	} else if (num_good_servers == 0) {
18027c478bd9Sstevel@tonic-gate 		/*
18037c478bd9Sstevel@tonic-gate 		 * if no good server found,
18047c478bd9Sstevel@tonic-gate 		 * set refresh TTL to miminum
18057c478bd9Sstevel@tonic-gate 		 */
18067c478bd9Sstevel@tonic-gate 		*refresh_ttl = refresh_ttl_min;
18077c478bd9Sstevel@tonic-gate 		*no_gd_server = TRUE;
18087c478bd9Sstevel@tonic-gate 		num_walked_ok = 0;
18097c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_SERVER_LIST_REFRESH) {
18107c478bd9Sstevel@tonic-gate 			logit("getldap_set_refresh_ttl:(5) refresh ttl is %d "
18117c478bd9Sstevel@tonic-gate 			    "seconds\n", *refresh_ttl);
18127c478bd9Sstevel@tonic-gate 		}
18137c478bd9Sstevel@tonic-gate 	} else if (num_prev_good_servers > num_good_servers) {
18147c478bd9Sstevel@tonic-gate 		/*
18157c478bd9Sstevel@tonic-gate 		 * if more down/bad servers found,
18167c478bd9Sstevel@tonic-gate 		 * decrease the refresh TTL by half
18177c478bd9Sstevel@tonic-gate 		 * but no less than the min. refresh TTL
18187c478bd9Sstevel@tonic-gate 		 */
18197c478bd9Sstevel@tonic-gate 		*refresh_ttl = *refresh_ttl / 2;
18207c478bd9Sstevel@tonic-gate 		if (*refresh_ttl < refresh_ttl_min)
18217c478bd9Sstevel@tonic-gate 			*refresh_ttl = refresh_ttl_min;
18227c478bd9Sstevel@tonic-gate 		num_walked_ok = 0;
18237c478bd9Sstevel@tonic-gate 		logit("getldap_set_refresh_ttl:(6) refresh ttl is %d "
18247c478bd9Sstevel@tonic-gate 		    "seconds\n", *refresh_ttl);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_SERVER_LIST_REFRESH) {
18297c478bd9Sstevel@tonic-gate 		logit("getldap_set_refresh_ttl:(7) refresh ttl is %d seconds\n",
18307c478bd9Sstevel@tonic-gate 		    *refresh_ttl);
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&refresh_mutex);
18337c478bd9Sstevel@tonic-gate 	return (0);
18347c478bd9Sstevel@tonic-gate }
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate static int
18377c478bd9Sstevel@tonic-gate getldap_serverInfo_op(info_op_t op, char *input, char **output)
18387c478bd9Sstevel@tonic-gate {
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	static rwlock_t 	info_lock = DEFAULTRWLOCK;
18417c478bd9Sstevel@tonic-gate 	static rwlock_t 	info_lock_old = DEFAULTRWLOCK;
18427c478bd9Sstevel@tonic-gate 	static mutex_t		info_mutex;
18437c478bd9Sstevel@tonic-gate 	static cond_t		info_cond;
18447c478bd9Sstevel@tonic-gate 	static int		creating = FALSE;
18457c478bd9Sstevel@tonic-gate 	static int		refresh_ttl = 0;
18467c478bd9Sstevel@tonic-gate 	static int		sec_to_refresh = 0;
18477c478bd9Sstevel@tonic-gate 	static int		in_no_server_mode = FALSE;
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	static server_info_t 	*serverInfo = NULL;
18507c478bd9Sstevel@tonic-gate 	static server_info_t 	*serverInfo_old = NULL;
18517c478bd9Sstevel@tonic-gate 	server_info_t 		*serverInfo_1;
18527c478bd9Sstevel@tonic-gate 	int 			is_creating;
18537c478bd9Sstevel@tonic-gate 	int 			err, no_server_good = FALSE;
18547c478bd9Sstevel@tonic-gate 	int 			server_removed = FALSE;
18557c478bd9Sstevel@tonic-gate 	static struct timespec	timeout;
18567c478bd9Sstevel@tonic-gate 	struct timespec		new_timeout;
18577c478bd9Sstevel@tonic-gate 	struct timeval		tp;
18587c478bd9Sstevel@tonic-gate 	static time_t		prev_refresh = 0, next_refresh = 0;
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
18617c478bd9Sstevel@tonic-gate 		logit("getldap_serverInfo_op()...\n");
18627c478bd9Sstevel@tonic-gate 	}
18637c478bd9Sstevel@tonic-gate 	switch (op) {
18647c478bd9Sstevel@tonic-gate 	case INFO_OP_CREATE:
18657c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
18667c478bd9Sstevel@tonic-gate 			logit("operation is INFO_OP_CREATE...\n");
18677c478bd9Sstevel@tonic-gate 		}
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 		/*
18707c478bd9Sstevel@tonic-gate 		 * indicate that the server info is being
18717c478bd9Sstevel@tonic-gate 		 * (re)created, so that the refresh thread
18727c478bd9Sstevel@tonic-gate 		 * will not refresh the info list right
18737c478bd9Sstevel@tonic-gate 		 * after the list got (re)created
18747c478bd9Sstevel@tonic-gate 		 */
18757c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&info_mutex);
18767c478bd9Sstevel@tonic-gate 		is_creating = creating;
18777c478bd9Sstevel@tonic-gate 		creating = TRUE;
18787c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&info_mutex);
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 		if (is_creating)
18817c478bd9Sstevel@tonic-gate 			break;
18827c478bd9Sstevel@tonic-gate 		/*
18837c478bd9Sstevel@tonic-gate 		 * create an empty info list
18847c478bd9Sstevel@tonic-gate 		 */
18857c478bd9Sstevel@tonic-gate 		(void) getldap_init_serverInfo(&serverInfo_1);
18867c478bd9Sstevel@tonic-gate 		/*
18877c478bd9Sstevel@tonic-gate 		 * exit if list not created
18887c478bd9Sstevel@tonic-gate 		 */
18897c478bd9Sstevel@tonic-gate 		if (serverInfo_1 == NULL) {
18907c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&info_mutex);
18917c478bd9Sstevel@tonic-gate 			creating = FALSE;
18927c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&info_mutex);
18937c478bd9Sstevel@tonic-gate 			break;
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 		/*
18967c478bd9Sstevel@tonic-gate 		 * make the new server info available:
18977c478bd9Sstevel@tonic-gate 		 * use writer lock here, so that the switch
18987c478bd9Sstevel@tonic-gate 		 * is done after all the reader locks have
18997c478bd9Sstevel@tonic-gate 		 * been released.
19007c478bd9Sstevel@tonic-gate 		 */
19017c478bd9Sstevel@tonic-gate 		(void) rw_wrlock(&info_lock);
19027c478bd9Sstevel@tonic-gate 		serverInfo = serverInfo_1;
19037c478bd9Sstevel@tonic-gate 		/*
19047c478bd9Sstevel@tonic-gate 		 * if this is the first time
19057c478bd9Sstevel@tonic-gate 		 * the server list is being created,
19067c478bd9Sstevel@tonic-gate 		 * (i.e., serverInfo_old is NULL)
19077c478bd9Sstevel@tonic-gate 		 * make the old list same as the new
19087c478bd9Sstevel@tonic-gate 		 * so the GETSERVER code can do its work
19097c478bd9Sstevel@tonic-gate 		 */
19107c478bd9Sstevel@tonic-gate 		if (serverInfo_old == NULL)
19117c478bd9Sstevel@tonic-gate 			serverInfo_old = serverInfo_1;
19127c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&info_lock);
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 		/*
19157c478bd9Sstevel@tonic-gate 		 * fill the new info list
19167c478bd9Sstevel@tonic-gate 		 */
19177c478bd9Sstevel@tonic-gate 		(void) rw_rdlock(&info_lock);
19187c478bd9Sstevel@tonic-gate 		/* reset bind time (tcptimeout) */
19197c478bd9Sstevel@tonic-gate 		(void) getldap_set_serverInfo(serverInfo, 1);
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&info_mutex);
19227c478bd9Sstevel@tonic-gate 		/*
19237c478bd9Sstevel@tonic-gate 		 * set cache manager server list TTL,
19247c478bd9Sstevel@tonic-gate 		 * set refresh_ttl to zero to indicate a fresh one
19257c478bd9Sstevel@tonic-gate 		 */
19267c478bd9Sstevel@tonic-gate 		refresh_ttl = 0;
19277c478bd9Sstevel@tonic-gate 		(void) getldap_set_refresh_ttl(serverInfo,
19287c478bd9Sstevel@tonic-gate 		    &refresh_ttl, &no_server_good);
19297c478bd9Sstevel@tonic-gate 		sec_to_refresh = refresh_ttl;
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 		/* statistics: previous refresh time */
19327c478bd9Sstevel@tonic-gate 		if (gettimeofday(&tp, NULL) == 0)
19337c478bd9Sstevel@tonic-gate 			prev_refresh = tp.tv_sec;
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 		creating = FALSE;
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 		/*
19387c478bd9Sstevel@tonic-gate 		 * if no server found or available,
19397c478bd9Sstevel@tonic-gate 		 * tell the server info refresh thread
19407c478bd9Sstevel@tonic-gate 		 * to start the "no-server" refresh loop
19417c478bd9Sstevel@tonic-gate 		 * otherwise reset the in_no_server_mode flag
19427c478bd9Sstevel@tonic-gate 		 */
19437c478bd9Sstevel@tonic-gate 		if (no_server_good) {
19447c478bd9Sstevel@tonic-gate 			sec_to_refresh = 0;
19457c478bd9Sstevel@tonic-gate 			in_no_server_mode = TRUE;
19467c478bd9Sstevel@tonic-gate 		} else
19477c478bd9Sstevel@tonic-gate 			in_no_server_mode = FALSE;
19487c478bd9Sstevel@tonic-gate 		/*
19497c478bd9Sstevel@tonic-gate 		 * awake the sleeping refresh thread
19507c478bd9Sstevel@tonic-gate 		 */
19517c478bd9Sstevel@tonic-gate 		(void) cond_signal(&info_cond);
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&info_mutex);
19547c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&info_lock);
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 		/*
19577c478bd9Sstevel@tonic-gate 		 * delete the old server info
19587c478bd9Sstevel@tonic-gate 		 */
19597c478bd9Sstevel@tonic-gate 		(void) rw_wrlock(&info_lock_old);
19607c478bd9Sstevel@tonic-gate 		if (serverInfo_old != serverInfo)
19617c478bd9Sstevel@tonic-gate 			(void) getldap_destroy_serverInfo(serverInfo_old);
19627c478bd9Sstevel@tonic-gate 		/*
19637c478bd9Sstevel@tonic-gate 		 * serverInfo_old needs to be the same as
19647c478bd9Sstevel@tonic-gate 		 * serverinfo now.
19657c478bd9Sstevel@tonic-gate 		 * it will be used by GETSERVER processing.
19667c478bd9Sstevel@tonic-gate 		 */
19677c478bd9Sstevel@tonic-gate 		serverInfo_old = serverInfo;
19687c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&info_lock_old);
19697c478bd9Sstevel@tonic-gate 		break;
19707c478bd9Sstevel@tonic-gate 	case INFO_OP_DELETE:
19717c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
19727c478bd9Sstevel@tonic-gate 			logit("operation is INFO_OP_DELETE...\n");
19737c478bd9Sstevel@tonic-gate 		}
19747c478bd9Sstevel@tonic-gate 		/*
19757c478bd9Sstevel@tonic-gate 		 * use writer lock here, so that the delete would
19767c478bd9Sstevel@tonic-gate 		 * not start until all the reader locks have
19777c478bd9Sstevel@tonic-gate 		 * been released.
19787c478bd9Sstevel@tonic-gate 		 */
19797c478bd9Sstevel@tonic-gate 		(void) rw_wrlock(&info_lock);
19807c478bd9Sstevel@tonic-gate 		if (serverInfo)
19817c478bd9Sstevel@tonic-gate 			(void) getldap_destroy_serverInfo(serverInfo);
19827c478bd9Sstevel@tonic-gate 		serverInfo = NULL;
19837c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&info_lock);
19847c478bd9Sstevel@tonic-gate 		break;
19857c478bd9Sstevel@tonic-gate 	case INFO_OP_REFRESH:
19867c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_SERVER_LIST_REFRESH) {
19877c478bd9Sstevel@tonic-gate 			logit("operation is INFO_OP_REFRESH...\n");
19887c478bd9Sstevel@tonic-gate 		}
19897c478bd9Sstevel@tonic-gate 		/*
19907c478bd9Sstevel@tonic-gate 		 * if server info is currently being
19917c478bd9Sstevel@tonic-gate 		 * (re)created, do nothing
19927c478bd9Sstevel@tonic-gate 		 */
19937c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&info_mutex);
19947c478bd9Sstevel@tonic-gate 		is_creating = creating;
19957c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&info_mutex);
19967c478bd9Sstevel@tonic-gate 		if (is_creating)
19977c478bd9Sstevel@tonic-gate 			break;
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 		(void) rw_rdlock(&info_lock);
20007c478bd9Sstevel@tonic-gate 		if (serverInfo) {
20017c478bd9Sstevel@tonic-gate 			/* do not reset bind time (tcptimeout) */
20027c478bd9Sstevel@tonic-gate 			(void) getldap_set_serverInfo(serverInfo, 0);
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&info_mutex);
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 			/* statistics: previous refresh time */
20077c478bd9Sstevel@tonic-gate 			if (gettimeofday(&tp, NULL) == 0)
20087c478bd9Sstevel@tonic-gate 				prev_refresh = tp.tv_sec;
20097c478bd9Sstevel@tonic-gate 			/*
20107c478bd9Sstevel@tonic-gate 			 * set cache manager server list TTL
20117c478bd9Sstevel@tonic-gate 			 */
20127c478bd9Sstevel@tonic-gate 			(void) getldap_set_refresh_ttl(serverInfo,
20137c478bd9Sstevel@tonic-gate 			    &refresh_ttl, &no_server_good);
20147c478bd9Sstevel@tonic-gate 			/*
20157c478bd9Sstevel@tonic-gate 			 * if no good server found,
20167c478bd9Sstevel@tonic-gate 			 * tell the server info refresh thread
20177c478bd9Sstevel@tonic-gate 			 * to start the "no-server" refresh loop
20187c478bd9Sstevel@tonic-gate 			 * otherwise reset the in_no_server_mode flag
20197c478bd9Sstevel@tonic-gate 			 */
20207c478bd9Sstevel@tonic-gate 			if (no_server_good) {
20217c478bd9Sstevel@tonic-gate 				in_no_server_mode = TRUE;
20227c478bd9Sstevel@tonic-gate 				sec_to_refresh = 0;
20237c478bd9Sstevel@tonic-gate 			} else {
20247c478bd9Sstevel@tonic-gate 				in_no_server_mode = FALSE;
20257c478bd9Sstevel@tonic-gate 				sec_to_refresh = refresh_ttl;
20267c478bd9Sstevel@tonic-gate 			}
20277c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >=
20287c478bd9Sstevel@tonic-gate 			    DBG_SERVER_LIST_REFRESH) {
20297c478bd9Sstevel@tonic-gate 				logit("getldap_serverInfo_op("
20307c478bd9Sstevel@tonic-gate 				    "INFO_OP_REFRESH):"
20317c478bd9Sstevel@tonic-gate 				    " seconds refresh: %d second(s)....\n",
20327c478bd9Sstevel@tonic-gate 				    sec_to_refresh);
20337c478bd9Sstevel@tonic-gate 			}
20347c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&info_mutex);
20357c478bd9Sstevel@tonic-gate 		}
20367c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&info_lock);
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 		break;
20397c478bd9Sstevel@tonic-gate 	case INFO_OP_REFRESH_WAIT:
20407c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_SERVER_LIST_REFRESH) {
20417c478bd9Sstevel@tonic-gate 			logit("operation is INFO_OP_REFRESH_WAIT...\n");
20427c478bd9Sstevel@tonic-gate 		}
20437c478bd9Sstevel@tonic-gate 		(void) cond_init(&info_cond, NULL, NULL);
20447c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&info_mutex);
20457c478bd9Sstevel@tonic-gate 		err = 0;
20467c478bd9Sstevel@tonic-gate 		while (err != ETIME) {
20477c478bd9Sstevel@tonic-gate 			int sleeptime;
20487c478bd9Sstevel@tonic-gate 			/*
20497c478bd9Sstevel@tonic-gate 			 * if need to go into the "no-server" refresh
20507c478bd9Sstevel@tonic-gate 			 * loop, set timout value to
20517c478bd9Sstevel@tonic-gate 			 * REFRESH_DELAY_WHEN_NO_SERVER
20527c478bd9Sstevel@tonic-gate 			 */
20537c478bd9Sstevel@tonic-gate 			if (sec_to_refresh == 0) {
20547c478bd9Sstevel@tonic-gate 				sec_to_refresh = refresh_ttl;
20557c478bd9Sstevel@tonic-gate 				timeout.tv_sec = time(NULL) +
20567c478bd9Sstevel@tonic-gate 				    REFRESH_DELAY_WHEN_NO_SERVER;
20577c478bd9Sstevel@tonic-gate 				sleeptime = REFRESH_DELAY_WHEN_NO_SERVER;
20587c478bd9Sstevel@tonic-gate 				if (current_admin.debug_level >=
20597c478bd9Sstevel@tonic-gate 				    DBG_SERVER_LIST_REFRESH) {
20607c478bd9Sstevel@tonic-gate 					logit("getldap_serverInfo_op("
20617c478bd9Sstevel@tonic-gate 					    "INFO_OP_REFRESH_WAIT):"
20627c478bd9Sstevel@tonic-gate 					    " entering no-server "
20637c478bd9Sstevel@tonic-gate 					    "refresh loop...\n");
20647c478bd9Sstevel@tonic-gate 				}
20657c478bd9Sstevel@tonic-gate 			} else {
20667c478bd9Sstevel@tonic-gate 				timeout.tv_sec = time(NULL) + sec_to_refresh;
20677c478bd9Sstevel@tonic-gate 				sleeptime = sec_to_refresh;
20687c478bd9Sstevel@tonic-gate 			}
20697c478bd9Sstevel@tonic-gate 			timeout.tv_nsec = 0;
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 			/* statistics: next refresh time */
20727c478bd9Sstevel@tonic-gate 			next_refresh = timeout.tv_sec;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >=
20757c478bd9Sstevel@tonic-gate 			    DBG_SERVER_LIST_REFRESH) {
20767c478bd9Sstevel@tonic-gate 				logit("getldap_serverInfo_op("
20777c478bd9Sstevel@tonic-gate 				    "INFO_OP_REFRESH_WAIT):"
20787c478bd9Sstevel@tonic-gate 				    " about to sleep for %d second(s)...\n",
20797c478bd9Sstevel@tonic-gate 				    sleeptime);
20807c478bd9Sstevel@tonic-gate 			}
20817c478bd9Sstevel@tonic-gate 			err = cond_timedwait(&info_cond,
20827c478bd9Sstevel@tonic-gate 			    &info_mutex, &timeout);
20837c478bd9Sstevel@tonic-gate 		}
20847c478bd9Sstevel@tonic-gate 		(void) cond_destroy(&info_cond);
20857c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&info_mutex);
20867c478bd9Sstevel@tonic-gate 		break;
20877c478bd9Sstevel@tonic-gate 	case INFO_OP_GETSERVER:
20887c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
20897c478bd9Sstevel@tonic-gate 			logit("operation is INFO_OP_GETSERVER...\n");
20907c478bd9Sstevel@tonic-gate 		}
20917c478bd9Sstevel@tonic-gate 		*output = NULL;
20927c478bd9Sstevel@tonic-gate 		/*
20937c478bd9Sstevel@tonic-gate 		 * GETSERVER processing always use
20947c478bd9Sstevel@tonic-gate 		 * serverInfo_old to retrieve server infomation.
20957c478bd9Sstevel@tonic-gate 		 * serverInfo_old is equal to serverInfo
20967c478bd9Sstevel@tonic-gate 		 * most of the time, except when a new
20977c478bd9Sstevel@tonic-gate 		 * server list is being created.
20987c478bd9Sstevel@tonic-gate 		 * This is why the check for is_creating
20997c478bd9Sstevel@tonic-gate 		 * is needed below.
21007c478bd9Sstevel@tonic-gate 		 */
21017c478bd9Sstevel@tonic-gate 		(void) rw_rdlock(&info_lock_old);
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 		if (serverInfo_old == NULL) {
21047c478bd9Sstevel@tonic-gate 			(void) rw_unlock(&info_lock_old);
21057c478bd9Sstevel@tonic-gate 			break;
21067c478bd9Sstevel@tonic-gate 		} else
21077c478bd9Sstevel@tonic-gate 			(void) getldap_get_serverInfo(serverInfo_old,
21087c478bd9Sstevel@tonic-gate 			    input, output, &server_removed);
21097c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&info_lock_old);
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 		/*
21127c478bd9Sstevel@tonic-gate 		 * if server info is currently being
21137c478bd9Sstevel@tonic-gate 		 * (re)created, do nothing
21147c478bd9Sstevel@tonic-gate 		 */
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&info_mutex);
21177c478bd9Sstevel@tonic-gate 		is_creating = creating;
21187c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&info_mutex);
21197c478bd9Sstevel@tonic-gate 		if (is_creating)
21207c478bd9Sstevel@tonic-gate 			break;
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 		/*
21237c478bd9Sstevel@tonic-gate 		 * set cache manager server list TTL if necessary
21247c478bd9Sstevel@tonic-gate 		 */
21257c478bd9Sstevel@tonic-gate 		if (*output == NULL || server_removed) {
21267c478bd9Sstevel@tonic-gate 			(void) rw_rdlock(&info_lock);
21277c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&info_mutex);
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 			(void) getldap_set_refresh_ttl(serverInfo,
21307c478bd9Sstevel@tonic-gate 			    &refresh_ttl, &no_server_good);
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 			/*
21337c478bd9Sstevel@tonic-gate 			 * if no good server found, need to go into
21347c478bd9Sstevel@tonic-gate 			 * the "no-server" refresh loop
21357c478bd9Sstevel@tonic-gate 			 * to find a server as soon as possible
21367c478bd9Sstevel@tonic-gate 			 * otherwise reset the in_no_server_mode flag
21377c478bd9Sstevel@tonic-gate 			 */
21387c478bd9Sstevel@tonic-gate 			if (no_server_good) {
21397c478bd9Sstevel@tonic-gate 				/*
21407c478bd9Sstevel@tonic-gate 				 * if already in no-server mode,
21417c478bd9Sstevel@tonic-gate 				 * don't brother
21427c478bd9Sstevel@tonic-gate 				 */
21437c478bd9Sstevel@tonic-gate 				if (in_no_server_mode == FALSE) {
21447c478bd9Sstevel@tonic-gate 					sec_to_refresh = 0;
21457c478bd9Sstevel@tonic-gate 					in_no_server_mode = TRUE;
21467c478bd9Sstevel@tonic-gate 					(void) cond_signal(&info_cond);
21477c478bd9Sstevel@tonic-gate 				}
21487c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&info_mutex);
21497c478bd9Sstevel@tonic-gate 				(void) rw_unlock(&info_lock);
21507c478bd9Sstevel@tonic-gate 				break;
21517c478bd9Sstevel@tonic-gate 			} else {
21527c478bd9Sstevel@tonic-gate 				in_no_server_mode = FALSE;
21537c478bd9Sstevel@tonic-gate 				sec_to_refresh = refresh_ttl;
21547c478bd9Sstevel@tonic-gate 			}
21557c478bd9Sstevel@tonic-gate 			/*
21567c478bd9Sstevel@tonic-gate 			 * if the refresh thread will be timed out
21577c478bd9Sstevel@tonic-gate 			 * longer than refresh_ttl seconds,
21587c478bd9Sstevel@tonic-gate 			 * wake it up to make it wait on the new
21597c478bd9Sstevel@tonic-gate 			 * time out value
21607c478bd9Sstevel@tonic-gate 			 */
21617c478bd9Sstevel@tonic-gate 			new_timeout.tv_sec = time(NULL) + refresh_ttl;
21627c478bd9Sstevel@tonic-gate 			if (new_timeout.tv_sec < timeout.tv_sec)
21637c478bd9Sstevel@tonic-gate 				(void) cond_signal(&info_cond);
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&info_mutex);
21667c478bd9Sstevel@tonic-gate 			(void) rw_unlock(&info_lock);
21677c478bd9Sstevel@tonic-gate 		}
21687c478bd9Sstevel@tonic-gate 		break;
21697c478bd9Sstevel@tonic-gate 	case INFO_OP_GETSTAT:
21707c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
21717c478bd9Sstevel@tonic-gate 			logit("operation is INFO_OP_GETSTAT...\n");
21727c478bd9Sstevel@tonic-gate 		}
21737c478bd9Sstevel@tonic-gate 		*output = NULL;
21747c478bd9Sstevel@tonic-gate 		(void) rw_rdlock(&info_lock);
21757c478bd9Sstevel@tonic-gate 		if (serverInfo) {
21767c478bd9Sstevel@tonic-gate 			(void) getldap_get_server_stat(serverInfo,
21777c478bd9Sstevel@tonic-gate 			    output, &prev_refresh, &next_refresh);
21787c478bd9Sstevel@tonic-gate 		}
21797c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&info_lock);
21807c478bd9Sstevel@tonic-gate 		break;
21817c478bd9Sstevel@tonic-gate 	default:
21827c478bd9Sstevel@tonic-gate 		logit("getldap_serverInfo_op(): "
21837c478bd9Sstevel@tonic-gate 		    "invalid operation code (%d).\n", op);
21847c478bd9Sstevel@tonic-gate 		return (-1);
21857c478bd9Sstevel@tonic-gate 		break;
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
21887c478bd9Sstevel@tonic-gate }
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate void
21917c478bd9Sstevel@tonic-gate getldap_serverInfo_refresh()
21927c478bd9Sstevel@tonic-gate {
21937c478bd9Sstevel@tonic-gate 	int always = 1;
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
21967c478bd9Sstevel@tonic-gate 		logit("getldap_serverInfo_refresh()...\n");
21977c478bd9Sstevel@tonic-gate 	}
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	/* create the server info list */
22007c478bd9Sstevel@tonic-gate 	(void) getldap_serverInfo_op(INFO_OP_CREATE, NULL, NULL);
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	while (always) {
22037c478bd9Sstevel@tonic-gate 		/*
22047c478bd9Sstevel@tonic-gate 		 * the operation INFO_OP_REFRESH_WAIT
22057c478bd9Sstevel@tonic-gate 		 * causes this thread to wait until
22067c478bd9Sstevel@tonic-gate 		 * it is time to do refresh,
22077c478bd9Sstevel@tonic-gate 		 * see getldap_serverInfo_op() for details
22087c478bd9Sstevel@tonic-gate 		 */
22097c478bd9Sstevel@tonic-gate 		(void) getldap_serverInfo_op(INFO_OP_REFRESH_WAIT, NULL, NULL);
22107c478bd9Sstevel@tonic-gate 		(void) getldap_serverInfo_op(INFO_OP_REFRESH, NULL, NULL);
22117c478bd9Sstevel@tonic-gate 	}
22127c478bd9Sstevel@tonic-gate }
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate void
22157c478bd9Sstevel@tonic-gate getldap_getserver(ldap_return_t *out, ldap_call_t *in)
22167c478bd9Sstevel@tonic-gate {
22177c478bd9Sstevel@tonic-gate 	char 		*outstr = NULL;
22187c478bd9Sstevel@tonic-gate 	char 		req[] = "0";
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
22217c478bd9Sstevel@tonic-gate 		logit("getldap_getserver()...\n");
22227c478bd9Sstevel@tonic-gate 	}
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	/* assume no server found */
22257c478bd9Sstevel@tonic-gate 	out->ldap_errno = -1;
22267c478bd9Sstevel@tonic-gate 	out->ldap_return_code = NOTFOUND;
22277c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (*out);
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	/* make sure the request is valid */
22307c478bd9Sstevel@tonic-gate 	req[0] = (in->ldap_u.servername)[0];
22317c478bd9Sstevel@tonic-gate 	if ((req[0] != '\0') &&
22327c478bd9Sstevel@tonic-gate 	    (strcmp(req, NS_CACHE_NEW) != 0) &&
22337c478bd9Sstevel@tonic-gate 	    (strcmp(req, NS_CACHE_NORESP)  != 0) &&
22347c478bd9Sstevel@tonic-gate 	    (strcmp(req, NS_CACHE_NEXT)    != 0) &&
22357c478bd9Sstevel@tonic-gate 	    (strcmp(req, NS_CACHE_WRITE)   != 0)) {
22367c478bd9Sstevel@tonic-gate 		return;
22377c478bd9Sstevel@tonic-gate 	}
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	(void) getldap_serverInfo_op(INFO_OP_GETSERVER,
22407c478bd9Sstevel@tonic-gate 	    in->ldap_u.domainname, &outstr);
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	if (outstr == NULL)
22437c478bd9Sstevel@tonic-gate 		return;
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (ldap_return_t);
22467c478bd9Sstevel@tonic-gate 	(void) strncpy(out->ldap_u.config, outstr, strlen(outstr)+1);
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_PROFILE_REFRESH) {
22497c478bd9Sstevel@tonic-gate 		/* Log server IP */
22507c478bd9Sstevel@tonic-gate 		char *ptr;
22517c478bd9Sstevel@tonic-gate 		ptr = strstr(outstr, DOORLINESEP);
22527c478bd9Sstevel@tonic-gate 		if (ptr) {
22537c478bd9Sstevel@tonic-gate 			*ptr = '\0';
22547c478bd9Sstevel@tonic-gate 			logit("getldap_getserver: got server %s\n", outstr);
22557c478bd9Sstevel@tonic-gate 		} else
22567c478bd9Sstevel@tonic-gate 			logit("getldap_getserver: Missing %s."
22577c478bd9Sstevel@tonic-gate 			    " Internal error\n", DOORLINESEP);
22587c478bd9Sstevel@tonic-gate 	}
22597c478bd9Sstevel@tonic-gate 	free(outstr);
22607c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
22617c478bd9Sstevel@tonic-gate 	out->ldap_errno = 0;
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate }
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate void
22667c478bd9Sstevel@tonic-gate getldap_get_cacheData(ldap_return_t *out, ldap_call_t *in)
22677c478bd9Sstevel@tonic-gate {
22687c478bd9Sstevel@tonic-gate 	char	*outstr = NULL, *instr = NULL;
22697c478bd9Sstevel@tonic-gate 	int	datatype = CACHE_MAP_UNKNOWN;
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
22727c478bd9Sstevel@tonic-gate 		logit("getldap_get_cacheData()...\n");
22737c478bd9Sstevel@tonic-gate 	}
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	/* assume no cache data found */
22767c478bd9Sstevel@tonic-gate 	out->ldap_errno = -1;
22777c478bd9Sstevel@tonic-gate 	out->ldap_return_code = NOTFOUND;
22787c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (*out);
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	/* make sure the request is valid */
22817c478bd9Sstevel@tonic-gate 	if (strncmp(in->ldap_u.servername,
22827c478bd9Sstevel@tonic-gate 	    NS_CACHE_DN2DOMAIN, strlen(NS_CACHE_DN2DOMAIN)) == 0)
22837c478bd9Sstevel@tonic-gate 		datatype = CACHE_MAP_DN2DOMAIN;
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	if (datatype == CACHE_MAP_UNKNOWN)
22867c478bd9Sstevel@tonic-gate 		return;
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate 	instr = strstr(in->ldap_u.servername, DOORLINESEP);
22897c478bd9Sstevel@tonic-gate 	if (instr == NULL)
22907c478bd9Sstevel@tonic-gate 		return;
22917c478bd9Sstevel@tonic-gate 	instr += strlen(DOORLINESEP);
22927c478bd9Sstevel@tonic-gate 	if (*instr == '\0')
22937c478bd9Sstevel@tonic-gate 		return;
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 	(void) getldap_cache_op(CACHE_OP_FIND, datatype,
22967c478bd9Sstevel@tonic-gate 	    instr, &outstr);
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	if (outstr == NULL)
22997c478bd9Sstevel@tonic-gate 		return;
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (ldap_return_t);
23027c478bd9Sstevel@tonic-gate 	(void) strncpy(out->ldap_u.config, outstr, strlen(outstr)+1);
23037c478bd9Sstevel@tonic-gate 	free(outstr);
23047c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
23057c478bd9Sstevel@tonic-gate 	out->ldap_errno = 0;
23067c478bd9Sstevel@tonic-gate }
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate void
23097c478bd9Sstevel@tonic-gate getldap_set_cacheData(ldap_return_t *out, ldap_call_t *in)
23107c478bd9Sstevel@tonic-gate {
23117c478bd9Sstevel@tonic-gate 	char	*instr1 = NULL;
23127c478bd9Sstevel@tonic-gate 	char	*instr2 = NULL;
23137c478bd9Sstevel@tonic-gate 	int	datatype = CACHE_MAP_UNKNOWN;
23147c478bd9Sstevel@tonic-gate 	int	rc = 0;
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
23177c478bd9Sstevel@tonic-gate 		logit("getldap_set_cacheData()...\n");
23187c478bd9Sstevel@tonic-gate 	}
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 	/* assume error */
23217c478bd9Sstevel@tonic-gate 	out->ldap_errno = -1;
23227c478bd9Sstevel@tonic-gate 	out->ldap_return_code = NOTFOUND;
23237c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (*out);
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	/* make sure the request is valid */
23267c478bd9Sstevel@tonic-gate 	if (strncmp(in->ldap_u.servername,
23277c478bd9Sstevel@tonic-gate 	    NS_CACHE_DN2DOMAIN, strlen(NS_CACHE_DN2DOMAIN)) == 0)
23287c478bd9Sstevel@tonic-gate 		datatype = CACHE_MAP_DN2DOMAIN;
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	if (datatype == CACHE_MAP_UNKNOWN)
23317c478bd9Sstevel@tonic-gate 		return;
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	instr1 = strstr(in->ldap_u.servername, DOORLINESEP);
23347c478bd9Sstevel@tonic-gate 	if (instr1 == NULL)
23357c478bd9Sstevel@tonic-gate 		return;
23367c478bd9Sstevel@tonic-gate 	*instr1 = '\0';
23377c478bd9Sstevel@tonic-gate 	instr1 += strlen(DOORLINESEP);
23387c478bd9Sstevel@tonic-gate 	if (*instr1 == '\0')
23397c478bd9Sstevel@tonic-gate 		return;
23407c478bd9Sstevel@tonic-gate 	instr2 = strstr(instr1, DOORLINESEP);
23417c478bd9Sstevel@tonic-gate 	if (instr2 == NULL)
23427c478bd9Sstevel@tonic-gate 		return;
23437c478bd9Sstevel@tonic-gate 	*instr2 = '\0';
23447c478bd9Sstevel@tonic-gate 	instr2 += strlen(DOORLINESEP);
23457c478bd9Sstevel@tonic-gate 	if (*instr2 == '\0')
23467c478bd9Sstevel@tonic-gate 		return;
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 	rc = getldap_cache_op(CACHE_OP_ADD, datatype,
23497c478bd9Sstevel@tonic-gate 	    instr1, &instr2);
23507c478bd9Sstevel@tonic-gate 	if (rc != NS_LDAP_SUCCESS)
23517c478bd9Sstevel@tonic-gate 		return;
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (ldap_return_t);
23547c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
23557c478bd9Sstevel@tonic-gate 	out->ldap_errno = 0;
23567c478bd9Sstevel@tonic-gate }
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate void
23597c478bd9Sstevel@tonic-gate getldap_get_cacheStat(ldap_return_t *out)
23607c478bd9Sstevel@tonic-gate {
23617c478bd9Sstevel@tonic-gate 	char	*foutstr = NULL;
23627c478bd9Sstevel@tonic-gate 	char	*soutstr = NULL;
23637c478bd9Sstevel@tonic-gate 	char	*coutstr = NULL;
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
23667c478bd9Sstevel@tonic-gate 		logit("getldap_get_cacheStat()...\n");
23677c478bd9Sstevel@tonic-gate 	}
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 	/* setup for error return */
23707c478bd9Sstevel@tonic-gate 	out->ldap_errno = -1;
23717c478bd9Sstevel@tonic-gate 	out->ldap_return_code = NOTFOUND;
23727c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (*out);
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 	/* get refersh statisitcs */
23757c478bd9Sstevel@tonic-gate 	(void) getldap_get_refresh_stat(&foutstr);
23767c478bd9Sstevel@tonic-gate 	if (foutstr == NULL)
23777c478bd9Sstevel@tonic-gate 		return;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	/* get server statisitcs */
23807c478bd9Sstevel@tonic-gate 	(void) getldap_serverInfo_op(INFO_OP_GETSTAT, NULL, &soutstr);
23817c478bd9Sstevel@tonic-gate 	if (soutstr == NULL) {
23827c478bd9Sstevel@tonic-gate 		free(foutstr);
23837c478bd9Sstevel@tonic-gate 		return;
23847c478bd9Sstevel@tonic-gate 	}
23857c478bd9Sstevel@tonic-gate 	/* get cache data statisitcs */
23867c478bd9Sstevel@tonic-gate 	(void) getldap_cache_op(CACHE_OP_GETSTAT, NULL, NULL, &coutstr);
23877c478bd9Sstevel@tonic-gate 	if (coutstr == NULL) {
23887c478bd9Sstevel@tonic-gate 		free(foutstr);
23897c478bd9Sstevel@tonic-gate 		free(soutstr);
23907c478bd9Sstevel@tonic-gate 		return;
23917c478bd9Sstevel@tonic-gate 	}
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (ldap_return_t);
23947c478bd9Sstevel@tonic-gate 	(void) strncpy(out->ldap_u.config, foutstr, strlen(foutstr) + 1);
23957c478bd9Sstevel@tonic-gate 	(void) strncat(out->ldap_u.config, soutstr, strlen(soutstr) + 1);
23967c478bd9Sstevel@tonic-gate 	(void) strncat(out->ldap_u.config, coutstr, strlen(coutstr) + 1);
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	free(foutstr);
23997c478bd9Sstevel@tonic-gate 	free(soutstr);
24007c478bd9Sstevel@tonic-gate 	free(coutstr);
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
24037c478bd9Sstevel@tonic-gate 	out->ldap_errno = 0;
24047c478bd9Sstevel@tonic-gate }
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate static int
24077c478bd9Sstevel@tonic-gate checkupdate(int sighup)
24087c478bd9Sstevel@tonic-gate {
24097c478bd9Sstevel@tonic-gate 	int	value;
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&ldap_lock);
24127c478bd9Sstevel@tonic-gate 	value = sighup;
24137c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&ldap_lock);
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	return (value == TRUE);
24167c478bd9Sstevel@tonic-gate }
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate static int
24207c478bd9Sstevel@tonic-gate update_from_profile()
24217c478bd9Sstevel@tonic-gate {
24227c478bd9Sstevel@tonic-gate 	ns_ldap_result_t *result = NULL;
24237c478bd9Sstevel@tonic-gate 	char		searchfilter[BUFSIZ];
24247c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	*error;
24257c478bd9Sstevel@tonic-gate 	int		rc;
24267c478bd9Sstevel@tonic-gate 	void		**paramVal = NULL;
24277c478bd9Sstevel@tonic-gate 	ns_config_t	*ptr = NULL;
24287c478bd9Sstevel@tonic-gate 	char		*profile = NULL;
24297c478bd9Sstevel@tonic-gate 	char		errstr[MAXERROR];
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
24327c478bd9Sstevel@tonic-gate 		logit("update_from_profile....\n");
24337c478bd9Sstevel@tonic-gate 	}
24347c478bd9Sstevel@tonic-gate 	do {
24357c478bd9Sstevel@tonic-gate 		(void) rw_wrlock(&ldap_lock);
24367c478bd9Sstevel@tonic-gate 		sighup_update = FALSE;
24377c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&ldap_lock);
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 		if ((rc = __ns_ldap_getParam(NS_LDAP_PROFILE_P,
24407c478bd9Sstevel@tonic-gate 		    &paramVal, &error)) != NS_LDAP_SUCCESS) {
24417c478bd9Sstevel@tonic-gate 			if (error != NULL && error->message != NULL)
24427c478bd9Sstevel@tonic-gate 				logit("Error: Unable to  profile name: %s\n",
24437c478bd9Sstevel@tonic-gate 				    error->message);
24447c478bd9Sstevel@tonic-gate 			else {
24457c478bd9Sstevel@tonic-gate 				char *tmp;
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 				__ns_ldap_err2str(rc, &tmp);
24487c478bd9Sstevel@tonic-gate 				logit("Error: Unable to  profile name: %s\n",
24497c478bd9Sstevel@tonic-gate 				    tmp);
24507c478bd9Sstevel@tonic-gate 			}
24517c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeParam(&paramVal);
24527c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&error);
24537c478bd9Sstevel@tonic-gate 			return (-1);
24547c478bd9Sstevel@tonic-gate 		}
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 		if (paramVal && *paramVal)
24577c478bd9Sstevel@tonic-gate 			profile = strdup((char *)*paramVal);
24587c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeParam(&paramVal);
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 		if (profile == NULL) {
24617c478bd9Sstevel@tonic-gate 			return (-1);
24627c478bd9Sstevel@tonic-gate 		}
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 		(void) snprintf(searchfilter, BUFSIZ, _PROFILE_FILTER,
24657c478bd9Sstevel@tonic-gate 		    _PROFILE1_OBJECTCLASS, _PROFILE2_OBJECTCLASS, profile);
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 		if ((rc = __ns_ldap_list(_PROFILE_CONTAINER,
24687c478bd9Sstevel@tonic-gate 		    (const char *)searchfilter, NULL,
24697c478bd9Sstevel@tonic-gate 		    NULL, NULL, 0,
24707c478bd9Sstevel@tonic-gate 		    &result, &error, NULL, NULL)) != NS_LDAP_SUCCESS) {
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 			/*
24737c478bd9Sstevel@tonic-gate 			 * Is profile name the DEFAULTCONFIGNAME?
24747c478bd9Sstevel@tonic-gate 			 * syslog Warning, otherwise syslog error.
24757c478bd9Sstevel@tonic-gate 			 */
24767c478bd9Sstevel@tonic-gate 			if (strcmp(profile, DEFAULTCONFIGNAME) == 0) {
24777c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING,
24787c478bd9Sstevel@tonic-gate 				    "Ignoring attempt to refresh nonexistent "
24797c478bd9Sstevel@tonic-gate 				    "default profile: %s.\n",
24807c478bd9Sstevel@tonic-gate 				    profile);
24817c478bd9Sstevel@tonic-gate 				logit("Ignoring attempt to refresh nonexistent "
24827c478bd9Sstevel@tonic-gate 				    "default profile: %s.\n",
24837c478bd9Sstevel@tonic-gate 				    profile);
24847c478bd9Sstevel@tonic-gate 			} else if ((error != NULL) &&
24857c478bd9Sstevel@tonic-gate 			    (error->message != NULL)) {
24867c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
24877c478bd9Sstevel@tonic-gate 				    "Error: Unable to refresh profile:%s:"
24887c478bd9Sstevel@tonic-gate 				    " %s\n", profile, error->message);
24897c478bd9Sstevel@tonic-gate 				logit("Error: Unable to refresh profile:"
24907c478bd9Sstevel@tonic-gate 				    "%s:%s\n", profile, error->message);
24917c478bd9Sstevel@tonic-gate 			} else {
24927c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Error: Unable to refresh "
24937c478bd9Sstevel@tonic-gate 				    "from profile:%s. (error=%d)\n",
24947c478bd9Sstevel@tonic-gate 				    profile, rc);
24957c478bd9Sstevel@tonic-gate 				logit("Error: Unable to refresh from profile "
24967c478bd9Sstevel@tonic-gate 				    "%s (error=%d)\n", profile, rc);
24977c478bd9Sstevel@tonic-gate 			}
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&error);
25007c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeResult(&result);
25017c478bd9Sstevel@tonic-gate 			free(profile);
25027c478bd9Sstevel@tonic-gate 			return (-1);
25037c478bd9Sstevel@tonic-gate 		}
25047c478bd9Sstevel@tonic-gate 		free(profile);
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	} while (checkupdate(sighup_update) == TRUE);
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&ldap_lock);
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 	ptr = __ns_ldap_make_config(result);
25127c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&result);
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
25157c478bd9Sstevel@tonic-gate 		logit("Error: __ns_ldap_make_config failed.\n");
25167c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&ldap_lock);
25177c478bd9Sstevel@tonic-gate 		return (-1);
25187c478bd9Sstevel@tonic-gate 	}
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	/*
25217c478bd9Sstevel@tonic-gate 	 * cross check the config parameters
25227c478bd9Sstevel@tonic-gate 	 */
25237c478bd9Sstevel@tonic-gate 	if (__s_api_crosscheck(ptr, errstr, B_TRUE) == NS_SUCCESS) {
25247c478bd9Sstevel@tonic-gate 		/*
25257c478bd9Sstevel@tonic-gate 		 * reset the local profile TTL
25267c478bd9Sstevel@tonic-gate 		 */
25277c478bd9Sstevel@tonic-gate 		if (ptr->paramList[NS_LDAP_CACHETTL_P].ns_pc)
25287c478bd9Sstevel@tonic-gate 			current_admin.ldap_stat.ldap_ttl =
25297c478bd9Sstevel@tonic-gate 			    atol(ptr->paramList[NS_LDAP_CACHETTL_P].ns_pc);
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_PROFILE_REFRESH) {
25327c478bd9Sstevel@tonic-gate 			logit("update_from_profile: reset profile TTL to %d"
25337c478bd9Sstevel@tonic-gate 			    "  seconds\n",
25347c478bd9Sstevel@tonic-gate 			    current_admin.ldap_stat.ldap_ttl);
2535cb5caa98Sdjl 			logit("update_from_profile: expire time %ld "
25367c478bd9Sstevel@tonic-gate 			    "seconds\n",
2537cb5caa98Sdjl 			    ptr->paramList[NS_LDAP_EXP_P].ns_tm);
25387c478bd9Sstevel@tonic-gate 		}
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate 		/* set ptr as current_config */
25417c478bd9Sstevel@tonic-gate 		__s_api_init_config(ptr);
25427c478bd9Sstevel@tonic-gate 		rc = 0;
25437c478bd9Sstevel@tonic-gate 	} else {
25447c478bd9Sstevel@tonic-gate 		__s_api_destroy_config(ptr);
25457c478bd9Sstevel@tonic-gate 		logit("Error: downloaded profile failed to pass "
25467c478bd9Sstevel@tonic-gate 		    "crosscheck (%s).\n", errstr);
25477c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "ldap_cachemgr: %s", errstr);
25487c478bd9Sstevel@tonic-gate 		rc = -1;
25497c478bd9Sstevel@tonic-gate 	}
25507c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&ldap_lock);
2551cb5caa98Sdjl 
25527c478bd9Sstevel@tonic-gate 	return (rc);
25537c478bd9Sstevel@tonic-gate }
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate int
25567c478bd9Sstevel@tonic-gate getldap_init()
25577c478bd9Sstevel@tonic-gate {
25587c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	*error;
25597c478bd9Sstevel@tonic-gate 	struct timeval	tp;
25607c478bd9Sstevel@tonic-gate 
25617c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
25627c478bd9Sstevel@tonic-gate 		logit("getldap_init()...\n");
25637c478bd9Sstevel@tonic-gate 	}
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_setServer(TRUE);
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&ldap_lock);
25687c478bd9Sstevel@tonic-gate 	if ((error = __ns_ldap_LoadConfiguration()) != NULL) {
25697c478bd9Sstevel@tonic-gate 		logit("Error: Unable to read '%s': %s\n",
25707c478bd9Sstevel@tonic-gate 		    NSCONFIGFILE, error->message);
25717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
25727c478bd9Sstevel@tonic-gate 		    gettext("\nError: Unable to read '%s': %s\n"),
25737c478bd9Sstevel@tonic-gate 		    NSCONFIGFILE, error->message);
25747c478bd9Sstevel@tonic-gate 		__ns_ldap_freeError(&error);
25757c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&ldap_lock);
25767c478bd9Sstevel@tonic-gate 		return (-1);
25777c478bd9Sstevel@tonic-gate 	}
25787c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&ldap_lock);
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	if (gettimeofday(&tp, NULL) == 0) {
25817c478bd9Sstevel@tonic-gate 		/* statistics: previous refresh time */
25827c478bd9Sstevel@tonic-gate 		prev_refresh_time = tp.tv_sec;
25837c478bd9Sstevel@tonic-gate 	}
25847c478bd9Sstevel@tonic-gate 
25857c478bd9Sstevel@tonic-gate 	/* initialize the data cache */
25867c478bd9Sstevel@tonic-gate 	(void) getldap_cache_op(CACHE_OP_CREATE,
25877c478bd9Sstevel@tonic-gate 	    0, NULL, NULL);
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 	return (0);
25907c478bd9Sstevel@tonic-gate }
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate static void
25937c478bd9Sstevel@tonic-gate perform_update(void)
25947c478bd9Sstevel@tonic-gate {
25955f6e3a12Sjs198686 	ns_ldap_error_t	*error = NULL;
25967c478bd9Sstevel@tonic-gate 	struct timeval	tp;
25977c478bd9Sstevel@tonic-gate 	char		buf[20];
2598cb5caa98Sdjl 	int		rc, rc1;
25995f6e3a12Sjs198686 	void		**paramVal = NULL;
2600cb5caa98Sdjl 	ns_ldap_self_gssapi_config_t	config;
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
26037c478bd9Sstevel@tonic-gate 		logit("perform_update()...\n");
26047c478bd9Sstevel@tonic-gate 	}
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_setServer(TRUE);
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate 	if (gettimeofday(&tp, NULL) != 0)
26097c478bd9Sstevel@tonic-gate 		return;
26107c478bd9Sstevel@tonic-gate 
26115f6e3a12Sjs198686 	rc = __ns_ldap_getParam(NS_LDAP_CACHETTL_P, &paramVal, &error);
26125f6e3a12Sjs198686 
26135f6e3a12Sjs198686 	if (rc == NS_LDAP_SUCCESS && paramVal != NULL) {
26145f6e3a12Sjs198686 		current_admin.ldap_stat.ldap_ttl = atol((char *)*paramVal);
26155f6e3a12Sjs198686 	}
26165f6e3a12Sjs198686 
26175f6e3a12Sjs198686 	if (error != NULL)
26185f6e3a12Sjs198686 		(void) __ns_ldap_freeError(&error);
26195f6e3a12Sjs198686 
26205f6e3a12Sjs198686 	if (paramVal != NULL)
26215f6e3a12Sjs198686 		(void) __ns_ldap_freeParam(&paramVal);
26225f6e3a12Sjs198686 
26235f6e3a12Sjs198686 	if (current_admin.debug_level >= DBG_PROFILE_REFRESH) {
26245f6e3a12Sjs198686 		logit("perform_update: current profile TTL is %d seconds\n",
26255f6e3a12Sjs198686 		    current_admin.ldap_stat.ldap_ttl);
26265f6e3a12Sjs198686 	}
26275f6e3a12Sjs198686 
26285f6e3a12Sjs198686 	if (current_admin.ldap_stat.ldap_ttl > 0) {
26297c478bd9Sstevel@tonic-gate 		/*
26307c478bd9Sstevel@tonic-gate 		 * set the profile TTL parameter, just
26317c478bd9Sstevel@tonic-gate 		 * in case that the downloading of
26327c478bd9Sstevel@tonic-gate 		 * the profile from server would fail
26337c478bd9Sstevel@tonic-gate 		 */
26345f6e3a12Sjs198686 
26357c478bd9Sstevel@tonic-gate 		/*
26367c478bd9Sstevel@tonic-gate 		 * NS_LDAP_EXP_P is a no op for __ns_ldap_setParam
26377c478bd9Sstevel@tonic-gate 		 * It depends on NS_LDAP_CACHETTL_P to set it's value
26387c478bd9Sstevel@tonic-gate 		 * Set NS_LDAP_CACHETTL_P here so NS_LDAP_EXP_P value
26397c478bd9Sstevel@tonic-gate 		 * can be set.
26407c478bd9Sstevel@tonic-gate 		 * NS_LDAP_CACHETTL_P value can be reset after the profile is
26417c478bd9Sstevel@tonic-gate 		 * downloaded from the server, so is NS_LDAP_EXP_P.
26427c478bd9Sstevel@tonic-gate 		 */
26437c478bd9Sstevel@tonic-gate 		buf[19] = '\0'; /* null terminated the buffer */
26447c478bd9Sstevel@tonic-gate 		if (__ns_ldap_setParam(NS_LDAP_CACHETTL_P,
26455f6e3a12Sjs198686 		    lltostr((long long)current_admin.ldap_stat.ldap_ttl,
26465f6e3a12Sjs198686 		    &buf[19]),
26477c478bd9Sstevel@tonic-gate 		    &error) != NS_LDAP_SUCCESS) {
26487c478bd9Sstevel@tonic-gate 			logit("Error: __ns_ldap_setParam failed, status: %d "
26497c478bd9Sstevel@tonic-gate 			    "message: %s\n", error->status, error->message);
2650cb5caa98Sdjl 			(void)  __ns_ldap_freeError(&error);
26517c478bd9Sstevel@tonic-gate 			return;
26527c478bd9Sstevel@tonic-gate 		}
26537c478bd9Sstevel@tonic-gate 
26547c478bd9Sstevel@tonic-gate 		(void) rw_wrlock(&ldap_lock);
26557c478bd9Sstevel@tonic-gate 		sighup_update = FALSE;
26567c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&ldap_lock);
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 		do {
26597c478bd9Sstevel@tonic-gate 			rc = update_from_profile();
26607c478bd9Sstevel@tonic-gate 			if (rc != 0) {
26617c478bd9Sstevel@tonic-gate 				logit("Error: Unable to update from profile\n");
26627c478bd9Sstevel@tonic-gate 			}
26637c478bd9Sstevel@tonic-gate 		} while (checkupdate(sighup_update) == TRUE);
26645f6e3a12Sjs198686 	} else {
26655f6e3a12Sjs198686 		rc = 0;
26665f6e3a12Sjs198686 	}
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	/*
26697c478bd9Sstevel@tonic-gate 	 * recreate the server info list
26707c478bd9Sstevel@tonic-gate 	 */
26717c478bd9Sstevel@tonic-gate 	if (rc == 0) {
26727c478bd9Sstevel@tonic-gate 		(void) getldap_serverInfo_op(INFO_OP_CREATE, NULL, NULL);
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 		/* flush the data cache */
26757c478bd9Sstevel@tonic-gate 		(void) getldap_cache_op(CACHE_OP_DELETE,
26767c478bd9Sstevel@tonic-gate 		    0, NULL, NULL);
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 		/* statistics: previous refresh time */
26797c478bd9Sstevel@tonic-gate 		prev_refresh_time = tp.tv_sec;
26807c478bd9Sstevel@tonic-gate 	}
2681cb5caa98Sdjl 	rc1 = __ns_ldap_self_gssapi_config(&config);
2682cb5caa98Sdjl 	if (rc1 == NS_LDAP_SUCCESS) {
2683cb5caa98Sdjl 		if (config != NS_LDAP_SELF_GSSAPI_CONFIG_NONE) {
2684cb5caa98Sdjl 			rc1 = __ns_ldap_check_all_preq(0, 0, 0, config, &error);
2685cb5caa98Sdjl 			(void)  __ns_ldap_freeError(&error);
2686cb5caa98Sdjl 			if (rc1 != NS_LDAP_SUCCESS) {
2687cb5caa98Sdjl 				logit("Error: Check on self credential "
2688cb5caa98Sdjl 				    "prerquesites failed: %d\n",
2689cb5caa98Sdjl 				    rc1);
2690cb5caa98Sdjl 				exit(rc1);
2691cb5caa98Sdjl 			}
2692cb5caa98Sdjl 		}
2693cb5caa98Sdjl 	} else {
2694cb5caa98Sdjl 		logit("Error: Failed to get self credential configuration %d\n",
2695cb5caa98Sdjl 		    rc1);
2696cb5caa98Sdjl 			exit(rc1);
2697cb5caa98Sdjl 	}
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate 	(void) rw_rdlock(&ldap_lock);
27007c478bd9Sstevel@tonic-gate 	if ((error = __ns_ldap_DumpConfiguration(NSCONFIGREFRESH)) != NULL) {
27017c478bd9Sstevel@tonic-gate 		logit("Error: __ns_ldap_DumpConfiguration(\"%s\") failed, "
27027c478bd9Sstevel@tonic-gate 		    "status: %d "
27037c478bd9Sstevel@tonic-gate 		    "message: %s\n", NSCONFIGREFRESH,
27047c478bd9Sstevel@tonic-gate 		    error->status, error->message);
27057c478bd9Sstevel@tonic-gate 		__ns_ldap_freeError(&error);
27067c478bd9Sstevel@tonic-gate 	}
27077c478bd9Sstevel@tonic-gate 	if ((error = __ns_ldap_DumpConfiguration(NSCREDREFRESH)) != NULL) {
27087c478bd9Sstevel@tonic-gate 		logit("Error: __ns_ldap_DumpConfiguration(\"%s\") failed, "
27097c478bd9Sstevel@tonic-gate 		    "status: %d "
27107c478bd9Sstevel@tonic-gate 		    "message: %s\n", NSCREDREFRESH,
27117c478bd9Sstevel@tonic-gate 		    error->status, error->message);
27127c478bd9Sstevel@tonic-gate 		__ns_ldap_freeError(&error);
27137c478bd9Sstevel@tonic-gate 	}
27147c478bd9Sstevel@tonic-gate 	if (rename(NSCONFIGREFRESH, NSCONFIGFILE) != 0)
27157c478bd9Sstevel@tonic-gate 		logit("Error: unlink failed - errno: %d\n", errno);
27167c478bd9Sstevel@tonic-gate 	if (rename(NSCREDREFRESH, NSCREDFILE) != 0)
27177c478bd9Sstevel@tonic-gate 		logit("Error: unlink failed - errno: %d\n", errno);
2718cb5caa98Sdjl 
27197c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&ldap_lock);
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate }
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate void
27247c478bd9Sstevel@tonic-gate getldap_refresh()
27257c478bd9Sstevel@tonic-gate {
27267c478bd9Sstevel@tonic-gate 	struct timespec	timeout;
27277c478bd9Sstevel@tonic-gate 	int		sleeptime;
27287c478bd9Sstevel@tonic-gate 	struct timeval	tp;
27297c478bd9Sstevel@tonic-gate 	long		expire = 0;
27307c478bd9Sstevel@tonic-gate 	void		**paramVal = NULL;
27317c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	*errorp;
27327c478bd9Sstevel@tonic-gate 	int		always = 1, err;
27337c478bd9Sstevel@tonic-gate 	int		first_time = 1;
27347c478bd9Sstevel@tonic-gate 	int		sig_done = 0;
27357c478bd9Sstevel@tonic-gate 	int		dbg_level;
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
27387c478bd9Sstevel@tonic-gate 		logit("getldap_refresh()...\n");
27397c478bd9Sstevel@tonic-gate 	}
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	/*
27427c478bd9Sstevel@tonic-gate 	 * wait for an available server
27437c478bd9Sstevel@tonic-gate 	 */
27447c478bd9Sstevel@tonic-gate 	while (sig_done == 0) {
27457c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&sig_mutex);
27467c478bd9Sstevel@tonic-gate 		sig_done = signal_done;
27477c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&sig_mutex);
27487c478bd9Sstevel@tonic-gate 	}
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_setServer(TRUE);
27517c478bd9Sstevel@tonic-gate 	while (always) {
27527c478bd9Sstevel@tonic-gate 		dbg_level = current_admin.debug_level;
27537c478bd9Sstevel@tonic-gate 		(void) rw_rdlock(&ldap_lock);
27547c478bd9Sstevel@tonic-gate 		sleeptime = current_admin.ldap_stat.ldap_ttl;
27557c478bd9Sstevel@tonic-gate 		if (dbg_level >= DBG_PROFILE_REFRESH) {
27567c478bd9Sstevel@tonic-gate 			logit("getldap_refresh: current profile TTL is %d "
27577c478bd9Sstevel@tonic-gate 			"seconds\n", current_admin.ldap_stat.ldap_ttl);
27587c478bd9Sstevel@tonic-gate 		}
27597c478bd9Sstevel@tonic-gate 		if (gettimeofday(&tp, NULL) == 0) {
27607c478bd9Sstevel@tonic-gate 			if ((__ns_ldap_getParam(NS_LDAP_EXP_P,
27617c478bd9Sstevel@tonic-gate 			    &paramVal, &errorp) == NS_LDAP_SUCCESS) &&
27627c478bd9Sstevel@tonic-gate 			    paramVal != NULL &&
27637c478bd9Sstevel@tonic-gate 			    (char *)*paramVal != NULL) {
27647c478bd9Sstevel@tonic-gate 				errno = 0;
27657c478bd9Sstevel@tonic-gate 				expire = atol((char *)*paramVal);
27667c478bd9Sstevel@tonic-gate 				(void) __ns_ldap_freeParam(&paramVal);
27677c478bd9Sstevel@tonic-gate 				if (errno == 0) {
27687c478bd9Sstevel@tonic-gate 					if (expire == 0) {
27697c478bd9Sstevel@tonic-gate 						first_time = 0;
27707c478bd9Sstevel@tonic-gate 						(void) rw_unlock(&ldap_lock);
27717c478bd9Sstevel@tonic-gate 						(void) cond_init(&cond,
27727c478bd9Sstevel@tonic-gate 						    NULL, NULL);
27737c478bd9Sstevel@tonic-gate 						(void) mutex_lock(&sighuplock);
27747c478bd9Sstevel@tonic-gate 						timeout.tv_sec =
27757c478bd9Sstevel@tonic-gate 						    CACHESLEEPTIME;
27767c478bd9Sstevel@tonic-gate 						timeout.tv_nsec = 0;
27777c478bd9Sstevel@tonic-gate 						if (dbg_level >=
27787c478bd9Sstevel@tonic-gate 						    DBG_PROFILE_REFRESH) {
27797c478bd9Sstevel@tonic-gate 							logit("getldap_refresh:"
2780*4a6b6ac4Schinlong 							    "(1)about to sleep"
2781*4a6b6ac4Schinlong 							    " for %d seconds\n",
27827c478bd9Sstevel@tonic-gate 							    CACHESLEEPTIME);
27837c478bd9Sstevel@tonic-gate 						}
27847c478bd9Sstevel@tonic-gate 						err = cond_reltimedwait(&cond,
27857c478bd9Sstevel@tonic-gate 						    &sighuplock, &timeout);
27867c478bd9Sstevel@tonic-gate 						(void) cond_destroy(&cond);
27877c478bd9Sstevel@tonic-gate 						(void) mutex_unlock(
27887c478bd9Sstevel@tonic-gate 						    &sighuplock);
27897c478bd9Sstevel@tonic-gate 						/*
27907c478bd9Sstevel@tonic-gate 						 * if woke up by
27917c478bd9Sstevel@tonic-gate 						 * getldap_revalidate(),
27927c478bd9Sstevel@tonic-gate 						 * do update right away
27937c478bd9Sstevel@tonic-gate 						 */
27947c478bd9Sstevel@tonic-gate 						if (err == ETIME)
27957c478bd9Sstevel@tonic-gate 							continue;
27967c478bd9Sstevel@tonic-gate 						else {
27977c478bd9Sstevel@tonic-gate 							/*
27987c478bd9Sstevel@tonic-gate 							 * if load
27997c478bd9Sstevel@tonic-gate 							 * configuration failed
28007c478bd9Sstevel@tonic-gate 							 * don't do update
28017c478bd9Sstevel@tonic-gate 							 */
28027c478bd9Sstevel@tonic-gate 							if (load_config())
2803*4a6b6ac4Schinlong 								perform_update
2804*4a6b6ac4Schinlong 								    ();
28057c478bd9Sstevel@tonic-gate 							continue;
28067c478bd9Sstevel@tonic-gate 						}
28077c478bd9Sstevel@tonic-gate 					}
28087c478bd9Sstevel@tonic-gate 					sleeptime = expire - tp.tv_sec;
28097c478bd9Sstevel@tonic-gate 					if (dbg_level >= DBG_PROFILE_REFRESH) {
2810*4a6b6ac4Schinlong 						logit("getldap_refresh: expire "
2811*4a6b6ac4Schinlong 						    "time = %ld\n", expire);
28127c478bd9Sstevel@tonic-gate 					}
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 				}
28157c478bd9Sstevel@tonic-gate 			}
28167c478bd9Sstevel@tonic-gate 		}
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&ldap_lock);
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 		/*
28217c478bd9Sstevel@tonic-gate 		 * if this is the first time downloading
28227c478bd9Sstevel@tonic-gate 		 * the profile or expire time already passed,
28237c478bd9Sstevel@tonic-gate 		 * do not wait, do update
28247c478bd9Sstevel@tonic-gate 		 */
28257c478bd9Sstevel@tonic-gate 		if (first_time == 0 && sleeptime > 0) {
28267c478bd9Sstevel@tonic-gate 			if (dbg_level >= DBG_PROFILE_REFRESH) {
28277c478bd9Sstevel@tonic-gate 				logit("getldap_refresh: (2)about to sleep "
28287c478bd9Sstevel@tonic-gate 				"for %d seconds\n", sleeptime);
28297c478bd9Sstevel@tonic-gate 			}
28307c478bd9Sstevel@tonic-gate 			(void) cond_init(&cond, NULL, NULL);
28317c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&sighuplock);
28327c478bd9Sstevel@tonic-gate 			timeout.tv_sec = sleeptime;
28337c478bd9Sstevel@tonic-gate 			timeout.tv_nsec = 0;
28347c478bd9Sstevel@tonic-gate 			err = cond_reltimedwait(&cond,
28357c478bd9Sstevel@tonic-gate 			    &sighuplock, &timeout);
28367c478bd9Sstevel@tonic-gate 			(void) cond_destroy(&cond);
28377c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&sighuplock);
28387c478bd9Sstevel@tonic-gate 		}
28397c478bd9Sstevel@tonic-gate 		/*
28407c478bd9Sstevel@tonic-gate 		 * if load concfiguration failed
28417c478bd9Sstevel@tonic-gate 		 * don't do update
28427c478bd9Sstevel@tonic-gate 		 */
28437c478bd9Sstevel@tonic-gate 		if (load_config())
28447c478bd9Sstevel@tonic-gate 			perform_update();
28457c478bd9Sstevel@tonic-gate 		first_time = 0;
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate }
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate void
28507c478bd9Sstevel@tonic-gate getldap_revalidate()
28517c478bd9Sstevel@tonic-gate {
28527c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
28537c478bd9Sstevel@tonic-gate 		logit("getldap_revalidate()...\n");
28547c478bd9Sstevel@tonic-gate 	}
28557c478bd9Sstevel@tonic-gate 	/* block signal SIGHUP */
28567c478bd9Sstevel@tonic-gate 	(void) sighold(SIGHUP);
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 	/* now awake the sleeping refresh thread */
28597c478bd9Sstevel@tonic-gate 	(void) cond_signal(&cond);
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	/* release signal SIGHUP */
28627c478bd9Sstevel@tonic-gate 	(void) sigrelse(SIGHUP);
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate }
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate void
28677c478bd9Sstevel@tonic-gate getldap_lookup(ldap_return_t *out, ldap_call_t *in)
28687c478bd9Sstevel@tonic-gate {
28697c478bd9Sstevel@tonic-gate 	LineBuf		configinfo;
28707c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	*error;
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
28737c478bd9Sstevel@tonic-gate 		logit("getldap_lookup()...\n");
28747c478bd9Sstevel@tonic-gate 	}
28757c478bd9Sstevel@tonic-gate 
28767c478bd9Sstevel@tonic-gate 	(void) rw_rdlock(&ldap_lock);
28777c478bd9Sstevel@tonic-gate 	if ((error = __ns_ldap_LoadDoorInfo(&configinfo, in->ldap_u.domainname))
28787c478bd9Sstevel@tonic-gate 	    != NULL) {
28797c478bd9Sstevel@tonic-gate 		if (error != NULL && error->message != NULL)
28807c478bd9Sstevel@tonic-gate 			logit("Error: ldap_lookup: %s\n", error->message);
28817c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&error);
28827c478bd9Sstevel@tonic-gate 		out->ldap_errno = -1;
28837c478bd9Sstevel@tonic-gate 		out->ldap_return_code = NOTFOUND;
28847c478bd9Sstevel@tonic-gate 		out->ldap_bufferbytesused = sizeof (*out);
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate 	} else {
28877c478bd9Sstevel@tonic-gate 		out->ldap_bufferbytesused = sizeof (ldap_return_t);
28887c478bd9Sstevel@tonic-gate 		(void) strncpy(out->ldap_u.config,
28897c478bd9Sstevel@tonic-gate 		    configinfo.str, configinfo.len);
28907c478bd9Sstevel@tonic-gate 		out->ldap_return_code = SUCCESS;
28917c478bd9Sstevel@tonic-gate 		out->ldap_errno = 0;
28927c478bd9Sstevel@tonic-gate 	}
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	if (configinfo.str != NULL) {
28957c478bd9Sstevel@tonic-gate 		free(configinfo.str);
28967c478bd9Sstevel@tonic-gate 		configinfo.str = NULL;
28977c478bd9Sstevel@tonic-gate 		configinfo.alloc = 0;
28987c478bd9Sstevel@tonic-gate 		configinfo.len = 0;
28997c478bd9Sstevel@tonic-gate 	}
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&ldap_lock);
29027c478bd9Sstevel@tonic-gate }
2903