2 * Copyright (c) 2004 Apple Computer, 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 Computer, 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#11 $
33 #include <bsm/libbsm.h>
41 * Parse the contents of the audit_class file to return struct au_class_ent
44 static FILE *fp = NULL;
45 static char linestr[AU_LINE_MAX];
46 static const char *classdelim = ":";
48 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
51 * Parse a single line from the audit_class file passed in str to the struct
52 * au_class_ent elements; store the result in c.
54 static struct au_class_ent *
55 classfromstr(char *str, struct au_class_ent *c)
57 char *classname, *classdesc, *classflag;
60 /* Each line contains flag:name:desc. */
61 classflag = strtok_r(str, classdelim, &last);
62 classname = strtok_r(NULL, classdelim, &last);
63 classdesc = strtok_r(NULL, classdelim, &last);
65 if ((classflag == NULL) || (classname == NULL) || (classdesc == NULL))
69 * Check for very large classnames.
71 if (strlen(classname) >= AU_CLASS_NAME_MAX)
74 strcpy(c->ac_name, classname);
77 * Check for very large class description.
79 if (strlen(classdesc) >= AU_CLASS_DESC_MAX)
81 strcpy(c->ac_desc, classdesc);
82 c->ac_class = strtoul(classflag, (char **) NULL, 0);
88 * Return the next au_class_ent structure from the file setauclass should be
89 * called before invoking this function for the first time.
91 * Must be called with mutex held.
93 static struct au_class_ent *
94 getauclassent_r_locked(struct au_class_ent *c)
98 if ((fp == NULL) && ((fp = fopen(AUDIT_CLASS_FILE, "r")) == NULL))
102 * Read until next non-comment line is found, or EOF.
105 if (fgets(linestr, AU_LINE_MAX, fp) == NULL)
109 if (linestr[0] == '#')
112 /* Remove trailing new line character. */
113 if ((nl = strrchr(linestr, '\n')) != NULL)
116 /* Parse tokptr to au_class_ent components. */
118 if (classfromstr(tokptr, c) == NULL)
126 struct au_class_ent *
127 getauclassent_r(struct au_class_ent *c)
129 struct au_class_ent *cp;
131 pthread_mutex_lock(&mutex);
132 cp = getauclassent_r_locked(c);
133 pthread_mutex_unlock(&mutex);
137 struct au_class_ent *
140 static char class_ent_name[AU_CLASS_NAME_MAX];
141 static char class_ent_desc[AU_CLASS_DESC_MAX];
142 static struct au_class_ent c, *cp;
144 bzero(&c, sizeof(c));
145 bzero(class_ent_name, sizeof(class_ent_name));
146 bzero(class_ent_desc, sizeof(class_ent_desc));
147 c.ac_name = class_ent_name;
148 c.ac_desc = class_ent_desc;
150 pthread_mutex_lock(&mutex);
151 cp = getauclassent_r_locked(&c);
152 pthread_mutex_unlock(&mutex);
157 * Rewind to the beginning of the enumeration.
159 * Must be called with mutex held.
162 setauclass_locked(void)
166 fseek(fp, 0, SEEK_SET);
173 pthread_mutex_lock(&mutex);
175 pthread_mutex_unlock(&mutex);
179 * Return the next au_class_entry having the given class name.
181 struct au_class_ent *
182 getauclassnam_r(struct au_class_ent *c, const char *name)
184 struct au_class_ent *cp;
189 pthread_mutex_lock(&mutex);
191 while ((cp = getauclassent_r_locked(c)) != NULL) {
192 if (strcmp(name, cp->ac_name) == 0) {
193 pthread_mutex_unlock(&mutex);
197 pthread_mutex_unlock(&mutex);
201 struct au_class_ent *
202 getauclassnam(const char *name)
204 static char class_ent_name[AU_CLASS_NAME_MAX];
205 static char class_ent_desc[AU_CLASS_DESC_MAX];
206 static struct au_class_ent c;
208 bzero(&c, sizeof(c));
209 bzero(class_ent_name, sizeof(class_ent_name));
210 bzero(class_ent_desc, sizeof(class_ent_desc));
211 c.ac_name = class_ent_name;
212 c.ac_desc = class_ent_desc;
214 return (getauclassnam_r(&c, name));
219 * Return the next au_class_entry having the given class number.
223 struct au_class_ent *
224 getauclassnum_r(struct au_class_ent *c, au_class_t class_number)
226 struct au_class_ent *cp;
228 pthread_mutex_lock(&mutex);
230 while ((cp = getauclassent_r_locked(c)) != NULL) {
231 if (class_number == cp->ac_class)
234 pthread_mutex_unlock(&mutex);
238 struct au_class_ent *
239 getauclassnum(au_class_t class_number)
241 static char class_ent_name[AU_CLASS_NAME_MAX];
242 static char class_ent_desc[AU_CLASS_DESC_MAX];
243 static struct au_class_ent c;
245 bzero(&c, sizeof(c));
246 bzero(class_ent_name, sizeof(class_ent_name));
247 bzero(class_ent_desc, sizeof(class_ent_desc));
248 c.ac_name = class_ent_name;
249 c.ac_desc = class_ent_desc;
251 return (getauclassnum_r(&c, class_number));
255 * audit_class processing is complete; close any open files.
261 pthread_mutex_lock(&mutex);
266 pthread_mutex_unlock(&mutex);