summaryrefslogtreecommitdiff
path: root/usr/src/lib/libbsm/common/audit_event.c
blob: ad4a5b033afaa658386f9f91fc0a7df94f711633 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * 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
 */
#pragma ident	"%Z%%M%	%I%	%E% SMI"

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

/*
 * Interfaces to audit_event(5)  (/etc/security/audit_event)
 */

/*
 * This routine is obsolete.  I have removed its inclusion by removing
 * the .o from the makefile.  Please use cacheauevent() or any of the
 * getauev* routines.
 */

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

/*
 *	Macros to produce a quoted string containing the value of a
 *	preprocessor macro. For example, if SIZE is defined to be 256,
 *	VAL2STR(SIZE) is "256". This is used to construct format
 *	strings for scanf-family functions below.
 */
#define	QUOTE(x)	#x
#define	VAL2STR(x)	QUOTE(x)

static au_class_t flagstohex(char *);

static char	au_event_fname[PATH_MAX] = AUDITEVENTFILE;
static FILE *au_event_file = (FILE *)0;
static mutex_t mutex_eventfile = DEFAULTMUTEX;
static mutex_t mutex_eventcache = DEFAULTMUTEX;
/*
 * If an error occurs during the call to cacheauclassnam() inside
 * flagstohex() any return value could be seen as a valid class mask so
 * the following global variable, cacheauclass_failure, is set to indicate
 * that an error has occurred.
 */
static int cacheauclass_failure = 0;

extern int _mutex_lock(mutex_t *);
extern int _mutex_unlock(mutex_t *);


void
setauevent()
{
	_mutex_lock(&mutex_eventfile);
	if (au_event_file) {
		(void) fseek(au_event_file, 0L, 0);
	}
	_mutex_unlock(&mutex_eventfile);
}


void
endauevent()
{
	_mutex_lock(&mutex_eventfile);
	if (au_event_file) {
		(void) fclose(au_event_file);
		au_event_file = (FILE *)0;
	}
	_mutex_unlock(&mutex_eventfile);
}

au_event_ent_t *
getauevent()
{
	static au_event_ent_t au_event_entry;
	static char	ename[AU_EVENT_NAME_MAX];
	static char	edesc[AU_EVENT_DESC_MAX];

	/* initialize au_event_entry structure */
	au_event_entry.ae_name = ename;
	au_event_entry.ae_desc = edesc;

	return (getauevent_r(&au_event_entry));
}

au_event_ent_t *
getauevent_r(au_event_entry)
	au_event_ent_t *au_event_entry;
{
	int	i, error = 0, found = 0;
	char	*s, input[AU_EVENT_LINE_MAX];
	char	trim_buf[AU_EVENT_NAME_MAX+1];

	/* open audit event file if it isn't already */
	_mutex_lock(&mutex_eventfile);
	if (!au_event_file)
		if (!(au_event_file = fopen(au_event_fname, "rF"))) {
			_mutex_unlock(&mutex_eventfile);
			return ((au_event_ent_t *)0);
		}

	while (fgets(input, AU_EVENT_LINE_MAX, au_event_file)) {
		if (input[0] != '#') {
			s = input + strspn(input, " \t\r\n");
			if ((*s == '\0') || (*s == '#')) {
				continue;
			}
			found = 1;
			s = input;

			/* parse number */
			i = strcspn(s, ":");
			s[i] = '\0';
			(void) sscanf(s, "%hd", &au_event_entry->ae_number);
			s = &s[i+1];

			/* parse event name */
			i = strcspn(s, ":");
			s[i] = '\0';
			(void) sscanf(s, "%" VAL2STR(AU_EVENT_NAME_MAX) "s",
			    trim_buf);
			(void) strncpy(au_event_entry->ae_name, trim_buf,
				AU_EVENT_NAME_MAX);
			s = &s[i+1];

			/* parse event description */
			i = strcspn(s, ":");
			s[i] = '\0';
			(void) strncpy(au_event_entry->ae_desc, s,
				AU_EVENT_DESC_MAX);
			s = &s[i+1];

			/* parse class */
			i = strcspn(s, "\n\0");
			s[i] = '\0';
			(void) sscanf(s, "%" VAL2STR(AU_EVENT_NAME_MAX) "s",
			    trim_buf);
			au_event_entry->ae_class = flagstohex(trim_buf);
			if (cacheauclass_failure == 1) {
				error = 1;
				cacheauclass_failure = 0;
			}

			break;
		}
	}
	_mutex_unlock(&mutex_eventfile);

	if (!error && found) {
		return (au_event_entry);
	} else {
		return ((au_event_ent_t *)0);
	}
}


