summaryrefslogtreecommitdiffstats
path: root/ipalib/plugins/aci.py
blob: be399f25a989029872854d98b7c1b57b739fce30 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# Authors:
#   Rob Crittenden <rcritten@redhat.com>
#   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
"""
Directory Server Access Control Instructions (ACIs)
"""

from ipalib import api, crud, errors
from ipalib import Object, Command
from ipalib import Flag, Int, List, Str, StrEnum
from ipalib.aci import ACI

_type_map = {
    'user': 'ldap:///uid=*,%s,%s' % (api.env.container_user, api.env.basedn),
    'group': 'ldap:///cn=*,%s,%s' % (api.env.container_group, api.env.basedn),
    'host': 'ldap:///cn=*,%s,%s' % (api.env.container_host, api.env.basedn)
}

_valid_permissions_values = [
    u'read', u'write', u'add', u'delete', u'selfwrite', u'all'
]


def _make_aci(current, aciname, kw):
    try:
        (dn, entry_attrs) = api.Command['taskgroup_show'](kw['taskgroup'])
    except errors.NotFound:
        # The task group doesn't exist, let's be helpful and add it
        tgkw = {'description': aciname}
        (dn, entry_attrs) = api.Command['taskgroup_add'](
            kw['taskgroup'], **tgkw
        )

    a = ACI(current)
    a.name = aciname
    a.permissions = kw['permissions']
    a.set_bindrule('groupdn = "ldap:///%s"' % dn)
    if 'attrs' in kw:
        a.set_target_attr(kw['attrs'])
    if 'memberof' in kw:
        (dn, entry_attrs) = api.Command['group_show'](kw['memberof'])
        a.set_target_filter('memberOf=%s' % dn)
    if 'filter' in kw:
        a.set_target_filter(kw['filter'])
    if 'type' in kw:
        target = _type_map[kw['type']]
        a.set_target(target)
    if 'targetgroup' in kw:
        # Purposely no try here so we'll raise a NotFound
        (dn, entry_attrs) = api.Command['group_show'](kw['targetgroup'])
        target = 'ldap:///%s' % dn
        a.set_target(target)
    if 'subtree' in kw:
        # See if the subtree is a full URI
        target = kw['subtree']
        if not target.startswith('ldap:///'):
            target = 'ldap:///%s' % target
        a.set_target(target)

    return a

def _convert_strings_to_acis(acistrs):
    acis = []
    for a in acistrs:
        try:
            acis.append(ACI(a))
        except SyntaxError, e:
            # FIXME: need to log syntax errors, ignore for now
            pass
    return acis

def _find_aci_by_name(acis, aciname):
    for a in acis:
        if a.name.lower() == aciname.lower():
            return a
    raise errors.NotFound('ACI with name "%s" not found' % aciname)

def _normalize_permissions(permissions):
    valid_permissions = []
    permissions = permissions.split(',')
    for p in permissions:
        p = p.strip().lower()
        if p in _valid_permissions_values and p not in valid_permissions:
            valid_permissions.append(p)
    return ','.join(valid_permissions)


class aci(Object):
    """
    ACI object.
    """
    takes_params = (
        Str('aciname',
            cli_name='name',
            doc='name',
            primary_key=True,
        ),
        Str('taskgroup',
            cli_name='taskgroup',
            doc='taskgroup ACI grants access to',
        ),
        List('permissions',
            cli_name='permissions',
            doc='comma-separated list of permissions to grant' \
                '(read, write, add, delete, selfwrite, all)',
            normalizer=_normalize_permissions,
        ),
        List('attrs?',
            cli_name='attrs',
            doc='comma-separated list of attributes',
        ),
        StrEnum('type?',
            cli_name='type',
            doc='type of IPA object (user, group, host)',
            values=(u'user', u'group', u'host'),
        ),
        Str('memberof?',
            cli_name='memberof',
            doc='member of a group',
        ),
        Str('filter?',
            cli_name='filter',
            doc='legal LDAP filter (e.g. ou=Engineering)',
        ),
        Str('subtree?',
            cli_name='subtree',
            doc='subtree to apply ACI to',
        ),
        Str('targetgroup?',
            cli_name='targetgroup',
            doc='group to apply ACI to',
        ),
    )

api.register(aci)


class aci_add(crud.Create):
    """
    Create new ACI.
    """
    def execute(self, aciname, **kw):
        """
        Execute the aci-create operation.

        Returns the entry as it will be created in LDAP.

        :param aciname: The name of the ACI being added.
        :param kw: Keyword arguments for the other LDAP attributes.
        """
        assert 'aciname' not in kw
        ldap = self.api.Backend.ldap2

        newaci = _make_aci(None, aciname, kw)

        (dn, entry_attrs) = ldap.get_entry(self.api.env.basedn, ['aci'])

        acis = _convert_strings_to_acis(entry_attrs.get('aci', []))
        for a in acis:
            if a.isequal(newaci):
                raise errors.DuplicateEntry()

        newaci_str = str(newaci)
        entry_attrs['acis'].append(newaci_str)

        ldap.update_entry(dn, entry_attrs)

        return newaci_str

    def output_for_cli(self, textui, result, aciname, **options):
        textui.print_name(self.name)
        textui.print_plain(result)
        textui.print_dashed('Created ACI "%s".' % aciname)

api.register(aci_add)


