FreeRadius + Mysql + PPTPD en Debian.

Siguiendo al post anterior, ahora toca crear el servidor PPTPD, para por ejemplo, lanzar la vpn desde nuestro android.

Primero comprobamos si tenemos en el Kernel de nuestro linux, el soporte para MPPE, para ello:

# modprobe ppp-compress-18 && echo OK

Si sale OK, pues eso, ha ido bien…. ¿ y qué es eso de MPPE ?, pues nada, un protocolo de micro$oft para encriptar datos, léase:

http://en.wikipedia.org/wiki/Microsoft_Point-to-Point_Encryption

Y ojo, que tiene su propia RFC (O_o).

Ahora, instalamos el server pptpd en nuestro querido debian:

# apt-get install pptpd

Antes de empezar a configurar, hemos de tener claro:

1º) Pool de Ip’s que les asignaremos a los usuarios
2º) Dentro del Pool, las opciones de dns y demás que querremos usar para los clientes

Editamos el fichero /etc/pptpd.conf y lo vamos dejando tal que:

localip 192.168.2.13
remoteip 192.168.100.234-240

Donde localip, que con gran ojo avizor, deducimos que es la ip de nuestro servidor y, el que tenga la suerte de poder ponerle la ip pública directamente, pues eso que se lleva.

La remoteip, será el pool de IP’s que asignaremos a los usuarios remotos.

Guardamos los cambios, reinciamos el daemon pptpd y seguimos configurando y instalamos el paquete libradiusclient-ng2, que será el responsable de la relación entre pptpd y freeradius… para ello:

apt-get install libradiusclient-ng2

Y renombramos la carpeta para que quede más elegante:

mv /etc/radiusclient-ng/ /etc/radiusclient/

Creamos un fichero, que ya explicaremos la función:

echo “” > /etc/radiusclient/port-id-map

Ahora editaremos el fichero radiusclient.conf

# nano /etc/radiusclient/radiusclient.conf

Y lo dejamos tal que:

# General settings

# specify which authentication comes first respectively which
# authentication is used. possible values are: “radius” and “local”.
# if you specify “radius,local” then the RADIUS server is asked
# first then the local one. if only one keyword is specified only
# this server is asked.
auth_order radius

# maximum login tries a user has
login_tries 4

# timeout for all login tries
# if this time is exceeded the user is kicked out
login_timeout 60

# name of the nologin file which when it exists disables logins.
# it may be extended by the ttyname which will result in
# a terminal specific lock (e.g. /etc/nologin.ttyS2 will disable
# logins on /dev/ttyS2)
nologin /etc/nologin

# name of the issue file. it’s only display when no username is passed
# on the radlogin command line
issue /etc/radiusclient/issue

# RADIUS settings

# RADIUS server to use for authentication requests. this config
# item can appear more then one time. if multiple servers are
# defined they are tried in a round robin fashion if one
# server is not answering.
# optionally you can specify a the port number on which is remote
# RADIUS listens separated by a colon from the hostname. if
# no port is specified /etc/services is consulted of the radius
# service. if this fails also a compiled in default is used.
authserver localhost:1812

# RADIUS server to use for accouting requests. All that I
# said for authserver applies, too.
#
acctserver localhost:1812

# file holding shared secrets used for the communication
# between the RADIUS client and server
servers /etc/radiusclient/servers

# dictionary of allowed attributes and values
# just like in the normal RADIUS distributions
dictionary /etc/radiusclient/dictionary

# program to call for a RADIUS authenticated login
login_radius /usr/sbin/login.radius

# file which holds sequence number for communication with the
# RADIUS server
seqfile /var/run/radius.seq

# file which specifies mapping between ttyname and NAS-Port attribute
mapfile /etc/radiusclient/port-id-map

# default authentication realm to append to all usernames if no
# realm was explicitly specified by the user
# the radiusd directly form Livingston doesnt use any realms, so leave
# it blank then
default_realm

# time to wait for a reply from the RADIUS server
radius_timeout 10

# resend request this many times before trying the next server
radius_retries 3

# local address from which radius packets have to be sent
# bindaddr 0.0.0.0

# LOCAL settings

# program to execute for local login
# it must support the -f flag for preauthenticated login
login_local /bin/login

