Debian Codegraph @

src/shared/bus-polkit.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-bus.h"
6 #include "sd-event.h"
7
8 #include "bus-message.h"
9 #include "bus-polkit.h"
10 #include "bus-util.h"
11 #include "errno-util.h"
12 #include "hashmap.h"
13 #include "list.h"
14 #include "log.h"
15 #include "pidref.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "varlink-util.h"
19
20 static int bus_message_check_good_user(sd_bus_message *m, uid_t good_user) {
21 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
22 uid_t sender_uid;
23 int r;
24
25 assert(m);
26
27 if (good_user == UID_INVALID)
28 return false;
29
30 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_EUID, &creds);
31 if (r < 0)
32 return r;
33
34 /* Don't trust augmented credentials for authorization */
35 assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM);
36
37 r = sd_bus_creds_get_euid(creds, &sender_uid);
38 if (r < 0)
39 return r;
40
41 return sender_uid == good_user;
42 }
43
44 #if ENABLE_POLKIT
45 static int bus_message_append_strv_key_value(sd_bus_message *m, const char **l) {
46 int r;
47
48 assert(m);
49
50 r = sd_bus_message_open_container(m, 'a', "{ss}");
51 if (r < 0)
52 return r;
53
54 STRV_FOREACH_PAIR(k, v, l) {
55 r = sd_bus_message_append(m, "{ss}", *k, *v);
56 if (r < 0)
57 return r;
58 }
59
60 return sd_bus_message_close_container(m);
61 }
62
63 static int bus_message_new_polkit_auth_call_for_bus(
64 sd_bus_message *m,
65 const char *action,
66 const char **details,
67 PolkitFlags flags,
68 sd_bus_message **ret) {
69
70 _cleanup_(sd_bus_message_unrefp) sd_bus_message *c = NULL;
71 const char *sender;
72 int r;
73
74 assert(m);
75 assert(action);
76 assert(ret);
77
78 sender = sd_bus_message_get_sender(m);
79 if (!sender)
80 return -EBADMSG;
81
82 r = sd_bus_message_new_method_call(
83 ASSERT_PTR(m->bus),
84 &c,
85 "org.freedesktop.PolicyKit1",
86 "/org/freedesktop/PolicyKit1/Authority",
87 "org.freedesktop.PolicyKit1.Authority",
88 "CheckAuthorization");
89 if (r < 0)
90 return r;
91
92 r = sd_bus_message_append(c, "(sa{sv})s", "system-bus-name", 1, "name", "s", sender, action);
93 if (r < 0)
94 return r;
95
96 r = bus_message_append_strv_key_value(c, details);
97 if (r < 0)
98 return r;
99
100 r = sd_bus_message_append(c, "us", (uint32_t) (flags & _POLKIT_MASK_PUBLIC), NULL);
101 if (r < 0)
102 return r;
103
104 *ret = TAKE_PTR(c);
105 return 0;
106 }
107 #endif
108
109 int bus_test_polkit(
110 sd_bus_message *call,
111 const char *action,
112 const char **details,
113 uid_t good_user,
114 bool *ret_challenge,
115 sd_bus_error *reterr_error) {
116
117 int r;
118
119 assert(call);
120 assert(action);
121
122 /* Tests non-interactively! */
123
124 r = bus_message_check_good_user(call, good_user);
125 if (r != 0)
126 return r;
127
128 r = sd_bus_query_sender_privilege(call, -1);
129 if (r < 0)
130 return r;
131 if (r > 0) {
132 if (ret_challenge)
133 *ret_challenge = false;
134 return 1;
135 }
136
137 #if ENABLE_POLKIT
138 _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL, *reply = NULL;
139 int authorized = false, challenge = false;
140
141 r = bus_message_new_polkit_auth_call_for_bus(call, action, details, /* flags= */ 0, &request);
142 if (r < 0)
143 return r;
144
145 r = sd_bus_call(call->bus, request, 0, reterr_error, &reply);
146 if (r < 0) {
147 /* Treat no PK available as access denied */
148 if (bus_error_is_unknown_service(reterr_error)) {
149 sd_bus_error_free(reterr_error);
150 return -EACCES;
151 }
152
153 return r;
154 }
155
156 r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
157 if (r < 0)
158 return r;
159
160 r = sd_bus_message_read(reply, "bb", &authorized, &challenge);
161 if (r < 0)
162 return r;
163
164 if (authorized)
165 return 1;
166
167 if (ret_challenge) {
168 *ret_challenge = challenge;
169 return 0;
170 }
171 #endif
172
173 return -EACCES;
174 }
175
176 #if ENABLE_POLKIT
177
178 typedef struct AsyncPolkitQueryAction {
179 char *action;
180 char **details;
181
182 LIST_FIELDS(struct AsyncPolkitQueryAction, authorized);
183 } AsyncPolkitQueryAction;
184
185 static AsyncPolkitQueryAction* async_polkit_query_action_free(AsyncPolkitQueryAction *a) {
186 if (!a)
187 return NULL;
188
189 free(a->action);
190 strv_free(a->details);
191
192 return mfree(a);
193 }
194
195 DEFINE_TRIVIAL_CLEANUP_FUNC(AsyncPolkitQueryAction*, async_polkit_query_action_free);
196
197 typedef struct AsyncPolkitQuery {
198 unsigned n_ref;
199
200 AsyncPolkitQueryAction *action; /* action currently being processed */
201
202 sd_bus *bus;
203 sd_bus_message *request; /* the original bus method call that triggered the polkit auth, NULL in case of varlink */
204 sd_bus_slot *slot;
205 sd_varlink *link; /* the original varlink method call that triggered the polkit auth, NULL in case of bus */
206
207 Hashmap *registry;
208 sd_event_source *defer_event_source;
209
210 LIST_HEAD(AsyncPolkitQueryAction, authorized_actions); /* actions we successfully were authorized for */
211 AsyncPolkitQueryAction *denied_action; /* if we received denial for an action, it's this one */
212 AsyncPolkitQueryAction *absent_action; /* If polkit was absent for some action, it's this one */
213 AsyncPolkitQueryAction *error_action; /* if we encountered any other error, it's this one */
214 sd_bus_error error; /* the precise error, in case error_action is set */
215 } AsyncPolkitQuery;
216
217 static AsyncPolkitQuery* async_polkit_query_free(AsyncPolkitQuery *q) {
218 if (!q)
219 return NULL;
220
221 sd_bus_slot_unref(q->slot);
222
223 if (q->registry) {
224 if (q->request)
225 hashmap_remove(q->registry, q->request);
226 if (q->link)
227 hashmap_remove(q->registry, q->link);
228 }
229
230 sd_bus_message_unref(q->request);
231
232 sd_bus_unref(q->bus);
233 sd_varlink_unref(q->link);
234
235 async_polkit_query_action_free(q->action);
236
237 sd_event_source_disable_unref(q->defer_event_source);
238
239 LIST_CLEAR(authorized, q->authorized_actions, async_polkit_query_action_free);
240
241 async_polkit_query_action_free(q->denied_action);
242 async_polkit_query_action_free(q->absent_action);
243 async_polkit_query_action_free(q->error_action);
244
245 sd_bus_error_free(&q->error);
246
247 return mfree(q);
248 }
249
250 DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(AsyncPolkitQuery, async_polkit_query, async_polkit_query_free);
251 DEFINE_TRIVIAL_CLEANUP_FUNC(AsyncPolkitQuery*, async_polkit_query_unref);
252
253 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
254 async_polkit_query_hash_ops,
255 void, trivial_hash_func, trivial_compare_func,
256 AsyncPolkitQuery, async_polkit_query_unref);
257
258 static int async_polkit_defer(sd_event_source *s, void *userdata) {
259 AsyncPolkitQuery *q = ASSERT_PTR(userdata);
260
261 assert(s);
262
263 /* This is called as idle event source after we processed the async polkit reply, hopefully after the
264 * method call we re-enqueued has been properly processed. */
265
266 async_polkit_query_unref(q);
267 return 0;
268 }
269
270 static int async_polkit_read_reply(sd_bus_message *reply, AsyncPolkitQuery *q) {
271 _cleanup_(async_polkit_query_action_freep) AsyncPolkitQueryAction *a = NULL;
272 int authorized, challenge, r;
273
274 assert(reply);
275 assert(q);
276
277 /* Processing of a PolicyKit checks is canceled on the first auth. error. */
278 assert(!q->denied_action);
279 assert(!q->absent_action);
280 assert(!q->error_action);
281 assert(!sd_bus_error_is_set(&q->error));
282
283 a = ASSERT_PTR(TAKE_PTR(q->action));
284
285 if (sd_bus_message_is_method_error(reply, NULL)) {
286 const sd_bus_error *e = ASSERT_PTR(sd_bus_message_get_error(reply));
287
288 if (bus_error_is_unknown_service(e)) {
289 /* If PK is absent, then store this away, as it depends on the callers flags whether
290 * this means deny or allow */
291 log_debug("Polkit found to be unavailable while trying to authorize action '%s'.", a->action);
292 q->absent_action = TAKE_PTR(a);
293 } else if (sd_bus_error_has_names(
294 e,
295 "org.freedesktop.PolicyKit1.Error.Failed",
296 "org.freedesktop.PolicyKit1.Error.Cancelled",
297 "org.freedesktop.PolicyKit1.Error.NotAuthorized")) {
298 /* Treat some of the well-known PK errors as denial. */
299 log_debug("Polkit authorization for action '%s' failed with an polkit error: %s", a->action, e->name);
300 q->denied_action = TAKE_PTR(a);
301 } else {
302 /* Save error from polkit reply, so it can be returned when the same authorization
303 * is attempted for second time */
304 log_debug("Polkit authorization for action '%s' failed with an unexpected error: %s", a->action, e->name);
305 q->error_action = TAKE_PTR(a);
306 r = sd_bus_error_copy(&q->error, e);
307 if (r == -ENOMEM)
308 return r;
309 }
310
311 return 0;
312 }
313
314 r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
315 if (r >= 0)
316 r = sd_bus_message_read(reply, "bb", &authorized, &challenge);
317 if (r < 0)
318 return r;
319
320 if (authorized) {
321 log_debug("Polkit authorization for action '%s' succeeded.", a->action);
322 LIST_PREPEND(authorized, q->authorized_actions, TAKE_PTR(a));
323 } else if (challenge) {
324 log_debug("Polkit authorization for action '%s' requires interactive authentication, which we didn't allow.", a->action);
325 q->error_action = TAKE_PTR(a);
326 sd_bus_error_set_const(&q->error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED,
327 "Access denied as the requested operation requires interactive authentication. However, interactive authentication has not been enabled by the calling program.");
328 } else {
329 log_debug("Polkit authorization for action '%s' denied.", a->action);
330 q->denied_action = TAKE_PTR(a);
331 }
332
333 return 0;
334 }
335
336 static int async_polkit_process_reply(sd_bus_message *reply, AsyncPolkitQuery *q) {
337 int r;
338
339 assert(reply);
340 assert(q);
341
342 assert(q->slot);
343 q->slot = sd_bus_slot_unref(q->slot);
344
345 r = async_polkit_read_reply(reply, q);
346 if (r < 0)
347 return r;
348
349 /* Now, let's dispatch the original message a second time be re-enqueuing. This will then traverse the
350 * whole message processing again, and thus re-validating and re-retrieving the "userdata" field
351 * again.
352 *
353 * We install an idle event loop event to clean-up the PolicyKit request data when we are idle again,
354 * i.e. after the second time the message is processed is complete. */
355
356 if (!q->defer_event_source) {
357 r = sd_event_add_defer(
358 sd_bus_get_event(q->bus),
359 &q->defer_event_source,
360 async_polkit_defer,
361 q);
362 if (r < 0)
363 return r;
364
365 r = sd_event_source_set_priority(q->defer_event_source, SD_EVENT_PRIORITY_IDLE);
366 if (r < 0)
367 return r;
368 }
369
370 r = sd_event_source_set_enabled(q->defer_event_source, SD_EVENT_ONESHOT);
371 if (r < 0)
372 return r;
373
374 if (q->request) {
375 r = sd_bus_message_rewind(q->request, true);
376 if (r < 0)
377 return r;
378
379 r = sd_bus_enqueue_for_read(q->bus, q->request);
380 if (r < 0)
381 return r;
382 }
383
384 if (q->link) {
385 r = sd_varlink_dispatch_again(q->link);
386 if (r < 0)
387 return r;
388 }
389
390 return 1;
391 }
392
393 static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *reterr_error) {
394 AsyncPolkitQuery *q = ASSERT_PTR(userdata);
395 int r;
396
397 assert(reply);
398
399 r = async_polkit_process_reply(reply, q);
400 if (r < 0) {
401 log_debug_errno(r, "Processing asynchronous PolicyKit reply failed, ignoring: %m");
402 if (q->request)
403 (void) sd_bus_reply_method_errno(q->request, r, NULL);
404 if (q->link)
405 (void) sd_varlink_error_errno(q->link, r);
406 async_polkit_query_unref(q);
407 }
408 return r;
409 }
410
411 static bool async_polkit_query_have_action(
412 AsyncPolkitQuery *q,
413 const char *action,
414 const char **details) {
415
416 assert(q);
417 assert(action);
418
419 LIST_FOREACH(authorized, a, q->authorized_actions)
420 if (streq(a->action, action) && strv_equal(a->details, (char**) details))
421 return true;
422
423 return false;
424 }
425
426 static int async_polkit_query_check_action(
427 AsyncPolkitQuery *q,
428 const char *action,
429 const char **details,
430 PolkitFlags flags,
431 sd_bus_error *reterr_error) {
432
433 assert(q);
434 assert(action);
435
436 if (async_polkit_query_have_action(q, action, details))
437 return 1; /* Allow! */
438
439 if (q->error_action && streq(q->error_action->action, action))
440 return sd_bus_error_copy(reterr_error, &q->error);
441
442 if (q->denied_action && streq(q->denied_action->action, action))
443 return -EACCES; /* Deny! */
444
445 if (q->absent_action)
446 return FLAGS_SET(flags, POLKIT_DEFAULT_ALLOW) ? 1 /* Allow! */ : -EACCES /* Deny! */;
447
448 /* Also deny if we've got an auth. failure for a previous action */
449 if (q->denied_action || q->error_action)
450 return -EBUSY;
451
452 return 0; /* no reply yet */
453 }
454 #endif
455
456 /* bus_verify_polkit_async() handles verification of D-Bus calls with polkit. Because the polkit API
457 * is asynchronous, the whole thing is a bit complex and requires some support in the code that uses
458 * it. It relies on sd-bus's support for interrupting the processing of a message.
459 *
460 * Requirements:
461 *
462 * * bus_verify_polkit_async() must be called before any changes to internal state.
463 * * If bus_verify_polkit_async() has made a new polkit query (signaled by return value 0),
464 * processing of the message should be interrupted. This is done by returning 1--which sd-bus
465 * handles specially--and is usually accompanied by a comment. (The message will be queued for
466 * processing again later when a reply from polkit is received.)
467 * * The code needs to keep a hashmap, here called registry, in which bus_verify_polkit_async()
468 * stores active queries. This hashmap's lifetime must be larger than the method handler's;
469 * e.g., it can be a member of some "manager" object or a global variable.
470 *
471 * Return value:
472 *
473 * * 0 - a new polkit call has been made, which means the processing of the message should be
474 * interrupted;
475 * * 1 - the action has been allowed;
476 * * -EACCES - the action has been denied;
477 * * < 0 - an unspecified error.
478 *
479 * A step-by-step description of how it works:
480 *
481 * 1. A D-Bus method handler calls bus_verify_polkit_async(), passing it the D-Bus message being
482 * processed and the polkit action to verify.
483 * 2. bus_verify_polkit_async() checks the registry for an existing query object associated with the
484 * message. Let's assume this is the first call, so it finds nothing.
485 * 3. A new AsyncPolkitQuery object is created and an async D-Bus call to polkit is made. The
486 * function then returns 0. The method handler returns 1 to tell sd-bus that the processing of
487 * the message has been interrupted.
488 * 4. (Later) A reply from polkit is received and async_polkit_callback() is called.
489 * 5. async_polkit_callback() reads the reply and stores its result in the passed query.
490 * 6. async_polkit_callback() enqueues the original message again.
491 * 7. (Later) The same D-Bus method handler is called for the same message. It calls
492 * bus_verify_polkit_async() again.
493 * 8. bus_verify_polkit_async() checks the registry for an existing query object associated with the
494 * message. It finds one and returns the result for the action.
495 * 9. The method handler continues processing of the message. If there's another action that needs
496 * to be verified:
497 * 10. bus_verify_polkit_async() is called again for the new action. The registry already contains a
498 * query for the message, but the new action hasn't been seen yet, hence steps 4-8 are repeated.
499 * 11. (In the method handler again.) bus_verify_polkit_async() returns query results for both
500 * actions and the processing continues as in step 9.
501 *
502 * Memory handling:
503 *
504 * async_polkit_callback() registers a deferred call of async_polkit_defer() for the query, which
505 * causes the query to be removed from the registry and freed. Deferred events are run with idle
506 * priority, so this will happen after processing of the D-Bus message, when the query is no longer
507 * needed.
508 *
509 * Schematically:
510 *
511 * (m - D-Bus message, a - polkit action, q - polkit query)
512 *
513 * -> foo_method(m)
514 * -> bus_verify_polkit_async(m, a)
515 * -> async_polkit_query_ref(q)
516 * -> bus_call_method_async(q)
517 * <- bus_verify_polkit_async(m, a) = 0
518 * <- foo_method(m) = 1
519 * ...
520 * -> async_polkit_callback(q)
521 * -> sd_event_add_defer(async_polkit_defer, q)
522 * -> sd_bus_enqueue_for_read(m)
523 * <- async_polkit_callback(q)
524 * ...
525 * -> foo_method(m)
526 * -> bus_verify_polkit_async(m, a)
527 * <- bus_verify_polkit_async(m, a) = 1/-EACCES/error
528 * ...
529 * // possibly another call to bus_verify_polkit_async with action a2
530 * <- foo_method(m)
531 * ...
532 * -> async_polkit_defer(q)
533 * -> async_polkit_query_unref(q)
534 * <- async_polkit_defer(q)
535 */
536
537 int bus_verify_polkit_async_full(
538 sd_bus_message *call,
539 const char *action,
540 const char **details,
541 uid_t good_user,
542 PolkitFlags flags,
543 Hashmap **registry,
544 sd_bus_error *reterr_error) {
545
546 int r;
547
548 assert(call);
549 assert(action);
550 assert(registry);
551
552 log_debug("Trying to acquire polkit authentication for '%s'.", action);
553
554 r = bus_message_check_good_user(call, good_user);
555 if (r != 0)
556 return r;
557
558 #if ENABLE_POLKIT
559 _cleanup_(async_polkit_query_unrefp) AsyncPolkitQuery *q = NULL;
560
561 q = async_polkit_query_ref(hashmap_get(*registry, call));
562 /* This is a repeated invocation of this function, hence let's check if we've already got
563 * a response from polkit for this action */
564 if (q) {
565 r = async_polkit_query_check_action(q, action, details, flags, reterr_error);
566 if (r != 0) {
567 log_debug("Found matching previous polkit authentication for '%s'.", action);
568 return r;
569 }
570 }
571
572 if (!FLAGS_SET(flags, POLKIT_ALWAYS_QUERY)) {
573 #endif
574 /* Don't query PK if client is privileged */
575 r = sd_bus_query_sender_privilege(call, /* capability= */ -1);
576 if (r < 0)
577 return r;
578 if (r > 0)
579 return 1;
580 #if ENABLE_POLKIT
581 }
582
583 int c = sd_bus_message_get_allow_interactive_authorization(call);
584 if (c < 0)
585 return c;
586 if (c > 0)
587 flags |= POLKIT_ALLOW_INTERACTIVE;
588
589 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL;
590 r = bus_message_new_polkit_auth_call_for_bus(call, action, details, flags, &pk);
591 if (r < 0)
592 return r;
593
594 if (!q) {
595 q = new(AsyncPolkitQuery, 1);
596 if (!q)
597 return -ENOMEM;
598
599 *q = (AsyncPolkitQuery) {
600 .n_ref = 1,
601 .request = sd_bus_message_ref(call),
602 .bus = sd_bus_ref(sd_bus_message_get_bus(call)),
603 };
604 }
605
606 assert(!q->action);
607 q->action = new(AsyncPolkitQueryAction, 1);
608 if (!q->action)
609 return -ENOMEM;
610
611 *q->action = (AsyncPolkitQueryAction) {
612 .action = strdup(action),
613 .details = strv_copy((char**) details),
614 };
615 if (!q->action->action || !q->action->details)
616 return -ENOMEM;
617
618 if (!q->registry) {
619 r = hashmap_ensure_put(registry, &async_polkit_query_hash_ops, call, q);
620 if (r < 0)
621 return r;
622
623 q->registry = *registry;
624 }
625
626 r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0);
627 if (r < 0)
628 return r;
629
630 TAKE_PTR(q);
631
632 return 0;
633 #else
634 return FLAGS_SET(flags, POLKIT_DEFAULT_ALLOW) ? 1 : -EACCES;
635 #endif
636 }
637
638 static int varlink_check_good_user(sd_varlink *link, uid_t good_user) {
639 int r;
640
641 assert(link);
642
643 if (good_user == UID_INVALID)
644 return false;
645
646 uid_t peer_uid;
647 r = sd_varlink_get_peer_uid(link, &peer_uid);
648 if (r < 0)
649 return r;
650
651 return good_user == peer_uid;
652 }
653
654 static int varlink_check_peer_privilege(sd_varlink *link) {
655 int r;
656
657 assert(link);
658
659 uid_t peer_uid;
660 r = sd_varlink_get_peer_uid(link, &peer_uid);
661 if (r < 0)
662 return r;
663
664 uid_t our_uid = getuid();
665 return peer_uid == our_uid ||
666 (our_uid != 0 && peer_uid == 0);
667 }
668
669 #if ENABLE_POLKIT
670 static int bus_message_new_polkit_auth_call_for_varlink(
671 sd_bus *bus,
672 sd_varlink *link,
673 const char *action,
674 const char **details,
675 PolkitFlags flags,
676 sd_bus_message **ret) {
677
678 _cleanup_(sd_bus_message_unrefp) sd_bus_message *c = NULL;
679 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
680 int r;
681
682 assert(bus);
683 assert(link);
684 assert(action);
685 assert(ret);
686
687 r = varlink_get_peer_pidref(link, &pidref);
688 if (r < 0)
689 return r;
690 if (r == 0) /* if we couldn't get a pidfd this returns == 0 */
691 return log_debug_errno(SYNTHETIC_ERRNO(EPERM), "Failed to get peer pidfd, cannot securely authenticate.");
692
693 uid_t uid;
694 r = sd_varlink_get_peer_uid(link, &uid);
695 if (r < 0)
696 return r;
697
698 r = sd_bus_message_new_method_call(
699 bus,
700 &c,
701 "org.freedesktop.PolicyKit1",
702 "/org/freedesktop/PolicyKit1/Authority",
703 "org.freedesktop.PolicyKit1.Authority",
704 "CheckAuthorization");
705 if (r < 0)
706 return r;
707
708 r = sd_bus_message_append(
709 c,
710 "(sa{sv})s",
711 "unix-process", 2,
712 "pidfd", "h", (uint32_t) pidref.fd,
713 "uid", "i", (int32_t) uid,
714 action);
715 if (r < 0)
716 return r;
717
718 r = bus_message_append_strv_key_value(c, details);
719 if (r < 0)
720 return r;
721
722 r = sd_bus_message_append(c, "us", (uint32_t) (flags & _POLKIT_MASK_PUBLIC), NULL);
723 if (r < 0)
724 return r;
725
726 *ret = TAKE_PTR(c);
727 return 0;
728 }
729
730 static bool varlink_allow_interactive_authentication(sd_varlink *link) {
731 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
732 int r;
733
734 assert(link);
735
736 /* We look for the allowInteractiveAuthentication field in the message currently being dispatched,
737 * always under the same name. */
738
739 r = sd_varlink_get_current_parameters(link, &v);
740 if (r < 0) {
741 log_debug_errno(r, "Unable to query current parameters: %m");
742 return false;
743 }
744
745 sd_json_variant *b;
746 b = sd_json_variant_by_key(v, "allowInteractiveAuthentication");
747 if (b) {
748 if (sd_json_variant_is_boolean(b))
749 return sd_json_variant_boolean(b);
750
751 log_debug("Incoming 'allowInteractiveAuthentication' field is not a boolean, ignoring.");
752 }
753
754 return false;
755 }
756 #endif
757
758 int varlink_verify_polkit_async_full(
759 sd_varlink *link,
760 sd_bus *bus,
761 const char *action,
762 const char **details,
763 uid_t good_user,
764 PolkitFlags flags,
765 Hashmap **registry) {
766
767 int r;
768
769 assert(link);
770 assert(registry);
771
772 log_debug("Trying to acquire polkit authentication for '%s'.", action);
773
774 /* This is the same as bus_verify_polkit_async_full(), but authenticates the peer of a varlink
775 * connection rather than the sender of a bus message. */
776
777 r = varlink_check_good_user(link, good_user);
778 if (r != 0)
779 return r;
780
781 #if ENABLE_POLKIT
782 if (!FLAGS_SET(flags, POLKIT_ALWAYS_QUERY)) {
783 #endif
784 r = varlink_check_peer_privilege(link);
785 if (r != 0)
786 return r;
787 #if ENABLE_POLKIT
788 }
789
790 _cleanup_(async_polkit_query_unrefp) AsyncPolkitQuery *q = NULL;
791
792 q = async_polkit_query_ref(hashmap_get(*registry, link));
793 /* This is a repeated invocation of this function, hence let's check if we've already got
794 * a response from polkit for this action */
795 if (q) {
796 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
797 r = async_polkit_query_check_action(q, action, details, flags, &error);
798 if (r != 0)
799 log_debug("Found matching previous polkit authentication for '%s'.", action);
800 if (r < 0) {
801 if (!FLAGS_SET(flags, POLKIT_DONT_REPLY)) {
802 /* Reply with a nice error */
803 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED))
804 (void) sd_varlink_error(link, SD_VARLINK_ERROR_INTERACTIVE_AUTHENTICATION_REQUIRED, NULL);
805 else if (ERRNO_IS_NEG_PRIVILEGE(r))
806 (void) sd_varlink_error(link, SD_VARLINK_ERROR_PERMISSION_DENIED, NULL);
807 }
808
809 return r;
810 }
811 if (r > 0)
812 return r;
813 }
814
815 _cleanup_(sd_bus_unrefp) sd_bus *mybus = NULL;
816 if (!bus) {
817 r = sd_bus_open_system(&mybus);
818 if (r < 0)
819 return r;
820
821 r = sd_bus_attach_event(mybus, sd_varlink_get_event(link), 0);
822 if (r < 0)
823 return r;
824
825 bus = mybus;
826 }
827
828 if (varlink_allow_interactive_authentication(link))
829 flags |= POLKIT_ALLOW_INTERACTIVE;
830
831 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL;
832 r = bus_message_new_polkit_auth_call_for_varlink(bus, link, action, details, flags, &pk);
833 if (r < 0)
834 return r;
835
836 if (!q) {
837 q = new(AsyncPolkitQuery, 1);
838 if (!q)
839 return -ENOMEM;
840
841 *q = (AsyncPolkitQuery) {
842 .n_ref = 1,
843 .link = sd_varlink_ref(link),
844 .bus = sd_bus_ref(bus),
845 };
846 }
847
848 assert(!q->action);
849 q->action = new(AsyncPolkitQueryAction, 1);
850 if (!q->action)
851 return -ENOMEM;
852
853 *q->action = (AsyncPolkitQueryAction) {
854 .action = strdup(action),
855 .details = strv_copy((char**) details),
856 };
857 if (!q->action->action || !q->action->details)
858 return -ENOMEM;
859
860 if (!q->registry) {
861 r = hashmap_ensure_put(registry, &async_polkit_query_hash_ops, link, q);
862 if (r < 0)
863 return r;
864
865 q->registry = *registry;
866 }
867
868 r = sd_bus_call_async(bus, &q->slot, pk, async_polkit_callback, q, 0);
869 if (r < 0)
870 return r;
871
872 TAKE_PTR(q);
873
874 return 0;
875 #else
876 return FLAGS_SET(flags, POLKIT_DEFAULT_ALLOW) ? 1 : -EACCES;
877 #endif
878 }
879
880 bool varlink_has_polkit_action(sd_varlink *link, const char *action, const char **details, Hashmap **registry) {
881 assert(link);
882 assert(action);
883 assert(registry);
884
885 /* Checks if we already have acquired some action previously */
886
887 #if ENABLE_POLKIT
888 AsyncPolkitQuery *q = hashmap_get(*registry, link);
889 if (!q)
890 return false;
891
892 return async_polkit_query_have_action(q, action, details);
893 #else
894 return false;
895 #endif
896 }
897
898 const sd_json_dispatch_field dispatch_table_polkit_only[] = {
899 VARLINK_DISPATCH_POLKIT_FIELD,
900 {}
901 };