postplug/postplug.d/20network
2011-04-27 19:57:03 +02:00

398 lines
11 KiB
Bash

#!/bin/bash
#
# network - network plugin for postplug
#
# Copyright (c) 2004-2007 by Davide Madrisan <davide.madrisan@qilinux.it>
# Copyright (c) 2009 by Silvan Calarco <silvan.calarco@mambasoft.it>
#
if [ $UID != 0 ]; then
echo "$0: must be superuser." >&2
exit 1
fi
me="network"
. /etc/postplug/postplug.defs
postplugnew_file=$postplugnewdir/network
rm -f $postplugnew_file
[ -r /etc/sysconfig/postplug ] && . /etc/sysconfig/postplug
[ -r /etc/sysconfig/rc ] && . /etc/sysconfig/rc
[ -r $rc_networkfunctions ] && . $rc_networkfunctions
resolv_conf=/etc/resolv.conf
sysconf_network=/etc/sysconfig/network
function wlan_config_auto() {
local wlan_interface="$1"
cat > $network_devices/ifconfig.${wlan_interface} << _EOF
IF=${wlan_interface}
BOOTPROTO=dhcp
ZONE=local
ONBOOT=yes
#WIRELESS_ESSID=""
#WIRELESS_MODE=managed
#WIRELESS_ENC=s:ENCRYPTIONKEY
#WIRELESS_NWID=
#WIRELESS_FREQ=
_EOF
}
function eth_config_auto() {
local eth_interface="$1"
cat > $network_devices/ifconfig.${eth_interface} << _EOF
IF=${eth_interface}
BOOTPROTO=dhcp
ZONE=local
ONBOOT=yes
_EOF
}
# FIXME : add support for 'ZONE=$IF_ZONE'
function eth_config() {
# eth_config {--enable|--disable|--auto} <int_name>
TEMP=`LANG=C getopt -o eda --long enable,disable,auto -n "$FUNCNAME" -- "$@"`
[[ $? = 0 ]] || return 1
eval set -- "$TEMP"
local action
while :; do
case $1 in
-e|--enable)
action="enable" ;;
-d|--disable)
action="disable" ;;
-a|--auto)
action="auto" ;;
--) shift; break ;;
*) logmsg "$me" "FIXME: "$"\`getopt' error"
exit 1
;;
esac
shift
done
local eth_interface="$1"
[ "$eth_interface" ] ||
{ logmsg "$me" "FIXME: "$"no interface set"
exit 1; }
unset IF_IPADDR IF_NETMASK IF_NETWORK IF_BROADCAST DEST_GATEWAY_IF
if [ "$action" = "disable" ]; then
cat > $network_devices/ifconfig.${eth_interface} << _EOF
IF=${eth_interface}
ONBOOT=no
_EOF
return
fi
$DIALOG --colors \
--backtitle "\
$dialog_backtitle -- "$"Network devices configuration" \
--title $" LAN INTERFACE CONFIGURATION " \
--yesno "
"$"Automatically configure (DHCP) the \Z4$2\Zn interface?" 7 62
# configure DHCP for this interface
if [ $? -eq 0 ]; then
#unset IF_ZONE
cat > $network_devices/ifconfig.${eth_interface} << _EOF
IF=${eth_interface}
BOOTPROTO=dhcp
ZONE=local
ONBOOT=yes
_EOF
else # no DHCP. Ask for manual configuration
while :; do
$DIALOG --colors \
--output-fd 4 \
--ok-label "OK" --no-cancel \
--backtitle "\
$dialog_backtitle -- "$"Network interface configuration" \
--title $" LAN INTERFACE CONFIGURATION ""(${eth_interface}) " \
--form "
"$"Please enter configuration for network interface" 10 54 2 \
$"IP address:" 1 1 "$IF_IPADDR" 1 18 30 15 \
$"Netmask:" 2 1 "$IF_NETMASK" 2 18 30 15 4>$tmpdialog
# workaround to correctly process blank field
local field icount=0
for field in $(sed 's/^/=/g' $tmpdialog); do
field=$(echo $field | sed 's/^=//')
case $icount in
0) IF_IPADDR="$field" ;;
1) IF_NETMASK="$field" ;;
esac
let icount+=1
done
if [[ -n "$IF_IPADDR" && -n "$IF_NETMASK" ]]; then
calculate_ip_parameters "$IF_IPADDR" "$IF_NETMASK"
[[ $? -eq 0 ]] || {
unset IF_IPADDR IF_NETMASK; continue; }
IF_NETWORK=$network
IF_BROADCAST=$broadcast
break
fi
done
cat > $network_devices/ifconfig.${eth_interface} << _EOF
IF=${eth_interface}
ONBOOT=yes
ZONE=local
IPADDR=$IF_IPADDR
NETWORK=$IF_NETWORK
NETMASK=$IF_NETMASK
BROADCAST=$IF_BROADCAST
_EOF
fi
}
# FIXME
DEST_GATEWAY_ZONE=local
# Get Default Gateway IP address and DNS addresses
function net_config() {
unset DEST_GATEWAY_IP DNS_PRIMARY DNS_SECONDARY DEST_DOMAIN
let "retval = 0"
while test $retval != 99; do
let "error = 0"
$DIALOG --colors \
--cr-wrap \
--output-fd 4 \
--no-cancel \
--backtitle "\
$dialog_backtitle -- "$"Network devices configuration" \
--title $" DEFAULT GATEWAY / DNS ADDRESSES / DOMAIN " \
--form "
"$"Please enter the IP addresses of the \Z1default gateway\Zn, \
\Z1primary DNS server\Zn and \Z1secondary DNS server\Zn (optional) \
and the domain name." 13 72 4 \
$"Default gateway:" 1 1 "$DEST_GATEWAY_IP" 1 22 44 15 \
$"Primary DNS:" 2 1 "$DNS_PRIMARY" 2 22 44 15 \
$"Secondary DNS:" 3 1 "$DNS_SECONDARY" 3 22 44 15 \
$"Domain:" 4 1 "$DEST_DOMAIN" 4 22 44 15 4>$tmpdialog
case $? in
0) # workaround to correctly process blank fields and chars
let "icount = 0"
for field in $(sed 's/ /+_:\./g; s/^/=/g' $tmpdialog); do
field=`echo $field | sed 's/^=//; s/+_:\./ /g'`
case $icount in
0) DEST_GATEWAY_IP="$field"
# check for invalid IP addresses
ip_check_args "$DEST_GATEWAY_IP/32"
[[ $? -eq 1 ]] ||
{ unset DEST_GATEWAY_IP; let "error += 1"; } ;;
1) DNS_PRIMARY="$field"
ip_check_args "$DNS_PRIMARY/32"
[[ $? -eq 1 ]] ||
{ unset DNS_PRIMARY; let "error += 1"; } ;;
2) DNS_SECONDARY="$field"
if test -n "$DNS_SECONDARY"; then
ip_check_args "$DNS_SECONDARY/32"
[[ $? -eq 1 ]] ||
{ unset DNS_SECONDARY; let "error += 1"; }
fi ;;
3) DESTHOSTNAME="$field" ;;
esac
let "icount += 1"
done
[[ $error -eq 0 ]] && let "retval = 99" || continue
# DNS_SECONDARY is optional
if [[ -n "$DEST_GATEWAY_IP" && -n "$DNS_PRIMARY" ]]; then
# FIXME : has /etc/resolv.conf already been configured?
[[ -e $resolv_conf ]] && sed -i '/#/d' $resolv_conf
cat >> $resolv_conf << _EOF
domain $DESTHOSTNAME
search $DESTHOSTNAME
nameserver $DNS_PRIMARY
_EOF
[[ "$DNS_SECONDARY" ]] &&
echo "nameserver $DNS_SECONDARY" >> $resolv_conf
sed -i '/nameserver 127.0.0.1/d' $resolv_conf
# we want this line in the last position
echo "nameserver 127.0.0.1" >> $resolv_conf
fi
(cd /etc/sysconfig/network-devices/ &&
for sysconfile in $(ls ifconfig.* 2>/dev/null); do
. $sysconfile
# configure the default gateway using the first
# interface not configured with DHCP
if [[ "$ONBOOT" = yes && "$BOOTPROTO" != dhcp ]]; then
sed -i "/GATEWAY=/d;/GATEWAY_IF=/d" $sysconf_network
echo "\
GATEWAY_IF=${sysconfile##*\.}
GATEWAY=$DEST_GATEWAY_IP" >> $sysconf_network
break
fi
done) ;;
esac
done
}
wlan_ints=()
for wlan_int in $(cat /proc/net/wireless 2>/dev/null | sed -n '/[a-z]*[0-9]*:/{s,:.*,,;p}'); do
# checks for existing physical interfaces
/sbin/ifconfig $wlan_int >/dev/null 2>&1 || continue
# found interface already configured
[ -e $network_devices/ifconfig.$wlan_int ] && continue
wlan_ints[${#wlan_ints[*]}]="$wlan_int"
echo "$wlan_int" >> $postplugnew_file 2>/dev/null
done
eth_ints=()
for eth_int in $(cat /proc/net/dev | sed -n '/eth[0-9]*:/{s,:.*,,;p}'); do
# checks for existing physical interfaces
/sbin/ifconfig $eth_int >/dev/null 2>&1 || continue
# found interface already configured
[ -e $network_devices/ifconfig.$eth_int ] && continue
eth_ints[${#eth_ints[*]}]="$eth_int"
echo "$eth_int" >> $postplugnew_file 2>/dev/null
done
# how many interface have been configured?
let "int_newconf = 0"
if [ ${#eth_ints[*]} -gt 0 ]; then
case "$NETWORK_NEWDEVICE" in
"auto")
for eth_int in ${eth_ints[*]}; do
eth_config_auto $eth_int
let "int_newconf += 1"
done
exit
;;
"dialog")
tmpdialog=`mktemp -q -t ${0##*/}.XXXXXXXX` ||
{ logmsg "$me" "error: "$"cannot create temporary files"
{ (exit 1); exit 1; }; }
trap "rm -f $tmpdialog" 0 1 2 3 5 15
$DIALOG --clear --colors $dialog_timeout_cmd \
--backtitle "\
$dialog_backtitle -- "$"Network devices configuration" \
--title $" LAN INTERFACE CONFIGURATION " \
--yesno "
"$"Found ${#eth_ints[*]} LAN interface(s) not configured.""
"$"Do you want to start the configuration wizard?" 8 62 2>/dev/null
case $? in
0) # ask configuration for all the eth. interfaces
for eth_int in ${eth_ints[*]}; do
eth_config --enable $eth_int
let "int_newconf += 1"
done ;;
1) # disable all the ethernet interfaces
for eth_int in ${eth_ints[*]}; do
eth_config --disable $eth_int
done
exit ;;
*) : ;; # timeout (code = 255)
esac
;;
"off"|"")
exit
;;
*) logmsg "$me" "error: "$"\
illegal value for NETWORK_NEWDEVICE ($NETWORK_NEWDEVICE)"
exit 1
;;
esac
fi
if [ ${#wlan_ints[*]} -gt 0 ]; then
# FIXME: wlan dialog support is missing
case "$NETWORK_NEWDEVICE" in
"auto")
for wlan_int in ${wlan_ints[*]}; do
wlan_config_auto $wlan_int
let "int_newconf += 1"
done
exit
;;
esac
fi
# if at least one of the new interfaces has been configured...
if [ $NETWORK_NEWDEVICE = "dialog" -a $int_newconf -gt 0 ]; then
for eth_int in ${eth_ints[*]}; do
[ -r $network_devices/ifconfig.$eth_int ] || continue
unset BOOTPROTO
. $network_devices/ifconfig.$eth_int
[ "$BOOTPROTO" = "dhcp" ] && exit
done
for wlan_int in ${wlan_ints[*]}; do
[ -r $network_devices/ifconfig.$wlan_int ] || continue
unset BOOTPROTO
. $network_devices/ifconfig.$wlan_int
[ "$BOOTPROTO" = "dhcp" ] && exit
done
# if no interface has been configured as DHCP interface
# get the Default Gateway + DNS + Domain infos via a dialog window
net_config
$DIALOG --clear
fi
# modify the file /etc/hosts if a static IP has been configured
#if grep -Eq "^127\.0\.0\.1 `hostname -f`" /etc/hosts; then
# get_interfaces_by_zone
# # check first local interfaces, then public ones and finally dmz interfaces
# for if_name in ${ifzone_local[*]} ${ifzone_public[*]} ${ifzone_dmz[*]}; do
# get_interface_parameters $if_name
# if [[ $? -eq 0 && "$int_static_ip" = 1 ]]; then
# sed -i "\
#/127\.0\.0\.1 `hostname -f`/{
# s/.*/127.0.0.1 localhost.localdomain localhost/}
# /127.0.0.1.*/a\\
#$int_ip `hostname -f` `hostname`" /etc/hosts
# fi
# done
#fi
rm -f $tmpdialog
if [ -e /etc/wicd/manager-settings.conf ]; then
eval `grep "wireless_interface" /etc/wicd/manager-settings.conf | tr -d ' '`
eval `grep "wired_interface" /etc/wicd/manager-settings.conf | tr -d ' '`
if [ "$wireless_interface" ]; then
test_iwconfig=`iwconfig $wireless_interface 2>/dev/null`
[ "$test_iwconfig" = "" ] && \
sed -i "/wireless_interface =.*/d" /etc/wicd/manager-settings.conf
fi
if [ "$wired_interface" ]; then
test_iwconfig=`iwconfig $wired_interface 2>/dev/null`
test_ifconfig=`ifconfig $wired_interface 2>/dev/null`
[ "$test_ifconfig" = "" -o "$test_iwconfig" != "" ] && \
sed -i "/wired_interface =.*/d" /etc/wicd/manager-settings.conf
fi
fi
exit 0