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 "wifi-state.h"
39 #include "netsupplicant.h"
40 #include "wifi-key-encryption.h"
42 #define CONNMAN_STORAGE "/var/lib/connman"
44 #define WIFI_SECURITY_NONE "none"
45 #define WIFI_SECURITY_WEP "wep"
46 #define WIFI_SECURITY_WPA_PSK "psk"
47 #define WIFI_SECURITY_EAP "ieee8021x"
49 #define WIFI_CONFIG_PREFIX "wifi_"
50 #define MAC_ADDRESS_LENGTH 12
51 #define WIFI_PREFIX_LENGTH MAC_ADDRESS_LENGTH + 6 /* wifi_485a3f2f506a_ */
52 #define PROFILE_PREFIX_LENGTH WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
54 #define NET_DNS_ADDR_MAX 2
56 #define MAX_WIFI_PROFILES 200
58 struct wifi_eap_config {
59 gchar *anonymous_identity;
63 gchar *private_key_password;
73 gchar *gateway_address;
74 gchar *dns_address[NET_DNS_ADDR_MAX];
91 struct wifi_eap_config *eap_config;
92 wifi_ip_info_s *ip_info;
97 static void __free_wifi_configuration(struct wifi_config *conf)
104 g_free(conf->passphrase);
105 g_free(conf->security_type);
106 g_free(conf->is_hidden);
107 g_free(conf->proxy_address);
108 g_free(conf->last_error);
109 if (conf->eap_config) {
110 g_free(conf->eap_config->anonymous_identity);
111 g_free(conf->eap_config->ca_cert);
112 g_free(conf->eap_config->client_cert);
113 g_free(conf->eap_config->private_key);
114 g_free(conf->eap_config->private_key_password);
115 g_free(conf->eap_config->identity);
116 g_free(conf->eap_config->eap_type);
117 g_free(conf->eap_config->eap_auth_type);
118 g_free(conf->eap_config->subject_match);
119 g_free(conf->eap_config);
123 g_free(conf->ip_info->ip_type);
124 g_free(conf->ip_info->ip_address);
125 g_free(conf->ip_info->subnet_mask);
126 g_free(conf->ip_info->gateway_address);
127 g_free(conf->ip_info->dns_type);
128 conf->ip_info->prefix_length = 0;
130 int i = 0, count = conf->ip_info->dns_count;
132 g_free(conf->ip_info->dns_address[i]);
135 g_free(conf->ip_info);
140 static gboolean __get_mac_address(const gchar *interface_name, gchar **mac_address)
143 char buf[WIFI_MAC_ADDR_LENGTH + 1];
144 char path[WIFI_MAC_PATH_LENGTH];
145 gchar *tmp_mac = NULL;
147 gchar mac[13] = { 0, };
150 snprintf(path, WIFI_MAC_PATH_LENGTH, WIFI_MAC_ADDR_PATH, interface_name);
152 if (0 == access(path, F_OK))
153 fp = fopen(path, "r");
156 if (fgets(buf, sizeof(buf), fp) == NULL) {
157 ERR("Failed to get MAC info from %s\n", path);
162 tmp_mac = (gchar *)malloc(WIFI_MAC_ADDR_LENGTH + 1);
163 if (tmp_mac == NULL) {
164 ERR("malloc() failed");
169 memset(tmp_mac, 0, WIFI_MAC_ADDR_LENGTH + 1);
170 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADDR_LENGTH + 1);
173 ERR("Failed to open file %s\n", path);
175 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
176 if (tmp_mac == NULL) {
177 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
183 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
185 while (tmp && tmp[i]) {
191 *mac_address = g_strdup(mac);
197 gboolean wifi_config_get_group_name(const gchar *prefix,
198 const gchar *interface_name, 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(interface_name, &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 *interface_name,
310 const gchar *config_id, struct wifi_config *config)
314 gboolean hidden = FALSE;
315 gboolean ret = FALSE;
317 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
318 interface_name, config_id, &group_name);
320 ERR("Fail to get_wifi_config_group_name");
324 keyfile = __get_configuration_keyfile(group_name);
325 if (keyfile == NULL) {
326 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
331 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
332 DBG("name [%s]", config->name);
334 __get_security_type(config_id, &config->security_type);
335 if (config->security_type == NULL) {
336 ERR("Fail to _get_security_type");
337 g_key_file_free(keyfile);
341 DBG("security_type [%s]", config->security_type);
343 config->passphrase = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
344 DBG("passphrase []");
346 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
347 if (config->proxy_address)
348 DBG("proxy_address [%s]", config->proxy_address);
350 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
352 config->is_hidden = g_strdup("TRUE");
354 config->is_hidden = g_strdup("FALSE");
355 DBG("is_hidden [%s]", config->is_hidden);
357 config->frequency = g_key_file_get_integer(keyfile, group_name, WIFI_CONFIG_FREQUENCY, NULL);
358 if (config->frequency)
359 DBG("Frequency [%d]", config->frequency);
361 if (config->ip_info) {
362 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
363 WIFI_CONFIG_IPV4_METHOD, NULL);
364 if (config->ip_info->ip_type)
365 DBG("IPv4.Method:%s", config->ip_info->ip_type);
367 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
368 WIFI_CONFIG_IPV4_ADDRESS, NULL);
369 if (config->ip_info->ip_address)
370 DBG("IPv4.Address:%s", config->ip_info->ip_address);
374 struct in_addr netmask;
376 GError *error = NULL;
377 prefix_len = g_key_file_get_integer(keyfile, group_name,
378 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
380 DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
383 if (prefix_len > 0 && prefix_len < 32) {
384 addr = 0xffffffff << (32 - prefix_len);
385 netmask.s_addr = htonl(addr);
386 mask = inet_ntoa(netmask);
387 config->ip_info->subnet_mask = g_strdup(mask);
389 if (config->ip_info->subnet_mask)
390 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
393 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
394 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
395 if (config->ip_info->gateway_address)
396 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
398 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
399 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
400 if (config->ip_info->dns_type)
401 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
405 nameservers = g_key_file_get_string_list(keyfile, group_name,
406 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
409 config->ip_info->dns_count = length;
411 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
412 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
413 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
417 g_strfreev(nameservers);
422 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
423 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
424 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
425 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
426 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
427 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
428 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
429 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
430 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
431 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
433 if (config->eap_config->anonymous_identity)
434 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
435 if (config->eap_config->ca_cert)
436 DBG("ca_cert [%s]", config->eap_config->ca_cert);
437 if (config->eap_config->client_cert)
438 DBG("client_cert [%s]", config->eap_config->client_cert);
439 if (config->eap_config->private_key)
440 DBG("private_key [%s]", config->eap_config->private_key);
441 if (config->eap_config->private_key_password)
442 DBG("private_key_password [%s]", config->eap_config->private_key_password);
443 if (config->eap_config->identity)
444 DBG("identity [%s]", config->eap_config->identity);
445 if (config->eap_config->eap_type)
446 DBG("eap_type [%s]", config->eap_config->eap_type);
447 if (config->eap_config->eap_auth_type)
448 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
449 if (config->eap_config->subject_match)
450 DBG("subject_match [%s]", config->eap_config->subject_match);
453 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
454 if (config->last_error)
455 DBG("last_error [%s]", config->last_error);
457 g_key_file_free(keyfile);
463 gboolean wifi_config_save_configuration(const gchar *interface_name,
464 const gchar *config_id, GKeyFile *keyfile)
469 gboolean ret = FALSE;
471 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
472 interface_name, config_id, &group_name);
474 ERR("Fail to get_wifi_config_group_name");
478 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
479 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
480 if (__remove_configuration(dir) != TRUE) {
481 ERR("[%s] is existed, but cannot remove", dir);
488 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
489 ERR("Cannot mkdir %s", dir);
495 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
496 netconfig_keyfile_save(keyfile, path);
504 static gboolean _remove_configuration(const gchar *interface_name, const gchar *config_id)
506 gboolean ret = FALSE;
510 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
511 interface_name, config_id, &group_name);
513 ERR("Fail to get_wifi_config_group_name");
517 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
518 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
519 if (__remove_configuration(dir) != TRUE) {
520 ERR("[%s] is existed, but cannot remove", dir);
523 INFO("Success to remove [%s]", dir);
526 ERR("[%s] is not existed", dir);
537 static gboolean _set_field(const gchar *interface_name,
538 const gchar *config_id, const gchar *key, const gchar *value)
544 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
545 interface_name, config_id, &group_name);
547 ERR("Fail to get_wifi_config_group_name");
550 DBG("group_name %s", group_name);
552 keyfile = __get_configuration_keyfile(group_name);
553 if (keyfile == NULL) {
554 ERR("Fail to __get_configuration_keyfile");
559 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
560 g_key_file_set_string(keyfile, group_name, key, value);
561 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
562 g_key_file_set_string(keyfile, group_name, key, value);
563 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
564 gboolean hidden = FALSE;
565 if (g_strcmp0(value, "TRUE") == 0)
567 g_key_file_set_boolean(keyfile, group_name, key, hidden);
568 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
569 g_key_file_set_string(keyfile, group_name, key, value);
570 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
571 g_key_file_set_string(keyfile, group_name, key, value);
572 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
573 g_key_file_set_string(keyfile, group_name, key, value);
574 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
575 g_key_file_set_string(keyfile, group_name, key, value);
576 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
577 g_key_file_set_string(keyfile, group_name, key, value);
578 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
579 g_key_file_set_string(keyfile, group_name, key, value);
580 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
581 g_key_file_set_string(keyfile, group_name, key, value);
582 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
583 g_key_file_set_string(keyfile, group_name, key, value);
585 ERR("key[%s] is not supported", key);
589 wifi_config_save_configuration(interface_name, config_id, keyfile);
591 g_key_file_free(keyfile);
597 static gboolean _get_field(const gchar *interface_name,
598 const gchar *config_id, const gchar *key, gchar **value)
603 gboolean hidden = FALSE;
604 gboolean ret = FALSE;
606 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
607 interface_name, config_id, &group_name);
609 ERR("Fail to get_wifi_config_group_name");
612 DBG("group_name %s", group_name);
614 keyfile = __get_configuration_keyfile(group_name);
615 if (keyfile == NULL) {
616 ERR("Fail to __get_configuration_keyfile");
621 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
622 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
623 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
624 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
625 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
626 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
627 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
628 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
630 val = g_strdup("TRUE");
632 val = g_strdup("FALSE");
633 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
634 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
635 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
636 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
637 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
638 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
639 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
640 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
641 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
642 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
643 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
644 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
645 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
646 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
647 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
648 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
649 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
650 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
652 ERR("Invalid key[%s]", key);
653 val = g_strdup("NOTSUPPORTED");
656 *value = g_strdup(val);
659 g_key_file_free(keyfile);
665 static GSList *_get_list(const char *mac_addr)
668 struct dirent *dp = NULL;
671 dir = opendir(CONNMAN_STORAGE);
673 ERR("Cannot open dir %s", CONNMAN_STORAGE);
677 while ((dp = readdir(dir)) != NULL) {
678 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
679 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
683 DBG("%s", dp->d_name);
685 if (netconfig_check_mac_address(dp->d_name, mac_addr)) {
686 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
687 DBG("%s", config_id);
688 list = g_slist_append(list, g_strdup(config_id));
697 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
699 gboolean ret = FALSE;
702 if ((service_profile == NULL) || (config_id == NULL)) {
703 ERR("Invalid parameter");
707 ret = __get_config_id(service_profile, &val);
708 *config_id = g_strdup(val);
714 gboolean wifi_config_remove_configuration(const gchar *interface_name,
715 const gchar *config_id)
717 gboolean ret = FALSE;
719 ret = _remove_configuration(interface_name, config_id);
724 int __netconfig_hex_char_to_num(char c)
726 if (c >= '0' && c <= '9')
729 if (c >= 'a' && c <= 'f')
732 if (c >= 'A' && c <= 'F')
738 int __netconfig_hex_to_byte(const char *hex)
742 a = __netconfig_hex_char_to_num(*hex++);
746 b = __netconfig_hex_char_to_num(*hex++);
753 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
757 const char *ipos = hex;
758 unsigned char *opos = buf;
760 for (i = 0; i < len; i++) {
761 a = __netconfig_hex_to_byte(ipos);
772 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
779 ERR("Invalid parameter.");
783 *dst = (char *) g_try_malloc0((2*src_len)+1);
785 ERR("failed to allocate memory to buffer.");
791 for (i = 0; i < src_len; i++) {
792 snprintf(buf, 3, "%02x", src[i]);
800 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
802 GVariantIter *iter_copy = NULL;
805 unsigned char *tmp_dst = NULL;
807 if (!dst || *dst || !iter) {
808 ERR("Invalid parameter");
812 iter_copy = g_variant_iter_copy(iter);
814 while (g_variant_iter_loop(iter, "y", &tmp))
816 g_variant_iter_free(iter);
818 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
820 ERR("failed to allocate memory");
821 g_variant_iter_free(iter_copy);
826 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
828 g_variant_iter_free(iter_copy);
834 tmp_dst[length] = '\0';
838 DBG("Length [%d]", length);
842 gboolean _add_vsie(const char *interface_name, int frame_id, const char* vsie)
844 GVariant *params = NULL;
845 GVariant *message = NULL;
846 GVariantBuilder *bytearray_builder = NULL;
851 unsigned char *bytearray = NULL;
852 size_t bytearray_len = 0;
854 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
855 DBG("Invalid parameter, frame-id: %d", frame_id);
859 vsie_len = strlen(vsie);
861 DBG("vsie length is zero");
865 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
867 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
868 if (bytearray == NULL) {
869 DBG("Failed to allocate memory to bytearray");
873 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
874 DBG("invalid vsie string");
879 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
880 for (i = 0; i < bytearray_len; i++)
881 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
883 params = g_variant_new("(iay)", frame_id, bytearray_builder);
884 g_variant_builder_unref(bytearray_builder);
886 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
887 if (if_path == NULL) {
888 ERR("Fail to get wpa_supplicant DBus path");
893 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
894 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
897 if (message == NULL) {
898 ERR("Failed to send command to wpa_supplicant");
903 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
909 gboolean _get_vsie(const char *interface_name, int frame_id, char **vsie)
911 GVariant *params = NULL;
912 GVariant *message = NULL;
915 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
916 DBG("Invalid parameter, frame-id: %d", frame_id);
920 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
921 if (if_path == NULL) {
922 ERR("Fail to get wpa_supplicant DBus path");
926 params = g_variant_new("(i)", frame_id);
928 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
929 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
932 if (message == NULL) {
933 ERR("Failed to send command to wpa_supplicant");
936 GVariantIter *iter = NULL;
937 unsigned char *vsie_bytes = NULL;
941 g_variant_get(message, "(ay)", &iter);
943 ERR("vsie is not present");
947 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
948 if (vsie_bytes == NULL) {
949 ERR("vsie_bytes not allocated");
953 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
956 ERR("vsie not allocated.");
963 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
969 gboolean _remove_vsie(const char *interface_name, int frame_id, const char *vsie)
971 GVariant *params = NULL;
972 GVariant *message = NULL;
973 GVariantBuilder *bytearray_builder = NULL;
978 unsigned char *bytearray = NULL;
979 size_t bytearray_len = 0;
981 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
982 DBG("Invalid parameter, frame-id: %d", frame_id);
986 vsie_len = strlen(vsie);
988 DBG("vsie length is zero");
992 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
994 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
995 if (bytearray == NULL) {
996 DBG("Failed to allocate memory to bytearray");
1000 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
1001 DBG("invalid vsie string");
1006 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1007 for (i = 0; i < bytearray_len; i++)
1008 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
1010 params = g_variant_new("(iay)", frame_id, bytearray_builder);
1011 g_variant_builder_unref(bytearray_builder);
1013 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
1014 if (if_path == NULL) {
1015 ERR("Fail to get wpa_supplicant DBus path");
1020 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
1021 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
1024 if (message == NULL) {
1025 ERR("Failed to send command to wpa_supplicant");
1030 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1037 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context,
1038 const gchar *ifname)
1041 GSList *config_ids = NULL;
1043 gchar **result = NULL;
1044 const gchar *mac_addr = NULL;
1046 g_return_val_if_fail(wifi != NULL, TRUE);
1048 mac_addr = wifi_state_get_mac_address(ifname);
1050 ERR("Fail to get mac-address");
1051 netconfig_error_no_profile(context);
1055 DBG("%s", mac_addr);
1056 config_ids = _get_list(mac_addr);
1057 if (config_ids == NULL) {
1058 ERR("Fail to get config list");
1059 netconfig_error_no_profile(context);
1063 length = g_slist_length(config_ids);
1064 result = g_new0(gchar *, length + 1);
1065 for (i = 0; i < length; i++) {
1066 gchar *config_id = g_slist_nth_data(config_ids, i);
1067 result[i] = g_strdup(config_id);
1070 config_ids = g_slist_nth(config_ids, 0);
1071 g_slist_free_full(config_ids, g_free);
1073 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1075 for (i = 0; i < length; i++)
1085 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1086 const gchar *ifname, const gchar *config_id)
1088 gboolean ret = FALSE;
1089 GVariantBuilder *b = NULL;
1090 struct wifi_config *conf = NULL;
1092 g_return_val_if_fail(wifi != NULL, TRUE);
1094 conf = g_new0(struct wifi_config, 1);
1095 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1097 ret = _load_configuration(ifname, config_id, conf);
1099 g_free(conf->ip_info);
1101 ERR("Fail to _load_configuration");
1102 netconfig_error_no_profile(context);
1106 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1107 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1108 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1109 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PASSPHRASE, g_variant_new_string(conf->passphrase));
1110 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1111 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FREQUENCY, g_variant_new_int32(conf->frequency));
1113 if (conf->proxy_address != NULL)
1114 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1116 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1118 if (conf->ip_info->ip_type != NULL)
1119 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1121 if (conf->ip_info->ip_address != NULL)
1122 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1124 if (conf->ip_info->subnet_mask != NULL)
1125 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1127 if (conf->ip_info->prefix_length > 0)
1128 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV6_PREFIX_LEN, g_variant_new_int32(conf->ip_info->prefix_length));
1130 if (conf->ip_info->gateway_address != NULL)
1131 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1133 if (conf->ip_info->dns_type != NULL)
1134 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1136 int i = 0, count = conf->ip_info->dns_count;
1138 if (conf->ip_info->dns_address[i] != NULL)
1139 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1144 if (conf->last_error != NULL)
1145 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1147 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1149 __free_wifi_configuration(conf);
1151 INFO("Success to load configuration [%s:%s]", ifname, config_id);
1153 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1154 g_variant_builder_unref(b);
1158 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1159 const char *netmask)
1168 mask = inet_network(netmask);
1171 /* a valid netmask must be 2^n - 1 */
1172 if ((host & (host + 1)) != 0)
1176 for (; mask; mask <<= 1)
1182 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1183 const gchar *ifname, const gchar *config_id, GVariant *configuration)
1185 gboolean ret = FALSE;
1186 struct wifi_config *conf = NULL;
1187 GKeyFile *keyfile = NULL;
1191 gchar *group_name = NULL;
1194 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1195 ERR("Invalid parameter");
1196 netconfig_error_invalid_parameter(context);
1200 conf = g_new0(struct wifi_config, 1);
1201 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1203 g_variant_get(configuration, "a{sv}", &iter);
1204 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1205 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1206 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1207 conf->name = g_strdup(g_variant_get_string(value, NULL));
1208 DBG("name [%s]", conf->name);
1212 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1213 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1214 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1215 DBG("ssid [%s]", conf->ssid);
1219 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1220 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1221 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1222 DBG("passphrase []");
1224 conf->passphrase = NULL;
1226 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1227 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1228 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1229 DBG("is_hidden [%s]", conf->is_hidden);
1231 conf->is_hidden = NULL;
1233 } else if (g_strcmp0(field, WIFI_CONFIG_FREQUENCY) == 0) {
1234 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1235 conf->frequency = g_variant_get_int32(value);
1236 DBG("frequency [%d]", conf->frequency);
1238 conf->frequency = 0;
1240 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1241 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1242 conf->is_created = g_variant_get_boolean(value);
1243 DBG("is_created [%d]", conf->is_created);
1245 conf->is_created = FALSE;
1247 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1248 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1249 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1250 DBG("IP config type [%s]", conf->ip_info->ip_type);
1252 conf->ip_info->ip_type = NULL;
1254 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1255 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1256 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1257 DBG("IP address [%s]", conf->ip_info->ip_address);
1259 conf->ip_info->ip_address = NULL;
1261 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1262 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1263 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1264 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1266 conf->ip_info->subnet_mask = NULL;
1268 } else if (g_strcmp0(field, WIFI_CONFIG_IPV6_PREFIX_LEN) == 0) {
1269 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1270 conf->ip_info->prefix_length = g_variant_get_int32(value);
1271 DBG("IPv6 Prefix Length [%d]", conf->ip_info->prefix_length);
1273 conf->ip_info->prefix_length = 0;
1275 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1276 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1277 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1278 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1280 conf->ip_info->gateway_address = NULL;
1282 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1283 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1284 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1285 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1287 conf->ip_info->dns_type = NULL;
1289 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1290 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1291 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1292 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1293 conf->ip_info->dns_count = order + 1;
1296 conf->ip_info->dns_address[order++] = NULL;
1298 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1299 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1300 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1301 DBG("proxy_address [%s]", conf->proxy_address);
1303 conf->proxy_address = NULL;
1307 conf->favorite = TRUE;
1308 conf->autoconnect = TRUE;
1310 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
1311 ifname, config_id, &group_name);
1313 __free_wifi_configuration(conf);
1314 ERR("Fail to get_wifi_config_group_name");
1315 netconfig_error_fail_save_congifuration(context);
1319 keyfile = g_key_file_new();
1320 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1321 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1323 if (conf->passphrase != NULL) {
1324 gchar *enc_data = NULL;
1326 if (conf->is_created == true)
1327 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1329 enc_data = g_strdup(conf->passphrase);
1332 ERR("Failed to encrypt the passphrase");
1334 g_free(conf->passphrase);
1335 conf->passphrase = enc_data;
1338 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1341 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1342 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1344 #if GLIB_CHECK_VERSION(2,62,0)
1345 gint64 real_time = 0;
1346 GDateTime *dt_real_time = NULL;
1348 real_time = g_get_real_time();
1349 dt_real_time = g_date_time_new_from_unix_utc(real_time);
1351 gchar *str = g_date_time_format_iso8601(dt_real_time);
1352 g_date_time_unref(dt_real_time);
1354 g_key_file_set_string(keyfile, group_name,
1355 WIFI_CONFIG_MODIFIED, str);
1359 #else /* GLIB_CHECK_VERSION(2,62,0) */
1361 g_get_current_time(&modified);
1362 gchar *str = g_time_val_to_iso8601(&modified);
1364 g_key_file_set_string(keyfile, group_name,
1365 WIFI_CONFIG_MODIFIED, str);
1368 #endif /* GLIB_CHECK_VERSION(2,62,0) */
1370 /* Optional field */
1371 if (conf->proxy_address != NULL) {
1372 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1373 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1376 if (conf->is_hidden != NULL) {
1377 gboolean hidden = FALSE;
1378 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1380 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1383 if (conf->frequency > 0)
1384 g_key_file_set_integer(keyfile, group_name,
1385 WIFI_CONFIG_FREQUENCY, conf->frequency);
1387 if (conf->ip_info->ip_type != NULL)
1388 g_key_file_set_string(keyfile, group_name,
1389 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1391 if (conf->ip_info->ip_address != NULL)
1392 g_key_file_set_string(keyfile, group_name,
1393 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1395 if (conf->ip_info->subnet_mask != NULL) {
1396 unsigned char prefix_len;
1397 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1398 conf->ip_info->subnet_mask);
1399 if (prefix_len > 0 && prefix_len < 32)
1400 g_key_file_set_integer(keyfile, group_name,
1401 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1404 if (conf->ip_info->prefix_length > 0)
1405 g_key_file_set_integer(keyfile, group_name,
1406 WIFI_CONFIG_IPV6_PREFIX_LEN, conf->ip_info->prefix_length);
1408 if (conf->ip_info->gateway_address != NULL)
1409 g_key_file_set_string(keyfile, group_name,
1410 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1412 if (conf->ip_info->dns_type != NULL)
1413 g_key_file_set_string(keyfile, group_name,
1414 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1416 int i = 0, count = conf->ip_info->dns_count;
1418 if (conf->ip_info->dns_address[i] != NULL)
1419 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1420 conf->ip_info->dns_address[i]);
1425 ret = wifi_config_save_configuration(ifname, config_id, keyfile);
1427 INFO("Success to save configuration [%s]", config_id);
1428 wifi_complete_save_configuration(wifi, context);
1430 if (get_files_count(CONNMAN_STORAGE) > MAX_WIFI_PROFILES) {
1431 file = get_least_recently_profile(CONNMAN_STORAGE);
1433 gchar *profileName = g_strdup_printf(CONNMAN_STORAGE "/%s", file);
1434 INFO("least modified file: %s", profileName);
1436 if (__remove_configuration(profileName) != TRUE)
1437 DBG("Failed to remove profile: [%s]", profileName);
1439 ERR("Profile: [%s] does not exist", file);
1441 g_free(profileName);
1445 INFO("Fail to save configuration [%s]", config_id);
1446 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1449 g_key_file_free(keyfile);
1451 __free_wifi_configuration(conf);
1453 g_variant_iter_free(iter);
1458 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1459 const gchar *ifname, const gchar *config_id)
1461 gboolean ret = FALSE;
1462 GVariantBuilder *b = NULL;
1463 struct wifi_config *conf = NULL;
1465 g_return_val_if_fail(wifi != NULL, TRUE);
1467 conf = g_new0(struct wifi_config, 1);
1468 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1469 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1471 ret = _load_configuration(ifname, config_id, conf);
1473 g_free(conf->eap_config);
1474 g_free(conf->ip_info);
1476 ERR("Fail to _load_configuration");
1477 netconfig_error_no_profile(context);
1481 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1482 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1483 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1484 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1485 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FREQUENCY, g_variant_new_int32(conf->frequency));
1487 if (conf->proxy_address != NULL)
1488 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1490 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1492 if (conf->last_error != NULL)
1493 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1495 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1497 if (conf->eap_config != NULL) {
1498 if (conf->eap_config->anonymous_identity != NULL)
1499 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1501 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1503 if (conf->eap_config->ca_cert != NULL)
1504 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1506 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1508 if (conf->eap_config->client_cert != NULL)
1509 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1511 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1513 if (conf->eap_config->private_key != NULL)
1514 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1516 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1518 if (conf->eap_config->private_key_password != NULL)
1519 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1521 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1523 if (conf->eap_config->identity != NULL)
1524 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1526 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1528 if (conf->eap_config->eap_type != NULL)
1529 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1531 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1533 if (conf->eap_config->eap_auth_type != NULL)
1534 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1536 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1538 if (conf->eap_config->subject_match != NULL)
1539 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1541 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1544 __free_wifi_configuration(conf);
1546 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1547 g_variant_builder_unref(b);
1551 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1552 const gchar *ifname, const gchar *config_id, GVariant *configuration)
1554 gboolean ret = FALSE;
1555 struct wifi_config *conf = NULL;
1556 GKeyFile *keyfile = NULL;
1560 gchar *group_name = NULL;
1562 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1563 ERR("Invalid parameter");
1564 netconfig_error_invalid_parameter(context);
1568 conf = g_new0(struct wifi_config, 1);
1569 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1571 g_variant_get(configuration, "a{sv}", &iter);
1572 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1573 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1574 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1575 conf->name = g_strdup(g_variant_get_string(value, NULL));
1576 DBG("name [%s]", conf->name);
1580 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1581 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1582 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1583 DBG("ssid [%s]", conf->ssid);
1587 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1588 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1589 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1590 DBG("passphrase [%s]", conf->passphrase);
1592 conf->passphrase = NULL;
1594 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1595 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1596 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1597 DBG("is_hidden [%s]", conf->is_hidden);
1599 conf->is_hidden = NULL;
1601 } else if (g_strcmp0(field, WIFI_CONFIG_FREQUENCY) == 0) {
1602 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1603 conf->frequency = g_variant_get_int32(value);
1604 DBG("frequency [%d]", conf->frequency);
1606 conf->frequency = 0;
1608 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1609 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1610 conf->is_created = g_variant_get_boolean(value);
1611 DBG("is_created [%d]", conf->is_created);
1613 conf->is_created = FALSE;
1615 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1616 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1617 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1618 DBG("proxy_address [%s]", conf->proxy_address);
1620 conf->proxy_address = NULL;
1622 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1623 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1624 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1625 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1627 conf->eap_config->anonymous_identity = NULL;
1629 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1630 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1631 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1632 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1634 conf->eap_config->ca_cert = NULL;
1636 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1637 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1638 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1639 DBG("client_cert [%s]", conf->eap_config->client_cert);
1641 conf->eap_config->client_cert = NULL;
1643 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1644 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1645 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1646 DBG("private_key [%s]", conf->eap_config->private_key);
1648 conf->eap_config->private_key = NULL;
1650 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1651 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1652 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1653 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1655 conf->eap_config->private_key_password = NULL;
1657 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1658 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1659 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1660 DBG("identity [%s]", conf->eap_config->identity);
1662 conf->eap_config->identity = NULL;
1664 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1665 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1666 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1667 DBG("eap_type [%s]", conf->eap_config->eap_type);
1669 conf->eap_config->eap_type = NULL;
1671 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1672 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1673 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1674 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1676 conf->eap_config->eap_auth_type = NULL;
1678 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1679 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1680 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1681 DBG("subject_match [%s]", conf->eap_config->subject_match);
1683 conf->eap_config->subject_match = NULL;
1687 conf->favorite = TRUE;
1688 conf->autoconnect = TRUE;
1690 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
1691 ifname, config_id, &group_name);
1693 __free_wifi_configuration(conf);
1694 ERR("Fail to get_wifi_config_group_name");
1698 keyfile = g_key_file_new();
1699 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1700 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1702 if (conf->passphrase != NULL) {
1703 gchar *enc_data = NULL;
1705 if (conf->is_created == true)
1706 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1708 enc_data = g_strdup(conf->passphrase);
1711 ERR("Failed to encrypt the passphrase");
1713 g_free(conf->passphrase);
1714 conf->passphrase = enc_data;
1716 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1719 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1720 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1722 /* Optional field */
1723 if (conf->proxy_address != NULL) {
1724 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1725 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1728 if (conf->is_hidden != NULL) {
1729 gboolean hidden = FALSE;
1730 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1732 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1735 if (conf->frequency > 0)
1736 g_key_file_set_integer(keyfile, group_name,
1737 WIFI_CONFIG_FREQUENCY, conf->frequency);
1739 if (conf->eap_config->anonymous_identity != NULL)
1740 g_key_file_set_string(keyfile, group_name,
1741 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1743 if (conf->eap_config->ca_cert != NULL)
1744 g_key_file_set_string(keyfile, group_name,
1745 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1747 if (conf->eap_config->client_cert != NULL)
1748 g_key_file_set_string(keyfile, group_name,
1749 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1751 if (conf->eap_config->private_key != NULL)
1752 g_key_file_set_string(keyfile, group_name,
1753 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1755 if (conf->eap_config->private_key_password != NULL)
1756 g_key_file_set_string(keyfile, group_name,
1757 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1759 if (conf->eap_config->identity != NULL)
1760 g_key_file_set_string(keyfile, group_name,
1761 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1763 if (conf->eap_config->eap_type != NULL)
1764 g_key_file_set_string(keyfile, group_name,
1765 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1767 if (conf->eap_config->eap_auth_type != NULL)
1768 g_key_file_set_string(keyfile, group_name,
1769 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1771 if (conf->eap_config->subject_match != NULL)
1772 g_key_file_set_string(keyfile, group_name,
1773 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1775 ret = wifi_config_save_configuration(ifname, config_id, keyfile);
1777 INFO("Success to save eap configuration [%s]", config_id);
1778 wifi_complete_save_eap_configuration(wifi, context);
1780 INFO("Fail to save eap configuration [%s]", config_id);
1781 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1784 g_key_file_free(keyfile);
1786 __free_wifi_configuration(conf);
1788 g_variant_iter_free(iter);
1793 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1794 const gchar *ifname, const gchar *config_id)
1796 gboolean ret = FALSE;
1798 if ((wifi == NULL) || (config_id == NULL)) {
1799 ERR("Invalid parameter");
1800 netconfig_error_invalid_parameter(context);
1804 ret = _remove_configuration(ifname, config_id);
1806 /* no configuration or error */
1807 ERR("No [%s] configuration", config_id);
1808 netconfig_error_no_profile(context);
1812 wifi_complete_remove_configuration(wifi, context);
1816 /* config field key / value */
1818 * [wifi_macaddress_config_id]
1819 * Name=name (mandatory)
1820 * SSID=SSID (mandatory)
1821 * Frequency=2462 (X)
1823 * AutoConnect=true (Default true)
1824 * Modified=2015-03-20 (X)
1825 * IPv4.method=manual (O)
1826 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1827 * IPv6.method=auto (X)
1828 * IPv6.privacy=disabled (X)
1829 * IPv4.netmask_prefixlen=24 (X)
1830 * IPv4.local_address=192.0.0.1 (O)
1831 * IPv4.gateway=192.0.0.1 (O ? X ?)
1832 * Nameservers=192.168.43.22; (O)
1833 * Proxy.Method=manual (O)
1834 * Proxy.Servers=trst.com:8888; (O)
1836 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1837 const gchar *ifname, const gchar *config_id, const gchar *key, const gchar *value)
1839 gboolean ret = FALSE;
1840 gchar *keyfile_key = NULL;
1842 g_return_val_if_fail(wifi != NULL, TRUE);
1843 g_return_val_if_fail(config_id != NULL, TRUE);
1844 g_return_val_if_fail(key != NULL, TRUE);
1846 DBG("Key[%s] Value[%s]", key, value);
1848 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1849 ret = _set_field(ifname, config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1851 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1852 netconfig_error_invalid_parameter(context);
1855 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1856 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1857 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1858 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1859 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1860 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1861 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1862 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1863 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1864 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1865 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1866 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1867 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1868 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1869 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1870 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1871 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1872 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1873 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1875 ERR("Not supported key[%s]", key);
1876 netconfig_error_invalid_parameter(context);
1880 ret = _set_field(ifname, config_id, keyfile_key, (const gchar *)value);
1882 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1885 if (keyfile_key != NULL)
1886 g_free(keyfile_key);
1888 wifi_complete_set_config_field(wifi, context);
1892 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context,
1893 const gchar *ifname, const gchar *config_id)
1895 gboolean ret = FALSE;
1896 gchar *passphrase = NULL;
1898 if ((wifi == NULL) || (config_id == NULL)) {
1899 ERR("Invalid parameter");
1900 netconfig_error_invalid_parameter(context);
1904 ret = _get_field(ifname, config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1906 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1907 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1911 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1917 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1918 const gchar *ifname, int frame_id, const gchar *vsie)
1920 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1922 g_return_val_if_fail(wifi != NULL, TRUE);
1923 g_return_val_if_fail(vsie != NULL, TRUE);
1925 gboolean ret = FALSE;
1927 ret = _add_vsie(ifname, frame_id, vsie);
1929 DBG("Failed to add vsie: %s", vsie);
1930 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1934 wifi_complete_add_vsie(wifi, context);
1938 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1939 const gchar *ifname, int frame_id)
1941 DBG("Frame ID: [%d]", frame_id);
1943 g_return_val_if_fail(wifi != NULL, TRUE);
1945 gboolean ret = FALSE;
1948 ret = _get_vsie(ifname, frame_id, &vsie);
1950 DBG("Failed to get vsie for frame:[%d]", frame_id);
1951 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1955 DBG("Received vsie: %s", vsie);
1956 wifi_complete_get_vsie(wifi, context, vsie);
1961 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1962 const gchar *ifname, int frame_id, const gchar *vsie)
1964 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1966 g_return_val_if_fail(wifi != NULL, TRUE);
1967 g_return_val_if_fail(vsie != NULL, TRUE);
1969 gboolean ret = FALSE;
1971 ret = _remove_vsie(ifname, frame_id, vsie);
1973 DBG("Failed to remove vsie: %s", vsie);
1974 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1978 wifi_complete_remove_vsie(wifi, context);