blob: 6a24569c3578af93673cdd40372fdd6224638de9 [file] [log] [blame]
Paul Moorebfc5e3a2016-12-21 10:39:25 -05001
2/* NOTE: we really do want to use the kernel headers here */
3#define __EXPORTED_HEADERS__
4
Stephen Smalley8753f6b2009-09-30 13:41:02 -04005#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <string.h>
9#include <errno.h>
10#include <ctype.h>
Nicolas Ioossc017c712017-03-05 15:01:52 +010011#include <sys/socket.h>
Stephen Smalley8753f6b2009-09-30 13:41:02 -040012
13struct security_class_mapping {
14 const char *name;
15 const char *perms[sizeof(unsigned) * 8 + 1];
16};
17
18#include "classmap.h"
19#include "initial_sid_to_string.h"
20
Eric Paris85c3b522009-11-20 11:00:12 -050021#define max(x, y) (((int)(x) > (int)(y)) ? x : y)
Stephen Smalley8753f6b2009-09-30 13:41:02 -040022
23const char *progname;
24
Alan Cox821d35a2009-11-18 14:39:51 +000025static void usage(void)
Stephen Smalley8753f6b2009-09-30 13:41:02 -040026{
27 printf("usage: %s flask.h av_permissions.h\n", progname);
28 exit(1);
29}
30
Alan Cox821d35a2009-11-18 14:39:51 +000031static char *stoupperx(const char *s)
Stephen Smalley8753f6b2009-09-30 13:41:02 -040032{
33 char *s2 = strdup(s);
34 char *p;
35
36 if (!s2) {
37 fprintf(stderr, "%s: out of memory\n", progname);
38 exit(3);
39 }
40
41 for (p = s2; *p; p++)
42 *p = toupper(*p);
43 return s2;
44}
45
46int main(int argc, char *argv[])
47{
48 int i, j, k;
49 int isids_len;
50 FILE *fout;
Harry Ciao4bc6c2d2011-03-02 13:46:08 +080051 const char *needle = "SOCKET";
52 char *substr;
Stephen Smalley8753f6b2009-09-30 13:41:02 -040053
54 progname = argv[0];
55
56 if (argc < 3)
57 usage();
58
59 fout = fopen(argv[1], "w");
60 if (!fout) {
61 fprintf(stderr, "Could not open %s for writing: %s\n",
62 argv[1], strerror(errno));
63 exit(2);
64 }
65
66 for (i = 0; secclass_map[i].name; i++) {
67 struct security_class_mapping *map = &secclass_map[i];
68 map->name = stoupperx(map->name);
69 for (j = 0; map->perms[j]; j++)
70 map->perms[j] = stoupperx(map->perms[j]);
71 }
72
73 isids_len = sizeof(initial_sid_to_string) / sizeof (char *);
74 for (i = 1; i < isids_len; i++)
75 initial_sid_to_string[i] = stoupperx(initial_sid_to_string[i]);
76
77 fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
78 fprintf(fout, "#ifndef _SELINUX_FLASK_H_\n#define _SELINUX_FLASK_H_\n\n");
79
80 for (i = 0; secclass_map[i].name; i++) {
81 struct security_class_mapping *map = &secclass_map[i];
82 fprintf(fout, "#define SECCLASS_%s", map->name);
83 for (j = 0; j < max(1, 40 - strlen(map->name)); j++)
84 fprintf(fout, " ");
85 fprintf(fout, "%2d\n", i+1);
86 }
87
88 fprintf(fout, "\n");
89
90 for (i = 1; i < isids_len; i++) {
James Morris310de042010-03-16 08:47:36 +110091 const char *s = initial_sid_to_string[i];
Stephen Smalley8753f6b2009-09-30 13:41:02 -040092 fprintf(fout, "#define SECINITSID_%s", s);
93 for (j = 0; j < max(1, 40 - strlen(s)); j++)
94 fprintf(fout, " ");
95 fprintf(fout, "%2d\n", i);
96 }
97 fprintf(fout, "\n#define SECINITSID_NUM %d\n", i-1);
Harry Ciao4bc6c2d2011-03-02 13:46:08 +080098 fprintf(fout, "\nstatic inline bool security_is_socket_class(u16 kern_tclass)\n");
99 fprintf(fout, "{\n");
100 fprintf(fout, "\tbool sock = false;\n\n");
101 fprintf(fout, "\tswitch (kern_tclass) {\n");
102 for (i = 0; secclass_map[i].name; i++) {
103 struct security_class_mapping *map = &secclass_map[i];
104 substr = strstr(map->name, needle);
105 if (substr && strcmp(substr, needle) == 0)
106 fprintf(fout, "\tcase SECCLASS_%s:\n", map->name);
107 }
108 fprintf(fout, "\t\tsock = true;\n");
109 fprintf(fout, "\t\tbreak;\n");
110 fprintf(fout, "\tdefault:\n");
111 fprintf(fout, "\t\tbreak;\n");
112 fprintf(fout, "\t}\n\n");
113 fprintf(fout, "\treturn sock;\n");
114 fprintf(fout, "}\n");
115
Stephen Smalley8753f6b2009-09-30 13:41:02 -0400116 fprintf(fout, "\n#endif\n");
117 fclose(fout);
118
119 fout = fopen(argv[2], "w");
120 if (!fout) {
121 fprintf(stderr, "Could not open %s for writing: %s\n",
122 argv[2], strerror(errno));
123 exit(4);
124 }
125
126 fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
127 fprintf(fout, "#ifndef _SELINUX_AV_PERMISSIONS_H_\n#define _SELINUX_AV_PERMISSIONS_H_\n\n");
128
129 for (i = 0; secclass_map[i].name; i++) {
130 struct security_class_mapping *map = &secclass_map[i];
131 for (j = 0; map->perms[j]; j++) {
132 fprintf(fout, "#define %s__%s", map->name,
133 map->perms[j]);
134 for (k = 0; k < max(1, 40 - strlen(map->name) - strlen(map->perms[j])); k++)
135 fprintf(fout, " ");
136 fprintf(fout, "0x%08xUL\n", (1<<j));
137 }
138 }
139
140 fprintf(fout, "\n#endif\n");
141 fclose(fout);
142 exit(0);
143}