Home | History | Annotate | Download | only in setools
      1 # Copyright 2014-2015, 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 import logging
     20 
     21 from .descriptors import CriteriaDescriptor
     22 from .mixins import MatchName
     23 from .query import PolicyQuery
     24 
     25 
     26 class BoolQuery(MatchName, PolicyQuery):
     27 
     28     """Query SELinux policy Booleans.
     29 
     30     Parameter:
     31     policy          The policy to query.
     32 
     33     Keyword Parameters/Class attributes:
     34     name            The Boolean name to match.
     35     name_regex      If true, regular expression matching
     36                     will be used on the Boolean name.
     37     default         The default state to match.  If this
     38                     is None, the default state not be matched.
     39     """
     40 
     41     _default = None
     42 
     43     @property
     44     def default(self):
     45         return self._default
     46 
     47     @default.setter
     48     def default(self, value):
     49         if value is None:
     50             self._default = None
     51         else:
     52             self._default = bool(value)
     53 
     54     def __init__(self, policy, **kwargs):
     55         super(BoolQuery, self).__init__(policy, **kwargs)
     56         self.log = logging.getLogger(__name__)
     57 
     58     def results(self):
     59         """Generator which yields all Booleans matching the criteria."""
     60         self.log.info("Generating Boolean results from {0.policy}".format(self))
     61         self._match_name_debug(self.log)
     62         self.log.debug("Default: {0.default}".format(self))
     63 
     64         for boolean in self.policy.bools():
     65             if not self._match_name(boolean):
     66                 continue
     67 
     68             if self.default is not None and boolean.state != self.default:
     69                 continue
     70 
     71             yield boolean
     72