config: Allow user to specify how IP address is used
[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 check_address(char *address_str, char **address)
396 {
397         if (g_ascii_strcasecmp(address_str, "auto") == 0 ||
398                         g_ascii_strcasecmp(address_str, "dhcp") == 0 ||
399                         g_ascii_strcasecmp(address_str, "off") == 0) {
400                 *address = address_str;
401                 return FALSE;
402         }
403
404         return TRUE;
405 }
406
407 static connman_bool_t load_service_generic(GKeyFile *keyfile,
408                         const char *group, struct connman_config *config,
409                         struct connman_config_service *service)
410 {
411         char *str, *mask;
412         char **strlist;
413         gsize length;
414
415         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv4, NULL);
416         if (str != NULL && check_address(str, &service->ipv4_address) == TRUE) {
417                 mask = NULL;
418
419                 if (parse_address(str, AF_INET, &service->ipv4_address,
420                                         &mask, &service->ipv4_gateway) < 0) {
421                         connman_warn("Invalid format for IPv4 address %s",
422                                                                         str);
423                         g_free(str);
424                         goto err;
425                 }
426
427                 if (g_strrstr(mask, ".") == NULL) {
428                         /* We have netmask length */
429                         in_addr_t addr;
430                         struct in_addr netmask_in;
431                         unsigned char prefix_len = 32;
432                         char *ptr;
433                         long int value = strtol(mask, &ptr, 10);
434
435                         if (ptr != mask && *ptr == '\0' && value <= 32)
436                                 prefix_len = value;
437
438                         addr = 0xffffffff << (32 - prefix_len);
439                         netmask_in.s_addr = htonl(addr);
440                         service->ipv4_netmask =
441                                 g_strdup(inet_ntoa(netmask_in));
442
443                         g_free(mask);
444                 } else
445                         service->ipv4_netmask = mask;
446
447                 g_free(str);
448         }
449
450         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv6, NULL);
451         if (str != NULL && check_address(str, &service->ipv6_address) == TRUE) {
452                 long int value;
453                 char *ptr;
454
455                 mask = NULL;
456
457                 if (parse_address(str, AF_INET6, &service->ipv6_address,
458                                         &mask, &service->ipv6_gateway) < 0) {
459                         connman_warn("Invalid format for IPv6 address %s",
460                                                                         str);
461                         g_free(str);
462                         goto err;
463                 }
464
465                 value = strtol(mask, &ptr, 10);
466                 if (ptr != mask && *ptr == '\0' && value <= 128)
467                         service->ipv6_prefix_length = value;
468                 else
469                         service->ipv6_prefix_length = 128;
470
471                 g_free(mask);
472                 g_free(str);
473         }
474
475         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv6_PRIVACY,
476                                                                         NULL);
477         if (str != NULL) {
478                 g_free(service->ipv6_privacy);
479                 service->ipv6_privacy = str;
480         }
481
482         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_MAC, NULL);
483         if (str != NULL) {
484                 g_free(service->mac);
485                 service->mac = str;
486         }
487
488         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_DOMAIN, NULL);
489         if (str != NULL) {
490                 g_free(service->domain_name);
491                 service->domain_name = str;
492         }
493
494         strlist = g_key_file_get_string_list(keyfile, group,
495                                         SERVICE_KEY_NAMESERVERS,
496                                         &length, NULL);
497         if (strlist != NULL) {
498                 if (length != 0) {
499                         g_strfreev(service->nameservers);
500                         service->nameservers = strlist;
501                 } else
502                         g_strfreev(strlist);
503         }
504
505         strlist = g_key_file_get_string_list(keyfile, group,
506                                         SERVICE_KEY_SEARCH_DOMAINS,
507                                         &length, NULL);
508         if (strlist != NULL) {
509                 if (length != 0) {
510                         g_strfreev(service->search_domains);
511                         service->search_domains = strlist;
512                 } else
513                         g_strfreev(strlist);
514         }
515
516         strlist = g_key_file_get_string_list(keyfile, group,
517                                         SERVICE_KEY_TIMESERVERS,
518                                         &length, NULL);
519         if (strlist != NULL) {
520                 if (length != 0) {
521                         g_strfreev(service->timeservers);
522                         service->timeservers = strlist;
523                 } else
524                         g_strfreev(strlist);
525         }
526
527         return TRUE;
528
529 err:
530         g_free(service->ident);
531         g_free(service->type);
532         g_free(service->ipv4_address);
533         g_free(service->ipv4_netmask);
534         g_free(service->ipv4_gateway);
535         g_free(service->ipv6_address);
536         g_free(service->ipv6_gateway);
537         g_free(service->mac);
538         g_free(service);
539
540         return FALSE;
541 }
542
543 static connman_bool_t load_service(GKeyFile *keyfile, const char *group,
544                                                 struct connman_config *config)
545 {
546         struct connman_config_service *service;
547         const char *ident;
548         char *str, *hex_ssid;
549         gboolean service_created = FALSE;
550
551         /* Strip off "service_" prefix */
552         ident = group + 8;
553
554         if (strlen(ident) < 1)
555                 return FALSE;
556
557         /* Verify that provided keys are good */
558         check_keys(keyfile, group, service_possible_keys);
559
560         service = g_hash_table_lookup(config->service_table, ident);
561         if (service == NULL) {
562                 service = g_try_new0(struct connman_config_service, 1);
563                 if (service == NULL)
564                         return FALSE;
565
566                 service->ident = g_strdup(ident);
567
568                 service_created = TRUE;
569         }
570
571         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
572         if (str != NULL) {
573                 g_free(service->type);
574                 service->type = str;
575         } else {
576                 DBG("Type of the configured service is missing for group %s",
577                                                                         group);
578                 goto err;
579         }
580
581         if (load_service_generic(keyfile, group, config, service) == FALSE)
582                 return FALSE;
583
584         if (g_strcmp0(str, "ethernet") == 0) {
585                 service->config_ident = g_strdup(config->ident);
586                 service->config_entry = g_strdup_printf("service_%s",
587                                                         service->ident);
588
589                 g_hash_table_insert(config->service_table, service->ident,
590                                                                 service);
591                 return 0;
592         }
593
594         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
595         if (str != NULL) {
596                 g_free(service->name);
597                 service->name = str;
598         }
599
600         hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
601                                          NULL);
602         if (hex_ssid != NULL) {
603                 char *ssid;
604                 unsigned int i, j = 0, hex;
605                 size_t hex_ssid_len = strlen(hex_ssid);
606
607                 ssid = g_try_malloc0(hex_ssid_len / 2);
608                 if (ssid == NULL) {
609                         g_free(hex_ssid);
610                         goto err;
611                 }
612
613                 for (i = 0; i < hex_ssid_len; i += 2) {
614                         if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
615                                 connman_warn("Invalid SSID %s", hex_ssid);
616                                 g_free(ssid);
617                                 g_free(hex_ssid);
618                                 goto err;
619                         }
620                         ssid[j++] = hex;
621                 }
622
623                 g_free(hex_ssid);
624
625                 g_free(service->ssid);
626                 service->ssid = ssid;
627                 service->ssid_len = hex_ssid_len / 2;
628         } else if (service->name != NULL) {
629                 char *ssid;
630                 unsigned int ssid_len;
631
632                 ssid_len = strlen(service->name);
633                 ssid = g_try_malloc0(ssid_len);
634                 if (ssid == NULL)
635                         goto err;
636
637                 memcpy(ssid, service->name, ssid_len);
638                 g_free(service->ssid);
639                 service->ssid = ssid;
640                 service->ssid_len = ssid_len;
641         }
642
643         if (is_protected_service(service) == TRUE) {
644                 connman_error("Trying to provision a protected service");
645                 goto err;
646         }
647
648         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
649         if (str != NULL) {
650                 g_free(service->eap);
651                 service->eap = str;
652         }
653
654         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
655         if (str != NULL) {
656                 g_free(service->ca_cert_file);
657                 service->ca_cert_file = str;
658         }
659
660         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
661         if (str != NULL) {
662                 g_free(service->client_cert_file);
663                 service->client_cert_file = str;
664         }
665
666         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
667         if (str != NULL) {
668                 g_free(service->private_key_file);
669                 service->private_key_file = str;
670         }
671
672         str = g_key_file_get_string(keyfile, group,
673                                                 SERVICE_KEY_PRV_KEY_PASS, NULL);
674         if (str != NULL) {
675                 g_free(service->private_key_passphrase);
676                 service->private_key_passphrase = str;
677         }
678
679         str = g_key_file_get_string(keyfile, group,
680                                         SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
681         if (str != NULL) {
682                 g_free(service->private_key_passphrase_type);
683                 service->private_key_passphrase_type = str;
684         }
685
686         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
687         if (str != NULL) {
688                 g_free(service->identity);
689                 service->identity = str;
690         }
691
692         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
693         if (str != NULL) {
694                 g_free(service->phase2);
695                 service->phase2 = str;
696         }
697
698         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
699                                         NULL);
700         if (str != NULL) {
701                 g_free(service->passphrase);
702                 service->passphrase = str;
703         }
704
705         service->config_ident = g_strdup(config->ident);
706         service->config_entry = g_strdup_printf("service_%s", service->ident);
707
708         service->hidden = g_key_file_get_boolean(keyfile, group,
709                                                 SERVICE_KEY_HIDDEN, NULL);
710
711         if (service_created)
712                 g_hash_table_insert(config->service_table, service->ident,
713                                         service);
714
715         if (config->protected == TRUE)
716                 protected_services =
717                         g_slist_prepend(protected_services, service);
718
719         connman_info("Adding service configuration %s", service->ident);
720
721         return TRUE;
722
723 err:
724         if (service_created == TRUE) {
725                 g_free(service->ident);
726                 g_free(service->type);
727                 g_free(service->name);
728                 g_free(service->ssid);
729                 g_free(service);
730         }
731
732         return FALSE;
733 }
734
735 static connman_bool_t load_service_from_keyfile(GKeyFile *keyfile,
736                                                 struct connman_config *config)
737 {
738         connman_bool_t found = FALSE;
739         char **groups;
740         int i;
741
742         groups = g_key_file_get_groups(keyfile, NULL);
743
744         for (i = 0; groups[i] != NULL; i++) {
745                 if (g_str_has_prefix(groups[i], "service_") == FALSE)
746                         continue;
747                 if (load_service(keyfile, groups[i], config) == TRUE)
748                         found = TRUE;
749         }
750
751         g_strfreev(groups);
752
753         return found;
754 }
755
756 static int load_config(struct connman_config *config)
757 {
758         GError *error = NULL;
759         gboolean protected;
760         GKeyFile *keyfile;
761         char *str;
762
763         DBG("config %p", config);
764
765         keyfile = __connman_storage_load_config(config->ident);
766         if (keyfile == NULL)
767                 return -EIO;
768
769         /* Verify keys validity of the global section */
770         check_keys(keyfile, "global", config_possible_keys);
771
772         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
773         if (str != NULL) {
774                 g_free(config->name);
775                 config->name = str;
776         }
777
778         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
779         if (str != NULL) {
780                 g_free(config->description);
781                 config->description = str;
782         }
783
784         protected = g_key_file_get_boolean(keyfile, "global",
785                                         CONFIG_KEY_PROT, &error);
786         if (error == NULL)
787                 config->protected = protected;
788         else
789                 config->protected = TRUE;
790         g_clear_error(&error);
791
792         if (load_service_from_keyfile(keyfile, config) == FALSE)
793                 connman_warn("Config file %s/%s.config does not contain any "
794                         "configuration that can be provisioned!",
795                         STORAGEDIR, config->ident);
796
797         g_key_file_free(keyfile);
798
799         return 0;
800 }
801
802 static struct connman_config *create_config(const char *ident)
803 {
804         struct connman_config *config;
805
806         DBG("ident %s", ident);
807
808         if (g_hash_table_lookup(config_table, ident) != NULL)
809                 return NULL;
810
811         config = g_try_new0(struct connman_config, 1);
812         if (config == NULL)
813                 return NULL;
814
815         config->ident = g_strdup(ident);
816
817         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
818                                                 NULL, unregister_service);
819
820         g_hash_table_insert(config_table, config->ident, config);
821
822         connman_info("Adding configuration %s", config->ident);
823
824         return config;
825 }
826
827 static connman_bool_t validate_ident(const char *ident)
828 {
829         unsigned int i;
830
831         if (ident == NULL)
832                 return FALSE;
833
834         for (i = 0; i < strlen(ident); i++)
835                 if (g_ascii_isprint(ident[i]) == FALSE)
836                         return FALSE;
837
838         return TRUE;
839 }
840
841 static int read_configs(void)
842 {
843         GDir *dir;
844
845         DBG("");
846
847         dir = g_dir_open(STORAGEDIR, 0, NULL);
848         if (dir != NULL) {
849                 const gchar *file;
850
851                 while ((file = g_dir_read_name(dir)) != NULL) {
852                         GString *str;
853                         gchar *ident;
854
855                         if (g_str_has_suffix(file, ".config") == FALSE)
856                                 continue;
857
858                         ident = g_strrstr(file, ".config");
859                         if (ident == NULL)
860                                 continue;
861
862                         str = g_string_new_len(file, ident - file);
863                         if (str == NULL)
864                                 continue;
865
866                         ident = g_string_free(str, FALSE);
867
868                         if (validate_ident(ident) == TRUE) {
869                                 struct connman_config *config;
870
871                                 config = create_config(ident);
872                                 if (config != NULL)
873                                         load_config(config);
874                         } else {
875                                 connman_error("Invalid config ident %s", ident);
876                         }
877                         g_free(ident);
878                 }
879
880                 g_dir_close(dir);
881         }
882
883         return 0;
884 }
885
886 static void config_notify_handler(struct inotify_event *event,
887                                         const char *ident)
888 {
889         char *ext;
890
891         if (ident == NULL)
892                 return;
893
894         if (g_str_has_suffix(ident, ".config") == FALSE)
895                 return;
896
897         ext = g_strrstr(ident, ".config");
898         if (ext == NULL)
899                 return;
900
901         *ext = '\0';
902
903         if (validate_ident(ident) == FALSE) {
904                 connman_error("Invalid config ident %s", ident);
905                 return;
906         }
907
908         if (event->mask & IN_CREATE)
909                 create_config(ident);
910
911         if (event->mask & IN_MODIFY) {
912                 struct connman_config *config;
913
914                 config = g_hash_table_lookup(config_table, ident);
915                 if (config != NULL) {
916                         int ret;
917
918                         g_hash_table_remove_all(config->service_table);
919                         load_config(config);
920                         ret = __connman_service_provision_changed(ident);
921                         if (ret > 0) {
922                                 /*
923                                  * Re-scan the config file for any
924                                  * changes
925                                  */
926                                 g_hash_table_remove_all(config->service_table);
927                                 load_config(config);
928                                 __connman_service_provision_changed(ident);
929                         }
930                 }
931         }
932
933         if (event->mask & IN_DELETE)
934                 g_hash_table_remove(config_table, ident);
935 }
936
937 int __connman_config_init(void)
938 {
939         DBG("");
940
941         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
942                                                 NULL, unregister_config);
943
944         connman_inotify_register(STORAGEDIR, config_notify_handler);
945
946         return read_configs();
947 }
948
949 void __connman_config_cleanup(void)
950 {
951         DBG("");
952
953         cleanup = TRUE;
954
955         connman_inotify_unregister(STORAGEDIR, config_notify_handler);
956
957         g_hash_table_destroy(config_table);
958         config_table = NULL;
959
960         cleanup = FALSE;
961 }
962
963 static char *config_pem_fsid(const char *pem_file)
964 {
965         struct statfs buf;
966         unsigned *fsid = (unsigned *) &buf.f_fsid;
967         unsigned long long fsid64;
968
969         if (pem_file == NULL)
970                 return NULL;
971
972         if (statfs(pem_file, &buf) < 0) {
973                 connman_error("statfs error %s for %s",
974                                                 strerror(errno), pem_file);
975                 return NULL;
976         }
977
978         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
979
980         return g_strdup_printf("%llx", fsid64);
981 }
982
983 static void provision_service_wifi(gpointer key,
984                                 struct connman_config_service *config,
985                                 struct connman_service *service,
986                                 struct connman_network *network,
987                                 const void *ssid, unsigned int ssid_len)
988 {
989         if (config->eap != NULL)
990                 __connman_service_set_string(service, "EAP", config->eap);
991
992         if (config->identity != NULL)
993                 __connman_service_set_string(service, "Identity",
994                                                         config->identity);
995
996         if (config->ca_cert_file != NULL)
997                 __connman_service_set_string(service, "CACertFile",
998                                                         config->ca_cert_file);
999
1000         if (config->client_cert_file != NULL)
1001                 __connman_service_set_string(service, "ClientCertFile",
1002                                                 config->client_cert_file);
1003
1004         if (config->private_key_file != NULL)
1005                 __connman_service_set_string(service, "PrivateKeyFile",
1006                                                 config->private_key_file);
1007
1008         if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
1009                                         config->private_key_file != NULL) {
1010                 char *fsid;
1011
1012                 fsid = config_pem_fsid(config->private_key_file);
1013                 if (fsid == NULL)
1014                         return;
1015
1016                 g_free(config->private_key_passphrase);
1017                 config->private_key_passphrase = fsid;
1018         }
1019
1020         if (config->private_key_passphrase != NULL) {
1021                 __connman_service_set_string(service, "PrivateKeyPassphrase",
1022                                                 config->private_key_passphrase);
1023                 /*
1024                  * TODO: Support for PEAP with both identity and key passwd.
1025                  * In that case, we should check if both of them are found
1026                  * from the config file. If not, we should not set the
1027                  * service passphrase in order for the UI to request for an
1028                  * additional passphrase.
1029                  */
1030         }
1031
1032         if (config->phase2 != NULL)
1033                 __connman_service_set_string(service, "Phase2", config->phase2);
1034
1035         if (config->passphrase != NULL)
1036                 __connman_service_set_string(service, "Passphrase", config->passphrase);
1037
1038         if (config->hidden == TRUE)
1039                 __connman_service_set_hidden(service);
1040 }
1041
1042 struct connect_virtual {
1043         struct connman_service *service;
1044         const char *vfile;
1045 };
1046
1047 static gboolean remove_virtual_config(gpointer user_data)
1048 {
1049         struct connect_virtual *virtual = user_data;
1050
1051         __connman_service_connect(virtual->service);
1052         g_hash_table_remove(config_table, virtual->vfile);
1053
1054         g_free(virtual);
1055
1056         return FALSE;
1057 }
1058
1059 static void provision_service(gpointer key, gpointer value,
1060                                                         gpointer user_data)
1061 {
1062         struct connman_service *service = user_data;
1063         struct connman_config_service *config = value;
1064         struct connman_network *network;
1065         const void *service_id;
1066         enum connman_service_type type;
1067         const void *ssid;
1068         unsigned int ssid_len;
1069
1070         type = connman_service_get_type(service);
1071         if (type == CONNMAN_SERVICE_TYPE_WIFI &&
1072                                 g_strcmp0(config->type, "wifi") != 0)
1073                 return;
1074
1075         if (type == CONNMAN_SERVICE_TYPE_ETHERNET &&
1076                                 g_strcmp0(config->type, "ethernet") != 0)
1077                 return;
1078
1079         DBG("service %p ident %s", service,
1080                                         __connman_service_get_ident(service));
1081
1082         network = __connman_service_get_network(service);
1083         if (network == NULL) {
1084                 connman_error("Service has no network set");
1085                 return;
1086         }
1087
1088         DBG("network %p ident %s", network,
1089                                 connman_network_get_identifier(network));
1090
1091         if (config->mac != NULL) {
1092                 struct connman_device *device;
1093                 const char *device_addr;
1094
1095                 device = connman_network_get_device(network);
1096                 if (device == NULL) {
1097                         connman_error("Network device is missing");
1098                         return;
1099                 }
1100
1101                 device_addr = connman_device_get_string(device, "Address");
1102
1103                 DBG("wants %s has %s", config->mac, device_addr);
1104
1105                 if (g_ascii_strcasecmp(device_addr, config->mac) != 0)
1106                         return;
1107         }
1108
1109         if (g_strcmp0(config->type, "wifi") == 0 &&
1110                                 type == CONNMAN_SERVICE_TYPE_WIFI) {
1111                 ssid = connman_network_get_blob(network, "WiFi.SSID",
1112                                                 &ssid_len);
1113                 if (ssid == NULL) {
1114                         connman_error("Network SSID not set");
1115                         return;
1116                 }
1117
1118                 if (config->ssid == NULL || ssid_len != config->ssid_len)
1119                         return;
1120
1121                 if (memcmp(config->ssid, ssid, ssid_len) != 0)
1122                         return;
1123         }
1124
1125         if (config->ipv6_address == NULL) {
1126                 connman_network_set_ipv6_method(network,
1127                                                 CONNMAN_IPCONFIG_METHOD_AUTO);
1128         } else if (g_ascii_strcasecmp(config->ipv6_address, "off") == 0) {
1129                 connman_network_set_ipv6_method(network,
1130                                                 CONNMAN_IPCONFIG_METHOD_OFF);
1131         } else if (g_ascii_strcasecmp(config->ipv6_address, "auto") == 0 ||
1132                         g_ascii_strcasecmp(config->ipv6_address, "dhcp") == 0) {
1133                 connman_network_set_ipv6_method(network,
1134                                                 CONNMAN_IPCONFIG_METHOD_AUTO);
1135         } else {
1136                 struct connman_ipaddress *address;
1137
1138                 if (config->ipv6_prefix_length == 0 ||
1139                                         config->ipv6_gateway == NULL) {
1140                         DBG("IPv6 prefix or gateway missing");
1141                         return;
1142                 }
1143
1144                 address = connman_ipaddress_alloc(AF_INET6);
1145                 if (address == NULL)
1146                         return;
1147
1148                 connman_ipaddress_set_ipv6(address, config->ipv6_address,
1149                                         config->ipv6_prefix_length,
1150                                         config->ipv6_gateway);
1151
1152                 connman_network_set_ipv6_method(network,
1153                                                 CONNMAN_IPCONFIG_METHOD_FIXED);
1154
1155                 if (connman_network_set_ipaddress(network, address) < 0)
1156                         DBG("Unable to set IPv6 address to network %p",
1157                                                                 network);
1158
1159                 connman_ipaddress_free(address);
1160         }
1161
1162         if (config->ipv6_privacy != NULL) {
1163                 struct connman_ipconfig *ipconfig;
1164
1165                 ipconfig = __connman_service_get_ip6config(service);
1166                 if (ipconfig != NULL)
1167                         __connman_ipconfig_ipv6_set_privacy(ipconfig,
1168                                                         config->ipv6_privacy);
1169         }
1170
1171         if (config->ipv4_address == NULL) {
1172                 connman_network_set_ipv4_method(network,
1173                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
1174         } else if (g_ascii_strcasecmp(config->ipv4_address, "off") == 0) {
1175                 connman_network_set_ipv4_method(network,
1176                                                 CONNMAN_IPCONFIG_METHOD_OFF);
1177         } else if (g_ascii_strcasecmp(config->ipv4_address, "auto") == 0 ||
1178                         g_ascii_strcasecmp(config->ipv4_address, "dhcp") == 0) {
1179                 connman_network_set_ipv4_method(network,
1180                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
1181         } else {
1182                 struct connman_ipaddress *address;
1183
1184                 if (config->ipv4_netmask == 0 ||
1185                                         config->ipv4_gateway == NULL) {
1186                         DBG("IPv4 netmask or gateway missing");
1187                         return;
1188                 }
1189
1190                 address = connman_ipaddress_alloc(AF_INET);
1191                 if (address == NULL)
1192                         return;
1193
1194                 connman_ipaddress_set_ipv4(address, config->ipv4_address,
1195                                         config->ipv4_netmask,
1196                                         config->ipv4_gateway);
1197
1198                 connman_network_set_ipv4_method(network,
1199                                                 CONNMAN_IPCONFIG_METHOD_FIXED);
1200
1201                 if (connman_network_set_ipaddress(network, address) < 0)
1202                         DBG("Unable to set IPv4 address to network %p",
1203                                                                 network);
1204
1205                 connman_ipaddress_free(address);
1206         }
1207
1208         __connman_service_disconnect(service);
1209
1210         service_id = __connman_service_get_ident(service);
1211         config->service_identifiers =
1212                 g_slist_prepend(config->service_identifiers,
1213                                 g_strdup(service_id));
1214
1215         if (config->virtual == FALSE)
1216                 __connman_service_set_immutable(service, TRUE);
1217
1218         __connman_service_set_favorite_delayed(service, TRUE, TRUE);
1219
1220         __connman_service_set_config(service, config->config_ident,
1221                                                 config->config_entry);
1222
1223         if (config->domain_name != NULL)
1224                 __connman_service_set_domainname(service, config->domain_name);
1225
1226         if (config->nameservers != NULL) {
1227                 int i;
1228
1229                 __connman_service_nameserver_clear(service);
1230
1231                 for (i = 0; config->nameservers[i] != NULL; i++) {
1232                         __connman_service_nameserver_append(service,
1233                                                 config->nameservers[i], FALSE);
1234                 }
1235         }
1236
1237         if (config->search_domains != NULL)
1238                 __connman_service_set_search_domains(service,
1239                                                 config->search_domains);
1240
1241         if (config->timeservers != NULL)
1242                 __connman_service_set_timeservers(service,
1243                                                 config->timeservers);
1244
1245         if (g_strcmp0(config->type, "wifi") == 0 &&
1246                                 type == CONNMAN_SERVICE_TYPE_WIFI) {
1247                 provision_service_wifi(key, config, service, network,
1248                                                         ssid, ssid_len);
1249         } else
1250                 __connman_service_connect(service);
1251
1252         __connman_service_mark_dirty();
1253
1254         __connman_service_save(service);
1255
1256         if (config->virtual == TRUE) {
1257                 struct connect_virtual *virtual;
1258
1259                 virtual = g_malloc0(sizeof(struct connect_virtual));
1260                 virtual->service = service;
1261                 virtual->vfile = config->virtual_file;
1262
1263                 g_timeout_add(0, remove_virtual_config, virtual);
1264         } else
1265                 __connman_service_auto_connect();
1266 }
1267
1268 int __connman_config_provision_service(struct connman_service *service)
1269 {
1270         enum connman_service_type type;
1271         GHashTableIter iter;
1272         gpointer value, key;
1273
1274         /* For now only WiFi and Ethernet services are supported */
1275         type = connman_service_get_type(service);
1276
1277         DBG("service %p type %d", service, type);
1278
1279         if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1280                                         type != CONNMAN_SERVICE_TYPE_ETHERNET)
1281                 return -ENOSYS;
1282
1283         g_hash_table_iter_init(&iter, config_table);
1284
1285         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1286                 struct connman_config *config = value;
1287
1288                 g_hash_table_foreach(config->service_table,
1289                                                 provision_service, service);
1290         }
1291
1292         return 0;
1293 }
1294
1295 int __connman_config_provision_service_ident(struct connman_service *service,
1296                         const char *ident, const char *file, const char *entry)
1297 {
1298         enum connman_service_type type;
1299         struct connman_config *config;
1300         int ret = 0;
1301
1302         /* For now only WiFi and Ethernet services are supported */
1303         type = connman_service_get_type(service);
1304
1305         DBG("service %p type %d", service, type);
1306
1307         if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1308                                         type != CONNMAN_SERVICE_TYPE_ETHERNET)
1309                 return -ENOSYS;
1310
1311         config = g_hash_table_lookup(config_table, ident);
1312         if (config != NULL) {
1313                 GHashTableIter iter;
1314                 gpointer value, key;
1315                 gboolean found = FALSE;
1316
1317                 g_hash_table_iter_init(&iter, config->service_table);
1318
1319                 /*
1320                  * Check if we need to remove individual service if it
1321                  * is missing from config file.
1322                  */
1323                 if (file != NULL && entry != NULL) {
1324                         while (g_hash_table_iter_next(&iter, &key,
1325                                                         &value) == TRUE) {
1326                                 struct connman_config_service *config_service;
1327
1328                                 config_service = value;
1329
1330                                 if (g_strcmp0(config_service->config_ident,
1331                                                                 file) != 0)
1332                                         continue;
1333
1334                                 if (g_strcmp0(config_service->config_entry,
1335                                                                 entry) != 0)
1336                                         continue;
1337
1338                                 found = TRUE;
1339                                 break;
1340                         }
1341
1342                         DBG("found %d ident %s file %s entry %s", found, ident,
1343                                                                 file, entry);
1344
1345                         if (found == FALSE) {
1346                                 /*
1347                                  * The entry+8 will skip "service_" prefix
1348                                  */
1349                                 g_hash_table_remove(config->service_table,
1350                                                 entry + 8);
1351                                 ret = 1;
1352                         }
1353                 }
1354
1355                 g_hash_table_foreach(config->service_table,
1356                                                 provision_service, service);
1357         }
1358
1359         return ret;
1360 }
1361
1362 static void generate_random_string(char *str, int length)
1363 {
1364         uint8_t val;
1365         int i;
1366
1367         memset(str, '\0', length);
1368
1369         for (i = 0; i < length-1; i++) {
1370                 do {
1371                         val = (uint8_t)(random() % 122);
1372                         if (val < 48)
1373                                 val += 48;
1374                 } while((val > 57 && val < 65) || (val > 90 && val < 97));
1375
1376                 str[i] = val;
1377         }
1378 }
1379
1380 int connman_config_provision_mutable_service(GKeyFile *keyfile)
1381 {
1382         struct connman_config_service *service_config;
1383         struct connman_config *config;
1384         char *vfile, *group;
1385         char rstr[11];
1386
1387         DBG("");
1388
1389         generate_random_string(rstr, 11);
1390
1391         vfile = g_strdup_printf("service_mutable_%s.config", rstr);
1392
1393         config = create_config(vfile);
1394         if (config == NULL)
1395                 return -ENOMEM;
1396
1397         if (load_service_from_keyfile(keyfile, config) == FALSE)
1398                 goto error;
1399
1400         group = g_key_file_get_start_group(keyfile);
1401
1402         service_config = g_hash_table_lookup(config->service_table, group+8);
1403         if (service_config == NULL)
1404                 goto error;
1405
1406         /* Specific to non file based config: */
1407         g_free(service_config->config_ident);
1408         service_config->config_ident = NULL;
1409         g_free(service_config->config_entry);
1410         service_config->config_entry = NULL;
1411
1412         service_config->virtual = TRUE;
1413         service_config->virtual_file = vfile;
1414
1415         __connman_service_provision_changed(vfile);
1416
1417         if (g_strcmp0(service_config->type, "wifi") == 0)
1418                 __connman_device_request_scan(CONNMAN_SERVICE_TYPE_WIFI);
1419
1420         return 0;
1421
1422 error:
1423         DBG("Could not proceed");
1424         g_hash_table_remove(config_table, vfile);
1425         g_free(vfile);
1426
1427         return -EINVAL;
1428 }
1429
1430 struct connman_config_entry **connman_config_get_entries(const char *type)
1431 {
1432         GHashTableIter iter_file, iter_config;
1433         gpointer value, key;
1434         struct connman_config_entry **entries = NULL;
1435         int i = 0, count;
1436
1437         g_hash_table_iter_init(&iter_file, config_table);
1438         while (g_hash_table_iter_next(&iter_file, &key, &value) == TRUE) {
1439                 struct connman_config *config_file = value;
1440
1441                 count = g_hash_table_size(config_file->service_table);
1442
1443                 entries = g_try_realloc(entries, (i + count + 1) *
1444                                         sizeof(struct connman_config_entry *));
1445                 if (entries == NULL)
1446                         return NULL;
1447
1448                 g_hash_table_iter_init(&iter_config,
1449                                                 config_file->service_table);
1450                 while (g_hash_table_iter_next(&iter_config, &key,
1451                                                         &value) == TRUE) {
1452                         struct connman_config_service *config = value;
1453
1454                         if (type != NULL &&
1455                                         g_strcmp0(config->type, type) != 0)
1456                                 continue;
1457
1458                         entries[i] = g_try_new0(struct connman_config_entry,
1459                                                 1);
1460                         if (entries[i] == NULL)
1461                                 goto cleanup;
1462
1463                         entries[i]->ident = g_strdup(config->ident);
1464                         entries[i]->name = g_strdup(config->name);
1465                         entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
1466                         if (entries[i]->ssid == NULL)
1467                                 goto cleanup;
1468
1469                         memcpy(entries[i]->ssid, config->ssid,
1470                                                         config->ssid_len);
1471                         entries[i]->ssid_len = config->ssid_len;
1472                         entries[i]->hidden = config->hidden;
1473
1474                         i++;
1475                 }
1476         }
1477
1478         if (entries != NULL) {
1479                 entries = g_try_realloc(entries, (i + 1) *
1480                                         sizeof(struct connman_config_entry *));
1481                 if (entries == NULL)
1482                         return NULL;
1483
1484                 entries[i] = NULL;
1485
1486                 DBG("%d provisioned AP found", i);
1487         }
1488
1489         return entries;
1490
1491 cleanup:
1492         connman_config_free_entries(entries);
1493         return NULL;
1494 }
1495
1496 void connman_config_free_entries(struct connman_config_entry **entries)
1497 {
1498         int i;
1499
1500         if (entries == NULL)
1501                 return;
1502
1503         for (i = 0; entries[i]; i++) {
1504                 g_free(entries[i]->ident);
1505                 g_free(entries[i]->name);
1506                 g_free(entries[i]->ssid);
1507                 g_free(entries[i]);
1508         }
1509
1510         g_free(entries);
1511         return;
1512 }