def run(self): for conn in self.conns: if self.run_flag: ssid = TypeConvert.ssid_ascii2string(conn.settings_dict["802-11-wireless"]["ssid"]) if ssid in self.device.get_ssid_record(): try: specific = self.device.get_ap_by_ssid(ssid) nmclient = get_cache().getobject("/org/freedesktop/NetworkManager") active_conn = nmclient.activate_connection(conn.object_path, self.device.object_path, specific.object_path) while(active_conn.get_state() == 1 and self.run_flag): time.sleep(1) if active_conn.get_state() == 2: self.stop_run() return True else: continue except: pass else: continue else: return False self.stop_run()
def run(self): for conn in self.conns: if self.run_flag: ssid = TypeConvert.ssid_ascii2string( conn.settings_dict["802-11-wireless"]["ssid"]) if ssid in self.device.get_ssid_record(): try: specific = self.device.get_ap_by_ssid(ssid) nmclient = get_cache().getobject( "/org/freedesktop/NetworkManager") active_conn = nmclient.activate_connection( conn.object_path, self.device.object_path, specific.object_path) while (active_conn.get_state() == 1 and self.run_flag): time.sleep(1) if active_conn.get_state() == 2: self.stop_run() return True else: continue except: pass else: continue else: return False self.stop_run()
def active_ssid_connection(self, ssid): '''try only one connection now''' if get_cache().getobject(self.object_path).is_active(): conn = get_cache().getobject(self.object_path).get_active_connection().get_connection() if TypeConvert.ssid_ascii2string(conn.settings_dict["802-11-wireless"]["ssid"])== ssid: return None else: pass nm_remote_settings = get_cache().getobject("/org/freedesktop/NetworkManager/Settings") connections = nm_remote_settings.get_ssid_associate_connections(ssid) #print connections, "active ssid connection" if connections: conn = sorted(connections, key = lambda x: int(nm_remote_settings.cf.get("conn_priority", x.settings_dict["connection"]["uuid"])), reverse = True)[0] try: specific = self.get_ap_by_ssid(ssid) nmclient = get_cache().getobject("/org/freedesktop/NetworkManager") nmclient.activate_connection(conn.object_path, self.object_path, specific.object_path) if get_cache().getobject(self.object_path).is_active(): return None else: return conn except: return conn else: return nm_remote_settings.new_wireless_connection(ssid, None)
def connection_compatible(self, connection): info_dict = TypeConvert.dbus2py(connection.settings_dict) if info_dict["connection"]["type"] != "802-11-wireless": return False if "802-11-wireless" not in info_dict.iterkeys(): return False if self.get_perm_hw_address(): if "mac-address" in info_dict.iterkeys(): if self.get_perm_hw_address() != info_dict["802-11-wireless"]["mac-address"]: return False if "802-11-wireless-security" in info_dict.iterkeys(): if info_dict["802-11-wireless-security"]["key-mgmt"] in ["wpa-none", "wpa-psk", "wpa-eap"]: caps = self.get_capabilities() if not caps & 4 or not caps & 8 or not caps & 16 or not caps & 32: return False protos = info_dict["802-11-wireless-security"]["proto"] if protos: if "rsn" in protos and "wpa" not in protos: if not caps & 8 or not caps & 32: return False return True
def run(self): for conn in self.conns: if self.run_flag: if "mac-address" in conn.settings_dict[ "802-3-ethernet"].iterkeys(): if TypeConvert.dbus2py( conn.settings_dict["802-3-ethernet"] ["mac-address"]) != self.device.get_hw_address(): print "connection mac address doesn't match the device" continue else: pass try: nmclient = get_cache().getobject( "/org/freedesktop/NetworkManager") active_conn = nmclient.activate_connection( conn.object_path, self.device.object_path, "/") while (active_conn.get_state() == 1 and self.run_flag): time.sleep(1) if active_conn.get_state() == 2: self.stop_run() return True else: continue except: pass else: return True self.stop_run() return True
def get_ssid_associate_connections(self, ssid): '''return wireless connection objects have the given ssid''' try: return filter(lambda x: TypeConvert.ssid_ascii2string(x.settings_dict["802-11-wireless"]["ssid"]) == ssid, self.get_wireless_connections()) except: return []
def connection_compatible(self, connection): info_dict = TypeConvert.dbus2py(connection.settings_dict) if info_dict["connection"]["type"] != "802-11-wireless": return False if "802-11-wireless" not in info_dict.iterkeys(): return False if self.get_perm_hw_address(): if "mac-address" in info_dict.iterkeys(): if self.get_perm_hw_address( ) != info_dict["802-11-wireless"]["mac-address"]: return False if "802-11-wireless-security" in info_dict.iterkeys(): if info_dict["802-11-wireless-security"]["key-mgmt"] in [ "wpa-none", "wpa-psk", "wpa-eap" ]: caps = self.get_capabilities() if not caps & 4 or not caps & 8 or not caps & 16 or not caps & 32: return False protos = info_dict["802-11-wireless-security"]["proto"] if protos: if "rsn" in protos and "wpa" not in protos: if not caps & 8 or not caps & 32: return False return True
def run(self): for conn in self.conns: if self.run_flag: if "mac-address" in conn.settings_dict["802-3-ethernet"].iterkeys(): if TypeConvert.dbus2py(conn.settings_dict["802-3-ethernet"]["mac-address"]) != self.device.get_hw_address(): print "connection mac address doesn't match the device" continue else: pass try: nmclient = get_cache().getobject("/org/freedesktop/NetworkManager") active_conn = nmclient.activate_connection(conn.object_path, self.device.object_path, "/") while(active_conn.get_state() == 1 and self.run_flag): time.sleep(1) if active_conn.get_state() == 2: self.stop_run() return True else: continue except: pass else: return True self.stop_run() return True
def init_properties(self): try: return TypeConvert.dbus2py(self.properties_interface.GetAll(self.object_interface)) except dbus.exceptions.DBusException: prop = {} for key in self.prop_list: prop[key] = "" return prop
def get_ssid_not_associate_connections(self, ssid): try: return filter( lambda x: TypeConvert.ssid_ascii2string(x.settings_dict[ "802-11-wireless"]["ssid"]) != ssid, self.get_wireless_connections()) except: return []
def list_connections(self): '''return connections object''' try: return filter(lambda x: "read-only" not in x.settings_dict["connection"] or not x.settings_dict["connection"]["read-only"], map(lambda x:get_cache().getobject(x), TypeConvert.dbus2py(self.dbus_method("ListConnections"))) ) except: traceback.print_exc() return []
def get_ssid_associate_connections(self, ssid): '''return wireless connection objects have the given ssid''' try: return filter( lambda x: TypeConvert.ssid_ascii2string(x.settings_dict[ "802-11-wireless"]["ssid"]) == ssid, self.get_wireless_connections()) except: return []
def init_properties(self): try: return TypeConvert.dbus2py( self.properties_interface.GetAll(self.object_interface)) except dbus.exceptions.DBusException: prop = {} for key in self.prop_list: prop[key] = "" return prop
def list_connections(self): '''return connections object''' try: return filter( lambda x: "read-only" not in x.settings_dict["connection"] or not x.settings_dict["connection"]["read-only"], map(lambda x: get_cache().getobject(x), TypeConvert.dbus2py(self.dbus_method("ListConnections")))) except: traceback.print_exc() return []
def get_access_points(self): try: ap = self.dbus_method("GetAccessPoints") if not self.ap_record_inited: self.ap_record_inited = 1 try: from nmaccesspoint import NMAccessPoint for ap_path in ap: self.ap_record_dict[ap_path] = NMAccessPoint(ap_path).get_ssid() except: self.ap_recrod_dict = {} return map(lambda x:get_cache().getobject(x), TypeConvert.dbus2py(ap)) except: return []
def get_access_points(self): try: ap = self.dbus_method("GetAccessPoints") if not self.ap_record_inited: self.ap_record_inited = 1 try: from nmaccesspoint import NMAccessPoint for ap_path in ap: self.ap_record_dict[ap_path] = NMAccessPoint( ap_path).get_ssid() except: self.ap_recrod_dict = {} return map(lambda x: get_cache().getobject(x), TypeConvert.dbus2py(ap)) except: return []
def active_ssid_connection(self, ssid): '''try only one connection now''' if get_cache().getobject(self.object_path).is_active(): conn = get_cache().getobject( self.object_path).get_active_connection().get_connection() if TypeConvert.ssid_ascii2string( conn.settings_dict["802-11-wireless"]["ssid"]) == ssid: return None else: pass nm_remote_settings = get_cache().getobject( "/org/freedesktop/NetworkManager/Settings") connections = nm_remote_settings.get_ssid_associate_connections(ssid) #print connections, "active ssid connection" if connections: conn = sorted(connections, key=lambda x: int( nm_remote_settings.cf.get( "conn_priority", x.settings_dict["connection" ]["uuid"])), reverse=True)[0] try: specific = self.get_ap_by_ssid(ssid) nmclient = get_cache().getobject( "/org/freedesktop/NetworkManager") nmclient.activate_connection(conn.object_path, self.object_path, specific.object_path) if get_cache().getobject(self.object_path).is_active(): return None else: return conn except: return conn else: return nm_remote_settings.new_wireless_connection(ssid, None)
def is_connection_valid(self, connection): info_dict = TypeConvert.dbus2py(connection.settings_dict) if info_dict["connection"]["type"] != "802-11-wireless": return False if "802-11-wireless" not in info_dict.iterkeys(): return False if self.get_ssid() != info_dict["802-11-wireless"]["ssid"]: return False if self.get_bssid() != None and info_dict["802-11-wireless"]["bssid"] != None: pass if self.get_mode() != None: if self.get_mode() == 0 or "mode" not in info_dict["802-11-wireless"].iterkeys(): return False if self.get_mode() == 1 and info_dict["802-11-wireless"]["mode"] !="adhoc": return False if self.get_mode() == 2 and info_dict["802-11-wireless"]["mode"] != "infrastructure": return False if self.get_frequency() > 0: if info_dict["802-11-wireless"]["band"] == "a": if self.get_frequency() < 4915 or self.get_frequency() > 5825: return False if info_dict["802-11-wireless"]["band"] == "bg": if self.get_frequency() < 2412 or self.get_frequency() > 2484: return False if "channel" in info_dict["802-11-wireless"].iterkeys(): if self.get_channel() != info_dict["802-11-wireless"]["channel"]: return False ###assert wireless security if self.get_flags() != None: flags = self.get_flags() else: flags = 0 if self.get_wpa_flags() != None: wpa_flags = self.get_wpa_flags() else: wpa_flags = 0 if self.get_rsn_flags() != None: rsn_flags = self.get_rsn_flags() else: rsn_flags = 0 if not "802-11-wireless-security" in info_dict.iterkeys(): if flags == 1 or wpa_flags !=0 or rsn_flags != 0: return False return True else: if "key-mgmt" not in info_dict["802-11-wireless-security"].iterkeys(): return False ###static wep elif info_dict["802-11-wireless-security"]["key-mgmt"] == "none": if not flags & 1 or wpa_flags != 0 or rsn_flags != 0: return False return True ###adhoc wpa elif info_dict["802-11-wireless-security"]["key-mgmt"] == "wpa-none": if self.get_mode() != 1: return False return True # stuff after this point requires infrastructure else: if self.get_mode() != 2: return False ###dynamic wep or leap if self.info_dict["802-11-wireless-security"]["key-mgmt"] == "ieee8021x": if not flags & 1: return False # if the ap is advertising a WPA IE, make sure it supports WEP ciphers if wpa_flags != 0: if not wpa_flags & 512: return False # quick check; can't use AP if it doesn't support at least one # WEP cipher in both pairwise and group suites. if not (wpa_flags & (1 | 2)) or not (wpa_flags & (16 | 32)): return False # Match at least one pairwise cipher with AP's capability if the # wireless-security setting explicitly lists pairwise ciphers if "pairwise" in self.info_dict["802-11-wireless-security"].iterkeys(): if self.info_dict["802-11-wireless-security"]["pairwise"]: found = False for cipher in self.info_dict["802-11-wireless-security"]["pairwise"]: if self.match_cipher(cipher, "wep40", wpa_flags, wpa_flags, 1): found = True break if self.match_cipher(cipher, "wep104", wpa_flags, wpa_flags, 2): found = True break if not found: return False else: pass else: pass # Match at least one group cipher with AP's capability if the # wireless-security setting explicitly lists group ciphers if "group" in self.info_dict["802-11-wireless-security"].iterkeys(): if self.info_dict["802-11-wireless-security"]["group"]: found = False for cipher in self.info_dict["802-11-wireless-security"]["group"]: if self.match_cipher(cipher, "wep40", wpa_flags, wpa_flags, 16): found = True break if self.match_cipher(cipher, "wep104", wpa_flags, wpa_flags, 32): found = True break if not found: return False else: pass else: pass return True # WPA[2]-PSK and WPA[2] Enterprise elif self.info_dict["802-11-wireless-security"]["key-mgmt"] == "wpa-psk" or "wpa-eap": if self.info_dict["802-11-wireless-security"]["key-mgmt"] == "wpa-psk": if not (wpa_flags & 256) and not (rsn_flags & 256): return False elif self.info_dict["802-11-wireless-security"]["key-mgmt"] == "wpa-eap": if not (wpa_flags & 512) and not (rsn_flags & 512): return False # FIXME: should handle WPA and RSN separately here to ensure that # if the Connection only uses WPA we don't match a cipher against # the AP's RSN IE instead # Match at least one pairwise cipher with AP's capability if the # wireless-security setting explicitly lists pairwise ciphers if "pairwise" in self.info_dict["802-11-wireless-security"].iterkeys(): if self.info_dict["802-11-wireless-security"]["pairwise"]: found = False for cipher in self.info_dict["802-11-wireless-security"]["pairwise"]: if self.match_cipher(cipher, "tkip", wpa_flags, rsn_flags, 8): found = True break if self.match_cipher(cipher, "ccmp", wpa_flags, rsn_flags, 10): found = True break if not found: return False else: pass else: pass # Match at least one group cipher with AP's capability if the # wireless-security setting explicitly lists group ciphers if "group" in self.info_dict["802-11-wireless-security"].iterkeys(): if self.info_dict["802-11-wireless-security"]["group"]: found = False for cipher in self.info_dict["802-11-wireless-security"]["group"]: if self.match_cipher(cipher, "wep40", wpa_flags, rsn_flags, 16): found = True break if self.match_cipher(cipher, "wep104", wpa_flags, rsn_flags, 32): found = True break if self.match_cipher(cipher, "tkip", wpa_flags, rsn_flags, 64): found = True break if self.match_cipher(cipher, "ccmp", wpa_flags,rsn_flags, 128): found = True break if not found: return False else: pass else: pass return True return True
def get_ip4address(self): return TypeConvert.ip4_net2native(self.properties["Ip4Address"])
def get_devices(self): '''return father device objects''' try: return map(lambda x: get_cache().getobject(x), TypeConvert.dbus2py(self.dbus_method("GetDevices"))) except: return []
def properties_changed_cb(self, prop_dict): if "ActiveConnections" in prop_dict.keys() and len(prop_dict["ActiveConnections"]) != len(self.properties["ActiveConnections"]): print TypeConvert.dbus_dictionary_2py(prop_dict)["ActiveConnections"] prop_dict = map(lambda x: get_cache().getobject(x), TypeConvert.dbus_dictionary_2py(prop_dict)["ActiveConnections"]) nm_events.emit("properties_changed", prop_dict, obj=self) self.init_nmobject_with_properties()
def state_changed_cb(self, state): self.emit("state-changed", TypeConvert.dbus2py(state))
def get_routes(self): return [ TypeConvert.ip4route_net2native(item) for item in self.properties["Routes"] ]
def get_addresses(self): return [ TypeConvert.ip4address_net2native(item) for item in self.properties["Addresses"] ]
def get_ssid (self): if self.properties["Ssid"]: return TypeConvert.ssid_ascii2string(self.properties["Ssid"]) else: return None
def is_connection_valid(self, connection): info_dict = TypeConvert.dbus2py(connection.settings_dict) if info_dict["connection"]["type"] != "802-11-wireless": return False if "802-11-wireless" not in info_dict.iterkeys(): return False if self.get_ssid() != info_dict["802-11-wireless"]["ssid"]: return False if self.get_bssid( ) != None and info_dict["802-11-wireless"]["bssid"] != None: pass if self.get_mode() != None: if self.get_mode( ) == 0 or "mode" not in info_dict["802-11-wireless"].iterkeys(): return False if self.get_mode( ) == 1 and info_dict["802-11-wireless"]["mode"] != "adhoc": return False if self.get_mode() == 2 and info_dict["802-11-wireless"][ "mode"] != "infrastructure": return False if self.get_frequency() > 0: if info_dict["802-11-wireless"]["band"] == "a": if self.get_frequency() < 4915 or self.get_frequency() > 5825: return False if info_dict["802-11-wireless"]["band"] == "bg": if self.get_frequency() < 2412 or self.get_frequency() > 2484: return False if "channel" in info_dict["802-11-wireless"].iterkeys(): if self.get_channel( ) != info_dict["802-11-wireless"]["channel"]: return False ###assert wireless security if self.get_flags() != None: flags = self.get_flags() else: flags = 0 if self.get_wpa_flags() != None: wpa_flags = self.get_wpa_flags() else: wpa_flags = 0 if self.get_rsn_flags() != None: rsn_flags = self.get_rsn_flags() else: rsn_flags = 0 if not "802-11-wireless-security" in info_dict.iterkeys(): if flags == 1 or wpa_flags != 0 or rsn_flags != 0: return False return True else: if "key-mgmt" not in info_dict[ "802-11-wireless-security"].iterkeys(): return False ###static wep elif info_dict["802-11-wireless-security"]["key-mgmt"] == "none": if not flags & 1 or wpa_flags != 0 or rsn_flags != 0: return False return True ###adhoc wpa elif info_dict["802-11-wireless-security"][ "key-mgmt"] == "wpa-none": if self.get_mode() != 1: return False return True # stuff after this point requires infrastructure else: if self.get_mode() != 2: return False ###dynamic wep or leap if self.info_dict["802-11-wireless-security"][ "key-mgmt"] == "ieee8021x": if not flags & 1: return False # if the ap is advertising a WPA IE, make sure it supports WEP ciphers if wpa_flags != 0: if not wpa_flags & 512: return False # quick check; can't use AP if it doesn't support at least one # WEP cipher in both pairwise and group suites. if not (wpa_flags & (1 | 2)) or not (wpa_flags & (16 | 32)): return False # Match at least one pairwise cipher with AP's capability if the # wireless-security setting explicitly lists pairwise ciphers if "pairwise" in self.info_dict[ "802-11-wireless-security"].iterkeys(): if self.info_dict["802-11-wireless-security"][ "pairwise"]: found = False for cipher in self.info_dict[ "802-11-wireless-security"][ "pairwise"]: if self.match_cipher( cipher, "wep40", wpa_flags, wpa_flags, 1): found = True break if self.match_cipher( cipher, "wep104", wpa_flags, wpa_flags, 2): found = True break if not found: return False else: pass else: pass # Match at least one group cipher with AP's capability if the # wireless-security setting explicitly lists group ciphers if "group" in self.info_dict[ "802-11-wireless-security"].iterkeys(): if self.info_dict["802-11-wireless-security"][ "group"]: found = False for cipher in self.info_dict[ "802-11-wireless-security"]["group"]: if self.match_cipher( cipher, "wep40", wpa_flags, wpa_flags, 16): found = True break if self.match_cipher( cipher, "wep104", wpa_flags, wpa_flags, 32): found = True break if not found: return False else: pass else: pass return True # WPA[2]-PSK and WPA[2] Enterprise elif self.info_dict["802-11-wireless-security"][ "key-mgmt"] == "wpa-psk" or "wpa-eap": if self.info_dict["802-11-wireless-security"][ "key-mgmt"] == "wpa-psk": if not (wpa_flags & 256) and not (rsn_flags & 256): return False elif self.info_dict["802-11-wireless-security"][ "key-mgmt"] == "wpa-eap": if not (wpa_flags & 512) and not (rsn_flags & 512): return False # FIXME: should handle WPA and RSN separately here to ensure that # if the Connection only uses WPA we don't match a cipher against # the AP's RSN IE instead # Match at least one pairwise cipher with AP's capability if the # wireless-security setting explicitly lists pairwise ciphers if "pairwise" in self.info_dict[ "802-11-wireless-security"].iterkeys(): if self.info_dict["802-11-wireless-security"][ "pairwise"]: found = False for cipher in self.info_dict[ "802-11-wireless-security"]["pairwise"]: if self.match_cipher(cipher, "tkip", wpa_flags, rsn_flags, 8): found = True break if self.match_cipher(cipher, "ccmp", wpa_flags, rsn_flags, 10): found = True break if not found: return False else: pass else: pass # Match at least one group cipher with AP's capability if the # wireless-security setting explicitly lists group ciphers if "group" in self.info_dict[ "802-11-wireless-security"].iterkeys(): if self.info_dict["802-11-wireless-security"]["group"]: found = False for cipher in self.info_dict[ "802-11-wireless-security"]["group"]: if self.match_cipher(cipher, "wep40", wpa_flags, rsn_flags, 16): found = True break if self.match_cipher(cipher, "wep104", wpa_flags, rsn_flags, 32): found = True break if self.match_cipher(cipher, "tkip", wpa_flags, rsn_flags, 64): found = True break if self.match_cipher(cipher, "ccmp", wpa_flags, rsn_flags, 128): found = True break if not found: return False else: pass else: pass return True return True
def get_ssid_not_associate_connections(self, ssid): try: return filter(lambda x: TypeConvert.ssid_ascii2string(x.settings_dict["802-11-wireless"]["ssid"]) != ssid, self.get_wireless_connections()) except: return []
def get_addresses(self): return [TypeConvert.ip4address_net2native(item) for item in self.properties["Addresses"]]
def get_nameservers(self): return [ TypeConvert.ip4_net2native(item) for item in self.properties["Nameservers"] ]
def get_nameservers(self): return [TypeConvert.ip4_net2native(item) for item in self.properties["Nameservers"]]
def get_ssid(self): if self.properties["Ssid"]: return TypeConvert.ssid_ascii2string(self.properties["Ssid"]) else: return None
def get_routes(self): return [TypeConvert.ip4route_net2native(item) for item in self.properties["Routes"]]
def get_secrets(self, setting_name): try: return TypeConvert.dbus2py( self.dbus_method("GetSecrets", setting_name)) except: return {}
def get_secrets(self, setting_name): try: return TypeConvert.dbus2py(self.dbus_method("GetSecrets", setting_name)) except: return {}