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
,
106 const char **details
,
109 sd_bus_error
*ret_error
) {
116 /* Tests non-interactively! */
118 r
= check_good_user(call
, good_user
);
122 r
= sd_bus_query_sender_privilege(call
, -1);
129 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*request
= NULL
, *reply
= NULL
;
130 int authorized
= false, challenge
= false;
132 r
= bus_message_new_polkit_auth_call(call
, action
, details
, /* interactive = */ false, &request
);
136 r
= sd_bus_call(call
->bus
, request
, 0, ret_error
, &reply
);
138 /* Treat no PK available as access denied */
139 if (bus_error_is_unknown_service(ret_error
)) {
140 sd_bus_error_free(ret_error
);
147 r
= sd_bus_message_enter_container(reply
, 'r', "bba{ss}");
151 r
= sd_bus_message_read(reply
, "bb", &authorized
, &challenge
);
159 *_challenge
= challenge
;
169 typedef struct AsyncPolkitQueryAction
{
173 LIST_FIELDS(struct AsyncPolkitQueryAction
, authorized
);
174 } AsyncPolkitQueryAction
;
176 static AsyncPolkitQueryAction
*async_polkit_query_action_free(AsyncPolkitQueryAction
*a
) {
181 strv_free(a
->details
);
186 DEFINE_TRIVIAL_CLEANUP_FUNC(AsyncPolkitQueryAction
*, async_polkit_query_action_free
);
188 typedef struct AsyncPolkitQuery
{
191 AsyncPolkitQueryAction
*action
;
193 sd_bus_message
*request
;
197 sd_event_source
*defer_event_source
;
199 LIST_HEAD(AsyncPolkitQueryAction
, authorized_actions
);
200 AsyncPolkitQueryAction
*denied_action
;
201 AsyncPolkitQueryAction
*error_action
;
205 static AsyncPolkitQuery
*async_polkit_query_free(AsyncPolkitQuery
*q
) {
209 sd_bus_slot_unref(q
->slot
);
211 if (q
->registry
&& q
->request
)
212 hashmap_remove(q
->registry
, q
->request
);
214 sd_bus_message_unref(q
->request
);
216 async_polkit_query_action_free(q
->action
);
218 sd_event_source_disable_unref(q
->defer_event_source
);
220 LIST_CLEAR(authorized
, q
->authorized_actions
, async_polkit_query_action_free
);
222 async_polkit_query_action_free(q
->denied_action
);
223 async_polkit_query_action_free(q
->error_action
);
225 sd_bus_error_free(&q
->error
);
230 DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(AsyncPolkitQuery
, async_polkit_query
, async_polkit_query_free
);
231 DEFINE_TRIVIAL_CLEANUP_FUNC(AsyncPolkitQuery
*, async_polkit_query_unref
);
233 static int async_polkit_defer(sd_event_source
*s
, void *userdata
) {
234 AsyncPolkitQuery
*q
= ASSERT_PTR(userdata
);
238 /* This is called as idle event source after we processed the async polkit reply, hopefully after the
239 * method call we re-enqueued has been properly processed. */
241 async_polkit_query_unref(q
);
245 static int async_polkit_read_reply(sd_bus_message
*reply
, AsyncPolkitQuery
*q
) {
246 _cleanup_(async_polkit_query_action_freep
) AsyncPolkitQueryAction
*a
= NULL
;
247 int authorized
, challenge
, r
;
252 /* Processing of a PolicyKit checks is canceled on the first auth. error. */
253 assert(!q
->denied_action
);
254 assert(!q
->error_action
);
255 assert(!sd_bus_error_is_set(&q
->error
));
258 a
= TAKE_PTR(q
->action
);
260 if (sd_bus_message_is_method_error(reply
, NULL
)) {
261 const sd_bus_error
*e
;
263 e
= sd_bus_message_get_error(reply
);
265 if (bus_error_is_unknown_service(e
))
266 /* Treat no PK available as access denied */
267 q
->denied_action
= TAKE_PTR(a
);
269 /* Save error from polkit reply, so it can be returned when the same authorization
270 * is attempted for second time */
271 q
->error_action
= TAKE_PTR(a
);
272 r
= sd_bus_error_copy(&q
->error
, e
);
280 r
= sd_bus_message_enter_container(reply
, 'r', "bba{ss}");
282 r
= sd_bus_message_read(reply
, "bb", &authorized
, &challenge
);
287 LIST_PREPEND(authorized
, q
->authorized_actions
, TAKE_PTR(a
));
288 else if (challenge
) {
289 q
->error_action
= TAKE_PTR(a
);
290 sd_bus_error_set_const(&q
->error
, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
, "Interactive authentication required.");
292 q
->denied_action
= TAKE_PTR(a
);
297 static int async_polkit_process_reply(sd_bus_message
*reply
, AsyncPolkitQuery
*q
) {
304 q
->slot
= sd_bus_slot_unref(q
->slot
);
306 r
= async_polkit_read_reply(reply
, q
);
310 /* Now, let's dispatch the original message a second time be re-enqueing. This will then traverse the
311 * whole message processing again, and thus re-validating and re-retrieving the "userdata" field
314 * We install an idle event loop event to clean-up the PolicyKit request data when we are idle again,
315 * i.e. after the second time the message is processed is complete. */
317 if (!q
->defer_event_source
) {
318 r
= sd_event_add_defer(
319 sd_bus_get_event(sd_bus_message_get_bus(reply
)),
320 &q
->defer_event_source
,
326 r
= sd_event_source_set_priority(q
->defer_event_source
, SD_EVENT_PRIORITY_IDLE
);
331 r
= sd_event_source_set_enabled(q
->defer_event_source
, SD_EVENT_ONESHOT
);
335 r
= sd_bus_message_rewind(q
->request
, true);
339 r
= sd_bus_enqueue_for_read(sd_bus_message_get_bus(q
->request
), q
->request
);
346 static int async_polkit_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
347 AsyncPolkitQuery
*q
= ASSERT_PTR(userdata
);
352 r
= async_polkit_process_reply(reply
, q
);
354 log_debug_errno(r
, "Processing asynchronous PolicyKit reply failed, ignoring: %m");
355 (void) sd_bus_reply_method_errno(q
->request
, r
, NULL
);
356 async_polkit_query_unref(q
);
361 static int async_polkit_query_check_action(
364 const char **details
,
365 sd_bus_error
*ret_error
) {
371 LIST_FOREACH(authorized
, a
, q
->authorized_actions
)
372 if (streq(a
->action
, action
) && strv_equal(a
->details
, (char**) details
))
375 if (q
->error_action
&& streq(q
->error_action
->action
, action
))
376 return sd_bus_error_copy(ret_error
, &q
->error
);
378 if (q
->denied_action
&& streq(q
->denied_action
->action
, action
))
386 /* bus_verify_polkit_async() handles verification of D-Bus calls with polkit. Because the polkit API
387 * is asynchronous, the whole thing is a bit complex and requires some support in the code that uses
388 * it. It relies on sd-bus's support for interrupting the processing of a message.
392 * * bus_verify_polkit_async() must be called before any changes to internal state.
393 * * If bus_verify_polkit_async() has made a new polkit query (signaled by return value 0),
394 * processing of the message should be interrupted. This is done by returning 1--which sd-bus
395 * handles specially--and is usually accompanied by a comment. (The message will be queued for
396 * processing again later when a reply from polkit is received.)
397 * * The code needs to keep a hashmap, here called registry, in which bus_verify_polkit_async()
398 * stores active queries. This hashmap's lifetime must be larger than the method handler's;
399 * e.g., it can be a member of some "manager" object or a global variable.
403 * * 0 - a new polkit call has been made, which means the processing of the message should be
405 * * 1 - the action has been allowed;
406 * * -EACCES - the action has been denied;
407 * * < 0 - an unspecified error.
409 * A step-by-step description of how it works:
411 * 1. A D-Bus method handler calls bus_verify_polkit_async(), passing it the D-Bus message being
412 * processed and the polkit action to verify.
413 * 2. bus_verify_polkit_async() checks the registry for an existing query object associated with the
414 * message. Let's assume this is the first call, so it finds nothing.
415 * 3. A new AsyncPolkitQuery object is created and an async. D-Bus call to polkit is made. The
416 * function then returns 0. The method handler returns 1 to tell sd-bus that the processing of
417 * the message has been interrupted.
418 * 4. (Later) A reply from polkit is received and async_polkit_callback() is called.
419 * 5. async_polkit_callback() reads the reply and stores its result in the passed query.
420 * 6. async_polkit_callback() enqueues the original message again.
421 * 7. (Later) The same D-Bus method handler is called for the same message. It calls
422 * bus_verify_polkit_async() again.
423 * 8. bus_verify_polkit_async() checks the registry for an existing query object associated with the
424 * message. It finds one and returns the result for the action.
425 * 9. The method handler continues processing of the message. If there's another action that needs
427 * 10. bus_verify_polkit_async() is called again for the new action. The registry already contains a
428 * query for the message, but the new action hasn't been seen yet, hence steps 4-8 are repeated.
429 * 11. (In the method handler again.) bus_verify_polkit_async() returns query results for both
430 * actions and the processing continues as in step 9.
434 * async_polkit_callback() registers a deferred call of async_polkit_defer() for the query, which
435 * causes the query to be removed from the registry and freed. Deferred events are run with idle
436 * priority, so this will happen after processing of the D-Bus message, when the query is no longer
441 * (m - D-Bus message, a - polkit action, q - polkit query)
444 * -> bus_verify_polkit_async(m, a)
445 * -> async_polkit_query_ref(q)
446 * -> bus_call_method_async(q)
447 * <- bus_verify_polkit_async(m, a) = 0
448 * <- foo_method(m) = 1
450 * -> async_polkit_callback(q)
451 * -> sd_event_add_defer(async_polkit_defer, q)
452 * -> sd_bus_enqueue_for_read(m)
453 * <- async_polkit_callback(q)
456 * -> bus_verify_polkit_async(m, a)
457 * <- bus_verify_polkit_async(m, a) = 1/-EACCES/error
459 * // possibly another call to bus_verify_polkit_async with action a2
462 * -> async_polkit_defer(q)
463 * -> async_polkit_query_unref(q)
464 * <- async_polkit_defer(q)
467 int bus_verify_polkit_async_full(
468 sd_bus_message
*call
,
470 const char **details
,
471 bool interactive
, /* Use only for legacy method calls that have a separate "allow_interactive_authentication" field */
474 sd_bus_error
*ret_error
) {
483 r
= check_good_user(call
, good_user
);
488 _cleanup_(async_polkit_query_unrefp
) AsyncPolkitQuery
*q
= NULL
;
490 q
= async_polkit_query_ref(hashmap_get(*registry
, call
));
491 /* This is a repeated invocation of this function, hence let's check if we've already got
492 * a response from polkit for this action */
494 r
= async_polkit_query_check_action(q
, action
, details
, ret_error
);
500 r
= sd_bus_query_sender_privilege(call
, -1);
507 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pk
= NULL
;
509 int c
= sd_bus_message_get_allow_interactive_authorization(call
);
515 r
= hashmap_ensure_allocated(registry
, NULL
);
519 r
= bus_message_new_polkit_auth_call(call
, action
, details
, interactive
, &pk
);
524 q
= new(AsyncPolkitQuery
, 1);
528 *q
= (AsyncPolkitQuery
) {
530 .request
= sd_bus_message_ref(call
),
535 q
->action
= new(AsyncPolkitQueryAction
, 1);
539 *q
->action
= (AsyncPolkitQueryAction
) {
540 .action
= strdup(action
),
541 .details
= strv_copy((char**) details
),
543 if (!q
->action
->action
|| !q
->action
->details
)
547 r
= hashmap_put(*registry
, call
, q
);
551 q
->registry
= *registry
;
554 r
= sd_bus_call_async(call
->bus
, &q
->slot
, pk
, async_polkit_callback
, q
, 0);
566 Hashmap
*bus_verify_polkit_async_registry_free(Hashmap
*registry
) {
568 return hashmap_free_with_destructor(registry
, async_polkit_query_unref
);
570 assert(hashmap_isempty(registry
));
571 return hashmap_free(registry
);