2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <sys/types.h>
33 #include "wifi-config.h"
35 #define CONNMAN_STORAGE "/var/lib/connman"
37 #define WIFI_SECURITY_NONE "none"
38 #define WIFI_SECURITY_WEP "wep"
39 #define WIFI_SECURITY_WPA_PSK "psk"
40 #define WIFI_SECURITY_EAP "ieee8021x"
42 #define WIFI_CONFIG_PREFIX "wifi_"
43 #define MAC_ADDRESS_LENGTH 12
44 #define WIFI_PREFIX_LENGTH MAC_ADDRESS_LENGTH + 6 /* wifi_485a3f2f506a_ */
45 #define PROFILE_PREFIX_LENGTH WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
47 #define WIFI_MAC_ADD_LENGTH 17
48 #define WIFI_MAC_ADD_PATH "/sys/class/net/wlan0/address"
50 struct wifi_eap_config {
51 gchar *anonymous_identity;
70 struct wifi_eap_config *eap_config;
74 static void __free_wifi_configuration(struct wifi_config *conf)
81 g_free(conf->passphrase);
82 g_free(conf->security_type);
83 g_free(conf->is_hidden);
84 g_free(conf->proxy_address);
85 g_free(conf->last_error);
86 if (conf->eap_config) {
87 g_free(conf->eap_config->anonymous_identity);
88 g_free(conf->eap_config->ca_cert);
89 g_free(conf->eap_config->client_cert);
90 g_free(conf->eap_config->private_key);
91 g_free(conf->eap_config->identity);
92 g_free(conf->eap_config->eap_type);
93 g_free(conf->eap_config->eap_auth_type);
94 g_free(conf->eap_config->subject_match);
95 g_free(conf->eap_config);
100 static gboolean __get_mac_address(gchar **mac_address)
102 gchar *tmp_mac = NULL;
104 gchar mac[13] = { 0, };
108 char buf[WIFI_MAC_ADD_LENGTH + 1];
109 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
110 fp = fopen(WIFI_MAC_ADD_PATH, "r");
113 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
118 if (fgets(buf, sizeof(buf), fp) == NULL) {
119 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
124 tmp_mac = (char *)g_try_malloc0(WIFI_MAC_ADD_LENGTH + 1);
125 if (tmp_mac == NULL) {
126 ERR("malloc() failed");
131 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
134 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
135 if (tmp_mac == NULL) {
136 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
141 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
143 while (tmp && tmp[i]) {
149 *mac_address = g_strdup(mac);
154 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
156 gchar *mac_address = NULL;
157 gchar *g_name = NULL;
158 gboolean ret = FALSE;
160 ret = __get_mac_address(&mac_address);
161 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
162 ERR("Cannot get WIFI MAC address");
166 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
167 if (g_name == NULL) {
172 *group_name = g_strdup(g_name);
180 static gboolean __get_security_type(const gchar *config_id, gchar **type)
182 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
183 *type = g_strdup(WIFI_SECURITY_NONE);
184 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
185 *type = g_strdup(WIFI_SECURITY_WEP);
186 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
187 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
188 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
189 *type = g_strdup(WIFI_SECURITY_EAP);
198 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
200 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
201 if (*config_id == NULL) {
210 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
212 GKeyFile *keyfile = NULL;
215 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
217 keyfile = netconfig_keyfile_load(path);
218 if (keyfile == NULL) {
219 ERR("keyfile[%s] is NULL", path);
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");
436 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
437 g_key_file_set_string(keyfile, group_name, key, value);
438 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
439 g_key_file_set_string(keyfile, group_name, key, value);
440 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
441 gboolean hidden = FALSE;
442 if (g_strcmp0(value, "TRUE") == 0)
444 g_key_file_set_boolean(keyfile, group_name, key, hidden);
445 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
446 g_key_file_set_string(keyfile, group_name, key, value);
447 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
448 g_key_file_set_string(keyfile, group_name, key, value);
449 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
450 g_key_file_set_string(keyfile, group_name, key, value);
451 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
452 g_key_file_set_string(keyfile, group_name, key, value);
453 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
454 g_key_file_set_string(keyfile, group_name, key, value);
455 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
456 g_key_file_set_string(keyfile, group_name, key, value);
457 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
458 g_key_file_set_string(keyfile, group_name, key, value);
459 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
460 g_key_file_set_string(keyfile, group_name, key, value);
462 ERR("key[%s] is not supported", key);
466 _save_configuration(config_id, keyfile);
468 g_key_file_free(keyfile);
474 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
479 gboolean hidden = FALSE;
480 gboolean ret = FALSE;
482 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
484 ERR("Fail to get_wifi_config_group_name");
487 DBG("group_name %s", group_name);
489 keyfile = __get_configuration_keyfile(group_name);
490 if (keyfile == NULL) {
491 ERR("Fail to __get_configuration_keyfile");
495 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
496 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
497 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
498 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
499 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
500 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
501 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
502 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
504 val = g_strdup("TRUE");
506 val = g_strdup("FALSE");
507 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
508 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
509 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
510 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
511 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
512 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
513 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
514 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
515 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
516 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
517 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
518 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
519 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
520 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
521 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
522 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
523 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
524 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
526 ERR("Invalid key[%s]", key);
527 val = g_strdup("NOTSUPPORTED");
530 *value = g_strdup(val);
533 g_key_file_free(keyfile);
539 static GSList *_get_list(void)
542 struct dirent ent_struct;
543 struct dirent *dp = NULL;
546 dir = opendir(CONNMAN_STORAGE);
548 ERR("Cannot open dir %s", CONNMAN_STORAGE);
552 while ((readdir_r(dir, &ent_struct, &dp) == 0) && dp) {
553 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
554 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
557 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
558 list = g_slist_append(list, g_strdup(config_id));
566 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
568 gboolean ret = FALSE;
571 if ((service_profile == NULL) || (config_id == NULL)) {
572 ERR("Invalid parameter");
576 ret = __get_config_id(service_profile, &val);
577 *config_id = g_strdup(val);
583 gboolean wifi_config_remove_configuration(const gchar *config_id)
585 gboolean ret = FALSE;
587 ret = _remove_configuration(config_id);
593 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
596 GSList *config_ids = NULL;
598 gchar **result = NULL;
600 g_return_val_if_fail(wifi != NULL, FALSE);
602 config_ids = _get_list();
603 if (config_ids == NULL) {
604 netconfig_error_no_profile(context);
605 ERR("Fail to get config list");
609 length = g_slist_length(config_ids);
610 result = g_new0(gchar *, length + 1);
611 for (i = 0; i < length; i++) {
612 gchar *config_id = g_slist_nth_data(config_ids, i);
613 result[i] = g_strdup(config_id);
616 config_ids = g_slist_nth(config_ids, 0);
617 g_slist_free_full(config_ids, g_free);
619 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
621 for (i = 0; i < length; i++)
631 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
632 const gchar *config_id)
634 gboolean ret = FALSE;
635 GVariantBuilder *b = NULL;
636 struct wifi_config *conf = NULL;
638 g_return_val_if_fail(wifi != NULL, FALSE);
640 conf = g_new0(struct wifi_config, 1);
642 ret = _load_configuration(config_id, conf);
645 ERR("Fail to _load_configuration");
646 netconfig_error_no_profile(context);
650 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
651 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
652 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
653 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
655 if (conf->proxy_address != NULL)
656 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
658 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
660 if (conf->last_error != NULL)
661 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
663 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
665 g_free(conf->proxy_address);
666 g_free(conf->last_error);
668 g_free(conf->security_type);
669 g_free(conf->is_hidden);
672 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
673 g_variant_builder_unref(b);
677 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
678 const gchar *config_id, GVariant *configuration)
680 gboolean ret = FALSE;
681 struct wifi_config *conf = NULL;
682 GKeyFile *keyfile = NULL;
686 gchar *group_name = NULL;
688 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
689 ERR("Invalid parameter");
690 netconfig_error_invalid_parameter(context);
694 conf = g_new0(struct wifi_config, 1);
696 g_variant_get(configuration, "a{sv}", &iter);
697 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
698 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
699 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
700 conf->name = g_strdup(g_variant_get_string(value, NULL));
701 DBG("name [%s]", conf->name);
705 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
706 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
707 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
708 DBG("ssid [%s]", conf->ssid);
712 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
713 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
714 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
715 DBG("passphrase []");
717 conf->passphrase = NULL;
719 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
720 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
721 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
722 DBG("is_hidden [%s]", conf->is_hidden);
724 conf->is_hidden = NULL;
726 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
727 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
728 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
729 DBG("proxy_address [%s]", conf->proxy_address);
731 conf->proxy_address = NULL;
735 conf->favorite = TRUE;
736 conf->autoconnect = TRUE;
738 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
740 ERR("Fail to get_wifi_config_group_name");
744 keyfile = g_key_file_new();
745 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
746 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
748 if (conf->passphrase != NULL)
749 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
751 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
752 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
755 if (conf->proxy_address != NULL) {
756 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
757 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
760 if (conf->is_hidden != NULL) {
761 gboolean hidden = FALSE;
762 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
764 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
767 ret = _save_configuration(config_id, keyfile);
769 INFO("Success to save configuration [%s]", config_id);
770 wifi_complete_save_configuration(wifi, context);
772 INFO("Fail to save configuration [%s]", config_id);
773 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
776 g_key_file_free(keyfile);
779 g_free(conf->passphrase);
780 g_free(conf->is_hidden);
781 g_free(conf->proxy_address);
784 g_variant_iter_free(iter);
789 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
790 const gchar *config_id)
792 gboolean ret = FALSE;
793 GVariantBuilder *b = NULL;
794 struct wifi_config *conf = NULL;
796 g_return_val_if_fail(wifi != NULL, FALSE);
798 conf = g_new0(struct wifi_config, 1);
799 conf->eap_config = g_new0(struct wifi_eap_config, 1);
801 ret = _load_configuration(config_id, conf);
803 g_free(conf->eap_config);
805 ERR("Fail to _load_configuration");
806 netconfig_error_no_profile(context);
810 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
811 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
812 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
813 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
814 if (conf->proxy_address != NULL) {
815 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
817 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
819 if (conf->last_error != NULL) {
820 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
822 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
824 if (conf->eap_config != NULL) {
825 if (conf->eap_config->anonymous_identity != NULL)
826 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
828 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
830 if (conf->eap_config->ca_cert != NULL)
831 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
833 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
835 if (conf->eap_config->client_cert != NULL)
836 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
838 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
840 if (conf->eap_config->private_key != NULL)
841 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
843 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
845 if (conf->eap_config->identity != NULL)
846 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
848 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
850 if (conf->eap_config->eap_type != NULL)
851 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
853 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
855 if (conf->eap_config->eap_auth_type != NULL)
856 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
858 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
860 if (conf->eap_config->subject_match != NULL)
861 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
863 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
866 __free_wifi_configuration(conf);
868 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
869 g_variant_builder_unref(b);
873 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
874 const gchar *config_id, GVariant *configuration)
876 gboolean ret = FALSE;
877 struct wifi_config *conf = NULL;
878 GKeyFile *keyfile = NULL;
882 gchar *group_name = NULL;
884 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
885 ERR("Invalid parameter");
886 netconfig_error_invalid_parameter(context);
890 conf = g_new0(struct wifi_config, 1);
891 conf->eap_config = g_new0(struct wifi_eap_config, 1);
893 g_variant_get(configuration, "a{sv}", &iter);
894 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
895 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
896 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
897 conf->name = g_strdup(g_variant_get_string(value, NULL));
898 DBG("name [%s]", conf->name);
902 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
903 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
904 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
905 DBG("ssid [%s]", conf->ssid);
909 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
910 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
911 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
912 DBG("passphrase [%s]", conf->passphrase);
914 conf->passphrase = NULL;
916 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
917 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
918 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
919 DBG("is_hidden [%s]", conf->is_hidden);
921 conf->is_hidden = NULL;
923 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
924 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
925 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
926 DBG("proxy_address [%s]", conf->proxy_address);
928 conf->proxy_address = NULL;
930 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
931 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
932 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
933 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
935 conf->eap_config->anonymous_identity = NULL;
937 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
938 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
939 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
940 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
942 conf->eap_config->ca_cert = NULL;
944 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
945 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
946 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
947 DBG("client_cert [%s]", conf->eap_config->client_cert);
949 conf->eap_config->client_cert = NULL;
951 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
952 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
953 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
954 DBG("private_key [%s]", conf->eap_config->private_key);
956 conf->eap_config->private_key = NULL;
958 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
959 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
960 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
961 DBG("identity [%s]", conf->eap_config->identity);
963 conf->eap_config->identity = NULL;
965 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
966 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
967 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
968 DBG("eap_type [%s]", conf->eap_config->eap_type);
970 conf->eap_config->eap_type = NULL;
972 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
973 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
974 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
975 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
977 conf->eap_config->eap_auth_type = NULL;
979 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
980 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
981 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
982 DBG("subject_match [%s]", conf->eap_config->subject_match);
984 conf->eap_config->subject_match = NULL;
988 conf->favorite = TRUE;
989 conf->autoconnect = TRUE;
991 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
993 __free_wifi_configuration(conf);
994 ERR("Fail to get_wifi_config_group_name");
998 keyfile = g_key_file_new();
999 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1000 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1002 if (conf->passphrase != NULL)
1003 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1005 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1006 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1008 /* Optional field */
1009 if (conf->proxy_address != NULL) {
1010 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1011 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1014 if (conf->is_hidden != NULL) {
1015 gboolean hidden = FALSE;
1016 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1018 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1021 if (conf->eap_config->anonymous_identity != NULL)
1022 g_key_file_set_string(keyfile, group_name,
1023 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1025 if (conf->eap_config->ca_cert != NULL)
1026 g_key_file_set_string(keyfile, group_name,
1027 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1029 if (conf->eap_config->client_cert != NULL)
1030 g_key_file_set_string(keyfile, group_name,
1031 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1033 if (conf->eap_config->private_key != NULL)
1034 g_key_file_set_string(keyfile, group_name,
1035 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1037 if (conf->eap_config->identity != NULL)
1038 g_key_file_set_string(keyfile, group_name,
1039 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1041 if (conf->eap_config->eap_type != NULL)
1042 g_key_file_set_string(keyfile, group_name,
1043 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1045 if (conf->eap_config->eap_auth_type != NULL)
1046 g_key_file_set_string(keyfile, group_name,
1047 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1049 if (conf->eap_config->subject_match != NULL)
1050 g_key_file_set_string(keyfile, group_name,
1051 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1053 ret = _save_configuration(config_id, keyfile);
1055 INFO("Success to save eap configuration [%s]", config_id);
1056 wifi_complete_save_eap_configuration(wifi, context);
1058 INFO("Fail to save eap configuration [%s]", config_id);
1059 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1062 g_key_file_free(keyfile);
1063 __free_wifi_configuration(conf);
1065 g_variant_iter_free(iter);
1070 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1072 gboolean ret = FALSE;
1074 if ((wifi == NULL) || (config_id == NULL)) {
1075 ERR("Invalid parameter");
1076 netconfig_error_invalid_parameter(context);
1080 ret = _remove_configuration(config_id);
1082 /* no configuration or error */
1083 ERR("No [%s] configuration", config_id);
1084 netconfig_error_no_profile(context);
1088 wifi_complete_remove_configuration(wifi, context);
1092 /* config field key / value */
1094 * [wifi_macaddress_config_id]
1095 * Name=name (mandatory)
1096 * SSID=SSID (mandatory)
1097 * Frequency=2462 (X)
1099 * AutoConnect=true (Default true)
1100 * Modified=2015-03-20 (X)
1101 * IPv4.method=manual (O)
1102 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1103 * IPv6.method=auto (X)
1104 * IPv6.privacy=disabled (X)
1105 * IPv4.netmask_prefixlen=24 (X)
1106 * IPv4.local_address=192.0.0.1 (O)
1107 * IPv4.gateway=192.0.0.1 (O ? X ?)
1108 * Nameservers=192.168.43.22; (O)
1109 * Proxy.Method=manual (O)
1110 * Proxy.Servers=trst.com:8888; (O)
1112 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1113 const gchar *config_id, const gchar *key, const gchar *value)
1115 gboolean ret = FALSE;
1116 gchar *keyfile_key = NULL;
1118 g_return_val_if_fail(wifi != NULL, FALSE);
1119 g_return_val_if_fail(config_id != NULL, FALSE);
1120 g_return_val_if_fail(key != NULL, FALSE);
1122 DBG("Key[%s] Value[%d]", key, value);
1124 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1125 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1127 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1128 netconfig_error_invalid_parameter(context);
1131 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1132 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1133 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1134 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1135 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1136 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1137 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1138 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1139 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1140 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1141 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1142 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1143 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1144 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1145 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1146 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1147 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1148 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1149 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1151 ERR("Not supported key[%s]", key);
1152 netconfig_error_invalid_parameter(context);
1156 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1158 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1162 if (keyfile_key != NULL)
1163 g_free(keyfile_key);
1165 wifi_complete_set_config_field(wifi, context);
1169 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1171 gboolean ret = FALSE;
1172 gchar *passphrase = NULL;
1174 if ((wifi == NULL) || (config_id == NULL)) {
1175 ERR("Invalid parameter");
1176 netconfig_error_invalid_parameter(context);
1180 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1182 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1183 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1187 wifi_complete_get_config_passphrase(wifi, context, passphrase);