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));
149 *mac_address = g_strdup(mac);
154 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
156 gchar *mac_address = NULL;
157 gchar *g_name = NULL;
158 gboolean ret = FALSE;
160 ret = __get_mac_address(&mac_address);
161 if ((ret != TRUE) || (strlen(mac_address) == 0)) {
162 ERR("Cannot get WIFI MAC address");
166 g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
167 if (g_name == NULL) {
172 *group_name = g_strdup(g_name);
180 static gboolean __get_security_type(const gchar *config_id, gchar **type)
182 if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
183 *type = g_strdup(WIFI_SECURITY_NONE);
184 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
185 *type = g_strdup(WIFI_SECURITY_WEP);
186 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
187 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
188 } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
189 *type = g_strdup(WIFI_SECURITY_EAP);
198 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
200 *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
201 if (*config_id == NULL) {
210 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
212 GKeyFile *keyfile = NULL;
215 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
217 keyfile = netconfig_keyfile_load(path);
218 if (keyfile == NULL) {
219 ERR("keyfile[%s] is NULL", path);
226 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
228 gboolean ret = FALSE;
231 path = g_strdup_printf("%s/%s", pathname, filename);
232 if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
234 } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
243 static gboolean __remove_configuration(const gchar *pathname)
247 if (__remove_file(pathname, "settings") != TRUE) {
248 ERR("Cannot remove [%s/settings]", pathname);
251 if (__remove_file(pathname, "data") != TRUE) {
252 ERR("Cannot remove [%s/data]", pathname);
256 ret = rmdir(pathname);
258 ERR("Cannot remove [%s]", pathname);
265 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
269 gboolean hidden = FALSE;
270 gboolean ret = FALSE;
272 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
274 ERR("Fail to get_wifi_config_group_name");
278 keyfile = __get_configuration_keyfile(group_name);
279 if (keyfile == NULL) {
280 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
285 config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
286 ret = __get_security_type(config_id, &config->security_type);
288 ERR("Fail to _get_security_type");
289 g_key_file_free(keyfile);
293 config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
294 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
296 config->is_hidden = g_strdup("TRUE");
298 config->is_hidden = g_strdup("FALSE");
301 if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
302 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
303 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
304 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
305 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
306 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
307 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
308 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
309 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
312 config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
314 g_key_file_free(keyfile);
320 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
325 gboolean ret = FALSE;
327 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
329 ERR("Fail to get_wifi_config_group_name");
333 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
334 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
335 if (__remove_configuration(dir) != TRUE) {
336 ERR("[%s] is existed, but cannot remove", dir);
343 if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
344 ERR("Cannot mkdir %s", dir);
350 path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
351 netconfig_keyfile_save(keyfile, path);
359 static gboolean _remove_configuration(const gchar *config_id)
361 gboolean ret = FALSE;
365 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
367 ERR("Fail to get_wifi_config_group_name");
371 dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
372 if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
373 if (__remove_configuration(dir) != TRUE) {
374 ERR("[%s] is existed, but cannot remove", dir);
377 INFO("Success to remove [%s]", dir);
380 ERR("[%s] is not existed", dir);
391 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
397 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
399 ERR("Fail to get_wifi_config_group_name");
402 DBG("group_name %s", group_name);
404 keyfile = __get_configuration_keyfile(group_name);
405 if (keyfile == NULL) {
406 ERR("Fail to __get_configuration_keyfile");
410 if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
411 g_key_file_set_string(keyfile, group_name, key, value);
412 }else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
413 g_key_file_set_string(keyfile, group_name, key, value);
414 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
415 gboolean hidden = FALSE;
416 if (g_strcmp0(value, "TRUE") == 0) {
419 g_key_file_set_boolean(keyfile, group_name, key, hidden);
420 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
421 g_key_file_set_string(keyfile, group_name, key, value);
422 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
423 g_key_file_set_string(keyfile, group_name, key, value);
424 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
425 g_key_file_set_string(keyfile, group_name, key, value);
426 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
427 g_key_file_set_string(keyfile, group_name, key, value);
428 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
429 g_key_file_set_string(keyfile, group_name, key, value);
430 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
431 g_key_file_set_string(keyfile, group_name, key, value);
432 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
433 g_key_file_set_string(keyfile, group_name, key, value);
434 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
435 g_key_file_set_string(keyfile, group_name, key, value);
437 ERR("key[%s] is not supported", key);
441 _save_configuration(config_id, keyfile);
443 g_key_file_free(keyfile);
449 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
454 gboolean hidden = FALSE;
455 gboolean ret = FALSE;
457 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
459 ERR("Fail to get_wifi_config_group_name");
462 DBG("group_name %s", group_name);
464 keyfile = __get_configuration_keyfile(group_name);
465 if (keyfile == NULL) {
466 ERR("Fail to __get_configuration_keyfile");
470 if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
471 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
472 } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
473 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
474 } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
475 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
476 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
477 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
479 val = g_strdup("TRUE");
481 val = g_strdup("FALSE");
483 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
484 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
485 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
486 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
487 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
488 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
489 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
490 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
491 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
492 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
493 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
494 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
495 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
496 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
497 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
498 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
499 } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
500 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
502 ERR("Invalid key[%s]", key);
503 val = g_strdup("NOTSUPPORTED");
506 *value = g_strdup(val);
509 g_key_file_free(keyfile);
515 static GSList *_get_list(void)
518 struct dirent ent_struct;
519 struct dirent *dp = NULL;
522 dir = opendir(CONNMAN_STORAGE);
524 ERR("Cannot open dir %s", CONNMAN_STORAGE);
528 while ((readdir_r(dir, &ent_struct, &dp) == 0) && dp) {
529 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
530 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
533 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
534 list = g_slist_append(list, g_strdup(config_id));
542 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
544 gboolean ret = FALSE;
547 if ((service_profile == NULL) || (config_id == NULL)) {
548 ERR("Invalid parameter");
552 ret = __get_config_id(service_profile, &val);
553 *config_id = g_strdup(val);
559 gboolean wifi_config_remove_configuration(const gchar *config_id)
561 gboolean ret = FALSE;
563 ret = _remove_configuration(config_id);
569 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
572 GSList *config_ids = NULL;
574 gchar **result = NULL;
576 g_return_val_if_fail(wifi != NULL, FALSE);
578 config_ids = _get_list();
579 if (config_ids == NULL) {
580 netconfig_error_no_profile(context);
581 ERR("Fail to get config list");
585 length = g_slist_length(config_ids);
586 result = g_new0(gchar *, length + 1);
587 for (i = 0; i < length; i++) {
588 gchar *config_id = g_slist_nth_data(config_ids, i);
589 result[i] = g_strdup(config_id);
592 config_ids = g_slist_nth(config_ids, 0);
593 g_slist_free_full(config_ids, g_free);
595 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) {
736 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
739 ret = _save_configuration(config_id, keyfile);
741 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Succeed");
742 wifi_complete_save_configuration(wifi, context);
744 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
745 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
748 g_key_file_free(keyfile);
751 g_free(conf->passphrase);
752 g_free(conf->is_hidden);
753 g_free(conf->proxy_address);
756 g_variant_iter_free(iter);
761 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
762 const gchar *config_id)
764 gboolean ret = FALSE;
765 GVariantBuilder *b = NULL;
766 struct wifi_config *conf = NULL;
768 g_return_val_if_fail(wifi != NULL, FALSE);
770 conf = g_new0(struct wifi_config, 1);
771 conf->eap_config = g_new0(struct wifi_eap_config, 1);
773 ret = _load_configuration(config_id, conf);
775 g_free(conf->eap_config);
777 ERR("Fail to _load_configuration");
778 netconfig_error_no_profile(context);
782 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
783 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
784 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
785 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
786 if (conf->proxy_address != NULL) {
787 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
788 g_free(conf->proxy_address);
790 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
792 if (conf->last_error != NULL) {
793 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
794 g_free(conf->last_error);
796 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
798 if (conf->eap_config != NULL) {
799 if (conf->eap_config->anonymous_identity != NULL) {
800 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
802 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
804 if (conf->eap_config->ca_cert != NULL) {
805 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
807 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
809 if (conf->eap_config->client_cert != NULL) {
810 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
812 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
814 if (conf->eap_config->private_key != NULL) {
815 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
817 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
819 if (conf->eap_config->identity != NULL) {
820 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
822 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
824 if (conf->eap_config->eap_type != NULL) {
825 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
827 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
829 if (conf->eap_config->eap_auth_type != NULL) {
830 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
832 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
834 if (conf->eap_config->subject_match != NULL) {
835 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
837 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
841 __free_wifi_configuration(conf);
843 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
844 g_variant_builder_unref(b);
848 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
849 const gchar *config_id, GVariant *configuration)
851 gboolean ret = FALSE;
852 struct wifi_config *conf = NULL;
853 GKeyFile *keyfile = NULL;
857 gchar *group_name = NULL;
859 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
860 ERR("Invalid parameter");
861 netconfig_error_invalid_parameter(context);
862 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
866 INFO("save [%s]", config_id);
868 conf = g_new0(struct wifi_config, 1);
869 conf->eap_config = g_new0(struct wifi_eap_config, 1);
871 g_variant_get(configuration, "a{sv}", &iter);
872 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
873 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
874 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
875 conf->name = g_strdup(g_variant_get_string(value, NULL));
876 ERR("name [%s]", conf->name);
880 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
881 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
882 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
883 ERR("ssid [%s]", conf->ssid);
887 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
888 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
889 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
890 ERR("passphrase [%s]", conf->passphrase);
892 conf->passphrase = NULL;
894 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
895 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
896 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
897 ERR("is_hidden [%s]", conf->is_hidden);
899 conf->is_hidden = NULL;
901 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
902 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
903 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
904 ERR("proxy_address [%s]", conf->proxy_address);
906 conf->proxy_address = NULL;
908 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
909 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
910 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
911 ERR("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
913 conf->eap_config->anonymous_identity = NULL;
915 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
916 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
917 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
918 ERR("ca_cert [%s]", conf->eap_config->ca_cert);
920 conf->eap_config->ca_cert = NULL;
922 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
923 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
924 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
925 ERR("client_cert [%s]", conf->eap_config->client_cert);
927 conf->eap_config->client_cert = NULL;
929 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
930 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
931 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
932 ERR("private_key [%s]", conf->eap_config->private_key);
934 conf->eap_config->private_key = NULL;
936 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
937 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
938 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
939 ERR("identity [%s]", conf->eap_config->identity);
941 conf->eap_config->identity = NULL;
943 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
944 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
945 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
946 ERR("eap_type [%s]", conf->eap_config->eap_type);
948 conf->eap_config->eap_type = NULL;
950 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
951 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
952 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
953 ERR("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
955 conf->eap_config->eap_auth_type = NULL;
957 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
958 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
959 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
960 ERR("subject_match [%s]", conf->eap_config->subject_match);
962 conf->eap_config->subject_match = NULL;
966 conf->favorite = TRUE;
967 conf->autoconnect = TRUE;
969 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
971 __free_wifi_configuration(conf);
972 ERR("Fail to get_wifi_config_group_name");
976 keyfile = g_key_file_new();
977 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
978 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
980 if (conf->passphrase != NULL)
981 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
983 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
984 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
987 if (conf->proxy_address != NULL) {
988 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
989 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
992 if (conf->is_hidden != NULL) {
993 gboolean hidden = FALSE;
994 if (g_strcmp0(conf->is_hidden, "TRUE") == 0) {
997 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1000 ret = _save_configuration(config_id, keyfile);
1002 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Succeed");
1003 wifi_complete_save_eap_configuration(wifi, context);
1005 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
1006 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1009 g_key_file_free(keyfile);
1010 __free_wifi_configuration(conf);
1012 g_variant_iter_free(iter);
1017 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1019 gboolean ret = FALSE;
1021 if ((wifi == NULL) || (config_id == NULL)) {
1022 ERR("Invalid parameter");
1023 netconfig_error_invalid_parameter(context);
1027 ret = _remove_configuration(config_id);
1029 // no configuration or error
1030 ERR("No [%s] configuration", config_id);
1031 netconfig_error_no_profile(context);
1035 wifi_complete_remove_configuration(wifi, context);
1039 // config field key / value
1041 * [wifi_macaddress_config_id]
1042 * Name=name (mandatory)
1043 * SSID=SSID (mandatory)
1044 * Frequency=2462 (X)
1046 * AutoConnect=true (Default true)
1047 * Modified=2015-03-20 (X)
1048 * IPv4.method=manual (O)
1049 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1050 * IPv6.method=auto (X)
1051 * IPv6.privacy=disabled (X)
1052 * IPv4.netmask_prefixlen=24 (X)
1053 * IPv4.local_address=192.0.0.1 (O)
1054 * IPv4.gateway=192.0.0.1 (O ? X ?)
1055 * Nameservers=192.168.43.22; (O)
1056 * Proxy.Method=manual (O)
1057 * Proxy.Servers=trst.com:8888; (O)
1059 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1060 const gchar *config_id, const gchar *key, const gchar *value)
1062 gboolean ret = FALSE;
1063 gchar *keyfile_key = NULL;
1065 g_return_val_if_fail(wifi != NULL, FALSE);
1066 g_return_val_if_fail(config_id != NULL, FALSE);
1067 g_return_val_if_fail(key != NULL, FALSE);
1069 DBG("Key[%s] Value[%d]", key, value);
1071 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1072 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1074 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1075 netconfig_error_invalid_parameter(context);
1078 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1079 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1080 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1081 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1082 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1083 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1084 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1085 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1086 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1087 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1088 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1089 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1090 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1091 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1092 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1093 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1094 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1095 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1096 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1098 ERR("Not supported key[%s]", key);
1099 netconfig_error_invalid_parameter(context);
1103 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1105 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1109 if (keyfile_key != NULL)
1110 g_free(keyfile_key);
1112 wifi_complete_set_config_field(wifi,context);
1116 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1118 gboolean ret = FALSE;
1119 gchar *passphrase = NULL;
1121 if ((wifi == NULL) || (config_id == NULL)) {
1122 ERR("Invalid parameter");
1123 netconfig_error_invalid_parameter(context);
1127 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1129 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1130 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1134 wifi_complete_get_config_passphrase(wifi, context, passphrase);