Merge tag 'upstream/1.40' into tizen.
[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 #if !defined TIZEN_EXT
207         const char *ident, *host, *domain;
208 #else
209         const char *ident, *host, *domain, *name;
210 #endif
211         int err;
212
213         /* Strip off "provider_" prefix */
214         ident = group + 9;
215
216         if (strlen(ident) < 1)
217                 return -EINVAL;
218
219         config_provider = g_hash_table_lookup(config->provider_table, ident);
220         if (config_provider)
221                 return -EALREADY;
222
223         config_provider = g_try_new0(struct vpn_config_provider, 1);
224         if (!config_provider)
225                 return -ENOMEM;
226
227         config_provider->ident = g_strdup(ident);
228
229         config_provider->setting_strings = g_hash_table_new_full(g_str_hash,
230                                                 g_str_equal, g_free, g_free);
231
232         add_keys(config_provider, keyfile, group);
233
234         host = get_string(config_provider, "Host");
235         domain = get_string(config_provider, "Domain");
236 #if !defined TIZEN_EXT
237         if (host) {
238                 char *id = __vpn_provider_create_identifier(host, domain);
239 #else
240         name = get_string(config_provider, "Name");
241         if (host && name) {
242                 char *id = __vpn_provider_create_identifier(host, domain, name);
243 #endif
244
245                 struct vpn_provider *provider;
246                 provider = __vpn_provider_lookup(id);
247                 if (provider) {
248                         if (action == REMOVE) {
249                                 __vpn_provider_delete(provider);
250                                 err = 0;
251                         } else {
252                                 connman_warn("Provider configuration %s "
253                                                 "already exist", id);
254                                 err = -EALREADY;
255                         }
256
257                         g_free(id);
258                         goto err;
259                 }
260
261                 config_provider->provider_identifier = id;
262
263                 DBG("provider identifier %s", id);
264         } else {
265 #if !defined TIZEN_EXT
266                 DBG("invalid values host %s domain %s", host, domain);
267 #else
268                 DBG("invalid configuration: no host specified");
269 #endif
270                 err = -EINVAL;
271                 goto err;
272         }
273
274         config_provider->config_ident = g_strdup(config->ident);
275         config_provider->config_entry = g_strdup_printf("provider_%s",
276                                                 config_provider->ident);
277
278         err = __vpn_provider_create_from_config(
279                                         config_provider->setting_strings,
280                                         config_provider->config_ident,
281                                         config_provider->config_entry);
282         if (err != 0) {
283                 DBG("Cannot create provider from config file (%d/%s)",
284                         -err, strerror(-err));
285                 goto err;
286         }
287
288         g_hash_table_insert(config->provider_table, config_provider->ident,
289                                 config_provider);
290
291
292         connman_info("Added provider configuration %s",
293                                                 config_provider->ident);
294         return 0;
295
296 err:
297         g_free(config_provider->ident);
298         g_free(config_provider->type);
299         g_free(config_provider->name);
300         g_free(config_provider->host);
301         g_free(config_provider->domain);
302         g_free(config_provider->networks);
303         g_hash_table_destroy(config_provider->setting_strings);
304         g_free(config_provider);
305
306         return err;
307 }
308
309 static void check_keys(GKeyFile *keyfile, const char *group,
310                         const char **possible_keys)
311 {
312         char **avail_keys;
313         gsize nb_avail_keys, i, j;
314
315         avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
316         if (!avail_keys)
317                 return;
318
319         for (i = 0 ; i < nb_avail_keys; i++) {
320                 for (j = 0; possible_keys[j] ; j++)
321                         if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
322                                 break;
323
324                 if (!possible_keys[j])
325                         connman_warn("Unknown configuration key %s in [%s]",
326                                         avail_keys[i], group);
327         }
328
329         g_strfreev(avail_keys);
330 }
331
332 static int load_config(struct vpn_config *config, char *path, enum what action)
333 {
334         GKeyFile *keyfile;
335         gsize length;
336         char **groups;
337         char *str;
338         bool found = false;
339         int i;
340
341         DBG("config %p", config);
342
343         keyfile = __connman_storage_load_provider_config(config->ident);
344         if (!keyfile)
345                 return -EIO;
346
347         /* Verify keys validity of the global section */
348         check_keys(keyfile, "global", config_possible_keys);
349
350         str = __vpn_config_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
351         if (str) {
352                 g_free(config->name);
353                 config->name = str;
354         }
355
356         str = __vpn_config_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
357         if (str) {
358                 g_free(config->description);
359                 config->description = str;
360         }
361
362         groups = g_key_file_get_groups(keyfile, &length);
363
364         for (i = 0; groups[i]; i++) {
365                 if (g_str_has_prefix(groups[i], "provider_")) {
366                         int ret = load_provider(keyfile, groups[i], config,
367                                                 action);
368                         if (ret == 0 || ret == -EALREADY)
369                                 found = true;
370                 }
371         }
372
373         if (!found)
374                 connman_warn("Config file %s/%s.config does not contain any "
375                         "configuration that can be provisioned!",
376                         path, config->ident);
377
378         g_strfreev(groups);
379
380         g_key_file_free(keyfile);
381
382         return 0;
383 }
384
385 static struct vpn_config *create_config(const char *ident)
386 {
387         struct vpn_config *config;
388
389         DBG("ident %s", ident);
390
391         if (g_hash_table_lookup(config_table, ident))
392                 return NULL;
393
394         config = g_try_new0(struct vpn_config, 1);
395         if (!config)
396                 return NULL;
397
398         config->ident = g_strdup(ident);
399
400         config->provider_table = g_hash_table_new_full(g_str_hash, g_str_equal,
401                                                 NULL, unregister_provider);
402
403         g_hash_table_insert(config_table, config->ident, config);
404
405         connman_info("Adding configuration %s", config->ident);
406
407         return config;
408 }
409
410 static bool validate_ident(const char *ident)
411 {
412         unsigned int i;
413
414         if (!ident)
415                 return false;
416
417         for (i = 0; i < strlen(ident); i++)
418                 if (!g_ascii_isprint(ident[i]))
419                         return false;
420
421         return true;
422 }
423
424 static char *get_dir(void)
425 {
426         return g_strdup_printf("%s", VPN_STORAGEDIR);
427 }
428
429 static int read_configs(void)
430 {
431         GDir *dir;
432         char *path = get_dir();
433
434         DBG("path %s", path);
435
436         dir = g_dir_open(path, 0, NULL);
437         if (dir) {
438                 const gchar *file;
439
440                 while ((file = g_dir_read_name(dir))) {
441                         GString *str;
442                         gchar *ident;
443
444                         if (!g_str_has_suffix(file, ".config"))
445                                 continue;
446
447                         ident = g_strrstr(file, ".config");
448                         if (!ident)
449                                 continue;
450
451                         str = g_string_new_len(file, ident - file);
452                         if (!str)
453                                 continue;
454
455                         ident = g_string_free(str, FALSE);
456
457                         if (validate_ident(ident)) {
458                                 struct vpn_config *config;
459
460                                 config = create_config(ident);
461                                 if (config)
462                                         load_config(config, path, ADD);
463                         } else {
464                                 connman_error("Invalid config ident %s", ident);
465                         }
466                         g_free(ident);
467                 }
468
469                 g_dir_close(dir);
470         }
471
472         g_free(path);
473
474         return 0;
475 }
476
477 static void config_notify_handler(struct inotify_event *event,
478                                         const char *ident)
479 {
480         char *ext;
481
482         if (!ident)
483                 return;
484
485         if (!g_str_has_suffix(ident, ".config"))
486                 return;
487
488         ext = g_strrstr(ident, ".config");
489         if (!ext)
490                 return;
491
492         *ext = '\0';
493
494         if (!validate_ident(ident)) {
495                 connman_error("Invalid config ident %s", ident);
496                 return;
497         }
498
499         if (event->mask & IN_CREATE)
500                 return;
501
502         if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
503                 g_hash_table_remove(config_table, ident);
504                 return;
505         }
506
507         if (event->mask & (IN_MODIFY | IN_MOVED_TO)) {
508                 struct vpn_config *config;
509                 char *path = get_dir();
510
511                 config = g_hash_table_lookup(config_table, ident);
512                 if (config) {
513                         g_hash_table_remove_all(config->provider_table);
514                         load_config(config, path, REMOVE);
515
516                         /* Re-scan the config file for any changes */
517                         g_hash_table_remove_all(config->provider_table);
518                         load_config(config, path, ADD);
519                 } else {
520                         /*
521                          * Inotify will send create event followed by modify
522                          * event for any config file that is copied to
523                          * monitored directory. So in practice we should just
524                          * ignore the create event and trust only the modify
525                          * one in order to avoid create/remove/create loop
526                          */
527                         config = create_config(ident);
528                         if (config)
529                                 load_config(config, path, ADD);
530                 }
531
532                 g_free(path);
533         }
534 }
535
536 int __vpn_config_init(void)
537 {
538         char *dir = get_dir();
539
540         DBG("");
541
542         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
543                                                 NULL, unregister_config);
544
545         connman_inotify_register(dir, config_notify_handler);
546
547         g_free(dir);
548
549         return read_configs();
550 }
551
552 void __vpn_config_cleanup(void)
553 {
554         char *dir = get_dir();
555
556         DBG("");
557
558         cleanup = true;
559
560         connman_inotify_unregister(dir, config_notify_handler);
561
562         g_free(dir);
563
564         g_hash_table_destroy(config_table);
565         config_table = NULL;
566
567         cleanup = false;
568 }
569
570 char *__vpn_config_get_string(GKeyFile *key_file,
571         const char *group_name, const char *key, GError **error)
572 {
573         char *str = g_key_file_get_string(key_file, group_name, key, error);
574         if (!str)
575                 return NULL;
576
577         return g_strchomp(str);
578 }
579
580 char **__vpn_config_get_string_list(GKeyFile *key_file,
581         const char *group_name, const char *key, gsize *length, GError **error)
582 {
583         char **p;
584         char **strlist = g_key_file_get_string_list(key_file, group_name, key,
585                 length, error);
586         if (!strlist)
587                 return NULL;
588
589         p = strlist;
590         while (*p) {
591                 *p = g_strstrip(*p);
592                 p++;
593         }
594
595         return strlist;
596 }
597
598 bool __vpn_config_get_boolean(GKeyFile *key_file, const char *group_name,
599                         const char *key, bool default_value)
600 {
601         GError *error = NULL;
602         bool val;
603
604         val = g_key_file_get_boolean(key_file, group_name, key, &error);
605         if (error) {
606                 g_error_free(error);
607                 return default_value;
608         }
609
610         return val;
611 }