2020-03-09 10:11:07 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2016, Peter Sagerson <psagers@ignorare.net>
|
|
|
|
# Copyright (c) 2016, Jiri Tyr <jiri.tyr@gmail.com>
|
|
|
|
# Copyright (c) 2017-2018 Keller Fuchs (@KellerFuchs) <kellerfuchs@hashbang.sh>
|
2020-03-09 10:11:07 +01:00
|
|
|
#
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2023-02-27 20:26:01 +01:00
|
|
|
import re
|
2020-03-09 10:11:07 +01:00
|
|
|
import traceback
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
import ldap
|
2022-11-05 12:43:28 +01:00
|
|
|
import ldap.dn
|
|
|
|
import ldap.filter
|
2020-03-09 10:11:07 +01:00
|
|
|
import ldap.sasl
|
|
|
|
|
|
|
|
HAS_LDAP = True
|
2021-01-27 08:15:38 +01:00
|
|
|
|
|
|
|
SASCL_CLASS = {
|
|
|
|
'gssapi': ldap.sasl.gssapi,
|
|
|
|
'external': ldap.sasl.external,
|
|
|
|
}
|
2020-03-09 10:11:07 +01:00
|
|
|
except ImportError:
|
|
|
|
HAS_LDAP = False
|
|
|
|
|
|
|
|
|
|
|
|
def gen_specs(**specs):
|
|
|
|
specs.update({
|
|
|
|
'bind_dn': dict(),
|
|
|
|
'bind_pw': dict(default='', no_log=True),
|
2023-03-22 07:39:58 +01:00
|
|
|
'ca_path': dict(type='path'),
|
2020-03-09 10:11:07 +01:00
|
|
|
'dn': dict(required=True),
|
2021-01-27 07:56:07 +01:00
|
|
|
'referrals_chasing': dict(type='str', default='anonymous', choices=['disabled', 'anonymous']),
|
2020-03-09 10:11:07 +01:00
|
|
|
'server_uri': dict(default='ldapi:///'),
|
|
|
|
'start_tls': dict(default=False, type='bool'),
|
|
|
|
'validate_certs': dict(default=True, type='bool'),
|
2021-01-27 08:15:38 +01:00
|
|
|
'sasl_class': dict(choices=['external', 'gssapi'], default='external', type='str'),
|
2023-02-27 20:26:01 +01:00
|
|
|
'xorder_discovery': dict(choices=['enable', 'auto', 'disable'], default='auto', type='str'),
|
2023-06-15 08:42:42 +02:00
|
|
|
'client_cert': dict(default=None, type='path'),
|
|
|
|
'client_key': dict(default=None, type='path'),
|
2020-03-09 10:11:07 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return specs
|
|
|
|
|
|
|
|
|
2023-06-15 08:42:42 +02:00
|
|
|
def ldap_required_together():
|
|
|
|
return [['client_cert', 'client_key']]
|
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
class LdapGeneric(object):
|
|
|
|
def __init__(self, module):
|
|
|
|
# Shortcuts
|
|
|
|
self.module = module
|
|
|
|
self.bind_dn = self.module.params['bind_dn']
|
|
|
|
self.bind_pw = self.module.params['bind_pw']
|
2023-03-22 07:39:58 +01:00
|
|
|
self.ca_path = self.module.params['ca_path']
|
2021-01-27 07:56:07 +01:00
|
|
|
self.referrals_chasing = self.module.params['referrals_chasing']
|
2020-03-09 10:11:07 +01:00
|
|
|
self.server_uri = self.module.params['server_uri']
|
|
|
|
self.start_tls = self.module.params['start_tls']
|
|
|
|
self.verify_cert = self.module.params['validate_certs']
|
2021-01-27 08:15:38 +01:00
|
|
|
self.sasl_class = self.module.params['sasl_class']
|
2023-02-27 20:26:01 +01:00
|
|
|
self.xorder_discovery = self.module.params['xorder_discovery']
|
2023-06-15 08:42:42 +02:00
|
|
|
self.client_cert = self.module.params['client_cert']
|
|
|
|
self.client_key = self.module.params['client_key']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# Establish connection
|
|
|
|
self.connection = self._connect_to_ldap()
|
|
|
|
|
2023-02-27 20:26:01 +01:00
|
|
|
if self.xorder_discovery == "enable" or (self.xorder_discovery == "auto" and not self._xorder_dn()):
|
|
|
|
# Try to find the X_ORDERed version of the DN
|
|
|
|
self.dn = self._find_dn()
|
|
|
|
else:
|
|
|
|
self.dn = self.module.params['dn']
|
2022-11-05 12:43:28 +01:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def fail(self, msg, exn):
|
|
|
|
self.module.fail_json(
|
|
|
|
msg=msg,
|
|
|
|
details=to_native(exn),
|
|
|
|
exception=traceback.format_exc()
|
|
|
|
)
|
|
|
|
|
2022-11-05 12:43:28 +01:00
|
|
|
def _find_dn(self):
|
|
|
|
dn = self.module.params['dn']
|
|
|
|
|
|
|
|
explode_dn = ldap.dn.explode_dn(dn)
|
|
|
|
|
|
|
|
if len(explode_dn) > 1:
|
|
|
|
try:
|
|
|
|
escaped_value = ldap.filter.escape_filter_chars(explode_dn[0])
|
|
|
|
filterstr = "(%s)" % escaped_value
|
|
|
|
dns = self.connection.search_s(','.join(explode_dn[1:]),
|
|
|
|
ldap.SCOPE_ONELEVEL, filterstr)
|
|
|
|
if len(dns) == 1:
|
|
|
|
dn, dummy = dns[0]
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return dn
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def _connect_to_ldap(self):
|
|
|
|
if not self.verify_cert:
|
|
|
|
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
|
|
|
|
|
2023-03-22 07:39:58 +01:00
|
|
|
if self.ca_path:
|
|
|
|
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, self.ca_path)
|
|
|
|
|
2023-06-15 08:42:42 +02:00
|
|
|
if self.client_cert and self.client_key:
|
|
|
|
ldap.set_option(ldap.OPT_X_TLS_CERTFILE, self.client_cert)
|
|
|
|
ldap.set_option(ldap.OPT_X_TLS_KEYFILE, self.client_key)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
connection = ldap.initialize(self.server_uri)
|
|
|
|
|
2021-01-27 07:56:07 +01:00
|
|
|
if self.referrals_chasing == 'disabled':
|
|
|
|
# Switch off chasing of referrals (https://github.com/ansible-collections/community.general/issues/1067)
|
|
|
|
connection.set_option(ldap.OPT_REFERRALS, 0)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
if self.start_tls:
|
|
|
|
try:
|
|
|
|
connection.start_tls_s()
|
|
|
|
except ldap.LDAPError as e:
|
|
|
|
self.fail("Cannot start TLS.", e)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if self.bind_dn is not None:
|
|
|
|
connection.simple_bind_s(self.bind_dn, self.bind_pw)
|
|
|
|
else:
|
2021-01-27 08:15:38 +01:00
|
|
|
klass = SASCL_CLASS.get(self.sasl_class, ldap.sasl.external)
|
|
|
|
connection.sasl_interactive_bind_s('', klass())
|
2020-03-09 10:11:07 +01:00
|
|
|
except ldap.LDAPError as e:
|
|
|
|
self.fail("Cannot bind to the server.", e)
|
|
|
|
|
|
|
|
return connection
|
2023-02-27 20:26:01 +01:00
|
|
|
|
|
|
|
def _xorder_dn(self):
|
|
|
|
# match X_ORDERed DNs
|
2024-01-29 19:30:04 +01:00
|
|
|
regex = r".+\{\d+\}.+"
|
|
|
|
explode_dn = ldap.dn.explode_dn(self.module.params['dn'])
|
|
|
|
|
|
|
|
return re.match(regex, explode_dn[0]) is not None
|