summaryrefslogtreecommitdiffstats
path: root/ipalib/plugins/basegroup2.py
blob: 79dbfa0bfbc91b3dc89a127f18be553fb6aac795 (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# 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

"""
Base plugin for groups.
"""

from ipalib import api, crud, errors
from ipalib import Command, Object
from ipalib import Flag, Int, List, Str

_default_attributes = ['cn', 'description', 'member', 'memberof']
_default_class = 'groupofnames'


def get_dn_by_attr(ldap, attr, value, object_class, parent_dn=''):
    search_kw = {}
    search_kw[attr] = value
    search_kw['objectclass'] = object_class
    filter = ldap.make_filter(search_kw, rules=ldap.MATCH_ALL)
    (dn, entry_attrs) = ldap.find_entries(filter, [''], base_dn=parent_dn)[0]
    return dn

def find_members(ldap, failed, members, attr, object_class, parent_dn=''):
    """
    Search for a list of members to operate on.

    Returns a tuple of 2 lists: a list of DNs found, a list of errors

    :param ldap: The ldap connection
    :param failed: The current list of failed entries
    :param members: list of members to find DNs for
    :param attr: The primary key attribute (cn, uid, etc)
    :param object_class: type of entry we're looking for
    :param parent_dn: base DN for the search
    """
    found = []
    for m in members:
        if not m: continue
        try:
            member_dn = get_dn_by_attr(ldap, attr, m, object_class, parent_dn)
            found.append(member_dn)
        except errors.NotFound:
            failed.append(m)

    return (found, failed)

def add_members(ldap, completed, members, add_failed, group_dn, memberattr):
    """
    Add members to a group.

    Returns a tuple of the # completed and those that weren't added

    :param ldap: The ldap connection
    :param completed: number of entries successfully added
    :param members: list of member DNs to add
    :param add_failed: members who failed to be added
    :param dn: DN of group to add members to
    :param membetattr: The attribute where members are stored
    """
    for member_dn in members:
        if not member_dn:
            continue
        try:
            ldap.add_entry_to_group(member_dn, group_dn, memberattr)
            completed += 1
        except:
            add_failed.append(member_dn)

    return (completed, add_failed)

def del_members(ldap, completed, members, rem_failed, group_dn, memberattr):
    """
    Remove members from group.

    Returns a tuple of the # completed and those that weren't removed

    :param ldap: The ldap connection
    :param completed: number of entries successfully removed
    :param members: list of member DNs to remove
    :param remove_failed: members who failed to be removed
    :param dn: DN of group to remove members from
    :param membetattr: The attribute where members are stored
    """
    for member_dn in members:
        if not member_dn: continue
        try:
            ldap.remove_entry_from_group(member_dn, group_dn, memberattr)
            completed += 1
        except:
            rem_failed.append(member_dn)

    return (completed, rem_failed)


class basegroup2(Object):
    """
    Basic Group object.
    """
    container = None

    takes_params = (
        Str('cn',
            cli_name='name',
            doc='group name',
            primary_key=True,
            normalizer=lambda value: value.lower(),
        ),
        Str('description',
            cli_name='desc',
            doc='A description of this group',
        ),
    )

    def get_dn(self, ldap, cn):
        """
        Construct group dn from cn.
        """
        assert self.container
        return ldap.make_dn_from_attr('cn', cn, self.container)


class basegroup2_create(crud.Create):
    """
    Create new group.
    """
    base_classes = ('top', _default_class)

    def execute(self, cn, **kw):
        """
        Execute a group add operation.

        The dn should not be passed as a keyword argument as it is constructed
        by this method.

        Returns the entry as it will be created in LDAP.

        :param cn: The name of the group being added.
        :param kw: Keyword arguments for the other LDAP attributes.
        """
        assert 'cn' not in kw
        assert 'dn' not in kw
        assert self.api.env.use_ldap2, 'use_ldap2 is False'
        ldap = self.api.Backend.ldap2
        entry_attrs = self.args_options_2_entry(cn, **kw)
        dn = self.obj.get_dn(ldap, cn)

        if kw.get('objectclass'):
            entry_attrs['objectclass'] = kw['objectclass']
        else:
            entry_attrs['objectclass'] = self.base_classes

        ldap.add_entry(dn, entry_attrs)

        return ldap.get_entry(dn, entry_attrs.keys())

    def output_for_cli(self, textui, result, cn, **options):
        """
        Output result of this command to command line interface.
        """
        (dn, entry_attrs) = result

        textui.print_name(self.name)
        textui.print_attribute('dn', dn)
        textui.print_entry(entry_attrs)
        textui.print_dashed('Created group "%s".' % cn)


class basegroup2_delete(crud.Delete):
    """
    Delete group.
    """
    filter_class = _default_class
    container = None

    def execute(self, cn):
        """
        Delete a group

        The memberOf plugin handles removing the group from any other
        groups.

        :param cn: The name of the group being removed
        :param kw: Unused
        """
        assert self.container
        assert self.api.env.use_ldap2, 'use_ldap2 is False'
        ldap = self.api.Backend.ldap2
        dn = get_dn_by_attr(ldap, 'cn', cn, self.filter_class, self.container)

        ldap.delete_entry(dn)

        return True

    def output_for_cli(self, textui, result, cn):
        """
        Output result of this command to command line interface.
        """
        textui.print_name(self.name)
        textui.print_dashed('Deleted group "%s"' % cn)


class basegroup2_mod(crud.Update):
    """
    Modify group.
    """
    filter_class = _default_class
    container = None

    def execute(self, cn, **kw):
        """
        Execute the group-mod operation.

        The dn should not be passed as a keyword argument as it is constructed
        by this method.

        Returns the entry

        :param cn: The name of the group to update.
        :param kw: Keyword arguments for the other LDAP attributes.
        """
        assert 'cn' not in kw
        assert 'dn' not in kw
        assert self.container
        assert self.filter_class
        assert self.api.env.use_ldap2, 'use_ldap2 is False'
        ldap = self.api.Backend.ldap2
        dn = get_dn_by_attr(ldap, 'cn', cn, self.filter_class, self.container)

        entry_attrs = self.args_options_2_entry(cn, **kw)
        if 'objectclass' in kw:
            entry_attrs['objectclass'] = kw['objectclass']

        ldap.update_entry(dn, entry_attrs)

        return ldap.get_entry(dn, entry_attrs.keys())

    def output_for_cli(self, textui, result, cn, **options):
        """
        Output result of this command to command line interface.
        """
        (dn, entry_attrs) = result

        textui.print_name(self.name)
        textui.print_attribute('dn', dn)
        textui.print_entry(entry_attrs)
        textui.print_dashed('Modified group "%s"' % cn)


class basegroup2_find(crud.Search):
    """
    Search for groups.
    """
    filter_class = _default_class
    searchfields = []
    container = None

    takes_options = (
        Flag('all',
            cli_name='all',
            doc='Retrieve all attributes'
        ),
    )

    def execute(self, term, **kw):
        assert self.api.env.use_ldap2, 'use_ldap2 is False'
        ldap = self.api.Backend.ldap2

        search_kw = self.args_options_2_entry(**kw)
        if self.filter_class:
            search_kw['objectclass'] = self.filter_class
        filter = ldap.make_filter(search_kw, rules=ldap.MATCH_ALL)
        if term:
            if not self.searchfields:
                # Pull the list of searchable attributes out of the IPA config.
                conf = ldap.get_ipa_config()[1]
                search_fields = conf.get('ipagroupsearchfields')[0].split(',')
            else:
                search_fields = self.searchfields

            search_kw = {}
            for s in search_fields:
                search_kw[s] = term
            term_filter = ldap.make_filter(search_kw, exact=False)
            filter = ldap.combine_filters(
                (filter, term_filter), ldap.MATCH_ALL
            )

        if kw['all']:
            attrs_list = ['*']
        else:
            attrs_list = _default_attributes

        parent_dn = self.container or ''

        try:
            entries = ldap.find_entries(filter, attrs_list, parent_dn)
        except errors.NotFound:
            entries = tuple()

        return entries

    def output_for_cli(self, textui, result, criteria, **options):
        textui.print_name(self.name)
        for e in result:
            (dn, entry_attrs) = e
            textui.print_attribute('dn', dn)
            textui.print_entry(entry_attrs)
            textui.print_plain('')
        textui.print_count(
            len(result), '%i group matched.', '%i groups matched.'
        )


class basegroup2_show(crud.Retrieve):
    """
    Display group.
    """
    filter_class = _default_class
    default_attributes = _default_attributes
    container = None

    takes_options = (
        Flag('all',
            doc='Retrieve all attributes'
        ),
    )

    def execute(self, cn, **kw):
        """
        Execute the group-show operation.

        The dn should not be passed as a keyword argument as it is constructed
        by this method.

        Returns the entry

        :param cn: The group name to retrieve.
        :param kw: Not used.
        """
        assert self.container
        assert self.api.env.use_ldap2, 'use_ldap2 is False'
        ldap = self.api.Backend.ldap2
        dn = get_dn_by_attr(ldap, 'cn', cn, self.filter_class, self.container)

        if kw['all']:
            attrs_list = ['*']
        else:
            attrs_list = self.default_attributes

        return ldap.get_entry(dn, attrs_list)

    def output_for_cli(self, textui, result, *args, **options):
        (dn, entry_attrs) = result

        textui.print_name(self.name)
        textui.print_attribute('dn', dn)
        textui.print_entry(entry_attrs)


class basegroup2_add_member(Command):
    """
    Add members to group.
    """
    filter_class = _default_class
    default_attributes = _default_attributes
    container = None

    takes_args = (
        Str('cn',
            cli_name='name',
            doc='group name',
        ),
    )

    takes_options = (
        List('users?',
            cli_name='users',
            doc='comma-separated list of users to add',
        ),
        List('groups?',
            cli_name='groups',
            doc='comma-separated list of user groups to add',
        ),
    )

    def execute(self, cn, **kw):
        """
        Execute the group-add-member operation.

        Returns a tuple containing the number of members added
        and the updated entry.

        :param cn: The group name to add new members to.
        :param kw: groups is a comma-separated list of groups to add
        :param kw: users is a comma-separated list of users to add
        """
        assert self.container
        assert self.api.env.use_ldap2, 'use_ldap2 is False'
        ldap = self.api.Backend.ldap2
        dn = get_dn_by_attr(ldap, 'cn', cn, self.filter_class, self.container)
        to_add = []
        add_failed = []
        completed = 0

        members = kw.get('groups', [])
        (to_add, add_failed) = find_members(
            ldap, add_failed, members, 'cn', 'ipausergroup',
            self.api.env.container_group
        )
        (completed, add_failed) = add_members(
            ldap, completed, to_add, add_failed, dn, 'member'
        )

        members = kw.get('users', [])
        (to_add, add_failed) = find_members(
            ldap, add_failed, members, 'uid', 'posixaccount',
            self.api.env.container_user
        )
        (completed, add_failed) = add_members(
            ldap, completed, to_add, add_failed, dn, 'member'
        )

        return (completed, ldap.get_entry(dn, self.default_attributes))

    def output_for_cli(self, textui, result, *args, **options):
        """
        Output result of this command to command line interface.
        """
        (total, (dn, entry_attrs)) = result

        textui.print_name(self.name)
        textui.print_attribute('dn', dn)
        textui.print_entry(entry_attrs)
        textui.print_count(total, '%i member added.', '%i members added.')


class basegroup2_del_member(Command):
    """
    Remove members from group.
    """
    filter_class = _default_class
    default_attributes = _default_attributes
    container = None

    takes_args = (
        Str('cn',
            cli_name='name',
            doc='group name',
        ),
    )

    takes_options = (
        List('users?',
            cli_name='users',
            doc='comma-separated list of users to remove',
        ),
        List('groups?',
            cli_name='groups',
            doc='comma-separated list of user groups to remove',
        ),
    )

    def execute(self, cn, **kw):
        """
        Execute the group-del-member operation.

        Returns a tuple containing the number of members removed
        and the updated entry.

        :param cn: The group name to add new members to.
        :param kw: groups is a comma-separated list of groups to remove
        :param kw: users is a comma-separated list of users to remove
        """
        assert self.container
        assert self.api.env.use_ldap2, 'use_ldap2 is False'
        ldap = self.api.Backend.ldap2
        dn = get_dn_by_attr(ldap, 'cn', cn, self.filter_class, self.container)
        to_remove = []
        remove_failed = []
        completed = 0

        members = kw.get('groups', [])
        (to_remove, remove_failed) = find_members(
            ldap, remove_failed, members, 'cn', 'ipausergroup',
            self.api.env.container_group
        )
        (completed, remove_failed) = del_members(
            ldap, completed, to_remove, remove_failed, dn, 'member'
        )

        members = kw.get('users', [])
        (to_remove, remove_failed) = find_members(
            ldap, remove_failed, members, 'uid', 'posixaccount',
            self.api.env.container_user
        )
        (completed, remove_failed) = del_members(
            ldap, completed, to_remove, remove_failed, dn, 'member'
        )

        return (completed, ldap.get_entry(dn, self.default_attributes))

    def output_for_cli(self, textui, result, *args, **options):
        """
        Output result of this command to command line interface.
        """
        (total, (dn, entry_attrs)) = result

        textui.print_name(self.name)
        textui.print_attribute('dn', dn)
        textui.print_entry(entry_attrs)
        textui.print_count(total, '%i member removed.', '%i members removed.')