1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Copyright 2014-2015, Tresys Technology, LLC 2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# This file is part of SETools. 4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# SETools is free software: you can redistribute it and/or modify 6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# it under the terms of the GNU Lesser General Public License as 7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# published by the Free Software Foundation, either version 2.1 of 8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# the License, or (at your option) any later version. 9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# SETools is distributed in the hope that it will be useful, 11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# but WITHOUT ANY WARRANTY; without even the implied warranty of 12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# GNU Lesser General Public License for more details. 14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# You should have received a copy of the GNU Lesser General Public 16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# License along with SETools. If not, see 17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# <http://www.gnu.org/licenses/>. 18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport logging 20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport re 21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom .query import PolicyQuery 23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom .descriptors import CriteriaDescriptor, CriteriaSetDescriptor 24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom .mixins import MatchObjClass 25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass DefaultQuery(MatchObjClass, PolicyQuery): 28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Query default_* statements. 31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Parameter: 33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep policy The policy to query. 34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Keyword Parameters/Class attributes: 36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ruletype The rule type(s) to match. 37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep tclass The object class(es) to match. 38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep tclass_regex If true, use a regular expression for 39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep matching the rule's object class. 40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep default The default to base new contexts (e.g. "source" or "target") 41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep default_range The range to use on new context, default_range only 42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ("low", "high", "low_high") 43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ruletype = CriteriaSetDescriptor(lookup_function="validate_default_ruletype") 46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep default = CriteriaDescriptor(lookup_function="validate_default_value") 47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep default_range = CriteriaDescriptor(lookup_function="validate_default_range") 48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, policy, **kwargs): 50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep super(DefaultQuery, self).__init__(policy, **kwargs) 51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.log = logging.getLogger(__name__) 52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def results(self): 54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Generator which yields all matching default_* statements.""" 55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.log.info("Generating default_* results from {0.policy}".format(self)) 56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.log.debug("Ruletypes: {0.ruletype}".format(self)) 57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._match_object_class_debug(self.log) 58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.log.debug("Default: {0.default}".format(self)) 59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.log.debug("Range: {0.default_range}".format(self)) 60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for d in self.policy.defaults(): 62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.ruletype and d.ruletype not in self.ruletype: 63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep continue 64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not self._match_object_class(d): 66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep continue 67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.default and d.default != self.default: 69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep continue 70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.default_range: 72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if d.default_range != self.default_range: 74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep continue 75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except AttributeError: 76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep continue 77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep yield d 79