2012-02-24 02:30:39 -06:00
|
|
|
# Authors:
|
|
|
|
# Martin Kosek <mkosek@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012 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/>.
|
|
|
|
|
2015-01-06 03:36:06 -06:00
|
|
|
import re
|
2014-06-13 03:20:23 -05:00
|
|
|
import traceback
|
|
|
|
import time
|
|
|
|
|
|
|
|
from ldif import LDIFWriter
|
|
|
|
|
2016-03-03 08:12:19 -06:00
|
|
|
from ipalib import Registry, errors, util
|
2015-03-18 09:46:00 -05:00
|
|
|
from ipalib import Updater
|
Use DN objects instead of strings
* Convert every string specifying a DN into a DN object
* Every place a dn was manipulated in some fashion it was replaced by
the use of DN operators
* Add new DNParam parameter type for parameters which are DN's
* DN objects are used 100% of the time throughout the entire data
pipeline whenever something is logically a dn.
* Many classes now enforce DN usage for their attributes which are
dn's. This is implmented via ipautil.dn_attribute_property(). The
only permitted types for a class attribute specified to be a DN are
either None or a DN object.
* Require that every place a dn is used it must be a DN object.
This translates into lot of::
assert isinstance(dn, DN)
sprinkled through out the code. Maintaining these asserts is
valuable to preserve DN type enforcement. The asserts can be
disabled in production.
The goal of 100% DN usage 100% of the time has been realized, these
asserts are meant to preserve that.
The asserts also proved valuable in detecting functions which did
not obey their function signatures, such as the baseldap pre and
post callbacks.
* Moved ipalib.dn to ipapython.dn because DN class is shared with all
components, not just the server which uses ipalib.
* All API's now accept DN's natively, no need to convert to str (or
unicode).
* Removed ipalib.encoder and encode/decode decorators. Type conversion
is now explicitly performed in each IPASimpleLDAPObject method which
emulates a ldap.SimpleLDAPObject method.
* Entity & Entry classes now utilize DN's
* Removed __getattr__ in Entity & Entity clases. There were two
problems with it. It presented synthetic Python object attributes
based on the current LDAP data it contained. There is no way to
validate synthetic attributes using code checkers, you can't search
the code to find LDAP attribute accesses (because synthetic
attriutes look like Python attributes instead of LDAP data) and
error handling is circumscribed. Secondly __getattr__ was hiding
Python internal methods which broke class semantics.
* Replace use of methods inherited from ldap.SimpleLDAPObject via
IPAdmin class with IPAdmin methods. Directly using inherited methods
was causing us to bypass IPA logic. Mostly this meant replacing the
use of search_s() with getEntry() or getList(). Similarly direct
access of the LDAP data in classes using IPAdmin were replaced with
calls to getValue() or getValues().
* Objects returned by ldap2.find_entries() are now compatible with
either the python-ldap access methodology or the Entity/Entry access
methodology.
* All ldap operations now funnel through the common
IPASimpleLDAPObject giving us a single location where we interface
to python-ldap and perform conversions.
* The above 4 modifications means we've greatly reduced the
proliferation of multiple inconsistent ways to perform LDAP
operations. We are well on the way to having a single API in IPA for
doing LDAP (a long range goal).
* All certificate subject bases are now DN's
* DN objects were enhanced thusly:
- find, rfind, index, rindex, replace and insert methods were added
- AVA, RDN and DN classes were refactored in immutable and mutable
variants, the mutable variants are EditableAVA, EditableRDN and
EditableDN. By default we use the immutable variants preserving
important semantics. To edit a DN cast it to an EditableDN and
cast it back to DN when done editing. These issues are fully
described in other documentation.
- first_key_match was removed
- DN equalty comparison permits comparison to a basestring
* Fixed ldapupdate to work with DN's. This work included:
- Enhance test_updates.py to do more checking after applying
update. Add test for update_from_dict(). Convert code to use
unittest classes.
- Consolidated duplicate code.
- Moved code which should have been in the class into the class.
- Fix the handling of the 'deleteentry' update action. It's no longer
necessary to supply fake attributes to make it work. Detect case
where subsequent update applies a change to entry previously marked
for deletetion. General clean-up and simplification of the
'deleteentry' logic.
- Rewrote a couple of functions to be clearer and more Pythonic.
- Added documentation on the data structure being used.
- Simplfy the use of update_from_dict()
* Removed all usage of get_schema() which was being called prior to
accessing the .schema attribute of an object. If a class is using
internal lazy loading as an optimization it's not right to require
users of the interface to be aware of internal
optimization's. schema is now a property and when the schema
property is accessed it calls a private internal method to perform
the lazy loading.
* Added SchemaCache class to cache the schema's from individual
servers. This was done because of the observation we talk to
different LDAP servers, each of which may have it's own
schema. Previously we globally cached the schema from the first
server we connected to and returned that schema in all contexts. The
cache includes controls to invalidate it thus forcing a schema
refresh.
* Schema caching is now senstive to the run time context. During
install and upgrade the schema can change leading to errors due to
out-of-date cached schema. The schema cache is refreshed in these
contexts.
* We are aware of the LDAP syntax of all LDAP attributes. Every
attribute returned from an LDAP operation is passed through a
central table look-up based on it's LDAP syntax. The table key is
the LDAP syntax it's value is a Python callable that returns a
Python object matching the LDAP syntax. There are a handful of LDAP
attributes whose syntax is historically incorrect
(e.g. DistguishedNames that are defined as DirectoryStrings). The
table driven conversion mechanism is augmented with a table of
hard coded exceptions.
Currently only the following conversions occur via the table:
- dn's are converted to DN objects
- binary objects are converted to Python str objects (IPA
convention).
- everything else is converted to unicode using UTF-8 decoding (IPA
convention).
However, now that the table driven conversion mechanism is in place
it would be trivial to do things such as converting attributes
which have LDAP integer syntax into a Python integer, etc.
* Expected values in the unit tests which are a DN no longer need to
use lambda expressions to promote the returned value to a DN for
equality comparison. The return value is automatically promoted to
a DN. The lambda expressions have been removed making the code much
simpler and easier to read.
* Add class level logging to a number of classes which did not support
logging, less need for use of root_logger.
* Remove ipaserver/conn.py, it was unused.
* Consolidated duplicate code wherever it was found.
* Fixed many places that used string concatenation to form a new
string rather than string formatting operators. This is necessary
because string formatting converts it's arguments to a string prior
to building the result string. You can't concatenate a string and a
non-string.
* Simplify logic in rename_managed plugin. Use DN operators to edit
dn's.
* The live version of ipa-ldap-updater did not generate a log file.
The offline version did, now both do.
https://fedorahosted.org/freeipa/ticket/1670
https://fedorahosted.org/freeipa/ticket/1671
https://fedorahosted.org/freeipa/ticket/1672
https://fedorahosted.org/freeipa/ticket/1673
https://fedorahosted.org/freeipa/ticket/1674
https://fedorahosted.org/freeipa/ticket/1392
https://fedorahosted.org/freeipa/ticket/2872
2012-05-13 06:36:35 -05:00
|
|
|
from ipapython.dn import DN
|
2016-04-27 07:44:17 -05:00
|
|
|
from ipapython import dnsutil
|
2015-12-16 12:04:20 -06:00
|
|
|
from ipapython.ipa_log_manager import root_logger
|
2016-06-13 13:41:24 -05:00
|
|
|
from ipaserver.install import sysupgrade
|
2016-04-28 03:30:05 -05:00
|
|
|
from ipaserver.plugins.dns import dns_container_exists
|
2012-02-24 02:30:39 -06:00
|
|
|
|
2016-03-03 08:12:19 -06:00
|
|
|
register = Registry()
|
2014-06-13 03:20:23 -05:00
|
|
|
|
2016-03-03 08:12:19 -06:00
|
|
|
|
2016-04-25 07:07:16 -05:00
|
|
|
class DNSUpdater(Updater):
|
2016-04-27 05:49:23 -05:00
|
|
|
backup_dir = u'/var/lib/ipa/backup/'
|
|
|
|
# override backup_filename in subclass, it will be mangled by strftime
|
|
|
|
backup_filename = None
|
|
|
|
|
|
|
|
def __init__(self, api):
|
|
|
|
super(DNSUpdater, self).__init__(api)
|
|
|
|
backup_path = u'%s%s' % (self.backup_dir, self.backup_filename)
|
|
|
|
self.backup_path = time.strftime(backup_path)
|
|
|
|
self._ldif_writer = None
|
|
|
|
self._saved_privileges = set() # store privileges only once
|
|
|
|
self.saved_zone_to_privilege = {}
|
|
|
|
|
2016-04-25 07:07:16 -05:00
|
|
|
def version_update_needed(self, target_version):
|
|
|
|
"""Test if IPA DNS version is smaller than target version."""
|
|
|
|
assert isinstance(target_version, int)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return int(self.api.Command['dnsconfig_show'](
|
|
|
|
all=True)['result']['ipadnsversion'][0]) < target_version
|
|
|
|
except errors.NotFound:
|
|
|
|
# IPA DNS is not configured
|
|
|
|
return False
|
|
|
|
|
2016-04-27 05:49:23 -05:00
|
|
|
@property
|
|
|
|
def ldif_writer(self):
|
|
|
|
if not self._ldif_writer:
|
|
|
|
self.log.info('Original zones will be saved in LDIF format in '
|
|
|
|
'%s file' % self.backup_path)
|
|
|
|
self._ldif_writer = LDIFWriter(open(self.backup_path, 'w'))
|
|
|
|
return self._ldif_writer
|
|
|
|
|
|
|
|
def backup_zone(self, zone):
|
|
|
|
"""Backup zone object, its records, permissions, and privileges.
|
|
|
|
|
|
|
|
Mapping from zone to privilege (containing zone's permissions)
|
|
|
|
will be stored in saved_zone_to_privilege dict for further usage.
|
|
|
|
"""
|
|
|
|
dn = str(zone['dn'])
|
|
|
|
del zone['dn'] # dn shouldn't be as attribute in ldif
|
|
|
|
self.ldif_writer.unparse(dn, zone)
|
|
|
|
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
if 'managedBy' in zone:
|
|
|
|
permission = ldap.get_entry(DN(zone['managedBy'][0]))
|
|
|
|
self.ldif_writer.unparse(str(permission.dn), dict(permission.raw))
|
|
|
|
for privilege_dn in permission.get('member', []):
|
|
|
|
# privileges can be shared by multiples zones
|
|
|
|
if privilege_dn not in self._saved_privileges:
|
|
|
|
self._saved_privileges.add(privilege_dn)
|
|
|
|
privilege = ldap.get_entry(privilege_dn)
|
|
|
|
self.ldif_writer.unparse(str(privilege.dn),
|
|
|
|
dict(privilege.raw))
|
|
|
|
|
|
|
|
# remember privileges referened by permission
|
|
|
|
if 'member' in permission:
|
|
|
|
self.saved_zone_to_privilege[
|
|
|
|
zone['idnsname'][0]
|
|
|
|
] = permission['member']
|
|
|
|
|
|
|
|
if 'idnszone' in zone['objectClass']:
|
|
|
|
# raw values are required to store into ldif
|
|
|
|
records = self.api.Command['dnsrecord_find'](zone['idnsname'][0],
|
|
|
|
all=True,
|
|
|
|
raw=True,
|
|
|
|
sizelimit=0)['result']
|
|
|
|
for record in records:
|
|
|
|
if record['idnsname'][0] == u'@':
|
|
|
|
# zone record was saved before
|
|
|
|
continue
|
|
|
|
dn = str(record['dn'])
|
|
|
|
del record['dn']
|
|
|
|
self.ldif_writer.unparse(dn, record)
|
|
|
|
|
2016-04-25 07:07:16 -05:00
|
|
|
|
|
|
|
@register()
|
|
|
|
class update_ipaconfigstring_dnsversion_to_ipadnsversion(Updater):
|
|
|
|
"""
|
|
|
|
IPA <= 4.3.1 used ipaConfigString "DNSVersion 1" on DNS container.
|
|
|
|
This was hard to deal with in API so from IPA 4.3.2 we are using
|
|
|
|
new ipaDNSVersion attribute with integer syntax.
|
|
|
|
Old ipaConfigString is left there for now so if someone accidentally
|
|
|
|
executes upgrade on an old replica again it will not re-upgrade the data.
|
|
|
|
"""
|
|
|
|
def execute(self, **options):
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
dns_container_dn = DN(self.api.env.container_dns, self.api.env.basedn)
|
|
|
|
try:
|
|
|
|
container_entry = ldap.get_entry(dns_container_dn)
|
|
|
|
except errors.NotFound:
|
|
|
|
# DNS container not found, nothing to upgrade
|
|
|
|
return False, []
|
|
|
|
|
|
|
|
if 'ipadnscontainer' in [
|
|
|
|
o.lower() for o in container_entry['objectclass']
|
|
|
|
]:
|
|
|
|
# version data are already migrated
|
|
|
|
return False, []
|
|
|
|
|
|
|
|
self.log.debug('Migrating DNS ipaConfigString to ipaDNSVersion')
|
|
|
|
container_entry['objectclass'].append('ipadnscontainer')
|
|
|
|
version = 0
|
|
|
|
for config_option in container_entry.get("ipaConfigString", []):
|
|
|
|
matched = re.match("^DNSVersion\s+(?P<version>\d+)$",
|
|
|
|
config_option, flags=re.I)
|
|
|
|
if matched:
|
|
|
|
version = int(matched.group("version"))
|
|
|
|
else:
|
|
|
|
self.log.error(
|
|
|
|
'Failed to parse DNS version from ipaConfigString, '
|
|
|
|
'defaulting to version %s', version)
|
|
|
|
container_entry['ipadnsversion'] = version
|
|
|
|
ldap.update_entry(container_entry)
|
|
|
|
self.log.debug('ipaDNSVersion = %s', version)
|
|
|
|
return False, []
|
|
|
|
|
|
|
|
|
2016-03-03 08:12:19 -06:00
|
|
|
@register()
|
2015-03-18 09:46:00 -05:00
|
|
|
class update_dnszones(Updater):
|
2012-02-24 02:30:39 -06:00
|
|
|
"""
|
2012-02-24 09:23:52 -06:00
|
|
|
Update all zones to meet requirements in the new FreeIPA versions
|
|
|
|
|
|
|
|
1) AllowQuery and AllowTransfer
|
2012-02-24 02:30:39 -06:00
|
|
|
Set AllowQuery and AllowTransfer ACLs in all zones that may be configured
|
|
|
|
in an upgraded FreeIPA instance.
|
|
|
|
|
|
|
|
Upgrading to new version of bind-dyndb-ldap and having these ACLs empty
|
|
|
|
would result in a leak of potentially sensitive DNS information as
|
|
|
|
zone transfers are enabled for all hosts if not disabled in named.conf
|
|
|
|
or LDAP.
|
|
|
|
|
|
|
|
This plugin disables the zone transfer by default so that it needs to be
|
|
|
|
explicitly enabled by FreeIPA Administrator.
|
2012-02-24 09:23:52 -06:00
|
|
|
|
|
|
|
2) Update policy
|
|
|
|
SSH public key support includes a feature to automatically add/update
|
|
|
|
client SSH fingerprints in SSHFP records. However, the update won't
|
|
|
|
work for zones created before this support was added as they don't allow
|
|
|
|
clients to update SSHFP records in their update policies.
|
|
|
|
|
|
|
|
This module extends the original policy to allow the SSHFP updates.
|
2012-02-24 02:30:39 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
def execute(self, **options):
|
2015-03-18 09:46:00 -05:00
|
|
|
ldap = self.api.Backend.ldap2
|
2014-07-21 09:54:12 -05:00
|
|
|
if not dns_container_exists(ldap):
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, []
|
2012-02-24 02:30:39 -06:00
|
|
|
|
|
|
|
try:
|
2015-03-19 09:32:21 -05:00
|
|
|
zones = self.api.Command.dnszone_find(all=True)['result']
|
2012-02-24 02:30:39 -06:00
|
|
|
except errors.NotFound:
|
2015-06-08 10:33:11 -05:00
|
|
|
self.log.debug('No DNS zone to update found')
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, []
|
2012-02-24 02:30:39 -06:00
|
|
|
|
|
|
|
for zone in zones:
|
|
|
|
update = {}
|
|
|
|
if not zone.get('idnsallowquery'):
|
|
|
|
# allow query from any client by default
|
|
|
|
update['idnsallowquery'] = u'any;'
|
|
|
|
|
|
|
|
if not zone.get('idnsallowtransfer'):
|
|
|
|
# do not open zone transfers by default
|
|
|
|
update['idnsallowtransfer'] = u'none;'
|
|
|
|
|
2015-03-19 09:32:21 -05:00
|
|
|
old_policy = util.get_dns_forward_zone_update_policy(
|
|
|
|
self.api.env.realm, ('A', 'AAAA'))
|
2012-02-24 09:23:52 -06:00
|
|
|
if zone.get('idnsupdatepolicy', [''])[0] == old_policy:
|
2012-06-04 10:53:34 -05:00
|
|
|
update['idnsupdatepolicy'] = util.get_dns_forward_zone_update_policy(\
|
2015-03-19 09:32:21 -05:00
|
|
|
self.api.env.realm)
|
2012-02-24 09:23:52 -06:00
|
|
|
|
2012-02-24 02:30:39 -06:00
|
|
|
if update:
|
2014-11-07 08:09:29 -06:00
|
|
|
# FIXME: https://fedorahosted.org/freeipa/ticket/4722
|
2015-03-19 09:32:21 -05:00
|
|
|
self.api.Command.dnszone_mod(zone[u'idnsname'][0].make_absolute(),
|
2014-11-07 08:09:29 -06:00
|
|
|
**update)
|
2012-02-24 02:30:39 -06:00
|
|
|
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, []
|
2012-02-24 02:30:39 -06:00
|
|
|
|
2012-05-10 02:28:02 -05:00
|
|
|
|
2016-03-03 08:12:19 -06:00
|
|
|
@register()
|
2015-03-18 09:46:00 -05:00
|
|
|
class update_dns_limits(Updater):
|
2012-05-10 02:28:02 -05:00
|
|
|
"""
|
|
|
|
bind-dyndb-ldap persistent search queries LDAP for all DNS records.
|
|
|
|
The LDAP connection must have no size or time limits to work
|
|
|
|
properly. This plugin updates limits of the existing DNS service
|
|
|
|
principal to match there requirements.
|
|
|
|
"""
|
|
|
|
limit_attributes = ['nsTimeLimit', 'nsSizeLimit', 'nsIdleTimeout', 'nsLookThroughLimit']
|
|
|
|
limit_value = '-1'
|
|
|
|
|
|
|
|
def execute(self, **options):
|
2015-03-18 09:46:00 -05:00
|
|
|
ldap = self.api.Backend.ldap2
|
2012-05-10 02:28:02 -05:00
|
|
|
|
|
|
|
if not dns_container_exists(ldap):
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, []
|
2012-05-10 02:28:02 -05:00
|
|
|
|
|
|
|
dns_principal = 'DNS/%s@%s' % (self.env.host, self.env.realm)
|
Use DN objects instead of strings
* Convert every string specifying a DN into a DN object
* Every place a dn was manipulated in some fashion it was replaced by
the use of DN operators
* Add new DNParam parameter type for parameters which are DN's
* DN objects are used 100% of the time throughout the entire data
pipeline whenever something is logically a dn.
* Many classes now enforce DN usage for their attributes which are
dn's. This is implmented via ipautil.dn_attribute_property(). The
only permitted types for a class attribute specified to be a DN are
either None or a DN object.
* Require that every place a dn is used it must be a DN object.
This translates into lot of::
assert isinstance(dn, DN)
sprinkled through out the code. Maintaining these asserts is
valuable to preserve DN type enforcement. The asserts can be
disabled in production.
The goal of 100% DN usage 100% of the time has been realized, these
asserts are meant to preserve that.
The asserts also proved valuable in detecting functions which did
not obey their function signatures, such as the baseldap pre and
post callbacks.
* Moved ipalib.dn to ipapython.dn because DN class is shared with all
components, not just the server which uses ipalib.
* All API's now accept DN's natively, no need to convert to str (or
unicode).
* Removed ipalib.encoder and encode/decode decorators. Type conversion
is now explicitly performed in each IPASimpleLDAPObject method which
emulates a ldap.SimpleLDAPObject method.
* Entity & Entry classes now utilize DN's
* Removed __getattr__ in Entity & Entity clases. There were two
problems with it. It presented synthetic Python object attributes
based on the current LDAP data it contained. There is no way to
validate synthetic attributes using code checkers, you can't search
the code to find LDAP attribute accesses (because synthetic
attriutes look like Python attributes instead of LDAP data) and
error handling is circumscribed. Secondly __getattr__ was hiding
Python internal methods which broke class semantics.
* Replace use of methods inherited from ldap.SimpleLDAPObject via
IPAdmin class with IPAdmin methods. Directly using inherited methods
was causing us to bypass IPA logic. Mostly this meant replacing the
use of search_s() with getEntry() or getList(). Similarly direct
access of the LDAP data in classes using IPAdmin were replaced with
calls to getValue() or getValues().
* Objects returned by ldap2.find_entries() are now compatible with
either the python-ldap access methodology or the Entity/Entry access
methodology.
* All ldap operations now funnel through the common
IPASimpleLDAPObject giving us a single location where we interface
to python-ldap and perform conversions.
* The above 4 modifications means we've greatly reduced the
proliferation of multiple inconsistent ways to perform LDAP
operations. We are well on the way to having a single API in IPA for
doing LDAP (a long range goal).
* All certificate subject bases are now DN's
* DN objects were enhanced thusly:
- find, rfind, index, rindex, replace and insert methods were added
- AVA, RDN and DN classes were refactored in immutable and mutable
variants, the mutable variants are EditableAVA, EditableRDN and
EditableDN. By default we use the immutable variants preserving
important semantics. To edit a DN cast it to an EditableDN and
cast it back to DN when done editing. These issues are fully
described in other documentation.
- first_key_match was removed
- DN equalty comparison permits comparison to a basestring
* Fixed ldapupdate to work with DN's. This work included:
- Enhance test_updates.py to do more checking after applying
update. Add test for update_from_dict(). Convert code to use
unittest classes.
- Consolidated duplicate code.
- Moved code which should have been in the class into the class.
- Fix the handling of the 'deleteentry' update action. It's no longer
necessary to supply fake attributes to make it work. Detect case
where subsequent update applies a change to entry previously marked
for deletetion. General clean-up and simplification of the
'deleteentry' logic.
- Rewrote a couple of functions to be clearer and more Pythonic.
- Added documentation on the data structure being used.
- Simplfy the use of update_from_dict()
* Removed all usage of get_schema() which was being called prior to
accessing the .schema attribute of an object. If a class is using
internal lazy loading as an optimization it's not right to require
users of the interface to be aware of internal
optimization's. schema is now a property and when the schema
property is accessed it calls a private internal method to perform
the lazy loading.
* Added SchemaCache class to cache the schema's from individual
servers. This was done because of the observation we talk to
different LDAP servers, each of which may have it's own
schema. Previously we globally cached the schema from the first
server we connected to and returned that schema in all contexts. The
cache includes controls to invalidate it thus forcing a schema
refresh.
* Schema caching is now senstive to the run time context. During
install and upgrade the schema can change leading to errors due to
out-of-date cached schema. The schema cache is refreshed in these
contexts.
* We are aware of the LDAP syntax of all LDAP attributes. Every
attribute returned from an LDAP operation is passed through a
central table look-up based on it's LDAP syntax. The table key is
the LDAP syntax it's value is a Python callable that returns a
Python object matching the LDAP syntax. There are a handful of LDAP
attributes whose syntax is historically incorrect
(e.g. DistguishedNames that are defined as DirectoryStrings). The
table driven conversion mechanism is augmented with a table of
hard coded exceptions.
Currently only the following conversions occur via the table:
- dn's are converted to DN objects
- binary objects are converted to Python str objects (IPA
convention).
- everything else is converted to unicode using UTF-8 decoding (IPA
convention).
However, now that the table driven conversion mechanism is in place
it would be trivial to do things such as converting attributes
which have LDAP integer syntax into a Python integer, etc.
* Expected values in the unit tests which are a DN no longer need to
use lambda expressions to promote the returned value to a DN for
equality comparison. The return value is automatically promoted to
a DN. The lambda expressions have been removed making the code much
simpler and easier to read.
* Add class level logging to a number of classes which did not support
logging, less need for use of root_logger.
* Remove ipaserver/conn.py, it was unused.
* Consolidated duplicate code wherever it was found.
* Fixed many places that used string concatenation to form a new
string rather than string formatting operators. This is necessary
because string formatting converts it's arguments to a string prior
to building the result string. You can't concatenate a string and a
non-string.
* Simplify logic in rename_managed plugin. Use DN operators to edit
dn's.
* The live version of ipa-ldap-updater did not generate a log file.
The offline version did, now both do.
https://fedorahosted.org/freeipa/ticket/1670
https://fedorahosted.org/freeipa/ticket/1671
https://fedorahosted.org/freeipa/ticket/1672
https://fedorahosted.org/freeipa/ticket/1673
https://fedorahosted.org/freeipa/ticket/1674
https://fedorahosted.org/freeipa/ticket/1392
https://fedorahosted.org/freeipa/ticket/2872
2012-05-13 06:36:35 -05:00
|
|
|
dns_service_dn = DN(('krbprincipalname', dns_principal),
|
|
|
|
self.env.container_service,
|
|
|
|
self.env.basedn)
|
2012-05-10 02:28:02 -05:00
|
|
|
|
|
|
|
try:
|
2013-10-31 11:55:07 -05:00
|
|
|
entry = ldap.get_entry(dns_service_dn, self.limit_attributes)
|
2012-05-10 02:28:02 -05:00
|
|
|
except errors.NotFound:
|
|
|
|
# this host may not have DNS service set
|
|
|
|
root_logger.debug("DNS: service %s not found, no need to update limits" % dns_service_dn)
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, []
|
2012-05-10 02:28:02 -05:00
|
|
|
|
|
|
|
if all(entry.get(limit.lower(), [None])[0] == self.limit_value for limit in self.limit_attributes):
|
|
|
|
root_logger.debug("DNS: limits for service %s already set" % dns_service_dn)
|
|
|
|
# service is already updated
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, []
|
2012-05-10 02:28:02 -05:00
|
|
|
|
|
|
|
limit_updates = []
|
|
|
|
|
|
|
|
for limit in self.limit_attributes:
|
2015-05-05 08:12:12 -05:00
|
|
|
limit_updates.append(dict(action='only', attr=limit,
|
|
|
|
value=self.limit_value))
|
2012-05-10 02:28:02 -05:00
|
|
|
|
2015-03-06 08:14:17 -06:00
|
|
|
dnsupdate = {'dn': dns_service_dn, 'updates': limit_updates}
|
2012-05-10 02:28:02 -05:00
|
|
|
root_logger.debug("DNS: limits for service %s will be updated" % dns_service_dn)
|
|
|
|
|
|
|
|
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, [dnsupdate]
|
2012-05-10 02:28:02 -05:00
|
|
|
|
2014-06-13 03:20:23 -05:00
|
|
|
|
2016-03-03 08:12:19 -06:00
|
|
|
@register()
|
2016-04-25 07:07:16 -05:00
|
|
|
class update_master_to_dnsforwardzones(DNSUpdater):
|
2014-06-13 03:20:23 -05:00
|
|
|
"""
|
|
|
|
Update all zones to meet requirements in the new FreeIPA versions
|
|
|
|
|
|
|
|
All masters zones with specified forwarders, and forward-policy different
|
|
|
|
than none, will be tranformed to forward zones.
|
|
|
|
Original masters zone will be backed up to ldif file.
|
|
|
|
|
2016-04-28 15:19:03 -05:00
|
|
|
This should be applied only once,
|
|
|
|
and only if original version was lower than 4.0
|
2014-06-13 03:20:23 -05:00
|
|
|
"""
|
2016-04-27 05:49:23 -05:00
|
|
|
backup_filename = u'dns-master-to-forward-zones-%Y-%m-%d-%H-%M-%S.ldif'
|
2014-06-13 03:20:23 -05:00
|
|
|
|
|
|
|
def execute(self, **options):
|
2015-03-18 09:46:00 -05:00
|
|
|
ldap = self.api.Backend.ldap2
|
2015-01-06 03:36:06 -06:00
|
|
|
# check LDAP if forwardzones already uses new semantics
|
2016-04-25 07:07:16 -05:00
|
|
|
if not self.version_update_needed(target_version=1):
|
|
|
|
# forwardzones already uses new semantics,
|
|
|
|
# no upgrade is required
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, []
|
2014-06-13 03:20:23 -05:00
|
|
|
|
2015-06-08 10:33:11 -05:00
|
|
|
self.log.debug('Updating forward zones')
|
2015-01-06 03:36:06 -06:00
|
|
|
# update the DNSVersion, following upgrade can be executed only once
|
2016-04-25 07:07:16 -05:00
|
|
|
self.api.Command['dnsconfig_mod'](ipadnsversion=1)
|
2015-01-06 03:36:06 -06:00
|
|
|
|
|
|
|
# Updater in IPA version from 4.0 to 4.1.2 doesn't work well, this
|
|
|
|
# should detect if update in past has been executed, and set proper
|
|
|
|
# DNSVersion into LDAP
|
|
|
|
try:
|
2015-03-19 09:32:21 -05:00
|
|
|
fwzones = self.api.Command.dnsforwardzone_find()['result']
|
2015-01-06 03:36:06 -06:00
|
|
|
except errors.NotFound:
|
|
|
|
# No forwardzones found, update probably has not been executed yet
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if fwzones:
|
|
|
|
# fwzones exist, do not execute upgrade again
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, []
|
2015-01-06 03:36:06 -06:00
|
|
|
|
2015-01-07 06:21:04 -06:00
|
|
|
zones = []
|
2014-06-13 03:20:23 -05:00
|
|
|
try:
|
|
|
|
# raw values are required to store into ldif
|
2015-03-19 09:32:21 -05:00
|
|
|
zones = self.api.Command.dnszone_find(all=True,
|
2014-06-13 03:20:23 -05:00
|
|
|
raw=True,
|
|
|
|
sizelimit=0)['result']
|
|
|
|
except errors.NotFound:
|
2015-01-07 06:21:04 -06:00
|
|
|
pass
|
|
|
|
|
|
|
|
if not zones:
|
2015-06-08 10:33:11 -05:00
|
|
|
self.log.debug('No DNS zone to update found')
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, []
|
2014-06-13 03:20:23 -05:00
|
|
|
|
|
|
|
zones_to_transform = []
|
|
|
|
|
|
|
|
for zone in zones:
|
|
|
|
if (
|
|
|
|
zone.get('idnsforwardpolicy', [u'first'])[0] == u'none' or
|
|
|
|
zone.get('idnsforwarders', []) == []
|
|
|
|
):
|
|
|
|
continue # don't update zone
|
|
|
|
|
|
|
|
zones_to_transform.append(zone)
|
|
|
|
|
|
|
|
if zones_to_transform:
|
|
|
|
self.log.info('Zones with specified forwarders with policy different'
|
|
|
|
' than none will be transformed to forward zones.')
|
|
|
|
# update
|
|
|
|
for zone in zones_to_transform:
|
2016-04-27 05:49:23 -05:00
|
|
|
try:
|
|
|
|
self.backup_zone(zone)
|
|
|
|
except Exception:
|
|
|
|
self.log.error('Unable to create backup for zone, '
|
|
|
|
'terminating zone upgrade')
|
|
|
|
self.log.error(traceback.format_exc())
|
|
|
|
return False, []
|
|
|
|
|
2014-06-13 03:20:23 -05:00
|
|
|
# delete master zone
|
|
|
|
try:
|
2015-03-19 09:32:21 -05:00
|
|
|
self.api.Command['dnszone_del'](zone['idnsname'])
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2014-06-13 03:20:23 -05:00
|
|
|
self.log.error('Transform to forwardzone terminated: '
|
|
|
|
'removing zone %s failed (%s)' % (
|
|
|
|
zone['idnsname'][0], e)
|
|
|
|
)
|
|
|
|
self.log.error(traceback.format_exc())
|
|
|
|
continue
|
|
|
|
|
|
|
|
# create forward zone
|
|
|
|
try:
|
|
|
|
kw = {
|
|
|
|
'idnsforwarders': zone.get('idnsforwarders', []),
|
2016-04-26 13:09:19 -05:00
|
|
|
'idnsforwardpolicy': zone.get('idnsforwardpolicy',
|
|
|
|
[u'first'])[0],
|
|
|
|
'skip_overlap_check': True,
|
2014-06-13 03:20:23 -05:00
|
|
|
}
|
2015-03-19 09:32:21 -05:00
|
|
|
self.api.Command['dnsforwardzone_add'](zone['idnsname'][0], **kw)
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2014-06-13 03:20:23 -05:00
|
|
|
self.log.error('Transform to forwardzone terminated: creating '
|
|
|
|
'forwardzone %s failed' %
|
|
|
|
zone['idnsname'][0])
|
|
|
|
self.log.error(traceback.format_exc())
|
|
|
|
continue
|
|
|
|
|
|
|
|
# create permission if original zone has one
|
|
|
|
if 'managedBy' in zone:
|
|
|
|
try:
|
2015-03-19 09:32:21 -05:00
|
|
|
perm_name = self.api.Command['dnsforwardzone_add_permission'](
|
2014-07-03 08:50:27 -05:00
|
|
|
zone['idnsname'][0])['value']
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2014-06-13 03:20:23 -05:00
|
|
|
self.log.error('Transform to forwardzone terminated: '
|
|
|
|
'Adding managed by permission to forward zone'
|
|
|
|
' %s failed' % zone['idnsname'])
|
|
|
|
self.log.error(traceback.format_exc())
|
|
|
|
self.log.info('Zone %s was transformed to forward zone '
|
|
|
|
' without managed permissions',
|
|
|
|
zone['idnsname'][0])
|
|
|
|
continue
|
|
|
|
|
2014-07-03 08:50:27 -05:00
|
|
|
else:
|
2016-04-27 05:49:23 -05:00
|
|
|
if zone['idnsname'][0] in self.saved_zone_to_privilege:
|
2014-07-03 08:50:27 -05:00
|
|
|
privileges = [
|
2016-04-27 05:49:23 -05:00
|
|
|
dn[0].value for dn in self.saved_zone_to_privilege[zone['idnsname'][0]]
|
2014-07-03 08:50:27 -05:00
|
|
|
]
|
|
|
|
try:
|
2015-03-19 09:32:21 -05:00
|
|
|
self.api.Command['permission_add_member'](perm_name,
|
2014-07-03 08:50:27 -05:00
|
|
|
privilege=privileges)
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2014-07-03 08:50:27 -05:00
|
|
|
self.log.error('Unable to restore privileges for '
|
|
|
|
'permission %s, for zone %s'
|
|
|
|
% (perm_name, zone['idnsname']))
|
|
|
|
self.log.error(traceback.format_exc())
|
|
|
|
self.log.info('Zone %s was transformed to forward zone'
|
|
|
|
' without restored privileges',
|
|
|
|
zone['idnsname'][0])
|
|
|
|
continue
|
|
|
|
|
2015-06-08 10:33:11 -05:00
|
|
|
self.log.debug('Zone %s was sucessfully transformed to forward zone',
|
2014-06-13 03:20:23 -05:00
|
|
|
zone['idnsname'][0])
|
|
|
|
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, []
|
2016-04-27 07:44:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
@register()
|
|
|
|
class update_dnsforward_emptyzones(DNSUpdater):
|
|
|
|
"""
|
|
|
|
Migrate forward policies which conflict with automatic empty zones
|
|
|
|
(RFC 6303) to use forward policy = only.
|
|
|
|
|
|
|
|
BIND ignores conflicting forwarding configuration
|
|
|
|
when forwarding policy != only.
|
|
|
|
bind-dyndb-ldap 9.0+ will do the same so we have to adjust FreeIPA zones
|
|
|
|
accordingly.
|
|
|
|
"""
|
|
|
|
backup_filename = u'dns-forwarding-empty-zones-%Y-%m-%d-%H-%M-%S.ldif'
|
|
|
|
|
|
|
|
def update_zones(self):
|
|
|
|
try:
|
|
|
|
fwzones = self.api.Command.dnsforwardzone_find(all=True,
|
|
|
|
raw=True)['result']
|
|
|
|
except errors.NotFound:
|
|
|
|
# No forwardzones found, we are done
|
|
|
|
return
|
|
|
|
|
|
|
|
logged_once = False
|
|
|
|
for zone in fwzones:
|
|
|
|
if not (
|
|
|
|
dnsutil.related_to_auto_empty_zone(
|
|
|
|
dnsutil.DNSName(zone.get('idnsname')[0]))
|
|
|
|
and zone.get('idnsforwardpolicy', [u'first'])[0] != u'only'
|
|
|
|
and zone.get('idnsforwarders', []) != []
|
|
|
|
):
|
|
|
|
# this zone does not conflict with automatic empty zone
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not logged_once:
|
|
|
|
self.log.info('Forward policy for zones conflicting with '
|
|
|
|
'automatic empty zones will be changed to '
|
|
|
|
'"only"')
|
|
|
|
logged_once = True
|
|
|
|
|
|
|
|
# backup
|
|
|
|
try:
|
|
|
|
self.backup_zone(zone)
|
|
|
|
except Exception:
|
|
|
|
self.log.error('Unable to create backup for zone %s, '
|
|
|
|
'terminating zone upgrade', zone['idnsname'][0])
|
|
|
|
self.log.error(traceback.format_exc())
|
|
|
|
continue
|
|
|
|
|
|
|
|
# change forward policy
|
|
|
|
try:
|
|
|
|
self.api.Command['dnsforwardzone_mod'](
|
|
|
|
zone['idnsname'][0],
|
|
|
|
idnsforwardpolicy=u'only'
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
self.log.error('Forward policy update for zone %s failed '
|
|
|
|
'(%s)' % (zone['idnsname'][0], e))
|
|
|
|
self.log.error(traceback.format_exc())
|
|
|
|
continue
|
|
|
|
|
|
|
|
self.log.debug('Zone %s was sucessfully modified to use '
|
|
|
|
'forward policy "only"', zone['idnsname'][0])
|
|
|
|
|
2016-04-27 08:24:01 -05:00
|
|
|
def update_global_ldap_forwarder(self):
|
|
|
|
config = self.api.Command['dnsconfig_show'](all=True,
|
|
|
|
raw=True)['result']
|
|
|
|
if (
|
|
|
|
config.get('idnsforwardpolicy', [u'first'])[0] == u'first'
|
|
|
|
and config.get('idnsforwarders', [])
|
|
|
|
):
|
|
|
|
self.log.info('Global forward policy in LDAP for all servers will '
|
|
|
|
'be changed to "only" to avoid conflicts with '
|
|
|
|
'automatic empty zones')
|
|
|
|
self.backup_zone(config)
|
|
|
|
self.api.Command['dnsconfig_mod'](idnsforwardpolicy=u'only')
|
|
|
|
|
2016-04-27 07:44:17 -05:00
|
|
|
def execute(self, **options):
|
|
|
|
# check LDAP if DNS subtree already uses new semantics
|
|
|
|
if not self.version_update_needed(target_version=2):
|
|
|
|
# forwardzones already use new semantics, no upgrade is required
|
|
|
|
return False, []
|
|
|
|
|
2016-04-28 15:19:03 -05:00
|
|
|
self.log.debug('Updating forwarding policies in LDAP '
|
|
|
|
'to avoid conflicts with automatic empty zones')
|
2016-04-27 07:44:17 -05:00
|
|
|
# update the DNSVersion, following upgrade can be executed only once
|
|
|
|
self.api.Command['dnsconfig_mod'](ipadnsversion=2)
|
|
|
|
|
|
|
|
self.update_zones()
|
2016-04-27 08:24:01 -05:00
|
|
|
if dnsutil.has_empty_zone_addresses(self.api.env.host):
|
|
|
|
self.update_global_ldap_forwarder()
|
|
|
|
|
2016-04-27 07:44:17 -05:00
|
|
|
return False, []
|
2016-06-13 13:41:24 -05:00
|
|
|
|
|
|
|
|
|
|
|
@register()
|
|
|
|
class update_dnsserver_configuration_into_ldap(DNSUpdater):
|
|
|
|
"""
|
|
|
|
DNS Locations feature requires to have DNS configuration stored in LDAP DB.
|
|
|
|
Create DNS server configuration in LDAP for each old server
|
|
|
|
"""
|
|
|
|
def execute(self, **options):
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
if sysupgrade.get_upgrade_state('dns', 'server_config_to_ldap'):
|
|
|
|
self.log.debug('upgrade is not needed')
|
|
|
|
return False, []
|
|
|
|
|
|
|
|
dns_container_dn = DN(self.api.env.container_dns, self.api.env.basedn)
|
|
|
|
try:
|
|
|
|
ldap.get_entry(dns_container_dn)
|
|
|
|
except errors.NotFound:
|
|
|
|
self.log.debug('DNS container not found, nothing to upgrade')
|
|
|
|
sysupgrade.set_upgrade_state('dns', 'server_config_to_ldap', True)
|
|
|
|
return False, []
|
|
|
|
|
|
|
|
result = self.api.Command.server_show(self.api.env.host)['result']
|
|
|
|
if not 'DNS server' in result.get('enabled_role_servrole', []):
|
|
|
|
self.log.debug('This server is not DNS server, nothing to upgrade')
|
|
|
|
sysupgrade.set_upgrade_state('dns', 'server_config_to_ldap', True)
|
|
|
|
return False, []
|
|
|
|
|
|
|
|
# create container first, if doesn't exist
|
|
|
|
entry = ldap.make_entry(
|
|
|
|
DN(self.api.env.container_dnsservers, self.api.env.basedn),
|
|
|
|
{
|
|
|
|
u'objectclass': [u'top', u'nsContainer'],
|
|
|
|
u'cn': [u'servers']
|
|
|
|
}
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
ldap.add_entry(entry)
|
|
|
|
except errors.DuplicateEntry:
|
|
|
|
self.log.debug('cn=dnsservers container already exists')
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.api.Command.dnsserver_add(self.api.env.host)
|
|
|
|
except errors.DuplicateEntry:
|
|
|
|
self.log.debug("DNS server configuration already exists "
|
|
|
|
"in LDAP database")
|
|
|
|
else:
|
|
|
|
self.log.debug("DNS server configuration has been sucessfully "
|
|
|
|
"created in LDAP database")
|
|
|
|
sysupgrade.set_upgrade_state('dns', 'server_config_to_ldap', True)
|
|
|
|
return False, []
|