Imported Upstream version 1.38
[platform/upstream/connman.git] / vpn / vpn-config.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012-2013  Intel Corporation. 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 <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <sys/vfs.h>
31 #include <sys/inotify.h>
32 #include <glib.h>
33
34 #include <connman/log.h>
35 #include "../src/connman.h"
36
37 #include "vpn.h"
38
39 enum what {
40         REMOVE = 1,
41         ADD = 2,
42 };
43
44 struct vpn_config_provider {
45         char *provider_identifier;
46         char *ident;
47         char *name;
48         char *type;
49         char *host;
50         char *domain;
51         char *networks;
52         GHashTable *setting_strings;
53
54         char *config_ident; /* file prefix */
55         char *config_entry; /* entry name */
56 };
57
58 struct vpn_config {
59         char *ident;
60         char *name;
61         char *description;
62         GHashTable *provider_table;
63 };
64
65 static GHashTable *config_table = NULL;
66
67 static bool cleanup = false;
68
69 /* Definition of possible strings in the .config files */
70 #define CONFIG_KEY_NAME                "Name"
71 #define CONFIG_KEY_DESC                "Description"
72
73 static const char *config_possible_keys[] = {
74         CONFIG_KEY_NAME,
75         CONFIG_KEY_DESC,
76         NULL,
77 };
78
79 static void unregister_config(gpointer data)
80 {
81         struct vpn_config *config = data;
82
83         connman_info("Removing configuration %s", config->ident);
84
85         g_hash_table_destroy(config->provider_table);
86
87         g_free(config->description);
88         g_free(config->name);
89         g_free(config->ident);
90         g_free(config);
91 }
92
93 static void unregister_provider(gpointer data)
94 {
95         struct vpn_config_provider *config_provider = data;
96         struct vpn_provider *provider;
97         char *provider_id;
98
99         if (cleanup)
100                 goto free_only;
101
102         provider_id = config_provider->provider_identifier;
103
104         connman_info("Removing provider configuration %s provider %s",
105                                 config_provider->ident, provider_id);
106
107         provider = __vpn_provider_lookup(provider_id);
108         if (provider)
109                 __vpn_provider_delete(provider);
110         else {
111                 if (!__connman_storage_remove_provider(provider_id))
112                         DBG("Could not remove all files for provider %s",
113                                                                 provider_id);
114         }
115
116 free_only:
117         g_free(config_provider->ident);
118         g_free(config_provider->type);
119         g_free(config_provider->name);
120         g_free(config_provider->host);
121         g_free(config_provider->domain);
122         g_free(config_provider->networks);
123         g_hash_table_destroy(config_provider->setting_strings);
124         g_free(config_provider->provider_identifier);
125         g_free(config_provider->config_ident);
126         g_free(config_provider->config_entry);
127         g_free(config_provider);
128 }
129
130 static int set_string(struct vpn_config_provider *config_provider,
131                                         const char *key, const char *value)
132 {
133         DBG("provider %p key %s value %s", config_provider, key, value);
134
135         if (g_str_equal(key, "Type")) {
136                 g_free(config_provider->type);
137                 config_provider->type = g_strdup(value);
138         } else if (g_str_equal(key, "Name")) {
139                 g_free(config_provider->name);
140                 config_provider->name = g_strdup(value);
141         } else if (g_str_equal(key, "Host")) {
142                 g_free(config_provider->host);
143                 config_provider->host = g_strdup(value);
144         } else if (g_str_equal(key, "Domain")) {
145                 g_free(config_provider->domain);
146                 config_provider->domain = g_strdup(value);
147         } else if (g_str_equal(key, "Networks")) {
148                 g_free(config_provider->networks);
149                 config_provider->networks = g_strdup(value);
150         }
151
152         g_hash_table_replace(config_provider->setting_strings,
153                                         g_strdup(key), g_strdup(value));
154         return 0;
155 }
156
157 static const char *get_string(struct vpn_config_provider *config_provider,
158                                                         const char *key)
159 {
160         DBG("provider %p key %s", config_provider, key);
161
162         if (g_str_equal(key, "Type"))
163                 return config_provider->type;
164         else if (g_str_equal(key, "Name"))
165                 return config_provider->name;
166         else if (g_str_equal(key, "Host"))
167                 return config_provider->host;
168         else if (g_str_equal(key, "Domain"))
169                 return config_provider->domain;
170         else if (g_str_equal(key, "Networks"))
171                 return config_provider->networks;
172
173         return g_hash_table_lookup(config_provider->setting_strings, key);
174 }
175
176 static void add_keys(struct vpn_config_provider *config_provider,
177                         GKeyFile *keyfile, const char *group)
178 {
179         char **avail_keys;
180         gsize nb_avail_keys, i;
181
182         avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
183         if (!avail_keys)
184                 return;
185
186         for (i = 0 ; i < nb_avail_keys; i++) {
187                 char *value = g_key_file_get_value(keyfile, group,
188                                                 avail_keys[i], NULL);
189                 if (!value) {
190                         connman_warn("Cannot find value for %s",
191                                                         avail_keys[i]);
192                         continue;
193                 }
194
195                 set_string(config_provider, avail_keys[i], value);
196                 g_free(value);
197         }
198
199         g_strfreev(avail_keys);
200 }
201
202 static int load_provider(GKeyFile *keyfile, const char *group,
203                                 struct vpn_config *config, enum what action)
204 {
205         struct vpn_config_provider *config_provider;
206         const char *ident, *host, *domain;
207         int err;
208
209         /* Strip off "provider_" prefix */
210         ident = group + 9;
211
212         if (strlen(ident) < 1)
213                 return -EINVAL;
214
215         config_provider = g_hash_table_lookup(config->provider_table, ident);
216         if (config_provider)
217                 return -EALREADY;
218
219         config_provider = g_try_new0(struct vpn_config_provider, 1);
220         if (!config_provider)
221                 return -ENOMEM;
222
223         config_provider->ident = g_strdup(ident);
224
225         config_provider->setting_strings = g_hash_table_new_full(g_str_hash,
226                                                 g_str_equal, g_free, g_free);
227
228         add_keys(config_provider, keyfile, group);
229
230         host = get_string(config_provider, "Host");
231         domain = get_string(config_provider, "Domain");
232         if (host && domain) {
233                 char *id = __vpn_provider_create_identifier(host, domain);
234
235                 struct vpn_provider *provider;
236                 provider = __vpn_provider_lookup(id);
237                 if (provider) {
238                         if (action == REMOVE) {
239                                 __vpn_provider_delete(provider);
240                                 err = 0;
241                         } else {
242                                 connman_warn("Provider configuration %s "
243                                                 "already exist", id);
244                                 err = -EALREADY;
245                         }
246
247                         g_free(id);
248                         goto err;
249                 }
250
251                 config_provider->provider_identifier = id;
252
253                 DBG("provider identifier %s", id);
254         } else {
255                 DBG("invalid values host %s domain %s", host, domain);
256                 err = -EINVAL;
257                 goto err;
258         }
259
260         config_provider->config_ident = g_strdup(config->ident);
261         config_provider->config_entry = g_strdup_printf("provider_%s",
262                                                 config_provider->ident);
263
264         err = __vpn_provider_create_from_config(
265                                         config_provider->setting_strings,
266                                         config_provider->config_ident,
267                                         config_provider->config_entry);
268         if (err != 0) {
269                 DBG("Cannot create provider from config file (%d/%s)",
270                         -err, strerror(-err));
271                 goto err;
272         }
273
274         g_hash_table_insert(config->provider_table, config_provider->ident,
275                                 config_provider);
276
277
278         connman_info("Added provider configuration %s",
279                                                 config_provider->ident);
280         return 0;
281
282 err:
283         g_free(config_provider->ident);
284         g_free(config_provider->type);
285         g_free(config_provider->name);
286         g_free(config_provider->host);
287         g_free(config_provider->domain);
288         g_free(config_provider->networks);
289         g_hash_table_destroy(config_provider->setting_strings);
290         g_free(config_provider);
291
292         return err;
293 }
294
295 static void check_keys(GKeyFile *keyfile, const char *group,
296                         const char **possible_keys)
297 {
298         char **avail_keys;
299         gsize nb_avail_keys, i, j;
300
301         avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
302         if (!avail_keys)
303                 return;
304
305         for (i = 0 ; i < nb_avail_keys; i++) {
306                 for (j = 0; possible_keys[j] ; j++)
307                         if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
308                                 break;
309
310                 if (!possible_keys[j])
311                         connman_warn("Unknown configuration key %s in [%s]",
312                                         avail_keys[i], group);
313         }
314
315         g_strfreev(avail_keys);
316 }
317
318 static int load_config(struct vpn_config *config, char *path, enum what action)
319 {
320         GKeyFile *keyfile;
321         gsize length;
322         char **groups;
323         char *str;
324         bool found = false;
325         int i;
326
327         DBG("config %p", config);
328
329         keyfile = __connman_storage_load_provider_config(config->ident);
330         if (!keyfile)
331                 return -EIO;
332
333         /* Verify keys validity of the global section */
334         check_keys(keyfile, "global", config_possible_keys);
335
336         str = __vpn_config_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
337         if (str) {
338                 g_free(config->name);
339                 config->name = str;
340         }
341
342         str = __vpn_config_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
343         if (str) {
344                 g_free(config->description);
345                 config->description = str;
346         }
347
348         groups = g_key_file_get_groups(keyfile, &length);
349
350         for (i = 0; groups[i]; i++) {
351                 if (g_str_has_prefix(groups[i], "provider_")) {
352                         int ret = load_provider(keyfile, groups[i], config,
353                                                 action);
354                         if (ret == 0 || ret == -EALREADY)
355                                 found = true;
356                 }
357         }
358
359         if (!found)
360                 connman_warn("Config file %s/%s.config does not contain any "
361                         "configuration that can be provisioned!",
362                         path, config->ident);
363
364         g_strfreev(groups);
365
366         g_key_file_free(keyfile);
367
368         return 0;
369 }
370
371 static struct vpn_config *create_config(const char *ident)
372 {
373         struct vpn_config *config;
374
375         DBG("ident %s", ident);
376
377         if (g_hash_table_lookup(config_table, ident))
378                 return NULL;
379
380         config = g_try_new0(struct vpn_config, 1);
381         if (!config)
382                 return NULL;
383
384         config->ident = g_strdup(ident);
385
386         config->provider_table = g_hash_table_new_full(g_str_hash, g_str_equal,
387                                                 NULL, unregister_provider);
388
389         g_hash_table_insert(config_table, config->ident, config);
390
391         connman_info("Adding configuration %s", config->ident);
392
393         return config;
394 }
395
396 static bool validate_ident(const char *ident)
397 {
398         unsigned int i;
399
400         if (!ident)
401                 return false;
402
403         for (i = 0; i < strlen(ident); i++)
404                 if (!g_ascii_isprint(ident[i]))
405                         return false;
406
407         return true;
408 }
409
410 static char *get_dir(void)
411 {
412         return g_strdup_printf("%s", VPN_STORAGEDIR);
413 }
414
415 static int read_configs(void)
416 {
417         GDir *dir;
418         char *path = get_dir();
419
420         DBG("path %s", path);
421
422         dir = g_dir_open(path, 0, NULL);
423         if (dir) {
424                 const gchar *file;
425
426                 while ((file = g_dir_read_name(dir))) {
427                         GString *str;
428                         gchar *ident;
429
430                         if (!g_str_has_suffix(file, ".config"))
431                                 continue;
432
433                         ident = g_strrstr(file, ".config");
434                         if (!ident)
435                                 continue;
436
437                         str = g_string_new_len(file, ident - file);
438                         if (!str)
439                                 continue;
440
441                         ident = g_string_free(str, FALSE);
442
443                         if (validate_ident(ident)) {
444                                 struct vpn_config *config;
445
446                                 config = create_config(ident);
447                                 if (config)
448                                         load_config(config, path, ADD);
449                         } else {
450                                 connman_error("Invalid config ident %s", ident);
451                         }
452                         g_free(ident);
453                 }
454
455                 g_dir_close(dir);
456         }
457
458         g_free(path);
459
460         return 0;
461 }
462
463 static void config_notify_handler(struct inotify_event *event,
464                                         const char *ident)
465 {
466         char *ext;
467
468         if (!ident)
469                 return;
470
471         if (!g_str_has_suffix(ident, ".config"))
472                 return;
473
474         ext = g_strrstr(ident, ".config");
475         if (!ext)
476                 return;
477
478         *ext = '\0';
479
480         if (!validate_ident(ident)) {
481                 connman_error("Invalid config ident %s", ident);
482                 return;
483         }
484
485         if (event->mask & IN_CREATE)
486                 return;
487
488         if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
489                 g_hash_table_remove(config_table, ident);
490                 return;
491         }
492
493         if (event->mask & (IN_MODIFY | IN_MOVED_TO)) {
494                 struct vpn_config *config;
495                 char *path = get_dir();
496
497                 config = g_hash_table_lookup(config_table, ident);
498                 if (config) {
499                         g_hash_table_remove_all(config->provider_table);
500                         load_config(config, path, REMOVE);
501
502                         /* Re-scan the config file for any changes */
503                         g_hash_table_remove_all(config->provider_table);
504                         load_config(config, path, ADD);
505                 } else {
506                         /*
507                          * Inotify will send create event followed by modify
508                          * event for any config file that is copied to
509                          * monitored directory. So in practice we should just
510                          * ignore the create event and trust only the modify
511                          * one in order to avoid create/remove/create loop
512                          */
513                         config = create_config(ident);
514                         if (config)
515                                 load_config(config, path, ADD);
516                 }
517
518                 g_free(path);
519         }
520 }
521
522 int __vpn_config_init(void)
523 {
524         char *dir = get_dir();
525
526         DBG("");
527
528         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
529                                                 NULL, unregister_config);
530
531         connman_inotify_register(dir, config_notify_handler);
532
533         g_free(dir);
534
535         return read_configs();
536 }
537
538 void __vpn_config_cleanup(void)
539 {
540         char *dir = get_dir();
541
542         DBG("");
543
544         cleanup = true;
545
546         connman_inotify_unregister(dir, config_notify_handler);
547
548         g_free(dir);
549
550         g_hash_table_destroy(config_table);
551         config_table = NULL;
552
553         cleanup = false;
554 }
555
556 char *__vpn_config_get_string(GKeyFile *key_file,
557         const char *group_name, const char *key, GError **error)
558 {
559         char *str = g_key_file_get_string(key_file, group_name, key, error);
560         if (!str)
561                 return NULL;
562
563         return g_strchomp(str);
564 }
565
566 char **__vpn_config_get_string_list(GKeyFile *key_file,
567         const char *group_name, const char *key, gsize *length, GError **error)
568 {
569         char **p;
570         char **strlist = g_key_file_get_string_list(key_file, group_name, key,
571                 length, error);
572         if (!strlist)
573                 return NULL;
574
575         p = strlist;
576         while (*p) {
577                 *p = g_strstrip(*p);
578                 p++;
579         }
580
581         return strlist;
582 }