generated from NethServer/ns8-kickstart
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from NethServer/sdl-7103-ldap-autodiscovery
Autodiscovery LDAP with service restart NethServer/dev#7103 NethServer/dev#7182
- Loading branch information
Showing
3 changed files
with
79 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# | ||
# Copyright (C) 2024 Nethesis S.r.l. | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
|
||
import json | ||
import os | ||
import agent | ||
import agent.ldapproxy | ||
import subprocess | ||
import sys | ||
|
||
# | ||
# Find settings for LDAP service | ||
# | ||
|
||
# due to early start before the environment is set see ../actions/configure-module/15start_service | ||
# we exit if the MAIL_MODULE is not set | ||
if not os.environ.get("MAIL_MODULE"): | ||
sys.exit(0) | ||
|
||
rdb = agent.redis_connect() | ||
|
||
def domain_setup(mail_domain, user_domain): | ||
user_domain_uri = "ldapneth://accountprovider" + ":" + user_domain["port"] | ||
user_domain_admin = user_domain["bind_dn"] | ||
|
||
user_domain_password = subprocess.check_output( | ||
[ | ||
'podman', 'run', '--replace', '--name', 'webtop-pass-encode', '--rm', '-i', os.environ["WEBTOP_WEBAPP_IMAGE"], | ||
"java", "-classpath", "/usr/share/webtop/", "WebtopPassEncode" | ||
], | ||
input=user_domain["bind_password"], | ||
text=True | ||
).strip() | ||
|
||
user_domain_parameters = { | ||
"loginDn": user_domain["base_dn"], | ||
"loginFilter": None, | ||
"userDn": user_domain["base_dn"], | ||
"userFilter": None, | ||
"userIdField": "uid", | ||
"userFirstnameField": "givenName", | ||
"userLastnameField": "sn", | ||
"userDisplayNameField": "displayName", | ||
} | ||
|
||
if user_domain["schema"] == "ad": | ||
user_domain_parameters["loginFilter"] = "&(objectCategory=person)(objectClass=user)" | ||
user_domain_parameters["userIdField"] = "sAMAccountName" | ||
user_domain_parameters["userFilter"] = "(&(objectClass=user)(objectCategory=person)(!(isCriticalSystemObject=TRUE)))" | ||
user_domain_parameters["userDisplayNameField"] = "displayName" | ||
|
||
with subprocess.Popen(['podman', 'exec', '-i', 'postgres', 'psql', '-U', 'postgres', 'webtop5'], stdin=subprocess.PIPE, text=True) as psql: | ||
print("DELETE FROM core.domains WHERE domain_id = 'NethServer';\n", file=psql.stdin) | ||
print("INSERT INTO core.domains (domain_id, internet_name, enabled, description, user_auto_creation, dir_uri, dir_admin, dir_password, dir_connection_security, dir_case_sensitive, dir_password_policy, dir_parameters) VALUES ('NethServer', '" + mail_domain + "', 't', 'NethServer', 't', '" + user_domain_uri + "', '" + user_domain_admin + "', '" + user_domain_password + "', null, 'f', 'f', '" + json.dumps(user_domain_parameters) + "');\n", file=psql.stdin) | ||
|
||
agent.set_env("USER_DOMAIN_PORT", user_domain["port"]) | ||
|
||
|
||
user_domain_name = rdb.hget(f'module/{os.environ["MAIL_MODULE"]}/srv/tcp/imap', "user_domain") or "" | ||
user_domain = agent.ldapproxy.Ldapproxy().get_domain(user_domain_name) or { | ||
'host': '127.0.0.1', | ||
'port': 20000, | ||
'schema': 'rfc2307', | ||
'location': 'internal', | ||
'base_dn': 'dc=webtop,dc=invalid', | ||
'bind_dn': 'cn=example,dc=webtop,dc=invalid', | ||
'bind_password': 'invalid', | ||
} | ||
|
||
domain_setup(user_domain_name, user_domain) | ||
|
||
if user_domain_name and user_domain['bind_password'] != 'invalid': | ||
agent.bind_user_domains([user_domain_name]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters