From dffeaf9b17fa0ea8ed3c74cb3ab1deb6540f0010 Mon Sep 17 00:00:00 2001 From: lotherk Date: Wed, 11 Nov 2026 08:37:38 +0000 Subject: [PATCH] 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 --- AGENTS.md | 8 + lib/McdeSys/linux.c | 108 +++++++++- lib/McdeSys/netbsd.c | 230 +++++++++++++++++++++- lib/McdeSys/test_sysinfo.c | 14 ++ programs/dtsystemsettings/Makefile.am | 2 +- programs/dtsystemsettings/main.c | 9 +- programs/dtsystemsettings/ui.h | 4 + programs/dtsystemsettings/wifi.c | 272 ++++++++++++++++++++++++++ 8 files changed, 643 insertions(+), 4 deletions(-) create mode 100644 programs/dtsystemsettings/wifi.c diff --git a/AGENTS.md b/AGENTS.md index 5aa68e4e..5a918f38 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -134,6 +134,14 @@ place of CDE; pkgsrc builds it on any platform from `pkgsrc/`. (sys.resources) the same way, for programs started afterwards; the families come from Xft. dtsession saves RESOURCE_MANAGER at logout (`SmSave.c`, OutputResource), so both persist. + Network lists the Wi-Fi networks in reach (`wifi.c`, + mcde_sys_wifi_scan: NetBSD wpa_cli scan_results, else ifconfig -v IF + list scan as root; Linux nmcli -t). "Join..." opens a terminal: su, then + `dtsystemsettings -join-wifi IF [SSID as hex]`, which asks the network's + password without echo and calls mcde_sys_wifi_join (NetBSD: a network + block in /etc/wpa_supplicant.conf, wpa_supplicant and dhcpcd in rc.conf, + as sysinst does -- docs/reference/wifi.txt; Linux: nmcli device wifi + connect). Never test joining on the development host. Date & Time chooses the time zone (`timezone.c`): regions and places from mcde_sys_timezones (zone1970.tab); "Set Time Zone" runs mcde_sys_timezone_set as root at once, otherwise diff --git a/lib/McdeSys/linux.c b/lib/McdeSys/linux.c index 5f7516c0..eb0336bf 100644 --- a/lib/McdeSys/linux.c +++ b/lib/McdeSys/linux.c @@ -8,7 +8,7 @@ * /proc/self/mounts for the startup disk; power through systemctl(1) where * the system has it, else shutdown(8); /sys/class/net and iw(8) for Wi-Fi, * amixer(1) when no sound server runs, bluetoothctl(1) for Bluetooth, - * /sys/class/power_supply for the battery. + * /sys/class/power_supply for the battery, nmcli(1) for Wi-Fi. */ #define _POSIX_C_SOURCE 200809L @@ -318,6 +318,112 @@ mcde_sys_battery(McdeSysBattery *bat) return MCDE_SYS_OK; } +/* ---- Wi-Fi: NetworkManager's nmcli(1). "device wifi list" in terse form + * (-t: fields split by ':', a ':' inside escaped as "\:"), "device wifi + * connect SSID password P ifname IF" (a system-wide profile, DHCP, at every + * boot). The password is on nmcli's command line for the seconds it runs: + * nmcli has no other way for a new network (its passwd-file is for + * "connection up" of a profile that exists); only root runs this. */ +static const char *const nmcliPaths[] = { "/usr/bin/nmcli", NULL }; + +/* the next field of a terse nmcli line, unescaped; *P moves past it */ +static char * +TerseField(char **p) +{ + char *start = *p, *r = *p, *w = *p; + + if (start == NULL) + return NULL; + while (*r != '\0' && *r != ':') { + if (*r == '\\' && r[1] != '\0') + r++; + *w++ = *r++; + } + *p = *r == ':' ? r + 1 : NULL; + *w = '\0'; + return start; +} + +int +mcde_sys_wifi_scan(const char *ifname, McdeSysWifi *nets, size_t max) +{ + static char out[16384]; + const char *nm = mcde_sys_which(nmcliPaths); + char *argv[10], *line, *last = NULL; + int n = 0, i; + + if (nm == NULL) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "NetworkManager (nmcli) " + "is not there."); + argv[0] = (char *) nm; + argv[1] = "-t"; + argv[2] = "-f"; + argv[3] = "SSID,SIGNAL,SECURITY"; + argv[4] = "device"; + argv[5] = "wifi"; + argv[6] = "list"; + argv[7] = "ifname"; + argv[8] = (char *) ifname; + argv[9] = NULL; + if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK) + return -1; + for (line = strtok_r(out, "\n", &last); line != NULL && + (size_t) n < max; line = strtok_r(NULL, "\n", &last)) { + char *p = line, *ssid = TerseField(&p), *sig = TerseField(&p); + char *sec = TerseField(&p); + + if (ssid == NULL || ssid[0] == '\0' || sig == NULL) + continue; + for (i = 0; i < n; i++) + if (strcmp(nets[i].ssid, ssid) == 0) + break; + if (i < n) { + if (atoi(sig) > nets[i].signal) + nets[i].signal = atoi(sig); + continue; + } + snprintf(nets[n].ssid, sizeof nets[n].ssid, "%s", ssid); + nets[n].signal = atoi(sig); + nets[n].secured = sec != NULL && sec[0] != '\0' && + strcmp(sec, "--") != 0; + n++; + } + return n; +} + +int +mcde_sys_wifi_join(const char *ifname, const char *ssid, + const char *passphrase) +{ + const char *nm = mcde_sys_which(nmcliPaths); + char *argv[12]; + int n = 0; + + if (geteuid() != 0) + return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator " + "can join a Wi-Fi network."); + if (nm == NULL) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "NetworkManager (nmcli) " + "is not there."); + if (ifname == NULL || ssid == NULL || ssid[0] == '\0' || + strlen(ssid) > 32) + return mcde_sys_fail(MCDE_SYS_EINVAL, "The network name cannot " + "be used."); + argv[n++] = (char *) nm; + argv[n++] = "device"; + argv[n++] = "wifi"; + argv[n++] = "connect"; + argv[n++] = (char *) ssid; + if (passphrase != NULL) { + argv[n++] = "password"; + argv[n++] = (char *) passphrase; + } + argv[n++] = "ifname"; + argv[n++] = (char *) ifname; + argv[n] = NULL; + return mcde_sys_run(argv); +} + /* ---- users: the group sudo administers (Debian; wheel where there is no * sudo group); chpasswd(8) takes the password on its standard input, * gpasswd(1) adds to and removes from a group */ diff --git a/lib/McdeSys/netbsd.c b/lib/McdeSys/netbsd.c index d4697804..fe140ead 100644 --- a/lib/McdeSys/netbsd.c +++ b/lib/McdeSys/netbsd.c @@ -8,7 +8,8 @@ * (based on NetBSD ...)") for the system, statvfs(2)'s f_mntfromname for the * startup disk, shutdown(8) and zzz(8) for power, the SIOCG80211NWID ioctl * for Wi-Fi, mixerctl(1) when no sound server runs, btconfig(8) for - * Bluetooth, the envsys(4) dictionary for the battery. + * Bluetooth, the envsys(4) dictionary for the battery, wpa_supplicant(8) as + * sysinst sets it up for Wi-Fi. */ #define _NETBSD_SOURCE @@ -312,6 +313,233 @@ mcde_sys_battery(McdeSysBattery *bat) return MCDE_SYS_OK; } +/* ---- Wi-Fi: as the installer does it (src/usr.sbin/sysinst/net.c: + * a network block in /etc/wpa_supplicant.conf, wpa_supplicant and dhcpcd in + * rc.conf); the networks in reach from wpa_cli(8) when wpa_supplicant runs, + * else from ifconfig(8) "list scan" (root). docs/reference/wifi.txt. */ +#define WPA_CONF "/etc/wpa_supplicant.conf" +#define RC_CONF "/etc/rc.conf" +static char wpacli[] = "/usr/sbin/wpa_cli"; +static char ifconfigp[] = "/sbin/ifconfig"; + +/* signal in dBm (wpa_cli) or the driver's RSSI (ifconfig) as 0..100 */ +static int +SignalPercent(int v) +{ + if (v < 0) + v = 2 * (v + 100); + return v < 0 ? 0 : v > 100 ? 100 : v; +} + +static int +AddNet(McdeSysWifi *nets, int n, size_t max, const char *ssid, int signal, + int secured) +{ + int i; + + if (ssid[0] == '\0') + return n; + for (i = 0; i < n; i++) + if (strcmp(nets[i].ssid, ssid) == 0) { + if (signal > nets[i].signal) + nets[i].signal = signal; + return n; + } + if ((size_t) n >= max) + return n; + snprintf(nets[n].ssid, sizeof nets[n].ssid, "%s", ssid); + nets[n].signal = signal; + nets[n].secured = secured; + return n + 1; +} + +int +mcde_sys_wifi_scan(const char *ifname, McdeSysWifi *nets, size_t max) +{ + static char out[16384]; + char *argv[6], *line, *last = NULL; + int n = 0; + + /* wpa_cli: "bssid / frequency / signal level / flags / ssid", tabs */ + argv[0] = wpacli; + argv[1] = "-i"; + argv[2] = (char *) ifname; + argv[3] = "scan_results"; + argv[4] = NULL; + if (mcde_sys_capture(argv, out, sizeof out) == MCDE_SYS_OK && + strstr(out, "bssid") != NULL) { + for (line = strtok_r(out, "\n", &last); line != NULL; + line = strtok_r(NULL, "\n", &last)) { + char *f[5], *l2 = NULL; + int k; + + f[0] = strtok_r(line, "\t", &l2); + for (k = 1; k < 5; k++) + f[k] = strtok_r(NULL, "\t", &l2); + if (f[4] == NULL || strchr(f[0], ':') == NULL) + continue; + n = AddNet(nets, n, max, f[4], + SignalPercent(atoi(f[2])), + strstr(f[3], "WPA") != NULL || + strstr(f[3], "WEP") != NULL); + } + return n; + } + /* ifconfig -v IF list scan: "SSID BSSID CHAN RATE S:N INT CAPS", + * the SSID padded; the interface must be up (root) */ + if (geteuid() != 0) + return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator " + "can look for Wi-Fi networks while none is joined."); + argv[0] = ifconfigp; + argv[1] = (char *) ifname; + argv[2] = "up"; + argv[3] = NULL; + (void) mcde_sys_run(argv); + argv[1] = "-v"; + argv[2] = (char *) ifname; + argv[3] = "list"; + argv[4] = "scan"; + argv[5] = NULL; + if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK) + return -1; + for (line = strtok_r(out, "\n", &last); line != NULL; + line = strtok_r(NULL, "\n", &last)) { + char *p, *bssid = NULL, *sn, *caps, ssid[64]; + int rssi = 0; + + /* the BSSID: 17 characters, colons at 2, 5, 8, 11, 14 */ + for (p = line; *p != '\0' && strlen(p) >= 17; p++) + if (p[2] == ':' && p[5] == ':' && p[8] == ':' && + p[11] == ':' && p[14] == ':' && + (p == line || p[-1] == ' ')) { + bssid = p; + break; + } + if (bssid == NULL || bssid == line) + continue; + snprintf(ssid, sizeof ssid, "%.*s", (int) (bssid - line), line); + while (ssid[0] != '\0' && ssid[strlen(ssid) - 1] == ' ') + ssid[strlen(ssid) - 1] = '\0'; + if ((sn = strchr(bssid + 17, ':')) != NULL) { + char *q = sn; + + while (q > bssid + 17 && q[-1] != ' ') + q--; + rssi = atoi(q); + } + /* the capabilities are the last word: P is privacy */ + caps = strrchr(line, ' '); + n = AddNet(nets, n, max, ssid, SignalPercent(rssi), + caps != NULL && strchr(caps, 'P') != NULL); + } + return n; +} + +/* VAR=VALUE in rc.conf: the line of VAR replaced, or added at the end */ +static int +RcConfSet(const char *var, const char *value) +{ + char line[1024], tmp[] = "/etc/rc.conf.mcdeXXXXXX"; + size_t vlen = strlen(var); + struct stat st; + FILE *from, *to; + int fd, done = 0; + + if (stat(RC_CONF, &st) != 0 || (from = fopen(RC_CONF, "r")) == NULL) + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read %s.", RC_CONF); + if ((fd = mkstemp(tmp)) < 0 || (to = fdopen(fd, "w")) == NULL) { + fclose(from); + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write beside %s.", + RC_CONF); + } + while (fgets(line, sizeof line, from) != NULL) { + if (strncmp(line, var, vlen) == 0 && line[vlen] == '=') { + if (!done) + fprintf(to, "%s=%s\n", var, value); + done = 1; + continue; + } + fputs(line, to); + } + fclose(from); + if (!done) + fprintf(to, "%s=%s\n", var, value); + if (fchmod(fd, st.st_mode & 07777) != 0 || fclose(to) != 0 || + rename(tmp, RC_CONF) != 0) { + unlink(tmp); + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.", RC_CONF); + } + return MCDE_SYS_OK; +} + +int +mcde_sys_wifi_join(const char *ifname, const char *ssid, + const char *passphrase) +{ + char flags[256]; + struct stat st; + FILE *fp; + int fresh; + char *argv[4]; + + if (geteuid() != 0) + return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator " + "can join a Wi-Fi network."); + if (ifname == NULL || strspn(ifname, "abcdefghijklmnopqrstuvwxyz" + "0123456789") != strlen(ifname) || ssid == NULL || + ssid[0] == '\0' || strlen(ssid) > 32 || + strpbrk(ssid, "\"\n\\") != NULL || + (passphrase != NULL && (strlen(passphrase) < 8 || + strlen(passphrase) > 63 || strpbrk(passphrase, "\"\n\\") != NULL))) + return mcde_sys_fail(MCDE_SYS_EINVAL, "The network name or the " + "password cannot be used (a password has 8 to 63 " + "characters, no quotes)."); + fresh = stat(WPA_CONF, &st) != 0; + if ((fp = fopen(WPA_CONF, "a")) == NULL) + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.", + WPA_CONF); + (void) fchmod(fileno(fp), 0600); + /* the example's head (share/examples/wpa_supplicant): wpa_cli for + * the wheel group, which lets System Settings see the networks */ + if (fresh) + fprintf(fp, "ctrl_interface=/var/run/wpa_supplicant/\n" + "ctrl_interface_group=wheel\n"); + fprintf(fp, "\n# Added by MCDE System Settings\nnetwork={\n" + "\tssid=\"%s\"\n", ssid); + if (passphrase != NULL) + fprintf(fp, "\tpsk=\"%s\"\n", passphrase); + else + fprintf(fp, "\tkey_mgmt=NONE\n"); + fprintf(fp, "\tscan_ssid=1\n}\n"); + if (fclose(fp) != 0) + return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.", + WPA_CONF); + /* sysinst's lines (net.c:1203-1209) */ + snprintf(flags, sizeof flags, + "\"-B -s -i %s -D bsd -c " WPA_CONF "\"", ifname); + if (RcConfSet("wpa_supplicant", "YES") != MCDE_SYS_OK || + RcConfSet("wpa_supplicant_flags", flags) != MCDE_SYS_OK || + RcConfSet("dhcpcd", "YES") != MCDE_SYS_OK) + return MCDE_SYS_EFAIL; + /* now: wpa_supplicant (re)started with the new network, dhcpcd for + * the address */ + argv[0] = "/etc/rc.d/wpa_supplicant"; + argv[1] = "restart"; + argv[2] = NULL; + if (mcde_sys_run(argv) != MCDE_SYS_OK) { + argv[1] = "start"; + if (mcde_sys_run(argv) != MCDE_SYS_OK) + return MCDE_SYS_EFAIL; + } + argv[0] = "/etc/rc.d/dhcpcd"; + argv[1] = "restart"; + if (mcde_sys_run(argv) != MCDE_SYS_OK) { + argv[1] = "start"; + (void) mcde_sys_run(argv); + } + return MCDE_SYS_OK; +} + /* ---- users: wheel administers (su(1) takes only wheel); the password is * hashed here, as passwd(1) does, with pw_gensalt(3) and crypt(3), and only * the hash goes to usermod(8) -- the password itself is on no command line */ diff --git a/lib/McdeSys/test_sysinfo.c b/lib/McdeSys/test_sysinfo.c index ac3c0e0e..33b16c98 100644 --- a/lib/McdeSys/test_sysinfo.c +++ b/lib/McdeSys/test_sysinfo.c @@ -69,6 +69,20 @@ main(void) printf("bluetooth %s%s%s\n", bt.present ? bt.name : "none", bt.present ? " " : "", bt.present ? (bt.on ? "on" : "off") : ""); + /* the Wi-Fi networks in reach, read only */ + for (k = 0; k < n; k++) + if (ifs[k].kind == MCDE_NET_WIFI) { + McdeSysWifi nets[64]; + int nn = mcde_sys_wifi_scan(ifs[k].name, nets, 64); + + printf("wifi %s: %d networks%s%s\n", + ifs[k].name, nn, nn < 0 ? ", " : "", + nn < 0 ? mcde_sys_error() : ""); + if (nn > 0) + printf(" first: %s, %d%%%s\n", + nets[0].ssid, nets[0].signal, + nets[0].secured ? ", secured" : ""); + } { static char zones[600][MCDE_SYS_ZONE_LEN]; char zone[MCDE_SYS_ZONE_LEN]; diff --git a/programs/dtsystemsettings/Makefile.am b/programs/dtsystemsettings/Makefile.am index 9b8e7505..bdddd021 100644 --- a/programs/dtsystemsettings/Makefile.am +++ b/programs/dtsystemsettings/Makefile.am @@ -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)\" \ diff --git a/programs/dtsystemsettings/main.c b/programs/dtsystemsettings/main.c index 85e863d4..05de73d2 100644 --- a/programs/dtsystemsettings/main.c +++ b/programs/dtsystemsettings/main.c @@ -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; } } diff --git a/programs/dtsystemsettings/ui.h b/programs/dtsystemsettings/ui.h index e0ebacac..b7e3aca9 100644 --- a/programs/dtsystemsettings/ui.h +++ b/programs/dtsystemsettings/ui.h @@ -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" diff --git a/programs/dtsystemsettings/wifi.c b/programs/dtsystemsettings/wifi.c new file mode 100644 index 00000000..26670724 --- /dev/null +++ b/programs/dtsystemsettings/wifi.c @@ -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 +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#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); +}