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 if (conf->eap_config) {
86 g_free(conf->eap_config->anonymous_identity);
87 g_free(conf->eap_config->ca_cert);
88 g_free(conf->eap_config->client_cert);
89 g_free(conf->eap_config->private_key);
90 g_free(conf->eap_config->identity);
91 g_free(conf->eap_config->eap_type);
92 g_free(conf->eap_config->eap_auth_type);
93 g_free(conf->eap_config->subject_match);
94 g_free(conf->eap_config);
99 static gboolean __get_mac_address(gchar **mac_address)
101 gchar *tmp_mac = NULL;
103 gchar mac[13] = { 0, };
107 char buf[WIFI_MAC_ADD_LENGTH + 1];
108 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
109 fp = fopen(WIFI_MAC_ADD_PATH, "r");
112 ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
117 if (fgets(buf, sizeof(buf), fp) == NULL) {
118 ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
123 tmp_mac = (char *)g_try_malloc0(WIFI_MAC_ADD_LENGTH + 1);
124 if (tmp_mac == NULL) {
125 ERR("malloc() failed");
130 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
133 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
134 if (tmp_mac == NULL) {
135 ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
140 tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
148 *mac_address = g_strdup(mac);
153 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
155 gchar *mac_address = NULL;
156 gchar *g_name = NULL;
157 gboolean ret = FALSE;
159 ret = __get_mac_address(&mac_address);
160 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
161 ERR("Cannot get WIFI MAC address");
165 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
166 if (g_name == NULL) {
171 *group_name = g_strdup(g_name);
179 static gboolean __get_security_type(const gchar *config_id, gchar **type)
181 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
182 *type = g_strdup(WIFI_SECURITY_NONE);
183 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
184 *type = g_strdup(WIFI_SECURITY_WEP);
185 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
186 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
187 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
188 *type = g_strdup(WIFI_SECURITY_EAP);
197 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
199 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
200 if (*config_id == NULL) {
209 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
211 GKeyFile *keyfile = NULL;
214 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
216 keyfile = netconfig_keyfile_load(path);
217 if (keyfile == NULL) {
218 ERR("keyfile[%s] is NULL", path);
225 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
227 gboolean ret = FALSE;
230 path = g_strdup_printf("%s/%s", pathname, filename);
231 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
233 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
242 static gboolean __remove_configuration(const gchar *pathname)
246 if (__remove_file(pathname, "settings") != TRUE) {
247 ERR("Cannot remove [%s/settings]", pathname);
250 if (__remove_file(pathname, "data") != TRUE) {
251 ERR("Cannot remove [%s/data]", pathname);
255 ret = rmdir(pathname);
257 ERR("Cannot remove [%s]", pathname);
264 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
268 gboolean hidden = FALSE;
269 gboolean ret = FALSE;
271 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
273 ERR("Fail to get_wifi_config_group_name");
277 keyfile = __get_configuration_keyfile(group_name);
278 if (keyfile == NULL) {
279 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
284 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
285 ret = __get_security_type(config_id, &config->security_type);
287 ERR("Fail to _get_security_type");
288 g_key_file_free(keyfile);
292 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
293 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
295 config->is_hidden = g_strdup("TRUE");
297 config->is_hidden = g_strdup("FALSE");
299 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
300 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
301 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
302 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
303 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
304 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
305 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
306 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
307 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
310 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
312 g_key_file_free(keyfile);
318 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
323 gboolean ret = FALSE;
325 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
327 ERR("Fail to get_wifi_config_group_name");
331 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
332 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
333 if (__remove_configuration(dir) != TRUE) {
334 ERR("[%s] is existed, but cannot remove", dir);
341 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
342 ERR("Cannot mkdir %s", dir);
348 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
349 netconfig_keyfile_save(keyfile, path);
357 static gboolean _remove_configuration(const gchar *config_id)
359 gboolean ret = FALSE;
363 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
365 ERR("Fail to get_wifi_config_group_name");
369 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
370 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
371 if (__remove_configuration(dir) != TRUE) {
372 ERR("[%s] is existed, but cannot remove", dir);
375 INFO("Success to remove [%s]", dir);
378 ERR("[%s] is not existed", dir);
389 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
395 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
397 ERR("Fail to get_wifi_config_group_name");
400 DBG("group_name %s", group_name);
402 keyfile = __get_configuration_keyfile(group_name);
403 if (keyfile == NULL) {
404 ERR("Fail to __get_configuration_keyfile");
408 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
409 g_key_file_set_string(keyfile, group_name, key, value);
410 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
411 g_key_file_set_string(keyfile, group_name, key, value);
412 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
413 gboolean hidden = FALSE;
414 if (g_strcmp0(value, "TRUE") == 0)
416 g_key_file_set_boolean(keyfile, group_name, key, hidden);
417 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
418 g_key_file_set_string(keyfile, group_name, key, value);
419 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
420 g_key_file_set_string(keyfile, group_name, key, value);
421 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
422 g_key_file_set_string(keyfile, group_name, key, value);
423 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
424 g_key_file_set_string(keyfile, group_name, key, value);
425 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
426 g_key_file_set_string(keyfile, group_name, key, value);
427 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
428 g_key_file_set_string(keyfile, group_name, key, value);
429 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
430 g_key_file_set_string(keyfile, group_name, key, value);
431 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
432 g_key_file_set_string(keyfile, group_name, key, value);
434 ERR("key[%s] is not supported", key);
438 _save_configuration(config_id, keyfile);
440 g_key_file_free(keyfile);
446 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
451 gboolean hidden = FALSE;
452 gboolean ret = FALSE;
454 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
456 ERR("Fail to get_wifi_config_group_name");
459 DBG("group_name %s", group_name);
461 keyfile = __get_configuration_keyfile(group_name);
462 if (keyfile == NULL) {
463 ERR("Fail to __get_configuration_keyfile");
467 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
468 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
469 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
470 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
471 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
472 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
473 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
474 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
476 val = g_strdup("TRUE");
478 val = g_strdup("FALSE");
479 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
480 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
481 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
482 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
483 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
484 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
485 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
486 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
487 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
488 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
489 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
490 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
491 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
492 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
493 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
494 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
495 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
496 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
498 ERR("Invalid key[%s]", key);
499 val = g_strdup("NOTSUPPORTED");
502 *value = g_strdup(val);
505 g_key_file_free(keyfile);
511 static GSList *_get_list(void)
514 struct dirent ent_struct;
515 struct dirent *dp = NULL;
518 dir = opendir(CONNMAN_STORAGE);
520 ERR("Cannot open dir %s", CONNMAN_STORAGE);
524 while ((readdir_r(dir, &ent_struct, &dp) == 0) && dp) {
525 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
526 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
529 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
530 list = g_slist_append(list, g_strdup(config_id));
538 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
540 gboolean ret = FALSE;
543 if ((service_profile == NULL) || (config_id == NULL)) {
544 ERR("Invalid parameter");
548 ret = __get_config_id(service_profile, &val);
549 *config_id = g_strdup(val);
555 gboolean wifi_config_remove_configuration(const gchar *config_id)
557 gboolean ret = FALSE;
559 ret = _remove_configuration(config_id);
565 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
568 GSList *config_ids = NULL;
570 gchar **result = NULL;
572 g_return_val_if_fail(wifi != NULL, FALSE);
574 config_ids = _get_list();
575 if (config_ids == NULL) {
576 netconfig_error_no_profile(context);
577 ERR("Fail to get config list");
581 length = g_slist_length(config_ids);
582 result = g_new0(gchar *, length + 1);
583 for (i = 0; i < length; i++) {
584 gchar *config_id = g_slist_nth_data(config_ids, i);
585 result[i] = g_strdup(config_id);
588 config_ids = g_slist_nth(config_ids, 0);
589 g_slist_free_full(config_ids, g_free);
591 wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
599 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
600 const gchar *config_id)
602 gboolean ret = FALSE;
603 GVariantBuilder *b = NULL;
604 struct wifi_config *conf = NULL;
606 g_return_val_if_fail(wifi != NULL, FALSE);
608 conf = g_new0(struct wifi_config, 1);
610 ret = _load_configuration(config_id, conf);
613 ERR("Fail to _load_configuration");
614 netconfig_error_no_profile(context);
618 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
619 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
620 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
621 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
622 if (conf->proxy_address != NULL) {
623 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
624 g_free(conf->proxy_address);
626 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
628 if (conf->last_error != NULL) {
629 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
630 g_free(conf->last_error);
632 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
636 g_free(conf->security_type);
637 g_free(conf->is_hidden);
640 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
641 g_variant_builder_unref(b);
645 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
646 const gchar *config_id, GVariant *configuration)
648 gboolean ret = FALSE;
649 struct wifi_config *conf = NULL;
650 GKeyFile *keyfile = NULL;
654 gchar *group_name = NULL;
656 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
657 ERR("Invalid parameter");
658 netconfig_error_invalid_parameter(context);
659 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
663 ERR("save_configuration [%s]", config_id);
665 conf = g_new0(struct wifi_config, 1);
667 g_variant_get(configuration, "a{sv}", &iter);
668 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
669 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
670 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
671 conf->name = g_strdup(g_variant_get_string(value, NULL));
672 ERR("name [%s]", conf->name);
676 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
677 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
678 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
679 ERR("ssid [%s]", conf->ssid);
683 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
684 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
685 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
686 ERR("passphrase []");
688 conf->passphrase = NULL;
690 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
691 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
692 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
693 ERR("is_hidden [%s]", conf->is_hidden);
695 conf->is_hidden = NULL;
697 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
698 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
699 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
700 ERR("proxy_address [%s]", conf->proxy_address);
702 conf->proxy_address = NULL;
706 conf->favorite = TRUE;
707 conf->autoconnect = TRUE;
709 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
711 ERR("Fail to get_wifi_config_group_name");
715 keyfile = g_key_file_new();
716 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
717 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
719 if (conf->passphrase != NULL)
720 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
722 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
723 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
726 if (conf->proxy_address != NULL) {
727 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
728 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
731 if (conf->is_hidden != NULL) {
732 gboolean hidden = FALSE;
733 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
735 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
738 ret = _save_configuration(config_id, keyfile);
740 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Succeed");
741 wifi_complete_save_configuration(wifi, context);
743 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
744 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
747 g_key_file_free(keyfile);
750 g_free(conf->passphrase);
751 g_free(conf->is_hidden);
752 g_free(conf->proxy_address);
755 g_variant_iter_free(iter);
760 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
761 const gchar *config_id)
763 gboolean ret = FALSE;
764 GVariantBuilder *b = NULL;
765 struct wifi_config *conf = NULL;
767 g_return_val_if_fail(wifi != NULL, FALSE);
769 conf = g_new0(struct wifi_config, 1);
770 conf->eap_config = g_new0(struct wifi_eap_config, 1);
772 ret = _load_configuration(config_id, conf);
774 g_free(conf->eap_config);
776 ERR("Fail to _load_configuration");
777 netconfig_error_no_profile(context);
781 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
782 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
783 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
784 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
785 if (conf->proxy_address != NULL) {
786 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
787 g_free(conf->proxy_address);
789 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
791 if (conf->last_error != NULL) {
792 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
793 g_free(conf->last_error);
795 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
797 if (conf->eap_config != NULL) {
798 if (conf->eap_config->anonymous_identity != NULL)
799 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
801 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
803 if (conf->eap_config->ca_cert != NULL)
804 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
806 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
808 if (conf->eap_config->client_cert != NULL)
809 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
811 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
813 if (conf->eap_config->private_key != NULL)
814 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
816 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
818 if (conf->eap_config->identity != NULL)
819 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
821 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
823 if (conf->eap_config->eap_type != NULL)
824 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
826 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
828 if (conf->eap_config->eap_auth_type != NULL)
829 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
831 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
833 if (conf->eap_config->subject_match != NULL)
834 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
836 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
839 __free_wifi_configuration(conf);
841 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
842 g_variant_builder_unref(b);
846 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
847 const gchar *config_id, GVariant *configuration)
849 gboolean ret = FALSE;
850 struct wifi_config *conf = NULL;
851 GKeyFile *keyfile = NULL;
855 gchar *group_name = NULL;
857 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
858 ERR("Invalid parameter");
859 netconfig_error_invalid_parameter(context);
860 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
864 INFO("save [%s]", config_id);
866 conf = g_new0(struct wifi_config, 1);
867 conf->eap_config = g_new0(struct wifi_eap_config, 1);
869 g_variant_get(configuration, "a{sv}", &iter);
870 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
871 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
872 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
873 conf->name = g_strdup(g_variant_get_string(value, NULL));
874 ERR("name [%s]", conf->name);
878 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
879 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
880 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
881 ERR("ssid [%s]", conf->ssid);
885 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
886 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
887 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
888 ERR("passphrase [%s]", conf->passphrase);
890 conf->passphrase = NULL;
892 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
893 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
894 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
895 ERR("is_hidden [%s]", conf->is_hidden);
897 conf->is_hidden = NULL;
899 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
900 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
901 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
902 ERR("proxy_address [%s]", conf->proxy_address);
904 conf->proxy_address = NULL;
906 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
907 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
908 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
909 ERR("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
911 conf->eap_config->anonymous_identity = NULL;
913 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
914 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
915 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
916 ERR("ca_cert [%s]", conf->eap_config->ca_cert);
918 conf->eap_config->ca_cert = NULL;
920 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
921 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
922 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
923 ERR("client_cert [%s]", conf->eap_config->client_cert);
925 conf->eap_config->client_cert = NULL;
927 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
928 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
929 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
930 ERR("private_key [%s]", conf->eap_config->private_key);
932 conf->eap_config->private_key = NULL;
934 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
935 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
936 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
937 ERR("identity [%s]", conf->eap_config->identity);
939 conf->eap_config->identity = NULL;
941 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
942 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
943 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
944 ERR("eap_type [%s]", conf->eap_config->eap_type);
946 conf->eap_config->eap_type = NULL;
948 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
949 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
950 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
951 ERR("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
953 conf->eap_config->eap_auth_type = NULL;
955 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
956 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
957 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
958 ERR("subject_match [%s]", conf->eap_config->subject_match);
960 conf->eap_config->subject_match = NULL;
964 conf->favorite = TRUE;
965 conf->autoconnect = TRUE;
967 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
969 __free_wifi_configuration(conf);
970 ERR("Fail to get_wifi_config_group_name");
974 keyfile = g_key_file_new();
975 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
976 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
978 if (conf->passphrase != NULL)
979 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
981 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
982 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
985 if (conf->proxy_address != NULL) {
986 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
987 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
990 if (conf->is_hidden != NULL) {
991 gboolean hidden = FALSE;
992 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
994 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
997 ret = _save_configuration(config_id, keyfile);
999 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Succeed");
1000 wifi_complete_save_eap_configuration(wifi, context);
1002 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
1003 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1006 g_key_file_free(keyfile);
1007 __free_wifi_configuration(conf);
1009 g_variant_iter_free(iter);
1014 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1016 gboolean ret = FALSE;
1018 if ((wifi == NULL) || (config_id == NULL)) {
1019 ERR("Invalid parameter");
1020 netconfig_error_invalid_parameter(context);
1024 ret = _remove_configuration(config_id);
1026 /* no configuration or error */
1027 ERR("No [%s] configuration", config_id);
1028 netconfig_error_no_profile(context);
1032 wifi_complete_remove_configuration(wifi, context);
1036 /* config field key / value */
1038 * [wifi_macaddress_config_id]
1039 * Name=name (mandatory)
1040 * SSID=SSID (mandatory)
1041 * Frequency=2462 (X)
1043 * AutoConnect=true (Default true)
1044 * Modified=2015-03-20 (X)
1045 * IPv4.method=manual (O)
1046 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1047 * IPv6.method=auto (X)
1048 * IPv6.privacy=disabled (X)
1049 * IPv4.netmask_prefixlen=24 (X)
1050 * IPv4.local_address=192.0.0.1 (O)
1051 * IPv4.gateway=192.0.0.1 (O ? X ?)
1052 * Nameservers=192.168.43.22; (O)
1053 * Proxy.Method=manual (O)
1054 * Proxy.Servers=trst.com:8888; (O)
1056 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1057 const gchar *config_id, const gchar *key, const gchar *value)
1059 gboolean ret = FALSE;
1060 gchar *keyfile_key = NULL;
1062 g_return_val_if_fail(wifi != NULL, FALSE);
1063 g_return_val_if_fail(config_id != NULL, FALSE);
1064 g_return_val_if_fail(key != NULL, FALSE);
1066 DBG("Key[%s] Value[%d]", key, value);
1068 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1069 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1071 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1072 netconfig_error_invalid_parameter(context);
1075 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1076 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1077 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1078 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1079 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1080 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1081 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1082 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1083 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1084 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1085 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1086 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1087 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1088 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1089 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1090 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1091 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1092 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1093 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1095 ERR("Not supported key[%s]", key);
1096 netconfig_error_invalid_parameter(context);
1100 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1102 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1106 if (keyfile_key != NULL)
1107 g_free(keyfile_key);
1109 wifi_complete_set_config_field(wifi, context);
1113 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1115 gboolean ret = FALSE;
1116 gchar *passphrase = NULL;
1118 if ((wifi == NULL) || (config_id == NULL)) {
1119 ERR("Invalid parameter");
1120 netconfig_error_invalid_parameter(context);
1124 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1126 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1127 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1131 wifi_complete_get_config_passphrase(wifi, context, passphrase);