Definiremos nuestros servidores radius (modificando lo que está en negrita), con lo que toque.

Ahora definiremos la pre-shared key para los servidores radius:

nano /etc/radiusclient/servers

y colocamos nuestros datos:

#Server Name or Client/Server pair Key
#—————- —————
ip.de.nuestro.radius_server misuperclavemolona

Editamos el fichero pptpd-options

nano /etc/ppp/pptpd-options

Por ahora, nos vamos a centrar en pap / spap… una vez nos funcione, buscaremos otros métodos más seguros:

###############################################################################
# $Id: pptpd-options 4643 2006-11-06 18:42:43Z rene $
#
# Sample Poptop PPP options file /etc/ppp/pptpd-options
# Options used by PPP when a connection arrives from a client.
# This file is pointed to by /etc/pptpd.conf option keyword.
# Changes are effective on the next connection. See “man pppd”.
#
# You are expected to change this file to suit your system. As
# packaged, it requires PPP 2.4.2 and the kernel MPPE module.
###############################################################################

# Authentication

# Name of the local system for authentication purposes
# (must match the second field in /etc/ppp/chap-secrets entries)
name minombrechulo-del-servidor

# Optional: domain name to use for authentication
domain craem.net

# Strip the domain prefix from the username before authentication.
# (applies if you use pppd with chapms-strip-domain patch)
#chapms-strip-domain

# Encryption
# Debian: on systems with a kernel built with the package
# kernel-patch-mppe >= 2.4.2 and using ppp >= 2.4.2, …
# {{{
require-pap
require-chap
refuse-mschap

# Require the peer to authenticate itself using MS-CHAPv2 [Microsoft
# Challenge Handshake Authentication Protocol, Version 2] authentication.
# refuse-mschap-v2
# Require MPPE 128-bit encryption
# (note that MPPE requires the use of MSCHAP-V2 during authentication)
# refuse-mppe-128
# }}}

# Network and Routing

# If pppd is acting as a server for Microsoft Windows clients, this
# option allows pppd to supply one or two DNS (Domain Name Server)
# addresses to the clients. The first instance of this option
# specifies the primary DNS address; the second instance (if given)
# specifies the secondary DNS address.
# Attention! This information may not be taken into account by a Windows
# client. See KB311218 in Microsoft’s knowledge base for more information.
ms-dns 8.8.8.8
ms-dns 8.8.4.4

# If pppd is acting as a server for Microsoft Windows or “Samba”
# clients, this option allows pppd to supply one or two WINS (Windows
# Internet Name Services) server addresses to the clients. The first
# instance of this option specifies the primary WINS address; the
# second instance (if given) specifies the secondary WINS address.
#ms-wins 10.0.0.3
#ms-wins 10.0.0.4

# Add an entry to this system’s ARP [Address Resolution Protocol]
# table with the IP address of the peer and the Ethernet address of this
# system. This will have the effect of making the peer appear to other
# systems to be on the local ethernet.
# (you do not need this if your PPTP server is responsible for routing
# packets to the clients — James Cameron)
proxyarp

# Debian: do not replace the default route
nodefaultroute

# Logging

# Enable connection debugging facilities.
# (see your syslog configuration for where pppd sends to)
#debug

# Print out all the option values which have been set.
# (often requexsted by mailing list to verify options)
#dump

# Miscellaneous

# Create a UUCP-style lock file for the pseudo-tty to ensure exclusive
# access.
lock

# Disable BSD-Compress compression
nobsdcomp

mtu 1200
mru 1200
plugin radius.so
plugin radattr.so
lcp-echo-failure 50

Creamos un NAS en nuestro radius (lo vimos en el post anterior), reiniciamos el servicio pptpd y listo.

Probamos un cliente ( un android, por ejemplo)

