xref: /titanic_54/usr/src/cmd/ldapcachemgr/cachemgr.c (revision a506a34ceb0e9dcc6c61bf0560202f8538928650)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Simple doors ldap cache daemon
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <signal.h>
367c478bd9Sstevel@tonic-gate #include <door.h>
377c478bd9Sstevel@tonic-gate #include <time.h>
387c478bd9Sstevel@tonic-gate #include <string.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 */
557c478bd9Sstevel@tonic-gate #include "cachemgr.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static void	detachfromtty();
587c478bd9Sstevel@tonic-gate admin_t		current_admin;
597c478bd9Sstevel@tonic-gate static int	will_become_server;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static void switcher(void *cookie, char *argp, size_t arg_size,
627c478bd9Sstevel@tonic-gate 			door_desc_t *dp, uint_t n_desc);
637c478bd9Sstevel@tonic-gate static void usage(char *s);
647c478bd9Sstevel@tonic-gate static int cachemgr_set_lf(admin_t *ptr, char *logfile);
657c478bd9Sstevel@tonic-gate static int client_getadmin(admin_t *ptr);
667c478bd9Sstevel@tonic-gate static int getadmin(ldap_return_t *out);
677c478bd9Sstevel@tonic-gate static int setadmin(ldap_return_t *out, ldap_call_t *ptr);
687c478bd9Sstevel@tonic-gate static  int client_setadmin(admin_t *ptr);
697c478bd9Sstevel@tonic-gate static int client_showstats(admin_t *ptr);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #ifdef SLP
727c478bd9Sstevel@tonic-gate int			use_slp = 0;
737c478bd9Sstevel@tonic-gate static unsigned int	refresh = 10800;	/* dynamic discovery interval */
747c478bd9Sstevel@tonic-gate #endif /* SLP */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static ldap_stat_t *
777c478bd9Sstevel@tonic-gate getcacheptr(char *s)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	static const char *caches[1] = {"ldap"};
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (strncmp(caches[0], s, strlen(caches[0])) == 0)
827c478bd9Sstevel@tonic-gate 		return (&current_admin.ldap_stat);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	return (NULL);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate char *
887c478bd9Sstevel@tonic-gate getcacheopt(char *s)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	while (*s && *s != ',')
917c478bd9Sstevel@tonic-gate 		s++;
927c478bd9Sstevel@tonic-gate 	return ((*s == ',') ? (s + 1) : NULL);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  *  This is here to prevent the ldap_cachemgr becomes
977c478bd9Sstevel@tonic-gate  *  daemonlized to early to soon during boot time.
987c478bd9Sstevel@tonic-gate  *  This causes problems during boot when automounter
997c478bd9Sstevel@tonic-gate  *  and others try to use libsldap before ldap_cachemgr
1007c478bd9Sstevel@tonic-gate  *  finishes walking the server list.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate static void
1037c478bd9Sstevel@tonic-gate sig_ok_to_exit(int signo)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	if (signo == SIGUSR1) {
1067c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): parent exiting...\n");
1077c478bd9Sstevel@tonic-gate 		exit(0);
1087c478bd9Sstevel@tonic-gate 	} else {
1097c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): invalid signal(%d) received.\n",
1107c478bd9Sstevel@tonic-gate 			signo);
1117c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: "
1127c478bd9Sstevel@tonic-gate 			"invalid signal(%d) received."), signo);
1137c478bd9Sstevel@tonic-gate 		exit(1);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate #define	LDAP_TABLES		1	/* ldap */
1177c478bd9Sstevel@tonic-gate #define	TABLE_THREADS		10
1187c478bd9Sstevel@tonic-gate #define	COMMON_THREADS		20
1197c478bd9Sstevel@tonic-gate #define	CACHE_MISS_THREADS	(COMMON_THREADS + LDAP_TABLES * TABLE_THREADS)
1207c478bd9Sstevel@tonic-gate #define	CACHE_HIT_THREADS	20
1217c478bd9Sstevel@tonic-gate #define	MAX_SERVER_THREADS	(CACHE_HIT_THREADS + CACHE_MISS_THREADS)
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static sema_t common_sema;
1247c478bd9Sstevel@tonic-gate static sema_t ldap_sema;
1257c478bd9Sstevel@tonic-gate static thread_key_t lookup_state_key;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate static void
1287c478bd9Sstevel@tonic-gate initialize_lookup_clearance()
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	(void) thr_keycreate(&lookup_state_key, NULL);
1317c478bd9Sstevel@tonic-gate 	(void) sema_init(&common_sema, COMMON_THREADS, USYNC_THREAD, 0);
1327c478bd9Sstevel@tonic-gate 	(void) sema_init(&ldap_sema, TABLE_THREADS, USYNC_THREAD, 0);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int
1367c478bd9Sstevel@tonic-gate get_clearance(int callnumber)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
1397c478bd9Sstevel@tonic-gate 	char	*tab;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (sema_trywait(&common_sema) == 0) {
1427c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, NULL);
1437c478bd9Sstevel@tonic-gate 		return (0);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	switch (callnumber) {
1477c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
1487c478bd9Sstevel@tonic-gate 			tab = "ldap";
1497c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 		default:
1527c478bd9Sstevel@tonic-gate 			logit("Internal Error: get_clearance\n");
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (sema_trywait(table_sema) == 0) {
1577c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, (void*)1);
1587c478bd9Sstevel@tonic-gate 		return (0);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_CANT_FIND) {
1627c478bd9Sstevel@tonic-gate 		logit("get_clearance: throttling load for %s table\n", tab);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	return (-1);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate int
1697c478bd9Sstevel@tonic-gate release_clearance(int callnumber)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	int	which;
1727c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	(void) thr_getspecific(lookup_state_key, (void**)&which);
1757c478bd9Sstevel@tonic-gate 	if (which == 0) /* from common pool */ {
1767c478bd9Sstevel@tonic-gate 		(void) sema_post(&common_sema);
1777c478bd9Sstevel@tonic-gate 		return (0);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	switch (callnumber) {
1817c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
1827c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
1837c478bd9Sstevel@tonic-gate 			break;
1847c478bd9Sstevel@tonic-gate 		default:
1857c478bd9Sstevel@tonic-gate 			logit("Internal Error: release_clearance\n");
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	(void) sema_post(table_sema);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	return (0);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static mutex_t		create_lock;
1957c478bd9Sstevel@tonic-gate static int		num_servers = 0;
1967c478bd9Sstevel@tonic-gate static thread_key_t	server_key;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * Bind a TSD value to a server thread. This enables the destructor to
2017c478bd9Sstevel@tonic-gate  * be called if/when this thread exits.  This would be a programming error,
2027c478bd9Sstevel@tonic-gate  * but better safe than sorry.
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2067c478bd9Sstevel@tonic-gate static void *
2077c478bd9Sstevel@tonic-gate server_tsd_bind(void *arg)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	static void	*value = 0;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/*
2127c478bd9Sstevel@tonic-gate 	 * disable cancellation to prevent hangs when server
2137c478bd9Sstevel@tonic-gate 	 * threads disappear
2147c478bd9Sstevel@tonic-gate 	 */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	(void) thr_setspecific(server_key, value);
2177c478bd9Sstevel@tonic-gate 	door_return(NULL, 0, NULL, 0);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	return (value);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * Server threads are created here.
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2277c478bd9Sstevel@tonic-gate static void
2287c478bd9Sstevel@tonic-gate server_create(door_info_t *dip)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
2317c478bd9Sstevel@tonic-gate 	if (++num_servers > MAX_SERVER_THREADS) {
2327c478bd9Sstevel@tonic-gate 		num_servers--;
2337c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&create_lock);
2347c478bd9Sstevel@tonic-gate 		return;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
2377c478bd9Sstevel@tonic-gate 	(void) thr_create(NULL, 0, server_tsd_bind, NULL,
2387c478bd9Sstevel@tonic-gate 		THR_BOUND|THR_DETACHED, NULL);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  * Server thread are destroyed here
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2467c478bd9Sstevel@tonic-gate static void
2477c478bd9Sstevel@tonic-gate server_destroy(void *arg)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
2507c478bd9Sstevel@tonic-gate 	num_servers--;
2517c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
254*a506a34cSth160488 int
2557c478bd9Sstevel@tonic-gate main(int argc, char ** argv)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	int			did;
2587c478bd9Sstevel@tonic-gate 	int			opt;
2597c478bd9Sstevel@tonic-gate 	int			errflg = 0;
2607c478bd9Sstevel@tonic-gate 	int			showstats = 0;
2617c478bd9Sstevel@tonic-gate 	int			doset = 0;
2627c478bd9Sstevel@tonic-gate 	int			dofg = 0;
2637c478bd9Sstevel@tonic-gate 	struct stat		buf;
2647c478bd9Sstevel@tonic-gate 	sigset_t		myset;
2657c478bd9Sstevel@tonic-gate 	struct sigaction	sighupaction;
2667c478bd9Sstevel@tonic-gate 	static void		client_killserver();
2677c478bd9Sstevel@tonic-gate 	int			debug_level = 0;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	/* setup for localization */
2707c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2717c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	openlog("ldap_cachemgr", LOG_PID, LOG_DAEMON);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (chdir(NSLDAPDIRECTORY) < 0) {
2767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("chdir(\"%s\") failed: %s\n"),
2777c478bd9Sstevel@tonic-gate 			NSLDAPDIRECTORY, strerror(errno));
2787c478bd9Sstevel@tonic-gate 		exit(1);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * Correctly set file mode creation mask, so to make the new files
2837c478bd9Sstevel@tonic-gate 	 * created for door calls being readable by all.
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	(void) umask(0);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 *  Special case non-root user here -	he/she/they/it can just print
2897c478bd9Sstevel@tonic-gate 	 *					stats
2907c478bd9Sstevel@tonic-gate 	 */
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if (geteuid()) {
2937c478bd9Sstevel@tonic-gate 		if (argc != 2 || strcmp(argv[1], "-g")) {
2947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2957c478bd9Sstevel@tonic-gate 			    gettext("Must be root to use any option "
2967c478bd9Sstevel@tonic-gate 			    "other than -g.\n\n"));
2977c478bd9Sstevel@tonic-gate 			usage(argv[0]);
2987c478bd9Sstevel@tonic-gate 		}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 		if ((__ns_ldap_cache_ping() != SUCCESS) ||
3017c478bd9Sstevel@tonic-gate 		    (client_getadmin(&current_admin) != 0)) {
3027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3037c478bd9Sstevel@tonic-gate 				gettext("%s doesn't appear to be running.\n"),
3047c478bd9Sstevel@tonic-gate 				argv[0]);
3057c478bd9Sstevel@tonic-gate 			exit(1);
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 		(void) client_showstats(&current_admin);
3087c478bd9Sstevel@tonic-gate 		exit(0);
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 *  Determine if there is already a daemon running
3157c478bd9Sstevel@tonic-gate 	 */
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	will_become_server = (__ns_ldap_cache_ping() != SUCCESS);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 *  load normal config file
3217c478bd9Sstevel@tonic-gate 	 */
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (will_become_server) {
3247c478bd9Sstevel@tonic-gate 		static const ldap_stat_t defaults = {
3257c478bd9Sstevel@tonic-gate 			0,		/* stat */
3267c478bd9Sstevel@tonic-gate 			DEFAULTTTL};	/* ttl */
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat = defaults;
3297c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, LOGFILE);
3307c478bd9Sstevel@tonic-gate 	} else {
3317c478bd9Sstevel@tonic-gate 		if (client_getadmin(&current_admin)) {
3327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Cannot contact %s "
3337c478bd9Sstevel@tonic-gate 				"properly(?)\n"), argv[0]);
3347c478bd9Sstevel@tonic-gate 			exit(1);
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate #ifndef SLP
3397c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgl:r:d:")) != EOF) {
3407c478bd9Sstevel@tonic-gate #else
3417c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgs:l:r:d:")) != EOF) {
3427c478bd9Sstevel@tonic-gate #endif /* SLP */
3437c478bd9Sstevel@tonic-gate 		ldap_stat_t	*cache;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		switch (opt) {
3467c478bd9Sstevel@tonic-gate 		case 'K':
3477c478bd9Sstevel@tonic-gate 			client_killserver();
3487c478bd9Sstevel@tonic-gate 			exit(0);
3497c478bd9Sstevel@tonic-gate 			break;
3507c478bd9Sstevel@tonic-gate 		case 'g':
3517c478bd9Sstevel@tonic-gate 			showstats++;
3527c478bd9Sstevel@tonic-gate 			break;
3537c478bd9Sstevel@tonic-gate 		case 'f':
3547c478bd9Sstevel@tonic-gate 			dofg++;
3557c478bd9Sstevel@tonic-gate 			break;
3567c478bd9Sstevel@tonic-gate 		case 'r':
3577c478bd9Sstevel@tonic-gate 			doset++;
3587c478bd9Sstevel@tonic-gate 			cache = getcacheptr("ldap");
3597c478bd9Sstevel@tonic-gate 			if (!optarg) {
3607c478bd9Sstevel@tonic-gate 				errflg++;
3617c478bd9Sstevel@tonic-gate 				break;
3627c478bd9Sstevel@tonic-gate 			}
3637c478bd9Sstevel@tonic-gate 			cache->ldap_ttl = atoi(optarg);
3647c478bd9Sstevel@tonic-gate 			break;
3657c478bd9Sstevel@tonic-gate 		case 'l':
3667c478bd9Sstevel@tonic-gate 			doset++;
3677c478bd9Sstevel@tonic-gate 			(void) strlcpy(current_admin.logfile,
3687c478bd9Sstevel@tonic-gate 				optarg, sizeof (current_admin.logfile));
3697c478bd9Sstevel@tonic-gate 			break;
3707c478bd9Sstevel@tonic-gate 		case 'd':
3717c478bd9Sstevel@tonic-gate 			doset++;
3727c478bd9Sstevel@tonic-gate 			debug_level = atoi(optarg);
3737c478bd9Sstevel@tonic-gate 			break;
3747c478bd9Sstevel@tonic-gate #ifdef SLP
3757c478bd9Sstevel@tonic-gate 		case 's':	/* undocumented: use dynamic (SLP) config */
3767c478bd9Sstevel@tonic-gate 			use_slp = 1;
3777c478bd9Sstevel@tonic-gate 			break;
3787c478bd9Sstevel@tonic-gate #endif /* SLP */
3797c478bd9Sstevel@tonic-gate 		default:
3807c478bd9Sstevel@tonic-gate 			errflg++;
3817c478bd9Sstevel@tonic-gate 			break;
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (errflg)
3867c478bd9Sstevel@tonic-gate 	    usage(argv[0]);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	/*
3897c478bd9Sstevel@tonic-gate 	 * will not show statistics if no daemon running
3907c478bd9Sstevel@tonic-gate 	 */
3917c478bd9Sstevel@tonic-gate 	if (will_become_server && showstats) {
3927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3937c478bd9Sstevel@tonic-gate 			gettext("%s doesn't appear to be running.\n"),
3947c478bd9Sstevel@tonic-gate 				argv[0]);
3957c478bd9Sstevel@tonic-gate 		exit(1);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (!will_become_server) {
3997c478bd9Sstevel@tonic-gate 		if (showstats) {
4007c478bd9Sstevel@tonic-gate 			(void) client_showstats(&current_admin);
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 		if (doset) {
4037c478bd9Sstevel@tonic-gate 			current_admin.debug_level = debug_level;
4047c478bd9Sstevel@tonic-gate 			if (client_setadmin(&current_admin) < 0) {
4057c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4067c478bd9Sstevel@tonic-gate 					gettext("Error during admin call\n"));
4077c478bd9Sstevel@tonic-gate 				exit(1);
4087c478bd9Sstevel@tonic-gate 			}
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 		if (!showstats && !doset) {
4117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4127c478bd9Sstevel@tonic-gate 			gettext("%s already running....use '%s "
4137c478bd9Sstevel@tonic-gate 				"-K' to stop\n"), argv[0], argv[0]);
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 		exit(0);
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 *   daemon from here on
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if (debug_level) {
4237c478bd9Sstevel@tonic-gate 		/*
4247c478bd9Sstevel@tonic-gate 		 * we're debugging...
4257c478bd9Sstevel@tonic-gate 		 */
4267c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
4277c478bd9Sstevel@tonic-gate 			/*
4287c478bd9Sstevel@tonic-gate 			 * no specified log file
4297c478bd9Sstevel@tonic-gate 			 */
4307c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, LOGFILE);
4317c478bd9Sstevel@tonic-gate 		else
4327c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
4337c478bd9Sstevel@tonic-gate 				current_admin.logfile);
4347c478bd9Sstevel@tonic-gate 		/*
4357c478bd9Sstevel@tonic-gate 		 * validate the range of debug level number
4367c478bd9Sstevel@tonic-gate 		 * and set the number to current_admin.debug_level
4377c478bd9Sstevel@tonic-gate 		 */
4387c478bd9Sstevel@tonic-gate 		if (cachemgr_set_dl(&current_admin, debug_level) < 0) {
4397c478bd9Sstevel@tonic-gate 				/*
4407c478bd9Sstevel@tonic-gate 				 * print error messages to the screen
4417c478bd9Sstevel@tonic-gate 				 * cachemgr_set_dl prints msgs to cachemgr.log
4427c478bd9Sstevel@tonic-gate 				 * only
4437c478bd9Sstevel@tonic-gate 				 */
4447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4457c478bd9Sstevel@tonic-gate 				gettext("Incorrect Debug Level: %d\n"
4467c478bd9Sstevel@tonic-gate 				"It should be between %d and %d\n"),
4477c478bd9Sstevel@tonic-gate 				debug_level, DBG_OFF, MAXDEBUG);
4487c478bd9Sstevel@tonic-gate 			exit(-1);
4497c478bd9Sstevel@tonic-gate 		}
4507c478bd9Sstevel@tonic-gate 	} else {
4517c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
4527c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, "/dev/null");
4537c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
4547c478bd9Sstevel@tonic-gate 				current_admin.logfile);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (dofg == 0)
4587c478bd9Sstevel@tonic-gate 		detachfromtty(argv[0]);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	/*
4617c478bd9Sstevel@tonic-gate 	 * perform some initialization
4627c478bd9Sstevel@tonic-gate 	 */
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	initialize_lookup_clearance();
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if (getldap_init() != 0)
4677c478bd9Sstevel@tonic-gate 		exit(-1);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * Establish our own server thread pool
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	door_server_create(server_create);
4747c478bd9Sstevel@tonic-gate 	if (thr_keycreate(&server_key, server_destroy) != 0) {
4757c478bd9Sstevel@tonic-gate 		logit("thr_keycreate() call failed\n");
4767c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4777c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_keycreate() call failed"));
4787c478bd9Sstevel@tonic-gate 		perror("thr_keycreate");
4797c478bd9Sstevel@tonic-gate 		exit(-1);
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	/*
4837c478bd9Sstevel@tonic-gate 	 * Create a door
4847c478bd9Sstevel@tonic-gate 	 */
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if ((did = door_create(switcher, LDAP_CACHE_DOOR_COOKIE,
4877c478bd9Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
4887c478bd9Sstevel@tonic-gate 		logit("door_create() call failed\n");
4897c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
4907c478bd9Sstevel@tonic-gate 			"ldap_cachemgr: door_create() call failed"));
4917c478bd9Sstevel@tonic-gate 		perror("door_create");
4927c478bd9Sstevel@tonic-gate 		exit(-1);
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	/*
4967c478bd9Sstevel@tonic-gate 	 * bind to file system
4977c478bd9Sstevel@tonic-gate 	 */
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	if (stat(LDAP_CACHE_DOOR, &buf) < 0) {
5007c478bd9Sstevel@tonic-gate 		int	newfd;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		if ((newfd = creat(LDAP_CACHE_DOOR, 0444)) < 0) {
5037c478bd9Sstevel@tonic-gate 			logit("Cannot create %s:%s\n",
5047c478bd9Sstevel@tonic-gate 				LDAP_CACHE_DOOR,
5057c478bd9Sstevel@tonic-gate 				strerror(errno));
5067c478bd9Sstevel@tonic-gate 			exit(1);
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 		(void) close(newfd);
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (fattach(did, LDAP_CACHE_DOOR) < 0) {
5127c478bd9Sstevel@tonic-gate 		if ((errno != EBUSY) ||
5137c478bd9Sstevel@tonic-gate 		    (fdetach(LDAP_CACHE_DOOR) <  0) ||
5147c478bd9Sstevel@tonic-gate 		    (fattach(did, LDAP_CACHE_DOOR) < 0)) {
5157c478bd9Sstevel@tonic-gate 			logit("fattach() call failed\n");
5167c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
5177c478bd9Sstevel@tonic-gate 				"ldap_cachemgr: fattach() call failed"));
5187c478bd9Sstevel@tonic-gate 			perror("fattach");
5197c478bd9Sstevel@tonic-gate 			exit(2);
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/* catch SIGHUP revalid signals */
5247c478bd9Sstevel@tonic-gate 	sighupaction.sa_handler = getldap_revalidate;
5257c478bd9Sstevel@tonic-gate 	sighupaction.sa_flags = 0;
5267c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sighupaction.sa_mask);
5277c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&myset);
5287c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGHUP);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (sigaction(SIGHUP, &sighupaction, NULL) < 0) {
5317c478bd9Sstevel@tonic-gate 		logit("sigaction() call failed\n");
5327c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5337c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: sigaction() call failed"));
5347c478bd9Sstevel@tonic-gate 		perror("sigaction");
5357c478bd9Sstevel@tonic-gate 		exit(1);
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_BLOCK, &myset, NULL) < 0) {
5397c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
5407c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5417c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_sigsetmask() call failed"));
5427c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
5437c478bd9Sstevel@tonic-gate 		exit(1);
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	/*
5477c478bd9Sstevel@tonic-gate 	 *  kick off revalidate threads only if ttl != 0
5487c478bd9Sstevel@tonic-gate 	 */
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL, (void *(*)(void*))getldap_refresh,
5517c478bd9Sstevel@tonic-gate 		0, 0, NULL) != 0) {
5527c478bd9Sstevel@tonic-gate 		logit("thr_create() call failed\n");
5537c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5547c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_create() call failed"));
5557c478bd9Sstevel@tonic-gate 		perror("thr_create");
5567c478bd9Sstevel@tonic-gate 		exit(1);
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	/*
5607c478bd9Sstevel@tonic-gate 	 *  kick off the thread which refreshes the server info
5617c478bd9Sstevel@tonic-gate 	 */
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL, (void *(*)(void*))getldap_serverInfo_refresh,
5647c478bd9Sstevel@tonic-gate 		0, 0, NULL) != 0) {
5657c478bd9Sstevel@tonic-gate 		logit("thr_create() call failed\n");
5667c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5677c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_create() call failed"));
5687c478bd9Sstevel@tonic-gate 		perror("thr_create");
5697c478bd9Sstevel@tonic-gate 		exit(1);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate #ifdef SLP
5737c478bd9Sstevel@tonic-gate 	if (use_slp) {
5747c478bd9Sstevel@tonic-gate 		/* kick off SLP discovery thread */
5757c478bd9Sstevel@tonic-gate 		if (thr_create(NULL, NULL, (void *(*)(void *))discover,
5767c478bd9Sstevel@tonic-gate 			(void *)&refresh, 0, NULL) != 0) {
5777c478bd9Sstevel@tonic-gate 			logit("thr_create() call failed\n");
5787c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("ldap_cachemgr: thr_create() "
5797c478bd9Sstevel@tonic-gate 				"call failed"));
5807c478bd9Sstevel@tonic-gate 			perror("thr_create");
5817c478bd9Sstevel@tonic-gate 			exit(1);
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate #endif /* SLP */
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_UNBLOCK, &myset, NULL) < 0) {
5877c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
5887c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
5897c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: the_sigsetmask() call failed"));
5907c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
5917c478bd9Sstevel@tonic-gate 		exit(1);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
5957c478bd9Sstevel@tonic-gate 	while (1) {
5967c478bd9Sstevel@tonic-gate 		(void) pause();
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6027c478bd9Sstevel@tonic-gate static void
6037c478bd9Sstevel@tonic-gate switcher(void *cookie, char *argp, size_t arg_size,
6047c478bd9Sstevel@tonic-gate     door_desc_t *dp, uint_t n_desc)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	dataunion		u;
6077c478bd9Sstevel@tonic-gate 	ldap_call_t	*ptr = (ldap_call_t *)argp;
6087c478bd9Sstevel@tonic-gate 	door_cred_t	dc;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	if (argp == DOOR_UNREF_DATA) {
6117c478bd9Sstevel@tonic-gate 		logit("Door Slam... invalid door param\n");
6127c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: Door Slam... "
6137c478bd9Sstevel@tonic-gate 			"invalid door param"));
6147c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Door Slam... invalid door param\n"));
6157c478bd9Sstevel@tonic-gate 		exit(0);
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (ptr == NULL) { /* empty door call */
6197c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0); /* return the favor */
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	switch (ptr->ldap_callnumber) {
6237c478bd9Sstevel@tonic-gate 	case NULLCALL:
6247c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_return_code = SUCCESS;
6257c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_bufferbytesused = sizeof (ldap_return_t);
6267c478bd9Sstevel@tonic-gate 		break;
6277c478bd9Sstevel@tonic-gate 	case GETLDAPCONFIG:
6287c478bd9Sstevel@tonic-gate 		getldap_lookup(&u.data.ldap_ret, ptr);
6297c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
6307c478bd9Sstevel@tonic-gate 		break;
6317c478bd9Sstevel@tonic-gate 	case GETADMIN:
6327c478bd9Sstevel@tonic-gate 		(void) getadmin(&u.data.ldap_ret);
6337c478bd9Sstevel@tonic-gate 		break;
6347c478bd9Sstevel@tonic-gate 	case SETADMIN:
6357c478bd9Sstevel@tonic-gate 	case KILLSERVER:
6367c478bd9Sstevel@tonic-gate 		if (door_cred(&dc) < 0) {
6377c478bd9Sstevel@tonic-gate 			logit("door_cred() call failed\n");
6387c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("ldap_cachemgr: door_cred() "
6397c478bd9Sstevel@tonic-gate 				"call failed"));
6407c478bd9Sstevel@tonic-gate 			perror("door_cred");
6417c478bd9Sstevel@tonic-gate 			break;
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate 		if (dc.dc_euid != 0 && ptr->ldap_callnumber == SETADMIN) {
6447c478bd9Sstevel@tonic-gate 			logit("SETADMIN call failed (cred): caller "
6457c478bd9Sstevel@tonic-gate 			    "pid %ld, uid %ld, euid %ld\n",
6467c478bd9Sstevel@tonic-gate 			    dc.dc_pid, dc.dc_ruid, dc.dc_euid);
6477c478bd9Sstevel@tonic-gate 			u.data.ldap_ret.ldap_return_code = NOTFOUND;
6487c478bd9Sstevel@tonic-gate 			break;
6497c478bd9Sstevel@tonic-gate 		}
6507c478bd9Sstevel@tonic-gate 		if (ptr->ldap_callnumber == KILLSERVER) {
6517c478bd9Sstevel@tonic-gate 			logit("ldap_cachemgr received KILLSERVER cmd from "
6527c478bd9Sstevel@tonic-gate 			    "pid %ld, uid %ld, euid %ld\n",
6537c478bd9Sstevel@tonic-gate 			    dc.dc_pid, dc.dc_ruid, dc.dc_euid);
6547c478bd9Sstevel@tonic-gate 			exit(0);
6557c478bd9Sstevel@tonic-gate 		} else {
6567c478bd9Sstevel@tonic-gate 			(void) setadmin(&u.data.ldap_ret, ptr);
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 		break;
6597c478bd9Sstevel@tonic-gate 	case GETLDAPSERVER:
6607c478bd9Sstevel@tonic-gate 		getldap_getserver(&u.data.ldap_ret, ptr);
6617c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
6627c478bd9Sstevel@tonic-gate 		break;
6637c478bd9Sstevel@tonic-gate 	case GETCACHE:
6647c478bd9Sstevel@tonic-gate 		getldap_get_cacheData(&u.data.ldap_ret, ptr);
6657c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
6667c478bd9Sstevel@tonic-gate 		break;
6677c478bd9Sstevel@tonic-gate 	case SETCACHE:
6687c478bd9Sstevel@tonic-gate 		getldap_set_cacheData(&u.data.ldap_ret, ptr);
6697c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
6707c478bd9Sstevel@tonic-gate 		break;
6717c478bd9Sstevel@tonic-gate 	case GETCACHESTAT:
6727c478bd9Sstevel@tonic-gate 		getldap_get_cacheStat(&u.data.ldap_ret);
6737c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
6747c478bd9Sstevel@tonic-gate 		break;
6757c478bd9Sstevel@tonic-gate 	default:
6767c478bd9Sstevel@tonic-gate 		logit("Unknown ldap service door call op %d\n",
6777c478bd9Sstevel@tonic-gate 		    ptr->ldap_callnumber);
6787c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_return_code = -99;
6797c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_bufferbytesused = sizeof (ldap_return_t);
6807c478bd9Sstevel@tonic-gate 		break;
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 	door_return((char *)&u.data,
6837c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_bufferbytesused, NULL, 0);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate static void
6877c478bd9Sstevel@tonic-gate usage(char *s)
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6907c478bd9Sstevel@tonic-gate 		gettext("Usage: %s [-d debug_level] [-l logfilename]\n"), s);
6917c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-K] "
6927c478bd9Sstevel@tonic-gate 					"[-r revalidate_interval] "));
6937c478bd9Sstevel@tonic-gate #ifndef SLP
6947c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g]\n"));
6957c478bd9Sstevel@tonic-gate #else
6967c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g] [-s]\n"));
6977c478bd9Sstevel@tonic-gate #endif /* SLP */
6987c478bd9Sstevel@tonic-gate 	exit(1);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate static int	logfd = -1;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate static int
7057c478bd9Sstevel@tonic-gate cachemgr_set_lf(admin_t *ptr, char *logfile)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	int	newlogfd;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	/*
7107c478bd9Sstevel@tonic-gate 	 *  we don't really want to try and open the log file
7117c478bd9Sstevel@tonic-gate 	 *  /dev/null since that will fail w/ our security fixes
7127c478bd9Sstevel@tonic-gate 	 */
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if (logfile == NULL || *logfile == 0) {
7157c478bd9Sstevel@tonic-gate 		/*EMPTY*/;
7167c478bd9Sstevel@tonic-gate 	} else if (strcmp(logfile, "/dev/null") == 0) {
7177c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, "/dev/null");
7187c478bd9Sstevel@tonic-gate 		(void) close(logfd);
7197c478bd9Sstevel@tonic-gate 		logfd = -1;
7207c478bd9Sstevel@tonic-gate 	} else {
7217c478bd9Sstevel@tonic-gate 		if ((newlogfd =
7227c478bd9Sstevel@tonic-gate 			open(logfile, O_EXCL|O_WRONLY|O_CREAT, 0644)) < 0) {
7237c478bd9Sstevel@tonic-gate 			/*
7247c478bd9Sstevel@tonic-gate 			 * File already exists... now we need to get cute
7257c478bd9Sstevel@tonic-gate 			 * since opening a file in a world-writeable directory
7267c478bd9Sstevel@tonic-gate 			 * safely is hard = it could be a hard link or a
7277c478bd9Sstevel@tonic-gate 			 * symbolic link to a system file.
7287c478bd9Sstevel@tonic-gate 			 *
7297c478bd9Sstevel@tonic-gate 			 */
7307c478bd9Sstevel@tonic-gate 			struct stat	before;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 			if (lstat(logfile, &before) < 0) {
7337c478bd9Sstevel@tonic-gate 				logit("Cannot open new logfile \"%s\": %sn",
7347c478bd9Sstevel@tonic-gate 					logfile, strerror(errno));
7357c478bd9Sstevel@tonic-gate 				return (-1);
7367c478bd9Sstevel@tonic-gate 			}
7377c478bd9Sstevel@tonic-gate 			if (S_ISREG(before.st_mode) &&	/* no symbolic links */
7387c478bd9Sstevel@tonic-gate 			    (before.st_nlink == 1) &&	/* no hard links */
7397c478bd9Sstevel@tonic-gate 			    (before.st_uid == 0)) {	/* owned by root */
7407c478bd9Sstevel@tonic-gate 				if ((newlogfd =
7417c478bd9Sstevel@tonic-gate 				    open(logfile,
7427c478bd9Sstevel@tonic-gate 				    O_APPEND|O_WRONLY, 0644)) < 0) {
7437c478bd9Sstevel@tonic-gate 					logit("Cannot open new logfile "
7447c478bd9Sstevel@tonic-gate 						"\"%s\": %s\n",
7457c478bd9Sstevel@tonic-gate 						logfile, strerror(errno));
7467c478bd9Sstevel@tonic-gate 					return (-1);
7477c478bd9Sstevel@tonic-gate 				}
7487c478bd9Sstevel@tonic-gate 			} else {
7497c478bd9Sstevel@tonic-gate 				logit("Cannot use specified logfile "
7507c478bd9Sstevel@tonic-gate 				    "\"%s\": file is/has links or isn't "
7517c478bd9Sstevel@tonic-gate 				    "owned by root\n", logfile);
7527c478bd9Sstevel@tonic-gate 				return (-1);
7537c478bd9Sstevel@tonic-gate 			}
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 		(void) strlcpy(ptr->logfile, logfile, sizeof (ptr->logfile));
7567c478bd9Sstevel@tonic-gate 		(void) close(logfd);
7577c478bd9Sstevel@tonic-gate 		logfd = newlogfd;
7587c478bd9Sstevel@tonic-gate 		logit("Starting ldap_cachemgr, logfile %s\n", logfile);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 	return (0);
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
7647c478bd9Sstevel@tonic-gate void
7657c478bd9Sstevel@tonic-gate logit(char *format, ...)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	static mutex_t	loglock;
7687c478bd9Sstevel@tonic-gate 	struct timeval	tv;
7697c478bd9Sstevel@tonic-gate 	char		buffer[BUFSIZ];
7707c478bd9Sstevel@tonic-gate 	va_list		ap;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	va_start(ap, format);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if (logfd >= 0) {
7757c478bd9Sstevel@tonic-gate 		int	safechars;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tv, NULL);
7787c478bd9Sstevel@tonic-gate 		(void) ctime_r(&tv.tv_sec, buffer, BUFSIZ);
7797c478bd9Sstevel@tonic-gate 		(void) snprintf(buffer+19, BUFSIZE, ".%.4ld	",
7807c478bd9Sstevel@tonic-gate 			tv.tv_usec/100);
7817c478bd9Sstevel@tonic-gate 		safechars = sizeof (buffer) - 30;
7827c478bd9Sstevel@tonic-gate 		if (vsnprintf(buffer+25, safechars, format, ap) > safechars)
7837c478bd9Sstevel@tonic-gate 			(void) strcat(buffer, "...\n");
7847c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&loglock);
7857c478bd9Sstevel@tonic-gate 		(void) write(logfd, buffer, strlen(buffer));
7867c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&loglock);
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 	va_end(ap);
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate void
7937c478bd9Sstevel@tonic-gate do_update(ldap_call_t *in)
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	dataunion		u;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	switch (in->ldap_callnumber) {
7987c478bd9Sstevel@tonic-gate 	case GETLDAPCONFIG:
7997c478bd9Sstevel@tonic-gate 		getldap_lookup(&u.data.ldap_ret, in);
8007c478bd9Sstevel@tonic-gate 		break;
8017c478bd9Sstevel@tonic-gate 	default:
8027c478bd9Sstevel@tonic-gate 		assert(0);
8037c478bd9Sstevel@tonic-gate 		break;
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	free(in);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate static int
8117c478bd9Sstevel@tonic-gate client_getadmin(admin_t *ptr)
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate 	dataunion		u;
8147c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
8157c478bd9Sstevel@tonic-gate 	int		ndata;
8167c478bd9Sstevel@tonic-gate 	int		adata;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETADMIN;
8197c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
8207c478bd9Sstevel@tonic-gate 	adata = sizeof (u.data);
8217c478bd9Sstevel@tonic-gate 	dptr = &u.data;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
8247c478bd9Sstevel@tonic-gate 		return (-1);
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 	(void) memcpy(ptr, dptr->ldap_ret.ldap_u.buff, sizeof (*ptr));
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	return (0);
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate static int
8327c478bd9Sstevel@tonic-gate getadmin(ldap_return_t *out)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
8357c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (current_admin);
8367c478bd9Sstevel@tonic-gate 	(void) memcpy(out->ldap_u.buff, &current_admin, sizeof (current_admin));
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	return (0);
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate static int
8437c478bd9Sstevel@tonic-gate setadmin(ldap_return_t *out, ldap_call_t *ptr)
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	admin_t	*new;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
8487c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (ldap_return_t);
8497c478bd9Sstevel@tonic-gate 	new = (admin_t *)ptr->ldap_u.domainname;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	/*
8527c478bd9Sstevel@tonic-gate 	 *  global admin stuff
8537c478bd9Sstevel@tonic-gate 	 */
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if ((cachemgr_set_lf(&current_admin, new->logfile) < 0) ||
8567c478bd9Sstevel@tonic-gate 	    cachemgr_set_dl(&current_admin, new->debug_level) < 0) {
8577c478bd9Sstevel@tonic-gate 		out->ldap_return_code = NOTFOUND;
8587c478bd9Sstevel@tonic-gate 		return (-1);
8597c478bd9Sstevel@tonic-gate 	}
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	if (cachemgr_set_ttl(&current_admin.ldap_stat,
8627c478bd9Sstevel@tonic-gate 			"ldap",
8637c478bd9Sstevel@tonic-gate 			new->ldap_stat.ldap_ttl) < 0) {
8647c478bd9Sstevel@tonic-gate 		out->ldap_return_code = NOTFOUND;
8657c478bd9Sstevel@tonic-gate 		return (-1);
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	return (0);
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate static void
8747c478bd9Sstevel@tonic-gate client_killserver()
8757c478bd9Sstevel@tonic-gate {
8767c478bd9Sstevel@tonic-gate 	dataunion		u;
8777c478bd9Sstevel@tonic-gate 	ldap_data_t		*dptr;
8787c478bd9Sstevel@tonic-gate 	int			ndata;
8797c478bd9Sstevel@tonic-gate 	int			adata;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = KILLSERVER;
8827c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
8837c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
8847c478bd9Sstevel@tonic-gate 	dptr = &u.data;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	__ns_ldap_trydoorcall(&dptr, &ndata, &adata);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate static int
8917c478bd9Sstevel@tonic-gate client_setadmin(admin_t *ptr)
8927c478bd9Sstevel@tonic-gate {
8937c478bd9Sstevel@tonic-gate 	dataunion		u;
8947c478bd9Sstevel@tonic-gate 	ldap_data_t		*dptr;
8957c478bd9Sstevel@tonic-gate 	int			ndata;
8967c478bd9Sstevel@tonic-gate 	int			adata;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = SETADMIN;
8997c478bd9Sstevel@tonic-gate 	(void) memcpy(u.data.ldap_call.ldap_u.domainname, ptr, sizeof (*ptr));
9007c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
9017c478bd9Sstevel@tonic-gate 	adata = sizeof (*ptr);
9027c478bd9Sstevel@tonic-gate 	dptr = &u.data;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
9057c478bd9Sstevel@tonic-gate 		return (-1);
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	return (0);
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate static int
9127c478bd9Sstevel@tonic-gate client_showstats(admin_t *ptr)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate 	dataunion	u;
9157c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
9167c478bd9Sstevel@tonic-gate 	int		ndata;
9177c478bd9Sstevel@tonic-gate 	int		adata;
9187c478bd9Sstevel@tonic-gate 	char		*rbuf, *sptr, *rest;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	/*
9217c478bd9Sstevel@tonic-gate 	 * print admin data
9227c478bd9Sstevel@tonic-gate 	 */
9237c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr configuration:\n"));
9247c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server debug level %10d\n"), ptr->debug_level);
9257c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server log file\t\"%s\"\n"), ptr->logfile);
9267c478bd9Sstevel@tonic-gate 	(void) printf(gettext("number of calls to ldapcachemgr %10d\n"),
9277c478bd9Sstevel@tonic-gate 		ptr->ldap_stat.ldap_numbercalls);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	/*
9307c478bd9Sstevel@tonic-gate 	 * get cache data statistics
9317c478bd9Sstevel@tonic-gate 	 */
9327c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETCACHESTAT;
9337c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
9347c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
9357c478bd9Sstevel@tonic-gate 	dptr = &u.data;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
9387c478bd9Sstevel@tonic-gate 		(void) printf(
9397c478bd9Sstevel@tonic-gate 			gettext("\nCache data statistics not available!\n"));
9407c478bd9Sstevel@tonic-gate 		return (0);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	/*
9447c478bd9Sstevel@tonic-gate 	 * print cache data statistics line by line
9457c478bd9Sstevel@tonic-gate 	 */
9467c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr cache data statistics:\n"));
9477c478bd9Sstevel@tonic-gate 	rbuf = dptr->ldap_ret.ldap_u.buff;
9487c478bd9Sstevel@tonic-gate 	sptr = strtok_r(rbuf, DOORLINESEP, &rest);
9497c478bd9Sstevel@tonic-gate 	for (;;) {
9507c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", sptr);
9517c478bd9Sstevel@tonic-gate 		sptr = strtok_r(NULL, DOORLINESEP, &rest);
9527c478bd9Sstevel@tonic-gate 		if (sptr == NULL)
9537c478bd9Sstevel@tonic-gate 			break;
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 	return (0);
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate /*
9607c478bd9Sstevel@tonic-gate  * detach from tty
9617c478bd9Sstevel@tonic-gate  */
9627c478bd9Sstevel@tonic-gate static void
9637c478bd9Sstevel@tonic-gate detachfromtty(char *pgm)
9647c478bd9Sstevel@tonic-gate {
9657c478bd9Sstevel@tonic-gate 	int 	status;
9667c478bd9Sstevel@tonic-gate 	pid_t	pid, wret;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	(void) close(0);
9697c478bd9Sstevel@tonic-gate 	(void) close(1);
9707c478bd9Sstevel@tonic-gate 	/*
9717c478bd9Sstevel@tonic-gate 	 * Block the SIGUSR1 signal
9727c478bd9Sstevel@tonic-gate 	 * just in case that the child
9737c478bd9Sstevel@tonic-gate 	 * process may run faster than
9747c478bd9Sstevel@tonic-gate 	 * the parent process and
9757c478bd9Sstevel@tonic-gate 	 * send this signal before
9767c478bd9Sstevel@tonic-gate 	 * the signal handler is ready
9777c478bd9Sstevel@tonic-gate 	 * in the parent process.
9787c478bd9Sstevel@tonic-gate 	 * This error will cause the parent
9797c478bd9Sstevel@tonic-gate 	 * to exit with the User Signal 1
9807c478bd9Sstevel@tonic-gate 	 * exit code (144).
9817c478bd9Sstevel@tonic-gate 	 */
9827c478bd9Sstevel@tonic-gate 	(void) sighold(SIGUSR1);
9837c478bd9Sstevel@tonic-gate 	pid = fork1();
9847c478bd9Sstevel@tonic-gate 	switch (pid) {
9857c478bd9Sstevel@tonic-gate 		case (pid_t)-1:
9867c478bd9Sstevel@tonic-gate 			logit("detachfromtty(): fork1() call failed\n");
9877c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9887c478bd9Sstevel@tonic-gate 					gettext("%s: fork1() call failed.\n"),
9897c478bd9Sstevel@tonic-gate 					pgm);
9907c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
9917c478bd9Sstevel@tonic-gate 				gettext("ldap_cachemgr: fork1() call failed."));
9927c478bd9Sstevel@tonic-gate 			exit(1);
9937c478bd9Sstevel@tonic-gate 			break;
9947c478bd9Sstevel@tonic-gate 		case 0:
9957c478bd9Sstevel@tonic-gate 			/*
9967c478bd9Sstevel@tonic-gate 			 * child process does not
9977c478bd9Sstevel@tonic-gate 			 * need to worry about
9987c478bd9Sstevel@tonic-gate 			 * the SIGUSR1 signal
9997c478bd9Sstevel@tonic-gate 			 */
10007c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
10017c478bd9Sstevel@tonic-gate 			(void) close(2);
10027c478bd9Sstevel@tonic-gate 			break;
10037c478bd9Sstevel@tonic-gate 		default:
10047c478bd9Sstevel@tonic-gate 			/*
10057c478bd9Sstevel@tonic-gate 			 * Wait forever until the child process
10067c478bd9Sstevel@tonic-gate 			 * has exited, or has signalled that at
10077c478bd9Sstevel@tonic-gate 			 * least one server in the server list
10087c478bd9Sstevel@tonic-gate 			 * is up.
10097c478bd9Sstevel@tonic-gate 			 */
10107c478bd9Sstevel@tonic-gate 			if (signal(SIGUSR1, sig_ok_to_exit) == SIG_ERR) {
10117c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
10127c478bd9Sstevel@tonic-gate 					"can't set up signal handler to "
10137c478bd9Sstevel@tonic-gate 					" catch SIGUSR1.\n");
10147c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10157c478bd9Sstevel@tonic-gate 					gettext("%s: signal() call failed.\n"),
10167c478bd9Sstevel@tonic-gate 					pgm);
10177c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, gettext("ldap_cachemgr: "
10187c478bd9Sstevel@tonic-gate 					"can't set up signal handler to "
10197c478bd9Sstevel@tonic-gate 					" catch SIGUSR1."));
10207c478bd9Sstevel@tonic-gate 				exit(1);
10217c478bd9Sstevel@tonic-gate 			}
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 			/*
10247c478bd9Sstevel@tonic-gate 			 * now unblock the SIGUSR1 signal
10257c478bd9Sstevel@tonic-gate 			 * to handle the pending or
10267c478bd9Sstevel@tonic-gate 			 * soon to arrive SIGUSR1 signal
10277c478bd9Sstevel@tonic-gate 			 */
10287c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
10297c478bd9Sstevel@tonic-gate 			wret = waitpid(pid, &status, 0);
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 			if (wret == -1) {
10327c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
10337c478bd9Sstevel@tonic-gate 					"waitpid() call failed\n");
10347c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10357c478bd9Sstevel@tonic-gate 					gettext("%s: waitpid() call failed.\n"),
10367c478bd9Sstevel@tonic-gate 					pgm);
10377c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
10387c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: waitpid() "
10397c478bd9Sstevel@tonic-gate 						"call failed."));
10407c478bd9Sstevel@tonic-gate 				exit(1);
10417c478bd9Sstevel@tonic-gate 			}
10427c478bd9Sstevel@tonic-gate 			if (wret != pid) {
10437c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
10447c478bd9Sstevel@tonic-gate 					"waitpid() returned %ld when "
10457c478bd9Sstevel@tonic-gate 					"child pid was %ld\n",
10467c478bd9Sstevel@tonic-gate 					wret, pid);
10477c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10487c478bd9Sstevel@tonic-gate 					gettext(
10497c478bd9Sstevel@tonic-gate 					"%s: waitpid() returned %ld when "
10507c478bd9Sstevel@tonic-gate 					"child pid was %ld.\n"),
10517c478bd9Sstevel@tonic-gate 					pgm, wret, pid);
10527c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
10537c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: waitpid() "
10547c478bd9Sstevel@tonic-gate 						"returned different "
10557c478bd9Sstevel@tonic-gate 						"child pid."));
10567c478bd9Sstevel@tonic-gate 				exit(1);
10577c478bd9Sstevel@tonic-gate 			}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 			/* evaluate return status */
10607c478bd9Sstevel@tonic-gate 			if (WIFEXITED(status)) {
10617c478bd9Sstevel@tonic-gate 				if (WEXITSTATUS(status) == 0) {
10627c478bd9Sstevel@tonic-gate 					exit(0);
10637c478bd9Sstevel@tonic-gate 				}
10647c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
10657c478bd9Sstevel@tonic-gate 					"child failed (rc = %d).\n",
10667c478bd9Sstevel@tonic-gate 					WEXITSTATUS(status));
10677c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10687c478bd9Sstevel@tonic-gate 					gettext("%s: failed. Please see "
10697c478bd9Sstevel@tonic-gate 					"syslog for details.\n"),
10707c478bd9Sstevel@tonic-gate 					pgm);
10717c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
10727c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: failed "
10737c478bd9Sstevel@tonic-gate 					"(rc = %d)."),
10747c478bd9Sstevel@tonic-gate 					WEXITSTATUS(status));
10757c478bd9Sstevel@tonic-gate 			} else if (WIFSIGNALED(status)) {
10767c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
10777c478bd9Sstevel@tonic-gate 					"child terminated by signal %d.\n",
10787c478bd9Sstevel@tonic-gate 					WTERMSIG(status));
10797c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10807c478bd9Sstevel@tonic-gate 				gettext("%s: terminated by signal %d.\n"),
10817c478bd9Sstevel@tonic-gate 					pgm, WTERMSIG(status));
10827c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
10837c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: terminated by "
10847c478bd9Sstevel@tonic-gate 					"signal %d.\n"),
10857c478bd9Sstevel@tonic-gate 					WTERMSIG(status));
10867c478bd9Sstevel@tonic-gate 			} else if (WCOREDUMP(status)) {
10877c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): child core dumped.\n"),
10887c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10897c478bd9Sstevel@tonic-gate 					gettext("%s: core dumped.\n"),
10907c478bd9Sstevel@tonic-gate 					pgm);
10917c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
10927c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: "
10937c478bd9Sstevel@tonic-gate 						"core dumped.\n"));
10947c478bd9Sstevel@tonic-gate 			}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 			exit(1);
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 	(void) setsid();
10997c478bd9Sstevel@tonic-gate 	if (open("/dev/null", O_RDWR, 0) != -1) {
11007c478bd9Sstevel@tonic-gate 		(void) dup(0);
11017c478bd9Sstevel@tonic-gate 		(void) dup(0);
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate }
1104