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
|
/*
*
* Connection Manager
*
* Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <glib.h>
#include <polkit-dbus/polkit-dbus.h>
#include <connman/plugin.h>
#include <connman/security.h>
#include <connman/dbus.h>
#include <connman/log.h>
#define ACTION "org.moblin.connman.modify"
static DBusConnection *connection;
static PolKitContext *polkit_context;
static int polkit_authorize(const char *sender)
{
DBusError error;
PolKitCaller *caller;
PolKitAction *action;
PolKitResult result;
DBG("sender %s", sender);
dbus_error_init(&error);
caller = polkit_caller_new_from_dbus_name(connection, sender, &error);
if (caller == NULL) {
if (dbus_error_is_set(&error) == TRUE) {
connman_error("%s", error.message);
dbus_error_free(&error);
} else
connman_error("Failed to get caller information");
return -EIO;
}
action = polkit_action_new();
polkit_action_set_action_id(action, ACTION);
result = polkit_context_is_caller_authorized(polkit_context,
action, caller, TRUE, NULL);
polkit_action_unref(action);
polkit_caller_unref(caller);
DBG("result %s", polkit_result_to_string_representation(result));
if (result == POLKIT_RESULT_NO)
return -EPERM;
return 0;
}
static struct connman_security polkit_security = {
.name = "polkit",
.authorize_sender = polkit_authorize,
};
static gboolean watch_event(GIOChannel *channel, GIOCondition condition,
gpointer user_data)
{
PolKitContext *context = user_data;
int fd;
DBG("context %p", context);
fd = g_io_channel_unix_get_fd(channel);
polkit_context_io_func(context, fd);
return TRUE;
}
static int add_watch(PolKitContext *context, int fd)
{
GIOChannel *channel;
guint id = 0;
DBG("context %p", context);
channel = g_io_channel_unix_new(fd);
if (channel == NULL)
return 0;
id = g_io_add_watch(channel, G_IO_IN, watch_event, context);
g_io_channel_unref(channel);
return id;
}
static void remove_watch(PolKitContext *context, int id)
{
DBG("context %p", context);
g_source_remove(id);
}
static int polkit_init(void)
{
int err;
connection = connman_dbus_get_connection();
if (connection == NULL)
return -EIO;
polkit_context = polkit_context_new();
polkit_context_set_io_watch_functions(polkit_context,
add_watch, remove_watch);
if (polkit_context_init(polkit_context, NULL) == FALSE) {
connman_error("Can't initialize PolicyKit");
polkit_context_unref(polkit_context);
dbus_connection_unref(connection);
return -EIO;
}
err = connman_security_register(&polkit_security);
if (err < 0) {
polkit_context_unref(polkit_context);
dbus_connection_unref(connection);
return err;
}
return 0;
}
static void polkit_exit(void)
{
connman_security_unregister(&polkit_security);
polkit_context_unref(polkit_context);
dbus_connection_unref(connection);
}
CONNMAN_PLUGIN_DEFINE(polkit, "PolicyKit authorization plugin", VERSION,
polkit_init, polkit_exit)
|