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, };
109 char buf[WIFI_MAC_ADD_LENGTH + 1];
110 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
111 fp = fopen(WIFI_MAC_ADD_PATH, "r");
114 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
119 if (fgets(buf, sizeof(buf), fp) == NULL) {
120 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
125 tmp_mac = (char *)g_try_malloc0(WIFI_MAC_ADD_LENGTH + 1);
126 if (tmp_mac == NULL) {
127 ERR("malloc() failed");
132 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
135 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
136 if (tmp_mac == NULL) {
137 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
142 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
144 while (tmp && tmp[i]) {
150 *mac_address = g_strdup(mac);
155 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
157 gchar *mac_address = NULL;
158 gchar *g_name = NULL;
159 gboolean ret = FALSE;
161 ret = __get_mac_address(&mac_address);
162 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
163 ERR("Cannot get WIFI MAC address");
167 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
168 if (g_name == NULL) {
173 *group_name = g_strdup(g_name);
181 static gboolean __get_security_type(const gchar *config_id, gchar **type)
183 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
184 *type = g_strdup(WIFI_SECURITY_NONE);
185 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
186 *type = g_strdup(WIFI_SECURITY_WEP);
187 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
188 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
189 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
190 *type = g_strdup(WIFI_SECURITY_EAP);
199 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
201 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
202 if (*config_id == NULL) {
211 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
213 GKeyFile *keyfile = NULL;
216 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
218 keyfile = netconfig_keyfile_load(path);
219 if (keyfile == NULL) {
220 ERR("keyfile[%s] is NULL", path);
228 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
230 gboolean ret = FALSE;
233 path = g_strdup_printf("%s/%s", pathname, filename);
234 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
236 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
245 static gboolean __remove_configuration(const gchar *pathname)
249 if (__remove_file(pathname, "settings") != TRUE) {
250 ERR("Cannot remove [%s/settings]", pathname);
253 if (__remove_file(pathname, "data") != TRUE) {
254 ERR("Cannot remove [%s/data]", pathname);
258 ret = rmdir(pathname);
260 ERR("Cannot remove [%s]", pathname);
267 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
271 gboolean hidden = FALSE;
272 gboolean ret = FALSE;
274 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
276 ERR("Fail to get_wifi_config_group_name");
280 keyfile = __get_configuration_keyfile(group_name);
281 if (keyfile == NULL) {
282 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
287 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
288 DBG("name [%s]", config->name);
290 ret = __get_security_type(config_id, &config->security_type);
292 ERR("Fail to _get_security_type");
293 g_key_file_free(keyfile);
297 DBG("security_type [%s]", config->security_type);
299 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
300 if (config->proxy_address)
301 DBG("proxy_address [%s]", config->proxy_address);
303 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
305 config->is_hidden = g_strdup("TRUE");
307 config->is_hidden = g_strdup("FALSE");
308 DBG("is_hidden [%s]", config->is_hidden);
310 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
311 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
312 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
313 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
314 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
315 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
316 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
317 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
318 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
320 if (config->eap_config->anonymous_identity)
321 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
322 if (config->eap_config->ca_cert)
323 DBG("ca_cert [%s]", config->eap_config->ca_cert);
324 if (config->eap_config->client_cert)
325 DBG("client_cert [%s]", config->eap_config->client_cert);
326 if (config->eap_config->private_key)
327 DBG("private_key [%s]", config->eap_config->private_key);
328 if (config->eap_config->identity)
329 DBG("identity [%s]", config->eap_config->identity);
330 if (config->eap_config->eap_type)
331 DBG("eap_type [%s]", config->eap_config->eap_type);
332 if (config->eap_config->eap_auth_type)
333 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
334 if (config->eap_config->subject_match)
335 DBG("subject_match [%s]", config->eap_config->subject_match);
338 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
339 if (config->last_error)
340 DBG("last_error [%s]", config->last_error);
342 g_key_file_free(keyfile);
348 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
353 gboolean ret = FALSE;
355 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
357 ERR("Fail to get_wifi_config_group_name");
361 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
362 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
363 if (__remove_configuration(dir) != TRUE) {
364 ERR("[%s] is existed, but cannot remove", dir);
371 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
372 ERR("Cannot mkdir %s", dir);
378 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
379 netconfig_keyfile_save(keyfile, path);
387 static gboolean _remove_configuration(const gchar *config_id)
389 gboolean ret = FALSE;
393 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
395 ERR("Fail to get_wifi_config_group_name");
399 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
400 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
401 if (__remove_configuration(dir) != TRUE) {
402 ERR("[%s] is existed, but cannot remove", dir);
405 INFO("Success to remove [%s]", dir);
408 ERR("[%s] is not existed", dir);
419 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
425 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
427 ERR("Fail to get_wifi_config_group_name");
430 DBG("group_name %s", group_name);
432 keyfile = __get_configuration_keyfile(group_name);
433 if (keyfile == NULL) {
434 ERR("Fail to __get_configuration_keyfile");
438 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
439 g_key_file_set_string(keyfile, group_name, key, value);
440 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
441 g_key_file_set_string(keyfile, group_name, key, value);
442 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
443 gboolean hidden = FALSE;
444 if (g_strcmp0(value, "TRUE") == 0)
446 g_key_file_set_boolean(keyfile, group_name, key, hidden);
447 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
448 g_key_file_set_string(keyfile, group_name, key, value);
449 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
450 g_key_file_set_string(keyfile, group_name, key, value);
451 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
452 g_key_file_set_string(keyfile, group_name, key, value);
453 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
454 g_key_file_set_string(keyfile, group_name, key, value);
455 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
456 g_key_file_set_string(keyfile, group_name, key, value);
457 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
458 g_key_file_set_string(keyfile, group_name, key, value);
459 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
460 g_key_file_set_string(keyfile, group_name, key, value);
461 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
462 g_key_file_set_string(keyfile, group_name, key, value);
464 ERR("key[%s] is not supported", key);
468 _save_configuration(config_id, keyfile);
470 g_key_file_free(keyfile);
476 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
481 gboolean hidden = FALSE;
482 gboolean ret = FALSE;
484 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
486 ERR("Fail to get_wifi_config_group_name");
489 DBG("group_name %s", group_name);
491 keyfile = __get_configuration_keyfile(group_name);
492 if (keyfile == NULL) {
493 ERR("Fail to __get_configuration_keyfile");
497 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
498 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
499 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
500 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
501 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
502 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
503 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
504 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
506 val = g_strdup("TRUE");
508 val = g_strdup("FALSE");
509 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
510 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
511 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
512 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
513 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
514 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
515 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
516 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
517 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
518 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
519 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
520 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
521 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
522 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
523 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
524 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
525 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
526 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
528 ERR("Invalid key[%s]", key);
529 val = g_strdup("NOTSUPPORTED");
532 *value = g_strdup(val);
535 g_key_file_free(keyfile);
541 static GSList *_get_list(void)
544 struct dirent *dp = NULL;
547 dir = opendir(CONNMAN_STORAGE);
549 ERR("Cannot open dir %s", CONNMAN_STORAGE);
553 while ((dp = readdir(dir)) != NULL) {
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);