scan flags -> wpa_flags.
authorRoy Marples <roy@marples.name>
Mon, 1 Dec 2014 13:15:25 +0000 (13:15 +0000)
committerRoy Marples <roy@marples.name>
Mon, 1 Dec 2014 13:15:25 +0000 (13:15 +0000)
scan flags is now an int which is masked from wpa_flags.
Use these new flags in dhcpcd-gtk/dhcpcd-qt instead of parsing strings
in each.
Don't prompt for a passphrase unless the scan flags say one is expected.

src/dhcpcd-gtk/menu.c
src/dhcpcd-gtk/wpa.c
src/dhcpcd-qt/dhcpcd-ssidmenuwidget.cpp
src/dhcpcd-qt/dhcpcd-wi.cpp
src/libdhcpcd/dhcpcd.h
src/libdhcpcd/wpa.c

index 33835222b4b741f1b373f285fe0eed4ce183fe63..6c644a79878cc32696c7708904527950acf9f725 100644 (file)
@@ -128,10 +128,10 @@ update_item(WI_SCAN *wi, WI_MENU *m, DHCPCD_WI_SCAN *scan)
            GTK_IMAGE_MENU_ITEM(m->menu), sel);
 
        gtk_label_set_text(GTK_LABEL(m->ssid), scan->ssid);
-       if (scan->flags[0] == '\0')
-               icon = "network-wireless";
-       else
+       if (scan->flags & WSF_SECURE)
                icon = "network-wireless-encrypted";
+       else
+               icon = "network-wireless";
        m->icon = gtk_image_new_from_icon_name(icon,
            GTK_ICON_SIZE_MENU);
 
@@ -140,10 +140,11 @@ update_item(WI_SCAN *wi, WI_MENU *m, DHCPCD_WI_SCAN *scan)
                GTK_ICON_SIZE_MENU);
 
 #if 0
-       if (scan->flags[0] == '\0')
+       if (scan->wpa_flags[0] == '\0')
                gtk_widget_set_tooltip_text(m->menu, scan->bssid);
        else {
-               char *tip = g_strconcat(scan->bssid, " ", scan->flags, NULL);
+               char *tip = g_strconcat(scan->bssid, " ", scan->wpa_flags,
+                   NULL);
                gtk_widget_set_tooltip_text(m->menu, tip);
                g_free(tip);
        }
@@ -178,10 +179,10 @@ create_menu(WI_SCAN *wis, DHCPCD_WI_SCAN *scan)
        gtk_misc_set_alignment(GTK_MISC(wim->ssid), 0.0, 0.5);
        gtk_box_pack_start(GTK_BOX(box), wim->ssid, TRUE, TRUE, 0);
 
-       if (scan->flags[0] == '\0')
-               icon = "network-wireless";
-       else
+       if (scan->flags & WSF_SECURE)
                icon = "network-wireless-encrypted";
+       else
+               icon = "network-wireless";
        wim->icon = gtk_image_new_from_icon_name(icon,
            GTK_ICON_SIZE_MENU);
        gtk_box_pack_start(GTK_BOX(box), wim->icon, FALSE, FALSE, 0);
@@ -192,10 +193,10 @@ create_menu(WI_SCAN *wis, DHCPCD_WI_SCAN *scan)
        gtk_box_pack_start(GTK_BOX(box), wim->strength, FALSE, FALSE, 0);
 
 #if 0
-       if (scan->flags[0] == '\0')
+       if (scan->wpa_flags[0] == '\0')
                gtk_widget_set_tooltip_text(wim->menu, scan->bssid);
        else {
-               tip = g_strconcat(scan->bssid, " ", scan->flags, NULL);
+               tip = g_strconcat(scan->bssid, " ", scan->wpa_flags, NULL);
                gtk_widget_set_tooltip_text(wim->menu, tip);
                g_free(tip);
        }
index 9d49d7564661194ff8bf65b65e7d819e3098dee5..8565fc32b17e8a02521d15e0a63ddb16c103509e 100644 (file)
@@ -64,12 +64,43 @@ wpa_abort(void)
        }
 }
 
