1 # Copyright 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 collections import namedtuple 20 21 from .context import ContextWrapper 22 from .descriptors import DiffResultDescriptor 23 from .difference import Difference, Wrapper 24 25 26 modified_fsuse_record = namedtuple("modified_fsuse", ["rule", 27 "added_context", 28 "removed_context"]) 29 30 31 class FSUsesDifference(Difference): 32 33 """Determine the difference in fs_use_* rules between two policies.""" 34 35 added_fs_uses = DiffResultDescriptor("diff_fs_uses") 36 removed_fs_uses = DiffResultDescriptor("diff_fs_uses") 37 modified_fs_uses = DiffResultDescriptor("diff_fs_uses") 38 39 def diff_fs_uses(self): 40 """Generate the difference in fs_use rules between the policies.""" 41 42 self.log.info( 43 "Generating fs_use_* differences from {0.left_policy} to {0.right_policy}". 44 format(self)) 45 46 self.added_fs_uses, self.removed_fs_uses, matched = self._set_diff( 47 (FSUseWrapper(fs) for fs in self.left_policy.fs_uses()), 48 (FSUseWrapper(fs) for fs in self.right_policy.fs_uses())) 49 50 self.modified_fs_uses = [] 51 52 for left_rule, right_rule in matched: 53 # Criteria for modified rules 54 # 1. change to context 55 if ContextWrapper(left_rule.context) != ContextWrapper(right_rule.context): 56 self.modified_fs_uses.append(modified_fsuse_record(left_rule, 57 right_rule.context, 58 left_rule.context)) 59 60 # 61 # Internal functions 62 # 63 def _reset_diff(self): 64 """Reset diff results on policy changes.""" 65 self.log.debug("Resetting fs_use_* rule differences") 66 self.added_fs_uses = None 67 self.removed_fs_uses = None 68 self.modified_fs_uses = None 69 70 71 class FSUseWrapper(Wrapper): 72 73 """Wrap fs_use_* rules to allow set operations.""" 74 75 def __init__(self, rule): 76 self.origin = rule 77 self.ruletype = rule.ruletype 78 self.fs = rule.fs 79 self.context = ContextWrapper(rule.context) 80 self.key = hash(rule) 81 82 def __hash__(self): 83 return self.key 84 85 def __lt__(self, other): 86 return self.key < other.key 87 88 def __eq__(self, other): 89 return self.ruletype == other.ruletype and self.fs == other.fs 90