xref: /titanic_44/usr/src/cmd/ldapcachemgr/cachemgr.c (revision dd1104fbe0f0f41434502f335b9f0b34999f771c)
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*dd1104fbSMichen Chang  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Simple doors ldap cache daemon
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <signal.h>
337c478bd9Sstevel@tonic-gate #include <door.h>
347c478bd9Sstevel@tonic-gate #include <time.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367ddae043Siz202018 #include <strings.h>
377c478bd9Sstevel@tonic-gate #include <libintl.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/time.h>
407c478bd9Sstevel@tonic-gate #include <sys/wait.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <pthread.h>
447c478bd9Sstevel@tonic-gate #include <thread.h>
457c478bd9Sstevel@tonic-gate #include <stdarg.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <assert.h>
487c478bd9Sstevel@tonic-gate #include <unistd.h>
497c478bd9Sstevel@tonic-gate #include <memory.h>
507c478bd9Sstevel@tonic-gate #include <sys/types.h>
517c478bd9Sstevel@tonic-gate #include <syslog.h>
527c478bd9Sstevel@tonic-gate #include <locale.h>	/* LC_ALL */
537ddae043Siz202018 
547ddae043Siz202018 #include <alloca.h>
557ddae043Siz202018 #include <ucontext.h>
56*dd1104fbSMichen Chang #include <stddef.h>	/* offsetof */
57*dd1104fbSMichen Chang #include <priv.h>
587ddae043Siz202018 
598142c2b2Schinlong #include "getxby_door.h"
607c478bd9Sstevel@tonic-gate #include "cachemgr.h"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static void	detachfromtty();
637c478bd9Sstevel@tonic-gate admin_t		current_admin;
647c478bd9Sstevel@tonic-gate static int	will_become_server;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static void switcher(void *cookie, char *argp, size_t arg_size,
677c478bd9Sstevel@tonic-gate 			door_desc_t *dp, uint_t n_desc);
687c478bd9Sstevel@tonic-gate static void usage(char *s);
697c478bd9Sstevel@tonic-gate static int cachemgr_set_lf(admin_t *ptr, char *logfile);
707c478bd9Sstevel@tonic-gate static int client_getadmin(admin_t *ptr);
717ddae043Siz202018 static int setadmin(ldap_call_t *ptr);
727c478bd9Sstevel@tonic-gate static  int client_setadmin(admin_t *ptr);
737c478bd9Sstevel@tonic-gate static int client_showstats(admin_t *ptr);
748142c2b2Schinlong static int is_root(int free_uc, char *dc_str, ucred_t **uc);
75*dd1104fbSMichen Chang static int is_root_or_all_privs(char *dc_str, ucred_t **ucp);
76*dd1104fbSMichen Chang static void admin_modify(LineBuf *config_info, ldap_call_t *in);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #ifdef SLP
797c478bd9Sstevel@tonic-gate int			use_slp = 0;
807c478bd9Sstevel@tonic-gate static unsigned int	refresh = 10800;	/* dynamic discovery interval */
817c478bd9Sstevel@tonic-gate #endif /* SLP */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static ldap_stat_t *
847c478bd9Sstevel@tonic-gate getcacheptr(char *s)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	static const char *caches[1] = {"ldap"};
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (strncmp(caches[0], s, strlen(caches[0])) == 0)
897c478bd9Sstevel@tonic-gate 		return (&current_admin.ldap_stat);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	return (NULL);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate char *
957c478bd9Sstevel@tonic-gate getcacheopt(char *s)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	while (*s && *s != ',')
987c478bd9Sstevel@tonic-gate 		s++;
997c478bd9Sstevel@tonic-gate 	return ((*s == ',') ? (s + 1) : NULL);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  *  This is here to prevent the ldap_cachemgr becomes
1047c478bd9Sstevel@tonic-gate  *  daemonlized to early to soon during boot time.
1057c478bd9Sstevel@tonic-gate  *  This causes problems during boot when automounter
1067c478bd9Sstevel@tonic-gate  *  and others try to use libsldap before ldap_cachemgr
1077c478bd9Sstevel@tonic-gate  *  finishes walking the server list.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static void
1107c478bd9Sstevel@tonic-gate sig_ok_to_exit(int signo)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	if (signo == SIGUSR1) {
1137c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): parent exiting...\n");
1147c478bd9Sstevel@tonic-gate 		exit(0);
1157c478bd9Sstevel@tonic-gate 	} else {
1167c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): invalid signal(%d) received.\n",
1177c478bd9Sstevel@tonic-gate 		    signo);
1187c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: "
1197c478bd9Sstevel@tonic-gate 		    "invalid signal(%d) received."), signo);
1207c478bd9Sstevel@tonic-gate 		exit(1);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate #define	LDAP_TABLES		1	/* ldap */
1247c478bd9Sstevel@tonic-gate #define	TABLE_THREADS		10
1257c478bd9Sstevel@tonic-gate #define	COMMON_THREADS		20
1267c478bd9Sstevel@tonic-gate #define	CACHE_MISS_THREADS	(COMMON_THREADS + LDAP_TABLES * TABLE_THREADS)
1277c478bd9Sstevel@tonic-gate #define	CACHE_HIT_THREADS	20
128e1dd0a2fSth160488 /*
129e1dd0a2fSth160488  * There is only one thread handling GETSTATUSCHANGE START from main nscd
130e1dd0a2fSth160488  * most of time. But it could happen that a main nscd is restarted, old main
131e1dd0a2fSth160488  * nscd's handling thread is still alive when new main nscd starts and sends
132e1dd0a2fSth160488  * START, or old main dies. STOP is not sent in both cases.
133e1dd0a2fSth160488  * The main nscd requires 2 threads to handle START and STOP. So max number
134e1dd0a2fSth160488  * of change threads is set to 4.
135e1dd0a2fSth160488  */
136e1dd0a2fSth160488 #define	MAX_CHG_THREADS		4
137e1dd0a2fSth160488 #define	MAX_SERVER_THREADS	(CACHE_HIT_THREADS + CACHE_MISS_THREADS + \
138e1dd0a2fSth160488 				MAX_CHG_THREADS)
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static sema_t common_sema;
1417c478bd9Sstevel@tonic-gate static sema_t ldap_sema;
1427c478bd9Sstevel@tonic-gate static thread_key_t lookup_state_key;
143e1dd0a2fSth160488 static int chg_threads_num = 0;
144e1dd0a2fSth160488 static mutex_t chg_threads_num_lock = DEFAULTMUTEX;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static void
1477c478bd9Sstevel@tonic-gate initialize_lookup_clearance()
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	(void) thr_keycreate(&lookup_state_key, NULL);
1507c478bd9Sstevel@tonic-gate 	(void) sema_init(&common_sema, COMMON_THREADS, USYNC_THREAD, 0);
1517c478bd9Sstevel@tonic-gate 	(void) sema_init(&ldap_sema, TABLE_THREADS, USYNC_THREAD, 0);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate int
1557c478bd9Sstevel@tonic-gate get_clearance(int callnumber)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
1587c478bd9Sstevel@tonic-gate 	char	*tab;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (sema_trywait(&common_sema) == 0) {
1617c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, NULL);
1627c478bd9Sstevel@tonic-gate 		return (0);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	switch (callnumber) {
1667c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
1677c478bd9Sstevel@tonic-gate 			tab = "ldap";
1687c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
1697c478bd9Sstevel@tonic-gate 			break;
1707c478bd9Sstevel@tonic-gate 		default:
1717c478bd9Sstevel@tonic-gate 			logit("Internal Error: get_clearance\n");
1727c478bd9Sstevel@tonic-gate 			break;
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (sema_trywait(table_sema) == 0) {
1767c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, (void*)1);
1777c478bd9Sstevel@tonic-gate 		return (0);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_CANT_FIND) {
1817c478bd9Sstevel@tonic-gate 		logit("get_clearance: throttling load for %s table\n", tab);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	return (-1);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate int
1887c478bd9Sstevel@tonic-gate release_clearance(int callnumber)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	int	which;
1917c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	(void) thr_getspecific(lookup_state_key, (void**)&which);
1947c478bd9Sstevel@tonic-gate 	if (which == 0) /* from common pool */ {
1957c478bd9Sstevel@tonic-gate 		(void) sema_post(&common_sema);
1967c478bd9Sstevel@tonic-gate 		return (0);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	switch (callnumber) {
2007c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
2017c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
2027c478bd9Sstevel@tonic-gate 			break;
2037c478bd9Sstevel@tonic-gate 		default:
2047c478bd9Sstevel@tonic-gate 			logit("Internal Error: release_clearance\n");
2057c478bd9Sstevel@tonic-gate 			break;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	(void) sema_post(table_sema);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	return (0);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate static mutex_t		create_lock;
2147c478bd9Sstevel@tonic-gate static int		num_servers = 0;
2157c478bd9Sstevel@tonic-gate static thread_key_t	server_key;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Bind a TSD value to a server thread. This enables the destructor to
2207c478bd9Sstevel@tonic-gate  * be called if/when this thread exits.  This would be a programming error,
2217c478bd9Sstevel@tonic-gate  * but better safe than sorry.
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2257c478bd9Sstevel@tonic-gate static void *
2267c478bd9Sstevel@tonic-gate server_tsd_bind(void *arg)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	static void	*value = 0;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/*
2317c478bd9Sstevel@tonic-gate 	 * disable cancellation to prevent hangs when server
2327c478bd9Sstevel@tonic-gate 	 * threads disappear
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	(void) thr_setspecific(server_key, value);
236cb5caa98Sdjl 	(void) door_return(NULL, 0, NULL, 0);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	return (value);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  * Server threads are created here.
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2467c478bd9Sstevel@tonic-gate static void
2477c478bd9Sstevel@tonic-gate server_create(door_info_t *dip)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
2507c478bd9Sstevel@tonic-gate 	if (++num_servers > MAX_SERVER_THREADS) {
2517c478bd9Sstevel@tonic-gate 		num_servers--;
2527c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&create_lock);
2537c478bd9Sstevel@tonic-gate 		return;
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
2567c478bd9Sstevel@tonic-gate 	(void) thr_create(NULL, 0, server_tsd_bind, NULL,
2577c478bd9Sstevel@tonic-gate 	    THR_BOUND|THR_DETACHED, NULL);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * Server thread are destroyed here
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2657c478bd9Sstevel@tonic-gate static void
2667c478bd9Sstevel@tonic-gate server_destroy(void *arg)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
2697c478bd9Sstevel@tonic-gate 	num_servers--;
2707c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
273a506a34cSth160488 int
2747c478bd9Sstevel@tonic-gate main(int argc, char ** argv)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	int			did;
2777c478bd9Sstevel@tonic-gate 	int			opt;
2787c478bd9Sstevel@tonic-gate 	int			errflg = 0;
2797c478bd9Sstevel@tonic-gate 	int			showstats = 0;
2807c478bd9Sstevel@tonic-gate 	int			doset = 0;
2817c478bd9Sstevel@tonic-gate 	int			dofg = 0;
2827c478bd9Sstevel@tonic-gate 	struct stat		buf;
2837c478bd9Sstevel@tonic-gate 	sigset_t		myset;
2847c478bd9Sstevel@tonic-gate 	struct sigaction	sighupaction;
2857c478bd9Sstevel@tonic-gate 	static void		client_killserver();
2867c478bd9Sstevel@tonic-gate 	int			debug_level = 0;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	/* setup for localization */
2897c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2907c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	openlog("ldap_cachemgr", LOG_PID, LOG_DAEMON);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (chdir(NSLDAPDIRECTORY) < 0) {
2957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("chdir(\"%s\") failed: %s\n"),
2967c478bd9Sstevel@tonic-gate 		    NSLDAPDIRECTORY, strerror(errno));
2977c478bd9Sstevel@tonic-gate 		exit(1);
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/*
3017c478bd9Sstevel@tonic-gate 	 * Correctly set file mode creation mask, so to make the new files
3027c478bd9Sstevel@tonic-gate 	 * created for door calls being readable by all.
3037c478bd9Sstevel@tonic-gate 	 */
3047c478bd9Sstevel@tonic-gate 	(void) umask(0);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 *  Special case non-root user here -	he/she/they/it can just print
3087c478bd9Sstevel@tonic-gate 	 *					stats
3097c478bd9Sstevel@tonic-gate 	 */
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if (geteuid()) {
3127c478bd9Sstevel@tonic-gate 		if (argc != 2 || strcmp(argv[1], "-g")) {
3137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3147c478bd9Sstevel@tonic-gate 			    gettext("Must be root to use any option "
3157c478bd9Sstevel@tonic-gate 			    "other than -g.\n\n"));
3167c478bd9Sstevel@tonic-gate 			usage(argv[0]);
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 
319e1dd0a2fSth160488 		if ((__ns_ldap_cache_ping() != NS_CACHE_SUCCESS) ||
3207c478bd9Sstevel@tonic-gate 		    (client_getadmin(&current_admin) != 0)) {
3217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3227c478bd9Sstevel@tonic-gate 			    gettext("%s doesn't appear to be running.\n"),
3237c478bd9Sstevel@tonic-gate 			    argv[0]);
3247c478bd9Sstevel@tonic-gate 			exit(1);
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		(void) client_showstats(&current_admin);
3277c478bd9Sstevel@tonic-gate 		exit(0);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 *  Determine if there is already a daemon running
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 
336e1dd0a2fSth160488 	will_become_server = (__ns_ldap_cache_ping() != NS_CACHE_SUCCESS);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/*
3397c478bd9Sstevel@tonic-gate 	 *  load normal config file
3407c478bd9Sstevel@tonic-gate 	 */
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (will_become_server) {
3437c478bd9Sstevel@tonic-gate 		static const ldap_stat_t defaults = {
3447c478bd9Sstevel@tonic-gate 			0,		/* stat */
3457c478bd9Sstevel@tonic-gate 			DEFAULTTTL};	/* ttl */
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat = defaults;
3487c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, LOGFILE);
3497c478bd9Sstevel@tonic-gate 	} else {
3507c478bd9Sstevel@tonic-gate 		if (client_getadmin(&current_admin)) {
3517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Cannot contact %s "
3527c478bd9Sstevel@tonic-gate 			    "properly(?)\n"), argv[0]);
3537c478bd9Sstevel@tonic-gate 			exit(1);
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate #ifndef SLP
3587c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgl:r:d:")) != EOF) {
3597c478bd9Sstevel@tonic-gate #else
3607c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgs:l:r:d:")) != EOF) {
3617c478bd9Sstevel@tonic-gate #endif /* SLP */
3627c478bd9Sstevel@tonic-gate 		ldap_stat_t	*cache;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		switch (opt) {
3657c478bd9Sstevel@tonic-gate 		case 'K':
3667c478bd9Sstevel@tonic-gate 			client_killserver();
3677c478bd9Sstevel@tonic-gate 			exit(0);
3687c478bd9Sstevel@tonic-gate 			break;
3697c478bd9Sstevel@tonic-gate 		case 'g':
3707c478bd9Sstevel@tonic-gate 			showstats++;
3717c478bd9Sstevel@tonic-gate 			break;
3727c478bd9Sstevel@tonic-gate 		case 'f':
3737c478bd9Sstevel@tonic-gate 			dofg++;
3747c478bd9Sstevel@tonic-gate 			break;
3757c478bd9Sstevel@tonic-gate 		case 'r':
3767c478bd9Sstevel@tonic-gate 			doset++;
3777c478bd9Sstevel@tonic-gate 			cache = getcacheptr("ldap");
3787c478bd9Sstevel@tonic-gate 			if (!optarg) {
3797c478bd9Sstevel@tonic-gate 				errflg++;
3807c478bd9Sstevel@tonic-gate 				break;
3817c478bd9Sstevel@tonic-gate 			}
3827c478bd9Sstevel@tonic-gate 			cache->ldap_ttl = atoi(optarg);
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate 		case 'l':
3857c478bd9Sstevel@tonic-gate 			doset++;
3867c478bd9Sstevel@tonic-gate 			(void) strlcpy(current_admin.logfile,
3877c478bd9Sstevel@tonic-gate 			    optarg, sizeof (current_admin.logfile));
3887c478bd9Sstevel@tonic-gate 			break;
3897c478bd9Sstevel@tonic-gate 		case 'd':
3907c478bd9Sstevel@tonic-gate 			doset++;
3917c478bd9Sstevel@tonic-gate 			debug_level = atoi(optarg);
3927c478bd9Sstevel@tonic-gate 			break;
3937c478bd9Sstevel@tonic-gate #ifdef SLP
3947c478bd9Sstevel@tonic-gate 		case 's':	/* undocumented: use dynamic (SLP) config */
3957c478bd9Sstevel@tonic-gate 			use_slp = 1;
3967c478bd9Sstevel@tonic-gate 			break;
3977c478bd9Sstevel@tonic-gate #endif /* SLP */
3987c478bd9Sstevel@tonic-gate 		default:
3997c478bd9Sstevel@tonic-gate 			errflg++;
4007c478bd9Sstevel@tonic-gate 			break;
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (errflg)
4057c478bd9Sstevel@tonic-gate 		usage(argv[0]);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/*
4087c478bd9Sstevel@tonic-gate 	 * will not show statistics if no daemon running
4097c478bd9Sstevel@tonic-gate 	 */
4107c478bd9Sstevel@tonic-gate 	if (will_become_server && showstats) {
4117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4127c478bd9Sstevel@tonic-gate 		    gettext("%s doesn't appear to be running.\n"),
4137c478bd9Sstevel@tonic-gate 		    argv[0]);
4147c478bd9Sstevel@tonic-gate 		exit(1);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (!will_become_server) {
4187c478bd9Sstevel@tonic-gate 		if (showstats) {
4197c478bd9Sstevel@tonic-gate 			(void) client_showstats(&current_admin);
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 		if (doset) {
4227c478bd9Sstevel@tonic-gate 			current_admin.debug_level = debug_level;
4237c478bd9Sstevel@tonic-gate 			if (client_setadmin(&current_admin) < 0) {
4247c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4257c478bd9Sstevel@tonic-gate 				    gettext("Error during admin call\n"));
4267c478bd9Sstevel@tonic-gate 				exit(1);
4277c478bd9Sstevel@tonic-gate 			}
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 		if (!showstats && !doset) {
4307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4317c478bd9Sstevel@tonic-gate 			gettext("%s already running....use '%s "
4327c478bd9Sstevel@tonic-gate 			    "-K' to stop\n"), argv[0], argv[0]);
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		exit(0);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 *   daemon from here on
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if (debug_level) {
4427c478bd9Sstevel@tonic-gate 		/*
4437c478bd9Sstevel@tonic-gate 		 * we're debugging...
4447c478bd9Sstevel@tonic-gate 		 */
4457c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
4467c478bd9Sstevel@tonic-gate 			/*
4477c478bd9Sstevel@tonic-gate 			 * no specified log file
4487c478bd9Sstevel@tonic-gate 			 */
4497c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, LOGFILE);
4507c478bd9Sstevel@tonic-gate 		else
4517c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
4527c478bd9Sstevel@tonic-gate 			    current_admin.logfile);
4537c478bd9Sstevel@tonic-gate 		/*
4547c478bd9Sstevel@tonic-gate 		 * validate the range of debug level number
4557c478bd9Sstevel@tonic-gate 		 * and set the number to current_admin.debug_level
4567c478bd9Sstevel@tonic-gate 		 */
4577c478bd9Sstevel@tonic-gate 		if (cachemgr_set_dl(&current_admin, debug_level) < 0) {
4587c478bd9Sstevel@tonic-gate 				/*
4597c478bd9Sstevel@tonic-gate 				 * print error messages to the screen
4607c478bd9Sstevel@tonic-gate 				 * cachemgr_set_dl prints msgs to cachemgr.log
4617c478bd9Sstevel@tonic-gate 				 * only
4627c478bd9Sstevel@tonic-gate 				 */
4637c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4647c478bd9Sstevel@tonic-gate 				gettext("Incorrect Debug Level: %d\n"
4657c478bd9Sstevel@tonic-gate 				"It should be between %d and %d\n"),
4667c478bd9Sstevel@tonic-gate 				    debug_level, DBG_OFF, MAXDEBUG);
4677c478bd9Sstevel@tonic-gate 			exit(-1);
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 	} else {
4707c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
4717c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, "/dev/null");
4727c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
4737c478bd9Sstevel@tonic-gate 			    current_admin.logfile);
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if (dofg == 0)
4777c478bd9Sstevel@tonic-gate 		detachfromtty(argv[0]);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/*
4807c478bd9Sstevel@tonic-gate 	 * perform some initialization
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	initialize_lookup_clearance();
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (getldap_init() != 0)
4867c478bd9Sstevel@tonic-gate 		exit(-1);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * Establish our own server thread pool
4907c478bd9Sstevel@tonic-gate 	 */
4917c478bd9Sstevel@tonic-gate 
492cb5caa98Sdjl 	(void) door_server_create(server_create);
4937c478bd9Sstevel@tonic-gate 	if (thr_keycreate(&server_key, server_destroy) != 0) {
4947c478bd9Sstevel@tonic-gate 		logit("thr_keycreate() call failed\n");
4957c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4967c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: thr_keycreate() call failed"));
4977c478bd9Sstevel@tonic-gate 		perror("thr_keycreate");
4987c478bd9Sstevel@tonic-gate 		exit(-1);
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	/*
5027c478bd9Sstevel@tonic-gate 	 * Create a door
5037c478bd9Sstevel@tonic-gate 	 */
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if ((did = door_create(switcher, LDAP_CACHE_DOOR_COOKIE,
5067c478bd9Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
5077c478bd9Sstevel@tonic-gate 		logit("door_create() call failed\n");
5087c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
5097c478bd9Sstevel@tonic-gate 		    "ldap_cachemgr: door_create() call failed"));
5107c478bd9Sstevel@tonic-gate 		perror("door_create");
5117c478bd9Sstevel@tonic-gate 		exit(-1);
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	/*
5157c478bd9Sstevel@tonic-gate 	 * bind to file system
5167c478bd9Sstevel@tonic-gate 	 */
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	if (stat(LDAP_CACHE_DOOR, &buf) < 0) {
5197c478bd9Sstevel@tonic-gate 		int	newfd;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		if ((newfd = creat(LDAP_CACHE_DOOR, 0444)) < 0) {
5227c478bd9Sstevel@tonic-gate 			logit("Cannot create %s:%s\n",
5237c478bd9Sstevel@tonic-gate 			    LDAP_CACHE_DOOR,
5247c478bd9Sstevel@tonic-gate 			    strerror(errno));
5257c478bd9Sstevel@tonic-gate 			exit(1);
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 		(void) close(newfd);
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (fattach(did, LDAP_CACHE_DOOR) < 0) {
5317c478bd9Sstevel@tonic-gate 		if ((errno != EBUSY) ||
5327c478bd9Sstevel@tonic-gate 		    (fdetach(LDAP_CACHE_DOOR) <  0) ||
5337c478bd9Sstevel@tonic-gate 		    (fattach(did, LDAP_CACHE_DOOR) < 0)) {
5347c478bd9Sstevel@tonic-gate 			logit("fattach() call failed\n");
5357c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
5367c478bd9Sstevel@tonic-gate 			    "ldap_cachemgr: fattach() call failed"));
5377c478bd9Sstevel@tonic-gate 			perror("fattach");
5387c478bd9Sstevel@tonic-gate 			exit(2);
5397c478bd9Sstevel@tonic-gate 		}
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/* catch SIGHUP revalid signals */
5437c478bd9Sstevel@tonic-gate 	sighupaction.sa_handler = getldap_revalidate;
5447c478bd9Sstevel@tonic-gate 	sighupaction.sa_flags = 0;
5457c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sighupaction.sa_mask);
5467c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&myset);
5477c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGHUP);
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (sigaction(SIGHUP, &sighupaction, NULL) < 0) {
5507c478bd9Sstevel@tonic-gate 		logit("sigaction() call failed\n");
5517c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5527c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: sigaction() call failed"));
5537c478bd9Sstevel@tonic-gate 		perror("sigaction");
5547c478bd9Sstevel@tonic-gate 		exit(1);
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_BLOCK, &myset, NULL) < 0) {
5587c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
5597c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5607c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: thr_sigsetmask() call failed"));
5617c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
5627c478bd9Sstevel@tonic-gate 		exit(1);
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	/*
5667c478bd9Sstevel@tonic-gate 	 *  kick off revalidate threads only if ttl != 0
5677c478bd9Sstevel@tonic-gate 	 */
5687c478bd9Sstevel@tonic-gate 
569e1dd0a2fSth160488 	if (thr_create(NULL, 0, (void *(*)(void*))getldap_refresh,
570e1dd0a2fSth160488 	    NULL, 0, NULL) != 0) {
5717c478bd9Sstevel@tonic-gate 		logit("thr_create() call failed\n");
5727c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5737c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: thr_create() call failed"));
5747c478bd9Sstevel@tonic-gate 		perror("thr_create");
5757c478bd9Sstevel@tonic-gate 		exit(1);
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/*
5797c478bd9Sstevel@tonic-gate 	 *  kick off the thread which refreshes the server info
5807c478bd9Sstevel@tonic-gate 	 */
5817c478bd9Sstevel@tonic-gate 
582e1dd0a2fSth160488 	if (thr_create(NULL, 0, (void *(*)(void*))getldap_serverInfo_refresh,
583e1dd0a2fSth160488 	    NULL, 0, NULL) != 0) {
5847c478bd9Sstevel@tonic-gate 		logit("thr_create() call failed\n");
5857c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5867c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: thr_create() call failed"));
5877c478bd9Sstevel@tonic-gate 		perror("thr_create");
5887c478bd9Sstevel@tonic-gate 		exit(1);
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 
591e1dd0a2fSth160488 	/*
592e1dd0a2fSth160488 	 * kick off the thread which cleans up waiting threads for
593e1dd0a2fSth160488 	 * GETSTATUSCHANGE
594e1dd0a2fSth160488 	 */
595e1dd0a2fSth160488 
596e1dd0a2fSth160488 	if (thr_create(NULL, 0, chg_cleanup_waiting_threads,
597e1dd0a2fSth160488 	    NULL, 0, NULL) != 0) {
598e1dd0a2fSth160488 		logit("thr_create() chg_cleanup_waiting_threads call failed\n");
599e1dd0a2fSth160488 		syslog(LOG_ERR,
600e1dd0a2fSth160488 		    gettext("ldap_cachemgr: thr_create() "
601e1dd0a2fSth160488 		    "chg_cleanup_waiting_threads call failed"));
602e1dd0a2fSth160488 		exit(1);
603e1dd0a2fSth160488 	}
604e1dd0a2fSth160488 
6057c478bd9Sstevel@tonic-gate #ifdef SLP
6067c478bd9Sstevel@tonic-gate 	if (use_slp) {
6077c478bd9Sstevel@tonic-gate 		/* kick off SLP discovery thread */
608e1dd0a2fSth160488 		if (thr_create(NULL, 0, (void *(*)(void *))discover,
6097c478bd9Sstevel@tonic-gate 		    (void *)&refresh, 0, NULL) != 0) {
6107c478bd9Sstevel@tonic-gate 			logit("thr_create() call failed\n");
6117c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("ldap_cachemgr: thr_create() "
6127c478bd9Sstevel@tonic-gate 			    "call failed"));
6137c478bd9Sstevel@tonic-gate 			perror("thr_create");
6147c478bd9Sstevel@tonic-gate 			exit(1);
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate #endif /* SLP */
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_UNBLOCK, &myset, NULL) < 0) {
6207c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
6217c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
6227c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: the_sigsetmask() call failed"));
6237c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
6247c478bd9Sstevel@tonic-gate 		exit(1);
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
6287c478bd9Sstevel@tonic-gate 	while (1) {
6297c478bd9Sstevel@tonic-gate 		(void) pause();
6307c478bd9Sstevel@tonic-gate 	}
631cb5caa98Sdjl 	/* NOTREACHED */
632cb5caa98Sdjl 	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 
6367ddae043Siz202018 /*
6377ddae043Siz202018  * Before calling the alloca() function we have to be sure that we won't get
6387ddae043Siz202018  * beyond the stack. Since we don't know the precise layout of the stack,
6397ddae043Siz202018  * the address of an automatic of the function gives us a rough idea, plus/minus
6407ddae043Siz202018  * a bit. We also need a bit more of stackspace after the call to be able
6417ddae043Siz202018  * to call further functions. Even something as simple as making a system call
6427ddae043Siz202018  * from within this function can take ~100 Bytes of stackspace.
6437ddae043Siz202018  */
6447ddae043Siz202018 #define	SAFETY_BUFFER 32 * 1024 /* 32KB */
6457ddae043Siz202018 
6467ddae043Siz202018 static
6477ddae043Siz202018 size_t
6487ddae043Siz202018 get_data_size(LineBuf *config_info, int *err_code)
6497ddae043Siz202018 {
6507ddae043Siz202018 	size_t		configSize = sizeof (ldap_return_t);
6517ddae043Siz202018 	dataunion	*buf = NULL; /* For the 'sizeof' purpose */
6527ddae043Siz202018 
6537ddae043Siz202018 	if (config_info->str != NULL &&
6547ddae043Siz202018 	    config_info->len >= sizeof (buf->data.ldap_ret.ldap_u.config)) {
6557ddae043Siz202018 		configSize = sizeof (buf->space) +
6567ddae043Siz202018 		    config_info->len -
6577ddae043Siz202018 		    sizeof (buf->data.ldap_ret.ldap_u.config);
6587ddae043Siz202018 
6597ddae043Siz202018 		if (!stack_inbounds((char *)&buf -
6607ddae043Siz202018 		    (configSize + SAFETY_BUFFER))) {
6617ddae043Siz202018 			/*
6627ddae043Siz202018 			 * We do not have enough space on the stack
6637ddae043Siz202018 			 * to accomodate the whole DUAProfile
6647ddae043Siz202018 			 */
6657ddae043Siz202018 			logit("The DUAProfile is too big. There is not enough "
6667ddae043Siz202018 			    "space to process it. Ignoring it.\n");
6677ddae043Siz202018 			syslog(LOG_ERR, gettext("ldap_cachemgr: The DUAProfile "
6687ddae043Siz202018 			    "is too big. There is not enough space "
6697ddae043Siz202018 			    "to process it. Ignoring it."));
6707ddae043Siz202018 
671e1dd0a2fSth160488 			*err_code = NS_CACHE_SERVERERROR;
6727ddae043Siz202018 
6737ddae043Siz202018 			free(config_info->str);
6747ddae043Siz202018 			config_info->str = NULL;
6757ddae043Siz202018 			config_info->len = 0;
6767ddae043Siz202018 			configSize = sizeof (ldap_return_t);
6777ddae043Siz202018 		}
6787ddae043Siz202018 	}
6797ddae043Siz202018 
6807ddae043Siz202018 	return (configSize);
6817ddae043Siz202018 }
6827ddae043Siz202018 
6837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6847c478bd9Sstevel@tonic-gate static void
6857c478bd9Sstevel@tonic-gate switcher(void *cookie, char *argp, size_t arg_size,
6867c478bd9Sstevel@tonic-gate     door_desc_t *dp, uint_t n_desc)
6877c478bd9Sstevel@tonic-gate {
6887ddae043Siz202018 #define	GETSIZE 1000
6897ddae043Siz202018 #define	ALLOCATE 1001
6907ddae043Siz202018 
6917c478bd9Sstevel@tonic-gate 	ldap_call_t	*ptr = (ldap_call_t *)argp;
6928142c2b2Schinlong 	ucred_t		*uc = NULL;
6937c478bd9Sstevel@tonic-gate 
6947ddae043Siz202018 	LineBuf		configInfo;
6957ddae043Siz202018 	dataunion	*buf = NULL;
696e1dd0a2fSth160488 
6977ddae043Siz202018 	/*
6987ddae043Siz202018 	 * By default the size of  a buffer to be passed down to a client
6997ddae043Siz202018 	 * is equal to the size of the ldap_return_t structure. We need
7007ddae043Siz202018 	 * a bigger buffer in a few cases.
7017ddae043Siz202018 	 */
7027ddae043Siz202018 	size_t		configSize = sizeof (ldap_return_t);
7038142c2b2Schinlong 	int		ldapErrno = 0, state, callnumber;
7047ddae043Siz202018 	struct {
7057ddae043Siz202018 		void	*begin;
7067ddae043Siz202018 		size_t	size;
7077ddae043Siz202018 		uint8_t	destroy;
7087ddae043Siz202018 	} dataSource;
7097ddae043Siz202018 
7107c478bd9Sstevel@tonic-gate 	if (argp == DOOR_UNREF_DATA) {
7117c478bd9Sstevel@tonic-gate 		logit("Door Slam... invalid door param\n");
7127c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: Door Slam... "
7137c478bd9Sstevel@tonic-gate 		    "invalid door param"));
7147c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Door Slam... invalid door param\n"));
7157c478bd9Sstevel@tonic-gate 		exit(0);
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	if (ptr == NULL) { /* empty door call */
7197c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0); /* return the favor */
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227ddae043Siz202018 	bzero(&dataSource, sizeof (dataSource));
7237ddae043Siz202018 
7247ddae043Siz202018 	/*
7257ddae043Siz202018 	 * We presume that sizeof (ldap_return_t) bytes are always available
7267ddae043Siz202018 	 * on the stack
7277ddae043Siz202018 	 */
7288142c2b2Schinlong 	callnumber = ptr->ldap_callnumber;
7297ddae043Siz202018 
7308142c2b2Schinlong 	switch (callnumber) {
7317c478bd9Sstevel@tonic-gate 		case NULLCALL:
7327ddae043Siz202018 			/*
7337ddae043Siz202018 			 * Just a 'ping'. Use the default size
7347ddae043Siz202018 			 * of the buffer and set the
7357ddae043Siz202018 			 * 'OK' error code.
7367ddae043Siz202018 			 */
7377ddae043Siz202018 			state = ALLOCATE;
7387c478bd9Sstevel@tonic-gate 			break;
7397c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
7407ddae043Siz202018 			/*
7417ddae043Siz202018 			 * Get the current LDAP configuration.
7427ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
7437ddae043Siz202018 			 * the size of ldap_return_t, the next step will
7447ddae043Siz202018 			 * calculate who much space exactly is required.
7457ddae043Siz202018 			 */
7467ddae043Siz202018 			getldap_lookup(&configInfo, ptr);
7477ddae043Siz202018 
7487ddae043Siz202018 			state = GETSIZE;
7497ddae043Siz202018 			break;
7507ddae043Siz202018 		case GETLDAPSERVER:
7517ddae043Siz202018 			/*
7527ddae043Siz202018 			 * Get the root DSE for a next server in the list.
7537ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
7547ddae043Siz202018 			 * the size of ldap_return_t, the next step will
7557ddae043Siz202018 			 * calculate who much space exactly is required.
7567ddae043Siz202018 			 */
7577ddae043Siz202018 			getldap_getserver(&configInfo, ptr);
7587ddae043Siz202018 
7597ddae043Siz202018 			state = GETSIZE;
7607ddae043Siz202018 			break;
7617ddae043Siz202018 		case GETCACHESTAT:
7627ddae043Siz202018 			/*
7637ddae043Siz202018 			 * Get the cache stattistics.
7647ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
7657ddae043Siz202018 			 * the size of ldap_return_t, the next step will
7667ddae043Siz202018 			 * calculate how much space exactly is required.
7677ddae043Siz202018 			 */
7687ddae043Siz202018 			getldap_get_cacheStat(&configInfo);
7697ddae043Siz202018 
7707ddae043Siz202018 			state = GETSIZE;
7717c478bd9Sstevel@tonic-gate 			break;
7727c478bd9Sstevel@tonic-gate 		case GETADMIN:
7737ddae043Siz202018 			/*
7747ddae043Siz202018 			 * Get current configuration and statistics.
7757ddae043Siz202018 			 * The size of the statistics structure is less then
7767ddae043Siz202018 			 * sizeof (ldap_return_t). So specify the source
7777ddae043Siz202018 			 * where to take the info and proceed with the memory
7787ddae043Siz202018 			 * allocation.
7797ddae043Siz202018 			 */
7807ddae043Siz202018 			state = ALLOCATE;
7817ddae043Siz202018 
7827ddae043Siz202018 			if (ldapErrno == 0) {
7837ddae043Siz202018 				dataSource.begin = &current_admin;
7847ddae043Siz202018 				dataSource.size = sizeof (current_admin);
7857ddae043Siz202018 				dataSource.destroy = 0;
7867ddae043Siz202018 			}
7877c478bd9Sstevel@tonic-gate 			break;
7887c478bd9Sstevel@tonic-gate 		case KILLSERVER:
7897ddae043Siz202018 			/*
7907ddae043Siz202018 			 * Process the request and proceed with the default
7917ddae043Siz202018 			 * buffer allocation.
7927ddae043Siz202018 			 */
7938142c2b2Schinlong 			if (is_root(1, "KILLSERVER", &uc))
7947c478bd9Sstevel@tonic-gate 				exit(0);
7958142c2b2Schinlong 
7968142c2b2Schinlong 			ldapErrno = -1;
7978142c2b2Schinlong 			state = ALLOCATE;
7987c478bd9Sstevel@tonic-gate 			break;
7997ddae043Siz202018 		case SETADMIN:
8008142c2b2Schinlong 			/*
8018142c2b2Schinlong 			 * Process the request and proceed with the default
8028142c2b2Schinlong 			 * buffer allocation.
8038142c2b2Schinlong 			 */
8048142c2b2Schinlong 			if (is_root(1, "SETADMIN", &uc))
8057ddae043Siz202018 				ldapErrno = setadmin(ptr);
8068142c2b2Schinlong 			else
8077ddae043Siz202018 				ldapErrno = -1;
8087ddae043Siz202018 
8097ddae043Siz202018 			state = ALLOCATE;
8107c478bd9Sstevel@tonic-gate 			break;
8117c478bd9Sstevel@tonic-gate 		case GETCACHE:
8127ddae043Siz202018 			/*
8137ddae043Siz202018 			 * Get the cache stattistics.
8147ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
8157ddae043Siz202018 			 * the size of ldap_return_t, the next step will
8167ddae043Siz202018 			 * calculate how much space exactly is required.
8177ddae043Siz202018 			 */
8187ddae043Siz202018 			getldap_get_cacheData(&configInfo, ptr);
8197ddae043Siz202018 
8207ddae043Siz202018 			state = GETSIZE;
8217c478bd9Sstevel@tonic-gate 			break;
8227c478bd9Sstevel@tonic-gate 		case SETCACHE:
8237ddae043Siz202018 			/*
8247ddae043Siz202018 			 * Process the request and proceed with the default
8257ddae043Siz202018 			 * buffer allocation.
8267ddae043Siz202018 			 */
8278142c2b2Schinlong 			if (is_root(0, "SETCACHE", &uc) &&
828e1dd0a2fSth160488 			    is_called_from_nscd(ucred_getpid(uc))) {
8297ddae043Siz202018 				ldapErrno = getldap_set_cacheData(ptr);
8307c478bd9Sstevel@tonic-gate 				current_admin.ldap_stat.ldap_numbercalls++;
8318142c2b2Schinlong 			} else
8328142c2b2Schinlong 				ldapErrno = -1;
8337ddae043Siz202018 
8348142c2b2Schinlong 			if (uc != NULL)
8358142c2b2Schinlong 				ucred_free(uc);
8367ddae043Siz202018 			state = ALLOCATE;
8377c478bd9Sstevel@tonic-gate 			break;
838*dd1104fbSMichen Chang 		case ADMINMODIFY:
839*dd1104fbSMichen Chang 			admin_modify(&configInfo, ptr);
840*dd1104fbSMichen Chang 
841*dd1104fbSMichen Chang 			state = GETSIZE;
842*dd1104fbSMichen Chang 			break;
843e1dd0a2fSth160488 		case GETSTATUSCHANGE:
844e1dd0a2fSth160488 			/*
845e1dd0a2fSth160488 			 * Process the request and proceed with the default
846e1dd0a2fSth160488 			 * buffer allocation.
847e1dd0a2fSth160488 			 */
848e1dd0a2fSth160488 			(void) mutex_lock(&chg_threads_num_lock);
849e1dd0a2fSth160488 			chg_threads_num++;
850e1dd0a2fSth160488 			if (chg_threads_num > MAX_CHG_THREADS) {
851e1dd0a2fSth160488 				chg_threads_num--;
852e1dd0a2fSth160488 				(void) mutex_unlock(&chg_threads_num_lock);
853e1dd0a2fSth160488 				ldapErrno = CHG_EXCEED_MAX_THREADS;
854e1dd0a2fSth160488 				state = ALLOCATE;
855e1dd0a2fSth160488 				break;
856e1dd0a2fSth160488 			}
857e1dd0a2fSth160488 			(void) mutex_unlock(&chg_threads_num_lock);
858e1dd0a2fSth160488 
859e1dd0a2fSth160488 			if (is_root(0, "GETSTATUSCHANGE", &uc) &&
860e1dd0a2fSth160488 			    is_called_from_nscd(ucred_getpid(uc))) {
861e1dd0a2fSth160488 				ldapErrno = chg_get_statusChange(
862e1dd0a2fSth160488 				    &configInfo, ptr, ucred_getpid(uc));
863e1dd0a2fSth160488 				state = GETSIZE;
864e1dd0a2fSth160488 			} else {
865e1dd0a2fSth160488 				ldapErrno = -1;
866e1dd0a2fSth160488 				state = ALLOCATE;
867e1dd0a2fSth160488 			}
868e1dd0a2fSth160488 			if (uc != NULL)
869e1dd0a2fSth160488 				ucred_free(uc);
870e1dd0a2fSth160488 
871e1dd0a2fSth160488 			(void) mutex_lock(&chg_threads_num_lock);
872e1dd0a2fSth160488 			chg_threads_num--;
873e1dd0a2fSth160488 			(void) mutex_unlock(&chg_threads_num_lock);
874e1dd0a2fSth160488 			break;
8757c478bd9Sstevel@tonic-gate 		default:
8767ddae043Siz202018 			/*
8777ddae043Siz202018 			 * This means an unknown request type. Proceed with
8787ddae043Siz202018 			 * the default buffer allocation.
8797ddae043Siz202018 			 */
8807c478bd9Sstevel@tonic-gate 			logit("Unknown ldap service door call op %d\n",
8817c478bd9Sstevel@tonic-gate 			    ptr->ldap_callnumber);
8827ddae043Siz202018 			ldapErrno = -99;
8837ddae043Siz202018 
8847ddae043Siz202018 			state = ALLOCATE;
8857ddae043Siz202018 			break;
8868142c2b2Schinlong 	}
8878142c2b2Schinlong 
8888142c2b2Schinlong 	switch (state) {
8897ddae043Siz202018 		case GETSIZE:
8907ddae043Siz202018 			/*
8917ddae043Siz202018 			 * This stage calculates how much data will be
8927ddae043Siz202018 			 * passed down to the client, checks if there is
8937ddae043Siz202018 			 * enough space on the stack to accommodate the data,
8947ddae043Siz202018 			 * increases the value of the configSize variable
8957ddae043Siz202018 			 * if necessary and specifies the data source.
8967ddae043Siz202018 			 * In case of any error occurred ldapErrno will be set
8977ddae043Siz202018 			 * appropriately.
8987ddae043Siz202018 			 */
8997ddae043Siz202018 			if (configInfo.str == NULL) {
9007ddae043Siz202018 				ldapErrno = -1;
9017ddae043Siz202018 			}
9027ddae043Siz202018 
9037ddae043Siz202018 			configSize = get_data_size(&configInfo, &ldapErrno);
9047ddae043Siz202018 
9057ddae043Siz202018 			if (ldapErrno == 0) {
9067ddae043Siz202018 				dataSource.begin = configInfo.str;
9077ddae043Siz202018 				dataSource.size = configInfo.len;
9087ddae043Siz202018 				dataSource.destroy = 1;
9097ddae043Siz202018 			}
9107ddae043Siz202018 
9117ddae043Siz202018 			current_admin.ldap_stat.ldap_numbercalls++;
9128142c2b2Schinlong 			/* FALLTHRU */
9137ddae043Siz202018 		case ALLOCATE:
9147ddae043Siz202018 			/*
9157ddae043Siz202018 			 * Allocate a buffer of the calculated (or default) size
9167ddae043Siz202018 			 * and proceed with populating it with data.
9177ddae043Siz202018 			 */
9187ddae043Siz202018 			buf = (dataunion *) alloca(configSize);
9197ddae043Siz202018 
9207ddae043Siz202018 			/*
9217ddae043Siz202018 			 * Set a return code and, if a data source is specified,
9227ddae043Siz202018 			 * copy data from the source to the buffer.
9237ddae043Siz202018 			 */
9247ddae043Siz202018 			buf->data.ldap_ret.ldap_errno = ldapErrno;
9257ddae043Siz202018 			buf->data.ldap_ret.ldap_return_code = ldapErrno;
9267ddae043Siz202018 			buf->data.ldap_ret.ldap_bufferbytesused = configSize;
9277ddae043Siz202018 
9287ddae043Siz202018 			if (dataSource.begin != NULL) {
9297ddae043Siz202018 				(void) memcpy(buf->data.ldap_ret.ldap_u.config,
9307ddae043Siz202018 				    dataSource.begin,
9317ddae043Siz202018 				    dataSource.size);
9327ddae043Siz202018 				if (dataSource.destroy) {
9337ddae043Siz202018 					free(dataSource.begin);
9347ddae043Siz202018 				}
9357ddae043Siz202018 			}
9367ddae043Siz202018 
9377c478bd9Sstevel@tonic-gate 	}
9387ddae043Siz202018 	(void) door_return((char *)&buf->data,
9397ddae043Siz202018 	    buf->data.ldap_ret.ldap_bufferbytesused,
9407ddae043Siz202018 	    NULL,
9417ddae043Siz202018 	    0);
9427ddae043Siz202018 #undef	GETSIZE
9437ddae043Siz202018 #undef	ALLOCATE
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate static void
9477c478bd9Sstevel@tonic-gate usage(char *s)
9487c478bd9Sstevel@tonic-gate {
9497c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
9507c478bd9Sstevel@tonic-gate 	    gettext("Usage: %s [-d debug_level] [-l logfilename]\n"), s);
9517c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-K] "
9527c478bd9Sstevel@tonic-gate 	    "[-r revalidate_interval] "));
9537c478bd9Sstevel@tonic-gate #ifndef SLP
9547c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g]\n"));
9557c478bd9Sstevel@tonic-gate #else
9567c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g] [-s]\n"));
9577c478bd9Sstevel@tonic-gate #endif /* SLP */
9587c478bd9Sstevel@tonic-gate 	exit(1);
9597c478bd9Sstevel@tonic-gate }
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate static int	logfd = -1;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate static int
9657c478bd9Sstevel@tonic-gate cachemgr_set_lf(admin_t *ptr, char *logfile)
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate 	int	newlogfd;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	/*
9707c478bd9Sstevel@tonic-gate 	 *  we don't really want to try and open the log file
9717c478bd9Sstevel@tonic-gate 	 *  /dev/null since that will fail w/ our security fixes
9727c478bd9Sstevel@tonic-gate 	 */
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	if (logfile == NULL || *logfile == 0) {
9757c478bd9Sstevel@tonic-gate 		/*EMPTY*/;
9767c478bd9Sstevel@tonic-gate 	} else if (strcmp(logfile, "/dev/null") == 0) {
9777c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, "/dev/null");
9787c478bd9Sstevel@tonic-gate 		(void) close(logfd);
9797c478bd9Sstevel@tonic-gate 		logfd = -1;
9807c478bd9Sstevel@tonic-gate 	} else {
9817c478bd9Sstevel@tonic-gate 		if ((newlogfd =
9827c478bd9Sstevel@tonic-gate 		    open(logfile, O_EXCL|O_WRONLY|O_CREAT, 0644)) < 0) {
9837c478bd9Sstevel@tonic-gate 			/*
9847c478bd9Sstevel@tonic-gate 			 * File already exists... now we need to get cute
9857c478bd9Sstevel@tonic-gate 			 * since opening a file in a world-writeable directory
9867c478bd9Sstevel@tonic-gate 			 * safely is hard = it could be a hard link or a
9877c478bd9Sstevel@tonic-gate 			 * symbolic link to a system file.
9887c478bd9Sstevel@tonic-gate 			 *
9897c478bd9Sstevel@tonic-gate 			 */
9907c478bd9Sstevel@tonic-gate 			struct stat	before;
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 			if (lstat(logfile, &before) < 0) {
9937c478bd9Sstevel@tonic-gate 				logit("Cannot open new logfile \"%s\": %sn",
9947c478bd9Sstevel@tonic-gate 				    logfile, strerror(errno));
9957c478bd9Sstevel@tonic-gate 				return (-1);
9967c478bd9Sstevel@tonic-gate 			}
9977c478bd9Sstevel@tonic-gate 			if (S_ISREG(before.st_mode) &&	/* no symbolic links */
9987c478bd9Sstevel@tonic-gate 			    (before.st_nlink == 1) &&	/* no hard links */
9997c478bd9Sstevel@tonic-gate 			    (before.st_uid == 0)) {	/* owned by root */
10007c478bd9Sstevel@tonic-gate 				if ((newlogfd =
10017c478bd9Sstevel@tonic-gate 				    open(logfile,
10027c478bd9Sstevel@tonic-gate 				    O_APPEND|O_WRONLY, 0644)) < 0) {
10037c478bd9Sstevel@tonic-gate 					logit("Cannot open new logfile "
10047c478bd9Sstevel@tonic-gate 					    "\"%s\": %s\n",
10057c478bd9Sstevel@tonic-gate 					    logfile, strerror(errno));
10067c478bd9Sstevel@tonic-gate 					return (-1);
10077c478bd9Sstevel@tonic-gate 				}
10087c478bd9Sstevel@tonic-gate 			} else {
10097c478bd9Sstevel@tonic-gate 				logit("Cannot use specified logfile "
10107c478bd9Sstevel@tonic-gate 				    "\"%s\": file is/has links or isn't "
10117c478bd9Sstevel@tonic-gate 				    "owned by root\n", logfile);
10127c478bd9Sstevel@tonic-gate 				return (-1);
10137c478bd9Sstevel@tonic-gate 			}
10147c478bd9Sstevel@tonic-gate 		}
10157c478bd9Sstevel@tonic-gate 		(void) strlcpy(ptr->logfile, logfile, sizeof (ptr->logfile));
10167c478bd9Sstevel@tonic-gate 		(void) close(logfd);
10177c478bd9Sstevel@tonic-gate 		logfd = newlogfd;
10187c478bd9Sstevel@tonic-gate 		logit("Starting ldap_cachemgr, logfile %s\n", logfile);
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 	return (0);
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
10247c478bd9Sstevel@tonic-gate void
10257c478bd9Sstevel@tonic-gate logit(char *format, ...)
10267c478bd9Sstevel@tonic-gate {
10277c478bd9Sstevel@tonic-gate 	static mutex_t	loglock;
10287c478bd9Sstevel@tonic-gate 	struct timeval	tv;
10297c478bd9Sstevel@tonic-gate 	char		buffer[BUFSIZ];
10307c478bd9Sstevel@tonic-gate 	va_list		ap;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	va_start(ap, format);
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	if (logfd >= 0) {
10357c478bd9Sstevel@tonic-gate 		int	safechars;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tv, NULL);
10387c478bd9Sstevel@tonic-gate 		(void) ctime_r(&tv.tv_sec, buffer, BUFSIZ);
10397c478bd9Sstevel@tonic-gate 		(void) snprintf(buffer+19, BUFSIZE, ".%.4ld	",
10407c478bd9Sstevel@tonic-gate 		    tv.tv_usec/100);
10417c478bd9Sstevel@tonic-gate 		safechars = sizeof (buffer) - 30;
10427c478bd9Sstevel@tonic-gate 		if (vsnprintf(buffer+25, safechars, format, ap) > safechars)
10437c478bd9Sstevel@tonic-gate 			(void) strcat(buffer, "...\n");
10447c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&loglock);
10457c478bd9Sstevel@tonic-gate 		(void) write(logfd, buffer, strlen(buffer));
10467c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&loglock);
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 	va_end(ap);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate static int
10537c478bd9Sstevel@tonic-gate client_getadmin(admin_t *ptr)
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate 	dataunion		u;
10567c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
10577c478bd9Sstevel@tonic-gate 	int		ndata;
10587c478bd9Sstevel@tonic-gate 	int		adata;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETADMIN;
10617c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
10627c478bd9Sstevel@tonic-gate 	adata = sizeof (u.data);
10637c478bd9Sstevel@tonic-gate 	dptr = &u.data;
10647c478bd9Sstevel@tonic-gate 
1065e1dd0a2fSth160488 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != NS_CACHE_SUCCESS) {
10667c478bd9Sstevel@tonic-gate 		return (-1);
10677c478bd9Sstevel@tonic-gate 	}
10687c478bd9Sstevel@tonic-gate 	(void) memcpy(ptr, dptr->ldap_ret.ldap_u.buff, sizeof (*ptr));
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	return (0);
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate static int
10757ddae043Siz202018 setadmin(ldap_call_t *ptr)
10767c478bd9Sstevel@tonic-gate {
10777c478bd9Sstevel@tonic-gate 	admin_t	*new;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	new = (admin_t *)ptr->ldap_u.domainname;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	/*
10827c478bd9Sstevel@tonic-gate 	 *  global admin stuff
10837c478bd9Sstevel@tonic-gate 	 */
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	if ((cachemgr_set_lf(&current_admin, new->logfile) < 0) ||
10867c478bd9Sstevel@tonic-gate 	    cachemgr_set_dl(&current_admin, new->debug_level) < 0) {
10877c478bd9Sstevel@tonic-gate 		return (-1);
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	if (cachemgr_set_ttl(&current_admin.ldap_stat,
10917c478bd9Sstevel@tonic-gate 	    "ldap",
10927c478bd9Sstevel@tonic-gate 	    new->ldap_stat.ldap_ttl) < 0) {
10937c478bd9Sstevel@tonic-gate 		return (-1);
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	return (0);
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate static void
11017c478bd9Sstevel@tonic-gate client_killserver()
11027c478bd9Sstevel@tonic-gate {
11037c478bd9Sstevel@tonic-gate 	dataunion		u;
11047c478bd9Sstevel@tonic-gate 	ldap_data_t		*dptr;
11057c478bd9Sstevel@tonic-gate 	int			ndata;
11067c478bd9Sstevel@tonic-gate 	int			adata;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = KILLSERVER;
11097c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
11107c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
11117c478bd9Sstevel@tonic-gate 	dptr = &u.data;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	__ns_ldap_trydoorcall(&dptr, &ndata, &adata);
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate static int
11187c478bd9Sstevel@tonic-gate client_setadmin(admin_t *ptr)
11197c478bd9Sstevel@tonic-gate {
11207c478bd9Sstevel@tonic-gate 	dataunion		u;
11217c478bd9Sstevel@tonic-gate 	ldap_data_t		*dptr;
11227c478bd9Sstevel@tonic-gate 	int			ndata;
11237c478bd9Sstevel@tonic-gate 	int			adata;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = SETADMIN;
11267c478bd9Sstevel@tonic-gate 	(void) memcpy(u.data.ldap_call.ldap_u.domainname, ptr, sizeof (*ptr));
11277c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
11287c478bd9Sstevel@tonic-gate 	adata = sizeof (*ptr);
11297c478bd9Sstevel@tonic-gate 	dptr = &u.data;
11307c478bd9Sstevel@tonic-gate 
1131e1dd0a2fSth160488 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != NS_CACHE_SUCCESS) {
11327c478bd9Sstevel@tonic-gate 		return (-1);
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	return (0);
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate static int
11397c478bd9Sstevel@tonic-gate client_showstats(admin_t *ptr)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 	dataunion	u;
11427c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
11437c478bd9Sstevel@tonic-gate 	int		ndata;
11447c478bd9Sstevel@tonic-gate 	int		adata;
11457c478bd9Sstevel@tonic-gate 	char		*rbuf, *sptr, *rest;
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	/*
11487c478bd9Sstevel@tonic-gate 	 * print admin data
11497c478bd9Sstevel@tonic-gate 	 */
11507c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr configuration:\n"));
11517c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server debug level %10d\n"), ptr->debug_level);
11527c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server log file\t\"%s\"\n"), ptr->logfile);
11537c478bd9Sstevel@tonic-gate 	(void) printf(gettext("number of calls to ldapcachemgr %10d\n"),
11547c478bd9Sstevel@tonic-gate 	    ptr->ldap_stat.ldap_numbercalls);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	/*
11577c478bd9Sstevel@tonic-gate 	 * get cache data statistics
11587c478bd9Sstevel@tonic-gate 	 */
11597c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETCACHESTAT;
11607c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
11617c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
11627c478bd9Sstevel@tonic-gate 	dptr = &u.data;
11637c478bd9Sstevel@tonic-gate 
1164e1dd0a2fSth160488 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != NS_CACHE_SUCCESS) {
11657c478bd9Sstevel@tonic-gate 		(void) printf(
11667c478bd9Sstevel@tonic-gate 		    gettext("\nCache data statistics not available!\n"));
11677c478bd9Sstevel@tonic-gate 		return (0);
11687c478bd9Sstevel@tonic-gate 	}
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	/*
11717c478bd9Sstevel@tonic-gate 	 * print cache data statistics line by line
11727c478bd9Sstevel@tonic-gate 	 */
11737c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr cache data statistics:\n"));
11747c478bd9Sstevel@tonic-gate 	rbuf = dptr->ldap_ret.ldap_u.buff;
11757c478bd9Sstevel@tonic-gate 	sptr = strtok_r(rbuf, DOORLINESEP, &rest);
11767c478bd9Sstevel@tonic-gate 	for (;;) {
11777c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", sptr);
11787c478bd9Sstevel@tonic-gate 		sptr = strtok_r(NULL, DOORLINESEP, &rest);
11797c478bd9Sstevel@tonic-gate 		if (sptr == NULL)
11807c478bd9Sstevel@tonic-gate 			break;
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	return (0);
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate /*
11877c478bd9Sstevel@tonic-gate  * detach from tty
11887c478bd9Sstevel@tonic-gate  */
11897c478bd9Sstevel@tonic-gate static void
11907c478bd9Sstevel@tonic-gate detachfromtty(char *pgm)
11917c478bd9Sstevel@tonic-gate {
11927c478bd9Sstevel@tonic-gate 	int 	status;
11937c478bd9Sstevel@tonic-gate 	pid_t	pid, wret;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	(void) close(0);
11967c478bd9Sstevel@tonic-gate 	(void) close(1);
11977c478bd9Sstevel@tonic-gate 	/*
11987c478bd9Sstevel@tonic-gate 	 * Block the SIGUSR1 signal
11997c478bd9Sstevel@tonic-gate 	 * just in case that the child
12007c478bd9Sstevel@tonic-gate 	 * process may run faster than
12017c478bd9Sstevel@tonic-gate 	 * the parent process and
12027c478bd9Sstevel@tonic-gate 	 * send this signal before
12037c478bd9Sstevel@tonic-gate 	 * the signal handler is ready
12047c478bd9Sstevel@tonic-gate 	 * in the parent process.
12057c478bd9Sstevel@tonic-gate 	 * This error will cause the parent
12067c478bd9Sstevel@tonic-gate 	 * to exit with the User Signal 1
12077c478bd9Sstevel@tonic-gate 	 * exit code (144).
12087c478bd9Sstevel@tonic-gate 	 */
12097c478bd9Sstevel@tonic-gate 	(void) sighold(SIGUSR1);
12107c478bd9Sstevel@tonic-gate 	pid = fork1();
12117c478bd9Sstevel@tonic-gate 	switch (pid) {
12127c478bd9Sstevel@tonic-gate 		case (pid_t)-1:
12137c478bd9Sstevel@tonic-gate 			logit("detachfromtty(): fork1() call failed\n");
12147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12157c478bd9Sstevel@tonic-gate 			    gettext("%s: fork1() call failed.\n"),
12167c478bd9Sstevel@tonic-gate 			    pgm);
12177c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
12187c478bd9Sstevel@tonic-gate 			    gettext("ldap_cachemgr: fork1() call failed."));
12197c478bd9Sstevel@tonic-gate 			exit(1);
12207c478bd9Sstevel@tonic-gate 			break;
12217c478bd9Sstevel@tonic-gate 		case 0:
12227c478bd9Sstevel@tonic-gate 			/*
12237c478bd9Sstevel@tonic-gate 			 * child process does not
12247c478bd9Sstevel@tonic-gate 			 * need to worry about
12257c478bd9Sstevel@tonic-gate 			 * the SIGUSR1 signal
12267c478bd9Sstevel@tonic-gate 			 */
12277c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
12287c478bd9Sstevel@tonic-gate 			(void) close(2);
12297c478bd9Sstevel@tonic-gate 			break;
12307c478bd9Sstevel@tonic-gate 		default:
12317c478bd9Sstevel@tonic-gate 			/*
12327c478bd9Sstevel@tonic-gate 			 * Wait forever until the child process
12337c478bd9Sstevel@tonic-gate 			 * has exited, or has signalled that at
12347c478bd9Sstevel@tonic-gate 			 * least one server in the server list
12357c478bd9Sstevel@tonic-gate 			 * is up.
12367c478bd9Sstevel@tonic-gate 			 */
12377c478bd9Sstevel@tonic-gate 			if (signal(SIGUSR1, sig_ok_to_exit) == SIG_ERR) {
12387c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12397c478bd9Sstevel@tonic-gate 				    "can't set up signal handler to "
12407c478bd9Sstevel@tonic-gate 				    " catch SIGUSR1.\n");
12417c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12427c478bd9Sstevel@tonic-gate 				    gettext("%s: signal() call failed.\n"),
12437c478bd9Sstevel@tonic-gate 				    pgm);
12447c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, gettext("ldap_cachemgr: "
12457c478bd9Sstevel@tonic-gate 				    "can't set up signal handler to "
12467c478bd9Sstevel@tonic-gate 				    " catch SIGUSR1."));
12477c478bd9Sstevel@tonic-gate 				exit(1);
12487c478bd9Sstevel@tonic-gate 			}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 			/*
12517c478bd9Sstevel@tonic-gate 			 * now unblock the SIGUSR1 signal
12527c478bd9Sstevel@tonic-gate 			 * to handle the pending or
12537c478bd9Sstevel@tonic-gate 			 * soon to arrive SIGUSR1 signal
12547c478bd9Sstevel@tonic-gate 			 */
12557c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
12567c478bd9Sstevel@tonic-gate 			wret = waitpid(pid, &status, 0);
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 			if (wret == -1) {
12597c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12607c478bd9Sstevel@tonic-gate 				    "waitpid() call failed\n");
12617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12627c478bd9Sstevel@tonic-gate 				    gettext("%s: waitpid() call failed.\n"),
12637c478bd9Sstevel@tonic-gate 				    pgm);
12647c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
12657c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: waitpid() "
12667c478bd9Sstevel@tonic-gate 				    "call failed."));
12677c478bd9Sstevel@tonic-gate 				exit(1);
12687c478bd9Sstevel@tonic-gate 			}
12697c478bd9Sstevel@tonic-gate 			if (wret != pid) {
12707c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12717c478bd9Sstevel@tonic-gate 				    "waitpid() returned %ld when "
12727c478bd9Sstevel@tonic-gate 				    "child pid was %ld\n",
12737c478bd9Sstevel@tonic-gate 				    wret, pid);
12747c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12757c478bd9Sstevel@tonic-gate 				    gettext(
12767c478bd9Sstevel@tonic-gate 				    "%s: waitpid() returned %ld when "
12777c478bd9Sstevel@tonic-gate 				    "child pid was %ld.\n"),
12787c478bd9Sstevel@tonic-gate 				    pgm, wret, pid);
12797c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
12807c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: waitpid() "
12817c478bd9Sstevel@tonic-gate 				    "returned different "
12827c478bd9Sstevel@tonic-gate 				    "child pid."));
12837c478bd9Sstevel@tonic-gate 				exit(1);
12847c478bd9Sstevel@tonic-gate 			}
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 			/* evaluate return status */
12877c478bd9Sstevel@tonic-gate 			if (WIFEXITED(status)) {
12887c478bd9Sstevel@tonic-gate 				if (WEXITSTATUS(status) == 0) {
12897c478bd9Sstevel@tonic-gate 					exit(0);
12907c478bd9Sstevel@tonic-gate 				}
12917c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12927c478bd9Sstevel@tonic-gate 				    "child failed (rc = %d).\n",
12937c478bd9Sstevel@tonic-gate 				    WEXITSTATUS(status));
12947c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12957c478bd9Sstevel@tonic-gate 				    gettext("%s: failed. Please see "
12967c478bd9Sstevel@tonic-gate 				    "syslog for details.\n"),
12977c478bd9Sstevel@tonic-gate 				    pgm);
12987c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
12997c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: failed "
13007c478bd9Sstevel@tonic-gate 				    "(rc = %d)."),
13017c478bd9Sstevel@tonic-gate 				    WEXITSTATUS(status));
13027c478bd9Sstevel@tonic-gate 			} else if (WIFSIGNALED(status)) {
13037c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
13047c478bd9Sstevel@tonic-gate 				    "child terminated by signal %d.\n",
13057c478bd9Sstevel@tonic-gate 				    WTERMSIG(status));
13067c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
13077c478bd9Sstevel@tonic-gate 				gettext("%s: terminated by signal %d.\n"),
13087c478bd9Sstevel@tonic-gate 				    pgm, WTERMSIG(status));
13097c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
13107c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: terminated by "
13117c478bd9Sstevel@tonic-gate 				    "signal %d.\n"),
13127c478bd9Sstevel@tonic-gate 				    WTERMSIG(status));
13137c478bd9Sstevel@tonic-gate 			} else if (WCOREDUMP(status)) {
13147c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): child core dumped.\n"),
13157c478bd9Sstevel@tonic-gate 				    (void) fprintf(stderr,
13167c478bd9Sstevel@tonic-gate 				    gettext("%s: core dumped.\n"),
13177c478bd9Sstevel@tonic-gate 				    pgm);
13187c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
13197c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: "
13207c478bd9Sstevel@tonic-gate 				    "core dumped.\n"));
13217c478bd9Sstevel@tonic-gate 			}
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 			exit(1);
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 	(void) setsid();
13267c478bd9Sstevel@tonic-gate 	if (open("/dev/null", O_RDWR, 0) != -1) {
13277c478bd9Sstevel@tonic-gate 		(void) dup(0);
13287c478bd9Sstevel@tonic-gate 		(void) dup(0);
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate }
13318142c2b2Schinlong 
13328142c2b2Schinlong /*
13338142c2b2Schinlong  * Check if the door client's euid is 0
13348142c2b2Schinlong  *
13358142c2b2Schinlong  * We could check for some privilege or re-design the interfaces that
13368142c2b2Schinlong  * lead to is_root() being called so that we rely on SMF and RBAC, but
13378142c2b2Schinlong  * we need this check only for dealing with undocumented-but-possibly-
13388142c2b2Schinlong  * used interfaces.  Anything beyond checking for euid == 0 here would
13398142c2b2Schinlong  * be overkill considering that those are undocumented interfaces.
13408142c2b2Schinlong  *
13418142c2b2Schinlong  * If free_uc is 0, the caller is responsible for freeing *ucp.
13428142c2b2Schinlong  *
13438142c2b2Schinlong  * return - 0 euid != 0
13448142c2b2Schinlong  *          1 euid == 0
13458142c2b2Schinlong  */
13468142c2b2Schinlong static int
13478142c2b2Schinlong is_root(int free_uc, char *dc_str, ucred_t **ucp)
13488142c2b2Schinlong {
13498142c2b2Schinlong 	int	rc;
13508142c2b2Schinlong 
13518142c2b2Schinlong 	if (door_ucred(ucp) != 0) {
13528142c2b2Schinlong 		rc = errno;
13538142c2b2Schinlong 		logit("door_ucred() call failed %s\n", strerror(rc));
13548142c2b2Schinlong 		syslog(LOG_ERR, gettext("ldap_cachemgr: door_ucred() call %s "
13558142c2b2Schinlong 		    "failed %s"), strerror(rc));
13568142c2b2Schinlong 		return (0);
13578142c2b2Schinlong 	}
13588142c2b2Schinlong 
13598142c2b2Schinlong 
13608142c2b2Schinlong 	if (ucred_geteuid(*ucp) != 0) {
13618142c2b2Schinlong 
13628142c2b2Schinlong 		if (current_admin.debug_level >= DBG_CANT_FIND)
13638142c2b2Schinlong 			logit("%s call failed(cred): caller pid %ld, uid %u, "
1364*dd1104fbSMichen Chang 			    "euid %u (if uid or euid is %u, it may be "
1365*dd1104fbSMichen Chang 			    "unavailable)\n", dc_str, ucred_getpid(*ucp),
1366*dd1104fbSMichen Chang 			    ucred_getruid(*ucp), ucred_geteuid(*ucp), -1);
13678142c2b2Schinlong 
13688142c2b2Schinlong 		rc = 0;
13698142c2b2Schinlong 	} else {
13708142c2b2Schinlong 
13718142c2b2Schinlong 		if (current_admin.debug_level >= DBG_ALL)
1372*dd1104fbSMichen Chang 			logit("received %s call from pid %ld, uid %u, euid %u "
1373*dd1104fbSMichen Chang 			    "(if uid or euid is %u, it may be unavailable)\n",
1374*dd1104fbSMichen Chang 			    dc_str, ucred_getpid(*ucp), ucred_getruid(*ucp),
1375*dd1104fbSMichen Chang 			    ucred_geteuid(*ucp), -1);
13768142c2b2Schinlong 		rc = 1;
13778142c2b2Schinlong 	}
13788142c2b2Schinlong 
13798142c2b2Schinlong 	if (free_uc)
13808142c2b2Schinlong 		ucred_free(*ucp);
13818142c2b2Schinlong 
13828142c2b2Schinlong 	return (rc);
13838142c2b2Schinlong }
13848142c2b2Schinlong 
13858142c2b2Schinlong /*
13868142c2b2Schinlong  * Check if pid is nscd
13878142c2b2Schinlong  *
13888142c2b2Schinlong  * Input: pid - process id of the door client that calls ldap_cachemgr
13898142c2b2Schinlong  *
13908142c2b2Schinlong  * Return: 0 - No
13918142c2b2Schinlong  *         1 - Yes
13928142c2b2Schinlong  */
13938142c2b2Schinlong 
1394e1dd0a2fSth160488 int
1395e1dd0a2fSth160488 is_called_from_nscd(pid_t pid)
13968142c2b2Schinlong 
13978142c2b2Schinlong {
13988142c2b2Schinlong 	static mutex_t	_door_lock = DEFAULTMUTEX;
13998142c2b2Schinlong 	static	int	doorfd = -1;
14008142c2b2Schinlong 	int		match;
14018142c2b2Schinlong 	door_info_t 	my_door;
14028142c2b2Schinlong 
14038142c2b2Schinlong 	/*
14048142c2b2Schinlong 	 * the first time in we try and open and validate the door.
14058142c2b2Schinlong 	 * the validations are that the door must have been
14068142c2b2Schinlong 	 * created with the door cookie and
14078142c2b2Schinlong 	 * that the file attached to the door is owned by root
14088142c2b2Schinlong 	 * and readonly by user, group and other.  If any of these
14098142c2b2Schinlong 	 * validations fail we refuse to use the door.
14108142c2b2Schinlong 	 */
14118142c2b2Schinlong 
14128142c2b2Schinlong 	(void) mutex_lock(&_door_lock);
14138142c2b2Schinlong 
14148142c2b2Schinlong try_again:
14158142c2b2Schinlong 
14168142c2b2Schinlong 	if (doorfd == -1) {
14178142c2b2Schinlong 
14188142c2b2Schinlong 		if ((doorfd = open(NAME_SERVICE_DOOR, O_RDONLY, 0))
14198142c2b2Schinlong 		    == -1) {
14208142c2b2Schinlong 			(void) mutex_unlock(&_door_lock);
14218142c2b2Schinlong 			return (0);
14228142c2b2Schinlong 		}
14238142c2b2Schinlong 
14248142c2b2Schinlong 		if (door_info(doorfd, &my_door) == -1 ||
14258142c2b2Schinlong 		    (my_door.di_attributes & DOOR_REVOKED) ||
14268142c2b2Schinlong 		    my_door.di_data != (uintptr_t)NAME_SERVICE_DOOR_COOKIE) {
14278142c2b2Schinlong 			/*
14288142c2b2Schinlong 			 * we should close doorfd because we just opened it
14298142c2b2Schinlong 			 */
14308142c2b2Schinlong 			(void) close(doorfd);
14318142c2b2Schinlong 			doorfd = -1;
14328142c2b2Schinlong 			(void) mutex_unlock(&_door_lock);
14338142c2b2Schinlong 			return (0);
14348142c2b2Schinlong 		}
14358142c2b2Schinlong 	} else {
14368142c2b2Schinlong 		/*
14378142c2b2Schinlong 		 * doorfd is cached. Double check just in case
14388142c2b2Schinlong 		 * the door server is restarted or is down.
14398142c2b2Schinlong 		 */
14408142c2b2Schinlong 		if (door_info(doorfd, &my_door) == -1 ||
14418142c2b2Schinlong 		    my_door.di_data != (uintptr_t)NAME_SERVICE_DOOR_COOKIE) {
14428142c2b2Schinlong 			/*
14438142c2b2Schinlong 			 * don't close it -
14448142c2b2Schinlong 			 * someone else has clobbered fd
14458142c2b2Schinlong 			 */
14468142c2b2Schinlong 			doorfd = -1;
14478142c2b2Schinlong 			goto try_again;
14488142c2b2Schinlong 		}
14498142c2b2Schinlong 
14508142c2b2Schinlong 		if (my_door.di_attributes & DOOR_REVOKED) {
14518142c2b2Schinlong 			(void) close(doorfd);
14528142c2b2Schinlong 			doorfd = -1;	/* try and restart connection */
14538142c2b2Schinlong 			goto try_again;
14548142c2b2Schinlong 		}
14558142c2b2Schinlong 	}
14568142c2b2Schinlong 
14578142c2b2Schinlong 	/*
14588142c2b2Schinlong 	 * door descriptor exists and is valid
14598142c2b2Schinlong 	 */
14608142c2b2Schinlong 	if (pid == my_door.di_target)
14618142c2b2Schinlong 		match = 1;
14628142c2b2Schinlong 	else
14638142c2b2Schinlong 		match = 0;
14648142c2b2Schinlong 
14658142c2b2Schinlong 	(void) mutex_unlock(&_door_lock);
14668142c2b2Schinlong 
14678142c2b2Schinlong 	return (match);
14688142c2b2Schinlong 
14698142c2b2Schinlong }
1470*dd1104fbSMichen Chang 
1471*dd1104fbSMichen Chang /*
1472*dd1104fbSMichen Chang  * new_attr(name, value)
1473*dd1104fbSMichen Chang  *
1474*dd1104fbSMichen Chang  * create a new LDAP attribute to be sent to the server
1475*dd1104fbSMichen Chang  */
1476*dd1104fbSMichen Chang static ns_ldap_attr_t *
1477*dd1104fbSMichen Chang new_attr(char *name, char *value)
1478*dd1104fbSMichen Chang {
1479*dd1104fbSMichen Chang 	ns_ldap_attr_t *tmp;
1480*dd1104fbSMichen Chang 
1481*dd1104fbSMichen Chang 	tmp = malloc(sizeof (*tmp));
1482*dd1104fbSMichen Chang 	if (tmp != NULL) {
1483*dd1104fbSMichen Chang 		tmp->attrname = name;
1484*dd1104fbSMichen Chang 		tmp->attrvalue = (char **)calloc(2, sizeof (char *));
1485*dd1104fbSMichen Chang 		if (tmp->attrvalue == NULL) {
1486*dd1104fbSMichen Chang 			free(tmp);
1487*dd1104fbSMichen Chang 			return (NULL);
1488*dd1104fbSMichen Chang 		}
1489*dd1104fbSMichen Chang 		tmp->attrvalue[0] = value;
1490*dd1104fbSMichen Chang 		tmp->value_count = 1;
1491*dd1104fbSMichen Chang 	}
1492*dd1104fbSMichen Chang 
1493*dd1104fbSMichen Chang 	return (tmp);
1494*dd1104fbSMichen Chang }
1495*dd1104fbSMichen Chang 
1496*dd1104fbSMichen Chang /*
1497*dd1104fbSMichen Chang  * Convert the flatten ldap attributes in a ns_ldap_attr_t back
1498*dd1104fbSMichen Chang  * to an ns_ldap_attr_t array.
1499*dd1104fbSMichen Chang  *
1500*dd1104fbSMichen Chang  * strlist->ldap_offsets[] contains offsets to strings:
1501*dd1104fbSMichen Chang  * "dn", <dn value>, <attr 1>, <attrval 1>, ... <attr n>, <attrval n>
1502*dd1104fbSMichen Chang  * where n is (strlist->ldap_count/2 -1).
1503*dd1104fbSMichen Chang  * The output ns_ldap_attr_t array has a size of (strlist->ldap_count/2)
1504*dd1104fbSMichen Chang  * the first (strlist->ldap_count/2 -1) contains all the attribute data,
1505*dd1104fbSMichen Chang  * the last one is a NULL pointer. DN will be extracted out and pointed
1506*dd1104fbSMichen Chang  * to by *dn.
1507*dd1104fbSMichen Chang  */
1508*dd1104fbSMichen Chang static ns_ldap_attr_t **
1509*dd1104fbSMichen Chang str2attrs(ldap_strlist_t *strlist, char **dn)
1510*dd1104fbSMichen Chang {
1511*dd1104fbSMichen Chang 	int		c;
1512*dd1104fbSMichen Chang 	int		i;
1513*dd1104fbSMichen Chang 	int		j;
1514*dd1104fbSMichen Chang 	ns_ldap_attr_t	**ret;
1515*dd1104fbSMichen Chang 
1516*dd1104fbSMichen Chang 	c = strlist->ldap_count;
1517*dd1104fbSMichen Chang 	ret = calloc(c/2, sizeof (ns_ldap_attr_t *));
1518*dd1104fbSMichen Chang 	if (ret == NULL)
1519*dd1104fbSMichen Chang 		return (NULL);
1520*dd1104fbSMichen Chang 	*dn = (char *)strlist + strlist->ldap_offsets[1];
1521*dd1104fbSMichen Chang 
1522*dd1104fbSMichen Chang 	/*
1523*dd1104fbSMichen Chang 	 * skip the first 'dn'/<dn value> pair, for all other attr type/value
1524*dd1104fbSMichen Chang 	 * pairs, get pointers to the attr type (offset [i]) and attr value
1525*dd1104fbSMichen Chang 	 * (offset [i+1]) and put in ns_ldap_attr_t at ret[j]
1526*dd1104fbSMichen Chang 	 */
1527*dd1104fbSMichen Chang 	for (i = 2, j = 0; i < c; i = i + 2, j++) {
1528*dd1104fbSMichen Chang 		ret[j] = new_attr((char *)strlist + strlist->ldap_offsets[i],
1529*dd1104fbSMichen Chang 		    (char *)strlist + strlist->ldap_offsets[i + 1]);
1530*dd1104fbSMichen Chang 	}
1531*dd1104fbSMichen Chang 	return (ret);
1532*dd1104fbSMichen Chang }
1533*dd1104fbSMichen Chang 
1534*dd1104fbSMichen Chang static int
1535*dd1104fbSMichen Chang get_admin_dn(ns_cred_t *credp, int *status, ns_ldap_error_t **errorp)
1536*dd1104fbSMichen Chang {
1537*dd1104fbSMichen Chang 	void	**paramVal = NULL;
1538*dd1104fbSMichen Chang 	int	rc;
1539*dd1104fbSMichen Chang 
1540*dd1104fbSMichen Chang 	/* get bind DN for shadow update */
1541*dd1104fbSMichen Chang 	rc = __ns_ldap_getParam(NS_LDAP_ADMIN_BINDDN_P,
1542*dd1104fbSMichen Chang 	    &paramVal, errorp);
1543*dd1104fbSMichen Chang 	if (rc != NS_LDAP_SUCCESS)
1544*dd1104fbSMichen Chang 		return (rc);
1545*dd1104fbSMichen Chang 
1546*dd1104fbSMichen Chang 	if (paramVal == NULL || *paramVal == NULL) {
1547*dd1104fbSMichen Chang 		rc = NS_LDAP_CONFIG;
1548*dd1104fbSMichen Chang 		*status = NS_CONFIG_NOTALLOW;
1549*dd1104fbSMichen Chang 		if (paramVal != NULL)
1550*dd1104fbSMichen Chang 			(void) __ns_ldap_freeParam(&paramVal);
1551*dd1104fbSMichen Chang 		return (rc);
1552*dd1104fbSMichen Chang 	}
1553*dd1104fbSMichen Chang 	credp->cred.unix_cred.userID = strdup((char *)*paramVal);
1554*dd1104fbSMichen Chang 	(void) __ns_ldap_freeParam(&paramVal);
1555*dd1104fbSMichen Chang 	if (credp->cred.unix_cred.userID == NULL)
1556*dd1104fbSMichen Chang 		return (NS_LDAP_MEMORY);
1557*dd1104fbSMichen Chang 
1558*dd1104fbSMichen Chang 	return (NS_LDAP_SUCCESS);
1559*dd1104fbSMichen Chang }
1560*dd1104fbSMichen Chang 
1561*dd1104fbSMichen Chang /*
1562*dd1104fbSMichen Chang  * admin_modify() does a privileged modify within the ldap_cachemgr daemon
1563*dd1104fbSMichen Chang  * process using the admin DN/password configured with parameters
1564*dd1104fbSMichen Chang  * NS_LDAP_ADMIN_BINDDN and NS_LDAP_ADMIN_BINDPASSWD. It will only
1565*dd1104fbSMichen Chang  * be done if NS_LDAP_ENABLE_SHADOW_UPDATE is set to TRUE.
1566*dd1104fbSMichen Chang  *
1567*dd1104fbSMichen Chang  * The input ldap_call_t (*in) contains LDAP shadowAccount attributes to
1568*dd1104fbSMichen Chang  * be modified. The data is a flatten ns_ldap_attr_t arrary stored in
1569*dd1104fbSMichen Chang  * the strlist element of the input ldap_call_t.
1570*dd1104fbSMichen Chang  * The output will be in LineBuf (*config_info), an ldap_admin_mod_result_t
1571*dd1104fbSMichen Chang  * structure that contains error code, error status, and error message.
1572*dd1104fbSMichen Chang  */
1573*dd1104fbSMichen Chang static void
1574*dd1104fbSMichen Chang admin_modify(LineBuf *config_info, ldap_call_t *in)
1575*dd1104fbSMichen Chang {
1576*dd1104fbSMichen Chang 	int		rc = NS_LDAP_SUCCESS;
1577*dd1104fbSMichen Chang 	int		authstried = 0;
1578*dd1104fbSMichen Chang 	int		shadow_enabled = 0;
1579*dd1104fbSMichen Chang 	char		*dn = NULL;
1580*dd1104fbSMichen Chang 	char		**certpath = NULL;
1581*dd1104fbSMichen Chang 	char		**enable_shadow = NULL;
1582*dd1104fbSMichen Chang 	ns_auth_t	**app;
1583*dd1104fbSMichen Chang 	ns_auth_t	**authpp = NULL;
1584*dd1104fbSMichen Chang 	ns_auth_t	*authp = NULL;
1585*dd1104fbSMichen Chang 	ns_cred_t	*credp = NULL;
1586*dd1104fbSMichen Chang 	char		buffer[MAXERROR];
1587*dd1104fbSMichen Chang 	const int	rlen = offsetof(ldap_admin_mod_result_t, msg);
1588*dd1104fbSMichen Chang 	int		mlen = 0;
1589*dd1104fbSMichen Chang 	const int	msgmax = MAXERROR - rlen;
1590*dd1104fbSMichen Chang 	int		status = 0;
1591*dd1104fbSMichen Chang 	ucred_t		*uc = NULL;
1592*dd1104fbSMichen Chang 	ldap_strlist_t	*strlist;
1593*dd1104fbSMichen Chang 	ns_ldap_attr_t	**attrs = NULL;
1594*dd1104fbSMichen Chang 	ns_ldap_error_t *error = NULL;
1595*dd1104fbSMichen Chang 	ldap_admin_mod_result_t *result;
1596*dd1104fbSMichen Chang 
1597*dd1104fbSMichen Chang 	(void) memset((char *)config_info, 0, sizeof (LineBuf));
1598*dd1104fbSMichen Chang 
1599*dd1104fbSMichen Chang 	/* only root or an ALL privs user can do admin modify */
1600*dd1104fbSMichen Chang 	if (is_root_or_all_privs("ADMINMODIFY", &uc) == 0) {
1601*dd1104fbSMichen Chang 		mlen = snprintf(buffer, msgmax, "%s",
1602*dd1104fbSMichen Chang 		    gettext("shadow update by a non-root and no ALL privilege "
1603*dd1104fbSMichen Chang 		    "user not allowed"));
1604*dd1104fbSMichen Chang 		rc = NS_LDAP_CONFIG;
1605*dd1104fbSMichen Chang 		goto out;
1606*dd1104fbSMichen Chang 	}
1607*dd1104fbSMichen Chang 
1608*dd1104fbSMichen Chang 	/* check to see if shadow update is enabled */
1609*dd1104fbSMichen Chang 	rc = __ns_ldap_getParam(NS_LDAP_ENABLE_SHADOW_UPDATE_P,
1610*dd1104fbSMichen Chang 	    (void ***)&enable_shadow, &error);
1611*dd1104fbSMichen Chang 	if (rc != NS_LDAP_SUCCESS)
1612*dd1104fbSMichen Chang 		goto out;
1613*dd1104fbSMichen Chang 	if (enable_shadow != NULL && *enable_shadow != NULL) {
1614*dd1104fbSMichen Chang 		shadow_enabled = (*(int *)enable_shadow[0] ==
1615*dd1104fbSMichen Chang 		    NS_LDAP_ENABLE_SHADOW_UPDATE_TRUE);
1616*dd1104fbSMichen Chang 	}
1617*dd1104fbSMichen Chang 	if (enable_shadow != NULL)
1618*dd1104fbSMichen Chang 		(void) __ns_ldap_freeParam((void ***)&enable_shadow);
1619*dd1104fbSMichen Chang 	if (shadow_enabled == 0) {
1620*dd1104fbSMichen Chang 		rc = NS_LDAP_CONFIG;
1621*dd1104fbSMichen Chang 		status = NS_CONFIG_NOTALLOW;
1622*dd1104fbSMichen Chang 		mlen = snprintf(buffer, msgmax, "%s",
1623*dd1104fbSMichen Chang 		    gettext("shadow update not enabled"));
1624*dd1104fbSMichen Chang 		goto out;
1625*dd1104fbSMichen Chang 	}
1626*dd1104fbSMichen Chang 
1627*dd1104fbSMichen Chang 	/* convert attributes in string buffer into an ldap attribute array */
1628*dd1104fbSMichen Chang 	strlist = &in->ldap_u.strlist;
1629*dd1104fbSMichen Chang 	attrs = str2attrs(strlist, &dn);
1630*dd1104fbSMichen Chang 	if (attrs == NULL || *attrs == NULL || dn == NULL || *dn == '\0') {
1631*dd1104fbSMichen Chang 		rc = NS_LDAP_INVALID_PARAM;
1632*dd1104fbSMichen Chang 		goto out;
1633*dd1104fbSMichen Chang 	}
1634*dd1104fbSMichen Chang 
1635*dd1104fbSMichen Chang 	if ((credp = (ns_cred_t *)calloc(1, sizeof (ns_cred_t))) == NULL) {
1636*dd1104fbSMichen Chang 		rc = NS_LDAP_MEMORY;
1637*dd1104fbSMichen Chang 		goto out;
1638*dd1104fbSMichen Chang 	}
1639*dd1104fbSMichen Chang 
1640*dd1104fbSMichen Chang 	/* get host certificate path, if one is configured */
1641*dd1104fbSMichen Chang 	rc = __ns_ldap_getParam(NS_LDAP_HOST_CERTPATH_P,
1642*dd1104fbSMichen Chang 	    (void ***)&certpath, &error);
1643*dd1104fbSMichen Chang 	if (rc != NS_LDAP_SUCCESS)
1644*dd1104fbSMichen Chang 		goto out;
1645*dd1104fbSMichen Chang 	if (certpath != NULL && *certpath != NULL) {
1646*dd1104fbSMichen Chang 		credp->hostcertpath = strdup(*certpath);
1647*dd1104fbSMichen Chang 		if (credp->hostcertpath == NULL)
1648*dd1104fbSMichen Chang 			rc = NS_LDAP_MEMORY;
1649*dd1104fbSMichen Chang 	}
1650*dd1104fbSMichen Chang 	if (certpath != NULL)
1651*dd1104fbSMichen Chang 		(void) __ns_ldap_freeParam((void ***)&certpath);
1652*dd1104fbSMichen Chang 	if (rc != NS_LDAP_SUCCESS)
1653*dd1104fbSMichen Chang 		goto out;
1654*dd1104fbSMichen Chang 
1655*dd1104fbSMichen Chang 	/* Load the service specific authentication method */
1656*dd1104fbSMichen Chang 	rc = __ns_ldap_getServiceAuthMethods("passwd-cmd", &authpp,
1657*dd1104fbSMichen Chang 	    &error);
1658*dd1104fbSMichen Chang 	if (rc != NS_LDAP_SUCCESS) {
1659*dd1104fbSMichen Chang 		if (credp->hostcertpath != NULL)
1660*dd1104fbSMichen Chang 			free(credp->hostcertpath);
1661*dd1104fbSMichen Chang 		goto out;
1662*dd1104fbSMichen Chang 	}
1663*dd1104fbSMichen Chang 
1664*dd1104fbSMichen Chang 	/*
1665*dd1104fbSMichen Chang 	 * if authpp is null, there is no serviceAuthenticationMethod
1666*dd1104fbSMichen Chang 	 * try default authenticationMethod
1667*dd1104fbSMichen Chang 	 */
1668*dd1104fbSMichen Chang 	if (authpp == NULL) {
1669*dd1104fbSMichen Chang 		rc = __ns_ldap_getParam(NS_LDAP_AUTH_P, (void ***)&authpp,
1670*dd1104fbSMichen Chang 		    &error);
1671*dd1104fbSMichen Chang 		if (rc != NS_LDAP_SUCCESS)
1672*dd1104fbSMichen Chang 			goto out;
1673*dd1104fbSMichen Chang 	}
1674*dd1104fbSMichen Chang 
1675*dd1104fbSMichen Chang 	/*
1676*dd1104fbSMichen Chang 	 * if authpp is still null, then can not authenticate, syslog
1677*dd1104fbSMichen Chang 	 * error message and return error
1678*dd1104fbSMichen Chang 	 */
1679*dd1104fbSMichen Chang 	if (authpp == NULL) {
1680*dd1104fbSMichen Chang 		rc = NS_LDAP_CONFIG;
1681*dd1104fbSMichen Chang 		mlen = snprintf(buffer, msgmax, "%s",
1682*dd1104fbSMichen Chang 		    gettext("No legal LDAP authentication method configured"));
1683*dd1104fbSMichen Chang 		goto out;
1684*dd1104fbSMichen Chang 	}
1685*dd1104fbSMichen Chang 
1686*dd1104fbSMichen Chang 	/*
1687*dd1104fbSMichen Chang 	 * Walk the array and try all authentication methods in order except
1688*dd1104fbSMichen Chang 	 * for "none".
1689*dd1104fbSMichen Chang 	 */
1690*dd1104fbSMichen Chang 	for (app = authpp; *app; app++) {
1691*dd1104fbSMichen Chang 		authp = *app;
1692*dd1104fbSMichen Chang 		if (authp->type == NS_LDAP_AUTH_NONE)
1693*dd1104fbSMichen Chang 			continue;
1694*dd1104fbSMichen Chang 		authstried++;
1695*dd1104fbSMichen Chang 		credp->auth.type = authp->type;
1696*dd1104fbSMichen Chang 		credp->auth.tlstype = authp->tlstype;
1697*dd1104fbSMichen Chang 		credp->auth.saslmech = authp->saslmech;
1698*dd1104fbSMichen Chang 		credp->auth.saslopt = authp->saslopt;
1699*dd1104fbSMichen Chang 
1700*dd1104fbSMichen Chang 		/*
1701*dd1104fbSMichen Chang 		 * For GSSAPI, host credential will be used. No admin
1702*dd1104fbSMichen Chang 		 * DN is needed. For other authentication methods,
1703*dd1104fbSMichen Chang 		 * we need to set admin.
1704*dd1104fbSMichen Chang 		 */
1705*dd1104fbSMichen Chang 		if (credp->auth.saslmech != NS_LDAP_SASL_GSSAPI) {
1706*dd1104fbSMichen Chang 			if ((rc = get_admin_dn(credp, &status,
1707*dd1104fbSMichen Chang 			    &error)) != NS_LDAP_SUCCESS) {
1708*dd1104fbSMichen Chang 				if (error != NULL)
1709*dd1104fbSMichen Chang 					goto out;
1710*dd1104fbSMichen Chang 				if (status == NS_CONFIG_NOTALLOW) {
1711*dd1104fbSMichen Chang 					mlen = snprintf(buffer, msgmax, "%s",
1712*dd1104fbSMichen Chang 					    gettext("Admin bind DN not "
1713*dd1104fbSMichen Chang 					    "configured"));
1714*dd1104fbSMichen Chang 					goto out;
1715*dd1104fbSMichen Chang 				}
1716*dd1104fbSMichen Chang 			}
1717*dd1104fbSMichen Chang 		}
1718*dd1104fbSMichen Chang 
1719*dd1104fbSMichen Chang 		rc = __ns_ldap_repAttr(NS_ADMIN_SHADOW_UPDATE, dn,
1720*dd1104fbSMichen Chang 		    (const ns_ldap_attr_t * const *)attrs,
1721*dd1104fbSMichen Chang 		    credp, 0, &error);
1722*dd1104fbSMichen Chang 		if (rc == NS_LDAP_SUCCESS)
1723*dd1104fbSMichen Chang 			goto out;
1724*dd1104fbSMichen Chang 
1725*dd1104fbSMichen Chang 		/*
1726*dd1104fbSMichen Chang 		 * Other errors might need to be added to this list, for
1727*dd1104fbSMichen Chang 		 * the current supported mechanisms this is sufficient.
1728*dd1104fbSMichen Chang 		 */
1729*dd1104fbSMichen Chang 		if (rc == NS_LDAP_INTERNAL &&
1730*dd1104fbSMichen Chang 		    error->pwd_mgmt.status == NS_PASSWD_GOOD &&
1731*dd1104fbSMichen Chang 		    (error->status == LDAP_INAPPROPRIATE_AUTH ||
1732*dd1104fbSMichen Chang 		    error->status == LDAP_INVALID_CREDENTIALS))
1733*dd1104fbSMichen Chang 			goto out;
1734*dd1104fbSMichen Chang 
1735*dd1104fbSMichen Chang 		/*
1736*dd1104fbSMichen Chang 		 * If there is error related to password policy,
1737*dd1104fbSMichen Chang 		 * return it to caller.
1738*dd1104fbSMichen Chang 		 */
1739*dd1104fbSMichen Chang 		if (rc == NS_LDAP_INTERNAL &&
1740*dd1104fbSMichen Chang 		    error->pwd_mgmt.status != NS_PASSWD_GOOD) {
1741*dd1104fbSMichen Chang 			rc = NS_LDAP_CONFIG;
1742*dd1104fbSMichen Chang 			status = NS_CONFIG_NOTALLOW;
1743*dd1104fbSMichen Chang 			(void) __ns_ldap_freeError(&error);
1744*dd1104fbSMichen Chang 			mlen = snprintf(buffer, msgmax, "%s",
1745*dd1104fbSMichen Chang 			    gettext("update failed due to "
1746*dd1104fbSMichen Chang 			    "password policy on server (%d)"),
1747*dd1104fbSMichen Chang 			    error->pwd_mgmt.status);
1748*dd1104fbSMichen Chang 			goto out;
1749*dd1104fbSMichen Chang 		}
1750*dd1104fbSMichen Chang 
1751*dd1104fbSMichen Chang 		/* we don't really care about the error, just clean it up */
1752*dd1104fbSMichen Chang 		if (error)
1753*dd1104fbSMichen Chang 			(void) __ns_ldap_freeError(&error);
1754*dd1104fbSMichen Chang 	}
1755*dd1104fbSMichen Chang 	if (authstried == 0) {
1756*dd1104fbSMichen Chang 		rc = NS_LDAP_CONFIG;
1757*dd1104fbSMichen Chang 		mlen = snprintf(buffer, msgmax, "%s",
1758*dd1104fbSMichen Chang 		    gettext("No legal LDAP authentication method configured"));
1759*dd1104fbSMichen Chang 		goto out;
1760*dd1104fbSMichen Chang 	}
1761*dd1104fbSMichen Chang 
1762*dd1104fbSMichen Chang 	rc = NS_LDAP_OP_FAILED;
1763*dd1104fbSMichen Chang 
1764*dd1104fbSMichen Chang out:
1765*dd1104fbSMichen Chang 	if (credp != NULL)
1766*dd1104fbSMichen Chang 		(void) __ns_ldap_freeCred(&credp);
1767*dd1104fbSMichen Chang 
1768*dd1104fbSMichen Chang 	if (authpp != NULL)
1769*dd1104fbSMichen Chang 		(void) __ns_ldap_freeParam((void ***)&authpp);
1770*dd1104fbSMichen Chang 
1771*dd1104fbSMichen Chang 	if (error != NULL) {
1772*dd1104fbSMichen Chang 		mlen = snprintf(buffer, msgmax, "%s", error->message);
1773*dd1104fbSMichen Chang 		status = error->status;
1774*dd1104fbSMichen Chang 		(void) __ns_ldap_freeError(&error);
1775*dd1104fbSMichen Chang 	}
1776*dd1104fbSMichen Chang 
1777*dd1104fbSMichen Chang 	if (attrs != NULL) {
1778*dd1104fbSMichen Chang 		int i;
1779*dd1104fbSMichen Chang 		for (i = 0; attrs[i]; i++) {
1780*dd1104fbSMichen Chang 			free(attrs[i]->attrvalue);
1781*dd1104fbSMichen Chang 			free(attrs[i]);
1782*dd1104fbSMichen Chang 		}
1783*dd1104fbSMichen Chang 	}
1784*dd1104fbSMichen Chang 
1785*dd1104fbSMichen Chang 	config_info->len = rlen + mlen + 1;
1786*dd1104fbSMichen Chang 	config_info->str = malloc(config_info->len);
1787*dd1104fbSMichen Chang 	if (config_info->str == NULL) {
1788*dd1104fbSMichen Chang 		config_info->len = 0;
1789*dd1104fbSMichen Chang 		return;
1790*dd1104fbSMichen Chang 	}
1791*dd1104fbSMichen Chang 	result = (ldap_admin_mod_result_t *)config_info->str;
1792*dd1104fbSMichen Chang 	result->ns_err = rc;
1793*dd1104fbSMichen Chang 	result->status = status;
1794*dd1104fbSMichen Chang 	if (mlen != 0) {
1795*dd1104fbSMichen Chang 		result->msg_size = mlen + 1;
1796*dd1104fbSMichen Chang 		(void) strcpy(config_info->str + rlen, buffer);
1797*dd1104fbSMichen Chang 	}
1798*dd1104fbSMichen Chang }
1799*dd1104fbSMichen Chang 
1800*dd1104fbSMichen Chang /*
1801*dd1104fbSMichen Chang  * Check to see if the door client's euid is 0 or if it has ALL zone privilege.
1802*dd1104fbSMichen Chang  * return - 0 No or error
1803*dd1104fbSMichen Chang  *          1 Yes
1804*dd1104fbSMichen Chang  */
1805*dd1104fbSMichen Chang static int
1806*dd1104fbSMichen Chang is_root_or_all_privs(char *dc_str, ucred_t **ucp)
1807*dd1104fbSMichen Chang {
1808*dd1104fbSMichen Chang 	const priv_set_t *ps;	/* door client */
1809*dd1104fbSMichen Chang 	priv_set_t *zs;		/* zone */
1810*dd1104fbSMichen Chang 	int rc = 0;
1811*dd1104fbSMichen Chang 
1812*dd1104fbSMichen Chang 	*ucp = NULL;
1813*dd1104fbSMichen Chang 
1814*dd1104fbSMichen Chang 	/* no more to do if door client's euid is 0 */
1815*dd1104fbSMichen Chang 	if (is_root(0, dc_str, ucp) == 1) {
1816*dd1104fbSMichen Chang 		ucred_free(*ucp);
1817*dd1104fbSMichen Chang 		return (1);
1818*dd1104fbSMichen Chang 	}
1819*dd1104fbSMichen Chang 
1820*dd1104fbSMichen Chang 	/* error if couldn't get the ucred_t */
1821*dd1104fbSMichen Chang 	if (*ucp == NULL)
1822*dd1104fbSMichen Chang 		return (0);
1823*dd1104fbSMichen Chang 
1824*dd1104fbSMichen Chang 	if ((ps = ucred_getprivset(*ucp, PRIV_EFFECTIVE)) != NULL) {
1825*dd1104fbSMichen Chang 		zs = priv_str_to_set("zone", ",", NULL);
1826*dd1104fbSMichen Chang 		if (priv_isequalset(ps, zs))
1827*dd1104fbSMichen Chang 			rc = 1; /* has all zone privs */
1828*dd1104fbSMichen Chang 		else {
1829*dd1104fbSMichen Chang 			if (current_admin.debug_level >= DBG_CANT_FIND)
1830*dd1104fbSMichen Chang 				logit("%s call failed (no all zone privs): "
1831*dd1104fbSMichen Chang 				    "caller pid %ld, uid %u, euid %u "
1832*dd1104fbSMichen Chang 				    "(if uid or euid is %u, it may "
1833*dd1104fbSMichen Chang 				    "be unavailable)\n", dc_str,
1834*dd1104fbSMichen Chang 				    ucred_getpid(*ucp), ucred_getruid(*ucp),
1835*dd1104fbSMichen Chang 				    ucred_geteuid(*ucp), -1);
1836*dd1104fbSMichen Chang 		}
1837*dd1104fbSMichen Chang 		priv_freeset(zs);
1838*dd1104fbSMichen Chang 	}
1839*dd1104fbSMichen Chang 
1840*dd1104fbSMichen Chang 	ucred_free(*ucp);
1841*dd1104fbSMichen Chang 	return (rc);
1842*dd1104fbSMichen Chang }
1843