class aci_del(crud.Delete):
    """
    Delete ACI.
    """
    def execute(self, aciname, **kw):
        """
        Execute the aci-delete operation.

        :param aciname: The name of the ACI being added.
        :param kw: unused
        """
        assert 'aciname' not in kw
        ldap = self.api.Backend.ldap2

        (dn, entry_attrs) = ldap.get_entry(self.api.env.basedn, ['aci'])

        acistrs = entry_attrs.get('aci', [])
        acis = _convert_strings_to_acis(acistrs)
        aci = _find_aci_by_name(acis, aciname)
        for a in acistrs:
            if aci.isequal(a):
                acistrs.remove(a)
                break

        entry_attrs['aci'] = acistrs

        ldap.update_entry(dn, entry_attrs)

        return True

    def output_for_cli(self, textui, result, aciname, **options):
        """
        Output result of this command to command line interface.
        """
        textui.print_name(self.name)
        textui.print_plain('Deleted ACI "%s".' % aciname)

api.register(aci_del)


class aci_mod(crud.Update):
    """
    Modify ACI.
    """
    def execute(self, aciname, **kw):
        ldap = self.api.Backend.ldap2
 
        (dn, entry_attrs) = ldap.get_entry(self.api.env.basedn, ['aci'])

        acis = _convert_strings_to_acis(entry_attrs.get('aci', []))
        aci = _find_aci_by_name(acis, aciname)

        kw.setdefault('aciname', aci.name)
        kw.setdefault('taskgroup', aci.bindrule['expression'])
        kw.setdefault('permissions', aci.permissions)
        kw.setdefault('attrs', aci.target['targetattr']['expression'])
        if 'type' not in kw and 'targetgroup' not in kw and 'subtree' not in kw:
            kw['subtree'] = aci.target['target']['expression']
        if 'memberof' not in kw and 'filter' not in kw:
            kw['filter'] = aci.target['targetfilter']['expression']

        self.api.Command['aci_del'](aciname)

        return self.api.Command['aci_add'](aciname, **kw)

    def output_for_cli(self, textui, result, aciname, **options):
        textui.print_name(self.name)
        textui.print_plain(result)
        textui.print_dashed('Modified ACI "%s".' % aciname)

api.register(aci_mod)


class aci_find(crud.Search):
    """
    Search for ACIs.
    """
    def execute(self, term, **kw):
        ldap = self.api.Backend.ldap2

        (dn, entry_attrs) = ldap.get_entry(self.api.env.basedn, ['aci'])

        acis = _convert_strings_to_acis(entry_attrs.get('aci', []))
        results = []

        if term:
            term = term.lower()
            for a in acis:
                if a.name.lower().find(term) != -1 and a not in results:
                    results.append(a)
            acis = list(results)
        else:
            results = list(acis)

        if 'aciname' in kw:
            for a in acis:
                if a.name != kw['aciname']:
                    results.remove(a)
            acis = list(results)

        if 'attrs' in kw:
            for a in acis:
                alist1 = sorted(
                    [t.lower() for t in a.target['targetattr']['expression']]
                )
                alist2 = sorted([t.lower() for t in kw['attrs']])
                if alist1 != alist2:
                    results.remove(a)
            acis = list(results)

        if 'taskgroup' in kw:
            try:
                (dn, entry_attrs) = self.api.Command['taskgroup_show'](
                    kw['taskgroup']
                )
            except errors.NotFound:
                pass
            else:
                for a in acis:
                    if a.bindrule['expression'] != ('ldap:///%s' % dn):
                        results.remove(a)
                acis = list(results)

        if 'permissions' in kw:
            for a in acis:
                alist1 = sorted(a.permissions)
                alist2 = sorted(kw['permissions'])
                if alist1 != alist2:
                    results.remove(a)
                acis = list(results)

        if 'memberof' in kw:
            try:
                (dn, entry_attrs) = self.api.Command['group_show'](
                    kw['memberof']
                )
            except errors.NotFound:
                pass
            else:
                memberof_filter = '(memberOf=%s)' % dn
                for a in acis:
                    if 'targetfilter' in a.target:
                        filter = a.target['targetfilter']['expression']
                        if filter != memberof_filter:
                            results.remove(a)
                    else:
                        results.remove(a)
                # uncomment next line if you add more search criteria
                # acis = list(results)

        # TODO: searching by: type, filter, subtree

        return [str(aci) for aci in results]

    def output_for_cli(self, textui, result, term, **options):
        textui.print_name(self.name)
        for aci in result:
            textui.print_plain(aci)
            textui.print_plain('')
        textui.print_count(
            len(result), '%i ACI matched.', '%i ACIs matched.'
        )

api.register(aci_find)


class aci_show(crud.Retrieve):
    """
    Display ACI.
    """
    def execute(self, aciname, **kw):
        """
        Execute the aci-show operation.

        Returns the entry

        :param uid: The login name of the user to retrieve.
        :param kw: unused
        """
        ldap = self.api.Backend.ldap2

        (dn, entry_attrs) = ldap.get_entry(self.api.env.basedn, ['aci'])

        acis = _convert_strings_to_acis(entry_attrs.get('aci', []))

        return str(_find_aci_by_name(acis, aciname))

    def output_for_cli(self, textui, result, aciname, **options):
        textui.print_name(self.name)
        textui.print_plain(result)

api.register(aci_show)