summaryrefslogtreecommitdiffstats
path: root/ipaserver/install/plugins/update_idranges.py
blob: badde9453b18edf03943c4edab041f043eb882a2 (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
# Authors:
#   Tomas Babej <tbabej@redhat.com>
#
# Copyright (C) 2013  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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.

from ipalib import api, errors
from ipalib import Updater
from ipapython.dn import DN
from ipapython.ipa_log_manager import *


class update_idrange_type(Updater):
    """
    Update all ID ranges that do not have ipaRangeType attribute filled.
    This applies to all ID ranges prior to IPA 3.3.
    """

    def execute(self, **options):
        ldap = self.api.Backend.ldap2

        base_dn = DN(self.api.env.container_ranges, self.api.env.basedn)
        search_filter = ("(&(objectClass=ipaIDrange)(!(ipaRangeType=*)))")
        root_logger.debug("update_idrange_type: search for ID ranges with no "
                          "type set")

        while True:
            # Run the search in loop to avoid issues when LDAP limits are hit
            # during update

            try:
                (entries, truncated) = ldap.find_entries(search_filter,
                    ['objectclass'], base_dn, time_limit=0, size_limit=0)

            except errors.NotFound:
                root_logger.debug("update_idrange_type: no ID range without "
                                  "type set found")
                return False, []

            except errors.ExecutionError, e:
                root_logger.error("update_idrange_type: cannot retrieve list "
                                  "of ranges with no type set: %s", e)
                return False, []

            if not entries:
                # No entry was returned, rather break than continue cycling
                root_logger.debug("update_idrange_type: no ID range was "
                                  "returned")
                return False, []

            root_logger.debug("update_idrange_type: found %d "
                              "idranges to update, truncated: %s",
                              len(entries), truncated)

            error = False

            # Set the range type
            for entry in entries:
                objectclasses = [o.lower() for o
                                           in entry.get('objectclass', [])]

                if 'ipatrustedaddomainrange' in objectclasses:
                    # NOTICE: assumes every AD range does not use POSIX
                    #         attributes
                    entry['ipaRangeType'] = ['ipa-ad-trust']
                elif 'ipadomainidrange' in objectclasses:
                    entry['ipaRangeType'] = ['ipa-local']
                else:
                    entry['ipaRangeType'] = ['unknown']
                    root_logger.error("update_idrange_type: could not detect "
                                      "range type for entry: %s" % str(entry.dn))
                    root_logger.error("update_idrange_type: ID range type set "
                                      "to 'unknown' for entry: %s" % str(entry.dn))

                try:
                    ldap.update_entry(entry)
                except (errors.EmptyModlist, errors.NotFound):
                    pass
                except errors.ExecutionError, e:
                    root_logger.debug("update_idrange_type: cannot "
                                      "update idrange type: %s", e)
                    error = True

            if error:
                # Exit loop to avoid infinite cycles
                root_logger.error("update_idrange_type: error(s) "
                                  "detected during idrange type update")
                return False, []

            elif not truncated:
                # All affected entries updated, exit the loop
                root_logger.debug("update_idrange_type: all affected idranges "
                                  "were assigned types")
                return False, []

        return False, []


class update_idrange_baserid(Updater):
    """
    Update ipa-ad-trust-posix ranges' base RID to 0. This applies to AD trust
    posix ranges prior to IPA 4.1.
    """

    def execute(self, **options):
        ldap = self.api.Backend.ldap2

        base_dn = DN(self.api.env.container_ranges, self.api.env.basedn)
        search_filter = ("(&(objectClass=ipaTrustedADDomainRange)"
                         "(ipaRangeType=ipa-ad-trust-posix)"
                         "(!(ipaBaseRID=0)))")
        root_logger.debug(
            "update_idrange_baserid: search for ipa-ad-trust-posix ID ranges "
            "with ipaBaseRID != 0"
        )

        try:
            (entries, truncated) = ldap.find_entries(
                search_filter, ['ipabaserid'], base_dn,
                paged_search=True, time_limit=0, size_limit=0)

        except errors.NotFound:
            root_logger.debug("update_idrange_baserid: no AD domain "
                              "range with posix attributes found")
            return False, []

        except errors.ExecutionError, e:
            root_logger.error("update_idrange_baserid: cannot retrieve "
                              "list of affected ranges: %s", e)
            return False, []

        root_logger.debug("update_idrange_baserid: found %d "
                          "idranges possible to update",
                          len(entries))

        error = False

        # Set the range type
        for entry in entries:
            entry['ipabaserid'] = 0
            try:
                root_logger.debug("Updating existing idrange: %s" % (entry.dn))
                ldap.update_entry(entry)
                root_logger.info("Done")
            except (errors.EmptyModlist, errors.NotFound):
                pass
            except errors.ExecutionError, e:
                root_logger.debug("update_idrange_type: cannot "
                                  "update idrange: %s", e)
                error = True

        if error:
            root_logger.error("update_idrange_baserid: error(s) "
                              "detected during idrange baserid update")
        else:
            # All affected entries updated, exit the loop
            root_logger.debug("update_idrange_baserid: all affected "
                              "idranges updated")

        return False, []

api.register(update_idrange_type)
api.register(update_idrange_baserid)