summaryrefslogtreecommitdiff
path: root/src/pk-security-polkit.c
blob: 30744afbf60467dafc770e04fd298bd712254efa (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

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

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>

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

#include <polkit/polkit.h>
#include <polkit-dbus/polkit-dbus.h>

#include <pk-enum.h>
#include <pk-common.h>

#include "pk-debug.h"
#include "pk-security.h"

#define PK_SECURITY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PK_TYPE_SECURITY, PkSecurityPrivate))

struct PkSecurityPrivate
{
	PolKitContext		*pk_context;
	DBusConnection		*connection;
};

G_DEFINE_TYPE (PkSecurity, pk_security, G_TYPE_OBJECT)
static gpointer pk_security_object = NULL;

/**
 * pk_security_can_do_action:
 **/
G_GNUC_WARN_UNUSED_RESULT static PolKitResult
pk_security_can_do_action (PkSecurity *security, const gchar *dbus_sender, const gchar *action)
{
	PolKitResult pk_result;
	PolKitAction *pk_action;
	PolKitCaller *pk_caller;
	DBusError dbus_error;

	/* set action */
	pk_action = polkit_action_new ();
	if (pk_action == NULL) {
		pk_warning ("error: polkit_action_new failed");
		return POLKIT_RESULT_NO;
	}
	polkit_action_set_action_id (pk_action, action);

	/* set caller */
	pk_debug ("using caller %s for action %s", dbus_sender, action);
	dbus_error_init (&dbus_error);
	pk_caller = polkit_caller_new_from_dbus_name (security->priv->connection, dbus_sender, &dbus_error);
	if (pk_caller == NULL) {
		if (dbus_error_is_set (&dbus_error)) {
			pk_warning ("error: polkit_caller_new_from_dbus_name(): %s: %s\n",
				    dbus_error.name, dbus_error.message);
			dbus_error_free (&dbus_error);
		}
		return POLKIT_RESULT_NO;
	}

	pk_result = polkit_context_is_caller_authorized (security->priv->pk_context, pk_action, pk_caller, TRUE, NULL);
	pk_debug ("PolicyKit result = '%s'", polkit_result_to_string_representation (pk_result));

	polkit_action_unref (pk_action);
	polkit_caller_unref (pk_caller);

	return pk_result;
}

/**
 * pk_security_role_to_action:
 **/
static const gchar *
pk_security_role_to_action (PkSecurity *security, gboolean trusted, PkRoleEnum role)
{
	const gchar *policy = NULL;

	g_return_val_if_fail (security != NULL, NULL);
	g_return_val_if_fail (PK_IS_SECURITY (security), NULL);

	if (role == PK_ROLE_ENUM_UPDATE_PACKAGES ||
	    role == PK_ROLE_ENUM_UPDATE_SYSTEM) {
		policy = "org.freedesktop.packagekit.system-update";
	} else if (role == PK_ROLE_ENUM_INSTALL_SIGNATURE) {
		policy = "org.freedesktop.packagekit.system-trust-signing-key";
	} else if (role == PK_ROLE_ENUM_ROLLBACK) {
		policy = "org.freedesktop.packagekit.system-rollback";
	} else if (role == PK_ROLE_ENUM_REPO_ENABLE ||
		   role == PK_ROLE_ENUM_REPO_SET_DATA) {
		policy = "org.freedesktop.packagekit.systems-sources-configure";
	} else if (role == PK_ROLE_ENUM_REFRESH_CACHE) {
		policy = "org.freedesktop.packagekit.system-sources-refresh";
	} else if (role == PK_ROLE_ENUM_SET_PROXY_PRIVATE) {
		policy = "org.freedesktop.packagekit.system-network-proxy-configure";
	} else if (role == PK_ROLE_ENUM_REMOVE_PACKAGES) {
		policy = "org.freedesktop.packagekit.package-remove";
	} else if (role == PK_ROLE_ENUM_INSTALL_PACKAGES) {
		policy = "org.freedesktop.packagekit.package-install";
	} else if (role == PK_ROLE_ENUM_INSTALL_FILES && trusted) {
		policy = "org.freedesktop.packagekit.package-install";
	} else if (role == PK_ROLE_ENUM_INSTALL_FILES && !trusted) {
		policy = "org.freedesktop.packagekit.package-install-untrusted";
	} else if (role == PK_ROLE_ENUM_ACCEPT_EULA) {
		policy = "org.freedesktop.packagekit.package-eula-accept";
	}
	return policy;
}

/**
 * pk_security_action_is_allowed:
 *
 * Only valid from an async caller, which is fine, as we won't prompt the user
 * when not async.
 **/
gboolean
pk_security_action_is_allowed (PkSecurity *security, const gchar *dbus_sender,
			       gboolean trusted, PkRoleEnum role, gchar **error_detail)
{
	PolKitResult pk_result;
	const gchar *policy;

	g_return_val_if_fail (PK_IS_SECURITY (security), FALSE);
	g_return_val_if_fail (dbus_sender != NULL, FALSE);

	/* map the roles to policykit rules */
	policy = pk_security_role_to_action (security, trusted, role);
	if (policy == NULL) {
		pk_warning ("policykit type required for '%s'", pk_role_enum_to_text (role));
		return FALSE;
	}

	/* get the dbus sender */
	pk_result = pk_security_can_do_action (security, dbus_sender, policy);
	if (pk_result != POLKIT_RESULT_YES) {
		if (error_detail != NULL) {
			*error_detail = g_strdup_printf ("%s %s", policy, polkit_result_to_string_representation (pk_result));
		}
		return FALSE;
	}
	return TRUE;
}

/**
 * pk_security_finalize:
 **/
static void
pk_security_finalize (GObject *object)
{
	PkSecurity *security;
	g_return_if_fail (PK_IS_SECURITY (object));
	security = PK_SECURITY (object);

	/* unref PolicyKit */
	polkit_context_unref (security->priv->pk_context);

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

/**
 * pk_security_class_init:
 **/
static void
pk_security_class_init (PkSecurityClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = pk_security_finalize;
	g_type_class_add_private (klass, sizeof (PkSecurityPrivate));
}

/**
 * pk_security_io_watch_have_data:
 **/
static gboolean
pk_security_io_watch_have_data (GIOChannel *channel, GIOCondition condition, gpointer user_data)
{
	int fd;
	PolKitContext *pk_context = user_data;
	fd = g_io_channel_unix_get_fd (channel);
	polkit_context_io_func (pk_context, fd);
	return TRUE;
}

/**
 * pk_security_io_add_watch:
 **/
static int
pk_security_io_add_watch (PolKitContext *pk_context, int fd)
{
	guint id = 0;
	GIOChannel *channel;
	channel = g_io_channel_unix_new (fd);
	if (channel == NULL) {
		return id;
	}
	id = g_io_add_watch (channel, G_IO_IN, pk_security_io_watch_have_data, pk_context);
	if (id == 0) {
		g_io_channel_unref (channel);
		return id;
	}
	g_io_channel_unref (channel);
	return id;
}

/**
 * pk_security_io_remove_watch:
 **/
static void
pk_security_io_remove_watch (PolKitContext *pk_context, int watch_id)
{
	g_source_remove (watch_id);
}

/**
 * pk_security_init:
 *
 * initializes the security class. NOTE: We expect security objects
 * to *NOT* be removed or added during the session.
 * We only control the first security object if there are more than one.
 **/
static void
pk_security_init (PkSecurity *security)
{
	PolKitError *pk_error;
	polkit_bool_t retval;
	DBusError dbus_error;

	security->priv = PK_SECURITY_GET_PRIVATE (security);

	pk_debug ("Using PolicyKit security framework");

	/* get a connection to the bus */
	dbus_error_init (&dbus_error);
	security->priv->connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error);
	if (security->priv->connection == NULL) {
		if (dbus_error_is_set (&dbus_error)) {
			pk_warning ("failed to get system connection %s: %s\n", dbus_error.name, dbus_error.message);
			dbus_error_free (&dbus_error);
		}
	}

	/* get PolicyKit context */
	security->priv->pk_context = polkit_context_new ();

	/* watch for changes */
	polkit_context_set_io_watch_functions (security->priv->pk_context,
					       pk_security_io_add_watch,
					       pk_security_io_remove_watch);

	pk_error = NULL;
	retval = polkit_context_init (security->priv->pk_context, &pk_error);
	if (retval == FALSE) {
		pk_warning ("Could not init PolicyKit context: %s", polkit_error_get_error_message (pk_error));
		polkit_error_free (pk_error);
	}
}

/**
 * pk_security_new:
 * Return value: A new security class instance.
 **/
PkSecurity *
pk_security_new (void)
{
	if (pk_security_object != NULL) {
		g_object_ref (pk_security_object);
	} else {
		pk_security_object = g_object_new (PK_TYPE_SECURITY, NULL);
		g_object_add_weak_pointer (pk_security_object, &pk_security_object);
	}
	return PK_SECURITY (pk_security_object);
}

/***************************************************************************
 ***                          MAKE CHECK TESTS                           ***
 ***************************************************************************/
#ifdef PK_BUILD_TESTS
#include <libselftest.h>

void
libst_security (LibSelfTest *test)
{
	PkSecurity *security;
	const gchar *action;
	gboolean ret;
	gchar *error;

	if (libst_start (test, "PkSecurity", CLASS_AUTO) == FALSE) {
		return;
	}

	/************************************************************/
	libst_title (test, "get an instance");
	security = pk_security_new ();
	if (security != NULL) {
		libst_success (test, NULL);
	} else {
		libst_failed (test, NULL);
	}

	/************************************************************/
	libst_title (test, "check connection");
	if (security->priv->connection != NULL) {
		libst_success (test, NULL);
	} else {
		libst_failed (test, NULL);
	}

	/************************************************************/
	libst_title (test, "check PolKit context");
	if (security->priv->pk_context != NULL) {
		libst_success (test, NULL);
	} else {
		libst_failed (test, NULL);
	}

	/************************************************************/
	libst_title (test, "map valid role to action");
	action = pk_security_role_to_action (security, FALSE, PK_ROLE_ENUM_UPDATE_PACKAGES);
	if (pk_strequal (action, "org.freedesktop.packagekit.update-package")) {
		libst_success (test, NULL, error);
	} else {
		libst_failed (test, "did not get correct action '%s'", action);
	}

	/************************************************************/
	libst_title (test, "map invalid role to action");
	action = pk_security_role_to_action (security, FALSE, PK_ROLE_ENUM_SEARCH_NAME);
	if (action == NULL) {
		libst_success (test, NULL, error);
	} else {
		libst_failed (test, "did not get correct action '%s'", action);
	}

	/************************************************************/
	libst_title (test, "get the default backend");
	error = NULL;
	ret = pk_security_action_is_allowed (security, ":0", FALSE, PK_ROLE_ENUM_UPDATE_PACKAGES, &error);
	if (ret == FALSE) {
		libst_success (test, "did not authenticate update-package, error '%s'", error);
	} else {
		libst_failed (test, "authenticated update-package!");
	}
	g_free (error);

	g_object_unref (security);

	libst_end (test);
}
#endif