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