Use the ssid for the conn id, not __cnm_handcrafted__
[cnetworkmanager.git] / networkmanager / applet / settings.py
blobbc22291a54ad43e2b8e1a181d43c9426b6d297a1
1 import dbus
2 import uuid
3 import math
4 import hashlib
5 import pbkdf2
6 import binascii
8 class Settings(object):
9 def __init__(self, conmap = {}):
10 #print "INIT", conmap
11 self.conmap = conmap
13 "Act like a dict"
14 def __getitem__(self, key):
15 return self.conmap.__getitem__(key)
17 "Act like a dict"
18 def __setitem__(self, key, value):
19 return self.conmap.__setitem__(key, value)
21 def Type(self):
22 return self.conmap["connection"]["type"]
24 def ID(self):
25 return self.conmap["connection"]["id"]
27 def Ssid(self):
28 try:
29 return self.conmap["802-11-wireless"]["ssid"]
30 except KeyError:
31 pass
32 # probably 802-3-ethernet
33 return ""
35 def Key(self):
36 try:
37 return self.conmap["802-11-wireless-security"]["psk"]
38 except KeyError:
39 pass
40 try:
41 return self.conmap["802-11-wireless-security"]["wep-key0"]
42 except KeyError:
43 pass
44 # no key
45 return ""
47 def Security(self):
48 try:
49 return self.conmap[self.Type()]["security"]
50 except KeyError:
51 return ""
53 def isNet(self, net_name):
54 return self.ID() == net_name or self.Ssid() == net_name
56 # FIXME check spec/NM what to censor
57 secrets = dict.fromkeys(["wep-key0", "psk"])
59 def ConMap(self):
60 "For GetSettings: censor secrets."
62 cm = dict()
63 for n1, v1 in self.conmap.iteritems():
64 cm[n1] = dict()
65 for n2, v2 in v1.iteritems():
66 cv2 = v2
67 if self.secrets.has_key(n2):
68 cv2 = ""
69 cm[n1][n2] = cv2
70 return cm
72 def SecMap(self):
73 "For GetSecrets: only secrets."
74 s = self.Security()
75 r = {
76 s: self.conmap[s]
78 print "SECMAP", r
79 return r
81 def Dump(self):
82 for n1, v1 in self.conmap.iteritems():
83 print " ",n1
84 for n2, v2 in v1.iteritems():
85 print " %s: %s" % (n2, v2)
88 class WiFi(Settings):
89 def __init__(self, ssid):
90 conmap = {
91 'connection': {
92 'id': ssid,
93 'uuid': str(uuid.uuid1()), # new in oS 11.1
94 'type': '802-11-wireless',
96 '802-11-wireless': {
97 'ssid': dbus.ByteArray(ssid), # TODO move to ConMap?
98 'mode': 'infrastructure',
101 super(WiFi, self).__init__(conmap)
103 class Wep(WiFi):
104 def __init__(self, ssid, key, hashed_key=""):
105 "One of key, hashed_key must be present"
107 super(WiFi, self).__init__(ssid)
108 self["802-11-wireless"]["security"] = "802-11-wireless-security"
109 self["802-11-wireless-security"] = {}
110 self["802-11-wireless-security"]["key-mgmt"] = "none"
111 self["802-11-wireless-security"]["wep-tx-keyidx"] = 0
112 if hashed_key == "":
113 # http://www.mail-archive.com/networkmanager-list@gnome.org/msg07935.html
114 hashed_key = hashlib.md5(Wep._elongate(key, 64)).hexdigest()
115 self["802-11-wireless-security"]["wep-key0"] = hashed_key
117 @staticmethod
118 def _elongate(s, tlen):
119 "repeat string s to target length tlen"
120 if s == "":
121 return ""
122 copies_needed = int(math.ceil(tlen / float(len(s))))
123 return (s * copies_needed)[:tlen]
125 class WpaPsk(WiFi):
126 def __init__(self, ssid, key, hashed_key=""):
127 "One of key, hashed_key must be present"
129 super(WiFi, self).__init__(ssid)
130 self["802-11-wireless"]["security"] = "802-11-wireless-security"
131 self["802-11-wireless-security"] = {}
132 self["802-11-wireless-security"]["group"] = ["tkip", "cselfp"]
133 self["802-11-wireless-security"]["pairwise"] = ["tkip", "ccmp"]
134 self["802-11-wireless-security"]["key-mgmt"] = "wpa-psk"
135 if hashed_key == "":
136 hashed_key = binascii.b2a_hex(pbkdf2.pbkdf2(key, ssid, 4096, 32))
137 self["802-11-wireless-security"]["psk"] = hashed_key