static void
constructed (GObject *object)
{
    NMWifiItemPrivate *priv = GET_PRIVATE (object);
    const GByteArray *ssid;
    char *str;
    guint32 flags;
    guint32 wpa_flags;
    guint32 rsn_flags;

    if (G_OBJECT_CLASS (nm_wifi_item_parent_class)->constructed)
        G_OBJECT_CLASS (nm_wifi_item_parent_class)->constructed (object);

    ssid = nm_access_point_get_ssid (priv->current_ap);
    str = nm_utils_ssid_to_utf8 ((char *) ssid->data, ssid->len);
    g_object_set (object, NM_LIST_ITEM_NAME, str, NULL);
    g_free (str);

    flags = nm_access_point_get_flags (priv->current_ap);
    wpa_flags = nm_access_point_get_wpa_flags (priv->current_ap);
    rsn_flags = nm_access_point_get_rsn_flags (priv->current_ap);

    if (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK ||
            wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK ||
            rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X ||
            wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
        str = "WPA encrypted";
    else if (flags & NM_802_11_AP_FLAGS_PRIVACY)
        str = "WEP encrypted";
    else
        str = NULL;

    if (str)
        g_object_set (object, NM_LIST_ITEM_SECURITY, str, NULL);
}
static guint
get_access_point_security (NMAccessPoint *ap)
{
  NM80211ApFlags flags;
  NM80211ApSecurityFlags wpa_flags;
  NM80211ApSecurityFlags rsn_flags;
  guint type;

  flags = nm_access_point_get_flags (ap);
  wpa_flags = nm_access_point_get_wpa_flags (ap);
  rsn_flags = nm_access_point_get_rsn_flags (ap);

  if (!(flags & NM_802_11_AP_FLAGS_PRIVACY) &&
      wpa_flags == NM_802_11_AP_SEC_NONE &&
      rsn_flags == NM_802_11_AP_SEC_NONE)
    type = NM_AP_SEC_NONE;
  else if ((flags & NM_802_11_AP_FLAGS_PRIVACY) &&
           wpa_flags == NM_802_11_AP_SEC_NONE &&
           rsn_flags == NM_802_11_AP_SEC_NONE)
    type = NM_AP_SEC_WEP;
  else if (!(flags & NM_802_11_AP_FLAGS_PRIVACY) &&
           wpa_flags != NM_802_11_AP_SEC_NONE &&
           rsn_flags != NM_802_11_AP_SEC_NONE)
    type = NM_AP_SEC_WPA;
  else
    type = NM_AP_SEC_WPA2;

  return type;
}
WifiAccessPoint *createNMWifiAccessPoint(NMAccessPoint *ap) {
  const GByteArray *ssid = nm_access_point_get_ssid(ap);
  //GBytes *ssid = nm_access_point_get_ssid(ap);
  char *ssid_str = NULL, *ssid_hex_str = NULL;
  bool security = resolveAPSecurity(ap);

  /* Convert to strings */
  if (ssid) {
    const guint8 *ssid_data = ssid->data;
    gsize ssid_len = ssid->len;

    //ssid_data = (const guint8 *) g_bytes_get_data(ssid, &ssid_len);
    ssid_str = nm_utils_ssid_to_utf8(ssid);
  }

  if (!ssid_str || !ssid)
    DBG("libnm conversion of ssid to utf8 failed, using empty string");

  return new WifiAccessPoint {
    !ssid_str ? "" : ssid_str,
    nm_access_point_get_strength(ap),
    security,
    utils_hash_ap(nm_access_point_get_ssid(ap),
                  nm_access_point_get_mode(ap),
                  nm_access_point_get_flags(ap),
                  nm_access_point_get_wpa_flags(ap),
                  nm_access_point_get_rsn_flags(ap)),
  };
}
bool resolveAPSecurity(NMAccessPoint *ap) {
  //FIXME: Assumes all security types equal
  return (
    nm_access_point_get_flags(ap) == NM_802_11_AP_FLAGS_PRIVACY ||
    nm_access_point_get_wpa_flags(ap) != NM_802_11_AP_SEC_NONE ||
    nm_access_point_get_rsn_flags(ap) != NM_802_11_AP_SEC_NONE
  );
}
Beispiel #5
0
static void
detail_access_point (gpointer data, gpointer user_data)
{
	NMAccessPoint *ap = NM_ACCESS_POINT (data);
	const char *active_bssid = (const char *) user_data;
	GString *str;
	gboolean active = FALSE;
	guint32 flags, wpa_flags, rsn_flags;
	const GByteArray * ssid;
	char *tmp;

	flags = nm_access_point_get_flags (ap);
	wpa_flags = nm_access_point_get_wpa_flags (ap);
	rsn_flags = nm_access_point_get_rsn_flags (ap);

	if (active_bssid) {
		const char *current_bssid = nm_access_point_get_hw_address (ap);
		if (current_bssid && !strcmp (current_bssid, active_bssid))
			active = TRUE;
	}

	str = g_string_new (NULL);
	g_string_append_printf (str,
	                        "%s, %s, Freq %d MHz, Rate %d Mb/s, Strength %d",
	                        (nm_access_point_get_mode (ap) == NM_802_11_MODE_INFRA) ? "Infra" : "Ad-Hoc",
	                        nm_access_point_get_hw_address (ap),
	                        nm_access_point_get_frequency (ap),
	                        nm_access_point_get_max_bitrate (ap) / 1000,
	                        nm_access_point_get_strength (ap));

	if (   !(flags & NM_802_11_AP_FLAGS_PRIVACY)
	    &&  (wpa_flags != NM_802_11_AP_SEC_NONE)
	    &&  (rsn_flags != NM_802_11_AP_SEC_NONE))
		g_string_append (str, ", Encrypted: ");

	if (   (flags & NM_802_11_AP_FLAGS_PRIVACY)
	    && (wpa_flags == NM_802_11_AP_SEC_NONE)
	    && (rsn_flags == NM_802_11_AP_SEC_NONE))
		g_string_append (str, " WEP");
	if (wpa_flags != NM_802_11_AP_SEC_NONE)
		g_string_append (str, " WPA");
	if (rsn_flags != NM_802_11_AP_SEC_NONE)
		g_string_append (str, " WPA2");
	if (   (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
	    || (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X))
		g_string_append (str, " Enterprise");

	/* FIXME: broadcast/hidden */

	ssid = nm_access_point_get_ssid (ap);
	tmp = g_strdup_printf ("  %s%s", active ? "*" : "",
	                       ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(none)");

	print_string (tmp, str->str);

	g_string_free (str, TRUE);
	g_free (tmp);
}
static void
get_property (GObject *object,
              guint prop_id,
              GValue *value,
              GParamSpec *pspec)
{
	NMAccessPoint *ap = NM_ACCESS_POINT (object);

	_nm_object_ensure_inited (NM_OBJECT (object));

	switch (prop_id) {
	case PROP_FLAGS:
		g_value_set_uint (value, nm_access_point_get_flags (ap));
		break;
	case PROP_WPA_FLAGS:
		g_value_set_uint (value, nm_access_point_get_wpa_flags (ap));
		break;
	case PROP_RSN_FLAGS:
		g_value_set_uint (value, nm_access_point_get_rsn_flags (ap));
		break;
	case PROP_SSID:
		g_value_set_boxed (value, nm_access_point_get_ssid (ap));
		break;
	case PROP_FREQUENCY:
		g_value_set_uint (value, nm_access_point_get_frequency (ap));
		break;
	case PROP_HW_ADDRESS:
		g_value_set_string (value, nm_access_point_get_bssid (ap));
		break;
	case PROP_BSSID:
		g_value_set_string (value, nm_access_point_get_bssid (ap));
		break;
	case PROP_MODE:
		g_value_set_uint (value, nm_access_point_get_mode (ap));
		break;
	case PROP_MAX_BITRATE:
		g_value_set_uint (value, nm_access_point_get_max_bitrate (ap));
		break;
	case PROP_STRENGTH:
		g_value_set_uchar (value, nm_access_point_get_strength (ap));
		break;
	case PROP_LAST_SEEN:
		g_value_set_int (value, nm_access_point_get_last_seen (ap));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}
/* stolen from nm-applet */
static char *
hash_ap (NMAccessPoint *ap)
{
	unsigned char input[66];
	GBytes *ssid;
	NM80211Mode mode;
	guint32 flags;
	guint32 wpa_flags;
	guint32 rsn_flags;

	memset (&input[0], 0, sizeof (input));

	ssid = nm_access_point_get_ssid (ap);
	if (ssid)
		memcpy (input, g_bytes_get_data (ssid, NULL), g_bytes_get_size (ssid));

	mode = nm_access_point_get_mode (ap);
	if (mode == NM_802_11_MODE_INFRA)
		input[32] |= (1 << 0);
	else if (mode == NM_802_11_MODE_ADHOC)
		input[32] |= (1 << 1);
	else
		input[32] |= (1 << 2);

	/* Separate out no encryption, WEP-only, and WPA-capable */
	flags = nm_access_point_get_flags (ap);
	wpa_flags = nm_access_point_get_wpa_flags (ap);
	rsn_flags = nm_access_point_get_rsn_flags (ap);
	if (  !(flags & NM_802_11_AP_FLAGS_PRIVACY)
	      && (wpa_flags == NM_802_11_AP_SEC_NONE)
	      && (rsn_flags == NM_802_11_AP_SEC_NONE))
		input[32] |= (1 << 3);
	else if (   (flags & NM_802_11_AP_FLAGS_PRIVACY)
	            && (wpa_flags == NM_802_11_AP_SEC_NONE)
	            && (rsn_flags == NM_802_11_AP_SEC_NONE))
		input[32] |= (1 << 4);
	else if (   !(flags & NM_802_11_AP_FLAGS_PRIVACY)
	            &&  (wpa_flags != NM_802_11_AP_SEC_NONE)
	            &&  (rsn_flags != NM_802_11_AP_SEC_NONE))
		input[32] |= (1 << 5);
	else
		input[32] |= (1 << 6);

	/* duplicate it */
	memcpy (&input[33], &input[0], 32);
	return g_compute_checksum_for_data (G_CHECKSUM_MD5, input, sizeof (input));
}
static void
dump_access_point (NMAccessPoint *ap)
{
	const GByteArray * ssid;
	const char * str;

	ssid = nm_access_point_get_ssid (ap);
	g_print ("\tSsid: %s\n",
	         ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(none)");

	str = nm_access_point_get_hw_address (ap);
	g_print ("\tMAC Address: %s\n", str);

	g_print ("\tFlags: 0x%X\n", nm_access_point_get_flags (ap));
	g_print ("\tWPA Flags: 0x%X\n", nm_access_point_get_wpa_flags (ap));
	g_print ("\tRSN Flags: 0x%X\n", nm_access_point_get_rsn_flags (ap));
	g_print ("\tFrequency: %u\n", nm_access_point_get_frequency (ap));

	g_print ("\tMode: %d\n", nm_access_point_get_mode (ap));
	g_print ("\tRate: %d\n", nm_access_point_get_max_bitrate (ap));
	g_print ("\tStrength: %d\n", nm_access_point_get_strength (ap));
}
static gchar *
get_ap_security_string (NMAccessPoint *ap)
{
        NM80211ApSecurityFlags wpa_flags, rsn_flags;
        NM80211ApFlags flags;
        GString *str;

        flags = nm_access_point_get_flags (ap);
        wpa_flags = nm_access_point_get_wpa_flags (ap);
        rsn_flags = nm_access_point_get_rsn_flags (ap);

        str = g_string_new ("");
        if ((flags & NM_802_11_AP_FLAGS_PRIVACY) &&
            (wpa_flags == NM_802_11_AP_SEC_NONE) &&
            (rsn_flags == NM_802_11_AP_SEC_NONE)) {
                /* TRANSLATORS: this WEP WiFi security */
                g_string_append_printf (str, "%s, ", _("WEP"));
        }
        if (wpa_flags != NM_802_11_AP_SEC_NONE) {
                /* TRANSLATORS: this WPA WiFi security */
                g_string_append_printf (str, "%s, ", _("WPA"));
        }
        if (rsn_flags != NM_802_11_AP_SEC_NONE) {
                /* TRANSLATORS: this WPA WiFi security */
                g_string_append_printf (str, "%s, ", _("WPA2"));
        }
        if ((wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X) ||
            (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)) {
                /* TRANSLATORS: this Enterprise WiFi security */
                g_string_append_printf (str, "%s, ", _("Enterprise"));
        }
        if (str->len > 0)
                g_string_set_size (str, str->len - 2);
        else {
                g_string_append (str, C_("Wifi security", "None"));
        }
        return g_string_free (str, FALSE);
}
Beispiel #10
0
void
nm_network_menu_item_set_detail (NMNetworkMenuItem *item,
                                 NMAccessPoint *ap,
                                 GdkPixbuf *adhoc_icon,
                                 guint32 dev_caps)
{
	gboolean is_adhoc = FALSE;
	guint32 ap_flags, ap_wpa, ap_rsn;

	g_return_if_fail (item != NULL);
	g_return_if_fail (NM_IS_NETWORK_MENU_ITEM (item));

	ap_flags = nm_access_point_get_flags (ap);
	ap_wpa = nm_access_point_get_wpa_flags (ap);
	ap_rsn = nm_access_point_get_rsn_flags (ap);

	if ((ap_flags & NM_802_11_AP_FLAGS_PRIVACY) || ap_wpa || ap_rsn)
		item->is_encrypted = TRUE;

	if (nm_access_point_get_mode (ap) == NM_802_11_MODE_ADHOC) {
		item->is_adhoc = is_adhoc = TRUE;
		gtk_image_set_from_pixbuf (GTK_IMAGE (item->detail), adhoc_icon);
	} else
		gtk_image_set_from_stock (GTK_IMAGE (item->detail), NULL, GTK_ICON_SIZE_MENU);

	/* Don't enable the menu item the device can't even connect to the AP */
	if (   !nm_utils_security_valid (NMU_SEC_NONE, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn)
        && !nm_utils_security_valid (NMU_SEC_STATIC_WEP, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn)
	    && !nm_utils_security_valid (NMU_SEC_LEAP, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn)
	    && !nm_utils_security_valid (NMU_SEC_DYNAMIC_WEP, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn)
	    && !nm_utils_security_valid (NMU_SEC_WPA_PSK, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn)
	    && !nm_utils_security_valid (NMU_SEC_WPA2_PSK, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn)
	    && !nm_utils_security_valid (NMU_SEC_WPA_ENTERPRISE, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn)
	    && !nm_utils_security_valid (NMU_SEC_WPA2_ENTERPRISE, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn)) {
		gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
	}
}
static NMSettingWirelessSecurity *
get_security_for_ap (NMAccessPoint *ap,
                     guint32 dev_caps,
                     gboolean *supported,
                     NMSetting8021x **s_8021x)
{
    NMSettingWirelessSecurity *sec;
    NM80211Mode mode;
    guint32 flags;
    guint32 wpa_flags;
    guint32 rsn_flags;

    g_return_val_if_fail (NM_IS_ACCESS_POINT (ap), NULL);
    g_return_val_if_fail (supported != NULL, NULL);
    g_return_val_if_fail (*supported == TRUE, NULL);
    g_return_val_if_fail (s_8021x != NULL, NULL);
    g_return_val_if_fail (*s_8021x == NULL, NULL);

    sec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();

    mode = nm_access_point_get_mode (ap);
    flags = nm_access_point_get_flags (ap);
    wpa_flags = nm_access_point_get_wpa_flags (ap);
    rsn_flags = nm_access_point_get_rsn_flags (ap);

    /* No security */
    if (   !(flags & NM_802_11_AP_FLAGS_PRIVACY)
            && (wpa_flags == NM_802_11_AP_SEC_NONE)
            && (rsn_flags == NM_802_11_AP_SEC_NONE))
        goto none;

    /* Static WEP, Dynamic WEP, or LEAP */
    if (flags & NM_802_11_AP_FLAGS_PRIVACY) {
        if ((dev_caps & NM_WIFI_DEVICE_CAP_RSN) || (dev_caps & NM_WIFI_DEVICE_CAP_WPA)) {
            /* If the device can do WPA/RSN but the AP has no WPA/RSN informatoin
             * elements, it must be LEAP or static/dynamic WEP.
             */
            if ((wpa_flags == NM_802_11_AP_SEC_NONE) && (rsn_flags == NM_802_11_AP_SEC_NONE)) {
                g_object_set (sec,
                              NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "none",
                              NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX, 0,
                              NULL);
                return sec;
            }
            /* Otherwise, the AP supports WPA or RSN, which is preferred */
        } else {
            /* Device can't do WPA/RSN, but can at least pass through the
             * WPA/RSN information elements from a scan.  Since Privacy was
             * advertised, LEAP or static/dynamic WEP must be in use.
             */
            g_object_set (sec,
                          NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "none",
                          NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX, 0,
                          NULL);
            return sec;
        }
    }

    /* Stuff after this point requires infrastructure */
    if (mode != NM_802_11_MODE_INFRA) {
        *supported = FALSE;
        goto none;
    }

    /* WPA2 PSK first */
    if (   (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
            && (dev_caps & NM_WIFI_DEVICE_CAP_RSN)) {
        g_object_set (sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", NULL);
        nm_setting_wireless_security_add_proto (sec, "rsn");
        add_ciphers_from_flags (sec, rsn_flags, TRUE);
        add_ciphers_from_flags (sec, rsn_flags, FALSE);
        return sec;
    }

    /* WPA PSK */
    if (   (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
            && (dev_caps & NM_WIFI_DEVICE_CAP_WPA)) {
        g_object_set (sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", NULL);
        nm_setting_wireless_security_add_proto (sec, "wpa");
        add_ciphers_from_flags (sec, wpa_flags, TRUE);
        add_ciphers_from_flags (sec, wpa_flags, FALSE);
        return sec;
    }

    /* WPA2 Enterprise */
    if (   (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
            && (dev_caps & NM_WIFI_DEVICE_CAP_RSN)) {
        g_object_set (sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-eap", NULL);
        nm_setting_wireless_security_add_proto (sec, "rsn");
        add_ciphers_from_flags (sec, rsn_flags, TRUE);
        add_ciphers_from_flags (sec, rsn_flags, FALSE);

        *s_8021x = NM_SETTING_802_1X (nm_setting_802_1x_new ());
        nm_setting_802_1x_add_eap_method (*s_8021x, "ttls");
        g_object_set (*s_8021x, NM_SETTING_802_1X_PHASE2_AUTH, "mschapv2", NULL);
        return sec;
    }

    /* WPA Enterprise */
    if (   (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
            && (dev_caps & NM_WIFI_DEVICE_CAP_WPA)) {
        g_object_set (sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-eap", NULL);
        nm_setting_wireless_security_add_proto (sec, "wpa");
        add_ciphers_from_flags (sec, wpa_flags, TRUE);
        add_ciphers_from_flags (sec, wpa_flags, FALSE);

        *s_8021x = NM_SETTING_802_1X (nm_setting_802_1x_new ());
        nm_setting_802_1x_add_eap_method (*s_8021x, "ttls");
        g_object_set (*s_8021x, NM_SETTING_802_1X_PHASE2_AUTH, "mschapv2", NULL);
        return sec;
    }

    *supported = FALSE;

none:
    g_object_unref (sec);
    return NULL;
}
void WifiStatusNM::setConnectedAccessPoint(WifiAccessPoint *ap, String psk) {
  ScopedPointer<StringArray> cmd;

  for (const auto& listener : listeners)
    listener->handleWifiBusy();

  // disconnect if no ap provided
  if (ap == nullptr) {
    NMActiveConnection *conn = nm_device_get_active_connection(nmdevice);
    removeNMConnection(nmdevice, conn);

    return;
  }
  // try to connect to ap, dispatch events on success and failure
  else {
    NMConnection *connection = NULL;
    NMSettingWireless *s_wifi = NULL;
    NMSettingWirelessSecurity *s_wsec = NULL;
    const char *nm_ap_path = NULL;
    const GPtrArray *ap_list;
    NMAccessPoint *candidate_ap;

    //FIXME: expand WifiAccessPoint struct to know which NMAccessPoint it is
    ap_list = nm_device_wifi_get_access_points(NM_DEVICE_WIFI(nmdevice));
    if (ap_list == NULL)
      return;

    for (int i = 0; i < ap_list->len; i++) {
      const char *candidate_hash;
      candidate_ap = (NMAccessPoint *) g_ptr_array_index(ap_list, i);
      candidate_hash = utils_hash_ap(nm_access_point_get_ssid(candidate_ap),
                                     nm_access_point_get_mode(candidate_ap),
                                     nm_access_point_get_flags(candidate_ap),
                                     nm_access_point_get_wpa_flags(candidate_ap),
                                     nm_access_point_get_rsn_flags(candidate_ap));

      if (ap->hash == candidate_hash) {
        nm_ap_path = nm_object_get_path(NM_OBJECT(candidate_ap));
        break;
      }
    }

    if (!nm_ap_path)
      return;

    connecting = true;

    connection = nm_connection_new();
    s_wifi = (NMSettingWireless *) nm_setting_wireless_new();
    nm_connection_add_setting(connection, NM_SETTING(s_wifi));
    g_object_set(s_wifi,
                 NM_SETTING_WIRELESS_SSID, nm_access_point_get_ssid(candidate_ap),
                 NM_SETTING_WIRELESS_HIDDEN, false,
                 NULL);

    if (!psk.isEmpty()) {
      s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new();
      nm_connection_add_setting(connection, NM_SETTING(s_wsec));

      if (nm_access_point_get_wpa_flags(candidate_ap) == NM_802_11_AP_SEC_NONE &&
          nm_access_point_get_rsn_flags(candidate_ap) == NM_802_11_AP_SEC_NONE) {
        /* WEP */
        nm_setting_wireless_security_set_wep_key(s_wsec, 0, psk.toRawUTF8());
	if (isValidWEPKeyFormat(psk))
          g_object_set(G_OBJECT(s_wsec), NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE,
                       NM_WEP_KEY_TYPE_KEY, NULL);
	else if (isValidWEPPassphraseFormat(psk))
          g_object_set(G_OBJECT(s_wsec), NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE,
                       NM_WEP_KEY_TYPE_PASSPHRASE, NULL);
	else
	  DBG("User input invalid WEP Key type, psk.length() = " << psk.length()
              << ", not in [5,10,13,26]");
      } else {
        g_object_set(s_wsec, NM_SETTING_WIRELESS_SECURITY_PSK, psk.toRawUTF8(), NULL);
      }
    }

    nm_client_add_and_activate_connection(nmclient,
                                                connection,
                                                nmdevice,
                                                nm_ap_path,
                                                handle_add_and_activate_finish,
                                                this);
  }
}
static void
print_access_point_info (NMAccessPoint * ap, gboolean active,
		guint32 device_capas)
{
	const char * bssid;
	const GByteArray * ssid;
	NM80211Mode mode;
	guint32 frequency;
	guint32 max_bitrate;
	guint32 signal_strength;
	guint32 ap_flags;
	guint32 wpa_flags;
	guint32 rsn_flags;

	char *ssid_str;
	gboolean is_adhoc;
	gchar * sec_opts[5]; /* Currently five security options is defined */
	gint sec_opts_num, i;

	g_return_if_fail (NM_IS_ACCESS_POINT (ap));


	mode = nm_access_point_get_mode (ap);
	ap_flags = nm_access_point_get_flags (ap);
	wpa_flags = nm_access_point_get_wpa_flags (ap);
	rsn_flags = nm_access_point_get_rsn_flags (ap);

	is_adhoc = (mode == NM_802_11_MODE_ADHOC);

	/* Skip access point not compatible with device's capabilities */
	if (   !nm_utils_security_valid (NMU_SEC_NONE, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags)
		&& !nm_utils_security_valid (NMU_SEC_STATIC_WEP, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags)
		&& !nm_utils_security_valid (NMU_SEC_LEAP, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags)
		&& !nm_utils_security_valid (NMU_SEC_DYNAMIC_WEP, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags)
		&& !nm_utils_security_valid (NMU_SEC_WPA_PSK, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags)
		&& !nm_utils_security_valid (NMU_SEC_WPA2_PSK, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags)
		&& !nm_utils_security_valid (NMU_SEC_WPA_ENTERPRISE, device_capas,TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags)
		&& !nm_utils_security_valid (NMU_SEC_WPA2_ENTERPRISE, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags))
		return;

	bssid = nm_access_point_get_hw_address (ap);
	ssid = nm_access_point_get_ssid (ap);
	frequency = nm_access_point_get_frequency (ap);
	max_bitrate = nm_access_point_get_max_bitrate (ap);
	signal_strength = nm_access_point_get_strength (ap);

	ssid_str = nm_utils_ssid_to_utf8 ((const char *) ssid->data, ssid->len);

	sec_opts_num = 0;
	if ((ap_flags & NM_802_11_AP_FLAGS_PRIVACY) && !wpa_flags && !rsn_flags)
		sec_opts[sec_opts_num++] = "wep";
	if (!is_adhoc) {
		if (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
			sec_opts[sec_opts_num++] = "wpa-psk";
		if (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
			sec_opts[sec_opts_num++] = "wpa2-psk";
		if (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
			sec_opts[sec_opts_num++] = "wpa-eap";
		if (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
			sec_opts[sec_opts_num++] = "wpa2-eap";
	}

	g_print ("%-9s BSSID:%s  Frequency:%dMHz", "", bssid, frequency);
	if (active)
		g_print ("  <--  ACTIVE");
	g_print ("\n");
	g_print ("%-15s SSID:%s  Mode:%s\n", "", ssid_str,
			wifi_mode_to_string(mode));
	g_print ("%-15s Signal:%d  MaxBitrate:%.1fMb/s  Security:", "",
			signal_strength, max_bitrate/1000.0);

	if (sec_opts_num == 0) {
		g_print ("none");
	}
	else {
		for (i = 0; i < sec_opts_num; i++) {
			g_print ("%s", sec_opts[i]);
			if (i != sec_opts_num - 1)
				g_print (" ");
		}
	}
	g_print ("\n");
}
static gboolean
security_combo_init (NMAWirelessDialog *self)
{
    NMAWirelessDialogPrivate *priv;
    GtkListStore *sec_model;
    GtkTreeIter iter;
    guint32 ap_flags = 0;
    guint32 ap_wpa = 0;
    guint32 ap_rsn = 0;
    guint32 dev_caps;
    NMSettingWirelessSecurity *wsec = NULL;
    NMUtilsSecurityType default_type = NMU_SEC_NONE;
    NMWepKeyType wep_type = NM_WEP_KEY_TYPE_KEY;
    int active = -1;
    int item = 0;
    NMSettingWireless *s_wireless = NULL;
    gboolean is_adhoc;

    g_return_val_if_fail (self != NULL, FALSE);

    priv = NMA_WIRELESS_DIALOG_GET_PRIVATE (self);
    g_return_val_if_fail (priv->device != NULL, FALSE);
    g_return_val_if_fail (priv->sec_combo != NULL, FALSE);

    is_adhoc = priv->adhoc_create;

    /* The security options displayed are filtered based on device
     * capabilities, and if provided, additionally by access point capabilities.
     * If a connection is given, that connection's options should be selected
     * by default.
     */
    dev_caps = nm_device_wifi_get_capabilities (NM_DEVICE_WIFI (priv->device));
    if (priv->ap != NULL) {
        ap_flags = nm_access_point_get_flags (priv->ap);
        ap_wpa = nm_access_point_get_wpa_flags (priv->ap);
        ap_rsn = nm_access_point_get_rsn_flags (priv->ap);
    }

    if (priv->connection) {
        const char *mode;
        const char *security;

        s_wireless = NM_SETTING_WIRELESS (nm_connection_get_setting (priv->connection, NM_TYPE_SETTING_WIRELESS));

        mode = nm_setting_wireless_get_mode (s_wireless);
        if (mode && !strcmp (mode, "adhoc"))
            is_adhoc = TRUE;

        wsec = NM_SETTING_WIRELESS_SECURITY (nm_connection_get_setting (priv->connection, 
                                                                        NM_TYPE_SETTING_WIRELESS_SECURITY));

        security = nm_setting_wireless_get_security (s_wireless);
        if (!security || strcmp (security, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME))
            wsec = NULL;
        if (wsec) {
            default_type = get_default_type_for_security (wsec, !!priv->ap, ap_flags, dev_caps);
            if (default_type == NMU_SEC_STATIC_WEP)
                wep_type = nm_setting_wireless_security_get_wep_key_type (wsec);
            if (wep_type == NM_WEP_KEY_TYPE_UNKNOWN)
                wep_type = NM_WEP_KEY_TYPE_KEY;
        }
    } else if (is_adhoc) {
        default_type = NMU_SEC_STATIC_WEP;
        wep_type = NM_WEP_KEY_TYPE_PASSPHRASE;
    }

    sec_model = gtk_list_store_new (2, G_TYPE_STRING, wireless_security_get_g_type ());

    if (nm_utils_security_valid (NMU_SEC_NONE, dev_caps, !!priv->ap, is_adhoc, ap_flags, ap_wpa, ap_rsn)) {
        gtk_list_store_append (sec_model, &iter);
        gtk_list_store_set (sec_model, &iter,
                            S_NAME_COLUMN, _("None"),
                            -1);
        if (default_type == NMU_SEC_NONE)
            active = item;
        item++;
    }

    /* Don't show Static WEP if both the AP and the device are capable of WPA,
     * even though technically it's possible to have this configuration.
     */
    if (   nm_utils_security_valid (NMU_SEC_STATIC_WEP, dev_caps, !!priv->ap, is_adhoc, ap_flags, ap_wpa, ap_rsn)
           && ((!ap_wpa && !ap_rsn) || !(dev_caps & (NM_WIFI_DEVICE_CAP_WPA | NM_WIFI_DEVICE_CAP_RSN)))) {
        WirelessSecurityWEPKey *ws_wep;

        ws_wep = ws_wep_key_new (priv->connection, NM_WEP_KEY_TYPE_KEY, priv->adhoc_create);
        if (ws_wep) {
            add_security_item (self, WIRELESS_SECURITY (ws_wep), sec_model,
                               &iter, _("WEP 40/128-bit Key"));
            if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_KEY))
                active = item;
            item++;
        }

        ws_wep = ws_wep_key_new (priv->connection, NM_WEP_KEY_TYPE_PASSPHRASE, priv->adhoc_create);
        if (ws_wep) {
            add_security_item (self, WIRELESS_SECURITY (ws_wep), sec_model,
                               &iter, _("WEP 128-bit Passphrase"));
            if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_PASSPHRASE))
                active = item;
            item++;
        }
    }

    /* Don't show LEAP if both the AP and the device are capable of WPA,
     * even though technically it's possible to have this configuration.
     */
    if (   nm_utils_security_valid (NMU_SEC_LEAP, dev_caps, !!priv->ap, is_adhoc, ap_flags, ap_wpa, ap_rsn)
           && ((!ap_wpa && !ap_rsn) || !(dev_caps & (NM_WIFI_DEVICE_CAP_WPA | NM_WIFI_DEVICE_CAP_RSN)))) {
        WirelessSecurityLEAP *ws_leap;

        ws_leap = ws_leap_new (priv->connection);
        if (ws_leap) {
            add_security_item (self, WIRELESS_SECURITY (ws_leap), sec_model,
                               &iter, _("LEAP"));
            if ((active < 0) && (default_type == NMU_SEC_LEAP))
                active = item;
            item++;
        }
    }

    if (nm_utils_security_valid (NMU_SEC_DYNAMIC_WEP, dev_caps, !!priv->ap, is_adhoc, ap_flags, ap_wpa, ap_rsn)) {
        WirelessSecurityDynamicWEP *ws_dynamic_wep;

        ws_dynamic_wep = ws_dynamic_wep_new (priv->connection);
        if (ws_dynamic_wep) {
            add_security_item (self, WIRELESS_SECURITY (ws_dynamic_wep), sec_model,
                               &iter, _("Dynamic WEP (802.1x)"));
            if ((active < 0) && (default_type == NMU_SEC_DYNAMIC_WEP))
                active = item;
            item++;
        }
    }

    if (   nm_utils_security_valid (NMU_SEC_WPA_PSK, dev_caps, !!priv->ap, is_adhoc, ap_flags, ap_wpa, ap_rsn)
           || nm_utils_security_valid (NMU_SEC_WPA2_PSK, dev_caps, !!priv->ap, is_adhoc, ap_flags, ap_wpa, ap_rsn)) {
        WirelessSecurityWPAPSK *ws_wpa_psk;

        ws_wpa_psk = ws_wpa_psk_new (priv->connection);
        if (ws_wpa_psk) {
            add_security_item (self, WIRELESS_SECURITY (ws_wpa_psk), sec_model,
                               &iter, _("WPA & WPA2 Personal"));
            if ((active < 0) && ((default_type == NMU_SEC_WPA_PSK) || (default_type == NMU_SEC_WPA2_PSK)))
                active = item;
            item++;
        }
    }

    if (   nm_utils_security_valid (NMU_SEC_WPA_ENTERPRISE, dev_caps, !!priv->ap, is_adhoc, ap_flags, ap_wpa, ap_rsn)
           || nm_utils_security_valid (NMU_SEC_WPA2_ENTERPRISE, dev_caps, !!priv->ap, is_adhoc, ap_flags, ap_wpa, ap_rsn)) {
        WirelessSecurityWPAEAP *ws_wpa_eap;

        ws_wpa_eap = ws_wpa_eap_new (priv->connection);
        if (ws_wpa_eap) {
            add_security_item (self, WIRELESS_SECURITY (ws_wpa_eap), sec_model,
                               &iter, _("WPA & WPA2 Enterprise"));
            if ((active < 0) && ((default_type == NMU_SEC_WPA_ENTERPRISE) || (default_type == NMU_SEC_WPA2_ENTERPRISE)))
                active = item;
            item++;
        }
    }

    gtk_combo_box_set_model (GTK_COMBO_BOX (priv->sec_combo), GTK_TREE_MODEL (sec_model));
    gtk_combo_box_set_active (GTK_COMBO_BOX (priv->sec_combo), active < 0 ? 0 : (guint32) active);
    g_object_unref (G_OBJECT (sec_model));
    return TRUE;
}
/**
 * nm_access_point_connection_valid:
 * @ap: an #NMAccessPoint to validate @connection against
 * @connection: an #NMConnection to validate against @ap
 *
 * Validates a given connection against a given Wi-Fi access point to ensure that
 * the connection may be activated with that AP.  The connection must match the
 * @ap's SSID, (if given) BSSID, and other attributes like security settings,
 * channel, band, etc.
 *
 * Returns: %TRUE if the connection may be activated with this Wi-Fi AP,
 * %FALSE if it cannot be.
 **/
gboolean
nm_access_point_connection_valid (NMAccessPoint *ap, NMConnection *connection)
{
	NMSettingConnection *s_con;
	NMSettingWireless *s_wifi;
	NMSettingWirelessSecurity *s_wsec;
	const char *ctype, *ap_bssid_str;
	const GByteArray *setting_ssid;
	const GByteArray *ap_ssid;
	const GByteArray *setting_bssid;
	struct ether_addr *ap_bssid;
	const char *setting_mode;
	NM80211Mode ap_mode;
	const char *setting_band;
	guint32 ap_freq, setting_chan, ap_chan;

	s_con = nm_connection_get_setting_connection (connection);
	g_assert (s_con);
	ctype = nm_setting_connection_get_connection_type (s_con);
	if (strcmp (ctype, NM_SETTING_WIRELESS_SETTING_NAME) != 0)
		return FALSE;

	s_wifi = nm_connection_get_setting_wireless (connection);
	if (!s_wifi)
		return FALSE;

	/* SSID checks */
	ap_ssid = nm_access_point_get_ssid (ap);
	g_warn_if_fail (ap_ssid != NULL);
	setting_ssid = nm_setting_wireless_get_ssid (s_wifi);
	if (!setting_ssid || !ap_ssid || (setting_ssid->len != ap_ssid->len))
		return FALSE;
	if (memcmp (setting_ssid->data, ap_ssid->data, ap_ssid->len) != 0)
		return FALSE;

	/* BSSID checks */
	ap_bssid_str = nm_access_point_get_bssid (ap);
	g_warn_if_fail (ap_bssid_str);
	setting_bssid = nm_setting_wireless_get_bssid (s_wifi);
	if (setting_bssid && ap_bssid_str) {
		g_assert (setting_bssid->len == ETH_ALEN);
		ap_bssid = ether_aton (ap_bssid_str);
		g_warn_if_fail (ap_bssid);
		if (ap_bssid) {
			if (memcmp (ap_bssid->ether_addr_octet, setting_bssid->data, ETH_ALEN) != 0)
				return FALSE;
		}
	}

	/* Mode */
	ap_mode = nm_access_point_get_mode (ap);
	g_warn_if_fail (ap_mode != NM_802_11_MODE_UNKNOWN);
	setting_mode = nm_setting_wireless_get_mode (s_wifi);
	if (setting_mode && ap_mode) {
		if (!strcmp (setting_mode, "infrastructure") && (ap_mode != NM_802_11_MODE_INFRA))
			return FALSE;
		if (!strcmp (setting_mode, "adhoc") && (ap_mode != NM_802_11_MODE_ADHOC))
			return FALSE;
		/* Hotspot never matches against APs as it's a device-specific mode. */
		if (!strcmp (setting_mode, "ap"))
			return FALSE;
	}

	/* Band and Channel/Frequency */
	ap_freq = nm_access_point_get_frequency (ap);
	if (ap_freq) {
		setting_band = nm_setting_wireless_get_band (s_wifi);
		if (g_strcmp0 (setting_band, "a") == 0) {
			if (ap_freq < 4915 || ap_freq > 5825)
				return FALSE;
		} else if (g_strcmp0 (setting_band, "bg") == 0) {
			if (ap_freq < 2412 || ap_freq > 2484)
				return FALSE;
		}

		setting_chan = nm_setting_wireless_get_channel (s_wifi);
		if (setting_chan) {
			ap_chan = nm_utils_wifi_freq_to_channel (ap_freq);
			if (setting_chan != ap_chan)
				return FALSE;
		}
	}

	s_wsec = nm_connection_get_setting_wireless_security (connection);
	if (!nm_setting_wireless_ap_security_compatible (s_wifi,
	                                                 s_wsec,
	                                                 nm_access_point_get_flags (ap),
	                                                 nm_access_point_get_wpa_flags (ap),
	                                                 nm_access_point_get_rsn_flags (ap),
	                                                 ap_mode))
		return FALSE;

	return TRUE;
}
static void
show_access_point_info (NMAccessPoint *ap)
{
	guint32 flags, wpa_flags, rsn_flags, freq, bitrate;
	guint8 strength;
	const GByteArray *ssid; 
	const char *hwaddr;
	NM80211Mode mode;
	char *freq_str, *ssid_str, *bitrate_str, *strength_str, *wpa_flags_str, *rsn_flags_str;
	GString *security_str;

	/* Get AP properties */
	flags = nm_access_point_get_flags (ap);
	wpa_flags = nm_access_point_get_wpa_flags (ap);
	rsn_flags = nm_access_point_get_rsn_flags (ap);
	ssid = nm_access_point_get_ssid (ap);
	hwaddr = nm_access_point_get_hw_address (ap);
	freq = nm_access_point_get_frequency (ap);
	mode = nm_access_point_get_mode (ap);
	bitrate = nm_access_point_get_max_bitrate (ap);
	strength = nm_access_point_get_strength (ap);

	/* Convert to strings */
	ssid_str = nm_utils_ssid_to_utf8 (ssid);
	freq_str = g_strdup_printf ("%u MHz", freq);
	bitrate_str = g_strdup_printf ("%u MB/s", bitrate/1000);
	strength_str = g_strdup_printf ("%u", strength);
	wpa_flags_str = ap_wpa_rsn_flags_to_string (wpa_flags);
	rsn_flags_str = ap_wpa_rsn_flags_to_string (rsn_flags);

	security_str = g_string_new (NULL);
	if (   !(flags & NM_802_11_AP_FLAGS_PRIVACY)
	    &&  (wpa_flags != NM_802_11_AP_SEC_NONE)
	    &&  (rsn_flags != NM_802_11_AP_SEC_NONE))
		g_string_append (security_str, "Encrypted: ");

	if (   (flags & NM_802_11_AP_FLAGS_PRIVACY)
	    && (wpa_flags == NM_802_11_AP_SEC_NONE)
	    && (rsn_flags == NM_802_11_AP_SEC_NONE))
		g_string_append (security_str, "WEP ");
	if (wpa_flags != NM_802_11_AP_SEC_NONE)
		g_string_append (security_str, "WPA ");
	if (rsn_flags != NM_802_11_AP_SEC_NONE)
		g_string_append (security_str, "WPA2 ");
	if (   (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
	    || (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X))
		g_string_append (security_str, "Enterprise ");

	if (security_str->len > 0)
		g_string_truncate (security_str, security_str->len-1);  /* Chop off last space */

	printf ("SSID:       %s\n", ssid_str);
	printf ("BSSID:      %s\n", hwaddr);
	printf ("Mode:       %s\n", mode == NM_802_11_MODE_ADHOC ? "Ad-Hoc"
	                          : mode == NM_802_11_MODE_INFRA ? "Infrastructure"
	                          : "Unknown");
	printf ("Freq:       %s\n", freq_str);
	printf ("Bitrate:    %s\n", bitrate_str);
	printf ("Strength:   %s\n", strength_str);
	printf ("Security:   %s\n", security_str->str);
	printf ("WPA flags:  %s\n", wpa_flags_str);
	printf ("RSN flags:  %s\n", rsn_flags_str);
	printf ("D-Bus path: %s\n\n", nm_object_get_path (NM_OBJECT (ap)));

	g_free (ssid_str);
	g_free (freq_str);
	g_free (bitrate_str);
	g_free (strength_str);
	g_free (wpa_flags_str);
	g_free (rsn_flags_str);
	g_string_free (security_str, TRUE);
}