s4:dsdb - Fixed attribute dereferencing for FDS
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / extended_dn_out.c
index f8526faf3bde3f0b461332a1fa5019fd59e83b64..6fbd0559db7f67236c45c8fc409fea14f0b8fd7e 100644 (file)
@@ -34,7 +34,7 @@
 #include "includes.h"
 #include "ldb/include/ldb.h"
 #include "ldb/include/ldb_errors.h"
-#include "ldb/include/ldb_private.h"
+#include "ldb/include/ldb_module.h"
 #include "librpc/gen_ndr/ndr_misc.h"
 #include "librpc/ndr/libndr.h"
 #include "dsdb/samdb/samdb.h"
@@ -172,7 +172,7 @@ static int inject_extended_dn_out(struct ldb_reply *ares,
        return LDB_SUCCESS;
 }
 
-static int handle_dereference(struct ldb_dn *dn,
+static int handle_dereference_openldap(struct ldb_dn *dn,
                              struct dsdb_openldap_dereference_result **dereference_attrs, 
                              const char *attr, const DATA_BLOB *val)
 {
@@ -228,6 +228,63 @@ static int handle_dereference(struct ldb_dn *dn,
        return LDB_SUCCESS;
 }
 
+static int handle_dereference_fds(struct ldb_dn *dn,
+                             struct dsdb_openldap_dereference_result **dereference_attrs, 
+                             const char *attr, const DATA_BLOB *val)
+{
+       const struct ldb_val *nsUniqueIdBlob, *sidBlob;
+       struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
+       int j;
+       
+       fake_msg.num_elements = 0;
+                       
+       /* Look for this attribute in the returned control */
+       for (j = 0; dereference_attrs && dereference_attrs[j]; j++) {
+               struct ldb_val source_dn = data_blob_string_const(dereference_attrs[j]->dereferenced_dn);
+               if (ldb_attr_cmp(dereference_attrs[j]->source_attribute, attr) == 0
+                   && data_blob_cmp(&source_dn, val) == 0) {
+                       fake_msg.num_elements = dereference_attrs[j]->num_attributes;
+                       fake_msg.elements = dereference_attrs[j]->attributes;
+                       break;
+               }
+       }
+       if (!fake_msg.num_elements) {
+               return LDB_SUCCESS;
+       }
+
+       /* Look for the nsUniqueId */
+       
+       nsUniqueIdBlob = ldb_msg_find_ldb_val(&fake_msg, "nsUniqueId");
+       if (nsUniqueIdBlob) {
+               NTSTATUS status;
+               enum ndr_err_code ndr_err;
+               
+               struct ldb_val guid_blob;
+               struct GUID guid;
+               
+               status = NS_GUID_from_string((char *)nsUniqueIdBlob->data, &guid);
+               
+               if (!NT_STATUS_IS_OK(status)) {
+                       return LDB_ERR_INVALID_DN_SYNTAX;
+               }
+               ndr_err = ndr_push_struct_blob(&guid_blob, NULL, NULL, &guid,
+                                               (ndr_push_flags_fn_t)ndr_push_GUID);
+               if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                       return LDB_ERR_INVALID_DN_SYNTAX;
+               }
+               
+               ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
+       }
+       
+       /* Look for the objectSID */
+
+       sidBlob = ldb_msg_find_ldb_val(&fake_msg, "objectSID");
+       if (sidBlob) {
+               ldb_dn_set_extended_component(dn, "SID", sidBlob);
+       }
+       return LDB_SUCCESS;
+}
+
 /* search */
 struct extended_search_context {
        struct ldb_module *module;
@@ -239,7 +296,10 @@ struct extended_search_context {
        int extended_type;
 };
 
-static int extended_callback(struct ldb_request *req, struct ldb_reply *ares)
+static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
+               int (*handle_dereference)(struct ldb_dn *dn,
+                               struct dsdb_openldap_dereference_result **dereference_attrs, 
+                               const char *attr, const DATA_BLOB *val))
 {
        struct extended_search_context *ac;
        struct ldb_control *control;
@@ -249,7 +309,7 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares)
        struct extended_dn_out_private *p;
 
        ac = talloc_get_type(req->context, struct extended_search_context);
-       p = talloc_get_type(ac->module->private_data, struct extended_dn_out_private);
+       p = talloc_get_type(ldb_module_get_private(ac->module), struct extended_dn_out_private);
 
        if (!ares) {
                return ldb_module_done(ac->req, NULL, NULL,
@@ -281,7 +341,7 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares)
        if (ac->inject) {
                /* for each record returned post-process to add any derived
                   attributes that have been asked for */
-               ret = inject_extended_dn_out(ares, ac->module->ldb,
+               ret = inject_extended_dn_out(ares, ldb_module_get_ctx(ac->module),
                                             ac->extended_type, ac->remove_guid,
                                             ac->remove_sid);
                if (ret != LDB_SUCCESS) {
@@ -301,7 +361,7 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares)
                                                         ldb_dn_get_linearized(ares->message->dn));
                        }
                        if (ret != LDB_SUCCESS) {
-                               ldb_oom(ac->module->ldb);
+                               ldb_oom(ldb_module_get_ctx(ac->module));
                                return LDB_ERR_OPERATIONS_ERROR;
                        }
                }
@@ -329,7 +389,7 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares)
                         * correct case */
                        msg->elements[i].name = talloc_strdup(msg->elements, attribute->lDAPDisplayName);
                        if (!msg->elements[i].name) {
-                               ldb_oom(ac->module->ldb);
+                               ldb_oom(ldb_module_get_ctx(ac->module));
                                return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
                        }
                }
