aboutsummaryrefslogtreecommitdiffstats
path: root/tools/flask/utils/setenforce.c
blob: 0a92d53184cee71d95ccf3227aa976d74dd4e983 (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
62
63
64
65
66
67
68
69
70
71
72
/*
 *
 *  Authors:  Machon Gregory, <mbgrego@tycho.ncsc.mil>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2,
 *  as published by the Free Software Foundation.
 */

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <xenctrl.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>

static void usage (int argCnt, const char *args[])
{
    fprintf(stderr, "Usage: %s [ (Enforcing|1) | (Permissive|0) ]\n", args[0]);
    exit(1);
}

int main (int argCnt, const char *args[])
{
    int ret = 0;
    xc_interface *xch = 0;
    long mode = 0;
    char *end;

    if (argCnt != 2)
        usage(argCnt, args);

    xch = xc_interface_open(0,0,0);
    if ( !xch )
    {
        fprintf(stderr, "Unable to create interface to xenctrl: %s\n",
                strerror(errno));
        ret = -1;
        goto done;
    }

    if( strlen(args[1]) == 1 && (args[1][0] == '0' || args[1][0] == '1')){
        mode = strtol(args[1], &end, 10);
        ret = xc_flask_setenforce(xch, mode);
    } else {
        if( strcasecmp(args[1], "enforcing") == 0 ){
            ret = xc_flask_setenforce(xch, 1);
        } else if( strcasecmp(args[1], "permissive") == 0 ){
            ret = xc_flask_setenforce(xch, 0);
        } else {
            usage(argCnt, args);
        }
    }

    if ( ret < 0 )
    {
        errno = -ret;
        fprintf(stderr, "Unable to get enforcing mode: %s\n",
                strerror(errno));
        ret = -1;
        goto done;
    }

done:
    if ( xch )
        xc_interface_close(xch);

    return ret;
}