1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
35#include <sys/errno.h>
36#include <sys/mount.h>
37#include <sys/time.h>
38#include <sys/sysctl.h>
39
40#include <security/mac_bsdextended/mac_bsdextended.h>
41
42#include <err.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <ugidfw.h>
47
48void add_rule(int argc, char *argv[]);
49void list_rules(void);
50void remove_rule(int argc, char *argv[]);
51void set_rule(int argc, char *argv[]);
52void usage(void);
53
54void
55usage(void)
56{
57
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");
66
67	exit(1);
68}
69
70void
71add_rule(int argc, char *argv[])
72{
73	char errstr[BUFSIZ], charstr[BUFSIZ];
74	struct mac_bsdextended_rule rule;
75	int error, rulenum;
76
77	error = bsde_parse_rule(argc, argv, &rule, BUFSIZ, errstr);
78	if (error) {
79		warnx("%s", errstr);
80		return;
81	}
82
83	error = bsde_add_rule(&rulenum, &rule, BUFSIZ, errstr);
84	if (error) {
85		warnx("%s", errstr);
86		return;
87	}
88	if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1)
89		warnx("Added rule, but unable to print string.");
90	else
91		printf("%d %s\n", rulenum, charstr);
92}
93
94void
95list_rules(void)
96{
97	char errstr[BUFSIZ], charstr[BUFSIZ];
98	struct mac_bsdextended_rule rule;
99	int error, i, rule_count, rule_slots;
100
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);
106	}
107
108	rule_count = bsde_get_rule_count(BUFSIZ, errstr);
109	if (rule_count == -1)
110		errx(1, "bsde_get_rule_count: %s", errstr);
111
112	printf("%d slots, %d rules\n", rule_slots, rule_count);
113
114	for (i = 0; i < rule_slots; i++) {
115		error = bsde_get_rule(i, &rule, BUFSIZ, errstr);
116		switch (error) {
117		case -2:
118			continue;
119		case -1:
120			warnx("rule %d: %s", i, errstr);
121			continue;
122		case 0:
123			break;
124		}
125
126		if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1)
127			warnx("unable to translate rule %d to string", i);
128		else
129			printf("%d %s\n", i, charstr);
130	}
131}
132
133void
134set_rule(int argc, char *argv[])
135{
136	char errstr[BUFSIZ];
137	struct mac_bsdextended_rule rule;
138	long value;
139	int error, rulenum;
140	char *endp;
141
142	if (argc < 1)
143		usage();
144
145	value = strtol(argv[0], &endp, 10);
146	if (*endp != '\0')
147		usage();
148
149	if ((long) value != (int) value || value < 0)
150		usage();
151
152	rulenum = value;
153
154	error = bsde_parse_rule(argc - 1, argv + 1, &rule, BUFSIZ, errstr);
155	if (error) {
156		warnx("%s", errstr);
157		return;
158	}
159
160	error = bsde_set_rule(rulenum, &rule, BUFSIZ, errstr);
161	if (error) {
162		warnx("%s", errstr);
163		return;
164	}
165}
166
167void
168remove_rule(int argc, char *argv[])
169{
170	char errstr[BUFSIZ];
171	long value;
172	int error, rulenum;
173	char *endp;
174
175	if (argc != 1)
176		usage();
177
178	value = strtol(argv[0], &endp, 10);
179	if (*endp != '\0')
180		usage();
181
182	if ((long) value != (int) value || value < 0)
183		usage();
184
185	rulenum = value;
186
187	error = bsde_delete_rule(rulenum, BUFSIZ, errstr);
188	if (error)
189		warnx("%s", errstr);
190}
191
192int
193main(int argc, char *argv[])
194{
195
196	if (argc < 2)
197		usage();
198
199	if (strcmp("add", argv[1]) == 0) {
200		add_rule(argc-2, argv+2);
201	} else if (strcmp("list", argv[1]) == 0) {
202		if (argc != 2)
203			usage();
204		list_rules();
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);
209	} else
210		usage();
211
212	return (0);
213}
214