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 from . import exception 20 from . import qpol 21 from . import symbol 22 from . import user 23 from . import role 24 from . import typeattr 25 from . import mls 26 27 28 def context_factory(policy, name): 29 """Factory function for creating context objects.""" 30 31 if not isinstance(name, qpol.qpol_context_t): 32 raise TypeError("Contexts cannot be looked-up.") 33 34 return Context(policy, name) 35 36 37 class Context(symbol.PolicySymbol): 38 39 """A SELinux security context/security attribute.""" 40 41 def __str__(self): 42 try: 43 return "{0.user}:{0.role}:{0.type_}:{0.range_}".format(self) 44 except exception.MLSDisabled: 45 return "{0.user}:{0.role}:{0.type_}".format(self) 46 47 @property 48 def user(self): 49 """The user portion of the context.""" 50 return user.user_factory(self.policy, self.qpol_symbol.user(self.policy)) 51 52 @property 53 def role(self): 54 """The role portion of the context.""" 55 return role.role_factory(self.policy, self.qpol_symbol.role(self.policy)) 56 57 @property 58 def type_(self): 59 """The type portion of the context.""" 60 return typeattr.type_factory(self.policy, self.qpol_symbol.type_(self.policy)) 61 62 @property 63 def range_(self): 64 """The MLS range of the context.""" 65 return mls.range_factory(self.policy, self.qpol_symbol.range(self.policy)) 66 67 def statement(self): 68 raise exception.NoStatement 69