au_event_ent_t *
#ifdef __STDC__
getauevnam(char *name)
#else
getauevnam(name)
	char *name;
#endif
{
	static au_event_ent_t au_event_entry;
	static char	ename[AU_EVENT_NAME_MAX];
	static char	edesc[AU_EVENT_DESC_MAX];

	/* initialize au_event_entry structure */
	au_event_entry.ae_name = ename;
	au_event_entry.ae_desc = edesc;

	return (getauevnam_r(&au_event_entry, name));
}

au_event_ent_t *
#ifdef __STDC__
getauevnam_r(au_event_ent_t *e, char *name)
#else
getauevnam_r(e, name)
	au_event_ent_t &e;
	char *name;
#endif
{
	setauevent();
	while (getauevent_r(e) != NULL) {
		if (strcmp(e->ae_name, name) == 0) {
			endauevent();
			return (e);
		}
	}
	endauevent();
	return ((au_event_ent_t *)NULL);
}

au_event_ent_t *
#ifdef __STDC__
getauevnum_r(au_event_ent_t *e, au_event_t event_number)
#else
getauevnum_r(e, event_number)
	au_event_ent_t *e;
	au_event_t event_number;
#endif
{
	setauevent();
	while (getauevent_r(e) != NULL) {
		if (e->ae_number == event_number) {
			endauevent();
			return (e);
		}
	}
	endauevent();
	return ((au_event_ent_t *)NULL);
}

au_event_ent_t *
#ifdef __STDC__
getauevnum(au_event_t event_number)
#else
getauevnum(event_number)
	au_event_t event_number;
#endif
{
	static au_event_ent_t e;
	static char	ename[AU_EVENT_NAME_MAX];
	static char	edesc[AU_EVENT_DESC_MAX];

	/* initialize au_event_entry structure */
	e.ae_name = ename;
	e.ae_desc = edesc;

	return (getauevnum_r(&e, event_number));
}

au_event_t
#ifdef __STDC__
getauevnonam(char *event_name)
#else
getauevnonam(event_name)
	char	*event_name;
#endif
{
	au_event_ent_t e;
	char ename[AU_EVENT_NAME_MAX];
	char edesc[AU_EVENT_DESC_MAX];

	/* initialize au_event_entry structure */
	e.ae_name = ename;
	e.ae_desc = edesc;

	if (getauevnam_r(&e, event_name) == (au_event_ent_t *)0) {
		return (-1);
	}
	return (e.ae_number);
}

/*
 * cacheauevent:
 *	Read the entire audit_event file into memory.
 *	Set a pointer to the requested entry in the cache
 *	or a pointer to an invalid entry if the event number
 *	is not known.
 *
 *	Return < 0, if error.
 *	Return   0, if event number not in cache.
 *	Return   1, if event number is in cache.
 */

int
#ifdef __STDC__
cacheauevent(au_event_ent_t **result, au_event_t event_number)
#else
cacheauevent(result, event_number)
	au_event_ent_t **result; /* set this pointer to an entry in the cache */
	au_event_t event_number; /* request this event number */
