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_process_reply(sd_bus_message
*reply
, AsyncPolkitQuery
*q
) {
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 static int async_polkit_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
259 AsyncPolkitQuery
*q
= ASSERT_PTR(userdata
);
264 r
= async_polkit_process_reply(reply
, q
);
266 log_debug_errno(r
, "Processing asynchronous PolicyKit reply failed, ignoring: %m");
267 (void) sd_bus_reply_method_errno(q
->request
, r
, NULL
);
268 async_polkit_query_free(q
);
273 static int process_polkit_response(
275 sd_bus_message
*call
,
277 const char **details
,
278 sd_bus_error
*ret_error
) {
280 int authorized
, challenge
, r
;
290 /* If the operation we want to authenticate changed between the first and the second time,
291 * let's not use this authentication, it might be out of date as the object and context we
292 * operate on might have changed. */
293 if (!streq(q
->action
, action
) || !strv_equal(q
->details
, (char**) details
))
296 if (sd_bus_message_is_method_error(q
->reply
, NULL
)) {
297 const sd_bus_error
*e
;
299 e
= sd_bus_message_get_error(q
->reply
);
301 /* Treat no PK available as access denied */
302 if (bus_error_is_unknown_service(e
))
305 /* Copy error from polkit reply */
306 sd_bus_error_copy(ret_error
, e
);
307 return -sd_bus_error_get_errno(e
);
310 r
= sd_bus_message_enter_container(q
->reply
, 'r', "bba{ss}");
312 r
= sd_bus_message_read(q
->reply
, "bb", &authorized
, &challenge
);
320 return sd_bus_error_set(ret_error
, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
, "Interactive authentication required.");
327 /* bus_verify_polkit_async() handles verification of D-Bus calls with polkit. Because the polkit API
328 * is asynchronous, the whole thing is a bit complex and requires some support in the code that uses
329 * it. It relies on sd-bus's support for interrupting the processing of a message.
333 * * bus_verify_polkit_async() must be called before any changes to internal state.
334 * * If bus_verify_polkit_async() has made a new polkit query (signaled by return value 0),
335 * processing of the message should be interrupted. This is done by returning 1--which sd-bus
336 * handles specially--and is usually accompanied by a comment. (The message will be queued for
337 * processing again later when a reply from polkit is received.)
338 * * The code needs to keep a hashmap, here called registry, in which bus_verify_polkit_async()
339 * stores active queries. This hashmap's lifetime must be larger than the method handler's;
340 * e.g., it can be a member of some "manager" object or a global variable.
344 * * 0 - a new polkit call has been made, which means the processing of the message should be
346 * * 1 - the action has been allowed;
347 * * -EACCES - the action has been denied;
348 * * < 0 - an unspecified error.
350 * A step-by-step description of how it works:
352 * 1. A D-Bus method handler calls bus_verify_polkit_async(), passing it the D-Bus message being
353 * processed and the polkit action to verify.
354 * 2. bus_verify_polkit_async() checks the registry for the message and action combination. Let's
355 * assume this is the first call, so it finds nothing.
356 * 3. A new AsyncPolkitQuery object is created and an async. D-Bus call to polkit is made. The
357 * function then returns 0. The method handler returns 1 to tell sd-bus that the processing of
358 * the message has been interrupted.
359 * 4. (Later) A reply from polkit is received and async_polkit_callback() is called.
360 * 5. async_polkit_callback() reads the reply and stores result into the passed query.
361 * 6. async_polkit_callback() enqueues the original message again.
362 * 7. (Later) The same D-Bus method handler is called for the same message. It calls
363 * bus_verify_polkit_async() again.
364 * 8. bus_verify_polkit_async() checks the registry for the message and action combination. It finds
365 * an existing query and returns its result.
366 * 9. The method handler continues processing of the message.
370 * async_polkit_callback() registers a deferred call of async_polkit_defer() for the query, which
371 * causes the query to be removed from the registry and freed. Deferred events are run with idle
372 * priority, so this will happen after processing of the D-Bus message, when the query is no longer
377 * (m - D-Bus message, a - polkit action, q - polkit query)
380 * -> bus_verify_polkit_async(m, a)
381 * -> bus_call_method_async(q)
382 * <- bus_verify_polkit_async(m, a) = 0
383 * <- foo_method(m) = 1
385 * -> async_polkit_callback(q)
386 * -> sd_event_add_defer(async_polkit_defer, q)
387 * -> sd_bus_enqueue_for_read(m)
388 * <- async_polkit_callback(q)
391 * -> bus_verify_polkit_async(m, a)
392 * <- bus_verify_polkit_async(m, a) = 1/-EACCES/error
396 * -> async_polkit_defer(q)
397 * -> async_polkit_query_free(q)
398 * <- async_polkit_defer(q)
401 int bus_verify_polkit_async(
402 sd_bus_message
*call
,
405 const char **details
,
409 sd_bus_error
*ret_error
) {
417 r
= check_good_user(call
, good_user
);
422 AsyncPolkitQuery
*q
= hashmap_get(*registry
, call
);
423 /* This is the second invocation of this function, and there's already a response from
424 * polkit, let's process it */
426 return process_polkit_response(q
, call
, action
, details
, ret_error
);
429 r
= sd_bus_query_sender_privilege(call
, capability
);
436 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pk
= NULL
;
437 _cleanup_(async_polkit_query_freep
) AsyncPolkitQuery
*q_new
= NULL
;
439 int c
= sd_bus_message_get_allow_interactive_authorization(call
);
445 r
= hashmap_ensure_allocated(registry
, NULL
);
449 r
= bus_message_new_polkit_auth_call(call
, action
, details
, interactive
, &pk
);
453 q
= q_new
= new(AsyncPolkitQuery
, 1);
457 *q
= (AsyncPolkitQuery
) {
458 .request
= sd_bus_message_ref(call
),
461 q
->action
= strdup(action
);
465 q
->details
= strv_copy((char**) details
);
469 r
= hashmap_put(*registry
, call
, q
);
473 q
->registry
= *registry
;
475 r
= sd_bus_call_async(call
->bus
, &q
->slot
, pk
, async_polkit_callback
, q
, 0);
487 Hashmap
*bus_verify_polkit_async_registry_free(Hashmap
*registry
) {
489 return hashmap_free_with_destructor(registry
, async_polkit_query_free
);
491 assert(hashmap_isempty(registry
));
492 return hashmap_free(registry
);