From 55162685e6bc46ba6ab048a1b69cffbab82a4354 Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Sun, 14 Jun 2009 12:21:08 -0500
Subject: linhes-config: add support for WPA/WEP/NONE using wpa_supplicant.
 Currently it only works when using dhcp, but this is an upstream problem with
 etcnet. new mythvantage

---
 abs/core-testing/LinHES-config/PKGBUILD      |   2 +-
 abs/core-testing/LinHES-config/mv_network.py | 103 +++++++++++++++++++++++----
 2 files changed, 92 insertions(+), 13 deletions(-)

diff --git a/abs/core-testing/LinHES-config/PKGBUILD b/abs/core-testing/LinHES-config/PKGBUILD
index 2091f09..36a9184 100755
--- a/abs/core-testing/LinHES-config/PKGBUILD
+++ b/abs/core-testing/LinHES-config/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-config
 pkgver=2.0
-pkgrel=10
+pkgrel=11
 conflicts=(MythVantage-config MythVantage-config-dev LinHES-config-dev )
 pkgdesc="Install and configure your system"
 depends=(bc libstatgrab  mysql-python expect curl dnsutils parted sg3_utils nmbscan system-templates rsync python-parted ddcxinfo)
diff --git a/abs/core-testing/LinHES-config/mv_network.py b/abs/core-testing/LinHES-config/mv_network.py
index ca29c7d..3978f95 100755
--- a/abs/core-testing/LinHES-config/mv_network.py
+++ b/abs/core-testing/LinHES-config/mv_network.py
@@ -82,6 +82,64 @@ def setup_hostname(systemconfig):
     cmd = '''cat /tmp/hosts >> /etc/hosts '''
     mv_common.runcmd(cmd)
 
+
+def write_wpafile(line,netdev):
+    logging.debug("    writing out wpa_supplicant.conf file")
+    logging.debug(line)
+    wpafile = etcnetdir+"/"+netdev+"/wpa_supplicant.conf"
+    try:
+        f = open(wpafile,'w')
+        f.write(line)
+        f.close()
+    except:
+        logging.debug("    Couldn't write to %s",wpafile)
+
+def setup_wpa(netinfo,netdev):
+    logging.info("    %s will use wpa for encryption",netdev)
+    line = '''
+#This file was automaticly generated, any changes may be lost
+ctrl_interface=/var/run/wpa_supplicant
+network={
+    ssid="%s"
+    # Preshared key as an ASCII passphrase
+    psk="%s"
+    scan_ssid=1
+    key_mgmt=WPA-EAP WPA-PSK NONE
+    pairwise=CCMP TKIP
+    proto=WPA RSN
+}''' %(netinfo["ESSID"],netinfo["KEY"])
+    write_wpafile(line,netdev)
+
+def setup_wep(netinfo,netdev):
+    logging.info("    %s will use wep for encryption",netdev)
+    logging.debug("    When a packet comes along, you must wep it")
+    line='''
+#This file was automaticly generated, any changes may be lost
+ctrl_interface=/var/run/wpa_supplicant
+network={
+        ssid="%s"
+        key_mgmt=NONE
+        wep_key0=%s
+        wep_tx_keyidx=0
+        scan_ssid=1
+        auth_alg=SHARED
+}
+''' %(netinfo["ESSID"],netinfo["KEY"])
+    write_wpafile(line,netdev)
+
+
+def setup_none(netinfo,netdev):
+    logging.info("    %s will not use encryption",netdev)
+    line = '''
+#This file was automaticly generated, any changes may be lost
+ctrl_interface=/var/run/wpa_supplicant
+network={
+        ssid="%s"
+        key_mgmt=NONE
+}
+''' %netinfo["ESSID"]
+    write_wpafile(line,netdev)
+
 def setup_interface(netdev,systemconfig):
     logging.debug("    _Start of setup_interface for %s",netdev)
     nettrans = {'Hostip':'ip',
@@ -93,7 +151,7 @@ def setup_interface(netdev,systemconfig):
                 'HostMTU':'mtu',
                 'HOST_iswireless':"wireless",
                 'HostESSID':'ESSID',
-                'HostKey':'key',
+                'HostKey':'KEY',
                 'HostUseEncryption':'ENCRYPT'}
     netinfo = {}
     #populate the netinfo dict
