1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
#!/bin/sh
set -e
if [ "$BANDWIDTHD_PACKAGE_DEBUG" != "" ]; then
set -x
fi
# Source debconf library.
. /usr/share/debconf/confmodule
db_version 2.0
db_title BandwidthD
# find devices to use as choices for debconf.
# (this should really be picked up from the output of "bandwidthd -l",
# but since the config script is ran before the package is even unpacked
# it can not be used.)
IFCONFIG="/sbin/ifconfig"
ROUTE="/sbin/route"
IPROUTE="/bin/ip"
LIST_DEV=""
LIST_SUBNETS=""
if [ -f $IPROUTE ]; then
LIST_DEV=$($IPROUTE link show | grep "UP" | sed -e "s/.*: \(.*\): <.*/\1/")
LIST_SUBNETS=$($IPROUTE route show | grep -v ^default | awk '{if (match($1, '/\\\//')) { print $1 } else { print $1 "/32" } }')
elif [ -f $IFCONFIG ] && [ -f $ROUTE ]; then
LIST_DEV=$($IFCONFIG | grep "Link encap:" | cut -d" " -f1)
LIST_SUBNETS=$($ROUTE -n | grep ^[0-9] | grep -v ^0.0.0.0 | cut -c0-15,33-47 | sed -e 's# \+#/#')
#else
# echo "WARNING: bandwidthd.config: Could not find eigther ip(route2) nor ifconfig to list interfaces."
fi
# create device list, starting by pseudo-device "any".
DEBCONF_INTERFACES="any"
for DEV in $LIST_DEV; do
DEBCONF_INTERFACES="${DEBCONF_INTERFACES}, ${DEV}"
done
# (set default interface to eth0 if it exists, otherwise use any as
# a fallback default since it always exists).
if [ "$(echo $DEBCONF_INTERFACES | grep -c1 ', eth0')" != 0 ]; then
DEBCONF_INTERFACES_DEFAULT="eth0"
else
DEBCONF_INTERFACES_DEFAULT="any"
fi
# find subnets to use as default for debconf.
for SUBNET in $LIST_SUBNETS; do
if [ "$DEBCONF_SUBNET_DEFAULT" = "" ]; then
DEBCONF_SUBNET_DEFAULT="${SUBNET}"
else
DEBCONF_SUBNET_DEFAULT="${DEBCONF_SUBNET_DEFAULT}, ${SUBNET}"
fi
done
# Use full hostname as default sensor id.
DEBCONF_SENSORID=$(/bin/hostname)
if [ "$BANDWIDTHD_PACKAGE_DEBUG" != "" ]; then
echo "dev: $DEBCONF_INTERFACES"
echo "dev(default): $DEBCONF_INTERFACES_DEFAULT"
echo "subnets: $DEBCONF_SUBNET_DEFAULT"
fi
if [ "$BANDWIDTHD_PACKAGE_DEBUG" != "" ]; then
db_fset bandwidthd/dev seen false
db_fset bandwidthd/subnet seen false
db_fset bandwidthd/promisc seen false
# bwdpgsql-specific:
db_fset bandwidthd-pgsql/sensorid seen false
fi
# --- Listen on which interface? ---
# FIXME: replace db_subst "interfaces" with db_setchoices when it becomes
# available in debconf. See Debian bug #174130.
# Set available choices.
db_subst bandwidthd/dev "interfaces" "$DEBCONF_INTERFACES" || true
# Set default choice (unless valid old value exists).
db_get bandwidthd/dev || true
if [ "$RET" = "" ]; then
db_set bandwidthd/dev "$DEBCONF_INTERFACES_DEFAULT" || true
elif [ "$(echo $DEBCONF_INTERFACES | grep -c1 $RET)" = 0 ]; then
db_set bandwidthd/dev "$DEBCONF_INTERFACES_DEFAULT" || true
fi
# Ask question.
db_input high bandwidthd/dev || true
db_go || true
# --- Log/Graph which subnets? ---
# Set default subnets (unless value already exists).
db_get bandwidthd/subnet || true
if [ "$RET" = "" ] ; then
db_set bandwidthd/subnet "$DEBCONF_SUBNET_DEFAULT" || true
fi
# Ask question.
db_input high bandwidthd/subnet || true
db_go || true
# --- Sensor id? ---
# Set fully qualified hostname as default sensor string
db_get bandwidthd-pgsql/sensorid || true
if [ "$RET" = "" ]; then
db_set bandwidthd-pgsql/sensorid "$DEBCONF_SENSORID" || true
fi
# ask question.
db_input medium bandwidthd-pgsql/sensorid || true
db_go || true
# --- PROMISC ? ---
db_input low bandwidthd/promisc || true
db_go || true
# -------------------------------------------------------------
# Create bandwidthd.conf based on debconf data, and install it.
# -------------------------------------------------------------
db_get bandwidthd/dev || true
DEV=$RET
db_get bandwidthd/subnet || true
SUBNETS=$RET
# ATTENTION! $SUBNETS needs to be parsed and split.
# Example contents: 192.168.1.0/24, 172.20.0.0 255.255.0.0, 10.0.0.0/8
# dbconfig-common
. /usr/share/dbconfig-common/dpkg/config.pgsql
dbc_go bandwidthd-pgsql $@
# Get pgsql-settings from dbconfig-common.
if [ -f /etc/dbconfig-common/bandwidthd-pgsql.conf ]; then
. /etc/dbconfig-common/bandwidthd-pgsql.conf
fi
if [ "$dbc_install" = "true" ]; then
PGSQL_USER=$dbc_dbuser
PGSQL_PASS=$dbc_dbpass
PGSQL_DB=$dbc_dbname
PGSQL_HOST=$dbc_dbserver
if [ -z "$PGSQL_HOST" ]; then
PGSQL_HOST="localhost"
fi
PGSQL_PORT=$dbc_dbport
if [ -z "$PGSQL_PORT" ]; then
PGSQL_PORT=5432
fi
else
PGSQL_USER="bwduser"
PGSQL_PASS="bwdpass"
PGSQL_DB="bwddb"
PGSQL_HOST="localhost"
PGSQL_PORT=5432
fi
db_get bandwidthd-pgsql/sensorid || true
SENSORID=$RET
db_get bandwidthd/promisc || true
PROMISC=$RET
if [ "$BANDWIDTHD_PACKAGE_DEBUG" != "" ]; then
echo "This is the bandwidthd postinst script reporting debconf settings:"
echo "dev: $DEV"
echo "subnets: $SUBNETS"
echo "promisc: $PROMISC"
fi
# parse a bandwidthd.conf template and insert dev, subnet and
# recover_cdf rules, then use ucf to install config file.
TMPDIR=$(mktemp -d)
TMPFILE=$TMPDIR/bandwidthd.conf
cp /usr/share/doc/bandwidthd-pgsql/bandwidthd-pgsql.conf $TMPFILE
chmod 640 $TMPFILE
RULE='s%^#DEBCONF_DEVS#$%dev "'$DEV'"%'
sed -ie "$RULE" $TMPFILE
PARSED_SUBNETS=$(echo $SUBNETS | sed -e 's/, */:/g')
#PARSED_SUBNETS=$(echo $PARSED_SUBNETS | sed -e 's/^/subnet /')
OLDIFS=$IFS
IFS=":"
for subnet in $PARSED_SUBNETS ; do
# CIDR and dotted-quad subnets have different syntax:
# subnet 1.2.3.4/24
# subnet 4.3.2.1 255.255.255.0
subnet=$(echo $subnet | sed -e 's#/\(.*\..*\)# \1#')
RULE='s%^#DEBCONF_SUBNETS#$%subnet '$subnet'\
#DEBCONF_SUBNETS#%'
sed -ie "$RULE" $TMPFILE
done
RULE='s%^#DEBCONF_SUBNETS#$%%'
sed -ie "$RULE" $TMPFILE
IFS=$OLDIFS
RULE='s%^#DEBCONF_PGSQL#$%pgsql_connect_string "user = '$PGSQL_USER' password = '$PGSQL_PASS' dbname = '$PGSQL_DB' host = '$PGSQL_HOST'"%'
sed -ie "$RULE" $TMPFILE
RULE='s%^#DEBCONF_SENSORID#$%sensor_id "'$SENSORID'"%'
sed -ie "$RULE" $TMPFILE
RULE='s%^#DEBCONF_PROMISC#$%promiscuous '$PROMISC'%'
sed -ie "$RULE" $TMPFILE
# Install new configuration...
chmod 640 $TMPFILE
ucf --debconf-ok $TMPFILE /etc/bandwidthd/bandwidthd.conf
chmod 640 /etc/bandwidthd/bandwidthd.conf
# create new password config file for psql (used in weekly cron script)
TMPFILE=$TMPDIR/pgpass
echo "$PGSQL_HOST:$PGSQL_PORT:$PGSQL_DB:$PGSQL_USER:$PGSQL_PASS" > $TMPFILE
chown root:root $TMPFILE
chmod 0600 $TMPFILE
ucf --debconf-ok $TMPFILE /etc/bandwidthd/.pgpass
# clean up
rm -rf $TMPDIR
|