config: Make load service from each keyfile group as a function
[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 <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <sys/vfs.h>
32 #include <sys/inotify.h>
33 #include <netdb.h>
34 #include <glib.h>
35
36 #include <connman/provision.h>
37 #include <connman/ipaddress.h>
38 #include "connman.h"
39
40 struct connman_config_service {
41         char *ident;
42         char *name;
43         char *type;
44         void *ssid;
45         unsigned int ssid_len;
46         char *eap;
47         char *identity;
48         char *ca_cert_file;
49         char *client_cert_file;
50         char *private_key_file;
51         char *private_key_passphrase;
52         char *private_key_passphrase_type;
53         char *phase2;
54         char *passphrase;
55         GSList *service_identifiers;
56         char *config_ident; /* file prefix */
57         char *config_entry; /* entry name */
58         connman_bool_t hidden;
59         char *ipv4_address;
60         char *ipv4_netmask;
61         char *ipv4_gateway;
62         char *ipv6_address;
63         unsigned char ipv6_prefix_length;
64         char *ipv6_gateway;
65         char *ipv6_privacy;
66         char *mac;
67         char **nameservers;
68         char **search_domains;
69         char **timeservers;
70         char *domain_name;
71 };
72
73 struct connman_config {
74         char *ident;
75         char *name;
76         char *description;
77         connman_bool_t protected;
78         GHashTable *service_table;
79 };
80
81 static GHashTable *config_table = NULL;
82 static GSList *protected_services = NULL;
83
84 static connman_bool_t cleanup = FALSE;
85
86 /* Definition of possible strings in the .config files */
87 #define CONFIG_KEY_NAME                "Name"
88 #define CONFIG_KEY_DESC                "Description"
89 #define CONFIG_KEY_PROT                "Protected"
90
91 #define SERVICE_KEY_TYPE               "Type"
92 #define SERVICE_KEY_NAME               "Name"
93 #define SERVICE_KEY_SSID               "SSID"
94 #define SERVICE_KEY_EAP                "EAP"
95 #define SERVICE_KEY_CA_CERT            "CACertFile"
96 #define SERVICE_KEY_CL_CERT            "ClientCertFile"
97 #define SERVICE_KEY_PRV_KEY            "PrivateKeyFile"
98 #define SERVICE_KEY_PRV_KEY_PASS       "PrivateKeyPassphrase"
99 #define SERVICE_KEY_PRV_KEY_PASS_TYPE  "PrivateKeyPassphraseType"
100 #define SERVICE_KEY_IDENTITY           "Identity"
101 #define SERVICE_KEY_PHASE2             "Phase2"
102 #define SERVICE_KEY_PASSPHRASE         "Passphrase"
103 #define SERVICE_KEY_HIDDEN             "Hidden"
104
105 #define SERVICE_KEY_IPv4               "IPv4"
106 #define SERVICE_KEY_IPv6               "IPv6"
107 #define SERVICE_KEY_IPv6_PRIVACY       "IPv6.Privacy"
108 #define SERVICE_KEY_MAC                "MAC"
109 #define SERVICE_KEY_NAMESERVERS        "Nameservers"
110 #define SERVICE_KEY_SEARCH_DOMAINS     "SearchDomains"
111 #define SERVICE_KEY_TIMESERVERS        "Timeservers"
112 #define SERVICE_KEY_DOMAIN             "Domain"
113
114 static const char *config_possible_keys[] = {
115         CONFIG_KEY_NAME,
116         CONFIG_KEY_DESC,
117         CONFIG_KEY_PROT,
118         NULL,
119 };
120
121 static const char *service_possible_keys[] = {
122         SERVICE_KEY_TYPE,
123         SERVICE_KEY_NAME,
124         SERVICE_KEY_SSID,
125         SERVICE_KEY_EAP,
126         SERVICE_KEY_CA_CERT,
127         SERVICE_KEY_CL_CERT,
128         SERVICE_KEY_PRV_KEY,
129         SERVICE_KEY_PRV_KEY_PASS,
130         SERVICE_KEY_PRV_KEY_PASS_TYPE,
131         SERVICE_KEY_IDENTITY,
132         SERVICE_KEY_PHASE2,
133         SERVICE_KEY_PASSPHRASE,
134         SERVICE_KEY_HIDDEN,
135         SERVICE_KEY_IPv4,
136         SERVICE_KEY_IPv6,
137         SERVICE_KEY_IPv6_PRIVACY,
138         SERVICE_KEY_MAC,
139         SERVICE_KEY_NAMESERVERS,
140         SERVICE_KEY_SEARCH_DOMAINS,
141         SERVICE_KEY_TIMESERVERS,
142         SERVICE_KEY_DOMAIN,
143         NULL,
144 };
145
146 static void unregister_config(gpointer data)
147 {
148         struct connman_config *config = data;
149
150         connman_info("Removing configuration %s", config->ident);
151
152         g_hash_table_destroy(config->service_table);
153
154         g_free(config->description);
155         g_free(config->name);
156         g_free(config->ident);
157         g_free(config);
158 }
159
160 static void unregister_service(gpointer data)
161 {
162         struct connman_config_service *config_service = data;
163         struct connman_service *service;
164         char *service_id;
165         GSList *list;
166
167         if (cleanup == TRUE)
168                 goto free_only;
169
170         connman_info("Removing service configuration %s",
171                                                 config_service->ident);
172
173         protected_services = g_slist_remove(protected_services,
174                                                 config_service);
175
176         for (list = config_service->service_identifiers; list != NULL;
177                                                         list = list->next) {
178                 service_id = list->data;
179
180                 service = __connman_service_lookup_from_ident(service_id);
181                 if (service != NULL) {
182                         __connman_service_set_immutable(service, FALSE);
183                         __connman_service_set_config(service, NULL, NULL);
184                         __connman_service_remove(service);
185
186                         /*
187                          * Ethernet service cannot be removed by
188                          * __connman_service_remove() so reset the ipconfig
189                          * here.
190                          */
191                         if (connman_service_get_type(service) ==
192                                                 CONNMAN_SERVICE_TYPE_ETHERNET) {
193                                 __connman_service_disconnect(service);
194                                 __connman_service_reset_ipconfig(service,
195                                         CONNMAN_IPCONFIG_TYPE_IPV4, NULL, NULL);
196                                 __connman_service_reset_ipconfig(service,
197                                         CONNMAN_IPCONFIG_TYPE_IPV6, NULL, NULL);
198                                 __connman_service_set_ignore(service, TRUE);
199
200                                 /*
201                                  * After these operations, user needs to
202                                  * reconnect ethernet cable to get IP
203                                  * address.
204                                  */
205                         }
206                 }
207
208                 if (__connman_storage_remove_service(service_id) == FALSE)
209                         DBG("Could not remove all files for service %s",
210                                                                 service_id);
211         }
212
213 free_only:
214         g_free(config_service->ident);
215         g_free(config_service->type);
216         g_free(config_service->name);
217         g_free(config_service->ssid);
218         g_free(config_service->eap);
219         g_free(config_service->identity);
220         g_free(config_service->ca_cert_file);
221         g_free(config_service->client_cert_file);
222         g_free(config_service->private_key_file);
223         g_free(config_service->private_key_passphrase);
224         g_free(config_service->private_key_passphrase_type);
225         g_free(config_service->phase2);
226         g_free(config_service->passphrase);
227         g_free(config_service->ipv4_address);
228         g_free(config_service->ipv4_gateway);
229         g_free(config_service->ipv4_netmask);
230         g_free(config_service->ipv6_address);
231         g_free(config_service->ipv6_gateway);
232         g_free(config_service->ipv6_privacy);
233         g_free(config_service->mac);
234         g_strfreev(config_service->nameservers);
235         g_strfreev(config_service->search_domains);
236         g_strfreev(config_service->timeservers);
237         g_free(config_service->domain_name);
238         g_slist_free_full(config_service->service_identifiers, g_free);
239         g_free(config_service->config_ident);
240         g_free(config_service->config_entry);
241         g_free(config_service);
242 }
243
244 static void check_keys(GKeyFile *keyfile, const char *group,
245                         const char **possible_keys)
246 {
247         char **avail_keys;
248         gsize nb_avail_keys, i, j;
249
250         avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
251         if (avail_keys == NULL)
252                 return;
253
254         /*
255          * For each key in the configuration file,
256          * verify it is understood by connman
257          */
258         for (i = 0 ; i < nb_avail_keys; i++) {
259                 for (j = 0; possible_keys[j] ; j++)
260                         if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
261                                 break;
262
263                 if (possible_keys[j] == NULL)
264                         connman_warn("Unknown configuration key %s in [%s]",
265                                         avail_keys[i], group);
266         }
267
268         g_strfreev(avail_keys);
269 }
270
271 static connman_bool_t
272 is_protected_service(struct connman_config_service *service)
273 {
274         GSList *list;
275
276         DBG("ident %s", service->ident);
277
278         for (list = protected_services; list; list = list->next) {
279                 struct connman_config_service *s = list->data;
280
281                 if (g_strcmp0(s->type, service->type) != 0)
282                         continue;
283
284                 if (s->ssid == NULL || service->ssid == NULL)
285                         continue;
286
287                 if (s->ssid_len != service->ssid_len)
288                         continue;
289
290                 if (g_strcmp0(service->type, "wifi") == 0 &&
291                         strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
292                         return TRUE;
293                 }
294         }
295
296         return FALSE;
297 }
298
299 static int check_family(const char *address, int expected_family)
300 {
301         int family;
302         int err = 0;
303
304         family = connman_inet_check_ipaddress(address);
305         if (family < 0) {
306                 DBG("Cannot get address family of %s (%d/%s)", address,
307                         family, gai_strerror(family));
308                 err = -EINVAL;
309                 goto out;
310         }
311
312         switch (family) {
313         case AF_INET:
314                 if (expected_family != AF_INET) {
315                         DBG("Wrong type address %s, expecting IPv4", address);
316                         err = -EINVAL;
317                         goto out;
318                 }
319                 break;
320         case AF_INET6:
321                 if (expected_family != AF_INET6) {
322                         DBG("Wrong type address %s, expecting IPv6", address);
323                         err = -EINVAL;
324                         goto out;
325                 }
326                 break;
327         default:
328                 DBG("Unsupported address family %d", family);
329                 err = -EINVAL;
330                 goto out;
331         }
332
333 out:
334         return err;
335 }
336
337 static int parse_address(const char *address_str, int address_family,
338                         char **address, char **netmask, char **gateway)
339 {
340         char *addr_str, *mask_str, *gw_str;
341         int err = 0;
342         char **route;
343
344         route = g_strsplit(address_str, "/", 0);
345         if (route == NULL)
346                 return -EINVAL;
347
348         addr_str = route[0];
349         if (addr_str == NULL || addr_str[0] == '\0') {
350                 err = -EINVAL;
351                 goto out;
352         }
353
354         if ((err = check_family(addr_str, address_family)) < 0)
355                 goto out;
356
357         mask_str = route[1];
358         if (mask_str == NULL || mask_str[0] == '\0') {
359                 err = -EINVAL;
360                 goto out;
361         }
362
363         gw_str = route[2];
364         if (gw_str == NULL || gw_str[0] == '\0') {
365                 err = -EINVAL;
366                 goto out;
367         }
368
369         if ((err = check_family(gw_str, address_family)) < 0)
370                 goto out;
371
372         g_free(*address);
373         *address = g_strdup(addr_str);
374
375         g_free(*netmask);
376         *netmask = g_strdup(mask_str);
377
378         g_free(*gateway);
379         *gateway = g_strdup(gw_str);
380
381         DBG("address %s/%s via %s", *address, *netmask, *gateway);
382
383 out:
384         g_strfreev(route);
385
386         return err;
387 }
388
389 static connman_bool_t load_service_generic(GKeyFile *keyfile,
390                         const char *group, struct connman_config *config,
391                         struct connman_config_service *service)
392 {
393         char *str, *mask;
394         char **strlist;
395         gsize length;
396
397         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv4, NULL);
398         if (str != NULL) {
399                 mask = NULL;
400
401                 if (parse_address(str, AF_INET, &service->ipv4_address,
402                                         &mask, &service->ipv4_gateway) < 0) {
403                         connman_warn("Invalid format for IPv4 address %s",
404                                                                         str);
405                         g_free(str);
406                         goto err;
407                 }
408
409                 if (g_strrstr(mask, ".") == NULL) {
410                         /* We have netmask length */
411                         in_addr_t addr;
412                         struct in_addr netmask_in;
413                         unsigned char prefix_len = 32;
414                         char *ptr;
415                         long int value = strtol(mask, &ptr, 10);
416
417                         if (ptr != mask && *ptr == '\0' && value <= 32)
418                                 prefix_len = value;
419
420                         addr = 0xffffffff << (32 - prefix_len);
421                         netmask_in.s_addr = htonl(addr);
422                         service->ipv4_netmask =
423                                 g_strdup(inet_ntoa(netmask_in));
424
425                         g_free(mask);
426                 } else
427                         service->ipv4_netmask = mask;
428
429                 g_free(str);
430         }
431
432         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv6, NULL);
433         if (str != NULL) {
434                 long int value;
435                 char *ptr;
436
437                 mask = NULL;
438
439                 if (parse_address(str, AF_INET6, &service->ipv6_address,
440                                         &mask, &service->ipv6_gateway) < 0) {
441                         connman_warn("Invalid format for IPv6 address %s",
442                                                                         str);
443                         g_free(str);
444                         goto err;
445                 }
446
447                 value = strtol(mask, &ptr, 10);
448                 if (ptr != mask && *ptr == '\0' && value <= 128)
449                         service->ipv6_prefix_length = value;
450                 else
451                         service->ipv6_prefix_length = 128;
452
453                 g_free(mask);
454                 g_free(str);
455         }
456
457         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv6_PRIVACY,
458                                                                         NULL);
459         if (str != NULL) {
460                 g_free(service->ipv6_privacy);
461                 service->ipv6_privacy = str;
462         }
463
464         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_MAC, NULL);
465         if (str != NULL) {
466                 g_free(service->mac);
467                 service->mac = str;
468         }
469
470         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_DOMAIN, NULL);
471         if (str != NULL) {
472                 g_free(service->domain_name);
473                 service->domain_name = str;
474         }
475
476         strlist = g_key_file_get_string_list(keyfile, group,
477                                         SERVICE_KEY_NAMESERVERS,
478                                         &length, NULL);
479         if (strlist != NULL) {
480                 if (length != 0) {
481                         g_strfreev(service->nameservers);
482                         service->nameservers = strlist;
483                 } else
484                         g_strfreev(strlist);
485         }
486
487         strlist = g_key_file_get_string_list(keyfile, group,
488                                         SERVICE_KEY_SEARCH_DOMAINS,
489                                         &length, NULL);
490         if (strlist != NULL) {
491                 if (length != 0) {
492                         g_strfreev(service->search_domains);
493                         service->search_domains = strlist;
494                 } else
495                         g_strfreev(strlist);
496         }
497
498         strlist = g_key_file_get_string_list(keyfile, group,
499                                         SERVICE_KEY_TIMESERVERS,
500                                         &length, NULL);
501         if (strlist != NULL) {
502                 if (length != 0) {
503                         g_strfreev(service->timeservers);
504                         service->timeservers = strlist;
505                 } else
506                         g_strfreev(strlist);
507         }
508
509         return TRUE;
510
511 err:
512         g_free(service->ident);
513         g_free(service->type);
514         g_free(service->ipv4_address);
515         g_free(service->ipv4_netmask);
516         g_free(service->ipv4_gateway);
517         g_free(service->ipv6_address);
518         g_free(service->ipv6_gateway);
519         g_free(service->mac);
520         g_free(service);
521
522         return FALSE;
523 }
524
525 static connman_bool_t load_service(GKeyFile *keyfile, const char *group,
526                                                 struct connman_config *config)
527 {
528         struct connman_config_service *service;
529         const char *ident;
530         char *str, *hex_ssid;
531         gboolean service_created = FALSE;
532
533         /* Strip off "service_" prefix */
534         ident = group + 8;
535
536         if (strlen(ident) < 1)
537                 return FALSE;
538
539         /* Verify that provided keys are good */
540         check_keys(keyfile, group, service_possible_keys);
541
542         service = g_hash_table_lookup(config->service_table, ident);
543         if (service == NULL) {
544                 service = g_try_new0(struct connman_config_service, 1);
545                 if (service == NULL)
546                         return FALSE;
547
548                 service->ident = g_strdup(ident);
549
550                 service_created = TRUE;
551         }
552
553         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
554         if (str != NULL) {
555                 g_free(service->type);
556                 service->type = str;
557         } else {
558                 DBG("Type of the configured service is missing for group %s",
559                                                                         group);
560                 goto err;
561         }
562
563         if (load_service_generic(keyfile, group, config, service) == FALSE)
564                 return FALSE;
565
566         if (g_strcmp0(str, "ethernet") == 0) {
567                 service->config_ident = g_strdup(config->ident);
568                 service->config_entry = g_strdup_printf("service_%s",
569                                                         service->ident);
570
571                 g_hash_table_insert(config->service_table, service->ident,
572                                                                 service);
573                 return 0;
574         }
575
576         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
577         if (str != NULL) {
578                 g_free(service->name);
579                 service->name = str;
580         }
581
582         hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
583                                          NULL);
584         if (hex_ssid != NULL) {
585                 char *ssid;
586                 unsigned int i, j = 0, hex;
587                 size_t hex_ssid_len = strlen(hex_ssid);
588
589                 ssid = g_try_malloc0(hex_ssid_len / 2);
590                 if (ssid == NULL) {
591                         g_free(hex_ssid);
592                         goto err;
593                 }
594
595                 for (i = 0; i < hex_ssid_len; i += 2) {
596                         if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
597                                 connman_warn("Invalid SSID %s", hex_ssid);
598                                 g_free(ssid);
599                                 g_free(hex_ssid);
600                                 goto err;
601                         }
602                         ssid[j++] = hex;
603                 }
604
605                 g_free(hex_ssid);
606
607                 g_free(service->ssid);
608                 service->ssid = ssid;
609                 service->ssid_len = hex_ssid_len / 2;
610         } else if (service->name != NULL) {
611                 char *ssid;
612                 unsigned int ssid_len;
613
614                 ssid_len = strlen(service->name);
615                 ssid = g_try_malloc0(ssid_len);
616                 if (ssid == NULL)
617                         goto err;
618
619                 memcpy(ssid, service->name, ssid_len);
620                 g_free(service->ssid);
621                 service->ssid = ssid;
622                 service->ssid_len = ssid_len;
623         }
624
625         if (is_protected_service(service) == TRUE) {
626                 connman_error("Trying to provision a protected service");
627                 goto err;
628         }
629
630         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
631         if (str != NULL) {
632                 g_free(service->eap);
633                 service->eap = str;
634         }
635
636         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
637         if (str != NULL) {
638                 g_free(service->ca_cert_file);
639                 service->ca_cert_file = str;
640         }
641
642         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
643         if (str != NULL) {
644                 g_free(service->client_cert_file);
645                 service->client_cert_file = str;
646         }
647
648         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
649         if (str != NULL) {
650                 g_free(service->private_key_file);
651                 service->private_key_file = str;
652         }
653
654         str = g_key_file_get_string(keyfile, group,
655                                                 SERVICE_KEY_PRV_KEY_PASS, NULL);
656         if (str != NULL) {
657                 g_free(service->private_key_passphrase);
658                 service->private_key_passphrase = str;
659         }
660
661         str = g_key_file_get_string(keyfile, group,
662                                         SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
663         if (str != NULL) {
664                 g_free(service->private_key_passphrase_type);
665                 service->private_key_passphrase_type = str;
666         }
667
668         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
669         if (str != NULL) {
670                 g_free(service->identity);
671                 service->identity = str;
672         }
673
674         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
675         if (str != NULL) {
676                 g_free(service->phase2);
677                 service->phase2 = str;
678         }
679
680         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
681                                         NULL);
682         if (str != NULL) {
683                 g_free(service->passphrase);
684                 service->passphrase = str;
685         }
686
687         service->config_ident = g_strdup(config->ident);
688         service->config_entry = g_strdup_printf("service_%s", service->ident);
689
690         service->hidden = g_key_file_get_boolean(keyfile, group,
691                                                 SERVICE_KEY_HIDDEN, NULL);
692
693         if (service_created)
694                 g_hash_table_insert(config->service_table, service->ident,
695                                         service);
696
697         if (config->protected == TRUE)
698                 protected_services =
699                         g_slist_prepend(protected_services, service);
700
701         connman_info("Adding service configuration %s", service->ident);
702
703         return TRUE;
704
705 err:
706         if (service_created == TRUE) {
707                 g_free(service->ident);
708                 g_free(service->type);
709                 g_free(service->name);
710                 g_free(service->ssid);
711                 g_free(service);
712         }
713
714         return FALSE;
715 }
716
717 static connman_bool_t load_service_from_keyfile(GKeyFile *keyfile,
718                                                 struct connman_config *config)
719 {
720         connman_bool_t found = FALSE;
721         char **groups;
722         int i;
723
724         groups = g_key_file_get_groups(keyfile, NULL);
725
726         for (i = 0; groups[i] != NULL; i++) {
727                 if (g_str_has_prefix(groups[i], "service_") == FALSE)
728                         continue;
729                 if (load_service(keyfile, groups[i], config) == TRUE)
730                         found = TRUE;
731         }
732
733         g_strfreev(groups);
734
735         return found;
736 }
737
738 static int load_config(struct connman_config *config)
739 {
740         GError *error = NULL;
741         gboolean protected;
742         GKeyFile *keyfile;
743         char *str;
744
745         DBG("config %p", config);
746
747         keyfile = __connman_storage_load_config(config->ident);
748         if (keyfile == NULL)
749                 return -EIO;
750
751         /* Verify keys validity of the global section */
752         check_keys(keyfile, "global", config_possible_keys);
753
754         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
755         if (str != NULL) {
756                 g_free(config->name);
757                 config->name = str;
758         }
759
760         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
761         if (str != NULL) {
762                 g_free(config->description);
763                 config->description = str;
764         }
765
766         protected = g_key_file_get_boolean(keyfile, "global",
767                                         CONFIG_KEY_PROT, &error);
768         if (error == NULL)
769                 config->protected = protected;
770         else
771                 config->protected = TRUE;
772         g_clear_error(&error);
773
774         if (load_service_from_keyfile(keyfile, config) == FALSE)
775                 connman_warn("Config file %s/%s.config does not contain any "
776                         "configuration that can be provisioned!",
777                         STORAGEDIR, config->ident);
778
779         g_key_file_free(keyfile);
780
781         return 0;
782 }
783
784 static struct connman_config *create_config(const char *ident)
785 {
786         struct connman_config *config;
787
788         DBG("ident %s", ident);
789
790         if (g_hash_table_lookup(config_table, ident) != NULL)
791                 return NULL;
792
793         config = g_try_new0(struct connman_config, 1);
794         if (config == NULL)
795                 return NULL;
796
797         config->ident = g_strdup(ident);
798
799         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
800                                                 NULL, unregister_service);
801
802         g_hash_table_insert(config_table, config->ident, config);
803
804         connman_info("Adding configuration %s", config->ident);
805
806         return config;
807 }
808
809 static connman_bool_t validate_ident(const char *ident)
810 {
811         unsigned int i;
812
813         if (ident == NULL)
814                 return FALSE;
815
816         for (i = 0; i < strlen(ident); i++)
817                 if (g_ascii_isprint(ident[i]) == FALSE)
818                         return FALSE;
819
820         return TRUE;
821 }
822
823 static int read_configs(void)
824 {
825         GDir *dir;
826
827         DBG("");
828
829         dir = g_dir_open(STORAGEDIR, 0, NULL);
830         if (dir != NULL) {
831                 const gchar *file;
832
833                 while ((file = g_dir_read_name(dir)) != NULL) {
834                         GString *str;
835                         gchar *ident;
836
837                         if (g_str_has_suffix(file, ".config") == FALSE)
838                                 continue;
839
840                         ident = g_strrstr(file, ".config");
841                         if (ident == NULL)
842                                 continue;
843
844                         str = g_string_new_len(file, ident - file);
845                         if (str == NULL)
846                                 continue;
847
848                         ident = g_string_free(str, FALSE);
849
850                         if (validate_ident(ident) == TRUE) {
851                                 struct connman_config *config;
852
853                                 config = create_config(ident);
854                                 if (config != NULL)
855                                         load_config(config);
856                         } else {
857                                 connman_error("Invalid config ident %s", ident);
858                         }
859                         g_free(ident);
860                 }
861
862                 g_dir_close(dir);
863         }
864
865         return 0;
866 }
867
868 static void config_notify_handler(struct inotify_event *event,
869                                         const char *ident)
870 {
871         char *ext;
872
873         if (ident == NULL)
874                 return;
875
876         if (g_str_has_suffix(ident, ".config") == FALSE)
877                 return;
878
879         ext = g_strrstr(ident, ".config");
880         if (ext == NULL)
881                 return;
882
883         *ext = '\0';
884
885         if (validate_ident(ident) == FALSE) {
886                 connman_error("Invalid config ident %s", ident);
887                 return;
888         }
889
890         if (event->mask & IN_CREATE)
891                 create_config(ident);
892
893         if (event->mask & IN_MODIFY) {
894                 struct connman_config *config;
895
896                 config = g_hash_table_lookup(config_table, ident);
897                 if (config != NULL) {
898                         int ret;
899
900                         g_hash_table_remove_all(config->service_table);
901                         load_config(config);
902                         ret = __connman_service_provision_changed(ident);
903                         if (ret > 0) {
904                                 /*
905                                  * Re-scan the config file for any
906                                  * changes
907                                  */
908                                 g_hash_table_remove_all(config->service_table);
909                                 load_config(config);
910                                 __connman_service_provision_changed(ident);
911                         }
912                 }
913         }
914
915         if (event->mask & IN_DELETE)
916                 g_hash_table_remove(config_table, ident);
917 }
918
919 int __connman_config_init(void)
920 {
921         DBG("");
922
923         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
924                                                 NULL, unregister_config);
925
926         connman_inotify_register(STORAGEDIR, config_notify_handler);
927
928         return read_configs();
929 }
930
931 void __connman_config_cleanup(void)
932 {
933         DBG("");
934
935         cleanup = TRUE;
936
937         connman_inotify_unregister(STORAGEDIR, config_notify_handler);
938
939         g_hash_table_destroy(config_table);
940         config_table = NULL;
941
942         cleanup = FALSE;
943 }
944
945 static char *config_pem_fsid(const char *pem_file)
946 {
947         struct statfs buf;
948         unsigned *fsid = (unsigned *) &buf.f_fsid;
949         unsigned long long fsid64;
950
951         if (pem_file == NULL)
952                 return NULL;
953
954         if (statfs(pem_file, &buf) < 0) {
955                 connman_error("statfs error %s for %s",
956                                                 strerror(errno), pem_file);
957                 return NULL;
958         }
959
960         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
961
962         return g_strdup_printf("%llx", fsid64);
963 }
964
965 static void provision_service_wifi(gpointer key,
966                                 struct connman_config_service *config,
967                                 struct connman_service *service,
968                                 struct connman_network *network,
969                                 const void *ssid, unsigned int ssid_len)
970 {
971         if (config->eap != NULL)
972                 __connman_service_set_string(service, "EAP", config->eap);
973
974         if (config->identity != NULL)
975                 __connman_service_set_string(service, "Identity",
976                                                         config->identity);
977
978         if (config->ca_cert_file != NULL)
979                 __connman_service_set_string(service, "CACertFile",
980                                                         config->ca_cert_file);
981
982         if (config->client_cert_file != NULL)
983                 __connman_service_set_string(service, "ClientCertFile",
984                                                 config->client_cert_file);
985
986         if (config->private_key_file != NULL)
987                 __connman_service_set_string(service, "PrivateKeyFile",
988                                                 config->private_key_file);
989
990         if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
991                                         config->private_key_file != NULL) {
992                 char *fsid;
993
994                 fsid = config_pem_fsid(config->private_key_file);
995                 if (fsid == NULL)
996                         return;
997
998                 g_free(config->private_key_passphrase);
999                 config->private_key_passphrase = fsid;
1000         }
1001
1002         if (config->private_key_passphrase != NULL) {
1003                 __connman_service_set_string(service, "PrivateKeyPassphrase",
1004                                                 config->private_key_passphrase);
1005                 /*
1006                  * TODO: Support for PEAP with both identity and key passwd.
1007                  * In that case, we should check if both of them are found
1008                  * from the config file. If not, we should not set the
1009                  * service passphrase in order for the UI to request for an
1010                  * additional passphrase.
1011                  */
1012         }
1013
1014         if (config->phase2 != NULL)
1015                 __connman_service_set_string(service, "Phase2", config->phase2);
1016
1017         if (config->passphrase != NULL)
1018                 __connman_service_set_string(service, "Passphrase", config->passphrase);
1019
1020         if (config->hidden == TRUE)
1021                 __connman_service_set_hidden(service);
1022 }
1023
1024 static void provision_service(gpointer key, gpointer value,
1025                                                         gpointer user_data)
1026 {
1027         struct connman_service *service = user_data;
1028         struct connman_config_service *config = value;
1029         struct connman_network *network;
1030         const void *service_id;
1031         enum connman_service_type type;
1032         const void *ssid;
1033         unsigned int ssid_len;
1034
1035         type = connman_service_get_type(service);
1036         if (type == CONNMAN_SERVICE_TYPE_WIFI &&
1037                                 g_strcmp0(config->type, "wifi") != 0)
1038                 return;
1039
1040         if (type == CONNMAN_SERVICE_TYPE_ETHERNET &&
1041                                 g_strcmp0(config->type, "ethernet") != 0)
1042                 return;
1043
1044         DBG("service %p ident %s", service,
1045                                         __connman_service_get_ident(service));
1046
1047         network = __connman_service_get_network(service);
1048         if (network == NULL) {
1049                 connman_error("Service has no network set");
1050                 return;
1051         }
1052
1053         DBG("network %p ident %s", network,
1054                                 connman_network_get_identifier(network));
1055
1056         if (config->mac != NULL) {
1057                 struct connman_device *device;
1058                 const char *device_addr;
1059
1060                 device = connman_network_get_device(network);
1061                 if (device == NULL) {
1062                         connman_error("Network device is missing");
1063                         return;
1064                 }
1065
1066                 device_addr = connman_device_get_string(device, "Address");
1067
1068                 DBG("wants %s has %s", config->mac, device_addr);
1069
1070                 if (g_ascii_strcasecmp(device_addr, config->mac) != 0)
1071                         return;
1072         }
1073
1074         if (g_strcmp0(config->type, "wifi") == 0 &&
1075                                 type == CONNMAN_SERVICE_TYPE_WIFI) {
1076                 ssid = connman_network_get_blob(network, "WiFi.SSID",
1077                                                 &ssid_len);
1078                 if (ssid == NULL) {
1079                         connman_error("Network SSID not set");
1080                         return;
1081                 }
1082
1083                 if (config->ssid == NULL || ssid_len != config->ssid_len)
1084                         return;
1085
1086                 if (memcmp(config->ssid, ssid, ssid_len) != 0)
1087                         return;
1088         }
1089
1090         if (config->ipv6_address != NULL) {
1091                 struct connman_ipaddress *address;
1092
1093                 if (config->ipv6_prefix_length == 0 ||
1094                                         config->ipv6_gateway == NULL) {
1095                         DBG("IPv6 prefix or gateway missing");
1096                         return;
1097                 }
1098
1099                 address = connman_ipaddress_alloc(AF_INET6);
1100                 if (address == NULL)
1101                         return;
1102
1103                 connman_ipaddress_set_ipv6(address, config->ipv6_address,
1104                                         config->ipv6_prefix_length,
1105                                         config->ipv6_gateway);
1106
1107                 connman_network_set_ipv6_method(network,
1108                                                 CONNMAN_IPCONFIG_METHOD_FIXED);
1109
1110                 if (connman_network_set_ipaddress(network, address) < 0)
1111                         DBG("Unable to set IPv6 address to network %p",
1112                                                                 network);
1113
1114                 connman_ipaddress_free(address);
1115         }
1116
1117         if (config->ipv6_privacy != NULL) {
1118                 struct connman_ipconfig *ipconfig;
1119
1120                 ipconfig = __connman_service_get_ip6config(service);
1121                 if (ipconfig != NULL)
1122                         __connman_ipconfig_ipv6_set_privacy(ipconfig,
1123                                                         config->ipv6_privacy);
1124         }
1125
1126         if (config->ipv4_address != NULL) {
1127                 struct connman_ipaddress *address;
1128
1129                 if (config->ipv4_netmask == 0 ||
1130                                         config->ipv4_gateway == NULL) {
1131                         DBG("IPv4 netmask or gateway missing");
1132                         return;
1133                 }
1134
1135                 address = connman_ipaddress_alloc(AF_INET);
1136                 if (address == NULL)
1137                         return;
1138
1139                 connman_ipaddress_set_ipv4(address, config->ipv4_address,
1140                                         config->ipv4_netmask,
1141                                         config->ipv4_gateway);
1142
1143                 connman_network_set_ipv4_method(network,
1144                                                 CONNMAN_IPCONFIG_METHOD_FIXED);
1145
1146                 if (connman_network_set_ipaddress(network, address) < 0)
1147                         DBG("Unable to set IPv4 address to network %p",
1148                                                                 network);
1149
1150                 connman_ipaddress_free(address);
1151         }
1152
1153         __connman_service_disconnect(service);
1154
1155         service_id = __connman_service_get_ident(service);
1156         config->service_identifiers =
1157                 g_slist_prepend(config->service_identifiers,
1158                                 g_strdup(service_id));
1159
1160         __connman_service_set_immutable(service, TRUE);
1161
1162         __connman_service_set_favorite_delayed(service, TRUE, TRUE);
1163
1164         __connman_service_set_config(service, config->config_ident,
1165                                                 config->config_entry);
1166
1167         if (config->domain_name != NULL)
1168                 __connman_service_set_domainname(service, config->domain_name);
1169
1170         if (config->nameservers != NULL) {
1171                 int i;
1172
1173                 __connman_service_nameserver_clear(service);
1174
1175                 for (i = 0; config->nameservers[i] != NULL; i++) {
1176                         __connman_service_nameserver_append(service,
1177                                                 config->nameservers[i], FALSE);
1178                 }
1179         }
1180
1181         if (config->search_domains != NULL)
1182                 __connman_service_set_search_domains(service,
1183                                                 config->search_domains);
1184
1185         if (config->timeservers != NULL)
1186                 __connman_service_set_timeservers(service,
1187                                                 config->timeservers);
1188
1189         if (g_strcmp0(config->type, "wifi") == 0 &&
1190                                 type == CONNMAN_SERVICE_TYPE_WIFI) {
1191                 provision_service_wifi(key, config, service, network,
1192                                                         ssid, ssid_len);
1193         } else
1194                 __connman_service_connect(service);
1195
1196         __connman_service_mark_dirty();
1197
1198         __connman_service_save(service);
1199
1200         __connman_service_auto_connect();
1201 }
1202
1203 int __connman_config_provision_service(struct connman_service *service)
1204 {
1205         enum connman_service_type type;
1206         GHashTableIter iter;
1207         gpointer value, key;
1208
1209         /* For now only WiFi and Ethernet services are supported */
1210         type = connman_service_get_type(service);
1211
1212         DBG("service %p type %d", service, type);
1213
1214         if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1215                                         type != CONNMAN_SERVICE_TYPE_ETHERNET)
1216                 return -ENOSYS;
1217
1218         g_hash_table_iter_init(&iter, config_table);
1219
1220         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1221                 struct connman_config *config = value;
1222
1223                 g_hash_table_foreach(config->service_table,
1224                                                 provision_service, service);
1225         }
1226
1227         return 0;
1228 }
1229
1230 int __connman_config_provision_service_ident(struct connman_service *service,
1231                         const char *ident, const char *file, const char *entry)
1232 {
1233         enum connman_service_type type;
1234         struct connman_config *config;
1235         int ret = 0;
1236
1237         /* For now only WiFi and Ethernet services are supported */
1238         type = connman_service_get_type(service);
1239
1240         DBG("service %p type %d", service, type);
1241
1242         if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1243                                         type != CONNMAN_SERVICE_TYPE_ETHERNET)
1244                 return -ENOSYS;
1245
1246         config = g_hash_table_lookup(config_table, ident);
1247         if (config != NULL) {
1248                 GHashTableIter iter;
1249                 gpointer value, key;
1250                 gboolean found = FALSE;
1251
1252                 g_hash_table_iter_init(&iter, config->service_table);
1253
1254                 /*
1255                  * Check if we need to remove individual service if it
1256                  * is missing from config file.
1257                  */
1258                 if (file != NULL && entry != NULL) {
1259                         while (g_hash_table_iter_next(&iter, &key,
1260                                                         &value) == TRUE) {
1261                                 struct connman_config_service *config_service;
1262
1263                                 config_service = value;
1264
1265                                 if (g_strcmp0(config_service->config_ident,
1266                                                                 file) != 0)
1267                                         continue;
1268
1269                                 if (g_strcmp0(config_service->config_entry,
1270                                                                 entry) != 0)
1271                                         continue;
1272
1273                                 found = TRUE;
1274                                 break;
1275                         }
1276
1277                         DBG("found %d ident %s file %s entry %s", found, ident,
1278                                                                 file, entry);
1279
1280                         if (found == FALSE) {
1281                                 /*
1282                                  * The entry+8 will skip "service_" prefix
1283                                  */
1284                                 g_hash_table_remove(config->service_table,
1285                                                 entry + 8);
1286                                 ret = 1;
1287                         }
1288                 }
1289
1290                 g_hash_table_foreach(config->service_table,
1291                                                 provision_service, service);
1292         }
1293
1294         return ret;
1295 }
1296
1297 struct connman_config_entry **connman_config_get_entries(const char *type)
1298 {
1299         GHashTableIter iter_file, iter_config;
1300         gpointer value, key;
1301         struct connman_config_entry **entries = NULL;
1302         int i = 0, count;
1303
1304         g_hash_table_iter_init(&iter_file, config_table);
1305         while (g_hash_table_iter_next(&iter_file, &key, &value) == TRUE) {
1306                 struct connman_config *config_file = value;
1307
1308                 count = g_hash_table_size(config_file->service_table);
1309
1310                 entries = g_try_realloc(entries, (i + count + 1) *
1311                                         sizeof(struct connman_config_entry *));
1312                 if (entries == NULL)
1313                         return NULL;
1314
1315                 g_hash_table_iter_init(&iter_config,
1316                                                 config_file->service_table);
1317                 while (g_hash_table_iter_next(&iter_config, &key,
1318                                                         &value) == TRUE) {
1319                         struct connman_config_service *config = value;
1320
1321                         if (type != NULL &&
1322                                         g_strcmp0(config->type, type) != 0)
1323                                 continue;
1324
1325                         entries[i] = g_try_new0(struct connman_config_entry,
1326                                                 1);
1327                         if (entries[i] == NULL)
1328                                 goto cleanup;
1329
1330                         entries[i]->ident = g_strdup(config->ident);
1331                         entries[i]->name = g_strdup(config->name);
1332                         entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
1333                         if (entries[i]->ssid == NULL)
1334                                 goto cleanup;
1335
1336                         memcpy(entries[i]->ssid, config->ssid,
1337                                                         config->ssid_len);
1338                         entries[i]->ssid_len = config->ssid_len;
1339                         entries[i]->hidden = config->hidden;
1340
1341                         i++;
1342                 }
1343         }
1344
1345         if (entries != NULL) {
1346                 entries = g_try_realloc(entries, (i + 1) *
1347                                         sizeof(struct connman_config_entry *));
1348                 if (entries == NULL)
1349                         return NULL;
1350
1351                 entries[i] = NULL;
1352
1353                 DBG("%d provisioned AP found", i);
1354         }
1355
1356         return entries;
1357
1358 cleanup:
1359         connman_config_free_entries(entries);
1360         return NULL;
1361 }
1362
1363 void connman_config_free_entries(struct connman_config_entry **entries)
1364 {
1365         int i;
1366
1367         if (entries == NULL)
1368                 return;
1369
1370         for (i = 0; entries[i]; i++) {
1371                 g_free(entries[i]->ident);
1372                 g_free(entries[i]->name);
1373                 g_free(entries[i]->ssid);
1374                 g_free(entries[i]);
1375         }
1376
1377         g_free(entries);
1378         return;
1379 }