GCC Code Coverage Report


Directory: ./
File: panels/network/cc-wifi-hotspot-dialog.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 251 0.0%
Functions: 0 23 0.0%
Branches: 0 189 0.0%

Line Branch Exec Source
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* cc-wifi-hotspot-dialog.c
3 *
4 * Copyright 2019 Purism SPC
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author(s):
20 * Mohammed Sadiq <sadiq@sadiqpk.org>
21 *
22 * SPDX-License-Identifier: GPL-3.0-or-later
23 */
24
25 #undef G_LOG_DOMAIN
26 #define G_LOG_DOMAIN "cc-wifi-hotspot-dialog"
27
28 #include <config.h>
29 #include <glib/gi18n.h>
30 #include <libmm-glib.h>
31
32 #include "cc-wifi-hotspot-dialog.h"
33 #include "cc-network-resources.h"
34 #include "ui-helpers.h"
35
36 /**
37 * @short_description: WWAN network type selection dialog
38 */
39
40 struct _CcWifiHotspotDialog
41 {
42 GtkDialog parent_instance;
43
44 GtkLabel *connection_label;
45 GtkEntry *name_entry;
46 GtkLabel *name_error_label;
47 GtkEntry *password_entry;
48 GtkLabel *password_error_label;
49 GtkButton *ok_button;
50
51 GCancellable *cancellable;
52
53 NMDeviceWifi *device;
54 NMConnection *connection;
55 gchar *host_name;
56 gboolean wpa_supported; /* WPA/WPA2 supported */
57 };
58
59 G_DEFINE_TYPE (CcWifiHotspotDialog, cc_wifi_hotspot_dialog, GTK_TYPE_DIALOG)
60
61 static gchar *
62 get_random_wpa_key (void)
63 {
64 gchar *key;
65 gint i;
66
67 key = g_malloc (10 * sizeof (key));
68 for (i = 0; i < 8; i++)
69 {
70 gint c = 0;
71 /* too many non alphanumeric characters are hard to remember for humans */
72 while (!g_ascii_isalnum (c))
73 c = g_random_int_range (33, 126);
74
75 key[i] = (gchar) c;
76 }
77 key[i] = '\0';
78
79 return key;
80 }
81
82 static gchar *
83 get_random_wep_key (void)
84 {
85 const gchar *hexdigits = "0123456789abcdef";
86 gchar *key;
87 gint i;
88
89 key = g_malloc (12 * sizeof (key));
90
91 /* generate a 10-digit hex WEP key */
92 for (i = 0; i < 10; i++)
93 {
94 gint digit;
95 digit = g_random_int_range (0, 16);
96 key[i] = hexdigits[digit];
97 }
98
99 key[i] = '\0';
100
101 return key;
102 }
103
104 static void
105 wifi_hotspot_dialog_update_main_label (CcWifiHotspotDialog *self)
106 {
107 NMAccessPoint *ap;
108 GBytes *ssid = NULL;
109 g_autofree gchar *active_ssid = NULL;
110 g_autofree gchar *escape = NULL;
111 g_autofree gchar *ssid_text = NULL;
112 g_autofree gchar *label = NULL;
113
114 g_assert (CC_IS_WIFI_HOTSPOT_DIALOG (self));
115
116 gtk_label_set_markup (self->connection_label, "");
117
118 if (!self->device)
119 return;
120
121 ap = nm_device_wifi_get_active_access_point (self->device);
122
123 if (ap)
124 ssid = nm_access_point_get_ssid (ap);
125 if (ssid)
126 active_ssid = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL), g_bytes_get_size (ssid));
127
128 if (!active_ssid || !*active_ssid)
129 return;
130
131 escape = g_markup_escape_text (active_ssid, -1);
132 ssid_text = g_strdup_printf ("<b>%s</b>", escape);
133 /* TRANSLATORS: ā€˜%sā€™ is a Wi-Fi Network(SSID) name */
134 label = g_strdup_printf (_("Turning on the hotspot will disconnect from %s, "
135 "and it will not be possible to access the internet through Wi-Fi."), ssid_text);
136 gtk_label_set_markup (self->connection_label, label);
137 }
138
139 static void
140 get_secrets_cb (GObject *source_object,
141 GAsyncResult *res,
142 gpointer data)
143 {
144 CcWifiHotspotDialog *self;
145 g_autoptr(GVariant) secrets = NULL;
146 NMSettingWirelessSecurity *security_setting;
147 const gchar *key;
148 g_autoptr(GError) error = NULL;
149
150 secrets = nm_remote_connection_get_secrets_finish (NM_REMOTE_CONNECTION (source_object), res, &error);
151 if (!secrets)
152 {
153 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
154 g_warning ("Could not get secrets: %s", error->message);
155 return;
156 }
157
158 self = CC_WIFI_HOTSPOT_DIALOG (data);
159
160 nm_connection_update_secrets (self->connection,
161 NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
162 secrets, &error);
163 if (error)
164 {
165 g_warning ("Error updating secrets: %s", error->message);
166 return;
167 }
168
169 security_setting = nm_connection_get_setting_wireless_security (self->connection);
170 if (self->wpa_supported)
171 key = nm_setting_wireless_security_get_psk (security_setting);
172 else
173 key = nm_setting_wireless_security_get_wep_key (security_setting, 0);
174
175 if (key)
176 gtk_editable_set_text (GTK_EDITABLE (self->password_entry), key);
177
178 nm_connection_clear_secrets (self->connection);
179 }
180
181 static void
182 wifi_hotspot_dialog_update_entries (CcWifiHotspotDialog *self)
183 {
184 NMSettingWireless *setting;
185 GBytes *ssid;
186 g_autofree gchar *ssid_text = NULL;
187
188 g_assert (CC_IS_WIFI_HOTSPOT_DIALOG (self));
189
190 gtk_editable_set_text (GTK_EDITABLE (self->name_entry), "");
191 gtk_editable_set_text (GTK_EDITABLE (self->password_entry), "");
192
193 if (!self->connection)
194 return;
195
196 setting = nm_connection_get_setting_wireless (self->connection);
197
198 ssid = nm_setting_wireless_get_ssid (setting);
199 ssid_text = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL), g_bytes_get_size (ssid));
200
201 if (!ssid_text && self->host_name)
202 ssid_text = g_strdup (self->host_name);
203
204 if (ssid_text)
205 gtk_editable_set_text (GTK_EDITABLE (self->name_entry), ssid_text);
206
207 if (!NM_IS_REMOTE_CONNECTION (self->connection))
208 return;
209
210 /* Secrets may not be already loaded, we have to manually load it. */
211 nm_remote_connection_get_secrets_async (NM_REMOTE_CONNECTION (self->connection),
212 NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
213 self->cancellable,
214 get_secrets_cb,
215 self);
216 }
217
218 static gboolean
219 hotspot_password_is_valid (CcWifiHotspotDialog *self,
220 const gchar *password)
221 {
222 g_assert (CC_IS_WIFI_HOTSPOT_DIALOG (self));
223
224 if (!self->device)
225 return FALSE;
226
227 if (!password || !*password)
228 return TRUE;
229
230 if (self->wpa_supported)
231 return nm_utils_wpa_psk_valid (password);
232 else
233 return nm_utils_wep_key_valid (password, NM_WEP_KEY_TYPE_KEY);
234 }
235
236 static void
237 hotspot_entry_changed_cb (CcWifiHotspotDialog *self)
238 {
239 const gchar *ssid, *ssid_error_label, *password, *password_error_label;
240 gboolean valid_ssid, valid_password;
241
242 g_assert (CC_IS_WIFI_HOTSPOT_DIALOG (self));
243
244 valid_ssid = valid_password = FALSE;
245 ssid = gtk_editable_get_text (GTK_EDITABLE (self->name_entry));
246 password = gtk_editable_get_text (GTK_EDITABLE (self->password_entry));
247
248 if (!ssid || !*ssid)
249 {
250 ssid_error_label = _("Network name cannot be empty");
251 widget_set_error (GTK_WIDGET (self->name_entry));
252 }
253 else if (strlen (ssid) > 32)
254 {
255 /* SSID length needs to be in the 1-32 byte range */
256 ssid_error_label = _("Network name is too long");
257 widget_set_error (GTK_WIDGET (self->name_entry));
258 }
259 else
260 {
261 valid_ssid = TRUE;
262 ssid_error_label = "";
263 widget_unset_error (GTK_WIDGET (self->name_entry));
264 }
265
266 valid_password = hotspot_password_is_valid (self, password);
267
268 if (valid_password)
269 {
270 password_error_label = "";
271 widget_unset_error (GTK_WIDGET (self->password_entry));
272 }
273 else
274 {
275 if (strlen (password) < 8)
276 {
277 password_error_label = _("Must have a minimum of 8 characters");
278 }
279 else
280 {
281 guint max_chars = self->wpa_supported ? 63 : 16;
282 password_error_label = g_strdup_printf (ngettext ("Must have a maximum of %d character",
283 "Must have a maximum of %d characters", max_chars), max_chars);
284 }
285
286 widget_set_error (GTK_WIDGET(self->password_entry));
287 }
288
289 gtk_label_set_label (self->name_error_label, ssid_error_label);
290 gtk_label_set_label (self->password_error_label, password_error_label);
291 gtk_widget_set_sensitive (GTK_WIDGET (self->ok_button),
292 valid_ssid && valid_password);
293 }
294
295 static void
296 generate_password_clicked_cb (CcWifiHotspotDialog *self)
297 {
298 g_autofree gchar *key = NULL;
299
300 g_assert (CC_IS_WIFI_HOTSPOT_DIALOG (self));
301
302 if (self->wpa_supported)
303 key = get_random_wpa_key ();
304 else
305 key = get_random_wep_key ();
306
307 gtk_editable_set_text (GTK_EDITABLE (self->password_entry), key);
308 }
309
310 static void
311 hotspot_update_wireless_settings (CcWifiHotspotDialog *self)
312 {
313 NMSettingWireless *setting;
314 g_autoptr(GBytes) ssid = NULL;
315 const gchar *ssid_text;
316 NMDeviceWifiCapabilities capabilities;
317
318 g_assert (CC_IS_WIFI_HOTSPOT_DIALOG (self));
319
320 if (nm_connection_get_setting_wireless (self->connection) == NULL)
321 nm_connection_add_setting (self->connection, nm_setting_wireless_new ());
322
323 setting = nm_connection_get_setting_wireless (self->connection);
324
325 capabilities = nm_device_wifi_get_capabilities (self->device);
326 if (capabilities & NM_WIFI_DEVICE_CAP_AP)
327 g_object_set (setting, "mode", "ap", NULL);
328 else
329 g_object_set (setting, "mode", "adhoc", NULL);
330
331 ssid_text = gtk_editable_get_text (GTK_EDITABLE (self->name_entry));
332 ssid = g_bytes_new (ssid_text, strlen (ssid_text));
333 g_object_set (setting, "ssid", ssid, NULL);
334 }
335
336 static void
337 hotspot_update_wireless_security_settings (CcWifiHotspotDialog *self)
338 {
339 NMSettingWirelessSecurity *setting;
340 const gchar *value, *key_type;
341
342 g_assert (CC_IS_WIFI_HOTSPOT_DIALOG (self));
343
344 if (nm_connection_get_setting_wireless_security (self->connection) == NULL)
345 nm_connection_add_setting (self->connection, nm_setting_wireless_security_new ());
346
347 setting = nm_connection_get_setting_wireless_security (self->connection);
348 nm_setting_wireless_security_clear_protos (setting);
349 nm_setting_wireless_security_clear_pairwise (setting);
350 nm_setting_wireless_security_clear_groups (setting);
351 value = gtk_editable_get_text (GTK_EDITABLE (self->password_entry));
352
353 if (self->wpa_supported)
354 key_type = "psk";
355 else
356 key_type = "wep-key0";
357
358 if (self->wpa_supported)
359 g_object_set (setting, "key-mgmt", "wpa-psk", NULL);
360 else
361 g_object_set (setting,
362 "key-mgmt", "none",
363 "wep-key-type", NM_WEP_KEY_TYPE_KEY,
364 NULL);
365
366 if (!value || !*value)
367 {
368 g_autofree gchar *key = NULL;
369
370 if (self->wpa_supported)
371 key = get_random_wpa_key ();
372 else
373 key = get_random_wep_key ();
374
375 g_object_set (setting, key_type, key, NULL);
376 }
377 else
378 g_object_set (setting, key_type, value, NULL);
379
380 if (self->wpa_supported)
381 {
382 NMDeviceWifiCapabilities caps;
383
384 caps = nm_device_wifi_get_capabilities (self->device);
385
386 if (caps & NM_WIFI_DEVICE_CAP_RSN)
387 {
388 nm_setting_wireless_security_add_proto (setting, "rsn");
389 nm_setting_wireless_security_add_pairwise (setting, "ccmp");
390 nm_setting_wireless_security_add_group (setting, "ccmp");
391 }
392 else if (caps & NM_WIFI_DEVICE_CAP_WPA)
393 {
394 nm_setting_wireless_security_add_proto (setting, "wpa");
395 nm_setting_wireless_security_add_pairwise (setting, "tkip");
396 nm_setting_wireless_security_add_group (setting, "tkip");
397 }
398 }
399 }
400
401 static void
402 cc_wifi_hotspot_dialog_finalize (GObject *object)
403 {
404 CcWifiHotspotDialog *self = (CcWifiHotspotDialog *)object;
405
406 g_cancellable_cancel(self->cancellable);
407 g_clear_object (&self->cancellable);
408 g_clear_pointer (&self->host_name, g_free);
409 g_clear_object (&self->device);
410 g_clear_object (&self->connection);
411
412 G_OBJECT_CLASS (cc_wifi_hotspot_dialog_parent_class)->finalize (object);
413 }
414
415 static void
416 cc_wifi_hotspot_dialog_show (GtkWidget *widget)
417 {
418 CcWifiHotspotDialog *self = (CcWifiHotspotDialog *)widget;
419 g_warn_if_fail (self->device != NULL);
420
421 gtk_widget_grab_focus (GTK_WIDGET (self->ok_button));
422 wifi_hotspot_dialog_update_entries (self);
423
424 if (!self->connection)
425 if (self->host_name)
426 gtk_editable_set_text (GTK_EDITABLE (self->name_entry), self->host_name);
427
428 GTK_WIDGET_CLASS (cc_wifi_hotspot_dialog_parent_class)->show (widget);
429 }
430
431 static void
432 cc_wifi_hotspot_dialog_response (GtkDialog *dialog,
433 gint response_id)
434 {
435 CcWifiHotspotDialog *self = CC_WIFI_HOTSPOT_DIALOG (dialog);
436 NMSetting *setting;
437
438 if (response_id != GTK_RESPONSE_APPLY)
439 return;
440
441 if (!self->connection)
442 self->connection = NM_CONNECTION (nm_simple_connection_new ());
443
444 if (nm_connection_get_setting_connection (self->connection) == NULL)
445 {
446 setting = nm_setting_connection_new ();
447 g_object_set (setting,
448 "type", "802-11-wireless",
449 "id", "Hotspot",
450 "autoconnect", FALSE,
451 NULL);
452 nm_connection_add_setting (self->connection, setting);
453 }
454
455 if (nm_connection_get_setting_ip4_config (self->connection) == NULL)
456 {
457 setting = nm_setting_ip4_config_new ();
458 g_object_set (setting, "method", "shared", NULL);
459 nm_connection_add_setting (self->connection, setting);
460 }
461
462 hotspot_update_wireless_settings (self);
463 hotspot_update_wireless_security_settings (self);
464 }
465
466 static void
467 cc_wifi_hotspot_dialog_class_init (CcWifiHotspotDialogClass *klass)
468 {
469 GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
470 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
471 GObjectClass *object_class = G_OBJECT_CLASS (klass);
472
473 object_class->finalize = cc_wifi_hotspot_dialog_finalize;
474
475 widget_class->show = cc_wifi_hotspot_dialog_show;
476 dialog_class->response = cc_wifi_hotspot_dialog_response;
477
478 gtk_widget_class_set_template_from_resource (widget_class,
479 "/org/gnome/control-center/network/cc-wifi-hotspot-dialog.ui");
480
481 gtk_widget_class_bind_template_child (widget_class, CcWifiHotspotDialog, connection_label);
482 gtk_widget_class_bind_template_child (widget_class, CcWifiHotspotDialog, name_entry);
483 gtk_widget_class_bind_template_child (widget_class, CcWifiHotspotDialog, name_error_label);
484 gtk_widget_class_bind_template_child (widget_class, CcWifiHotspotDialog, password_entry);
485 gtk_widget_class_bind_template_child (widget_class, CcWifiHotspotDialog, password_error_label);
486 gtk_widget_class_bind_template_child (widget_class, CcWifiHotspotDialog, ok_button);
487
488 gtk_widget_class_bind_template_callback (widget_class, hotspot_entry_changed_cb);
489 gtk_widget_class_bind_template_callback (widget_class, generate_password_clicked_cb);
490 }
491
492 static void
493 cc_wifi_hotspot_dialog_init (CcWifiHotspotDialog *self)
494 {
495 self->cancellable = g_cancellable_new ();
496
497 gtk_widget_init_template (GTK_WIDGET (self));
498 }
499
500 CcWifiHotspotDialog *
501 cc_wifi_hotspot_dialog_new (GtkWindow *parent_window)
502 {
503 g_return_val_if_fail (GTK_IS_WINDOW (parent_window), NULL);
504
505 return g_object_new (CC_TYPE_WIFI_HOTSPOT_DIALOG,
506 "use-header-bar", TRUE,
507 "transient-for", parent_window,
508 NULL);
509 }
510
511 void
512 cc_wifi_hotspot_dialog_set_hostname (CcWifiHotspotDialog *self,
513 const gchar *host_name)
514 {
515 g_return_if_fail (CC_IS_WIFI_HOTSPOT_DIALOG (self));
516
517 g_clear_pointer (&self->host_name, g_free);
518 self->host_name = g_strdup (host_name);
519 }
520
521 void
522 cc_wifi_hotspot_dialog_set_device (CcWifiHotspotDialog *self,
523 NMDeviceWifi *device)
524 {
525 g_return_if_fail (CC_IS_WIFI_HOTSPOT_DIALOG (self));
526 g_return_if_fail (NM_IS_DEVICE_WIFI (device));
527
528 g_set_object (&self->device, device);
529
530 if (device)
531 {
532 NMDeviceWifiCapabilities caps;
533
534 caps = nm_device_wifi_get_capabilities (device);
535 self->wpa_supported = FALSE;
536
537 if (caps & NM_WIFI_DEVICE_CAP_AP)
538 if (caps & (NM_WIFI_DEVICE_CAP_RSN | NM_WIFI_DEVICE_CAP_WPA))
539 self->wpa_supported = TRUE;
540 }
541
542 wifi_hotspot_dialog_update_main_label (self);
543 }
544
545 NMConnection *
546 cc_wifi_hotspot_dialog_get_connection (CcWifiHotspotDialog *self)
547 {
548 g_return_val_if_fail (CC_IS_WIFI_HOTSPOT_DIALOG (self), NULL);
549
550 return self->connection;
551 }
552
553 void
554 cc_wifi_hotspot_dialog_set_connection (CcWifiHotspotDialog *self,
555 NMConnection *connection)
556 {
557 NMSettingWireless *setting;
558
559 g_return_if_fail (CC_IS_WIFI_HOTSPOT_DIALOG (self));
560 g_return_if_fail (NM_IS_CONNECTION (connection));
561
562 setting = nm_connection_get_setting_wireless (connection);
563 g_return_if_fail (setting);
564
565 g_set_object (&self->connection, connection);
566 }
567