aboutsummaryrefslogtreecommitdiff
path: root/tools/sepolicy-analyze/sepolicy-analyze.c
blob: 8c0c423f9874a61bef19e5ca6fd62f6682c24824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stddef.h>
#include <stdio.h>
#include <string.h>

#include "dups.h"
#include "neverallow.h"
#include "perm.h"
#include "typecmp.h"
#include "utils.h"

#define NUM_COMPONENTS (int) (sizeof(analyze_components)/sizeof(analyze_components[0]))

#define COMP(x) { #x, sizeof(#x) - 1, x ##_usage, x ##_func }
static struct {
    const char *key;
    size_t keylen;
    void (*usage) (void);
    int (*func) (int argc, char **argv, policydb_t *policydb);
} analyze_components[] = {
    COMP(dups),
    COMP(neverallow),
    COMP(permissive),
    COMP(typecmp)
};

void usage(char *arg0)
{
    int i;

    fprintf(stderr, "%s must be called on a policy file with a component and the appropriate arguments specified\n", arg0);
    fprintf(stderr, "%s <policy-file>:\n", arg0);
    for(i = 0; i < NUM_COMPONENTS; i++) {
        analyze_components[i].usage();
    }
    exit(1);
}

int main(int argc, char **argv)
{
    char *policy;
    struct policy_file pf;
    policydb_t policydb;
    int rc;
    int i;

    if (argc < 3)
        usage(argv[0]);
    policy = argv[1];
    if(load_policy(policy, &policydb, &pf))
        exit(1);
    for(i = 0; i < NUM_COMPONENTS; i++) {
        if (!strcmp(analyze_components[i].key, argv[2])) {
            rc = analyze_components[i].func(argc - 2, argv + 2, &policydb);
            if (rc && USAGE_ERROR) {
                usage(argv[0]); }
            return rc;
        }
    }
    usage(argv[0]);
    exit(0);
}