session_policy_local: Create policy directory if necessary
[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         connman_session_config_cb callback;
55         void *user_data;
56 };
57
58 struct policy_data {
59         int refcount;
60         char *ident;
61
62         struct connman_session *session;
63         struct connman_session_config *config;
64 };
65
66 static void cleanup_policy(gpointer user_data)
67 {
68         struct policy_data *policy = user_data;
69
70         if (policy->config != NULL)
71                 g_slist_free(policy->config->allowed_bearers);
72
73         g_free(policy->ident);
74         g_free(policy->config);
75         g_free(policy);
76 }
77
78 static char *parse_ident(const unsigned char *context)
79 {
80         char *str, *ident, **tokens;
81
82         /*
83          * SELinux combines Role-Based Access Control (RBAC), Type
84          * Enforcment (TE) and optionally Multi-Level Security (MLS).
85          *
86          * When SELinux is enabled all processes and files are labeled
87          * with a contex that contains information such as user, role
88          * type (and optionally a level). E.g.
89          *
90          * $ ls -Z
91          * -rwxrwxr-x. wagi wagi unconfined_u:object_r:haifux_exec_t:s0 session_ui.py
92          *
93          * For identifyng application we (ab)using the type
94          * information. In the above example the haifux_exec_t type
95          * will be transfered to haifux_t as defined in the domain
96          * transition and thus we are able to identify the application
97          * as haifux_t.
98          */
99
100         str = g_strdup((const gchar*)context);
101         if (str == NULL)
102                 return NULL;
103
104         DBG("SELinux context %s", str);
105
106         tokens = g_strsplit(str, ":", 0);
107         if (tokens == NULL) {
108                 g_free(str);
109                 return NULL;
110         }
111
112         /* Use the SELinux type as identification token. */
113         ident = g_strdup(tokens[2]);
114
115         g_strfreev(tokens);
116         g_free(str);
117
118         return ident;
119 }
120
121 static struct policy_data *create_policy(const char *ident)
122 {
123         struct policy_data *policy;
124
125         DBG("ident %s", ident);
126
127         policy = g_try_new0(struct policy_data, 1);
128         if (policy == NULL)
129                 return NULL;
130
131         policy->config = connman_session_create_default_config();
132         if (policy->config == NULL) {
133                 g_free(policy);
134                 return NULL;
135         }
136
137         policy->refcount = 1;
138         policy->ident = g_strdup(ident);
139
140         g_hash_table_replace(policy_hash, policy->ident, policy);
141
142         return policy;
143 }
144
145 static struct policy_data *policy_ref(struct policy_data *policy)
146 {
147         DBG("%p %s ref %d", policy, policy->ident, policy->refcount + 1);
148
149         __sync_fetch_and_add(&policy->refcount, 1);
150
151         return policy;
152 }
153
154 static void policy_unref(struct policy_data *policy)
155 {
156         DBG(" %p %s ref %d", policy, policy->ident, policy->refcount - 1);
157
158         if (__sync_fetch_and_sub(&policy->refcount, 1) != 1)
159                 return;
160
161         g_hash_table_remove(policy_hash, policy->ident);
162 };
163
164 static void selinux_context_reply(const unsigned char *context, void *user_data,
165                                         int err)
166 {
167         struct create_data *data = user_data;
168         struct policy_data *policy;
169         struct connman_session_config *config = NULL;
170         char *ident = NULL;
171
172         DBG("session %p", data->session);
173
174         if (err < 0)
175                 goto done;
176
177         ident = parse_ident(context);
178         if (ident == NULL) {
179                 err = -EINVAL;
180                 goto done;
181         }
182
183         policy = g_hash_table_lookup(policy_hash, ident);
184         if (policy != NULL) {
185                 policy_ref(policy);
186                 policy->session = data->session;
187         } else {
188                 policy = create_policy(ident);
189                 if (policy == NULL) {
190                         err = -ENOMEM;
191                         goto done;
192                 }
193         }
194
195         g_hash_table_replace(session_hash, data->session, policy);
196         config = policy->config;
197
198 done:
199         (*data->callback)(data->session, config, data->user_data, err);
200
201         g_free(data);
202         g_free(ident);
203 }
204
205 static int policy_local_create(struct connman_session *session,
206                                 connman_session_config_cb callback,
207                                 void *user_data)
208 {
209         struct create_data *data;
210         const char *owner;
211         int err;
212
213         DBG("session %p", session);
214
215         data = g_try_new0(struct create_data, 1);
216         if (data == NULL)
217                 return -ENOMEM;
218
219         data->session = session;
220         data->callback = callback;
221         data->user_data = user_data;
222
223         owner = connman_session_get_owner(session);
224
225         err = connman_dbus_get_selinux_context(connection, owner,
226                                         selinux_context_reply,
227                                         data);
228         if (err < 0) {
229                 connman_error("Could not get SELinux context");
230                 g_free(data);
231                 return err;
232         }
233
234         return 0;
235 }
236
237 static void policy_local_destroy(struct connman_session *session)
238 {
239         struct policy_data *policy;
240
241         DBG("session %p", session);
242
243         policy = g_hash_table_lookup(session_hash, session);
244         g_hash_table_remove(session_hash, session);
245         policy->session = NULL;
246
247         policy_unref(policy);
248 }
249
250 static struct connman_session_policy session_policy_local = {
251         .name = "session local policy configuration",
252         .priority = CONNMAN_SESSION_POLICY_PRIORITY_DEFAULT,
253         .create = policy_local_create,
254         .destroy = policy_local_destroy,
255 };
256
257 static int load_keyfile(const char *pathname, GKeyFile **keyfile)
258 {
259         GError *error = NULL;
260         int err;
261
262         DBG("Loading %s", pathname);
263
264         *keyfile = g_key_file_new();
265
266         if (g_key_file_load_from_file(*keyfile, pathname, 0, &error) == FALSE)
267                 goto err;
268
269         return 0;
270
271 err:
272         /*
273          * The fancy G_FILE_ERROR_* codes are identical to the native
274          * error codes.
275          */
276         err = -error->code;
277
278         DBG("Unable to load %s: %s", pathname, error->message);
279         g_clear_error(&error);
280
281         g_key_file_free(*keyfile);
282         *keyfile = NULL;
283
284         return err;
285 }
286
287 static int load_policy(struct policy_data *policy)
288 {
289         struct connman_session_config *config = policy->config;
290         GKeyFile *keyfile;
291         char *pathname;
292         char *str, **tokens;
293         int i, err = 0;
294
295         pathname = g_strdup_printf("%s/%s", POLICYDIR, policy->ident);
296         if(pathname == NULL)
297                 return -ENOMEM;
298
299         err = load_keyfile(pathname, &keyfile);
300         if (err < 0) {
301                 g_free(pathname);
302
303                 if (err == -ENOENT) {
304                         /* Ignore empty files */
305                         return 0;
306                 }
307
308                 return err;
309         }
310
311         config->priority = g_key_file_get_boolean(keyfile, "Default",
312                                                 "Priority", NULL);
313
314         str = g_key_file_get_string(keyfile, "Default", "RoamingPolicy",
315                                 NULL);
316         if (str != NULL) {
317                 config->roaming_policy = connman_session_parse_roaming_policy(str);
318                 g_free(str);
319         } else {
320                 config->roaming_policy = CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
321         }
322
323         str = g_key_file_get_string(keyfile, "Default", "ConnectionType",
324                                 NULL);
325         if (str != NULL) {
326                 config->type = connman_session_parse_connection_type(str);
327                 g_free(str);
328         } else {
329                 config->type = CONNMAN_SESSION_TYPE_ANY;
330         }
331
332         config->ecall = g_key_file_get_boolean(keyfile, "Default",
333                                                 "EmergencyCall", NULL);
334
335         g_slist_free(config->allowed_bearers);
336         config->allowed_bearers = NULL;
337
338         str = g_key_file_get_string(keyfile, "Default", "AllowedBearers",
339                                 NULL);
340
341         if (str != NULL) {
342                 tokens = g_strsplit(str, " ", 0);
343
344                 for (i = 0; tokens[i] != NULL; i++) {
345                         err = connman_session_parse_bearers(tokens[i],
346                                         &config->allowed_bearers);
347                         if (err < 0)
348                                 break;
349                 }
350
351                 g_free(str);
352                 g_strfreev(tokens);
353         } else {
354                 config->allowed_bearers = g_slist_append(NULL,
355                                 GINT_TO_POINTER(CONNMAN_SERVICE_TYPE_UNKNOWN));
356                 if (config->allowed_bearers == NULL)
357                         err = -ENOMEM;
358         }
359
360         g_key_file_free(keyfile);
361         g_free(pathname);
362
363         return err;
364 }
365
366 static void update_session(struct connman_session *session)
367 {
368         if (connman_session_config_update(session) < 0)
369                 connman_session_destroy(session);
370 }
371
372 static void remove_policy(struct policy_data *policy)
373 {
374         connman_bool_t update = FALSE;
375         int err;
376
377         if (policy->session != NULL)
378                 update = TRUE;
379
380         policy_unref(policy);
381
382         if (update == FALSE)
383                 return;
384
385         err = connman_session_set_default_config(policy->config);
386         if (err < 0) {
387                 connman_session_destroy(policy->session);
388                 return;
389         }
390
391         update_session(policy->session);
392 }
393
394 static void notify_handler(struct inotify_event *event,
395                                         const char *ident)
396 {
397         struct policy_data *policy;
398
399         if (ident == NULL)
400                 return;
401
402         policy = g_hash_table_lookup(policy_hash, ident);
403
404         if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
405                 connman_info("Policy added for '%s'", ident);
406
407                 if (policy != NULL)
408                         policy_ref(policy);
409                 else
410                         policy = create_policy(ident);
411         }
412
413         if (policy == NULL)
414                 return;
415
416         if (event->mask & IN_MODIFY) {
417                 connman_info("Policy modifed for '%s'", ident);
418
419                 if (load_policy(policy) < 0) {
420                         remove_policy(policy);
421                         return;
422                 }
423         }
424
425         if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
426                 connman_info("Policy deleted for '%s'", ident);
427
428                 remove_policy(policy);
429                 return;
430         }
431
432         if (policy->session != NULL)
433                 update_session(policy->session);
434 }
435
436 static int read_policies(void)
437 {
438         GDir *dir;
439         int err = 0;
440
441         DBG("");
442
443         dir = g_dir_open(POLICYDIR, 0, NULL);
444         if (dir != NULL) {
445                 const gchar *file;
446
447                 while ((file = g_dir_read_name(dir)) != NULL) {
448                         struct policy_data *policy;
449
450                         policy = create_policy(file);
451                         if (policy == NULL) {
452                                 err = -ENOMEM;
453                                 break;
454                         }
455
456                         err = load_policy(policy);
457                         if (err < 0)
458                                 break;
459                 }
460
461                 g_dir_close(dir);
462         }
463
464         return err;
465 }
466
467 static int session_policy_local_init(void)
468 {
469         int err;
470
471         /* If the dir doesn't exist, create it */
472         if (g_file_test(POLICYDIR, G_FILE_TEST_IS_DIR) == FALSE) {
473                 if (mkdir(POLICYDIR, MODE) < 0) {
474                         if (errno != EEXIST)
475                                 return -errno;
476                 }
477         }
478
479         connection = connman_dbus_get_connection();
480         if (connection == NULL)
481                 return -EIO;
482
483         session_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
484                                                 NULL, NULL);
485         if (session_hash == NULL) {
486                 err = -ENOMEM;
487                 goto err;
488         }
489
490         policy_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
491                                         NULL, cleanup_policy);
492         if (policy_hash == NULL) {
493                 err = -ENOMEM;
494                 goto err;
495         }
496
497         err = connman_inotify_register(POLICYDIR, notify_handler);
498         if (err < 0)
499                 goto err;
500
501         err = read_policies();
502         if (err < 0)
503                 goto err_notify;
504
505         err = connman_session_policy_register(&session_policy_local);
506         if (err < 0)
507                 goto err_notify;
508
509         return 0;
510
511 err_notify:
512
513         connman_inotify_unregister(POLICYDIR, notify_handler);
514
515 err:
516         if (session_hash != NULL)
517                 g_hash_table_destroy(session_hash);
518         if (policy_hash != NULL)
519                 g_hash_table_destroy(policy_hash);
520
521         connman_session_policy_unregister(&session_policy_local);
522
523         dbus_connection_unref(connection);
524
525         return err;
526 }
527
528 static void session_policy_local_exit(void)
529 {
530         g_hash_table_destroy(session_hash);
531         g_hash_table_destroy(policy_hash);
532
533         connman_session_policy_unregister(&session_policy_local);
534
535         dbus_connection_unref(connection);
536
537         connman_inotify_unregister(POLICYDIR, notify_handler);
538 }
539
540 CONNMAN_PLUGIN_DEFINE(session_policy_local,
541                 "Session local file policy configuration plugin",
542                 VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
543                 session_policy_local_init, session_policy_local_exit)