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;
49 char *client_cert_file;
50 char *private_key_file;
51 char *private_key_passphrase;
52 char *private_key_passphrase_type;
55 enum connman_service_security security;
56 GSList *service_identifiers;
57 char *config_ident; /* file prefix */
58 char *config_entry; /* entry name */
66 unsigned char ipv6_prefix_length;
71 char **search_domains;
76 struct connman_config {
80 GHashTable *service_table;
83 static GHashTable *config_table = NULL;
85 static bool cleanup = false;
87 /* Definition of possible strings in the .config files */
88 #define CONFIG_KEY_NAME "Name"
89 #define CONFIG_KEY_DESC "Description"
91 #define SERVICE_KEY_TYPE "Type"
92 #define SERVICE_KEY_NAME "Name"
93 #define SERVICE_KEY_SSID "SSID"
94 #define SERVICE_KEY_EAP "EAP"
95 #define SERVICE_KEY_CA_CERT "CACertFile"
96 #define SERVICE_KEY_CL_CERT "ClientCertFile"
97 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
98 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
99 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
100 #define SERVICE_KEY_IDENTITY "Identity"
101 #define SERVICE_KEY_PHASE2 "Phase2"
102 #define SERVICE_KEY_PASSPHRASE "Passphrase"
103 #define SERVICE_KEY_SECURITY "Security"
104 #define SERVICE_KEY_HIDDEN "Hidden"
106 #define SERVICE_KEY_IPv4 "IPv4"
107 #define SERVICE_KEY_IPv6 "IPv6"
108 #define SERVICE_KEY_IPv6_PRIVACY "IPv6.Privacy"
109 #define SERVICE_KEY_MAC "MAC"
110 #define SERVICE_KEY_NAMESERVERS "Nameservers"
111 #define SERVICE_KEY_SEARCH_DOMAINS "SearchDomains"
112 #define SERVICE_KEY_TIMESERVERS "Timeservers"
113 #define SERVICE_KEY_DOMAIN "Domain"
115 static const char *config_possible_keys[] = {
121 static const char *service_possible_keys[] = {
129 SERVICE_KEY_PRV_KEY_PASS,
130 SERVICE_KEY_PRV_KEY_PASS_TYPE,
131 SERVICE_KEY_IDENTITY,
133 SERVICE_KEY_PASSPHRASE,
134 SERVICE_KEY_SECURITY,
138 SERVICE_KEY_IPv6_PRIVACY,
140 SERVICE_KEY_NAMESERVERS,
141 SERVICE_KEY_SEARCH_DOMAINS,
142 SERVICE_KEY_TIMESERVERS,
147 static void unregister_config(gpointer data)
149 struct connman_config *config = data;
151 connman_info("Removing configuration %s", config->ident);
153 g_hash_table_destroy(config->service_table);
155 g_free(config->description);
156 g_free(config->name);
157 g_free(config->ident);
161 static void unregister_service(gpointer data)
163 struct connman_config_service *config_service = data;
164 struct connman_service *service;
171 connman_info("Removing service configuration %s",
172 config_service->ident);
174 if (config_service->virtual)
177 for (list = config_service->service_identifiers; list;
179 service_id = list->data;
181 service = __connman_service_lookup_from_ident(service_id);
183 __connman_service_set_immutable(service, false);
184 __connman_service_set_config(service, NULL, NULL);
185 __connman_service_remove(service);
188 * Ethernet or gadget service cannot be removed by
189 * __connman_service_remove() so reset the ipconfig
192 if (connman_service_get_type(service) ==
193 CONNMAN_SERVICE_TYPE_ETHERNET ||
194 connman_service_get_type(service) ==
195 CONNMAN_SERVICE_TYPE_GADGET) {
196 __connman_service_disconnect(service);
197 __connman_service_reset_ipconfig(service,
198 CONNMAN_IPCONFIG_TYPE_IPV4, NULL, NULL);
199 __connman_service_reset_ipconfig(service,
200 CONNMAN_IPCONFIG_TYPE_IPV6, NULL, NULL);
201 __connman_service_set_ignore(service, true);
204 * After these operations, user needs to
205 * reconnect ethernet cable to get IP
211 if (!__connman_storage_remove_service(service_id))
212 DBG("Could not remove all files for service %s",
217 g_free(config_service->ident);
218 g_free(config_service->type);
219 g_free(config_service->name);
220 g_free(config_service->ssid);
221 g_free(config_service->eap);
222 g_free(config_service->identity);
223 g_free(config_service->ca_cert_file);
224 g_free(config_service->client_cert_file);
225 g_free(config_service->private_key_file);
226 g_free(config_service->private_key_passphrase);
227 g_free(config_service->private_key_passphrase_type);
228 g_free(config_service->phase2);
229 g_free(config_service->passphrase);
230 g_free(config_service->ipv4_address);
231 g_free(config_service->ipv4_gateway);
232 g_free(config_service->ipv4_netmask);
233 g_free(config_service->ipv6_address);
234 g_free(config_service->ipv6_gateway);
235 g_free(config_service->ipv6_privacy);
236 g_free(config_service->mac);
237 g_strfreev(config_service->nameservers);
238 g_strfreev(config_service->search_domains);
239 g_strfreev(config_service->timeservers);
240 g_free(config_service->domain_name);
241 g_slist_free_full(config_service->service_identifiers, g_free);
242 g_free(config_service->config_ident);
243 g_free(config_service->config_entry);
244 g_free(config_service->virtual_file);
245 g_free(config_service);
248 static void check_keys(GKeyFile *keyfile, const char *group,
249 const char **possible_keys)
252 gsize nb_avail_keys, i, j;
254 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
259 * For each key in the configuration file,
260 * verify it is understood by connman
262 for (i = 0 ; i < nb_avail_keys; i++) {
263 for (j = 0; possible_keys[j] ; j++)
264 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
267 if (!possible_keys[j])
268 connman_warn("Unknown configuration key %s in [%s]",
269 avail_keys[i], group);
272 g_strfreev(avail_keys);
275 static int check_family(const char *address, int expected_family)
280 family = connman_inet_check_ipaddress(address);
282 DBG("Cannot get address family of %s (%d/%s)", address,
283 family, gai_strerror(family));
290 if (expected_family != AF_INET) {
291 DBG("Wrong type address %s, expecting IPv4", address);
297 if (expected_family != AF_INET6) {
298 DBG("Wrong type address %s, expecting IPv6", address);
304 DBG("Unsupported address family %d", family);
313 static int parse_address(const char *address_str, int address_family,
314 char **address, char **netmask, char **gateway)
316 char *addr_str, *mask_str, *gw_str;
320 route = g_strsplit(address_str, "/", 0);
325 if (!addr_str || addr_str[0] == '\0') {
330 if ((err = check_family(addr_str, address_family)) < 0)
334 if (!mask_str || mask_str[0] == '\0') {
340 if (gw_str && gw_str[0]) {
341 if ((err = check_family(gw_str, address_family)) < 0)
346 *address = g_strdup(addr_str);
349 *netmask = g_strdup(mask_str);
352 *gateway = g_strdup(gw_str);
355 DBG("address %s/%s via %s", *address, *netmask, *gateway);
357 DBG("address %s/%s", *address, *netmask);
365 static bool check_address(char *address_str, char **address)
367 if (g_ascii_strcasecmp(address_str, "auto") == 0 ||
368 g_ascii_strcasecmp(address_str, "dhcp") == 0 ||
369 g_ascii_strcasecmp(address_str, "off") == 0) {
370 *address = address_str;
377 static bool load_service_generic(GKeyFile *keyfile,
378 const char *group, struct connman_config *config,
379 struct connman_config_service *service)
385 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv4, NULL);
386 if (str && check_address(str, &service->ipv4_address)) {
389 if (parse_address(str, AF_INET, &service->ipv4_address,
390 &mask, &service->ipv4_gateway) < 0) {
391 connman_warn("Invalid format for IPv4 address %s",
397 if (!g_strrstr(mask, ".")) {
398 /* We have netmask length */
400 struct in_addr netmask_in;
401 unsigned char prefix_len = 32;
403 long int value = strtol(mask, &ptr, 10);
405 if (ptr != mask && *ptr == '\0' && value && value <= 32)
408 addr = 0xffffffff << (32 - prefix_len);
409 netmask_in.s_addr = htonl(addr);
410 service->ipv4_netmask =
411 g_strdup(inet_ntoa(netmask_in));
415 service->ipv4_netmask = mask;
420 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv6, NULL);
421 if (str && check_address(str, &service->ipv6_address)) {
427 if (parse_address(str, AF_INET6, &service->ipv6_address,
428 &mask, &service->ipv6_gateway) < 0) {
429 connman_warn("Invalid format for IPv6 address %s",
435 value = strtol(mask, &ptr, 10);
436 if (ptr != mask && *ptr == '\0' && value <= 128)
437 service->ipv6_prefix_length = value;
439 service->ipv6_prefix_length = 128;
445 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IPv6_PRIVACY,
448 g_free(service->ipv6_privacy);
449 service->ipv6_privacy = str;
452 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_MAC, NULL);
454 g_free(service->mac);
458 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_DOMAIN, NULL);
460 g_free(service->domain_name);
461 service->domain_name = str;
464 strlist = __connman_config_get_string_list(keyfile, group,
465 SERVICE_KEY_NAMESERVERS,
469 g_strfreev(service->nameservers);
470 service->nameservers = strlist;
475 strlist = __connman_config_get_string_list(keyfile, group,
476 SERVICE_KEY_SEARCH_DOMAINS,
480 g_strfreev(service->search_domains);
481 service->search_domains = strlist;
486 strlist = __connman_config_get_string_list(keyfile, group,
487 SERVICE_KEY_TIMESERVERS,
491 g_strfreev(service->timeservers);
492 service->timeservers = strlist;
500 g_free(service->ident);
501 g_free(service->type);
502 g_free(service->ipv4_address);
503 g_free(service->ipv4_netmask);
504 g_free(service->ipv4_gateway);
505 g_free(service->ipv6_address);
506 g_free(service->ipv6_gateway);
507 g_free(service->mac);
513 static bool load_service(GKeyFile *keyfile, const char *group,
514 struct connman_config *config)
516 struct connman_config_service *service;
518 char *str, *hex_ssid;
519 enum connman_service_security security;
520 bool service_created = false;
522 /* Strip off "service_" prefix */
525 if (strlen(ident) < 1)
528 /* Verify that provided keys are good */
529 check_keys(keyfile, group, service_possible_keys);
531 service = g_hash_table_lookup(config->service_table, ident);
533 service = g_try_new0(struct connman_config_service, 1);
537 service->ident = g_strdup(ident);
539 service_created = true;
542 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
544 g_free(service->type);
547 DBG("Type of the configured service is missing for group %s",
552 if (!load_service_generic(keyfile, group, config, service))
555 if (g_strcmp0(str, "ethernet") == 0) {
556 service->config_ident = g_strdup(config->ident);
557 service->config_entry = g_strdup_printf("service_%s",
560 g_hash_table_insert(config->service_table, service->ident,
565 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
567 g_free(service->name);
571 hex_ssid = __connman_config_get_string(keyfile, group, SERVICE_KEY_SSID,
575 unsigned int i, j = 0, hex;
576 size_t hex_ssid_len = strlen(hex_ssid);
578 ssid = g_try_malloc0(hex_ssid_len / 2);
584 for (i = 0; i < hex_ssid_len; i += 2) {
585 if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
586 connman_warn("Invalid SSID %s", hex_ssid);
596 g_free(service->ssid);
597 service->ssid = ssid;
598 service->ssid_len = hex_ssid_len / 2;
599 } else if (service->name) {
601 unsigned int ssid_len;
603 ssid_len = strlen(service->name);
604 ssid = g_try_malloc0(ssid_len);
608 memcpy(ssid, service->name, ssid_len);
609 g_free(service->ssid);
610 service->ssid = ssid;
611 service->ssid_len = ssid_len;
614 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
616 g_free(service->eap);
620 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
622 g_free(service->ca_cert_file);
623 service->ca_cert_file = str;
626 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
628 g_free(service->client_cert_file);
629 service->client_cert_file = str;
632 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
634 g_free(service->private_key_file);
635 service->private_key_file = str;
638 str = __connman_config_get_string(keyfile, group,
639 SERVICE_KEY_PRV_KEY_PASS, NULL);
641 g_free(service->private_key_passphrase);
642 service->private_key_passphrase = str;
645 str = __connman_config_get_string(keyfile, group,
646 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
648 g_free(service->private_key_passphrase_type);
649 service->private_key_passphrase_type = str;
652 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
654 g_free(service->identity);
655 service->identity = str;
658 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
660 g_free(service->phase2);
661 service->phase2 = str;
664 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
667 g_free(service->passphrase);
668 service->passphrase = str;
671 str = __connman_config_get_string(keyfile, group, SERVICE_KEY_SECURITY,
673 security = __connman_service_string2security(str);
677 if (str && security != CONNMAN_SERVICE_SECURITY_8021X)
678 connman_info("Mismatch between EAP configuration and "
680 SERVICE_KEY_SECURITY, str);
682 service->security = CONNMAN_SERVICE_SECURITY_8021X;
684 } else if (service->passphrase) {
687 if (security == CONNMAN_SERVICE_SECURITY_PSK ||
688 #if defined TIZEN_EXT
689 security == CONNMAN_SERVICE_SECURITY_RSN ||
691 security == CONNMAN_SERVICE_SECURITY_WEP) {
692 service->security = security;
694 connman_info("Mismatch with passphrase and "
696 SERVICE_KEY_SECURITY, str);
699 CONNMAN_SERVICE_SECURITY_PSK;
703 service->security = CONNMAN_SERVICE_SECURITY_PSK;
706 service->config_ident = g_strdup(config->ident);
707 service->config_entry = g_strdup_printf("service_%s", service->ident);
709 service->hidden = __connman_config_get_bool(keyfile, group,
710 SERVICE_KEY_HIDDEN, NULL);
713 g_hash_table_insert(config->service_table, service->ident,
716 connman_info("Adding service configuration %s", service->ident);
721 if (service_created) {
722 g_free(service->ident);
723 g_free(service->type);
724 g_free(service->name);
725 g_free(service->ssid);
732 static bool load_service_from_keyfile(GKeyFile *keyfile,
733 struct connman_config *config)
739 groups = g_key_file_get_groups(keyfile, NULL);
741 for (i = 0; groups[i]; i++) {
742 if (!g_str_has_prefix(groups[i], "service_"))
744 if (load_service(keyfile, groups[i], config))
753 static int load_config(struct connman_config *config)
758 DBG("config %p", config);
760 keyfile = __connman_storage_load_config(config->ident);
764 g_key_file_set_list_separator(keyfile, ',');
766 /* Verify keys validity of the global section */
767 check_keys(keyfile, "global", config_possible_keys);
769 str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
771 g_free(config->name);
775 str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
777 g_free(config->description);
778 config->description = str;
781 if (!load_service_from_keyfile(keyfile, config))
782 connman_warn("Config file %s/%s.config does not contain any "
783 "configuration that can be provisioned!",
784 STORAGEDIR, config->ident);
786 g_key_file_free(keyfile);
791 static struct connman_config *create_config(const char *ident)
793 struct connman_config *config;
795 DBG("ident %s", ident);
797 if (g_hash_table_lookup(config_table, ident))
800 config = g_try_new0(struct connman_config, 1);
804 config->ident = g_strdup(ident);
806 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
807 NULL, unregister_service);
809 g_hash_table_insert(config_table, config->ident, config);
811 connman_info("Adding configuration %s", config->ident);
816 static bool validate_ident(const char *ident)
823 for (i = 0; i < strlen(ident); i++)
824 if (!g_ascii_isprint(ident[i]))
830 static int read_configs(void)
836 dir = g_dir_open(STORAGEDIR, 0, NULL);
840 while ((file = g_dir_read_name(dir))) {
844 if (!g_str_has_suffix(file, ".config"))
847 ident = g_strrstr(file, ".config");
851 str = g_string_new_len(file, ident - file);
855 ident = g_string_free(str, FALSE);
857 if (validate_ident(ident)) {
858 struct connman_config *config;
860 config = create_config(ident);
864 connman_error("Invalid config ident %s", ident);
875 static void config_notify_handler(struct inotify_event *event,
883 if (!g_str_has_suffix(ident, ".config"))
886 ext = g_strrstr(ident, ".config");
892 if (!validate_ident(ident)) {
893 connman_error("Invalid config ident %s", ident);
897 if (event->mask & IN_CREATE || event->mask & IN_MOVED_TO)
898 create_config(ident);
900 if (event->mask & IN_MODIFY) {
901 struct connman_config *config;
903 config = g_hash_table_lookup(config_table, ident);
907 g_hash_table_remove_all(config->service_table);
909 ret = __connman_service_provision_changed(ident);
912 * Re-scan the config file for any
915 g_hash_table_remove_all(config->service_table);
917 __connman_service_provision_changed(ident);
922 if (event->mask & IN_DELETE)
923 g_hash_table_remove(config_table, ident);
926 int __connman_config_init(void)
930 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
931 NULL, unregister_config);
933 connman_inotify_register(STORAGEDIR, config_notify_handler);
935 return read_configs();
938 void __connman_config_cleanup(void)
944 connman_inotify_unregister(STORAGEDIR, config_notify_handler);
946 g_hash_table_destroy(config_table);
952 char *__connman_config_get_string(GKeyFile *key_file,
953 const char *group_name, const char *key, GError **error)
955 char *str = g_key_file_get_string(key_file, group_name, key, error);
959 return g_strchomp(str);
962 char **__connman_config_get_string_list(GKeyFile *key_file,
963 const char *group_name, const char *key, gsize *length, GError **error)
966 char **strlist = g_key_file_get_string_list(key_file, group_name, key,
980 bool __connman_config_get_bool(GKeyFile *key_file,
981 const char *group_name, const char *key, GError **error)
986 valstr = g_key_file_get_value(key_file, group_name, key, error);
990 valstr = g_strchomp(valstr);
991 if (strcmp(valstr, "true") == 0 || strcmp(valstr, "1") == 0)
999 static char *config_pem_fsid(const char *pem_file)
1002 unsigned *fsid = (unsigned *) &buf.f_fsid;
1003 unsigned long long fsid64;
1008 if (statfs(pem_file, &buf) < 0) {
1009 connman_error("statfs error %s for %s",
1010 strerror(errno), pem_file);
1014 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
1016 return g_strdup_printf("%llx", fsid64);
1019 static void provision_service_wifi(struct connman_config_service *config,
1020 struct connman_service *service,
1021 struct connman_network *network,
1022 const void *ssid, unsigned int ssid_len)
1025 __connman_service_set_string(service, "EAP", config->eap);
1027 if (config->identity)
1028 __connman_service_set_string(service, "Identity",
1031 if (config->ca_cert_file)
1032 __connman_service_set_string(service, "CACertFile",
1033 config->ca_cert_file);
1035 if (config->client_cert_file)
1036 __connman_service_set_string(service, "ClientCertFile",
1037 config->client_cert_file);
1039 if (config->private_key_file)
1040 __connman_service_set_string(service, "PrivateKeyFile",
1041 config->private_key_file);
1043 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
1044 config->private_key_file) {
1047 fsid = config_pem_fsid(config->private_key_file);
1051 g_free(config->private_key_passphrase);
1052 config->private_key_passphrase = fsid;
1055 if (config->private_key_passphrase) {
1056 __connman_service_set_string(service, "PrivateKeyPassphrase",
1057 config->private_key_passphrase);
1059 * TODO: Support for PEAP with both identity and key passwd.
1060 * In that case, we should check if both of them are found
1061 * from the config file. If not, we should not set the
1062 * service passphrase in order for the UI to request for an
1063 * additional passphrase.
1068 __connman_service_set_string(service, "Phase2", config->phase2);
1069 #if defined TIZEN_EXT
1071 __connman_service_set_string(service, "Phase2", NULL);
1074 if (config->passphrase)
1075 __connman_service_set_string(service, "Passphrase",
1076 config->passphrase);
1079 __connman_service_set_hidden(service);
1082 struct connect_virtual {
1083 struct connman_service *service;
1087 static gboolean remove_virtual_config(gpointer user_data)
1089 struct connect_virtual *virtual = user_data;
1091 __connman_service_connect(virtual->service,
1092 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1093 g_hash_table_remove(config_table, virtual->vfile);
1100 #if defined TIZEN_EXT
1101 static bool __check_address_type(int address_family, const char *address)
1103 unsigned char buf[sizeof(struct in6_addr)] = {0, };
1106 err = inet_pton(address_family, address, buf);
1114 static int try_provision_service(struct connman_config_service *config,
1115 struct connman_service *service)
1117 struct connman_network *network;
1118 const void *service_id;
1119 enum connman_service_type type;
1121 unsigned int ssid_len;
1123 network = __connman_service_get_network(service);
1125 connman_error("Service has no network set");
1129 DBG("network %p ident %s", network,
1130 connman_network_get_identifier(network));
1132 type = connman_service_get_type(service);
1135 case CONNMAN_SERVICE_TYPE_WIFI:
1136 if (__connman_service_string2type(config->type) != type)
1139 ssid = connman_network_get_blob(network, "WiFi.SSID",
1142 connman_error("Network SSID not set");
1146 if (!config->ssid || ssid_len != config->ssid_len)
1149 if (memcmp(config->ssid, ssid, ssid_len))
1154 case CONNMAN_SERVICE_TYPE_ETHERNET:
1155 case CONNMAN_SERVICE_TYPE_GADGET:
1157 if (__connman_service_string2type(config->type) != type)
1162 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1163 case CONNMAN_SERVICE_TYPE_SYSTEM:
1164 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1165 case CONNMAN_SERVICE_TYPE_CELLULAR:
1166 case CONNMAN_SERVICE_TYPE_GPS:
1167 case CONNMAN_SERVICE_TYPE_VPN:
1168 case CONNMAN_SERVICE_TYPE_P2P:
1173 DBG("service %p ident %s", service,
1174 __connman_service_get_ident(service));
1177 struct connman_device *device;
1178 const char *device_addr;
1180 device = connman_network_get_device(network);
1182 connman_error("Network device is missing");
1186 device_addr = connman_device_get_string(device, "Address");
1188 DBG("wants %s has %s", config->mac, device_addr);
1190 if (g_ascii_strcasecmp(device_addr, config->mac) != 0)
1194 if (!config->ipv6_address) {
1195 connman_network_set_ipv6_method(network,
1196 CONNMAN_IPCONFIG_METHOD_AUTO);
1197 } else if (g_ascii_strcasecmp(config->ipv6_address, "off") == 0) {
1198 connman_network_set_ipv6_method(network,
1199 CONNMAN_IPCONFIG_METHOD_OFF);
1200 } else if (g_ascii_strcasecmp(config->ipv6_address, "auto") == 0 ||
1201 g_ascii_strcasecmp(config->ipv6_address, "dhcp") == 0) {
1202 connman_network_set_ipv6_method(network,
1203 CONNMAN_IPCONFIG_METHOD_AUTO);
1205 struct connman_ipaddress *address;
1207 if (config->ipv6_prefix_length == 0) {
1208 DBG("IPv6 prefix missing");
1212 address = connman_ipaddress_alloc(AF_INET6);
1216 connman_ipaddress_set_ipv6(address, config->ipv6_address,
1217 config->ipv6_prefix_length,
1218 config->ipv6_gateway);
1220 connman_network_set_ipv6_method(network,
1221 CONNMAN_IPCONFIG_METHOD_FIXED);
1223 if (connman_network_set_ipaddress(network, address) < 0)
1224 DBG("Unable to set IPv6 address to network %p",
1227 connman_ipaddress_free(address);
1230 if (config->ipv6_privacy) {
1231 struct connman_ipconfig *ipconfig;
1233 ipconfig = __connman_service_get_ip6config(service);
1235 __connman_ipconfig_ipv6_set_privacy(ipconfig,
1236 config->ipv6_privacy);
1239 if (!config->ipv4_address) {
1240 connman_network_set_ipv4_method(network,
1241 CONNMAN_IPCONFIG_METHOD_DHCP);
1242 } else if (g_ascii_strcasecmp(config->ipv4_address, "off") == 0) {
1243 connman_network_set_ipv4_method(network,
1244 CONNMAN_IPCONFIG_METHOD_OFF);
1245 } else if (g_ascii_strcasecmp(config->ipv4_address, "auto") == 0 ||
1246 g_ascii_strcasecmp(config->ipv4_address, "dhcp") == 0) {
1247 connman_network_set_ipv4_method(network,
1248 CONNMAN_IPCONFIG_METHOD_DHCP);
1250 struct connman_ipaddress *address;
1252 if (!config->ipv4_netmask) {
1253 DBG("IPv4 netmask missing");
1257 address = connman_ipaddress_alloc(AF_INET);
1261 connman_ipaddress_set_ipv4(address, config->ipv4_address,
1262 config->ipv4_netmask,
1263 config->ipv4_gateway);
1265 connman_network_set_ipv4_method(network,
1266 CONNMAN_IPCONFIG_METHOD_FIXED);
1268 if (connman_network_set_ipaddress(network, address) < 0)
1269 DBG("Unable to set IPv4 address to network %p",
1272 connman_ipaddress_free(address);
1275 __connman_service_disconnect(service);
1277 service_id = __connman_service_get_ident(service);
1278 config->service_identifiers =
1279 g_slist_prepend(config->service_identifiers,
1280 g_strdup(service_id));
1282 __connman_service_set_favorite_delayed(service, true, true);
1284 __connman_service_set_config(service, config->config_ident,
1285 config->config_entry);
1287 if (config->domain_name)
1288 __connman_service_set_domainname(service, config->domain_name);
1290 if (config->nameservers) {
1293 __connman_service_nameserver_clear(service);
1295 for (i = 0; config->nameservers[i]; i++) {
1296 #if defined TIZEN_EXT
1297 if (__check_address_type(AF_INET, config->nameservers[i]))
1298 __connman_service_nameserver_append(service,
1299 config->nameservers[i], false,
1300 CONNMAN_IPCONFIG_TYPE_IPV4);
1301 else if (__check_address_type(AF_INET6, config->nameservers[i]))
1302 __connman_service_nameserver_append(service,
1303 config->nameservers[i], false,
1304 CONNMAN_IPCONFIG_TYPE_IPV6);
1306 __connman_service_nameserver_append(service,
1307 config->nameservers[i], false);
1312 if (config->search_domains)
1313 __connman_service_set_search_domains(service,
1314 config->search_domains);
1316 if (config->timeservers)
1317 __connman_service_set_timeservers(service,
1318 config->timeservers);
1320 if (type == CONNMAN_SERVICE_TYPE_WIFI) {
1321 provision_service_wifi(config, service, network,
1325 __connman_service_mark_dirty();
1327 __connman_service_load_modifiable(service);
1329 if (config->virtual) {
1330 struct connect_virtual *virtual;
1332 virtual = g_malloc0(sizeof(struct connect_virtual));
1333 virtual->service = service;
1334 virtual->vfile = config->virtual_file;
1336 g_timeout_add(0, remove_virtual_config, virtual);
1341 __connman_service_set_immutable(service, true);
1343 if (type == CONNMAN_SERVICE_TYPE_ETHERNET ||
1344 type == CONNMAN_SERVICE_TYPE_GADGET) {
1345 __connman_service_connect(service,
1346 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1351 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1356 static int find_and_provision_service(struct connman_service *service)
1358 GHashTableIter iter, iter_service;
1359 gpointer value, key, value_service, key_service;
1361 g_hash_table_iter_init(&iter, config_table);
1363 while (g_hash_table_iter_next(&iter, &key, &value)) {
1364 struct connman_config *config = value;
1366 g_hash_table_iter_init(&iter_service, config->service_table);
1367 while (g_hash_table_iter_next(&iter_service, &key_service,
1369 if (!try_provision_service(value_service, service))
1377 int __connman_config_provision_service(struct connman_service *service)
1379 enum connman_service_type type;
1381 /* For now only WiFi, Gadget and Ethernet services are supported */
1382 type = connman_service_get_type(service);
1384 DBG("service %p type %d", service, type);
1386 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1387 type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1388 type != CONNMAN_SERVICE_TYPE_GADGET)
1391 #if defined TIZEN_EXT
1392 if(type == CONNMAN_SERVICE_TYPE_WIFI &&
1393 __connman_service_get_security(service) ==
1394 CONNMAN_SERVICE_SECURITY_NONE)
1398 return find_and_provision_service(service);
1401 int __connman_config_provision_service_ident(struct connman_service *service,
1402 const char *ident, const char *file, const char *entry)
1404 enum connman_service_type type;
1405 struct connman_config *config;
1408 /* For now only WiFi, Gadget and Ethernet services are supported */
1409 type = connman_service_get_type(service);
1411 DBG("service %p type %d", service, type);
1413 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1414 type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1415 type != CONNMAN_SERVICE_TYPE_GADGET)
1418 config = g_hash_table_lookup(config_table, ident);
1420 GHashTableIter iter;
1421 gpointer value, key;
1424 g_hash_table_iter_init(&iter, config->service_table);
1427 * Check if we need to remove individual service if it
1428 * is missing from config file.
1430 if (file && entry) {
1431 while (g_hash_table_iter_next(&iter, &key,
1433 struct connman_config_service *config_service;
1435 config_service = value;
1437 if (g_strcmp0(config_service->config_ident,
1441 if (g_strcmp0(config_service->config_entry,
1449 DBG("found %d ident %s file %s entry %s", found, ident,
1454 * The entry+8 will skip "service_" prefix
1456 g_hash_table_remove(config->service_table,
1462 find_and_provision_service(service);
1468 static void generate_random_string(char *str, int length)
1474 memset(str, '\0', length);
1476 for (i = 0; i < length-1; i++) {
1478 __connman_util_get_random(&rand);
1479 val = (uint8_t)(rand % 122);
1482 } while((val > 57 && val < 65) || (val > 90 && val < 97));
1488 int connman_config_provision_mutable_service(GKeyFile *keyfile)
1490 struct connman_config_service *service_config;
1491 struct connman_config *config;
1492 char *vfile, *group;
1497 generate_random_string(rstr, 11);
1499 vfile = g_strdup_printf("service_mutable_%s.config", rstr);
1501 config = create_config(vfile);
1505 if (!load_service_from_keyfile(keyfile, config))
1508 group = g_key_file_get_start_group(keyfile);
1510 service_config = g_hash_table_lookup(config->service_table, group+8);
1511 if (!service_config)
1514 /* Specific to non file based config: */
1515 g_free(service_config->config_ident);
1516 service_config->config_ident = NULL;
1517 g_free(service_config->config_entry);
1518 service_config->config_entry = NULL;
1520 service_config->virtual = true;
1521 service_config->virtual_file = vfile;
1523 __connman_service_provision_changed(vfile);
1525 if (g_strcmp0(service_config->type, "wifi") == 0)
1526 __connman_device_request_scan(CONNMAN_SERVICE_TYPE_WIFI);
1531 DBG("Could not proceed");
1532 g_hash_table_remove(config_table, vfile);
1538 struct connman_config_entry **connman_config_get_entries(const char *type)
1540 GHashTableIter iter_file, iter_config;
1541 gpointer value, key;
1542 struct connman_config_entry **entries = NULL;
1545 g_hash_table_iter_init(&iter_file, config_table);
1546 while (g_hash_table_iter_next(&iter_file, &key, &value)) {
1547 struct connman_config *config_file = value;
1549 count = g_hash_table_size(config_file->service_table);
1551 entries = g_try_realloc(entries, (i + count + 1) *
1552 sizeof(struct connman_config_entry *));
1556 g_hash_table_iter_init(&iter_config,
1557 config_file->service_table);
1558 while (g_hash_table_iter_next(&iter_config, &key,
1560 struct connman_config_service *config = value;
1563 g_strcmp0(config->type, type) != 0)
1566 entries[i] = g_try_new0(struct connman_config_entry,
1571 entries[i]->ident = g_strdup(config->ident);
1572 entries[i]->name = g_strdup(config->name);
1573 entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
1574 if (!entries[i]->ssid)
1577 memcpy(entries[i]->ssid, config->ssid,
1579 entries[i]->ssid_len = config->ssid_len;
1580 entries[i]->hidden = config->hidden;
1587 entries = g_try_realloc(entries, (i + 1) *
1588 sizeof(struct connman_config_entry *));
1594 DBG("%d provisioned AP found", i);
1600 connman_config_free_entries(entries);
1604 void connman_config_free_entries(struct connman_config_entry **entries)
1611 for (i = 0; entries[i]; i++) {
1612 g_free(entries[i]->ident);
1613 g_free(entries[i]->name);
1614 g_free(entries[i]->ssid);
1622 bool __connman_config_address_provisioned(const char *address,
1623 const char *netmask)
1625 GHashTableIter iter, siter;
1626 gpointer value, key, svalue, skey;
1628 if (!address || !netmask)
1631 g_hash_table_iter_init(&iter, config_table);
1633 while (g_hash_table_iter_next(&iter, &key, &value)) {
1634 struct connman_config *config = value;
1636 g_hash_table_iter_init(&siter, config->service_table);
1637 while (g_hash_table_iter_next(&siter, &skey, &svalue)) {
1638 struct connman_config_service *service = svalue;
1640 if (!g_strcmp0(address, service->ipv4_address) &&
1642 service->ipv4_netmask))