auth.py: proprification légère
This commit is contained in:
parent
a0f0c80ead
commit
4205d73132
1 changed files with 23 additions and 19 deletions
|
@ -1,9 +1,14 @@
|
||||||
# ⁻*- coding: utf-8 -*-
|
# ⁻*- coding: utf-8 -*-
|
||||||
#
|
"""
|
||||||
# Ce fichier contient la définition de plusieurs fonctions d'interface à freeradius
|
Backend python pour freeradius.
|
||||||
# qui peuvent être appelées (suivant les configurations) à certains moment de
|
|
||||||
# l'éxécution.
|
Ce fichier contient la définition de plusieurs fonctions d'interface à
|
||||||
#
|
freeradius qui peuvent être appelées (suivant les configurations) à certains
|
||||||
|
moment de l'authentification, en WiFi, filaire, ou par les NAS eux-mêmes.
|
||||||
|
|
||||||
|
Inspirés d'autres exemples trouvés ici :
|
||||||
|
https://github.com/FreeRADIUS/freeradius-server/blob/master/src/modules/rlm_python/
|
||||||
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import netaddr
|
import netaddr
|
||||||
|
@ -91,8 +96,6 @@ def radius_event(fun):
|
||||||
et autres trucs du genre)
|
et autres trucs du genre)
|
||||||
* un tuple de couples (clé, valeur) pour les valeurs internes à mettre à
|
* un tuple de couples (clé, valeur) pour les valeurs internes à mettre à
|
||||||
jour (mot de passe par exemple)
|
jour (mot de passe par exemple)
|
||||||
Voir des exemples plus complets ici:
|
|
||||||
https://github.com/FreeRADIUS/freeradius-server/blob/master/src/modules/rlm_python/
|
|
||||||
|
|
||||||
On se contente avec ce décorateur (pour l'instant) de convertir la liste de
|
On se contente avec ce décorateur (pour l'instant) de convertir la liste de
|
||||||
tuples en entrée en un dictionnaire."""
|
tuples en entrée en un dictionnaire."""
|
||||||
|
@ -105,8 +108,8 @@ def radius_event(fun):
|
||||||
data[key] = value.replace('"', '')
|
data[key] = value.replace('"', '')
|
||||||
try:
|
try:
|
||||||
return fun(data)
|
return fun(data)
|
||||||
except Exception as e:
|
except Exception as err:
|
||||||
logger.error(repr(e) + ' on data ' + repr(auth_data))
|
logger.error('Failed %r on data %r' % (err, auth_data))
|
||||||
raise
|
raise
|
||||||
|
|
||||||
return new_f
|
return new_f
|
||||||
|
@ -189,7 +192,7 @@ def get_prise_chbre(data):
|
||||||
try:
|
try:
|
||||||
bat_name = nas[3].upper()
|
bat_name = nas[3].upper()
|
||||||
bat_num = int(nas.split('-', 1)[1])
|
bat_num = int(nas.split('-', 1)[1])
|
||||||
except IndexError, ValueError:
|
except (IndexError, ValueError):
|
||||||
pass
|
pass
|
||||||
port = data.get('NAS-Port', None)
|
port = data.get('NAS-Port', None)
|
||||||
if port:
|
if port:
|
||||||
|
@ -215,12 +218,12 @@ def realm_of_machine(machine):
|
||||||
|
|
||||||
def get_fresh_rid(machine):
|
def get_fresh_rid(machine):
|
||||||
"""Génère un rid tout frais pour la machine. Fonction kludge"""
|
"""Génère un rid tout frais pour la machine. Fonction kludge"""
|
||||||
lockId = machine.conn.lockholder.newid()
|
lock_id = machine.conn.lockholder.newid()
|
||||||
realm = realm_of_machine(machine)
|
realm = realm_of_machine(machine)
|
||||||
try:
|
try:
|
||||||
return machine.conn._find_id('rid', realm, lockId)
|
return machine.conn._find_id('rid', realm, lock_id)
|
||||||
finally:
|
finally:
|
||||||
machine.conn.lockholder.purge(lockId)
|
machine.conn.lockholder.purge(lock_id)
|
||||||
|
|
||||||
@use_ldap_admin
|
@use_ldap_admin
|
||||||
def register_machine(data, machine, conn):
|
def register_machine(data, machine, conn):
|
||||||
|
@ -236,7 +239,7 @@ def register_machine(data, machine, conn):
|
||||||
mac = mac.decode('ascii', 'ignore').replace('"','')
|
mac = mac.decode('ascii', 'ignore').replace('"','')
|
||||||
try:
|
try:
|
||||||
mac = lc_ldap.crans_utils.format_mac(mac).lower()
|
mac = lc_ldap.crans_utils.format_mac(mac).lower()
|
||||||
except:
|
except Exception:
|
||||||
logger.warn('Cannot format MAC for registration (aborting)')
|
logger.warn('Cannot format MAC for registration (aborting)')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -258,7 +261,7 @@ def register_machine(data, machine, conn):
|
||||||
@radius_event
|
@radius_event
|
||||||
@use_ldap_admin
|
@use_ldap_admin
|
||||||
@use_ldap
|
@use_ldap
|
||||||
def instantiate(_, *conns):
|
def instantiate(*_):
|
||||||
"""Utile pour initialiser les connexions ldap une première fois (otherwise,
|
"""Utile pour initialiser les connexions ldap une première fois (otherwise,
|
||||||
do nothing)"""
|
do nothing)"""
|
||||||
logger.info('Instantiation')
|
logger.info('Instantiation')
|
||||||
|
@ -337,11 +340,11 @@ def authorize_fil(data):
|
||||||
chap_ok = True
|
chap_ok = True
|
||||||
else:
|
else:
|
||||||
logger.info("(fil) Chap wrong")
|
logger.info("(fil) Chap wrong")
|
||||||
except:
|
except Exception as err:
|
||||||
logger.info("(fil) Chap challenge check failed")
|
logger.info("(fil) Chap challenge check failed with %r" % err)
|
||||||
|
|
||||||
if not chap_ok:
|
if not chap_ok:
|
||||||
if DEBUG:
|
if TEST_SERVER:
|
||||||
logger.debug('(fil) Continue auth (debug)')
|
logger.debug('(fil) Continue auth (debug)')
|
||||||
else:
|
else:
|
||||||
return radiusd.RLM_MODULE_REJECT
|
return radiusd.RLM_MODULE_REJECT
|
||||||
|
@ -574,7 +577,8 @@ def decide_vlan(data, is_wifi, conn):
|
||||||
return (port,) + decision
|
return (port,) + decision
|
||||||
|
|
||||||
@radius_event
|
@radius_event
|
||||||
def dummy_fun(p):
|
def dummy_fun(_):
|
||||||
|
"""Do nothing, successfully. (C'est pour avoir un truc à mettre)"""
|
||||||
return radiusd.RLM_MODULE_OK
|
return radiusd.RLM_MODULE_OK
|
||||||
|
|
||||||
def detach(_=None):
|
def detach(_=None):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue