]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/mac/mac_bsdextended/test_ugidfw.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / mac / mac_bsdextended / test_ugidfw.c
1 /*-
2  * Copyright (c) 2005 McAfee, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/param.h>
30 #include <sys/mac.h>
31 #include <sys/mount.h>
32
33 #include <security/mac_bsdextended/mac_bsdextended.h>
34
35 #include <err.h>
36 #include <grp.h>
37 #include <pwd.h>
38 #include <ugidfw.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 /*
44  * Starting point for a regression test for mac_bsdextended(4) and the
45  * supporting libugidfw(3).
46  */
47 void
48 usage(void)
49 {
50
51         fprintf(stderr, "test_ugidfw\n");
52         exit(-1);
53 }
54
55 /*
56  * This section of the regression test passes some test cases through the
57  * rule<->string routines to confirm they work approximately as desired.
58  */
59
60 /*
61  * List of users and groups we must check exists before we can begin, since
62  * they are used in the string test rules.  We use users and groups that will
63  * always exist in a default install used for regression testing.
64  */
65 static const char *test_users[] = {
66         "root",
67         "daemon",
68         "operator",
69         "bin",
70 };
71 static const int test_users_len = sizeof(test_users) / sizeof(char *);
72
73 static const char *test_groups[] = {
74         "wheel",
75         "daemon",
76         "operator",
77         "bin",
78 };
79 static const int test_groups_len = sizeof(test_groups) / sizeof(char *);
80
81 /*
82  * List of test strings that must go in (and come out) of libugidfw intact.
83  */
84 static const char *test_strings[] = {
85         /* Variations on subject and object uids. */
86         "subject uid root object uid root mode n",
87         "subject uid root object uid daemon mode n",
88         "subject uid daemon object uid root mode n",
89         "subject uid daemon object uid daemon mode n",
90         /* Variations on mode. */
91         "subject uid root object uid root mode a",
92         "subject uid root object uid root mode r",
93         "subject uid root object uid root mode s",
94         "subject uid root object uid root mode w",
95         "subject uid root object uid root mode x",
96         "subject uid root object uid root mode arswx",
97         /* Variations on subject and object gids. */
98         "subject gid wheel object gid wheel mode n",
99         "subject gid wheel object gid daemon mode n",
100         "subject gid daemon object gid wheel mode n",
101         "subject gid daemon object gid daemon mode n",
102         /* Subject uids and subject gids. */
103         "subject uid bin gid daemon object uid operator gid wheel mode n",
104         /* Not */
105         "subject not uid operator object uid bin mode n",
106         "subject uid bin object not uid operator mode n",
107         "subject not uid daemon object not uid operator mode n",
108         /* Ranges */
109         "subject uid root:operator object gid wheel:bin mode n",
110         /* Jail ID */
111         "subject jailid 1 object uid root mode n",
112         /* Filesys */
113         "subject uid root object filesys / mode n",
114         "subject uid root object filesys /dev mode n",
115         /* S/UGID */
116         "subject not uid root object sgid mode n",
117         "subject not uid root object sgid mode n",
118         /* Matching uid/gid */
119         "subject not uid root:operator object not uid_of_subject mode n",
120         "subject not gid wheel:bin object not gid_of_subject mode n",
121         /* Object types */
122         "subject uid root object type a mode a",
123         "subject uid root object type r mode a",
124         "subject uid root object type d mode a",
125         "subject uid root object type b mode a",
126         "subject uid root object type c mode a",
127         "subject uid root object type l mode a",
128         "subject uid root object type s mode a",
129         "subject uid root object type rbc mode a",
130         "subject uid root object type dls mode a",
131         /* Empty rules always match */
132         "subject object mode a",
133         /* Partial negations */
134         "subject ! uid root object mode n",
135         "subject ! gid wheel object mode n",
136         "subject ! jailid 2 object mode n",
137         "subject object ! uid root mode n",
138         "subject object ! gid wheel mode n",
139         "subject object ! filesys / mode n",
140         "subject object ! suid mode n",
141         "subject object ! sgid mode n",
142         "subject object ! uid_of_subject mode n",
143         "subject object ! gid_of_subject mode n",
144         "subject object ! type d mode n",
145         /* All out nonsense */
146         "subject uid root ! gid wheel:bin ! jailid 1 "
147             "object ! uid root:daemon gid daemon filesys / suid sgid uid_of_subject gid_of_subject ! type r "
148             "mode rsx",
149 };
150 static const int test_strings_len = sizeof(test_strings) / sizeof(char *);
151
152 static void
153 test_libugidfw_strings(void)
154 {
155         struct mac_bsdextended_rule rule;
156         char errorstr[256];
157         char rulestr[256];
158         int i, error;
159
160         for (i = 0; i < test_users_len; i++) {
161                 if (getpwnam(test_users[i]) == NULL)
162                         err(-1, "test_libugidfw_strings: getpwnam: %s",
163                             test_users[i]);
164         }
165
166         for (i = 0; i < test_groups_len; i++) {
167                 if (getgrnam(test_groups[i]) == NULL)
168                         err(-1, "test_libugidfw_strings: getgrnam: %s",
169                             test_groups[i]);
170         }
171
172         for (i = 0; i < test_strings_len; i++) {
173                 error = bsde_parse_rule_string(test_strings[i], &rule,
174                     sizeof(errorstr), errorstr);
175                 if (error == -1)
176                         errx(-1, "bsde_parse_rule_string: '%s' (%d): %s",
177                             test_strings[i], i, errorstr);
178                 error = bsde_rule_to_string(&rule, rulestr, sizeof(rulestr));
179                 if (error < 0)
180                         errx(-1, "bsde_rule_to_string: rule for '%s' "
181                             "returned %d", test_strings[i], error);
182
183                 if (strcmp(test_strings[i], rulestr) != 0)
184                         errx(-1, "test_libugidfw: '%s' in, '%s' out",
185                             test_strings[i], rulestr);
186         }
187 }
188
189 int
190 main(int argc, char *argv[])
191 {
192         char errorstr[256];
193         int count, slots;
194
195         if (argc != 1)
196                 usage();
197
198         /* Print an error if a non-root user attemps to run the tests. */
199         if (getuid() != 0) {
200                 fprintf(stderr, "Error!  Only root may run this utility\n");
201                 return (EXIT_FAILURE);
202         }
203
204         /*
205          * We can test some parts of the library without the MAC Framework
206          * and policy loaded, so run those tests before calling
207          * mac_is_present().
208          */
209         test_libugidfw_strings();
210
211         switch (mac_is_present("bsdextended")) {
212         case -1:
213                 err(-1, "mac_is_present");
214         case 1:
215                 break;
216         case 0:
217         default:
218                 errx(-1, "mac_bsdextended not loaded");
219         }
220
221         /*
222          * Some simple up-front checks to see if we're able to query the
223          * policy for basic state.  We want the rule count to be 0 before
224          * starting, but "slots" is a property of prior runs and so we ignore
225          * the return value.
226          */
227         count = bsde_get_rule_count(sizeof(errorstr), errorstr);
228         if (count == -1)
229                 errx(-1, "bsde_get_rule_count: %s", errorstr);
230         if (count != 0)
231                 errx(-1, "bsde_get_rule_count: %d rules", count);
232
233         slots = bsde_get_rule_slots(sizeof(errorstr), errorstr);
234         if (slots == -1)
235                 errx(-1, "bsde_get_rule_slots: %s", errorstr);
236
237         return (0);
238 }