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);
126 conf->ip_info->prefix_length = 0;
128 int i = 0, count = conf->ip_info->dns_count;
130 g_free(conf->ip_info->dns_address[i]);
133 g_free(conf->ip_info);
138 static gboolean __get_mac_address(gchar **mac_address)
140 gchar *tmp_mac = NULL;
142 gchar mac[13] = { 0, };
147 char buf[WIFI_MAC_ADD_LENGTH + 1];
148 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
149 fp = fopen(WIFI_MAC_ADD_PATH, "r");
152 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
157 if (fgets(buf, sizeof(buf), fp) == NULL) {
158 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
163 tmp_mac = (gchar *)malloc(WIFI_MAC_ADD_LENGTH + 1);
164 if (tmp_mac == NULL) {
165 ERR("malloc() failed");
170 memset(tmp_mac, 0, WIFI_MAC_ADD_LENGTH + 1);
171 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
174 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
175 if (tmp_mac == NULL) {
176 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
181 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
183 while (tmp && tmp[i]) {
189 *mac_address = g_strdup(mac);
195 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
197 gchar *mac_address = NULL;
198 gchar *g_name = NULL;
199 gboolean ret = FALSE;
201 ret = __get_mac_address(&mac_address);
202 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
203 ERR("Cannot get WIFI MAC address");
208 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
209 if (g_name == NULL) {
214 *group_name = g_strdup(g_name);
222 static gboolean __get_security_type(const gchar *config_id, gchar **type)
224 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
225 *type = g_strdup(WIFI_SECURITY_NONE);
226 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
227 *type = g_strdup(WIFI_SECURITY_WEP);
228 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
229 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
230 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
231 *type = g_strdup(WIFI_SECURITY_EAP);
240 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
242 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
243 if (*config_id == NULL) {
252 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
254 GKeyFile *keyfile = NULL;
257 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
259 keyfile = netconfig_keyfile_load(path);
261 ERR("keyfile[%s] is NULL", path);
268 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
270 gboolean ret = FALSE;
273 path = g_strdup_printf("%s/%s", pathname, filename);
274 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
276 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
285 static gboolean __remove_configuration(const gchar *pathname)
289 if (__remove_file(pathname, "settings") != TRUE) {
290 ERR("Cannot remove [%s/settings]", pathname);
293 if (__remove_file(pathname, "data") != TRUE) {
294 ERR("Cannot remove [%s/data]", pathname);
298 ret = rmdir(pathname);
300 ERR("Cannot remove [%s]", pathname);
307 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
311 gboolean hidden = FALSE;
312 gboolean ret = FALSE;
314 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
316 ERR("Fail to get_wifi_config_group_name");
320 keyfile = __get_configuration_keyfile(group_name);
321 if (keyfile == NULL) {
322 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
327 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
328 DBG("name [%s]", config->name);
330 __get_security_type(config_id, &config->security_type);
331 if (config->security_type == NULL) {
332 ERR("Fail to _get_security_type");
333 g_key_file_free(keyfile);
337 DBG("security_type [%s]", config->security_type);
339 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
340 if (config->proxy_address)
341 DBG("proxy_address [%s]", config->proxy_address);
343 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
345 config->is_hidden = g_strdup("TRUE");
347 config->is_hidden = g_strdup("FALSE");
348 DBG("is_hidden [%s]", config->is_hidden);
350 if (config->ip_info) {
351 GError *error = NULL;
352 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
353 WIFI_CONFIG_IPV4_METHOD, NULL);
354 if (config->ip_info->ip_type)
355 DBG("IPv4.Method:%s", config->ip_info->ip_type);
357 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
358 WIFI_CONFIG_IPV4_ADDRESS, NULL);
359 if (config->ip_info->ip_address)
360 DBG("IPv4.Address:%s", config->ip_info->ip_address);
364 struct in_addr netmask;
366 prefix_len = g_key_file_get_integer(keyfile, group_name,
367 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
369 DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
372 addr = 0xffffffff << (32 - prefix_len);
373 netmask.s_addr = htonl(addr);
374 mask = inet_ntoa(netmask);
375 config->ip_info->subnet_mask = g_strdup(mask);
376 if (config->ip_info->subnet_mask)
377 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
380 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
381 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
382 if (config->ip_info->gateway_address)
383 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
385 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
386 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
387 if (config->ip_info->dns_type)
388 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
392 nameservers = g_key_file_get_string_list(keyfile, group_name,
393 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
396 config->ip_info->dns_count = length;
398 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
399 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
400 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
404 g_strfreev(nameservers);
409 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
410 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
411 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
412 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
413 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
414 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
415 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
416 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
417 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
418 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
420 if (config->eap_config->anonymous_identity)
421 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
422 if (config->eap_config->ca_cert)
423 DBG("ca_cert [%s]", config->eap_config->ca_cert);
424 if (config->eap_config->client_cert)
425 DBG("client_cert [%s]", config->eap_config->client_cert);
426 if (config->eap_config->private_key)
427 DBG("private_key [%s]", config->eap_config->private_key);
428 if (config->eap_config->private_key_password)
429 DBG("private_key_password [%s]", config->eap_config->private_key_password);
430 if (config->eap_config->identity)
431 DBG("identity [%s]", config->eap_config->identity);
432 if (config->eap_config->eap_type)
433 DBG("eap_type [%s]", config->eap_config->eap_type);
434 if (config->eap_config->eap_auth_type)
435 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
436 if (config->eap_config->subject_match)
437 DBG("subject_match [%s]", config->eap_config->subject_match);
440 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
441 if (config->last_error)
442 DBG("last_error [%s]", config->last_error);
444 g_key_file_free(keyfile);
450 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
455 gboolean ret = FALSE;
457 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
459 ERR("Fail to get_wifi_config_group_name");
463 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
464 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
465 if (__remove_configuration(dir) != TRUE) {
466 ERR("[%s] is existed, but cannot remove", dir);
473 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
474 ERR("Cannot mkdir %s", dir);
480 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
481 netconfig_keyfile_save(keyfile, path);
489 static gboolean _remove_configuration(const gchar *config_id)
491 gboolean ret = FALSE;
495 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
497 ERR("Fail to get_wifi_config_group_name");
501 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
502 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
503 if (__remove_configuration(dir) != TRUE) {
504 ERR("[%s] is existed, but cannot remove", dir);
507 INFO("Success to remove [%s]", dir);
510 ERR("[%s] is not existed", dir);
521 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
527 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
529 ERR("Fail to get_wifi_config_group_name");
532 DBG("group_name %s", group_name);
534 keyfile = __get_configuration_keyfile(group_name);
535 if (keyfile == NULL) {
536 ERR("Fail to __get_configuration_keyfile");
541 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
542 g_key_file_set_string(keyfile, group_name, key, value);
543 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
544 g_key_file_set_string(keyfile, group_name, key, value);
545 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
546 gboolean hidden = FALSE;
547 if (g_strcmp0(value, "TRUE") == 0)
549 g_key_file_set_boolean(keyfile, group_name, key, hidden);
550 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
551 g_key_file_set_string(keyfile, group_name, key, value);
552 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
553 g_key_file_set_string(keyfile, group_name, key, value);
554 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
555 g_key_file_set_string(keyfile, group_name, key, value);
556 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
557 g_key_file_set_string(keyfile, group_name, key, value);
558 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
559 g_key_file_set_string(keyfile, group_name, key, value);
560 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
561 g_key_file_set_string(keyfile, group_name, key, value);
562 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
563 g_key_file_set_string(keyfile, group_name, key, value);
564 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
565 g_key_file_set_string(keyfile, group_name, key, value);
567 ERR("key[%s] is not supported", key);
571 _save_configuration(config_id, keyfile);
573 g_key_file_free(keyfile);
579 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
584 gboolean hidden = FALSE;
585 gboolean ret = FALSE;
587 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
589 ERR("Fail to get_wifi_config_group_name");
592 DBG("group_name %s", group_name);
594 keyfile = __get_configuration_keyfile(group_name);
595 if (keyfile == NULL) {
596 ERR("Fail to __get_configuration_keyfile");
601 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
602 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
603 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
604 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
605 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
606 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
607 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
608 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
610 val = g_strdup("TRUE");
612 val = g_strdup("FALSE");
613 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
614 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
615 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
616 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
617 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
618 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
619 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
620 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
621 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
622 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
623 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
624 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
625 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
626 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
627 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
628 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
629 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
630 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
632 ERR("Invalid key[%s]", key);
633 val = g_strdup("NOTSUPPORTED");
636 *value = g_strdup(val);
639 g_key_file_free(keyfile);
645 static GSList *_get_list(void)
648 struct dirent *dp = NULL;
651 dir = opendir(CONNMAN_STORAGE);
653 ERR("Cannot open dir %s", CONNMAN_STORAGE);
657 while ((dp = readdir(dir)) != NULL) {
658 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
659 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
662 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
663 list = g_slist_append(list, g_strdup(config_id));
671 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
673 gboolean ret = FALSE;
676 if ((service_profile == NULL) || (config_id == NULL)) {
677 ERR("Invalid parameter");
681 ret = __get_config_id(service_profile, &val);
682 *config_id = g_strdup(val);
688 gboolean wifi_config_remove_configuration(const gchar *config_id)
690 gboolean ret = FALSE;
692 ret = _remove_configuration(config_id);
697 int __netconfig_hex_char_to_num(char c)
699 if (c >= '0' && c <= '9')
702 if (c >= 'a' && c <= 'f')
705 if (c >= 'A' && c <= 'F')
711 int __netconfig_hex_to_byte(const char *hex)
715 a = __netconfig_hex_char_to_num(*hex++);
719 b = __netconfig_hex_char_to_num(*hex++);
726 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
730 const char *ipos = hex;
731 unsigned char *opos = buf;
733 for (i = 0; i < len; i++) {
734 a = __netconfig_hex_to_byte(ipos);
745 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
752 ERR("Invalid parameter.");
756 *dst = (char *) g_try_malloc0((2*src_len)+1);
758 ERR("failed to allocate memory to buffer.");
764 for (i = 0; i < src_len; i++) {
765 snprintf(buf, 3, "%02x", src[i]);
773 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
775 GVariantIter *iter_copy = NULL;
778 unsigned char *tmp_dst = NULL;
780 if (!dst || *dst || !iter) {
781 ERR("Invalid parameter");
785 iter_copy = g_variant_iter_copy(iter);
787 while (g_variant_iter_loop(iter, "y", &tmp))
789 g_variant_iter_free(iter);
791 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
793 ERR("failed to allocate memory");
794 g_variant_iter_free(iter_copy);
799 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
801 g_variant_iter_free(iter_copy);
807 tmp_dst[length] = '\0';
811 DBG("Length [%d]", length);
815 gboolean _add_vsie(int frame_id, const char* vsie)
817 GVariant *params = NULL;
818 GVariant *message = NULL;
819 GVariantBuilder *bytearray_builder = NULL;
824 unsigned char *bytearray = NULL;
825 size_t bytearray_len = 0;
827 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
828 DBG("Invalid parameter, frame-id: %d", frame_id);
832 vsie_len = strlen(vsie);
834 DBG("vsie length is zero");
838 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
840 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
841 if (bytearray == NULL) {
842 DBG("Failed to allocate memory to bytearray");
846 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
847 DBG("invalid vsie string");
852 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
853 for (i = 0; i < bytearray_len; i++)
854 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
856 params = g_variant_new("(iay)", frame_id, bytearray_builder);
857 g_variant_builder_unref(bytearray_builder);
859 if_path = netconfig_wifi_get_supplicant_interface();
861 if (if_path == NULL) {
862 ERR("Fail to get wpa_supplicant DBus path");
867 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
868 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
871 if (message == NULL) {
872 ERR("Failed to send command to wpa_supplicant");
877 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
883 gboolean _get_vsie(int frame_id, char **vsie)
885 GVariant *params = NULL;
886 GVariant *message = NULL;
889 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
890 DBG("Invalid parameter, frame-id: %d", frame_id);
894 if_path = netconfig_wifi_get_supplicant_interface();
895 if (if_path == NULL) {
896 ERR("Fail to get wpa_supplicant DBus path");
900 params = g_variant_new("(i)", frame_id);
902 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
903 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
906 if (message == NULL) {
907 ERR("Failed to send command to wpa_supplicant");
910 GVariantIter *iter = NULL;
911 unsigned char *vsie_bytes = NULL;
915 g_variant_get(message, "(ay)", &iter);
917 ERR("vsie is not present");
921 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
922 if (vsie_bytes == NULL) {
923 ERR("vsie_bytes not allocated");
927 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
930 ERR("vsie not allocated.");
937 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
943 gboolean _remove_vsie(int frame_id, const char *vsie)
945 GVariant *params = NULL;
946 GVariant *message = NULL;
947 GVariantBuilder *bytearray_builder = NULL;
952 unsigned char *bytearray = NULL;
953 size_t bytearray_len = 0;
955 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
956 DBG("Invalid parameter, frame-id: %d", frame_id);
960 vsie_len = strlen(vsie);
962 DBG("vsie length is zero");
966 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
968 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
969 if (bytearray == NULL) {
970 DBG("Failed to allocate memory to bytearray");
974 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
975 DBG("invalid vsie string");
980 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
981 for (i = 0; i < bytearray_len; i++)
982 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
984 params = g_variant_new("(iay)", frame_id, bytearray_builder);
985 g_variant_builder_unref(bytearray_builder);
987 if_path = netconfig_wifi_get_supplicant_interface();
988 if (if_path == NULL) {
989 ERR("Fail to get wpa_supplicant DBus path");
994 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
995 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
998 if (message == NULL) {
999 ERR("Failed to send command to wpa_supplicant");
1004 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1011 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
1014 GSList *config_ids = NULL;
1016 gchar **result = NULL;
1018 g_return_val_if_fail(wifi != NULL, TRUE);
1020 config_ids = _get_list();
1021 if (config_ids == NULL) {
1022 ERR("Fail to get config list");
1023 netconfig_error_no_profile(context);
1027 length = g_slist_length(config_ids);
1028 result = g_new0(gchar *, length + 1);
1029 for (i = 0; i < length; i++) {
1030 gchar *config_id = g_slist_nth_data(config_ids, i);
1031 result[i] = g_strdup(config_id);
1034 config_ids = g_slist_nth(config_ids, 0);
1035 g_slist_free_full(config_ids, g_free);
1037 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1039 for (i = 0; i < length; i++)
1049 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1050 const gchar *config_id)
1052 gboolean ret = FALSE;
1053 GVariantBuilder *b = NULL;
1054 struct wifi_config *conf = NULL;
1056 g_return_val_if_fail(wifi != NULL, TRUE);
1058 conf = g_new0(struct wifi_config, 1);
1059 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1061 ret = _load_configuration(config_id, conf);
1063 g_free(conf->ip_info);
1065 ERR("Fail to _load_configuration");
1066 netconfig_error_no_profile(context);
1070 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1071 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1072 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1073 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1075 if (conf->proxy_address != NULL)
1076 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1078 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1080 if (conf->ip_info->ip_type != NULL)
1081 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1083 if (conf->ip_info->ip_address != NULL)
1084 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1086 if (conf->ip_info->subnet_mask != NULL)
1087 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1089 if (conf->ip_info->prefix_length > 0)
1090 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV6_PREFIX_LEN, g_variant_new_int32(conf->ip_info->prefix_length));
1092 if (conf->ip_info->gateway_address != NULL)
1093 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1095 if (conf->ip_info->dns_type != NULL)
1096 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1098 int i = 0, count = conf->ip_info->dns_count;
1100 if (conf->ip_info->dns_address[i] != NULL)
1101 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1106 if (conf->last_error != NULL)
1107 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1109 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1111 __free_wifi_configuration(conf);
1113 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1114 g_variant_builder_unref(b);
1118 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1119 const char *netmask)
1128 mask = inet_network(netmask);
1131 /* a valid netmask must be 2^n - 1 */
1132 if ((host & (host + 1)) != 0)
1136 for (; mask; mask <<= 1)
1142 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1143 const gchar *config_id, GVariant *configuration)
1145 gboolean ret = FALSE;
1146 struct wifi_config *conf = NULL;
1147 GKeyFile *keyfile = NULL;
1151 gchar *group_name = NULL;
1154 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1155 ERR("Invalid parameter");
1156 netconfig_error_invalid_parameter(context);
1160 conf = g_new0(struct wifi_config, 1);
1161 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1163 g_variant_get(configuration, "a{sv}", &iter);
1164 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1165 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1166 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1167 conf->name = g_strdup(g_variant_get_string(value, NULL));
1168 DBG("name [%s]", conf->name);
1172 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1173 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1174 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1175 DBG("ssid [%s]", conf->ssid);
1179 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1180 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1181 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1182 DBG("passphrase []");
1184 conf->passphrase = NULL;
1186 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1187 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1188 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1189 DBG("is_hidden [%s]", conf->is_hidden);
1191 conf->is_hidden = NULL;
1193 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1194 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1195 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1196 DBG("IP config type [%s]", conf->ip_info->ip_type);
1198 conf->ip_info->ip_type = NULL;
1200 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1201 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1202 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1203 DBG("IP address [%s]", conf->ip_info->ip_address);
1205 conf->ip_info->ip_address = NULL;
1207 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1208 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1209 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1210 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1212 conf->ip_info->subnet_mask = NULL;
1214 } else if (g_strcmp0(field, WIFI_CONFIG_IPV6_PREFIX_LEN) == 0) {
1215 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1216 conf->ip_info->prefix_length = g_variant_get_int32(value);
1217 DBG("IPv6 Prefix Length [%d]", conf->ip_info->prefix_length);
1219 conf->ip_info->prefix_length = 0;
1221 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1222 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1223 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1224 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1226 conf->ip_info->gateway_address = NULL;
1228 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1229 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1230 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1231 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1233 conf->ip_info->dns_type = NULL;
1235 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1236 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1237 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1238 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1239 conf->ip_info->dns_count = order + 1;
1242 conf->ip_info->dns_address[order++] = NULL;
1244 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1245 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1246 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1247 DBG("proxy_address [%s]", conf->proxy_address);
1249 conf->proxy_address = NULL;
1253 conf->favorite = TRUE;
1254 conf->autoconnect = TRUE;
1256 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1258 __free_wifi_configuration(conf);
1259 ERR("Fail to get_wifi_config_group_name");
1260 netconfig_error_fail_save_congifuration(context);
1264 keyfile = g_key_file_new();
1265 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1266 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1268 if (conf->passphrase != NULL) {
1269 gchar *enc_data = NULL;
1270 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1273 ERR("Failed to encrypt the passphrase");
1275 g_free(conf->passphrase);
1276 conf->passphrase = enc_data;
1279 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1282 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1283 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1285 /* Optional field */
1286 if (conf->proxy_address != NULL) {
1287 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1288 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1291 if (conf->is_hidden != NULL) {
1292 gboolean hidden = FALSE;
1293 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1295 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1298 if (conf->ip_info->ip_type != NULL)
1299 g_key_file_set_string(keyfile, group_name,
1300 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1302 if (conf->ip_info->ip_address != NULL)
1303 g_key_file_set_string(keyfile, group_name,
1304 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1306 if (conf->ip_info->subnet_mask != NULL) {
1307 unsigned char prefix_len;
1308 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1309 conf->ip_info->subnet_mask);
1310 g_key_file_set_integer(keyfile, group_name,
1311 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1314 if (conf->ip_info->prefix_length > 0)
1315 g_key_file_set_integer(keyfile, group_name,
1316 WIFI_CONFIG_IPV6_PREFIX_LEN, conf->ip_info->prefix_length);
1318 if (conf->ip_info->gateway_address != NULL)
1319 g_key_file_set_string(keyfile, group_name,
1320 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1322 if (conf->ip_info->dns_type != NULL)
1323 g_key_file_set_string(keyfile, group_name,
1324 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1326 int i = 0, count = conf->ip_info->dns_count;
1328 if (conf->ip_info->dns_address[i] != NULL)
1329 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1330 conf->ip_info->dns_address[i]);
1335 ret = _save_configuration(config_id, keyfile);
1337 INFO("Success to save configuration [%s]", config_id);
1338 wifi_complete_save_configuration(wifi, context);
1340 INFO("Fail to save configuration [%s]", config_id);
1341 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1344 g_key_file_free(keyfile);
1346 __free_wifi_configuration(conf);
1348 g_variant_iter_free(iter);
1353 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1354 const gchar *config_id)
1356 gboolean ret = FALSE;
1357 GVariantBuilder *b = NULL;
1358 struct wifi_config *conf = NULL;
1360 g_return_val_if_fail(wifi != NULL, TRUE);
1362 conf = g_new0(struct wifi_config, 1);
1363 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1364 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1366 ret = _load_configuration(config_id, conf);
1368 g_free(conf->eap_config);
1369 g_free(conf->ip_info);
1371 ERR("Fail to _load_configuration");
1372 netconfig_error_no_profile(context);
1376 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1377 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1378 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1379 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1380 if (conf->proxy_address != NULL)
1381 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1383 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1385 if (conf->last_error != NULL)
1386 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1388 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1390 if (conf->eap_config != NULL) {
1391 if (conf->eap_config->anonymous_identity != NULL)
1392 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1394 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1396 if (conf->eap_config->ca_cert != NULL)
1397 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1399 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1401 if (conf->eap_config->client_cert != NULL)
1402 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1404 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1406 if (conf->eap_config->private_key != NULL)
1407 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1409 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1411 if (conf->eap_config->private_key_password != NULL)
1412 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1414 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1416 if (conf->eap_config->identity != NULL)
1417 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1419 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1421 if (conf->eap_config->eap_type != NULL)
1422 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1424 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1426 if (conf->eap_config->eap_auth_type != NULL)
1427 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1429 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1431 if (conf->eap_config->subject_match != NULL)
1432 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1434 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1437 __free_wifi_configuration(conf);
1439 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1440 g_variant_builder_unref(b);
1444 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1445 const gchar *config_id, GVariant *configuration)
1447 gboolean ret = FALSE;
1448 struct wifi_config *conf = NULL;
1449 GKeyFile *keyfile = NULL;
1453 gchar *group_name = NULL;
1455 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1456 ERR("Invalid parameter");
1457 netconfig_error_invalid_parameter(context);
1461 conf = g_new0(struct wifi_config, 1);
1462 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1464 g_variant_get(configuration, "a{sv}", &iter);
1465 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1466 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1467 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1468 conf->name = g_strdup(g_variant_get_string(value, NULL));
1469 DBG("name [%s]", conf->name);
1473 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1474 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1475 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1476 DBG("ssid [%s]", conf->ssid);
1480 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1481 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1482 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1483 DBG("passphrase [%s]", conf->passphrase);
1485 conf->passphrase = NULL;
1487 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1488 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1489 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1490 DBG("is_hidden [%s]", conf->is_hidden);
1492 conf->is_hidden = NULL;
1494 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1495 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1496 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1497 DBG("proxy_address [%s]", conf->proxy_address);
1499 conf->proxy_address = NULL;
1501 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1502 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1503 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1504 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1506 conf->eap_config->anonymous_identity = NULL;
1508 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1509 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1510 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1511 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1513 conf->eap_config->ca_cert = NULL;
1515 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1516 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1517 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1518 DBG("client_cert [%s]", conf->eap_config->client_cert);
1520 conf->eap_config->client_cert = NULL;
1522 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1523 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1524 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1525 DBG("private_key [%s]", conf->eap_config->private_key);
1527 conf->eap_config->private_key = NULL;
1529 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1530 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1531 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1532 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1534 conf->eap_config->private_key_password = NULL;
1536 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1537 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1538 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1539 DBG("identity [%s]", conf->eap_config->identity);
1541 conf->eap_config->identity = NULL;
1543 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1544 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1545 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1546 DBG("eap_type [%s]", conf->eap_config->eap_type);
1548 conf->eap_config->eap_type = NULL;
1550 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1551 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1552 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1553 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1555 conf->eap_config->eap_auth_type = NULL;
1557 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1558 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1559 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1560 DBG("subject_match [%s]", conf->eap_config->subject_match);
1562 conf->eap_config->subject_match = NULL;
1566 conf->favorite = TRUE;
1567 conf->autoconnect = TRUE;
1569 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1571 __free_wifi_configuration(conf);
1572 ERR("Fail to get_wifi_config_group_name");
1576 keyfile = g_key_file_new();
1577 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1578 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1580 if (conf->passphrase != NULL) {
1581 gchar *enc_data = NULL;
1582 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1585 ERR("Failed to encrypt the passphrase");
1587 g_free(conf->passphrase);
1588 conf->passphrase = enc_data;
1590 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1593 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1594 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1596 /* Optional field */
1597 if (conf->proxy_address != NULL) {
1598 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1599 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1602 if (conf->is_hidden != NULL) {
1603 gboolean hidden = FALSE;
1604 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1606 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1609 if (conf->eap_config->anonymous_identity != NULL)
1610 g_key_file_set_string(keyfile, group_name,
1611 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1613 if (conf->eap_config->ca_cert != NULL)
1614 g_key_file_set_string(keyfile, group_name,
1615 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1617 if (conf->eap_config->client_cert != NULL)
1618 g_key_file_set_string(keyfile, group_name,
1619 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1621 if (conf->eap_config->private_key != NULL)
1622 g_key_file_set_string(keyfile, group_name,
1623 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1625 if (conf->eap_config->private_key_password != NULL)
1626 g_key_file_set_string(keyfile, group_name,
1627 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1629 if (conf->eap_config->identity != NULL)
1630 g_key_file_set_string(keyfile, group_name,
1631 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1633 if (conf->eap_config->eap_type != NULL)
1634 g_key_file_set_string(keyfile, group_name,
1635 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1637 if (conf->eap_config->eap_auth_type != NULL)
1638 g_key_file_set_string(keyfile, group_name,
1639 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1641 if (conf->eap_config->subject_match != NULL)
1642 g_key_file_set_string(keyfile, group_name,
1643 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1645 ret = _save_configuration(config_id, keyfile);
1647 INFO("Success to save eap configuration [%s]", config_id);
1648 wifi_complete_save_eap_configuration(wifi, context);
1650 INFO("Fail to save eap configuration [%s]", config_id);
1651 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1654 g_key_file_free(keyfile);
1656 __free_wifi_configuration(conf);
1658 g_variant_iter_free(iter);
1663 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1665 gboolean ret = FALSE;
1667 if ((wifi == NULL) || (config_id == NULL)) {
1668 ERR("Invalid parameter");
1669 netconfig_error_invalid_parameter(context);
1673 ret = _remove_configuration(config_id);
1675 /* no configuration or error */
1676 ERR("No [%s] configuration", config_id);
1677 netconfig_error_no_profile(context);
1681 wifi_complete_remove_configuration(wifi, context);
1685 /* config field key / value */
1687 * [wifi_macaddress_config_id]
1688 * Name=name (mandatory)
1689 * SSID=SSID (mandatory)
1690 * Frequency=2462 (X)
1692 * AutoConnect=true (Default true)
1693 * Modified=2015-03-20 (X)
1694 * IPv4.method=manual (O)
1695 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1696 * IPv6.method=auto (X)
1697 * IPv6.privacy=disabled (X)
1698 * IPv4.netmask_prefixlen=24 (X)
1699 * IPv4.local_address=192.0.0.1 (O)
1700 * IPv4.gateway=192.0.0.1 (O ? X ?)
1701 * Nameservers=192.168.43.22; (O)
1702 * Proxy.Method=manual (O)
1703 * Proxy.Servers=trst.com:8888; (O)
1705 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1706 const gchar *config_id, const gchar *key, const gchar *value)
1708 gboolean ret = FALSE;
1709 gchar *keyfile_key = NULL;
1711 g_return_val_if_fail(wifi != NULL, TRUE);
1712 g_return_val_if_fail(config_id != NULL, TRUE);
1713 g_return_val_if_fail(key != NULL, TRUE);
1715 DBG("Key[%s] Value[%d]", key, value);
1717 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1718 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1720 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1721 netconfig_error_invalid_parameter(context);
1724 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1725 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1726 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1727 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1728 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1729 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1730 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1731 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1732 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1733 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1734 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1735 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1736 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1737 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1738 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1739 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1740 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1741 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1742 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1744 ERR("Not supported key[%s]", key);
1745 netconfig_error_invalid_parameter(context);
1749 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1751 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1754 if (keyfile_key != NULL)
1755 g_free(keyfile_key);
1757 wifi_complete_set_config_field(wifi, context);
1761 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1763 gboolean ret = FALSE;
1764 gchar *passphrase = NULL;
1766 if ((wifi == NULL) || (config_id == NULL)) {
1767 ERR("Invalid parameter");
1768 netconfig_error_invalid_parameter(context);
1772 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1774 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1775 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1779 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1785 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1786 int frame_id, const gchar *vsie)
1788 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1790 g_return_val_if_fail(wifi != NULL, TRUE);
1791 g_return_val_if_fail(vsie != NULL, TRUE);
1793 gboolean ret = FALSE;
1795 ret = _add_vsie(frame_id, vsie);
1797 DBG("Failed to add vsie: %s", vsie);
1798 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1802 wifi_complete_add_vsie(wifi, context);
1806 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1809 DBG("Frame ID: [%d]", frame_id);
1811 g_return_val_if_fail(wifi != NULL, TRUE);
1813 gboolean ret = FALSE;
1816 ret = _get_vsie(frame_id, &vsie);
1818 DBG("Failed to get vsie for frame:[%d]", frame_id);
1819 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1823 DBG("Received vsie: %s", vsie);
1824 wifi_complete_get_vsie(wifi, context, vsie);
1829 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1830 int frame_id, const gchar *vsie)
1832 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1834 g_return_val_if_fail(wifi != NULL, TRUE);
1835 g_return_val_if_fail(vsie != NULL, TRUE);
1837 gboolean ret = FALSE;
1839 ret = _remove_vsie(frame_id, vsie);
1841 DBG("Failed to remove vsie: %s", vsie);
1842 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1846 wifi_complete_remove_vsie(wifi, context);