2020-12-23 22:21:36 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2019-09-25 13:13:40 +02:00
|
|
|
/*
|
2010-05-28 18:23:00 -07:00
|
|
|
* Copyright (C) 2010 Red Hat, Inc.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-02-04 18:04:13 +01:00
|
|
|
#include "src/core/nm-default-daemon.h"
|
2014-11-13 10:07:02 -05:00
|
|
|
|
2016-06-01 12:31:36 +02:00
|
|
|
#include "nm-auth-utils.h"
|
|
|
|
|
|
2021-02-18 17:37:47 +01:00
|
|
|
#include "libnm-glib-aux/nm-c-list.h"
|
2011-05-18 22:20:24 -05:00
|
|
|
#include "nm-setting-connection.h"
|
2021-02-12 15:01:09 +01:00
|
|
|
#include "libnm-core-aux-intern/nm-auth-subject.h"
|
2014-08-14 13:34:57 +02:00
|
|
|
#include "nm-auth-manager.h"
|
2014-07-17 17:06:44 -04:00
|
|
|
#include "nm-session-monitor.h"
|
2019-12-19 11:30:38 +01:00
|
|
|
#include "nm-dbus-manager.h"
|
2020-09-24 19:11:40 +02:00
|
|
|
#include "nm-core-utils.h"
|
2010-05-28 18:23:00 -07:00
|
|
|
|
2018-04-04 21:38:38 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2020-04-26 17:41:55 +02:00
|
|
|
typedef struct {
|
2021-11-09 13:28:54 +01:00
|
|
|
const char *tag;
|
2020-04-26 17:41:55 +02:00
|
|
|
gpointer data;
|
|
|
|
|
GDestroyNotify destroy;
|
|
|
|
|
} ChainData;
|
|
|
|
|
|
2019-12-23 10:54:16 +01:00
|
|
|
struct _NMAuthChain {
|
2019-05-26 18:49:55 +02:00
|
|
|
CList parent_lst;
|
|
|
|
|
|
|
|
|
|
ChainData *data_arr;
|
|
|
|
|
guint data_len;
|
2020-04-26 17:41:55 +02:00
|
|
|
guint data_alloc;
|
2019-05-26 18:49:55 +02:00
|
|
|
|
2020-04-26 17:41:55 +02:00
|
|
|
CList auth_call_lst_head;
|
2010-05-28 18:23:00 -07:00
|
|
|
|
2018-04-04 21:38:38 +02:00
|
|
|
GDBusMethodInvocation *context;
|
2021-11-09 13:28:54 +01:00
|
|
|
NMAuthSubject *subject;
|
2018-04-04 21:38:38 +02:00
|
|
|
|
2015-04-16 12:34:55 -04:00
|
|
|
GCancellable *cancellable;
|
2010-05-28 18:23:00 -07:00
|
|
|
|
2020-04-26 15:40:40 +02:00
|
|
|
/* if set, it also means that the chain is already started and was cancelled. */
|
|
|
|
|
GSource *cancellable_idle_source;
|
|
|
|
|
|
|
|
|
|
NMAuthChainResultFunc done_func;
|
|
|
|
|
gpointer user_data;
|
|
|
|
|
|
2010-05-28 18:23:00 -07:00
|
|
|
gulong cancellable_id;
|
2018-04-04 21:38:38 +02:00
|
|
|
|
2020-04-26 15:40:40 +02:00
|
|
|
guint num_pending_auth_calls;
|
|
|
|
|
|
|
|
|
|
bool is_started : 1;
|
2019-05-02 10:33:53 +02:00
|
|
|
bool is_destroyed : 1;
|
|
|
|
|
bool is_finishing : 1;
|
2010-05-28 18:23:00 -07:00
|
|
|
};
|
|
|
|
|
|
2019-05-26 18:49:55 +02:00
|
|
|
G_STATIC_ASSERT(G_STRUCT_OFFSET(NMAuthChain, parent_lst) == 0);
|
|
|
|
|
|
2010-05-28 18:23:00 -07:00
|
|
|
typedef struct {
|
2018-04-04 21:38:38 +02:00
|
|
|
CList auth_call_lst;
|
2021-11-09 13:28:54 +01:00
|
|
|
NMAuthChain *chain;
|
2018-04-09 13:27:03 +02:00
|
|
|
NMAuthManagerCallId *call_id;
|
2021-11-09 13:28:54 +01:00
|
|
|
const char *permission;
|
2019-05-04 09:33:26 +02:00
|
|
|
NMAuthCallResult result;
|
2011-05-20 10:07:26 -05:00
|
|
|
} AuthCall;
|
2010-05-28 18:23:00 -07:00
|
|
|
|
2018-04-04 21:38:38 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2019-05-02 10:33:53 +02:00
|
|
|
static void _auth_chain_destroy(NMAuthChain *self);
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2018-04-05 09:24:22 +02:00
|
|
|
static void
|
|
|
|
|
_ASSERT_call(AuthCall *call)
|
|
|
|
|
{
|
|
|
|
|
nm_assert(call);
|
|
|
|
|
nm_assert(call->chain);
|
2019-05-04 10:31:18 +02:00
|
|
|
nm_assert(call->permission && strlen(call->permission) > 0);
|
2018-04-05 09:24:22 +02:00
|
|
|
nm_assert(nm_c_list_contains_entry(&call->chain->auth_call_lst_head, call, auth_call_lst));
|
2019-05-04 09:33:26 +02:00
|
|
|
#if NM_MORE_ASSERTS > 5
|
|
|
|
|
{
|
|
|
|
|
AuthCall *auth_call;
|
|
|
|
|
guint n = 0;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2019-05-04 09:33:26 +02:00
|
|
|
c_list_for_each_entry (auth_call, &call->chain->auth_call_lst_head, auth_call_lst) {
|
|
|
|
|
nm_assert(auth_call->result == NM_AUTH_CALL_RESULT_UNKNOWN || !auth_call->call_id);
|
|
|
|
|
if (auth_call->call_id)
|
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
nm_assert(n == call->chain->num_pending_auth_calls);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2018-04-05 09:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2020-04-26 15:40:40 +02:00
|
|
|
static void
|
|
|
|
|
_done_and_destroy(NMAuthChain *self)
|
|
|
|
|
{
|
|
|
|
|
self->is_finishing = TRUE;
|
|
|
|
|
self->done_func(self, self->context, self->user_data);
|
|
|
|
|
nm_assert(self->is_finishing);
|
|
|
|
|
_auth_chain_destroy(self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
_cancellable_idle_cb(gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
NMAuthChain *self = user_data;
|
2021-11-09 13:28:54 +01:00
|
|
|
AuthCall *call;
|
2020-04-26 15:40:40 +02:00
|
|
|
|
|
|
|
|
nm_assert(g_cancellable_is_cancelled(self->cancellable));
|
|
|
|
|
nm_assert(self->cancellable_idle_source);
|
|
|
|
|
|
|
|
|
|
c_list_for_each_entry (call, &self->auth_call_lst_head, auth_call_lst) {
|
|
|
|
|
if (call->call_id) {
|
|
|
|
|
self->num_pending_auth_calls--;
|
|
|
|
|
nm_auth_manager_check_authorization_cancel(g_steal_pointer(&call->call_id));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_done_and_destroy(self);
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_cancellable_on_idle(NMAuthChain *self)
|
|
|
|
|
{
|
2021-06-24 11:39:27 +02:00
|
|
|
if (!self->cancellable_idle_source)
|
|
|
|
|
self->cancellable_idle_source = nm_g_idle_add_source(_cancellable_idle_cb, self);
|
2020-04-26 15:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GCancellable *
|
|
|
|
|
nm_auth_chain_get_cancellable(NMAuthChain *self)
|
|
|
|
|
{
|
|
|
|
|
return self->cancellable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_cancellable_cancelled(GCancellable *cancellable, NMAuthChain *self)
|
|
|
|
|
{
|
|
|
|
|
_cancellable_on_idle(self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nm_auth_chain_set_cancellable(NMAuthChain *self, GCancellable *cancellable)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail(self);
|
|
|
|
|
g_return_if_fail(G_IS_CANCELLABLE(cancellable));
|
|
|
|
|
|
|
|
|
|
/* after the chain is started, the cancellable can no longer be changed.
|
|
|
|
|
* No need to handle the complexity of swapping the cancellable *after*
|
|
|
|
|
* requests are already started. */
|
|
|
|
|
g_return_if_fail(!self->is_started);
|
|
|
|
|
nm_assert(c_list_is_empty(&self->auth_call_lst_head));
|
|
|
|
|
|
|
|
|
|
/* also no need to allow setting different cancellables. */
|
|
|
|
|
g_return_if_fail(!self->cancellable);
|
|
|
|
|
|
|
|
|
|
self->cancellable = g_object_ref(cancellable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2018-04-05 09:07:12 +02:00
|
|
|
static void
|
|
|
|
|
auth_call_free(AuthCall *call)
|
|
|
|
|
{
|
2019-05-04 09:33:26 +02:00
|
|
|
_ASSERT_call(call);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-04-05 09:07:12 +02:00
|
|
|
c_list_unlink_stale(&call->auth_call_lst);
|
2019-05-04 09:33:26 +02:00
|
|
|
if (call->call_id) {
|
|
|
|
|
call->chain->num_pending_auth_calls--;
|
|
|
|
|
nm_auth_manager_check_authorization_cancel(call->call_id);
|
|
|
|
|
}
|
2020-04-26 15:40:40 +02:00
|
|
|
nm_g_slice_free(call);
|
2018-04-05 09:07:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-04 09:33:26 +02:00
|
|
|
static AuthCall *
|
|
|
|
|
_find_auth_call(NMAuthChain *self, const char *permission)
|
|
|
|
|
{
|
|
|
|
|
AuthCall *auth_call;
|
|
|
|
|
|
|
|
|
|
c_list_for_each_entry (auth_call, &self->auth_call_lst_head, auth_call_lst) {
|
|
|
|
|
if (nm_streq(auth_call->permission, permission))
|
|
|
|
|
return auth_call;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 09:07:12 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2019-05-02 11:37:49 +02:00
|
|
|
static ChainData *
|
2015-08-31 16:37:55 +02:00
|
|
|
_get_data(NMAuthChain *self, const char *tag)
|
2010-05-29 23:00:46 -07:00
|
|
|
{
|
2020-04-26 17:41:55 +02:00
|
|
|
guint i;
|
2019-05-02 11:21:22 +02:00
|
|
|
|
2020-04-26 17:41:55 +02:00
|
|
|
for (i = 0; i < self->data_len; i++) {
|
|
|
|
|
ChainData *chain_data = &self->data_arr[i];
|
|
|
|
|
|
|
|
|
|
if (chain_data->tag && nm_streq(chain_data->tag, tag))
|
2019-05-02 11:21:22 +02:00
|
|
|
return chain_data;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
2015-08-31 16:37:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gpointer
|
|
|
|
|
nm_auth_chain_get_data(NMAuthChain *self, const char *tag)
|
|
|
|
|
{
|
2019-05-02 11:37:49 +02:00
|
|
|
ChainData *chain_data;
|
|
|
|
|
|
2018-04-05 09:33:52 +02:00
|
|
|
g_return_val_if_fail(self, NULL);
|
|
|
|
|
g_return_val_if_fail(tag, NULL);
|
2010-05-29 23:00:46 -07:00
|
|
|
|
2019-05-02 11:37:49 +02:00
|
|
|
chain_data = _get_data(self, tag);
|
|
|
|
|
return chain_data ? chain_data->data : NULL;
|
2010-05-29 23:00:46 -07:00
|
|
|
}
|
|
|
|
|
|
2011-07-01 14:18:46 -05:00
|
|
|
/**
|
|
|
|
|
* nm_auth_chain_steal_data:
|
|
|
|
|
* @self: A #NMAuthChain.
|
|
|
|
|
* @tag: A "tag" uniquely identifying the data to steal.
|
|
|
|
|
*
|
2018-09-15 07:20:54 -04:00
|
|
|
* Removes the datum associated with @tag from the chain's data associations,
|
2011-07-01 14:18:46 -05:00
|
|
|
* without invoking the association's destroy handler. The caller assumes
|
|
|
|
|
* ownership over the returned value.
|
|
|
|
|
*
|
|
|
|
|
* Returns: the datum originally associated with @tag
|
|
|
|
|
*/
|
|
|
|
|
gpointer
|
|
|
|
|
nm_auth_chain_steal_data(NMAuthChain *self, const char *tag)
|
|
|
|
|
{
|
2019-05-02 11:37:49 +02:00
|
|
|
ChainData *chain_data;
|
2011-07-01 14:18:46 -05:00
|
|
|
|
2018-04-05 09:33:52 +02:00
|
|
|
g_return_val_if_fail(self, NULL);
|
|
|
|
|
g_return_val_if_fail(tag, NULL);
|
2011-07-01 14:18:46 -05:00
|
|
|
|
2019-05-02 11:37:49 +02:00
|
|
|
chain_data = _get_data(self, tag);
|
|
|
|
|
if (!chain_data)
|
2018-04-05 09:43:45 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
2020-04-26 17:41:55 +02:00
|
|
|
/* Make sure the destroy handler isn't called when freeing.
|
|
|
|
|
*
|
|
|
|
|
* We don't bother to really remove the element from the array.
|
|
|
|
|
* Just mark the entry as unused by clearing the tag. */
|
2019-05-02 11:37:49 +02:00
|
|
|
chain_data->destroy = NULL;
|
2020-04-26 17:41:55 +02:00
|
|
|
chain_data->tag = NULL;
|
|
|
|
|
return chain_data->data;
|
2011-07-01 14:18:46 -05:00
|
|
|
}
|
|
|
|
|
|
2019-05-06 10:04:16 +02:00
|
|
|
/**
|
|
|
|
|
* nm_auth_chain_set_data_unsafe:
|
|
|
|
|
* @self: the #NMAuthChain
|
|
|
|
|
* @tag: the tag for referencing the attached data.
|
|
|
|
|
* @data: the data to attach. If %NULL, this call has no effect
|
|
|
|
|
* and nothing is attached.
|
|
|
|
|
* @data_destroy: (allow-none): the destroy function for the data pointer.
|
|
|
|
|
*
|
2020-04-26 17:41:55 +02:00
|
|
|
* @tag string is not cloned and must outlive @self. That is why
|
2019-05-06 10:04:16 +02:00
|
|
|
* the function is "unsafe". Use nm_auth_chain_set_data() with a C literal
|
|
|
|
|
* instead.
|
|
|
|
|
*
|
|
|
|
|
* It is a bug to add the same tag more than once.
|
|
|
|
|
*/
|
2010-05-29 23:00:46 -07:00
|
|
|
void
|
2021-11-09 13:28:54 +01:00
|
|
|
nm_auth_chain_set_data_unsafe(NMAuthChain *self,
|
|
|
|
|
const char *tag,
|
2019-05-06 10:04:16 +02:00
|
|
|
gpointer data,
|
|
|
|
|
GDestroyNotify data_destroy)
|
2010-05-29 23:00:46 -07:00
|
|
|
{
|
2019-05-02 11:21:22 +02:00
|
|
|
ChainData *chain_data;
|
|
|
|
|
|
2018-04-05 09:33:52 +02:00
|
|
|
g_return_if_fail(self);
|
|
|
|
|
g_return_if_fail(tag);
|
2010-05-29 23:00:46 -07:00
|
|
|
|
2020-07-01 17:20:40 -04:00
|
|
|
/* The tag must not yet exist. Otherwise, we'd have to first search the
|
2020-04-26 17:41:55 +02:00
|
|
|
* list for an existing entry. That usage pattern is not supported. */
|
2019-05-04 10:07:21 +02:00
|
|
|
nm_assert(!_get_data(self, tag));
|
|
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
|
/* we don't track user data of %NULL.
|
|
|
|
|
*
|
|
|
|
|
* In the past this had also the meaning of removing a user-data. But since
|
|
|
|
|
* nm_auth_chain_set_data() does not allow being called more than once
|
|
|
|
|
* for the same tag, we don't need to remove anything. */
|
2019-05-02 11:21:22 +02:00
|
|
|
return;
|
2019-05-02 11:37:49 +02:00
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-26 17:41:55 +02:00
|
|
|
if (self->data_len + 1 > self->data_alloc) {
|
|
|
|
|
if (self->data_alloc == 0)
|
|
|
|
|
self->data_alloc = 8;
|
|
|
|
|
else
|
|
|
|
|
self->data_alloc *= 2;
|
|
|
|
|
self->data_arr = g_realloc(self->data_arr, sizeof(self->data_arr[0]) * self->data_alloc);
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-26 17:41:55 +02:00
|
|
|
chain_data = &self->data_arr[self->data_len++];
|
2019-05-06 10:04:16 +02:00
|
|
|
*chain_data = (ChainData){
|
|
|
|
|
.tag = tag,
|
|
|
|
|
.data = data,
|
|
|
|
|
.destroy = data_destroy,
|
|
|
|
|
};
|
2010-05-29 23:00:46 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-05 09:07:12 +02:00
|
|
|
/*****************************************************************************/
|
2015-07-15 15:42:50 +02:00
|
|
|
|
2010-11-17 16:56:34 -06:00
|
|
|
NMAuthCallResult
|
|
|
|
|
nm_auth_chain_get_result(NMAuthChain *self, const char *permission)
|
|
|
|
|
{
|
2019-05-04 09:33:26 +02:00
|
|
|
AuthCall *auth_call;
|
2015-08-31 16:37:55 +02:00
|
|
|
|
2018-04-05 09:33:52 +02:00
|
|
|
g_return_val_if_fail(self, NM_AUTH_CALL_RESULT_UNKNOWN);
|
|
|
|
|
g_return_val_if_fail(permission, NM_AUTH_CALL_RESULT_UNKNOWN);
|
2010-11-17 16:56:34 -06:00
|
|
|
|
2019-05-04 09:33:26 +02:00
|
|
|
/* it is a bug to request the result other than from the done_func()
|
|
|
|
|
* callback. You are not supposed to poll for the result but request
|
|
|
|
|
* it upon notification. */
|
|
|
|
|
nm_assert(self->is_finishing);
|
|
|
|
|
|
|
|
|
|
auth_call = _find_auth_call(self, permission);
|
|
|
|
|
|
|
|
|
|
/* it is a bug to request a permission result that was not
|
|
|
|
|
* previously requested or which did not complete yet. */
|
|
|
|
|
if (!auth_call)
|
|
|
|
|
g_return_val_if_reached(NM_AUTH_CALL_RESULT_UNKNOWN);
|
|
|
|
|
|
|
|
|
|
nm_assert(!auth_call->call_id);
|
|
|
|
|
|
2020-04-26 15:40:40 +02:00
|
|
|
if (self->cancellable_idle_source) {
|
|
|
|
|
/* already cancelled. We always return unknown (even if we happen to
|
|
|
|
|
* have already received the response. */
|
|
|
|
|
return NM_AUTH_CALL_RESULT_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 09:33:26 +02:00
|
|
|
return auth_call->result;
|
2011-05-18 22:20:24 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-05 09:07:12 +02:00
|
|
|
NMAuthSubject *
|
|
|
|
|
nm_auth_chain_get_subject(NMAuthChain *self)
|
2010-05-28 18:23:00 -07:00
|
|
|
{
|
2018-04-05 09:33:52 +02:00
|
|
|
g_return_val_if_fail(self, NULL);
|
2018-04-05 09:07:12 +02:00
|
|
|
|
|
|
|
|
return self->subject;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-23 10:54:16 +01:00
|
|
|
GDBusMethodInvocation *
|
|
|
|
|
nm_auth_chain_get_context(NMAuthChain *self)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail(self, NULL);
|
|
|
|
|
|
|
|
|
|
return self->context;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 09:07:12 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2010-05-28 18:23:00 -07:00
|
|
|
static void
|
2021-11-09 13:28:54 +01:00
|
|
|
pk_call_cb(NMAuthManager *auth_manager,
|
2018-04-09 13:27:03 +02:00
|
|
|
NMAuthManagerCallId *call_id,
|
|
|
|
|
gboolean is_authorized,
|
|
|
|
|
gboolean is_challenge,
|
2021-11-09 13:28:54 +01:00
|
|
|
GError *error,
|
2018-04-09 13:27:03 +02:00
|
|
|
gpointer user_data)
|
2010-05-28 18:23:00 -07:00
|
|
|
{
|
2019-05-04 09:33:26 +02:00
|
|
|
NMAuthChain *self;
|
2021-11-09 13:28:54 +01:00
|
|
|
AuthCall *call;
|
2019-05-04 09:33:26 +02:00
|
|
|
|
|
|
|
|
nm_assert(call_id);
|
2018-04-09 13:27:03 +02:00
|
|
|
|
|
|
|
|
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
|
2010-05-28 18:23:00 -07:00
|
|
|
return;
|
|
|
|
|
|
2018-04-04 21:38:38 +02:00
|
|
|
call = user_data;
|
|
|
|
|
|
2019-05-04 09:33:26 +02:00
|
|
|
_ASSERT_call(call);
|
2018-04-09 13:27:03 +02:00
|
|
|
nm_assert(call->call_id == call_id);
|
2019-05-04 09:33:26 +02:00
|
|
|
nm_assert(call->result == NM_AUTH_CALL_RESULT_UNKNOWN);
|
|
|
|
|
|
|
|
|
|
self = call->chain;
|
|
|
|
|
|
|
|
|
|
nm_assert(!self->is_destroyed);
|
|
|
|
|
nm_assert(!self->is_finishing);
|
2018-04-09 13:27:03 +02:00
|
|
|
|
|
|
|
|
call->call_id = NULL;
|
2018-04-04 21:38:38 +02:00
|
|
|
|
2019-05-04 09:33:26 +02:00
|
|
|
call->result = nm_auth_call_result_eval(is_authorized, is_challenge, error);
|
2018-04-09 17:03:58 +02:00
|
|
|
|
2019-05-04 09:33:26 +02:00
|
|
|
call->chain->num_pending_auth_calls--;
|
2016-11-11 17:39:03 +01:00
|
|
|
|
2019-05-04 09:33:26 +02:00
|
|
|
_ASSERT_call(call);
|
|
|
|
|
|
|
|
|
|
if (call->chain->num_pending_auth_calls == 0) {
|
|
|
|
|
/* we are on an idle-handler or a clean call-stack (non-reentrant) so it's safe
|
|
|
|
|
* to invoke the callback right away. */
|
2020-04-26 15:40:40 +02:00
|
|
|
_done_and_destroy(self);
|
2019-05-04 09:33:26 +02:00
|
|
|
}
|
2011-05-18 22:20:24 -05:00
|
|
|
}
|
|
|
|
|
|
2019-05-04 10:31:18 +02:00
|
|
|
/**
|
|
|
|
|
* nm_auth_chain_add_call_unsafe:
|
|
|
|
|
* @self: the #NMAuthChain
|
|
|
|
|
* @permission: the permission string. This string is kept by reference
|
|
|
|
|
* and you must make sure that it's lifetime lasts until the NMAuthChain
|
|
|
|
|
* gets destroyed. That's why the function is "unsafe". Use
|
|
|
|
|
* nm_auth_chain_add_call() instead.
|
|
|
|
|
* @allow_interaction: flag
|
|
|
|
|
*
|
|
|
|
|
* It's "unsafe" because @permission is not copied. It's the callers responsibility
|
|
|
|
|
* that the permission string stays valid as long as NMAuthChain.
|
|
|
|
|
*
|
|
|
|
|
* If you can, use nm_auth_chain_add_call() instead!
|
|
|
|
|
*
|
|
|
|
|
* If you have a non-static string, you may attach the permission string as
|
|
|
|
|
* user-data via nm_auth_chain_set_data().
|
|
|
|
|
*/
|
2014-08-14 13:34:57 +02:00
|
|
|
void
|
2019-05-04 10:31:18 +02:00
|
|
|
nm_auth_chain_add_call_unsafe(NMAuthChain *self, const char *permission, gboolean allow_interaction)
|
2012-10-08 12:52:15 -05:00
|
|
|
{
|
|
|
|
|
AuthCall *call;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-04-04 21:38:38 +02:00
|
|
|
g_return_if_fail(self);
|
2014-08-14 13:34:57 +02:00
|
|
|
g_return_if_fail(self->subject);
|
2019-05-02 10:33:53 +02:00
|
|
|
g_return_if_fail(!self->is_finishing);
|
|
|
|
|
g_return_if_fail(!self->is_destroyed);
|
2018-04-09 12:36:06 +02:00
|
|
|
g_return_if_fail(permission && *permission);
|
2020-04-26 15:40:40 +02:00
|
|
|
nm_assert(NM_IN_SET(nm_auth_subject_get_subject_type(self->subject),
|
|
|
|
|
NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS,
|
|
|
|
|
NM_AUTH_SUBJECT_TYPE_INTERNAL));
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2019-05-04 09:33:26 +02:00
|
|
|
/* duplicate permissions are not supported, also because nm_auth_chain_get_result()
|
|
|
|
|
* can only return one-permission. */
|
|
|
|
|
nm_assert(!_find_auth_call(self, permission));
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-26 15:40:40 +02:00
|
|
|
if (!self->is_started) {
|
|
|
|
|
self->is_started = TRUE;
|
|
|
|
|
nm_assert(!self->cancellable_id);
|
|
|
|
|
if (self->cancellable) {
|
|
|
|
|
if (g_cancellable_is_cancelled(self->cancellable)) {
|
|
|
|
|
/* the operation is already cancelled. Schedule the callback on idle. */
|
|
|
|
|
_cancellable_on_idle(self);
|
|
|
|
|
} else {
|
|
|
|
|
self->cancellable_id = g_signal_connect(self->cancellable,
|
|
|
|
|
"cancelled",
|
|
|
|
|
G_CALLBACK(_cancellable_cancelled),
|
|
|
|
|
self);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-26 15:40:40 +02:00
|
|
|
call = g_slice_new(AuthCall);
|
2019-05-04 10:31:18 +02:00
|
|
|
*call = (AuthCall){
|
|
|
|
|
.chain = self,
|
|
|
|
|
.call_id = NULL,
|
|
|
|
|
.result = NM_AUTH_CALL_RESULT_UNKNOWN,
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-04-26 15:40:40 +02:00
|
|
|
/* we don't clone the permission string. It's the callers responsibility. */
|
2019-05-04 10:31:18 +02:00
|
|
|
.permission = permission,
|
|
|
|
|
};
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2019-05-04 09:33:26 +02:00
|
|
|
/* above we assert that no duplicate permissions are added. Still, track the
|
|
|
|
|
* new request to the front of the list so that it would shadow an earlier
|
|
|
|
|
* call. */
|
|
|
|
|
c_list_link_front(&self->auth_call_lst_head, &call->auth_call_lst);
|
|
|
|
|
|
2020-04-26 15:40:40 +02:00
|
|
|
if (self->cancellable_idle_source) {
|
|
|
|
|
/* already cancelled. No need to actually start the request. */
|
|
|
|
|
nm_assert(call->result == NM_AUTH_CALL_RESULT_UNKNOWN);
|
|
|
|
|
} else {
|
|
|
|
|
call->call_id = nm_auth_manager_check_authorization(nm_auth_manager_get(),
|
|
|
|
|
self->subject,
|
|
|
|
|
permission,
|
|
|
|
|
allow_interaction,
|
|
|
|
|
pk_call_cb,
|
|
|
|
|
call);
|
|
|
|
|
|
|
|
|
|
self->num_pending_auth_calls++;
|
|
|
|
|
}
|
2019-05-04 09:33:26 +02:00
|
|
|
|
|
|
|
|
_ASSERT_call(call);
|
|
|
|
|
|
|
|
|
|
/* we track auth-calls in a linked list. If we end up requesting too many permissions this
|
|
|
|
|
* becomes inefficient. If that ever happens, consider a more efficient data structure for
|
|
|
|
|
* a large number of requests. */
|
2020-04-26 17:41:55 +02:00
|
|
|
nm_assert(c_list_length(&self->auth_call_lst_head) < 25);
|
|
|
|
|
G_STATIC_ASSERT_EXPR(NM_CLIENT_PERMISSION_LAST < 25);
|
2010-05-28 18:23:00 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-05 09:07:12 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/* Creates the NMAuthSubject automatically */
|
|
|
|
|
NMAuthChain *
|
|
|
|
|
nm_auth_chain_new_context(GDBusMethodInvocation *context,
|
|
|
|
|
NMAuthChainResultFunc done_func,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
NMAuthSubject *subject;
|
2021-11-09 13:28:54 +01:00
|
|
|
NMAuthChain *chain;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-04-05 09:33:52 +02:00
|
|
|
g_return_val_if_fail(context, NULL);
|
2019-05-02 10:33:53 +02:00
|
|
|
nm_assert(done_func);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2019-12-19 11:30:38 +01:00
|
|
|
subject = nm_dbus_manager_new_auth_subject_from_context(context);
|
2018-04-05 09:07:12 +02:00
|
|
|
if (!subject)
|
|
|
|
|
return NULL;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-04-05 09:07:12 +02:00
|
|
|
chain = nm_auth_chain_new_subject(subject, context, done_func, user_data);
|
|
|
|
|
g_object_unref(subject);
|
|
|
|
|
return chain;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NMAuthChain *
|
2021-11-09 13:28:54 +01:00
|
|
|
nm_auth_chain_new_subject(NMAuthSubject *subject,
|
2018-04-05 09:07:12 +02:00
|
|
|
GDBusMethodInvocation *context,
|
|
|
|
|
NMAuthChainResultFunc done_func,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
NMAuthChain *self;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-04-05 09:07:12 +02:00
|
|
|
g_return_val_if_fail(NM_IS_AUTH_SUBJECT(subject), NULL);
|
2020-04-26 15:40:40 +02:00
|
|
|
nm_assert(NM_IN_SET(nm_auth_subject_get_subject_type(subject),
|
|
|
|
|
NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS,
|
|
|
|
|
NM_AUTH_SUBJECT_TYPE_INTERNAL));
|
2019-05-02 10:33:53 +02:00
|
|
|
nm_assert(done_func);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2019-05-02 10:33:53 +02:00
|
|
|
self = g_slice_new(NMAuthChain);
|
|
|
|
|
*self = (NMAuthChain){
|
|
|
|
|
.done_func = done_func,
|
|
|
|
|
.user_data = user_data,
|
|
|
|
|
.context = nm_g_object_ref(context),
|
|
|
|
|
.subject = g_object_ref(subject),
|
2019-05-26 18:49:55 +02:00
|
|
|
.parent_lst = C_LIST_INIT(self->parent_lst),
|
2019-05-02 10:33:53 +02:00
|
|
|
.auth_call_lst_head = C_LIST_INIT(self->auth_call_lst_head),
|
|
|
|
|
};
|
2018-04-05 09:07:12 +02:00
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 16:37:55 +02:00
|
|
|
/**
|
2018-04-09 11:52:55 +02:00
|
|
|
* nm_auth_chain_destroy:
|
2015-08-31 16:37:55 +02:00
|
|
|
* @self: the auth-chain
|
|
|
|
|
*
|
2018-04-09 11:52:55 +02:00
|
|
|
* Destroys the auth-chain. By destroying the auth-chain, you also cancel
|
2015-08-31 16:37:55 +02:00
|
|
|
* the receipt of the done-callback. IOW, the callback will not be invoked.
|
|
|
|
|
*
|
2019-05-02 10:08:09 +02:00
|
|
|
* The only exception is, you may call nm_auth_chain_destroy() from inside
|
|
|
|
|
* the callback. In this case the call has no effect and @self stays alive
|
|
|
|
|
* until the callback returns.
|
2018-04-09 11:52:55 +02:00
|
|
|
*
|
|
|
|
|
* Note that you might only destroy an auth-chain exactly once, and never
|
2019-05-02 10:08:09 +02:00
|
|
|
* after the callback was handled. After the callback returns, the auth chain
|
|
|
|
|
* always gets automatically destroyed. So you only need to explicitly destroy
|
|
|
|
|
* it, if you want to abort it before the callback complets.
|
2015-08-31 16:37:55 +02:00
|
|
|
*/
|
2010-05-28 18:23:00 -07:00
|
|
|
void
|
2018-04-09 11:52:55 +02:00
|
|
|
nm_auth_chain_destroy(NMAuthChain *self)
|
2010-05-28 18:23:00 -07:00
|
|
|
{
|
2018-04-04 21:38:38 +02:00
|
|
|
g_return_if_fail(self);
|
2019-05-02 10:33:53 +02:00
|
|
|
g_return_if_fail(!self->is_destroyed);
|
|
|
|
|
|
|
|
|
|
self->is_destroyed = TRUE;
|
2010-05-28 18:23:00 -07:00
|
|
|
|
2019-05-02 10:33:53 +02:00
|
|
|
if (self->is_finishing) {
|
|
|
|
|
/* we are called from inside the callback. Keep the instance alive for the moment. */
|
2010-05-28 18:23:00 -07:00
|
|
|
return;
|
2019-05-02 10:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_auth_chain_destroy(self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_auth_chain_destroy(NMAuthChain *self)
|
|
|
|
|
{
|
|
|
|
|
AuthCall *call;
|
2010-05-28 18:23:00 -07:00
|
|
|
|
2019-05-26 18:49:55 +02:00
|
|
|
c_list_unlink(&self->parent_lst);
|
|
|
|
|
|
2018-04-04 21:38:38 +02:00
|
|
|
nm_clear_g_object(&self->subject);
|
|
|
|
|
nm_clear_g_object(&self->context);
|
2015-08-31 16:37:55 +02:00
|
|
|
|
2020-04-26 15:40:40 +02:00
|
|
|
nm_clear_g_signal_handler(self->cancellable, &self->cancellable_id);
|
|
|
|
|
nm_clear_g_source_inst(&self->cancellable_idle_source);
|
|
|
|
|
|
2020-04-26 17:41:55 +02:00
|
|
|
/* we must first destroy all AuthCall instances before ChainData. The reason is
|
2019-05-04 10:31:18 +02:00
|
|
|
* that AuthData.permission is not cloned and the lifetime of the string must
|
|
|
|
|
* be ensured by the caller. A sensible thing to do for the caller is attach the
|
|
|
|
|
* permission string via nm_auth_chain_set_data(). Hence, first free the AuthCall. */
|
2018-04-04 21:38:38 +02:00
|
|
|
while ((call = c_list_first_entry(&self->auth_call_lst_head, AuthCall, auth_call_lst)))
|
|
|
|
|
auth_call_free(call);
|
2010-05-28 18:23:00 -07:00
|
|
|
|
2020-04-26 17:41:55 +02:00
|
|
|
while (self->data_len > 0) {
|
|
|
|
|
ChainData *chain_data = &self->data_arr[--self->data_len];
|
|
|
|
|
|
|
|
|
|
if (chain_data->destroy)
|
|
|
|
|
chain_data->destroy(chain_data->data);
|
|
|
|
|
}
|
|
|
|
|
g_free(self->data_arr);
|
2010-05-28 18:23:00 -07:00
|
|
|
|
2020-04-26 15:40:40 +02:00
|
|
|
nm_g_object_unref(self->cancellable);
|
|
|
|
|
|
|
|
|
|
nm_g_slice_free(self);
|
2010-05-28 18:23:00 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-04 21:38:38 +02:00
|
|
|
/******************************************************************************
|
|
|
|
|
* utils
|
|
|
|
|
*****************************************************************************/
|
2010-05-30 08:30:37 -07:00
|
|
|
|
2010-11-18 13:49:47 -06:00
|
|
|
gboolean
|
2014-08-14 13:34:57 +02:00
|
|
|
nm_auth_is_subject_in_acl(NMConnection *connection, NMAuthSubject *subject, char **out_error_desc)
|
2010-11-18 13:49:47 -06:00
|
|
|
{
|
|
|
|
|
NMSettingConnection *s_con;
|
2021-11-09 13:28:54 +01:00
|
|
|
gs_free char *user = NULL;
|
2014-08-14 13:34:57 +02:00
|
|
|
gulong uid;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2018-04-05 09:33:52 +02:00
|
|
|
g_return_val_if_fail(connection, FALSE);
|
2014-08-14 13:34:57 +02:00
|
|
|
g_return_val_if_fail(NM_IS_AUTH_SUBJECT(subject), FALSE);
|
2020-04-26 15:40:40 +02:00
|
|
|
nm_assert(NM_IN_SET(nm_auth_subject_get_subject_type(subject),
|
|
|
|
|
NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS,
|
|
|
|
|
NM_AUTH_SUBJECT_TYPE_INTERNAL));
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2019-12-17 20:36:18 +01:00
|
|
|
if (nm_auth_subject_get_subject_type(subject) == NM_AUTH_SUBJECT_TYPE_INTERNAL)
|
2014-08-14 13:34:57 +02:00
|
|
|
return TRUE;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
uid = nm_auth_subject_get_unix_process_uid(subject);
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2012-12-16 11:38:04 -06:00
|
|
|
/* Root gets a free pass */
|
|
|
|
|
if (0 == uid)
|
|
|
|
|
return TRUE;
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2020-09-24 19:49:25 +02:00
|
|
|
user = nm_utils_uid_to_name(uid);
|
|
|
|
|
if (!user) {
|
2018-04-12 09:34:40 +02:00
|
|
|
NM_SET_OUT(out_error_desc,
|
|
|
|
|
g_strdup_printf("Could not determine username for uid %lu", uid));
|
2010-11-18 13:49:47 -06:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
2020-09-28 16:03:33 +02:00
|
|
|
|
2014-01-28 08:39:11 -05:00
|
|
|
s_con = nm_connection_get_setting_connection(connection);
|
|
|
|
|
if (!s_con) {
|
|
|
|
|
/* This can only happen when called from AddAndActivate, so we know
|
|
|
|
|
* the user will be authorized when the connection is completed.
|
|
|
|
|
*/
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-18 13:49:47 -06:00
|
|
|
/* Match the username returned by the session check to a user in the ACL */
|
|
|
|
|
if (!nm_setting_connection_permissions_user_allowed(s_con, user)) {
|
2018-04-12 09:34:40 +02:00
|
|
|
NM_SET_OUT(out_error_desc,
|
|
|
|
|
g_strdup_printf("uid %lu has no permission to perform this operation", uid));
|
2010-11-18 13:49:47 -06:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2018-04-12 09:34:40 +02:00
|
|
|
|
|
|
|
|
gboolean
|
2021-11-09 13:28:54 +01:00
|
|
|
nm_auth_is_subject_in_acl_set_error(NMConnection *connection,
|
2018-04-12 09:34:40 +02:00
|
|
|
NMAuthSubject *subject,
|
|
|
|
|
GQuark err_domain,
|
|
|
|
|
int err_code,
|
2021-11-09 13:28:54 +01:00
|
|
|
GError **error)
|
2018-04-12 09:34:40 +02:00
|
|
|
{
|
|
|
|
|
char *error_desc = NULL;
|
|
|
|
|
|
|
|
|
|
nm_assert(!error || !*error);
|
|
|
|
|
|
|
|
|
|
if (nm_auth_is_subject_in_acl(connection, subject, error ? &error_desc : NULL))
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
g_set_error_literal(error, err_domain, err_code, error_desc);
|
|
|
|
|
g_free(error_desc);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2020-09-24 19:11:40 +02:00
|
|
|
|
|
|
|
|
gboolean
|
2021-11-09 13:28:54 +01:00
|
|
|
nm_auth_is_invocation_in_acl_set_error(NMConnection *connection,
|
2020-09-24 19:11:40 +02:00
|
|
|
GDBusMethodInvocation *invocation,
|
|
|
|
|
GQuark err_domain,
|
|
|
|
|
int err_code,
|
2021-11-09 13:28:54 +01:00
|
|
|
NMAuthSubject **out_subject,
|
|
|
|
|
GError **error)
|
2020-09-24 19:11:40 +02:00
|
|
|
{
|
|
|
|
|
gs_unref_object NMAuthSubject *subject = NULL;
|
|
|
|
|
gboolean success;
|
|
|
|
|
|
|
|
|
|
nm_assert(!out_subject || !*out_subject);
|
|
|
|
|
|
|
|
|
|
subject = nm_dbus_manager_new_auth_subject_from_context(invocation);
|
|
|
|
|
if (!subject) {
|
|
|
|
|
g_set_error_literal(error, err_domain, err_code, NM_UTILS_ERROR_MSG_REQ_UID_UKNOWN);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
success = nm_auth_is_subject_in_acl_set_error(connection, subject, err_domain, err_code, error);
|
|
|
|
|
|
|
|
|
|
NM_SET_OUT(out_subject, g_steal_pointer(&subject));
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|