xref: /onnv-gate/usr/src/lib/libbsm/common/au_preselect.c (revision 8743:05a024aa481c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54893Stz204579  * Common Development and Distribution License (the "License").
64893Stz204579  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
23*8743SMarek.Pospisil@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
244893Stz204579  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * au_preselect.c
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <bsm/audit.h>
350Sstevel@tonic-gate #include <bsm/libbsm.h>
360Sstevel@tonic-gate #include <synch.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define	ALLOC_INIT (600)	/* initially allocate ALLOC_INIT map entries */
390Sstevel@tonic-gate #define	ALLOC_INCR (100)	/* if more map entries are needed, realloc */
400Sstevel@tonic-gate 				/* in ALLOC_INCR increments */
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static int alloc_map();
430Sstevel@tonic-gate static int load_map();
440Sstevel@tonic-gate static int realloc_map();
450Sstevel@tonic-gate 
460Sstevel@tonic-gate typedef struct event_map {
470Sstevel@tonic-gate 	au_event_t event;	/* audit event number */
480Sstevel@tonic-gate 	au_class_t class;	/* audit event class mask */
490Sstevel@tonic-gate } event_map_t;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static event_map_t *event_map;	/* the map */
524893Stz204579 static uint_t alloc_count;	/* number of entries currently allocated */
534893Stz204579 static uint_t event_count;	/* number of entries in map */
540Sstevel@tonic-gate static mutex_t mutex_au_preselect = DEFAULTMUTEX;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * au_preselect:
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  * Keep a dynamic array of event<-->class mappings.
60*8743SMarek.Pospisil@Sun.COM  * Refresh the map when the value of flag is AU_PRS_REREAD.
610Sstevel@tonic-gate  * Return:
620Sstevel@tonic-gate  *	1: The event is preselected.
630Sstevel@tonic-gate  *	0: The event is not preselected.
640Sstevel@tonic-gate  *	-1: There was an error:
650Sstevel@tonic-gate  *		Couldn't allocate memory.
660Sstevel@tonic-gate  *		Couldn't find event.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate int
au_preselect(au_event_t au_event,au_mask_t * au_mask_p,int sorf,int flag)690Sstevel@tonic-gate au_preselect(au_event_t au_event, au_mask_t *au_mask_p, int sorf, int flag)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	static char been_here_before;  /* we cache the map */
720Sstevel@tonic-gate 	register int i;
730Sstevel@tonic-gate 	register au_class_t comp_class;
740Sstevel@tonic-gate 
756812Sraf 	(void) mutex_lock(&mutex_au_preselect);
760Sstevel@tonic-gate 	if (!been_here_before) {
770Sstevel@tonic-gate 		if (alloc_map() == -1) {
786812Sraf 			(void) mutex_unlock(&mutex_au_preselect);
790Sstevel@tonic-gate 			return (-1);
800Sstevel@tonic-gate 		}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 		if (load_map() == -1) {
836812Sraf 			(void) mutex_unlock(&mutex_au_preselect);
840Sstevel@tonic-gate 			return (-1);
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 		been_here_before = 1;
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	/*
910Sstevel@tonic-gate 	 * Don't use the cache. Re-read the audit_event(5) db every time
920Sstevel@tonic-gate 	 */
930Sstevel@tonic-gate 	if (flag == AU_PRS_REREAD) {
940Sstevel@tonic-gate 		if (load_map() == -1) {
956812Sraf 			(void) mutex_unlock(&mutex_au_preselect);
960Sstevel@tonic-gate 			return (-1);
970Sstevel@tonic-gate 		}
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	/* Determine what portion of the preselection mask to check. */
1010Sstevel@tonic-gate 	if (sorf == AU_PRS_SUCCESS)
1020Sstevel@tonic-gate 		comp_class = au_mask_p->am_success;
1030Sstevel@tonic-gate 	else if (sorf == AU_PRS_FAILURE)
1040Sstevel@tonic-gate 		comp_class = au_mask_p->am_failure;
1050Sstevel@tonic-gate 	else
1060Sstevel@tonic-gate 		comp_class = au_mask_p->am_success | au_mask_p->am_failure;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	for (i = 0; i < event_count; i++) {
1090Sstevel@tonic-gate 		if (event_map[i].event == au_event) {
1100Sstevel@tonic-gate 			if (event_map[i].class & comp_class) {
1116812Sraf 				(void) mutex_unlock(&mutex_au_preselect);
1120Sstevel@tonic-gate 				return (1);
1130Sstevel@tonic-gate 			} else {
1146812Sraf 				(void) mutex_unlock(&mutex_au_preselect);
1150Sstevel@tonic-gate 				return (0);
1160Sstevel@tonic-gate 			}
1170Sstevel@tonic-gate 		}
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 
1206812Sraf 	(void) mutex_unlock(&mutex_au_preselect);
1210Sstevel@tonic-gate 	return (-1);	/* could not find event in the table */
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * Initially allocate about as many map entries as are there
1260Sstevel@tonic-gate  * are audit events shipped with the system. For sites
1270Sstevel@tonic-gate  * that don't add audit events, this should be enough.
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate static int
alloc_map()1300Sstevel@tonic-gate alloc_map()
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	if ((event_map = (event_map_t *)
1334893Stz204579 	    calloc(ALLOC_INIT, (size_t)sizeof (event_map_t))) ==
1344893Stz204579 	    (event_map_t *)NULL)
1350Sstevel@tonic-gate 		return (-1);
1360Sstevel@tonic-gate 	else
1370Sstevel@tonic-gate 		alloc_count = ALLOC_INIT;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	return (0);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate  * load the event<->class map into memory
1440Sstevel@tonic-gate  */
1450Sstevel@tonic-gate static int
load_map()1460Sstevel@tonic-gate load_map()
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	register au_event_ent_t *evp;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	event_count = 0;
1510Sstevel@tonic-gate 	setauevent();
1520Sstevel@tonic-gate 	while ((evp = getauevent()) != (au_event_ent_t *)NULL) {
1530Sstevel@tonic-gate 		if (event_count > alloc_count)
1540Sstevel@tonic-gate 			if (realloc_map() == -1) {
1550Sstevel@tonic-gate 				endauevent();
1560Sstevel@tonic-gate 				return (-1);
1570Sstevel@tonic-gate 			}
1580Sstevel@tonic-gate 		event_map[event_count].event = evp->ae_number;
1590Sstevel@tonic-gate 		event_map[event_count].class = evp->ae_class;
1600Sstevel@tonic-gate 		++event_count;
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 	endauevent();
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	return (0);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * realloc the event map in ALLOC_INCR increments
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate static int
realloc_map()1710Sstevel@tonic-gate realloc_map()
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	register size_t rsize;
1740Sstevel@tonic-gate 	rsize = sizeof (event_map_t) * (alloc_count + ALLOC_INCR);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	if ((event_map = (event_map_t *)
1774893Stz204579 	    realloc(event_map, rsize)) == (event_map_t *)NULL)
1780Sstevel@tonic-gate 		return (-1);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	return (0);
1810Sstevel@tonic-gate }
182