summaryrefslogtreecommitdiffstats
path: root/ipalib/plugins/hbac.py
blob: 6fd2f912e2ddb966110fbe82e1a775c3cfbab033 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Authors:
#   Pavel Zuna <pzuna@redhat.com>
#
# Copyright (C) 2009  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Host based access control
"""

from ipalib import api, errors
from ipalib import GeneralizedTime, Password, Str, StrEnum
from ipalib.plugins.baseldap import *

class hbac(LDAPObject):
    """
    HBAC object.
    """
    container_dn = api.env.container_hbac
    object_name = 'HBAC rule'
    object_name_plural = 'HBAC rules'
    object_class = ['ipaassociation', 'ipahbacrule']
    default_attributes = [
        'cn', 'accessruletype', 'ipaenabledflag', 'servicename',
        'accesstime', 'description',
        
    ]
    uuid_attribute = 'ipauniqueid'
    attribute_names = {
        'cn': 'name',
        'accessruletype': 'type',
        'ipaenabledflag': 'status',
        'servicename': 'service',
        'ipauniqueid': 'unique id',
        'memberuser user': 'affected users',
        'memberuser group': 'affected groups',
        'memberhost host': 'affected hosts',
        'memberhost hostgroup': 'affected hostgroups',
        'sourcehost host': 'affected source hosts',
        'sourcehost hostgroup': 'affected source hostgroups',
    }
    attribute_order = ['cn', 'accessruletype', 'ipaenabledflag', 'servicename']
    attribute_members = {
        'memberuser': ['user', 'group'],
        'memberhost': ['host', 'hostgroup'],
        'sourcehost': ['host', 'hostgroup'],
    }

    takes_params = (
        Str('cn',
            cli_name='name',
            doc='rule name',
            primary_key=True,
        ),
        StrEnum('accessruletype',
            cli_name='type',
            doc='rule type (allow or deny)',
            values=(u'allow', u'deny'),
        ),
        Str('servicename?',
            cli_name='service',
            doc='name of service the rule applies to (e.g. ssh)',
        ),
        GeneralizedTime('accesstime?',
            cli_name='time',
            doc='access time in generalizedTime format (RFC 4517)',
        ),
        Str('description?',
            cli_name='desc',
            doc='description',
        ),
    )

    def get_dn(self, *keys, **kwargs):
        try:
            (dn, entry_attrs) = self.backend.find_entry_by_attr(
                self.primary_key.name, keys[-1], self.object_class, [''],
                self.container_dn
            )
        except errors.NotFound:
            dn = super(hbac, self).get_dn(*keys, **kwargs)
        return dn

    def get_primary_key_from_dn(self, dn):
        pkey = self.primary_key.name
        (dn, entry_attrs) = self.backend.get_entry(dn, [pkey])
        try:
            return entry_attrs[pkey][0]
        except (KeyError, IndexError):
            return ''

api.register(hbac)


class hbac_add(LDAPCreate):
    """
    Create new HBAC rule.
    """
    def pre_callback(self, ldap, dn, entry_attrs, *keys, **options):
        if not dn.startswith('cn='):
            msg = 'HBAC rule with name "%s" already exists' % keys[-1]
            raise errors.DuplicateEntry(message=msg)
        # HBAC rules are enabled by default 
        entry_attrs['ipaenabledflag'] = 'enabled'
        return ldap.make_dn(
            entry_attrs, self.obj.uuid_attribute, self.obj.container_dn
        )

api.register(hbac_add)


class hbac_del(LDAPDelete):
    """
    Delete HBAC rule.
    """

api.register(hbac_del)


class hbac_mod(LDAPUpdate):
    """
    Modify HBAC rule.
    """

api.register(hbac_mod)


class hbac_find(LDAPSearch):
    """
    Search for HBAC rules.
    """

api.register(hbac_find)


class hbac_show(LDAPRetrieve):
    """
    Dispaly HBAC rule.
    """

api.register(hbac_show)


class hbac_enable(LDAPQuery):
    """
    Enable HBAC rule.
    """
    def execute(self, cn):
        ldap = self.obj.backend

        dn = self.obj.get_dn(cn)
        entry_attrs = {'ipaenabledflag': 'enabled'}

        try:
            ldap.update_entry(dn, entry_attrs)
        except errors.EmptyModlist:
            pass

        return True

    def output_for_cli(self, textui, result, cn):
        textui.print_name(self.name)
        textui.print_dashed('Enabled HBAC rule "%s".' % cn)

api.register(hbac_enable)


class hbac_disable(LDAPQuery):
    """
    Disable HBAC rule.
    """
    def execute(self, cn):
        ldap = self.obj.backend

        dn = self.obj.get_dn(cn)
        entry_attrs = {'ipaenabledflag': 'disabled'}

        try:
            ldap.update_entry(dn, entry_attrs)
        except errors.EmptyModlist:
            pass

        return True

    def output_for_cli(self, textui, result, cn):
        textui.print_name(self.name)
        textui.print_dashed('Disabled HBAC rule "%s".' % cn)

api.register(hbac_disable)


class hbac_add_user(LDAPAddMember):
    """
    Add users and groups affected by HBAC rule.
    """
    member_attributes = ['memberuser']
    member_count_out = ('%i object added.', '%i objects added.')

api.register(hbac_add_user)


class hbac_remove_user(LDAPRemoveMember):
    """
    Remove users and groups affected by HBAC rule.
    """
    member_attributes = ['memberuser']
    member_count_out = ('%i object removed.', '%i objects removed.')

api.register(hbac_remove_user)


class hbac_add_host(LDAPAddMember):
    """
    Add hosts and hostgroups affected by HBAC rule.
    """
    member_attributes = ['memberhost']
    member_count_out = ('%i object added.', '%i objects added.')

api.register(hbac_add_host)


class hbac_remove_host(LDAPRemoveMember):
    """
    Remove hosts and hostgroups affected by HBAC rule.
    """
    member_attributes = ['memberhost']
    member_count_out = ('%i object removed.', '%i objects removed.')

api.register(hbac_remove_host)


class hbac_add_sourcehost(LDAPAddMember):
    """
    Add source hosts and hostgroups affected by HBAC rule.
    """
    member_attributes = ['sourcehost']
    member_count_out = ('%i object added.', '%i objects added.')

api.register(hbac_add_sourcehost)


class hbac_remove_sourcehost(LDAPRemoveMember):
    """
    Remove source hosts and hostgroups affected by HBAC rule.
    """
    member_attributes = ['sourcehost']
    member_count_out = ('%i object removed.', '%i objects removed.')

api.register(hbac_remove_sourcehost)