1# Authors: Karl MacMillan <kmacmillan@mentalrootkit.com> 2# 3# Copyright (C) 2006 Red Hat 4# see file 'COPYING' for use and warranty information 5# 6# This program is free software; you can redistribute it and/or 7# modify it under the terms of the GNU General Public License as 8# published by the Free Software Foundation; version 2 only 9# 10# This program is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with this program; if not, write to the Free Software 17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18# 19 20import os 21import re 22 23# Select the correct location for the development files based on a 24# path variable (optionally read from a configuration file) 25class PathChooser(object): 26 def __init__(self, pathname): 27 self.config = dict() 28 if not os.path.exists(pathname): 29 self.config_pathname = "(defaults)" 30 self.config["SELINUX_DEVEL_PATH"] = "/usr/share/selinux/default:/usr/share/selinux/mls:/usr/share/selinux/devel" 31 return 32 self.config_pathname = pathname 33 ignore = re.compile(r"^\s*(?:#.+)?$") 34 consider = re.compile(r"^\s*(\w+)\s*=\s*(.+?)\s*$") 35 with open(pathname, "r") as fd: 36 for lineno, line in enumerate(fd): 37 if ignore.match(line): continue 38 mo = consider.match(line) 39 if not mo: 40 raise ValueError("%s:%d: line is not in key = value format" % (pathname, lineno+1)) 41 self.config[mo.group(1)] = mo.group(2) 42 43 # We're only exporting one useful function, so why not be a function 44 def __call__(self, testfilename, pathset="SELINUX_DEVEL_PATH"): 45 paths = self.config.get(pathset, None) 46 if paths is None: 47 raise ValueError("%s was not in %s" % (pathset, self.config_pathname)) 48 paths = paths.split(":") 49 for p in paths: 50 target = os.path.join(p, testfilename) 51 if os.path.exists(target): return target 52 return os.path.join(paths[0], testfilename) 53 54 55""" 56Various default settings, including file and directory locations. 57""" 58 59def data_dir(): 60 return "/var/lib/sepolgen" 61 62def perm_map(): 63 return data_dir() + "/perm_map" 64 65def interface_info(): 66 return data_dir() + "/interface_info" 67 68def attribute_info(): 69 return data_dir() + "/attribute_info" 70 71def refpolicy_makefile(): 72 chooser = PathChooser("/etc/selinux/sepolgen.conf") 73 result = chooser("Makefile") 74 if not os.path.exists(result): 75 result = chooser("include/Makefile") 76 return result 77 78def headers(): 79 chooser = PathChooser("/etc/selinux/sepolgen.conf") 80 return chooser("include") 81 82