ia64/xen-unstable

view tools/python/xen/xm/rmlabel.py @ 10720:8922c1fbe684

[XM][ACM] Add xm subcommands to work with security resource labels.

This patch adds new xm subcommands to support working with resource
labels. The new subcommands are 'xm resources', 'xm rmlabel', 'xm
getlabel' and 'xm dry-run'. In addition, the 'xm addlabel' subcommand
now uses an updated syntax to support labeling both domains and
resources. See the xm man page for details on each subcommand.

Beyond the new subcommands, this patch allows users to immediately see
when security checks will fail by pushing some basic security checking
into the beginning of 'xm create' and 'xm block-attach'. ACM security
attributes for block devices are added to XenStore in order to support
the final security enforcement, which will be performed in the kernel
and included in a separate patch.

Signed-off-by: Bryan D. Payne <bdpayne@us.ibm.com>
Signed-off-by: Reiner Sailer <sailer@us.ibm.com>
author kfraser@localhost.localdomain
date Mon Jul 10 17:18:07 2006 +0100 (2006-07-10)
parents
children 956e9aaf88c9
line source
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 #============================================================================
19 """Remove a label from a domain configuration file or a resoruce.
20 """
21 import sys, os, re
22 import string
23 import traceback
24 from xen.util import dictio
25 from xen.util import security
27 def usage():
28 print "\nUsage: xm rmlabel dom <configfile>"
29 print " xm rmlabel res <resource>\n"
30 print " This program removes an acm_label entry from the 'configfile'"
31 print " for a domain or from the global resource label file for a"
32 print " resource. If the label does not exist for the given domain or"
33 print " resource, then rmlabel fails.\n"
36 def rm_resource_label(resource):
37 """Removes a resource label from the global resource label file.
38 """
39 # read in the resource file
40 file = security.res_label_filename
41 try:
42 access_control = dictio.dict_read("resources", file)
43 except:
44 security.err("Resource file not found, cannot remove label!")
46 # remove the entry and update file
47 if access_control.has_key(resource):
48 del access_control[resource]
49 dictio.dict_write(access_control, "resources", file)
50 else:
51 security.err("Label does not exist in resource label file.")
54 def rm_domain_label(configfile):
55 # open the domain config file
56 fd = None
57 file = None
58 if configfile[0] == '/':
59 fd = open(configfile, "rb")
60 else:
61 for prefix in [".", "/etc/xen"]:
62 file = prefix + "/" + configfile
63 if os.path.isfile(file):
64 fd = open(file, "rb")
65 break
66 if not fd:
67 security.err("Configuration file '"+configfile+"' not found.")
69 # read in the domain config file, removing label
70 ac_entry_re = re.compile("^access_control\s*=.*", re.IGNORECASE)
71 ac_exit_re = re.compile(".*'\].*")
72 file_contents = ""
73 comment = 0
74 removed = 0
75 for line in fd.readlines():
76 if ac_entry_re.match(line):
77 comment = 1
78 if comment:
79 removed = 1
80 line = "#"+line
81 if comment and ac_exit_re.match(line):
82 comment = 0
83 file_contents = file_contents + line
84 fd.close()
86 # send error message if we didn't find anything to remove
87 if not removed:
88 security.err("Label does not exist in domain configuration file.")
90 # write the data back out to the file
91 fd = open(file, "wb")
92 fd.writelines(file_contents)
93 fd.close()
96 def main (argv):
97 try:
98 if len(argv) != 3:
99 usage()
100 return
102 if argv[1].lower() == "dom":
103 configfile = argv[2]
104 rm_domain_label(configfile)
105 elif argv[1].lower() == "res":
106 resource = argv[2]
107 rm_resource_label(resource)
108 else:
109 usage()
111 except security.ACMError:
112 traceback.print_exc(limit=1)
115 if __name__ == '__main__':
116 main(sys.argv)