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 security == CONNMAN_SERVICE_SECURITY_WEP) {
750 service->security = security;
752 connman_info("Mismatch with passphrase and "
754 SERVICE_KEY_SECURITY, str);
757 CONNMAN_SERVICE_SECURITY_PSK;
761 service->security = CONNMAN_SERVICE_SECURITY_PSK;
764 if (security != CONNMAN_SERVICE_SECURITY_NONE)
765 connman_info("Mismatch no security and "
767 SERVICE_KEY_SECURITY, str);
769 service->security = CONNMAN_SERVICE_SECURITY_NONE;
771 service->security = CONNMAN_SERVICE_SECURITY_NONE;
775 service->config_ident = g_strdup(config->ident);
776 service->config_entry = g_strdup_printf("service_%s", service->ident);
778 service->hidden = __connman_config_get_bool(keyfile, group,
779 SERVICE_KEY_HIDDEN, NULL);
782 g_hash_table_insert(config->service_table, service->ident,
785 connman_info("Adding service configuration %s", service->ident);
790 if (service_created) {
791 g_free(service->ident);
792 g_free(service->type);
793 g_free(service->name);
794 g_free(service->ssid);
801 static bool load_service_from_keyfile(GKeyFile *keyfile,
802 struct connman_config *config)
808 groups = g_key_file_get_groups(keyfile, NULL);
810 for (i = 0; groups[i]; i++) {
811 if (!g_str_has_prefix(groups[i], "service_")) {
812 connman_warn("Ignore group named '%s' because prefix "
813 "is not 'service_'", groups[i]);
816 if (load_service(keyfile, groups[i], config))
825 static int load_config(struct connman_config *config)
830 DBG("config %p", config);
832 keyfile = __connman_storage_load_config(config->ident);
836 g_key_file_set_list_separator(keyfile, ',');
838 /* Verify keys validity of the global section */
839 check_keys(keyfile, "global", config_possible_keys);
841 str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
843 g_free(config->name);
847 str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
849 g_free(config->description);
850 config->description = str;
853 if (!load_service_from_keyfile(keyfile, config))
854 connman_warn("Config file %s/%s.config does not contain any "
855 "configuration that can be provisioned!",
856 STORAGEDIR, config->ident);
858 g_key_file_free(keyfile);
863 static struct connman_config *create_config(const char *ident)
865 struct connman_config *config;
867 DBG("ident %s", ident);
869 if (g_hash_table_lookup(config_table, ident))
872 config = g_try_new0(struct connman_config, 1);
876 config->ident = g_strdup(ident);
878 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
879 NULL, unregister_service);
881 g_hash_table_insert(config_table, config->ident, config);
883 connman_info("Adding configuration %s", config->ident);
888 static bool validate_ident(const char *ident)
895 for (i = 0; i < strlen(ident); i++)
896 if (!g_ascii_isprint(ident[i]))
902 static int read_configs(void)
908 dir = g_dir_open(STORAGEDIR, 0, NULL);
912 while ((file = g_dir_read_name(dir))) {
916 if (!g_str_has_suffix(file, ".config"))
919 ident = g_strrstr(file, ".config");
923 str = g_string_new_len(file, ident - file);
927 ident = g_string_free(str, FALSE);
929 if (validate_ident(ident)) {
930 struct connman_config *config;
932 config = create_config(ident);
936 connman_error("Invalid config ident %s", ident);
947 static void config_notify_handler(struct inotify_event *event,
955 if (!g_str_has_suffix(ident, ".config"))
958 ext = g_strrstr(ident, ".config");
964 if (!validate_ident(ident)) {
965 connman_error("Invalid config ident %s", ident);
969 if (event->mask & (IN_CREATE | IN_MOVED_TO))
970 create_config(ident);
972 if (event->mask & (IN_MODIFY | IN_MOVED_TO)) {
973 struct connman_config *config;
975 config = g_hash_table_lookup(config_table, ident);
979 g_hash_table_remove_all(config->service_table);
981 ret = __connman_service_provision_changed(ident);
984 * Re-scan the config file for any
987 g_hash_table_remove_all(config->service_table);
989 __connman_service_provision_changed(ident);
994 if (event->mask & (IN_DELETE | IN_MOVED_FROM))
995 g_hash_table_remove(config_table, ident);
998 int __connman_config_init(void)
1002 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1003 NULL, unregister_config);
1005 connman_inotify_register(STORAGEDIR, config_notify_handler);
1007 return read_configs();
1010 void __connman_config_cleanup(void)
1016 connman_inotify_unregister(STORAGEDIR, config_notify_handler);
1018 g_hash_table_destroy(config_table);
1019 config_table = NULL;
1024 char *__connman_config_get_string(GKeyFile *key_file,
1025 const char *group_name, const char *key, GError **error)
1027 char *str = g_key_file_get_string(key_file, group_name, key, error);
1031 /* passphrases can have spaces in the end */
1032 if (!g_strcmp0(key, SERVICE_KEY_PASSPHRASE) ||
1033 !g_strcmp0(key, SERVICE_KEY_PRV_KEY_PASS))
1036 return g_strchomp(str);
1039 char **__connman_config_get_string_list(GKeyFile *key_file,
1040 const char *group_name, const char *key, gsize *length, GError **error)
1043 char **strlist = g_key_file_get_string_list(key_file, group_name, key,
1050 *p = g_strstrip(*p);
1057 bool __connman_config_get_bool(GKeyFile *key_file,
1058 const char *group_name, const char *key, GError **error)
1063 valstr = g_key_file_get_value(key_file, group_name, key, error);
1067 valstr = g_strchomp(valstr);
1068 if (strcmp(valstr, "true") == 0 || strcmp(valstr, "1") == 0)
1076 static char *config_pem_fsid(const char *pem_file)
1079 unsigned *fsid = (unsigned *) &buf.f_fsid;
1080 unsigned long long fsid64;
1085 if (statfs(pem_file, &buf) < 0) {
1086 connman_error("statfs error %s for %s",
1087 strerror(errno), pem_file);
1091 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
1093 return g_strdup_printf("%llx", fsid64);
1096 static void provision_service_wifi(struct connman_config_service *config,
1097 struct connman_service *service,
1098 struct connman_network *network,
1099 const void *ssid, unsigned int ssid_len)
1102 __connman_service_set_string(service, "EAP", config->eap);
1104 if (config->identity)
1105 __connman_service_set_string(service, "Identity",
1108 if (config->anonymous_identity)
1109 __connman_service_set_string(service, "AnonymousIdentity",
1110 config->anonymous_identity);
1112 if (config->ca_cert_file)
1113 __connman_service_set_string(service, "CACertFile",
1114 config->ca_cert_file);
1116 if (config->subject_match)
1117 __connman_service_set_string(service, "SubjectMatch",
1118 config->subject_match);
1120 if (config->altsubject_match)
1121 __connman_service_set_string(service, "AltSubjectMatch",
1122 config->altsubject_match);
1124 if (config->domain_suffix_match)
1125 __connman_service_set_string(service, "DomainSuffixMatch",
1126 config->domain_suffix_match);
1128 if (config->domain_match)
1129 __connman_service_set_string(service, "DomainMatch",
1130 config->domain_match);
1132 if (config->client_cert_file)
1133 __connman_service_set_string(service, "ClientCertFile",
1134 config->client_cert_file);
1136 if (config->private_key_file)
1137 __connman_service_set_string(service, "PrivateKeyFile",
1138 config->private_key_file);
1140 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
1141 config->private_key_file) {
1144 fsid = config_pem_fsid(config->private_key_file);
1148 g_free(config->private_key_passphrase);
1149 config->private_key_passphrase = fsid;
1152 if (config->private_key_passphrase) {
1153 __connman_service_set_string(service, "PrivateKeyPassphrase",
1154 config->private_key_passphrase);
1156 * TODO: Support for PEAP with both identity and key passwd.
1157 * In that case, we should check if both of them are found
1158 * from the config file. If not, we should not set the
1159 * service passphrase in order for the UI to request for an
1160 * additional passphrase.
1165 __connman_service_set_string(service, "Phase2", config->phase2);
1167 if (config->passphrase)
1168 __connman_service_set_string(service, "Passphrase",
1169 config->passphrase);
1172 __connman_service_set_hidden(service);
1175 struct connect_virtual {
1176 struct connman_service *service;
1180 static gboolean remove_virtual_config(gpointer user_data)
1182 struct connect_virtual *virtual = user_data;
1184 __connman_service_connect(virtual->service,
1185 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1186 g_hash_table_remove(config_table, virtual->vfile);
1193 static int try_provision_service(struct connman_config_service *config,
1194 struct connman_service *service)
1196 struct connman_network *network;
1197 const void *service_id;
1198 enum connman_service_type type;
1200 unsigned int ssid_len;
1203 network = __connman_service_get_network(service);
1205 connman_error("Service has no network set");
1209 DBG("network %p ident %s", network,
1210 connman_network_get_identifier(network));
1212 type = connman_service_get_type(service);
1215 case CONNMAN_SERVICE_TYPE_WIFI:
1216 if (__connman_service_string2type(config->type) != type)
1219 ssid = connman_network_get_blob(network, "WiFi.SSID",
1224 if (!config->ssid || ssid_len != config->ssid_len)
1227 if (memcmp(config->ssid, ssid, ssid_len))
1230 str = connman_network_get_string(network, "WiFi.Security");
1231 if (config->security != __connman_service_string2security(str))
1236 case CONNMAN_SERVICE_TYPE_ETHERNET:
1237 case CONNMAN_SERVICE_TYPE_GADGET:
1239 if (__connman_service_string2type(config->type) != type)
1244 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1245 case CONNMAN_SERVICE_TYPE_SYSTEM:
1246 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1247 case CONNMAN_SERVICE_TYPE_CELLULAR:
1248 case CONNMAN_SERVICE_TYPE_GPS:
1249 case CONNMAN_SERVICE_TYPE_VPN:
1250 case CONNMAN_SERVICE_TYPE_P2P:
1255 DBG("service %p ident %s", service,
1256 connman_service_get_identifier(service));
1259 struct connman_device *device;
1260 const char *device_addr;
1262 device = connman_network_get_device(network);
1264 connman_error("Network device is missing");
1268 device_addr = connman_device_get_string(device, "Address");
1270 DBG("wants %s has %s", config->mac, device_addr);
1272 if (g_ascii_strcasecmp(device_addr, config->mac) != 0)
1276 if (!config->ipv6_address) {
1277 connman_network_set_ipv6_method(network,
1278 CONNMAN_IPCONFIG_METHOD_AUTO);
1279 } else if (g_ascii_strcasecmp(config->ipv6_address, "off") == 0) {
1280 connman_network_set_ipv6_method(network,
1281 CONNMAN_IPCONFIG_METHOD_OFF);
1282 } else if (g_ascii_strcasecmp(config->ipv6_address, "auto") == 0 ||
1283 g_ascii_strcasecmp(config->ipv6_address, "dhcp") == 0) {
1284 connman_network_set_ipv6_method(network,
1285 CONNMAN_IPCONFIG_METHOD_AUTO);
1287 struct connman_ipaddress *address;
1289 if (config->ipv6_prefix_length == 0) {
1290 DBG("IPv6 prefix missing");
1294 address = connman_ipaddress_alloc(AF_INET6);
1298 connman_ipaddress_set_ipv6(address, config->ipv6_address,
1299 config->ipv6_prefix_length,
1300 config->ipv6_gateway);
1302 connman_network_set_ipv6_method(network,
1303 CONNMAN_IPCONFIG_METHOD_FIXED);
1305 if (connman_network_set_ipaddress(network, address) < 0)
1306 DBG("Unable to set IPv6 address to network %p",
1309 connman_ipaddress_free(address);
1312 if (config->ipv6_privacy) {
1313 struct connman_ipconfig *ipconfig;
1315 ipconfig = __connman_service_get_ip6config(service);
1317 __connman_ipconfig_ipv6_set_privacy(ipconfig,
1318 config->ipv6_privacy);
1321 if (!config->ipv4_address) {
1322 connman_network_set_ipv4_method(network,
1323 CONNMAN_IPCONFIG_METHOD_DHCP);
1324 } else if (g_ascii_strcasecmp(config->ipv4_address, "off") == 0) {
1325 connman_network_set_ipv4_method(network,
1326 CONNMAN_IPCONFIG_METHOD_OFF);
1327 } else if (g_ascii_strcasecmp(config->ipv4_address, "auto") == 0 ||
1328 g_ascii_strcasecmp(config->ipv4_address, "dhcp") == 0) {
1329 connman_network_set_ipv4_method(network,
1330 CONNMAN_IPCONFIG_METHOD_DHCP);
1332 struct connman_ipaddress *address;
1334 if (!config->ipv4_netmask) {
1335 DBG("IPv4 netmask missing");
1339 address = connman_ipaddress_alloc(AF_INET);
1343 connman_ipaddress_set_ipv4(address, config->ipv4_address,
1344 config->ipv4_netmask,
1345 config->ipv4_gateway);
1347 connman_network_set_ipv4_method(network,
1348 CONNMAN_IPCONFIG_METHOD_FIXED);
1350 if (connman_network_set_ipaddress(network, address) < 0)
1351 DBG("Unable to set IPv4 address to network %p",
1354 connman_ipaddress_free(address);
1357 __connman_service_disconnect(service);
1359 service_id = connman_service_get_identifier(service);
1360 config->service_identifiers =
1361 g_slist_prepend(config->service_identifiers,
1362 g_strdup(service_id));
1364 __connman_service_set_favorite_delayed(service, true, true);
1366 __connman_service_set_config(service, config->config_ident,
1367 config->config_entry);
1369 if (config->domain_name)
1370 __connman_service_set_domainname(service, config->domain_name);
1372 if (config->nameservers) {
1375 __connman_service_nameserver_clear(service);
1377 for (i = 0; config->nameservers[i]; i++) {
1378 __connman_service_nameserver_append(service,
1379 config->nameservers[i], false);
1383 if (config->search_domains)
1384 __connman_service_set_search_domains(service,
1385 config->search_domains);
1387 __connman_service_set_mdns(service, config->mdns);
1389 if (config->timeservers)
1390 __connman_service_set_timeservers(service,
1391 config->timeservers);
1393 if (type == CONNMAN_SERVICE_TYPE_WIFI) {
1394 provision_service_wifi(config, service, network,
1398 __connman_service_mark_dirty();
1400 __connman_service_load_modifiable(service);
1402 if (config->virtual) {
1403 struct connect_virtual *virtual;
1405 virtual = g_malloc0(sizeof(struct connect_virtual));
1406 virtual->service = service;
1407 virtual->vfile = config->virtual_file;
1409 g_idle_add(remove_virtual_config, virtual);
1414 __connman_service_set_immutable(service, true);
1416 if (type == CONNMAN_SERVICE_TYPE_ETHERNET ||
1417 type == CONNMAN_SERVICE_TYPE_GADGET) {
1418 __connman_service_connect(service,
1419 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1424 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1430 find_and_provision_service_from_config(struct connman_service *service,
1431 struct connman_config *config)
1433 GHashTableIter iter;
1434 gpointer value, key;
1436 g_hash_table_iter_init(&iter, config->service_table);
1437 while (g_hash_table_iter_next(&iter, &key,
1439 if (!try_provision_service(value, service))
1446 static int find_and_provision_service(struct connman_service *service)
1448 GHashTableIter iter;
1449 gpointer value, key;
1451 g_hash_table_iter_init(&iter, config_table);
1453 while (g_hash_table_iter_next(&iter, &key, &value)) {
1454 struct connman_config *config = value;
1456 if (!find_and_provision_service_from_config(service, config))
1463 int __connman_config_provision_service(struct connman_service *service)
1465 enum connman_service_type type;
1467 /* For now only WiFi, Gadget and Ethernet services are supported */
1468 type = connman_service_get_type(service);
1470 DBG("service %p type %d", service, type);
1472 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1473 type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1474 type != CONNMAN_SERVICE_TYPE_GADGET)
1477 return find_and_provision_service(service);
1480 int __connman_config_provision_service_ident(struct connman_service *service,
1481 const char *ident, const char *file, const char *entry)
1483 enum connman_service_type type;
1484 struct connman_config *config;
1487 /* For now only WiFi, Gadget and Ethernet services are supported */
1488 type = connman_service_get_type(service);
1490 DBG("service %p type %d", service, type);
1492 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1493 type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1494 type != CONNMAN_SERVICE_TYPE_GADGET)
1497 config = g_hash_table_lookup(config_table, ident);
1499 GHashTableIter iter;
1500 gpointer value, key;
1503 g_hash_table_iter_init(&iter, config->service_table);
1506 * Check if we need to remove individual service if it
1507 * is missing from config file.
1509 if (file && entry) {
1510 while (g_hash_table_iter_next(&iter, &key,
1512 struct connman_config_service *config_service;
1514 config_service = value;
1516 if (g_strcmp0(config_service->config_ident,
1520 if (g_strcmp0(config_service->config_entry,
1528 DBG("found %d ident %s file %s entry %s", found, ident,
1533 * The entry+8 will skip "service_" prefix
1535 g_hash_table_remove(config->service_table,
1541 find_and_provision_service_from_config(service, config);
1547 static void generate_random_string(char *str, int length)
1553 memset(str, '\0', length);
1555 for (i = 0; i < length-1; i++) {
1557 __connman_util_get_random(&rand);
1558 val = (uint8_t)(rand % 122);
1561 } while((val > 57 && val < 65) || (val > 90 && val < 97));
1567 int connman_config_provision_mutable_service(GKeyFile *keyfile)
1569 struct connman_config_service *service_config;
1570 struct connman_config *config;
1571 char *vfile, *group = NULL;
1576 generate_random_string(rstr, 11);
1578 vfile = g_strdup_printf("service_mutable_%s.config", rstr);
1580 config = create_config(vfile);
1584 if (!load_service_from_keyfile(keyfile, config))
1587 group = g_key_file_get_start_group(keyfile);
1589 service_config = g_hash_table_lookup(config->service_table, group+8);
1590 if (!service_config)
1593 /* Specific to non file based config: */
1594 g_free(service_config->config_ident);
1595 service_config->config_ident = NULL;
1596 g_free(service_config->config_entry);
1597 service_config->config_entry = NULL;
1599 service_config->virtual = true;
1600 service_config->virtual_file = vfile;
1602 __connman_service_provision_changed(vfile);
1604 if (g_strcmp0(service_config->type, "wifi") == 0)
1605 __connman_device_request_scan(CONNMAN_SERVICE_TYPE_WIFI);
1611 DBG("Could not proceed");
1612 g_hash_table_remove(config_table, vfile);
1618 struct connman_config_entry **connman_config_get_entries(const char *type)
1620 GHashTableIter iter_file, iter_config;
1621 gpointer value, key;
1622 struct connman_config_entry **entries = NULL;
1625 g_hash_table_iter_init(&iter_file, config_table);
1626 while (g_hash_table_iter_next(&iter_file, &key, &value)) {
1627 struct connman_config *config_file = value;
1628 struct connman_config_entry **tmp_entries = entries;
1630 count = g_hash_table_size(config_file->service_table);
1632 entries = g_try_realloc(entries, (i + count + 1) *
1633 sizeof(struct connman_config_entry *));
1635 g_free(tmp_entries);
1639 g_hash_table_iter_init(&iter_config,
1640 config_file->service_table);
1641 while (g_hash_table_iter_next(&iter_config, &key,
1643 struct connman_config_service *config = value;
1646 g_strcmp0(config->type, type) != 0)
1649 entries[i] = g_try_new0(struct connman_config_entry,
1654 entries[i]->ident = g_strdup(config->ident);
1655 entries[i]->name = g_strdup(config->name);
1656 entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
1657 if (!entries[i]->ssid)
1660 memcpy(entries[i]->ssid, config->ssid,
1662 entries[i]->ssid_len = config->ssid_len;
1663 entries[i]->hidden = config->hidden;
1670 struct connman_config_entry **tmp_entries = entries;
1672 entries = g_try_realloc(entries, (i + 1) *
1673 sizeof(struct connman_config_entry *));
1675 g_free(tmp_entries);
1681 DBG("%d provisioned AP found", i);
1687 connman_config_free_entries(entries);
1691 void connman_config_free_entries(struct connman_config_entry **entries)
1698 for (i = 0; entries[i]; i++) {
1699 g_free(entries[i]->ident);
1700 g_free(entries[i]->name);
1701 g_free(entries[i]->ssid);
1708 bool __connman_config_address_provisioned(const char *address,
1709 const char *netmask)
1711 GHashTableIter iter, siter;
1712 gpointer value, key, svalue, skey;
1714 if (!address || !netmask)
1717 g_hash_table_iter_init(&iter, config_table);
1719 while (g_hash_table_iter_next(&iter, &key, &value)) {
1720 struct connman_config *config = value;
1722 g_hash_table_iter_init(&siter, config->service_table);
1723 while (g_hash_table_iter_next(&siter, &skey, &svalue)) {
1724 struct connman_config_service *service = svalue;
1726 if (!g_strcmp0(address, service->ipv4_address) &&
1728 service->ipv4_netmask))