5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
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.
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.
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
32 #include <sys/inotify.h>
36 #include <connman/provision.h>
37 #include <connman/ipaddress.h>
40 struct connman_config_service {
45 unsigned int ssid_len;
48 char *anonymous_identity;
51 char *altsubject_match;
52 char *domain_suffix_match;
54 char *client_cert_file;
55 char *private_key_file;
56 char *private_key_passphrase;
57 char *private_key_passphrase_type;
60 enum connman_service_security security;
61 GSList *service_identifiers;
62 char *config_ident; /* file prefix */
63 char *config_entry; /* entry name */
71 unsigned char ipv6_prefix_length;
77 char **search_domains;
82 struct connman_config {
86 GHashTable *service_table;
89 static GHashTable *config_table = NULL;
91 static bool cleanup = false;
93 /* Definition of possible strings in the .config files */
94 #define CONFIG_KEY_NAME "Name"
95 #define CONFIG_KEY_DESC "Description"
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"
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"
127 static const char *config_possible_keys[] = {
133 static const char *service_possible_keys[] = {
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,
150 SERVICE_KEY_PASSPHRASE,
151 SERVICE_KEY_SECURITY,
155 SERVICE_KEY_IPv6_PRIVACY,
158 SERVICE_KEY_NAMESERVERS,
159 SERVICE_KEY_SEARCH_DOMAINS,
160 SERVICE_KEY_TIMESERVERS,
165 static void unregister_config(gpointer data)
167 struct connman_config *config = data;
169 connman_info("Removing configuration %s", config->ident);
171 g_hash_table_destroy(config->service_table);
173 g_free(config->description);
174 g_free(config->name);
175 g_free(config->ident);
179 static void unregister_service(gpointer data)
181 struct connman_config_service *config_service = data;
182 struct connman_service *service;
189 connman_info("Removing service configuration %s",
190 config_service->ident);
192 if (config_service->virtual)
195 for (list = config_service->service_identifiers; list;
197 service_id = list->data;
199 service = connman_service_lookup_from_identifier(service_id);
201 __connman_service_set_immutable(service, false);
202 __connman_service_set_config(service, NULL, NULL);
203 __connman_service_remove(service);
206 * Ethernet or gadget service cannot be removed by
207 * __connman_service_remove() so reset the ipconfig
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);
222 * After these operations, user needs to
223 * reconnect ethernet cable to get IP
229 if (!__connman_storage_remove_service(service_id))
230 DBG("Could not remove all files for service %s",
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);
271 static void check_keys(GKeyFile *keyfile, const char *group,
272 const char **possible_keys)
275 gsize nb_avail_keys, i, j;
277 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
282 * For each key in the configuration file,
283 * verify it is understood by connman
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)
290 if (!possible_keys[j])
291 connman_warn("Unknown configuration key %s in [%s]",
292 avail_keys[i], group);
295 g_strfreev(avail_keys);
298 static int check_family(const char *address, int expected_family)
303 family = connman_inet_check_ipaddress(address);
305 DBG("Cannot get address family of %s (%d/%s)", address,
306 family, gai_strerror(family));
313 if (expected_family != AF_INET) {
314 DBG("Wrong type address %s, expecting IPv4", address);
320 if (expected_family != AF_INET6) {
321 DBG("Wrong type address %s, expecting IPv6", address);
327 DBG("Unsupported address family %d", family);
336 static int parse_address(const char *address_str, int address_family,
337 char **address, char **netmask, char **gateway)
339 char *addr_str, *mask_str, *gw_str;
343 route = g_strsplit(address_str, "/", 0);
348 if (!addr_str || addr_str[0] == '\0') {
353 if ((err = check_family(addr_str, address_family)) < 0)
357 if (!mask_str || mask_str[0] == '\0') {
363 if (gw_str && gw_str[0]) {
364 if ((err = check_family(gw_str, address_family)) < 0)
369 *address = g_strdup(addr_str);
372 *netmask = g_strdup(mask_str);
375 *gateway = g_strdup(gw_str);
378 DBG("address %s/%s via %s", *address, *netmask, *gateway);
380 DBG("address %s/%s", *address, *netmask);
388 static bool check_address(char *address_str, char **address)
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;
400 static bool load_service_generic(GKeyFile *keyfile,
401 const char *group, struct connman_config *config,
402 struct connman_config_service *service)
408 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv4, NULL);
409 if (str && check_address(str, &service->ipv4_address)) {
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",
420 if (!g_strrstr(mask, ".")) {
421 /* We have netmask length */
423 struct in_addr netmask_in;
424 unsigned char prefix_len = 32;
426 long int value = strtol(mask, &ptr, 10);
428 if (ptr != mask && *ptr == '\0' && value && value <= 32)
431 addr = 0xffffffff << (32 - prefix_len);
432 netmask_in.s_addr = htonl(addr);
433 service->ipv4_netmask =
434 g_strdup(inet_ntoa(netmask_in));
438 service->ipv4_netmask = mask;
443 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv6, NULL);
444 if (str && check_address(str, &service->ipv6_address)) {
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",
458 value = strtol(mask, &ptr, 10);
459 if (ptr != mask && *ptr == '\0' && value <= 128)
460 service->ipv6_prefix_length = value;
462 service->ipv6_prefix_length = 128;
468 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv6_PRIVACY,
471 g_free(service->ipv6_privacy);
472 service->ipv6_privacy = str;
475 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_MAC, NULL);
477 g_free(service->mac);
481 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_DOMAIN, NULL);
483 g_free(service->domain_name);
484 service->domain_name = str;
487 strlist = __connman_config_get_string_list(keyfile, group,
488 SERVICE_KEY_NAMESERVERS,
492 g_strfreev(service->nameservers);
493 service->nameservers = strlist;
498 strlist = __connman_config_get_string_list(keyfile, group,
499 SERVICE_KEY_SEARCH_DOMAINS,
503 g_strfreev(service->search_domains);
504 service->search_domains = strlist;
509 strlist = __connman_config_get_string_list(keyfile, group,
510 SERVICE_KEY_TIMESERVERS,
514 g_strfreev(service->timeservers);
515 service->timeservers = strlist;
520 service->mdns = __connman_config_get_bool(keyfile, group,
521 SERVICE_KEY_MDNS, NULL);
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);
539 static bool load_service(GKeyFile *keyfile, const char *group,
540 struct connman_config *config)
542 struct connman_config_service *service;
544 char *str, *hex_ssid;
545 enum connman_service_security security;
546 bool service_created = false;
548 /* Strip off "service_" prefix */
551 if (strlen(ident) < 1)
554 /* Verify that provided keys are good */
555 check_keys(keyfile, group, service_possible_keys);
557 service = g_hash_table_lookup(config->service_table, ident);
559 service = g_try_new0(struct connman_config_service, 1);
563 service->ident = g_strdup(ident);
565 service_created = true;
568 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
570 g_free(service->type);
573 connman_warn("Type of the configured service is missing "
574 "for group %s", group);
578 if (!load_service_generic(keyfile, group, config, service))
581 if (g_strcmp0(str, "ethernet") == 0) {
582 service->config_ident = g_strdup(config->ident);
583 service->config_entry = g_strdup_printf("service_%s",
586 g_hash_table_insert(config->service_table, service->ident,
591 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
593 g_free(service->name);
597 hex_ssid = __connman_config_get_string(keyfile, group, SERVICE_KEY_SSID,
601 unsigned int i, j = 0, hex;
602 size_t hex_ssid_len = strlen(hex_ssid);
604 ssid = g_try_malloc0(hex_ssid_len / 2);
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);
622 g_free(service->ssid);
623 service->ssid = ssid;
624 service->ssid_len = hex_ssid_len / 2;
625 } else if (service->name) {
627 unsigned int ssid_len;
629 ssid_len = strlen(service->name);
630 ssid = g_try_malloc0(ssid_len);
634 memcpy(ssid, service->name, ssid_len);
635 g_free(service->ssid);
636 service->ssid = ssid;
637 service->ssid_len = ssid_len;
640 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
642 g_free(service->eap);
646 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
648 g_free(service->ca_cert_file);
649 service->ca_cert_file = str;
652 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
654 g_free(service->client_cert_file);
655 service->client_cert_file = str;
658 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
660 g_free(service->private_key_file);
661 service->private_key_file = str;
664 str = __connman_config_get_string(keyfile, group,
665 SERVICE_KEY_PRV_KEY_PASS, NULL);
667 g_free(service->private_key_passphrase);
668 service->private_key_passphrase = str;
671 str = __connman_config_get_string(keyfile, group,
672 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
674 g_free(service->private_key_passphrase_type);
675 service->private_key_passphrase_type = str;
678 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
680 g_free(service->identity);
681 service->identity = str;
684 str = __connman_config_get_string(keyfile, group,
685 SERVICE_KEY_ANONYMOUS_IDENTITY, NULL);
687 g_free(service->anonymous_identity);
688 service->anonymous_identity = str;
691 str = __connman_config_get_string(keyfile, group,
692 SERVICE_KEY_SUBJECT_MATCH, NULL);
694 g_free(service->subject_match);
695 service->subject_match = str;
698 str = __connman_config_get_string(keyfile, group,
699 SERVICE_KEY_ALT_SUBJECT_MATCH, NULL);
701 g_free(service->altsubject_match);
702 service->altsubject_match = str;
705 str = __connman_config_get_string(keyfile, group,
706 SERVICE_KEY_DOMAIN_SUFF_MATCH, NULL);
708 g_free(service->domain_suffix_match);
709 service->domain_suffix_match = str;
712 str = __connman_config_get_string(keyfile, group,
713 SERVICE_KEY_DOMAIN_MATCH, NULL);
715 g_free(service->domain_match);
716 service->domain_match = str;
719 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
721 g_free(service->phase2);
722 service->phase2 = str;
725 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
728 g_free(service->passphrase);
729 service->passphrase = str;
732 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_SECURITY,
734 security = __connman_service_string2security(str);
738 if (str && security != CONNMAN_SERVICE_SECURITY_8021X)
739 connman_info("Mismatch between EAP configuration and "
741 SERVICE_KEY_SECURITY, str);
743 service->security = CONNMAN_SERVICE_SECURITY_8021X;
745 } else if (service->passphrase) {
748 if (security == CONNMAN_SERVICE_SECURITY_PSK ||
749 #if defined TIZEN_EXT
750 security == CONNMAN_SERVICE_SECURITY_RSN ||
751 security == CONNMAN_SERVICE_SECURITY_SAE ||
753 security == CONNMAN_SERVICE_SECURITY_WEP) {
754 service->security = security;
756 connman_info("Mismatch with passphrase and "
758 SERVICE_KEY_SECURITY, str);
761 CONNMAN_SERVICE_SECURITY_PSK;
765 service->security = CONNMAN_SERVICE_SECURITY_PSK;
768 if (security != CONNMAN_SERVICE_SECURITY_NONE)
769 connman_info("Mismatch no security and "
771 SERVICE_KEY_SECURITY, str);
773 service->security = CONNMAN_SERVICE_SECURITY_NONE;
775 service->security = CONNMAN_SERVICE_SECURITY_NONE;
779 service->config_ident = g_strdup(config->ident);
780 service->config_entry = g_strdup_printf("service_%s", service->ident);
782 service->hidden = __connman_config_get_bool(keyfile, group,
783 SERVICE_KEY_HIDDEN, NULL);
786 g_hash_table_insert(config->service_table, service->ident,
789 connman_info("Adding service configuration %s", service->ident);
794 if (service_created) {
795 g_free(service->ident);
796 g_free(service->type);
797 g_free(service->name);
798 g_free(service->ssid);
805 static bool load_service_from_keyfile(GKeyFile *keyfile,
806 struct connman_config *config)
812 groups = g_key_file_get_groups(keyfile, NULL);
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]);
820 if (load_service(keyfile, groups[i], config))
829 static int load_config(struct connman_config *config)
834 DBG("config %p", config);
836 keyfile = __connman_storage_load_config(config->ident);
840 g_key_file_set_list_separator(keyfile, ',');
842 /* Verify keys validity of the global section */
843 check_keys(keyfile, "global", config_possible_keys);
845 str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
847 g_free(config->name);
851 str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
853 g_free(config->description);
854 config->description = str;
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);
862 g_key_file_free(keyfile);
867 static struct connman_config *create_config(const char *ident)
869 struct connman_config *config;
871 DBG("ident %s", ident);
873 if (g_hash_table_lookup(config_table, ident))
876 config = g_try_new0(struct connman_config, 1);
880 config->ident = g_strdup(ident);
882 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
883 NULL, unregister_service);
885 g_hash_table_insert(config_table, config->ident, config);
887 connman_info("Adding configuration %s", config->ident);
892 static bool validate_ident(const char *ident)
899 for (i = 0; i < strlen(ident); i++)
900 if (!g_ascii_isprint(ident[i]))
906 static int read_configs(void)
912 dir = g_dir_open(STORAGEDIR, 0, NULL);
916 while ((file = g_dir_read_name(dir))) {
920 if (!g_str_has_suffix(file, ".config"))
923 ident = g_strrstr(file, ".config");
927 str = g_string_new_len(file, ident - file);
931 ident = g_string_free(str, FALSE);
933 if (validate_ident(ident)) {
934 struct connman_config *config;
936 config = create_config(ident);
940 connman_error("Invalid config ident %s", ident);
951 static void config_notify_handler(struct inotify_event *event,
959 if (!g_str_has_suffix(ident, ".config"))
962 ext = g_strrstr(ident, ".config");
968 if (!validate_ident(ident)) {
969 connman_error("Invalid config ident %s", ident);
973 if (event->mask & (IN_CREATE | IN_MOVED_TO))
974 create_config(ident);
976 if (event->mask & (IN_MODIFY | IN_MOVED_TO)) {
977 struct connman_config *config;
979 config = g_hash_table_lookup(config_table, ident);
983 g_hash_table_remove_all(config->service_table);
985 ret = __connman_service_provision_changed(ident);
988 * Re-scan the config file for any
991 g_hash_table_remove_all(config->service_table);
993 __connman_service_provision_changed(ident);
998 if (event->mask & (IN_DELETE | IN_MOVED_FROM))
999 g_hash_table_remove(config_table, ident);
1002 int __connman_config_init(void)
1006 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1007 NULL, unregister_config);
1009 connman_inotify_register(STORAGEDIR, config_notify_handler);
1011 return read_configs();
1014 void __connman_config_cleanup(void)
1020 connman_inotify_unregister(STORAGEDIR, config_notify_handler);
1022 g_hash_table_destroy(config_table);
1023 config_table = NULL;
1028 char *__connman_config_get_string(GKeyFile *key_file,
1029 const char *group_name, const char *key, GError **error)
1031 char *str = g_key_file_get_string(key_file, group_name, key, error);
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))
1040 return g_strchomp(str);
1043 char **__connman_config_get_string_list(GKeyFile *key_file,
1044 const char *group_name, const char *key, gsize *length, GError **error)
1047 char **strlist = g_key_file_get_string_list(key_file, group_name, key,
1054 *p = g_strstrip(*p);
1061 bool __connman_config_get_bool(GKeyFile *key_file,
1062 const char *group_name, const char *key, GError **error)
1067 valstr = g_key_file_get_value(key_file, group_name, key, error);
1071 valstr = g_strchomp(valstr);
1072 if (strcmp(valstr, "true") == 0 || strcmp(valstr, "1") == 0)
1080 static char *config_pem_fsid(const char *pem_file)
1083 unsigned *fsid = (unsigned *) &buf.f_fsid;
1084 unsigned long long fsid64;
1089 if (statfs(pem_file, &buf) < 0) {
1090 connman_error("statfs error %s for %s",
1091 strerror(errno), pem_file);
1095 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
1097 return g_strdup_printf("%llx", fsid64);
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)
1106 __connman_service_set_string(service, "EAP", config->eap);
1108 if (config->identity)
1109 __connman_service_set_string(service, "Identity",
1112 if (config->anonymous_identity)
1113 __connman_service_set_string(service, "AnonymousIdentity",
1114 config->anonymous_identity);
1116 if (config->ca_cert_file)
1117 __connman_service_set_string(service, "CACertFile",
1118 config->ca_cert_file);
1120 if (config->subject_match)
1121 __connman_service_set_string(service, "SubjectMatch",
1122 config->subject_match);
1124 if (config->altsubject_match)
1125 __connman_service_set_string(service, "AltSubjectMatch",
1126 config->altsubject_match);
1128 if (config->domain_suffix_match)
1129 __connman_service_set_string(service, "DomainSuffixMatch",
1130 config->domain_suffix_match);
1132 if (config->domain_match)
1133 __connman_service_set_string(service, "DomainMatch",
1134 config->domain_match);
1136 if (config->client_cert_file)
1137 __connman_service_set_string(service, "ClientCertFile",
1138 config->client_cert_file);
1140 if (config->private_key_file)
1141 __connman_service_set_string(service, "PrivateKeyFile",
1142 config->private_key_file);
1144 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
1145 config->private_key_file) {
1148 fsid = config_pem_fsid(config->private_key_file);
1152 g_free(config->private_key_passphrase);
1153 config->private_key_passphrase = fsid;
1156 if (config->private_key_passphrase) {
1157 __connman_service_set_string(service, "PrivateKeyPassphrase",
1158 config->private_key_passphrase);
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.
1169 __connman_service_set_string(service, "Phase2", config->phase2);
1170 #if defined TIZEN_EXT
1172 __connman_service_set_string(service, "Phase2", NULL);
1175 if (config->passphrase)
1176 __connman_service_set_string(service, "Passphrase",
1177 config->passphrase);
1180 __connman_service_set_hidden(service);
1183 struct connect_virtual {
1184 struct connman_service *service;
1188 static gboolean remove_virtual_config(gpointer user_data)
1190 struct connect_virtual *virtual = user_data;
1192 __connman_service_connect(virtual->service,
1193 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1194 g_hash_table_remove(config_table, virtual->vfile);
1201 #if defined TIZEN_EXT
1202 static bool __check_address_type(int address_family, const char *address)
1204 unsigned char buf[sizeof(struct in6_addr)] = {0, };
1207 err = inet_pton(address_family, address, buf);
1215 static int try_provision_service(struct connman_config_service *config,
1216 struct connman_service *service)
1218 struct connman_network *network;
1219 const void *service_id;
1220 enum connman_service_type type;
1222 unsigned int ssid_len;
1225 network = __connman_service_get_network(service);
1227 connman_error("Service has no network set");
1231 DBG("network %p ident %s", network,
1232 connman_network_get_identifier(network));
1234 type = connman_service_get_type(service);
1237 case CONNMAN_SERVICE_TYPE_WIFI:
1238 if (__connman_service_string2type(config->type) != type)
1241 ssid = connman_network_get_blob(network, "WiFi.SSID",
1246 if (!config->ssid || ssid_len != config->ssid_len)
1249 if (memcmp(config->ssid, ssid, ssid_len))
1252 str = connman_network_get_string(network, "WiFi.Security");
1253 if (config->security != __connman_service_string2security(str))
1258 case CONNMAN_SERVICE_TYPE_ETHERNET:
1259 case CONNMAN_SERVICE_TYPE_GADGET:
1261 if (__connman_service_string2type(config->type) != type)
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:
1280 DBG("service %p ident %s", service,
1281 connman_service_get_identifier(service));
1284 struct connman_device *device;
1285 const char *device_addr;
1287 device = connman_network_get_device(network);
1289 connman_error("Network device is missing");
1293 device_addr = connman_device_get_string(device, "Address");
1295 DBG("wants %s has %s", config->mac, device_addr);
1297 if (g_ascii_strcasecmp(device_addr, config->mac) != 0)
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)
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);
1318 struct connman_ipaddress *address;
1320 if (config->ipv6_prefix_length == 0) {
1321 DBG("IPv6 prefix missing");
1325 address = connman_ipaddress_alloc(AF_INET6);
1329 connman_ipaddress_set_ipv6(address, config->ipv6_address,
1330 config->ipv6_prefix_length,
1331 config->ipv6_gateway);
1333 connman_network_set_ipv6_method(network,
1334 CONNMAN_IPCONFIG_METHOD_FIXED);
1336 if (connman_network_set_ipaddress(network, address) < 0)
1337 DBG("Unable to set IPv6 address to network %p",
1340 connman_ipaddress_free(address);
1343 #if defined TIZEN_EXT
1346 if (config->ipv6_privacy) {
1347 struct connman_ipconfig *ipconfig;
1349 ipconfig = __connman_service_get_ip6config(service);
1351 __connman_ipconfig_ipv6_set_privacy(ipconfig,
1352 config->ipv6_privacy);
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)
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);
1372 struct connman_ipaddress *address;
1374 if (!config->ipv4_netmask) {
1375 DBG("IPv4 netmask missing");
1379 address = connman_ipaddress_alloc(AF_INET);
1383 connman_ipaddress_set_ipv4(address, config->ipv4_address,
1384 config->ipv4_netmask,
1385 config->ipv4_gateway);
1387 connman_network_set_ipv4_method(network,
1388 CONNMAN_IPCONFIG_METHOD_FIXED);
1390 if (connman_network_set_ipaddress(network, address) < 0)
1391 DBG("Unable to set IPv4 address to network %p",
1394 connman_ipaddress_free(address);
1397 #if defined TIZEN_EXT
1400 __connman_service_disconnect(service);
1402 service_id = connman_service_get_identifier(service);
1403 config->service_identifiers =
1404 g_slist_prepend(config->service_identifiers,
1405 g_strdup(service_id));
1407 __connman_service_set_favorite_delayed(service, true, true);
1409 __connman_service_set_config(service, config->config_ident,
1410 config->config_entry);
1412 if (config->domain_name)
1413 __connman_service_set_domainname(service, config->domain_name);
1415 if (config->nameservers) {
1418 __connman_service_nameserver_clear(service);
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);
1431 __connman_service_nameserver_append(service,
1432 config->nameservers[i], false);
1437 if (config->search_domains)
1438 __connman_service_set_search_domains(service,
1439 config->search_domains);
1441 __connman_service_set_mdns(service, config->mdns);
1443 if (config->timeservers)
1444 __connman_service_set_timeservers(service,
1445 config->timeservers);
1447 if (type == CONNMAN_SERVICE_TYPE_WIFI) {
1448 provision_service_wifi(config, service, network,
1452 __connman_service_mark_dirty();
1454 __connman_service_load_modifiable(service);
1456 if (config->virtual) {
1457 struct connect_virtual *virtual;
1459 virtual = g_malloc0(sizeof(struct connect_virtual));
1460 virtual->service = service;
1461 virtual->vfile = config->virtual_file;
1463 g_idle_add(remove_virtual_config, virtual);
1468 #if !defined TIZEN_EXT
1469 __connman_service_set_immutable(service, true);
1472 if (type == CONNMAN_SERVICE_TYPE_ETHERNET ||
1473 type == CONNMAN_SERVICE_TYPE_GADGET) {
1474 __connman_service_connect(service,
1475 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1480 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1486 find_and_provision_service_from_config(struct connman_service *service,
1487 struct connman_config *config)
1489 GHashTableIter iter;
1490 gpointer value, key;
1492 g_hash_table_iter_init(&iter, config->service_table);
1493 while (g_hash_table_iter_next(&iter, &key,
1495 if (!try_provision_service(value, service))
1502 static int find_and_provision_service(struct connman_service *service)
1504 GHashTableIter iter;
1505 gpointer value, key;
1507 g_hash_table_iter_init(&iter, config_table);
1509 while (g_hash_table_iter_next(&iter, &key, &value)) {
1510 struct connman_config *config = value;
1512 if (!find_and_provision_service_from_config(service, config))
1519 int __connman_config_provision_service(struct connman_service *service)
1521 enum connman_service_type type;
1523 /* For now only WiFi, Gadget and Ethernet services are supported */
1524 type = connman_service_get_type(service);
1526 DBG("service %p type %d", service, type);
1528 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1529 type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1530 type != CONNMAN_SERVICE_TYPE_GADGET)
1533 #if defined TIZEN_EXT
1534 if(type == CONNMAN_SERVICE_TYPE_WIFI &&
1535 __connman_service_get_security(service) ==
1536 CONNMAN_SERVICE_SECURITY_NONE)
1540 return find_and_provision_service(service);
1543 int __connman_config_provision_service_ident(struct connman_service *service,
1544 const char *ident, const char *file, const char *entry)
1546 enum connman_service_type type;
1547 struct connman_config *config;
1550 /* For now only WiFi, Gadget and Ethernet services are supported */
1551 type = connman_service_get_type(service);
1553 DBG("service %p type %d", service, type);
1555 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1556 type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1557 type != CONNMAN_SERVICE_TYPE_GADGET)
1560 config = g_hash_table_lookup(config_table, ident);
1562 GHashTableIter iter;
1563 gpointer value, key;
1566 g_hash_table_iter_init(&iter, config->service_table);
1569 * Check if we need to remove individual service if it
1570 * is missing from config file.
1572 if (file && entry) {
1573 while (g_hash_table_iter_next(&iter, &key,
1575 struct connman_config_service *config_service;
1577 config_service = value;
1579 if (g_strcmp0(config_service->config_ident,
1583 if (g_strcmp0(config_service->config_entry,
1591 DBG("found %d ident %s file %s entry %s", found, ident,
1596 * The entry+8 will skip "service_" prefix
1598 g_hash_table_remove(config->service_table,
1604 find_and_provision_service_from_config(service, config);
1610 static void generate_random_string(char *str, int length)
1616 memset(str, '\0', length);
1618 for (i = 0; i < length-1; i++) {
1620 __connman_util_get_random(&rand);
1621 val = (uint8_t)(rand % 122);
1624 } while((val > 57 && val < 65) || (val > 90 && val < 97));
1630 int connman_config_provision_mutable_service(GKeyFile *keyfile)
1632 struct connman_config_service *service_config;
1633 struct connman_config *config;
1634 char *vfile, *group = NULL;
1639 generate_random_string(rstr, 11);
1641 vfile = g_strdup_printf("service_mutable_%s.config", rstr);
1643 config = create_config(vfile);
1647 if (!load_service_from_keyfile(keyfile, config))
1650 group = g_key_file_get_start_group(keyfile);
1652 service_config = g_hash_table_lookup(config->service_table, group+8);
1653 if (!service_config)
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;
1662 service_config->virtual = true;
1663 service_config->virtual_file = vfile;
1665 __connman_service_provision_changed(vfile);
1667 if (g_strcmp0(service_config->type, "wifi") == 0)
1668 __connman_device_request_scan(CONNMAN_SERVICE_TYPE_WIFI);
1674 DBG("Could not proceed");
1675 g_hash_table_remove(config_table, vfile);
1681 struct connman_config_entry **connman_config_get_entries(const char *type)
1683 GHashTableIter iter_file, iter_config;
1684 gpointer value, key;
1685 struct connman_config_entry **entries = NULL;
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;
1693 count = g_hash_table_size(config_file->service_table);
1695 entries = g_try_realloc(entries, (i + count + 1) *
1696 sizeof(struct connman_config_entry *));
1698 g_free(tmp_entries);
1702 g_hash_table_iter_init(&iter_config,
1703 config_file->service_table);
1704 while (g_hash_table_iter_next(&iter_config, &key,
1706 struct connman_config_service *config = value;
1709 g_strcmp0(config->type, type) != 0)
1712 entries[i] = g_try_new0(struct connman_config_entry,
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)
1723 memcpy(entries[i]->ssid, config->ssid,
1725 entries[i]->ssid_len = config->ssid_len;
1726 entries[i]->hidden = config->hidden;
1733 struct connman_config_entry **tmp_entries = entries;
1735 entries = g_try_realloc(entries, (i + 1) *
1736 sizeof(struct connman_config_entry *));
1738 g_free(tmp_entries);
1744 DBG("%d provisioned AP found", i);
1750 connman_config_free_entries(entries);
1754 void connman_config_free_entries(struct connman_config_entry **entries)
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);
1771 bool __connman_config_address_provisioned(const char *address,
1772 const char *netmask)
1774 GHashTableIter iter, siter;
1775 gpointer value, key, svalue, skey;
1777 if (!address || !netmask)
1780 g_hash_table_iter_init(&iter, config_table);
1782 while (g_hash_table_iter_next(&iter, &key, &value)) {
1783 struct connman_config *config = value;
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;
1789 if (!g_strcmp0(address, service->ipv4_address) &&
1791 service->ipv4_netmask))