2019-09-10 11:19:01 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2019-09-25 13:13:40 +02:00
|
|
|
/*
|
2019-10-01 09:20:35 +02:00
|
|
|
* Copyright (C) 2014 Red Hat, Inc.
|
2014-10-30 11:25:55 +01:00
|
|
|
*/
|
|
|
|
|
|
2016-02-19 14:57:48 +01:00
|
|
|
#include "nm-default.h"
|
2014-10-30 11:25:55 +01:00
|
|
|
|
2017-04-04 13:52:13 +02:00
|
|
|
#include "polkit-agent.h"
|
|
|
|
|
|
2014-10-30 11:25:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include "nm-polkit-listener.h"
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
|
|
static char *
|
2019-12-17 16:42:05 +01:00
|
|
|
polkit_read_passwd (gpointer instance,
|
|
|
|
|
const char *action_id,
|
|
|
|
|
const char *message,
|
|
|
|
|
const char *user,
|
|
|
|
|
gpointer user_data)
|
2014-10-30 11:25:55 +01:00
|
|
|
{
|
2018-10-09 11:50:26 +02:00
|
|
|
NmCli *nmc = user_data;
|
2014-10-30 11:25:55 +01:00
|
|
|
|
|
|
|
|
g_print ("%s\n", message);
|
|
|
|
|
g_print ("(action_id: %s)\n", action_id);
|
|
|
|
|
|
|
|
|
|
/* Ask user for polkit authorization password */
|
|
|
|
|
if (user) {
|
2019-12-17 16:42:05 +01:00
|
|
|
return nmc_readline_echo (&nmc->nmc_config, FALSE, "password (%s): ", user);
|
2018-10-09 11:50:26 +02:00
|
|
|
}
|
2019-12-17 16:42:05 +01:00
|
|
|
return nmc_readline_echo (&nmc->nmc_config, FALSE, "password: ");
|
2014-10-30 11:25:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2019-12-17 16:42:05 +01:00
|
|
|
polkit_error (gpointer instance,
|
|
|
|
|
const char *error,
|
|
|
|
|
gpointer user_data)
|
2014-10-30 11:25:55 +01:00
|
|
|
{
|
2019-12-17 16:42:05 +01:00
|
|
|
g_printerr (_("Error: polkit agent failed: %s\n"), error);
|
2014-10-30 11:25:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
nmc_polkit_agent_init (NmCli* nmc, gboolean for_session, GError **error)
|
|
|
|
|
{
|
2018-04-06 23:54:33 +02:00
|
|
|
NMPolkitListener *listener;
|
2019-12-17 16:42:05 +01:00
|
|
|
GDBusConnection *dbus_connection = NULL;
|
2014-10-30 11:25:55 +01:00
|
|
|
|
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
2019-12-17 16:42:05 +01:00
|
|
|
if (nmc->client && nm_client_get_dbus_connection (nmc->client)) {
|
|
|
|
|
dbus_connection = nm_client_get_dbus_connection (nmc->client);
|
|
|
|
|
listener = nm_polkit_listener_new (dbus_connection, for_session);
|
|
|
|
|
} else {
|
|
|
|
|
dbus_connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM,
|
|
|
|
|
NULL,
|
|
|
|
|
error);
|
|
|
|
|
|
|
|
|
|
if (!dbus_connection) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listener = nm_polkit_listener_new (dbus_connection, for_session);
|
|
|
|
|
g_object_unref (dbus_connection);
|
|
|
|
|
}
|
2014-10-30 11:25:55 +01:00
|
|
|
|
2019-12-17 16:42:05 +01:00
|
|
|
g_signal_connect (listener,
|
2020-04-06 12:06:45 +02:00
|
|
|
NM_POLKIT_LISTENER_SIGNAL_REQUEST_SYNC,
|
2019-12-17 16:42:05 +01:00
|
|
|
G_CALLBACK (polkit_read_passwd),
|
|
|
|
|
nmc);
|
|
|
|
|
g_signal_connect (listener,
|
|
|
|
|
NM_POLKIT_LISTENER_SIGNAL_ERROR,
|
|
|
|
|
G_CALLBACK (polkit_error),
|
|
|
|
|
NULL);
|
2014-10-30 11:25:55 +01:00
|
|
|
|
2018-04-06 23:54:33 +02:00
|
|
|
nmc->pk_listener = listener;
|
2014-10-30 11:25:55 +01:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nmc_polkit_agent_fini (NmCli* nmc)
|
|
|
|
|
{
|
2018-04-06 23:54:33 +02:00
|
|
|
if (nmc->pk_listener) {
|
|
|
|
|
g_clear_object (&nmc->pk_listener);
|
|
|
|
|
}
|
2014-10-30 11:25:55 +01:00
|
|
|
}
|
|
|
|
|
|
2014-10-30 15:45:43 +01:00
|
|
|
gboolean
|
|
|
|
|
nmc_start_polkit_agent_start_try (NmCli *nmc)
|
|
|
|
|
{
|
2019-12-17 16:42:05 +01:00
|
|
|
gs_free_error GError *error = NULL;
|
2014-10-30 15:45:43 +01:00
|
|
|
|
|
|
|
|
/* We don't register polkit agent at all when running non-interactively */
|
|
|
|
|
if (!nmc->ask)
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
if (!nmc_polkit_agent_init (nmc, FALSE, &error)) {
|
|
|
|
|
g_printerr (_("Warning: polkit agent initialization failed: %s\n"),
|
|
|
|
|
error->message);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|