mcde: Wi-Fi in System Settings: the networks in reach, joining one
McdeSys implements mcde_sys_wifi_scan and mcde_sys_wifi_join. NetBSD does what sysinst does (src/usr.sbin/sysinst/net.c): a network block in /etc/wpa_supplicant.conf -- a new file gets the example's ctrl_interface for the wheel group, so wpa_cli answers users -- and wpa_supplicant, wpa_supplicant_flags and dhcpcd in rc.conf, each line replaced or added; then both are restarted. Scanning asks wpa_cli scan_results, else ifconfig -v IF list scan (root). Linux uses nmcli. The sources are in docs/reference/wifi.txt. The Network pane lists the networks of the first Wi-Fi card with signal and lock, scanned once the pane is drawn. Join... opens a terminal where su asks for the administrator's password and dtsystemsettings -join-wifi IF SSID-HEX asks for the network's password without echo: the name goes as hexadecimal, the password on no command line of MCDE's (nmcli takes it on its own on Linux; said in linux.c). Measured on the Linux host: test_sysinfo finds 5 networks on wlp0s20f3 (read only); the pane lists them on Xvfb; -join-wifi as a user refuses with "Only the administrator can join" and refuses a bad name. Not measured: joining (never on this host), NetBSD scanning and joining (no Wi-Fi in the build VM). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
db32002727
commit
dffeaf9b17
8 files changed
+643
-4
No files matched your search
@@ -5,7 +5,7 @@ MAINTAINERCLEANFILES = Makefile.in
|
||||
bin_PROGRAMS = dtsystemsettings
|
||||
|
||||
dtsystemsettings_SOURCES = main.c panes.c panes.h ui.h accounts.c update.c \
|
||||
input.c appearance.c desktop.c timezone.c
|
||||
input.c appearance.c desktop.c timezone.c wifi.c
|
||||
dtsystemsettings_CPPFLAGS = -I$(top_srcdir)/include $(DT_INCDIR) \
|
||||
-I$(top_builddir)/include $(XFT_CFLAGS) \
|
||||
-DCDE_INSTALLATION_TOP=\"$(CDE_INSTALLATION_TOP)\" \
|
||||
|
||||
@@ -345,6 +345,9 @@ MakeNetwork(Widget parent)
|
||||
netKeys[i] = kids[i];
|
||||
PaneButton(box, "Refresh", NetworkRefresh, NULL);
|
||||
NetworkRead();
|
||||
if (MakeWifi(box) != NULL)
|
||||
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, box,
|
||||
XmNpositionIndex, 3, NULL);
|
||||
return box;
|
||||
}
|
||||
|
||||
@@ -634,9 +637,13 @@ main(int argc, char **argv)
|
||||
return ApplyUpdate();
|
||||
else if (strcmp(argv[i], "-set-timezone") == 0 && i + 1 < argc)
|
||||
return SetTimezoneCommand(argv[i + 1]);
|
||||
else if (strcmp(argv[i], "-join-wifi") == 0 && i + 1 < argc)
|
||||
return JoinWifiCommand(argv[i + 1],
|
||||
i + 2 < argc ? argv[i + 2] : NULL);
|
||||
else {
|
||||
fprintf(stderr, "usage: dtsystemsettings [-pane ID | "
|
||||
"-apply-update | -set-timezone ZONE]\n");
|
||||
"-apply-update | -set-timezone ZONE | -join-wifi IF "
|
||||
"[SSID-HEX]]\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,10 @@ void ServerResource(Display *dpy, const char *name, const char *class,
|
||||
/* the pane in desktop.c, opened on its desktop or its screen saver page */
|
||||
Widget MakeDesktop(Widget parent);
|
||||
Widget MakeScreenSaver(Widget parent);
|
||||
/* the Wi-Fi part of the Network pane (wifi.c); NULL without a Wi-Fi card */
|
||||
Widget MakeWifi(Widget parent);
|
||||
/* dtsystemsettings -join-wifi IF [SSID in hex]: the terminal of "Join..." */
|
||||
int JoinWifiCommand(const char *ifname, const char *hexssid);
|
||||
/* the time zone in Date & Time (timezone.c) */
|
||||
Widget MakeZoneChooser(Widget parent);
|
||||
/* dtsystemsettings -set-timezone ZONE: what the terminal of "Set Time Zone"
|
||||
|
||||
@@ -0,0 +1,272 @@
|
||||
/*
|
||||
* wifi.c -- dtsystemsettings: the Wi-Fi networks in reach, and joining one
|
||||
*
|
||||
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
||||
*
|
||||
* Part of the Network pane: for the first Wi-Fi card, the networks McdeSys
|
||||
* finds (mcde_sys_wifi_scan) with their signal and whether they are
|
||||
* secured; "Scan" looks again, "Join..." joins the one chosen. Joining
|
||||
* changes the system (mcde_sys_wifi_join, root), so it runs in a terminal
|
||||
* where su(1) asks for the administrator's password -- the way Software
|
||||
* Update and the time zone do -- and there dtsystemsettings -join-wifi IF
|
||||
* [SSID] asks for the network's password without echo. The SSID goes on
|
||||
* that command line as hexadecimal, so no name a network may have reaches a
|
||||
* shell; the password goes on no command line of MCDE's. Without an SSID
|
||||
* the terminal looks for the networks itself, as root -- on NetBSD a user
|
||||
* cannot scan while wpa_supplicant does not run.
|
||||
*
|
||||
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
|
||||
*/
|
||||
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <X11/Intrinsic.h>
|
||||
#include <Xm/Xm.h>
|
||||
#include <Xm/List.h>
|
||||
#include <Xm/RowColumn.h>
|
||||
|
||||
#include <Mcde/McdeSys.h>
|
||||
|
||||
#include "ui.h"
|
||||
|
||||
#define MAX_NETS 64
|
||||
|
||||
static Widget netList, wifiNote;
|
||||
static McdeSysWifi nets[MAX_NETS];
|
||||
static int nnets;
|
||||
static char wifiIf[32];
|
||||
|
||||
static void
|
||||
Hex(const char *s, char *buf, size_t len)
|
||||
{
|
||||
size_t i, n = 0;
|
||||
|
||||
for (i = 0; s[i] != '\0' && n + 3 < len; i++)
|
||||
n += (size_t) snprintf(buf + n, len - n, "%02x",
|
||||
(unsigned char) s[i]);
|
||||
buf[n] = '\0';
|
||||
}
|
||||
|
||||
static int
|
||||
Unhex(const char *h, char *buf, size_t len)
|
||||
{
|
||||
size_t n = 0;
|
||||
unsigned int c;
|
||||
|
||||
while (h[0] != '\0' && h[1] != '\0' && n + 1 < len) {
|
||||
if (sscanf(h, "%2x", &c) != 1)
|
||||
return 0;
|
||||
buf[n++] = (char) c;
|
||||
h += 2;
|
||||
}
|
||||
buf[n] = '\0';
|
||||
return h[0] == '\0' && n > 0;
|
||||
}
|
||||
|
||||
static void
|
||||
Scan(void)
|
||||
{
|
||||
char text[128];
|
||||
int i;
|
||||
|
||||
XmListDeleteAllItems(netList);
|
||||
nnets = mcde_sys_wifi_scan(wifiIf, nets, MAX_NETS);
|
||||
if (nnets < 0) {
|
||||
nnets = 0;
|
||||
SetText(wifiNote, mcde_sys_error());
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < nnets; i++) {
|
||||
XmString s;
|
||||
|
||||
snprintf(text, sizeof text, "%-32s %3d%%%s", nets[i].ssid,
|
||||
nets[i].signal, nets[i].secured ? " secured" : "");
|
||||
s = XmStringCreateLocalized(text);
|
||||
XmListAddItemUnselected(netList, s, 0);
|
||||
XmStringFree(s);
|
||||
}
|
||||
snprintf(text, sizeof text, nnets == 1 ? "%d network in reach." :
|
||||
"%d networks in reach.", nnets);
|
||||
SetText(wifiNote, text);
|
||||
}
|
||||
|
||||
/* the scan takes seconds (nmcli looks again when its list is older than 30
|
||||
* seconds): after the pane is drawn, not before */
|
||||
static void
|
||||
ScanLater(XtPointer data, XtIntervalId *id)
|
||||
{
|
||||
(void) data;
|
||||
(void) id;
|
||||
if (netList != NULL)
|
||||
Scan();
|
||||
}
|
||||
|
||||
static void
|
||||
WifiGone(Widget w, XtPointer data, XtPointer call)
|
||||
{
|
||||
(void) w;
|
||||
(void) data;
|
||||
(void) call;
|
||||
netList = NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
ScanClicked(Widget w, XtPointer data, XtPointer call)
|
||||
{
|
||||
(void) w;
|
||||
(void) data;
|
||||
(void) call;
|
||||
Scan();
|
||||
}
|
||||
|
||||
static void
|
||||
JoinClicked(Widget w, XtPointer data, XtPointer call)
|
||||
{
|
||||
int *pos = NULL, n = 0;
|
||||
char hex[80] = "", inner[160], cmd[512];
|
||||
|
||||
(void) w;
|
||||
(void) data;
|
||||
(void) call;
|
||||
if (XmListGetSelectedPos(netList, &pos, &n) && n > 0 &&
|
||||
pos[0] >= 1 && pos[0] <= nnets)
|
||||
Hex(nets[pos[0] - 1].ssid, hex, sizeof hex);
|
||||
XtFree((char *) pos);
|
||||
snprintf(inner, sizeof inner, "dtsystemsettings -join-wifi %s%s%s",
|
||||
wifiIf, hex[0] != '\0' ? " " : "", hex);
|
||||
AsRoot("Join a Wi-Fi Network", inner, cmd, sizeof cmd);
|
||||
Spawn(cmd);
|
||||
SetText(wifiNote, "The passwords are asked for in the window that "
|
||||
"opened.");
|
||||
}
|
||||
|
||||
/* the Wi-Fi part of the Network pane; NULL when there is no Wi-Fi card */
|
||||
Widget
|
||||
MakeWifi(Widget parent)
|
||||
{
|
||||
McdeSysInterface ifs[32];
|
||||
int n = mcde_sys_interfaces(ifs, 32), i;
|
||||
Widget box, row;
|
||||
Arg a[4];
|
||||
char text[96];
|
||||
|
||||
wifiIf[0] = '\0';
|
||||
for (i = 0; i < n; i++)
|
||||
if (ifs[i].kind == MCDE_NET_WIFI) {
|
||||
snprintf(wifiIf, sizeof wifiIf, "%s", ifs[i].name);
|
||||
break;
|
||||
}
|
||||
if (wifiIf[0] == '\0')
|
||||
return NULL;
|
||||
box = XtVaCreateManagedWidget("wifi", xmRowColumnWidgetClass, parent,
|
||||
XmNspacing, 6, NULL);
|
||||
snprintf(text, sizeof text, "Wi-Fi networks in reach of %s:", wifiIf);
|
||||
(void) Text(box, "label", text);
|
||||
XtSetArg(a[0], XmNvisibleItemCount, 7);
|
||||
XtSetArg(a[1], XmNselectionPolicy, XmBROWSE_SELECT);
|
||||
XtSetArg(a[2], XmNwidth, 460);
|
||||
netList = XmCreateScrolledList(box, "networks", a, 3);
|
||||
XtManageChild(netList);
|
||||
row = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass, box,
|
||||
XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0, NULL);
|
||||
(void) Button(row, "Scan", ScanClicked, NULL);
|
||||
(void) Button(row, "Join...", JoinClicked, NULL);
|
||||
wifiNote = Text(box, "note", "Looking for networks ...");
|
||||
XtAddCallback(box, XmNdestroyCallback, WifiGone, NULL);
|
||||
XtAppAddTimeOut(app, 300, ScanLater, NULL);
|
||||
return box;
|
||||
}
|
||||
|
||||
/* ---- dtsystemsettings -join-wifi IF [SSID-IN-HEX], in the terminal ------- */
|
||||
|
||||
static void
|
||||
ReadSecret(const char *prompt, char *buf, size_t len)
|
||||
{
|
||||
struct termios t, quiet;
|
||||
int tty = tcgetattr(STDIN_FILENO, &t) == 0;
|
||||
|
||||
printf("%s", prompt);
|
||||
fflush(stdout);
|
||||
if (tty) {
|
||||
quiet = t;
|
||||
quiet.c_lflag &= (tcflag_t) ~ECHO;
|
||||
(void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &quiet);
|
||||
}
|
||||
if (fgets(buf, (int) len, stdin) == NULL)
|
||||
buf[0] = '\0';
|
||||
buf[strcspn(buf, "\r\n")] = '\0';
|
||||
if (tty)
|
||||
(void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static int
|
||||
Finish(int r)
|
||||
{
|
||||
printf("\nPress Return to close this window.\n");
|
||||
fflush(stdout);
|
||||
(void) getchar();
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
JoinWifiCommand(const char *ifname, const char *hexssid)
|
||||
{
|
||||
static McdeSysWifi found[MAX_NETS];
|
||||
char ssid[64], line[64], pass[128];
|
||||
int secured = 1, n, i, r;
|
||||
|
||||
if (hexssid != NULL) {
|
||||
if (!Unhex(hexssid, ssid, sizeof ssid)) {
|
||||
printf("That is no network name.\n");
|
||||
return Finish(1);
|
||||
}
|
||||
n = mcde_sys_wifi_scan(ifname, found, MAX_NETS);
|
||||
for (i = 0; i < n; i++)
|
||||
if (strcmp(found[i].ssid, ssid) == 0)
|
||||
secured = found[i].secured;
|
||||
} else {
|
||||
printf("Looking for Wi-Fi networks on %s ...\n", ifname);
|
||||
fflush(stdout);
|
||||
n = mcde_sys_wifi_scan(ifname, found, MAX_NETS);
|
||||
if (n <= 0) {
|
||||
printf("No network found%s%s\n", n < 0 ? ": " : ".",
|
||||
n < 0 ? mcde_sys_error() : "");
|
||||
return Finish(1);
|
||||
}
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%3d %-32s %3d%%%s\n", i + 1, found[i].ssid,
|
||||
found[i].signal,
|
||||
found[i].secured ? " secured" : "");
|
||||
printf("\nNumber of the network to join: ");
|
||||
fflush(stdout);
|
||||
if (fgets(line, sizeof line, stdin) == NULL ||
|
||||
(i = atoi(line)) < 1 || i > n) {
|
||||
printf("Nothing joined.\n");
|
||||
return Finish(1);
|
||||
}
|
||||
snprintf(ssid, sizeof ssid, "%s", found[i - 1].ssid);
|
||||
secured = found[i - 1].secured;
|
||||
}
|
||||
pass[0] = '\0';
|
||||
if (secured) {
|
||||
printf("Network \"%s\"\n", ssid);
|
||||
ReadSecret("Its password (not shown): ", pass, sizeof pass);
|
||||
}
|
||||
printf("Joining \"%s\" ...\n", ssid);
|
||||
fflush(stdout);
|
||||
r = mcde_sys_wifi_join(ifname, ssid, pass[0] != '\0' ? pass : NULL);
|
||||
memset(pass, 0, sizeof pass);
|
||||
if (r == MCDE_SYS_OK)
|
||||
printf("Joined \"%s\"; the computer joins it at every start.\n",
|
||||
ssid);
|
||||
else
|
||||
printf("Not joined: %s\n", mcde_sys_error());
|
||||
return Finish(r == MCDE_SYS_OK ? 0 : 1);
|
||||
}
|
||||
Reference in new issue
Block a user