2020-12-23 22:21:36 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2014-10-30 15:45:43 +01:00
|
|
|
/*
|
2019-10-01 09:20:35 +02:00
|
|
|
* Copyright (C) 2014 Red Hat, Inc.
|
2014-10-30 15:45:43 +01:00
|
|
|
*/
|
|
|
|
|
|
2021-02-19 15:23:34 +01:00
|
|
|
#include "libnm-client-aux-extern/nm-default-client.h"
|
2014-10-30 15:45:43 +01:00
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <readline/readline.h>
|
|
|
|
|
#include <readline/history.h>
|
|
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
#include "utils.h"
|
2021-03-01 12:42:48 +01:00
|
|
|
#include "libnmc-base/nm-secret-agent-simple.h"
|
2014-10-30 15:45:43 +01:00
|
|
|
#include "polkit-agent.h"
|
2021-03-01 12:42:48 +01:00
|
|
|
#include "libnmc-base/nm-polkit-listener.h"
|
2014-10-30 15:45:43 +01:00
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
usage(void)
|
|
|
|
|
{
|
|
|
|
|
g_printerr(_("Usage: nmcli agent { COMMAND | help }\n\n"
|
|
|
|
|
"COMMAND := { secret | polkit | all }\n\n"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
usage_agent_secret(void)
|
|
|
|
|
{
|
|
|
|
|
g_printerr(_("Usage: nmcli agent secret { help }\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Runs nmcli as NetworkManager secret agent. When NetworkManager requires\n"
|
|
|
|
|
"a password it asks registered agents for it. This command keeps nmcli running\n"
|
|
|
|
|
"and if a password is required asks the user for it.\n\n"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
usage_agent_polkit(void)
|
|
|
|
|
{
|
|
|
|
|
g_printerr(_("Usage: nmcli agent polkit { help }\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Registers nmcli as a polkit action for the user session.\n"
|
|
|
|
|
"When a polkit daemon requires an authorization, nmcli asks the user and gives\n"
|
2014-11-20 12:33:49 +01:00
|
|
|
"the response back to polkit.\n\n"));
|
2014-10-30 15:45:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
usage_agent_all(void)
|
|
|
|
|
{
|
|
|
|
|
g_printerr(_("Usage: nmcli agent all { help }\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Runs nmcli as both NetworkManager secret and a polkit agent.\n\n"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* for pre-filling a string to readline prompt */
|
|
|
|
|
static char *pre_input_deftext;
|
|
|
|
|
static int
|
|
|
|
|
set_deftext(void)
|
|
|
|
|
{
|
|
|
|
|
if (pre_input_deftext && rl_startup_hook) {
|
|
|
|
|
rl_insert_text(pre_input_deftext);
|
|
|
|
|
g_free(pre_input_deftext);
|
|
|
|
|
pre_input_deftext = NULL;
|
|
|
|
|
rl_startup_hook = NULL;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2018-10-09 11:50:26 +02:00
|
|
|
get_secrets_from_user(const NmcConfig *nmc_config,
|
|
|
|
|
const char * request_id,
|
2014-10-30 15:45:43 +01:00
|
|
|
const char * title,
|
|
|
|
|
const char * msg,
|
|
|
|
|
GPtrArray * secrets)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2014-10-30 15:45:43 +01:00
|
|
|
for (i = 0; i < secrets->len; i++) {
|
|
|
|
|
NMSecretAgentSimpleSecret *secret = secrets->pdata[i];
|
|
|
|
|
char * pwd = NULL;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2014-10-30 15:45:43 +01:00
|
|
|
/* Ask user for the password */
|
2016-09-09 21:45:45 +02:00
|
|
|
if (msg)
|
|
|
|
|
g_print("%s\n", msg);
|
2014-10-30 15:45:43 +01:00
|
|
|
if (secret->value) {
|
|
|
|
|
/* Prefill the password if we have it. */
|
|
|
|
|
rl_startup_hook = set_deftext;
|
|
|
|
|
pre_input_deftext = g_strdup(secret->value);
|
|
|
|
|
}
|
2019-01-30 12:34:55 +01:00
|
|
|
if (secret->no_prompt_entry_id)
|
|
|
|
|
pwd = nmc_readline(nmc_config, "%s: ", secret->pretty_name);
|
|
|
|
|
else
|
|
|
|
|
pwd = nmc_readline(nmc_config, "%s (%s): ", secret->pretty_name, secret->entry_id);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2014-10-30 15:45:43 +01:00
|
|
|
/* No password provided, cancel the secrets. */
|
|
|
|
|
if (!pwd)
|
|
|
|
|
return FALSE;
|
|
|
|
|
g_free(secret->value);
|
|
|
|
|
secret->value = pwd;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
secrets_requested(NMSecretAgentSimple *agent,
|
|
|
|
|
const char * request_id,
|
|
|
|
|
const char * title,
|
|
|
|
|
const char * msg,
|
|
|
|
|
GPtrArray * secrets,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2018-10-09 11:50:26 +02:00
|
|
|
NmCli * nmc = user_data;
|
|
|
|
|
gboolean success;
|
2014-10-30 15:45:43 +01:00
|
|
|
|
2017-03-30 12:45:41 +02:00
|
|
|
if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
|
2014-10-30 15:45:43 +01:00
|
|
|
nmc_terminal_erase_line();
|
|
|
|
|
|
2018-10-09 11:50:26 +02:00
|
|
|
success = get_secrets_from_user(&nmc->nmc_config, request_id, title, msg, secrets);
|
|
|
|
|
nm_secret_agent_simple_response(agent, request_id, success ? secrets : NULL);
|
2014-10-30 15:45:43 +01:00
|
|
|
}
|
|
|
|
|
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
static void
|
2020-04-05 13:05:58 +02:00
|
|
|
do_agent_secret(const NMCCommand *cmd, NmCli *nmc, int argc, const char *const *argv)
|
2014-10-30 15:45:43 +01:00
|
|
|
{
|
2017-03-30 16:09:46 +02:00
|
|
|
next_arg(nmc, &argc, &argv, NULL);
|
2016-07-06 16:00:44 +02:00
|
|
|
if (nmc->complete)
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
return;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2014-10-30 15:45:43 +01:00
|
|
|
/* Create secret agent */
|
2014-11-13 20:22:04 +01:00
|
|
|
nmc->secret_agent = nm_secret_agent_simple_new("nmcli-agent");
|
2014-10-30 15:45:43 +01:00
|
|
|
if (nmc->secret_agent) {
|
|
|
|
|
/* We keep running */
|
2015-03-27 12:39:17 +01:00
|
|
|
nmc->should_wait++;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2019-01-22 12:51:10 +01:00
|
|
|
nm_secret_agent_simple_enable(nmc->secret_agent, NULL);
|
2016-09-09 17:33:09 +02:00
|
|
|
g_signal_connect(nmc->secret_agent,
|
|
|
|
|
NM_SECRET_AGENT_SIMPLE_REQUEST_SECRETS,
|
|
|
|
|
G_CALLBACK(secrets_requested),
|
|
|
|
|
nmc);
|
2014-10-30 15:45:43 +01:00
|
|
|
g_print(_("nmcli successfully registered as a NetworkManager's secret agent.\n"));
|
|
|
|
|
} else {
|
|
|
|
|
g_string_printf(nmc->return_text, _("Error: secret agent initialization failed"));
|
|
|
|
|
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 16:42:05 +01:00
|
|
|
static void
|
|
|
|
|
polkit_registered(gpointer instance, gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
g_print(_("nmcli successfully registered as a polkit agent.\n"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
polkit_error(gpointer instance, const char *error, gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
g_main_loop_quit(loop);
|
|
|
|
|
}
|
|
|
|
|
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
static void
|
2020-04-05 13:05:58 +02:00
|
|
|
do_agent_polkit(const NMCCommand *cmd, NmCli *nmc, int argc, const char *const *argv)
|
2014-10-30 15:45:43 +01:00
|
|
|
{
|
2019-12-17 16:42:05 +01:00
|
|
|
gs_free_error GError *error = NULL;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-03-30 16:09:46 +02:00
|
|
|
next_arg(nmc, &argc, &argv, NULL);
|
2016-07-06 16:00:44 +02:00
|
|
|
if (nmc->complete)
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
return;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2014-10-30 15:45:43 +01:00
|
|
|
if (!nmc_polkit_agent_init(nmc, TRUE, &error)) {
|
|
|
|
|
g_dbus_error_strip_remote_error(error);
|
2019-12-17 16:42:05 +01:00
|
|
|
g_string_printf(nmc->return_text,
|
|
|
|
|
_("Error: polkit agent initialization failed: %s"),
|
2014-10-30 15:45:43 +01:00
|
|
|
error->message);
|
|
|
|
|
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
|
|
|
|
} else {
|
|
|
|
|
/* We keep running */
|
2015-03-27 12:39:17 +01:00
|
|
|
nmc->should_wait++;
|
2019-12-17 16:42:05 +01:00
|
|
|
g_signal_connect(nmc->pk_listener,
|
|
|
|
|
NM_POLKIT_LISTENER_SIGNAL_ERROR,
|
|
|
|
|
G_CALLBACK(polkit_error),
|
|
|
|
|
NULL);
|
|
|
|
|
g_signal_connect(nmc->pk_listener,
|
|
|
|
|
NM_POLKIT_LISTENER_SIGNAL_REGISTERED,
|
|
|
|
|
G_CALLBACK(polkit_registered),
|
|
|
|
|
NULL);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2019-12-17 16:42:05 +01:00
|
|
|
/* keep running */
|
|
|
|
|
nmc->should_wait++;
|
2014-10-30 15:45:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
static void
|
2020-04-05 13:05:58 +02:00
|
|
|
do_agent_all(const NMCCommand *cmd, NmCli *nmc, int argc, const char *const *argv)
|
2014-10-30 15:45:43 +01:00
|
|
|
{
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
NMCResultCode r;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2017-03-30 16:09:46 +02:00
|
|
|
next_arg(nmc, &argc, &argv, NULL);
|
2016-07-06 16:00:44 +02:00
|
|
|
if (nmc->complete)
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
return;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2014-10-30 15:45:43 +01:00
|
|
|
/* Run both secret and polkit agent */
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
do_agent_secret(cmd, nmc, argc, argv);
|
|
|
|
|
r = nmc->return_value;
|
|
|
|
|
if (r != NMC_RESULT_SUCCESS) {
|
2014-10-30 15:45:43 +01:00
|
|
|
g_printerr("%s\n", nmc->return_text->str);
|
2018-01-23 14:49:15 +01:00
|
|
|
g_string_truncate(nmc->return_text, 0);
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
nmc->return_value = NMC_RESULT_SUCCESS;
|
2018-01-23 14:49:15 +01:00
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
do_agent_polkit(cmd, nmc, argc, argv);
|
2018-01-23 14:49:15 +01:00
|
|
|
if (nmc->return_value != NMC_RESULT_SUCCESS) {
|
|
|
|
|
g_printerr("%s\n", nmc->return_text->str);
|
|
|
|
|
g_string_truncate(nmc->return_text, 0);
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
if (r != NMC_RESULT_SUCCESS)
|
|
|
|
|
nmc->return_value = r;
|
2014-10-30 15:45:43 +01:00
|
|
|
}
|
|
|
|
|
|
cli: remove redundant return value from NMCCommand funcs
Many func implementations are asynchronous, that means, they
cannot return right away. Instead, they record the return value
in nmc->result_value.
The return value from the command functions was thus redundant.
In the best case, the return value agrees with the cached result
in nmc->result_value, in which it was unnecessary. In the worst case,
they disagree, and overwrite each other.
nmc->result_value is state. Tracking state is hard, and there should
be fewer places where the state gets mutated. Also, the rules how that
happened should be clearer. Drop the redundant, conflicting mechanism.
2020-04-05 15:36:30 +02:00
|
|
|
void
|
2020-04-05 13:05:58 +02:00
|
|
|
nmc_command_func_agent(const NMCCommand *cmd, NmCli *nmc, int argc, const char *const *argv)
|
2014-10-30 15:45:43 +01:00
|
|
|
{
|
2020-04-05 11:47:07 +02:00
|
|
|
static const NMCCommand cmds[] = {
|
|
|
|
|
{"secret", do_agent_secret, usage_agent_secret, TRUE, TRUE},
|
|
|
|
|
{"polkit", do_agent_polkit, usage_agent_polkit, TRUE, TRUE},
|
|
|
|
|
{"all", do_agent_all, usage_agent_all, TRUE, TRUE},
|
|
|
|
|
{NULL, do_agent_all, usage, TRUE, TRUE},
|
|
|
|
|
};
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-05 11:47:07 +02:00
|
|
|
next_arg(nmc, &argc, &argv, NULL);
|
|
|
|
|
nmc_do_cmd(nmc, cmds, *argv, argc, argv);
|
2014-10-30 15:45:43 +01:00
|
|
|
}
|