session_policy_local: Empty policy list indicated no match all
[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         if (policy == NULL)
245                 return;
246
247         g_hash_table_remove(session_hash, session);
248         policy->session = NULL;
249         policy_unref(policy);
250 }
251
252 static struct connman_session_policy session_policy_local = {
253         .name = "session local policy configuration",
254         .priority = CONNMAN_SESSION_POLICY_PRIORITY_DEFAULT,
255         .create = policy_local_create,
256         .destroy = policy_local_destroy,
257 };
258
259 static int load_keyfile(const char *pathname, GKeyFile **keyfile)
260 {
261         GError *error = NULL;
262         int err;
263
264         DBG("Loading %s", pathname);
265
266         *keyfile = g_key_file_new();
267
268         if (g_key_file_load_from_file(*keyfile, pathname, 0, &error) == FALSE)
269                 goto err;
270
271         return 0;
272
273 err:
274         /*
275          * The fancy G_FILE_ERROR_* codes are identical to the native
276          * error codes.
277          */
278         err = -error->code;
279
280         DBG("Unable to load %s: %s", pathname, error->message);
281         g_clear_error(&error);
282
283         g_key_file_free(*keyfile);
284         *keyfile = NULL;
285
286         return err;
287 }
288
289 static int load_policy(struct policy_data *policy)
290 {
291         struct connman_session_config *config = policy->config;
292         GKeyFile *keyfile;
293         char *pathname;
294         char *str, **tokens;
295         int i, err = 0;
296
297         connman_session_set_default_config(config);
298
299         pathname = g_strdup_printf("%s/%s", POLICYDIR, policy->ident);
300         if(pathname == NULL)
301                 return -ENOMEM;
302
303         err = load_keyfile(pathname, &keyfile);
304         if (err < 0) {
305                 g_free(pathname);
306
307                 if (err == -ENOENT) {
308                         /* Ignore empty files */
309                         return 0;
310                 }
311
312                 return err;
313         }
314
315         config->priority = g_key_file_get_boolean(keyfile, "Default",
316                                                 "Priority", NULL);
317
318         str = g_key_file_get_string(keyfile, "Default", "RoamingPolicy",
319                                 NULL);
320         if (str != NULL) {
321                 config->roaming_policy = connman_session_parse_roaming_policy(str);
322                 g_free(str);
323         }
324
325         str = g_key_file_get_string(keyfile, "Default", "ConnectionType",
326                                 NULL);
327         if (str != NULL) {
328                 config->type = connman_session_parse_connection_type(str);
329                 g_free(str);
330         }
331
332         config->ecall = g_key_file_get_boolean(keyfile, "Default",
333                                                 "EmergencyCall", NULL);
334
335         str = g_key_file_get_string(keyfile, "Default", "AllowedBearers",
336                                 NULL);
337
338         if (str != NULL) {
339                 g_slist_free(config->allowed_bearers);
340                 config->allowed_bearers = NULL;
341
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         }
354
355         g_key_file_free(keyfile);
356         g_free(pathname);
357
358         return err;
359 }
360
361 static void update_session(struct connman_session *session)
362 {
363         if (connman_session_config_update(session) < 0)
364                 connman_session_destroy(session);
365 }
366
367 static void remove_policy(struct policy_data *policy)
368 {
369         connman_bool_t update = FALSE;
370         int err;
371
372         if (policy->session != NULL)
373                 update = TRUE;
374
375         policy_unref(policy);
376
377         if (update == FALSE)
378                 return;
379
380         err = connman_session_set_default_config(policy->config);
381         if (err < 0) {
382                 connman_session_destroy(policy->session);
383                 return;
384         }
385
386         update_session(policy->session);
387 }
388
389 static void notify_handler(struct inotify_event *event,
390                                         const char *ident)
391 {
392         struct policy_data *policy;
393
394         if (ident == NULL)
395                 return;
396
397         policy = g_hash_table_lookup(policy_hash, ident);
398
399         if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
400                 connman_info("Policy added for '%s'", ident);
401
402                 if (policy != NULL)
403                         policy_ref(policy);
404                 else
405                         policy = create_policy(ident);
406         }
407
408         if (policy == NULL)
409                 return;
410
411         if (event->mask & IN_MODIFY) {
412                 connman_info("Policy modifed for '%s'", ident);
413
414                 if (load_policy(policy) < 0) {
415                         remove_policy(policy);
416                         return;
417                 }
418         }
419
420         if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
421                 connman_info("Policy deleted for '%s'", ident);
422
423                 remove_policy(policy);
424                 return;
425         }
426
427         if (policy->session != NULL)
428                 update_session(policy->session);
429 }
430
431 static int read_policies(void)
432 {
433         GDir *dir;
434         int err = 0;
435
436         DBG("");
437
438         dir = g_dir_open(POLICYDIR, 0, NULL);
439         if (dir != NULL) {
440                 const gchar *file;
441
442                 while ((file = g_dir_read_name(dir)) != NULL) {
443                         struct policy_data *policy;
444
445                         policy = create_policy(file);
446                         if (policy == NULL) {
447                                 err = -ENOMEM;
448                                 break;
449                         }
450
451                         err = load_policy(policy);
452                         if (err < 0)
453                                 break;
454                 }
455
456                 g_dir_close(dir);
457         }
458
459         return err;
460 }
461
462 static int session_policy_local_init(void)
463 {
464         int err;
465
466         /* If the dir doesn't exist, create it */
467         if (g_file_test(POLICYDIR, G_FILE_TEST_IS_DIR) == FALSE) {
468                 if (mkdir(POLICYDIR, MODE) < 0) {
469                         if (errno != EEXIST)
470                                 return -errno;
471                 }
472         }
473
474         connection = connman_dbus_get_connection();
475         if (connection == NULL)
476                 return -EIO;
477
478         session_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
479                                                 NULL, NULL);
480         if (session_hash == NULL) {
481                 err = -ENOMEM;
482                 goto err;
483         }
484
485         policy_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
486                                         NULL, cleanup_policy);
487         if (policy_hash == NULL) {
488                 err = -ENOMEM;
489                 goto err;
490         }
491
492         err = connman_inotify_register(POLICYDIR, notify_handler);
493         if (err < 0)
494                 goto err;
495
496         err = read_policies();
497         if (err < 0)
498                 goto err_notify;
499
500         err = connman_session_policy_register(&session_policy_local);
501         if (err < 0)
502                 goto err_notify;
503
504         return 0;
505
506 err_notify:
507
508         connman_inotify_unregister(POLICYDIR, notify_handler);
509
510 err:
511         if (session_hash != NULL)
512                 g_hash_table_destroy(session_hash);
513         if (policy_hash != NULL)
514                 g_hash_table_destroy(policy_hash);
515
516         connman_session_policy_unregister(&session_policy_local);
517
518         dbus_connection_unref(connection);
519
520         return err;
521 }
522
523 static void session_policy_local_exit(void)
524 {
525         g_hash_table_destroy(session_hash);
526         g_hash_table_destroy(policy_hash);
527
528         connman_session_policy_unregister(&session_policy_local);
529
530         dbus_connection_unref(connection);
531
532         connman_inotify_unregister(POLICYDIR, notify_handler);
533 }
534
535 CONNMAN_PLUGIN_DEFINE(session_policy_local,
536                 "Session local file policy configuration plugin",
537                 VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
538                 session_policy_local_init, session_policy_local_exit)