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 struct wifi_eap_config {
48 gchar *anonymous_identity;
67 struct wifi_eap_config *eap_config;
71 static void __free_wifi_configuration(struct wifi_config *conf)
78 g_free(conf->passphrase);
79 g_free(conf->security_type);
80 g_free(conf->is_hidden);
81 g_free(conf->proxy_address);
82 if (conf->eap_config) {
83 g_free(conf->eap_config->anonymous_identity);
84 g_free(conf->eap_config->ca_cert);
85 g_free(conf->eap_config->client_cert);
86 g_free(conf->eap_config->private_key);
87 g_free(conf->eap_config->identity);
88 g_free(conf->eap_config->eap_type);
89 g_free(conf->eap_config->eap_auth_type);
90 g_free(conf->eap_config->subject_match);
91 g_free(conf->eap_config);
96 static gboolean __get_mac_address(gchar **mac_address)
98 gchar *tmp_mac = NULL;
100 gchar mac[13] = { 0, };
103 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
104 if (tmp_mac == NULL) {
105 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
109 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
119 *mac_address = g_strdup(mac);
124 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
126 gchar *mac_address = NULL;
127 gchar *g_name = NULL;
128 gboolean ret = FALSE;
130 ret = __get_mac_address(&mac_address);
131 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
132 ERR("Cannot get WIFI MAC address");
136 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
137 if (g_name == NULL) {
142 *group_name = g_strdup(g_name);
150 static gboolean __get_security_type(const gchar *config_id, gchar **type)
152 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
153 *type = g_strdup(WIFI_SECURITY_NONE);
154 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
155 *type = g_strdup(WIFI_SECURITY_WEP);
156 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
157 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
158 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
159 *type = g_strdup(WIFI_SECURITY_EAP);
168 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
170 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
171 if (*config_id == NULL) {
180 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
182 GKeyFile *keyfile = NULL;
185 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
187 keyfile = netconfig_keyfile_load(path);
188 if (keyfile == NULL) {
189 ERR("keyfile[%s] is NULL", path);
196 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
198 gboolean ret = FALSE;
201 path = g_strdup_printf("%s/%s", pathname, filename);
202 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
204 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
213 static gboolean __remove_configuration(const gchar *pathname)
217 if (__remove_file(pathname, "settings") != TRUE) {
218 ERR("Cannot remove [%s/settings]", pathname);
221 if (__remove_file(pathname, "data") != TRUE) {
222 ERR("Cannot remove [%s/data]", pathname);
226 ret = rmdir(pathname);
228 ERR("Cannot remove [%s]", pathname);
235 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
239 gboolean hidden = FALSE;
240 gboolean ret = FALSE;
242 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
244 ERR("Fail to get_wifi_config_group_name");
248 keyfile = __get_configuration_keyfile(group_name);
249 if (keyfile == NULL) {
250 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
255 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
256 ret = __get_security_type(config_id, &config->security_type);
258 ERR("Fail to _get_security_type");
259 g_key_file_free(keyfile);
263 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
264 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
266 config->is_hidden = g_strdup("TRUE");
268 config->is_hidden = g_strdup("FALSE");
271 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
272 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
273 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
274 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
275 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
276 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
277 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
278 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
279 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
282 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
284 g_key_file_free(keyfile);
290 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
295 gboolean ret = FALSE;
297 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
299 ERR("Fail to get_wifi_config_group_name");
303 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
304 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
305 if (__remove_configuration(dir) != TRUE) {
306 ERR("[%s] is existed, but cannot remove", dir);
313 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
314 ERR("Cannot mkdir %s", dir);
320 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
321 netconfig_keyfile_save(keyfile, path);
329 static gboolean _remove_configuration(const gchar *config_id)
331 gboolean ret = FALSE;
335 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
337 ERR("Fail to get_wifi_config_group_name");
341 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
342 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
343 if (__remove_configuration(dir) != TRUE) {
344 ERR("[%s] is existed, but cannot remove", dir);
347 INFO("Success to remove [%s]", dir);
350 ERR("[%s] is not existed", dir);
361 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
367 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
369 ERR("Fail to get_wifi_config_group_name");
372 DBG("group_name %s", group_name);
374 keyfile = __get_configuration_keyfile(group_name);
375 if (keyfile == NULL) {
376 ERR("Fail to __get_configuration_keyfile");
380 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
381 g_key_file_set_string(keyfile, group_name, key, value);
382 }else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
383 g_key_file_set_string(keyfile, group_name, key, value);
384 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
385 gboolean hidden = FALSE;
386 if (g_strcmp0(value, "TRUE") == 0) {
389 g_key_file_set_boolean(keyfile, group_name, key, hidden);
390 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
391 g_key_file_set_string(keyfile, group_name, key, value);
392 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
393 g_key_file_set_string(keyfile, group_name, key, value);
394 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
395 g_key_file_set_string(keyfile, group_name, key, value);
396 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
397 g_key_file_set_string(keyfile, group_name, key, value);
398 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
399 g_key_file_set_string(keyfile, group_name, key, value);
400 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
401 g_key_file_set_string(keyfile, group_name, key, value);
402 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
403 g_key_file_set_string(keyfile, group_name, key, value);
404 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
405 g_key_file_set_string(keyfile, group_name, key, value);
407 ERR("key[%s] is not supported", key);
411 _save_configuration(config_id, keyfile);
413 g_key_file_free(keyfile);
419 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
424 gboolean hidden = FALSE;
425 gboolean ret = FALSE;
427 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
429 ERR("Fail to get_wifi_config_group_name");
432 DBG("group_name %s", group_name);
434 keyfile = __get_configuration_keyfile(group_name);
435 if (keyfile == NULL) {
436 ERR("Fail to __get_configuration_keyfile");
440 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
441 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
442 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
443 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
444 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
445 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
446 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
447 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
449 val = g_strdup("TRUE");
451 val = g_strdup("FALSE");
453 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
454 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
455 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
456 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
457 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
458 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
459 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
460 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
461 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
462 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
463 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
464 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
465 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
466 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
467 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
468 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
469 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
470 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
472 ERR("Invalid key[%s]", key);
473 val = g_strdup("NOTSUPPORTED");
476 *value = g_strdup(val);
479 g_key_file_free(keyfile);
485 static GSList *_get_list(void)
488 struct dirent ent_struct;
489 struct dirent *dp = NULL;
492 dir = opendir(CONNMAN_STORAGE);
494 ERR("Cannot open dir %s", CONNMAN_STORAGE);
498 while ((readdir_r(dir, &ent_struct, &dp) == 0) && dp) {
499 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
500 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
503 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
504 list = g_slist_append(list, g_strdup(config_id));
512 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
514 gboolean ret = FALSE;
517 if ((service_profile == NULL) || (config_id == NULL)) {
518 ERR("Invalid parameter");
522 ret = __get_config_id(service_profile, &val);
523 *config_id = g_strdup(val);
529 gboolean wifi_config_remove_configuration(const gchar *config_id)
531 gboolean ret = FALSE;
533 ret = _remove_configuration(config_id);
539 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
542 GSList *config_ids = NULL;
544 gchar **result = NULL;
546 g_return_val_if_fail(wifi != NULL, FALSE);
548 config_ids = _get_list();
549 if (config_ids == NULL) {
550 netconfig_error_no_profile(context);
551 ERR("Fail to get config list");
555 length = g_slist_length(config_ids);
556 result = g_new0(gchar *, length + 1);
557 for (i = 0; i < length; i++) {
558 gchar *config_id = g_slist_nth_data(config_ids, i);
559 result[i] = g_strdup(config_id);
562 config_ids = g_slist_nth(config_ids, 0);
563 g_slist_free_full(config_ids, g_free);
565 wifi_complete_get_config_ids(wifi, context, (const gchar * const*)result);
569 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
570 const gchar *config_id)
572 gboolean ret = FALSE;
573 GVariantBuilder *b = NULL;
574 struct wifi_config *conf = NULL;
576 g_return_val_if_fail(wifi != NULL, FALSE);
578 conf = g_new0(struct wifi_config, 1);
580 ret = _load_configuration(config_id, conf);
583 ERR("Fail to _load_configuration");
584 netconfig_error_no_profile(context);
588 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
589 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
590 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
591 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
592 if (conf->proxy_address != NULL) {
593 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
594 g_free(conf->proxy_address);
596 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
598 if (conf->last_error != NULL) {
599 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
600 g_free(conf->last_error);
602 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
606 g_free(conf->security_type);
607 g_free(conf->is_hidden);
610 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
611 g_variant_builder_unref(b);
615 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
616 const gchar *config_id, GVariant *configuration)
618 gboolean ret = FALSE;
619 struct wifi_config *conf = NULL;
620 GKeyFile *keyfile = NULL;
624 gchar *group_name = NULL;
626 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
627 ERR("Invalid parameter");
628 netconfig_error_invalid_parameter(context);
629 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
633 ERR("save_configuration [%s]", config_id);
635 conf = g_new0(struct wifi_config, 1);
637 g_variant_get(configuration, "a{sv}", &iter);
638 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
639 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
640 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
641 conf->name = g_strdup(g_variant_get_string(value, NULL));
642 ERR("name [%s]", conf->name);
646 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
647 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
648 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
649 ERR("ssid [%s]", conf->ssid);
653 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
654 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
655 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
656 ERR("passphrase []");
658 conf->passphrase = NULL;
660 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
661 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
662 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
663 ERR("is_hidden [%s]", conf->is_hidden);
665 conf->is_hidden = NULL;
667 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
668 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
669 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
670 ERR("proxy_address [%s]", conf->proxy_address);
672 conf->proxy_address = NULL;
676 conf->favorite = TRUE;
677 conf->autoconnect = TRUE;
679 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
681 ERR("Fail to get_wifi_config_group_name");
685 keyfile = g_key_file_new();
686 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
687 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
689 if (conf->passphrase != NULL)
690 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
692 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
693 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
696 if (conf->proxy_address != NULL) {
697 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
698 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
701 if (conf->is_hidden != NULL) {
702 gboolean hidden = FALSE;
703 if (g_strcmp0(conf->is_hidden, "TRUE") == 0) {
706 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
709 ret = _save_configuration(config_id, keyfile);
711 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Succeed");
712 wifi_complete_save_configuration(wifi, context);
714 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
715 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
718 g_key_file_free(keyfile);
721 g_free(conf->passphrase);
722 g_free(conf->is_hidden);
723 g_free(conf->proxy_address);
726 g_variant_iter_free(iter);
731 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
732 const gchar *config_id)
734 gboolean ret = FALSE;
735 GVariantBuilder *b = NULL;
736 struct wifi_config *conf = NULL;
738 g_return_val_if_fail(wifi != NULL, FALSE);
740 conf = g_new0(struct wifi_config, 1);
741 conf->eap_config = g_new0(struct wifi_eap_config, 1);
743 ret = _load_configuration(config_id, conf);
745 g_free(conf->eap_config);
747 ERR("Fail to _load_configuration");
748 netconfig_error_no_profile(context);
752 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
753 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
754 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
755 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
756 if (conf->proxy_address != NULL) {
757 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
758 g_free(conf->proxy_address);
760 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
762 if (conf->last_error != NULL) {
763 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
764 g_free(conf->last_error);
766 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
768 if (conf->eap_config != NULL) {
769 if (conf->eap_config->anonymous_identity != NULL) {
770 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
772 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
774 if (conf->eap_config->ca_cert != NULL) {
775 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
777 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
779 if (conf->eap_config->client_cert != NULL) {
780 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
782 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
784 if (conf->eap_config->private_key != NULL) {
785 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
787 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
789 if (conf->eap_config->identity != NULL) {
790 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
792 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
794 if (conf->eap_config->eap_type != NULL) {
795 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
797 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
799 if (conf->eap_config->eap_auth_type != NULL) {
800 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
802 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
804 if (conf->eap_config->subject_match != NULL) {
805 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
807 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
811 __free_wifi_configuration(conf);
813 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
814 g_variant_builder_unref(b);
818 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
819 const gchar *config_id, GVariant *configuration)
821 gboolean ret = FALSE;
822 struct wifi_config *conf = NULL;
823 GKeyFile *keyfile = NULL;
827 gchar *group_name = NULL;
829 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
830 ERR("Invalid parameter");
831 netconfig_error_invalid_parameter(context);
832 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
836 INFO("save [%s]", config_id);
838 conf = g_new0(struct wifi_config, 1);
839 conf->eap_config = g_new0(struct wifi_eap_config, 1);
841 g_variant_get(configuration, "a{sv}", &iter);
842 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
843 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
844 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
845 conf->name = g_strdup(g_variant_get_string(value, NULL));
846 ERR("name [%s]", conf->name);
850 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
851 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
852 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
853 ERR("ssid [%s]", conf->ssid);
857 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
858 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
859 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
860 ERR("passphrase [%s]", conf->passphrase);
862 conf->passphrase = NULL;
864 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
865 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
866 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
867 ERR("is_hidden [%s]", conf->is_hidden);
869 conf->is_hidden = NULL;
871 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
872 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
873 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
874 ERR("proxy_address [%s]", conf->proxy_address);
876 conf->proxy_address = NULL;
878 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
879 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
880 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
881 ERR("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
883 conf->eap_config->anonymous_identity = NULL;
885 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
886 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
887 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
888 ERR("ca_cert [%s]", conf->eap_config->ca_cert);
890 conf->eap_config->ca_cert = NULL;
892 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
893 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
894 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
895 ERR("client_cert [%s]", conf->eap_config->client_cert);
897 conf->eap_config->client_cert = NULL;
899 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
900 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
901 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
902 ERR("private_key [%s]", conf->eap_config->private_key);
904 conf->eap_config->private_key = NULL;
906 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
907 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
908 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
909 ERR("identity [%s]", conf->eap_config->identity);
911 conf->eap_config->identity = NULL;
913 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
914 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
915 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
916 ERR("eap_type [%s]", conf->eap_config->eap_type);
918 conf->eap_config->eap_type = NULL;
920 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
921 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
922 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
923 ERR("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
925 conf->eap_config->eap_auth_type = NULL;
927 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
928 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
929 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
930 ERR("subject_match [%s]", conf->eap_config->subject_match);
932 conf->eap_config->subject_match = NULL;
936 conf->favorite = TRUE;
937 conf->autoconnect = TRUE;
939 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
941 __free_wifi_configuration(conf);
942 ERR("Fail to get_wifi_config_group_name");
946 keyfile = g_key_file_new();
947 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
948 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
950 if (conf->passphrase != NULL)
951 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
953 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
954 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
957 if (conf->proxy_address != NULL) {
958 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
959 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
962 if (conf->is_hidden != NULL) {
963 gboolean hidden = FALSE;
964 if (g_strcmp0(conf->is_hidden, "TRUE") == 0) {
967 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
970 ret = _save_configuration(config_id, keyfile);
972 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Succeed");
973 wifi_complete_save_eap_configuration(wifi, context);
975 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
976 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
979 g_key_file_free(keyfile);
980 __free_wifi_configuration(conf);
982 g_variant_iter_free(iter);
987 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
989 gboolean ret = FALSE;
991 if ((wifi == NULL) || (config_id == NULL)) {
992 ERR("Invalid parameter");
993 netconfig_error_invalid_parameter(context);
997 ret = _remove_configuration(config_id);
999 // no configuration or error
1000 ERR("No [%s] configuration", config_id);
1001 netconfig_error_no_profile(context);
1005 wifi_complete_remove_configuration(wifi, context);
1009 // config field key / value
1011 * [wifi_macaddress_config_id]
1012 * Name=name (mandatory)
1013 * SSID=SSID (mandatory)
1014 * Frequency=2462 (X)
1016 * AutoConnect=true (Default true)
1017 * Modified=2015-03-20 (X)
1018 * IPv4.method=manual (O)
1019 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1020 * IPv6.method=auto (X)
1021 * IPv6.privacy=disabled (X)
1022 * IPv4.netmask_prefixlen=24 (X)
1023 * IPv4.local_address=192.0.0.1 (O)
1024 * IPv4.gateway=192.0.0.1 (O ? X ?)
1025 * Nameservers=192.168.43.22; (O)
1026 * Proxy.Method=manual (O)
1027 * Proxy.Servers=trst.com:8888; (O)
1029 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1030 const gchar *config_id, const gchar *key, const gchar *value)
1032 gboolean ret = FALSE;
1033 gchar *keyfile_key = NULL;
1035 g_return_val_if_fail(wifi != NULL, FALSE);
1036 g_return_val_if_fail(config_id != NULL, FALSE);
1037 g_return_val_if_fail(key != NULL, FALSE);
1039 DBG("Key[%s] Value[%d]", key, value);
1041 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1042 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1044 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1045 netconfig_error_invalid_parameter(context);
1048 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1049 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1050 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1051 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1052 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1053 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1054 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1055 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1056 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1057 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1058 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1059 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1060 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1061 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1062 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1063 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1064 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1065 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1066 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1068 ERR("Not supported key[%s]", key);
1069 netconfig_error_invalid_parameter(context);
1073 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1075 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1079 if (keyfile_key != NULL)
1080 g_free(keyfile_key);
1082 wifi_complete_set_config_field(wifi,context);
1086 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1088 gboolean ret = FALSE;
1089 gchar *passphrase = NULL;
1091 if ((wifi == NULL) || (config_id == NULL)) {
1092 ERR("Invalid parameter");
1093 netconfig_error_invalid_parameter(context);
1097 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1099 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1100 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1104 wifi_complete_get_config_passphrase(wifi, context, passphrase);