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 PathChoooser(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        for lineno, line in enumerate(open(pathname)):
36            if ignore.match(line): continue
37            mo = consider.match(line)
38            if not mo:
39                raise ValueError, "%s:%d: line is not in key = value format" % (pathname, lineno+1)
40            self.config[mo.group(1)] = mo.group(2)
41
42    # We're only exporting one useful function, so why not be a function
43    def __call__(self, testfilename, pathset="SELINUX_DEVEL_PATH"):
44        paths = self.config.get(pathset, None)
45        if paths is None:
46            raise ValueError, "%s was not in %s" % (pathset, self.config_pathname)
47        paths = paths.split(":")
48        for p in paths:
49            target = os.path.join(p, testfilename)
50            if os.path.exists(target): return target
51        return os.path.join(paths[0], testfilename)
52
53
54"""
55Various default settings, including file and directory locations.
56"""
57
58def data_dir():
59    return "/var/lib/sepolgen"
60
61def perm_map():
62    return data_dir() + "/perm_map"
63
64def interface_info():
65    return data_dir() + "/interface_info"
66
67def attribute_info():
68    return data_dir() + "/attribute_info"
69
70def refpolicy_makefile():
71    chooser = PathChoooser("/etc/selinux/sepolgen.conf")
72    return chooser("Makefile")
73
74def headers():
75    chooser = PathChoooser("/etc/selinux/sepolgen.conf")
76    return chooser("include")
77
78