sedlo |
Subversion Repositories: |
Rev 3 | Rev 4 | |
---|---|---|
Line 1... | Line 1... | |
#!/bin/bash |
#!/bin/bash | |
# author : Petr Simandl www.simandl.cz |
# author : Petr Simandl www.simandl.cz | |
# release date : 29/08/2004 |
# release date : 07/09/2004 | |
# name : sedlo |
# name : sedlo | |
# description : dynamic side routing tables tool |
# description : dynamic side routing tables tool | |
# license : GPL |
# license : GPL | |
|
| |
sl_version="0.0.2" |
sl_version="0.0.3pre1-wsh-4" | |
|
| |
PATH=$PATH:$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:/usr/local/sbin |
PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin | |
|
| |
sl_nmcnf="sedlo.conf" |
sl_nmcnf="sedlo.conf" | |
sl_sedlocnf="/etc/$sl_nmcnf" |
sl_sedlocnf="/etc/$sl_nmcnf" | |
sl_sedlocache="/var/cache/sedlo" |
sl_sedlocache="/var/cache/sedlo" | |
|
| |
sl_rttab="/etc/iproute2/rt_tables" |
sl_rttab="/etc/iproute2/rt_tables" | |
sl_rtnmin=110 |
sl_rtnmin=110 | |
sl_rtnmax=200 |
sl_rtnmax=200 | |
| ||
# sl_rule_prio_min - The lowest priority for rules created by | ||
# sedlo. Each configured user has one or more dynamic rules for | ||
# internet routing as specified in the config file. | ||
| ||
sl_rule_prio_min=2000 | ||
| ||
sl_ipnodef="10.0.0.0/8" |
sl_ipnodef="10.0.0.0/8" | |
|
| |
slm_unknown="Nezname parametry : " |
slm_unknown="Nezname parametry : " | |
|
| |
sl_ipcmd=`which ip` |
sl_ipcmd=`which ip` | |
Line 26... | Line 33... | |
sl_hnmcmd=`which hostname` |
sl_hnmcmd=`which hostname` | |
sl_awkcmd=`which awk` |
sl_awkcmd=`which awk` | |
sl_catcmd=`which cat` |
sl_catcmd=`which cat` | |
sl_grepcmd=`which grep` |
sl_grepcmd=`which grep` | |
|
| |
if [ -e $sl_sedlocnf ] |
if [ -r $sl_sedlocnf ] | |
then |
then | |
sl_nop=1 |
sl_nop=1 | |
else |
else | |
echo "$sl_sedlocnf not found" |
echo "$sl_sedlocnf not found or is not readable" | |
exit 1 |
exit 1 | |
fi |
fi | |
|
| |
if [ -e $sl_rttab ] |
if [ -w $sl_rttab ] | |
then |
then | |
sl_nop=1 |
sl_nop=1 | |
else |
else | |
echo "$sl_rttab not found" |
echo "Can't write to $sl_rttab" | |
exit 1 |
exit 1 | |
fi |
fi | |
|
| |
sl_murlcfg=`cat $sl_sedlocnf | grep mcnf | uniq | awk '{print $2}'` |
# Read local configuration file | |
sl_local_conf_murlcfg=`cat $sl_sedlocnf | grep "^mcnf" | head -n 1 | awk '{print $2}'` | ||
sl_local_conf_blackhole=`cat $sl_sedlocnf | grep "^blackhole" | head -n 1 | awk '{print $2}'` | ||
sl_local_conf_me2igw=`awk '/^me2igw/ { print $2 "*" $3 "*" $4 }' < $sl_sedlocnf` | ||
sl_local_conf_myigws=`awk '/^myigw/ { print $2; }' < $sl_sedlocnf` | ||
|
| |
###################################################################### | ||
s_flru() | ||
{ | ||
# Existing rules which were created by sedlo (priority | ||
# sl_rule_prio_min and above) are deleted. All other rules are | ||
# preserved. | ||
| ||
[ $scm_info -gt 0 ] && echo "Flushing all rules created by sedlo" | ||
ip rule ls \ | ||
| awk -F ':' -v PRIO=$sl_rule_prio_min '{ if ($1 == PRIO) { print $2, " prio ", PRIO; PRIO=PRIO+1;} }' \ | ||
| sed -e 's!all!0/0!' \ | ||
| while read RULE; do | ||
[ $scm_info -gt 1 ] && echo -n '#' | ||
ip rule del $RULE; | ||
done | ||
| ||
[ $scm_info -gt 1 ] && echo # new line after ###s | ||
| ||
} # s_flru | ||
###################################################################### |
###################################################################### | |
s_fillrules() |
s_fillrules() | |
{ |
{ | |
s_flru # Flush all rules | ||
| ||
if [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi |
if [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi | |
|
| |
sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'` |
sl_rule_prio=$sl_rule_prio_min # rules should have unique prio - see the iproute manual | |
|
| |
for sl_ip in $sl_ips |
# Insert rule for internal traffic | |
ip ru add from 0/0 to $sl_ipnodef table main prio $sl_rule_prio | ||
sl_rule_prio=$((sl_rule_prio + 1)) | ||
| ||
# Prepare rules for later processing. Delete comments and format them | ||
# for better parsing. | ||
| ||
sl_ips=`awk '/^ip/ {gsub("#.*$", ""); print $2"*"$4"*"$5"*"$6}' < $sl_sedlocache/$sl_nmcnf` | ||
| ||
# $sl_ips format: ip_range*igw1*igw2*igw3 | ||
| ||
default_rule="" | ||
default_igws=`awk '/^default/ { print $2"*"$3"*"$4; }' < $sl_sedlocache/$sl_nmcnf` | ||
[ "x$default_igws" != "x" ] && default_rule="DEFAULT*$default_igws" | ||
| ||
# $default_rule format: DEFAULT*igw1name*igw2name*igw3name | ||
| ||
this_router="ME2IGW*$sl_local_conf_me2igw" | ||
| ||
# $this_router format: THIS_ROUTER*igw1name*igw2name*igw3name | ||
| ||
for sl_ip in $this_router $sl_ips $default_rule | ||
do |
do | |
sl_ipn=`echo $sl_ip | $sl_awkcmd -F '*' '{print $1}'` |
sl_ipn=`echo $sl_ip | awk -F '*' '{print $1}'` # Client IP | |
sl_ipgws=`echo $sl_ip | $sl_awkcmd -F '*' '{print $2,$3,$4}'` |
sl_ipgws=`echo $sl_ip | awk -F '*' '{print $2,$3,$4}'` # iGW names | |
sl_ok="no" |
sl_attempt=1 # Which iGW is assigned (1st, 2nd or 3rd) | |
sl_rule_ok=no # whether there is a functional iGW for this rule | ||
for sl_ipgw in $sl_ipgws |
for sl_ipgw in $sl_ipgws | |
do |
do | |
sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw` |
sl_tbl=`ip ro ls ta $sl_ipgw` | |
if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ] |
if [ "$sl_tbl x" != " x" ] | |
then |
then | |
sl_empty="no" |
[ $scm_info -gt 1 ] && echo "Creating new rules to send $sl_ipn to table $sl_ipgw (attempt $sl_attempt)"; | |
sl_cnt=3 |
case "$sl_ipn" in | |
#removing until we have some rules in table but the maximum of cycles is limited |
"DEFAULT") | |
#to avoid neverending loop |
ip ru add table $sl_ipgw prio $sl_rule_prio ;; | |
while [ "$sl_empty" = "no" ] |
"ME2IGW") | |
do |
ip ru add iif lo table $sl_ipgw prio $sl_rule_prio ;; | |
sl_ipru=`$sl_ipcmd ru ls | $sl_grepcmd $sl_ipn` |
*) | |
if [ "$sl_ipru x" != " x" ] |
ip ru add from $sl_ipn table $sl_ipgw prio $sl_rule_prio ;; | |
then |
esac | |
if [ $scm_info -gt 1 ]; then echo "Removing old rules for $sl_ipn" ; fi |
sl_rule_prio=$((sl_rule_prio + 1)) | |
$sl_ipcmd ru del from $sl_ipn |
sl_rule_ok=yes | |
$sl_ipcmd ru del from $sl_ipn to $sl_ipnodef |
break | |
else |
||
sl_empty="yes" |
||
fi |
||
sl_cnt=$(($sl_cnt - 1 )) |
||
if [ "$sl_cnt" -eq -1 ] |
||
then |
||
sl_empty="yes" |
||
echo "Too much cycles" |
||
fi |
||
done |
||
if [ $scm_info -gt 1 ]; then echo "Creating new rules to send $sl_ipn to table $sl_ipgw" ; fi |
||
$sl_ipcmd ru add from $sl_ipn lookup $sl_ipgw |
||
$sl_ipcmd ru add from $sl_ipn to $sl_ipnodef lookup main |
||
sl_ok="yes" |
||
else |
else | |
if [ "$sl_ok" = "no" ] |
sl_attempt=$((sl_attempt + 1)) | |
then |
||
if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it is empty" ; fi |
||
else |
||
if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it has lower priority" ; fi |
||
fi |
||
fi |
fi | |
done |
done | |
if [ "x$sl_rule_ok" = "xno" ]; then | ||
case "$sl_ipn" in | ||
"DEFAULT") | ||
# Despite of there is no default iGW accessible, create | ||
# a rule for a default iGW. This prevents Internet | ||
# packets to enter the main table. | ||
| ||
first_igw=`echo $sl_ip|awk -F '*' '{ print $2 }'` | ||
[ $scm_info -gt 1 ] && echo "Creating new rule to send $sl_ipn to table $first_igw (the last attempt)"; | ||
ip ru add table $first_igw prio $sl_rule_prio | ||
sl_rule_prio=$((sl_rule_prio + 1)) | ||
;; | ||
esac | ||
fi | ||
| ||
done |
done | |
| ||
# Flushing the cache is necessary in order to new rules become | ||
# efective. | ||
| ||
ip route flush cache | ||
|
| |
} # s_fillrules |
} # s_fillrules | |
###################################################################### |
###################################################################### | |
s_filltables() |
s_filltables() | |
{ |
{ | |
if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi |
if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi | |
|
| |
sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3"*"$2}'` |
sl_igws=`awk '/^igw/ {print $3"*"$2}' < $sl_sedlocache/$sl_nmcnf` | |
# $sl_igws format: "name*ip_addr" for each iGW | ||
|
| |
for sl_igw in $sl_igws |
for sl_igw in $sl_igws | |
do |
do | |
sl_igwn=`echo $sl_igw | $sl_awkcmd -F '*' '{print $1}'` |
# iGW name | |
sl_igwip=`echo $sl_igw | $sl_awkcmd -F '*' '{print $2}'` |
sl_igwn=`echo $sl_igw | awk -F '*' '{print $1}'` | |
sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd "^$sl_igwip " | $sl_awkcmd '{print $3}'` |
if echo $sl_local_conf_myigws|grep --word-regexp --fixed-strings --quiet $sl_igwn; then | |
if [ "$sl_igwgt x" = " x" ] |
| |
then |
# This iGW name is listed in local config as myigw. Setup | |
if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table empty" ; fi |
# fixed route table. | |
sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn` |
| |
if [ "$sl_tbl x" != " x" ] |
myigw_ip=`awk "/^myigw[[:space:]]+$sl_igwn/ { print \\$3; }" < $sl_sedlocnf` | |
then |
ip ro replace default via $myigw_ip table $sl_igwn | |
$sl_ipcmd ro fl ta $sl_igwn |
else | |
fi |
| |
else |
# Normal iGW - create a default route based on the main route | |
sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn` |
# table state. | |
if [ "$sl_tbl x" != " x" ] |
| |
then |
# iGW IP with slash is escaped with a backslash 10.51.0.0/16 -> 10.21.0.0\/16 | |
$sl_ipcmd ro fl ta $sl_igwn |
sl_igwip=`echo $sl_igw | awk -F '*' '{gsub( "/", "\\\\/", $2); print $2}'` | |
fi |
# Where to send packets for this iGW (can be "via <ip-addres>" or "dev <dev-name>") | |
$sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn |
sl_igwgt=`ip ro ls | awk "/^$sl_igwip/ {print \\$2, \\$3}"` | |
$sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn |
| |
if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi |
# TODO: Try to find IGW IP in a IP subnet. It allows an iGW to | |
# publish their settings, becouse it will be the same | ||
# regardless # of router's cloud. | ||
| ||
sl_tbl=`ip ro ls ta $sl_igwn` | ||
if [ "$sl_igwgt x" = " x" ] # route to igw not found | ||
then | ||
if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table empty" ; fi | ||
if [ "$sl_tbl x" != " x" ] # if table is not empty, flush it | ||
then | ||
ip ro fl ta $sl_igwn | ||
fi | ||
else | ||
sl_no_default_routes_count=`ip ro ls ta $sl_igwn|grep -c -v '^default'` | ||
if [ $sl_no_default_routes_count -gt 0 ] # if the table contains other than default routes, flush it | ||
then | ||
ip ro fl ta $sl_igwn | ||
fi | ||
ip ro replace default $sl_igwgt table $sl_igwn | ||
if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi | ||
fi | ||
sl_tbl_new=`ip ro ls ta $sl_igwn` | ||
| ||
# If an iGW state changes from reachable to unreachable or | ||
# vice versa, rules needs to be rebuilt. | ||
| ||
if [ "x$sl_tbl" = "x" -a "x$sl_tbl_new" != "x" -o \ | ||
"x$sl_tbl" != "x" -a "x$sl_tbl_new" = "x" ]; then | ||
sl_rules_update_needed=yes | ||
fi | ||
fi |
fi | |
done |
done | |
|
| |
} # s_filltables |
} # s_filltables | |
###################################################################### |
###################################################################### | |
s_mktables() |
s_mktables() | |
{ |
{ | |
if [ $scm_info -gt 0 ]; then echo "Creating tables " ; fi |
if [ $scm_info -gt 0 ]; then echo "Creating tables " ; fi | |
sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3}'` |
sl_igws=`awk '/^igw/ {print $3}' < $sl_sedlocache/$sl_nmcnf` | |
sl_cnt="$sl_rtnmax" | ||
for sl_igw in $sl_igws |
for sl_igw in $sl_igws | |
do |
do | |
sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw ` |
sl_igwrttb=`awk "/$sl_igw/ {print \\$2}" < $sl_rttab` | |
if [ "$sl_igwrttb x" = " x" ] |
if [ "$sl_igwrttb x" = " x" ] # Table is not present in rttab | |
then |
then | |
if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi |
[ $scm_info -gt 1 ] && echo "Creating table for $sl_igw" | |
sl_cnt="$sl_rtnmax" |
||
sl_ok="no" |
sl_ok="no" | |
until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ] |
until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ] | |
do |
do | |
#space is used to recognized two and three digit numbers |
sl_igwrttb=`awk "/^[^#]*$sl_cnt/ {print \\$1}" < $sl_rttab` | |
sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " ` |
if [ "$sl_igwrttb x" = " x" ] # This number is free | |
if [ "$sl_igwrttb x" = " x" ] |
||
then |
then | |
sl_ok="yes" |
sl_ok="yes" | |
echo "$sl_cnt $sl_igw" >> $sl_rttab |
echo "$sl_cnt $sl_igw" >> $sl_rttab | |
fi |
fi | |
sl_cnt=$(($sl_cnt - 1 )) |
sl_cnt=$(($sl_cnt - 1 )) | |
done |
done | |
else |
else | |
if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi |
[ $scm_info -gt 1 ] && echo "Table found for $sl_igw no action taken" | |
fi |
fi | |
|
| |
done |
done | |
} # s_mktables |
} # s_mktables | |
###################################################################### |
###################################################################### | |
s_getcfg() |
s_getcfg() | |
{ |
{ | |
if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi |
if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi | |
if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi |
if [ $scm_info -gt 1 ]; then echo "Using main config $sl_local_conf_murlcfg" ; fi | |
if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi |
if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi | |
wget $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main" -q |
| |
if [ -s $sl_sedlocache/$sl_nmcnf.main ] |
wget --timeout=10 --tries 1 $sl_local_conf_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp" -q | |
| ||
if [ -s $sl_sedlocache/$sl_nmcnf.main.tmp ] | ||
then |
then | |
sl_nop=1 |
date > $sl_sedlocache/last_getcnf.txt | |
else |
cp $sl_sedlocache/$sl_nmcnf.main.tmp $sl_sedlocache/$sl_nmcnf.main | |
if [ $scm_info -gt 1 ]; then echo "Main config not found $sl_sedlocache/$sl_nmcnf.main" ; fi |
if [ $scm_info -gt 1 ]; then echo "Main config accepted" ; fi | |
else | ||
| ||
# Test if there is an old cached config file. This happens when | ||
# wget is not installed when run first. | ||
| ||
if [ ! -r $sl_sedlocache/$sl_nmcnf.main ]; then | ||
echo "No cached main config file found ($sl_sedlocache/${sl_nmcnf}.main)" | ||
exit 1; | ||
fi | ||
| ||
if [ $scm_info -gt 1 ]; then echo "Main config not accepted - using cached config" ; fi | ||
echo -n "Main config not found " > $sl_sedlocache/last_getcnf.txt || exit 1 | ||
date >> $sl_sedlocache/last_getcnf.txt | ||
fi |
fi | |
echo "# generated file" > $sl_sedlocache/$sl_nmcnf |
||
for sl_file in `ls $sl_sedlocnf $sl_sedlocache/$sl_nmcnf.main ` |
||
do |
||
cat $sl_file | grep "^mcnf" | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf |
||
cat $sl_file | grep "^igw" | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf |
||
cat $sl_file | grep "^ip" | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf |
||
done |
||
cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq |
||
mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf |
||
|
| |
# Preparing cached config from local and main one. The ^ip lines form | ||
# local config should have higher priority than the same lines in the | ||
# main config. For ^igw lines, the oposite is true. The default iGW | ||
# (first iGW in the config) should be the same for whole cloud. | ||
| ||
echo "# generated file" > $sl_sedlocache/$sl_nmcnf || exit 1 | ||
sl_conf_main_first=`ls $sl_sedlocache/$sl_nmcnf.main $sl_sedlocnf` | ||
sl_conf_local_first=`ls $sl_sedlocnf $sl_sedlocache/$sl_nmcnf.main` | ||
| ||
#cat $sl_file | grep "^mcnf" | tr ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf || exit | ||
grep --no-filename --extended-regexp "^(igw|default)" $sl_conf_main_first| tr ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf || exit 1 | ||
grep --no-filename "^ip" $sl_conf_local_first| tr ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf || exit 1 | ||
| ||
#cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq | ||
#mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf | ||
} |
} | |
###################################################################### |
###################################################################### | |
s_version() |
s_version() | |
{ |
{ | |
echo sedlo $sl_version |
echo sedlo $sl_version | |
exit 0 |
||
} # s_version |
} # s_version | |
###################################################################### |
###################################################################### | |
s_report() |
s_report() | |
{ |
{ | |
echo "##### SEDLO #####" |
echo "##### SEDLO #####" | |
echo "date : `date`" | ||
echo "version : $sl_version" |
echo "version : $sl_version" | |
echo "local_config : $sl_sedlocnf" |
echo "local_config : $sl_sedlocnf" | |
echo "main_config : $sl_murlcfg" |
echo "main_config : $sl_local_conf_murlcfg" | |
echo "last update : `cat $sl_sedlocache/last_getcnf.txt`" | ||
echo "##### TABLES #####" |
echo "##### TABLES #####" | |
cat $sl_rttab |
tables=`awk "/^[^#]/ { if (\\\$1 >= $sl_rtnmin && \\\$1 <= $sl_rtnmax) print \\\$2; }" < $sl_rttab` | |
for table in $tables; do | ||
echo "${table}:" | ||
ip ro ls table ${table}|awk '{ print " " $0; }' | ||
done | ||
echo "##### RULES #####" |
echo "##### RULES #####" | |
$sl_ipcmd ru ls |
ip ru ls | |
exit 0 |
||
} # s_report |
} # s_report | |
###################################################################### | ||
sl_lock_filename="/tmp/sedlo.lock" | ||
sl_tmp_lock_filename="/tmp/sedlo.$$" | ||
| ||
sl_try_lock() | ||
{ | ||
echo $$ > $sl_tmp_lock_filename 2>/dev/null || { | ||
echo >&2 "You don't have permission to access `dirname $TEMPFILE`" | ||
return 1 | ||
} | ||
ln $sl_tmp_lock_filename $sl_lock_filename 2>/dev/null && { | ||
rm -f $sl_tmp_lock_filename | ||
return 0 | ||
} | ||
rm -f $sl_tmp_lock_filename | ||
return 1 | ||
} | ||
| ||
# sl_invalid_lock: returns 0 (success) if the lock file doesn't | ||
# contain valid PID of sedlo process. | ||
| ||
sl_invalid_lock() | ||
{ | ||
local pid | ||
local proc_name | ||
[ -f $sl_lock_filename ] || return 1 # lock doesn't exist so it | ||
# can't be invalid | ||
pid=$(< $sl_lock_filename) | ||
proc_name=$(ps --pid $pid -o comm --no-headers) | ||
if [ "$proc_name" = "sedlo" ]; then return 1 # the lock is valid | ||
else return 0 # the lock is invalid | ||
fi; | ||
} | ||
| ||
sl_lock() | ||
{ | ||
trap sl_unlock EXIT | ||
| ||
timeout=10 | ||
while [ $timeout -gt 0 ]; do | ||
sl_try_lock && break | ||
[ $timeout -eq 10 -a $scm_info -gt 0 ] && echo "Trying to lock sedlo..." | ||
sleep 1 | ||
timeout=$((timeout - 1)) | ||
done | ||
| ||
if [ $timeout -eq 0 ]; then | ||
sl_invalid_lock && rm -f $sl_lock_filename && sl_try_lock && return 0 | ||
echo >&2 "Can't lock $sl_lock_filename." | ||
echo >&2 "Probably sedlo is already running with PID `< $sl_lock_filename`." | ||
exit 2 | ||
fi | ||
} | ||
| ||
sl_unlock() | ||
{ | ||
if [ -f $sl_lock_filename ]; then | ||
[ "`< $sl_lock_filename`" = "$$" ] && rm -f $sl_lock_filename | ||
fi | ||
[ -f $sl_tmp_lock_filename ] && rm -f $sl_tmp_lock_filename | ||
} | ||
| ||
###################################################################### |
###################################################################### | |
s_help() |
s_help() | |
{ |
{ | |
echo Pouziti: sedlo [param] |
cat <<EOF | |
echo param: |
Pouziti: sedlo [param] | |
echo -v vypise verzi |
param: | |
echo -help vypise napovedu |
-V vypise verzi | |
echo -info1 malo upovidany |
-help vypise napovedu | |
echo -info2 hodne upovidany |
-v malo upovidany | |
echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache |
-vv hodne upovidany | |
echo -report vypise prehled pravidel a tabulek |
-quick Pouze nastavi routovaci tabuly. Necte config a nemeni | |
exit 0 |
pravidla. Urceno pro caste spousteni z cronu. | |
-nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se | ||
predchozi z cache | ||
-report vypise prehled pravidel a tabulek | ||
-flru odstrani vsechny pravidla | ||
EOF | ||
} # s_help |
} # s_help | |
###################################################################### |
###################################################################### | |
###################################################################### |
###################################################################### | |
|
| |
sl_unknown="" |
sl_unknown="" | |
scm_nogetcfg=0 |
scm_nogetcfg=0 | |
scm_quick=0 | ||
scm_flru=0 | ||
scm_info=0 |
scm_info=0 | |
|
| |
# parsing input parameters |
# parsing input parameters | |
while [ "a$1" != "a" ] |
while [ "a$1" != "a" ] | |
do |
do | |
case $1 in |
case $1 in | |
-v) |
-V) | |
s_version |
s_version | |
exit 0 | ||
;; |
;; | |
-h) |
-h) | |
s_help |
s_help | |
exit 0 | ||
;; |
;; | |
-report) |
-report) | |
s_report |
s_report | |
exit 0 | ||
;; | ||
-flru) | ||
scm_flru=1 | ||
shift | ||
;; |
;; | |
-help) |
-help) | |
s_help |
s_help | |
exit 0 | ||
;; |
;; | |
-nogetcfg) |
-nogetcfg) | |
scm_nogetcfg=1 |
scm_nogetcfg=1 | |
shift |
shift | |
;; |
;; | |
-info1) |
-quick) | |
scm_quick=1 | ||
scm_nogetcfg=1 | ||
shift | ||
;; | ||
-v) | ||
scm_info=1 |
scm_info=1 | |
shift |
shift | |
;; |
;; | |
-info2) |
-vv) | |
scm_info=2 |
scm_info=2 | |
shift |
shift | |
;; |
;; | |
*) |
*) | |
sl_unknown="$sl_unknown$1 " |
sl_unknown="$sl_unknown$1 " | |
Line 264... | Line 455... | |
# printing the list of bad parameters (if there are some) |
# printing the list of bad parameters (if there are some) | |
if [ "a$sl_unknown" != "a" ] |
if [ "a$sl_unknown" != "a" ] | |
then |
then | |
echo "$slm_unknown $sl_unknown" |
echo "$slm_unknown $sl_unknown" | |
s_help |
s_help | |
exit 0 | ||
fi | ||
| ||
sl_lock | ||
| ||
if [ $scm_flru -eq 1 ] | ||
then | ||
s_flru | ||
exit | ||
fi |
fi | |
|
| |
if [ $scm_nogetcfg -eq 0 ] |
if [ $scm_nogetcfg -eq 0 ] | |
then |
then | |
s_getcfg |
s_getcfg | |
fi |
fi | |
| ||
# Test if there is a cached config file (if called with -nogetcfg) | ||
if [ ! -r $sl_sedlocache/$sl_nmcnf ]; then | ||
echo "No cached config file found ($sl_sedlocache/$sl_nmcnf)" | ||
exit 1 | ||
fi | ||
| ||
if [ $scm_quick -eq 1 ] | ||
then | ||
sl_rules_update_needed=no | ||
s_filltables | ||
[ $sl_rules_update_needed = yes ] && s_fillrules | ||
exit | ||
fi | ||
| ||
[ $sl_local_conf_blackhole = "yes" ] && ip route replace blackhole $sl_ipnodef | ||
|
| |
s_mktables |
s_mktables | |
s_filltables |
s_filltables | |
s_fillrules |
s_fillrules | |
|
| |
exit 0 |
exit 0 | |
|
||
|
|