2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <sys/types.h>
34 #include "wifi-config.h"
36 #define CONNMAN_STORAGE "/var/lib/connman"
38 #define WIFI_SECURITY_NONE "none"
39 #define WIFI_SECURITY_WEP "wep"
40 #define WIFI_SECURITY_WPA_PSK "psk"
41 #define WIFI_SECURITY_EAP "ieee8021x"
43 #define WIFI_CONFIG_PREFIX "wifi_"
44 #define MAC_ADDRESS_LENGTH 12
45 #define WIFI_PREFIX_LENGTH MAC_ADDRESS_LENGTH + 6 /* wifi_485a3f2f506a_ */
46 #define PROFILE_PREFIX_LENGTH WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
48 #define WIFI_MAC_ADD_LENGTH 17
49 #define WIFI_MAC_ADD_PATH "/sys/class/net/wlan0/address"
51 struct wifi_eap_config {
52 gchar *anonymous_identity;
71 struct wifi_eap_config *eap_config;
75 static void __free_wifi_configuration(struct wifi_config *conf)
82 g_free(conf->passphrase);
83 g_free(conf->security_type);
84 g_free(conf->is_hidden);
85 g_free(conf->proxy_address);
86 g_free(conf->last_error);
87 if (conf->eap_config) {
88 g_free(conf->eap_config->anonymous_identity);
89 g_free(conf->eap_config->ca_cert);
90 g_free(conf->eap_config->client_cert);
91 g_free(conf->eap_config->private_key);
92 g_free(conf->eap_config->identity);
93 g_free(conf->eap_config->eap_type);
94 g_free(conf->eap_config->eap_auth_type);
95 g_free(conf->eap_config->subject_match);
96 g_free(conf->eap_config);
101 static gboolean __get_mac_address(gchar **mac_address)
103 gchar *tmp_mac = NULL;
105 gchar mac[13] = { 0, };
110 char buf[WIFI_MAC_ADD_LENGTH + 1];
111 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
112 fp = fopen(WIFI_MAC_ADD_PATH, "r");
115 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
120 if (fgets(buf, sizeof(buf), fp) == NULL) {
121 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
126 tmp_mac = (gchar *)malloc(WIFI_MAC_ADD_LENGTH + 1);
127 if (tmp_mac == NULL) {
128 ERR("malloc() failed");
133 memset(tmp_mac, 0, WIFI_MAC_ADD_LENGTH + 1);
134 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
137 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
138 if (tmp_mac == NULL) {
139 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
144 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
146 while (tmp && tmp[i]) {
152 *mac_address = g_strdup(mac);
157 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
159 gchar *mac_address = NULL;
160 gchar *g_name = NULL;
161 gboolean ret = FALSE;
163 ret = __get_mac_address(&mac_address);
164 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
165 ERR("Cannot get WIFI MAC address");
169 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
170 if (g_name == NULL) {
175 *group_name = g_strdup(g_name);
183 static gboolean __get_security_type(const gchar *config_id, gchar **type)
185 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
186 *type = g_strdup(WIFI_SECURITY_NONE);
187 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
188 *type = g_strdup(WIFI_SECURITY_WEP);
189 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
190 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
191 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
192 *type = g_strdup(WIFI_SECURITY_EAP);
201 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
203 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
204 if (*config_id == NULL) {
213 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
215 GKeyFile *keyfile = NULL;
218 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
220 keyfile = netconfig_keyfile_load(path);
222 ERR("keyfile[%s] is NULL", path);
229 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
231 gboolean ret = FALSE;
234 path = g_strdup_printf("%s/%s", pathname, filename);
235 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
237 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
246 static gboolean __remove_configuration(const gchar *pathname)
250 if (__remove_file(pathname, "settings") != TRUE) {
251 ERR("Cannot remove [%s/settings]", pathname);
254 if (__remove_file(pathname, "data") != TRUE) {
255 ERR("Cannot remove [%s/data]", pathname);
259 ret = rmdir(pathname);
261 ERR("Cannot remove [%s]", pathname);
268 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
272 gboolean hidden = FALSE;
273 gboolean ret = FALSE;
275 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
277 ERR("Fail to get_wifi_config_group_name");
281 keyfile = __get_configuration_keyfile(group_name);
282 if (keyfile == NULL) {
283 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
288 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
289 DBG("name [%s]", config->name);
291 __get_security_type(config_id, &config->security_type);
292 if (config->security_type == NULL) {
293 ERR("Fail to _get_security_type");
294 g_key_file_free(keyfile);
298 DBG("security_type [%s]", config->security_type);
300 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
301 if (config->proxy_address)
302 DBG("proxy_address [%s]", config->proxy_address);
304 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
306 config->is_hidden = g_strdup("TRUE");
308 config->is_hidden = g_strdup("FALSE");
309 DBG("is_hidden [%s]", config->is_hidden);
311 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
312 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
313 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
314 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
315 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
316 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
317 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
318 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
319 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
321 if (config->eap_config->anonymous_identity)
322 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
323 if (config->eap_config->ca_cert)
324 DBG("ca_cert [%s]", config->eap_config->ca_cert);
325 if (config->eap_config->client_cert)
326 DBG("client_cert [%s]", config->eap_config->client_cert);
327 if (config->eap_config->private_key)
328 DBG("private_key [%s]", config->eap_config->private_key);
329 if (config->eap_config->identity)
330 DBG("identity [%s]", config->eap_config->identity);
331 if (config->eap_config->eap_type)
332 DBG("eap_type [%s]", config->eap_config->eap_type);
333 if (config->eap_config->eap_auth_type)
334 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
335 if (config->eap_config->subject_match)
336 DBG("subject_match [%s]", config->eap_config->subject_match);
339 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
340 if (config->last_error)
341 DBG("last_error [%s]", config->last_error);
343 g_key_file_free(keyfile);
349 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
354 gboolean ret = FALSE;
356 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
358 ERR("Fail to get_wifi_config_group_name");
362 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
363 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
364 if (__remove_configuration(dir) != TRUE) {
365 ERR("[%s] is existed, but cannot remove", dir);
372 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
373 ERR("Cannot mkdir %s", dir);
379 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
380 netconfig_keyfile_save(keyfile, path);
388 static gboolean _remove_configuration(const gchar *config_id)
390 gboolean ret = FALSE;
394 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
396 ERR("Fail to get_wifi_config_group_name");
400 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
401 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
402 if (__remove_configuration(dir) != TRUE) {
403 ERR("[%s] is existed, but cannot remove", dir);
406 INFO("Success to remove [%s]", dir);
409 ERR("[%s] is not existed", dir);
420 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
426 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
428 ERR("Fail to get_wifi_config_group_name");
431 DBG("group_name %s", group_name);
433 keyfile = __get_configuration_keyfile(group_name);
434 if (keyfile == NULL) {
435 ERR("Fail to __get_configuration_keyfile");
440 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
441 g_key_file_set_string(keyfile, group_name, key, value);
442 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
443 g_key_file_set_string(keyfile, group_name, key, value);
444 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
445 gboolean hidden = FALSE;
446 if (g_strcmp0(value, "TRUE") == 0)
448 g_key_file_set_boolean(keyfile, group_name, key, hidden);
449 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
450 g_key_file_set_string(keyfile, group_name, key, value);
451 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
452 g_key_file_set_string(keyfile, group_name, key, value);
453 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
454 g_key_file_set_string(keyfile, group_name, key, value);
455 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
456 g_key_file_set_string(keyfile, group_name, key, value);
457 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
458 g_key_file_set_string(keyfile, group_name, key, value);
459 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
460 g_key_file_set_string(keyfile, group_name, key, value);
461 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
462 g_key_file_set_string(keyfile, group_name, key, value);
463 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
464 g_key_file_set_string(keyfile, group_name, key, value);
466 ERR("key[%s] is not supported", key);
470 _save_configuration(config_id, keyfile);
472 g_key_file_free(keyfile);
478 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
483 gboolean hidden = FALSE;
484 gboolean ret = FALSE;
486 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
488 ERR("Fail to get_wifi_config_group_name");
491 DBG("group_name %s", group_name);
493 keyfile = __get_configuration_keyfile(group_name);
494 if (keyfile == NULL) {
495 ERR("Fail to __get_configuration_keyfile");
500 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
501 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
502 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
503 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
504 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
505 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
506 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
507 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
509 val = g_strdup("TRUE");
511 val = g_strdup("FALSE");
512 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
513 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
514 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
515 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
516 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
517 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
518 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
519 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
520 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
521 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
522 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
523 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
524 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
525 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
526 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
527 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
528 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
529 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
531 ERR("Invalid key[%s]", key);
532 val = g_strdup("NOTSUPPORTED");
535 *value = g_strdup(val);
538 g_key_file_free(keyfile);
544 static GSList *_get_list(void)
547 struct dirent *dp = NULL;
550 dir = opendir(CONNMAN_STORAGE);
552 ERR("Cannot open dir %s", CONNMAN_STORAGE);
556 while ((dp = readdir(dir)) != NULL) {
557 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
558 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
561 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
562 list = g_slist_append(list, g_strdup(config_id));
570 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
572 gboolean ret = FALSE;
575 if ((service_profile == NULL) || (config_id == NULL)) {
576 ERR("Invalid parameter");
580 ret = __get_config_id(service_profile, &val);
581 *config_id = g_strdup(val);
587 gboolean wifi_config_remove_configuration(const gchar *config_id)
589 gboolean ret = FALSE;
591 ret = _remove_configuration(config_id);
597 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
600 GSList *config_ids = NULL;
602 gchar **result = NULL;
604 g_return_val_if_fail(wifi != NULL, FALSE);
606 config_ids = _get_list();
607 if (config_ids == NULL) {
608 netconfig_error_no_profile(context);
609 ERR("Fail to get config list");
613 length = g_slist_length(config_ids);
614 result = g_new0(gchar *, length + 1);
615 for (i = 0; i < length; i++) {
616 gchar *config_id = g_slist_nth_data(config_ids, i);
617 result[i] = g_strdup(config_id);
620 config_ids = g_slist_nth(config_ids, 0);
621 g_slist_free_full(config_ids, g_free);
623 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
625 for (i = 0; i < length; i++)
635 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
636 const gchar *config_id)
638 gboolean ret = FALSE;
639 GVariantBuilder *b = NULL;
640 struct wifi_config *conf = NULL;
642 g_return_val_if_fail(wifi != NULL, FALSE);
644 conf = g_new0(struct wifi_config, 1);
646 ret = _load_configuration(config_id, conf);
649 ERR("Fail to _load_configuration");
650 netconfig_error_no_profile(context);
654 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
655 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
656 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
657 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
659 if (conf->proxy_address != NULL)
660 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
662 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
664 if (conf->last_error != NULL)
665 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
667 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
669 g_free(conf->proxy_address);
670 g_free(conf->last_error);
672 g_free(conf->security_type);
673 g_free(conf->is_hidden);
676 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
677 g_variant_builder_unref(b);
681 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
682 const gchar *config_id, GVariant *configuration)
684 gboolean ret = FALSE;
685 struct wifi_config *conf = NULL;
686 GKeyFile *keyfile = NULL;
690 gchar *group_name = NULL;
692 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
693 ERR("Invalid parameter");
694 netconfig_error_invalid_parameter(context);
698 conf = g_new0(struct wifi_config, 1);
700 g_variant_get(configuration, "a{sv}", &iter);
701 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
702 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
703 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
704 conf->name = g_strdup(g_variant_get_string(value, NULL));
705 DBG("name [%s]", conf->name);
709 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
710 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
711 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
712 DBG("ssid [%s]", conf->ssid);
716 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
717 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
718 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
719 DBG("passphrase []");
721 conf->passphrase = NULL;
723 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
724 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
725 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
726 DBG("is_hidden [%s]", conf->is_hidden);
728 conf->is_hidden = NULL;
730 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
731 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
732 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
733 DBG("proxy_address [%s]", conf->proxy_address);
735 conf->proxy_address = NULL;
739 conf->favorite = TRUE;
740 conf->autoconnect = TRUE;
742 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
746 g_free(conf->passphrase);
747 g_free(conf->is_hidden);
748 g_free(conf->proxy_address);
750 ERR("Fail to get_wifi_config_group_name");
754 keyfile = g_key_file_new();
755 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
756 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
758 if (conf->passphrase != NULL)
759 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
761 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
762 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
765 if (conf->proxy_address != NULL) {
766 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
767 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
770 if (conf->is_hidden != NULL) {
771 gboolean hidden = FALSE;
772 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
774 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
777 ret = _save_configuration(config_id, keyfile);
779 INFO("Success to save configuration [%s]", config_id);
780 wifi_complete_save_configuration(wifi, context);
782 INFO("Fail to save configuration [%s]", config_id);
783 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
786 g_key_file_free(keyfile);
790 g_free(conf->passphrase);
791 g_free(conf->is_hidden);
792 g_free(conf->proxy_address);
795 g_variant_iter_free(iter);
800 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
801 const gchar *config_id)
803 gboolean ret = FALSE;
804 GVariantBuilder *b = NULL;
805 struct wifi_config *conf = NULL;
807 g_return_val_if_fail(wifi != NULL, FALSE);
809 conf = g_new0(struct wifi_config, 1);
810 conf->eap_config = g_new0(struct wifi_eap_config, 1);
812 ret = _load_configuration(config_id, conf);
814 g_free(conf->eap_config);
816 ERR("Fail to _load_configuration");
817 netconfig_error_no_profile(context);
821 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
822 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
823 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
824 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
825 if (conf->proxy_address != NULL)
826 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
828 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
830 if (conf->last_error != NULL)
831 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
833 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
835 if (conf->eap_config != NULL) {
836 if (conf->eap_config->anonymous_identity != NULL)
837 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
839 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
841 if (conf->eap_config->ca_cert != NULL)
842 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
844 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
846 if (conf->eap_config->client_cert != NULL)
847 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
849 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
851 if (conf->eap_config->private_key != NULL)
852 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
854 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
856 if (conf->eap_config->identity != NULL)
857 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
859 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
861 if (conf->eap_config->eap_type != NULL)
862 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
864 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
866 if (conf->eap_config->eap_auth_type != NULL)
867 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
869 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
871 if (conf->eap_config->subject_match != NULL)
872 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
874 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
877 __free_wifi_configuration(conf);
879 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
880 g_variant_builder_unref(b);
884 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
885 const gchar *config_id, GVariant *configuration)
887 gboolean ret = FALSE;
888 struct wifi_config *conf = NULL;
889 GKeyFile *keyfile = NULL;
893 gchar *group_name = NULL;
895 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
896 ERR("Invalid parameter");
897 netconfig_error_invalid_parameter(context);
901 conf = g_new0(struct wifi_config, 1);
902 conf->eap_config = g_new0(struct wifi_eap_config, 1);
904 g_variant_get(configuration, "a{sv}", &iter);
905 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
906 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
907 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
908 conf->name = g_strdup(g_variant_get_string(value, NULL));
909 DBG("name [%s]", conf->name);
913 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
914 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
915 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
916 DBG("ssid [%s]", conf->ssid);
920 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
921 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
922 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
923 DBG("passphrase [%s]", conf->passphrase);
925 conf->passphrase = NULL;
927 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
928 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
929 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
930 DBG("is_hidden [%s]", conf->is_hidden);
932 conf->is_hidden = NULL;
934 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
935 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
936 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
937 DBG("proxy_address [%s]", conf->proxy_address);
939 conf->proxy_address = NULL;
941 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
942 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
943 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
944 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
946 conf->eap_config->anonymous_identity = NULL;
948 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
949 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
950 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
951 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
953 conf->eap_config->ca_cert = NULL;
955 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
956 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
957 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
958 DBG("client_cert [%s]", conf->eap_config->client_cert);
960 conf->eap_config->client_cert = NULL;
962 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
963 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
964 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
965 DBG("private_key [%s]", conf->eap_config->private_key);
967 conf->eap_config->private_key = NULL;
969 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
970 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
971 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
972 DBG("identity [%s]", conf->eap_config->identity);
974 conf->eap_config->identity = NULL;
976 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
977 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
978 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
979 DBG("eap_type [%s]", conf->eap_config->eap_type);
981 conf->eap_config->eap_type = NULL;
983 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
984 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
985 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
986 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
988 conf->eap_config->eap_auth_type = NULL;
990 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
991 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
992 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
993 DBG("subject_match [%s]", conf->eap_config->subject_match);
995 conf->eap_config->subject_match = NULL;
999 conf->favorite = TRUE;
1000 conf->autoconnect = TRUE;
1002 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1004 __free_wifi_configuration(conf);
1005 ERR("Fail to get_wifi_config_group_name");
1009 keyfile = g_key_file_new();
1010 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1011 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1013 if (conf->passphrase != NULL)
1014 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1016 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1017 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1019 /* Optional field */
1020 if (conf->proxy_address != NULL) {
1021 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1022 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1025 if (conf->is_hidden != NULL) {
1026 gboolean hidden = FALSE;
1027 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1029 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1032 if (conf->eap_config->anonymous_identity != NULL)
1033 g_key_file_set_string(keyfile, group_name,
1034 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1036 if (conf->eap_config->ca_cert != NULL)
1037 g_key_file_set_string(keyfile, group_name,
1038 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1040 if (conf->eap_config->client_cert != NULL)
1041 g_key_file_set_string(keyfile, group_name,
1042 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1044 if (conf->eap_config->private_key != NULL)
1045 g_key_file_set_string(keyfile, group_name,
1046 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1048 if (conf->eap_config->identity != NULL)
1049 g_key_file_set_string(keyfile, group_name,
1050 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1052 if (conf->eap_config->eap_type != NULL)
1053 g_key_file_set_string(keyfile, group_name,
1054 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1056 if (conf->eap_config->eap_auth_type != NULL)
1057 g_key_file_set_string(keyfile, group_name,
1058 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1060 if (conf->eap_config->subject_match != NULL)
1061 g_key_file_set_string(keyfile, group_name,
1062 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1064 ret = _save_configuration(config_id, keyfile);
1066 INFO("Success to save eap configuration [%s]", config_id);
1067 wifi_complete_save_eap_configuration(wifi, context);
1069 INFO("Fail to save eap configuration [%s]", config_id);
1070 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1073 g_key_file_free(keyfile);
1075 __free_wifi_configuration(conf);
1077 g_variant_iter_free(iter);
1082 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1084 gboolean ret = FALSE;
1086 if ((wifi == NULL) || (config_id == NULL)) {
1087 ERR("Invalid parameter");
1088 netconfig_error_invalid_parameter(context);
1092 ret = _remove_configuration(config_id);
1094 /* no configuration or error */
1095 ERR("No [%s] configuration", config_id);
1096 netconfig_error_no_profile(context);
1100 wifi_complete_remove_configuration(wifi, context);
1104 /* config field key / value */
1106 * [wifi_macaddress_config_id]
1107 * Name=name (mandatory)
1108 * SSID=SSID (mandatory)
1109 * Frequency=2462 (X)
1111 * AutoConnect=true (Default true)
1112 * Modified=2015-03-20 (X)
1113 * IPv4.method=manual (O)
1114 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1115 * IPv6.method=auto (X)
1116 * IPv6.privacy=disabled (X)
1117 * IPv4.netmask_prefixlen=24 (X)
1118 * IPv4.local_address=192.0.0.1 (O)
1119 * IPv4.gateway=192.0.0.1 (O ? X ?)
1120 * Nameservers=192.168.43.22; (O)
1121 * Proxy.Method=manual (O)
1122 * Proxy.Servers=trst.com:8888; (O)
1124 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1125 const gchar *config_id, const gchar *key, const gchar *value)
1127 gboolean ret = FALSE;
1128 gchar *keyfile_key = NULL;
1130 g_return_val_if_fail(wifi != NULL, FALSE);
1131 g_return_val_if_fail(config_id != NULL, FALSE);
1132 g_return_val_if_fail(key != NULL, FALSE);
1134 DBG("Key[%s] Value[%d]", key, value);
1136 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1137 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1139 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1140 netconfig_error_invalid_parameter(context);
1143 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1144 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1145 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1146 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1147 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1148 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1149 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1150 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1151 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1152 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1153 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1154 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1155 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1156 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1157 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1158 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1159 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1160 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1161 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1163 ERR("Not supported key[%s]", key);
1164 netconfig_error_invalid_parameter(context);
1168 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1170 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1174 if (keyfile_key != NULL)
1175 g_free(keyfile_key);
1177 wifi_complete_set_config_field(wifi, context);
1181 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1183 gboolean ret = FALSE;
1184 gchar *passphrase = NULL;
1186 if ((wifi == NULL) || (config_id == NULL)) {
1187 ERR("Invalid parameter");
1188 netconfig_error_invalid_parameter(context);
1192 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1194 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1195 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1199 wifi_complete_get_config_passphrase(wifi, context, passphrase);