xref: /titanic_54/usr/src/cmd/ldapcachemgr/cachemgr.c (revision e1dd0a2f3a26050d1f183c1cafae42c4e3a0b57e)
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*e1dd0a2fSth160488  * Copyright 2008 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 
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);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #ifdef SLP
777c478bd9Sstevel@tonic-gate int			use_slp = 0;
787c478bd9Sstevel@tonic-gate static unsigned int	refresh = 10800;	/* dynamic discovery interval */
797c478bd9Sstevel@tonic-gate #endif /* SLP */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static ldap_stat_t *
827c478bd9Sstevel@tonic-gate getcacheptr(char *s)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	static const char *caches[1] = {"ldap"};
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (strncmp(caches[0], s, strlen(caches[0])) == 0)
877c478bd9Sstevel@tonic-gate 		return (&current_admin.ldap_stat);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	return (NULL);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate char *
937c478bd9Sstevel@tonic-gate getcacheopt(char *s)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	while (*s && *s != ',')
967c478bd9Sstevel@tonic-gate 		s++;
977c478bd9Sstevel@tonic-gate 	return ((*s == ',') ? (s + 1) : NULL);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  *  This is here to prevent the ldap_cachemgr becomes
1027c478bd9Sstevel@tonic-gate  *  daemonlized to early to soon during boot time.
1037c478bd9Sstevel@tonic-gate  *  This causes problems during boot when automounter
1047c478bd9Sstevel@tonic-gate  *  and others try to use libsldap before ldap_cachemgr
1057c478bd9Sstevel@tonic-gate  *  finishes walking the server list.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate static void
1087c478bd9Sstevel@tonic-gate sig_ok_to_exit(int signo)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	if (signo == SIGUSR1) {
1117c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): parent exiting...\n");
1127c478bd9Sstevel@tonic-gate 		exit(0);
1137c478bd9Sstevel@tonic-gate 	} else {
1147c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): invalid signal(%d) received.\n",
1157c478bd9Sstevel@tonic-gate 		    signo);
1167c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: "
1177c478bd9Sstevel@tonic-gate 		    "invalid signal(%d) received."), signo);
1187c478bd9Sstevel@tonic-gate 		exit(1);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate #define	LDAP_TABLES		1	/* ldap */
1227c478bd9Sstevel@tonic-gate #define	TABLE_THREADS		10
1237c478bd9Sstevel@tonic-gate #define	COMMON_THREADS		20
1247c478bd9Sstevel@tonic-gate #define	CACHE_MISS_THREADS	(COMMON_THREADS + LDAP_TABLES * TABLE_THREADS)
1257c478bd9Sstevel@tonic-gate #define	CACHE_HIT_THREADS	20
126*e1dd0a2fSth160488 /*
127*e1dd0a2fSth160488  * There is only one thread handling GETSTATUSCHANGE START from main nscd
128*e1dd0a2fSth160488  * most of time. But it could happen that a main nscd is restarted, old main
129*e1dd0a2fSth160488  * nscd's handling thread is still alive when new main nscd starts and sends
130*e1dd0a2fSth160488  * START, or old main dies. STOP is not sent in both cases.
131*e1dd0a2fSth160488  * The main nscd requires 2 threads to handle START and STOP. So max number
132*e1dd0a2fSth160488  * of change threads is set to 4.
133*e1dd0a2fSth160488  */
134*e1dd0a2fSth160488 #define	MAX_CHG_THREADS		4
135*e1dd0a2fSth160488 #define	MAX_SERVER_THREADS	(CACHE_HIT_THREADS + CACHE_MISS_THREADS + \
136*e1dd0a2fSth160488 				MAX_CHG_THREADS)
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static sema_t common_sema;
1397c478bd9Sstevel@tonic-gate static sema_t ldap_sema;
1407c478bd9Sstevel@tonic-gate static thread_key_t lookup_state_key;
141*e1dd0a2fSth160488 static int chg_threads_num = 0;
142*e1dd0a2fSth160488 static mutex_t chg_threads_num_lock = DEFAULTMUTEX;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static void
1457c478bd9Sstevel@tonic-gate initialize_lookup_clearance()
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	(void) thr_keycreate(&lookup_state_key, NULL);
1487c478bd9Sstevel@tonic-gate 	(void) sema_init(&common_sema, COMMON_THREADS, USYNC_THREAD, 0);
1497c478bd9Sstevel@tonic-gate 	(void) sema_init(&ldap_sema, TABLE_THREADS, USYNC_THREAD, 0);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate int
1537c478bd9Sstevel@tonic-gate get_clearance(int callnumber)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
1567c478bd9Sstevel@tonic-gate 	char	*tab;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (sema_trywait(&common_sema) == 0) {
1597c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, NULL);
1607c478bd9Sstevel@tonic-gate 		return (0);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	switch (callnumber) {
1647c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
1657c478bd9Sstevel@tonic-gate 			tab = "ldap";
1667c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
1677c478bd9Sstevel@tonic-gate 			break;
1687c478bd9Sstevel@tonic-gate 		default:
1697c478bd9Sstevel@tonic-gate 			logit("Internal Error: get_clearance\n");
1707c478bd9Sstevel@tonic-gate 			break;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (sema_trywait(table_sema) == 0) {
1747c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, (void*)1);
1757c478bd9Sstevel@tonic-gate 		return (0);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_CANT_FIND) {
1797c478bd9Sstevel@tonic-gate 		logit("get_clearance: throttling load for %s table\n", tab);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	return (-1);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate int
1867c478bd9Sstevel@tonic-gate release_clearance(int callnumber)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	int	which;
1897c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	(void) thr_getspecific(lookup_state_key, (void**)&which);
1927c478bd9Sstevel@tonic-gate 	if (which == 0) /* from common pool */ {
1937c478bd9Sstevel@tonic-gate 		(void) sema_post(&common_sema);
1947c478bd9Sstevel@tonic-gate 		return (0);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	switch (callnumber) {
1987c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
1997c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
2007c478bd9Sstevel@tonic-gate 			break;
2017c478bd9Sstevel@tonic-gate 		default:
2027c478bd9Sstevel@tonic-gate 			logit("Internal Error: release_clearance\n");
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	(void) sema_post(table_sema);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	return (0);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static mutex_t		create_lock;
2127c478bd9Sstevel@tonic-gate static int		num_servers = 0;
2137c478bd9Sstevel@tonic-gate static thread_key_t	server_key;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * Bind a TSD value to a server thread. This enables the destructor to
2187c478bd9Sstevel@tonic-gate  * be called if/when this thread exits.  This would be a programming error,
2197c478bd9Sstevel@tonic-gate  * but better safe than sorry.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2237c478bd9Sstevel@tonic-gate static void *
2247c478bd9Sstevel@tonic-gate server_tsd_bind(void *arg)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	static void	*value = 0;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * disable cancellation to prevent hangs when server
2307c478bd9Sstevel@tonic-gate 	 * threads disappear
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	(void) thr_setspecific(server_key, value);
234cb5caa98Sdjl 	(void) door_return(NULL, 0, NULL, 0);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	return (value);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  * Server threads are created here.
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2447c478bd9Sstevel@tonic-gate static void
2457c478bd9Sstevel@tonic-gate server_create(door_info_t *dip)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
2487c478bd9Sstevel@tonic-gate 	if (++num_servers > MAX_SERVER_THREADS) {
2497c478bd9Sstevel@tonic-gate 		num_servers--;
2507c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&create_lock);
2517c478bd9Sstevel@tonic-gate 		return;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
2547c478bd9Sstevel@tonic-gate 	(void) thr_create(NULL, 0, server_tsd_bind, NULL,
2557c478bd9Sstevel@tonic-gate 	    THR_BOUND|THR_DETACHED, NULL);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Server thread are destroyed here
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2637c478bd9Sstevel@tonic-gate static void
2647c478bd9Sstevel@tonic-gate server_destroy(void *arg)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
2677c478bd9Sstevel@tonic-gate 	num_servers--;
2687c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
271a506a34cSth160488 int
2727c478bd9Sstevel@tonic-gate main(int argc, char ** argv)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	int			did;
2757c478bd9Sstevel@tonic-gate 	int			opt;
2767c478bd9Sstevel@tonic-gate 	int			errflg = 0;
2777c478bd9Sstevel@tonic-gate 	int			showstats = 0;
2787c478bd9Sstevel@tonic-gate 	int			doset = 0;
2797c478bd9Sstevel@tonic-gate 	int			dofg = 0;
2807c478bd9Sstevel@tonic-gate 	struct stat		buf;
2817c478bd9Sstevel@tonic-gate 	sigset_t		myset;
2827c478bd9Sstevel@tonic-gate 	struct sigaction	sighupaction;
2837c478bd9Sstevel@tonic-gate 	static void		client_killserver();
2847c478bd9Sstevel@tonic-gate 	int			debug_level = 0;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/* setup for localization */
2877c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2887c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	openlog("ldap_cachemgr", LOG_PID, LOG_DAEMON);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if (chdir(NSLDAPDIRECTORY) < 0) {
2937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("chdir(\"%s\") failed: %s\n"),
2947c478bd9Sstevel@tonic-gate 		    NSLDAPDIRECTORY, strerror(errno));
2957c478bd9Sstevel@tonic-gate 		exit(1);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/*
2997c478bd9Sstevel@tonic-gate 	 * Correctly set file mode creation mask, so to make the new files
3007c478bd9Sstevel@tonic-gate 	 * created for door calls being readable by all.
3017c478bd9Sstevel@tonic-gate 	 */
3027c478bd9Sstevel@tonic-gate 	(void) umask(0);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 *  Special case non-root user here -	he/she/they/it can just print
3067c478bd9Sstevel@tonic-gate 	 *					stats
3077c478bd9Sstevel@tonic-gate 	 */
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (geteuid()) {
3107c478bd9Sstevel@tonic-gate 		if (argc != 2 || strcmp(argv[1], "-g")) {
3117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3127c478bd9Sstevel@tonic-gate 			    gettext("Must be root to use any option "
3137c478bd9Sstevel@tonic-gate 			    "other than -g.\n\n"));
3147c478bd9Sstevel@tonic-gate 			usage(argv[0]);
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 
317*e1dd0a2fSth160488 		if ((__ns_ldap_cache_ping() != NS_CACHE_SUCCESS) ||
3187c478bd9Sstevel@tonic-gate 		    (client_getadmin(&current_admin) != 0)) {
3197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3207c478bd9Sstevel@tonic-gate 			    gettext("%s doesn't appear to be running.\n"),
3217c478bd9Sstevel@tonic-gate 			    argv[0]);
3227c478bd9Sstevel@tonic-gate 			exit(1);
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		(void) client_showstats(&current_admin);
3257c478bd9Sstevel@tonic-gate 		exit(0);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 *  Determine if there is already a daemon running
3327c478bd9Sstevel@tonic-gate 	 */
3337c478bd9Sstevel@tonic-gate 
334*e1dd0a2fSth160488 	will_become_server = (__ns_ldap_cache_ping() != NS_CACHE_SUCCESS);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 *  load normal config file
3387c478bd9Sstevel@tonic-gate 	 */
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if (will_become_server) {
3417c478bd9Sstevel@tonic-gate 		static const ldap_stat_t defaults = {
3427c478bd9Sstevel@tonic-gate 			0,		/* stat */
3437c478bd9Sstevel@tonic-gate 			DEFAULTTTL};	/* ttl */
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat = defaults;
3467c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, LOGFILE);
3477c478bd9Sstevel@tonic-gate 	} else {
3487c478bd9Sstevel@tonic-gate 		if (client_getadmin(&current_admin)) {
3497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Cannot contact %s "
3507c478bd9Sstevel@tonic-gate 			    "properly(?)\n"), argv[0]);
3517c478bd9Sstevel@tonic-gate 			exit(1);
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate #ifndef SLP
3567c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgl:r:d:")) != EOF) {
3577c478bd9Sstevel@tonic-gate #else
3587c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgs:l:r:d:")) != EOF) {
3597c478bd9Sstevel@tonic-gate #endif /* SLP */
3607c478bd9Sstevel@tonic-gate 		ldap_stat_t	*cache;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		switch (opt) {
3637c478bd9Sstevel@tonic-gate 		case 'K':
3647c478bd9Sstevel@tonic-gate 			client_killserver();
3657c478bd9Sstevel@tonic-gate 			exit(0);
3667c478bd9Sstevel@tonic-gate 			break;
3677c478bd9Sstevel@tonic-gate 		case 'g':
3687c478bd9Sstevel@tonic-gate 			showstats++;
3697c478bd9Sstevel@tonic-gate 			break;
3707c478bd9Sstevel@tonic-gate 		case 'f':
3717c478bd9Sstevel@tonic-gate 			dofg++;
3727c478bd9Sstevel@tonic-gate 			break;
3737c478bd9Sstevel@tonic-gate 		case 'r':
3747c478bd9Sstevel@tonic-gate 			doset++;
3757c478bd9Sstevel@tonic-gate 			cache = getcacheptr("ldap");
3767c478bd9Sstevel@tonic-gate 			if (!optarg) {
3777c478bd9Sstevel@tonic-gate 				errflg++;
3787c478bd9Sstevel@tonic-gate 				break;
3797c478bd9Sstevel@tonic-gate 			}
3807c478bd9Sstevel@tonic-gate 			cache->ldap_ttl = atoi(optarg);
3817c478bd9Sstevel@tonic-gate 			break;
3827c478bd9Sstevel@tonic-gate 		case 'l':
3837c478bd9Sstevel@tonic-gate 			doset++;
3847c478bd9Sstevel@tonic-gate 			(void) strlcpy(current_admin.logfile,
3857c478bd9Sstevel@tonic-gate 			    optarg, sizeof (current_admin.logfile));
3867c478bd9Sstevel@tonic-gate 			break;
3877c478bd9Sstevel@tonic-gate 		case 'd':
3887c478bd9Sstevel@tonic-gate 			doset++;
3897c478bd9Sstevel@tonic-gate 			debug_level = atoi(optarg);
3907c478bd9Sstevel@tonic-gate 			break;
3917c478bd9Sstevel@tonic-gate #ifdef SLP
3927c478bd9Sstevel@tonic-gate 		case 's':	/* undocumented: use dynamic (SLP) config */
3937c478bd9Sstevel@tonic-gate 			use_slp = 1;
3947c478bd9Sstevel@tonic-gate 			break;
3957c478bd9Sstevel@tonic-gate #endif /* SLP */
3967c478bd9Sstevel@tonic-gate 		default:
3977c478bd9Sstevel@tonic-gate 			errflg++;
3987c478bd9Sstevel@tonic-gate 			break;
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (errflg)
4037c478bd9Sstevel@tonic-gate 		usage(argv[0]);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * will not show statistics if no daemon running
4077c478bd9Sstevel@tonic-gate 	 */
4087c478bd9Sstevel@tonic-gate 	if (will_become_server && showstats) {
4097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4107c478bd9Sstevel@tonic-gate 		    gettext("%s doesn't appear to be running.\n"),
4117c478bd9Sstevel@tonic-gate 		    argv[0]);
4127c478bd9Sstevel@tonic-gate 		exit(1);
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (!will_become_server) {
4167c478bd9Sstevel@tonic-gate 		if (showstats) {
4177c478bd9Sstevel@tonic-gate 			(void) client_showstats(&current_admin);
4187c478bd9Sstevel@tonic-gate 		}
4197c478bd9Sstevel@tonic-gate 		if (doset) {
4207c478bd9Sstevel@tonic-gate 			current_admin.debug_level = debug_level;
4217c478bd9Sstevel@tonic-gate 			if (client_setadmin(&current_admin) < 0) {
4227c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4237c478bd9Sstevel@tonic-gate 				    gettext("Error during admin call\n"));
4247c478bd9Sstevel@tonic-gate 				exit(1);
4257c478bd9Sstevel@tonic-gate 			}
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 		if (!showstats && !doset) {
4287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4297c478bd9Sstevel@tonic-gate 			gettext("%s already running....use '%s "
4307c478bd9Sstevel@tonic-gate 			    "-K' to stop\n"), argv[0], argv[0]);
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 		exit(0);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/*
4367c478bd9Sstevel@tonic-gate 	 *   daemon from here on
4377c478bd9Sstevel@tonic-gate 	 */
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (debug_level) {
4407c478bd9Sstevel@tonic-gate 		/*
4417c478bd9Sstevel@tonic-gate 		 * we're debugging...
4427c478bd9Sstevel@tonic-gate 		 */
4437c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
4447c478bd9Sstevel@tonic-gate 			/*
4457c478bd9Sstevel@tonic-gate 			 * no specified log file
4467c478bd9Sstevel@tonic-gate 			 */
4477c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, LOGFILE);
4487c478bd9Sstevel@tonic-gate 		else
4497c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
4507c478bd9Sstevel@tonic-gate 			    current_admin.logfile);
4517c478bd9Sstevel@tonic-gate 		/*
4527c478bd9Sstevel@tonic-gate 		 * validate the range of debug level number
4537c478bd9Sstevel@tonic-gate 		 * and set the number to current_admin.debug_level
4547c478bd9Sstevel@tonic-gate 		 */
4557c478bd9Sstevel@tonic-gate 		if (cachemgr_set_dl(&current_admin, debug_level) < 0) {
4567c478bd9Sstevel@tonic-gate 				/*
4577c478bd9Sstevel@tonic-gate 				 * print error messages to the screen
4587c478bd9Sstevel@tonic-gate 				 * cachemgr_set_dl prints msgs to cachemgr.log
4597c478bd9Sstevel@tonic-gate 				 * only
4607c478bd9Sstevel@tonic-gate 				 */
4617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4627c478bd9Sstevel@tonic-gate 				gettext("Incorrect Debug Level: %d\n"
4637c478bd9Sstevel@tonic-gate 				"It should be between %d and %d\n"),
4647c478bd9Sstevel@tonic-gate 				    debug_level, DBG_OFF, MAXDEBUG);
4657c478bd9Sstevel@tonic-gate 			exit(-1);
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 	} else {
4687c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
4697c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, "/dev/null");
4707c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
4717c478bd9Sstevel@tonic-gate 			    current_admin.logfile);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (dofg == 0)
4757c478bd9Sstevel@tonic-gate 		detachfromtty(argv[0]);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * perform some initialization
4797c478bd9Sstevel@tonic-gate 	 */
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	initialize_lookup_clearance();
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (getldap_init() != 0)
4847c478bd9Sstevel@tonic-gate 		exit(-1);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	/*
4877c478bd9Sstevel@tonic-gate 	 * Establish our own server thread pool
4887c478bd9Sstevel@tonic-gate 	 */
4897c478bd9Sstevel@tonic-gate 
490cb5caa98Sdjl 	(void) door_server_create(server_create);
4917c478bd9Sstevel@tonic-gate 	if (thr_keycreate(&server_key, server_destroy) != 0) {
4927c478bd9Sstevel@tonic-gate 		logit("thr_keycreate() call failed\n");
4937c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4947c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: thr_keycreate() call failed"));
4957c478bd9Sstevel@tonic-gate 		perror("thr_keycreate");
4967c478bd9Sstevel@tonic-gate 		exit(-1);
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	/*
5007c478bd9Sstevel@tonic-gate 	 * Create a door
5017c478bd9Sstevel@tonic-gate 	 */
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	if ((did = door_create(switcher, LDAP_CACHE_DOOR_COOKIE,
5047c478bd9Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
5057c478bd9Sstevel@tonic-gate 		logit("door_create() call failed\n");
5067c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
5077c478bd9Sstevel@tonic-gate 		    "ldap_cachemgr: door_create() call failed"));
5087c478bd9Sstevel@tonic-gate 		perror("door_create");
5097c478bd9Sstevel@tonic-gate 		exit(-1);
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * bind to file system
5147c478bd9Sstevel@tonic-gate 	 */
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	if (stat(LDAP_CACHE_DOOR, &buf) < 0) {
5177c478bd9Sstevel@tonic-gate 		int	newfd;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		if ((newfd = creat(LDAP_CACHE_DOOR, 0444)) < 0) {
5207c478bd9Sstevel@tonic-gate 			logit("Cannot create %s:%s\n",
5217c478bd9Sstevel@tonic-gate 			    LDAP_CACHE_DOOR,
5227c478bd9Sstevel@tonic-gate 			    strerror(errno));
5237c478bd9Sstevel@tonic-gate 			exit(1);
5247c478bd9Sstevel@tonic-gate 		}
5257c478bd9Sstevel@tonic-gate 		(void) close(newfd);
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	if (fattach(did, LDAP_CACHE_DOOR) < 0) {
5297c478bd9Sstevel@tonic-gate 		if ((errno != EBUSY) ||
5307c478bd9Sstevel@tonic-gate 		    (fdetach(LDAP_CACHE_DOOR) <  0) ||
5317c478bd9Sstevel@tonic-gate 		    (fattach(did, LDAP_CACHE_DOOR) < 0)) {
5327c478bd9Sstevel@tonic-gate 			logit("fattach() call failed\n");
5337c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
5347c478bd9Sstevel@tonic-gate 			    "ldap_cachemgr: fattach() call failed"));
5357c478bd9Sstevel@tonic-gate 			perror("fattach");
5367c478bd9Sstevel@tonic-gate 			exit(2);
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/* catch SIGHUP revalid signals */
5417c478bd9Sstevel@tonic-gate 	sighupaction.sa_handler = getldap_revalidate;
5427c478bd9Sstevel@tonic-gate 	sighupaction.sa_flags = 0;
5437c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sighupaction.sa_mask);
5447c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&myset);
5457c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGHUP);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (sigaction(SIGHUP, &sighupaction, NULL) < 0) {
5487c478bd9Sstevel@tonic-gate 		logit("sigaction() call failed\n");
5497c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5507c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: sigaction() call failed"));
5517c478bd9Sstevel@tonic-gate 		perror("sigaction");
5527c478bd9Sstevel@tonic-gate 		exit(1);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_BLOCK, &myset, NULL) < 0) {
5567c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
5577c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5587c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: thr_sigsetmask() call failed"));
5597c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
5607c478bd9Sstevel@tonic-gate 		exit(1);
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/*
5647c478bd9Sstevel@tonic-gate 	 *  kick off revalidate threads only if ttl != 0
5657c478bd9Sstevel@tonic-gate 	 */
5667c478bd9Sstevel@tonic-gate 
567*e1dd0a2fSth160488 	if (thr_create(NULL, 0, (void *(*)(void*))getldap_refresh,
568*e1dd0a2fSth160488 	    NULL, 0, NULL) != 0) {
5697c478bd9Sstevel@tonic-gate 		logit("thr_create() call failed\n");
5707c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5717c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: thr_create() call failed"));
5727c478bd9Sstevel@tonic-gate 		perror("thr_create");
5737c478bd9Sstevel@tonic-gate 		exit(1);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/*
5777c478bd9Sstevel@tonic-gate 	 *  kick off the thread which refreshes the server info
5787c478bd9Sstevel@tonic-gate 	 */
5797c478bd9Sstevel@tonic-gate 
580*e1dd0a2fSth160488 	if (thr_create(NULL, 0, (void *(*)(void*))getldap_serverInfo_refresh,
581*e1dd0a2fSth160488 	    NULL, 0, NULL) != 0) {
5827c478bd9Sstevel@tonic-gate 		logit("thr_create() call failed\n");
5837c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5847c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: thr_create() call failed"));
5857c478bd9Sstevel@tonic-gate 		perror("thr_create");
5867c478bd9Sstevel@tonic-gate 		exit(1);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
589*e1dd0a2fSth160488 	/*
590*e1dd0a2fSth160488 	 * kick off the thread which cleans up waiting threads for
591*e1dd0a2fSth160488 	 * GETSTATUSCHANGE
592*e1dd0a2fSth160488 	 */
593*e1dd0a2fSth160488 
594*e1dd0a2fSth160488 	if (thr_create(NULL, 0, chg_cleanup_waiting_threads,
595*e1dd0a2fSth160488 	    NULL, 0, NULL) != 0) {
596*e1dd0a2fSth160488 		logit("thr_create() chg_cleanup_waiting_threads call failed\n");
597*e1dd0a2fSth160488 		syslog(LOG_ERR,
598*e1dd0a2fSth160488 		    gettext("ldap_cachemgr: thr_create() "
599*e1dd0a2fSth160488 		    "chg_cleanup_waiting_threads call failed"));
600*e1dd0a2fSth160488 		exit(1);
601*e1dd0a2fSth160488 	}
602*e1dd0a2fSth160488 
6037c478bd9Sstevel@tonic-gate #ifdef SLP
6047c478bd9Sstevel@tonic-gate 	if (use_slp) {
6057c478bd9Sstevel@tonic-gate 		/* kick off SLP discovery thread */
606*e1dd0a2fSth160488 		if (thr_create(NULL, 0, (void *(*)(void *))discover,
6077c478bd9Sstevel@tonic-gate 		    (void *)&refresh, 0, NULL) != 0) {
6087c478bd9Sstevel@tonic-gate 			logit("thr_create() call failed\n");
6097c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("ldap_cachemgr: thr_create() "
6107c478bd9Sstevel@tonic-gate 			    "call failed"));
6117c478bd9Sstevel@tonic-gate 			perror("thr_create");
6127c478bd9Sstevel@tonic-gate 			exit(1);
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate #endif /* SLP */
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_UNBLOCK, &myset, NULL) < 0) {
6187c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
6197c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
6207c478bd9Sstevel@tonic-gate 		    gettext("ldap_cachemgr: the_sigsetmask() call failed"));
6217c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
6227c478bd9Sstevel@tonic-gate 		exit(1);
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
6267c478bd9Sstevel@tonic-gate 	while (1) {
6277c478bd9Sstevel@tonic-gate 		(void) pause();
6287c478bd9Sstevel@tonic-gate 	}
629cb5caa98Sdjl 	/* NOTREACHED */
630cb5caa98Sdjl 	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 
6347ddae043Siz202018 /*
6357ddae043Siz202018  * Before calling the alloca() function we have to be sure that we won't get
6367ddae043Siz202018  * beyond the stack. Since we don't know the precise layout of the stack,
6377ddae043Siz202018  * the address of an automatic of the function gives us a rough idea, plus/minus
6387ddae043Siz202018  * a bit. We also need a bit more of stackspace after the call to be able
6397ddae043Siz202018  * to call further functions. Even something as simple as making a system call
6407ddae043Siz202018  * from within this function can take ~100 Bytes of stackspace.
6417ddae043Siz202018  */
6427ddae043Siz202018 #define	SAFETY_BUFFER 32 * 1024 /* 32KB */
6437ddae043Siz202018 
6447ddae043Siz202018 static
6457ddae043Siz202018 size_t
6467ddae043Siz202018 get_data_size(LineBuf *config_info, int *err_code)
6477ddae043Siz202018 {
6487ddae043Siz202018 	size_t		configSize = sizeof (ldap_return_t);
6497ddae043Siz202018 	dataunion	*buf = NULL; /* For the 'sizeof' purpose */
6507ddae043Siz202018 
6517ddae043Siz202018 	if (config_info->str != NULL &&
6527ddae043Siz202018 	    config_info->len >= sizeof (buf->data.ldap_ret.ldap_u.config)) {
6537ddae043Siz202018 		configSize = sizeof (buf->space) +
6547ddae043Siz202018 		    config_info->len -
6557ddae043Siz202018 		    sizeof (buf->data.ldap_ret.ldap_u.config);
6567ddae043Siz202018 
6577ddae043Siz202018 		if (!stack_inbounds((char *)&buf -
6587ddae043Siz202018 		    (configSize + SAFETY_BUFFER))) {
6597ddae043Siz202018 			/*
6607ddae043Siz202018 			 * We do not have enough space on the stack
6617ddae043Siz202018 			 * to accomodate the whole DUAProfile
6627ddae043Siz202018 			 */
6637ddae043Siz202018 			logit("The DUAProfile is too big. There is not enough "
6647ddae043Siz202018 			    "space to process it. Ignoring it.\n");
6657ddae043Siz202018 			syslog(LOG_ERR, gettext("ldap_cachemgr: The DUAProfile "
6667ddae043Siz202018 			    "is too big. There is not enough space "
6677ddae043Siz202018 			    "to process it. Ignoring it."));
6687ddae043Siz202018 
669*e1dd0a2fSth160488 			*err_code = NS_CACHE_SERVERERROR;
6707ddae043Siz202018 
6717ddae043Siz202018 			free(config_info->str);
6727ddae043Siz202018 			config_info->str = NULL;
6737ddae043Siz202018 			config_info->len = 0;
6747ddae043Siz202018 			configSize = sizeof (ldap_return_t);
6757ddae043Siz202018 		}
6767ddae043Siz202018 	}
6777ddae043Siz202018 
6787ddae043Siz202018 	return (configSize);
6797ddae043Siz202018 }
6807ddae043Siz202018 
6817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6827c478bd9Sstevel@tonic-gate static void
6837c478bd9Sstevel@tonic-gate switcher(void *cookie, char *argp, size_t arg_size,
6847c478bd9Sstevel@tonic-gate     door_desc_t *dp, uint_t n_desc)
6857c478bd9Sstevel@tonic-gate {
6867ddae043Siz202018 #define	GETSIZE 1000
6877ddae043Siz202018 #define	ALLOCATE 1001
6887ddae043Siz202018 
6897c478bd9Sstevel@tonic-gate 	ldap_call_t	*ptr = (ldap_call_t *)argp;
6908142c2b2Schinlong 	ucred_t		*uc = NULL;
6917c478bd9Sstevel@tonic-gate 
6927ddae043Siz202018 	LineBuf		configInfo;
6937ddae043Siz202018 	dataunion	*buf = NULL;
694*e1dd0a2fSth160488 
6957ddae043Siz202018 	/*
6967ddae043Siz202018 	 * By default the size of  a buffer to be passed down to a client
6977ddae043Siz202018 	 * is equal to the size of the ldap_return_t structure. We need
6987ddae043Siz202018 	 * a bigger buffer in a few cases.
6997ddae043Siz202018 	 */
7007ddae043Siz202018 	size_t		configSize = sizeof (ldap_return_t);
7018142c2b2Schinlong 	int		ldapErrno = 0, state, callnumber;
7027ddae043Siz202018 	struct {
7037ddae043Siz202018 		void	*begin;
7047ddae043Siz202018 		size_t	size;
7057ddae043Siz202018 		uint8_t	destroy;
7067ddae043Siz202018 	} dataSource;
7077ddae043Siz202018 
7087c478bd9Sstevel@tonic-gate 	if (argp == DOOR_UNREF_DATA) {
7097c478bd9Sstevel@tonic-gate 		logit("Door Slam... invalid door param\n");
7107c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: Door Slam... "
7117c478bd9Sstevel@tonic-gate 		    "invalid door param"));
7127c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Door Slam... invalid door param\n"));
7137c478bd9Sstevel@tonic-gate 		exit(0);
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if (ptr == NULL) { /* empty door call */
7177c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0); /* return the favor */
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 
7207ddae043Siz202018 	bzero(&dataSource, sizeof (dataSource));
7217ddae043Siz202018 
7227ddae043Siz202018 	/*
7237ddae043Siz202018 	 * We presume that sizeof (ldap_return_t) bytes are always available
7247ddae043Siz202018 	 * on the stack
7257ddae043Siz202018 	 */
7268142c2b2Schinlong 	callnumber = ptr->ldap_callnumber;
7277ddae043Siz202018 
7288142c2b2Schinlong 	switch (callnumber) {
7297c478bd9Sstevel@tonic-gate 		case NULLCALL:
7307ddae043Siz202018 			/*
7317ddae043Siz202018 			 * Just a 'ping'. Use the default size
7327ddae043Siz202018 			 * of the buffer and set the
7337ddae043Siz202018 			 * 'OK' error code.
7347ddae043Siz202018 			 */
7357ddae043Siz202018 			state = ALLOCATE;
7367c478bd9Sstevel@tonic-gate 			break;
7377c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
7387ddae043Siz202018 			/*
7397ddae043Siz202018 			 * Get the current LDAP configuration.
7407ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
7417ddae043Siz202018 			 * the size of ldap_return_t, the next step will
7427ddae043Siz202018 			 * calculate who much space exactly is required.
7437ddae043Siz202018 			 */
7447ddae043Siz202018 			getldap_lookup(&configInfo, ptr);
7457ddae043Siz202018 
7467ddae043Siz202018 			state = GETSIZE;
7477ddae043Siz202018 			break;
7487ddae043Siz202018 		case GETLDAPSERVER:
7497ddae043Siz202018 			/*
7507ddae043Siz202018 			 * Get the root DSE for a next server in the list.
7517ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
7527ddae043Siz202018 			 * the size of ldap_return_t, the next step will
7537ddae043Siz202018 			 * calculate who much space exactly is required.
7547ddae043Siz202018 			 */
7557ddae043Siz202018 			getldap_getserver(&configInfo, ptr);
7567ddae043Siz202018 
7577ddae043Siz202018 			state = GETSIZE;
7587ddae043Siz202018 			break;
7597ddae043Siz202018 		case GETCACHESTAT:
7607ddae043Siz202018 			/*
7617ddae043Siz202018 			 * Get the cache stattistics.
7627ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
7637ddae043Siz202018 			 * the size of ldap_return_t, the next step will
7647ddae043Siz202018 			 * calculate how much space exactly is required.
7657ddae043Siz202018 			 */
7667ddae043Siz202018 			getldap_get_cacheStat(&configInfo);
7677ddae043Siz202018 
7687ddae043Siz202018 			state = GETSIZE;
7697c478bd9Sstevel@tonic-gate 			break;
7707c478bd9Sstevel@tonic-gate 		case GETADMIN:
7717ddae043Siz202018 			/*
7727ddae043Siz202018 			 * Get current configuration and statistics.
7737ddae043Siz202018 			 * The size of the statistics structure is less then
7747ddae043Siz202018 			 * sizeof (ldap_return_t). So specify the source
7757ddae043Siz202018 			 * where to take the info and proceed with the memory
7767ddae043Siz202018 			 * allocation.
7777ddae043Siz202018 			 */
7787ddae043Siz202018 			state = ALLOCATE;
7797ddae043Siz202018 
7807ddae043Siz202018 			if (ldapErrno == 0) {
7817ddae043Siz202018 				dataSource.begin = &current_admin;
7827ddae043Siz202018 				dataSource.size = sizeof (current_admin);
7837ddae043Siz202018 				dataSource.destroy = 0;
7847ddae043Siz202018 			}
7857c478bd9Sstevel@tonic-gate 			break;
7867c478bd9Sstevel@tonic-gate 		case KILLSERVER:
7877ddae043Siz202018 			/*
7887ddae043Siz202018 			 * Process the request and proceed with the default
7897ddae043Siz202018 			 * buffer allocation.
7907ddae043Siz202018 			 */
7918142c2b2Schinlong 			if (is_root(1, "KILLSERVER", &uc))
7927c478bd9Sstevel@tonic-gate 				exit(0);
7938142c2b2Schinlong 
7948142c2b2Schinlong 			ldapErrno = -1;
7958142c2b2Schinlong 			state = ALLOCATE;
7967c478bd9Sstevel@tonic-gate 			break;
7977ddae043Siz202018 		case SETADMIN:
7988142c2b2Schinlong 			/*
7998142c2b2Schinlong 			 * Process the request and proceed with the default
8008142c2b2Schinlong 			 * buffer allocation.
8018142c2b2Schinlong 			 */
8028142c2b2Schinlong 			if (is_root(1, "SETADMIN", &uc))
8037ddae043Siz202018 				ldapErrno = setadmin(ptr);
8048142c2b2Schinlong 			else
8057ddae043Siz202018 				ldapErrno = -1;
8067ddae043Siz202018 
8077ddae043Siz202018 			state = ALLOCATE;
8087c478bd9Sstevel@tonic-gate 			break;
8097c478bd9Sstevel@tonic-gate 		case GETCACHE:
8107ddae043Siz202018 			/*
8117ddae043Siz202018 			 * Get the cache stattistics.
8127ddae043Siz202018 			 * Since this is dynamic data and its size can exceed
8137ddae043Siz202018 			 * the size of ldap_return_t, the next step will
8147ddae043Siz202018 			 * calculate how much space exactly is required.
8157ddae043Siz202018 			 */
8167ddae043Siz202018 			getldap_get_cacheData(&configInfo, ptr);
8177ddae043Siz202018 
8187ddae043Siz202018 			state = GETSIZE;
8197c478bd9Sstevel@tonic-gate 			break;
8207c478bd9Sstevel@tonic-gate 		case SETCACHE:
8217ddae043Siz202018 			/*
8227ddae043Siz202018 			 * Process the request and proceed with the default
8237ddae043Siz202018 			 * buffer allocation.
8247ddae043Siz202018 			 */
8258142c2b2Schinlong 			if (is_root(0, "SETCACHE", &uc) &&
826*e1dd0a2fSth160488 			    is_called_from_nscd(ucred_getpid(uc))) {
8277ddae043Siz202018 				ldapErrno = getldap_set_cacheData(ptr);
8287c478bd9Sstevel@tonic-gate 				current_admin.ldap_stat.ldap_numbercalls++;
8298142c2b2Schinlong 			} else
8308142c2b2Schinlong 				ldapErrno = -1;
8317ddae043Siz202018 
8328142c2b2Schinlong 			if (uc != NULL)
8338142c2b2Schinlong 				ucred_free(uc);
8347ddae043Siz202018 			state = ALLOCATE;
8357c478bd9Sstevel@tonic-gate 			break;
836*e1dd0a2fSth160488 		case GETSTATUSCHANGE:
837*e1dd0a2fSth160488 			/*
838*e1dd0a2fSth160488 			 * Process the request and proceed with the default
839*e1dd0a2fSth160488 			 * buffer allocation.
840*e1dd0a2fSth160488 			 */
841*e1dd0a2fSth160488 			(void) mutex_lock(&chg_threads_num_lock);
842*e1dd0a2fSth160488 			chg_threads_num++;
843*e1dd0a2fSth160488 			if (chg_threads_num > MAX_CHG_THREADS) {
844*e1dd0a2fSth160488 				chg_threads_num--;
845*e1dd0a2fSth160488 				(void) mutex_unlock(&chg_threads_num_lock);
846*e1dd0a2fSth160488 				ldapErrno = CHG_EXCEED_MAX_THREADS;
847*e1dd0a2fSth160488 				state = ALLOCATE;
848*e1dd0a2fSth160488 				break;
849*e1dd0a2fSth160488 			}
850*e1dd0a2fSth160488 			(void) mutex_unlock(&chg_threads_num_lock);
851*e1dd0a2fSth160488 
852*e1dd0a2fSth160488 			if (is_root(0, "GETSTATUSCHANGE", &uc) &&
853*e1dd0a2fSth160488 			    is_called_from_nscd(ucred_getpid(uc))) {
854*e1dd0a2fSth160488 				ldapErrno = chg_get_statusChange(
855*e1dd0a2fSth160488 				    &configInfo, ptr, ucred_getpid(uc));
856*e1dd0a2fSth160488 				state = GETSIZE;
857*e1dd0a2fSth160488 			} else {
858*e1dd0a2fSth160488 				ldapErrno = -1;
859*e1dd0a2fSth160488 				state = ALLOCATE;
860*e1dd0a2fSth160488 			}
861*e1dd0a2fSth160488 			if (uc != NULL)
862*e1dd0a2fSth160488 				ucred_free(uc);
863*e1dd0a2fSth160488 
864*e1dd0a2fSth160488 			(void) mutex_lock(&chg_threads_num_lock);
865*e1dd0a2fSth160488 			chg_threads_num--;
866*e1dd0a2fSth160488 			(void) mutex_unlock(&chg_threads_num_lock);
867*e1dd0a2fSth160488 			break;
8687c478bd9Sstevel@tonic-gate 		default:
8697ddae043Siz202018 			/*
8707ddae043Siz202018 			 * This means an unknown request type. Proceed with
8717ddae043Siz202018 			 * the default buffer allocation.
8727ddae043Siz202018 			 */
8737c478bd9Sstevel@tonic-gate 			logit("Unknown ldap service door call op %d\n",
8747c478bd9Sstevel@tonic-gate 			    ptr->ldap_callnumber);
8757ddae043Siz202018 			ldapErrno = -99;
8767ddae043Siz202018 
8777ddae043Siz202018 			state = ALLOCATE;
8787ddae043Siz202018 			break;
8798142c2b2Schinlong 	}
8808142c2b2Schinlong 
8818142c2b2Schinlong 	switch (state) {
8827ddae043Siz202018 		case GETSIZE:
8837ddae043Siz202018 			/*
8847ddae043Siz202018 			 * This stage calculates how much data will be
8857ddae043Siz202018 			 * passed down to the client, checks if there is
8867ddae043Siz202018 			 * enough space on the stack to accommodate the data,
8877ddae043Siz202018 			 * increases the value of the configSize variable
8887ddae043Siz202018 			 * if necessary and specifies the data source.
8897ddae043Siz202018 			 * In case of any error occurred ldapErrno will be set
8907ddae043Siz202018 			 * appropriately.
8917ddae043Siz202018 			 */
8927ddae043Siz202018 			if (configInfo.str == NULL) {
8937ddae043Siz202018 				ldapErrno = -1;
8947ddae043Siz202018 			}
8957ddae043Siz202018 
8967ddae043Siz202018 			configSize = get_data_size(&configInfo, &ldapErrno);
8977ddae043Siz202018 
8987ddae043Siz202018 			if (ldapErrno == 0) {
8997ddae043Siz202018 				dataSource.begin = configInfo.str;
9007ddae043Siz202018 				dataSource.size = configInfo.len;
9017ddae043Siz202018 				dataSource.destroy = 1;
9027ddae043Siz202018 			}
9037ddae043Siz202018 
9047ddae043Siz202018 			current_admin.ldap_stat.ldap_numbercalls++;
9058142c2b2Schinlong 			/* FALLTHRU */
9067ddae043Siz202018 		case ALLOCATE:
9077ddae043Siz202018 			/*
9087ddae043Siz202018 			 * Allocate a buffer of the calculated (or default) size
9097ddae043Siz202018 			 * and proceed with populating it with data.
9107ddae043Siz202018 			 */
9117ddae043Siz202018 			buf = (dataunion *) alloca(configSize);
9127ddae043Siz202018 
9137ddae043Siz202018 			/*
9147ddae043Siz202018 			 * Set a return code and, if a data source is specified,
9157ddae043Siz202018 			 * copy data from the source to the buffer.
9167ddae043Siz202018 			 */
9177ddae043Siz202018 			buf->data.ldap_ret.ldap_errno = ldapErrno;
9187ddae043Siz202018 			buf->data.ldap_ret.ldap_return_code = ldapErrno;
9197ddae043Siz202018 			buf->data.ldap_ret.ldap_bufferbytesused = configSize;
9207ddae043Siz202018 
9217ddae043Siz202018 			if (dataSource.begin != NULL) {
9227ddae043Siz202018 				(void) memcpy(buf->data.ldap_ret.ldap_u.config,
9237ddae043Siz202018 				    dataSource.begin,
9247ddae043Siz202018 				    dataSource.size);
9257ddae043Siz202018 				if (dataSource.destroy) {
9267ddae043Siz202018 					free(dataSource.begin);
9277ddae043Siz202018 				}
9287ddae043Siz202018 			}
9297ddae043Siz202018 
9307c478bd9Sstevel@tonic-gate 	}
9317ddae043Siz202018 	(void) door_return((char *)&buf->data,
9327ddae043Siz202018 	    buf->data.ldap_ret.ldap_bufferbytesused,
9337ddae043Siz202018 	    NULL,
9347ddae043Siz202018 	    0);
9357ddae043Siz202018 #undef	GETSIZE
9367ddae043Siz202018 #undef	ALLOCATE
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate static void
9407c478bd9Sstevel@tonic-gate usage(char *s)
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
9437c478bd9Sstevel@tonic-gate 	    gettext("Usage: %s [-d debug_level] [-l logfilename]\n"), s);
9447c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-K] "
9457c478bd9Sstevel@tonic-gate 	    "[-r revalidate_interval] "));
9467c478bd9Sstevel@tonic-gate #ifndef SLP
9477c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g]\n"));
9487c478bd9Sstevel@tonic-gate #else
9497c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g] [-s]\n"));
9507c478bd9Sstevel@tonic-gate #endif /* SLP */
9517c478bd9Sstevel@tonic-gate 	exit(1);
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate static int	logfd = -1;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate static int
9587c478bd9Sstevel@tonic-gate cachemgr_set_lf(admin_t *ptr, char *logfile)
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate 	int	newlogfd;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	/*
9637c478bd9Sstevel@tonic-gate 	 *  we don't really want to try and open the log file
9647c478bd9Sstevel@tonic-gate 	 *  /dev/null since that will fail w/ our security fixes
9657c478bd9Sstevel@tonic-gate 	 */
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	if (logfile == NULL || *logfile == 0) {
9687c478bd9Sstevel@tonic-gate 		/*EMPTY*/;
9697c478bd9Sstevel@tonic-gate 	} else if (strcmp(logfile, "/dev/null") == 0) {
9707c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, "/dev/null");
9717c478bd9Sstevel@tonic-gate 		(void) close(logfd);
9727c478bd9Sstevel@tonic-gate 		logfd = -1;
9737c478bd9Sstevel@tonic-gate 	} else {
9747c478bd9Sstevel@tonic-gate 		if ((newlogfd =
9757c478bd9Sstevel@tonic-gate 		    open(logfile, O_EXCL|O_WRONLY|O_CREAT, 0644)) < 0) {
9767c478bd9Sstevel@tonic-gate 			/*
9777c478bd9Sstevel@tonic-gate 			 * File already exists... now we need to get cute
9787c478bd9Sstevel@tonic-gate 			 * since opening a file in a world-writeable directory
9797c478bd9Sstevel@tonic-gate 			 * safely is hard = it could be a hard link or a
9807c478bd9Sstevel@tonic-gate 			 * symbolic link to a system file.
9817c478bd9Sstevel@tonic-gate 			 *
9827c478bd9Sstevel@tonic-gate 			 */
9837c478bd9Sstevel@tonic-gate 			struct stat	before;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 			if (lstat(logfile, &before) < 0) {
9867c478bd9Sstevel@tonic-gate 				logit("Cannot open new logfile \"%s\": %sn",
9877c478bd9Sstevel@tonic-gate 				    logfile, strerror(errno));
9887c478bd9Sstevel@tonic-gate 				return (-1);
9897c478bd9Sstevel@tonic-gate 			}
9907c478bd9Sstevel@tonic-gate 			if (S_ISREG(before.st_mode) &&	/* no symbolic links */
9917c478bd9Sstevel@tonic-gate 			    (before.st_nlink == 1) &&	/* no hard links */
9927c478bd9Sstevel@tonic-gate 			    (before.st_uid == 0)) {	/* owned by root */
9937c478bd9Sstevel@tonic-gate 				if ((newlogfd =
9947c478bd9Sstevel@tonic-gate 				    open(logfile,
9957c478bd9Sstevel@tonic-gate 				    O_APPEND|O_WRONLY, 0644)) < 0) {
9967c478bd9Sstevel@tonic-gate 					logit("Cannot open new logfile "
9977c478bd9Sstevel@tonic-gate 					    "\"%s\": %s\n",
9987c478bd9Sstevel@tonic-gate 					    logfile, strerror(errno));
9997c478bd9Sstevel@tonic-gate 					return (-1);
10007c478bd9Sstevel@tonic-gate 				}
10017c478bd9Sstevel@tonic-gate 			} else {
10027c478bd9Sstevel@tonic-gate 				logit("Cannot use specified logfile "
10037c478bd9Sstevel@tonic-gate 				    "\"%s\": file is/has links or isn't "
10047c478bd9Sstevel@tonic-gate 				    "owned by root\n", logfile);
10057c478bd9Sstevel@tonic-gate 				return (-1);
10067c478bd9Sstevel@tonic-gate 			}
10077c478bd9Sstevel@tonic-gate 		}
10087c478bd9Sstevel@tonic-gate 		(void) strlcpy(ptr->logfile, logfile, sizeof (ptr->logfile));
10097c478bd9Sstevel@tonic-gate 		(void) close(logfd);
10107c478bd9Sstevel@tonic-gate 		logfd = newlogfd;
10117c478bd9Sstevel@tonic-gate 		logit("Starting ldap_cachemgr, logfile %s\n", logfile);
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 	return (0);
10147c478bd9Sstevel@tonic-gate }
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
10177c478bd9Sstevel@tonic-gate void
10187c478bd9Sstevel@tonic-gate logit(char *format, ...)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	static mutex_t	loglock;
10217c478bd9Sstevel@tonic-gate 	struct timeval	tv;
10227c478bd9Sstevel@tonic-gate 	char		buffer[BUFSIZ];
10237c478bd9Sstevel@tonic-gate 	va_list		ap;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	va_start(ap, format);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	if (logfd >= 0) {
10287c478bd9Sstevel@tonic-gate 		int	safechars;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tv, NULL);
10317c478bd9Sstevel@tonic-gate 		(void) ctime_r(&tv.tv_sec, buffer, BUFSIZ);
10327c478bd9Sstevel@tonic-gate 		(void) snprintf(buffer+19, BUFSIZE, ".%.4ld	",
10337c478bd9Sstevel@tonic-gate 		    tv.tv_usec/100);
10347c478bd9Sstevel@tonic-gate 		safechars = sizeof (buffer) - 30;
10357c478bd9Sstevel@tonic-gate 		if (vsnprintf(buffer+25, safechars, format, ap) > safechars)
10367c478bd9Sstevel@tonic-gate 			(void) strcat(buffer, "...\n");
10377c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&loglock);
10387c478bd9Sstevel@tonic-gate 		(void) write(logfd, buffer, strlen(buffer));
10397c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&loglock);
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 	va_end(ap);
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate static int
10467c478bd9Sstevel@tonic-gate client_getadmin(admin_t *ptr)
10477c478bd9Sstevel@tonic-gate {
10487c478bd9Sstevel@tonic-gate 	dataunion		u;
10497c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
10507c478bd9Sstevel@tonic-gate 	int		ndata;
10517c478bd9Sstevel@tonic-gate 	int		adata;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETADMIN;
10547c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
10557c478bd9Sstevel@tonic-gate 	adata = sizeof (u.data);
10567c478bd9Sstevel@tonic-gate 	dptr = &u.data;
10577c478bd9Sstevel@tonic-gate 
1058*e1dd0a2fSth160488 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != NS_CACHE_SUCCESS) {
10597c478bd9Sstevel@tonic-gate 		return (-1);
10607c478bd9Sstevel@tonic-gate 	}
10617c478bd9Sstevel@tonic-gate 	(void) memcpy(ptr, dptr->ldap_ret.ldap_u.buff, sizeof (*ptr));
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	return (0);
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate static int
10687ddae043Siz202018 setadmin(ldap_call_t *ptr)
10697c478bd9Sstevel@tonic-gate {
10707c478bd9Sstevel@tonic-gate 	admin_t	*new;
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	new = (admin_t *)ptr->ldap_u.domainname;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	/*
10757c478bd9Sstevel@tonic-gate 	 *  global admin stuff
10767c478bd9Sstevel@tonic-gate 	 */
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	if ((cachemgr_set_lf(&current_admin, new->logfile) < 0) ||
10797c478bd9Sstevel@tonic-gate 	    cachemgr_set_dl(&current_admin, new->debug_level) < 0) {
10807c478bd9Sstevel@tonic-gate 		return (-1);
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	if (cachemgr_set_ttl(&current_admin.ldap_stat,
10847c478bd9Sstevel@tonic-gate 	    "ldap",
10857c478bd9Sstevel@tonic-gate 	    new->ldap_stat.ldap_ttl) < 0) {
10867c478bd9Sstevel@tonic-gate 		return (-1);
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	return (0);
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate static void
10947c478bd9Sstevel@tonic-gate client_killserver()
10957c478bd9Sstevel@tonic-gate {
10967c478bd9Sstevel@tonic-gate 	dataunion		u;
10977c478bd9Sstevel@tonic-gate 	ldap_data_t		*dptr;
10987c478bd9Sstevel@tonic-gate 	int			ndata;
10997c478bd9Sstevel@tonic-gate 	int			adata;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = KILLSERVER;
11027c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
11037c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
11047c478bd9Sstevel@tonic-gate 	dptr = &u.data;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	__ns_ldap_trydoorcall(&dptr, &ndata, &adata);
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate static int
11117c478bd9Sstevel@tonic-gate client_setadmin(admin_t *ptr)
11127c478bd9Sstevel@tonic-gate {
11137c478bd9Sstevel@tonic-gate 	dataunion		u;
11147c478bd9Sstevel@tonic-gate 	ldap_data_t		*dptr;
11157c478bd9Sstevel@tonic-gate 	int			ndata;
11167c478bd9Sstevel@tonic-gate 	int			adata;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = SETADMIN;
11197c478bd9Sstevel@tonic-gate 	(void) memcpy(u.data.ldap_call.ldap_u.domainname, ptr, sizeof (*ptr));
11207c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
11217c478bd9Sstevel@tonic-gate 	adata = sizeof (*ptr);
11227c478bd9Sstevel@tonic-gate 	dptr = &u.data;
11237c478bd9Sstevel@tonic-gate 
1124*e1dd0a2fSth160488 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != NS_CACHE_SUCCESS) {
11257c478bd9Sstevel@tonic-gate 		return (-1);
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	return (0);
11297c478bd9Sstevel@tonic-gate }
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate static int
11327c478bd9Sstevel@tonic-gate client_showstats(admin_t *ptr)
11337c478bd9Sstevel@tonic-gate {
11347c478bd9Sstevel@tonic-gate 	dataunion	u;
11357c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
11367c478bd9Sstevel@tonic-gate 	int		ndata;
11377c478bd9Sstevel@tonic-gate 	int		adata;
11387c478bd9Sstevel@tonic-gate 	char		*rbuf, *sptr, *rest;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	/*
11417c478bd9Sstevel@tonic-gate 	 * print admin data
11427c478bd9Sstevel@tonic-gate 	 */
11437c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr configuration:\n"));
11447c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server debug level %10d\n"), ptr->debug_level);
11457c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server log file\t\"%s\"\n"), ptr->logfile);
11467c478bd9Sstevel@tonic-gate 	(void) printf(gettext("number of calls to ldapcachemgr %10d\n"),
11477c478bd9Sstevel@tonic-gate 	    ptr->ldap_stat.ldap_numbercalls);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	/*
11507c478bd9Sstevel@tonic-gate 	 * get cache data statistics
11517c478bd9Sstevel@tonic-gate 	 */
11527c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETCACHESTAT;
11537c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
11547c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
11557c478bd9Sstevel@tonic-gate 	dptr = &u.data;
11567c478bd9Sstevel@tonic-gate 
1157*e1dd0a2fSth160488 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != NS_CACHE_SUCCESS) {
11587c478bd9Sstevel@tonic-gate 		(void) printf(
11597c478bd9Sstevel@tonic-gate 		    gettext("\nCache data statistics not available!\n"));
11607c478bd9Sstevel@tonic-gate 		return (0);
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	/*
11647c478bd9Sstevel@tonic-gate 	 * print cache data statistics line by line
11657c478bd9Sstevel@tonic-gate 	 */
11667c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr cache data statistics:\n"));
11677c478bd9Sstevel@tonic-gate 	rbuf = dptr->ldap_ret.ldap_u.buff;
11687c478bd9Sstevel@tonic-gate 	sptr = strtok_r(rbuf, DOORLINESEP, &rest);
11697c478bd9Sstevel@tonic-gate 	for (;;) {
11707c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", sptr);
11717c478bd9Sstevel@tonic-gate 		sptr = strtok_r(NULL, DOORLINESEP, &rest);
11727c478bd9Sstevel@tonic-gate 		if (sptr == NULL)
11737c478bd9Sstevel@tonic-gate 			break;
11747c478bd9Sstevel@tonic-gate 	}
11757c478bd9Sstevel@tonic-gate 	return (0);
11767c478bd9Sstevel@tonic-gate }
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate /*
11807c478bd9Sstevel@tonic-gate  * detach from tty
11817c478bd9Sstevel@tonic-gate  */
11827c478bd9Sstevel@tonic-gate static void
11837c478bd9Sstevel@tonic-gate detachfromtty(char *pgm)
11847c478bd9Sstevel@tonic-gate {
11857c478bd9Sstevel@tonic-gate 	int 	status;
11867c478bd9Sstevel@tonic-gate 	pid_t	pid, wret;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	(void) close(0);
11897c478bd9Sstevel@tonic-gate 	(void) close(1);
11907c478bd9Sstevel@tonic-gate 	/*
11917c478bd9Sstevel@tonic-gate 	 * Block the SIGUSR1 signal
11927c478bd9Sstevel@tonic-gate 	 * just in case that the child
11937c478bd9Sstevel@tonic-gate 	 * process may run faster than
11947c478bd9Sstevel@tonic-gate 	 * the parent process and
11957c478bd9Sstevel@tonic-gate 	 * send this signal before
11967c478bd9Sstevel@tonic-gate 	 * the signal handler is ready
11977c478bd9Sstevel@tonic-gate 	 * in the parent process.
11987c478bd9Sstevel@tonic-gate 	 * This error will cause the parent
11997c478bd9Sstevel@tonic-gate 	 * to exit with the User Signal 1
12007c478bd9Sstevel@tonic-gate 	 * exit code (144).
12017c478bd9Sstevel@tonic-gate 	 */
12027c478bd9Sstevel@tonic-gate 	(void) sighold(SIGUSR1);
12037c478bd9Sstevel@tonic-gate 	pid = fork1();
12047c478bd9Sstevel@tonic-gate 	switch (pid) {
12057c478bd9Sstevel@tonic-gate 		case (pid_t)-1:
12067c478bd9Sstevel@tonic-gate 			logit("detachfromtty(): fork1() call failed\n");
12077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12087c478bd9Sstevel@tonic-gate 			    gettext("%s: fork1() call failed.\n"),
12097c478bd9Sstevel@tonic-gate 			    pgm);
12107c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
12117c478bd9Sstevel@tonic-gate 			    gettext("ldap_cachemgr: fork1() call failed."));
12127c478bd9Sstevel@tonic-gate 			exit(1);
12137c478bd9Sstevel@tonic-gate 			break;
12147c478bd9Sstevel@tonic-gate 		case 0:
12157c478bd9Sstevel@tonic-gate 			/*
12167c478bd9Sstevel@tonic-gate 			 * child process does not
12177c478bd9Sstevel@tonic-gate 			 * need to worry about
12187c478bd9Sstevel@tonic-gate 			 * the SIGUSR1 signal
12197c478bd9Sstevel@tonic-gate 			 */
12207c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
12217c478bd9Sstevel@tonic-gate 			(void) close(2);
12227c478bd9Sstevel@tonic-gate 			break;
12237c478bd9Sstevel@tonic-gate 		default:
12247c478bd9Sstevel@tonic-gate 			/*
12257c478bd9Sstevel@tonic-gate 			 * Wait forever until the child process
12267c478bd9Sstevel@tonic-gate 			 * has exited, or has signalled that at
12277c478bd9Sstevel@tonic-gate 			 * least one server in the server list
12287c478bd9Sstevel@tonic-gate 			 * is up.
12297c478bd9Sstevel@tonic-gate 			 */
12307c478bd9Sstevel@tonic-gate 			if (signal(SIGUSR1, sig_ok_to_exit) == SIG_ERR) {
12317c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12327c478bd9Sstevel@tonic-gate 				    "can't set up signal handler to "
12337c478bd9Sstevel@tonic-gate 				    " catch SIGUSR1.\n");
12347c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12357c478bd9Sstevel@tonic-gate 				    gettext("%s: signal() call failed.\n"),
12367c478bd9Sstevel@tonic-gate 				    pgm);
12377c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, gettext("ldap_cachemgr: "
12387c478bd9Sstevel@tonic-gate 				    "can't set up signal handler to "
12397c478bd9Sstevel@tonic-gate 				    " catch SIGUSR1."));
12407c478bd9Sstevel@tonic-gate 				exit(1);
12417c478bd9Sstevel@tonic-gate 			}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 			/*
12447c478bd9Sstevel@tonic-gate 			 * now unblock the SIGUSR1 signal
12457c478bd9Sstevel@tonic-gate 			 * to handle the pending or
12467c478bd9Sstevel@tonic-gate 			 * soon to arrive SIGUSR1 signal
12477c478bd9Sstevel@tonic-gate 			 */
12487c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
12497c478bd9Sstevel@tonic-gate 			wret = waitpid(pid, &status, 0);
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 			if (wret == -1) {
12527c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12537c478bd9Sstevel@tonic-gate 				    "waitpid() call failed\n");
12547c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12557c478bd9Sstevel@tonic-gate 				    gettext("%s: waitpid() call failed.\n"),
12567c478bd9Sstevel@tonic-gate 				    pgm);
12577c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
12587c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: waitpid() "
12597c478bd9Sstevel@tonic-gate 				    "call failed."));
12607c478bd9Sstevel@tonic-gate 				exit(1);
12617c478bd9Sstevel@tonic-gate 			}
12627c478bd9Sstevel@tonic-gate 			if (wret != pid) {
12637c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12647c478bd9Sstevel@tonic-gate 				    "waitpid() returned %ld when "
12657c478bd9Sstevel@tonic-gate 				    "child pid was %ld\n",
12667c478bd9Sstevel@tonic-gate 				    wret, pid);
12677c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12687c478bd9Sstevel@tonic-gate 				    gettext(
12697c478bd9Sstevel@tonic-gate 				    "%s: waitpid() returned %ld when "
12707c478bd9Sstevel@tonic-gate 				    "child pid was %ld.\n"),
12717c478bd9Sstevel@tonic-gate 				    pgm, wret, pid);
12727c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
12737c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: waitpid() "
12747c478bd9Sstevel@tonic-gate 				    "returned different "
12757c478bd9Sstevel@tonic-gate 				    "child pid."));
12767c478bd9Sstevel@tonic-gate 				exit(1);
12777c478bd9Sstevel@tonic-gate 			}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 			/* evaluate return status */
12807c478bd9Sstevel@tonic-gate 			if (WIFEXITED(status)) {
12817c478bd9Sstevel@tonic-gate 				if (WEXITSTATUS(status) == 0) {
12827c478bd9Sstevel@tonic-gate 					exit(0);
12837c478bd9Sstevel@tonic-gate 				}
12847c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12857c478bd9Sstevel@tonic-gate 				    "child failed (rc = %d).\n",
12867c478bd9Sstevel@tonic-gate 				    WEXITSTATUS(status));
12877c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12887c478bd9Sstevel@tonic-gate 				    gettext("%s: failed. Please see "
12897c478bd9Sstevel@tonic-gate 				    "syslog for details.\n"),
12907c478bd9Sstevel@tonic-gate 				    pgm);
12917c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
12927c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: failed "
12937c478bd9Sstevel@tonic-gate 				    "(rc = %d)."),
12947c478bd9Sstevel@tonic-gate 				    WEXITSTATUS(status));
12957c478bd9Sstevel@tonic-gate 			} else if (WIFSIGNALED(status)) {
12967c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
12977c478bd9Sstevel@tonic-gate 				    "child terminated by signal %d.\n",
12987c478bd9Sstevel@tonic-gate 				    WTERMSIG(status));
12997c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
13007c478bd9Sstevel@tonic-gate 				gettext("%s: terminated by signal %d.\n"),
13017c478bd9Sstevel@tonic-gate 				    pgm, WTERMSIG(status));
13027c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
13037c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: terminated by "
13047c478bd9Sstevel@tonic-gate 				    "signal %d.\n"),
13057c478bd9Sstevel@tonic-gate 				    WTERMSIG(status));
13067c478bd9Sstevel@tonic-gate 			} else if (WCOREDUMP(status)) {
13077c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): child core dumped.\n"),
13087c478bd9Sstevel@tonic-gate 				    (void) fprintf(stderr,
13097c478bd9Sstevel@tonic-gate 				    gettext("%s: core dumped.\n"),
13107c478bd9Sstevel@tonic-gate 				    pgm);
13117c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
13127c478bd9Sstevel@tonic-gate 				    gettext("ldap_cachemgr: "
13137c478bd9Sstevel@tonic-gate 				    "core dumped.\n"));
13147c478bd9Sstevel@tonic-gate 			}
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 			exit(1);
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 	(void) setsid();
13197c478bd9Sstevel@tonic-gate 	if (open("/dev/null", O_RDWR, 0) != -1) {
13207c478bd9Sstevel@tonic-gate 		(void) dup(0);
13217c478bd9Sstevel@tonic-gate 		(void) dup(0);
13227c478bd9Sstevel@tonic-gate 	}
13237c478bd9Sstevel@tonic-gate }
13248142c2b2Schinlong 
13258142c2b2Schinlong /*
13268142c2b2Schinlong  * Check if the door client's euid is 0
13278142c2b2Schinlong  *
13288142c2b2Schinlong  * We could check for some privilege or re-design the interfaces that
13298142c2b2Schinlong  * lead to is_root() being called so that we rely on SMF and RBAC, but
13308142c2b2Schinlong  * we need this check only for dealing with undocumented-but-possibly-
13318142c2b2Schinlong  * used interfaces.  Anything beyond checking for euid == 0 here would
13328142c2b2Schinlong  * be overkill considering that those are undocumented interfaces.
13338142c2b2Schinlong  *
13348142c2b2Schinlong  * If free_uc is 0, the caller is responsible for freeing *ucp.
13358142c2b2Schinlong  *
13368142c2b2Schinlong  * return - 0 euid != 0
13378142c2b2Schinlong  *          1 euid == 0
13388142c2b2Schinlong  */
13398142c2b2Schinlong static int
13408142c2b2Schinlong is_root(int free_uc, char *dc_str, ucred_t **ucp)
13418142c2b2Schinlong {
13428142c2b2Schinlong 	int	rc;
13438142c2b2Schinlong 
13448142c2b2Schinlong 	if (door_ucred(ucp) != 0) {
13458142c2b2Schinlong 		rc = errno;
13468142c2b2Schinlong 		logit("door_ucred() call failed %s\n", strerror(rc));
13478142c2b2Schinlong 		syslog(LOG_ERR, gettext("ldap_cachemgr: door_ucred() call %s "
13488142c2b2Schinlong 		    "failed %s"), strerror(rc));
13498142c2b2Schinlong 		return (0);
13508142c2b2Schinlong 	}
13518142c2b2Schinlong 
13528142c2b2Schinlong 
13538142c2b2Schinlong 	if (ucred_geteuid(*ucp) != 0) {
13548142c2b2Schinlong 
13558142c2b2Schinlong 		if (current_admin.debug_level >= DBG_CANT_FIND)
13568142c2b2Schinlong 			logit("%s call failed(cred): caller pid %ld, uid %u, "
13578142c2b2Schinlong 			    "euid %u\n", dc_str, ucred_getpid(*ucp),
13588142c2b2Schinlong 			    ucred_getruid(*ucp), ucred_geteuid(*ucp));
13598142c2b2Schinlong 
13608142c2b2Schinlong 		rc = 0;
13618142c2b2Schinlong 	} else {
13628142c2b2Schinlong 
13638142c2b2Schinlong 		if (current_admin.debug_level >= DBG_ALL)
13648142c2b2Schinlong 			logit("ldap_cachemgr received %s call from pid %ld, "
13658142c2b2Schinlong 			    "uid %u, euid %u\n", dc_str, ucred_getpid(*ucp),
13668142c2b2Schinlong 			    ucred_getruid(*ucp), ucred_geteuid(*ucp));
13678142c2b2Schinlong 		rc = 1;
13688142c2b2Schinlong 	}
13698142c2b2Schinlong 
13708142c2b2Schinlong 	if (free_uc)
13718142c2b2Schinlong 		ucred_free(*ucp);
13728142c2b2Schinlong 
13738142c2b2Schinlong 	return (rc);
13748142c2b2Schinlong }
13758142c2b2Schinlong 
13768142c2b2Schinlong /*
13778142c2b2Schinlong  * Check if pid is nscd
13788142c2b2Schinlong  *
13798142c2b2Schinlong  * Input: pid - process id of the door client that calls ldap_cachemgr
13808142c2b2Schinlong  *
13818142c2b2Schinlong  * Return: 0 - No
13828142c2b2Schinlong  *         1 - Yes
13838142c2b2Schinlong  */
13848142c2b2Schinlong 
1385*e1dd0a2fSth160488 int
1386*e1dd0a2fSth160488 is_called_from_nscd(pid_t pid)
13878142c2b2Schinlong 
13888142c2b2Schinlong {
13898142c2b2Schinlong 	static mutex_t	_door_lock = DEFAULTMUTEX;
13908142c2b2Schinlong 	static	int	doorfd = -1;
13918142c2b2Schinlong 	int		match;
13928142c2b2Schinlong 	door_info_t 	my_door;
13938142c2b2Schinlong 
13948142c2b2Schinlong 	/*
13958142c2b2Schinlong 	 * the first time in we try and open and validate the door.
13968142c2b2Schinlong 	 * the validations are that the door must have been
13978142c2b2Schinlong 	 * created with the door cookie and
13988142c2b2Schinlong 	 * that the file attached to the door is owned by root
13998142c2b2Schinlong 	 * and readonly by user, group and other.  If any of these
14008142c2b2Schinlong 	 * validations fail we refuse to use the door.
14018142c2b2Schinlong 	 */
14028142c2b2Schinlong 
14038142c2b2Schinlong 	(void) mutex_lock(&_door_lock);
14048142c2b2Schinlong 
14058142c2b2Schinlong try_again:
14068142c2b2Schinlong 
14078142c2b2Schinlong 	if (doorfd == -1) {
14088142c2b2Schinlong 
14098142c2b2Schinlong 		if ((doorfd = open(NAME_SERVICE_DOOR, O_RDONLY, 0))
14108142c2b2Schinlong 		    == -1) {
14118142c2b2Schinlong 			(void) mutex_unlock(&_door_lock);
14128142c2b2Schinlong 			return (0);
14138142c2b2Schinlong 		}
14148142c2b2Schinlong 
14158142c2b2Schinlong 		if (door_info(doorfd, &my_door) == -1 ||
14168142c2b2Schinlong 		    (my_door.di_attributes & DOOR_REVOKED) ||
14178142c2b2Schinlong 		    my_door.di_data != (uintptr_t)NAME_SERVICE_DOOR_COOKIE) {
14188142c2b2Schinlong 			/*
14198142c2b2Schinlong 			 * we should close doorfd because we just opened it
14208142c2b2Schinlong 			 */
14218142c2b2Schinlong 			(void) close(doorfd);
14228142c2b2Schinlong 			doorfd = -1;
14238142c2b2Schinlong 			(void) mutex_unlock(&_door_lock);
14248142c2b2Schinlong 			return (0);
14258142c2b2Schinlong 		}
14268142c2b2Schinlong 	} else {
14278142c2b2Schinlong 		/*
14288142c2b2Schinlong 		 * doorfd is cached. Double check just in case
14298142c2b2Schinlong 		 * the door server is restarted or is down.
14308142c2b2Schinlong 		 */
14318142c2b2Schinlong 		if (door_info(doorfd, &my_door) == -1 ||
14328142c2b2Schinlong 		    my_door.di_data != (uintptr_t)NAME_SERVICE_DOOR_COOKIE) {
14338142c2b2Schinlong 			/*
14348142c2b2Schinlong 			 * don't close it -
14358142c2b2Schinlong 			 * someone else has clobbered fd
14368142c2b2Schinlong 			 */
14378142c2b2Schinlong 			doorfd = -1;
14388142c2b2Schinlong 			goto try_again;
14398142c2b2Schinlong 		}
14408142c2b2Schinlong 
14418142c2b2Schinlong 		if (my_door.di_attributes & DOOR_REVOKED) {
14428142c2b2Schinlong 			(void) close(doorfd);
14438142c2b2Schinlong 			doorfd = -1;	/* try and restart connection */
14448142c2b2Schinlong 			goto try_again;
14458142c2b2Schinlong 		}
14468142c2b2Schinlong 	}
14478142c2b2Schinlong 
14488142c2b2Schinlong 	/*
14498142c2b2Schinlong 	 * door descriptor exists and is valid
14508142c2b2Schinlong 	 */
14518142c2b2Schinlong 	if (pid == my_door.di_target)
14528142c2b2Schinlong 		match = 1;
14538142c2b2Schinlong 	else
14548142c2b2Schinlong 		match = 0;
14558142c2b2Schinlong 
14568142c2b2Schinlong 	(void) mutex_unlock(&_door_lock);
14578142c2b2Schinlong 
14588142c2b2Schinlong 	return (match);
14598142c2b2Schinlong 
14608142c2b2Schinlong }
1461