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];
91 struct wifi_eap_config *eap_config;
92 wifi_ip_info_s *ip_info;
96 static void __free_wifi_configuration(struct wifi_config *conf)
103 g_free(conf->passphrase);
104 g_free(conf->security_type);
105 g_free(conf->is_hidden);
106 g_free(conf->proxy_address);
107 g_free(conf->last_error);
108 if (conf->eap_config) {
109 g_free(conf->eap_config->anonymous_identity);
110 g_free(conf->eap_config->ca_cert);
111 g_free(conf->eap_config->client_cert);
112 g_free(conf->eap_config->private_key);
113 g_free(conf->eap_config->private_key_password);
114 g_free(conf->eap_config->identity);
115 g_free(conf->eap_config->eap_type);
116 g_free(conf->eap_config->eap_auth_type);
117 g_free(conf->eap_config->subject_match);
118 g_free(conf->eap_config);
122 g_free(conf->ip_info->ip_type);
123 g_free(conf->ip_info->ip_address);
124 g_free(conf->ip_info->subnet_mask);
125 g_free(conf->ip_info->gateway_address);
126 g_free(conf->ip_info->dns_type);
127 conf->ip_info->prefix_length = 0;
129 int i = 0, count = conf->ip_info->dns_count;
131 g_free(conf->ip_info->dns_address[i]);
134 g_free(conf->ip_info);
139 static gboolean __get_mac_address(gchar **mac_address)
141 gchar *tmp_mac = NULL;
143 gchar mac[13] = { 0, };
148 char buf[WIFI_MAC_ADD_LENGTH + 1];
149 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
150 fp = fopen(WIFI_MAC_ADD_PATH, "r");
153 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
158 if (fgets(buf, sizeof(buf), fp) == NULL) {
159 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
164 tmp_mac = (gchar *)malloc(WIFI_MAC_ADD_LENGTH + 1);
165 if (tmp_mac == NULL) {
166 ERR("malloc() failed");
171 memset(tmp_mac, 0, WIFI_MAC_ADD_LENGTH + 1);
172 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
175 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
176 if (tmp_mac == NULL) {
177 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
182 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
184 while (tmp && tmp[i]) {
190 *mac_address = g_strdup(mac);
196 gboolean wifi_config_get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
198 gchar *mac_address = NULL;
199 gchar *g_name = NULL;
200 gboolean ret = FALSE;
202 ret = __get_mac_address(&mac_address);
203 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
204 ERR("Cannot get WIFI MAC address");
209 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
210 if (g_name == NULL) {
215 *group_name = g_strdup(g_name);
223 static gboolean __get_security_type(const gchar *config_id, gchar **type)
225 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
226 *type = g_strdup(WIFI_SECURITY_NONE);
227 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
228 *type = g_strdup(WIFI_SECURITY_WEP);
229 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
230 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
231 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
232 *type = g_strdup(WIFI_SECURITY_EAP);
241 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
243 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
244 if (*config_id == NULL) {
253 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
255 GKeyFile *keyfile = NULL;
258 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
260 keyfile = netconfig_keyfile_load(path);
262 ERR("keyfile[%s] is NULL", path);
269 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
271 gboolean ret = FALSE;
274 path = g_strdup_printf("%s/%s", pathname, filename);
275 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
277 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
286 static gboolean __remove_configuration(const gchar *pathname)
290 if (__remove_file(pathname, "settings") != TRUE) {
291 ERR("Cannot remove [%s/settings]", pathname);
294 if (__remove_file(pathname, "data") != TRUE) {
295 ERR("Cannot remove [%s/data]", pathname);
299 ret = rmdir(pathname);
301 ERR("Cannot remove [%s]", pathname);
308 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
312 gboolean hidden = FALSE;
313 gboolean ret = FALSE;
315 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
317 ERR("Fail to get_wifi_config_group_name");
321 keyfile = __get_configuration_keyfile(group_name);
322 if (keyfile == NULL) {
323 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
328 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
329 DBG("name [%s]", config->name);
331 __get_security_type(config_id, &config->security_type);
332 if (config->security_type == NULL) {
333 ERR("Fail to _get_security_type");
334 g_key_file_free(keyfile);
338 DBG("security_type [%s]", config->security_type);
340 config->passphrase = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
341 DBG("passphrase []");
343 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
344 if (config->proxy_address)
345 DBG("proxy_address [%s]", config->proxy_address);
347 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
349 config->is_hidden = g_strdup("TRUE");
351 config->is_hidden = g_strdup("FALSE");
352 DBG("is_hidden [%s]", config->is_hidden);
354 if (config->ip_info) {
355 GError *error = NULL;
356 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
357 WIFI_CONFIG_IPV4_METHOD, NULL);
358 if (config->ip_info->ip_type)
359 DBG("IPv4.Method:%s", config->ip_info->ip_type);
361 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
362 WIFI_CONFIG_IPV4_ADDRESS, NULL);
363 if (config->ip_info->ip_address)
364 DBG("IPv4.Address:%s", config->ip_info->ip_address);
368 struct in_addr netmask;
370 prefix_len = g_key_file_get_integer(keyfile, group_name,
371 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
373 DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
376 if (prefix_len > 0 && prefix_len < 32) {
377 addr = 0xffffffff << (32 - prefix_len);
378 netmask.s_addr = htonl(addr);
379 mask = inet_ntoa(netmask);
380 config->ip_info->subnet_mask = g_strdup(mask);
382 if (config->ip_info->subnet_mask)
383 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
386 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
387 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
388 if (config->ip_info->gateway_address)
389 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
391 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
392 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
393 if (config->ip_info->dns_type)
394 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
398 nameservers = g_key_file_get_string_list(keyfile, group_name,
399 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
402 config->ip_info->dns_count = length;
404 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
405 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
406 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
410 g_strfreev(nameservers);
415 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
416 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
417 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
418 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
419 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
420 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
421 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
422 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
423 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
424 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
426 if (config->eap_config->anonymous_identity)
427 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
428 if (config->eap_config->ca_cert)
429 DBG("ca_cert [%s]", config->eap_config->ca_cert);
430 if (config->eap_config->client_cert)
431 DBG("client_cert [%s]", config->eap_config->client_cert);
432 if (config->eap_config->private_key)
433 DBG("private_key [%s]", config->eap_config->private_key);
434 if (config->eap_config->private_key_password)
435 DBG("private_key_password [%s]", config->eap_config->private_key_password);
436 if (config->eap_config->identity)
437 DBG("identity [%s]", config->eap_config->identity);
438 if (config->eap_config->eap_type)
439 DBG("eap_type [%s]", config->eap_config->eap_type);
440 if (config->eap_config->eap_auth_type)
441 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
442 if (config->eap_config->subject_match)
443 DBG("subject_match [%s]", config->eap_config->subject_match);
446 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
447 if (config->last_error)
448 DBG("last_error [%s]", config->last_error);
450 g_key_file_free(keyfile);
456 gboolean wifi_config_save_configuration(const gchar *config_id, GKeyFile *keyfile)
461 gboolean ret = FALSE;
463 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
465 ERR("Fail to get_wifi_config_group_name");
469 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
470 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
471 if (__remove_configuration(dir) != TRUE) {
472 ERR("[%s] is existed, but cannot remove", dir);
479 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
480 ERR("Cannot mkdir %s", dir);
486 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
487 netconfig_keyfile_save(keyfile, path);
495 static gboolean _remove_configuration(const gchar *config_id)
497 gboolean ret = FALSE;
501 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
503 ERR("Fail to get_wifi_config_group_name");
507 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
508 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
509 if (__remove_configuration(dir) != TRUE) {
510 ERR("[%s] is existed, but cannot remove", dir);
513 INFO("Success to remove [%s]", dir);
516 ERR("[%s] is not existed", dir);
527 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
533 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
535 ERR("Fail to get_wifi_config_group_name");
538 DBG("group_name %s", group_name);
540 keyfile = __get_configuration_keyfile(group_name);
541 if (keyfile == NULL) {
542 ERR("Fail to __get_configuration_keyfile");
547 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
548 g_key_file_set_string(keyfile, group_name, key, value);
549 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
550 g_key_file_set_string(keyfile, group_name, key, value);
551 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
552 gboolean hidden = FALSE;
553 if (g_strcmp0(value, "TRUE") == 0)
555 g_key_file_set_boolean(keyfile, group_name, key, hidden);
556 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
557 g_key_file_set_string(keyfile, group_name, key, value);
558 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
559 g_key_file_set_string(keyfile, group_name, key, value);
560 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
561 g_key_file_set_string(keyfile, group_name, key, value);
562 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
563 g_key_file_set_string(keyfile, group_name, key, value);
564 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
565 g_key_file_set_string(keyfile, group_name, key, value);
566 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
567 g_key_file_set_string(keyfile, group_name, key, value);
568 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
569 g_key_file_set_string(keyfile, group_name, key, value);
570 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
571 g_key_file_set_string(keyfile, group_name, key, value);
573 ERR("key[%s] is not supported", key);
577 wifi_config_save_configuration(config_id, keyfile);
579 g_key_file_free(keyfile);
585 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
590 gboolean hidden = FALSE;
591 gboolean ret = FALSE;
593 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
595 ERR("Fail to get_wifi_config_group_name");
598 DBG("group_name %s", group_name);
600 keyfile = __get_configuration_keyfile(group_name);
601 if (keyfile == NULL) {
602 ERR("Fail to __get_configuration_keyfile");
607 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
608 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
609 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
610 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
611 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
612 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
613 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
614 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
616 val = g_strdup("TRUE");
618 val = g_strdup("FALSE");
619 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
620 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
621 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
622 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
623 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
624 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
625 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
626 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
627 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
628 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
629 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
630 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
631 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
632 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
633 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
634 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
635 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
636 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
638 ERR("Invalid key[%s]", key);
639 val = g_strdup("NOTSUPPORTED");
642 *value = g_strdup(val);
645 g_key_file_free(keyfile);
651 static GSList *_get_list(void)
654 struct dirent *dp = NULL;
657 dir = opendir(CONNMAN_STORAGE);
659 ERR("Cannot open dir %s", CONNMAN_STORAGE);
663 while ((dp = readdir(dir)) != NULL) {
664 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
665 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
668 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
669 list = g_slist_append(list, g_strdup(config_id));
677 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
679 gboolean ret = FALSE;
682 if ((service_profile == NULL) || (config_id == NULL)) {
683 ERR("Invalid parameter");
687 ret = __get_config_id(service_profile, &val);
688 *config_id = g_strdup(val);
694 gboolean wifi_config_remove_configuration(const gchar *config_id)
696 gboolean ret = FALSE;
698 ret = _remove_configuration(config_id);
703 int __netconfig_hex_char_to_num(char c)
705 if (c >= '0' && c <= '9')
708 if (c >= 'a' && c <= 'f')
711 if (c >= 'A' && c <= 'F')
717 int __netconfig_hex_to_byte(const char *hex)
721 a = __netconfig_hex_char_to_num(*hex++);
725 b = __netconfig_hex_char_to_num(*hex++);
732 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
736 const char *ipos = hex;
737 unsigned char *opos = buf;
739 for (i = 0; i < len; i++) {
740 a = __netconfig_hex_to_byte(ipos);
751 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
758 ERR("Invalid parameter.");
762 *dst = (char *) g_try_malloc0((2*src_len)+1);
764 ERR("failed to allocate memory to buffer.");
770 for (i = 0; i < src_len; i++) {
771 snprintf(buf, 3, "%02x", src[i]);
779 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
781 GVariantIter *iter_copy = NULL;
784 unsigned char *tmp_dst = NULL;
786 if (!dst || *dst || !iter) {
787 ERR("Invalid parameter");
791 iter_copy = g_variant_iter_copy(iter);
793 while (g_variant_iter_loop(iter, "y", &tmp))
795 g_variant_iter_free(iter);
797 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
799 ERR("failed to allocate memory");
800 g_variant_iter_free(iter_copy);
805 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
807 g_variant_iter_free(iter_copy);
813 tmp_dst[length] = '\0';
817 DBG("Length [%d]", length);
821 gboolean _add_vsie(int frame_id, const char* vsie)
823 GVariant *params = NULL;
824 GVariant *message = NULL;
825 GVariantBuilder *bytearray_builder = NULL;
830 unsigned char *bytearray = NULL;
831 size_t bytearray_len = 0;
833 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
834 DBG("Invalid parameter, frame-id: %d", frame_id);
838 vsie_len = strlen(vsie);
840 DBG("vsie length is zero");
844 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
846 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
847 if (bytearray == NULL) {
848 DBG("Failed to allocate memory to bytearray");
852 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
853 DBG("invalid vsie string");
858 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
859 for (i = 0; i < bytearray_len; i++)
860 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
862 params = g_variant_new("(iay)", frame_id, bytearray_builder);
863 g_variant_builder_unref(bytearray_builder);
865 if_path = netconfig_wifi_get_supplicant_interface();
867 if (if_path == NULL) {
868 ERR("Fail to get wpa_supplicant DBus path");
873 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
874 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
877 if (message == NULL) {
878 ERR("Failed to send command to wpa_supplicant");
883 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
889 gboolean _get_vsie(int frame_id, char **vsie)
891 GVariant *params = NULL;
892 GVariant *message = NULL;
895 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
896 DBG("Invalid parameter, frame-id: %d", frame_id);
900 if_path = netconfig_wifi_get_supplicant_interface();
901 if (if_path == NULL) {
902 ERR("Fail to get wpa_supplicant DBus path");
906 params = g_variant_new("(i)", frame_id);
908 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
909 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
912 if (message == NULL) {
913 ERR("Failed to send command to wpa_supplicant");
916 GVariantIter *iter = NULL;
917 unsigned char *vsie_bytes = NULL;
921 g_variant_get(message, "(ay)", &iter);
923 ERR("vsie is not present");
927 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
928 if (vsie_bytes == NULL) {
929 ERR("vsie_bytes not allocated");
933 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
936 ERR("vsie not allocated.");
943 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
949 gboolean _remove_vsie(int frame_id, const char *vsie)
951 GVariant *params = NULL;
952 GVariant *message = NULL;
953 GVariantBuilder *bytearray_builder = NULL;
958 unsigned char *bytearray = NULL;
959 size_t bytearray_len = 0;
961 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
962 DBG("Invalid parameter, frame-id: %d", frame_id);
966 vsie_len = strlen(vsie);
968 DBG("vsie length is zero");
972 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
974 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
975 if (bytearray == NULL) {
976 DBG("Failed to allocate memory to bytearray");
980 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
981 DBG("invalid vsie string");
986 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
987 for (i = 0; i < bytearray_len; i++)
988 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
990 params = g_variant_new("(iay)", frame_id, bytearray_builder);
991 g_variant_builder_unref(bytearray_builder);
993 if_path = netconfig_wifi_get_supplicant_interface();
994 if (if_path == NULL) {
995 ERR("Fail to get wpa_supplicant DBus path");
1000 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
1001 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
1004 if (message == NULL) {
1005 ERR("Failed to send command to wpa_supplicant");
1010 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1017 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
1020 GSList *config_ids = NULL;
1022 gchar **result = NULL;
1024 g_return_val_if_fail(wifi != NULL, TRUE);
1026 config_ids = _get_list();
1027 if (config_ids == NULL) {
1028 ERR("Fail to get config list");
1029 netconfig_error_no_profile(context);
1033 length = g_slist_length(config_ids);
1034 result = g_new0(gchar *, length + 1);
1035 for (i = 0; i < length; i++) {
1036 gchar *config_id = g_slist_nth_data(config_ids, i);
1037 result[i] = g_strdup(config_id);
1040 config_ids = g_slist_nth(config_ids, 0);
1041 g_slist_free_full(config_ids, g_free);
1043 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1045 for (i = 0; i < length; i++)
1055 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1056 const gchar *config_id)
1058 gboolean ret = FALSE;
1059 GVariantBuilder *b = NULL;
1060 struct wifi_config *conf = NULL;
1062 g_return_val_if_fail(wifi != NULL, TRUE);
1064 conf = g_new0(struct wifi_config, 1);
1065 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1067 ret = _load_configuration(config_id, conf);
1069 g_free(conf->ip_info);
1071 ERR("Fail to _load_configuration");
1072 netconfig_error_no_profile(context);
1076 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1077 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1078 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1079 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PASSPHRASE, g_variant_new_string(conf->passphrase));
1080 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1082 if (conf->proxy_address != NULL)
1083 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1085 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1087 if (conf->ip_info->ip_type != NULL)
1088 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1090 if (conf->ip_info->ip_address != NULL)
1091 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1093 if (conf->ip_info->subnet_mask != NULL)
1094 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1096 if (conf->ip_info->prefix_length > 0)
1097 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV6_PREFIX_LEN, g_variant_new_int32(conf->ip_info->prefix_length));
1099 if (conf->ip_info->gateway_address != NULL)
1100 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1102 if (conf->ip_info->dns_type != NULL)
1103 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1105 int i = 0, count = conf->ip_info->dns_count;
1107 if (conf->ip_info->dns_address[i] != NULL)
1108 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1113 if (conf->last_error != NULL)
1114 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1116 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1118 __free_wifi_configuration(conf);
1120 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1121 g_variant_builder_unref(b);
1125 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1126 const char *netmask)
1135 mask = inet_network(netmask);
1138 /* a valid netmask must be 2^n - 1 */
1139 if ((host & (host + 1)) != 0)
1143 for (; mask; mask <<= 1)
1149 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1150 const gchar *config_id, GVariant *configuration)
1152 gboolean ret = FALSE;
1153 struct wifi_config *conf = NULL;
1154 GKeyFile *keyfile = NULL;
1158 gchar *group_name = NULL;
1161 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1162 ERR("Invalid parameter");
1163 netconfig_error_invalid_parameter(context);
1167 conf = g_new0(struct wifi_config, 1);
1168 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1170 g_variant_get(configuration, "a{sv}", &iter);
1171 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1172 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1173 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1174 conf->name = g_strdup(g_variant_get_string(value, NULL));
1175 DBG("name [%s]", conf->name);
1179 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1180 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1181 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1182 DBG("ssid [%s]", conf->ssid);
1186 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1187 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1188 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1189 DBG("passphrase []");
1191 conf->passphrase = NULL;
1193 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1194 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1195 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1196 DBG("is_hidden [%s]", conf->is_hidden);
1198 conf->is_hidden = NULL;
1200 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1201 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1202 conf->is_created = g_variant_get_boolean(value);
1203 DBG("is_created [%d]", conf->is_created);
1205 conf->is_created = FALSE;
1207 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1208 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1209 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1210 DBG("IP config type [%s]", conf->ip_info->ip_type);
1212 conf->ip_info->ip_type = NULL;
1214 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1215 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1216 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1217 DBG("IP address [%s]", conf->ip_info->ip_address);
1219 conf->ip_info->ip_address = NULL;
1221 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1222 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1223 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1224 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1226 conf->ip_info->subnet_mask = NULL;
1228 } else if (g_strcmp0(field, WIFI_CONFIG_IPV6_PREFIX_LEN) == 0) {
1229 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1230 conf->ip_info->prefix_length = g_variant_get_int32(value);
1231 DBG("IPv6 Prefix Length [%d]", conf->ip_info->prefix_length);
1233 conf->ip_info->prefix_length = 0;
1235 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1236 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1237 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1238 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1240 conf->ip_info->gateway_address = NULL;
1242 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1243 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1244 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1245 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1247 conf->ip_info->dns_type = NULL;
1249 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1250 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1251 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1252 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1253 conf->ip_info->dns_count = order + 1;
1256 conf->ip_info->dns_address[order++] = NULL;
1258 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1259 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1260 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1261 DBG("proxy_address [%s]", conf->proxy_address);
1263 conf->proxy_address = NULL;
1267 conf->favorite = TRUE;
1268 conf->autoconnect = TRUE;
1270 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1272 __free_wifi_configuration(conf);
1273 ERR("Fail to get_wifi_config_group_name");
1274 netconfig_error_fail_save_congifuration(context);
1278 keyfile = g_key_file_new();
1279 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1280 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1282 if (conf->passphrase != NULL) {
1283 gchar *enc_data = NULL;
1285 if (conf->is_created == true)
1286 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1288 enc_data = g_strdup(conf->passphrase);
1291 ERR("Failed to encrypt the passphrase");
1293 g_free(conf->passphrase);
1294 conf->passphrase = enc_data;
1297 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1300 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1301 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1303 /* Optional field */
1304 if (conf->proxy_address != NULL) {
1305 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1306 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1309 if (conf->is_hidden != NULL) {
1310 gboolean hidden = FALSE;
1311 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1313 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1316 if (conf->ip_info->ip_type != NULL)
1317 g_key_file_set_string(keyfile, group_name,
1318 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1320 if (conf->ip_info->ip_address != NULL)
1321 g_key_file_set_string(keyfile, group_name,
1322 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1324 if (conf->ip_info->subnet_mask != NULL) {
1325 unsigned char prefix_len;
1326 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1327 conf->ip_info->subnet_mask);
1328 if (prefix_len > 0 && prefix_len < 32)
1329 g_key_file_set_integer(keyfile, group_name,
1330 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1333 if (conf->ip_info->prefix_length > 0)
1334 g_key_file_set_integer(keyfile, group_name,
1335 WIFI_CONFIG_IPV6_PREFIX_LEN, conf->ip_info->prefix_length);
1337 if (conf->ip_info->gateway_address != NULL)
1338 g_key_file_set_string(keyfile, group_name,
1339 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1341 if (conf->ip_info->dns_type != NULL)
1342 g_key_file_set_string(keyfile, group_name,
1343 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1345 int i = 0, count = conf->ip_info->dns_count;
1347 if (conf->ip_info->dns_address[i] != NULL)
1348 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1349 conf->ip_info->dns_address[i]);
1354 ret = wifi_config_save_configuration(config_id, keyfile);
1356 INFO("Success to save configuration [%s]", config_id);
1357 wifi_complete_save_configuration(wifi, context);
1359 INFO("Fail to save configuration [%s]", config_id);
1360 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1363 g_key_file_free(keyfile);
1365 __free_wifi_configuration(conf);
1367 g_variant_iter_free(iter);
1372 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1373 const gchar *config_id)
1375 gboolean ret = FALSE;
1376 GVariantBuilder *b = NULL;
1377 struct wifi_config *conf = NULL;
1379 g_return_val_if_fail(wifi != NULL, TRUE);
1381 conf = g_new0(struct wifi_config, 1);
1382 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1383 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1385 ret = _load_configuration(config_id, conf);
1387 g_free(conf->eap_config);
1388 g_free(conf->ip_info);
1390 ERR("Fail to _load_configuration");
1391 netconfig_error_no_profile(context);
1395 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1396 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1397 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1398 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1399 if (conf->proxy_address != NULL)
1400 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1402 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1404 if (conf->last_error != NULL)
1405 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1407 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1409 if (conf->eap_config != NULL) {
1410 if (conf->eap_config->anonymous_identity != NULL)
1411 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1413 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1415 if (conf->eap_config->ca_cert != NULL)
1416 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1418 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1420 if (conf->eap_config->client_cert != NULL)
1421 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1423 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1425 if (conf->eap_config->private_key != NULL)
1426 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1428 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1430 if (conf->eap_config->private_key_password != NULL)
1431 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1433 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1435 if (conf->eap_config->identity != NULL)
1436 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1438 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1440 if (conf->eap_config->eap_type != NULL)
1441 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1443 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1445 if (conf->eap_config->eap_auth_type != NULL)
1446 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1448 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1450 if (conf->eap_config->subject_match != NULL)
1451 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1453 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1456 __free_wifi_configuration(conf);
1458 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1459 g_variant_builder_unref(b);
1463 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1464 const gchar *config_id, GVariant *configuration)
1466 gboolean ret = FALSE;
1467 struct wifi_config *conf = NULL;
1468 GKeyFile *keyfile = NULL;
1472 gchar *group_name = NULL;
1474 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1475 ERR("Invalid parameter");
1476 netconfig_error_invalid_parameter(context);
1480 conf = g_new0(struct wifi_config, 1);
1481 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1483 g_variant_get(configuration, "a{sv}", &iter);
1484 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1485 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1486 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1487 conf->name = g_strdup(g_variant_get_string(value, NULL));
1488 DBG("name [%s]", conf->name);
1492 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1493 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1494 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1495 DBG("ssid [%s]", conf->ssid);
1499 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1500 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1501 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1502 DBG("passphrase [%s]", conf->passphrase);
1504 conf->passphrase = NULL;
1506 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1507 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1508 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1509 DBG("is_hidden [%s]", conf->is_hidden);
1511 conf->is_hidden = NULL;
1513 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1514 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1515 conf->is_created = g_variant_get_boolean(value);
1516 DBG("is_created [%d]", conf->is_created);
1518 conf->is_created = FALSE;
1520 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1521 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1522 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1523 DBG("proxy_address [%s]", conf->proxy_address);
1525 conf->proxy_address = NULL;
1527 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1528 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1529 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1530 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1532 conf->eap_config->anonymous_identity = NULL;
1534 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1535 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1536 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1537 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1539 conf->eap_config->ca_cert = NULL;
1541 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1542 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1543 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1544 DBG("client_cert [%s]", conf->eap_config->client_cert);
1546 conf->eap_config->client_cert = NULL;
1548 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1549 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1550 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1551 DBG("private_key [%s]", conf->eap_config->private_key);
1553 conf->eap_config->private_key = NULL;
1555 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1556 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1557 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1558 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1560 conf->eap_config->private_key_password = NULL;
1562 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1563 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1564 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1565 DBG("identity [%s]", conf->eap_config->identity);
1567 conf->eap_config->identity = NULL;
1569 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1570 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1571 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1572 DBG("eap_type [%s]", conf->eap_config->eap_type);
1574 conf->eap_config->eap_type = NULL;
1576 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1577 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1578 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1579 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1581 conf->eap_config->eap_auth_type = NULL;
1583 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1584 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1585 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1586 DBG("subject_match [%s]", conf->eap_config->subject_match);
1588 conf->eap_config->subject_match = NULL;
1592 conf->favorite = TRUE;
1593 conf->autoconnect = TRUE;
1595 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1597 __free_wifi_configuration(conf);
1598 ERR("Fail to get_wifi_config_group_name");
1602 keyfile = g_key_file_new();
1603 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1604 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1606 if (conf->passphrase != NULL) {
1607 gchar *enc_data = NULL;
1609 if (conf->is_created == true)
1610 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1612 enc_data = g_strdup(conf->passphrase);
1615 ERR("Failed to encrypt the passphrase");
1617 g_free(conf->passphrase);
1618 conf->passphrase = enc_data;
1620 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1623 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1624 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1626 /* Optional field */
1627 if (conf->proxy_address != NULL) {
1628 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1629 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1632 if (conf->is_hidden != NULL) {
1633 gboolean hidden = FALSE;
1634 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1636 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1639 if (conf->eap_config->anonymous_identity != NULL)
1640 g_key_file_set_string(keyfile, group_name,
1641 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1643 if (conf->eap_config->ca_cert != NULL)
1644 g_key_file_set_string(keyfile, group_name,
1645 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1647 if (conf->eap_config->client_cert != NULL)
1648 g_key_file_set_string(keyfile, group_name,
1649 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1651 if (conf->eap_config->private_key != NULL)
1652 g_key_file_set_string(keyfile, group_name,
1653 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1655 if (conf->eap_config->private_key_password != NULL)
1656 g_key_file_set_string(keyfile, group_name,
1657 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1659 if (conf->eap_config->identity != NULL)
1660 g_key_file_set_string(keyfile, group_name,
1661 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1663 if (conf->eap_config->eap_type != NULL)
1664 g_key_file_set_string(keyfile, group_name,
1665 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1667 if (conf->eap_config->eap_auth_type != NULL)
1668 g_key_file_set_string(keyfile, group_name,
1669 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1671 if (conf->eap_config->subject_match != NULL)
1672 g_key_file_set_string(keyfile, group_name,
1673 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1675 ret = wifi_config_save_configuration(config_id, keyfile);
1677 INFO("Success to save eap configuration [%s]", config_id);
1678 wifi_complete_save_eap_configuration(wifi, context);
1680 INFO("Fail to save eap configuration [%s]", config_id);
1681 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1684 g_key_file_free(keyfile);
1686 __free_wifi_configuration(conf);
1688 g_variant_iter_free(iter);
1693 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1695 gboolean ret = FALSE;
1697 if ((wifi == NULL) || (config_id == NULL)) {
1698 ERR("Invalid parameter");
1699 netconfig_error_invalid_parameter(context);
1703 ret = _remove_configuration(config_id);
1705 /* no configuration or error */
1706 ERR("No [%s] configuration", config_id);
1707 netconfig_error_no_profile(context);
1711 wifi_complete_remove_configuration(wifi, context);
1715 /* config field key / value */
1717 * [wifi_macaddress_config_id]
1718 * Name=name (mandatory)
1719 * SSID=SSID (mandatory)
1720 * Frequency=2462 (X)
1722 * AutoConnect=true (Default true)
1723 * Modified=2015-03-20 (X)
1724 * IPv4.method=manual (O)
1725 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1726 * IPv6.method=auto (X)
1727 * IPv6.privacy=disabled (X)
1728 * IPv4.netmask_prefixlen=24 (X)
1729 * IPv4.local_address=192.0.0.1 (O)
1730 * IPv4.gateway=192.0.0.1 (O ? X ?)
1731 * Nameservers=192.168.43.22; (O)
1732 * Proxy.Method=manual (O)
1733 * Proxy.Servers=trst.com:8888; (O)
1735 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1736 const gchar *config_id, const gchar *key, const gchar *value)
1738 gboolean ret = FALSE;
1739 gchar *keyfile_key = NULL;
1741 g_return_val_if_fail(wifi != NULL, TRUE);
1742 g_return_val_if_fail(config_id != NULL, TRUE);
1743 g_return_val_if_fail(key != NULL, TRUE);
1745 DBG("Key[%s] Value[%s]", key, value);
1747 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1748 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1750 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1751 netconfig_error_invalid_parameter(context);
1754 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1755 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1756 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1757 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1758 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1759 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1760 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1761 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1762 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1763 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1764 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1765 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1766 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1767 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1768 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1769 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1770 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1771 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1772 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1774 ERR("Not supported key[%s]", key);
1775 netconfig_error_invalid_parameter(context);
1779 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1781 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1784 if (keyfile_key != NULL)
1785 g_free(keyfile_key);
1787 wifi_complete_set_config_field(wifi, context);
1791 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1793 gboolean ret = FALSE;
1794 gchar *passphrase = NULL;
1796 if ((wifi == NULL) || (config_id == NULL)) {
1797 ERR("Invalid parameter");
1798 netconfig_error_invalid_parameter(context);
1802 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1804 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1805 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1809 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1815 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1816 int frame_id, const gchar *vsie)
1818 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1820 g_return_val_if_fail(wifi != NULL, TRUE);
1821 g_return_val_if_fail(vsie != NULL, TRUE);
1823 gboolean ret = FALSE;
1825 ret = _add_vsie(frame_id, vsie);
1827 DBG("Failed to add vsie: %s", vsie);
1828 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1832 wifi_complete_add_vsie(wifi, context);
1836 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1839 DBG("Frame ID: [%d]", frame_id);
1841 g_return_val_if_fail(wifi != NULL, TRUE);
1843 gboolean ret = FALSE;
1846 ret = _get_vsie(frame_id, &vsie);
1848 DBG("Failed to get vsie for frame:[%d]", frame_id);
1849 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1853 DBG("Received vsie: %s", vsie);
1854 wifi_complete_get_vsie(wifi, context, vsie);
1859 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1860 int frame_id, const gchar *vsie)
1862 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1864 g_return_val_if_fail(wifi != NULL, TRUE);
1865 g_return_val_if_fail(vsie != NULL, TRUE);
1867 gboolean ret = FALSE;
1869 ret = _remove_vsie(frame_id, vsie);
1871 DBG("Failed to remove vsie: %s", vsie);
1872 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1876 wifi_complete_remove_vsie(wifi, context);