direct-io.hg

view tools/python/xen/xm/rmlabel.py @ 11688:432bf17b3b09

[XM] Fix rmlabel filename assignment

The patch fixes a bug in the rmlabel tool.

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
author Alastair Tse <atse@xensource.com>
date Tue Oct 03 11:24:48 2006 +0100 (2006-10-03)
parents 3dea280880e2
children 58144f4b102c
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 from xen.util import dictio
23 from xen.util import security
24 from xen.xm.opts import OptionError
26 def help():
27 return """
28 Example: xm rmlabel dom <configfile>
29 xm rmlabel res <resource>
31 This program removes an acm_label entry from the 'configfile'
32 for a domain or from the global resource label file for a
33 resource. If the label does not exist for the given domain or
34 resource, then rmlabel fails."""
37 def rm_resource_label(resource):
38 """Removes a resource label from the global resource label file.
39 """
40 # read in the resource file
41 file = security.res_label_filename
42 try:
43 access_control = dictio.dict_read("resources", file)
44 except:
45 raise security.ACMError("Resource file not found, cannot remove label!")
47 # remove the entry and update file
48 if access_control.has_key(resource):
49 del access_control[resource]
50 dictio.dict_write(access_control, "resources", file)
51 else:
52 raise security.ACMError("Resource not labeled")
55 def rm_domain_label(configfile):
56 # open the domain config file
57 fd = None
58 file = None
59 if configfile[0] == '/':
60 file = configfile
61 fd = open(file, "rb")
62 else:
63 for prefix in [".", "/etc/xen"]:
64 file = prefix + "/" + configfile
65 if os.path.isfile(file):
66 fd = open(file, "rb")
67 break
68 if not fd:
69 raise OptionError("Configuration file '%s' not found." % configfile)
71 # read in the domain config file, removing label
72 ac_entry_re = re.compile("^access_control\s*=.*", re.IGNORECASE)
73 ac_exit_re = re.compile(".*'\].*")
74 file_contents = ""
75 comment = 0
76 removed = 0
77 for line in fd.readlines():
78 if ac_entry_re.match(line):
79 comment = 1
80 if comment:
81 removed = 1
82 line = "#"+line
83 if comment and ac_exit_re.match(line):
84 comment = 0
85 file_contents = file_contents + line
86 fd.close()
88 # send error message if we didn't find anything to remove
89 if not removed:
90 raise security.ACMError('Domain not labeled')
92 # write the data back out to the file
93 fd = open(file, "wb")
94 fd.writelines(file_contents)
95 fd.close()
98 def main (argv):
100 if len(argv) != 3:
101 raise OptionError('Requires 2 arguments')
103 if argv[1].lower() not in ('dom', 'res'):
104 raise OptionError('Unrecognised type argument: %s' % argv[1])
106 if argv[1].lower() == "dom":
107 configfile = argv[2]
108 rm_domain_label(configfile)
109 elif argv[1].lower() == "res":
110 resource = argv[2]
111 rm_resource_label(resource)
113 if __name__ == '__main__':
114 try:
115 main(sys.argv)
116 except Exception, e:
117 sys.stderr.write('Error: %s\n' % str(e))
118 sys.exit(-1)