audump.c revision 293161
1/*-
2 * Copyright (c) 2005-2009 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
27#include <bsm/libbsm.h>
28#include <string.h>
29#include <err.h>
30#include <limits.h>
31#include <stdio.h>
32#include <stdlib.h>
33
34/*
35 * Simple tool to dump various /etc/security databases using the defined APIs.
36 */
37
38static void
39usage(void)
40{
41
42	fprintf(stderr, "usage: audump [class|class_r|control|event|event_r|"
43	    "user|user_r]\n");
44	exit(-1);
45}
46
47static void
48audump_class(void)
49{
50	au_class_ent_t *cp;
51
52	while ((cp = getauclassent()) != NULL)
53		printf("0x%08x:%s:%s\n", cp->ac_class, cp->ac_name,
54		    cp->ac_desc);
55}
56
57static void
58audump_class_r(void)
59{
60	char class_ent_name[AU_CLASS_NAME_MAX];
61	char class_ent_desc[AU_CLASS_DESC_MAX];
62	au_class_ent_t c, *cp;
63
64	bzero(&c, sizeof(c));
65	bzero(class_ent_name, sizeof(class_ent_name));
66	bzero(class_ent_desc, sizeof(class_ent_desc));
67	c.ac_name = class_ent_name;
68	c.ac_desc = class_ent_desc;
69
70	while ((cp = getauclassent_r(&c)) != NULL)
71		printf("0x%08x:%s:%s\n", cp->ac_class, cp->ac_name,
72		    cp->ac_desc);
73}
74
75static void
76audump_control(void)
77{
78	char string[PATH_MAX], string2[PATH_MAX];
79	int ret, val;
80	long policy;
81	time_t age;
82	size_t size;
83
84	ret = getacflg(string, PATH_MAX);
85	if (ret == -2)
86		err(-1, "getacflg");
87	if (ret != 0)
88		errx(-1, "getacflg: %d", ret);
89
90	printf("flags:%s\n", string);
91
92	ret = getacmin(&val);
93	if (ret == -2)
94		err(-1, "getacmin");
95	if (ret != 0)
96		errx(-1, "getacmin: %d", ret);
97
98	printf("min:%d\n", val);
99
100	ret = getacna(string, PATH_MAX);
101	if (ret == -2)
102		err(-1, "getacna");
103	if (ret != 0)
104		errx(-1, "getacna: %d", ret);
105
106	printf("naflags:%s\n", string);
107
108	setac();
109	do {
110		ret = getacdir(string, PATH_MAX);
111		if (ret == -1)
112			break;
113		if (ret == -2)
114			err(-1, "getacdir");
115		if (ret != 0)
116			errx(-1, "getacdir: %d", ret);
117		printf("dir:%s\n", string);
118
119	} while (ret == 0);
120
121	ret = getacpol(string, PATH_MAX);
122	if (ret != 0)
123		err(-1, "getacpol");
124	if (au_strtopol(string, &policy) < 0)
125		err(-1, "au_strtopol");
126	if (au_poltostr(policy, PATH_MAX, string2) < 0)
127		err(-1, "au_poltostr");
128	printf("policy:%s\n", string2);
129
130	ret = getacfilesz(&size);
131	if (ret == -2)
132		err(-1, "getacfilesz");
133	if (ret != 0)
134		err(-1, "getacfilesz: %d", ret);
135
136	printf("filesz:%ldB\n", size);
137
138
139	ret = getachost(string, PATH_MAX);
140	if (ret == -2)
141		err(-1, "getachost");
142	if (ret == -3)
143		err(-1, "getachost: %d", ret);
144	if (ret == 0 && ret != 1)
145		printf("host:%s\n", string);
146
147	ret = getacexpire(&val, &age, &size);
148	if (ret == -2)
149		err(-1, "getacexpire");
150	if (ret == -1)
151		err(-1, "getacexpire: %d", ret);
152	if (ret == 0 && ret != 1)
153		printf("expire-after:%ldB  %s %lds\n", size,
154		    val ? "AND" : "OR", age);
155}
156
157static void
158printf_classmask(au_class_t classmask)
159{
160	au_class_ent_t *c;
161	u_int32_t i;
162	int first;
163
164	first = 1;
165	for (i = 0; i < 32; i++) {
166		if (classmask & (1 << i)) {
167			if (first)
168				first = 0;
169			else
170				printf(",");
171			c = getauclassnum(1 << i);
172			if (c != NULL)
173				printf("%s", c->ac_name);
174			else
175				printf("0x%x", 1 << i);
176		}
177	}
178}
179
180static void
181audump_event(void)
182{
183	au_event_ent_t *ep;
184
185	while ((ep = getauevent()) != NULL) {
186		printf("%d:%s:%s:", ep->ae_number, ep->ae_name, ep->ae_desc);
187		printf_classmask(ep->ae_class);
188		printf("\n");
189	}
190}
191
192static void
193audump_event_r(void)
194{
195	char event_ent_name[AU_EVENT_NAME_MAX];
196	char event_ent_desc[AU_EVENT_DESC_MAX];
197	au_event_ent_t e, *ep;
198
199	bzero(&e, sizeof(e));
200	bzero(event_ent_name, sizeof(event_ent_name));
201	bzero(event_ent_desc, sizeof(event_ent_desc));
202	e.ae_name = event_ent_name;
203	e.ae_desc = event_ent_desc;
204
205	while ((ep = getauevent_r(&e)) != NULL) {
206		printf("%d:%s:%s:", ep->ae_number, ep->ae_name, ep->ae_desc);
207		printf_classmask(ep->ae_class);
208		printf("\n");
209	}
210}
211
212static void
213audump_user(void)
214{
215	au_user_ent_t *up;
216
217	while ((up = getauuserent()) != NULL) {
218		printf("%s:", up->au_name);
219		// printf_classmask(up->au_always);
220		printf(":");
221		// printf_classmask(up->au_never);
222		printf("\n");
223	}
224}
225
226static void
227audump_user_r(void)
228{
229	char user_ent_name[AU_USER_NAME_MAX];
230	au_user_ent_t u, *up;
231
232	bzero(&u, sizeof(u));
233	bzero(user_ent_name, sizeof(user_ent_name));
234	u.au_name = user_ent_name;
235
236	while ((up = getauuserent_r(&u)) != NULL) {
237		printf("%s:", up->au_name);
238		// printf_classmask(up->au_always);
239		printf(":");
240		// printf_classmask(up->au_never);
241		printf("\n");
242	}
243}
244
245int
246main(int argc, char *argv[])
247{
248
249	if (argc != 2)
250		usage();
251
252	if (strcmp(argv[1], "class") == 0)
253		audump_class();
254	else if (strcmp(argv[1], "class_r") == 0)
255		audump_class_r();
256	else if (strcmp(argv[1], "control") == 0)
257		audump_control();
258	else if (strcmp(argv[1], "event") == 0)
259		audump_event();
260	else if (strcmp(argv[1], "event_r") == 0)
261		audump_event_r();
262	else if (strcmp(argv[1], "user") == 0)
263		audump_user();
264	else if (strcmp(argv[1], "user_r") == 0)
265		audump_user_r();
266	else
267		usage();
268
269	return (0);
270}
271