Create PolicyKit context and register security callbacks
[connman] / plugins / polkit.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27
28 #include <glib.h>
29 #include <polkit-dbus/polkit-dbus.h>
30
31 #include <connman/plugin.h>
32 #include <connman/security.h>
33 #include <connman/log.h>
34
35 static PolKitContext *polkit_context = NULL;
36
37 static int polkit_authorize(const char *sender)
38 {
39         DBG("sender %s", sender);
40
41         return -EPERM;
42 }
43
44 static struct connman_security polkit_security = {
45         .name                   = "polkit",
46         .authorize_sender       = polkit_authorize,
47 };
48
49 static gboolean watch_event(GIOChannel *channel, GIOCondition condition,
50                                                         gpointer user_data)
51 {
52         PolKitContext *context = user_data;
53         int fd;
54
55         DBG("context %p", context);
56
57         fd = g_io_channel_unix_get_fd(channel);
58
59         polkit_context_io_func(context, fd);
60
61         return TRUE;
62 }
63
64 static int add_watch(PolKitContext *context, int fd)
65 {
66         GIOChannel *channel;
67         guint id = 0;
68
69         DBG("context %p", context);
70
71         channel = g_io_channel_unix_new(fd);
72         if (channel == NULL)
73                 return 0;
74
75         id = g_io_add_watch(channel, G_IO_IN, watch_event, context);
76
77         g_io_channel_unref(channel);
78
79         return id;
80 }
81
82 static void remove_watch(PolKitContext *context, int id)
83 {
84         DBG("context %p", context);
85
86         g_source_remove(id);
87 }
88
89 static int polkit_init(void)
90 {
91         int err;
92
93         polkit_context = polkit_context_new();
94
95         polkit_context_set_io_watch_functions(polkit_context,
96                                                 add_watch, remove_watch);
97
98         if (polkit_context_init(polkit_context, NULL) == FALSE) {
99                 connman_error("Can't initialize PolicyKit");
100                 polkit_context_unref(polkit_context);
101                 polkit_context = NULL;
102                 return -EIO;
103         }
104
105         err = connman_security_register(&polkit_security);
106         if (err < 0) {
107                 polkit_context_unref(polkit_context);
108                 polkit_context = NULL;
109                 return err;
110         }
111
112         return 0;
113 }
114
115 static void polkit_exit(void)
116 {
117         connman_security_unregister(&polkit_security);
118
119         if (polkit_context == NULL)
120                 return;
121
122         polkit_context_unref(polkit_context);
123         polkit_context = NULL;
124 }
125
126 CONNMAN_PLUGIN_DEFINE("polkit", "PolicyKit authorization plugin", VERSION,
127                                                 polkit_init, polkit_exit)