Apply DNS configuration method
[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 #if defined TIZEN_EXT
689                             security == CONNMAN_SERVICE_SECURITY_RSN ||
690 #endif
691                                         security == CONNMAN_SERVICE_SECURITY_WEP) {
692                                 service->security = security;
693                         } else {
694                                 connman_info("Mismatch with passphrase and "
695                                                 "setting %s = %s",
696                                                 SERVICE_KEY_SECURITY, str);
697
698                                 service->security =
699                                         CONNMAN_SERVICE_SECURITY_PSK;
700                         }
701
702                 } else
703                         service->security = CONNMAN_SERVICE_SECURITY_PSK;
704         }
705
706         service->config_ident = g_strdup(config->ident);
707         service->config_entry = g_strdup_printf("service_%s", service->ident);
708
709         service->hidden = __connman_config_get_bool(keyfile, group,
710                                                 SERVICE_KEY_HIDDEN, NULL);
711
712         if (service_created)
713                 g_hash_table_insert(config->service_table, service->ident,
714                                         service);
715
716         connman_info("Adding service configuration %s", service->ident);
717
718         return true;
719
720 err:
721         if (service_created) {
722                 g_free(service->ident);
723                 g_free(service->type);
724                 g_free(service->name);
725                 g_free(service->ssid);
726                 g_free(service);
727         }
728
729         return false;
730 }
731
732 static bool load_service_from_keyfile(GKeyFile *keyfile,
733                                                 struct connman_config *config)
734 {
735         bool found = false;
736         char **groups;
737         int i;
738
739         groups = g_key_file_get_groups(keyfile, NULL);
740
741         for (i = 0; groups[i]; i++) {
742                 if (!g_str_has_prefix(groups[i], "service_"))
743                         continue;
744                 if (load_service(keyfile, groups[i], config))
745                         found = true;
746         }
747
748         g_strfreev(groups);
749
750         return found;
751 }
752
753 static int load_config(struct connman_config *config)
754 {
755         GKeyFile *keyfile;
756         char *str;
757
758         DBG("config %p", config);
759
760         keyfile = __connman_storage_load_config(config->ident);
761         if (!keyfile)
762                 return -EIO;
763
764         g_key_file_set_list_separator(keyfile, ',');
765
766         /* Verify keys validity of the global section */
767         check_keys(keyfile, "global", config_possible_keys);
768
769         str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
770         if (str) {
771                 g_free(config->name);
772                 config->name = str;
773         }
774
775         str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
776         if (str) {
777                 g_free(config->description);
778                 config->description = str;
779         }
780
781         if (!load_service_from_keyfile(keyfile, config))
782                 connman_warn("Config file %s/%s.config does not contain any "
783                         "configuration that can be provisioned!",
784                         STORAGEDIR, config->ident);
785
786         g_key_file_free(keyfile);
787
788         return 0;
789 }
790
791 static struct connman_config *create_config(const char *ident)
792 {
793         struct connman_config *config;
794
795         DBG("ident %s", ident);
796
797         if (g_hash_table_lookup(config_table, ident))
798                 return NULL;
799
800         config = g_try_new0(struct connman_config, 1);
801         if (!config)
802                 return NULL;
803
804         config->ident = g_strdup(ident);
805
806         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
807                                                 NULL, unregister_service);
808
809         g_hash_table_insert(config_table, config->ident, config);
810
811         connman_info("Adding configuration %s", config->ident);
812
813         return config;
814 }
815
816 static bool validate_ident(const char *ident)
817 {
818         unsigned int i;
819
820         if (!ident)
821                 return false;
822
823         for (i = 0; i < strlen(ident); i++)
824                 if (!g_ascii_isprint(ident[i]))
825                         return false;
826
827         return true;
828 }
829
830 static int read_configs(void)
831 {
832         GDir *dir;
833
834         DBG("");
835
836         dir = g_dir_open(STORAGEDIR, 0, NULL);
837         if (dir) {
838                 const gchar *file;
839
840                 while ((file = g_dir_read_name(dir))) {
841                         GString *str;
842                         gchar *ident;
843
844                         if (!g_str_has_suffix(file, ".config"))
845                                 continue;
846
847                         ident = g_strrstr(file, ".config");
848                         if (!ident)
849                                 continue;
850
851                         str = g_string_new_len(file, ident - file);
852                         if (!str)
853                                 continue;
854
855                         ident = g_string_free(str, FALSE);
856
857                         if (validate_ident(ident)) {
858                                 struct connman_config *config;
859
860                                 config = create_config(ident);
861                                 if (config)
862                                         load_config(config);
863                         } else {
864                                 connman_error("Invalid config ident %s", ident);
865                         }
866                         g_free(ident);
867                 }
868
869                 g_dir_close(dir);
870         }
871
872         return 0;
873 }
874
875 static void config_notify_handler(struct inotify_event *event,
876                                         const char *ident)
877 {
878         char *ext;
879
880         if (!ident)
881                 return;
882
883         if (!g_str_has_suffix(ident, ".config"))
884                 return;
885
886         ext = g_strrstr(ident, ".config");
887         if (!ext)
888                 return;
889
890         *ext = '\0';
891
892         if (!validate_ident(ident)) {
893                 connman_error("Invalid config ident %s", ident);
894                 return;
895         }
896
897         if (event->mask & IN_CREATE || event->mask & IN_MOVED_TO)
898                 create_config(ident);
899
900         if (event->mask & IN_MODIFY) {
901                 struct connman_config *config;
902
903                 config = g_hash_table_lookup(config_table, ident);
904                 if (config) {
905                         int ret;
906
907                         g_hash_table_remove_all(config->service_table);
908                         load_config(config);
909                         ret = __connman_service_provision_changed(ident);
910                         if (ret > 0) {
911                                 /*
912                                  * Re-scan the config file for any
913                                  * changes
914                                  */
915                                 g_hash_table_remove_all(config->service_table);
916                                 load_config(config);
917                                 __connman_service_provision_changed(ident);
918                         }
919                 }
920         }
921
922         if (event->mask & IN_DELETE)
923                 g_hash_table_remove(config_table, ident);
924 }
925
926 int __connman_config_init(void)
927 {
928         DBG("");
929
930         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
931                                                 NULL, unregister_config);
932
933         connman_inotify_register(STORAGEDIR, config_notify_handler);
934
935         return read_configs();
936 }
937
938 void __connman_config_cleanup(void)
939 {
940         DBG("");
941
942         cleanup = true;
943
944         connman_inotify_unregister(STORAGEDIR, config_notify_handler);
945
946         g_hash_table_destroy(config_table);
947         config_table = NULL;
948
949         cleanup = false;
950 }
951
952 char *__connman_config_get_string(GKeyFile *key_file,
953         const char *group_name, const char *key, GError **error)
954 {
955         char *str = g_key_file_get_string(key_file, group_name, key, error);
956         if (!str)
957                 return NULL;
958
959         return g_strchomp(str);
960 }
961
962 char **__connman_config_get_string_list(GKeyFile *key_file,
963         const char *group_name, const char *key, gsize *length, GError **error)
964 {
965         char **p;
966         char **strlist = g_key_file_get_string_list(key_file, group_name, key,
967                 length, error);
968         if (!strlist)
969                 return NULL;
970
971         p = strlist;
972         while (*p) {
973                 *p = g_strstrip(*p);
974                 p++;
975         }
976
977         return strlist;
978 }
979
980 bool __connman_config_get_bool(GKeyFile *key_file,
981         const char *group_name, const char *key, GError **error)
982 {
983         char *valstr;
984         bool val = false;
985
986         valstr = g_key_file_get_value(key_file, group_name, key, error);
987         if (!valstr)
988                 return false;
989
990         valstr = g_strchomp(valstr);
991         if (strcmp(valstr, "true") == 0 || strcmp(valstr, "1") == 0)
992                 val = true;
993
994         g_free(valstr);
995
996         return val;
997 }
998
999 static char *config_pem_fsid(const char *pem_file)
1000 {
1001         struct statfs buf;
1002         unsigned *fsid = (unsigned *) &buf.f_fsid;
1003         unsigned long long fsid64;
1004
1005         if (!pem_file)
1006                 return NULL;
1007
1008         if (statfs(pem_file, &buf) < 0) {
1009                 connman_error("statfs error %s for %s",
1010                                                 strerror(errno), pem_file);
1011                 return NULL;
1012         }
1013
1014         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
1015
1016         return g_strdup_printf("%llx", fsid64);
1017 }
1018
1019 static void provision_service_wifi(struct connman_config_service *config,
1020                                 struct connman_service *service,
1021                                 struct connman_network *network,
1022                                 const void *ssid, unsigned int ssid_len)
1023 {
1024         if (config->eap)
1025                 __connman_service_set_string(service, "EAP", config->eap);
1026
1027         if (config->identity)
1028                 __connman_service_set_string(service, "Identity",
1029                                                         config->identity);
1030
1031         if (config->ca_cert_file)
1032                 __connman_service_set_string(service, "CACertFile",
1033                                                         config->ca_cert_file);
1034
1035         if (config->client_cert_file)
1036                 __connman_service_set_string(service, "ClientCertFile",
1037                                                 config->client_cert_file);
1038
1039         if (config->private_key_file)
1040                 __connman_service_set_string(service, "PrivateKeyFile",
1041                                                 config->private_key_file);
1042
1043         if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
1044                                         config->private_key_file) {
1045                 char *fsid;
1046
1047                 fsid = config_pem_fsid(config->private_key_file);
1048                 if (!fsid)
1049                         return;
1050
1051                 g_free(config->private_key_passphrase);
1052                 config->private_key_passphrase = fsid;
1053         }
1054
1055         if (config->private_key_passphrase) {
1056                 __connman_service_set_string(service, "PrivateKeyPassphrase",
1057                                                 config->private_key_passphrase);
1058                 /*
1059                  * TODO: Support for PEAP with both identity and key passwd.
1060                  * In that case, we should check if both of them are found
1061                  * from the config file. If not, we should not set the
1062                  * service passphrase in order for the UI to request for an
1063                  * additional passphrase.
1064                  */
1065         }
1066
1067         if (config->phase2)
1068                 __connman_service_set_string(service, "Phase2", config->phase2);
1069 #if defined TIZEN_EXT
1070         else
1071                 __connman_service_set_string(service, "Phase2", NULL);
1072 #endif
1073
1074         if (config->passphrase)
1075                 __connman_service_set_string(service, "Passphrase",
1076                                                 config->passphrase);
1077
1078         if (config->hidden)
1079                 __connman_service_set_hidden(service);
1080 }
1081
1082 struct connect_virtual {
1083         struct connman_service *service;
1084         const char *vfile;
1085 };
1086
1087 static gboolean remove_virtual_config(gpointer user_data)
1088 {
1089         struct connect_virtual *virtual = user_data;
1090
1091         __connman_service_connect(virtual->service,
1092                                 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1093         g_hash_table_remove(config_table, virtual->vfile);
1094
1095         g_free(virtual);
1096
1097         return FALSE;
1098 }
1099
1100 #if defined TIZEN_EXT
1101 static bool __check_address_type(int address_family, const char *address)
1102 {
1103         unsigned char buf[sizeof(struct in6_addr)] = {0, };
1104         int err = 0;
1105
1106         err = inet_pton(address_family, address, buf);
1107         if(err > 0)
1108                 return TRUE;
1109
1110         return FALSE;
1111 }
1112 #endif
1113
1114 static int try_provision_service(struct connman_config_service *config,
1115                                 struct connman_service *service)
1116 {
1117         struct connman_network *network;
1118         const void *service_id;
1119         enum connman_service_type type;
1120         const void *ssid;
1121         unsigned int ssid_len;
1122
1123         network = __connman_service_get_network(service);
1124         if (!network) {
1125                 connman_error("Service has no network set");
1126                 return -EINVAL;
1127         }
1128
1129         DBG("network %p ident %s", network,
1130                                 connman_network_get_identifier(network));
1131
1132         type = connman_service_get_type(service);
1133
1134         switch(type) {
1135         case CONNMAN_SERVICE_TYPE_WIFI:
1136                 if (__connman_service_string2type(config->type) != type)
1137                         return -ENOENT;
1138
1139                 ssid = connman_network_get_blob(network, "WiFi.SSID",
1140                                                 &ssid_len);
1141                 if (!ssid) {
1142                         connman_error("Network SSID not set");
1143                         return -EINVAL;
1144                 }
1145
1146                 if (!config->ssid || ssid_len != config->ssid_len)
1147                         return -ENOENT;
1148
1149                 if (memcmp(config->ssid, ssid, ssid_len))
1150                         return -ENOENT;
1151
1152                 break;
1153
1154         case CONNMAN_SERVICE_TYPE_ETHERNET:
1155         case CONNMAN_SERVICE_TYPE_GADGET:
1156
1157                 if (__connman_service_string2type(config->type) != type)
1158                         return -ENOENT;
1159
1160                 break;
1161
1162         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1163         case CONNMAN_SERVICE_TYPE_SYSTEM:
1164         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1165         case CONNMAN_SERVICE_TYPE_CELLULAR:
1166         case CONNMAN_SERVICE_TYPE_GPS:
1167         case CONNMAN_SERVICE_TYPE_VPN:
1168         case CONNMAN_SERVICE_TYPE_P2P:
1169
1170                 return -ENOENT;
1171         }
1172
1173         DBG("service %p ident %s", service,
1174                                         __connman_service_get_ident(service));
1175
1176         if (config->mac) {
1177                 struct connman_device *device;
1178                 const char *device_addr;
1179
1180                 device = connman_network_get_device(network);
1181                 if (!device) {
1182                         connman_error("Network device is missing");
1183                         return -ENODEV;
1184                 }
1185
1186                 device_addr = connman_device_get_string(device, "Address");
1187
1188                 DBG("wants %s has %s", config->mac, device_addr);
1189
1190                 if (g_ascii_strcasecmp(device_addr, config->mac) != 0)
1191                         return -ENOENT;
1192         }
1193
1194         if (!config->ipv6_address) {
1195                 connman_network_set_ipv6_method(network,
1196                                                 CONNMAN_IPCONFIG_METHOD_AUTO);
1197         } else if (g_ascii_strcasecmp(config->ipv6_address, "off") == 0) {
1198                 connman_network_set_ipv6_method(network,
1199                                                 CONNMAN_IPCONFIG_METHOD_OFF);
1200         } else if (g_ascii_strcasecmp(config->ipv6_address, "auto") == 0 ||
1201                         g_ascii_strcasecmp(config->ipv6_address, "dhcp") == 0) {
1202                 connman_network_set_ipv6_method(network,
1203                                                 CONNMAN_IPCONFIG_METHOD_AUTO);
1204         } else {
1205                 struct connman_ipaddress *address;
1206
1207                 if (config->ipv6_prefix_length == 0) {
1208                         DBG("IPv6 prefix missing");
1209                         return -EINVAL;
1210                 }
1211
1212                 address = connman_ipaddress_alloc(AF_INET6);
1213                 if (!address)
1214                         return -ENOENT;
1215
1216                 connman_ipaddress_set_ipv6(address, config->ipv6_address,
1217                                         config->ipv6_prefix_length,
1218                                         config->ipv6_gateway);
1219
1220                 connman_network_set_ipv6_method(network,
1221                                                 CONNMAN_IPCONFIG_METHOD_FIXED);
1222
1223                 if (connman_network_set_ipaddress(network, address) < 0)
1224                         DBG("Unable to set IPv6 address to network %p",
1225                                                                 network);
1226
1227                 connman_ipaddress_free(address);
1228         }
1229
1230         if (config->ipv6_privacy) {
1231                 struct connman_ipconfig *ipconfig;
1232
1233                 ipconfig = __connman_service_get_ip6config(service);
1234                 if (ipconfig)
1235                         __connman_ipconfig_ipv6_set_privacy(ipconfig,
1236                                                         config->ipv6_privacy);
1237         }
1238
1239         if (!config->ipv4_address) {
1240                 connman_network_set_ipv4_method(network,
1241                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
1242         } else if (g_ascii_strcasecmp(config->ipv4_address, "off") == 0) {
1243                 connman_network_set_ipv4_method(network,
1244                                                 CONNMAN_IPCONFIG_METHOD_OFF);
1245         } else if (g_ascii_strcasecmp(config->ipv4_address, "auto") == 0 ||
1246                         g_ascii_strcasecmp(config->ipv4_address, "dhcp") == 0) {
1247                 connman_network_set_ipv4_method(network,
1248                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
1249         } else {
1250                 struct connman_ipaddress *address;
1251
1252                 if (!config->ipv4_netmask) {
1253                         DBG("IPv4 netmask missing");
1254                         return -EINVAL;
1255                 }
1256
1257                 address = connman_ipaddress_alloc(AF_INET);
1258                 if (!address)
1259                         return -ENOENT;
1260
1261                 connman_ipaddress_set_ipv4(address, config->ipv4_address,
1262                                         config->ipv4_netmask,
1263                                         config->ipv4_gateway);
1264
1265                 connman_network_set_ipv4_method(network,
1266                                                 CONNMAN_IPCONFIG_METHOD_FIXED);
1267
1268                 if (connman_network_set_ipaddress(network, address) < 0)
1269                         DBG("Unable to set IPv4 address to network %p",
1270                                                                 network);
1271
1272                 connman_ipaddress_free(address);
1273         }
1274
1275         __connman_service_disconnect(service);
1276
1277         service_id = __connman_service_get_ident(service);
1278         config->service_identifiers =
1279                 g_slist_prepend(config->service_identifiers,
1280                                 g_strdup(service_id));
1281
1282         __connman_service_set_favorite_delayed(service, true, true);
1283
1284         __connman_service_set_config(service, config->config_ident,
1285                                                 config->config_entry);
1286
1287         if (config->domain_name)
1288                 __connman_service_set_domainname(service, config->domain_name);
1289
1290         if (config->nameservers) {
1291                 int i;
1292
1293                 __connman_service_nameserver_clear(service);
1294
1295                 for (i = 0; config->nameservers[i]; i++) {
1296 #if defined TIZEN_EXT
1297                         if (__check_address_type(AF_INET, config->nameservers[i]))
1298                                 __connman_service_nameserver_append(service,
1299                                                 config->nameservers[i], false,
1300                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
1301                         else if (__check_address_type(AF_INET6, config->nameservers[i]))
1302                                 __connman_service_nameserver_append(service,
1303                                                 config->nameservers[i], false,
1304                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
1305 #else
1306                         __connman_service_nameserver_append(service,
1307                                                 config->nameservers[i], false);
1308 #endif
1309                 }
1310         }
1311
1312         if (config->search_domains)
1313                 __connman_service_set_search_domains(service,
1314                                                 config->search_domains);
1315
1316         if (config->timeservers)
1317                 __connman_service_set_timeservers(service,
1318                                                 config->timeservers);
1319
1320         if (type == CONNMAN_SERVICE_TYPE_WIFI) {
1321                 provision_service_wifi(config, service, network,
1322                                                         ssid, ssid_len);
1323         }
1324
1325         __connman_service_mark_dirty();
1326
1327         __connman_service_load_modifiable(service);
1328
1329         if (config->virtual) {
1330                 struct connect_virtual *virtual;
1331
1332                 virtual = g_malloc0(sizeof(struct connect_virtual));
1333                 virtual->service = service;
1334                 virtual->vfile = config->virtual_file;
1335
1336                 g_timeout_add(0, remove_virtual_config, virtual);
1337
1338                 return 0;
1339         }
1340
1341         __connman_service_set_immutable(service, true);
1342
1343         if (type == CONNMAN_SERVICE_TYPE_ETHERNET ||
1344                         type == CONNMAN_SERVICE_TYPE_GADGET) {
1345                 __connman_service_connect(service,
1346                                 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1347
1348                 return 0;
1349         }
1350
1351         __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1352
1353         return 0;
1354 }
1355
1356 static int find_and_provision_service(struct connman_service *service)
1357 {
1358         GHashTableIter iter, iter_service;
1359         gpointer value, key, value_service, key_service;
1360
1361         g_hash_table_iter_init(&iter, config_table);
1362
1363         while (g_hash_table_iter_next(&iter, &key, &value)) {
1364                 struct connman_config *config = value;
1365
1366                 g_hash_table_iter_init(&iter_service, config->service_table);
1367                 while (g_hash_table_iter_next(&iter_service, &key_service,
1368                                                 &value_service)) {
1369                         if (!try_provision_service(value_service, service))
1370                                 return 0;
1371                 }
1372         }
1373
1374         return -ENOENT;
1375 }
1376
1377 int __connman_config_provision_service(struct connman_service *service)
1378 {
1379         enum connman_service_type type;
1380
1381         /* For now only WiFi, Gadget and Ethernet services are supported */
1382         type = connman_service_get_type(service);
1383
1384         DBG("service %p type %d", service, type);
1385
1386         if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1387                         type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1388                         type != CONNMAN_SERVICE_TYPE_GADGET)
1389                 return -ENOSYS;
1390
1391 #if defined TIZEN_EXT
1392         if(type == CONNMAN_SERVICE_TYPE_WIFI &&
1393                         __connman_service_get_security(service) ==
1394                         CONNMAN_SERVICE_SECURITY_NONE)
1395                 return -ENOSYS;
1396 #endif
1397
1398         return find_and_provision_service(service);
1399 }
1400
1401 int __connman_config_provision_service_ident(struct connman_service *service,
1402                         const char *ident, const char *file, const char *entry)
1403 {
1404         enum connman_service_type type;
1405         struct connman_config *config;
1406         int ret = 0;
1407
1408         /* For now only WiFi, Gadget and Ethernet services are supported */
1409         type = connman_service_get_type(service);
1410
1411         DBG("service %p type %d", service, type);
1412
1413         if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1414                         type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1415                         type != CONNMAN_SERVICE_TYPE_GADGET)
1416                 return -ENOSYS;
1417
1418         config = g_hash_table_lookup(config_table, ident);
1419         if (config) {
1420                 GHashTableIter iter;
1421                 gpointer value, key;
1422                 bool found = false;
1423
1424                 g_hash_table_iter_init(&iter, config->service_table);
1425
1426                 /*
1427                  * Check if we need to remove individual service if it
1428                  * is missing from config file.
1429                  */
1430                 if (file && entry) {
1431                         while (g_hash_table_iter_next(&iter, &key,
1432                                                         &value)) {
1433                                 struct connman_config_service *config_service;
1434
1435                                 config_service = value;
1436
1437                                 if (g_strcmp0(config_service->config_ident,
1438                                                                 file) != 0)
1439                                         continue;
1440
1441                                 if (g_strcmp0(config_service->config_entry,
1442                                                                 entry) != 0)
1443                                         continue;
1444
1445                                 found = true;
1446                                 break;
1447                         }
1448
1449                         DBG("found %d ident %s file %s entry %s", found, ident,
1450                                                                 file, entry);
1451
1452                         if (!found) {
1453                                 /*
1454                                  * The entry+8 will skip "service_" prefix
1455                                  */
1456                                 g_hash_table_remove(config->service_table,
1457                                                 entry + 8);
1458                                 ret = 1;
1459                         }
1460                 }
1461
1462                 find_and_provision_service(service);
1463         }
1464
1465         return ret;
1466 }
1467
1468 static void generate_random_string(char *str, int length)
1469 {
1470         uint8_t val;
1471         int i;
1472         uint64_t rand;
1473
1474         memset(str, '\0', length);
1475
1476         for (i = 0; i < length-1; i++) {
1477                 do {
1478                         __connman_util_get_random(&rand);
1479                         val = (uint8_t)(rand % 122);
1480                         if (val < 48)
1481                                 val += 48;
1482                 } while((val > 57 && val < 65) || (val > 90 && val < 97));
1483
1484                 str[i] = val;
1485         }
1486 }
1487
1488 int connman_config_provision_mutable_service(GKeyFile *keyfile)
1489 {
1490         struct connman_config_service *service_config;
1491         struct connman_config *config;
1492         char *vfile, *group;
1493         char rstr[11];
1494
1495         DBG("");
1496
1497         generate_random_string(rstr, 11);
1498
1499         vfile = g_strdup_printf("service_mutable_%s.config", rstr);
1500
1501         config = create_config(vfile);
1502         if (!config)
1503                 return -ENOMEM;
1504
1505         if (!load_service_from_keyfile(keyfile, config))
1506                 goto error;
1507
1508         group = g_key_file_get_start_group(keyfile);
1509
1510         service_config = g_hash_table_lookup(config->service_table, group+8);
1511         if (!service_config)
1512                 goto error;
1513
1514         /* Specific to non file based config: */
1515         g_free(service_config->config_ident);
1516         service_config->config_ident = NULL;
1517         g_free(service_config->config_entry);
1518         service_config->config_entry = NULL;
1519
1520         service_config->virtual = true;
1521         service_config->virtual_file = vfile;
1522
1523         __connman_service_provision_changed(vfile);
1524
1525         if (g_strcmp0(service_config->type, "wifi") == 0)
1526                 __connman_device_request_scan(CONNMAN_SERVICE_TYPE_WIFI);
1527
1528         return 0;
1529
1530 error:
1531         DBG("Could not proceed");
1532         g_hash_table_remove(config_table, vfile);
1533         g_free(vfile);
1534
1535         return -EINVAL;
1536 }
1537
1538 struct connman_config_entry **connman_config_get_entries(const char *type)
1539 {
1540         GHashTableIter iter_file, iter_config;
1541         gpointer value, key;
1542         struct connman_config_entry **entries = NULL;
1543         int i = 0, count;
1544
1545         g_hash_table_iter_init(&iter_file, config_table);
1546         while (g_hash_table_iter_next(&iter_file, &key, &value)) {
1547                 struct connman_config *config_file = value;
1548
1549                 count = g_hash_table_size(config_file->service_table);
1550
1551                 entries = g_try_realloc(entries, (i + count + 1) *
1552                                         sizeof(struct connman_config_entry *));
1553                 if (!entries)
1554                         return NULL;
1555
1556                 g_hash_table_iter_init(&iter_config,
1557                                                 config_file->service_table);
1558                 while (g_hash_table_iter_next(&iter_config, &key,
1559                                                         &value)) {
1560                         struct connman_config_service *config = value;
1561
1562                         if (type &&
1563                                         g_strcmp0(config->type, type) != 0)
1564                                 continue;
1565
1566                         entries[i] = g_try_new0(struct connman_config_entry,
1567                                                 1);
1568                         if (!entries[i])
1569                                 goto cleanup;
1570
1571                         entries[i]->ident = g_strdup(config->ident);
1572                         entries[i]->name = g_strdup(config->name);
1573                         entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
1574                         if (!entries[i]->ssid)
1575                                 goto cleanup;
1576
1577                         memcpy(entries[i]->ssid, config->ssid,
1578                                                         config->ssid_len);
1579                         entries[i]->ssid_len = config->ssid_len;
1580                         entries[i]->hidden = config->hidden;
1581
1582                         i++;
1583                 }
1584         }
1585
1586         if (entries) {
1587                 entries = g_try_realloc(entries, (i + 1) *
1588                                         sizeof(struct connman_config_entry *));
1589                 if (!entries)
1590                         return NULL;
1591
1592                 entries[i] = NULL;
1593
1594                 DBG("%d provisioned AP found", i);
1595         }
1596
1597         return entries;
1598
1599 cleanup:
1600         connman_config_free_entries(entries);
1601         return NULL;
1602 }
1603
1604 void connman_config_free_entries(struct connman_config_entry **entries)
1605 {
1606         int i;
1607
1608         if (!entries)
1609                 return;
1610
1611         for (i = 0; entries[i]; i++) {
1612                 g_free(entries[i]->ident);
1613                 g_free(entries[i]->name);
1614                 g_free(entries[i]->ssid);
1615                 g_free(entries[i]);
1616         }
1617
1618         g_free(entries);
1619         return;
1620 }
1621
1622 bool __connman_config_address_provisioned(const char *address,
1623                                         const char *netmask)
1624 {
1625         GHashTableIter iter, siter;
1626         gpointer value, key, svalue, skey;
1627
1628         if (!address || !netmask)
1629                 return false;
1630
1631         g_hash_table_iter_init(&iter, config_table);
1632
1633         while (g_hash_table_iter_next(&iter, &key, &value)) {
1634                 struct connman_config *config = value;
1635
1636                 g_hash_table_iter_init(&siter, config->service_table);
1637                 while (g_hash_table_iter_next(&siter, &skey, &svalue)) {
1638                         struct connman_config_service *service = svalue;
1639
1640                         if (!g_strcmp0(address, service->ipv4_address) &&
1641                                         !g_strcmp0(netmask,
1642                                                 service->ipv4_netmask))
1643                                 return true;
1644                 }
1645         }
1646
1647         return false;
1648 }