[Sugar-devel] [PATCH sugar] Don't treat SSID as UTF-8 character sequence (fixes SL#2023)

Sascha Silbe silbe at activitycentral.com
Mon Apr 2 12:40:36 EDT 2012


IEEE 802.11 [2] defines the SSID as a sequence of octets (i.e. bytes), but
Sugar treated it as UTF-8 character data. While in most cases the SSID is
actually some human-readable string, there's neither a guarantee for that nor
does any (de-facto or de-jure) standard specify the encoding to use. As a
result, we'll encounter SSIDs in a large variety of encodings and will also
need to cope with arbitrary byte strings. Any assumption of a single (or in
fact any) character encoding is incorrect.

The D-Bus API of NetworkManager 0.9 [3] passes SSIDs as uninterpreted byte
strings (D-Bus signature "ay"). Before SSIDs can be displayed on screen, some
kind of interpretation must happen.

NetworkManager has a rather elaborate heuristic that takes the user locale into
account. In the future (i.e. when the NetworkManager client code in Sugar has
been ported to gobject-introspection) we may use nm_utils_ssid_to_utf8() [4],
but for now we're doing the following to allow the user to use non-UTF-8 APs at
all:

1. If the SSID is a valid character string consisting only of
   printable characters in one of the following encodings (tried in
   the given order), decode it accordingly:
   UTF-8, ISO-8859-1, Windows-1251.

2. Return a hex dump of the SSID.

The first rule should cover the majority of current Sugar users and hopefully
all AP vendors will switch to UTF-8 eventually. In the meantime, the second
rule allows users to distinguish between several APs with SSIDs in unknown
encodings (or even using arbitrary byte sequences that don't _have_ a character
representation).

Tested:
- filtering on ASCII and non-ASCII parts of the name of and connecting to:
  - an unsecured AP with a UTF-8 SSID ("äöü߀sugartest", HostAP)
  - an unsecured AP with an ISO-8859-1 SSID ("äöüßsugartest", HostAP)
  - an unsecured AP with a non-character SSID (0d:06:f0:0d, HostAP)
  - a WEP-secured AP with a UTF-8 name ("äöü߀sugartest2", HostAP)
  - a WEP-secured AP with an ISO-8859-1 name ("äöüßsugartest2", HostAP)
  - a WEP-secured AP with a non-character SSID (0d:06:f0:0d, HostAP)
  - a WPA-secured AP with an ASCII name (COTS AP)

In each case the name was displayed correctly in
a) the palette of the AP icon in the Neighbourhood,
b) the palette of the wireless network Frame device and
c) the title of the WLAN credentials (WEP/WPA passphrase) dialog (for
   the WEP/WPA cases).

[1] https://bugs.sugarlabs.org/ticket/2023
[2] http://standards.ieee.org/getieee802/download/802.11-2007.pdf
[3] http://projects.gnome.org/NetworkManager/developers/api/09/spec.html
[4] http://projects.gnome.org/NetworkManager/developers/libnm-util/09/libnm-util-nm-utils.html#nm-utils-ssid-to-utf8

Signed-off-by: Sascha Silbe <silbe at activitycentral.com>
---
 extensions/deviceicon/network.py   |   17 +++++++-----
 src/jarabe/desktop/keydialog.py    |    5 ++-
 src/jarabe/desktop/meshbox.py      |    4 +-
 src/jarabe/desktop/networkviews.py |   33 ++++++++++++-----------
 src/jarabe/model/adhoc.py          |    6 ++--
 src/jarabe/model/network.py        |   49 +++++++++++++++++++++++++++++++++--
 6 files changed, 81 insertions(+), 33 deletions(-)

diff --git a/extensions/deviceicon/network.py b/extensions/deviceicon/network.py
index 1beeb88..09a3abb 100644
--- a/extensions/deviceicon/network.py
+++ b/extensions/deviceicon/network.py
@@ -374,7 +374,8 @@ class WirelessDeviceView(ToolButton):
         self._device = device
         self._device_props = None
         self._flags = 0
-        self._name = ''
+        self._ssid = ''
+        self._display_name = ''
         self._mode = network.NM_802_11_MODE_UNKNOWN
         self._strength = 0
         self._frequency = 0
@@ -394,7 +395,7 @@ class WirelessDeviceView(ToolButton):
         self._icon.show()
 
         self.set_palette_invoker(FrameWidgetInvoker(self))
