2007-08-16 17:00:16 -05:00
|
|
|
# Authors: Simo Sorce <ssorce@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2007 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# 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.
|
2007-08-16 17:00:16 -05:00
|
|
|
#
|
|
|
|
# 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
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-08-16 17:00:16 -05:00
|
|
|
#
|
|
|
|
|
2017-04-04 01:42:10 -05:00
|
|
|
import operator
|
2007-08-16 17:00:16 -05:00
|
|
|
import socket
|
2013-01-31 07:26:38 -06:00
|
|
|
|
2016-02-19 10:26:57 -06:00
|
|
|
import six
|
|
|
|
|
2013-01-31 07:26:38 -06:00
|
|
|
from ipapython.ipa_log_manager import root_logger
|
2012-05-11 07:38:09 -05:00
|
|
|
from dns import resolver, rdatatype
|
|
|
|
from dns.exception import DNSException
|
2013-01-31 07:26:38 -06:00
|
|
|
from ipalib import errors
|
|
|
|
from ipapython import ipaldap
|
2014-05-29 07:47:17 -05:00
|
|
|
from ipaplatform.paths import paths
|
2016-02-29 06:55:07 -06:00
|
|
|
from ipapython.ipautil import valid_ip, realm_to_suffix
|
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
|
2011-07-06 09:30:24 -05:00
|
|
|
|
|
|
|
NOT_FQDN = -1
|
|
|
|
NO_LDAP_SERVER = -2
|
|
|
|
REALM_NOT_FOUND = -3
|
|
|
|
NOT_IPA_SERVER = -4
|
2011-09-28 15:31:38 -05:00
|
|
|
NO_ACCESS_TO_LDAP = -5
|
2012-11-15 13:57:52 -06:00
|
|
|
NO_TLS_LDAP = -6
|
2011-07-06 09:30:24 -05:00
|
|
|
BAD_HOST_CONFIG = -10
|
2011-09-28 15:31:38 -05:00
|
|
|
UNKNOWN_ERROR = -15
|
2011-07-06 09:30:24 -05:00
|
|
|
|
2016-02-29 06:55:07 -06:00
|
|
|
IPA_BASEDN_INFO = 'ipa v2.0'
|
|
|
|
|
2012-06-13 10:44:06 -05:00
|
|
|
error_names = {
|
|
|
|
0: 'Success',
|
|
|
|
NOT_FQDN: 'NOT_FQDN',
|
|
|
|
NO_LDAP_SERVER: 'NO_LDAP_SERVER',
|
|
|
|
REALM_NOT_FOUND: 'REALM_NOT_FOUND',
|
|
|
|
NOT_IPA_SERVER: 'NOT_IPA_SERVER',
|
|
|
|
NO_ACCESS_TO_LDAP: 'NO_ACCESS_TO_LDAP',
|
2012-11-15 13:57:52 -06:00
|
|
|
NO_TLS_LDAP: 'NO_TLS_LDAP',
|
2012-06-13 10:44:06 -05:00
|
|
|
BAD_HOST_CONFIG: 'BAD_HOST_CONFIG',
|
|
|
|
UNKNOWN_ERROR: 'UNKNOWN_ERROR',
|
|
|
|
}
|
|
|
|
|
2016-02-29 06:55:07 -06:00
|
|
|
def get_ipa_basedn(conn):
|
|
|
|
"""
|
|
|
|
Get base DN of IPA suffix in given LDAP server.
|
|
|
|
|
|
|
|
None is returned if the suffix is not found
|
|
|
|
|
|
|
|
:param conn: Bound LDAPClient that will be used for searching
|
|
|
|
"""
|
|
|
|
entry = conn.get_entry(
|
|
|
|
DN(), attrs_list=['defaultnamingcontext', 'namingcontexts'])
|
|
|
|
|
2016-02-19 10:26:57 -06:00
|
|
|
contexts = [c.decode('utf-8') for c in entry.raw['namingcontexts']]
|
2016-02-29 06:55:07 -06:00
|
|
|
if 'defaultnamingcontext' in entry:
|
|
|
|
# If there is a defaultNamingContext examine that one first
|
2016-02-19 10:26:57 -06:00
|
|
|
[default] = entry.raw['defaultnamingcontext']
|
|
|
|
default = default.decode('utf-8')
|
2016-02-29 06:55:07 -06:00
|
|
|
if default in contexts:
|
|
|
|
contexts.remove(default)
|
|
|
|
contexts.insert(0, default)
|
|
|
|
for context in contexts:
|
|
|
|
root_logger.debug("Check if naming context '%s' is for IPA" % context)
|
|
|
|
try:
|
|
|
|
[entry] = conn.get_entries(
|
|
|
|
DN(context), conn.SCOPE_BASE, "(info=IPA*)")
|
|
|
|
except errors.NotFound:
|
|
|
|
root_logger.debug("LDAP server did not return info attribute to "
|
|
|
|
"check for IPA version")
|
|
|
|
continue
|
2016-02-19 10:26:57 -06:00
|
|
|
[info] = entry.raw['info']
|
|
|
|
info = info.decode('utf-8').lower()
|
2016-02-29 06:55:07 -06:00
|
|
|
if info != IPA_BASEDN_INFO:
|
|
|
|
root_logger.debug("Detected IPA server version (%s) did not match the client (%s)" \
|
|
|
|
% (info, IPA_BASEDN_INFO))
|
|
|
|
continue
|
|
|
|
root_logger.debug("Naming context '%s' is a valid IPA context" % context)
|
|
|
|
return DN(context)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2012-06-13 10:44:06 -05:00
|
|
|
class IPADiscovery(object):
|
2007-08-16 17:00:16 -05:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.realm = None
|
|
|
|
self.domain = None
|
|
|
|
self.server = None
|
2013-02-04 08:35:13 -06:00
|
|
|
self.servers = []
|
2007-08-30 18:40:54 -05:00
|
|
|
self.basedn = None
|
2007-08-16 17:00:16 -05:00
|
|
|
|
2012-06-13 10:44:06 -05:00
|
|
|
self.realm_source = None
|
|
|
|
self.domain_source = None
|
|
|
|
self.server_source = None
|
|
|
|
self.basedn_source = None
|
|
|
|
|
2010-09-17 20:23:08 -05:00
|
|
|
def __get_resolver_domains(self):
|
|
|
|
"""
|
|
|
|
Read /etc/resolv.conf and return all the domains found in domain and
|
|
|
|
search.
|
|
|
|
|
2012-06-13 10:44:06 -05:00
|
|
|
Returns a list of (domain, info) pairs. The info contains a reason why
|
|
|
|
the domain is returned.
|
2010-09-17 20:23:08 -05:00
|
|
|
"""
|
|
|
|
domains = []
|
|
|
|
domain = None
|
|
|
|
try:
|
2014-05-29 07:47:17 -05:00
|
|
|
fp = open(paths.RESOLV_CONF, 'r')
|
2010-09-17 20:23:08 -05:00
|
|
|
lines = fp.readlines()
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
if line.lower().startswith('domain'):
|
2012-06-13 10:44:06 -05:00
|
|
|
domain = (line.split()[-1],
|
|
|
|
'local domain from /etc/resolv.conf')
|
2010-09-17 20:23:08 -05:00
|
|
|
elif line.lower().startswith('search'):
|
2012-06-13 10:44:06 -05:00
|
|
|
domains += [(d, 'search domain from /etc/resolv.conf') for
|
|
|
|
d in line.split()[1:]]
|
2016-03-11 12:51:07 -06:00
|
|
|
except Exception:
|
2010-09-17 20:23:08 -05:00
|
|
|
pass
|
2012-06-13 10:44:06 -05:00
|
|
|
if domain:
|
2010-09-17 20:23:08 -05:00
|
|
|
domains = [domain] + domains
|
|
|
|
return domains
|
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
def getServerName(self):
|
2008-03-05 15:33:12 -06:00
|
|
|
return self.server
|
2007-08-16 17:00:16 -05:00
|
|
|
|
|
|
|
def getDomainName(self):
|
2008-03-05 15:33:12 -06:00
|
|
|
return self.domain
|
2007-08-16 17:00:16 -05:00
|
|
|
|
|
|
|
def getRealmName(self):
|
2008-03-05 15:33:12 -06:00
|
|
|
return self.realm
|
2007-08-16 17:00:16 -05:00
|
|
|
|
2011-03-21 08:50:05 -05:00
|
|
|
def getKDCName(self):
|
|
|
|
return self.kdc
|
|
|
|
|
2007-08-30 18:40:54 -05:00
|
|
|
def getBaseDN(self):
|
2008-03-05 15:33:12 -06:00
|
|
|
return self.basedn
|
2007-08-30 18:40:54 -05:00
|
|
|
|
2012-06-13 10:44:06 -05:00
|
|
|
def check_domain(self, domain, tried, reason):
|
2010-09-17 20:23:08 -05:00
|
|
|
"""
|
|
|
|
Given a domain search it for SRV records, breaking it down to search
|
|
|
|
all subdomains too.
|
|
|
|
|
2013-02-04 08:35:13 -06:00
|
|
|
Returns a tuple (servers, domain) or (None,None) if a SRV record
|
|
|
|
isn't found. servers is a list of servers found. domain is a string.
|
2012-06-13 10:44:06 -05:00
|
|
|
|
|
|
|
:param tried: A set of domains that were tried already
|
|
|
|
:param reason: Reason this domain is searched (included in the log)
|
2010-09-17 20:23:08 -05:00
|
|
|
"""
|
2013-02-04 08:35:13 -06:00
|
|
|
servers = None
|
2012-06-13 10:44:06 -05:00
|
|
|
root_logger.debug('Start searching for LDAP SRV record in "%s" (%s) ' +
|
|
|
|
'and its sub-domains', domain, reason)
|
2013-02-04 08:35:13 -06:00
|
|
|
while not servers:
|
2012-06-13 10:44:06 -05:00
|
|
|
if domain in tried:
|
|
|
|
root_logger.debug("Already searched %s; skipping", domain)
|
|
|
|
break
|
|
|
|
tried.add(domain)
|
|
|
|
|
2013-02-04 08:35:13 -06:00
|
|
|
servers = self.ipadns_search_srv(domain, '_ldap._tcp', 389,
|
|
|
|
break_on_first=False)
|
|
|
|
if servers:
|
|
|
|
return (servers, domain)
|
2010-09-17 20:23:08 -05:00
|
|
|
else:
|
|
|
|
p = domain.find(".")
|
|
|
|
if p == -1: #no ldap server found and last component of the domain already tested
|
|
|
|
return (None, None)
|
|
|
|
domain = domain[p+1:]
|
|
|
|
return (None, None)
|
|
|
|
|
2014-08-27 05:31:09 -05:00
|
|
|
def search(self, domain="", servers="", realm=None, hostname=None, ca_cert_path=None):
|
2013-02-04 08:35:13 -06:00
|
|
|
"""
|
|
|
|
Use DNS discovery to identify valid IPA servers.
|
|
|
|
|
|
|
|
servers may contain an optional list of servers which will be used
|
|
|
|
instead of discovering available LDAP SRV records.
|
|
|
|
|
|
|
|
Returns a constant representing the overall search result.
|
|
|
|
"""
|
2012-06-13 10:44:06 -05:00
|
|
|
root_logger.debug("[IPA Discovery]")
|
|
|
|
root_logger.debug(
|
2013-02-04 08:35:13 -06:00
|
|
|
'Starting IPA discovery with domain=%s, servers=%s, hostname=%s',
|
|
|
|
domain, servers, hostname)
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2013-02-04 08:35:13 -06:00
|
|
|
self.server = None
|
|
|
|
autodiscovered = False
|
2012-07-03 16:37:22 -05:00
|
|
|
|
2013-02-04 08:35:13 -06:00
|
|
|
if not servers:
|
2007-08-16 17:00:16 -05:00
|
|
|
|
|
|
|
if not domain: #domain not provided do full DNS discovery
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
# get the local host name
|
|
|
|
if not hostname:
|
2011-07-06 09:30:24 -05:00
|
|
|
hostname = socket.getfqdn()
|
2012-06-13 10:44:06 -05:00
|
|
|
root_logger.debug('Hostname: %s', hostname)
|
2011-07-06 09:30:24 -05:00
|
|
|
if not hostname:
|
|
|
|
return BAD_HOST_CONFIG
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2011-07-25 10:14:01 -05:00
|
|
|
if valid_ip(hostname):
|
|
|
|
return NOT_FQDN
|
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
# first, check for an LDAP server for the local domain
|
|
|
|
p = hostname.find(".")
|
|
|
|
if p == -1: #no domain name
|
2011-07-06 09:30:24 -05:00
|
|
|
return NOT_FQDN
|
2007-08-16 17:00:16 -05:00
|
|
|
domain = hostname[p+1:]
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2010-09-17 20:23:08 -05:00
|
|
|
# Get the list of domains from /etc/resolv.conf, we'll search
|
2012-06-13 10:44:06 -05:00
|
|
|
# them all. We search the domain of our hostname first though.
|
|
|
|
# This is to avoid the situation where domain isn't set in
|
|
|
|
# /etc/resolv.conf and the search list has the hostname domain
|
|
|
|
# not first. We could end up with the wrong SRV record.
|
2010-09-17 20:23:08 -05:00
|
|
|
domains = self.__get_resolver_domains()
|
2012-06-13 10:44:06 -05:00
|
|
|
domains = [(domain, 'domain of the hostname')] + domains
|
|
|
|
tried = set()
|
|
|
|
for domain, reason in domains:
|
2013-02-04 08:35:13 -06:00
|
|
|
servers, domain = self.check_domain(domain, tried, reason)
|
|
|
|
if servers:
|
|
|
|
autodiscovered = True
|
2007-08-16 17:00:16 -05:00
|
|
|
self.domain = domain
|
2012-06-13 10:44:06 -05:00
|
|
|
self.server_source = self.domain_source = (
|
|
|
|
'Discovered LDAP SRV records from %s (%s)' %
|
|
|
|
(domain, reason))
|
2010-09-17 20:23:08 -05:00
|
|
|
break
|
|
|
|
if not self.domain: #no ldap server found
|
2012-06-13 10:44:06 -05:00
|
|
|
root_logger.debug('No LDAP server found')
|
2011-07-06 09:30:24 -05:00
|
|
|
return NO_LDAP_SERVER
|
2007-08-16 17:00:16 -05:00
|
|
|
else:
|
2012-05-11 07:38:09 -05:00
|
|
|
root_logger.debug("Search for LDAP SRV record in %s", domain)
|
2013-02-04 08:35:13 -06:00
|
|
|
servers = self.ipadns_search_srv(domain, '_ldap._tcp', 389,
|
|
|
|
break_on_first=False)
|
|
|
|
if servers:
|
|
|
|
autodiscovered = True
|
2007-08-16 17:00:16 -05:00
|
|
|
self.domain = domain
|
2012-06-13 10:44:06 -05:00
|
|
|
self.server_source = self.domain_source = (
|
|
|
|
'Discovered LDAP SRV records from %s' % domain)
|
2007-08-16 17:00:16 -05:00
|
|
|
else:
|
2012-05-11 07:38:09 -05:00
|
|
|
self.server = None
|
2012-06-13 10:44:06 -05:00
|
|
|
root_logger.debug('No LDAP server found')
|
2011-07-06 09:30:24 -05:00
|
|
|
return NO_LDAP_SERVER
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2012-06-13 10:44:06 -05:00
|
|
|
else:
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2012-06-13 10:44:06 -05:00
|
|
|
root_logger.debug("Server and domain forced")
|
2007-08-16 17:00:16 -05:00
|
|
|
self.domain = domain
|
2012-06-13 10:44:06 -05:00
|
|
|
self.domain_source = self.server_source = 'Forced'
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2011-03-21 08:50:05 -05:00
|
|
|
#search for kerberos
|
2012-06-13 10:44:06 -05:00
|
|
|
root_logger.debug("[Kerberos realm search]")
|
2014-08-27 05:31:09 -05:00
|
|
|
if realm:
|
|
|
|
root_logger.debug("Kerberos realm forced")
|
|
|
|
self.realm = realm
|
|
|
|
self.realm_source = 'Forced'
|
|
|
|
else:
|
|
|
|
realm = self.ipadnssearchkrbrealm()
|
|
|
|
self.realm = realm
|
|
|
|
self.realm_source = (
|
|
|
|
'Discovered Kerberos DNS records from %s' % self.domain)
|
|
|
|
|
|
|
|
if not servers and not realm:
|
2011-07-06 09:30:24 -05:00
|
|
|
return REALM_NOT_FOUND
|
2011-03-21 08:50:05 -05:00
|
|
|
|
2016-03-07 10:41:15 -06:00
|
|
|
if autodiscovered:
|
|
|
|
self.kdc = self.ipadnssearchkrbkdc()
|
|
|
|
self.kdc_source = (
|
|
|
|
'Discovered Kerberos DNS records from %s' % self.domain)
|
|
|
|
else:
|
|
|
|
self.kdc = ', '.join(servers)
|
|
|
|
self.kdc_source = "Kerberos DNS record discovery bypassed"
|
2011-03-21 08:50:05 -05:00
|
|
|
|
2011-12-07 06:40:46 -06:00
|
|
|
# We may have received multiple servers corresponding to the domain
|
|
|
|
# Iterate through all of those to check if it is IPA LDAP server
|
|
|
|
ldapret = [NOT_IPA_SERVER]
|
|
|
|
ldapaccess = True
|
2013-02-04 08:35:13 -06:00
|
|
|
root_logger.debug("[LDAP server check]")
|
|
|
|
valid_servers = []
|
|
|
|
for server in servers:
|
|
|
|
root_logger.debug('Verifying that %s (realm %s) is an IPA server',
|
|
|
|
server, self.realm)
|
2011-12-07 06:40:46 -06:00
|
|
|
# check ldap now
|
2013-02-04 08:35:13 -06:00
|
|
|
ldapret = self.ipacheckldap(server, self.realm, ca_cert_path=ca_cert_path)
|
2011-12-07 06:40:46 -06:00
|
|
|
|
|
|
|
if ldapret[0] == 0:
|
|
|
|
self.server = ldapret[1]
|
|
|
|
self.realm = ldapret[2]
|
2012-06-13 10:44:06 -05:00
|
|
|
self.server_source = self.realm_source = (
|
|
|
|
'Discovered from LDAP DNS records in %s' % self.server)
|
2013-03-13 08:42:12 -05:00
|
|
|
valid_servers.append(server)
|
2013-02-04 08:35:13 -06:00
|
|
|
# verified, we actually talked to the remote server and it
|
|
|
|
# is definetely an IPA server
|
|
|
|
if autodiscovered:
|
|
|
|
# No need to keep verifying servers if we discovered them
|
|
|
|
# via DNS
|
|
|
|
break
|
2012-11-15 13:57:52 -06:00
|
|
|
elif ldapret[0] == NO_ACCESS_TO_LDAP or ldapret[0] == NO_TLS_LDAP:
|
2011-12-07 06:40:46 -06:00
|
|
|
ldapaccess = False
|
2013-03-13 08:42:12 -05:00
|
|
|
valid_servers.append(server)
|
2013-02-04 08:35:13 -06:00
|
|
|
# we may set verified_servers below, we don't have it yet
|
|
|
|
if autodiscovered:
|
|
|
|
# No need to keep verifying servers if we discovered them
|
|
|
|
# via DNS
|
|
|
|
break
|
|
|
|
elif ldapret[0] == NOT_IPA_SERVER:
|
2016-01-15 09:25:33 -06:00
|
|
|
root_logger.warning(
|
2013-03-13 08:42:12 -05:00
|
|
|
'Skip %s: not an IPA server', server)
|
2013-02-04 08:35:13 -06:00
|
|
|
elif ldapret[0] == NO_LDAP_SERVER:
|
2016-01-15 09:25:33 -06:00
|
|
|
root_logger.warning(
|
2013-03-13 08:42:12 -05:00
|
|
|
'Skip %s: LDAP server is not responding, unable to verify if '
|
|
|
|
'this is an IPA server', server)
|
|
|
|
else:
|
2016-01-15 09:25:33 -06:00
|
|
|
root_logger.warning(
|
2013-03-13 08:42:12 -05:00
|
|
|
'Skip %s: cannot verify if this is an IPA server', server)
|
2011-12-07 06:40:46 -06:00
|
|
|
|
2012-06-13 10:44:06 -05:00
|
|
|
# If one of LDAP servers checked rejects access (maybe anonymous
|
2011-12-07 06:40:46 -06:00
|
|
|
# bind is disabled), assume realm and basedn generated off domain.
|
|
|
|
# Note that in case ldapret[0] == 0 and ldapaccess == False (one of
|
|
|
|
# servers didn't provide access but another one succeeded), self.realm
|
|
|
|
# will be set already to a proper value above, self.basdn will be
|
|
|
|
# initialized during the LDAP check itself and we'll skip these two checks.
|
|
|
|
if not ldapaccess and self.realm is None:
|
2011-09-28 15:31:38 -05:00
|
|
|
# Assume realm is the same as domain.upper()
|
|
|
|
self.realm = self.domain.upper()
|
2012-06-13 10:44:06 -05:00
|
|
|
self.realm_source = 'Assumed same as domain'
|
|
|
|
root_logger.debug(
|
|
|
|
"Assuming realm is the same as domain: %s", self.realm)
|
2011-09-30 09:52:30 -05:00
|
|
|
|
2011-12-07 06:40:46 -06:00
|
|
|
if not ldapaccess and self.basedn is None:
|
2011-09-30 09:52:30 -05:00
|
|
|
# Generate suffix from realm
|
2012-09-27 05:40:55 -05:00
|
|
|
self.basedn = realm_to_suffix(self.realm)
|
2012-06-13 10:44:06 -05:00
|
|
|
self.basedn_source = 'Generated from Kerberos realm'
|
|
|
|
root_logger.debug("Generated basedn from realm: %s" % self.basedn)
|
|
|
|
|
|
|
|
root_logger.debug(
|
|
|
|
"Discovery result: %s; server=%s, domain=%s, kdc=%s, basedn=%s",
|
|
|
|
error_names.get(ldapret[0], ldapret[0]),
|
|
|
|
self.server, self.domain, self.kdc, self.basedn)
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2013-02-04 08:35:13 -06:00
|
|
|
root_logger.debug("Validated servers: %s" % ','.join(valid_servers))
|
|
|
|
self.servers = valid_servers
|
|
|
|
|
|
|
|
# If we have any servers left then override the last return value
|
|
|
|
# to indicate success.
|
2013-03-19 02:57:18 -05:00
|
|
|
if valid_servers:
|
2013-02-04 08:35:13 -06:00
|
|
|
self.server = servers[0]
|
|
|
|
ldapret[0] = 0
|
|
|
|
|
2011-09-28 15:31:38 -05:00
|
|
|
return ldapret[0]
|
2007-08-16 17:00:16 -05:00
|
|
|
|
2012-11-15 13:57:52 -06:00
|
|
|
def ipacheckldap(self, thost, trealm, ca_cert_path=None):
|
2011-07-06 09:30:24 -05:00
|
|
|
"""
|
|
|
|
Given a host and kerberos realm verify that it is an IPA LDAP
|
2012-11-15 13:57:52 -06:00
|
|
|
server hosting the realm.
|
2011-07-06 09:30:24 -05:00
|
|
|
|
2011-09-28 15:31:38 -05:00
|
|
|
Returns a list [errno, host, realm] or an empty list on error.
|
|
|
|
Errno is an error number:
|
|
|
|
0 means all ok
|
|
|
|
1 means we could not check the info in LDAP (may happend when
|
2011-12-07 06:40:46 -06:00
|
|
|
anonymous binds are disabled)
|
2011-09-28 15:31:38 -05:00
|
|
|
2 means the server is certainly not an IPA server
|
2011-07-06 09:30:24 -05:00
|
|
|
"""
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
lrealms = []
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
#now verify the server is really an IPA server
|
|
|
|
try:
|
2016-11-01 08:52:33 -05:00
|
|
|
ldap_uri = ipaldap.get_ldap_uri(thost)
|
|
|
|
start_tls = False
|
2012-11-15 13:57:52 -06:00
|
|
|
if ca_cert_path:
|
2016-11-01 08:52:33 -05:00
|
|
|
start_tls = True
|
|
|
|
root_logger.debug("Init LDAP connection to: %s", ldap_uri)
|
|
|
|
lh = ipaldap.LDAPClient(
|
|
|
|
ldap_uri, cacert=ca_cert_path, start_tls=start_tls,
|
|
|
|
no_schema=True, decode_attrs=False)
|
2013-01-31 07:26:38 -06:00
|
|
|
try:
|
2016-10-05 10:42:32 -05:00
|
|
|
lh.simple_bind(DN(), '')
|
2014-07-24 02:57:54 -05:00
|
|
|
|
|
|
|
# get IPA base DN
|
|
|
|
root_logger.debug("Search LDAP server for IPA base DN")
|
|
|
|
basedn = get_ipa_basedn(lh)
|
2013-01-31 07:26:38 -06:00
|
|
|
except errors.ACIError:
|
|
|
|
root_logger.debug("LDAP Error: Anonymous access not allowed")
|
|
|
|
return [NO_ACCESS_TO_LDAP]
|
2015-07-30 09:49:29 -05:00
|
|
|
except errors.DatabaseError as err:
|
2013-01-31 07:26:38 -06:00
|
|
|
root_logger.error("Error checking LDAP: %s" % err.strerror)
|
|
|
|
# We should only get UNWILLING_TO_PERFORM if the remote LDAP
|
|
|
|
# server has minssf > 0 and we have attempted a non-TLS conn.
|
|
|
|
if ca_cert_path is None:
|
|
|
|
root_logger.debug(
|
|
|
|
"Cannot connect to LDAP server. Check that minssf is "
|
|
|
|
"not enabled")
|
|
|
|
return [NO_TLS_LDAP]
|
|
|
|
else:
|
|
|
|
return [UNKNOWN_ERROR]
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2011-09-30 09:52:30 -05:00
|
|
|
if basedn is None:
|
2012-06-13 10:44:06 -05:00
|
|
|
root_logger.debug("The server is not an IPA server")
|
2011-09-28 15:31:38 -05:00
|
|
|
return [NOT_IPA_SERVER]
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2011-09-30 09:52:30 -05:00
|
|
|
self.basedn = basedn
|
2013-01-31 07:26:38 -06:00
|
|
|
self.basedn_source = 'From IPA server %s' % lh.ldap_uri
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
#search and return known realms
|
2012-06-13 10:44:06 -05:00
|
|
|
root_logger.debug(
|
|
|
|
"Search for (objectClass=krbRealmContainer) in %s (sub)",
|
|
|
|
self.basedn)
|
2013-01-31 07:26:38 -06:00
|
|
|
try:
|
|
|
|
lret = lh.get_entries(
|
|
|
|
DN(('cn', 'kerberos'), self.basedn),
|
|
|
|
lh.SCOPE_SUBTREE, "(objectClass=krbRealmContainer)")
|
|
|
|
except errors.NotFound:
|
2007-08-16 17:00:16 -05:00
|
|
|
#something very wrong
|
2011-09-28 15:31:38 -05:00
|
|
|
return [REALM_NOT_FOUND]
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
for lres in lret:
|
2013-01-31 07:26:38 -06:00
|
|
|
root_logger.debug("Found: %s", lres.dn)
|
2016-02-19 10:26:57 -06:00
|
|
|
[cn] = lres.raw['cn']
|
|
|
|
if six.PY3:
|
|
|
|
cn = cn.decode('utf-8')
|
|
|
|
lrealms.append(cn)
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
if trealm:
|
|
|
|
for r in lrealms:
|
|
|
|
if trealm == r:
|
2011-09-28 15:31:38 -05:00
|
|
|
return [0, thost, trealm]
|
2007-08-16 17:00:16 -05:00
|
|
|
# must match or something is very wrong
|
2015-01-20 10:30:51 -06:00
|
|
|
root_logger.debug("Realm %s does not match any realm in LDAP "
|
|
|
|
"database", trealm)
|
2011-09-28 15:31:38 -05:00
|
|
|
return [REALM_NOT_FOUND]
|
2007-08-16 17:00:16 -05:00
|
|
|
else:
|
|
|
|
if len(lrealms) != 1:
|
|
|
|
#which one? we can't attach to a multi-realm server without DNS working
|
2015-01-20 10:30:51 -06:00
|
|
|
root_logger.debug("Multiple realms found, cannot decide "
|
|
|
|
"which realm is the right without "
|
|
|
|
"working DNS")
|
2011-09-28 15:31:38 -05:00
|
|
|
return [REALM_NOT_FOUND]
|
2007-08-16 17:00:16 -05:00
|
|
|
else:
|
2011-09-28 15:31:38 -05:00
|
|
|
return [0, thost, lrealms[0]]
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
#we shouldn't get here
|
2016-02-02 05:50:26 -06:00
|
|
|
assert False, "Unknown error in ipadiscovery"
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2013-01-31 07:26:38 -06:00
|
|
|
except errors.DatabaseTimeout:
|
2013-03-13 08:42:12 -05:00
|
|
|
root_logger.debug("LDAP Error: timeout")
|
2013-01-31 07:26:38 -06:00
|
|
|
return [NO_LDAP_SERVER]
|
2015-07-30 09:49:29 -05:00
|
|
|
except errors.NetworkError as err:
|
2013-01-31 07:26:38 -06:00
|
|
|
root_logger.debug("LDAP Error: %s" % err.strerror)
|
|
|
|
return [NO_LDAP_SERVER]
|
|
|
|
except errors.ACIError:
|
|
|
|
root_logger.debug("LDAP Error: Anonymous access not allowed")
|
|
|
|
return [NO_ACCESS_TO_LDAP]
|
2015-07-30 09:49:29 -05:00
|
|
|
except errors.DatabaseError as err:
|
2013-03-13 08:42:12 -05:00
|
|
|
root_logger.debug("Error checking LDAP: %s" % err.strerror)
|
2013-01-31 07:26:38 -06:00
|
|
|
return [UNKNOWN_ERROR]
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as err:
|
2013-03-13 08:42:12 -05:00
|
|
|
root_logger.debug("Error checking LDAP: %s" % err)
|
2012-11-15 13:57:52 -06:00
|
|
|
|
2011-09-28 15:31:38 -05:00
|
|
|
return [UNKNOWN_ERROR]
|
2007-08-16 17:00:16 -05:00
|
|
|
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2012-05-11 07:38:09 -05:00
|
|
|
def ipadns_search_srv(self, domain, srv_record_name, default_port,
|
|
|
|
break_on_first=True):
|
|
|
|
"""
|
|
|
|
Search for SRV records in given domain. When no record is found,
|
2012-06-13 10:44:06 -05:00
|
|
|
en empty list is returned
|
2012-05-11 07:38:09 -05:00
|
|
|
|
|
|
|
:param domain: Search domain name
|
|
|
|
:param srv_record_name: SRV record name, e.g. "_ldap._tcp"
|
|
|
|
:param default_port: When default_port is not None, it is being
|
|
|
|
checked with the port in SRV record and if they don't
|
|
|
|
match, the port from SRV record is appended to
|
|
|
|
found hostname in this format: "hostname:port"
|
|
|
|
:param break_on_first: break on the first find and return just one
|
|
|
|
entry
|
|
|
|
"""
|
|
|
|
servers = []
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2012-05-11 07:38:09 -05:00
|
|
|
qname = '%s.%s' % (srv_record_name, domain)
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2012-05-11 07:38:09 -05:00
|
|
|
root_logger.debug("Search DNS for SRV record of %s", qname)
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2012-05-11 07:38:09 -05:00
|
|
|
try:
|
|
|
|
answers = resolver.query(qname, rdatatype.SRV)
|
2017-04-04 01:42:10 -05:00
|
|
|
answers = sorted(answers, key=operator.attrgetter('priority'))
|
2015-07-30 09:49:29 -05:00
|
|
|
except DNSException as e:
|
2012-05-11 07:38:09 -05:00
|
|
|
root_logger.debug("DNS record not found: %s", e.__class__.__name__)
|
|
|
|
answers = []
|
|
|
|
|
|
|
|
for answer in answers:
|
|
|
|
root_logger.debug("DNS record found: %s", answer)
|
|
|
|
server = str(answer.target).rstrip(".")
|
|
|
|
if not server:
|
|
|
|
root_logger.debug("Cannot parse the hostname from SRV record: %s", answer)
|
|
|
|
continue
|
|
|
|
if default_port is not None and answer.port != default_port:
|
|
|
|
server = "%s:%s" % (server, str(answer.port))
|
|
|
|
servers.append(server)
|
|
|
|
if break_on_first:
|
2011-10-05 09:25:09 -05:00
|
|
|
break
|
|
|
|
|
|
|
|
return servers
|
|
|
|
|
2014-08-27 05:31:09 -05:00
|
|
|
def ipadnssearchkrbrealm(self, domain=None):
|
2011-09-28 15:31:38 -05:00
|
|
|
realm = None
|
2014-08-27 05:31:09 -05:00
|
|
|
if not domain:
|
|
|
|
domain = self.domain
|
2007-08-16 17:00:16 -05:00
|
|
|
# now, check for a Kerberos realm the local host or domain is in
|
2014-08-27 05:31:09 -05:00
|
|
|
qname = "_kerberos." + domain
|
2012-05-11 07:38:09 -05:00
|
|
|
|
|
|
|
root_logger.debug("Search DNS for TXT record of %s", qname)
|
|
|
|
|
|
|
|
try:
|
|
|
|
answers = resolver.query(qname, rdatatype.TXT)
|
2015-07-30 09:49:29 -05:00
|
|
|
except DNSException as e:
|
2012-05-11 07:38:09 -05:00
|
|
|
root_logger.debug("DNS record not found: %s", e.__class__.__name__)
|
|
|
|
answers = []
|
|
|
|
|
|
|
|
for answer in answers:
|
|
|
|
root_logger.debug("DNS record found: %s", answer)
|
|
|
|
if answer.strings:
|
|
|
|
realm = answer.strings[0]
|
2007-08-16 17:00:16 -05:00
|
|
|
if realm:
|
|
|
|
break
|
2014-08-27 05:31:09 -05:00
|
|
|
return realm
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2014-08-27 05:31:09 -05:00
|
|
|
def ipadnssearchkrbkdc(self, domain=None):
|
|
|
|
kdc = None
|
2012-05-11 07:38:09 -05:00
|
|
|
|
2014-08-27 05:31:09 -05:00
|
|
|
if not domain:
|
|
|
|
domain = self.domain
|
2007-08-16 17:00:16 -05:00
|
|
|
|
2014-08-27 05:31:09 -05:00
|
|
|
kdc = self.ipadns_search_srv(domain, '_kerberos._udp', 88,
|
|
|
|
break_on_first=False)
|
|
|
|
|
|
|
|
if kdc:
|
|
|
|
kdc = ','.join(kdc)
|
|
|
|
else:
|
|
|
|
root_logger.debug("SRV record for KDC not found! Domain: %s" % domain)
|
|
|
|
kdc = None
|
2011-07-06 09:30:24 -05:00
|
|
|
|
2014-08-27 05:31:09 -05:00
|
|
|
return kdc
|