session: Do not fail when creating default policy configuration
[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_ident(const unsigned char *context)
77 {
78         char *str, *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         str = g_strdup((const gchar*)context);
99         if (str == NULL)
100                 return NULL;
101
102         DBG("SELinux context %s", str);
103
104         tokens = g_strsplit(str, ":", 0);
105         if (tokens == NULL) {
106                 g_free(str);
107                 return NULL;
108         }
109
110         /* Use the SELinux type as identification token. */
111         ident = g_strdup(tokens[2]);
112
113         g_strfreev(tokens);
114         g_free(str);
115
116         return ident;
117 }
118
119 static struct policy_data *create_policy(const char *ident)
120 {
121         struct policy_data *policy;
122
123         DBG("ident %s", ident);
124
125         policy = g_try_new0(struct policy_data, 1);
126         if (policy == NULL)
127                 return NULL;
128
129         policy->config = connman_session_create_default_config();
130         policy->refcount = 1;
131         policy->ident = g_strdup(ident);
132
133         g_hash_table_replace(policy_hash, policy->ident, policy);
134
135         return policy;
136 }
137
138 static struct policy_data *policy_ref(struct policy_data *policy)
139 {
140         DBG("%p %s ref %d", policy, policy->ident, policy->refcount + 1);
141
142         __sync_fetch_and_add(&policy->refcount, 1);
143
144         return policy;
145 }
146
147 static void policy_unref(struct policy_data *policy)
148 {
149         DBG(" %p %s ref %d", policy, policy->ident, policy->refcount - 1);
150
151         if (__sync_fetch_and_sub(&policy->refcount, 1) != 1)
152                 return;
153
154         g_hash_table_remove(policy_hash, policy->ident);
155 };
156
157 static void selinux_context_reply(const unsigned char *context, void *user_data,
158                                         int err)
159 {
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;
165         char *ident = NULL;
166
167         DBG("session %p", data->session);
168
169         if (err < 0)
170                 goto done;
171
172         ident = parse_ident(context);
173         if (ident == NULL) {
174                 err = -EINVAL;
175                 goto done;
176         }
177
178         policy = g_hash_table_lookup(policy_hash, ident);
179         if (policy != NULL) {
180                 policy_ref(policy);
181                 policy->session = data->session;
182         } else {
183                 policy = create_policy(ident);
184                 if (policy == NULL) {
185                         err = -ENOMEM;
186                         goto done;
187                 }
188         }
189
190         g_hash_table_replace(session_hash, data->session, policy);
191         config = policy->config;
192
193 done:
194         (*cb)(data->session, config, cbd->user_data, err);
195
196         g_free(cbd);
197         g_free(data);
198         g_free(ident);
199 }
200
201 static int policy_local_create(struct connman_session *session,
202                                 connman_session_config_func_t cb,
203                                 void *user_data)
204 {
205         struct cb_data *cbd = cb_data_new(cb, user_data);
206         struct create_data *data;
207         const char *owner;
208         int err;
209
210         DBG("session %p", session);
211
212         data = g_try_new0(struct create_data, 1);
213         if (data == NULL)
214                 return -ENOMEM;
215         cbd->data = data;
216
217         data->session = session;
218
219         owner = connman_session_get_owner(session);
220
221         err = connman_dbus_get_selinux_context(connection, owner,
222                                         selinux_context_reply,
223                                         cbd);
224         if (err < 0) {
225                 connman_error("Could not get SELinux context");
226                 g_free(data);
227                 g_free(cbd);
228                 return err;
229         }
230
231         return 0;
232 }
233
234 static void policy_local_destroy(struct connman_session *session)
235 {
236         struct policy_data *policy;
237
238         DBG("session %p", session);
239
240         policy = g_hash_table_lookup(session_hash, session);
241         if (policy == NULL)
242                 return;
243
244         g_hash_table_remove(session_hash, session);
245         policy->session = NULL;
246         policy_unref(policy);
247 }
248
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,
254 };
255
256 static int load_keyfile(const char *pathname, GKeyFile **keyfile)
257 {
258         GError *error = NULL;
259         int err;
260
261         DBG("Loading %s", pathname);
262
263         *keyfile = g_key_file_new();
264
265         if (g_key_file_load_from_file(*keyfile, pathname, 0, &error) == FALSE)
266                 goto err;
267
268         return 0;
269
270 err:
271         /*
272          * The fancy G_FILE_ERROR_* codes are identical to the native
273          * error codes.
274          */
275         err = -error->code;
276
277         DBG("Unable to load %s: %s", pathname, error->message);
278         g_clear_error(&error);
279
280         g_key_file_free(*keyfile);
281         *keyfile = NULL;
282
283         return err;
284 }
285
286 static int load_policy(struct policy_data *policy)
287 {
288         struct connman_session_config *config = policy->config;
289         GKeyFile *keyfile;
290         char *pathname;
291         char *str, **tokens;
292         int i, err = 0;
293
294         connman_session_set_default_config(config);
295
296         pathname = g_strdup_printf("%s/%s", POLICYDIR, policy->ident);
297         if(pathname == NULL)
298                 return -ENOMEM;
299
300         err = load_keyfile(pathname, &keyfile);
301         if (err < 0) {
302                 g_free(pathname);
303
304                 if (err == -ENOENT) {
305                         /* Ignore empty files */
306                         return 0;
307                 }
308
309                 return err;
310         }
311
312         config->priority = g_key_file_get_boolean(keyfile, "Default",
313                                                 "Priority", NULL);
314
315         str = g_key_file_get_string(keyfile, "Default", "RoamingPolicy",
316                                 NULL);
317         if (str != NULL) {
318                 config->roaming_policy = connman_session_parse_roaming_policy(str);
319                 g_free(str);
320         }
321
322         str = g_key_file_get_string(keyfile, "Default", "ConnectionType",
323                                 NULL);
324         if (str != NULL) {
325                 config->type = connman_session_parse_connection_type(str);
326                 g_free(str);
327         }
328
329         config->ecall = g_key_file_get_boolean(keyfile, "Default",
330                                                 "EmergencyCall", NULL);
331
332         str = g_key_file_get_string(keyfile, "Default", "AllowedBearers",
333                                 NULL);
334
335         if (str != NULL) {
336                 g_slist_free(config->allowed_bearers);
337                 config->allowed_bearers = NULL;
338
339                 tokens = g_strsplit(str, " ", 0);
340
341                 for (i = 0; tokens[i] != NULL; i++) {
342                         err = connman_session_parse_bearers(tokens[i],
343                                         &config->allowed_bearers);
344                         if (err < 0)
345                                 break;
346                 }
347
348                 g_free(str);
349                 g_strfreev(tokens);
350         }
351
352         g_key_file_free(keyfile);
353         g_free(pathname);
354
355         return err;
356 }
357
358 static void update_session(struct connman_session *session)
359 {
360         if (connman_session_config_update(session) < 0)
361                 connman_session_destroy(session);
362 }
363
364 static void remove_policy(struct policy_data *policy)
365 {
366         connman_bool_t update = FALSE;
367
368         if (policy->session != NULL)
369                 update = TRUE;
370
371         policy_unref(policy);
372
373         if (update == FALSE)
374                 return;
375
376         connman_session_set_default_config(policy->config);
377         update_session(policy->session);
378 }
379
380 static void notify_handler(struct inotify_event *event,
381                                         const char *ident)
382 {
383         struct policy_data *policy;
384         int err;
385
386         if (ident == NULL)
387                 return;
388
389         policy = g_hash_table_lookup(policy_hash, ident);
390
391         if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
392                 connman_info("Policy added for '%s'", ident);
393
394                 if (policy != NULL)
395                         policy_ref(policy);
396                 else
397                         policy = create_policy(ident);
398
399                 err = load_policy(policy);
400                 if (err < 0) {
401                         connman_warn("Loading policy file '%s' failed with %s",
402                                         ident, strerror(-err));
403                         return;
404                 }
405         }
406
407         if (policy == NULL)
408                 return;
409
410         if (event->mask & IN_MODIFY) {
411                 connman_info("Policy modifed for '%s'", ident);
412
413                 err = load_policy(policy);
414                 if (err < 0) {
415                         connman_warn("Loading policy file '%s' failed with %s",
416                                         ident, strerror(-err));
417                         return;
418                 }
419         }
420
421         if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
422                 connman_info("Policy deleted for '%s'", ident);
423
424                 remove_policy(policy);
425                 return;
426         }
427
428         if (policy->session != NULL)
429                 update_session(policy->session);
430 }
431
432 static int read_policies(void)
433 {
434         GDir *dir;
435         int err = 0;
436
437         DBG("");
438
439         dir = g_dir_open(POLICYDIR, 0, NULL);
440         if (dir != NULL) {
441                 const gchar *file;
442
443                 while ((file = g_dir_read_name(dir)) != NULL) {
444                         struct policy_data *policy;
445
446                         policy = create_policy(file);
447                         if (policy == NULL) {
448                                 err = -ENOMEM;
449                                 break;
450                         }
451
452                         err = load_policy(policy);
453                         if (err < 0)
454                                 break;
455                 }
456
457                 g_dir_close(dir);
458         }
459
460         return err;
461 }
462
463 static int session_policy_local_init(void)
464 {
465         int err;
466
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) {
470                         if (errno != EEXIST)
471                                 return -errno;
472                 }
473         }
474
475         connection = connman_dbus_get_connection();
476         if (connection == NULL)
477                 return -EIO;
478
479         session_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
480                                                 NULL, NULL);
481         if (session_hash == NULL) {
482                 err = -ENOMEM;
483                 goto err;
484         }
485
486         policy_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
487                                         NULL, cleanup_policy);
488         if (policy_hash == NULL) {
489                 err = -ENOMEM;
490                 goto err;
491         }
492
493         err = connman_inotify_register(POLICYDIR, notify_handler);
494         if (err < 0)
495                 goto err;
496
497         err = read_policies();
498         if (err < 0)
499                 goto err_notify;
500
501         err = connman_session_policy_register(&session_policy_local);
502         if (err < 0)
503                 goto err_notify;
504
505         return 0;
506
507 err_notify:
508
509         connman_inotify_unregister(POLICYDIR, notify_handler);
510
511 err:
512         if (session_hash != NULL)
513                 g_hash_table_destroy(session_hash);
514         if (policy_hash != NULL)
515                 g_hash_table_destroy(policy_hash);
516
517         connman_session_policy_unregister(&session_policy_local);
518
519         dbus_connection_unref(connection);
520
521         return err;
522 }
523
524 static void session_policy_local_exit(void)
525 {
526         g_hash_table_destroy(session_hash);
527         g_hash_table_destroy(policy_hash);
528
529         connman_session_policy_unregister(&session_policy_local);
530
531         dbus_connection_unref(connection);
532
533         connman_inotify_unregister(POLICYDIR, notify_handler);
534 }
535
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)