2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
37 #include "wifi-config.h"
38 #include "wifi-state.h"
39 #include "netsupplicant.h"
40 #include "wifi-key-encryption.h"
42 #define CONNMAN_STORAGE "/var/lib/connman"
44 #define WIFI_SECURITY_NONE "none"
45 #define WIFI_SECURITY_WEP "wep"
46 #define WIFI_SECURITY_WPA_PSK "psk"
47 #define WIFI_SECURITY_EAP "ieee8021x"
48 #define WIFI_SECURITY_SAE "sae"
50 #define WIFI_CONFIG_PREFIX "wifi_"
51 #define MAC_ADDRESS_LENGTH 12
52 #define WIFI_PREFIX_LENGTH MAC_ADDRESS_LENGTH + 6 /* wifi_485a3f2f506a_ */
53 #define PROFILE_PREFIX_LENGTH WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
55 #define NET_DNS_ADDR_MAX 2
57 #define MAX_WIFI_PROFILES 200
59 struct wifi_eap_config {
60 gchar *anonymous_identity;
64 gchar *private_key_password;
74 gchar *gateway_address;
75 gchar *dns_address[NET_DNS_ADDR_MAX];
92 struct wifi_eap_config *eap_config;
93 wifi_ip_info_s *ip_info;
98 static void __free_wifi_configuration(struct wifi_config *conf)
105 g_free(conf->passphrase);
106 g_free(conf->security_type);
107 g_free(conf->is_hidden);
108 g_free(conf->proxy_address);
109 g_free(conf->last_error);
110 if (conf->eap_config) {
111 g_free(conf->eap_config->anonymous_identity);
112 g_free(conf->eap_config->ca_cert);
113 g_free(conf->eap_config->client_cert);
114 g_free(conf->eap_config->private_key);
115 g_free(conf->eap_config->private_key_password);
116 g_free(conf->eap_config->identity);
117 g_free(conf->eap_config->eap_type);
118 g_free(conf->eap_config->eap_auth_type);
119 g_free(conf->eap_config->subject_match);
120 g_free(conf->eap_config);
124 g_free(conf->ip_info->ip_type);
125 g_free(conf->ip_info->ip_address);
126 g_free(conf->ip_info->subnet_mask);
127 g_free(conf->ip_info->gateway_address);
128 g_free(conf->ip_info->dns_type);
129 conf->ip_info->prefix_length = 0;
131 int i = 0, count = conf->ip_info->dns_count;
133 g_free(conf->ip_info->dns_address[i]);
136 g_free(conf->ip_info);
141 static gboolean __get_mac_address(const gchar *interface_name, gchar **mac_address)
144 char buf[WIFI_MAC_ADDR_LENGTH + 1];
145 char path[WIFI_MAC_PATH_LENGTH];
146 gchar *tmp_mac = NULL;
148 gchar mac[13] = { 0, };
151 snprintf(path, WIFI_MAC_PATH_LENGTH, WIFI_MAC_ADDR_PATH, interface_name);
153 if (0 == access(path, F_OK))
154 fp = fopen(path, "r");
157 if (fgets(buf, sizeof(buf), fp) == NULL) {
158 ERR("Failed to get MAC info from %s\n", path);
163 tmp_mac = (gchar *)malloc(WIFI_MAC_ADDR_LENGTH + 1);
164 if (tmp_mac == NULL) {
165 ERR("malloc() failed");
170 memset(tmp_mac, 0, WIFI_MAC_ADDR_LENGTH + 1);
171 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADDR_LENGTH + 1);
174 ERR("Failed to open file %s\n", path);
176 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
177 if (tmp_mac == NULL) {
178 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
184 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
186 while (tmp && tmp[i]) {
192 *mac_address = g_strdup(mac);
198 gboolean wifi_config_get_group_name(const gchar *prefix,
199 const gchar *interface_name, const gchar *config_id, gchar **group_name)
201 gchar *mac_address = NULL;
202 gchar *g_name = NULL;
203 gboolean ret = FALSE;
205 ret = __get_mac_address(interface_name, &mac_address);
206 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
207 ERR("Cannot get WIFI MAC address");
212 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
213 if (g_name == NULL) {
218 *group_name = g_strdup(g_name);
226 static gboolean __get_security_type(const gchar *config_id, gchar **type)
228 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
229 *type = g_strdup(WIFI_SECURITY_NONE);
230 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
231 *type = g_strdup(WIFI_SECURITY_WEP);
232 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
233 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
234 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
235 *type = g_strdup(WIFI_SECURITY_EAP);
236 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_SAE) == TRUE) {
237 *type = g_strdup(WIFI_SECURITY_SAE);
246 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
248 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
249 if (*config_id == NULL) {
258 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
260 GKeyFile *keyfile = NULL;
263 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
265 keyfile = netconfig_keyfile_load(path);
267 ERR("keyfile[%s] is NULL", path);
274 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
276 gboolean ret = FALSE;
279 path = g_strdup_printf("%s/%s", pathname, filename);
280 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
282 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
291 static gboolean __remove_all_files(const gchar *pathname)
294 struct dirent *file = NULL;
296 if ((dir_ptr = opendir(pathname)) == NULL)
299 while ((file = readdir(dir_ptr)) != NULL) {
300 if (strncmp(file->d_name, ".", 1) == 0 || strncmp(file->d_name, "..", 2) == 0)
303 if (__remove_file(pathname, file->d_name) != TRUE) {
304 ERR("Cannot remove [%s/%s]", pathname, file->d_name);
316 static gboolean __remove_configuration(const gchar *pathname)
320 if (__remove_all_files(pathname) != TRUE) {
321 ERR("Cannot remove [%s] directory", pathname);
325 ret = rmdir(pathname);
327 ERR("Cannot remove [%s]", pathname);
333 static gboolean _load_configuration(const gchar *interface_name,
334 const gchar *config_id, struct wifi_config *config)
338 gboolean hidden = FALSE;
339 gboolean ret = FALSE;
341 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
342 interface_name, config_id, &group_name);
344 ERR("Fail to get_wifi_config_group_name");
348 keyfile = __get_configuration_keyfile(group_name);
349 if (keyfile == NULL) {
350 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
355 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
356 DBG("name [%s]", config->name);
358 if (config->name == NULL) {
359 ERR("Fail to get Name of [%s]", group_name);
360 g_key_file_free(keyfile);
365 __get_security_type(config_id, &config->security_type);
366 if (config->security_type == NULL) {
367 ERR("Fail to _get_security_type");
368 g_key_file_free(keyfile);
372 DBG("security_type [%s]", config->security_type);
374 config->passphrase = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
375 DBG("passphrase []");
377 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
378 if (config->proxy_address)
379 DBG("proxy_address [%s]", config->proxy_address);
381 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
383 config->is_hidden = g_strdup("TRUE");
385 config->is_hidden = g_strdup("FALSE");
386 DBG("is_hidden [%s]", config->is_hidden);
388 config->frequency = g_key_file_get_integer(keyfile, group_name, WIFI_CONFIG_FREQUENCY, NULL);
389 if (config->frequency)
390 DBG("Frequency [%d]", config->frequency);
392 if (config->ip_info) {
393 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
394 WIFI_CONFIG_IPV4_METHOD, NULL);
395 if (config->ip_info->ip_type)
396 DBG("IPv4.Method:%s", config->ip_info->ip_type);
398 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
399 WIFI_CONFIG_IPV4_ADDRESS, NULL);
400 if (config->ip_info->ip_address)
401 DBG("IPv4.Address:%s", config->ip_info->ip_address);
405 struct in_addr netmask;
407 GError *error = NULL;
408 prefix_len = g_key_file_get_integer(keyfile, group_name,
409 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
411 DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
414 if (prefix_len > 0 && prefix_len < 32) {
415 addr = 0xffffffff << (32 - prefix_len);
416 netmask.s_addr = htonl(addr);
417 mask = inet_ntoa(netmask);
418 config->ip_info->subnet_mask = g_strdup(mask);
420 if (config->ip_info->subnet_mask)
421 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
424 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
425 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
426 if (config->ip_info->gateway_address)
427 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
429 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
430 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
431 if (config->ip_info->dns_type)
432 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
436 nameservers = g_key_file_get_string_list(keyfile, group_name,
437 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
440 config->ip_info->dns_count = length;
442 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
443 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
444 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
448 g_strfreev(nameservers);
453 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
454 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
455 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
456 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
457 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
458 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
459 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
460 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
461 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
462 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
464 if (config->eap_config->anonymous_identity)
465 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
466 if (config->eap_config->ca_cert)
467 DBG("ca_cert [%s]", config->eap_config->ca_cert);
468 if (config->eap_config->client_cert)
469 DBG("client_cert [%s]", config->eap_config->client_cert);
470 if (config->eap_config->private_key)
471 DBG("private_key [%s]", config->eap_config->private_key);
472 if (config->eap_config->private_key_password)
473 DBG("private_key_password [%s]", config->eap_config->private_key_password);
474 if (config->eap_config->identity)
475 DBG("identity [%s]", config->eap_config->identity);
476 if (config->eap_config->eap_type)
477 DBG("eap_type [%s]", config->eap_config->eap_type);
478 if (config->eap_config->eap_auth_type)
479 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
480 if (config->eap_config->subject_match)
481 DBG("subject_match [%s]", config->eap_config->subject_match);
484 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
485 if (config->last_error)
486 DBG("last_error [%s]", config->last_error);
488 g_key_file_free(keyfile);
494 gboolean wifi_config_save_configuration(const gchar *interface_name,
495 const gchar *config_id, GKeyFile *keyfile)
500 gboolean ret = FALSE;
502 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
503 interface_name, config_id, &group_name);
505 ERR("Fail to get_wifi_config_group_name");
509 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
510 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
511 if (__remove_configuration(dir) != TRUE) {
512 ERR("[%s] is existed, but cannot remove", dir);
519 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
520 ERR("Cannot mkdir %s", dir);
526 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
527 netconfig_keyfile_save(keyfile, path);
535 static gboolean _remove_configuration(const gchar *interface_name, const gchar *config_id)
537 gboolean ret = FALSE;
541 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
542 interface_name, config_id, &group_name);
544 ERR("Fail to get_wifi_config_group_name");
548 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
549 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
550 if (__remove_configuration(dir) != TRUE) {
551 ERR("[%s] is existed, but cannot remove", dir);
554 INFO("Success to remove [%s]", dir);
557 ERR("[%s] is not existed", dir);
568 static gboolean _set_field(const gchar *interface_name,
569 const gchar *config_id, const gchar *key, const gchar *value)
575 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
576 interface_name, config_id, &group_name);
578 ERR("Fail to get_wifi_config_group_name");
581 DBG("group_name %s", group_name);
583 keyfile = __get_configuration_keyfile(group_name);
584 if (keyfile == NULL) {
585 ERR("Fail to __get_configuration_keyfile");
590 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
591 g_key_file_set_string(keyfile, group_name, key, value);
592 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
593 g_key_file_set_string(keyfile, group_name, key, value);
594 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
595 gboolean hidden = FALSE;
596 if (g_strcmp0(value, "TRUE") == 0)
598 g_key_file_set_boolean(keyfile, group_name, key, hidden);
599 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
600 g_key_file_set_string(keyfile, group_name, key, value);
601 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
602 g_key_file_set_string(keyfile, group_name, key, value);
603 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
604 g_key_file_set_string(keyfile, group_name, key, value);
605 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
606 g_key_file_set_string(keyfile, group_name, key, value);
607 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
608 g_key_file_set_string(keyfile, group_name, key, value);
609 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
610 g_key_file_set_string(keyfile, group_name, key, value);
611 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
612 g_key_file_set_string(keyfile, group_name, key, value);
613 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
614 g_key_file_set_string(keyfile, group_name, key, value);
616 ERR("key[%s] is not supported", key);
620 wifi_config_save_configuration(interface_name, config_id, keyfile);
622 g_key_file_free(keyfile);
628 static gboolean _get_field(const gchar *interface_name,
629 const gchar *config_id, const gchar *key, gchar **value)
634 gboolean hidden = FALSE;
635 gboolean ret = FALSE;
637 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
638 interface_name, config_id, &group_name);
640 ERR("Fail to get_wifi_config_group_name");
643 DBG("group_name %s", group_name);
645 keyfile = __get_configuration_keyfile(group_name);
646 if (keyfile == NULL) {
647 ERR("Fail to __get_configuration_keyfile");
652 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
653 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
654 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
655 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
656 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
657 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
658 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
659 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
661 val = g_strdup("TRUE");
663 val = g_strdup("FALSE");
664 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
665 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
666 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
667 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
668 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
669 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
670 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
671 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
672 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
673 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
674 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
675 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
676 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
677 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
678 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
679 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
680 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
681 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
683 ERR("Invalid key[%s]", key);
684 val = g_strdup("NOTSUPPORTED");
687 *value = g_strdup(val);
690 g_key_file_free(keyfile);
696 static GSList *_get_list(const char *mac_addr)
699 struct dirent *dp = NULL;
702 dir = opendir(CONNMAN_STORAGE);
704 ERR("Cannot open dir %s", CONNMAN_STORAGE);
708 while ((dp = readdir(dir)) != NULL) {
709 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
710 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
714 DBG("%s", dp->d_name);
716 if (netconfig_check_mac_address(dp->d_name, mac_addr)) {
717 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
718 DBG("%s", config_id);
719 list = g_slist_append(list, g_strdup(config_id));
728 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
730 gboolean ret = FALSE;
733 if ((service_profile == NULL) || (config_id == NULL)) {
734 ERR("Invalid parameter");
738 ret = __get_config_id(service_profile, &val);
739 *config_id = g_strdup(val);
745 gboolean wifi_config_remove_configuration(const gchar *interface_name,
746 const gchar *config_id)
748 gboolean ret = FALSE;
750 ret = _remove_configuration(interface_name, config_id);
755 int __netconfig_hex_char_to_num(char c)
757 if (c >= '0' && c <= '9')
760 if (c >= 'a' && c <= 'f')
763 if (c >= 'A' && c <= 'F')
769 int __netconfig_hex_to_byte(const char *hex)
773 a = __netconfig_hex_char_to_num(*hex++);
777 b = __netconfig_hex_char_to_num(*hex++);
784 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
788 const char *ipos = hex;
789 unsigned char *opos = buf;
791 for (i = 0; i < len; i++) {
792 a = __netconfig_hex_to_byte(ipos);
803 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
810 ERR("Invalid parameter.");
814 *dst = (char *) g_try_malloc0((2*src_len)+1);
816 ERR("failed to allocate memory to buffer.");
822 for (i = 0; i < src_len; i++) {
823 snprintf(buf, 3, "%02x", src[i]);
831 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
833 GVariantIter *iter_copy = NULL;
836 unsigned char *tmp_dst = NULL;
838 if (!dst || *dst || !iter) {
839 ERR("Invalid parameter");
843 iter_copy = g_variant_iter_copy(iter);
845 while (g_variant_iter_loop(iter, "y", &tmp))
847 g_variant_iter_free(iter);
849 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
851 ERR("failed to allocate memory");
852 g_variant_iter_free(iter_copy);
857 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
859 g_variant_iter_free(iter_copy);
865 tmp_dst[length] = '\0';
869 DBG("Length [%d]", length);
873 gboolean _add_vsie(const char *interface_name, int frame_id, const char* vsie)
875 GVariant *params = NULL;
876 GVariant *message = NULL;
877 GVariantBuilder *bytearray_builder = NULL;
882 unsigned char *bytearray = NULL;
883 size_t bytearray_len = 0;
885 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
886 DBG("Invalid parameter, frame-id: %d", frame_id);
890 vsie_len = strlen(vsie);
892 DBG("vsie length is zero");
896 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
898 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
899 if (bytearray == NULL) {
900 DBG("Failed to allocate memory to bytearray");
904 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
905 DBG("invalid vsie string");
910 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
911 for (i = 0; i < bytearray_len; i++)
912 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
914 params = g_variant_new("(iay)", frame_id, bytearray_builder);
915 g_variant_builder_unref(bytearray_builder);
917 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
918 if (if_path == NULL) {
919 ERR("Fail to get wpa_supplicant DBus path");
924 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
925 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
928 if (message == NULL) {
929 ERR("Failed to send command to wpa_supplicant");
934 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
940 gboolean _get_vsie(const char *interface_name, int frame_id, char **vsie)
942 GVariant *params = NULL;
943 GVariant *message = NULL;
946 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
947 DBG("Invalid parameter, frame-id: %d", frame_id);
951 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
952 if (if_path == NULL) {
953 ERR("Fail to get wpa_supplicant DBus path");
957 params = g_variant_new("(i)", frame_id);
959 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
960 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
963 if (message == NULL) {
964 ERR("Failed to send command to wpa_supplicant");
967 GVariantIter *iter = NULL;
968 unsigned char *vsie_bytes = NULL;
972 g_variant_get(message, "(ay)", &iter);
974 ERR("vsie is not present");
978 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
979 if (vsie_bytes == NULL) {
980 ERR("vsie_bytes not allocated");
984 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
987 ERR("vsie not allocated.");
994 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
1000 gboolean _remove_vsie(const char *interface_name, int frame_id, const char *vsie)
1002 GVariant *params = NULL;
1003 GVariant *message = NULL;
1004 GVariantBuilder *bytearray_builder = NULL;
1007 size_t vsie_len = 0;
1009 unsigned char *bytearray = NULL;
1010 size_t bytearray_len = 0;
1012 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
1013 DBG("Invalid parameter, frame-id: %d", frame_id);
1017 vsie_len = strlen(vsie);
1018 if (vsie_len == 0) {
1019 DBG("vsie length is zero");
1023 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
1025 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
1026 if (bytearray == NULL) {
1027 DBG("Failed to allocate memory to bytearray");
1031 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
1032 DBG("invalid vsie string");
1037 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1038 for (i = 0; i < bytearray_len; i++)
1039 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
1041 params = g_variant_new("(iay)", frame_id, bytearray_builder);
1042 g_variant_builder_unref(bytearray_builder);
1044 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
1045 if (if_path == NULL) {
1046 ERR("Fail to get wpa_supplicant DBus path");
1051 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
1052 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
1055 if (message == NULL) {
1056 ERR("Failed to send command to wpa_supplicant");
1061 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1068 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context,
1069 const gchar *ifname)
1072 GSList *config_ids = NULL;
1074 gchar **result = NULL;
1075 const gchar *mac_addr = NULL;
1077 g_return_val_if_fail(wifi != NULL, TRUE);
1079 mac_addr = wifi_state_get_mac_address(ifname);
1081 ERR("Fail to get mac-address");
1082 netconfig_error_no_profile(context);
1086 DBG("%s", mac_addr);
1087 config_ids = _get_list(mac_addr);
1088 if (config_ids == NULL) {
1089 ERR("Fail to get config list");
1090 netconfig_error_no_profile(context);
1094 length = g_slist_length(config_ids);
1095 result = g_new0(gchar *, length + 1);
1096 for (i = 0; i < length; i++) {
1097 gchar *config_id = g_slist_nth_data(config_ids, i);
1098 result[i] = g_strdup(config_id);
1101 config_ids = g_slist_nth(config_ids, 0);
1102 g_slist_free_full(config_ids, g_free);
1104 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1106 for (i = 0; i < length; i++)
1116 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1117 const gchar *ifname, const gchar *config_id)
1119 gboolean ret = FALSE;
1120 GVariantBuilder *b = NULL;
1121 struct wifi_config *conf = NULL;
1123 g_return_val_if_fail(wifi != NULL, TRUE);
1125 conf = g_new0(struct wifi_config, 1);
1126 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1128 ret = _load_configuration(ifname, config_id, conf);
1130 g_free(conf->ip_info);
1132 ERR("Fail to _load_configuration");
1133 netconfig_error_no_profile(context);
1137 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1138 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1139 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1140 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PASSPHRASE, g_variant_new_string(conf->passphrase));
1141 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1142 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FREQUENCY, g_variant_new_int32(conf->frequency));
1144 if (conf->proxy_address != NULL)
1145 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1147 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1149 if (conf->ip_info->ip_type != NULL)
1150 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1152 if (conf->ip_info->ip_address != NULL)
1153 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1155 if (conf->ip_info->subnet_mask != NULL)
1156 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1158 if (conf->ip_info->prefix_length > 0)
1159 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV6_PREFIX_LEN, g_variant_new_int32(conf->ip_info->prefix_length));
1161 if (conf->ip_info->gateway_address != NULL)
1162 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1164 if (conf->ip_info->dns_type != NULL)
1165 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1167 int i = 0, count = conf->ip_info->dns_count;
1169 if (conf->ip_info->dns_address[i] != NULL)
1170 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1175 if (conf->last_error != NULL)
1176 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1178 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1180 __free_wifi_configuration(conf);
1182 INFO("Success to load configuration [%s:%s]", ifname, config_id);
1184 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1185 g_variant_builder_unref(b);
1189 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1190 const char *netmask)
1199 mask = inet_network(netmask);
1202 /* a valid netmask must be 2^n - 1 */
1203 if ((host & (host + 1)) != 0)
1207 for (; mask; mask <<= 1)
1213 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1214 const gchar *ifname, const gchar *config_id, GVariant *configuration)
1216 gboolean ret = FALSE;
1217 struct wifi_config *conf = NULL;
1218 GKeyFile *keyfile = NULL;
1222 gchar *group_name = NULL;
1225 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1226 ERR("Invalid parameter");
1227 netconfig_error_invalid_parameter(context);
1231 conf = g_new0(struct wifi_config, 1);
1232 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1234 g_variant_get(configuration, "a{sv}", &iter);
1235 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1236 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1237 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1238 conf->name = g_strdup(g_variant_get_string(value, NULL));
1239 DBG("name [%s]", conf->name);
1243 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1244 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1245 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1246 DBG("ssid [%s]", conf->ssid);
1250 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1251 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1252 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1253 DBG("passphrase []");
1255 conf->passphrase = NULL;
1257 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1258 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1259 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1260 DBG("is_hidden [%s]", conf->is_hidden);
1262 conf->is_hidden = NULL;
1264 } else if (g_strcmp0(field, WIFI_CONFIG_FREQUENCY) == 0) {
1265 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1266 conf->frequency = g_variant_get_int32(value);
1267 DBG("frequency [%d]", conf->frequency);
1269 conf->frequency = 0;
1271 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1272 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1273 conf->is_created = g_variant_get_boolean(value);
1274 DBG("is_created [%d]", conf->is_created);
1276 conf->is_created = FALSE;
1278 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1279 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1280 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1281 DBG("IP config type [%s]", conf->ip_info->ip_type);
1283 conf->ip_info->ip_type = NULL;
1285 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1286 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1287 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1288 DBG("IP address [%s]", conf->ip_info->ip_address);
1290 conf->ip_info->ip_address = NULL;
1292 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1293 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1294 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1295 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1297 conf->ip_info->subnet_mask = NULL;
1299 } else if (g_strcmp0(field, WIFI_CONFIG_IPV6_PREFIX_LEN) == 0) {
1300 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1301 conf->ip_info->prefix_length = g_variant_get_int32(value);
1302 DBG("IPv6 Prefix Length [%d]", conf->ip_info->prefix_length);
1304 conf->ip_info->prefix_length = 0;
1306 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1307 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1308 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1309 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1311 conf->ip_info->gateway_address = NULL;
1313 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1314 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1315 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1316 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1318 conf->ip_info->dns_type = NULL;
1320 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1321 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1322 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1323 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1324 conf->ip_info->dns_count = order + 1;
1327 conf->ip_info->dns_address[order++] = NULL;
1329 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1330 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1331 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1332 DBG("proxy_address [%s]", conf->proxy_address);
1334 conf->proxy_address = NULL;
1338 conf->favorite = TRUE;
1339 conf->autoconnect = TRUE;
1341 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
1342 ifname, config_id, &group_name);
1344 __free_wifi_configuration(conf);
1345 ERR("Fail to get_wifi_config_group_name");
1346 netconfig_error_fail_save_congifuration(context);
1350 keyfile = g_key_file_new();
1351 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1352 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1354 if (conf->passphrase != NULL) {
1355 gchar *enc_data = NULL;
1357 if (conf->is_created == true)
1358 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1360 enc_data = g_strdup(conf->passphrase);
1363 ERR("Failed to encrypt the passphrase");
1365 g_free(conf->passphrase);
1366 conf->passphrase = enc_data;
1369 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1372 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1373 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1375 #if GLIB_CHECK_VERSION(2,62,0)
1376 gint64 real_time = 0;
1377 GDateTime *dt_real_time = NULL;
1379 real_time = g_get_real_time();
1380 dt_real_time = g_date_time_new_from_unix_utc(real_time);
1382 gchar *str = g_date_time_format_iso8601(dt_real_time);
1383 g_date_time_unref(dt_real_time);
1385 g_key_file_set_string(keyfile, group_name,
1386 WIFI_CONFIG_MODIFIED, str);
1390 #else /* GLIB_CHECK_VERSION(2,62,0) */
1392 g_get_current_time(&modified);
1393 gchar *str = g_time_val_to_iso8601(&modified);
1395 g_key_file_set_string(keyfile, group_name,
1396 WIFI_CONFIG_MODIFIED, str);
1399 #endif /* GLIB_CHECK_VERSION(2,62,0) */
1401 /* Optional field */
1402 if (conf->proxy_address != NULL) {
1403 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1404 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1407 if (conf->is_hidden != NULL) {
1408 gboolean hidden = FALSE;
1409 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1411 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1414 if (conf->frequency > 0)
1415 g_key_file_set_integer(keyfile, group_name,
1416 WIFI_CONFIG_FREQUENCY, conf->frequency);
1418 if (conf->ip_info->ip_type != NULL)
1419 g_key_file_set_string(keyfile, group_name,
1420 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1422 if (conf->ip_info->ip_address != NULL)
1423 g_key_file_set_string(keyfile, group_name,
1424 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1426 if (conf->ip_info->subnet_mask != NULL) {
1427 unsigned char prefix_len;
1428 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1429 conf->ip_info->subnet_mask);
1430 if (prefix_len > 0 && prefix_len < 32)
1431 g_key_file_set_integer(keyfile, group_name,
1432 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1435 if (conf->ip_info->prefix_length > 0)
1436 g_key_file_set_integer(keyfile, group_name,
1437 WIFI_CONFIG_IPV6_PREFIX_LEN, conf->ip_info->prefix_length);
1439 if (conf->ip_info->gateway_address != NULL)
1440 g_key_file_set_string(keyfile, group_name,
1441 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1443 if (conf->ip_info->dns_type != NULL)
1444 g_key_file_set_string(keyfile, group_name,
1445 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1447 int i = 0, count = conf->ip_info->dns_count;
1449 if (conf->ip_info->dns_address[i] != NULL)
1450 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1451 conf->ip_info->dns_address[i]);
1456 ret = wifi_config_save_configuration(ifname, config_id, keyfile);
1458 INFO("Success to save configuration [%s]", config_id);
1459 wifi_complete_save_configuration(wifi, context);
1461 if (get_files_count(CONNMAN_STORAGE) > MAX_WIFI_PROFILES) {
1462 file = get_least_recently_profile(CONNMAN_STORAGE);
1464 gchar *profileName = g_strdup_printf(CONNMAN_STORAGE "/%s", file);
1465 INFO("least modified file: %s", profileName);
1467 if (__remove_configuration(profileName) != TRUE)
1468 DBG("Failed to remove profile: [%s]", profileName);
1470 ERR("Profile: [%s] does not exist", file);
1472 g_free(profileName);
1476 INFO("Fail to save configuration [%s]", config_id);
1477 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1480 g_key_file_free(keyfile);
1482 __free_wifi_configuration(conf);
1484 g_variant_iter_free(iter);
1489 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1490 const gchar *ifname, const gchar *config_id)
1492 gboolean ret = FALSE;
1493 GVariantBuilder *b = NULL;
1494 struct wifi_config *conf = NULL;
1496 g_return_val_if_fail(wifi != NULL, TRUE);
1498 conf = g_new0(struct wifi_config, 1);
1499 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1500 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1502 ret = _load_configuration(ifname, config_id, conf);
1504 g_free(conf->eap_config);
1505 g_free(conf->ip_info);
1507 ERR("Fail to _load_configuration");
1508 netconfig_error_no_profile(context);
1512 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1513 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1514 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1515 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1516 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FREQUENCY, g_variant_new_int32(conf->frequency));
1518 if (conf->proxy_address != NULL)
1519 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1521 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1523 if (conf->last_error != NULL)
1524 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1526 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1528 if (conf->eap_config != NULL) {
1529 if (conf->eap_config->anonymous_identity != NULL)
1530 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1532 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1534 if (conf->eap_config->ca_cert != NULL)
1535 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1537 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1539 if (conf->eap_config->client_cert != NULL)
1540 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1542 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1544 if (conf->eap_config->private_key != NULL)
1545 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1547 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1549 if (conf->eap_config->private_key_password != NULL)
1550 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1552 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1554 if (conf->eap_config->identity != NULL)
1555 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1557 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1559 if (conf->eap_config->eap_type != NULL)
1560 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1562 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1564 if (conf->eap_config->eap_auth_type != NULL)
1565 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1567 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1569 if (conf->eap_config->subject_match != NULL)
1570 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1572 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1575 __free_wifi_configuration(conf);
1577 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1578 g_variant_builder_unref(b);
1582 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1583 const gchar *ifname, const gchar *config_id, GVariant *configuration)
1585 gboolean ret = FALSE;
1586 struct wifi_config *conf = NULL;
1587 GKeyFile *keyfile = NULL;
1591 gchar *group_name = NULL;
1593 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1594 ERR("Invalid parameter");
1595 netconfig_error_invalid_parameter(context);
1599 conf = g_new0(struct wifi_config, 1);
1600 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1602 g_variant_get(configuration, "a{sv}", &iter);
1603 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1604 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1605 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1606 conf->name = g_strdup(g_variant_get_string(value, NULL));
1607 DBG("name [%s]", conf->name);
1611 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1612 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1613 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1614 DBG("ssid [%s]", conf->ssid);
1618 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1619 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1620 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1621 DBG("passphrase [%s]", conf->passphrase);
1623 conf->passphrase = NULL;
1625 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1626 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1627 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1628 DBG("is_hidden [%s]", conf->is_hidden);
1630 conf->is_hidden = NULL;
1632 } else if (g_strcmp0(field, WIFI_CONFIG_FREQUENCY) == 0) {
1633 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1634 conf->frequency = g_variant_get_int32(value);
1635 DBG("frequency [%d]", conf->frequency);
1637 conf->frequency = 0;
1639 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1640 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1641 conf->is_created = g_variant_get_boolean(value);
1642 DBG("is_created [%d]", conf->is_created);
1644 conf->is_created = FALSE;
1646 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1647 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1648 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1649 DBG("proxy_address [%s]", conf->proxy_address);
1651 conf->proxy_address = NULL;
1653 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1654 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1655 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1656 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1658 conf->eap_config->anonymous_identity = NULL;
1660 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1661 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1662 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1663 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1665 conf->eap_config->ca_cert = NULL;
1667 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1668 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1669 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1670 DBG("client_cert [%s]", conf->eap_config->client_cert);
1672 conf->eap_config->client_cert = NULL;
1674 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1675 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1676 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1677 DBG("private_key [%s]", conf->eap_config->private_key);
1679 conf->eap_config->private_key = NULL;
1681 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1682 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1683 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1684 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1686 conf->eap_config->private_key_password = NULL;
1688 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1689 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1690 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1691 DBG("identity [%s]", conf->eap_config->identity);
1693 conf->eap_config->identity = NULL;
1695 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1696 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1697 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1698 DBG("eap_type [%s]", conf->eap_config->eap_type);
1700 conf->eap_config->eap_type = NULL;
1702 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1703 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1704 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1705 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1707 conf->eap_config->eap_auth_type = NULL;
1709 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1710 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1711 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1712 DBG("subject_match [%s]", conf->eap_config->subject_match);
1714 conf->eap_config->subject_match = NULL;
1718 conf->favorite = TRUE;
1719 conf->autoconnect = TRUE;
1721 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
1722 ifname, config_id, &group_name);
1724 __free_wifi_configuration(conf);
1725 ERR("Fail to get_wifi_config_group_name");
1729 keyfile = g_key_file_new();
1730 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1731 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1733 if (conf->passphrase != NULL) {
1734 gchar *enc_data = NULL;
1736 if (conf->is_created == true)
1737 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1739 enc_data = g_strdup(conf->passphrase);
1742 ERR("Failed to encrypt the passphrase");
1744 g_free(conf->passphrase);
1745 conf->passphrase = enc_data;
1747 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1750 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1751 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1753 /* Optional field */
1754 if (conf->proxy_address != NULL) {
1755 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1756 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1759 if (conf->is_hidden != NULL) {
1760 gboolean hidden = FALSE;
1761 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1763 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1766 if (conf->frequency > 0)
1767 g_key_file_set_integer(keyfile, group_name,
1768 WIFI_CONFIG_FREQUENCY, conf->frequency);
1770 if (conf->eap_config->anonymous_identity != NULL)
1771 g_key_file_set_string(keyfile, group_name,
1772 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1774 if (conf->eap_config->ca_cert != NULL)
1775 g_key_file_set_string(keyfile, group_name,
1776 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1778 if (conf->eap_config->client_cert != NULL)
1779 g_key_file_set_string(keyfile, group_name,
1780 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1782 if (conf->eap_config->private_key != NULL)
1783 g_key_file_set_string(keyfile, group_name,
1784 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1786 if (conf->eap_config->private_key_password != NULL)
1787 g_key_file_set_string(keyfile, group_name,
1788 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1790 if (conf->eap_config->identity != NULL)
1791 g_key_file_set_string(keyfile, group_name,
1792 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1794 if (conf->eap_config->eap_type != NULL)
1795 g_key_file_set_string(keyfile, group_name,
1796 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1798 if (conf->eap_config->eap_auth_type != NULL)
1799 g_key_file_set_string(keyfile, group_name,
1800 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1802 if (conf->eap_config->subject_match != NULL)
1803 g_key_file_set_string(keyfile, group_name,
1804 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1806 ret = wifi_config_save_configuration(ifname, config_id, keyfile);
1808 INFO("Success to save eap configuration [%s]", config_id);
1809 wifi_complete_save_eap_configuration(wifi, context);
1811 INFO("Fail to save eap configuration [%s]", config_id);
1812 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1815 g_key_file_free(keyfile);
1817 __free_wifi_configuration(conf);
1819 g_variant_iter_free(iter);
1824 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1825 const gchar *ifname, const gchar *config_id)
1827 gboolean ret = FALSE;
1829 if ((wifi == NULL) || (config_id == NULL)) {
1830 ERR("Invalid parameter");
1831 netconfig_error_invalid_parameter(context);
1835 ret = _remove_configuration(ifname, config_id);
1837 /* no configuration or error */
1838 ERR("No [%s] configuration", config_id);
1839 netconfig_error_no_profile(context);
1843 wifi_complete_remove_configuration(wifi, context);
1847 /* config field key / value */
1849 * [wifi_macaddress_config_id]
1850 * Name=name (mandatory)
1851 * SSID=SSID (mandatory)
1852 * Frequency=2462 (X)
1854 * AutoConnect=true (Default true)
1855 * Modified=2015-03-20 (X)
1856 * IPv4.method=manual (O)
1857 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1858 * IPv6.method=auto (X)
1859 * IPv6.privacy=disabled (X)
1860 * IPv4.netmask_prefixlen=24 (X)
1861 * IPv4.local_address=192.0.0.1 (O)
1862 * IPv4.gateway=192.0.0.1 (O ? X ?)
1863 * Nameservers=192.168.43.22; (O)
1864 * Proxy.Method=manual (O)
1865 * Proxy.Servers=trst.com:8888; (O)
1867 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1868 const gchar *ifname, const gchar *config_id, const gchar *key, const gchar *value)
1870 gboolean ret = FALSE;
1871 gchar *keyfile_key = NULL;
1873 g_return_val_if_fail(wifi != NULL, TRUE);
1874 g_return_val_if_fail(config_id != NULL, TRUE);
1875 g_return_val_if_fail(key != NULL, TRUE);
1877 DBG("Key[%s] Value[%s]", key, value);
1879 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1880 ret = _set_field(ifname, config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1882 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1883 netconfig_error_invalid_parameter(context);
1886 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1887 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1888 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1889 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1890 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1891 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1892 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1893 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1894 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1895 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1896 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1897 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1898 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1899 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1900 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1901 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1902 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1903 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1904 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1906 ERR("Not supported key[%s]", key);
1907 netconfig_error_invalid_parameter(context);
1911 ret = _set_field(ifname, config_id, keyfile_key, (const gchar *)value);
1913 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1916 if (keyfile_key != NULL)
1917 g_free(keyfile_key);
1919 wifi_complete_set_config_field(wifi, context);
1923 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context,
1924 const gchar *ifname, const gchar *config_id)
1926 gboolean ret = FALSE;
1927 gchar *passphrase = NULL;
1929 if ((wifi == NULL) || (config_id == NULL)) {
1930 ERR("Invalid parameter");
1931 netconfig_error_invalid_parameter(context);
1935 ret = _get_field(ifname, config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1937 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1938 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1942 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1948 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1949 const gchar *ifname, int frame_id, const gchar *vsie)
1951 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1953 g_return_val_if_fail(wifi != NULL, TRUE);
1954 g_return_val_if_fail(vsie != NULL, TRUE);
1956 gboolean ret = FALSE;
1958 ret = _add_vsie(ifname, frame_id, vsie);
1960 DBG("Failed to add vsie: %s", vsie);
1961 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1965 wifi_complete_add_vsie(wifi, context);
1969 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1970 const gchar *ifname, int frame_id)
1972 DBG("Frame ID: [%d]", frame_id);
1974 g_return_val_if_fail(wifi != NULL, TRUE);
1976 gboolean ret = FALSE;
1979 ret = _get_vsie(ifname, frame_id, &vsie);
1981 DBG("Failed to get vsie for frame:[%d]", frame_id);
1982 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1986 DBG("Received vsie: %s", vsie);
1987 wifi_complete_get_vsie(wifi, context, vsie);
1992 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1993 const gchar *ifname, int frame_id, const gchar *vsie)
1995 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1997 g_return_val_if_fail(wifi != NULL, TRUE);
1998 g_return_val_if_fail(vsie != NULL, TRUE);
2000 gboolean ret = FALSE;
2002 ret = _remove_vsie(ifname, frame_id, vsie);
2004 DBG("Failed to remove vsie: %s", vsie);
2005 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
2009 wifi_complete_remove_vsie(wifi, context);