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
|
/*
* 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
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Interfaces to audit_class(5) (/etc/security/audit_class)
*/
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <bsm/audit.h>
#include <bsm/libbsm.h>
#include <string.h>
#include <synch.h>
static char au_class_fname[PATH_MAX] = AUDITCLASSFILE;
static FILE *au_class_file = NULL;
static mutex_t mutex_classfile = DEFAULTMUTEX;
static mutex_t mutex_classcache = DEFAULTMUTEX;
void
setauclass()
{
(void) mutex_lock(&mutex_classfile);
if (au_class_file) {
(void) fseek(au_class_file, 0L, 0);
}
(void) mutex_unlock(&mutex_classfile);
}
void
endauclass()
{
(void) mutex_lock(&mutex_classfile);
if (au_class_file) {
(void) fclose(au_class_file);
au_class_file = NULL;
}
(void) mutex_unlock(&mutex_classfile);
}
/*
* getauclassent():
* This is not MT-safe because of the static variables.
*/
au_class_ent_t *
getauclassent()
{
static au_class_ent_t e;
static char cname[AU_CLASS_NAME_MAX];
static char cdesc[AU_CLASS_DESC_MAX];
e.ac_name = cname;
e.ac_desc = cdesc;
return (getauclassent_r(&e));
}
/*
* getauclassent_r
* This is MT-safe if each thread passes in its own pointer
* to the space where the class entry is returned. Becareful
* to also allocate space from the cname and cdesc pointers
* in the au_class_ent structure.
*/
au_class_ent_t *
getauclassent_r(au_class_entry)
au_class_ent_t *au_class_entry;
{
int i, error = 0, found = 0;
char *s, input[256];
unsigned long v;
if (au_class_entry == (au_class_ent_t *)NULL ||
au_class_entry->ac_name == (char *)NULL ||
au_class_entry->ac_desc == (char *)NULL) {
return ((au_class_ent_t *)NULL);
}
/* open audit class file if it isn't already */
(void) mutex_lock(&mutex_classfile);
if (!au_class_file) {
if (!(au_class_file = fopen(au_class_fname, "rF"))) {
(void) mutex_unlock(&mutex_classfile);
return ((au_class_ent_t *)0);
}
}
while (fgets(input, 256, au_class_file)) {
if (input[0] != '#') {
s = input + strspn(input, " \t\r\n");
if ((*s == '\0') || (*s == '#')) {
continue;
}
found = 1;
s = input;
/* parse bitfield */
i = strcspn(s, ":");
s[i] = '\0';
if (strncmp(s, "0x", 2) == 0) {
(void) sscanf(&s[2], "%lx", &v);
} else {
(void) sscanf(s, "%lu", &v);
}
au_class_entry->ac_class = v;
s = &s[i+1];
/* parse class name */
i = strcspn(s, ":");
s[i] = '\0';
(void) strncpy(au_class_entry->ac_name, s,
AU_CLASS_NAME_MAX);
s = &s[i+1];
/* parse class description */
i = strcspn(s, "\n\0");
s[i] = '\0';
(void) strncpy(au_class_entry->ac_desc, s,
AU_CLASS_DESC_MAX);
break;
}
}
(void) mutex_unlock(&mutex_classfile);
if (!error && found) {
return (au_class_entry);
} else {
return ((au_class_ent_t *)0);
}
}
au_class_ent_t *
#ifdef __STDC__
getauclassnam(char *name)
#else
getauclassnam(name)
char *name;
#endif
{
static au_class_ent_t e;
static char cname[AU_CLASS_NAME_MAX];
static char cdesc[AU_CLASS_DESC_MAX];
e.ac_name = cname;
e.ac_desc = cdesc;
return (getauclassnam_r(&e, name));
}
au_class_ent_t *
#ifdef __STDC__
getauclassnam_r(au_class_ent_t *e, char *name)
#else
getauclassnam_r()
au_class_ent_t *e;
char *name;
#endif
{
while (getauclassent_r(e) != NULL) {
if (strcmp(e->ac_name, name) == 0) {
return (e);
}
}
return ((au_class_ent_t *)NULL);
}
/*
* xcacheauclass:
* Read the entire audit_class file into memory.
* Return a pointer to the requested entry in the cache
* or a pointer to an invalid entry if the the class
* requested is not known.
*
* Return < 0, do not set result pointer, if error.
* Return 0, set result pointer to invalid entry, if class not in cache.
* Return 1, set result pointer to a valid entry, if class is in cache.
*/
static int
xcacheauclass(result, class_name, class_no, flags)
au_class_ent_t **result; /* set this pointer to an entry in the cache */
char *class_name; /* name of class to look up */
au_class_t class_no;
int flags;
{
static int invalid;
static au_class_ent_t **class_tbl;
static int called_once;
static int lines = 0;
char line[256];
FILE *fp;
au_class_ent_t *p_class;
int i;
int hit = 0;
char *s;
(void) mutex_lock(&mutex_classcache);
if (called_once == 0) {
/* Count number of lines in the class file */
if ((fp = fopen(au_class_fname, "rF")) == NULL) {
(void) mutex_unlock(&mutex_classcache);
return (-1);
}
while (fgets(line, 256, fp) != NULL) {
s = line + strspn(line, " \t\r\n");
if ((*s == '\0') || (*s == '#')) {
continue;
}
lines++;
}
(void) fclose(fp);
class_tbl = (au_class_ent_t **)calloc((size_t)lines + 1,
sizeof (au_class_ent_t));
if (class_tbl == NULL) {
(void) mutex_unlock(&mutex_classcache);
return (-2);
}
lines = 0;
setauclass();
/*
* This call to getauclassent is protected by
* mutex_classcache, so we don't need to use the thread-
* safe version (getauclassent_r).
*/
while ((p_class = getauclassent()) != NULL) {
class_tbl[lines] = (au_class_ent_t *)
malloc(sizeof (au_class_ent_t));
if (class_tbl[lines] == NULL) {
(void) mutex_unlock(&mutex_classcache);
return (-3);
}
class_tbl[lines]->ac_name = strdup(p_class->ac_name);
class_tbl[lines]->ac_class = p_class->ac_class;
class_tbl[lines]->ac_desc = strdup(p_class->ac_desc);
#ifdef DEBUG2
printclass(class_tbl[lines]);
#endif
lines++;
}
endauclass();
invalid = lines;
class_tbl[invalid] = (au_class_ent_t *)
malloc(sizeof (au_class_ent_t));
if (class_tbl[invalid] == NULL) {
(void) mutex_unlock(&mutex_classcache);
return (-4);
}
class_tbl[invalid]->ac_name = "invalid class";
class_tbl[invalid]->ac_class = 0;
class_tbl[invalid]->ac_desc = class_tbl[invalid]->ac_name;
called_once = 1;
#ifdef DEBUG2
for (i = 0; i <= lines; i++) {
printclass(class_tbl[i]);
}
#endif
} /* END if called_once */
*result = class_tbl[invalid];
if (flags & AU_CACHE_NAME) {
for (i = 0; i < lines; i++) {
if (strcmp(class_name, class_tbl[i]->ac_name) == 0) {
*result = class_tbl[i];
hit = 1;
break;
}
}
} else if (flags & AU_CACHE_NUMBER) {
for (i = 0; i < lines; i++) {
if (class_no == class_tbl[i]->ac_class) {
*result = class_tbl[i];
hit = 1;
break;
}
}
}
(void) mutex_unlock(&mutex_classcache);
return (hit);
}
int
#ifdef __STDC__
cacheauclass(au_class_ent_t **result, au_class_t class_no)
#else
cacheauclass(result, class_no)
au_class_ent_t **result; /* set this pointer to an entry in the cache */
au_class_t class_no;
#endif
{
return (xcacheauclass(result, "", class_no, AU_CACHE_NUMBER));
}
int
#ifdef __STDC__
cacheauclassnam(au_class_ent_t **result, char *class_name)
#else
cacheauclassnam(result, class_name)
au_class_ent_t **result; /* set this pointer to an entry in the cache */
char *class_name;
#endif
{
return (xcacheauclass(result, class_name, (au_class_t)0,
AU_CACHE_NAME));
}
#ifdef DEBUG2
void
printclass(p_c)
au_class_ent_t *p_c;
{
printf("%x:%s:%s\n", p_c->ac_class, p_c->ac_name, p_c->ac_desc);
fflush(stdout);
}
#endif
|