| 1 |
# Copyright (c) 2016, Peter Sagerson <[email protected]> |
| 2 |
# Copyright (c) 2016, Jiri Tyr <[email protected]> |
| 3 |
# Copyright (c) 2017-2018 Keller Fuchs (@KellerFuchs) <[email protected]> |
| 4 |
# |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 9 |
# Do not use this from other collections or standalone plugins/modules! |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
import re |
| 14 |
import traceback |
| 15 |
import typing as t |
| 16 |
|
| 17 |
if t.TYPE_CHECKING: |
| 18 |
from ansible.module_utils.basic import AnsibleModule |
| 19 |
|
| 20 |
try: |
| 21 |
import ldap |
| 22 |
import ldap.dn |
| 23 |
import ldap.filter |
| 24 |
import ldap.sasl |
| 25 |
|
| 26 |
HAS_LDAP = True |
| 27 |
|
| 28 |
SASCL_CLASS = { |
| 29 |
"gssapi": ldap.sasl.gssapi, |
| 30 |
"external": ldap.sasl.external, |
| 31 |
} |
| 32 |
except ImportError: |
| 33 |
HAS_LDAP = False |
| 34 |
|
| 35 |
|
| 36 |
def gen_specs(**specs: t.Any) -> dict[str, t.Any]: |
| 37 |
specs.update( |
| 38 |
{ |
| 39 |
"bind_dn": dict(), |
| 40 |
"bind_pw": dict(default="", no_log=True), |
| 41 |
"ca_path": dict(type="path"), |
| 42 |
"dn": dict(required=True), |
| 43 |
"referrals_chasing": dict(type="str", default="anonymous", choices=["disabled", "anonymous"]), |
| 44 |
"server_uri": dict(default="ldapi:///"), |
| 45 |
"start_tls": dict(default=False, type="bool"), |
| 46 |
"validate_certs": dict(default=True, type="bool"), |
| 47 |
"sasl_class": dict(choices=["external", "gssapi"], default="external", type="str"), |
| 48 |
"xorder_discovery": dict(choices=["enable", "auto", "disable"], default="auto", type="str"), |
| 49 |
"client_cert": dict(default=None, type="path"), |
| 50 |
"client_key": dict(default=None, type="path"), |
| 51 |
} |
| 52 |
) |
| 53 |
|
| 54 |
return specs |
| 55 |
|
| 56 |
|
| 57 |
def ldap_required_together() -> list[list[str]]: |
| 58 |
return [["client_cert", "client_key"]] |
| 59 |
|
| 60 |
|
| 61 |
class LdapGeneric: |
| 62 |
def __init__(self, module: AnsibleModule) -> None: |
| 63 |
# Shortcuts |
| 64 |
self.module = module |
| 65 |
self.bind_dn = self.module.params["bind_dn"] |
| 66 |
self.bind_pw = self.module.params["bind_pw"] |
| 67 |
self.ca_path = self.module.params["ca_path"] |
| 68 |
self.referrals_chasing = self.module.params["referrals_chasing"] |
| 69 |
self.server_uri = self.module.params["server_uri"] |
| 70 |
self.start_tls = self.module.params["start_tls"] |
| 71 |
self.verify_cert = self.module.params["validate_certs"] |
| 72 |
self.sasl_class = self.module.params["sasl_class"] |
| 73 |
self.xorder_discovery = self.module.params["xorder_discovery"] |
| 74 |
self.client_cert = self.module.params["client_cert"] |
| 75 |
self.client_key = self.module.params["client_key"] |
| 76 |
|
| 77 |
# Establish connection |
| 78 |
self.connection = self._connect_to_ldap() |
| 79 |
|
| 80 |
if self.xorder_discovery == "enable" or (self.xorder_discovery == "auto" and not self._xorder_dn()): |
| 81 |
# Try to find the X_ORDERed version of the DN |
| 82 |
self.dn = self._find_dn() |
| 83 |
else: |
| 84 |
self.dn = self.module.params["dn"] |
| 85 |
|
| 86 |
def fail(self, msg: str, exn: str | Exception) -> t.NoReturn: |
| 87 |
self.module.fail_json(msg=msg, details=f"{exn}", exception=traceback.format_exc()) |
| 88 |
|
| 89 |
def _find_dn(self) -> str: |
| 90 |
dn: str = self.module.params["dn"] |
| 91 |
|
| 92 |
explode_dn = ldap.dn.explode_dn(dn) |
| 93 |
|
| 94 |
if len(explode_dn) > 1: |
| 95 |
try: |
| 96 |
escaped_value = ldap.filter.escape_filter_chars(explode_dn[0]) |
| 97 |
filterstr = f"({escaped_value})" |
| 98 |
dns = self.connection.search_s(",".join(explode_dn[1:]), ldap.SCOPE_ONELEVEL, filterstr) |
| 99 |
if len(dns) == 1: |
| 100 |
dn, dummy = dns[0] |
| 101 |
except Exception: |
| 102 |
pass |
| 103 |
|
| 104 |
return dn |
| 105 |
|
| 106 |
def _connect_to_ldap(self): |
| 107 |
if not self.verify_cert: |
| 108 |
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) |
| 109 |
|
| 110 |
if self.ca_path: |
| 111 |
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, self.ca_path) |
| 112 |
|
| 113 |
if self.client_cert and self.client_key: |
| 114 |
ldap.set_option(ldap.OPT_X_TLS_CERTFILE, self.client_cert) |
| 115 |
ldap.set_option(ldap.OPT_X_TLS_KEYFILE, self.client_key) |
| 116 |
|
| 117 |
connection = ldap.initialize(self.server_uri) |
| 118 |
|
| 119 |
if self.referrals_chasing == "disabled": |
| 120 |
# Switch off chasing of referrals (https://github.com/ansible-collections/community.general/issues/1067) |
| 121 |
connection.set_option(ldap.OPT_REFERRALS, 0) |
| 122 |
|
| 123 |
if self.start_tls: |
| 124 |
try: |
| 125 |
connection.start_tls_s() |
| 126 |
except ldap.LDAPError as e: |
| 127 |
self.fail("Cannot start TLS.", e) |
| 128 |
|
| 129 |
try: |
| 130 |
if self.bind_dn is not None: |
| 131 |
connection.simple_bind_s(self.bind_dn, self.bind_pw) |
| 132 |
else: |
| 133 |
klass = SASCL_CLASS.get(self.sasl_class, ldap.sasl.external) |
| 134 |
connection.sasl_interactive_bind_s("", klass()) |
| 135 |
except ldap.LDAPError as e: |
| 136 |
self.fail("Cannot bind to the server.", e) |
| 137 |
|
| 138 |
return connection |
| 139 |
|
| 140 |
def _xorder_dn(self) -> bool: |
| 141 |
# match X_ORDERed DNs |
| 142 |
regex = r".+\{\d+\}.+" |
| 143 |
explode_dn = ldap.dn.explode_dn(self.module.params["dn"]) |
| 144 |
|
| 145 |
return re.match(regex, explode_dn[0]) is not None |