-        self._palette = WirelessPalette(self._name)
+        self._palette = WirelessPalette(self._display_name)
         self._palette.connect('deactivate-connection',
                               self.__deactivate_connection_cb)
         self.set_palette(self._palette)
@@ -471,7 +472,8 @@ class WirelessDeviceView(ToolButton):
             self._mode = properties['Mode']
             self._color = None
         if 'Ssid' in properties:
-            self._name = properties['Ssid']
+            self._ssid = properties['Ssid']
+            self._display_name = network.ssid_to_display_name(self._ssid)
             self._color = None
         if 'Strength' in properties:
             self._strength = properties['Strength']
@@ -482,11 +484,11 @@ class WirelessDeviceView(ToolButton):
 
         if self._color == None:
             if self._mode == network.NM_802_11_MODE_ADHOC and \
-                    network.is_sugar_adhoc_network(self._name):
+                    network.is_sugar_adhoc_network(self._ssid):
                 self._color = profile.get_color()
             else:
                 sha_hash = hashlib.sha1()
-                data = self._name + hex(self._flags)
+                data = self._ssid + hex(self._flags)
                 sha_hash.update(data)
                 digest = hash(sha_hash.digest())
                 index = digest % len(xocolor.colors)
@@ -508,7 +510,8 @@ class WirelessDeviceView(ToolButton):
         else:
             self._icon.props.badge_name = None
 
-        self._palette.props.primary_text = glib.markup_escape_text(self._name)
+        label = glib.markup_escape_text(self._display_name)
+        self._palette.props.primary_text = label
 
         self._update_state()
         self._update_color()
@@ -520,7 +523,7 @@ class WirelessDeviceView(ToolButton):
             state = network.NM_DEVICE_STATE_UNKNOWN
 
         if self._mode != network.NM_802_11_MODE_ADHOC and \
-                network.is_sugar_adhoc_network(self._name) == False:
+                network.is_sugar_adhoc_network(self._ssid) == False:
             if state == network.NM_DEVICE_STATE_ACTIVATED:
                 icon_name = '%s-connected' % 'network-wireless'
             else:
diff --git a/src/jarabe/desktop/keydialog.py b/src/jarabe/desktop/keydialog.py
index 17ff6bd..41c2a51 100644
--- a/src/jarabe/desktop/keydialog.py
+++ b/src/jarabe/desktop/keydialog.py
@@ -87,8 +87,9 @@ class KeyDialog(gtk.Dialog):
 
         self.set_has_separator(False)
 
-        label = gtk.Label("A wireless encryption key is required for\n" \
-                          " the wireless network '%s'." % self._ssid)
+        display_name = network.ssid_to_display_name(ssid)
+        label = gtk.Label(_("A wireless encryption key is required for\n"
+                            " the wireless network '%s'.") % (display_name, ))
         self.vbox.pack_start(label)
 
         self.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
diff --git a/src/jarabe/desktop/meshbox.py b/src/jarabe/desktop/meshbox.py
index 2ce6163..0d0c9c1 100644
--- a/src/jarabe/desktop/meshbox.py
+++ b/src/jarabe/desktop/meshbox.py
@@ -543,13 +543,13 @@ class MeshBox(gtk.VBox):
         # if we have mesh hardware, ignore OLPC mesh networks that appear as
         # normal wifi networks
         if len(self._mesh) > 0 and ap.mode == network.NM_802_11_MODE_ADHOC \
-                and ap.name == 'olpc-mesh':
+                and ap.ssid == 'olpc-mesh':
             logging.debug('ignoring OLPC mesh IBSS')
             ap.disconnect()
             return
 
         if self._adhoc_manager is not None and \
-                network.is_sugar_adhoc_network(ap.name) and \
+                network.is_sugar_adhoc_network(ap.ssid) and \
                 ap.mode == network.NM_802_11_MODE_ADHOC:
             if old_hash_value is None:
                 # new Ad-hoc network finished initializing
diff --git a/src/jarabe/desktop/networkviews.py b/src/jarabe/desktop/networkviews.py
index 31a366c..f42bfed 100644
--- a/src/jarabe/desktop/networkviews.py
+++ b/src/jarabe/desktop/networkviews.py
@@ -60,7 +60,8 @@ class WirelessNetworkView(CanvasPulsingIcon):
         self._disconnect_item = None
         self._connect_item = None
         self._filtered = False
