17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5d75d0dc9Stz204579 * Common Development and Distribution License (the "License"). 6d75d0dc9Stz204579 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate /* 23*5707ed5dSMarek Pospisil * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24d75d0dc9Stz204579 * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * au_preselect.c 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <sys/types.h> 327c478bd9Sstevel@tonic-gate #include <stdio.h> 337c478bd9Sstevel@tonic-gate #include <stdlib.h> 347c478bd9Sstevel@tonic-gate #include <bsm/audit.h> 357c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h> 367c478bd9Sstevel@tonic-gate #include <synch.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #define ALLOC_INIT (600) /* initially allocate ALLOC_INIT map entries */ 397c478bd9Sstevel@tonic-gate #define ALLOC_INCR (100) /* if more map entries are needed, realloc */ 407c478bd9Sstevel@tonic-gate /* in ALLOC_INCR increments */ 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate static int alloc_map(); 437c478bd9Sstevel@tonic-gate static int load_map(); 447c478bd9Sstevel@tonic-gate static int realloc_map(); 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate typedef struct event_map { 477c478bd9Sstevel@tonic-gate au_event_t event; /* audit event number */ 487c478bd9Sstevel@tonic-gate au_class_t class; /* audit event class mask */ 497c478bd9Sstevel@tonic-gate } event_map_t; 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate static event_map_t *event_map; /* the map */ 52d75d0dc9Stz204579 static uint_t alloc_count; /* number of entries currently allocated */ 53d75d0dc9Stz204579 static uint_t event_count; /* number of entries in map */ 547c478bd9Sstevel@tonic-gate static mutex_t mutex_au_preselect = DEFAULTMUTEX; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate /* 577c478bd9Sstevel@tonic-gate * au_preselect: 587c478bd9Sstevel@tonic-gate * 597c478bd9Sstevel@tonic-gate * Keep a dynamic array of event<-->class mappings. 60*5707ed5dSMarek Pospisil * Refresh the map when the value of flag is AU_PRS_REREAD. 617c478bd9Sstevel@tonic-gate * Return: 627c478bd9Sstevel@tonic-gate * 1: The event is preselected. 637c478bd9Sstevel@tonic-gate * 0: The event is not preselected. 647c478bd9Sstevel@tonic-gate * -1: There was an error: 657c478bd9Sstevel@tonic-gate * Couldn't allocate memory. 667c478bd9Sstevel@tonic-gate * Couldn't find event. 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate int 697c478bd9Sstevel@tonic-gate au_preselect(au_event_t au_event, au_mask_t *au_mask_p, int sorf, int flag) 707c478bd9Sstevel@tonic-gate { 717c478bd9Sstevel@tonic-gate static char been_here_before; /* we cache the map */ 727c478bd9Sstevel@tonic-gate register int i; 737c478bd9Sstevel@tonic-gate register au_class_t comp_class; 747c478bd9Sstevel@tonic-gate 757257d1b4Sraf (void) mutex_lock(&mutex_au_preselect); 767c478bd9Sstevel@tonic-gate if (!been_here_before) { 777c478bd9Sstevel@tonic-gate if (alloc_map() == -1) { 787257d1b4Sraf (void) mutex_unlock(&mutex_au_preselect); 797c478bd9Sstevel@tonic-gate return (-1); 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate if (load_map() == -1) { 837257d1b4Sraf (void) mutex_unlock(&mutex_au_preselect); 847c478bd9Sstevel@tonic-gate return (-1); 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate been_here_before = 1; 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate * Don't use the cache. Re-read the audit_event(5) db every time 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate if (flag == AU_PRS_REREAD) { 947c478bd9Sstevel@tonic-gate if (load_map() == -1) { 957257d1b4Sraf (void) mutex_unlock(&mutex_au_preselect); 967c478bd9Sstevel@tonic-gate return (-1); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* Determine what portion of the preselection mask to check. */ 1017c478bd9Sstevel@tonic-gate if (sorf == AU_PRS_SUCCESS) 1027c478bd9Sstevel@tonic-gate comp_class = au_mask_p->am_success; 1037c478bd9Sstevel@tonic-gate else if (sorf == AU_PRS_FAILURE) 1047c478bd9Sstevel@tonic-gate comp_class = au_mask_p->am_failure; 1057c478bd9Sstevel@tonic-gate else 1067c478bd9Sstevel@tonic-gate comp_class = au_mask_p->am_success | au_mask_p->am_failure; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate for (i = 0; i < event_count; i++) { 1097c478bd9Sstevel@tonic-gate if (event_map[i].event == au_event) { 1107c478bd9Sstevel@tonic-gate if (event_map[i].class & comp_class) { 1117257d1b4Sraf (void) mutex_unlock(&mutex_au_preselect); 1127c478bd9Sstevel@tonic-gate return (1); 1137c478bd9Sstevel@tonic-gate } else { 1147257d1b4Sraf (void) mutex_unlock(&mutex_au_preselect); 1157c478bd9Sstevel@tonic-gate return (0); 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate 1207257d1b4Sraf (void) mutex_unlock(&mutex_au_preselect); 1217c478bd9Sstevel@tonic-gate return (-1); /* could not find event in the table */ 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate * Initially allocate about as many map entries as are there 1267c478bd9Sstevel@tonic-gate * are audit events shipped with the system. For sites 1277c478bd9Sstevel@tonic-gate * that don't add audit events, this should be enough. 1287c478bd9Sstevel@tonic-gate */ 1297c478bd9Sstevel@tonic-gate static int 1307c478bd9Sstevel@tonic-gate alloc_map() 1317c478bd9Sstevel@tonic-gate { 1327c478bd9Sstevel@tonic-gate if ((event_map = (event_map_t *) 1337c478bd9Sstevel@tonic-gate calloc(ALLOC_INIT, (size_t)sizeof (event_map_t))) == 1347c478bd9Sstevel@tonic-gate (event_map_t *)NULL) 1357c478bd9Sstevel@tonic-gate return (-1); 1367c478bd9Sstevel@tonic-gate else 1377c478bd9Sstevel@tonic-gate alloc_count = ALLOC_INIT; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate return (0); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate /* 1437c478bd9Sstevel@tonic-gate * load the event<->class map into memory 1447c478bd9Sstevel@tonic-gate */ 1457c478bd9Sstevel@tonic-gate static int 1467c478bd9Sstevel@tonic-gate load_map() 1477c478bd9Sstevel@tonic-gate { 1487c478bd9Sstevel@tonic-gate register au_event_ent_t *evp; 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate event_count = 0; 1517c478bd9Sstevel@tonic-gate setauevent(); 1527c478bd9Sstevel@tonic-gate while ((evp = getauevent()) != (au_event_ent_t *)NULL) { 1537c478bd9Sstevel@tonic-gate if (event_count > alloc_count) 1547c478bd9Sstevel@tonic-gate if (realloc_map() == -1) { 1557c478bd9Sstevel@tonic-gate endauevent(); 1567c478bd9Sstevel@tonic-gate return (-1); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate event_map[event_count].event = evp->ae_number; 1597c478bd9Sstevel@tonic-gate event_map[event_count].class = evp->ae_class; 1607c478bd9Sstevel@tonic-gate ++event_count; 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate endauevent(); 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate return (0); 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* 1687c478bd9Sstevel@tonic-gate * realloc the event map in ALLOC_INCR increments 1697c478bd9Sstevel@tonic-gate */ 1707c478bd9Sstevel@tonic-gate static int 1717c478bd9Sstevel@tonic-gate realloc_map() 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate register size_t rsize; 1747c478bd9Sstevel@tonic-gate rsize = sizeof (event_map_t) * (alloc_count + ALLOC_INCR); 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate if ((event_map = (event_map_t *) 1777c478bd9Sstevel@tonic-gate realloc(event_map, rsize)) == (event_map_t *)NULL) 1787c478bd9Sstevel@tonic-gate return (-1); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate return (0); 1817c478bd9Sstevel@tonic-gate } 182