xref: /illumos-gate/usr/src/lib/libbsm/common/audit_halt.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <sys/fcntl.h>
32 #include <bsm/audit.h>
33 #include <bsm/audit_record.h>
34 #include <bsm/audit_uevents.h>
35 #include <bsm/libbsm.h>
36 #include <bsm/audit_private.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <syslog.h>
40 #include <netinet/in.h>
41 #include <libgen.h>
42 #include <generic.h>
43 
44 #ifdef C2_DEBUG
45 #define	dprintf(x) {printf x; }
46 #else
47 #define	dprintf(x)
48 #endif
49 
50 static int audit_halt_generic(int);
51 
52 /* ARGSUSED */
53 int
54 audit_halt_setup(int argc, char **argv)
55 {
56 	char *cmdname;
57 
58 	dprintf(("audit_halt_setup()\n"));
59 
60 	if (cannot_audit(0)) {
61 		return (0);
62 	}
63 
64 	cmdname = basename(*argv);
65 
66 	aug_init();
67 
68 	if (strcmp(cmdname, "halt") == 0)
69 		aug_save_event(AUE_halt_solaris);
70 	else if (strcmp(cmdname, "poweroff") == 0)
71 		aug_save_event(AUE_poweroff_solaris);
72 	else
73 		exit(1);
74 	(void) aug_save_me();
75 	return (0);
76 }
77 
78 int
79 audit_halt_fail()
80 {
81 	return (audit_halt_generic(-1));
82 }
83 
84 int
85 audit_halt_success()
86 {
87 	int res = 0;
88 
89 	(void) audit_halt_generic(0);
90 
91 	/* wait for audit daemon to put halt message onto audit trail */
92 	if (!cannot_audit(0)) {
93 		int cond = AUC_NOAUDIT;
94 		int canaudit;
95 
96 		(void) sleep(1);
97 
98 		/* find out if audit daemon is running */
99 		(void) auditon(A_GETCOND, (caddr_t)&cond, sizeof (cond));
100 		canaudit = ((cond == AUC_AUDITING) || (cond == AUC_NOSPACE));
101 
102 		/* turn off audit daemon and try to flush audit queue */
103 		if (canaudit && system("/usr/sbin/audit -t"))
104 			res = -1;
105 		else
106 		/* give a chance for syslogd to do the job */
107 			(void) sleep(5);
108 	}
109 
110 	return (res);
111 }
112 
113 int
114 audit_halt_generic(sorf)
115 	int sorf;
116 {
117 	int r;
118 
119 	dprintf(("audit_halt_generic(%d)\n", sorf));
120 
121 	if (cannot_audit(0)) {
122 		return (0);
123 	}
124 
125 	aug_save_sorf(sorf);
126 	r = aug_audit();
127 
128 	return (r);
129 }
130