From 067459eb445337ac420b6fe275b5b5628a24f172 Mon Sep 17 00:00:00 2001 From: Valentin Samir Date: Tue, 18 Mar 2014 22:45:42 +0100 Subject: [PATCH] =?UTF-8?q?[crans=5Futils,=20lc=5Fldap]=20M=C3=A9thode=20p?= =?UTF-8?q?our=20r=C3=A9cup=C3=A9rer=20les=20machines=20ldap=20corresponda?= =?UTF-8?q?nt=20=C3=A0=20la=20machine=20physique=20locale?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crans_utils.py | 12 ++++++++++++ lc_ldap.py | 10 ++++++++++ shortcuts.py | 7 +++---- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/crans_utils.py b/crans_utils.py index 7f1629c..632acfe 100644 --- a/crans_utils.py +++ b/crans_utils.py @@ -45,6 +45,7 @@ sys.path.append('/usr/scripts/gestion') import config from unicodedata import normalize import subprocess +from netifaces import interfaces, ifaddresses, AF_INET DEVNULL = open(os.devnull, 'w') @@ -292,3 +293,14 @@ def fetch_cert_info(x509): for ext in x509.get_extensions(): do_ext(data, ext) return data + + +def ip4_addresses(): + """Renvois la liste des ipv4 de la machine physique courante""" + ip_list = [] + for interface in interfaces(): + if interface!='lo' and AF_INET in ifaddresses(interface).keys(): + for link in ifaddresses(interface)[AF_INET]: + ip_list.append(link['addr']) + return ip_list + diff --git a/lc_ldap.py b/lc_ldap.py index a63899e..017127c 100644 --- a/lc_ldap.py +++ b/lc_ldap.py @@ -553,3 +553,13 @@ class lc_ldap(ldap.ldapobject.LDAPObject, object): return [attributs.soi] else: return [] + + + + def get_local_machines(self, mode='ro'): + filter="" + for ip in set(crans_utils.ip4_addresses()): + filter+=u'(ipHostNumber=%s)' % ip + filter = u"(|%s)" % filter + return self.search(filter, mode=mode) + diff --git a/shortcuts.py b/shortcuts.py index 0246ed0..f5a8347 100644 --- a/shortcuts.py +++ b/shortcuts.py @@ -34,7 +34,6 @@ current_user = os.getenv("SUDO_USER") or os.getenv("USER") or os.getenv("LOGNAME if isinstance(current_user, str): current_user = current_user.decode("utf-8") - def lc_ldap(*args, **kwargs): """Renvoie une connexion à la base LDAP.""" return module_qui_a_le_meme_nom_que_sa_classe_principale.lc_ldap(*args, **kwargs) @@ -60,7 +59,7 @@ def lc_ldap_test(*args, **kwargs): def lc_ldap_admin(*args, **kwargs): """Renvoie une connexion LDAP à la vraie base, en admin. Possible seulement si on peut lire secrets.py - + """ kwargs["uri"] = 'ldap://ldap.adm.crans.org/' kwargs["dn"] = secrets.get('ldap_auth_dn') @@ -71,7 +70,7 @@ def lc_ldap_admin(*args, **kwargs): def lc_ldap_readonly(*args, **kwargs): """Connexion LDAP à la vraie base, en readonly. Possible seulement si on peut lire secrets.py - + """ kwargs["uri"] = 'ldap://ldap.adm.crans.org/' kwargs["dn"] = secrets.get('ldap_readonly_auth_dn') @@ -90,7 +89,7 @@ def lc_ldap_local(*args, **kwargs): si vous souhaitez faire beaucoup de recherches indépendantes (c'est le temps d'accès à la socket qui est problématique). - + """ if os.path.exists('/var/run/slapd/ldapi'): ro_uri = 'ldapi://%2fvar%2frun%2fslapd%2fldapi/'