@@ -340,13 +400,14 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares)
                }
 
                /* Look to see if this attributeSyntax is a DN */
-               if (strcmp(attribute->attributeSyntax_oid, "2.5.5.1") != 0) {
+               if (strcmp(attribute->attributeSyntax_oid, "2.5.5.1") != 0 &&
+                   strcmp(attribute->attributeSyntax_oid, "2.5.5.7") != 0) {
                        continue;
                }
 
                for (j = 0; j < msg->elements[i].num_values; j++) {
                        const char *dn_str;
-                       struct ldb_dn *dn = ldb_dn_from_ldb_val(ac, ac->module->ldb, &msg->elements[i].values[j]);
+                       struct ldb_dn *dn = ldb_dn_from_ldb_val(ac, ldb_module_get_ctx(ac->module), &msg->elements[i].values[j]);
                        if (!dn || !ldb_dn_validate(dn)) {
                                return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
                        }
@@ -385,7 +446,7 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares)
                                                                                     dn, ac->extended_type));
                        }
                        if (!dn_str) {
-                               ldb_oom(ac->module->ldb);
+                               ldb_oom(ldb_module_get_ctx(ac->module));
                                return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
                        }
                        msg->elements[i].values[j] = data_blob_string_const(dn_str);
@@ -395,8 +456,18 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares)
        return ldb_module_send_entry(ac->req, msg, ares->controls);
 }
 
+static int extended_callback_openldap(struct ldb_request *req, struct ldb_reply *ares)
+{
+       return extended_callback(req, ares, handle_dereference_openldap);
+}
 
