Allow a blank psk to just select the network.
[dhcpcd-ui] / src / libdhcpcd / wpa.c
index 2146d4593a23362e5c3c105f177e01acef92162f..358b37333d653a69ba9fbef6012bedc57dbbf98c 100644 (file)
@@ -697,7 +697,7 @@ dhcpcd_wpa_network_find(DHCPCD_WPA *wpa, const char *fssid)
        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');
@@ -1023,8 +1023,30 @@ dhcpcd_wpa_start(DHCPCD_CONNECTION *con)
                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;
        int id, retval;
@@ -1032,36 +1054,24 @@ dhcpcd_wpa_configure_psk(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
        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;
+               return DHCPCD_WPA_ERR_RECONF;
 
        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;
-       }
-
-       if (!dhcpcd_wpa_network_set(wpa, id, "key_mgmt", mgmt))
-               return DHCPCD_WPA_ERR_SET;
+       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 (var) {
                if (psk)
                        psk_len = strlen(psk);
                else
@@ -1090,7 +1100,46 @@ dhcpcd_wpa_configure_psk(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
         * This should not be saved. */
        if (!dhcpcd_wpa_network_select(wpa, id))
                return DHCPCD_WPA_ERR_SELECT;
-       if (!dhcpcd_wpa_reassociate(wpa))
-               return DHCPCD_WPA_ERR_ASSOC;
+       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;
 }