summaryrefslogtreecommitdiff
path: root/clients/cli/agent.c
blob: d82c2514547310b3a26fadfeef96791577e85b2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2014 Red Hat, Inc.
 */

#include "nm-default.h"

#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>

#include "common.h"
#include "utils.h"
#include "nm-secret-agent-simple.h"
#include "polkit-agent.h"
#include "nm-polkit-listener.h"

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"
	              "the response back to polkit.\n\n"));
}

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
get_secrets_from_user (const NmcConfig *nmc_config,
                       NmcReadlineStatus *readline_status,
                       const char *request_id,
                       const char *title,
                       const char *msg,
                       GPtrArray *secrets)
{
	int i;

	for (i = 0; i < secrets->len; i++) {
		NMSecretAgentSimpleSecret *secret = secrets->pdata[i];
		char *pwd = NULL;

		/* Ask user for the password */
		if (msg)
			g_print ("%s\n", msg);
		if (secret->value) {
			/* Prefill the password if we have it. */
			rl_startup_hook = set_deftext;
			pre_input_deftext = g_strdup (secret->value);
		}
		if (secret->no_prompt_entry_id)
			pwd = nmc_readline (nmc_config, readline_status, "%s: ", secret->pretty_name);
		else
			pwd = nmc_readline (nmc_config, readline_status, "%s (%s): ", secret->pretty_name, secret->entry_id);

		/* 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)
{
	NmCli *nmc = user_data;
	gboolean success;

	if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
		nmc_terminal_erase_line ();

	success = get_secrets_from_user (&nmc->nmc_config, &nmc->readline_status, request_id, title, msg, secrets);
	nm_secret_agent_simple_response (agent,
	                                 request_id,
	                                 success ? secrets : NULL);
}

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)
{
	NmCli *nmc = user_data;

	nmc_run_status_return (&nmc->run_status,
	                       NMC_RESULT_ERROR_UNKNOWN,
	                       _("Error: polkit agent failed: %s"),
	                       error);
}

static void
do_agent (const NMCCommand *cmd,
          NmCli *nmc,
          int argc,
          const char *const*argv)
{
	gs_free_error GError *error = NULL;
	gboolean with_polkit_agent = NM_IN_STRSET (cmd->cmd, NULL, "all", "polkit");
	gboolean with_secret_agent = NM_IN_STRSET (cmd->cmd, NULL, "all", "secret");

	nm_assert (with_polkit_agent || with_secret_agent);

	if (with_polkit_agent) {
		if (!nmc_polkit_agent_init (nmc, TRUE, &error)) {
			g_dbus_error_strip_remote_error (error);
			nmc_run_status_return (&nmc->run_status,
			                       NMC_RESULT_ERROR_UNKNOWN,
			                       _("Error: polkit agent initialization failed: %s"),
			                       error->message);
			return;
		}
	}

	if (with_secret_agent) {
		nmc->secret_agent = nm_secret_agent_simple_new ("nmcli-agent");
		if (!nmc->secret_agent) {
			nmc_run_status_return (&nmc->run_status,
			                       NMC_RESULT_ERROR_UNKNOWN,
			                       _("Error: secret agent initialization failed"));
			return;
		}
	}

	nmc_run_status_wait_push (&nmc->run_status);

	if (with_polkit_agent) {
		g_signal_connect (nmc->pk_listener,
		                  NM_POLKIT_LISTENER_SIGNAL_ERROR,
		                  G_CALLBACK (polkit_error),
		                  nmc);
		g_signal_connect (nmc->pk_listener,
		                  NM_POLKIT_LISTENER_SIGNAL_REGISTERED,
		                  G_CALLBACK (polkit_registered),
		                  nmc);
	}

	if (with_secret_agent) {
		nm_secret_agent_simple_enable (nmc->secret_agent, NULL);
		g_signal_connect (nmc->secret_agent,
		                  NM_SECRET_AGENT_SIMPLE_REQUEST_SECRETS,
		                  G_CALLBACK (secrets_requested),
		                  nmc);
		g_print (_("nmcli successfully registered as a NetworkManager's secret agent.\n"));
	}
}

void
nmc_command_do_agent (const NMCCommand *cmd, NmCli *nmc, int argc, const char *const*argv)
{
	static const NMCCommand agent_cmds[] = {
	    { "secret",  do_agent, usage_agent_secret, TRUE, TRUE },
	    { "polkit",  do_agent, usage_agent_polkit, TRUE, TRUE },
	    { "all",     do_agent, usage_agent_all,    TRUE, TRUE },
	    { NULL,      do_agent, usage,              TRUE, TRUE },
	};

	next_arg (nmc, &argc, &argv, NULL);
	nmc_do_cmd (nmc, agent_cmds, *argv, argc, argv);
}