1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2008 David Zeuthen <davidz@redhat.com>
4
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
6
* Licensed under the GNU General Public License Version 2
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28
#include <dbus/dbus-glib.h>
29
#include <dbus/dbus-glib-lowlevel.h>
31
#include <polkit/polkit.h>
33
#include "egg-debug.h"
35
#include "up-polkit.h"
36
#include "up-daemon.h"
38
#define UP_POLKIT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_POLKIT, UpPolkitPrivate))
40
struct UpPolkitPrivate
42
DBusGConnection *connection;
43
PolkitAuthority *authority;
46
G_DEFINE_TYPE (UpPolkit, up_polkit, G_TYPE_OBJECT)
47
static gpointer up_polkit_object = NULL;
50
* up_polkit_get_subject:
53
up_polkit_get_subject (UpPolkit *polkit, DBusGMethodInvocation *context)
56
PolkitSubject *subject;
58
sender = dbus_g_method_get_sender (context);
59
subject = polkit_system_bus_name_new (sender);
65
* up_polkit_check_auth:
68
up_polkit_check_auth (UpPolkit *polkit, PolkitSubject *subject, const gchar *action_id, DBusGMethodInvocation *context)
73
PolkitAuthorizationResult *result;
76
result = polkit_authority_check_authorization_sync (polkit->priv->authority, subject, action_id, NULL, POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, NULL, &error_local);
78
error = g_error_new (UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "failed to check authorisation: %s", error_local->message);
79
dbus_g_method_return_error (context, error);
80
g_error_free (error_local);
86
if (polkit_authorization_result_get_is_authorized (result)) {
89
error = g_error_new (UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "not authorized");
90
dbus_g_method_return_error (context, error);
95
g_object_unref (result);
100
* up_polkit_is_allowed:
103
up_polkit_is_allowed (UpPolkit *polkit, PolkitSubject *subject, const gchar *action_id, DBusGMethodInvocation *context)
105
gboolean ret = FALSE;
108
PolkitAuthorizationResult *result;
111
result = polkit_authority_check_authorization_sync (polkit->priv->authority, subject, action_id, NULL, POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE, NULL, &error_local);
112
if (result == NULL) {
113
error = g_error_new (UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "failed to check authorisation: %s", error_local->message);
114
dbus_g_method_return_error (context, error);
115
g_error_free (error_local);
116
g_error_free (error);
120
ret = polkit_authorization_result_get_is_authorized (result) ||
121
polkit_authorization_result_get_is_challenge (result);
124
g_object_unref (result);
132
up_polkit_get_uid (UpPolkit *polkit, PolkitSubject *subject, uid_t *uid)
134
DBusConnection *connection;
137
if (!POLKIT_IS_SYSTEM_BUS_NAME (subject)) {
138
egg_debug ("not system bus name");
142
connection = dbus_g_connection_get_connection (polkit->priv->connection);
143
name = polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject));
144
*uid = dbus_bus_get_unix_user (connection, name, NULL);
152
up_polkit_get_pid (UpPolkit *polkit, PolkitSubject *subject, pid_t *pid)
154
gboolean ret = FALSE;
155
GError *error = NULL;
157
DBusGProxy *proxy = NULL;
160
if (!POLKIT_IS_SYSTEM_BUS_NAME (subject)) {
161
egg_debug ("not system bus name");
165
name = polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject));
166
proxy = dbus_g_proxy_new_for_name_owner (polkit->priv->connection,
167
"org.freedesktop.DBus",
168
"/org/freedesktop/DBus/Bus",
169
"org.freedesktop.DBus", &error);
171
egg_warning ("DBUS error: %s", error->message);
172
g_error_free (error);
176
/* get pid from DBus (quite slow) */
177
ret = dbus_g_proxy_call (proxy, "GetConnectionUnixProcessID", &error,
183
egg_warning ("failed to get pid: %s", error->message);
184
g_error_free (error);
189
g_object_unref (proxy);
194
* up_polkit_finalize:
197
up_polkit_finalize (GObject *object)
200
g_return_if_fail (UP_IS_POLKIT (object));
201
polkit = UP_POLKIT (object);
203
if (polkit->priv->connection != NULL)
204
dbus_g_connection_unref (polkit->priv->connection);
205
g_object_unref (polkit->priv->authority);
207
G_OBJECT_CLASS (up_polkit_parent_class)->finalize (object);
211
* up_polkit_class_init:
214
up_polkit_class_init (UpPolkitClass *klass)
216
GObjectClass *object_class = G_OBJECT_CLASS (klass);
217
object_class->finalize = up_polkit_finalize;
218
g_type_class_add_private (klass, sizeof (UpPolkitPrivate));
224
* initializes the polkit class. NOTE: We expect polkit objects
225
* to *NOT* be removed or added during the session.
226
* We only control the first polkit object if there are more than one.
229
up_polkit_init (UpPolkit *polkit)
231
GError *error = NULL;
233
polkit->priv = UP_POLKIT_GET_PRIVATE (polkit);
235
polkit->priv->connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
236
if (polkit->priv->connection == NULL) {
238
g_critical ("error getting system bus: %s", error->message);
239
g_error_free (error);
243
polkit->priv->authority = polkit_authority_get ();
250
* Return value: A new polkit class instance.
255
if (up_polkit_object != NULL) {
256
g_object_ref (up_polkit_object);
258
up_polkit_object = g_object_new (UP_TYPE_POLKIT, NULL);
259
g_object_add_weak_pointer (up_polkit_object, &up_polkit_object);
261
return UP_POLKIT (up_polkit_object);
264
/***************************************************************************
265
*** MAKE CHECK TESTS ***
266
***************************************************************************/
268
#include "egg-test.h"
271
up_polkit_test (gpointer user_data)
273
EggTest *test = (EggTest *) user_data;
276
if (!egg_test_start (test, "UpPolkit"))
279
/************************************************************/
280
egg_test_title (test, "get instance");
281
polkit = up_polkit_new ();
282
egg_test_assert (test, polkit != NULL);
285
g_object_unref (polkit);