sepolicy-analyze.c revision ef4fd30672ebfeac1a0ad04f65deb7b38050b818
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 "utils.h"
10
11#define NUM_COMPONENTS (int) (sizeof(analyze_components)/sizeof(analyze_components[0]))
12
13#define COMP(x) { #x, sizeof(#x) - 1, x ##_usage, x ##_func }
14static struct {
15    const char *key;
16    size_t keylen;
17    void (*usage) (void);
18    int (*func) (int argc, char **argv, policydb_t *policydb);
19} analyze_components[] = {
20    COMP(dups),
21    COMP(neverallow),
22    COMP(permissive),
23    COMP(typecmp)
24};
25
26void usage(char *arg0)
27{
28    fprintf(stderr, "%s must be called on a policy file with a component and the appropriate arguments specified\n", arg0);
29    fprintf(stderr, "%s <policy-file>:\n", arg0);
30    for(int i = 0; i < NUM_COMPONENTS; i++) {
31        analyze_components[i].usage();
32    }
33    exit(1);
34}
35
36int main(int argc, char **argv)
37{
38    char *policy;
39    struct policy_file pf;
40    policydb_t policydb;
41    int rc;
42    if (argc < 3)
43        usage(argv[0]);
44    policy = argv[1];
45    if(load_policy(policy, &policydb, &pf))
46        exit(1);
47    for(int i = 0; i < NUM_COMPONENTS; i++) {
48        if (!strcmp(analyze_components[i].key, argv[2])) {
49            rc = analyze_components[i].func(argc - 2, argv + 2, &policydb);
50            if (rc && USAGE_ERROR) {
51                usage(argv[0]); }
52            return rc;
53        }
54    }
55    usage(argv[0]);
56}
57