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

« back to all changes in this revision

Viewing changes to tools/python/xen/xend/XendXSPolicy.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) 2007 IBM Corporation
 
16
# Copyright (c) 2006 Xensource
 
17
#============================================================================
 
18
 
 
19
import base64
 
20
import logging
 
21
from xen.xend import XendDomain
 
22
from xen.xend.XendBase import XendBase
 
23
from xen.xend.XendError import *
 
24
from xen.xend.XendAPIConstants import *
 
25
from xen.xend.XendXSPolicyAdmin import XSPolicyAdminInstance
 
26
from xen.util import xsconstants
 
27
import xen.util.xsm.xsm as security
 
28
 
 
29
log = logging.getLogger("xend.XendXSPolicy")
 
30
log.setLevel(logging.TRACE)
 
31
 
 
32
 
 
33
class XendXSPolicy(XendBase):
 
34
    """ Administration class for an XSPolicy. """
 
35
 
 
36
    def getClass(self):
 
37
        return "XSPolicy"
 
38
 
 
39
    def getMethods(self):
 
40
        methods = ['activate_xspolicy']
 
41
        return XendBase.getMethods() + methods
 
42
 
 
43
    def getFuncs(self):
 
44
        funcs = [ 'get_xstype',
 
45
                  'set_xspolicy',
 
46
                  'reset_xspolicy',
 
47
                  'get_xspolicy',
 
48
                  'rm_xsbootpolicy',
 
49
                  'get_resource_label',
 
50
                  'set_resource_label',
 
51
                  'get_labeled_resources',
 
52
                  'can_run',
 
53
                  'getenforce',
 
54
                  'setenforce']
 
55
        return XendBase.getFuncs() + funcs
 
56
 
 
57
    getClass    = classmethod(getClass)
 
58
    getMethods  = classmethod(getMethods)
 
59
    getFuncs    = classmethod(getFuncs)
 
60
 
 
61
    def __init__(self, xspol, record, uuid):
 
62
        """ xspol = actual XSPolicy  object """
 
63
        self.xspol = xspol
 
64
        XendBase.__init__(self, uuid, record)
 
65
 
 
66
    def get_record(self):
 
67
        xspol_record = {
 
68
          'uuid'   : self.get_uuid(),
 
69
          'flags'  : XSPolicyAdminInstance().get_policy_flags(self.xspol),
 
70
          'repr'   : self.xspol.toxml(),
 
71
          'type'   : self.xspol.get_type(),
 
72
        }
 
73
        return xspol_record
 
74
 
 
75
    def get_xstype(self):
 
76
        return XSPolicyAdminInstance().isXSEnabled()
 
77
 
 
78
    def set_xspolicy(self, xstype, policy, flags, overwrite):
 
79
        ref = ""
 
80
        xstype = int(xstype)
 
81
        flags  = int(flags)
 
82
 
 
83
        polstate = { 'xs_ref': "", 'repr'   : "", 'type'   : 0,
 
84
                     'flags' : 0 , 'version': 0 , 'errors' : "", 'xserr' : 0 }
 
85
        if xstype == xsconstants.XS_POLICY_ACM:
 
86
            poladmin = XSPolicyAdminInstance()
 
87
            try:
 
88
                (xspol, rc, errors) = poladmin.add_acmpolicy_to_system(
 
89
                                                                   policy, flags,
 
90
                                                                   overwrite)
 
91
                if rc != 0:
 
92
                    polstate.update( { 'xserr' : rc,
 
93
                                       'errors': base64.b64encode(errors) } )
 
94
                else:
 
95
                    ref = xspol.get_ref()
 
96
                    polstate = {
 
97
                      'xs_ref' : ref,
 
98
                      'flags'  : poladmin.get_policy_flags(xspol),
 
99
                      'type'   : xstype,
 
100
                      'repr'   : "",
 
101
                      'version': xspol.get_version(),
 
102
                      'errors' : base64.b64encode(errors),
 
103
                      'xserr'  : rc,
 
104
                    }
 
105
            except Exception, e:
 
106
                raise
 
107
        elif xstype == xsconstants.XS_POLICY_FLASK:
 
108
            rc, errors = security.set_policy(xstype, policy);
 
109
            if rc != 0:
 
110
                polstate.update( { 'xserr' : -xsconstants.XSERR_POLICY_LOAD_FAILED,
 
111
                                   'errors': errors } )
 
112
            else:
 
