1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "bus-internal.h"
4 #include "bus-message.h"
5 #include "bus-polkit.h"
10 static int check_good_user(sd_bus_message
*m
, uid_t good_user
) {
11 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
17 if (good_user
== UID_INVALID
)
20 r
= sd_bus_query_sender_creds(m
, SD_BUS_CREDS_EUID
, &creds
);
24 /* Don't trust augmented credentials for authorization */
25 assert_return((sd_bus_creds_get_augmented_mask(creds
) & SD_BUS_CREDS_EUID
) == 0, -EPERM
);
27 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
31 return sender_uid
== good_user
;
35 static int bus_message_append_strv_key_value(sd_bus_message
*m
, const char **l
) {
40 r
= sd_bus_message_open_container(m
, 'a', "{ss}");
44 STRV_FOREACH_PAIR(k
, v
, l
) {
45 r
= sd_bus_message_append(m
, "{ss}", *k
, *v
);
50 r
= sd_bus_message_close_container(m
);
57 static int bus_message_new_polkit_auth_call(
62 sd_bus_message
**ret
) {
64 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
72 sender
= sd_bus_message_get_sender(m
);
76 r
= sd_bus_message_new_method_call(
79 "org.freedesktop.PolicyKit1",
80 "/org/freedesktop/PolicyKit1/Authority",
81 "org.freedesktop.PolicyKit1.Authority",
82 "CheckAuthorization");
86 r
= sd_bus_message_append(c
, "(sa{sv})s", "system-bus-name", 1, "name", "s", sender
, action
);
90 r
= bus_message_append_strv_key_value(c
, details
);
94 r
= sd_bus_message_append(c
, "us", interactive
, NULL
);
104 sd_bus_message
*call
,
107 const char **details
,
110 sd_bus_error
*ret_error
) {
117 /* Tests non-interactively! */
119 r
= check_good_user(call
, good_user
);
123 r
= sd_bus_query_sender_privilege(call
, capability
);
130 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*request
= NULL
, *reply
= NULL
;
131 int authorized
= false, challenge
= false;
133 r
= bus_message_new_polkit_auth_call(call
, action
, details
, /* interactive = */ false, &request
);
137 r
= sd_bus_call(call
->bus
, request
, 0, ret_error
, &reply
);
139 /* Treat no PK available as access denied */
140 if (bus_error_is_unknown_service(ret_error
)) {
141 sd_bus_error_free(ret_error
);
148 r
= sd_bus_message_enter_container(reply
, 'r', "bba{ss}");
152 r
= sd_bus_message_read(reply
, "bb", &authorized
, &challenge
);
160 *_challenge
= challenge
;
170 typedef struct AsyncPolkitQuery
{
174 sd_bus_message
*request
, *reply
;
178 sd_event_source
*defer_event_source
;
181 static AsyncPolkitQuery
*async_polkit_query_free(AsyncPolkitQuery
*q
) {
185 sd_bus_slot_unref(q
->slot
);
187 if (q
->registry
&& q
->request
)
188 hashmap_remove(q
->registry
, q
->request
);
190 sd_bus_message_unref(q
->request
);
191 sd_bus_message_unref(q
->reply
);
194 strv_free(q
->details
);
196 sd_event_source_disable_unref(q
->defer_event_source
);
201 DEFINE_TRIVIAL_CLEANUP_FUNC(AsyncPolkitQuery
*, async_polkit_query_free
);
203 static int async_polkit_defer(sd_event_source
*s
, void *userdata
) {
204 AsyncPolkitQuery
*q
= ASSERT_PTR(userdata
);
208 /* This is called as idle event source after we processed the async polkit reply, hopefully after the
209 * method call we re-enqueued has been properly processed. */
211 async_polkit_query_free(q
);
215 static int async_polkit_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
216 AsyncPolkitQuery
*q
= ASSERT_PTR(userdata
);
222 q
->slot
= sd_bus_slot_unref(q
->slot
);
225 q
->reply
= sd_bus_message_ref(reply
);
227 /* Now, let's dispatch the original message a second time be re-enqueing. This will then traverse the
228 * whole message processing again, and thus re-validating and re-retrieving the "userdata" field
231 * We install an idle event loop event to clean-up the PolicyKit request data when we are idle again,
232 * i.e. after the second time the message is processed is complete. */
234 assert(!q
->defer_event_source
);
235 r
= sd_event_add_defer(sd_bus_get_event(sd_bus_message_get_bus(reply
)), &q
->defer_event_source
, async_polkit_defer
, q
);
239 r
= sd_event_source_set_priority(q
->defer_event_source
, SD_EVENT_PRIORITY_IDLE
);
243 r
= sd_event_source_set_enabled(q
->defer_event_source
, SD_EVENT_ONESHOT
);
247 r
= sd_bus_message_rewind(q
->request
, true);
251 r
= sd_bus_enqueue_for_read(sd_bus_message_get_bus(q
->request
), q
->request
);
258 log_debug_errno(r
, "Processing asynchronous PolicyKit reply failed, ignoring: %m");
259 (void) sd_bus_reply_method_errno(q
->request
, r
, NULL
);
260 async_polkit_query_free(q
);
264 static int process_polkit_response(
266 sd_bus_message
*call
,
268 const char **details
,
269 sd_bus_error
*ret_error
) {
271 int authorized
, challenge
, r
;
281 /* If the operation we want to authenticate changed between the first and the second time,
282 * let's not use this authentication, it might be out of date as the object and context we
283 * operate on might have changed. */
284 if (!streq(q
->action
, action
) || !strv_equal(q
->details
, (char**) details
))
287 if (sd_bus_message_is_method_error(q
->reply
, NULL
)) {
288 const sd_bus_error
*e
;
290 e
= sd_bus_message_get_error(q
->reply
);
292 /* Treat no PK available as access denied */
293 if (bus_error_is_unknown_service(e
))
296 /* Copy error from polkit reply */
297 sd_bus_error_copy(ret_error
, e
);
298 return -sd_bus_error_get_errno(e
);
301 r
= sd_bus_message_enter_container(q
->reply
, 'r', "bba{ss}");
303 r
= sd_bus_message_read(q
->reply
, "bb", &authorized
, &challenge
);
311 return sd_bus_error_set(ret_error
, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
, "Interactive authentication required.");
318 int bus_verify_polkit_async(
319 sd_bus_message
*call
,
322 const char **details
,
326 sd_bus_error
*ret_error
) {
334 r
= check_good_user(call
, good_user
);
339 AsyncPolkitQuery
*q
= hashmap_get(*registry
, call
);
340 /* This is the second invocation of this function, and there's already a response from
341 * polkit, let's process it */
343 return process_polkit_response(q
, call
, action
, details
, ret_error
);
346 r
= sd_bus_query_sender_privilege(call
, capability
);
353 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pk
= NULL
;
354 _cleanup_(async_polkit_query_freep
) AsyncPolkitQuery
*q_new
= NULL
;
356 int c
= sd_bus_message_get_allow_interactive_authorization(call
);
362 r
= hashmap_ensure_allocated(registry
, NULL
);
366 r
= bus_message_new_polkit_auth_call(call
, action
, details
, interactive
, &pk
);
370 q
= q_new
= new(AsyncPolkitQuery
, 1);
374 *q
= (AsyncPolkitQuery
) {
375 .request
= sd_bus_message_ref(call
),
378 q
->action
= strdup(action
);
382 q
->details
= strv_copy((char**) details
);
386 r
= hashmap_put(*registry
, call
, q
);
390 q
->registry
= *registry
;
392 r
= sd_bus_call_async(call
->bus
, &q
->slot
, pk
, async_polkit_callback
, q
, 0);
404 Hashmap
*bus_verify_polkit_async_registry_free(Hashmap
*registry
) {
406 return hashmap_free_with_destructor(registry
, async_polkit_query_free
);
408 assert(hashmap_isempty(registry
));
409 return hashmap_free(registry
);