From 8404f0fe914a44dc843659b2320c03f1aae65036 Mon Sep 17 00:00:00 2001 From: Niraj Kumar Goit Date: Sun, 3 May 2020 00:08:41 +0530 Subject: [PATCH] [net-config] Added support to set eapol property in connman. Change-Id: I4ce867d457b60545137d8273a19defd2a22c3a37 Signed-off-by: Niraj Kumar Goit --- include/ethernet.h | 13 ++++ src/ethernet.c | 152 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 136 insertions(+), 29 deletions(-) diff --git a/include/ethernet.h b/include/ethernet.h index 22df62a..132edbe 100644 --- a/include/ethernet.h +++ b/include/ethernet.h @@ -30,6 +30,19 @@ extern "C" { #include "generated-code.h" +typedef struct { + gchar *eap_type; + gchar *identity; + gchar *anonymous_identity; + gchar *ca_cert_file; + gchar *client_cert_file; + gchar *private_key_file; + gchar *private_key_password; + gchar *pac_file; + gchar *phase2; + int phase1; +} netconfig_eapol_s; + void ethernet_object_create_and_init(void); void ethernet_object_deinit(void); Ethernet *get_ethernet_object(void); diff --git a/src/ethernet.c b/src/ethernet.c index 8c7fb52..81c6a3f 100644 --- a/src/ethernet.c +++ b/src/ethernet.c @@ -46,6 +46,7 @@ static Ethernet *ethernet_object = NULL; static gboolean g_eap_supported = FALSE; +netconfig_eapol_s eapol; Ethernet *get_ethernet_object(void) { @@ -60,6 +61,10 @@ static gboolean __netconfig_set_eap_config_file(GVariant *fields) FILE *fp = NULL; int err = 0; + /* initialize eap settings */ + memset(&eapol, 0, sizeof(netconfig_eapol_s)); + eapol.phase1 = -1; + /* create eapol conf file */ filename = g_strdup_printf("%s/%s-eapol.conf", EAPOL_STORAGEDIR, ETH_IFNAME); if (!filename) { @@ -84,18 +89,24 @@ static gboolean __netconfig_set_eap_config_file(GVariant *fields) if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_EAP_METHOD) == 0) { DBG("field: %s, value: %s", field, value); - if (value != NULL) + if (value != NULL) { fprintf (fp, "eap=%s\n", value); + eapol.eap_type = g_strdup(value); + } } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_IDENTITY) == 0) { DBG("field: %s, value: %s", field, value); - if (value != NULL) + if (value != NULL) { fprintf (fp, "identity=\"%s\"\n", value); + eapol.identity = g_strdup(value); + } } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_ANONYMOUS_IDENTITY) == 0) { DBG("field: %s, value: %s", field, value); - if (value != NULL) + if (value != NULL) { fprintf (fp, "anonymous_identity=\"%s\"\n", value); + eapol.anonymous_identity = g_strdup(value); + } } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_PASSPHRASE) == 0) { DBG("field: %s, value: %s", field, value); @@ -104,28 +115,38 @@ static gboolean __netconfig_set_eap_config_file(GVariant *fields) } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_CA_CERT_FILE) == 0) { DBG("field: %s, value: %s", field, value); - if (value != NULL) + if (value != NULL) { fprintf (fp, "ca_cert=\"%s\"\n", value); + eapol.ca_cert_file = g_strdup(value); + } } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_CLIENT_CERT_FILE) == 0) { DBG("field: %s, value: %s", field, value); - if (value != NULL) + if (value != NULL) { fprintf (fp, "client_cert=\"%s\"\n", value); + eapol.client_cert_file = g_strdup(value); + } } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_PVT_KEY_FILE) == 0) { DBG("field: %s, value: %s", field, value); - if (value != NULL) + if (value != NULL) { fprintf (fp, "private_key=\"%s\"\n", value); + eapol.private_key_file = g_strdup(value); + } } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_PVT_KEY_PASSPHRASE) == 0) { DBG("field: %s, value: %s", field, value); - if (value != NULL) + if (value != NULL) { fprintf (fp, "private_key_passwd=\"%s\"\n", value); + eapol.private_key_password = g_strdup(value); + } } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_PAC_FILE) == 0) { DBG("field: %s, value: %s", field, value); - if (value != NULL) + if (value != NULL) { fprintf (fp, "pac_file=\"%s\"\n", value); + eapol.pac_file = g_strdup(value); + } } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_PHASE2) == 0) { DBG("field: %s, value: %s", field, value); @@ -133,6 +154,8 @@ static gboolean __netconfig_set_eap_config_file(GVariant *fields) char str[50] = {0,}; sprintf(str, "auth=%s", value); fprintf (fp, "phase2=\"%s\"\n", str); + + eapol.phase2 = g_strdup(value); } } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_PHASE1) == 0) { DBG("field: %s, value: %s", field, value); @@ -145,8 +168,10 @@ static gboolean __netconfig_set_eap_config_file(GVariant *fields) peap_version = 0; else peap_version = 1; + sprintf(str, "peapver=%d", peap_version); fprintf (fp, "phase1=\"%s\"\n", str); + eapol.phase1 = peap_version; } } } @@ -161,6 +186,84 @@ out: return err; } +static void __netconfig_cleanup_eapol() +{ + g_free(eapol.eap_type); + g_free(eapol.identity); + g_free(eapol.anonymous_identity); + g_free(eapol.ca_cert_file); + g_free(eapol.client_cert_file); + g_free(eapol.private_key_file); + g_free(eapol.private_key_password); + g_free(eapol.pac_file); + g_free(eapol.phase2); + eapol.phase1 = -1; +} + +static int __netconfig_set_eapol_property(const gchar *service, gboolean use_eapol) +{ + GVariant *params = NULL; + GVariant *message = NULL; + GVariantBuilder *builder; + + const char *prop_eap_on_ethernet = "EapOverEthernet"; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + + g_variant_builder_add(builder, "{sv}", "UseEapol", g_variant_new_boolean(use_eapol)); + + if (use_eapol) { + if (eapol.eap_type) + g_variant_builder_add(builder, "{sv}", "EAP", + g_variant_new_string(eapol.eap_type)); + if (eapol.identity) + g_variant_builder_add(builder, "{sv}", "Identity", + g_variant_new_string(eapol.identity)); + if (eapol.anonymous_identity) + g_variant_builder_add(builder, "{sv}", "AnonymousIdentity", + g_variant_new_string(eapol.anonymous_identity)); + if (eapol.ca_cert_file) + g_variant_builder_add(builder, "{sv}", "CACertFile", + g_variant_new_string(eapol.ca_cert_file)); + if (eapol.client_cert_file) + g_variant_builder_add(builder, "{sv}", "ClientCertFile", + g_variant_new_string(eapol.client_cert_file)); + if (eapol.private_key_file) + g_variant_builder_add(builder, "{sv}", "PrivateKeyFile", + g_variant_new_string(eapol.private_key_file)); + if (eapol.private_key_password) + g_variant_builder_add(builder, "{sv}", "PrivateKeyPassphrase", + g_variant_new_string(eapol.private_key_password)); + if (eapol.phase2) + g_variant_builder_add(builder, "{sv}", "Phase2", + g_variant_new_string(eapol.phase2)); + + if (eapol.phase1 >= 0) + g_variant_builder_add(builder, "{sv}", "Phase1", + g_variant_new_int32(eapol.phase1)); + + if (eapol.pac_file) + g_variant_builder_add(builder, "{sv}", "PacFile", + g_variant_new_string(eapol.pac_file)); + + __netconfig_cleanup_eapol(); + } + + params = g_variant_new("(sv)", prop_eap_on_ethernet, g_variant_builder_end(builder)); + g_variant_builder_unref(builder); + + message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, service, + CONNMAN_SERVICE_INTERFACE, "SetProperty", params); + if (message == NULL) { + g_variant_unref(message); + return -1; + } + + g_variant_unref(message); + + return 0; +} + /********************* * Handler Functions * ********************/ @@ -186,15 +289,12 @@ gboolean handle_enable_eap(Ethernet *object, GDBusMethodInvocation *invocation, g_eap_supported = enable; if (enable == false) { - gboolean use_eapol = false; - GVariant *params = g_variant_new("(sv)", "UseEapol", g_variant_new_boolean(use_eapol)); - - GVariant *message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, service, - CONNMAN_SERVICE_INTERFACE, "SetProperty", params); - if (message == NULL) - ERR("Failed to reset EAPoL property"); - - g_variant_unref(message); + int err = __netconfig_set_eapol_property(service, false); + if (err < 0) { + ERR("Failed to set eapol property."); + netconfig_error_dbus_method_return(invocation, NETCONFIG_ERROR_INTERNAL, "InternalError"); + return TRUE; + } } ethernet_complete_enable_eap(object, invocation); @@ -219,10 +319,6 @@ gboolean handle_set_eap_config(Ethernet *object, GDBusMethodInvocation *invocati { g_return_val_if_fail(object != NULL, TRUE); - GVariant *message = NULL; - GVariant *params = NULL; - gboolean use_eapol = true; - DBG("handle_set_eap_config for service [%s]", service); if (netconfig_is_ethernet_profile(service) != TRUE) { @@ -245,14 +341,12 @@ gboolean handle_set_eap_config(Ethernet *object, GDBusMethodInvocation *invocati return TRUE; } - params = g_variant_new("(sv)", "UseEapol", g_variant_new_boolean(use_eapol)); - - message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, service, - CONNMAN_SERVICE_INTERFACE, "SetProperty", params); - if (message == NULL) - ERR("Failed to Set EAPoL property"); - - g_variant_unref(message); + err = __netconfig_set_eapol_property(service, true); + if (err < 0) { + ERR("Failed to set eapol property."); + netconfig_error_dbus_method_return(invocation, NETCONFIG_ERROR_INTERNAL, "InternalError"); + return TRUE; + } ethernet_complete_set_eap_config(object, invocation); return TRUE; -- 2.34.1