xref: /titanic_50/usr/src/cmd/auditd/auditd.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /* Audit daemon server */
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * These routines make up the audit daemon server.  This daemon, called
32*7c478bd9Sstevel@tonic-gate  * auditd, handles the user level parts of auditing.  It receives buffered
33*7c478bd9Sstevel@tonic-gate  * audit records (usually one or more per buffer, potentially less than
34*7c478bd9Sstevel@tonic-gate  * one) and passes them to one or more plugins for processing.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * The major interrupts are AU_SIG_READ_CONTROL (start over),
37*7c478bd9Sstevel@tonic-gate  * AU_SIG_DISABLE (start shutting down), SIGALRM (quit), and
38*7c478bd9Sstevel@tonic-gate  * AU_SIG_NEXT_DIR (start a new audit log file). SIGTERM (the implementation
39*7c478bd9Sstevel@tonic-gate  * value of AU_SIG_DISABLE) is also used for the child to tell the parent
40*7c478bd9Sstevel@tonic-gate  * that audit is ready.
41*7c478bd9Sstevel@tonic-gate  *
42*7c478bd9Sstevel@tonic-gate  * Configuration data comes from /etc/security/audit_control and the auditon
43*7c478bd9Sstevel@tonic-gate  * system call.
44*7c478bd9Sstevel@tonic-gate  *
45*7c478bd9Sstevel@tonic-gate  * The major errors are EBUSY (auditing is already in use) and EINTR
46*7c478bd9Sstevel@tonic-gate  * (one of the above signals was received).  File space errors are
47*7c478bd9Sstevel@tonic-gate  * handled by the audit_binfile plugin
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #define	DEBUG 		0
51*7c478bd9Sstevel@tonic-gate #define	MEM_TEST	0	/* set to one to generate core dump on exit */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #include <assert.h>
54*7c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
55*7c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
56*7c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
57*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
58*7c478bd9Sstevel@tonic-gate #include <libintl.h>
59*7c478bd9Sstevel@tonic-gate #include <locale.h>
60*7c478bd9Sstevel@tonic-gate #include <netdb.h>
61*7c478bd9Sstevel@tonic-gate #include <pwd.h>
62*7c478bd9Sstevel@tonic-gate #include <secdb.h>
63*7c478bd9Sstevel@tonic-gate #include <signal.h>
64*7c478bd9Sstevel@tonic-gate #include <stdio.h>
65*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
66*7c478bd9Sstevel@tonic-gate #include <string.h>
67*7c478bd9Sstevel@tonic-gate #include <errno.h>
68*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
69*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
70*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
71*7c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
72*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
73*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
74*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
75*7c478bd9Sstevel@tonic-gate #include <termios.h>
76*7c478bd9Sstevel@tonic-gate #include <unistd.h>
77*7c478bd9Sstevel@tonic-gate #include "plugin.h"
78*7c478bd9Sstevel@tonic-gate #include "audit_sig_infc.h"
79*7c478bd9Sstevel@tonic-gate #include <audit_plugin.h>
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
82*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
83*7c478bd9Sstevel@tonic-gate #endif
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate  * After we get a AU_SIG_DISABLE, we want to set a timer for 2 seconds
86*7c478bd9Sstevel@tonic-gate  * and let c2audit write as many records as it can until the timer
87*7c478bd9Sstevel@tonic-gate  * goes off(at which point it returns to auditd with SIGALRM).  If any
88*7c478bd9Sstevel@tonic-gate  * other signals are received during that time, we call
89*7c478bd9Sstevel@tonic-gate  * __audit_dowarn() to indicate that the queue may not have been fully
90*7c478bd9Sstevel@tonic-gate  * flushed.
91*7c478bd9Sstevel@tonic-gate  */
92*7c478bd9Sstevel@tonic-gate #define	ALRM_TIME	2
93*7c478bd9Sstevel@tonic-gate #define	SLEEP_TIME	20	/* # of seconds to sleep in all hard loop */
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate #if DEBUG
96*7c478bd9Sstevel@tonic-gate #define	DPRINT(x) {(void) fprintf x; }
97*7c478bd9Sstevel@tonic-gate static FILE	*dbfp;	/* debug file */
98*7c478bd9Sstevel@tonic-gate #else
99*7c478bd9Sstevel@tonic-gate #define	DPRINT(x)
100*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate static plugin_t	*binfile = NULL;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate static int	turn_audit_on  = AUC_AUDITING;
105*7c478bd9Sstevel@tonic-gate static int	turn_audit_off = AUC_NOAUDIT;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate static int	running = 1;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate /*
110*7c478bd9Sstevel@tonic-gate  * GLOBALS:
111*7c478bd9Sstevel@tonic-gate  */
112*7c478bd9Sstevel@tonic-gate plugin_t		*plugin_head = NULL;
113*7c478bd9Sstevel@tonic-gate static thr_data_t	main_thr;	/* auditd thread (0) */
114*7c478bd9Sstevel@tonic-gate pthread_mutex_t		plugin_mutex;	/* for plugin_t list */
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate static int	caught_alrm = 0;	/* number of SIGALRMs pending */
117*7c478bd9Sstevel@tonic-gate static int	caught_readc = 0;	/* number of AU_SIG_READ_CONTROLs */
118*7c478bd9Sstevel@tonic-gate static int	caught_term = 0;	/* number of AU_SIG_DISABLEs pending */
119*7c478bd9Sstevel@tonic-gate static int	caught_nextd = 0;	/* number of AU_SIG_NEXT_DIRs pending */
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate static int	reset_list = 1;	/* 1 to re-read audit_control */
122*7c478bd9Sstevel@tonic-gate static int	reset_file = 1; /* 1 to close/open binary log */
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate static int	auditing_set = 0;	/* 1 if auditon(A_SETCOND, on... */
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate static void	my_sleep();
127*7c478bd9Sstevel@tonic-gate static void	signal_thread();
128*7c478bd9Sstevel@tonic-gate static void	loadauditlist();
129*7c478bd9Sstevel@tonic-gate static void	block_signals();
130*7c478bd9Sstevel@tonic-gate static int	do_sethost();
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate /* common exit function */
133*7c478bd9Sstevel@tonic-gate void
134*7c478bd9Sstevel@tonic-gate auditd_exit(int status)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate #if MEM_TEST
137*7c478bd9Sstevel@tonic-gate 	sigset_t	set;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "mem_test intentional abort (status=%d)\n",
140*7c478bd9Sstevel@tonic-gate 	    status));
141*7c478bd9Sstevel@tonic-gate 	abort();
142*7c478bd9Sstevel@tonic-gate #endif
143*7c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "%ld exit status = %d auditing_set = %d\n",
144*7c478bd9Sstevel@tonic-gate 	    getpid(), status, auditing_set));
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if (auditing_set)
147*7c478bd9Sstevel@tonic-gate 		(void) auditon(A_SETCOND, (caddr_t)&turn_audit_off,
148*7c478bd9Sstevel@tonic-gate 		    (int)sizeof (int));
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	exit(status);
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
154*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	auditinfo_addr_t	as_null;	/* audit state to set */
157*7c478bd9Sstevel@tonic-gate 	au_id_t			auid;
158*7c478bd9Sstevel@tonic-gate 	pthread_t		tid;
159*7c478bd9Sstevel@tonic-gate 	plugin_t		*p;
160*7c478bd9Sstevel@tonic-gate 	pid_t			pid;
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate #if DEBUG
163*7c478bd9Sstevel@tonic-gate 	/* LINTED */
164*7c478bd9Sstevel@tonic-gate 	char			*envp;
165*7c478bd9Sstevel@tonic-gate 	dbfp = __auditd_debug_file_open();
166*7c478bd9Sstevel@tonic-gate #endif
167*7c478bd9Sstevel@tonic-gate 	(void) setsid();
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	/* Internationalization */
170*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
171*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	/*
174*7c478bd9Sstevel@tonic-gate 	 * Set the audit host-id.
175*7c478bd9Sstevel@tonic-gate 	 */
176*7c478bd9Sstevel@tonic-gate 	if (do_sethost() != 0) {
177*7c478bd9Sstevel@tonic-gate 		__audit_dowarn("nostart", "", 0);
178*7c478bd9Sstevel@tonic-gate 		auditd_exit(1);
179*7c478bd9Sstevel@tonic-gate 	}
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	/*
182*7c478bd9Sstevel@tonic-gate 	 * Turn off all auditing for this process.
183*7c478bd9Sstevel@tonic-gate 	 */
184*7c478bd9Sstevel@tonic-gate 	if (getaudit_addr(&as_null, sizeof (as_null)) == -1) {
185*7c478bd9Sstevel@tonic-gate 		__audit_dowarn("nostart", "", 0);
186*7c478bd9Sstevel@tonic-gate 		auditd_exit(2);
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate 	as_null.ai_mask.as_success = 0;
189*7c478bd9Sstevel@tonic-gate 	as_null.ai_mask.as_failure = 0;
190*7c478bd9Sstevel@tonic-gate 	(void) setaudit_addr(&as_null, sizeof (as_null));
191*7c478bd9Sstevel@tonic-gate 	auid = AU_NOAUDITID;
192*7c478bd9Sstevel@tonic-gate 	(void) setauid(&auid);
193*7c478bd9Sstevel@tonic-gate 	/*
194*7c478bd9Sstevel@tonic-gate 	 * Set the audit state flag to AUDITING.
195*7c478bd9Sstevel@tonic-gate 	 */
196*7c478bd9Sstevel@tonic-gate 	if (auditon(A_SETCOND, (caddr_t)&turn_audit_on, (int)sizeof (int)) !=
197*7c478bd9Sstevel@tonic-gate 	    0) {
198*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "auditon(A_SETCOND...) failed (exit)\n"));
199*7c478bd9Sstevel@tonic-gate 		__audit_dowarn("nostart", "", 0);
200*7c478bd9Sstevel@tonic-gate 		auditd_exit(7);
201*7c478bd9Sstevel@tonic-gate 	}
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	block_signals();
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate #if DEBUG
206*7c478bd9Sstevel@tonic-gate 	/* output to dbfp shouldn't be duplicated by parent and child */
207*7c478bd9Sstevel@tonic-gate 	(void) fflush(dbfp);
208*7c478bd9Sstevel@tonic-gate #endif
209*7c478bd9Sstevel@tonic-gate 	/*
210*7c478bd9Sstevel@tonic-gate 	 * wait for "ready" signal before exit -- for greenline
211*7c478bd9Sstevel@tonic-gate 	 */
212*7c478bd9Sstevel@tonic-gate 	if (fork()) {
213*7c478bd9Sstevel@tonic-gate 		sigset_t	set;
214*7c478bd9Sstevel@tonic-gate 		int		signal_caught = 0;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&set);
217*7c478bd9Sstevel@tonic-gate 		(void) sigaddset(&set, AU_SIG_DISABLE);
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 		while (signal_caught != AU_SIG_DISABLE)
220*7c478bd9Sstevel@tonic-gate 			signal_caught = sigwait(&set);
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "init complete:  parent can now exit\n"));
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 		auditd_exit(0);
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 	pid = getppid();
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	auditing_set = 1;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate #if DEBUG && MEM_TEST
231*7c478bd9Sstevel@tonic-gate 	envp = getenv("UMEM_DEBUG");
232*7c478bd9Sstevel@tonic-gate 	if (envp != NULL)
233*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "UMEM_DEBUG=%s\n", envp));
234*7c478bd9Sstevel@tonic-gate 	envp = getenv("UMEM_LOGGING");
235*7c478bd9Sstevel@tonic-gate 	if (envp != NULL)
236*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "UMEM_LOGGING=%s\n", envp));
237*7c478bd9Sstevel@tonic-gate #endif
238*7c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "auditd pid=%ld\n", getpid()));
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	/* thread 0 sync */
241*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&(main_thr.thd_mutex), NULL);
242*7c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&(main_thr.thd_cv), NULL);
243*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&plugin_mutex, NULL);
244*7c478bd9Sstevel@tonic-gate 	/*
245*7c478bd9Sstevel@tonic-gate 	 * Set up a separate thread for signal handling.
246*7c478bd9Sstevel@tonic-gate 	 */
247*7c478bd9Sstevel@tonic-gate 	if (pthread_create(&tid, NULL, (void *(*)(void *))signal_thread,
248*7c478bd9Sstevel@tonic-gate 	    NULL)) {
249*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
250*7c478bd9Sstevel@tonic-gate 		    "auditd can't create a thread\n"));
251*7c478bd9Sstevel@tonic-gate 		auditd_exit(3);
252*7c478bd9Sstevel@tonic-gate 	}
253*7c478bd9Sstevel@tonic-gate 	/*
254*7c478bd9Sstevel@tonic-gate 	 * Set the umask so that only audit or other users in the audit group
255*7c478bd9Sstevel@tonic-gate 	 * can get to the files created by auditd.
256*7c478bd9Sstevel@tonic-gate 	 */
257*7c478bd9Sstevel@tonic-gate 	(void) umask(007);
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	if (__logpost("")) {	/* Open the audit_data file. */
260*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "logpost failed\n"));
261*7c478bd9Sstevel@tonic-gate 		auditd_exit(4);
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 	/*
264*7c478bd9Sstevel@tonic-gate 	 * Here is the main body of the audit daemon.  running == 0 means that
265*7c478bd9Sstevel@tonic-gate 	 * after flushing out the audit queue, it is time to exit in response to
266*7c478bd9Sstevel@tonic-gate 	 * AU_SIG_DISABLE
267*7c478bd9Sstevel@tonic-gate 	 */
268*7c478bd9Sstevel@tonic-gate 	while (running) {
269*7c478bd9Sstevel@tonic-gate 		/*
270*7c478bd9Sstevel@tonic-gate 		 * Read audit_control and create plugin lists.
271*7c478bd9Sstevel@tonic-gate 		 *
272*7c478bd9Sstevel@tonic-gate 		 * loadauditlist() and auditd_thread_init() are called
273*7c478bd9Sstevel@tonic-gate 		 * while under the plugin_mutex lock to avoid a race
274*7c478bd9Sstevel@tonic-gate 		 * with unload_plugin().
275*7c478bd9Sstevel@tonic-gate 		 */
276*7c478bd9Sstevel@tonic-gate 		if (reset_list || reset_file) {
277*7c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_lock(&plugin_mutex);
278*7c478bd9Sstevel@tonic-gate 			if (reset_list)
279*7c478bd9Sstevel@tonic-gate 				loadauditlist();
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 			if (auditd_thread_init()) {
282*7c478bd9Sstevel@tonic-gate 				auditd_thread_close();
283*7c478bd9Sstevel@tonic-gate 				/* continue; wait for audit -s */
284*7c478bd9Sstevel@tonic-gate 			}
285*7c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&plugin_mutex);
286*7c478bd9Sstevel@tonic-gate 			reset_list = 0;
287*7c478bd9Sstevel@tonic-gate 		}
288*7c478bd9Sstevel@tonic-gate 		/*
289*7c478bd9Sstevel@tonic-gate 		 * tell parent I'm running whether or not the initialization
290*7c478bd9Sstevel@tonic-gate 		 * actually worked.  The failure case is to wait for an
291*7c478bd9Sstevel@tonic-gate 		 * audit -n or audit -s to fix the problem.
292*7c478bd9Sstevel@tonic-gate 		 */
293*7c478bd9Sstevel@tonic-gate 		if (pid != 0) {
294*7c478bd9Sstevel@tonic-gate 			(void) kill(pid, AU_SIG_DISABLE);
295*7c478bd9Sstevel@tonic-gate 			pid = 0;
296*7c478bd9Sstevel@tonic-gate 		}
297*7c478bd9Sstevel@tonic-gate 		/*
298*7c478bd9Sstevel@tonic-gate 		 * thread_signal() signals main (this thread) when
299*7c478bd9Sstevel@tonic-gate 		 * it has received a signal.
300*7c478bd9Sstevel@tonic-gate 		 */
301*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "main thread is waiting\n"));
302*7c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&(main_thr.thd_mutex));
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 		if (!(caught_readc || caught_term || caught_alrm ||
305*7c478bd9Sstevel@tonic-gate 		    caught_nextd))
306*7c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&(main_thr.thd_cv),
307*7c478bd9Sstevel@tonic-gate 			    &(main_thr.thd_mutex));
308*7c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&(main_thr.thd_mutex));
309*7c478bd9Sstevel@tonic-gate 		/*
310*7c478bd9Sstevel@tonic-gate 		 * Got here because a signal came in.
311*7c478bd9Sstevel@tonic-gate 		 * Since we may have gotten more than one, we assume a
312*7c478bd9Sstevel@tonic-gate 		 * priority scheme with SIGALRM being the most
313*7c478bd9Sstevel@tonic-gate 		 * significant.
314*7c478bd9Sstevel@tonic-gate 		 */
315*7c478bd9Sstevel@tonic-gate 		if (caught_alrm) {
316*7c478bd9Sstevel@tonic-gate 			/*
317*7c478bd9Sstevel@tonic-gate 			 * We have returned from our timed wait for
318*7c478bd9Sstevel@tonic-gate 			 * c2audit to calm down.  We need to really shut
319*7c478bd9Sstevel@tonic-gate 			 * down here.
320*7c478bd9Sstevel@tonic-gate 			 */
321*7c478bd9Sstevel@tonic-gate 			caught_alrm = 0;
322*7c478bd9Sstevel@tonic-gate 			running = 0;	/* shut down now */
323*7c478bd9Sstevel@tonic-gate 		} else if (caught_term) {
324*7c478bd9Sstevel@tonic-gate 			/*
325*7c478bd9Sstevel@tonic-gate 			 * we are going to shut down, but need to
326*7c478bd9Sstevel@tonic-gate 			 * allow time for the audit queues in
327*7c478bd9Sstevel@tonic-gate 			 * c2audit and for the threads to empty.
328*7c478bd9Sstevel@tonic-gate 			 */
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 			p = plugin_head;
331*7c478bd9Sstevel@tonic-gate 			while (p != NULL) {
332*7c478bd9Sstevel@tonic-gate 				DPRINT((dbfp, "signalling thread %d\n",
333*7c478bd9Sstevel@tonic-gate 				    p->plg_tid));
334*7c478bd9Sstevel@tonic-gate 				(void) pthread_mutex_lock(&(p->plg_mutex));
335*7c478bd9Sstevel@tonic-gate 				p->plg_removed = 1;
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 				if (p->plg_initialized)
338*7c478bd9Sstevel@tonic-gate 					(void) pthread_cond_signal(
339*7c478bd9Sstevel@tonic-gate 					    &(p->plg_cv));
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 				(void) pthread_mutex_unlock(&(p->plg_mutex));
342*7c478bd9Sstevel@tonic-gate 				p = p->plg_next;
343*7c478bd9Sstevel@tonic-gate 			}
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 			caught_alrm = 0;
346*7c478bd9Sstevel@tonic-gate 			caught_readc  = 0;
347*7c478bd9Sstevel@tonic-gate 			caught_term = 0;
348*7c478bd9Sstevel@tonic-gate 			caught_nextd = 0;
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 			DPRINT((dbfp,
351*7c478bd9Sstevel@tonic-gate 			    "main thread is pausing before exit.\n"));
352*7c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_lock(&(main_thr.thd_mutex));
353*7c478bd9Sstevel@tonic-gate 			caught_alrm = 0;
354*7c478bd9Sstevel@tonic-gate 			(void) alarm(ALRM_TIME);
355*7c478bd9Sstevel@tonic-gate 			while (!caught_alrm)
356*7c478bd9Sstevel@tonic-gate 				(void) pthread_cond_wait(&(main_thr.thd_cv),
357*7c478bd9Sstevel@tonic-gate 				    &(main_thr.thd_mutex));
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&(main_thr.thd_mutex));
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 			running = 0;	/* Close down auditing and exit */
362*7c478bd9Sstevel@tonic-gate 		} else if (caught_readc) {
363*7c478bd9Sstevel@tonic-gate 			/*
364*7c478bd9Sstevel@tonic-gate 			 * if both hup and usr1 are caught, the logic in
365*7c478bd9Sstevel@tonic-gate 			 * loadauditlist() results in hup winning.  The
366*7c478bd9Sstevel@tonic-gate 			 * result will be that the audit file is not rolled
367*7c478bd9Sstevel@tonic-gate 			 * over unless audit_control actually changed.
368*7c478bd9Sstevel@tonic-gate 			 *
369*7c478bd9Sstevel@tonic-gate 			 * They want to reread the audit_control file.
370*7c478bd9Sstevel@tonic-gate 			 * Set reset_list which will return us to the
371*7c478bd9Sstevel@tonic-gate 			 * main while loop in the main routine.
372*7c478bd9Sstevel@tonic-gate 			 */
373*7c478bd9Sstevel@tonic-gate 			caught_readc = 0;
374*7c478bd9Sstevel@tonic-gate 			reset_list = 1;
375*7c478bd9Sstevel@tonic-gate 		} else if (caught_nextd) {
376*7c478bd9Sstevel@tonic-gate 			/*
377*7c478bd9Sstevel@tonic-gate 			 * This is a special case for the binfile
378*7c478bd9Sstevel@tonic-gate 			 * plugin. (audit -n)  NULL out kvlist
379*7c478bd9Sstevel@tonic-gate 			 * so binfile won't re-read audit_control
380*7c478bd9Sstevel@tonic-gate 			 */
381*7c478bd9Sstevel@tonic-gate 			caught_nextd = 0;
382*7c478bd9Sstevel@tonic-gate 			reset_file = 1;
383*7c478bd9Sstevel@tonic-gate 			if (binfile != NULL) {
384*7c478bd9Sstevel@tonic-gate 				_kva_free(binfile->plg_kvlist);
385*7c478bd9Sstevel@tonic-gate 				binfile->plg_kvlist = NULL;
386*7c478bd9Sstevel@tonic-gate 				binfile->plg_reopen = 1;
387*7c478bd9Sstevel@tonic-gate 			}
388*7c478bd9Sstevel@tonic-gate 		}
389*7c478bd9Sstevel@tonic-gate 	}	/* end while (running) */
390*7c478bd9Sstevel@tonic-gate 	auditd_thread_close();
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	auditd_exit(0);
393*7c478bd9Sstevel@tonic-gate 	return (0);
394*7c478bd9Sstevel@tonic-gate }
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate /*
397*7c478bd9Sstevel@tonic-gate  * my_sleep - sleep for SLEEP_TIME seconds but only accept the signals
398*7c478bd9Sstevel@tonic-gate  *	that we want to accept.  (Premature termination just means the
399*7c478bd9Sstevel@tonic-gate  *	caller retries more often, not a big deal.)
400*7c478bd9Sstevel@tonic-gate  */
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate static void
403*7c478bd9Sstevel@tonic-gate my_sleep()
404*7c478bd9Sstevel@tonic-gate {
405*7c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "auditd: sleeping for 20 seconds\n"));
406*7c478bd9Sstevel@tonic-gate 	/*
407*7c478bd9Sstevel@tonic-gate 	 * Set timer to "sleep"
408*7c478bd9Sstevel@tonic-gate 	 */
409*7c478bd9Sstevel@tonic-gate 	(void) alarm(SLEEP_TIME);
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "main thread is waiting for SIGALRM before exit.\n"));
412*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&(main_thr.thd_mutex));
413*7c478bd9Sstevel@tonic-gate 	(void) pthread_cond_wait(&(main_thr.thd_cv), &(main_thr.thd_mutex));
414*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&(main_thr.thd_mutex));
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	if (caught_term) {
417*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "normal AU_SIG_DISABLE exit\n"));
418*7c478bd9Sstevel@tonic-gate 		/*
419*7c478bd9Sstevel@tonic-gate 		 * Exit, as requested.
420*7c478bd9Sstevel@tonic-gate 		 */
421*7c478bd9Sstevel@tonic-gate 		auditd_thread_close();
422*7c478bd9Sstevel@tonic-gate 	}
423*7c478bd9Sstevel@tonic-gate 	if (caught_readc)
424*7c478bd9Sstevel@tonic-gate 		reset_list = 1;		/* Reread the audit_control file */
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 	caught_readc = 0;
427*7c478bd9Sstevel@tonic-gate 	caught_nextd = 0;
428*7c478bd9Sstevel@tonic-gate }
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate /*
431*7c478bd9Sstevel@tonic-gate  * search for $ISA/ in path and replace it with "" if auditd
432*7c478bd9Sstevel@tonic-gate  * is 32 bit, else "sparcv9/"  The plugin $ISA must match however
433*7c478bd9Sstevel@tonic-gate  * auditd was compiled.
434*7c478bd9Sstevel@tonic-gate  */
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate static void
437*7c478bd9Sstevel@tonic-gate isa_ified(char *path, char **newpath)
438*7c478bd9Sstevel@tonic-gate {
439*7c478bd9Sstevel@tonic-gate 	char	*p, *q;
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	if (((p = strchr(path, '$')) != NULL) &&
442*7c478bd9Sstevel@tonic-gate 	    (strncmp("$ISA/", p, 5) == 0)) {
443*7c478bd9Sstevel@tonic-gate 		(void) memcpy(*newpath, path, p - path);
444*7c478bd9Sstevel@tonic-gate 		q = *newpath + (p - path);
445*7c478bd9Sstevel@tonic-gate #ifdef __sparcv9
446*7c478bd9Sstevel@tonic-gate 		q += strlcpy(q, "sparcv9/", avail_length);
447*7c478bd9Sstevel@tonic-gate #endif
448*7c478bd9Sstevel@tonic-gate 		(void) strcpy(q, p + 5);
449*7c478bd9Sstevel@tonic-gate 	} else
450*7c478bd9Sstevel@tonic-gate 		*newpath = path;
451*7c478bd9Sstevel@tonic-gate }
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate /*
454*7c478bd9Sstevel@tonic-gate  * init_plugin first searches the existing plugin list to see
455*7c478bd9Sstevel@tonic-gate  * if the plugin already has been defined; if not, it creates it
456*7c478bd9Sstevel@tonic-gate  * and links it into the list.  It returns a pointer to the found
457*7c478bd9Sstevel@tonic-gate  * or created struct.  A change of path in audit_control for a
458*7c478bd9Sstevel@tonic-gate  * given plugin will cause a miss.
459*7c478bd9Sstevel@tonic-gate  */
460*7c478bd9Sstevel@tonic-gate /*
461*7c478bd9Sstevel@tonic-gate  * for 64 bits, the path name can grow 3 bytes (minus 5 for the
462*7c478bd9Sstevel@tonic-gate  * removed "$ISA" and plus 8 for the added "sparcv9/"
463*7c478bd9Sstevel@tonic-gate  */
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate #define	ISA_GROW	8 - 5
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate static plugin_t *
468*7c478bd9Sstevel@tonic-gate init_plugin(char *name, kva_t *list, int cnt_flag)
469*7c478bd9Sstevel@tonic-gate {
470*7c478bd9Sstevel@tonic-gate 	plugin_t	*p, *q;
471*7c478bd9Sstevel@tonic-gate 	char		filepath[MAXPATHLEN + 1 + ISA_GROW];
472*7c478bd9Sstevel@tonic-gate 	char		*path = filepath;
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	if (*name != '/') {
475*7c478bd9Sstevel@tonic-gate #ifdef  __sparcv9
476*7c478bd9Sstevel@tonic-gate 		(void) strcpy(filepath, "/usr/lib/security/sparcv9/");
477*7c478bd9Sstevel@tonic-gate #else
478*7c478bd9Sstevel@tonic-gate 		(void) strcpy(filepath, "/usr/lib/security/");
479*7c478bd9Sstevel@tonic-gate #endif
480*7c478bd9Sstevel@tonic-gate 		if (strlcat(filepath, name, MAXPATHLEN) >= MAXPATHLEN)
481*7c478bd9Sstevel@tonic-gate 			return (NULL);
482*7c478bd9Sstevel@tonic-gate 	} else {
483*7c478bd9Sstevel@tonic-gate 		if (strlen(name) > MAXPATHLEN + ISA_GROW)
484*7c478bd9Sstevel@tonic-gate 			return (NULL);
485*7c478bd9Sstevel@tonic-gate 		isa_ified(name, &path);
486*7c478bd9Sstevel@tonic-gate 	}
487*7c478bd9Sstevel@tonic-gate 	p = plugin_head;
488*7c478bd9Sstevel@tonic-gate 	q = plugin_head;
489*7c478bd9Sstevel@tonic-gate 	while (p != NULL) {
490*7c478bd9Sstevel@tonic-gate 		if (p->plg_path != NULL) {
491*7c478bd9Sstevel@tonic-gate 			if (strcmp(p->plg_path, path) == 0) {
492*7c478bd9Sstevel@tonic-gate 				p->plg_removed = 0;
493*7c478bd9Sstevel@tonic-gate 				p->plg_to_be_removed = 0;
494*7c478bd9Sstevel@tonic-gate 				p->plg_cnt = cnt_flag;
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 				_kva_free(p->plg_kvlist);
497*7c478bd9Sstevel@tonic-gate 				p->plg_kvlist = list;
498*7c478bd9Sstevel@tonic-gate 				p->plg_reopen = 1;
499*7c478bd9Sstevel@tonic-gate 				DPRINT((dbfp, "reusing %s\n", p->plg_path));
500*7c478bd9Sstevel@tonic-gate 				return (p);
501*7c478bd9Sstevel@tonic-gate 			}
502*7c478bd9Sstevel@tonic-gate 		}
503*7c478bd9Sstevel@tonic-gate 		q = p;
504*7c478bd9Sstevel@tonic-gate 		p = p->plg_next;
505*7c478bd9Sstevel@tonic-gate 	}
506*7c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "creating new plugin structure for %s\n", path));
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate 	p = malloc(sizeof (plugin_t));
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate 	if (p == NULL) {
511*7c478bd9Sstevel@tonic-gate 		perror("auditd");
512*7c478bd9Sstevel@tonic-gate 		return (NULL);
513*7c478bd9Sstevel@tonic-gate 	}
514*7c478bd9Sstevel@tonic-gate 	if (q == NULL)
515*7c478bd9Sstevel@tonic-gate 		plugin_head = p;
516*7c478bd9Sstevel@tonic-gate 	else
517*7c478bd9Sstevel@tonic-gate 		q->plg_next = p;
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	p->plg_next = NULL;
520*7c478bd9Sstevel@tonic-gate 	p->plg_initialized = 0;
521*7c478bd9Sstevel@tonic-gate 	p->plg_reopen = 1;
522*7c478bd9Sstevel@tonic-gate 	p->plg_tid = 0;
523*7c478bd9Sstevel@tonic-gate 	p->plg_removed = 0;
524*7c478bd9Sstevel@tonic-gate 	p->plg_to_be_removed = 0;
525*7c478bd9Sstevel@tonic-gate 	p->plg_tossed = 0;
526*7c478bd9Sstevel@tonic-gate 	p->plg_queued = 0;
527*7c478bd9Sstevel@tonic-gate 	p->plg_output = 0;
528*7c478bd9Sstevel@tonic-gate 	p->plg_sequence = 1;
529*7c478bd9Sstevel@tonic-gate 	p->plg_last_seq_out = 0;
530*7c478bd9Sstevel@tonic-gate 	p->plg_path = strdup(path);
531*7c478bd9Sstevel@tonic-gate 	p->plg_kvlist = list;
532*7c478bd9Sstevel@tonic-gate 	p->plg_cnt = cnt_flag;
533*7c478bd9Sstevel@tonic-gate 	p->plg_retry_time = SLEEP_TIME;
534*7c478bd9Sstevel@tonic-gate 	p->plg_qmax = 0;
535*7c478bd9Sstevel@tonic-gate 	p->plg_save_q_copy = NULL;
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "created plugin:  %s\n", path));
538*7c478bd9Sstevel@tonic-gate 	return (p);
539*7c478bd9Sstevel@tonic-gate }
540*7c478bd9Sstevel@tonic-gate 
541*7c478bd9Sstevel@tonic-gate /*
542*7c478bd9Sstevel@tonic-gate  * loadauditlist - read the directory list from the audit_control file.
543*7c478bd9Sstevel@tonic-gate  *		   to determine if a binary file is to be written.
544*7c478bd9Sstevel@tonic-gate  *		 - read the plugin entries from the audit_control file
545*7c478bd9Sstevel@tonic-gate  *
546*7c478bd9Sstevel@tonic-gate  * globals -
547*7c478bd9Sstevel@tonic-gate  *
548*7c478bd9Sstevel@tonic-gate  *	plugin queues
549*7c478bd9Sstevel@tonic-gate  *
550*7c478bd9Sstevel@tonic-gate  * success is when at least one plug in is defined.
551*7c478bd9Sstevel@tonic-gate  *
552*7c478bd9Sstevel@tonic-gate  * set cnt policy here based on auditconfig setting.  future could
553*7c478bd9Sstevel@tonic-gate  * have a policy = {+|-}cnt entry per plugin with auditconfig providing the
554*7c478bd9Sstevel@tonic-gate  * default.
555*7c478bd9Sstevel@tonic-gate  */
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate static void
558*7c478bd9Sstevel@tonic-gate loadauditlist()
559*7c478bd9Sstevel@tonic-gate {
560*7c478bd9Sstevel@tonic-gate 	char		buf[MAXPATHLEN];
561*7c478bd9Sstevel@tonic-gate 	char		*value;
562*7c478bd9Sstevel@tonic-gate 	plugin_t	*p;
563*7c478bd9Sstevel@tonic-gate 	int		acresult;
564*7c478bd9Sstevel@tonic-gate 	int		wait_count = 0;
565*7c478bd9Sstevel@tonic-gate 	kva_t		*kvlist;
566*7c478bd9Sstevel@tonic-gate 	long		policy;
567*7c478bd9Sstevel@tonic-gate 	int		cnt_flag;
568*7c478bd9Sstevel@tonic-gate 	struct au_qctrl	kqmax;
569*7c478bd9Sstevel@tonic-gate 	au_acinfo_t	*ach = NULL;
570*7c478bd9Sstevel@tonic-gate 	int		got_dir = 0;
571*7c478bd9Sstevel@tonic-gate 	int		have_plugin = 0;
572*7c478bd9Sstevel@tonic-gate 	char		*endptr;
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate 	if (auditon(A_GETPOLICY, (char *)&policy, 0) == -1) {
575*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "auditon(A_GETPOLICY...) failed (exit)\n"));
576*7c478bd9Sstevel@tonic-gate 		__audit_dowarn("auditoff", "", 0);
577*7c478bd9Sstevel@tonic-gate 		auditd_thread_close();
578*7c478bd9Sstevel@tonic-gate 		auditd_exit(5);
579*7c478bd9Sstevel@tonic-gate 	}
580*7c478bd9Sstevel@tonic-gate 	cnt_flag = ((policy & AUDIT_CNT) != 0) ? 1 : 0;
581*7c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "loadauditlist:  policy is to %s\n", (cnt_flag == 1) ?
582*7c478bd9Sstevel@tonic-gate 	    "continue" : "block"));
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate #if DEBUG
585*7c478bd9Sstevel@tonic-gate 	if (auditon(A_GETCOND, (caddr_t)&acresult, (int)sizeof (int)) !=
586*7c478bd9Sstevel@tonic-gate 	    0)
587*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "auditon(A_GETCOND...) failed (exit)\n"));
588*7c478bd9Sstevel@tonic-gate #endif
589*7c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "audit cond = %d (1 is on)\n", acresult));
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 	if (auditon(A_GETQCTRL, (char *)&kqmax, sizeof (struct au_qctrl)) !=
593*7c478bd9Sstevel@tonic-gate 	    0) {
594*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "auditon(A_GETQCTRL...) failed (exit)\n"));
595*7c478bd9Sstevel@tonic-gate 		__audit_dowarn("auditoff", "", 0);
596*7c478bd9Sstevel@tonic-gate 		auditd_thread_close();
597*7c478bd9Sstevel@tonic-gate 		auditd_exit(6);
598*7c478bd9Sstevel@tonic-gate 	}
599*7c478bd9Sstevel@tonic-gate 	kqmax.aq_hiwater *= 5;		/* RAM is cheaper in userspace */
600*7c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "auditd: reading audit_control\n"));
601*7c478bd9Sstevel@tonic-gate 
602*7c478bd9Sstevel@tonic-gate 	p = plugin_head;
603*7c478bd9Sstevel@tonic-gate 	/*
604*7c478bd9Sstevel@tonic-gate 	 * two-step on setting p->plg_removed because the input thread
605*7c478bd9Sstevel@tonic-gate 	 * in doorway.c uses p->plg_removed to decide if the plugin is
606*7c478bd9Sstevel@tonic-gate 	 * active.
607*7c478bd9Sstevel@tonic-gate 	 */
608*7c478bd9Sstevel@tonic-gate 	while (p != NULL) {
609*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "loadauditlist:  %X, %s previously created\n",
610*7c478bd9Sstevel@tonic-gate 		    p, p->plg_path));
611*7c478bd9Sstevel@tonic-gate 		p->plg_to_be_removed = 1;	/* tentative removal */
612*7c478bd9Sstevel@tonic-gate 		p = p->plg_next;
613*7c478bd9Sstevel@tonic-gate 	}
614*7c478bd9Sstevel@tonic-gate 	/*
615*7c478bd9Sstevel@tonic-gate 	 * have_plugin may over count by one if both a "dir" entry
616*7c478bd9Sstevel@tonic-gate 	 * and a "plugin" entry for binfile are found.  All that
617*7c478bd9Sstevel@tonic-gate 	 * matters is that it be zero if no plugin or dir entries
618*7c478bd9Sstevel@tonic-gate 	 * are found.
619*7c478bd9Sstevel@tonic-gate 	 */
620*7c478bd9Sstevel@tonic-gate 	have_plugin = 0;
621*7c478bd9Sstevel@tonic-gate 	for (;;) {
622*7c478bd9Sstevel@tonic-gate 		/* NULL == use standard path for audit_control */
623*7c478bd9Sstevel@tonic-gate 		ach = _openac(NULL);
624*7c478bd9Sstevel@tonic-gate 		/*
625*7c478bd9Sstevel@tonic-gate 		 * loop until a directory entry is found (0) or eof (-1)
626*7c478bd9Sstevel@tonic-gate 		 */
627*7c478bd9Sstevel@tonic-gate 		while (((acresult = _getacdir(ach, buf, sizeof (buf))) != 0) &&
628*7c478bd9Sstevel@tonic-gate 		    acresult != -1) {
629*7c478bd9Sstevel@tonic-gate 		}
630*7c478bd9Sstevel@tonic-gate 		if (acresult == 0) {
631*7c478bd9Sstevel@tonic-gate 			DPRINT((dbfp,
632*7c478bd9Sstevel@tonic-gate 			    "loadauditlist: "
633*7c478bd9Sstevel@tonic-gate 			    "got binfile via old config syntax\n"));
634*7c478bd9Sstevel@tonic-gate 			/*
635*7c478bd9Sstevel@tonic-gate 			 * A directory entry was found.
636*7c478bd9Sstevel@tonic-gate 			 */
637*7c478bd9Sstevel@tonic-gate 			got_dir = 1;
638*7c478bd9Sstevel@tonic-gate 			kvlist = _str2kva("name=audit_binfile.so.1",
639*7c478bd9Sstevel@tonic-gate 			    "=", ";");
640*7c478bd9Sstevel@tonic-gate 
641*7c478bd9Sstevel@tonic-gate 			p = init_plugin("audit_binfile.so.1", kvlist, cnt_flag);
642*7c478bd9Sstevel@tonic-gate 
643*7c478bd9Sstevel@tonic-gate 			if (p != NULL) {
644*7c478bd9Sstevel@tonic-gate 				binfile = p;
645*7c478bd9Sstevel@tonic-gate 				p->plg_qmax = kqmax.aq_hiwater;
646*7c478bd9Sstevel@tonic-gate 				have_plugin++;
647*7c478bd9Sstevel@tonic-gate 			}
648*7c478bd9Sstevel@tonic-gate 		}
649*7c478bd9Sstevel@tonic-gate 		/*
650*7c478bd9Sstevel@tonic-gate 		 * collect plugin entries.  If there is an entry for
651*7c478bd9Sstevel@tonic-gate 		 * binfile.so.1, the parameters from the plugin line
652*7c478bd9Sstevel@tonic-gate 		 * override those set above.  For binfile, p_dir is
653*7c478bd9Sstevel@tonic-gate 		 * required only if dir wasn't specified elsewhere in
654*7c478bd9Sstevel@tonic-gate 		 * audit_control
655*7c478bd9Sstevel@tonic-gate 		 */
656*7c478bd9Sstevel@tonic-gate 		_rewindac(ach);
657*7c478bd9Sstevel@tonic-gate 		while ((acresult = _getacplug(ach, &kvlist)) == 0) {
658*7c478bd9Sstevel@tonic-gate 			value = kva_match(kvlist, "name");
659*7c478bd9Sstevel@tonic-gate 			if (value == NULL)
660*7c478bd9Sstevel@tonic-gate 				break;
661*7c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "loadauditlist: have an entry for %s\n",
662*7c478bd9Sstevel@tonic-gate 			    value));
663*7c478bd9Sstevel@tonic-gate 			p = init_plugin(value, kvlist, cnt_flag);
664*7c478bd9Sstevel@tonic-gate 			if (p == NULL)
665*7c478bd9Sstevel@tonic-gate 				continue;
666*7c478bd9Sstevel@tonic-gate 
667*7c478bd9Sstevel@tonic-gate 			if (strstr(value, "/audit_binfile.so") != NULL) {
668*7c478bd9Sstevel@tonic-gate 				binfile = p;
669*7c478bd9Sstevel@tonic-gate 				if (!got_dir &&
670*7c478bd9Sstevel@tonic-gate 				    (kva_match(kvlist, "p_dir") ==
671*7c478bd9Sstevel@tonic-gate 				    NULL)) {
672*7c478bd9Sstevel@tonic-gate 					__audit_dowarn("getacdir", "",
673*7c478bd9Sstevel@tonic-gate 					    wait_count);
674*7c478bd9Sstevel@tonic-gate 				}
675*7c478bd9Sstevel@tonic-gate 			}
676*7c478bd9Sstevel@tonic-gate 			p->plg_qmax = kqmax.aq_hiwater; /* default */
677*7c478bd9Sstevel@tonic-gate 			value = kva_match(kvlist, "qsize");
678*7c478bd9Sstevel@tonic-gate 			if (value != NULL) {
679*7c478bd9Sstevel@tonic-gate 				long	tmp;
680*7c478bd9Sstevel@tonic-gate 
681*7c478bd9Sstevel@tonic-gate 				tmp = strtol(value, &endptr, 10);
682*7c478bd9Sstevel@tonic-gate 				if (*endptr == '\0')
683*7c478bd9Sstevel@tonic-gate 					p->plg_qmax = tmp;
684*7c478bd9Sstevel@tonic-gate 			}
685*7c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "%s queue max = %d\n",
686*7c478bd9Sstevel@tonic-gate 				p->plg_path, p->plg_qmax));
687*7c478bd9Sstevel@tonic-gate 
688*7c478bd9Sstevel@tonic-gate 			have_plugin++;
689*7c478bd9Sstevel@tonic-gate 		}
690*7c478bd9Sstevel@tonic-gate 		_endac(ach);
691*7c478bd9Sstevel@tonic-gate 		if (have_plugin != 0)
692*7c478bd9Sstevel@tonic-gate 			break;
693*7c478bd9Sstevel@tonic-gate 		/*
694*7c478bd9Sstevel@tonic-gate 		 * there was a problem getting the directory
695*7c478bd9Sstevel@tonic-gate 		 * list or remote host info from the audit_control file
696*7c478bd9Sstevel@tonic-gate 		 */
697*7c478bd9Sstevel@tonic-gate 		wait_count++;
698*7c478bd9Sstevel@tonic-gate #if	DEBUG
699*7c478bd9Sstevel@tonic-gate 		if (wait_count < 2)
700*7c478bd9Sstevel@tonic-gate 			DPRINT((dbfp,
701*7c478bd9Sstevel@tonic-gate 			    "auditd: problem getting directory "
702*7c478bd9Sstevel@tonic-gate 			    "/ or plugin list from audit_control.\n"));
703*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
704*7c478bd9Sstevel@tonic-gate 		__audit_dowarn("getacdir", "", wait_count);
705*7c478bd9Sstevel@tonic-gate 		/*
706*7c478bd9Sstevel@tonic-gate 		 * sleep for SLEEP_TIME seconds.
707*7c478bd9Sstevel@tonic-gate 		 */
708*7c478bd9Sstevel@tonic-gate 		my_sleep();
709*7c478bd9Sstevel@tonic-gate 	}    /* end for(;;) */
710*7c478bd9Sstevel@tonic-gate 
711*7c478bd9Sstevel@tonic-gate 	p = plugin_head;
712*7c478bd9Sstevel@tonic-gate 	while (p != NULL) {
713*7c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "loadauditlist: %s remove flag=%d; cnt=%d\n",
714*7c478bd9Sstevel@tonic-gate 		    p->plg_path, p->plg_to_be_removed, p->plg_cnt));
715*7c478bd9Sstevel@tonic-gate 		p->plg_removed = p->plg_to_be_removed;
716*7c478bd9Sstevel@tonic-gate 		p = p->plg_next;
717*7c478bd9Sstevel@tonic-gate 	}
718*7c478bd9Sstevel@tonic-gate }
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate /*
721*7c478bd9Sstevel@tonic-gate  * block signals -- thread-specific blocking of the signals expected
722*7c478bd9Sstevel@tonic-gate  * by the main thread.
723*7c478bd9Sstevel@tonic-gate  */
724*7c478bd9Sstevel@tonic-gate 
725*7c478bd9Sstevel@tonic-gate static void
726*7c478bd9Sstevel@tonic-gate block_signals()
727*7c478bd9Sstevel@tonic-gate {
728*7c478bd9Sstevel@tonic-gate 	sigset_t	set;
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate 	(void) sigfillset(&set);
731*7c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_BLOCK, &set, NULL);
732*7c478bd9Sstevel@tonic-gate }
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate /*
735*7c478bd9Sstevel@tonic-gate  * signal_thread is the designated signal catcher.  It wakes up the
736*7c478bd9Sstevel@tonic-gate  * main thread whenever it receives a signal and then goes back to
737*7c478bd9Sstevel@tonic-gate  * sleep; it does not exit.  The global variables caught_* let
738*7c478bd9Sstevel@tonic-gate  * the main thread which signal was received.
739*7c478bd9Sstevel@tonic-gate  *
740*7c478bd9Sstevel@tonic-gate  * The thread is created with all signals blocked.
741*7c478bd9Sstevel@tonic-gate  */
742*7c478bd9Sstevel@tonic-gate 
743*7c478bd9Sstevel@tonic-gate static void
744*7c478bd9Sstevel@tonic-gate signal_thread()
745*7c478bd9Sstevel@tonic-gate {
746*7c478bd9Sstevel@tonic-gate 	sigset_t	set;
747*7c478bd9Sstevel@tonic-gate 	int		signal_caught;
748*7c478bd9Sstevel@tonic-gate 
749*7c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "the signal thread is thread %d\n",
750*7c478bd9Sstevel@tonic-gate 	    pthread_self()));
751*7c478bd9Sstevel@tonic-gate 
752*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
753*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGALRM);
754*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, AU_SIG_DISABLE);
755*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, AU_SIG_READ_CONTROL);
756*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, AU_SIG_NEXT_DIR);
757*7c478bd9Sstevel@tonic-gate 
758*7c478bd9Sstevel@tonic-gate 	for (;;) {
759*7c478bd9Sstevel@tonic-gate 		signal_caught = sigwait(&set);
760*7c478bd9Sstevel@tonic-gate 		switch (signal_caught) {
761*7c478bd9Sstevel@tonic-gate 		case SIGALRM:
762*7c478bd9Sstevel@tonic-gate 			caught_alrm++;
763*7c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "caught SIGALRM\n"));
764*7c478bd9Sstevel@tonic-gate 			break;
765*7c478bd9Sstevel@tonic-gate 		case AU_SIG_DISABLE:
766*7c478bd9Sstevel@tonic-gate 			caught_term++;
767*7c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "caught AU_SIG_DISABLE\n"));
768*7c478bd9Sstevel@tonic-gate 			break;
769*7c478bd9Sstevel@tonic-gate 		case AU_SIG_READ_CONTROL:
770*7c478bd9Sstevel@tonic-gate 			caught_readc++;
771*7c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "caught AU_SIG_READ_CONTROL\n"));
772*7c478bd9Sstevel@tonic-gate 			break;
773*7c478bd9Sstevel@tonic-gate 		case AU_SIG_NEXT_DIR:
774*7c478bd9Sstevel@tonic-gate 			caught_nextd++;
775*7c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "caught AU_SIG_NEXT_DIR\n"));
776*7c478bd9Sstevel@tonic-gate 			break;
777*7c478bd9Sstevel@tonic-gate 		default:
778*7c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "caught unexpected signal:  %d\n",
779*7c478bd9Sstevel@tonic-gate 			    signal_caught));
780*7c478bd9Sstevel@tonic-gate 			break;
781*7c478bd9Sstevel@tonic-gate 		}
782*7c478bd9Sstevel@tonic-gate 		(void) pthread_cond_signal(&(main_thr.thd_cv));
783*7c478bd9Sstevel@tonic-gate 	}
784*7c478bd9Sstevel@tonic-gate }
785*7c478bd9Sstevel@tonic-gate 
786*7c478bd9Sstevel@tonic-gate /*
787*7c478bd9Sstevel@tonic-gate  * do_sethost - do auditon(2) to set the audit host-id.
788*7c478bd9Sstevel@tonic-gate  *		Returns 0 if success, error code or -1 otherwise.
789*7c478bd9Sstevel@tonic-gate  */
790*7c478bd9Sstevel@tonic-gate static int
791*7c478bd9Sstevel@tonic-gate do_sethost(void)
792*7c478bd9Sstevel@tonic-gate {
793*7c478bd9Sstevel@tonic-gate 	int		err;
794*7c478bd9Sstevel@tonic-gate 	char		host_name[MAXHOSTNAMELEN + 1];
795*7c478bd9Sstevel@tonic-gate 	auditinfo_addr_t	audit_info;
796*7c478bd9Sstevel@tonic-gate 	struct addrinfo	hints;
797*7c478bd9Sstevel@tonic-gate 	struct addrinfo	*ai;
798*7c478bd9Sstevel@tonic-gate 	int		addr_type;
799*7c478bd9Sstevel@tonic-gate 	void		*p;
800*7c478bd9Sstevel@tonic-gate 
801*7c478bd9Sstevel@tonic-gate 	/* First, get our machine name and convert to IP address */
802*7c478bd9Sstevel@tonic-gate 	if ((err = gethostname(host_name, sizeof (host_name)))) {
803*7c478bd9Sstevel@tonic-gate 		return (err);
804*7c478bd9Sstevel@tonic-gate 	}
805*7c478bd9Sstevel@tonic-gate 	(void) memset(&hints, 0, sizeof (hints));
806*7c478bd9Sstevel@tonic-gate 	hints.ai_family = PF_INET;
807*7c478bd9Sstevel@tonic-gate 	err = getaddrinfo(host_name, NULL, &hints, &ai);
808*7c478bd9Sstevel@tonic-gate 	if (err == 0) {
809*7c478bd9Sstevel@tonic-gate 		addr_type = AU_IPv4;
810*7c478bd9Sstevel@tonic-gate 		/* LINTED */
811*7c478bd9Sstevel@tonic-gate 		p = &((struct sockaddr_in *)ai->ai_addr)->sin_addr;
812*7c478bd9Sstevel@tonic-gate 	} else {
813*7c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET6;
814*7c478bd9Sstevel@tonic-gate 		err = getaddrinfo(host_name, NULL, &hints, &ai);
815*7c478bd9Sstevel@tonic-gate 		if (err != 0) {
816*7c478bd9Sstevel@tonic-gate 			return (-1);
817*7c478bd9Sstevel@tonic-gate 		}
818*7c478bd9Sstevel@tonic-gate 		addr_type = AU_IPv6;
819*7c478bd9Sstevel@tonic-gate 		/* LINTED */
820*7c478bd9Sstevel@tonic-gate 		p = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
821*7c478bd9Sstevel@tonic-gate 	}
822*7c478bd9Sstevel@tonic-gate 
823*7c478bd9Sstevel@tonic-gate 	/* Get current kernel audit info, and fill in the IP address */
824*7c478bd9Sstevel@tonic-gate 	if ((err = auditon(A_GETKAUDIT, (caddr_t)&audit_info,
825*7c478bd9Sstevel@tonic-gate 	    sizeof (audit_info))) < 0) {
826*7c478bd9Sstevel@tonic-gate 		return (err);
827*7c478bd9Sstevel@tonic-gate 	}
828*7c478bd9Sstevel@tonic-gate 	audit_info.ai_termid.at_type = addr_type;
829*7c478bd9Sstevel@tonic-gate 	(void) memcpy(&audit_info.ai_termid.at_addr[0], p,
830*7c478bd9Sstevel@tonic-gate 	    addr_type);
831*7c478bd9Sstevel@tonic-gate 
832*7c478bd9Sstevel@tonic-gate 	freeaddrinfo(ai);
833*7c478bd9Sstevel@tonic-gate 
834*7c478bd9Sstevel@tonic-gate 	/* Update the kernel audit info with new IP address */
835*7c478bd9Sstevel@tonic-gate 	if ((err = auditon(A_SETKAUDIT, (caddr_t)&audit_info,
836*7c478bd9Sstevel@tonic-gate 	    sizeof (audit_info))) < 0) {
837*7c478bd9Sstevel@tonic-gate 		return (err);
838*7c478bd9Sstevel@tonic-gate 	}
839*7c478bd9Sstevel@tonic-gate 
840*7c478bd9Sstevel@tonic-gate 	return (0);
841*7c478bd9Sstevel@tonic-gate }
842