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