config: Remove superfluous error code
[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 int load_config(struct connman_config *config)
718 {
719         GKeyFile *keyfile;
720         GError *error = NULL;
721         gsize length;
722         char **groups;
723         char *str;
724         gboolean protected, found = FALSE;
725         int i;
726
727         DBG("config %p", config);
728
729         keyfile = __connman_storage_load_config(config->ident);
730         if (keyfile == NULL)
731                 return -EIO;
732
733         /* Verify keys validity of the global section */
734         check_keys(keyfile, "global", config_possible_keys);
735
736         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
737         if (str != NULL) {
738                 g_free(config->name);
739                 config->name = str;
740         }
741
742         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
743         if (str != NULL) {
744                 g_free(config->description);
745                 config->description = str;
746         }
747
748         protected = g_key_file_get_boolean(keyfile, "global",
749                                         CONFIG_KEY_PROT, &error);
750         if (error == NULL)
751                 config->protected = protected;
752         else
753                 config->protected = TRUE;
754         g_clear_error(&error);
755
756         groups = g_key_file_get_groups(keyfile, &length);
757
758         for (i = 0; groups[i] != NULL; i++) {
759                 if (g_str_has_prefix(groups[i], "service_") == TRUE) {
760                         if (load_service(keyfile, groups[i], config) == TRUE)
761                                 found = TRUE;
762                 }
763         }
764
765         if (found == FALSE)
766                 connman_warn("Config file %s/%s.config does not contain any "
767                         "configuration that can be provisioned!",
768                         STORAGEDIR, config->ident);
769
770         g_strfreev(groups);
771
772         g_key_file_free(keyfile);
773
774         return 0;
775 }
776
777 static struct connman_config *create_config(const char *ident)
778 {
779         struct connman_config *config;
780
781         DBG("ident %s", ident);
782
783         if (g_hash_table_lookup(config_table, ident) != NULL)
784                 return NULL;
785
786         config = g_try_new0(struct connman_config, 1);
787         if (config == NULL)
788                 return NULL;
789
790         config->ident = g_strdup(ident);
791
792         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
793                                                 NULL, unregister_service);
794
795         g_hash_table_insert(config_table, config->ident, config);
796
797         connman_info("Adding configuration %s", config->ident);
798
799         return config;
800 }
801
802 static connman_bool_t validate_ident(const char *ident)
803 {
804         unsigned int i;
805
806         if (ident == NULL)
807                 return FALSE;
808
809         for (i = 0; i < strlen(ident); i++)
810                 if (g_ascii_isprint(ident[i]) == FALSE)
811                         return FALSE;
812
813         return TRUE;
814 }
815
816 static int read_configs(void)
817 {
818         GDir *dir;
819
820         DBG("");
821
822         dir = g_dir_open(STORAGEDIR, 0, NULL);
823         if (dir != NULL) {
824                 const gchar *file;
825
826                 while ((file = g_dir_read_name(dir)) != NULL) {
827                         GString *str;
828                         gchar *ident;
829
830                         if (g_str_has_suffix(file, ".config") == FALSE)
831                                 continue;
832
833                         ident = g_strrstr(file, ".config");
834                         if (ident == NULL)
835                                 continue;
836
837                         str = g_string_new_len(file, ident - file);
838                         if (str == NULL)
839                                 continue;
840
841                         ident = g_string_free(str, FALSE);
842
843                         if (validate_ident(ident) == TRUE) {
844                                 struct connman_config *config;
845
846                                 config = create_config(ident);
847                                 if (config != NULL)
848                                         load_config(config);
849                         } else {
850                                 connman_error("Invalid config ident %s", ident);
851                         }
852                         g_free(ident);
853                 }
854
855                 g_dir_close(dir);
856         }
857
858         return 0;
859 }
860
861 static void config_notify_handler(struct inotify_event *event,
862                                         const char *ident)
863 {
864         char *ext;
865
866         if (ident == NULL)
867                 return;
868
869         if (g_str_has_suffix(ident, ".config") == FALSE)
870                 return;
871
872         ext = g_strrstr(ident, ".config");
873         if (ext == NULL)
874                 return;
875
876         *ext = '\0';
877
878         if (validate_ident(ident) == FALSE) {
879                 connman_error("Invalid config ident %s", ident);
880                 return;
881         }
882
883         if (event->mask & IN_CREATE)
884                 create_config(ident);
885
886         if (event->mask & IN_MODIFY) {
887                 struct connman_config *config;
888
889                 config = g_hash_table_lookup(config_table, ident);
890                 if (config != NULL) {
891                         int ret;
892
893                         g_hash_table_remove_all(config->service_table);
894                         load_config(config);
895                         ret = __connman_service_provision_changed(ident);
896                         if (ret > 0) {
897                                 /*
898                                  * Re-scan the config file for any
899                                  * changes
900                                  */
901                                 g_hash_table_remove_all(config->service_table);
902                                 load_config(config);
903                                 __connman_service_provision_changed(ident);
904                         }
905                 }
906         }
907
908         if (event->mask & IN_DELETE)
909                 g_hash_table_remove(config_table, ident);
910 }
911
912 int __connman_config_init(void)
913 {
914         DBG("");
915
916         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
917                                                 NULL, unregister_config);
918
919         connman_inotify_register(STORAGEDIR, config_notify_handler);
920
921         return read_configs();
922 }
923
924 void __connman_config_cleanup(void)
925 {
926         DBG("");
927
928         cleanup = TRUE;
929
930         connman_inotify_unregister(STORAGEDIR, config_notify_handler);
931
932         g_hash_table_destroy(config_table);
933         config_table = NULL;
934
935         cleanup = FALSE;
936 }
937
938 static char *config_pem_fsid(const char *pem_file)
939 {
940         struct statfs buf;
941         unsigned *fsid = (unsigned *) &buf.f_fsid;
942         unsigned long long fsid64;
943
944         if (pem_file == NULL)
945                 return NULL;
946
947         if (statfs(pem_file, &buf) < 0) {
948                 connman_error("statfs error %s for %s",
949                                                 strerror(errno), pem_file);
950                 return NULL;
951         }
952
953         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
954
955         return g_strdup_printf("%llx", fsid64);
956 }
957
958 static void provision_service_wifi(gpointer key,
959                                 struct connman_config_service *config,
960                                 struct connman_service *service,
961                                 struct connman_network *network,
962                                 const void *ssid, unsigned int ssid_len)
963 {
964         if (config->eap != NULL)
965                 __connman_service_set_string(service, "EAP", config->eap);
966
967         if (config->identity != NULL)
968                 __connman_service_set_string(service, "Identity",
969                                                         config->identity);
970
971         if (config->ca_cert_file != NULL)
972                 __connman_service_set_string(service, "CACertFile",
973                                                         config->ca_cert_file);
974
975         if (config->client_cert_file != NULL)
976                 __connman_service_set_string(service, "ClientCertFile",
977                                                 config->client_cert_file);
978
979         if (config->private_key_file != NULL)
980                 __connman_service_set_string(service, "PrivateKeyFile",
981                                                 config->private_key_file);
982
983         if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
984                                         config->private_key_file != NULL) {
985                 char *fsid;
986
987                 fsid = config_pem_fsid(config->private_key_file);
988                 if (fsid == NULL)
989                         return;
990
991                 g_free(config->private_key_passphrase);
992                 config->private_key_passphrase = fsid;
993         }
994
995         if (config->private_key_passphrase != NULL) {
996                 __connman_service_set_string(service, "PrivateKeyPassphrase",
997                                                 config->private_key_passphrase);
998                 /*
999                  * TODO: Support for PEAP with both identity and key passwd.
1000                  * In that case, we should check if both of them are found
1001                  * from the config file. If not, we should not set the
1002                  * service passphrase in order for the UI to request for an
1003                  * additional passphrase.
1004                  */
1005         }
1006
1007         if (config->phase2 != NULL)
1008                 __connman_service_set_string(service, "Phase2", config->phase2);
1009
1010         if (config->passphrase != NULL)
1011                 __connman_service_set_string(service, "Passphrase", config->passphrase);
1012
1013         if (config->hidden == TRUE)
1014                 __connman_service_set_hidden(service);
1015 }
1016
1017 static void provision_service(gpointer key, gpointer value,
1018                                                         gpointer user_data)
1019 {
1020         struct connman_service *service = user_data;
1021         struct connman_config_service *config = value;
1022         struct connman_network *network;
1023         const void *service_id;
1024         enum connman_service_type type;
1025         const void *ssid;
1026         unsigned int ssid_len;
1027
1028         type = connman_service_get_type(service);
1029         if (type == CONNMAN_SERVICE_TYPE_WIFI &&
1030                                 g_strcmp0(config->type, "wifi") != 0)
1031                 return;
1032
1033         if (type == CONNMAN_SERVICE_TYPE_ETHERNET &&
1034                                 g_strcmp0(config->type, "ethernet") != 0)
1035                 return;
1036
1037         DBG("service %p ident %s", service,
1038                                         __connman_service_get_ident(service));
1039
1040         network = __connman_service_get_network(service);
1041         if (network == NULL) {
1042                 connman_error("Service has no network set");
1043                 return;
1044         }
1045
1046         DBG("network %p ident %s", network,
1047                                 connman_network_get_identifier(network));
1048
1049         if (config->mac != NULL) {
1050                 struct connman_device *device;
1051                 const char *device_addr;
1052
1053                 device = connman_network_get_device(network);
1054                 if (device == NULL) {
1055                         connman_error("Network device is missing");
1056                         return;
1057                 }
1058
1059                 device_addr = connman_device_get_string(device, "Address");
1060
1061                 DBG("wants %s has %s", config->mac, device_addr);
1062
1063                 if (g_ascii_strcasecmp(device_addr, config->mac) != 0)
1064                         return;
1065         }
1066
1067         if (g_strcmp0(config->type, "wifi") == 0 &&
1068                                 type == CONNMAN_SERVICE_TYPE_WIFI) {
1069                 ssid = connman_network_get_blob(network, "WiFi.SSID",
1070                                                 &ssid_len);
1071                 if (ssid == NULL) {
1072                         connman_error("Network SSID not set");
1073                         return;
1074                 }
1075
1076                 if (config->ssid == NULL || ssid_len != config->ssid_len)
1077                         return;
1078
1079                 if (memcmp(config->ssid, ssid, ssid_len) != 0)
1080                         return;
1081         }
1082
1083         if (config->ipv6_address != NULL) {
1084                 struct connman_ipaddress *address;
1085
1086                 if (config->ipv6_prefix_length == 0 ||
1087                                         config->ipv6_gateway == NULL) {
1088                         DBG("IPv6 prefix or gateway missing");
1089                         return;
1090                 }
1091
1092                 address = connman_ipaddress_alloc(AF_INET6);
1093                 if (address == NULL)
1094                         return;
1095
1096                 connman_ipaddress_set_ipv6(address, config->ipv6_address,
1097                                         config->ipv6_prefix_length,
1098                                         config->ipv6_gateway);
1099
1100                 connman_network_set_ipv6_method(network,
1101                                                 CONNMAN_IPCONFIG_METHOD_FIXED);
1102
1103                 if (connman_network_set_ipaddress(network, address) < 0)
1104                         DBG("Unable to set IPv6 address to network %p",
1105                                                                 network);
1106
1107                 connman_ipaddress_free(address);
1108         }
1109
1110         if (config->ipv6_privacy != NULL) {
1111                 struct connman_ipconfig *ipconfig;
1112
1113                 ipconfig = __connman_service_get_ip6config(service);
1114                 if (ipconfig != NULL)
1115                         __connman_ipconfig_ipv6_set_privacy(ipconfig,
1116                                                         config->ipv6_privacy);
1117         }
1118
1119         if (config->ipv4_address != NULL) {
1120                 struct connman_ipaddress *address;
1121
1122                 if (config->ipv4_netmask == 0 ||
1123                                         config->ipv4_gateway == NULL) {
1124                         DBG("IPv4 netmask or gateway missing");
1125                         return;
1126                 }
1127
1128                 address = connman_ipaddress_alloc(AF_INET);
1129                 if (address == NULL)
1130                         return;
1131
1132                 connman_ipaddress_set_ipv4(address, config->ipv4_address,
1133                                         config->ipv4_netmask,
1134                                         config->ipv4_gateway);
1135
1136                 connman_network_set_ipv4_method(network,
1137                                                 CONNMAN_IPCONFIG_METHOD_FIXED);
1138
1139                 if (connman_network_set_ipaddress(network, address) < 0)
1140                         DBG("Unable to set IPv4 address to network %p",
1141                                                                 network);
1142
1143                 connman_ipaddress_free(address);
1144         }
1145
1146         __connman_service_disconnect(service);
1147
1148         service_id = __connman_service_get_ident(service);
1149         config->service_identifiers =
1150                 g_slist_prepend(config->service_identifiers,
1151                                 g_strdup(service_id));
1152
1153         __connman_service_set_immutable(service, TRUE);
1154
1155         __connman_service_set_favorite_delayed(service, TRUE, TRUE);
1156
1157         __connman_service_set_config(service, config->config_ident,
1158                                                 config->config_entry);
1159
1160         if (config->domain_name != NULL)
1161                 __connman_service_set_domainname(service, config->domain_name);
1162
1163         if (config->nameservers != NULL) {
1164                 int i;
1165
1166                 __connman_service_nameserver_clear(service);
1167
1168                 for (i = 0; config->nameservers[i] != NULL; i++) {
1169                         __connman_service_nameserver_append(service,
1170                                                 config->nameservers[i], FALSE);
1171                 }
1172         }
1173
1174         if (config->search_domains != NULL)
1175                 __connman_service_set_search_domains(service,
1176                                                 config->search_domains);
1177
1178         if (config->timeservers != NULL)
1179                 __connman_service_set_timeservers(service,
1180                                                 config->timeservers);
1181
1182         if (g_strcmp0(config->type, "wifi") == 0 &&
1183                                 type == CONNMAN_SERVICE_TYPE_WIFI) {
1184                 provision_service_wifi(key, config, service, network,
1185                                                         ssid, ssid_len);
1186         } else
1187                 __connman_service_connect(service);
1188
1189         __connman_service_mark_dirty();
1190
1191         __connman_service_save(service);
1192
1193         __connman_service_auto_connect();
1194 }
1195
1196 int __connman_config_provision_service(struct connman_service *service)
1197 {
1198         enum connman_service_type type;
1199         GHashTableIter iter;
1200         gpointer value, key;
1201
1202         /* For now only WiFi and Ethernet services are supported */
1203         type = connman_service_get_type(service);
1204
1205         DBG("service %p type %d", service, type);
1206
1207         if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1208                                         type != CONNMAN_SERVICE_TYPE_ETHERNET)
1209                 return -ENOSYS;
1210
1211         g_hash_table_iter_init(&iter, config_table);
1212
1213         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1214                 struct connman_config *config = value;
1215
1216                 g_hash_table_foreach(config->service_table,
1217                                                 provision_service, service);
1218         }
1219
1220         return 0;
1221 }
1222
1223 int __connman_config_provision_service_ident(struct connman_service *service,
1224                         const char *ident, const char *file, const char *entry)
1225 {
1226         enum connman_service_type type;
1227         struct connman_config *config;
1228         int ret = 0;
1229
1230         /* For now only WiFi and Ethernet services are supported */
1231         type = connman_service_get_type(service);
1232
1233         DBG("service %p type %d", service, type);
1234
1235         if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1236                                         type != CONNMAN_SERVICE_TYPE_ETHERNET)
1237                 return -ENOSYS;
1238
1239         config = g_hash_table_lookup(config_table, ident);
1240         if (config != NULL) {
1241                 GHashTableIter iter;
1242                 gpointer value, key;
1243                 gboolean found = FALSE;
1244
1245                 g_hash_table_iter_init(&iter, config->service_table);
1246
1247                 /*
1248                  * Check if we need to remove individual service if it
1249                  * is missing from config file.
1250                  */
1251                 if (file != NULL && entry != NULL) {
1252                         while (g_hash_table_iter_next(&iter, &key,
1253                                                         &value) == TRUE) {
1254                                 struct connman_config_service *config_service;
1255
1256                                 config_service = value;
1257
1258                                 if (g_strcmp0(config_service->config_ident,
1259                                                                 file) != 0)
1260                                         continue;
1261
1262                                 if (g_strcmp0(config_service->config_entry,
1263                                                                 entry) != 0)
1264                                         continue;
1265
1266                                 found = TRUE;
1267                                 break;
1268                         }
1269
1270                         DBG("found %d ident %s file %s entry %s", found, ident,
1271                                                                 file, entry);
1272
1273                         if (found == FALSE) {
1274                                 /*
1275                                  * The entry+8 will skip "service_" prefix
1276                                  */
1277                                 g_hash_table_remove(config->service_table,
1278                                                 entry + 8);
1279                                 ret = 1;
1280                         }
1281                 }
1282
1283                 g_hash_table_foreach(config->service_table,
1284                                                 provision_service, service);
1285         }
1286
1287         return ret;
1288 }
1289
1290 struct connman_config_entry **connman_config_get_entries(const char *type)
1291 {
1292         GHashTableIter iter_file, iter_config;
1293         gpointer value, key;
1294         struct connman_config_entry **entries = NULL;
1295         int i = 0, count;
1296
1297         g_hash_table_iter_init(&iter_file, config_table);
1298         while (g_hash_table_iter_next(&iter_file, &key, &value) == TRUE) {
1299                 struct connman_config *config_file = value;
1300
1301                 count = g_hash_table_size(config_file->service_table);
1302
1303                 entries = g_try_realloc(entries, (i + count + 1) *
1304                                         sizeof(struct connman_config_entry *));
1305                 if (entries == NULL)
1306                         return NULL;
1307
1308                 g_hash_table_iter_init(&iter_config,
1309                                                 config_file->service_table);
1310                 while (g_hash_table_iter_next(&iter_config, &key,
1311                                                         &value) == TRUE) {
1312                         struct connman_config_service *config = value;
1313
1314                         if (type != NULL &&
1315                                         g_strcmp0(config->type, type) != 0)
1316                                 continue;
1317
1318                         entries[i] = g_try_new0(struct connman_config_entry,
1319                                                 1);
1320                         if (entries[i] == NULL)
1321                                 goto cleanup;
1322
1323                         entries[i]->ident = g_strdup(config->ident);
1324                         entries[i]->name = g_strdup(config->name);
1325                         entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
1326                         if (entries[i]->ssid == NULL)
1327                                 goto cleanup;
1328
1329                         memcpy(entries[i]->ssid, config->ssid,
1330                                                         config->ssid_len);
1331                         entries[i]->ssid_len = config->ssid_len;
1332                         entries[i]->hidden = config->hidden;
1333
1334                         i++;
1335                 }
1336         }
1337
1338         if (entries != NULL) {
1339                 entries = g_try_realloc(entries, (i + 1) *
1340                                         sizeof(struct connman_config_entry *));
1341                 if (entries == NULL)
1342                         return NULL;
1343
1344                 entries[i] = NULL;
1345
1346                 DBG("%d provisioned AP found", i);
1347         }
1348
1349         return entries;
1350
1351 cleanup:
1352         connman_config_free_entries(entries);
1353         return NULL;
1354 }
1355
1356 void connman_config_free_entries(struct connman_config_entry **entries)
1357 {
1358         int i;
1359
1360         if (entries == NULL)
1361                 return;
1362
1363         for (i = 0; entries[i]; i++) {
1364                 g_free(entries[i]->ident);
1365                 g_free(entries[i]->name);
1366                 g_free(entries[i]->ssid);
1367                 g_free(entries[i]);
1368         }
1369
1370         g_free(entries);
1371         return;
1372 }