1# Copyright 2014, 2016, Tresys Technology, LLC
2#
3# This file is part of SETools.
4#
5# SETools is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as
7# published by the Free Software Foundation, either version 2.1 of
8# the License, or (at your option) any later version.
9#
10# SETools 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 Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public
16# License along with SETools.  If not, see
17# <http://www.gnu.org/licenses/>.
18#
19from . import exception
20from . import symbol
21from . import objclass
22
23
24class PolicyRule(symbol.PolicySymbol):
25
26    """This is base class for policy rules."""
27
28    extended = False
29
30    def __str__(self):
31        raise NotImplementedError
32
33    def __hash__(self):
34        try:
35            cond = self.conditional
36            cond_block = self.conditional_block
37        except exception.RuleNotConditional:
38            cond = None
39            cond_block = None
40
41        return hash("{0.ruletype}|{0.source}|{0.target}|{0.tclass}|{1}|{2}".format(
42            self, cond, cond_block))
43
44    @property
45    def ruletype(self):
46        """The rule type for the rule."""
47        return self.qpol_symbol.rule_type(self.policy)
48
49    @property
50    def source(self):
51        """
52        The source for the rule. This should be overridden by
53        subclasses.
54        """
55        raise NotImplementedError
56
57    @property
58    def target(self):
59        """
60        The target for the rule. This should be overridden by
61        subclasses.
62        """
63        raise NotImplementedError
64
65    @property
66    def tclass(self):
67        """The object class for the rule."""
68        return objclass.class_factory(self.policy, self.qpol_symbol.object_class(self.policy))
69
70    @property
71    def default(self):
72        """
73        The default for the rule. This should be overridden by
74        subclasses.
75        """
76        raise NotImplementedError
77
78    @property
79    def conditional(self):
80        """The conditional expression for this rule."""
81        # Most rules cannot be conditional.
82        raise exception.RuleNotConditional
83
84    @property
85    def conditional_block(self):
86        """The conditional block of the rule (T/F)"""
87        # Most rules cannot be conditional.
88        raise exception.RuleNotConditional
89
90    def expand(self):
91        """Expand the rule into an equivalent set of rules without attributes."""
92        raise NotImplementedError
93
94    def statement(self):
95        return str(self)
96