-static int extended_dn_out_search(struct ldb_module *module, struct ldb_request *req)
+static int extended_callback_fds(struct ldb_request *req, struct ldb_reply *ares)
+{
+       return extended_callback(req, ares, handle_dereference_fds);
+}
+
+static int extended_dn_out_search(struct ldb_module *module, struct ldb_request *req,
+               int (*callback)(struct ldb_request *req, struct ldb_reply *ares))
 {
        struct ldb_control *control;
        struct ldb_control *storage_format_control;
@@ -408,7 +479,7 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
        const char * const *const_attrs;
        int ret;
 
-       struct extended_dn_out_private *p = talloc_get_type(module->private_data, struct extended_dn_out_private);
+       struct extended_dn_out_private *p = talloc_get_type(ldb_module_get_private(module), struct extended_dn_out_private);
 
        /* check if there's an extended dn control */
        control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
@@ -426,19 +497,19 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
        if (!control && storage_format_control && storage_format_control->data) {
                extended_ctrl = talloc_get_type(storage_format_control->data, struct ldb_extended_dn_control);
                if (!extended_ctrl) {
-                       ldb_set_errstring(module->ldb, "extended_dn_out: extended_ctrl was of the wrong data type");
+                       ldb_set_errstring(ldb_module_get_ctx(module), "extended_dn_out: extended_ctrl was of the wrong data type");
                        return LDB_ERR_PROTOCOL_ERROR;
                }
        }
 
        ac = talloc_zero(req, struct extended_search_context);
        if (ac == NULL) {
-               ldb_oom(module->ldb);
+               ldb_oom(ldb_module_get_ctx(module));
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        ac->module = module;
-       ac->schema = dsdb_get_schema(module->ldb);
+       ac->schema = dsdb_get_schema(ldb_module_get_ctx(module));
        ac->req = req;
        ac->inject = false;
        ac->remove_guid = false;
@@ -469,7 +540,7 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
                        if (ac->remove_guid || ac->remove_sid) {
                                new_attrs = copy_attrs(ac, req->op.search.attrs);
                                if (new_attrs == NULL) {
-                                       ldb_oom(module->ldb);
+                                       ldb_oom(ldb_module_get_ctx(module));
                                        return LDB_ERR_OPERATIONS_ERROR;
                                }
 
@@ -487,13 +558,13 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
        }
 
        ret = ldb_build_search_req_ex(&down_req,
-                                     module->ldb, ac,
+                                     ldb_module_get_ctx(module), ac,
                                      req->op.search.base,
                                      req->op.search.scope,
                                      req->op.search.tree,
                                      const_attrs,
                                      req->controls,
-                                     ac, extended_callback,
+                                     ac, callback,
                                      req);
        if (ret != LDB_SUCCESS) {
                return ret;
@@ -535,16 +606,31 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
        return ldb_next_request(module, down_req);
 }
 
+static int extended_dn_out_ldb_search(struct ldb_module *module, struct ldb_request *req)
+{
+       return extended_dn_out_search(module, req, extended_callback_openldap);
+}
+
+static int extended_dn_out_openldap_search(struct ldb_module *module, struct ldb_request *req)
+{
+       return extended_dn_out_search(module, req, extended_callback_openldap);
+}
+
+static int extended_dn_out_fds_search(struct ldb_module *module, struct ldb_request *req)
+{
+       return extended_dn_out_search(module, req, extended_callback_fds);
+}
+
 static int extended_dn_out_ldb_init(struct ldb_module *module)
 {
        int ret;
 
        struct extended_dn_out_private *p = talloc(module, struct extended_dn_out_private);
 
-       module->private_data = p;
+       ldb_module_set_private(module, p);
 
        if (!p) {
-               ldb_oom(module->ldb);
+               ldb_oom(ldb_module_get_ctx(module));
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -553,7 +639,7 @@ static int extended_dn_out_ldb_init(struct ldb_module *module)
 
        ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
        if (ret != LDB_SUCCESS) {
-               ldb_debug(module->ldb, LDB_DEBUG_ERROR,
+               ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
                        "extended_dn_out: Unable to register control with rootdse!\n");
                return LDB_ERR_OPERATIONS_ERROR;
        }
@@ -561,19 +647,19 @@ static int extended_dn_out_ldb_init(struct ldb_module *module)
        return ldb_next_init(module);
 }
 
-static int extended_dn_out_dereference_init(struct ldb_module *module)
+static int extended_dn_out_dereference_init(struct ldb_module *module, const char *attrs[])
 {
        int ret, i = 0;
-       struct extended_dn_out_private *p;
+       struct extended_dn_out_private *p = talloc_zero(module, struct extended_dn_out_private);
        struct dsdb_openldap_dereference_control *dereference_control;
        struct dsdb_attribute *cur;
 
        struct dsdb_schema *schema;
 
-       module->private_data = p = talloc_zero(module, struct extended_dn_out_private);
+       ldb_module_set_private(module, p);
 
        if (!p) {
-               ldb_oom(module->ldb);
+               ldb_oom(ldb_module_get_ctx(module));
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -585,7 +671,7 @@ static int extended_dn_out_dereference_init(struct ldb_module *module)
 
        ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
        if (ret != LDB_SUCCESS) {
-               ldb_debug(module->ldb, LDB_DEBUG_ERROR,
+               ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
                        "extended_dn_out: Unable to register control with rootdse!\n");
                return LDB_ERR_OPERATIONS_ERROR;
        }
@@ -596,7 +682,7 @@ static int extended_dn_out_dereference_init(struct ldb_module *module)
                return ret;
        }
 
-       schema = dsdb_get_schema(module->ldb);
+       schema = dsdb_get_schema(ldb_module_get_ctx(module));
        if (!schema) {
                /* No schema on this DB (yet) */
                return LDB_SUCCESS;
@@ -606,31 +692,26 @@ static int extended_dn_out_dereference_init(struct ldb_module *module)
                = talloc_zero(p, struct dsdb_openldap_dereference_control);
 
        if (!p->dereference_control) {
-               ldb_oom(module->ldb);
+               ldb_oom(ldb_module_get_ctx(module));
                return LDB_ERR_OPERATIONS_ERROR;
        }
        
        for (cur = schema->attributes; cur; cur = cur->next) {
-               static const char *attrs[] = {
-                       "entryUUID",
-                       "objectSID",
-                       NULL
-               };
-
-               if (strcmp(cur->syntax->attributeSyntax_oid, "2.5.5.1") != 0) {
+               if (strcmp(cur->syntax->attributeSyntax_oid, "2.5.5.1") != 0 &&
+                   strcmp(cur->syntax->attributeSyntax_oid, "2.5.5.7") != 0) {
                        continue;
                }
                dereference_control->dereference
                        = talloc_realloc(p, dereference_control->dereference,
                                         struct dsdb_openldap_dereference *, i + 2);
                if (!dereference_control) {
-                       ldb_oom(module->ldb);
+                       ldb_oom(ldb_module_get_ctx(module));
                        return LDB_ERR_OPERATIONS_ERROR;
                }
                dereference_control->dereference[i] = talloc(dereference_control->dereference,  
                                         struct dsdb_openldap_dereference);
                if (!dereference_control->dereference[i]) {
-                       ldb_oom(module->ldb);
+                       ldb_oom(ldb_module_get_ctx(module));
                        return LDB_ERR_OPERATIONS_ERROR;
                }
                dereference_control->dereference[i]->source_attribute = cur->lDAPDisplayName;
@@ -641,15 +722,42 @@ static int extended_dn_out_dereference_init(struct ldb_module *module)
        return LDB_SUCCESS;
 }
 
+static int extended_dn_out_openldap_init(struct ldb_module *module)
+{
+       static const char *attrs[] = {
+               "entryUUID",
+               "objectSID",
+               NULL
+       };
+
+       return extended_dn_out_dereference_init(module, attrs);
+}
+
+static int extended_dn_out_fds_init(struct ldb_module *module)
+{
+       static const char *attrs[] = {
+               "nsUniqueId",
+               "objectSID",
+               NULL
+       };
+
+       return extended_dn_out_dereference_init(module, attrs);
+}
+
 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
        .name              = "extended_dn_out_ldb",
-       .search            = extended_dn_out_search,
+       .search            = extended_dn_out_ldb_search,
        .init_context      = extended_dn_out_ldb_init,
 };
 
+_PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_openldap_module_ops = {
+       .name              = "extended_dn_out_openldap",
+       .search            = extended_dn_out_openldap_search,
+       .init_context      = extended_dn_out_openldap_init,
+};
 
-_PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_dereference_module_ops = {
-       .name              = "extended_dn_out_dereference",
-       .search            = extended_dn_out_search,
-       .init_context      = extended_dn_out_dereference_init,
+_PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_fds_module_ops = {
+       .name              = "extended_dn_out_fds",
+       .search            = extended_dn_out_fds_search,
+       .init_context      = extended_dn_out_fds_init,
 };