Update copyrights.
[oweals/tinc.git] / redhat / tinc
index aa404b34cac9b62c4834ef994553a64ed3c246a1..9fce417433cccf9aab500f7e90a6091121e7ea43 100644 (file)
@@ -2,15 +2,15 @@
 #
 # tinc         tincd VPN setup script
 #
-# chkconfig:   2345 15 85
+# chkconfig:   2345 46 54
 #
-# author:      Lubomir Bulej <pallas@kadan.cz>
-#               Modified for RPM by Mads Kiilerich <mads@kiilerich.com>
-# version:     1.0.3
+# version:     1.0.8
+# authors:     Lubomir Bulej <pallas@kadan.cz>
+#               Mads Kiilerich <mads@kiilerich.com>
 #
-# description: this script takes care of starting and setting up of VPNs \
-#              provided by tincd daemon. It parses the configuration files \
-#              in all VPN directories and sets up the interfaces and routing
+# description: This script parses tinc configuration files for networks given \
+#              in /etc/tinc/nets.boot and for each of the networks it sets up \
+#              the interface and static routes and starts the tinc daemon.
 #
 # processname: tincd
 
 [ ${NETWORKING} = "no" ] && exit 0
 
 #############################################################################
-# configuration
+# configuration & sanity checks
 
 TINCD=/usr/sbin/tincd
 TCONF=/etc/tinc
 TPIDS=/var/run
-#DEBUG_OPT=-dddd
+#DEBUG=-dddd
 
-# Check if ip-route is installed
-if [ ! -f /sbin/ip ]; then
-    echo "**tinc: ip-route utilities not installed!"
+NETSFILE=$TCONF/nets.boot
+
+# Check the daemon
+if [ ! -x $TINCD ]; then
+    echo "**tinc: $TINCD does not exist or is not executable!" >&2
     exit
 fi
-                
 
-##############################################################################
-# vpn_load ()          Loads VPN configuration
-# 
-# $1 ... VPN to load
+# Check the configuration directory
+if [ ! -d $TCONF ]; then
+    echo "**tinc: configuration directory ($TCONF) not found!" >&2
+    exit
+fi
 
-vpn_load () {
-    CFG="$TCONF/$1/tincd.conf"
-    [ -f $CFG ] || { echo "Error: $CFG does not exist" >&2 ; return 1 }
-    
-    # load TINCD config
-    DEV=`grep -i -e '^TapDevice' $CFG | sed 's/[[:space:]]//g;s/^.*=//g'`
-    VPN=`grep -i -e '^(MyOwnVPNIP|MyVirtualIP)' -E $CFG | head -1 | sed 's/[[:space:]]//g;s/^.*=//g'`
-    
-    # discourage empty and multiple entries
-    [ -z "$DEV" ] && \
-               { echo "Error: TapDevice needed" >&2 ; return 2 }
-    echo $DEV | grep -q '^/dev/tap' ||
-               { echo "Error: TapDevice needs /dev/tapX" >&2 ; return 2 }
-    [ `echo $DEV | wc -l` -gt 1 ] && \
-               { echo "Error in TapDevice" >&2 ; return 3 }
-    [ -z "$VPN" ] && \
-               { echo "Error: MyOwnVPNIP/MyVirtualIP needed" >&2 ; return 2 }
-    [ `echo $VPN | wc -l` -gt 1 ] && \
-               { echo "Error in MyOwnVPNIP/MyVirtualIP" >&2 ; return 3 }
-    echo $VPN | grep -q -x  \
-       '\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}/[[:digit:]]\{1,2\}' ||
-               { echo "Error in MyOwnVPNIP/MyVirtualIP address $VPN" ; 
-                       return 3 }
-    
-    # network device
-    TAP=`echo $DEV | cut -d"/" -f3`
-    NUM=`echo $TAP | sed 's/tap//'`
-
-    # IP address, netmask length
-    ADR=`echo $VPN | cut -d"/" -f1`
-    LEN=`echo $VPN | cut -d"/" -f2`
-
-    # Expand bitlength to netmask    
-    MSK=""; len=$LEN
-    for cnt in 1 1 1 0; do
-       if [ $len -ge 8 ]; then
-           msk=8
-       else
-           msk=$len
-       fi
-           
-       MSK="$MSK$((255 & (255 << (8 - msk))))"
-       [ $cnt -ne 0 ] && MSK="$MSK."
-       len=$((len-msk))
-    done
-
-    # Network & broadcast
-    BRD=`ipcalc --broadcast $ADR $MSK | cut -d"=" -f2`
-    NET=`ipcalc --network $ADR $MSK | cut -d"=" -f2`
-        
-    # MAC address
-    MAC=`printf "fe:fd:%0.2x:%0.2x:%0.2x:%0.2x" $(echo $ADR | sed 's/\./ /g')`
-    # echo "TAP $TAP NUM $NUM ADR $ADR LEN $LEN MSK $MSK BRD $BRD NET $NET MAC $MAC" >&2
-    return 0
-}
+# Check nets.boot
+if [ ! -f $NETSFILE ]; then
+    echo "**tinc: file with list of VPNs to start ($NETSFILE) not found!" >&2
+    exit
+fi
+
+# Load names of networks to be started
+NETS="$(sed -e 's/#.*//; s/[[:space:]]//g; /^$/ d' $NETSFILE)"
 
 
 ##############################################################################
