4 Copyright (C) Simo Sorce 2005-2008
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * Component: ldb extended dn control module
26 * Description: this module builds a special dn for returned search
27 * results, and fixes some other aspects of the result (returned case issues)
36 #include <ldb_errors.h>
37 #include <ldb_module.h>
38 #include "libcli/security/security.h"
39 #include "librpc/gen_ndr/ndr_misc.h"
40 #include "librpc/gen_ndr/ndr_security.h"
41 #include "librpc/ndr/libndr.h"
42 #include "dsdb/samdb/samdb.h"
43 #include "dsdb/samdb/ldb_modules/util.h"
45 struct extended_dn_out_private {
48 struct dsdb_openldap_dereference_control *dereference_control;
52 /* Do the lazy init of the derererence control */
54 static int extended_dn_out_dereference_setup_control(struct ldb_context *ldb, struct extended_dn_out_private *p)
56 const struct dsdb_schema *schema;
57 struct dsdb_openldap_dereference_control *dereference_control;
58 struct dsdb_attribute *cur;
61 if (p->dereference_control) {
65 schema = dsdb_get_schema(ldb, p);
67 /* No schema on this DB (yet) */
71 p->dereference_control = dereference_control
72 = talloc_zero(p, struct dsdb_openldap_dereference_control);
74 if (!p->dereference_control) {
78 for (cur = schema->attributes; cur; cur = cur->next) {
79 if (cur->dn_format != DSDB_NORMAL_DN) {
82 dereference_control->dereference
83 = talloc_realloc(p, dereference_control->dereference,
84 struct dsdb_openldap_dereference *, i + 2);
85 if (!dereference_control) {
88 dereference_control->dereference[i] = talloc(dereference_control->dereference,
89 struct dsdb_openldap_dereference);
90 if (!dereference_control->dereference[i]) {
93 dereference_control->dereference[i]->source_attribute = cur->lDAPDisplayName;
94 dereference_control->dereference[i]->dereference_attribute = p->attrs;
96 dereference_control->dereference[i] = NULL;
101 static char **copy_attrs(void *mem_ctx, const char * const * attrs)
106 for (num = 0; attrs[num]; num++);
108 nattrs = talloc_array(mem_ctx, char *, num + 1);
109 if (!nattrs) return NULL;
111 for(i = 0; i < num; i++) {
112 nattrs[i] = talloc_strdup(nattrs, attrs[i]);
123 static bool add_attrs(void *mem_ctx, char ***attrs, const char *attr)
128 for (num = 0; (*attrs)[num]; num++);
130 nattrs = talloc_realloc(mem_ctx, *attrs, char *, num + 2);
131 if (!nattrs) return false;
135 nattrs[num] = talloc_strdup(nattrs, attr);
136 if (!nattrs[num]) return false;
138 nattrs[num + 1] = NULL;
143 /* Fix the DN so that the relative attribute names are in upper case so that the DN:
144 cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
145 CN=Adminstrator,CN=users,DC=samba,DC=example,DC=com
147 static int fix_dn(struct ldb_context *ldb, struct ldb_dn *dn)
150 char *upper_rdn_attr;
152 for (i=0; i < ldb_dn_get_comp_num(dn); i++) {
153 /* We need the attribute name in upper case */
154 upper_rdn_attr = strupper_talloc(dn,
155 ldb_dn_get_component_name(dn, i));
156 if (!upper_rdn_attr) {
160 /* And replace it with CN=foo (we need the attribute in upper case */
161 ret = ldb_dn_set_component(dn, i, upper_rdn_attr,
162 *ldb_dn_get_component_val(dn, i));
163 talloc_free(upper_rdn_attr);
164 if (ret != LDB_SUCCESS) {
172 /* Inject the extended DN components, so the DN cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
173 <GUID=541203ae-f7d6-47ef-8390-bfcf019f9583>;<SID=S-1-5-21-4177067393-1453636373-93818737-500>;cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com */
175 static int inject_extended_dn_out(struct ldb_reply *ares,
176 struct ldb_context *ldb,
182 const DATA_BLOB *guid_blob;
183 const DATA_BLOB *sid_blob;
185 guid_blob = ldb_msg_find_ldb_val(ares->message, "objectGUID");
186 sid_blob = ldb_msg_find_ldb_val(ares->message, "objectSid");
189 ldb_set_errstring(ldb, "Did not find objectGUID to inject into extended DN");
190 return LDB_ERR_OPERATIONS_ERROR;
193 ret = ldb_dn_set_extended_component(ares->message->dn, "GUID", guid_blob);
194 if (ret != LDB_SUCCESS) {
198 ret = ldb_dn_set_extended_component(ares->message->dn, "SID", sid_blob);
199 if (ret != LDB_SUCCESS) {
205 ldb_msg_remove_attr(ares->message, "objectGUID");
208 if (sid_blob && remove_sid) {
209 ldb_msg_remove_attr(ares->message, "objectSid");
215 static int handle_dereference_openldap(struct ldb_dn *dn,
216 struct dsdb_openldap_dereference_result **dereference_attrs,
217 const char *attr, const DATA_BLOB *val)
219 const struct ldb_val *entryUUIDblob, *sid_blob;
220 struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
223 fake_msg.num_elements = 0;
225 /* Look for this attribute in the returned control */
226 for (j = 0; dereference_attrs && dereference_attrs[j]; j++) {
227 struct ldb_val source_dn = data_blob_string_const(dereference_attrs[j]->dereferenced_dn);
228 if (ldb_attr_cmp(dereference_attrs[j]->source_attribute, attr) == 0
229 && data_blob_cmp(&source_dn, val) == 0) {
230 fake_msg.num_elements = dereference_attrs[j]->num_attributes;
231 fake_msg.elements = dereference_attrs[j]->attributes;
235 if (!fake_msg.num_elements) {
238 /* Look for an OpenLDAP entryUUID */
240 entryUUIDblob = ldb_msg_find_ldb_val(&fake_msg, "entryUUID");
243 struct ldb_val guid_blob;
246 status = GUID_from_data_blob(entryUUIDblob, &guid);
248 if (!NT_STATUS_IS_OK(status)) {
249 return LDB_ERR_INVALID_DN_SYNTAX;
251 status = GUID_to_ndr_blob(&guid, dn, &guid_blob);
252 if (!NT_STATUS_IS_OK(status)) {
253 return LDB_ERR_INVALID_DN_SYNTAX;
256 ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
259 sid_blob = ldb_msg_find_ldb_val(&fake_msg, "objectSid");
261 /* Look for the objectSid */
263 ldb_dn_set_extended_component(dn, "SID", sid_blob);
268 static int handle_dereference_fds(struct ldb_dn *dn,
269 struct dsdb_openldap_dereference_result **dereference_attrs,
270 const char *attr, const DATA_BLOB *val)
272 const struct ldb_val *nsUniqueIdBlob, *sidBlob;
273 struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
276 fake_msg.num_elements = 0;
278 /* Look for this attribute in the returned control */
279 for (j = 0; dereference_attrs && dereference_attrs[j]; j++) {
280 struct ldb_val source_dn = data_blob_string_const(dereference_attrs[j]->dereferenced_dn);
281 if (ldb_attr_cmp(dereference_attrs[j]->source_attribute, attr) == 0
282 && data_blob_cmp(&source_dn, val) == 0) {
283 fake_msg.num_elements = dereference_attrs[j]->num_attributes;
284 fake_msg.elements = dereference_attrs[j]->attributes;
288 if (!fake_msg.num_elements) {
292 /* Look for the nsUniqueId */
294 nsUniqueIdBlob = ldb_msg_find_ldb_val(&fake_msg, "nsUniqueId");
295 if (nsUniqueIdBlob) {
297 struct ldb_val guid_blob;
300 status = NS_GUID_from_string((char *)nsUniqueIdBlob->data, &guid);
302 if (!NT_STATUS_IS_OK(status)) {
303 return LDB_ERR_INVALID_DN_SYNTAX;
305 status = GUID_to_ndr_blob(&guid, dn, &guid_blob);
306 if (!NT_STATUS_IS_OK(status)) {
307 return LDB_ERR_INVALID_DN_SYNTAX;
310 ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
313 /* Look for the objectSid */
315 sidBlob = ldb_msg_find_ldb_val(&fake_msg, "sambaSID");
317 enum ndr_err_code ndr_err;
319 struct ldb_val sid_blob;
322 sid = dom_sid_parse_length(NULL, sidBlob);
325 return LDB_ERR_INVALID_DN_SYNTAX;
328 ndr_err = ndr_push_struct_blob(&sid_blob, NULL, sid,
329 (ndr_push_flags_fn_t)ndr_push_dom_sid);
331 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
332 return LDB_ERR_INVALID_DN_SYNTAX;
335 ldb_dn_set_extended_component(dn, "SID", &sid_blob);
341 struct extended_search_context {
342 struct ldb_module *module;
343 const struct dsdb_schema *schema;
344 struct ldb_request *req;
353 fix one-way links to have the right string DN, to cope with
354 renames of the target
356 static int fix_one_way_link(struct extended_search_context *ac, struct ldb_dn *dn,
362 struct ldb_dn *real_dn;
363 uint32_t search_flags;
364 TALLOC_CTX *tmp_ctx = talloc_new(ac);
365 const char *attrs[] = { NULL };
366 struct ldb_result *res;
368 (*remove_value) = false;
370 status = dsdb_get_extended_dn_guid(dn, &guid, "GUID");
371 if (!NT_STATUS_IS_OK(status)) {
372 /* this is a strange DN that doesn't have a GUID! just
373 return the current DN string?? */
374 talloc_free(tmp_ctx);
378 search_flags = DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SEARCH_ALL_PARTITIONS | DSDB_SEARCH_ONE_ONLY;
380 if (ldb_request_get_control(ac->req, LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID)) {
381 search_flags |= DSDB_SEARCH_SHOW_DELETED;
384 ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
385 search_flags, ac->req, "objectguid=%s", GUID_string(tmp_ctx, &guid));
386 if (ret != LDB_SUCCESS || res->count != 1) {
387 /* if we can't resolve this GUID, then we don't
388 display the link. This could be a link to a NC that we don't
389 have, or it could be a link to a deleted object
391 (*remove_value) = true;
392 talloc_free(tmp_ctx);
395 real_dn = res->msgs[0]->dn;
397 if (strcmp(ldb_dn_get_linearized(dn), ldb_dn_get_linearized(real_dn)) == 0) {
398 /* its already correct */
399 talloc_free(tmp_ctx);
403 /* fix the DN by replacing its components with those from the
406 if (!ldb_dn_replace_components(dn, real_dn)) {
407 talloc_free(tmp_ctx);
408 return ldb_operr(ldb_module_get_ctx(ac->module));
410 talloc_free(tmp_ctx);
417 this is called to post-process the results from the search
419 static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
420 int (*handle_dereference)(struct ldb_dn *dn,
421 struct dsdb_openldap_dereference_result **dereference_attrs,
422 const char *attr, const DATA_BLOB *val))
424 struct extended_search_context *ac;
425 struct ldb_control *control;
426 struct dsdb_openldap_dereference_result_control *dereference_control = NULL;
429 struct ldb_message *msg;
430 struct extended_dn_out_private *p;
431 struct ldb_context *ldb;
432 bool have_reveal_control=false, checked_reveal_control=false;
434 ac = talloc_get_type(req->context, struct extended_search_context);
435 p = talloc_get_type(ldb_module_get_private(ac->module), struct extended_dn_out_private);
436 ldb = ldb_module_get_ctx(ac->module);
438 return ldb_module_done(ac->req, NULL, NULL,
439 LDB_ERR_OPERATIONS_ERROR);
441 if (ares->error != LDB_SUCCESS) {
442 return ldb_module_done(ac->req, ares->controls,
443 ares->response, ares->error);
448 switch (ares->type) {
449 case LDB_REPLY_REFERRAL:
450 return ldb_module_send_referral(ac->req, ares->referral);
453 return ldb_module_done(ac->req, ares->controls,
454 ares->response, LDB_SUCCESS);
455 case LDB_REPLY_ENTRY:
459 if (p && p->normalise) {
460 ret = fix_dn(ldb, ares->message->dn);
461 if (ret != LDB_SUCCESS) {
462 return ldb_module_done(ac->req, NULL, NULL, ret);
467 /* for each record returned post-process to add any derived
468 attributes that have been asked for */
469 ret = inject_extended_dn_out(ares, ldb,
470 ac->extended_type, ac->remove_guid,
472 if (ret != LDB_SUCCESS) {
473 return ldb_module_done(ac->req, NULL, NULL, ret);
477 if ((p && p->normalise) || ac->inject) {
478 const struct ldb_val *val = ldb_msg_find_ldb_val(ares->message, "distinguishedName");
480 ldb_msg_remove_attr(ares->message, "distinguishedName");
482 ret = ldb_msg_add_steal_string(ares->message, "distinguishedName",
483 ldb_dn_get_extended_linearized(ares->message, ares->message->dn, ac->extended_type));
485 ret = ldb_msg_add_linearized_dn(ares->message,
489 if (ret != LDB_SUCCESS) {
495 if (p && p->dereference) {
496 control = ldb_reply_get_control(ares, DSDB_OPENLDAP_DEREFERENCE_CONTROL);
498 if (control && control->data) {
499 dereference_control = talloc_get_type(control->data, struct dsdb_openldap_dereference_result_control);
503 /* Walk the returned elements (but only if we have a schema to
504 * interpret the list with) */
505 for (i = 0; ac->schema && i < msg->num_elements; i++) {
506 bool make_extended_dn;
507 const struct dsdb_attribute *attribute;
509 attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
515 /* If we are also in 'normalise' mode, then
516 * fix the attribute names to be in the
518 msg->elements[i].name = talloc_strdup(msg->elements, attribute->lDAPDisplayName);
519 if (!msg->elements[i].name) {
521 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
525 /* distinguishedName has been dealt with above */
526 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) {
530 /* Look to see if this attributeSyntax is a DN */
531 if (attribute->dn_format == DSDB_INVALID_DN) {
535 make_extended_dn = ac->inject;
537 /* Always show plain DN in case of Object(OR-Name) syntax */
538 if (make_extended_dn) {
539 make_extended_dn = (strcmp(attribute->syntax->ldap_oid, DSDB_SYNTAX_OR_NAME) != 0);
542 for (j = 0; j < msg->elements[i].num_values; j++) {
545 struct dsdb_dn *dsdb_dn = NULL;
546 struct ldb_val *plain_dn = &msg->elements[i].values[j];
548 if (!checked_reveal_control) {
549 have_reveal_control =
550 ldb_request_get_control(req, LDB_CONTROL_REVEAL_INTERNALS) != NULL;
551 checked_reveal_control = true;
554 /* this is a fast method for detecting deleted
555 linked attributes, working on the unparsed
557 if (dsdb_dn_is_deleted_val(plain_dn) && !have_reveal_control) {
558 /* it's a deleted linked attribute,
559 and we don't have the reveal control */
560 memmove(&msg->elements[i].values[j],
561 &msg->elements[i].values[j+1],
562 (msg->elements[i].num_values-(j+1))*sizeof(struct ldb_val));
563 msg->elements[i].num_values--;
569 dsdb_dn = dsdb_dn_parse(msg, ldb, plain_dn, attribute->syntax->ldap_oid);
571 if (!dsdb_dn || !ldb_dn_validate(dsdb_dn->dn)) {
572 ldb_asprintf_errstring(ldb,
573 "could not parse %.*s in %s on %s as a %s DN",
574 (int)plain_dn->length, plain_dn->data,
575 msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
576 attribute->syntax->ldap_oid);
577 talloc_free(dsdb_dn);
578 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
582 /* don't let users see the internal extended
584 if (!have_reveal_control) {
585 const char *accept[] = { "GUID", "SID", "WKGUID", NULL };
586 ldb_dn_extended_filter(dn, accept);
590 ret = fix_dn(ldb, dn);
591 if (ret != LDB_SUCCESS) {
592 talloc_free(dsdb_dn);
593 return ldb_module_done(ac->req, NULL, NULL, ret);
597 /* If we are running in dereference mode (such
598 * as against OpenLDAP) then the DN in the msg
599 * above does not contain the extended values,
600 * and we need to look in the dereference
603 /* Look for this value in the attribute */
605 if (dereference_control) {
606 ret = handle_dereference(dn,
607 dereference_control->attributes,
608 msg->elements[i].name,
609 &msg->elements[i].values[j]);
610 if (ret != LDB_SUCCESS) {
611 talloc_free(dsdb_dn);
612 return ldb_module_done(ac->req, NULL, NULL, ret);
616 /* note that we don't fixup objectCategory as
617 it should not be possible to move
618 objectCategory elements in the schema */
619 if (attribute->one_way_link &&
620 strcasecmp(attribute->lDAPDisplayName, "objectCategory") != 0) {
622 ret = fix_one_way_link(ac, dn, &remove_value);
623 if (ret != LDB_SUCCESS) {
624 talloc_free(dsdb_dn);
625 return ldb_module_done(ac->req, NULL, NULL, ret);
628 !ldb_request_get_control(req, LDB_CONTROL_REVEAL_INTERNALS)) {
629 /* we show these with REVEAL
630 to allow dbcheck to find and
631 cleanup these orphaned links */
632 memmove(&msg->elements[i].values[j],
633 &msg->elements[i].values[j+1],
634 (msg->elements[i].num_values-(j+1))*sizeof(struct ldb_val));
635 msg->elements[i].num_values--;
641 if (make_extended_dn) {
642 dn_str = dsdb_dn_get_extended_linearized(msg->elements[i].values,
643 dsdb_dn, ac->extended_type);
645 dn_str = dsdb_dn_get_linearized(msg->elements[i].values,
651 talloc_free(dsdb_dn);
652 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
654 msg->elements[i].values[j] = data_blob_string_const(dn_str);
655 talloc_free(dsdb_dn);
657 if (msg->elements[i].num_values == 0) {
658 /* we've deleted all of the values from this
659 * element - remove the element */
660 memmove(&msg->elements[i],
662 (msg->num_elements-(i+1))*sizeof(struct ldb_message_element));
667 return ldb_module_send_entry(ac->req, msg, ares->controls);
670 static int extended_callback_ldb(struct ldb_request *req, struct ldb_reply *ares)
672 return extended_callback(req, ares, NULL);
675 static int extended_callback_openldap(struct ldb_request *req, struct ldb_reply *ares)
677 return extended_callback(req, ares, handle_dereference_openldap);
680 static int extended_callback_fds(struct ldb_request *req, struct ldb_reply *ares)
682 return extended_callback(req, ares, handle_dereference_fds);
685 static int extended_dn_out_search(struct ldb_module *module, struct ldb_request *req,
686 int (*callback)(struct ldb_request *req, struct ldb_reply *ares))
688 struct ldb_control *control;
689 struct ldb_control *storage_format_control;
690 struct ldb_extended_dn_control *extended_ctrl = NULL;
691 struct extended_search_context *ac;
692 struct ldb_request *down_req;
694 const char * const *const_attrs;
695 struct ldb_context *ldb = ldb_module_get_ctx(module);
699 struct extended_dn_out_private *p = talloc_get_type(ldb_module_get_private(module), struct extended_dn_out_private);
701 /* The schema manipulation does not apply to special DNs */
702 if (ldb_dn_is_special(req->op.search.base)) {
703 return ldb_next_request(module, req);
706 /* check if there's an extended dn control */
707 control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
708 if (control && control->data) {
709 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
710 if (!extended_ctrl) {
711 return LDB_ERR_PROTOCOL_ERROR;
715 /* Look to see if, as we are in 'store DN+GUID+SID' mode, the
716 * client is after the storage format (to fill in linked
718 storage_format_control = ldb_request_get_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID);
719 if (!control && storage_format_control && storage_format_control->data) {
720 extended_ctrl = talloc_get_type(storage_format_control->data, struct ldb_extended_dn_control);
721 if (!extended_ctrl) {
722 ldb_set_errstring(ldb, "extended_dn_out: extended_ctrl was of the wrong data type");
723 return LDB_ERR_PROTOCOL_ERROR;
727 ac = talloc_zero(req, struct extended_search_context);
733 ac->schema = dsdb_get_schema(ldb, ac);
736 ac->remove_guid = false;
737 ac->remove_sid = false;
739 const_attrs = req->op.search.attrs;
741 /* We only need to do special processing if we were asked for
742 * the extended DN, or we are 'store DN+GUID+SID'
743 * (!dereference) mode. (This is the normal mode for LDB on
745 if (control || (storage_format_control && p && !p->dereference)) {
748 ac->extended_type = extended_ctrl->type;
750 ac->extended_type = 0;
753 /* check if attrs only is specified, in that case check wether we need to modify them */
754 if (req->op.search.attrs && !is_attr_in_list(req->op.search.attrs, "*")) {
755 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
756 ac->remove_guid = true;
758 if (! is_attr_in_list(req->op.search.attrs, "objectSid")) {
759 ac->remove_sid = true;
761 if (ac->remove_guid || ac->remove_sid) {
762 new_attrs = copy_attrs(ac, req->op.search.attrs);
763 if (new_attrs == NULL) {
767 if (ac->remove_guid) {
768 if (!add_attrs(ac, &new_attrs, "objectGUID"))
769 return ldb_operr(ldb);
771 if (ac->remove_sid) {
772 if (!add_attrs(ac, &new_attrs, "objectSid"))
773 return ldb_operr(ldb);
775 const_attrs = (const char * const *)new_attrs;
780 ret = ldb_build_search_req_ex(&down_req,
783 req->op.search.scope,
789 LDB_REQ_SET_LOCATION(down_req);
790 if (ret != LDB_SUCCESS) {
794 /* mark extended DN and storage format controls as done */
796 critical = control->critical;
797 control->critical = 0;
800 if (storage_format_control) {
801 storage_format_control->critical = 0;
804 /* Add in dereference control, if we were asked to, we are
805 * using the 'dereference' mode (such as with an OpenLDAP
806 * backend) and have the control prepared */
807 if (control && p && p->dereference) {
808 ret = extended_dn_out_dereference_setup_control(ldb, p);
809 if (ret != LDB_SUCCESS) {
813 /* We should always have this, but before the schema
814 * is with us, things get tricky */
815 if (p->dereference_control) {
817 /* This control must *not* be critical,
818 * because if this particular request did not
819 * return any dereferencable attributes in the
820 * end, then OpenLDAP will reply with
821 * unavailableCriticalExtension, rather than
822 * just an empty return control */
823 ret = ldb_request_add_control(down_req,
824 DSDB_OPENLDAP_DEREFERENCE_CONTROL,
825 false, p->dereference_control);
826 if (ret != LDB_SUCCESS) {
832 /* perform the search */
833 return ldb_next_request(module, down_req);
836 static int extended_dn_out_ldb_search(struct ldb_module *module, struct ldb_request *req)
838 return extended_dn_out_search(module, req, extended_callback_ldb);
841 static int extended_dn_out_openldap_search(struct ldb_module *module, struct ldb_request *req)
843 return extended_dn_out_search(module, req, extended_callback_openldap);
846 static int extended_dn_out_fds_search(struct ldb_module *module, struct ldb_request *req)
848 return extended_dn_out_search(module, req, extended_callback_fds);
851 static int extended_dn_out_ldb_init(struct ldb_module *module)
855 struct extended_dn_out_private *p = talloc(module, struct extended_dn_out_private);
856 struct dsdb_extended_dn_store_format *dn_format;
858 ldb_module_set_private(module, p);
861 return ldb_oom(ldb_module_get_ctx(module));
864 dn_format = talloc(p, struct dsdb_extended_dn_store_format);
867 return ldb_oom(ldb_module_get_ctx(module));
870 dn_format->store_extended_dn_in_ldb = true;
871 ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
872 if (ret != LDB_SUCCESS) {
877 p->dereference = false;
878 p->normalise = false;
880 ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
881 if (ret != LDB_SUCCESS) {
882 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
883 "extended_dn_out: Unable to register control with rootdse!\n");
884 return ldb_operr(ldb_module_get_ctx(module));
887 return ldb_next_init(module);
890 static int extended_dn_out_dereference_init(struct ldb_module *module, const char *attrs[])
893 struct extended_dn_out_private *p = talloc_zero(module, struct extended_dn_out_private);
894 struct dsdb_extended_dn_store_format *dn_format;
896 ldb_module_set_private(module, p);
899 return ldb_module_oom(module);
902 dn_format = talloc(p, struct dsdb_extended_dn_store_format);
905 return ldb_module_oom(module);
908 dn_format->store_extended_dn_in_ldb = false;
910 ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
911 if (ret != LDB_SUCCESS) {
916 p->dereference = true;
919 /* At the moment, servers that need dereference also need the
920 * DN and attribute names to be normalised */
923 ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
924 if (ret != LDB_SUCCESS) {
925 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
926 "extended_dn_out: Unable to register control with rootdse!\n");
927 return ldb_operr(ldb_module_get_ctx(module));
930 return ldb_next_init(module);
933 static int extended_dn_out_openldap_init(struct ldb_module *module)
935 static const char *attrs[] = {
941 return extended_dn_out_dereference_init(module, attrs);
944 static int extended_dn_out_fds_init(struct ldb_module *module)
946 static const char *attrs[] = {
952 return extended_dn_out_dereference_init(module, attrs);
955 static const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
956 .name = "extended_dn_out_ldb",
957 .search = extended_dn_out_ldb_search,
958 .init_context = extended_dn_out_ldb_init,
961 static const struct ldb_module_ops ldb_extended_dn_out_openldap_module_ops = {
962 .name = "extended_dn_out_openldap",
963 .search = extended_dn_out_openldap_search,
964 .init_context = extended_dn_out_openldap_init,
967 static const struct ldb_module_ops ldb_extended_dn_out_fds_module_ops = {
968 .name = "extended_dn_out_fds",
969 .search = extended_dn_out_fds_search,
970 .init_context = extended_dn_out_fds_init,
974 initialise the module
976 _PUBLIC_ int ldb_extended_dn_out_module_init(const char *version)
979 LDB_MODULE_CHECK_VERSION(version);
980 ret = ldb_register_module(&ldb_extended_dn_out_ldb_module_ops);
981 if (ret != LDB_SUCCESS) {
984 ret = ldb_register_module(&ldb_extended_dn_out_openldap_module_ops);
985 if (ret != LDB_SUCCESS) {
988 ret = ldb_register_module(&ldb_extended_dn_out_fds_module_ops);
989 if (ret != LDB_SUCCESS) {