5 * Copyright (C) 2007-2012 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 GSList *service_identifiers;
56 char *config_ident; /* file prefix */
57 char *config_entry; /* entry name */
58 connman_bool_t hidden;
59 connman_bool_t virtual;
65 unsigned char ipv6_prefix_length;
70 char **search_domains;
75 struct connman_config {
79 connman_bool_t protected;
80 GHashTable *service_table;
83 static GHashTable *config_table = NULL;
84 static GSList *protected_services = NULL;
86 static connman_bool_t cleanup = FALSE;
88 /* Definition of possible strings in the .config files */
89 #define CONFIG_KEY_NAME "Name"
90 #define CONFIG_KEY_DESC "Description"
91 #define CONFIG_KEY_PROT "Protected"
93 #define SERVICE_KEY_TYPE "Type"
94 #define SERVICE_KEY_NAME "Name"
95 #define SERVICE_KEY_SSID "SSID"
96 #define SERVICE_KEY_EAP "EAP"
97 #define SERVICE_KEY_CA_CERT "CACertFile"
98 #define SERVICE_KEY_CL_CERT "ClientCertFile"
99 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
100 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
101 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
102 #define SERVICE_KEY_IDENTITY "Identity"
103 #define SERVICE_KEY_PHASE2 "Phase2"
104 #define SERVICE_KEY_PASSPHRASE "Passphrase"
105 #define SERVICE_KEY_HIDDEN "Hidden"
107 #define SERVICE_KEY_IPv4 "IPv4"
108 #define SERVICE_KEY_IPv6 "IPv6"
109 #define SERVICE_KEY_IPv6_PRIVACY "IPv6.Privacy"
110 #define SERVICE_KEY_MAC "MAC"
111 #define SERVICE_KEY_NAMESERVERS "Nameservers"
112 #define SERVICE_KEY_SEARCH_DOMAINS "SearchDomains"
113 #define SERVICE_KEY_TIMESERVERS "Timeservers"
114 #define SERVICE_KEY_DOMAIN "Domain"
116 static const char *config_possible_keys[] = {
123 static const char *service_possible_keys[] = {
131 SERVICE_KEY_PRV_KEY_PASS,
132 SERVICE_KEY_PRV_KEY_PASS_TYPE,
133 SERVICE_KEY_IDENTITY,
135 SERVICE_KEY_PASSPHRASE,
139 SERVICE_KEY_IPv6_PRIVACY,
141 SERVICE_KEY_NAMESERVERS,
142 SERVICE_KEY_SEARCH_DOMAINS,
143 SERVICE_KEY_TIMESERVERS,
148 static void unregister_config(gpointer data)
150 struct connman_config *config = data;
152 connman_info("Removing configuration %s", config->ident);
154 g_hash_table_destroy(config->service_table);
156 g_free(config->description);
157 g_free(config->name);
158 g_free(config->ident);
162 static void unregister_service(gpointer data)
164 struct connman_config_service *config_service = data;
165 struct connman_service *service;
172 connman_info("Removing service configuration %s",
173 config_service->ident);
175 protected_services = g_slist_remove(protected_services,
178 if (config_service->virtual == TRUE)
181 for (list = config_service->service_identifiers; list != NULL;
183 service_id = list->data;
185 service = __connman_service_lookup_from_ident(service_id);
186 if (service != NULL) {
187 __connman_service_set_immutable(service, FALSE);
188 __connman_service_set_config(service, NULL, NULL);
189 __connman_service_remove(service);
192 * Ethernet service cannot be removed by
193 * __connman_service_remove() so reset the ipconfig
196 if (connman_service_get_type(service) ==
197 CONNMAN_SERVICE_TYPE_ETHERNET) {
198 __connman_service_disconnect(service);
199 __connman_service_reset_ipconfig(service,
200 CONNMAN_IPCONFIG_TYPE_IPV4, NULL, NULL);
201 __connman_service_reset_ipconfig(service,
202 CONNMAN_IPCONFIG_TYPE_IPV6, NULL, NULL);
203 __connman_service_set_ignore(service, TRUE);
206 * After these operations, user needs to
207 * reconnect ethernet cable to get IP
213 if (__connman_storage_remove_service(service_id) == FALSE)
214 DBG("Could not remove all files for service %s",
219 g_free(config_service->ident);
220 g_free(config_service->type);
221 g_free(config_service->name);
222 g_free(config_service->ssid);
223 g_free(config_service->eap);
224 g_free(config_service->identity);
225 g_free(config_service->ca_cert_file);
226 g_free(config_service->client_cert_file);
227 g_free(config_service->private_key_file);
228 g_free(config_service->private_key_passphrase);
229 g_free(config_service->private_key_passphrase_type);
230 g_free(config_service->phase2);
231 g_free(config_service->passphrase);
232 g_free(config_service->ipv4_address);
233 g_free(config_service->ipv4_gateway);
234 g_free(config_service->ipv4_netmask);
235 g_free(config_service->ipv6_address);
236 g_free(config_service->ipv6_gateway);
237 g_free(config_service->ipv6_privacy);
238 g_free(config_service->mac);
239 g_strfreev(config_service->nameservers);
240 g_strfreev(config_service->search_domains);
241 g_strfreev(config_service->timeservers);
242 g_free(config_service->domain_name);
243 g_slist_free_full(config_service->service_identifiers, g_free);
244 g_free(config_service->config_ident);
245 g_free(config_service->config_entry);
246 g_free(config_service->virtual_file);
247 g_free(config_service);
250 static void check_keys(GKeyFile *keyfile, const char *group,
251 const char **possible_keys)
254 gsize nb_avail_keys, i, j;
256 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
257 if (avail_keys == NULL)
261 * For each key in the configuration file,
262 * verify it is understood by connman
264 for (i = 0 ; i < nb_avail_keys; i++) {
265 for (j = 0; possible_keys[j] ; j++)
266 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
269 if (possible_keys[j] == NULL)
270 connman_warn("Unknown configuration key %s in [%s]",
271 avail_keys[i], group);
274 g_strfreev(avail_keys);
277 static connman_bool_t
278 is_protected_service(struct connman_config_service *service)
282 DBG("ident %s", service->ident);
284 for (list = protected_services; list; list = list->next) {
285 struct connman_config_service *s = list->data;
287 if (g_strcmp0(s->type, service->type) != 0)
290 if (s->ssid == NULL || service->ssid == NULL)
293 if (s->ssid_len != service->ssid_len)
296 if (g_strcmp0(service->type, "wifi") == 0 &&
297 strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
305 static int check_family(const char *address, int expected_family)
310 family = connman_inet_check_ipaddress(address);
312 DBG("Cannot get address family of %s (%d/%s)", address,
313 family, gai_strerror(family));
320 if (expected_family != AF_INET) {
321 DBG("Wrong type address %s, expecting IPv4", address);
327 if (expected_family != AF_INET6) {
328 DBG("Wrong type address %s, expecting IPv6", address);
334 DBG("Unsupported address family %d", family);
343 static int parse_address(const char *address_str, int address_family,
344 char **address, char **netmask, char **gateway)
346 char *addr_str, *mask_str, *gw_str;
350 route = g_strsplit(address_str, "/", 0);
355 if (addr_str == NULL || addr_str[0] == '\0') {
360 if ((err = check_family(addr_str, address_family)) < 0)
364 if (mask_str == NULL || mask_str[0] == '\0') {
370 if (gw_str == NULL || gw_str[0] == '\0') {
375 if ((err = check_family(gw_str, address_family)) < 0)
379 *address = g_strdup(addr_str);
382 *netmask = g_strdup(mask_str);
385 *gateway = g_strdup(gw_str);
387 DBG("address %s/%s via %s", *address, *netmask, *gateway);
395 static connman_bool_t load_service_generic(GKeyFile *keyfile,
396 const char *group, struct connman_config *config,
397 struct connman_config_service *service)
403 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv4, NULL);
407 if (parse_address(str, AF_INET, &service->ipv4_address,
408 &mask, &service->ipv4_gateway) < 0) {
409 connman_warn("Invalid format for IPv4 address %s",
415 if (g_strrstr(mask, ".") == NULL) {
416 /* We have netmask length */
418 struct in_addr netmask_in;
419 unsigned char prefix_len = 32;
421 long int value = strtol(mask, &ptr, 10);
423 if (ptr != mask && *ptr == '\0' && value <= 32)
426 addr = 0xffffffff << (32 - prefix_len);
427 netmask_in.s_addr = htonl(addr);
428 service->ipv4_netmask =
429 g_strdup(inet_ntoa(netmask_in));
433 service->ipv4_netmask = mask;
438 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv6, NULL);
445 if (parse_address(str, AF_INET6, &service->ipv6_address,
446 &mask, &service->ipv6_gateway) < 0) {
447 connman_warn("Invalid format for IPv6 address %s",
453 value = strtol(mask, &ptr, 10);
454 if (ptr != mask && *ptr == '\0' && value <= 128)
455 service->ipv6_prefix_length = value;
457 service->ipv6_prefix_length = 128;
463 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv6_PRIVACY,
466 g_free(service->ipv6_privacy);
467 service->ipv6_privacy = str;
470 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_MAC, NULL);
472 g_free(service->mac);
476 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_DOMAIN, NULL);
478 g_free(service->domain_name);
479 service->domain_name = str;
482 strlist = g_key_file_get_string_list(keyfile, group,
483 SERVICE_KEY_NAMESERVERS,
485 if (strlist != NULL) {
487 g_strfreev(service->nameservers);
488 service->nameservers = strlist;
493 strlist = g_key_file_get_string_list(keyfile, group,
494 SERVICE_KEY_SEARCH_DOMAINS,
496 if (strlist != NULL) {
498 g_strfreev(service->search_domains);
499 service->search_domains = strlist;
504 strlist = g_key_file_get_string_list(keyfile, group,
505 SERVICE_KEY_TIMESERVERS,
507 if (strlist != NULL) {
509 g_strfreev(service->timeservers);
510 service->timeservers = strlist;
518 g_free(service->ident);
519 g_free(service->type);
520 g_free(service->ipv4_address);
521 g_free(service->ipv4_netmask);
522 g_free(service->ipv4_gateway);
523 g_free(service->ipv6_address);
524 g_free(service->ipv6_gateway);
525 g_free(service->mac);
531 static connman_bool_t load_service(GKeyFile *keyfile, const char *group,
532 struct connman_config *config)
534 struct connman_config_service *service;
536 char *str, *hex_ssid;
537 gboolean service_created = FALSE;
539 /* Strip off "service_" prefix */
542 if (strlen(ident) < 1)
545 /* Verify that provided keys are good */
546 check_keys(keyfile, group, service_possible_keys);
548 service = g_hash_table_lookup(config->service_table, ident);
549 if (service == NULL) {
550 service = g_try_new0(struct connman_config_service, 1);
554 service->ident = g_strdup(ident);
556 service_created = TRUE;
559 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
561 g_free(service->type);
564 DBG("Type of the configured service is missing for group %s",
569 if (load_service_generic(keyfile, group, config, service) == FALSE)
572 if (g_strcmp0(str, "ethernet") == 0) {
573 service->config_ident = g_strdup(config->ident);
574 service->config_entry = g_strdup_printf("service_%s",
577 g_hash_table_insert(config->service_table, service->ident,
582 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
584 g_free(service->name);
588 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
590 if (hex_ssid != NULL) {
592 unsigned int i, j = 0, hex;
593 size_t hex_ssid_len = strlen(hex_ssid);
595 ssid = g_try_malloc0(hex_ssid_len / 2);
601 for (i = 0; i < hex_ssid_len; i += 2) {
602 if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
603 connman_warn("Invalid SSID %s", hex_ssid);
613 g_free(service->ssid);
614 service->ssid = ssid;
615 service->ssid_len = hex_ssid_len / 2;
616 } else if (service->name != NULL) {
618 unsigned int ssid_len;
620 ssid_len = strlen(service->name);
621 ssid = g_try_malloc0(ssid_len);
625 memcpy(ssid, service->name, ssid_len);
626 g_free(service->ssid);
627 service->ssid = ssid;
628 service->ssid_len = ssid_len;
631 if (is_protected_service(service) == TRUE) {
632 connman_error("Trying to provision a protected service");
636 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
638 g_free(service->eap);
642 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
644 g_free(service->ca_cert_file);
645 service->ca_cert_file = str;
648 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
650 g_free(service->client_cert_file);
651 service->client_cert_file = str;
654 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
656 g_free(service->private_key_file);
657 service->private_key_file = str;
660 str = g_key_file_get_string(keyfile, group,
661 SERVICE_KEY_PRV_KEY_PASS, NULL);
663 g_free(service->private_key_passphrase);
664 service->private_key_passphrase = str;
667 str = g_key_file_get_string(keyfile, group,
668 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
670 g_free(service->private_key_passphrase_type);
671 service->private_key_passphrase_type = str;
674 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
676 g_free(service->identity);
677 service->identity = str;
680 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
682 g_free(service->phase2);
683 service->phase2 = str;
686 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
689 g_free(service->passphrase);
690 service->passphrase = str;
693 service->config_ident = g_strdup(config->ident);
694 service->config_entry = g_strdup_printf("service_%s", service->ident);
696 service->hidden = g_key_file_get_boolean(keyfile, group,
697 SERVICE_KEY_HIDDEN, NULL);
700 g_hash_table_insert(config->service_table, service->ident,
703 if (config->protected == TRUE)
705 g_slist_prepend(protected_services, service);
707 connman_info("Adding service configuration %s", service->ident);
712 if (service_created == TRUE) {
713 g_free(service->ident);
714 g_free(service->type);
715 g_free(service->name);
716 g_free(service->ssid);
723 static connman_bool_t load_service_from_keyfile(GKeyFile *keyfile,
724 struct connman_config *config)
726 connman_bool_t found = FALSE;
730 groups = g_key_file_get_groups(keyfile, NULL);
732 for (i = 0; groups[i] != NULL; i++) {
733 if (g_str_has_prefix(groups[i], "service_") == FALSE)
735 if (load_service(keyfile, groups[i], config) == TRUE)
744 static int load_config(struct connman_config *config)
746 GError *error = NULL;
751 DBG("config %p", config);
753 keyfile = __connman_storage_load_config(config->ident);
757 /* Verify keys validity of the global section */
758 check_keys(keyfile, "global", config_possible_keys);
760 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
762 g_free(config->name);
766 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
768 g_free(config->description);
769 config->description = str;
772 protected = g_key_file_get_boolean(keyfile, "global",
773 CONFIG_KEY_PROT, &error);
775 config->protected = protected;
777 config->protected = TRUE;
778 g_clear_error(&error);
780 if (load_service_from_keyfile(keyfile, config) == FALSE)
781 connman_warn("Config file %s/%s.config does not contain any "
782 "configuration that can be provisioned!",
783 STORAGEDIR, config->ident);
785 g_key_file_free(keyfile);
790 static struct connman_config *create_config(const char *ident)
792 struct connman_config *config;
794 DBG("ident %s", ident);
796 if (g_hash_table_lookup(config_table, ident) != NULL)
799 config = g_try_new0(struct connman_config, 1);
803 config->ident = g_strdup(ident);
805 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
806 NULL, unregister_service);
808 g_hash_table_insert(config_table, config->ident, config);
810 connman_info("Adding configuration %s", config->ident);
815 static connman_bool_t validate_ident(const char *ident)
822 for (i = 0; i < strlen(ident); i++)
823 if (g_ascii_isprint(ident[i]) == FALSE)
829 static int read_configs(void)
835 dir = g_dir_open(STORAGEDIR, 0, NULL);
839 while ((file = g_dir_read_name(dir)) != NULL) {
843 if (g_str_has_suffix(file, ".config") == FALSE)
846 ident = g_strrstr(file, ".config");
850 str = g_string_new_len(file, ident - file);
854 ident = g_string_free(str, FALSE);
856 if (validate_ident(ident) == TRUE) {
857 struct connman_config *config;
859 config = create_config(ident);
863 connman_error("Invalid config ident %s", ident);
874 static void config_notify_handler(struct inotify_event *event,
882 if (g_str_has_suffix(ident, ".config") == FALSE)
885 ext = g_strrstr(ident, ".config");
891 if (validate_ident(ident) == FALSE) {
892 connman_error("Invalid config ident %s", ident);
896 if (event->mask & IN_CREATE)
897 create_config(ident);
899 if (event->mask & IN_MODIFY) {
900 struct connman_config *config;
902 config = g_hash_table_lookup(config_table, ident);
903 if (config != NULL) {
906 g_hash_table_remove_all(config->service_table);
908 ret = __connman_service_provision_changed(ident);
911 * Re-scan the config file for any
914 g_hash_table_remove_all(config->service_table);
916 __connman_service_provision_changed(ident);
921 if (event->mask & IN_DELETE)
922 g_hash_table_remove(config_table, ident);
925 int __connman_config_init(void)
929 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
930 NULL, unregister_config);
932 connman_inotify_register(STORAGEDIR, config_notify_handler);
934 return read_configs();
937 void __connman_config_cleanup(void)
943 connman_inotify_unregister(STORAGEDIR, config_notify_handler);
945 g_hash_table_destroy(config_table);
951 static char *config_pem_fsid(const char *pem_file)
954 unsigned *fsid = (unsigned *) &buf.f_fsid;
955 unsigned long long fsid64;
957 if (pem_file == NULL)
960 if (statfs(pem_file, &buf) < 0) {
961 connman_error("statfs error %s for %s",
962 strerror(errno), pem_file);
966 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
968 return g_strdup_printf("%llx", fsid64);
971 static void provision_service_wifi(gpointer key,
972 struct connman_config_service *config,
973 struct connman_service *service,
974 struct connman_network *network,
975 const void *ssid, unsigned int ssid_len)
977 if (config->eap != NULL)
978 __connman_service_set_string(service, "EAP", config->eap);
980 if (config->identity != NULL)
981 __connman_service_set_string(service, "Identity",
984 if (config->ca_cert_file != NULL)
985 __connman_service_set_string(service, "CACertFile",
986 config->ca_cert_file);
988 if (config->client_cert_file != NULL)
989 __connman_service_set_string(service, "ClientCertFile",
990 config->client_cert_file);
992 if (config->private_key_file != NULL)
993 __connman_service_set_string(service, "PrivateKeyFile",
994 config->private_key_file);
996 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
997 config->private_key_file != NULL) {
1000 fsid = config_pem_fsid(config->private_key_file);
1004 g_free(config->private_key_passphrase);
1005 config->private_key_passphrase = fsid;
1008 if (config->private_key_passphrase != NULL) {
1009 __connman_service_set_string(service, "PrivateKeyPassphrase",
1010 config->private_key_passphrase);
1012 * TODO: Support for PEAP with both identity and key passwd.
1013 * In that case, we should check if both of them are found
1014 * from the config file. If not, we should not set the
1015 * service passphrase in order for the UI to request for an
1016 * additional passphrase.
1020 if (config->phase2 != NULL)
1021 __connman_service_set_string(service, "Phase2", config->phase2);
1023 if (config->passphrase != NULL)
1024 __connman_service_set_string(service, "Passphrase", config->passphrase);
1026 if (config->hidden == TRUE)
1027 __connman_service_set_hidden(service);
1030 struct connect_virtual {
1031 struct connman_service *service;
1035 static gboolean remove_virtual_config(gpointer user_data)
1037 struct connect_virtual *virtual = user_data;
1039 __connman_service_connect(virtual->service);
1040 g_hash_table_remove(config_table, virtual->vfile);
1047 static void provision_service(gpointer key, gpointer value,
1050 struct connman_service *service = user_data;
1051 struct connman_config_service *config = value;
1052 struct connman_network *network;
1053 const void *service_id;
1054 enum connman_service_type type;
1056 unsigned int ssid_len;
1058 type = connman_service_get_type(service);
1059 if (type == CONNMAN_SERVICE_TYPE_WIFI &&
1060 g_strcmp0(config->type, "wifi") != 0)
1063 if (type == CONNMAN_SERVICE_TYPE_ETHERNET &&
1064 g_strcmp0(config->type, "ethernet") != 0)
1067 DBG("service %p ident %s", service,
1068 __connman_service_get_ident(service));
1070 network = __connman_service_get_network(service);
1071 if (network == NULL) {
1072 connman_error("Service has no network set");
1076 DBG("network %p ident %s", network,
1077 connman_network_get_identifier(network));
1079 if (config->mac != NULL) {
1080 struct connman_device *device;
1081 const char *device_addr;
1083 device = connman_network_get_device(network);
1084 if (device == NULL) {
1085 connman_error("Network device is missing");
1089 device_addr = connman_device_get_string(device, "Address");
1091 DBG("wants %s has %s", config->mac, device_addr);
1093 if (g_ascii_strcasecmp(device_addr, config->mac) != 0)
1097 if (g_strcmp0(config->type, "wifi") == 0 &&
1098 type == CONNMAN_SERVICE_TYPE_WIFI) {
1099 ssid = connman_network_get_blob(network, "WiFi.SSID",
1102 connman_error("Network SSID not set");
1106 if (config->ssid == NULL || ssid_len != config->ssid_len)
1109 if (memcmp(config->ssid, ssid, ssid_len) != 0)
1113 if (config->ipv6_address != NULL) {
1114 struct connman_ipaddress *address;
1116 if (config->ipv6_prefix_length == 0 ||
1117 config->ipv6_gateway == NULL) {
1118 DBG("IPv6 prefix or gateway missing");
1122 address = connman_ipaddress_alloc(AF_INET6);
1123 if (address == NULL)
1126 connman_ipaddress_set_ipv6(address, config->ipv6_address,
1127 config->ipv6_prefix_length,
1128 config->ipv6_gateway);
1130 connman_network_set_ipv6_method(network,
1131 CONNMAN_IPCONFIG_METHOD_FIXED);
1133 if (connman_network_set_ipaddress(network, address) < 0)
1134 DBG("Unable to set IPv6 address to network %p",
1137 connman_ipaddress_free(address);
1140 if (config->ipv6_privacy != NULL) {
1141 struct connman_ipconfig *ipconfig;
1143 ipconfig = __connman_service_get_ip6config(service);
1144 if (ipconfig != NULL)
1145 __connman_ipconfig_ipv6_set_privacy(ipconfig,
1146 config->ipv6_privacy);
1149 if (config->ipv4_address != NULL) {
1150 struct connman_ipaddress *address;
1152 if (config->ipv4_netmask == 0 ||
1153 config->ipv4_gateway == NULL) {
1154 DBG("IPv4 netmask or gateway missing");
1158 address = connman_ipaddress_alloc(AF_INET);
1159 if (address == NULL)
1162 connman_ipaddress_set_ipv4(address, config->ipv4_address,
1163 config->ipv4_netmask,
1164 config->ipv4_gateway);
1166 connman_network_set_ipv4_method(network,
1167 CONNMAN_IPCONFIG_METHOD_FIXED);
1169 if (connman_network_set_ipaddress(network, address) < 0)
1170 DBG("Unable to set IPv4 address to network %p",
1173 connman_ipaddress_free(address);
1176 __connman_service_disconnect(service);
1178 service_id = __connman_service_get_ident(service);
1179 config->service_identifiers =
1180 g_slist_prepend(config->service_identifiers,
1181 g_strdup(service_id));
1183 if (config->virtual == FALSE)
1184 __connman_service_set_immutable(service, TRUE);
1186 __connman_service_set_favorite_delayed(service, TRUE, TRUE);
1188 __connman_service_set_config(service, config->config_ident,
1189 config->config_entry);
1191 if (config->domain_name != NULL)
1192 __connman_service_set_domainname(service, config->domain_name);
1194 if (config->nameservers != NULL) {
1197 __connman_service_nameserver_clear(service);
1199 for (i = 0; config->nameservers[i] != NULL; i++) {
1200 __connman_service_nameserver_append(service,
1201 config->nameservers[i], FALSE);
1205 if (config->search_domains != NULL)
1206 __connman_service_set_search_domains(service,
1207 config->search_domains);
1209 if (config->timeservers != NULL)
1210 __connman_service_set_timeservers(service,
1211 config->timeservers);
1213 if (g_strcmp0(config->type, "wifi") == 0 &&
1214 type == CONNMAN_SERVICE_TYPE_WIFI) {
1215 provision_service_wifi(key, config, service, network,
1218 __connman_service_connect(service);
1220 __connman_service_mark_dirty();
1222 __connman_service_save(service);
1224 if (config->virtual == TRUE) {
1225 struct connect_virtual *virtual;
1227 virtual = g_malloc0(sizeof(struct connect_virtual));
1228 virtual->service = service;
1229 virtual->vfile = config->virtual_file;
1231 g_timeout_add(0, remove_virtual_config, virtual);
1233 __connman_service_auto_connect();
1236 int __connman_config_provision_service(struct connman_service *service)
1238 enum connman_service_type type;
1239 GHashTableIter iter;
1240 gpointer value, key;
1242 /* For now only WiFi and Ethernet services are supported */
1243 type = connman_service_get_type(service);
1245 DBG("service %p type %d", service, type);
1247 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1248 type != CONNMAN_SERVICE_TYPE_ETHERNET)
1251 g_hash_table_iter_init(&iter, config_table);
1253 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1254 struct connman_config *config = value;
1256 g_hash_table_foreach(config->service_table,
1257 provision_service, service);
1263 int __connman_config_provision_service_ident(struct connman_service *service,
1264 const char *ident, const char *file, const char *entry)
1266 enum connman_service_type type;
1267 struct connman_config *config;
1270 /* For now only WiFi and Ethernet services are supported */
1271 type = connman_service_get_type(service);
1273 DBG("service %p type %d", service, type);
1275 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1276 type != CONNMAN_SERVICE_TYPE_ETHERNET)
1279 config = g_hash_table_lookup(config_table, ident);
1280 if (config != NULL) {
1281 GHashTableIter iter;
1282 gpointer value, key;
1283 gboolean found = FALSE;
1285 g_hash_table_iter_init(&iter, config->service_table);
1288 * Check if we need to remove individual service if it
1289 * is missing from config file.
1291 if (file != NULL && entry != NULL) {
1292 while (g_hash_table_iter_next(&iter, &key,
1294 struct connman_config_service *config_service;
1296 config_service = value;
1298 if (g_strcmp0(config_service->config_ident,
1302 if (g_strcmp0(config_service->config_entry,
1310 DBG("found %d ident %s file %s entry %s", found, ident,
1313 if (found == FALSE) {
1315 * The entry+8 will skip "service_" prefix
1317 g_hash_table_remove(config->service_table,
1323 g_hash_table_foreach(config->service_table,
1324 provision_service, service);
1330 static void generate_random_string(char *str, int length)
1335 memset(str, '\0', length);
1337 for (i = 0; i < length-1; i++) {
1339 val = (uint8_t)(random() % 122);
1342 } while((val > 57 && val < 65) || (val > 90 && val < 97));
1348 int connman_config_provision_mutable_service(GKeyFile *keyfile)
1350 struct connman_config_service *service_config;
1351 struct connman_config *config;
1352 char *vfile, *group;
1357 generate_random_string(rstr, 11);
1359 vfile = g_strdup_printf("service_mutable_%s.config", rstr);
1361 config = create_config(vfile);
1365 if (load_service_from_keyfile(keyfile, config) == FALSE)
1368 group = g_key_file_get_start_group(keyfile);
1370 service_config = g_hash_table_lookup(config->service_table, group+8);
1371 if (service_config == NULL)
1374 /* Specific to non file based config: */
1375 g_free(service_config->config_ident);
1376 service_config->config_ident = NULL;
1377 g_free(service_config->config_entry);
1378 service_config->config_entry = NULL;
1380 service_config->virtual = TRUE;
1381 service_config->virtual_file = vfile;
1383 __connman_service_provision_changed(vfile);
1385 if (g_strcmp0(service_config->type, "wifi") == 0)
1386 __connman_device_request_scan(CONNMAN_SERVICE_TYPE_WIFI);
1391 DBG("Could not proceed");
1392 g_hash_table_remove(config_table, vfile);
1398 struct connman_config_entry **connman_config_get_entries(const char *type)
1400 GHashTableIter iter_file, iter_config;
1401 gpointer value, key;
1402 struct connman_config_entry **entries = NULL;
1405 g_hash_table_iter_init(&iter_file, config_table);
1406 while (g_hash_table_iter_next(&iter_file, &key, &value) == TRUE) {
1407 struct connman_config *config_file = value;
1409 count = g_hash_table_size(config_file->service_table);
1411 entries = g_try_realloc(entries, (i + count + 1) *
1412 sizeof(struct connman_config_entry *));
1413 if (entries == NULL)
1416 g_hash_table_iter_init(&iter_config,
1417 config_file->service_table);
1418 while (g_hash_table_iter_next(&iter_config, &key,
1420 struct connman_config_service *config = value;
1423 g_strcmp0(config->type, type) != 0)
1426 entries[i] = g_try_new0(struct connman_config_entry,
1428 if (entries[i] == NULL)
1431 entries[i]->ident = g_strdup(config->ident);
1432 entries[i]->name = g_strdup(config->name);
1433 entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
1434 if (entries[i]->ssid == NULL)
1437 memcpy(entries[i]->ssid, config->ssid,
1439 entries[i]->ssid_len = config->ssid_len;
1440 entries[i]->hidden = config->hidden;
1446 if (entries != NULL) {
1447 entries = g_try_realloc(entries, (i + 1) *
1448 sizeof(struct connman_config_entry *));
1449 if (entries == NULL)
1454 DBG("%d provisioned AP found", i);
1460 connman_config_free_entries(entries);
1464 void connman_config_free_entries(struct connman_config_entry **entries)
1468 if (entries == NULL)
1471 for (i = 0; entries[i]; i++) {
1472 g_free(entries[i]->ident);
1473 g_free(entries[i]->name);
1474 g_free(entries[i]->ssid);