xref: /titanic_44/usr/src/lib/libbsm/common/au_preselect.c (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * au_preselect.c
31  */
32 
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <bsm/audit.h>
37 #include <bsm/libbsm.h>
38 #include <synch.h>
39 
40 #define	ALLOC_INIT (600)	/* initially allocate ALLOC_INIT map entries */
41 #define	ALLOC_INCR (100)	/* if more map entries are needed, realloc */
42 				/* in ALLOC_INCR increments */
43 
44 static int alloc_map();
45 static int load_map();
46 static int realloc_map();
47 
48 typedef struct event_map {
49 	au_event_t event;	/* audit event number */
50 	au_class_t class;	/* audit event class mask */
51 } event_map_t;
52 
53 static event_map_t *event_map;	/* the map */
54 static uint_t alloc_count;	/* number of entries currently allocated */
55 static uint_t event_count;	/* number of entries in map */
56 static mutex_t mutex_au_preselect = DEFAULTMUTEX;
57 
58 /*
59  * au_preselect:
60  *
61  * Keep a dynamic array of event<-->class mappings.
62  * Refresh the map when the value of flag is non-zero.
63  * Return:
64  *	1: The event is preselected.
65  *	0: The event is not preselected.
66  *	-1: There was an error:
67  *		Couldn't allocate memory.
68  *		Couldn't find event.
69  */
70 int
71 #ifdef __STDC__
72 au_preselect(au_event_t au_event, au_mask_t *au_mask_p, int sorf, int flag)
73 #else
74 au_preselect(au_event, au_mask_p, sorf, flag)
75 	au_event_t au_event;	/* event */
76 	au_mask_t *au_mask_p;	/* preselection mask */
77 	int sorf;		/* success or failure */
78 	int flag;		/* re-read flag */
79 #endif /* __STDC__ */
80 {
81 	static char been_here_before;  /* we cache the map */
82 	register int i;
83 	register au_class_t comp_class;
84 
85 	(void) mutex_lock(&mutex_au_preselect);
86 	if (!been_here_before) {
87 		if (alloc_map() == -1) {
88 			(void) mutex_unlock(&mutex_au_preselect);
89 			return (-1);
90 		}
91 
92 		if (load_map() == -1) {
93 			(void) mutex_unlock(&mutex_au_preselect);
94 			return (-1);
95 		}
96 
97 		been_here_before = 1;
98 	}
99 
100 	/*
101 	 * Don't use the cache. Re-read the audit_event(5) db every time
102 	 */
103 	if (flag == AU_PRS_REREAD) {
104 		if (load_map() == -1) {
105 			(void) mutex_unlock(&mutex_au_preselect);
106 			return (-1);
107 		}
108 	}
109 
110 	/* Determine what portion of the preselection mask to check. */
111 	if (sorf == AU_PRS_SUCCESS)
112 		comp_class = au_mask_p->am_success;
113 	else if (sorf == AU_PRS_FAILURE)
114 		comp_class = au_mask_p->am_failure;
115 	else
116 		comp_class = au_mask_p->am_success | au_mask_p->am_failure;
117 
118 	for (i = 0; i < event_count; i++) {
119 		if (event_map[i].event == au_event) {
120 			if (event_map[i].class & comp_class) {
121 				(void) mutex_unlock(&mutex_au_preselect);
122 				return (1);
123 			} else {
124 				(void) mutex_unlock(&mutex_au_preselect);
125 				return (0);
126 			}
127 		}
128 	}
129 
130 	(void) mutex_unlock(&mutex_au_preselect);
131 	return (-1);	/* could not find event in the table */
132 }
133 
134 /*
135  * Initially allocate about as many map entries as are there
136  * are audit events shipped with the system. For sites
137  * that don't add audit events, this should be enough.
138  */
139 static int
140 alloc_map()
141 {
142 	if ((event_map = (event_map_t *)
143 	    calloc(ALLOC_INIT, (size_t)sizeof (event_map_t))) ==
144 	    (event_map_t *)NULL)
145 		return (-1);
146 	else
147 		alloc_count = ALLOC_INIT;
148 
149 	return (0);
150 }
151 
152 /*
153  * load the event<->class map into memory
154  */
155 static int
156 load_map()
157 {
158 	register au_event_ent_t *evp;
159 
160 	event_count = 0;
161 	setauevent();
162 	while ((evp = getauevent()) != (au_event_ent_t *)NULL) {
163 		if (event_count > alloc_count)
164 			if (realloc_map() == -1) {
165 				endauevent();
166 				return (-1);
167 			}
168 		event_map[event_count].event = evp->ae_number;
169 		event_map[event_count].class = evp->ae_class;
170 		++event_count;
171 	}
172 	endauevent();
173 
174 	return (0);
175 }
176 
177 /*
178  * realloc the event map in ALLOC_INCR increments
179  */
180 static int
181 realloc_map()
182 {
183 	register size_t rsize;
184 	rsize = sizeof (event_map_t) * (alloc_count + ALLOC_INCR);
185 
186 	if ((event_map = (event_map_t *)
187 	    realloc(event_map, rsize)) == (event_map_t *)NULL)
188 		return (-1);
189 
190 	return (0);
191 }
192