Allow a blank psk to just select the network.
authorRoy Marples <roy@marples.name>
Mon, 1 Dec 2014 20:56:48 +0000 (20:56 +0000)
committerRoy Marples <roy@marples.name>
Mon, 1 Dec 2014 20:56:48 +0000 (20:56 +0000)
src/dhcpcd-gtk/wpa.c
src/dhcpcd-qt/dhcpcd-wi.cpp
src/libdhcpcd/dhcpcd.h
src/libdhcpcd/wpa.c

index 7548824d34077b32627fe7c2505a6be4e12e20fa..e2f70972e3b8a163bf564bd327604cd9f8b15332 100644 (file)
@@ -65,11 +65,11 @@ wpa_abort(void)
 }
 
 static bool
-wpa_conf(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *scan, const char *psk)
+wpa_conf(int werr)
 {
        const char *errt;
 
-       switch (dhcpcd_wpa_configure_psk(wpa, scan, psk)) {
+       switch (werr) {
        case DHCPCD_WPA_SUCCESS:
                return true;
        case DHCPCD_WPA_ERR_DISCONN:
@@ -118,7 +118,7 @@ wpa_configure(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *scan)
        s.next = NULL;
 
        if (!(s.flags & WSF_PSK))
-               return wpa_conf(wpa, &s, NULL);
+               return wpa_conf(dhcpcd_wpa_configure(wpa, &s, NULL));
 
        if (wpa_dialog)
                gtk_widget_destroy(wpa_dialog);
@@ -152,7 +152,10 @@ wpa_configure(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *scan)
        retval = false;
        if (result == GTK_RESPONSE_ACCEPT) {
                var = gtk_entry_get_text(GTK_ENTRY(psk));
-               retval = wpa_conf(wpa, &s, var);
+               if (*var == '\0')
+                       retval = wpa_conf(dhcpcd_wpa_select(wpa, &s));
+               else
+                       retval = wpa_conf(dhcpcd_wpa_configure(wpa, &s, var));
        }
        if (wpa_dialog) {
                gtk_widget_destroy(wpa_dialog);
index d8bab853144502b53a709d28347a8f1b621eb271..680091575832422389530df291cbebb9a922e179 100644 (file)
@@ -226,7 +226,7 @@ void DhcpcdWi::dispatch()
 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
 {
        DHCPCD_WI_SCAN s;
-       const char *psk;
+       int err;
 
        /* Take a copy of scan incase it's destroyed by a scan update */
        memcpy(&s, scan, sizeof(s));
@@ -238,13 +238,16 @@ void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
                    tr("Pre Shared key"), QLineEdit::Normal, NULL, &ok);
                if (!ok)
                        return;
-               psk = pwd.toAscii();
+               if (pwd.isNull() || pwd.isEmpty())
+                       err = dhcpcd_wpa_select(wpa, &s);
+               else
+                       err = dhcpcd_wpa_configure(wpa, &s, pwd.toAscii());
        } else
-               psk = NULL;
+               err = dhcpcd_wpa_configure(wpa, &s, NULL);
 
        QString errt;
 
-       switch (dhcpcd_wpa_configure_psk(wpa, &s, psk)) {
+       switch (err) {
        case DHCPCD_WPA_SUCCESS:
                return;
        case DHCPCD_WPA_ERR_DISCONN:
index 4de494d531d0b602f25660de1c1c7ef9728bf168..5b90036b5eafc74dece8b9a59669c9bd63bd4619 100644 (file)
@@ -265,7 +265,8 @@ bool dhcpcd_wpa_network_set(DHCPCD_WPA *, int, const char *, const char *);
 #define DHCPCD_WPA_ERR_ASSOC   -7
 #define DHCPCD_WPA_ERR_DISCONN -8
 #define DHCPCD_WPA_ERR_RECONF  -9
-int dhcpcd_wpa_configure_psk(DHCPCD_WPA *w, DHCPCD_WI_SCAN *s, const char *p);
+int dhcpcd_wpa_configure(DHCPCD_WPA *w, DHCPCD_WI_SCAN *s, const char *p);
+int dhcpcd_wpa_select(DHCPCD_WPA *w, DHCPCD_WI_SCAN *s);
 
 char ** dhcpcd_config_blocks(DHCPCD_CONNECTION *, const char *);
 DHCPCD_OPTION *dhcpcd_config_read(DHCPCD_CONNECTION *,
index fa1a38460ad18d433bb85f45439f582b48c52753..358b37333d653a69ba9fbef6012bedc57dbbf98c 100644 (file)
@@ -1023,8 +1023,30 @@ dhcpcd_wpa_start(DHCPCD_CONNECTION *con)
                dhcpcd_wpa_if_event(i);
 }
 
+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_configure_psk1(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
+dhcpcd_wpa_configure1(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
 {
        const char *mgmt, *var;
        int id, retval;
@@ -1044,24 +1066,12 @@ dhcpcd_wpa_configure_psk1(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
        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
@@ -1094,15 +1104,39 @@ dhcpcd_wpa_configure_psk1(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
 }
 
 int
-dhcpcd_wpa_configure_psk(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
+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);
 
-       retval = dhcpcd_wpa_configure_psk1(wpa, s, psk);
-       /* Always reassociate regardless of error */
+       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;