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);
126 conf->ip_info->prefix_length = 0;
128 int i = 0, count = conf->ip_info->dns_count;
130 g_free(conf->ip_info->dns_address[i]);
133 g_free(conf->ip_info);
138 static gboolean __get_mac_address(gchar **mac_address)
140 gchar *tmp_mac = NULL;
142 gchar mac[13] = { 0, };
147 char buf[WIFI_MAC_ADD_LENGTH + 1];
148 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
149 fp = fopen(WIFI_MAC_ADD_PATH, "r");
152 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
157 if (fgets(buf, sizeof(buf), fp) == NULL) {
158 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
163 tmp_mac = (gchar *)malloc(WIFI_MAC_ADD_LENGTH + 1);
164 if (tmp_mac == NULL) {
165 ERR("malloc() failed");
170 memset(tmp_mac, 0, WIFI_MAC_ADD_LENGTH + 1);
171 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
174 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
175 if (tmp_mac == NULL) {
176 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
181 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
183 while (tmp && tmp[i]) {
189 *mac_address = g_strdup(mac);
195 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
197 gchar *mac_address = NULL;
198 gchar *g_name = NULL;
199 gboolean ret = FALSE;
201 ret = __get_mac_address(&mac_address);
202 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
203 ERR("Cannot get WIFI MAC address");
208 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
209 if (g_name == NULL) {
214 *group_name = g_strdup(g_name);
222 static gboolean __get_security_type(const gchar *config_id, gchar **type)
224 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
225 *type = g_strdup(WIFI_SECURITY_NONE);
226 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
227 *type = g_strdup(WIFI_SECURITY_WEP);
228 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
229 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
230 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
231 *type = g_strdup(WIFI_SECURITY_EAP);
240 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
242 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
243 if (*config_id == NULL) {
252 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
254 GKeyFile *keyfile = NULL;
257 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
259 keyfile = netconfig_keyfile_load(path);
261 ERR("keyfile[%s] is NULL", path);
268 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
270 gboolean ret = FALSE;
273 path = g_strdup_printf("%s/%s", pathname, filename);
274 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
276 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
285 static gboolean __remove_configuration(const gchar *pathname)
289 if (__remove_file(pathname, "settings") != TRUE) {
290 ERR("Cannot remove [%s/settings]", pathname);
293 if (__remove_file(pathname, "data") != TRUE) {
294 ERR("Cannot remove [%s/data]", pathname);
298 ret = rmdir(pathname);
300 ERR("Cannot remove [%s]", pathname);
307 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
311 gboolean hidden = FALSE;
312 gboolean ret = FALSE;
314 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
316 ERR("Fail to get_wifi_config_group_name");
320 keyfile = __get_configuration_keyfile(group_name);
321 if (keyfile == NULL) {
322 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
327 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
328 DBG("name [%s]", config->name);
330 __get_security_type(config_id, &config->security_type);
331 if (config->security_type == NULL) {
332 ERR("Fail to _get_security_type");
333 g_key_file_free(keyfile);
337 DBG("security_type [%s]", config->security_type);
339 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
340 if (config->proxy_address)
341 DBG("proxy_address [%s]", config->proxy_address);
343 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
345 config->is_hidden = g_strdup("TRUE");
347 config->is_hidden = g_strdup("FALSE");
348 DBG("is_hidden [%s]", config->is_hidden);
350 if (config->ip_info) {
351 GError *error = NULL;
352 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
353 WIFI_CONFIG_IPV4_METHOD, NULL);
354 if (config->ip_info->ip_type)
355 DBG("IPv4.Method:%s", config->ip_info->ip_type);
357 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
358 WIFI_CONFIG_IPV4_ADDRESS, NULL);
359 if (config->ip_info->ip_address)
360 DBG("IPv4.Address:%s", config->ip_info->ip_address);
364 struct in_addr netmask;
366 prefix_len = g_key_file_get_integer(keyfile, group_name,
367 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
369 DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
372 if (prefix_len > 0 && prefix_len < 32) {
373 addr = 0xffffffff << (32 - prefix_len);
374 netmask.s_addr = htonl(addr);
375 mask = inet_ntoa(netmask);
376 config->ip_info->subnet_mask = g_strdup(mask);
378 if (config->ip_info->subnet_mask)
379 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
382 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
383 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
384 if (config->ip_info->gateway_address)
385 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
387 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
388 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
389 if (config->ip_info->dns_type)
390 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
394 nameservers = g_key_file_get_string_list(keyfile, group_name,
395 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
398 config->ip_info->dns_count = length;
400 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
401 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
402 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
406 g_strfreev(nameservers);
411 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
412 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
413 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
414 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
415 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
416 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
417 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
418 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
419 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
420 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
422 if (config->eap_config->anonymous_identity)
423 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
424 if (config->eap_config->ca_cert)
425 DBG("ca_cert [%s]", config->eap_config->ca_cert);
426 if (config->eap_config->client_cert)
427 DBG("client_cert [%s]", config->eap_config->client_cert);
428 if (config->eap_config->private_key)
429 DBG("private_key [%s]", config->eap_config->private_key);
430 if (config->eap_config->private_key_password)
431 DBG("private_key_password [%s]", config->eap_config->private_key_password);
432 if (config->eap_config->identity)
433 DBG("identity [%s]", config->eap_config->identity);
434 if (config->eap_config->eap_type)
435 DBG("eap_type [%s]", config->eap_config->eap_type);
436 if (config->eap_config->eap_auth_type)
437 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
438 if (config->eap_config->subject_match)
439 DBG("subject_match [%s]", config->eap_config->subject_match);
442 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
443 if (config->last_error)
444 DBG("last_error [%s]", config->last_error);
446 g_key_file_free(keyfile);
452 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
457 gboolean ret = FALSE;
459 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
461 ERR("Fail to get_wifi_config_group_name");
465 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
466 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
467 if (__remove_configuration(dir) != TRUE) {
468 ERR("[%s] is existed, but cannot remove", dir);
475 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
476 ERR("Cannot mkdir %s", dir);
482 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
483 netconfig_keyfile_save(keyfile, path);
491 static gboolean _remove_configuration(const gchar *config_id)
493 gboolean ret = FALSE;
497 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
499 ERR("Fail to get_wifi_config_group_name");
503 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
504 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
505 if (__remove_configuration(dir) != TRUE) {
506 ERR("[%s] is existed, but cannot remove", dir);
509 INFO("Success to remove [%s]", dir);
512 ERR("[%s] is not existed", dir);
523 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
529 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
531 ERR("Fail to get_wifi_config_group_name");
534 DBG("group_name %s", group_name);
536 keyfile = __get_configuration_keyfile(group_name);
537 if (keyfile == NULL) {
538 ERR("Fail to __get_configuration_keyfile");
543 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
544 g_key_file_set_string(keyfile, group_name, key, value);
545 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
546 g_key_file_set_string(keyfile, group_name, key, value);
547 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
548 gboolean hidden = FALSE;
549 if (g_strcmp0(value, "TRUE") == 0)
551 g_key_file_set_boolean(keyfile, group_name, key, hidden);
552 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
553 g_key_file_set_string(keyfile, group_name, key, value);
554 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
555 g_key_file_set_string(keyfile, group_name, key, value);
556 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
557 g_key_file_set_string(keyfile, group_name, key, value);
558 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
559 g_key_file_set_string(keyfile, group_name, key, value);
560 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
561 g_key_file_set_string(keyfile, group_name, key, value);
562 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
563 g_key_file_set_string(keyfile, group_name, key, value);
564 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
565 g_key_file_set_string(keyfile, group_name, key, value);
566 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
567 g_key_file_set_string(keyfile, group_name, key, value);
569 ERR("key[%s] is not supported", key);
573 _save_configuration(config_id, keyfile);
575 g_key_file_free(keyfile);
581 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
586 gboolean hidden = FALSE;
587 gboolean ret = FALSE;
589 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
591 ERR("Fail to get_wifi_config_group_name");
594 DBG("group_name %s", group_name);
596 keyfile = __get_configuration_keyfile(group_name);
597 if (keyfile == NULL) {
598 ERR("Fail to __get_configuration_keyfile");
603 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
604 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
605 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
606 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
607 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
608 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
609 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
610 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
612 val = g_strdup("TRUE");
614 val = g_strdup("FALSE");
615 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
616 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
617 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
618 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
619 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
620 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
621 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
622 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
623 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
624 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
625 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
626 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
627 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
628 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
629 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
630 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
631 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
632 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
634 ERR("Invalid key[%s]", key);
635 val = g_strdup("NOTSUPPORTED");
638 *value = g_strdup(val);
641 g_key_file_free(keyfile);
647 static GSList *_get_list(void)
650 struct dirent *dp = NULL;
653 dir = opendir(CONNMAN_STORAGE);
655 ERR("Cannot open dir %s", CONNMAN_STORAGE);
659 while ((dp = readdir(dir)) != NULL) {
660 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
661 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
664 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
665 list = g_slist_append(list, g_strdup(config_id));
673 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
675 gboolean ret = FALSE;
678 if ((service_profile == NULL) || (config_id == NULL)) {
679 ERR("Invalid parameter");
683 ret = __get_config_id(service_profile, &val);
684 *config_id = g_strdup(val);
690 gboolean wifi_config_remove_configuration(const gchar *config_id)
692 gboolean ret = FALSE;
694 ret = _remove_configuration(config_id);
699 int __netconfig_hex_char_to_num(char c)
701 if (c >= '0' && c <= '9')
704 if (c >= 'a' && c <= 'f')
707 if (c >= 'A' && c <= 'F')
713 int __netconfig_hex_to_byte(const char *hex)
717 a = __netconfig_hex_char_to_num(*hex++);
721 b = __netconfig_hex_char_to_num(*hex++);
728 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
732 const char *ipos = hex;
733 unsigned char *opos = buf;
735 for (i = 0; i < len; i++) {
736 a = __netconfig_hex_to_byte(ipos);
747 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
754 ERR("Invalid parameter.");
758 *dst = (char *) g_try_malloc0((2*src_len)+1);
760 ERR("failed to allocate memory to buffer.");
766 for (i = 0; i < src_len; i++) {
767 snprintf(buf, 3, "%02x", src[i]);
775 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
777 GVariantIter *iter_copy = NULL;
780 unsigned char *tmp_dst = NULL;
782 if (!dst || *dst || !iter) {
783 ERR("Invalid parameter");
787 iter_copy = g_variant_iter_copy(iter);
789 while (g_variant_iter_loop(iter, "y", &tmp))
791 g_variant_iter_free(iter);
793 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
795 ERR("failed to allocate memory");
796 g_variant_iter_free(iter_copy);
801 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
803 g_variant_iter_free(iter_copy);
809 tmp_dst[length] = '\0';
813 DBG("Length [%d]", length);
817 gboolean _add_vsie(int frame_id, const char* vsie)
819 GVariant *params = NULL;
820 GVariant *message = NULL;
821 GVariantBuilder *bytearray_builder = NULL;
826 unsigned char *bytearray = NULL;
827 size_t bytearray_len = 0;
829 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
830 DBG("Invalid parameter, frame-id: %d", frame_id);
834 vsie_len = strlen(vsie);
836 DBG("vsie length is zero");
840 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
842 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
843 if (bytearray == NULL) {
844 DBG("Failed to allocate memory to bytearray");
848 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
849 DBG("invalid vsie string");
854 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
855 for (i = 0; i < bytearray_len; i++)
856 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
858 params = g_variant_new("(iay)", frame_id, bytearray_builder);
859 g_variant_builder_unref(bytearray_builder);
861 if_path = netconfig_wifi_get_supplicant_interface();
863 if (if_path == NULL) {
864 ERR("Fail to get wpa_supplicant DBus path");
869 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
870 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
873 if (message == NULL) {
874 ERR("Failed to send command to wpa_supplicant");
879 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
885 gboolean _get_vsie(int frame_id, char **vsie)
887 GVariant *params = NULL;
888 GVariant *message = NULL;
891 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
892 DBG("Invalid parameter, frame-id: %d", frame_id);
896 if_path = netconfig_wifi_get_supplicant_interface();
897 if (if_path == NULL) {
898 ERR("Fail to get wpa_supplicant DBus path");
902 params = g_variant_new("(i)", frame_id);
904 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
905 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
908 if (message == NULL) {
909 ERR("Failed to send command to wpa_supplicant");
912 GVariantIter *iter = NULL;
913 unsigned char *vsie_bytes = NULL;
917 g_variant_get(message, "(ay)", &iter);
919 ERR("vsie is not present");
923 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
924 if (vsie_bytes == NULL) {
925 ERR("vsie_bytes not allocated");
929 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
932 ERR("vsie not allocated.");
939 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
945 gboolean _remove_vsie(int frame_id, const char *vsie)
947 GVariant *params = NULL;
948 GVariant *message = NULL;
949 GVariantBuilder *bytearray_builder = NULL;
954 unsigned char *bytearray = NULL;
955 size_t bytearray_len = 0;
957 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
958 DBG("Invalid parameter, frame-id: %d", frame_id);
962 vsie_len = strlen(vsie);
964 DBG("vsie length is zero");
968 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
970 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
971 if (bytearray == NULL) {
972 DBG("Failed to allocate memory to bytearray");
976 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
977 DBG("invalid vsie string");
982 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
983 for (i = 0; i < bytearray_len; i++)
984 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
986 params = g_variant_new("(iay)", frame_id, bytearray_builder);
987 g_variant_builder_unref(bytearray_builder);
989 if_path = netconfig_wifi_get_supplicant_interface();
990 if (if_path == NULL) {
991 ERR("Fail to get wpa_supplicant DBus path");
996 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
997 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
1000 if (message == NULL) {
1001 ERR("Failed to send command to wpa_supplicant");
1006 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1013 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
1016 GSList *config_ids = NULL;
1018 gchar **result = NULL;
1020 g_return_val_if_fail(wifi != NULL, TRUE);
1022 config_ids = _get_list();
1023 if (config_ids == NULL) {
1024 ERR("Fail to get config list");
1025 netconfig_error_no_profile(context);
1029 length = g_slist_length(config_ids);
1030 result = g_new0(gchar *, length + 1);
1031 for (i = 0; i < length; i++) {
1032 gchar *config_id = g_slist_nth_data(config_ids, i);
1033 result[i] = g_strdup(config_id);
1036 config_ids = g_slist_nth(config_ids, 0);
1037 g_slist_free_full(config_ids, g_free);
1039 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1041 for (i = 0; i < length; i++)
1051 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1052 const gchar *config_id)
1054 gboolean ret = FALSE;
1055 GVariantBuilder *b = NULL;
1056 struct wifi_config *conf = NULL;
1058 g_return_val_if_fail(wifi != NULL, TRUE);
1060 conf = g_new0(struct wifi_config, 1);
1061 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1063 ret = _load_configuration(config_id, conf);
1065 g_free(conf->ip_info);
1067 ERR("Fail to _load_configuration");
1068 netconfig_error_no_profile(context);
1072 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1073 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1074 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1075 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1077 if (conf->proxy_address != NULL)
1078 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1080 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1082 if (conf->ip_info->ip_type != NULL)
1083 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1085 if (conf->ip_info->ip_address != NULL)
1086 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1088 if (conf->ip_info->subnet_mask != NULL)
1089 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1091 if (conf->ip_info->prefix_length > 0)
1092 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV6_PREFIX_LEN, g_variant_new_int32(conf->ip_info->prefix_length));
1094 if (conf->ip_info->gateway_address != NULL)
1095 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1097 if (conf->ip_info->dns_type != NULL)
1098 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1100 int i = 0, count = conf->ip_info->dns_count;
1102 if (conf->ip_info->dns_address[i] != NULL)
1103 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1108 if (conf->last_error != NULL)
1109 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1111 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1113 __free_wifi_configuration(conf);
1115 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1116 g_variant_builder_unref(b);
1120 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1121 const char *netmask)
1130 mask = inet_network(netmask);
1133 /* a valid netmask must be 2^n - 1 */
1134 if ((host & (host + 1)) != 0)
1138 for (; mask; mask <<= 1)
1144 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1145 const gchar *config_id, GVariant *configuration)
1147 gboolean ret = FALSE;
1148 struct wifi_config *conf = NULL;
1149 GKeyFile *keyfile = NULL;
1153 gchar *group_name = NULL;
1156 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1157 ERR("Invalid parameter");
1158 netconfig_error_invalid_parameter(context);
1162 conf = g_new0(struct wifi_config, 1);
1163 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1165 g_variant_get(configuration, "a{sv}", &iter);
1166 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1167 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1168 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1169 conf->name = g_strdup(g_variant_get_string(value, NULL));
1170 DBG("name [%s]", conf->name);
1174 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1175 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1176 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1177 DBG("ssid [%s]", conf->ssid);
1181 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1182 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1183 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1184 DBG("passphrase []");
1186 conf->passphrase = NULL;
1188 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1189 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1190 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1191 DBG("is_hidden [%s]", conf->is_hidden);
1193 conf->is_hidden = NULL;
1195 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1196 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1197 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1198 DBG("IP config type [%s]", conf->ip_info->ip_type);
1200 conf->ip_info->ip_type = NULL;
1202 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1203 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1204 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1205 DBG("IP address [%s]", conf->ip_info->ip_address);
1207 conf->ip_info->ip_address = NULL;
1209 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1210 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1211 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1212 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1214 conf->ip_info->subnet_mask = NULL;
1216 } else if (g_strcmp0(field, WIFI_CONFIG_IPV6_PREFIX_LEN) == 0) {
1217 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1218 conf->ip_info->prefix_length = g_variant_get_int32(value);
1219 DBG("IPv6 Prefix Length [%d]", conf->ip_info->prefix_length);
1221 conf->ip_info->prefix_length = 0;
1223 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1224 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1225 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1226 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1228 conf->ip_info->gateway_address = NULL;
1230 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1231 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1232 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1233 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1235 conf->ip_info->dns_type = NULL;
1237 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1238 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1239 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1240 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1241 conf->ip_info->dns_count = order + 1;
1244 conf->ip_info->dns_address[order++] = NULL;
1246 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1247 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1248 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1249 DBG("proxy_address [%s]", conf->proxy_address);
1251 conf->proxy_address = NULL;
1255 conf->favorite = TRUE;
1256 conf->autoconnect = TRUE;
1258 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1260 __free_wifi_configuration(conf);
1261 ERR("Fail to get_wifi_config_group_name");
1262 netconfig_error_fail_save_congifuration(context);
1266 keyfile = g_key_file_new();
1267 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1268 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1270 if (conf->passphrase != NULL) {
1271 gchar *enc_data = NULL;
1272 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1275 ERR("Failed to encrypt the passphrase");
1277 g_free(conf->passphrase);
1278 conf->passphrase = enc_data;
1281 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1284 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1285 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1287 /* Optional field */
1288 if (conf->proxy_address != NULL) {
1289 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1290 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1293 if (conf->is_hidden != NULL) {
1294 gboolean hidden = FALSE;
1295 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1297 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1300 if (conf->ip_info->ip_type != NULL)
1301 g_key_file_set_string(keyfile, group_name,
1302 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1304 if (conf->ip_info->ip_address != NULL)
1305 g_key_file_set_string(keyfile, group_name,
1306 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1308 if (conf->ip_info->subnet_mask != NULL) {
1309 unsigned char prefix_len;
1310 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1311 conf->ip_info->subnet_mask);
1312 if (prefix_len > 0 && prefix_len < 32)
1313 g_key_file_set_integer(keyfile, group_name,
1314 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1317 if (conf->ip_info->prefix_length > 0)
1318 g_key_file_set_integer(keyfile, group_name,
1319 WIFI_CONFIG_IPV6_PREFIX_LEN, conf->ip_info->prefix_length);
1321 if (conf->ip_info->gateway_address != NULL)
1322 g_key_file_set_string(keyfile, group_name,
1323 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1325 if (conf->ip_info->dns_type != NULL)
1326 g_key_file_set_string(keyfile, group_name,
1327 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1329 int i = 0, count = conf->ip_info->dns_count;
1331 if (conf->ip_info->dns_address[i] != NULL)
1332 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1333 conf->ip_info->dns_address[i]);
1338 ret = _save_configuration(config_id, keyfile);
1340 INFO("Success to save configuration [%s]", config_id);
1341 wifi_complete_save_configuration(wifi, context);
1343 INFO("Fail to save configuration [%s]", config_id);
1344 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1347 g_key_file_free(keyfile);
1349 __free_wifi_configuration(conf);
1351 g_variant_iter_free(iter);
1356 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1357 const gchar *config_id)
1359 gboolean ret = FALSE;
1360 GVariantBuilder *b = NULL;
1361 struct wifi_config *conf = NULL;
1363 g_return_val_if_fail(wifi != NULL, TRUE);
1365 conf = g_new0(struct wifi_config, 1);
1366 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1367 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1369 ret = _load_configuration(config_id, conf);
1371 g_free(conf->eap_config);
1372 g_free(conf->ip_info);
1374 ERR("Fail to _load_configuration");
1375 netconfig_error_no_profile(context);
1379 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1380 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1381 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1382 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1383 if (conf->proxy_address != NULL)
1384 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1386 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1388 if (conf->last_error != NULL)
1389 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1391 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1393 if (conf->eap_config != NULL) {
1394 if (conf->eap_config->anonymous_identity != NULL)
1395 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1397 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1399 if (conf->eap_config->ca_cert != NULL)
1400 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1402 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1404 if (conf->eap_config->client_cert != NULL)
1405 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1407 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1409 if (conf->eap_config->private_key != NULL)
1410 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1412 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1414 if (conf->eap_config->private_key_password != NULL)
1415 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1417 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1419 if (conf->eap_config->identity != NULL)
1420 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1422 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1424 if (conf->eap_config->eap_type != NULL)
1425 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1427 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1429 if (conf->eap_config->eap_auth_type != NULL)
1430 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1432 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1434 if (conf->eap_config->subject_match != NULL)
1435 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1437 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1440 __free_wifi_configuration(conf);
1442 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1443 g_variant_builder_unref(b);
1447 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1448 const gchar *config_id, GVariant *configuration)
1450 gboolean ret = FALSE;
1451 struct wifi_config *conf = NULL;
1452 GKeyFile *keyfile = NULL;
1456 gchar *group_name = NULL;
1458 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1459 ERR("Invalid parameter");
1460 netconfig_error_invalid_parameter(context);
1464 conf = g_new0(struct wifi_config, 1);
1465 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1467 g_variant_get(configuration, "a{sv}", &iter);
1468 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1469 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1470 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1471 conf->name = g_strdup(g_variant_get_string(value, NULL));
1472 DBG("name [%s]", conf->name);
1476 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1477 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1478 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1479 DBG("ssid [%s]", conf->ssid);
1483 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1484 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1485 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1486 DBG("passphrase [%s]", conf->passphrase);
1488 conf->passphrase = NULL;
1490 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1491 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1492 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1493 DBG("is_hidden [%s]", conf->is_hidden);
1495 conf->is_hidden = NULL;
1497 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1498 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1499 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1500 DBG("proxy_address [%s]", conf->proxy_address);
1502 conf->proxy_address = NULL;
1504 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1505 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1506 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1507 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1509 conf->eap_config->anonymous_identity = NULL;
1511 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1512 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1513 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1514 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1516 conf->eap_config->ca_cert = NULL;
1518 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1519 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1520 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1521 DBG("client_cert [%s]", conf->eap_config->client_cert);
1523 conf->eap_config->client_cert = NULL;
1525 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1526 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1527 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1528 DBG("private_key [%s]", conf->eap_config->private_key);
1530 conf->eap_config->private_key = NULL;
1532 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1533 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1534 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1535 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1537 conf->eap_config->private_key_password = NULL;
1539 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1540 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1541 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1542 DBG("identity [%s]", conf->eap_config->identity);
1544 conf->eap_config->identity = NULL;
1546 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1547 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1548 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1549 DBG("eap_type [%s]", conf->eap_config->eap_type);
1551 conf->eap_config->eap_type = NULL;
1553 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1554 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1555 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1556 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1558 conf->eap_config->eap_auth_type = NULL;
1560 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1561 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1562 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1563 DBG("subject_match [%s]", conf->eap_config->subject_match);
1565 conf->eap_config->subject_match = NULL;
1569 conf->favorite = TRUE;
1570 conf->autoconnect = TRUE;
1572 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1574 __free_wifi_configuration(conf);
1575 ERR("Fail to get_wifi_config_group_name");
1579 keyfile = g_key_file_new();
1580 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1581 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1583 if (conf->passphrase != NULL) {
1584 gchar *enc_data = NULL;
1585 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1588 ERR("Failed to encrypt the passphrase");
1590 g_free(conf->passphrase);
1591 conf->passphrase = enc_data;
1593 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1596 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1597 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1599 /* Optional field */
1600 if (conf->proxy_address != NULL) {
1601 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1602 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1605 if (conf->is_hidden != NULL) {
1606 gboolean hidden = FALSE;
1607 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1609 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1612 if (conf->eap_config->anonymous_identity != NULL)
1613 g_key_file_set_string(keyfile, group_name,
1614 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1616 if (conf->eap_config->ca_cert != NULL)
1617 g_key_file_set_string(keyfile, group_name,
1618 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1620 if (conf->eap_config->client_cert != NULL)
1621 g_key_file_set_string(keyfile, group_name,
1622 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1624 if (conf->eap_config->private_key != NULL)
1625 g_key_file_set_string(keyfile, group_name,
1626 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1628 if (conf->eap_config->private_key_password != NULL)
1629 g_key_file_set_string(keyfile, group_name,
1630 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1632 if (conf->eap_config->identity != NULL)
1633 g_key_file_set_string(keyfile, group_name,
1634 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1636 if (conf->eap_config->eap_type != NULL)
1637 g_key_file_set_string(keyfile, group_name,
1638 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1640 if (conf->eap_config->eap_auth_type != NULL)
1641 g_key_file_set_string(keyfile, group_name,
1642 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1644 if (conf->eap_config->subject_match != NULL)
1645 g_key_file_set_string(keyfile, group_name,
1646 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1648 ret = _save_configuration(config_id, keyfile);
1650 INFO("Success to save eap configuration [%s]", config_id);
1651 wifi_complete_save_eap_configuration(wifi, context);
1653 INFO("Fail to save eap configuration [%s]", config_id);
1654 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1657 g_key_file_free(keyfile);
1659 __free_wifi_configuration(conf);
1661 g_variant_iter_free(iter);
1666 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1668 gboolean ret = FALSE;
1670 if ((wifi == NULL) || (config_id == NULL)) {
1671 ERR("Invalid parameter");
1672 netconfig_error_invalid_parameter(context);
1676 ret = _remove_configuration(config_id);
1678 /* no configuration or error */
1679 ERR("No [%s] configuration", config_id);
1680 netconfig_error_no_profile(context);
1684 wifi_complete_remove_configuration(wifi, context);
1688 /* config field key / value */
1690 * [wifi_macaddress_config_id]
1691 * Name=name (mandatory)
1692 * SSID=SSID (mandatory)
1693 * Frequency=2462 (X)
1695 * AutoConnect=true (Default true)
1696 * Modified=2015-03-20 (X)
1697 * IPv4.method=manual (O)
1698 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1699 * IPv6.method=auto (X)
1700 * IPv6.privacy=disabled (X)
1701 * IPv4.netmask_prefixlen=24 (X)
1702 * IPv4.local_address=192.0.0.1 (O)
1703 * IPv4.gateway=192.0.0.1 (O ? X ?)
1704 * Nameservers=192.168.43.22; (O)
1705 * Proxy.Method=manual (O)
1706 * Proxy.Servers=trst.com:8888; (O)
1708 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1709 const gchar *config_id, const gchar *key, const gchar *value)
1711 gboolean ret = FALSE;
1712 gchar *keyfile_key = NULL;
1714 g_return_val_if_fail(wifi != NULL, TRUE);
1715 g_return_val_if_fail(config_id != NULL, TRUE);
1716 g_return_val_if_fail(key != NULL, TRUE);
1718 DBG("Key[%s] Value[%s]", key, value);
1720 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1721 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1723 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1724 netconfig_error_invalid_parameter(context);
1727 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1728 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1729 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1730 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1731 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1732 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1733 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1734 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1735 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1736 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1737 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1738 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1739 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1740 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1741 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1742 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1743 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1744 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1745 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1747 ERR("Not supported key[%s]", key);
1748 netconfig_error_invalid_parameter(context);
1752 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1754 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1757 if (keyfile_key != NULL)
1758 g_free(keyfile_key);
1760 wifi_complete_set_config_field(wifi, context);
1764 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1766 gboolean ret = FALSE;
1767 gchar *passphrase = NULL;
1769 if ((wifi == NULL) || (config_id == NULL)) {
1770 ERR("Invalid parameter");
1771 netconfig_error_invalid_parameter(context);
1775 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1777 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1778 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1782 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1788 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1789 int frame_id, const gchar *vsie)
1791 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1793 g_return_val_if_fail(wifi != NULL, TRUE);
1794 g_return_val_if_fail(vsie != NULL, TRUE);
1796 gboolean ret = FALSE;
1798 ret = _add_vsie(frame_id, vsie);
1800 DBG("Failed to add vsie: %s", vsie);
1801 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1805 wifi_complete_add_vsie(wifi, context);
1809 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1812 DBG("Frame ID: [%d]", frame_id);
1814 g_return_val_if_fail(wifi != NULL, TRUE);
1816 gboolean ret = FALSE;
1819 ret = _get_vsie(frame_id, &vsie);
1821 DBG("Failed to get vsie for frame:[%d]", frame_id);
1822 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1826 DBG("Received vsie: %s", vsie);
1827 wifi_complete_get_vsie(wifi, context, vsie);
1832 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1833 int frame_id, const gchar *vsie)
1835 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1837 g_return_val_if_fail(wifi != NULL, TRUE);
1838 g_return_val_if_fail(vsie != NULL, TRUE);
1840 gboolean ret = FALSE;
1842 ret = _remove_vsie(frame_id, vsie);
1844 DBG("Failed to remove vsie: %s", vsie);
1845 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1849 wifi_complete_remove_vsie(wifi, context);