summaryrefslogtreecommitdiffstats
path: root/ipa-python/ipaclient.py
blob: 426f6681709f9f126f2cfa9cefa1cdc69f30e745 (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
# Authors: Rob Crittenden <rcritten@redhat.com>
#
# Copyright (C) 2007    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 or later
#
# 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
#

#!/usr/bin/python

import sys

import ipa.rpcclient as rpcclient
import entity
import user
import group
import ipa
import config

class IPAClient:

    def __init__(self,transport=None):
        if transport:
            self.local = True
            self.transport = transport
        else:
            self.local = False
            self.transport = rpcclient.RPCClient()

    def set_principal(self,princ):
        """Set the name of the principal that will be used for
           LDAP proxy authentication"""
        if self.local:
            self.transport.set_principal(princ)

    def set_krbccache(self,krbccache):
        """Set the file location of the Kerberos credentials cache to be used
           for LDAP authentication"""
        if self.local:
            self.transport.set_krbccache(krbccache)

# Higher-level API

    def get_aci_entry(self, sattrs=None):
        """Returns the entry containing access control ACIs."""

        result = self.transport.get_aci_entry(sattrs)
        return entity.Entity(result)

# General searches

    def get_entry_by_dn(self,dn,sattrs=None):
        """Get a specific entry by dn. If sattrs is set then only those
           attributes will be returned, otherwise all available attributes
           are returned."""
        result = self.transport.get_entry_by_dn(dn,sattrs)
        return entity.Entity(result)

    def get_entry_by_cn(self,cn,sattrs=None):
        """Get a specific entry by cn. If sattrs is set then only those
           attributes will be returned, otherwise all available attributes
           are returned."""
        result = self.transport.get_entry_by_cn(cn,sattrs)
        return entity.Entity(result)

    def update_entry(self,entry):
        """Update a entry."""

        result = self.transport.update_entry(entry.origDataDict(), entry.toDict())
        return result

# User support
    def get_user_by_uid(self,uid,sattrs=None):
        """Get a specific user by uid. If sattrs is set then only those
           attributes will be returned, otherwise all available attributes
           are returned."""
        result = self.transport.get_user_by_uid(uid,sattrs)
        return user.User(result)

    def get_user_by_principal(self,principal,sattrs=None):
        """Get a specific user by uid. If sattrs is set then only those
           attributes will be returned, otherwise all available attributes
           are returned."""
        result = self.transport.get_user_by_principal(principal,sattrs)
        return user.User(result)

    def get_user_by_email(self,email,sattrs=None):
        """Get a specific user's entry. Return as a dict of values.
           Multi-valued fields are represented as lists.
        """
        result = self.transport.get_user_by_email(email,sattrs)
        return user.User(result)

    def get_users_by_manager(self,manager_dn,sattrs=None):
        """Gets the users the report to a particular manager.
           If sattrs is not None then only those
           attributes will be returned, otherwise all available
           attributes are returned. The result is a list of groups."""
        results = self.transport.get_users_by_manager(manager_dn, sattrs)

        return map(lambda result: user.User(result), results)

    def add_user(self,user,user_container=None):
        """Add a user. user is a ipa.user.User object"""

        user_dict = user.toDict()

        # convert to a regular dict before sending
        result = self.transport.add_user(user_dict, user_container)
        return result

    def get_all_users(self):
        """Get as a list of User objects all users in the directory"""
        result = self.transport.get_all_users()

        all_users = []
        for attrs in result:
            if attrs is not None:
                all_users.append(user.User(attrs))

        return all_users

    def get_custom_fields(self):
        """Get custom user fields"""
        result = self.transport.get_custom_fields()
        return result

    def set_custom_fields(self, schema):
        """Set custom user fields"""
        result = self.transport.set_custom_fields(schema)
        return result

    def find_users(self, criteria, sattrs=None, searchlimit=0, timelimit=-1):
        """Return a list: counter followed by a User object for each user that
           matches the criteria. If the results are truncated, counter will
           be set to -1"""
        result = self.transport.find_users(criteria, sattrs, searchlimit, timelimit)
        counter = result[0]

        users = [counter]
        for attrs in result[1:]:
            if attrs is not None:
                users.append(user.User(attrs))

        return users

    def update_user(self,user):
        """Update a user entry."""

        result = self.transport.update_user(user.origDataDict(), user.toDict())
        return result

    def delete_user(self,uid):
        """Delete a user entry."""

        result = self.transport.delete_user(uid)
        return result

    def modifyPassword(self,principal,oldpass,newpass):
        """Modify a user's password"""

        result = self.transport.modifyPassword(principal,oldpass,newpass)

        return result

    def mark_user_active(self,uid):
        """Set a user as active by uid."""

        result = self.transport.mark_user_active(uid)
        return result

    def mark_user_inactive(self,uid):
        """Set a user as inactive by uid."""

        result = self.transport.mark_user_inactive(uid)
        return result

# Groups support

    def get_groups_by_member(self,member_dn,sattrs=None):
        """Gets the groups that member_dn belongs to.
           If sattrs is not None then only those
           attributes will be returned, otherwise all available
           attributes are returned. The result is a list of groups."""
        results = self.transport.get_groups_by_member(member_dn,sattrs)

        return map(lambda result: group.Group(result), results)

    def add_group(self,group,group_container=None):
        """Add a group. group is a ipa.group.Group object"""

        group_dict = group.toDict()

        # dn is set on the server-side
        del group_dict['dn']

        # convert to a regular dict before sending
        result = self.transport.add_group(group_dict, group_container)
        return result

    def find_groups(self, criteria, sattrs=None, searchlimit=0, timelimit=-1):
        """Find groups whose cn matches the criteria. Wildcards are 
           acceptable. Returns a list of Group objects."""
        result = self.transport.find_groups(criteria, sattrs, searchlimit, timelimit)
        counter = result[0]

        groups = [counter]
        for attrs in result[1:]:
            if attrs is not None:
                groups.append(group.Group(attrs))

        return groups

    def add_member_to_group(self, member_dn, group_dn):
        """Add a member to an existing group.
        """

        return self.transport.add_member_to_group(member_dn, group_dn)

    def add_members_to_group(self, member_dns, group_dn):
        """Add several members to an existing group.
           member_dns is a list of dns to add

           Returns a list of the dns that were not added.
        """

        return self.transport.add_members_to_group(member_dns, group_dn)

    def remove_member_from_group(self, member_dn, group_dn):
        """Remove a member from an existing group.
        """

        return self.transport.remove_member_from_group(member_dn, group_dn)

    def remove_members_from_group(self, member_dns, group_dn):
        """Remove several members from an existing group.
           member_dns is a list of dns to remove

           Returns a list of the dns that were not removed.
        """

        return self.transport.remove_members_from_group(member_dns, group_dn)

    def add_user_to_group(self, user_uid, group_dn):
        """Add a user to an existing group.
           user is a uid of the user to add
           group is the cn of the group to be added to
        """

        return self.transport.add_user_to_group(user_uid, group_dn)

    def add_users_to_group(self, user_uids, group_dn):
        """Add several users to an existing group.
           user_uids is a list of uids of the users to add

           Returns a list of the user uids that were not added.
        """

        return self.transport.add_users_to_group(user_uids, group_dn)

    def remove_user_from_group(self, user_uid, group_dn):
        """Remove a user from an existing group.
           user is a uid of the user to remove
           group is the cn of the group to be removed from
        """

        return self.transport.remove_user_from_group(user_uid, group_dn)

    def remove_users_from_group(self, user_uids, group_dn):
        """Remove several users from an existing group.
           user_uids is a list of uids of the users to remove

           Returns a list of the user uids that were not removed.
        """

        return self.transport.remove_users_from_group(user_uids, group_dn)

    def add_groups_to_user(self, group_dns, user_dn):
        """Given a list of group dn's add them to the user.

           Returns a list of the group dns that were not added.
        """
        return self.transport.add_groups_to_user(group_dns, user_dn)

    def remove_groups_from_user(self, group_dns, user_dn):
        """Given a list of group dn's remove them from the user.

           Returns a list of the group dns that were not removed.
        """

        return self.transport.remove_groups_from_user(group_dns, user_dn)

    def update_group(self,group):
        """Update a group entry."""

        return self.transport.update_group(group.origDataDict(), group.toDict())

    def delete_group(self,group_dn):
        """Delete a group entry."""

        return self.transport.delete_group(group_dn)

    def add_group_to_group(self, group_cn, tgroup_cn):
        """Add a group to an existing group.
           group_cn is a cn of the group to add
           tgroup_cn is the cn of the group to be added to
        """

        return self.transport.add_group_to_group(group_cn, tgroup_cn)

    def attrs_to_labels(self,attrs):
        """Convert a list of LDAP attributes into a more readable form."""

        return self.transport.attrs_to_labels(attrs)

    def group_members(self, groupdn, attr_list):
        """Do a memberOf search of groupdn and return the attributes in
           attr_list (an empty list returns everything)."""

        results = self.transport.group_members(groupdn, attr_list)

        counter = results[0]

        entries = [counter]
        for e in results[1:]:
            if e is not None:
                entries.append(user.User(e))

        return entries
 
    def mark_group_active(self,cn):
        """Set a group as active by cn."""

        result = self.transport.mark_group_active(cn)
        return result

    def mark_group_inactive(self,cn):
        """Set a group as inactive by cn."""

        result = self.transport.mark_group_inactive(cn)
        return result

# Configuration

    def get_ipa_config(self):
        """Get the IPA configuration"""
        result = self.transport.get_ipa_config()
        return entity.Entity(result)

    def update_ipa_config(self, config):
        """Updates the IPA configuration.

           config is an Entity object.
        """
        result = self.transport.update_ipa_config(config.origDataDict(), config.toDict())
        return result

    def get_password_policy(self):
        """Get the IPA password policy"""
        result = self.transport.get_password_policy()
        return entity.Entity(result)

    def update_password_policy(self, policy):
        """Updates the IPA password policy.

           policy is an Entity object.
        """
        result = self.transport.update_password_policy(policy.origDataDict(), policy.toDict())
        return result

    def add_service_principal(self, princ_name):
        return self.transport.add_service_principal(princ_name)

    def find_service_principal(self, criteria, sattrs=None, searchlimit=0, timelimit=-1):
        """Return a list: counter followed by a Entity object for each host that
           matches the criteria. If the results are truncated, counter will
           be set to -1"""
        result = self.transport.find_service_principal(criteria, sattrs, searchlimit, timelimit)
        counter = result[0]

        hosts = [counter]
        for attrs in result[1:]:
            if attrs is not None:
                hosts.append(entity.Entity(attrs))

        return hosts

    def get_keytab(self, princ_name):
        return self.transport.get_keytab(princ_name)