config: Read only wifi config
[platform/upstream/connman.git] / src / config.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <sys/vfs.h>
31 #include <sys/inotify.h>
32 #include <glib.h>
33
34 #include <connman/provision.h>
35 #include "connman.h"
36
37 struct connman_config_service {
38         char *ident;
39         char *name;
40         char *type;
41         void *ssid;
42         unsigned int ssid_len;
43         char *eap;
44         char *identity;
45         char *ca_cert_file;
46         char *client_cert_file;
47         char *private_key_file;
48         char *private_key_passphrase;
49         char *private_key_passphrase_type;
50         char *phase2;
51         char *passphrase;
52         GSList *service_identifiers;
53         char *config_ident; /* file prefix */
54         char *config_entry; /* entry name */
55         connman_bool_t hidden;
56 };
57
58 struct connman_config {
59         char *ident;
60         char *name;
61         char *description;
62         connman_bool_t protected;
63         GHashTable *service_table;
64 };
65
66 static GHashTable *config_table = NULL;
67 static GSList *protected_services = NULL;
68
69 static connman_bool_t cleanup = FALSE;
70
71 /* Definition of possible strings in the .config files */
72 #define CONFIG_KEY_NAME                "Name"
73 #define CONFIG_KEY_DESC                "Description"
74 #define CONFIG_KEY_PROT                "Protected"
75
76 #define SERVICE_KEY_TYPE               "Type"
77 #define SERVICE_KEY_NAME               "Name"
78 #define SERVICE_KEY_SSID               "SSID"
79 #define SERVICE_KEY_EAP                "EAP"
80 #define SERVICE_KEY_CA_CERT            "CACertFile"
81 #define SERVICE_KEY_CL_CERT            "ClientCertFile"
82 #define SERVICE_KEY_PRV_KEY            "PrivateKeyFile"
83 #define SERVICE_KEY_PRV_KEY_PASS       "PrivateKeyPassphrase"
84 #define SERVICE_KEY_PRV_KEY_PASS_TYPE  "PrivateKeyPassphraseType"
85 #define SERVICE_KEY_IDENTITY           "Identity"
86 #define SERVICE_KEY_PHASE2             "Phase2"
87 #define SERVICE_KEY_PASSPHRASE         "Passphrase"
88 #define SERVICE_KEY_HIDDEN             "Hidden"
89
90 static const char *config_possible_keys[] = {
91         CONFIG_KEY_NAME,
92         CONFIG_KEY_DESC,
93         CONFIG_KEY_PROT,
94         NULL,
95 };
96
97 static const char *service_possible_keys[] = {
98         SERVICE_KEY_TYPE,
99         SERVICE_KEY_NAME,
100         SERVICE_KEY_SSID,
101         SERVICE_KEY_EAP,
102         SERVICE_KEY_CA_CERT,
103         SERVICE_KEY_CL_CERT,
104         SERVICE_KEY_PRV_KEY,
105         SERVICE_KEY_PRV_KEY_PASS,
106         SERVICE_KEY_PRV_KEY_PASS_TYPE,
107         SERVICE_KEY_IDENTITY,
108         SERVICE_KEY_PHASE2,
109         SERVICE_KEY_PASSPHRASE,
110         SERVICE_KEY_HIDDEN,
111         NULL,
112 };
113
114 static void unregister_config(gpointer data)
115 {
116         struct connman_config *config = data;
117
118         connman_info("Removing configuration %s", config->ident);
119
120         g_hash_table_destroy(config->service_table);
121
122         g_free(config->description);
123         g_free(config->name);
124         g_free(config->ident);
125         g_free(config);
126 }
127
128 static void unregister_service(gpointer data)
129 {
130         struct connman_config_service *config_service = data;
131         struct connman_service *service;
132         char *service_id;
133         GSList *list;
134
135         if (cleanup == TRUE)
136                 goto free_only;
137
138         connman_info("Removing service configuration %s",
139                                                 config_service->ident);
140
141         protected_services = g_slist_remove(protected_services,
142                                                 config_service);
143
144         for (list = config_service->service_identifiers; list != NULL;
145                                                         list = list->next) {
146                 service_id = list->data;
147
148                 service = __connman_service_lookup_from_ident(service_id);
149                 if (service != NULL) {
150                         __connman_service_set_immutable(service, FALSE);
151                         __connman_service_remove(service);
152                 }
153
154                 if (__connman_storage_remove_service(service_id) == FALSE)
155                         DBG("Could not remove all files for service %s",
156                                                                 service_id);
157         }
158
159 free_only:
160         g_free(config_service->ident);
161         g_free(config_service->type);
162         g_free(config_service->name);
163         g_free(config_service->ssid);
164         g_free(config_service->eap);
165         g_free(config_service->identity);
166         g_free(config_service->ca_cert_file);
167         g_free(config_service->client_cert_file);
168         g_free(config_service->private_key_file);
169         g_free(config_service->private_key_passphrase);
170         g_free(config_service->private_key_passphrase_type);
171         g_free(config_service->phase2);
172         g_free(config_service->passphrase);
173         g_slist_free_full(config_service->service_identifiers, g_free);
174         g_free(config_service->config_ident);
175         g_free(config_service->config_entry);
176         g_free(config_service);
177 }
178
179 static void check_keys(GKeyFile *keyfile, const char *group,
180                         const char **possible_keys)
181 {
182         char **avail_keys;
183         gsize nb_avail_keys, i, j;
184
185         avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
186         if (avail_keys == NULL)
187                 return;
188
189         /*
190          * For each key in the configuration file,
191          * verify it is understood by connman
192          */
193         for (i = 0 ; i < nb_avail_keys; i++) {
194                 for (j = 0; possible_keys[j] ; j++)
195                         if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
196                                 break;
197
198                 if (possible_keys[j] == NULL)
199                         connman_warn("Unknown configuration key %s in [%s]",
200                                         avail_keys[i], group);
201         }
202
203         g_strfreev(avail_keys);
204 }
205
206 static connman_bool_t
207 is_protected_service(struct connman_config_service *service)
208 {
209         GSList *list;
210
211         DBG("ident %s", service->ident);
212
213         for (list = protected_services; list; list = list->next) {
214                 struct connman_config_service *s = list->data;
215
216                 if (g_strcmp0(s->type, service->type) != 0)
217                         continue;
218
219                 if (s->ssid == NULL || service->ssid == NULL)
220                         continue;
221
222                 if (s->ssid_len != service->ssid_len)
223                         continue;
224
225                 if (g_strcmp0(service->type, "wifi") == 0 &&
226                         strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
227                         return TRUE;
228                 }
229         }
230
231         return FALSE;
232 }
233
234 static int load_service(GKeyFile *keyfile, const char *group,
235                                                 struct connman_config *config)
236 {
237         struct connman_config_service *service;
238         const char *ident;
239         char *str, *hex_ssid;
240         gboolean service_created = FALSE;
241         int err;
242
243         /* Strip off "service_" prefix */
244         ident = group + 8;
245
246         if (strlen(ident) < 1)
247                 return -EINVAL;
248
249         /* Verify that provided keys are good */
250         check_keys(keyfile, group, service_possible_keys);
251
252         service = g_hash_table_lookup(config->service_table, ident);
253         if (service == NULL) {
254                 service = g_try_new0(struct connman_config_service, 1);
255                 if (service == NULL)
256                         return -ENOMEM;
257
258                 service->ident = g_strdup(ident);
259
260                 service_created = TRUE;
261         }
262
263         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
264         if (str != NULL) {
265                 g_free(service->type);
266                 service->type = str;
267         }
268
269         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
270         if (str != NULL) {
271                 g_free(service->name);
272                 service->name = str;
273         }
274
275         hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
276                                          NULL);
277         if (hex_ssid != NULL) {
278                 char *ssid;
279                 unsigned int i, j = 0, hex;
280                 size_t hex_ssid_len = strlen(hex_ssid);
281
282                 ssid = g_try_malloc0(hex_ssid_len / 2);
283                 if (ssid == NULL) {
284                         err = -ENOMEM;
285                         g_free(hex_ssid);
286                         goto err;
287                 }
288
289                 for (i = 0; i < hex_ssid_len; i += 2) {
290                         if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
291                                 connman_warn("Invalid SSID %s", hex_ssid);
292                                 g_free(ssid);
293                                 g_free(hex_ssid);
294                                 err = -EILSEQ;
295                                 goto err;
296                         }
297                         ssid[j++] = hex;
298                 }
299
300                 g_free(hex_ssid);
301
302                 g_free(service->ssid);
303                 service->ssid = ssid;
304                 service->ssid_len = hex_ssid_len / 2;
305         } else if (service->name != NULL) {
306                 char *ssid;
307                 unsigned int ssid_len;
308
309                 ssid_len = strlen(service->name);
310                 ssid = g_try_malloc0(ssid_len);
311                 if (ssid == NULL) {
312                         err = -ENOMEM;
313                         goto err;
314                 }
315
316                 memcpy(ssid, service->name, ssid_len);
317                 g_free(service->ssid);
318                 service->ssid = ssid;
319                 service->ssid_len = ssid_len;
320         }
321
322         if (is_protected_service(service) == TRUE) {
323                 connman_error("Trying to provision a protected service");
324                 err = -EACCES;
325                 goto err;
326         }
327
328         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
329         if (str != NULL) {
330                 g_free(service->eap);
331                 service->eap = str;
332         }
333
334         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
335         if (str != NULL) {
336                 g_free(service->ca_cert_file);
337                 service->ca_cert_file = str;
338         }
339
340         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
341         if (str != NULL) {
342                 g_free(service->client_cert_file);
343                 service->client_cert_file = str;
344         }
345
346         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
347         if (str != NULL) {
348                 g_free(service->private_key_file);
349                 service->private_key_file = str;
350         }
351
352         str = g_key_file_get_string(keyfile, group,
353                                                 SERVICE_KEY_PRV_KEY_PASS, NULL);
354         if (str != NULL) {
355                 g_free(service->private_key_passphrase);
356                 service->private_key_passphrase = str;
357         }
358
359         str = g_key_file_get_string(keyfile, group,
360                                         SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
361         if (str != NULL) {
362                 g_free(service->private_key_passphrase_type);
363                 service->private_key_passphrase_type = str;
364         }
365
366         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
367         if (str != NULL) {
368                 g_free(service->identity);
369                 service->identity = str;
370         }
371
372         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
373         if (str != NULL) {
374                 g_free(service->phase2);
375                 service->phase2 = str;
376         }
377
378         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
379                                         NULL);
380         if (str != NULL) {
381                 g_free(service->passphrase);
382                 service->passphrase = str;
383         }
384
385         service->config_ident = g_strdup(config->ident);
386         service->config_entry = g_strdup_printf("service_%s", service->ident);
387
388         service->hidden = g_key_file_get_boolean(keyfile, group,
389                                                 SERVICE_KEY_HIDDEN, NULL);
390
391         if (service_created)
392                 g_hash_table_insert(config->service_table, service->ident,
393                                         service);
394
395         if (config->protected == TRUE)
396                 protected_services =
397                         g_slist_prepend(protected_services, service);
398
399         connman_info("Adding service configuration %s", service->ident);
400
401         return 0;
402
403 err:
404         if (service_created == TRUE) {
405                 g_free(service->ident);
406                 g_free(service->type);
407                 g_free(service->name);
408                 g_free(service->ssid);
409                 g_free(service);
410         }
411
412         return err;
413 }
414
415 static int load_config(struct connman_config *config)
416 {
417         GKeyFile *keyfile;
418         GError *error = NULL;
419         gsize length;
420         char **groups;
421         char *str;
422         gboolean protected, found = FALSE;
423         int i;
424
425         DBG("config %p", config);
426
427         keyfile = __connman_storage_load_config(config->ident);
428         if (keyfile == NULL)
429                 return -EIO;
430
431         /* Verify keys validity of the global section */
432         check_keys(keyfile, "global", config_possible_keys);
433
434         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
435         if (str != NULL) {
436                 g_free(config->name);
437                 config->name = str;
438         }
439
440         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
441         if (str != NULL) {
442                 g_free(config->description);
443                 config->description = str;
444         }
445
446         protected = g_key_file_get_boolean(keyfile, "global",
447                                         CONFIG_KEY_PROT, &error);
448         if (error == NULL)
449                 config->protected = protected;
450         else
451                 config->protected = TRUE;
452         g_clear_error(&error);
453
454         groups = g_key_file_get_groups(keyfile, &length);
455
456         for (i = 0; groups[i] != NULL; i++) {
457                 if (g_str_has_prefix(groups[i], "service_") == TRUE) {
458                         if (load_service(keyfile, groups[i], config) == 0)
459                                 found = TRUE;
460                 }
461         }
462
463         if (found == FALSE)
464                 connman_warn("Config file %s/%s.config does not contain any "
465                         "configuration that can be provisioned!",
466                         STORAGEDIR, config->ident);
467
468         g_strfreev(groups);
469
470         g_key_file_free(keyfile);
471
472         return 0;
473 }
474
475 static struct connman_config *create_config(const char *ident)
476 {
477         struct connman_config *config;
478
479         DBG("ident %s", ident);
480
481         if (g_hash_table_lookup(config_table, ident) != NULL)
482                 return NULL;
483
484         config = g_try_new0(struct connman_config, 1);
485         if (config == NULL)
486                 return NULL;
487
488         config->ident = g_strdup(ident);
489
490         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
491                                                 NULL, unregister_service);
492
493         g_hash_table_insert(config_table, config->ident, config);
494
495         connman_info("Adding configuration %s", config->ident);
496
497         return config;
498 }
499
500 static connman_bool_t validate_ident(const char *ident)
501 {
502         unsigned int i;
503
504         if (ident == NULL)
505                 return FALSE;
506
507         for (i = 0; i < strlen(ident); i++)
508                 if (g_ascii_isprint(ident[i]) == FALSE)
509                         return FALSE;
510
511         return TRUE;
512 }
513
514 static int read_configs(void)
515 {
516         GDir *dir;
517
518         DBG("");
519
520         dir = g_dir_open(STORAGEDIR, 0, NULL);
521         if (dir != NULL) {
522                 const gchar *file;
523
524                 while ((file = g_dir_read_name(dir)) != NULL) {
525                         GString *str;
526                         gchar *ident;
527
528                         if (g_str_has_suffix(file, ".config") == FALSE)
529                                 continue;
530
531                         ident = g_strrstr(file, ".config");
532                         if (ident == NULL)
533                                 continue;
534
535                         str = g_string_new_len(file, ident - file);
536                         if (str == NULL)
537                                 continue;
538
539                         ident = g_string_free(str, FALSE);
540
541                         if (validate_ident(ident) == TRUE) {
542                                 struct connman_config *config;
543
544                                 config = create_config(ident);
545                                 if (config != NULL)
546                                         load_config(config);
547                         } else {
548                                 connman_error("Invalid config ident %s", ident);
549                         }
550                         g_free(ident);
551                 }
552
553                 g_dir_close(dir);
554         }
555
556         return 0;
557 }
558
559 static void config_notify_handler(struct inotify_event *event,
560                                         const char *ident)
561 {
562         char *ext;
563
564         if (ident == NULL)
565                 return;
566
567         if (g_str_has_suffix(ident, ".config") == FALSE)
568                 return;
569
570         ext = g_strrstr(ident, ".config");
571         if (ext == NULL)
572                 return;
573
574         *ext = '\0';
575
576         if (validate_ident(ident) == FALSE) {
577                 connman_error("Invalid config ident %s", ident);
578                 return;
579         }
580
581         if (event->mask & IN_CREATE)
582                 create_config(ident);
583
584         if (event->mask & IN_MODIFY) {
585                 struct connman_config *config;
586
587                 config = g_hash_table_lookup(config_table, ident);
588                 if (config != NULL) {
589                         int ret;
590
591                         g_hash_table_remove_all(config->service_table);
592                         load_config(config);
593                         ret = __connman_service_provision_changed(ident);
594                         if (ret > 0) {
595                                 /*
596                                  * Re-scan the config file for any
597                                  * changes
598                                  */
599                                 g_hash_table_remove_all(config->service_table);
600                                 load_config(config);
601                                 __connman_service_provision_changed(ident);
602                         }
603                 }
604         }
605
606         if (event->mask & IN_DELETE)
607                 g_hash_table_remove(config_table, ident);
608 }
609
610 int __connman_config_init(void)
611 {
612         DBG("");
613
614         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
615                                                 NULL, unregister_config);
616
617         connman_inotify_register(STORAGEDIR, config_notify_handler);
618
619         return read_configs();
620 }
621
622 void __connman_config_cleanup(void)
623 {
624         DBG("");
625
626         cleanup = TRUE;
627
628         connman_inotify_unregister(STORAGEDIR, config_notify_handler);
629
630         g_hash_table_destroy(config_table);
631         config_table = NULL;
632
633         cleanup = FALSE;
634 }
635
636 static char *config_pem_fsid(const char *pem_file)
637 {
638         struct statfs buf;
639         unsigned *fsid = (unsigned *) &buf.f_fsid;
640         unsigned long long fsid64;
641
642         if (pem_file == NULL)
643                 return NULL;
644
645         if (statfs(pem_file, &buf) < 0) {
646                 connman_error("statfs error %s for %s",
647                                                 strerror(errno), pem_file);
648                 return NULL;
649         }
650
651         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
652
653         return g_strdup_printf("%llx", fsid64);
654 }
655
656 static void provision_service(gpointer key, gpointer value, gpointer user_data)
657 {
658         struct connman_service *service = user_data;
659         struct connman_config_service *config = value;
660         struct connman_network *network;
661         const void *ssid, *service_id;
662         unsigned int ssid_len;
663
664         /* For now only WiFi service entries are supported */
665         if (g_strcmp0(config->type, "wifi") != 0)
666                 return;
667
668         network = __connman_service_get_network(service);
669         if (network == NULL) {
670                 connman_error("Service has no network set");
671                 return;
672         }
673
674         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
675         if (ssid == NULL) {
676                 connman_error("Network SSID not set");
677                 return;
678         }
679
680         if (config->ssid == NULL || ssid_len != config->ssid_len)
681                 return;
682
683         if (memcmp(config->ssid, ssid, ssid_len) != 0)
684                 return;
685
686         service_id = __connman_service_get_ident(service);
687         config->service_identifiers =
688                 g_slist_prepend(config->service_identifiers,
689                                 g_strdup(service_id));
690
691         __connman_service_set_immutable(service, TRUE);
692
693         __connman_service_set_favorite_delayed(service, TRUE, TRUE);
694
695         __connman_service_set_config(service, config->config_ident,
696                                                 config->config_entry);
697
698         if (config->eap != NULL)
699                 __connman_service_set_string(service, "EAP", config->eap);
700
701         if (config->identity != NULL)
702                 __connman_service_set_string(service, "Identity",
703                                                         config->identity);
704
705         if (config->ca_cert_file != NULL)
706                 __connman_service_set_string(service, "CACertFile",
707                                                         config->ca_cert_file);
708
709         if (config->client_cert_file != NULL)
710                 __connman_service_set_string(service, "ClientCertFile",
711                                                 config->client_cert_file);
712
713         if (config->private_key_file != NULL)
714                 __connman_service_set_string(service, "PrivateKeyFile",
715                                                 config->private_key_file);
716
717         if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
718                                         config->private_key_file != NULL) {
719                 char *fsid;
720
721                 fsid = config_pem_fsid(config->private_key_file);
722                 if (fsid == NULL)
723                         return;
724
725                 g_free(config->private_key_passphrase);
726                 config->private_key_passphrase = fsid;
727         }
728
729         if (config->private_key_passphrase != NULL) {
730                 __connman_service_set_string(service, "PrivateKeyPassphrase",
731                                                 config->private_key_passphrase);
732                 /*
733                  * TODO: Support for PEAP with both identity and key passwd.
734                  * In that case, we should check if both of them are found
735                  * from the config file. If not, we should not set the
736                  * service passphrase in order for the UI to request for an
737                  * additional passphrase.
738                  */
739         }
740
741         if (config->phase2 != NULL)
742                 __connman_service_set_string(service, "Phase2", config->phase2);
743
744         if (config->passphrase != NULL)
745                 __connman_service_set_string(service, "Passphrase", config->passphrase);
746
747         if (config->hidden == TRUE)
748                 __connman_service_set_hidden(service);
749
750         __connman_service_mark_dirty();
751
752         __connman_service_save(service);
753 }
754
755 int __connman_config_provision_service(struct connman_service *service)
756 {
757         enum connman_service_type type;
758         GHashTableIter iter;
759         gpointer value, key;
760
761         DBG("service %p", service);
762
763         /* For now only WiFi services are supported */
764         type = connman_service_get_type(service);
765         if (type != CONNMAN_SERVICE_TYPE_WIFI)
766                 return -ENOSYS;
767
768         g_hash_table_iter_init(&iter, config_table);
769
770         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
771                 struct connman_config *config = value;
772
773                 g_hash_table_foreach(config->service_table,
774                                                 provision_service, service);
775         }
776
777         return 0;
778 }
779
780 int __connman_config_provision_service_ident(struct connman_service *service,
781                         const char *ident, const char *file, const char *entry)
782 {
783         enum connman_service_type type;
784         struct connman_config *config;
785         int ret = 0;
786
787         DBG("service %p", service);
788
789         /* For now only WiFi services are supported */
790         type = connman_service_get_type(service);
791         if (type != CONNMAN_SERVICE_TYPE_WIFI)
792                 return -ENOSYS;
793
794         config = g_hash_table_lookup(config_table, ident);
795         if (config != NULL) {
796                 GHashTableIter iter;
797                 gpointer value, key;
798                 gboolean found = FALSE;
799
800                 g_hash_table_iter_init(&iter, config->service_table);
801
802                 /*
803                  * Check if we need to remove individual service if it
804                  * is missing from config file.
805                  */
806                 if (file != NULL && entry != NULL) {
807                         while (g_hash_table_iter_next(&iter, &key,
808                                                         &value) == TRUE) {
809                                 struct connman_config_service *config_service;
810
811                                 config_service = value;
812
813                                 if (g_strcmp0(config_service->config_ident,
814                                                                 file) != 0)
815                                         continue;
816
817                                 if (g_strcmp0(config_service->config_entry,
818                                                                 entry) != 0)
819                                         continue;
820
821                                 found = TRUE;
822                                 break;
823                         }
824
825                         DBG("found %d ident %s file %s entry %s", found, ident,
826                                                                 file, entry);
827
828                         if (found == FALSE) {
829                                 /*
830                                  * The entry+8 will skip "service_" prefix
831                                  */
832                                 g_hash_table_remove(config->service_table,
833                                                 entry + 8);
834                                 ret = 1;
835                         }
836                 }
837
838                 g_hash_table_foreach(config->service_table,
839                                                 provision_service, service);
840         }
841
842         return ret;
843 }
844
845 struct connman_config_entry **connman_config_get_entries(const char *type)
846 {
847         GHashTableIter iter_file, iter_config;
848         gpointer value, key;
849         struct connman_config_entry **entries = NULL;
850         int i = 0, count;
851
852         g_hash_table_iter_init(&iter_file, config_table);
853         while (g_hash_table_iter_next(&iter_file, &key, &value) == TRUE) {
854                 struct connman_config *config_file = value;
855
856                 count = g_hash_table_size(config_file->service_table);
857
858                 entries = g_try_realloc(entries, (i + count + 1) *
859                                         sizeof(struct connman_config_entry *));
860                 if (entries == NULL)
861                         return NULL;
862
863                 g_hash_table_iter_init(&iter_config,
864                                                 config_file->service_table);
865                 while (g_hash_table_iter_next(&iter_config, &key,
866                                                         &value) == TRUE) {
867                         struct connman_config_service *config = value;
868
869                         if (type != NULL &&
870                                         g_strcmp0(config->type, type) != 0)
871                                 continue;
872
873                         entries[i] = g_try_new0(struct connman_config_entry,
874                                                 1);
875                         if (entries[i] == NULL)
876                                 goto cleanup;
877
878                         entries[i]->ident = g_strdup(config->ident);
879                         entries[i]->name = g_strdup(config->name);
880                         entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
881                         if (entries[i]->ssid == NULL)
882                                 goto cleanup;
883
884                         memcpy(entries[i]->ssid, config->ssid,
885                                                         config->ssid_len);
886                         entries[i]->ssid_len = config->ssid_len;
887                         entries[i]->hidden = config->hidden;
888
889                         i++;
890                 }
891         }
892
893         if (entries != NULL) {
894                 entries = g_try_realloc(entries, (i + 1) *
895                                         sizeof(struct connman_config_entry *));
896                 if (entries == NULL)
897                         return NULL;
898
899                 entries[i] = NULL;
900
901                 DBG("%d provisioned AP found", i);
902         }
903
904         return entries;
905
906 cleanup:
907         connman_config_free_entries(entries);
908         return NULL;
909 }
910
911 void connman_config_free_entries(struct connman_config_entry **entries)
912 {
913         int i;
914
915         if (entries == NULL)
916                 return;
917
918         for (i = 0; entries[i]; i++) {
919                 g_free(entries[i]->ident);
920                 g_free(entries[i]->name);
921                 g_free(entries[i]->ssid);
922                 g_free(entries[i]);
923         }
924
925         g_free(entries);
926         return;
927 }