xref: /illumos-gate/usr/src/cmd/audit/audit.c (revision bbf21555)
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
56d59ee37Spaulson  * Common Development and Distribution License (the "License").
66d59ee37Spaulson  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
211b2d1c94SMarek Pospisil 
227c478bd9Sstevel@tonic-gate /*
231b2d1c94SMarek Pospisil  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
2636ac8d7dSJohn Levon /*
2736ac8d7dSJohn Levon  * Copyright (c) 2018, Joyent, Inc.
2836ac8d7dSJohn Levon  */
2936ac8d7dSJohn Levon 
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <libscf.h>
327c478bd9Sstevel@tonic-gate #include <secdb.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <sys/file.h>
37f8994074SJan Friedel #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/wait.h>
407c478bd9Sstevel@tonic-gate #include <signal.h>
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
447c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
457c478bd9Sstevel@tonic-gate #include <locale.h>
467c478bd9Sstevel@tonic-gate #include <zone.h>
47f8994074SJan Friedel #include <audit_scf.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
507c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SUNW_OST_OSCMD"
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	VERIFY -1
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* GLOBALS */
567c478bd9Sstevel@tonic-gate static char	*progname = "audit";
57f8994074SJan Friedel static char	*usage = "audit [-n] | [-s] | [-t] | [-v]";
581b2d1c94SMarek Pospisil static int	silent = 0;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static void	display_smf_error();
617c478bd9Sstevel@tonic-gate 
62f8994074SJan Friedel static boolean_t is_audit_config_ok();		/* config validation  */
637c478bd9Sstevel@tonic-gate static boolean_t is_valid_zone(boolean_t);	/* operation ok in this zone? */
64f8994074SJan Friedel static boolean_t contains_valid_dirs(char *);	/* p_dir contents validation */
65f8994074SJan Friedel static boolean_t validate_path(char *);		/* is it path to dir? */
66f8994074SJan Friedel static void	start_auditd();			/* start audit daemon */
674c17c04fSgww static int	sig_auditd(int);		/* send signal to auditd */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * audit() - This program serves as a general administrator's interface to
717c478bd9Sstevel@tonic-gate  *	the audit trail.  Only one option is valid at a time.
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  * input:
747c478bd9Sstevel@tonic-gate  *	audit -s
75f8994074SJan Friedel  *		- signal audit daemon to read audit configuration and
767c478bd9Sstevel@tonic-gate  *		  start auditd if needed.
777c478bd9Sstevel@tonic-gate  *	audit -n
78f8994074SJan Friedel  *		- signal audit daemon to use next audit_binfile directory.
797c478bd9Sstevel@tonic-gate  *	audit -t
801b2d1c94SMarek Pospisil  *		- signal audit daemon to disable auditing.
811b2d1c94SMarek Pospisil  *	audit -T
821b2d1c94SMarek Pospisil  *		- signal audit daemon to temporarily disable auditing reporting
831b2d1c94SMarek Pospisil  *		  no errors.
84f8994074SJan Friedel  *	audit -v
85f8994074SJan Friedel  *		- validate audit configuration parameters;
86f8994074SJan Friedel  *		  Print errors or "configuration ok".
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  *
897c478bd9Sstevel@tonic-gate  * output:
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * returns:	0 - command successful
927c478bd9Sstevel@tonic-gate  *		>0 - command failed
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate 
957883e825Spaulson int
main(int argc,char * argv[])967c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
977c478bd9Sstevel@tonic-gate {
98f8994074SJan Friedel 	int	c;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/* Internationalization */
1017c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1027c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1037c478bd9Sstevel@tonic-gate 
104f8994074SJan Friedel 	/* second or more options not allowed; please pick one */
105f8994074SJan Friedel 	if (argc > 2) {
1067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("usage: %s\n"), usage);
107f8994074SJan Friedel 		exit(1);
1087c478bd9Sstevel@tonic-gate 	}
109f8994074SJan Friedel 
110f8994074SJan Friedel 	/* first option required */
111f8994074SJan Friedel 	if ((c = getopt(argc, argv, "nstTv")) == -1) {
1127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("usage: %s\n"), usage);
113f8994074SJan Friedel 		exit(1);
1147c478bd9Sstevel@tonic-gate 	}
115f8994074SJan Friedel 
1167c478bd9Sstevel@tonic-gate 	switch (c) {
1177c478bd9Sstevel@tonic-gate 	case 'n':
1187c478bd9Sstevel@tonic-gate 		if (!is_valid_zone(1))	/* 1 == display error if any */
119f8994074SJan Friedel 			exit(1);
1207c478bd9Sstevel@tonic-gate 
121f8994074SJan Friedel 		if (sig_auditd(SIGUSR1) != 0)
1224c17c04fSgww 			exit(1);
1237c478bd9Sstevel@tonic-gate 		break;
1247c478bd9Sstevel@tonic-gate 	case 's':
1257c478bd9Sstevel@tonic-gate 		if (!is_valid_zone(1))	/* 1 == display error if any */
126f8994074SJan Friedel 			exit(1);
127f8994074SJan Friedel 		else if (!is_audit_config_ok())
128f8994074SJan Friedel 			exit(1);
1297c478bd9Sstevel@tonic-gate 
130f8994074SJan Friedel 		start_auditd();
131f8994074SJan Friedel 		return (0);
1327c478bd9Sstevel@tonic-gate 	case 't':
1337c478bd9Sstevel@tonic-gate 		if (!is_valid_zone(0))	/* 0 == no error message display */
134f8994074SJan Friedel 			exit(1);
135005d3febSMarek Pospisil 		if (smf_disable_instance(AUDITD_FMRI, 0) != 0) {
1367c478bd9Sstevel@tonic-gate 			display_smf_error();
137f8994074SJan Friedel 			exit(1);
1386d59ee37Spaulson 		}
1397c478bd9Sstevel@tonic-gate 		break;
1401b2d1c94SMarek Pospisil 	case 'T':
1411b2d1c94SMarek Pospisil 		silent = 1;
1421b2d1c94SMarek Pospisil 		if (!is_valid_zone(0))	/* 0 == no error message display */
143f8994074SJan Friedel 			exit(1);
1441b2d1c94SMarek Pospisil 		if (smf_disable_instance(AUDITD_FMRI, SMF_TEMPORARY) != 0) {
145f8994074SJan Friedel 			exit(1);
1461b2d1c94SMarek Pospisil 		}
1471b2d1c94SMarek Pospisil 		break;
1487c478bd9Sstevel@tonic-gate 	case 'v':
149f8994074SJan Friedel 		if (is_audit_config_ok()) {
150f8994074SJan Friedel 			(void) fprintf(stderr, gettext("configuration ok\n"));
1517c478bd9Sstevel@tonic-gate 			exit(0);
1527c478bd9Sstevel@tonic-gate 		} else {
153f8994074SJan Friedel 			exit(1);
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 		break;
1567c478bd9Sstevel@tonic-gate 	default:
1577c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("usage: %s\n"), usage);
158f8994074SJan Friedel 		exit(1);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	return (0);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1654c17c04fSgww  * sig_auditd(sig)
1667c478bd9Sstevel@tonic-gate  *
1674c17c04fSgww  * send a signal to auditd service
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  * returns:	0 - successful
1707c478bd9Sstevel@tonic-gate  *		1 - error
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static int
sig_auditd(int sig)1744c17c04fSgww sig_auditd(int sig)
1757c478bd9Sstevel@tonic-gate {
1764c17c04fSgww 	scf_simple_prop_t *prop = NULL;
1774c17c04fSgww 	uint64_t	*cid = NULL;
1787c478bd9Sstevel@tonic-gate 
1794c17c04fSgww 	if ((prop = scf_simple_prop_get(NULL, AUDITD_FMRI, SCF_PG_RESTARTER,
1804c17c04fSgww 	    SCF_PROPERTY_CONTRACT)) == NULL) {
1814c17c04fSgww 		display_smf_error();
1824c17c04fSgww 		return (1);
1834c17c04fSgww 	}
1844c17c04fSgww 	if ((scf_simple_prop_numvalues(prop) < 0) ||
1854c17c04fSgww 	    (cid = scf_simple_prop_next_count(prop)) == NULL) {
1864c17c04fSgww 		scf_simple_prop_free(prop);
1874c17c04fSgww 		display_smf_error();
1884c17c04fSgww 		return (1);
1894c17c04fSgww 	}
1904c17c04fSgww 	if (sigsend(P_CTID, (ctid_t)*cid, sig) != 0) {
1914c17c04fSgww 		perror("audit: can't signal auditd");
1924c17c04fSgww 		scf_simple_prop_free(prop);
1937c478bd9Sstevel@tonic-gate 		return (1);
1947c478bd9Sstevel@tonic-gate 	}
1954c17c04fSgww 	scf_simple_prop_free(prop);
1964c17c04fSgww 	return (0);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
200f8994074SJan Friedel  * perform reasonableness check on audit configuration
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate static boolean_t
is_audit_config_ok()204f8994074SJan Friedel is_audit_config_ok() {
205f8994074SJan Friedel 	int			state = B_TRUE;	/* B_TRUE/B_FALSE = ok/not_ok */
206f8994074SJan Friedel 	char			*cval_str;
207f8994074SJan Friedel 	int			cval_int;
208f8994074SJan Friedel 	kva_t			*kvlist;
209f8994074SJan Friedel 	scf_plugin_kva_node_t   *plugin_kva_ll;
210f8994074SJan Friedel 	scf_plugin_kva_node_t   *plugin_kva_ll_head;
211f8994074SJan Friedel 	boolean_t		one_plugin_enabled = B_FALSE;
212f8994074SJan Friedel 
2137c478bd9Sstevel@tonic-gate 	/*
214f8994074SJan Friedel 	 * There must be at least one active plugin configured; if the
215*bbf21555SRichard Lowe 	 * configured plugin is audit_binfile(7), then the p_dir must not be
216f8994074SJan Friedel 	 * empty.
2177c478bd9Sstevel@tonic-gate 	 */
218f8994074SJan Friedel 	if (!do_getpluginconfig_scf(NULL, &plugin_kva_ll)) {
2197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
220f8994074SJan Friedel 		    gettext("Could not get plugin configuration.\n"));
221f8994074SJan Friedel 		exit(1);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
224f8994074SJan Friedel 	plugin_kva_ll_head = plugin_kva_ll;
225f8994074SJan Friedel 
226f8994074SJan Friedel 	while (plugin_kva_ll != NULL) {
227f8994074SJan Friedel 		kvlist = plugin_kva_ll->plugin_kva;
228f8994074SJan Friedel 
229f8994074SJan Friedel 		if (!one_plugin_enabled) {
230f8994074SJan Friedel 			cval_str = kva_match(kvlist, "active");
231f8994074SJan Friedel 			if (atoi(cval_str) == 1) {
232f8994074SJan Friedel 				one_plugin_enabled = B_TRUE;
2331a578a15Spaulson 			}
2347c478bd9Sstevel@tonic-gate 		}
235f8994074SJan Friedel 
236f8994074SJan Friedel 		if (strcmp((char *)&(*plugin_kva_ll).plugin_name,
237f8994074SJan Friedel 		    "audit_binfile") == 0) {
238f8994074SJan Friedel 			cval_str = kva_match(kvlist, "p_dir");
23936ac8d7dSJohn Levon 			if (cval_str == NULL || cval_str[0] == '\0') {
240f8994074SJan Friedel 				(void) fprintf(stderr,
241*bbf21555SRichard Lowe 				    gettext("%s: audit_binfile(7) \"p_dir:\" "
242f8994074SJan Friedel 				    "attribute empty\n"), progname);
243f8994074SJan Friedel 				state = B_FALSE;
244f8994074SJan Friedel 			} else if (!contains_valid_dirs(cval_str)) {
245f8994074SJan Friedel 				(void) fprintf(stderr,
246*bbf21555SRichard Lowe 				    gettext("%s: audit_binfile(7) \"p_dir:\" "
247f8994074SJan Friedel 				    "attribute invalid\n"), progname);
248f8994074SJan Friedel 				state = B_FALSE;
249f8994074SJan Friedel 			}
250f8994074SJan Friedel 
251f8994074SJan Friedel 			cval_str = kva_match(kvlist, "p_minfree");
252f8994074SJan Friedel 			cval_int = atoi(cval_str);
253f8994074SJan Friedel 			if (cval_int < 0 || cval_int > 100) {
254f8994074SJan Friedel 				(void) fprintf(stderr,
255*bbf21555SRichard Lowe 				    gettext("%s: audit_binfile(7) "
256f8994074SJan Friedel 				    "\"p_minfree:\" attribute invalid\n"),
257f8994074SJan Friedel 				    progname);
258f8994074SJan Friedel 				state = B_FALSE;
259f8994074SJan Friedel 			}
260f8994074SJan Friedel 		}
261f8994074SJan Friedel 
262f8994074SJan Friedel 		plugin_kva_ll = plugin_kva_ll->next;
2637c478bd9Sstevel@tonic-gate 	}
264f8994074SJan Friedel 
265f8994074SJan Friedel 	plugin_kva_ll_free(plugin_kva_ll_head);
266f8994074SJan Friedel 
267f8994074SJan Friedel 	if (!one_plugin_enabled) {
268f8994074SJan Friedel 		(void) fprintf(stderr, gettext("%s: no active plugin found\n"),
269f8994074SJan Friedel 		    progname);
270f8994074SJan Friedel 		state = B_FALSE;
2717c478bd9Sstevel@tonic-gate 	}
272f8994074SJan Friedel 
2737c478bd9Sstevel@tonic-gate 	return (state);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate  * The operations that call this function are only valid in the global
2787c478bd9Sstevel@tonic-gate  * zone unless the perzone audit policy is set.
2791b2d1c94SMarek Pospisil  *
2801b2d1c94SMarek Pospisil  * "!silent" and "show_err" are slightly different; silent is from
2811b2d1c94SMarek Pospisil  * -T for which no error messages should be displayed and show_err
2821b2d1c94SMarek Pospisil  * applies to more options (including -T)
2831b2d1c94SMarek Pospisil  *
2847c478bd9Sstevel@tonic-gate  */
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate static boolean_t
is_valid_zone(boolean_t show_err)2877c478bd9Sstevel@tonic-gate is_valid_zone(boolean_t show_err)
2887c478bd9Sstevel@tonic-gate {
28996093503SMarek Pospisil 	uint32_t	policy;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (auditon(A_GETPOLICY, (char *)&policy, 0) == -1) {
2921b2d1c94SMarek Pospisil 		if (!silent) {
2931b2d1c94SMarek Pospisil 			(void) fprintf(stderr, gettext(
2941b2d1c94SMarek Pospisil 			    "%s: Cannot read audit policy:  %s\n"),
2951b2d1c94SMarek Pospisil 			    progname, strerror(errno));
2961b2d1c94SMarek Pospisil 		}
2977c478bd9Sstevel@tonic-gate 		return (0);
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	if (policy & AUDIT_PERZONE)
3007c478bd9Sstevel@tonic-gate 		return (1);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
3037c478bd9Sstevel@tonic-gate 		if (show_err)
3047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3057c478bd9Sstevel@tonic-gate 			    gettext("%s: Not valid in a local zone.\n"),
3067c478bd9Sstevel@tonic-gate 			    progname);
3077c478bd9Sstevel@tonic-gate 		return (0);
3087c478bd9Sstevel@tonic-gate 	} else {
3097c478bd9Sstevel@tonic-gate 		return (1);
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
313f8994074SJan Friedel /*
314f8994074SJan Friedel  * Verify, whether the dirs_str contains at least one currently valid path to
315f8994074SJan Friedel  * the directory. All invalid paths are reported. In case no valid directory
316f8994074SJan Friedel  * path is found function returns B_FALSE, otherwise B_TRUE.
317f8994074SJan Friedel  */
318f8994074SJan Friedel 
319f8994074SJan Friedel static boolean_t
contains_valid_dirs(char * dirs_str)320f8994074SJan Friedel contains_valid_dirs(char *dirs_str)
321f8994074SJan Friedel {
322f8994074SJan Friedel 	boolean_t	rc = B_FALSE;
323f8994074SJan Friedel 	boolean_t	rc_validate_path = B_TRUE;
324f8994074SJan Friedel 	char		*tok_ptr;
325f8994074SJan Friedel 	char		*tok_lasts;
326f8994074SJan Friedel 
327f8994074SJan Friedel 	if (dirs_str == NULL) {
328f8994074SJan Friedel 		return (rc);
329f8994074SJan Friedel 	}
330f8994074SJan Friedel 
331f8994074SJan Friedel 	if ((tok_ptr = strtok_r(dirs_str, ",", &tok_lasts)) != NULL) {
332f8994074SJan Friedel 		if (validate_path(tok_ptr)) {
333f8994074SJan Friedel 			rc = B_TRUE;
334f8994074SJan Friedel 		} else {
335f8994074SJan Friedel 			rc_validate_path = B_FALSE;
336f8994074SJan Friedel 		}
337f8994074SJan Friedel 		while ((tok_ptr = strtok_r(NULL, ",", &tok_lasts)) != NULL) {
338f8994074SJan Friedel 			if (validate_path(tok_ptr)) {
339f8994074SJan Friedel 				rc = B_TRUE;
340f8994074SJan Friedel 			} else {
341f8994074SJan Friedel 				rc_validate_path = B_FALSE;
342f8994074SJan Friedel 			}
343f8994074SJan Friedel 		}
344f8994074SJan Friedel 	}
345f8994074SJan Friedel 
346f8994074SJan Friedel 	if (rc && !rc_validate_path) {
347f8994074SJan Friedel 		(void) fprintf(stderr, gettext("%s: at least one valid "
348f8994074SJan Friedel 		    "directory path found\n"), progname);
349f8994074SJan Friedel 	}
350f8994074SJan Friedel 
351f8994074SJan Friedel 	return (rc);
352f8994074SJan Friedel }
353f8994074SJan Friedel 
354f8994074SJan Friedel /*
355f8994074SJan Friedel  * Verify, that the dir_path is path to a directory.
356f8994074SJan Friedel  */
357f8994074SJan Friedel 
358f8994074SJan Friedel static boolean_t
validate_path(char * dir_path)359f8994074SJan Friedel validate_path(char *dir_path)
360f8994074SJan Friedel {
361f8994074SJan Friedel 	boolean_t	rc = B_FALSE;
362f8994074SJan Friedel 	struct stat	statbuf;
363f8994074SJan Friedel 
364f8994074SJan Friedel 	if (dir_path == NULL) {
365f8994074SJan Friedel 		return (rc);
366f8994074SJan Friedel 	}
367f8994074SJan Friedel 
368f8994074SJan Friedel 	if (stat(dir_path, &statbuf) == -1) {
369f8994074SJan Friedel 		(void) fprintf(stderr, gettext("%s: %s error: %s\n"), progname,
370f8994074SJan Friedel 		    dir_path, strerror(errno));
371f8994074SJan Friedel 	} else if (statbuf.st_mode & S_IFDIR) {
372f8994074SJan Friedel 			rc = B_TRUE;
373f8994074SJan Friedel 	} else {
374f8994074SJan Friedel 		(void) fprintf(stderr, gettext("%s: %s is not a directory\n"),
375f8994074SJan Friedel 		    progname, dir_path);
376f8994074SJan Friedel 	}
377f8994074SJan Friedel 
378f8994074SJan Friedel 	return (rc);
379f8994074SJan Friedel }
380f8994074SJan Friedel 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * if auditd isn't running, start it.  Otherwise refresh.
3837c478bd9Sstevel@tonic-gate  * First check to see if c2audit is loaded via the auditon()
3847c478bd9Sstevel@tonic-gate  * system call, then check SMF state.
3857c478bd9Sstevel@tonic-gate  */
386f8994074SJan Friedel static void
start_auditd()3877c478bd9Sstevel@tonic-gate start_auditd()
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	int	audit_state;
3907c478bd9Sstevel@tonic-gate 	char	*state;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if (auditon(A_GETCOND, (caddr_t)&audit_state,
3937c478bd9Sstevel@tonic-gate 	    sizeof (audit_state)) != 0)
394f8994074SJan Friedel 		exit(1);
3957c478bd9Sstevel@tonic-gate 
3964c17c04fSgww 	if ((state = smf_get_state(AUDITD_FMRI)) == NULL) {
3977c478bd9Sstevel@tonic-gate 		display_smf_error();
398f8994074SJan Friedel 		exit(1);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 	if (strcmp(SCF_STATE_STRING_ONLINE, state) != 0) {
4014c17c04fSgww 		if (smf_enable_instance(AUDITD_FMRI, 0) != 0) {
4027c478bd9Sstevel@tonic-gate 			display_smf_error();
4036d59ee37Spaulson 			free(state);
404f8994074SJan Friedel 			exit(1);
4056d59ee37Spaulson 		}
4067c478bd9Sstevel@tonic-gate 	} else {
4074c17c04fSgww 		if (smf_refresh_instance(AUDITD_FMRI) != 0) {
4087c478bd9Sstevel@tonic-gate 			display_smf_error();
4096d59ee37Spaulson 			free(state);
410f8994074SJan Friedel 			exit(1);
4116d59ee37Spaulson 		}
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 	free(state);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate static void
display_smf_error()4177c478bd9Sstevel@tonic-gate display_smf_error()
4187c478bd9Sstevel@tonic-gate {
4194c17c04fSgww 	scf_error_t	rc = scf_error();
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	switch (rc) {
4227c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
4237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4247c478bd9Sstevel@tonic-gate 		    "SMF error: \"%s\" not found.\n",
4254c17c04fSgww 		    AUDITD_FMRI);
4267c478bd9Sstevel@tonic-gate 		break;
4277c478bd9Sstevel@tonic-gate 	default:
4286d59ee37Spaulson 		(void) fprintf(stderr, "SMF error: %s\n", scf_strerror(rc));
4297c478bd9Sstevel@tonic-gate 		break;
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate }
432