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;
60 struct connman_session *session;
61 struct connman_session_config *config;
64 static void cleanup_policy(gpointer user_data)
66 struct policy_data *policy = user_data;
68 if (policy->config != NULL)
69 g_slist_free(policy->config->allowed_bearers);
71 g_free(policy->ident);
72 g_free(policy->config);
76 static char *parse_ident(const unsigned char *context)
78 char *str, *ident, **tokens;
81 * SELinux combines Role-Based Access Control (RBAC), Type
82 * Enforcment (TE) and optionally Multi-Level Security (MLS).
84 * When SELinux is enabled all processes and files are labeled
85 * with a contex that contains information such as user, role
86 * type (and optionally a level). E.g.
89 * -rwxrwxr-x. wagi wagi unconfined_u:object_r:haifux_exec_t:s0 session_ui.py
91 * For identifyng application we (ab)using the type
92 * information. In the above example the haifux_exec_t type
93 * will be transfered to haifux_t as defined in the domain
94 * transition and thus we are able to identify the application
98 str = g_strdup((const gchar*)context);
102 DBG("SELinux context %s", str);
104 tokens = g_strsplit(str, ":", 0);
105 if (tokens == NULL) {
110 /* Use the SELinux type as identification token. */
111 ident = g_strdup(tokens[2]);
119 static struct policy_data *create_policy(const char *ident)
121 struct policy_data *policy;
123 DBG("ident %s", ident);
125 policy = g_try_new0(struct policy_data, 1);
129 policy->config = connman_session_create_default_config();
130 policy->refcount = 1;
131 policy->ident = g_strdup(ident);
133 g_hash_table_replace(policy_hash, policy->ident, policy);
138 static struct policy_data *policy_ref(struct policy_data *policy)
140 DBG("%p %s ref %d", policy, policy->ident, policy->refcount + 1);
142 __sync_fetch_and_add(&policy->refcount, 1);
147 static void policy_unref(struct policy_data *policy)
149 DBG(" %p %s ref %d", policy, policy->ident, policy->refcount - 1);
151 if (__sync_fetch_and_sub(&policy->refcount, 1) != 1)
154 g_hash_table_remove(policy_hash, policy->ident);
157 static void selinux_context_reply(const unsigned char *context, void *user_data,
160 struct cb_data *cbd = user_data;
161 connman_session_config_func_t cb = cbd->cb;
162 struct create_data *data = cbd->data;
163 struct policy_data *policy;
164 struct connman_session_config *config = NULL;
167 DBG("session %p", data->session);
172 ident = parse_ident(context);
178 policy = g_hash_table_lookup(policy_hash, ident);
179 if (policy != NULL) {
181 policy->session = data->session;
183 policy = create_policy(ident);
184 if (policy == NULL) {
190 g_hash_table_replace(session_hash, data->session, policy);
191 config = policy->config;
194 (*cb)(data->session, config, cbd->user_data, err);
201 static int policy_local_create(struct connman_session *session,
202 connman_session_config_func_t cb,
205 struct cb_data *cbd = cb_data_new(cb, user_data);
206 struct create_data *data;
210 DBG("session %p", session);
212 data = g_try_new0(struct create_data, 1);
217 data->session = session;
219 owner = connman_session_get_owner(session);
221 err = connman_dbus_get_selinux_context(connection, owner,
222 selinux_context_reply,
225 connman_error("Could not get SELinux context");
234 static void policy_local_destroy(struct connman_session *session)
236 struct policy_data *policy;
238 DBG("session %p", session);
240 policy = g_hash_table_lookup(session_hash, session);
244 g_hash_table_remove(session_hash, session);
245 policy->session = NULL;
246 policy_unref(policy);
249 static struct connman_session_policy session_policy_local = {
250 .name = "session local policy configuration",
251 .priority = CONNMAN_SESSION_POLICY_PRIORITY_DEFAULT,
252 .create = policy_local_create,
253 .destroy = policy_local_destroy,
256 static int load_keyfile(const char *pathname, GKeyFile **keyfile)
258 GError *error = NULL;
261 DBG("Loading %s", pathname);
263 *keyfile = g_key_file_new();
265 if (g_key_file_load_from_file(*keyfile, pathname, 0, &error) == FALSE)
272 * The fancy G_FILE_ERROR_* codes are identical to the native
277 DBG("Unable to load %s: %s", pathname, error->message);
278 g_clear_error(&error);
280 g_key_file_free(*keyfile);
286 static int load_policy(struct policy_data *policy)
288 struct connman_session_config *config = policy->config;
294 connman_session_set_default_config(config);
296 pathname = g_strdup_printf("%s/%s", POLICYDIR, policy->ident);
300 err = load_keyfile(pathname, &keyfile);
304 if (err == -ENOENT) {
305 /* Ignore empty files */
312 config->priority = g_key_file_get_boolean(keyfile, "Default",
315 str = g_key_file_get_string(keyfile, "Default", "RoamingPolicy",
318 config->roaming_policy = connman_session_parse_roaming_policy(str);
322 str = g_key_file_get_string(keyfile, "Default", "ConnectionType",
325 config->type = connman_session_parse_connection_type(str);
329 config->ecall = g_key_file_get_boolean(keyfile, "Default",
330 "EmergencyCall", NULL);
332 str = g_key_file_get_string(keyfile, "Default", "AllowedBearers",
336 g_slist_free(config->allowed_bearers);
337 config->allowed_bearers = NULL;
339 tokens = g_strsplit(str, " ", 0);
341 for (i = 0; tokens[i] != NULL; i++) {
342 err = connman_session_parse_bearers(tokens[i],
343 &config->allowed_bearers);
352 g_key_file_free(keyfile);
358 static void update_session(struct connman_session *session)
360 if (connman_session_config_update(session) < 0)
361 connman_session_destroy(session);
364 static void remove_policy(struct policy_data *policy)
366 connman_bool_t update = FALSE;
368 if (policy->session != NULL)
371 policy_unref(policy);
376 connman_session_set_default_config(policy->config);
377 update_session(policy->session);
380 static void notify_handler(struct inotify_event *event,
383 struct policy_data *policy;
389 policy = g_hash_table_lookup(policy_hash, ident);
391 if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
392 connman_info("Policy added for '%s'", ident);
397 policy = create_policy(ident);
399 err = load_policy(policy);
401 connman_warn("Loading policy file '%s' failed with %s",
402 ident, strerror(-err));
410 if (event->mask & IN_MODIFY) {
411 connman_info("Policy modifed for '%s'", ident);
413 err = load_policy(policy);
415 connman_warn("Loading policy file '%s' failed with %s",
416 ident, strerror(-err));
421 if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
422 connman_info("Policy deleted for '%s'", ident);
424 remove_policy(policy);
428 if (policy->session != NULL)
429 update_session(policy->session);
432 static int read_policies(void)
439 dir = g_dir_open(POLICYDIR, 0, NULL);
443 while ((file = g_dir_read_name(dir)) != NULL) {
444 struct policy_data *policy;
446 policy = create_policy(file);
447 if (policy == NULL) {
452 err = load_policy(policy);
463 static int session_policy_local_init(void)
467 /* If the dir doesn't exist, create it */
468 if (g_file_test(POLICYDIR, G_FILE_TEST_IS_DIR) == FALSE) {
469 if (mkdir(POLICYDIR, MODE) < 0) {
475 connection = connman_dbus_get_connection();
476 if (connection == NULL)
479 session_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
481 if (session_hash == NULL) {
486 policy_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
487 NULL, cleanup_policy);
488 if (policy_hash == NULL) {
493 err = connman_inotify_register(POLICYDIR, notify_handler);
497 err = read_policies();
501 err = connman_session_policy_register(&session_policy_local);
509 connman_inotify_unregister(POLICYDIR, notify_handler);
512 if (session_hash != NULL)
513 g_hash_table_destroy(session_hash);
514 if (policy_hash != NULL)
515 g_hash_table_destroy(policy_hash);
517 connman_session_policy_unregister(&session_policy_local);
519 dbus_connection_unref(connection);
524 static void session_policy_local_exit(void)
526 g_hash_table_destroy(session_hash);
527 g_hash_table_destroy(policy_hash);
529 connman_session_policy_unregister(&session_policy_local);
531 dbus_connection_unref(connection);
533 connman_inotify_unregister(POLICYDIR, notify_handler);
536 CONNMAN_PLUGIN_DEFINE(session_policy_local,
537 "Session local file policy configuration plugin",
538 VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
539 session_policy_local_init, session_policy_local_exit)