From c57a28e2c6b56f65d0bf6aade996f30bb7b2821b Mon Sep 17 00:00:00 2001 From: Martin Vidner Date: Fri, 10 Jul 2009 22:39:20 +0200 Subject: [PATCH] Separated more client classes. --- applet.py | 42 ++++++++++ applet06.py | 22 +++++ cnetworkmanager | 246 +------------------------------------------------------- connection.py | 32 ++++++++ connection06.py | 31 +++++++ object.py | 5 +- settings.py | 125 ++++++++++++++++++++++++++++ 7 files changed, 257 insertions(+), 246 deletions(-) create mode 100644 applet.py create mode 100644 applet06.py create mode 100644 connection.py create mode 100644 connection06.py create mode 100644 settings.py diff --git a/applet.py b/applet.py new file mode 100644 index 0000000..1e9bec0 --- /dev/null +++ b/applet.py @@ -0,0 +1,42 @@ +import dbus +from object import * +from manager import * +from connection import cConnection + +SSC = "org.freedesktop.NetworkManagerSystemSettings" +USC = "org.freedesktop.NetworkManagerUserSettings" +NMIC = "org.freedesktop.NetworkManagerInfo" + +# this is the client side of the applet; see also UserSettings +class cApplet(cObject): + def __init__(self, svc, opath): + cObject.__init__(self, opath, svc) + self.si = dbus.Interface(self.obj, 'org.freedesktop.NetworkManagerSettings') + + def get_property(self, property_name): + return cObject.get_property(self, NMI, property_name) + + def isSystem(self): + return self.svc == SSC; + + def Dump(self): + for conn in self.Connections(): + conn.Dump() + if self.isSystem(): + self.DumpSystem() + + def DumpSystem(self): + print "Unmanaged Devices" + umds = self.get_property("UnmanagedDevices") + for umd in umds: + print " ", umd + # dump_settings_conn(svc, conn) umd? + + + def myConnection(self, opath): + return cConnection(self.svc, opath) + + def Connections(self): + opaths = self.si.ListConnections() + return map(self.myConnection, opaths) + diff --git a/applet06.py b/applet06.py new file mode 100644 index 0000000..a2eb1f0 --- /dev/null +++ b/applet06.py @@ -0,0 +1,22 @@ +from connection06 import cConnection_06 +from applet import cApplet + +NETWORK_TYPE_ALLOWED = 1 +class cApplet_06(cApplet): + def __init__(self, svc, opath): + self.svc = svc + self.opath = opath + self.io = bus.get_object(self.svc, self.opath) + self.ii = dbus.Interface(self.io, 'org.freedesktop.NetworkManagerInfo') + + def isSystem(self): + return False; + + def myConnection(self, opath): + return cConnection_06(self, opath) + + # TODO also VPN conns + def Connections(self): + names = self.ii.getNetworks(NETWORK_TYPE_ALLOWED) + return map(self.myConnection, names) + diff --git a/cnetworkmanager b/cnetworkmanager index 39c07c2..6428b1c 100755 --- a/cnetworkmanager +++ b/cnetworkmanager @@ -55,6 +55,8 @@ from device07 import cDevice_07 from ap import cAP from ap06 import cAP_06 from ap07 import cAP_07 +from applet import cApplet, SSC, USC, NMIC +from applet06 import cApplet_06 LOOP = False @@ -65,10 +67,6 @@ bus = dbus.SystemBus() # fooo = object # fooi = interface # foopi = property interface -SSC = "org.freedesktop.NetworkManagerSystemSettings" -USC = "org.freedesktop.NetworkManagerUserSettings" -NMIC = "org.freedesktop.NetworkManagerInfo" - def introspect(obj): ii = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable') print ii.Introspect() @@ -108,249 +106,9 @@ def opath_unescape(s): # all "_2d_" -> "-" return re.sub("_.._", lambda p: unhex(p.group()[1:3]), s) -# this is the client side of the applet; see also UserSettings -class cApplet: - def __init__(self, svc, opath): - self.svc = svc - self.opath = opath - self.so = bus.get_object(self.svc, self.opath) - self.si = dbus.Interface(self.so, 'org.freedesktop.NetworkManagerSettings') - - def isSystem(self): - return self.svc == SSC; - - def Dump(self): - for conn in self.Connections(): - conn.Dump() - if self.isSystem(): - self.DumpSystem() - - def DumpSystem(self): - sspi = dbus.Interface(self.so, PI) - print "Unmanaged Devices" - umds = sspi.Get(NMI, "UnmanagedDevices") - for umd in umds: - print " ", umd - # dump_settings_conn(svc, conn) umd? - - - def myConnection(self, opath): - return cConnection(self.svc, opath) - - def Connections(self): - opaths = self.si.ListConnections() - return map(self.myConnection, opaths) - -NETWORK_TYPE_ALLOWED = 1 -class cApplet_06(cApplet): - def __init__(self, svc, opath): - self.svc = svc - self.opath = opath - self.io = bus.get_object(self.svc, self.opath) - self.ii = dbus.Interface(self.io, 'org.freedesktop.NetworkManagerInfo') - - def isSystem(self): - return False; - - def myConnection(self, opath): - return cConnection_06(self, opath) - - # TODO also VPN conns - def Connections(self): - names = self.ii.getNetworks(NETWORK_TYPE_ALLOWED) - return map(self.myConnection, names) - -class cConnection: - def __init__(self, svc, opath): - self.svc = svc - self.opath = opath - self.co = bus.get_object(self.svc, self.opath) - self.ci = dbus.Interface(self.co, 'org.freedesktop.NetworkManagerSettings.Connection') - - def Dump(self): - print "Conn:", self.opath - settings = self.Settings() - settings.Dump() - - si = dbus.Interface(self.co, 'org.freedesktop.NetworkManagerSettings.Connection.Secrets') - security = settings.Security() - if security != "": - print " SECRETS:", security - try: - # TODO merge them - secrets = cSettings(si.GetSecrets(security,[],False)) - secrets.Dump() - except dbus.exceptions.DBusException, e: - if e.get_dbus_name() == "org.freedesktop.DBus.Error.AccessDenied": - print " Access denied" - else: - print " ", e - print " FIXME figure out 802-1x secrets" - - def Settings(self): - return cSettings(self.ci.GetSettings()) - def dump_time(unixtime): return time.asctime(time.localtime(unixtime)) -class cConnection_06: - def __init__(self, applet, id): - self.id = id - self.applet = applet - - def Dump(self): - print "Conn:", self.id - - np = self.applet.ii.getNetworkProperties(self.id, NETWORK_TYPE_ALLOWED) - ssid = np[0] - print " ssid:", ssid - print " time:", dump_time(np[1]) - print " trusted:", bool(np[2]) - print " bssids:", ", ".join(np[3]) - enctype = np[4] - print " we_cipher:", enctype - if enctype != 1: - print " secret:", np[5] - if enctype == 16: - print " wep_auth_algorithm:", np[6] - elif enctype == 0: - print " wpa_psk_key_mgt:", np[6] - print " wpa_psk_wpa_version:", np[7] - - return # nm-applet will not tell kfn anyway - devp = "/org/freedesktop/NetworkManager/Devices/ath0" #FIXME - netp = devp + "/Networks/" + opath_escape(self.id) - attempt = 1 - newkey = False - kfn = self.applet.ii.getKeyForNetwork(devp, netp, ssid, attempt, newkey) - print " kfn:", kfn - - -# 06 -NM_AUTH_TYPE_WPA_PSK_AUTO = 0x00000000 -NM_AUTH_TYPE_NONE = 0x00000001 -NM_AUTH_TYPE_WEP40 = 0x00000002 -NM_AUTH_TYPE_WPA_PSK_TKIP = 0x00000004 -NM_AUTH_TYPE_WPA_PSK_CCMP = 0x00000008 -NM_AUTH_TYPE_WEP104 = 0x00000010 -NM_AUTH_TYPE_WPA_EAP = 0x00000020 -NM_AUTH_TYPE_LEAP = 0x00000040 - -IW_AUTH_ALG_OPEN_SYSTEM = 0x00000001 -IW_AUTH_ALG_SHARED_KEY = 0x00000002 -IW_AUTH_ALG_LEAP = 0x00000004 - -class cSettings: - def __init__(self, conmap): - #print "INIT", conmap - self.conmap = conmap - - def Type(self): - return self.conmap["connection"]["type"] - - def ID(self): - return self.conmap["connection"]["id"] - - def Ssid(self): - try: - return self.conmap["802-11-wireless"]["ssid"] - except KeyError: - pass - # probably 802-3-ethernet - return "" - - def Timestamp(self): - try: - return self.conmap["connection"]["timestamp"] - except KeyError: - return 0 - - def Trusted(self): - # false by default - return False - - def SeenBssids(self): - try: - return self.conmap["802-11-wireless"]["seen-bssids"] - except KeyError: - return [] - - # for 06 - def WeCipher(self): - k = self.Key() - if len(k) == 26: - return NM_AUTH_TYPE_WEP104 - elif len(k) == 64: - return NM_AUTH_TYPE_WPA_PSK_AUTO - elif len(k) == 0: - return NM_AUTH_TYPE_NONE - print "Defaulting cipher type to none" - return NM_AUTH_TYPE_NONE - - def Key(self): - try: - return self.conmap["802-11-wireless-security"]["psk"] - except KeyError: - pass - try: - return self.conmap["802-11-wireless-security"]["wep-key0"] - except KeyError: - pass - # no key - return "" - - def WepAuthAlgorithm(self): - print "FIXME Defaulting WEP auth alg to open" - return IW_AUTH_ALG_OPEN_SYSTEM - - def PskKeyMgt(self): - print "FIXME Defaulting PSK key mgmt to 2" - return 2 - - def PskWpaVersion(self): - print "FIXME Defaulting WPA version to 2" - return 2 - - def Security(self): - try: - return self.conmap[self.Type()]["security"] - except KeyError: - return "" - - def isNet(self, net_name): - return self.ID() == net_name or self.Ssid() == net_name - - # FIXME check spec/NM what to censor - secrets = dict.fromkeys(["wep-key0", "psk"]) - - def ConMap(self): - "For GetSettings: censor secrets." - - cm = dict() - for n1, v1 in self.conmap.iteritems(): - cm[n1] = dict() - for n2, v2 in v1.iteritems(): - cv2 = v2 - if self.secrets.has_key(n2): - cv2 = "" - cm[n1][n2] = cv2 - return cm - - def SecMap(self): - "For GetSecrets: only secrets." - s = self.Security() - r = { - s: self.conmap[s] - } - print "SECMAP", r - return r - - def Dump(self): - for n1, v1 in self.conmap.iteritems(): - print " ",n1 - for n2, v2 in v1.iteritems(): - print " %s: %s" % (n2, v2) - # server analog of cApplet class UserSettings(dbus.service.Object): # conmaps is a list diff --git a/connection.py b/connection.py new file mode 100644 index 0000000..8de0adf --- /dev/null +++ b/connection.py @@ -0,0 +1,32 @@ +import dbus +from object import * +from settings import cSettings + +class cConnection(cObject): + def __init__(self, svc, opath): + cObject.__init__(self, opath, svc) + self.ci = dbus.Interface(self.obj, 'org.freedesktop.NetworkManagerSettings.Connection') + + def Dump(self): + print "Conn:", self.opath + settings = self.Settings() + settings.Dump() + + si = dbus.Interface(self.obj, 'org.freedesktop.NetworkManagerSettings.Connection.Secrets') + security = settings.Security() + if security != "": + print " SECRETS:", security + try: + # TODO merge them + secrets = cSettings(si.GetSecrets(security,[],False)) + secrets.Dump() + except dbus.exceptions.DBusException, e: + if e.get_dbus_name() == "org.freedesktop.DBus.Error.AccessDenied": + print " Access denied" + else: + print " ", e + print " FIXME figure out 802-1x secrets" + + def Settings(self): + return cSettings(self.ci.GetSettings()) + diff --git a/connection06.py b/connection06.py new file mode 100644 index 0000000..38e5c09 --- /dev/null +++ b/connection06.py @@ -0,0 +1,31 @@ +class cConnection_06: + def __init__(self, applet, id): + self.id = id + self.applet = applet + + def Dump(self): + print "Conn:", self.id + + np = self.applet.ii.getNetworkProperties(self.id, NETWORK_TYPE_ALLOWED) + ssid = np[0] + print " ssid:", ssid + print " time:", dump_time(np[1]) + print " trusted:", bool(np[2]) + print " bssids:", ", ".join(np[3]) + enctype = np[4] + print " we_cipher:", enctype + if enctype != 1: + print " secret:", np[5] + if enctype == 16: + print " wep_auth_algorithm:", np[6] + elif enctype == 0: + print " wpa_psk_key_mgt:", np[6] + print " wpa_psk_wpa_version:", np[7] + + return # nm-applet will not tell kfn anyway + devp = "/org/freedesktop/NetworkManager/Devices/ath0" #FIXME + netp = devp + "/Networks/" + opath_escape(self.id) + attempt = 1 + newkey = False + kfn = self.applet.ii.getKeyForNetwork(devp, netp, ssid, attempt, newkey) + print " kfn:", kfn diff --git a/object.py b/object.py index f774644..d2e35d0 100644 --- a/object.py +++ b/object.py @@ -4,10 +4,11 @@ NMC = 'org.freedesktop.NetworkManager' PI = 'org.freedesktop.DBus.Properties' class cObject: - def __init__(self, opath): + def __init__(self, opath, svc=NMC): self.bus = dbus.SystemBus() self.opath = opath - self.obj = self.bus.get_object(NMC, opath) + self.svc = svc + self.obj = self.bus.get_object(self.svc, self.opath) self.pi = dbus.Interface(self.obj, dbus_interface=PI) def get_property(self, iface, property_name): diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..412df23 --- /dev/null +++ b/settings.py @@ -0,0 +1,125 @@ +# 06 +NM_AUTH_TYPE_WPA_PSK_AUTO = 0x00000000 +NM_AUTH_TYPE_NONE = 0x00000001 +NM_AUTH_TYPE_WEP40 = 0x00000002 +NM_AUTH_TYPE_WPA_PSK_TKIP = 0x00000004 +NM_AUTH_TYPE_WPA_PSK_CCMP = 0x00000008 +NM_AUTH_TYPE_WEP104 = 0x00000010 +NM_AUTH_TYPE_WPA_EAP = 0x00000020 +NM_AUTH_TYPE_LEAP = 0x00000040 + +IW_AUTH_ALG_OPEN_SYSTEM = 0x00000001 +IW_AUTH_ALG_SHARED_KEY = 0x00000002 +IW_AUTH_ALG_LEAP = 0x00000004 + +class cSettings: + def __init__(self, conmap): + #print "INIT", conmap + self.conmap = conmap + + def Type(self): + return self.conmap["connection"]["type"] + + def ID(self): + return self.conmap["connection"]["id"] + + def Ssid(self): + try: + return self.conmap["802-11-wireless"]["ssid"] + except KeyError: + pass + # probably 802-3-ethernet + return "" + + def Timestamp(self): + try: + return self.conmap["connection"]["timestamp"] + except KeyError: + return 0 + + def Trusted(self): + # false by default + return False + + def SeenBssids(self): + try: + return self.conmap["802-11-wireless"]["seen-bssids"] + except KeyError: + return [] + + # for 06 + def WeCipher(self): + k = self.Key() + if len(k) == 26: + return NM_AUTH_TYPE_WEP104 + elif len(k) == 64: + return NM_AUTH_TYPE_WPA_PSK_AUTO + elif len(k) == 0: + return NM_AUTH_TYPE_NONE + print "Defaulting cipher type to none" + return NM_AUTH_TYPE_NONE + + def Key(self): + try: + return self.conmap["802-11-wireless-security"]["psk"] + except KeyError: + pass + try: + return self.conmap["802-11-wireless-security"]["wep-key0"] + except KeyError: + pass + # no key + return "" + + def WepAuthAlgorithm(self): + print "FIXME Defaulting WEP auth alg to open" + return IW_AUTH_ALG_OPEN_SYSTEM + + def PskKeyMgt(self): + print "FIXME Defaulting PSK key mgmt to 2" + return 2 + + def PskWpaVersion(self): + print "FIXME Defaulting WPA version to 2" + return 2 + + def Security(self): + try: + return self.conmap[self.Type()]["security"] + except KeyError: + return "" + + def isNet(self, net_name): + return self.ID() == net_name or self.Ssid() == net_name + + # FIXME check spec/NM what to censor + secrets = dict.fromkeys(["wep-key0", "psk"]) + + def ConMap(self): + "For GetSettings: censor secrets." + + cm = dict() + for n1, v1 in self.conmap.iteritems(): + cm[n1] = dict() + for n2, v2 in v1.iteritems(): + cv2 = v2 + if self.secrets.has_key(n2): + cv2 = "" + cm[n1][n2] = cv2 + return cm + + def SecMap(self): + "For GetSecrets: only secrets." + s = self.Security() + r = { + s: self.conmap[s] + } + print "SECMAP", r + return r + + def Dump(self): + for n1, v1 in self.conmap.iteritems(): + print " ",n1 + for n2, v2 in v1.iteritems(): + print " %s: %s" % (n2, v2) + -- 2.11.4.GIT