Cómo usar FreeRADIUS con LinOTP 2 para autenticación de dos factores con contraseñas de un solo uso (OTP)
Introducción
LinOTP es un backend de contraseñas de un solo uso (OTP) que permite implementar autenticación de dos factores (2FA) con una amplia variedad de tokens hardware, tokens software y SMS. La edición Community (AGPLv3) no incluye un módulo C para FreeRADIUS, pero ofrece una API web sencilla que podemos invocar desde FreeRADIUS.
La API de LinOTP
LinOTP expone vías HTTP para validar OTPs. Las URLs típicas son:
https://yourServer/validate/check?user=....&pass=....
o
https://yourServer/validate/simplecheck?user=...&pass=...
Sustituya yourServer por la dirección de su servidor LinOTP y proteja la comunicación por TLS. En los ejemplos más abajo se usa la ruta /validate/simplecheck.
Importante: la API acepta parámetros user y pass por GET en los ejemplos; para producción, considere HTTPS estricto, validación del certificado y timeouts.
FreeRADIUS: opciones y módulo recomendado
Hay varias formas de integrar LinOTP con FreeRADIUS:
- rlm_exec: ejecutar un programa externo que haga la validación.
- rlm_perl: escribir una extensión en Perl que consulte la API de LinOTP (este howto usa rlm_perl).
- módulos comerciales/Enterprise: con integración directa en C (no disponible en la Community).
Este documento adapta el ejemplo de rlm_perl para realizar una llamada HTTP a LinOTP y devolver OK o REJECT según la respuesta.
El módulo Perl (ejemplo)
A continuación se muestra el módulo Perl que puede usar tal cual o adaptar. Configure el valor de $URL al endpoint de su LinOTP.
#
# 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 St, Fifth Floor, Boston, MA 02110-1301, USA
#
# Copyright 2002 The FreeRADIUS server project
# Copyright 2002 Boian Jordanov <[email protected]>
# Copyright 2011 linotp project <[email protected]>
#
# Based on the Example code for use with rlm_perl
#
#
=head1 NAME
freeradius_perl - Perl module for use with FreeRADIUS rlm_perl, to authenticate against
LinOTP http://www.linotp.org
=head1 SYNOPSIS
use with freeradius:
Configure rlm_perl to work with LinOTP:
in /etc/freeradius/users
set:
DEFAULT Auth-type := perl
in /etc/freeradius/modules/perl
point
perl {
module =
to this file
in /etc/freeradius/sites-enabled/
set
authenticate{
perl
[....]
=head1 DESCRIPTION
This module enables freeradius to authenticate using LinOTP.
TODO:
* checking of server certificate
=head2 Methods
* authenticate
=head1 AUTHOR
Cornelius Koelbel ([email protected])
=head1 COPYRIGHT
Copyright 2011
This library is free software; you can redistribute it
under the GPLv2.
=head1 SEE ALSO
perl(1).
=cut
use strict;
use LWP 5.64;
# use ...
# This is very important ! Without this script will not get the filled hashesh from main.
use vars qw(%RAD_REQUEST %RAD_REPLY %RAD_CHECK $URL);
use Data::Dumper;
$URL = "https://localhost/validate/simplecheck";
# This is hash wich hold original request from radius
#my %RAD_REQUEST;
# In this hash you add values that will be returned to NAS.
#my %RAD_REPLY;
#This is for check items
#my %RAD_CHECK;
#
# This the remapping of return values
#
use constant RLM_MODULE_REJECT=> 0;# /* immediately reject the request */
use constant RLM_MODULE_FAIL=> 1;# /* module failed, don't reply */
use constant RLM_MODULE_OK=> 2;# /* the module is OK, continue */
use constant RLM_MODULE_HANDLED=> 3;# /* the module handled the request, so stop. */
use constant RLM_MODULE_INVALID=> 4;# /* the module considers the request invalid. */
use constant RLM_MODULE_USERLOCK=> 5;# /* reject the request (user is locked out) */
use constant RLM_MODULE_NOTFOUND=> 6;# /* user not found */
use constant RLM_MODULE_NOOP=> 7;# /* module succeeded without doing anything */
use constant RLM_MODULE_UPDATED=> 8;# /* OK (pairs modified) */
use constant RLM_MODULE_NUMCODES=> 9;# /* How many return codes there are */
# Function to handle authorize
sub authorize {
# For debugging purposes only
# &log_request_attributes;
# Here's where your authorization code comes
# You can call another function from here:
&test_call;
return RLM_MODULE_OK;
}
# Function to handle authenticate
sub authenticate {
# For debugging purposes only
# &log_request_attributes;
my $ua = LWP::UserAgent->new();
my $req = HTTP::Request->new(GET => $URL . "?user=" .
$RAD_REQUEST{'User-Name'} . "&pass=" .
$RAD_REQUEST{'User-Password'} );
my $response = $ua->request( $req );
die "Error at $URL\n ", $response->status_line, "\n Aborting"
unless $response->is_success;
if($response->content =~ m/:\-\)/i) {
return RLM_MODULE_OK;
} else {
$RAD_REPLY{'Reply-Message'} = "LinOTP server denied access!";
return RLM_MODULE_REJECT;
}
}
# Function to handle preacct
sub preacct {
# For debugging purposes only
# &log_request_attributes;
return RLM_MODULE_OK;
}
# Function to handle accounting
sub accounting {
# For debugging purposes only
# &log_request_attributes;
# You can call another subroutine from here
&test_call;
return RLM_MODULE_OK;
}
# Function to handle checksimul
sub checksimul {
# For debugging purposes only
# &log_request_attributes;
return RLM_MODULE_OK;
}
# Function to handle pre_proxy
sub pre_proxy {
# For debugging purposes only
# &log_request_attributes;
return RLM_MODULE_OK;
}
# Function to handle post_proxy
sub post_proxy {
# For debugging purposes only
# &log_request_attributes;
return RLM_MODULE_OK;
}
# Function to handle post_auth
sub post_auth {
# For debugging purposes only
# &log_request_attributes;
return RLM_MODULE_OK;
}
# Function to handle xlat
sub xlat {
# For debugging purposes only
# &log_request_attributes;
# Loads some external perl and evaluate it
my ($filename,$a,$b,$c,$d) = @_;
&radiusd::radlog(1, "From xlat $filename ");
&radiusd::radlog(1,"From xlat $a $b $c $d ");
local *FH;
open FH, $filename or die "open '$filename' $!";
local($/) = undef;
my $sub = ;
close FH;
my $eval = qq{ sub handler{ $sub;} };
eval $eval;
eval {main->handler;};
}
# Function to handle detach
sub detach {
# For debugging purposes only
# &log_request_attributes;
# Do some logging.
&radiusd::radlog(0,"rlm_perl::Detaching. Reloading. Done.");
}
#
# Some functions that can be called from other functions
#
sub test_call {
# Some code goes here
}
sub log_request_attributes {
# This shouldn't be done in production environments!
# This is only meant for debugging!
for (keys %RAD_REQUEST) {
&radiusd::radlog(1, "RAD_REQUEST: $_ = $RAD_REQUEST{$_}");
}
}
1;
Nota: debe adaptar la variable $URL dentro del módulo Perl al endpoint real de su servidor LinOTP.
Configuración mínima de FreeRADIUS
En /etc/freeradius/users añada una regla para usar perl:
DEFAULT Auth-Type := perl
En /etc/freeradius/modules/perl apunte ‘module’ al fichero Perl que contiene el código anterior. Ejemplo:
perl { module = /etc/freeradius/mods-perl/linotp_perl.pl }
En /etc/freeradius/sites-enabled/
dentro de la sección authenticate incluya ‘perl’ para invocar el módulo.
Reinicie y pruebe FreeRADIUS en modo depuración (freeradius -X) para ver la validación en vivo.
Mejoras y consideraciones de seguridad
Importante
- Validación TLS: el ejemplo NO valida el certificado SSL del servidor LinOTP. En producción debe verificar el certificado y usar CA de confianza.
- Manejo de errores: añada timeouts, reintentos limitados y registro (logging) detallado para diagnósticos.
- Robustez: considere balanceo y redundancia para el servicio LinOTP (múltiples endpoints) y manejo de fallos favor del usuario.
- Evite exponer credenciales en URLs GET en entornos compartidos; si su API lo permite, prefiera POST o métodos más seguros.
Alternativas y cuándo elegirlas
- rlm_exec: útil si prefiere un script en Python/Go/Shell que haga la validación.
- Integración de PAM: cuando el sistema de autenticación ya se basa en PAM.
- Módulo Enterprise de LinOTP: si necesita integración más profunda y soporte comercial.
Cuándo no usar este enfoque
- Cuando la latencia entre RADIUS y LinOTP sea alta o la comunicación sea poco fiable; entonces prefiera replicación local o uso de módulos nativos.
Mini-metodología de despliegue (pasos rápidos)
- Preparar y asegurar el servidor LinOTP (HTTPS, certificados).
- Probar el endpoint /validate/simplecheck con curl desde el servidor RADIUS.
- Colocar el script Perl en /etc/freeradius/mods-perl/ y ajustar $URL.
- Configurar /etc/freeradius/modules/perl y /etc/freeradius/users.
- Test en laboratorio con freeradius -X.
- Habilitar logging y métricas, planificar HA.
Lista de verificación por roles
Administrador de seguridad:
- Revisar certificados TLS y políticas de expiración.
- Aprobar el uso de OTPs para grupos de usuarios.
Ingeniero de RADIUS:
- Implementar y probar el módulo Perl en entorno de pruebas.
- Monitorizar latencia y errores.
Operaciones:
- Configurar alertas si LinOTP responde con errores persistentes.
- Planificar rollback a autenticación previa si falla el sistema 2FA.
Criterios de aceptación
- FreeRADIUS responde OK cuando LinOTP valida el OTP.
- FreeRADIUS rechaza cuando LinOTP niega la autenticación.
- Los certificados TLS del servidor LinOTP se verifican correctamente.
- Los timeouts y reintentos están configurados para evitar bloqueos largos.
Glosario de 1 línea
- LinOTP: backend para gestionar tokens OTP y validar contraseñas de un solo uso.
- RADIUS: protocolo de autenticación y autorización para redes.
- OTP: contraseña de un solo uso; factor adicional para 2FA.
- rlm_perl: módulo de FreeRADIUS que permite extender lógica con Perl.
Resumen
Integrar FreeRADIUS con LinOTP 2 usando rlm_perl es una solución práctica y rápida para habilitar 2FA con OTPs. El ejemplo Perl provee un punto de partida funcional, pero requiere mejoras en seguridad (validación TLS), manejo de errores y disponibilidad para uso en producción.
Resumen breve: adapte $URL, pruebe en entorno controlado, habilite verificación de certificados y monitorice la latencia y errores.
Materiales similares

Inmovilizar las 2 primeras filas en Excel

Restaurar canales desaparecidos en Teams

Refrescar Windows 11: 3 métodos rápidos

Configurar Servidor Terminal RDS en Server 2008 R2

Malware a nivel de kernel: guía de protección
