session_policy_local: Update session before unref policy
[platform/upstream/connman.git] / plugins / session_policy_local.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  BMW Car IT GbmH. 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 #include <string.h>
28 #include <sys/inotify.h>
29 #include <sys/stat.h>
30
31 #include <glib.h>
32
33 #include <gdbus.h>
34
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>
41
42 #define POLICYDIR STORAGEDIR "/session_policy_local"
43
44 #define MODE            (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
45                         S_IXGRP | S_IROTH | S_IXOTH)
46
47 static DBusConnection *connection;
48
49 static GHashTable *policy_hash;
50 static GHashTable *session_hash;
51
52 struct create_data {
53         struct connman_session *session;
54 };
55
56 struct policy_data {
57         int refcount;
58         char *ident;
59
60         struct connman_session *session;
61         struct connman_session_config *config;
62 };
63
64 static void cleanup_policy(gpointer user_data)
65 {
66         struct policy_data *policy = user_data;
67
68         if (policy->config != NULL)
69                 g_slist_free(policy->config->allowed_bearers);
70
71         g_free(policy->ident);
72         g_free(policy->config);
73         g_free(policy);
74 }
75
76 static char *parse_selinux_type(const char *context)
77 {
78         char *ident, **tokens;
79
80         /*
81          * SELinux combines Role-Based Access Control (RBAC), Type
82          * Enforcment (TE) and optionally Multi-Level Security (MLS).
83          *
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.
87          *
88          * $ ls -Z
89          * -rwxrwxr-x. wagi wagi unconfined_u:object_r:haifux_exec_t:s0 session_ui.py
90          *
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
95          * as haifux_t.
96          */
97
98         tokens = g_strsplit(context, ":", 0);
99         if (g_strv_length(tokens) < 2) {
100                 g_strfreev(tokens);
101                 return NULL;
102         }
103
104         /* Use the SELinux type as identification token. */
105         ident = g_strdup(tokens[2]);
106
107         g_strfreev(tokens);
108
109         return ident;
110 }
111
112 static struct policy_data *create_policy(const char *ident)
113 {
114         struct policy_data *policy;
115
116         DBG("ident %s", ident);
117
118         policy = g_new0(struct policy_data, 1);
119         policy->refcount = 1;
120
121         policy->config = connman_session_create_default_config();
122         policy->ident = g_strdup(ident);
123
124         g_hash_table_replace(policy_hash, policy->ident, policy);
125
126         return policy;
127 }
128
129 static struct policy_data *policy_ref(struct policy_data *policy)
130 {
131         DBG("%p %s ref %d", policy, policy->ident, policy->refcount + 1);
132
133         __sync_fetch_and_add(&policy->refcount, 1);
134
135         return policy;
136 }
137
138 static void policy_unref(struct policy_data *policy)
139 {
140         DBG(" %p %s ref %d", policy, policy->ident, policy->refcount - 1);
141
142         if (__sync_fetch_and_sub(&policy->refcount, 1) != 1)
143                 return;
144
145         g_hash_table_remove(policy_hash, policy->ident);
146 };
147
148 static void selinux_context_reply(const unsigned char *context, void *user_data,
149                                         int err)
150 {
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;
156         char *ident = NULL;
157
158         DBG("session %p", data->session);
159
160         if (err < 0)
161                 goto done;
162
163         DBG("SELinux context %s", context);
164
165         ident = parse_selinux_type((const char*)context);
166         if (ident == NULL) {
167                 err = -EINVAL;
168                 goto done;
169         }
170
171         policy = g_hash_table_lookup(policy_hash, ident);
172         if (policy != NULL) {
173                 policy_ref(policy);
174                 policy->session = data->session;
175         } else
176                 policy = create_policy(ident);
177
178         g_hash_table_replace(session_hash, data->session, policy);
179         config = policy->config;
180
181 done:
182         (*cb)(data->session, config, cbd->user_data, err);
183
184         g_free(cbd);
185         g_free(data);
186         g_free(ident);
187 }
188
189 static int policy_local_create(struct connman_session *session,
190                                 connman_session_config_func_t cb,
191                                 void *user_data)
192 {
193         struct cb_data *cbd = cb_data_new(cb, user_data);
194         struct create_data *data;
195         const char *owner;
196         int err;
197
198         DBG("session %p", session);
199
200         data = g_new0(struct create_data, 1);
201         cbd->data = data;
202
203         data->session = session;
204
205         owner = connman_session_get_owner(session);
206
207         err = connman_dbus_get_selinux_context(connection, owner,
208                                         selinux_context_reply,
209                                         cbd);
210         if (err < 0) {
211                 connman_error("Could not get SELinux context");
212                 g_free(data);
213                 g_free(cbd);
214                 return err;
215         }
216
217         return 0;
218 }
219
220 static void policy_local_destroy(struct connman_session *session)
221 {
222         struct policy_data *policy;
223
224         DBG("session %p", session);
225
226         policy = g_hash_table_lookup(session_hash, session);
227         if (policy == NULL)
228                 return;
229
230         g_hash_table_remove(session_hash, session);
231         policy->session = NULL;
232         policy_unref(policy);
233 }
234
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,
240 };
241
242 static int load_keyfile(const char *pathname, GKeyFile **keyfile)
243 {
244         GError *error = NULL;
245         int err;
246
247         DBG("Loading %s", pathname);
248
249         *keyfile = g_key_file_new();
250
251         if (g_key_file_load_from_file(*keyfile, pathname, 0, &error) == FALSE)
252                 goto err;
253
254         return 0;
255
256 err:
257         /*
258          * The fancy G_FILE_ERROR_* codes are identical to the native
259          * error codes.
260          */
261         err = -error->code;
262
263         DBG("Unable to load %s: %s", pathname, error->message);
264         g_clear_error(&error);
265
266         g_key_file_free(*keyfile);
267         *keyfile = NULL;
268
269         return err;
270 }
271
272 static int load_policy(struct policy_data *policy)
273 {
274         struct connman_session_config *config = policy->config;
275         GKeyFile *keyfile;
276         char *pathname;
277         char *str, **tokens;
278         int i, err = 0;
279
280         connman_session_set_default_config(config);
281
282         pathname = g_strdup_printf("%s/%s", POLICYDIR, policy->ident);
283
284         err = load_keyfile(pathname, &keyfile);
285         if (err < 0) {
286                 g_free(pathname);
287
288                 if (err == -ENOENT) {
289                         /* Ignore empty files */
290                         return 0;
291                 }
292
293                 return err;
294         }
295
296         config->priority = g_key_file_get_boolean(keyfile, "Default",
297                                                 "Priority", NULL);
298
299         str = g_key_file_get_string(keyfile, "Default", "RoamingPolicy",
300                                 NULL);
301         if (str != NULL) {
302                 config->roaming_policy = connman_session_parse_roaming_policy(str);
303                 g_free(str);
304         }
305
306         str = g_key_file_get_string(keyfile, "Default", "ConnectionType",
307                                 NULL);
308         if (str != NULL) {
309                 config->type = connman_session_parse_connection_type(str);
310                 g_free(str);
311         }
312
313         config->ecall = g_key_file_get_boolean(keyfile, "Default",
314                                                 "EmergencyCall", NULL);
315
316         str = g_key_file_get_string(keyfile, "Default", "AllowedBearers",
317                                 NULL);
318
319         if (str != NULL) {
320                 g_slist_free(config->allowed_bearers);
321                 config->allowed_bearers = NULL;
322
323                 tokens = g_strsplit(str, " ", 0);
324
325                 for (i = 0; tokens[i] != NULL; i++) {
326                         err = connman_session_parse_bearers(tokens[i],
327                                         &config->allowed_bearers);
328                         if (err < 0)
329                                 break;
330                 }
331
332                 g_free(str);
333                 g_strfreev(tokens);
334         }
335
336         g_key_file_free(keyfile);
337         g_free(pathname);
338
339         return err;
340 }
341
342 static void update_session(struct connman_session *session)
343 {
344         if (connman_session_config_update(session) < 0)
345                 connman_session_destroy(session);
346 }
347
348 static void remove_policy(struct policy_data *policy)
349 {
350         if (policy->session != NULL) {
351                 connman_session_set_default_config(policy->config);
352                 update_session(policy->session);
353         }
354
355         policy_unref(policy);
356 }
357
358 static void notify_handler(struct inotify_event *event,
359                                         const char *ident)
360 {
361         struct policy_data *policy;
362         int err;
363
364         if (ident == NULL)
365                 return;
366
367         policy = g_hash_table_lookup(policy_hash, ident);
368
369         if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
370                 connman_info("Policy added for '%s'", ident);
371
372                 if (policy != NULL)
373                         policy_ref(policy);
374                 else
375                         policy = create_policy(ident);
376
377                 err = load_policy(policy);
378                 if (err < 0) {
379                         connman_warn("Loading policy file '%s' failed with %s",
380                                         ident, strerror(-err));
381                         return;
382                 }
383         }
384
385         if (policy == NULL)
386                 return;
387
388         if (event->mask & IN_MODIFY) {
389                 connman_info("Policy modifed for '%s'", ident);
390
391                 err = load_policy(policy);
392                 if (err < 0) {
393                         connman_warn("Loading policy file '%s' failed with %s",
394                                         ident, strerror(-err));
395                         return;
396                 }
397         }
398
399         if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
400                 connman_info("Policy deleted for '%s'", ident);
401
402                 remove_policy(policy);
403                 return;
404         }
405
406         if (policy->session != NULL)
407                 update_session(policy->session);
408 }
409
410 static int read_policies(void)
411 {
412         GDir *dir;
413         int err = 0;
414
415         DBG("");
416
417         dir = g_dir_open(POLICYDIR, 0, NULL);
418         if (dir != NULL) {
419                 const gchar *file;
420
421                 while ((file = g_dir_read_name(dir)) != NULL) {
422                         struct policy_data *policy;
423
424                         policy = create_policy(file);
425                         err = load_policy(policy);
426                         if (err < 0)
427                                 break;
428                 }
429
430                 g_dir_close(dir);
431         }
432
433         return err;
434 }
435
436 static int session_policy_local_init(void)
437 {
438         int err;
439
440         /* If the dir doesn't exist, create it */
441         if (g_file_test(POLICYDIR, G_FILE_TEST_IS_DIR) == FALSE) {
442                 if (mkdir(POLICYDIR, MODE) < 0) {
443                         if (errno != EEXIST)
444                                 return -errno;
445                 }
446         }
447
448         connection = connman_dbus_get_connection();
449         if (connection == NULL)
450                 return -EIO;
451
452         session_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
453                                                 NULL, NULL);
454         policy_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
455                                         NULL, cleanup_policy);
456
457         err = connman_inotify_register(POLICYDIR, notify_handler);
458         if (err < 0)
459                 goto err;
460
461         err = read_policies();
462         if (err < 0)
463                 goto err_notify;
464
465         err = connman_session_policy_register(&session_policy_local);
466         if (err < 0)
467                 goto err_notify;
468
469         return 0;
470
471 err_notify:
472
473         connman_inotify_unregister(POLICYDIR, notify_handler);
474
475 err:
476         if (session_hash != NULL)
477                 g_hash_table_destroy(session_hash);
478         if (policy_hash != NULL)
479                 g_hash_table_destroy(policy_hash);
480
481         connman_session_policy_unregister(&session_policy_local);
482
483         dbus_connection_unref(connection);
484
485         return err;
486 }
487
488 static void session_policy_local_exit(void)
489 {
490         g_hash_table_destroy(session_hash);
491         g_hash_table_destroy(policy_hash);
492
493         connman_session_policy_unregister(&session_policy_local);
494
495         dbus_connection_unref(connection);
496
497         connman_inotify_unregister(POLICYDIR, notify_handler);
498 }
499
500 CONNMAN_PLUGIN_DEFINE(session_policy_local,
501                 "Session local file policy configuration plugin",
502                 VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
503                 session_policy_local_init, session_policy_local_exit)