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