Blame clients/tui/nmt-password-dialog.c

Packit Service 87a54e
/* SPDX-License-Identifier: GPL-2.0-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2013 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * SECTION:nmt-password-dialog
Packit 5756e2
 * @short_description: A password dialog
Packit 5756e2
 *
Packit 5756e2
 * #NmtPasswordDialog is the password dialog used to get connection
Packit 5756e2
 * secrets when activating a connection.
Packit 5756e2
 */
Packit 5756e2
Packit Service 2bceb2
#include "libnm/nm-default-client.h"
Packit 5756e2
Packit 5756e2
#include "nmt-password-dialog.h"
Packit 5756e2
#include "nm-secret-agent-simple.h"
Packit 5756e2
#include "nmtui.h"
Packit 5756e2
Packit Service a1bd4f
G_DEFINE_TYPE(NmtPasswordDialog, nmt_password_dialog, NMT_TYPE_NEWT_FORM)
Packit 5756e2
Packit Service a1bd4f
#define NMT_PASSWORD_DIALOG_GET_PRIVATE(o) \
Packit Service a1bd4f
    (G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_PASSWORD_DIALOG, NmtPasswordDialogPrivate))
Packit 5756e2
Packit 5756e2
typedef struct {
Packit Service a1bd4f
    char *     request_id;
Packit Service a1bd4f
    char *     prompt;
Packit Service a1bd4f
    GPtrArray *secrets;
Packit Service a1bd4f
    GPtrArray *entries;
Packit 5756e2
Packit Service a1bd4f
    NmtNewtWidget *ok, *cancel;
Packit Service a1bd4f
    NmtNewtWidget *last_entry;
Packit Service a1bd4f
    NmtNewtWidget *secret_grid;
Packit 5756e2
Packit Service a1bd4f
    gboolean succeeded;
Packit 5756e2
} NmtPasswordDialogPrivate;
Packit 5756e2
Packit 5756e2
enum {
Packit Service a1bd4f
    PROP_0,
Packit Service a1bd4f
    PROP_REQUEST_ID,
Packit Service a1bd4f
    PROP_PROMPT,
Packit Service a1bd4f
    PROP_SECRETS,
Packit 5756e2
Packit Service a1bd4f
    LAST_PROP
Packit 5756e2
};
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nmt_password_dialog_new:
Packit 5756e2
 * @request_id: the request ID from the #NMSecretAgentSimple
Packit 5756e2
 * @title: the dialog title
Packit 5756e2
 * @prompt: the prompt text to display
