2010-05-28 18:23:00 -07:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
|
|
|
|
/* NetworkManager -- Network link manager
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2010 Red Hat, Inc.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "nm-manager-auth.h"
|
|
|
|
|
#include "nm-logging.h"
|
|
|
|
|
|
|
|
|
|
#include <dbus/dbus-glib-lowlevel.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
struct NMAuthChain {
|
|
|
|
|
guint32 refcount;
|
|
|
|
|
PolkitAuthority *authority;
|
|
|
|
|
GSList *calls;
|
2010-05-29 23:00:46 -07:00
|
|
|
GHashTable *data;
|
2010-05-28 18:23:00 -07:00
|
|
|
|
|
|
|
|
DBusGMethodInvocation *context;
|
2010-06-03 13:03:07 -07:00
|
|
|
char *owner;
|
2010-05-28 18:23:00 -07:00
|
|
|
GError *error;
|
|
|
|
|
|
|
|
|
|
NMAuthChainResultFunc done_func;
|
|
|
|
|
NMAuthChainCallFunc call_func;
|
|
|
|
|
gpointer user_data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
NMAuthChain *chain;
|
|
|
|
|
GCancellable *cancellable;
|
|
|
|
|
char *permission;
|
|
|
|
|
gboolean disposed;
|
|
|
|
|
} PolkitCall;
|
|
|
|
|
|
2010-05-29 23:00:46 -07:00
|
|
|
typedef struct {
|
|
|
|
|
gpointer data;
|
|
|
|
|
GDestroyNotify destroy;
|
|
|
|
|
} ChainData;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
free_data (gpointer data)
|
|
|
|
|
{
|
|
|
|
|
ChainData *tmp = data;
|
|
|
|
|
|
|
|
|
|
if (tmp->destroy)
|
|
|
|
|
tmp->destroy (tmp->data);
|
|
|
|
|
memset (tmp, 0, sizeof (ChainData));
|
|
|
|
|
g_free (tmp);
|
|
|
|
|
}
|
2010-05-28 18:23:00 -07:00
|
|
|
|
2010-06-02 02:16:14 -07:00
|
|
|
static void
|
|
|
|
|
default_call_func (NMAuthChain *chain,
|
|
|
|
|
const char *permission,
|
|
|
|
|
GError *error,
|
|
|
|
|
NMAuthCallResult result,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
if (!error)
|
|
|
|
|
nm_auth_chain_set_data (chain, permission, GUINT_TO_POINTER (result), NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-25 15:12:32 -05:00
|
|
|
static NMAuthChain *
|
|
|
|
|
_auth_chain_new (PolkitAuthority *authority,
|
|
|
|
|
DBusGMethodInvocation *context,
|
|
|
|
|
DBusGProxy *proxy,
|
|
|
|
|
DBusMessage *message,
|
|
|
|
|
NMAuthChainResultFunc done_func,
|
|
|
|
|
gpointer user_data)
|
2010-05-28 18:23:00 -07:00
|
|
|
{
|
|
|
|
|
NMAuthChain *self;
|
|
|
|
|
|
2010-08-25 15:12:32 -05:00
|
|
|
g_return_val_if_fail (context || proxy || message, NULL);
|
2010-06-03 13:03:07 -07:00
|
|
|
|
2010-05-28 18:23:00 -07:00
|
|
|
self = g_malloc0 (sizeof (NMAuthChain));
|
|
|
|
|
self->refcount = 1;
|
|
|
|
|
self->authority = g_object_ref (authority);
|
2010-05-29 23:00:46 -07:00
|
|
|
self->data = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_data);
|
2010-05-28 18:23:00 -07:00
|
|
|
self->done_func = done_func;
|
2010-06-02 02:16:14 -07:00
|
|
|
self->call_func = /* call_func ? call_func : */ default_call_func;
|
2010-05-28 18:23:00 -07:00
|
|
|
self->user_data = user_data;
|
2010-06-03 13:03:07 -07:00
|
|
|
self->context = context;
|
|
|
|
|
|
|
|
|
|
if (proxy)
|
|
|
|
|
self->owner = g_strdup (dbus_g_proxy_get_bus_name (proxy));
|
|
|
|
|
else if (context)
|
|
|
|
|
self->owner = dbus_g_method_get_sender (context);
|
2010-08-25 15:12:32 -05:00
|
|
|
else if (message)
|
|
|
|
|
self->owner = g_strdup (dbus_message_get_sender (message));
|
2010-06-03 13:03:07 -07:00
|
|
|
|
|
|
|
|
if (!self->owner) {
|
|
|
|
|
/* Need an owner */
|
|
|
|
|
g_warn_if_fail (self->owner);
|
|
|
|
|
nm_auth_chain_unref (self);
|
|
|
|
|
self = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-28 18:23:00 -07:00
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-25 15:12:32 -05:00
|
|
|
NMAuthChain *
|
|
|
|
|
nm_auth_chain_new (PolkitAuthority *authority,
|
|
|
|
|
DBusGMethodInvocation *context,
|
|
|
|
|
DBusGProxy *proxy,
|
|
|
|
|
NMAuthChainResultFunc done_func,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
return _auth_chain_new (authority, context, proxy, NULL, done_func, user_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NMAuthChain *
|
|
|
|
|
nm_auth_chain_new_raw_message (PolkitAuthority *authority,
|
|
|
|
|
DBusMessage *message,
|
|
|
|
|
NMAuthChainResultFunc done_func,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
return _auth_chain_new (authority, NULL, NULL, message, done_func, user_data);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-29 23:00:46 -07:00
|
|
|
gpointer
|
|
|
|
|
nm_auth_chain_get_data (NMAuthChain *self, const char *tag)
|
|
|
|
|
{
|
|
|
|
|
ChainData *tmp;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (self != NULL, NULL);
|
|
|
|
|
g_return_val_if_fail (tag != NULL, NULL);
|
|
|
|
|
|
|
|
|
|
tmp = g_hash_table_lookup (self->data, tag);
|
|
|
|
|
return tmp ? tmp->data : NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nm_auth_chain_set_data (NMAuthChain *self,
|
|
|
|
|
const char *tag,
|
|
|
|
|
gpointer data,
|
|
|
|
|
GDestroyNotify data_destroy)
|
|
|
|
|
{
|
|
|
|
|
ChainData *tmp;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (self != NULL);
|
|
|
|
|
g_return_if_fail (tag != NULL);
|
|
|
|
|
|
|
|
|
|
if (data == NULL)
|
|
|
|
|
g_hash_table_remove (self->data, tag);
|
|
|
|
|
else {
|
|
|
|
|
tmp = g_malloc0 (sizeof (ChainData));
|
|
|
|
|
tmp->data = data;
|
|
|
|
|
tmp->destroy = data_destroy;
|
|
|
|
|
|
|
|
|
|
g_hash_table_insert (self->data, g_strdup (tag), tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-28 18:23:00 -07:00
|
|
|
static void
|
|
|
|
|
nm_auth_chain_check_done (NMAuthChain *self)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (self != NULL);
|
|
|
|
|
|
|
|
|
|
if (g_slist_length (self->calls) == 0) {
|
|
|
|
|
/* Ensure we say alive across the callback */
|
|
|
|
|
self->refcount++;
|
2010-05-29 23:00:46 -07:00
|
|
|
self->done_func (self, self->error, self->context, self->user_data);
|
2010-05-28 18:23:00 -07:00
|
|
|
nm_auth_chain_unref (self);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
polkit_call_cancel (PolkitCall *call)
|
|
|
|
|
{
|
|
|
|
|
call->disposed = TRUE;
|
|
|
|
|
g_cancellable_cancel (call->cancellable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
polkit_call_free (PolkitCall *call)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (call != NULL);
|
|
|
|
|
|
|
|
|
|
call->disposed = TRUE;
|
|
|
|
|
g_free (call->permission);
|
|
|
|
|
call->permission = NULL;
|
|
|
|
|
call->chain = NULL;
|
|
|
|
|
g_object_unref (call->cancellable);
|
|
|
|
|
call->cancellable = NULL;
|
|
|
|
|
g_free (call);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
pk_call_cb (GObject *object, GAsyncResult *result, gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
PolkitCall *call = user_data;
|
|
|
|
|
NMAuthChain *chain;
|
|
|
|
|
PolkitAuthorizationResult *pk_result;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
guint call_result = NM_AUTH_CALL_RESULT_UNKNOWN;
|
|
|
|
|
|
|
|
|
|
/* If the call is already disposed do nothing */
|
|
|
|
|
if (call->disposed) {
|
|
|
|
|
polkit_call_free (call);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chain = call->chain;
|
|
|
|
|
chain->calls = g_slist_remove (chain->calls, call);
|
|
|
|
|
|
|
|
|
|
pk_result = polkit_authority_check_authorization_finish (chain->authority,
|
|
|
|
|
result,
|
|
|
|
|
&error);
|
|
|
|
|
if (error) {
|
|
|
|
|
if (!chain->error)
|
|
|
|
|
chain->error = g_error_copy (error);
|
|
|
|
|
|
|
|
|
|
nm_log_warn (LOGD_CORE, "error requesting auth for %s: (%d) %s",
|
|
|
|
|
call->permission,
|
|
|
|
|
error ? error->code : -1,
|
|
|
|
|
error && error->message ? error->message : "(unknown)");
|
|
|
|
|
} else {
|
|
|
|
|
if (polkit_authorization_result_get_is_authorized (pk_result)) {
|
|
|
|
|
/* Caller has the permission */
|
|
|
|
|
call_result = NM_AUTH_CALL_RESULT_YES;
|
|
|
|
|
} else if (polkit_authorization_result_get_is_challenge (pk_result)) {
|
|
|
|
|
/* Caller could authenticate to get the permission */
|
|
|
|
|
call_result = NM_AUTH_CALL_RESULT_AUTH;
|
|
|
|
|
} else
|
|
|
|
|
call_result = NM_AUTH_CALL_RESULT_NO;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-29 23:00:46 -07:00
|
|
|
chain->call_func (chain, call->permission, error, call_result, chain->user_data);
|
2010-05-28 18:23:00 -07:00
|
|
|
nm_auth_chain_check_done (chain);
|
|
|
|
|
|
|
|
|
|
g_clear_error (&error);
|
|
|
|
|
polkit_call_free (call);
|
|
|
|
|
if (pk_result)
|
|
|
|
|
g_object_unref (pk_result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
nm_auth_chain_add_call (NMAuthChain *self,
|
2010-05-29 23:00:46 -07:00
|
|
|
const char *permission,
|
|
|
|
|
gboolean allow_interaction)
|
2010-05-28 18:23:00 -07:00
|
|
|
{
|
|
|
|
|
PolkitCall *call;
|
|
|
|
|
PolkitSubject *subject;
|
2010-05-29 23:00:46 -07:00
|
|
|
PolkitCheckAuthorizationFlags flags = POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE;
|
2010-05-28 18:23:00 -07:00
|
|
|
|
|
|
|
|
g_return_val_if_fail (self != NULL, FALSE);
|
2010-06-03 13:03:07 -07:00
|
|
|
g_return_val_if_fail (self->owner != NULL, FALSE);
|
2010-05-28 18:23:00 -07:00
|
|
|
g_return_val_if_fail (permission != NULL, FALSE);
|
|
|
|
|
|
2010-06-03 13:03:07 -07:00
|
|
|
subject = polkit_system_bus_name_new (self->owner);
|
2010-05-28 18:23:00 -07:00
|
|
|
if (!subject)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
call = g_malloc0 (sizeof (PolkitCall));
|
|
|
|
|
call->chain = self;
|
|
|
|
|
call->permission = g_strdup (permission);
|
|
|
|
|
call->cancellable = g_cancellable_new ();
|
|
|
|
|
|
|
|
|
|
self->calls = g_slist_append (self->calls, call);
|
|
|
|
|
|
2010-05-29 23:00:46 -07:00
|
|
|
if (allow_interaction)
|
|
|
|
|
flags = POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION;
|
|
|
|
|
|
2010-05-28 18:23:00 -07:00
|
|
|
polkit_authority_check_authorization (self->authority,
|
|
|
|
|
subject,
|
|
|
|
|
permission,
|
|
|
|
|
NULL,
|
2010-05-29 23:00:46 -07:00
|
|
|
flags,
|
2010-05-28 18:23:00 -07:00
|
|
|
call->cancellable,
|
|
|
|
|
pk_call_cb,
|
|
|
|
|
call);
|
|
|
|
|
g_object_unref (subject);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nm_auth_chain_unref (NMAuthChain *self)
|
|
|
|
|
{
|
|
|
|
|
GSList *iter;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (self != NULL);
|
|
|
|
|
|
|
|
|
|
self->refcount--;
|
|
|
|
|
if (self->refcount > 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
g_object_unref (self->authority);
|
2010-06-03 13:03:07 -07:00
|
|
|
g_free (self->owner);
|
2010-05-28 18:23:00 -07:00
|
|
|
|
|
|
|
|
for (iter = self->calls; iter; iter = g_slist_next (iter))
|
|
|
|
|
polkit_call_cancel ((PolkitCall *) iter->data);
|
|
|
|
|
g_slist_free (self->calls);
|
|
|
|
|
|
|
|
|
|
g_clear_error (&self->error);
|
2010-05-29 23:00:46 -07:00
|
|
|
g_hash_table_destroy (self->data);
|
2010-05-28 18:23:00 -07:00
|
|
|
|
|
|
|
|
memset (self, 0, sizeof (NMAuthChain));
|
|
|
|
|
g_free (self);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-30 08:30:37 -07:00
|
|
|
/************ utils **************/
|
|
|
|
|
|
|
|
|
|
gboolean
|
2010-05-31 09:45:26 -07:00
|
|
|
nm_auth_get_caller_uid (DBusGMethodInvocation *context,
|
2010-05-30 08:30:37 -07:00
|
|
|
NMDBusManager *dbus_mgr,
|
2010-05-31 09:45:26 -07:00
|
|
|
gulong *out_uid,
|
2010-05-30 08:30:37 -07:00
|
|
|
const char **out_error_desc)
|
|
|
|
|
{
|
|
|
|
|
DBusConnection *connection;
|
|
|
|
|
char *sender = NULL;
|
|
|
|
|
gboolean success = FALSE;
|
|
|
|
|
DBusError dbus_error;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (context != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (dbus_mgr != NULL, FALSE);
|
2010-05-31 09:45:26 -07:00
|
|
|
g_return_val_if_fail (out_uid != NULL, FALSE);
|
2010-05-30 08:30:37 -07:00
|
|
|
|
2010-05-31 09:45:26 -07:00
|
|
|
*out_uid = G_MAXULONG;
|
2010-05-30 08:30:37 -07:00
|
|
|
|
|
|
|
|
sender = dbus_g_method_get_sender (context);
|
|
|
|
|
if (!sender) {
|
|
|
|
|
if (out_error_desc)
|
|
|
|
|
*out_error_desc = "Could not determine D-Bus requestor";
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connection = nm_dbus_manager_get_dbus_connection (dbus_mgr);
|
|
|
|
|
if (!connection) {
|
|
|
|
|
if (out_error_desc)
|
|
|
|
|
*out_error_desc = "Could not get the D-Bus system bus";
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_error_init (&dbus_error);
|
|
|
|
|
/* FIXME: do this async */
|
2010-05-31 09:45:26 -07:00
|
|
|
*out_uid = dbus_bus_get_unix_user (connection, sender, &dbus_error);
|
2010-05-30 08:30:37 -07:00
|
|
|
if (dbus_error_is_set (&dbus_error)) {
|
|
|
|
|
if (out_error_desc)
|
2010-05-31 09:45:26 -07:00
|
|
|
*out_error_desc = "Could not determine the user ID of the requestor";
|
2010-05-30 08:30:37 -07:00
|
|
|
dbus_error_free (&dbus_error);
|
2010-05-31 09:45:26 -07:00
|
|
|
*out_uid = G_MAXULONG;
|
|
|
|
|
} else
|
|
|
|
|
success = TRUE;
|
2010-05-30 08:30:37 -07:00
|
|
|
|
|
|
|
|
out:
|
2010-05-31 09:45:26 -07:00
|
|
|
g_free (sender);
|
2010-05-30 08:30:37 -07:00
|
|
|
return success;
|
|
|
|
|
}
|