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