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;
63 unsigned char ipv6_prefix_length;
68 char **search_domains;
73 struct connman_config {
77 connman_bool_t protected;
78 GHashTable *service_table;
81 static GHashTable *config_table = NULL;
82 static GSList *protected_services = NULL;
84 static connman_bool_t cleanup = FALSE;
86 /* Definition of possible strings in the .config files */
87 #define CONFIG_KEY_NAME "Name"
88 #define CONFIG_KEY_DESC "Description"
89 #define CONFIG_KEY_PROT "Protected"
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_HIDDEN "Hidden"
105 #define SERVICE_KEY_IPv4 "IPv4"
106 #define SERVICE_KEY_IPv6 "IPv6"
107 #define SERVICE_KEY_IPv6_PRIVACY "IPv6.Privacy"
108 #define SERVICE_KEY_MAC "MAC"
109 #define SERVICE_KEY_NAMESERVERS "Nameservers"
110 #define SERVICE_KEY_SEARCH_DOMAINS "SearchDomains"
111 #define SERVICE_KEY_TIMESERVERS "Timeservers"
112 #define SERVICE_KEY_DOMAIN "Domain"
114 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,
137 SERVICE_KEY_IPv6_PRIVACY,
139 SERVICE_KEY_NAMESERVERS,
140 SERVICE_KEY_SEARCH_DOMAINS,
141 SERVICE_KEY_TIMESERVERS,
146 static void unregister_config(gpointer data)
148 struct connman_config *config = data;
150 connman_info("Removing configuration %s", config->ident);
152 g_hash_table_destroy(config->service_table);
154 g_free(config->description);
155 g_free(config->name);
156 g_free(config->ident);
160 static void unregister_service(gpointer data)
162 struct connman_config_service *config_service = data;
163 struct connman_service *service;
170 connman_info("Removing service configuration %s",
171 config_service->ident);
173 protected_services = g_slist_remove(protected_services,
176 for (list = config_service->service_identifiers; list != NULL;
178 service_id = list->data;
180 service = __connman_service_lookup_from_ident(service_id);
181 if (service != NULL) {
182 __connman_service_set_immutable(service, FALSE);
183 __connman_service_set_config(service, NULL, NULL);
184 __connman_service_remove(service);
187 * Ethernet service cannot be removed by
188 * __connman_service_remove() so reset the ipconfig
191 if (connman_service_get_type(service) ==
192 CONNMAN_SERVICE_TYPE_ETHERNET) {
193 __connman_service_disconnect(service);
194 __connman_service_reset_ipconfig(service,
195 CONNMAN_IPCONFIG_TYPE_IPV4, NULL, NULL);
196 __connman_service_reset_ipconfig(service,
197 CONNMAN_IPCONFIG_TYPE_IPV6, NULL, NULL);
198 __connman_service_set_ignore(service, TRUE);
201 * After these operations, user needs to
202 * reconnect ethernet cable to get IP
208 if (__connman_storage_remove_service(service_id) == FALSE)
209 DBG("Could not remove all files for service %s",
214 g_free(config_service->ident);
215 g_free(config_service->type);
216 g_free(config_service->name);
217 g_free(config_service->ssid);
218 g_free(config_service->eap);
219 g_free(config_service->identity);
220 g_free(config_service->ca_cert_file);
221 g_free(config_service->client_cert_file);
222 g_free(config_service->private_key_file);
223 g_free(config_service->private_key_passphrase);
224 g_free(config_service->private_key_passphrase_type);
225 g_free(config_service->phase2);
226 g_free(config_service->passphrase);
227 g_free(config_service->ipv4_address);
228 g_free(config_service->ipv4_gateway);
229 g_free(config_service->ipv4_netmask);
230 g_free(config_service->ipv6_address);
231 g_free(config_service->ipv6_gateway);
232 g_free(config_service->ipv6_privacy);
233 g_free(config_service->mac);
234 g_strfreev(config_service->nameservers);
235 g_strfreev(config_service->search_domains);
236 g_strfreev(config_service->timeservers);
237 g_free(config_service->domain_name);
238 g_slist_free_full(config_service->service_identifiers, g_free);
239 g_free(config_service->config_ident);
240 g_free(config_service->config_entry);
241 g_free(config_service);
244 static void check_keys(GKeyFile *keyfile, const char *group,
245 const char **possible_keys)
248 gsize nb_avail_keys, i, j;
250 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
251 if (avail_keys == NULL)
255 * For each key in the configuration file,
256 * verify it is understood by connman
258 for (i = 0 ; i < nb_avail_keys; i++) {
259 for (j = 0; possible_keys[j] ; j++)
260 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
263 if (possible_keys[j] == NULL)
264 connman_warn("Unknown configuration key %s in [%s]",
265 avail_keys[i], group);
268 g_strfreev(avail_keys);
271 static connman_bool_t
272 is_protected_service(struct connman_config_service *service)
276 DBG("ident %s", service->ident);
278 for (list = protected_services; list; list = list->next) {
279 struct connman_config_service *s = list->data;
281 if (g_strcmp0(s->type, service->type) != 0)
284 if (s->ssid == NULL || service->ssid == NULL)
287 if (s->ssid_len != service->ssid_len)
290 if (g_strcmp0(service->type, "wifi") == 0 &&
291 strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
299 static int check_family(const char *address, int expected_family)
304 family = connman_inet_check_ipaddress(address);
306 DBG("Cannot get address family of %s (%d/%s)", address,
307 family, gai_strerror(family));
314 if (expected_family != AF_INET) {
315 DBG("Wrong type address %s, expecting IPv4", address);
321 if (expected_family != AF_INET6) {
322 DBG("Wrong type address %s, expecting IPv6", address);
328 DBG("Unsupported address family %d", family);
337 static int parse_address(const char *address_str, int address_family,
338 char **address, char **netmask, char **gateway)
340 char *addr_str, *mask_str, *gw_str;
344 route = g_strsplit(address_str, "/", 0);
349 if (addr_str == NULL || addr_str[0] == '\0') {
354 if ((err = check_family(addr_str, address_family)) < 0)
358 if (mask_str == NULL || mask_str[0] == '\0') {
364 if (gw_str == NULL || gw_str[0] == '\0') {
369 if ((err = check_family(gw_str, address_family)) < 0)
373 *address = g_strdup(addr_str);
376 *netmask = g_strdup(mask_str);
379 *gateway = g_strdup(gw_str);
381 DBG("address %s/%s via %s", *address, *netmask, *gateway);
389 static int load_service_generic(GKeyFile *keyfile, const char *group,
390 struct connman_config *config,
391 struct connman_config_service *service)
397 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv4, NULL);
401 if (parse_address(str, AF_INET, &service->ipv4_address,
402 &mask, &service->ipv4_gateway) < 0) {
403 connman_warn("Invalid format for IPv4 address %s",
409 if (g_strrstr(mask, ".") == NULL) {
410 /* We have netmask length */
412 struct in_addr netmask_in;
413 unsigned char prefix_len = 32;
415 long int value = strtol(mask, &ptr, 10);
417 if (ptr != mask && *ptr == '\0' && value <= 32)
420 addr = 0xffffffff << (32 - prefix_len);
421 netmask_in.s_addr = htonl(addr);
422 service->ipv4_netmask =
423 g_strdup(inet_ntoa(netmask_in));
427 service->ipv4_netmask = mask;
432 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv6, NULL);
439 if (parse_address(str, AF_INET6, &service->ipv6_address,
440 &mask, &service->ipv6_gateway) < 0) {
441 connman_warn("Invalid format for IPv6 address %s",
447 value = strtol(mask, &ptr, 10);
448 if (ptr != mask && *ptr == '\0' && value <= 128)
449 service->ipv6_prefix_length = value;
451 service->ipv6_prefix_length = 128;
457 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IPv6_PRIVACY,
460 g_free(service->ipv6_privacy);
461 service->ipv6_privacy = str;
464 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_MAC, NULL);
466 g_free(service->mac);
470 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_DOMAIN, NULL);
472 g_free(service->domain_name);
473 service->domain_name = str;
476 strlist = g_key_file_get_string_list(keyfile, group,
477 SERVICE_KEY_NAMESERVERS,
479 if (strlist != NULL) {
481 g_strfreev(service->nameservers);
482 service->nameservers = strlist;
487 strlist = g_key_file_get_string_list(keyfile, group,
488 SERVICE_KEY_SEARCH_DOMAINS,
490 if (strlist != NULL) {
492 g_strfreev(service->search_domains);
493 service->search_domains = strlist;
498 strlist = g_key_file_get_string_list(keyfile, group,
499 SERVICE_KEY_TIMESERVERS,
501 if (strlist != NULL) {
503 g_strfreev(service->timeservers);
504 service->timeservers = strlist;
512 g_free(service->ident);
513 g_free(service->type);
514 g_free(service->ipv4_address);
515 g_free(service->ipv4_netmask);
516 g_free(service->ipv4_gateway);
517 g_free(service->ipv6_address);
518 g_free(service->ipv6_gateway);
519 g_free(service->mac);
525 static int load_service(GKeyFile *keyfile, const char *group,
526 struct connman_config *config)
528 struct connman_config_service *service;
530 char *str, *hex_ssid;
531 gboolean service_created = FALSE;
534 /* Strip off "service_" prefix */
537 if (strlen(ident) < 1)
540 /* Verify that provided keys are good */
541 check_keys(keyfile, group, service_possible_keys);
543 service = g_hash_table_lookup(config->service_table, ident);
544 if (service == NULL) {
545 service = g_try_new0(struct connman_config_service, 1);
549 service->ident = g_strdup(ident);
551 service_created = TRUE;
554 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
556 g_free(service->type);
559 DBG("Type of the configured service is missing for group %s",
565 err = load_service_generic(keyfile, group, config, service);
569 if (g_strcmp0(str, "ethernet") == 0) {
570 service->config_ident = g_strdup(config->ident);
571 service->config_entry = g_strdup_printf("service_%s",
574 g_hash_table_insert(config->service_table, service->ident,
579 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
581 g_free(service->name);
585 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
587 if (hex_ssid != NULL) {
589 unsigned int i, j = 0, hex;
590 size_t hex_ssid_len = strlen(hex_ssid);
592 ssid = g_try_malloc0(hex_ssid_len / 2);
599 for (i = 0; i < hex_ssid_len; i += 2) {
600 if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
601 connman_warn("Invalid SSID %s", hex_ssid);
612 g_free(service->ssid);
613 service->ssid = ssid;
614 service->ssid_len = hex_ssid_len / 2;
615 } else if (service->name != NULL) {
617 unsigned int ssid_len;
619 ssid_len = strlen(service->name);
620 ssid = g_try_malloc0(ssid_len);
626 memcpy(ssid, service->name, ssid_len);
627 g_free(service->ssid);
628 service->ssid = ssid;
629 service->ssid_len = ssid_len;
632 if (is_protected_service(service) == TRUE) {
633 connman_error("Trying to provision a protected service");
638 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
640 g_free(service->eap);
644 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
646 g_free(service->ca_cert_file);
647 service->ca_cert_file = str;
650 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
652 g_free(service->client_cert_file);
653 service->client_cert_file = str;
656 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
658 g_free(service->private_key_file);
659 service->private_key_file = str;
662 str = g_key_file_get_string(keyfile, group,
663 SERVICE_KEY_PRV_KEY_PASS, NULL);
665 g_free(service->private_key_passphrase);
666 service->private_key_passphrase = str;
669 str = g_key_file_get_string(keyfile, group,
670 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
672 g_free(service->private_key_passphrase_type);
673 service->private_key_passphrase_type = str;
676 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
678 g_free(service->identity);
679 service->identity = str;
682 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
684 g_free(service->phase2);
685 service->phase2 = str;
688 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
691 g_free(service->passphrase);
692 service->passphrase = str;
695 service->config_ident = g_strdup(config->ident);
696 service->config_entry = g_strdup_printf("service_%s", service->ident);
698 service->hidden = g_key_file_get_boolean(keyfile, group,
699 SERVICE_KEY_HIDDEN, NULL);
702 g_hash_table_insert(config->service_table, service->ident,
705 if (config->protected == TRUE)
707 g_slist_prepend(protected_services, service);
709 connman_info("Adding service configuration %s", service->ident);
714 if (service_created == TRUE) {
715 g_free(service->ident);
716 g_free(service->type);
717 g_free(service->name);
718 g_free(service->ssid);
725 static int load_config(struct connman_config *config)
728 GError *error = NULL;
732 gboolean protected, found = FALSE;
735 DBG("config %p", config);
737 keyfile = __connman_storage_load_config(config->ident);
741 /* Verify keys validity of the global section */
742 check_keys(keyfile, "global", config_possible_keys);
744 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
746 g_free(config->name);
750 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
752 g_free(config->description);
753 config->description = str;
756 protected = g_key_file_get_boolean(keyfile, "global",
757 CONFIG_KEY_PROT, &error);
759 config->protected = protected;
761 config->protected = TRUE;
762 g_clear_error(&error);
764 groups = g_key_file_get_groups(keyfile, &length);
766 for (i = 0; groups[i] != NULL; i++) {
767 if (g_str_has_prefix(groups[i], "service_") == TRUE) {
768 if (load_service(keyfile, groups[i], config) == 0)
774 connman_warn("Config file %s/%s.config does not contain any "
775 "configuration that can be provisioned!",
776 STORAGEDIR, config->ident);
780 g_key_file_free(keyfile);
785 static struct connman_config *create_config(const char *ident)
787 struct connman_config *config;
789 DBG("ident %s", ident);
791 if (g_hash_table_lookup(config_table, ident) != NULL)
794 config = g_try_new0(struct connman_config, 1);
798 config->ident = g_strdup(ident);
800 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
801 NULL, unregister_service);
803 g_hash_table_insert(config_table, config->ident, config);
805 connman_info("Adding configuration %s", config->ident);
810 static connman_bool_t validate_ident(const char *ident)
817 for (i = 0; i < strlen(ident); i++)
818 if (g_ascii_isprint(ident[i]) == FALSE)
824 static int read_configs(void)
830 dir = g_dir_open(STORAGEDIR, 0, NULL);
834 while ((file = g_dir_read_name(dir)) != NULL) {
838 if (g_str_has_suffix(file, ".config") == FALSE)
841 ident = g_strrstr(file, ".config");
845 str = g_string_new_len(file, ident - file);
849 ident = g_string_free(str, FALSE);
851 if (validate_ident(ident) == TRUE) {
852 struct connman_config *config;
854 config = create_config(ident);
858 connman_error("Invalid config ident %s", ident);
869 static void config_notify_handler(struct inotify_event *event,
877 if (g_str_has_suffix(ident, ".config") == FALSE)
880 ext = g_strrstr(ident, ".config");
886 if (validate_ident(ident) == FALSE) {
887 connman_error("Invalid config ident %s", ident);
891 if (event->mask & IN_CREATE)
892 create_config(ident);
894 if (event->mask & IN_MODIFY) {
895 struct connman_config *config;
897 config = g_hash_table_lookup(config_table, ident);
898 if (config != NULL) {
901 g_hash_table_remove_all(config->service_table);
903 ret = __connman_service_provision_changed(ident);
906 * Re-scan the config file for any
909 g_hash_table_remove_all(config->service_table);
911 __connman_service_provision_changed(ident);
916 if (event->mask & IN_DELETE)
917 g_hash_table_remove(config_table, ident);
920 int __connman_config_init(void)
924 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
925 NULL, unregister_config);
927 connman_inotify_register(STORAGEDIR, config_notify_handler);
929 return read_configs();
932 void __connman_config_cleanup(void)
938 connman_inotify_unregister(STORAGEDIR, config_notify_handler);
940 g_hash_table_destroy(config_table);
946 static char *config_pem_fsid(const char *pem_file)
949 unsigned *fsid = (unsigned *) &buf.f_fsid;
950 unsigned long long fsid64;
952 if (pem_file == NULL)
955 if (statfs(pem_file, &buf) < 0) {
956 connman_error("statfs error %s for %s",
957 strerror(errno), pem_file);
961 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
963 return g_strdup_printf("%llx", fsid64);
966 static void provision_service_wifi(gpointer key,
967 struct connman_config_service *config,
968 struct connman_service *service,
969 struct connman_network *network,
970 const void *ssid, unsigned int ssid_len)
972 if (config->eap != NULL)
973 __connman_service_set_string(service, "EAP", config->eap);
975 if (config->identity != NULL)
976 __connman_service_set_string(service, "Identity",
979 if (config->ca_cert_file != NULL)
980 __connman_service_set_string(service, "CACertFile",
981 config->ca_cert_file);
983 if (config->client_cert_file != NULL)
984 __connman_service_set_string(service, "ClientCertFile",
985 config->client_cert_file);
987 if (config->private_key_file != NULL)
988 __connman_service_set_string(service, "PrivateKeyFile",
989 config->private_key_file);
991 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
992 config->private_key_file != NULL) {
995 fsid = config_pem_fsid(config->private_key_file);
999 g_free(config->private_key_passphrase);
1000 config->private_key_passphrase = fsid;
1003 if (config->private_key_passphrase != NULL) {
1004 __connman_service_set_string(service, "PrivateKeyPassphrase",
1005 config->private_key_passphrase);
1007 * TODO: Support for PEAP with both identity and key passwd.
1008 * In that case, we should check if both of them are found
1009 * from the config file. If not, we should not set the
1010 * service passphrase in order for the UI to request for an
1011 * additional passphrase.
1015 if (config->phase2 != NULL)
1016 __connman_service_set_string(service, "Phase2", config->phase2);
1018 if (config->passphrase != NULL)
1019 __connman_service_set_string(service, "Passphrase", config->passphrase);
1021 if (config->hidden == TRUE)
1022 __connman_service_set_hidden(service);
1025 static void provision_service(gpointer key, gpointer value,
1028 struct connman_service *service = user_data;
1029 struct connman_config_service *config = value;
1030 struct connman_network *network;
1031 const void *service_id;
1032 enum connman_service_type type;
1034 unsigned int ssid_len;
1036 type = connman_service_get_type(service);
1037 if (type == CONNMAN_SERVICE_TYPE_WIFI &&
1038 g_strcmp0(config->type, "wifi") != 0)
1041 if (type == CONNMAN_SERVICE_TYPE_ETHERNET &&
1042 g_strcmp0(config->type, "ethernet") != 0)
1045 DBG("service %p ident %s", service,
1046 __connman_service_get_ident(service));
1048 network = __connman_service_get_network(service);
1049 if (network == NULL) {
1050 connman_error("Service has no network set");
1054 DBG("network %p ident %s", network,
1055 connman_network_get_identifier(network));
1057 if (config->mac != NULL) {
1058 struct connman_device *device;
1059 const char *device_addr;
1061 device = connman_network_get_device(network);
1062 if (device == NULL) {
1063 connman_error("Network device is missing");
1067 device_addr = connman_device_get_string(device, "Address");
1069 DBG("wants %s has %s", config->mac, device_addr);
1071 if (g_ascii_strcasecmp(device_addr, config->mac) != 0)
1075 if (g_strcmp0(config->type, "wifi") == 0 &&
1076 type == CONNMAN_SERVICE_TYPE_WIFI) {
1077 ssid = connman_network_get_blob(network, "WiFi.SSID",
1080 connman_error("Network SSID not set");
1084 if (config->ssid == NULL || ssid_len != config->ssid_len)
1087 if (memcmp(config->ssid, ssid, ssid_len) != 0)
1091 if (config->ipv6_address != NULL) {
1092 struct connman_ipaddress *address;
1094 if (config->ipv6_prefix_length == 0 ||
1095 config->ipv6_gateway == NULL) {
1096 DBG("IPv6 prefix or gateway missing");
1100 address = connman_ipaddress_alloc(AF_INET6);
1101 if (address == NULL)
1104 connman_ipaddress_set_ipv6(address, config->ipv6_address,
1105 config->ipv6_prefix_length,
1106 config->ipv6_gateway);
1108 connman_network_set_ipv6_method(network,
1109 CONNMAN_IPCONFIG_METHOD_FIXED);
1111 if (connman_network_set_ipaddress(network, address) < 0)
1112 DBG("Unable to set IPv6 address to network %p",
1115 connman_ipaddress_free(address);
1118 if (config->ipv6_privacy != NULL) {
1119 struct connman_ipconfig *ipconfig;
1121 ipconfig = __connman_service_get_ip6config(service);
1122 if (ipconfig != NULL)
1123 __connman_ipconfig_ipv6_set_privacy(ipconfig,
1124 config->ipv6_privacy);
1127 if (config->ipv4_address != NULL) {
1128 struct connman_ipaddress *address;
1130 if (config->ipv4_netmask == 0 ||
1131 config->ipv4_gateway == NULL) {
1132 DBG("IPv4 netmask or gateway missing");
1136 address = connman_ipaddress_alloc(AF_INET);
1137 if (address == NULL)
1140 connman_ipaddress_set_ipv4(address, config->ipv4_address,
1141 config->ipv4_netmask,
1142 config->ipv4_gateway);
1144 connman_network_set_ipv4_method(network,
1145 CONNMAN_IPCONFIG_METHOD_FIXED);
1147 if (connman_network_set_ipaddress(network, address) < 0)
1148 DBG("Unable to set IPv4 address to network %p",
1151 connman_ipaddress_free(address);
1154 __connman_service_disconnect(service);
1156 service_id = __connman_service_get_ident(service);
1157 config->service_identifiers =
1158 g_slist_prepend(config->service_identifiers,
1159 g_strdup(service_id));
1161 __connman_service_set_immutable(service, TRUE);
1163 __connman_service_set_favorite_delayed(service, TRUE, TRUE);
1165 __connman_service_set_config(service, config->config_ident,
1166 config->config_entry);
1168 if (config->domain_name != NULL)
1169 __connman_service_set_domainname(service, config->domain_name);
1171 if (config->nameservers != NULL) {
1174 __connman_service_nameserver_clear(service);
1176 for (i = 0; config->nameservers[i] != NULL; i++) {
1177 __connman_service_nameserver_append(service,
1178 config->nameservers[i], FALSE);
1182 if (config->search_domains != NULL)
1183 __connman_service_set_search_domains(service,
1184 config->search_domains);
1186 if (config->timeservers != NULL)
1187 __connman_service_set_timeservers(service,
1188 config->timeservers);
1190 if (g_strcmp0(config->type, "wifi") == 0 &&
1191 type == CONNMAN_SERVICE_TYPE_WIFI) {
1192 provision_service_wifi(key, config, service, network,
1195 __connman_service_connect(service);
1197 __connman_service_mark_dirty();
1199 __connman_service_save(service);
1201 __connman_service_auto_connect();
1204 int __connman_config_provision_service(struct connman_service *service)
1206 enum connman_service_type type;
1207 GHashTableIter iter;
1208 gpointer value, key;
1210 /* For now only WiFi and Ethernet services are supported */
1211 type = connman_service_get_type(service);
1213 DBG("service %p type %d", service, type);
1215 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1216 type != CONNMAN_SERVICE_TYPE_ETHERNET)
1219 g_hash_table_iter_init(&iter, config_table);
1221 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1222 struct connman_config *config = value;
1224 g_hash_table_foreach(config->service_table,
1225 provision_service, service);
1231 int __connman_config_provision_service_ident(struct connman_service *service,
1232 const char *ident, const char *file, const char *entry)
1234 enum connman_service_type type;
1235 struct connman_config *config;
1238 /* For now only WiFi and Ethernet services are supported */
1239 type = connman_service_get_type(service);
1241 DBG("service %p type %d", service, type);
1243 if (type != CONNMAN_SERVICE_TYPE_WIFI &&
1244 type != CONNMAN_SERVICE_TYPE_ETHERNET)
1247 config = g_hash_table_lookup(config_table, ident);
1248 if (config != NULL) {
1249 GHashTableIter iter;
1250 gpointer value, key;
1251 gboolean found = FALSE;
1253 g_hash_table_iter_init(&iter, config->service_table);
1256 * Check if we need to remove individual service if it
1257 * is missing from config file.
1259 if (file != NULL && entry != NULL) {
1260 while (g_hash_table_iter_next(&iter, &key,
1262 struct connman_config_service *config_service;
1264 config_service = value;
1266 if (g_strcmp0(config_service->config_ident,
1270 if (g_strcmp0(config_service->config_entry,
1278 DBG("found %d ident %s file %s entry %s", found, ident,
1281 if (found == FALSE) {
1283 * The entry+8 will skip "service_" prefix
1285 g_hash_table_remove(config->service_table,
1291 g_hash_table_foreach(config->service_table,
1292 provision_service, service);
1298 struct connman_config_entry **connman_config_get_entries(const char *type)
1300 GHashTableIter iter_file, iter_config;
1301 gpointer value, key;
1302 struct connman_config_entry **entries = NULL;
1305 g_hash_table_iter_init(&iter_file, config_table);
1306 while (g_hash_table_iter_next(&iter_file, &key, &value) == TRUE) {
1307 struct connman_config *config_file = value;
1309 count = g_hash_table_size(config_file->service_table);
1311 entries = g_try_realloc(entries, (i + count + 1) *
1312 sizeof(struct connman_config_entry *));
1313 if (entries == NULL)
1316 g_hash_table_iter_init(&iter_config,
1317 config_file->service_table);
1318 while (g_hash_table_iter_next(&iter_config, &key,
1320 struct connman_config_service *config = value;
1323 g_strcmp0(config->type, type) != 0)
1326 entries[i] = g_try_new0(struct connman_config_entry,
1328 if (entries[i] == NULL)
1331 entries[i]->ident = g_strdup(config->ident);
1332 entries[i]->name = g_strdup(config->name);
1333 entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
1334 if (entries[i]->ssid == NULL)
1337 memcpy(entries[i]->ssid, config->ssid,
1339 entries[i]->ssid_len = config->ssid_len;
1340 entries[i]->hidden = config->hidden;
1346 if (entries != NULL) {
1347 entries = g_try_realloc(entries, (i + 1) *
1348 sizeof(struct connman_config_entry *));
1349 if (entries == NULL)
1354 DBG("%d provisioned AP found", i);
1360 connman_config_free_entries(entries);
1364 void connman_config_free_entries(struct connman_config_entry **entries)
1368 if (entries == NULL)
1371 for (i = 0; entries[i]; i++) {
1372 g_free(entries[i]->ident);
1373 g_free(entries[i]->name);
1374 g_free(entries[i]->ssid);