dataset/seed/ldap-client/funcs/openldap_client.py

67 lines
1.9 KiB
Python
Raw Normal View History

2023-06-23 08:12:05 +02:00
from ipaddress import ip_network as _ip_network, ip_address as _ip_address
2022-03-08 19:42:28 +01:00
def valid_base_dn(base_dn: str) -> None:
2023-06-23 08:12:05 +02:00
# copied from openldap
2022-03-08 19:42:28 +01:00
for att in ['o', 'dc', 'ou']:
if base_dn.startswith(att + '='):
break
else:
raise ValueError('La racine doit débuter par une organisation (o=), une composante du domaine (dc=) ou une unité organisationnelle (ou=)')
2022-05-04 10:29:03 +02:00
def calc_ldapclient_base_dn(ldap_base_dn: str,
2022-06-24 19:00:16 +02:00
family_name: str=None,
base: bool=False,
group: bool=False,
2022-05-04 10:29:03 +02:00
) -> str:
2023-06-23 08:12:05 +02:00
# copied from openldap
2022-08-18 10:19:43 +02:00
if ldap_base_dn is None:
return
2022-06-24 19:00:16 +02:00
if family_name == 'all':
family_name = None
base = True
if group:
return f'ou=groups,{ldap_base_dn}'
if not ldap_base_dn.startswith('ou=accounts,'):
base_name = f'ou=accounts,{ldap_base_dn}'
else:
base_name = ldap_base_dn
if base:
return base_name
2022-05-04 10:29:03 +02:00
if not family_name:
2022-06-24 19:00:16 +02:00
return f'ou=users,{base_name}'
base_name = f'ou=families,{base_name}'
2022-05-07 08:11:18 +02:00
if family_name != '-':
2022-06-24 19:00:16 +02:00
base_name = f'ou={family_name},{base_name}'
return base_name
2022-08-18 10:19:43 +02:00
class _Undefined:
pass
_undefined = _Undefined()
2023-06-23 08:12:05 +02:00
def get_default_base_dn(prefix: str) -> str:
# copied from openldap
if not prefix or '.' not in prefix:
2022-08-18 10:19:43 +02:00
return None
2023-06-23 08:12:05 +02:00
values = prefix.split('.')
# cannot calculated base dn should be subdomain.domain.tld
2022-08-18 10:19:43 +02:00
# remove 'server' in dn
if len(values) < 3:
return None
2023-06-23 08:12:05 +02:00
domain = ['ou=' + domain for domain in values[0:-2]]
2022-08-18 10:19:43 +02:00
domain.append(f'o={values[-2]},o={values[-1]}')
return ','.join(domain)
2023-06-23 08:12:05 +02:00
def get_client_address(ip, infos, network_eth):
ip_mail = _ip_address(ip)
for idx, net in enumerate(network_eth):
if ip_mail in _ip_network(net):
val = infos[idx]
return val