2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <sys/types.h>
33 #include "wifi-config.h"
35 #define CONNMAN_STORAGE "/var/lib/connman"
37 #define WIFI_SECURITY_NONE "none"
38 #define WIFI_SECURITY_WEP "wep"
39 #define WIFI_SECURITY_WPA_PSK "psk"
40 #define WIFI_SECURITY_EAP "ieee8021x"
42 #define WIFI_CONFIG_PREFIX "wifi_"
43 #define MAC_ADDRESS_LENGTH 12
44 #define WIFI_PREFIX_LENGTH MAC_ADDRESS_LENGTH + 6 /* wifi_485a3f2f506a_ */
45 #define PROFILE_PREFIX_LENGTH WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
47 #define WIFI_MAC_ADD_LENGTH 17
48 #define WIFI_MAC_ADD_PATH "/sys/class/net/wlan0/address"
50 struct wifi_eap_config {
51 gchar *anonymous_identity;
70 struct wifi_eap_config *eap_config;
74 static void __free_wifi_configuration(struct wifi_config *conf)
81 g_free(conf->passphrase);
82 g_free(conf->security_type);
83 g_free(conf->is_hidden);
84 g_free(conf->proxy_address);
85 g_free(conf->last_error);
86 if (conf->eap_config) {
87 g_free(conf->eap_config->anonymous_identity);
88 g_free(conf->eap_config->ca_cert);
89 g_free(conf->eap_config->client_cert);
90 g_free(conf->eap_config->private_key);
91 g_free(conf->eap_config->identity);
92 g_free(conf->eap_config->eap_type);
93 g_free(conf->eap_config->eap_auth_type);
94 g_free(conf->eap_config->subject_match);
95 g_free(conf->eap_config);
100 static gboolean __get_mac_address(gchar **mac_address)
102 gchar *tmp_mac = NULL;
104 gchar mac[13] = { 0, };
109 char buf[WIFI_MAC_ADD_LENGTH + 1];
110 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
111 fp = fopen(WIFI_MAC_ADD_PATH, "r");
114 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
119 if (fgets(buf, sizeof(buf), fp) == NULL) {
120 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
125 tmp_mac = (char *)g_try_malloc0(WIFI_MAC_ADD_LENGTH + 1);
126 if (tmp_mac == NULL) {
127 ERR("malloc() failed");
132 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
135 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
136 if (tmp_mac == NULL) {
137 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
142 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
144 while (tmp && tmp[i]) {
150 *mac_address = g_strdup(mac);
155 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
157 gchar *mac_address = NULL;
158 gchar *g_name = NULL;
159 gboolean ret = FALSE;
161 ret = __get_mac_address(&mac_address);
162 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
163 ERR("Cannot get WIFI MAC address");
167 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
168 if (g_name == NULL) {
173 *group_name = g_strdup(g_name);
181 static gboolean __get_security_type(const gchar *config_id, gchar **type)
183 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
184 *type = g_strdup(WIFI_SECURITY_NONE);
185 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
186 *type = g_strdup(WIFI_SECURITY_WEP);
187 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
188 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
189 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
190 *type = g_strdup(WIFI_SECURITY_EAP);
199 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
201 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
202 if (*config_id == NULL) {
211 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
213 GKeyFile *keyfile = NULL;
216 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
218 keyfile = netconfig_keyfile_load(path);
219 if (keyfile == NULL) {
220 ERR("keyfile[%s] is NULL", path);
228 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
230 gboolean ret = FALSE;
233 path = g_strdup_printf("%s/%s", pathname, filename);
234 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
236 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
245 static gboolean __remove_configuration(const gchar *pathname)
249 if (__remove_file(pathname, "settings") != TRUE) {
250 ERR("Cannot remove [%s/settings]", pathname);
253 if (__remove_file(pathname, "data") != TRUE) {
254 ERR("Cannot remove [%s/data]", pathname);
258 ret = rmdir(pathname);
260 ERR("Cannot remove [%s]", pathname);
267 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
271 gboolean hidden = FALSE;
272 gboolean ret = FALSE;
274 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
276 ERR("Fail to get_wifi_config_group_name");
280 keyfile = __get_configuration_keyfile(group_name);
281 if (keyfile == NULL) {
282 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
287 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
288 DBG("name [%s]", config->name);
290 ret = __get_security_type(config_id, &config->security_type);
292 ERR("Fail to _get_security_type");
293 g_key_file_free(keyfile);
297 DBG("security_type [%s]", config->security_type);
299 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
300 if (config->proxy_address)
301 DBG("proxy_address [%s]", config->proxy_address);
303 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
305 config->is_hidden = g_strdup("TRUE");
307 config->is_hidden = g_strdup("FALSE");
308 DBG("is_hidden [%s]", config->is_hidden);
310 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
311 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
312 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
313 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
314 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
315 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
316 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
317 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
318 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
320 if (config->eap_config->anonymous_identity)
321 DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
322 if (config->eap_config->ca_cert)
323 DBG("ca_cert [%s]", config->eap_config->ca_cert);
324 if (config->eap_config->client_cert)
325 DBG("client_cert [%s]", config->eap_config->client_cert);
326 if (config->eap_config->private_key)
327 DBG("private_key [%s]", config->eap_config->private_key);
328 if (config->eap_config->identity)
329 DBG("identity [%s]", config->eap_config->identity);
330 if (config->eap_config->eap_type)
331 DBG("eap_type [%s]", config->eap_config->eap_type);
332 if (config->eap_config->eap_auth_type)
333 DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
334 if (config->eap_config->subject_match)
335 DBG("subject_match [%s]", config->eap_config->subject_match);
338 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
339 if (config->last_error)
340 DBG("last_error [%s]", config->last_error);
342 g_key_file_free(keyfile);
348 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
353 gboolean ret = FALSE;
355 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
357 ERR("Fail to get_wifi_config_group_name");
361 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
362 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
363 if (__remove_configuration(dir) != TRUE) {
364 ERR("[%s] is existed, but cannot remove", dir);
371 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
372 ERR("Cannot mkdir %s", dir);
378 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
379 netconfig_keyfile_save(keyfile, path);
387 static gboolean _remove_configuration(const gchar *config_id)
389 gboolean ret = FALSE;
393 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
395 ERR("Fail to get_wifi_config_group_name");
399 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
400 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
401 if (__remove_configuration(dir) != TRUE) {
402 ERR("[%s] is existed, but cannot remove", dir);
405 INFO("Success to remove [%s]", dir);
408 ERR("[%s] is not existed", dir);
419 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
425 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
427 ERR("Fail to get_wifi_config_group_name");
430 DBG("group_name %s", group_name);
432 keyfile = __get_configuration_keyfile(group_name);
433 if (keyfile == NULL) {
434 ERR("Fail to __get_configuration_keyfile");
439 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
440 g_key_file_set_string(keyfile, group_name, key, value);
441 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
442 g_key_file_set_string(keyfile, group_name, key, value);
443 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
444 gboolean hidden = FALSE;
445 if (g_strcmp0(value, "TRUE") == 0)
447 g_key_file_set_boolean(keyfile, group_name, key, hidden);
448 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
449 g_key_file_set_string(keyfile, group_name, key, value);
450 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
451 g_key_file_set_string(keyfile, group_name, key, value);
452 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
453 g_key_file_set_string(keyfile, group_name, key, value);
454 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
455 g_key_file_set_string(keyfile, group_name, key, value);
456 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
457 g_key_file_set_string(keyfile, group_name, key, value);
458 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
459 g_key_file_set_string(keyfile, group_name, key, value);
460 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
461 g_key_file_set_string(keyfile, group_name, key, value);
462 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
463 g_key_file_set_string(keyfile, group_name, key, value);
465 ERR("key[%s] is not supported", key);
469 _save_configuration(config_id, keyfile);
471 g_key_file_free(keyfile);
477 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
482 gboolean hidden = FALSE;
483 gboolean ret = FALSE;
485 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
487 ERR("Fail to get_wifi_config_group_name");
490 DBG("group_name %s", group_name);
492 keyfile = __get_configuration_keyfile(group_name);
493 if (keyfile == NULL) {
494 ERR("Fail to __get_configuration_keyfile");
499 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
500 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
501 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
502 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
503 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
504 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
505 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
506 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
508 val = g_strdup("TRUE");
510 val = g_strdup("FALSE");
511 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
512 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
513 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
514 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
515 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
516 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
517 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
518 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
519 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
520 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
521 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
522 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
523 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
524 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
525 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
526 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
527 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
528 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
530 ERR("Invalid key[%s]", key);
531 val = g_strdup("NOTSUPPORTED");
534 *value = g_strdup(val);
537 g_key_file_free(keyfile);
543 static GSList *_get_list(void)
546 struct dirent *dp = NULL;
549 dir = opendir(CONNMAN_STORAGE);
551 ERR("Cannot open dir %s", CONNMAN_STORAGE);
555 while ((dp = readdir(dir)) != NULL) {
556 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
557 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
560 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
561 list = g_slist_append(list, g_strdup(config_id));
569 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
571 gboolean ret = FALSE;
574 if ((service_profile == NULL) || (config_id == NULL)) {
575 ERR("Invalid parameter");
579 ret = __get_config_id(service_profile, &val);
580 *config_id = g_strdup(val);
586 gboolean wifi_config_remove_configuration(const gchar *config_id)
588 gboolean ret = FALSE;
590 ret = _remove_configuration(config_id);
596 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
599 GSList *config_ids = NULL;
601 gchar **result = NULL;
603 g_return_val_if_fail(wifi != NULL, FALSE);
605 config_ids = _get_list();
606 if (config_ids == NULL) {
607 netconfig_error_no_profile(context);
608 ERR("Fail to get config list");
612 length = g_slist_length(config_ids);
613 result = g_new0(gchar *, length + 1);
614 for (i = 0; i < length; i++) {
615 gchar *config_id = g_slist_nth_data(config_ids, i);
616 result[i] = g_strdup(config_id);
619 config_ids = g_slist_nth(config_ids, 0);
620 g_slist_free_full(config_ids, g_free);
622 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
624 for (i = 0; i < length; i++)
634 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
635 const gchar *config_id)
637 gboolean ret = FALSE;
638 GVariantBuilder *b = NULL;
639 struct wifi_config *conf = NULL;
641 g_return_val_if_fail(wifi != NULL, FALSE);
643 conf = g_new0(struct wifi_config, 1);
645 ret = _load_configuration(config_id, conf);
648 ERR("Fail to _load_configuration");
649 netconfig_error_no_profile(context);
653 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
654 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
655 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
656 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
658 if (conf->proxy_address != NULL)
659 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
661 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
663 if (conf->last_error != NULL)
664 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
666 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
668 g_free(conf->proxy_address);
669 g_free(conf->last_error);
671 g_free(conf->security_type);
672 g_free(conf->is_hidden);
675 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
676 g_variant_builder_unref(b);
680 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
681 const gchar *config_id, GVariant *configuration)
683 gboolean ret = FALSE;
684 struct wifi_config *conf = NULL;
685 GKeyFile *keyfile = NULL;
689 gchar *group_name = NULL;
691 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
692 ERR("Invalid parameter");
693 netconfig_error_invalid_parameter(context);
697 conf = g_new0(struct wifi_config, 1);
699 g_variant_get(configuration, "a{sv}", &iter);
700 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
701 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
702 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
703 conf->name = g_strdup(g_variant_get_string(value, NULL));
704 DBG("name [%s]", conf->name);
708 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
709 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
710 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
711 DBG("ssid [%s]", conf->ssid);
715 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
716 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
717 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
718 DBG("passphrase []");
720 conf->passphrase = NULL;
722 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
723 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
724 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
725 DBG("is_hidden [%s]", conf->is_hidden);
727 conf->is_hidden = NULL;
729 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
730 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
731 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
732 DBG("proxy_address [%s]", conf->proxy_address);
734 conf->proxy_address = NULL;
738 conf->favorite = TRUE;
739 conf->autoconnect = TRUE;
741 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
745 g_free(conf->passphrase);
746 g_free(conf->is_hidden);
747 g_free(conf->proxy_address);
749 ERR("Fail to get_wifi_config_group_name");
753 keyfile = g_key_file_new();
754 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
755 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
757 if (conf->passphrase != NULL)
758 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
760 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
761 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
764 if (conf->proxy_address != NULL) {
765 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
766 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
769 if (conf->is_hidden != NULL) {
770 gboolean hidden = FALSE;
771 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
773 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
776 ret = _save_configuration(config_id, keyfile);
778 INFO("Success to save configuration [%s]", config_id);
779 wifi_complete_save_configuration(wifi, context);
781 INFO("Fail to save configuration [%s]", config_id);
782 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
785 g_key_file_free(keyfile);
788 g_free(conf->passphrase);
789 g_free(conf->is_hidden);
790 g_free(conf->proxy_address);
793 g_variant_iter_free(iter);
798 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
799 const gchar *config_id)
801 gboolean ret = FALSE;
802 GVariantBuilder *b = NULL;
803 struct wifi_config *conf = NULL;
805 g_return_val_if_fail(wifi != NULL, FALSE);
807 conf = g_new0(struct wifi_config, 1);
808 conf->eap_config = g_new0(struct wifi_eap_config, 1);
810 ret = _load_configuration(config_id, conf);
812 g_free(conf->eap_config);
814 ERR("Fail to _load_configuration");
815 netconfig_error_no_profile(context);
819 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
820 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
821 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
822 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
823 if (conf->proxy_address != NULL)
824 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
826 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
828 if (conf->last_error != NULL)
829 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
831 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
833 if (conf->eap_config != NULL) {
834 if (conf->eap_config->anonymous_identity != NULL)
835 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
837 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
839 if (conf->eap_config->ca_cert != NULL)
840 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
842 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
844 if (conf->eap_config->client_cert != NULL)
845 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
847 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
849 if (conf->eap_config->private_key != NULL)
850 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
852 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
854 if (conf->eap_config->identity != NULL)
855 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
857 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
859 if (conf->eap_config->eap_type != NULL)
860 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
862 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
864 if (conf->eap_config->eap_auth_type != NULL)
865 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
867 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
869 if (conf->eap_config->subject_match != NULL)
870 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
872 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
875 __free_wifi_configuration(conf);
877 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
878 g_variant_builder_unref(b);
882 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
883 const gchar *config_id, GVariant *configuration)
885 gboolean ret = FALSE;
886 struct wifi_config *conf = NULL;
887 GKeyFile *keyfile = NULL;
891 gchar *group_name = NULL;
893 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
894 ERR("Invalid parameter");
895 netconfig_error_invalid_parameter(context);
899 conf = g_new0(struct wifi_config, 1);
900 conf->eap_config = g_new0(struct wifi_eap_config, 1);
902 g_variant_get(configuration, "a{sv}", &iter);
903 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
904 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
905 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
906 conf->name = g_strdup(g_variant_get_string(value, NULL));
907 DBG("name [%s]", conf->name);
911 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
912 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
913 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
914 DBG("ssid [%s]", conf->ssid);
918 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
919 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
920 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
921 DBG("passphrase [%s]", conf->passphrase);
923 conf->passphrase = NULL;
925 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
926 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
927 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
928 DBG("is_hidden [%s]", conf->is_hidden);
930 conf->is_hidden = NULL;
932 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
933 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
934 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
935 DBG("proxy_address [%s]", conf->proxy_address);
937 conf->proxy_address = NULL;
939 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
940 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
941 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
942 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
944 conf->eap_config->anonymous_identity = NULL;
946 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
947 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
948 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
949 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
951 conf->eap_config->ca_cert = NULL;
953 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
954 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
955 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
956 DBG("client_cert [%s]", conf->eap_config->client_cert);
958 conf->eap_config->client_cert = NULL;
960 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
961 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
962 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
963 DBG("private_key [%s]", conf->eap_config->private_key);
965 conf->eap_config->private_key = NULL;
967 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
968 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
969 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
970 DBG("identity [%s]", conf->eap_config->identity);
972 conf->eap_config->identity = NULL;
974 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
975 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
976 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
977 DBG("eap_type [%s]", conf->eap_config->eap_type);
979 conf->eap_config->eap_type = NULL;
981 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
982 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
983 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
984 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
986 conf->eap_config->eap_auth_type = NULL;
988 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
989 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
990 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
991 DBG("subject_match [%s]", conf->eap_config->subject_match);
993 conf->eap_config->subject_match = NULL;
997 conf->favorite = TRUE;
998 conf->autoconnect = TRUE;
1000 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1002 __free_wifi_configuration(conf);
1003 ERR("Fail to get_wifi_config_group_name");
1007 keyfile = g_key_file_new();
1008 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1009 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1011 if (conf->passphrase != NULL)
1012 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1014 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1015 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1017 /* Optional field */
1018 if (conf->proxy_address != NULL) {
1019 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1020 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1023 if (conf->is_hidden != NULL) {
1024 gboolean hidden = FALSE;
1025 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1027 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1030 if (conf->eap_config->anonymous_identity != NULL)
1031 g_key_file_set_string(keyfile, group_name,
1032 WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1034 if (conf->eap_config->ca_cert != NULL)
1035 g_key_file_set_string(keyfile, group_name,
1036 WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1038 if (conf->eap_config->client_cert != NULL)
1039 g_key_file_set_string(keyfile, group_name,
1040 WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1042 if (conf->eap_config->private_key != NULL)
1043 g_key_file_set_string(keyfile, group_name,
1044 WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1046 if (conf->eap_config->identity != NULL)
1047 g_key_file_set_string(keyfile, group_name,
1048 WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1050 if (conf->eap_config->eap_type != NULL)
1051 g_key_file_set_string(keyfile, group_name,
1052 WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1054 if (conf->eap_config->eap_auth_type != NULL)
1055 g_key_file_set_string(keyfile, group_name,
1056 WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1058 if (conf->eap_config->subject_match != NULL)
1059 g_key_file_set_string(keyfile, group_name,
1060 WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1062 ret = _save_configuration(config_id, keyfile);
1064 INFO("Success to save eap configuration [%s]", config_id);
1065 wifi_complete_save_eap_configuration(wifi, context);
1067 INFO("Fail to save eap configuration [%s]", config_id);
1068 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1071 g_key_file_free(keyfile);
1072 __free_wifi_configuration(conf);
1074 g_variant_iter_free(iter);
1079 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1081 gboolean ret = FALSE;
1083 if ((wifi == NULL) || (config_id == NULL)) {
1084 ERR("Invalid parameter");
1085 netconfig_error_invalid_parameter(context);
1089 ret = _remove_configuration(config_id);
1091 /* no configuration or error */
1092 ERR("No [%s] configuration", config_id);
1093 netconfig_error_no_profile(context);
1097 wifi_complete_remove_configuration(wifi, context);
1101 /* config field key / value */
1103 * [wifi_macaddress_config_id]
1104 * Name=name (mandatory)
1105 * SSID=SSID (mandatory)
1106 * Frequency=2462 (X)
1108 * AutoConnect=true (Default true)
1109 * Modified=2015-03-20 (X)
1110 * IPv4.method=manual (O)
1111 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1112 * IPv6.method=auto (X)
1113 * IPv6.privacy=disabled (X)
1114 * IPv4.netmask_prefixlen=24 (X)
1115 * IPv4.local_address=192.0.0.1 (O)
1116 * IPv4.gateway=192.0.0.1 (O ? X ?)
1117 * Nameservers=192.168.43.22; (O)
1118 * Proxy.Method=manual (O)
1119 * Proxy.Servers=trst.com:8888; (O)
1121 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1122 const gchar *config_id, const gchar *key, const gchar *value)
1124 gboolean ret = FALSE;
1125 gchar *keyfile_key = NULL;
1127 g_return_val_if_fail(wifi != NULL, FALSE);
1128 g_return_val_if_fail(config_id != NULL, FALSE);
1129 g_return_val_if_fail(key != NULL, FALSE);
1131 DBG("Key[%s] Value[%d]", key, value);
1133 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1134 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1136 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1137 netconfig_error_invalid_parameter(context);
1140 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1141 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1142 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1143 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1144 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1145 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1146 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1147 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1148 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1149 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1150 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1151 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1152 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1153 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1154 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1155 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1156 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1157 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1158 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1160 ERR("Not supported key[%s]", key);
1161 netconfig_error_invalid_parameter(context);
1165 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1167 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1171 if (keyfile_key != NULL)
1172 g_free(keyfile_key);
1174 wifi_complete_set_config_field(wifi, context);
1178 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1180 gboolean ret = FALSE;
1181 gchar *passphrase = NULL;
1183 if ((wifi == NULL) || (config_id == NULL)) {
1184 ERR("Invalid parameter");
1185 netconfig_error_invalid_parameter(context);
1189 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1191 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1192 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1196 wifi_complete_get_config_passphrase(wifi, context, passphrase);