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