| 1 |
# This code is part of Ansible, but is an independent component. |
| 2 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 3 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 4 |
# still belong to the author of the module, and may assign their own license |
| 5 |
# to the complete work. |
| 6 |
# |
| 7 |
# Copyright (c) 2016, Adfinis SyGroup AG |
| 8 |
# @keachi |
| 9 |
# Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 10 |
# SPDX-License-Identifier: BSD-2-Clause |
| 11 |
|
| 12 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 13 |
# Do not use this from other collections or standalone plugins/modules! |
| 14 |
|
| 15 |
from __future__ import annotations |
| 16 |
|
| 17 |
"""Univention Corporate Server (UCS) access module. |
| 18 |
|
| 19 |
Provides the following functions for working with an UCS server. |
| 20 |
|
| 21 |
- ldap_search(filter, base=None, attr=None) |
| 22 |
Search the LDAP via Univention's LDAP wrapper (ULDAP) |
| 23 |
|
| 24 |
- config_registry() |
| 25 |
Return the UCR registry object |
| 26 |
|
| 27 |
- base_dn() |
| 28 |
Return the configured Base DN according to the UCR |
| 29 |
|
| 30 |
- uldap() |
| 31 |
Return a handle to the ULDAP LDAP wrapper |
| 32 |
|
| 33 |
- umc_module_for_add(module, container_dn, superordinate=None) |
| 34 |
Return a UMC module for creating a new object of the given type |
| 35 |
|
| 36 |
- umc_module_for_edit(module, object_dn, superordinate=None) |
| 37 |
Return a UMC module for editing an existing object of the given type |
| 38 |
|
| 39 |
|
| 40 |
Any other module is not part of the "official" API and may change at any time. |
| 41 |
""" |
| 42 |
|
| 43 |
import re |
| 44 |
|
| 45 |
__all__ = [ |
| 46 |
"base_dn", |
| 47 |
"config_registry", |
| 48 |
"ldap_search", |
| 49 |
"uldap", |
| 50 |
"umc_module_for_add", |
| 51 |
"umc_module_for_edit", |
| 52 |
] |
| 53 |
|
| 54 |
|
| 55 |
_singletons: dict[str, object] = {} |
| 56 |
|
| 57 |
|
| 58 |
def ldap_module(): |
| 59 |
import ldap as orig_ldap |
| 60 |
|
| 61 |
return orig_ldap |
| 62 |
|
| 63 |
|
| 64 |
def _singleton(name: str, constructor): |
| 65 |
if name in _singletons: |
| 66 |
return _singletons[name] |
| 67 |
_singletons[name] = constructor() |
| 68 |
return _singletons[name] |
| 69 |
|
| 70 |
|
| 71 |
def config_registry(): |
| 72 |
def construct(): |
| 73 |
import univention.config_registry |
| 74 |
|
| 75 |
ucr = univention.config_registry.ConfigRegistry() |
| 76 |
ucr.load() |
| 77 |
return ucr |
| 78 |
|
| 79 |
return _singleton("config_registry", construct) |
| 80 |
|
| 81 |
|
| 82 |
def base_dn(): |
| 83 |
return config_registry()["ldap/base"] |
| 84 |
|
| 85 |
|
| 86 |
def uldap(): |
| 87 |
"Return a configured univention uldap object" |
| 88 |
|
| 89 |
def construct(): |
| 90 |
try: |
| 91 |
secret_file = open("/etc/ldap.secret") |
| 92 |
bind_dn = f"cn=admin,{base_dn()}" |
| 93 |
except OSError: # pragma: no cover |
| 94 |
secret_file = open("/etc/machine.secret") |
| 95 |
bind_dn = config_registry()["ldap/hostdn"] |
| 96 |
pwd_line = secret_file.readline() |
| 97 |
pwd = re.sub("\n", "", pwd_line) |
| 98 |
|
| 99 |
import univention.admin.uldap |
| 100 |
|
| 101 |
return univention.admin.uldap.access( |
| 102 |
host=config_registry()["ldap/master"], |
| 103 |
base=base_dn(), |
| 104 |
binddn=bind_dn, |
| 105 |
bindpw=pwd, |
| 106 |
start_tls=1, |
| 107 |
) |
| 108 |
|
| 109 |
return _singleton("uldap", construct) |
| 110 |
|
| 111 |
|
| 112 |
def config(): |
| 113 |
def construct(): |
| 114 |
import univention.admin.config |
| 115 |
|
| 116 |
return univention.admin.config.config() |
| 117 |
|
| 118 |
return _singleton("config", construct) |
| 119 |
|
| 120 |
|
| 121 |
def init_modules(): |
| 122 |
def construct(): |
| 123 |
import univention.admin.modules |
| 124 |
|
| 125 |
univention.admin.modules.update() |
| 126 |
return True |
| 127 |
|
| 128 |
return _singleton("modules_initialized", construct) |
| 129 |
|
| 130 |
|
| 131 |
def position_base_dn(): |
| 132 |
def construct(): |
| 133 |
import univention.admin.uldap |
| 134 |
|
| 135 |
return univention.admin.uldap.position(base_dn()) |
| 136 |
|
| 137 |
return _singleton("position_base_dn", construct) |
| 138 |
|
| 139 |
|
| 140 |
def ldap_dn_tree_parent(dn, count=1): |
| 141 |
dn_array = dn.split(",") |
| 142 |
dn_array[0:count] = [] |
| 143 |
return ",".join(dn_array) |
| 144 |
|
| 145 |
|
| 146 |
def ldap_search(filter, base=None, attr=None): |
| 147 |
"""Replaces uldaps search and uses a generator. |
| 148 |
!! Arguments are not the same.""" |
| 149 |
|
| 150 |
if base is None: |
| 151 |
base = base_dn() |
| 152 |
msgid = uldap().lo.lo.search(base, ldap_module().SCOPE_SUBTREE, filterstr=filter, attrlist=attr) |
| 153 |
# I used to have a try: finally: here but there seems to be a bug in python |
| 154 |
# which swallows the KeyboardInterrupt |
| 155 |
# The abandon now doesn't make too much sense |
| 156 |
while True: |
| 157 |
result_type, result_data = uldap().lo.lo.result(msgid, all=0) |
| 158 |
if not result_data: |
| 159 |
break |
| 160 |
if result_type is ldap_module().RES_SEARCH_RESULT: # pragma: no cover |
| 161 |
break |
| 162 |
else: |
| 163 |
if result_type is ldap_module().RES_SEARCH_ENTRY: |
| 164 |
yield from result_data |
| 165 |
uldap().lo.lo.abandon(msgid) |
| 166 |
|
| 167 |
|
| 168 |
def module_by_name(module_name_: str): |
| 169 |
"""Returns an initialized UMC module, identified by the given name. |
| 170 |
|
| 171 |
The module is a module specification according to the udm commandline. |
| 172 |
Example values are: |
| 173 |
* users/user |
| 174 |
* shares/share |
| 175 |
* groups/group |
| 176 |
|
| 177 |
If the module does not exist, a KeyError is raised. |
| 178 |
|
| 179 |
The modules are cached, so they won't be re-initialized |
| 180 |
in subsequent calls. |
| 181 |
""" |
| 182 |
|
| 183 |
def construct(): |
| 184 |
import univention.admin.modules |
| 185 |
|
| 186 |
init_modules() |
| 187 |
module = univention.admin.modules.get(module_name_) |
| 188 |
univention.admin.modules.init(uldap(), position_base_dn(), module) |
| 189 |
return module |
| 190 |
|
| 191 |
return _singleton(f"module/{module_name_}", construct) |
| 192 |
|
| 193 |
|
| 194 |
def get_umc_admin_objects(): |
| 195 |
"""Convenience accessor for getting univention.admin.objects. |
| 196 |
|
| 197 |
This implements delayed importing, so the univention.* modules |
| 198 |
are not loaded until this function is called. |
| 199 |
""" |
| 200 |
import univention.admin |
| 201 |
|
| 202 |
return univention.admin.objects |
| 203 |
|
| 204 |
|
| 205 |
def umc_module_for_add(module: str, container_dn, superordinate=None): |
| 206 |
"""Returns an UMC module object prepared for creating a new entry. |
| 207 |
|
| 208 |
The module is a module specification according to the udm commandline. |
| 209 |
Example values are: |
| 210 |
* users/user |
| 211 |
* shares/share |
| 212 |
* groups/group |
| 213 |
|
| 214 |
The container_dn MUST be the dn of the container (not of the object to |
| 215 |
be created itself!). |
| 216 |
""" |
| 217 |
mod = module_by_name(module) |
| 218 |
|
| 219 |
position = position_base_dn() |
| 220 |
position.setDn(container_dn) |
| 221 |
|
| 222 |
# config, ldap objects from common module |
| 223 |
obj = mod.object(config(), uldap(), position, superordinate=superordinate) |
| 224 |
obj.open() |
| 225 |
|
| 226 |
return obj |
| 227 |
|
| 228 |
|
| 229 |
def umc_module_for_edit(module: str, object_dn, superordinate=None): |
| 230 |
"""Returns an UMC module object prepared for editing an existing entry. |
| 231 |
|
| 232 |
The module is a module specification according to the udm commandline. |
| 233 |
Example values are: |
| 234 |
* users/user |
| 235 |
* shares/share |
| 236 |
* groups/group |
| 237 |
|
| 238 |
The object_dn MUST be the dn of the object itself, not the container! |
| 239 |
""" |
| 240 |
mod = module_by_name(module) |
| 241 |
|
| 242 |
objects = get_umc_admin_objects() |
| 243 |
|
| 244 |
position = position_base_dn() |
| 245 |
position.setDn(ldap_dn_tree_parent(object_dn)) |
| 246 |
|
| 247 |
obj = objects.get(mod, config(), uldap(), position=position, superordinate=superordinate, dn=object_dn) |
| 248 |
obj.open() |
| 249 |
|
| 250 |
return obj |
| 251 |
|
| 252 |
|
| 253 |
def create_containers_and_parents(container_dn): |
| 254 |
"""Create a container and if needed the parents containers""" |
| 255 |
import univention.admin.uexceptions as uexcp |
| 256 |
|
| 257 |
if not container_dn.startswith("cn="): |
| 258 |
raise AssertionError() |
| 259 |
try: |
| 260 |
parent = ldap_dn_tree_parent(container_dn) |
| 261 |
obj = umc_module_for_add("container/cn", parent) |
| 262 |
obj["name"] = container_dn.split(",")[0].split("=")[1] |
| 263 |
obj["description"] = "container created by import" |
| 264 |
except uexcp.ldapError: |
| 265 |
create_containers_and_parents(parent) |
| 266 |
obj = umc_module_for_add("container/cn", parent) |
| 267 |
obj["name"] = container_dn.split(",")[0].split("=")[1] |
| 268 |
obj["description"] = "container created by import" |