diff --git a/lib/McdeSys/Makefile.am b/lib/McdeSys/Makefile.am index 13ccdaa1..504a7be7 100644 --- a/lib/McdeSys/Makefile.am +++ b/lib/McdeSys/Makefile.am @@ -21,7 +21,7 @@ endif libMcdeSys_la_LDFLAGS = -version-info 1:0:0 # what the system tells about itself, for a look by hand: make check runs it -check_PROGRAMS = test_sysinfo test_netmount test_updatesource test_processes test_job +check_PROGRAMS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput test_sysinfo_SOURCES = test_sysinfo.c test_sysinfo_CPPFLAGS = -I$(top_srcdir)/include test_sysinfo_LDADD = libMcdeSys.la @@ -37,4 +37,7 @@ test_processes_LDADD = libMcdeSys.la test_job_SOURCES = test_job.c test_job_CPPFLAGS = -I$(top_srcdir)/include test_job_LDADD = libMcdeSys.la -TESTS = test_sysinfo test_netmount test_updatesource test_processes test_job +test_netinput_SOURCES = test_netinput.c +test_netinput_CPPFLAGS = -I$(top_srcdir)/include +test_netinput_LDADD = libMcdeSys.la +TESTS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput diff --git a/lib/McdeSys/net.c b/lib/McdeSys/net.c index cfa4dc89..02190dff 100644 --- a/lib/McdeSys/net.c +++ b/lib/McdeSys/net.c @@ -119,6 +119,36 @@ mcde_sys_net_ifname_ok(const char *name) return 1; } +/* The checks of mcde_sys_wifi_join where the network is written into + * wpa_supplicant.conf (netbsd.c): the interface name is lower-case letters and + * digits (it goes into rc.conf's wpa_supplicant_flags); the network name has 1 + * to 32 bytes, the passphrase, if there is one, 8 to 63; neither has a quote, + * a backslash or a newline, which would end the quoted string of the file. + * Kept here, apart from the NetBSD branch, so that make check can run them as + * an ordinary user (the join itself needs the administrator; tasksd t0081). + * 1 if all is well. An empty interface name passed the inline check this + * replaces. */ +int +mcde_sys_wifi_config_ok(const char *ifname, const char *ssid, + const char *passphrase) +{ + size_t n; + + if (ifname == NULL || ifname[0] == '\0' || + strspn(ifname, "abcdefghijklmnopqrstuvwxyz0123456789") != + strlen(ifname)) + return 0; + if (ssid == NULL || ssid[0] == '\0' || strlen(ssid) > 32 || + strpbrk(ssid, "\"\n\\") != NULL) + return 0; + if (passphrase != NULL) { + n = strlen(passphrase); + if (n < 8 || n > 63 || strpbrk(passphrase, "\"\n\\") != NULL) + return 0; + } + return 1; +} + /* the prefix length of a dotted netmask, -1 when it is none */ int mcde_sys_net_prefix(const char *netmask) diff --git a/lib/McdeSys/netbsd.c b/lib/McdeSys/netbsd.c index cb205d7d..895b4a04 100644 --- a/lib/McdeSys/netbsd.c +++ b/lib/McdeSys/netbsd.c @@ -543,12 +543,7 @@ mcde_sys_wifi_join(const char *ifname, const char *ssid, 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))) + if (!mcde_sys_wifi_config_ok(ifname, ssid, passphrase)) 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)."); diff --git a/lib/McdeSys/private.h b/lib/McdeSys/private.h index edcbb458..3afd5ef5 100644 --- a/lib/McdeSys/private.h +++ b/lib/McdeSys/private.h @@ -55,6 +55,10 @@ McdeSysNetKind mcde_sys_if_kind(const char *name, char *ssid, size_t len); /* net.c: an interface name that may go on a command line; the prefix * length of a dotted netmask (-1: none) and the netmask of a prefix */ int mcde_sys_net_ifname_ok(const char *name); +/* net.c: may this network go into wpa_supplicant.conf (the checks of the + * NetBSD mcde_sys_wifi_join); 1 if so */ +int mcde_sys_wifi_config_ok(const char *ifname, const char *ssid, + const char *passphrase); int mcde_sys_net_prefix(const char *netmask); void mcde_sys_net_mask(int prefix, char *buf, size_t len); /* the backend: the configured settings of IFNAME, its router, Ethernet ID diff --git a/lib/McdeSys/test_netinput.c b/lib/McdeSys/test_netinput.c new file mode 100644 index 00000000..14f7d81c --- /dev/null +++ b/lib/McdeSys/test_netinput.c @@ -0,0 +1,99 @@ +/* + * test_netinput.c -- McdeSys: what may reach a network command or a file (make check) + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * The checks that stand between a typed network name or password and + * wpa_supplicant.conf, rc.conf and the command lines of the network tools + * (review 2026-11-13, tasksd t0081): the Wi-Fi network (a name of 33 bytes, a + * quote, a backslash or a newline would end the quoted string of the file or + * add a line to it), the interface name, the netmask. + */ + +#include +#include + +#include + +#include "private.h" /* the library's checks, not the API */ + +static int failures; + +#define CHECK(cond) do { \ + if (!(cond)) { \ + printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ + failures++; \ + } \ +} while (0) + +int +main(void) +{ + char s32[33], s33[34], p63[64], p64[65], p7[8], p8[9]; + + memset(s32, 'n', 32); s32[32] = '\0'; + memset(s33, 'n', 33); s33[33] = '\0'; + memset(p63, 'p', 63); p63[63] = '\0'; + memset(p64, 'p', 64); p64[64] = '\0'; + memset(p7, 'p', 7); p7[7] = '\0'; + memset(p8, 'p', 8); p8[8] = '\0'; + + /* the network: name 1..32 bytes, passphrase none or 8..63 */ + CHECK(mcde_sys_wifi_config_ok("iwn0", "Home", "hunter22")); + CHECK(mcde_sys_wifi_config_ok("wlan0", "Home Network", NULL)); /* open */ + CHECK(mcde_sys_wifi_config_ok("iwn0", "caf\xc3\xa9", "pass word")); + CHECK(mcde_sys_wifi_config_ok("iwn0", s32, p63)); /* the longest */ + CHECK(mcde_sys_wifi_config_ok("iwn0", "a", p8)); /* the shortest */ + CHECK(!mcde_sys_wifi_config_ok("iwn0", s33, p8)); /* name 33 bytes */ + CHECK(!mcde_sys_wifi_config_ok("iwn0", "", p8)); /* no name */ + CHECK(!mcde_sys_wifi_config_ok("iwn0", NULL, p8)); + CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", p7)); /* 7 */ + CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", p64)); /* 64 */ + CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", "")); + /* what would end the quoted string or add a line to the file */ + CHECK(!mcde_sys_wifi_config_ok("iwn0", "a\"b", p8)); + CHECK(!mcde_sys_wifi_config_ok("iwn0", "a\\b", p8)); + CHECK(!mcde_sys_wifi_config_ok("iwn0", "a\nb", p8)); + CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home\"\n}\nnetwork={ssid=\"x", p8)); + CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", "pass\"word1")); + CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", "pass\\word1")); + CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", "pass\nword1")); + CHECK(!mcde_sys_wifi_config_ok("iwn0", "Home", + "12345678\"\n\tkey_mgmt=NONE")); + /* the interface name goes into rc.conf: lower case and digits only */ + CHECK(mcde_sys_wifi_config_ok("urtwn0", "Home", p8)); + CHECK(!mcde_sys_wifi_config_ok(NULL, "Home", p8)); + CHECK(!mcde_sys_wifi_config_ok("", "Home", p8)); /* was accepted */ + CHECK(!mcde_sys_wifi_config_ok("iwn0 -x", "Home", p8)); + CHECK(!mcde_sys_wifi_config_ok("iwn0;id", "Home", p8)); + CHECK(!mcde_sys_wifi_config_ok("-iwn0", "Home", p8)); + CHECK(!mcde_sys_wifi_config_ok("IWN0", "Home", p8)); + CHECK(!mcde_sys_wifi_config_ok("iwn0\"", "Home", p8)); + + /* an interface name for a command line (the settings pane) */ + CHECK(mcde_sys_net_ifname_ok("iwn0")); + CHECK(mcde_sys_net_ifname_ok("eth0.100")); + CHECK(mcde_sys_net_ifname_ok("br-lan")); + CHECK(!mcde_sys_net_ifname_ok(NULL)); + CHECK(!mcde_sys_net_ifname_ok("")); + CHECK(!mcde_sys_net_ifname_ok("-i")); + CHECK(!mcde_sys_net_ifname_ok("eth0 x")); + CHECK(!mcde_sys_net_ifname_ok("eth0;id")); + CHECK(!mcde_sys_net_ifname_ok("a\nb")); + CHECK(!mcde_sys_net_ifname_ok("0123456789abcdef")); /* IFNAMSIZ */ + + /* a netmask: contiguous ones only */ + CHECK(mcde_sys_net_prefix("255.255.255.0") == 24); + CHECK(mcde_sys_net_prefix("255.255.255.255") == 32); + CHECK(mcde_sys_net_prefix("255.255.254.0") == 23); + CHECK(mcde_sys_net_prefix("0.0.0.0") == 0); + CHECK(mcde_sys_net_prefix("255.0.255.0") == -1); + CHECK(mcde_sys_net_prefix("255.255.255.1") == -1); + CHECK(mcde_sys_net_prefix("24") == -1); + CHECK(mcde_sys_net_prefix("255.255.255.0; id") == -1); + CHECK(mcde_sys_net_prefix("") == -1); + + if (failures == 0) + printf("test_netinput: all passed\n"); + return failures != 0; +}