From 7b966e8577fdb56f069cf26a6ab4d6c77b8743b9 Mon Sep 17 00:00:00 2001 From: Jan Cholasta Date: Nov 29 2016 13:50:51 +0000 Subject: ipautil: remove get_domain_name() get_domain_name() and related code depends on ipaplatform. Replace all uses of get_domain_name() with api.env.domain and remove get_domain_name() and all of the related code. https://fedorahosted.org/freeipa/ticket/6474 Reviewed-By: Stanislav Laznicka --- diff --git a/install/tools/ipa-compat-manage b/install/tools/ipa-compat-manage index 307af1f..77468b4 100755 --- a/install/tools/ipa-compat-manage +++ b/install/tools/ipa-compat-manage @@ -56,8 +56,6 @@ def parse_options(): config.add_standard_options(parser) options, args = parser.parse_args() - config.init_config(options) - return options, args def get_dirman_password(): diff --git a/install/tools/ipa-nis-manage b/install/tools/ipa-nis-manage index fae458c..21ff183 100755 --- a/install/tools/ipa-nis-manage +++ b/install/tools/ipa-nis-manage @@ -58,8 +58,6 @@ def parse_options(): config.add_standard_options(parser) options, args = parser.parse_args() - config.init_config(options) - return options, args def get_dirman_password(): diff --git a/ipapython/config.py b/ipapython/config.py index f71c696..5f1295c 100644 --- a/ipapython/config.py +++ b/ipapython/config.py @@ -23,29 +23,8 @@ from optparse import ( # pylint: enable=deprecated-module from copy import copy -from dns import resolver, rdatatype -from dns.exception import DNSException -# pylint: disable=import-error -from six.moves.configparser import SafeConfigParser -from six.moves.urllib.parse import urlsplit -# pylint: enable=import-error - from ipapython.dn import DN -from ipaplatform.paths import paths -import dns.name - -import socket - - -class IPAConfigError(Exception): - def __init__(self, msg=''): - self.msg = msg - Exception.__init__(self, msg) - def __repr__(self): - return self.msg - - __str__ = __repr__ class IPAFormatter(IndentedHelpFormatter): """Our own optparse formatter that indents multiple lined usage string.""" @@ -132,131 +111,8 @@ def verify_args(parser, args, needed_args = None): parser.error("no %s specified" % needed_list[len_have]) -class IPAConfig(object): - def __init__(self): - self.default_realm = None - self.default_server = [] - self.default_domain = None - - def get_realm(self): - if self.default_realm: - return self.default_realm - else: - raise IPAConfigError("no default realm") - - def get_server(self): - if len(self.default_server): - return self.default_server - else: - raise IPAConfigError("no default server") - - def get_domain(self): - if self.default_domain: - return self.default_domain - else: - raise IPAConfigError("no default domain") - -# Global library config -config = IPAConfig() - -def __parse_config(discover_server = True): - p = SafeConfigParser() - p.read(paths.IPA_DEFAULT_CONF) - - try: - if not config.default_realm: - config.default_realm = p.get("global", "realm") - except Exception: - pass - if discover_server: - try: - s = p.get("global", "xmlrpc_uri") - server = urlsplit(s) - config.default_server.append(server.netloc) - except Exception: - pass - try: - if not config.default_domain: - config.default_domain = p.get("global", "domain") - except Exception: - pass - -def __discover_config(discover_server = True): - servers = [] - try: - if not config.default_domain: - # try once with REALM -> domain - domain = str(config.default_realm).lower() - name = "_ldap._tcp." + domain - - try: - servers = resolver.query(name, rdatatype.SRV) - except DNSException: - # try cycling on domain components of FQDN - try: - domain = dns.name.from_text(socket.getfqdn()) - except DNSException: - return False - - while True: - domain = domain.parent() - - if str(domain) == '.': - return False - name = "_ldap._tcp.%s" % domain - try: - servers = resolver.query(name, rdatatype.SRV) - break - except DNSException: - pass - - config.default_domain = str(domain).rstrip(".") - - if discover_server: - if not servers: - name = "_ldap._tcp.%s." % config.default_domain - try: - servers = resolver.query(name, rdatatype.SRV) - except DNSException: - pass - - for server in servers: - hostname = str(server.target).rstrip(".") - config.default_server.append(hostname) - - except Exception: - pass - def add_standard_options(parser): parser.add_option("--realm", dest="realm", help="Override default IPA realm") parser.add_option("--server", dest="server", help="Override default FQDN of IPA server") parser.add_option("--domain", dest="domain", help="Override default IPA DNS domain") - -def init_config(options=None): - if options: - config.default_realm = options.realm - config.default_domain = options.domain - if options.server: - config.default_server.extend(options.server.split(",")) - - if len(config.default_server): - discover_server = False - else: - discover_server = True - __parse_config(discover_server) - __discover_config(discover_server) - - # make sure the server list only contains unique items - new_server = [] - for server in config.default_server: - if server not in new_server: - new_server.append(server) - config.default_server = new_server - - if not config.default_realm: - raise IPAConfigError("IPA realm not found in DNS, in the config file (/etc/ipa/default.conf) or on the command line.") - if not config.default_server: - raise IPAConfigError("IPA server not found in DNS, in the config file (/etc/ipa/default.conf) or on the command line.") - if not config.default_domain: - raise IPAConfigError("IPA domain not found in the config file (/etc/ipa/default.conf) or on the command line.") diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py index e613fd4..762cd91 100644 --- a/ipapython/ipautil.py +++ b/ipapython/ipautil.py @@ -50,7 +50,6 @@ from six.moves import input from six.moves import urllib from ipapython.ipa_log_manager import root_logger -from ipapython import config from ipaplatform.paths import paths from ipapython.dn import DN @@ -63,16 +62,6 @@ KRB5KDC_ERR_SVC_UNAVAILABLE = 2529638941 # A service is not available that is # required to process the request -def get_domain_name(): - try: - config.init_config() - domain_name = config.config.get_domain() - except Exception: - return None - - return domain_name - - class UnsafeIPAddress(netaddr.IPAddress): """Any valid IP address with or without netmask.""" diff --git a/ipaserver/install/installutils.py b/ipaserver/install/installutils.py index 1c8ea94..3968b5a 100644 --- a/ipaserver/install/installutils.py +++ b/ipaserver/install/installutils.py @@ -50,7 +50,6 @@ from ipapython import ipautil, admintool, version from ipapython.admintool import ScriptError from ipapython.ipa_log_manager import root_logger from ipalib.util import validate_hostname -from ipapython import config from ipalib import api, errors, x509 from ipapython.dn import DN from ipaserver.install import certs, service, sysupgrade @@ -848,10 +847,6 @@ def handle_error(error, log_file_name=None): ) return message, 1 - if isinstance(error, config.IPAConfigError): - message = "An IPA server to update cannot be found. Has one been configured yet?" - message += "\nThe error was: %s" % error - return message, 1 if isinstance(error, errors.LDAPError): return "An error occurred while performing operations: %s" % error, 1 diff --git a/ipaserver/install/ldapupdate.py b/ipaserver/install/ldapupdate.py index 1e49e7a..576b035 100644 --- a/ipaserver/install/ldapupdate.py +++ b/ipaserver/install/ldapupdate.py @@ -274,7 +274,6 @@ class LDAPUpdate(object): self.ldapuri = installutils.realm_to_ldapi_uri(self.realm) if suffix is not None: assert isinstance(suffix, DN) - domain = ipautil.get_domain_name() libarch = self._identify_arch() fqdn = installutils.get_fqdn() @@ -286,7 +285,7 @@ class LDAPUpdate(object): if not self.sub_dict.get("FQDN"): self.sub_dict["FQDN"] = fqdn if not self.sub_dict.get("DOMAIN"): - self.sub_dict["DOMAIN"] = domain + self.sub_dict["DOMAIN"] = api.env.domain if not self.sub_dict.get("SUFFIX") and suffix is not None: self.sub_dict["SUFFIX"] = suffix if not self.sub_dict.get("ESCAPED_SUFFIX"): @@ -295,8 +294,6 @@ class LDAPUpdate(object): self.sub_dict["LIBARCH"] = libarch if not self.sub_dict.get("TIME"): self.sub_dict["TIME"] = int(time.time()) - if not self.sub_dict.get("DOMAIN") and domain is not None: - self.sub_dict["DOMAIN"] = domain if not self.sub_dict.get("MIN_DOMAIN_LEVEL"): self.sub_dict["MIN_DOMAIN_LEVEL"] = str(constants.MIN_DOMAIN_LEVEL) if not self.sub_dict.get("MAX_DOMAIN_LEVEL"): diff --git a/ipaserver/plugins/realmdomains.py b/ipaserver/plugins/realmdomains.py index 26cd5bd..13631e4 100644 --- a/ipaserver/plugins/realmdomains.py +++ b/ipaserver/plugins/realmdomains.py @@ -27,7 +27,6 @@ from .baseldap import LDAPObject, LDAPUpdate, LDAPRetrieve from ipalib.util import has_soa_or_ns_record, validate_domain_name from ipalib.util import detect_dns_zone_realm_type from ipapython.dn import DN -from ipapython.ipautil import get_domain_name if six.PY3: unicode = str @@ -209,7 +208,7 @@ class realmdomains_mod(LDAPUpdate): del_domain = entry_attrs.get('del_domain') force = options.get('force') - current_domain = get_domain_name() + current_domain = self.api.env.domain # User specified the list of domains explicitly if associateddomain: