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);
194 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
196 gchar *mac_address = NULL;
197 gchar *g_name = NULL;
198 gboolean ret = FALSE;
200 ret = __get_mac_address(&mac_address);
201 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
202 ERR("Cannot get WIFI MAC address");
207 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
208 if (g_name == NULL) {
213 *group_name = g_strdup(g_name);
221 static gboolean __get_security_type(const gchar *config_id, gchar **type)
223 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
224 *type = g_strdup(WIFI_SECURITY_NONE);
225 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
226 *type = g_strdup(WIFI_SECURITY_WEP);
227 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
228 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
229 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
230 *type = g_strdup(WIFI_SECURITY_EAP);
239 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
241 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
242 if (*config_id == NULL) {
251 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
253 GKeyFile *keyfile = NULL;
256 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
258 keyfile = netconfig_keyfile_load(path);
260 ERR("keyfile[%s] is NULL", path);
267 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
269 gboolean ret = FALSE;
272 path = g_strdup_printf("%s/%s", pathname, filename);
273 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
275 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
284 static gboolean __remove_configuration(const gchar *pathname)
288 if (__remove_file(pathname, "settings") != TRUE) {
289 ERR("Cannot remove [%s/settings]", pathname);
292 if (__remove_file(pathname, "data") != TRUE) {
293 ERR("Cannot remove [%s/data]", pathname);
297 ret = rmdir(pathname);
299 ERR("Cannot remove [%s]", pathname);
306 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
310 gboolean hidden = FALSE;
311 gboolean ret = FALSE;
313 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
315 ERR("Fail to get_wifi_config_group_name");
319 keyfile = __get_configuration_keyfile(group_name);
320 if (keyfile == NULL) {
321 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
326 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
327 DBG("name [%s]", config->name);
329 __get_security_type(config_id, &config->security_type);
330 if (config->security_type == NULL) {
331 ERR("Fail to _get_security_type");
332 g_key_file_free(keyfile);
336 DBG("security_type [%s]", config->security_type);
338 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
339 if (config->proxy_address)
340 DBG("proxy_address [%s]", config->proxy_address);
342 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
344 config->is_hidden = g_strdup("TRUE");
346 config->is_hidden = g_strdup("FALSE");
347 DBG("is_hidden [%s]", config->is_hidden);
349 if (config->ip_info) {
350 GError *error = NULL;
351 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
352 WIFI_CONFIG_IPV4_METHOD, NULL);
353 if (config->ip_info->ip_type)
354 DBG("IPv4.Method:%s", config->ip_info->ip_type);
356 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
357 WIFI_CONFIG_IPV4_ADDRESS, NULL);
358 if (config->ip_info->ip_address)
359 DBG("IPv4.Address:%s", config->ip_info->ip_address);
363 struct in_addr netmask;
365 prefix_len = g_key_file_get_integer(keyfile, group_name,
366 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
368 DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
371 addr = 0xffffffff << (32 - prefix_len);
372 netmask.s_addr = htonl(addr);
373 mask = inet_ntoa(netmask);
374 config->ip_info->subnet_mask = g_strdup(mask);
375 if (config->ip_info->subnet_mask)
376 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
379 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
380 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
381 if (config->ip_info->gateway_address)
382 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
384 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
385 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
386 if (config->ip_info->dns_type)
387 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
391 nameservers = g_key_file_get_string_list(keyfile, group_name,
392 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
395 config->ip_info->dns_count = length;
397 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
398 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
399 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
403 g_strfreev(nameservers);
408 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
409 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
410 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
411 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
412 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
413 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
414 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
415 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
416 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
417 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
419 if (config->eap_config->anonymous_identity)
420 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
421 if (config->eap_config->ca_cert)
422 DBG("ca_cert [%s]", config->eap_config->ca_cert);
423 if (config->eap_config->client_cert)
424 DBG("client_cert [%s]", config->eap_config->client_cert);
425 if (config->eap_config->private_key)
426 DBG("private_key [%s]", config->eap_config->private_key);
427 if (config->eap_config->private_key_password)
428 DBG("private_key_password [%s]", config->eap_config->private_key_password);
429 if (config->eap_config->identity)
430 DBG("identity [%s]", config->eap_config->identity);
431 if (config->eap_config->eap_type)
432 DBG("eap_type [%s]", config->eap_config->eap_type);
433 if (config->eap_config->eap_auth_type)
434 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
435 if (config->eap_config->subject_match)
436 DBG("subject_match [%s]", config->eap_config->subject_match);
439 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
440 if (config->last_error)
441 DBG("last_error [%s]", config->last_error);
443 g_key_file_free(keyfile);
449 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
454 gboolean ret = FALSE;
456 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
458 ERR("Fail to get_wifi_config_group_name");
462 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
463 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
464 if (__remove_configuration(dir) != TRUE) {
465 ERR("[%s] is existed, but cannot remove", dir);
472 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
473 ERR("Cannot mkdir %s", dir);
479 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
480 netconfig_keyfile_save(keyfile, path);
488 static gboolean _remove_configuration(const gchar *config_id)
490 gboolean ret = FALSE;
494 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
496 ERR("Fail to get_wifi_config_group_name");
500 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
501 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
502 if (__remove_configuration(dir) != TRUE) {
503 ERR("[%s] is existed, but cannot remove", dir);
506 INFO("Success to remove [%s]", dir);
509 ERR("[%s] is not existed", dir);
520 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
526 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
528 ERR("Fail to get_wifi_config_group_name");
531 DBG("group_name %s", group_name);
533 keyfile = __get_configuration_keyfile(group_name);
534 if (keyfile == NULL) {
535 ERR("Fail to __get_configuration_keyfile");
540 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
541 g_key_file_set_string(keyfile, group_name, key, value);
542 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
543 g_key_file_set_string(keyfile, group_name, key, value);
544 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
545 gboolean hidden = FALSE;
546 if (g_strcmp0(value, "TRUE") == 0)
548 g_key_file_set_boolean(keyfile, group_name, key, hidden);
549 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
550 g_key_file_set_string(keyfile, group_name, key, value);
551 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
552 g_key_file_set_string(keyfile, group_name, key, value);
553 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
554 g_key_file_set_string(keyfile, group_name, key, value);
555 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
556 g_key_file_set_string(keyfile, group_name, key, value);
557 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
558 g_key_file_set_string(keyfile, group_name, key, value);
559 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
560 g_key_file_set_string(keyfile, group_name, key, value);
561 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
562 g_key_file_set_string(keyfile, group_name, key, value);
563 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
564 g_key_file_set_string(keyfile, group_name, key, value);
566 ERR("key[%s] is not supported", key);
570 _save_configuration(config_id, keyfile);
572 g_key_file_free(keyfile);
578 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
583 gboolean hidden = FALSE;
584 gboolean ret = FALSE;
586 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
588 ERR("Fail to get_wifi_config_group_name");
591 DBG("group_name %s", group_name);
593 keyfile = __get_configuration_keyfile(group_name);
594 if (keyfile == NULL) {
595 ERR("Fail to __get_configuration_keyfile");
600 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
601 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
602 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
603 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
604 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
605 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
606 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
607 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
609 val = g_strdup("TRUE");
611 val = g_strdup("FALSE");
612 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
613 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
614 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
615 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
616 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
617 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
618 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
619 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
620 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
621 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
622 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
623 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
624 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
625 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
626 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
627 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
628 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
629 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
631 ERR("Invalid key[%s]", key);
632 val = g_strdup("NOTSUPPORTED");
635 *value = g_strdup(val);
638 g_key_file_free(keyfile);
644 static GSList *_get_list(void)
647 struct dirent *dp = NULL;
650 dir = opendir(CONNMAN_STORAGE);
652 ERR("Cannot open dir %s", CONNMAN_STORAGE);
656 while ((dp = readdir(dir)) != NULL) {
657 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
658 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
661 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
662 list = g_slist_append(list, g_strdup(config_id));
670 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
672 gboolean ret = FALSE;
675 if ((service_profile == NULL) || (config_id == NULL)) {
676 ERR("Invalid parameter");
680 ret = __get_config_id(service_profile, &val);
681 *config_id = g_strdup(val);
687 gboolean wifi_config_remove_configuration(const gchar *config_id)
689 gboolean ret = FALSE;
691 ret = _remove_configuration(config_id);
696 int __netconfig_hex_char_to_num(char c)
698 if (c >= '0' && c <= '9')
701 if (c >= 'a' && c <= 'f')
704 if (c >= 'A' && c <= 'F')
710 int __netconfig_hex_to_byte(const char *hex)
714 a = __netconfig_hex_char_to_num(*hex++);
718 b = __netconfig_hex_char_to_num(*hex++);
725 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
729 const char *ipos = hex;
730 unsigned char *opos = buf;
732 for (i = 0; i < len; i++) {
733 a = __netconfig_hex_to_byte(ipos);
744 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
751 ERR("Invalid parameter.");
755 *dst = (char *) g_try_malloc0((2*src_len)+1);
757 ERR("failed to allocate memory to buffer.");
763 for (i = 0; i < src_len; i++) {
764 snprintf(buf, 3, "%02x", src[i]);
772 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
774 GVariantIter *iter_copy = NULL;
777 unsigned char *tmp_dst = NULL;
779 if (!dst || *dst || !iter) {
780 ERR("Invalid parameter");
784 iter_copy = g_variant_iter_copy(iter);
786 while (g_variant_iter_loop(iter, "y", &tmp))
788 g_variant_iter_free(iter);
790 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
792 ERR("failed to allocate memory");
793 g_variant_iter_free(iter_copy);
798 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
800 g_variant_iter_free(iter_copy);
806 tmp_dst[length] = '\0';
810 DBG("Length [%d]", length);
814 gboolean _add_vsie(int frame_id, const char* vsie)
816 GVariant *params = NULL;
817 GVariant *message = NULL;
818 GVariantBuilder *bytearray_builder = NULL;
823 unsigned char *bytearray = NULL;
824 size_t bytearray_len = 0;
826 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
827 DBG("Invalid parameter, frame-id: %d", frame_id);
831 vsie_len = strlen(vsie);
833 DBG("vsie length is zero");
837 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
839 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
840 if (bytearray == NULL) {
841 DBG("Failed to allocate memory to bytearray");
845 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
846 DBG("invalid vsie string");
851 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
852 for (i = 0; i < bytearray_len; i++)
853 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
855 params = g_variant_new("(iay)", frame_id, bytearray_builder);
856 g_variant_builder_unref(bytearray_builder);
858 if_path = netconfig_wifi_get_supplicant_interface();
860 if (if_path == NULL) {
861 ERR("Fail to get wpa_supplicant DBus path");
866 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
867 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
870 if (message == NULL) {
871 ERR("Failed to send command to wpa_supplicant");
876 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
882 gboolean _get_vsie(int frame_id, char **vsie)
884 GVariant *params = NULL;
885 GVariant *message = NULL;
888 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
889 DBG("Invalid parameter, frame-id: %d", frame_id);
893 if_path = netconfig_wifi_get_supplicant_interface();
894 if (if_path == NULL) {
895 ERR("Fail to get wpa_supplicant DBus path");
899 params = g_variant_new("(i)", frame_id);
901 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
902 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
905 if (message == NULL) {
906 ERR("Failed to send command to wpa_supplicant");
909 GVariantIter *iter = NULL;
910 unsigned char *vsie_bytes = NULL;
914 g_variant_get(message, "(ay)", &iter);
916 ERR("vsie is not present");
920 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
921 if (vsie_bytes == NULL) {
922 ERR("vsie_bytes not allocated");
926 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
929 ERR("vsie not allocated.");
936 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
942 gboolean _remove_vsie(int frame_id, const char *vsie)
944 GVariant *params = NULL;
945 GVariant *message = NULL;
946 GVariantBuilder *bytearray_builder = NULL;
951 unsigned char *bytearray = NULL;
952 size_t bytearray_len = 0;
954 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
955 DBG("Invalid parameter, frame-id: %d", frame_id);
959 vsie_len = strlen(vsie);
961 DBG("vsie length is zero");
965 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
967 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
968 if (bytearray == NULL) {
969 DBG("Failed to allocate memory to bytearray");
973 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
974 DBG("invalid vsie string");
979 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
980 for (i = 0; i < bytearray_len; i++)
981 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
983 params = g_variant_new("(iay)", frame_id, bytearray_builder);
984 g_variant_builder_unref(bytearray_builder);
986 if_path = netconfig_wifi_get_supplicant_interface();
987 if (if_path == NULL) {
988 ERR("Fail to get wpa_supplicant DBus path");
993 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
994 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
997 if (message == NULL) {
998 ERR("Failed to send command to wpa_supplicant");
1003 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1010 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
1013 GSList *config_ids = NULL;
1015 gchar **result = NULL;
1017 g_return_val_if_fail(wifi != NULL, TRUE);
1019 config_ids = _get_list();
1020 if (config_ids == NULL) {
1021 ERR("Fail to get config list");
1022 netconfig_error_no_profile(context);
1026 length = g_slist_length(config_ids);
1027 result = g_new0(gchar *, length + 1);
1028 for (i = 0; i < length; i++) {
1029 gchar *config_id = g_slist_nth_data(config_ids, i);
1030 result[i] = g_strdup(config_id);
1033 config_ids = g_slist_nth(config_ids, 0);
1034 g_slist_free_full(config_ids, g_free);
1036 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1038 for (i = 0; i < length; i++)
1048 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1049 const gchar *config_id)
1051 gboolean ret = FALSE;
1052 GVariantBuilder *b = NULL;
1053 struct wifi_config *conf = NULL;
1055 g_return_val_if_fail(wifi != NULL, TRUE);
1057 conf = g_new0(struct wifi_config, 1);
1058 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1060 ret = _load_configuration(config_id, conf);
1062 g_free(conf->ip_info);
1064 ERR("Fail to _load_configuration");
1065 netconfig_error_no_profile(context);
1069 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1070 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1071 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1072 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1074 if (conf->proxy_address != NULL)
1075 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1077 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1079 if (conf->ip_info->ip_type != NULL)
1080 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1082 if (conf->ip_info->ip_address != NULL)
1083 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1085 if (conf->ip_info->subnet_mask != NULL)
1086 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1088 if (conf->ip_info->gateway_address != NULL)
1089 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1091 if (conf->ip_info->dns_type != NULL)
1092 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1094 int i = 0, count = conf->ip_info->dns_count;
1096 if (conf->ip_info->dns_address[i] != NULL)
1097 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1102 if (conf->last_error != NULL)
1103 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1105 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1107 __free_wifi_configuration(conf);
1109 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1110 g_variant_builder_unref(b);
1114 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1115 const char *netmask)
1124 mask = inet_network(netmask);
1127 /* a valid netmask must be 2^n - 1 */
1128 if ((host & (host + 1)) != 0)
1132 for (; mask; mask <<= 1)
1138 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1139 const gchar *config_id, GVariant *configuration)
1141 gboolean ret = FALSE;
1142 struct wifi_config *conf = NULL;
1143 GKeyFile *keyfile = NULL;
1147 gchar *group_name = NULL;
1150 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1151 ERR("Invalid parameter");
1152 netconfig_error_invalid_parameter(context);
1156 conf = g_new0(struct wifi_config, 1);
1157 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1159 g_variant_get(configuration, "a{sv}", &iter);
1160 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1161 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1162 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1163 conf->name = g_strdup(g_variant_get_string(value, NULL));
1164 DBG("name [%s]", conf->name);
1168 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1169 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1170 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1171 DBG("ssid [%s]", conf->ssid);
1175 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1176 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1177 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1178 DBG("passphrase []");
1180 conf->passphrase = NULL;
1182 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1183 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1184 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1185 DBG("is_hidden [%s]", conf->is_hidden);
1187 conf->is_hidden = NULL;
1189 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1190 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1191 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1192 DBG("IP config type [%s]", conf->ip_info->ip_type);
1194 conf->ip_info->ip_type = NULL;
1196 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1197 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1198 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1199 DBG("IP address [%s]", conf->ip_info->ip_address);
1201 conf->ip_info->ip_address = NULL;
1203 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1204 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1205 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1206 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1208 conf->ip_info->subnet_mask = NULL;
1210 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1211 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1212 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1213 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1215 conf->ip_info->gateway_address = NULL;
1217 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1218 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1219 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1220 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1222 conf->ip_info->dns_type = NULL;
1224 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1225 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1226 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1227 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1228 conf->ip_info->dns_count = order + 1;
1231 conf->ip_info->dns_address[order++] = NULL;
1233 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1234 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1235 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1236 DBG("proxy_address [%s]", conf->proxy_address);
1238 conf->proxy_address = NULL;
1242 conf->favorite = TRUE;
1243 conf->autoconnect = TRUE;
1245 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1247 __free_wifi_configuration(conf);
1248 ERR("Fail to get_wifi_config_group_name");
1249 netconfig_error_fail_save_congifuration(context);
1253 keyfile = g_key_file_new();
1254 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1255 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1257 if (conf->passphrase != NULL) {
1258 gchar *enc_data = NULL;
1259 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1262 ERR("Failed to encrypt the passphrase");
1264 g_free(conf->passphrase);
1265 conf->passphrase = enc_data;
1268 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1271 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1272 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1274 /* Optional field */
1275 if (conf->proxy_address != NULL) {
1276 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1277 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1280 if (conf->is_hidden != NULL) {
1281 gboolean hidden = FALSE;
1282 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1284 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1287 if (conf->ip_info->ip_type != NULL)
1288 g_key_file_set_string(keyfile, group_name,
1289 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1291 if (conf->ip_info->ip_address != NULL)
1292 g_key_file_set_string(keyfile, group_name,
1293 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1295 if (conf->ip_info->subnet_mask != NULL) {
1296 unsigned char prefix_len;
1297 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1298 conf->ip_info->subnet_mask);
1299 g_key_file_set_integer(keyfile, group_name,
1300 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1303 if (conf->ip_info->gateway_address != NULL)
1304 g_key_file_set_string(keyfile, group_name,
1305 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1307 if (conf->ip_info->dns_type != NULL)
1308 g_key_file_set_string(keyfile, group_name,
1309 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1311 int i = 0, count = conf->ip_info->dns_count;
1313 if (conf->ip_info->dns_address[i] != NULL)
1314 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1315 conf->ip_info->dns_address[i]);
1320 ret = _save_configuration(config_id, keyfile);
1322 INFO("Success to save configuration [%s]", config_id);
1323 wifi_complete_save_configuration(wifi, context);
1325 INFO("Fail to save configuration [%s]", config_id);
1326 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1329 g_key_file_free(keyfile);
1331 __free_wifi_configuration(conf);
1333 g_variant_iter_free(iter);
1338 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1339 const gchar *config_id)
1341 gboolean ret = FALSE;
1342 GVariantBuilder *b = NULL;
1343 struct wifi_config *conf = NULL;
1345 g_return_val_if_fail(wifi != NULL, TRUE);
1347 conf = g_new0(struct wifi_config, 1);
1348 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1349 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1351 ret = _load_configuration(config_id, conf);
1353 g_free(conf->eap_config);
1354 g_free(conf->ip_info);
1356 ERR("Fail to _load_configuration");
1357 netconfig_error_no_profile(context);
1361 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1362 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1363 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1364 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1365 if (conf->proxy_address != NULL)
1366 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1368 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1370 if (conf->last_error != NULL)
1371 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1373 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1375 if (conf->eap_config != NULL) {
1376 if (conf->eap_config->anonymous_identity != NULL)
1377 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1379 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1381 if (conf->eap_config->ca_cert != NULL)
1382 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1384 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1386 if (conf->eap_config->client_cert != NULL)
1387 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1389 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1391 if (conf->eap_config->private_key != NULL)
1392 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1394 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1396 if (conf->eap_config->private_key_password != NULL)
1397 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1399 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1401 if (conf->eap_config->identity != NULL)
1402 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1404 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1406 if (conf->eap_config->eap_type != NULL)
1407 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1409 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1411 if (conf->eap_config->eap_auth_type != NULL)
1412 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1414 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1416 if (conf->eap_config->subject_match != NULL)
1417 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1419 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1422 __free_wifi_configuration(conf);
1424 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1425 g_variant_builder_unref(b);
1429 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1430 const gchar *config_id, GVariant *configuration)
1432 gboolean ret = FALSE;
1433 struct wifi_config *conf = NULL;
1434 GKeyFile *keyfile = NULL;
1438 gchar *group_name = NULL;
1440 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1441 ERR("Invalid parameter");
1442 netconfig_error_invalid_parameter(context);
1446 conf = g_new0(struct wifi_config, 1);
1447 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1449 g_variant_get(configuration, "a{sv}", &iter);
1450 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1451 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1452 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1453 conf->name = g_strdup(g_variant_get_string(value, NULL));
1454 DBG("name [%s]", conf->name);
1458 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1459 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1460 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1461 DBG("ssid [%s]", conf->ssid);
1465 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1466 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1467 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1468 DBG("passphrase [%s]", conf->passphrase);
1470 conf->passphrase = NULL;
1472 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1473 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1474 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1475 DBG("is_hidden [%s]", conf->is_hidden);
1477 conf->is_hidden = NULL;
1479 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1480 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1481 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1482 DBG("proxy_address [%s]", conf->proxy_address);
1484 conf->proxy_address = NULL;
1486 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1487 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1488 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1489 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1491 conf->eap_config->anonymous_identity = NULL;
1493 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1494 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1495 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1496 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1498 conf->eap_config->ca_cert = NULL;
1500 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1501 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1502 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1503 DBG("client_cert [%s]", conf->eap_config->client_cert);
1505 conf->eap_config->client_cert = NULL;
1507 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1508 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1509 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1510 DBG("private_key [%s]", conf->eap_config->private_key);
1512 conf->eap_config->private_key = NULL;
1514 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1515 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1516 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1517 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1519 conf->eap_config->private_key_password = NULL;
1521 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1522 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1523 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1524 DBG("identity [%s]", conf->eap_config->identity);
1526 conf->eap_config->identity = NULL;
1528 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1529 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1530 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1531 DBG("eap_type [%s]", conf->eap_config->eap_type);
1533 conf->eap_config->eap_type = NULL;
1535 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1536 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1537 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1538 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1540 conf->eap_config->eap_auth_type = NULL;
1542 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1543 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1544 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1545 DBG("subject_match [%s]", conf->eap_config->subject_match);
1547 conf->eap_config->subject_match = NULL;
1551 conf->favorite = TRUE;
1552 conf->autoconnect = TRUE;
1554 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1556 __free_wifi_configuration(conf);
1557 ERR("Fail to get_wifi_config_group_name");
1561 keyfile = g_key_file_new();
1562 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1563 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1565 if (conf->passphrase != NULL) {
1566 gchar *enc_data = NULL;
1567 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1570 ERR("Failed to encrypt the passphrase");
1572 g_free(conf->passphrase);
1573 conf->passphrase = enc_data;
1575 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1578 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1579 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1581 /* Optional field */
1582 if (conf->proxy_address != NULL) {
1583 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1584 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1587 if (conf->is_hidden != NULL) {
1588 gboolean hidden = FALSE;
1589 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1591 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1594 if (conf->eap_config->anonymous_identity != NULL)
1595 g_key_file_set_string(keyfile, group_name,
1596 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1598 if (conf->eap_config->ca_cert != NULL)
1599 g_key_file_set_string(keyfile, group_name,
1600 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1602 if (conf->eap_config->client_cert != NULL)
1603 g_key_file_set_string(keyfile, group_name,
1604 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1606 if (conf->eap_config->private_key != NULL)
1607 g_key_file_set_string(keyfile, group_name,
1608 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1610 if (conf->eap_config->private_key_password != NULL)
1611 g_key_file_set_string(keyfile, group_name,
1612 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1614 if (conf->eap_config->identity != NULL)
1615 g_key_file_set_string(keyfile, group_name,
1616 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1618 if (conf->eap_config->eap_type != NULL)
1619 g_key_file_set_string(keyfile, group_name,
1620 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1622 if (conf->eap_config->eap_auth_type != NULL)
1623 g_key_file_set_string(keyfile, group_name,
1624 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1626 if (conf->eap_config->subject_match != NULL)
1627 g_key_file_set_string(keyfile, group_name,
1628 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1630 ret = _save_configuration(config_id, keyfile);
1632 INFO("Success to save eap configuration [%s]", config_id);
1633 wifi_complete_save_eap_configuration(wifi, context);
1635 INFO("Fail to save eap configuration [%s]", config_id);
1636 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1639 g_key_file_free(keyfile);
1641 __free_wifi_configuration(conf);
1643 g_variant_iter_free(iter);
1648 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1650 gboolean ret = FALSE;
1652 if ((wifi == NULL) || (config_id == NULL)) {
1653 ERR("Invalid parameter");
1654 netconfig_error_invalid_parameter(context);
1658 ret = _remove_configuration(config_id);
1660 /* no configuration or error */
1661 ERR("No [%s] configuration", config_id);
1662 netconfig_error_no_profile(context);
1666 wifi_complete_remove_configuration(wifi, context);
1670 /* config field key / value */
1672 * [wifi_macaddress_config_id]
1673 * Name=name (mandatory)
1674 * SSID=SSID (mandatory)
1675 * Frequency=2462 (X)
1677 * AutoConnect=true (Default true)
1678 * Modified=2015-03-20 (X)
1679 * IPv4.method=manual (O)
1680 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1681 * IPv6.method=auto (X)
1682 * IPv6.privacy=disabled (X)
1683 * IPv4.netmask_prefixlen=24 (X)
1684 * IPv4.local_address=192.0.0.1 (O)
1685 * IPv4.gateway=192.0.0.1 (O ? X ?)
1686 * Nameservers=192.168.43.22; (O)
1687 * Proxy.Method=manual (O)
1688 * Proxy.Servers=trst.com:8888; (O)
1690 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1691 const gchar *config_id, const gchar *key, const gchar *value)
1693 gboolean ret = FALSE;
1694 gchar *keyfile_key = NULL;
1696 g_return_val_if_fail(wifi != NULL, TRUE);
1697 g_return_val_if_fail(config_id != NULL, TRUE);
1698 g_return_val_if_fail(key != NULL, TRUE);
1700 DBG("Key[%s] Value[%d]", key, value);
1702 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1703 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1705 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1706 netconfig_error_invalid_parameter(context);
1709 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1710 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1711 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1712 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1713 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1714 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1715 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1716 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1717 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1718 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1719 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1720 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1721 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1722 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1723 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1724 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1725 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1726 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1727 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1729 ERR("Not supported key[%s]", key);
1730 netconfig_error_invalid_parameter(context);
1734 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1736 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1739 if (keyfile_key != NULL)
1740 g_free(keyfile_key);
1742 wifi_complete_set_config_field(wifi, context);
1746 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1748 gboolean ret = FALSE;
1749 gchar *passphrase = NULL;
1751 if ((wifi == NULL) || (config_id == NULL)) {
1752 ERR("Invalid parameter");
1753 netconfig_error_invalid_parameter(context);
1757 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1759 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1760 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1764 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1770 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1771 int frame_id, const gchar *vsie)
1773 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1775 g_return_val_if_fail(wifi != NULL, TRUE);
1776 g_return_val_if_fail(vsie != NULL, TRUE);
1778 gboolean ret = FALSE;
1780 ret = _add_vsie(frame_id, vsie);
1782 DBG("Failed to add vsie: %s", vsie);
1783 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1787 wifi_complete_add_vsie(wifi, context);
1791 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1794 DBG("Frame ID: [%d]", frame_id);
1796 g_return_val_if_fail(wifi != NULL, TRUE);
1798 gboolean ret = FALSE;
1801 ret = _get_vsie(frame_id, &vsie);
1803 DBG("Failed to get vsie for frame:[%d]", frame_id);
1804 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1808 DBG("Received vsie: %s", vsie);
1809 wifi_complete_get_vsie(wifi, context, vsie);
1814 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1815 int frame_id, const gchar *vsie)
1817 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1819 g_return_val_if_fail(wifi != NULL, TRUE);
1820 g_return_val_if_fail(vsie != NULL, TRUE);
1822 gboolean ret = FALSE;
1824 ret = _remove_vsie(frame_id, vsie);
1826 DBG("Failed to remove vsie: %s", vsie);
1827 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1831 wifi_complete_remove_vsie(wifi, context);