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 "netsupplicant.h"
39 #include "wifi-key-encryption.h"
41 #define CONNMAN_STORAGE "/var/lib/connman"
43 #define WIFI_SECURITY_NONE "none"
44 #define WIFI_SECURITY_WEP "wep"
45 #define WIFI_SECURITY_WPA_PSK "psk"
46 #define WIFI_SECURITY_EAP "ieee8021x"
48 #define WIFI_CONFIG_PREFIX "wifi_"
49 #define MAC_ADDRESS_LENGTH 12
50 #define WIFI_PREFIX_LENGTH MAC_ADDRESS_LENGTH + 6 /* wifi_485a3f2f506a_ */
51 #define PROFILE_PREFIX_LENGTH WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
53 #define WIFI_MAC_ADD_LENGTH 17
54 #define WIFI_MAC_ADD_PATH "/sys/class/net/wlan0/address"
56 #define NET_DNS_ADDR_MAX 2
58 struct wifi_eap_config {
59 gchar *anonymous_identity;
63 gchar *private_key_password;
73 gchar *gateway_address;
74 gchar *dns_address[NET_DNS_ADDR_MAX];
90 struct wifi_eap_config *eap_config;
91 wifi_ip_info_s *ip_info;
95 static void __free_wifi_configuration(struct wifi_config *conf)
102 g_free(conf->passphrase);
103 g_free(conf->security_type);
104 g_free(conf->is_hidden);
105 g_free(conf->proxy_address);
106 g_free(conf->last_error);
107 if (conf->eap_config) {
108 g_free(conf->eap_config->anonymous_identity);
109 g_free(conf->eap_config->ca_cert);
110 g_free(conf->eap_config->client_cert);
111 g_free(conf->eap_config->private_key);
112 g_free(conf->eap_config->private_key_password);
113 g_free(conf->eap_config->identity);
114 g_free(conf->eap_config->eap_type);
115 g_free(conf->eap_config->eap_auth_type);
116 g_free(conf->eap_config->subject_match);
117 g_free(conf->eap_config);
121 g_free(conf->ip_info->ip_type);
122 g_free(conf->ip_info->ip_address);
123 g_free(conf->ip_info->subnet_mask);
124 g_free(conf->ip_info->gateway_address);
125 g_free(conf->ip_info->dns_type);
127 int i = 0, count = conf->ip_info->dns_count;
129 g_free(conf->ip_info->dns_address[i]);
132 g_free(conf->ip_info);
137 static gboolean __get_mac_address(gchar **mac_address)
139 gchar *tmp_mac = NULL;
141 gchar mac[13] = { 0, };
146 char buf[WIFI_MAC_ADD_LENGTH + 1];
147 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
148 fp = fopen(WIFI_MAC_ADD_PATH, "r");
151 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
156 if (fgets(buf, sizeof(buf), fp) == NULL) {
157 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
162 tmp_mac = (gchar *)malloc(WIFI_MAC_ADD_LENGTH + 1);
163 if (tmp_mac == NULL) {
164 ERR("malloc() failed");
169 memset(tmp_mac, 0, WIFI_MAC_ADD_LENGTH + 1);
170 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
173 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
174 if (tmp_mac == NULL) {
175 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
180 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
182 while (tmp && tmp[i]) {
188 *mac_address = g_strdup(mac);
193 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
195 gchar *mac_address = NULL;
196 gchar *g_name = NULL;
197 gboolean ret = FALSE;
199 ret = __get_mac_address(&mac_address);
200 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
201 ERR("Cannot get WIFI MAC address");
206 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
207 if (g_name == NULL) {
212 *group_name = g_strdup(g_name);
220 static gboolean __get_security_type(const gchar *config_id, gchar **type)
222 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
223 *type = g_strdup(WIFI_SECURITY_NONE);
224 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
225 *type = g_strdup(WIFI_SECURITY_WEP);
226 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
227 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
228 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
229 *type = g_strdup(WIFI_SECURITY_EAP);
238 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
240 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
241 if (*config_id == NULL) {
250 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
252 GKeyFile *keyfile = NULL;
255 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
257 keyfile = netconfig_keyfile_load(path);
259 ERR("keyfile[%s] is NULL", path);
266 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
268 gboolean ret = FALSE;
271 path = g_strdup_printf("%s/%s", pathname, filename);
272 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
274 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
283 static gboolean __remove_configuration(const gchar *pathname)
287 if (__remove_file(pathname, "settings") != TRUE) {
288 ERR("Cannot remove [%s/settings]", pathname);
291 if (__remove_file(pathname, "data") != TRUE) {
292 ERR("Cannot remove [%s/data]", pathname);
296 ret = rmdir(pathname);
298 ERR("Cannot remove [%s]", pathname);
305 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
309 gboolean hidden = FALSE;
310 gboolean ret = FALSE;
312 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
314 ERR("Fail to get_wifi_config_group_name");
318 keyfile = __get_configuration_keyfile(group_name);
319 if (keyfile == NULL) {
320 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
325 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
326 DBG("name [%s]", config->name);
328 __get_security_type(config_id, &config->security_type);
329 if (config->security_type == NULL) {
330 ERR("Fail to _get_security_type");
331 g_key_file_free(keyfile);
335 DBG("security_type [%s]", config->security_type);
337 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
338 if (config->proxy_address)
339 DBG("proxy_address [%s]", config->proxy_address);
341 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
343 config->is_hidden = g_strdup("TRUE");
345 config->is_hidden = g_strdup("FALSE");
346 DBG("is_hidden [%s]", config->is_hidden);
348 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
349 WIFI_CONFIG_IPV4_METHOD, NULL);
350 if (config->ip_info->ip_type)
351 DBG("IPv4.Method:%s", config->ip_info->ip_type);
353 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
354 WIFI_CONFIG_IPV4_ADDRESS, NULL);
355 if (config->ip_info->ip_address)
356 DBG("IPv4.Address:%s", config->ip_info->ip_address);
360 struct in_addr netmask;
362 prefix_len = g_key_file_get_integer(keyfile, group_name,
363 WIFI_CONFIG_IPV4_SUBNET_MASK, NULL);
364 addr = 0xffffffff << (32 - prefix_len);
365 netmask.s_addr = htonl(addr);
366 mask = inet_ntoa(netmask);
367 config->ip_info->subnet_mask = g_strdup(mask);
368 if (config->ip_info->subnet_mask)
369 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
371 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
372 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS,
374 if (config->ip_info->gateway_address)
375 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
377 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
378 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
379 if (config->ip_info->dns_type)
380 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
384 nameservers = g_key_file_get_string_list(keyfile, group_name,
385 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
388 config->ip_info->dns_count = length;
390 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
391 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
392 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
396 g_strfreev(nameservers);
400 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
401 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
402 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
403 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
404 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
405 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
406 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
407 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
408 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
409 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
411 if (config->eap_config->anonymous_identity)
412 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
413 if (config->eap_config->ca_cert)
414 DBG("ca_cert [%s]", config->eap_config->ca_cert);
415 if (config->eap_config->client_cert)
416 DBG("client_cert [%s]", config->eap_config->client_cert);
417 if (config->eap_config->private_key)
418 DBG("private_key [%s]", config->eap_config->private_key);
419 if (config->eap_config->private_key_password)
420 DBG("private_key_password [%s]", config->eap_config->private_key_password);
421 if (config->eap_config->identity)
422 DBG("identity [%s]", config->eap_config->identity);
423 if (config->eap_config->eap_type)
424 DBG("eap_type [%s]", config->eap_config->eap_type);
425 if (config->eap_config->eap_auth_type)
426 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
427 if (config->eap_config->subject_match)
428 DBG("subject_match [%s]", config->eap_config->subject_match);
431 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
432 if (config->last_error)
433 DBG("last_error [%s]", config->last_error);
435 g_key_file_free(keyfile);
441 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
446 gboolean ret = FALSE;
448 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
450 ERR("Fail to get_wifi_config_group_name");
454 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
455 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
456 if (__remove_configuration(dir) != TRUE) {
457 ERR("[%s] is existed, but cannot remove", dir);
464 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
465 ERR("Cannot mkdir %s", dir);
471 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
472 netconfig_keyfile_save(keyfile, path);
480 static gboolean _remove_configuration(const gchar *config_id)
482 gboolean ret = FALSE;
486 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
488 ERR("Fail to get_wifi_config_group_name");
492 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
493 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
494 if (__remove_configuration(dir) != TRUE) {
495 ERR("[%s] is existed, but cannot remove", dir);
498 INFO("Success to remove [%s]", dir);
501 ERR("[%s] is not existed", dir);
512 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
518 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
520 ERR("Fail to get_wifi_config_group_name");
523 DBG("group_name %s", group_name);
525 keyfile = __get_configuration_keyfile(group_name);
526 if (keyfile == NULL) {
527 ERR("Fail to __get_configuration_keyfile");
532 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
533 g_key_file_set_string(keyfile, group_name, key, value);
534 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
535 g_key_file_set_string(keyfile, group_name, key, value);
536 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
537 gboolean hidden = FALSE;
538 if (g_strcmp0(value, "TRUE") == 0)
540 g_key_file_set_boolean(keyfile, group_name, key, hidden);
541 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
542 g_key_file_set_string(keyfile, group_name, key, value);
543 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
544 g_key_file_set_string(keyfile, group_name, key, value);
545 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
546 g_key_file_set_string(keyfile, group_name, key, value);
547 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
548 g_key_file_set_string(keyfile, group_name, key, value);
549 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
550 g_key_file_set_string(keyfile, group_name, key, value);
551 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
552 g_key_file_set_string(keyfile, group_name, key, value);
553 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
554 g_key_file_set_string(keyfile, group_name, key, value);
555 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
556 g_key_file_set_string(keyfile, group_name, key, value);
558 ERR("key[%s] is not supported", key);
562 _save_configuration(config_id, keyfile);
564 g_key_file_free(keyfile);
570 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
575 gboolean hidden = FALSE;
576 gboolean ret = FALSE;
578 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
580 ERR("Fail to get_wifi_config_group_name");
583 DBG("group_name %s", group_name);
585 keyfile = __get_configuration_keyfile(group_name);
586 if (keyfile == NULL) {
587 ERR("Fail to __get_configuration_keyfile");
592 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
593 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
594 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
595 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
596 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
597 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
598 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
599 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
601 val = g_strdup("TRUE");
603 val = g_strdup("FALSE");
604 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
605 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
606 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
607 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
608 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
609 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
610 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
611 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
612 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
613 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
614 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
615 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
616 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
617 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
618 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
619 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
620 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
621 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
623 ERR("Invalid key[%s]", key);
624 val = g_strdup("NOTSUPPORTED");
627 *value = g_strdup(val);
630 g_key_file_free(keyfile);
636 static GSList *_get_list(void)
639 struct dirent *dp = NULL;
642 dir = opendir(CONNMAN_STORAGE);
644 ERR("Cannot open dir %s", CONNMAN_STORAGE);
648 while ((dp = readdir(dir)) != NULL) {
649 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
650 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
653 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
654 list = g_slist_append(list, g_strdup(config_id));
662 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
664 gboolean ret = FALSE;
667 if ((service_profile == NULL) || (config_id == NULL)) {
668 ERR("Invalid parameter");
672 ret = __get_config_id(service_profile, &val);
673 *config_id = g_strdup(val);
679 gboolean wifi_config_remove_configuration(const gchar *config_id)
681 gboolean ret = FALSE;
683 ret = _remove_configuration(config_id);
688 int __netconfig_hex_char_to_num(char c)
690 if (c >= '0' && c <= '9')
693 if (c >= 'a' && c <= 'f')
696 if (c >= 'A' && c <= 'F')
702 int __netconfig_hex_to_byte(const char *hex)
706 a = __netconfig_hex_char_to_num(*hex++);
710 b = __netconfig_hex_char_to_num(*hex++);
717 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
721 const char *ipos = hex;
722 unsigned char *opos = buf;
724 for (i = 0; i < len; i++) {
725 a = __netconfig_hex_to_byte(ipos);
736 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
743 ERR("Invalid parameter.");
747 *dst = (char *) g_try_malloc0((2*src_len)+1);
749 ERR("failed to allocate memory to buffer.");
755 for (i = 0; i < src_len; i++) {
756 snprintf(buf, 3, "%02x", src[i]);
764 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
766 GVariantIter *iter_copy = NULL;
769 unsigned char *tmp_dst = NULL;
771 if (!dst || *dst || !iter) {
772 ERR("Invalid parameter");
776 iter_copy = g_variant_iter_copy(iter);
778 while (g_variant_iter_loop(iter, "y", &tmp))
780 g_variant_iter_free(iter);
782 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
784 ERR("failed to allocate memory");
785 g_variant_iter_free(iter_copy);
790 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
792 g_variant_iter_free(iter_copy);
798 tmp_dst[length] = '\0';
802 DBG("Length [%d]", length);
806 gboolean _add_vsie(int frame_id, const char* vsie)
808 GVariant *params = NULL;
809 GVariant *message = NULL;
810 GVariantBuilder *bytearray_builder = NULL;
815 unsigned char *bytearray = NULL;
816 size_t bytearray_len = 0;
818 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
819 DBG("Invalid parameter, frame-id: %d", frame_id);
823 vsie_len = strlen(vsie);
825 DBG("vsie length is zero");
829 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
831 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
832 if (bytearray == NULL) {
833 DBG("Failed to allocate memory to bytearray");
837 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
838 DBG("invalid vsie string");
843 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
844 for (i = 0; i < bytearray_len; i++)
845 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
847 params = g_variant_new("(iay)", frame_id, bytearray_builder);
848 g_variant_builder_unref(bytearray_builder);
850 if_path = netconfig_wifi_get_supplicant_interface();
852 if (if_path == NULL) {
853 ERR("Fail to get wpa_supplicant DBus path");
858 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
859 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
862 if (message == NULL) {
863 ERR("Failed to send command to wpa_supplicant");
868 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
874 gboolean _get_vsie(int frame_id, char **vsie)
876 GVariant *params = NULL;
877 GVariant *message = NULL;
880 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
881 DBG("Invalid parameter, frame-id: %d", frame_id);
885 if_path = netconfig_wifi_get_supplicant_interface();
886 if (if_path == NULL) {
887 ERR("Fail to get wpa_supplicant DBus path");
891 params = g_variant_new("(i)", frame_id);
893 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
894 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
897 if (message == NULL) {
898 ERR("Failed to send command to wpa_supplicant");
901 GVariantIter *iter = NULL;
902 unsigned char *vsie_bytes = NULL;
906 g_variant_get(message, "(ay)", &iter);
908 ERR("vsie is not present");
912 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
913 if (vsie_bytes == NULL) {
914 ERR("vsie_bytes not allocated");
918 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
921 ERR("vsie not allocated.");
928 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
934 gboolean _remove_vsie(int frame_id, const char *vsie)
936 GVariant *params = NULL;
937 GVariant *message = NULL;
938 GVariantBuilder *bytearray_builder = NULL;
943 unsigned char *bytearray = NULL;
944 size_t bytearray_len = 0;
946 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
947 DBG("Invalid parameter, frame-id: %d", frame_id);
951 vsie_len = strlen(vsie);
953 DBG("vsie length is zero");
957 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
959 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
960 if (bytearray == NULL) {
961 DBG("Failed to allocate memory to bytearray");
965 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
966 DBG("invalid vsie string");
971 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
972 for (i = 0; i < bytearray_len; i++)
973 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
975 params = g_variant_new("(iay)", frame_id, bytearray_builder);
976 g_variant_builder_unref(bytearray_builder);
978 if_path = netconfig_wifi_get_supplicant_interface();
979 if (if_path == NULL) {
980 ERR("Fail to get wpa_supplicant DBus path");
985 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
986 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
989 if (message == NULL) {
990 ERR("Failed to send command to wpa_supplicant");
995 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1002 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
1005 GSList *config_ids = NULL;
1007 gchar **result = NULL;
1009 g_return_val_if_fail(wifi != NULL, TRUE);
1011 config_ids = _get_list();
1012 if (config_ids == NULL) {
1013 ERR("Fail to get config list");
1014 netconfig_error_no_profile(context);
1018 length = g_slist_length(config_ids);
1019 result = g_new0(gchar *, length + 1);
1020 for (i = 0; i < length; i++) {
1021 gchar *config_id = g_slist_nth_data(config_ids, i);
1022 result[i] = g_strdup(config_id);
1025 config_ids = g_slist_nth(config_ids, 0);
1026 g_slist_free_full(config_ids, g_free);
1028 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1030 for (i = 0; i < length; i++)
1040 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1041 const gchar *config_id)
1043 gboolean ret = FALSE;
1044 GVariantBuilder *b = NULL;
1045 struct wifi_config *conf = NULL;
1047 g_return_val_if_fail(wifi != NULL, TRUE);
1049 conf = g_new0(struct wifi_config, 1);
1050 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1052 ret = _load_configuration(config_id, conf);
1054 g_free(conf->ip_info);
1056 ERR("Fail to _load_configuration");
1057 netconfig_error_no_profile(context);
1061 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1062 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1063 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1064 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1066 if (conf->proxy_address != NULL)
1067 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1069 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1071 if (conf->ip_info->ip_type != NULL)
1072 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1074 if (conf->ip_info->ip_address != NULL)
1075 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1077 if (conf->ip_info->subnet_mask != NULL)
1078 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1080 if (conf->ip_info->gateway_address != NULL)
1081 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1083 if (conf->ip_info->dns_type != NULL)
1084 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1086 int i = 0, count = conf->ip_info->dns_count;
1088 if (conf->ip_info->dns_address[i] != NULL)
1089 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1094 if (conf->last_error != NULL)
1095 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1097 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1099 __free_wifi_configuration(conf);
1101 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1102 g_variant_builder_unref(b);
1106 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1107 const char *netmask)
1116 mask = inet_network(netmask);
1119 /* a valid netmask must be 2^n - 1 */
1120 if ((host & (host + 1)) != 0)
1124 for (; mask; mask <<= 1)
1130 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1131 const gchar *config_id, GVariant *configuration)
1133 gboolean ret = FALSE;
1134 struct wifi_config *conf = NULL;
1135 GKeyFile *keyfile = NULL;
1139 gchar *group_name = NULL;
1142 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1143 ERR("Invalid parameter");
1144 netconfig_error_invalid_parameter(context);
1148 conf = g_new0(struct wifi_config, 1);
1149 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1151 g_variant_get(configuration, "a{sv}", &iter);
1152 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1153 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1154 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1155 conf->name = g_strdup(g_variant_get_string(value, NULL));
1156 DBG("name [%s]", conf->name);
1160 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1161 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1162 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1163 DBG("ssid [%s]", conf->ssid);
1167 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1168 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1169 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1170 DBG("passphrase []");
1172 conf->passphrase = NULL;
1174 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1175 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1176 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1177 DBG("is_hidden [%s]", conf->is_hidden);
1179 conf->is_hidden = NULL;
1181 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1182 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1183 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1184 DBG("IP config type [%s]", conf->ip_info->ip_type);
1186 conf->ip_info->ip_type = NULL;
1188 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1189 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1190 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1191 DBG("IP address [%s]", conf->ip_info->ip_address);
1193 conf->ip_info->ip_address = NULL;
1195 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1196 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1197 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1198 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1200 conf->ip_info->subnet_mask = NULL;
1202 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1203 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1204 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1205 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1207 conf->ip_info->gateway_address = NULL;
1209 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1210 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1211 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1212 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1214 conf->ip_info->dns_type = NULL;
1216 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1217 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1218 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1219 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1220 conf->ip_info->dns_count = order + 1;
1223 conf->ip_info->dns_address[order++] = NULL;
1225 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1226 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1227 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1228 DBG("proxy_address [%s]", conf->proxy_address);
1230 conf->proxy_address = NULL;
1234 conf->favorite = TRUE;
1235 conf->autoconnect = TRUE;
1237 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1239 __free_wifi_configuration(conf);
1240 ERR("Fail to get_wifi_config_group_name");
1241 netconfig_error_fail_save_congifuration(context);
1245 keyfile = g_key_file_new();
1246 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1247 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1249 if (conf->passphrase != NULL) {
1250 gchar *enc_data = NULL;
1251 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1254 ERR("Failed to encrypt the passphrase");
1256 g_free(conf->passphrase);
1257 conf->passphrase = enc_data;
1260 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1263 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1264 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1266 /* Optional field */
1267 if (conf->proxy_address != NULL) {
1268 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1269 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1272 if (conf->is_hidden != NULL) {
1273 gboolean hidden = FALSE;
1274 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1276 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1279 if (conf->ip_info->ip_type != NULL)
1280 g_key_file_set_string(keyfile, group_name,
1281 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1283 if (conf->ip_info->ip_address != NULL)
1284 g_key_file_set_string(keyfile, group_name,
1285 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1287 if (conf->ip_info->subnet_mask != NULL) {
1288 unsigned char prefix_len;
1289 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1290 conf->ip_info->subnet_mask);
1291 g_key_file_set_integer(keyfile, group_name,
1292 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1295 if (conf->ip_info->gateway_address != NULL)
1296 g_key_file_set_string(keyfile, group_name,
1297 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1299 if (conf->ip_info->dns_type != NULL)
1300 g_key_file_set_string(keyfile, group_name,
1301 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1303 int i = 0, count = conf->ip_info->dns_count;
1305 if (conf->ip_info->dns_address[i] != NULL)
1306 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1307 conf->ip_info->dns_address[i]);
1312 ret = _save_configuration(config_id, keyfile);
1314 INFO("Success to save configuration [%s]", config_id);
1315 wifi_complete_save_configuration(wifi, context);
1317 INFO("Fail to save configuration [%s]", config_id);
1318 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1321 g_key_file_free(keyfile);
1323 __free_wifi_configuration(conf);
1325 g_variant_iter_free(iter);
1330 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1331 const gchar *config_id)
1333 gboolean ret = FALSE;
1334 GVariantBuilder *b = NULL;
1335 struct wifi_config *conf = NULL;
1337 g_return_val_if_fail(wifi != NULL, TRUE);
1339 conf = g_new0(struct wifi_config, 1);
1340 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1342 ret = _load_configuration(config_id, conf);
1344 g_free(conf->eap_config);
1346 ERR("Fail to _load_configuration");
1347 netconfig_error_no_profile(context);
1351 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1352 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1353 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1354 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1355 if (conf->proxy_address != NULL)
1356 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1358 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1360 if (conf->last_error != NULL)
1361 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1363 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1365 if (conf->eap_config != NULL) {
1366 if (conf->eap_config->anonymous_identity != NULL)
1367 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1369 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1371 if (conf->eap_config->ca_cert != NULL)
1372 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1374 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1376 if (conf->eap_config->client_cert != NULL)
1377 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1379 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1381 if (conf->eap_config->private_key != NULL)
1382 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1384 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1386 if (conf->eap_config->private_key_password != NULL)
1387 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1389 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1391 if (conf->eap_config->identity != NULL)
1392 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1394 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1396 if (conf->eap_config->eap_type != NULL)
1397 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1399 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1401 if (conf->eap_config->eap_auth_type != NULL)
1402 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1404 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1406 if (conf->eap_config->subject_match != NULL)
1407 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1409 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1412 __free_wifi_configuration(conf);
1414 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1415 g_variant_builder_unref(b);
1419 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1420 const gchar *config_id, GVariant *configuration)
1422 gboolean ret = FALSE;
1423 struct wifi_config *conf = NULL;
1424 GKeyFile *keyfile = NULL;
1428 gchar *group_name = NULL;
1430 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1431 ERR("Invalid parameter");
1432 netconfig_error_invalid_parameter(context);
1436 conf = g_new0(struct wifi_config, 1);
1437 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1439 g_variant_get(configuration, "a{sv}", &iter);
1440 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1441 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1442 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1443 conf->name = g_strdup(g_variant_get_string(value, NULL));
1444 DBG("name [%s]", conf->name);
1448 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1449 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1450 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1451 DBG("ssid [%s]", conf->ssid);
1455 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1456 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1457 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1458 DBG("passphrase [%s]", conf->passphrase);
1460 conf->passphrase = NULL;
1462 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1463 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1464 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1465 DBG("is_hidden [%s]", conf->is_hidden);
1467 conf->is_hidden = NULL;
1469 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1470 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1471 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1472 DBG("proxy_address [%s]", conf->proxy_address);
1474 conf->proxy_address = NULL;
1476 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1477 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1478 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1479 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1481 conf->eap_config->anonymous_identity = NULL;
1483 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1484 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1485 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1486 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1488 conf->eap_config->ca_cert = NULL;
1490 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1491 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1492 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1493 DBG("client_cert [%s]", conf->eap_config->client_cert);
1495 conf->eap_config->client_cert = NULL;
1497 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1498 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1499 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1500 DBG("private_key [%s]", conf->eap_config->private_key);
1502 conf->eap_config->private_key = NULL;
1504 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1505 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1506 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1507 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1509 conf->eap_config->private_key_password = NULL;
1511 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1512 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1513 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1514 DBG("identity [%s]", conf->eap_config->identity);
1516 conf->eap_config->identity = NULL;
1518 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1519 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1520 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1521 DBG("eap_type [%s]", conf->eap_config->eap_type);
1523 conf->eap_config->eap_type = NULL;
1525 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1526 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1527 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1528 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1530 conf->eap_config->eap_auth_type = NULL;
1532 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1533 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1534 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1535 DBG("subject_match [%s]", conf->eap_config->subject_match);
1537 conf->eap_config->subject_match = NULL;
1541 conf->favorite = TRUE;
1542 conf->autoconnect = TRUE;
1544 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1546 __free_wifi_configuration(conf);
1547 ERR("Fail to get_wifi_config_group_name");
1551 keyfile = g_key_file_new();
1552 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1553 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1555 if (conf->passphrase != NULL) {
1556 gchar *enc_data = NULL;
1557 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1560 ERR("Failed to encrypt the passphrase");
1562 g_free(conf->passphrase);
1563 conf->passphrase = enc_data;
1565 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1568 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1569 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1571 /* Optional field */
1572 if (conf->proxy_address != NULL) {
1573 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1574 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1577 if (conf->is_hidden != NULL) {
1578 gboolean hidden = FALSE;
1579 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1581 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1584 if (conf->eap_config->anonymous_identity != NULL)
1585 g_key_file_set_string(keyfile, group_name,
1586 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1588 if (conf->eap_config->ca_cert != NULL)
1589 g_key_file_set_string(keyfile, group_name,
1590 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1592 if (conf->eap_config->client_cert != NULL)
1593 g_key_file_set_string(keyfile, group_name,
1594 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1596 if (conf->eap_config->private_key != NULL)
1597 g_key_file_set_string(keyfile, group_name,
1598 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1600 if (conf->eap_config->private_key_password != NULL)
1601 g_key_file_set_string(keyfile, group_name,
1602 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1604 if (conf->eap_config->identity != NULL)
1605 g_key_file_set_string(keyfile, group_name,
1606 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1608 if (conf->eap_config->eap_type != NULL)
1609 g_key_file_set_string(keyfile, group_name,
1610 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1612 if (conf->eap_config->eap_auth_type != NULL)
1613 g_key_file_set_string(keyfile, group_name,
1614 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1616 if (conf->eap_config->subject_match != NULL)
1617 g_key_file_set_string(keyfile, group_name,
1618 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1620 ret = _save_configuration(config_id, keyfile);
1622 INFO("Success to save eap configuration [%s]", config_id);
1623 wifi_complete_save_eap_configuration(wifi, context);
1625 INFO("Fail to save eap configuration [%s]", config_id);
1626 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1629 g_key_file_free(keyfile);
1631 __free_wifi_configuration(conf);
1633 g_variant_iter_free(iter);
1638 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1640 gboolean ret = FALSE;
1642 if ((wifi == NULL) || (config_id == NULL)) {
1643 ERR("Invalid parameter");
1644 netconfig_error_invalid_parameter(context);
1648 ret = _remove_configuration(config_id);
1650 /* no configuration or error */
1651 ERR("No [%s] configuration", config_id);
1652 netconfig_error_no_profile(context);
1656 wifi_complete_remove_configuration(wifi, context);
1660 /* config field key / value */
1662 * [wifi_macaddress_config_id]
1663 * Name=name (mandatory)
1664 * SSID=SSID (mandatory)
1665 * Frequency=2462 (X)
1667 * AutoConnect=true (Default true)
1668 * Modified=2015-03-20 (X)
1669 * IPv4.method=manual (O)
1670 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1671 * IPv6.method=auto (X)
1672 * IPv6.privacy=disabled (X)
1673 * IPv4.netmask_prefixlen=24 (X)
1674 * IPv4.local_address=192.0.0.1 (O)
1675 * IPv4.gateway=192.0.0.1 (O ? X ?)
1676 * Nameservers=192.168.43.22; (O)
1677 * Proxy.Method=manual (O)
1678 * Proxy.Servers=trst.com:8888; (O)
1680 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1681 const gchar *config_id, const gchar *key, const gchar *value)
1683 gboolean ret = FALSE;
1684 gchar *keyfile_key = NULL;
1686 g_return_val_if_fail(wifi != NULL, TRUE);
1687 g_return_val_if_fail(config_id != NULL, TRUE);
1688 g_return_val_if_fail(key != NULL, TRUE);
1690 DBG("Key[%s] Value[%d]", key, value);
1692 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1693 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1695 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1696 netconfig_error_invalid_parameter(context);
1699 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1700 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1701 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1702 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1703 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1704 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1705 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1706 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1707 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1708 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1709 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1710 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1711 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1712 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1713 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1714 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1715 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1716 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1717 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1719 ERR("Not supported key[%s]", key);
1720 netconfig_error_invalid_parameter(context);
1724 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1726 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1729 if (keyfile_key != NULL)
1730 g_free(keyfile_key);
1732 wifi_complete_set_config_field(wifi, context);
1736 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1738 gboolean ret = FALSE;
1739 gchar *passphrase = NULL;
1741 if ((wifi == NULL) || (config_id == NULL)) {
1742 ERR("Invalid parameter");
1743 netconfig_error_invalid_parameter(context);
1747 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1749 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1750 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1754 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1760 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1761 int frame_id, const gchar *vsie)
1763 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1765 g_return_val_if_fail(wifi != NULL, TRUE);
1766 g_return_val_if_fail(vsie != NULL, TRUE);
1768 gboolean ret = FALSE;
1770 ret = _add_vsie(frame_id, vsie);
1772 DBG("Failed to add vsie: %s", vsie);
1773 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1777 wifi_complete_add_vsie(wifi, context);
1781 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1784 DBG("Frame ID: [%d]", frame_id);
1786 g_return_val_if_fail(wifi != NULL, TRUE);
1788 gboolean ret = FALSE;
1791 ret = _get_vsie(frame_id, &vsie);
1793 DBG("Failed to get vsie for frame:[%d]", frame_id);
1794 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1798 DBG("Received vsie: %s", vsie);
1799 wifi_complete_get_vsie(wifi, context, vsie);
1804 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1805 int frame_id, const gchar *vsie)
1807 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1809 g_return_val_if_fail(wifi != NULL, TRUE);
1810 g_return_val_if_fail(vsie != NULL, TRUE);
1812 gboolean ret = FALSE;
1814 ret = _remove_vsie(frame_id, vsie);
1816 DBG("Failed to remove vsie: %s", vsie);
1817 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1821 wifi_complete_remove_vsie(wifi, context);