From 088d64756406f56b2c9cae72a2e1baed2b03722a Mon Sep 17 00:00:00 2001
From: Rich Megginson <rmeggins@redhat.com>
Date: Tue, 4 Sep 2012 08:25:59 -0600
Subject: [PATCH] Ticket #426 - support posix schema for user and group sync

https://fedorahosted.org/389/ticket/426
Resolves: Ticket #426
Bug Description: support posix schema for user and group sync
Reviewed by: ???
Branch: master
Fix Description:
1) plugin config ldif must contain pluginid, etc. during upgrade or it
   will fail due to schema errors
2) posix winsync should have a lower precedence (25) than the default (50)
   so that it will be run first
3) posix winsync should support the Winsync API v3 - the v2 functions are
   just stubs for now - but the precedence cb is active
Platforms tested: RHEL6 x86_64
Flag Day: no
Doc impact: no
---
 ldap/ldif/50posix-winsync-plugin.ldif              |    5 +
 ldap/servers/plugins/posix-winsync/posix-winsync.c |  252 +++++++++++++++++++-
 2 files changed, 255 insertions(+), 2 deletions(-)

diff --git a/ldap/ldif/50posix-winsync-plugin.ldif b/ldap/ldif/50posix-winsync-plugin.ldif
index a9c7908..de8c432 100644
--- a/ldap/ldif/50posix-winsync-plugin.ldif
+++ b/ldap/ldif/50posix-winsync-plugin.ldif
@@ -13,3 +13,8 @@ posixWinsyncMapMemberUID: true
 posixWinsyncCreateMemberOfTask: false
 posixWinsyncLowerCaseUID: false
 nsslapd-pluginprecedence: 25
+# these will be replaced when the server loads the plugin
+nsslapd-pluginId: ID
+nsslapd-pluginVersion: PACKAGE_VERSION
+nsslapd-pluginVendor: VENDOR
+nsslapd-pluginDescription: DESC
diff --git a/ldap/servers/plugins/posix-winsync/posix-winsync.c b/ldap/servers/plugins/posix-winsync/posix-winsync.c
index 6bdac20..398541d 100644
--- a/ldap/servers/plugins/posix-winsync/posix-winsync.c
+++ b/ldap/servers/plugins/posix-winsync/posix-winsync.c
@@ -1333,6 +1333,232 @@ posix_winsync_destroy_agmt_cb(void *cbdata, const Slapi_DN *ds_subtree, const Sl
     return;
 }
 
