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"
48 #define WIFI_SECURITY_SAE "sae"
50 #define WIFI_CONFIG_PREFIX "wifi_"
51 #define MAC_ADDRESS_LENGTH 12
52 #define WIFI_PREFIX_LENGTH MAC_ADDRESS_LENGTH + 6 /* wifi_485a3f2f506a_ */
53 #define PROFILE_PREFIX_LENGTH WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
55 #define NET_DNS_ADDR_MAX 2
57 #define MAX_WIFI_PROFILES 200
59 struct wifi_eap_config {
60 gchar *anonymous_identity;
64 gchar *private_key_password;
74 gchar *gateway_address;
75 gchar *dns_address[NET_DNS_ADDR_MAX];
92 struct wifi_eap_config *eap_config;
93 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(const gchar *interface_name, gchar **mac_address)
144 char buf[WIFI_MAC_ADDR_LENGTH + 1];
145 char path[WIFI_MAC_PATH_LENGTH];
146 gchar *tmp_mac = NULL;
148 gchar mac[13] = { 0, };
151 snprintf(path, WIFI_MAC_PATH_LENGTH, WIFI_MAC_ADDR_PATH, interface_name);
153 if (0 == access(path, F_OK))
154 fp = fopen(path, "r");
157 if (fgets(buf, sizeof(buf), fp) == NULL) {
158 ERR("Failed to get MAC info from %s\n", path);
163 tmp_mac = (gchar *)malloc(WIFI_MAC_ADDR_LENGTH + 1);
164 if (tmp_mac == NULL) {
165 ERR("malloc() failed");
170 memset(tmp_mac, 0, WIFI_MAC_ADDR_LENGTH + 1);
171 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADDR_LENGTH + 1);
174 ERR("Failed to open file %s\n", path);
176 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
177 if (tmp_mac == NULL) {
178 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,
199 const gchar *interface_name, const gchar *config_id, gchar **group_name)
201 gchar *mac_address = NULL;
202 gchar *g_name = NULL;
203 gboolean ret = FALSE;
205 ret = __get_mac_address(interface_name, &mac_address);
206 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
207 ERR("Cannot get WIFI MAC address");
212 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
213 if (g_name == NULL) {
218 *group_name = g_strdup(g_name);
226 static gboolean __get_security_type(const gchar *config_id, gchar **type)
228 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
229 *type = g_strdup(WIFI_SECURITY_NONE);
230 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
231 *type = g_strdup(WIFI_SECURITY_WEP);
232 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
233 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
234 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
235 *type = g_strdup(WIFI_SECURITY_EAP);
236 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_SAE) == TRUE) {
237 *type = g_strdup(WIFI_SECURITY_SAE);
246 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
248 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
249 if (*config_id == NULL) {
258 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
260 GKeyFile *keyfile = NULL;
263 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
265 keyfile = netconfig_keyfile_load(path);
267 ERR("keyfile[%s] is NULL", path);
274 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
276 gboolean ret = FALSE;
279 path = g_strdup_printf("%s/%s", pathname, filename);
280 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
282 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
291 static gboolean __remove_configuration(const gchar *pathname)
295 if (__remove_file(pathname, "settings") != TRUE) {
296 ERR("Cannot remove [%s/settings]", pathname);
299 if (__remove_file(pathname, "data") != TRUE) {
300 ERR("Cannot remove [%s/data]", pathname);
304 ret = rmdir(pathname);
306 ERR("Cannot remove [%s]", pathname);
312 static gboolean _load_configuration(const gchar *interface_name,
313 const gchar *config_id, struct wifi_config *config)
317 gboolean hidden = FALSE;
318 gboolean ret = FALSE;
320 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
321 interface_name, config_id, &group_name);
323 ERR("Fail to get_wifi_config_group_name");
327 keyfile = __get_configuration_keyfile(group_name);
328 if (keyfile == NULL) {
329 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
334 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
335 DBG("name [%s]", config->name);
337 __get_security_type(config_id, &config->security_type);
338 if (config->security_type == NULL) {
339 ERR("Fail to _get_security_type");
340 g_key_file_free(keyfile);
344 DBG("security_type [%s]", config->security_type);
346 config->passphrase = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
347 DBG("passphrase []");
349 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
350 if (config->proxy_address)
351 DBG("proxy_address [%s]", config->proxy_address);
353 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
355 config->is_hidden = g_strdup("TRUE");
357 config->is_hidden = g_strdup("FALSE");
358 DBG("is_hidden [%s]", config->is_hidden);
360 config->frequency = g_key_file_get_integer(keyfile, group_name, WIFI_CONFIG_FREQUENCY, NULL);
361 if (config->frequency)
362 DBG("Frequency [%d]", config->frequency);
364 if (config->ip_info) {
365 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
366 WIFI_CONFIG_IPV4_METHOD, NULL);
367 if (config->ip_info->ip_type)
368 DBG("IPv4.Method:%s", config->ip_info->ip_type);
370 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
371 WIFI_CONFIG_IPV4_ADDRESS, NULL);
372 if (config->ip_info->ip_address)
373 DBG("IPv4.Address:%s", config->ip_info->ip_address);
377 struct in_addr netmask;
379 GError *error = NULL;
380 prefix_len = g_key_file_get_integer(keyfile, group_name,
381 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
383 DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
386 if (prefix_len > 0 && prefix_len < 32) {
387 addr = 0xffffffff << (32 - prefix_len);
388 netmask.s_addr = htonl(addr);
389 mask = inet_ntoa(netmask);
390 config->ip_info->subnet_mask = g_strdup(mask);
392 if (config->ip_info->subnet_mask)
393 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
396 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
397 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
398 if (config->ip_info->gateway_address)
399 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
401 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
402 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
403 if (config->ip_info->dns_type)
404 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
408 nameservers = g_key_file_get_string_list(keyfile, group_name,
409 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
412 config->ip_info->dns_count = length;
414 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
415 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
416 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
420 g_strfreev(nameservers);
425 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
426 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
427 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
428 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
429 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
430 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
431 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
432 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
433 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
434 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
436 if (config->eap_config->anonymous_identity)
437 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
438 if (config->eap_config->ca_cert)
439 DBG("ca_cert [%s]", config->eap_config->ca_cert);
440 if (config->eap_config->client_cert)
441 DBG("client_cert [%s]", config->eap_config->client_cert);
442 if (config->eap_config->private_key)
443 DBG("private_key [%s]", config->eap_config->private_key);
444 if (config->eap_config->private_key_password)
445 DBG("private_key_password [%s]", config->eap_config->private_key_password);
446 if (config->eap_config->identity)
447 DBG("identity [%s]", config->eap_config->identity);
448 if (config->eap_config->eap_type)
449 DBG("eap_type [%s]", config->eap_config->eap_type);
450 if (config->eap_config->eap_auth_type)
451 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
452 if (config->eap_config->subject_match)
453 DBG("subject_match [%s]", config->eap_config->subject_match);
456 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
457 if (config->last_error)
458 DBG("last_error [%s]", config->last_error);
460 g_key_file_free(keyfile);
466 gboolean wifi_config_save_configuration(const gchar *interface_name,
467 const gchar *config_id, GKeyFile *keyfile)
472 gboolean ret = FALSE;
474 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
475 interface_name, config_id, &group_name);
477 ERR("Fail to get_wifi_config_group_name");
481 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
482 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
483 if (__remove_configuration(dir) != TRUE) {
484 ERR("[%s] is existed, but cannot remove", dir);
491 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
492 ERR("Cannot mkdir %s", dir);
498 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
499 netconfig_keyfile_save(keyfile, path);
507 static gboolean _remove_configuration(const gchar *interface_name, const gchar *config_id)
509 gboolean ret = FALSE;
513 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
514 interface_name, config_id, &group_name);
516 ERR("Fail to get_wifi_config_group_name");
520 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
521 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
522 if (__remove_configuration(dir) != TRUE) {
523 ERR("[%s] is existed, but cannot remove", dir);
526 INFO("Success to remove [%s]", dir);
529 ERR("[%s] is not existed", dir);
540 static gboolean _set_field(const gchar *interface_name,
541 const gchar *config_id, const gchar *key, const gchar *value)
547 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
548 interface_name, config_id, &group_name);
550 ERR("Fail to get_wifi_config_group_name");
553 DBG("group_name %s", group_name);
555 keyfile = __get_configuration_keyfile(group_name);
556 if (keyfile == NULL) {
557 ERR("Fail to __get_configuration_keyfile");
562 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
563 g_key_file_set_string(keyfile, group_name, key, value);
564 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
565 g_key_file_set_string(keyfile, group_name, key, value);
566 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
567 gboolean hidden = FALSE;
568 if (g_strcmp0(value, "TRUE") == 0)
570 g_key_file_set_boolean(keyfile, group_name, key, hidden);
571 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
572 g_key_file_set_string(keyfile, group_name, key, value);
573 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
574 g_key_file_set_string(keyfile, group_name, key, value);
575 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
576 g_key_file_set_string(keyfile, group_name, key, value);
577 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
578 g_key_file_set_string(keyfile, group_name, key, value);
579 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
580 g_key_file_set_string(keyfile, group_name, key, value);
581 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
582 g_key_file_set_string(keyfile, group_name, key, value);
583 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
584 g_key_file_set_string(keyfile, group_name, key, value);
585 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
586 g_key_file_set_string(keyfile, group_name, key, value);
588 ERR("key[%s] is not supported", key);
592 wifi_config_save_configuration(interface_name, config_id, keyfile);
594 g_key_file_free(keyfile);
600 static gboolean _get_field(const gchar *interface_name,
601 const gchar *config_id, const gchar *key, gchar **value)
606 gboolean hidden = FALSE;
607 gboolean ret = FALSE;
609 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
610 interface_name, config_id, &group_name);
612 ERR("Fail to get_wifi_config_group_name");
615 DBG("group_name %s", group_name);
617 keyfile = __get_configuration_keyfile(group_name);
618 if (keyfile == NULL) {
619 ERR("Fail to __get_configuration_keyfile");
624 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
625 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
626 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
627 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
628 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
629 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
630 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
631 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
633 val = g_strdup("TRUE");
635 val = g_strdup("FALSE");
636 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
637 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
638 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
639 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
640 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
641 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
642 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
643 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
644 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
645 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
646 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
647 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
648 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
649 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
650 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
651 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
652 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
653 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
655 ERR("Invalid key[%s]", key);
656 val = g_strdup("NOTSUPPORTED");
659 *value = g_strdup(val);
662 g_key_file_free(keyfile);
668 static GSList *_get_list(const char *mac_addr)
671 struct dirent *dp = NULL;
674 dir = opendir(CONNMAN_STORAGE);
676 ERR("Cannot open dir %s", CONNMAN_STORAGE);
680 while ((dp = readdir(dir)) != NULL) {
681 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
682 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
686 DBG("%s", dp->d_name);
688 if (netconfig_check_mac_address(dp->d_name, mac_addr)) {
689 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
690 DBG("%s", config_id);
691 list = g_slist_append(list, g_strdup(config_id));
700 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
702 gboolean ret = FALSE;
705 if ((service_profile == NULL) || (config_id == NULL)) {
706 ERR("Invalid parameter");
710 ret = __get_config_id(service_profile, &val);
711 *config_id = g_strdup(val);
717 gboolean wifi_config_remove_configuration(const gchar *interface_name,
718 const gchar *config_id)
720 gboolean ret = FALSE;
722 ret = _remove_configuration(interface_name, config_id);
727 int __netconfig_hex_char_to_num(char c)
729 if (c >= '0' && c <= '9')
732 if (c >= 'a' && c <= 'f')
735 if (c >= 'A' && c <= 'F')
741 int __netconfig_hex_to_byte(const char *hex)
745 a = __netconfig_hex_char_to_num(*hex++);
749 b = __netconfig_hex_char_to_num(*hex++);
756 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
760 const char *ipos = hex;
761 unsigned char *opos = buf;
763 for (i = 0; i < len; i++) {
764 a = __netconfig_hex_to_byte(ipos);
775 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
782 ERR("Invalid parameter.");
786 *dst = (char *) g_try_malloc0((2*src_len)+1);
788 ERR("failed to allocate memory to buffer.");
794 for (i = 0; i < src_len; i++) {
795 snprintf(buf, 3, "%02x", src[i]);
803 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
805 GVariantIter *iter_copy = NULL;
808 unsigned char *tmp_dst = NULL;
810 if (!dst || *dst || !iter) {
811 ERR("Invalid parameter");
815 iter_copy = g_variant_iter_copy(iter);
817 while (g_variant_iter_loop(iter, "y", &tmp))
819 g_variant_iter_free(iter);
821 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
823 ERR("failed to allocate memory");
824 g_variant_iter_free(iter_copy);
829 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
831 g_variant_iter_free(iter_copy);
837 tmp_dst[length] = '\0';
841 DBG("Length [%d]", length);
845 gboolean _add_vsie(const char *interface_name, int frame_id, const char* vsie)
847 GVariant *params = NULL;
848 GVariant *message = NULL;
849 GVariantBuilder *bytearray_builder = NULL;
854 unsigned char *bytearray = NULL;
855 size_t bytearray_len = 0;
857 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
858 DBG("Invalid parameter, frame-id: %d", frame_id);
862 vsie_len = strlen(vsie);
864 DBG("vsie length is zero");
868 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
870 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
871 if (bytearray == NULL) {
872 DBG("Failed to allocate memory to bytearray");
876 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
877 DBG("invalid vsie string");
882 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
883 for (i = 0; i < bytearray_len; i++)
884 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
886 params = g_variant_new("(iay)", frame_id, bytearray_builder);
887 g_variant_builder_unref(bytearray_builder);
889 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
890 if (if_path == NULL) {
891 ERR("Fail to get wpa_supplicant DBus path");
896 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
897 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
900 if (message == NULL) {
901 ERR("Failed to send command to wpa_supplicant");
906 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
912 gboolean _get_vsie(const char *interface_name, int frame_id, char **vsie)
914 GVariant *params = NULL;
915 GVariant *message = NULL;
918 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
919 DBG("Invalid parameter, frame-id: %d", frame_id);
923 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
924 if (if_path == NULL) {
925 ERR("Fail to get wpa_supplicant DBus path");
929 params = g_variant_new("(i)", frame_id);
931 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
932 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
935 if (message == NULL) {
936 ERR("Failed to send command to wpa_supplicant");
939 GVariantIter *iter = NULL;
940 unsigned char *vsie_bytes = NULL;
944 g_variant_get(message, "(ay)", &iter);
946 ERR("vsie is not present");
950 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
951 if (vsie_bytes == NULL) {
952 ERR("vsie_bytes not allocated");
956 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
959 ERR("vsie not allocated.");
966 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
972 gboolean _remove_vsie(const char *interface_name, int frame_id, const char *vsie)
974 GVariant *params = NULL;
975 GVariant *message = NULL;
976 GVariantBuilder *bytearray_builder = NULL;
981 unsigned char *bytearray = NULL;
982 size_t bytearray_len = 0;
984 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
985 DBG("Invalid parameter, frame-id: %d", frame_id);
989 vsie_len = strlen(vsie);
991 DBG("vsie length is zero");
995 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
997 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
998 if (bytearray == NULL) {
999 DBG("Failed to allocate memory to bytearray");
1003 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
1004 DBG("invalid vsie string");
1009 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1010 for (i = 0; i < bytearray_len; i++)
1011 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
1013 params = g_variant_new("(iay)", frame_id, bytearray_builder);
1014 g_variant_builder_unref(bytearray_builder);
1016 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
1017 if (if_path == NULL) {
1018 ERR("Fail to get wpa_supplicant DBus path");
1023 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
1024 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
1027 if (message == NULL) {
1028 ERR("Failed to send command to wpa_supplicant");
1033 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1040 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context,
1041 const gchar *ifname)
1044 GSList *config_ids = NULL;
1046 gchar **result = NULL;
1047 const gchar *mac_addr = NULL;
1049 g_return_val_if_fail(wifi != NULL, TRUE);
1051 mac_addr = wifi_state_get_mac_address(ifname);
1053 ERR("Fail to get mac-address");
1054 netconfig_error_no_profile(context);
1058 DBG("%s", mac_addr);
1059 config_ids = _get_list(mac_addr);
1060 if (config_ids == NULL) {
1061 ERR("Fail to get config list");
1062 netconfig_error_no_profile(context);
1066 length = g_slist_length(config_ids);
1067 result = g_new0(gchar *, length + 1);
1068 for (i = 0; i < length; i++) {
1069 gchar *config_id = g_slist_nth_data(config_ids, i);
1070 result[i] = g_strdup(config_id);
1073 config_ids = g_slist_nth(config_ids, 0);
1074 g_slist_free_full(config_ids, g_free);
1076 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1078 for (i = 0; i < length; i++)
1088 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1089 const gchar *ifname, const gchar *config_id)
1091 gboolean ret = FALSE;
1092 GVariantBuilder *b = NULL;
1093 struct wifi_config *conf = NULL;
1095 g_return_val_if_fail(wifi != NULL, TRUE);
1097 conf = g_new0(struct wifi_config, 1);
1098 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1100 ret = _load_configuration(ifname, config_id, conf);
1102 g_free(conf->ip_info);
1104 ERR("Fail to _load_configuration");
1105 netconfig_error_no_profile(context);
1109 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1110 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1111 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1112 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PASSPHRASE, g_variant_new_string(conf->passphrase));
1113 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1114 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FREQUENCY, g_variant_new_int32(conf->frequency));
1116 if (conf->proxy_address != NULL)
1117 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1119 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1121 if (conf->ip_info->ip_type != NULL)
1122 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1124 if (conf->ip_info->ip_address != NULL)
1125 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1127 if (conf->ip_info->subnet_mask != NULL)
1128 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1130 if (conf->ip_info->prefix_length > 0)
1131 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV6_PREFIX_LEN, g_variant_new_int32(conf->ip_info->prefix_length));
1133 if (conf->ip_info->gateway_address != NULL)
1134 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1136 if (conf->ip_info->dns_type != NULL)
1137 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1139 int i = 0, count = conf->ip_info->dns_count;
1141 if (conf->ip_info->dns_address[i] != NULL)
1142 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1147 if (conf->last_error != NULL)
1148 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1150 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1152 __free_wifi_configuration(conf);
1154 INFO("Success to load configuration [%s:%s]", ifname, config_id);
1156 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1157 g_variant_builder_unref(b);
1161 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1162 const char *netmask)
1171 mask = inet_network(netmask);
1174 /* a valid netmask must be 2^n - 1 */
1175 if ((host & (host + 1)) != 0)
1179 for (; mask; mask <<= 1)
1185 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1186 const gchar *ifname, const gchar *config_id, GVariant *configuration)
1188 gboolean ret = FALSE;
1189 struct wifi_config *conf = NULL;
1190 GKeyFile *keyfile = NULL;
1194 gchar *group_name = NULL;
1197 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1198 ERR("Invalid parameter");
1199 netconfig_error_invalid_parameter(context);
1203 conf = g_new0(struct wifi_config, 1);
1204 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1206 g_variant_get(configuration, "a{sv}", &iter);
1207 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1208 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1209 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1210 conf->name = g_strdup(g_variant_get_string(value, NULL));
1211 DBG("name [%s]", conf->name);
1215 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1216 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1217 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1218 DBG("ssid [%s]", conf->ssid);
1222 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1223 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1224 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1225 DBG("passphrase []");
1227 conf->passphrase = NULL;
1229 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1230 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1231 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1232 DBG("is_hidden [%s]", conf->is_hidden);
1234 conf->is_hidden = NULL;
1236 } else if (g_strcmp0(field, WIFI_CONFIG_FREQUENCY) == 0) {
1237 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1238 conf->frequency = g_variant_get_int32(value);
1239 DBG("frequency [%d]", conf->frequency);
1241 conf->frequency = 0;
1243 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1244 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1245 conf->is_created = g_variant_get_boolean(value);
1246 DBG("is_created [%d]", conf->is_created);
1248 conf->is_created = FALSE;
1250 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1251 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1252 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1253 DBG("IP config type [%s]", conf->ip_info->ip_type);
1255 conf->ip_info->ip_type = NULL;
1257 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1258 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1259 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1260 DBG("IP address [%s]", conf->ip_info->ip_address);
1262 conf->ip_info->ip_address = NULL;
1264 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1265 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1266 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1267 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1269 conf->ip_info->subnet_mask = NULL;
1271 } else if (g_strcmp0(field, WIFI_CONFIG_IPV6_PREFIX_LEN) == 0) {
1272 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1273 conf->ip_info->prefix_length = g_variant_get_int32(value);
1274 DBG("IPv6 Prefix Length [%d]", conf->ip_info->prefix_length);
1276 conf->ip_info->prefix_length = 0;
1278 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1279 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1280 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1281 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1283 conf->ip_info->gateway_address = NULL;
1285 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1286 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1287 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1288 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1290 conf->ip_info->dns_type = NULL;
1292 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1293 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1294 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1295 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1296 conf->ip_info->dns_count = order + 1;
1299 conf->ip_info->dns_address[order++] = NULL;
1301 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1302 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1303 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1304 DBG("proxy_address [%s]", conf->proxy_address);
1306 conf->proxy_address = NULL;
1310 conf->favorite = TRUE;
1311 conf->autoconnect = TRUE;
1313 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
1314 ifname, config_id, &group_name);
1316 __free_wifi_configuration(conf);
1317 ERR("Fail to get_wifi_config_group_name");
1318 netconfig_error_fail_save_congifuration(context);
1322 keyfile = g_key_file_new();
1323 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1324 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1326 if (conf->passphrase != NULL) {
1327 gchar *enc_data = NULL;
1329 if (conf->is_created == true)
1330 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1332 enc_data = g_strdup(conf->passphrase);
1335 ERR("Failed to encrypt the passphrase");
1337 g_free(conf->passphrase);
1338 conf->passphrase = enc_data;
1341 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1344 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1345 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1347 #if GLIB_CHECK_VERSION(2,62,0)
1348 gint64 real_time = 0;
1349 GDateTime *dt_real_time = NULL;
1351 real_time = g_get_real_time();
1352 dt_real_time = g_date_time_new_from_unix_utc(real_time);
1354 gchar *str = g_date_time_format_iso8601(dt_real_time);
1355 g_date_time_unref(dt_real_time);
1357 g_key_file_set_string(keyfile, group_name,
1358 WIFI_CONFIG_MODIFIED, str);
1362 #else /* GLIB_CHECK_VERSION(2,62,0) */
1364 g_get_current_time(&modified);
1365 gchar *str = g_time_val_to_iso8601(&modified);
1367 g_key_file_set_string(keyfile, group_name,
1368 WIFI_CONFIG_MODIFIED, str);
1371 #endif /* GLIB_CHECK_VERSION(2,62,0) */
1373 /* Optional field */
1374 if (conf->proxy_address != NULL) {
1375 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1376 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1379 if (conf->is_hidden != NULL) {
1380 gboolean hidden = FALSE;
1381 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1383 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1386 if (conf->frequency > 0)
1387 g_key_file_set_integer(keyfile, group_name,
1388 WIFI_CONFIG_FREQUENCY, conf->frequency);
1390 if (conf->ip_info->ip_type != NULL)
1391 g_key_file_set_string(keyfile, group_name,
1392 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1394 if (conf->ip_info->ip_address != NULL)
1395 g_key_file_set_string(keyfile, group_name,
1396 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1398 if (conf->ip_info->subnet_mask != NULL) {
1399 unsigned char prefix_len;
1400 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1401 conf->ip_info->subnet_mask);
1402 if (prefix_len > 0 && prefix_len < 32)
1403 g_key_file_set_integer(keyfile, group_name,
1404 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1407 if (conf->ip_info->prefix_length > 0)
1408 g_key_file_set_integer(keyfile, group_name,
1409 WIFI_CONFIG_IPV6_PREFIX_LEN, conf->ip_info->prefix_length);
1411 if (conf->ip_info->gateway_address != NULL)
1412 g_key_file_set_string(keyfile, group_name,
1413 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1415 if (conf->ip_info->dns_type != NULL)
1416 g_key_file_set_string(keyfile, group_name,
1417 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1419 int i = 0, count = conf->ip_info->dns_count;
1421 if (conf->ip_info->dns_address[i] != NULL)
1422 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1423 conf->ip_info->dns_address[i]);
1428 ret = wifi_config_save_configuration(ifname, config_id, keyfile);
1430 INFO("Success to save configuration [%s]", config_id);
1431 wifi_complete_save_configuration(wifi, context);
1433 if (get_files_count(CONNMAN_STORAGE) > MAX_WIFI_PROFILES) {
1434 file = get_least_recently_profile(CONNMAN_STORAGE);
1436 gchar *profileName = g_strdup_printf(CONNMAN_STORAGE "/%s", file);
1437 INFO("least modified file: %s", profileName);
1439 if (__remove_configuration(profileName) != TRUE)
1440 DBG("Failed to remove profile: [%s]", profileName);
1442 ERR("Profile: [%s] does not exist", file);
1444 g_free(profileName);
1448 INFO("Fail to save configuration [%s]", config_id);
1449 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1452 g_key_file_free(keyfile);
1454 __free_wifi_configuration(conf);
1456 g_variant_iter_free(iter);
1461 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1462 const gchar *ifname, const gchar *config_id)
1464 gboolean ret = FALSE;
1465 GVariantBuilder *b = NULL;
1466 struct wifi_config *conf = NULL;
1468 g_return_val_if_fail(wifi != NULL, TRUE);
1470 conf = g_new0(struct wifi_config, 1);
1471 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1472 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1474 ret = _load_configuration(ifname, config_id, conf);
1476 g_free(conf->eap_config);
1477 g_free(conf->ip_info);
1479 ERR("Fail to _load_configuration");
1480 netconfig_error_no_profile(context);
1484 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1485 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1486 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1487 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1488 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FREQUENCY, g_variant_new_int32(conf->frequency));
1490 if (conf->proxy_address != NULL)
1491 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1493 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1495 if (conf->last_error != NULL)
1496 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1498 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1500 if (conf->eap_config != NULL) {
1501 if (conf->eap_config->anonymous_identity != NULL)
1502 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1504 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1506 if (conf->eap_config->ca_cert != NULL)
1507 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1509 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1511 if (conf->eap_config->client_cert != NULL)
1512 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1514 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1516 if (conf->eap_config->private_key != NULL)
1517 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1519 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1521 if (conf->eap_config->private_key_password != NULL)
1522 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1524 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1526 if (conf->eap_config->identity != NULL)
1527 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1529 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1531 if (conf->eap_config->eap_type != NULL)
1532 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1534 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1536 if (conf->eap_config->eap_auth_type != NULL)
1537 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1539 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1541 if (conf->eap_config->subject_match != NULL)
1542 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1544 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1547 __free_wifi_configuration(conf);
1549 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1550 g_variant_builder_unref(b);
1554 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1555 const gchar *ifname, const gchar *config_id, GVariant *configuration)
1557 gboolean ret = FALSE;
1558 struct wifi_config *conf = NULL;
1559 GKeyFile *keyfile = NULL;
1563 gchar *group_name = NULL;
1565 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1566 ERR("Invalid parameter");
1567 netconfig_error_invalid_parameter(context);
1571 conf = g_new0(struct wifi_config, 1);
1572 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1574 g_variant_get(configuration, "a{sv}", &iter);
1575 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1576 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1577 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1578 conf->name = g_strdup(g_variant_get_string(value, NULL));
1579 DBG("name [%s]", conf->name);
1583 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1584 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1585 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1586 DBG("ssid [%s]", conf->ssid);
1590 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1591 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1592 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1593 DBG("passphrase [%s]", conf->passphrase);
1595 conf->passphrase = NULL;
1597 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1598 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1599 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1600 DBG("is_hidden [%s]", conf->is_hidden);
1602 conf->is_hidden = NULL;
1604 } else if (g_strcmp0(field, WIFI_CONFIG_FREQUENCY) == 0) {
1605 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1606 conf->frequency = g_variant_get_int32(value);
1607 DBG("frequency [%d]", conf->frequency);
1609 conf->frequency = 0;
1611 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1612 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1613 conf->is_created = g_variant_get_boolean(value);
1614 DBG("is_created [%d]", conf->is_created);
1616 conf->is_created = FALSE;
1618 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1619 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1620 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1621 DBG("proxy_address [%s]", conf->proxy_address);
1623 conf->proxy_address = NULL;
1625 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1626 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1627 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1628 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1630 conf->eap_config->anonymous_identity = NULL;
1632 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1633 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1634 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1635 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1637 conf->eap_config->ca_cert = NULL;
1639 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1640 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1641 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1642 DBG("client_cert [%s]", conf->eap_config->client_cert);
1644 conf->eap_config->client_cert = NULL;
1646 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1647 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1648 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1649 DBG("private_key [%s]", conf->eap_config->private_key);
1651 conf->eap_config->private_key = NULL;
1653 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1654 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1655 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1656 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1658 conf->eap_config->private_key_password = NULL;
1660 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1661 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1662 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1663 DBG("identity [%s]", conf->eap_config->identity);
1665 conf->eap_config->identity = NULL;
1667 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1668 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1669 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1670 DBG("eap_type [%s]", conf->eap_config->eap_type);
1672 conf->eap_config->eap_type = NULL;
1674 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1675 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1676 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1677 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1679 conf->eap_config->eap_auth_type = NULL;
1681 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1682 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1683 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1684 DBG("subject_match [%s]", conf->eap_config->subject_match);
1686 conf->eap_config->subject_match = NULL;
1690 conf->favorite = TRUE;
1691 conf->autoconnect = TRUE;
1693 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
1694 ifname, config_id, &group_name);
1696 __free_wifi_configuration(conf);
1697 ERR("Fail to get_wifi_config_group_name");
1701 keyfile = g_key_file_new();
1702 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1703 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1705 if (conf->passphrase != NULL) {
1706 gchar *enc_data = NULL;
1708 if (conf->is_created == true)
1709 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1711 enc_data = g_strdup(conf->passphrase);
1714 ERR("Failed to encrypt the passphrase");
1716 g_free(conf->passphrase);
1717 conf->passphrase = enc_data;
1719 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1722 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1723 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1725 /* Optional field */
1726 if (conf->proxy_address != NULL) {
1727 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1728 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1731 if (conf->is_hidden != NULL) {
1732 gboolean hidden = FALSE;
1733 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1735 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1738 if (conf->frequency > 0)
1739 g_key_file_set_integer(keyfile, group_name,
1740 WIFI_CONFIG_FREQUENCY, conf->frequency);
1742 if (conf->eap_config->anonymous_identity != NULL)
1743 g_key_file_set_string(keyfile, group_name,
1744 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1746 if (conf->eap_config->ca_cert != NULL)
1747 g_key_file_set_string(keyfile, group_name,
1748 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1750 if (conf->eap_config->client_cert != NULL)
1751 g_key_file_set_string(keyfile, group_name,
1752 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1754 if (conf->eap_config->private_key != NULL)
1755 g_key_file_set_string(keyfile, group_name,
1756 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1758 if (conf->eap_config->private_key_password != NULL)
1759 g_key_file_set_string(keyfile, group_name,
1760 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1762 if (conf->eap_config->identity != NULL)
1763 g_key_file_set_string(keyfile, group_name,
1764 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1766 if (conf->eap_config->eap_type != NULL)
1767 g_key_file_set_string(keyfile, group_name,
1768 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1770 if (conf->eap_config->eap_auth_type != NULL)
1771 g_key_file_set_string(keyfile, group_name,
1772 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1774 if (conf->eap_config->subject_match != NULL)
1775 g_key_file_set_string(keyfile, group_name,
1776 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1778 ret = wifi_config_save_configuration(ifname, config_id, keyfile);
1780 INFO("Success to save eap configuration [%s]", config_id);
1781 wifi_complete_save_eap_configuration(wifi, context);
1783 INFO("Fail to save eap configuration [%s]", config_id);
1784 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1787 g_key_file_free(keyfile);
1789 __free_wifi_configuration(conf);
1791 g_variant_iter_free(iter);
1796 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1797 const gchar *ifname, const gchar *config_id)
1799 gboolean ret = FALSE;
1801 if ((wifi == NULL) || (config_id == NULL)) {
1802 ERR("Invalid parameter");
1803 netconfig_error_invalid_parameter(context);
1807 ret = _remove_configuration(ifname, config_id);
1809 /* no configuration or error */
1810 ERR("No [%s] configuration", config_id);
1811 netconfig_error_no_profile(context);
1815 wifi_complete_remove_configuration(wifi, context);
1819 /* config field key / value */
1821 * [wifi_macaddress_config_id]
1822 * Name=name (mandatory)
1823 * SSID=SSID (mandatory)
1824 * Frequency=2462 (X)
1826 * AutoConnect=true (Default true)
1827 * Modified=2015-03-20 (X)
1828 * IPv4.method=manual (O)
1829 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1830 * IPv6.method=auto (X)
1831 * IPv6.privacy=disabled (X)
1832 * IPv4.netmask_prefixlen=24 (X)
1833 * IPv4.local_address=192.0.0.1 (O)
1834 * IPv4.gateway=192.0.0.1 (O ? X ?)
1835 * Nameservers=192.168.43.22; (O)
1836 * Proxy.Method=manual (O)
1837 * Proxy.Servers=trst.com:8888; (O)
1839 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1840 const gchar *ifname, const gchar *config_id, const gchar *key, const gchar *value)
1842 gboolean ret = FALSE;
1843 gchar *keyfile_key = NULL;
1845 g_return_val_if_fail(wifi != NULL, TRUE);
1846 g_return_val_if_fail(config_id != NULL, TRUE);
1847 g_return_val_if_fail(key != NULL, TRUE);
1849 DBG("Key[%s] Value[%s]", key, value);
1851 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1852 ret = _set_field(ifname, config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1854 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1855 netconfig_error_invalid_parameter(context);
1858 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1859 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1860 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1861 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1862 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1863 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1864 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1865 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1866 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1867 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1868 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1869 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1870 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1871 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1872 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1873 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1874 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1875 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1876 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1878 ERR("Not supported key[%s]", key);
1879 netconfig_error_invalid_parameter(context);
1883 ret = _set_field(ifname, config_id, keyfile_key, (const gchar *)value);
1885 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1888 if (keyfile_key != NULL)
1889 g_free(keyfile_key);
1891 wifi_complete_set_config_field(wifi, context);
1895 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context,
1896 const gchar *ifname, const gchar *config_id)
1898 gboolean ret = FALSE;
1899 gchar *passphrase = NULL;
1901 if ((wifi == NULL) || (config_id == NULL)) {
1902 ERR("Invalid parameter");
1903 netconfig_error_invalid_parameter(context);
1907 ret = _get_field(ifname, config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1909 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1910 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1914 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1920 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1921 const gchar *ifname, int frame_id, const gchar *vsie)
1923 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1925 g_return_val_if_fail(wifi != NULL, TRUE);
1926 g_return_val_if_fail(vsie != NULL, TRUE);
1928 gboolean ret = FALSE;
1930 ret = _add_vsie(ifname, frame_id, vsie);
1932 DBG("Failed to add vsie: %s", vsie);
1933 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1937 wifi_complete_add_vsie(wifi, context);
1941 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1942 const gchar *ifname, int frame_id)
1944 DBG("Frame ID: [%d]", frame_id);
1946 g_return_val_if_fail(wifi != NULL, TRUE);
1948 gboolean ret = FALSE;
1951 ret = _get_vsie(ifname, frame_id, &vsie);
1953 DBG("Failed to get vsie for frame:[%d]", frame_id);
1954 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1958 DBG("Received vsie: %s", vsie);
1959 wifi_complete_get_vsie(wifi, context, vsie);
1964 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1965 const gchar *ifname, int frame_id, const gchar *vsie)
1967 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1969 g_return_val_if_fail(wifi != NULL, TRUE);
1970 g_return_val_if_fail(vsie != NULL, TRUE);
1972 gboolean ret = FALSE;
1974 ret = _remove_vsie(ifname, frame_id, vsie);
1976 DBG("Failed to remove vsie: %s", vsie);
1977 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1981 wifi_complete_remove_vsie(wifi, context);