Imported Upstream version 1.37
[platform/upstream/connman.git] / src / config.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-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 <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <sys/vfs.h>
32 #include <sys/inotify.h>
33 #include <netdb.h>
34 #include <glib.h>
35
36 #include <connman/provision.h>
37 #include <connman/ipaddress.h>
38 #include "connman.h"
39
40 struct connman_config_service {
41         char *ident;
42         char *name;
43         char *type;
44         void *ssid;
45         unsigned int ssid_len;
46         char *eap;
47         char *identity;
48         char *anonymous_identity;
49         char *ca_cert_file;
50         char *subject_match;
51         char *altsubject_match;
52         char *domain_suffix_match;
53         char *domain_match;
54         char *client_cert_file;
55         char *private_key_file;
56         char *private_key_passphrase;
57         char *private_key_passphrase_type;
58         char *phase2;
59         char *passphrase;
60         enum connman_service_security security;
61         GSList *service_identifiers;
62         char *config_ident; /* file prefix */
63         char *config_entry; /* entry name */
64         bool hidden;
65         bool virtual;
66         char *virtual_file;
67         char *ipv4_address;
68         char *ipv4_netmask;
69         char *ipv4_gateway;
70         char *ipv6_address;
71         unsigned char ipv6_prefix_length;
72         char *ipv6_gateway;
73         char *ipv6_privacy;
74         char *mac;
75         bool mdns;
76         char **nameservers;
77         char **search_domains;
78         char **timeservers;
79         char *domain_name;
80 };
81
82 struct connman_config {
83         char *ident;
84         char *name;
85         char *description;
86         GHashTable *service_table;
87 };
88
89 static GHashTable *config_table = NULL;
90
91 static bool cleanup = false;
92
93 /* Definition of possible strings in the .config files */
94 #define CONFIG_KEY_NAME                "Name"
95 #define CONFIG_KEY_DESC                "Description"
96
97 #define SERVICE_KEY_TYPE               "Type"
98 #define SERVICE_KEY_NAME               "Name"
99 #define SERVICE_KEY_SSID               "SSID"
100 #define SERVICE_KEY_EAP                "EAP"
101 #define SERVICE_KEY_CA_CERT            "CACertFile"
102 #define SERVICE_KEY_CL_CERT            "ClientCertFile"
103 #define SERVICE_KEY_PRV_KEY            "PrivateKeyFile"
104 #define SERVICE_KEY_PRV_KEY_PASS       "PrivateKeyPassphrase"
105 #define SERVICE_KEY_PRV_KEY_PASS_TYPE  "PrivateKeyPassphraseType"
106 #define SERVICE_KEY_IDENTITY           "Identity"
107 #define SERVICE_KEY_ANONYMOUS_IDENTITY "AnonymousIdentity"
108 #define SERVICE_KEY_SUBJECT_MATCH      "SubjectMatch"
109 #define SERVICE_KEY_ALT_SUBJECT_MATCH  "AltSubjectMatch"
110 #define SERVICE_KEY_DOMAIN_SUFF_MATCH  "DomainSuffixMatch"
111 #define SERVICE_KEY_DOMAIN_MATCH       "DomainMatch"
112 #define SERVICE_KEY_PHASE2             "Phase2"
113 #define SERVICE_KEY_PASSPHRASE         "Passphrase"
114 #define SERVICE_KEY_SECURITY           "Security"
115 #define SERVICE_KEY_HIDDEN             "Hidden"
116 #define SERVICE_KEY_MDNS               "mDNS"
117
118 #define SERVICE_KEY_IPv4               "IPv4"
119 #define SERVICE_KEY_IPv6               "IPv6"
120 #define SERVICE_KEY_IPv6_PRIVACY       "IPv6.Privacy"
121 #define SERVICE_KEY_MAC                "MAC"
122 #define SERVICE_KEY_NAMESERVERS        "Nameservers"
123 #define SERVICE_KEY_SEARCH_DOMAINS     "SearchDomains"
124 #define SERVICE_KEY_TIMESERVERS        "Timeservers"
125 #define SERVICE_KEY_DOMAIN             "Domain"
126
127 static const char *config_possible_keys[] = {
128         CONFIG_KEY_NAME,
129         CONFIG_KEY_DESC,
130         NULL,
131 };
132
133 static const char *service_possible_keys[] = {
134         SERVICE_KEY_TYPE,
135         SERVICE_KEY_NAME,
136         SERVICE_KEY_SSID,
137         SERVICE_KEY_EAP,
138         SERVICE_KEY_CA_CERT,
139         SERVICE_KEY_CL_CERT,
140         SERVICE_KEY_PRV_KEY,
141         SERVICE_KEY_PRV_KEY_PASS,
142         SERVICE_KEY_PRV_KEY_PASS_TYPE,
143         SERVICE_KEY_IDENTITY,
144         SERVICE_KEY_ANONYMOUS_IDENTITY,
145         SERVICE_KEY_SUBJECT_MATCH,
146         SERVICE_KEY_ALT_SUBJECT_MATCH,
147         SERVICE_KEY_DOMAIN_SUFF_MATCH,
148         SERVICE_KEY_DOMAIN_MATCH,
149         SERVICE_KEY_PHASE2,
150         SERVICE_KEY_PASSPHRASE,
151         SERVICE_KEY_SECURITY,
152         SERVICE_KEY_HIDDEN,
153         SERVICE_KEY_IPv4,
154         SERVICE_KEY_IPv6,
155         SERVICE_KEY_IPv6_PRIVACY,
156         SERVICE_KEY_MAC,
157         SERVICE_KEY_MDNS,
158         SERVICE_KEY_NAMESERVERS,
159         SERVICE_KEY_SEARCH_DOMAINS,
160         SERVICE_KEY_TIMESERVERS,
161         SERVICE_KEY_DOMAIN,
162         NULL,
163 };
164
165 static void unregister_config(gpointer data)
166 {
167         struct connman_config *config = data;
168
169         connman_info("Removing configuration %s", config->ident);
170
171         g_hash_table_destroy(config->service_table);
172
173         g_free(config->description);
174         g_free(config->name);
175         g_free(config->ident);
176         g_free(config);
177 }
178
179 static void unregister_service(gpointer data)
180 {
181         struct connman_config_service *config_service = data;
182         struct connman_service *service;
183         char *service_id;
184         GSList *list;
185
186         if (cleanup)
187                 goto free_only;
188
189         connman_info("Removing service configuration %s",
190                                                 config_service->ident);
191
192         if (config_service->virtual)
193                 goto free_only;
194
195         for (list = config_service->service_identifiers; list;
196                                                         list = list->next) {
197                 service_id = list->data;
198
199                 service = connman_service_lookup_from_identifier(service_id);
200                 if (service) {
201                         __connman_service_set_immutable(service, false);
202                         __connman_service_set_config(service, NULL, NULL);
203                         __connman_service_remove(service);
204
205                         /*
206                          * Ethernet or gadget service cannot be removed by
207                          * __connman_service_remove() so reset the ipconfig
208                          * here.
209                          */
210                         if (connman_service_get_type(service) ==
211                                                 CONNMAN_SERVICE_TYPE_ETHERNET ||
212                                         connman_service_get_type(service) ==
213                                                 CONNMAN_SERVICE_TYPE_GADGET) {
214                                 __connman_service_disconnect(service);
215                                 __connman_service_reset_ipconfig(service,
216                                         CONNMAN_IPCONFIG_TYPE_IPV4, NULL, NULL);
217                                 __connman_service_reset_ipconfig(service,
218                                         CONNMAN_IPCONFIG_TYPE_IPV6, NULL, NULL);
219                                 __connman_service_set_ignore(service, true);
220
221                                 /*
222                                  * After these operations, user needs to
223                                  * reconnect ethernet cable to get IP
224                                  * address.
225                                  */
226                         }
227                 }
228
229                 if (!__connman_storage_remove_service(service_id))
230                         DBG("Could not remove all files for service %s",
231                                                                 service_id);
232         }
233
234 free_only:
235         g_free(config_service->ident);
236         g_free(config_service->type);
237         g_free(config_service->name);
238         g_free(config_service->ssid);
239         g_free(config_service->eap);
240         g_free(config_service->identity);
241         g_free(config_service->anonymous_identity);
242         g_free(config_service->ca_cert_file);
243         g_free(config_service->subject_match);
244         g_free(config_service->altsubject_match);
245         g_free(config_service->domain_suffix_match);
246         g_free(config_service->domain_match);
247         g_free(config_service->client_cert_file);
248         g_free(config_service->private_key_file);
249         g_free(config_service->private_key_passphrase);
250         g_free(config_service->private_key_passphrase_type);
251         g_free(config_service->phase2);
252         g_free(config_service->passphrase);
253         g_free(config_service->ipv4_address);
254         g_free(config_service->ipv4_gateway);
255         g_free(config_service->ipv4_netmask);
256         g_free(config_service->ipv6_address);
257         g_free(config_service->ipv6_gateway);
258         g_free(config_service->ipv6_privacy);
259         g_free(config_service->mac);
260         g_strfreev(config_service->nameservers);
261         g_strfreev(config_service->search_domains);
262         g_strfreev(config_service->timeservers);
263         g_free(config_service->domain_name);
264         g_slist_free_full(config_service->service_identifiers, g_free);
265         g_free(config_service->config_ident);
266         g_free(config_service->config_entry);
267         g_free(config_service->virtual_file);
268         g_free(config_service);
269 }
270
271 static void check_keys(GKeyFile *keyfile, const char *group,
272                         const char **possible_keys)
273 {
274         char **avail_keys;
275         gsize nb_avail_keys, i, j;
276
277         avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
278         if (!avail_keys)
279                 return;
280
281         /*
282          * For each key in the configuration file,
283          * verify it is understood by connman
284          */
285         for (i = 0 ; i < nb_avail_keys; i++) {
286                 for (j = 0; possible_keys[j] ; j++)
287                         if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
288                                 break;
289
290                 if (!possible_keys[j])
291                         connman_warn("Unknown configuration key %s in [%s]",
292                                         avail_keys[i], group);
293         }
294
295         g_strfreev(avail_keys);
296 }
297
298 static int check_family(const char *address, int expected_family)
299 {
300         int family;
301         int err = 0;
302
303         family = connman_inet_check_ipaddress(address);
304         if (family < 0) {
305                 DBG("Cannot get address family of %s (%d/%s)", address,
306                         family, gai_strerror(family));
307                 err = -EINVAL;
308                 goto out;
309         }
310
311         switch (family) {
312         case AF_INET:
313                 if (expected_family != AF_INET) {
314                         DBG("Wrong type address %s, expecting IPv4", address);
315                         err = -EINVAL;
316                         goto out;
317                 }
318                 break;
319         case AF_INET6:
320                 if (expected_family != AF_INET6) {
321                         DBG("Wrong type address %s, expecting IPv6", address);
322                         err = -EINVAL;
323                         goto out;
324                 }
325                 break;
326         default:
327                 DBG("Unsupported address family %d", family);
328                 err = -EINVAL;
329                 goto out;
330         }
331
332 out:
333         return err;
334 }
335
336 static int parse_address(const char *address_str, int address_family,
337                         char **address, char **netmask, char **gateway)
338 {
339         char *addr_str, *mask_str, *gw_str;
340         int err = 0;
341         char **route;
342
343         route = g_strsplit(address_str, "/", 0);
344         if (!route)
345                 return -EINVAL;
346
347         addr_str = route[0];
348         if (!addr_str || addr_str[0] == '\0') {
349                 err = -EINVAL;
350                 goto out;
351         }
352
353         if ((err = check_family(addr_str, address_family)) < 0)
354                 goto out;
355
356         mask_str = route[1];
357         if (!mask_str || mask_str[0] == '\0') {
358                 err = -EINVAL;
359                 goto out;
360         }
361
362         gw_str = route[2];
363         if (gw_str && gw_str[0]) {
364                 if ((err = check_family(gw_str, address_family)) < 0)
365                         goto out;
366         }
367
368         g_free(*address);
369         *address = g_strdup(addr_str);
370
371         g_free(*netmask);
372         *netmask = g_strdup(mask_str);
373
374         g_free(*gateway);
375         *gateway = g_strdup(gw_str);
376
377         if (*gateway)
378                 DBG("address %s/%s via %s", *address, *netmask, *gateway);
379         else
380                 DBG("address %s/%s", *address, *netmask);
381
382 out:
383         g_strfreev(route);
384
385         return err;
386 }
387
388 static bool check_address(char *address_str, char **address)
389 {
390         if (g_ascii_strcasecmp(address_str, "auto") == 0 ||
391                         g_ascii_strcasecmp(address_str, "dhcp") == 0 ||
392                         g_ascii_strcasecmp(address_str, "off") == 0) {
393                 *address = address_str;
394                 return false;
395         }
396
397         return true;
398 }
399
400 static bool load_service_generic(GKeyFile *keyfile,
401                         const char *group, struct connman_config *config,
402                         struct connman_config_service *service)
403 {
404         char *str, *mask;
405         char **strlist;
406         gsize length;
407
408         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv4, NULL);
409         if (str && check_address(str, &service->ipv4_address)) {
410                 mask = NULL;
411
412                 if (parse_address(str, AF_INET, &service->ipv4_address,
413                                         &mask, &service->ipv4_gateway) < 0) {
414                         connman_warn("Invalid format for IPv4 address %s",
415                                                                         str);
416                         g_free(str);
417                         goto err;
418                 }
419
420                 if (!g_strrstr(mask, ".")) {
421                         /* We have netmask length */
422                         in_addr_t addr;
423                         struct in_addr netmask_in;
424                         unsigned char prefix_len = 32;
425                         char *ptr;
426                         long int value = strtol(mask, &ptr, 10);
427
428                         if (ptr != mask && *ptr == '\0' && value && value <= 32)
429                                 prefix_len = value;
430
431                         addr = 0xffffffff << (32 - prefix_len);
432                         netmask_in.s_addr = htonl(addr);
433                         service->ipv4_netmask =
434                                 g_strdup(inet_ntoa(netmask_in));
435
436                         g_free(mask);
437                 } else
438                         service->ipv4_netmask = mask;
439
440                 g_free(str);
441         }
442
443         str =  __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv6, NULL);
444         if (str && check_address(str, &service->ipv6_address)) {
445                 long int value;
446                 char *ptr;
447
448                 mask = NULL;
449
450                 if (parse_address(str, AF_INET6, &service->ipv6_address,
451                                         &mask, &service->ipv6_gateway) < 0) {
452                         connman_warn("Invalid format for IPv6 address %s",
453                                                                         str);
454                         g_free(str);
455                         goto err;
456                 }
457
458                 value = strtol(mask, &ptr, 10);
459                 if (ptr != mask && *ptr == '\0' && value <= 128)
460                         service->ipv6_prefix_length = value;
461                 else
462                         service->ipv6_prefix_length = 128;
463
464                 g_free(mask);
465                 g_free(str);
466         }
467
468         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv6_PRIVACY,
469                                                                         NULL);
470         if (str) {
471                 g_free(service->ipv6_privacy);
472                 service->ipv6_privacy = str;
473         }
474
475         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_MAC, NULL);
476         if (str) {
477                 g_free(service->mac);
478                 service->mac = str;
479         }
480
481         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_DOMAIN, NULL);
482         if (str) {
483                 g_free(service->domain_name);
484                 service->domain_name = str;
485         }
486
487         strlist = __connman_config_get_string_list(keyfile, group,
488                                         SERVICE_KEY_NAMESERVERS,
489                                         &length, NULL);
490         if (strlist) {
491                 if (length != 0) {
492                         g_strfreev(service->nameservers);
493                         service->nameservers = strlist;
494                 } else
495                         g_strfreev(strlist);
496         }
497
498         strlist = __connman_config_get_string_list(keyfile, group,
499                                         SERVICE_KEY_SEARCH_DOMAINS,
500                                         &length, NULL);
501         if (strlist) {
502                 if (length != 0) {
503                         g_strfreev(service->search_domains);
504                         service->search_domains = strlist;
505                 } else
506                         g_strfreev(strlist);
507         }
508
509         strlist = __connman_config_get_string_list(keyfile, group,
510                                         SERVICE_KEY_TIMESERVERS,
511                                         &length, NULL);
512         if (strlist) {
513                 if (length != 0) {
514                         g_strfreev(service->timeservers);
515                         service->timeservers = strlist;
516                 } else
517                         g_strfreev(strlist);
518         }
519
520         service->mdns = __connman_config_get_bool(keyfile, group,
521                                                 SERVICE_KEY_MDNS, NULL);
522
523         return true;
524
525 err:
526         g_free(service->ident);
527         g_free(service->type);
528         g_free(service->ipv4_address);
529         g_free(service->ipv4_netmask);
530         g_free(service->ipv4_gateway);
531         g_free(service->ipv6_address);
532         g_free(service->ipv6_gateway);
533         g_free(service->mac);
534         g_free(service);
535
536         return false;
537 }
538
539 static bool load_service(GKeyFile *keyfile, const char *group,
540                                                 struct connman_config *config)
541 {
542         struct connman_config_service *service;
543         const char *ident;
544         char *str, *hex_ssid;
545         enum connman_service_security security;
546         bool service_created = false;
547
548         /* Strip off "service_" prefix */
549         ident = group + 8;
550
551         if (strlen(ident) < 1)
552                 return false;
553
554         /* Verify that provided keys are good */
555         check_keys(keyfile, group, service_possible_keys);
556
557         service = g_hash_table_lookup(config->service_table, ident);
558         if (!service) {
559                 service = g_try_new0(struct connman_config_service, 1);
560                 if (!service)
561                         return false;
562
563                 service->ident = g_strdup(ident);
564
565                 service_created = true;
566         }
567
568         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
569         if (str) {
570                 g_free(service->type);
571                 service->type = str;
572         } else {
573                 connman_warn("Type of the configured service is missing "
574                         "for group %s", group);
575                 goto err;
576         }
577
578         if (!load_service_generic(keyfile, group, config, service))
579                 return false;
580
581         if (g_strcmp0(str, "ethernet") == 0) {
582                 service->config_ident = g_strdup(config->ident);
583                 service->config_entry = g_strdup_printf("service_%s",
584                                                         service->ident);
585
586                 g_hash_table_insert(config->service_table, service->ident,
587                                                                 service);
588                 return true;
589         }
590
591         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
592         if (str) {
593                 g_free(service->name);
594                 service->name = str;
595         }
596
597         hex_ssid = __connman_config_get_string(keyfile, group, SERVICE_KEY_SSID,
598                                          NULL);
599         if (hex_ssid) {
600                 char *ssid;
601                 unsigned int i, j = 0, hex;
602                 size_t hex_ssid_len = strlen(hex_ssid);
603
604                 ssid = g_try_malloc0(hex_ssid_len / 2);
605                 if (!ssid) {
606                         g_free(hex_ssid);
607                         goto err;
608                 }
609
610                 for (i = 0; i < hex_ssid_len; i += 2) {
611                         if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
612                                 connman_warn("Invalid SSID %s", hex_ssid);
613                                 g_free(ssid);
614                                 g_free(hex_ssid);
615                                 goto err;
616                         }
617                         ssid[j++] = hex;
618                 }
619
620                 g_free(hex_ssid);
621
622                 g_free(service->ssid);
623                 service->ssid = ssid;
624                 service->ssid_len = hex_ssid_len / 2;
625         } else if (service->name) {
626                 char *ssid;
627                 unsigned int ssid_len;
628
629                 ssid_len = strlen(service->name);
630                 ssid = g_try_malloc0(ssid_len);
631                 if (!ssid)
632                         goto err;
633
634                 memcpy(ssid, service->name, ssid_len);
635                 g_free(service->ssid);
636                 service->ssid = ssid;
637                 service->ssid_len = ssid_len;
638         }
639
640         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
641         if (str) {
642                 g_free(service->eap);
643                 service->eap = str;
644         }
645
646         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
647         if (str) {
648                 g_free(service->ca_cert_file);
649                 service->ca_cert_file = str;
650         }
651
652         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
653         if (str) {
654                 g_free(service->client_cert_file);
655                 service->client_cert_file = str;
656         }
657
658         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
659         if (str) {
660                 g_free(service->private_key_file);
661                 service->private_key_file = str;
662         }
663
664         str = __connman_config_get_string(keyfile, group,
665                                                 SERVICE_KEY_PRV_KEY_PASS, NULL);
666         if (str) {
667                 g_free(service->private_key_passphrase);
668                 service->private_key_passphrase = str;
669         }
670
671         str = __connman_config_get_string(keyfile, group,
672                                         SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
673         if (str) {
674                 g_free(service->private_key_passphrase_type);
675                 service->private_key_passphrase_type = str;
676         }
677
678         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
679         if (str) {
680                 g_free(service->identity);
681                 service->identity = str;
682         }
683
684         str = __connman_config_get_string(keyfile, group,
685                                         SERVICE_KEY_ANONYMOUS_IDENTITY, NULL);
686         if (str) {
687                 g_free(service->anonymous_identity);
688                 service->anonymous_identity = str;
689         }
690
691         str = __connman_config_get_string(keyfile, group,
692                                         SERVICE_KEY_SUBJECT_MATCH, NULL);
693         if (str) {
694                 g_free(service->subject_match);
695                 service->subject_match = str;
696         }
697
698         str = __connman_config_get_string(keyfile, group,
699                                         SERVICE_KEY_ALT_SUBJECT_MATCH, NULL);
700         if (str) {
701                 g_free(service->altsubject_match);
702                 service->altsubject_match = str;
703         }
704
705         str = __connman_config_get_string(keyfile, group,
706                                         SERVICE_KEY_DOMAIN_SUFF_MATCH, NULL);
707         if (str) {
708                 g_free(service->domain_suffix_match);
709                 service->domain_suffix_match = str;
710         }
711
712         str = __connman_config_get_string(keyfile, group,
713                                         SERVICE_KEY_DOMAIN_MATCH, NULL);
714         if (str) {
715                 g_free(service->domain_match);
716                 service->domain_match = str;
717         }
718
719         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
720         if (str) {
721                 g_free(service->phase2);
722                 service->phase2 = str;
723         }
724
725         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
726                                         NULL);
727         if (str) {
728                 g_free(service->passphrase);
729                 service->passphrase = str;
730         }
731
732         str = __connman_config_get_string(keyfile, group, SERVICE_KEY_SECURITY,
733                         NULL);
734         security = __connman_service_string2security(str);
735
736         if (service->eap) {
737
738                 if (str && security != CONNMAN_SERVICE_SECURITY_8021X)
739                         connman_info("Mismatch between EAP configuration and "
740                                         "setting %s = %s",
741                                         SERVICE_KEY_SECURITY, str);
742
743                 service->security = CONNMAN_SERVICE_SECURITY_8021X;
744
745         } else if (service->passphrase) {
746
747                 if (str) {
748                         if (security == CONNMAN_SERVICE_SECURITY_PSK ||
749                                         security == CONNMAN_SERVICE_SECURITY_WEP) {
750                                 service->security = security;
751                         } else {
752                                 connman_info("Mismatch with passphrase and "
753                                                 "setting %s = %s",
754                                                 SERVICE_KEY_SECURITY, str);
755
756                                 service->security =
757                                         CONNMAN_SERVICE_SECURITY_PSK;
758                         }
759
760                 } else
761                         service->security = CONNMAN_SERVICE_SECURITY_PSK;
762         } else if (str) {
763
764                 if (security != CONNMAN_SERVICE_SECURITY_NONE)
765                         connman_info("Mismatch no security and "
766                                         "setting %s = %s",
767                                         SERVICE_KEY_SECURITY, str);
768
769                 service->security = CONNMAN_SERVICE_SECURITY_NONE;
770         } else
771                 service->security = CONNMAN_SERVICE_SECURITY_NONE;
772
773         g_free(str);
774
775         service->config_ident = g_strdup(config->ident);
776         service->config_entry = g_strdup_printf("service_%s", service->ident);
777
778         service->hidden = __connman_config_get_bool(keyfile, group,
779                                                 SERVICE_KEY_HIDDEN, NULL);
780
781         if (service_created)
782                 g_hash_table_insert(config->service_table, service->ident,
783                                         service);
784
785         connman_info("Adding service configuration %s", service->ident);
786
787         return true;
788
789 err:
790         if (service_created) {
791                 g_free(service->ident);
792                 g_free(service->type);
793                 g_free(service->name);
794                 g_free(service->ssid);
795                 g_free(service);
796         }
797
798         return false;
799 }
800
801 static bool load_service_from_keyfile(GKeyFile *keyfile,
802                                                 struct connman_config *config)
803 {
804         bool found = false;
805         char **groups;
806         int i;
807
808         groups = g_key_file_get_groups(keyfile, NULL);
809
810         for (i = 0; groups[i]; i++) {
811                 if (!g_str_has_prefix(groups[i], "service_")) {
812                         connman_warn("Ignore group named '%s' because prefix "
813                                 "is not 'service_'", groups[i]);
814                         continue;
815                 }
816                 if (load_service(keyfile, groups[i], config))
817                         found = true;
818         }
819
820         g_strfreev(groups);
821
822         return found;
823 }
824
825 static int load_config(struct connman_config *config)
826 {
827         GKeyFile *keyfile;
828         char *str;
829
830         DBG("config %p", config);
831
832         keyfile = __connman_storage_load_config(config->ident);
833         if (!keyfile)
834                 return -EIO;
835
836         g_key_file_set_list_separator(keyfile, ',');
837
838         /* Verify keys validity of the global section */
839         check_keys(keyfile, "global", config_possible_keys);
840
841         str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
842         if (str) {
843                 g_free(config->name);
844                 config->name = str;
845         }
846
847         str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
848         if (str) {
849                 g_free(config->description);
850                 config->description = str;
851         }
852
853         if (!load_service_from_keyfile(keyfile, config))
854                 connman_warn("Config file %s/%s.config does not contain any "
855                         "configuration that can be provisioned!",
856                         STORAGEDIR, config->ident);
857
858         g_key_file_free(keyfile);
859
860         return 0;
861 }
862
863 static struct connman_config *create_config(const char *ident)
864 {
865         struct connman_config *config;
866
867         DBG("ident %s", ident);
868
869         if (g_hash_table_lookup(config_table, ident))
870                 return NULL;
871
872         config = g_try_new0(struct connman_config, 1);
873         if (!config)
874                 return NULL;
875
876         config->ident = g_strdup(ident);
877
878         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
879                                                 NULL, unregister_service);
880
881         g_hash_table_insert(config_table, config->ident, config);
882
883         connman_info("Adding configuration %s", config->ident);
884
885         return config;
886 }
887
888 static bool validate_ident(const char *ident)
889 {
890         unsigned int i;
891
892         if (!ident)
893                 return false;
894
895         for (i = 0; i < strlen(ident); i++)
896                 if (!g_ascii_isprint(ident[i]))
897                         return false;
898
899         return true;
900 }
901
902 static int read_configs(void)
903 {
904         GDir *dir;
905
906         DBG("");
907
908         dir = g_dir_open(STORAGEDIR, 0, NULL);
909         if (dir) {
910                 const gchar *file;
911
912                 while ((file = g_dir_read_name(dir))) {
913                         GString *str;
914                         gchar *ident;
915
916                         if (!g_str_has_suffix(file, ".config"))
917                                 continue;
918
919                         ident = g_strrstr(file, ".config");
920                         if (!ident)
921                                 continue;
922
923                         str = g_string_new_len(file, ident - file);
924                         if (!str)
925                                 continue;
926
927                         ident = g_string_free(str, FALSE);
928
929                         if (validate_ident(ident)) {
930                                 struct connman_config *config;
931
932                                 config = create_config(ident);
933                                 if (config)
934                                         load_config(config);
935                         } else {
936                                 connman_error("Invalid config ident %s", ident);
937                         }
938                         g_free(ident);
939                 }
940
941                 g_dir_close(dir);
942         }
943
944         return 0;
945 }
946
947 static void config_notify_handler(struct inotify_event *event,
948                                         const char *ident)
949 {
950         char *ext;
951
952         if (!ident)
953                 return;
954
955         if (!g_str_has_suffix(ident, ".config"))
956                 return;
957
958         ext = g_strrstr(ident, ".config");
959         if (!ext)
960                 return;
961
962         *ext = '\0';
963
964         if (!validate_ident(ident)) {
965                 connman_error("Invalid config ident %s", ident);
966                 return;
967         }
968
969         if (event->mask & (IN_CREATE | IN_MOVED_TO))
970                 create_config(ident);
971
972         if (event->mask & (IN_MODIFY | IN_MOVED_TO)) {
973                 struct connman_config *config;
974
975                 config = g_hash_table_lookup(config_table, ident);
976                 if (config) {
977                         int ret;
978
979                         g_hash_table_remove_all(config->service_table);
980                         load_config(config);
981                         ret = __connman_service_provision_changed(ident);
982                         if (ret > 0) {
983                                 /*
984                                  * Re-scan the config file for any
985                                  * changes
986                                  */
987                                 g_hash_table_remove_all(config->service_table);
988                                 load_config(config);
989                                 __connman_service_provision_changed(ident);
990                         }
991                 }
992         }
993
994         if (event->mask & (IN_DELETE | IN_MOVED_FROM))
995                 g_hash_table_remove(config_table, ident);
996 }
997
998 int __connman_config_init(void)
999 {
1000         DBG("");
1001
1002         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1003                                                 NULL, unregister_config);
1004
1005         connman_inotify_register(STORAGEDIR, config_notify_handler);
1006
1007         return read_configs();
1008 }
1009
1010 void __connman_config_cleanup(void)
1011 {
1012         DBG("");
1013
1014         cleanup = true;
1015
1016         connman_inotify_unregister(STORAGEDIR, config_notify_handler);
1017
1018         g_hash_table_destroy(config_table);
1019         config_table = NULL;
1020
1021         cleanup = false;
1022 }
1023
1024 char *__connman_config_get_string(GKeyFile *key_file,
1025         const char *group_name, const char *key, GError **error)
1026 {
1027         char *str = g_key_file_get_string(key_file, group_name, key, error);
1028         if (!str)
1029                 return NULL;
1030
1031         /* passphrases can have spaces in the end */
1032         if (!g_strcmp0(key, SERVICE_KEY_PASSPHRASE) ||
1033                         !g_strcmp0(key, SERVICE_KEY_PRV_KEY_PASS))
1034                 return str;
1035
1036         return g_strchomp(str);
1037 }
1038
1039 char **__connman_config_get_string_list(GKeyFile *key_file,
1040         const char *group_name, const char *key, gsize *length, GError **error)
1041 {
1042         char **p;
1043         char **strlist = g_key_file_get_string_list(key_file, group_name, key,
1044                 length, error);
1045         if (!strlist)
1046                 return NULL;
1047
1048         p = strlist;
1049         while (*p) {
1050                 *p = g_strstrip(*p);
1051                 p++;
1052         }
1053
1054         return strlist;
1055 }
1056
1057 bool __connman_config_get_bool(GKeyFile *key_file,
1058         const char *group_name, const char *key, GError **error)
1059 {
1060         char *valstr;
1061         bool val = false;
1062
1063         valstr = g_key_file_get_value(key_file, group_name, key, error);
1064         if (!valstr)
1065                 return false;
1066
1067         valstr = g_strchomp(valstr);
1068         if (strcmp(valstr, "true") == 0 || strcmp(valstr, "1") == 0)
1069                 val = true;
1070
1071         g_free(valstr);
1072
1073         return val;
1074 }
1075
1076 static char *config_pem_fsid(const char *pem_file)
1077 {
1078         struct statfs buf;
1079         unsigned *fsid = (unsigned *) &buf.f_fsid;
1080         unsigned long long fsid64;
1081
1082         if (!pem_file)
1083                 return NULL;
1084
1085         if (statfs(pem_file, &buf) < 0) {
1086                 connman_error("statfs error %s for %s",
1087                                                 strerror(errno), pem_file);
1088                 return NULL;
1089         }
1090
1091         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
1092
1093         return g_strdup_printf("%llx", fsid64);
1094 }
1095
1096 static void provision_service_wifi(struct connman_config_service *config,
1097                                 struct connman_service *service,
1098                                 struct connman_network *network,
1099                                 const void *ssid, unsigned int ssid_len)
1100 {
1101         if (config->eap)
1102                 __connman_service_set_string(service, "EAP", config->eap);
1103
1104         if (config->identity)
1105                 __connman_service_set_string(service, "Identity",
1106                                                         config->identity);
1107
1108         if (config->anonymous_identity)
1109                 __connman_service_set_string(service, "AnonymousIdentity",
1110                                                 config->anonymous_identity);
1111
1112         if (config->ca_cert_file)
1113                 __connman_service_set_string(service, "CACertFile",
1114                                                         config->ca_cert_file);
1115
1116         if (config->subject_match)
1117                 __connman_service_set_string(service, "SubjectMatch",
1118                                                         config->subject_match);
1119
1120         if (config->altsubject_match)
1121                 __connman_service_set_string(service, "AltSubjectMatch",
1122                                                         config->altsubject_match);
1123
1124         if (config->domain_suffix_match)
1125                 __connman_service_set_string(service, "DomainSuffixMatch",
1126                                                         config->domain_suffix_match);
1127
1128         if (config->domain_match)
1129                 __connman_service_set_string(service, "DomainMatch",
1130                                                         config->domain_match);
1131
1132         if (config->client_cert_file)
1133                 __connman_service_set_string(service, "ClientCertFile",
1134                                                 config->client_cert_file);
1135
1136         if (config->private_key_file)
1137                 __connman_service_set_string(service, "PrivateKeyFile",
1138                                                 config->private_key_file);
1139
1140         if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
1141                                         config->private_key_file) {
1142                 char *fsid;
1143
1144                 fsid = config_pem_fsid(config->private_key_file);
1145                 if (!fsid)
1146                         return;
1147
1148                 g_free(config->private_key_passphrase);
1149                 config->private_key_passphrase = fsid;
1150         }
1151
1152         if (config->private_key_passphrase) {
1153                 __connman_service_set_string(service, "PrivateKeyPassphrase",
1154                                                 config->private_key_passphrase);
1155                 /*
1156                  * TODO: Support for PEAP with both identity and key passwd.
1157                  * In that case, we should check if both of them are found
1158                  * from the config file. If not, we should not set the
1159                  * service passphrase in order for the UI to request for an
1160                  * additional passphrase.
1161                  */
1162         }
1163
1164         if (config->phase2)
1165                 __connman_service_set_string(service, "Phase2", config->phase2);
1166
1167         if (config->passphrase)
1168                 __connman_service_set_string(service, "Passphrase",
1169                                                 config->passphrase);
1170
1171         if (config->hidden)
1172                 __connman_service_set_hidden(service);
1173 }
1174
1175 struct connect_virtual {
1176         struct connman_service *service;
1177         const char *vfile;
1178 };
1179
1180 static gboolean remove_virtual_config(gpointer user_data)
1181 {
1182         struct connect_virtual *virtual = user_data;
1183
1184         __connman_service_connect(virtual->service,
1185                                 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1186         g_hash_table_remove(config_table, virtual->vfile);
1187
1188         g_free(virtual);
1189
1190         return FALSE;
1191 }
1192
1193 static int try_provision_service(struct connman_config_service *config,
1194                                 struct connman_service *service)
1195 {
1196         struct connman_network *network;
1197         const void *service_id;
1198         enum connman_service_type type;
1199         const void *ssid;
1200         unsigned int ssid_len;
1201         const char *str;
1202
1203         network = __connman_service_get_network(service);
1204         if (!network) {
1205                 connman_error("Service has no network set");
1206                 return -EINVAL;
1207         }
1208
1209         DBG("network %p ident %s", network,
1210                                 connman_network_get_identifier(network));
1211
1212         type = connman_service_get_type(service);
1213
1214         switch(type) {
1215         case CONNMAN_SERVICE_TYPE_WIFI:
1216                 if (__connman_service_string2type(config->type) != type)
1217                         return -ENOENT;
1218
1219                 ssid = connman_network_get_blob(network, "WiFi.SSID",
1220                                                 &ssid_len);
1221                 if (!ssid)
1222                         return -ENOENT;
1223
1224                 if (!config->ssid || ssid_len != config->ssid_len)
1225                         return -ENOENT;
1226
1227                 if (memcmp(config->ssid, ssid, ssid_len))
1228                         return -ENOENT;
1229
1230                 str = connman_network_get_string(network, "WiFi.Security");
1231                 if (config->security != __connman_service_string2security(str))
1232                         return -ENOENT;
1233
1234                 break;
1235
1236         case CONNMAN_SERVICE_TYPE_ETHERNET:
1237         case CONNMAN_SERVICE_TYPE_GADGET:
1238
1239                 if (__connman_service_string2type(config->type) != type)
1240                         return -ENOENT;
1241
1242                 break;
1243
1244         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1245         case CONNMAN_SERVICE_TYPE_SYSTEM:
1246         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1247         case CONNMAN_SERVICE_TYPE_CELLULAR:
1248         case CONNMAN_SERVICE_TYPE_GPS:
1249         case CONNMAN_SERVICE_TYPE_VPN:
1250         case CONNMAN_SERVICE_TYPE_P2P:
1251
1252                 return -ENOENT;
1253         }
1254
1255         DBG("service %p ident %s", service,
1256                                 connman_service_get_identifier(service));
1257
1258         if (config->mac) {
1259                 struct connman_device *device;
1260                 const char *device_addr;
1261
1262                 device = connman_network_get_device(network);
1263                 if (!device) {
1264                         connman_error("Network device is missing");
1265                         return -ENODEV;
1266                 }
1267
1268                 device_addr = connman_device_get_string(device, "Address");
1269
1270                 DBG("wants %s has %s", config->mac, device_addr);
1271
1272                 if (g_ascii_strcasecmp(device_addr, config->mac) != 0)
1273                         return -ENOENT;
1274         }
1275
1276         if (!config->ipv6_address) {
1277                 connman_network_set_ipv6_method(network,
1278                                                 CONNMAN_IPCONFIG_METHOD_AUTO);
1279         } else if (g_ascii_strcasecmp(config->ipv6_address, "off") == 0) {
1280                 connman_network_set_ipv6_method(network,
1281                                                 CONNMAN_IPCONFIG_METHOD_OFF);
1282         } else if (g_ascii_strcasecmp(config->ipv6_address, "auto") == 0 ||
1283                         g_ascii_strcasecmp(config->ipv6_address, "dhcp") == 0) {
1284                 connman_network_set_ipv6_method(network,
1285                                                 CONNMAN_IPCONFIG_METHOD_AUTO);
1286         } else {
1287                 struct connman_ipaddress *address;
1288
1289                 if (config->ipv6_prefix_length == 0) {
1290                         DBG("IPv6 prefix missing");
1291                         return -EINVAL;
1292                 }
1293
1294                 address = connman_ipaddress_alloc(AF_INET6);
1295                 if (!address)
1296                         return -ENOENT;
1297
1298                 connman_ipaddress_set_ipv6(address, config->ipv6_address,
1299                                         config->ipv6_prefix_length,
1300                                         config->ipv6_gateway);
1301
1302                 connman_network_set_ipv6_method(network,
1303                                                 CONNMAN_IPCONFIG_METHOD_FIXED);
1304
1305                 if (connman_network_set_ipaddress(network, address) < 0)
1306                         DBG("Unable to set IPv6 address to network %p",
1307                                                                 network);
1308
1309                 connman_ipaddress_free(address);
1310         }
1311
1312         if (config->ipv6_privacy) {
1313                 struct connman_ipconfig *ipconfig;
1314
1315                 ipconfig = __connman_service_get_ip6config(service);
1316                 if (ipconfig)
1317                         __connman_ipconfig_ipv6_set_privacy(ipconfig,
1318                                                         config->ipv6_privacy);
1319         }
1320
1321         if (!config->ipv4_address) {
1322                 connman_network_set_ipv4_method(network,
1323                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
1324         } else if (g_ascii_strcasecmp(config->ipv4_address, "off") == 0) {
1325                 connman_network_set_ipv4_method(network,
1326                                                 CONNMAN_IPCONFIG_METHOD_OFF);
1327         } else if (g_ascii_strcasecmp(config->ipv4_address, "auto") == 0 ||
1328                         g_ascii_strcasecmp(config->ipv4_address, "dhcp") == 0) {
1329                 connman_network_set_ipv4_method(network,
1330                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
1331         } else {
1332                 struct connman_ipaddress *address;
1333
1334                 if (!config->ipv4_netmask) {
1335                         DBG("IPv4 netmask missing");
1336                         return -EINVAL;
1337                 }
1338
1339                 address = connman_ipaddress_alloc(AF_INET);
1340                 if (!address)
1341                         return -ENOENT;
1342
1343                 connman_ipaddress_set_ipv4(address, config->ipv4_address,
1344                                         config->ipv4_netmask,
1345                                         config->ipv4_gateway);
1346
1347                 connman_network_set_ipv4_method(network,
1348                                                 CONNMAN_IPCONFIG_METHOD_FIXED);
1349
1350                 if (connman_network_set_ipaddress(network, address) < 0)
1351                         DBG("Unable to set IPv4 address to network %p",
1352                                                                 network);
1353
1354                 connman_ipaddress_free(address);
1355         }
1356
1357         __connman_service_disconnect(service);
1358
1359         service_id = connman_service_get_identifier(service);
1360         config->service_identifiers =
1361                 g_slist_prepend(config->service_identifiers,
1362                                 g_strdup(service_id));
1363
1364         __connman_service_set_favorite_delayed(service, true, true);
1365
1366         __connman_service_set_config(service, config->config_ident,
1367                                                 config->config_entry);
1368
1369         if (config->domain_name)
1370                 __connman_service_set_domainname(service, config->domain_name);
1371
1372         if (config->nameservers) {
1373                 int i;
1374
1375                 __connman_service_nameserver_clear(service);
1376
1377                 for (i = 0; config->nameservers[i]; i++) {
1378                         __connman_service_nameserver_append(service,
1379                                                 config->nameservers[i], false);
1380                 }
1381         }
1382
1383         if (config->search_domains)
1384                 __connman_service_set_search_domains(service,
1385                                                 config->search_domains);
1386
1387         __connman_service_set_mdns(service, config->mdns);
1388
1389         if (config->timeservers)
1390                 __connman_service_set_timeservers(service,
1391                                                 config->timeservers);
1392
1393         if (type == CONNMAN_SERVICE_TYPE_WIFI) {
1394                 provision_service_wifi(config, service, network,
1395                                                         ssid, ssid_len);
1396         }
1397
1398         __connman_service_mark_dirty();
1399
1400         __connman_service_load_modifiable(service);
1401
1402         if (config->virtual) {
1403                 struct connect_virtual *virtual;
1404
1405                 virtual = g_malloc0(sizeof(struct connect_virtual));
1406                 virtual->service = service;
1407                 virtual->vfile = config->virtual_file;
1408
1409                 g_idle_add(remove_virtual_config, virtual);
1410
1411                 return 0;
1412         }
1413
1414         __connman_service_set_immutable(service, true);
1415
1416         if (type == CONNMAN_SERVICE_TYPE_ETHERNET ||
1417                         type == CONNMAN_SERVICE_TYPE_GADGET) {
1418                 __connman_service_connect(service,
1419                                 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1420
1421                 return 0;
1422         }
1423
1424         __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1425
1426         return 0;
1427 }
1428
1429 static int
1430 find_and_provision_service_from_config(struct connman_service *service,
1431                                         struct connman_config *config)
1432 {
1433         GHashTableIter iter;
1434         gpointer value, key;
1435
1436         g_hash_table_iter_init(&iter, config->service_table);
1437         while (g_hash_table_iter_next(&iter, &key,
1438                                         &value)) {
1439                 if (!try_provision_service(value, service))
1440                         return 0;
1441         }
1442
1443         return -ENOENT;
1444 }
1445
1446 static int find_and_provision_service(struct connman_service *service)
1447 {
1448         GHashTableIter iter;
1449         gpointer value, key;
1450
1451         g_hash_table_iter_init(&iter, config_table);
1452
1453         while (g_hash_table_iter_next(&iter, &key, &value)) {
1454                 struct connman_config *config = value;
1455
1456                 if (!find_and_provision_service_from_config(service, config))
1457                         return 0;
1458         }
1459
1460         return -ENOENT;
1461 }
1462
1463 int __connman_config_provision_service(struct connman_service *service)
1464 {
1465         enum connman_service_type type;
1466
1467         /* For now only WiFi, Gadget and Ethernet services are supported */
1468         type = connman_service_get_type(service);
1469
1470         DBG("service %p type %d", service, type);
1471
1472         if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1473                         type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1474                         type != CONNMAN_SERVICE_TYPE_GADGET)
1475                 return -ENOSYS;
1476
1477         return find_and_provision_service(service);
1478 }
1479
1480 int __connman_config_provision_service_ident(struct connman_service *service,
1481                         const char *ident, const char *file, const char *entry)
1482 {
1483         enum connman_service_type type;
1484         struct connman_config *config;
1485         int ret = 0;
1486
1487         /* For now only WiFi, Gadget and Ethernet services are supported */
1488         type = connman_service_get_type(service);
1489
1490         DBG("service %p type %d", service, type);
1491
1492         if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1493                         type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1494                         type != CONNMAN_SERVICE_TYPE_GADGET)
1495                 return -ENOSYS;
1496
1497         config = g_hash_table_lookup(config_table, ident);
1498         if (config) {
1499                 GHashTableIter iter;
1500                 gpointer value, key;
1501                 bool found = false;
1502
1503                 g_hash_table_iter_init(&iter, config->service_table);
1504
1505                 /*
1506                  * Check if we need to remove individual service if it
1507                  * is missing from config file.
1508                  */
1509                 if (file && entry) {
1510                         while (g_hash_table_iter_next(&iter, &key,
1511                                                         &value)) {
1512                                 struct connman_config_service *config_service;
1513
1514                                 config_service = value;
1515
1516                                 if (g_strcmp0(config_service->config_ident,
1517                                                                 file) != 0)
1518                                         continue;
1519
1520                                 if (g_strcmp0(config_service->config_entry,
1521                                                                 entry) != 0)
1522                                         continue;
1523
1524                                 found = true;
1525                                 break;
1526                         }
1527
1528                         DBG("found %d ident %s file %s entry %s", found, ident,
1529                                                                 file, entry);
1530
1531                         if (!found) {
1532                                 /*
1533                                  * The entry+8 will skip "service_" prefix
1534                                  */
1535                                 g_hash_table_remove(config->service_table,
1536                                                 entry + 8);
1537                                 ret = 1;
1538                         }
1539                 }
1540
1541                 find_and_provision_service_from_config(service, config);
1542         }
1543
1544         return ret;
1545 }
1546
1547 static void generate_random_string(char *str, int length)
1548 {
1549         uint8_t val;
1550         int i;
1551         uint64_t rand;
1552
1553         memset(str, '\0', length);
1554
1555         for (i = 0; i < length-1; i++) {
1556                 do {
1557                         __connman_util_get_random(&rand);
1558                         val = (uint8_t)(rand % 122);
1559                         if (val < 48)
1560                                 val += 48;
1561                 } while((val > 57 && val < 65) || (val > 90 && val < 97));
1562
1563                 str[i] = val;
1564         }
1565 }
1566
1567 int connman_config_provision_mutable_service(GKeyFile *keyfile)
1568 {
1569         struct connman_config_service *service_config;
1570         struct connman_config *config;
1571         char *vfile, *group = NULL;
1572         char rstr[11];
1573
1574         DBG("");
1575
1576         generate_random_string(rstr, 11);
1577
1578         vfile = g_strdup_printf("service_mutable_%s.config", rstr);
1579
1580         config = create_config(vfile);
1581         if (!config)
1582                 return -ENOMEM;
1583
1584         if (!load_service_from_keyfile(keyfile, config))
1585                 goto error;
1586
1587         group = g_key_file_get_start_group(keyfile);
1588
1589         service_config = g_hash_table_lookup(config->service_table, group+8);
1590         if (!service_config)
1591                 goto error;
1592
1593         /* Specific to non file based config: */
1594         g_free(service_config->config_ident);
1595         service_config->config_ident = NULL;
1596         g_free(service_config->config_entry);
1597         service_config->config_entry = NULL;
1598
1599         service_config->virtual = true;
1600         service_config->virtual_file = vfile;
1601
1602         __connman_service_provision_changed(vfile);
1603
1604         if (g_strcmp0(service_config->type, "wifi") == 0)
1605                 __connman_device_request_scan(CONNMAN_SERVICE_TYPE_WIFI);
1606
1607         g_free(group);
1608         return 0;
1609
1610 error:
1611         DBG("Could not proceed");
1612         g_hash_table_remove(config_table, vfile);
1613         g_free(vfile);
1614         g_free(group);
1615         return -EINVAL;
1616 }
1617
1618 struct connman_config_entry **connman_config_get_entries(const char *type)
1619 {
1620         GHashTableIter iter_file, iter_config;
1621         gpointer value, key;
1622         struct connman_config_entry **entries = NULL;
1623         int i = 0, count;
1624
1625         g_hash_table_iter_init(&iter_file, config_table);
1626         while (g_hash_table_iter_next(&iter_file, &key, &value)) {
1627                 struct connman_config *config_file = value;
1628                 struct connman_config_entry **tmp_entries = entries;
1629
1630                 count = g_hash_table_size(config_file->service_table);
1631
1632                 entries = g_try_realloc(entries, (i + count + 1) *
1633                                         sizeof(struct connman_config_entry *));
1634                 if (!entries) {
1635                         g_free(tmp_entries);
1636                         return NULL;
1637                 }
1638
1639                 g_hash_table_iter_init(&iter_config,
1640                                                 config_file->service_table);
1641                 while (g_hash_table_iter_next(&iter_config, &key,
1642                                                         &value)) {
1643                         struct connman_config_service *config = value;
1644
1645                         if (type &&
1646                                         g_strcmp0(config->type, type) != 0)
1647                                 continue;
1648
1649                         entries[i] = g_try_new0(struct connman_config_entry,
1650                                                 1);
1651                         if (!entries[i])
1652                                 goto cleanup;
1653
1654                         entries[i]->ident = g_strdup(config->ident);
1655                         entries[i]->name = g_strdup(config->name);
1656                         entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
1657                         if (!entries[i]->ssid)
1658                                 goto cleanup;
1659
1660                         memcpy(entries[i]->ssid, config->ssid,
1661                                                         config->ssid_len);
1662                         entries[i]->ssid_len = config->ssid_len;
1663                         entries[i]->hidden = config->hidden;
1664
1665                         i++;
1666                 }
1667         }
1668
1669         if (entries) {
1670                 struct connman_config_entry **tmp_entries = entries;
1671
1672                 entries = g_try_realloc(entries, (i + 1) *
1673                                         sizeof(struct connman_config_entry *));
1674                 if (!entries) {
1675                         g_free(tmp_entries);
1676                         return NULL;
1677                 }
1678
1679                 entries[i] = NULL;
1680
1681                 DBG("%d provisioned AP found", i);
1682         }
1683
1684         return entries;
1685
1686 cleanup:
1687         connman_config_free_entries(entries);
1688         return NULL;
1689 }
1690
1691 void connman_config_free_entries(struct connman_config_entry **entries)
1692 {
1693         int i;
1694
1695         if (!entries)
1696                 return;
1697
1698         for (i = 0; entries[i]; i++) {
1699                 g_free(entries[i]->ident);
1700                 g_free(entries[i]->name);
1701                 g_free(entries[i]->ssid);
1702                 g_free(entries[i]);
1703         }
1704
1705         g_free(entries);
1706 }
1707
1708 bool __connman_config_address_provisioned(const char *address,
1709                                         const char *netmask)
1710 {
1711         GHashTableIter iter, siter;
1712         gpointer value, key, svalue, skey;
1713
1714         if (!address || !netmask)
1715                 return false;
1716
1717         g_hash_table_iter_init(&iter, config_table);
1718
1719         while (g_hash_table_iter_next(&iter, &key, &value)) {
1720                 struct connman_config *config = value;
1721
1722                 g_hash_table_iter_init(&siter, config->service_table);
1723                 while (g_hash_table_iter_next(&siter, &skey, &svalue)) {
1724                         struct connman_config_service *service = svalue;
1725
1726                         if (!g_strcmp0(address, service->ipv4_address) &&
1727                                         !g_strcmp0(netmask,
1728                                                 service->ipv4_netmask))
1729                                 return true;
1730                 }
1731         }
1732
1733         return false;
1734 }