Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/desktop/keydialog.py
blob: d8c8cf95697873828d2328aa4c1bda40d5a12697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# Copyright (C) 2006-2007 Red Hat, Inc.
# Copyright (C) 2009 One Laptop per Child
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import hashlib
from gettext import gettext as _

import gtk
import dbus
import os
import shutil

from sugar import env
from jarabe.model import network
from jarabe.model.network import Secrets
from jarabe.journal.objectchooser import ObjectChooser


IW_AUTH_ALG_OPEN_SYSTEM = 'open'
IW_AUTH_ALG_SHARED_KEY = 'shared'

WEP_PASSPHRASE = 1
WEP_HEX = 2
WEP_ASCII = 3

SETTING_TYPE_STRING = 1
SETTING_TYPE_LIST = 2
SETTING_TYPE_CHOOSER = 3



def string_is_hex(key):
    is_hex = True
    for c in key:
        if not 'a' <= c.lower() <= 'f' and not '0' <= c <= '9':
            is_hex = False
    return is_hex


def string_is_ascii(string):
    try:
        string.encode('ascii')
        return True
    except UnicodeEncodeError:
        return False


def string_to_hex(passphrase):
    key = ''
    for c in passphrase:
        key += '%02x' % ord(c)
    return key


def hash_passphrase(passphrase):
    # passphrase must have a length of 64
    if len(passphrase) > 64:
        passphrase = passphrase[:64]
    elif len(passphrase) < 64:
        while len(passphrase) < 64:
            passphrase += passphrase[:64 - len(passphrase)]
    passphrase = hashlib.md5(passphrase).digest()
    return string_to_hex(passphrase)[:26]


class CanceledKeyRequestError(dbus.DBusException):
    def __init__(self):
        dbus.DBusException.__init__(self)
        self._dbus_error_name = network.NM_SETTINGS_IFACE + '.CanceledError'


class KeyDialog(gtk.Dialog):
    def __init__(self, ssid, flags, wpa_flags, rsn_flags, dev_caps, settings,
                 response):
        gtk.Dialog.__init__(self, flags=gtk.DIALOG_MODAL)
        self.set_title('Wireless Key Required')

        self._settings = settings
        self._response = response
        self._entry = None
        self._ssid = ssid
        self._flags = flags
        self._wpa_flags = wpa_flags
        self._rsn_flags = rsn_flags
        self._dev_caps = dev_caps

        self.set_has_separator(False)

        label = gtk.Label("A wireless encryption key is required for\n" \
                          " the wireless network '%s'." % self._ssid)
        self.vbox.pack_start(label)

        self.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                         gtk.STOCK_OK, gtk.RESPONSE_OK)
        self.set_default_response(gtk.RESPONSE_OK)
        self.set_has_separator(True)

    def add_key_entry(self):
        self._entry = gtk.Entry()
        self._entry.connect('changed', self._update_response_sensitivity)
        self._entry.connect('activate', self._entry_activate_cb)
        self.vbox.pack_start(self._entry)
        self.vbox.set_spacing(6)
        self.vbox.show_all()

        self._update_response_sensitivity()
        self._entry.grab_focus()

    def _entry_activate_cb(self, entry):
        self.response(gtk.RESPONSE_OK)

    def create_security(self):
        raise NotImplementedError

    def get_response_object(self):
        return self._response


