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 WIFI_MAC_PATH_LENGTH 64
55 #define WIFI_MAC_ADDR_LENGTH 17
56 #define WIFI_MAC_ADDR_PATH "/sys/class/net/%s/address"
58 #define NET_DNS_ADDR_MAX 2
60 #define MAX_WIFI_PROFILES 200
62 struct wifi_eap_config {
63 gchar *anonymous_identity;
67 gchar *private_key_password;
77 gchar *gateway_address;
78 gchar *dns_address[NET_DNS_ADDR_MAX];
95 struct wifi_eap_config *eap_config;
96 wifi_ip_info_s *ip_info;
101 static void __free_wifi_configuration(struct wifi_config *conf)
108 g_free(conf->passphrase);
109 g_free(conf->security_type);
110 g_free(conf->is_hidden);
111 g_free(conf->proxy_address);
112 g_free(conf->last_error);
113 if (conf->eap_config) {
114 g_free(conf->eap_config->anonymous_identity);
115 g_free(conf->eap_config->ca_cert);
116 g_free(conf->eap_config->client_cert);
117 g_free(conf->eap_config->private_key);
118 g_free(conf->eap_config->private_key_password);
119 g_free(conf->eap_config->identity);
120 g_free(conf->eap_config->eap_type);
121 g_free(conf->eap_config->eap_auth_type);
122 g_free(conf->eap_config->subject_match);
123 g_free(conf->eap_config);
127 g_free(conf->ip_info->ip_type);
128 g_free(conf->ip_info->ip_address);
129 g_free(conf->ip_info->subnet_mask);
130 g_free(conf->ip_info->gateway_address);
131 g_free(conf->ip_info->dns_type);
132 conf->ip_info->prefix_length = 0;
134 int i = 0, count = conf->ip_info->dns_count;
136 g_free(conf->ip_info->dns_address[i]);
139 g_free(conf->ip_info);
144 static gboolean __get_mac_address(const gchar *interface_name, gchar **mac_address)
147 char buf[WIFI_MAC_ADDR_LENGTH + 1];
148 char path[WIFI_MAC_PATH_LENGTH];
149 gchar *tmp_mac = NULL;
151 gchar mac[13] = { 0, };
154 snprintf(path, WIFI_MAC_PATH_LENGTH, WIFI_MAC_ADDR_PATH, interface_name);
156 if (0 == access(path, F_OK))
157 fp = fopen(path, "r");
160 if (fgets(buf, sizeof(buf), fp) == NULL) {
161 ERR("Failed to get MAC info from %s\n", path);
166 tmp_mac = (gchar *)malloc(WIFI_MAC_ADDR_LENGTH + 1);
167 if (tmp_mac == NULL) {
168 ERR("malloc() failed");
173 memset(tmp_mac, 0, WIFI_MAC_ADDR_LENGTH + 1);
174 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADDR_LENGTH + 1);
177 ERR("Failed to open file %s\n", path);
179 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
180 if (tmp_mac == NULL) {
181 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
187 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
189 while (tmp && tmp[i]) {
195 *mac_address = g_strdup(mac);
201 gboolean wifi_config_get_group_name(const gchar *prefix,
202 const gchar *interface_name, const gchar *config_id, gchar **group_name)
204 gchar *mac_address = NULL;
205 gchar *g_name = NULL;
206 gboolean ret = FALSE;
208 ret = __get_mac_address(interface_name, &mac_address);
209 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
210 ERR("Cannot get WIFI MAC address");
215 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
216 if (g_name == NULL) {
221 *group_name = g_strdup(g_name);
229 static gboolean __get_security_type(const gchar *config_id, gchar **type)
231 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
232 *type = g_strdup(WIFI_SECURITY_NONE);
233 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
234 *type = g_strdup(WIFI_SECURITY_WEP);
235 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
236 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
237 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
238 *type = g_strdup(WIFI_SECURITY_EAP);
247 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
249 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
250 if (*config_id == NULL) {
259 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
261 GKeyFile *keyfile = NULL;
264 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
266 keyfile = netconfig_keyfile_load(path);
268 ERR("keyfile[%s] is NULL", path);
275 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
277 gboolean ret = FALSE;
280 path = g_strdup_printf("%s/%s", pathname, filename);
281 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
283 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
292 static gboolean __remove_configuration(const gchar *pathname)
296 if (__remove_file(pathname, "settings") != TRUE) {
297 ERR("Cannot remove [%s/settings]", pathname);
300 if (__remove_file(pathname, "data") != TRUE) {
301 ERR("Cannot remove [%s/data]", pathname);
305 ret = rmdir(pathname);
307 ERR("Cannot remove [%s]", pathname);
313 static gboolean _load_configuration(const gchar *interface_name,
314 const gchar *config_id, struct wifi_config *config)
318 gboolean hidden = FALSE;
319 gboolean ret = FALSE;
321 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
322 interface_name, config_id, &group_name);
324 ERR("Fail to get_wifi_config_group_name");
328 keyfile = __get_configuration_keyfile(group_name);
329 if (keyfile == NULL) {
330 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
335 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
336 DBG("name [%s]", config->name);
338 __get_security_type(config_id, &config->security_type);
339 if (config->security_type == NULL) {
340 ERR("Fail to _get_security_type");
341 g_key_file_free(keyfile);
345 DBG("security_type [%s]", config->security_type);
347 config->passphrase = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
348 DBG("passphrase []");
350 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
351 if (config->proxy_address)
352 DBG("proxy_address [%s]", config->proxy_address);
354 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
356 config->is_hidden = g_strdup("TRUE");
358 config->is_hidden = g_strdup("FALSE");
359 DBG("is_hidden [%s]", config->is_hidden);
361 config->frequency = g_key_file_get_integer(keyfile, group_name, WIFI_CONFIG_FREQUENCY, NULL);
362 if (config->frequency)
363 DBG("Frequency [%d]", config->frequency);
365 if (config->ip_info) {
366 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
367 WIFI_CONFIG_IPV4_METHOD, NULL);
368 if (config->ip_info->ip_type)
369 DBG("IPv4.Method:%s", config->ip_info->ip_type);
371 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
372 WIFI_CONFIG_IPV4_ADDRESS, NULL);
373 if (config->ip_info->ip_address)
374 DBG("IPv4.Address:%s", config->ip_info->ip_address);
378 struct in_addr netmask;
380 GError *error = NULL;
381 prefix_len = g_key_file_get_integer(keyfile, group_name,
382 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
384 DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
387 if (prefix_len > 0 && prefix_len < 32) {
388 addr = 0xffffffff << (32 - prefix_len);
389 netmask.s_addr = htonl(addr);
390 mask = inet_ntoa(netmask);
391 config->ip_info->subnet_mask = g_strdup(mask);
393 if (config->ip_info->subnet_mask)
394 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
397 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
398 group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
399 if (config->ip_info->gateway_address)
400 DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
402 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
403 WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
404 if (config->ip_info->dns_type)
405 DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
409 nameservers = g_key_file_get_string_list(keyfile, group_name,
410 WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
413 config->ip_info->dns_count = length;
415 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
416 config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
417 DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
421 g_strfreev(nameservers);
426 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
427 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
428 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
429 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
430 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
431 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
432 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
433 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
434 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
435 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
437 if (config->eap_config->anonymous_identity)
438 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
439 if (config->eap_config->ca_cert)
440 DBG("ca_cert [%s]", config->eap_config->ca_cert);
441 if (config->eap_config->client_cert)
442 DBG("client_cert [%s]", config->eap_config->client_cert);
443 if (config->eap_config->private_key)
444 DBG("private_key [%s]", config->eap_config->private_key);
445 if (config->eap_config->private_key_password)
446 DBG("private_key_password [%s]", config->eap_config->private_key_password);
447 if (config->eap_config->identity)
448 DBG("identity [%s]", config->eap_config->identity);
449 if (config->eap_config->eap_type)
450 DBG("eap_type [%s]", config->eap_config->eap_type);
451 if (config->eap_config->eap_auth_type)
452 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
453 if (config->eap_config->subject_match)
454 DBG("subject_match [%s]", config->eap_config->subject_match);
457 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
458 if (config->last_error)
459 DBG("last_error [%s]", config->last_error);
461 g_key_file_free(keyfile);
467 gboolean wifi_config_save_configuration(const gchar *interface_name,
468 const gchar *config_id, GKeyFile *keyfile)
473 gboolean ret = FALSE;
475 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
476 interface_name, config_id, &group_name);
478 ERR("Fail to get_wifi_config_group_name");
482 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
483 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
484 if (__remove_configuration(dir) != TRUE) {
485 ERR("[%s] is existed, but cannot remove", dir);
492 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
493 ERR("Cannot mkdir %s", dir);
499 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
500 netconfig_keyfile_save(keyfile, path);
508 static gboolean _remove_configuration(const gchar *interface_name, const gchar *config_id)
510 gboolean ret = FALSE;
514 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
515 interface_name, config_id, &group_name);
517 ERR("Fail to get_wifi_config_group_name");
521 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
522 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
523 if (__remove_configuration(dir) != TRUE) {
524 ERR("[%s] is existed, but cannot remove", dir);
527 INFO("Success to remove [%s]", dir);
530 ERR("[%s] is not existed", dir);
541 static gboolean _set_field(const gchar *interface_name,
542 const gchar *config_id, const gchar *key, const gchar *value)
548 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
549 interface_name, config_id, &group_name);
551 ERR("Fail to get_wifi_config_group_name");
554 DBG("group_name %s", group_name);
556 keyfile = __get_configuration_keyfile(group_name);
557 if (keyfile == NULL) {
558 ERR("Fail to __get_configuration_keyfile");
563 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
564 g_key_file_set_string(keyfile, group_name, key, value);
565 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
566 g_key_file_set_string(keyfile, group_name, key, value);
567 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
568 gboolean hidden = FALSE;
569 if (g_strcmp0(value, "TRUE") == 0)
571 g_key_file_set_boolean(keyfile, group_name, key, hidden);
572 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
573 g_key_file_set_string(keyfile, group_name, key, value);
574 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
575 g_key_file_set_string(keyfile, group_name, key, value);
576 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
577 g_key_file_set_string(keyfile, group_name, key, value);
578 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
579 g_key_file_set_string(keyfile, group_name, key, value);
580 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
581 g_key_file_set_string(keyfile, group_name, key, value);
582 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
583 g_key_file_set_string(keyfile, group_name, key, value);
584 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
585 g_key_file_set_string(keyfile, group_name, key, value);
586 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
587 g_key_file_set_string(keyfile, group_name, key, value);
589 ERR("key[%s] is not supported", key);
593 wifi_config_save_configuration(interface_name, config_id, keyfile);
595 g_key_file_free(keyfile);
601 static gboolean _get_field(const gchar *interface_name,
602 const gchar *config_id, const gchar *key, gchar **value)
607 gboolean hidden = FALSE;
608 gboolean ret = FALSE;
610 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
611 interface_name, config_id, &group_name);
613 ERR("Fail to get_wifi_config_group_name");
616 DBG("group_name %s", group_name);
618 keyfile = __get_configuration_keyfile(group_name);
619 if (keyfile == NULL) {
620 ERR("Fail to __get_configuration_keyfile");
625 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
626 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
627 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
628 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
629 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
630 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
631 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
632 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
634 val = g_strdup("TRUE");
636 val = g_strdup("FALSE");
637 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
638 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
639 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
640 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
641 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
642 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
643 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
644 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
645 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
646 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
647 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
648 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
649 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
650 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
651 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
652 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
653 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
654 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
656 ERR("Invalid key[%s]", key);
657 val = g_strdup("NOTSUPPORTED");
660 *value = g_strdup(val);
663 g_key_file_free(keyfile);
669 static GSList *_get_list(const char *mac_addr)
672 struct dirent *dp = NULL;
675 dir = opendir(CONNMAN_STORAGE);
677 ERR("Cannot open dir %s", CONNMAN_STORAGE);
681 while ((dp = readdir(dir)) != NULL) {
682 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
683 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
687 DBG("%s", dp->d_name);
689 if (netconfig_check_mac_address(dp->d_name, mac_addr)) {
690 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
691 DBG("%s", config_id);
692 list = g_slist_append(list, g_strdup(config_id));
701 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
703 gboolean ret = FALSE;
706 if ((service_profile == NULL) || (config_id == NULL)) {
707 ERR("Invalid parameter");
711 ret = __get_config_id(service_profile, &val);
712 *config_id = g_strdup(val);
718 gboolean wifi_config_remove_configuration(const gchar *interface_name,
719 const gchar *config_id)
721 gboolean ret = FALSE;
723 ret = _remove_configuration(interface_name, config_id);
728 int __netconfig_hex_char_to_num(char c)
730 if (c >= '0' && c <= '9')
733 if (c >= 'a' && c <= 'f')
736 if (c >= 'A' && c <= 'F')
742 int __netconfig_hex_to_byte(const char *hex)
746 a = __netconfig_hex_char_to_num(*hex++);
750 b = __netconfig_hex_char_to_num(*hex++);
757 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
761 const char *ipos = hex;
762 unsigned char *opos = buf;
764 for (i = 0; i < len; i++) {
765 a = __netconfig_hex_to_byte(ipos);
776 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
783 ERR("Invalid parameter.");
787 *dst = (char *) g_try_malloc0((2*src_len)+1);
789 ERR("failed to allocate memory to buffer.");
795 for (i = 0; i < src_len; i++) {
796 snprintf(buf, 3, "%02x", src[i]);
804 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
806 GVariantIter *iter_copy = NULL;
809 unsigned char *tmp_dst = NULL;
811 if (!dst || *dst || !iter) {
812 ERR("Invalid parameter");
816 iter_copy = g_variant_iter_copy(iter);
818 while (g_variant_iter_loop(iter, "y", &tmp))
820 g_variant_iter_free(iter);
822 tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
824 ERR("failed to allocate memory");
825 g_variant_iter_free(iter_copy);
830 while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
832 g_variant_iter_free(iter_copy);
838 tmp_dst[length] = '\0';
842 DBG("Length [%d]", length);
846 gboolean _add_vsie(const char *interface_name, int frame_id, const char* vsie)
848 GVariant *params = NULL;
849 GVariant *message = NULL;
850 GVariantBuilder *bytearray_builder = NULL;
855 unsigned char *bytearray = NULL;
856 size_t bytearray_len = 0;
858 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
859 DBG("Invalid parameter, frame-id: %d", frame_id);
863 vsie_len = strlen(vsie);
865 DBG("vsie length is zero");
869 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
871 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
872 if (bytearray == NULL) {
873 DBG("Failed to allocate memory to bytearray");
877 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
878 DBG("invalid vsie string");
883 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
884 for (i = 0; i < bytearray_len; i++)
885 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
887 params = g_variant_new("(iay)", frame_id, bytearray_builder);
888 g_variant_builder_unref(bytearray_builder);
890 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
891 if (if_path == NULL) {
892 ERR("Fail to get wpa_supplicant DBus path");
897 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
898 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
901 if (message == NULL) {
902 ERR("Failed to send command to wpa_supplicant");
907 DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
913 gboolean _get_vsie(const char *interface_name, int frame_id, char **vsie)
915 GVariant *params = NULL;
916 GVariant *message = NULL;
919 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
920 DBG("Invalid parameter, frame-id: %d", frame_id);
924 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
925 if (if_path == NULL) {
926 ERR("Fail to get wpa_supplicant DBus path");
930 params = g_variant_new("(i)", frame_id);
932 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
933 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
936 if (message == NULL) {
937 ERR("Failed to send command to wpa_supplicant");
940 GVariantIter *iter = NULL;
941 unsigned char *vsie_bytes = NULL;
945 g_variant_get(message, "(ay)", &iter);
947 ERR("vsie is not present");
951 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
952 if (vsie_bytes == NULL) {
953 ERR("vsie_bytes not allocated");
957 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
960 ERR("vsie not allocated.");
967 ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
973 gboolean _remove_vsie(const char *interface_name, int frame_id, const char *vsie)
975 GVariant *params = NULL;
976 GVariant *message = NULL;
977 GVariantBuilder *bytearray_builder = NULL;
982 unsigned char *bytearray = NULL;
983 size_t bytearray_len = 0;
985 if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
986 DBG("Invalid parameter, frame-id: %d", frame_id);
990 vsie_len = strlen(vsie);
992 DBG("vsie length is zero");
996 bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
998 bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
999 if (bytearray == NULL) {
1000 DBG("Failed to allocate memory to bytearray");
1004 if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
1005 DBG("invalid vsie string");
1010 bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1011 for (i = 0; i < bytearray_len; i++)
1012 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
1014 params = g_variant_new("(iay)", frame_id, bytearray_builder);
1015 g_variant_builder_unref(bytearray_builder);
1017 if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
1018 if (if_path == NULL) {
1019 ERR("Fail to get wpa_supplicant DBus path");
1024 message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
1025 if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
1028 if (message == NULL) {
1029 ERR("Failed to send command to wpa_supplicant");
1034 DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1041 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context,
1042 const gchar *ifname)
1045 GSList *config_ids = NULL;
1047 gchar **result = NULL;
1048 const gchar *mac_addr = NULL;
1050 g_return_val_if_fail(wifi != NULL, TRUE);
1052 mac_addr = wifi_state_get_mac_address(ifname);
1054 ERR("Fail to get mac-address");
1055 netconfig_error_no_profile(context);
1059 DBG("%s", mac_addr);
1060 config_ids = _get_list(mac_addr);
1061 if (config_ids == NULL) {
1062 ERR("Fail to get config list");
1063 netconfig_error_no_profile(context);
1067 length = g_slist_length(config_ids);
1068 result = g_new0(gchar *, length + 1);
1069 for (i = 0; i < length; i++) {
1070 gchar *config_id = g_slist_nth_data(config_ids, i);
1071 result[i] = g_strdup(config_id);
1074 config_ids = g_slist_nth(config_ids, 0);
1075 g_slist_free_full(config_ids, g_free);
1077 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1079 for (i = 0; i < length; i++)
1089 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1090 const gchar *ifname, const gchar *config_id)
1092 gboolean ret = FALSE;
1093 GVariantBuilder *b = NULL;
1094 struct wifi_config *conf = NULL;
1096 g_return_val_if_fail(wifi != NULL, TRUE);
1098 conf = g_new0(struct wifi_config, 1);
1099 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1101 ret = _load_configuration(ifname, config_id, conf);
1103 g_free(conf->ip_info);
1105 ERR("Fail to _load_configuration");
1106 netconfig_error_no_profile(context);
1110 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1111 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1112 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1113 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PASSPHRASE, g_variant_new_string(conf->passphrase));
1114 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1115 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FREQUENCY, g_variant_new_int32(conf->frequency));
1117 if (conf->proxy_address != NULL)
1118 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1120 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1122 if (conf->ip_info->ip_type != NULL)
1123 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1125 if (conf->ip_info->ip_address != NULL)
1126 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1128 if (conf->ip_info->subnet_mask != NULL)
1129 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1131 if (conf->ip_info->prefix_length > 0)
1132 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV6_PREFIX_LEN, g_variant_new_int32(conf->ip_info->prefix_length));
1134 if (conf->ip_info->gateway_address != NULL)
1135 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1137 if (conf->ip_info->dns_type != NULL)
1138 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1140 int i = 0, count = conf->ip_info->dns_count;
1142 if (conf->ip_info->dns_address[i] != NULL)
1143 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1148 if (conf->last_error != NULL)
1149 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1151 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1153 __free_wifi_configuration(conf);
1155 INFO("Success to load configuration [%s:%s]", ifname, config_id);
1157 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1158 g_variant_builder_unref(b);
1162 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1163 const char *netmask)
1172 mask = inet_network(netmask);
1175 /* a valid netmask must be 2^n - 1 */
1176 if ((host & (host + 1)) != 0)
1180 for (; mask; mask <<= 1)
1186 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1187 const gchar *ifname, const gchar *config_id, GVariant *configuration)
1189 gboolean ret = FALSE;
1190 struct wifi_config *conf = NULL;
1191 GKeyFile *keyfile = NULL;
1195 gchar *group_name = NULL;
1198 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1199 ERR("Invalid parameter");
1200 netconfig_error_invalid_parameter(context);
1204 conf = g_new0(struct wifi_config, 1);
1205 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1207 g_variant_get(configuration, "a{sv}", &iter);
1208 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1209 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1210 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1211 conf->name = g_strdup(g_variant_get_string(value, NULL));
1212 DBG("name [%s]", conf->name);
1216 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1217 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1218 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1219 DBG("ssid [%s]", conf->ssid);
1223 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1224 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1225 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1226 DBG("passphrase []");
1228 conf->passphrase = NULL;
1230 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1231 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1232 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1233 DBG("is_hidden [%s]", conf->is_hidden);
1235 conf->is_hidden = NULL;
1237 } else if (g_strcmp0(field, WIFI_CONFIG_FREQUENCY) == 0) {
1238 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1239 conf->frequency = g_variant_get_int32(value);
1240 DBG("frequency [%d]", conf->frequency);
1242 conf->frequency = 0;
1244 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1245 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1246 conf->is_created = g_variant_get_boolean(value);
1247 DBG("is_created [%d]", conf->is_created);
1249 conf->is_created = FALSE;
1251 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1252 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1253 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1254 DBG("IP config type [%s]", conf->ip_info->ip_type);
1256 conf->ip_info->ip_type = NULL;
1258 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1259 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1260 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1261 DBG("IP address [%s]", conf->ip_info->ip_address);
1263 conf->ip_info->ip_address = NULL;
1265 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1266 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1267 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1268 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1270 conf->ip_info->subnet_mask = NULL;
1272 } else if (g_strcmp0(field, WIFI_CONFIG_IPV6_PREFIX_LEN) == 0) {
1273 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1274 conf->ip_info->prefix_length = g_variant_get_int32(value);
1275 DBG("IPv6 Prefix Length [%d]", conf->ip_info->prefix_length);
1277 conf->ip_info->prefix_length = 0;
1279 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1280 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1281 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1282 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1284 conf->ip_info->gateway_address = NULL;
1286 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1287 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1288 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1289 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1291 conf->ip_info->dns_type = NULL;
1293 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1294 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1295 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1296 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1297 conf->ip_info->dns_count = order + 1;
1300 conf->ip_info->dns_address[order++] = NULL;
1302 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1303 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1304 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1305 DBG("proxy_address [%s]", conf->proxy_address);
1307 conf->proxy_address = NULL;
1311 conf->favorite = TRUE;
1312 conf->autoconnect = TRUE;
1314 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
1315 ifname, config_id, &group_name);
1317 __free_wifi_configuration(conf);
1318 ERR("Fail to get_wifi_config_group_name");
1319 netconfig_error_fail_save_congifuration(context);
1323 keyfile = g_key_file_new();
1324 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1325 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1327 if (conf->passphrase != NULL) {
1328 gchar *enc_data = NULL;
1330 if (conf->is_created == true)
1331 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1333 enc_data = g_strdup(conf->passphrase);
1336 ERR("Failed to encrypt the passphrase");
1338 g_free(conf->passphrase);
1339 conf->passphrase = enc_data;
1342 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1345 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1346 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1348 #if GLIB_CHECK_VERSION(2,62,0)
1349 gint64 real_time = 0;
1350 GDateTime *dt_real_time = NULL;
1352 real_time = g_get_real_time();
1353 dt_real_time = g_date_time_new_from_unix_utc(real_time);
1355 gchar *str = g_date_time_format_iso8601(dt_real_time);
1356 g_date_time_unref(dt_real_time);
1358 g_key_file_set_string(keyfile, group_name,
1359 WIFI_CONFIG_MODIFIED, str);
1363 #else /* GLIB_CHECK_VERSION(2,62,0) */
1365 g_get_current_time(&modified);
1366 gchar *str = g_time_val_to_iso8601(&modified);
1368 g_key_file_set_string(keyfile, group_name,
1369 WIFI_CONFIG_MODIFIED, str);
1372 #endif /* GLIB_CHECK_VERSION(2,62,0) */
1374 /* Optional field */
1375 if (conf->proxy_address != NULL) {
1376 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1377 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1380 if (conf->is_hidden != NULL) {
1381 gboolean hidden = FALSE;
1382 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1384 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1387 if (conf->frequency > 0)
1388 g_key_file_set_integer(keyfile, group_name,
1389 WIFI_CONFIG_FREQUENCY, conf->frequency);
1391 if (conf->ip_info->ip_type != NULL)
1392 g_key_file_set_string(keyfile, group_name,
1393 WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1395 if (conf->ip_info->ip_address != NULL)
1396 g_key_file_set_string(keyfile, group_name,
1397 WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1399 if (conf->ip_info->subnet_mask != NULL) {
1400 unsigned char prefix_len;
1401 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1402 conf->ip_info->subnet_mask);
1403 if (prefix_len > 0 && prefix_len < 32)
1404 g_key_file_set_integer(keyfile, group_name,
1405 WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1408 if (conf->ip_info->prefix_length > 0)
1409 g_key_file_set_integer(keyfile, group_name,
1410 WIFI_CONFIG_IPV6_PREFIX_LEN, conf->ip_info->prefix_length);
1412 if (conf->ip_info->gateway_address != NULL)
1413 g_key_file_set_string(keyfile, group_name,
1414 WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1416 if (conf->ip_info->dns_type != NULL)
1417 g_key_file_set_string(keyfile, group_name,
1418 WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1420 int i = 0, count = conf->ip_info->dns_count;
1422 if (conf->ip_info->dns_address[i] != NULL)
1423 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1424 conf->ip_info->dns_address[i]);
1429 ret = wifi_config_save_configuration(ifname, config_id, keyfile);
1431 INFO("Success to save configuration [%s]", config_id);
1432 wifi_complete_save_configuration(wifi, context);
1434 if (get_files_count(CONNMAN_STORAGE) > MAX_WIFI_PROFILES) {
1435 file = get_least_recently_profile(CONNMAN_STORAGE);
1437 gchar *profileName = g_strdup_printf(CONNMAN_STORAGE "/%s", file);
1438 INFO("least modified file: %s", profileName);
1440 if (__remove_configuration(profileName) != TRUE)
1441 DBG("Failed to remove profile: [%s]", profileName);
1443 ERR("Profile: [%s] does not exist", file);
1445 g_free(profileName);
1449 INFO("Fail to save configuration [%s]", config_id);
1450 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1453 g_key_file_free(keyfile);
1455 __free_wifi_configuration(conf);
1457 g_variant_iter_free(iter);
1462 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1463 const gchar *ifname, const gchar *config_id)
1465 gboolean ret = FALSE;
1466 GVariantBuilder *b = NULL;
1467 struct wifi_config *conf = NULL;
1469 g_return_val_if_fail(wifi != NULL, TRUE);
1471 conf = g_new0(struct wifi_config, 1);
1472 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1473 conf->ip_info = g_new0(wifi_ip_info_s, 1);
1475 ret = _load_configuration(ifname, config_id, conf);
1477 g_free(conf->eap_config);
1478 g_free(conf->ip_info);
1480 ERR("Fail to _load_configuration");
1481 netconfig_error_no_profile(context);
1485 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1486 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1487 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1488 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1489 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FREQUENCY, g_variant_new_int32(conf->frequency));
1491 if (conf->proxy_address != NULL)
1492 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1494 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1496 if (conf->last_error != NULL)
1497 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1499 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1501 if (conf->eap_config != NULL) {
1502 if (conf->eap_config->anonymous_identity != NULL)
1503 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1505 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1507 if (conf->eap_config->ca_cert != NULL)
1508 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1510 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1512 if (conf->eap_config->client_cert != NULL)
1513 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1515 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1517 if (conf->eap_config->private_key != NULL)
1518 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1520 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1522 if (conf->eap_config->private_key_password != NULL)
1523 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1525 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1527 if (conf->eap_config->identity != NULL)
1528 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1530 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1532 if (conf->eap_config->eap_type != NULL)
1533 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1535 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1537 if (conf->eap_config->eap_auth_type != NULL)
1538 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1540 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1542 if (conf->eap_config->subject_match != NULL)
1543 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1545 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1548 __free_wifi_configuration(conf);
1550 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1551 g_variant_builder_unref(b);
1555 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1556 const gchar *ifname, const gchar *config_id, GVariant *configuration)
1558 gboolean ret = FALSE;
1559 struct wifi_config *conf = NULL;
1560 GKeyFile *keyfile = NULL;
1564 gchar *group_name = NULL;
1566 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1567 ERR("Invalid parameter");
1568 netconfig_error_invalid_parameter(context);
1572 conf = g_new0(struct wifi_config, 1);
1573 conf->eap_config = g_new0(struct wifi_eap_config, 1);
1575 g_variant_get(configuration, "a{sv}", &iter);
1576 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1577 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1578 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1579 conf->name = g_strdup(g_variant_get_string(value, NULL));
1580 DBG("name [%s]", conf->name);
1584 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1585 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1586 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1587 DBG("ssid [%s]", conf->ssid);
1591 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1592 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1593 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1594 DBG("passphrase [%s]", conf->passphrase);
1596 conf->passphrase = NULL;
1598 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1599 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1600 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1601 DBG("is_hidden [%s]", conf->is_hidden);
1603 conf->is_hidden = NULL;
1605 } else if (g_strcmp0(field, WIFI_CONFIG_FREQUENCY) == 0) {
1606 if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1607 conf->frequency = g_variant_get_int32(value);
1608 DBG("frequency [%d]", conf->frequency);
1610 conf->frequency = 0;
1612 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1613 if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1614 conf->is_created = g_variant_get_boolean(value);
1615 DBG("is_created [%d]", conf->is_created);
1617 conf->is_created = FALSE;
1619 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1620 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1621 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1622 DBG("proxy_address [%s]", conf->proxy_address);
1624 conf->proxy_address = NULL;
1626 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1627 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1628 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1629 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1631 conf->eap_config->anonymous_identity = NULL;
1633 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1634 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1635 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1636 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1638 conf->eap_config->ca_cert = NULL;
1640 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1641 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1642 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1643 DBG("client_cert [%s]", conf->eap_config->client_cert);
1645 conf->eap_config->client_cert = NULL;
1647 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1648 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1649 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1650 DBG("private_key [%s]", conf->eap_config->private_key);
1652 conf->eap_config->private_key = NULL;
1654 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1655 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1656 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1657 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1659 conf->eap_config->private_key_password = NULL;
1661 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1662 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1663 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1664 DBG("identity [%s]", conf->eap_config->identity);
1666 conf->eap_config->identity = NULL;
1668 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1669 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1670 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1671 DBG("eap_type [%s]", conf->eap_config->eap_type);
1673 conf->eap_config->eap_type = NULL;
1675 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1676 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1677 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1678 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1680 conf->eap_config->eap_auth_type = NULL;
1682 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1683 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1684 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1685 DBG("subject_match [%s]", conf->eap_config->subject_match);
1687 conf->eap_config->subject_match = NULL;
1691 conf->favorite = TRUE;
1692 conf->autoconnect = TRUE;
1694 ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
1695 ifname, config_id, &group_name);
1697 __free_wifi_configuration(conf);
1698 ERR("Fail to get_wifi_config_group_name");
1702 keyfile = g_key_file_new();
1703 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1704 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1706 if (conf->passphrase != NULL) {
1707 gchar *enc_data = NULL;
1709 if (conf->is_created == true)
1710 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1712 enc_data = g_strdup(conf->passphrase);
1715 ERR("Failed to encrypt the passphrase");
1717 g_free(conf->passphrase);
1718 conf->passphrase = enc_data;
1720 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1723 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1724 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1726 /* Optional field */
1727 if (conf->proxy_address != NULL) {
1728 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1729 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1732 if (conf->is_hidden != NULL) {
1733 gboolean hidden = FALSE;
1734 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1736 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1739 if (conf->frequency > 0)
1740 g_key_file_set_integer(keyfile, group_name,
1741 WIFI_CONFIG_FREQUENCY, conf->frequency);
1743 if (conf->eap_config->anonymous_identity != NULL)
1744 g_key_file_set_string(keyfile, group_name,
1745 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1747 if (conf->eap_config->ca_cert != NULL)
1748 g_key_file_set_string(keyfile, group_name,
1749 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1751 if (conf->eap_config->client_cert != NULL)
1752 g_key_file_set_string(keyfile, group_name,
1753 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1755 if (conf->eap_config->private_key != NULL)
1756 g_key_file_set_string(keyfile, group_name,
1757 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1759 if (conf->eap_config->private_key_password != NULL)
1760 g_key_file_set_string(keyfile, group_name,
1761 WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1763 if (conf->eap_config->identity != NULL)
1764 g_key_file_set_string(keyfile, group_name,
1765 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1767 if (conf->eap_config->eap_type != NULL)
1768 g_key_file_set_string(keyfile, group_name,
1769 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1771 if (conf->eap_config->eap_auth_type != NULL)
1772 g_key_file_set_string(keyfile, group_name,
1773 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1775 if (conf->eap_config->subject_match != NULL)
1776 g_key_file_set_string(keyfile, group_name,
1777 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1779 ret = wifi_config_save_configuration(ifname, config_id, keyfile);
1781 INFO("Success to save eap configuration [%s]", config_id);
1782 wifi_complete_save_eap_configuration(wifi, context);
1784 INFO("Fail to save eap configuration [%s]", config_id);
1785 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1788 g_key_file_free(keyfile);
1790 __free_wifi_configuration(conf);
1792 g_variant_iter_free(iter);
1797 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1798 const gchar *ifname, const gchar *config_id)
1800 gboolean ret = FALSE;
1802 if ((wifi == NULL) || (config_id == NULL)) {
1803 ERR("Invalid parameter");
1804 netconfig_error_invalid_parameter(context);
1808 ret = _remove_configuration(ifname, config_id);
1810 /* no configuration or error */
1811 ERR("No [%s] configuration", config_id);
1812 netconfig_error_no_profile(context);
1816 wifi_complete_remove_configuration(wifi, context);
1820 /* config field key / value */
1822 * [wifi_macaddress_config_id]
1823 * Name=name (mandatory)
1824 * SSID=SSID (mandatory)
1825 * Frequency=2462 (X)
1827 * AutoConnect=true (Default true)
1828 * Modified=2015-03-20 (X)
1829 * IPv4.method=manual (O)
1830 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1831 * IPv6.method=auto (X)
1832 * IPv6.privacy=disabled (X)
1833 * IPv4.netmask_prefixlen=24 (X)
1834 * IPv4.local_address=192.0.0.1 (O)
1835 * IPv4.gateway=192.0.0.1 (O ? X ?)
1836 * Nameservers=192.168.43.22; (O)
1837 * Proxy.Method=manual (O)
1838 * Proxy.Servers=trst.com:8888; (O)
1840 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1841 const gchar *ifname, const gchar *config_id, const gchar *key, const gchar *value)
1843 gboolean ret = FALSE;
1844 gchar *keyfile_key = NULL;
1846 g_return_val_if_fail(wifi != NULL, TRUE);
1847 g_return_val_if_fail(config_id != NULL, TRUE);
1848 g_return_val_if_fail(key != NULL, TRUE);
1850 DBG("Key[%s] Value[%s]", key, value);
1852 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1853 ret = _set_field(ifname, config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1855 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1856 netconfig_error_invalid_parameter(context);
1859 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1860 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1861 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1862 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1863 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1864 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1865 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1866 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1867 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1868 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1869 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1870 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1871 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1872 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1873 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1874 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1875 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1876 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1877 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1879 ERR("Not supported key[%s]", key);
1880 netconfig_error_invalid_parameter(context);
1884 ret = _set_field(ifname, config_id, keyfile_key, (const gchar *)value);
1886 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1889 if (keyfile_key != NULL)
1890 g_free(keyfile_key);
1892 wifi_complete_set_config_field(wifi, context);
1896 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context,
1897 const gchar *ifname, const gchar *config_id)
1899 gboolean ret = FALSE;
1900 gchar *passphrase = NULL;
1902 if ((wifi == NULL) || (config_id == NULL)) {
1903 ERR("Invalid parameter");
1904 netconfig_error_invalid_parameter(context);
1908 ret = _get_field(ifname, config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1910 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1911 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1915 wifi_complete_get_config_passphrase(wifi, context, passphrase);
1921 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1922 const gchar *ifname, int frame_id, const gchar *vsie)
1924 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1926 g_return_val_if_fail(wifi != NULL, TRUE);
1927 g_return_val_if_fail(vsie != NULL, TRUE);
1929 gboolean ret = FALSE;
1931 ret = _add_vsie(ifname, frame_id, vsie);
1933 DBG("Failed to add vsie: %s", vsie);
1934 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1938 wifi_complete_add_vsie(wifi, context);
1942 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1943 const gchar *ifname, int frame_id)
1945 DBG("Frame ID: [%d]", frame_id);
1947 g_return_val_if_fail(wifi != NULL, TRUE);
1949 gboolean ret = FALSE;
1952 ret = _get_vsie(ifname, frame_id, &vsie);
1954 DBG("Failed to get vsie for frame:[%d]", frame_id);
1955 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1959 DBG("Received vsie: %s", vsie);
1960 wifi_complete_get_vsie(wifi, context, vsie);
1965 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1966 const gchar *ifname, int frame_id, const gchar *vsie)
1968 DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1970 g_return_val_if_fail(wifi != NULL, TRUE);
1971 g_return_val_if_fail(vsie != NULL, TRUE);
1973 gboolean ret = FALSE;
1975 ret = _remove_vsie(ifname, frame_id, vsie);
1977 DBG("Failed to remove vsie: %s", vsie);
1978 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1982 wifi_complete_remove_vsie(wifi, context);