s4-dsdb: it is a better pattern to mark a control as done than remove it
[samba.git] / source4 / dsdb / samdb / ldb_modules / extended_dn_out.c
index bf70c3e80f1d30dee66a2a33ac74383d5d718fe5..2d0ee6a4aa22a4c8075720624e5d441ecc6d61d8 100644 (file)
@@ -202,8 +202,6 @@ static int handle_dereference_openldap(struct ldb_dn *dn,
        entryUUIDblob = ldb_msg_find_ldb_val(&fake_msg, "entryUUID");
        if (entryUUIDblob) {
                NTSTATUS status;
-               enum ndr_err_code ndr_err;
-               
                struct ldb_val guid_blob;
                struct GUID guid;
                
@@ -212,9 +210,8 @@ static int handle_dereference_openldap(struct ldb_dn *dn,
                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)) {
+               status = GUID_to_ndr_blob(&guid, dn, &guid_blob);
+               if (!NT_STATUS_IS_OK(status)) {
                        return LDB_ERR_INVALID_DN_SYNTAX;
                }
                
@@ -259,8 +256,6 @@ static int handle_dereference_fds(struct ldb_dn *dn,
        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;
                
@@ -269,9 +264,8 @@ static int handle_dereference_fds(struct ldb_dn *dn,
                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)) {
+               status = GUID_to_ndr_blob(&guid, dn, &guid_blob);
+               if (!NT_STATUS_IS_OK(status)) {
                        return LDB_ERR_INVALID_DN_SYNTAX;
                }
                
@@ -328,6 +322,8 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
        struct ldb_message *msg = ares->message;
        struct extended_dn_out_private *p;
        struct ldb_context *ldb;
+       bool have_reveal_control, checked_reveal_control=false;
+
        ac = talloc_get_type(req->context, struct extended_search_context);
        p = talloc_get_type(ldb_module_get_private(ac->module), struct extended_dn_out_private);
        ldb = ldb_module_get_ctx(ac->module);
@@ -377,8 +373,9 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                                ret = ldb_msg_add_steal_string(ares->message, "distinguishedName", 
                                                               ldb_dn_get_extended_linearized(ares->message, ares->message->dn, ac->extended_type));
                        } else {
-                               ret = ldb_msg_add_string(ares->message, "distinguishedName", 
-                                                        ldb_dn_get_linearized(ares->message->dn));
+                               ret = ldb_msg_add_linearized_dn(ares->message,
+                                                               "distinguishedName",
+                                                               ares->message->dn);
                        }
                        if (ret != LDB_SUCCESS) {
                                ldb_oom(ldb);
@@ -397,6 +394,7 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
 
        /* Walk the retruned elements (but only if we have a schema to interpret the list with) */
        for (i = 0; ac->schema && i < msg->num_elements; i++) {
+               bool make_extended_dn;
                const struct dsdb_attribute *attribute;
                attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
                if (!attribute) {
@@ -424,13 +422,42 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                        continue;
                }
 
+               make_extended_dn = ac->inject;
+
+               /* Always show plain DN in case of Object(OR-Name) syntax */
+               if (make_extended_dn) {
+                       make_extended_dn = (strcmp(attribute->syntax->ldap_oid, DSDB_SYNTAX_OR_NAME) != 0);
+               }
+
                for (j = 0; j < msg->elements[i].num_values; j++) {
                        const char *dn_str;
                        struct ldb_dn *dn;
                        struct dsdb_dn *dsdb_dn = NULL;
                        struct ldb_val *plain_dn = &msg->elements[i].values[j];         
+
+                       if (!checked_reveal_control) {
+                               have_reveal_control =
+                                       ldb_request_get_control(req, LDB_CONTROL_REVEAL_INTERNALS) != NULL;
+                               checked_reveal_control = true;
+                       }
+
+                       /* this is a fast method for detecting deleted
+                          linked attributes, working on the unparsed
+                          ldb_val */
+                       if (dsdb_dn_is_deleted_val(plain_dn) && !have_reveal_control) {
+                               /* it's a deleted linked attribute,
+                                 and we don't have the reveal control */
+                               memmove(&msg->elements[i].values[j],
+                                       &msg->elements[i].values[j+1],
+                                       (msg->elements[i].num_values-(j+1))*sizeof(struct ldb_val));
+                               msg->elements[i].num_values--;
+                               j--;
+                               continue;
+                       }
+
+
                        dsdb_dn = dsdb_dn_parse(msg, ldb, plain_dn, attribute->syntax->ldap_oid);
-                       
+
                        if (!dsdb_dn || !ldb_dn_validate(dsdb_dn->dn)) {
                                ldb_asprintf_errstring(ldb, 
                                                       "could not parse %.*s in %s on %s as a %s DN", 
@@ -442,6 +469,13 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                        }
                        dn = dsdb_dn->dn;
 
+                       /* don't let users see the internal extended
+                          GUID components */
+                       if (!have_reveal_control) {
+                               const char *accept[] = { "GUID", "SID", "WKGUID", NULL };
+                               ldb_dn_extended_filter(dn, accept);
+                       }
+
                        if (p->normalise) {
                                ret = fix_dn(dn);
                                if (ret != LDB_SUCCESS) {
@@ -469,12 +503,12 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                                }
                        }
                        
-                       if (!ac->inject) {
+                       if (make_extended_dn) {
+                               dn_str = dsdb_dn_get_extended_linearized(msg->elements[i].values,
+                                                                        dsdb_dn, ac->extended_type);
+                       } else {
                                dn_str = dsdb_dn_get_linearized(msg->elements[i].values, 
                                                                dsdb_dn);
-                       } else {
-                               dn_str = dsdb_dn_get_extended_linearized(msg->elements[i].values, 
-                                                                        dsdb_dn, ac->extended_type);
                        }
                        
                        if (!dn_str) {
@@ -485,6 +519,15 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                        msg->elements[i].values[j] = data_blob_string_const(dn_str);
                        talloc_free(dsdb_dn);
                }
+               if (msg->elements[i].num_values == 0) {
+                       /* we've deleted all of the values from this
+                        * element - remove the element */
+                       memmove(&msg->elements[i],
+                               &msg->elements[i+1],
+                               (msg->num_elements-(i+1))*sizeof(struct ldb_message_element));
+                       msg->num_elements--;
+                       i--;
+               }
        }
        return ldb_module_send_entry(ac->req, msg, ares->controls);
 }
@@ -510,7 +553,6 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
        struct ldb_control *control;
        struct ldb_control *storage_format_control;
        struct ldb_extended_dn_control *extended_ctrl = NULL;
-       struct ldb_control **saved_controls;
        struct extended_search_context *ac;
        struct ldb_request *down_req;
        char **new_attrs;
@@ -609,24 +651,13 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
                return ret;
        }
 
-       /* Remove extended DN and storage format controls */
-
+       /* mark extended DN and storage format controls as done */
        if (control) {
-               /* save it locally and remove it from the list */
-               /* we do not need to replace them later as we
-                * are keeping the original req intact */
-               if (!save_controls(control, down_req, &saved_controls)) {
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
+               control->critical = 0;
        }
 
        if (storage_format_control) {
-               /* save it locally and remove it from the list */
-               /* we do not need to replace them later as we
-                * are keeping the original req intact */
-               if (!save_controls(storage_format_control, down_req, &saved_controls)) {
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
+               storage_format_control->critical = 0;
        }
 
        /* Add in dereference control, if we were asked to, we are
@@ -665,6 +696,7 @@ 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);
+       struct dsdb_extended_dn_store_format *dn_format;
 
        ldb_module_set_private(module, p);
 
@@ -673,6 +705,20 @@ static int extended_dn_out_ldb_init(struct ldb_module *module)
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
+       dn_format = talloc(p, struct dsdb_extended_dn_store_format);
+       if (!dn_format) {
+               talloc_free(p);
+               ldb_oom(ldb_module_get_ctx(module));
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       dn_format->store_extended_dn_in_ldb = true;
+       ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(p);
+               return ret;
+       }
+
        p->dereference = false;
        p->normalise = false;
 
@@ -690,6 +736,7 @@ static int extended_dn_out_dereference_init(struct ldb_module *module, const cha
 {
        int ret, i = 0;
        struct extended_dn_out_private *p = talloc_zero(module, struct extended_dn_out_private);
+       struct dsdb_extended_dn_store_format *dn_format;
        struct dsdb_openldap_dereference_control *dereference_control;
        struct dsdb_attribute *cur;
        struct ldb_context *ldb = ldb_module_get_ctx(module);
@@ -702,6 +749,21 @@ static int extended_dn_out_dereference_init(struct ldb_module *module, const cha
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
+       dn_format = talloc(p, struct dsdb_extended_dn_store_format);
+       if (!dn_format) {
+               talloc_free(p);
+               ldb_oom(ldb_module_get_ctx(module));
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       dn_format->store_extended_dn_in_ldb = false;
+
+       ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(p);
+               return ret;
+       }
+
        p->dereference = true;
 
        /* At the moment, servers that need dereference also need the