audump.c revision 155518
1/*-
2 * Copyright (c) 2005 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $P4: //depot/projects/trustedbsd/openbsm/tools/audump.c#5 $
27 */
28
29#include <bsm/libbsm.h>
30#include <string.h>
31#include <err.h>
32#include <limits.h>
33#include <stdio.h>
34#include <stdlib.h>
35
36/*
37 * Simple tool to dump various /etc/security databases using the defined APIs.
38 */
39
40static void
41usage(void)
42{
43
44	fprintf(stderr, "usage: audump [class|class_r|control|event|event_r|"
45	    "user|user_r]\n");
46	exit(-1);
47}
48
49static void
50audump_class(void)
51{
52	au_class_ent_t *cp;
53
54	while ((cp = getauclassent()) != NULL)
55		printf("0x%08x:%s:%s\n", cp->ac_class, cp->ac_name,
56		    cp->ac_desc);
57}
58
59static void
60audump_class_r(void)
61{
62	char class_ent_name[AU_CLASS_NAME_MAX];
63	char class_ent_desc[AU_CLASS_DESC_MAX];
64	au_class_ent_t c, *cp;
65
66	bzero(&c, sizeof(c));
67	bzero(class_ent_name, sizeof(class_ent_name));
68	bzero(class_ent_desc, sizeof(class_ent_desc));
69	c.ac_name = class_ent_name;
70	c.ac_desc = class_ent_desc;
71
72	while ((cp = getauclassent_r(&c)) != NULL)
73		printf("0x%08x:%s:%s\n", cp->ac_class, cp->ac_name,
74		    cp->ac_desc);
75}
76
77static void
78audump_control(void)
79{
80	char string[PATH_MAX];
81	int ret, val;
82
83	ret = getacflg(string, PATH_MAX);
84	if (ret == -2)
85		err(-1, "getacflg");
86	if (ret != 0)
87		errx(-1, "getacflg: %d", ret);
88
89	printf("flags:%s\n", string);
90
91	ret = getacmin(&val);
92	if (ret == -2)
93		err(-1, "getacmin");
94	if (ret != 0)
95		errx(-1, "getacmin: %d", ret);
96
97	printf("min:%d\n", val);
98
99	ret = getacna(string, PATH_MAX);
100	if (ret == -2)
101		err(-1, "getacna");
102	if (ret != 0)
103		errx(-1, "getacna: %d", ret);
104
105	printf("naflags:%s\n", string);
106
107	setac();
108	do {
109		ret = getacdir(string, PATH_MAX);
110		if (ret == -1)
111			break;
112		if (ret == -2)
113			err(-1, "getacdir");
114		if (ret != 0)
115			errx(-1, "getacdir: %d", ret);
116		printf("dir:%s\n", string);
117
118	} while (ret == 0);
119}
120
121static void
122printf_classmask(au_class_t classmask)
123{
124	au_class_ent_t *c;
125	u_int32_t i;
126	int first;
127
128	first = 1;
129	for (i = 0; i < 32; i++) {
130		if (classmask & (2 << i)) {
131			if (first)
132				first = 0;
133			else
134				printf(",");
135			c = getauclassnum(2 << i);
136			if (c != NULL)
137				printf("%s", c->ac_name);
138			else
139				printf("0x%x", 2 << i);
140		}
141	}
142}
143
144static void
145audump_event(void)
146{
147	au_event_ent_t *ep;
148
149	while ((ep = getauevent()) != NULL) {
150		printf("%d:%s:%s:", ep->ae_number, ep->ae_name, ep->ae_desc);
151		printf_classmask(ep->ae_class);
152		printf("\n");
153	}
154}
155
156static void
157audump_event_r(void)
158{
159	char event_ent_name[AU_EVENT_NAME_MAX];
160	char event_ent_desc[AU_EVENT_DESC_MAX];
161	au_event_ent_t e, *ep;
162
163	bzero(&e, sizeof(e));
164	bzero(event_ent_name, sizeof(event_ent_name));
165	bzero(event_ent_desc, sizeof(event_ent_desc));
166	e.ae_name = event_ent_name;
167	e.ae_desc = event_ent_desc;
168
169	while ((ep = getauevent_r(&e)) != NULL) {
170		printf("%d:%s:%s:", ep->ae_number, ep->ae_name, ep->ae_desc);
171		printf_classmask(ep->ae_class);
172		printf("\n");
173	}
174}
175
176static void
177audump_user(void)
178{
179	au_user_ent_t *up;
180
181	while ((up = getauuserent()) != NULL) {
182		printf("%s:", up->au_name);
183		// printf_classmask(up->au_always);
184		printf(":");
185		// printf_classmask(up->au_never);
186		printf("\n");
187	}
188}
189
190static void
191audump_user_r(void)
192{
193	char user_ent_name[AU_USER_NAME_MAX];
194	au_user_ent_t u, *up;
195
196	bzero(&u, sizeof(u));
197	bzero(user_ent_name, sizeof(user_ent_name));
198	u.au_name = user_ent_name;
199
200	while ((up = getauuserent_r(&u)) != NULL) {
201		printf("%s:", up->au_name);
202		// printf_classmask(up->au_always);
203		printf(":");
204		// printf_classmask(up->au_never);
205		printf("\n");
206	}
207}
208
209int
210main(int argc, char *argv[])
211{
212
213	if (argc != 2)
214		usage();
215
216	if (strcmp(argv[1], "class") == 0)
217		audump_class();
218	else if (strcmp(argv[1], "class_r") == 0)
219		audump_class_r();
220	else if (strcmp(argv[1], "control") == 0)
221		audump_control();
222	else if (strcmp(argv[1], "event") == 0)
223		audump_event();
224	else if (strcmp(argv[1], "event_r") == 0)
225		audump_event_r();
226	else if (strcmp(argv[1], "user") == 0)
227		audump_user();
228	else if (strcmp(argv[1], "user_r") == 0)
229		audump_user_r();
230	else
231		usage();
232
233	return (0);
234}
235