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