-        self._name = initial_ap.name
+        self._ssid = initial_ap.ssid
+        self._display_name = network.ssid_to_display_name(self._ssid)
         self._mode = initial_ap.mode
         self._strength = initial_ap.strength
         self._flags = initial_ap.flags
@@ -71,11 +72,11 @@ class WirelessNetworkView(CanvasPulsingIcon):
         self._color = None
 
         if self._mode == network.NM_802_11_MODE_ADHOC and \
-                network.is_sugar_adhoc_network(self._name):
+                network.is_sugar_adhoc_network(self._ssid):
             self._color = profile.get_color()
         else:
             sha_hash = hashlib.sha1()
-            data = self._name + hex(self._flags)
+            data = self._ssid + hex(self._flags)
             sha_hash.update(data)
             digest = hash(sha_hash.digest())
             index = digest % len(xocolor.colors)
@@ -118,8 +119,8 @@ class WirelessNetworkView(CanvasPulsingIcon):
                                   icon_size=style.STANDARD_ICON_SIZE,
                                   badge_name=self.props.badge_name)
 
-        p = palette.Palette(primary_text=glib.markup_escape_text(self._name),
-                            icon=self._palette_icon)
+        label = glib.markup_escape_text(self._display_name)
+        p = palette.Palette(primary_text=label, icon=self._palette_icon)
 
         self._connect_item = MenuItem(_('Connect'), 'dialog-ok')
         self._connect_item.connect('activate', self.__connect_activate_cb)
@@ -182,7 +183,7 @@ class WirelessNetworkView(CanvasPulsingIcon):
 
     def _update_icon(self):
         if self._mode == network.NM_802_11_MODE_ADHOC and \
-                network.is_sugar_adhoc_network(self._name):
+                network.is_sugar_adhoc_network(self._ssid):
             channel = max([1] + [ap.channel for ap in
                                  self._access_points.values()])
             if self._device_state == network.NM_DEVICE_STATE_ACTIVATED and \
@@ -208,7 +209,7 @@ class WirelessNetworkView(CanvasPulsingIcon):
 
     def _update_badge(self):
         if self._mode != network.NM_802_11_MODE_ADHOC:
-            if network.find_connection_by_ssid(self._name) is not None:
+            if network.find_connection_by_ssid(self._ssid) is not None:
                 self.props.badge_name = 'emblem-favorite'
                 self._palette_icon.props.badge_name = 'emblem-favorite'
             elif self._flags == network.NM_802_11_AP_FLAGS_PRIVACY:
@@ -333,19 +334,20 @@ class WirelessNetworkView(CanvasPulsingIcon):
 
     def _connect(self):
         # Activate existing connection, if there is one
-        connection = network.find_connection_by_ssid(self._name)
+        connection = network.find_connection_by_ssid(self._ssid)
         if connection:
-            logging.debug("Activating existing connection for %s", self._name)
+            logging.debug('Activating existing connection for SSID %r',
+                          self._ssid)
             connection.activate(self._device)
             return
 
         # Otherwise, create new connection and activate it
-        logging.debug("Creating new connection for %s", self._name)
+        logging.debug('Creating new connection for SSID %r', self._ssid)
         settings = Settings()
-        settings.connection.id = str(self._name)
+        settings.connection.id = self._display_name
         settings.connection.uuid = unique_id()
         settings.connection.type = '802-11-wireless'
-        settings.wireless.ssid = self._name
+        settings.wireless.ssid = self._ssid
 
         if self._mode == network.NM_802_11_MODE_INFRA:
             settings.wireless.mode = 'infrastructure'
@@ -366,12 +368,12 @@ class WirelessNetworkView(CanvasPulsingIcon):
                                             self.get_first_ap().model)
 
     def set_filter(self, query):
-        self._filtered = self._name.lower().find(query) == -1
+        self._filtered = self._display_name.lower().find(query) == -1
         self._update_icon()
         self._update_color()
 
     def create_keydialog(self, response):