+static void
+posix_winsync_post_ad_mod_user_cb(void *cookie, const Slapi_Entry *rawentry, Slapi_Entry *ad_entry, Slapi_Entry *ds_entry, Slapi_Mods *smods, int *result)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_post_ad_mod_user_cb -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Result of modifying AD entry [%s] was [%d:%s]\n",
+                    slapi_entry_get_dn(ad_entry), *result, ldap_err2string(*result));
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_post_ad_mod_user_cb -- end\n");
+
+    return;
+}
+
+static void
+posix_winsync_post_ad_mod_group_cb(void *cookie, const Slapi_Entry *rawentry, Slapi_Entry *ad_entry, Slapi_Entry *ds_entry, Slapi_Mods *smods, int *result)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_post_ad_mod_group_cb -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Result of modifying AD entry [%s] was [%d:%s]\n",
+                    slapi_entry_get_dn(ad_entry), *result, ldap_err2string(*result));
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_post_ad_mod_group_cb -- end\n");
+
+    return;
+}
+
+static void
+posix_winsync_post_ds_mod_user_cb(void *cookie, const Slapi_Entry *rawentry, Slapi_Entry *ad_entry, Slapi_Entry *ds_entry, Slapi_Mods *smods, int *result)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_post_ds_mod_user_cb -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Result of modifying DS entry [%s] was [%d:%s]\n",
+                    slapi_entry_get_dn(ds_entry), *result, ldap_err2string(*result));
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_post_ds_mod_user_cb -- end\n");
+
+    return;
+}
+
+static void
+posix_winsync_post_ds_mod_group_cb(void *cookie, const Slapi_Entry *rawentry, Slapi_Entry *ad_entry, Slapi_Entry *ds_entry, Slapi_Mods *smods, int *result)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_post_ds_mod_group_cb -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Result of modifying DS entry [%s] was [%d:%s]\n",
+                    slapi_entry_get_dn(ds_entry), *result, ldap_err2string(*result));
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_post_ds_mod_group_cb -- end\n");
+
+    return;
+}
+
+static void
+posix_winsync_post_ds_add_user_cb(void *cookie, const Slapi_Entry *rawentry, Slapi_Entry *ad_entry, Slapi_Entry *ds_entry, int *result)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_post_ds_add_user_cb -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Result of adding DS entry [%s] was [%d:%s]\n",
+                    slapi_entry_get_dn(ds_entry), *result, ldap_err2string(*result));
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_post_ds_add_user_cb -- end\n");
+
+    return;
+}
+
+static void
+posix_winsync_post_ds_add_group_cb(void *cookie, const Slapi_Entry *rawentry, Slapi_Entry *ad_entry, Slapi_Entry *ds_entry, int *result)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_post_ds_add_group_cb -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Result of adding DS entry [%s] was [%d:%s]\n",
+                    slapi_entry_get_dn(ds_entry), *result, ldap_err2string(*result));
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_post_ds_add_group_cb -- end\n");
+
+    return;
+}
+
+static void
+posix_winsync_pre_ad_add_user_cb(void *cookie, Slapi_Entry *ds_entry, Slapi_Entry *ad_entry)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_pre_ad_add_user_cb -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Adding AD entry [%s] from add of DS entry [%s]\n",
+                    slapi_entry_get_dn(ad_entry), slapi_entry_get_dn(ds_entry));
+    /* make modifications to ad_entry here */
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_pre_ad_add_user_cb -- end\n");
+
+    return;
+}
+
+static void
+posix_winsync_pre_ad_add_group_cb(void *cookie, Slapi_Entry *ds_entry, Slapi_Entry *ad_entry)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_pre_ad_add_group_cb -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Adding AD entry [%s] from add of DS entry [%s]\n",
+                    slapi_entry_get_dn(ad_entry), slapi_entry_get_dn(ds_entry));
+    /* make modifications to ad_entry here */
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_pre_ad_add_group_cb -- end\n");
+
+    return;
+}
+
+static void
+posix_winsync_post_ad_add_user_cb(void *cookie, Slapi_Entry *ds_entry, Slapi_Entry *ad_entry, int *result)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_post_ad_add_user_cb -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Result of adding AD entry [%s] was [%d:%s]\n",
+                    slapi_entry_get_dn(ad_entry), *result, ldap_err2string(*result));
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_post_ad_add_user_cb -- end\n");
+
+    return;
+}
+
+static void
+posix_winsync_post_ad_add_group_cb(void *cookie, Slapi_Entry *ds_entry, Slapi_Entry *ad_entry, int *result)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_post_ad_add_group_cb -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Result of adding AD entry [%s] was [%d:%s]\n",
+                    slapi_entry_get_dn(ad_entry), *result, ldap_err2string(*result));
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_post_ad_add_group_cb -- end\n");
+
+    return;
+}
+
+static void
+posix_winsync_post_ad_mod_user_mods_cb(void *cookie, const Slapi_Entry *rawentry, const Slapi_DN *local_dn, const Slapi_Entry *ds_entry, LDAPMod * const *origmods, Slapi_DN *remote_dn, LDAPMod ***modstosend, int *result)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_post_ad_mod_user_mods_cb  -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Result of modifying AD entry [%s] was [%d:%s]\n",
+                    slapi_sdn_get_dn(remote_dn), *result, ldap_err2string(*result));
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_post_ad_mod_user_mods_cb -- end\n");
+
+    return;
+}
+
+static void
+posix_winsync_post_ad_mod_group_mods_cb(void *cookie, const Slapi_Entry *rawentry, const Slapi_DN *local_dn, const Slapi_Entry *ds_entry, LDAPMod * const *origmods, Slapi_DN *remote_dn, LDAPMod ***modstosend, int *result)
+{
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "--> posix_winsync_post_ad_mod_group_mods_cb  -- begin\n");
+
+#ifdef THIS_IS_JUST_AN_EXAMPLE
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "Result of modifying AD entry [%s] was [%d:%s]\n",
+                    slapi_sdn_get_dn(remote_dn), *result, ldap_err2string(*result));
+#endif
+
+    slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                    "<-- posix_winsync_post_ad_mod_group_mods_cb -- end\n");
+
+    return;
+}
+
+#define DEFAULT_PRECEDENCE 25
+static int precedence = DEFAULT_PRECEDENCE; /* default */
+
+static int posix_winsync_precedence(void)
+{
+    return precedence;
+}
+
 static void *posix_winsync_api[] = { NULL, /* reserved for api broker use, must be zero */
                                      posix_winsync_agmt_init,
                                      posix_winsync_dirsync_search_params_cb,
@@ -1352,7 +1578,21 @@ static void *posix_winsync_api[] = { NULL, /* reserved for api broker use, must
                                      posix_winsync_can_add_entry_to_ad_cb,
                                      posix_winsync_begin_update_cb,
                                      posix_winsync_end_update_cb,
-                                     posix_winsync_destroy_agmt_cb };
+                                     posix_winsync_destroy_agmt_cb,
+                                     posix_winsync_post_ad_mod_user_cb,
+                                     posix_winsync_post_ad_mod_group_cb,
+                                     posix_winsync_post_ds_mod_user_cb,
+                                     posix_winsync_post_ds_mod_group_cb,
+                                     posix_winsync_post_ds_add_user_cb,
+                                     posix_winsync_post_ds_add_group_cb,
+                                     posix_winsync_pre_ad_add_user_cb,
+                                     posix_winsync_pre_ad_add_group_cb,
+                                     posix_winsync_post_ad_add_user_cb,
+                                     posix_winsync_post_ad_add_group_cb,
+                                     posix_winsync_post_ad_mod_user_mods_cb,
+                                     posix_winsync_post_ad_mod_group_mods_cb,
+                                     posix_winsync_precedence
+};
 
 static Slapi_ComponentId *posix_winsync_plugin_id = NULL;
 
@@ -1381,7 +1621,7 @@ posix_winsync_plugin_start(Slapi_PBlock *pb)
     slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
                     "--> posix_winsync_plugin_start -- begin\n");
 
-    if (slapi_apib_register(WINSYNC_v1_0_GUID, posix_winsync_api)) {
+    if (slapi_apib_register(WINSYNC_v3_0_GUID, posix_winsync_api)) {
         slapi_log_error(SLAPI_LOG_FATAL, posix_winsync_plugin_name,
                         "<-- posix_winsync_plugin_start -- failed to register winsync api -- end\n");
         return -1;
@@ -1422,10 +1662,18 @@ int
 posix_winsync_plugin_init(Slapi_PBlock *pb)
 {
     void *plugin_id = NULL;
+    Slapi_Entry *confige = NULL;
 
     slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
                     "--> posix_winsync_plugin_init -- begin\n");
 
+    if (slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &confige) && confige) {
+        precedence = slapi_entry_attr_get_int(confige, "nsslapd-pluginprecedence");
+        if (!precedence) {
+            precedence = DEFAULT_PRECEDENCE;
+        }
+    }
+
     if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_01) != 0
         || slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN, (void *) posix_winsync_plugin_start) != 0
         || slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN, (void *) posix_winsync_plugin_close) != 0
-- 
1.7.1

