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);
219 ERR("keyfile[%s] is NULL", path);
226 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
228 gboolean ret = FALSE;
231 path = g_strdup_printf("%s/%s", pathname, filename);
232 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
234 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
243 static gboolean __remove_configuration(const gchar *pathname)
247 if (__remove_file(pathname, "settings") != TRUE) {
248 ERR("Cannot remove [%s/settings]", pathname);
251 if (__remove_file(pathname, "data") != TRUE) {
252 ERR("Cannot remove [%s/data]", pathname);
256 ret = rmdir(pathname);
258 ERR("Cannot remove [%s]", pathname);
265 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
269 gboolean hidden = FALSE;
270 gboolean ret = FALSE;
272 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
274 ERR("Fail to get_wifi_config_group_name");
278 keyfile = __get_configuration_keyfile(group_name);
279 if (keyfile == NULL) {
280 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
285 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
286 DBG("name [%s]", config->name);
288 ret = __get_security_type(config_id, &config->security_type);
290 ERR("Fail to _get_security_type");
291 g_key_file_free(keyfile);
295 DBG("security_type [%s]", config->security_type);
297 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
298 if (config->proxy_address)
299 DBG("proxy_address [%s]", config->proxy_address);
301 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
303 config->is_hidden = g_strdup("TRUE");
305 config->is_hidden = g_strdup("FALSE");
306 DBG("is_hidden [%s]", config->is_hidden);
308 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
309 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
310 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
311 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
312 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
313 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
314 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
315 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
316 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
318 if (config->eap_config->anonymous_identity)
319 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
320 if (config->eap_config->ca_cert)
321 DBG("ca_cert [%s]", config->eap_config->ca_cert);
322 if (config->eap_config->client_cert)
323 DBG("client_cert [%s]", config->eap_config->client_cert);
324 if (config->eap_config->private_key)
325 DBG("private_key [%s]", config->eap_config->private_key);
326 if (config->eap_config->identity)
327 DBG("identity [%s]", config->eap_config->identity);
328 if (config->eap_config->eap_type)
329 DBG("eap_type [%s]", config->eap_config->eap_type);
330 if (config->eap_config->eap_auth_type)
331 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
332 if (config->eap_config->subject_match)
333 DBG("subject_match [%s]", config->eap_config->subject_match);
336 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
337 if (config->last_error)
338 DBG("last_error [%s]", config->last_error);
340 g_key_file_free(keyfile);
346 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
351 gboolean ret = FALSE;
353 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
355 ERR("Fail to get_wifi_config_group_name");
359 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
360 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
361 if (__remove_configuration(dir) != TRUE) {
362 ERR("[%s] is existed, but cannot remove", dir);
369 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
370 ERR("Cannot mkdir %s", dir);
376 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
377 netconfig_keyfile_save(keyfile, path);
385 static gboolean _remove_configuration(const gchar *config_id)
387 gboolean ret = FALSE;
391 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
393 ERR("Fail to get_wifi_config_group_name");
397 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
398 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
399 if (__remove_configuration(dir) != TRUE) {
400 ERR("[%s] is existed, but cannot remove", dir);
403 INFO("Success to remove [%s]", dir);
406 ERR("[%s] is not existed", dir);
417 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
423 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
425 ERR("Fail to get_wifi_config_group_name");
428 DBG("group_name %s", group_name);
430 keyfile = __get_configuration_keyfile(group_name);
431 if (keyfile == NULL) {
432 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");
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 ent_struct;
545 struct dirent *dp = NULL;
548 dir = opendir(CONNMAN_STORAGE);
550 ERR("Cannot open dir %s", CONNMAN_STORAGE);
554 while ((readdir_r(dir, &ent_struct, &dp) == 0) && dp) {
555 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
556 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
559 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
560 list = g_slist_append(list, g_strdup(config_id));
568 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
570 gboolean ret = FALSE;
573 if ((service_profile == NULL) || (config_id == NULL)) {
574 ERR("Invalid parameter");
578 ret = __get_config_id(service_profile, &val);
579 *config_id = g_strdup(val);
585 gboolean wifi_config_remove_configuration(const gchar *config_id)
587 gboolean ret = FALSE;
589 ret = _remove_configuration(config_id);
595 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
598 GSList *config_ids = NULL;
600 gchar **result = NULL;
602 g_return_val_if_fail(wifi != NULL, FALSE);
604 config_ids = _get_list();
605 if (config_ids == NULL) {
606 netconfig_error_no_profile(context);
607 ERR("Fail to get config list");
611 length = g_slist_length(config_ids);
612 result = g_new0(gchar *, length + 1);
613 for (i = 0; i < length; i++) {
614 gchar *config_id = g_slist_nth_data(config_ids, i);
615 result[i] = g_strdup(config_id);
618 config_ids = g_slist_nth(config_ids, 0);
619 g_slist_free_full(config_ids, g_free);
621 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
623 for (i = 0; i < length; i++)
633 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
634 const gchar *config_id)
636 gboolean ret = FALSE;
637 GVariantBuilder *b = NULL;
638 struct wifi_config *conf = NULL;
640 g_return_val_if_fail(wifi != NULL, FALSE);
642 conf = g_new0(struct wifi_config, 1);
644 ret = _load_configuration(config_id, conf);
647 ERR("Fail to _load_configuration");
648 netconfig_error_no_profile(context);
652 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
653 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
654 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
655 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
657 if (conf->proxy_address != NULL)
658 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
660 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
662 if (conf->last_error != NULL)
663 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
665 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
667 g_free(conf->proxy_address);
668 g_free(conf->last_error);
670 g_free(conf->security_type);
671 g_free(conf->is_hidden);
674 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
675 g_variant_builder_unref(b);
679 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
680 const gchar *config_id, GVariant *configuration)
682 gboolean ret = FALSE;
683 struct wifi_config *conf = NULL;
684 GKeyFile *keyfile = NULL;
688 gchar *group_name = NULL;
690 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
691 ERR("Invalid parameter");
692 netconfig_error_invalid_parameter(context);
696 conf = g_new0(struct wifi_config, 1);
698 g_variant_get(configuration, "a{sv}", &iter);
699 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
700 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
701 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
702 conf->name = g_strdup(g_variant_get_string(value, NULL));
703 DBG("name [%s]", conf->name);
707 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
708 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
709 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
710 DBG("ssid [%s]", conf->ssid);
714 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
715 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
716 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
717 DBG("passphrase []");
719 conf->passphrase = NULL;
721 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
722 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
723 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
724 DBG("is_hidden [%s]", conf->is_hidden);
726 conf->is_hidden = NULL;
728 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
729 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
730 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
731 DBG("proxy_address [%s]", conf->proxy_address);
733 conf->proxy_address = NULL;
737 conf->favorite = TRUE;
738 conf->autoconnect = TRUE;
740 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
744 g_free(conf->passphrase);
745 g_free(conf->is_hidden);
746 g_free(conf->proxy_address);
748 ERR("Fail to get_wifi_config_group_name");
752 keyfile = g_key_file_new();
753 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
754 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
756 if (conf->passphrase != NULL)
757 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
759 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
760 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
763 if (conf->proxy_address != NULL) {
764 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
765 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
768 if (conf->is_hidden != NULL) {
769 gboolean hidden = FALSE;
770 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
772 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
775 ret = _save_configuration(config_id, keyfile);
777 INFO("Success to save configuration [%s]", config_id);
778 wifi_complete_save_configuration(wifi, context);
780 INFO("Fail to save configuration [%s]", config_id);
781 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
784 g_key_file_free(keyfile);
787 g_free(conf->passphrase);
788 g_free(conf->is_hidden);
789 g_free(conf->proxy_address);
792 g_variant_iter_free(iter);
797 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
798 const gchar *config_id)
800 gboolean ret = FALSE;
801 GVariantBuilder *b = NULL;
802 struct wifi_config *conf = NULL;
804 g_return_val_if_fail(wifi != NULL, FALSE);
806 conf = g_new0(struct wifi_config, 1);
807 conf->eap_config = g_new0(struct wifi_eap_config, 1);
809 ret = _load_configuration(config_id, conf);
811 g_free(conf->eap_config);
813 ERR("Fail to _load_configuration");
814 netconfig_error_no_profile(context);
818 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
819 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
820 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
821 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
822 if (conf->proxy_address != NULL)
823 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
825 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
827 if (conf->last_error != NULL)
828 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
830 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
832 if (conf->eap_config != NULL) {
833 if (conf->eap_config->anonymous_identity != NULL)
834 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
836 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
838 if (conf->eap_config->ca_cert != NULL)
839 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
841 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
843 if (conf->eap_config->client_cert != NULL)
844 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
846 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
848 if (conf->eap_config->private_key != NULL)
849 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
851 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
853 if (conf->eap_config->identity != NULL)
854 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
856 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
858 if (conf->eap_config->eap_type != NULL)
859 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
861 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
863 if (conf->eap_config->eap_auth_type != NULL)
864 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
866 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
868 if (conf->eap_config->subject_match != NULL)
869 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
871 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
874 __free_wifi_configuration(conf);
876 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
877 g_variant_builder_unref(b);
881 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
882 const gchar *config_id, GVariant *configuration)
884 gboolean ret = FALSE;
885 struct wifi_config *conf = NULL;
886 GKeyFile *keyfile = NULL;
890 gchar *group_name = NULL;
892 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
893 ERR("Invalid parameter");
894 netconfig_error_invalid_parameter(context);
898 conf = g_new0(struct wifi_config, 1);
899 conf->eap_config = g_new0(struct wifi_eap_config, 1);
901 g_variant_get(configuration, "a{sv}", &iter);
902 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
903 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
904 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
905 conf->name = g_strdup(g_variant_get_string(value, NULL));
906 DBG("name [%s]", conf->name);
910 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
911 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
912 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
913 DBG("ssid [%s]", conf->ssid);
917 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
918 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
919 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
920 DBG("passphrase [%s]", conf->passphrase);
922 conf->passphrase = NULL;
924 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
925 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
926 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
927 DBG("is_hidden [%s]", conf->is_hidden);
929 conf->is_hidden = NULL;
931 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
932 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
933 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
934 DBG("proxy_address [%s]", conf->proxy_address);
936 conf->proxy_address = NULL;
938 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
939 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
940 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
941 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
943 conf->eap_config->anonymous_identity = NULL;
945 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
946 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
947 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
948 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
950 conf->eap_config->ca_cert = NULL;
952 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
953 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
954 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
955 DBG("client_cert [%s]", conf->eap_config->client_cert);
957 conf->eap_config->client_cert = NULL;
959 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
960 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
961 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
962 DBG("private_key [%s]", conf->eap_config->private_key);
964 conf->eap_config->private_key = NULL;
966 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
967 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
968 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
969 DBG("identity [%s]", conf->eap_config->identity);
971 conf->eap_config->identity = NULL;
973 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
974 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
975 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
976 DBG("eap_type [%s]", conf->eap_config->eap_type);
978 conf->eap_config->eap_type = NULL;
980 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
981 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
982 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
983 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
985 conf->eap_config->eap_auth_type = NULL;
987 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
988 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
989 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
990 DBG("subject_match [%s]", conf->eap_config->subject_match);
992 conf->eap_config->subject_match = NULL;
996 conf->favorite = TRUE;
997 conf->autoconnect = TRUE;
999 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1001 __free_wifi_configuration(conf);
1002 ERR("Fail to get_wifi_config_group_name");
1006 keyfile = g_key_file_new();
1007 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1008 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1010 if (conf->passphrase != NULL)
1011 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1013 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1014 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1016 /* Optional field */
1017 if (conf->proxy_address != NULL) {
1018 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1019 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1022 if (conf->is_hidden != NULL) {
1023 gboolean hidden = FALSE;
1024 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1026 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1029 if (conf->eap_config->anonymous_identity != NULL)
1030 g_key_file_set_string(keyfile, group_name,
1031 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1033 if (conf->eap_config->ca_cert != NULL)
1034 g_key_file_set_string(keyfile, group_name,
1035 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1037 if (conf->eap_config->client_cert != NULL)
1038 g_key_file_set_string(keyfile, group_name,
1039 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1041 if (conf->eap_config->private_key != NULL)
1042 g_key_file_set_string(keyfile, group_name,
1043 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1045 if (conf->eap_config->identity != NULL)
1046 g_key_file_set_string(keyfile, group_name,
1047 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1049 if (conf->eap_config->eap_type != NULL)
1050 g_key_file_set_string(keyfile, group_name,
1051 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1053 if (conf->eap_config->eap_auth_type != NULL)
1054 g_key_file_set_string(keyfile, group_name,
1055 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1057 if (conf->eap_config->subject_match != NULL)
1058 g_key_file_set_string(keyfile, group_name,
1059 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1061 ret = _save_configuration(config_id, keyfile);
1063 INFO("Success to save eap configuration [%s]", config_id);
1064 wifi_complete_save_eap_configuration(wifi, context);
1066 INFO("Fail to save eap configuration [%s]", config_id);
1067 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1070 g_key_file_free(keyfile);
1071 __free_wifi_configuration(conf);
1073 g_variant_iter_free(iter);
1078 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1080 gboolean ret = FALSE;
1082 if ((wifi == NULL) || (config_id == NULL)) {
1083 ERR("Invalid parameter");
1084 netconfig_error_invalid_parameter(context);
1088 ret = _remove_configuration(config_id);
1090 /* no configuration or error */
1091 ERR("No [%s] configuration", config_id);
1092 netconfig_error_no_profile(context);
1096 wifi_complete_remove_configuration(wifi, context);
1100 /* config field key / value */
1102 * [wifi_macaddress_config_id]
1103 * Name=name (mandatory)
1104 * SSID=SSID (mandatory)
1105 * Frequency=2462 (X)
1107 * AutoConnect=true (Default true)
1108 * Modified=2015-03-20 (X)
1109 * IPv4.method=manual (O)
1110 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1111 * IPv6.method=auto (X)
1112 * IPv6.privacy=disabled (X)
1113 * IPv4.netmask_prefixlen=24 (X)
1114 * IPv4.local_address=192.0.0.1 (O)
1115 * IPv4.gateway=192.0.0.1 (O ? X ?)
1116 * Nameservers=192.168.43.22; (O)
1117 * Proxy.Method=manual (O)
1118 * Proxy.Servers=trst.com:8888; (O)
1120 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1121 const gchar *config_id, const gchar *key, const gchar *value)
1123 gboolean ret = FALSE;
1124 gchar *keyfile_key = NULL;
1126 g_return_val_if_fail(wifi != NULL, FALSE);
1127 g_return_val_if_fail(config_id != NULL, FALSE);
1128 g_return_val_if_fail(key != NULL, FALSE);
1130 DBG("Key[%s] Value[%d]", key, value);
1132 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1133 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1135 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1136 netconfig_error_invalid_parameter(context);
1139 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1140 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1141 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1142 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1143 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1144 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1145 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1146 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1147 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1148 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1149 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1150 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1151 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1152 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1153 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1154 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1155 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1156 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1157 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1159 ERR("Not supported key[%s]", key);
1160 netconfig_error_invalid_parameter(context);
1164 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1166 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1170 if (keyfile_key != NULL)
1171 g_free(keyfile_key);
1173 wifi_complete_set_config_field(wifi, context);
1177 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1179 gboolean ret = FALSE;
1180 gchar *passphrase = NULL;
1182 if ((wifi == NULL) || (config_id == NULL)) {
1183 ERR("Invalid parameter");
1184 netconfig_error_invalid_parameter(context);
1188 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1190 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1191 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1195 wifi_complete_get_config_passphrase(wifi, context, passphrase);