113
                polstate.update( { 'xserr' : xsconstants.XSERR_SUCCESS,
 
114
                                   'errors': errors } )
 
115
        else:
 
116
            raise SecurityError(-xsconstants.XSERR_POLICY_TYPE_UNSUPPORTED)
 
117
        return polstate
 
118
 
 
119
 
 
120
    def reset_xspolicy(self, xstype):
 
121
        xstype = int(xstype)
 
122
        polstate = { 'xs_ref': "", 'repr'   : "", 'type'   : 0,
 
123
                     'flags' : 0 , 'version': 0 , 'errors' : "", 'xserr' : 0 }
 
124
        if xstype == xsconstants.XS_POLICY_ACM:
 
125
            poladmin = XSPolicyAdminInstance()
 
126
            try:
 
127
                (xspol, rc, errors) = poladmin.reset_acmpolicy()
 
128
                if rc != 0:
 
129
                    polstate.update( { 'xserr' : rc,
 
130
                                       'errors': base64.b64encode(errors) } )
 
131
                else:
 
132
                    ref = xspol.get_ref()
 
133
                    polstate = {
 
134
                      'xs_ref' : ref,
 
135
                      'flags'  : poladmin.get_policy_flags(xspol),
 
136
                      'type'   : xstype,
 
137
                      'repr'   : "",
 
138
                      'version': xspol.get_version(),
 
139
                      'errors' : base64.b64encode(errors),
 
140
                      'xserr'  : rc,
 
141
                    }
 
142
            except Exception, e:
 
143
                raise
 
144
        else:
 
145
            raise SecurityError(-xsconstants.XSERR_POLICY_TYPE_UNSUPPORTED)
 
146
        return polstate
 
147
 
 
148
 
 
149
    def activate_xspolicy(self, flags):
 
150
        flags = int(flags)
 
151
        rc = -xsconstants.XSERR_GENERAL_FAILURE
 
152
        poladmin = XSPolicyAdminInstance()
 
153
        try:
 
154
            rc = poladmin.activate_xspolicy(self.xspol, flags)
 
155
        except Exception, e:
 
156
            log.info("Activate_policy: %s" % str(e))
 
157
        if rc != flags:
 
158
            raise SecurityError(rc)
 
159
        return flags
 
160
 
 
161
    def get_xspolicy(self):
 
162
        polstate = { 'xs_ref' : "",
 
163
                     'repr'   : "",
 
164
                     'type'   : 0,
 
165
                     'flags'  : 0,
 
166
                     'version': "",
 
167
                     'errors' : "",
 
168
                     'xserr'  : 0 }
 
169
        poladmin = XSPolicyAdminInstance()
 
170
        refs = poladmin.get_policies_refs()
 
171
        # Will return one or no policy
 
172
        if refs and len(refs) > 0:
 
173
            ref = refs[0]
 
174
            xspol = XSPolicyAdminInstance().policy_from_ref(ref)
 
175
            if xspol:
 
176
                polstate = {
 
177
                  'xs_ref' : ref,
 
178
                  'repr'   : xspol.toxml(),
 
179
                  'type'   : xspol.get_type(),
 
180
                  'flags'  : poladmin.get_policy_flags(xspol),
 
181
                  'version': xspol.get_version(),
 
182
                  'errors' : "",
 
183
                  'xserr'  : 0,
 
184
                }
 
185
        return polstate
 
186
 
 
187
    def rm_xsbootpolicy(self):
 
188
        rc = XSPolicyAdminInstance().rm_bootpolicy()
 
189
        if rc != xsconstants.XSERR_SUCCESS:
 
190
            raise SecurityError(rc)
 
191
 
 
192
    def get_labeled_resources(self):
 
193
        return security.get_labeled_resources_xapi()
 
194
 
 
195
    def set_resource_label(self, resource, sec_lab, old_lab):
 
196
        rc = security.set_resource_label_xapi(resource, sec_lab, old_lab)
 
197
        if rc != xsconstants.XSERR_SUCCESS:
 
198
            raise SecurityError(rc)
 
199
 
 
200
    def get_resource_label(self, resource):
 
201
        res = security.get_resource_label_xapi(resource)
 
202
        return res
 
203
 
 
204
    def can_run(self, sec_label):
 
205
        irc = security.validate_label_xapi(sec_label, 'dom')
 
206
        if irc != xsconstants.XSERR_SUCCESS:
 
