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_selinux_type(const char *context)
78 char *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 tokens = g_strsplit(context, ":", 0);
99 if (g_strv_length(tokens) < 2) {
104 /* Use the SELinux type as identification token. */
105 ident = g_strdup(tokens[2]);
112 static struct policy_data *create_policy(const char *ident)
114 struct policy_data *policy;
116 DBG("ident %s", ident);
118 policy = g_new0(struct policy_data, 1);
119 policy->refcount = 1;
121 policy->config = connman_session_create_default_config();
122 policy->ident = g_strdup(ident);
124 g_hash_table_replace(policy_hash, policy->ident, policy);
129 static struct policy_data *policy_ref(struct policy_data *policy)
131 DBG("%p %s ref %d", policy, policy->ident, policy->refcount + 1);
133 __sync_fetch_and_add(&policy->refcount, 1);
138 static void policy_unref(struct policy_data *policy)
140 DBG(" %p %s ref %d", policy, policy->ident, policy->refcount - 1);
142 if (__sync_fetch_and_sub(&policy->refcount, 1) != 1)
145 g_hash_table_remove(policy_hash, policy->ident);
148 static void selinux_context_reply(const unsigned char *context, void *user_data,
151 struct cb_data *cbd = user_data;
152 connman_session_config_func_t cb = cbd->cb;
153 struct create_data *data = cbd->data;
154 struct policy_data *policy;
155 struct connman_session_config *config = NULL;
158 DBG("session %p", data->session);
163 DBG("SELinux context %s", context);
165 ident = parse_selinux_type((const char*)context);
171 policy = g_hash_table_lookup(policy_hash, ident);
172 if (policy != NULL) {
174 policy->session = data->session;
176 policy = create_policy(ident);
178 g_hash_table_replace(session_hash, data->session, policy);
179 config = policy->config;
182 (*cb)(data->session, config, cbd->user_data, err);
189 static int policy_local_create(struct connman_session *session,
190 connman_session_config_func_t cb,
193 struct cb_data *cbd = cb_data_new(cb, user_data);
194 struct create_data *data;
198 DBG("session %p", session);
200 data = g_new0(struct create_data, 1);
203 data->session = session;
205 owner = connman_session_get_owner(session);
207 err = connman_dbus_get_selinux_context(connection, owner,
208 selinux_context_reply,
211 connman_error("Could not get SELinux context");
220 static void policy_local_destroy(struct connman_session *session)
222 struct policy_data *policy;
224 DBG("session %p", session);
226 policy = g_hash_table_lookup(session_hash, session);
230 g_hash_table_remove(session_hash, session);
231 policy->session = NULL;
232 policy_unref(policy);
235 static struct connman_session_policy session_policy_local = {
236 .name = "session local policy configuration",
237 .priority = CONNMAN_SESSION_POLICY_PRIORITY_DEFAULT,
238 .create = policy_local_create,
239 .destroy = policy_local_destroy,
242 static int load_keyfile(const char *pathname, GKeyFile **keyfile)
244 GError *error = NULL;
247 DBG("Loading %s", pathname);
249 *keyfile = g_key_file_new();
251 if (g_key_file_load_from_file(*keyfile, pathname, 0, &error) == FALSE)
258 * The fancy G_FILE_ERROR_* codes are identical to the native
263 DBG("Unable to load %s: %s", pathname, error->message);
264 g_clear_error(&error);
266 g_key_file_free(*keyfile);
272 static int load_policy(struct policy_data *policy)
274 struct connman_session_config *config = policy->config;
280 connman_session_set_default_config(config);
282 pathname = g_strdup_printf("%s/%s", POLICYDIR, policy->ident);
284 err = load_keyfile(pathname, &keyfile);
288 if (err == -ENOENT) {
289 /* Ignore empty files */
296 config->priority = g_key_file_get_boolean(keyfile, "Default",
299 str = g_key_file_get_string(keyfile, "Default", "RoamingPolicy",
302 config->roaming_policy = connman_session_parse_roaming_policy(str);
306 str = g_key_file_get_string(keyfile, "Default", "ConnectionType",
309 config->type = connman_session_parse_connection_type(str);
313 config->ecall = g_key_file_get_boolean(keyfile, "Default",
314 "EmergencyCall", NULL);
316 str = g_key_file_get_string(keyfile, "Default", "AllowedBearers",
320 g_slist_free(config->allowed_bearers);
321 config->allowed_bearers = NULL;
323 tokens = g_strsplit(str, " ", 0);
325 for (i = 0; tokens[i] != NULL; i++) {
326 err = connman_session_parse_bearers(tokens[i],
327 &config->allowed_bearers);
336 g_key_file_free(keyfile);
342 static void update_session(struct connman_session *session)
344 if (connman_session_config_update(session) < 0)
345 connman_session_destroy(session);
348 static void remove_policy(struct policy_data *policy)
350 connman_bool_t update = FALSE;
352 if (policy->session != NULL)
355 policy_unref(policy);
360 connman_session_set_default_config(policy->config);
361 update_session(policy->session);
364 static void notify_handler(struct inotify_event *event,
367 struct policy_data *policy;
373 policy = g_hash_table_lookup(policy_hash, ident);
375 if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
376 connman_info("Policy added for '%s'", ident);
381 policy = create_policy(ident);
383 err = load_policy(policy);
385 connman_warn("Loading policy file '%s' failed with %s",
386 ident, strerror(-err));
394 if (event->mask & IN_MODIFY) {
395 connman_info("Policy modifed for '%s'", ident);
397 err = load_policy(policy);
399 connman_warn("Loading policy file '%s' failed with %s",
400 ident, strerror(-err));
405 if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
406 connman_info("Policy deleted for '%s'", ident);
408 remove_policy(policy);
412 if (policy->session != NULL)
413 update_session(policy->session);
416 static int read_policies(void)
423 dir = g_dir_open(POLICYDIR, 0, NULL);
427 while ((file = g_dir_read_name(dir)) != NULL) {
428 struct policy_data *policy;
430 policy = create_policy(file);
431 err = load_policy(policy);
442 static int session_policy_local_init(void)
446 /* If the dir doesn't exist, create it */
447 if (g_file_test(POLICYDIR, G_FILE_TEST_IS_DIR) == FALSE) {
448 if (mkdir(POLICYDIR, MODE) < 0) {
454 connection = connman_dbus_get_connection();
455 if (connection == NULL)
458 session_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
460 policy_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
461 NULL, cleanup_policy);
463 err = connman_inotify_register(POLICYDIR, notify_handler);
467 err = read_policies();
471 err = connman_session_policy_register(&session_policy_local);
479 connman_inotify_unregister(POLICYDIR, notify_handler);
482 if (session_hash != NULL)
483 g_hash_table_destroy(session_hash);
484 if (policy_hash != NULL)
485 g_hash_table_destroy(policy_hash);
487 connman_session_policy_unregister(&session_policy_local);
489 dbus_connection_unref(connection);
494 static void session_policy_local_exit(void)
496 g_hash_table_destroy(session_hash);
497 g_hash_table_destroy(policy_hash);
499 connman_session_policy_unregister(&session_policy_local);
501 dbus_connection_unref(connection);
503 connman_inotify_unregister(POLICYDIR, notify_handler);
506 CONNMAN_PLUGIN_DEFINE(session_policy_local,
507 "Session local file policy configuration plugin",
508 VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
509 session_policy_local_init, session_policy_local_exit)