Allow a blank psk to just select the network.
[dhcpcd-ui] / src / libdhcpcd / wpa.c
index 4cd917d1e7fac283c6f5e3d26250ddb9666e271b..358b37333d653a69ba9fbef6012bedc57dbbf98c 100644 (file)
@@ -576,6 +576,13 @@ dhcpcd_wi_scans(DHCPCD_IF *i)
 }
 
 bool
 }
 
 bool
+dhcpcd_wpa_reconfigure(DHCPCD_WPA *wpa)
+{
+
+       return dhcpcd_wpa_command(wpa, "RECONFIGURE");
+}
+
+bool
 dhcpcd_wpa_reassociate(DHCPCD_WPA *wpa)
 {
 
 dhcpcd_wpa_reassociate(DHCPCD_WPA *wpa)
 {
 
@@ -623,6 +630,13 @@ dhcpcd_wpa_network_enable(DHCPCD_WPA *wpa, int id)
 }
 
 bool
 }
 
 bool
+dhcpcd_wpa_network_select(DHCPCD_WPA *wpa, int id)
+{
+
+       return dhcpcd_wpa_network(wpa, "SELECT_NETWORK", id);
+}
+
+bool
 dhcpcd_wpa_network_remove(DHCPCD_WPA *wpa, int id)
 {
 
 dhcpcd_wpa_network_remove(DHCPCD_WPA *wpa, int id)
 {
 
@@ -683,7 +697,7 @@ dhcpcd_wpa_network_find(DHCPCD_WPA *wpa, const char *fssid)
        s = strchr(wpa->con->buf, '\n');
        if (s == NULL)
                return -1;
        s = strchr(wpa->con->buf, '\n');
        if (s == NULL)
                return -1;
-       while ((t = strsep(&s, "\b"))) {
+       while ((t = strsep(&s, "\b\n"))) {
                if (*t == '\0')
                        continue;
                ssid = strchr(t, '\t');
                if (*t == '\0')
                        continue;
                ssid = strchr(t, '\t');
@@ -1009,40 +1023,55 @@ dhcpcd_wpa_start(DHCPCD_CONNECTION *con)
                dhcpcd_wpa_if_event(i);
 }
 
                dhcpcd_wpa_if_event(i);
 }
 
-int
-dhcpcd_wpa_configure_psk(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
+static const char *
+dhcpcd_wpa_var_psk(DHCPCD_WI_SCAN *s)
+{
+
+       if (s->flags & WSF_WEP)
+               return "wep_key0";
+       else if ((s->flags & (WSF_WPA | WSF_PSK)) == (WSF_WPA | WSF_PSK))
+               return "psk";
+       return NULL;
+}
+
+static const char *
+dhcpcd_wpa_var_mgmt(DHCPCD_WI_SCAN *s)
+{
+
+       if (s->flags & WSF_WPA) {
+               if (s->flags & WSF_PSK)
+                       return "WPA-PSK";
+       }
+       return "NONE";
+}
+
+static int
+dhcpcd_wpa_configure1(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
 {
        const char *mgmt, *var;
 {
        const char *mgmt, *var;
-       int id;
+       int id, retval;
        char *npsk;
        size_t psk_len;
        bool r;
 
        char *npsk;
        size_t psk_len;
        bool r;
 
-       assert(wpa);
-       assert(s);
+       if (!dhcpcd_wpa_disconnect(wpa))
+               return DHCPCD_WPA_ERR_DISCONN;
+
+       /* reload the configuration so that when we don't save
+        * the disabled networks to the config file. */
+       if (!dhcpcd_wpa_reconfigure(wpa))
+               return DHCPCD_WPA_ERR_RECONF;
 
        id = dhcpcd_wpa_network_find_new(wpa, s->ssid);
        if (id == -1)
                return DHCPCD_WPA_ERR;
 
 
        id = dhcpcd_wpa_network_find_new(wpa, s->ssid);
        if (id == -1)
                return DHCPCD_WPA_ERR;
 
-       if (s->flags & WSF_WEP) {
-               mgmt = "NONE";
-               var = "wep_key0";
-       } else if (s->flags & WSF_WPA) {
-               mgmt = "WPA-PSK";
-               if (s->flags & WSF_PSK)
-                       var = "psk";
-               else
-                       var = NULL;
-       } else {
-               mgmt = "NONE";
-               var = NULL;
-       }
+       mgmt = dhcpcd_wpa_var_mgmt(s);
+       var = dhcpcd_wpa_var_psk(s);
+       if (mgmt && var) {
+               if (!dhcpcd_wpa_network_set(wpa, id, "key_mgmt", mgmt))
+                       return DHCPCD_WPA_ERR_SET;
 
 
-       if (!dhcpcd_wpa_network_set(wpa, id, "key_mgmt", mgmt))
-               return DHCPCD_WPA_ERR_SET;
-
-       if (var) {
                if (psk)
                        psk_len = strlen(psk);
                else
                if (psk)
                        psk_len = strlen(psk);
                else
@@ -1063,9 +1092,54 @@ dhcpcd_wpa_configure_psk(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
 
        if (!dhcpcd_wpa_network_enable(wpa, id))
                return DHCPCD_WPA_ERR_ENABLE;
 
        if (!dhcpcd_wpa_network_enable(wpa, id))
                return DHCPCD_WPA_ERR_ENABLE;
-       if (!dhcpcd_wpa_reassociate(wpa))
-               return DHCPCD_WPA_ERR_ASSOC;
-       if (!dhcpcd_wpa_config_write(wpa))
-               return DHCPCD_WPA_ERR_WRITE;
-       return DHCPCD_WPA_SUCCESS;
+       if (dhcpcd_wpa_config_write(wpa))
+               retval = DHCPCD_WPA_SUCCESS;
+       else
+               retval = DHCPCD_WPA_ERR_WRITE;
+       /* Selecting a network disbales the others.
+        * This should not be saved. */
+       if (!dhcpcd_wpa_network_select(wpa, id))
+               return DHCPCD_WPA_ERR_SELECT;
+       return retval;
+}
+
+int
+dhcpcd_wpa_configure(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
+{
+       int retval;
+
+       retval = dhcpcd_wpa_configure1(wpa, s, psk);
+       /* Always reassociate */
+       if (!dhcpcd_wpa_reassociate(wpa)) {
+               if (retval == DHCPCD_WPA_SUCCESS)
+                       retval = DHCPCD_WPA_ERR_ASSOC;
+       }
+       return retval;
+}
+
+int
+dhcpcd_wpa_select(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s)
+{
+       int id, retval;
+
+       assert(wpa);
+       assert(s);
+
+       id = dhcpcd_wpa_network_find(wpa, s->ssid);
+       if (id == -1)
+               return DHCPCD_WPA_ERR;
+
+       if (!dhcpcd_wpa_disconnect(wpa))
+               retval = DHCPCD_WPA_ERR_DISCONN;
+       else if (!dhcpcd_wpa_network_select(wpa, id))
+               retval = DHCPCD_WPA_ERR_SELECT;
+       else
+               retval = DHCPCD_WPA_SUCCESS;
+
+       /* Always reassociate */
+       if (!dhcpcd_wpa_reassociate(wpa)) {
+               if (retval == DHCPCD_WPA_SUCCESS)
+                       retval = DHCPCD_WPA_ERR_ASSOC;
+       }
+       return retval;
 }
 }