| 1 |
/* sysprof-polkit.c |
| 2 |
* |
| 3 |
* Copyright 2023 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "sysprof-polkit-private.h" |
| 24 |
|
| 25 |
static void |
| 26 |
sysprof_polkit_authority_cb (GObject *object, |
| 27 |
GAsyncResult *result, |
| 28 |
gpointer user_data) |
| 29 |
{ |
| 30 |
g_autoptr(PolkitAuthority) authority = NULL; |
| 31 |
g_autoptr(DexPromise) promise = user_data; |
| 32 |
g_autoptr(GError) error = NULL; |
| 33 |
|
| 34 |
g_assert (G_IS_ASYNC_RESULT (result)); |
| 35 |
g_assert (DEX_IS_PROMISE (promise)); |
| 36 |
|
| 37 |
if (!(authority = polkit_authority_get_finish (result, &error))) |
| 38 |
dex_promise_reject (promise, g_steal_pointer (&error)); |
| 39 |
else |
| 40 |
dex_promise_resolve_object (promise, g_steal_pointer (&authority)); |
| 41 |
} |
| 42 |
|
| 43 |
static DexFuture * |
| 44 |
_sysprof_polkit_authority (void) |
| 45 |
{ |
| 46 |
DexPromise *promise = dex_promise_new (); |
| 47 |
|
| 48 |
polkit_authority_get_async (dex_promise_get_cancellable (DEX_PROMISE (promise)), |
| 49 |
sysprof_polkit_authority_cb, |
| 50 |
dex_ref (promise)); |
| 51 |
|
| 52 |
return DEX_FUTURE (promise); |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
sysprof_polkit_check_cb (GObject *object, |
| 57 |
GAsyncResult *result, |
| 58 |
gpointer user_data) |
| 59 |
{ |
| 60 |
PolkitAuthority *authority = (PolkitAuthority *)object; |
| 61 |
g_autoptr(PolkitAuthorizationResult) res = NULL; |
| 62 |
g_autoptr(DexPromise) promise = user_data; |
| 63 |
g_autoptr(GError) error = NULL; |
| 64 |
|
| 65 |
g_assert (POLKIT_IS_AUTHORITY (authority)); |
| 66 |
g_assert (G_IS_ASYNC_RESULT (result)); |
| 67 |
g_assert (DEX_IS_PROMISE (promise)); |
| 68 |
|
| 69 |
if (!(res = polkit_authority_check_authorization_finish (authority, result, &error))) |
| 70 |
dex_promise_reject (promise, g_steal_pointer (&error)); |
| 71 |
else if (!polkit_authorization_result_get_is_authorized (res)) |
| 72 |
dex_promise_reject (promise, |
| 73 |
g_error_new (G_DBUS_ERROR, |
| 74 |
G_DBUS_ERROR_AUTH_FAILED, |
| 75 |
"Failed to authorize user credentials")); |
| 76 |
else |
| 77 |
dex_promise_resolve_boolean (promise, TRUE); |
| 78 |
} |
| 79 |
|
| 80 |
static DexFuture * |
| 81 |
_sysprof_polkit_check (PolkitAuthority *authority, |
| 82 |
PolkitSubject *subject, |
| 83 |
const char *policy, |
| 84 |
PolkitDetails *details, |
| 85 |
PolkitCheckAuthorizationFlags flags) |
| 86 |
{ |
| 87 |
DexPromise *promise = dex_promise_new (); |
| 88 |
|
| 89 |
polkit_authority_check_authorization (authority, |
| 90 |
subject, |
| 91 |
policy, |
| 92 |
details, |
| 93 |
flags, |
| 94 |
dex_promise_get_cancellable (DEX_PROMISE (promise)), |
| 95 |
sysprof_polkit_check_cb, |
| 96 |
dex_ref (promise)); |
| 97 |
|
| 98 |
return DEX_FUTURE (promise); |
| 99 |
} |
| 100 |
|
| 101 |
typedef struct _Authorize |
| 102 |
{ |
| 103 |
GDBusConnection *connection; |
| 104 |
char *policy; |
| 105 |
PolkitDetails *details; |
| 106 |
guint allow_user_interaction : 1; |
| 107 |
} Authorize; |
| 108 |
|
| 109 |
static void |
| 110 |
authorize_free (gpointer data) |
| 111 |
{ |
| 112 |
Authorize *auth = data; |
| 113 |
|
| 114 |
g_clear_pointer (&auth->policy, g_free); |
| 115 |
g_clear_object (&auth->connection); |
| 116 |
g_clear_object (&auth->details); |
| 117 |
g_free (auth); |
| 118 |
} |
| 119 |
|
| 120 |
static Authorize * |
| 121 |
authorize_new (GDBusConnection *connection, |
| 122 |
const char *policy, |
| 123 |
PolkitDetails *details, |
| 124 |
gboolean allow_user_interaction) |
| 125 |
{ |
| 126 |
Authorize *auth; |
| 127 |
|
| 128 |
auth = g_new0 (Authorize, 1); |
| 129 |
g_set_object (&auth->connection, connection); |
| 130 |
g_set_object (&auth->details, details); |
| 131 |
auth->policy = g_strdup (policy); |
| 132 |
auth->allow_user_interaction = !!allow_user_interaction; |
| 133 |
|
| 134 |
return auth; |
| 135 |
} |
| 136 |
|
| 137 |
static DexFuture * |
| 138 |
authorize_fiber (gpointer data) |
| 139 |
{ |
| 140 |
g_autoptr(PolkitAuthority) authority = NULL; |
| 141 |
g_autoptr(PolkitSubject) subject = NULL; |
| 142 |
g_autoptr(GError) error = NULL; |
| 143 |
const char *bus_name; |
| 144 |
Authorize *auth = data; |
| 145 |
guint flags = 0; |
| 146 |
|
| 147 |
g_assert (auth != NULL); |
| 148 |
g_assert (G_IS_DBUS_CONNECTION (auth->connection)); |
| 149 |
g_assert (!auth->details || POLKIT_IS_DETAILS (auth->details)); |
| 150 |
g_assert (auth->policy != NULL); |
| 151 |
|
| 152 |
bus_name = g_dbus_connection_get_unique_name (auth->connection); |
| 153 |
subject = polkit_system_bus_name_new (bus_name); |
| 154 |
|
| 155 |
if (!(authority = dex_await_object (_sysprof_polkit_authority (), &error))) |
| 156 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 157 |
|
| 158 |
if (auth->allow_user_interaction) |
| 159 |
flags |= POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION; |
| 160 |
|
| 161 |
if (!dex_await_boolean (_sysprof_polkit_check (authority, subject, auth->policy, auth->details, flags), &error)) |
| 162 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 163 |
|
| 164 |
return dex_future_new_for_boolean (TRUE); |
| 165 |
} |
| 166 |
|
| 167 |
DexFuture * |
| 168 |
_sysprof_polkit_authorize (GDBusConnection *connection, |
| 169 |
const char *policy, |
| 170 |
PolkitDetails *details, |
| 171 |
gboolean allow_user_interaction) |
| 172 |
{ |
| 173 |
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL); |
| 174 |
g_return_val_if_fail (policy != NULL, NULL); |
| 175 |
g_return_val_if_fail (!details || POLKIT_IS_DETAILS (details), NULL); |
| 176 |
|
| 177 |
return dex_scheduler_spawn (NULL, 0, |
| 178 |
authorize_fiber, |
| 179 |
authorize_new (connection, policy, details, allow_user_interaction), |
| 180 |
authorize_free); |
| 181 |
} |