summaryrefslogtreecommitdiff
path: root/lib/python2.7/site-packages/setools/policyrep/objclass.py
blob: bf9a5532c1a9191d7e98d12abc22354651e17be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright 2014-2015, Tresys Technology, LLC
#
# This file is part of SETools.
#
# SETools is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 2.1 of
# the License, or (at your option) any later version.
#
# SETools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with SETools.  If not, see
# <http://www.gnu.org/licenses/>.
#
from . import exception
from . import symbol
from . import qpol


def common_factory(policy, name):
    """Factory function for creating common permission set objects."""

    if isinstance(name, Common):
        assert name.policy == policy
        return name
    elif isinstance(name, qpol.qpol_common_t):
        return Common(policy, name)

    try:
        return Common(policy, qpol.qpol_common_t(policy, str(name)))
    except ValueError:
        raise exception.InvalidCommon("{0} is not a valid common".format(name))


def class_factory(policy, name):
    """Factory function for creating object class objects."""

    if isinstance(name, ObjClass):
        assert name.policy == policy
        return name
    elif isinstance(name, qpol.qpol_class_t):
        return ObjClass(policy, name)

    try:
        return ObjClass(policy, qpol.qpol_class_t(policy, str(name)))
    except ValueError:
        raise exception.InvalidClass("{0} is not a valid object class".format(name))


class Common(symbol.PolicySymbol):

    """A common permission set."""

    def __contains__(self, other):
        return other in self.perms

    @property
    def perms(self):
        """The list of the common's permissions."""
        return set(self.qpol_symbol.perm_iter(self.policy))

    def statement(self):
        return "common {0}\n{{\n\t{1}\n}}".format(self, '\n\t'.join(self.perms))


class ObjClass(Common):

    """An object class."""

    def __contains__(self, other):
        try:
            if other in self.common.perms:
                return True
        except exception.NoCommon:
            pass

        return other in self.perms

    @property
    def common(self):
        """
        The common that the object class inherits.

        Exceptions:
        NoCommon    The object class does not inherit a common.
        """

        try:
            return common_factory(self.policy, self.qpol_symbol.common(self.policy))
        except ValueError:
            raise exception.NoCommon("{0} does not inherit a common.".format(self))

    def statement(self):
        stmt = "class {0}\n".format(self)

        try:
            stmt += "inherits {0}\n".format(self.common)
        except exception.NoCommon:
            pass

        # a class that inherits may not have additional permissions
        perms = self.perms
        if len(perms) > 0:
            stmt += "{{\n\t{0}\n}}".format('\n\t'.join(perms))

        return stmt