xref: /freebsd/usr.sbin/ugidfw/ugidfw.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002, 2004 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * This software was developed for the FreeBSD Project by NAI Labs, the
8  * Security Research Division of Network Associates, Inc. under
9  * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
10  * CHATS research program.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/errno.h>
39 #include <sys/mount.h>
40 #include <sys/time.h>
41 #include <sys/sysctl.h>
42 
43 #include <security/mac_bsdextended/mac_bsdextended.h>
44 
45 #include <err.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <ugidfw.h>
50 
51 void add_rule(int argc, char *argv[]);
52 void list_rules(void);
53 void remove_rule(int argc, char *argv[]);
54 void set_rule(int argc, char *argv[]);
55 void usage(void);
56 
57 void
58 usage(void)
59 {
60 
61 	fprintf(stderr, "usage: ugidfw add [subject [not] [uid uid] [gid gid]]"
62 	    " [object [not] [uid uid] \\\n");
63 	fprintf(stderr, "    [gid gid]] mode arswxn\n");
64 	fprintf(stderr, "       ugidfw list\n");
65 	fprintf(stderr, "       ugidfw set rulenum [subject [not] [uid uid] [gid gid]]"
66 	    " [object [not] \\\n");
67 	fprintf(stderr, "    [uid uid] [gid gid]] mode arswxn\n");
68 	fprintf(stderr, "       ugidfw remove rulenum\n");
69 
70 	exit(1);
71 }
72 
73 void
74 add_rule(int argc, char *argv[])
75 {
76 	char errstr[BUFSIZ], charstr[BUFSIZ];
77 	struct mac_bsdextended_rule rule;
78 	int error, rulenum;
79 
80 	error = bsde_parse_rule(argc, argv, &rule, BUFSIZ, errstr);
81 	if (error) {
82 		warnx("%s", errstr);
83 		return;
84 	}
85 
86 	error = bsde_add_rule(&rulenum, &rule, BUFSIZ, errstr);
87 	if (error) {
88 		warnx("%s", errstr);
89 		return;
90 	}
91 	if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1)
92 		warnx("Added rule, but unable to print string.");
93 	else
94 		printf("%d %s\n", rulenum, charstr);
95 }
96 
97 void
98 list_rules(void)
99 {
100 	char errstr[BUFSIZ], charstr[BUFSIZ];
101 	struct mac_bsdextended_rule rule;
102 	int error, i, rule_count, rule_slots;
103 
104 	rule_slots = bsde_get_rule_slots(BUFSIZ, errstr);
105 	if (rule_slots == -1) {
106 		warnx("unable to get rule slots; mac_bsdextended.ko "
107 		    "may not be loaded");
108 		errx(1, "bsde_get_rule_slots: %s", errstr);
109 	}
110 
111 	rule_count = bsde_get_rule_count(BUFSIZ, errstr);
112 	if (rule_count == -1)
113 		errx(1, "bsde_get_rule_count: %s", errstr);
114 
115 	printf("%d slots, %d rules\n", rule_slots, rule_count);
116 
117 	for (i = 0; i < rule_slots; i++) {
118 		error = bsde_get_rule(i, &rule, BUFSIZ, errstr);
119 		switch (error) {
120 		case -2:
121 			continue;
122 		case -1:
123 			warnx("rule %d: %s", i, errstr);
124 			continue;
125 		case 0:
126 			break;
127 		}
128 
129 		if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1)
130 			warnx("unable to translate rule %d to string", i);
131 		else
132 			printf("%d %s\n", i, charstr);
133 	}
134 }
135 
136 void
137 set_rule(int argc, char *argv[])
138 {
139 	char errstr[BUFSIZ];
140 	struct mac_bsdextended_rule rule;
141 	long value;
142 	int error, rulenum;
143 	char *endp;
144 
145 	if (argc < 1)
146 		usage();
147 
148 	value = strtol(argv[0], &endp, 10);
149 	if (*endp != '\0')
150 		usage();
151 
152 	if ((long) value != (int) value || value < 0)
153 		usage();
154 
155 	rulenum = value;
156 
157 	error = bsde_parse_rule(argc - 1, argv + 1, &rule, BUFSIZ, errstr);
158 	if (error) {
159 		warnx("%s", errstr);
160 		return;
161 	}
162 
163 	error = bsde_set_rule(rulenum, &rule, BUFSIZ, errstr);
164 	if (error) {
165 		warnx("%s", errstr);
166 		return;
167 	}
168 }
169 
170 void
171 remove_rule(int argc, char *argv[])
172 {
173 	char errstr[BUFSIZ];
174 	long value;
175 	int error, rulenum;
176 	char *endp;
177 
178 	if (argc != 1)
179 		usage();
180 
181 	value = strtol(argv[0], &endp, 10);
182 	if (*endp != '\0')
183 		usage();
184 
185 	if ((long) value != (int) value || value < 0)
186 		usage();
187 
188 	rulenum = value;
189 
190 	error = bsde_delete_rule(rulenum, BUFSIZ, errstr);
191 	if (error)
192 		warnx("%s", errstr);
193 }
194 
195 int
196 main(int argc, char *argv[])
197 {
198 
199 	if (argc < 2)
200 		usage();
201 
202 	if (strcmp("add", argv[1]) == 0) {
203 		add_rule(argc-2, argv+2);
204 	} else if (strcmp("list", argv[1]) == 0) {
205 		if (argc != 2)
206 			usage();
207 		list_rules();
208 	} else if (strcmp("set", argv[1]) == 0) {
209 		set_rule(argc-2, argv+2);
210 	} else if (strcmp("remove", argv[1]) == 0) {
211 		remove_rule(argc-2, argv+2);
212 	} else
213 		usage();
214 
215 	return (0);
216 }
217