NetworkManager/src/nmcli/polkit-agent.c
Thomas Haller 615221a99c format: reformat source tree with clang-format 13.0
We use clang-format for automatic formatting of our source files.
Since clang-format is actively maintained software, the actual
formatting depends on the used version of clang-format. That is
unfortunate and painful, but really unavoidable unless clang-format
would be strictly bug-compatible.

So the version that we must use is from the current Fedora release, which
is also tested by our gitlab-ci. Previously, we were using Fedora 34 with
clang-tools-extra-12.0.1-1.fc34.x86_64.

As Fedora 35 comes along, we need to update our formatting as Fedora 35
comes with version "13.0.0~rc1-1.fc35".
An alternative would be to freeze on version 12, but that has different
problems (like, it's cumbersome to rebuild clang 12 on Fedora 35 and it
would be cumbersome for our developers which are on Fedora 35 to use a
clang that they cannot easily install).

The (differently painful) solution is to reformat from time to time, as we
switch to a new Fedora (and thus clang) version.
Usually we would expect that such a reformatting brings minor changes.
But this time, the changes are huge. That is mentioned in the release
notes [1] as

  Makes PointerAligment: Right working with AlignConsecutiveDeclarations. (Fixes https://llvm.org/PR27353)

[1] https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#clang-format
2021-11-29 09:31:09 +00:00

96 lines
2.5 KiB
C

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2014 Red Hat, Inc.
*/
#include "libnm-client-aux-extern/nm-default-client.h"
#include "polkit-agent.h"
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include "libnmc-base/nm-polkit-listener.h"
#include "common.h"
static char *
polkit_read_passwd(gpointer instance,
const char *action_id,
const char *message,
const char *user,
gpointer user_data)
{
NmCli *nmc = user_data;
g_print("%s\n", message);
g_print("(action_id: %s)\n", action_id);
/* Ask user for polkit authorization password */
if (user) {
return nmc_readline_echo(&nmc->nmc_config, FALSE, "password (%s): ", user);
}
return nmc_readline_echo(&nmc->nmc_config, FALSE, "password: ");
}
static void
polkit_error(gpointer instance, const char *error, gpointer user_data)
{
g_printerr(_("Error: polkit agent failed: %s\n"), error);
}
gboolean
nmc_polkit_agent_init(NmCli *nmc, gboolean for_session, GError **error)
{
NMPolkitListener *listener;
GDBusConnection *dbus_connection = NULL;
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
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);
}
g_signal_connect(listener,
NM_POLKIT_LISTENER_SIGNAL_REQUEST_SYNC,
G_CALLBACK(polkit_read_passwd),
nmc);
g_signal_connect(listener, NM_POLKIT_LISTENER_SIGNAL_ERROR, G_CALLBACK(polkit_error), NULL);
nmc->pk_listener = listener;
return TRUE;
}
void
nmc_polkit_agent_fini(NmCli *nmc)
{
if (nmc->pk_listener) {
g_clear_object(&nmc->pk_listener);
}
}
gboolean
nmc_start_polkit_agent_start_try(NmCli *nmc)
{
gs_free_error GError *error = NULL;
/* 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;
}