class NetworkParameters(gtk.HBox):
    def __init__(self, auth_param):
        gtk.HBox.__init__(self, homogeneous=True)
        self._key = auth_param._key_name
        self._label = gtk.Label(_(auth_param._key_label))
        self._key_type = auth_param._key_type
        self._auth_param = auth_param

        self.pack_start(self._label)
        self._label.show()

        if self._is_entry():
            self._entry = gtk.Entry()
            self.pack_start(self._entry)
            self._entry.show()
        elif self._is_liststore():
            self._option_store = gtk.ListStore(str, str)
            for option in auth_param._options:
                self._option_store.append(option)

            self._entry = auth_param._options[0][1]
            self._option_combo = gtk.ComboBox(self._option_store)
            cell = gtk.CellRendererText()
            self._option_combo.pack_start(cell, True)
            self._option_combo.add_attribute(cell, 'text', 0)
            self._option_combo.set_active(0)
            self._option_combo.connect('changed',
                    self._option_combo_changed_cb)
            self.pack_start(self._option_combo)
            self.show()
            self._option_combo.show()
        elif self._is_chooser():
            self._chooser_button = gtk.Button(_('Choose..'))
            self._chooser_button.connect('clicked',
                                          self._object_chooser_cb)
            self.pack_start(self._chooser_button)
            self._chooser_button.show()
            self._entry = ''

    def _is_entry(self):
        return ( not self._is_chooser() ) and \
               ( len(self._auth_param._options) == 0 )

    def _is_liststore(self):
        return ( not self._is_chooser() ) and \
               ( len(self._auth_param._options) > 0 )

    def _is_chooser(self):
        return self._key_type == SETTING_TYPE_CHOOSER

    def _object_chooser_cb(self, chooser_button):
        self._want_document = True
        self._show_picker_cb()

    def _show_picker_cb(self):
        if not self._want_document:
            return
        self._chooser = ObjectChooser()
        self._chooser._set_callback(self.__process_selected_journal_object)

        self._chooser.show()

    def __process_selected_journal_object(self, object_id):
        jobject = self._chooser.get_selected_object()
        if jobject and jobject.file_path:
            file_basename = \
                    os.path.basename(jobject._metadata._properties['title'])
            self._chooser_button.set_label(file_basename)



            # The chooser entries are chosen from the journal.
            # Now, the journal entries misbehave, and the entries keep
            # changing their names. Thus, in order to have the same
            # entry available at later times, we need to store the
            # selected entries at a 'fixed-name' place.
            #
            # DISCOVERY-CUM-REQUIREMENT:
            # -------------------------
            #   It is needed that networks auto-connect on reboot; and
            #   the user need not ne re-prompted to enter the
            #   network-parameters.

            profile_path = env.get_profile_path()
            self._entry = os.path.join(profile_path, 'nm',
                                       file_basename)

            # Remove (older) file, if it exists.
            if os.path.exists(self._entry):
                os.remove(self._entry)

            # Copy the file.
            shutil.copy2(jobject.file_path, self._entry)

        self._chooser.destroy()

    def _option_combo_changed_cb(self, widget):
        it = self._option_combo.get_active_iter()
        (value, ) = self._option_store.get(it, 1)
        self._entry = value

    def _get_key(self):
        return self._key

    def _get_value(self):
        if self._is_entry():
            return self._entry.get_text()
        elif self._is_liststore():
            return self._entry
        elif self._is_chooser():
            if len(self._entry) > 0:
                return dbus.ByteArray('file://' + self._entry + '\0')
            else:
                return self._entry


class KeyValuesDialog(gtk.Dialog):
    def __init__(self, auth_lists, final_callback, uuid, settings):
        # This must not be "modal", else the "chooser" widgets won't
        # accept anything !!
        gtk.Dialog.__init__(self)
        self.set_title(_('Wireless Parameters required'))

        self._spacing_between_children_widgets = 5
        self._auth_lists = auth_lists
        self._final_callback = final_callback
        self._uuid = uuid
        self._settings = settings

        label = gtk.Label(_("Please enter parameters\n"))
        self.vbox.set_spacing(self._spacing_between_children_widgets)
        self.vbox.pack_start(label)

        self._auth_type_store = gtk.ListStore(str, str)
        for auth_list in self._auth_lists:
            self._auth_type_store.append([auth_list._auth_label,
                                          auth_list._auth_type])

        self._auth_type_combo = gtk.ComboBox(self._auth_type_store)
        cell = gtk.CellRendererText()
        self._auth_type_combo.pack_start(cell, True)
        self._auth_type_combo.add_attribute(cell, 'text', 0)
        self._auth_type_combo.set_active(0)
        self._auth_type_combo.connect('changed',
                self._auth_type_combo_changed_cb)
        self._auth_type_box = gtk.HBox(homogeneous=True)
        self._auth_label = gtk.Label(_('Authentication'))
        self._auth_type_box.pack_start(self._auth_label, expand=False)
        self._auth_type_box.pack_start(self._auth_type_combo,
                                       expand=False)
        self.vbox.pack_start(self._auth_type_box)
        self._auth_label.show()
        self._auth_type_combo.show()

        self.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK)
        self.set_default_response(gtk.RESPONSE_OK)
        self.set_has_separator(True)

        self.connect('response', self._fetch_values)

        auth_type = self._auth_lists[0]._auth_type
        self._selected_auth_list = self._select_auth_list(auth_type)
        self._add_key_value('eap', auth_type)
        self._add_container_box()

    def _auth_type_combo_changed_cb(self, widget):
        it = self._auth_type_combo.get_active_iter()
        (auth_type, ) = self._auth_type_store.get(it, 1)
        self._selected_auth_list = self._select_auth_list(auth_type)
        self._add_key_value('eap', auth_type)
        self._reset()

    def _select_auth_list(self, auth_type):
        for auth_list in self._auth_lists:
            if auth_list._params_list[0]._options[0][1] == auth_type:
                return auth_list

    def _populate_auth_params(self, auth_list):
        for auth_param in auth_list._params_list[1:]:
            obj = NetworkParameters(auth_param)
            self._key_values_box.pack_start(obj)
            obj.show()

    def _reset(self):
        self.vbox.remove(self._key_values_box)
        self._add_container_box()

    def _add_container_box(self):
        self._key_values_box = \
                gtk.VBox(spacing=self._spacing_between_children_widgets)
        self.vbox.pack_start(self._key_values_box)
        self._key_values_box.show()
        self._populate_auth_params(self._selected_auth_list)

    def _remove_all_params(self):
        self._key_values_box.remove_all()

    def _fetch_values(self, key_dialog, response_id):
        if response_id == gtk.RESPONSE_OK:
            for child in self._key_values_box.get_children():
                key = child._get_key()
                value = child._get_value()
                self._add_key_value(key, value)

            key_dialog.destroy()
            self._final_callback(self._uuid, self._settings,
                                 self._selected_auth_list)

    def _add_key_value(self, key, value):
        for auth_param in self._selected_auth_list._params_list:
            if auth_param._key_name == key:
                if (auth_param._key_type == SETTING_TYPE_STRING) or \
                   (auth_param._key_type == SETTING_TYPE_CHOOSER):
                    auth_param._value = value
                elif auth_param._key_type == SETTING_TYPE_LIST:
                    values = []
                    values.append(value)
                    auth_param._value = values


