--- trunk/magellan-initscripts/etc/rc.d/init.d/network 2005/03/15 19:07:56 71 +++ trunk/magellan-initscripts/etc/rc.d/init.d/network 2005/10/21 15:24:25 275 @@ -1,5 +1,5 @@ #!/bin/bash -# $Header: /home/cvsd/magellan-cvs/magellan-src/magellan-initscripts/etc/rc.d/init.d/network,v 1.3 2005-03-15 19:07:56 niro Exp $ +# $Header: /home/cvsd/magellan-cvs/magellan-src/magellan-initscripts/etc/rc.d/init.d/network,v 1.11 2005-10-21 15:24:25 niro Exp $ #%rlevels: 0:k 1:k 2:k 3:s 4:s 5:s 6:k #%start: 20 @@ -13,7 +13,61 @@ source /etc/sysconfig/rc source ${rc_functions} -checkconfig() { +# read values from files +read_value() +{ + local var="$1" + local file="$2" + local value + + # local all possible vars + # global + local ONBOOT + local NETWORKING + + # static + local IP + local NETMASK + local BROADCAST + local NETWORKING + local FORCE_MAC_TO + + # dhcp + local DHCP_PROG + local DHCP_START + local DHCP_STOP + + # default gw + local GATEWAY + local GATEWAY_IF + + # wireless extensions + local WIRELESS_AP + local WIRELESS_AUTH_MODE + local WIRELESS_BITRATE + local WIRELESS_CHANNEL + local WIRELESS_DEFAULT_KEY + local WIRELESS_ESSID + local WIRELESS_FREQUENCY + local WIRELESS_KEY + local WIRELESS_KEY_ASCII + local WIRELESS_KEY_0 + local WIRELESS_KEY_1 + local WIRELESS_KEY_2 + local WIRELESS_KEY_3 + local WIRELESS_KEY_LENGTH + local WIRELESS_MODE + local WIRELESS_NICK + local WIRELESS_NWID + local WIRELESS_POWER + + source ${file} + eval value=\$$(echo ${var}) + echo "${value}" +} + +checkconfig() +{ if [ -z "${NETWORKING}" ] then echo "NETWORKING missing in net.${interface}, aborted" @@ -53,89 +107,265 @@ esac } - -case "$1" in - start) - for file in $(grep -il "ONBOOT=\"yes\"" ${network_settings}/net.*) - do - interface=$(basename ${file} | sed s/net.//) - case "${interface}" in +# onboot_interface_list /path/to/files* +onboot_interface_list() +{ + local file + local devices + local iface + + # get list of all devices + for file in $@ + do + if [[ $(read_value ONBOOT ${file}) = yes ]] + then + iface="$(basename ${file} | sed s/net.//)" + # exclude backup files + case "${iface}" in *~) ;; - *) - source ${network_settings}/net.${interface} || exit 1 - checkconfig - case "${NETWORKING}" in - dhcp) - echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${interface}${COLDEFAULT} ..." - ${CURS_UP} - ${SET_WWCOL} - echo "[DHCP]" - loadproc ${DHCP_PROG} ${DHCP_START} - ;; - static) - echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${interface}${COLDEFAULT} ..." - ${CURS_UP} - ${SET_WWCOL} - echo "[STATIC]" - ifconfig ${interface} ${IP} netmask ${NETMASK} broadcast ${BROADCAST} - evaluate_retval - ;; - esac - ;; + *) devices="${devices} $(basename ${file} | sed s/net.//)" ;; esac - done + fi + done + + echo "${devices}" +} + +config_wireless_wep() +{ + local iface="$1" + + if [[ -z ${iface} ]] + then + echo "WEP: no \$iface given. Aborting setup." + return 1 + fi + + ${CURS_UP} + ${SET_WWCOL} + echo "[AUTH: WEP]" + + iwconfig "${iface}" enc on + [[ -n ${WIRELESS_KEY_LENGTH} ]] && iwconfig "${iface}" enc "${WIRELESS_KEY_LENGTH}" + [[ -n ${WIRELESS_KEY} ]] && iwconfig "${iface}" key "${WIRELESS_KEY}" + [[ -n ${WIRELESS_KEY_ASCII} ]] && iwconfig "${iface}" key s:"${WIRELESS_KEY_ASCII}" +} + +config_wireless_wpa() +{ + local iface="$1" + + if [[ -z ${iface} ]] + then + echo "WPA: no \$iface given. Aborting setup." + return 1 + fi + + if [ ! -x /sbin/wpa_supplicant ] + then + echo "WPA: wpa_supplicant not installed. Aborting setup." + return 1 + fi + + ${CURS_UP} + ${SET_WWCOL} + echo "[AUTH: WPA]" + + # get default settings + [[ -f /etc/conf.d/wpa_supplicant ]] && source /etc/conf.d/wpa_supplicant + + # check the configuration + [[ -z ${WIRELESS_WPA_CONFIG} ]] && WIRELESS_WPA_CONFIG=/etc/wpa_supplicant.auto + [[ -z ${WIRELESS_WPA_SKEL} ]] && WIRELESS_WPA_SKEL=/etc/conf.d/wpa_supplicant.skel + if [[ -z ${WIRELESS_WPA_DRIVER} ]] + then + echo "WPA: WIRELESS_WPA_DRIVER given. Aborting setup." + return 1 + fi + + # write a config with the settings from net.${iface} + # only wpa-psk ! all other needs manual setup + if [[ ${WIRELESS_WPA_AUTOCONF} = true ]] + then + # write default cfg from skeleton + cat ${WIRELESS_WPA_SKEL} > ${WIRELESS_WPA_CONFIG} + + # setup the network entry + sed -i -e "s:@WIRELESS_ESSID@:${WIRELESS_ESSID}:g" \ + -e "s:@WIRELESS_KEY@:${WIRELESS_KEY}:g" \ + ${WIRELESS_WPA_CONFIG} + fi + + # remove old state dir + [ -d /var/run/wpa_supplicant ] && rm -rf /var/run/wpa_supplicant + + # now run the wpa_supplicant dameon + wpa_supplicant -B \ + -D"${WIRELESS_WPA_DRIVER}" \ + -c"${WIRELESS_WPA_CONFIG}" \ + -i"${iface}" \ + ${WIRELESS_WPA_OPTS} + + # echo wait 5 seconds + echo " Waiting 5 seconds to retrieve authentification reply ... " + sleep 5 +} + +setup_wireless_extensions() +{ + local iface="$1" + + if [[ -z ${iface} ]] + then + echo "WIRELESS_EXTENSIONS: no \$iface given. Aborting setup." + return 1 + fi + + if [[ -n ${WIRELESS_BITRATE} ]] || + [[ -n ${WIRELESS_CHANNEL} ]] || + [[ -n ${WIRELESS_ESSID} ]] || + [[ -n ${WIRELESS_FREQUENCY} ]] || + [[ -n ${WIRELESS_MODE} ]] || + [[ -n ${WIRELESS_NICK} ]] || + [[ -n ${WIRELESS_AUTH_MODE} ]] + then + echo -e ${COLOREDSTAR}"Setting up wlan-ext for ${COLBLUE}${iface}${COLDEFAULT} ... " + fi - if [ -n "${GATEWAY}" -a -n "${GATEWAY_IF}" ] + [[ -n ${WIRELESS_BITRATE} ]] && iwconfig "${iface}" rate "${WIRELESS_BITRATE}" + [[ -n ${WIRELESS_CHANNEL} ]] && iwconfig "${iface}" channel "${WIRELESS_CHANNEL}" + [[ -n ${WIRELESS_ESSID} ]] && iwconfig "${iface}" essid "${WIRELESS_ESSID}" + [[ -n ${WIRELESS_FREQUENCY} ]] && iwconfig "${iface}" freq "${WIRELESS_FREQUENCY}" + [[ -n ${WIRELESS_MODE} ]] && iwconfig "${iface}" mode "${WIRELESS_MODE}" + [[ -n ${WIRELESS_NICK} ]] && iwconfig "${iface}" nick "${WIRELESS_NICK}" + + case "${WIRELESS_AUTH_MODE}" in + wpa) config_wireless_wpa "${iface}" ;; + wep|on) config_wireless_wep "${iface}" ;; + off) iwconfig "${iface}" enc off ;; + esac +} + +networking_start() +{ + local iface dns + + # get list of all devices + for iface in $(onboot_interface_list ${network_settings}/net.*) + do + # checkconfig + source ${network_settings}/net.${iface} || exit 1 + checkconfig + + # setup mac + if [ -n "${FORCE_MAC_TO}" ] then - echo -e ${COLOREDSTAR}"Setting up default gateway ..." - route add default gateway ${GATEWAY} metric 1 \ - dev ${GATEWAY_IF} + echo -e ${COLOREDSTAR}"Faking MAC to ${FORCE_MAC_TO} for ${COLBLUE}${iface}${COLDEFAULT} ... " + ifconfig "${iface}" hw ether "${FORCE_MAC_TO}" evaluate_retval fi - update_svcstatus $1 - splash svc_started "$(basename $0)" 0 - ;; + # activate the interface + ifconfig "${iface}" up + + # now configure wireless_extensions + [ -x /usr/sbin/iwconfig ] && setup_wireless_extensions "${iface}" + + echo -e ${COLOREDSTAR}"Bringing up interface ${COLBLUE}${iface}${COLDEFAULT} ..." + + # setup static or dhcp + case ${NETWORKING} in + dhcp|DHCP) + ${CURS_UP} + ${SET_WWCOL} + echo "[DHCP]" + loadproc ${DHCP_PROG} ${DHCP_START} "${iface}" + ;; + static|STATIC) + ${CURS_UP} + ${SET_WWCOL} + echo "[STATIC]" + ifconfig "${iface}" "${IP}" netmask "${NETMASK}" broadcast "${BROADCAST}" + evaluate_retval + ;; + esac - stop) - if [ -n "${GATEWAY}" ] + # setup def gw + if [[ -n ${GATEWAY} ]] + then + echo -e ${COLOREDSTAR}"Setting up default gateway for ${COLBLUE}${iface}${COLDEFAULT} ..." + route add default gateway ${GATEWAY} metric 1 dev ${iface} + evaluate_retval + fi + + # setup /etc/resolv.conf + if [[ -n ${NAMESERVER} ]] + then + echo -e ${COLOREDSTAR}"Setting up all nameserver for ${COLBLUE}${iface}${COLDEFAULT} ..." + + # whipe out the old one + echo "# Generated by the magellan-initscripts for ${iface}" > /etc/resolv.conf + for dns in ${NAMESERVER} + do + echo "nameserver ${dns}" >> /etc/resolv.conf + done + fi + done +} + +networking_stop() +{ + # get list of all devices + for iface in $(onboot_interface_list ${network_settings}/net.*) + do + source ${network_settings}/net.${iface} || exit 1 + checkconfig + + if [[ -n ${GATEWAY} ]] then echo -e ${COLOREDSTAR}"Removing default gateway ..." route del -net default evaluate_retval fi - for file in $(grep -il "ONBOOT=\"yes\"" ${network_settings}/net.*) - do - interface=$(basename ${file} | sed s/net.//) - case "${interface}" in - *~) ;; - *) - #$network_devices/ifdown $interface - source ${network_settings}/net.${interface} || exit 1 - checkconfig - echo -e ${COLOREDSTAR}"Bringing down interface ${COLBLUE}${interface}${COLDEFAULT} ..." - ifconfig ${interface} down - evaluate_retval - - #shutdown dhcp-daemon - if [ "${NETWORKING}" == dhcp ] - then - my_runlevel="`runlevel | cut -d ' ' -f2`" - if [ -n "$(pidof ${DHCP_PROG})" ] - then - echo -e ${COLOREDSTAR}"Stopping the dhcp-daemon ..." - ${CURS_UP} - ${SET_WWCOL} - echo "[$(basename ${DHCP_PROG})]" - ${DHCP_PROG} ${DHCP_STOP} - evaluate_retval - fi - fi - ;; - esac - done + echo -e ${COLOREDSTAR}"Bringing down interface ${COLBLUE}${iface}${COLDEFAULT} ..." + ifconfig ${iface} down + evaluate_retval + + # shutdown dhcp-daemon + if [[ ${NETWORKING} = dhcp ]] && [[ -n $(pidof ${DHCP_PROG}) ]] + then + echo -e ${COLOREDSTAR}"Stopping the dhcp-daemon ..." + ${CURS_UP} + ${SET_WWCOL} + echo "[$(basename ${DHCP_PROG})]" + ${DHCP_PROG} ${DHCP_STOP} "${iface}" + evaluate_retval + fi + # shutdown wpa_supplicant daemon + if [[ -n $(pidof wpa_supplicant) ]] + then + killall wpa_supplicant + fi + done + + # remove state dir + if [ -d /var/run/wpa_supplicant ] + then + rm -rf /var/run/wpa_supplicant + fi +} + +case $1 in + start) + networking_start + update_svcstatus $1 + splash svc_started "$(basename $0)" 0 + ;; + + stop) + networking_stop update_svcstatus $1 splash svc_stopped "$(basename $0)" 0 ;;