From: Mladen Horvat Date: Wed, 30 Mar 2011 08:49:10 +0000 (+0200) Subject: SystemPlugins/WirelessLan: Don't depend on device names where possible. Better wirele... X-Git-Url: https://git.cweiske.de/enigma2.git/commitdiff_plain/26a1c69f126a59ec9aeda715ef6d516d42fd10c4 SystemPlugins/WirelessLan: Don't depend on device names where possible. Better wireless lan interface detection and scanning. Changed file naming for wpa_supplicant configuration to wpa_supplicant."interfacename".conf. refs #389 --- diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/Wlan.py b/lib/python/Plugins/SystemPlugins/WirelessLan/Wlan.py index 718adf9c..5b76f926 100755 --- a/lib/python/Plugins/SystemPlugins/WirelessLan/Wlan.py +++ b/lib/python/Plugins/SystemPlugins/WirelessLan/Wlan.py @@ -1,5 +1,6 @@ from Components.config import config, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, ConfigSelection, ConfigPassword from Components.Console import Console +from Components.Network import iNetwork from os import system, path as os_path from string import maketrans, strip @@ -29,8 +30,14 @@ config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = "WP config.plugins.wlan.encryption.wepkeytype = NoSave(ConfigSelection(weplist, default = "ASCII")) config.plugins.wlan.encryption.psk = NoSave(ConfigPassword(default = "mysecurewlan", fixed_size = False)) +def getWlanConfigName(iface): + return '/etc/wpa_supplicant.' + iface + '.conf' + class Wlan: - def __init__(self, iface): + def __init__(self, iface = None): + self.iface = iface + self.oldInterfaceState = None + a = ''; b = '' for i in range(0, 255): a = a + chr(i) @@ -39,109 +46,30 @@ class Wlan: else: b = b + chr(i) - self.iface = iface - self.wlaniface = {} - self.WlanConsole = Console() self.asciitrans = maketrans(a, b) - def stopWlanConsole(self): - if self.WlanConsole is not None: - print "[Wlan] killing self.WlanConsole" - self.WlanConsole.killAll() - self.WlanConsole = None - del self.WlanConsole - - def getDataForInterface(self, callback = None): - if len(self.WlanConsole.appContainers) == 0: - self.WlanConsole = Console() - cmd = "iwconfig " + self.iface - self.WlanConsole.ePopen(cmd, self.iwconfigFinished, callback) - - def iwconfigFinished(self, result, retval, extra_args): - callback = extra_args - data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False } - - for line in result.splitlines(): - line = line.strip() - if "ESSID" in line: - if "off/any" in line: - ssid = _("No Connection") - else: - if "Nickname" in line: - tmpssid=(line[line.index('ESSID')+7:line.index('" Nickname')]) - if tmpssid == '': - ssid = _("Hidden networkname") - elif tmpssid ==' ': - ssid = _("Hidden networkname") - else: - ssid = tmpssid - else: - tmpssid=(line[line.index('ESSID')+7:len(line)-1]) - if tmpssid == '': - ssid = _("Hidden networkname") - elif tmpssid ==' ': - ssid = _("Hidden networkname") - else: - ssid = tmpssid - if ssid is not None: - data['essid'] = ssid - if 'Frequency' in line: - frequency = line[line.index('Frequency')+10 :line.index(' GHz')] - if frequency is not None: - data['frequency'] = frequency - if "Access Point" in line: - ap=line[line.index('Access Point')+14:len(line)-1] - if ap is not None: - data['acesspoint'] = ap - if "Bit Rate" in line: - br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')] - if br is not None: - data['bitrate'] = br - if 'Encryption key' in line: - if ":off" in line: - enc = _("Disabled") - else: - enc = line[line.index('Encryption key')+15 :line.index(' Security')] - if enc is not None: - data['encryption'] = _("Enabled") - if 'Quality' in line: - if "/100" in line: - qual = line[line.index('Quality')+8:line.index('/100')] - else: - qual = line[line.index('Quality')+8:line.index('Sig')] - if qual is not None: - data['quality'] = qual - if 'Signal level' in line: - signal = line[line.index('Signal level')+13 :line.index(' dBm')] - if signal is not None: - data['signal'] = signal - - self.wlaniface[self.iface] = data - - if len(self.WlanConsole.appContainers) == 0: - print "[Wlan.py] self.wlaniface after loading:", self.wlaniface - if callback is not None: - callback(True,self.wlaniface) - - def getAdapterAttribute(self, attribute): - if self.wlaniface.has_key(self.iface): - print "[Wlan.py] self.wlaniface.has_key",self.iface - if self.wlaniface[self.iface].has_key(attribute): - return self.wlaniface[self.iface][attribute] - return None - def asciify(self, str): return str.translate(self.asciitrans) def getWirelessInterfaces(self): return getWNICnames() + def setInterface(self, iface = None): + self.iface = iface + + def getInterface(self): + return self.iface + def getNetworkList(self): - system("ifconfig "+self.iface+" up") + if self.oldInterfaceState is None: + self.oldInterfaceState = iNetwork.getAdapterAttribute(self.iface, "up") + if self.oldInterfaceState is False: + if iNetwork.getAdapterAttribute(self.iface, "up") is False: + iNetwork.setAdapterAttribute(self.iface, "up", True) + system("ifconfig "+self.iface+" up") + ifobj = Wireless(self.iface) # a Wireless NIC Object - #Association mappings - #stats, quality, discard, missed_beacon = ifobj.getStatistics() - #snr = quality.signallevel - quality.noiselevel + try: scanresults = ifobj.scan() except: @@ -170,7 +98,6 @@ class Wlan: element = element.encode() extra.append( strip(self.asciify(element)) ) for element in extra: - print element if 'SignalStrength' in element: signal = element[element.index('SignalStrength')+15:element.index(',L')] if 'LinkQuality' in element: @@ -193,92 +120,79 @@ class Wlan: index = index + 1 return aps - def getStatus(self): - ifobj = Wireless(self.iface) - fq = Iwfreq() - try: - self.channel = str(fq.getChannel(str(ifobj.getFrequency()[0:-3]))) - except: - self.channel = 0 - status = { - 'BSSID': str(ifobj.getAPaddr()), #ifobj.getStatistics() - 'ESSID': str(ifobj.getEssid()), - 'quality': "%s/%s" % (ifobj.getStatistics()[1].quality,ifobj.getQualityMax().quality), - 'signal': str(ifobj.getStatistics()[1].siglevel-0x100) + " dBm", - 'bitrate': str(ifobj.getBitrate()), - 'channel': str(self.channel), - #'channel': str(fq.getChannel(str(ifobj.getFrequency()[0:-3]))), - } - for (key, item) in status.items(): - if item is "None" or item is "": - status[key] = _("N/A") - return status + def stopGetNetworkList(self): + if self.oldInterfaceState is not None: + if self.oldInterfaceState is False: + iNetwork.setAdapterAttribute(self.iface, "up", False) + system("ifconfig "+self.iface+" down") + self.oldInterfaceState = None + self.iface = None +iWlan = Wlan() class wpaSupplicant: - def __init__(self, iface): - self.iface = iface + def __init__(self): pass - def writeConfig(self): - essid = config.plugins.wlan.essid.value - hiddenessid = config.plugins.wlan.hiddenessid.value - encrypted = config.plugins.wlan.encryption.enabled.value - encryption = config.plugins.wlan.encryption.type.value - wepkeytype = config.plugins.wlan.encryption.wepkeytype.value - psk = config.plugins.wlan.encryption.psk.value - fp = file('/etc/' + self.iface + '_wpa_supplicant.conf', 'w') - fp.write('#WPA Supplicant Configuration by enigma2\n') - fp.write('ctrl_interface=/var/run/wpa_supplicant\n') - fp.write('eapol_version=1\n') - fp.write('fast_reauth=1\n') - if essid == 'hidden...': - fp.write('ap_scan=2\n') - else: - fp.write('ap_scan=1\n') - fp.write('network={\n') - if essid == 'hidden...': - fp.write('\tssid="'+hiddenessid+'"\n') - else: - fp.write('\tssid="'+essid+'"\n') - fp.write('\tscan_ssid=0\n') - if encrypted: - if encryption in ('WPA', 'WPA2', 'WPA/WPA2'): - fp.write('\tkey_mgmt=WPA-PSK\n') + def writeConfig(self, iface): + essid = config.plugins.wlan.essid.value + hiddenessid = config.plugins.wlan.hiddenessid.value + encrypted = config.plugins.wlan.encryption.enabled.value + encryption = config.plugins.wlan.encryption.type.value + wepkeytype = config.plugins.wlan.encryption.wepkeytype.value + psk = config.plugins.wlan.encryption.psk.value + fp = file(getWlanConfigName(iface), 'w') + fp.write('#WPA Supplicant Configuration by enigma2\n') + fp.write('ctrl_interface=/var/run/wpa_supplicant\n') + fp.write('eapol_version=1\n') + fp.write('fast_reauth=1\n') + if essid == 'hidden...': + fp.write('ap_scan=2\n') + else: + fp.write('ap_scan=1\n') + fp.write('network={\n') + if essid == 'hidden...': + fp.write('\tssid="'+hiddenessid+'"\n') + else: + fp.write('\tssid="'+essid+'"\n') + fp.write('\tscan_ssid=0\n') + if encrypted: + if encryption in ('WPA', 'WPA2', 'WPA/WPA2'): + fp.write('\tkey_mgmt=WPA-PSK\n') - if encryption == 'WPA': - fp.write('\tproto=WPA\n') - fp.write('\tpairwise=TKIP\n') - fp.write('\tgroup=TKIP\n') - elif encryption == 'WPA2': - fp.write('\tproto=RSN\n') - fp.write('\tpairwise=CCMP\n') - fp.write('\tgroup=CCMP\n') - else: - fp.write('\tproto=WPA RSN\n') - fp.write('\tpairwise=CCMP TKIP\n') - fp.write('\tgroup=CCMP TKIP\n') - fp.write('\tpsk="'+psk+'"\n') - elif encryption == 'WEP': - fp.write('\tkey_mgmt=NONE\n') - if wepkeytype == 'ASCII': - fp.write('\twep_key0="'+psk+'"\n') - else: - fp.write('\twep_key0='+psk+'\n') - else: - fp.write('\tkey_mgmt=NONE\n') - fp.write('}') - fp.write('\n') - fp.close() - system('cat /etc/' + self.iface + '_wpa_supplicant.conf') + if encryption == 'WPA': + fp.write('\tproto=WPA\n') + fp.write('\tpairwise=TKIP\n') + fp.write('\tgroup=TKIP\n') + elif encryption == 'WPA2': + fp.write('\tproto=RSN\n') + fp.write('\tpairwise=CCMP\n') + fp.write('\tgroup=CCMP\n') + else: + fp.write('\tproto=WPA RSN\n') + fp.write('\tpairwise=CCMP TKIP\n') + fp.write('\tgroup=CCMP TKIP\n') + fp.write('\tpsk="'+psk+'"\n') + elif encryption == 'WEP': + fp.write('\tkey_mgmt=NONE\n') + if wepkeytype == 'ASCII': + fp.write('\twep_key0="'+psk+'"\n') + else: + fp.write('\twep_key0='+psk+'\n') + else: + fp.write('\tkey_mgmt=NONE\n') + fp.write('}') + fp.write('\n') + fp.close() + system('cat ' + getWlanConfigName(iface)) - def loadConfig(self): - configfile = '/etc/wpa_supplicant.conf' - if os_path.isfile('/etc/' + self.iface + '_wpa_supplicant.conf'): - configfile = '/etc/' + self.iface + '_wpa_supplicant.conf' - print "[wpaSupplicant] using configfile:",configfile + def loadConfig(self,iface): + configfile = getWlanConfigName(iface) + if not os_path.exists(configfile): + configfile = '/etc/wpa_supplicant.conf' try: #parse the wpasupplicant configfile + print "[Wlan.py] parsing configfile: ",configfile fp = file(configfile, 'r') supplicant = fp.readlines() fp.close() @@ -288,14 +202,14 @@ class wpaSupplicant: for s in supplicant: split = s.strip().split('=',1) if split[0] == 'ap_scan': - print "[Wlan.py] Got Hidden SSID Scan Value "+split[1] + print "[Wlan.py] Got Hidden SSID Scan Value ",split[1] if split[1] == '2': ap_scan = True else: ap_scan = False elif split[0] == 'ssid': - print "[Wlan.py] Got SSID "+split[1][1:-1] + print "[Wlan.py] Got SSID ",split[1][1:-1] essid = split[1][1:-1] elif split[0] == 'proto': diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/plugin.py b/lib/python/Plugins/SystemPlugins/WirelessLan/plugin.py index 290d139b..c296ab70 100644 --- a/lib/python/Plugins/SystemPlugins/WirelessLan/plugin.py +++ b/lib/python/Plugins/SystemPlugins/WirelessLan/plugin.py @@ -15,10 +15,10 @@ from os import system, path as os_path, listdir from Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE from Tools.LoadPixmap import LoadPixmap from Tools.HardwareInfo import HardwareInfo -from Wlan import Wlan, wpaSupplicant, iStatus -import sha +from Wlan import iWlan, wpaSupplicant, iStatus, getWlanConfigName +import hashlib from time import time -from os import urandom +from os import urandom, system plugin_path = eEnv.resolve("${libdir}/enigma2/python/Plugins/SystemPlugins/WirelessLan") @@ -219,6 +219,8 @@ class WlanScan(Screen): "red": self.cancel, "green": self.select, }) + iWlan.setInterface(self.iface) + self.w = iWlan.getInterface() self.onLayoutFinish.append(self.layoutFinished) self.getAccessPoints(refresh = False) @@ -228,6 +230,7 @@ class WlanScan(Screen): def select(self): cur = self["list"].getCurrent() if cur is not None: + iWlan.stopGetNetworkList() self.rescanTimer.stop() del self.rescanTimer if cur[1] is not None: @@ -239,31 +242,16 @@ class WlanScan(Screen): else: self.close(None,None) else: + iWlan.stopGetNetworkList() self.rescanTimer.stop() del self.rescanTimer self.close(None,None) - def WlanSetupClosed(self, *ret): - if ret[0] == 2: - self.rescanTimer.stop() - del self.rescanTimer - self.close(None) - def cancel(self): - if self.oldInterfaceState is False: - iNetwork.setAdapterAttribute(self.iface, "up", False) - iNetwork.deactivateInterface(self.iface,self.deactivateInterfaceCB) - else: - self.rescanTimer.stop() - del self.rescanTimer - self.close(None) - - def deactivateInterfaceCB(self,data): - if data is not None: - if data is True: - self.rescanTimer.stop() - del self.rescanTimer - self.close(None) + iWlan.stopGetNetworkList() + self.rescanTimer.stop() + del self.rescanTimer + self.close(None) def rescanTimerFired(self): self.rescanTimer.stop() @@ -314,8 +302,7 @@ class WlanScan(Screen): def getAccessPoints(self, refresh = False): self.APList = [] self.cleanList = [] - self.w = Wlan(self.iface) - aps = self.w.getNetworkList() + aps = iWlan.getNetworkList() if aps is not None: print "[WirelessLan.py] got Accespoints!" tmpList = [] @@ -364,7 +351,7 @@ class WlanScan(Screen): self.WlanList = [] for entry in self['list'].list: if entry[1] == "hidden...": - self.WlanList.append(( "hidden...",_("enter hidden network SSID") ))#continue + self.WlanList.append(( "hidden...",_("enter hidden network SSID") )) else: self.WlanList.append( (entry[0], entry[0]) ) @@ -393,7 +380,7 @@ def decrypt_block(src, mod): if len(src) != 128 and len(src) != 202: return None dest = rsa_pub1024(src[:128], mod) - hash = sha.new(dest[1:107]) + hash = hashlib.sha1(dest[1:107]) if len(src) == 202: hash.update(src[131:192]) result = hash.digest() @@ -422,16 +409,12 @@ def WlanStatusScreenMain(session, iface): session.open(WlanStatus, iface) def callFunction(iface): - w = Wlan(iface) - i = w.getWirelessInterfaces() + iWlan.setInterface(iface) + i = iWlan.getWirelessInterfaces() if i: - if iface in i: + if iface in i or iNetwork.isWirelessInterface(iface): return WlanStatusScreenMain - else: - if iNetwork.isWirelessInterface(iface): - return WlanStatusScreenMain - else: - return None + return None return None def configStrings(iface): @@ -462,9 +445,9 @@ def configStrings(iface): driver = 'dreambox' print 'Using "%s" as wpa-supplicant driver' % (driver) ret = "" - if config.plugins.wlan.essid.value == "hidden...": + if driver == 'madwifi' and config.plugins.wlan.essid.value == "hidden...": ret += "\tpre-up iwconfig " + iface + " essid \"" + config.plugins.wlan.hiddenessid.value + "\" || true\n" - ret += "\tpre-up wpa_supplicant -i" + iface + " -c/etc/" + iface + "_wpa_supplicant.conf -B -dd -D" + driver + " || true\n" + ret += "\tpre-up wpa_supplicant -i" + iface + " -c" + getWlanConfigName(iface) + " -B -dd -D" + driver + " || true\n" ret += "\tpre-down wpa_cli -i" + iface + " terminate || true\n" return ret