class WEPKeyDialog(KeyDialog):
    def __init__(self, ssid, flags, wpa_flags, rsn_flags, dev_caps, settings,
                 response):
        KeyDialog.__init__(self, ssid, flags, wpa_flags, rsn_flags,
                           dev_caps, settings, response)

        # WEP key type
        self.key_store = gtk.ListStore(str, int)
        self.key_store.append(['Passphrase (128-bit)', WEP_PASSPHRASE])
        self.key_store.append(['Hex (40/128-bit)', WEP_HEX])
        self.key_store.append(['ASCII (40/128-bit)', WEP_ASCII])

        self.key_combo = gtk.ComboBox(self.key_store)
        cell = gtk.CellRendererText()
        self.key_combo.pack_start(cell, True)
        self.key_combo.add_attribute(cell, 'text', 0)
        self.key_combo.set_active(0)
        self.key_combo.connect('changed', self._key_combo_changed_cb)

        hbox = gtk.HBox()
        hbox.pack_start(gtk.Label(_('Key Type:')))
        hbox.pack_start(self.key_combo)
        hbox.show_all()
        self.vbox.pack_start(hbox)

        # Key entry field
        self.add_key_entry()

        # WEP authentication mode
        self.auth_store = gtk.ListStore(str, str)
        self.auth_store.append(['Open System', IW_AUTH_ALG_OPEN_SYSTEM])
        self.auth_store.append(['Shared Key', IW_AUTH_ALG_SHARED_KEY])

        self.auth_combo = gtk.ComboBox(self.auth_store)
        cell = gtk.CellRendererText()
        self.auth_combo.pack_start(cell, True)
        self.auth_combo.add_attribute(cell, 'text', 0)
        self.auth_combo.set_active(0)

        hbox = gtk.HBox()
        hbox.pack_start(gtk.Label(_('Authentication Type:')))
        hbox.pack_start(self.auth_combo)
        hbox.show_all()

        self.vbox.pack_start(hbox)

    def _key_combo_changed_cb(self, widget):
        self._update_response_sensitivity()

    def _get_security(self):
        key = self._entry.get_text()

        it = self.key_combo.get_active_iter()
        (key_type, ) = self.key_store.get(it, 1)

        if key_type == WEP_PASSPHRASE:
            key = hash_passphrase(key)
        elif key_type == WEP_ASCII:
            key = string_to_hex(key)

        it = self.auth_combo.get_active_iter()
        (auth_alg, ) = self.auth_store.get(it, 1)

        return (key, auth_alg)

    def print_security(self):
        (key, auth_alg) = self._get_security()
        print 'Key: %s' % key
        print 'Auth: %d' % auth_alg

    def create_security(self):
        (key, auth_alg) = self._get_security()
        secrets = Secrets(self._settings)
        secrets.wep_key = key
        secrets.auth_alg = auth_alg
        return secrets

    def _update_response_sensitivity(self, ignored=None):
        key = self._entry.get_text()
        it = self.key_combo.get_active_iter()
        (key_type, ) = self.key_store.get(it, 1)

        valid = False
        if key_type == WEP_PASSPHRASE:
            # As the md5 passphrase can be of any length and has no indicator,
            # we cannot check for the validity of the input.
            if len(key) > 0:
                valid = True
        elif key_type == WEP_ASCII:
            if len(key) == 5 or len(key) == 13:
                valid = string_is_ascii(key)
        elif key_type == WEP_HEX:
            if len(key) == 10 or len(key) == 26:
                valid = string_is_hex(key)

        self.set_response_sensitive(gtk.RESPONSE_OK, valid)


