2018-04-13 20:11:55 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
2017-01-15 23:01:18 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
|
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
2017-12-03 18:44:24 +00:00
|
|
|
# Copyirght © 2017 Daniel Stan
|
2017-01-15 23:01:18 +00:00
|
|
|
# Copyright © 2017 Gabriel Détraz
|
|
|
|
# Copyright © 2017 Goulven Kermarec
|
|
|
|
# Copyright © 2017 Augustin Lemesle
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2016-12-08 14:13:41 +00:00
|
|
|
"""
|
|
|
|
Backend python pour freeradius.
|
|
|
|
|
|
|
|
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/
|
2017-10-11 20:22:31 +00:00
|
|
|
|
|
|
|
Inspiré du travail de Daniel Stan au Crans
|
2016-12-08 14:13:41 +00:00
|
|
|
"""
|
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2018-04-14 15:30:14 +00:00
|
|
|
import logging
|
|
|
|
import radiusd # Module magique freeradius (radiusd.py is dummy)
|
2018-04-13 20:11:55 +00:00
|
|
|
|
2018-04-14 15:30:14 +00:00
|
|
|
from django.core.wsgi import get_wsgi_application
|
2018-04-13 20:11:55 +00:00
|
|
|
from django.db.models import Q
|
2018-04-14 15:30:14 +00:00
|
|
|
|
2017-09-10 20:50:20 +00:00
|
|
|
proj_path = "/var/www/re2o/"
|
|
|
|
# This is so Django knows where to find stuff.
|
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "re2o.settings")
|
|
|
|
sys.path.append(proj_path)
|
|
|
|
|
|
|
|
# This is so my local_settings.py gets loaded.
|
|
|
|
os.chdir(proj_path)
|
|
|
|
|
|
|
|
# This is so models get loaded.
|
|
|
|
application = get_wsgi_application()
|
|
|
|
|
2018-04-26 07:19:10 +00:00
|
|
|
from machines.models import Interface, IpList, Nas, Domain
|
|
|
|
from topologie.models import Port, Switch
|
|
|
|
from users.models import User
|
2018-12-04 19:00:18 +00:00
|
|
|
from preferences.models import RadiusOption
|
2018-04-26 07:19:10 +00:00
|
|
|
|
|
|
|
|
2016-12-08 14:13:41 +00:00
|
|
|
#: Serveur radius de test (pas la prod)
|
|
|
|
TEST_SERVER = bool(os.getenv('DBG_FREERADIUS', False))
|
|
|
|
|
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
# Logging
|
2016-12-08 14:13:41 +00:00
|
|
|
class RadiusdHandler(logging.Handler):
|
|
|
|
"""Handler de logs pour freeradius"""
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
"""Process un message de log, en convertissant les niveaux"""
|
|
|
|
if record.levelno >= logging.WARN:
|
|
|
|
rad_sig = radiusd.L_ERR
|
|
|
|
elif record.levelno >= logging.INFO:
|
|
|
|
rad_sig = radiusd.L_INFO
|
|
|
|
else:
|
|
|
|
rad_sig = radiusd.L_DBG
|
|
|
|
radiusd.radlog(rad_sig, record.msg)
|
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
|
2016-12-08 14:13:41 +00:00
|
|
|
# Initialisation d'un logger (pour logguer unifié)
|
|
|
|
logger = logging.getLogger('auth.py')
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
formatter = logging.Formatter('%(name)s: [%(levelname)s] %(message)s')
|
|
|
|
handler = RadiusdHandler()
|
|
|
|
handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(handler)
|
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
|
2016-12-08 14:13:41 +00:00
|
|
|
def radius_event(fun):
|
|
|
|
"""Décorateur pour les fonctions d'interfaces avec radius.
|
2018-04-13 20:11:55 +00:00
|
|
|
Une telle fonction prend un uniquement argument, qui est une liste de
|
|
|
|
tuples (clé, valeur) et renvoie un triplet dont les composantes sont :
|
2016-12-08 14:13:41 +00:00
|
|
|
* le code de retour (voir radiusd.RLM_MODULE_* )
|
|
|
|
* un tuple de couples (clé, valeur) pour les valeurs de réponse (accès ok
|
|
|
|
et autres trucs du genre)
|
|
|
|
* un tuple de couples (clé, valeur) pour les valeurs internes à mettre à
|
|
|
|
jour (mot de passe par exemple)
|
|
|
|
|
|
|
|
On se contente avec ce décorateur (pour l'instant) de convertir la liste de
|
|
|
|
tuples en entrée en un dictionnaire."""
|
|
|
|
|
|
|
|
def new_f(auth_data):
|
2018-04-14 15:30:14 +00:00
|
|
|
""" The function transforming the tuples as dict """
|
|
|
|
if isinstance(auth_data, dict):
|
2016-12-08 14:13:41 +00:00
|
|
|
data = auth_data
|
|
|
|
else:
|
|
|
|
data = dict()
|
|
|
|
for (key, value) in auth_data or []:
|
|
|
|
# Beware: les valeurs scalaires sont entre guillemets
|
|
|
|
# Ex: Calling-Station-Id: "une_adresse_mac"
|
|
|
|
data[key] = value.replace('"', '')
|
|
|
|
try:
|
2018-04-13 20:11:55 +00:00
|
|
|
# TODO s'assurer ici que les tuples renvoyés sont bien des
|
|
|
|
# (str,str) : rlm_python ne digère PAS les unicodes
|
2016-12-08 14:13:41 +00:00
|
|
|
return fun(data)
|
|
|
|
except Exception as err:
|
|
|
|
logger.error('Failed %r on data %r' % (err, auth_data))
|
2018-11-05 13:26:46 +00:00
|
|
|
return radiusd.RLM_MODULE_FAIL
|
2016-12-08 14:13:41 +00:00
|
|
|
|
|
|
|
return new_f
|
|
|
|
|
2017-12-03 18:44:24 +00:00
|
|
|
|
2016-12-08 14:13:41 +00:00
|
|
|
@radius_event
|
|
|
|
def instantiate(*_):
|
|
|
|
"""Utile pour initialiser les connexions ldap une première fois (otherwise,
|
|
|
|
do nothing)"""
|
|
|
|
logger.info('Instantiation')
|
|
|
|
if TEST_SERVER:
|
2017-09-14 15:24:12 +00:00
|
|
|
logger.info(u'DBG_FREERADIUS is enabled')
|
2016-12-08 14:13:41 +00:00
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
|
2016-12-08 14:13:41 +00:00
|
|
|
@radius_event
|
|
|
|
def authorize(data):
|
2017-11-19 20:09:57 +00:00
|
|
|
"""On test si on connait le calling nas:
|
2018-04-13 20:11:55 +00:00
|
|
|
- si le nas est inconnue, on suppose que c'est une requète 802.1X, on la
|
|
|
|
traite
|
2017-11-19 20:09:57 +00:00
|
|
|
- si le nas est connu, on applique 802.1X si le mode est activé
|
2018-04-13 20:11:55 +00:00
|
|
|
- si le nas est connu et si il s'agit d'un nas auth par mac, on repond
|
|
|
|
accept en authorize
|
2017-11-19 20:09:57 +00:00
|
|
|
"""
|
2017-09-12 17:40:04 +00:00
|
|
|
# Pour les requetes proxifiees, on split
|
2017-09-14 14:42:50 +00:00
|
|
|
nas = data.get('NAS-IP-Address', data.get('NAS-Identifier', None))
|
|
|
|
nas_instance = find_nas_from_request(nas)
|
|
|
|
# Toutes les reuquètes non proxifiées
|
2017-11-19 20:17:17 +00:00
|
|
|
nas_type = None
|
2017-11-19 20:09:57 +00:00
|
|
|
if nas_instance:
|
2017-09-14 14:42:50 +00:00
|
|
|
nas_type = Nas.objects.filter(nas_type=nas_instance.type).first()
|
|
|
|
if not nas_type or nas_type.port_access_mode == '802.1X':
|
2017-11-11 18:06:50 +00:00
|
|
|
user = data.get('User-Name', '').decode('utf-8', errors='replace')
|
2017-09-12 17:40:04 +00:00
|
|
|
user = user.split('@', 1)[0]
|
2017-09-14 14:42:50 +00:00
|
|
|
mac = data.get('Calling-Station-Id', '')
|
2018-04-13 20:11:55 +00:00
|
|
|
result, log, password = check_user_machine_and_register(
|
|
|
|
nas_type,
|
|
|
|
user,
|
|
|
|
mac
|
|
|
|
)
|
2017-09-13 13:04:09 +00:00
|
|
|
logger.info(log.encode('utf-8'))
|
2017-11-11 18:06:50 +00:00
|
|
|
logger.info(user.encode('utf-8'))
|
|
|
|
|
2017-09-12 17:40:04 +00:00
|
|
|
if not result:
|
|
|
|
return radiusd.RLM_MODULE_REJECT
|
|
|
|
else:
|
2018-04-13 20:11:55 +00:00
|
|
|
return (
|
|
|
|
radiusd.RLM_MODULE_UPDATED,
|
|
|
|
(),
|
|
|
|
(
|
|
|
|
(str("NT-Password"), str(password)),
|
|
|
|
),
|
2017-09-12 17:40:04 +00:00
|
|
|
)
|
2016-12-08 14:13:41 +00:00
|
|
|
|
2017-09-12 17:40:04 +00:00
|
|
|
else:
|
2018-04-13 20:11:55 +00:00
|
|
|
return (
|
|
|
|
radiusd.RLM_MODULE_UPDATED,
|
|
|
|
(),
|
|
|
|
(
|
|
|
|
("Auth-Type", "Accept"),
|
|
|
|
),
|
2017-09-12 17:40:04 +00:00
|
|
|
)
|
2016-12-08 14:13:41 +00:00
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
|
2016-12-08 14:13:41 +00:00
|
|
|
@radius_event
|
|
|
|
def post_auth(data):
|
2018-04-14 15:30:14 +00:00
|
|
|
""" Function called after the user is authenticated
|
|
|
|
"""
|
|
|
|
|
2017-09-12 17:40:04 +00:00
|
|
|
nas = data.get('NAS-IP-Address', data.get('NAS-Identifier', None))
|
2017-09-13 01:53:06 +00:00
|
|
|
nas_instance = find_nas_from_request(nas)
|
2017-09-14 15:24:12 +00:00
|
|
|
# Toutes les reuquètes non proxifiées
|
2017-11-19 20:09:57 +00:00
|
|
|
if not nas_instance:
|
|
|
|
logger.info(u"Requète proxifiée, nas inconnu".encode('utf-8'))
|
2017-09-14 15:24:12 +00:00
|
|
|
return radiusd.RLM_MODULE_OK
|
|
|
|
nas_type = Nas.objects.filter(nas_type=nas_instance.type).first()
|
|
|
|
if not nas_type:
|
2018-04-13 20:11:55 +00:00
|
|
|
logger.info(
|
|
|
|
u"Type de nas non enregistré dans la bdd!".encode('utf-8')
|
|
|
|
)
|
2017-09-14 15:24:12 +00:00
|
|
|
return radiusd.RLM_MODULE_OK
|
2017-12-03 18:44:24 +00:00
|
|
|
|
2016-12-08 14:13:41 +00:00
|
|
|
mac = data.get('Calling-Station-Id', None)
|
2017-12-03 18:44:24 +00:00
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
# Switch et bornes héritent de machine et peuvent avoir plusieurs
|
|
|
|
# interfaces filles
|
2018-03-26 23:26:17 +00:00
|
|
|
nas_machine = nas_instance.machine
|
2017-09-12 17:40:04 +00:00
|
|
|
# Si il s'agit d'un switch
|
2018-03-26 23:26:17 +00:00
|
|
|
if hasattr(nas_machine, 'switch'):
|
2017-09-14 15:24:12 +00:00
|
|
|
port = data.get('NAS-Port-Id', data.get('NAS-Port', None))
|
2018-04-13 20:11:55 +00:00
|
|
|
# Pour les infrastructures possédant des switchs Juniper :
|
|
|
|
# On vérifie si le switch fait partie d'un stack Juniper
|
2018-03-26 23:26:17 +00:00
|
|
|
instance_stack = nas_machine.switch.stack
|
2017-12-03 18:44:24 +00:00
|
|
|
if instance_stack:
|
|
|
|
# Si c'est le cas, on resélectionne le bon switch dans la stack
|
|
|
|
id_stack_member = port.split("-")[1].split('/')[0]
|
2018-04-13 20:11:55 +00:00
|
|
|
nas_machine = (Switch.objects
|
|
|
|
.filter(stack=instance_stack)
|
|
|
|
.filter(stack_member_id=id_stack_member)
|
|
|
|
.prefetch_related(
|
|
|
|
'interface_set__domain__extension'
|
2018-04-14 15:30:14 +00:00
|
|
|
)
|
2018-04-13 20:11:55 +00:00
|
|
|
.first())
|
|
|
|
# On récupère le numéro du port sur l'output de freeradius.
|
|
|
|
# La ligne suivante fonctionne pour cisco, HP et Juniper
|
2017-09-12 17:40:04 +00:00
|
|
|
port = port.split(".")[0].split('/')[-1][-2:]
|
2018-08-29 23:53:05 +00:00
|
|
|
out = decide_vlan_switch(nas_machine, nas_type, port, mac)
|
2018-08-30 00:11:14 +00:00
|
|
|
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)
|
2017-09-12 17:40:04 +00:00
|
|
|
|
2018-08-30 00:11:14 +00:00
|
|
|
# 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)
|
2017-09-12 17:40:04 +00:00
|
|
|
|
2018-08-30 00:11:14 +00:00
|
|
|
return radiusd.RLM_MODULE_REJECT
|
2016-12-08 14:13:41 +00:00
|
|
|
|
2017-09-12 17:40:04 +00:00
|
|
|
else:
|
|
|
|
return radiusd.RLM_MODULE_OK
|
2016-12-08 14:13:41 +00:00
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
|
2018-04-14 15:30:14 +00:00
|
|
|
# TODO : remove this function
|
2016-12-08 14:13:41 +00:00
|
|
|
@radius_event
|
|
|
|
def dummy_fun(_):
|
|
|
|
"""Do nothing, successfully. (C'est pour avoir un truc à mettre)"""
|
|
|
|
return radiusd.RLM_MODULE_OK
|
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
|
2016-12-08 14:13:41 +00:00
|
|
|
def detach(_=None):
|
|
|
|
"""Appelé lors du déchargement du module (enfin, normalement)"""
|
2018-04-14 15:30:14 +00:00
|
|
|
print("*** goodbye from auth.py ***")
|
2016-12-08 14:13:41 +00:00
|
|
|
return radiusd.RLM_MODULE_OK
|
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
|
2017-09-12 17:40:04 +00:00
|
|
|
def find_nas_from_request(nas_id):
|
2018-04-14 15:30:14 +00:00
|
|
|
""" Get the nas object from its ID """
|
2018-04-13 20:11:55 +00:00
|
|
|
nas = (Interface.objects
|
|
|
|
.filter(
|
|
|
|
Q(domain=Domain.objects.filter(name=nas_id)) |
|
|
|
|
Q(ipv4=IpList.objects.filter(ipv4=nas_id))
|
|
|
|
)
|
|
|
|
.select_related('type')
|
|
|
|
.select_related('machine__switch__stack'))
|
2017-09-13 01:53:06 +00:00
|
|
|
return nas.first()
|
2017-09-12 17:40:04 +00:00
|
|
|
|
2018-12-04 19:00:18 +00:00
|
|
|
|
2017-09-14 14:42:50 +00:00
|
|
|
def check_user_machine_and_register(nas_type, username, mac_address):
|
2018-04-13 20:11:55 +00:00
|
|
|
"""Verifie le username et la mac renseignee. L'enregistre si elle est
|
|
|
|
inconnue.
|
2017-09-12 17:40:04 +00:00
|
|
|
Renvoie le mot de passe ntlm de l'user si tout est ok
|
|
|
|
Utilise pour les authentifications en 802.1X"""
|
|
|
|
interface = Interface.objects.filter(mac_address=mac_address).first()
|
|
|
|
user = User.objects.filter(pseudo=username).first()
|
|
|
|
if not user:
|
2017-09-13 13:04:09 +00:00
|
|
|
return (False, u"User inconnu", '')
|
|
|
|
if not user.has_access():
|
|
|
|
return (False, u"Adhérent non cotisant", '')
|
2017-09-12 17:40:04 +00:00
|
|
|
if interface:
|
|
|
|
if interface.machine.user != user:
|
2018-04-13 20:11:55 +00:00
|
|
|
return (False,
|
|
|
|
u"Machine enregistrée sur le compte d'un autre "
|
|
|
|
"user...",
|
|
|
|
'')
|
2017-09-12 17:40:04 +00:00
|
|
|
elif not interface.is_active:
|
|
|
|
return (False, u"Machine desactivée", '')
|
2017-10-30 19:53:49 +00:00
|
|
|
elif not interface.ipv4:
|
|
|
|
interface.assign_ipv4()
|
|
|
|
return (True, u"Ok, Reassignation de l'ipv4", user.pwd_ntlm)
|
2017-09-12 17:40:04 +00:00
|
|
|
else:
|
2017-09-13 13:04:09 +00:00
|
|
|
return (True, u"Access ok", user.pwd_ntlm)
|
2017-09-14 14:42:50 +00:00
|
|
|
elif nas_type:
|
2017-09-14 15:24:12 +00:00
|
|
|
if nas_type.autocapture_mac:
|
2017-09-14 14:42:50 +00:00
|
|
|
result, reason = user.autoregister_machine(mac_address, nas_type)
|
|
|
|
if result:
|
2018-04-13 20:11:55 +00:00
|
|
|
return (True,
|
|
|
|
u'Access Ok, Capture de la mac...',
|
|
|
|
user.pwd_ntlm)
|
2017-09-14 14:42:50 +00:00
|
|
|
else:
|
2017-12-03 18:44:24 +00:00
|
|
|
return (False, u'Erreur dans le register mac %s' % reason, '')
|
2017-10-04 11:50:12 +00:00
|
|
|
else:
|
|
|
|
return (False, u'Machine inconnue', '')
|
2017-09-12 17:40:04 +00:00
|
|
|
else:
|
2017-09-13 13:04:09 +00:00
|
|
|
return (False, u"Machine inconnue", '')
|
2017-09-12 17:40:04 +00:00
|
|
|
|
|
|
|
|
2018-08-29 23:53:05 +00:00
|
|
|
def decide_vlan_switch(nas_machine, nas_type, port_number,
|
2018-12-04 19:00:18 +00:00
|
|
|
mac_address):
|
2018-04-13 20:11:55 +00:00
|
|
|
"""Fonction de placement vlan pour un switch en radius filaire auth par
|
|
|
|
mac.
|
2017-11-19 20:09:57 +00:00
|
|
|
Plusieurs modes :
|
2018-12-04 19:00:18 +00:00
|
|
|
- tous les modes:
|
|
|
|
- nas inconnu: VLAN_OK
|
|
|
|
- port inconnu: Politique définie dans RadiusOption
|
|
|
|
- pas de radius sur le port: VLAN_OK
|
|
|
|
- force: placement sur le vlan indiqué dans la bdd
|
|
|
|
- mode strict:
|
|
|
|
- pas de chambre associée: Politique définie
|
|
|
|
dans RadiusOption
|
|
|
|
- pas d'utilisateur dans la chambre : Rejet
|
|
|
|
(redirection web si disponible)
|
|
|
|
- utilisateur de la chambre banni ou désactivé : Rejet
|
|
|
|
(redirection web si disponible)
|
|
|
|
- utilisateur de la chambre non cotisant et non whiteslist:
|
|
|
|
Politique définie dans RadiusOption
|
|
|
|
|
|
|
|
- sinon passe à common (ci-dessous)
|
|
|
|
- mode common :
|
|
|
|
- interface connue (macaddress):
|
|
|
|
- utilisateur proprio non cotisant / machine désactivée:
|
|
|
|
Politique définie dans RadiusOption
|
|
|
|
- utilisateur proprio banni :
|
|
|
|
Politique définie dans RadiusOption
|
|
|
|
- user à jour : VLAN_OK (réassignation de l'ipv4 au besoin)
|
|
|
|
- interface inconnue :
|
|
|
|
- register mac désactivé : Politique définie
|
|
|
|
dans RadiusOption
|
|
|
|
- register mac activé: redirection vers webauth
|
|
|
|
Returns:
|
|
|
|
tuple avec :
|
|
|
|
- Nom du switch (str)
|
|
|
|
- chambre (str)
|
|
|
|
- raison de la décision (str)
|
|
|
|
- vlan_id (int)
|
|
|
|
- decision (bool)
|
2017-11-19 20:09:57 +00:00
|
|
|
"""
|
2017-09-10 20:50:20 +00:00
|
|
|
# Get port from switch and port number
|
2017-10-04 02:31:28 +00:00
|
|
|
extra_log = ""
|
2017-11-19 20:09:57 +00:00
|
|
|
# Si le NAS est inconnu, on place sur le vlan defaut
|
2018-03-26 23:26:17 +00:00
|
|
|
if not nas_machine:
|
2018-12-08 20:47:21 +00:00
|
|
|
return ('?', u'Chambre inconnue', u'Nas inconnu', RadiusOption.get_cached_value('vlan_decision_ok').vlan_id, True)
|
2017-09-10 20:50:20 +00:00
|
|
|
|
2018-08-07 15:22:21 +00:00
|
|
|
sw_name = str(getattr(nas_machine, 'short_name', str(nas_machine)))
|
2017-09-10 20:50:20 +00:00
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
port = (Port.objects
|
|
|
|
.filter(
|
|
|
|
switch=Switch.objects.filter(machine_ptr=nas_machine),
|
|
|
|
port=port_number
|
|
|
|
)
|
|
|
|
.first())
|
2018-07-11 17:37:22 +00:00
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
# Si le port est inconnu, on place sur le vlan defaut
|
2018-06-30 22:17:24 +00:00
|
|
|
# Aucune information particulière ne permet de déterminer quelle
|
|
|
|
# politique à appliquer sur ce port
|
2017-09-10 20:50:20 +00:00
|
|
|
if not port:
|
2018-12-04 19:00:18 +00:00
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
"Chambre inconnue",
|
|
|
|
u'Port inconnu',
|
2018-12-08 20:47:21 +00:00
|
|
|
RadiusOption('unknown_port_vlan').vlan_id,
|
|
|
|
RadiusOption('unknown_port')!= RadiusOption.REJECT
|
2018-12-04 19:00:18 +00:00
|
|
|
)
|
2017-09-10 20:50:20 +00:00
|
|
|
|
2018-06-30 16:36:14 +00:00
|
|
|
# On récupère le profil du port
|
2018-07-11 17:37:22 +00:00
|
|
|
port_profile = port.get_port_profile
|
2018-06-30 16:36:14 +00:00
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
# Si un vlan a été précisé dans la config du port,
|
2018-06-30 22:17:24 +00:00
|
|
|
# on l'utilise pour VLAN_OK
|
2018-07-11 17:37:22 +00:00
|
|
|
if port_profile.vlan_untagged:
|
|
|
|
DECISION_VLAN = int(port_profile.vlan_untagged.vlan_id)
|
2017-10-04 02:31:28 +00:00
|
|
|
extra_log = u"Force sur vlan " + str(DECISION_VLAN)
|
|
|
|
else:
|
2018-12-08 20:47:21 +00:00
|
|
|
DECISION_VLAN = RadiusOption.get_cached_value('vlan_decision_ok')
|
2017-09-10 20:50:20 +00:00
|
|
|
|
2018-12-04 19:00:18 +00:00
|
|
|
# Si le port est désactivé, on rejette la connexion
|
2018-06-30 17:04:15 +00:00
|
|
|
if not port.state:
|
2018-12-04 19:00:18 +00:00
|
|
|
return (sw_name, port.room, u'Port desactivé', None, False)
|
2018-06-30 17:04:15 +00:00
|
|
|
|
2018-06-30 22:17:24 +00:00
|
|
|
# Si radius est désactivé, on laisse passer
|
2018-07-11 17:37:22 +00:00
|
|
|
if port_profile.radius_type == 'NO':
|
2018-04-13 20:11:55 +00:00
|
|
|
return (sw_name,
|
|
|
|
"",
|
|
|
|
u"Pas d'authentification sur ce port" + extra_log,
|
2018-08-30 00:11:14 +00:00
|
|
|
DECISION_VLAN,
|
|
|
|
True)
|
2017-09-10 20:50:20 +00:00
|
|
|
|
2018-12-04 19:00:18 +00:00
|
|
|
# Si le 802.1X est activé sur ce port, cela veut dire que la personne a
|
|
|
|
# été accept précédemment
|
2018-06-30 22:17:24 +00:00
|
|
|
# Par conséquent, on laisse passer sur le bon vlan
|
2018-12-04 19:00:18 +00:00
|
|
|
if (nas_type.port_access_mode, port_profile.radius_type) == ('802.1X', '802.1X'):
|
2018-06-30 22:17:24 +00:00
|
|
|
room = port.room or "Chambre/local inconnu"
|
2018-12-04 19:00:18 +00:00
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
room,
|
|
|
|
u'Acceptation authentification 802.1X',
|
|
|
|
DECISION_VLAN,
|
|
|
|
True
|
|
|
|
)
|
2018-06-30 22:17:24 +00:00
|
|
|
|
|
|
|
# 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
|
2018-12-04 19:00:18 +00:00
|
|
|
# rattachés à ce port sont bien à jour de cotisation. Sinon on rejette
|
|
|
|
# (anti squattage)
|
|
|
|
# Il n'est pas possible de se connecter sur une prise strict sans adhérent
|
|
|
|
# à jour de cotis dedans
|
2018-07-11 17:37:22 +00:00
|
|
|
if port_profile.radius_mode == 'STRICT':
|
2017-12-09 03:11:44 +00:00
|
|
|
room = port.room
|
|
|
|
if not room:
|
2018-12-04 19:00:18 +00:00
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
"Inconnue",
|
|
|
|
u'Chambre inconnue',
|
2018-12-08 20:47:21 +00:00
|
|
|
RadiusOption('unknown_room_vlan').vlan_id,
|
|
|
|
RadiusOption('unknown_room')!= RadiusOption.REJECT
|
2018-12-04 19:00:18 +00:00
|
|
|
)
|
2017-09-10 20:50:20 +00:00
|
|
|
|
2018-04-13 20:11:55 +00:00
|
|
|
room_user = User.objects.filter(
|
|
|
|
Q(club__room=port.room) | Q(adherent__room=port.room)
|
|
|
|
)
|
2017-09-10 20:50:20 +00:00
|
|
|
if not room_user:
|
2018-12-04 19:00:18 +00:00
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
room,
|
|
|
|
u'Chambre non cotisante -> Web redirect',
|
|
|
|
None,
|
|
|
|
False
|
|
|
|
)
|
2017-10-25 22:55:53 +00:00
|
|
|
for user in room_user:
|
2018-09-02 14:53:21 +00:00
|
|
|
if user.is_ban() or user.state != User.STATE_ACTIVE:
|
2018-12-04 19:00:18 +00:00
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
room,
|
|
|
|
u'Utilisateur banni ou désactivé -> Web redirect',
|
|
|
|
None,
|
|
|
|
False
|
|
|
|
)
|
2018-09-02 14:53:21 +00:00
|
|
|
elif not (user.is_connected() or user.is_whitelisted()):
|
2018-12-04 19:00:18 +00:00
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
room,
|
|
|
|
u'Utilisateur non cotisant',
|
2018-12-08 20:47:21 +00:00
|
|
|
RadiusOption('non_member_vlan').vlan_id,
|
|
|
|
RadiusOption('non_member')!= RadiusOption.REJECT
|
2018-12-04 19:00:18 +00:00
|
|
|
)
|
2017-09-10 20:50:20 +00:00
|
|
|
# else: user OK, on passe à la verif MAC
|
|
|
|
|
2018-12-04 19:00:18 +00:00
|
|
|
# Si on fait de l'auth par mac, on cherche l'interface
|
|
|
|
# via sa mac dans la bdd
|
2018-07-11 17:37:22 +00:00
|
|
|
if port_profile.radius_mode == 'COMMON' or port_profile.radius_mode == 'STRICT':
|
2017-09-10 20:50:20 +00:00
|
|
|
# Authentification par mac
|
2018-04-13 20:11:55 +00:00
|
|
|
interface = (Interface.objects
|
|
|
|
.filter(mac_address=mac_address)
|
|
|
|
.select_related('machine__user')
|
|
|
|
.select_related('ipv4')
|
|
|
|
.first())
|
2017-09-10 20:50:20 +00:00
|
|
|
if not interface:
|
2017-12-09 03:11:44 +00:00
|
|
|
room = port.room
|
2018-12-04 19:00:18 +00:00
|
|
|
# On essaye de register la mac, si l'autocapture a été activée,
|
|
|
|
# on rejette pour faire une redirection web si possible.
|
|
|
|
if nas_type.autocapture_mac:
|
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
room,
|
|
|
|
u'Machine Inconnue -> Web redirect',
|
|
|
|
None,
|
|
|
|
False
|
|
|
|
)
|
|
|
|
# Sinon on bascule sur la politique définie dans les options
|
|
|
|
# radius.
|
2017-09-10 20:50:20 +00:00
|
|
|
else:
|
2018-12-04 19:00:18 +00:00
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
"",
|
|
|
|
u'Machine inconnue',
|
2018-12-08 20:47:21 +00:00
|
|
|
RadiusOption('unknown_machine_vlan').vlan_id,
|
|
|
|
RadiusOption('unknown_machine')!= RadiusOption.REJECT
|
2018-12-04 19:00:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# L'interface a été trouvée, on vérifie qu'elle est active,
|
|
|
|
# sinon on reject
|
2018-06-30 22:17:24 +00:00
|
|
|
# Si elle n'a pas d'ipv4, on lui en met une
|
|
|
|
# Enfin on laisse passer sur le vlan pertinent
|
2017-09-10 20:50:20 +00:00
|
|
|
else:
|
2017-12-09 03:11:44 +00:00
|
|
|
room = port.room
|
2018-12-05 11:19:29 +00:00
|
|
|
if interface.machine.user.is_ban():
|
2018-12-04 19:00:18 +00:00
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
room,
|
|
|
|
u'Adherent banni',
|
2018-12-08 20:47:21 +00:00
|
|
|
RadiusOption('banned_vlan').vlan_id,
|
|
|
|
RadiusOption('banned')!= RadiusOption.REJECT
|
2018-12-04 19:00:18 +00:00
|
|
|
)
|
2017-10-30 19:53:49 +00:00
|
|
|
if not interface.is_active:
|
2018-12-04 19:00:18 +00:00
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
room,
|
|
|
|
u'Machine non active / adherent non cotisant',
|
2018-12-08 20:47:21 +00:00
|
|
|
RadiusOption('non_member_vlan').vlan_id,
|
|
|
|
RadiusOption('non_member')!= RadiusOption.REJECT
|
2018-12-04 19:00:18 +00:00
|
|
|
)
|
|
|
|
# Si on choisi de placer les machines sur le vlan
|
|
|
|
# correspondant à leur type :
|
2018-12-08 20:47:21 +00:00
|
|
|
if RadiusOption.get_cached_value('radius_general_policy') == 'MACHINE':
|
2018-08-03 16:08:33 +00:00
|
|
|
DECISION_VLAN = interface.type.ip_type.vlan.vlan_id
|
|
|
|
if not interface.ipv4:
|
2017-10-30 19:53:49 +00:00
|
|
|
interface.assign_ipv4()
|
2018-12-04 19:00:18 +00:00
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
room,
|
|
|
|
u"Ok, Reassignation de l'ipv4" + extra_log,
|
|
|
|
DECISION_VLAN,
|
|
|
|
True
|
|
|
|
)
|
2017-10-30 19:53:49 +00:00
|
|
|
else:
|
2018-12-04 19:00:18 +00:00
|
|
|
return (
|
|
|
|
sw_name,
|
|
|
|
room,
|
|
|
|
u'Machine OK' + extra_log,
|
|
|
|
DECISION_VLAN,
|
|
|
|
True
|
|
|
|
)
|