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 #define MAX_WIFI_PROFILES 200
60 struct wifi_eap_config {
61 gchar *anonymous_identity;
65 gchar *private_key_password;
75 gchar *gateway_address;
76 gchar *dns_address[NET_DNS_ADDR_MAX];
94 struct wifi_eap_config *eap_config;
95 wifi_ip_info_s *ip_info;
99 static void __free_wifi_configuration(struct wifi_config *conf)
106 g_free(conf->passphrase);
107 g_free(conf->security_type);
108 g_free(conf->is_hidden);
109 g_free(conf->proxy_address);
110 g_free(conf->last_error);
111 if (conf->eap_config) {
112 g_free(conf->eap_config->anonymous_identity);
113 g_free(conf->eap_config->ca_cert);
114 g_free(conf->eap_config->client_cert);
115 g_free(conf->eap_config->private_key);
116 g_free(conf->eap_config->private_key_password);
117 g_free(conf->eap_config->identity);
118 g_free(conf->eap_config->eap_type);
119 g_free(conf->eap_config->eap_auth_type);
120 g_free(conf->eap_config->subject_match);
121 g_free(conf->eap_config);
125 g_free(conf->ip_info->ip_type);
126 g_free(conf->ip_info->ip_address);
127 g_free(conf->ip_info->subnet_mask);
128 g_free(conf->ip_info->gateway_address);
129 g_free(conf->ip_info->dns_type);
130 conf->ip_info->prefix_length = 0;
132 int i = 0, count = conf->ip_info->dns_count;
134 g_free(conf->ip_info->dns_address[i]);
137 g_free(conf->ip_info);
142 static gboolean __get_mac_address(gchar **mac_address)
144 gchar *tmp_mac = NULL;
146 gchar mac[13] = { 0, };
151 char buf[WIFI_MAC_ADD_LENGTH + 1];
152 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
153 fp = fopen(WIFI_MAC_ADD_PATH, "r");
156 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
161 if (fgets(buf, sizeof(buf), fp) == NULL) {
162 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
167 tmp_mac = (gchar *)malloc(WIFI_MAC_ADD_LENGTH + 1);
168 if (tmp_mac == NULL) {
169 ERR("malloc() failed");
174 memset(tmp_mac, 0, WIFI_MAC_ADD_LENGTH + 1);
175 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
178 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
179 if (tmp_mac == NULL) {
180 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
185 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
187 while (tmp && tmp[i]) {
193 *mac_address = g_strdup(mac);
199 gboolean wifi_config_get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
201 gchar *mac_address = NULL;
202 gchar *g_name = NULL;
203 gboolean ret = FALSE;
205 ret = __get_mac_address(&mac_address);
206 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
207 ERR("Cannot get WIFI MAC address");
212 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
213 if (g_name == NULL) {
218 *group_name = g_strdup(g_name);
226 static gboolean __get_security_type(const gchar *config_id, gchar **type)
228 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
229 *type = g_strdup(WIFI_SECURITY_NONE);
230 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
231 *type = g_strdup(WIFI_SECURITY_WEP);
232 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
233 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
234 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
235 *type = g_strdup(WIFI_SECURITY_EAP);
244 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
246 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
247 if (*config_id == NULL) {
256 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
258 GKeyFile *keyfile = NULL;
261 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
263 keyfile = netconfig_keyfile_load(path);
265 ERR("keyfile[%s] is NULL", path);
272 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
274 gboolean ret = FALSE;
277 path = g_strdup_printf("%s/%s", pathname, filename);
278 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
280 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
289 static gboolean __remove_configuration(const gchar *pathname)
293 if (__remove_file(pathname, "settings") != TRUE) {
294 ERR("Cannot remove [%s/settings]", pathname);
297 if (__remove_file(pathname, "data") != TRUE) {
298 ERR("Cannot remove [%s/data]", pathname);
302 ret = rmdir(pathname);
304 ERR("Cannot remove [%s]", pathname);
310 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
314 gboolean hidden = FALSE;
315 gboolean ret = FALSE;
317 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
319 ERR("Fail to get_wifi_config_group_name");
323 keyfile = __get_configuration_keyfile(group_name);
324 if (keyfile == NULL) {
325 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
330 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
331 DBG("name [%s]", config->name);
333 __get_security_type(config_id, &config->security_type);
334 if (config->security_type == NULL) {
335 ERR("Fail to _get_security_type");
336 g_key_file_free(keyfile);
340 DBG("security_type [%s]", config->security_type);
342 config->passphrase = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
343 DBG("passphrase []");
345 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
346 if (config->proxy_address)
347 DBG("proxy_address [%s]", config->proxy_address);
349 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
351 config->is_hidden = g_strdup("TRUE");
353 config->is_hidden = g_strdup("FALSE");
354 DBG("is_hidden [%s]", config->is_hidden);
356 if (config->ip_info) {
357 GError *error = NULL;
358 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
359 WIFI_CONFIG_IPV4_METHOD, NULL);
360 if (config->ip_info->ip_type)
361 DBG("IPv4.Method:%s", config->ip_info->ip_type);
363 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
364 WIFI_CONFIG_IPV4_ADDRESS, NULL);
365 if (config->ip_info->ip_address)
366 DBG("IPv4.Address:%s", config->ip_info->ip_address);
370 struct in_addr netmask;
372 prefix_len = g_key_file_get_integer(keyfile, group_name,
373 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
375 DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
378 if (prefix_len > 0 && prefix_len < 32) {
379 addr = 0xffffffff << (32 - prefix_len);
380 netmask.s_addr = htonl(addr);
381 mask = inet_ntoa(netmask);
382 config->ip_info->subnet_mask = g_strdup(mask);
384 if (config->ip_info->subnet_mask)
385 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
388 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
389 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
390 if (config->ip_info->gateway_address)
391 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
393 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
394 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
395 if (config->ip_info->dns_type)
396 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
400 nameservers = g_key_file_get_string_list(keyfile, group_name,
401 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
404 config->ip_info->dns_count = length;
406 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
407 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
408 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
412 g_strfreev(nameservers);
417 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
418 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
419 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
420 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
421 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
422 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
423 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
424 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
425 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
426 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
428 if (config->eap_config->anonymous_identity)
429 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
430 if (config->eap_config->ca_cert)
431 DBG("ca_cert [%s]", config->eap_config->ca_cert);
432 if (config->eap_config->client_cert)
433 DBG("client_cert [%s]", config->eap_config->client_cert);
434 if (config->eap_config->private_key)
435 DBG("private_key [%s]", config->eap_config->private_key);
436 if (config->eap_config->private_key_password)
437 DBG("private_key_password [%s]", config->eap_config->private_key_password);
438 if (config->eap_config->identity)
439 DBG("identity [%s]", config->eap_config->identity);
440 if (config->eap_config->eap_type)
441 DBG("eap_type [%s]", config->eap_config->eap_type);
442 if (config->eap_config->eap_auth_type)
443 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
444 if (config->eap_config->subject_match)
445 DBG("subject_match [%s]", config->eap_config->subject_match);
448 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
449 if (config->last_error)
450 DBG("last_error [%s]", config->last_error);
452 g_key_file_free(keyfile);
458 gboolean wifi_config_save_configuration(const gchar *config_id, GKeyFile *keyfile)
463 gboolean ret = FALSE;
465 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
467 ERR("Fail to get_wifi_config_group_name");
471 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
472 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
473 if (__remove_configuration(dir) != TRUE) {
474 ERR("[%s] is existed, but cannot remove", dir);
481 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
482 ERR("Cannot mkdir %s", dir);
488 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
489 netconfig_keyfile_save(keyfile, path);
497 static gboolean _remove_configuration(const gchar *config_id)
499 gboolean ret = FALSE;
503 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
505 ERR("Fail to get_wifi_config_group_name");
509 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
510 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
511 if (__remove_configuration(dir) != TRUE) {
512 ERR("[%s] is existed, but cannot remove", dir);
515 INFO("Success to remove [%s]", dir);
518 ERR("[%s] is not existed", dir);
529 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
535 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
537 ERR("Fail to get_wifi_config_group_name");
540 DBG("group_name %s", group_name);
542 keyfile = __get_configuration_keyfile(group_name);
543 if (keyfile == NULL) {
544 ERR("Fail to __get_configuration_keyfile");
549 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
550 g_key_file_set_string(keyfile, group_name, key, value);
551 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
552 g_key_file_set_string(keyfile, group_name, key, value);
553 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
554 gboolean hidden = FALSE;
555 if (g_strcmp0(value, "TRUE") == 0)
557 g_key_file_set_boolean(keyfile, group_name, key, hidden);
558 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
559 g_key_file_set_string(keyfile, group_name, key, value);
560 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
561 g_key_file_set_string(keyfile, group_name, key, value);
562 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
563 g_key_file_set_string(keyfile, group_name, key, value);
564 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
565 g_key_file_set_string(keyfile, group_name, key, value);
566 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
567 g_key_file_set_string(keyfile, group_name, key, value);
568 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
569 g_key_file_set_string(keyfile, group_name, key, value);
570 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
571 g_key_file_set_string(keyfile, group_name, key, value);
572 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
573 g_key_file_set_string(keyfile, group_name, key, value);
575 ERR("key[%s] is not supported", key);
579 wifi_config_save_configuration(config_id, keyfile);
581 g_key_file_free(keyfile);
587 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
592 gboolean hidden = FALSE;
593 gboolean ret = FALSE;
595 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
597 ERR("Fail to get_wifi_config_group_name");
600 DBG("group_name %s", group_name);
602 keyfile = __get_configuration_keyfile(group_name);
603 if (keyfile == NULL) {
604 ERR("Fail to __get_configuration_keyfile");
609 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
610 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
611 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
612 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
613 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
614 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
615 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
616 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
618 val = g_strdup("TRUE");
620 val = g_strdup("FALSE");
621 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
622 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
623 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
624 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
625 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
626 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
627 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
628 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
629 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
630 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
631 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
632 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
633 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
634 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
635 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
636 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
637 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
638 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
640 ERR("Invalid key[%s]", key);
641 val = g_strdup("NOTSUPPORTED");
644 *value = g_strdup(val);
647 g_key_file_free(keyfile);
653 static GSList *_get_list(void)
656 struct dirent *dp = NULL;
659 dir = opendir(CONNMAN_STORAGE);
661 ERR("Cannot open dir %s", CONNMAN_STORAGE);
665 while ((dp = readdir(dir)) != NULL) {
666 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
667 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
670 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
671 list = g_slist_append(list, g_strdup(config_id));
679 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
681 gboolean ret = FALSE;
684 if ((service_profile == NULL) || (config_id == NULL)) {
685 ERR("Invalid parameter");
689 ret = __get_config_id(service_profile, &val);
690 *config_id = g_strdup(val);
696 gboolean wifi_config_remove_configuration(const gchar *config_id)
698 gboolean ret = FALSE;
700 ret = _remove_configuration(config_id);
705 int __netconfig_hex_char_to_num(char c)
707 if (c >= '0' && c <= '9')
710 if (c >= 'a' && c <= 'f')
713 if (c >= 'A' && c <= 'F')
719 int __netconfig_hex_to_byte(const char *hex)
723 a = __netconfig_hex_char_to_num(*hex++);
727 b = __netconfig_hex_char_to_num(*hex++);
734 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
738 const char *ipos = hex;
739 unsigned char *opos = buf;
741 for (i = 0; i < len; i++) {
742 a = __netconfig_hex_to_byte(ipos);
753 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
760 ERR("Invalid parameter.");
764 *dst = (char *) g_try_malloc0((2*src_len)+1);
766 ERR("failed to allocate memory to buffer.");
772 for (i = 0; i < src_len; i++) {
773 snprintf(buf, 3, "%02x", src[i]);
781 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
783 GVariantIter *iter_copy = NULL;
786 unsigned char *tmp_dst = NULL;
788 if (!dst || *dst || !iter) {
789 ERR("Invalid parameter");
793 iter_copy = g_variant_iter_copy(iter);
795 while (g_variant_iter_loop(iter, "y", &tmp))
797 g_variant_iter_free(iter);
799 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
801 ERR("failed to allocate memory");
802 g_variant_iter_free(iter_copy);
807 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
809 g_variant_iter_free(iter_copy);
815 tmp_dst[length] = '\0';
819 DBG("Length [%d]", length);
823 gboolean _add_vsie(int frame_id, const char* vsie)
825 GVariant *params = NULL;
826 GVariant *message = NULL;
827 GVariantBuilder *bytearray_builder = NULL;
832 unsigned char *bytearray = NULL;
833 size_t bytearray_len = 0;
835 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
836 DBG("Invalid parameter, frame-id: %d", frame_id);
840 vsie_len = strlen(vsie);
842 DBG("vsie length is zero");
846 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
848 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
849 if (bytearray == NULL) {
850 DBG("Failed to allocate memory to bytearray");
854 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
855 DBG("invalid vsie string");
860 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
861 for (i = 0; i < bytearray_len; i++)
862 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
864 params = g_variant_new("(iay)", frame_id, bytearray_builder);
865 g_variant_builder_unref(bytearray_builder);
867 if_path = netconfig_wifi_get_supplicant_interface();
869 if (if_path == NULL) {
870 ERR("Fail to get wpa_supplicant DBus path");
875 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
876 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
879 if (message == NULL) {
880 ERR("Failed to send command to wpa_supplicant");
885 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
891 gboolean _get_vsie(int frame_id, char **vsie)
893 GVariant *params = NULL;
894 GVariant *message = NULL;
897 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
898 DBG("Invalid parameter, frame-id: %d", frame_id);
902 if_path = netconfig_wifi_get_supplicant_interface();
903 if (if_path == NULL) {
904 ERR("Fail to get wpa_supplicant DBus path");
908 params = g_variant_new("(i)", frame_id);
910 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
911 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
914 if (message == NULL) {
915 ERR("Failed to send command to wpa_supplicant");
918 GVariantIter *iter = NULL;
919 unsigned char *vsie_bytes = NULL;
923 g_variant_get(message, "(ay)", &iter);
925 ERR("vsie is not present");
929 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
930 if (vsie_bytes == NULL) {
931 ERR("vsie_bytes not allocated");
935 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
938 ERR("vsie not allocated.");
945 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
951 gboolean _remove_vsie(int frame_id, const char *vsie)
953 GVariant *params = NULL;
954 GVariant *message = NULL;
955 GVariantBuilder *bytearray_builder = NULL;
960 unsigned char *bytearray = NULL;
961 size_t bytearray_len = 0;
963 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
964 DBG("Invalid parameter, frame-id: %d", frame_id);
968 vsie_len = strlen(vsie);
970 DBG("vsie length is zero");
974 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
976 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
977 if (bytearray == NULL) {
978 DBG("Failed to allocate memory to bytearray");
982 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
983 DBG("invalid vsie string");
988 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
989 for (i = 0; i < bytearray_len; i++)
990 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
992 params = g_variant_new("(iay)", frame_id, bytearray_builder);
993 g_variant_builder_unref(bytearray_builder);
995 if_path = netconfig_wifi_get_supplicant_interface();
996 if (if_path == NULL) {
997 ERR("Fail to get wpa_supplicant DBus path");
1002 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
1003 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
1006 if (message == NULL) {
1007 ERR("Failed to send command to wpa_supplicant");
1012 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1019 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
1022 GSList *config_ids = NULL;
1024 gchar **result = NULL;
1026 g_return_val_if_fail(wifi != NULL, TRUE);
1028 config_ids = _get_list();
1029 if (config_ids == NULL) {
1030 ERR("Fail to get config list");
1031 netconfig_error_no_profile(context);
1035 length = g_slist_length(config_ids);
1036 result = g_new0(gchar *, length + 1);
1037 for (i = 0; i < length; i++) {
1038 gchar *config_id = g_slist_nth_data(config_ids, i);
1039 result[i] = g_strdup(config_id);
1042 config_ids = g_slist_nth(config_ids, 0);
1043 g_slist_free_full(config_ids, g_free);
1045 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1047 for (i = 0; i < length; i++)
1057 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1058 const gchar *config_id)
1060 gboolean ret = FALSE;
1061 GVariantBuilder *b = NULL;
1062 struct wifi_config *conf = NULL;
1064 g_return_val_if_fail(wifi != NULL, TRUE);
1066 conf = g_new0(struct wifi_config, 1);
1067 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1069 ret = _load_configuration(config_id, conf);
1071 g_free(conf->ip_info);
1073 ERR("Fail to _load_configuration");
1074 netconfig_error_no_profile(context);
1078 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1079 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1080 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1081 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PASSPHRASE, g_variant_new_string(conf->passphrase));
1082 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1084 if (conf->proxy_address != NULL)
1085 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1087 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1089 if (conf->ip_info->ip_type != NULL)
1090 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1092 if (conf->ip_info->ip_address != NULL)
1093 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1095 if (conf->ip_info->subnet_mask != NULL)
1096 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1098 if (conf->ip_info->prefix_length > 0)
1099 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV6_PREFIX_LEN, g_variant_new_int32(conf->ip_info->prefix_length));
1101 if (conf->ip_info->gateway_address != NULL)
1102 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1104 if (conf->ip_info->dns_type != NULL)
1105 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1107 int i = 0, count = conf->ip_info->dns_count;
1109 if (conf->ip_info->dns_address[i] != NULL)
1110 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1115 if (conf->last_error != NULL)
1116 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1118 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1120 __free_wifi_configuration(conf);
1122 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1123 g_variant_builder_unref(b);
1127 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1128 const char *netmask)
1137 mask = inet_network(netmask);
1140 /* a valid netmask must be 2^n - 1 */
1141 if ((host & (host + 1)) != 0)
1145 for (; mask; mask <<= 1)
1151 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1152 const gchar *config_id, GVariant *configuration)
1154 gboolean ret = FALSE;
1155 struct wifi_config *conf = NULL;
1156 GKeyFile *keyfile = NULL;
1160 gchar *group_name = NULL;
1163 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1164 ERR("Invalid parameter");
1165 netconfig_error_invalid_parameter(context);
1169 conf = g_new0(struct wifi_config, 1);
1170 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1172 g_variant_get(configuration, "a{sv}", &iter);
1173 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1174 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1175 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1176 conf->name = g_strdup(g_variant_get_string(value, NULL));
1177 DBG("name [%s]", conf->name);
1181 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1182 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1183 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1184 DBG("ssid [%s]", conf->ssid);
1188 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1189 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1190 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1191 DBG("passphrase []");
1193 conf->passphrase = NULL;
1195 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1196 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1197 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1198 DBG("is_hidden [%s]", conf->is_hidden);
1200 conf->is_hidden = NULL;
1202 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1203 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1204 conf->is_created = g_variant_get_boolean(value);
1205 DBG("is_created [%d]", conf->is_created);
1207 conf->is_created = FALSE;
1209 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1210 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1211 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1212 DBG("IP config type [%s]", conf->ip_info->ip_type);
1214 conf->ip_info->ip_type = NULL;
1216 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1217 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1218 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1219 DBG("IP address [%s]", conf->ip_info->ip_address);
1221 conf->ip_info->ip_address = NULL;
1223 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1224 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1225 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1226 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1228 conf->ip_info->subnet_mask = NULL;
1230 } else if (g_strcmp0(field, WIFI_CONFIG_IPV6_PREFIX_LEN) == 0) {
1231 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1232 conf->ip_info->prefix_length = g_variant_get_int32(value);
1233 DBG("IPv6 Prefix Length [%d]", conf->ip_info->prefix_length);
1235 conf->ip_info->prefix_length = 0;
1237 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1238 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1239 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1240 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1242 conf->ip_info->gateway_address = NULL;
1244 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1245 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1246 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1247 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1249 conf->ip_info->dns_type = NULL;
1251 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1252 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1253 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1254 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1255 conf->ip_info->dns_count = order + 1;
1258 conf->ip_info->dns_address[order++] = NULL;
1260 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1261 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1262 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1263 DBG("proxy_address [%s]", conf->proxy_address);
1265 conf->proxy_address = NULL;
1269 conf->favorite = TRUE;
1270 conf->autoconnect = TRUE;
1272 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1274 __free_wifi_configuration(conf);
1275 ERR("Fail to get_wifi_config_group_name");
1276 netconfig_error_fail_save_congifuration(context);
1280 keyfile = g_key_file_new();
1281 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1282 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1284 if (conf->passphrase != NULL) {
1285 gchar *enc_data = NULL;
1287 if (conf->is_created == true)
1288 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1290 enc_data = g_strdup(conf->passphrase);
1293 ERR("Failed to encrypt the passphrase");
1295 g_free(conf->passphrase);
1296 conf->passphrase = enc_data;
1299 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1302 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1303 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1304 g_get_current_time(&conf->modified);
1305 gchar *str = g_time_val_to_iso8601(&conf->modified);
1307 g_key_file_set_string(keyfile, group_name,
1308 WIFI_CONFIG_MODIFIED, str);
1312 /* Optional field */
1313 if (conf->proxy_address != NULL) {
1314 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1315 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1318 if (conf->is_hidden != NULL) {
1319 gboolean hidden = FALSE;
1320 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1322 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1325 if (conf->ip_info->ip_type != NULL)
1326 g_key_file_set_string(keyfile, group_name,
1327 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1329 if (conf->ip_info->ip_address != NULL)
1330 g_key_file_set_string(keyfile, group_name,
1331 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1333 if (conf->ip_info->subnet_mask != NULL) {
1334 unsigned char prefix_len;
1335 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1336 conf->ip_info->subnet_mask);
1337 if (prefix_len > 0 && prefix_len < 32)
1338 g_key_file_set_integer(keyfile, group_name,
1339 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1342 if (conf->ip_info->prefix_length > 0)
1343 g_key_file_set_integer(keyfile, group_name,
1344 WIFI_CONFIG_IPV6_PREFIX_LEN, conf->ip_info->prefix_length);
1346 if (conf->ip_info->gateway_address != NULL)
1347 g_key_file_set_string(keyfile, group_name,
1348 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1350 if (conf->ip_info->dns_type != NULL)
1351 g_key_file_set_string(keyfile, group_name,
1352 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1354 int i = 0, count = conf->ip_info->dns_count;
1356 if (conf->ip_info->dns_address[i] != NULL)
1357 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1358 conf->ip_info->dns_address[i]);
1363 ret = wifi_config_save_configuration(config_id, keyfile);
1365 INFO("Success to save configuration [%s]", config_id);
1366 wifi_complete_save_configuration(wifi, context);
1368 if (get_files_count(CONNMAN_STORAGE) > MAX_WIFI_PROFILES) {
1369 file = get_least_recently_profile(CONNMAN_STORAGE);
1371 gchar *profileName = g_strdup_printf(CONNMAN_STORAGE "/%s", file);
1372 INFO("least modified file: %s", profileName);
1374 if (__remove_configuration(profileName) != TRUE)
1375 DBG("Failed to remove profile: [%s]", profileName);
1377 ERR("Profile: [%s] does not exist", profileName);
1379 g_free(profileName);
1383 INFO("Fail to save configuration [%s]", config_id);
1384 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1387 g_key_file_free(keyfile);
1389 __free_wifi_configuration(conf);
1391 g_variant_iter_free(iter);
1396 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1397 const gchar *config_id)
1399 gboolean ret = FALSE;
1400 GVariantBuilder *b = NULL;
1401 struct wifi_config *conf = NULL;
1403 g_return_val_if_fail(wifi != NULL, TRUE);
1405 conf = g_new0(struct wifi_config, 1);
1406 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1407 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1409 ret = _load_configuration(config_id, conf);
1411 g_free(conf->eap_config);
1412 g_free(conf->ip_info);
1414 ERR("Fail to _load_configuration");
1415 netconfig_error_no_profile(context);
1419 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1420 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1421 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1422 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1423 if (conf->proxy_address != NULL)
1424 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1426 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1428 if (conf->last_error != NULL)
1429 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1431 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1433 if (conf->eap_config != NULL) {
1434 if (conf->eap_config->anonymous_identity != NULL)
1435 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1437 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1439 if (conf->eap_config->ca_cert != NULL)
1440 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1442 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1444 if (conf->eap_config->client_cert != NULL)
1445 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1447 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1449 if (conf->eap_config->private_key != NULL)
1450 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1452 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1454 if (conf->eap_config->private_key_password != NULL)
1455 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1457 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1459 if (conf->eap_config->identity != NULL)
1460 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1462 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1464 if (conf->eap_config->eap_type != NULL)
1465 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1467 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1469 if (conf->eap_config->eap_auth_type != NULL)
1470 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1472 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1474 if (conf->eap_config->subject_match != NULL)
1475 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1477 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1480 __free_wifi_configuration(conf);
1482 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1483 g_variant_builder_unref(b);
1487 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1488 const gchar *config_id, GVariant *configuration)
1490 gboolean ret = FALSE;
1491 struct wifi_config *conf = NULL;
1492 GKeyFile *keyfile = NULL;
1496 gchar *group_name = NULL;
1498 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1499 ERR("Invalid parameter");
1500 netconfig_error_invalid_parameter(context);
1504 conf = g_new0(struct wifi_config, 1);
1505 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1507 g_variant_get(configuration, "a{sv}", &iter);
1508 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1509 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1510 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1511 conf->name = g_strdup(g_variant_get_string(value, NULL));
1512 DBG("name [%s]", conf->name);
1516 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1517 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1518 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1519 DBG("ssid [%s]", conf->ssid);
1523 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1524 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1525 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1526 DBG("passphrase [%s]", conf->passphrase);
1528 conf->passphrase = NULL;
1530 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1531 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1532 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1533 DBG("is_hidden [%s]", conf->is_hidden);
1535 conf->is_hidden = NULL;
1537 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1538 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1539 conf->is_created = g_variant_get_boolean(value);
1540 DBG("is_created [%d]", conf->is_created);
1542 conf->is_created = FALSE;
1544 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1545 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1546 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1547 DBG("proxy_address [%s]", conf->proxy_address);
1549 conf->proxy_address = NULL;
1551 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1552 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1553 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1554 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1556 conf->eap_config->anonymous_identity = NULL;
1558 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1559 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1560 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1561 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1563 conf->eap_config->ca_cert = NULL;
1565 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1566 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1567 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1568 DBG("client_cert [%s]", conf->eap_config->client_cert);
1570 conf->eap_config->client_cert = NULL;
1572 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1573 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1574 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1575 DBG("private_key [%s]", conf->eap_config->private_key);
1577 conf->eap_config->private_key = NULL;
1579 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1580 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1581 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1582 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1584 conf->eap_config->private_key_password = NULL;
1586 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1587 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1588 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1589 DBG("identity [%s]", conf->eap_config->identity);
1591 conf->eap_config->identity = NULL;
1593 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1594 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1595 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1596 DBG("eap_type [%s]", conf->eap_config->eap_type);
1598 conf->eap_config->eap_type = NULL;
1600 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1601 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1602 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1603 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1605 conf->eap_config->eap_auth_type = NULL;
1607 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1608 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1609 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1610 DBG("subject_match [%s]", conf->eap_config->subject_match);
1612 conf->eap_config->subject_match = NULL;
1616 conf->favorite = TRUE;
1617 conf->autoconnect = TRUE;
1619 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1621 __free_wifi_configuration(conf);
1622 ERR("Fail to get_wifi_config_group_name");
1626 keyfile = g_key_file_new();
1627 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1628 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1630 if (conf->passphrase != NULL) {
1631 gchar *enc_data = NULL;
1633 if (conf->is_created == true)
1634 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1636 enc_data = g_strdup(conf->passphrase);
1639 ERR("Failed to encrypt the passphrase");
1641 g_free(conf->passphrase);
1642 conf->passphrase = enc_data;
1644 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1647 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1648 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1650 /* Optional field */
1651 if (conf->proxy_address != NULL) {
1652 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1653 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1656 if (conf->is_hidden != NULL) {
1657 gboolean hidden = FALSE;
1658 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1660 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1663 if (conf->eap_config->anonymous_identity != NULL)
1664 g_key_file_set_string(keyfile, group_name,
1665 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1667 if (conf->eap_config->ca_cert != NULL)
1668 g_key_file_set_string(keyfile, group_name,
1669 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1671 if (conf->eap_config->client_cert != NULL)
1672 g_key_file_set_string(keyfile, group_name,
1673 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1675 if (conf->eap_config->private_key != NULL)
1676 g_key_file_set_string(keyfile, group_name,
1677 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1679 if (conf->eap_config->private_key_password != NULL)
1680 g_key_file_set_string(keyfile, group_name,
1681 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1683 if (conf->eap_config->identity != NULL)
1684 g_key_file_set_string(keyfile, group_name,
1685 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1687 if (conf->eap_config->eap_type != NULL)
1688 g_key_file_set_string(keyfile, group_name,
1689 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1691 if (conf->eap_config->eap_auth_type != NULL)
1692 g_key_file_set_string(keyfile, group_name,
1693 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1695 if (conf->eap_config->subject_match != NULL)
1696 g_key_file_set_string(keyfile, group_name,
1697 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1699 ret = wifi_config_save_configuration(config_id, keyfile);
1701 INFO("Success to save eap configuration [%s]", config_id);
1702 wifi_complete_save_eap_configuration(wifi, context);
1704 INFO("Fail to save eap configuration [%s]", config_id);
1705 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1708 g_key_file_free(keyfile);
1710 __free_wifi_configuration(conf);
1712 g_variant_iter_free(iter);
1717 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1719 gboolean ret = FALSE;
1721 if ((wifi == NULL) || (config_id == NULL)) {
1722 ERR("Invalid parameter");
1723 netconfig_error_invalid_parameter(context);
1727 ret = _remove_configuration(config_id);
1729 /* no configuration or error */
1730 ERR("No [%s] configuration", config_id);
1731 netconfig_error_no_profile(context);
1735 wifi_complete_remove_configuration(wifi, context);
1739 /* config field key / value */
1741 * [wifi_macaddress_config_id]
1742 * Name=name (mandatory)
1743 * SSID=SSID (mandatory)
1744 * Frequency=2462 (X)
1746 * AutoConnect=true (Default true)
1747 * Modified=2015-03-20 (X)
1748 * IPv4.method=manual (O)
1749 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1750 * IPv6.method=auto (X)
1751 * IPv6.privacy=disabled (X)
1752 * IPv4.netmask_prefixlen=24 (X)
1753 * IPv4.local_address=192.0.0.1 (O)
1754 * IPv4.gateway=192.0.0.1 (O ? X ?)
1755 * Nameservers=192.168.43.22; (O)
1756 * Proxy.Method=manual (O)
1757 * Proxy.Servers=trst.com:8888; (O)
1759 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1760 const gchar *config_id, const gchar *key, const gchar *value)
1762 gboolean ret = FALSE;
1763 gchar *keyfile_key = NULL;
1765 g_return_val_if_fail(wifi != NULL, TRUE);
1766 g_return_val_if_fail(config_id != NULL, TRUE);
1767 g_return_val_if_fail(key != NULL, TRUE);
1769 DBG("Key[%s] Value[%s]", key, value);
1771 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1772 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1774 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1775 netconfig_error_invalid_parameter(context);
1778 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1779 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1780 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1781 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1782 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1783 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1784 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1785 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1786 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1787 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1788 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1789 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1790 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1791 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1792 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1793 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1794 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1795 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1796 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1798 ERR("Not supported key[%s]", key);
1799 netconfig_error_invalid_parameter(context);
1803 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1805 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1808 if (keyfile_key != NULL)
1809 g_free(keyfile_key);
1811 wifi_complete_set_config_field(wifi, context);
1815 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1817 gboolean ret = FALSE;
1818 gchar *passphrase = NULL;
1820 if ((wifi == NULL) || (config_id == NULL)) {
1821 ERR("Invalid parameter");
1822 netconfig_error_invalid_parameter(context);
1826 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1828 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1829 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1833 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1839 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1840 int frame_id, const gchar *vsie)
1842 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1844 g_return_val_if_fail(wifi != NULL, TRUE);
1845 g_return_val_if_fail(vsie != NULL, TRUE);
1847 gboolean ret = FALSE;
1849 ret = _add_vsie(frame_id, vsie);
1851 DBG("Failed to add vsie: %s", vsie);
1852 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1856 wifi_complete_add_vsie(wifi, context);
1860 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1863 DBG("Frame ID: [%d]", frame_id);
1865 g_return_val_if_fail(wifi != NULL, TRUE);
1867 gboolean ret = FALSE;
1870 ret = _get_vsie(frame_id, &vsie);
1872 DBG("Failed to get vsie for frame:[%d]", frame_id);
1873 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1877 DBG("Received vsie: %s", vsie);
1878 wifi_complete_get_vsie(wifi, context, vsie);
1883 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1884 int frame_id, const gchar *vsie)
1886 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1888 g_return_val_if_fail(wifi != NULL, TRUE);
1889 g_return_val_if_fail(vsie != NULL, TRUE);
1891 gboolean ret = FALSE;
1893 ret = _remove_vsie(frame_id, vsie);
1895 DBG("Failed to remove vsie: %s", vsie);
1896 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1900 wifi_complete_remove_vsie(wifi, context);