1# Copyright (C) 2011 Red Hat
2# see file 'COPYING' for use and warranty information
3#
4# setrans is a tool for analyzing process transitions in SELinux policy
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; either version 2 of
9#    the License, or (at your option) any later version.
10#
11#    This program is distributed in the hope that it will be useful,
12#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#    GNU General Public License for more details.
15#
16#    You should have received a copy of the GNU General Public License
17#    along with this program; if not, write to the Free Software
18#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19#                                        02111-1307  USA
20#
21#
22import sepolicy
23__all__ = ['setrans']
24
25
26def _entrypoint(src):
27    trans = sepolicy.search([sepolicy.ALLOW], {sepolicy.SOURCE: src})
28    return map(lambda y: y[sepolicy.TARGET], filter(lambda x: "entrypoint" in x[sepolicy.PERMS], trans))
29
30
31def _get_trans(src):
32    src_list = [src] + list(filter(lambda x: x['name'] == src, sepolicy.get_all_types_info()))[0]['attributes']
33    trans_list = list(filter(lambda x: x['source'] in src_list and x['class'] == 'process', sepolicy.get_all_transitions()))
34    return trans_list
35
36
37class setrans:
38
39    def __init__(self, source, dest=None):
40        self.seen = []
41        self.sdict = {}
42        self.source = source
43        self.dest = dest
44        self._process(self.source)
45
46    def _process(self, source):
47        if source in self.sdict:
48            return self.sdict[source]
49        self.sdict[source] = {}
50        trans = _get_trans(source)
51        if not trans:
52            return
53        self.sdict[source]["name"] = source
54        if not self.dest:
55            self.sdict[source]["map"] = trans
56        else:
57            self.sdict[source]["map"] = list(map(lambda y: y, filter(lambda x: x["transtype"] == self.dest, trans)))
58            self.sdict[source]["child"] = list(map(lambda y: y["transtype"], filter(lambda x: x["transtype"] not in [self.dest, source], trans)))
59            for s in self.sdict[source]["child"]:
60                self._process(s)
61
62    def out(self, name, header=""):
63        buf = ""
64        if name in self.seen:
65            return buf
66        self.seen.append(name)
67
68        if "map" in self.sdict[name]:
69            for t in self.sdict[name]["map"]:
70                cond = sepolicy.get_conditionals(t["source"], t["transtype"], "process", ["transition"])
71                if cond:
72                    buf += "%s%s @ %s --> %s %s\n" % (header, t["source"], t["target"], t["transtype"], sepolicy.get_conditionals_format_text(cond))
73                else:
74                    buf += "%s%s @ %s --> %s\n" % (header, t["source"], t["target"], t["transtype"])
75
76        if "child" in self.sdict[name]:
77            for x in self.sdict[name]["child"]:
78                buf += self.out(x, "%s%s ... " % (header, name))
79        return buf
80
81    def output(self):
82        self.seen = []
83        print(self.out(self.source))
84