class WPAPersonalKeyDialog(KeyDialog):
    def __init__(self, ssid, flags, wpa_flags, rsn_flags, dev_caps, settings,
                 response):
        KeyDialog.__init__(self, ssid, flags, wpa_flags, rsn_flags,
                           dev_caps, settings, response)
        self.add_key_entry()

        self.store = gtk.ListStore(str)
        self.store.append([_('WPA & WPA2 Personal')])

        self.combo = gtk.ComboBox(self.store)
        cell = gtk.CellRendererText()
        self.combo.pack_start(cell, True)
        self.combo.add_attribute(cell, 'text', 0)
        self.combo.set_active(0)

        self.hbox = gtk.HBox()
        self.hbox.pack_start(gtk.Label(_('Wireless Security:')))
        self.hbox.pack_start(self.combo)
        self.hbox.show_all()

        self.vbox.pack_start(self.hbox)

    def _get_security(self):
        ssid = self._ssid
        key = self._entry.get_text()
        is_hex = string_is_hex(key)

        real_key = None
        if len(key) == 64 and is_hex:
            # Hex key
            real_key = key
        elif len(key) >= 8 and len(key) <= 63:
            # passphrase
            from subprocess import Popen, PIPE
            p = Popen(['wpa_passphrase', ssid, key], stdout=PIPE)
            for line in p.stdout:
                if line.strip().startswith('psk='):
                    real_key = line.strip()[4:]
            if p.wait() != 0:
                raise RuntimeError('Error hashing passphrase')
            if real_key and len(real_key) != 64:
                real_key = None

        if not real_key:
            raise RuntimeError('Invalid key')

        return real_key

    def print_security(self):
        key = self._get_security()
        print 'Key: %s' % key

    def create_security(self):
        secrets = Secrets(self._settings)
        secrets.psk = self._get_security()
        return secrets

    def _update_response_sensitivity(self, ignored=None):
        key = self._entry.get_text()
        is_hex = string_is_hex(key)

        valid = False
        if len(key) == 64 and is_hex:
            # hex key
            valid = True
        elif len(key) >= 8 and len(key) <= 63:
            # passphrase
            valid = True
        self.set_response_sensitive(gtk.RESPONSE_OK, valid)
        return False


def create(ssid, flags, wpa_flags, rsn_flags, dev_caps, settings, response):
    if wpa_flags == network.NM_802_11_AP_SEC_NONE and \
            rsn_flags == network.NM_802_11_AP_SEC_NONE:
        key_dialog = WEPKeyDialog(ssid, flags, wpa_flags, rsn_flags,
                                  dev_caps, settings, response)
    elif (wpa_flags & network.NM_802_11_AP_SEC_KEY_MGMT_PSK) or \
            (rsn_flags & network.NM_802_11_AP_SEC_KEY_MGMT_PSK):
        key_dialog = WPAPersonalKeyDialog(ssid, flags, wpa_flags, rsn_flags,
                                  dev_caps, settings, response)
    elif (wpa_flags & network.NM_802_11_AP_SEC_KEY_MGMT_802_1X) or \
            (rsn_flags & network.NM_802_11_AP_SEC_KEY_MGMT_802_1X):
        # nothing. All details are asked for WPA/WPA2-Enterprise
        # networks, before the conneection-activation is done.
        return

    key_dialog.connect('response', _key_dialog_response_cb)
    key_dialog.show_all()


def get_key_values(key_list, final_callback, uuid, settings):
    key_dialog = KeyValuesDialog(key_list, final_callback,
                                 uuid, settings)
    key_dialog.show_all()


def _key_dialog_response_cb(key_dialog, response_id):
    response = key_dialog.get_response_object()
    secrets = None
    if response_id == gtk.RESPONSE_OK:
        secrets = key_dialog.create_security()

    if response_id in [gtk.RESPONSE_CANCEL, gtk.RESPONSE_NONE,
                       gtk.RESPONSE_DELETE_EVENT]:
        # key dialog dialog was canceled; send the error back to NM
        response.set_error(CanceledKeyRequestError())
    elif response_id == gtk.RESPONSE_OK:
        if not secrets:
            raise RuntimeError('Invalid security arguments.')
        response.set_secrets(secrets)
    else:
        raise RuntimeError('Unhandled key dialog response %d' % response_id)

    key_dialog.destroy()