pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone

Blame SOURCES/0072-Enable-last-token-deletion-when-password-auth-type-i.patch

e3ffab
From 0aeae1d16bedd0f39f788644167675a99d30c3b2 Mon Sep 17 00:00:00 2001
e3ffab
From: Nathaniel McCallum <npmccallum@redhat.com>
e3ffab
Date: Tue, 11 Nov 2014 12:05:23 -0500
e3ffab
Subject: [PATCH] Enable last token deletion when password auth type is
e3ffab
 configured
e3ffab
e3ffab
Also, ensure that the last token check only executes on DNs/entries that
e3ffab
are tokens. This resolves a large performance issue where a query was
e3ffab
being performed to load all the user's tokens on every del/mod operation.
e3ffab
e3ffab
https://fedorahosted.org/freeipa/ticket/4697
e3ffab
https://fedorahosted.org/freeipa/ticket/4719
e3ffab
e3ffab
Reviewed-By: Thierry Bordaz <tbordaz@redhat.com>
e3ffab
---
e3ffab
 .../ipa-otp-lasttoken/ipa_otp_lasttoken.c          | 243 +++++++++++++++------
e3ffab
 1 file changed, 173 insertions(+), 70 deletions(-)
e3ffab
e3ffab
diff --git a/daemons/ipa-slapi-plugins/ipa-otp-lasttoken/ipa_otp_lasttoken.c b/daemons/ipa-slapi-plugins/ipa-otp-lasttoken/ipa_otp_lasttoken.c
e3ffab
index 19217ba7fbf562231dd74b25cfd13a0f4d930e7c..233813745795344f31a7dcf1931cf74a09f1e552 100644
e3ffab
--- a/daemons/ipa-slapi-plugins/ipa-otp-lasttoken/ipa_otp_lasttoken.c
e3ffab
+++ b/daemons/ipa-slapi-plugins/ipa-otp-lasttoken/ipa_otp_lasttoken.c
e3ffab
@@ -46,112 +46,171 @@
e3ffab
 
e3ffab
 #include "util.h"
e3ffab
 
