|
|
|
@ -227,27 +227,37 @@ def post_auth(data): |
|
|
|
# On récupère le numéro du port sur l'output de freeradius. |
|
|
|
# La ligne suivante fonctionne pour cisco, HP et Juniper |
|
|
|
port = port.split(".")[0].split('/')[-1][-2:] |
|
|
|
out = decide_vlan_and_register_switch(nas_machine, nas_type, port, mac) |
|
|
|
sw_name, room, reason, vlan_id = out |
|
|
|
|
|
|
|
log_message = '(fil) %s -> %s [%s%s]' % ( |
|
|
|
sw_name + u":" + port + u"/" + str(room), |
|
|
|
mac, |
|
|
|
vlan_id, |
|
|
|
(reason and u': ' + reason).encode('utf-8') |
|
|
|
) |
|
|
|
logger.info(log_message) |
|
|
|
out = decide_vlan_switch(nas_machine, nas_type, port, mac) |
|
|
|
sw_name, room, reason, vlan_id, decision = out |
|
|
|
|
|
|
|
if decision: |
|
|
|
log_message = '(fil) %s -> %s [%s%s]' % ( |
|
|
|
sw_name + u":" + port + u"/" + str(room), |
|
|
|
mac, |
|
|
|
vlan_id, |
|
|
|
(reason and u': ' + reason).encode('utf-8') |
|
|
|
) |
|
|
|
logger.info(log_message) |
|
|
|
|
|
|
|
# Filaire |
|
|
|
return ( |
|
|
|
radiusd.RLM_MODULE_UPDATED, |
|
|
|
( |
|
|
|
("Tunnel-Type", "VLAN"), |
|
|
|
("Tunnel-Medium-Type", "IEEE-802"), |
|
|
|
("Tunnel-Private-Group-Id", '%d' % int(vlan_id)), |
|
|
|
), |
|
|
|
() |
|
|
|
) |
|
|
|
# Filaire |
|
|
|
return ( |
|
|
|
radiusd.RLM_MODULE_UPDATED, |
|
|
|
( |
|
|
|
("Tunnel-Type", "VLAN"), |
|
|
|
("Tunnel-Medium-Type", "IEEE-802"), |
|
|
|
("Tunnel-Private-Group-Id", '%d' % int(vlan_id)), |
|
|
|
), |
|
|
|
() |
|
|
|
) |
|
|
|
else: |
|
|
|
log_message = '(fil) %s -> %s [Reject:%s]' % ( |
|
|
|
sw_name + u":" + port + u"/" + str(room), |
|
|
|
mac, |
|
|
|
(reason and u': ' + reason).encode('utf-8') |
|
|
|
) |
|
|
|
logger.info(log_message) |
|
|
|
|
|
|
|
return radiusd.RLM_MODULE_REJECT |
|
|
|
|
|
|
|
else: |
|
|
|
return radiusd.RLM_MODULE_OK |
|
|
|
@ -277,7 +287,6 @@ def find_nas_from_request(nas_id): |
|
|
|
.select_related('machine__switch__stack')) |
|
|
|
return nas.first() |
|
|
|
|
|
|
|
|
|
|
|
def check_user_machine_and_register(nas_type, username, mac_address): |
|
|
|
"""Verifie le username et la mac renseignee. L'enregistre si elle est |
|
|
|
inconnue. |
|
|
|
@ -317,7 +326,7 @@ def check_user_machine_and_register(nas_type, username, mac_address): |
|
|
|
return (False, u"Machine inconnue", '') |
|
|
|
|
|
|
|
|
|
|
|
def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, |
|
|
|
def decide_vlan_switch(nas_machine, nas_type, port_number, |
|
|
|
mac_address): |
|
|
|
"""Fonction de placement vlan pour un switch en radius filaire auth par |
|
|
|
mac. |
|
|
|
@ -337,16 +346,13 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, |
|
|
|
- user à jour : VLAN_OK |
|
|
|
- interface inconnue : |
|
|
|
- register mac désactivé : VLAN_NOK |
|
|
|
- register mac activé : |
|
|
|
- dans la chambre associé au port, pas d'user ou non à |
|
|
|
jour : VLAN_NOK |
|
|
|
- user à jour, autocapture de la mac et VLAN_OK |
|
|
|
- register mac activé -> redirection vers webauth |
|
|
|
""" |
|
|
|
# Get port from switch and port number |
|
|
|
extra_log = "" |
|
|
|
# Si le NAS est inconnu, on place sur le vlan defaut |
|
|
|
if not nas_machine: |
|
|
|
return ('?', u'Chambre inconnue', u'Nas inconnu', VLAN_OK) |
|
|
|
return ('?', u'Chambre inconnue', u'Nas inconnu', VLAN_OK, True) |
|
|
|
|
|
|
|
sw_name = str(getattr(nas_machine, 'short_name', str(nas_machine))) |
|
|
|
|
|
|
|
@ -361,7 +367,7 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, |
|
|
|
# Aucune information particulière ne permet de déterminer quelle |
|
|
|
# politique à appliquer sur ce port |
|
|
|
if not port: |
|
|
|
return (sw_name, "Chambre inconnue", u'Port inconnu', VLAN_OK) |
|
|
|
return (sw_name, "Chambre inconnue", u'Port inconnu', VLAN_OK, True) |
|
|
|
|
|
|
|
# On récupère le profil du port |
|
|
|
port_profile = port.get_port_profile |
|
|
|
@ -376,20 +382,21 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, |
|
|
|
|
|
|
|
# Si le port est désactivé, on rejette sur le vlan de déconnexion |
|
|
|
if not port.state: |
|
|
|
return (sw_name, port.room, u'Port desactivé', VLAN_NOK) |
|
|
|
return (sw_name, port.room, u'Port desactivé', VLAN_NOK, True) |
|
|
|
|
|
|
|
# Si radius est désactivé, on laisse passer |
|
|
|
if port_profile.radius_type == 'NO': |
|
|
|
return (sw_name, |
|
|
|
"", |
|
|
|
u"Pas d'authentification sur ce port" + extra_log, |
|
|
|
DECISION_VLAN) |
|
|
|
DECISION_VLAN, |
|
|
|
True) |
|
|
|
|
|
|
|
# Si le 802.1X est activé sur ce port, cela veut dire que la personne a été accept précédemment |
|
|
|
# Par conséquent, on laisse passer sur le bon vlan |
|
|
|
if nas_type.port_access_mode == '802.1X' and port_profile.radius_type == '802.1X': |
|
|
|
room = port.room or "Chambre/local inconnu" |
|
|
|
return (sw_name, room, u'Acceptation authentification 802.1X', DECISION_VLAN) |
|
|
|
return (sw_name, room, u'Acceptation authentification 802.1X', DECISION_VLAN, True) |
|
|
|
|
|
|
|
# Sinon, cela veut dire qu'on fait de l'auth radius par mac |
|
|
|
# Si le port est en mode strict, on vérifie que tous les users |
|
|
|
@ -399,16 +406,16 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, |
|
|
|
if port_profile.radius_mode == 'STRICT': |
|
|
|
room = port.room |
|
|
|
if not room: |
|
|
|
return (sw_name, "Inconnue", u'Chambre inconnue', VLAN_NOK) |
|
|
|
return (sw_name, "Inconnue", u'Chambre inconnue', VLAN_NOK, True) |
|
|
|
|
|
|
|
room_user = User.objects.filter( |
|
|
|
Q(club__room=port.room) | Q(adherent__room=port.room) |
|
|
|
) |
|
|
|
if not room_user: |
|
|
|
return (sw_name, room, u'Chambre non cotisante', VLAN_NOK) |
|
|
|
return (sw_name, room, u'Chambre non cotisante -> Web redirect', None, False) |
|
|
|
for user in room_user: |
|
|
|
if not user.has_access(): |
|
|
|
return (sw_name, room, u'Chambre resident desactive', VLAN_NOK) |
|
|
|
return (sw_name, room, u'Chambre resident desactive -> Web redirect', None, False) |
|
|
|
# else: user OK, on passe à la verif MAC |
|
|
|
|
|
|
|
# Si on fait de l'auth par mac, on cherche l'interface via sa mac dans la bdd |
|
|
|
@ -424,64 +431,11 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, |
|
|
|
# On essaye de register la mac, si l'autocapture a été activée |
|
|
|
# Sinon on rejette sur vlan_nok |
|
|
|
if not nas_type.autocapture_mac: |
|
|
|
return (sw_name, "", u'Machine inconnue', VLAN_NOK) |
|
|
|
# On ne peut autocapturer que si on connait la chambre et donc l'user correspondant |
|
|
|
elif not room: |
|
|
|
return (sw_name, |
|
|
|
"Inconnue", |
|
|
|
u'Chambre et machine inconnues', |
|
|
|
VLAN_NOK) |
|
|
|
return (sw_name, "", u'Machine inconnue', VLAN_NOK, True) |
|
|
|
# On rejette pour basculer sur du webauth |
|
|
|
else: |
|
|
|
# Si la chambre est vide (local club, prises en libre services) |
|
|
|
# Impossible d'autocapturer |
|
|
|
if not room_user: |
|
|
|
room_user = User.objects.filter( |
|
|
|
Q(club__room=port.room) | Q(adherent__room=port.room) |
|
|
|
) |
|
|
|
if not room_user: |
|
|
|
return (sw_name, |
|
|
|
room, |
|
|
|
u'Machine et propriétaire de la chambre ' |
|
|
|
'inconnus', |
|
|
|
VLAN_NOK) |
|
|
|
# Si il y a plus d'un user dans la chambre, impossible de savoir à qui |
|
|
|
# Ajouter la machine |
|
|
|
elif room_user.count() > 1: |
|
|
|
return (sw_name, |
|
|
|
room, |
|
|
|
u'Machine inconnue, il y a au moins 2 users ' |
|
|
|
'dans la chambre/local -> ajout de mac ' |
|
|
|
'automatique impossible', |
|
|
|
VLAN_NOK) |
|
|
|
# Si l'adhérent de la chambre n'est pas à jour de cotis, pas d'autocapture |
|
|
|
elif not room_user.first().has_access(): |
|
|
|
return (sw_name, |
|
|
|
room, |
|
|
|
u'Machine inconnue et adhérent non cotisant', |
|
|
|
VLAN_NOK) |
|
|
|
# Sinon on capture et on laisse passer sur le bon vlan |
|
|
|
else: |
|
|
|
interface, reason = (room_user |
|
|
|
.first() |
|
|
|
.autoregister_machine( |
|
|
|
mac_address, |
|
|
|
nas_type |
|
|
|
)) |
|
|
|
if interface: |
|
|
|
## Si on choisi de placer les machines sur le vlan correspondant à leur type : |
|
|
|
if RADIUS_POLICY == 'MACHINE': |
|
|
|
DECISION_VLAN = interface.type.ip_type.vlan.vlan_id |
|
|
|
return (sw_name, |
|
|
|
room, |
|
|
|
u'Access Ok, Capture de la mac: ' + extra_log, |
|
|
|
DECISION_VLAN) |
|
|
|
else: |
|
|
|
return (sw_name, |
|
|
|
room, |
|
|
|
u'Erreur dans le register mac %s' % ( |
|
|
|
reason + str(mac_address) |
|
|
|
), |
|
|
|
VLAN_NOK) |
|
|
|
return (sw_name, room, u'Machine Inconnue -> Web redirect', None, False) |
|
|
|
|
|
|
|
# L'interface a été trouvée, on vérifie qu'elle est active, sinon on reject |
|
|
|
# Si elle n'a pas d'ipv4, on lui en met une |
|
|
|
# Enfin on laisse passer sur le vlan pertinent |
|
|
|
@ -491,7 +445,8 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, |
|
|
|
return (sw_name, |
|
|
|
room, |
|
|
|
u'Machine non active / adherent non cotisant', |
|
|
|
VLAN_NOK) |
|
|
|
VLAN_NOK, |
|
|
|
True) |
|
|
|
## Si on choisi de placer les machines sur le vlan correspondant à leur type : |
|
|
|
if RADIUS_POLICY == 'MACHINE': |
|
|
|
DECISION_VLAN = interface.type.ip_type.vlan.vlan_id |
|
|
|
@ -500,9 +455,11 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, |
|
|
|
return (sw_name, |
|
|
|
room, |
|
|
|
u"Ok, Reassignation de l'ipv4" + extra_log, |
|
|
|
DECISION_VLAN) |
|
|
|
DECISION_VLAN, |
|
|
|
True) |
|
|
|
else: |
|
|
|
return (sw_name, |
|
|
|
room, |
|
|
|
u'Machine OK' + extra_log, |
|
|
|
DECISION_VLAN) |
|
|
|
DECISION_VLAN, |
|
|
|
True) |
|
|
|
|