summaryrefslogtreecommitdiff
path: root/src/up-polkit.c
blob: 9b8639418acb32a7ce9221f737d2545ed5b2201a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 David Zeuthen <davidz@redhat.com>
 * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <glib.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#include <polkit/polkit.h>

#include "egg-debug.h"

#include "up-polkit.h"
#include "up-daemon.h"

#define UP_POLKIT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_POLKIT, UpPolkitPrivate))

struct UpPolkitPrivate
{
	DBusGConnection		*connection;
	PolkitAuthority         *authority;
};

G_DEFINE_TYPE (UpPolkit, up_polkit, G_TYPE_OBJECT)
static gpointer up_polkit_object = NULL;

/**
 * up_polkit_get_subject:
 **/
PolkitSubject *
up_polkit_get_subject (UpPolkit *polkit, DBusGMethodInvocation *context)
{
	const gchar *sender;
	PolkitSubject *subject;

	sender = dbus_g_method_get_sender (context);
	subject = polkit_system_bus_name_new (sender);

	return subject;
}

/**
 * up_polkit_check_auth:
 **/
gboolean
up_polkit_check_auth (UpPolkit *polkit, PolkitSubject *subject, const gchar *action_id, DBusGMethodInvocation *context)
{
	gboolean ret = FALSE;
	GError *error;
	GError *error_local = NULL;
	PolkitAuthorizationResult *result;

	/* check auth */
	result = polkit_authority_check_authorization_sync (polkit->priv->authority,
							    subject, action_id, NULL,
							    POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
							    NULL, &error_local);
	if (result == NULL) {
		error = g_error_new (UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "failed to check authorisation: %s", error_local->message);
		dbus_g_method_return_error (context, error);
		g_error_free (error_local);
		g_error_free (error);
		goto out;
	}

	/* okay? */
	if (polkit_authorization_result_get_is_authorized (result)) {
		ret = TRUE;
	} else {
		error = g_error_new (UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "not authorized");
		dbus_g_method_return_error (context, error);
		g_error_free (error);
	}
out:
	if (result != NULL)
		g_object_unref (result);
	return ret;
}

/**
 * up_polkit_is_allowed:
 **/
gboolean
up_polkit_is_allowed (UpPolkit *polkit, PolkitSubject *subject, const gchar *action_id, DBusGMethodInvocation *context)
{
	gboolean ret = FALSE;
	GError *error;
	GError *error_local = NULL;
	PolkitAuthorizationResult *result;

	/* check auth */
	result = polkit_authority_check_authorization_sync (polkit->priv->authority,
							    subject, action_id, NULL,
							    POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
							    NULL, &error_local);
	if (result == NULL) {
		error = g_error_new (UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "failed to check authorisation: %s", error_local->message);
		dbus_g_method_return_error (context, error);
		g_error_free (error_local);
		g_error_free (error);
		goto out;
	}

	ret = polkit_authorization_result_get_is_authorized (result) ||
	      polkit_authorization_result_get_is_challenge (result);
out:
	if (result != NULL)
		g_object_unref (result);
	return ret;
}

/**
 * up_polkit_get_uid:
 **/
gboolean
up_polkit_get_uid (UpPolkit *polkit, PolkitSubject *subject, uid_t *uid)
{
	DBusConnection *connection;
	const gchar *name;

	if (!POLKIT_IS_SYSTEM_BUS_NAME (subject)) {
		egg_debug ("not system bus name");
		return FALSE;
	}

	connection = dbus_g_connection_get_connection (polkit->priv->connection);
	name = polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject));
	*uid = dbus_bus_get_unix_user (connection, name, NULL);
	return TRUE;
}

/**
 * up_polkit_get_pid:
 **/
gboolean
up_polkit_get_pid (UpPolkit *polkit, PolkitSubject *subject, pid_t *pid)
{
	gboolean ret = FALSE;
	GError *error = NULL;
	const gchar *name;
	DBusGProxy *proxy = NULL;

	/* bus name? */
	if (!POLKIT_IS_SYSTEM_BUS_NAME (subject)) {
		egg_debug ("not system bus name");
		goto out;
	}

	name = polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject));
	proxy = dbus_g_proxy_new_for_name_owner (polkit->priv->connection,
						 "org.freedesktop.DBus",
						 "/org/freedesktop/DBus/Bus",
						 "org.freedesktop.DBus", &error);
	if (proxy == NULL) {
		egg_warning ("DBUS error: %s", error->message);
		g_error_free (error);
		goto out;
	}

	/* get pid from DBus (quite slow) */
	ret = dbus_g_proxy_call (proxy, "GetConnectionUnixProcessID", &error,
				 G_TYPE_STRING, name,
				 G_TYPE_INVALID,
				 G_TYPE_UINT, pid,
				 G_TYPE_INVALID);
	if (!ret) {
		egg_warning ("failed to get pid: %s", error->message);
		g_error_free (error);
		goto out;
        }
out:
	if (proxy != NULL)
		g_object_unref (proxy);
	return ret;
}

/**
 * up_polkit_finalize:
 **/
static void
up_polkit_finalize (GObject *object)
{
	UpPolkit *polkit;
	g_return_if_fail (UP_IS_POLKIT (object));
	polkit = UP_POLKIT (object);

	if (polkit->priv->connection != NULL)
		dbus_g_connection_unref (polkit->priv->connection);
	g_object_unref (polkit->priv->authority);

	G_OBJECT_CLASS (up_polkit_parent_class)->finalize (object);
}

/**
 * up_polkit_class_init:
 **/
static void
up_polkit_class_init (UpPolkitClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = up_polkit_finalize;
	g_type_class_add_private (klass, sizeof (UpPolkitPrivate));
}

/**
 * up_polkit_init:
 *
 * initializes the polkit class. NOTE: We expect polkit objects
 * to *NOT* be removed or added during the session.
 * We only control the first polkit object if there are more than one.
 **/
static void
up_polkit_init (UpPolkit *polkit)
{
	GError *error = NULL;

	polkit->priv = UP_POLKIT_GET_PRIVATE (polkit);

	polkit->priv->connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
	if (polkit->priv->connection == NULL) {
		if (error != NULL) {
			g_critical ("error getting system bus: %s", error->message);
			g_error_free (error);
		}
		goto out;
	}

#ifdef USE_SECURITY_POLKIT_NEW
	polkit->priv->authority = polkit_authority_get_sync (NULL, &error);
	if (polkit->priv->authority == NULL) {
		g_error ("failed to get pokit authority: %s", error->message);
		g_error_free (error);
		goto out;
	}
#else
	polkit->priv->authority = polkit_authority_get ();
#endif
out:
	return;
}

/**
 * up_polkit_new:
 * Return value: A new polkit class instance.
 **/
UpPolkit *
up_polkit_new (void)
{
	if (up_polkit_object != NULL) {
		g_object_ref (up_polkit_object);
	} else {
		up_polkit_object = g_object_new (UP_TYPE_POLKIT, NULL);
		g_object_add_weak_pointer (up_polkit_object, &up_polkit_object);
	}
	return UP_POLKIT (up_polkit_object);
}