r19731: Modify the ldb_map infrustructure to always map from requested
[kamenim/samba.git] / source4 / dsdb / samdb / ldb_modules / samba3sam.c
index 06774780a154e054a8bd4309180798acd4d2c7c0..6c7c3c70662a7ceb966df01a447f24f2c4d58936 100644 (file)
@@ -5,11 +5,17 @@
 */
 
 #include "includes.h"
-#include "ldb/modules/ldb_map.h"
 #include "ldb/include/ldb.h"
 #include "ldb/include/ldb_private.h"
+#include "ldb/include/ldb_errors.h"
+#include "ldb/modules/ldb_map.h"
 #include "system/passwd.h"
 
+#include "librpc/gen_ndr/ndr_security.h"
+#include "librpc/ndr/libndr.h"
+#include "libcli/security/security.h"
+#include "libcli/security/proto.h"
+
 /* 
  * sambaSID -> member  (dn!)
  * sambaSIDList -> member (dn!) 
 /* In Samba4 but not in Samba3:
 */
 
-static struct ldb_message_element *generate_primaryGroupID(struct ldb_module *module, TALLOC_CTX *ctx, const char *attr, const struct ldb_message *remote)
+/* From a sambaPrimaryGroupSID, generate a primaryGroupID (integer) attribute */
+static struct ldb_message_element *generate_primaryGroupID(struct ldb_module *module, TALLOC_CTX *ctx, const char *local_attr, const struct ldb_message *remote)
 {
        struct ldb_message_element *el;
-       const char *sid = ldb_msg_find_string(remote, attr, NULL);
-
+       const char *sid = ldb_msg_find_attr_as_string(remote, "sambaPrimaryGroupSID", NULL);
+       const char *p;
+       
        if (!sid)
                return NULL;
 
-       if (strchr(sid, '-') == NULL)
+       p = strrchr(sid, '-');
+       if (!p)
                return NULL;
 
        el = talloc_zero(ctx, struct ldb_message_element);
        el->name = talloc_strdup(ctx, "primaryGroupID");
        el->num_values = 1;
        el->values = talloc_array(ctx, struct ldb_val, 1);
-       el->values[0].data = (uint8_t *)talloc_strdup(ctx, strchr(sid, '-')+1);
+       el->values[0].data = (uint8_t *)talloc_strdup(el->values, strrchr(sid, '-')+1);
        el->values[0].length = strlen((char *)el->values[0].data);
 
        return el;
@@ -74,6 +83,7 @@ static void generate_sambaPrimaryGroupSID(struct ldb_module *module, const char
        struct dom_sid *sid;
        NTSTATUS status;
 
+       /* We need the domain, so we get it from the objectSid that we hope is here... */
        sidval = ldb_msg_find_ldb_val(local, "objectSid");
 
        if (!sidval) 
@@ -96,7 +106,7 @@ static void generate_sambaPrimaryGroupSID(struct ldb_module *module, const char
 
        sidstring = dom_sid_string(remote_mp, sid);
        talloc_free(sid);
-       ldb_msg_add_fmt(remote_mp, "sambaPrimaryGroupSID", "%s-%d", sidstring, ldb_msg_find_uint(local, "primaryGroupID", 0));
+       ldb_msg_add_fmt(remote_mp, "sambaPrimaryGroupSID", "%s-%d", sidstring, ldb_msg_find_attr_as_uint(local, "primaryGroupID", 0));
        talloc_free(sidstring);
 }
 
@@ -855,8 +865,33 @@ const struct ldb_map_attribute samba3_attributes[] =
        }
 };
 
+/* the context init function */
+static int samba3sam_init(struct ldb_module *module)
+{
+        int ret;
+
+       ret = ldb_map_init(module, samba3_attributes, samba3_objectclasses, NULL, "samba3sam");
+        if (ret != LDB_SUCCESS)
+                return ret;
+
+        return ldb_next_init(module);
+}
+
+static struct ldb_module_ops samba3sam_ops = {
+       .name              = "samba3sam",
+       .init_context      = samba3sam_init,
+};
+
 /* the init function */
 int ldb_samba3sam_module_init(void)
 {
-       return ldb_map_init(ldb, samba3_attributes, samba3_objectclasses, "samba3sam");
+       struct ldb_module_ops ops = ldb_map_get_ops();
+       samba3sam_ops.add       = ops.add;
+       samba3sam_ops.modify    = ops.modify;
+       samba3sam_ops.del       = ops.del;
+       samba3sam_ops.rename    = ops.rename;
+       samba3sam_ops.search    = ops.search;
+       samba3sam_ops.wait      = ops.wait;
+
+       return ldb_register_module(&samba3sam_ops);
 }