0000-12-31 18:09:24 -05:50
|
|
|
# Authors: Karl MacMillan <kmacmill@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.
|
0000-12-31 18:09:24 -05:50
|
|
|
#
|
|
|
|
# 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/>.
|
0000-12-31 18:09:24 -05:50
|
|
|
#
|
|
|
|
|
2016-11-23 03:04:43 -06:00
|
|
|
# pylint: disable=deprecated-module
|
|
|
|
from optparse import (
|
|
|
|
Option, Values, OptionParser, IndentedHelpFormatter, OptionValueError)
|
|
|
|
# pylint: enable=deprecated-module
|
2011-05-27 13:17:22 -05:00
|
|
|
from copy import copy
|
2015-09-14 07:03:58 -05:00
|
|
|
|
2012-05-11 07:38:09 -05:00
|
|
|
from dns import resolver, rdatatype
|
|
|
|
from dns.exception import DNSException
|
2016-08-24 06:37:30 -05:00
|
|
|
# pylint: disable=import-error
|
2015-09-14 07:03:58 -05:00
|
|
|
from six.moves.configparser import SafeConfigParser
|
2016-08-24 06:37:30 -05:00
|
|
|
from six.moves.urllib.parse import urlsplit
|
|
|
|
# pylint: enable=import-error
|
2015-09-14 07:03:58 -05:00
|
|
|
|
2012-08-13 02:38:24 -05:00
|
|
|
from ipapython.dn import DN
|
2014-05-29 07:47:17 -05:00
|
|
|
from ipaplatform.paths import paths
|
2012-05-11 07:38:09 -05:00
|
|
|
import dns.name
|
0000-12-31 18:09:24 -05:50
|
|
|
|
2007-12-11 09:58:39 -06:00
|
|
|
import socket
|
2015-12-16 09:06:03 -06:00
|
|
|
|
2007-12-11 09:58:39 -06:00
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
class IPAConfigError(Exception):
|
|
|
|
def __init__(self, msg=''):
|
|
|
|
self.msg = msg
|
|
|
|
Exception.__init__(self, msg)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.msg
|
|
|
|
|
|
|
|
__str__ = __repr__
|
|
|
|
|
2008-08-15 11:08:01 -05:00
|
|
|
class IPAFormatter(IndentedHelpFormatter):
|
|
|
|
"""Our own optparse formatter that indents multiple lined usage string."""
|
|
|
|
def format_usage(self, usage):
|
|
|
|
usage_string = "Usage:"
|
|
|
|
spacing = " " * len(usage_string)
|
|
|
|
lines = usage.split("\n")
|
|
|
|
ret = "%s %s\n" % (usage_string, lines[0])
|
|
|
|
for line in lines[1:]:
|
|
|
|
ret += "%s %s\n" % (spacing, line)
|
|
|
|
return ret
|
|
|
|
|
2011-05-27 13:17:22 -05:00
|
|
|
def check_ip_option(option, opt, value):
|
|
|
|
from ipapython.ipautil import CheckedIPAddress
|
2011-06-16 03:47:11 -05:00
|
|
|
|
|
|
|
ip_local = option.ip_local is True
|
|
|
|
ip_netmask = option.ip_netmask is True
|
2011-05-27 13:17:22 -05:00
|
|
|
try:
|
2011-06-16 03:47:11 -05:00
|
|
|
return CheckedIPAddress(value, parse_netmask=ip_netmask, match_local=ip_local)
|
2011-05-27 13:17:22 -05:00
|
|
|
except Exception as e:
|
|
|
|
raise OptionValueError("option %s: invalid IP address %s: %s" % (opt, value, e))
|
|
|
|
|
2012-08-13 02:38:24 -05:00
|
|
|
def check_dn_option(option, opt, value):
|
|
|
|
try:
|
|
|
|
return DN(value)
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2012-08-13 02:38:24 -05:00
|
|
|
raise OptionValueError("option %s: invalid DN: %s" % (opt, e))
|
|
|
|
|
2010-10-29 13:24:31 -05:00
|
|
|
class IPAOption(Option):
|
|
|
|
"""
|
|
|
|
optparse.Option subclass with support of options labeled as
|
|
|
|
security-sensitive such as passwords.
|
|
|
|
"""
|
2011-06-16 03:47:11 -05:00
|
|
|
ATTRS = Option.ATTRS + ["sensitive", "ip_local", "ip_netmask"]
|
2012-08-13 02:38:24 -05:00
|
|
|
TYPES = Option.TYPES + ("ip", "dn")
|
2011-05-27 13:17:22 -05:00
|
|
|
TYPE_CHECKER = copy(Option.TYPE_CHECKER)
|
2011-06-16 03:47:11 -05:00
|
|
|
TYPE_CHECKER["ip"] = check_ip_option
|
2012-08-13 02:38:24 -05:00
|
|
|
TYPE_CHECKER["dn"] = check_dn_option
|
2010-10-29 13:24:31 -05:00
|
|
|
|
|
|
|
class IPAOptionParser(OptionParser):
|
|
|
|
"""
|
|
|
|
optparse.OptionParser subclass that uses IPAOption by default
|
|
|
|
for storing options.
|
|
|
|
"""
|
|
|
|
def __init__(self,
|
|
|
|
usage=None,
|
|
|
|
option_list=None,
|
|
|
|
option_class=IPAOption,
|
|
|
|
version=None,
|
|
|
|
conflict_handler="error",
|
|
|
|
description=None,
|
|
|
|
formatter=None,
|
|
|
|
add_help_option=True,
|
|
|
|
prog=None):
|
|
|
|
OptionParser.__init__(self, usage, option_list, option_class,
|
|
|
|
version, conflict_handler, description,
|
|
|
|
formatter, add_help_option, prog)
|
|
|
|
|
|
|
|
def get_safe_opts(self, opts):
|
|
|
|
"""
|
|
|
|
Returns all options except those with sensitive=True in the same
|
|
|
|
fashion as parse_args would
|
|
|
|
"""
|
|
|
|
all_opts_dict = dict([ (o.dest, o) for o in self._get_all_options() if hasattr(o, 'sensitive') ])
|
|
|
|
safe_opts_dict = {}
|
|
|
|
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
for option, value in opts.__dict__.items():
|
2010-10-29 13:24:31 -05:00
|
|
|
if all_opts_dict[option].sensitive != True:
|
|
|
|
safe_opts_dict[option] = value
|
|
|
|
|
|
|
|
return Values(safe_opts_dict)
|
|
|
|
|
2008-08-15 11:08:01 -05:00
|
|
|
def verify_args(parser, args, needed_args = None):
|
|
|
|
"""Verify that we have all positional arguments we need, if not, exit."""
|
|
|
|
if needed_args:
|
|
|
|
needed_list = needed_args.split(" ")
|
|
|
|
else:
|
|
|
|
needed_list = []
|
|
|
|
len_need = len(needed_list)
|
|
|
|
len_have = len(args)
|
|
|
|
if len_have > len_need:
|
|
|
|
parser.error("too many arguments")
|
|
|
|
elif len_have < len_need:
|
|
|
|
parser.error("no %s specified" % needed_list[len_have])
|
|
|
|
|
2016-06-03 05:45:01 -05:00
|
|
|
|
|
|
|
class IPAConfig(object):
|
0000-12-31 18:09:24 -05:50
|
|
|
def __init__(self):
|
|
|
|
self.default_realm = None
|
2008-02-22 13:47:15 -06:00
|
|
|
self.default_server = []
|
2008-05-23 13:51:50 -05:00
|
|
|
self.default_domain = None
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
def get_realm(self):
|
|
|
|
if self.default_realm:
|
|
|
|
return self.default_realm
|
|
|
|
else:
|
|
|
|
raise IPAConfigError("no default realm")
|
|
|
|
|
|
|
|
def get_server(self):
|
2008-05-08 17:05:29 -05:00
|
|
|
if len(self.default_server):
|
0000-12-31 18:09:24 -05:50
|
|
|
return self.default_server
|
|
|
|
else:
|
|
|
|
raise IPAConfigError("no default server")
|
|
|
|
|
2008-05-23 13:51:50 -05:00
|
|
|
def get_domain(self):
|
|
|
|
if self.default_domain:
|
|
|
|
return self.default_domain
|
|
|
|
else:
|
|
|
|
raise IPAConfigError("no default domain")
|
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
# Global library config
|
|
|
|
config = IPAConfig()
|
|
|
|
|
2008-09-17 05:58:20 -05:00
|
|
|
def __parse_config(discover_server = True):
|
2015-09-14 07:03:58 -05:00
|
|
|
p = SafeConfigParser()
|
2014-05-29 07:47:17 -05:00
|
|
|
p.read(paths.IPA_DEFAULT_CONF)
|
0000-12-31 18:09:24 -05:50
|
|
|
|
|
|
|
try:
|
2007-12-11 09:58:39 -06:00
|
|
|
if not config.default_realm:
|
2009-11-25 16:16:06 -06:00
|
|
|
config.default_realm = p.get("global", "realm")
|
2016-03-22 10:39:39 -05:00
|
|
|
except Exception:
|
2008-08-15 11:08:01 -05:00
|
|
|
pass
|
2008-09-17 05:58:20 -05:00
|
|
|
if discover_server:
|
|
|
|
try:
|
2009-11-25 16:16:06 -06:00
|
|
|
s = p.get("global", "xmlrpc_uri")
|
2015-09-14 05:52:29 -05:00
|
|
|
server = urlsplit(s)
|
2012-05-23 06:36:21 -05:00
|
|
|
config.default_server.append(server.netloc)
|
2016-03-11 12:51:07 -06:00
|
|
|
except Exception:
|
2008-09-17 05:58:20 -05:00
|
|
|
pass
|
2008-08-15 11:08:01 -05:00
|
|
|
try:
|
2008-05-23 13:51:50 -05:00
|
|
|
if not config.default_domain:
|
2009-11-25 16:16:06 -06:00
|
|
|
config.default_domain = p.get("global", "domain")
|
2016-03-22 10:39:39 -05:00
|
|
|
except Exception:
|
0000-12-31 18:09:24 -05:50
|
|
|
pass
|
|
|
|
|
2008-09-17 05:58:20 -05:00
|
|
|
def __discover_config(discover_server = True):
|
2012-05-11 07:38:09 -05:00
|
|
|
servers = []
|
2007-12-11 09:58:39 -06:00
|
|
|
try:
|
2008-05-23 13:51:50 -05:00
|
|
|
if not config.default_domain:
|
2012-05-11 07:38:09 -05:00
|
|
|
# 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:
|
2009-11-25 16:16:06 -06:00
|
|
|
return False
|
2008-05-23 13:51:50 -05:00
|
|
|
|
2012-05-11 07:38:09 -05:00
|
|
|
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(".")
|
2008-04-09 13:37:01 -05:00
|
|
|
|
2008-09-17 05:58:20 -05:00
|
|
|
if discover_server:
|
2012-05-11 07:38:09 -05:00
|
|
|
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)
|
2008-04-09 13:37:01 -05:00
|
|
|
|
2016-03-11 12:51:07 -06:00
|
|
|
except Exception:
|
2008-04-09 13:37:01 -05:00
|
|
|
pass
|
2007-12-11 09:58:39 -06:00
|
|
|
|
2008-08-15 11:08:01 -05:00
|
|
|
def add_standard_options(parser):
|
|
|
|
parser.add_option("--realm", dest="realm", help="Override default IPA realm")
|
2016-09-22 10:37:25 -05:00
|
|
|
parser.add_option("--server", dest="server",
|
|
|
|
help="Override default FQDN of IPA server")
|
2008-08-15 11:08:01 -05:00
|
|
|
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(","))
|
0000-12-31 18:09:24 -05:50
|
|
|
|
2008-09-17 05:58:20 -05:00
|
|
|
if len(config.default_server):
|
|
|
|
discover_server = False
|
|
|
|
else:
|
|
|
|
discover_server = True
|
|
|
|
__parse_config(discover_server)
|
|
|
|
__discover_config(discover_server)
|
2007-12-11 09:58:39 -06:00
|
|
|
|
2008-08-15 11:08:01 -05:00
|
|
|
# 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
|
2008-06-04 12:44:08 -05:00
|
|
|
|
0000-12-31 18:09:24 -05:50
|
|
|
if not config.default_realm:
|
2009-11-25 16:16:06 -06:00
|
|
|
raise IPAConfigError("IPA realm not found in DNS, in the config file (/etc/ipa/default.conf) or on the command line.")
|
0000-12-31 18:09:24 -05:50
|
|
|
if not config.default_server:
|
2009-11-25 16:16:06 -06:00
|
|
|
raise IPAConfigError("IPA server not found in DNS, in the config file (/etc/ipa/default.conf) or on the command line.")
|
2008-05-23 13:51:50 -05:00
|
|
|
if not config.default_domain:
|
2009-11-25 16:16:06 -06:00
|
|
|
raise IPAConfigError("IPA domain not found in the config file (/etc/ipa/default.conf) or on the command line.")
|