Starting PPTP Daemon: pptpd.
root@radius_lan:/etc/ppp# tail -f /var/log/syslog
Oct 8 00:51:12 rlan pppd[2488]: Using interface ppp0
Oct 8 00:51:12 rlan pppd[2488]: Connect: ppp0 <--> /dev/pts/1
Oct 8 00:51:12 rlan pptpd[2486]: GRE: Bad checksum from pppd.
Oct 8 00:51:12 rlan pppd[2488]: Deflate (15) compression enabled
Oct 8 00:51:12 rlan pppd[2488]: Cannot determine ethernet address for proxy ARP
Oct 8 00:51:12 rlan pppd[2488]: local IP address 192.168.2.94
Oct 8 00:51:12 rlan pppd[2488]: remote IP address 192.168.100.234
Oct 8 00:51:26 rlan pptpd[2495]: MGR: Maximum of 200 connections reduced to 7, not enough IP addresses given
Oct 8 00:51:26 rlan pptpd[2496]: MGR: Manager process started
Oct 8 00:51:26 rlan pptpd[2496]: MGR: Maximum of 7 connections available
Oct 8 00:51:40 rlan pptpd[2486]: CTRL: EOF or bad error reading ctrl packet length.
Oct 8 00:51:40 rlan pptpd[2486]: CTRL: couldn’t read packet header (exit)
Oct 8 00:51:40 rlan pptpd[2486]: CTRL: CTRL read failed
Oct 8 00:51:40 rlan pptpd[2486]: CTRL: Reaping child PPP[2488]
Oct 8 00:51:40 rlan pptpd[2498]: CTRL: Client 192.168.2.68 control connection started
Oct 8 00:51:40 rlan pptpd[2498]: CTRL: Starting call (launching pppd, opening GRE)
Oct 8 00:51:40 rlan pppd[2499]: Plugin radius.so loaded.
Oct 8 00:51:40 rlan pppd[2499]: RADIUS plugin initialized.
Oct 8 00:51:40 rlan pppd[2499]: Plugin radattr.so loaded.
Oct 8 00:51:40 rlan pppd[2499]: RADATTR plugin initialized.
Oct 8 00:51:40 rlan pppd[2499]: Plugin /usr/lib/pptpd/pptpd-logwtmp.so loaded.
Oct 8 00:51:40 rlan pppd[2499]: pppd 2.4.5 started by root, uid 0
Oct 8 00:51:40 rlan pppd[2499]: Using interface ppp1
Oct 8 00:51:40 rlan pppd[2499]: Connect: ppp1 <--> /dev/pts/2
Oct 8 00:51:40 rlan pptpd[2498]: GRE: Bad checksum from pppd.
Oct 8 00:51:40 rlan pppd[2499]: Deflate (15) compression enabled
Oct 8 00:51:40 rlan pppd[2499]: Cannot determine ethernet address for proxy ARP
Oct 8 00:51:40 rlan pppd[2499]: local IP address 192.168.2.13
Oct 8 00:51:40 rlan pppd[2499]: remote IP address 192.168.100.234

Y vemos el log del freeradius:

Listening on authentication address * port 1812
Listening on accounting address * port 1813
Listening on authentication address 127.0.0.1 port 18120 as server inner-tunnel
Listening on proxy address * port 1814
Ready to process requests.
rad_recv: Access-Request packet from host 127.0.0.1 port 33461, id=45, length=108
Service-Type = Framed-User
Framed-Protocol = PPP
User-Name = “angel”
CHAP-Challenge = 0x33c4ad40b662648176b52f85b7b47c47030561884ce3
CHAP-Password = 0x679fa22be2a3a09c79e7ff5f047edb60ae
Calling-Station-Id = “192.168.2.68”
NAS-IP-Address = 127.0.1.1
NAS-Port = 0

