~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/python/xen/xm/dry-run.py

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#============================================================================
 
2
# This library is free software; you can redistribute it and/or
 
3
# modify it under the terms of version 2.1 of the GNU Lesser General Public
 
4
# License as published by the Free Software Foundation.
 
5
#
 
6
# This library is distributed in the hope that it will be useful,
 
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
9
# Lesser General Public License for more details.
 
10
#
 
11
# You should have received a copy of the GNU Lesser General Public
 
12
# License along with this library; if not, write to the Free Software
 
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
14
#============================================================================
 
15
# Copyright (C) 2006 International Business Machines Corp.
 
16
# Author: Bryan D. Payne <bdpayne@us.ibm.com>
 
17
#============================================================================
 
18
 
 
19
"""Tests the security settings for a domain and its resources.
 
20
"""
 
21
import sys
 
22
import xen.util.xsm.xsm as security
 
23
from xen.xm import create
 
24
from xen.xend import sxp
 
25
from xen.util import xsconstants
 
26
from xen.xm.opts import OptionError
 
27
 
 
28
def help():
 
29
    return """
 
30
    This program checks each resource listed in the configfile
 
31
    to see if the domain created by the configfile can access
 
32
    the resources.  The status of each resource is listed
 
33
    individually along with the final security decision."""
 
34
 
 
35
 
 
36
def check_domain_label(config, verbose):
 
37
    """All that we need to check here is that the domain label exists and
 
38
       is not null when security is on.  Other error conditions are
 
39
       handled when the config file is parsed.
 
40
    """
 
41
    answer = 0
 
42
    default_label = None
 
43
    secon = 0
 
44
    if security.on() == xsconstants.XS_POLICY_ACM:
 
45
        default_label = security.ssidref2label(security.NULL_SSIDREF)
 
46
        secon = 1
 
47
 
 
48
    # get the domain acm_label
 
49
    dom_label = None
 
50
    dom_name = None
 
51
    for x in sxp.children(config):
 
52
        if sxp.name(x) == 'security':
 
53
            dom_label = sxp.child_value(sxp.name(sxp.child0(x)), 'label')
 
54
        if sxp.name(x) == 'name':
 
55
            dom_name = sxp.child0(x)
 
56
 
 
57
    # sanity check on domain label
 
58
    if verbose:
 
59
        print "Checking domain:"
 
60
    if (not secon) and (not dom_label):
 
61
        answer = 1
 
62
        if verbose:
 
63
            print "   %s: PERMITTED" % (dom_name)
 
64
    elif (secon) and (dom_label) and (dom_label != default_label):
 
65
        answer = 1
 
66
        if verbose:
 
67
            print "   %s: PERMITTED" % (dom_name)
 
68
    else:
 
69
        print "   %s: DENIED" % (dom_name)
 
70
        if not secon:
 
71
            print "   --> Security off, but domain labeled"
 
72
        else:
 
73
            print "   --> Domain not labeled"
 
74
        answer = 0
 
75
 
 
76
    return answer
 
77
 
 
78
def config_security_check(config, verbose):
 
79
    """Checks each resource listed in the config to see if the active
 
80
       policy will permit creation of a new domain using the config.
 
81
       Returns 1 if the config passes all tests, otherwise 0.
 
82
    """
 
83
    answer = 1
 
84
 
 
85
    # get the domain acm_label
 
86
    domain_label = None
 
87
    domain_policy = None
 
88
    for x in sxp.children(config):
 
89
        if sxp.name(x) == 'security':
 
90
            domain_label = sxp.child_value(sxp.name(sxp.child0(x)), 'label')
 
91
            domain_policy = sxp.child_value(sxp.name(sxp.child0(x)), 'policy')
 
92
 
 
93
    # if no domain label, use default
 
94
    if not domain_label and security.on() == xsconstants.XS_POLICY_ACM:
 
95
        try:
 
96
            domain_label = security.ssidref2label(security.NULL_SSIDREF)
 
97
        except:
 
98
            import traceback
 
99
            traceback.print_exc(limit=1)
 
100
            return 0
 
101
        domain_policy = 'NULL'
 
102
    elif not domain_label:
 
103
        domain_label = ""
 
104
        domain_policy = 'NULL'
 
105
 
 
106
    if verbose:
 
107
        print "Checking resources:"
 
108
 
 
109
    # build a list of all resources in the config file
 
110
    resources = []
 
111
    for x in sxp.children(config):
 
112
        if sxp.name(x) == 'device':
 
113
            if sxp.name(sxp.child0(x)) == 'vbd':
 
114
                resources.append(sxp.child_value(sxp.child0(x), 'uname'))
 
115
 
 
116
    # perform a security check on each resource
 
117
    for resource in resources:
 
118
        try:
 
119
            security.res_security_check(resource, domain_label)
 
120
            if verbose:
 
121
                print "   %s: PERMITTED" % (resource)
 
122
 
 
123
        except security.XSMError:
 
124
            print "   %s: DENIED" % (resource)
 
125
            (poltype, res_label, res_policy) = security.get_res_label(resource)
 
126
            if not res_label:
 
127
                res_label = ""
 
128
            print "   --> res: %s (%s:%s)" % (str(res_label),
 
129
                                           str(poltype), str(res_policy))
 
130
            print "   --> dom: %s (%s:%s)" % (str(domain_label),
 
131
                                           str(poltype), str(domain_policy))
 
132
 
 
133
            answer = 0
 
134
 
 
135
    return answer
 
136
 
 
137
 
 
138
def main (argv):
 
139
    if len(argv) != 2:
 
140
        raise OptionError('Invalid number of arguments')
 
141
    
 
142
    passed = 0
 
143
    (opts, config) = create.parseCommandLine(argv)
 
144
    if check_domain_label(config, verbose=1):
 
145
        if config_security_check(config, verbose=1):
 
146
            passed = 1
 
147
    else:
 
148
        print "Checking resources: (skipped)"
 
149
        
 
150
    if passed:
 
151
        print "Dry Run: PASSED"
 
152
    else:
 
153
        print "Dry Run: FAILED"
 
154
        sys.exit(-1)
 
155
 
 
156
if __name__ == '__main__':
 
157
    try:
 
158
        main(sys.argv)
 
159
    except Exception, e:
 
160
        sys.stderr.write('Error: %s\n' % str(e))
 
161
        sys.exit(-1)