diff --git a/lib/McdeSys/linux.c b/lib/McdeSys/linux.c index 74d913c1..d719d178 100644 --- a/lib/McdeSys/linux.c +++ b/lib/McdeSys/linux.c @@ -450,8 +450,7 @@ mcde_sys_wifi_join(const char *ifname, const char *ssid, 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) + if (ifname == NULL || !mcde_sys_wifi_ssid_arg_ok(ssid)) return mcde_sys_fail(MCDE_SYS_EINVAL, "The network name cannot " "be used."); argv[n++] = (char *) nm; diff --git a/lib/McdeSys/net.c b/lib/McdeSys/net.c index 02190dff..77692989 100644 --- a/lib/McdeSys/net.c +++ b/lib/McdeSys/net.c @@ -149,6 +149,21 @@ mcde_sys_wifi_config_ok(const char *ifname, const char *ssid, return 1; } +/* May this network name be a command-line argument of nmcli (the Linux + * mcde_sys_wifi_join)? 1 to 32 bytes, and not starting with "-": a name that + * does is an option to nmcli, and the name of a neighbour's access point is + * whatever it broadcasts. Measured with nmcli 1.56.1: "nmcli device wifi + * connect --ask password PW ifname IF" gives "invalid extra argument" -- the + * name was taken as the option --ask; and "--" in front is no remedy: nmcli + * takes the "--" for the name ("invalid extra argument 'Home'"). A passphrase + * is the argument of "password", whatever it starts with. */ +int +mcde_sys_wifi_ssid_arg_ok(const char *ssid) +{ + return ssid != NULL && ssid[0] != '\0' && ssid[0] != '-' && + strlen(ssid) <= 32; +} + /* 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/private.h b/lib/McdeSys/private.h index a27f1576..d77656f0 100644 --- a/lib/McdeSys/private.h +++ b/lib/McdeSys/private.h @@ -59,6 +59,8 @@ int mcde_sys_net_ifname_ok(const char *name); * NetBSD mcde_sys_wifi_join); 1 if so */ int mcde_sys_wifi_config_ok(const char *ifname, const char *ssid, const char *passphrase); +/* net.c: may this network name be an argument of nmcli (Linux wifi_join) */ +int mcde_sys_wifi_ssid_arg_ok(const char *ssid); 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 index 14f7d81c..553dcaed 100644 --- a/lib/McdeSys/test_netinput.c +++ b/lib/McdeSys/test_netinput.c @@ -70,6 +70,19 @@ main(void) CHECK(!mcde_sys_wifi_config_ok("IWN0", "Home", p8)); CHECK(!mcde_sys_wifi_config_ok("iwn0\"", "Home", p8)); + /* the name as an argument of nmcli (Linux): not an option */ + CHECK(mcde_sys_wifi_ssid_arg_ok("Home")); + CHECK(mcde_sys_wifi_ssid_arg_ok("Home Network")); + CHECK(mcde_sys_wifi_ssid_arg_ok("a-b")); /* a dash inside is fine */ + CHECK(mcde_sys_wifi_ssid_arg_ok("quote\"and\\ok")); /* nmcli is no file */ + CHECK(mcde_sys_wifi_ssid_arg_ok(s32)); + CHECK(!mcde_sys_wifi_ssid_arg_ok(s33)); + CHECK(!mcde_sys_wifi_ssid_arg_ok("")); + CHECK(!mcde_sys_wifi_ssid_arg_ok(NULL)); + CHECK(!mcde_sys_wifi_ssid_arg_ok("--ask")); /* measured: an option */ + CHECK(!mcde_sys_wifi_ssid_arg_ok("-x")); + CHECK(!mcde_sys_wifi_ssid_arg_ok("-")); + /* 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"));