| 1 |
/* |
| 2 |
* Copyright (C) 2009 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: David Zeuthen <[email protected]> |
| 20 |
*/ |
| 21 |
|
| 22 |
/* Simple example that shows how to check for an authorization |
| 23 |
* including cancelling the check. |
| 24 |
* |
| 25 |
* Cancelling an authorization check is desirable in situations where |
| 26 |
* the object/action to check for vanishes. |
| 27 |
* |
| 28 |
* One concrete example of this is a disks service in which the user |
| 29 |
* needs to authenticate to modify a disk. If the disk is removed |
| 30 |
* while the authentication dialog is shown, the disks service should |
| 31 |
* cancel the authorization check. A side effect of this, is that the |
| 32 |
* authentication dialog is removed. |
| 33 |
*/ |
| 34 |
|
| 35 |
#include <polkit/polkit.h> |
| 36 |
|
| 37 |
static gboolean |
| 38 |
on_tensec_timeout (gpointer user_data) |
| 39 |
{ |
| 40 |
GMainLoop *loop = user_data; |
| 41 |
g_print ("Ten seconds has passed. Now exiting.\n"); |
| 42 |
g_main_loop_quit (loop); |
| 43 |
return FALSE; |
| 44 |
} |
| 45 |
|
| 46 |
static void |
| 47 |
check_authorization_cb (PolkitAuthority *authority, |
| 48 |
GAsyncResult *res, |
| 49 |
gpointer user_data) |
| 50 |
{ |
| 51 |
GMainLoop *loop = user_data; |
| 52 |
PolkitAuthorizationResult *result; |
| 53 |
GError *error; |
| 54 |
|
| 55 |
error = NULL; |
| 56 |
result = polkit_authority_check_authorization_finish (authority, res, &error); |
| 57 |
if (error != NULL) |
| 58 |
{ |
| 59 |
g_print ("Error checking authorization: %s\n", error->message); |
| 60 |
g_error_free (error); |
| 61 |
} |
| 62 |
else |
| 63 |
{ |
| 64 |
const gchar *result_str; |
| 65 |
if (polkit_authorization_result_get_is_authorized (result)) |
| 66 |
{ |
| 67 |
result_str = "authorized"; |
| 68 |
} |
| 69 |
else if (polkit_authorization_result_get_is_challenge (result)) |
| 70 |
{ |
| 71 |
result_str = "challenge"; |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
result_str = "not authorized"; |
| 76 |
} |
| 77 |
|
| 78 |
g_print ("Authorization result: %s\n", result_str); |
| 79 |
} |
| 80 |
|
| 81 |
g_print ("Authorization check has been cancelled and the dialog should now be hidden.\n" |
| 82 |
"This process will exit in ten seconds.\n"); |
| 83 |
g_timeout_add (10000, on_tensec_timeout, loop); |
| 84 |
} |
| 85 |
|
| 86 |
static gboolean |
| 87 |
do_cancel (GCancellable *cancellable) |
| 88 |
{ |
| 89 |
g_print ("Timer has expired; cancelling authorization check\n"); |
| 90 |
g_cancellable_cancel (cancellable); |
| 91 |
return FALSE; |
| 92 |
} |
| 93 |
|
| 94 |
int |
| 95 |
main (int argc, char *argv[]) |
| 96 |
{ |
| 97 |
pid_t parent_pid; |
| 98 |
const gchar *action_id; |
| 99 |
GMainLoop *loop; |
| 100 |
PolkitSubject *subject; |
| 101 |
PolkitAuthority *authority; |
| 102 |
GCancellable *cancellable; |
| 103 |
|
| 104 |
if (argc != 2) |
| 105 |
{ |
| 106 |
g_printerr ("usage: %s <action_id>\n", argv[0]); |
| 107 |
return 1; |
| 108 |
} |
| 109 |
action_id = argv[1]; |
| 110 |
|
| 111 |
loop = g_main_loop_new (NULL, FALSE); |
| 112 |
|
| 113 |
authority = polkit_authority_get_sync (NULL, NULL); |
| 114 |
|
| 115 |
/* Typically mechanisms will use a PolkitSystemBusName since most |
| 116 |
* clients communicate with the mechanism via D-Bus. However for |
| 117 |
* this simple example we use the process id of the calling process. |
| 118 |
* |
| 119 |
* Note that if the parent was reaped we have to be careful not to |
| 120 |
* check if init(1) is authorized (it always is). |
| 121 |
*/ |
| 122 |
parent_pid = getppid (); |
| 123 |
if (parent_pid == 1) |
| 124 |
{ |
| 125 |
g_printerr ("Parent process was reaped by init(1)\n"); |
| 126 |
return 1; |
| 127 |
} |
| 128 |
subject = polkit_unix_process_new_for_owner (parent_pid, 0, getuid ()); |
| 129 |
|
| 130 |
cancellable = g_cancellable_new (); |
| 131 |
|
| 132 |
g_print ("Will cancel authorization check in 10 seconds\n"); |
| 133 |
|
| 134 |
/* Set up a 10 second timer to cancel the check */ |
| 135 |
g_timeout_add (10 * 1000, |
| 136 |
(GSourceFunc) do_cancel, |
| 137 |
cancellable); |
| 138 |
|
| 139 |
polkit_authority_check_authorization (authority, |
| 140 |
subject, |
| 141 |
action_id, |
| 142 |
NULL, /* PolkitDetails */ |
| 143 |
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 144 |
cancellable, |
| 145 |
(GAsyncReadyCallback) check_authorization_cb, |
| 146 |
loop); |
| 147 |
|
| 148 |
g_main_loop_run (loop); |
| 149 |
|
| 150 |
g_object_unref (authority); |
| 151 |
g_object_unref (subject); |
| 152 |
g_object_unref (cancellable); |
| 153 |
g_main_loop_unref (loop); |
| 154 |
|
| 155 |
return 0; |
| 156 |
} |