config: Factor out config inotify handler
[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 int inotify_wd = -1;
70
71 static GIOChannel *inotify_channel = NULL;
72 static uint inotify_watch = 0;
73 static connman_bool_t cleanup = FALSE;
74
75 #define INTERNAL_CONFIG_PREFIX           "__internal"
76
77 /* Definition of possible strings in the .config files */
78 #define CONFIG_KEY_NAME                "Name"
79 #define CONFIG_KEY_DESC                "Description"
80 #define CONFIG_KEY_PROT                "Protected"
81
82 #define SERVICE_KEY_TYPE               "Type"
83 #define SERVICE_KEY_NAME               "Name"
84 #define SERVICE_KEY_SSID               "SSID"
85 #define SERVICE_KEY_EAP                "EAP"
86 #define SERVICE_KEY_CA_CERT            "CACertFile"
87 #define SERVICE_KEY_CL_CERT            "ClientCertFile"
88 #define SERVICE_KEY_PRV_KEY            "PrivateKeyFile"
89 #define SERVICE_KEY_PRV_KEY_PASS       "PrivateKeyPassphrase"
90 #define SERVICE_KEY_PRV_KEY_PASS_TYPE  "PrivateKeyPassphraseType"
91 #define SERVICE_KEY_IDENTITY           "Identity"
92 #define SERVICE_KEY_PHASE2             "Phase2"
93 #define SERVICE_KEY_PASSPHRASE         "Passphrase"
94 #define SERVICE_KEY_HIDDEN             "Hidden"
95
96 static const char *config_possible_keys[] = {
97         CONFIG_KEY_NAME,
98         CONFIG_KEY_DESC,
99         CONFIG_KEY_PROT,
100         NULL,
101 };
102
103 static const char *service_possible_keys[] = {
104         SERVICE_KEY_TYPE,
105         SERVICE_KEY_NAME,
106         SERVICE_KEY_SSID,
107         SERVICE_KEY_EAP,
108         SERVICE_KEY_CA_CERT,
109         SERVICE_KEY_CL_CERT,
110         SERVICE_KEY_PRV_KEY,
111         SERVICE_KEY_PRV_KEY_PASS,
112         SERVICE_KEY_PRV_KEY_PASS_TYPE,
113         SERVICE_KEY_IDENTITY,
114         SERVICE_KEY_PHASE2,
115         SERVICE_KEY_PASSPHRASE,
116         SERVICE_KEY_HIDDEN,
117         NULL,
118 };
119
120 static void unregister_config(gpointer data)
121 {
122         struct connman_config *config = data;
123
124         connman_info("Removing configuration %s", config->ident);
125
126         g_hash_table_destroy(config->service_table);
127
128         g_free(config->description);
129         g_free(config->name);
130         g_free(config->ident);
131         g_free(config);
132 }
133
134 static void unregister_service(gpointer data)
135 {
136         struct connman_config_service *config_service = data;
137         struct connman_service *service;
138         char *service_id;
139         GSList *list;
140
141         if (cleanup == TRUE)
142                 goto free_only;
143
144         connman_info("Removing service configuration %s",
145                                                 config_service->ident);
146
147         protected_services = g_slist_remove(protected_services,
148                                                 config_service);
149
150         for (list = config_service->service_identifiers; list != NULL;
151                                                         list = list->next) {
152                 service_id = list->data;
153
154                 service = __connman_service_lookup_from_ident(service_id);
155                 if (service != NULL) {
156                         __connman_service_set_immutable(service, FALSE);
157                         __connman_service_remove(service);
158                 }
159
160                 if (__connman_storage_remove_service(service_id) == FALSE)
161                         DBG("Could not remove all files for service %s",
162                                                                 service_id);
163         }
164
165 free_only:
166         g_free(config_service->ident);
167         g_free(config_service->type);
168         g_free(config_service->name);
169         g_free(config_service->ssid);
170         g_free(config_service->eap);
171         g_free(config_service->identity);
172         g_free(config_service->ca_cert_file);
173         g_free(config_service->client_cert_file);
174         g_free(config_service->private_key_file);
175         g_free(config_service->private_key_passphrase);
176         g_free(config_service->private_key_passphrase_type);
177         g_free(config_service->phase2);
178         g_free(config_service->passphrase);
179         g_slist_free_full(config_service->service_identifiers, g_free);
180         g_free(config_service->config_ident);
181         g_free(config_service->config_entry);
182         g_free(config_service);
183 }
184
185 static void check_keys(GKeyFile *keyfile, const char *group,
186                         const char **possible_keys)
187 {
188         char **avail_keys;
189         gsize nb_avail_keys, i, j;
190
191         avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
192         if (avail_keys == NULL)
193                 return;
194
195         /*
196          * For each key in the configuration file,
197          * verify it is understood by connman
198          */
199         for (i = 0 ; i < nb_avail_keys; i++) {
200                 for (j = 0; possible_keys[j] ; j++)
201                         if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
202                                 break;
203
204                 if (possible_keys[j] == NULL)
205                         connman_warn("Unknown configuration key %s in [%s]",
206                                         avail_keys[i], group);
207         }
208
209         g_strfreev(avail_keys);
210 }
211
212 static connman_bool_t
213 is_protected_service(struct connman_config_service *service)
214 {
215         GSList *list;
216
217         DBG("ident %s", service->ident);
218
219         for (list = protected_services; list; list = list->next) {
220                 struct connman_config_service *s = list->data;
221
222                 if (g_strcmp0(s->type, service->type) != 0)
223                         continue;
224
225                 if (s->ssid == NULL || service->ssid == NULL)
226                         continue;
227
228                 if (s->ssid_len != service->ssid_len)
229                         continue;
230
231                 if (g_strcmp0(service->type, "wifi") == 0 &&
232                         strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
233                         return TRUE;
234                 }
235         }
236
237         return FALSE;
238 }
239
240 static int load_service(GKeyFile *keyfile, const char *group,
241                                                 struct connman_config *config)
242 {
243         struct connman_config_service *service;
244         const char *ident;
245         char *str, *hex_ssid;
246         gboolean service_created = FALSE;
247         int err;
248
249         /* Strip off "service_" prefix */
250         ident = group + 8;
251
252         if (strlen(ident) < 1)
253                 return -EINVAL;
254
255         /* Verify that provided keys are good */
256         check_keys(keyfile, group, service_possible_keys);
257
258         service = g_hash_table_lookup(config->service_table, ident);
259         if (service == NULL) {
260                 service = g_try_new0(struct connman_config_service, 1);
261                 if (service == NULL)
262                         return -ENOMEM;
263
264                 service->ident = g_strdup(ident);
265
266                 service_created = TRUE;
267         }
268
269         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
270         if (str != NULL) {
271                 g_free(service->type);
272                 service->type = str;
273         }
274
275         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
276         if (str != NULL) {
277                 g_free(service->name);
278                 service->name = str;
279         }
280
281         hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
282                                          NULL);
283         if (hex_ssid != NULL) {
284                 char *ssid;
285                 unsigned int i, j = 0, hex;
286                 size_t hex_ssid_len = strlen(hex_ssid);
287
288                 ssid = g_try_malloc0(hex_ssid_len / 2);
289                 if (ssid == NULL) {
290                         err = -ENOMEM;
291                         g_free(hex_ssid);
292                         goto err;
293                 }
294
295                 for (i = 0; i < hex_ssid_len; i += 2) {
296                         if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
297                                 connman_warn("Invalid SSID %s", hex_ssid);
298                                 g_free(ssid);
299                                 g_free(hex_ssid);
300                                 err = -EILSEQ;
301                                 goto err;
302                         }
303                         ssid[j++] = hex;
304                 }
305
306                 g_free(hex_ssid);
307
308                 g_free(service->ssid);
309                 service->ssid = ssid;
310                 service->ssid_len = hex_ssid_len / 2;
311         } else if (service->name != NULL) {
312                 char *ssid;
313                 unsigned int ssid_len;
314
315                 ssid_len = strlen(service->name);
316                 ssid = g_try_malloc0(ssid_len);
317                 if (ssid == NULL) {
318                         err = -ENOMEM;
319                         goto err;
320                 }
321
322                 memcpy(ssid, service->name, ssid_len);
323                 g_free(service->ssid);
324                 service->ssid = ssid;
325                 service->ssid_len = ssid_len;
326         }
327
328         if (is_protected_service(service) == TRUE) {
329                 connman_error("Trying to provision a protected service");
330                 err = -EACCES;
331                 goto err;
332         }
333
334         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
335         if (str != NULL) {
336                 g_free(service->eap);
337                 service->eap = str;
338         }
339
340         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
341         if (str != NULL) {
342                 g_free(service->ca_cert_file);
343                 service->ca_cert_file = str;
344         }
345
346         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
347         if (str != NULL) {
348                 g_free(service->client_cert_file);
349                 service->client_cert_file = str;
350         }
351
352         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
353         if (str != NULL) {
354                 g_free(service->private_key_file);
355                 service->private_key_file = str;
356         }
357
358         str = g_key_file_get_string(keyfile, group,
359                                                 SERVICE_KEY_PRV_KEY_PASS, NULL);
360         if (str != NULL) {
361                 g_free(service->private_key_passphrase);
362                 service->private_key_passphrase = str;
363         }
364
365         str = g_key_file_get_string(keyfile, group,
366                                         SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
367         if (str != NULL) {
368                 g_free(service->private_key_passphrase_type);
369                 service->private_key_passphrase_type = str;
370         }
371
372         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
373         if (str != NULL) {
374                 g_free(service->identity);
375                 service->identity = str;
376         }
377
378         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
379         if (str != NULL) {
380                 g_free(service->phase2);
381                 service->phase2 = str;
382         }
383
384         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
385                                         NULL);
386         if (str != NULL) {
387                 g_free(service->passphrase);
388                 service->passphrase = str;
389         }
390
391         service->config_ident = g_strdup(config->ident);
392         service->config_entry = g_strdup_printf("service_%s", service->ident);
393
394         service->hidden = g_key_file_get_boolean(keyfile, group,
395                                                 SERVICE_KEY_HIDDEN, NULL);
396
397         if (service_created)
398                 g_hash_table_insert(config->service_table, service->ident,
399                                         service);
400
401         if (config->protected == TRUE)
402                 protected_services =
403                         g_slist_prepend(protected_services, service);
404
405         connman_info("Adding service configuration %s", service->ident);
406
407         return 0;
408
409 err:
410         if (service_created == TRUE) {
411                 g_free(service->ident);
412                 g_free(service->type);
413                 g_free(service->name);
414                 g_free(service->ssid);
415                 g_free(service);
416         }
417
418         return err;
419 }
420
421 static int load_config(struct connman_config *config)
422 {
423         GKeyFile *keyfile;
424         GError *error = NULL;
425         gsize length;
426         char **groups;
427         char *str;
428         gboolean protected, found = FALSE;
429         int i;
430
431         DBG("config %p", config);
432
433         keyfile = __connman_storage_load_config(config->ident);
434         if (keyfile == NULL)
435                 return -EIO;
436
437         /* Verify keys validity of the global section */
438         check_keys(keyfile, "global", config_possible_keys);
439
440         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
441         if (str != NULL) {
442                 g_free(config->name);
443                 config->name = str;
444         }
445
446         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
447         if (str != NULL) {
448                 g_free(config->description);
449                 config->description = str;
450         }
451
452         protected = g_key_file_get_boolean(keyfile, "global",
453                                         CONFIG_KEY_PROT, &error);
454         if (error == NULL)
455                 config->protected = protected;
456         else
457                 config->protected = TRUE;
458         g_clear_error(&error);
459
460         groups = g_key_file_get_groups(keyfile, &length);
461
462         for (i = 0; groups[i] != NULL; i++) {
463                 if (g_str_has_prefix(groups[i], "service_") == TRUE) {
464                         if (load_service(keyfile, groups[i], config) == 0)
465                                 found = TRUE;
466                 }
467         }
468
469         if (found == FALSE)
470                 connman_warn("Config file %s/%s.config does not contain any "
471                         "configuration that can be provisioned!",
472                         STORAGEDIR, config->ident);
473
474         g_strfreev(groups);
475
476         g_key_file_free(keyfile);
477
478         return 0;
479 }
480
481 static struct connman_config *create_config(const char *ident)
482 {
483         struct connman_config *config;
484
485         DBG("ident %s", ident);
486
487         if (g_hash_table_lookup(config_table, ident) != NULL)
488                 return NULL;
489
490         config = g_try_new0(struct connman_config, 1);
491         if (config == NULL)
492                 return NULL;
493
494         config->ident = g_strdup(ident);
495
496         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
497                                                 NULL, unregister_service);
498
499         g_hash_table_insert(config_table, config->ident, config);
500
501         connman_info("Adding configuration %s", config->ident);
502
503         return config;
504 }
505
506 static connman_bool_t validate_ident(const char *ident)
507 {
508         unsigned int i;
509
510         if (ident == NULL)
511                 return FALSE;
512
513         for (i = 0; i < strlen(ident); i++)
514                 if (g_ascii_isprint(ident[i]) == FALSE)
515                         return FALSE;
516
517         return TRUE;
518 }
519
520 static int read_configs(void)
521 {
522         GDir *dir;
523
524         DBG("");
525
526         dir = g_dir_open(STORAGEDIR, 0, NULL);
527         if (dir != NULL) {
528                 const gchar *file;
529
530                 while ((file = g_dir_read_name(dir)) != NULL) {
531                         GString *str;
532                         gchar *ident;
533
534                         if (g_str_has_suffix(file, ".config") == FALSE)
535                                 continue;
536
537                         ident = g_strrstr(file, ".config");
538                         if (ident == NULL)
539                                 continue;
540
541                         str = g_string_new_len(file, ident - file);
542                         if (str == NULL)
543                                 continue;
544
545                         ident = g_string_free(str, FALSE);
546
547                         if (validate_ident(ident) == TRUE) {
548                                 struct connman_config *config;
549
550                                 config = create_config(ident);
551                                 if (config != NULL)
552                                         load_config(config);
553                         } else {
554                                 connman_error("Invalid config ident %s", ident);
555                         }
556                         g_free(ident);
557                 }
558
559                 g_dir_close(dir);
560         }
561
562         return 0;
563 }
564
565 static void config_notify_handler(struct inotify_event *event,
566                                         const char *ident)
567 {
568         char *ext;
569
570         if (ident == NULL)
571                 return;
572
573         if (g_str_has_suffix(ident, ".config") == FALSE)
574                 return;
575
576         ext = g_strrstr(ident, ".config");
577         if (ext == NULL)
578                 return;
579
580         *ext = '\0';
581
582         if (validate_ident(ident) == FALSE) {
583                 connman_error("Invalid config ident %s", ident);
584                 return;
585         }
586
587         if (event->mask & IN_CREATE)
588                 create_config(ident);
589
590         if (event->mask & IN_MODIFY) {
591                 struct connman_config *config;
592
593                 config = g_hash_table_lookup(config_table, ident);
594                 if (config != NULL) {
595                         int ret;
596
597                         g_hash_table_remove_all(config->service_table);
598                         load_config(config);
599                         ret = __connman_service_provision_changed(ident);
600                         if (ret > 0) {
601                                 /*
602                                  * Re-scan the config file for any
603                                  * changes
604                                  */
605                                 g_hash_table_remove_all(config->service_table);
606                                 load_config(config);
607                                 __connman_service_provision_changed(ident);
608                         }
609                 }
610         }
611
612         if (event->mask & IN_DELETE)
613                 g_hash_table_remove(config_table, ident);
614 }
615
616 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
617                                                         gpointer user_data)
618 {
619         char buffer[256];
620         char *next_event;
621         gsize bytes_read;
622         GIOStatus status;
623
624         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
625                 inotify_watch = 0;
626                 return FALSE;
627         }
628
629         status = g_io_channel_read_chars(channel, buffer,
630                                         sizeof(buffer) -1, &bytes_read, NULL);
631
632         switch (status) {
633         case G_IO_STATUS_NORMAL:
634                 break;
635         case G_IO_STATUS_AGAIN:
636                 return TRUE;
637         default:
638                 connman_error("Reading from inotify channel failed");
639                 inotify_watch = 0;
640                 return FALSE;
641         }
642
643         next_event = buffer;
644
645         while (bytes_read > 0) {
646                 struct inotify_event *event;
647                 gchar *ident;
648                 gsize len;
649
650                 event = (struct inotify_event *) next_event;
651                 if (event->len)
652                         ident = next_event + sizeof(struct inotify_event);
653                 else
654                         ident = NULL;
655
656                 len = sizeof(struct inotify_event) + event->len;
657
658                 /* check if inotify_event block fit */
659                 if (len > bytes_read)
660                         break;
661
662                 next_event += len;
663                 bytes_read -= len;
664
665                 config_notify_handler(event, ident);
666         }
667
668         return TRUE;
669 }
670
671 static int create_watch(void)
672 {
673         int fd;
674
675         fd = inotify_init();
676         if (fd < 0)
677                 return -EIO;
678
679         inotify_wd = inotify_add_watch(fd, STORAGEDIR,
680                                         IN_MODIFY | IN_CREATE | IN_DELETE);
681         if (inotify_wd < 0) {
682                 connman_error("Creation of STORAGEDIR  watch failed");
683                 close(fd);
684                 return -EIO;
685         }
686
687         inotify_channel = g_io_channel_unix_new(fd);
688         if (inotify_channel == NULL) {
689                 connman_error("Creation of inotify channel failed");
690                 inotify_rm_watch(fd, inotify_wd);
691                 inotify_wd = 0;
692
693                 close(fd);
694                 return -EIO;
695         }
696
697         g_io_channel_set_close_on_unref(inotify_channel, TRUE);
698         g_io_channel_set_encoding(inotify_channel, NULL, NULL);
699         g_io_channel_set_buffered(inotify_channel, FALSE);
700
701         inotify_watch = g_io_add_watch(inotify_channel,
702                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
703                                 inotify_data, NULL);
704
705         return 0;
706 }
707
708 static void remove_watch(void)
709 {
710         int fd;
711
712         if (inotify_channel == NULL)
713                 return;
714
715         if (inotify_watch > 0) {
716                 g_source_remove(inotify_watch);
717                 inotify_watch = 0;
718         }
719
720         fd = g_io_channel_unix_get_fd(inotify_channel);
721
722         if (inotify_wd >= 0) {
723                 inotify_rm_watch(fd, inotify_wd);
724                 inotify_wd = 0;
725         }
726
727         g_io_channel_unref(inotify_channel);
728 }
729
730 int __connman_config_init(void)
731 {
732         DBG("");
733
734         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
735                                                 NULL, unregister_config);
736
737         create_watch();
738
739         return read_configs();
740 }
741
742 void __connman_config_cleanup(void)
743 {
744         DBG("");
745
746         cleanup = TRUE;
747
748         remove_watch();
749
750         g_hash_table_destroy(config_table);
751         config_table = NULL;
752
753         cleanup = FALSE;
754 }
755
756 static char *config_pem_fsid(const char *pem_file)
757 {
758         struct statfs buf;
759         unsigned *fsid = (unsigned *) &buf.f_fsid;
760         unsigned long long fsid64;
761
762         if (pem_file == NULL)
763                 return NULL;
764
765         if (statfs(pem_file, &buf) < 0) {
766                 connman_error("statfs error %s for %s",
767                                                 strerror(errno), pem_file);
768                 return NULL;
769         }
770
771         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
772
773         return g_strdup_printf("%llx", fsid64);
774 }
775
776 static void provision_service(gpointer key, gpointer value, gpointer user_data)
777 {
778         struct connman_service *service = user_data;
779         struct connman_config_service *config = value;
780         struct connman_network *network;
781         const void *ssid, *service_id;
782         unsigned int ssid_len;
783
784         /* For now only WiFi service entries are supported */
785         if (g_strcmp0(config->type, "wifi") != 0)
786                 return;
787
788         network = __connman_service_get_network(service);
789         if (network == NULL) {
790                 connman_error("Service has no network set");
791                 return;
792         }
793
794         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
795         if (ssid == NULL) {
796                 connman_error("Network SSID not set");
797                 return;
798         }
799
800         if (config->ssid == NULL || ssid_len != config->ssid_len)
801                 return;
802
803         if (memcmp(config->ssid, ssid, ssid_len) != 0)
804                 return;
805
806         service_id = __connman_service_get_ident(service);
807         config->service_identifiers =
808                 g_slist_prepend(config->service_identifiers,
809                                 g_strdup(service_id));
810
811         __connman_service_set_immutable(service, TRUE);
812
813         __connman_service_set_favorite_delayed(service, TRUE, TRUE);
814
815         __connman_service_set_config(service, config->config_ident,
816                                                 config->config_entry);
817
818         if (config->eap != NULL)
819                 __connman_service_set_string(service, "EAP", config->eap);
820
821         if (config->identity != NULL)
822                 __connman_service_set_string(service, "Identity",
823                                                         config->identity);
824
825         if (config->ca_cert_file != NULL)
826                 __connman_service_set_string(service, "CACertFile",
827                                                         config->ca_cert_file);
828
829         if (config->client_cert_file != NULL)
830                 __connman_service_set_string(service, "ClientCertFile",
831                                                 config->client_cert_file);
832
833         if (config->private_key_file != NULL)
834                 __connman_service_set_string(service, "PrivateKeyFile",
835                                                 config->private_key_file);
836
837         if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
838                                         config->private_key_file != NULL) {
839                 char *fsid;
840
841                 fsid = config_pem_fsid(config->private_key_file);
842                 if (fsid == NULL)
843                         return;
844
845                 g_free(config->private_key_passphrase);
846                 config->private_key_passphrase = fsid;
847         }
848
849         if (config->private_key_passphrase != NULL) {
850                 __connman_service_set_string(service, "PrivateKeyPassphrase",
851                                                 config->private_key_passphrase);
852                 /*
853                  * TODO: Support for PEAP with both identity and key passwd.
854                  * In that case, we should check if both of them are found
855                  * from the config file. If not, we should not set the
856                  * service passphrase in order for the UI to request for an
857                  * additional passphrase.
858                  */
859         }
860
861         if (config->phase2 != NULL)
862                 __connman_service_set_string(service, "Phase2", config->phase2);
863
864         if (config->passphrase != NULL)
865                 __connman_service_set_string(service, "Passphrase", config->passphrase);
866
867         if (config->hidden == TRUE)
868                 __connman_service_set_hidden(service);
869
870         __connman_service_mark_dirty();
871
872         __connman_service_save(service);
873 }
874
875 int __connman_config_provision_service(struct connman_service *service)
876 {
877         enum connman_service_type type;
878         GHashTableIter iter;
879         gpointer value, key;
880
881         DBG("service %p", service);
882
883         /* For now only WiFi services are supported */
884         type = connman_service_get_type(service);
885         if (type != CONNMAN_SERVICE_TYPE_WIFI)
886                 return -ENOSYS;
887
888         g_hash_table_iter_init(&iter, config_table);
889
890         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
891                 struct connman_config *config = value;
892
893                 g_hash_table_foreach(config->service_table,
894                                                 provision_service, service);
895         }
896
897         return 0;
898 }
899
900 int __connman_config_provision_service_ident(struct connman_service *service,
901                         const char *ident, const char *file, const char *entry)
902 {
903         enum connman_service_type type;
904         struct connman_config *config;
905         int ret = 0;
906
907         DBG("service %p", service);
908
909         /* For now only WiFi services are supported */
910         type = connman_service_get_type(service);
911         if (type != CONNMAN_SERVICE_TYPE_WIFI)
912                 return -ENOSYS;
913
914         config = g_hash_table_lookup(config_table, ident);
915         if (config != NULL) {
916                 GHashTableIter iter;
917                 gpointer value, key;
918                 gboolean found = FALSE;
919
920                 g_hash_table_iter_init(&iter, config->service_table);
921
922                 /*
923                  * Check if we need to remove individual service if it
924                  * is missing from config file.
925                  */
926                 if (file != NULL && entry != NULL) {
927                         while (g_hash_table_iter_next(&iter, &key,
928                                                         &value) == TRUE) {
929                                 struct connman_config_service *config_service;
930
931                                 config_service = value;
932
933                                 if (g_strcmp0(config_service->config_ident,
934                                                                 file) != 0)
935                                         continue;
936
937                                 if (g_strcmp0(config_service->config_entry,
938                                                                 entry) != 0)
939                                         continue;
940
941                                 found = TRUE;
942                                 break;
943                         }
944
945                         DBG("found %d ident %s file %s entry %s", found, ident,
946                                                                 file, entry);
947
948                         if (found == FALSE) {
949                                 /*
950                                  * The entry+8 will skip "service_" prefix
951                                  */
952                                 g_hash_table_remove(config->service_table,
953                                                 entry + 8);
954                                 ret = 1;
955                         }
956                 }
957
958                 g_hash_table_foreach(config->service_table,
959                                                 provision_service, service);
960         }
961
962         return ret;
963 }
964
965 struct connman_config_entry **connman_config_get_entries(void)
966 {
967         GHashTableIter iter_file, iter_config;
968         gpointer value, key;
969         struct connman_config_entry **entries = NULL;
970         int i = 0, count;
971
972         g_hash_table_iter_init(&iter_file, config_table);
973         while (g_hash_table_iter_next(&iter_file, &key, &value) == TRUE) {
974                 struct connman_config *config_file = value;
975
976                 count = g_hash_table_size(config_file->service_table);
977
978                 entries = g_try_realloc(entries, (i + count + 1) *
979                                         sizeof(struct connman_config_entry *));
980                 if (entries == NULL)
981                         return NULL;
982
983                 g_hash_table_iter_init(&iter_config,
984                                                 config_file->service_table);
985                 while (g_hash_table_iter_next(&iter_config, &key,
986                                                         &value) == TRUE) {
987                         struct connman_config_service *config = value;
988
989                         entries[i] = g_try_new0(struct connman_config_entry,
990                                                 1);
991                         if (entries[i] == NULL)
992                                 goto cleanup;
993
994                         entries[i]->ident = g_strdup(config->ident);
995                         entries[i]->name = g_strdup(config->name);
996                         entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
997                         if (entries[i]->ssid == NULL)
998                                 goto cleanup;
999
1000                         memcpy(entries[i]->ssid, config->ssid,
1001                                                         config->ssid_len);
1002                         entries[i]->ssid_len = config->ssid_len;
1003                         entries[i]->hidden = config->hidden;
1004
1005                         i++;
1006                 }
1007         }
1008
1009         if (entries != NULL) {
1010                 entries = g_try_realloc(entries, (i + 1) *
1011                                         sizeof(struct connman_config_entry *));
1012                 if (entries == NULL)
1013                         return NULL;
1014
1015                 entries[i] = NULL;
1016
1017                 DBG("%d provisioned AP found", i);
1018         }
1019
1020         return entries;
1021
1022 cleanup:
1023         connman_config_free_entries(entries);
1024         return NULL;
1025 }
1026
1027 void connman_config_free_entries(struct connman_config_entry **entries)
1028 {
1029         int i;
1030
1031         if (entries == NULL)
1032                 return;
1033
1034         for (i = 0; entries[i]; i++) {
1035                 g_free(entries[i]->ident);
1036                 g_free(entries[i]->name);
1037                 g_free(entries[i]->ssid);
1038                 g_free(entries[i]);
1039         }
1040
1041         g_free(entries);
1042         return;
1043 }