wifi: Fix tethering with kernel 3.5
[profile/ivi/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 #if defined TIZEN_EXT
452         g_clear_error(&error);
453 #endif
454
455         groups = g_key_file_get_groups(keyfile, &length);
456
457         for (i = 0; groups[i] != NULL; i++) {
458                 if (g_str_has_prefix(groups[i], "service_") == TRUE) {
459                         if (load_service(keyfile, groups[i], config) == 0)
460                                 found = TRUE;
461                 }
462         }
463
464         if (found == FALSE)
465                 connman_warn("Config file %s/%s.config does not contain any "
466                         "configuration that can be provisioned!",
467                         STORAGEDIR, config->ident);
468
469         g_strfreev(groups);
470
471         g_key_file_free(keyfile);
472
473         return 0;
474 }
475
476 static struct connman_config *create_config(const char *ident)
477 {
478         struct connman_config *config;
479
480         DBG("ident %s", ident);
481
482         if (g_hash_table_lookup(config_table, ident) != NULL)
483                 return NULL;
484
485         config = g_try_new0(struct connman_config, 1);
486         if (config == NULL)
487                 return NULL;
488
489         config->ident = g_strdup(ident);
490
491         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
492                                                 NULL, unregister_service);
493
494         g_hash_table_insert(config_table, config->ident, config);
495
496         connman_info("Adding configuration %s", config->ident);
497
498         return config;
499 }
500
501 static connman_bool_t validate_ident(const char *ident)
502 {
503         unsigned int i;
504
505         if (ident == NULL)
506                 return FALSE;
507
508         for (i = 0; i < strlen(ident); i++)
509                 if (g_ascii_isprint(ident[i]) == FALSE)
510                         return FALSE;
511
512         return TRUE;
513 }
514
515 static int read_configs(void)
516 {
517         GDir *dir;
518
519         DBG("");
520
521         dir = g_dir_open(STORAGEDIR, 0, NULL);
522         if (dir != NULL) {
523                 const gchar *file;
524
525                 while ((file = g_dir_read_name(dir)) != NULL) {
526                         GString *str;
527                         gchar *ident;
528
529                         if (g_str_has_suffix(file, ".config") == FALSE)
530                                 continue;
531
532                         ident = g_strrstr(file, ".config");
533                         if (ident == NULL)
534                                 continue;
535
536                         str = g_string_new_len(file, ident - file);
537                         if (str == NULL)
538                                 continue;
539
540                         ident = g_string_free(str, FALSE);
541
542                         if (validate_ident(ident) == TRUE) {
543                                 struct connman_config *config;
544
545                                 config = create_config(ident);
546                                 if (config != NULL)
547                                         load_config(config);
548                         } else {
549                                 connman_error("Invalid config ident %s", ident);
550                         }
551                         g_free(ident);
552                 }
553
554                 g_dir_close(dir);
555         }
556
557         return 0;
558 }
559
560 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
561                                                         gpointer user_data)
562 {
563         char buffer[256];
564         char *next_event;
565         gsize bytes_read;
566         GIOStatus status;
567
568         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
569                 inotify_watch = 0;
570                 return FALSE;
571         }
572
573         status = g_io_channel_read_chars(channel, buffer,
574                                         sizeof(buffer) -1, &bytes_read, NULL);
575
576         switch (status) {
577         case G_IO_STATUS_NORMAL:
578                 break;
579         case G_IO_STATUS_AGAIN:
580                 return TRUE;
581         default:
582                 connman_error("Reading from inotify channel failed");
583                 inotify_watch = 0;
584                 return FALSE;
585         }
586
587         next_event = buffer;
588
589         while (bytes_read > 0) {
590                 struct inotify_event *event;
591                 gchar *ext;
592                 gchar *ident;
593                 gsize len;
594
595                 event = (struct inotify_event *) next_event;
596                 if (event->len)
597                         ident = next_event + sizeof(struct inotify_event);
598                 else
599                         ident = NULL;
600
601                 len = sizeof(struct inotify_event) + event->len;
602
603                 /* check if inotify_event block fit */
604                 if (len > bytes_read)
605                         break;
606
607                 next_event += len;
608                 bytes_read -= len;
609
610                 if (ident == NULL)
611                         continue;
612
613                 if (g_str_has_suffix(ident, ".config") == FALSE)
614                         continue;
615
616                 ext = g_strrstr(ident, ".config");
617                 if (ext == NULL)
618                         continue;
619
620                 *ext = '\0';
621
622                 if (validate_ident(ident) == FALSE) {
623                         connman_error("Invalid config ident %s", ident);
624                         continue;
625                 }
626
627                 if (event->mask & IN_CREATE)
628                         create_config(ident);
629
630                 if (event->mask & IN_MODIFY) {
631                         struct connman_config *config;
632
633                         config = g_hash_table_lookup(config_table, ident);
634                         if (config != NULL) {
635                                 int ret;
636
637                                 g_hash_table_remove_all(config->service_table);
638                                 load_config(config);
639                                 ret = __connman_service_provision_changed(ident);
640                                 if (ret > 0) {
641                                         /*
642                                          * Re-scan the config file for any
643                                          * changes
644                                          */
645                                         g_hash_table_remove_all(config->service_table);
646                                         load_config(config);
647                                         __connman_service_provision_changed(ident);
648                                 }
649 #if defined TIZEN_EXT
650                                 __connman_service_auto_connect();
651 #endif
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         __connman_service_mark_dirty();
859
860         __connman_service_save(service);
861 }
862
863 int __connman_config_provision_service(struct connman_service *service)
864 {
865         enum connman_service_type type;
866         GHashTableIter iter;
867         gpointer value, key;
868
869         DBG("service %p", service);
870
871         /* For now only WiFi services are supported */
872         type = connman_service_get_type(service);
873         if (type != CONNMAN_SERVICE_TYPE_WIFI)
874                 return -ENOSYS;
875
876         g_hash_table_iter_init(&iter, config_table);
877
878         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
879                 struct connman_config *config = value;
880
881                 g_hash_table_foreach(config->service_table,
882                                                 provision_service, service);
883         }
884
885         return 0;
886 }
887
888 int __connman_config_provision_service_ident(struct connman_service *service,
889                         const char *ident, const char *file, const char *entry)
890 {
891         enum connman_service_type type;
892         struct connman_config *config;
893         int ret = 0;
894
895         DBG("service %p", service);
896
897         /* For now only WiFi services are supported */
898         type = connman_service_get_type(service);
899         if (type != CONNMAN_SERVICE_TYPE_WIFI)
900                 return -ENOSYS;
901
902         config = g_hash_table_lookup(config_table, ident);
903         if(config != NULL) {
904                 GHashTableIter iter;
905                 gpointer value, key;
906                 gboolean found = FALSE;
907
908                 g_hash_table_iter_init(&iter, config->service_table);
909
910                 /*
911                  * Check if we need to remove individual service if it
912                  * is missing from config file.
913                  */
914                 if (file != NULL && entry != NULL) {
915                         while (g_hash_table_iter_next(&iter, &key,
916                                                         &value) == TRUE) {
917                                 struct connman_config_service *config = value;
918
919                                 if (g_strcmp0(config->config_ident,
920                                                                 file) == 0 &&
921                                                 g_strcmp0(config->config_entry,
922                                                                 entry) == 0) {
923                                         found = TRUE;
924                                         break;
925                                 }
926                         }
927
928                         DBG("found %d ident %s file %s entry %s", found, ident,
929                                                                 file, entry);
930
931                         if (found == FALSE) {
932                                 /*
933                                  * The entry+8 will skip "service_" prefix
934                                  */
935                                 g_hash_table_remove(config->service_table,
936                                                 entry + 8);
937                                 ret = 1;
938                         }
939                 }
940
941                 g_hash_table_foreach(config->service_table,
942                                                 provision_service, service);
943         }
944
945         return ret;
946 }