Beispiel #1
0
def get_server_ip_address(host_name, unattended, setup_dns, ip_addresses):
    hostaddr = resolve_ip_addresses_nss(host_name)
    if hostaddr.intersection(
            {ipautil.UnsafeIPAddress(ip) for ip in ['127.0.0.1', '::1']}):
        print("The hostname resolves to the localhost address (127.0.0.1/::1)", file=sys.stderr)
        print("Please change your /etc/hosts file so that the hostname", file=sys.stderr)
        print("resolves to the ip address of your network interface.", file=sys.stderr)
        print("The KDC service does not listen on localhost", file=sys.stderr)
        print("", file=sys.stderr)
        print("Please fix your /etc/hosts file and restart the setup program", file=sys.stderr)
        raise ScriptError()

    ips = []
    if len(hostaddr):
        for ha in hostaddr:
            try:
                ips.append(ipautil.CheckedIPAddress(ha))
            except ValueError as e:
                logger.warning("Invalid IP address %s for %s: %s",
                               ha, host_name, unicode(e))

    if not ips and not ip_addresses:
        if not unattended:
            ip_addresses = read_ip_addresses()

    if ip_addresses:
        if setup_dns:
            ips = ip_addresses
        else:
            # all specified addresses was resolved for this host
            if set(ip_addresses) <= set(ips):
                ips = ip_addresses
            else:
                print("Error: the hostname resolves to IP address(es) that are different", file=sys.stderr)
                print("from those provided on the command line.  Please fix your DNS", file=sys.stderr)
                print("or /etc/hosts file and restart the installation.", file=sys.stderr)
                print("Provided but not resolved address(es): %s" % \
                                    ", ".join(str(ip) for ip in (set(ip_addresses) - set(ips))), file=sys.stderr)
                raise ScriptError()

    if not ips:
        print("No usable IP address provided nor resolved.", file=sys.stderr)
        raise ScriptError()

    for ip_address in ips:
        # check /etc/hosts sanity
        hosts_record = record_in_hosts(str(ip_address))

        if hosts_record is not None:
            primary_host = hosts_record[1][0]
            if primary_host != host_name:
                print("Error: there is already a record in /etc/hosts for IP address %s:" \
                        % ip_address, file=sys.stderr)
                print(hosts_record[0], " ".join(hosts_record[1]), file=sys.stderr)
                print("Chosen hostname %s does not match configured canonical hostname %s" \
                        % (host_name, primary_host), file=sys.stderr)
                print("Please fix your /etc/hosts file and restart the installation.", file=sys.stderr)
                raise ScriptError()

    return ips
Beispiel #2
0
    def check_ipv6_stack_enabled(self):
        """Checks whether IPv6 kernel module is loaded.

        Function checks if /proc/net/if_inet6 is present. If IPv6 stack is
        enabled, it exists and contains the interfaces configuration.

        :raises: RuntimeError when IPv6 stack is disabled
        """
        if not os.path.exists(paths.IF_INET6):
            raise RuntimeError(
                "IPv6 stack has to be enabled in the kernel and some "
                "interface has to have ::1 address assigned. Typically "
                "this is 'lo' interface. If you do not wish to use IPv6 "
                "globally, disable it on the specific interfaces in "
                "sysctl.conf except 'lo' interface.")

        try:
            localhost6 = ipautil.CheckedIPAddress('::1', allow_loopback=True)
            if localhost6.get_matching_interface() is None:
                raise ValueError("no interface for ::1 address found")
        except ValueError:
            raise RuntimeError(
                "IPv6 stack is enabled in the kernel but there is no "
                "interface that has ::1 address assigned. Add ::1 address "
                "resolution to 'lo' interface. You might need to enable IPv6 "
                "on the interface 'lo' in sysctl.conf.")
Beispiel #3
0
def test_ip_address(addr, words, prefixlen):
    if words is None:
        pytest.raises(ValueError, ipautil.CheckedIPAddress, addr)
    else:
        ip = ipautil.CheckedIPAddress(addr)
        assert ip.words == words
        assert ip.prefixlen == prefixlen
