]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ugidfw/ugidfw.c
This commit was generated by cvs2svn to compensate for changes in r127904,
[FreeBSD/FreeBSD.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  * $FreeBSD$
32  */
33 #include <sys/param.h>
34 #include <sys/errno.h>
35 #include <sys/time.h>
36 #include <sys/sysctl.h>
37 #include <sys/vnode.h>
38
39 #include <security/mac_bsdextended/mac_bsdextended.h>
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ugidfw.h>
45
46 void
47 usage(void)
48 {
49
50         fprintf(stderr, "ugidfw add [subject [not] [uid uid] [gid gid]]"
51             " [object [not] [uid uid] \\\n");
52         fprintf(stderr, "    [gid gid]] mode arswxn\n");
53         fprintf(stderr, "ugidfw list\n");
54         fprintf(stderr, "ugidfw set rulenum [subject [not] [uid uid] [gid gid]]"
55             " [object [not] \\\n");
56         fprintf(stderr, "    [uid uid] [gid gid]] mode arswxn\n");
57         fprintf(stderr, "ugidfw remove rulenum\n");
58
59         exit(-1);
60 }
61
62 void
63 add_rule(int argc, char *argv[])
64 {
65         char errstr[BUFSIZ];
66         struct mac_bsdextended_rule rule;
67         long value;
68         int error, rulenum;
69         char *endp;
70
71         error = bsde_parse_rule(argc, argv, &rule, BUFSIZ, errstr);
72         if (error) {
73                 fprintf(stderr, "%s\n", errstr);
74                 return;
75         }
76
77         error = bsde_add_rule(&rulenum, &rule, BUFSIZ, errstr);
78         if (error) {
79                 fprintf(stderr, "%s\n", errstr);
80                 return;
81         }
82         printf("Added rule %d\n", rulenum);
83 }
84
85 void
86 list_rules(void)
87 {
88         char errstr[BUFSIZ], charstr[BUFSIZ];
89         struct mac_bsdextended_rule rule;
90         int error, i, rule_count, rule_slots;
91
92         rule_slots = bsde_get_rule_slots(BUFSIZ, errstr);
93         if (rule_slots == -1) {
94                 fprintf(stderr, "Unable to get rule slots; mac_bsdextended.ko "
95                     "may not be loaded.\n");
96                 fprintf(stderr, "bsde_get_rule_slots: %s\n", errstr);
97                 exit (-1);
98         }
99
100         rule_count = bsde_get_rule_count(BUFSIZ, errstr);
101         if (rule_count == -1) {
102                 fprintf(stderr, "bsde_get_rule_count: %s\n", errstr);
103                 exit (-1);
104         }
105
106         printf("%d slots, %d rules\n", rule_slots, rule_count);
107
108         for (i = 0; i <= rule_slots; i++) {
109                 error = bsde_get_rule(i, &rule, BUFSIZ, errstr);
110                 switch (error) {
111                 case -2:
112                         continue;
113                 case -1:
114                         fprintf(stderr, "rule %d: %s\n", i, errstr);
115                         continue;
116                 case 0:
117                         break;
118                 }
119
120                 if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1)
121                         fprintf(stderr,
122                             "Unable to translate rule %d to string\n", i);
123                 else
124                         printf("%d %s\n", i, charstr);
125         }
126 }
127
128 void
129 set_rule(int argc, char *argv[])
130 {
131         char errstr[BUFSIZ];
132         struct mac_bsdextended_rule rule;
133         long value;
134         int error, rulenum;
135         char *endp;
136
137         if (argc < 1)
138                 usage();
139
140         value = strtol(argv[0], &endp, 10);
141         if (*endp != '\0')
142                 usage();
143
144         if ((long) value != (int) value || value < 0)
145                 usage();
146
147         rulenum = value;
148
149         error = bsde_parse_rule(argc - 1, argv + 1, &rule, BUFSIZ, errstr);
150         if (error) {
151                 fprintf(stderr, "%s\n", errstr);
152                 return;
153         }
154
155         error = bsde_set_rule(rulenum, &rule, BUFSIZ, errstr);
156         if (error) {
157                 fprintf(stderr, "%s\n", errstr);
158                 return;
159         }
160 }
161
162 void
163 remove_rule(int argc, char *argv[])
164 {
165         char errstr[BUFSIZ];
166         long value;
167         int error, rulenum;
168         char *endp;
169
170         if (argc != 1)
171                 usage();
172
173         value = strtol(argv[0], &endp, 10);
174         if (*endp != '\0')  
175                 usage();
176
177         if ((long) value != (int) value || value < 0)
178                 usage();
179
180         rulenum = value;
181
182         error = bsde_delete_rule(rulenum, BUFSIZ, errstr);
183         if (error)
184                 fprintf(stderr, "%s\n", errstr);
185 }
186
187 int
188 main(int argc, char *argv[])
189 {
190
191         if (argc < 2)
192                 usage();
193
194         if (strcmp("add", argv[1]) == 0) {
195                 add_rule(argc-2, argv+2);
196         } else if (strcmp("list", argv[1]) == 0) {
197                 if (argc != 2)
198                         usage();
199                 list_rules();
200         } else if (strcmp("set", argv[1]) == 0) {
201                 set_rule(argc-2, argv+2);
202         } else if (strcmp("remove", argv[1]) == 0) {
203                 remove_rule(argc-2, argv+2);
204         } else
205                 usage();
206
207         return (0);
208 }