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_new0(struct policy_data, 1);
126 policy->refcount = 1;
128 policy->config = connman_session_create_default_config();
129 policy->ident = g_strdup(ident);
131 g_hash_table_replace(policy_hash, policy->ident, policy);
136 static struct policy_data *policy_ref(struct policy_data *policy)
138 DBG("%p %s ref %d", policy, policy->ident, policy->refcount + 1);
140 __sync_fetch_and_add(&policy->refcount, 1);
145 static void policy_unref(struct policy_data *policy)
147 DBG(" %p %s ref %d", policy, policy->ident, policy->refcount - 1);
149 if (__sync_fetch_and_sub(&policy->refcount, 1) != 1)
152 g_hash_table_remove(policy_hash, policy->ident);
155 static void selinux_context_reply(const unsigned char *context, void *user_data,
158 struct cb_data *cbd = user_data;
159 connman_session_config_func_t cb = cbd->cb;
160 struct create_data *data = cbd->data;
161 struct policy_data *policy;
162 struct connman_session_config *config = NULL;
165 DBG("session %p", data->session);
170 ident = parse_ident(context);
176 policy = g_hash_table_lookup(policy_hash, ident);
177 if (policy != NULL) {
179 policy->session = data->session;
181 policy = create_policy(ident);
183 g_hash_table_replace(session_hash, data->session, policy);
184 config = policy->config;
187 (*cb)(data->session, config, cbd->user_data, err);
194 static int policy_local_create(struct connman_session *session,
195 connman_session_config_func_t cb,
198 struct cb_data *cbd = cb_data_new(cb, user_data);
199 struct create_data *data;
203 DBG("session %p", session);
205 data = g_new0(struct create_data, 1);
208 data->session = session;
210 owner = connman_session_get_owner(session);
212 err = connman_dbus_get_selinux_context(connection, owner,
213 selinux_context_reply,
216 connman_error("Could not get SELinux context");
225 static void policy_local_destroy(struct connman_session *session)
227 struct policy_data *policy;
229 DBG("session %p", session);
231 policy = g_hash_table_lookup(session_hash, session);
235 g_hash_table_remove(session_hash, session);
236 policy->session = NULL;
237 policy_unref(policy);
240 static struct connman_session_policy session_policy_local = {
241 .name = "session local policy configuration",
242 .priority = CONNMAN_SESSION_POLICY_PRIORITY_DEFAULT,
243 .create = policy_local_create,
244 .destroy = policy_local_destroy,
247 static int load_keyfile(const char *pathname, GKeyFile **keyfile)
249 GError *error = NULL;
252 DBG("Loading %s", pathname);
254 *keyfile = g_key_file_new();
256 if (g_key_file_load_from_file(*keyfile, pathname, 0, &error) == FALSE)
263 * The fancy G_FILE_ERROR_* codes are identical to the native
268 DBG("Unable to load %s: %s", pathname, error->message);
269 g_clear_error(&error);
271 g_key_file_free(*keyfile);
277 static int load_policy(struct policy_data *policy)
279 struct connman_session_config *config = policy->config;
285 connman_session_set_default_config(config);
287 pathname = g_strdup_printf("%s/%s", POLICYDIR, policy->ident);
289 err = load_keyfile(pathname, &keyfile);
293 if (err == -ENOENT) {
294 /* Ignore empty files */
301 config->priority = g_key_file_get_boolean(keyfile, "Default",
304 str = g_key_file_get_string(keyfile, "Default", "RoamingPolicy",
307 config->roaming_policy = connman_session_parse_roaming_policy(str);
311 str = g_key_file_get_string(keyfile, "Default", "ConnectionType",
314 config->type = connman_session_parse_connection_type(str);
318 config->ecall = g_key_file_get_boolean(keyfile, "Default",
319 "EmergencyCall", NULL);
321 str = g_key_file_get_string(keyfile, "Default", "AllowedBearers",
325 g_slist_free(config->allowed_bearers);
326 config->allowed_bearers = NULL;
328 tokens = g_strsplit(str, " ", 0);
330 for (i = 0; tokens[i] != NULL; i++) {
331 err = connman_session_parse_bearers(tokens[i],
332 &config->allowed_bearers);
341 g_key_file_free(keyfile);
347 static void update_session(struct connman_session *session)
349 if (connman_session_config_update(session) < 0)
350 connman_session_destroy(session);
353 static void remove_policy(struct policy_data *policy)
355 connman_bool_t update = FALSE;
357 if (policy->session != NULL)
360 policy_unref(policy);
365 connman_session_set_default_config(policy->config);
366 update_session(policy->session);
369 static void notify_handler(struct inotify_event *event,
372 struct policy_data *policy;
378 policy = g_hash_table_lookup(policy_hash, ident);
380 if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
381 connman_info("Policy added for '%s'", ident);
386 policy = create_policy(ident);
388 err = load_policy(policy);
390 connman_warn("Loading policy file '%s' failed with %s",
391 ident, strerror(-err));
399 if (event->mask & IN_MODIFY) {
400 connman_info("Policy modifed for '%s'", ident);
402 err = load_policy(policy);
404 connman_warn("Loading policy file '%s' failed with %s",
405 ident, strerror(-err));
410 if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
411 connman_info("Policy deleted for '%s'", ident);
413 remove_policy(policy);
417 if (policy->session != NULL)
418 update_session(policy->session);
421 static int read_policies(void)
428 dir = g_dir_open(POLICYDIR, 0, NULL);
432 while ((file = g_dir_read_name(dir)) != NULL) {
433 struct policy_data *policy;
435 policy = create_policy(file);
436 err = load_policy(policy);
447 static int session_policy_local_init(void)
451 /* If the dir doesn't exist, create it */
452 if (g_file_test(POLICYDIR, G_FILE_TEST_IS_DIR) == FALSE) {
453 if (mkdir(POLICYDIR, MODE) < 0) {
459 connection = connman_dbus_get_connection();
460 if (connection == NULL)
463 session_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
465 policy_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
466 NULL, cleanup_policy);
468 err = connman_inotify_register(POLICYDIR, notify_handler);
472 err = read_policies();
476 err = connman_session_policy_register(&session_policy_local);
484 connman_inotify_unregister(POLICYDIR, notify_handler);
487 if (session_hash != NULL)
488 g_hash_table_destroy(session_hash);
489 if (policy_hash != NULL)
490 g_hash_table_destroy(policy_hash);
492 connman_session_policy_unregister(&session_policy_local);
494 dbus_connection_unref(connection);
499 static void session_policy_local_exit(void)
501 g_hash_table_destroy(session_hash);
502 g_hash_table_destroy(policy_hash);
504 connman_session_policy_unregister(&session_policy_local);
506 dbus_connection_unref(connection);
508 connman_inotify_unregister(POLICYDIR, notify_handler);
511 CONNMAN_PLUGIN_DEFINE(session_policy_local,
512 "Session local file policy configuration plugin",
513 VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
514 session_policy_local_init, session_policy_local_exit)