summaryrefslogtreecommitdiff
path: root/system-settings/src/nm-polkit-helpers.c
blob: 7ac336c80791941ba6d22138f19a2d6e28ce6a1c (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
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */

#include "nm-polkit-helpers.h"
#include <nm-dbus-settings.h>

GQuark
nm_sysconfig_settings_error_quark (void)
{
	static GQuark ret = 0;

	if (ret == 0)
		ret = g_quark_from_static_string ("nm_sysconfig_settings_error");

	return ret;
}

#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }

GType
nm_sysconfig_settings_error_get_type (void)
{
	static GType etype = 0;

	if (etype == 0) {
		static const GEnumValue values[] = {
			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_GENERAL, "GeneralError"),
			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_NOT_PRIVILEGED, "NotPrivileged"),
			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_INVALID_CONNECTION, "InvalidConnection"),
			{ 0, 0, 0 }
		};

		etype = g_enum_register_static ("NMSysconfigSettingsError", values);
	}

	return etype;
}

static gboolean
pk_io_watch_have_data (GIOChannel *channel, GIOCondition condition, gpointer user_data)
{
	int fd;
	PolKitContext *pk_context = (PolKitContext *) user_data;

	fd = g_io_channel_unix_get_fd (channel);
	polkit_context_io_func (pk_context, fd);
	
	return TRUE;
}

static int
pk_io_add_watch (PolKitContext *pk_context, int fd)
{
	guint id = 0;
	GIOChannel *channel;
	
	channel = g_io_channel_unix_new (fd);
	if (channel == NULL)
		goto out;
	id = g_io_add_watch (channel, G_IO_IN, pk_io_watch_have_data, pk_context);
	if (id == 0) {
		g_io_channel_unref (channel);
		goto out;
	}
	g_io_channel_unref (channel);

 out:
	return id;
}

static void
pk_io_remove_watch (PolKitContext *pk_context, int watch_id)
{
	g_source_remove (watch_id);
}

PolKitContext *
create_polkit_context (void)
{
	static PolKitContext *global_context = NULL;
	PolKitError *err;

	if (G_LIKELY (global_context))
		return polkit_context_ref (global_context);

	global_context = polkit_context_new ();
	polkit_context_set_io_watch_functions (global_context, pk_io_add_watch, pk_io_remove_watch);
	err = NULL;
	if (!polkit_context_init (global_context, &err)) {
		g_warning ("Cannot initialize libpolkit: %s", polkit_error_get_error_message (err));
		polkit_error_free (err);

		polkit_context_unref (global_context);
		global_context = NULL;
	}

	return global_context;
}

gboolean
check_polkit_privileges (DBusGConnection *dbus_connection,
					PolKitContext *pol_ctx,
					DBusGMethodInvocation *context,
					GError **err)
{
	DBusError dbus_error;
	char *sender;
	PolKitCaller *pk_caller;
	PolKitAction *pk_action;
	PolKitResult pk_result;

	dbus_error_init (&dbus_error);
	sender = dbus_g_method_get_sender (context);
	pk_caller = polkit_caller_new_from_dbus_name (dbus_g_connection_get_connection (dbus_connection),
										 sender,
										 &dbus_error);
	g_free (sender);

	if (dbus_error_is_set (&dbus_error)) {
		*err = g_error_new (NM_SYSCONFIG_SETTINGS_ERROR,
						NM_SYSCONFIG_SETTINGS_ERROR_NOT_PRIVILEGED,
						"Error getting information about caller: %s: %s",
						dbus_error.name, dbus_error.message);
		dbus_error_free (&dbus_error);

		if (pk_caller)
			polkit_caller_unref (pk_caller);

		return FALSE;
	}

	pk_action = polkit_action_new ();
	polkit_action_set_action_id (pk_action, NM_SYSCONFIG_POLICY_ACTION);

#if (POLKIT_VERSION_MAJOR == 0) && (POLKIT_VERSION_MINOR < 7)
	pk_result = polkit_context_can_caller_do_action (pol_ctx, pk_action, pk_caller);
#else
	pk_result = polkit_context_is_caller_authorized (pol_ctx, pk_action, pk_caller, TRUE, NULL);
#endif
	polkit_caller_unref (pk_caller);
	polkit_action_unref (pk_action);

	if (pk_result != POLKIT_RESULT_YES) {
		*err = g_error_new (NM_SYSCONFIG_SETTINGS_ERROR,
						NM_SYSCONFIG_SETTINGS_ERROR_NOT_PRIVILEGED,
						"%s %s",
						NM_SYSCONFIG_POLICY_ACTION,
						polkit_result_to_string_representation (pk_result));
		return FALSE;
	}

	return TRUE;
}