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
|
|
|
#
|
|
|
|
|
|
|
|
import socket
|
2011-02-21 14:32:21 -06:00
|
|
|
import os
|
2007-08-16 17:00:16 -05:00
|
|
|
import logging
|
2009-02-05 14:03:08 -06:00
|
|
|
import ipapython.dnsclient
|
2011-02-21 14:32:21 -06:00
|
|
|
import tempfile
|
2007-08-16 17:00:16 -05:00
|
|
|
import ldap
|
|
|
|
from ldap import LDAPError
|
2011-07-25 10:14:01 -05:00
|
|
|
from ipapython.ipautil import run, CalledProcessError, valid_ip
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2011-07-06 09:30:24 -05:00
|
|
|
|
|
|
|
NOT_FQDN = -1
|
|
|
|
NO_LDAP_SERVER = -2
|
|
|
|
REALM_NOT_FOUND = -3
|
|
|
|
NOT_IPA_SERVER = -4
|
|
|
|
BAD_HOST_CONFIG = -10
|
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
class IPADiscovery:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.realm = None
|
|
|
|
self.domain = None
|
|
|
|
self.server = None
|
2007-08-30 18:40:54 -05:00
|
|
|
self.basedn = None
|
2007-08-16 17:00:16 -05:00
|
|
|
|
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.
|
|
|
|
|
|
|
|
Returns a list
|
|
|
|
"""
|
|
|
|
domains = []
|
|
|
|
domain = None
|
|
|
|
try:
|
|
|
|
fp = open('/etc/resolv.conf', 'r')
|
|
|
|
lines = fp.readlines()
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
if line.lower().startswith('domain'):
|
|
|
|
domain = line.split(None)[-1]
|
|
|
|
elif line.lower().startswith('search'):
|
|
|
|
domains = domains + line.split(None)[1:]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if domain and not domain in domains:
|
|
|
|
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
|
|
|
|
2010-09-17 20:23:08 -05:00
|
|
|
def check_domain(self, domain):
|
|
|
|
"""
|
|
|
|
Given a domain search it for SRV records, breaking it down to search
|
|
|
|
all subdomains too.
|
|
|
|
|
|
|
|
Returns a tuple (server, domain) or (None,None) if a SRV record
|
|
|
|
isn't found.
|
|
|
|
"""
|
|
|
|
server = None
|
|
|
|
while not server:
|
|
|
|
logging.debug("[ipadnssearchldap("+domain+")]")
|
|
|
|
server = self.ipadnssearchldap(domain)
|
|
|
|
if server:
|
|
|
|
return (server, domain)
|
|
|
|
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)
|
|
|
|
|
2011-07-06 09:30:24 -05:00
|
|
|
def search(self, domain = "", server = "", hostname=None):
|
2007-08-16 17:00:16 -05:00
|
|
|
qname = ""
|
|
|
|
results = []
|
|
|
|
result = []
|
|
|
|
krbret = []
|
|
|
|
ldapret = []
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
if not server:
|
|
|
|
|
|
|
|
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()
|
|
|
|
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
|
|
|
|
# them all. We search the domain of our hostname first though,
|
|
|
|
# even if that means searching it twice. 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.
|
|
|
|
domains = self.__get_resolver_domains()
|
|
|
|
domains = [domain] + domains
|
|
|
|
for domain in domains:
|
|
|
|
(server, domain) = self.check_domain(domain)
|
|
|
|
if server:
|
|
|
|
self.server = server
|
2007-08-16 17:00:16 -05:00
|
|
|
self.domain = domain
|
2010-09-17 20:23:08 -05:00
|
|
|
break
|
|
|
|
if not self.domain: #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:
|
|
|
|
logging.debug("[ipadnssearchldap]")
|
|
|
|
self.server = self.ipadnssearchldap(domain)
|
|
|
|
if self.server:
|
|
|
|
self.domain = domain
|
|
|
|
else:
|
2011-07-06 09:30:24 -05:00
|
|
|
return NO_LDAP_SERVER
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
else: #server forced on us, this means DNS doesn't work :/
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
self.domain = domain
|
|
|
|
self.server = server
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2011-03-21 08:50:05 -05:00
|
|
|
#search for kerberos
|
|
|
|
logging.debug("[ipadnssearchkrb]")
|
|
|
|
krbret = self.ipadnssearchkrb(self.domain)
|
|
|
|
if not server and not krbret[0]:
|
2011-07-06 09:30:24 -05:00
|
|
|
return REALM_NOT_FOUND
|
2011-03-21 08:50:05 -05:00
|
|
|
|
|
|
|
self.realm = krbret[0]
|
|
|
|
self.kdc = krbret[1]
|
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
logging.debug("[ipacheckldap]")
|
|
|
|
# check ldap now
|
2008-03-05 15:33:12 -06:00
|
|
|
ldapret = self.ipacheckldap(self.server, self.realm)
|
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
if not ldapret:
|
2011-07-06 09:30:24 -05:00
|
|
|
return NOT_IPA_SERVER
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
self.server = ldapret[0]
|
|
|
|
self.realm = ldapret[1]
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
return 0
|
|
|
|
|
|
|
|
def ipacheckldap(self, thost, trealm):
|
2011-07-06 09:30:24 -05:00
|
|
|
"""
|
|
|
|
Given a host and kerberos realm verify that it is an IPA LDAP
|
|
|
|
server hosting the realm. The connection is an SSL connection
|
|
|
|
so the remote IPA CA cert must be available at
|
|
|
|
http://HOST/ipa/config/ca.crt
|
|
|
|
|
|
|
|
Returns a list [host, realm] or an empty list on error.
|
|
|
|
"""
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
lret = []
|
|
|
|
lres = []
|
|
|
|
lattr = ""
|
|
|
|
linfo = ""
|
|
|
|
lrealms = []
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
i = 0
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2011-02-21 14:32:21 -06:00
|
|
|
# Get the CA certificate
|
|
|
|
try:
|
|
|
|
# Create TempDir
|
|
|
|
temp_ca_dir = tempfile.mkdtemp()
|
|
|
|
except OSError, e:
|
|
|
|
raise RuntimeError("Creating temporary directory failed: %s" % str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
run(["/usr/bin/wget", "-O", "%s/ca.crt" % temp_ca_dir, "http://%s/ipa/config/ca.crt" % thost])
|
|
|
|
except CalledProcessError, e:
|
2011-03-29 12:27:11 -05:00
|
|
|
logging.debug('Retrieving CA from %s failed.\n%s' % (thost, str(e)))
|
|
|
|
return []
|
2011-02-21 14:32:21 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
#now verify the server is really an IPA server
|
|
|
|
try:
|
|
|
|
logging.debug("Init ldap with: ldap://"+thost+":389")
|
|
|
|
lh = ldap.initialize("ldap://"+thost+":389")
|
2011-02-21 14:32:21 -06:00
|
|
|
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, True)
|
|
|
|
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, "%s/ca.crt" % temp_ca_dir)
|
|
|
|
lh.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
|
|
|
|
lh.set_option(ldap.OPT_X_TLS_DEMAND, True)
|
|
|
|
lh.start_tls_s()
|
2007-08-16 17:00:16 -05:00
|
|
|
lh.simple_bind_s("","")
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
logging.debug("Search rootdse")
|
|
|
|
lret = lh.search_s("", ldap.SCOPE_BASE, "(objectClass=*)")
|
|
|
|
for lattr in lret[0][1]:
|
|
|
|
if lattr.lower() == "namingcontexts":
|
2007-08-30 18:40:54 -05:00
|
|
|
self.basedn = lret[0][1][lattr][0]
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-30 18:40:54 -05:00
|
|
|
logging.debug("Search for (info=*) in "+self.basedn+"(base)")
|
|
|
|
lret = lh.search_s(self.basedn, ldap.SCOPE_BASE, "(info=IPA*)")
|
2007-08-16 17:00:16 -05:00
|
|
|
if not lret:
|
|
|
|
return []
|
|
|
|
logging.debug("Found: "+str(lret))
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
for lattr in lret[0][1]:
|
|
|
|
if lattr.lower() == "info":
|
|
|
|
linfo = lret[0][1][lattr][0].lower()
|
|
|
|
break
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2011-07-06 09:30:24 -05:00
|
|
|
if not linfo or linfo.lower() != 'ipa v2.0':
|
2007-08-16 17:00:16 -05:00
|
|
|
return []
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
#search and return known realms
|
2007-08-30 18:40:54 -05:00
|
|
|
logging.debug("Search for (objectClass=krbRealmContainer) in "+self.basedn+"(sub)")
|
|
|
|
lret = lh.search_s("cn=kerberos,"+self.basedn, ldap.SCOPE_SUBTREE, "(objectClass=krbRealmContainer)")
|
2007-08-16 17:00:16 -05:00
|
|
|
if not lret:
|
|
|
|
#something very wrong
|
|
|
|
return []
|
|
|
|
logging.debug("Found: "+str(lret))
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
for lres in lret:
|
|
|
|
for lattr in lres[1]:
|
|
|
|
if lattr.lower() == "cn":
|
|
|
|
lrealms.append(lres[1][lattr][0])
|
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:
|
|
|
|
return [thost, trealm]
|
|
|
|
# must match or something is very wrong
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
if len(lrealms) != 1:
|
|
|
|
#which one? we can't attach to a multi-realm server without DNS working
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return [thost, lrealms[0]]
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
#we shouldn't get here
|
|
|
|
return []
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
except LDAPError, err:
|
2009-11-30 14:48:31 -06:00
|
|
|
if not isinstance(err, ldap.TIMEOUT):
|
|
|
|
logging.error("LDAP Error: %s: %s" %
|
|
|
|
(err.args[0]['desc'], err.args[0].get('info', '')))
|
|
|
|
else:
|
|
|
|
logging.error("LDAP Error: timeout")
|
2007-08-16 17:00:16 -05:00
|
|
|
return []
|
|
|
|
|
2011-02-21 14:32:21 -06:00
|
|
|
finally:
|
|
|
|
os.remove("%s/ca.crt" % temp_ca_dir)
|
|
|
|
os.removedirs(temp_ca_dir)
|
|
|
|
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
def ipadnssearchldap(self, tdomain):
|
|
|
|
servers = ""
|
|
|
|
rserver = ""
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
qname = "_ldap._tcp."+tdomain
|
|
|
|
# terminate the name
|
|
|
|
if not qname.endswith("."):
|
|
|
|
qname += "."
|
2009-02-05 14:03:08 -06:00
|
|
|
results = ipapython.dnsclient.query(qname, ipapython.dnsclient.DNS_C_IN, ipapython.dnsclient.DNS_T_SRV)
|
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
for result in results:
|
2009-02-05 14:03:08 -06:00
|
|
|
if result.dns_type == ipapython.dnsclient.DNS_T_SRV:
|
2007-08-16 17:00:16 -05:00
|
|
|
rserver = result.rdata.server.rstrip(".")
|
|
|
|
if result.rdata.port and result.rdata.port != 389:
|
|
|
|
rserver += ":" + str(result.rdata.port)
|
|
|
|
if servers:
|
|
|
|
servers += "," + rserver
|
|
|
|
else:
|
|
|
|
servers = rserver
|
|
|
|
break
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
return servers
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
def ipadnssearchkrb(self, tdomain):
|
|
|
|
realm = ""
|
|
|
|
kdc = ""
|
|
|
|
# now, check for a Kerberos realm the local host or domain is in
|
|
|
|
qname = "_kerberos." + tdomain
|
|
|
|
# terminate the name
|
|
|
|
if not qname.endswith("."):
|
|
|
|
qname += "."
|
2009-02-05 14:03:08 -06:00
|
|
|
results = ipapython.dnsclient.query(qname, ipapython.dnsclient.DNS_C_IN, ipapython.dnsclient.DNS_T_TXT)
|
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
for result in results:
|
2009-02-05 14:03:08 -06:00
|
|
|
if result.dns_type == ipapython.dnsclient.DNS_T_TXT:
|
2007-08-16 17:00:16 -05:00
|
|
|
realm = result.rdata.data
|
|
|
|
if realm:
|
|
|
|
break
|
2009-02-05 14:03:08 -06:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
if realm:
|
|
|
|
# now fetch server information for the realm
|
2011-03-21 08:50:05 -05:00
|
|
|
qname = "_kerberos._udp." + realm.lower()
|
2007-08-16 17:00:16 -05:00
|
|
|
# terminate the name
|
|
|
|
if not qname.endswith("."):
|
|
|
|
qname += "."
|
2009-02-05 14:03:08 -06:00
|
|
|
results = ipapython.dnsclient.query(qname, ipapython.dnsclient.DNS_C_IN, ipapython.dnsclient.DNS_T_SRV)
|
2007-08-16 17:00:16 -05:00
|
|
|
for result in results:
|
2009-02-05 14:03:08 -06:00
|
|
|
if result.dns_type == ipapython.dnsclient.DNS_T_SRV:
|
2007-08-16 17:00:16 -05:00
|
|
|
qname = result.rdata.server.rstrip(".")
|
|
|
|
if result.rdata.port and result.rdata.port != 88:
|
|
|
|
qname += ":" + str(result.rdata.port)
|
|
|
|
if kdc:
|
|
|
|
kdc += "," + qname
|
|
|
|
else:
|
|
|
|
kdc = qname
|
|
|
|
|
2011-03-21 08:50:05 -05:00
|
|
|
if not kdc:
|
|
|
|
logging.debug("SRV record for KDC not found! Realm: %s, SRV record: %s" % (realm, qname))
|
2011-07-06 09:30:24 -05:00
|
|
|
|
2007-08-16 17:00:16 -05:00
|
|
|
return [realm, kdc]
|