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 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
350 WIFI_CONFIG_IPV4_METHOD, NULL);
351 if (config->ip_info->ip_type)
352 DBG("IPv4.Method:%s", config->ip_info->ip_type);
354 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
355 WIFI_CONFIG_IPV4_ADDRESS, NULL);
356 if (config->ip_info->ip_address)
357 DBG("IPv4.Address:%s", config->ip_info->ip_address);
361 struct in_addr netmask;
363 prefix_len = g_key_file_get_integer(keyfile, group_name,
364 WIFI_CONFIG_IPV4_SUBNET_MASK, NULL);
365 addr = 0xffffffff << (32 - prefix_len);
366 netmask.s_addr = htonl(addr);
367 mask = inet_ntoa(netmask);
368 config->ip_info->subnet_mask = g_strdup(mask);
369 if (config->ip_info->subnet_mask)
370 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
372 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
373 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS,
375 if (config->ip_info->gateway_address)
376 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
378 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
379 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
380 if (config->ip_info->dns_type)
381 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
385 nameservers = g_key_file_get_string_list(keyfile, group_name,
386 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
389 config->ip_info->dns_count = length;
391 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
392 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
393 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
397 g_strfreev(nameservers);
401 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
402 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
403 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
404 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
405 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
406 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
407 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
408 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
409 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
410 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
412 if (config->eap_config->anonymous_identity)
413 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
414 if (config->eap_config->ca_cert)
415 DBG("ca_cert [%s]", config->eap_config->ca_cert);
416 if (config->eap_config->client_cert)
417 DBG("client_cert [%s]", config->eap_config->client_cert);
418 if (config->eap_config->private_key)
419 DBG("private_key [%s]", config->eap_config->private_key);
420 if (config->eap_config->private_key_password)
421 DBG("private_key_password [%s]", config->eap_config->private_key_password);
422 if (config->eap_config->identity)
423 DBG("identity [%s]", config->eap_config->identity);
424 if (config->eap_config->eap_type)
425 DBG("eap_type [%s]", config->eap_config->eap_type);
426 if (config->eap_config->eap_auth_type)
427 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
428 if (config->eap_config->subject_match)
429 DBG("subject_match [%s]", config->eap_config->subject_match);
432 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
433 if (config->last_error)
434 DBG("last_error [%s]", config->last_error);
436 g_key_file_free(keyfile);
442 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
447 gboolean ret = FALSE;
449 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
451 ERR("Fail to get_wifi_config_group_name");
455 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
456 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
457 if (__remove_configuration(dir) != TRUE) {
458 ERR("[%s] is existed, but cannot remove", dir);
465 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
466 ERR("Cannot mkdir %s", dir);
472 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
473 netconfig_keyfile_save(keyfile, path);
481 static gboolean _remove_configuration(const gchar *config_id)
483 gboolean ret = FALSE;
487 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
489 ERR("Fail to get_wifi_config_group_name");
493 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
494 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
495 if (__remove_configuration(dir) != TRUE) {
496 ERR("[%s] is existed, but cannot remove", dir);
499 INFO("Success to remove [%s]", dir);
502 ERR("[%s] is not existed", dir);
513 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
519 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
521 ERR("Fail to get_wifi_config_group_name");
524 DBG("group_name %s", group_name);
526 keyfile = __get_configuration_keyfile(group_name);
527 if (keyfile == NULL) {
528 ERR("Fail to __get_configuration_keyfile");
533 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
534 g_key_file_set_string(keyfile, group_name, key, value);
535 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
536 g_key_file_set_string(keyfile, group_name, key, value);
537 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
538 gboolean hidden = FALSE;
539 if (g_strcmp0(value, "TRUE") == 0)
541 g_key_file_set_boolean(keyfile, group_name, key, hidden);
542 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
543 g_key_file_set_string(keyfile, group_name, key, value);
544 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
545 g_key_file_set_string(keyfile, group_name, key, value);
546 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
547 g_key_file_set_string(keyfile, group_name, key, value);
548 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
549 g_key_file_set_string(keyfile, group_name, key, value);
550 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
551 g_key_file_set_string(keyfile, group_name, key, value);
552 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
553 g_key_file_set_string(keyfile, group_name, key, value);
554 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
555 g_key_file_set_string(keyfile, group_name, key, value);
556 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
557 g_key_file_set_string(keyfile, group_name, key, value);
559 ERR("key[%s] is not supported", key);
563 _save_configuration(config_id, keyfile);
565 g_key_file_free(keyfile);
571 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
576 gboolean hidden = FALSE;
577 gboolean ret = FALSE;
579 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
581 ERR("Fail to get_wifi_config_group_name");
584 DBG("group_name %s", group_name);
586 keyfile = __get_configuration_keyfile(group_name);
587 if (keyfile == NULL) {
588 ERR("Fail to __get_configuration_keyfile");
593 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
594 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
595 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
596 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
597 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
598 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
599 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
600 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
602 val = g_strdup("TRUE");
604 val = g_strdup("FALSE");
605 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
606 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
607 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
608 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
609 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
610 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
611 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
612 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
613 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
614 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
615 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
616 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
617 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
618 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
619 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
620 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
621 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
622 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
624 ERR("Invalid key[%s]", key);
625 val = g_strdup("NOTSUPPORTED");
628 *value = g_strdup(val);
631 g_key_file_free(keyfile);
637 static GSList *_get_list(void)
640 struct dirent *dp = NULL;
643 dir = opendir(CONNMAN_STORAGE);
645 ERR("Cannot open dir %s", CONNMAN_STORAGE);
649 while ((dp = readdir(dir)) != NULL) {
650 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
651 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
654 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
655 list = g_slist_append(list, g_strdup(config_id));
663 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
665 gboolean ret = FALSE;
668 if ((service_profile == NULL) || (config_id == NULL)) {
669 ERR("Invalid parameter");
673 ret = __get_config_id(service_profile, &val);
674 *config_id = g_strdup(val);
680 gboolean wifi_config_remove_configuration(const gchar *config_id)
682 gboolean ret = FALSE;
684 ret = _remove_configuration(config_id);
689 int __netconfig_hex_char_to_num(char c)
691 if (c >= '0' && c <= '9')
694 if (c >= 'a' && c <= 'f')
697 if (c >= 'A' && c <= 'F')
703 int __netconfig_hex_to_byte(const char *hex)
707 a = __netconfig_hex_char_to_num(*hex++);
711 b = __netconfig_hex_char_to_num(*hex++);
718 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
722 const char *ipos = hex;
723 unsigned char *opos = buf;
725 for (i = 0; i < len; i++) {
726 a = __netconfig_hex_to_byte(ipos);
737 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
744 ERR("Invalid parameter.");
748 *dst = (char *) g_try_malloc0((2*src_len)+1);
750 ERR("failed to allocate memory to buffer.");
756 for (i = 0; i < src_len; i++) {
757 snprintf(buf, 3, "%02x", src[i]);
765 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
767 GVariantIter *iter_copy = NULL;
770 unsigned char *tmp_dst = NULL;
772 if (!dst || *dst || !iter) {
773 ERR("Invalid parameter");
777 iter_copy = g_variant_iter_copy(iter);
779 while (g_variant_iter_loop(iter, "y", &tmp))
781 g_variant_iter_free(iter);
783 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
785 ERR("failed to allocate memory");
786 g_variant_iter_free(iter_copy);
791 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
793 g_variant_iter_free(iter_copy);
799 tmp_dst[length] = '\0';
803 DBG("Length [%d]", length);
807 gboolean _add_vsie(int frame_id, const char* vsie)
809 GVariant *params = NULL;
810 GVariant *message = NULL;
811 GVariantBuilder *bytearray_builder = NULL;
816 unsigned char *bytearray = NULL;
817 size_t bytearray_len = 0;
819 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
820 DBG("Invalid parameter, frame-id: %d", frame_id);
824 vsie_len = strlen(vsie);
826 DBG("vsie length is zero");
830 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
832 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
833 if (bytearray == NULL) {
834 DBG("Failed to allocate memory to bytearray");
838 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
839 DBG("invalid vsie string");
844 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
845 for (i = 0; i < bytearray_len; i++)
846 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
848 params = g_variant_new("(iay)", frame_id, bytearray_builder);
849 g_variant_builder_unref(bytearray_builder);
851 if_path = netconfig_wifi_get_supplicant_interface();
853 if (if_path == NULL) {
854 ERR("Fail to get wpa_supplicant DBus path");
859 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
860 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
863 if (message == NULL) {
864 ERR("Failed to send command to wpa_supplicant");
869 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
875 gboolean _get_vsie(int frame_id, char **vsie)
877 GVariant *params = NULL;
878 GVariant *message = NULL;
881 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
882 DBG("Invalid parameter, frame-id: %d", frame_id);
886 if_path = netconfig_wifi_get_supplicant_interface();
887 if (if_path == NULL) {
888 ERR("Fail to get wpa_supplicant DBus path");
892 params = g_variant_new("(i)", frame_id);
894 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
895 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
898 if (message == NULL) {
899 ERR("Failed to send command to wpa_supplicant");
902 GVariantIter *iter = NULL;
903 unsigned char *vsie_bytes = NULL;
907 g_variant_get(message, "(ay)", &iter);
909 ERR("vsie is not present");
913 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
914 if (vsie_bytes == NULL) {
915 ERR("vsie_bytes not allocated");
919 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
922 ERR("vsie not allocated.");
929 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
935 gboolean _remove_vsie(int frame_id, const char *vsie)
937 GVariant *params = NULL;
938 GVariant *message = NULL;
939 GVariantBuilder *bytearray_builder = NULL;
944 unsigned char *bytearray = NULL;
945 size_t bytearray_len = 0;
947 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
948 DBG("Invalid parameter, frame-id: %d", frame_id);
952 vsie_len = strlen(vsie);
954 DBG("vsie length is zero");
958 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
960 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
961 if (bytearray == NULL) {
962 DBG("Failed to allocate memory to bytearray");
966 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
967 DBG("invalid vsie string");
972 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
973 for (i = 0; i < bytearray_len; i++)
974 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
976 params = g_variant_new("(iay)", frame_id, bytearray_builder);
977 g_variant_builder_unref(bytearray_builder);
979 if_path = netconfig_wifi_get_supplicant_interface();
980 if (if_path == NULL) {
981 ERR("Fail to get wpa_supplicant DBus path");
986 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
987 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
990 if (message == NULL) {
991 ERR("Failed to send command to wpa_supplicant");
996 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1003 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
1006 GSList *config_ids = NULL;
1008 gchar **result = NULL;
1010 g_return_val_if_fail(wifi != NULL, TRUE);
1012 config_ids = _get_list();
1013 if (config_ids == NULL) {
1014 ERR("Fail to get config list");
1015 netconfig_error_no_profile(context);
1019 length = g_slist_length(config_ids);
1020 result = g_new0(gchar *, length + 1);
1021 for (i = 0; i < length; i++) {
1022 gchar *config_id = g_slist_nth_data(config_ids, i);
1023 result[i] = g_strdup(config_id);
1026 config_ids = g_slist_nth(config_ids, 0);
1027 g_slist_free_full(config_ids, g_free);
1029 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1031 for (i = 0; i < length; i++)
1041 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1042 const gchar *config_id)
1044 gboolean ret = FALSE;
1045 GVariantBuilder *b = NULL;
1046 struct wifi_config *conf = NULL;
1048 g_return_val_if_fail(wifi != NULL, TRUE);
1050 conf = g_new0(struct wifi_config, 1);
1051 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1053 ret = _load_configuration(config_id, conf);
1055 g_free(conf->ip_info);
1057 ERR("Fail to _load_configuration");
1058 netconfig_error_no_profile(context);
1062 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1063 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1064 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1065 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1067 if (conf->proxy_address != NULL)
1068 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1070 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1072 if (conf->ip_info->ip_type != NULL)
1073 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1075 if (conf->ip_info->ip_address != NULL)
1076 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1078 if (conf->ip_info->subnet_mask != NULL)
1079 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1081 if (conf->ip_info->gateway_address != NULL)
1082 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1084 if (conf->ip_info->dns_type != NULL)
1085 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1087 int i = 0, count = conf->ip_info->dns_count;
1089 if (conf->ip_info->dns_address[i] != NULL)
1090 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1095 if (conf->last_error != NULL)
1096 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1098 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1100 __free_wifi_configuration(conf);
1102 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1103 g_variant_builder_unref(b);
1107 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1108 const char *netmask)
1117 mask = inet_network(netmask);
1120 /* a valid netmask must be 2^n - 1 */
1121 if ((host & (host + 1)) != 0)
1125 for (; mask; mask <<= 1)
1131 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1132 const gchar *config_id, GVariant *configuration)
1134 gboolean ret = FALSE;
1135 struct wifi_config *conf = NULL;
1136 GKeyFile *keyfile = NULL;
1140 gchar *group_name = NULL;
1143 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1144 ERR("Invalid parameter");
1145 netconfig_error_invalid_parameter(context);
1149 conf = g_new0(struct wifi_config, 1);
1150 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1152 g_variant_get(configuration, "a{sv}", &iter);
1153 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1154 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1155 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1156 conf->name = g_strdup(g_variant_get_string(value, NULL));
1157 DBG("name [%s]", conf->name);
1161 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1162 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1163 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1164 DBG("ssid [%s]", conf->ssid);
1168 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1169 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1170 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1171 DBG("passphrase []");
1173 conf->passphrase = NULL;
1175 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1176 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1177 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1178 DBG("is_hidden [%s]", conf->is_hidden);
1180 conf->is_hidden = NULL;
1182 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1183 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1184 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1185 DBG("IP config type [%s]", conf->ip_info->ip_type);
1187 conf->ip_info->ip_type = NULL;
1189 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1190 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1191 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1192 DBG("IP address [%s]", conf->ip_info->ip_address);
1194 conf->ip_info->ip_address = NULL;
1196 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1197 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1198 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1199 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1201 conf->ip_info->subnet_mask = NULL;
1203 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1204 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1205 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1206 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1208 conf->ip_info->gateway_address = NULL;
1210 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1211 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1212 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1213 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1215 conf->ip_info->dns_type = NULL;
1217 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1218 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1219 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1220 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1221 conf->ip_info->dns_count = order + 1;
1224 conf->ip_info->dns_address[order++] = NULL;
1226 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1227 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1228 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1229 DBG("proxy_address [%s]", conf->proxy_address);
1231 conf->proxy_address = NULL;
1235 conf->favorite = TRUE;
1236 conf->autoconnect = TRUE;
1238 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1240 __free_wifi_configuration(conf);
1241 ERR("Fail to get_wifi_config_group_name");
1242 netconfig_error_fail_save_congifuration(context);
1246 keyfile = g_key_file_new();
1247 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1248 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1250 if (conf->passphrase != NULL) {
1251 gchar *enc_data = NULL;
1252 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1255 ERR("Failed to encrypt the passphrase");
1257 g_free(conf->passphrase);
1258 conf->passphrase = enc_data;
1261 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1264 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1265 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1267 /* Optional field */
1268 if (conf->proxy_address != NULL) {
1269 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1270 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1273 if (conf->is_hidden != NULL) {
1274 gboolean hidden = FALSE;
1275 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1277 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1280 if (conf->ip_info->ip_type != NULL)
1281 g_key_file_set_string(keyfile, group_name,
1282 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1284 if (conf->ip_info->ip_address != NULL)
1285 g_key_file_set_string(keyfile, group_name,
1286 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1288 if (conf->ip_info->subnet_mask != NULL) {
1289 unsigned char prefix_len;
1290 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1291 conf->ip_info->subnet_mask);
1292 g_key_file_set_integer(keyfile, group_name,
1293 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1296 if (conf->ip_info->gateway_address != NULL)
1297 g_key_file_set_string(keyfile, group_name,
1298 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1300 if (conf->ip_info->dns_type != NULL)
1301 g_key_file_set_string(keyfile, group_name,
1302 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1304 int i = 0, count = conf->ip_info->dns_count;
1306 if (conf->ip_info->dns_address[i] != NULL)
1307 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1308 conf->ip_info->dns_address[i]);
1313 ret = _save_configuration(config_id, keyfile);
1315 INFO("Success to save configuration [%s]", config_id);
1316 wifi_complete_save_configuration(wifi, context);
1318 INFO("Fail to save configuration [%s]", config_id);
1319 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1322 g_key_file_free(keyfile);
1324 __free_wifi_configuration(conf);
1326 g_variant_iter_free(iter);
1331 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1332 const gchar *config_id)
1334 gboolean ret = FALSE;
1335 GVariantBuilder *b = NULL;
1336 struct wifi_config *conf = NULL;
1338 g_return_val_if_fail(wifi != NULL, TRUE);
1340 conf = g_new0(struct wifi_config, 1);
1341 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1343 ret = _load_configuration(config_id, conf);
1345 g_free(conf->eap_config);
1347 ERR("Fail to _load_configuration");
1348 netconfig_error_no_profile(context);
1352 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1353 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1354 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1355 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1356 if (conf->proxy_address != NULL)
1357 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1359 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1361 if (conf->last_error != NULL)
1362 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1364 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1366 if (conf->eap_config != NULL) {
1367 if (conf->eap_config->anonymous_identity != NULL)
1368 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1370 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1372 if (conf->eap_config->ca_cert != NULL)
1373 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1375 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1377 if (conf->eap_config->client_cert != NULL)
1378 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1380 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1382 if (conf->eap_config->private_key != NULL)
1383 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1385 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1387 if (conf->eap_config->private_key_password != NULL)
1388 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1390 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1392 if (conf->eap_config->identity != NULL)
1393 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1395 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1397 if (conf->eap_config->eap_type != NULL)
1398 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1400 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1402 if (conf->eap_config->eap_auth_type != NULL)
1403 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1405 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1407 if (conf->eap_config->subject_match != NULL)
1408 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1410 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1413 __free_wifi_configuration(conf);
1415 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1416 g_variant_builder_unref(b);
1420 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1421 const gchar *config_id, GVariant *configuration)
1423 gboolean ret = FALSE;
1424 struct wifi_config *conf = NULL;
1425 GKeyFile *keyfile = NULL;
1429 gchar *group_name = NULL;
1431 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1432 ERR("Invalid parameter");
1433 netconfig_error_invalid_parameter(context);
1437 conf = g_new0(struct wifi_config, 1);
1438 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1440 g_variant_get(configuration, "a{sv}", &iter);
1441 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1442 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1443 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1444 conf->name = g_strdup(g_variant_get_string(value, NULL));
1445 DBG("name [%s]", conf->name);
1449 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1450 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1451 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1452 DBG("ssid [%s]", conf->ssid);
1456 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1457 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1458 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1459 DBG("passphrase [%s]", conf->passphrase);
1461 conf->passphrase = NULL;
1463 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1464 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1465 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1466 DBG("is_hidden [%s]", conf->is_hidden);
1468 conf->is_hidden = NULL;
1470 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1471 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1472 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1473 DBG("proxy_address [%s]", conf->proxy_address);
1475 conf->proxy_address = NULL;
1477 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1478 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1479 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1480 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1482 conf->eap_config->anonymous_identity = NULL;
1484 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1485 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1486 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1487 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1489 conf->eap_config->ca_cert = NULL;
1491 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1492 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1493 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1494 DBG("client_cert [%s]", conf->eap_config->client_cert);
1496 conf->eap_config->client_cert = NULL;
1498 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1499 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1500 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1501 DBG("private_key [%s]", conf->eap_config->private_key);
1503 conf->eap_config->private_key = NULL;
1505 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1506 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1507 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1508 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1510 conf->eap_config->private_key_password = NULL;
1512 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1513 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1514 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1515 DBG("identity [%s]", conf->eap_config->identity);
1517 conf->eap_config->identity = NULL;
1519 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1520 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1521 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1522 DBG("eap_type [%s]", conf->eap_config->eap_type);
1524 conf->eap_config->eap_type = NULL;
1526 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1527 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1528 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1529 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1531 conf->eap_config->eap_auth_type = NULL;
1533 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1534 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1535 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1536 DBG("subject_match [%s]", conf->eap_config->subject_match);
1538 conf->eap_config->subject_match = NULL;
1542 conf->favorite = TRUE;
1543 conf->autoconnect = TRUE;
1545 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1547 __free_wifi_configuration(conf);
1548 ERR("Fail to get_wifi_config_group_name");
1552 keyfile = g_key_file_new();
1553 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1554 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1556 if (conf->passphrase != NULL) {
1557 gchar *enc_data = NULL;
1558 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1561 ERR("Failed to encrypt the passphrase");
1563 g_free(conf->passphrase);
1564 conf->passphrase = enc_data;
1566 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1569 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1570 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1572 /* Optional field */
1573 if (conf->proxy_address != NULL) {
1574 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1575 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1578 if (conf->is_hidden != NULL) {
1579 gboolean hidden = FALSE;
1580 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1582 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1585 if (conf->eap_config->anonymous_identity != NULL)
1586 g_key_file_set_string(keyfile, group_name,
1587 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1589 if (conf->eap_config->ca_cert != NULL)
1590 g_key_file_set_string(keyfile, group_name,
1591 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1593 if (conf->eap_config->client_cert != NULL)
1594 g_key_file_set_string(keyfile, group_name,
1595 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1597 if (conf->eap_config->private_key != NULL)
1598 g_key_file_set_string(keyfile, group_name,
1599 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1601 if (conf->eap_config->private_key_password != NULL)
1602 g_key_file_set_string(keyfile, group_name,
1603 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1605 if (conf->eap_config->identity != NULL)
1606 g_key_file_set_string(keyfile, group_name,
1607 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1609 if (conf->eap_config->eap_type != NULL)
1610 g_key_file_set_string(keyfile, group_name,
1611 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1613 if (conf->eap_config->eap_auth_type != NULL)
1614 g_key_file_set_string(keyfile, group_name,
1615 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1617 if (conf->eap_config->subject_match != NULL)
1618 g_key_file_set_string(keyfile, group_name,
1619 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1621 ret = _save_configuration(config_id, keyfile);
1623 INFO("Success to save eap configuration [%s]", config_id);
1624 wifi_complete_save_eap_configuration(wifi, context);
1626 INFO("Fail to save eap configuration [%s]", config_id);
1627 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1630 g_key_file_free(keyfile);
1632 __free_wifi_configuration(conf);
1634 g_variant_iter_free(iter);
1639 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1641 gboolean ret = FALSE;
1643 if ((wifi == NULL) || (config_id == NULL)) {
1644 ERR("Invalid parameter");
1645 netconfig_error_invalid_parameter(context);
1649 ret = _remove_configuration(config_id);
1651 /* no configuration or error */
1652 ERR("No [%s] configuration", config_id);
1653 netconfig_error_no_profile(context);
1657 wifi_complete_remove_configuration(wifi, context);
1661 /* config field key / value */
1663 * [wifi_macaddress_config_id]
1664 * Name=name (mandatory)
1665 * SSID=SSID (mandatory)
1666 * Frequency=2462 (X)
1668 * AutoConnect=true (Default true)
1669 * Modified=2015-03-20 (X)
1670 * IPv4.method=manual (O)
1671 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1672 * IPv6.method=auto (X)
1673 * IPv6.privacy=disabled (X)
1674 * IPv4.netmask_prefixlen=24 (X)
1675 * IPv4.local_address=192.0.0.1 (O)
1676 * IPv4.gateway=192.0.0.1 (O ? X ?)
1677 * Nameservers=192.168.43.22; (O)
1678 * Proxy.Method=manual (O)
1679 * Proxy.Servers=trst.com:8888; (O)
1681 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1682 const gchar *config_id, const gchar *key, const gchar *value)
1684 gboolean ret = FALSE;
1685 gchar *keyfile_key = NULL;
1687 g_return_val_if_fail(wifi != NULL, TRUE);
1688 g_return_val_if_fail(config_id != NULL, TRUE);
1689 g_return_val_if_fail(key != NULL, TRUE);
1691 DBG("Key[%s] Value[%d]", key, value);
1693 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1694 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1696 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1697 netconfig_error_invalid_parameter(context);
1700 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1701 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1702 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1703 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1704 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1705 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1706 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1707 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1708 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1709 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1710 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1711 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1712 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1713 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1714 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1715 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1716 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1717 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1718 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1720 ERR("Not supported key[%s]", key);
1721 netconfig_error_invalid_parameter(context);
1725 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1727 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1730 if (keyfile_key != NULL)
1731 g_free(keyfile_key);
1733 wifi_complete_set_config_field(wifi, context);
1737 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1739 gboolean ret = FALSE;
1740 gchar *passphrase = NULL;
1742 if ((wifi == NULL) || (config_id == NULL)) {
1743 ERR("Invalid parameter");
1744 netconfig_error_invalid_parameter(context);
1748 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1750 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1751 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1755 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1761 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1762 int frame_id, const gchar *vsie)
1764 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1766 g_return_val_if_fail(wifi != NULL, TRUE);
1767 g_return_val_if_fail(vsie != NULL, TRUE);
1769 gboolean ret = FALSE;
1771 ret = _add_vsie(frame_id, vsie);
1773 DBG("Failed to add vsie: %s", vsie);
1774 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1778 wifi_complete_add_vsie(wifi, context);
1782 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1785 DBG("Frame ID: [%d]", frame_id);
1787 g_return_val_if_fail(wifi != NULL, TRUE);
1789 gboolean ret = FALSE;
1792 ret = _get_vsie(frame_id, &vsie);
1794 DBG("Failed to get vsie for frame:[%d]", frame_id);
1795 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1799 DBG("Received vsie: %s", vsie);
1800 wifi_complete_get_vsie(wifi, context, vsie);
1805 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1806 int frame_id, const gchar *vsie)
1808 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1810 g_return_val_if_fail(wifi != NULL, TRUE);
1811 g_return_val_if_fail(vsie != NULL, TRUE);
1813 gboolean ret = FALSE;
1815 ret = _remove_vsie(frame_id, vsie);
1817 DBG("Failed to remove vsie: %s", vsie);
1818 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1822 wifi_complete_remove_vsie(wifi, context);