@@ -106,36 +61,9 @@ vpn_load () {
 # $1 ... VPN to start
 
 vpn_start () {    
-
-    vpn_load $1 || { echo "Error: Could not vpn_load $1" >&2 ; return 1 }
-            
-    # create device file
-    if [ ! -c $DEV ]; then
-       [ -e $DEV ] && rm -f $DEV
-       mknod --mode=0600 $DEV c 36 $((16 + NUM))
-    fi
-    
-    # load device module
-    { insmod ethertap --name="ethertap$NUM" unit="$NUM" 2>&1  || \
-               { echo "Error: cannot insmod ethertap$NUM" >&2 ; return 2 }
-    } | grep -v '^Us'
-    
-    # configure the interface
-    ip link set $TAP address $MAC #&> /dev/null
-    ip link set $TAP up #&> /dev/null
-    ip addr flush dev $TAP 2>&1 | grep -v -x '^Nothing to flush.' #&> /dev/null
-    ip addr add $VPN brd $BRD dev $TAP #&> /dev/null
-    
     # start tincd
-    $TINCD --net="$1" $DEBUG_OPT || { echo "Error: Cannot start $TINCD" >&2; 
-               return 3 }
-
-    # default interface route
-    ip route add $NET/$LEN dev $TAP #&> /dev/null
-
-    # setup routes
-    /etc/sysconfig/network-scripts/ifup-routes $TAP
-
+    $TINCD --net="$1" $DEBUG || \
+       { MSG="could not start daemon for network $1"; return 3; }
     return 0
 } # vpn_start
 
@@ -146,14 +74,8 @@ vpn_start () {
 # $1 ... VPN to stop
 
 vpn_stop () {
-
-    vpn_load $1 || return 1
-    
-    # flush the routing table
-    ip route flush dev $TAP &> /dev/null
-    
     # kill the tincd daemon
-    PID="$TPIDS/tincd.$1.pid"
+    PID="$TPIDS/tinc.$1.pid"
     if [ -f $PID ]; then
         $TINCD --net="$1" --kill &> /dev/null
         RET=$?
@@ -162,56 +84,68 @@ vpn_stop () {
            dly=0
            while [ $dly -le 5 ]; do
                [ -f $PID ] || break
-               sleep 1; dly=$((dly+1))
+               sleep 1; dly=$((dly + 1))
            done
-       else
-           rm -f $PID &> /dev/null
        fi
+       
+       # remove stale PID file
+       [ -f $PID ] && rm -f $PID
     fi
-    
-    # bring the interface down
-    ip link set $TAP down &> /dev/null
-    
-    # remove kernel module
-    rmmod "ethertap$NUM" &> /dev/null
-    
     return 0
 } # vpn_stop
 
 
+# Check if there is anything to start
+if [ ! -z "$1" -a "$1" != "status" -a -z "$NETS" ]; then
+    echo "**tinc: no networks found in $NETSFILE!" >&2
+    exit
+fi
+
+
 # See how we were called.
 case "$1" in
     start)
-        echo -n "Bringing up VPNs: "
-       for vpn in `ls -1 $TCONF`; do
-           vpn_start $vpn && echo -n "$vpn "
+       for vpn in $NETS; do
+            echo -n "Bringing up TINC network $vpn: "
+           vpn_start $vpn && \
+               success "startup of network $vpn" || \
+               failure "startup of network $vpn"
+           echo
+           
+           if [ ! -z "$MSG" ]; then
+               [ ! -z "$ERR" ] && echo "$ERR" >&2
+               echo "**tinc: $MSG" >&2
+           fi
        done
        
-       touch /var/lock/subsys/tinc
-        action "" /bin/true
+        touch /var/lock/subsys/tinc
         ;;
        
     stop)
-        echo -n "Shutting down VPNs: "
-       for vpn in `ls -1 $TCONF`; do
-           vpn_stop $vpn && echo -n "$vpn "
+       for vpn in $NETS; do
+           echo -n "Shutting down TINC network $vpn: "
+           vpn_stop $vpn && \
+               success "shutdown of network $vpn" || \
+               failure "shutdown of network $vpn"
+           echo
+           
+           if [ ! -z "$MSG" ]; then
+               [ ! -z "$ERR" ] && echo "$ERR" >&2
+               echo "**tinc: $MSG" >&2
+           fi
        done
        
-       rm -f /var/lock/susbsys/tinc
-        action "" /bin/true
+       rm -f /var/lock/subsys/tinc
        ;;
        
     status)
-       echo -n "Currently running VPNs: "
-       for vpn in `ls -1 $TCONF`; do
-           PID="$TPIDS/tincd.$vpn.pid"
-           echo -n "$vpn "
-           if [ -f $PID -a `ps ax | grep "^ *$(cat $PID)" | wc -l` -eq 1 ] 
-           then
-               echo -n "OK "
-           else
-               echo -n "Dead "
-           fi
+       echo -n "Configured VPNs: "
+       for vpn in $NETS; do
+           PID="$TPIDS/tinc.$vpn.pid"
+           
+           [ -f $PID ] && PID="$(cat $PID)" || PID="-dead-"
+           ps ax | grep "^[[:space:]]*$PID" && STS="OK" || STS="DEAD"
+           echo -n "$vpn:$STS "
        done
        echo             
        ;;