Rc.openvpn (bridged): differenze tra le versioni
Da Slacky.eu.
(→Installazione) |
|||
| (7 revisioni intermedie di un utente non mostrate) | |||
| Riga 2: | Riga 2: | ||
= Introduzione = |
= Introduzione = |
||
| − | Dietro suggerimento di conraid in questo thread del forum: http://www.slacky.eu/forum/viewtopic.php?f=20&t=23897&start=0&st=0&sk=t&sd=a eccomi qui a scrivere la mia prima pagina wiki.:) |
+ | Dietro suggerimento di conraid in [http://www.slacky.eu/forum/viewtopic.php?f=20&t=23897&start=0&st=0&sk=t&sd=a questo] thread del forum eccomi qui a scrivere la mia prima pagina wiki.:) |
= Descrizione = |
= Descrizione = |
||
| − | Questo script avvia un server OpenVPN (già configurato) in modalità bridged. Per indicazioni su come configurare un server vpn in modalità bridged tramite OpenVPN vedere qui: http://www.slacky.eu/wikislack/index.php?title=VPN_%28OpenVPN_2.0%29_-_Ethernet_Bridging_HOWTO. |
+ | Questo script avvia un server OpenVPN (già configurato) in modalità bridged. Per indicazioni su come configurare un server vpn in modalità bridged tramite OpenVPN vedere [http://www.slacky.eu/wikislack/index.php?title=VPN_%28OpenVPN_2.0%29_-_Ethernet_Bridging_HOWTO. qui]. |
| + | |||
| + | = Versione corrente = |
||
| + | |||
| + | La versione corrente è la 1.04 del 10/04/2008. |
||
= Requisiti = |
= Requisiti = |
||
| Riga 24: | Riga 24: | ||
; OPENVPN_CFG_FILE : è il percorso (assoluto) del file di configurazione di OpenVPN da usare. Se non specificato, viene assunto come file di configurazione /etc/openvpn/openvpn.conf. |
; OPENVPN_CFG_FILE : è il percorso (assoluto) del file di configurazione di OpenVPN da usare. Se non specificato, viene assunto come file di configurazione /etc/openvpn/openvpn.conf. |
||
; OPENVPN_OPTS : permette di specificare eventuali opzioni aggiuntive da passare a OpenVPN. Non viene effettuato alcun controllo sulla loro validità (percui OCCHIO). |
; OPENVPN_OPTS : permette di specificare eventuali opzioni aggiuntive da passare a OpenVPN. Non viene effettuato alcun controllo sulla loro validità (percui OCCHIO). |
||
| − | ; NEED_RECONF : è una variabile usata per ripristinare la configurazione di rete originaria dopo il lancio degli script per avviare/fermare il bridge.<br/> Se viene effettuato il bridging dell'interfaccia di rete che si usa per accedere ad internet, per esempio eth0, questi script fanno perdere al kernel la cognizione del default gateway e dell'indirizzo ip di eth0. Questo perchè il bridge appena avviato "maschera" dentro di sé l'interfaccia eth0 e quella utilizzata per il bridging sulla vpn (solitamente tap0). Il default gateway, che prima il kernel sapeva accessibile tramite l'interfaccia eth0, ora non lo è più perchè eth0 non è più su nessuna rete. Al suo posto è comparso il bridge (br0 se non diversamente configurato). È quindi necessario rispecificare il default gateway in modo che riappaia nella tabella di routing del kernel. <br/>Tornando alla variabile NEED_RECONF, se settata a TRUE, ogni qualvolta sarà necessario, il default gateway e/o l'indirizzo ip dell'interfaccia di rete usata per il bridging verrano ripristinati. Qualsiasi altro valore diverso da TRUE è valido per disattivare questo "work-around". |
+ | ; NEED_RECONF : è una variabile booleana usata per ripristinare la configurazione di rete originaria dopo il lancio degli script per avviare/fermare il bridge.<br/> Se viene effettuato il bridging dell'interfaccia di rete che si usa per accedere ad internet, per esempio eth0, questi script fanno perdere al kernel la cognizione del default gateway e dell'indirizzo ip di eth0. Questo perchè il bridge appena avviato "maschera" dentro di sé l'interfaccia eth0 e quella utilizzata per il bridging sulla vpn (solitamente tap0). Il default gateway, che prima il kernel sapeva accessibile tramite l'interfaccia eth0, ora non lo è più perchè eth0 non è più su nessuna rete. Al suo posto è comparso il bridge (br0 se non diversamente configurato). È quindi necessario rispecificare il default gateway in modo che riappaia nella tabella di routing del kernel. <br/>In pratica, dovrebbe essere necessario settare a TRUE la variabile NEED_RECONF solo nel caso sopra descritto, cioè di bridging dell'interfaccia di rete con accesso a internet. Se settata a TRUE, ogni qualvolta sarà necessario, il default gateway e/o l'indirizzo ip dell'interfaccia di rete usata per il bridging verrano ripristinati. <br/>Qualsiasi altro valore diverso da TRUE è valido per disattivare questo "work-around". |
; GATEWAY_IP : è l'indirizzo IP del gateway. Va specificato solo se NEED_RECONF è TRUE. |
; GATEWAY_IP : è l'indirizzo IP del gateway. Va specificato solo se NEED_RECONF è TRUE. |
||
| Riga 33: | Riga 33: | ||
# Script able to start/stop a configured OpenVPN daemon in bridged mode. |
# Script able to start/stop a configured OpenVPN daemon in bridged mode. |
||
# Brought to you by 414n. |
# Brought to you by 414n. |
||
| − | # 8/4/2008 |
+ | # 10/4/2008 |
| − | # Version 1.03 |
+ | # Version 1.04 |
# Global vars: |
# Global vars: |
||
| Riga 70: | Riga 70: | ||
# This is the gateway ip. If you specify NEED_RECONF=TRUE, the gateway will |
# This is the gateway ip. If you specify NEED_RECONF=TRUE, the gateway will |
||
# be defaulted to what you type here. |
# be defaulted to what you type here. |
||
| − | GATEWAY_IP="192.168.5.254" |
+ | GATEWAY_IP="a.b.c.d" |
| Riga 83: | Riga 83: | ||
echo "Couldn\'t find $BRIDGE_START_CMD in $BRIDGE_SCRIPTS_PATH." |
echo "Couldn\'t find $BRIDGE_START_CMD in $BRIDGE_SCRIPTS_PATH." |
||
exit 1 |
exit 1 |
||
| − | #else |
+ | fi |
| − | # echo "Bridge scripts found!" |
+ | |
| + | |||
| + | if ! [ -x "$BRIDGE_SCRIPTS_PATH/$BRIDGE_STOP_CMD" ] |
||
| + | then |
||
| + | echo "Couldn\'t find $BRIDGE_STOP_CMD in $BRIDGE_SCRIPTS_PATH." |
||
| + | exit 1 |
||
fi |
fi |
||
| Riga 297: | Riga 297: | ||
* Avvio automatico al boot |
* Avvio automatico al boot |
||
*# Copiare lo script in /etc/rc.d/rc.openvpn-br |
*# Copiare lo script in /etc/rc.d/rc.openvpn-br |
||
| + | *# Rendere lo script eseguibile (chmod +x /etc/rc.d/rc.openvpn-br). |
||
*# Aggiungere una riga a /etc/rc.d/rc.local con all'interno: |
*# Aggiungere una riga a /etc/rc.d/rc.local con all'interno: |
||
*#:<pre> |
*#:<pre> |
||
| − | *#:/etc/rc.d/rc.openvpn-br start |
+ | *#:if [ -x /etc/rc.d/rc.openvpn-br ] |
| + | *#:then |
||
| + | *#: /etc/rc.d/rc.openvpn-br start |
||
| + | *#:fi |
||
*#:</pre> |
*#:</pre> |
||
*# Aggiungere una riga a /etc/rc.d/rc.local_shutdown: |
*# Aggiungere una riga a /etc/rc.d/rc.local_shutdown: |
||
*#:<pre> |
*#:<pre> |
||
| − | *#:/etc/rc.d/rc.openvpn-br stop |
+ | *#:if [ -x /etc/rc.d/rc.openvpn-br ] |
| + | *#:then |
||
| + | *#: /etc/rc.d/rc.openvpn-br stop |
||
| + | *#:fi |
||
*#:</pre> |
*#:</pre> |
||
* Avvio manuale |
* Avvio manuale |
||
| − | ** In questo caso lo script può essere messo in una cartella a piacere. Consiglio comunque di copiarlo in /etc/rc.d, ma di non renderlo eseguibile (in modo che all'avvio non venga lanciato in automatico). |
+ | ** In questo caso lo script può essere messo in una cartella a piacere. Consiglio comunque di copiarlo in /etc/rc.d. |
** Lo script deve essere lanciato da root. |
** Lo script deve essere lanciato da root. |
||
** Per eseguire lo script senza che venga reso eseguibile, usare |
** Per eseguire lo script senza che venga reso eseguibile, usare |
||
| Riga 320: | Riga 321: | ||
; 1.02 : aggiunto controllo sulla presenza di GATEWAY_IP se NEED_RECONF è TRUE. |
; 1.02 : aggiunto controllo sulla presenza di GATEWAY_IP se NEED_RECONF è TRUE. |
||
; 1.03 : aggiunti ulteriori controlli sull'opzione NEED_RECONF. |
; 1.03 : aggiunti ulteriori controlli sull'opzione NEED_RECONF. |
||
| + | ; 1.04 : migliorato il controllo sulla presenza degli script bridge-start/stop. |
||
= Feedback = |
= Feedback = |
||
| − | Se avete suggerimenti/segnalazioni, per favore fiondatevi a scrivere un post qui: http://www.slacky.eu/forum/viewtopic.php?f=12&t=23946&start=0&st=0&sk=t&sd=a |
+ | Se avete suggerimenti/segnalazioni, per favore fiondatevi a scrivere un post [http://www.slacky.eu/forum/viewtopic.php?f=12&t=23946&start=0&st=0&sk=t&sd=a qui]. |
| + | |||
| + | 414n |
||
Versione attuale delle 13:20, 10 apr 2008
Indice |
[modifica] Introduzione
Dietro suggerimento di conraid in questo thread del forum eccomi qui a scrivere la mia prima pagina wiki.:)
[modifica] Descrizione
Questo script avvia un server OpenVPN (già configurato) in modalità bridged. Per indicazioni su come configurare un server vpn in modalità bridged tramite OpenVPN vedere qui.
[modifica] Versione corrente
La versione corrente è la 1.04 del 10/04/2008.
[modifica] Requisiti
- GNU Bash
- per eseguire lo script.
- OpenVPN 2.0
- installato e già configurato come server di una connessione bridged.
- Script bridge-start / bridge-stop
- normalmente installati insieme a OpenVPN. Questi devono anche essere già configurati prima di lanciare lo script.
[modifica] Configurazione
Prima di usare lo script è necessario impostare alcune variabili presenti all'interno dello stesso. La loro funzione è spiegata, oltre che nell'elenco successivo, anche all'interno dei commenti nello script (in inglese).
- BRIDGE_SCRIPTS_PATH
- è la cartella contenente gli scripts bridge-start / bridge-stop. Normalmente, OpenVPN li installa in /usr/doc/openvpnx.y/sample-scripts, dove x.y indica la versione di OpenVPN.
- BRIDGE_START_CMD
- è il nome dello script per avviare il bridge. Di solito è "bridge-start", ma, nel caso fosse diverso, qui è possibile specificarlo.
- BRIDGE_STOP_CMD
- è il nome dello script per fermare il bridge. Solitamente è "bridge-stop".
- OPENVPN_PATH
- è il percorso dell'eseguibile openvpn. Può essere lasciato vuoto ("") se è già presente nel path (di root). Per verificarlo vedere il risultato del comando
which openvpn
In una installazione standard questo è /usr/sbin/openvpn. - OPENVPN_CFG_FILE
- è il percorso (assoluto) del file di configurazione di OpenVPN da usare. Se non specificato, viene assunto come file di configurazione /etc/openvpn/openvpn.conf.
- OPENVPN_OPTS
- permette di specificare eventuali opzioni aggiuntive da passare a OpenVPN. Non viene effettuato alcun controllo sulla loro validità (percui OCCHIO).
- NEED_RECONF
- è una variabile booleana usata per ripristinare la configurazione di rete originaria dopo il lancio degli script per avviare/fermare il bridge.
Se viene effettuato il bridging dell'interfaccia di rete che si usa per accedere ad internet, per esempio eth0, questi script fanno perdere al kernel la cognizione del default gateway e dell'indirizzo ip di eth0. Questo perchè il bridge appena avviato "maschera" dentro di sé l'interfaccia eth0 e quella utilizzata per il bridging sulla vpn (solitamente tap0). Il default gateway, che prima il kernel sapeva accessibile tramite l'interfaccia eth0, ora non lo è più perchè eth0 non è più su nessuna rete. Al suo posto è comparso il bridge (br0 se non diversamente configurato). È quindi necessario rispecificare il default gateway in modo che riappaia nella tabella di routing del kernel.
In pratica, dovrebbe essere necessario settare a TRUE la variabile NEED_RECONF solo nel caso sopra descritto, cioè di bridging dell'interfaccia di rete con accesso a internet. Se settata a TRUE, ogni qualvolta sarà necessario, il default gateway e/o l'indirizzo ip dell'interfaccia di rete usata per il bridging verrano ripristinati.
Qualsiasi altro valore diverso da TRUE è valido per disattivare questo "work-around". - GATEWAY_IP
- è l'indirizzo IP del gateway. Va specificato solo se NEED_RECONF è TRUE.
[modifica] Script
#!/bin/bash
# Script able to start/stop a configured OpenVPN daemon in bridged mode.
# Brought to you by 414n.
# 10/4/2008
# Version 1.04
# Global vars:
# This is the path to the start/stop bridge scripts that come with OpenVPN.
# The default path is /usr/doc/openvpnx.y/sample-scripts
# You must edit these scripts before launching OpenVPN.
BRIDGE_SCRIPTS_PATH="/etc/openvpn"
# These are the names of the two scripts to start/stop the bridge.
# You have to edit them accordingly to your settings.
BRIDGE_START_CMD="bridge-start"
BRIDGE_STOP_CMD="bridge-stop"
# This is the OpenVPN executable (complete) path.
# If openvpn is in your path you can leave this field empty.
OPENVPN_PATH=""
# Here you can specify the config file to be used.
# Default is /etc/openvpn/openvpn.conf if unspecified.
OPENVPN_CFG_FILE=/etc/openvpn/openvpn-test1.conf
# These are some other options you may want to pass to the openvpn executable.
# They are not checked.
OPENVPN_OPTS=""
# This tells wether we need to reconfigure the network interface after
# switching on/off the bridge.
# I think this is only needed when you bridge the network that has internet
# access. It's a pity that the bridge scripts only work with a physical
# network interface...
# Possible values: TRUE, * (anything else).
NEED_RECONF="TRUE"
# This is the gateway ip. If you specify NEED_RECONF=TRUE, the gateway will
# be defaulted to what you type here.
GATEWAY_IP="a.b.c.d"
# This function performs checks on the information you provided in this script.
initial_checks()
{
# Checking bridge-scripts presence.
if ! [ -x "$BRIDGE_SCRIPTS_PATH/$BRIDGE_START_CMD" ]
then
echo "Couldn\'t find $BRIDGE_START_CMD in $BRIDGE_SCRIPTS_PATH."
exit 1
fi
if ! [ -x "$BRIDGE_SCRIPTS_PATH/$BRIDGE_STOP_CMD" ]
then
echo "Couldn\'t find $BRIDGE_STOP_CMD in $BRIDGE_SCRIPTS_PATH."
exit 1
fi
# Checking openvpn executable & configuration file existance.
if [ "$OPENVPN_PATH" -a ! -x "$OPENVPN_PATH" ]
then
echo "Couldn\'t find openvpn executable in $OPENVPN_PATH."
exit 2
else
OPENVPN_PATH=`which openvpn`
if [ ` echo "$OPENVPN_PATH" | grep 'which'` ]
then
echo "Couldn\'t find openvpn in you PATH enviroment variable."
exit 3
fi
fi
if [ "$OPENVPN_CFG_FILE" ]
then
if ! [ -e "$OPENVPN_CFG_FILE" ]
then
echo "Couldn\'t find openvpn configuration file in $OPENVPN_CFG_FILE".
exit 3
fi
else
if [ -e "/etc/openvpn/openvpn.conf" ]
then
OPENVPN_CFG_FILE="/etc/openvpn/openvpn.conf"
else
echo "Couldn\'t find /etc/openvpn/openvpn.conf. Please select a proper configuration file."
exit 4
fi
fi
# Checking if openvpn is already configured to be run as a daemon.
# This test checks both the config file and the OPENVPN_OPTS variable.
ISDAEMON=`echo "$OPENVPN_OPTS" | grep '\-\-daemon'`
ISDAEMON+=`grep ^daemon$ "$OPENVPN_CFG_FILE"`
if [ -z "$ISDAEMON" ]
then
if [ "$OPENVPN_OPTS" ]
then
OPENVPN_OPTS+=" --daemon"
else
OPENVPN_OPTS='--daemon'
fi
fi
# Checking if user supplied a gateway IP (if NEED_RECONF=TRUE)
if [ "$NEED_RECONF" == TRUE -a -z "$GATEWAY_IP" ]
then
echo "You didn't supply a gateway ip to be reset. Exiting."
exit 9
fi
}
# This function reads information about the actual bridged network interface
# from the bridge-start script, in order to reconfigure it if needed.
getifacedata ()
{
IFACE=`cat "$BRIDGE_SCRIPTS_PATH/$BRIDGE_START_CMD" | grep 'eth=' | cut -d= -f2 | sed -e 's/"//g'`
IFACE_IP=`cat "$BRIDGE_SCRIPTS_PATH/$BRIDGE_START_CMD" | grep 'eth_ip=' | cut -d= -f2 | sed -e 's/"//g'`
IFACE_NETMASK=`cat "$BRIDGE_SCRIPTS_PATH/$BRIDGE_START_CMD" | grep 'eth_netmask=' | cut -d= -f2 | sed -e 's/"//g'`
}
# This function retrieves the openvpn daemon PID
getpid ()
{
OPENVPN_PID=`ps aux | awk '{ print $2,$11 }' | grep "$OPENVPN_PATH"$ | awk '{ print $1 }'`
}
#This function reconfigures the default gateway only
reconfgateway ()
{
route add default gw "$GATEWAY_IP"
}
# This function restores network configuration
reconfnet ()
{
getifacedata
ifconfig "$IFACE" "$IFACE_IP" netmask "$IFACE_NETMASK"
}
# This function starts the daemon
start ()
{
"$BRIDGE_SCRIPTS_PATH/$BRIDGE_START_CMD" 1>/dev/null
if [ $? -ne 0 ]
then
echo "Problem starting bridge. Please review bridge settings in "$BRIDGE_SCRIPTS_PATH/$BRIDGE_START_CMD". Exiting"
"$BRIDGE_SCRIPTS_PATH/$BRIDGE_STOP_CMD"
if [ "$NEED_RECONF" == "TRUE" ]
then
reconfnet
reconfgateway
fi
exit 5
fi
if [ "$OPENVPN_OPTS" ]
then
"$OPENVPN_PATH" $OPENVPN_OPTS --config "$OPENVPN_CFG_FILE"
else
"$OPENVPN_PATH" "$OPENVPN_CFG_FILE"
fi
if [ $? -ne 0 ]
then
echo "Something screwed up..."
"$BRIDGE_SCRIPTS_PATH/$BRIDGE_STOP_CMD"
if [ "$NEED_RECONF" == "TRUE" ]
then
reconfnet
reconfgateway
fi
exit 9
fi
if [ "$NEED_RECONF" == "TRUE" ]
then
reconfgateway
fi
}
# This function stops the daemon
# Arguments:
# $1 : OpenVPN process PID
stop ()
{
kill -9 "$1"
"$BRIDGE_SCRIPTS_PATH/$BRIDGE_STOP_CMD" 1>/dev/null
if [ $? -ne 0 ]
then
echo "Problem stopping bridge. Please review settings in $BRIDGE_SCRIPTS_PATH/$BRIDGE_STOP_CMD. Exiting"
exit 6
fi
if [ "$NEED_RECONF" == "TRUE" ]
then
reconfnet
reconfgateway
fi
}
initial_checks
case "$1" in
start)
getpid
if [ "$OPENVPN_PID" ]
then
echo "OpenVPN is already running (PID=$OPENVPN_PID)."
exit 7
else
if [ -z "`lsmod | grep tun`" ]
then
modprobe tun
fi
echo -n "Starting OpenVPN..."
start
echo "Done."
if ! [ "$ISDAEMON" ]
then
echo "Please add the \"daemon\" option to your config file ($OPENVPN_CFG_FILE) or to OPENVPN_OPTS."
fi
fi
;;
stop)
getpid
if [ "$OPENVPN_PID" ]
then
echo -n "Stopping OpenVPN..."
stop "$OPENVPN_PID"
if [ `lsmod | grep tun | awk '{ print $3 }'` == "0" ]
then
modprobe -r tun
fi
modprobe -r bridge
echo "Done."
else
echo "OpenVPN is not running."
exit 8
fi
;;
*)
echo "Usage: `basename $0` start/stop."
exit 1
;;
esac
[modifica] Installazione
- Avvio automatico al boot
- Copiare lo script in /etc/rc.d/rc.openvpn-br
- Rendere lo script eseguibile (chmod +x /etc/rc.d/rc.openvpn-br).
- Aggiungere una riga a /etc/rc.d/rc.local con all'interno:
- if [ -x /etc/rc.d/rc.openvpn-br ]
- then
- /etc/rc.d/rc.openvpn-br start
- fi
- Aggiungere una riga a /etc/rc.d/rc.local_shutdown:
- if [ -x /etc/rc.d/rc.openvpn-br ]
- then
- /etc/rc.d/rc.openvpn-br stop
- fi
- Avvio manuale
- In questo caso lo script può essere messo in una cartella a piacere. Consiglio comunque di copiarlo in /etc/rc.d.
- Lo script deve essere lanciato da root.
- Per eseguire lo script senza che venga reso eseguibile, usare
- sh /path/to/script/rc.openvpn-br [start|stop]
[modifica] Changelog
- 1.00
- versione iniziale dello script.
- 1.01
- aggiunto controllo sull'opzione "daemon". Se non viene specificata da nessuna parte (file di configurazione, OPENVPN_OPTS), viene aggiunta in automatico (ringrazio Zeros per avermi segnalato la lacuna).
- 1.02
- aggiunto controllo sulla presenza di GATEWAY_IP se NEED_RECONF è TRUE.
- 1.03
- aggiunti ulteriori controlli sull'opzione NEED_RECONF.
- 1.04
- migliorato il controllo sulla presenza degli script bridge-start/stop.
[modifica] Feedback
Se avete suggerimenti/segnalazioni, per favore fiondatevi a scrivere un post qui.
414n