Home | History | Annotate | Download | only in policyrep
      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 #
     19 from . import exception
     20 from . import symbol
     21 from . import objclass
     22 
     23 
     24 class PolicyRule(symbol.PolicySymbol):
     25 
     26     """This is base class for policy rules."""
     27 
     28     def __str__(self):
     29         raise NotImplementedError
     30 
     31     def __hash__(self):
     32         try:
     33             cond = self.conditional
     34             cond_block = self.conditional_block
     35         except exception.RuleNotConditional:
     36             cond = None
     37             cond_block = None
     38 
     39         return hash("{0.ruletype}|{0.source}|{0.target}|{0.tclass}|{1}|{2}".format(
     40             self, cond, cond_block))
     41 
     42     @property
     43     def ruletype(self):
     44         """The rule type for the rule."""
     45         return self.qpol_symbol.rule_type(self.policy)
     46 
     47     @property
     48     def source(self):
     49         """
     50         The source for the rule. This should be overridden by
     51         subclasses.
     52         """
     53         raise NotImplementedError
     54 
     55     @property
     56     def target(self):
     57         """
     58         The target for the rule. This should be overridden by
     59         subclasses.
     60         """
     61         raise NotImplementedError
     62 
     63     @property
     64     def tclass(self):
     65         """The object class for the rule."""
     66         return objclass.class_factory(self.policy, self.qpol_symbol.object_class(self.policy))
     67 
     68     @property
     69     def default(self):
     70         """
     71         The default for the rule. This should be overridden by
     72         subclasses.
     73         """
     74         raise NotImplementedError
     75 
     76     @property
     77     def conditional(self):
     78         """The conditional expression for this rule."""
     79         # Most rules cannot be conditional.
     80         raise exception.RuleNotConditional
     81 
     82     @property
     83     def conditional_block(self):
     84         """The conditional block of the rule (T/F)"""
     85         # Most rules cannot be conditional.
     86         raise exception.RuleNotConditional
     87 
     88     def expand(self):
     89         """Expand the rule into an equivalent set of rules without attributes."""
     90         raise NotImplementedError
     91 
     92     def statement(self):
     93         return str(self)
     94