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(
44 r
= sd_bus_message_open_container(m
, 'a', "{ss}");
48 STRV_FOREACH_PAIR(k
, v
, l
) {
49 r
= sd_bus_message_append(m
, "{ss}", *k
, *v
);
54 r
= sd_bus_message_close_container(m
);
69 sd_bus_error
*ret_error
) {
76 /* Tests non-interactively! */
78 r
= check_good_user(call
, good_user
);
82 r
= sd_bus_query_sender_privilege(call
, capability
);
89 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*request
= NULL
;
90 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
91 int authorized
= false, challenge
= false;
94 sender
= sd_bus_message_get_sender(call
);
98 r
= sd_bus_message_new_method_call(
101 "org.freedesktop.PolicyKit1",
102 "/org/freedesktop/PolicyKit1/Authority",
103 "org.freedesktop.PolicyKit1.Authority",
104 "CheckAuthorization");
108 r
= sd_bus_message_append(
111 "system-bus-name", 1, "name", "s", sender
,
116 r
= bus_message_append_strv_key_value(request
, details
);
120 r
= sd_bus_message_append(request
, "us", 0, NULL
);
124 r
= sd_bus_call(call
->bus
, request
, 0, ret_error
, &reply
);
126 /* Treat no PK available as access denied */
127 if (bus_error_is_unknown_service(ret_error
)) {
128 sd_bus_error_free(ret_error
);
135 r
= sd_bus_message_enter_container(reply
, 'r', "bba{ss}");
139 r
= sd_bus_message_read(reply
, "bb", &authorized
, &challenge
);
147 *_challenge
= challenge
;
158 typedef struct AsyncPolkitQuery
{
162 sd_bus_message
*request
, *reply
;
166 sd_event_source
*defer_event_source
;
169 static void async_polkit_query_free(AsyncPolkitQuery
*q
) {
173 sd_bus_slot_unref(q
->slot
);
175 if (q
->registry
&& q
->request
)
176 hashmap_remove(q
->registry
, q
->request
);
178 sd_bus_message_unref(q
->request
);
179 sd_bus_message_unref(q
->reply
);
182 strv_free(q
->details
);
184 sd_event_source_disable_unref(q
->defer_event_source
);
188 static int async_polkit_defer(sd_event_source
*s
, void *userdata
) {
189 AsyncPolkitQuery
*q
= userdata
;
193 /* This is called as idle event source after we processed the async polkit reply, hopefully after the
194 * method call we re-enqueued has been properly processed. */
196 async_polkit_query_free(q
);
200 static int async_polkit_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
201 AsyncPolkitQuery
*q
= userdata
;
208 q
->slot
= sd_bus_slot_unref(q
->slot
);
211 q
->reply
= sd_bus_message_ref(reply
);
213 /* Now, let's dispatch the original message a second time be re-enqueing. This will then traverse the
214 * whole message processing again, and thus re-validating and re-retrieving the "userdata" field
217 * We install an idle event loop event to clean-up the PolicyKit request data when we are idle again,
218 * i.e. after the second time the message is processed is complete. */
220 assert(!q
->defer_event_source
);
221 r
= sd_event_add_defer(sd_bus_get_event(sd_bus_message_get_bus(reply
)), &q
->defer_event_source
, async_polkit_defer
, q
);
225 r
= sd_event_source_set_priority(q
->defer_event_source
, SD_EVENT_PRIORITY_IDLE
);
229 r
= sd_event_source_set_enabled(q
->defer_event_source
, SD_EVENT_ONESHOT
);
233 r
= sd_bus_message_rewind(q
->request
, true);
237 r
= sd_bus_enqueue_for_read(sd_bus_message_get_bus(q
->request
), q
->request
);
244 log_debug_errno(r
, "Processing asynchronous PolicyKit reply failed, ignoring: %m");
245 (void) sd_bus_reply_method_errno(q
->request
, r
, NULL
);
246 async_polkit_query_free(q
);
252 int bus_verify_polkit_async(
253 sd_bus_message
*call
,
256 const char **details
,
260 sd_bus_error
*ret_error
) {
263 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pk
= NULL
;
274 r
= check_good_user(call
, good_user
);
279 q
= hashmap_get(*registry
, call
);
281 int authorized
, challenge
;
283 /* This is the second invocation of this function, and there's already a response from
284 * polkit, let's process it */
287 /* If the operation we want to authenticate changed between the first and the second time,
288 * let's not use this authentication, it might be out of date as the object and context we
289 * operate on might have changed. */
290 if (!streq(q
->action
, action
) ||
291 !strv_equal(q
->details
, (char**) details
))
294 if (sd_bus_message_is_method_error(q
->reply
, NULL
)) {
295 const sd_bus_error
*e
;
297 e
= sd_bus_message_get_error(q
->reply
);
299 /* Treat no PK available as access denied */
300 if (bus_error_is_unknown_service(e
))
303 /* Copy error from polkit reply */
304 sd_bus_error_copy(ret_error
, e
);
305 return -sd_bus_error_get_errno(e
);
308 r
= sd_bus_message_enter_container(q
->reply
, 'r', "bba{ss}");
310 r
= sd_bus_message_read(q
->reply
, "bb", &authorized
, &challenge
);
318 return sd_bus_error_set(ret_error
, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
, "Interactive authentication required.");
324 r
= sd_bus_query_sender_privilege(call
, capability
);
330 sender
= sd_bus_message_get_sender(call
);
335 c
= sd_bus_message_get_allow_interactive_authorization(call
);
341 r
= hashmap_ensure_allocated(registry
, NULL
);
345 r
= sd_bus_message_new_method_call(
348 "org.freedesktop.PolicyKit1",
349 "/org/freedesktop/PolicyKit1/Authority",
350 "org.freedesktop.PolicyKit1.Authority",
351 "CheckAuthorization");
355 r
= sd_bus_message_append(
358 "system-bus-name", 1, "name", "s", sender
,
363 r
= bus_message_append_strv_key_value(pk
, details
);
367 r
= sd_bus_message_append(pk
, "us", interactive
, NULL
);
371 q
= new(AsyncPolkitQuery
, 1);
375 *q
= (AsyncPolkitQuery
) {
376 .request
= sd_bus_message_ref(call
),
379 q
->action
= strdup(action
);
381 async_polkit_query_free(q
);
385 q
->details
= strv_copy((char**) details
);
387 async_polkit_query_free(q
);
391 r
= hashmap_put(*registry
, call
, q
);
393 async_polkit_query_free(q
);
397 q
->registry
= *registry
;
399 r
= sd_bus_call_async(call
->bus
, &q
->slot
, pk
, async_polkit_callback
, q
, 0);
401 async_polkit_query_free(q
);
411 void bus_verify_polkit_async_registry_free(Hashmap
*registry
) {
413 hashmap_free_with_destructor(registry
, async_polkit_query_free
);