xref: /freebsd/contrib/openbsm/tools/audump.c (revision 33644623554bb0fc57ed3c7d874193a498679b22)
1 /*-
2  * Copyright (c) 2005-2006 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#7 $
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 
40 static void
41 usage(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 
49 static void
50 audump_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 
59 static void
60 audump_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 
77 static void
78 audump_control(void)
79 {
80 	char string[PATH_MAX], string2[PATH_MAX];
81 	int ret, val;
82 	long policy;
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 
131 static void
132 printf_classmask(au_class_t classmask)
133 {
134 	au_class_ent_t *c;
135 	u_int32_t i;
136 	int first;
137 
138 	first = 1;
139 	for (i = 0; i < 32; i++) {
140 		if (classmask & (2 << i)) {
141 			if (first)
142 				first = 0;
143 			else
144 				printf(",");
145 			c = getauclassnum(2 << i);
146 			if (c != NULL)
147 				printf("%s", c->ac_name);
148 			else
149 				printf("0x%x", 2 << i);
150 		}
151 	}
152 }
153 
154 static void
155 audump_event(void)
156 {
157 	au_event_ent_t *ep;
158 
159 	while ((ep = getauevent()) != NULL) {
160 		printf("%d:%s:%s:", ep->ae_number, ep->ae_name, ep->ae_desc);
161 		printf_classmask(ep->ae_class);
162 		printf("\n");
163 	}
164 }
165 
166 static void
167 audump_event_r(void)
168 {
169 	char event_ent_name[AU_EVENT_NAME_MAX];
170 	char event_ent_desc[AU_EVENT_DESC_MAX];
171 	au_event_ent_t e, *ep;
172 
173 	bzero(&e, sizeof(e));
174 	bzero(event_ent_name, sizeof(event_ent_name));
175 	bzero(event_ent_desc, sizeof(event_ent_desc));
176 	e.ae_name = event_ent_name;
177 	e.ae_desc = event_ent_desc;
178 
179 	while ((ep = getauevent_r(&e)) != NULL) {
180 		printf("%d:%s:%s:", ep->ae_number, ep->ae_name, ep->ae_desc);
181 		printf_classmask(ep->ae_class);
182 		printf("\n");
183 	}
184 }
185 
186 static void
187 audump_user(void)
188 {
189 	au_user_ent_t *up;
190 
191 	while ((up = getauuserent()) != NULL) {
192 		printf("%s:", up->au_name);
193 		// printf_classmask(up->au_always);
194 		printf(":");
195 		// printf_classmask(up->au_never);
196 		printf("\n");
197 	}
198 }
199 
200 static void
201 audump_user_r(void)
202 {
203 	char user_ent_name[AU_USER_NAME_MAX];
204 	au_user_ent_t u, *up;
205 
206 	bzero(&u, sizeof(u));
207 	bzero(user_ent_name, sizeof(user_ent_name));
208 	u.au_name = user_ent_name;
209 
210 	while ((up = getauuserent_r(&u)) != NULL) {
211 		printf("%s:", up->au_name);
212 		// printf_classmask(up->au_always);
213 		printf(":");
214 		// printf_classmask(up->au_never);
215 		printf("\n");
216 	}
217 }
218 
219 int
220 main(int argc, char *argv[])
221 {
222 
223 	if (argc != 2)
224 		usage();
225 
226 	if (strcmp(argv[1], "class") == 0)
227 		audump_class();
228 	else if (strcmp(argv[1], "class_r") == 0)
229 		audump_class_r();
230 	else if (strcmp(argv[1], "control") == 0)
231 		audump_control();
232 	else if (strcmp(argv[1], "event") == 0)
233 		audump_event();
234 	else if (strcmp(argv[1], "event_r") == 0)
235 		audump_event_r();
236 	else if (strcmp(argv[1], "user") == 0)
237 		audump_user();
238 	else if (strcmp(argv[1], "user_r") == 0)
239 		audump_user_r();
240 	else
241 		usage();
242 
243 	return (0);
244 }
245