#endif
{
	static ushort_t	max; /* the highest event number in the file */
	static ushort_t	min; /* the lowest event number in the file */
	static int	invalid; /* 1+index of the highest event number */
	static au_event_ent_t **index_tbl;
	static au_event_ent_t **p_tbl;
	static int	called_once = 0;

	char	line[AU_EVENT_LINE_MAX];
	int	lines = 0;
	FILE	*fp;
	au_event_ent_t *p_event;
	int	i, size;
	int	hit = 0;
	char	*s;

	_mutex_lock(&mutex_eventcache);
	if (called_once == 0) {

		/* Count number of lines in the events file */
		if ((fp = fopen(au_event_fname, "rF")) == NULL) {
			_mutex_unlock(&mutex_eventcache);
			return (-1);
		}
		while (fgets(line, AU_EVENT_LINE_MAX, fp) != NULL) {
			s = line + strspn(line, " \t\r\n");
			if ((*s == '\0') || (*s == '#')) {
				continue;
			}
			lines++;
		}
		(void) fclose(fp);
		size = lines;

		/*
		 * Make an array in which each element in an entry in the
		 * events file.  Make the next to last element an invalid
		 * event.  Make the last element a NULL pointer.
		 */

		p_tbl = calloc(lines + 1, sizeof (au_event_ent_t));
		if (p_tbl == NULL) {
			_mutex_unlock(&mutex_eventcache);
			return (-2);
		}
		lines = 0;
		max = 0;
		min = 65535;
		setauevent();
		while ((p_event = getauevent()) != NULL) {
			p_tbl[lines] = (au_event_ent_t *)
				malloc(sizeof (au_event_ent_t));
			if (p_tbl[lines] == NULL) {
				_mutex_unlock(&mutex_eventcache);
				return (-3);
			}
			p_tbl[lines]->ae_number = p_event->ae_number;
			p_tbl[lines]->ae_name   = strdup(p_event->ae_name);
			p_tbl[lines]->ae_desc   = strdup(p_event->ae_desc);
			p_tbl[lines]->ae_class  = p_event->ae_class;
#ifdef DEBUG2
			printevent(p_tbl[lines]);
#endif
			if ((ushort_t)p_event->ae_number > max) {
				max = p_event->ae_number;
			}
			if ((ushort_t)p_event->ae_number < min) {
				min = p_event->ae_number;
			}
			lines++;
		}
		endauevent();
		invalid = lines;
		p_tbl[invalid] = (au_event_ent_t *)
			malloc(sizeof (au_event_ent_t));
		if (p_tbl[invalid] == NULL) {
			_mutex_unlock(&mutex_eventcache);
			return (-4);
		}
		p_tbl[invalid]->ae_number = -1;
		p_tbl[invalid]->ae_name   = "invalid event number";
		p_tbl[invalid]->ae_desc   = p_tbl[invalid]->ae_name;
		p_tbl[invalid]->ae_class  = (au_class_t)-1;

#ifdef DEBUG2
		for (i = 0; i < size; i++) {
			printf("%d:%s:%s:%d\n", p_tbl[i]->ae_number,
				p_tbl[i]->ae_name, p_tbl[i]->ae_desc,
				p_tbl[i]->ae_class);
		}
#endif

		/* get space for the index_tbl */
		index_tbl = calloc(max+1, sizeof (au_event_ent_t *));
		if (index_tbl == NULL) {
			_mutex_unlock(&mutex_eventcache);
			return (-5);
		}

		/* intialize the index_tbl to the invalid event number */
		for (i = 0; (ushort_t)i < max; i++) {
			index_tbl[i] = p_tbl[invalid];
		}

		/* point each index_tbl element at the corresponding event */
		for (i = 0; i < size; i++) {
			index_tbl[(ushort_t)p_tbl[i]->ae_number] = p_tbl[i];
		}

		called_once = 1;

	}

	if ((ushort_t)event_number > max || (ushort_t)event_number < min) {
		*result = index_tbl[invalid];
	} else {
		*result = index_tbl[(ushort_t)event_number];
		hit = 1;
	}
	_mutex_unlock(&mutex_eventcache);
	return (hit);
}


static au_class_t
flagstohex(char *flags)
{
	au_class_ent_t *p_class;
	au_class_t	hex = 0;
	char	*comma = ",";
	char	*s;
	char	*last;

	s = strtok_r(flags, comma, &last);
	while (s != NULL) {
		if ((cacheauclassnam(&p_class, s)) < 0) {
			cacheauclass_failure = 1;
			return ((au_class_t)-1);
		}
		hex |= p_class->ac_class;
		s = strtok_r(NULL, comma, &last);
	}
	return (hex);
}


#ifdef DEBUG2
void
printevent(p_event)
au_event_ent_t *p_event;
{
	printf("%d:%s:%s:%d\n", p_event->ae_number, p_event->ae_name,
		p_event->ae_desc, p_event->ae_class);
	fflush(stdout);
}


#endif