66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
from ipaddress import ip_network as _ip_network, ip_address as _ip_address
|
|
|
|
|
|
def valid_base_dn(base_dn: str) -> None:
|
|
# copied from openldap
|
|
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=)')
|
|
|
|
|
|
def calc_ldapclient_base_dn(ldap_base_dn: str,
|
|
family_name: str=None,
|
|
base: bool=False,
|
|
group: bool=False,
|
|
) -> str:
|
|
# copied from openldap
|
|
if ldap_base_dn is None:
|
|
return
|
|
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
|
|
if not family_name:
|
|
return f'ou=users,{base_name}'
|
|
base_name = f'ou=families,{base_name}'
|
|
if family_name != '-':
|
|
base_name = f'ou={family_name},{base_name}'
|
|
return base_name
|
|
|
|
|
|
class _Undefined:
|
|
pass
|
|
|
|
|
|
_undefined = _Undefined()
|
|
|
|
|
|
def get_default_base_dn(prefix: str) -> str:
|
|
# copied from openldap
|
|
if not prefix or '.' not in prefix:
|
|
return None
|
|
values = prefix.split('.')
|
|
# cannot calculated base dn should be subdomain.domain.tld
|
|
# remove 'server' in dn
|
|
if len(values) < 3:
|
|
return None
|
|
domain = ['ou=' + domain for domain in values[0:-2]]
|
|
domain.append(f'o={values[-2]},o={values[-1]}')
|
|
return ','.join(domain)
|
|
|
|
|
|
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
|