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