summaryrefslogtreecommitdiffstats
path: root/ipalib/plugins/baseldap.py
blob: b7766cea73c7fcf6898c89e219f60b970a9b88e0 (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
# 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
"""
Base classes for LDAP plugins.
"""

from ipalib import crud, errors
from ipalib import Object
from ipalib import Flag, Str
from ipalib.base import NameSpace


class LDAPObject(Object):
    """
    Object representing a LDAP entry.
    """
    __public__ = frozenset((
        'backend',
        'methods',
        'properties',
        'params',
        'primary_key',
        'parent_key',
        'params_minus_pk',
        'params_minus',
        'get_dn',

        'container_dn',
        'object_name',
        'object_name_plural',
        'parent_object_name',
        'object_class',
        'object_class_config',
        'default_attributes',
    ))
    parent_key = None

    backend_name = 'ldap2'

    container_dn = ''
    object_name = 'entry'
    object_name_plural = 'entries'
    parent_object_name = ''
    object_class = ['top']
    object_class_config = None
    default_attributes = ['']

    def set_api(self, api):
        super(LDAPObject, self).set_api(api)
        parent_keys = filter(lambda p: p.parent_key, self.params())
        if len(parent_keys) > 1:
            raise ValueError(
                '%s (LDAPObject) has multiple parent keys: %s' % (
                    self.name,
                    ', '.join(p.name for p in parent_keys),
                )
            )
        if len(parent_keys) == 1:
            self.parent_key = parent_keys[0]
            self.params_minus_pk = NameSpace(
                filter(
                    lambda p: not p.primary_key and not p.parent_key,
                    self.params()
                ),
                sort=False
            )

    def get_dn(self, *keys, **kwargs):
        if len(keys) > 1:
            parent_dn = self.backend.make_dn_from_attr(
                self.parent_key.name, keys[0], self.container_dn
            )
            return self.backend.make_dn_from_attr(
                self.primary_key.name, keys[1], parent_dn
            )
        return self.backend.make_dn_from_attr(
            self.primary_key.name, keys[0], self.container_dn
        )


class LDAPCreate(crud.Create):
    """
    Create a new entry in LDAP.
    """
    def get_args(self):
        if self.obj.parent_key:
            yield self.obj.parent_key.clone(query=True)
        yield self.obj.primary_key.clone(attribute=True)

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        dn = self.obj.get_dn(*keys, **options)

        entry_attrs = self.args_options_2_entry(*keys, **options)
        entry_attrs['objectclass'] = self.obj.object_class

        if self.obj.object_class_config:
            config = ldap.get_ipa_config()[1]
            entry_attrs['objectclass'] = config.get(
                self.obj.object_class_config, entry_attrs['objectclass']
            )

        dn = self.pre_callback(ldap, dn, entry_attrs, *keys, **options)

        ldap.add_entry(dn, entry_attrs)

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

        dn = self.post_callback(ldap, dn, entry_attrs, *keys, **options)

        return (dn, entry_attrs)

    def output_for_cli(self, textui, entry, *keys, **options):
        (dn, entry_attrs) = entry

        textui.print_name(self.name)
        textui.print_attribute('dn', dn)
        textui.print_entry(entry_attrs)
        if len(keys) > 1:
            textui.print_dashed(
                'Created %s "%s" in %s "%s".' % (
                    self.obj.object_name, keys[1], self.obj.parent_object_name,
                    keys[0]
                )
            )
        else:
            textui.print_dashed(
                'Created %s "%s".' % (self.obj.object_name, keys[0])
            )

    def pre_callback(self, ldap, dn, entry_attrs, *keys, **options):
        return dn

    def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
        return dn


class LDAPRetrieve(crud.Retrieve):
    """
    Retrieve an LDAP entry.
    """
    takes_options = (
        Flag('all',
            cli_name='all',
            doc='retrieve all attributes',
        ),
    )

    def get_args(self):
        if self.obj.parent_key:
            yield self.obj.parent_key.clone(query=True)
        yield self.obj.primary_key.clone(attribute=True, query=True)

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        dn = self.obj.get_dn(*keys, **options)

        if options['all']:
            attrs_list = ['*']
        else:
            attrs_list = list(self.obj.default_attributes)

        dn = self.pre_callback(ldap, dn, attrs_list, *keys, **options)

        (dn, entry_attrs) = ldap.get_entry(dn, attrs_list)

        dn = self.post_callback(ldap, dn, entry_attrs, *keys, **options)

        return (dn, entry_attrs)

    def output_for_cli(self, textui, entry, *keys, **options):
        (dn, entry_attrs) = entry

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

    def pre_callback(self, ldap, dn, attrs_list, *keys, **options):
        return dn

    def post_callback(self, ldap, dn, attrs_list, *keys, **options):
        return dn


class LDAPUpdate(crud.Update):
    """
    Update an LDAP entry.
    """
    def get_args(self):
        if self.obj.parent_key:
            yield self.obj.parent_key.clone(query=True)
        yield self.obj.primary_key.clone(attribute=True, query=True)

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        dn = self.obj.get_dn(*keys, **options)

        entry_attrs = self.args_options_2_entry(**options)

        dn = self.pre_callback(ldap, dn, entry_attrs, *keys, **options)

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

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

        dn = self.post_callback(ldap, dn, entry_attrs, *keys, **options)

        return (dn, entry_attrs)

    def output_for_cli(self, textui, entry, *keys, **options):
        (dn, entry_attrs) = entry

        textui.print_name(self.name)
        textui.print_attribute('dn', dn)
        textui.print_entry(entry_attrs)
        if len(keys) > 1:
            textui.print_dashed(
                'Modified %s "%s" in %s "%s".' % (
                    self.obj.object_name, keys[1], self.obj.parent_object_name,
                    keys[0]
                )
            )
        else:
            textui.print_dashed(
                'Modified %s "%s".' % (self.obj.object_name, keys[0])
            )

    def pre_callback(self, ldap, dn, entry_attrs, *keys, **options):
        return dn

    def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
        return dn


class LDAPDelete(crud.Delete):
    """
    Delete an LDAP entry and all of its direct subentries.
    """
    def get_args(self):
        if self.obj.parent_key:
            yield self.obj.parent_key.clone(query=True)
        yield self.obj.primary_key.clone(attribute=True, query=True)

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        dn = self.obj.get_dn(*keys, **options)

        dn = self.pre_callback(ldap, dn, *keys, **options)

        truncated = True
        while truncated:
            try:
                (subentries, truncated) = ldap.find_entries(
                    None, [''], dn, ldap.SCOPE_ONELEVEL
                )
            except errors.NotFound:
                pass
            else:
                for (dn_, entry_attrs) in subentries:
                    ldap.delete_entry(dn_)

        ldap.delete_entry(dn)

        result = self.post_callback(ldap, dn, *keys, **options)

        return result

    def output_for_cli(self, textui, result, *keys, **options):
        textui.print_name(self.name)
        if len(keys) > 1:
            textui.print_dashed(
                'Deleted %s "%s" in %s "%s".' % (
                    self.obj.object_name, keys[1], self.obj.parent_object_name,
                    keys[0]
                )
            )
        else:
            textui.print_dashed(
                'Deleted %s "%s".' % (self.obj.object_name, keys[0])
            )

    def pre_callback(self, ldap, dn, *keys, **options):
        return dn

    def post_callback(self, ldap, dn, *keys, **options):
        return True


class LDAPSearch(crud.Search):
    """
    Retrieve all LDAP entries matching the given criteria.
    """
    takes_options = (
        Flag('all',
            cli_name='all',
            doc='retrieve all attributes',
        ),
    )

    def get_args(self):
        if self.obj.parent_key:
            yield self.obj.parent_key.clone(query=True)
        yield Str('criteria?')

    def execute(self, *args, **options):
        ldap = self.obj.backend

        base_dn = self.obj.container_dn
        if self.obj.parent_key:
            base_dn = ldap.make_dn_from_attr(
                self.obj.parent_key.name, args[0], base_dn
            )
            term = args[1]
        else:
            term = args[0]

        search_kw = self.args_options_2_entry(**options)

        if options['all']:
            attrs_list = ['*']
        else:
            attrs_list = self.obj.default_attributes + search_kw.keys()

        search_kw['objectclass'] = self.obj.object_class
        attr_filter = ldap.make_filter(search_kw, rules=ldap.MATCH_ALL)

        search_kw = {}
        for a in self.obj.default_attributes:
            search_kw[a] = term
        term_filter = ldap.make_filter(search_kw, exact=False)

        filter = ldap.combine_filters(
            (term_filter, attr_filter), rules=ldap.MATCH_ALL
        )

        filter = self.pre_callback(
            ldap, filter, attrs_list, base_dn, *args, **options
        )

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

        self.post_callback(self, ldap, entries, *args, **options)

        return (entries, truncated)

    def output_for_cli(self, textui, result, *args, **options):
        (entries, truncated) = result

        textui.print_name(self.name)
        for (dn, entry_attrs) in entries:
            textui.print_attribute('dn', dn)
            textui.print_entry(entry_attrs)
            textui.print_plain('')
        textui.print_count(
            len(entries),
            '%i ' + self.obj.object_name + ' matched.',
            '%i ' + self.obj.object_name_plural + ' matched.',
        )
        if truncated:
            textui.print_dashed('These results are truncated.', below=False)
            textui.print_dashed(
                'Please refine your search and try again.', above=False
            )

    def pre_callback(self, ldap, filter, attrs_list, base_dn, *args, **options):
        return filter

    def post_callback(self, ldap, entries, *args, **options):
        pass