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