summaryrefslogtreecommitdiffstats
path: root/ipalib/plugins/group2.py
blob: b4faa6ef823e197b62cedf210cc5e861f207aae2 (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
# 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

"""
Groups of users.
"""

from ipalib import api
from ipalib.plugins.basegroup2 import *

_container_dn = api.env.container_group
_default_attributes = ['cn', 'description', 'gidnumber', 'member', 'memberof']
_default_class = 'ipausergroup'


class group2(basegroup2):
    """
    Group object.
    """
    container = _container_dn

    takes_params = basegroup2.takes_params + (
        Int('gidnumber?',
            cli_name='gid',
            doc='GID (use this option to set it manually)',
        ),
    )

api.register(group2)


class group2_create(basegroup2_create):
    """
    Create new group.
    """
    takes_options = (
        Flag('posix',
             cli_name='posix',
             doc='create as posix group?',
        ),
    )

    def execute(self, cn, **kw):
        """
        Execute the 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.

        No need to explicitly set gidNumber. The dna_plugin will do this
        for us if the value isn't provided by the caller.

        :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

        config = ldap.get_ipa_config()[1]

        kw['objectclass'] = config.get('ipagroupobjectclasses')
        if kw['posix'] or 'gidnumber' in kw:
            kw['objectclass'].append('posixgroup')

        return super(group2_create, self).execute(cn, **kw)

api.register(group2_create)


class group2_delete(basegroup2_delete):
    """
    Delete group.
    """
    container = _container_dn
    filter_class = _default_class

    def execute(self, cn, **kw):
        """
        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.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)

        # Don't allow the default user group to be removed
        try:
            config = ldap.get_ipa_config()[1]
            def_group_cn = config.get('ipadefaultprimarygroup')
            def_group_dn = get_dn_by_attr(
                ldap, 'cn', def_group_cn, self.filter_class, self.container
            )
            if dn == def_group_dn:
                raise errors.DefaultGroup()
        except errors.NotFound:
            pass

        return super(group2_delete, self).execute(cn, **kw)

api.register(group2_delete)


class group2_mod(basegroup2_mod):
    """
    Modify group.
    """
    container = _container_dn
    filter_class = _default_class

    takes_options = (
        Flag('posix',
             cli_name='posix',
             doc='change to posix group',
        ),
    )
    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.api.env.use_ldap2, 'use_ldap2 is False'
        ldap = self.api.Backend.ldap2

        if kw['posix'] or 'gidnumber' in kw:
            dn = get_dn_by_attr(ldap, 'cn', cn, self.filter_class, self.container)
            (dn, entry_attrs) = ldap.get_entry(dn, ['objectclass'])
            if 'posixgroup' in entry_attrs['objectclass']:
                if kw['posix'] in entry_attrs['objectclass']:
                    raise errors.AlreadyPosixGroup()
            else:
                entry_attrs['objectclass'].append('posixgroup')
                kw['objectclass'] = entry_attrs['objectclass']

        return super(group2_mod, self).execute(cn, **kw)

api.register(group2_mod)


class group2_find(basegroup2_find):
    """
    Search for groups.
    """
    default_attributes = _default_attributes
    container = _container_dn
    filter_class = _default_class

    def execute(self, cn, **kw):
        assert self.api.env.use_ldap2, 'use_ldap2 is False'
        return super(group2_find, self).execute(cn, **kw)

api.register(group2_find)


class group2_show(basegroup2_show):
    """
    Display group.
    """
    default_attributes = _default_attributes
    container = _container_dn

    def execute(self, cn, **kw):
        assert self.api.env.use_ldap2, 'use_ldap2 is False'
        return super(group2_show, self).execute(cn, **kw)

api.register(group2_show)


class group2_add_member(basegroup2_add_member):
    """
    Add members to group.
    """
    container = _container_dn

    def execute(self, cn, **kw):
        assert self.api.env.use_ldap2, 'use_ldap2 is False'
        return super(group2_add_member, self).execute(cn, **kw)

api.register(group2_add_member)


class group2_del_member(basegroup2_del_member):
    """
    Remove members from group.
    """
    container = _container_dn

    def execute(self, cn, **kw):
        assert self.api.env.use_ldap2, 'use_ldap2 is False'
        return super(group2_del_member, self).execute(cn, **kw)

api.register(group2_del_member)