xref: /titanic_54/usr/src/cmd/ldapcachemgr/cachemgr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Simple doors ldap cache daemon
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <signal.h>
36*7c478bd9Sstevel@tonic-gate #include <door.h>
37*7c478bd9Sstevel@tonic-gate #include <time.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <libintl.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
43*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
44*7c478bd9Sstevel@tonic-gate #include <errno.h>
45*7c478bd9Sstevel@tonic-gate #include <pthread.h>
46*7c478bd9Sstevel@tonic-gate #include <thread.h>
47*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
48*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
49*7c478bd9Sstevel@tonic-gate #include <assert.h>
50*7c478bd9Sstevel@tonic-gate #include <unistd.h>
51*7c478bd9Sstevel@tonic-gate #include <memory.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
53*7c478bd9Sstevel@tonic-gate #include <syslog.h>
54*7c478bd9Sstevel@tonic-gate #include <locale.h>	/* LC_ALL */
55*7c478bd9Sstevel@tonic-gate #include "cachemgr.h"
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static void	detachfromtty();
58*7c478bd9Sstevel@tonic-gate admin_t		current_admin;
59*7c478bd9Sstevel@tonic-gate static int	will_become_server;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static void switcher(void *cookie, char *argp, size_t arg_size,
62*7c478bd9Sstevel@tonic-gate 			door_desc_t *dp, uint_t n_desc);
63*7c478bd9Sstevel@tonic-gate static void usage(char *s);
64*7c478bd9Sstevel@tonic-gate static int cachemgr_set_lf(admin_t *ptr, char *logfile);
65*7c478bd9Sstevel@tonic-gate static int client_getadmin(admin_t *ptr);
66*7c478bd9Sstevel@tonic-gate static int getadmin(ldap_return_t *out);
67*7c478bd9Sstevel@tonic-gate static int setadmin(ldap_return_t *out, ldap_call_t *ptr);
68*7c478bd9Sstevel@tonic-gate static  int client_setadmin(admin_t *ptr);
69*7c478bd9Sstevel@tonic-gate static int client_showstats(admin_t *ptr);
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate #ifdef SLP
72*7c478bd9Sstevel@tonic-gate int			use_slp = 0;
73*7c478bd9Sstevel@tonic-gate static unsigned int	refresh = 10800;	/* dynamic discovery interval */
74*7c478bd9Sstevel@tonic-gate #endif /* SLP */
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate static ldap_stat_t *
77*7c478bd9Sstevel@tonic-gate getcacheptr(char *s)
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	static const char *caches[1] = {"ldap"};
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (strncmp(caches[0], s, strlen(caches[0])) == 0)
82*7c478bd9Sstevel@tonic-gate 		return (&current_admin.ldap_stat);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	return (NULL);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate char *
88*7c478bd9Sstevel@tonic-gate getcacheopt(char *s)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	while (*s && *s != ',')
91*7c478bd9Sstevel@tonic-gate 		s++;
92*7c478bd9Sstevel@tonic-gate 	return ((*s == ',') ? (s + 1) : NULL);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate /*
96*7c478bd9Sstevel@tonic-gate  *  This is here to prevent the ldap_cachemgr becomes
97*7c478bd9Sstevel@tonic-gate  *  daemonlized to early to soon during boot time.
98*7c478bd9Sstevel@tonic-gate  *  This causes problems during boot when automounter
99*7c478bd9Sstevel@tonic-gate  *  and others try to use libsldap before ldap_cachemgr
100*7c478bd9Sstevel@tonic-gate  *  finishes walking the server list.
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate static void
103*7c478bd9Sstevel@tonic-gate sig_ok_to_exit(int signo)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	if (signo == SIGUSR1) {
106*7c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): parent exiting...\n");
107*7c478bd9Sstevel@tonic-gate 		exit(0);
108*7c478bd9Sstevel@tonic-gate 	} else {
109*7c478bd9Sstevel@tonic-gate 		logit("sig_ok_to_exit(): invalid signal(%d) received.\n",
110*7c478bd9Sstevel@tonic-gate 			signo);
111*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: "
112*7c478bd9Sstevel@tonic-gate 			"invalid signal(%d) received."), signo);
113*7c478bd9Sstevel@tonic-gate 		exit(1);
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate #define	LDAP_TABLES		1	/* ldap */
117*7c478bd9Sstevel@tonic-gate #define	TABLE_THREADS		10
118*7c478bd9Sstevel@tonic-gate #define	COMMON_THREADS		20
119*7c478bd9Sstevel@tonic-gate #define	CACHE_MISS_THREADS	(COMMON_THREADS + LDAP_TABLES * TABLE_THREADS)
120*7c478bd9Sstevel@tonic-gate #define	CACHE_HIT_THREADS	20
121*7c478bd9Sstevel@tonic-gate #define	MAX_SERVER_THREADS	(CACHE_HIT_THREADS + CACHE_MISS_THREADS)
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate static sema_t common_sema;
124*7c478bd9Sstevel@tonic-gate static sema_t ldap_sema;
125*7c478bd9Sstevel@tonic-gate static thread_key_t lookup_state_key;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate static void
128*7c478bd9Sstevel@tonic-gate initialize_lookup_clearance()
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate 	(void) thr_keycreate(&lookup_state_key, NULL);
131*7c478bd9Sstevel@tonic-gate 	(void) sema_init(&common_sema, COMMON_THREADS, USYNC_THREAD, 0);
132*7c478bd9Sstevel@tonic-gate 	(void) sema_init(&ldap_sema, TABLE_THREADS, USYNC_THREAD, 0);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate int
136*7c478bd9Sstevel@tonic-gate get_clearance(int callnumber)
137*7c478bd9Sstevel@tonic-gate {
138*7c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
139*7c478bd9Sstevel@tonic-gate 	char	*tab;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	if (sema_trywait(&common_sema) == 0) {
142*7c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, NULL);
143*7c478bd9Sstevel@tonic-gate 		return (0);
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	switch (callnumber) {
147*7c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
148*7c478bd9Sstevel@tonic-gate 			tab = "ldap";
149*7c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
150*7c478bd9Sstevel@tonic-gate 			break;
151*7c478bd9Sstevel@tonic-gate 		default:
152*7c478bd9Sstevel@tonic-gate 			logit("Internal Error: get_clearance\n");
153*7c478bd9Sstevel@tonic-gate 			break;
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	if (sema_trywait(table_sema) == 0) {
157*7c478bd9Sstevel@tonic-gate 		(void) thr_setspecific(lookup_state_key, (void*)1);
158*7c478bd9Sstevel@tonic-gate 		return (0);
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_CANT_FIND) {
162*7c478bd9Sstevel@tonic-gate 		logit("get_clearance: throttling load for %s table\n", tab);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	return (-1);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate int
169*7c478bd9Sstevel@tonic-gate release_clearance(int callnumber)
170*7c478bd9Sstevel@tonic-gate {
171*7c478bd9Sstevel@tonic-gate 	int	which;
172*7c478bd9Sstevel@tonic-gate 	sema_t	*table_sema = NULL;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	(void) thr_getspecific(lookup_state_key, (void**)&which);
175*7c478bd9Sstevel@tonic-gate 	if (which == 0) /* from common pool */ {
176*7c478bd9Sstevel@tonic-gate 		(void) sema_post(&common_sema);
177*7c478bd9Sstevel@tonic-gate 		return (0);
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	switch (callnumber) {
181*7c478bd9Sstevel@tonic-gate 		case GETLDAPCONFIG:
182*7c478bd9Sstevel@tonic-gate 			table_sema = &ldap_sema;
183*7c478bd9Sstevel@tonic-gate 			break;
184*7c478bd9Sstevel@tonic-gate 		default:
185*7c478bd9Sstevel@tonic-gate 			logit("Internal Error: release_clearance\n");
186*7c478bd9Sstevel@tonic-gate 			break;
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate 	(void) sema_post(table_sema);
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	return (0);
191*7c478bd9Sstevel@tonic-gate }
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate static mutex_t		create_lock;
195*7c478bd9Sstevel@tonic-gate static int		num_servers = 0;
196*7c478bd9Sstevel@tonic-gate static thread_key_t	server_key;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate /*
200*7c478bd9Sstevel@tonic-gate  * Bind a TSD value to a server thread. This enables the destructor to
201*7c478bd9Sstevel@tonic-gate  * be called if/when this thread exits.  This would be a programming error,
202*7c478bd9Sstevel@tonic-gate  * but better safe than sorry.
203*7c478bd9Sstevel@tonic-gate  */
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
206*7c478bd9Sstevel@tonic-gate static void *
207*7c478bd9Sstevel@tonic-gate server_tsd_bind(void *arg)
208*7c478bd9Sstevel@tonic-gate {
209*7c478bd9Sstevel@tonic-gate 	static void	*value = 0;
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	/*
212*7c478bd9Sstevel@tonic-gate 	 * disable cancellation to prevent hangs when server
213*7c478bd9Sstevel@tonic-gate 	 * threads disappear
214*7c478bd9Sstevel@tonic-gate 	 */
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	(void) thr_setspecific(server_key, value);
217*7c478bd9Sstevel@tonic-gate 	door_return(NULL, 0, NULL, 0);
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	return (value);
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate /*
223*7c478bd9Sstevel@tonic-gate  * Server threads are created here.
224*7c478bd9Sstevel@tonic-gate  */
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
227*7c478bd9Sstevel@tonic-gate static void
228*7c478bd9Sstevel@tonic-gate server_create(door_info_t *dip)
229*7c478bd9Sstevel@tonic-gate {
230*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
231*7c478bd9Sstevel@tonic-gate 	if (++num_servers > MAX_SERVER_THREADS) {
232*7c478bd9Sstevel@tonic-gate 		num_servers--;
233*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&create_lock);
234*7c478bd9Sstevel@tonic-gate 		return;
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
237*7c478bd9Sstevel@tonic-gate 	(void) thr_create(NULL, 0, server_tsd_bind, NULL,
238*7c478bd9Sstevel@tonic-gate 		THR_BOUND|THR_DETACHED, NULL);
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate /*
242*7c478bd9Sstevel@tonic-gate  * Server thread are destroyed here
243*7c478bd9Sstevel@tonic-gate  */
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
246*7c478bd9Sstevel@tonic-gate static void
247*7c478bd9Sstevel@tonic-gate server_destroy(void *arg)
248*7c478bd9Sstevel@tonic-gate {
249*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
250*7c478bd9Sstevel@tonic-gate 	num_servers--;
251*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate void
255*7c478bd9Sstevel@tonic-gate main(int argc, char ** argv)
256*7c478bd9Sstevel@tonic-gate {
257*7c478bd9Sstevel@tonic-gate 	int			did;
258*7c478bd9Sstevel@tonic-gate 	int			opt;
259*7c478bd9Sstevel@tonic-gate 	int			errflg = 0;
260*7c478bd9Sstevel@tonic-gate 	int			showstats = 0;
261*7c478bd9Sstevel@tonic-gate 	int			doset = 0;
262*7c478bd9Sstevel@tonic-gate 	int			dofg = 0;
263*7c478bd9Sstevel@tonic-gate 	struct stat		buf;
264*7c478bd9Sstevel@tonic-gate 	sigset_t		myset;
265*7c478bd9Sstevel@tonic-gate 	struct sigaction	sighupaction;
266*7c478bd9Sstevel@tonic-gate 	static void		client_killserver();
267*7c478bd9Sstevel@tonic-gate 	int			debug_level = 0;
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	/* setup for localization */
270*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
271*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	openlog("ldap_cachemgr", LOG_PID, LOG_DAEMON);
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	if (chdir(NSLDAPDIRECTORY) < 0) {
276*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("chdir(\"%s\") failed: %s\n"),
277*7c478bd9Sstevel@tonic-gate 			NSLDAPDIRECTORY, strerror(errno));
278*7c478bd9Sstevel@tonic-gate 		exit(1);
279*7c478bd9Sstevel@tonic-gate 	}
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	/*
282*7c478bd9Sstevel@tonic-gate 	 * Correctly set file mode creation mask, so to make the new files
283*7c478bd9Sstevel@tonic-gate 	 * created for door calls being readable by all.
284*7c478bd9Sstevel@tonic-gate 	 */
285*7c478bd9Sstevel@tonic-gate 	(void) umask(0);
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	/*
288*7c478bd9Sstevel@tonic-gate 	 *  Special case non-root user here -	he/she/they/it can just print
289*7c478bd9Sstevel@tonic-gate 	 *					stats
290*7c478bd9Sstevel@tonic-gate 	 */
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 	if (geteuid()) {
293*7c478bd9Sstevel@tonic-gate 		if (argc != 2 || strcmp(argv[1], "-g")) {
294*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
295*7c478bd9Sstevel@tonic-gate 			    gettext("Must be root to use any option "
296*7c478bd9Sstevel@tonic-gate 			    "other than -g.\n\n"));
297*7c478bd9Sstevel@tonic-gate 			usage(argv[0]);
298*7c478bd9Sstevel@tonic-gate 		}
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 		if ((__ns_ldap_cache_ping() != SUCCESS) ||
301*7c478bd9Sstevel@tonic-gate 		    (client_getadmin(&current_admin) != 0)) {
302*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
303*7c478bd9Sstevel@tonic-gate 				gettext("%s doesn't appear to be running.\n"),
304*7c478bd9Sstevel@tonic-gate 				argv[0]);
305*7c478bd9Sstevel@tonic-gate 			exit(1);
306*7c478bd9Sstevel@tonic-gate 		}
307*7c478bd9Sstevel@tonic-gate 		(void) client_showstats(&current_admin);
308*7c478bd9Sstevel@tonic-gate 		exit(0);
309*7c478bd9Sstevel@tonic-gate 	}
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 	/*
314*7c478bd9Sstevel@tonic-gate 	 *  Determine if there is already a daemon running
315*7c478bd9Sstevel@tonic-gate 	 */
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 	will_become_server = (__ns_ldap_cache_ping() != SUCCESS);
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	/*
320*7c478bd9Sstevel@tonic-gate 	 *  load normal config file
321*7c478bd9Sstevel@tonic-gate 	 */
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	if (will_become_server) {
324*7c478bd9Sstevel@tonic-gate 		static const ldap_stat_t defaults = {
325*7c478bd9Sstevel@tonic-gate 			0,		/* stat */
326*7c478bd9Sstevel@tonic-gate 			DEFAULTTTL};	/* ttl */
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat = defaults;
329*7c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, LOGFILE);
330*7c478bd9Sstevel@tonic-gate 	} else {
331*7c478bd9Sstevel@tonic-gate 		if (client_getadmin(&current_admin)) {
332*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Cannot contact %s "
333*7c478bd9Sstevel@tonic-gate 				"properly(?)\n"), argv[0]);
334*7c478bd9Sstevel@tonic-gate 			exit(1);
335*7c478bd9Sstevel@tonic-gate 		}
336*7c478bd9Sstevel@tonic-gate 	}
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate #ifndef SLP
339*7c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgl:r:d:")) != EOF) {
340*7c478bd9Sstevel@tonic-gate #else
341*7c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "fKgs:l:r:d:")) != EOF) {
342*7c478bd9Sstevel@tonic-gate #endif /* SLP */
343*7c478bd9Sstevel@tonic-gate 		ldap_stat_t	*cache;
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 		switch (opt) {
346*7c478bd9Sstevel@tonic-gate 		case 'K':
347*7c478bd9Sstevel@tonic-gate 			client_killserver();
348*7c478bd9Sstevel@tonic-gate 			exit(0);
349*7c478bd9Sstevel@tonic-gate 			break;
350*7c478bd9Sstevel@tonic-gate 		case 'g':
351*7c478bd9Sstevel@tonic-gate 			showstats++;
352*7c478bd9Sstevel@tonic-gate 			break;
353*7c478bd9Sstevel@tonic-gate 		case 'f':
354*7c478bd9Sstevel@tonic-gate 			dofg++;
355*7c478bd9Sstevel@tonic-gate 			break;
356*7c478bd9Sstevel@tonic-gate 		case 'r':
357*7c478bd9Sstevel@tonic-gate 			doset++;
358*7c478bd9Sstevel@tonic-gate 			cache = getcacheptr("ldap");
359*7c478bd9Sstevel@tonic-gate 			if (!optarg) {
360*7c478bd9Sstevel@tonic-gate 				errflg++;
361*7c478bd9Sstevel@tonic-gate 				break;
362*7c478bd9Sstevel@tonic-gate 			}
363*7c478bd9Sstevel@tonic-gate 			cache->ldap_ttl = atoi(optarg);
364*7c478bd9Sstevel@tonic-gate 			break;
365*7c478bd9Sstevel@tonic-gate 		case 'l':
366*7c478bd9Sstevel@tonic-gate 			doset++;
367*7c478bd9Sstevel@tonic-gate 			(void) strlcpy(current_admin.logfile,
368*7c478bd9Sstevel@tonic-gate 				optarg, sizeof (current_admin.logfile));
369*7c478bd9Sstevel@tonic-gate 			break;
370*7c478bd9Sstevel@tonic-gate 		case 'd':
371*7c478bd9Sstevel@tonic-gate 			doset++;
372*7c478bd9Sstevel@tonic-gate 			debug_level = atoi(optarg);
373*7c478bd9Sstevel@tonic-gate 			break;
374*7c478bd9Sstevel@tonic-gate #ifdef SLP
375*7c478bd9Sstevel@tonic-gate 		case 's':	/* undocumented: use dynamic (SLP) config */
376*7c478bd9Sstevel@tonic-gate 			use_slp = 1;
377*7c478bd9Sstevel@tonic-gate 			break;
378*7c478bd9Sstevel@tonic-gate #endif /* SLP */
379*7c478bd9Sstevel@tonic-gate 		default:
380*7c478bd9Sstevel@tonic-gate 			errflg++;
381*7c478bd9Sstevel@tonic-gate 			break;
382*7c478bd9Sstevel@tonic-gate 		}
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	if (errflg)
386*7c478bd9Sstevel@tonic-gate 	    usage(argv[0]);
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	/*
389*7c478bd9Sstevel@tonic-gate 	 * will not show statistics if no daemon running
390*7c478bd9Sstevel@tonic-gate 	 */
391*7c478bd9Sstevel@tonic-gate 	if (will_become_server && showstats) {
392*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
393*7c478bd9Sstevel@tonic-gate 			gettext("%s doesn't appear to be running.\n"),
394*7c478bd9Sstevel@tonic-gate 				argv[0]);
395*7c478bd9Sstevel@tonic-gate 		exit(1);
396*7c478bd9Sstevel@tonic-gate 	}
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 	if (!will_become_server) {
399*7c478bd9Sstevel@tonic-gate 		if (showstats) {
400*7c478bd9Sstevel@tonic-gate 			(void) client_showstats(&current_admin);
401*7c478bd9Sstevel@tonic-gate 		}
402*7c478bd9Sstevel@tonic-gate 		if (doset) {
403*7c478bd9Sstevel@tonic-gate 			current_admin.debug_level = debug_level;
404*7c478bd9Sstevel@tonic-gate 			if (client_setadmin(&current_admin) < 0) {
405*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
406*7c478bd9Sstevel@tonic-gate 					gettext("Error during admin call\n"));
407*7c478bd9Sstevel@tonic-gate 				exit(1);
408*7c478bd9Sstevel@tonic-gate 			}
409*7c478bd9Sstevel@tonic-gate 		}
410*7c478bd9Sstevel@tonic-gate 		if (!showstats && !doset) {
411*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
412*7c478bd9Sstevel@tonic-gate 			gettext("%s already running....use '%s "
413*7c478bd9Sstevel@tonic-gate 				"-K' to stop\n"), argv[0], argv[0]);
414*7c478bd9Sstevel@tonic-gate 		}
415*7c478bd9Sstevel@tonic-gate 		exit(0);
416*7c478bd9Sstevel@tonic-gate 	}
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	/*
419*7c478bd9Sstevel@tonic-gate 	 *   daemon from here on
420*7c478bd9Sstevel@tonic-gate 	 */
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	if (debug_level) {
423*7c478bd9Sstevel@tonic-gate 		/*
424*7c478bd9Sstevel@tonic-gate 		 * we're debugging...
425*7c478bd9Sstevel@tonic-gate 		 */
426*7c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
427*7c478bd9Sstevel@tonic-gate 			/*
428*7c478bd9Sstevel@tonic-gate 			 * no specified log file
429*7c478bd9Sstevel@tonic-gate 			 */
430*7c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, LOGFILE);
431*7c478bd9Sstevel@tonic-gate 		else
432*7c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
433*7c478bd9Sstevel@tonic-gate 				current_admin.logfile);
434*7c478bd9Sstevel@tonic-gate 		/*
435*7c478bd9Sstevel@tonic-gate 		 * validate the range of debug level number
436*7c478bd9Sstevel@tonic-gate 		 * and set the number to current_admin.debug_level
437*7c478bd9Sstevel@tonic-gate 		 */
438*7c478bd9Sstevel@tonic-gate 		if (cachemgr_set_dl(&current_admin, debug_level) < 0) {
439*7c478bd9Sstevel@tonic-gate 				/*
440*7c478bd9Sstevel@tonic-gate 				 * print error messages to the screen
441*7c478bd9Sstevel@tonic-gate 				 * cachemgr_set_dl prints msgs to cachemgr.log
442*7c478bd9Sstevel@tonic-gate 				 * only
443*7c478bd9Sstevel@tonic-gate 				 */
444*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
445*7c478bd9Sstevel@tonic-gate 				gettext("Incorrect Debug Level: %d\n"
446*7c478bd9Sstevel@tonic-gate 				"It should be between %d and %d\n"),
447*7c478bd9Sstevel@tonic-gate 				debug_level, DBG_OFF, MAXDEBUG);
448*7c478bd9Sstevel@tonic-gate 			exit(-1);
449*7c478bd9Sstevel@tonic-gate 		}
450*7c478bd9Sstevel@tonic-gate 	} else {
451*7c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
452*7c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, "/dev/null");
453*7c478bd9Sstevel@tonic-gate 			(void) cachemgr_set_lf(&current_admin,
454*7c478bd9Sstevel@tonic-gate 				current_admin.logfile);
455*7c478bd9Sstevel@tonic-gate 	}
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate 	if (dofg == 0)
458*7c478bd9Sstevel@tonic-gate 		detachfromtty(argv[0]);
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	/*
461*7c478bd9Sstevel@tonic-gate 	 * perform some initialization
462*7c478bd9Sstevel@tonic-gate 	 */
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	initialize_lookup_clearance();
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate 	if (getldap_init() != 0)
467*7c478bd9Sstevel@tonic-gate 		exit(-1);
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 	/*
470*7c478bd9Sstevel@tonic-gate 	 * Establish our own server thread pool
471*7c478bd9Sstevel@tonic-gate 	 */
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 	door_server_create(server_create);
474*7c478bd9Sstevel@tonic-gate 	if (thr_keycreate(&server_key, server_destroy) != 0) {
475*7c478bd9Sstevel@tonic-gate 		logit("thr_keycreate() call failed\n");
476*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
477*7c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_keycreate() call failed"));
478*7c478bd9Sstevel@tonic-gate 		perror("thr_keycreate");
479*7c478bd9Sstevel@tonic-gate 		exit(-1);
480*7c478bd9Sstevel@tonic-gate 	}
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 	/*
483*7c478bd9Sstevel@tonic-gate 	 * Create a door
484*7c478bd9Sstevel@tonic-gate 	 */
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	if ((did = door_create(switcher, LDAP_CACHE_DOOR_COOKIE,
487*7c478bd9Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
488*7c478bd9Sstevel@tonic-gate 		logit("door_create() call failed\n");
489*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
490*7c478bd9Sstevel@tonic-gate 			"ldap_cachemgr: door_create() call failed"));
491*7c478bd9Sstevel@tonic-gate 		perror("door_create");
492*7c478bd9Sstevel@tonic-gate 		exit(-1);
493*7c478bd9Sstevel@tonic-gate 	}
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate 	/*
496*7c478bd9Sstevel@tonic-gate 	 * bind to file system
497*7c478bd9Sstevel@tonic-gate 	 */
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate 	if (stat(LDAP_CACHE_DOOR, &buf) < 0) {
500*7c478bd9Sstevel@tonic-gate 		int	newfd;
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 		if ((newfd = creat(LDAP_CACHE_DOOR, 0444)) < 0) {
503*7c478bd9Sstevel@tonic-gate 			logit("Cannot create %s:%s\n",
504*7c478bd9Sstevel@tonic-gate 				LDAP_CACHE_DOOR,
505*7c478bd9Sstevel@tonic-gate 				strerror(errno));
506*7c478bd9Sstevel@tonic-gate 			exit(1);
507*7c478bd9Sstevel@tonic-gate 		}
508*7c478bd9Sstevel@tonic-gate 		(void) close(newfd);
509*7c478bd9Sstevel@tonic-gate 	}
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate 	if (fattach(did, LDAP_CACHE_DOOR) < 0) {
512*7c478bd9Sstevel@tonic-gate 		if ((errno != EBUSY) ||
513*7c478bd9Sstevel@tonic-gate 		    (fdetach(LDAP_CACHE_DOOR) <  0) ||
514*7c478bd9Sstevel@tonic-gate 		    (fattach(did, LDAP_CACHE_DOOR) < 0)) {
515*7c478bd9Sstevel@tonic-gate 			logit("fattach() call failed\n");
516*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
517*7c478bd9Sstevel@tonic-gate 				"ldap_cachemgr: fattach() call failed"));
518*7c478bd9Sstevel@tonic-gate 			perror("fattach");
519*7c478bd9Sstevel@tonic-gate 			exit(2);
520*7c478bd9Sstevel@tonic-gate 		}
521*7c478bd9Sstevel@tonic-gate 	}
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate 	/* catch SIGHUP revalid signals */
524*7c478bd9Sstevel@tonic-gate 	sighupaction.sa_handler = getldap_revalidate;
525*7c478bd9Sstevel@tonic-gate 	sighupaction.sa_flags = 0;
526*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sighupaction.sa_mask);
527*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&myset);
528*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGHUP);
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate 	if (sigaction(SIGHUP, &sighupaction, NULL) < 0) {
531*7c478bd9Sstevel@tonic-gate 		logit("sigaction() call failed\n");
532*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
533*7c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: sigaction() call failed"));
534*7c478bd9Sstevel@tonic-gate 		perror("sigaction");
535*7c478bd9Sstevel@tonic-gate 		exit(1);
536*7c478bd9Sstevel@tonic-gate 	}
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_BLOCK, &myset, NULL) < 0) {
539*7c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
540*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
541*7c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_sigsetmask() call failed"));
542*7c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
543*7c478bd9Sstevel@tonic-gate 		exit(1);
544*7c478bd9Sstevel@tonic-gate 	}
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate 	/*
547*7c478bd9Sstevel@tonic-gate 	 *  kick off revalidate threads only if ttl != 0
548*7c478bd9Sstevel@tonic-gate 	 */
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL, (void *(*)(void*))getldap_refresh,
551*7c478bd9Sstevel@tonic-gate 		0, 0, NULL) != 0) {
552*7c478bd9Sstevel@tonic-gate 		logit("thr_create() call failed\n");
553*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
554*7c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_create() call failed"));
555*7c478bd9Sstevel@tonic-gate 		perror("thr_create");
556*7c478bd9Sstevel@tonic-gate 		exit(1);
557*7c478bd9Sstevel@tonic-gate 	}
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 	/*
560*7c478bd9Sstevel@tonic-gate 	 *  kick off the thread which refreshes the server info
561*7c478bd9Sstevel@tonic-gate 	 */
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL, (void *(*)(void*))getldap_serverInfo_refresh,
564*7c478bd9Sstevel@tonic-gate 		0, 0, NULL) != 0) {
565*7c478bd9Sstevel@tonic-gate 		logit("thr_create() call failed\n");
566*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
567*7c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: thr_create() call failed"));
568*7c478bd9Sstevel@tonic-gate 		perror("thr_create");
569*7c478bd9Sstevel@tonic-gate 		exit(1);
570*7c478bd9Sstevel@tonic-gate 	}
571*7c478bd9Sstevel@tonic-gate 
572*7c478bd9Sstevel@tonic-gate #ifdef SLP
573*7c478bd9Sstevel@tonic-gate 	if (use_slp) {
574*7c478bd9Sstevel@tonic-gate 		/* kick off SLP discovery thread */
575*7c478bd9Sstevel@tonic-gate 		if (thr_create(NULL, NULL, (void *(*)(void *))discover,
576*7c478bd9Sstevel@tonic-gate 			(void *)&refresh, 0, NULL) != 0) {
577*7c478bd9Sstevel@tonic-gate 			logit("thr_create() call failed\n");
578*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("ldap_cachemgr: thr_create() "
579*7c478bd9Sstevel@tonic-gate 				"call failed"));
580*7c478bd9Sstevel@tonic-gate 			perror("thr_create");
581*7c478bd9Sstevel@tonic-gate 			exit(1);
582*7c478bd9Sstevel@tonic-gate 		}
583*7c478bd9Sstevel@tonic-gate 	}
584*7c478bd9Sstevel@tonic-gate #endif /* SLP */
585*7c478bd9Sstevel@tonic-gate 
586*7c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_UNBLOCK, &myset, NULL) < 0) {
587*7c478bd9Sstevel@tonic-gate 		logit("thr_sigsetmask() call failed\n");
588*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
589*7c478bd9Sstevel@tonic-gate 			gettext("ldap_cachemgr: the_sigsetmask() call failed"));
590*7c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
591*7c478bd9Sstevel@tonic-gate 		exit(1);
592*7c478bd9Sstevel@tonic-gate 	}
593*7c478bd9Sstevel@tonic-gate 
594*7c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
595*7c478bd9Sstevel@tonic-gate 	while (1) {
596*7c478bd9Sstevel@tonic-gate 		(void) pause();
597*7c478bd9Sstevel@tonic-gate 	}
598*7c478bd9Sstevel@tonic-gate }
599*7c478bd9Sstevel@tonic-gate 
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
602*7c478bd9Sstevel@tonic-gate static void
603*7c478bd9Sstevel@tonic-gate switcher(void *cookie, char *argp, size_t arg_size,
604*7c478bd9Sstevel@tonic-gate     door_desc_t *dp, uint_t n_desc)
605*7c478bd9Sstevel@tonic-gate {
606*7c478bd9Sstevel@tonic-gate 	dataunion		u;
607*7c478bd9Sstevel@tonic-gate 	ldap_call_t	*ptr = (ldap_call_t *)argp;
608*7c478bd9Sstevel@tonic-gate 	door_cred_t	dc;
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate 	if (argp == DOOR_UNREF_DATA) {
611*7c478bd9Sstevel@tonic-gate 		logit("Door Slam... invalid door param\n");
612*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("ldap_cachemgr: Door Slam... "
613*7c478bd9Sstevel@tonic-gate 			"invalid door param"));
614*7c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Door Slam... invalid door param\n"));
615*7c478bd9Sstevel@tonic-gate 		exit(0);
616*7c478bd9Sstevel@tonic-gate 	}
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL) { /* empty door call */
619*7c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0); /* return the favor */
620*7c478bd9Sstevel@tonic-gate 	}
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 	switch (ptr->ldap_callnumber) {
623*7c478bd9Sstevel@tonic-gate 	case NULLCALL:
624*7c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_return_code = SUCCESS;
625*7c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_bufferbytesused = sizeof (ldap_return_t);
626*7c478bd9Sstevel@tonic-gate 		break;
627*7c478bd9Sstevel@tonic-gate 	case GETLDAPCONFIG:
628*7c478bd9Sstevel@tonic-gate 		getldap_lookup(&u.data.ldap_ret, ptr);
629*7c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
630*7c478bd9Sstevel@tonic-gate 		break;
631*7c478bd9Sstevel@tonic-gate 	case GETADMIN:
632*7c478bd9Sstevel@tonic-gate 		(void) getadmin(&u.data.ldap_ret);
633*7c478bd9Sstevel@tonic-gate 		break;
634*7c478bd9Sstevel@tonic-gate 	case SETADMIN:
635*7c478bd9Sstevel@tonic-gate 	case KILLSERVER:
636*7c478bd9Sstevel@tonic-gate 		if (door_cred(&dc) < 0) {
637*7c478bd9Sstevel@tonic-gate 			logit("door_cred() call failed\n");
638*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("ldap_cachemgr: door_cred() "
639*7c478bd9Sstevel@tonic-gate 				"call failed"));
640*7c478bd9Sstevel@tonic-gate 			perror("door_cred");
641*7c478bd9Sstevel@tonic-gate 			break;
642*7c478bd9Sstevel@tonic-gate 		}
643*7c478bd9Sstevel@tonic-gate 		if (dc.dc_euid != 0 && ptr->ldap_callnumber == SETADMIN) {
644*7c478bd9Sstevel@tonic-gate 			logit("SETADMIN call failed (cred): caller "
645*7c478bd9Sstevel@tonic-gate 			    "pid %ld, uid %ld, euid %ld\n",
646*7c478bd9Sstevel@tonic-gate 			    dc.dc_pid, dc.dc_ruid, dc.dc_euid);
647*7c478bd9Sstevel@tonic-gate 			u.data.ldap_ret.ldap_return_code = NOTFOUND;
648*7c478bd9Sstevel@tonic-gate 			break;
649*7c478bd9Sstevel@tonic-gate 		}
650*7c478bd9Sstevel@tonic-gate 		if (ptr->ldap_callnumber == KILLSERVER) {
651*7c478bd9Sstevel@tonic-gate 			logit("ldap_cachemgr received KILLSERVER cmd from "
652*7c478bd9Sstevel@tonic-gate 			    "pid %ld, uid %ld, euid %ld\n",
653*7c478bd9Sstevel@tonic-gate 			    dc.dc_pid, dc.dc_ruid, dc.dc_euid);
654*7c478bd9Sstevel@tonic-gate 			exit(0);
655*7c478bd9Sstevel@tonic-gate 		} else {
656*7c478bd9Sstevel@tonic-gate 			(void) setadmin(&u.data.ldap_ret, ptr);
657*7c478bd9Sstevel@tonic-gate 		}
658*7c478bd9Sstevel@tonic-gate 		break;
659*7c478bd9Sstevel@tonic-gate 	case GETLDAPSERVER:
660*7c478bd9Sstevel@tonic-gate 		getldap_getserver(&u.data.ldap_ret, ptr);
661*7c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
662*7c478bd9Sstevel@tonic-gate 		break;
663*7c478bd9Sstevel@tonic-gate 	case GETCACHE:
664*7c478bd9Sstevel@tonic-gate 		getldap_get_cacheData(&u.data.ldap_ret, ptr);
665*7c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
666*7c478bd9Sstevel@tonic-gate 		break;
667*7c478bd9Sstevel@tonic-gate 	case SETCACHE:
668*7c478bd9Sstevel@tonic-gate 		getldap_set_cacheData(&u.data.ldap_ret, ptr);
669*7c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
670*7c478bd9Sstevel@tonic-gate 		break;
671*7c478bd9Sstevel@tonic-gate 	case GETCACHESTAT:
672*7c478bd9Sstevel@tonic-gate 		getldap_get_cacheStat(&u.data.ldap_ret);
673*7c478bd9Sstevel@tonic-gate 		current_admin.ldap_stat.ldap_numbercalls++;
674*7c478bd9Sstevel@tonic-gate 		break;
675*7c478bd9Sstevel@tonic-gate 	default:
676*7c478bd9Sstevel@tonic-gate 		logit("Unknown ldap service door call op %d\n",
677*7c478bd9Sstevel@tonic-gate 		    ptr->ldap_callnumber);
678*7c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_return_code = -99;
679*7c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_bufferbytesused = sizeof (ldap_return_t);
680*7c478bd9Sstevel@tonic-gate 		break;
681*7c478bd9Sstevel@tonic-gate 	}
682*7c478bd9Sstevel@tonic-gate 	door_return((char *)&u.data,
683*7c478bd9Sstevel@tonic-gate 		u.data.ldap_ret.ldap_bufferbytesused, NULL, 0);
684*7c478bd9Sstevel@tonic-gate }
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate static void
687*7c478bd9Sstevel@tonic-gate usage(char *s)
688*7c478bd9Sstevel@tonic-gate {
689*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
690*7c478bd9Sstevel@tonic-gate 		gettext("Usage: %s [-d debug_level] [-l logfilename]\n"), s);
691*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-K] "
692*7c478bd9Sstevel@tonic-gate 					"[-r revalidate_interval] "));
693*7c478bd9Sstevel@tonic-gate #ifndef SLP
694*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g]\n"));
695*7c478bd9Sstevel@tonic-gate #else
696*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("	[-g] [-s]\n"));
697*7c478bd9Sstevel@tonic-gate #endif /* SLP */
698*7c478bd9Sstevel@tonic-gate 	exit(1);
699*7c478bd9Sstevel@tonic-gate }
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate 
702*7c478bd9Sstevel@tonic-gate static int	logfd = -1;
703*7c478bd9Sstevel@tonic-gate 
704*7c478bd9Sstevel@tonic-gate static int
705*7c478bd9Sstevel@tonic-gate cachemgr_set_lf(admin_t *ptr, char *logfile)
706*7c478bd9Sstevel@tonic-gate {
707*7c478bd9Sstevel@tonic-gate 	int	newlogfd;
708*7c478bd9Sstevel@tonic-gate 
709*7c478bd9Sstevel@tonic-gate 	/*
710*7c478bd9Sstevel@tonic-gate 	 *  we don't really want to try and open the log file
711*7c478bd9Sstevel@tonic-gate 	 *  /dev/null since that will fail w/ our security fixes
712*7c478bd9Sstevel@tonic-gate 	 */
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate 	if (logfile == NULL || *logfile == 0) {
715*7c478bd9Sstevel@tonic-gate 		/*EMPTY*/;
716*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(logfile, "/dev/null") == 0) {
717*7c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, "/dev/null");
718*7c478bd9Sstevel@tonic-gate 		(void) close(logfd);
719*7c478bd9Sstevel@tonic-gate 		logfd = -1;
720*7c478bd9Sstevel@tonic-gate 	} else {
721*7c478bd9Sstevel@tonic-gate 		if ((newlogfd =
722*7c478bd9Sstevel@tonic-gate 			open(logfile, O_EXCL|O_WRONLY|O_CREAT, 0644)) < 0) {
723*7c478bd9Sstevel@tonic-gate 			/*
724*7c478bd9Sstevel@tonic-gate 			 * File already exists... now we need to get cute
725*7c478bd9Sstevel@tonic-gate 			 * since opening a file in a world-writeable directory
726*7c478bd9Sstevel@tonic-gate 			 * safely is hard = it could be a hard link or a
727*7c478bd9Sstevel@tonic-gate 			 * symbolic link to a system file.
728*7c478bd9Sstevel@tonic-gate 			 *
729*7c478bd9Sstevel@tonic-gate 			 */
730*7c478bd9Sstevel@tonic-gate 			struct stat	before;
731*7c478bd9Sstevel@tonic-gate 
732*7c478bd9Sstevel@tonic-gate 			if (lstat(logfile, &before) < 0) {
733*7c478bd9Sstevel@tonic-gate 				logit("Cannot open new logfile \"%s\": %sn",
734*7c478bd9Sstevel@tonic-gate 					logfile, strerror(errno));
735*7c478bd9Sstevel@tonic-gate 				return (-1);
736*7c478bd9Sstevel@tonic-gate 			}
737*7c478bd9Sstevel@tonic-gate 			if (S_ISREG(before.st_mode) &&	/* no symbolic links */
738*7c478bd9Sstevel@tonic-gate 			    (before.st_nlink == 1) &&	/* no hard links */
739*7c478bd9Sstevel@tonic-gate 			    (before.st_uid == 0)) {	/* owned by root */
740*7c478bd9Sstevel@tonic-gate 				if ((newlogfd =
741*7c478bd9Sstevel@tonic-gate 				    open(logfile,
742*7c478bd9Sstevel@tonic-gate 				    O_APPEND|O_WRONLY, 0644)) < 0) {
743*7c478bd9Sstevel@tonic-gate 					logit("Cannot open new logfile "
744*7c478bd9Sstevel@tonic-gate 						"\"%s\": %s\n",
745*7c478bd9Sstevel@tonic-gate 						logfile, strerror(errno));
746*7c478bd9Sstevel@tonic-gate 					return (-1);
747*7c478bd9Sstevel@tonic-gate 				}
748*7c478bd9Sstevel@tonic-gate 			} else {
749*7c478bd9Sstevel@tonic-gate 				logit("Cannot use specified logfile "
750*7c478bd9Sstevel@tonic-gate 				    "\"%s\": file is/has links or isn't "
751*7c478bd9Sstevel@tonic-gate 				    "owned by root\n", logfile);
752*7c478bd9Sstevel@tonic-gate 				return (-1);
753*7c478bd9Sstevel@tonic-gate 			}
754*7c478bd9Sstevel@tonic-gate 		}
755*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(ptr->logfile, logfile, sizeof (ptr->logfile));
756*7c478bd9Sstevel@tonic-gate 		(void) close(logfd);
757*7c478bd9Sstevel@tonic-gate 		logfd = newlogfd;
758*7c478bd9Sstevel@tonic-gate 		logit("Starting ldap_cachemgr, logfile %s\n", logfile);
759*7c478bd9Sstevel@tonic-gate 	}
760*7c478bd9Sstevel@tonic-gate 	return (0);
761*7c478bd9Sstevel@tonic-gate }
762*7c478bd9Sstevel@tonic-gate 
763*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
764*7c478bd9Sstevel@tonic-gate void
765*7c478bd9Sstevel@tonic-gate logit(char *format, ...)
766*7c478bd9Sstevel@tonic-gate {
767*7c478bd9Sstevel@tonic-gate 	static mutex_t	loglock;
768*7c478bd9Sstevel@tonic-gate 	struct timeval	tv;
769*7c478bd9Sstevel@tonic-gate 	char		buffer[BUFSIZ];
770*7c478bd9Sstevel@tonic-gate 	va_list		ap;
771*7c478bd9Sstevel@tonic-gate 
772*7c478bd9Sstevel@tonic-gate 	va_start(ap, format);
773*7c478bd9Sstevel@tonic-gate 
774*7c478bd9Sstevel@tonic-gate 	if (logfd >= 0) {
775*7c478bd9Sstevel@tonic-gate 		int	safechars;
776*7c478bd9Sstevel@tonic-gate 
777*7c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tv, NULL);
778*7c478bd9Sstevel@tonic-gate 		(void) ctime_r(&tv.tv_sec, buffer, BUFSIZ);
779*7c478bd9Sstevel@tonic-gate 		(void) snprintf(buffer+19, BUFSIZE, ".%.4ld	",
780*7c478bd9Sstevel@tonic-gate 			tv.tv_usec/100);
781*7c478bd9Sstevel@tonic-gate 		safechars = sizeof (buffer) - 30;
782*7c478bd9Sstevel@tonic-gate 		if (vsnprintf(buffer+25, safechars, format, ap) > safechars)
783*7c478bd9Sstevel@tonic-gate 			(void) strcat(buffer, "...\n");
784*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&loglock);
785*7c478bd9Sstevel@tonic-gate 		(void) write(logfd, buffer, strlen(buffer));
786*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&loglock);
787*7c478bd9Sstevel@tonic-gate 	}
788*7c478bd9Sstevel@tonic-gate 	va_end(ap);
789*7c478bd9Sstevel@tonic-gate }
790*7c478bd9Sstevel@tonic-gate 
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate void
793*7c478bd9Sstevel@tonic-gate do_update(ldap_call_t *in)
794*7c478bd9Sstevel@tonic-gate {
795*7c478bd9Sstevel@tonic-gate 	dataunion		u;
796*7c478bd9Sstevel@tonic-gate 
797*7c478bd9Sstevel@tonic-gate 	switch (in->ldap_callnumber) {
798*7c478bd9Sstevel@tonic-gate 	case GETLDAPCONFIG:
799*7c478bd9Sstevel@tonic-gate 		getldap_lookup(&u.data.ldap_ret, in);
800*7c478bd9Sstevel@tonic-gate 		break;
801*7c478bd9Sstevel@tonic-gate 	default:
802*7c478bd9Sstevel@tonic-gate 		assert(0);
803*7c478bd9Sstevel@tonic-gate 		break;
804*7c478bd9Sstevel@tonic-gate 	}
805*7c478bd9Sstevel@tonic-gate 
806*7c478bd9Sstevel@tonic-gate 	free(in);
807*7c478bd9Sstevel@tonic-gate }
808*7c478bd9Sstevel@tonic-gate 
809*7c478bd9Sstevel@tonic-gate 
810*7c478bd9Sstevel@tonic-gate static int
811*7c478bd9Sstevel@tonic-gate client_getadmin(admin_t *ptr)
812*7c478bd9Sstevel@tonic-gate {
813*7c478bd9Sstevel@tonic-gate 	dataunion		u;
814*7c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
815*7c478bd9Sstevel@tonic-gate 	int		ndata;
816*7c478bd9Sstevel@tonic-gate 	int		adata;
817*7c478bd9Sstevel@tonic-gate 
818*7c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETADMIN;
819*7c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
820*7c478bd9Sstevel@tonic-gate 	adata = sizeof (u.data);
821*7c478bd9Sstevel@tonic-gate 	dptr = &u.data;
822*7c478bd9Sstevel@tonic-gate 
823*7c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
824*7c478bd9Sstevel@tonic-gate 		return (-1);
825*7c478bd9Sstevel@tonic-gate 	}
826*7c478bd9Sstevel@tonic-gate 	(void) memcpy(ptr, dptr->ldap_ret.ldap_u.buff, sizeof (*ptr));
827*7c478bd9Sstevel@tonic-gate 
828*7c478bd9Sstevel@tonic-gate 	return (0);
829*7c478bd9Sstevel@tonic-gate }
830*7c478bd9Sstevel@tonic-gate 
831*7c478bd9Sstevel@tonic-gate static int
832*7c478bd9Sstevel@tonic-gate getadmin(ldap_return_t *out)
833*7c478bd9Sstevel@tonic-gate {
834*7c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
835*7c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (current_admin);
836*7c478bd9Sstevel@tonic-gate 	(void) memcpy(out->ldap_u.buff, &current_admin, sizeof (current_admin));
837*7c478bd9Sstevel@tonic-gate 
838*7c478bd9Sstevel@tonic-gate 	return (0);
839*7c478bd9Sstevel@tonic-gate }
840*7c478bd9Sstevel@tonic-gate 
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate static int
843*7c478bd9Sstevel@tonic-gate setadmin(ldap_return_t *out, ldap_call_t *ptr)
844*7c478bd9Sstevel@tonic-gate {
845*7c478bd9Sstevel@tonic-gate 	admin_t	*new;
846*7c478bd9Sstevel@tonic-gate 
847*7c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
848*7c478bd9Sstevel@tonic-gate 	out->ldap_bufferbytesused = sizeof (ldap_return_t);
849*7c478bd9Sstevel@tonic-gate 	new = (admin_t *)ptr->ldap_u.domainname;
850*7c478bd9Sstevel@tonic-gate 
851*7c478bd9Sstevel@tonic-gate 	/*
852*7c478bd9Sstevel@tonic-gate 	 *  global admin stuff
853*7c478bd9Sstevel@tonic-gate 	 */
854*7c478bd9Sstevel@tonic-gate 
855*7c478bd9Sstevel@tonic-gate 	if ((cachemgr_set_lf(&current_admin, new->logfile) < 0) ||
856*7c478bd9Sstevel@tonic-gate 	    cachemgr_set_dl(&current_admin, new->debug_level) < 0) {
857*7c478bd9Sstevel@tonic-gate 		out->ldap_return_code = NOTFOUND;
858*7c478bd9Sstevel@tonic-gate 		return (-1);
859*7c478bd9Sstevel@tonic-gate 	}
860*7c478bd9Sstevel@tonic-gate 
861*7c478bd9Sstevel@tonic-gate 	if (cachemgr_set_ttl(&current_admin.ldap_stat,
862*7c478bd9Sstevel@tonic-gate 			"ldap",
863*7c478bd9Sstevel@tonic-gate 			new->ldap_stat.ldap_ttl) < 0) {
864*7c478bd9Sstevel@tonic-gate 		out->ldap_return_code = NOTFOUND;
865*7c478bd9Sstevel@tonic-gate 		return (-1);
866*7c478bd9Sstevel@tonic-gate 	}
867*7c478bd9Sstevel@tonic-gate 	out->ldap_return_code = SUCCESS;
868*7c478bd9Sstevel@tonic-gate 
869*7c478bd9Sstevel@tonic-gate 	return (0);
870*7c478bd9Sstevel@tonic-gate }
871*7c478bd9Sstevel@tonic-gate 
872*7c478bd9Sstevel@tonic-gate 
873*7c478bd9Sstevel@tonic-gate static void
874*7c478bd9Sstevel@tonic-gate client_killserver()
875*7c478bd9Sstevel@tonic-gate {
876*7c478bd9Sstevel@tonic-gate 	dataunion		u;
877*7c478bd9Sstevel@tonic-gate 	ldap_data_t		*dptr;
878*7c478bd9Sstevel@tonic-gate 	int			ndata;
879*7c478bd9Sstevel@tonic-gate 	int			adata;
880*7c478bd9Sstevel@tonic-gate 
881*7c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = KILLSERVER;
882*7c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
883*7c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
884*7c478bd9Sstevel@tonic-gate 	dptr = &u.data;
885*7c478bd9Sstevel@tonic-gate 
886*7c478bd9Sstevel@tonic-gate 	__ns_ldap_trydoorcall(&dptr, &ndata, &adata);
887*7c478bd9Sstevel@tonic-gate }
888*7c478bd9Sstevel@tonic-gate 
889*7c478bd9Sstevel@tonic-gate 
890*7c478bd9Sstevel@tonic-gate static int
891*7c478bd9Sstevel@tonic-gate client_setadmin(admin_t *ptr)
892*7c478bd9Sstevel@tonic-gate {
893*7c478bd9Sstevel@tonic-gate 	dataunion		u;
894*7c478bd9Sstevel@tonic-gate 	ldap_data_t		*dptr;
895*7c478bd9Sstevel@tonic-gate 	int			ndata;
896*7c478bd9Sstevel@tonic-gate 	int			adata;
897*7c478bd9Sstevel@tonic-gate 
898*7c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = SETADMIN;
899*7c478bd9Sstevel@tonic-gate 	(void) memcpy(u.data.ldap_call.ldap_u.domainname, ptr, sizeof (*ptr));
900*7c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
901*7c478bd9Sstevel@tonic-gate 	adata = sizeof (*ptr);
902*7c478bd9Sstevel@tonic-gate 	dptr = &u.data;
903*7c478bd9Sstevel@tonic-gate 
904*7c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
905*7c478bd9Sstevel@tonic-gate 		return (-1);
906*7c478bd9Sstevel@tonic-gate 	}
907*7c478bd9Sstevel@tonic-gate 
908*7c478bd9Sstevel@tonic-gate 	return (0);
909*7c478bd9Sstevel@tonic-gate }
910*7c478bd9Sstevel@tonic-gate 
911*7c478bd9Sstevel@tonic-gate static int
912*7c478bd9Sstevel@tonic-gate client_showstats(admin_t *ptr)
913*7c478bd9Sstevel@tonic-gate {
914*7c478bd9Sstevel@tonic-gate 	dataunion	u;
915*7c478bd9Sstevel@tonic-gate 	ldap_data_t	*dptr;
916*7c478bd9Sstevel@tonic-gate 	int		ndata;
917*7c478bd9Sstevel@tonic-gate 	int		adata;
918*7c478bd9Sstevel@tonic-gate 	char		*rbuf, *sptr, *rest;
919*7c478bd9Sstevel@tonic-gate 
920*7c478bd9Sstevel@tonic-gate 	/*
921*7c478bd9Sstevel@tonic-gate 	 * print admin data
922*7c478bd9Sstevel@tonic-gate 	 */
923*7c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr configuration:\n"));
924*7c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server debug level %10d\n"), ptr->debug_level);
925*7c478bd9Sstevel@tonic-gate 	(void) printf(gettext("server log file\t\"%s\"\n"), ptr->logfile);
926*7c478bd9Sstevel@tonic-gate 	(void) printf(gettext("number of calls to ldapcachemgr %10d\n"),
927*7c478bd9Sstevel@tonic-gate 		ptr->ldap_stat.ldap_numbercalls);
928*7c478bd9Sstevel@tonic-gate 
929*7c478bd9Sstevel@tonic-gate 	/*
930*7c478bd9Sstevel@tonic-gate 	 * get cache data statistics
931*7c478bd9Sstevel@tonic-gate 	 */
932*7c478bd9Sstevel@tonic-gate 	u.data.ldap_call.ldap_callnumber = GETCACHESTAT;
933*7c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
934*7c478bd9Sstevel@tonic-gate 	adata = sizeof (ldap_call_t);
935*7c478bd9Sstevel@tonic-gate 	dptr = &u.data;
936*7c478bd9Sstevel@tonic-gate 
937*7c478bd9Sstevel@tonic-gate 	if (__ns_ldap_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
938*7c478bd9Sstevel@tonic-gate 		(void) printf(
939*7c478bd9Sstevel@tonic-gate 			gettext("\nCache data statistics not available!\n"));
940*7c478bd9Sstevel@tonic-gate 		return (0);
941*7c478bd9Sstevel@tonic-gate 	}
942*7c478bd9Sstevel@tonic-gate 
943*7c478bd9Sstevel@tonic-gate 	/*
944*7c478bd9Sstevel@tonic-gate 	 * print cache data statistics line by line
945*7c478bd9Sstevel@tonic-gate 	 */
946*7c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\ncachemgr cache data statistics:\n"));
947*7c478bd9Sstevel@tonic-gate 	rbuf = dptr->ldap_ret.ldap_u.buff;
948*7c478bd9Sstevel@tonic-gate 	sptr = strtok_r(rbuf, DOORLINESEP, &rest);
949*7c478bd9Sstevel@tonic-gate 	for (;;) {
950*7c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", sptr);
951*7c478bd9Sstevel@tonic-gate 		sptr = strtok_r(NULL, DOORLINESEP, &rest);
952*7c478bd9Sstevel@tonic-gate 		if (sptr == NULL)
953*7c478bd9Sstevel@tonic-gate 			break;
954*7c478bd9Sstevel@tonic-gate 	}
955*7c478bd9Sstevel@tonic-gate 	return (0);
956*7c478bd9Sstevel@tonic-gate }
957*7c478bd9Sstevel@tonic-gate 
958*7c478bd9Sstevel@tonic-gate 
959*7c478bd9Sstevel@tonic-gate /*
960*7c478bd9Sstevel@tonic-gate  * detach from tty
961*7c478bd9Sstevel@tonic-gate  */
962*7c478bd9Sstevel@tonic-gate static void
963*7c478bd9Sstevel@tonic-gate detachfromtty(char *pgm)
964*7c478bd9Sstevel@tonic-gate {
965*7c478bd9Sstevel@tonic-gate 	int 	status;
966*7c478bd9Sstevel@tonic-gate 	pid_t	pid, wret;
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 	(void) close(0);
969*7c478bd9Sstevel@tonic-gate 	(void) close(1);
970*7c478bd9Sstevel@tonic-gate 	/*
971*7c478bd9Sstevel@tonic-gate 	 * Block the SIGUSR1 signal
972*7c478bd9Sstevel@tonic-gate 	 * just in case that the child
973*7c478bd9Sstevel@tonic-gate 	 * process may run faster than
974*7c478bd9Sstevel@tonic-gate 	 * the parent process and
975*7c478bd9Sstevel@tonic-gate 	 * send this signal before
976*7c478bd9Sstevel@tonic-gate 	 * the signal handler is ready
977*7c478bd9Sstevel@tonic-gate 	 * in the parent process.
978*7c478bd9Sstevel@tonic-gate 	 * This error will cause the parent
979*7c478bd9Sstevel@tonic-gate 	 * to exit with the User Signal 1
980*7c478bd9Sstevel@tonic-gate 	 * exit code (144).
981*7c478bd9Sstevel@tonic-gate 	 */
982*7c478bd9Sstevel@tonic-gate 	(void) sighold(SIGUSR1);
983*7c478bd9Sstevel@tonic-gate 	pid = fork1();
984*7c478bd9Sstevel@tonic-gate 	switch (pid) {
985*7c478bd9Sstevel@tonic-gate 		case (pid_t)-1:
986*7c478bd9Sstevel@tonic-gate 			logit("detachfromtty(): fork1() call failed\n");
987*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
988*7c478bd9Sstevel@tonic-gate 					gettext("%s: fork1() call failed.\n"),
989*7c478bd9Sstevel@tonic-gate 					pgm);
990*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
991*7c478bd9Sstevel@tonic-gate 				gettext("ldap_cachemgr: fork1() call failed."));
992*7c478bd9Sstevel@tonic-gate 			exit(1);
993*7c478bd9Sstevel@tonic-gate 			break;
994*7c478bd9Sstevel@tonic-gate 		case 0:
995*7c478bd9Sstevel@tonic-gate 			/*
996*7c478bd9Sstevel@tonic-gate 			 * child process does not
997*7c478bd9Sstevel@tonic-gate 			 * need to worry about
998*7c478bd9Sstevel@tonic-gate 			 * the SIGUSR1 signal
999*7c478bd9Sstevel@tonic-gate 			 */
1000*7c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
1001*7c478bd9Sstevel@tonic-gate 			(void) close(2);
1002*7c478bd9Sstevel@tonic-gate 			break;
1003*7c478bd9Sstevel@tonic-gate 		default:
1004*7c478bd9Sstevel@tonic-gate 			/*
1005*7c478bd9Sstevel@tonic-gate 			 * Wait forever until the child process
1006*7c478bd9Sstevel@tonic-gate 			 * has exited, or has signalled that at
1007*7c478bd9Sstevel@tonic-gate 			 * least one server in the server list
1008*7c478bd9Sstevel@tonic-gate 			 * is up.
1009*7c478bd9Sstevel@tonic-gate 			 */
1010*7c478bd9Sstevel@tonic-gate 			if (signal(SIGUSR1, sig_ok_to_exit) == SIG_ERR) {
1011*7c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
1012*7c478bd9Sstevel@tonic-gate 					"can't set up signal handler to "
1013*7c478bd9Sstevel@tonic-gate 					" catch SIGUSR1.\n");
1014*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1015*7c478bd9Sstevel@tonic-gate 					gettext("%s: signal() call failed.\n"),
1016*7c478bd9Sstevel@tonic-gate 					pgm);
1017*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, gettext("ldap_cachemgr: "
1018*7c478bd9Sstevel@tonic-gate 					"can't set up signal handler to "
1019*7c478bd9Sstevel@tonic-gate 					" catch SIGUSR1."));
1020*7c478bd9Sstevel@tonic-gate 				exit(1);
1021*7c478bd9Sstevel@tonic-gate 			}
1022*7c478bd9Sstevel@tonic-gate 
1023*7c478bd9Sstevel@tonic-gate 			/*
1024*7c478bd9Sstevel@tonic-gate 			 * now unblock the SIGUSR1 signal
1025*7c478bd9Sstevel@tonic-gate 			 * to handle the pending or
1026*7c478bd9Sstevel@tonic-gate 			 * soon to arrive SIGUSR1 signal
1027*7c478bd9Sstevel@tonic-gate 			 */
1028*7c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGUSR1);
1029*7c478bd9Sstevel@tonic-gate 			wret = waitpid(pid, &status, 0);
1030*7c478bd9Sstevel@tonic-gate 
1031*7c478bd9Sstevel@tonic-gate 			if (wret == -1) {
1032*7c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
1033*7c478bd9Sstevel@tonic-gate 					"waitpid() call failed\n");
1034*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1035*7c478bd9Sstevel@tonic-gate 					gettext("%s: waitpid() call failed.\n"),
1036*7c478bd9Sstevel@tonic-gate 					pgm);
1037*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
1038*7c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: waitpid() "
1039*7c478bd9Sstevel@tonic-gate 						"call failed."));
1040*7c478bd9Sstevel@tonic-gate 				exit(1);
1041*7c478bd9Sstevel@tonic-gate 			}
1042*7c478bd9Sstevel@tonic-gate 			if (wret != pid) {
1043*7c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
1044*7c478bd9Sstevel@tonic-gate 					"waitpid() returned %ld when "
1045*7c478bd9Sstevel@tonic-gate 					"child pid was %ld\n",
1046*7c478bd9Sstevel@tonic-gate 					wret, pid);
1047*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1048*7c478bd9Sstevel@tonic-gate 					gettext(
1049*7c478bd9Sstevel@tonic-gate 					"%s: waitpid() returned %ld when "
1050*7c478bd9Sstevel@tonic-gate 					"child pid was %ld.\n"),
1051*7c478bd9Sstevel@tonic-gate 					pgm, wret, pid);
1052*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
1053*7c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: waitpid() "
1054*7c478bd9Sstevel@tonic-gate 						"returned different "
1055*7c478bd9Sstevel@tonic-gate 						"child pid."));
1056*7c478bd9Sstevel@tonic-gate 				exit(1);
1057*7c478bd9Sstevel@tonic-gate 			}
1058*7c478bd9Sstevel@tonic-gate 
1059*7c478bd9Sstevel@tonic-gate 			/* evaluate return status */
1060*7c478bd9Sstevel@tonic-gate 			if (WIFEXITED(status)) {
1061*7c478bd9Sstevel@tonic-gate 				if (WEXITSTATUS(status) == 0) {
1062*7c478bd9Sstevel@tonic-gate 					exit(0);
1063*7c478bd9Sstevel@tonic-gate 				}
1064*7c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
1065*7c478bd9Sstevel@tonic-gate 					"child failed (rc = %d).\n",
1066*7c478bd9Sstevel@tonic-gate 					WEXITSTATUS(status));
1067*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1068*7c478bd9Sstevel@tonic-gate 					gettext("%s: failed. Please see "
1069*7c478bd9Sstevel@tonic-gate 					"syslog for details.\n"),
1070*7c478bd9Sstevel@tonic-gate 					pgm);
1071*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
1072*7c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: failed "
1073*7c478bd9Sstevel@tonic-gate 					"(rc = %d)."),
1074*7c478bd9Sstevel@tonic-gate 					WEXITSTATUS(status));
1075*7c478bd9Sstevel@tonic-gate 			} else if (WIFSIGNALED(status)) {
1076*7c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): "
1077*7c478bd9Sstevel@tonic-gate 					"child terminated by signal %d.\n",
1078*7c478bd9Sstevel@tonic-gate 					WTERMSIG(status));
1079*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1080*7c478bd9Sstevel@tonic-gate 				gettext("%s: terminated by signal %d.\n"),
1081*7c478bd9Sstevel@tonic-gate 					pgm, WTERMSIG(status));
1082*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
1083*7c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: terminated by "
1084*7c478bd9Sstevel@tonic-gate 					"signal %d.\n"),
1085*7c478bd9Sstevel@tonic-gate 					WTERMSIG(status));
1086*7c478bd9Sstevel@tonic-gate 			} else if (WCOREDUMP(status)) {
1087*7c478bd9Sstevel@tonic-gate 				logit("detachfromtty(): child core dumped.\n"),
1088*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1089*7c478bd9Sstevel@tonic-gate 					gettext("%s: core dumped.\n"),
1090*7c478bd9Sstevel@tonic-gate 					pgm);
1091*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
1092*7c478bd9Sstevel@tonic-gate 					gettext("ldap_cachemgr: "
1093*7c478bd9Sstevel@tonic-gate 						"core dumped.\n"));
1094*7c478bd9Sstevel@tonic-gate 			}
1095*7c478bd9Sstevel@tonic-gate 
1096*7c478bd9Sstevel@tonic-gate 			exit(1);
1097*7c478bd9Sstevel@tonic-gate 	}
1098*7c478bd9Sstevel@tonic-gate 	(void) setsid();
1099*7c478bd9Sstevel@tonic-gate 	if (open("/dev/null", O_RDWR, 0) != -1) {
1100*7c478bd9Sstevel@tonic-gate 		(void) dup(0);
1101*7c478bd9Sstevel@tonic-gate 		(void) dup(0);
1102*7c478bd9Sstevel@tonic-gate 	}
1103*7c478bd9Sstevel@tonic-gate }
1104