3 * mdp - make dummy policy
5 * When pointed at a kernel tree, builds a dummy policy for that kernel
6 * with exactly one type with full rights to itself.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Copyright (C) IBM Corporation, 2006
24 * Authors: Serge E. Hallyn <serue@us.ibm.com>
34 static void usage(char *name
)
36 printf("usage: %s [-m] policy_file context_file\n", name
);
40 static void find_common_name(char *cname
, char *dest
, int len
)
44 start
= strchr(cname
, '_')+1;
45 end
= strchr(start
, '_');
46 if (!start
|| !end
|| start
-cname
> len
|| end
-start
> len
) {
47 printf("Error with commons defines\n");
50 strncpy(dest
, start
, end
-start
);
51 dest
[end
-start
] = '\0';
55 static char *classlist
[] = {
56 #include "class_to_string.h"
61 #include "initial_sid_to_string.h"
63 #define TB_(x) char *x[] = {
64 #define TE_(x) NULL };
66 #include "common_perm_to_string.h"
75 struct common common
[] = {
76 #define TB_(x) { #x, x },
79 #include "common_perm_to_string.h"
85 #define S_(x, y, z) {x, #y},
90 struct av_inherit av_inherit
[] = {
91 #include "av_inherit.h"
95 #include "av_permissions.h"
96 #define S_(x, y, z) {x, y, z},
102 struct av_perms av_perms
[] = {
103 #include "av_perm_to_string.h"
107 int main(int argc
, char *argv
[])
110 char **arg
, *polout
, *ctxout
;
111 int classlist_len
, initial_sid_to_string_len
;
117 if (argc
==4 && strcmp(argv
[1], "-m") == 0) {
124 fout
= fopen(polout
, "w");
126 printf("Could not open %s for writing\n", polout
);
130 classlist_len
= sizeof(classlist
) / sizeof(char *);
131 /* print out the classes */
132 for (i
=1; i
< classlist_len
; i
++) {
134 fprintf(fout
, "class %s\n", classlist
[i
]);
136 fprintf(fout
, "class user%d\n", i
);
140 initial_sid_to_string_len
= sizeof(initial_sid_to_string
) / sizeof (char *);
141 /* print out the sids */
142 for (i
=1; i
< initial_sid_to_string_len
; i
++)
143 fprintf(fout
, "sid %s\n", initial_sid_to_string
[i
]);
146 /* print out the commons */
147 for (i
=0; i
< sizeof(common
)/sizeof(struct common
); i
++) {
149 find_common_name(common
[i
].cname
, cname
, 100);
151 fprintf(fout
, "common %s\n{\n", cname
);
152 for (j
=0; common
[i
].perms
[j
]; j
++)
153 fprintf(fout
, "\t%s\n", common
[i
].perms
[j
]);
154 fprintf(fout
, "}\n\n");
158 /* print out the class permissions */
159 for (i
=1; i
< classlist_len
; i
++) {
161 int firstperm
= -1, numperms
= 0;
163 fprintf(fout
, "class %s\n", classlist
[i
]);
164 /* does it inherit from a common? */
165 for (j
=0; j
< sizeof(av_inherit
)/sizeof(struct av_inherit
); j
++)
166 if (av_inherit
[j
].class == i
)
167 fprintf(fout
, "inherits %s\n", av_inherit
[j
].common
);
169 for (j
=0; j
< sizeof(av_perms
)/sizeof(struct av_perms
); j
++) {
170 if (av_perms
[j
].class == i
) {
181 fprintf(fout
, "{\n");
182 /* print out the av_perms */
183 for (j
=0; j
< numperms
; j
++) {
184 fprintf(fout
, "\t%s\n", av_perms
[firstperm
+j
].perm_s
);
186 fprintf(fout
, "}\n\n");
191 /* NOW PRINT OUT MLS STUFF */
193 printf("MLS not yet implemented\n");
197 /* types, roles, and allows */
198 fprintf(fout
, "type base_t;\n");
199 fprintf(fout
, "role base_r types { base_t };\n");
200 for (i
=1; i
< classlist_len
; i
++) {
202 fprintf(fout
, "allow base_t base_t:%s *;\n", classlist
[i
]);
204 fprintf(fout
, "allow base_t base_t:user%d *;\n", i
);
206 fprintf(fout
, "user user_u roles { base_r };\n");
210 for (i
=1; i
< initial_sid_to_string_len
; i
++)
211 fprintf(fout
, "sid %s user_u:base_r:base_t\n", initial_sid_to_string
[i
]);
215 fprintf(fout
, "fs_use_xattr ext2 user_u:base_r:base_t;\n");
216 fprintf(fout
, "fs_use_xattr ext3 user_u:base_r:base_t;\n");
217 fprintf(fout
, "fs_use_xattr jfs user_u:base_r:base_t;\n");
218 fprintf(fout
, "fs_use_xattr xfs user_u:base_r:base_t;\n");
219 fprintf(fout
, "fs_use_xattr reiserfs user_u:base_r:base_t;\n");
221 fprintf(fout
, "fs_use_task pipefs user_u:base_r:base_t;\n");
222 fprintf(fout
, "fs_use_task sockfs user_u:base_r:base_t;\n");
224 fprintf(fout
, "fs_use_trans devpts user_u:base_r:base_t;\n");
225 fprintf(fout
, "fs_use_trans tmpfs user_u:base_r:base_t;\n");
226 fprintf(fout
, "fs_use_trans shm user_u:base_r:base_t;\n");
228 fprintf(fout
, "genfscon proc / user_u:base_r:base_t\n");
232 fout
= fopen(ctxout
, "w");
234 printf("Wrote policy, but cannot open %s for writing\n", ctxout
);
237 fprintf(fout
, "/ user_u:base_r:base_t\n");
238 fprintf(fout
, "/.* user_u:base_r:base_t\n");