Subversion Repositories ALCASAR

Rev

Rev 2983 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2956 rexy 1
#!/bin/bash
2
 
3
# alcasar-network.sh
4
# by Pierre RIVAULT and Rexy
5
# This script is distributed under the Gnu General Public License (GPL)
6
 
7
# Met à jour la configuration réseau conformément au fichier de configuration (alcasar.conf)
8
# update network configuration according to alcasar.conf
9
 
10
CONF_FILE="/usr/local/etc/alcasar.conf"
11
TMP_ip_gw_save="/tmp/ipset_ip_gw_save"
3285 rexy 12
DOMAIN=`grep ^DOMAIN= $CONF_FILE|cut -d"=" -f2`
2956 rexy 13
EXTIF=`grep ^EXTIF= $CONF_FILE|cut -d"=" -f2`
14
PUBLIC_IP=`grep ^PUBLIC_IP= $CONF_FILE|cut -d"=" -f2`
2983 rexy 15
if [ $PUBLIC_IP != "dhcp" ]; then
16
	GW1=`grep ^GW= $CONF_FILE|cut -d"=" -f2`
17
	MTU=`grep ^PUBLIC_MTU= $CONF_FILE|cut -d"=" -f2`
18
	MULTIWAN=`grep ^MULTIWAN= $CONF_FILE|cut -d"=" -f2`
19
	MULTIWAN=${MULTIWAN:=off}
20
	NET="`ipcalc -n $PUBLIC_IP | cut -d"=" -f2`/`ipcalc -p $PUBLIC_IP | cut -d"=" -f2`"
21
	IP=`echo $PUBLIC_IP | cut -d"/" -f1`
22
	PRIVATE_IP_MASK=`grep ^PRIVATE_IP $CONF_FILE | cut -d"=" -f2`
23
	PRIVATE_IP=`echo $PRIVATE_IP_MASK | cut -d"/" -f1`
24
	PRIVATE_NETMASK=`echo $PRIVATE_IP_MASK | cut -d"/" -f2`
25
	PRIVATE_PREFIX=`/bin/ipcalc -p $PRIVATE_IP $PRIVATE_NETMASK |cut -d"=" -f2`	# prefixe du réseau (ex. 24)
26
	PRIVATE_NETWORK=`/bin/ipcalc -n $PRIVATE_IP $PRIVATE_NETMASK| cut -d"=" -f2`	# @ réseau de consultation (ex.: 192.168.182.0)
27
	PRIVATE_NETWORK_MASK=$PRIVATE_NETWORK/$PRIVATE_PREFIX				# @ + masque du réseau de consult (192.168.182.0/24)
28
	nb_gw=`grep ^WAN $CONF_FILE | wc -l`
29
fi
2956 rexy 30
routecmd="ip route replace default scope global"
31
 
32
if [ $(whoami) != "root" ]; then
33
	echo "You must be root to run this!" ; echo ; exit 1
34
fi
35
 
36
if [ $# -eq 0 ]; then
37
	args="--apply"
38
else
39
	args=$1
40
fi
41
 
42
case $args in
2960 rexy 43
	--save)	# save all the IP before changing the configuration
2956 rexy 44
		rm -f $TMP_ip_gw_save
2960 rexy 45
		gw_list="gw0"	# ipset name list for load_balancing
2956 rexy 46
		for ((i=1 ; i<=$nb_gw ; i++)); do
47
			gw_list="${gw_list} gw$i"
48
		done
49
		# Saving all of the already connected IP in order to put them back in the load balancing after
50
		for i in $gw_list;do
51
			ipset list $i 1>/dev/null 2>&1
52
			if [ $? -eq 0 ]
53
			then
54
				# the cut -d":" -f5 deletes all the lines with a :, i.e all the lines except the members
2960 rexy 55
				ipset list $i | grep -v ":" >> $TMP_ip_gw_save
2956 rexy 56
			fi
57
		done
58
	exit 0
59
	;;
60
	--apply)
61
		[ -e /etc/sysconfig/network-scripts/ifcfg-$EXTIF ] && ifdown $EXTIF
