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