Packit 5756e2
 * @secrets: (element-type #NMSecretAgentSimpleSecret): the secrets requested
Packit 5756e2
 *
Packit 5756e2
 * Creates a new #NmtPasswordDialog to request passwords from
Packit 5756e2
 * the user.
Packit 5756e2
 *
Packit 5756e2
 * Returns: a new #NmtPasswordDialog.
Packit 5756e2
 */
Packit 5756e2
NmtNewtForm *
Packit Service a1bd4f
nmt_password_dialog_new(const char *request_id,
Packit Service a1bd4f
                        const char *title,
Packit Service a1bd4f
                        const char *prompt,
Packit Service a1bd4f
                        GPtrArray * secrets)
Packit 5756e2
{
Packit Service a1bd4f
    return g_object_new(NMT_TYPE_PASSWORD_DIALOG,
Packit Service a1bd4f
                        "request-id",
Packit Service a1bd4f
                        request_id,
Packit Service a1bd4f
                        "title",
Packit Service a1bd4f
                        title,
Packit Service a1bd4f
                        "prompt",
Packit Service a1bd4f
                        prompt,
Packit Service a1bd4f
                        "secrets",
Packit Service a1bd4f
                        secrets,
Packit Service a1bd4f
                        "escape-exits",
Packit Service a1bd4f
                        TRUE,
Packit Service a1bd4f
                        NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_password_dialog_init(NmtPasswordDialog *dialog)
Packit 5756e2
{
Packit Service a1bd4f
    NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
Packit 5756e2
Packit Service a1bd4f
    priv->entries = g_ptr_array_new();
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
maybe_save_input_and_exit(NmtNewtWidget *widget, gpointer dialog)
Packit 5756e2
{
Packit Service a1bd4f
    NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
Packit Service a1bd4f
    int                       i;
Packit 5756e2
Packit Service a1bd4f
    /* This gets invoked when the user types Return in the final entry,
Packit Service a1bd4f
     * but the form may not be fully valid in that case.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    if (!nmt_newt_widget_get_valid(priv->secret_grid))
Packit Service a1bd4f
        return;
Packit 5756e2
Packit Service a1bd4f
    priv->succeeded = TRUE;
Packit 5756e2
Packit Service a1bd4f
    for (i = 0; i < priv->secrets->len; i++) {
Packit Service a1bd4f
        NMSecretAgentSimpleSecret *secret = priv->secrets->pdata[i];
Packit 5756e2
Packit Service a1bd4f
        g_free(secret->value);
Packit Service a1bd4f
        g_object_get(priv->entries->pdata[i], "text", &secret->value, NULL);
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    nmt_newt_form_quit(nmt_newt_widget_get_form(widget));
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_password_dialog_constructed(GObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    NmtPasswordDialog *       dialog = NMT_PASSWORD_DIALOG(object);
Packit Service a1bd4f
    NmtPasswordDialogPrivate *priv   = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
Packit Service a1bd4f
    NmtNewtWidget *           widget;
Packit Service a1bd4f
    NmtNewtGrid *             grid, *secret_grid;
Packit Service a1bd4f
    NmtNewtButtonBox *        bbox;
Packit Service a1bd4f
    int                       i;
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_newt_grid_new();
Packit Service a1bd4f
    nmt_newt_form_set_content(NMT_NEWT_FORM(dialog), widget);
Packit Service a1bd4f
    grid = NMT_NEWT_GRID(widget);
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_newt_textbox_new(0, 60);
Packit Service a1bd4f
    nmt_newt_textbox_set_text(NMT_NEWT_TEXTBOX(widget), priv->prompt);
Packit Service a1bd4f
    nmt_newt_grid_add(grid, widget, 0, 0);
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_newt_grid_new();
Packit Service a1bd4f
    nmt_newt_grid_add(grid, widget, 0, 1);
Packit Service a1bd4f
    nmt_newt_widget_set_padding(widget, 0, 1, 0, 1);
Packit Service a1bd4f
    priv->secret_grid = widget;
Packit Service a1bd4f
    secret_grid       = NMT_NEWT_GRID(widget);
Packit Service a1bd4f
Packit Service a1bd4f
    for (i = 0; i < priv->secrets->len; i++) {
Packit Service a1bd4f
        NMSecretAgentSimpleSecret *secret = priv->secrets->pdata[i];
Packit Service a1bd4f
        NmtNewtEntryFlags          flags;
Packit Service a1bd4f
Packit Service a1bd4f
        widget = nmt_newt_label_new(secret->pretty_name);
Packit Service a1bd4f
        nmt_newt_grid_add(secret_grid, widget, 0, i);
Packit Service a1bd4f
        nmt_newt_widget_set_padding(widget, 4, 0, 1, 0);
Packit Service a1bd4f
Packit Service a1bd4f
        flags = NMT_NEWT_ENTRY_NONEMPTY;
Packit Service a1bd4f
        if (secret->is_secret)
Packit Service a1bd4f
            flags |= NMT_NEWT_ENTRY_PASSWORD;
Packit Service a1bd4f
        widget = nmt_newt_entry_new(30, flags);
Packit Service a1bd4f
        if (secret->value)
Packit Service a1bd4f
            nmt_newt_entry_set_text(NMT_NEWT_ENTRY(widget), secret->value);
Packit Service a1bd4f
        nmt_newt_grid_add(secret_grid, widget, 1, i);
Packit Service a1bd4f
        g_ptr_array_add(priv->entries, widget);
Packit Service a1bd4f
Packit Service a1bd4f
        if (i == priv->secrets->len - 1) {
Packit Service a1bd4f
            priv->last_entry = widget;
Packit Service a1bd4f
            g_signal_connect(widget, "activated", G_CALLBACK(maybe_save_input_and_exit), dialog);
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_newt_button_box_new(NMT_NEWT_BUTTON_BOX_HORIZONTAL);
Packit Service a1bd4f
    nmt_newt_grid_add(grid, widget, 0, 2);
Packit Service a1bd4f
    bbox = NMT_NEWT_BUTTON_BOX(widget);
Packit Service a1bd4f
Packit Service a1bd4f
    priv->cancel = nmt_newt_button_box_add_end(NMT_NEWT_BUTTON_BOX(bbox), _("Cancel"));
Packit Service a1bd4f
    nmt_newt_widget_set_exit_on_activate(priv->cancel, TRUE);
Packit Service a1bd4f
Packit Service a1bd4f
    priv->ok = nmt_newt_button_box_add_end(NMT_NEWT_BUTTON_BOX(bbox), _("OK"));
Packit Service a1bd4f
    g_signal_connect(priv->ok, "activated", G_CALLBACK(maybe_save_input_and_exit), dialog);
Packit Service a1bd4f
    g_object_bind_property(priv->secret_grid,
Packit Service a1bd4f
                           "valid",
Packit Service a1bd4f
                           priv->ok,
Packit Service a1bd4f
                           "sensitive",
Packit Service a1bd4f
                           G_BINDING_SYNC_CREATE);
Packit Service a1bd4f
Packit Service a1bd4f
    G_OBJECT_CLASS(nmt_password_dialog_parent_class)->constructed(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_password_dialog_finalize(GObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(object);
Packit 5756e2
Packit Service a1bd4f
    g_free(priv->request_id);
Packit Service a1bd4f
    g_free(priv->prompt);
Packit Service a1bd4f
    nm_clear_pointer(&priv->entries, g_ptr_array_unref);
Packit Service a1bd4f
    nm_clear_pointer(&priv->secrets, g_ptr_array_unref);
Packit 5756e2
Packit Service a1bd4f
    G_OBJECT_CLASS(nmt_password_dialog_parent_class)->finalize(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_password_dialog_set_property(GObject *     object,
Packit Service a1bd4f
                                 guint         prop_id,
Packit Service a1bd4f
                                 const GValue *value,
Packit Service a1bd4f
                                 GParamSpec *  pspec)
Packit 5756e2
{
Packit Service a1bd4f
    NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(object);
Packit Service a1bd4f
Packit Service a1bd4f
    switch (prop_id) {
Packit Service a1bd4f
    case PROP_REQUEST_ID:
Packit Service a1bd4f
        priv->request_id = g_value_dup_string(value);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_PROMPT:
Packit Service a1bd4f
        priv->prompt = g_value_dup_string(value);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_SECRETS:
Packit Service a1bd4f
        priv->secrets = g_value_dup_boxed(value);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    default:
Packit Service a1bd4f
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_password_dialog_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
Packit 5756e2
{
Packit Service a1bd4f
    NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(object);
Packit Service a1bd4f
Packit Service a1bd4f
    switch (prop_id) {
Packit Service a1bd4f
    case PROP_REQUEST_ID:
Packit Service a1bd4f
        g_value_set_string(value, priv->request_id);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_PROMPT:
Packit Service a1bd4f
        g_value_set_string(value, priv->prompt);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_SECRETS:
Packit Service a1bd4f
        g_value_set_boxed(value, priv->secrets);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    default:
Packit Service a1bd4f
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_password_dialog_class_init(NmtPasswordDialogClass *dialog_class)
Packit 5756e2
{
Packit Service a1bd4f
    GObjectClass *object_class = G_OBJECT_CLASS(dialog_class);
Packit Service a1bd4f
Packit Service a1bd4f
    g_type_class_add_private(dialog_class, sizeof(NmtPasswordDialogPrivate));
Packit Service a1bd4f
Packit Service a1bd4f
    /* virtual methods */
Packit Service a1bd4f
    object_class->constructed  = nmt_password_dialog_constructed;
Packit Service a1bd4f
    object_class->set_property = nmt_password_dialog_set_property;
Packit Service a1bd4f
    object_class->get_property = nmt_password_dialog_get_property;
Packit Service a1bd4f
    object_class->finalize     = nmt_password_dialog_finalize;
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NmtPasswordDialog:request-id:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The request ID from the #NMSecretAgentSimple
Packit Service a1bd4f
     */
Packit Service a1bd4f
    g_object_class_install_property(
Packit Service a1bd4f
        object_class,
Packit Service a1bd4f
        PROP_REQUEST_ID,
Packit Service a1bd4f
        g_param_spec_string("request-id",
Packit Service a1bd4f
                            "",
Packit Service a1bd4f
                            "",
Packit Service a1bd4f
                            NULL,
Packit Service a1bd4f
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NmtPasswordDialog:prompt:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The prompt text.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    g_object_class_install_property(
Packit Service a1bd4f
        object_class,
Packit Service a1bd4f
        PROP_PROMPT,
Packit Service a1bd4f
        g_param_spec_string("prompt",
Packit Service a1bd4f
                            "",
Packit Service a1bd4f
                            "",
Packit Service a1bd4f
                            NULL,
Packit Service a1bd4f
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NmtPasswordDialog:secrets:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The array of request secrets
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * Element-Type: #NMSecretAgentSimpleSecret.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    g_object_class_install_property(
Packit Service a1bd4f
        object_class,
Packit Service a1bd4f
        PROP_SECRETS,
Packit Service a1bd4f
        g_param_spec_boxed("secrets",
Packit Service a1bd4f
                           "",
Packit Service a1bd4f
                           "",
Packit Service a1bd4f
                           G_TYPE_PTR_ARRAY,
Packit Service a1bd4f
                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nmt_password_dialog_succeeded:
Packit 5756e2
 * @dialog: the #NmtPasswordDialog
Packit 5756e2
 *
Packit 5756e2
 * After the dialog has exited, returns %TRUE if the user clicked
Packit 5756e2
 * "OK", %FALSE if "Cancel".
Packit 5756e2
 *
Packit 5756e2
 * Returns: whether the dialog succeeded.
Packit 5756e2
 */
Packit 5756e2
gboolean
Packit Service a1bd4f
nmt_password_dialog_succeeded(NmtPasswordDialog *dialog)
Packit 5756e2
{
Packit Service a1bd4f
    NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
Packit 5756e2
Packit Service a1bd4f
    return priv->succeeded;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nmt_password_dialog_get_request_id:
Packit 5756e2
 * @dialog: the #NmtPasswordDialog
Packit 5756e2
 *
Packit 5756e2
 * Gets the dialog's request ID.
Packit 5756e2
 *
Packit 5756e2
 * Returns: the dialog's request ID.
Packit 5756e2
 */
Packit 5756e2
const char *
Packit Service a1bd4f
nmt_password_dialog_get_request_id(NmtPasswordDialog *dialog)
Packit 5756e2
{
Packit Service a1bd4f
    NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
Packit 5756e2
Packit Service a1bd4f
    return priv->request_id;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nmt_password_dialog_get_secrets:
Packit 5756e2
 * @dialog: the #NmtPasswordDialog
Packit 5756e2
 *
Packit 5756e2
 * Gets the dialog's secrets array.
Packit 5756e2
 *
Packit 5756e2
 * Returns: (transfer none): the dialog's secrets array.
Packit 5756e2
 */
Packit 5756e2
GPtrArray *
Packit Service a1bd4f
nmt_password_dialog_get_secrets(NmtPasswordDialog *dialog)
Packit 5756e2
{
Packit Service a1bd4f
    NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
Packit 5756e2
Packit Service a1bd4f
    return priv->secrets;
Packit 5756e2
}