5 * Copyright (C) 2012 BMW Car IT GbmH. All rights reserved.
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.
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.
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
28 #include <sys/inotify.h>
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/log.h>
38 #include <connman/session.h>
39 #include <connman/dbus.h>
40 #include <connman/inotify.h>
42 #define POLICYDIR STORAGEDIR "/session_policy_local"
44 #define MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
45 S_IXGRP | S_IROTH | S_IXOTH)
47 static DBusConnection *connection;
49 static GHashTable *policy_hash;
50 static GHashTable *session_hash;
53 struct connman_session *session;
54 connman_session_config_cb callback;
62 struct connman_session *session;
63 struct connman_session_config *config;
66 static void cleanup_policy(gpointer user_data)
68 struct policy_data *policy = user_data;
70 if (policy->config != NULL)
71 g_slist_free(policy->config->allowed_bearers);
73 g_free(policy->ident);
74 g_free(policy->config);
78 static char *parse_ident(const unsigned char *context)
80 char *str, *ident, **tokens;
83 * SELinux combines Role-Based Access Control (RBAC), Type
84 * Enforcment (TE) and optionally Multi-Level Security (MLS).
86 * When SELinux is enabled all processes and files are labeled
87 * with a contex that contains information such as user, role
88 * type (and optionally a level). E.g.
91 * -rwxrwxr-x. wagi wagi unconfined_u:object_r:haifux_exec_t:s0 session_ui.py
93 * For identifyng application we (ab)using the type
94 * information. In the above example the haifux_exec_t type
95 * will be transfered to haifux_t as defined in the domain
96 * transition and thus we are able to identify the application
100 str = g_strdup((const gchar*)context);
104 DBG("SELinux context %s", str);
106 tokens = g_strsplit(str, ":", 0);
107 if (tokens == NULL) {
112 /* Use the SELinux type as identification token. */
113 ident = g_strdup(tokens[2]);
121 static struct policy_data *create_policy(const char *ident)
123 struct policy_data *policy;
125 DBG("ident %s", ident);
127 policy = g_try_new0(struct policy_data, 1);
131 policy->config = connman_session_create_default_config();
132 if (policy->config == NULL) {
137 policy->refcount = 1;
138 policy->ident = g_strdup(ident);
140 g_hash_table_replace(policy_hash, policy->ident, policy);
145 static struct policy_data *policy_ref(struct policy_data *policy)
147 DBG("%p %s ref %d", policy, policy->ident, policy->refcount + 1);
149 __sync_fetch_and_add(&policy->refcount, 1);
154 static void policy_unref(struct policy_data *policy)
156 DBG(" %p %s ref %d", policy, policy->ident, policy->refcount - 1);
158 if (__sync_fetch_and_sub(&policy->refcount, 1) != 1)
161 g_hash_table_remove(policy_hash, policy->ident);
164 static void selinux_context_reply(const unsigned char *context, void *user_data,
167 struct create_data *data = user_data;
168 struct policy_data *policy;
169 struct connman_session_config *config = NULL;
172 DBG("session %p", data->session);
177 ident = parse_ident(context);
183 policy = g_hash_table_lookup(policy_hash, ident);
184 if (policy != NULL) {
186 policy->session = data->session;
188 policy = create_policy(ident);
189 if (policy == NULL) {
195 g_hash_table_replace(session_hash, data->session, policy);
196 config = policy->config;
199 (*data->callback)(data->session, config, data->user_data, err);
205 static int policy_local_create(struct connman_session *session,
206 connman_session_config_cb callback,
209 struct create_data *data;
213 DBG("session %p", session);
215 data = g_try_new0(struct create_data, 1);
219 data->session = session;
220 data->callback = callback;
221 data->user_data = user_data;
223 owner = connman_session_get_owner(session);
225 err = connman_dbus_get_selinux_context(connection, owner,
226 selinux_context_reply,
229 connman_error("Could not get SELinux context");
237 static void policy_local_destroy(struct connman_session *session)
239 struct policy_data *policy;
241 DBG("session %p", session);
243 policy = g_hash_table_lookup(session_hash, session);
247 g_hash_table_remove(session_hash, session);
248 policy->session = NULL;
249 policy_unref(policy);
252 static struct connman_session_policy session_policy_local = {
253 .name = "session local policy configuration",
254 .priority = CONNMAN_SESSION_POLICY_PRIORITY_DEFAULT,
255 .create = policy_local_create,
256 .destroy = policy_local_destroy,
259 static int load_keyfile(const char *pathname, GKeyFile **keyfile)
261 GError *error = NULL;
264 DBG("Loading %s", pathname);
266 *keyfile = g_key_file_new();
268 if (g_key_file_load_from_file(*keyfile, pathname, 0, &error) == FALSE)
275 * The fancy G_FILE_ERROR_* codes are identical to the native
280 DBG("Unable to load %s: %s", pathname, error->message);
281 g_clear_error(&error);
283 g_key_file_free(*keyfile);
289 static int load_policy(struct policy_data *policy)
291 struct connman_session_config *config = policy->config;
297 pathname = g_strdup_printf("%s/%s", POLICYDIR, policy->ident);
301 err = load_keyfile(pathname, &keyfile);
305 if (err == -ENOENT) {
306 /* Ignore empty files */
313 config->priority = g_key_file_get_boolean(keyfile, "Default",
316 str = g_key_file_get_string(keyfile, "Default", "RoamingPolicy",
319 config->roaming_policy = connman_session_parse_roaming_policy(str);
322 config->roaming_policy = CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
325 str = g_key_file_get_string(keyfile, "Default", "ConnectionType",
328 config->type = connman_session_parse_connection_type(str);
331 config->type = CONNMAN_SESSION_TYPE_ANY;
334 config->ecall = g_key_file_get_boolean(keyfile, "Default",
335 "EmergencyCall", NULL);
337 g_slist_free(config->allowed_bearers);
338 config->allowed_bearers = NULL;
340 str = g_key_file_get_string(keyfile, "Default", "AllowedBearers",
344 tokens = g_strsplit(str, " ", 0);
346 for (i = 0; tokens[i] != NULL; i++) {
347 err = connman_session_parse_bearers(tokens[i],
348 &config->allowed_bearers);
356 config->allowed_bearers = g_slist_append(NULL,
357 GINT_TO_POINTER(CONNMAN_SERVICE_TYPE_UNKNOWN));
358 if (config->allowed_bearers == NULL)
362 g_key_file_free(keyfile);
368 static void update_session(struct connman_session *session)
370 if (connman_session_config_update(session) < 0)
371 connman_session_destroy(session);
374 static void remove_policy(struct policy_data *policy)
376 connman_bool_t update = FALSE;
379 if (policy->session != NULL)
382 policy_unref(policy);
387 err = connman_session_set_default_config(policy->config);
389 connman_session_destroy(policy->session);
393 update_session(policy->session);
396 static void notify_handler(struct inotify_event *event,
399 struct policy_data *policy;
404 policy = g_hash_table_lookup(policy_hash, ident);
406 if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
407 connman_info("Policy added for '%s'", ident);
412 policy = create_policy(ident);
418 if (event->mask & IN_MODIFY) {
419 connman_info("Policy modifed for '%s'", ident);
421 if (load_policy(policy) < 0) {
422 remove_policy(policy);
427 if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
428 connman_info("Policy deleted for '%s'", ident);
430 remove_policy(policy);
434 if (policy->session != NULL)
435 update_session(policy->session);
438 static int read_policies(void)
445 dir = g_dir_open(POLICYDIR, 0, NULL);
449 while ((file = g_dir_read_name(dir)) != NULL) {
450 struct policy_data *policy;
452 policy = create_policy(file);
453 if (policy == NULL) {
458 err = load_policy(policy);
469 static int session_policy_local_init(void)
473 /* If the dir doesn't exist, create it */
474 if (g_file_test(POLICYDIR, G_FILE_TEST_IS_DIR) == FALSE) {
475 if (mkdir(POLICYDIR, MODE) < 0) {
481 connection = connman_dbus_get_connection();
482 if (connection == NULL)
485 session_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
487 if (session_hash == NULL) {
492 policy_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
493 NULL, cleanup_policy);
494 if (policy_hash == NULL) {
499 err = connman_inotify_register(POLICYDIR, notify_handler);
503 err = read_policies();
507 err = connman_session_policy_register(&session_policy_local);
515 connman_inotify_unregister(POLICYDIR, notify_handler);
518 if (session_hash != NULL)
519 g_hash_table_destroy(session_hash);
520 if (policy_hash != NULL)
521 g_hash_table_destroy(policy_hash);
523 connman_session_policy_unregister(&session_policy_local);
525 dbus_connection_unref(connection);
530 static void session_policy_local_exit(void)
532 g_hash_table_destroy(session_hash);
533 g_hash_table_destroy(policy_hash);
535 connman_session_policy_unregister(&session_policy_local);
537 dbus_connection_unref(connection);
539 connman_inotify_unregister(POLICYDIR, notify_handler);
542 CONNMAN_PLUGIN_DEFINE(session_policy_local,
543 "Session local file policy configuration plugin",
544 VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
545 session_policy_local_init, session_policy_local_exit)