xref: /titanic_54/usr/src/cmd/ldapcachemgr/cachemgr.c (revision 8142c2b2a11acff39ec7e0576c566a751eb30ba6)
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 /*
227ddae043Siz202018  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Simple doors ldap cache daemon
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <door.h>
367c478bd9Sstevel@tonic-gate #include <time.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387ddae043Siz202018 #include <strings.h>
397c478bd9Sstevel@tonic-gate #include <libintl.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <sys/time.h>
427c478bd9Sstevel@tonic-gate #include <sys/wait.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate #include <pthread.h>
467c478bd9Sstevel@tonic-gate #include <thread.h>
477c478bd9Sstevel@tonic-gate #include <stdarg.h>
487c478bd9Sstevel@tonic-gate #include <fcntl.h>
497c478bd9Sstevel@tonic-gate #include <assert.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <memory.h>
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate #include <syslog.h>
547c478bd9Sstevel@tonic-gate #include <locale.h>	/* LC_ALL */
557ddae043Siz202018 
567ddae043Siz202018 #include <alloca.h>
577ddae043Siz202018 #include <ucontext.h>
587ddae043Siz202018 
59*8142c2b2Schinlong #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);
74*8142c2b2Schinlong static int is_root(int free_uc, char *dc_str, ucred_t **uc);
75*8142c2b2Schinlong static int is_nscd(pid_t pid);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #ifdef SLP
787c478bd9Sstevel@tonic-gate int			use_slp = 0;
797c478bd9Sstevel@tonic-gate static unsigned int	refresh = 10800;	/* dynamic discovery interval */
807c478bd9Sstevel@tonic-gate #endif /* SLP */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static ldap_stat_t *
837c478bd9Sstevel@tonic-gate getcacheptr(char *s)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	static const char *caches[1] = {"ldap"};
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (strncmp(caches[0], s, strlen(caches[0])) == 0)
887c478bd9Sstevel@tonic-gate 		return (&current_admin.ldap_stat);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	return (NULL);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate char *
947c478bd9Sstevel@tonic-gate getcacheopt(char *s)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	while (*s && *s != ',')
977c478bd9Sstevel@tonic-gate 		s++;
987c478bd9Sstevel@tonic-gate 	return ((*s == ',') ? (s + 1) : NULL);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  *  This is here to prevent the ldap_cachemgr becomes
1037c478bd9Sstevel@tonic-gate  *  daemonlized to early to soon during boot time.
1047c478bd9Sstevel@tonic-gate  *  This causes problems during boot when automounter
1057c478bd9Sstevel@tonic-gate  *  and others try to use libsldap before ldap_cachemgr
1067c478bd9Sstevel@tonic-gate  *  finishes walking the server list.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate static void
1097c478bd9Sstevel@tonic-gate sig_ok_to_exit(int signo)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	if (signo == SIGUSR1) {
1127c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): parent exiting...\n");
1137c478bd9Sstevel@tonic-gate 		exit(0);
1147c478bd9Sstevel@tonic-gate 	} else {
1157c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): invalid signal(%d) received.\n",
1167c478bd9Sstevel@tonic-gate 		    signo);
1177c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: "
1187c478bd9Sstevel@tonic-gate 		    "invalid signal(%d) received."), signo);
1197c478bd9Sstevel@tonic-gate 		exit(1);
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate #define	LDAP_TABLES		1	/* ldap */
1237c478bd9Sstevel@tonic-gate #define	TABLE_THREADS		10
1247c478bd9Sstevel@tonic-gate #define	COMMON_THREADS		20
1257c478bd9Sstevel@tonic-gate #define	CACHE_MISS_THREADS	(COMMON_THREADS + LDAP_TABLES * TABLE_THREADS)
1267c478bd9Sstevel@tonic-gate #define	CACHE_HIT_THREADS	20
1277c478bd9Sstevel@tonic-gate #define	MAX_SERVER_THREADS	(CACHE_HIT_THREADS + CACHE_MISS_THREADS)
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static sema_t common_sema;
1307c478bd9Sstevel@tonic-gate static sema_t ldap_sema;
1317c478bd9Sstevel@tonic-gate static thread_key_t lookup_state_key;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static void
1347c478bd9Sstevel@tonic-gate initialize_lookup_clearance()
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	(void) thr_keycreate(&lookup_state_key, NULL);
1377c478bd9Sstevel@tonic-gate 	(void) sema_init(&common_sema, COMMON_THREADS, USYNC_THREAD, 0);
1387c478bd9Sstevel@tonic-gate 	(void) sema_init(&ldap_sema, TABLE_THREADS, USYNC_THREAD, 0);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate int
1427c478bd9Sstevel@tonic-gate get_clearance(int callnumber)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
1457c478bd9Sstevel@tonic-gate 	char	*tab;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (sema_trywait(&common_sema) == 0) {
1487c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, NULL);
1497c478bd9Sstevel@tonic-gate 		return (0);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	switch (callnumber) {
1537c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
1547c478bd9Sstevel@tonic-gate 			tab = "ldap";
1557c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
1567c478bd9Sstevel@tonic-gate 			break;
1577c478bd9Sstevel@tonic-gate 		default:
1587c478bd9Sstevel@tonic-gate 			logit("Internal Error: get_clearance\n");
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (sema_trywait(table_sema) == 0) {
1637c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, (void*)1);
1647c478bd9Sstevel@tonic-gate 		return (0);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_CANT_FIND) {
1687c478bd9Sstevel@tonic-gate 		logit("get_clearance: throttling load for %s table\n", tab);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	return (-1);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate int
1757c478bd9Sstevel@tonic-gate release_clearance(int callnumber)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	int	which;
1787c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	(void) thr_getspecific(lookup_state_key, (void**)&which);
1817c478bd9Sstevel@tonic-gate 	if (which == 0) /* from common pool */ {
1827c478bd9Sstevel@tonic-gate 		(void) sema_post(&common_sema);
1837c478bd9Sstevel@tonic-gate 		return (0);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	switch (callnumber) {
1877c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
1887c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 		default:
1917c478bd9Sstevel@tonic-gate 			logit("Internal Error: release_clearance\n");
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 	(void) sema_post(table_sema);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	return (0);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static mutex_t		create_lock;
2017c478bd9Sstevel@tonic-gate static int		num_servers = 0;
2027c478bd9Sstevel@tonic-gate static thread_key_t	server_key;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate  * Bind a TSD value to a server thread. This enables the destructor to
2077c478bd9Sstevel@tonic-gate  * be called if/when this thread exits.  This would be a programming error,
2087c478bd9Sstevel@tonic-gate  * but better safe than sorry.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2127c478bd9Sstevel@tonic-gate static void *
2137c478bd9Sstevel@tonic-gate server_tsd_bind(void *arg)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	static void	*value = 0;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/*
2187c478bd9Sstevel@tonic-gate 	 * disable cancellation to prevent hangs when server
2197c478bd9Sstevel@tonic-gate 	 * threads disappear
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	(void) thr_setspecific(server_key, value);
223cb5caa98Sdjl 	(void) door_return(NULL, 0, NULL, 0);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	return (value);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * Server threads are created here.
2307c478bd9Sstevel@tonic-gate  */
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2337c478bd9Sstevel@tonic-gate static void
2347c478bd9Sstevel@tonic-gate server_create(door_info_t *dip)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
2377c478bd9Sstevel@tonic-gate 	if (++num_servers > MAX_SERVER_THREADS) {
2387c478bd9Sstevel@tonic-gate 		num_servers--;
2397c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&create_lock);
2407c478bd9Sstevel@tonic-gate 		return;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
2437c478bd9Sstevel@tonic-gate 	(void) thr_create(NULL, 0, server_tsd_bind, NULL,
2447c478bd9Sstevel@tonic-gate 	    THR_BOUND|THR_DETACHED, NULL);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * Server thread are destroyed here
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2527c478bd9Sstevel@tonic-gate static void
2537c478bd9Sstevel@tonic-gate server_destroy(void *arg)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
2567c478bd9Sstevel@tonic-gate 	num_servers--;
2577c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
260a506a34cSth160488 int
2617c478bd9Sstevel@tonic-gate main(int argc, char ** argv)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	int			did;
2647c478bd9Sstevel@tonic-gate 	int			opt;
2657c478bd9Sstevel@tonic-gate 	int			errflg = 0;
2667c478bd9Sstevel@tonic-gate 	int			showstats = 0;
2677c478bd9Sstevel@tonic-gate 	int			doset = 0;
2687c478bd9Sstevel@tonic-gate 	int			dofg = 0;
2697c478bd9Sstevel@tonic-gate 	struct stat		buf;
2707c478bd9Sstevel@tonic-gate 	sigset_t		myset;
2717c478bd9Sstevel@tonic-gate 	struct sigaction	sighupaction;
2727c478bd9Sstevel@tonic-gate 	static void		client_killserver();
2737c478bd9Sstevel@tonic-gate 	int			debug_level = 0;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/* setup for localization */
2767c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2777c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	openlog("ldap_cachemgr", LOG_PID, LOG_DAEMON);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if (chdir(NSLDAPDIRECTORY) < 0) {
2827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("chdir(\"%s\") failed: %s\n"),
2837c478bd9Sstevel@tonic-gate 		    NSLDAPDIRECTORY, strerror(errno));
2847c478bd9Sstevel@tonic-gate 		exit(1);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * Correctly set file mode creation mask, so to make the new files
2897c478bd9Sstevel@tonic-gate 	 * created for door calls being readable by all.
2907c478bd9Sstevel@tonic-gate 	 */
2917c478bd9Sstevel@tonic-gate 	(void) umask(0);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 *  Special case non-root user here -	he/she/they/it can just print
2957c478bd9Sstevel@tonic-gate 	 *					stats
2967c478bd9Sstevel@tonic-gate 	 */
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (geteuid()) {
2997c478bd9Sstevel@tonic-gate 		if (argc != 2 || strcmp(argv[1], "-g")) {
3007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3017c478bd9Sstevel@tonic-gate 			    gettext("Must be root to use any option "
3027c478bd9Sstevel@tonic-gate 			    "other than -g.\n\n"));
3037c478bd9Sstevel@tonic-gate 			usage(argv[0]);
3047c478bd9Sstevel@tonic-gate 		}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		if ((__ns_ldap_cache_ping() != SUCCESS) ||
3077c478bd9Sstevel@tonic-gate 		    (client_getadmin(&current_admin) != 0)) {
3087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3097c478bd9Sstevel@tonic-gate 			    gettext("%s doesn't appear to be running.\n"),
3107c478bd9Sstevel@tonic-gate 			    argv[0]);
3117c478bd9Sstevel@tonic-gate 			exit(1);
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 		(void) client_showstats(&current_admin);
3147c478bd9Sstevel@tonic-gate 		exit(0);
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 *  Determine if there is already a daemon running
3217c478bd9Sstevel@tonic-gate 	 */
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	will_become_server = (__ns_ldap_cache_ping() != SUCCESS);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	/*
3267c478bd9Sstevel@tonic-gate 	 *  load normal config file
3277c478bd9Sstevel@tonic-gate 	 */
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (will_become_server) {
3307c478bd9Sstevel@tonic-gate 		static const ldap_stat_t defaults = {
3317c478bd9Sstevel@tonic-gate 			0,		/* stat */
3327c478bd9Sstevel@tonic-gate 			DEFAULTTTL};	/* ttl */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat = defaults;
3357c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, LOGFILE);
3367c478bd9Sstevel@tonic-gate 	} else {
3377c478bd9Sstevel@tonic-gate 		if (client_getadmin(&current_admin)) {
3387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Cannot contact %s "
3397c478bd9Sstevel@tonic-gate 			    "properly(?)\n"), argv[0]);
3407c478bd9Sstevel@tonic-gate 			exit(1);
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate #ifndef SLP
3457c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgl:r:d:")) != EOF) {
3467c478bd9Sstevel@tonic-gate #else
3477c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgs:l:r:d:")) != EOF) {
3487c478bd9Sstevel@tonic-gate #endif /* SLP */
3497c478bd9Sstevel@tonic-gate 		ldap_stat_t	*cache;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		switch (opt) {
3527c478bd9Sstevel@tonic-gate 		case 'K':
3537c478bd9Sstevel@tonic-gate 			client_killserver();
3547c478bd9Sstevel@tonic-gate 			exit(0);
3557c478bd9Sstevel@tonic-gate 			break;
3567c478bd9Sstevel@tonic-gate 		case 'g':
3577c478bd9Sstevel@tonic-gate 			showstats++;
3587c478bd9Sstevel@tonic-gate 			break;
3597c478bd9Sstevel@tonic-gate 		case 'f':
3607c478bd9Sstevel@tonic-gate 			dofg++;
3617c478bd9Sstevel@tonic-gate 			break;
3627c478bd9Sstevel@tonic-gate 		case 'r':
3637c478bd9Sstevel@tonic-gate 			doset++;
3647c478bd9Sstevel@tonic-gate 			cache = getcacheptr("ldap");
3657c478bd9Sstevel@tonic-gate 			if (!optarg) {
3667c478bd9Sstevel@tonic-gate 				errflg++;
3677c478bd9Sstevel@tonic-gate 				break;
3687c478bd9Sstevel@tonic-gate 			}
3697c478bd9Sstevel@tonic-gate 			cache->ldap_ttl = atoi(optarg);
3707c478bd9Sstevel@tonic-gate 			break;
3717c478bd9Sstevel@tonic-gate 		case 'l':
3727c478bd9Sstevel@tonic-gate 			doset++;
3737c478bd9Sstevel@tonic-gate 			(void) strlcpy(current_admin.logfile,
3747c478bd9Sstevel@tonic-gate 			    optarg, sizeof (current_admin.logfile));
3757c478bd9Sstevel@tonic-gate 			break;
3767c478bd9Sstevel@tonic-gate 		case 'd':
3777c478bd9Sstevel@tonic-gate 			doset++;
3787c478bd9Sstevel@tonic-gate 			debug_level = atoi(optarg);
3797c478bd9Sstevel@tonic-gate 			break;
3807c478bd9Sstevel@tonic-gate #ifdef SLP
3817c478bd9Sstevel@tonic-gate 		case 's':	/* undocumented: use dynamic (SLP) config */
3827c478bd9Sstevel@tonic-gate 			use_slp = 1;
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate #endif /* SLP */
3857c478bd9Sstevel@tonic-gate 		default:
3867c478bd9Sstevel@tonic-gate 			errflg++;
3877c478bd9Sstevel@tonic-gate 			break;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (errflg)
3927c478bd9Sstevel@tonic-gate 		usage(argv[0]);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * will not show statistics if no daemon running
3967c478bd9Sstevel@tonic-gate 	 */
3977c478bd9Sstevel@tonic-gate 	if (will_become_server && showstats) {
3987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3997c478bd9Sstevel@tonic-gate 		    gettext("%s doesn't appear to be running.\n"),
4007c478bd9Sstevel@tonic-gate 		    argv[0]);
4017c478bd9Sstevel@tonic-gate 		exit(1);
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (!will_become_server) {
4057c478bd9Sstevel@tonic-gate 		if (showstats) {
4067c478bd9Sstevel@tonic-gate 			(void) client_showstats(&current_admin);
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 		if (doset) {
4097c478bd9Sstevel@tonic-gate 			current_admin.debug_level = debug_level;
4107c478bd9Sstevel@tonic-gate 			if (client_setadmin(&current_admin) < 0) {
4117c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4127c478bd9Sstevel@tonic-gate 				    gettext("Error during admin call\n"));
4137c478bd9Sstevel@tonic-gate 				exit(1);
4147c478bd9Sstevel@tonic-gate 			}
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 		if (!showstats && !doset) {
4177c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4187c478bd9Sstevel@tonic-gate 			gettext("%s already running....use '%s "
4197c478bd9Sstevel@tonic-gate 			    "-K' to stop\n"), argv[0], argv[0]);
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 		exit(0);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	/*
4257c478bd9Sstevel@tonic-gate 	 *   daemon from here on
4267c478bd9Sstevel@tonic-gate 	 */
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	if (debug_level) {
4297c478bd9Sstevel@tonic-gate 		/*
4307c478bd9Sstevel@tonic-gate 		 * we're debugging...
4317c478bd9Sstevel@tonic-gate 		 */
4327c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
4337c478bd9Sstevel@tonic-gate 			/*
4347c478bd9Sstevel@tonic-gate 			 * no specified log file
4357c478bd9Sstevel@tonic-gate 			 */
4367c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, LOGFILE);
4377c478bd9Sstevel@tonic-gate 		else
4387c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
4397c478bd9Sstevel@tonic-gate 			    current_admin.logfile);
4407c478bd9Sstevel@tonic-gate 		/*
4417c478bd9Sstevel@tonic-gate 		 * validate the range of debug level number
4427c478bd9Sstevel@tonic-gate 		 * and set the number to current_admin.debug_level
4437c478bd9Sstevel@tonic-gate 		 */
4447c478bd9Sstevel@tonic-gate 		if (cachemgr_set_dl(&current_admin, debug_level) < 0) {
4457c478bd9Sstevel@tonic-gate 				/*
4467c478bd9Sstevel@tonic-gate 				 * print error messages to the screen
4477c478bd9Sstevel@tonic-gate 				 * cachemgr_set_dl prints msgs to cachemgr.log
4487c478bd9Sstevel@tonic-gate 				 * only
4497c478bd9Sstevel@tonic-gate 				 */
4507c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4517c478bd9Sstevel@tonic-gate 				gettext("Incorrect Debug Level: %d\n"
4527c478bd9Sstevel@tonic-gate 				"It should be between %d and %d\n"),
4537c478bd9Sstevel@tonic-gate 				    debug_level, DBG_OFF, MAXDEBUG);
4547c478bd9Sstevel@tonic-gate 			exit(-1);
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 	} else {
4577c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
4587c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, "/dev/null");
4597c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
4607c478bd9Sstevel@tonic-gate 			    current_admin.logfile);
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if (dofg == 0)
4647c478bd9Sstevel@tonic-gate 		detachfromtty(argv[0]);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/*
4677c478bd9Sstevel@tonic-gate 	 * perform some initialization
4687c478bd9Sstevel@tonic-gate 	 */
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	initialize_lookup_clearance();
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (getldap_init() != 0)
4737c478bd9Sstevel@tonic-gate 		exit(-1);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 * Establish our own server thread pool
4777c478bd9Sstevel@tonic-gate 	 */
4787c478bd9Sstevel@tonic-gate 
479cb5caa98Sdjl 	(void) door_server_create(server_create);
4807c478bd9Sstevel@tonic-gate 	if (thr_keycreate(&server_key, server_destroy) != 0) {
4817c478bd9Sstevel@tonic-gate 		logit("thr_keycreate() call failed\n");
4827c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4837c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: thr_keycreate() call failed"));
4847c478bd9Sstevel@tonic-gate 		perror("thr_keycreate");
4857c478bd9Sstevel@tonic-gate 		exit(-1);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * Create a door
4907c478bd9Sstevel@tonic-gate 	 */
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if ((did = door_create(switcher, LDAP_CACHE_DOOR_COOKIE,
4937c478bd9Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
4947c478bd9Sstevel@tonic-gate 		logit("door_create() call failed\n");
4957c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
4967c478bd9Sstevel@tonic-gate 		    "ldap_cachemgr: door_create() call failed"));
4977c478bd9Sstevel@tonic-gate 		perror("door_create");
4987c478bd9Sstevel@tonic-gate 		exit(-1);
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	/*
5027c478bd9Sstevel@tonic-gate 	 * bind to file system
5037c478bd9Sstevel@tonic-gate 	 */
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (stat(LDAP_CACHE_DOOR, &buf) < 0) {
5067c478bd9Sstevel@tonic-gate 		int	newfd;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		if ((newfd = creat(LDAP_CACHE_DOOR, 0444)) < 0) {
5097c478bd9Sstevel@tonic-gate 			logit("Cannot create %s:%s\n",
5107c478bd9Sstevel@tonic-gate 			    LDAP_CACHE_DOOR,
5117c478bd9Sstevel@tonic-gate 			    strerror(errno));
5127c478bd9Sstevel@tonic-gate 			exit(1);
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 		(void) close(newfd);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	if (fattach(did, LDAP_CACHE_DOOR) < 0) {
5187c478bd9Sstevel@tonic-gate 		if ((errno != EBUSY) ||
5197c478bd9Sstevel@tonic-gate 		    (fdetach(LDAP_CACHE_DOOR) <  0) ||
5207c478bd9Sstevel@tonic-gate 		    (fattach(did, LDAP_CACHE_DOOR) < 0)) {
5217c478bd9Sstevel@tonic-gate 			logit("fattach() call failed\n");
5227c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
5237c478bd9Sstevel@tonic-gate 			    "ldap_cachemgr: fattach() call failed"));
5247c478bd9Sstevel@tonic-gate 			perror("fattach");
5257c478bd9Sstevel@tonic-gate 			exit(2);
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	/* catch SIGHUP revalid signals */
5307c478bd9Sstevel@tonic-gate 	sighupaction.sa_handler = getldap_revalidate;
5317c478bd9Sstevel@tonic-gate 	sighupaction.sa_flags = 0;
5327c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sighupaction.sa_mask);
5337c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&myset);
5347c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGHUP);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (sigaction(SIGHUP, &sighupaction, NULL) < 0) {
5377c478bd9Sstevel@tonic-gate 		logit("sigaction() call failed\n");
5387c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5397c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: sigaction() call failed"));
5407c478bd9Sstevel@tonic-gate 		perror("sigaction");
5417c478bd9Sstevel@tonic-gate 		exit(1);
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_BLOCK, &myset, NULL) < 0) {
5457c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
5467c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5477c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: thr_sigsetmask() call failed"));
5487c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
5497c478bd9Sstevel@tonic-gate 		exit(1);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 *  kick off revalidate threads only if ttl != 0
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL, (void *(*)(void*))getldap_refresh,
5577c478bd9Sstevel@tonic-gate 	    0, 0, NULL) != 0) {
5587c478bd9Sstevel@tonic-gate 		logit("thr_create() call failed\n");
5597c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5607c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: thr_create() call failed"));
5617c478bd9Sstevel@tonic-gate 		perror("thr_create");
5627c478bd9Sstevel@tonic-gate 		exit(1);
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	/*
5667c478bd9Sstevel@tonic-gate 	 *  kick off the thread which refreshes the server info
5677c478bd9Sstevel@tonic-gate 	 */
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL, (void *(*)(void*))getldap_serverInfo_refresh,
5707c478bd9Sstevel@tonic-gate 	    0, 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 #ifdef SLP
5797c478bd9Sstevel@tonic-gate 	if (use_slp) {
5807c478bd9Sstevel@tonic-gate 		/* kick off SLP discovery thread */
5817c478bd9Sstevel@tonic-gate 		if (thr_create(NULL, NULL, (void *(*)(void *))discover,
5827c478bd9Sstevel@tonic-gate 		    (void *)&refresh, 0, NULL) != 0) {
5837c478bd9Sstevel@tonic-gate 			logit("thr_create() call failed\n");
5847c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("ldap_cachemgr: thr_create() "
5857c478bd9Sstevel@tonic-gate 			    "call failed"));
5867c478bd9Sstevel@tonic-gate 			perror("thr_create");
5877c478bd9Sstevel@tonic-gate 			exit(1);
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate #endif /* SLP */
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_UNBLOCK, &myset, NULL) < 0) {
5937c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
5947c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5957c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: the_sigsetmask() call failed"));
5967c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
5977c478bd9Sstevel@tonic-gate 		exit(1);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
6017c478bd9Sstevel@tonic-gate 	while (1) {
6027c478bd9Sstevel@tonic-gate 		(void) pause();
6037c478bd9Sstevel@tonic-gate 	}
604cb5caa98Sdjl 	/* NOTREACHED */
605cb5caa98Sdjl 	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 
6097ddae043Siz202018 /*
6107ddae043Siz202018  * Before calling the alloca() function we have to be sure that we won't get
6117ddae043Siz202018  * beyond the stack. Since we don't know the precise layout of the stack,
6127ddae043Siz202018  * the address of an automatic of the function gives us a rough idea, plus/minus
6137ddae043Siz202018  * a bit. We also need a bit more of stackspace after the call to be able
6147ddae043Siz202018  * to call further functions. Even something as simple as making a system call
6157ddae043Siz202018  * from within this function can take ~100 Bytes of stackspace.
6167ddae043Siz202018  */
6177ddae043Siz202018 #define	SAFETY_BUFFER 32 * 1024 /* 32KB */
6187ddae043Siz202018 
6197ddae043Siz202018 static
6207ddae043Siz202018 size_t
6217ddae043Siz202018 get_data_size(LineBuf *config_info, int *err_code)
6227ddae043Siz202018 {
6237ddae043Siz202018 	size_t		configSize = sizeof (ldap_return_t);
6247ddae043Siz202018 	dataunion	*buf = NULL; /* For the 'sizeof' purpose */
6257ddae043Siz202018 
6267ddae043Siz202018 	if (config_info->str != NULL &&
6277ddae043Siz202018 	    config_info->len >= sizeof (buf->data.ldap_ret.ldap_u.config)) {
6287ddae043Siz202018 		configSize = sizeof (buf->space) +
6297ddae043Siz202018 		    config_info->len -
6307ddae043Siz202018 		    sizeof (buf->data.ldap_ret.ldap_u.config);
6317ddae043Siz202018 
6327ddae043Siz202018 		if (!stack_inbounds((char *)&buf -
6337ddae043Siz202018 		    (configSize + SAFETY_BUFFER))) {
6347ddae043Siz202018 			/*
6357ddae043Siz202018 			 * We do not have enough space on the stack
6367ddae043Siz202018 			 * to accomodate the whole DUAProfile
6377ddae043Siz202018 			 */
6387ddae043Siz202018 			logit("The DUAProfile is too big. There is not enough "
6397ddae043Siz202018 			    "space to process it. Ignoring it.\n");
6407ddae043Siz202018 			syslog(LOG_ERR, gettext("ldap_cachemgr: The DUAProfile "
6417ddae043Siz202018 			    "is too big. There is not enough space "
6427ddae043Siz202018 			    "to process it. Ignoring it."));
6437ddae043Siz202018 
6447ddae043Siz202018 			*err_code = SERVERERROR;
6457ddae043Siz202018 
6467ddae043Siz202018 			free(config_info->str);
6477ddae043Siz202018 			config_info->str = NULL;
6487ddae043Siz202018 			config_info->len = 0;
6497ddae043Siz202018 			configSize = sizeof (ldap_return_t);
6507ddae043Siz202018 		}
6517ddae043Siz202018 	}
6527ddae043Siz202018 
6537ddae043Siz202018 	return (configSize);
6547ddae043Siz202018 }
6557ddae043Siz202018 
6567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6577c478bd9Sstevel@tonic-gate static void
6587c478bd9Sstevel@tonic-gate switcher(void *cookie, char *argp, size_t arg_size,
6597c478bd9Sstevel@tonic-gate     door_desc_t *dp, uint_t n_desc)
6607c478bd9Sstevel@tonic-gate {
6617ddae043Siz202018 #define	GETSIZE 1000
6627ddae043Siz202018 #define	ALLOCATE 1001
6637ddae043Siz202018 
6647c478bd9Sstevel@tonic-gate 	ldap_call_t	*ptr = (ldap_call_t *)argp;
665*8142c2b2Schinlong 	ucred_t		*uc = NULL;
6667c478bd9Sstevel@tonic-gate 
6677ddae043Siz202018 	LineBuf		configInfo;
6687ddae043Siz202018 	dataunion	*buf = NULL;
6697ddae043Siz202018 	/*
6707ddae043Siz202018 	 * By default the size of  a buffer to be passed down to a client
6717ddae043Siz202018 	 * is equal to the size of the ldap_return_t structure. We need
6727ddae043Siz202018 	 * a bigger buffer in a few cases.
6737ddae043Siz202018 	 */
6747ddae043Siz202018 	size_t		configSize = sizeof (ldap_return_t);
675*8142c2b2Schinlong 	int		ldapErrno = 0, state, callnumber;
6767ddae043Siz202018 	struct {
6777ddae043Siz202018 		void	*begin;
6787ddae043Siz202018 		size_t	size;
6797ddae043Siz202018 		uint8_t	destroy;
6807ddae043Siz202018 	} dataSource;
6817ddae043Siz202018 
6827c478bd9Sstevel@tonic-gate 	if (argp == DOOR_UNREF_DATA) {
6837c478bd9Sstevel@tonic-gate 		logit("Door Slam... invalid door param\n");
6847c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: Door Slam... "
6857c478bd9Sstevel@tonic-gate 		    "invalid door param"));
6867c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Door Slam... invalid door param\n"));
6877c478bd9Sstevel@tonic-gate 		exit(0);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	if (ptr == NULL) { /* empty door call */
6917c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0); /* return the favor */
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947ddae043Siz202018 	bzero(&dataSource, sizeof (dataSource));
6957ddae043Siz202018 
6967ddae043Siz202018 	/*
6977ddae043Siz202018 	 * We presume that sizeof (ldap_return_t) bytes are always available
6987ddae043Siz202018 	 * on the stack
6997ddae043Siz202018 	 */
700*8142c2b2Schinlong 	callnumber = ptr->ldap_callnumber;
7017ddae043Siz202018 
702*8142c2b2Schinlong 	switch (callnumber) {
7037c478bd9Sstevel@tonic-gate 		case NULLCALL:
7047ddae043Siz202018 			/*
7057ddae043Siz202018 			 * Just a 'ping'. Use the default size
7067ddae043Siz202018 			 * of the buffer and set the
7077ddae043Siz202018 			 * 'OK' error code.
7087ddae043Siz202018 			 */
7097ddae043Siz202018 			state = ALLOCATE;
7107c478bd9Sstevel@tonic-gate 			break;
7117c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
7127ddae043Siz202018 			/*
7137ddae043Siz202018 			 * Get the current LDAP configuration.
7147ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
7157ddae043Siz202018 			 * the size of ldap_return_t, the next step will
7167ddae043Siz202018 			 * calculate who much space exactly is required.
7177ddae043Siz202018 			 */
7187ddae043Siz202018 			getldap_lookup(&configInfo, ptr);
7197ddae043Siz202018 
7207ddae043Siz202018 			state = GETSIZE;
7217ddae043Siz202018 			break;
7227ddae043Siz202018 		case GETLDAPSERVER:
7237ddae043Siz202018 			/*
7247ddae043Siz202018 			 * Get the root DSE for a next server in the list.
7257ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
7267ddae043Siz202018 			 * the size of ldap_return_t, the next step will
7277ddae043Siz202018 			 * calculate who much space exactly is required.
7287ddae043Siz202018 			 */
7297ddae043Siz202018 			getldap_getserver(&configInfo, ptr);
7307ddae043Siz202018 
7317ddae043Siz202018 			state = GETSIZE;
7327ddae043Siz202018 			break;
7337ddae043Siz202018 		case GETCACHESTAT:
7347ddae043Siz202018 			/*
7357ddae043Siz202018 			 * Get the cache stattistics.
7367ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
7377ddae043Siz202018 			 * the size of ldap_return_t, the next step will
7387ddae043Siz202018 			 * calculate how much space exactly is required.
7397ddae043Siz202018 			 */
7407ddae043Siz202018 			getldap_get_cacheStat(&configInfo);
7417ddae043Siz202018 
7427ddae043Siz202018 			state = GETSIZE;
7437c478bd9Sstevel@tonic-gate 			break;
7447c478bd9Sstevel@tonic-gate 		case GETADMIN:
7457ddae043Siz202018 			/*
7467ddae043Siz202018 			 * Get current configuration and statistics.
7477ddae043Siz202018 			 * The size of the statistics structure is less then
7487ddae043Siz202018 			 * sizeof (ldap_return_t). So specify the source
7497ddae043Siz202018 			 * where to take the info and proceed with the memory
7507ddae043Siz202018 			 * allocation.
7517ddae043Siz202018 			 */
7527ddae043Siz202018 			state = ALLOCATE;
7537ddae043Siz202018 
7547ddae043Siz202018 			if (ldapErrno == 0) {
7557ddae043Siz202018 				dataSource.begin = &current_admin;
7567ddae043Siz202018 				dataSource.size = sizeof (current_admin);
7577ddae043Siz202018 				dataSource.destroy = 0;
7587ddae043Siz202018 			}
7597c478bd9Sstevel@tonic-gate 			break;
7607c478bd9Sstevel@tonic-gate 		case KILLSERVER:
7617ddae043Siz202018 			/*
7627ddae043Siz202018 			 * Process the request and proceed with the default
7637ddae043Siz202018 			 * buffer allocation.
7647ddae043Siz202018 			 */
765*8142c2b2Schinlong 			if (is_root(1, "KILLSERVER", &uc))
7667c478bd9Sstevel@tonic-gate 				exit(0);
767*8142c2b2Schinlong 
768*8142c2b2Schinlong 			ldapErrno = -1;
769*8142c2b2Schinlong 			state = ALLOCATE;
7707c478bd9Sstevel@tonic-gate 			break;
7717ddae043Siz202018 		case SETADMIN:
772*8142c2b2Schinlong 			/*
773*8142c2b2Schinlong 			 * Process the request and proceed with the default
774*8142c2b2Schinlong 			 * buffer allocation.
775*8142c2b2Schinlong 			 */
776*8142c2b2Schinlong 			if (is_root(1, "SETADMIN", &uc))
7777ddae043Siz202018 				ldapErrno = setadmin(ptr);
778*8142c2b2Schinlong 			else
7797ddae043Siz202018 				ldapErrno = -1;
7807ddae043Siz202018 
7817ddae043Siz202018 			state = ALLOCATE;
7827c478bd9Sstevel@tonic-gate 			break;
7837c478bd9Sstevel@tonic-gate 		case GETCACHE:
7847ddae043Siz202018 			/*
7857ddae043Siz202018 			 * Get the cache stattistics.
7867ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
7877ddae043Siz202018 			 * the size of ldap_return_t, the next step will
7887ddae043Siz202018 			 * calculate how much space exactly is required.
7897ddae043Siz202018 			 */
7907ddae043Siz202018 			getldap_get_cacheData(&configInfo, ptr);
7917ddae043Siz202018 
7927ddae043Siz202018 			state = GETSIZE;
7937c478bd9Sstevel@tonic-gate 			break;
7947c478bd9Sstevel@tonic-gate 		case SETCACHE:
7957ddae043Siz202018 			/*
7967ddae043Siz202018 			 * Process the request and proceed with the default
7977ddae043Siz202018 			 * buffer allocation.
7987ddae043Siz202018 			 */
799*8142c2b2Schinlong 			if (is_root(0, "SETCACHE", &uc) &&
800*8142c2b2Schinlong 			    is_nscd(ucred_getpid(uc))) {
8017ddae043Siz202018 				ldapErrno = getldap_set_cacheData(ptr);
8027c478bd9Sstevel@tonic-gate 				current_admin.ldap_stat.ldap_numbercalls++;
803*8142c2b2Schinlong 			} else
804*8142c2b2Schinlong 				ldapErrno = -1;
8057ddae043Siz202018 
806*8142c2b2Schinlong 			if (uc != NULL)
807*8142c2b2Schinlong 				ucred_free(uc);
8087ddae043Siz202018 			state = ALLOCATE;
8097c478bd9Sstevel@tonic-gate 			break;
8107c478bd9Sstevel@tonic-gate 		default:
8117ddae043Siz202018 			/*
8127ddae043Siz202018 			 * This means an unknown request type. Proceed with
8137ddae043Siz202018 			 * the default buffer allocation.
8147ddae043Siz202018 			 */
8157c478bd9Sstevel@tonic-gate 			logit("Unknown ldap service door call op %d\n",
8167c478bd9Sstevel@tonic-gate 			    ptr->ldap_callnumber);
8177ddae043Siz202018 			ldapErrno = -99;
8187ddae043Siz202018 
8197ddae043Siz202018 			state = ALLOCATE;
8207ddae043Siz202018 			break;
821*8142c2b2Schinlong 	}
822*8142c2b2Schinlong 
823*8142c2b2Schinlong 	switch (state) {
8247ddae043Siz202018 		case GETSIZE:
8257ddae043Siz202018 			/*
8267ddae043Siz202018 			 * This stage calculates how much data will be
8277ddae043Siz202018 			 * passed down to the client, checks if there is
8287ddae043Siz202018 			 * enough space on the stack to accommodate the data,
8297ddae043Siz202018 			 * increases the value of the configSize variable
8307ddae043Siz202018 			 * if necessary and specifies the data source.
8317ddae043Siz202018 			 * In case of any error occurred ldapErrno will be set
8327ddae043Siz202018 			 * appropriately.
8337ddae043Siz202018 			 */
8347ddae043Siz202018 			if (configInfo.str == NULL) {
8357ddae043Siz202018 				ldapErrno = -1;
8367ddae043Siz202018 			}
8377ddae043Siz202018 
8387ddae043Siz202018 			configSize = get_data_size(&configInfo, &ldapErrno);
8397ddae043Siz202018 
8407ddae043Siz202018 			if (ldapErrno == 0) {
8417ddae043Siz202018 				dataSource.begin = configInfo.str;
8427ddae043Siz202018 				dataSource.size = configInfo.len;
8437ddae043Siz202018 				dataSource.destroy = 1;
8447ddae043Siz202018 			}
8457ddae043Siz202018 
8467ddae043Siz202018 			current_admin.ldap_stat.ldap_numbercalls++;
847*8142c2b2Schinlong 			/* FALLTHRU */
8487ddae043Siz202018 		case ALLOCATE:
8497ddae043Siz202018 			/*
8507ddae043Siz202018 			 * Allocate a buffer of the calculated (or default) size
8517ddae043Siz202018 			 * and proceed with populating it with data.
8527ddae043Siz202018 			 */
8537ddae043Siz202018 			buf = (dataunion *) alloca(configSize);
8547ddae043Siz202018 
8557ddae043Siz202018 			/*
8567ddae043Siz202018 			 * Set a return code and, if a data source is specified,
8577ddae043Siz202018 			 * copy data from the source to the buffer.
8587ddae043Siz202018 			 */
8597ddae043Siz202018 			buf->data.ldap_ret.ldap_errno = ldapErrno;
8607ddae043Siz202018 			buf->data.ldap_ret.ldap_return_code = ldapErrno;
8617ddae043Siz202018 			buf->data.ldap_ret.ldap_bufferbytesused = configSize;
8627ddae043Siz202018 
8637ddae043Siz202018 			if (dataSource.begin != NULL) {
8647ddae043Siz202018 				(void) memcpy(buf->data.ldap_ret.ldap_u.config,
8657ddae043Siz202018 				    dataSource.begin,
8667ddae043Siz202018 				    dataSource.size);
8677ddae043Siz202018 				if (dataSource.destroy) {
8687ddae043Siz202018 					free(dataSource.begin);
8697ddae043Siz202018 				}
8707ddae043Siz202018 			}
8717ddae043Siz202018 
8727c478bd9Sstevel@tonic-gate 	}
8737ddae043Siz202018 	(void) door_return((char *)&buf->data,
8747ddae043Siz202018 	    buf->data.ldap_ret.ldap_bufferbytesused,
8757ddae043Siz202018 	    NULL,
8767ddae043Siz202018 	    0);
8777ddae043Siz202018 #undef	GETSIZE
8787ddae043Siz202018 #undef	ALLOCATE
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate static void
8827c478bd9Sstevel@tonic-gate usage(char *s)
8837c478bd9Sstevel@tonic-gate {
8847c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
8857c478bd9Sstevel@tonic-gate 	    gettext("Usage: %s [-d debug_level] [-l logfilename]\n"), s);
8867c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-K] "
8877c478bd9Sstevel@tonic-gate 	    "[-r revalidate_interval] "));
8887c478bd9Sstevel@tonic-gate #ifndef SLP
8897c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g]\n"));
8907c478bd9Sstevel@tonic-gate #else
8917c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g] [-s]\n"));
8927c478bd9Sstevel@tonic-gate #endif /* SLP */
8937c478bd9Sstevel@tonic-gate 	exit(1);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate static int	logfd = -1;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate static int
9007c478bd9Sstevel@tonic-gate cachemgr_set_lf(admin_t *ptr, char *logfile)
9017c478bd9Sstevel@tonic-gate {
9027c478bd9Sstevel@tonic-gate 	int	newlogfd;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/*
9057c478bd9Sstevel@tonic-gate 	 *  we don't really want to try and open the log file
9067c478bd9Sstevel@tonic-gate 	 *  /dev/null since that will fail w/ our security fixes
9077c478bd9Sstevel@tonic-gate 	 */
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	if (logfile == NULL || *logfile == 0) {
9107c478bd9Sstevel@tonic-gate 		/*EMPTY*/;
9117c478bd9Sstevel@tonic-gate 	} else if (strcmp(logfile, "/dev/null") == 0) {
9127c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, "/dev/null");
9137c478bd9Sstevel@tonic-gate 		(void) close(logfd);
9147c478bd9Sstevel@tonic-gate 		logfd = -1;
9157c478bd9Sstevel@tonic-gate 	} else {
9167c478bd9Sstevel@tonic-gate 		if ((newlogfd =
9177c478bd9Sstevel@tonic-gate 		    open(logfile, O_EXCL|O_WRONLY|O_CREAT, 0644)) < 0) {
9187c478bd9Sstevel@tonic-gate 			/*
9197c478bd9Sstevel@tonic-gate 			 * File already exists... now we need to get cute
9207c478bd9Sstevel@tonic-gate 			 * since opening a file in a world-writeable directory
9217c478bd9Sstevel@tonic-gate 			 * safely is hard = it could be a hard link or a
9227c478bd9Sstevel@tonic-gate 			 * symbolic link to a system file.
9237c478bd9Sstevel@tonic-gate 			 *
9247c478bd9Sstevel@tonic-gate 			 */
9257c478bd9Sstevel@tonic-gate 			struct stat	before;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 			if (lstat(logfile, &before) < 0) {
9287c478bd9Sstevel@tonic-gate 				logit("Cannot open new logfile \"%s\": %sn",
9297c478bd9Sstevel@tonic-gate 				    logfile, strerror(errno));
9307c478bd9Sstevel@tonic-gate 				return (-1);
9317c478bd9Sstevel@tonic-gate 			}
9327c478bd9Sstevel@tonic-gate 			if (S_ISREG(before.st_mode) &&	/* no symbolic links */
9337c478bd9Sstevel@tonic-gate 			    (before.st_nlink == 1) &&	/* no hard links */
9347c478bd9Sstevel@tonic-gate 			    (before.st_uid == 0)) {	/* owned by root */
9357c478bd9Sstevel@tonic-gate 				if ((newlogfd =
9367c478bd9Sstevel@tonic-gate 				    open(logfile,
9377c478bd9Sstevel@tonic-gate 				    O_APPEND|O_WRONLY, 0644)) < 0) {
9387c478bd9Sstevel@tonic-gate 					logit("Cannot open new logfile "
9397c478bd9Sstevel@tonic-gate 					    "\"%s\": %s\n",
9407c478bd9Sstevel@tonic-gate 					    logfile, strerror(errno));
9417c478bd9Sstevel@tonic-gate 					return (-1);
9427c478bd9Sstevel@tonic-gate 				}
9437c478bd9Sstevel@tonic-gate 			} else {
9447c478bd9Sstevel@tonic-gate 				logit("Cannot use specified logfile "
9457c478bd9Sstevel@tonic-gate 				    "\"%s\": file is/has links or isn't "
9467c478bd9Sstevel@tonic-gate 				    "owned by root\n", logfile);
9477c478bd9Sstevel@tonic-gate 				return (-1);
9487c478bd9Sstevel@tonic-gate 			}
9497c478bd9Sstevel@tonic-gate 		}
9507c478bd9Sstevel@tonic-gate 		(void) strlcpy(ptr->logfile, logfile, sizeof (ptr->logfile));
9517c478bd9Sstevel@tonic-gate 		(void) close(logfd);
9527c478bd9Sstevel@tonic-gate 		logfd = newlogfd;
9537c478bd9Sstevel@tonic-gate 		logit("Starting ldap_cachemgr, logfile %s\n", logfile);
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 	return (0);
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
9597c478bd9Sstevel@tonic-gate void
9607c478bd9Sstevel@tonic-gate logit(char *format, ...)
9617c478bd9Sstevel@tonic-gate {
9627c478bd9Sstevel@tonic-gate 	static mutex_t	loglock;
9637c478bd9Sstevel@tonic-gate 	struct timeval	tv;
9647c478bd9Sstevel@tonic-gate 	char		buffer[BUFSIZ];
9657c478bd9Sstevel@tonic-gate 	va_list		ap;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	va_start(ap, format);
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (logfd >= 0) {
9707c478bd9Sstevel@tonic-gate 		int	safechars;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tv, NULL);
9737c478bd9Sstevel@tonic-gate 		(void) ctime_r(&tv.tv_sec, buffer, BUFSIZ);
9747c478bd9Sstevel@tonic-gate 		(void) snprintf(buffer+19, BUFSIZE, ".%.4ld	",
9757c478bd9Sstevel@tonic-gate 		    tv.tv_usec/100);
9767c478bd9Sstevel@tonic-gate 		safechars = sizeof (buffer) - 30;
9777c478bd9Sstevel@tonic-gate 		if (vsnprintf(buffer+25, safechars, format, ap) > safechars)
9787c478bd9Sstevel@tonic-gate 			(void) strcat(buffer, "...\n");
9797c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&loglock);
9807c478bd9Sstevel@tonic-gate 		(void) write(logfd, buffer, strlen(buffer));
9817c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&loglock);
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate 	va_end(ap);
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate static int
9887c478bd9Sstevel@tonic-gate client_getadmin(admin_t *ptr)
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate 	dataunion		u;
9917c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
9927c478bd9Sstevel@tonic-gate 	int		ndata;
9937c478bd9Sstevel@tonic-gate 	int		adata;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETADMIN;
9967c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
9977c478bd9Sstevel@tonic-gate 	adata = sizeof (u.data);
9987c478bd9Sstevel@tonic-gate 	dptr = &u.data;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
10017c478bd9Sstevel@tonic-gate 		return (-1);
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 	(void) memcpy(ptr, dptr->ldap_ret.ldap_u.buff, sizeof (*ptr));
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	return (0);
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate static int
10107ddae043Siz202018 setadmin(ldap_call_t *ptr)
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate 	admin_t	*new;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	new = (admin_t *)ptr->ldap_u.domainname;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/*
10177c478bd9Sstevel@tonic-gate 	 *  global admin stuff
10187c478bd9Sstevel@tonic-gate 	 */
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	if ((cachemgr_set_lf(&current_admin, new->logfile) < 0) ||
10217c478bd9Sstevel@tonic-gate 	    cachemgr_set_dl(&current_admin, new->debug_level) < 0) {
10227c478bd9Sstevel@tonic-gate 		return (-1);
10237c478bd9Sstevel@tonic-gate 	}
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	if (cachemgr_set_ttl(&current_admin.ldap_stat,
10267c478bd9Sstevel@tonic-gate 	    "ldap",
10277c478bd9Sstevel@tonic-gate 	    new->ldap_stat.ldap_ttl) < 0) {
10287c478bd9Sstevel@tonic-gate 		return (-1);
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	return (0);
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate static void
10367c478bd9Sstevel@tonic-gate client_killserver()
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	dataunion		u;
10397c478bd9Sstevel@tonic-gate 	ldap_data_t		*dptr;
10407c478bd9Sstevel@tonic-gate 	int			ndata;
10417c478bd9Sstevel@tonic-gate 	int			adata;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = KILLSERVER;
10447c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
10457c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
10467c478bd9Sstevel@tonic-gate 	dptr = &u.data;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	__ns_ldap_trydoorcall(&dptr, &ndata, &adata);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate static int
10537c478bd9Sstevel@tonic-gate client_setadmin(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 = SETADMIN;
10617c478bd9Sstevel@tonic-gate 	(void) memcpy(u.data.ldap_call.ldap_u.domainname, ptr, sizeof (*ptr));
10627c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
10637c478bd9Sstevel@tonic-gate 	adata = sizeof (*ptr);
10647c478bd9Sstevel@tonic-gate 	dptr = &u.data;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
10677c478bd9Sstevel@tonic-gate 		return (-1);
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	return (0);
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate static int
10747c478bd9Sstevel@tonic-gate client_showstats(admin_t *ptr)
10757c478bd9Sstevel@tonic-gate {
10767c478bd9Sstevel@tonic-gate 	dataunion	u;
10777c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
10787c478bd9Sstevel@tonic-gate 	int		ndata;
10797c478bd9Sstevel@tonic-gate 	int		adata;
10807c478bd9Sstevel@tonic-gate 	char		*rbuf, *sptr, *rest;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	/*
10837c478bd9Sstevel@tonic-gate 	 * print admin data
10847c478bd9Sstevel@tonic-gate 	 */
10857c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr configuration:\n"));
10867c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server debug level %10d\n"), ptr->debug_level);
10877c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server log file\t\"%s\"\n"), ptr->logfile);
10887c478bd9Sstevel@tonic-gate 	(void) printf(gettext("number of calls to ldapcachemgr %10d\n"),
10897c478bd9Sstevel@tonic-gate 	    ptr->ldap_stat.ldap_numbercalls);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	/*
10927c478bd9Sstevel@tonic-gate 	 * get cache data statistics
10937c478bd9Sstevel@tonic-gate 	 */
10947c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETCACHESTAT;
10957c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
10967c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
10977c478bd9Sstevel@tonic-gate 	dptr = &u.data;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
11007c478bd9Sstevel@tonic-gate 		(void) printf(
11017c478bd9Sstevel@tonic-gate 		    gettext("\nCache data statistics not available!\n"));
11027c478bd9Sstevel@tonic-gate 		return (0);
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	/*
11067c478bd9Sstevel@tonic-gate 	 * print cache data statistics line by line
11077c478bd9Sstevel@tonic-gate 	 */
11087c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr cache data statistics:\n"));
11097c478bd9Sstevel@tonic-gate 	rbuf = dptr->ldap_ret.ldap_u.buff;
11107c478bd9Sstevel@tonic-gate 	sptr = strtok_r(rbuf, DOORLINESEP, &rest);
11117c478bd9Sstevel@tonic-gate 	for (;;) {
11127c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", sptr);
11137c478bd9Sstevel@tonic-gate 		sptr = strtok_r(NULL, DOORLINESEP, &rest);
11147c478bd9Sstevel@tonic-gate 		if (sptr == NULL)
11157c478bd9Sstevel@tonic-gate 			break;
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 	return (0);
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate  * detach from tty
11237c478bd9Sstevel@tonic-gate  */
11247c478bd9Sstevel@tonic-gate static void
11257c478bd9Sstevel@tonic-gate detachfromtty(char *pgm)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate 	int 	status;
11287c478bd9Sstevel@tonic-gate 	pid_t	pid, wret;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	(void) close(0);
11317c478bd9Sstevel@tonic-gate 	(void) close(1);
11327c478bd9Sstevel@tonic-gate 	/*
11337c478bd9Sstevel@tonic-gate 	 * Block the SIGUSR1 signal
11347c478bd9Sstevel@tonic-gate 	 * just in case that the child
11357c478bd9Sstevel@tonic-gate 	 * process may run faster than
11367c478bd9Sstevel@tonic-gate 	 * the parent process and
11377c478bd9Sstevel@tonic-gate 	 * send this signal before
11387c478bd9Sstevel@tonic-gate 	 * the signal handler is ready
11397c478bd9Sstevel@tonic-gate 	 * in the parent process.
11407c478bd9Sstevel@tonic-gate 	 * This error will cause the parent
11417c478bd9Sstevel@tonic-gate 	 * to exit with the User Signal 1
11427c478bd9Sstevel@tonic-gate 	 * exit code (144).
11437c478bd9Sstevel@tonic-gate 	 */
11447c478bd9Sstevel@tonic-gate 	(void) sighold(SIGUSR1);
11457c478bd9Sstevel@tonic-gate 	pid = fork1();
11467c478bd9Sstevel@tonic-gate 	switch (pid) {
11477c478bd9Sstevel@tonic-gate 		case (pid_t)-1:
11487c478bd9Sstevel@tonic-gate 			logit("detachfromtty(): fork1() call failed\n");
11497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11507c478bd9Sstevel@tonic-gate 			    gettext("%s: fork1() call failed.\n"),
11517c478bd9Sstevel@tonic-gate 			    pgm);
11527c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
11537c478bd9Sstevel@tonic-gate 			    gettext("ldap_cachemgr: fork1() call failed."));
11547c478bd9Sstevel@tonic-gate 			exit(1);
11557c478bd9Sstevel@tonic-gate 			break;
11567c478bd9Sstevel@tonic-gate 		case 0:
11577c478bd9Sstevel@tonic-gate 			/*
11587c478bd9Sstevel@tonic-gate 			 * child process does not
11597c478bd9Sstevel@tonic-gate 			 * need to worry about
11607c478bd9Sstevel@tonic-gate 			 * the SIGUSR1 signal
11617c478bd9Sstevel@tonic-gate 			 */
11627c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
11637c478bd9Sstevel@tonic-gate 			(void) close(2);
11647c478bd9Sstevel@tonic-gate 			break;
11657c478bd9Sstevel@tonic-gate 		default:
11667c478bd9Sstevel@tonic-gate 			/*
11677c478bd9Sstevel@tonic-gate 			 * Wait forever until the child process
11687c478bd9Sstevel@tonic-gate 			 * has exited, or has signalled that at
11697c478bd9Sstevel@tonic-gate 			 * least one server in the server list
11707c478bd9Sstevel@tonic-gate 			 * is up.
11717c478bd9Sstevel@tonic-gate 			 */
11727c478bd9Sstevel@tonic-gate 			if (signal(SIGUSR1, sig_ok_to_exit) == SIG_ERR) {
11737c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
11747c478bd9Sstevel@tonic-gate 				    "can't set up signal handler to "
11757c478bd9Sstevel@tonic-gate 				    " catch SIGUSR1.\n");
11767c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11777c478bd9Sstevel@tonic-gate 				    gettext("%s: signal() call failed.\n"),
11787c478bd9Sstevel@tonic-gate 				    pgm);
11797c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, gettext("ldap_cachemgr: "
11807c478bd9Sstevel@tonic-gate 				    "can't set up signal handler to "
11817c478bd9Sstevel@tonic-gate 				    " catch SIGUSR1."));
11827c478bd9Sstevel@tonic-gate 				exit(1);
11837c478bd9Sstevel@tonic-gate 			}
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 			/*
11867c478bd9Sstevel@tonic-gate 			 * now unblock the SIGUSR1 signal
11877c478bd9Sstevel@tonic-gate 			 * to handle the pending or
11887c478bd9Sstevel@tonic-gate 			 * soon to arrive SIGUSR1 signal
11897c478bd9Sstevel@tonic-gate 			 */
11907c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
11917c478bd9Sstevel@tonic-gate 			wret = waitpid(pid, &status, 0);
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 			if (wret == -1) {
11947c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
11957c478bd9Sstevel@tonic-gate 				    "waitpid() call failed\n");
11967c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11977c478bd9Sstevel@tonic-gate 				    gettext("%s: waitpid() call failed.\n"),
11987c478bd9Sstevel@tonic-gate 				    pgm);
11997c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
12007c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: waitpid() "
12017c478bd9Sstevel@tonic-gate 				    "call failed."));
12027c478bd9Sstevel@tonic-gate 				exit(1);
12037c478bd9Sstevel@tonic-gate 			}
12047c478bd9Sstevel@tonic-gate 			if (wret != pid) {
12057c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12067c478bd9Sstevel@tonic-gate 				    "waitpid() returned %ld when "
12077c478bd9Sstevel@tonic-gate 				    "child pid was %ld\n",
12087c478bd9Sstevel@tonic-gate 				    wret, pid);
12097c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12107c478bd9Sstevel@tonic-gate 				    gettext(
12117c478bd9Sstevel@tonic-gate 				    "%s: waitpid() returned %ld when "
12127c478bd9Sstevel@tonic-gate 				    "child pid was %ld.\n"),
12137c478bd9Sstevel@tonic-gate 				    pgm, wret, pid);
12147c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
12157c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: waitpid() "
12167c478bd9Sstevel@tonic-gate 				    "returned different "
12177c478bd9Sstevel@tonic-gate 				    "child pid."));
12187c478bd9Sstevel@tonic-gate 				exit(1);
12197c478bd9Sstevel@tonic-gate 			}
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 			/* evaluate return status */
12227c478bd9Sstevel@tonic-gate 			if (WIFEXITED(status)) {
12237c478bd9Sstevel@tonic-gate 				if (WEXITSTATUS(status) == 0) {
12247c478bd9Sstevel@tonic-gate 					exit(0);
12257c478bd9Sstevel@tonic-gate 				}
12267c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12277c478bd9Sstevel@tonic-gate 				    "child failed (rc = %d).\n",
12287c478bd9Sstevel@tonic-gate 				    WEXITSTATUS(status));
12297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12307c478bd9Sstevel@tonic-gate 				    gettext("%s: failed. Please see "
12317c478bd9Sstevel@tonic-gate 				    "syslog for details.\n"),
12327c478bd9Sstevel@tonic-gate 				    pgm);
12337c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
12347c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: failed "
12357c478bd9Sstevel@tonic-gate 				    "(rc = %d)."),
12367c478bd9Sstevel@tonic-gate 				    WEXITSTATUS(status));
12377c478bd9Sstevel@tonic-gate 			} else if (WIFSIGNALED(status)) {
12387c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12397c478bd9Sstevel@tonic-gate 				    "child terminated by signal %d.\n",
12407c478bd9Sstevel@tonic-gate 				    WTERMSIG(status));
12417c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12427c478bd9Sstevel@tonic-gate 				gettext("%s: terminated by signal %d.\n"),
12437c478bd9Sstevel@tonic-gate 				    pgm, WTERMSIG(status));
12447c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
12457c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: terminated by "
12467c478bd9Sstevel@tonic-gate 				    "signal %d.\n"),
12477c478bd9Sstevel@tonic-gate 				    WTERMSIG(status));
12487c478bd9Sstevel@tonic-gate 			} else if (WCOREDUMP(status)) {
12497c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): child core dumped.\n"),
12507c478bd9Sstevel@tonic-gate 				    (void) fprintf(stderr,
12517c478bd9Sstevel@tonic-gate 				    gettext("%s: core dumped.\n"),
12527c478bd9Sstevel@tonic-gate 				    pgm);
12537c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
12547c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: "
12557c478bd9Sstevel@tonic-gate 				    "core dumped.\n"));
12567c478bd9Sstevel@tonic-gate 			}
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 			exit(1);
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate 	(void) setsid();
12617c478bd9Sstevel@tonic-gate 	if (open("/dev/null", O_RDWR, 0) != -1) {
12627c478bd9Sstevel@tonic-gate 		(void) dup(0);
12637c478bd9Sstevel@tonic-gate 		(void) dup(0);
12647c478bd9Sstevel@tonic-gate 	}
12657c478bd9Sstevel@tonic-gate }
1266*8142c2b2Schinlong 
1267*8142c2b2Schinlong /*
1268*8142c2b2Schinlong  * Check if the door client's euid is 0
1269*8142c2b2Schinlong  *
1270*8142c2b2Schinlong  * We could check for some privilege or re-design the interfaces that
1271*8142c2b2Schinlong  * lead to is_root() being called so that we rely on SMF and RBAC, but
1272*8142c2b2Schinlong  * we need this check only for dealing with undocumented-but-possibly-
1273*8142c2b2Schinlong  * used interfaces.  Anything beyond checking for euid == 0 here would
1274*8142c2b2Schinlong  * be overkill considering that those are undocumented interfaces.
1275*8142c2b2Schinlong  *
1276*8142c2b2Schinlong  * If free_uc is 0, the caller is responsible for freeing *ucp.
1277*8142c2b2Schinlong  *
1278*8142c2b2Schinlong  * return - 0 euid != 0
1279*8142c2b2Schinlong  *          1 euid == 0
1280*8142c2b2Schinlong  */
1281*8142c2b2Schinlong static int
1282*8142c2b2Schinlong is_root(int free_uc, char *dc_str, ucred_t **ucp)
1283*8142c2b2Schinlong {
1284*8142c2b2Schinlong 	int	rc;
1285*8142c2b2Schinlong 
1286*8142c2b2Schinlong 	if (door_ucred(ucp) != 0) {
1287*8142c2b2Schinlong 		rc = errno;
1288*8142c2b2Schinlong 		logit("door_ucred() call failed %s\n", strerror(rc));
1289*8142c2b2Schinlong 		syslog(LOG_ERR, gettext("ldap_cachemgr: door_ucred() call %s "
1290*8142c2b2Schinlong 		    "failed %s"), strerror(rc));
1291*8142c2b2Schinlong 		return (0);
1292*8142c2b2Schinlong 	}
1293*8142c2b2Schinlong 
1294*8142c2b2Schinlong 
1295*8142c2b2Schinlong 	if (ucred_geteuid(*ucp) != 0) {
1296*8142c2b2Schinlong 
1297*8142c2b2Schinlong 		if (current_admin.debug_level >= DBG_CANT_FIND)
1298*8142c2b2Schinlong 			logit("%s call failed(cred): caller pid %ld, uid %u, "
1299*8142c2b2Schinlong 			    "euid %u\n", dc_str, ucred_getpid(*ucp),
1300*8142c2b2Schinlong 			    ucred_getruid(*ucp), ucred_geteuid(*ucp));
1301*8142c2b2Schinlong 
1302*8142c2b2Schinlong 		rc = 0;
1303*8142c2b2Schinlong 	} else {
1304*8142c2b2Schinlong 
1305*8142c2b2Schinlong 		if (current_admin.debug_level >= DBG_ALL)
1306*8142c2b2Schinlong 			logit("ldap_cachemgr received %s call from pid %ld, "
1307*8142c2b2Schinlong 			    "uid %u, euid %u\n", dc_str, ucred_getpid(*ucp),
1308*8142c2b2Schinlong 			    ucred_getruid(*ucp), ucred_geteuid(*ucp));
1309*8142c2b2Schinlong 		rc = 1;
1310*8142c2b2Schinlong 	}
1311*8142c2b2Schinlong 
1312*8142c2b2Schinlong 	if (free_uc)
1313*8142c2b2Schinlong 		ucred_free(*ucp);
1314*8142c2b2Schinlong 
1315*8142c2b2Schinlong 	return (rc);
1316*8142c2b2Schinlong }
1317*8142c2b2Schinlong 
1318*8142c2b2Schinlong /*
1319*8142c2b2Schinlong  * Check if pid is nscd
1320*8142c2b2Schinlong  *
1321*8142c2b2Schinlong  * Input: pid - process id of the door client that calls ldap_cachemgr
1322*8142c2b2Schinlong  *
1323*8142c2b2Schinlong  * Return: 0 - No
1324*8142c2b2Schinlong  *         1 - Yes
1325*8142c2b2Schinlong  */
1326*8142c2b2Schinlong 
1327*8142c2b2Schinlong static int
1328*8142c2b2Schinlong is_nscd(pid_t pid)
1329*8142c2b2Schinlong 
1330*8142c2b2Schinlong {
1331*8142c2b2Schinlong 	static mutex_t	_door_lock = DEFAULTMUTEX;
1332*8142c2b2Schinlong 	static	int	doorfd = -1;
1333*8142c2b2Schinlong 	int		match;
1334*8142c2b2Schinlong 	door_info_t 	my_door;
1335*8142c2b2Schinlong 
1336*8142c2b2Schinlong 	/*
1337*8142c2b2Schinlong 	 * the first time in we try and open and validate the door.
1338*8142c2b2Schinlong 	 * the validations are that the door must have been
1339*8142c2b2Schinlong 	 * created with the door cookie and
1340*8142c2b2Schinlong 	 * that the file attached to the door is owned by root
1341*8142c2b2Schinlong 	 * and readonly by user, group and other.  If any of these
1342*8142c2b2Schinlong 	 * validations fail we refuse to use the door.
1343*8142c2b2Schinlong 	 */
1344*8142c2b2Schinlong 
1345*8142c2b2Schinlong 	(void) mutex_lock(&_door_lock);
1346*8142c2b2Schinlong 
1347*8142c2b2Schinlong try_again:
1348*8142c2b2Schinlong 
1349*8142c2b2Schinlong 	if (doorfd == -1) {
1350*8142c2b2Schinlong 
1351*8142c2b2Schinlong 		if ((doorfd = open(NAME_SERVICE_DOOR, O_RDONLY, 0))
1352*8142c2b2Schinlong 		    == -1) {
1353*8142c2b2Schinlong 			(void) mutex_unlock(&_door_lock);
1354*8142c2b2Schinlong 			return (0);
1355*8142c2b2Schinlong 		}
1356*8142c2b2Schinlong 
1357*8142c2b2Schinlong 		if (door_info(doorfd, &my_door) == -1 ||
1358*8142c2b2Schinlong 		    (my_door.di_attributes & DOOR_REVOKED) ||
1359*8142c2b2Schinlong 		    my_door.di_data != (uintptr_t)NAME_SERVICE_DOOR_COOKIE) {
1360*8142c2b2Schinlong 			/*
1361*8142c2b2Schinlong 			 * we should close doorfd because we just opened it
1362*8142c2b2Schinlong 			 */
1363*8142c2b2Schinlong 			(void) close(doorfd);
1364*8142c2b2Schinlong 			doorfd = -1;
1365*8142c2b2Schinlong 			(void) mutex_unlock(&_door_lock);
1366*8142c2b2Schinlong 			return (0);
1367*8142c2b2Schinlong 		}
1368*8142c2b2Schinlong 	} else {
1369*8142c2b2Schinlong 		/*
1370*8142c2b2Schinlong 		 * doorfd is cached. Double check just in case
1371*8142c2b2Schinlong 		 * the door server is restarted or is down.
1372*8142c2b2Schinlong 		 */
1373*8142c2b2Schinlong 		if (door_info(doorfd, &my_door) == -1 ||
1374*8142c2b2Schinlong 		    my_door.di_data != (uintptr_t)NAME_SERVICE_DOOR_COOKIE) {
1375*8142c2b2Schinlong 			/*
1376*8142c2b2Schinlong 			 * don't close it -
1377*8142c2b2Schinlong 			 * someone else has clobbered fd
1378*8142c2b2Schinlong 			 */
1379*8142c2b2Schinlong 			doorfd = -1;
1380*8142c2b2Schinlong 			goto try_again;
1381*8142c2b2Schinlong 		}
1382*8142c2b2Schinlong 
1383*8142c2b2Schinlong 		if (my_door.di_attributes & DOOR_REVOKED) {
1384*8142c2b2Schinlong 			(void) close(doorfd);
1385*8142c2b2Schinlong 			doorfd = -1;	/* try and restart connection */
1386*8142c2b2Schinlong 			goto try_again;
1387*8142c2b2Schinlong 		}
1388*8142c2b2Schinlong 	}
1389*8142c2b2Schinlong 
1390*8142c2b2Schinlong 	/*
1391*8142c2b2Schinlong 	 * door descriptor exists and is valid
1392*8142c2b2Schinlong 	 */
1393*8142c2b2Schinlong 	if (pid == my_door.di_target)
1394*8142c2b2Schinlong 		match = 1;
1395*8142c2b2Schinlong 	else
1396*8142c2b2Schinlong 		match = 0;
1397*8142c2b2Schinlong 
1398*8142c2b2Schinlong 	(void) mutex_unlock(&_door_lock);
1399*8142c2b2Schinlong 
1400*8142c2b2Schinlong 	return (match);
1401*8142c2b2Schinlong 
1402*8142c2b2Schinlong }
1403