netcardconfig: show the wpasecret input box, even if password is already set
[grml-network.git] / sbin / netcardconfig
index 0107ab1..f602f79 100755 (executable)
@@ -4,7 +4,6 @@
 # Authors:       Michael Prokop <mika@grml.org>, Marcel Wichern 2006, Klaus Knopper 2002, Niall Walsh + Stefan Lippers-Hollmann 2004-2007
 # Bug-Reports:   see http://grml.org/bugs/
 # License:       This file is licensed under the GPL v2.
-# Latest change: Sam Jän 27 19:28:54 CET 2007 [mika]
 ################################################################################
 # Changes have been merged from Kanotix's and Sidux's netcardconfig:
 # http://kanotix.com/files/debian/pool/main/n/netcardconfig-kanotix/
@@ -16,7 +15,7 @@ LANGUAGE=C LANG=C
 export PATH LANGUAGE LANG
 
 . /etc/grml/script-functions
-check4root
+check4root || exit 1
 
 TMP=$(mktemp)
 
@@ -82,6 +81,32 @@ writeiwline() {
   #echo "DEBUG: for interfaces $IWOURLINE"
 }
 
+generate_udev_entry() {
+  interface=''
+  mkdir -p /etc/udev/scripts
+  echo "# Auto generated script from netcardconfig on $(date)
+# Executing this script generates an entry in /etc/udev/rules.d/z25_persistent-net.rules
+# for you, please check z25_persistent-net.rules for existing entries before
+# running this script (once more)." > /etc/udev/scripts/netcardconfig
+  for interface in `ifconfig | awk '/^[a-z]/ &&!/^lo/{ print $1} '` ; do
+      echo -n "INTERFACE=$interface /lib/udev/write_net_rules " >> /etc/udev/scripts/netcardconfig && \
+      if which udevadm >/dev/null 2>&1; then
+        udevadm info -a -p /sys/class/net/$interface | awk -F'==' '/address/ {print $2}' >> /etc/udev/scripts/netcardconfig
+      else
+        udevinfo -a -p /sys/class/net/$interface | awk -F'==' '/address/ {print $2}' >> /etc/udev/scripts/netcardconfig
+      fi
+  done
+  # send errors to /dev/null as well because the sed line inside the /lib/udev/write_net_rules
+  # script outputs a sed warning which might unsettle users
+  chmod +x /etc/udev/scripts/netcardconfig && /etc/udev/scripts/netcardconfig 1>/dev/null 2>&1
+}
+
+disable_config_dhcp() {
+  if grep -q CONFIG_DHCP /etc/grml/autoconfig ; then
+     sed -i "s|^CONFIG_DHCP.*|CONFIG_DHCP='no'|" /etc/grml/autoconfig
+  fi
+}
+
 device2props() {
   PARTCOUNT=0
   isauto=0
@@ -131,6 +156,8 @@ configiface() {
   DEVICE=${NETDEVICES[$DV]}
   device2props
   DV=$DEVICENAME
+  ifdown $DV
+  sleep 3
   # wireless config
   WLDEVICE="$(LANG=C LC_MESSAGEWS=C iwconfig $DV 2>/dev/null | awk '/802\.11|READY|ESSID/{print $1}')"
   WLDEVICECOUNT="$(LANG=C LC_MESSAGEWS=C iwconfig $DV 2>/dev/null | wc -l)"
@@ -300,10 +327,17 @@ configiface() {
                           }
                   }
                   ' /etc/network/interfaces)
-          $DIALOG --inputbox "$MESSAGEW23 $ESSID" 15 50 "$WPASECRET" 2>"$TMP" || bailout 1
-
-          # make sure backslashes inside passphrase are handled correct
-          WPASECRET=$(sed -e 's/\\/\\/g' "$TMP") && rm -r "$TMP"
+          FIRST_RUN=0  # show the wpasecret input box at least once
+          while ( [ -z "$WPASECRET" ] || [ "$FIRST_RUN" ] ) ; do
+            $DIALOG --inputbox "$MESSAGEW23 $ESSID" 15 50 "$WPASECRET" 2>"$TMP" || bailout 1
+            read WPASECRET <"$TMP"
+            if [ -z "$WPASECRET" ] ; then
+              $DIALOG --msgbox "Sorry, empty password not allowed, please retry." 0 0 || bailout 1
+            fi
+            FIRST_RUN=""
+          done
+
+          rm -f "$TMP"
 
           case $WPA_DEV in
                hostap)
