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 security == CONNMAN_SERVICE_SECURITY_WEP) {
689 service->security = security;
691 connman_info("Mismatch with passphrase and "
693 SERVICE_KEY_SECURITY, str);
696 CONNMAN_SERVICE_SECURITY_PSK;
700 service->security = CONNMAN_SERVICE_SECURITY_PSK;
703 service->config_ident = g_strdup(config->ident);
704 service->config_entry = g_strdup_printf("service_%s", service->ident);
706 service->hidden = __connman_config_get_bool(keyfile, group,
707 SERVICE_KEY_HIDDEN, NULL);
710 g_hash_table_insert(config->service_table, service->ident,
713 connman_info("Adding service configuration %s", service->ident);
718 if (service_created) {
719 g_free(service->ident);
720 g_free(service->type);
721 g_free(service->name);
722 g_free(service->ssid);
729 static bool load_service_from_keyfile(GKeyFile *keyfile,
730 struct connman_config *config)
736 groups = g_key_file_get_groups(keyfile, NULL);
738 for (i = 0; groups[i]; i++) {
739 if (!g_str_has_prefix(groups[i], "service_"))
741 if (load_service(keyfile, groups[i], config))
750 static int load_config(struct connman_config *config)
755 DBG("config %p", config);
757 keyfile = __connman_storage_load_config(config->ident);
761 g_key_file_set_list_separator(keyfile, ',');
763 /* Verify keys validity of the global section */
764 check_keys(keyfile, "global", config_possible_keys);
766 str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
768 g_free(config->name);
772 str = __connman_config_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
774 g_free(config->description);
775 config->description = str;
778 if (!load_service_from_keyfile(keyfile, config))
779 connman_warn("Config file %s/%s.config does not contain any "
780 "configuration that can be provisioned!",
781 STORAGEDIR, config->ident);
783 g_key_file_free(keyfile);
788 static struct connman_config *create_config(const char *ident)
790 struct connman_config *config;
792 DBG("ident %s", ident);
794 if (g_hash_table_lookup(config_table, ident))
797 config = g_try_new0(struct connman_config, 1);
801 config->ident = g_strdup(ident);
803 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
804 NULL, unregister_service);
806 g_hash_table_insert(config_table, config->ident, config);
808 connman_info("Adding configuration %s", config->ident);
813 static bool validate_ident(const char *ident)
820 for (i = 0; i < strlen(ident); i++)
821 if (!g_ascii_isprint(ident[i]))
827 static int read_configs(void)
833 dir = g_dir_open(STORAGEDIR, 0, NULL);
837 while ((file = g_dir_read_name(dir))) {
841 if (!g_str_has_suffix(file, ".config"))
844 ident = g_strrstr(file, ".config");
848 str = g_string_new_len(file, ident - file);
852 ident = g_string_free(str, FALSE);
854 if (validate_ident(ident)) {
855 struct connman_config *config;
857 config = create_config(ident);
861 connman_error("Invalid config ident %s", ident);
872 static void config_notify_handler(struct inotify_event *event,
880 if (!g_str_has_suffix(ident, ".config"))
883 ext = g_strrstr(ident, ".config");
889 if (!validate_ident(ident)) {
890 connman_error("Invalid config ident %s", ident);
894 if (event->mask & IN_CREATE)
895 create_config(ident);
897 if (event->mask & IN_MODIFY) {
898 struct connman_config *config;
900 config = g_hash_table_lookup(config_table, ident);
904 g_hash_table_remove_all(config->service_table);
906 ret = __connman_service_provision_changed(ident);
909 * Re-scan the config file for any
912 g_hash_table_remove_all(config->service_table);
914 __connman_service_provision_changed(ident);
919 if (event->mask & IN_DELETE)
920 g_hash_table_remove(config_table, ident);
923 int __connman_config_init(void)
927 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
928 NULL, unregister_config);
930 connman_inotify_register(STORAGEDIR, config_notify_handler);
932 return read_configs();
935 void __connman_config_cleanup(void)
941 connman_inotify_unregister(STORAGEDIR, config_notify_handler);
943 g_hash_table_destroy(config_table);
949 char *__connman_config_get_string(GKeyFile *key_file,
950 const char *group_name, const char *key, GError **error)
952 char *str = g_key_file_get_string(key_file, group_name, key, error);
956 return g_strchomp(str);
959 char **__connman_config_get_string_list(GKeyFile *key_file,
960 const char *group_name, const char *key, gsize *length, GError **error)
963 char **strlist = g_key_file_get_string_list(key_file, group_name, key,
977 bool __connman_config_get_bool(GKeyFile *key_file,
978 const char *group_name, const char *key, GError **error)
983 valstr = g_key_file_get_value(key_file, group_name, key, error);
987 valstr = g_strchomp(valstr);
988 if (strcmp(valstr, "true") == 0 || strcmp(valstr, "1") == 0)
996 static char *config_pem_fsid(const char *pem_file)
999 unsigned *fsid = (unsigned *) &buf.f_fsid;
1000 unsigned long long fsid64;
1005 if (statfs(pem_file, &buf) < 0) {
1006 connman_error("statfs error %s for %s",
1007 strerror(errno), pem_file);
1011 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
1013 return g_strdup_printf("%llx", fsid64);
1016 static void provision_service_wifi(struct connman_config_service *config,
1017 struct connman_service *service,
1018 struct connman_network *network,
1019 const void *ssid, unsigned int ssid_len)
1022 __connman_service_set_string(service, "EAP", config->eap);
1024 if (config->identity)
1025 __connman_service_set_string(service, "Identity",
1028 if (config->ca_cert_file)
1029 __connman_service_set_string(service, "CACertFile",
1030 config->ca_cert_file);
1032 if (config->client_cert_file)
1033 __connman_service_set_string(service, "ClientCertFile",
1034 config->client_cert_file);
1036 if (config->private_key_file)
1037 __connman_service_set_string(service, "PrivateKeyFile",
1038 config->private_key_file);
1040 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
1041 config->private_key_file) {
1044 fsid = config_pem_fsid(config->private_key_file);
1048 g_free(config->private_key_passphrase);
1049 config->private_key_passphrase = fsid;
1052 if (config->private_key_passphrase) {
1053 __connman_service_set_string(service, "PrivateKeyPassphrase",
1054 config->private_key_passphrase);
1056 * TODO: Support for PEAP with both identity and key passwd.
1057 * In that case, we should check if both of them are found
1058 * from the config file. If not, we should not set the
1059 * service passphrase in order for the UI to request for an
1060 * additional passphrase.
1065 __connman_service_set_string(service, "Phase2", config->phase2);
1067 if (config->passphrase)
1068 __connman_service_set_string(service, "Passphrase",
1069 config->passphrase);
1072 __connman_service_set_hidden(service);
1075 struct connect_virtual {
1076 struct connman_service *service;
1080 static gboolean remove_virtual_config(gpointer user_data)
1082 struct connect_virtual *virtual = user_data;
1084 __connman_service_connect(virtual->service,
1085 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1086 g_hash_table_remove(config_table, virtual->vfile);
1093 static int try_provision_service(struct connman_config_service *config,
1094 struct connman_service *service)
1096 struct connman_network *network;
1097 const void *service_id;
1098 enum connman_service_type type;
1100 unsigned int ssid_len;
1103 network = __connman_service_get_network(service);
1105 connman_error("Service has no network set");
1109 DBG("network %p ident %s", network,
1110 connman_network_get_identifier(network));
1112 type = connman_service_get_type(service);
1115 case CONNMAN_SERVICE_TYPE_WIFI:
1116 if (__connman_service_string2type(config->type) != type)
1119 ssid = connman_network_get_blob(network, "WiFi.SSID",
1122 connman_error("Network SSID not set");
1126 if (!config->ssid || ssid_len != config->ssid_len)
1129 if (memcmp(config->ssid, ssid, ssid_len))
1132 str = connman_network_get_string(network, "WiFi.Security");
1133 if (config->security != __connman_service_string2security(str))
1138 case CONNMAN_SERVICE_TYPE_ETHERNET:
1139 case CONNMAN_SERVICE_TYPE_GADGET:
1141 if (__connman_service_string2type(config->type) != type)
1146 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1147 case CONNMAN_SERVICE_TYPE_SYSTEM:
1148 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1149 case CONNMAN_SERVICE_TYPE_CELLULAR:
1150 case CONNMAN_SERVICE_TYPE_GPS:
1151 case CONNMAN_SERVICE_TYPE_VPN:
1152 case CONNMAN_SERVICE_TYPE_P2P:
1157 DBG("service %p ident %s", service,
1158 __connman_service_get_ident(service));
1161 struct connman_device *device;
1162 const char *device_addr;
1164 device = connman_network_get_device(network);
1166 connman_error("Network device is missing");
1170 device_addr = connman_device_get_string(device, "Address");
1172 DBG("wants %s has %s", config->mac, device_addr);
1174 if (g_ascii_strcasecmp(device_addr, config->mac) != 0)
1178 if (!config->ipv6_address) {
1179 connman_network_set_ipv6_method(network,
1180 CONNMAN_IPCONFIG_METHOD_AUTO);
1181 } else if (g_ascii_strcasecmp(config->ipv6_address, "off") == 0) {
1182 connman_network_set_ipv6_method(network,
1183 CONNMAN_IPCONFIG_METHOD_OFF);
1184 } else if (g_ascii_strcasecmp(config->ipv6_address, "auto") == 0 ||
1185 g_ascii_strcasecmp(config->ipv6_address, "dhcp") == 0) {
1186 connman_network_set_ipv6_method(network,
1187 CONNMAN_IPCONFIG_METHOD_AUTO);
1189 struct connman_ipaddress *address;
1191 if (config->ipv6_prefix_length == 0) {
1192 DBG("IPv6 prefix missing");
1196 address = connman_ipaddress_alloc(AF_INET6);
1200 connman_ipaddress_set_ipv6(address, config->ipv6_address,
1201 config->ipv6_prefix_length,
1202 config->ipv6_gateway);
1204 connman_network_set_ipv6_method(network,
1205 CONNMAN_IPCONFIG_METHOD_FIXED);
1207 if (connman_network_set_ipaddress(network, address) < 0)
1208 DBG("Unable to set IPv6 address to network %p",
1211 connman_ipaddress_free(address);
1214 if (config->ipv6_privacy) {
1215 struct connman_ipconfig *ipconfig;
1217 ipconfig = __connman_service_get_ip6config(service);
1219 __connman_ipconfig_ipv6_set_privacy(ipconfig,
1220 config->ipv6_privacy);
1223 if (!config->ipv4_address) {
1224 connman_network_set_ipv4_method(network,
1225 CONNMAN_IPCONFIG_METHOD_DHCP);
1226 } else if (g_ascii_strcasecmp(config->ipv4_address, "off") == 0) {
1227 connman_network_set_ipv4_method(network,
1228 CONNMAN_IPCONFIG_METHOD_OFF);
1229 } else if (g_ascii_strcasecmp(config->ipv4_address, "auto") == 0 ||
1230 g_ascii_strcasecmp(config->ipv4_address, "dhcp") == 0) {
1231 connman_network_set_ipv4_method(network,
1232 CONNMAN_IPCONFIG_METHOD_DHCP);
1234 struct connman_ipaddress *address;
1236 if (!config->ipv4_netmask) {
1237 DBG("IPv4 netmask missing");
1241 address = connman_ipaddress_alloc(AF_INET);
1245 connman_ipaddress_set_ipv4(address, config->ipv4_address,
1246 config->ipv4_netmask,
1247 config->ipv4_gateway);
1249 connman_network_set_ipv4_method(network,
1250 CONNMAN_IPCONFIG_METHOD_FIXED);
1252 if (connman_network_set_ipaddress(network, address) < 0)
1253 DBG("Unable to set IPv4 address to network %p",
1256 connman_ipaddress_free(address);
1259 __connman_service_disconnect(service);
1261 service_id = __connman_service_get_ident(service);
1262 config->service_identifiers =
1263 g_slist_prepend(config->service_identifiers,
1264 g_strdup(service_id));
1266 __connman_service_set_favorite_delayed(service, true, true);
1268 __connman_service_set_config(service, config->config_ident,
1269 config->config_entry);
1271 if (config->domain_name)
1272 __connman_service_set_domainname(service, config->domain_name);
1274 if (config->nameservers) {
1277 __connman_service_nameserver_clear(service);
1279 for (i = 0; config->nameservers[i]; i++) {
1280 __connman_service_nameserver_append(service,
1281 config->nameservers[i], false);
1285 if (config->search_domains)
1286 __connman_service_set_search_domains(service,
1287 config->search_domains);
1289 if (config->timeservers)
1290 __connman_service_set_timeservers(service,
1291 config->timeservers);
1293 if (type == CONNMAN_SERVICE_TYPE_WIFI) {
1294 provision_service_wifi(config, service, network,
1298 __connman_service_mark_dirty();
1300 __connman_service_load_modifiable(service);
1302 if (config->virtual) {
1303 struct connect_virtual *virtual;
1305 virtual = g_malloc0(sizeof(struct connect_virtual));
1306 virtual->service = service;
1307 virtual->vfile = config->virtual_file;
1309 g_timeout_add(0, remove_virtual_config, virtual);
1314 __connman_service_set_immutable(service, true);
1316 if (type == CONNMAN_SERVICE_TYPE_ETHERNET ||
1317 type == CONNMAN_SERVICE_TYPE_GADGET) {
1318 __connman_service_connect(service,
1319 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1324 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1329 static int find_and_provision_service(struct connman_service *service)
1331 GHashTableIter iter, iter_service;
1332 gpointer value, key, value_service, key_service;
1334 g_hash_table_iter_init(&iter, config_table);
1336 while (g_hash_table_iter_next(&iter, &key, &value)) {
1337 struct connman_config *config = value;
1339 g_hash_table_iter_init(&iter_service, config->service_table);
1340 while (g_hash_table_iter_next(&iter_service, &key_service,
1342 if (!try_provision_service(value_service, service))
1350 int __connman_config_provision_service(struct connman_service *service)
1352 enum connman_service_type type;
1354 /* For now only WiFi, Gadget and Ethernet services are supported */
1355 type = connman_service_get_type(service);
1357 DBG("service %p type %d", service, type);
1359 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1360 type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1361 type != CONNMAN_SERVICE_TYPE_GADGET)
1364 return find_and_provision_service(service);
1367 int __connman_config_provision_service_ident(struct connman_service *service,
1368 const char *ident, const char *file, const char *entry)
1370 enum connman_service_type type;
1371 struct connman_config *config;
1374 /* For now only WiFi, Gadget and Ethernet services are supported */
1375 type = connman_service_get_type(service);
1377 DBG("service %p type %d", service, type);
1379 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1380 type != CONNMAN_SERVICE_TYPE_ETHERNET &&
1381 type != CONNMAN_SERVICE_TYPE_GADGET)
1384 config = g_hash_table_lookup(config_table, ident);
1386 GHashTableIter iter;
1387 gpointer value, key;
1390 g_hash_table_iter_init(&iter, config->service_table);
1393 * Check if we need to remove individual service if it
1394 * is missing from config file.
1396 if (file && entry) {
1397 while (g_hash_table_iter_next(&iter, &key,
1399 struct connman_config_service *config_service;
1401 config_service = value;
1403 if (g_strcmp0(config_service->config_ident,
1407 if (g_strcmp0(config_service->config_entry,
1415 DBG("found %d ident %s file %s entry %s", found, ident,
1420 * The entry+8 will skip "service_" prefix
1422 g_hash_table_remove(config->service_table,
1428 find_and_provision_service(service);
1434 static void generate_random_string(char *str, int length)
1439 memset(str, '\0', length);
1441 for (i = 0; i < length-1; i++) {
1443 val = (uint8_t)(random() % 122);
1446 } while((val > 57 && val < 65) || (val > 90 && val < 97));
1452 int connman_config_provision_mutable_service(GKeyFile *keyfile)
1454 struct connman_config_service *service_config;
1455 struct connman_config *config;
1456 char *vfile, *group;
1461 generate_random_string(rstr, 11);
1463 vfile = g_strdup_printf("service_mutable_%s.config", rstr);
1465 config = create_config(vfile);
1469 if (!load_service_from_keyfile(keyfile, config))
1472 group = g_key_file_get_start_group(keyfile);
1474 service_config = g_hash_table_lookup(config->service_table, group+8);
1475 if (!service_config)
1478 /* Specific to non file based config: */
1479 g_free(service_config->config_ident);
1480 service_config->config_ident = NULL;
1481 g_free(service_config->config_entry);
1482 service_config->config_entry = NULL;
1484 service_config->virtual = true;
1485 service_config->virtual_file = vfile;
1487 __connman_service_provision_changed(vfile);
1489 if (g_strcmp0(service_config->type, "wifi") == 0)
1490 __connman_device_request_scan(CONNMAN_SERVICE_TYPE_WIFI);
1495 DBG("Could not proceed");
1496 g_hash_table_remove(config_table, vfile);
1502 struct connman_config_entry **connman_config_get_entries(const char *type)
1504 GHashTableIter iter_file, iter_config;
1505 gpointer value, key;
1506 struct connman_config_entry **entries = NULL;
1509 g_hash_table_iter_init(&iter_file, config_table);
1510 while (g_hash_table_iter_next(&iter_file, &key, &value)) {
1511 struct connman_config *config_file = value;
1513 count = g_hash_table_size(config_file->service_table);
1515 entries = g_try_realloc(entries, (i + count + 1) *
1516 sizeof(struct connman_config_entry *));
1520 g_hash_table_iter_init(&iter_config,
1521 config_file->service_table);
1522 while (g_hash_table_iter_next(&iter_config, &key,
1524 struct connman_config_service *config = value;
1527 g_strcmp0(config->type, type) != 0)
1530 entries[i] = g_try_new0(struct connman_config_entry,
1535 entries[i]->ident = g_strdup(config->ident);
1536 entries[i]->name = g_strdup(config->name);
1537 entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
1538 if (!entries[i]->ssid)
1541 memcpy(entries[i]->ssid, config->ssid,
1543 entries[i]->ssid_len = config->ssid_len;
1544 entries[i]->hidden = config->hidden;
1551 entries = g_try_realloc(entries, (i + 1) *
1552 sizeof(struct connman_config_entry *));
1558 DBG("%d provisioned AP found", i);
1564 connman_config_free_entries(entries);
1568 void connman_config_free_entries(struct connman_config_entry **entries)
1575 for (i = 0; entries[i]; i++) {
1576 g_free(entries[i]->ident);
1577 g_free(entries[i]->name);
1578 g_free(entries[i]->ssid);
1586 bool __connman_config_address_provisioned(const char *address,
1587 const char *netmask)
1589 GHashTableIter iter, siter;
1590 gpointer value, key, svalue, skey;
1592 if (!address || !netmask)
1595 g_hash_table_iter_init(&iter, config_table);
1597 while (g_hash_table_iter_next(&iter, &key, &value)) {
1598 struct connman_config *config = value;
1600 g_hash_table_iter_init(&siter, config->service_table);
1601 while (g_hash_table_iter_next(&siter, &skey, &svalue)) {
1602 struct connman_config_service *service = svalue;
1604 if (!g_strcmp0(address, service->ipv4_address) &&
1606 service->ipv4_netmask))