207
            raise SecurityError(irc)
 
208
        return security.check_can_run(sec_label)
 
209
 
 
210
    def getenforce(self):
 
211
        return security.getenforce()
 
212
 
 
213
    def setenforce(self, mode):
 
214
        return security.setenforce(mode)
 
215
 
 
216
    get_xstype      = classmethod(get_xstype)
 
217
    get_xspolicy    = classmethod(get_xspolicy)
 
218
    set_xspolicy    = classmethod(set_xspolicy)
 
219
    reset_xspolicy  = classmethod(reset_xspolicy)
 
220
    rm_xsbootpolicy = classmethod(rm_xsbootpolicy)
 
221
    set_resource_label = classmethod(set_resource_label)
 
222
    get_resource_label = classmethod(get_resource_label)
 
223
    get_labeled_resources = classmethod(get_labeled_resources)
 
224
    can_run = classmethod(can_run)
 
225
    getenforce      = classmethod(getenforce)
 
226
    setenforce      = classmethod(setenforce)
 
227
 
 
228
 
 
229
class XendACMPolicy(XendXSPolicy):
 
230
    """ Administration class of an ACMPolicy """
 
231
 
 
232
    def getClass(self):
 
233
        return "ACMPolicy"
 
234
 
 
235
    def getAttrRO(self):
 
236
        attrRO = [ 'xml',
 
237
                   'map',
 
238
                   'binary',
 
239
                   'header' ]
 
240
        return XendXSPolicy.getAttrRO() + attrRO
 
241
 
 
242
    def getFuncs(self):
 
243
        funcs = [ 'get_enforced_binary', 'get_VM_ssidref' ]
 
244
        return XendBase.getFuncs() + funcs
 
245
 
 
246
    getClass    = classmethod(getClass)
 
247
    getAttrRO   = classmethod(getAttrRO)
 
248
    getFuncs    = classmethod(getFuncs)
 
249
 
 
250
    def __init__(self, acmpol, record, uuid):
 
251
        """ acmpol = actual ACMPolicy object """
 
252
        self.acmpol = acmpol
 
253
        XendXSPolicy.__init__(self, acmpol, record, uuid)
 
254
 
 
255
    def get_record(self):
 
256
        polstate = {
 
257
          'uuid'   : self.get_uuid(),
 
258
          'flags'  : XSPolicyAdminInstance().get_policy_flags(self.acmpol),
 
259
          'repr'   : self.acmpol.toxml(),
 
260
          'type'   : self.acmpol.get_type(),
 
261
        }
 
262
        return polstate
 
263
 
 
264
    def get_header(self):
 
265
        header = {
 
266
          'policyname'   : "", 'policyurl'    : "", 'reference'    : "",
 
267
          'date'         : "", 'namespaceurl' : "", 'version'      : "",
 
268
        }
 
269
        try:
 
270
            header = self.acmpol.get_header_fields_map()
 
271
        except:
 
272
            pass
 
273
        return header
 
274
 
 
275
    def get_xml(self):
 
276
        return self.acmpol.toxml()
 
277
 
 
278
    def get_map(self):
 
279
        return self.acmpol.get_map()
 
280
 
 
281
    def get_binary(self):
 
282
        polbin = self.acmpol.get_bin()
 
283
        return base64.b64encode(polbin)
 
284
 
 
285
    def get_VM_ssidref(self, vm_ref):
 
286
        dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
 
287
        if not dom:
 
288
            raise InvalidHandleError("VM", vm_ref)
 
289
        if dom._stateGet() not in [ XEN_API_VM_POWER_STATE_RUNNING, \
 
290
                                    XEN_API_VM_POWER_STATE_PAUSED ]:
 
291
            raise VMBadState("Domain is not running or paused.")
 
292
        ssid = security.get_ssid(dom.getDomid())
 
293
        if not ssid:
 
294
            raise SecurityError(-xsconstants.XSERR_GENERAL_FAILURE)
 
295
        return ssid[3]
 
296
 
 
297
    def get_enforced_binary(self):
 
298
        polbin = XSPolicyAdminInstance(). \
 
299
                   get_enforced_binary(xsconstants.XS_POLICY_ACM)
 
300
        if polbin:
 
301
            return base64.b64encode(polbin)
 
302
        return None
 
303
 
 
304
    get_enforced_binary = classmethod(get_enforced_binary)
 
305
    get_VM_ssidref = classmethod(get_VM_ssidref)