mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-02-10 02:00:37 +01:00
settings: port AddConnection to NMAuthChain
More code saved. Yay.
This commit is contained in:
parent
670da10764
commit
1bbc624477
1 changed files with 35 additions and 115 deletions
|
|
@ -106,7 +106,6 @@ typedef struct {
|
|||
guint auth_changed_id;
|
||||
char *config_file;
|
||||
|
||||
GSList *pk_calls;
|
||||
GSList *auths;
|
||||
|
||||
GSList *plugins;
|
||||
|
|
@ -734,60 +733,6 @@ remove_default_wired_connection (NMSettings *self,
|
|||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
NMSettings *self;
|
||||
DBusGMethodInvocation *context;
|
||||
PolkitSubject *subject;
|
||||
GCancellable *cancellable;
|
||||
gboolean disposed;
|
||||
|
||||
NMConnection *connection;
|
||||
gpointer callback_data;
|
||||
|
||||
char *hostname;
|
||||
} PolkitCall;
|
||||
|
||||
#include "nm-dbus-manager.h"
|
||||
|
||||
static PolkitCall *
|
||||
polkit_call_new (NMSettings *self,
|
||||
DBusGMethodInvocation *context,
|
||||
NMConnection *connection,
|
||||
const char *hostname)
|
||||
{
|
||||
PolkitCall *call;
|
||||
char *sender;
|
||||
|
||||
g_return_val_if_fail (self != NULL, NULL);
|
||||
g_return_val_if_fail (context != NULL, NULL);
|
||||
|
||||
call = g_malloc0 (sizeof (PolkitCall));
|
||||
call->self = self;
|
||||
call->cancellable = g_cancellable_new ();
|
||||
call->context = context;
|
||||
if (connection)
|
||||
call->connection = g_object_ref (connection);
|
||||
if (hostname)
|
||||
call->hostname = g_strdup (hostname);
|
||||
|
||||
sender = dbus_g_method_get_sender (context);
|
||||
call->subject = polkit_system_bus_name_new (sender);
|
||||
g_free (sender);
|
||||
|
||||
return call;
|
||||
}
|
||||
|
||||
static void
|
||||
polkit_call_free (PolkitCall *call)
|
||||
{
|
||||
if (call->connection)
|
||||
g_object_unref (call->connection);
|
||||
g_object_unref (call->cancellable);
|
||||
g_free (call->hostname);
|
||||
g_object_unref (call->subject);
|
||||
g_free (call);
|
||||
}
|
||||
|
||||
static NMSysconfigConnection *
|
||||
add_new_connection (NMSettings *self,
|
||||
NMConnection *connection,
|
||||
|
|
@ -821,51 +766,43 @@ add_new_connection (NMSettings *self,
|
|||
}
|
||||
|
||||
static void
|
||||
pk_add_cb (GObject *object, GAsyncResult *result, gpointer user_data)
|
||||
pk_add_cb (NMAuthChain *chain,
|
||||
GError *chain_error,
|
||||
DBusGMethodInvocation *context,
|
||||
gpointer user_data)
|
||||
{
|
||||
PolkitCall *call = user_data;
|
||||
NMSettings *self = call->self;
|
||||
NMSettingsPrivate *priv;
|
||||
PolkitAuthorizationResult *pk_result;
|
||||
NMSettings *self = NM_SETTINGS (user_data);
|
||||
NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
|
||||
NMAuthCallResult result;
|
||||
GError *error = NULL, *add_error = NULL;
|
||||
NMConnection *connection;
|
||||
NMSysconfigConnection *added;
|
||||
|
||||
/* If NMSettings is already gone, do nothing */
|
||||
if (call->disposed) {
|
||||
error = g_error_new_literal (NM_SETTINGS_ERROR,
|
||||
NM_SETTINGS_ERROR_GENERAL,
|
||||
"Request was canceled.");
|
||||
dbus_g_method_return_error (call->context, error);
|
||||
g_error_free (error);
|
||||
polkit_call_free (call);
|
||||
return;
|
||||
priv->auths = g_slist_remove (priv->auths, chain);
|
||||
|
||||
/* If our NMSysconfigConnection is already gone, do nothing */
|
||||
if (chain_error) {
|
||||
error = g_error_new (NM_SETTINGS_ERROR,
|
||||
NM_SETTINGS_ERROR_GENERAL,
|
||||
"Error checking authorization: %s",
|
||||
chain_error->message ? chain_error->message : "(unknown)");
|
||||
goto done;
|
||||
}
|
||||
|
||||
priv = NM_SETTINGS_GET_PRIVATE (self);
|
||||
|
||||
priv->pk_calls = g_slist_remove (priv->pk_calls, call);
|
||||
|
||||
pk_result = polkit_authority_check_authorization_finish (priv->authority,
|
||||
result,
|
||||
&error);
|
||||
/* Some random error happened */
|
||||
if (error) {
|
||||
dbus_g_method_return_error (call->context, error);
|
||||
goto out;
|
||||
}
|
||||
result = nm_auth_chain_get_result (chain, NM_AUTH_PERMISSION_SETTINGS_HOSTNAME_MODIFY);
|
||||
|
||||
/* Caller didn't successfully authenticate */
|
||||
if (!polkit_authorization_result_get_is_authorized (pk_result)) {
|
||||
if (result != NM_AUTH_CALL_RESULT_YES) {
|
||||
error = g_error_new_literal (NM_SETTINGS_ERROR,
|
||||
NM_SETTINGS_ERROR_NOT_PRIVILEGED,
|
||||
"Insufficient privileges.");
|
||||
dbus_g_method_return_error (call->context, error);
|
||||
goto out;
|
||||
goto done;
|
||||
}
|
||||
|
||||
added = add_new_connection (self, call->connection, &add_error);
|
||||
connection = nm_auth_chain_get_data (chain, "connection");
|
||||
added = add_new_connection (self, connection, &add_error);
|
||||
if (added)
|
||||
dbus_g_method_return (call->context, nm_connection_get_path (NM_CONNECTION (added)));
|
||||
dbus_g_method_return (context, nm_connection_get_path (NM_CONNECTION (added)));
|
||||
else {
|
||||
error = g_error_new (NM_SETTINGS_ERROR,
|
||||
NM_SETTINGS_ERROR_ADD_FAILED,
|
||||
|
|
@ -873,14 +810,14 @@ pk_add_cb (GObject *object, GAsyncResult *result, gpointer user_data)
|
|||
add_error ? add_error->code : -1,
|
||||
(add_error && add_error->message) ? add_error->message : "(unknown)");
|
||||
g_error_free (add_error);
|
||||
dbus_g_method_return_error (call->context, error);
|
||||
}
|
||||
|
||||
out:
|
||||
done:
|
||||
if (error)
|
||||
dbus_g_method_return_error (context, error);
|
||||
|
||||
g_clear_error (&error);
|
||||
polkit_call_free (call);
|
||||
if (pk_result)
|
||||
g_object_unref (pk_result);
|
||||
nm_auth_chain_unref (chain);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -889,7 +826,7 @@ impl_settings_add_connection (NMSettings *self,
|
|||
DBusGMethodInvocation *context)
|
||||
{
|
||||
NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
|
||||
PolkitCall *call;
|
||||
NMAuthChain *chain;
|
||||
NMConnection *connection;
|
||||
GError *error = NULL;
|
||||
|
||||
|
|
@ -911,19 +848,12 @@ impl_settings_add_connection (NMSettings *self,
|
|||
return;
|
||||
}
|
||||
|
||||
call = polkit_call_new (self, context, connection, NULL);
|
||||
g_assert (call);
|
||||
polkit_authority_check_authorization (priv->authority,
|
||||
call->subject,
|
||||
NM_AUTH_PERMISSION_SETTINGS_CONNECTION_MODIFY,
|
||||
NULL,
|
||||
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
|
||||
call->cancellable,
|
||||
pk_add_cb,
|
||||
call);
|
||||
priv->pk_calls = g_slist_append (priv->pk_calls, call);
|
||||
|
||||
g_object_unref (connection);
|
||||
/* Otherwise validate the user request */
|
||||
chain = nm_auth_chain_new (priv->authority, context, NULL, pk_add_cb, self);
|
||||
g_assert (chain);
|
||||
priv->auths = g_slist_append (priv->auths, chain);
|
||||
nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SETTINGS_CONNECTION_MODIFY, TRUE);
|
||||
nm_auth_chain_set_data (chain, "connection", connection, g_object_unref);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -1382,16 +1312,6 @@ dispose (GObject *object)
|
|||
priv->auth_changed_id = 0;
|
||||
}
|
||||
|
||||
/* Cancel PolicyKit requests */
|
||||
for (iter = priv->pk_calls; iter; iter = g_slist_next (iter)) {
|
||||
PolkitCall *call = iter->data;
|
||||
|
||||
call->disposed = TRUE;
|
||||
g_cancellable_cancel (call->cancellable);
|
||||
}
|
||||
g_slist_free (priv->pk_calls);
|
||||
priv->pk_calls = NULL;
|
||||
|
||||
for (iter = priv->auths; iter; iter = g_slist_next (iter))
|
||||
nm_auth_chain_unref ((NMAuthChain *) iter->data);
|
||||
g_slist_free (priv->auths);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue