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.
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.
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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
31 #include <sys/types.h>
34 #include <bsm/audit.h>
35 #include <bsm/libbsm.h>
38 #define ALLOC_INIT (600) /* initially allocate ALLOC_INIT map entries */
39 #define ALLOC_INCR (100) /* if more map entries are needed, realloc */
40 /* in ALLOC_INCR increments */
42 static int alloc_map();
43 static int load_map();
44 static int realloc_map();
46 typedef struct event_map
{
47 au_event_t event
; /* audit event number */
48 au_class_t
class; /* audit event class mask */
51 static event_map_t
*event_map
; /* the map */
52 static uint_t alloc_count
; /* number of entries currently allocated */
53 static uint_t event_count
; /* number of entries in map */
54 static mutex_t mutex_au_preselect
= DEFAULTMUTEX
;
59 * Keep a dynamic array of event<-->class mappings.
60 * Refresh the map when the value of flag is AU_PRS_REREAD.
62 * 1: The event is preselected.
63 * 0: The event is not preselected.
64 * -1: There was an error:
65 * Couldn't allocate memory.
66 * Couldn't find event.
69 au_preselect(au_event_t au_event
, au_mask_t
*au_mask_p
, int sorf
, int flag
)
71 static char been_here_before
; /* we cache the map */
73 register au_class_t comp_class
;
75 (void) mutex_lock(&mutex_au_preselect
);
76 if (!been_here_before
) {
77 if (alloc_map() == -1) {
78 (void) mutex_unlock(&mutex_au_preselect
);
82 if (load_map() == -1) {
83 (void) mutex_unlock(&mutex_au_preselect
);
91 * Don't use the cache. Re-read the audit_event(5) db every time
93 if (flag
== AU_PRS_REREAD
) {
94 if (load_map() == -1) {
95 (void) mutex_unlock(&mutex_au_preselect
);
100 /* Determine what portion of the preselection mask to check. */
101 if (sorf
== AU_PRS_SUCCESS
)
102 comp_class
= au_mask_p
->am_success
;
103 else if (sorf
== AU_PRS_FAILURE
)
104 comp_class
= au_mask_p
->am_failure
;
106 comp_class
= au_mask_p
->am_success
| au_mask_p
->am_failure
;
108 for (i
= 0; i
< event_count
; i
++) {
109 if (event_map
[i
].event
== au_event
) {
110 if (event_map
[i
].class & comp_class
) {
111 (void) mutex_unlock(&mutex_au_preselect
);
114 (void) mutex_unlock(&mutex_au_preselect
);
120 (void) mutex_unlock(&mutex_au_preselect
);
121 return (-1); /* could not find event in the table */
125 * Initially allocate about as many map entries as are there
126 * are audit events shipped with the system. For sites
127 * that don't add audit events, this should be enough.
132 if ((event_map
= (event_map_t
*)
133 calloc(ALLOC_INIT
, (size_t)sizeof (event_map_t
))) ==
137 alloc_count
= ALLOC_INIT
;
143 * load the event<->class map into memory
148 register au_event_ent_t
*evp
;
152 while ((evp
= getauevent()) != (au_event_ent_t
*)NULL
) {
153 if (event_count
> alloc_count
)
154 if (realloc_map() == -1) {
158 event_map
[event_count
].event
= evp
->ae_number
;
159 event_map
[event_count
].class = evp
->ae_class
;
168 * realloc the event map in ALLOC_INCR increments
173 register size_t rsize
;
174 rsize
= sizeof (event_map_t
) * (alloc_count
+ ALLOC_INCR
);
176 if ((event_map
= (event_map_t
*)
177 realloc(event_map
, rsize
)) == (event_map_t
*)NULL
)