2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
35 #include <sys/errno.h>
36 #include <sys/mount.h>
38 #include <sys/sysctl.h>
40 #include <security/mac_bsdextended/mac_bsdextended.h>
48 void add_rule(int argc, char *argv[]);
49 void list_rules(void);
50 void remove_rule(int argc, char *argv[]);
51 void set_rule(int argc, char *argv[]);
58 fprintf(stderr, "usage: ugidfw add [subject [not] [uid uid] [gid gid]]"
59 " [object [not] [uid uid] \\\n");
60 fprintf(stderr, " [gid gid]] mode arswxn\n");
61 fprintf(stderr, " ugidfw list\n");
62 fprintf(stderr, " ugidfw set rulenum [subject [not] [uid uid] [gid gid]]"
63 " [object [not] \\\n");
64 fprintf(stderr, " [uid uid] [gid gid]] mode arswxn\n");
65 fprintf(stderr, " ugidfw remove rulenum\n");
71 add_rule(int argc, char *argv[])
73 char errstr[BUFSIZ], charstr[BUFSIZ];
74 struct mac_bsdextended_rule rule;
77 error = bsde_parse_rule(argc, argv, &rule, BUFSIZ, errstr);
83 error = bsde_add_rule(&rulenum, &rule, BUFSIZ, errstr);
88 if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1)
89 warnx("Added rule, but unable to print string.");
91 printf("%d %s\n", rulenum, charstr);
97 char errstr[BUFSIZ], charstr[BUFSIZ];
98 struct mac_bsdextended_rule rule;
99 int error, i, rule_count, rule_slots;
101 rule_slots = bsde_get_rule_slots(BUFSIZ, errstr);
102 if (rule_slots == -1) {
103 warnx("unable to get rule slots; mac_bsdextended.ko "
104 "may not be loaded");
105 errx(1, "bsde_get_rule_slots: %s", errstr);
108 rule_count = bsde_get_rule_count(BUFSIZ, errstr);
109 if (rule_count == -1)
110 errx(1, "bsde_get_rule_count: %s", errstr);
112 printf("%d slots, %d rules\n", rule_slots, rule_count);
114 for (i = 0; i < rule_slots; i++) {
115 error = bsde_get_rule(i, &rule, BUFSIZ, errstr);
120 warnx("rule %d: %s", i, errstr);
126 if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1)
127 warnx("unable to translate rule %d to string", i);
129 printf("%d %s\n", i, charstr);
134 set_rule(int argc, char *argv[])
137 struct mac_bsdextended_rule rule;
145 value = strtol(argv[0], &endp, 10);
149 if ((long) value != (int) value || value < 0)
154 error = bsde_parse_rule(argc - 1, argv + 1, &rule, BUFSIZ, errstr);
160 error = bsde_set_rule(rulenum, &rule, BUFSIZ, errstr);
168 remove_rule(int argc, char *argv[])
178 value = strtol(argv[0], &endp, 10);
182 if ((long) value != (int) value || value < 0)
187 error = bsde_delete_rule(rulenum, BUFSIZ, errstr);
193 main(int argc, char *argv[])
199 if (strcmp("add", argv[1]) == 0) {
200 add_rule(argc-2, argv+2);
201 } else if (strcmp("list", argv[1]) == 0) {
205 } else if (strcmp("set", argv[1]) == 0) {
206 set_rule(argc-2, argv+2);
207 } else if (strcmp("remove", argv[1]) == 0) {
208 remove_rule(argc-2, argv+2);