+static bool
+wpa_conf(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *scan, const char *psk)
+{
+       const char *errt;
+
+       switch (dhcpcd_wpa_configure_psk(wpa, scan, psk)) {
+       case DHCPCD_WPA_SUCCESS:
+               return true;
+       case DHCPCD_WPA_ERR_SET:
+               errt = _("Failed to set key management.");
+               break;
+       case DHCPCD_WPA_ERR_SET_PSK:
+               errt = _("Failed to set password, probably too short.");
+               break;
+       case DHCPCD_WPA_ERR_ENABLE:
+               errt = _("Failed to enable the network.");
+               break;
+       case DHCPCD_WPA_ERR_ASSOC:
+               errt = _("Failed to start association.");
+               break;
+       case DHCPCD_WPA_ERR_WRITE:
+               errt =_("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
+               break;
+       default:
+               errt = strerror(errno);
+               break;
+       }
+       wpa_show_err(_("Error enabling network"), errt);
+       return false;
+}
+
 bool
 wpa_configure(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *scan)
 {
        DHCPCD_WI_SCAN s;
        GtkWidget *label, *psk, *vbox, *hbox;
-       const char *var, *errt;
+       const char *var;
        int result;
        bool retval;
 
@@ -77,6 +108,9 @@ wpa_configure(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *scan)
        memcpy(&s, scan, sizeof(s));
        s.next = NULL;
 
+       if (!(s.flags & WSF_PSK))
+               return wpa_conf(wpa, &s, NULL);
+
        if (wpa_dialog)
                gtk_widget_destroy(wpa_dialog);
 
@@ -109,31 +143,7 @@ wpa_configure(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *scan)
        retval = false;
        if (result == GTK_RESPONSE_ACCEPT) {
                var = gtk_entry_get_text(GTK_ENTRY(psk));
-               switch (dhcpcd_wpa_configure_psk(wpa, &s, var)) {
-               case DHCPCD_WPA_SUCCESS:
-                       retval = true;
-                       break;
-               case DHCPCD_WPA_ERR_SET:
-                       errt = _("Failed to set key management.");
-                       break;
-               case DHCPCD_WPA_ERR_SET_PSK:
-                       errt = _("Failed to set password, probably too short.");
-                       break;
-               case DHCPCD_WPA_ERR_ENABLE:
-                       errt = _("Failed to enable the network.");
-                       break;
-               case DHCPCD_WPA_ERR_ASSOC:
-                       errt = _("Failed to start association.");
-                       break;
-               case DHCPCD_WPA_ERR_WRITE:
-                       errt =_("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
-                       break;
-               default:
-                       errt = strerror(errno);
-                       break;
-               }
-               if (!retval)
-                       wpa_show_err(_("Error enabling network"), errt);
+               retval = wpa_conf(wpa, &s, var);
        }
        if (wpa_dialog) {
                gtk_widget_destroy(wpa_dialog);
index 90934547adfdd62fb4153276d3d22d9d74a7a76e..df07fdf468bd6af79d944c9001e78c1b1b17729c 100644 (file)
@@ -98,11 +98,11 @@ void DhcpcdSsidMenuWidget::setScan(DHCPCD_WI_SCAN *scan)
        } else
                selicon->setPixmap(NULL);
        ssid->setText(scan->ssid);
-       if (scan->flags[0] == '\0')
-               icon = DhcpcdQt::getIcon("status", "dialog-warning");
-       else
+       if (scan->flags & WSF_SECURE)
                icon = DhcpcdQt::getIcon("status",
                    "network-wireless-encrypted");
+       else
+               icon = DhcpcdQt::getIcon("status", "dialog-warning");
        picon = icon.pixmap(16, 16);
        encicon->setPixmap(picon);
 
index f2c3fd3050650a7d115faefa5fb36d18affe809a..3154595e74bafaed459a41c8255000cbba0e748e 100644 (file)
@@ -225,22 +225,26 @@ void DhcpcdWi::dispatch()
 
 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
 {
-       bool ok;
        DHCPCD_WI_SCAN s;
+       const char *psk;
 
        /* Take a copy of scan incase it's destroyed by a scan update */
        memcpy(&s, scan, sizeof(s));
        s.next = NULL;
 
-       QString psk = QInputDialog::getText(dhcpcdQt, s.ssid,
-           tr("Pre Shared key"), QLineEdit::Normal, NULL, &ok);
-
-       if (!ok)
-               return;
+       if (s.flags & WSF_PSK) {
+               bool ok;
+               QString pwd = QInputDialog::getText(dhcpcdQt, s.ssid,
+                   tr("Pre Shared key"), QLineEdit::Normal, NULL, &ok);
+               if (!ok)
+                       return;
+               psk = pwd.toAscii();
+       } else
+               psk = NULL;
 
        QString errt;
 
-       switch (dhcpcd_wpa_configure_psk(wpa, &s, psk.toAscii())) {
+       switch (dhcpcd_wpa_configure_psk(wpa, &s, psk)) {
        case DHCPCD_WPA_SUCCESS:
                return;
        case DHCPCD_WPA_ERR_SET:
index 0608a1d7fc06762db433b23df3647a38351ac7fa..335776c87bfed124e71f1856fcf83525b2a4a527 100644 (file)
@@ -65,13 +65,18 @@ typedef struct dhcpcd_wi_avs {
 typedef struct dhcpcd_wi_scan {
        struct dhcpcd_wi_scan *next;
        char bssid[IF_BSSIDSIZE];
+       int flags;
+#define WSF_SECURE             0x01
+#define WSF_PSK                        0x02
+#define WSF_WEP                        0x10
+#define WSF_WPA                        0x20
        int frequency;
        DHCPCD_WI_AV quality;
        DHCPCD_WI_AV noise;
        DHCPCD_WI_AV level;
        DHCPCD_WI_AV strength;
        char ssid[IF_SSIDSIZE];
-       char flags[FLAGSIZE];
+       char wpa_flags[FLAGSIZE];
 } DHCPCD_WI_SCAN;
 
 #ifdef IN_LIBDHCPCD
index 408f710cafb10109cb76217ecc1a92e71d616342..4cd917d1e7fac283c6f5e3d26250ddb9666e271b 100644 (file)
@@ -277,6 +277,7 @@ dhcpcd_wpa_scans_read(DHCPCD_WPA *wpa)
        DHCPCD_WI_SCAN *wis, *w, *l;
        char *s, *p, buf[32];
        char wssid[sizeof(w->ssid)];
+       const char *proto;
 
        if (!dhcpcd_realloc(wpa->con, 2048))
                return NULL;
@@ -310,7 +311,8 @@ dhcpcd_wpa_scans_read(DHCPCD_WPA *wpa)
                        else if (strncmp(s, "level=", 6) == 0)
                                dhcpcd_strtoi(&w->level.value, s + 6);
                        else if (strncmp(s, "flags=", 6) == 0)
-                               strlcpy(w->flags, s + 6, sizeof(w->flags));
+                               strlcpy(w->wpa_flags, s + 6,
+                                   sizeof(w->wpa_flags));
                        else if (strncmp(s, "ssid=", 5) == 0) {
                                /* Decode it from \xNN to \NNN
                                 * so we're consistent */
@@ -335,6 +337,25 @@ dhcpcd_wpa_scans_read(DHCPCD_WPA *wpa)
                        l->next = w;
                l = w;
 
+               if ((proto = strstr(w->wpa_flags, "[WPA-")) ||
+                   (proto = strstr(w->wpa_flags, "[WPA2-")) ||
+                   (proto = strstr(w->wpa_flags, "[RSN-")))
+               {
+                       const char *endp, *psk;
+
+                       w->flags = WSF_WPA | WSF_SECURE;
+                       endp = strchr(proto, ']');
+                       if ((psk = strstr(proto, "-PSK]")) ||
+                           (psk = strstr(proto, "-PSK-")) ||
+                           (psk = strstr(proto, "-PSK+")))
+                       {
+                               if (psk < endp)
+                                       w->flags |= WSF_PSK;
+                       }
+               }
+               if (strstr(w->wpa_flags, "[WEP]"))
+                       w->flags = WSF_WEP | WSF_PSK | WSF_SECURE;
+
                w->strength.value = w->level.value;
                if (w->strength.value > 110 && w->strength.value < 256)
                        /* Convert WEXT level to dBm */
@@ -1004,33 +1025,41 @@ dhcpcd_wpa_configure_psk(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
        if (id == -1)
                return DHCPCD_WPA_ERR;
 
-       if (strcmp(s->flags, "[WEP]") == 0) {
+       if (s->flags & WSF_WEP) {
                mgmt = "NONE";
                var = "wep_key0";
-       } else {
+       } else if (s->flags & WSF_WPA) {
                mgmt = "WPA-PSK";
-               var = "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;
 
-       if (psk)
-               psk_len = strlen(psk);
-       else
-               psk_len = 0;
-       npsk = malloc(psk_len + 3);
-       if (npsk == NULL)
-               return DHCPCD_WPA_ERR;
-       npsk[0] = '"';
-       if (psk_len)
-               memcpy(npsk + 1, psk, psk_len);
-       npsk[psk_len + 1] = '"';
-       npsk[psk_len + 2] = '\0';
-       r = dhcpcd_wpa_network_set(wpa, id, var, npsk);
-       free(npsk);
-       if (!r)
-               return DHCPCD_WPA_ERR_SET_PSK;
+       if (var) {
+               if (psk)
+                       psk_len = strlen(psk);
+               else
+                       psk_len = 0;
+               npsk = malloc(psk_len + 3);
+               if (npsk == NULL)
+                       return DHCPCD_WPA_ERR;
+               npsk[0] = '"';
+               if (psk_len)
+                       memcpy(npsk + 1, psk, psk_len);
+               npsk[psk_len + 1] = '"';
+               npsk[psk_len + 2] = '\0';
+               r = dhcpcd_wpa_network_set(wpa, id, var, npsk);
+               free(npsk);
+               if (!r)
+                       return DHCPCD_WPA_ERR_SET_PSK;
+       }
 
        if (!dhcpcd_wpa_network_enable(wpa, id))
                return DHCPCD_WPA_ERR_ENABLE;