2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
37 #include "wifi-config.h"
38 #include "netsupplicant.h"
39 #include "wifi-key-encryption.h"
41 #define CONNMAN_STORAGE "/var/lib/connman"
43 #define WIFI_SECURITY_NONE "none"
44 #define WIFI_SECURITY_WEP "wep"
45 #define WIFI_SECURITY_WPA_PSK "psk"
46 #define WIFI_SECURITY_EAP "ieee8021x"
48 #define WIFI_CONFIG_PREFIX "wifi_"
49 #define MAC_ADDRESS_LENGTH 12
50 #define WIFI_PREFIX_LENGTH MAC_ADDRESS_LENGTH + 6 /* wifi_485a3f2f506a_ */
51 #define PROFILE_PREFIX_LENGTH WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
53 #define WIFI_MAC_ADD_LENGTH 17
54 #define WIFI_MAC_ADD_PATH "/sys/class/net/wlan0/address"
56 #define NET_DNS_ADDR_MAX 2
58 struct wifi_eap_config {
59 gchar *anonymous_identity;
63 gchar *private_key_password;
73 gchar *gateway_address;
74 gchar *dns_address[NET_DNS_ADDR_MAX];
90 struct wifi_eap_config *eap_config;
91 wifi_ip_info_s *ip_info;
95 static void __free_wifi_configuration(struct wifi_config *conf)
102 g_free(conf->passphrase);
103 g_free(conf->security_type);
104 g_free(conf->is_hidden);
105 g_free(conf->proxy_address);
106 g_free(conf->last_error);
107 if (conf->eap_config) {
108 g_free(conf->eap_config->anonymous_identity);
109 g_free(conf->eap_config->ca_cert);
110 g_free(conf->eap_config->client_cert);
111 g_free(conf->eap_config->private_key);
112 g_free(conf->eap_config->private_key_password);
113 g_free(conf->eap_config->identity);
114 g_free(conf->eap_config->eap_type);
115 g_free(conf->eap_config->eap_auth_type);
116 g_free(conf->eap_config->subject_match);
117 g_free(conf->eap_config);
121 g_free(conf->ip_info->ip_type);
122 g_free(conf->ip_info->ip_address);
123 g_free(conf->ip_info->subnet_mask);
124 g_free(conf->ip_info->gateway_address);
125 g_free(conf->ip_info->dns_type);
127 int i = 0, count = conf->ip_info->dns_count;
129 g_free(conf->ip_info->dns_address[i]);
132 g_free(conf->ip_info);
137 static gboolean __get_mac_address(gchar **mac_address)
139 gchar *tmp_mac = NULL;
141 gchar mac[13] = { 0, };
146 char buf[WIFI_MAC_ADD_LENGTH + 1];
147 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
148 fp = fopen(WIFI_MAC_ADD_PATH, "r");
151 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
156 if (fgets(buf, sizeof(buf), fp) == NULL) {
157 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
162 tmp_mac = (gchar *)malloc(WIFI_MAC_ADD_LENGTH + 1);
163 if (tmp_mac == NULL) {
164 ERR("malloc() failed");
169 memset(tmp_mac, 0, WIFI_MAC_ADD_LENGTH + 1);
170 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
173 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
174 if (tmp_mac == NULL) {
175 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
180 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
182 while (tmp && tmp[i]) {
188 *mac_address = g_strdup(mac);
194 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
196 gchar *mac_address = NULL;
197 gchar *g_name = NULL;
198 gboolean ret = FALSE;
200 ret = __get_mac_address(&mac_address);
201 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
202 ERR("Cannot get WIFI MAC address");
207 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
208 if (g_name == NULL) {
213 *group_name = g_strdup(g_name);
221 static gboolean __get_security_type(const gchar *config_id, gchar **type)
223 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
224 *type = g_strdup(WIFI_SECURITY_NONE);
225 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
226 *type = g_strdup(WIFI_SECURITY_WEP);
227 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
228 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
229 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
230 *type = g_strdup(WIFI_SECURITY_EAP);
239 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
241 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
242 if (*config_id == NULL) {
251 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
253 GKeyFile *keyfile = NULL;
256 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
258 keyfile = netconfig_keyfile_load(path);
260 ERR("keyfile[%s] is NULL", path);
267 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
269 gboolean ret = FALSE;
272 path = g_strdup_printf("%s/%s", pathname, filename);
273 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
275 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
284 static gboolean __remove_configuration(const gchar *pathname)
288 if (__remove_file(pathname, "settings") != TRUE) {
289 ERR("Cannot remove [%s/settings]", pathname);
292 if (__remove_file(pathname, "data") != TRUE) {
293 ERR("Cannot remove [%s/data]", pathname);
297 ret = rmdir(pathname);
299 ERR("Cannot remove [%s]", pathname);
306 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
310 gboolean hidden = FALSE;
311 gboolean ret = FALSE;
313 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
315 ERR("Fail to get_wifi_config_group_name");
319 keyfile = __get_configuration_keyfile(group_name);
320 if (keyfile == NULL) {
321 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
326 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
327 DBG("name [%s]", config->name);
329 __get_security_type(config_id, &config->security_type);
330 if (config->security_type == NULL) {
331 ERR("Fail to _get_security_type");
332 g_key_file_free(keyfile);
336 DBG("security_type [%s]", config->security_type);
338 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
339 if (config->proxy_address)
340 DBG("proxy_address [%s]", config->proxy_address);
342 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
344 config->is_hidden = g_strdup("TRUE");
346 config->is_hidden = g_strdup("FALSE");
347 DBG("is_hidden [%s]", config->is_hidden);
349 if (config->ip_info) {
350 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
351 WIFI_CONFIG_IPV4_METHOD, NULL);
352 if (config->ip_info->ip_type)
353 DBG("IPv4.Method:%s", config->ip_info->ip_type);
355 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
356 WIFI_CONFIG_IPV4_ADDRESS, NULL);
357 if (config->ip_info->ip_address)
358 DBG("IPv4.Address:%s", config->ip_info->ip_address);
362 struct in_addr netmask;
364 prefix_len = g_key_file_get_integer(keyfile, group_name,
365 WIFI_CONFIG_IPV4_SUBNET_MASK, NULL);
366 addr = 0xffffffff << (32 - prefix_len);
367 netmask.s_addr = htonl(addr);
368 mask = inet_ntoa(netmask);
369 config->ip_info->subnet_mask = g_strdup(mask);
370 if (config->ip_info->subnet_mask)
371 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
373 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
374 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
375 if (config->ip_info->gateway_address)
376 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
378 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
379 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
380 if (config->ip_info->dns_type)
381 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
385 nameservers = g_key_file_get_string_list(keyfile, group_name,
386 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
389 config->ip_info->dns_count = length;
391 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
392 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
393 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
397 g_strfreev(nameservers);
402 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
403 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
404 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
405 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
406 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
407 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
408 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
409 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
410 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
411 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
413 if (config->eap_config->anonymous_identity)
414 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
415 if (config->eap_config->ca_cert)
416 DBG("ca_cert [%s]", config->eap_config->ca_cert);
417 if (config->eap_config->client_cert)
418 DBG("client_cert [%s]", config->eap_config->client_cert);
419 if (config->eap_config->private_key)
420 DBG("private_key [%s]", config->eap_config->private_key);
421 if (config->eap_config->private_key_password)
422 DBG("private_key_password [%s]", config->eap_config->private_key_password);
423 if (config->eap_config->identity)
424 DBG("identity [%s]", config->eap_config->identity);
425 if (config->eap_config->eap_type)
426 DBG("eap_type [%s]", config->eap_config->eap_type);
427 if (config->eap_config->eap_auth_type)
428 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
429 if (config->eap_config->subject_match)
430 DBG("subject_match [%s]", config->eap_config->subject_match);
433 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
434 if (config->last_error)
435 DBG("last_error [%s]", config->last_error);
437 g_key_file_free(keyfile);
443 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
448 gboolean ret = FALSE;
450 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
452 ERR("Fail to get_wifi_config_group_name");
456 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
457 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
458 if (__remove_configuration(dir) != TRUE) {
459 ERR("[%s] is existed, but cannot remove", dir);
466 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
467 ERR("Cannot mkdir %s", dir);
473 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
474 netconfig_keyfile_save(keyfile, path);
482 static gboolean _remove_configuration(const gchar *config_id)
484 gboolean ret = FALSE;
488 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
490 ERR("Fail to get_wifi_config_group_name");
494 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
495 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
496 if (__remove_configuration(dir) != TRUE) {
497 ERR("[%s] is existed, but cannot remove", dir);
500 INFO("Success to remove [%s]", dir);
503 ERR("[%s] is not existed", dir);
514 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
520 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
522 ERR("Fail to get_wifi_config_group_name");
525 DBG("group_name %s", group_name);
527 keyfile = __get_configuration_keyfile(group_name);
528 if (keyfile == NULL) {
529 ERR("Fail to __get_configuration_keyfile");
534 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
535 g_key_file_set_string(keyfile, group_name, key, value);
536 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
537 g_key_file_set_string(keyfile, group_name, key, value);
538 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
539 gboolean hidden = FALSE;
540 if (g_strcmp0(value, "TRUE") == 0)
542 g_key_file_set_boolean(keyfile, group_name, key, hidden);
543 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
544 g_key_file_set_string(keyfile, group_name, key, value);
545 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
546 g_key_file_set_string(keyfile, group_name, key, value);
547 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
548 g_key_file_set_string(keyfile, group_name, key, value);
549 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
550 g_key_file_set_string(keyfile, group_name, key, value);
551 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
552 g_key_file_set_string(keyfile, group_name, key, value);
553 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
554 g_key_file_set_string(keyfile, group_name, key, value);
555 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
556 g_key_file_set_string(keyfile, group_name, key, value);
557 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
558 g_key_file_set_string(keyfile, group_name, key, value);
560 ERR("key[%s] is not supported", key);
564 _save_configuration(config_id, keyfile);
566 g_key_file_free(keyfile);
572 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
577 gboolean hidden = FALSE;
578 gboolean ret = FALSE;
580 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
582 ERR("Fail to get_wifi_config_group_name");
585 DBG("group_name %s", group_name);
587 keyfile = __get_configuration_keyfile(group_name);
588 if (keyfile == NULL) {
589 ERR("Fail to __get_configuration_keyfile");
594 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
595 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
596 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
597 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
598 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
599 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
600 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
601 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
603 val = g_strdup("TRUE");
605 val = g_strdup("FALSE");
606 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
607 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
608 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
609 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
610 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
611 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
612 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
613 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
614 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
615 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
616 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
617 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
618 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
619 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
620 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
621 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
622 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
623 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
625 ERR("Invalid key[%s]", key);
626 val = g_strdup("NOTSUPPORTED");
629 *value = g_strdup(val);
632 g_key_file_free(keyfile);
638 static GSList *_get_list(void)
641 struct dirent *dp = NULL;
644 dir = opendir(CONNMAN_STORAGE);
646 ERR("Cannot open dir %s", CONNMAN_STORAGE);
650 while ((dp = readdir(dir)) != NULL) {
651 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
652 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
655 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
656 list = g_slist_append(list, g_strdup(config_id));
664 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
666 gboolean ret = FALSE;
669 if ((service_profile == NULL) || (config_id == NULL)) {
670 ERR("Invalid parameter");
674 ret = __get_config_id(service_profile, &val);
675 *config_id = g_strdup(val);
681 gboolean wifi_config_remove_configuration(const gchar *config_id)
683 gboolean ret = FALSE;
685 ret = _remove_configuration(config_id);
690 int __netconfig_hex_char_to_num(char c)
692 if (c >= '0' && c <= '9')
695 if (c >= 'a' && c <= 'f')
698 if (c >= 'A' && c <= 'F')
704 int __netconfig_hex_to_byte(const char *hex)
708 a = __netconfig_hex_char_to_num(*hex++);
712 b = __netconfig_hex_char_to_num(*hex++);
719 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
723 const char *ipos = hex;
724 unsigned char *opos = buf;
726 for (i = 0; i < len; i++) {
727 a = __netconfig_hex_to_byte(ipos);
738 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
745 ERR("Invalid parameter.");
749 *dst = (char *) g_try_malloc0((2*src_len)+1);
751 ERR("failed to allocate memory to buffer.");
757 for (i = 0; i < src_len; i++) {
758 snprintf(buf, 3, "%02x", src[i]);
766 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
768 GVariantIter *iter_copy = NULL;
771 unsigned char *tmp_dst = NULL;
773 if (!dst || *dst || !iter) {
774 ERR("Invalid parameter");
778 iter_copy = g_variant_iter_copy(iter);
780 while (g_variant_iter_loop(iter, "y", &tmp))
782 g_variant_iter_free(iter);
784 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
786 ERR("failed to allocate memory");
787 g_variant_iter_free(iter_copy);
792 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
794 g_variant_iter_free(iter_copy);
800 tmp_dst[length] = '\0';
804 DBG("Length [%d]", length);
808 gboolean _add_vsie(int frame_id, const char* vsie)
810 GVariant *params = NULL;
811 GVariant *message = NULL;
812 GVariantBuilder *bytearray_builder = NULL;
817 unsigned char *bytearray = NULL;
818 size_t bytearray_len = 0;
820 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
821 DBG("Invalid parameter, frame-id: %d", frame_id);
825 vsie_len = strlen(vsie);
827 DBG("vsie length is zero");
831 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
833 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
834 if (bytearray == NULL) {
835 DBG("Failed to allocate memory to bytearray");
839 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
840 DBG("invalid vsie string");
845 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
846 for (i = 0; i < bytearray_len; i++)
847 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
849 params = g_variant_new("(iay)", frame_id, bytearray_builder);
850 g_variant_builder_unref(bytearray_builder);
852 if_path = netconfig_wifi_get_supplicant_interface();
854 if (if_path == NULL) {
855 ERR("Fail to get wpa_supplicant DBus path");
860 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
861 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
864 if (message == NULL) {
865 ERR("Failed to send command to wpa_supplicant");
870 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
876 gboolean _get_vsie(int frame_id, char **vsie)
878 GVariant *params = NULL;
879 GVariant *message = NULL;
882 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
883 DBG("Invalid parameter, frame-id: %d", frame_id);
887 if_path = netconfig_wifi_get_supplicant_interface();
888 if (if_path == NULL) {
889 ERR("Fail to get wpa_supplicant DBus path");
893 params = g_variant_new("(i)", frame_id);
895 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
896 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
899 if (message == NULL) {
900 ERR("Failed to send command to wpa_supplicant");
903 GVariantIter *iter = NULL;
904 unsigned char *vsie_bytes = NULL;
908 g_variant_get(message, "(ay)", &iter);
910 ERR("vsie is not present");
914 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
915 if (vsie_bytes == NULL) {
916 ERR("vsie_bytes not allocated");
920 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
923 ERR("vsie not allocated.");
930 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
936 gboolean _remove_vsie(int frame_id, const char *vsie)
938 GVariant *params = NULL;
939 GVariant *message = NULL;
940 GVariantBuilder *bytearray_builder = NULL;
945 unsigned char *bytearray = NULL;
946 size_t bytearray_len = 0;
948 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
949 DBG("Invalid parameter, frame-id: %d", frame_id);
953 vsie_len = strlen(vsie);
955 DBG("vsie length is zero");
959 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
961 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
962 if (bytearray == NULL) {
963 DBG("Failed to allocate memory to bytearray");
967 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
968 DBG("invalid vsie string");
973 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
974 for (i = 0; i < bytearray_len; i++)
975 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
977 params = g_variant_new("(iay)", frame_id, bytearray_builder);
978 g_variant_builder_unref(bytearray_builder);
980 if_path = netconfig_wifi_get_supplicant_interface();
981 if (if_path == NULL) {
982 ERR("Fail to get wpa_supplicant DBus path");
987 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
988 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
991 if (message == NULL) {
992 ERR("Failed to send command to wpa_supplicant");
997 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1004 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
1007 GSList *config_ids = NULL;
1009 gchar **result = NULL;
1011 g_return_val_if_fail(wifi != NULL, TRUE);
1013 config_ids = _get_list();
1014 if (config_ids == NULL) {
1015 ERR("Fail to get config list");
1016 netconfig_error_no_profile(context);
1020 length = g_slist_length(config_ids);
1021 result = g_new0(gchar *, length + 1);
1022 for (i = 0; i < length; i++) {
1023 gchar *config_id = g_slist_nth_data(config_ids, i);
1024 result[i] = g_strdup(config_id);
1027 config_ids = g_slist_nth(config_ids, 0);
1028 g_slist_free_full(config_ids, g_free);
1030 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1032 for (i = 0; i < length; i++)
1042 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1043 const gchar *config_id)
1045 gboolean ret = FALSE;
1046 GVariantBuilder *b = NULL;
1047 struct wifi_config *conf = NULL;
1049 g_return_val_if_fail(wifi != NULL, TRUE);
1051 conf = g_new0(struct wifi_config, 1);
1052 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1054 ret = _load_configuration(config_id, conf);
1056 g_free(conf->ip_info);
1058 ERR("Fail to _load_configuration");
1059 netconfig_error_no_profile(context);
1063 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1064 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1065 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1066 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1068 if (conf->proxy_address != NULL)
1069 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1071 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1073 if (conf->ip_info->ip_type != NULL)
1074 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1076 if (conf->ip_info->ip_address != NULL)
1077 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1079 if (conf->ip_info->subnet_mask != NULL)
1080 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1082 if (conf->ip_info->gateway_address != NULL)
1083 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1085 if (conf->ip_info->dns_type != NULL)
1086 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1088 int i = 0, count = conf->ip_info->dns_count;
1090 if (conf->ip_info->dns_address[i] != NULL)
1091 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1096 if (conf->last_error != NULL)
1097 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1099 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1101 __free_wifi_configuration(conf);
1103 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1104 g_variant_builder_unref(b);
1108 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1109 const char *netmask)
1118 mask = inet_network(netmask);
1121 /* a valid netmask must be 2^n - 1 */
1122 if ((host & (host + 1)) != 0)
1126 for (; mask; mask <<= 1)
1132 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1133 const gchar *config_id, GVariant *configuration)
1135 gboolean ret = FALSE;
1136 struct wifi_config *conf = NULL;
1137 GKeyFile *keyfile = NULL;
1141 gchar *group_name = NULL;
1144 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1145 ERR("Invalid parameter");
1146 netconfig_error_invalid_parameter(context);
1150 conf = g_new0(struct wifi_config, 1);
1151 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1153 g_variant_get(configuration, "a{sv}", &iter);
1154 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1155 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1156 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1157 conf->name = g_strdup(g_variant_get_string(value, NULL));
1158 DBG("name [%s]", conf->name);
1162 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1163 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1164 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1165 DBG("ssid [%s]", conf->ssid);
1169 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1170 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1171 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1172 DBG("passphrase []");
1174 conf->passphrase = NULL;
1176 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1177 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1178 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1179 DBG("is_hidden [%s]", conf->is_hidden);
1181 conf->is_hidden = NULL;
1183 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1184 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1185 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1186 DBG("IP config type [%s]", conf->ip_info->ip_type);
1188 conf->ip_info->ip_type = NULL;
1190 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1191 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1192 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1193 DBG("IP address [%s]", conf->ip_info->ip_address);
1195 conf->ip_info->ip_address = NULL;
1197 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1198 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1199 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1200 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1202 conf->ip_info->subnet_mask = NULL;
1204 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1205 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1206 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1207 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1209 conf->ip_info->gateway_address = NULL;
1211 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1212 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1213 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1214 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1216 conf->ip_info->dns_type = NULL;
1218 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1219 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1220 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1221 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1222 conf->ip_info->dns_count = order + 1;
1225 conf->ip_info->dns_address[order++] = NULL;
1227 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1228 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1229 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1230 DBG("proxy_address [%s]", conf->proxy_address);
1232 conf->proxy_address = NULL;
1236 conf->favorite = TRUE;
1237 conf->autoconnect = TRUE;
1239 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1241 __free_wifi_configuration(conf);
1242 ERR("Fail to get_wifi_config_group_name");
1243 netconfig_error_fail_save_congifuration(context);
1247 keyfile = g_key_file_new();
1248 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1249 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1251 if (conf->passphrase != NULL) {
1252 gchar *enc_data = NULL;
1253 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1256 ERR("Failed to encrypt the passphrase");
1258 g_free(conf->passphrase);
1259 conf->passphrase = enc_data;
1262 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1265 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1266 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1268 /* Optional field */
1269 if (conf->proxy_address != NULL) {
1270 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1271 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1274 if (conf->is_hidden != NULL) {
1275 gboolean hidden = FALSE;
1276 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1278 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1281 if (conf->ip_info->ip_type != NULL)
1282 g_key_file_set_string(keyfile, group_name,
1283 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1285 if (conf->ip_info->ip_address != NULL)
1286 g_key_file_set_string(keyfile, group_name,
1287 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1289 if (conf->ip_info->subnet_mask != NULL) {
1290 unsigned char prefix_len;
1291 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1292 conf->ip_info->subnet_mask);
1293 g_key_file_set_integer(keyfile, group_name,
1294 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1297 if (conf->ip_info->gateway_address != NULL)
1298 g_key_file_set_string(keyfile, group_name,
1299 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1301 if (conf->ip_info->dns_type != NULL)
1302 g_key_file_set_string(keyfile, group_name,
1303 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1305 int i = 0, count = conf->ip_info->dns_count;
1307 if (conf->ip_info->dns_address[i] != NULL)
1308 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1309 conf->ip_info->dns_address[i]);
1314 ret = _save_configuration(config_id, keyfile);
1316 INFO("Success to save configuration [%s]", config_id);
1317 wifi_complete_save_configuration(wifi, context);
1319 INFO("Fail to save configuration [%s]", config_id);
1320 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1323 g_key_file_free(keyfile);
1325 __free_wifi_configuration(conf);
1327 g_variant_iter_free(iter);
1332 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1333 const gchar *config_id)
1335 gboolean ret = FALSE;
1336 GVariantBuilder *b = NULL;
1337 struct wifi_config *conf = NULL;
1339 g_return_val_if_fail(wifi != NULL, TRUE);
1341 conf = g_new0(struct wifi_config, 1);
1342 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1343 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1345 ret = _load_configuration(config_id, conf);
1347 g_free(conf->eap_config);
1348 g_free(conf->ip_info);
1350 ERR("Fail to _load_configuration");
1351 netconfig_error_no_profile(context);
1355 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1356 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1357 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1358 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1359 if (conf->proxy_address != NULL)
1360 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1362 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1364 if (conf->last_error != NULL)
1365 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1367 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1369 if (conf->eap_config != NULL) {
1370 if (conf->eap_config->anonymous_identity != NULL)
1371 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1373 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1375 if (conf->eap_config->ca_cert != NULL)
1376 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1378 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1380 if (conf->eap_config->client_cert != NULL)
1381 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1383 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1385 if (conf->eap_config->private_key != NULL)
1386 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1388 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1390 if (conf->eap_config->private_key_password != NULL)
1391 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1393 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1395 if (conf->eap_config->identity != NULL)
1396 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1398 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1400 if (conf->eap_config->eap_type != NULL)
1401 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1403 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1405 if (conf->eap_config->eap_auth_type != NULL)
1406 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1408 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1410 if (conf->eap_config->subject_match != NULL)
1411 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1413 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1416 __free_wifi_configuration(conf);
1418 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1419 g_variant_builder_unref(b);
1423 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1424 const gchar *config_id, GVariant *configuration)
1426 gboolean ret = FALSE;
1427 struct wifi_config *conf = NULL;
1428 GKeyFile *keyfile = NULL;
1432 gchar *group_name = NULL;
1434 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1435 ERR("Invalid parameter");
1436 netconfig_error_invalid_parameter(context);
1440 conf = g_new0(struct wifi_config, 1);
1441 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1443 g_variant_get(configuration, "a{sv}", &iter);
1444 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1445 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1446 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1447 conf->name = g_strdup(g_variant_get_string(value, NULL));
1448 DBG("name [%s]", conf->name);
1452 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1453 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1454 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1455 DBG("ssid [%s]", conf->ssid);
1459 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1460 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1461 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1462 DBG("passphrase [%s]", conf->passphrase);
1464 conf->passphrase = NULL;
1466 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1467 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1468 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1469 DBG("is_hidden [%s]", conf->is_hidden);
1471 conf->is_hidden = NULL;
1473 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1474 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1475 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1476 DBG("proxy_address [%s]", conf->proxy_address);
1478 conf->proxy_address = NULL;
1480 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1481 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1482 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1483 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1485 conf->eap_config->anonymous_identity = NULL;
1487 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1488 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1489 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1490 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1492 conf->eap_config->ca_cert = NULL;
1494 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1495 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1496 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1497 DBG("client_cert [%s]", conf->eap_config->client_cert);
1499 conf->eap_config->client_cert = NULL;
1501 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1502 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1503 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1504 DBG("private_key [%s]", conf->eap_config->private_key);
1506 conf->eap_config->private_key = NULL;
1508 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1509 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1510 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1511 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1513 conf->eap_config->private_key_password = NULL;
1515 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1516 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1517 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1518 DBG("identity [%s]", conf->eap_config->identity);
1520 conf->eap_config->identity = NULL;
1522 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1523 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1524 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1525 DBG("eap_type [%s]", conf->eap_config->eap_type);
1527 conf->eap_config->eap_type = NULL;
1529 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1530 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1531 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1532 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1534 conf->eap_config->eap_auth_type = NULL;
1536 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1537 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1538 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1539 DBG("subject_match [%s]", conf->eap_config->subject_match);
1541 conf->eap_config->subject_match = NULL;
1545 conf->favorite = TRUE;
1546 conf->autoconnect = TRUE;
1548 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1550 __free_wifi_configuration(conf);
1551 ERR("Fail to get_wifi_config_group_name");
1555 keyfile = g_key_file_new();
1556 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1557 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1559 if (conf->passphrase != NULL) {
1560 gchar *enc_data = NULL;
1561 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1564 ERR("Failed to encrypt the passphrase");
1566 g_free(conf->passphrase);
1567 conf->passphrase = enc_data;
1569 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1572 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1573 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1575 /* Optional field */
1576 if (conf->proxy_address != NULL) {
1577 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1578 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1581 if (conf->is_hidden != NULL) {
1582 gboolean hidden = FALSE;
1583 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1585 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1588 if (conf->eap_config->anonymous_identity != NULL)
1589 g_key_file_set_string(keyfile, group_name,
1590 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1592 if (conf->eap_config->ca_cert != NULL)
1593 g_key_file_set_string(keyfile, group_name,
1594 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1596 if (conf->eap_config->client_cert != NULL)
1597 g_key_file_set_string(keyfile, group_name,
1598 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1600 if (conf->eap_config->private_key != NULL)
1601 g_key_file_set_string(keyfile, group_name,
1602 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1604 if (conf->eap_config->private_key_password != NULL)
1605 g_key_file_set_string(keyfile, group_name,
1606 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1608 if (conf->eap_config->identity != NULL)
1609 g_key_file_set_string(keyfile, group_name,
1610 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1612 if (conf->eap_config->eap_type != NULL)
1613 g_key_file_set_string(keyfile, group_name,
1614 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1616 if (conf->eap_config->eap_auth_type != NULL)
1617 g_key_file_set_string(keyfile, group_name,
1618 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1620 if (conf->eap_config->subject_match != NULL)
1621 g_key_file_set_string(keyfile, group_name,
1622 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1624 ret = _save_configuration(config_id, keyfile);
1626 INFO("Success to save eap configuration [%s]", config_id);
1627 wifi_complete_save_eap_configuration(wifi, context);
1629 INFO("Fail to save eap configuration [%s]", config_id);
1630 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1633 g_key_file_free(keyfile);
1635 __free_wifi_configuration(conf);
1637 g_variant_iter_free(iter);
1642 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1644 gboolean ret = FALSE;
1646 if ((wifi == NULL) || (config_id == NULL)) {
1647 ERR("Invalid parameter");
1648 netconfig_error_invalid_parameter(context);
1652 ret = _remove_configuration(config_id);
1654 /* no configuration or error */
1655 ERR("No [%s] configuration", config_id);
1656 netconfig_error_no_profile(context);
1660 wifi_complete_remove_configuration(wifi, context);
1664 /* config field key / value */
1666 * [wifi_macaddress_config_id]
1667 * Name=name (mandatory)
1668 * SSID=SSID (mandatory)
1669 * Frequency=2462 (X)
1671 * AutoConnect=true (Default true)
1672 * Modified=2015-03-20 (X)
1673 * IPv4.method=manual (O)
1674 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1675 * IPv6.method=auto (X)
1676 * IPv6.privacy=disabled (X)
1677 * IPv4.netmask_prefixlen=24 (X)
1678 * IPv4.local_address=192.0.0.1 (O)
1679 * IPv4.gateway=192.0.0.1 (O ? X ?)
1680 * Nameservers=192.168.43.22; (O)
1681 * Proxy.Method=manual (O)
1682 * Proxy.Servers=trst.com:8888; (O)
1684 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1685 const gchar *config_id, const gchar *key, const gchar *value)
1687 gboolean ret = FALSE;
1688 gchar *keyfile_key = NULL;
1690 g_return_val_if_fail(wifi != NULL, TRUE);
1691 g_return_val_if_fail(config_id != NULL, TRUE);
1692 g_return_val_if_fail(key != NULL, TRUE);
1694 DBG("Key[%s] Value[%d]", key, value);
1696 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1697 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1699 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1700 netconfig_error_invalid_parameter(context);
1703 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1704 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1705 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1706 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1707 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1708 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1709 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1710 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1711 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1712 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1713 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1714 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1715 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1716 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1717 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1718 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1719 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1720 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1721 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1723 ERR("Not supported key[%s]", key);
1724 netconfig_error_invalid_parameter(context);
1728 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1730 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1733 if (keyfile_key != NULL)
1734 g_free(keyfile_key);
1736 wifi_complete_set_config_field(wifi, context);
1740 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1742 gboolean ret = FALSE;
1743 gchar *passphrase = NULL;
1745 if ((wifi == NULL) || (config_id == NULL)) {
1746 ERR("Invalid parameter");
1747 netconfig_error_invalid_parameter(context);
1751 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1753 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1754 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1758 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1764 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1765 int frame_id, const gchar *vsie)
1767 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1769 g_return_val_if_fail(wifi != NULL, TRUE);
1770 g_return_val_if_fail(vsie != NULL, TRUE);
1772 gboolean ret = FALSE;
1774 ret = _add_vsie(frame_id, vsie);
1776 DBG("Failed to add vsie: %s", vsie);
1777 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1781 wifi_complete_add_vsie(wifi, context);
1785 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1788 DBG("Frame ID: [%d]", frame_id);
1790 g_return_val_if_fail(wifi != NULL, TRUE);
1792 gboolean ret = FALSE;
1795 ret = _get_vsie(frame_id, &vsie);
1797 DBG("Failed to get vsie for frame:[%d]", frame_id);
1798 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1802 DBG("Received vsie: %s", vsie);
1803 wifi_complete_get_vsie(wifi, context, vsie);
1808 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1809 int frame_id, const gchar *vsie)
1811 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1813 g_return_val_if_fail(wifi != NULL, TRUE);
1814 g_return_val_if_fail(vsie != NULL, TRUE);
1816 gboolean ret = FALSE;
1818 ret = _remove_vsie(frame_id, vsie);
1820 DBG("Failed to remove vsie: %s", vsie);
1821 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1825 wifi_complete_remove_vsie(wifi, context);