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