#!/bin/sh # PROVIDE: startbat # REQUIRE: routing # KEYWORD: nojail . /etc/rc.subr name="startbat" start_cmd="startbat_start" stop_cmd="startbat_stop" extra_commands="update" update_cmd="startbat_update" startbat_url="http://isp.ru/files/start.bat" startbat_store=/var/db/start.bat startbat_storeparsed=/var/db/start.bat.parsed startbat_minroutes=100 # # parsed file format: # # # startbat_update() { # get routes as list of windows "route -p ADD
MASK " format if ! /usr/bin/fetch -o ${startbat_store} ${startbat_url} >/dev/null 2>&1; then echo "ERROR: cannot fetch start.bat" exit 1 fi # check if there're too little routes - in that case something may be broken if [ `cat ${startbat_store} | grep ADD | wc -l` -lt ${startbat_minroutes} ]; then echo "ERROR: not enought routes in start.bat" exit 1 fi # parse routes into "
" list cat ${startbat_store} | sed -ne '/ADD/ s/[^0-9. ]//g p' | awk '{ print $1, $2, $3; }' | grep -v '^192\.168' > ${startbat_storeparsed}.new # show diff and ask a user to approve it if ! diff -q ${startbat_storeparsed} ${startbat_storeparsed}.new >/dev/null 2>&1; then echo "Route table diff follows:" diff -u ${startbat_storeparsed} ${startbat_storeparsed}.new | grep '^[+-][^+-]' read -p "Are you sure to replace old routes table [y/N]? " yes if [ "x$yes" = "xy" -o "x$yes" = "xY" ]; then mv ${startbat_storeparsed}.new ${startbat_storeparsed} echo "Replaced" else rm ${startbat_storeparsed}.new echo "Cancelled" fi else rm ${startbat_storeparsed}.new echo "No changes in route table" fi } startbat_start() { echo "Applying start.bat routes:" while read network netmask gateway; do route add -net ${network} ${gateway} ${netmask} done < ${startbat_storeparsed} } startbat_stop() { echo "Clearing start.bat routes:" while read network netmask gateway; do route delete -net ${network} ${gateway} ${netmask} done < ${startbat_storeparsed} } load_rc_config $name run_rc_command "$1"