@@ -364,8 +398,8 @@ configiface() {
         /netmask/{if(found){netmask=$NF}}
         /broadcast/{if(found){broadcast=$NF}}
         /gateway/{if(found){gateway=$NF}}
-        /dns-nameservers/{if(found){dns-nameservers=$NF}}
-        END{print address" "netmask" "broadcast" "gateway" "dns-nameservers}' /etc/network/interfaces >"$TMP"
+        /dns-nameservers/{if(found){dnsnameservers=$NF}}
+        END{print address" "netmask" "broadcast" "gateway" "dnsnameservers}' /etc/network/interfaces >"$TMP"
       read IP NM BC DG <"$TMP"
       rm -f "$TMP"
     fi
@@ -379,14 +413,14 @@ configiface() {
     $DIALOG --inputbox "$MESSAGE8 $DV" 10 45 "${BC:-${IP%.*}.255}" 2>"$TMP" || bailout 1
     read BC <"$TMP" ; rm -f "$TMP"
 
-    $DIALOG --inputbox "$MESSAGE9" 10 45 "${DG:-${IP%.*}.254}" 2>"$TMP"
+    $DIALOG --inputbox "$MESSAGE9" 10 45 "${DG:-${IP%.*}.1}" 2>"$TMP"
     read DG <"$TMP" ; rm -f "$TMP"
 
     if [ -f "/etc/resolv.conf" ]; then
       NS="$(awk '/^nameserver/{printf "%s ",$2}' /etc/resolv.conf)"
     fi
 
-    $DIALOG --inputbox "$MESSAGE10" 10 45 "${NS:-${IP%.*}.254}" 2>"$TMP"
+    $DIALOG --inputbox "$MESSAGE10" 10 45 "${NS:-$DG}" 2>"$TMP"
     read NS <"$TMP" ; rm -f "$TMP"
 
     if [ -w /etc/network/interfaces ]; then
@@ -456,9 +490,25 @@ MESSAGEW26=")?   If you say no, your wep key will be stored in /etc/network/inte
 
 
 NETDEVICESCOUNT=0
-LAN=$(tail -n +3 /proc/net/dev|awk -F: '{print $1}'|sed "s/\s*//"|grep -v -e ^lo -e ^vmnet|sort)
-[ -n "$WLAN" ] || WLAN=$(tail -n +3 /proc/net/wireless 2>/dev/null|awk -F: '{print $1}'|sort)
-unset LAN_DEVICES WLAN_DEVICES FIREWIRE_DEVICES NETDEVICES
+if [ -r /proc/net/dev ] ; then
+   LAN=$(tail -n +3 /proc/net/dev | awk -F: '{print $1}'| sed "s/\s*//" | grep -v -e ^lo -e ^vmnet | sort)
+else
+   LAN=""
+fi
+
+if [ -z "$WLAN" ] ; then
+  WLAN_OLD=$(tail -n +3 /proc/net/wireless 2>/dev/null|awk -F: '{print $1}')
+  # /proc/net/wireless does not work e.g. with iwl3945 on kernel 2.6.33 anymore
+  WLAN_NEW=$(for i in /sys/class/net/* ; do ( [ -d $i/wireless ] || [ -d $i/phy80211 ] ) && basename $i ; done)
+  set -a WLAN_IWCONFIG
+  for DEV in $LAN ; do
+    iwconfig $DEV 2>/dev/null 1>&2 && WLAN_IWCONFIG+=($DEV)
+  done
+  WLAN=$(echo $WLAN_OLD $WLAN_NEW ${WLAN_IWCONFIG[@]} | tr ' ' '\n' | sort -u)
+fi
+
+unset LAN_DEVICES WLAN_DEVICES FIREWIRE_DEVICES NETDEVICES WLAN_IWCONFIG
+
 while read dev mac; do
 #echo "Making NETDEVICES $NETDEVICESCOUNT $dev"
   iswlan=$(echo $dev $WLAN|tr ' ' '\n'|sort|uniq -d)
@@ -535,12 +585,18 @@ while (true); do
   IFACEDONE=""
   while [ -n "$DV" -a -z "$IFACEDONE" ]; do
     configiface
-    ifdown $DV
-    sleep 3
     if ! ifup $DV; then
       $DIALOG --yesno "$MESSAGE14" 15 50 || IFACEDONE="DONE"
     else
-      $DIALOG --yesno "$MESSAGE15" 15 50 && addauto || remauto
+      if $DIALOG --yesno "$MESSAGE15" 12 50 ; then
+         addauto
+         # get persistent interface names across reboots
+         generate_udev_entry
+        # make sure we don't run the dhcp stuff when using /etc/network/interfaces
+        disable_config_dhcp
+      else
+         remauto
+      fi
       IFACEDONE="DONE"
     fi
   done