-        keydialog.create(self._name, self._flags, self._wpa_flags,
+        keydialog.create(self._ssid, self._flags, self._wpa_flags,
                          self._rsn_flags, self._device_caps, response)
 
     def update_strength(self):
@@ -414,7 +416,7 @@ class WirelessNetworkView(CanvasPulsingIcon):
 
     def is_olpc_mesh(self):
         return self._mode == network.NM_802_11_MODE_ADHOC \
-            and self.name == 'olpc-mesh'
+            and self._ssid == 'olpc-mesh'
 
     def remove_all_aps(self):
         for ap in self._access_points.values():
@@ -580,7 +582,6 @@ class OlpcMeshView(CanvasPulsingIcon):
         self._disconnect_item = None
         self._connect_item = None
         self._filtered = False
-        self._name = ''
         self._device_state = None
         self._active = False
         device = mesh_mgr.mesh_device
diff --git a/src/jarabe/model/adhoc.py b/src/jarabe/model/adhoc.py
index 09325ed..68a9aa3 100644
--- a/src/jarabe/model/adhoc.py
+++ b/src/jarabe/model/adhoc.py
@@ -242,13 +242,13 @@ class AdHocManager(gobject.GObject):
         access_point -- Access Point
 
         """
-        if access_point.name.endswith(' 1'):
+        if access_point.ssid.endswith(' 1'):
             self._networks[self._CHANNEL_1] = access_point
             self.emit('members-changed', self._CHANNEL_1, True)
-        elif access_point.name.endswith(' 6'):
+        elif access_point.ssid.endswith(' 6'):
             self._networks[self._CHANNEL_6] = access_point
             self.emit('members-changed', self._CHANNEL_6, True)
-        elif access_point.name.endswith('11'):
+        elif access_point.ssid.endswith('11'):
             self._networks[self._CHANNEL_11] = access_point
             self.emit('members-changed', self._CHANNEL_11, True)
 
diff --git a/src/jarabe/model/network.py b/src/jarabe/model/network.py
index 40eb6d0..cc02b58 100644
--- a/src/jarabe/model/network.py
+++ b/src/jarabe/model/network.py
@@ -609,7 +609,7 @@ class AccessPoint(gobject.GObject):
         self._initialized = False
         self._bus = dbus.SystemBus()
 
-        self.name = ''
+        self.ssid = ''
         self.strength = 0
         self.flags = 0
         self.wpa_flags = 0
@@ -663,7 +663,7 @@ class AccessPoint(gobject.GObject):
         else:
             fl |= 1 << 6
 
-        hashstr = str(fl) + '@' + self.name
+        hashstr = str(fl) + '@' + self.ssid
         return hash(hashstr)
 
     def _update_properties(self, properties):
@@ -673,7 +673,7 @@ class AccessPoint(gobject.GObject):
             old_hash = None
 
         if 'Ssid' in properties:
-            self.name = properties['Ssid']
+            self.ssid = properties['Ssid']
         if 'Strength' in properties:
             self.strength = properties['Strength']
         if 'Flags' in properties:
@@ -1051,3 +1051,46 @@ def disconnect_access_points(ap_paths):
             dev_obj = bus.get_object(NM_SERVICE, dev_path)
             dev = dbus.Interface(dev_obj, NM_DEVICE_IFACE)
             dev.Disconnect()
+
+
+def _is_non_printable(char):
+    """
+    Return True if char is a non-printable unicode character, False otherwise
+    """
+    return (char < u' ') or (u'~' < char < u'\xA0') or (char == u'\xAD')
+
+
+def ssid_to_display_name(ssid):
+    """Convert an SSID into a unicode string for recognising Access Points
+
+    Return a unicode string that's useful for recognising and
+    distinguishing between Access Points (APs).
+
+    IEEE 802.11 defines SSIDs as arbitrary byte sequences. As random
+    bytes are not very user-friendly, most APs use some human-readable
+    character string as SSID. However, because there's no standard
+    specifying what encoding to use, AP vendors chose various
+    different encodings. Since there's also no indication of what
+    encoding was used for a particular SSID, the best we can do for
+    turning an SSID into a displayable string is to try a couple of
+    encodings based on some heuristic.
+
+    We're currently using the following heuristic:
+
+    1. If the SSID is a valid character string consisting only of
+       printable characters in one of the following encodings (tried in
+       the given order), decode it accordingly:
+       UTF-8, ISO-8859-1, Windows-1251.
+    2. Return a hex dump of the SSID.
+    """
+    for encoding in ['utf-8', 'iso-8859-1', 'windows-1251']:
+        try:
+            display_name = unicode(ssid, encoding)
+        except UnicodeDecodeError:
+            continue
+
+        if not [True for char in display_name if _is_non_printable(char)]:
+            # Only printable characters
+            return display_name
+
+    return ':'.join(['%02x' % (ord(byte), ) for byte in ssid])
-- 
1.7.9



More information about the Sugar-devel mailing list