aboutsummaryrefslogtreecommitdiffstats
path: root/tools/python/xen/xm/setenforce.py
blob: 38f245d6add9b2e4479bf663bcb1b2ae1f2c831f (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
73
74
#============================================================================
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#============================================================================
# Author: Machon Gregory <mbgrego@tycho.ncsc.mil> 
#============================================================================

"""Modify the current mode of the Flask XSM module.
"""

from xen.xm.opts import OptionError
from xen.xm import main as xm_main
from xen.xm.main import server
from xen.util import xsconstants

def help():
    return """
    Usage: xm setenforce [ Enforcing | Permissive | 1 | 0 ]

    Modifies the current mode of the Flask XSM module to be permissive or 
    enforcing. Using Enforcing or 1 will put the Flask module in enforcing
    mode. Using Permissive or 0 will put the Flask module in permissive 
    mode."""

def setenforce(mode):
    if len(mode) == 1 and ( mode == "0" or mode == "1" ):
        val = int(mode)
    elif mode.lower() == "enforcing":
        val = 1
    elif mode.lower() == "permissive":
        val = 0
    else:
        raise OptionError("%s is an unsupported mode" % mode)
        
    if xm_main.serverType == xm_main.SERVER_XEN_API:
        if xsconstants.XS_POLICY_FLASK != \
                int(server.xenapi.XSPolicy.get_xstype()):
            raise OptionError("Unsupported policy type")
        ret = server.xenapi.XSPolicy.setenforce(val)
    else:
        if server.xend.security.on() != xsconstants.XS_POLICY_FLASK:
            raise OptionError("Unsupported policy type")
        ret = server.xend.security.setenforce(val)

def main(argv): 
    if len(argv) != 2:
        raise OptionError("Invalid arguments")

    if "-?" in argv:
        help()
        return

    mode = argv[1];

    setenforce(mode)

if __name__ == '__main__':
    try:
        main(sys.argv)
    except Exception, e:
        sys.stderr.write('Error: %s\n' % str(e))    
        sys.exit(-1)