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