@@ -120,20 +178,26 @@ def setup_interface(netdev,systemconfig):
         change_iface_state(netdev,"disabled")
 
     if netinfo["wireless"] == "1" :
+
         logging.info("    Enabling wireless extensions")
         cmd = ''' sed -i -e 's/^CONFIG_WIRELESS=.*$/CONFIG_WIRLESS=yes/g'  %s/%s/options''' %(etcnetdir,netdev)
         mv_common.runcmd(cmd)
-        #set the key and essid
-        cmd = '''  echo "essid  %s" > %s/%s/iwconfig ''' %(netinfo["ESSID"], etcnetdir, netdev)
-        mv_common.runcmd(cmd)
-        if netinfo["ENCRYPT"] == "1":
-            logging.info("    Enabling encryption")
-            cmd = '''echo "key %s" >>  %s/%s/iwconfig ''' %(netinfo["KEY"], etcnetdir, netdev)
-            mv_common.runcmd(cmd)
+        if netinfo["ENCRYPT"] == "WEP":
+            setup_wep(netinfo,netdev)
+        elif netinfo["ENCRYPT"] == "WPA":
+            setup_wpa(netinfo,netdev)
+        elif netinfo["ENCRYPT"] == "none":
+            setup_none(netinfo,netdev)
+
     else:
         logging.info("    Disabling wireless extensions")
         cmd = '''sed -i -e 's/^CONFIG_WIRLESS=.*$/CONFIG_WIRLESS=no/g' %s/%s/options''' %(etcnetdir, netdev)
         mv_common.runcmd(cmd)
+        try:
+            wpafile=etcnetdir+"/"+netdev+"/wpa_supplicant.conf"
+            os.remove(wpafile)
+        except:
+            logging.debug("    Couldn't remove %s",wpafile)
 
     if netinfo["UseDHCP"] == "0" :
         logging.info("    Enabling DHCP support")
@@ -179,8 +243,16 @@ def change_iface_state(netdev, state):
         cmd = "ip address flush %s" %netdev
         mv_common.runcmd(cmd)
 
+def devcheck(netdev):
+    cmd = ''' /sbin/ifconfig %s''' %netdev
+    rc = mv_common.runcmd(cmd)
+    if rc == 0 :
+        return True
+    else:
+        return False
+
 def find_active(systemconfig):
-    interfacelist=('eth0', 'eth1', 'wlan0', 'wlan1')
+    interfacelist=('eth0', 'eth1', 'wlan0', 'wlan1', 'ath0')
     logging.debug("    _Start of find_active")
 
     if systemconfig["mythdhcp"] == "1":
@@ -199,9 +271,15 @@ def find_active(systemconfig):
             systemconfig[currentnet]
         except:
             logging.debug("    %s is not defined",currentnet)
+            change_iface_state(netdev, "disabled")
             continue
         if systemconfig[currentnet] == "1" :
-            setup_interface(netdev,systemconfig)
+            #check if device is present
+            if devcheck(netdev) :
+                setup_interface(netdev,systemconfig)
+            else:
+                logging.debug("    Interface %s not found in config", netdev)
+                #change_iface_state(netdev, "disabled")
         else:
             change_iface_state(netdev, "disabled")
 
@@ -232,7 +310,7 @@ def start_network():
         logging.info("    Restarting network")
         cmd ="/etc/net/scripts/network.init reload"
         mv_common.runcmd(cmd)
-        cmd ="/etc/net/scripts/network.init reload"
+        cmd ="/etc/net/scripts/network.init restart"
         mv_common.runcmd(cmd)
     else:
         logging.info("    Will not restart network due to netboot/vnc")
@@ -244,7 +322,7 @@ def stop_network():
         cmd ="/etc/net/scripts/network.init stop"
         mv_common.runcmd(cmd)
         kill_dhcp("")
-        interfacelist=('eth0', 'eth1', 'wlan0', 'wlan1')
+        interfacelist=('eth0', 'eth1', 'wlan0', 'wlan1', 'ath0')
         for i in interfacelist:
             flush(i)
     else:
@@ -258,6 +336,7 @@ def setup_network (systemconfig):
     logging.info("    Setting up the network")
     setup_hostname(systemconfig)
     find_active(systemconfig)
+    start_network()
     logging.info("__End of network\n")
 
 
-- 
cgit v0.12

