2 * Copyright (c) 2004 Apple Inc.
3 * Copyright (c) 2006 Robert N. M. Watson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * $P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_class.c#16 $
33 #include <config/config.h>
35 #include <bsm/libbsm.h>
38 #ifdef HAVE_PTHREAD_MUTEX_LOCK
45 #include <compat/strlcpy.h>
49 * Parse the contents of the audit_class file to return struct au_class_ent
52 static FILE *fp = NULL;
53 static char linestr[AU_LINE_MAX];
54 static const char *classdelim = ":";
56 #ifdef HAVE_PTHREAD_MUTEX_LOCK
57 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
61 * Parse a single line from the audit_class file passed in str to the struct
62 * au_class_ent elements; store the result in c.
64 static struct au_class_ent *
65 classfromstr(char *str, struct au_class_ent *c)
67 char *classname, *classdesc, *classflag;
70 /* Each line contains flag:name:desc. */
71 classflag = strtok_r(str, classdelim, &last);
72 classname = strtok_r(NULL, classdelim, &last);
73 classdesc = strtok_r(NULL, classdelim, &last);
75 if ((classflag == NULL) || (classname == NULL) || (classdesc == NULL))
79 * Check for very large classnames.
81 if (strlen(classname) >= AU_CLASS_NAME_MAX)
83 strlcpy(c->ac_name, classname, AU_CLASS_NAME_MAX);
86 * Check for very large class description.
88 if (strlen(classdesc) >= AU_CLASS_DESC_MAX)
90 strlcpy(c->ac_desc, classdesc, AU_CLASS_DESC_MAX);
91 c->ac_class = strtoul(classflag, (char **) NULL, 0);
97 * Return the next au_class_ent structure from the file setauclass should be
98 * called before invoking this function for the first time.
100 * Must be called with mutex held.
102 static struct au_class_ent *
103 getauclassent_r_locked(struct au_class_ent *c)
107 if ((fp == NULL) && ((fp = fopen(AUDIT_CLASS_FILE, "r")) == NULL))
111 * Read until next non-comment line is found, or EOF.
114 if (fgets(linestr, AU_LINE_MAX, fp) == NULL)
118 if (linestr[0] == '#')
121 /* Remove trailing new line character. */
122 if ((nl = strrchr(linestr, '\n')) != NULL)
125 /* Parse tokptr to au_class_ent components. */
127 if (classfromstr(tokptr, c) == NULL)
135 struct au_class_ent *
136 getauclassent_r(struct au_class_ent *c)
138 struct au_class_ent *cp;
140 #ifdef HAVE_PTHREAD_MUTEX_LOCK
141 pthread_mutex_lock(&mutex);
143 cp = getauclassent_r_locked(c);
144 #ifdef HAVE_PTHREAD_MUTEX_LOCK
145 pthread_mutex_unlock(&mutex);
150 struct au_class_ent *
153 static char class_ent_name[AU_CLASS_NAME_MAX];
154 static char class_ent_desc[AU_CLASS_DESC_MAX];
155 static struct au_class_ent c, *cp;
157 bzero(&c, sizeof(c));
158 bzero(class_ent_name, sizeof(class_ent_name));
159 bzero(class_ent_desc, sizeof(class_ent_desc));
160 c.ac_name = class_ent_name;
161 c.ac_desc = class_ent_desc;
163 #ifdef HAVE_PTHREAD_MUTEX_LOCK
164 pthread_mutex_lock(&mutex);
166 cp = getauclassent_r_locked(&c);
167 #ifdef HAVE_PTHREAD_MUTEX_LOCK
168 pthread_mutex_unlock(&mutex);
174 * Rewind to the beginning of the enumeration.
176 * Must be called with mutex held.
179 setauclass_locked(void)
183 fseek(fp, 0, SEEK_SET);
190 #ifdef HAVE_PTHREAD_MUTEX_LOCK
191 pthread_mutex_lock(&mutex);
194 #ifdef HAVE_PTHREAD_MUTEX_LOCK
195 pthread_mutex_unlock(&mutex);
200 * Return the next au_class_entry having the given class name.
202 struct au_class_ent *
203 getauclassnam_r(struct au_class_ent *c, const char *name)
205 struct au_class_ent *cp;
210 #ifdef HAVE_PTHREAD_MUTEX_LOCK
211 pthread_mutex_lock(&mutex);
214 while ((cp = getauclassent_r_locked(c)) != NULL) {
215 if (strcmp(name, cp->ac_name) == 0) {
216 #ifdef HAVE_PTHREAD_MUTEX_LOCK
217 pthread_mutex_unlock(&mutex);
222 #ifdef HAVE_PTHREAD_MUTEX_LOCK
223 pthread_mutex_unlock(&mutex);
228 struct au_class_ent *
229 getauclassnam(const char *name)
231 static char class_ent_name[AU_CLASS_NAME_MAX];
232 static char class_ent_desc[AU_CLASS_DESC_MAX];
233 static struct au_class_ent c;
235 bzero(&c, sizeof(c));
236 bzero(class_ent_name, sizeof(class_ent_name));
237 bzero(class_ent_desc, sizeof(class_ent_desc));
238 c.ac_name = class_ent_name;
239 c.ac_desc = class_ent_desc;
241 return (getauclassnam_r(&c, name));
246 * Return the next au_class_entry having the given class number.
250 struct au_class_ent *
251 getauclassnum_r(struct au_class_ent *c, au_class_t class_number)
253 struct au_class_ent *cp;
255 #ifdef HAVE_PTHREAD_MUTEX_LOCK
256 pthread_mutex_lock(&mutex);
259 while ((cp = getauclassent_r_locked(c)) != NULL) {
260 if (class_number == cp->ac_class)
263 #ifdef HAVE_PTHREAD_MUTEX_LOCK
264 pthread_mutex_unlock(&mutex);
269 struct au_class_ent *
270 getauclassnum(au_class_t class_number)
272 static char class_ent_name[AU_CLASS_NAME_MAX];
273 static char class_ent_desc[AU_CLASS_DESC_MAX];
274 static struct au_class_ent c;
276 bzero(&c, sizeof(c));
277 bzero(class_ent_name, sizeof(class_ent_name));
278 bzero(class_ent_desc, sizeof(class_ent_desc));
279 c.ac_name = class_ent_name;
280 c.ac_desc = class_ent_desc;
282 return (getauclassnum_r(&c, class_number));
286 * audit_class processing is complete; close any open files.
292 #ifdef HAVE_PTHREAD_MUTEX_LOCK
293 pthread_mutex_lock(&mutex);
299 #ifdef HAVE_PTHREAD_MUTEX_LOCK
300 pthread_mutex_unlock(&mutex);