Beispiel #4
0
def read_dns_forwarders():
    addrs = []
    if ipautil.user_input("Do you want to configure DNS forwarders?", True):
        print("Following DNS servers are configured in /etc/resolv.conf: %s" %
                ", ".join(resolver.get_default_resolver().nameservers))
        if ipautil.user_input("Do you want to configure these servers as DNS "
                "forwarders?", True):
            addrs = resolver.default_resolver.nameservers[:]
            print("All DNS servers from /etc/resolv.conf were added. You can "
                  "enter additional addresses now:")
        while True:
            ip = ipautil.user_input("Enter an IP address for a DNS forwarder, "
                                    "or press Enter to skip", allow_empty=True)
            if not ip:
                break
            try:
                ip_parsed = ipautil.CheckedIPAddress(ip, parse_netmask=False)
            except Exception as e:
                print("Error: Invalid IP Address %s: %s" % (ip, e))
                print("DNS forwarder %s not added." % ip)
                continue

            print("DNS forwarder %s added. You may add another." % ip)
            addrs.append(str(ip_parsed))

    if not addrs:
        print("No DNS forwarders configured")

    return addrs
Beispiel #5
0
    def check_ipv6_stack_enabled(self):
        """Checks whether IPv6 kernel module is loaded.

        Function checks if /proc/net/if_inet6 is present. If IPv6 stack is
        enabled, it exists and contains the interfaces configuration.

        :raises: RuntimeError when IPv6 stack is disabled
        """
        if not os.path.exists(paths.IF_INET6):
            raise RuntimeError(
                "IPv6 stack has to be enabled in the kernel and some "
                "interface has to have ::1 address assigned. Typically "
                "this is 'lo' interface. If you do not wish to use IPv6 "
                "globally, disable it on the specific interfaces in "
                "sysctl.conf except 'lo' interface.")

        # XXX This is a hack to work around an issue with Travis CI by
        # skipping IPv6 address test. The Dec 2017 update removed ::1 from
        # loopback, see https://github.com/travis-ci/travis-ci/issues/8891.
        if os.environ.get('TRAVIS') == 'true':
            return

        try:
            localhost6 = ipautil.CheckedIPAddress('::1', allow_loopback=True)
            if localhost6.get_matching_interface() is None:
                raise ValueError("no interface for ::1 address found")
        except ValueError:
            raise RuntimeError(
                "IPv6 stack is enabled in the kernel but there is no "
                "interface that has ::1 address assigned. Add ::1 address "
                "resolution to 'lo' interface. You might need to enable IPv6 "
                "on the interface 'lo' in sysctl.conf.")
Beispiel #6
0
def ansible_module_get_parsed_ip_addresses(ansible_module,
                                           param='ip_addresses'):
    ip_addrs = []
    for ip in ansible_module.params.get(param):
        try:
            ip_parsed = ipautil.CheckedIPAddress(ip)
        except Exception as e:
            ansible_module.fail_json(msg="Invalid IP Address %s: %s" % (ip, e))
        ip_addrs.append(ip_parsed)
    return ip_addrs
Beispiel #7
0
def read_ip_address(host_name, fstore):
    while True:
        ip = ipautil.user_input(
            "Please provide the IP address to be used for this host name",
            allow_empty=False)
        try:
            ip_parsed = ipautil.CheckedIPAddress(ip, match_local=True)
        except Exception, e:
            print "Error: Invalid IP Address %s: %s" % (ip, e)
            continue
        else:
            break
Beispiel #8
0
def read_ip_addresses(host_name, fstore):
    ips = []
    print "Enter the IP address to use, or press Enter to finish."
    while True:
        ip = ipautil.user_input(
            "Please provide the IP address to be used for this host name",
            allow_empty=True)
        if not ip:
            break
        try:
            ip_parsed = ipautil.CheckedIPAddress(ip, match_local=True)
        except Exception, e:
            print "Error: Invalid IP Address %s: %s" % (ip, e)
            continue
        ips.append(ip_parsed)
Beispiel #9
0
def read_ip_addresses():
    ips = []
    msg_first = "Please provide the IP address to be used for this host name"
    msg_other = "Enter an additional IP address, or press Enter to skip"
    while True:
        msg = msg_other if ips else msg_first
        ip = ipautil.user_input(msg, allow_empty=True)
        if not ip:
            break
        try:
            ip_parsed = ipautil.CheckedIPAddress(ip)
        except Exception as e:
            print("Error: Invalid IP Address %s: %s" % (ip, e))
            continue
        ips.append(ip_parsed)

    return ips
