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)
491 dir = opendir(CONNMAN_STORAGE);
493 ERR("Cannot open dir %s", CONNMAN_STORAGE);
497 while ((d = readdir(dir))) {
498 if (g_strcmp0(d->d_name, ".") == 0 || g_strcmp0(d->d_name, "..") == 0 ||
499 strncmp(d->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
502 gchar *config_id = g_strdup(d->d_name + WIFI_PREFIX_LENGTH);
503 list = g_slist_append(list, g_strdup(config_id));
511 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
513 gboolean ret = FALSE;
516 if ((service_profile == NULL) || (config_id == NULL)) {
517 ERR("Invalid parameter");
521 ret = __get_config_id(service_profile, &val);
522 *config_id = g_strdup(val);
528 gboolean wifi_config_remove_configuration(const gchar *config_id)
530 gboolean ret = FALSE;
532 ret = _remove_configuration(config_id);
538 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
541 GSList *config_ids = NULL;
543 gchar **result = NULL;
545 g_return_val_if_fail(wifi != NULL, FALSE);
547 config_ids = _get_list();
548 if (config_ids == NULL) {
549 netconfig_error_no_profile(context);
550 ERR("Fail to get config list");
554 length = g_slist_length(config_ids);
555 result = g_new0(gchar *, length + 1);
556 for (i = 0; i < length; i++) {
557 gchar *config_id = g_slist_nth_data(config_ids, i);
558 result[i] = g_strdup(config_id);
561 config_ids = g_slist_nth(config_ids, 0);
562 g_slist_free_full(config_ids, g_free);
564 wifi_complete_get_config_ids(wifi, context, (const gchar * const*)result);
568 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
569 const gchar *config_id)
571 gboolean ret = FALSE;
572 GVariantBuilder *b = NULL;
573 struct wifi_config *conf = NULL;
575 g_return_val_if_fail(wifi != NULL, FALSE);
577 conf = g_new0(struct wifi_config, 1);
579 ret = _load_configuration(config_id, conf);
582 ERR("Fail to _load_configuration");
583 netconfig_error_no_profile(context);
587 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
588 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
589 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
590 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
591 if (conf->proxy_address != NULL) {
592 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
593 g_free(conf->proxy_address);
595 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
597 if (conf->last_error != NULL) {
598 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
599 g_free(conf->last_error);
601 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
605 g_free(conf->security_type);
606 g_free(conf->is_hidden);
609 wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
610 g_variant_builder_unref(b);
614 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
615 const gchar *config_id, GVariant *configuration)
617 gboolean ret = FALSE;
618 struct wifi_config *conf = NULL;
619 GKeyFile *keyfile = NULL;
623 gchar *group_name = NULL;
625 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
626 ERR("Invalid parameter");
627 netconfig_error_invalid_parameter(context);
628 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
632 ERR("save_configuration [%s]", config_id);
634 conf = g_new0(struct wifi_config, 1);
636 g_variant_get(configuration, "a{sv}", &iter);
637 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
638 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
639 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
640 conf->name = g_strdup(g_variant_get_string(value, NULL));
641 ERR("name [%s]", conf->name);
645 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
646 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
647 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
648 ERR("ssid [%s]", conf->ssid);
652 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
653 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
654 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
655 ERR("passphrase []");
657 conf->passphrase = NULL;
659 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
660 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
661 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
662 ERR("is_hidden [%s]", conf->is_hidden);
664 conf->is_hidden = NULL;
666 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
667 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
668 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
669 ERR("proxy_address [%s]", conf->proxy_address);
671 conf->proxy_address = NULL;
675 conf->favorite = TRUE;
676 conf->autoconnect = TRUE;
678 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
680 ERR("Fail to get_wifi_config_group_name");
684 keyfile = g_key_file_new();
685 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
686 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
688 if (conf->passphrase != NULL)
689 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
691 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
692 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
695 if (conf->proxy_address != NULL) {
696 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
697 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
700 if (conf->is_hidden != NULL) {
701 gboolean hidden = FALSE;
702 if (g_strcmp0(conf->is_hidden, "TRUE") == 0) {
705 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
708 ret = _save_configuration(config_id, keyfile);
710 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Succeed");
711 wifi_complete_save_configuration(wifi, context);
713 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
714 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
717 g_key_file_free(keyfile);
720 g_free(conf->passphrase);
721 g_free(conf->is_hidden);
722 g_free(conf->proxy_address);
725 g_variant_iter_free(iter);
730 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
731 const gchar *config_id)
733 gboolean ret = FALSE;
734 GVariantBuilder *b = NULL;
735 struct wifi_config *conf = NULL;
737 g_return_val_if_fail(wifi != NULL, FALSE);
739 conf = g_new0(struct wifi_config, 1);
740 conf->eap_config = g_new0(struct wifi_eap_config, 1);
742 ret = _load_configuration(config_id, conf);
744 g_free(conf->eap_config);
746 ERR("Fail to _load_configuration");
747 netconfig_error_no_profile(context);
751 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
752 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
753 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
754 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
755 if (conf->proxy_address != NULL) {
756 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
757 g_free(conf->proxy_address);
759 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
761 if (conf->last_error != NULL) {
762 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
763 g_free(conf->last_error);
765 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
767 if (conf->eap_config != NULL) {
768 if (conf->eap_config->anonymous_identity != NULL) {
769 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
771 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
773 if (conf->eap_config->ca_cert != NULL) {
774 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
776 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
778 if (conf->eap_config->client_cert != NULL) {
779 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
781 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
783 if (conf->eap_config->private_key != NULL) {
784 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
786 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
788 if (conf->eap_config->identity != NULL) {
789 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
791 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
793 if (conf->eap_config->eap_type != NULL) {
794 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
796 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
798 if (conf->eap_config->eap_auth_type != NULL) {
799 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
801 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
803 if (conf->eap_config->subject_match != NULL) {
804 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
806 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
810 __free_wifi_configuration(conf);
812 wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
813 g_variant_builder_unref(b);
817 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
818 const gchar *config_id, GVariant *configuration)
820 gboolean ret = FALSE;
821 struct wifi_config *conf = NULL;
822 GKeyFile *keyfile = NULL;
826 gchar *group_name = NULL;
828 if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
829 ERR("Invalid parameter");
830 netconfig_error_invalid_parameter(context);
831 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
835 INFO("save [%s]", config_id);
837 conf = g_new0(struct wifi_config, 1);
838 conf->eap_config = g_new0(struct wifi_eap_config, 1);
840 g_variant_get(configuration, "a{sv}", &iter);
841 while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
842 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
843 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
844 conf->name = g_strdup(g_variant_get_string(value, NULL));
845 ERR("name [%s]", conf->name);
849 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
850 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
851 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
852 ERR("ssid [%s]", conf->ssid);
856 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
857 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
858 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
859 ERR("passphrase [%s]", conf->passphrase);
861 conf->passphrase = NULL;
863 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
864 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
865 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
866 ERR("is_hidden [%s]", conf->is_hidden);
868 conf->is_hidden = NULL;
870 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
871 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
872 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
873 ERR("proxy_address [%s]", conf->proxy_address);
875 conf->proxy_address = NULL;
877 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
878 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
879 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
880 ERR("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
882 conf->eap_config->anonymous_identity = NULL;
884 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
885 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
886 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
887 ERR("ca_cert [%s]", conf->eap_config->ca_cert);
889 conf->eap_config->ca_cert = NULL;
891 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
892 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
893 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
894 ERR("client_cert [%s]", conf->eap_config->client_cert);
896 conf->eap_config->client_cert = NULL;
898 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
899 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
900 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
901 ERR("private_key [%s]", conf->eap_config->private_key);
903 conf->eap_config->private_key = NULL;
905 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
906 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
907 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
908 ERR("identity [%s]", conf->eap_config->identity);
910 conf->eap_config->identity = NULL;
912 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
913 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
914 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
915 ERR("eap_type [%s]", conf->eap_config->eap_type);
917 conf->eap_config->eap_type = NULL;
919 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
920 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
921 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
922 ERR("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
924 conf->eap_config->eap_auth_type = NULL;
926 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
927 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
928 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
929 ERR("subject_match [%s]", conf->eap_config->subject_match);
931 conf->eap_config->subject_match = NULL;
935 conf->favorite = TRUE;
936 conf->autoconnect = TRUE;
938 ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
940 __free_wifi_configuration(conf);
941 ERR("Fail to get_wifi_config_group_name");
945 keyfile = g_key_file_new();
946 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
947 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
949 if (conf->passphrase != NULL)
950 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
952 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
953 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
956 if (conf->proxy_address != NULL) {
957 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
958 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
961 if (conf->is_hidden != NULL) {
962 gboolean hidden = FALSE;
963 if (g_strcmp0(conf->is_hidden, "TRUE") == 0) {
966 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
969 ret = _save_configuration(config_id, keyfile);
971 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Succeed");
972 wifi_complete_save_eap_configuration(wifi, context);
974 SLOG(LOG_INFO, "MDM_LOG_USER", "Object=wifi-profile, AccessType=Create, Result=Failed");
975 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
978 g_key_file_free(keyfile);
979 __free_wifi_configuration(conf);
981 g_variant_iter_free(iter);
986 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
988 gboolean ret = FALSE;
990 if ((wifi == NULL) || (config_id == NULL)) {
991 ERR("Invalid parameter");
992 netconfig_error_invalid_parameter(context);
996 ret = _remove_configuration(config_id);
998 // no configuration or error
999 ERR("No [%s] configuration", config_id);
1000 netconfig_error_no_profile(context);
1004 wifi_complete_remove_configuration(wifi, context);
1008 // config field key / value
1010 * [wifi_macaddress_config_id]
1011 * Name=name (mandatory)
1012 * SSID=SSID (mandatory)
1013 * Frequency=2462 (X)
1015 * AutoConnect=true (Default true)
1016 * Modified=2015-03-20 (X)
1017 * IPv4.method=manual (O)
1018 * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1019 * IPv6.method=auto (X)
1020 * IPv6.privacy=disabled (X)
1021 * IPv4.netmask_prefixlen=24 (X)
1022 * IPv4.local_address=192.0.0.1 (O)
1023 * IPv4.gateway=192.0.0.1 (O ? X ?)
1024 * Nameservers=192.168.43.22; (O)
1025 * Proxy.Method=manual (O)
1026 * Proxy.Servers=trst.com:8888; (O)
1028 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1029 const gchar *config_id, const gchar *key, const gchar *value)
1031 gboolean ret = FALSE;
1032 gchar *keyfile_key = NULL;
1034 g_return_val_if_fail(wifi != NULL, FALSE);
1035 g_return_val_if_fail(config_id != NULL, FALSE);
1036 g_return_val_if_fail(key != NULL, FALSE);
1038 DBG("Key[%s] Value[%d]", key, value);
1040 if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1041 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1043 ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1044 netconfig_error_invalid_parameter(context);
1047 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1048 } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1049 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1050 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1051 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1052 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1053 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1054 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1055 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1056 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1057 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1058 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1059 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1060 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1061 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1062 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1063 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1064 } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1065 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1067 ERR("Not supported key[%s]", key);
1068 netconfig_error_invalid_parameter(context);
1072 ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1074 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1078 if (keyfile_key != NULL)
1079 g_free(keyfile_key);
1081 wifi_complete_set_config_field(wifi,context);
1085 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1087 gboolean ret = FALSE;
1088 gchar *passphrase = NULL;
1090 if ((wifi == NULL) || (config_id == NULL)) {
1091 ERR("Invalid parameter");
1092 netconfig_error_invalid_parameter(context);
1096 ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1098 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1099 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1103 wifi_complete_get_config_passphrase(wifi, context, passphrase);