From f0c5cd080c6416f34854c3efda702d020ba07c45 Mon Sep 17 00:00:00 2001 From: James Valleroy Date: Tue, 17 Mar 2015 08:28:04 -0400 Subject: [PATCH] Add wifi security settings. --- plinth/modules/networks/forms.py | 4 +++ plinth/modules/networks/networks.py | 38 ++++++++++++++++++++++++++--- plinth/network.py | 24 ++++++++++++++++-- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/plinth/modules/networks/forms.py b/plinth/modules/networks/forms.py index fb9927193..077c8592e 100644 --- a/plinth/modules/networks/forms.py +++ b/plinth/modules/networks/forms.py @@ -41,6 +41,10 @@ class AddWifiForm(forms.Form): """Form to create a new wifi connection.""" name = forms.CharField(label=_('Connection Name')) ssid = forms.CharField(label=_('SSID')) + auth_mode = forms.ChoiceField( + label=_('Authentication Mode'), + choices=[('wpa', 'WPA'), ('open', 'Open')]) + passphrase = forms.CharField(label=_('Passphrase'), required=False) ipv4_method = forms.ChoiceField( label=_('IPv4 Addressing Method'), choices=[('auto', 'Automatic (DHCP)'), ('manual', 'Manual')]) diff --git a/plinth/modules/networks/networks.py b/plinth/modules/networks/networks.py index a2cb706a6..849fd6e97 100644 --- a/plinth/modules/networks/networks.py +++ b/plinth/modules/networks/networks.py @@ -83,7 +83,13 @@ def edit(request, conn_id): network.edit_ethernet_connection(conn, name, ipv4_method, ipv4_address) elif settings['connection']['type'] == '802-11-wireless': ssid = form.cleaned_data['ssid'] - network.edit_wifi_connection(conn, name, ssid, ipv4_method, ipv4_address) + auth_mode = form.cleaned_data['auth_mode'] + passphrase = form.cleaned_data['passphrase'] + + network.edit_wifi_connection( + conn, name, + ssid, auth_mode, passphrase, + ipv4_method, ipv4_address) else: messages.error( request, @@ -98,6 +104,17 @@ def edit(request, conn_id): if settings['connection']['type'] == '802-11-wireless': form_data['ssid'] = settings['802-11-wireless']['ssid'] + try: + if (settings['802-11-wireless']['security'] == '802-11-wireless-security' + and settings['802-11-wireless-security']['key-mgmt'] == 'wpa-psk'): + form_data['auth_mode'] = 'wpa' + secrets = conn.GetSecrets() + form_data['passphrase'] = secrets['802-11-wireless-security']['psk'] + else: + form_data['auth_mode'] = 'open' + except KeyError: + form_data['auth_mode'] = 'open' + form = AddWifiForm(form_data) else: form = AddEthernetForm(form_data) @@ -153,17 +170,25 @@ def connect(request, connect_path): """Create a new wifi connection to an existing AP.""" form = None ssid = urllib.parse.unquote_plus(connect_path) - form_data = {'name': ssid, 'ssid': ssid, 'ipv4_method': 'auto'} + form_data = {'name': ssid, + 'ssid': ssid, + 'auth_mode': 'wpa', + 'ipv4_method': 'auto'} if request.method == 'POST': form = AddWifiForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] ssid = form.cleaned_data['ssid'] + auth_mode = form.cleaned_data['auth_mode'] + passphrase = form.cleaned_data['passphrase'] ipv4_method = form.cleaned_data['ipv4_method'] ipv4_address = form.cleaned_data['ipv4_address'] - network.add_wifi_connection(name, ssid, ipv4_method, ipv4_address) + network.add_wifi_connection( + name, + ssid, auth_mode, passphrase, + ipv4_method, ipv4_address) return redirect(reverse_lazy('networks:index')) else: form = AddWifiForm(form_data) @@ -228,10 +253,15 @@ def add_wifi(request): if form.is_valid(): name = form.cleaned_data['name'] ssid = form.cleaned_data['ssid'] + auth_mode = form.cleaned_data['auth_mode'] + passphrase = form.cleaned_data['passphrase'] ipv4_method = form.cleaned_data['ipv4_method'] ipv4_address = form.cleaned_data['ipv4_address'] - network.add_wifi_connection(name, ssid, ipv4_method, ipv4_address) + network.add_wifi_connection( + name, + ssid, auth_mode, passphrase, + ipv4_method, ipv4_address) return redirect(reverse_lazy('networks:index')) else: form = AddWifiForm() diff --git a/plinth/network.py b/plinth/network.py index 136886edf..f7cd6e00b 100644 --- a/plinth/network.py +++ b/plinth/network.py @@ -19,6 +19,7 @@ Helper functions for working with network manager. """ +from dbus.exceptions import DBusException from gettext import gettext as _ import NetworkManager import uuid @@ -115,7 +116,9 @@ def edit_ethernet_connection(conn, name, ipv4_method, ipv4_address): conn.Update(new_settings) -def edit_wifi_connection(conn, name, ssid, ipv4_method, ipv4_address): +def edit_wifi_connection(conn, name, + ssid, auth_mode, passphrase, + ipv4_method, ipv4_address): settings = conn.GetSettings() new_settings = { @@ -129,6 +132,14 @@ def edit_wifi_connection(conn, name, ssid, ipv4_method, ipv4_address): }, 'ipv4': {'method': ipv4_method}, } + + if auth_mode == 'wpa' and passphrase: + new_settings['connection']['security'] = '802-11-wireless-security' + new_settings['802-11-wireless-security'] = { + 'key-mgmt': 'wpa-psk', + 'psk': passphrase, + } + if ipv4_method == 'manual' and ipv4_address: new_settings['ipv4']['addresses'] = [ (ipv4_address, @@ -206,7 +217,9 @@ def add_ethernet_connection(name, ipv4_method, ipv4_address): NetworkManager.Settings.AddConnection(conn) -def add_wifi_connection(name, ssid, ipv4_method, ipv4_address): +def add_wifi_connection(name, + ssid, auth_mode, passphrase, + ipv4_method, ipv4_address): conn = { 'connection': { 'id': name, @@ -219,6 +232,13 @@ def add_wifi_connection(name, ssid, ipv4_method, ipv4_address): 'ipv4': {'method': ipv4_method}, } + if auth_mode == 'wpa' and passphrase: + conn['connection']['security'] = '802-11-wireless-security' + conn['802-11-wireless-security'] = { + 'key-mgmt': 'wpa-psk', + 'psk': passphrase, + } + if ipv4_method == 'manual' and ipv4_address: conn['ipv4']['addresses'] = [ (ipv4_address,