Beispiel #10
0
def read_dns_forwarders():
    addrs = []
    if ipautil.user_input("Do you want to configure DNS forwarders?", True):
        print "Enter the IP address of DNS forwarder to use, or press Enter to finish."

        while True:
            ip = ipautil.user_input("Enter IP address for a DNS forwarder",
                                    allow_empty=True)
            if not ip:
                break
            try:
                ip_parsed = ipautil.CheckedIPAddress(ip, parse_netmask=False)
            except Exception, e:
                print "Error: Invalid IP Address %s: %s" % (ip, e)
                print "DNS forwarder %s not added" % ip
                continue

            print "DNS forwarder %s added" % ip
            addrs.append(str(ip_parsed))
Beispiel #11
0
def get_server_ip_address(host_name, fstore, unattended, options):
    # Check we have a public IP that is associated with the hostname
    try:
        hostaddr = resolve_host(host_name)
    except HostnameLocalhost:
        print >> sys.stderr, "The hostname resolves to the localhost address (127.0.0.1/::1)"
        print >> sys.stderr, "Please change your /etc/hosts file so that the hostname"
        print >> sys.stderr, "resolves to the ip address of your network interface."
        print >> sys.stderr, "The KDC service does not listen on localhost"
        print >> sys.stderr, ""
        print >> sys.stderr, "Please fix your /etc/hosts file and restart the setup program"
        sys.exit(1)

    ip_add_to_hosts = False

    if len(hostaddr) > 1:
        print >> sys.stderr, "The server hostname resolves to more than one address:"
        for addr in hostaddr:
            print >> sys.stderr, "  %s" % addr

        if options.ip_address:
            if str(options.ip_address) not in hostaddr:
                print >> sys.stderr, "Address passed in --ip-address did not match any resolved"
                print >> sys.stderr, "address!"
                sys.exit(1)
            print "Selected IP address:", str(options.ip_address)
            ip = options.ip_address
        else:
            if unattended:
                print >> sys.stderr, "Please use --ip-address option to specify the address"
                sys.exit(1)
            else:
                ip = read_ip_address(host_name, fstore)
    elif len(hostaddr) == 1:
        try:
            ip = ipautil.CheckedIPAddress(hostaddr[0], match_local=True)
        except ValueError, e:
            sys.exit("Invalid IP Address %s for %s: %s" %
                     (hostaddr[0], host_name, unicode(e)))
Beispiel #12
0
def get_server_ip_address(host_name, fstore, unattended, setup_dns,
                          ip_addresses):
    # Check we have a public IP that is associated with the hostname
    try:
        hostaddr = resolve_host(host_name)
    except HostnameLocalhost:
        print >> sys.stderr, "The hostname resolves to the localhost address (127.0.0.1/::1)"
        print >> sys.stderr, "Please change your /etc/hosts file so that the hostname"
        print >> sys.stderr, "resolves to the ip address of your network interface."
        print >> sys.stderr, "The KDC service does not listen on localhost"
        print >> sys.stderr, ""
        print >> sys.stderr, "Please fix your /etc/hosts file and restart the setup program"
        sys.exit(1)

    ip_add_to_hosts = False

    ips = []
    if len(hostaddr):
        for ha in hostaddr:
            try:
                ips.append(ipautil.CheckedIPAddress(ha, match_local=True))
            except ValueError, e:
                root_logger.warning("Invalid IP address %s for %s: %s", ha,
                                    host_name, unicode(e))
Beispiel #13
0
 def check_ipaddress():
     try:
         ip = ipautil.CheckedIPAddress(addr, match_local=False)
         assert ip.words == words and ip.prefixlen == prefixlen
     except:
         assert words is None and prefixlen is None
Beispiel #14
0
 def __call__(self, addr, words=None, prefixlen=None):
     try:
         ip = ipautil.CheckedIPAddress(addr, match_local=False)
         assert ip.words == words and ip.prefixlen == prefixlen
     except:
         assert words is None and prefixlen is None