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