xref: /titanic_53/usr/src/cmd/nscd/server.c (revision f166393f4d30d59a005967d6c6d2869ef830b75d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Simple doors name server cache daemon
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <sys/door.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <time.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <sys/time.h>
417c478bd9Sstevel@tonic-gate #include <sys/wait.h>
427c478bd9Sstevel@tonic-gate #include <sys/zone.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate #include <pthread.h>
467c478bd9Sstevel@tonic-gate #include <thread.h>
477c478bd9Sstevel@tonic-gate #include <stdarg.h>
487c478bd9Sstevel@tonic-gate #include <fcntl.h>
497c478bd9Sstevel@tonic-gate #include <assert.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <memory.h>
527c478bd9Sstevel@tonic-gate #include <sys/socket.h>
537c478bd9Sstevel@tonic-gate #include <net/route.h>
547c478bd9Sstevel@tonic-gate #include <net/if.h>
557c478bd9Sstevel@tonic-gate #include <netinet/in.h>
567c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
577c478bd9Sstevel@tonic-gate #include <resolv.h>
587c478bd9Sstevel@tonic-gate #include <door.h>
597c478bd9Sstevel@tonic-gate #include "getxby_door.h"
607c478bd9Sstevel@tonic-gate #include "server_door.h"
617c478bd9Sstevel@tonic-gate #include "nscd.h"
627c478bd9Sstevel@tonic-gate /* Includes for filenames of databases */
637c478bd9Sstevel@tonic-gate #include <shadow.h>
647c478bd9Sstevel@tonic-gate #include <userdefs.h>
657c478bd9Sstevel@tonic-gate #include <netdb.h>
667c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
677c478bd9Sstevel@tonic-gate #include <exec_attr.h>
687c478bd9Sstevel@tonic-gate #include <prof_attr.h>
697c478bd9Sstevel@tonic-gate #include <user_attr.h>
707c478bd9Sstevel@tonic-gate #include <ucred.h>
717c478bd9Sstevel@tonic-gate #include <priv.h>
727c478bd9Sstevel@tonic-gate #include <libscf.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate extern int 	optind;
757c478bd9Sstevel@tonic-gate extern int 	opterr;
767c478bd9Sstevel@tonic-gate extern int 	optopt;
777c478bd9Sstevel@tonic-gate extern char 	*optarg;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static void switcher(void *, char *, size_t, door_desc_t *, uint_t);
807c478bd9Sstevel@tonic-gate static void rts_mon(void);
817c478bd9Sstevel@tonic-gate static void usage(char *);
827c478bd9Sstevel@tonic-gate static int nsc_calllen(nsc_call_t *);
837c478bd9Sstevel@tonic-gate static int client_getadmin(admin_t *);
847c478bd9Sstevel@tonic-gate static void getadmin(nsc_return_t *, int, nsc_call_t *);
857c478bd9Sstevel@tonic-gate static int setadmin(nsc_return_t *, int, nsc_call_t *);
867c478bd9Sstevel@tonic-gate static void client_killserver(void);
877c478bd9Sstevel@tonic-gate static int client_setadmin(admin_t *);
887c478bd9Sstevel@tonic-gate static void client_showstats(admin_t *);
897c478bd9Sstevel@tonic-gate static void detachfromtty(void);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate admin_t	current_admin;
937c478bd9Sstevel@tonic-gate static int will_become_server;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate void
967c478bd9Sstevel@tonic-gate nsc_reaper(char *tbl_name, hash_t *tbl, nsc_stat_t *admin_ptr,
977c478bd9Sstevel@tonic-gate     mutex_t *hash_lock)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	uint_t count;
1007c478bd9Sstevel@tonic-gate 	uint_t interval;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	while (1) {
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL) {
1057c478bd9Sstevel@tonic-gate 			logit("reaper_%s: %d entries in cache\n",
1067c478bd9Sstevel@tonic-gate 			tbl_name, admin_ptr->nsc_entries);
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 		if (admin_ptr->nsc_entries > 0) {
1097c478bd9Sstevel@tonic-gate 			count = reap_hash(tbl, admin_ptr, hash_lock,
1107c478bd9Sstevel@tonic-gate 			admin_ptr->nsc_pos_ttl);
1117c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
1127c478bd9Sstevel@tonic-gate 				logit("reaper_%s: reaped %d entries\n",
1137c478bd9Sstevel@tonic-gate 				tbl_name, count);
1147c478bd9Sstevel@tonic-gate 			}
1157c478bd9Sstevel@tonic-gate 		} else {
1167c478bd9Sstevel@tonic-gate 			/*
1177c478bd9Sstevel@tonic-gate 			 * We set a minimum wait of 60 before checking again;
1187c478bd9Sstevel@tonic-gate 			 * we don't want to sleep for no time at all.
1197c478bd9Sstevel@tonic-gate 			 * We don't clamp it for the reaping itself, that is
1207c478bd9Sstevel@tonic-gate 			 * done in reap_hash, and with a different minimum.
1217c478bd9Sstevel@tonic-gate 			 */
1227c478bd9Sstevel@tonic-gate 			interval = admin_ptr->nsc_pos_ttl;
1237c478bd9Sstevel@tonic-gate 			if (interval < 60) interval = 60;
1247c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
1257c478bd9Sstevel@tonic-gate 				logit(
1267c478bd9Sstevel@tonic-gate 				    "reaper_%s: Nothing to reap, sleep %d\n",
1277c478bd9Sstevel@tonic-gate 				    tbl_name, interval);
1287c478bd9Sstevel@tonic-gate 			}
1297c478bd9Sstevel@tonic-gate 			sleep(interval);
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate nsc_stat_t *
1357c478bd9Sstevel@tonic-gate getcacheptr(char *s)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	static const char *caches[7] = {"passwd", "group", "hosts", "ipnodes",
1387c478bd9Sstevel@tonic-gate 	    "exec_attr", "prof_attr", "user_attr" };
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (strncmp(caches[0], s, strlen(caches[0])) == 0)
1417c478bd9Sstevel@tonic-gate 		return (&current_admin.passwd);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (strncmp(caches[1], s, strlen(caches[1])) == 0)
1447c478bd9Sstevel@tonic-gate 		return (&current_admin.group);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (strncmp(caches[2], s, strlen(caches[2])) == 0)
1477c478bd9Sstevel@tonic-gate 		return (&current_admin.host);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (strncmp(caches[3], s, strlen(caches[3])) == 0)
1507c478bd9Sstevel@tonic-gate 		return (&current_admin.node);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (strncmp(caches[4], s, strlen(caches[4])) == 0)
1537c478bd9Sstevel@tonic-gate 		return (&current_admin.exec);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (strncmp(caches[5], s, strlen(caches[5])) == 0)
1567c478bd9Sstevel@tonic-gate 		return (&current_admin.prof);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (strncmp(caches[6], s, strlen(caches[6])) == 0)
1597c478bd9Sstevel@tonic-gate 		return (&current_admin.user);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	return (NULL);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static char *
1657c478bd9Sstevel@tonic-gate getcacheopt(char *s)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	while (*s && *s != ',')
1687c478bd9Sstevel@tonic-gate 		s++;
1697c478bd9Sstevel@tonic-gate 	return ((*s == ',') ? (s + 1) : NULL);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  *  routine to check if server is already running
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static int
1777c478bd9Sstevel@tonic-gate nsc_ping(void)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	nsc_data_t data;
1807c478bd9Sstevel@tonic-gate 	nsc_data_t *dptr;
1817c478bd9Sstevel@tonic-gate 	int ndata;
1827c478bd9Sstevel@tonic-gate 	int adata;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	data.nsc_call.nsc_callnumber = NULLCALL;
1857c478bd9Sstevel@tonic-gate 	ndata = sizeof (data);
1867c478bd9Sstevel@tonic-gate 	adata = sizeof (data);
1877c478bd9Sstevel@tonic-gate 	dptr = &data;
1887c478bd9Sstevel@tonic-gate 	return (_nsc_trydoorcall(&dptr, &ndata, &adata));
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static void
1927c478bd9Sstevel@tonic-gate dozip(void)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	/* not much here */
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static void
1987c478bd9Sstevel@tonic-gate keep_open_dns_socket(void)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	_res.options |= RES_STAYOPEN; /* just keep this udp socket open */
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * declaring this causes the files backend to use hashing
2057c478bd9Sstevel@tonic-gate  * this is of course an utter hack, but provides a nice
2067c478bd9Sstevel@tonic-gate  * quiet back door to enable this feature for only the nscd.
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate void
2097c478bd9Sstevel@tonic-gate __nss_use_files_hash(void)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  *
2157c478bd9Sstevel@tonic-gate  *  The allocation of resources for cache lookups is an interesting
2167c478bd9Sstevel@tonic-gate  *  problem, and one that has caused several bugs in the beta release
2177c478bd9Sstevel@tonic-gate  *  of 2.5.  In particular, the introduction of a thottle to prevent
2187c478bd9Sstevel@tonic-gate  *  the creation of excessive numbers of LWPs in the case of a failed
2197c478bd9Sstevel@tonic-gate  *  name service has led to a denial of service problem when the
2207c478bd9Sstevel@tonic-gate  *  name service request rate exceeds the name service's ability
2217c478bd9Sstevel@tonic-gate  *  to respond.  As a result, I'm implementing the following
2227c478bd9Sstevel@tonic-gate  *  algorithm:
2237c478bd9Sstevel@tonic-gate  *
2247c478bd9Sstevel@tonic-gate  *  1) We cap the number of total threads.
2257c478bd9Sstevel@tonic-gate  *  2) We save CACHE_THREADS of those for cache lookups only.
2267c478bd9Sstevel@tonic-gate  *  3) We use a common pool of 2/3 of the remain threads that are used first
2277c478bd9Sstevel@tonic-gate  *  4) We save the remainder and allocate 1/3 of it for table specific lookups
2287c478bd9Sstevel@tonic-gate  *
2297c478bd9Sstevel@tonic-gate  *  The intent is to prevent the failure of a single name service from
2307c478bd9Sstevel@tonic-gate  *  causing denial of service, and to always have threads available for
2317c478bd9Sstevel@tonic-gate  *  cached lookups.  If a request comes in and the answer isn't in the
2327c478bd9Sstevel@tonic-gate  *  cache and we cannot get a thread, we simply return NOSERVER, forcing
2337c478bd9Sstevel@tonic-gate  *  the client to lookup the
2347c478bd9Sstevel@tonic-gate  *  data itself.  This will prevent the types of starvation seen
2357c478bd9Sstevel@tonic-gate  *  at UNC due to a single threaded DNS backend, and allows the cache
2367c478bd9Sstevel@tonic-gate  *  to eventually become filled.
2377c478bd9Sstevel@tonic-gate  *
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /* 7 tables: passwd, group, hosts, ipnodes, exec_attr, prof_attr, user_attr */
2417c478bd9Sstevel@tonic-gate #define	NSCD_TABLES		7
2427c478bd9Sstevel@tonic-gate #define	TABLE_THREADS		10
2437c478bd9Sstevel@tonic-gate #define	COMMON_THREADS		20
2447c478bd9Sstevel@tonic-gate #define	CACHE_MISS_THREADS	(COMMON_THREADS + NSCD_TABLES * TABLE_THREADS)
2457c478bd9Sstevel@tonic-gate #define	CACHE_HIT_THREADS	20
2467c478bd9Sstevel@tonic-gate #define	MAX_SERVER_THREADS	(CACHE_HIT_THREADS + CACHE_MISS_THREADS)
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate static sema_t common_sema;
2497c478bd9Sstevel@tonic-gate static sema_t passwd_sema;
2507c478bd9Sstevel@tonic-gate static sema_t hosts_sema;
2517c478bd9Sstevel@tonic-gate static sema_t nodes_sema;
2527c478bd9Sstevel@tonic-gate static sema_t group_sema;
2537c478bd9Sstevel@tonic-gate static sema_t exec_sema;
2547c478bd9Sstevel@tonic-gate static sema_t prof_sema;
2557c478bd9Sstevel@tonic-gate static sema_t user_sema;
2567c478bd9Sstevel@tonic-gate static thread_key_t lookup_state_key;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate static void
2597c478bd9Sstevel@tonic-gate initialize_lookup_clearance(void)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	thr_keycreate(&lookup_state_key, NULL);
2627c478bd9Sstevel@tonic-gate 	(void) sema_init(&common_sema, COMMON_THREADS, USYNC_THREAD, 0);
2637c478bd9Sstevel@tonic-gate 	(void) sema_init(&passwd_sema, TABLE_THREADS, USYNC_THREAD, 0);
2647c478bd9Sstevel@tonic-gate 	(void) sema_init(&hosts_sema, TABLE_THREADS, USYNC_THREAD, 0);
2657c478bd9Sstevel@tonic-gate 	(void) sema_init(&nodes_sema, TABLE_THREADS, USYNC_THREAD, 0);
2667c478bd9Sstevel@tonic-gate 	(void) sema_init(&group_sema, TABLE_THREADS, USYNC_THREAD, 0);
2677c478bd9Sstevel@tonic-gate 	(void) sema_init(&exec_sema, TABLE_THREADS, USYNC_THREAD, 0);
2687c478bd9Sstevel@tonic-gate 	(void) sema_init(&prof_sema, TABLE_THREADS, USYNC_THREAD, 0);
2697c478bd9Sstevel@tonic-gate 	(void) sema_init(&user_sema, TABLE_THREADS, USYNC_THREAD, 0);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate int
2737c478bd9Sstevel@tonic-gate get_clearance(int callnumber)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	sema_t *table_sema = NULL;
2767c478bd9Sstevel@tonic-gate 	char *tab;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (sema_trywait(&common_sema) == 0) {
2797c478bd9Sstevel@tonic-gate 		thr_setspecific(lookup_state_key, NULL);
2807c478bd9Sstevel@tonic-gate 		return (0);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	switch (MASKUPDATEBIT(callnumber)) {
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	case GETPWUID:
2867c478bd9Sstevel@tonic-gate 	case GETPWNAM:
2877c478bd9Sstevel@tonic-gate 		tab = "passwd";
2887c478bd9Sstevel@tonic-gate 		table_sema = &passwd_sema;
2897c478bd9Sstevel@tonic-gate 		break;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	case GETGRNAM:
2927c478bd9Sstevel@tonic-gate 	case GETGRGID:
2937c478bd9Sstevel@tonic-gate 		tab = "group";
2947c478bd9Sstevel@tonic-gate 		table_sema = &group_sema;
2957c478bd9Sstevel@tonic-gate 		break;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	case GETHOSTBYNAME:
2987c478bd9Sstevel@tonic-gate 	case GETHOSTBYADDR:
2997c478bd9Sstevel@tonic-gate 		tab = "hosts";
3007c478bd9Sstevel@tonic-gate 		table_sema = &hosts_sema;
3017c478bd9Sstevel@tonic-gate 		break;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	case GETIPNODEBYNAME:
3047c478bd9Sstevel@tonic-gate 	case GETIPNODEBYADDR:
3057c478bd9Sstevel@tonic-gate 		tab = "ipnodes";
3067c478bd9Sstevel@tonic-gate 		table_sema = &nodes_sema;
3077c478bd9Sstevel@tonic-gate 		break;
3087c478bd9Sstevel@tonic-gate 	case GETEXECID:
3097c478bd9Sstevel@tonic-gate 		tab = "exec_attr";
3107c478bd9Sstevel@tonic-gate 		table_sema = &exec_sema;
3117c478bd9Sstevel@tonic-gate 		break;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	case GETPROFNAM:
3147c478bd9Sstevel@tonic-gate 		tab = "prof_attr";
3157c478bd9Sstevel@tonic-gate 		table_sema = &prof_sema;
3167c478bd9Sstevel@tonic-gate 		break;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	case GETUSERNAM:
3197c478bd9Sstevel@tonic-gate 		tab = "user_attr";
3207c478bd9Sstevel@tonic-gate 		table_sema = &user_sema;
3217c478bd9Sstevel@tonic-gate 		break;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (sema_trywait(table_sema) == 0) {
3267c478bd9Sstevel@tonic-gate 		thr_setspecific(lookup_state_key, (void*)1);
3277c478bd9Sstevel@tonic-gate 		return (0);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_CANT_FIND) {
3317c478bd9Sstevel@tonic-gate 		logit("get_clearance: throttling load for %s table\n", tab);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 	return (-1);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate int
3377c478bd9Sstevel@tonic-gate release_clearance(int callnumber)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	int which;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	sema_t *table_sema = NULL;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	thr_getspecific(lookup_state_key, (void**)&which);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	if (which == 0) /* from common pool */ {
3467c478bd9Sstevel@tonic-gate 		(void) sema_post(&common_sema);
3477c478bd9Sstevel@tonic-gate 		return (0);
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	switch (MASKUPDATEBIT(callnumber)) {
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	case GETPWUID:
3537c478bd9Sstevel@tonic-gate 	case GETPWNAM:
3547c478bd9Sstevel@tonic-gate 		table_sema = &passwd_sema;
3557c478bd9Sstevel@tonic-gate 		break;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	case GETGRNAM:
3587c478bd9Sstevel@tonic-gate 	case GETGRGID:
3597c478bd9Sstevel@tonic-gate 		table_sema = &group_sema;
3607c478bd9Sstevel@tonic-gate 		break;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	case GETHOSTBYNAME:
3637c478bd9Sstevel@tonic-gate 	case GETHOSTBYADDR:
3647c478bd9Sstevel@tonic-gate 		table_sema = &hosts_sema;
3657c478bd9Sstevel@tonic-gate 		break;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	case GETIPNODEBYNAME:
3687c478bd9Sstevel@tonic-gate 	case GETIPNODEBYADDR:
3697c478bd9Sstevel@tonic-gate 		table_sema = &nodes_sema;
3707c478bd9Sstevel@tonic-gate 		break;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	case GETEXECID:
3737c478bd9Sstevel@tonic-gate 		table_sema = &exec_sema;
3747c478bd9Sstevel@tonic-gate 		break;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	case GETPROFNAM:
3777c478bd9Sstevel@tonic-gate 		table_sema = &prof_sema;
3787c478bd9Sstevel@tonic-gate 		break;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	case GETUSERNAM:
3817c478bd9Sstevel@tonic-gate 		table_sema = &user_sema;
3827c478bd9Sstevel@tonic-gate 		break;
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	(void) sema_post(table_sema);
3867c478bd9Sstevel@tonic-gate 	return (0);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate static mutex_t		create_lock;
3917c478bd9Sstevel@tonic-gate static int		nscd_max_servers = MAX_SERVER_THREADS;
3927c478bd9Sstevel@tonic-gate static int		num_servers = 0;
3937c478bd9Sstevel@tonic-gate static thread_key_t	server_key;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate  * Bind a TSD value to a server thread. This enables the destructor to
3977c478bd9Sstevel@tonic-gate  * be called if/when this thread exits.  This would be a programming error,
3987c478bd9Sstevel@tonic-gate  * but better safe than sorry.
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4017c478bd9Sstevel@tonic-gate static void *
4027c478bd9Sstevel@tonic-gate server_tsd_bind(void *arg)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	static void *value = 0;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/* disable cancellation to avoid hangs if server threads disappear */
4077c478bd9Sstevel@tonic-gate 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
4087c478bd9Sstevel@tonic-gate 	thr_setspecific(server_key, value);
4097c478bd9Sstevel@tonic-gate 	door_return(NULL, 0, NULL, 0);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/* make lint happy */
4127c478bd9Sstevel@tonic-gate 	return (NULL);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * Server threads are created here.
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4197c478bd9Sstevel@tonic-gate static void
4207c478bd9Sstevel@tonic-gate server_create(door_info_t *dip)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
4237c478bd9Sstevel@tonic-gate 	if (++num_servers > nscd_max_servers) {
4247c478bd9Sstevel@tonic-gate 		num_servers--;
4257c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&create_lock);
4267c478bd9Sstevel@tonic-gate 		return;
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
4297c478bd9Sstevel@tonic-gate 	thr_create(NULL, 0, server_tsd_bind, NULL, THR_BOUND|THR_DETACHED,
4307c478bd9Sstevel@tonic-gate 	    NULL);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * Server thread are destroyed here
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4377c478bd9Sstevel@tonic-gate static void
4387c478bd9Sstevel@tonic-gate server_destroy(void *arg)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_lock);
4417c478bd9Sstevel@tonic-gate 	num_servers--;
4427c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_lock);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate static char **saved_argv;
4467c478bd9Sstevel@tonic-gate static char saved_execname[MAXPATHLEN];
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate static void
4497c478bd9Sstevel@tonic-gate save_execname()
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	const char *name = getexecname();
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	saved_execname[0] = 0;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (name[0] != '/') { /* started w/ relative path */
4567c478bd9Sstevel@tonic-gate 		(void) getcwd(saved_execname, MAXPATHLEN);
4577c478bd9Sstevel@tonic-gate 		strlcat(saved_execname, "/", MAXPATHLEN);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	strlcat(saved_execname, name, MAXPATHLEN);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
462*f166393fSesolom int
4637c478bd9Sstevel@tonic-gate main(int argc, char ** argv)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate 	int did;
4667c478bd9Sstevel@tonic-gate 	int opt;
4677c478bd9Sstevel@tonic-gate 	int errflg = 0;
4687c478bd9Sstevel@tonic-gate 	int showstats = 0;
4697c478bd9Sstevel@tonic-gate 	int doset = 0;
4707c478bd9Sstevel@tonic-gate 	int loaded_config_file = 0;
4717c478bd9Sstevel@tonic-gate 	struct stat buf;
4727c478bd9Sstevel@tonic-gate 	sigset_t myset;
4737c478bd9Sstevel@tonic-gate 	struct sigaction action;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 *  Special case non-root user  here - he can just print stats
4777c478bd9Sstevel@tonic-gate 	 */
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (geteuid()) {
4807c478bd9Sstevel@tonic-gate 		if (argc != 2 || strcmp(argv[1], "-g")) {
4817c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4827c478bd9Sstevel@tonic-gate 			    "Must be root to use any option other than "\
4837c478bd9Sstevel@tonic-gate 			    "-g.\n\n");
4847c478bd9Sstevel@tonic-gate 			usage(argv[0]);
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		if ((nsc_ping() != SUCCESS) ||
4887c478bd9Sstevel@tonic-gate 		    (client_getadmin(&current_admin) != 0)) {
4897c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4907c478bd9Sstevel@tonic-gate 			    "%s doesn't appear to be running.\n", argv[0]);
4917c478bd9Sstevel@tonic-gate 			exit(1);
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 		client_showstats(&current_admin);
4947c478bd9Sstevel@tonic-gate 		exit(0);
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	/*
5007c478bd9Sstevel@tonic-gate 	 *  Determine if there is already a daemon running
5017c478bd9Sstevel@tonic-gate 	 */
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	will_become_server = (nsc_ping() != SUCCESS);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/*
5067c478bd9Sstevel@tonic-gate 	 *	process usual options
5077c478bd9Sstevel@tonic-gate 	 */
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 *  load normal config file
5117c478bd9Sstevel@tonic-gate 	 */
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (will_become_server) {
5147c478bd9Sstevel@tonic-gate 		static const nsc_stat_t defaults = {
5157c478bd9Sstevel@tonic-gate 			0,	/* stats */
5167c478bd9Sstevel@tonic-gate 			0,	/* stats */
5177c478bd9Sstevel@tonic-gate 			0,	/* stats */
5187c478bd9Sstevel@tonic-gate 			0,	/* stats */
5197c478bd9Sstevel@tonic-gate 			0,	/* stats */
5207c478bd9Sstevel@tonic-gate 			0,	/* stats */
5217c478bd9Sstevel@tonic-gate 			0,	/* stats */
5227c478bd9Sstevel@tonic-gate 			211,	/* suggested size */
5237c478bd9Sstevel@tonic-gate 			1,	/* enabled */
5247c478bd9Sstevel@tonic-gate 			0,	/* invalidate cmd */
5257c478bd9Sstevel@tonic-gate 			600,	/* positive ttl */
5267c478bd9Sstevel@tonic-gate 			10, 	/* netative ttl */
5277c478bd9Sstevel@tonic-gate 			20,	/* keep hot */
5287c478bd9Sstevel@tonic-gate 			0,	/* old data not ok */
5297c478bd9Sstevel@tonic-gate 			1 };	/* check files */
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 		current_admin.passwd = defaults;
5327c478bd9Sstevel@tonic-gate 		current_admin.group  = defaults;
5337c478bd9Sstevel@tonic-gate 		current_admin.host   = defaults;
5347c478bd9Sstevel@tonic-gate 		current_admin.node   = defaults;
5357c478bd9Sstevel@tonic-gate 		current_admin.exec   = defaults;
5367c478bd9Sstevel@tonic-gate 		current_admin.prof   = defaults;
5377c478bd9Sstevel@tonic-gate 		current_admin.user   = defaults;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		current_admin.logfile[0] = '\0';
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		if (access("/etc/nscd.conf", R_OK) == 0) {
5427c478bd9Sstevel@tonic-gate 			if (nscd_parse(argv[0], "/etc/nscd.conf") < 0) {
5437c478bd9Sstevel@tonic-gate 				exit(1);
5447c478bd9Sstevel@tonic-gate 			}
5457c478bd9Sstevel@tonic-gate 			loaded_config_file++;
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	else {
5507c478bd9Sstevel@tonic-gate 		if (client_getadmin(&current_admin)) {
5517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5527c478bd9Sstevel@tonic-gate 			    "Cannot contact nscd properly(?)\n");
5537c478bd9Sstevel@tonic-gate 			exit(1);
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		current_admin.logfile[0] = '\0';
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv,
5607c478bd9Sstevel@tonic-gate 	    "S:Kf:c:ge:p:n:i:l:d:s:h:o:")) != EOF) {
5617c478bd9Sstevel@tonic-gate 		nsc_stat_t *cache;
5627c478bd9Sstevel@tonic-gate 		char *cacheopt;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		switch (opt) {
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 		case 'S':		/* undocumented feature */
5677c478bd9Sstevel@tonic-gate 			doset++;
5687c478bd9Sstevel@tonic-gate 			cache = getcacheptr(optarg);
5697c478bd9Sstevel@tonic-gate 			cacheopt = getcacheopt(optarg);
5707c478bd9Sstevel@tonic-gate 			if (!cache || !cacheopt) {
5717c478bd9Sstevel@tonic-gate 				errflg++;
5727c478bd9Sstevel@tonic-gate 				break;
5737c478bd9Sstevel@tonic-gate 			}
5747c478bd9Sstevel@tonic-gate 			if (strcmp(cacheopt, "yes") == 0)
5757c478bd9Sstevel@tonic-gate 			    cache->nsc_secure_mode = 1;
5767c478bd9Sstevel@tonic-gate 			else if (strcmp(cacheopt, "no") == 0)
5777c478bd9Sstevel@tonic-gate 			    cache->nsc_secure_mode = 0;
5787c478bd9Sstevel@tonic-gate 			else
5797c478bd9Sstevel@tonic-gate 			    errflg++;
5807c478bd9Sstevel@tonic-gate 			break;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		case 'K':		/* undocumented feature */
5837c478bd9Sstevel@tonic-gate 			client_killserver();
5847c478bd9Sstevel@tonic-gate 			exit(0);
5857c478bd9Sstevel@tonic-gate 			break;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		case 'f':
5887c478bd9Sstevel@tonic-gate 			doset++;
5897c478bd9Sstevel@tonic-gate 			loaded_config_file++;
5907c478bd9Sstevel@tonic-gate 			if (nscd_parse(argv[0], optarg) < 0) {
5917c478bd9Sstevel@tonic-gate 				exit(1);
5927c478bd9Sstevel@tonic-gate 			}
5937c478bd9Sstevel@tonic-gate 			break;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		case 'g':
5967c478bd9Sstevel@tonic-gate 			showstats++;
5977c478bd9Sstevel@tonic-gate 			break;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		case 'p':
6007c478bd9Sstevel@tonic-gate 			doset++;
6017c478bd9Sstevel@tonic-gate 			cache = getcacheptr(optarg);
6027c478bd9Sstevel@tonic-gate 			cacheopt = getcacheopt(optarg);
6037c478bd9Sstevel@tonic-gate 			if (!cache || !cacheopt) {
6047c478bd9Sstevel@tonic-gate 				errflg++;
6057c478bd9Sstevel@tonic-gate 				break;
6067c478bd9Sstevel@tonic-gate 			}
6077c478bd9Sstevel@tonic-gate 			cache->nsc_pos_ttl = atoi(cacheopt);
6087c478bd9Sstevel@tonic-gate 			break;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		case 'n':
6117c478bd9Sstevel@tonic-gate 			doset++;
6127c478bd9Sstevel@tonic-gate 			cache = getcacheptr(optarg);
6137c478bd9Sstevel@tonic-gate 			cacheopt = getcacheopt(optarg);
6147c478bd9Sstevel@tonic-gate 			if (!cache || !cacheopt) {
6157c478bd9Sstevel@tonic-gate 				errflg++;
6167c478bd9Sstevel@tonic-gate 				break;
6177c478bd9Sstevel@tonic-gate 			}
6187c478bd9Sstevel@tonic-gate 			cache->nsc_neg_ttl = atoi(cacheopt);
6197c478bd9Sstevel@tonic-gate 			break;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		case 'c':
6227c478bd9Sstevel@tonic-gate 			doset++;
6237c478bd9Sstevel@tonic-gate 			cache = getcacheptr(optarg);
6247c478bd9Sstevel@tonic-gate 			cacheopt = getcacheopt(optarg);
6257c478bd9Sstevel@tonic-gate 			if (!cache || !cacheopt) {
6267c478bd9Sstevel@tonic-gate 				errflg++;
6277c478bd9Sstevel@tonic-gate 				break;
6287c478bd9Sstevel@tonic-gate 			}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 			if (strcmp(cacheopt, "yes") == 0)
6317c478bd9Sstevel@tonic-gate 			    cache->nsc_check_files = 1;
6327c478bd9Sstevel@tonic-gate 			else if (strcmp(cacheopt, "no") == 0)
6337c478bd9Sstevel@tonic-gate 			    cache->nsc_check_files = 0;
6347c478bd9Sstevel@tonic-gate 			else
6357c478bd9Sstevel@tonic-gate 			    errflg++;
6367c478bd9Sstevel@tonic-gate 			break;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		case 'i':
6407c478bd9Sstevel@tonic-gate 			doset++;
6417c478bd9Sstevel@tonic-gate 			cache = getcacheptr(optarg);
6427c478bd9Sstevel@tonic-gate 			if (!cache) {
6437c478bd9Sstevel@tonic-gate 				errflg++;
6447c478bd9Sstevel@tonic-gate 				break;
6457c478bd9Sstevel@tonic-gate 			}
6467c478bd9Sstevel@tonic-gate 			cache->nsc_invalidate = 1;
6477c478bd9Sstevel@tonic-gate 			break;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		case 'l':
6507c478bd9Sstevel@tonic-gate 			doset++;
6517c478bd9Sstevel@tonic-gate 			(void) strlcpy(current_admin.logfile, optarg, 128);
6527c478bd9Sstevel@tonic-gate 			break;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		case 'd':
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 			doset++;
6577c478bd9Sstevel@tonic-gate 			current_admin.debug_level = atoi(optarg);
6587c478bd9Sstevel@tonic-gate 			break;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		case 's':
6617c478bd9Sstevel@tonic-gate 			doset++;
6627c478bd9Sstevel@tonic-gate 			cache = getcacheptr(optarg);
6637c478bd9Sstevel@tonic-gate 			cacheopt = getcacheopt(optarg);
6647c478bd9Sstevel@tonic-gate 			if (!cache || !cacheopt) {
6657c478bd9Sstevel@tonic-gate 				errflg++;
6667c478bd9Sstevel@tonic-gate 				break;
6677c478bd9Sstevel@tonic-gate 			}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 			cache->nsc_suggestedsize = atoi(cacheopt);
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 			break;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		case 'h':
6747c478bd9Sstevel@tonic-gate 			doset++;
6757c478bd9Sstevel@tonic-gate 			cache = getcacheptr(optarg);
6767c478bd9Sstevel@tonic-gate 			cacheopt = getcacheopt(optarg);
6777c478bd9Sstevel@tonic-gate 			if (!cache || !cacheopt) {
6787c478bd9Sstevel@tonic-gate 				errflg++;
6797c478bd9Sstevel@tonic-gate 				break;
6807c478bd9Sstevel@tonic-gate 			}
6817c478bd9Sstevel@tonic-gate 			cache->nsc_keephot = atoi(cacheopt);
6827c478bd9Sstevel@tonic-gate 			break;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		case 'o':
6857c478bd9Sstevel@tonic-gate 			doset++;
6867c478bd9Sstevel@tonic-gate 			cache = getcacheptr(optarg);
6877c478bd9Sstevel@tonic-gate 			cacheopt = getcacheopt(optarg);
6887c478bd9Sstevel@tonic-gate 			if (!cache || !cacheopt) {
6897c478bd9Sstevel@tonic-gate 				errflg++;
6907c478bd9Sstevel@tonic-gate 				break;
6917c478bd9Sstevel@tonic-gate 			}
6927c478bd9Sstevel@tonic-gate 			if (strcmp(cacheopt, "yes") == 0)
6937c478bd9Sstevel@tonic-gate 			    cache->nsc_old_data_ok = 1;
6947c478bd9Sstevel@tonic-gate 			else if (strcmp(cacheopt, "no") == 0)
6957c478bd9Sstevel@tonic-gate 			    cache->nsc_old_data_ok = 0;
6967c478bd9Sstevel@tonic-gate 			else
6977c478bd9Sstevel@tonic-gate 			    errflg++;
6987c478bd9Sstevel@tonic-gate 			break;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		case 'e':
7017c478bd9Sstevel@tonic-gate 			doset++;
7027c478bd9Sstevel@tonic-gate 			cache = getcacheptr(optarg);
7037c478bd9Sstevel@tonic-gate 			cacheopt = getcacheopt(optarg);
7047c478bd9Sstevel@tonic-gate 			if (!cache || !cacheopt) {
7057c478bd9Sstevel@tonic-gate 				errflg++;
7067c478bd9Sstevel@tonic-gate 				break;
7077c478bd9Sstevel@tonic-gate 			}
7087c478bd9Sstevel@tonic-gate 			if (strcmp(cacheopt, "yes") == 0)
7097c478bd9Sstevel@tonic-gate 			    cache->nsc_enabled = 1;
7107c478bd9Sstevel@tonic-gate 			else if (strcmp(cacheopt, "no") == 0)
7117c478bd9Sstevel@tonic-gate 			    cache->nsc_enabled = 0;
7127c478bd9Sstevel@tonic-gate 			else
7137c478bd9Sstevel@tonic-gate 			    errflg++;
7147c478bd9Sstevel@tonic-gate 			break;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		default:
7177c478bd9Sstevel@tonic-gate 			errflg++;
7187c478bd9Sstevel@tonic-gate 			break;
7197c478bd9Sstevel@tonic-gate 		}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (errflg)
7247c478bd9Sstevel@tonic-gate 	    usage(argv[0]);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (!will_become_server) {
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		if (showstats) {
7297c478bd9Sstevel@tonic-gate 			client_showstats(&current_admin);
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		if (doset) {
7337c478bd9Sstevel@tonic-gate 			if (client_setadmin(&current_admin) < 0) {
7347c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
7357c478bd9Sstevel@tonic-gate 					"Error during admin call\n");
7367c478bd9Sstevel@tonic-gate 				exit(1);
7377c478bd9Sstevel@tonic-gate 			}
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 		if (!showstats && !doset) {
7407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7417c478bd9Sstevel@tonic-gate 				"%s already running.... no admin specified\n",
7427c478bd9Sstevel@tonic-gate 				argv[0]);
7437c478bd9Sstevel@tonic-gate 		}
7447c478bd9Sstevel@tonic-gate 		exit(0);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/*
7487c478bd9Sstevel@tonic-gate 	 *   daemon from here ou
7497c478bd9Sstevel@tonic-gate 	 */
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	if (!loaded_config_file) {
7527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7537c478bd9Sstevel@tonic-gate 			"No configuration file specifed and /etc/nscd.conf" \
7547c478bd9Sstevel@tonic-gate 			"not present\n");
7557c478bd9Sstevel@tonic-gate 		exit(1);
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	saved_argv = argv;
7597c478bd9Sstevel@tonic-gate 	save_execname();
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level) {
7627c478bd9Sstevel@tonic-gate 		/* we're debugging... */
7637c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
7647c478bd9Sstevel@tonic-gate 		/* no specified log file */
7657c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, "stderr");
7667c478bd9Sstevel@tonic-gate 		else
7677c478bd9Sstevel@tonic-gate 			(void) nscd_set_lf(&current_admin,
7687c478bd9Sstevel@tonic-gate 			    current_admin.logfile);
7697c478bd9Sstevel@tonic-gate 	} else {
7707c478bd9Sstevel@tonic-gate 		if (strlen(current_admin.logfile) == 0)
7717c478bd9Sstevel@tonic-gate 			(void) strcpy(current_admin.logfile, "/dev/null");
7727c478bd9Sstevel@tonic-gate 		(void) nscd_set_lf(&current_admin, current_admin.logfile);
7737c478bd9Sstevel@tonic-gate 		detachfromtty();
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	/* perform some initialization */
7777c478bd9Sstevel@tonic-gate 	initialize_lookup_clearance();
7787c478bd9Sstevel@tonic-gate 	keep_open_dns_socket();
7797c478bd9Sstevel@tonic-gate 	getpw_init();
7807c478bd9Sstevel@tonic-gate 	getgr_init();
7817c478bd9Sstevel@tonic-gate 	gethost_init();
7827c478bd9Sstevel@tonic-gate 	getnode_init();
7837c478bd9Sstevel@tonic-gate 	getexec_init();
7847c478bd9Sstevel@tonic-gate 	getprof_init();
7857c478bd9Sstevel@tonic-gate 	getuser_init();
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	/* Establish our own server thread pool */
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	door_server_create(server_create);
7907c478bd9Sstevel@tonic-gate 	if (thr_keycreate(&server_key, server_destroy) != 0) {
7917c478bd9Sstevel@tonic-gate 		perror("thr_keycreate");
7927c478bd9Sstevel@tonic-gate 		exit(-1);
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	/* Create a door */
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if ((did = door_create(switcher, NAME_SERVICE_DOOR_COOKIE,
7987c478bd9Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
7997c478bd9Sstevel@tonic-gate 		perror("door_create");
8007c478bd9Sstevel@tonic-gate 		exit(-1);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	/* bind to file system */
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if (stat(NAME_SERVICE_DOOR, &buf) < 0) {
8067c478bd9Sstevel@tonic-gate 		int newfd;
8077c478bd9Sstevel@tonic-gate 		if ((newfd = creat(NAME_SERVICE_DOOR, 0444)) < 0) {
8087c478bd9Sstevel@tonic-gate 			logit("Cannot create %s:%s\n",
8097c478bd9Sstevel@tonic-gate 				NAME_SERVICE_DOOR,
8107c478bd9Sstevel@tonic-gate 				strerror(errno));
8117c478bd9Sstevel@tonic-gate 			exit(1);
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		(void) close(newfd);
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	if (fattach(did, NAME_SERVICE_DOOR) < 0) {
8177c478bd9Sstevel@tonic-gate 		if ((errno != EBUSY) ||
8187c478bd9Sstevel@tonic-gate 		    (fdetach(NAME_SERVICE_DOOR) <  0) ||
8197c478bd9Sstevel@tonic-gate 		    (fattach(did, NAME_SERVICE_DOOR) < 0)) {
8207c478bd9Sstevel@tonic-gate 			perror("door_attach");
8217c478bd9Sstevel@tonic-gate 			exit(2);
8227c478bd9Sstevel@tonic-gate 		}
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	action.sa_handler = dozip;
8267c478bd9Sstevel@tonic-gate 	action.sa_flags = 0;
8277c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&action.sa_mask);
8287c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&myset);
8297c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGHUP);
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	if (sigaction(SIGHUP, &action, NULL) < 0) {
8327c478bd9Sstevel@tonic-gate 		perror("sigaction");
8337c478bd9Sstevel@tonic-gate 		exit(1);
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_BLOCK, &myset, NULL) < 0) {
8377c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
8387c478bd9Sstevel@tonic-gate 		exit(1);
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	/*
8437c478bd9Sstevel@tonic-gate 	 *  kick off revalidate threads
8447c478bd9Sstevel@tonic-gate 	 */
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
8477c478bd9Sstevel@tonic-gate 		(void *(*)(void *))getpw_revalidate, 0, 0, NULL) != 0) {
8487c478bd9Sstevel@tonic-gate 		perror("thr_create");
8497c478bd9Sstevel@tonic-gate 		exit(1);
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
8537c478bd9Sstevel@tonic-gate 		(void *(*)(void *))gethost_revalidate, 0, 0, NULL) != 0) {
8547c478bd9Sstevel@tonic-gate 		perror("thr_create");
8557c478bd9Sstevel@tonic-gate 		exit(1);
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
8597c478bd9Sstevel@tonic-gate 		(void *(*)(void*))getnode_revalidate, 0, 0, NULL) != 0) {
8607c478bd9Sstevel@tonic-gate 		perror("thr_create");
8617c478bd9Sstevel@tonic-gate 		exit(1);
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
8657c478bd9Sstevel@tonic-gate 		(void *(*)(void*))getgr_revalidate, 0, 0, NULL) != 0) {
8667c478bd9Sstevel@tonic-gate 		perror("thr_create");
8677c478bd9Sstevel@tonic-gate 		exit(1);
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
8717c478bd9Sstevel@tonic-gate 	    (void *(*)(void*))getexec_revalidate, 0, 0, NULL) != 0) {
8727c478bd9Sstevel@tonic-gate 		perror("thr_create");
8737c478bd9Sstevel@tonic-gate 		exit(1);
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
8777c478bd9Sstevel@tonic-gate 	    (void *(*)(void*))getprof_revalidate, 0, 0, NULL) != 0) {
8787c478bd9Sstevel@tonic-gate 		perror("thr_create");
8797c478bd9Sstevel@tonic-gate 		exit(1);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
8837c478bd9Sstevel@tonic-gate 	    (void *(*)(void*))getuser_revalidate, 0, 0, NULL) != 0) {
8847c478bd9Sstevel@tonic-gate 		perror("thr_create");
8857c478bd9Sstevel@tonic-gate 		exit(1);
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	/*
8897c478bd9Sstevel@tonic-gate 	 *  kick off reaper threads
8907c478bd9Sstevel@tonic-gate 	 */
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
8937c478bd9Sstevel@tonic-gate 	    (void *(*)(void *))getpw_uid_reaper, 0, 0, NULL) != 0) {
8947c478bd9Sstevel@tonic-gate 		perror("thr_create");
8957c478bd9Sstevel@tonic-gate 		exit(1);
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
8997c478bd9Sstevel@tonic-gate 	    (void *(*)(void *))getpw_nam_reaper, 0, 0, NULL) != 0) {
9007c478bd9Sstevel@tonic-gate 		perror("thr_create");
9017c478bd9Sstevel@tonic-gate 		exit(1);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
9057c478bd9Sstevel@tonic-gate 	    (void *(*)(void *))getgr_uid_reaper, 0, 0, NULL) != 0) {
9067c478bd9Sstevel@tonic-gate 		perror("thr_create");
9077c478bd9Sstevel@tonic-gate 		exit(1);
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
9117c478bd9Sstevel@tonic-gate 	    (void *(*)(void *))getgr_nam_reaper, 0, 0, NULL) != 0) {
9127c478bd9Sstevel@tonic-gate 		perror("thr_create");
9137c478bd9Sstevel@tonic-gate 		exit(1);
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
9187c478bd9Sstevel@tonic-gate 	    (void *(*)(void *))gethost_nam_reaper, 0, 0, NULL) != 0) {
9197c478bd9Sstevel@tonic-gate 		perror("thr_create");
9207c478bd9Sstevel@tonic-gate 		exit(1);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
9247c478bd9Sstevel@tonic-gate 	    (void *(*)(void *))gethost_addr_reaper, 0, 0, NULL) != 0) {
9257c478bd9Sstevel@tonic-gate 		perror("thr_create");
9267c478bd9Sstevel@tonic-gate 		exit(1);
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
9307c478bd9Sstevel@tonic-gate 	    (void *(*)(void *))getnode_nam_reaper, 0, 0, NULL) != 0) {
9317c478bd9Sstevel@tonic-gate 		perror("thr_create");
9327c478bd9Sstevel@tonic-gate 		exit(1);
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
9367c478bd9Sstevel@tonic-gate 	    (void *(*)(void *))getnode_addr_reaper, 0, 0, NULL) != 0) {
9377c478bd9Sstevel@tonic-gate 		perror("thr_create");
9387c478bd9Sstevel@tonic-gate 		exit(1);
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
9427c478bd9Sstevel@tonic-gate 	    (void *(*)(void *))getexec_reaper, 0, 0, NULL) != 0) {
9437c478bd9Sstevel@tonic-gate 		perror("thr_create");
9447c478bd9Sstevel@tonic-gate 		exit(1);
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
9487c478bd9Sstevel@tonic-gate 	    (void *(*)(void *))getprof_reaper, 0, 0, NULL) != 0) {
9497c478bd9Sstevel@tonic-gate 		perror("thr_create");
9507c478bd9Sstevel@tonic-gate 		exit(1);
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
9547c478bd9Sstevel@tonic-gate 	    (void *(*)(void *))getuser_reaper, 0, 0, NULL) != 0) {
9557c478bd9Sstevel@tonic-gate 		perror("thr_create");
9567c478bd9Sstevel@tonic-gate 		exit(1);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	/*
9607c478bd9Sstevel@tonic-gate 	 * kick off routing socket monitor thread
9617c478bd9Sstevel@tonic-gate 	 */
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL,
9647c478bd9Sstevel@tonic-gate 		(void *(*)(void *))rts_mon, 0, 0, NULL) != 0) {
9657c478bd9Sstevel@tonic-gate 		perror("thr_create");
9667c478bd9Sstevel@tonic-gate 		exit(1);
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (thr_sigsetmask(SIG_UNBLOCK, &myset, NULL) < 0) {
9707c478bd9Sstevel@tonic-gate 		perror("thr_sigsetmask");
971*f166393fSesolom 		return (1);
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	for (;;) {
9757c478bd9Sstevel@tonic-gate 		(void) pause();
9767c478bd9Sstevel@tonic-gate 		logit("Reloading /etc/nscd.conf\n");
9777c478bd9Sstevel@tonic-gate 		nscd_parse(argv[0], "/etc/nscd.conf");
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9837c478bd9Sstevel@tonic-gate static void
9847c478bd9Sstevel@tonic-gate switcher(void *cookie, char *argp, size_t arg_size,
9857c478bd9Sstevel@tonic-gate     door_desc_t *dp, uint_t n_desc)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate 	union {
9887c478bd9Sstevel@tonic-gate 		nsc_data_t	data;
9897c478bd9Sstevel@tonic-gate 		char		space[8192];
9907c478bd9Sstevel@tonic-gate 	} u;
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	time_t now;
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	static time_t last_nsswitch_check;
9957c478bd9Sstevel@tonic-gate 	static time_t last_nsswitch_modified;
9967c478bd9Sstevel@tonic-gate 	static time_t last_resolv_modified;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	static mutex_t nsswitch_lock;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	nsc_call_t *ptr = (nsc_call_t *)argp;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	if (argp == DOOR_UNREF_DATA) {
10037c478bd9Sstevel@tonic-gate 		(void) printf("Door Slam... exiting\n");
10047c478bd9Sstevel@tonic-gate 		exit(0);
10057c478bd9Sstevel@tonic-gate 	}
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	if (ptr == NULL) { /* empty door call */
10087c478bd9Sstevel@tonic-gate 		(void) door_return(NULL, 0, 0, 0); /* return the favor */
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	now = time(NULL);
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	/*
10147c478bd9Sstevel@tonic-gate 	 *  just in case check
10157c478bd9Sstevel@tonic-gate 	 */
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&nsswitch_lock);
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (now - last_nsswitch_check > 10) {
10207c478bd9Sstevel@tonic-gate 		struct stat nss_buf;
10217c478bd9Sstevel@tonic-gate 		struct stat res_buf;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 		last_nsswitch_check = now;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&nsswitch_lock); /* let others continue */
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 		/*
10287c478bd9Sstevel@tonic-gate 		 *  This code keeps us from statting resolv.conf
10297c478bd9Sstevel@tonic-gate 		 *  if it doesn't exist, yet prevents us from ignoring
10307c478bd9Sstevel@tonic-gate 		 *  it if it happens to disappear later on for a bit.
10317c478bd9Sstevel@tonic-gate 		 */
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 		if (last_resolv_modified >= 0) {
10347c478bd9Sstevel@tonic-gate 			if (stat("/etc/resolv.conf", &res_buf) < 0) {
10357c478bd9Sstevel@tonic-gate 				if (last_resolv_modified == 0)
10367c478bd9Sstevel@tonic-gate 				    last_resolv_modified = -1;
10377c478bd9Sstevel@tonic-gate 				else
10387c478bd9Sstevel@tonic-gate 				    res_buf.st_mtime = last_resolv_modified;
10397c478bd9Sstevel@tonic-gate 			} else if (last_resolv_modified == 0) {
10407c478bd9Sstevel@tonic-gate 			    last_resolv_modified = res_buf.st_mtime;
10417c478bd9Sstevel@tonic-gate 			}
10427c478bd9Sstevel@tonic-gate 		}
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 		if (stat("/etc/nsswitch.conf", &nss_buf) < 0) {
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 		} else if (last_nsswitch_modified == 0) {
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 			last_nsswitch_modified = nss_buf.st_mtime;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 		} else if ((last_nsswitch_modified < nss_buf.st_mtime) ||
10537c478bd9Sstevel@tonic-gate 		    ((last_resolv_modified > 0) &&
10547c478bd9Sstevel@tonic-gate 		    (last_resolv_modified < res_buf.st_mtime))) {
10557c478bd9Sstevel@tonic-gate 			static mutex_t exit_lock;
10567c478bd9Sstevel@tonic-gate 			char *fmri;
10577c478bd9Sstevel@tonic-gate 			/*
10587c478bd9Sstevel@tonic-gate 			 * time for restart
10597c478bd9Sstevel@tonic-gate 			 */
10607c478bd9Sstevel@tonic-gate 			logit("nscd restart due to /etc/nsswitch.conf or "\
10617c478bd9Sstevel@tonic-gate 				"resolv.conf change\n");
10627c478bd9Sstevel@tonic-gate 			/*
10637c478bd9Sstevel@tonic-gate 			 * try to restart under smf
10647c478bd9Sstevel@tonic-gate 			 */
10657c478bd9Sstevel@tonic-gate 			if ((fmri = getenv("SMF_FMRI")) == NULL) {
10667c478bd9Sstevel@tonic-gate 				/* not running under smf - reexec */
10677c478bd9Sstevel@tonic-gate 				execv(saved_execname, saved_argv);
10687c478bd9Sstevel@tonic-gate 				exit(1); /* just in case */
10697c478bd9Sstevel@tonic-gate 			}
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 			mutex_lock(&exit_lock); /* prevent multiple restarts */
10727c478bd9Sstevel@tonic-gate 			if (smf_restart_instance(fmri) == 0)
10737c478bd9Sstevel@tonic-gate 				sleep(10); /* wait a bit */
10747c478bd9Sstevel@tonic-gate 			exit(1); /* give up waiting for resurrection */
10757c478bd9Sstevel@tonic-gate 		}
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	} else
10787c478bd9Sstevel@tonic-gate 	    (void) mutex_unlock(&nsswitch_lock);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	switch (ptr->nsc_callnumber) {
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	case NULLCALL:
10837c478bd9Sstevel@tonic-gate 		u.data.nsc_ret.nsc_return_code = SUCCESS;
10847c478bd9Sstevel@tonic-gate 		u.data.nsc_ret.nsc_bufferbytesused = sizeof (nsc_return_t);
10857c478bd9Sstevel@tonic-gate 		break;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	case GETPWNAM:
10897c478bd9Sstevel@tonic-gate 		*(argp + arg_size - 1) = 0; /* FALLTHROUGH */
10907c478bd9Sstevel@tonic-gate 	case GETPWUID:
10917c478bd9Sstevel@tonic-gate 		getpw_lookup(&u.data.nsc_ret, sizeof (u), ptr, now);
10927c478bd9Sstevel@tonic-gate 		break;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	case GETGRNAM:
10957c478bd9Sstevel@tonic-gate 		*(argp + arg_size - 1) = 0; /* FALLTHROUGH */
10967c478bd9Sstevel@tonic-gate 	case GETGRGID:
10977c478bd9Sstevel@tonic-gate 		getgr_lookup(&u.data.nsc_ret, sizeof (u), ptr, now);
10987c478bd9Sstevel@tonic-gate 		break;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	case GETHOSTBYNAME:
11017c478bd9Sstevel@tonic-gate 		*(argp + arg_size - 1) = 0; /* FALLTHROUGH */
11027c478bd9Sstevel@tonic-gate 	case GETHOSTBYADDR:
11037c478bd9Sstevel@tonic-gate 		gethost_lookup(&u.data.nsc_ret, sizeof (u), ptr, now);
11047c478bd9Sstevel@tonic-gate 		break;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	case GETIPNODEBYNAME:
11077c478bd9Sstevel@tonic-gate 		*(argp + arg_size - 1) = 0; /* FALLTHROUGH */
11087c478bd9Sstevel@tonic-gate 	case GETIPNODEBYADDR:
11097c478bd9Sstevel@tonic-gate 		getnode_lookup(&u.data.nsc_ret, sizeof (u), ptr, now);
11107c478bd9Sstevel@tonic-gate 		break;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	case GETEXECID:
11137c478bd9Sstevel@tonic-gate 		*(argp + arg_size - 1) = 0;
11147c478bd9Sstevel@tonic-gate 		getexec_lookup(&u.data.nsc_ret, sizeof (u), ptr, now);
11157c478bd9Sstevel@tonic-gate 		break;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	case GETPROFNAM:
11187c478bd9Sstevel@tonic-gate 		*(argp + arg_size - 1) = 0;
11197c478bd9Sstevel@tonic-gate 		getprof_lookup(&u.data.nsc_ret, sizeof (u), ptr, now);
11207c478bd9Sstevel@tonic-gate 		break;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	case GETUSERNAM:
11237c478bd9Sstevel@tonic-gate 		*(argp + arg_size - 1) = 0;
11247c478bd9Sstevel@tonic-gate 		getuser_lookup(&u.data.nsc_ret, sizeof (u), ptr, now);
11257c478bd9Sstevel@tonic-gate 		break;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	case GETADMIN:
11287c478bd9Sstevel@tonic-gate 		getadmin(&u.data.nsc_ret, sizeof (u), ptr);
11297c478bd9Sstevel@tonic-gate 		break;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	case SETADMIN:
11327c478bd9Sstevel@tonic-gate 	case KILLSERVER: {
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 		ucred_t *uc = NULL;
11357c478bd9Sstevel@tonic-gate 		const priv_set_t *eset;
11367c478bd9Sstevel@tonic-gate 		zoneid_t zoneid;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 		if (door_ucred(&uc) != 0) {
11397c478bd9Sstevel@tonic-gate 			perror("door_ucred");
11407c478bd9Sstevel@tonic-gate 			u.data.nsc_ret.nsc_return_code = NOTFOUND;
11417c478bd9Sstevel@tonic-gate 			break;
11427c478bd9Sstevel@tonic-gate 		}
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 		eset = ucred_getprivset(uc, PRIV_EFFECTIVE);
11457c478bd9Sstevel@tonic-gate 		zoneid = ucred_getzoneid(uc);
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 		if ((zoneid != GLOBAL_ZONEID && zoneid != getzoneid()) ||
11487c478bd9Sstevel@tonic-gate 		    eset != NULL ? !priv_ismember(eset, PRIV_SYS_ADMIN) :
11497c478bd9Sstevel@tonic-gate 		    ucred_geteuid(uc) != 0) {
11507c478bd9Sstevel@tonic-gate 			logit("SETADMIN call failed(cred): caller pid %d, "
11517c478bd9Sstevel@tonic-gate 			    "uid %d, euid %d, zoneid %d\n", ucred_getpid(uc),
11527c478bd9Sstevel@tonic-gate 			    ucred_getruid(uc), ucred_geteuid(uc), zoneid);
11537c478bd9Sstevel@tonic-gate 			u.data.nsc_ret.nsc_return_code = NOTFOUND;
11547c478bd9Sstevel@tonic-gate 			ucred_free(uc);
11557c478bd9Sstevel@tonic-gate 			break;
11567c478bd9Sstevel@tonic-gate 		}
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 		if (ptr->nsc_callnumber == KILLSERVER) {
11597c478bd9Sstevel@tonic-gate 			logit("Nscd received KILLSERVER cmd from pid %d, "
11607c478bd9Sstevel@tonic-gate 			    "uid %d, euid %d, zoneid %d\n", ucred_getpid(uc),
11617c478bd9Sstevel@tonic-gate 			    ucred_getruid(uc), ucred_geteuid(uc), zoneid);
11627c478bd9Sstevel@tonic-gate 			exit(0);
11637c478bd9Sstevel@tonic-gate 		} else {
11647c478bd9Sstevel@tonic-gate 			if (setadmin(&u.data.nsc_ret, sizeof (u), ptr) != 0)
11657c478bd9Sstevel@tonic-gate 				logit("SETADMIN call failed\n");
11667c478bd9Sstevel@tonic-gate 		}
11677c478bd9Sstevel@tonic-gate 		ucred_free(uc);
11687c478bd9Sstevel@tonic-gate 		break;
11697c478bd9Sstevel@tonic-gate 	}
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	default:
11727c478bd9Sstevel@tonic-gate 		logit("Unknown name service door call op %d\n",
11737c478bd9Sstevel@tonic-gate 		    ptr->nsc_callnumber);
11747c478bd9Sstevel@tonic-gate 		u.data.nsc_ret.nsc_return_code = -1;
11757c478bd9Sstevel@tonic-gate 		u.data.nsc_ret.nsc_bufferbytesused = sizeof (nsc_return_t);
11767c478bd9Sstevel@tonic-gate 		break;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	}
11797c478bd9Sstevel@tonic-gate 	door_return((char *)&u.data, u.data.nsc_ret.nsc_bufferbytesused,
11807c478bd9Sstevel@tonic-gate 	    NULL, 0);
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate /*
11847c478bd9Sstevel@tonic-gate  * Monitor the routing socket.  Address lists stored in the ipnodes
11857c478bd9Sstevel@tonic-gate  * cache are sorted based on destination address selection rules,
11867c478bd9Sstevel@tonic-gate  * so when things change that could affect that sorting (interfaces
11877c478bd9Sstevel@tonic-gate  * go up or down, flags change, etc.), we clear that cache so the
11887c478bd9Sstevel@tonic-gate  * list will be re-ordered the next time the hostname is resolved.
11897c478bd9Sstevel@tonic-gate  */
11907c478bd9Sstevel@tonic-gate static void
11917c478bd9Sstevel@tonic-gate rts_mon(void)
11927c478bd9Sstevel@tonic-gate {
11937c478bd9Sstevel@tonic-gate 	int	rt_sock, rdlen;
11947c478bd9Sstevel@tonic-gate 	union {
11957c478bd9Sstevel@tonic-gate 		struct {
11967c478bd9Sstevel@tonic-gate 			struct rt_msghdr rtm;
11977c478bd9Sstevel@tonic-gate 			struct sockaddr_storage addrs[RTA_NUMBITS];
11987c478bd9Sstevel@tonic-gate 		} r;
11997c478bd9Sstevel@tonic-gate 		struct if_msghdr ifm;
12007c478bd9Sstevel@tonic-gate 		struct ifa_msghdr ifam;
12017c478bd9Sstevel@tonic-gate 	} mbuf;
12027c478bd9Sstevel@tonic-gate 	struct ifa_msghdr *ifam = &mbuf.ifam;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	rt_sock = socket(PF_ROUTE, SOCK_RAW, 0);
12057c478bd9Sstevel@tonic-gate 	if (rt_sock < 0) {
12067c478bd9Sstevel@tonic-gate 		logit("Failed to open routing socket: %s\n", strerror(errno));
12077c478bd9Sstevel@tonic-gate 		thr_exit(0);
12087c478bd9Sstevel@tonic-gate 	}
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	for (;;) {
12117c478bd9Sstevel@tonic-gate 		rdlen = read(rt_sock, &mbuf, sizeof (mbuf));
12127c478bd9Sstevel@tonic-gate 		if (rdlen <= 0) {
12137c478bd9Sstevel@tonic-gate 			if (rdlen == 0 || (errno != EINTR && errno != EAGAIN)) {
12147c478bd9Sstevel@tonic-gate 				logit("routing socket read: %s\n",
12157c478bd9Sstevel@tonic-gate 				    strerror(errno));
12167c478bd9Sstevel@tonic-gate 				thr_exit(0);
12177c478bd9Sstevel@tonic-gate 			}
12187c478bd9Sstevel@tonic-gate 			continue;
12197c478bd9Sstevel@tonic-gate 		}
12207c478bd9Sstevel@tonic-gate 		if (ifam->ifam_version != RTM_VERSION) {
12217c478bd9Sstevel@tonic-gate 			logit("rx unknown version (%d) on routing socket.\n",
12227c478bd9Sstevel@tonic-gate 			    ifam->ifam_version);
12237c478bd9Sstevel@tonic-gate 			continue;
12247c478bd9Sstevel@tonic-gate 		}
12257c478bd9Sstevel@tonic-gate 		switch (ifam->ifam_type) {
12267c478bd9Sstevel@tonic-gate 		case RTM_NEWADDR:
12277c478bd9Sstevel@tonic-gate 		case RTM_DELADDR:
12287c478bd9Sstevel@tonic-gate 			getnode_name_invalidate();
12297c478bd9Sstevel@tonic-gate 			break;
12307c478bd9Sstevel@tonic-gate 		case RTM_ADD:
12317c478bd9Sstevel@tonic-gate 		case RTM_DELETE:
12327c478bd9Sstevel@tonic-gate 		case RTM_CHANGE:
12337c478bd9Sstevel@tonic-gate 		case RTM_GET:
12347c478bd9Sstevel@tonic-gate 		case RTM_LOSING:
12357c478bd9Sstevel@tonic-gate 		case RTM_REDIRECT:
12367c478bd9Sstevel@tonic-gate 		case RTM_MISS:
12377c478bd9Sstevel@tonic-gate 		case RTM_LOCK:
12387c478bd9Sstevel@tonic-gate 		case RTM_OLDADD:
12397c478bd9Sstevel@tonic-gate 		case RTM_OLDDEL:
12407c478bd9Sstevel@tonic-gate 		case RTM_RESOLVE:
12417c478bd9Sstevel@tonic-gate 		case RTM_IFINFO:
12427c478bd9Sstevel@tonic-gate 			break;
12437c478bd9Sstevel@tonic-gate 		default:
12447c478bd9Sstevel@tonic-gate 			logit("rx unknown msg type (%d) on routing socket.\n",
12457c478bd9Sstevel@tonic-gate 			    ifam->ifam_type);
12467c478bd9Sstevel@tonic-gate 			break;
12477c478bd9Sstevel@tonic-gate 		}
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate static void
12527c478bd9Sstevel@tonic-gate usage(char *s)
12537c478bd9Sstevel@tonic-gate {
12547c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
12557c478bd9Sstevel@tonic-gate 		"Usage: %s [-d debug_level] [-l logfilename]\n", s);
12567c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
12577c478bd9Sstevel@tonic-gate 		"	[-p cachename,positive_time_to_live]\n");
12587c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
12597c478bd9Sstevel@tonic-gate 		"	[-n cachename,negative_time_to_live]\n");
12607c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
12617c478bd9Sstevel@tonic-gate 		"	[-i cachename] [-s cachename,suggestedsize]\n");
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
12647c478bd9Sstevel@tonic-gate 		"	[-h cachename,keep_hot_count] "\
12657c478bd9Sstevel@tonic-gate 		"[-o cachename,\"yes\"|\"no\"]\n");
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
12687c478bd9Sstevel@tonic-gate 		"	[-e cachename,\"yes\"|\"no\"] [-g] " \
12697c478bd9Sstevel@tonic-gate 		"[-c cachename,\"yes\"|\"no\"]\n");
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
12727c478bd9Sstevel@tonic-gate 		"	[-f configfilename] \n");
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
12757c478bd9Sstevel@tonic-gate 		"\n	Supported caches: passwd, group, hosts, ipnodes\n");
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
12787c478bd9Sstevel@tonic-gate 		"         exec_attr, prof_attr, and user_attr.\n");
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	exit(1);
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate static int logfd = 2;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate int
12887c478bd9Sstevel@tonic-gate nscd_set_lf(admin_t *ptr, char *s)
12897c478bd9Sstevel@tonic-gate {
12907c478bd9Sstevel@tonic-gate 	int newlogfd;
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	/*
12937c478bd9Sstevel@tonic-gate 	 *  we don't really want to try and open the log file
12947c478bd9Sstevel@tonic-gate 	 *  /dev/null since that will fail w/ our security fixes
12957c478bd9Sstevel@tonic-gate 	 */
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	if (*s == 0) {
12987c478bd9Sstevel@tonic-gate 		/* ignore empty log file specs */
12997c478bd9Sstevel@tonic-gate 		/*EMPTY*/;
13007c478bd9Sstevel@tonic-gate 	} else if (s == NULL || strcmp(s, "/dev/null") == 0) {
13017c478bd9Sstevel@tonic-gate 		(void) strcpy(current_admin.logfile, "/dev/null");
13027c478bd9Sstevel@tonic-gate 		(void) close(logfd);
13037c478bd9Sstevel@tonic-gate 		logfd = -1;
13047c478bd9Sstevel@tonic-gate 	} else {
13057c478bd9Sstevel@tonic-gate 		/*
13067c478bd9Sstevel@tonic-gate 		 * In order to open this file securely, we'll try a few tricks
13077c478bd9Sstevel@tonic-gate 		 */
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 		if ((newlogfd = open(s, O_EXCL|O_WRONLY|O_CREAT, 0644)) < 0) {
13107c478bd9Sstevel@tonic-gate 			/*
13117c478bd9Sstevel@tonic-gate 			 * File already exists... now we need to get cute
13127c478bd9Sstevel@tonic-gate 			 * since opening a file in a world-writeable directory
13137c478bd9Sstevel@tonic-gate 			 * safely is hard = it could be a hard link or a
13147c478bd9Sstevel@tonic-gate 			 * symbolic link to a system file.
13157c478bd9Sstevel@tonic-gate 			 */
13167c478bd9Sstevel@tonic-gate 			struct stat before;
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 			if (lstat(s, &before) < 0) {
13197c478bd9Sstevel@tonic-gate 				logit("Cannot open new logfile \"%s\": %sn",
13207c478bd9Sstevel@tonic-gate 					s, strerror(errno));
13217c478bd9Sstevel@tonic-gate 				return (-1);
13227c478bd9Sstevel@tonic-gate 			}
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 			if (S_ISREG(before.st_mode) && /* no symbolic links */
13257c478bd9Sstevel@tonic-gate 				(before.st_nlink == 1) && /* no hard links */
13267c478bd9Sstevel@tonic-gate 				(before.st_uid == 0)) {   /* owned by root */
13277c478bd9Sstevel@tonic-gate 				if ((newlogfd =
13287c478bd9Sstevel@tonic-gate 				    open(s, O_APPEND|O_WRONLY, 0644)) < 0) {
13297c478bd9Sstevel@tonic-gate 					logit("Cannot open new "\
13307c478bd9Sstevel@tonic-gate 					    "logfile \"%s\": %s\n", s,
13317c478bd9Sstevel@tonic-gate 					    strerror(errno));
13327c478bd9Sstevel@tonic-gate 					return (-1);
13337c478bd9Sstevel@tonic-gate 				}
13347c478bd9Sstevel@tonic-gate 			} else {
13357c478bd9Sstevel@tonic-gate 				logit("Cannot use specified logfile \"%s\": "\
13367c478bd9Sstevel@tonic-gate 				    "file is/has links or isn't owned by "\
13377c478bd9Sstevel@tonic-gate 				    "root\n", s);
13387c478bd9Sstevel@tonic-gate 				return (-1);
13397c478bd9Sstevel@tonic-gate 			}
13407c478bd9Sstevel@tonic-gate 		}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 		(void) strlcpy(ptr->logfile, s, 128);
13437c478bd9Sstevel@tonic-gate 		(void) close(logfd);
13447c478bd9Sstevel@tonic-gate 		logfd = newlogfd;
13457c478bd9Sstevel@tonic-gate 		logit("Start of new logfile %s\n", s);
13467c478bd9Sstevel@tonic-gate 	}
13477c478bd9Sstevel@tonic-gate 	return (0);
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate void
13517c478bd9Sstevel@tonic-gate logit(char *format, ...)
13527c478bd9Sstevel@tonic-gate {
13537c478bd9Sstevel@tonic-gate 	static mutex_t loglock;
13547c478bd9Sstevel@tonic-gate 	struct timeval tv;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate #define	LOGBUFLEN	1024
13577c478bd9Sstevel@tonic-gate 	char buffer[LOGBUFLEN];
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	va_list ap;
13607c478bd9Sstevel@tonic-gate 	va_start(ap, format);
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	if (logfd >= 0) {
13637c478bd9Sstevel@tonic-gate 		int safechars, offset;
13647c478bd9Sstevel@tonic-gate 		if (gettimeofday(&tv, NULL) != 0 ||
13657c478bd9Sstevel@tonic-gate 		    ctime_r(&tv.tv_sec, buffer, LOGBUFLEN) == NULL) {
13667c478bd9Sstevel@tonic-gate 			(void) snprintf(buffer, LOGBUFLEN,
13677c478bd9Sstevel@tonic-gate 			    "<time conversion failed>\t");
13687c478bd9Sstevel@tonic-gate 		} else {
13697c478bd9Sstevel@tonic-gate 			/*
13707c478bd9Sstevel@tonic-gate 			 * ctime_r() includes some stuff we don't want;
13717c478bd9Sstevel@tonic-gate 			 * adjust length to overwrite " YYYY\n".
13727c478bd9Sstevel@tonic-gate 			 */
13737c478bd9Sstevel@tonic-gate 			offset = strlen(buffer) - 6;
13747c478bd9Sstevel@tonic-gate 			safechars = LOGBUFLEN - (offset - 1);
13757c478bd9Sstevel@tonic-gate 			(void) snprintf(buffer + offset, safechars, ".%.4ld\t",
13767c478bd9Sstevel@tonic-gate 			    tv.tv_usec/100);
13777c478bd9Sstevel@tonic-gate 		}
13787c478bd9Sstevel@tonic-gate 		offset = strlen(buffer);
13797c478bd9Sstevel@tonic-gate 		safechars = LOGBUFLEN - (offset - 1);
13807c478bd9Sstevel@tonic-gate 		if (vsnprintf(buffer + offset, safechars, format, ap) >
13817c478bd9Sstevel@tonic-gate 		    safechars) {
13827c478bd9Sstevel@tonic-gate 			(void) strncat(buffer, "...\n", LOGBUFLEN);
13837c478bd9Sstevel@tonic-gate 		}
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&loglock);
13867c478bd9Sstevel@tonic-gate 		(void) write(logfd, buffer, strlen(buffer));
13877c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&loglock);
13887c478bd9Sstevel@tonic-gate 	}
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	va_end(ap);
13917c478bd9Sstevel@tonic-gate #undef	LOGBUFLEN
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate static void
13957c478bd9Sstevel@tonic-gate do_update(nsc_call_t *in)
13967c478bd9Sstevel@tonic-gate {
13977c478bd9Sstevel@tonic-gate 	union {
13987c478bd9Sstevel@tonic-gate 		nsc_data_t	data;
13997c478bd9Sstevel@tonic-gate 		char		space[8192];
14007c478bd9Sstevel@tonic-gate 	} u;
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	time_t now = time(NULL);
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	switch (MASKUPDATEBIT(in->nsc_callnumber)) {
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	case GETPWUID:
14077c478bd9Sstevel@tonic-gate 	case GETPWNAM:
14087c478bd9Sstevel@tonic-gate 		getpw_lookup(&u.data.nsc_ret, sizeof (u), in, now);
14097c478bd9Sstevel@tonic-gate 		break;
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	case GETGRNAM:
14127c478bd9Sstevel@tonic-gate 	case GETGRGID:
14137c478bd9Sstevel@tonic-gate 		getgr_lookup(&u.data.nsc_ret, sizeof (u), in, now);
14147c478bd9Sstevel@tonic-gate 		break;
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	case GETHOSTBYNAME:
14177c478bd9Sstevel@tonic-gate 	case GETHOSTBYADDR:
14187c478bd9Sstevel@tonic-gate 		gethost_lookup(&u.data.nsc_ret, sizeof (u), in, now);
14197c478bd9Sstevel@tonic-gate 		break;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	case GETIPNODEBYNAME:
14227c478bd9Sstevel@tonic-gate 	case GETIPNODEBYADDR:
14237c478bd9Sstevel@tonic-gate 		getnode_lookup(&u.data.nsc_ret, sizeof (u), in, now);
14247c478bd9Sstevel@tonic-gate 		break;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	case GETEXECID:
14277c478bd9Sstevel@tonic-gate 		getexec_lookup(&u.data.nsc_ret, sizeof (u), in, now);
14287c478bd9Sstevel@tonic-gate 		break;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	case GETPROFNAM:
14317c478bd9Sstevel@tonic-gate 		getprof_lookup(&u.data.nsc_ret, sizeof (u), in, now);
14327c478bd9Sstevel@tonic-gate 		break;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	case GETUSERNAM:
14357c478bd9Sstevel@tonic-gate 		getuser_lookup(&u.data.nsc_ret, sizeof (u), in, now);
14367c478bd9Sstevel@tonic-gate 		break;
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	default:
14397c478bd9Sstevel@tonic-gate 		assert(0);
14407c478bd9Sstevel@tonic-gate 		break;
14417c478bd9Sstevel@tonic-gate 	}
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	free(in);
14447c478bd9Sstevel@tonic-gate }
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate int
14477c478bd9Sstevel@tonic-gate launch_update(nsc_call_t *in)
14487c478bd9Sstevel@tonic-gate {
14497c478bd9Sstevel@tonic-gate 	nsc_call_t *c;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	int l = nsc_calllen(in);
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	in->nsc_callnumber |= UPDATEBIT;
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	if ((c = malloc(l)) == NULL) {
14567c478bd9Sstevel@tonic-gate 		logit("thread create failed: %s\n", strerror(errno));
14577c478bd9Sstevel@tonic-gate 		exit(1);
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate 	(void) memcpy(c, in, l);
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
14627c478bd9Sstevel@tonic-gate 		logit("launching update\n");
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	if (thr_create(NULL,
14667c478bd9Sstevel@tonic-gate 	    NULL,
14677c478bd9Sstevel@tonic-gate 	    (void *(*)(void*))do_update,
14687c478bd9Sstevel@tonic-gate 	    c,
14697c478bd9Sstevel@tonic-gate 	    0|THR_DETACHED, NULL) != 0) {
14707c478bd9Sstevel@tonic-gate 		logit("thread create failed\n");
14717c478bd9Sstevel@tonic-gate 		exit(1);
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	return (0);
14757c478bd9Sstevel@tonic-gate }
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate static int
14787c478bd9Sstevel@tonic-gate nsc_calllen(nsc_call_t *in)
14797c478bd9Sstevel@tonic-gate {
14807c478bd9Sstevel@tonic-gate 	switch (MASKUPDATEBIT(in->nsc_callnumber)) {
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	case GETPWUID:
14837c478bd9Sstevel@tonic-gate 	case GETGRGID:
14847c478bd9Sstevel@tonic-gate 	case NULLCALL:
14857c478bd9Sstevel@tonic-gate 		return (sizeof (*in));
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	case GETPWNAM:
14887c478bd9Sstevel@tonic-gate 	case GETGRNAM:
14897c478bd9Sstevel@tonic-gate 	case GETHOSTBYNAME:
14907c478bd9Sstevel@tonic-gate 		return (sizeof (*in) + strlen(in->nsc_u.name));
14917c478bd9Sstevel@tonic-gate 	case GETIPNODEBYNAME:
14927c478bd9Sstevel@tonic-gate 		return (sizeof (*in) + strlen(in->nsc_u.ipnode.name));
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	case GETHOSTBYADDR:
14957c478bd9Sstevel@tonic-gate 	case GETIPNODEBYADDR:
14967c478bd9Sstevel@tonic-gate 		return (sizeof (*in) + in->nsc_u.addr.a_length);
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	case GETEXECID:
14997c478bd9Sstevel@tonic-gate 	case GETPROFNAM:
15007c478bd9Sstevel@tonic-gate 	case GETUSERNAM:
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 		return (sizeof (*in) + strlen(in->nsc_u.name));
15037c478bd9Sstevel@tonic-gate 	}
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	return (0);
15067c478bd9Sstevel@tonic-gate }
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate static int
15097c478bd9Sstevel@tonic-gate client_getadmin(admin_t *ptr)
15107c478bd9Sstevel@tonic-gate {
15117c478bd9Sstevel@tonic-gate 	union {
15127c478bd9Sstevel@tonic-gate 		nsc_data_t data;
15137c478bd9Sstevel@tonic-gate 		char space[8192];
15147c478bd9Sstevel@tonic-gate 	} u;
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	nsc_data_t *dptr;
15177c478bd9Sstevel@tonic-gate 	int ndata;
15187c478bd9Sstevel@tonic-gate 	int adata;
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	u.data.nsc_call.nsc_callnumber = GETADMIN;
15217c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
15227c478bd9Sstevel@tonic-gate 	adata = sizeof (u.data);
15237c478bd9Sstevel@tonic-gate 	dptr = &u.data;
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	if (_nsc_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
15267c478bd9Sstevel@tonic-gate 		return (-1);
15277c478bd9Sstevel@tonic-gate 	}
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	(void) memcpy(ptr, dptr->nsc_ret.nsc_u.buff, sizeof (*ptr));
15307c478bd9Sstevel@tonic-gate 	return (0);
15317c478bd9Sstevel@tonic-gate }
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15347c478bd9Sstevel@tonic-gate static void
15357c478bd9Sstevel@tonic-gate getadmin(nsc_return_t *out, int size, nsc_call_t *ptr)
15367c478bd9Sstevel@tonic-gate {
15377c478bd9Sstevel@tonic-gate 	out->nsc_return_code = SUCCESS;
15387c478bd9Sstevel@tonic-gate 	out->nsc_bufferbytesused = sizeof (current_admin);
15397c478bd9Sstevel@tonic-gate 	(void) memcpy(out->nsc_u.buff, &current_admin, sizeof (current_admin));
15407c478bd9Sstevel@tonic-gate }
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate static int
15447c478bd9Sstevel@tonic-gate nscd_set_rbac(admin_t *new_admin, int invalidate)
15457c478bd9Sstevel@tonic-gate {
15467c478bd9Sstevel@tonic-gate 	int		i;
15477c478bd9Sstevel@tonic-gate 	char		*dbname = NULL;
15487c478bd9Sstevel@tonic-gate 	nsc_stat_t	*cache = NULL;
15497c478bd9Sstevel@tonic-gate 	nsc_stat_t	*new = NULL;
15507c478bd9Sstevel@tonic-gate 	void		(*invalidate_func)(void);
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	for (i = 1; i <= 3; i++) {
15547c478bd9Sstevel@tonic-gate 		/*
15557c478bd9Sstevel@tonic-gate 		 * Three of the RBAC databases are cached.
15567c478bd9Sstevel@tonic-gate 		 */
15577c478bd9Sstevel@tonic-gate 		switch (i) {
15587c478bd9Sstevel@tonic-gate 		case 1:
15597c478bd9Sstevel@tonic-gate 			dbname = NSS_DBNAM_EXECATTR;
15607c478bd9Sstevel@tonic-gate 			cache = &current_admin.exec;
15617c478bd9Sstevel@tonic-gate 			new = &new_admin->exec;
15627c478bd9Sstevel@tonic-gate 			invalidate_func = getexec_invalidate;
15637c478bd9Sstevel@tonic-gate 			break;
15647c478bd9Sstevel@tonic-gate 		case 2:
15657c478bd9Sstevel@tonic-gate 			dbname = NSS_DBNAM_PROFATTR;
15667c478bd9Sstevel@tonic-gate 			cache = &current_admin.prof;
15677c478bd9Sstevel@tonic-gate 			new = &new_admin->prof;
15687c478bd9Sstevel@tonic-gate 			invalidate_func = getprof_invalidate;
15697c478bd9Sstevel@tonic-gate 			break;
15707c478bd9Sstevel@tonic-gate 		case 3:
15717c478bd9Sstevel@tonic-gate 			dbname = NSS_DBNAM_USERATTR;
15727c478bd9Sstevel@tonic-gate 			cache = &current_admin.user;
15737c478bd9Sstevel@tonic-gate 			new = &new_admin->user;
15747c478bd9Sstevel@tonic-gate 			invalidate_func = getuser_invalidate;
15757c478bd9Sstevel@tonic-gate 			break;
15767c478bd9Sstevel@tonic-gate 		default:
15777c478bd9Sstevel@tonic-gate 			break;
15787c478bd9Sstevel@tonic-gate 		}
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 		if (invalidate) {
15817c478bd9Sstevel@tonic-gate 			if (new->nsc_invalidate) {
15827c478bd9Sstevel@tonic-gate 				logit("Invalidating %s cache\n", dbname);
15837c478bd9Sstevel@tonic-gate 				(*invalidate_func)();
15847c478bd9Sstevel@tonic-gate 			}
15857c478bd9Sstevel@tonic-gate 		} else {
15867c478bd9Sstevel@tonic-gate 			if (nscd_set_ttl_positive(cache, dbname,
15877c478bd9Sstevel@tonic-gate 			    new->nsc_pos_ttl) < 0 ||
15887c478bd9Sstevel@tonic-gate 			    nscd_set_ttl_negative(cache, dbname,
15897c478bd9Sstevel@tonic-gate 			    new->nsc_neg_ttl) < 0 ||
15907c478bd9Sstevel@tonic-gate 			    nscd_set_khc(cache, dbname, new->nsc_keephot) < 0 ||
15917c478bd9Sstevel@tonic-gate 			    nscd_set_odo(cache, dbname,
15927c478bd9Sstevel@tonic-gate 			    new->nsc_old_data_ok) < 0 ||
15937c478bd9Sstevel@tonic-gate 			    nscd_set_ec(cache, dbname, new->nsc_enabled) < 0 ||
15947c478bd9Sstevel@tonic-gate 			    nscd_set_ss(cache, dbname,
15957c478bd9Sstevel@tonic-gate 			    new->nsc_suggestedsize) < 0)
15967c478bd9Sstevel@tonic-gate 				return (-1);
15977c478bd9Sstevel@tonic-gate 		}
15987c478bd9Sstevel@tonic-gate 	}
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	return (0);
16017c478bd9Sstevel@tonic-gate }
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16047c478bd9Sstevel@tonic-gate static int
16057c478bd9Sstevel@tonic-gate setadmin(nsc_return_t *out, int size, nsc_call_t *ptr)
16067c478bd9Sstevel@tonic-gate {
16077c478bd9Sstevel@tonic-gate 	admin_t *new;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	out->nsc_return_code = SUCCESS;
16107c478bd9Sstevel@tonic-gate 	out->nsc_bufferbytesused = sizeof (nsc_return_t);
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	new = (admin_t *)ptr->nsc_u.name;
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	/*
16167c478bd9Sstevel@tonic-gate 	 *  global admin stuff
16177c478bd9Sstevel@tonic-gate 	 */
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	if ((nscd_set_lf(&current_admin, new->logfile) < 0) ||
16207c478bd9Sstevel@tonic-gate 	    nscd_set_dl(&current_admin, new->debug_level) < 0) {
16217c478bd9Sstevel@tonic-gate 		out->nsc_return_code = NOTFOUND;
16227c478bd9Sstevel@tonic-gate 		return (-1);
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	/*
16267c478bd9Sstevel@tonic-gate 	 * per cache items
16277c478bd9Sstevel@tonic-gate 	 */
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	if (new->passwd.nsc_invalidate) {
16307c478bd9Sstevel@tonic-gate 		logit("Invalidating passwd cache\n");
16317c478bd9Sstevel@tonic-gate 		getpw_invalidate();
16327c478bd9Sstevel@tonic-gate 	}
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	if (new->group.nsc_invalidate) {
16357c478bd9Sstevel@tonic-gate 		logit("Invalidating group cache\n");
16367c478bd9Sstevel@tonic-gate 		getgr_invalidate();
16377c478bd9Sstevel@tonic-gate 	}
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	if (new->host.nsc_invalidate) {
16407c478bd9Sstevel@tonic-gate 		logit("Invalidating host cache\n");
16417c478bd9Sstevel@tonic-gate 		gethost_invalidate();
16427c478bd9Sstevel@tonic-gate 	}
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	if (new->node.nsc_invalidate) {
16457c478bd9Sstevel@tonic-gate 		logit("Invalidating ipnodes cache\n");
16467c478bd9Sstevel@tonic-gate 		getnode_invalidate();
16477c478bd9Sstevel@tonic-gate 	}
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	(void) nscd_set_rbac(new, 1);		/* invalidate rbac cache */
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	if (nscd_set_ttl_positive(&current_admin.passwd,
16527c478bd9Sstevel@tonic-gate 			"passwd",
16537c478bd9Sstevel@tonic-gate 			new->passwd.nsc_pos_ttl) < 0		||
16547c478bd9Sstevel@tonic-gate 	    nscd_set_ttl_negative(&current_admin.passwd,
16557c478bd9Sstevel@tonic-gate 			"passwd",
16567c478bd9Sstevel@tonic-gate 			new->passwd.nsc_neg_ttl) < 0		||
16577c478bd9Sstevel@tonic-gate 	    nscd_set_khc(&current_admin.passwd,
16587c478bd9Sstevel@tonic-gate 			"passwd",
16597c478bd9Sstevel@tonic-gate 			new->passwd.nsc_keephot) < 0		||
16607c478bd9Sstevel@tonic-gate 	    nscd_set_odo(&current_admin.passwd,
16617c478bd9Sstevel@tonic-gate 			"passwd",
16627c478bd9Sstevel@tonic-gate 			new->passwd.nsc_old_data_ok) < 0	||
16637c478bd9Sstevel@tonic-gate 	    nscd_set_ec(&current_admin.passwd,
16647c478bd9Sstevel@tonic-gate 			"passwd",
16657c478bd9Sstevel@tonic-gate 			new->passwd.nsc_enabled) < 0		||
16667c478bd9Sstevel@tonic-gate 	    nscd_set_ss(&current_admin.passwd,
16677c478bd9Sstevel@tonic-gate 			"passwd",
16687c478bd9Sstevel@tonic-gate 			new->passwd.nsc_suggestedsize) < 0	   ||
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	    nscd_set_ttl_positive(&current_admin.group,
16717c478bd9Sstevel@tonic-gate 			"group",
16727c478bd9Sstevel@tonic-gate 			new->group.nsc_pos_ttl) < 0		||
16737c478bd9Sstevel@tonic-gate 	    nscd_set_ttl_negative(&current_admin.group,
16747c478bd9Sstevel@tonic-gate 			"group",
16757c478bd9Sstevel@tonic-gate 			new->group.nsc_neg_ttl) < 0		||
16767c478bd9Sstevel@tonic-gate 	    nscd_set_khc(&current_admin.group,
16777c478bd9Sstevel@tonic-gate 			"group",
16787c478bd9Sstevel@tonic-gate 			new->group.nsc_keephot) < 0		||
16797c478bd9Sstevel@tonic-gate 	    nscd_set_odo(&current_admin.group,
16807c478bd9Sstevel@tonic-gate 			"group",
16817c478bd9Sstevel@tonic-gate 			new->group.nsc_old_data_ok) < 0		||
16827c478bd9Sstevel@tonic-gate 	    nscd_set_ec(&current_admin.group,
16837c478bd9Sstevel@tonic-gate 			"group",
16847c478bd9Sstevel@tonic-gate 			new->group.nsc_enabled) < 0		||
16857c478bd9Sstevel@tonic-gate 	    nscd_set_ss(&current_admin.group,
16867c478bd9Sstevel@tonic-gate 			"group",
16877c478bd9Sstevel@tonic-gate 			new->group.nsc_suggestedsize) < 0	||
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	    nscd_set_ttl_positive(&current_admin.node,
16907c478bd9Sstevel@tonic-gate 			"ipnodes",
16917c478bd9Sstevel@tonic-gate 			new->node.nsc_pos_ttl) < 0		||
16927c478bd9Sstevel@tonic-gate 	    nscd_set_ttl_negative(&current_admin.node,
16937c478bd9Sstevel@tonic-gate 			"ipnodes",
16947c478bd9Sstevel@tonic-gate 			new->node.nsc_neg_ttl) < 0		||
16957c478bd9Sstevel@tonic-gate 	    nscd_set_khc(&current_admin.node,
16967c478bd9Sstevel@tonic-gate 			"ipnodes",
16977c478bd9Sstevel@tonic-gate 			new->node.nsc_keephot) < 0		||
16987c478bd9Sstevel@tonic-gate 	    nscd_set_odo(&current_admin.node,
16997c478bd9Sstevel@tonic-gate 			"ipnodes",
17007c478bd9Sstevel@tonic-gate 			new->node.nsc_old_data_ok) < 0		||
17017c478bd9Sstevel@tonic-gate 	    nscd_set_ec(&current_admin.node,
17027c478bd9Sstevel@tonic-gate 			"ipnodes",
17037c478bd9Sstevel@tonic-gate 			new->node.nsc_enabled) < 0		||
17047c478bd9Sstevel@tonic-gate 	    nscd_set_ss(&current_admin.node,
17057c478bd9Sstevel@tonic-gate 			"ipnodes",
17067c478bd9Sstevel@tonic-gate 			new->node.nsc_suggestedsize) < 0	||
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	    nscd_set_ttl_positive(&current_admin.host,
17097c478bd9Sstevel@tonic-gate 			"hosts",
17107c478bd9Sstevel@tonic-gate 			new->host.nsc_pos_ttl) < 0		||
17117c478bd9Sstevel@tonic-gate 	    nscd_set_ttl_negative(&current_admin.host,
17127c478bd9Sstevel@tonic-gate 			"hosts",
17137c478bd9Sstevel@tonic-gate 			new->host.nsc_neg_ttl) < 0		||
17147c478bd9Sstevel@tonic-gate 	    nscd_set_khc(&current_admin.host,
17157c478bd9Sstevel@tonic-gate 			"hosts",
17167c478bd9Sstevel@tonic-gate 			new->host.nsc_keephot) < 0		||
17177c478bd9Sstevel@tonic-gate 	    nscd_set_odo(&current_admin.host,
17187c478bd9Sstevel@tonic-gate 			"hosts",
17197c478bd9Sstevel@tonic-gate 			new->host.nsc_old_data_ok) < 0		||
17207c478bd9Sstevel@tonic-gate 	    nscd_set_ec(&current_admin.host,
17217c478bd9Sstevel@tonic-gate 			"hosts",
17227c478bd9Sstevel@tonic-gate 			new->host.nsc_enabled) < 0		||
17237c478bd9Sstevel@tonic-gate 	    nscd_set_ss(&current_admin.host,
17247c478bd9Sstevel@tonic-gate 			"hosts",
17257c478bd9Sstevel@tonic-gate 			new->host.nsc_suggestedsize) < 0	||
17267c478bd9Sstevel@tonic-gate 	    nscd_set_rbac(new, 0) < 0) {
17277c478bd9Sstevel@tonic-gate 		out->nsc_return_code = NOTFOUND;
17287c478bd9Sstevel@tonic-gate 		return (-1);
17297c478bd9Sstevel@tonic-gate 	}
17307c478bd9Sstevel@tonic-gate 	out->nsc_return_code = SUCCESS;
17317c478bd9Sstevel@tonic-gate 	return (0);
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate void
17357c478bd9Sstevel@tonic-gate client_killserver(void)
17367c478bd9Sstevel@tonic-gate {
17377c478bd9Sstevel@tonic-gate 	union {
17387c478bd9Sstevel@tonic-gate 		nsc_data_t data;
17397c478bd9Sstevel@tonic-gate 		char space[8192];
17407c478bd9Sstevel@tonic-gate 	} u;
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	nsc_data_t *dptr;
17437c478bd9Sstevel@tonic-gate 	int ndata;
17447c478bd9Sstevel@tonic-gate 	int adata;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	u.data.nsc_call.nsc_callnumber = KILLSERVER;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
17497c478bd9Sstevel@tonic-gate 	adata = sizeof (nsc_call_t);
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	dptr = &u.data;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	_nsc_trydoorcall(&dptr, &ndata, &adata);
17547c478bd9Sstevel@tonic-gate }
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate static int
17587c478bd9Sstevel@tonic-gate client_setadmin(admin_t *ptr)
17597c478bd9Sstevel@tonic-gate {
17607c478bd9Sstevel@tonic-gate 	union {
17617c478bd9Sstevel@tonic-gate 		nsc_data_t data;
17627c478bd9Sstevel@tonic-gate 		char space[8192];
17637c478bd9Sstevel@tonic-gate 	} u;
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	nsc_data_t *dptr;
17667c478bd9Sstevel@tonic-gate 	int ndata;
17677c478bd9Sstevel@tonic-gate 	int adata;
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	u.data.nsc_call.nsc_callnumber = SETADMIN;
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	(void) memcpy(u.data.nsc_call.nsc_u.name, ptr, sizeof (*ptr));
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	ndata = sizeof (u);
17747c478bd9Sstevel@tonic-gate 	adata = sizeof (*ptr);
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	dptr = &u.data;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	if (_nsc_trydoorcall(&dptr, &ndata, &adata) != SUCCESS) {
17797c478bd9Sstevel@tonic-gate 		return (-1);
17807c478bd9Sstevel@tonic-gate 	}
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	return (0);
17837c478bd9Sstevel@tonic-gate }
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate static void
17867c478bd9Sstevel@tonic-gate dump_stat(nsc_stat_t *ptr)
17877c478bd9Sstevel@tonic-gate {
17887c478bd9Sstevel@tonic-gate 	double hitrate;
17897c478bd9Sstevel@tonic-gate 	(void) printf("%10s  cache is enabled\n",
17907c478bd9Sstevel@tonic-gate 	    (ptr->nsc_enabled?"Yes":"No"));
17917c478bd9Sstevel@tonic-gate 	(void) printf("%10d  cache hits on positive entries\n",
17927c478bd9Sstevel@tonic-gate 	    ptr->nsc_pos_cache_hits);
17937c478bd9Sstevel@tonic-gate 	(void) printf("%10d  cache hits on negative entries\n",
17947c478bd9Sstevel@tonic-gate 	    ptr->nsc_neg_cache_hits);
17957c478bd9Sstevel@tonic-gate 	(void) printf("%10d  cache misses on positive entries\n",
17967c478bd9Sstevel@tonic-gate 	    ptr->nsc_pos_cache_misses);
17977c478bd9Sstevel@tonic-gate 	(void) printf("%10d  cache misses on negative entries\n",
17987c478bd9Sstevel@tonic-gate 	    ptr->nsc_neg_cache_misses);
17997c478bd9Sstevel@tonic-gate 	hitrate = ptr->nsc_pos_cache_misses + ptr->nsc_neg_cache_misses +
18007c478bd9Sstevel@tonic-gate 	    ptr->nsc_pos_cache_hits + ptr->nsc_neg_cache_hits;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	if (hitrate > 0.0)
18037c478bd9Sstevel@tonic-gate 		hitrate = (100.0 * ((double)ptr->nsc_pos_cache_hits +
18047c478bd9Sstevel@tonic-gate 		    (double)ptr->nsc_neg_cache_hits))/hitrate;
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	(void) printf("%10.1f%% cache hit rate\n",  hitrate);
18077c478bd9Sstevel@tonic-gate 	(void) printf("%10d  queries deferred\n", ptr->nsc_throttle_count);
18087c478bd9Sstevel@tonic-gate 	(void) printf("%10d  total entries\n", ptr->nsc_entries);
18097c478bd9Sstevel@tonic-gate 	(void) printf("%10d  complete cache invalidations\n",
18107c478bd9Sstevel@tonic-gate 	    ptr->nsc_invalidate_count);
18117c478bd9Sstevel@tonic-gate 	(void) printf("%10d  suggested size\n", ptr->nsc_suggestedsize);
18127c478bd9Sstevel@tonic-gate 	(void) printf("%10d  seconds time to live for positive entries\n",
18137c478bd9Sstevel@tonic-gate 	    ptr->nsc_pos_ttl);
18147c478bd9Sstevel@tonic-gate 	(void) printf("%10d  seconds time to live for negative entries\n",
18157c478bd9Sstevel@tonic-gate 	    ptr->nsc_neg_ttl);
18167c478bd9Sstevel@tonic-gate 	(void) printf("%10d  most active entries to be kept valid\n",
18177c478bd9Sstevel@tonic-gate 	    ptr->nsc_keephot);
18187c478bd9Sstevel@tonic-gate 	(void) printf("%10s  check /etc/{passwd, group, hosts, inet/ipnodes} "
18197c478bd9Sstevel@tonic-gate 	    "file for changes\n",
18207c478bd9Sstevel@tonic-gate 	    (ptr->nsc_check_files?"Yes":"No"));
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	(void) printf("%10s  use possibly stale data rather than waiting for "
18237c478bd9Sstevel@tonic-gate 	    "refresh\n",
18247c478bd9Sstevel@tonic-gate 	    (ptr->nsc_old_data_ok?"Yes":"No"));
18257c478bd9Sstevel@tonic-gate }
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate static void
18287c478bd9Sstevel@tonic-gate client_showstats(admin_t *ptr)
18297c478bd9Sstevel@tonic-gate {
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	(void) printf("nscd configuration:\n\n");
18327c478bd9Sstevel@tonic-gate 	(void) printf("%10d  server debug level\n", ptr->debug_level);
18337c478bd9Sstevel@tonic-gate 	(void) printf("\"%s\"  is server log file\n", ptr->logfile);
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	(void) printf("\npasswd cache:\n\n");
18367c478bd9Sstevel@tonic-gate 	dump_stat(&(ptr->passwd));
18377c478bd9Sstevel@tonic-gate 	(void) printf("\ngroup cache:\n\n");
18387c478bd9Sstevel@tonic-gate 	dump_stat(&(ptr->group));
18397c478bd9Sstevel@tonic-gate 	(void) printf("\nhosts cache:\n\n");
18407c478bd9Sstevel@tonic-gate 	dump_stat(&(ptr->host));
18417c478bd9Sstevel@tonic-gate 	(void) printf("\nipnodes cache:\n\n");
18427c478bd9Sstevel@tonic-gate 	dump_stat(&(ptr->node));
18437c478bd9Sstevel@tonic-gate 	(void) printf("\nexec_attr cache:\n\n");
18447c478bd9Sstevel@tonic-gate 	dump_stat(&(ptr->exec));
18457c478bd9Sstevel@tonic-gate 	(void) printf("\nprof_attr cache:\n\n");
18467c478bd9Sstevel@tonic-gate 	dump_stat(&(ptr->prof));
18477c478bd9Sstevel@tonic-gate 	(void) printf("\nuser_attr cache:\n\n");
18487c478bd9Sstevel@tonic-gate 	dump_stat(&(ptr->user));
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate /*
18547c478bd9Sstevel@tonic-gate  * detach from tty
18557c478bd9Sstevel@tonic-gate  */
18567c478bd9Sstevel@tonic-gate static void
18577c478bd9Sstevel@tonic-gate detachfromtty(void)
18587c478bd9Sstevel@tonic-gate {
18597c478bd9Sstevel@tonic-gate 	if (logfd > 0) {
18607c478bd9Sstevel@tonic-gate 		int i;
18617c478bd9Sstevel@tonic-gate 		for (i = 0; i < logfd; i++)
18627c478bd9Sstevel@tonic-gate 			(void) close(i);
18637c478bd9Sstevel@tonic-gate 		closefrom(logfd+1);
18647c478bd9Sstevel@tonic-gate 	} else
18657c478bd9Sstevel@tonic-gate 		closefrom(0);
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	(void) chdir("/");
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	switch (fork1()) {
18707c478bd9Sstevel@tonic-gate 	case (pid_t)-1:
18717c478bd9Sstevel@tonic-gate 		exit(1);
18727c478bd9Sstevel@tonic-gate 		break;
18737c478bd9Sstevel@tonic-gate 	case 0:
18747c478bd9Sstevel@tonic-gate 		break;
18757c478bd9Sstevel@tonic-gate 	default:
18767c478bd9Sstevel@tonic-gate 		exit(0);
18777c478bd9Sstevel@tonic-gate 	}
18787c478bd9Sstevel@tonic-gate 	(void) setsid();
18797c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR, 0);
18807c478bd9Sstevel@tonic-gate 	(void) dup(0);
18817c478bd9Sstevel@tonic-gate 	(void) dup(0);
18827c478bd9Sstevel@tonic-gate }
1883