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