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 GError *error = NULL;
351 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
352 WIFI_CONFIG_IPV4_METHOD, NULL);
353 if (config->ip_info->ip_type)
354 DBG("IPv4.Method:%s", config->ip_info->ip_type);
356 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
357 WIFI_CONFIG_IPV4_ADDRESS, NULL);
358 if (config->ip_info->ip_address)
359 DBG("IPv4.Address:%s", config->ip_info->ip_address);
363 struct in_addr netmask;
365 prefix_len = g_key_file_get_integer(keyfile, group_name,
366 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
368 DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
371 if (prefix_len > 0 && prefix_len < 32) {
372 addr = 0xffffffff << (32 - prefix_len);
373 netmask.s_addr = htonl(addr);
374 mask = inet_ntoa(netmask);
375 config->ip_info->subnet_mask = g_strdup(mask);
377 if (config->ip_info->subnet_mask)
378 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
381 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
382 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
383 if (config->ip_info->gateway_address)
384 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
386 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
387 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
388 if (config->ip_info->dns_type)
389 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
393 nameservers = g_key_file_get_string_list(keyfile, group_name,
394 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
397 config->ip_info->dns_count = length;
399 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
400 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
401 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
405 g_strfreev(nameservers);
410 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
411 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
412 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
413 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
414 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
415 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
416 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
417 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
418 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
419 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
421 if (config->eap_config->anonymous_identity)
422 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
423 if (config->eap_config->ca_cert)
424 DBG("ca_cert [%s]", config->eap_config->ca_cert);
425 if (config->eap_config->client_cert)
426 DBG("client_cert [%s]", config->eap_config->client_cert);
427 if (config->eap_config->private_key)
428 DBG("private_key [%s]", config->eap_config->private_key);
429 if (config->eap_config->private_key_password)
430 DBG("private_key_password [%s]", config->eap_config->private_key_password);
431 if (config->eap_config->identity)
432 DBG("identity [%s]", config->eap_config->identity);
433 if (config->eap_config->eap_type)
434 DBG("eap_type [%s]", config->eap_config->eap_type);
435 if (config->eap_config->eap_auth_type)
436 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
437 if (config->eap_config->subject_match)
438 DBG("subject_match [%s]", config->eap_config->subject_match);
441 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
442 if (config->last_error)
443 DBG("last_error [%s]", config->last_error);
445 g_key_file_free(keyfile);
451 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
456 gboolean ret = FALSE;
458 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
460 ERR("Fail to get_wifi_config_group_name");
464 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
465 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
466 if (__remove_configuration(dir) != TRUE) {
467 ERR("[%s] is existed, but cannot remove", dir);
474 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
475 ERR("Cannot mkdir %s", dir);
481 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
482 netconfig_keyfile_save(keyfile, path);
490 static gboolean _remove_configuration(const gchar *config_id)
492 gboolean ret = FALSE;
496 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
498 ERR("Fail to get_wifi_config_group_name");
502 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
503 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
504 if (__remove_configuration(dir) != TRUE) {
505 ERR("[%s] is existed, but cannot remove", dir);
508 INFO("Success to remove [%s]", dir);
511 ERR("[%s] is not existed", dir);
522 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
528 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
530 ERR("Fail to get_wifi_config_group_name");
533 DBG("group_name %s", group_name);
535 keyfile = __get_configuration_keyfile(group_name);
536 if (keyfile == NULL) {
537 ERR("Fail to __get_configuration_keyfile");
542 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
543 g_key_file_set_string(keyfile, group_name, key, value);
544 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
545 g_key_file_set_string(keyfile, group_name, key, value);
546 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
547 gboolean hidden = FALSE;
548 if (g_strcmp0(value, "TRUE") == 0)
550 g_key_file_set_boolean(keyfile, group_name, key, hidden);
551 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
552 g_key_file_set_string(keyfile, group_name, key, value);
553 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
554 g_key_file_set_string(keyfile, group_name, key, value);
555 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
556 g_key_file_set_string(keyfile, group_name, key, value);
557 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
558 g_key_file_set_string(keyfile, group_name, key, value);
559 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
560 g_key_file_set_string(keyfile, group_name, key, value);
561 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
562 g_key_file_set_string(keyfile, group_name, key, value);
563 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
564 g_key_file_set_string(keyfile, group_name, key, value);
565 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
566 g_key_file_set_string(keyfile, group_name, key, value);
568 ERR("key[%s] is not supported", key);
572 _save_configuration(config_id, keyfile);
574 g_key_file_free(keyfile);
580 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
585 gboolean hidden = FALSE;
586 gboolean ret = FALSE;
588 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
590 ERR("Fail to get_wifi_config_group_name");
593 DBG("group_name %s", group_name);
595 keyfile = __get_configuration_keyfile(group_name);
596 if (keyfile == NULL) {
597 ERR("Fail to __get_configuration_keyfile");
602 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
603 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
604 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
605 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
606 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
607 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
608 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
609 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
611 val = g_strdup("TRUE");
613 val = g_strdup("FALSE");
614 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
615 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
616 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
617 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
618 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
619 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
620 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
621 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
622 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
623 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
624 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
625 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
626 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
627 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
628 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
629 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
630 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
631 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
633 ERR("Invalid key[%s]", key);
634 val = g_strdup("NOTSUPPORTED");
637 *value = g_strdup(val);
640 g_key_file_free(keyfile);
646 static GSList *_get_list(void)
649 struct dirent *dp = NULL;
652 dir = opendir(CONNMAN_STORAGE);
654 ERR("Cannot open dir %s", CONNMAN_STORAGE);
658 while ((dp = readdir(dir)) != NULL) {
659 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
660 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
663 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
664 list = g_slist_append(list, g_strdup(config_id));
672 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
674 gboolean ret = FALSE;
677 if ((service_profile == NULL) || (config_id == NULL)) {
678 ERR("Invalid parameter");
682 ret = __get_config_id(service_profile, &val);
683 *config_id = g_strdup(val);
689 gboolean wifi_config_remove_configuration(const gchar *config_id)
691 gboolean ret = FALSE;
693 ret = _remove_configuration(config_id);
698 int __netconfig_hex_char_to_num(char c)
700 if (c >= '0' && c <= '9')
703 if (c >= 'a' && c <= 'f')
706 if (c >= 'A' && c <= 'F')
712 int __netconfig_hex_to_byte(const char *hex)
716 a = __netconfig_hex_char_to_num(*hex++);
720 b = __netconfig_hex_char_to_num(*hex++);
727 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
731 const char *ipos = hex;
732 unsigned char *opos = buf;
734 for (i = 0; i < len; i++) {
735 a = __netconfig_hex_to_byte(ipos);
746 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
753 ERR("Invalid parameter.");
757 *dst = (char *) g_try_malloc0((2*src_len)+1);
759 ERR("failed to allocate memory to buffer.");
765 for (i = 0; i < src_len; i++) {
766 snprintf(buf, 3, "%02x", src[i]);
774 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
776 GVariantIter *iter_copy = NULL;
779 unsigned char *tmp_dst = NULL;
781 if (!dst || *dst || !iter) {
782 ERR("Invalid parameter");
786 iter_copy = g_variant_iter_copy(iter);
788 while (g_variant_iter_loop(iter, "y", &tmp))
790 g_variant_iter_free(iter);
792 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
794 ERR("failed to allocate memory");
795 g_variant_iter_free(iter_copy);
800 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
802 g_variant_iter_free(iter_copy);
808 tmp_dst[length] = '\0';
812 DBG("Length [%d]", length);
816 gboolean _add_vsie(int frame_id, const char* vsie)
818 GVariant *params = NULL;
819 GVariant *message = NULL;
820 GVariantBuilder *bytearray_builder = NULL;
825 unsigned char *bytearray = NULL;
826 size_t bytearray_len = 0;
828 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
829 DBG("Invalid parameter, frame-id: %d", frame_id);
833 vsie_len = strlen(vsie);
835 DBG("vsie length is zero");
839 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
841 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
842 if (bytearray == NULL) {
843 DBG("Failed to allocate memory to bytearray");
847 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
848 DBG("invalid vsie string");
853 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
854 for (i = 0; i < bytearray_len; i++)
855 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
857 params = g_variant_new("(iay)", frame_id, bytearray_builder);
858 g_variant_builder_unref(bytearray_builder);
860 if_path = netconfig_wifi_get_supplicant_interface();
862 if (if_path == NULL) {
863 ERR("Fail to get wpa_supplicant DBus path");
868 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
869 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
872 if (message == NULL) {
873 ERR("Failed to send command to wpa_supplicant");
878 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
884 gboolean _get_vsie(int frame_id, char **vsie)
886 GVariant *params = NULL;
887 GVariant *message = NULL;
890 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
891 DBG("Invalid parameter, frame-id: %d", frame_id);
895 if_path = netconfig_wifi_get_supplicant_interface();
896 if (if_path == NULL) {
897 ERR("Fail to get wpa_supplicant DBus path");
901 params = g_variant_new("(i)", frame_id);
903 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
904 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
907 if (message == NULL) {
908 ERR("Failed to send command to wpa_supplicant");
911 GVariantIter *iter = NULL;
912 unsigned char *vsie_bytes = NULL;
916 g_variant_get(message, "(ay)", &iter);
918 ERR("vsie is not present");
922 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
923 if (vsie_bytes == NULL) {
924 ERR("vsie_bytes not allocated");
928 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
931 ERR("vsie not allocated.");
938 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
944 gboolean _remove_vsie(int frame_id, const char *vsie)
946 GVariant *params = NULL;
947 GVariant *message = NULL;
948 GVariantBuilder *bytearray_builder = NULL;
953 unsigned char *bytearray = NULL;
954 size_t bytearray_len = 0;
956 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
957 DBG("Invalid parameter, frame-id: %d", frame_id);
961 vsie_len = strlen(vsie);
963 DBG("vsie length is zero");
967 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
969 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
970 if (bytearray == NULL) {
971 DBG("Failed to allocate memory to bytearray");
975 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
976 DBG("invalid vsie string");
981 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
982 for (i = 0; i < bytearray_len; i++)
983 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
985 params = g_variant_new("(iay)", frame_id, bytearray_builder);
986 g_variant_builder_unref(bytearray_builder);
988 if_path = netconfig_wifi_get_supplicant_interface();
989 if (if_path == NULL) {
990 ERR("Fail to get wpa_supplicant DBus path");
995 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
996 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
999 if (message == NULL) {
1000 ERR("Failed to send command to wpa_supplicant");
1005 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1012 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
1015 GSList *config_ids = NULL;
1017 gchar **result = NULL;
1019 g_return_val_if_fail(wifi != NULL, TRUE);
1021 config_ids = _get_list();
1022 if (config_ids == NULL) {
1023 ERR("Fail to get config list");
1024 netconfig_error_no_profile(context);
1028 length = g_slist_length(config_ids);
1029 result = g_new0(gchar *, length + 1);
1030 for (i = 0; i < length; i++) {
1031 gchar *config_id = g_slist_nth_data(config_ids, i);
1032 result[i] = g_strdup(config_id);
1035 config_ids = g_slist_nth(config_ids, 0);
1036 g_slist_free_full(config_ids, g_free);
1038 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1040 for (i = 0; i < length; i++)
1050 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1051 const gchar *config_id)
1053 gboolean ret = FALSE;
1054 GVariantBuilder *b = NULL;
1055 struct wifi_config *conf = NULL;
1057 g_return_val_if_fail(wifi != NULL, TRUE);
1059 conf = g_new0(struct wifi_config, 1);
1060 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1062 ret = _load_configuration(config_id, conf);
1064 g_free(conf->ip_info);
1066 ERR("Fail to _load_configuration");
1067 netconfig_error_no_profile(context);
1071 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1072 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1073 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1074 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1076 if (conf->proxy_address != NULL)
1077 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1079 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1081 if (conf->ip_info->ip_type != NULL)
1082 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1084 if (conf->ip_info->ip_address != NULL)
1085 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1087 if (conf->ip_info->subnet_mask != NULL)
1088 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1090 if (conf->ip_info->gateway_address != NULL)
1091 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1093 if (conf->ip_info->dns_type != NULL)
1094 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1096 int i = 0, count = conf->ip_info->dns_count;
1098 if (conf->ip_info->dns_address[i] != NULL)
1099 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1104 if (conf->last_error != NULL)
1105 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1107 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1109 __free_wifi_configuration(conf);
1111 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1112 g_variant_builder_unref(b);
1116 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1117 const char *netmask)
1126 mask = inet_network(netmask);
1129 /* a valid netmask must be 2^n - 1 */
1130 if ((host & (host + 1)) != 0)
1134 for (; mask; mask <<= 1)
1140 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1141 const gchar *config_id, GVariant *configuration)
1143 gboolean ret = FALSE;
1144 struct wifi_config *conf = NULL;
1145 GKeyFile *keyfile = NULL;
1149 gchar *group_name = NULL;
1152 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1153 ERR("Invalid parameter");
1154 netconfig_error_invalid_parameter(context);
1158 conf = g_new0(struct wifi_config, 1);
1159 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1161 g_variant_get(configuration, "a{sv}", &iter);
1162 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1163 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1164 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1165 conf->name = g_strdup(g_variant_get_string(value, NULL));
1166 DBG("name [%s]", conf->name);
1170 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1171 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1172 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1173 DBG("ssid [%s]", conf->ssid);
1177 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1178 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1179 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1180 DBG("passphrase []");
1182 conf->passphrase = NULL;
1184 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1185 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1186 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1187 DBG("is_hidden [%s]", conf->is_hidden);
1189 conf->is_hidden = NULL;
1191 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1192 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1193 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1194 DBG("IP config type [%s]", conf->ip_info->ip_type);
1196 conf->ip_info->ip_type = NULL;
1198 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1199 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1200 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1201 DBG("IP address [%s]", conf->ip_info->ip_address);
1203 conf->ip_info->ip_address = NULL;
1205 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1206 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1207 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1208 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1210 conf->ip_info->subnet_mask = NULL;
1212 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1213 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1214 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1215 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1217 conf->ip_info->gateway_address = NULL;
1219 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1220 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1221 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1222 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1224 conf->ip_info->dns_type = NULL;
1226 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1227 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1228 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1229 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1230 conf->ip_info->dns_count = order + 1;
1233 conf->ip_info->dns_address[order++] = NULL;
1235 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1236 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1237 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1238 DBG("proxy_address [%s]", conf->proxy_address);
1240 conf->proxy_address = NULL;
1244 conf->favorite = TRUE;
1245 conf->autoconnect = TRUE;
1247 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1249 __free_wifi_configuration(conf);
1250 ERR("Fail to get_wifi_config_group_name");
1251 netconfig_error_fail_save_congifuration(context);
1255 keyfile = g_key_file_new();
1256 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1257 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1259 if (conf->passphrase != NULL) {
1260 gchar *enc_data = NULL;
1261 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1264 ERR("Failed to encrypt the passphrase");
1266 g_free(conf->passphrase);
1267 conf->passphrase = enc_data;
1270 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1273 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1274 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1276 /* Optional field */
1277 if (conf->proxy_address != NULL) {
1278 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1279 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1282 if (conf->is_hidden != NULL) {
1283 gboolean hidden = FALSE;
1284 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1286 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1289 if (conf->ip_info->ip_type != NULL)
1290 g_key_file_set_string(keyfile, group_name,
1291 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1293 if (conf->ip_info->ip_address != NULL)
1294 g_key_file_set_string(keyfile, group_name,
1295 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1297 if (conf->ip_info->subnet_mask != NULL) {
1298 unsigned char prefix_len;
1299 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1300 conf->ip_info->subnet_mask);
1301 if (prefix_len > 0 && prefix_len < 32)
1302 g_key_file_set_integer(keyfile, group_name,
1303 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1306 if (conf->ip_info->gateway_address != NULL)
1307 g_key_file_set_string(keyfile, group_name,
1308 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1310 if (conf->ip_info->dns_type != NULL)
1311 g_key_file_set_string(keyfile, group_name,
1312 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1314 int i = 0, count = conf->ip_info->dns_count;
1316 if (conf->ip_info->dns_address[i] != NULL)
1317 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1318 conf->ip_info->dns_address[i]);
1323 ret = _save_configuration(config_id, keyfile);
1325 INFO("Success to save configuration [%s]", config_id);
1326 wifi_complete_save_configuration(wifi, context);
1328 INFO("Fail to save configuration [%s]", config_id);
1329 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1332 g_key_file_free(keyfile);
1334 __free_wifi_configuration(conf);
1336 g_variant_iter_free(iter);
1341 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1342 const gchar *config_id)
1344 gboolean ret = FALSE;
1345 GVariantBuilder *b = NULL;
1346 struct wifi_config *conf = NULL;
1348 g_return_val_if_fail(wifi != NULL, TRUE);
1350 conf = g_new0(struct wifi_config, 1);
1351 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1352 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1354 ret = _load_configuration(config_id, conf);
1356 g_free(conf->eap_config);
1357 g_free(conf->ip_info);
1359 ERR("Fail to _load_configuration");
1360 netconfig_error_no_profile(context);
1364 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1365 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1366 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1367 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1368 if (conf->proxy_address != NULL)
1369 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1371 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1373 if (conf->last_error != NULL)
1374 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1376 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1378 if (conf->eap_config != NULL) {
1379 if (conf->eap_config->anonymous_identity != NULL)
1380 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1382 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1384 if (conf->eap_config->ca_cert != NULL)
1385 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1387 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1389 if (conf->eap_config->client_cert != NULL)
1390 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1392 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1394 if (conf->eap_config->private_key != NULL)
1395 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1397 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1399 if (conf->eap_config->private_key_password != NULL)
1400 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1402 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1404 if (conf->eap_config->identity != NULL)
1405 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1407 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1409 if (conf->eap_config->eap_type != NULL)
1410 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1412 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1414 if (conf->eap_config->eap_auth_type != NULL)
1415 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1417 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1419 if (conf->eap_config->subject_match != NULL)
1420 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1422 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1425 __free_wifi_configuration(conf);
1427 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1428 g_variant_builder_unref(b);
1432 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1433 const gchar *config_id, GVariant *configuration)
1435 gboolean ret = FALSE;
1436 struct wifi_config *conf = NULL;
1437 GKeyFile *keyfile = NULL;
1441 gchar *group_name = NULL;
1443 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1444 ERR("Invalid parameter");
1445 netconfig_error_invalid_parameter(context);
1449 conf = g_new0(struct wifi_config, 1);
1450 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1452 g_variant_get(configuration, "a{sv}", &iter);
1453 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1454 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1455 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1456 conf->name = g_strdup(g_variant_get_string(value, NULL));
1457 DBG("name [%s]", conf->name);
1461 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1462 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1463 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1464 DBG("ssid [%s]", conf->ssid);
1468 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1469 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1470 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1471 DBG("passphrase [%s]", conf->passphrase);
1473 conf->passphrase = NULL;
1475 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1476 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1477 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1478 DBG("is_hidden [%s]", conf->is_hidden);
1480 conf->is_hidden = NULL;
1482 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1483 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1484 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1485 DBG("proxy_address [%s]", conf->proxy_address);
1487 conf->proxy_address = NULL;
1489 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1490 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1491 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1492 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1494 conf->eap_config->anonymous_identity = NULL;
1496 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1497 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1498 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1499 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1501 conf->eap_config->ca_cert = NULL;
1503 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1504 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1505 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1506 DBG("client_cert [%s]", conf->eap_config->client_cert);
1508 conf->eap_config->client_cert = NULL;
1510 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1511 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1512 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1513 DBG("private_key [%s]", conf->eap_config->private_key);
1515 conf->eap_config->private_key = NULL;
1517 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1518 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1519 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1520 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1522 conf->eap_config->private_key_password = NULL;
1524 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1525 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1526 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1527 DBG("identity [%s]", conf->eap_config->identity);
1529 conf->eap_config->identity = NULL;
1531 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1532 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1533 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1534 DBG("eap_type [%s]", conf->eap_config->eap_type);
1536 conf->eap_config->eap_type = NULL;
1538 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1539 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1540 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1541 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1543 conf->eap_config->eap_auth_type = NULL;
1545 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1546 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1547 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1548 DBG("subject_match [%s]", conf->eap_config->subject_match);
1550 conf->eap_config->subject_match = NULL;
1554 conf->favorite = TRUE;
1555 conf->autoconnect = TRUE;
1557 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1559 __free_wifi_configuration(conf);
1560 ERR("Fail to get_wifi_config_group_name");
1564 keyfile = g_key_file_new();
1565 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1566 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1568 if (conf->passphrase != NULL) {
1569 gchar *enc_data = NULL;
1570 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1573 ERR("Failed to encrypt the passphrase");
1575 g_free(conf->passphrase);
1576 conf->passphrase = enc_data;
1578 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1581 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1582 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1584 /* Optional field */
1585 if (conf->proxy_address != NULL) {
1586 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1587 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1590 if (conf->is_hidden != NULL) {
1591 gboolean hidden = FALSE;
1592 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1594 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1597 if (conf->eap_config->anonymous_identity != NULL)
1598 g_key_file_set_string(keyfile, group_name,
1599 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1601 if (conf->eap_config->ca_cert != NULL)
1602 g_key_file_set_string(keyfile, group_name,
1603 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1605 if (conf->eap_config->client_cert != NULL)
1606 g_key_file_set_string(keyfile, group_name,
1607 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1609 if (conf->eap_config->private_key != NULL)
1610 g_key_file_set_string(keyfile, group_name,
1611 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1613 if (conf->eap_config->private_key_password != NULL)
1614 g_key_file_set_string(keyfile, group_name,
1615 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1617 if (conf->eap_config->identity != NULL)
1618 g_key_file_set_string(keyfile, group_name,
1619 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1621 if (conf->eap_config->eap_type != NULL)
1622 g_key_file_set_string(keyfile, group_name,
1623 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1625 if (conf->eap_config->eap_auth_type != NULL)
1626 g_key_file_set_string(keyfile, group_name,
1627 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1629 if (conf->eap_config->subject_match != NULL)
1630 g_key_file_set_string(keyfile, group_name,
1631 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1633 ret = _save_configuration(config_id, keyfile);
1635 INFO("Success to save eap configuration [%s]", config_id);
1636 wifi_complete_save_eap_configuration(wifi, context);
1638 INFO("Fail to save eap configuration [%s]", config_id);
1639 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1642 g_key_file_free(keyfile);
1644 __free_wifi_configuration(conf);
1646 g_variant_iter_free(iter);
1651 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1653 gboolean ret = FALSE;
1655 if ((wifi == NULL) || (config_id == NULL)) {
1656 ERR("Invalid parameter");
1657 netconfig_error_invalid_parameter(context);
1661 ret = _remove_configuration(config_id);
1663 /* no configuration or error */
1664 ERR("No [%s] configuration", config_id);
1665 netconfig_error_no_profile(context);
1669 wifi_complete_remove_configuration(wifi, context);
1673 /* config field key / value */
1675 * [wifi_macaddress_config_id]
1676 * Name=name (mandatory)
1677 * SSID=SSID (mandatory)
1678 * Frequency=2462 (X)
1680 * AutoConnect=true (Default true)
1681 * Modified=2015-03-20 (X)
1682 * IPv4.method=manual (O)
1683 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1684 * IPv6.method=auto (X)
1685 * IPv6.privacy=disabled (X)
1686 * IPv4.netmask_prefixlen=24 (X)
1687 * IPv4.local_address=192.0.0.1 (O)
1688 * IPv4.gateway=192.0.0.1 (O ? X ?)
1689 * Nameservers=192.168.43.22; (O)
1690 * Proxy.Method=manual (O)
1691 * Proxy.Servers=trst.com:8888; (O)
1693 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1694 const gchar *config_id, const gchar *key, const gchar *value)
1696 gboolean ret = FALSE;
1697 gchar *keyfile_key = NULL;
1699 g_return_val_if_fail(wifi != NULL, TRUE);
1700 g_return_val_if_fail(config_id != NULL, TRUE);
1701 g_return_val_if_fail(key != NULL, TRUE);
1703 DBG("Key[%s] Value[%d]", key, value);
1705 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1706 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1708 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1709 netconfig_error_invalid_parameter(context);
1712 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1713 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1714 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1715 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1716 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1717 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1718 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1719 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1720 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1721 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1722 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1723 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1724 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1725 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1726 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1727 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1728 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1729 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1730 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1732 ERR("Not supported key[%s]", key);
1733 netconfig_error_invalid_parameter(context);
1737 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1739 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1742 if (keyfile_key != NULL)
1743 g_free(keyfile_key);
1745 wifi_complete_set_config_field(wifi, context);
1749 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1751 gboolean ret = FALSE;
1752 gchar *passphrase = NULL;
1754 if ((wifi == NULL) || (config_id == NULL)) {
1755 ERR("Invalid parameter");
1756 netconfig_error_invalid_parameter(context);
1760 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1762 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1763 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1767 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1773 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1774 int frame_id, const gchar *vsie)
1776 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1778 g_return_val_if_fail(wifi != NULL, TRUE);
1779 g_return_val_if_fail(vsie != NULL, TRUE);
1781 gboolean ret = FALSE;
1783 ret = _add_vsie(frame_id, vsie);
1785 DBG("Failed to add vsie: %s", vsie);
1786 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1790 wifi_complete_add_vsie(wifi, context);
1794 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1797 DBG("Frame ID: [%d]", frame_id);
1799 g_return_val_if_fail(wifi != NULL, TRUE);
1801 gboolean ret = FALSE;
1804 ret = _get_vsie(frame_id, &vsie);
1806 DBG("Failed to get vsie for frame:[%d]", frame_id);
1807 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1811 DBG("Received vsie: %s", vsie);
1812 wifi_complete_get_vsie(wifi, context, vsie);
1817 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1818 int frame_id, const gchar *vsie)
1820 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1822 g_return_val_if_fail(wifi != NULL, TRUE);
1823 g_return_val_if_fail(vsie != NULL, TRUE);
1825 gboolean ret = FALSE;
1827 ret = _remove_vsie(frame_id, vsie);
1829 DBG("Failed to remove vsie: %s", vsie);
1830 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1834 wifi_complete_remove_vsie(wifi, context);