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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#============================================================================
# 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
#============================================================================
# Copyright (C) 2007 International Business Machines Corp.
# Author: Stefan Berger <stefanb@us.ibm.com>
#============================================================================
"""Get the managed policy of the system.
"""
import sys
from xen.util import xsconstants
from xml.dom import minidom
from xen.xm.opts import OptionError
from xen.util.acmpolicy import ACMPolicy
from xen.xm import main as xm_main
from xen.xm.main import server
def help():
return """
Usage: xm getpolicy [options]
The following options are defined
--dumpxml Display the XML of the policy
Get the policy managed by xend."""
def display_policy_info(acmpol, policytype, uuid, version, flags,
dumpxml, xml):
print "Policy name : %s" % acmpol.get_name()
print "Policy type : %s" % policytype
if uuid:
print "Reference : %s" % uuid
print "Version of XML policy : %s" % version
state = []
if flags & xsconstants.XS_INST_LOAD:
state.append("loaded")
if flags & xsconstants.XS_INST_BOOT:
state.append("activated for boot")
print "Policy configuration : %s" % ", ".join(state)
if dumpxml:
if xml:
dom = minidom.parseString(xml.encode("utf-8"))
print "%s" % dom.toprettyxml(indent=" ",newl="\n")
def display_security_subsystems(xstype):
types = []
if xstype & xsconstants.XS_POLICY_ACM:
types.append("ACM")
xstype ^= xsconstants.XS_POLICY_ACM
if xstype != 0:
types.append("unsupported (%08x)" % xstype)
if len(types) == 0:
types.append("None")
print "Supported security subsystems : %s \n" % ", ".join(types)
def getpolicy(dumpxml):
if xm_main.serverType == xm_main.SERVER_XEN_API:
xstype = int(server.xenapi.XSPolicy.get_xstype())
display_security_subsystems(xstype)
policystate = server.xenapi.XSPolicy.get_xspolicy()
if int(policystate['type']) == 0:
print "No policy is installed."
return
if int(policystate['type']) != xsconstants.XS_POLICY_ACM:
print "Unknown policy type '%s'." % policystate['type']
else:
xml = policystate['repr']
acmpol = None
if xml:
acmpol = ACMPolicy(xml=xml)
display_policy_info(acmpol,
xsconstants.ACM_POLICY_ID,
policystate['xs_ref'],
policystate['version'],
int(policystate['flags']),
dumpxml,
xml)
else:
xstype = server.xend.security.get_xstype()
display_security_subsystems(xstype)
xml, flags = server.xend.security.get_policy()
acmpol = None
if xml != "":
dom = None
try:
dom = minidom.parseString(xml)
if dom:
acmpol = ACMPolicy(dom=dom)
except Exception, e:
print "Error parsing the library: " + str(e)
if acmpol:
display_policy_info(acmpol,
xsconstants.ACM_POLICY_ID,
None,
acmpol.get_version(),
flags,
dumpxml,
xml)
else:
print "No policy is installed."
def main(argv):
dumpxml = False
if '--dumpxml' in argv:
dumpxml = True
getpolicy(dumpxml)
if __name__ == '__main__':
try:
main(sys.argv)
except Exception, e:
sys.stderr.write('Error: %s\n' % str(e))
sys.exit(-1)
|