Debian Codegraph @

src/polkit/polkitpermission.c

raw
1 /*
2 * Copyright (C) 2008-2010 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
18 *
19 * Author: Matthias Clasen <[email protected]>
20 * David Zeuthen <[email protected]>
21 */
22
23 #ifdef HAVE_LIBSYSTEMD
24 # include <systemd/sd-login.h>
25 #endif
26
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 #include <gio/gio.h>
31 #include "polkitpermission.h"
32 #include <polkit/polkit.h>
33
34 #include "polkitpermission.h"
35
36 /**
37 * SECTION:polkitpermission
38 * @title: PolkitPermission
39 * @short_description: PolicyKit #GPermission implementation
40 * @stability: Stable
41 *
42 * #PolkitPermission is a #GPermission implementation. It can be used
43 * with e.g. #GtkLockButton. See the #GPermission documentation for
44 * more information.
45 */
46
47 typedef GPermissionClass PolkitPermissionClass;
48
49 /**
50 * PolkitPermission:
51 *
52 * The #PolkitPermission struct should not be accessed directly.
53 */
54 struct _PolkitPermission
55 {
56 GPermission parent_instance;
57
58 PolkitAuthority *authority;
59 PolkitSubject *subject;
60
61 gchar *action_id;
62
63 gchar *session_state;
64
65 /* non-NULL exactly when authorized with a temporary authorization */
66 gchar *tmp_authz_id;
67 };
68
69 enum
70 {
71 PROP_0,
72 PROP_ACTION_ID,
73 PROP_SUBJECT
74 };
75
76 static void process_result (PolkitPermission *permission,
77 PolkitAuthorizationResult *result);
78
79 static char *get_session_state(void);
80
81 static void on_authority_changed (PolkitAuthority *authority,
82 gpointer user_data);
83
84 static void on_sessions_changed (PolkitAuthority *authority,
85 gpointer user_data);
86
87 static gboolean acquire (GPermission *permission,
88 GCancellable *cancellable,
89 GError **error);
90 static void acquire_async (GPermission *permission,
91 GCancellable *cancellable,
92 GAsyncReadyCallback callback,
93 gpointer user_data);
94 static gboolean acquire_finish (GPermission *permission,
95 GAsyncResult *result,
96 GError **error);
97
98 static gboolean release (GPermission *permission,
99 GCancellable *cancellable,
100 GError **error);
101 static void release_async (GPermission *permission,
102 GCancellable *cancellable,
103 GAsyncReadyCallback callback,
104 gpointer user_data);
105 static gboolean release_finish (GPermission *permission,
106 GAsyncResult *result,
107 GError **error);
108
109 static void initable_iface_init (GInitableIface *initable_iface);
110 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
111
112 static gboolean polkit_permission_initable_init (GInitable *initable,
113 GCancellable *cancellable,
114 GError **error);
115
116 G_DEFINE_TYPE_WITH_CODE (PolkitPermission, polkit_permission, G_TYPE_PERMISSION,
117 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
118 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init))
119
120
121 static void
122 polkit_permission_init (PolkitPermission *simple)
123 {
124 }
125
126 static void
127 polkit_permission_constructed (GObject *object)
128 {
129 PolkitPermission *permission = POLKIT_PERMISSION (object);
130
131 if (permission->subject == NULL)
132 permission->subject = polkit_unix_process_new_for_owner (getpid (), 0, getuid ());
133
134 if (G_OBJECT_CLASS (polkit_permission_parent_class)->constructed != NULL)
135 G_OBJECT_CLASS (polkit_permission_parent_class)->constructed (object);
136
137 permission->session_state = get_session_state();
138 }
139
140 static void
141 polkit_permission_finalize (GObject *object)
142 {
143 PolkitPermission *permission = POLKIT_PERMISSION (object);
144
145 g_free (permission->action_id);
146 g_free (permission->tmp_authz_id);
147 g_free (permission->session_state);
148 g_object_unref (permission->subject);
149
150 if (permission->authority != NULL)
151 {
152 g_signal_handlers_disconnect_by_func (permission->authority,
153 on_authority_changed,
154 permission);
155 g_signal_handlers_disconnect_by_func (permission->authority,
156 on_sessions_changed,
157 permission);
158 g_object_unref (permission->authority);
159 }
160
161 if (G_OBJECT_CLASS (polkit_permission_parent_class)->finalize != NULL)
162 G_OBJECT_CLASS (polkit_permission_parent_class)->finalize (object);
163 }
164
165 static void
166 polkit_permission_get_property (GObject *object,
167 guint property_id,
168 GValue *value,
169 GParamSpec *pspec)
170 {
171 PolkitPermission *permission = POLKIT_PERMISSION (object);
172
173 switch (property_id)
174 {
175 case PROP_ACTION_ID:
176 g_value_set_string (value, permission->action_id);
177 break;
178
179 case PROP_SUBJECT:
180 g_value_set_object (value, permission->subject);
181 break;
182
183 default:
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
185 break;
186 }
187 }
188
189 static void
190 polkit_permission_set_property (GObject *object,
191 guint property_id,
192 const GValue *value,
193 GParamSpec *pspec)
194 {
195 PolkitPermission *permission = POLKIT_PERMISSION (object);
196
197 switch (property_id)
198 {
199 case PROP_ACTION_ID:
200 permission->action_id = g_value_dup_string (value);
201 break;
202
203 case PROP_SUBJECT:
204 permission->subject = g_value_dup_object (value);
205 break;
206
207 default:
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
209 break;
210 }
211 }
212
213 static void
214 polkit_permission_class_init (PolkitPermissionClass *class)
215 {
216 GObjectClass *object_class;
217 GPermissionClass *permission_class;
218
219 permission_class = G_PERMISSION_CLASS (class);
220 permission_class->acquire = acquire;
221 permission_class->acquire_async = acquire_async;
222 permission_class->acquire_finish = acquire_finish;
223 permission_class->release = release;
224 permission_class->release_async = release_async;
225 permission_class->release_finish = release_finish;
226
227 object_class = G_OBJECT_CLASS (class);
228 object_class->finalize = polkit_permission_finalize;
229 object_class->constructed = polkit_permission_constructed;
230 object_class->get_property = polkit_permission_get_property;
231 object_class->set_property = polkit_permission_set_property;
232
233 /**
234 * PolkitPermission:action-id:
235 *
236 * The action identifier to use for the permission.
237 */
238 g_object_class_install_property (object_class,
239 PROP_ACTION_ID,
240 g_param_spec_string ("action-id",
241 "Action Identifier",
242 "The action identifier to use for the permission",
243 NULL,
244 G_PARAM_READWRITE |
245 G_PARAM_CONSTRUCT_ONLY |
246 G_PARAM_STATIC_STRINGS));
247 /**
248 * PolkitPermission:subject:
249 *
250 * The #PolkitSubject to use for the permission. If not set during
251 * construction, it will be set to match the current process.
252 */
253 g_object_class_install_property (object_class,
254 PROP_SUBJECT,
255 g_param_spec_object ("subject",
256 "Subject",
257 "The subject to use for the permission",
258 POLKIT_TYPE_SUBJECT,
259 G_PARAM_READWRITE |
260 G_PARAM_CONSTRUCT_ONLY |
261 G_PARAM_STATIC_STRINGS));
262 }
263
264 /**
265 * polkit_permission_new:
266 * @action_id: The PolicyKit action identifier.
267 * @subject: (allow-none): A #PolkitSubject or %NULL for the current process.
268 * @cancellable: (allow-none): A #GCancellable or %NULL.
269 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
270 * @user_data: The data to pass to @callback.
271 *
272 * Creates a #GPermission instance for the PolicyKit action
273 * @action_id.
274 *
275 * When the operation is finished, @callback will be invoked. You can
276 * then call polkit_permission_new_finish() to get the result of the
277 * operation.
278 *
279 * This is a asynchronous failable constructor. See
280 * polkit_permission_new_sync() for the synchronous version.
281 */
282 void
283 polkit_permission_new (const gchar *action_id,
284 PolkitSubject *subject,
285 GCancellable *cancellable,
286 GAsyncReadyCallback callback,
287 gpointer user_data)
288 {
289 g_return_if_fail (action_id != NULL);
290 g_return_if_fail (subject == NULL || POLKIT_IS_SUBJECT (subject));
291 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
292
293 g_async_initable_new_async (POLKIT_TYPE_PERMISSION,
294 G_PRIORITY_DEFAULT,
295 cancellable,
296 callback,
297 user_data,
298 "action-id", action_id,
299 "subject", subject,
300 NULL);
301 }
302
303 /**
304 * polkit_permission_new_finish:
305 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to polkit_permission_new().
306 * @error: (allow-none): Return location for error or %NULL.
307 *
308 * Finishes an operation started with polkit_permission_new().
309 *
310 * Returns: A #GPermission or %NULL if @error is set.
311 */
312 GPermission *
313 polkit_permission_new_finish (GAsyncResult *res,
314 GError **error)
315 {
316 GObject *object;
317 GObject *source_object;
318
319 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
320 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
321
322 source_object = g_async_result_get_source_object (res);
323 g_assert (source_object != NULL);
324 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
325 res,
326 error);
327 g_object_unref (source_object);
328 if (object != NULL)
329 return G_PERMISSION (object);
330 else
331 return NULL;
332 }
333
334 /**
335 * polkit_permission_new_sync:
336 * @action_id: The PolicyKit action identifier.
337 * @subject: (allow-none): A #PolkitSubject or %NULL for the current process.
338 * @cancellable: (allow-none): A #GCancellable or %NULL.
339 * @error: (allow-none): Return location for error or %NULL.
340 *
341 * Creates a #GPermission instance for the PolicyKit action
342 * @action_id.
343 *
344 * This is a synchronous failable constructor. See
345 * polkit_permission_new() for the asynchronous version.
346 *
347 * Returns: A #GPermission or %NULL if @error is set.
348 */
349 GPermission *
350 polkit_permission_new_sync (const gchar *action_id,
351 PolkitSubject *subject,
352 GCancellable *cancellable,
353 GError **error)
354 {
355 g_return_val_if_fail (action_id != NULL, NULL);
356 g_return_val_if_fail (subject == NULL || POLKIT_IS_SUBJECT (subject), NULL);
357 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
358 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
359 return g_initable_new (POLKIT_TYPE_PERMISSION,
360 cancellable,
361 error,
362 "action-id", action_id,
363 "subject", subject,
364 NULL);
365 }
366
367 static void
368 initable_iface_init (GInitableIface *initable_iface)
369 {
370 initable_iface->init = polkit_permission_initable_init;
371 }
372
373 static void
374 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
375 {
376 /* for now, we use default implementation to run GInitable code in a
377 * thread - would probably be nice to have real async version to
378 * avoid the thread-overhead
379 */
380 }
381
382 /* ---------------------------------------------------------------------------------------------------- */
383
384 /**
385 * polkit_permission_get_action_id:
386 * @permission: A #PolkitPermission.
387 *
388 * Gets the PolicyKit action identifier used for @permission.
389 *
390 * Returns: A string owned by @permission. Do not free.
391 */
392 const gchar *
393 polkit_permission_get_action_id (PolkitPermission *permission)
394 {
395 g_return_val_if_fail (POLKIT_IS_PERMISSION (permission), NULL);
396 return permission->action_id;
397 }
398
399 /**
400 * polkit_permission_get_subject:
401 * @permission: A #PolkitPermission.
402 *
403 * Gets the subject used for @permission.
404 *
405 * Returns: (transfer none): An object owned by @permission. Do not free.
406 */
407 PolkitSubject *
408 polkit_permission_get_subject (PolkitPermission *permission)
409 {
410 g_return_val_if_fail (POLKIT_IS_PERMISSION (permission), NULL);
411 return permission->subject;
412 }
413
414 /* ---------------------------------------------------------------------------------------------------- */
415
416 static gboolean
417 polkit_permission_initable_init (GInitable *initable,
418 GCancellable *cancellable,
419 GError **error)
420 {
421 PolkitPermission *permission = POLKIT_PERMISSION (initable);
422 PolkitAuthorizationResult *result;
423 gboolean ret;
424
425 ret = FALSE;
426
427 permission->authority = polkit_authority_get_sync (cancellable, error);
428 if (permission->authority == NULL)
429 goto out;
430
431 g_signal_connect (permission->authority,
432 "changed",
433 G_CALLBACK (on_authority_changed),
434 permission);
435
436 g_signal_connect (permission->authority,
437 "sessions-changed",
438 G_CALLBACK (on_sessions_changed),
439 permission);
440
441 result = polkit_authority_check_authorization_sync (permission->authority,
442 permission->subject,
443 permission->action_id,
444 NULL, /* PolkitDetails */
445 POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
446 cancellable,
447 error);
448 if (result == NULL)
449 goto out;
450
451 process_result (permission, result);
452 g_object_unref (result);
453
454 ret = TRUE;
455
456 out:
457 return ret;
458 }
459
460 /* ---------------------------------------------------------------------------------------------------- */
461
462 static void
463 changed_check_cb (GObject *source_object,
464 GAsyncResult *res,
465 gpointer user_data)
466 {
467 PolkitPermission *permission = POLKIT_PERMISSION (user_data);
468 PolkitAuthorizationResult *result;
469 GError *error;
470
471 error = NULL;
472 result = polkit_authority_check_authorization_finish (permission->authority,
473 res,
474 &error);
475 if (result != NULL)
476 {
477 process_result (permission, result);
478 g_object_unref (result);
479 }
480 else
481 {
482 /* this really should never fail (since we are not passing any
483 * details) so log to stderr if it happens
484 */
485 g_assert (error != NULL);
486 g_warning ("Error checking authorization for action id %s: %s",
487 permission->action_id,
488 error->message);
489 g_error_free (error);
490 }
491 g_object_unref (permission);
492 }
493
494 static char *get_session_state(void)
495 {
496 #ifdef HAVE_LIBSYSTEMD
497 char *session = NULL;
498 char *state = NULL;
499 uid_t uid;
500
501 if ( sd_pid_get_session(getpid(), &session) < 0 )
502 {
503 if ( sd_pid_get_owner_uid(getpid(), &uid) < 0)
504 {
505 goto out;
506 }
507 if (sd_uid_get_display(uid, &session) < 0)
508 {
509 goto out;
510 }
511 }
512
513 if (session != NULL)
514 {
515 sd_session_get_state(session, &state);
516 }
517 out:
518 g_free(session);
519 return state;
520 #else
521 return NULL;
522 #endif
523 }
524
525 static void
526 on_authority_changed (PolkitAuthority *authority,
527 gpointer user_data)
528 {
529 PolkitPermission *permission = POLKIT_PERMISSION (user_data);
530
531 polkit_authority_check_authorization (permission->authority,
532 permission->subject,
533 permission->action_id,
534 NULL, /* PolkitDetails */
535 POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
536 NULL /* cancellable */,
537 changed_check_cb,
538 g_object_ref (permission));
539 }
540
541
542 static void on_sessions_changed (PolkitAuthority *authority,
543 gpointer user_data)
544 {
545 #ifdef HAVE_LIBSYSTEMD
546 char *new_session_state = NULL;
547 char *last_state = NULL;
548
549 PolkitPermission *permission = POLKIT_PERMISSION (user_data);
550
551 new_session_state = get_session_state();
552
553 /* if we cannot tell the session state, we should do CheckAuthorization anyway */
554 if ((new_session_state == NULL) || ( g_strcmp0(new_session_state, permission->session_state) != 0 ))
555 {
556 last_state = permission->session_state;
557 permission->session_state = g_steal_pointer (&new_session_state);
558 g_free(last_state);
559
560 polkit_authority_check_authorization (permission->authority,
561 permission->subject,
562 permission->action_id,
563 NULL, /* PolkitDetails */
564 POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
565 NULL /* cancellable */,
566 changed_check_cb,
567 g_object_ref (permission));
568 }
569
570 g_free (new_session_state);
571 #else
572 on_authority_changed(authority, user_data); /* TODO: resolve the "too many session signals" issue for non-systemd systems later */
573 #endif
574 }
575
576
577 static void
578 process_result (PolkitPermission *permission,
579 PolkitAuthorizationResult *result)
580 {
581 gboolean can_acquire;
582 gboolean can_release;
583 gboolean allowed;
584
585 /* save the temporary authorization id */
586 g_free (permission->tmp_authz_id);
587 permission->tmp_authz_id = g_strdup (polkit_authorization_result_get_temporary_authorization_id (result));
588 allowed = polkit_authorization_result_get_is_authorized (result);
589 if (permission->tmp_authz_id != NULL)
590 {
591 can_acquire = FALSE;
592 can_release = TRUE;
593 }
594 else
595 {
596 if (allowed)
597 can_acquire = FALSE;
598 else
599 can_acquire = polkit_authorization_result_get_retains_authorization (result);
600 can_release = FALSE;
601 }
602 g_permission_impl_update (G_PERMISSION (permission), allowed, can_acquire, can_release);
603 }
604
605 /* ---------------------------------------------------------------------------------------------------- */
606
607 typedef struct
608 {
609 PolkitPermission *permission;
610 GSimpleAsyncResult *simple;
611 } AcquireData;
612
613 static void
614 acquire_data_free (AcquireData *data)
615 {
616 g_object_unref (data->simple);
617 g_free (data);
618 }
619
620 static void
621 acquire_cb (GObject *source_object,
622 GAsyncResult *res,
623 gpointer user_data)
624 {
625 AcquireData *data = user_data;
626 PolkitAuthorizationResult *result;
627 GError *error;
628
629 error = NULL;
630 result = polkit_authority_check_authorization_finish (data->permission->authority,
631 res,
632 &error);
633 if (result != NULL)
634 {
635 /* Process the result such that allowed, can_acquire and
636 * can_release are updated before returning to the user - see
637 * also release_cb for where we do this as well
638 */
639 process_result (data->permission, result);
640 if (!polkit_authorization_result_get_is_authorized (result))
641 {
642 if (polkit_authorization_result_get_dismissed (result))
643 {
644 g_simple_async_result_set_error (data->simple,
645 G_IO_ERROR,
646 G_IO_ERROR_CANCELLED,
647 "User dismissed authentication dialog while trying to acquire permission for action-id %s",
648 data->permission->action_id);
649 }
650 else
651 {
652 g_simple_async_result_set_error (data->simple,
653 POLKIT_ERROR,
654 POLKIT_ERROR_FAILED,
655 "Failed to acquire permission for action-id %s",
656 data->permission->action_id);
657 }
658 }
659 g_object_unref (result);
660 }
661 else
662 {
663 g_simple_async_result_set_from_error (data->simple, error);
664 g_error_free (error);
665 }
666 /* don't complete in idle since we're already completing in idle
667 * due to how PolkitAuthority works
668 */
669 g_simple_async_result_complete (data->simple);
670 acquire_data_free (data);
671 }
672
673 static void
674 acquire_async (GPermission *gpermission,
675 GCancellable *cancellable,
676 GAsyncReadyCallback callback,
677 gpointer user_data)
678 {
679 PolkitPermission *permission = POLKIT_PERMISSION (gpermission);
680 AcquireData *data;
681
682 data = g_new0 (AcquireData, 1);
683 data->permission = permission;
684 data->simple = g_simple_async_result_new (G_OBJECT (permission),
685 callback,
686 user_data,
687 acquire_async);
688
689 polkit_authority_check_authorization (permission->authority,
690 permission->subject,
691 permission->action_id,
692 NULL, /* PolkitDetails */
693 POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
694 cancellable,
695 acquire_cb,
696 data);
697 }
698
699 static gboolean
700 acquire_finish (GPermission *gpermission,
701 GAsyncResult *result,
702 GError **error)
703 {
704 GSimpleAsyncResult *simple;
705
706 simple = G_SIMPLE_ASYNC_RESULT (result);
707 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == acquire_async);
708
709 if (g_simple_async_result_propagate_error (simple, error))
710 return FALSE;
711
712 return TRUE;
713 }
714
715 static gboolean
716 acquire (GPermission *gpermission,
717 GCancellable *cancellable,
718 GError **error)
719 {
720 PolkitPermission *permission = POLKIT_PERMISSION (gpermission);
721 PolkitAuthorizationResult *result;
722 gboolean ret;
723
724 ret = FALSE;
725
726 result = polkit_authority_check_authorization_sync (permission->authority,
727 permission->subject,
728 permission->action_id,
729 NULL, /* PolkitDetails */
730 POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
731 cancellable,
732 error);
733 if (result != NULL)
734 {
735 /* need to update allowed, can_acquire, can_release before returning to the user */
736 process_result (permission, result);
737 if (polkit_authorization_result_get_is_authorized (result))
738 {
739 ret = TRUE;
740 }
741 else if (polkit_authorization_result_get_dismissed (result))
742 {
743 g_set_error (error,
744 G_IO_ERROR,
745 G_IO_ERROR_CANCELLED,
746 "User dismissed authentication dialog while trying to acquire permission for action-id %s",
747 permission->action_id);
748 }
749 else
750 {
751 g_set_error (error,
752 POLKIT_ERROR,
753 POLKIT_ERROR_FAILED,
754 "Failed to acquire permission for action-id %s",
755 permission->action_id);
756 }
757 g_object_unref (result);
758 }
759
760 return ret;
761 }
762
763 /* ---------------------------------------------------------------------------------------------------- */
764
765 typedef struct
766 {
767 PolkitPermission *permission;
768 GSimpleAsyncResult *simple;
769 } ReleaseData;
770
771 static void
772 release_data_free (ReleaseData *data)
773 {
774 g_object_unref (data->simple);
775 g_free (data);
776 }
777
778 static void
779 release_check_cb (GObject *source_object,
780 GAsyncResult *res,
781 gpointer user_data)
782 {
783 ReleaseData *data = user_data;
784 PolkitAuthorizationResult *result;
785 GError *error;
786
787 error = NULL;
788 result = polkit_authority_check_authorization_finish (data->permission->authority,
789 res,
790 &error);
791 if (result == NULL)
792 {
793 g_prefix_error (&error,
794 "Error checking authorization for action id %s after releasing the permission: ",
795 data->permission->action_id);
796 g_simple_async_result_set_from_error (data->simple, error);
797 g_error_free (error);
798 }
799 else
800 {
801 process_result (data->permission, result);
802 g_object_unref (result);
803 }
804 /* don't complete in idle since we're already completing in idle
805 * due to how PolkitAuthority works
806 */
807 g_simple_async_result_complete (data->simple);
808 release_data_free (data);
809 }
810
811 static void
812 release_cb (GObject *source_object,
813 GAsyncResult *res,
814 gpointer user_data)
815 {
816 ReleaseData *data = user_data;
817 GError *error;
818 gboolean ret;
819
820 ret = FALSE;
821
822 error = NULL;
823 ret = polkit_authority_revoke_temporary_authorization_by_id_finish (data->permission->authority,
824 res,
825 &error);
826 if (!ret)
827 {
828 g_simple_async_result_set_from_error (data->simple, error);
829 g_error_free (error);
830 /* don't complete in idle since we're already completing in idle
831 * due to how PolkitAuthority works
832 */
833 g_simple_async_result_complete (data->simple);
834 release_data_free (data);
835 }
836 else
837 {
838 /* need to update allowed, can_acquire and can_release before
839 * returning to the user - see also acquire_cb where we do this
840 * as well
841 */
842 polkit_authority_check_authorization (data->permission->authority,
843 data->permission->subject,
844 data->permission->action_id,
845 NULL, /* PolkitDetails */
846 POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
847 NULL /* cancellable */,
848 release_check_cb,
849 data);
850 }
851 }
852
853 static void
854 release_async (GPermission *gpermission,
855 GCancellable *cancellable,
856 GAsyncReadyCallback callback,
857 gpointer user_data)
858 {
859 PolkitPermission *permission = POLKIT_PERMISSION (gpermission);
860 ReleaseData *data;
861
862 data = g_new0 (ReleaseData, 1);
863 data->permission = permission;
864 data->simple = g_simple_async_result_new (G_OBJECT (permission),
865 callback,
866 user_data,
867 release_async);
868
869 if (permission->tmp_authz_id == NULL)
870 {
871 g_simple_async_result_set_error (data->simple,
872 POLKIT_ERROR,
873 POLKIT_ERROR_FAILED,
874 "Cannot release permission: no temporary authorization for action-id %s exist",
875 permission->action_id);
876 g_simple_async_result_complete_in_idle (data->simple);
877 release_data_free (data);
878 goto out;
879 }
880
881 polkit_authority_revoke_temporary_authorization_by_id (permission->authority,
882 permission->tmp_authz_id,
883 cancellable,
884 release_cb,
885 data);
886 out:
887 ;
888 }
889
890 static gboolean
891 release_finish (GPermission *gpermission,
892 GAsyncResult *result,
893 GError **error)
894 {
895 GSimpleAsyncResult *simple;
896
897 simple = G_SIMPLE_ASYNC_RESULT (result);
898 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == release_async);
899
900 if (g_simple_async_result_propagate_error (simple, error))
901 return FALSE;
902
903 return TRUE;
904 }
905
906 static gboolean
907 release (GPermission *gpermission,
908 GCancellable *cancellable,
909 GError **error)
910 {
911 PolkitPermission *permission = POLKIT_PERMISSION (gpermission);
912 PolkitAuthorizationResult *result;
913 gboolean ret;
914
915 ret = FALSE;
916
917 if (permission->tmp_authz_id == NULL)
918 {
919 g_set_error (error,
920 POLKIT_ERROR,
921 POLKIT_ERROR_FAILED,
922 "Cannot release permission: no temporary authorization for action-id %s exist",
923 permission->action_id);
924 goto out;
925 }
926
927 ret = polkit_authority_revoke_temporary_authorization_by_id_sync (permission->authority,
928 permission->tmp_authz_id,
929 cancellable,
930 error);
931 if (!ret)
932 goto out;
933
934 /* need to update allowed, can_acquire, can_release before returning to the user */
935 result = polkit_authority_check_authorization_sync (permission->authority,
936 permission->subject,
937 permission->action_id,
938 NULL, /* PolkitDetails */
939 POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
940 cancellable,
941 error);
942 if (result == NULL)
943 goto out;
944 process_result (permission, result);
945 g_object_unref (result);
946
947 out:
948 return ret;
949 }
950
951 /* ---------------------------------------------------------------------------------------------------- */