summaryrefslogtreecommitdiff
path: root/usr/src/lib/libbsm/common/au_preselect.c
blob: 67512a62129854ae29bccbfe9670ebbf494bd87f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * au_preselect.c
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <bsm/audit.h>
#include <bsm/libbsm.h>
#include <synch.h>

#define	ALLOC_INIT (600)	/* initially allocate ALLOC_INIT map entries */
#define	ALLOC_INCR (100)	/* if more map entries are needed, realloc */
				/* in ALLOC_INCR increments */

static int alloc_map();
static int load_map();
static int realloc_map();

typedef struct event_map {
	au_event_t event;	/* audit event number */
	au_class_t class;	/* audit event class mask */
} event_map_t;

static event_map_t *event_map;	/* the map */
static uint_t alloc_count;	/* number of entries currently allocated */
static uint_t event_count;	/* number of entries in map */
static mutex_t mutex_au_preselect = DEFAULTMUTEX;

/*
 * au_preselect:
 *
 * Keep a dynamic array of event<-->class mappings.
 * Refresh the map when the value of flag is AU_PRS_REREAD.
 * Return:
 *	1: The event is preselected.
 *	0: The event is not preselected.
 *	-1: There was an error:
 *		Couldn't allocate memory.
 *		Couldn't find event.
 */
int
au_preselect(au_event_t au_event, au_mask_t *au_mask_p, int sorf, int flag)
{
	static char been_here_before;  /* we cache the map */
	register int i;
	register au_class_t comp_class;

	(void) mutex_lock(&mutex_au_preselect);
	if (!been_here_before) {
		if (alloc_map() == -1) {
			(void) mutex_unlock(&mutex_au_preselect);
			return (-1);
		}

		if (load_map() == -1) {
			(void) mutex_unlock(&mutex_au_preselect);
			return (-1);
		}

		been_here_before = 1;
	}

	/*
	 * Don't use the cache. Re-read the audit_event(5) db every time
	 */
	if (flag == AU_PRS_REREAD) {
		if (load_map() == -1) {
			(void) mutex_unlock(&mutex_au_preselect);
			return (-1);
		}
	}

	/* Determine what portion of the preselection mask to check. */
	if (sorf == AU_PRS_SUCCESS)
		comp_class = au_mask_p->am_success;
	else if (sorf == AU_PRS_FAILURE)
		comp_class = au_mask_p->am_failure;
	else
		comp_class = au_mask_p->am_success | au_mask_p->am_failure;

	for (i = 0; i < event_count; i++) {
		if (event_map[i].event == au_event) {
			if (event_map[i].class & comp_class) {
				(void) mutex_unlock(&mutex_au_preselect);
				return (1);
			} else {
				(void) mutex_unlock(&mutex_au_preselect);
				return (0);
			}
		}
	}

	(void) mutex_unlock(&mutex_au_preselect);
	return (-1);	/* could not find event in the table */
}

/*
 * Initially allocate about as many map entries as are there
 * are audit events shipped with the system. For sites
 * that don't add audit events, this should be enough.
 */
static int
alloc_map()
{
	if ((event_map = (event_map_t *)
	    calloc(ALLOC_INIT, (size_t)sizeof (event_map_t))) ==
	    (event_map_t *)NULL)
		return (-1);
	else
		alloc_count = ALLOC_INIT;

	return (0);
}

/*
 * load the event<->class map into memory
 */
static int
load_map()
{
	register au_event_ent_t *evp;

	event_count = 0;
	setauevent();
	while ((evp = getauevent()) != (au_event_ent_t *)NULL) {
		if (event_count > alloc_count)
			if (realloc_map() == -1) {
				endauevent();
				return (-1);
			}
		event_map[event_count].event = evp->ae_number;
		event_map[event_count].class = evp->ae_class;
		++event_count;
	}
	endauevent();

	return (0);
}

/*
 * realloc the event map in ALLOC_INCR increments
 */
static int
realloc_map()
{
	register size_t rsize;
	rsize = sizeof (event_map_t) * (alloc_count + ALLOC_INCR);

	if ((event_map = (event_map_t *)
	    realloc(event_map, rsize)) == (event_map_t *)NULL)
		return (-1);

	return (0);
}