2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002, 2004 Networks Associates Technology, Inc.
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/errno.h>
39 #include <sys/mount.h>
41 #include <sys/sysctl.h>
43 #include <security/mac_bsdextended/mac_bsdextended.h>
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[]);
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");
74 add_rule(int argc, char *argv[])
76 char errstr[BUFSIZ], charstr[BUFSIZ];
77 struct mac_bsdextended_rule rule;
80 error = bsde_parse_rule(argc, argv, &rule, BUFSIZ, errstr);
86 error = bsde_add_rule(&rulenum, &rule, BUFSIZ, errstr);
91 if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1)
92 warnx("Added rule, but unable to print string.");
94 printf("%d %s\n", rulenum, charstr);
100 char errstr[BUFSIZ], charstr[BUFSIZ];
101 struct mac_bsdextended_rule rule;
102 int error, i, rule_count, rule_slots;
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);
111 rule_count = bsde_get_rule_count(BUFSIZ, errstr);
112 if (rule_count == -1)
113 errx(1, "bsde_get_rule_count: %s", errstr);
115 printf("%d slots, %d rules\n", rule_slots, rule_count);
117 for (i = 0; i < rule_slots; i++) {
118 error = bsde_get_rule(i, &rule, BUFSIZ, errstr);
123 warnx("rule %d: %s", i, errstr);
129 if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1)
130 warnx("unable to translate rule %d to string", i);
132 printf("%d %s\n", i, charstr);
137 set_rule(int argc, char *argv[])
140 struct mac_bsdextended_rule rule;
148 value = strtol(argv[0], &endp, 10);
152 if ((long) value != (int) value || value < 0)
157 error = bsde_parse_rule(argc - 1, argv + 1, &rule, BUFSIZ, errstr);
163 error = bsde_set_rule(rulenum, &rule, BUFSIZ, errstr);
171 remove_rule(int argc, char *argv[])
181 value = strtol(argv[0], &endp, 10);
185 if ((long) value != (int) value || value < 0)
190 error = bsde_delete_rule(rulenum, BUFSIZ, errstr);
196 main(int argc, char *argv[])
202 if (strcmp("add", argv[1]) == 0) {
203 add_rule(argc-2, argv+2);
204 } else if (strcmp("list", argv[1]) == 0) {
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);