e3ffab
-#define PLUGIN_NAME               "ipa-otp-lasttoken"
e3ffab
-
e3ffab
-static void *plugin_id;
e3ffab
-static const Slapi_PluginDesc preop_desc = {
e3ffab
-    PLUGIN_NAME,
e3ffab
-    "FreeIPA",
e3ffab
-    "FreeIPA/1.0",
e3ffab
-    "Protect the user's last active token"
e3ffab
-};
e3ffab
-
e3ffab
-static bool
e3ffab
-target_is_only_enabled_token(Slapi_PBlock *pb)
e3ffab
+#define PLUGIN_NAME "ipa-otp-lasttoken"
e3ffab
+#define OTP_CONTAINER "cn=otp,%s"
e3ffab
+
e3ffab
+static struct otp_config *otp_config;
e3ffab
+
e3ffab
+static bool entry_is_token(Slapi_Entry *entry)
e3ffab
 {
e3ffab
-    Slapi_DN *target_sdn = NULL;
e3ffab
-    Slapi_DN *token_sdn = NULL;
e3ffab
-    struct otp_token **tokens;
e3ffab
-    char *user_dn = NULL;
e3ffab
-    bool match;
e3ffab
+    char **ocls;
e3ffab
 
e3ffab
-    /* Ignore internal operations. */
e3ffab
-    if (slapi_op_internal(pb))
e3ffab
+    ocls = slapi_entry_attr_get_charray(entry, SLAPI_ATTR_OBJECTCLASS);
e3ffab
+    for (size_t i = 0; ocls != NULL && ocls[i] != NULL; i++) {
e3ffab
+        if (strcasecmp(ocls[i], "ipaToken") == 0) {
e3ffab
+            slapi_ch_array_free(ocls);
e3ffab
+            return true;
e3ffab
+        }
e3ffab
+    }
e3ffab
+
e3ffab
+    return false;
e3ffab
+}
e3ffab
+
e3ffab
+static bool sdn_in_otp_container(Slapi_DN *sdn)
e3ffab
+{
e3ffab
+    const Slapi_DN *base;
e3ffab
+    Slapi_DN *container;
e3ffab
+    bool result;
e3ffab
+    char *dn;
e3ffab
+
e3ffab
+    base = slapi_get_suffix_by_dn(sdn);
e3ffab
+    if (base == NULL)
e3ffab
         return false;
e3ffab
 
e3ffab
-    /* Get the current user's SDN. */
e3ffab
-    slapi_pblock_get(pb, SLAPI_CONN_DN, &user_dn);
e3ffab
-    if (user_dn == NULL)
e3ffab
+    dn = slapi_ch_smprintf(OTP_CONTAINER, slapi_sdn_get_dn(base));
e3ffab
+    if (dn == NULL)
e3ffab
         return false;
e3ffab
 
e3ffab
-    /* Get the SDN of the only enabled token. */
e3ffab
-    tokens = otp_token_find(plugin_id, user_dn, NULL, true, NULL);
e3ffab
-    if (tokens != NULL && tokens[0] != NULL && tokens[1] == NULL)
e3ffab
-        token_sdn = slapi_sdn_dup(otp_token_get_sdn(tokens[0]));
e3ffab
+    container = slapi_sdn_new_dn_passin(dn);
e3ffab
+    result = slapi_sdn_issuffix(sdn, container);
e3ffab
+    slapi_sdn_free(&container);
e3ffab
+
e3ffab
+    return result;
e3ffab
+}
e3ffab
+
e3ffab
+static bool sdn_is_only_enabled_token(Slapi_DN *target_sdn, const char *user_dn)
e3ffab
+{
e3ffab
+    struct otp_token **tokens;
e3ffab
+    bool result = false;
e3ffab
+
e3ffab
+    tokens = otp_token_find(otp_config, user_dn, NULL, true, NULL);
e3ffab
+
e3ffab
+    if (tokens != NULL && tokens[0] != NULL && tokens[1] == NULL) {
e3ffab
+        const Slapi_DN *token_sdn = otp_token_get_sdn(tokens[0]);
e3ffab
+        if (token_sdn != NULL)
e3ffab
+            result = slapi_sdn_compare(token_sdn, target_sdn) == 0;
e3ffab
+    }
e3ffab
+
e3ffab
     otp_token_free_array(tokens);
e3ffab
-    if (token_sdn == NULL)
e3ffab
+    return result;
e3ffab
+}
e3ffab
+
e3ffab
+static bool is_pwd_enabled(const char *user_dn)
e3ffab
+{
e3ffab
+    char *attrs[] = { "ipaUserAuthType", NULL };
e3ffab
+    Slapi_Entry *entry = NULL;
e3ffab
+    uint32_t authtypes;
e3ffab
+    Slapi_DN *sdn;
e3ffab
+
e3ffab
+    sdn = slapi_sdn_new_dn_byval(user_dn);
e3ffab
+    if (sdn == NULL)
e3ffab
         return false;
e3ffab
 
e3ffab
-    /* Get the target SDN. */
e3ffab
-    slapi_pblock_get(pb, SLAPI_TARGET_SDN, &target_sdn);
e3ffab
-    if (target_sdn == NULL) {
e3ffab
-        slapi_sdn_free(&token_sdn);
e3ffab
+    slapi_search_internal_get_entry(sdn, attrs, &entry,
e3ffab
+                                    otp_config_plugin_id(otp_config));
e3ffab
+    slapi_sdn_free(&sdn;;
e3ffab
+    if (entry == NULL)
e3ffab
+        return false;
e3ffab
+
e3ffab
+    authtypes = otp_config_auth_types(otp_config, entry);
e3ffab
+    slapi_entry_free(entry);
e3ffab
+
e3ffab
+    return authtypes & OTP_CONFIG_AUTH_TYPE_PASSWORD;
e3ffab
+}
e3ffab
+
e3ffab
+static bool is_allowed(Slapi_PBlock *pb, Slapi_Entry *entry)
e3ffab
+{
e3ffab
+    Slapi_DN *target_sdn = NULL;
e3ffab
+    const char *bind_dn;
e3ffab
+
e3ffab
+    /* Ignore internal operations. */
e3ffab
+    if (slapi_op_internal(pb))
e3ffab
+        return true;
e3ffab
+
e3ffab
+    /* Load parameters. */
e3ffab
+    (void) slapi_pblock_get(pb, SLAPI_TARGET_SDN, &target_sdn);
e3ffab
+    (void) slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn);
e3ffab
+    if (target_sdn == NULL || bind_dn == NULL) {
e3ffab
+        LOG_FATAL("Missing parameters!\n");
e3ffab
         return false;
e3ffab
     }
e3ffab
 
e3ffab
-    /* Does the target SDN match the only enabled token SDN? */
e3ffab
-    match = slapi_sdn_compare(token_sdn, target_sdn) == 0;
e3ffab
-    slapi_sdn_free(&token_sdn);
e3ffab
-    return match;
e3ffab
+    if (entry != NULL
e3ffab
+            ? !entry_is_token(entry)
e3ffab
+            : !sdn_in_otp_container(target_sdn))
e3ffab
+        return true;
e3ffab
+
e3ffab
+    if (!sdn_is_only_enabled_token(target_sdn, bind_dn))
e3ffab
+        return true;
e3ffab
+
e3ffab
+    if (is_pwd_enabled(bind_dn))
e3ffab
+        return true;
e3ffab
+
e3ffab
+    return false;
e3ffab
 }
e3ffab
 
e3ffab
-static inline int
e3ffab
-send_error(Slapi_PBlock *pb, int rc, char *errstr)
e3ffab
+static inline int send_error(Slapi_PBlock *pb, int rc, const char *errstr)
e3ffab
 {
e3ffab
-    slapi_send_ldap_result(pb, rc, NULL, errstr, 0, NULL);
e3ffab
+    slapi_send_ldap_result(pb, rc, NULL, (char *) errstr, 0, NULL);
e3ffab
     if (slapi_pblock_set(pb, SLAPI_RESULT_CODE, &rc)) {
e3ffab
         LOG_FATAL("slapi_pblock_set failed!\n");
e3ffab
     }
e3ffab
     return rc;
e3ffab
 }
e3ffab
 
e3ffab
-static int
e3ffab
-preop_del(Slapi_PBlock *pb)
e3ffab
+static int preop_del(Slapi_PBlock *pb)
e3ffab
 {
e3ffab
-    if (!target_is_only_enabled_token(pb))
e3ffab
+    if (is_allowed(pb, NULL))
e3ffab
         return 0;
e3ffab
 
e3ffab
     return send_error(pb, LDAP_UNWILLING_TO_PERFORM,
e3ffab
                       "Can't delete last active token");
e3ffab
 }
e3ffab
 
e3ffab
-static int
e3ffab
-preop_mod(Slapi_PBlock *pb)
e3ffab
+static int preop_mod(Slapi_PBlock *pb)
e3ffab
 {
e3ffab
-    LDAPMod **mods = NULL;
e3ffab
+    static const struct {
e3ffab
+        const char *attr;
e3ffab
+        const char *msg;
e3ffab
+    } errors[] = {
e3ffab
+        {"ipatokenDisabled",  "Can't disable last active token"},
e3ffab
+        {"ipatokenOwner",     "Can't change last active token's owner"},
e3ffab
+        {"ipatokenNotBefore", "Can't change last active token's start time"},
e3ffab
+        {"ipatokenNotAfter",  "Can't change last active token's end time"},
e3ffab
+        {}
e3ffab
+    };
e3ffab
 
e3ffab
-    if (!target_is_only_enabled_token(pb))
e3ffab
-        return 0;
e3ffab
+    const LDAPMod **mods = NULL;
e3ffab
+    Slapi_Entry *entry = NULL;
e3ffab
 
e3ffab
-    /* Do not permit deactivation of the last active token. */
e3ffab
-    slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods;;
e3ffab
-    for (int i = 0; mods != NULL && mods[i] != NULL; i++) {
e3ffab
-        if (strcasecmp(mods[i]->mod_type, "ipatokenDisabled") == 0) {
e3ffab
-            return send_error(pb, LDAP_UNWILLING_TO_PERFORM,
e3ffab
-                              "Can't disable last active token");
e3ffab
-        }
e3ffab
+    (void) slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &entry);
e3ffab
+    (void) slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods;;
e3ffab
 
e3ffab
-        if (strcasecmp(mods[i]->mod_type, "ipatokenOwner") == 0) {
e3ffab
-            return send_error(pb, LDAP_UNWILLING_TO_PERFORM,
e3ffab
-                              "Can't change last active token's owner");
e3ffab
-        }
e3ffab
-
e3ffab
-        if (strcasecmp(mods[i]->mod_type, "ipatokenNotBefore") == 0) {
e3ffab
-            return send_error(pb, LDAP_UNWILLING_TO_PERFORM,
e3ffab
-                              "Can't change last active token's start time");
e3ffab
-        }
e3ffab
+    if (is_allowed(pb, entry))
e3ffab
+        return 0;
e3ffab
 
e3ffab
-        if (strcasecmp(mods[i]->mod_type, "ipatokenNotAfter") == 0) {
e3ffab
-            return send_error(pb, LDAP_UNWILLING_TO_PERFORM,
e3ffab
-                              "Can't change last active token's end time");
e3ffab
+    /* If a protected attribute is modified, deny. */
e3ffab
+    for (int i = 0; mods != NULL && mods[i] != NULL; i++) {
e3ffab
+        for (int j = 0; errors[j].attr != NULL; j++) {
e3ffab
+            if (strcasecmp(mods[i]->mod_type, errors[j].attr) == 0)
e3ffab
+                return send_error(pb, LDAP_UNWILLING_TO_PERFORM, errors[j].msg);
e3ffab
         }
e3ffab
     }
e3ffab
 
e3ffab
     return 0;
e3ffab
 }
e3ffab
 
e3ffab
-static int
e3ffab
-preop_init(Slapi_PBlock *pb)
e3ffab
+static int preop_init(Slapi_PBlock *pb)
e3ffab
 {
e3ffab
     int ret = 0;
e3ffab
 
e3ffab
@@ -160,15 +219,59 @@ preop_init(Slapi_PBlock *pb)
e3ffab
     return ret;
e3ffab
 }
e3ffab
 
e3ffab
-int
e3ffab
-ipa_otp_lasttoken_init(Slapi_PBlock *pb)
e3ffab
+static int update_config(Slapi_PBlock *pb)
e3ffab
+{
e3ffab
+    otp_config_update(otp_config, pb);
e3ffab
+    return 0;
e3ffab
+}
e3ffab
+
e3ffab
+static int intpostop_init(Slapi_PBlock *pb)
e3ffab
+{
e3ffab
+    int ret = 0;
e3ffab
+
e3ffab
+    ret |= slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,    (void *) update_config);
e3ffab
+    ret |= slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN, (void *) update_config);
e3ffab
+    ret |= slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN, (void *) update_config);
e3ffab
+    ret |= slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN, (void *) update_config);
e3ffab
+
e3ffab
+    return ret;
e3ffab
+}
e3ffab
+
e3ffab
+static int postop_init(Slapi_PBlock *pb)
e3ffab
 {
e3ffab
     int ret = 0;
e3ffab
 
e3ffab
+    ret |= slapi_pblock_set(pb, SLAPI_PLUGIN_POST_ADD_FN,    (void *) update_config);
e3ffab
+    ret |= slapi_pblock_set(pb, SLAPI_PLUGIN_POST_DELETE_FN, (void *) update_config);
e3ffab
+    ret |= slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODIFY_FN, (void *) update_config);
e3ffab
+    ret |= slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODRDN_FN, (void *) update_config);
e3ffab
+
e3ffab
+    return ret;
e3ffab
+}
e3ffab
+
e3ffab
+int ipa_otp_lasttoken_init(Slapi_PBlock *pb)
e3ffab
+{
e3ffab
+    static const Slapi_PluginDesc preop_desc = {
e3ffab
+        PLUGIN_NAME,
e3ffab
+        "FreeIPA",
e3ffab
+        "FreeIPA/1.0",
e3ffab
+        "Protect the user's last active token"
e3ffab
+    };
e3ffab
+
e3ffab
+    Slapi_ComponentId *plugin_id = NULL;
e3ffab
+    int ret = 0;
e3ffab
+
e3ffab
     ret |= slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_id);
e3ffab
     ret |= slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_01);
e3ffab
     ret |= slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, (void *) &preop_desc);
e3ffab
     ret |= slapi_register_plugin("betxnpreoperation", 1, __func__, preop_init,
e3ffab
-                                 PLUGIN_NAME, NULL, plugin_id);
e3ffab
+                                 PLUGIN_NAME " betxnpreoperation", NULL, plugin_id);
e3ffab
+    ret |= slapi_register_plugin("postoperation", 1, __func__, postop_init,
e3ffab
+                                 PLUGIN_NAME " postoperation", NULL, plugin_id);
e3ffab
+    ret |= slapi_register_plugin("internalpostoperation", 1, __func__, intpostop_init,
e3ffab
+                                 PLUGIN_NAME " internalpostoperation", NULL, plugin_id);
e3ffab
+
e3ffab
+    /* NOTE: leak otp_config on process exit. */
e3ffab
+    otp_config = otp_config_init(plugin_id);
e3ffab
     return ret;
e3ffab
 }
e3ffab
-- 
e3ffab
2.1.0
e3ffab