62
		# set the new configuration for EXTIF
2983 rexy 63
		if [ $PUBLIC_IP == "dhcp" ]; then
64
			cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$EXTIF
2956 rexy 65
DEVICE=$EXTIF
2983 rexy 66
BOOTPROTO=dhcp
67
DNS1=127.0.0.1
68
PEERDNS=no
69
RESOLV_MODS=yes
70
ONBOOT=yes
71
NOZEROCONF=yes
72
METRIC=10
73
MII_NOT_SUPPORTED=yes
74
IPV6INIT=no
75
IPV6TO4INIT=no
76
ACCOUNTING=no
77
USERCTL=no
78
MTU=$MTU
79
EOF
80
			ifup $EXTIF
81
		else
82
			cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$EXTIF
83
DEVICE=$EXTIF
2956 rexy 84
BOOTPROTO=static
85
IPADDR=$IP
86
NETMASK=`ipcalc -m $PUBLIC_IP | cut -d= -f2`
87
NETWORK=`ipcalc -n $PUBLIC_IP | cut -d= -f2`
88
GATEWAY=$GW1
89
DNS1=127.0.0.1
90
RESOLV_MODS=yes
91
ONBOOT=yes
92
METRIC=10
93
MII_NOT_SUPPORTED=yes
94
IPV6INIT=no
95
IPV6TO4INIT=no
96
ACCOUNTING=no
97
USERCTL=no
98
MTU=$MTU
99
NOZEROCONF=yes
100
EOF
2983 rexy 101
			ifup $EXTIF
102
			ip route flush ${NET}	# Remove the previous route for the network of EXTIF
103
			ip route delete default scope global	# Remove the previous default route
104
			ip route add ${NET} dev ${EXTIF} src ${IP}	 # Set the new route for EXTIF network
105
			ip route add ${NET} dev ${EXTIF} src ${IP} table 200	# Set the new default route. If no multiwan, these lines are equivalent to `ip route add default via ${GW1}`
106
			ip route add default via ${GW1} table 200
107
			routecmd="${routecmd} nexthop via ${GW1} dev ${EXTIF}"
108
			ip rule flush	# Remove the previous routing rules
109
			ip rule add from all lookup main pref 32766	# Set back the main rules
110
			ip rule add from all lookup default pref 32767	# Set back the default rules
111
			ip rule add from ${PRIVATE_NETWORK_MASK} fwmark 200 lookup 200	# Add the rule for the first gateway
112
			if [ "$MULTIWAN" == "on" ] || [ "$MULTIWAN" == "On" ]; then
113
				nb_gw_supp=`grep ^WAN $CONF_FILE|wc -l`
114
				for ((i=0 ; $i < $nb_gw_supp ; i++)); do
115
					table=$(($i + 201))	# This number is used to mark the paquets in order to route them to the choosen GW 
116
					GW=`grep ^WAN$(($i + 1))= $CONF_FILE|awk -F'"' '{ print $2 }' | awk -F, '{print $1}'`
117
					ip route add ${NET} dev ${EXTIF} src ${IP} table $table	# Add the others route in their respective tables
118
					ip route add default via ${GW} table $table
119
					ip rule add from ${PRIVATE_NETWORK_MASK} fwmark $table lookup $table	# Add the rule for each rule depending of the mark set by the firewall
120
					routecmd="${routecmd} nexthop via ${GW} dev ${EXTIF}"	# add the added gateway into the default gateway
121
				done
122
			fi
123
			${routecmd}	# define the default gateway for outgoing traffic
124
			ip route flush cache
2956 rexy 125
		fi
2983 rexy 126
		/usr/local/bin/alcasar-iptables.sh
3285 rexy 127
		# write resolv.conf
128
		cat <<EOF > /etc/resolv.conf
129
# Basic resolv.conf : ALCASAR ask its local DNS servers (unbound)
130
search $DOMAIN
131
nameserver 127.0.0.1
132
EOF
2956 rexy 133
		exit 0
134
		;;
135
	*)
136
		exit 1
137
		;;
138
esac