xref: /aosp_15_r20/system/sepolicy/tools/sepolicy-analyze/sepolicy-analyze.c (revision e4a36f4174b17bbab9dc043f4a65dc8d87377290)
1*e4a36f41SAndroid Build Coastguard Worker #include <stddef.h>
2*e4a36f41SAndroid Build Coastguard Worker #include <stdio.h>
3*e4a36f41SAndroid Build Coastguard Worker #include <string.h>
4*e4a36f41SAndroid Build Coastguard Worker 
5*e4a36f41SAndroid Build Coastguard Worker #include "dups.h"
6*e4a36f41SAndroid Build Coastguard Worker #include "neverallow.h"
7*e4a36f41SAndroid Build Coastguard Worker #include "perm.h"
8*e4a36f41SAndroid Build Coastguard Worker #include "typecmp.h"
9*e4a36f41SAndroid Build Coastguard Worker #include "booleans.h"
10*e4a36f41SAndroid Build Coastguard Worker #include "attribute.h"
11*e4a36f41SAndroid Build Coastguard Worker #include "utils.h"
12*e4a36f41SAndroid Build Coastguard Worker 
13*e4a36f41SAndroid Build Coastguard Worker #define NUM_COMPONENTS (int) (sizeof(analyze_components)/sizeof(analyze_components[0]))
14*e4a36f41SAndroid Build Coastguard Worker 
15*e4a36f41SAndroid Build Coastguard Worker #define COMP(x) { #x, sizeof(#x) - 1, x ##_usage, x ##_func }
16*e4a36f41SAndroid Build Coastguard Worker static struct {
17*e4a36f41SAndroid Build Coastguard Worker     const char *key;
18*e4a36f41SAndroid Build Coastguard Worker     size_t keylen;
19*e4a36f41SAndroid Build Coastguard Worker     void (*usage) (void);
20*e4a36f41SAndroid Build Coastguard Worker     int (*func) (int argc, char **argv, policydb_t *policydb);
21*e4a36f41SAndroid Build Coastguard Worker } analyze_components[] = {
22*e4a36f41SAndroid Build Coastguard Worker     COMP(dups),
23*e4a36f41SAndroid Build Coastguard Worker     COMP(neverallow),
24*e4a36f41SAndroid Build Coastguard Worker     COMP(permissive),
25*e4a36f41SAndroid Build Coastguard Worker     COMP(typecmp),
26*e4a36f41SAndroid Build Coastguard Worker     COMP(booleans),
27*e4a36f41SAndroid Build Coastguard Worker     COMP(attribute)
28*e4a36f41SAndroid Build Coastguard Worker };
29*e4a36f41SAndroid Build Coastguard Worker 
usage(char * arg0)30*e4a36f41SAndroid Build Coastguard Worker void usage(char *arg0)
31*e4a36f41SAndroid Build Coastguard Worker {
32*e4a36f41SAndroid Build Coastguard Worker     int i;
33*e4a36f41SAndroid Build Coastguard Worker 
34*e4a36f41SAndroid Build Coastguard Worker     fprintf(stderr, "%s must be called on a policy file with a component and the appropriate arguments specified\n", arg0);
35*e4a36f41SAndroid Build Coastguard Worker     fprintf(stderr, "%s <policy-file>:\n", arg0);
36*e4a36f41SAndroid Build Coastguard Worker     for(i = 0; i < NUM_COMPONENTS; i++) {
37*e4a36f41SAndroid Build Coastguard Worker         analyze_components[i].usage();
38*e4a36f41SAndroid Build Coastguard Worker     }
39*e4a36f41SAndroid Build Coastguard Worker     exit(1);
40*e4a36f41SAndroid Build Coastguard Worker }
41*e4a36f41SAndroid Build Coastguard Worker 
main(int argc,char ** argv)42*e4a36f41SAndroid Build Coastguard Worker int main(int argc, char **argv)
43*e4a36f41SAndroid Build Coastguard Worker {
44*e4a36f41SAndroid Build Coastguard Worker     char *policy;
45*e4a36f41SAndroid Build Coastguard Worker     struct policy_file pf;
46*e4a36f41SAndroid Build Coastguard Worker     policydb_t policydb;
47*e4a36f41SAndroid Build Coastguard Worker     int rc;
48*e4a36f41SAndroid Build Coastguard Worker     int i;
49*e4a36f41SAndroid Build Coastguard Worker 
50*e4a36f41SAndroid Build Coastguard Worker     if (argc < 3)
51*e4a36f41SAndroid Build Coastguard Worker         usage(argv[0]);
52*e4a36f41SAndroid Build Coastguard Worker     policy = argv[1];
53*e4a36f41SAndroid Build Coastguard Worker     if(!load_policy(policy, &policydb, &pf))
54*e4a36f41SAndroid Build Coastguard Worker         exit(1);
55*e4a36f41SAndroid Build Coastguard Worker     for(i = 0; i < NUM_COMPONENTS; i++) {
56*e4a36f41SAndroid Build Coastguard Worker         if (!strcmp(analyze_components[i].key, argv[2])) {
57*e4a36f41SAndroid Build Coastguard Worker             rc = analyze_components[i].func(argc - 2, argv + 2, &policydb);
58*e4a36f41SAndroid Build Coastguard Worker             if (rc && USAGE_ERROR) {
59*e4a36f41SAndroid Build Coastguard Worker                 usage(argv[0]); }
60*e4a36f41SAndroid Build Coastguard Worker             policydb_destroy(&policydb);
61*e4a36f41SAndroid Build Coastguard Worker             return rc;
62*e4a36f41SAndroid Build Coastguard Worker         }
63*e4a36f41SAndroid Build Coastguard Worker     }
64*e4a36f41SAndroid Build Coastguard Worker     usage(argv[0]);
65*e4a36f41SAndroid Build Coastguard Worker     exit(0);
66*e4a36f41SAndroid Build Coastguard Worker }
67