# Executing section authorize from file /etc/freeradius/sites-enabled/default
+- entering group authorize {…}
++[preprocess] returns ok
[chap] Setting ‘Auth-Type := CHAP’
++[chap] returns ok
++[digest] returns noop
[suffix] No ‘@’ in User-Name = “angel”, looking up realm NULL
[suffix] No such realm “NULL”
++[suffix] returns noop
[eap] No EAP-Message, not doing EAP
++[eap] returns noop
[files] users: Matched entry DEFAULT at line 172
++[files] returns ok
[sql] expand: %{User-Name} -> angel
[sql] sql_set_user escaped user –> ‘angel’
rlm_sql (sql): Reserving sql socket id: 3
[sql] expand: SELECT id, username, attribute, value, op FROM radcheck WHERE username = ‘%{SQL-User-Name}’ ORDER BY id -> SELECT id, username, attribute, value, op FROM radcheck WHERE username = ‘angel’ ORDER BY id
[sql] User found in radcheck table
[sql] expand: SELECT id, username, attribute, value, op FROM radreply WHERE username = ‘%{SQL-User-Name}’ ORDER BY id -> SELECT id, username, attribute, value, op FROM radreply WHERE username = ‘angel’ ORDER BY id
[sql] expand: SELECT groupname FROM radusergroup WHERE username = ‘%{SQL-User-Name}’ ORDER BY priority -> SELECT groupname FROM radusergroup WHERE username = ‘angel’ ORDER BY priority
rlm_sql (sql): Released sql socket id: 3
++[sql] returns ok
++[expiration] returns noop
++[logintime] returns noop
[pap] WARNING: Auth-Type already set. Not setting to PAP
++[pap] returns noop
Found Auth-Type = CHAP
# Executing group from file /etc/freeradius/sites-enabled/default
+- entering group CHAP {…}
[chap] login attempt by “angel” with CHAP password
[chap] Using clear text password “password_chulo” for user angel authentication.
[chap] chap user angel authenticated succesfully
++[chap] returns ok
# Executing section post-auth from file /etc/freeradius/sites-enabled/default
+- entering group post-auth {…}
[sql] expand: %{User-Name} -> angel
[sql] sql_set_user escaped user –> ‘angel’
[sql] expand: %{User-Password} ->
[sql] … expanding second conditional

[sql] expand: %{Chap-Password} -> 0x679fa22be2a3a09c79e7ff5f047edb60ae
[sql] expand: INSERT INTO radpostauth (username, pass, reply, authdate) VALUES ( ‘%{User-Name}’, ‘%{%{User-Password}:-%{Chap-Password}}’, ‘%{reply:Packet-Type}’, ‘%S’) -> INSERT INTO radpostauth (username, pass, reply, authdate) VALUES ( ‘angel’, ‘0x679fa22be2a3a09c79e7ff5f047edb60ae’, ‘Access-Accept’, ‘2012-10-08 00:54:23’)
rlm_sql (sql) in sql_postauth: query is INSERT INTO radpostauth (username, pass, reply, authdate) VALUES ( ‘angel’, ‘0x679fa22be2a3a09c79e7ff5f047edb60ae’, ‘Access-Accept’, ‘2012-10-08 00:54:23’)
rlm_sql (sql): Reserving sql socket id: 2
rlm_sql (sql): Released sql socket id: 2
++[sql] returns ok
[sql_log] Processing sql_log_postauth
[sql_log] expand: %{User-Name} -> angel
[sql_log] expand: %{%{User-Name}:-DEFAULT} -> angel
[sql_log] sql_set_user escaped user –> ‘angel’
[sql_log] WARNING: Deprecated conditional expansion “:-“. See “man unlang” for details
[sql_log] … expanding second conditional
[sql_log] expand: Chap-Password -> Chap-Password
[sql_log] expand: INSERT INTO radpostauth (username, pass, reply, authdate) VALUES (‘%{User-Name}’, ‘%{User-Password:-Chap-Password}’, ‘%{reply:Packet-Type}’, ‘%S’); -> INSERT INTO radpostauth (username, pass, reply, authdate) VALUES (‘angel’, ‘Chap-Password’, ‘Access-Accept’, ‘2012-10-08 00:54:23’);
[sql_log] expand: /var/log/freeradius/radacct/sql-relay -> /var/log/freeradius/radacct/sql-relay
++[sql_log] returns ok
++[exec] returns noop
Sending Access-Accept of id 45 to 127.0.0.1 port 33461
Framed-Protocol = PPP
Framed-Compression = Van-Jacobson-TCP-IP

Finished request 0.
Going to the next request
Waking up in 4.9 seconds.
Invalid packet code 4 sent to authentication port from client port 51954 : IGNORED
Waking up in 4.9 seconds.
Cleaning up request 0 ID 45 with timestamp +8
Ready to process requests.
Invalid packet code 4 sent to authentication port from client port 51954 : IGNORED
Ready to process requests.

Enjoy your server 😉