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.
22 #include <sys/types.h>
33 #include "wifi-config.h"
35 #define CONNMAN_STORAGE "/var/lib/connman"
37 #define WIFI_SECURITY_NONE "none"
38 #define WIFI_SECURITY_WEP "wep"
39 #define WIFI_SECURITY_WPA_PSK "psk"
40 #define WIFI_SECURITY_EAP "ieee8021x"
42 #define WIFI_CONFIG_PREFIX "wifi_"
43 #define MAC_ADDRESS_LENGTH 12
44 #define WIFI_PREFIX_LENGTH MAC_ADDRESS_LENGTH + 6 /* wifi_485a3f2f506a_ */
45 #define PROFILE_PREFIX_LENGTH WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
47 #define WIFI_MAC_ADD_LENGTH 17
48 #define WIFI_MAC_ADD_PATH "/sys/class/net/wlan0/address"
50 struct wifi_eap_config {
51 gchar *anonymous_identity;
70 struct wifi_eap_config *eap_config;
74 static void __free_wifi_configuration(struct wifi_config *conf)
81 g_free(conf->passphrase);
82 g_free(conf->security_type);
83 g_free(conf->is_hidden);
84 g_free(conf->proxy_address);
85 g_free(conf->last_error);
86 if (conf->eap_config) {
87 g_free(conf->eap_config->anonymous_identity);
88 g_free(conf->eap_config->ca_cert);
89 g_free(conf->eap_config->client_cert);
90 g_free(conf->eap_config->private_key);
91 g_free(conf->eap_config->identity);
92 g_free(conf->eap_config->eap_type);
93 g_free(conf->eap_config->eap_auth_type);
94 g_free(conf->eap_config->subject_match);
95 g_free(conf->eap_config);
100 static gboolean __get_mac_address(gchar **mac_address)
102 gchar *tmp_mac = NULL;
104 gchar mac[13] = { 0, };
108 char buf[WIFI_MAC_ADD_LENGTH + 1];
109 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
110 fp = fopen(WIFI_MAC_ADD_PATH, "r");
113 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
118 if (fgets(buf, sizeof(buf), fp) == NULL) {
119 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
124 tmp_mac = (char *)g_try_malloc0(WIFI_MAC_ADD_LENGTH + 1);
125 if (tmp_mac == NULL) {
126 ERR("malloc() failed");
131 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
134 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
135 if (tmp_mac == NULL) {
136 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
141 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
143 while (tmp && tmp[i]) {
149 *mac_address = g_strdup(mac);
154 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
156 gchar *mac_address = NULL;
157 gchar *g_name = NULL;
158 gboolean ret = FALSE;
160 ret = __get_mac_address(&mac_address);
161 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
162 ERR("Cannot get WIFI MAC address");
166 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
167 if (g_name == NULL) {
172 *group_name = g_strdup(g_name);
180 static gboolean __get_security_type(const gchar *config_id, gchar **type)
182 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
183 *type = g_strdup(WIFI_SECURITY_NONE);
184 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
185 *type = g_strdup(WIFI_SECURITY_WEP);
186 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
187 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
188 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
189 *type = g_strdup(WIFI_SECURITY_EAP);
198 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
200 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
201 if (*config_id == NULL) {
210 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
212 GKeyFile *keyfile = NULL;
215 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
217 keyfile = netconfig_keyfile_load(path);
218 if (keyfile == NULL) {
219 ERR("keyfile[%s] is NULL", path);
227 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
229 gboolean ret = FALSE;
232 path = g_strdup_printf("%s/%s", pathname, filename);
233 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
235 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
244 static gboolean __remove_configuration(const gchar *pathname)
248 if (__remove_file(pathname, "settings") != TRUE) {
249 ERR("Cannot remove [%s/settings]", pathname);
252 if (__remove_file(pathname, "data") != TRUE) {
253 ERR("Cannot remove [%s/data]", pathname);
257 ret = rmdir(pathname);
259 ERR("Cannot remove [%s]", pathname);
266 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
270 gboolean hidden = FALSE;
271 gboolean ret = FALSE;
273 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
275 ERR("Fail to get_wifi_config_group_name");
279 keyfile = __get_configuration_keyfile(group_name);
280 if (keyfile == NULL) {
281 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
286 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
287 DBG("name [%s]", config->name);
289 ret = __get_security_type(config_id, &config->security_type);
291 ERR("Fail to _get_security_type");
292 g_key_file_free(keyfile);
296 DBG("security_type [%s]", config->security_type);
298 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
299 if (config->proxy_address)
300 DBG("proxy_address [%s]", config->proxy_address);
302 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
304 config->is_hidden = g_strdup("TRUE");
306 config->is_hidden = g_strdup("FALSE");
307 DBG("is_hidden [%s]", config->is_hidden);
309 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
310 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
311 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
312 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
313 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
314 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
315 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
316 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
317 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
319 if (config->eap_config->anonymous_identity)
320 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
321 if (config->eap_config->ca_cert)
322 DBG("ca_cert [%s]", config->eap_config->ca_cert);
323 if (config->eap_config->client_cert)
324 DBG("client_cert [%s]", config->eap_config->client_cert);
325 if (config->eap_config->private_key)
326 DBG("private_key [%s]", config->eap_config->private_key);
327 if (config->eap_config->identity)
328 DBG("identity [%s]", config->eap_config->identity);
329 if (config->eap_config->eap_type)
330 DBG("eap_type [%s]", config->eap_config->eap_type);
331 if (config->eap_config->eap_auth_type)
332 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
333 if (config->eap_config->subject_match)
334 DBG("subject_match [%s]", config->eap_config->subject_match);
337 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
338 if (config->last_error)
339 DBG("last_error [%s]", config->last_error);
341 g_key_file_free(keyfile);
347 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
352 gboolean ret = FALSE;
354 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
356 ERR("Fail to get_wifi_config_group_name");
360 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
361 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
362 if (__remove_configuration(dir) != TRUE) {
363 ERR("[%s] is existed, but cannot remove", dir);
370 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
371 ERR("Cannot mkdir %s", dir);
377 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
378 netconfig_keyfile_save(keyfile, path);
386 static gboolean _remove_configuration(const gchar *config_id)
388 gboolean ret = FALSE;
392 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
394 ERR("Fail to get_wifi_config_group_name");
398 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
399 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
400 if (__remove_configuration(dir) != TRUE) {
401 ERR("[%s] is existed, but cannot remove", dir);
404 INFO("Success to remove [%s]", dir);
407 ERR("[%s] is not existed", dir);
418 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
424 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
426 ERR("Fail to get_wifi_config_group_name");
429 DBG("group_name %s", group_name);
431 keyfile = __get_configuration_keyfile(group_name);
432 if (keyfile == NULL) {
433 ERR("Fail to __get_configuration_keyfile");
437 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
438 g_key_file_set_string(keyfile, group_name, key, value);
439 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
440 g_key_file_set_string(keyfile, group_name, key, value);
441 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
442 gboolean hidden = FALSE;
443 if (g_strcmp0(value, "TRUE") == 0)
445 g_key_file_set_boolean(keyfile, group_name, key, hidden);
446 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
447 g_key_file_set_string(keyfile, group_name, key, value);
448 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
449 g_key_file_set_string(keyfile, group_name, key, value);
450 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
451 g_key_file_set_string(keyfile, group_name, key, value);
452 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
453 g_key_file_set_string(keyfile, group_name, key, value);
454 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
455 g_key_file_set_string(keyfile, group_name, key, value);
456 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
457 g_key_file_set_string(keyfile, group_name, key, value);
458 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
459 g_key_file_set_string(keyfile, group_name, key, value);
460 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
461 g_key_file_set_string(keyfile, group_name, key, value);
463 ERR("key[%s] is not supported", key);
467 _save_configuration(config_id, keyfile);
469 g_key_file_free(keyfile);
475 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
480 gboolean hidden = FALSE;
481 gboolean ret = FALSE;
483 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
485 ERR("Fail to get_wifi_config_group_name");
488 DBG("group_name %s", group_name);
490 keyfile = __get_configuration_keyfile(group_name);
491 if (keyfile == NULL) {
492 ERR("Fail to __get_configuration_keyfile");
496 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
497 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
498 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
499 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
500 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
501 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
502 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
503 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
505 val = g_strdup("TRUE");
507 val = g_strdup("FALSE");
508 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
509 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
510 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
511 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
512 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
513 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
514 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
515 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
516 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
517 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
518 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
519 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
520 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
521 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
522 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
523 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
524 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
525 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
527 ERR("Invalid key[%s]", key);
528 val = g_strdup("NOTSUPPORTED");
531 *value = g_strdup(val);
534 g_key_file_free(keyfile);
540 static GSList *_get_list(void)
543 struct dirent ent_struct;
544 struct dirent *dp = NULL;
547 dir = opendir(CONNMAN_STORAGE);
549 ERR("Cannot open dir %s", CONNMAN_STORAGE);
553 while ((readdir_r(dir, &ent_struct, &dp) == 0) && dp) {
554 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
555 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
558 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
559 list = g_slist_append(list, g_strdup(config_id));
567 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
569 gboolean ret = FALSE;
572 if ((service_profile == NULL) || (config_id == NULL)) {
573 ERR("Invalid parameter");
577 ret = __get_config_id(service_profile, &val);
578 *config_id = g_strdup(val);
584 gboolean wifi_config_remove_configuration(const gchar *config_id)
586 gboolean ret = FALSE;
588 ret = _remove_configuration(config_id);
594 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
597 GSList *config_ids = NULL;
599 gchar **result = NULL;
601 g_return_val_if_fail(wifi != NULL, FALSE);
603 config_ids = _get_list();
604 if (config_ids == NULL) {
605 netconfig_error_no_profile(context);
606 ERR("Fail to get config list");
610 length = g_slist_length(config_ids);
611 result = g_new0(gchar *, length + 1);
612 for (i = 0; i < length; i++) {
613 gchar *config_id = g_slist_nth_data(config_ids, i);
614 result[i] = g_strdup(config_id);
617 config_ids = g_slist_nth(config_ids, 0);
618 g_slist_free_full(config_ids, g_free);
620 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
622 for (i = 0; i < length; i++)
632 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
633 const gchar *config_id)
635 gboolean ret = FALSE;
636 GVariantBuilder *b = NULL;
637 struct wifi_config *conf = NULL;
639 g_return_val_if_fail(wifi != NULL, FALSE);
641 conf = g_new0(struct wifi_config, 1);
643 ret = _load_configuration(config_id, conf);
646 ERR("Fail to _load_configuration");
647 netconfig_error_no_profile(context);
651 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
652 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
653 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
654 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
656 if (conf->proxy_address != NULL)
657 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
659 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
661 if (conf->last_error != NULL)
662 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
664 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
666 g_free(conf->proxy_address);
667 g_free(conf->last_error);
669 g_free(conf->security_type);
670 g_free(conf->is_hidden);
673 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
674 g_variant_builder_unref(b);
678 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
679 const gchar *config_id, GVariant *configuration)
681 gboolean ret = FALSE;
682 struct wifi_config *conf = NULL;
683 GKeyFile *keyfile = NULL;
687 gchar *group_name = NULL;
689 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
690 ERR("Invalid parameter");
691 netconfig_error_invalid_parameter(context);
695 conf = g_new0(struct wifi_config, 1);
697 g_variant_get(configuration, "a{sv}", &iter);
698 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
699 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
700 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
701 conf->name = g_strdup(g_variant_get_string(value, NULL));
702 DBG("name [%s]", conf->name);
706 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
707 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
708 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
709 DBG("ssid [%s]", conf->ssid);
713 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
714 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
715 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
716 DBG("passphrase []");
718 conf->passphrase = NULL;
720 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
721 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
722 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
723 DBG("is_hidden [%s]", conf->is_hidden);
725 conf->is_hidden = NULL;
727 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
728 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
729 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
730 DBG("proxy_address [%s]", conf->proxy_address);
732 conf->proxy_address = NULL;
736 conf->favorite = TRUE;
737 conf->autoconnect = TRUE;
739 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
741 ERR("Fail to get_wifi_config_group_name");
745 keyfile = g_key_file_new();
746 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
747 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
749 if (conf->passphrase != NULL)
750 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
752 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
753 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
756 if (conf->proxy_address != NULL) {
757 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
758 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
761 if (conf->is_hidden != NULL) {
762 gboolean hidden = FALSE;
763 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
765 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
768 ret = _save_configuration(config_id, keyfile);
770 INFO("Success to save configuration [%s]", config_id);
771 wifi_complete_save_configuration(wifi, context);
773 INFO("Fail to save configuration [%s]", config_id);
774 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
777 g_key_file_free(keyfile);
780 g_free(conf->passphrase);
781 g_free(conf->is_hidden);
782 g_free(conf->proxy_address);
785 g_variant_iter_free(iter);
790 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
791 const gchar *config_id)
793 gboolean ret = FALSE;
794 GVariantBuilder *b = NULL;
795 struct wifi_config *conf = NULL;
797 g_return_val_if_fail(wifi != NULL, FALSE);
799 conf = g_new0(struct wifi_config, 1);
800 conf->eap_config = g_new0(struct wifi_eap_config, 1);
802 ret = _load_configuration(config_id, conf);
804 g_free(conf->eap_config);
806 ERR("Fail to _load_configuration");
807 netconfig_error_no_profile(context);
811 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
812 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
813 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
814 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
815 if (conf->proxy_address != NULL)
816 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
818 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
820 if (conf->last_error != NULL)
821 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
823 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
825 if (conf->eap_config != NULL) {
826 if (conf->eap_config->anonymous_identity != NULL)
827 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
829 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
831 if (conf->eap_config->ca_cert != NULL)
832 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
834 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
836 if (conf->eap_config->client_cert != NULL)
837 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
839 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
841 if (conf->eap_config->private_key != NULL)
842 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
844 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
846 if (conf->eap_config->identity != NULL)
847 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
849 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
851 if (conf->eap_config->eap_type != NULL)
852 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
854 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
856 if (conf->eap_config->eap_auth_type != NULL)
857 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
859 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
861 if (conf->eap_config->subject_match != NULL)
862 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
864 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
867 __free_wifi_configuration(conf);
869 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
870 g_variant_builder_unref(b);
874 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
875 const gchar *config_id, GVariant *configuration)
877 gboolean ret = FALSE;
878 struct wifi_config *conf = NULL;
879 GKeyFile *keyfile = NULL;
883 gchar *group_name = NULL;
885 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
886 ERR("Invalid parameter");
887 netconfig_error_invalid_parameter(context);
891 conf = g_new0(struct wifi_config, 1);
892 conf->eap_config = g_new0(struct wifi_eap_config, 1);
894 g_variant_get(configuration, "a{sv}", &iter);
895 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
896 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
897 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
898 conf->name = g_strdup(g_variant_get_string(value, NULL));
899 DBG("name [%s]", conf->name);
903 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
904 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
905 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
906 DBG("ssid [%s]", conf->ssid);
910 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
911 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
912 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
913 DBG("passphrase [%s]", conf->passphrase);
915 conf->passphrase = NULL;
917 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
918 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
919 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
920 DBG("is_hidden [%s]", conf->is_hidden);
922 conf->is_hidden = NULL;
924 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
925 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
926 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
927 DBG("proxy_address [%s]", conf->proxy_address);
929 conf->proxy_address = NULL;
931 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
932 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
933 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
934 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
936 conf->eap_config->anonymous_identity = NULL;
938 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
939 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
940 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
941 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
943 conf->eap_config->ca_cert = NULL;
945 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
946 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
947 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
948 DBG("client_cert [%s]", conf->eap_config->client_cert);
950 conf->eap_config->client_cert = NULL;
952 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
953 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
954 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
955 DBG("private_key [%s]", conf->eap_config->private_key);
957 conf->eap_config->private_key = NULL;
959 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
960 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
961 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
962 DBG("identity [%s]", conf->eap_config->identity);
964 conf->eap_config->identity = NULL;
966 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
967 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
968 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
969 DBG("eap_type [%s]", conf->eap_config->eap_type);
971 conf->eap_config->eap_type = NULL;
973 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
974 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
975 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
976 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
978 conf->eap_config->eap_auth_type = NULL;
980 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
981 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
982 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
983 DBG("subject_match [%s]", conf->eap_config->subject_match);
985 conf->eap_config->subject_match = NULL;
989 conf->favorite = TRUE;
990 conf->autoconnect = TRUE;
992 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
994 __free_wifi_configuration(conf);
995 ERR("Fail to get_wifi_config_group_name");
999 keyfile = g_key_file_new();
1000 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1001 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1003 if (conf->passphrase != NULL)
1004 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1006 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1007 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1009 /* Optional field */
1010 if (conf->proxy_address != NULL) {
1011 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1012 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1015 if (conf->is_hidden != NULL) {
1016 gboolean hidden = FALSE;
1017 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1019 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1022 if (conf->eap_config->anonymous_identity != NULL)
1023 g_key_file_set_string(keyfile, group_name,
1024 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1026 if (conf->eap_config->ca_cert != NULL)
1027 g_key_file_set_string(keyfile, group_name,
1028 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1030 if (conf->eap_config->client_cert != NULL)
1031 g_key_file_set_string(keyfile, group_name,
1032 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1034 if (conf->eap_config->private_key != NULL)
1035 g_key_file_set_string(keyfile, group_name,
1036 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1038 if (conf->eap_config->identity != NULL)
1039 g_key_file_set_string(keyfile, group_name,
1040 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1042 if (conf->eap_config->eap_type != NULL)
1043 g_key_file_set_string(keyfile, group_name,
1044 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1046 if (conf->eap_config->eap_auth_type != NULL)
1047 g_key_file_set_string(keyfile, group_name,
1048 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1050 if (conf->eap_config->subject_match != NULL)
1051 g_key_file_set_string(keyfile, group_name,
1052 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1054 ret = _save_configuration(config_id, keyfile);
1056 INFO("Success to save eap configuration [%s]", config_id);
1057 wifi_complete_save_eap_configuration(wifi, context);
1059 INFO("Fail to save eap configuration [%s]", config_id);
1060 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1063 g_key_file_free(keyfile);
1064 __free_wifi_configuration(conf);
1066 g_variant_iter_free(iter);
1071 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1073 gboolean ret = FALSE;
1075 if ((wifi == NULL) || (config_id == NULL)) {
1076 ERR("Invalid parameter");
1077 netconfig_error_invalid_parameter(context);
1081 ret = _remove_configuration(config_id);
1083 /* no configuration or error */
1084 ERR("No [%s] configuration", config_id);
1085 netconfig_error_no_profile(context);
1089 wifi_complete_remove_configuration(wifi, context);
1093 /* config field key / value */
1095 * [wifi_macaddress_config_id]
1096 * Name=name (mandatory)
1097 * SSID=SSID (mandatory)
1098 * Frequency=2462 (X)
1100 * AutoConnect=true (Default true)
1101 * Modified=2015-03-20 (X)
1102 * IPv4.method=manual (O)
1103 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1104 * IPv6.method=auto (X)
1105 * IPv6.privacy=disabled (X)
1106 * IPv4.netmask_prefixlen=24 (X)
1107 * IPv4.local_address=192.0.0.1 (O)
1108 * IPv4.gateway=192.0.0.1 (O ? X ?)
1109 * Nameservers=192.168.43.22; (O)
1110 * Proxy.Method=manual (O)
1111 * Proxy.Servers=trst.com:8888; (O)
1113 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1114 const gchar *config_id, const gchar *key, const gchar *value)
1116 gboolean ret = FALSE;
1117 gchar *keyfile_key = NULL;
1119 g_return_val_if_fail(wifi != NULL, FALSE);
1120 g_return_val_if_fail(config_id != NULL, FALSE);
1121 g_return_val_if_fail(key != NULL, FALSE);
1123 DBG("Key[%s] Value[%d]", key, value);
1125 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1126 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1128 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1129 netconfig_error_invalid_parameter(context);
1132 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1133 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1134 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1135 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1136 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1137 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1138 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1139 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1140 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1141 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1142 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1143 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1144 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1145 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1146 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1147 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1148 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1149 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1150 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1152 ERR("Not supported key[%s]", key);
1153 netconfig_error_invalid_parameter(context);
1157 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1159 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1163 if (keyfile_key != NULL)
1164 g_free(keyfile_key);
1166 wifi_complete_set_config_field(wifi, context);
1170 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1172 gboolean ret = FALSE;
1173 gchar *passphrase = NULL;
1175 if ((wifi == NULL) || (config_id == NULL)) {
1176 ERR("Invalid parameter");
1177 netconfig_error_invalid_parameter(context);
1181 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1183 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1184 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1188 wifi_complete_get_config_passphrase(wifi, context, passphrase);