2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
37 #include "wifi-config.h"
38 #include "netsupplicant.h"
39 #include "wifi-key-encryption.h"
41 #define CONNMAN_STORAGE "/var/lib/connman"
43 #define WIFI_SECURITY_NONE "none"
44 #define WIFI_SECURITY_WEP "wep"
45 #define WIFI_SECURITY_WPA_PSK "psk"
46 #define WIFI_SECURITY_EAP "ieee8021x"
48 #define WIFI_CONFIG_PREFIX "wifi_"
49 #define MAC_ADDRESS_LENGTH 12
50 #define WIFI_PREFIX_LENGTH MAC_ADDRESS_LENGTH + 6 /* wifi_485a3f2f506a_ */
51 #define PROFILE_PREFIX_LENGTH WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
53 #define WIFI_MAC_ADD_LENGTH 17
54 #define WIFI_MAC_ADD_PATH "/sys/class/net/wlan0/address"
56 #define NET_DNS_ADDR_MAX 2
58 #define MAX_WIFI_PROFILES 200
60 struct wifi_eap_config {
61 gchar *anonymous_identity;
65 gchar *private_key_password;
75 gchar *gateway_address;
76 gchar *dns_address[NET_DNS_ADDR_MAX];
93 struct wifi_eap_config *eap_config;
94 wifi_ip_info_s *ip_info;
98 static void __free_wifi_configuration(struct wifi_config *conf)
105 g_free(conf->passphrase);
106 g_free(conf->security_type);
107 g_free(conf->is_hidden);
108 g_free(conf->proxy_address);
109 g_free(conf->last_error);
110 if (conf->eap_config) {
111 g_free(conf->eap_config->anonymous_identity);
112 g_free(conf->eap_config->ca_cert);
113 g_free(conf->eap_config->client_cert);
114 g_free(conf->eap_config->private_key);
115 g_free(conf->eap_config->private_key_password);
116 g_free(conf->eap_config->identity);
117 g_free(conf->eap_config->eap_type);
118 g_free(conf->eap_config->eap_auth_type);
119 g_free(conf->eap_config->subject_match);
120 g_free(conf->eap_config);
124 g_free(conf->ip_info->ip_type);
125 g_free(conf->ip_info->ip_address);
126 g_free(conf->ip_info->subnet_mask);
127 g_free(conf->ip_info->gateway_address);
128 g_free(conf->ip_info->dns_type);
129 conf->ip_info->prefix_length = 0;
131 int i = 0, count = conf->ip_info->dns_count;
133 g_free(conf->ip_info->dns_address[i]);
136 g_free(conf->ip_info);
141 static gboolean __get_mac_address(gchar **mac_address)
143 gchar *tmp_mac = NULL;
145 gchar mac[13] = { 0, };
150 char buf[WIFI_MAC_ADD_LENGTH + 1];
151 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
152 fp = fopen(WIFI_MAC_ADD_PATH, "r");
155 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
160 if (fgets(buf, sizeof(buf), fp) == NULL) {
161 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
166 tmp_mac = (gchar *)malloc(WIFI_MAC_ADD_LENGTH + 1);
167 if (tmp_mac == NULL) {
168 ERR("malloc() failed");
173 memset(tmp_mac, 0, WIFI_MAC_ADD_LENGTH + 1);
174 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
177 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
178 if (tmp_mac == NULL) {
179 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
184 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
186 while (tmp && tmp[i]) {
192 *mac_address = g_strdup(mac);
198 gboolean wifi_config_get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
200 gchar *mac_address = NULL;
201 gchar *g_name = NULL;
202 gboolean ret = FALSE;
204 ret = __get_mac_address(&mac_address);
205 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
206 ERR("Cannot get WIFI MAC address");
211 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
212 if (g_name == NULL) {
217 *group_name = g_strdup(g_name);
225 static gboolean __get_security_type(const gchar *config_id, gchar **type)
227 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
228 *type = g_strdup(WIFI_SECURITY_NONE);
229 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
230 *type = g_strdup(WIFI_SECURITY_WEP);
231 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
232 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
233 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
234 *type = g_strdup(WIFI_SECURITY_EAP);
243 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
245 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
246 if (*config_id == NULL) {
255 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
257 GKeyFile *keyfile = NULL;
260 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
262 keyfile = netconfig_keyfile_load(path);
264 ERR("keyfile[%s] is NULL", path);
271 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
273 gboolean ret = FALSE;
276 path = g_strdup_printf("%s/%s", pathname, filename);
277 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
279 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
288 static gboolean __remove_configuration(const gchar *pathname)
292 if (__remove_file(pathname, "settings") != TRUE) {
293 ERR("Cannot remove [%s/settings]", pathname);
296 if (__remove_file(pathname, "data") != TRUE) {
297 ERR("Cannot remove [%s/data]", pathname);
301 ret = rmdir(pathname);
303 ERR("Cannot remove [%s]", pathname);
309 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
313 gboolean hidden = FALSE;
314 gboolean ret = FALSE;
316 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
318 ERR("Fail to get_wifi_config_group_name");
322 keyfile = __get_configuration_keyfile(group_name);
323 if (keyfile == NULL) {
324 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
329 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
330 DBG("name [%s]", config->name);
332 __get_security_type(config_id, &config->security_type);
333 if (config->security_type == NULL) {
334 ERR("Fail to _get_security_type");
335 g_key_file_free(keyfile);
339 DBG("security_type [%s]", config->security_type);
341 config->passphrase = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
342 DBG("passphrase []");
344 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
345 if (config->proxy_address)
346 DBG("proxy_address [%s]", config->proxy_address);
348 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
350 config->is_hidden = g_strdup("TRUE");
352 config->is_hidden = g_strdup("FALSE");
353 DBG("is_hidden [%s]", config->is_hidden);
355 if (config->ip_info) {
356 GError *error = NULL;
357 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
358 WIFI_CONFIG_IPV4_METHOD, NULL);
359 if (config->ip_info->ip_type)
360 DBG("IPv4.Method:%s", config->ip_info->ip_type);
362 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
363 WIFI_CONFIG_IPV4_ADDRESS, NULL);
364 if (config->ip_info->ip_address)
365 DBG("IPv4.Address:%s", config->ip_info->ip_address);
369 struct in_addr netmask;
371 prefix_len = g_key_file_get_integer(keyfile, group_name,
372 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
374 DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
377 if (prefix_len > 0 && prefix_len < 32) {
378 addr = 0xffffffff << (32 - prefix_len);
379 netmask.s_addr = htonl(addr);
380 mask = inet_ntoa(netmask);
381 config->ip_info->subnet_mask = g_strdup(mask);
383 if (config->ip_info->subnet_mask)
384 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
387 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
388 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
389 if (config->ip_info->gateway_address)
390 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
392 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
393 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
394 if (config->ip_info->dns_type)
395 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
399 nameservers = g_key_file_get_string_list(keyfile, group_name,
400 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
403 config->ip_info->dns_count = length;
405 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
406 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
407 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
411 g_strfreev(nameservers);
416 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
417 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
418 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
419 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
420 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
421 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
422 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
423 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
424 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
425 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
427 if (config->eap_config->anonymous_identity)
428 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
429 if (config->eap_config->ca_cert)
430 DBG("ca_cert [%s]", config->eap_config->ca_cert);
431 if (config->eap_config->client_cert)
432 DBG("client_cert [%s]", config->eap_config->client_cert);
433 if (config->eap_config->private_key)
434 DBG("private_key [%s]", config->eap_config->private_key);
435 if (config->eap_config->private_key_password)
436 DBG("private_key_password [%s]", config->eap_config->private_key_password);
437 if (config->eap_config->identity)
438 DBG("identity [%s]", config->eap_config->identity);
439 if (config->eap_config->eap_type)
440 DBG("eap_type [%s]", config->eap_config->eap_type);
441 if (config->eap_config->eap_auth_type)
442 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
443 if (config->eap_config->subject_match)
444 DBG("subject_match [%s]", config->eap_config->subject_match);
447 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
448 if (config->last_error)
449 DBG("last_error [%s]", config->last_error);
451 g_key_file_free(keyfile);
457 gboolean wifi_config_save_configuration(const gchar *config_id, GKeyFile *keyfile)
462 gboolean ret = FALSE;
464 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
466 ERR("Fail to get_wifi_config_group_name");
470 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
471 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
472 if (__remove_configuration(dir) != TRUE) {
473 ERR("[%s] is existed, but cannot remove", dir);
480 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
481 ERR("Cannot mkdir %s", dir);
487 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
488 netconfig_keyfile_save(keyfile, path);
496 static gboolean _remove_configuration(const gchar *config_id)
498 gboolean ret = FALSE;
502 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
504 ERR("Fail to get_wifi_config_group_name");
508 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
509 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
510 if (__remove_configuration(dir) != TRUE) {
511 ERR("[%s] is existed, but cannot remove", dir);
514 INFO("Success to remove [%s]", dir);
517 ERR("[%s] is not existed", dir);
528 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
534 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
536 ERR("Fail to get_wifi_config_group_name");
539 DBG("group_name %s", group_name);
541 keyfile = __get_configuration_keyfile(group_name);
542 if (keyfile == NULL) {
543 ERR("Fail to __get_configuration_keyfile");
548 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
549 g_key_file_set_string(keyfile, group_name, key, value);
550 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
551 g_key_file_set_string(keyfile, group_name, key, value);
552 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
553 gboolean hidden = FALSE;
554 if (g_strcmp0(value, "TRUE") == 0)
556 g_key_file_set_boolean(keyfile, group_name, key, hidden);
557 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
558 g_key_file_set_string(keyfile, group_name, key, value);
559 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
560 g_key_file_set_string(keyfile, group_name, key, value);
561 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
562 g_key_file_set_string(keyfile, group_name, key, value);
563 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
564 g_key_file_set_string(keyfile, group_name, key, value);
565 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
566 g_key_file_set_string(keyfile, group_name, key, value);
567 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
568 g_key_file_set_string(keyfile, group_name, key, value);
569 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
570 g_key_file_set_string(keyfile, group_name, key, value);
571 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
572 g_key_file_set_string(keyfile, group_name, key, value);
574 ERR("key[%s] is not supported", key);
578 wifi_config_save_configuration(config_id, keyfile);
580 g_key_file_free(keyfile);
586 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
591 gboolean hidden = FALSE;
592 gboolean ret = FALSE;
594 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
596 ERR("Fail to get_wifi_config_group_name");
599 DBG("group_name %s", group_name);
601 keyfile = __get_configuration_keyfile(group_name);
602 if (keyfile == NULL) {
603 ERR("Fail to __get_configuration_keyfile");
608 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
609 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
610 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
611 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
612 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
613 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
614 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
615 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
617 val = g_strdup("TRUE");
619 val = g_strdup("FALSE");
620 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
621 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
622 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
623 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
624 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
625 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
626 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
627 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
628 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
629 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
630 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
631 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
632 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
633 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
634 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
635 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
636 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
637 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
639 ERR("Invalid key[%s]", key);
640 val = g_strdup("NOTSUPPORTED");
643 *value = g_strdup(val);
646 g_key_file_free(keyfile);
652 static GSList *_get_list(void)
655 struct dirent *dp = NULL;
658 dir = opendir(CONNMAN_STORAGE);
660 ERR("Cannot open dir %s", CONNMAN_STORAGE);
664 while ((dp = readdir(dir)) != NULL) {
665 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
666 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
669 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
670 list = g_slist_append(list, g_strdup(config_id));
678 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
680 gboolean ret = FALSE;
683 if ((service_profile == NULL) || (config_id == NULL)) {
684 ERR("Invalid parameter");
688 ret = __get_config_id(service_profile, &val);
689 *config_id = g_strdup(val);
695 gboolean wifi_config_remove_configuration(const gchar *config_id)
697 gboolean ret = FALSE;
699 ret = _remove_configuration(config_id);
704 int __netconfig_hex_char_to_num(char c)
706 if (c >= '0' && c <= '9')
709 if (c >= 'a' && c <= 'f')
712 if (c >= 'A' && c <= 'F')
718 int __netconfig_hex_to_byte(const char *hex)
722 a = __netconfig_hex_char_to_num(*hex++);
726 b = __netconfig_hex_char_to_num(*hex++);
733 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
737 const char *ipos = hex;
738 unsigned char *opos = buf;
740 for (i = 0; i < len; i++) {
741 a = __netconfig_hex_to_byte(ipos);
752 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
759 ERR("Invalid parameter.");
763 *dst = (char *) g_try_malloc0((2*src_len)+1);
765 ERR("failed to allocate memory to buffer.");
771 for (i = 0; i < src_len; i++) {
772 snprintf(buf, 3, "%02x", src[i]);
780 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
782 GVariantIter *iter_copy = NULL;
785 unsigned char *tmp_dst = NULL;
787 if (!dst || *dst || !iter) {
788 ERR("Invalid parameter");
792 iter_copy = g_variant_iter_copy(iter);
794 while (g_variant_iter_loop(iter, "y", &tmp))
796 g_variant_iter_free(iter);
798 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
800 ERR("failed to allocate memory");
801 g_variant_iter_free(iter_copy);
806 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
808 g_variant_iter_free(iter_copy);
814 tmp_dst[length] = '\0';
818 DBG("Length [%d]", length);
822 gboolean _add_vsie(int frame_id, const char* vsie)
824 GVariant *params = NULL;
825 GVariant *message = NULL;
826 GVariantBuilder *bytearray_builder = NULL;
831 unsigned char *bytearray = NULL;
832 size_t bytearray_len = 0;
834 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
835 DBG("Invalid parameter, frame-id: %d", frame_id);
839 vsie_len = strlen(vsie);
841 DBG("vsie length is zero");
845 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
847 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
848 if (bytearray == NULL) {
849 DBG("Failed to allocate memory to bytearray");
853 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
854 DBG("invalid vsie string");
859 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
860 for (i = 0; i < bytearray_len; i++)
861 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
863 params = g_variant_new("(iay)", frame_id, bytearray_builder);
864 g_variant_builder_unref(bytearray_builder);
866 if_path = netconfig_wifi_get_supplicant_interface();
868 if (if_path == NULL) {
869 ERR("Fail to get wpa_supplicant DBus path");
874 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
875 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
878 if (message == NULL) {
879 ERR("Failed to send command to wpa_supplicant");
884 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
890 gboolean _get_vsie(int frame_id, char **vsie)
892 GVariant *params = NULL;
893 GVariant *message = NULL;
896 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
897 DBG("Invalid parameter, frame-id: %d", frame_id);
901 if_path = netconfig_wifi_get_supplicant_interface();
902 if (if_path == NULL) {
903 ERR("Fail to get wpa_supplicant DBus path");
907 params = g_variant_new("(i)", frame_id);
909 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
910 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
913 if (message == NULL) {
914 ERR("Failed to send command to wpa_supplicant");
917 GVariantIter *iter = NULL;
918 unsigned char *vsie_bytes = NULL;
922 g_variant_get(message, "(ay)", &iter);
924 ERR("vsie is not present");
928 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
929 if (vsie_bytes == NULL) {
930 ERR("vsie_bytes not allocated");
934 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
937 ERR("vsie not allocated.");
944 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
950 gboolean _remove_vsie(int frame_id, const char *vsie)
952 GVariant *params = NULL;
953 GVariant *message = NULL;
954 GVariantBuilder *bytearray_builder = NULL;
959 unsigned char *bytearray = NULL;
960 size_t bytearray_len = 0;
962 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
963 DBG("Invalid parameter, frame-id: %d", frame_id);
967 vsie_len = strlen(vsie);
969 DBG("vsie length is zero");
973 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
975 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
976 if (bytearray == NULL) {
977 DBG("Failed to allocate memory to bytearray");
981 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
982 DBG("invalid vsie string");
987 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
988 for (i = 0; i < bytearray_len; i++)
989 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
991 params = g_variant_new("(iay)", frame_id, bytearray_builder);
992 g_variant_builder_unref(bytearray_builder);
994 if_path = netconfig_wifi_get_supplicant_interface();
995 if (if_path == NULL) {
996 ERR("Fail to get wpa_supplicant DBus path");
1001 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
1002 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
1005 if (message == NULL) {
1006 ERR("Failed to send command to wpa_supplicant");
1011 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1018 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
1021 GSList *config_ids = NULL;
1023 gchar **result = NULL;
1025 g_return_val_if_fail(wifi != NULL, TRUE);
1027 config_ids = _get_list();
1028 if (config_ids == NULL) {
1029 ERR("Fail to get config list");
1030 netconfig_error_no_profile(context);
1034 length = g_slist_length(config_ids);
1035 result = g_new0(gchar *, length + 1);
1036 for (i = 0; i < length; i++) {
1037 gchar *config_id = g_slist_nth_data(config_ids, i);
1038 result[i] = g_strdup(config_id);
1041 config_ids = g_slist_nth(config_ids, 0);
1042 g_slist_free_full(config_ids, g_free);
1044 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1046 for (i = 0; i < length; i++)
1056 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1057 const gchar *config_id)
1059 gboolean ret = FALSE;
1060 GVariantBuilder *b = NULL;
1061 struct wifi_config *conf = NULL;
1063 g_return_val_if_fail(wifi != NULL, TRUE);
1065 conf = g_new0(struct wifi_config, 1);
1066 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1068 ret = _load_configuration(config_id, conf);
1070 g_free(conf->ip_info);
1072 ERR("Fail to _load_configuration");
1073 netconfig_error_no_profile(context);
1077 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1078 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1079 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1080 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PASSPHRASE, g_variant_new_string(conf->passphrase));
1081 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1083 if (conf->proxy_address != NULL)
1084 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1086 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1088 if (conf->ip_info->ip_type != NULL)
1089 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1091 if (conf->ip_info->ip_address != NULL)
1092 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1094 if (conf->ip_info->subnet_mask != NULL)
1095 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1097 if (conf->ip_info->prefix_length > 0)
1098 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV6_PREFIX_LEN, g_variant_new_int32(conf->ip_info->prefix_length));
1100 if (conf->ip_info->gateway_address != NULL)
1101 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1103 if (conf->ip_info->dns_type != NULL)
1104 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1106 int i = 0, count = conf->ip_info->dns_count;
1108 if (conf->ip_info->dns_address[i] != NULL)
1109 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1114 if (conf->last_error != NULL)
1115 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1117 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1119 __free_wifi_configuration(conf);
1121 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1122 g_variant_builder_unref(b);
1126 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1127 const char *netmask)
1136 mask = inet_network(netmask);
1139 /* a valid netmask must be 2^n - 1 */
1140 if ((host & (host + 1)) != 0)
1144 for (; mask; mask <<= 1)
1150 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1151 const gchar *config_id, GVariant *configuration)
1153 gboolean ret = FALSE;
1154 struct wifi_config *conf = NULL;
1155 GKeyFile *keyfile = NULL;
1159 gchar *group_name = NULL;
1162 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1163 ERR("Invalid parameter");
1164 netconfig_error_invalid_parameter(context);
1168 conf = g_new0(struct wifi_config, 1);
1169 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1171 g_variant_get(configuration, "a{sv}", &iter);
1172 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1173 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1174 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1175 conf->name = g_strdup(g_variant_get_string(value, NULL));
1176 DBG("name [%s]", conf->name);
1180 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1181 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1182 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1183 DBG("ssid [%s]", conf->ssid);
1187 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1188 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1189 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1190 DBG("passphrase []");
1192 conf->passphrase = NULL;
1194 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1195 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1196 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1197 DBG("is_hidden [%s]", conf->is_hidden);
1199 conf->is_hidden = NULL;
1201 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1202 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1203 conf->is_created = g_variant_get_boolean(value);
1204 DBG("is_created [%d]", conf->is_created);
1206 conf->is_created = FALSE;
1208 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1209 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1210 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1211 DBG("IP config type [%s]", conf->ip_info->ip_type);
1213 conf->ip_info->ip_type = NULL;
1215 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1216 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1217 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1218 DBG("IP address [%s]", conf->ip_info->ip_address);
1220 conf->ip_info->ip_address = NULL;
1222 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1223 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1224 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1225 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1227 conf->ip_info->subnet_mask = NULL;
1229 } else if (g_strcmp0(field, WIFI_CONFIG_IPV6_PREFIX_LEN) == 0) {
1230 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1231 conf->ip_info->prefix_length = g_variant_get_int32(value);
1232 DBG("IPv6 Prefix Length [%d]", conf->ip_info->prefix_length);
1234 conf->ip_info->prefix_length = 0;
1236 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1237 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1238 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1239 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1241 conf->ip_info->gateway_address = NULL;
1243 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1244 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1245 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1246 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1248 conf->ip_info->dns_type = NULL;
1250 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1251 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1252 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1253 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1254 conf->ip_info->dns_count = order + 1;
1257 conf->ip_info->dns_address[order++] = NULL;
1259 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1260 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1261 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1262 DBG("proxy_address [%s]", conf->proxy_address);
1264 conf->proxy_address = NULL;
1268 conf->favorite = TRUE;
1269 conf->autoconnect = TRUE;
1271 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1273 __free_wifi_configuration(conf);
1274 ERR("Fail to get_wifi_config_group_name");
1275 netconfig_error_fail_save_congifuration(context);
1279 keyfile = g_key_file_new();
1280 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1281 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1283 if (conf->passphrase != NULL) {
1284 gchar *enc_data = NULL;
1286 if (conf->is_created == true)
1287 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1289 enc_data = g_strdup(conf->passphrase);
1292 ERR("Failed to encrypt the passphrase");
1294 g_free(conf->passphrase);
1295 conf->passphrase = enc_data;
1298 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1301 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1302 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1304 #if GLIB_CHECK_VERSION(2,62,0)
1305 gint64 real_time = 0;
1306 GDateTime *dt_real_time = NULL;
1308 real_time = g_get_real_time();
1309 dt_real_time = g_date_time_new_from_unix_utc(real_time);
1311 gchar *str = g_date_time_format_iso8601(dt_real_time);
1312 g_date_time_unref(dt_real_time);
1314 g_key_file_set_string(keyfile, group_name,
1315 WIFI_CONFIG_MODIFIED, str);
1319 #else /* GLIB_CHECK_VERSION(2,62,0) */
1321 g_get_current_time(&modified);
1322 gchar *str = g_time_val_to_iso8601(&modified);
1324 g_key_file_set_string(keyfile, group_name,
1325 WIFI_CONFIG_MODIFIED, str);
1328 #endif /* GLIB_CHECK_VERSION(2,62,0) */
1330 /* Optional field */
1331 if (conf->proxy_address != NULL) {
1332 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1333 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1336 if (conf->is_hidden != NULL) {
1337 gboolean hidden = FALSE;
1338 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1340 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1343 if (conf->ip_info->ip_type != NULL)
1344 g_key_file_set_string(keyfile, group_name,
1345 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1347 if (conf->ip_info->ip_address != NULL)
1348 g_key_file_set_string(keyfile, group_name,
1349 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1351 if (conf->ip_info->subnet_mask != NULL) {
1352 unsigned char prefix_len;
1353 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1354 conf->ip_info->subnet_mask);
1355 if (prefix_len > 0 && prefix_len < 32)
1356 g_key_file_set_integer(keyfile, group_name,
1357 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1360 if (conf->ip_info->prefix_length > 0)
1361 g_key_file_set_integer(keyfile, group_name,
1362 WIFI_CONFIG_IPV6_PREFIX_LEN, conf->ip_info->prefix_length);
1364 if (conf->ip_info->gateway_address != NULL)
1365 g_key_file_set_string(keyfile, group_name,
1366 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1368 if (conf->ip_info->dns_type != NULL)
1369 g_key_file_set_string(keyfile, group_name,
1370 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1372 int i = 0, count = conf->ip_info->dns_count;
1374 if (conf->ip_info->dns_address[i] != NULL)
1375 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1376 conf->ip_info->dns_address[i]);
1381 ret = wifi_config_save_configuration(config_id, keyfile);
1383 INFO("Success to save configuration [%s]", config_id);
1384 wifi_complete_save_configuration(wifi, context);
1386 if (get_files_count(CONNMAN_STORAGE) > MAX_WIFI_PROFILES) {
1387 file = get_least_recently_profile(CONNMAN_STORAGE);
1389 gchar *profileName = g_strdup_printf(CONNMAN_STORAGE "/%s", file);
1390 INFO("least modified file: %s", profileName);
1392 if (__remove_configuration(profileName) != TRUE)
1393 DBG("Failed to remove profile: [%s]", profileName);
1395 ERR("Profile: [%s] does not exist", file);
1397 g_free(profileName);
1401 INFO("Fail to save configuration [%s]", config_id);
1402 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1405 g_key_file_free(keyfile);
1407 __free_wifi_configuration(conf);
1409 g_variant_iter_free(iter);
1414 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1415 const gchar *config_id)
1417 gboolean ret = FALSE;
1418 GVariantBuilder *b = NULL;
1419 struct wifi_config *conf = NULL;
1421 g_return_val_if_fail(wifi != NULL, TRUE);
1423 conf = g_new0(struct wifi_config, 1);
1424 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1425 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1427 ret = _load_configuration(config_id, conf);
1429 g_free(conf->eap_config);
1430 g_free(conf->ip_info);
1432 ERR("Fail to _load_configuration");
1433 netconfig_error_no_profile(context);
1437 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1438 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1439 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1440 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1441 if (conf->proxy_address != NULL)
1442 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1444 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1446 if (conf->last_error != NULL)
1447 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1449 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1451 if (conf->eap_config != NULL) {
1452 if (conf->eap_config->anonymous_identity != NULL)
1453 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1455 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1457 if (conf->eap_config->ca_cert != NULL)
1458 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1460 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1462 if (conf->eap_config->client_cert != NULL)
1463 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1465 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1467 if (conf->eap_config->private_key != NULL)
1468 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1470 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1472 if (conf->eap_config->private_key_password != NULL)
1473 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1475 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1477 if (conf->eap_config->identity != NULL)
1478 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1480 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1482 if (conf->eap_config->eap_type != NULL)
1483 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1485 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1487 if (conf->eap_config->eap_auth_type != NULL)
1488 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1490 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1492 if (conf->eap_config->subject_match != NULL)
1493 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1495 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1498 __free_wifi_configuration(conf);
1500 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1501 g_variant_builder_unref(b);
1505 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1506 const gchar *config_id, GVariant *configuration)
1508 gboolean ret = FALSE;
1509 struct wifi_config *conf = NULL;
1510 GKeyFile *keyfile = NULL;
1514 gchar *group_name = NULL;
1516 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1517 ERR("Invalid parameter");
1518 netconfig_error_invalid_parameter(context);
1522 conf = g_new0(struct wifi_config, 1);
1523 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1525 g_variant_get(configuration, "a{sv}", &iter);
1526 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1527 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1528 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1529 conf->name = g_strdup(g_variant_get_string(value, NULL));
1530 DBG("name [%s]", conf->name);
1534 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1535 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1536 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1537 DBG("ssid [%s]", conf->ssid);
1541 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1542 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1543 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1544 DBG("passphrase [%s]", conf->passphrase);
1546 conf->passphrase = NULL;
1548 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1549 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1550 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1551 DBG("is_hidden [%s]", conf->is_hidden);
1553 conf->is_hidden = NULL;
1555 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1556 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1557 conf->is_created = g_variant_get_boolean(value);
1558 DBG("is_created [%d]", conf->is_created);
1560 conf->is_created = FALSE;
1562 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1563 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1564 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1565 DBG("proxy_address [%s]", conf->proxy_address);
1567 conf->proxy_address = NULL;
1569 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1570 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1571 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1572 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1574 conf->eap_config->anonymous_identity = NULL;
1576 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1577 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1578 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1579 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1581 conf->eap_config->ca_cert = NULL;
1583 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1584 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1585 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1586 DBG("client_cert [%s]", conf->eap_config->client_cert);
1588 conf->eap_config->client_cert = NULL;
1590 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1591 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1592 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1593 DBG("private_key [%s]", conf->eap_config->private_key);
1595 conf->eap_config->private_key = NULL;
1597 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1598 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1599 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1600 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1602 conf->eap_config->private_key_password = NULL;
1604 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1605 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1606 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1607 DBG("identity [%s]", conf->eap_config->identity);
1609 conf->eap_config->identity = NULL;
1611 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1612 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1613 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1614 DBG("eap_type [%s]", conf->eap_config->eap_type);
1616 conf->eap_config->eap_type = NULL;
1618 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1619 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1620 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1621 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1623 conf->eap_config->eap_auth_type = NULL;
1625 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1626 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1627 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1628 DBG("subject_match [%s]", conf->eap_config->subject_match);
1630 conf->eap_config->subject_match = NULL;
1634 conf->favorite = TRUE;
1635 conf->autoconnect = TRUE;
1637 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1639 __free_wifi_configuration(conf);
1640 ERR("Fail to get_wifi_config_group_name");
1644 keyfile = g_key_file_new();
1645 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1646 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1648 if (conf->passphrase != NULL) {
1649 gchar *enc_data = NULL;
1651 if (conf->is_created == true)
1652 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1654 enc_data = g_strdup(conf->passphrase);
1657 ERR("Failed to encrypt the passphrase");
1659 g_free(conf->passphrase);
1660 conf->passphrase = enc_data;
1662 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1665 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1666 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1668 /* Optional field */
1669 if (conf->proxy_address != NULL) {
1670 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1671 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1674 if (conf->is_hidden != NULL) {
1675 gboolean hidden = FALSE;
1676 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1678 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1681 if (conf->eap_config->anonymous_identity != NULL)
1682 g_key_file_set_string(keyfile, group_name,
1683 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1685 if (conf->eap_config->ca_cert != NULL)
1686 g_key_file_set_string(keyfile, group_name,
1687 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1689 if (conf->eap_config->client_cert != NULL)
1690 g_key_file_set_string(keyfile, group_name,
1691 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1693 if (conf->eap_config->private_key != NULL)
1694 g_key_file_set_string(keyfile, group_name,
1695 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1697 if (conf->eap_config->private_key_password != NULL)
1698 g_key_file_set_string(keyfile, group_name,
1699 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1701 if (conf->eap_config->identity != NULL)
1702 g_key_file_set_string(keyfile, group_name,
1703 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1705 if (conf->eap_config->eap_type != NULL)
1706 g_key_file_set_string(keyfile, group_name,
1707 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1709 if (conf->eap_config->eap_auth_type != NULL)
1710 g_key_file_set_string(keyfile, group_name,
1711 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1713 if (conf->eap_config->subject_match != NULL)
1714 g_key_file_set_string(keyfile, group_name,
1715 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1717 ret = wifi_config_save_configuration(config_id, keyfile);
1719 INFO("Success to save eap configuration [%s]", config_id);
1720 wifi_complete_save_eap_configuration(wifi, context);
1722 INFO("Fail to save eap configuration [%s]", config_id);
1723 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1726 g_key_file_free(keyfile);
1728 __free_wifi_configuration(conf);
1730 g_variant_iter_free(iter);
1735 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1737 gboolean ret = FALSE;
1739 if ((wifi == NULL) || (config_id == NULL)) {
1740 ERR("Invalid parameter");
1741 netconfig_error_invalid_parameter(context);
1745 ret = _remove_configuration(config_id);
1747 /* no configuration or error */
1748 ERR("No [%s] configuration", config_id);
1749 netconfig_error_no_profile(context);
1753 wifi_complete_remove_configuration(wifi, context);
1757 /* config field key / value */
1759 * [wifi_macaddress_config_id]
1760 * Name=name (mandatory)
1761 * SSID=SSID (mandatory)
1762 * Frequency=2462 (X)
1764 * AutoConnect=true (Default true)
1765 * Modified=2015-03-20 (X)
1766 * IPv4.method=manual (O)
1767 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1768 * IPv6.method=auto (X)
1769 * IPv6.privacy=disabled (X)
1770 * IPv4.netmask_prefixlen=24 (X)
1771 * IPv4.local_address=192.0.0.1 (O)
1772 * IPv4.gateway=192.0.0.1 (O ? X ?)
1773 * Nameservers=192.168.43.22; (O)
1774 * Proxy.Method=manual (O)
1775 * Proxy.Servers=trst.com:8888; (O)
1777 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1778 const gchar *config_id, const gchar *key, const gchar *value)
1780 gboolean ret = FALSE;
1781 gchar *keyfile_key = NULL;
1783 g_return_val_if_fail(wifi != NULL, TRUE);
1784 g_return_val_if_fail(config_id != NULL, TRUE);
1785 g_return_val_if_fail(key != NULL, TRUE);
1787 DBG("Key[%s] Value[%s]", key, value);
1789 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1790 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1792 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1793 netconfig_error_invalid_parameter(context);
1796 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1797 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1798 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1799 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1800 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1801 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1802 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1803 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1804 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1805 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1806 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1807 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1808 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1809 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1810 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1811 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1812 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1813 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1814 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1816 ERR("Not supported key[%s]", key);
1817 netconfig_error_invalid_parameter(context);
1821 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1823 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1826 if (keyfile_key != NULL)
1827 g_free(keyfile_key);
1829 wifi_complete_set_config_field(wifi, context);
1833 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1835 gboolean ret = FALSE;
1836 gchar *passphrase = NULL;
1838 if ((wifi == NULL) || (config_id == NULL)) {
1839 ERR("Invalid parameter");
1840 netconfig_error_invalid_parameter(context);
1844 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1846 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1847 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1851 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1857 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1858 int frame_id, const gchar *vsie)
1860 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1862 g_return_val_if_fail(wifi != NULL, TRUE);
1863 g_return_val_if_fail(vsie != NULL, TRUE);
1865 gboolean ret = FALSE;
1867 ret = _add_vsie(frame_id, vsie);
1869 DBG("Failed to add vsie: %s", vsie);
1870 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1874 wifi_complete_add_vsie(wifi, context);
1878 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1881 DBG("Frame ID: [%d]", frame_id);
1883 g_return_val_if_fail(wifi != NULL, TRUE);
1885 gboolean ret = FALSE;
1888 ret = _get_vsie(frame_id, &vsie);
1890 DBG("Failed to get vsie for frame:[%d]", frame_id);
1891 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1895 DBG("Received vsie: %s", vsie);
1896 wifi_complete_get_vsie(wifi, context, vsie);
1901 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1902 int frame_id, const gchar *vsie)
1904 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1906 g_return_val_if_fail(wifi != NULL, TRUE);
1907 g_return_val_if_fail(vsie != NULL, TRUE);
1909 gboolean ret = FALSE;
1911 ret = _remove_vsie(frame_id, vsie);
1913 DBG("Failed to remove vsie: %s", vsie);
1914 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1918 wifi_complete_remove_vsie(wifi, context);