WirelessSecurity *
wireless_security_init (gsize obj_size,
                        WSValidateFunc validate,
                        WSAddToSizeGroupFunc add_to_size_group,
                        WSFillConnectionFunc fill_connection,
                        WSUpdateSecretsFunc update_secrets,
                        WSDestroyFunc destroy,
                        const char *ui_resource,
                        const char *ui_widget_name,
                        const char *default_field)
{
	WirelessSecurity *sec;
	GError *error = NULL;

	g_return_val_if_fail (obj_size > 0, NULL);
	g_return_val_if_fail (ui_resource != NULL, NULL);
	g_return_val_if_fail (ui_widget_name != NULL, NULL);

	sec = g_slice_alloc0 (obj_size);
	g_assert (sec);

	sec->refcount = 1;
	sec->obj_size = obj_size;

	sec->validate = validate;
	sec->add_to_size_group = add_to_size_group;
	sec->fill_connection = fill_connection;
	sec->update_secrets = update_secrets;
	sec->destroy = destroy;
	sec->default_field = default_field;

	sec->builder = gtk_builder_new ();
	if (!gtk_builder_add_from_resource (sec->builder, ui_resource, &error)) {
		g_warning ("Couldn't load UI builder file %s: %s",
		           ui_resource, error->message);
		g_error_free (error);
		wireless_security_unref (sec);
		return NULL;
	}

	sec->ui_widget = GTK_WIDGET (gtk_builder_get_object (sec->builder, ui_widget_name));
	if (!sec->ui_widget) {
		g_warning ("Couldn't load UI widget '%s' from UI file %s",
		           ui_widget_name, ui_resource);
		wireless_security_unref (sec);
		return NULL;
	}
	g_object_ref_sink (sec->ui_widget);

	sec->adhoc_compatible = TRUE;

	return sec;
}
static void
ssid_entry_changed (GtkWidget *entry, gpointer user_data)
{
    NMAWirelessDialog *self = NMA_WIRELESS_DIALOG (user_data);
    NMAWirelessDialogPrivate *priv = NMA_WIRELESS_DIALOG_GET_PRIVATE (self);
    GtkTreeIter iter;
    WirelessSecurity *sec = NULL;
    GtkTreeModel *model;
    gboolean valid = FALSE;
    GByteArray *ssid;

    ssid = validate_dialog_ssid (self);
    if (!ssid)
        goto out;

    model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->sec_combo));
    if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->sec_combo), &iter))
        gtk_tree_model_get (model, &iter, S_SEC_COLUMN, &sec, -1);

    if (sec) {
        valid = wireless_security_validate (sec, ssid);
        wireless_security_unref (sec);
    } else {
        valid = TRUE;
    }

 out:
    gtk_dialog_set_response_sensitive (GTK_DIALOG (self), GTK_RESPONSE_OK, valid);
}
static void
add_security_item (CEPageWirelessSecurity *self,
                   WirelessSecurity *sec,
                   GtkListStore *model,
                   GtkTreeIter *iter,
                   const char *text)
{
	wireless_security_set_changed_notify (sec, stuff_changed_cb, self);
	gtk_list_store_append (model, iter);
	gtk_list_store_set (model, iter, S_NAME_COLUMN, text, S_SEC_COLUMN, sec, -1);
	wireless_security_unref (sec);
}
static gboolean
validate (CEPage        *page,
          NMConnection  *connection,
          GError       **error)
{
        NMSettingWireless *sw;
        NMSettingConnection *sc;
        WirelessSecurity *sec;
        gboolean valid = FALSE;
        const char *mode;

        sw = nm_connection_get_setting_wireless (connection);

        mode = nm_setting_wireless_get_mode (sw);
        if (g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC) == 0)
                CE_PAGE_SECURITY (page)->adhoc = TRUE;
        else
                CE_PAGE_SECURITY (page)->adhoc = FALSE;

        sec = security_combo_get_active (CE_PAGE_SECURITY (page));
        if (sec) {
                GBytes *ssid = nm_setting_wireless_get_ssid (sw);

                if (ssid) {
                        /* FIXME: get failed property and error out of wifi security objects */
                        valid = wireless_security_validate (sec, error);
                        if (valid)
                                wireless_security_fill_connection (sec, connection);
                } else {
                        g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_SETTING, "Missing SSID");
                        valid = FALSE;
                }

                if (CE_PAGE_SECURITY (page)->adhoc) {
                        if (!wireless_security_adhoc_compatible (sec)) {
                                g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_SETTING, "Security not compatible with Ad-Hoc mode");
                                valid = FALSE;
                        }
                }

                wireless_security_unref (sec);
        } else {
                /* No security, unencrypted */
                nm_connection_remove_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
                nm_connection_remove_setting (connection, NM_TYPE_SETTING_802_1X);
                valid = TRUE;
        }

        sc = nm_connection_get_setting_connection (connection);
        firewall_ui_to_setting (sc, GTK_WIDGET (CE_PAGE_SECURITY (page)->firewall_combo));

        return valid;
}
static gboolean
validate (CEPage        *page,
          NMConnection  *connection,
          GError       **error)
{
        NMSettingWireless *sw;
        WirelessSecurity *sec;
        gboolean valid = FALSE;
        const char *mode;

        sw = nm_connection_get_setting_wireless (connection);

        mode = nm_setting_wireless_get_mode (sw);
        if (g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC) == 0)
                CE_PAGE_SECURITY (page)->adhoc = TRUE;
        else
                CE_PAGE_SECURITY (page)->adhoc = FALSE;

        sec = security_combo_get_active (CE_PAGE_SECURITY (page));
        if (sec) {
                const GByteArray *ssid = nm_setting_wireless_get_ssid (sw);

                if (ssid) {
                        /* FIXME: get failed property and error out of wifi security objects */
                        valid = wireless_security_validate (sec, ssid);
                        if (valid)
                                wireless_security_fill_connection (sec, connection);
                        else
                                g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_UNKNOWN, "Invalid Wi-Fi security");
                } else {
                        g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_UNKNOWN, "Missing SSID");
                        valid = FALSE;
                }

                if (CE_PAGE_SECURITY (page)->adhoc) {
                        if (!wireless_security_adhoc_compatible (sec)) {
                                g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_UNKNOWN, "Security not compatible with Ad-Hoc mode");
                                valid = FALSE;
                        }
                }

                wireless_security_unref (sec);
        } else {
                /* No security, unencrypted */
                g_object_set (sw, NM_SETTING_WIRELESS_SEC, NULL, NULL);
                nm_connection_remove_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
                nm_connection_remove_setting (connection, NM_TYPE_SETTING_802_1X);
                valid = TRUE;
        }

        return valid;
}
static void
security_combo_changed (GtkWidget *combo,
                        gpointer user_data)
{
    NMAWirelessDialog *self = NMA_WIRELESS_DIALOG (user_data);
    NMAWirelessDialogPrivate *priv = NMA_WIRELESS_DIALOG_GET_PRIVATE (self);
    GtkWidget *vbox;
    GList *elt, *children;
    GtkTreeIter iter;
    GtkTreeModel *model;
    WirelessSecurity *sec = NULL;
    GtkWidget *sec_widget;

    vbox = GTK_WIDGET (gtk_builder_get_object (priv->builder, "security_vbox"));
    g_assert (vbox);

    size_group_clear (priv->group);

    /* Remove any previous wireless security widgets */
    children = gtk_container_get_children (GTK_CONTAINER (vbox));
    for (elt = children; elt; elt = g_list_next (elt))
        gtk_container_remove (GTK_CONTAINER (vbox), GTK_WIDGET (elt->data));

    model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
    if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter)) {
        g_warning ("%s: no active security combo box item.", __func__);
        return;
    }

    gtk_tree_model_get (model, &iter, S_SEC_COLUMN, &sec, -1);
    if (!sec)
        return;

    sec_widget = wireless_security_get_widget (sec);
    g_assert (sec_widget);

    size_group_add_permanent (priv->group, priv->builder);
    wireless_security_add_to_size_group (sec, priv->group);

    if (gtk_widget_get_parent (sec_widget))
        gtk_widget_reparent (sec_widget, vbox);
    else
        gtk_container_add (GTK_CONTAINER (vbox), sec_widget);

    wireless_security_unref (sec);

    /* Re-validate */
    wireless_security_changed_cb (NULL, sec);
}
static void
add_security_item (CEPageSecurity   *page,
                   WirelessSecurity *sec,
                   GtkListStore     *model,
                   GtkTreeIter      *iter,
                   const char       *text,
                   gboolean          adhoc_valid)
{
        wireless_security_set_changed_notify (sec, stuff_changed_cb, page);
        gtk_list_store_append (model, iter);
        gtk_list_store_set (model, iter,
                            S_NAME_COLUMN, text,
                            S_SEC_COLUMN, sec,
                            S_ADHOC_VALID_COLUMN, adhoc_valid,
                            -1);
        wireless_security_unref (sec);
}
Exemple #8
0
static void
destroy (EAPMethod *parent)
{
	EAPMethodSimple *method = (EAPMethodSimple *) parent;
	GtkWidget *widget;

	widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "eap_simple_notebook"));
	g_assert (widget);

	g_signal_handlers_disconnect_by_func (G_OBJECT (widget),
	                                      (GCallback) widgets_realized,
	                                      method);
	g_signal_handlers_disconnect_by_func (G_OBJECT (widget),
	                                      (GCallback) widgets_unrealized,
	                                      method);

	wireless_security_unref (method->ws_parent);
}
static void
wireless_security_combo_changed (GtkComboBox *combo,
                                 gpointer user_data)
{
	CEPageWirelessSecurity *self = CE_PAGE_WIRELESS_SECURITY (user_data);
	GtkWidget *vbox;
	GList *elt, *children;
	WirelessSecurity *sec;

	vbox = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (self)->builder, "wireless_security_vbox"));
	g_assert (vbox);

	wsec_size_group_clear (self->group);

	/* Remove any previous wireless security widgets */
	children = gtk_container_get_children (GTK_CONTAINER (vbox));
	for (elt = children; elt; elt = g_list_next (elt))
		gtk_container_remove (GTK_CONTAINER (vbox), GTK_WIDGET (elt->data));

	sec = wireless_security_combo_get_active (self);
	if (sec) {
		GtkWidget *sec_widget;
		GtkWidget *widget, *parent;

		sec_widget = wireless_security_get_widget (sec);
		g_assert (sec_widget);
		parent = gtk_widget_get_parent (sec_widget);
		if (parent)
			gtk_container_remove (GTK_CONTAINER (parent), sec_widget);

		widget = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (self)->builder, "wireless_security_combo_label"));
		gtk_size_group_add_widget (self->group, widget);
		wireless_security_add_to_size_group (sec, self->group);

		gtk_container_add (GTK_CONTAINER (vbox), sec_widget);
		wireless_security_unref (sec);
	}

	ce_page_changed (CE_PAGE (self));
}
static void
security_combo_changed (GtkComboBox *combo,
                        gpointer     user_data)
{
        CEPageSecurity *page = CE_PAGE_SECURITY (user_data);
        GtkWidget *vbox;
        GList *l, *children;
        WirelessSecurity *sec;

        wsec_size_group_clear (page->group);

        vbox = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (page)->builder, "vbox"));
        children = gtk_container_get_children (GTK_CONTAINER (vbox));
        for (l = children; l; l = l->next) {
                gtk_container_remove (GTK_CONTAINER (vbox), GTK_WIDGET (l->data));
        }

        sec = security_combo_get_active (page);
        if (sec) {
                GtkWidget *sec_widget;
                GtkWidget *parent;

                sec_widget = wireless_security_get_widget (sec);
                g_assert (sec_widget);
                parent = gtk_widget_get_parent (sec_widget);
                if (parent)
                        gtk_container_remove (GTK_CONTAINER (parent), sec_widget);

                gtk_size_group_add_widget (page->group, page->security_heading);
                gtk_size_group_add_widget (page->group, page->firewall_heading);
                wireless_security_add_to_size_group (sec, page->group);

                gtk_container_add (GTK_CONTAINER (vbox), sec_widget);
                wireless_security_unref (sec);
        }

        ce_page_changed (CE_PAGE (page));
}
static void
dialog_response (GtkDialog *dialog,
                 gint response,
                 gpointer user_data)
{
    NMAWirelessDialog *self = NMA_WIRELESS_DIALOG (user_data);
    NMAWirelessDialogPrivate *priv = NMA_WIRELESS_DIALOG_GET_PRIVATE (self);
    NMSetting *setting;
    GtkTreeModel *model;
    GtkTreeIter iter;
    WirelessSecurity *sec = NULL;

    if (response != GTK_RESPONSE_OK)
        goto out;

    if (!priv->connection) {
        /* Create new connection */
        char *id;
        char *uuid;
        GByteArray *ssid;

        priv->connection = nm_connection_new ();

        /* Wireless setting */
        setting = nm_setting_wireless_new ();
        ssid = validate_dialog_ssid (self);
        g_object_set (setting, NM_SETTING_WIRELESS_SSID, ssid, NULL);
        nm_connection_add_setting (priv->connection, setting);

        if (ssid) {
            id = nm_utils_ssid_to_utf8 ((char *) ssid->data, ssid->len);
            g_byte_array_free (ssid, TRUE);
        } else
            id = NULL;

        /* Connection setting */
        setting = nm_setting_connection_new ();
        uuid = nm_utils_uuid_generate ();

        /* FIXME: don't autoconnect until the connection is successful at least once */
        /* Don't autoconnect adhoc networks by default for now */
        g_object_set (setting,
                      NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME,
                      NM_SETTING_CONNECTION_UUID, uuid,
                      NM_SETTING_CONNECTION_ID, id,
                      NM_SETTING_CONNECTION_AUTOCONNECT, !priv->adhoc_create,
                      NULL);

        g_free (uuid);
        g_free (id);
        nm_connection_add_setting (priv->connection, setting);

        /* IPv4 setting */
        if (priv->adhoc_create) {
            g_object_set (setting, NM_SETTING_WIRELESS_MODE, "adhoc", NULL);

            setting = nm_setting_ip4_config_new ();
            g_object_set (setting,
                          NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_SHARED,
                          NULL);

            nm_connection_add_setting (priv->connection, setting);
        }
    }

    model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->sec_combo));
    gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->sec_combo), &iter);
    gtk_tree_model_get (model, &iter, S_SEC_COLUMN, &sec, -1);

    if (sec && !priv->nag_ignored) {
        GtkWidget *nag_dialog;

        /* Nag the user about certificates or whatever.  Only destroy the dialog
         * if no nagging was done.
         */
        nag_dialog = wireless_security_nag_user (sec);
        if (nag_dialog) {
            gtk_window_set_transient_for (GTK_WINDOW (nag_dialog), GTK_WINDOW (dialog));
            g_signal_connect (nag_dialog, "response",
                              G_CALLBACK (nag_dialog_response_cb),
                              dialog);
            return;
        }
    }

    /* Fill security */
    if (sec) {
        wireless_security_fill_connection (sec, priv->connection);
        wireless_security_unref (sec);
    } else {
        /* Unencrypted */
        setting = nm_connection_get_setting (priv->connection, NM_TYPE_SETTING_WIRELESS);
        g_object_set (setting, NM_SETTING_WIRELESS_SEC, NULL, NULL);
    }

 out:
    g_signal_emit (self, signals[DONE], 0, response);
    nm_utils_dialog_done ();

    /* FIXME: clear security? */
}