/* * wifi.c -- dtsystemsettings: the Wi-Fi networks in reach, and joining one * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * The Wi-Fi tab of a card in the Network pane: 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 tab of a card in the Network pane (IFNAME, or the first Wi-Fi * card when NULL); NULL when there is no Wi-Fi card */ Widget MakeWifi(Widget parent, const char *ifname) { McdeSysInterface ifs[32]; int n = ifname != NULL ? 0 : mcde_sys_interfaces(ifs, 32), i; Widget box, row; Arg a[4]; char text[96]; wifiIf[0] = '\0'; if (ifname != NULL) snprintf(wifiIf, sizeof wifiIf, "%s", ifname); 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); }