s4-dsdb: added tagging of requests in dsdb modules
[Samba.git] / source4 / dsdb / samdb / ldb_modules / extended_dn_out.c
blob2daae253ef1b915abb6f61ffa7311c3261991fb0
1 /*
2 ldb database library
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/>.
22 * Name: ldb
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)
28 * values.
30 * Authors: Simo Sorce
31 * Andrew Bartlett
34 #include "includes.h"
35 #include "ldb/include/ldb.h"
36 #include "ldb/include/ldb_errors.h"
37 #include "ldb/include/ldb_module.h"
38 #include "libcli/security/dom_sid.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 "util.h"
45 struct extended_dn_out_private {
46 bool dereference;
47 bool normalise;
48 struct dsdb_openldap_dereference_control *dereference_control;
51 static char **copy_attrs(void *mem_ctx, const char * const * attrs)
53 char **nattrs;
54 unsigned int i, num;
56 for (num = 0; attrs[num]; num++);
58 nattrs = talloc_array(mem_ctx, char *, num + 1);
59 if (!nattrs) return NULL;
61 for(i = 0; i < num; i++) {
62 nattrs[i] = talloc_strdup(nattrs, attrs[i]);
63 if (!nattrs[i]) {
64 talloc_free(nattrs);
65 return NULL;
68 nattrs[i] = NULL;
70 return nattrs;
73 static bool add_attrs(void *mem_ctx, char ***attrs, const char *attr)
75 char **nattrs;
76 unsigned int num;
78 for (num = 0; (*attrs)[num]; num++);
80 nattrs = talloc_realloc(mem_ctx, *attrs, char *, num + 2);
81 if (!nattrs) return false;
83 *attrs = nattrs;
85 nattrs[num] = talloc_strdup(nattrs, attr);
86 if (!nattrs[num]) return false;
88 nattrs[num + 1] = NULL;
90 return true;
93 /* Fix the DN so that the relative attribute names are in upper case so that the DN:
94 cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
95 CN=Adminstrator,CN=users,DC=samba,DC=example,DC=com
99 static int fix_dn(struct ldb_context *ldb, struct ldb_dn *dn)
101 int i, ret;
102 char *upper_rdn_attr;
104 for (i=0; i < ldb_dn_get_comp_num(dn); i++) {
105 /* We need the attribute name in upper case */
106 upper_rdn_attr = strupper_talloc(dn,
107 ldb_dn_get_component_name(dn, i));
108 if (!upper_rdn_attr) {
109 return ldb_oom(ldb);
112 /* And replace it with CN=foo (we need the attribute in upper case */
113 ret = ldb_dn_set_component(dn, i, upper_rdn_attr,
114 *ldb_dn_get_component_val(dn, i));
115 talloc_free(upper_rdn_attr);
116 if (ret != LDB_SUCCESS) {
117 return ret;
120 return LDB_SUCCESS;
123 /* Inject the extended DN components, so the DN cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
124 <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 */
126 static int inject_extended_dn_out(struct ldb_reply *ares,
127 struct ldb_context *ldb,
128 int type,
129 bool remove_guid,
130 bool remove_sid)
132 int ret;
133 const DATA_BLOB *guid_blob;
134 const DATA_BLOB *sid_blob;
136 guid_blob = ldb_msg_find_ldb_val(ares->message, "objectGUID");
137 sid_blob = ldb_msg_find_ldb_val(ares->message, "objectSid");
139 if (!guid_blob) {
140 ldb_set_errstring(ldb, "Did not find objectGUID to inject into extended DN");
141 return LDB_ERR_OPERATIONS_ERROR;
144 ret = ldb_dn_set_extended_component(ares->message->dn, "GUID", guid_blob);
145 if (ret != LDB_SUCCESS) {
146 return ret;
148 if (sid_blob) {
149 ret = ldb_dn_set_extended_component(ares->message->dn, "SID", sid_blob);
150 if (ret != LDB_SUCCESS) {
151 return ret;
155 if (remove_guid) {
156 ldb_msg_remove_attr(ares->message, "objectGUID");
159 if (sid_blob && remove_sid) {
160 ldb_msg_remove_attr(ares->message, "objectSid");
163 return LDB_SUCCESS;
166 static int handle_dereference_openldap(struct ldb_dn *dn,
167 struct dsdb_openldap_dereference_result **dereference_attrs,
168 const char *attr, const DATA_BLOB *val)
170 const struct ldb_val *entryUUIDblob, *sid_blob;
171 struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
172 unsigned int j;
174 fake_msg.num_elements = 0;
176 /* Look for this attribute in the returned control */
177 for (j = 0; dereference_attrs && dereference_attrs[j]; j++) {
178 struct ldb_val source_dn = data_blob_string_const(dereference_attrs[j]->dereferenced_dn);
179 if (ldb_attr_cmp(dereference_attrs[j]->source_attribute, attr) == 0
180 && data_blob_cmp(&source_dn, val) == 0) {
181 fake_msg.num_elements = dereference_attrs[j]->num_attributes;
182 fake_msg.elements = dereference_attrs[j]->attributes;
183 break;
186 if (!fake_msg.num_elements) {
187 return LDB_SUCCESS;
189 /* Look for an OpenLDAP entryUUID */
191 entryUUIDblob = ldb_msg_find_ldb_val(&fake_msg, "entryUUID");
192 if (entryUUIDblob) {
193 NTSTATUS status;
194 struct ldb_val guid_blob;
195 struct GUID guid;
197 status = GUID_from_data_blob(entryUUIDblob, &guid);
199 if (!NT_STATUS_IS_OK(status)) {
200 return LDB_ERR_INVALID_DN_SYNTAX;
202 status = GUID_to_ndr_blob(&guid, dn, &guid_blob);
203 if (!NT_STATUS_IS_OK(status)) {
204 return LDB_ERR_INVALID_DN_SYNTAX;
207 ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
210 sid_blob = ldb_msg_find_ldb_val(&fake_msg, "objectSid");
212 /* Look for the objectSid */
213 if (sid_blob) {
214 ldb_dn_set_extended_component(dn, "SID", sid_blob);
216 return LDB_SUCCESS;
219 static int handle_dereference_fds(struct ldb_dn *dn,
220 struct dsdb_openldap_dereference_result **dereference_attrs,
221 const char *attr, const DATA_BLOB *val)
223 const struct ldb_val *nsUniqueIdBlob, *sidBlob;
224 struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
225 unsigned int j;
227 fake_msg.num_elements = 0;
229 /* Look for this attribute in the returned control */
230 for (j = 0; dereference_attrs && dereference_attrs[j]; j++) {
231 struct ldb_val source_dn = data_blob_string_const(dereference_attrs[j]->dereferenced_dn);
232 if (ldb_attr_cmp(dereference_attrs[j]->source_attribute, attr) == 0
233 && data_blob_cmp(&source_dn, val) == 0) {
234 fake_msg.num_elements = dereference_attrs[j]->num_attributes;
235 fake_msg.elements = dereference_attrs[j]->attributes;
236 break;
239 if (!fake_msg.num_elements) {
240 return LDB_SUCCESS;
243 /* Look for the nsUniqueId */
245 nsUniqueIdBlob = ldb_msg_find_ldb_val(&fake_msg, "nsUniqueId");
246 if (nsUniqueIdBlob) {
247 NTSTATUS status;
248 struct ldb_val guid_blob;
249 struct GUID guid;
251 status = NS_GUID_from_string((char *)nsUniqueIdBlob->data, &guid);
253 if (!NT_STATUS_IS_OK(status)) {
254 return LDB_ERR_INVALID_DN_SYNTAX;
256 status = GUID_to_ndr_blob(&guid, dn, &guid_blob);
257 if (!NT_STATUS_IS_OK(status)) {
258 return LDB_ERR_INVALID_DN_SYNTAX;
261 ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
264 /* Look for the objectSid */
266 sidBlob = ldb_msg_find_ldb_val(&fake_msg, "sambaSID");
267 if (sidBlob) {
268 enum ndr_err_code ndr_err;
270 struct ldb_val sid_blob;
271 struct dom_sid *sid;
273 sid = dom_sid_parse_length(NULL, sidBlob);
275 if (sid == NULL) {
276 return LDB_ERR_INVALID_DN_SYNTAX;
279 ndr_err = ndr_push_struct_blob(&sid_blob, NULL, sid,
280 (ndr_push_flags_fn_t)ndr_push_dom_sid);
281 talloc_free(sid);
282 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
283 return LDB_ERR_INVALID_DN_SYNTAX;
286 ldb_dn_set_extended_component(dn, "SID", &sid_blob);
288 return LDB_SUCCESS;
291 /* search */
292 struct extended_search_context {
293 struct ldb_module *module;
294 const struct dsdb_schema *schema;
295 struct ldb_request *req;
296 bool inject;
297 bool remove_guid;
298 bool remove_sid;
299 int extended_type;
302 static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
303 int (*handle_dereference)(struct ldb_dn *dn,
304 struct dsdb_openldap_dereference_result **dereference_attrs,
305 const char *attr, const DATA_BLOB *val))
307 struct extended_search_context *ac;
308 struct ldb_control *control;
309 struct dsdb_openldap_dereference_result_control *dereference_control = NULL;
310 int ret;
311 unsigned int i, j;
312 struct ldb_message *msg;
313 struct extended_dn_out_private *p;
314 struct ldb_context *ldb;
315 bool have_reveal_control, checked_reveal_control=false;
317 ac = talloc_get_type(req->context, struct extended_search_context);
318 p = talloc_get_type(ldb_module_get_private(ac->module), struct extended_dn_out_private);
319 ldb = ldb_module_get_ctx(ac->module);
320 if (!ares) {
321 return ldb_module_done(ac->req, NULL, NULL,
322 LDB_ERR_OPERATIONS_ERROR);
324 if (ares->error != LDB_SUCCESS) {
325 return ldb_module_done(ac->req, ares->controls,
326 ares->response, ares->error);
329 msg = ares->message;
331 switch (ares->type) {
332 case LDB_REPLY_REFERRAL:
333 return ldb_module_send_referral(ac->req, ares->referral);
335 case LDB_REPLY_DONE:
336 return ldb_module_done(ac->req, ares->controls,
337 ares->response, LDB_SUCCESS);
338 case LDB_REPLY_ENTRY:
339 break;
342 if (p && p->normalise) {
343 ret = fix_dn(ldb, ares->message->dn);
344 if (ret != LDB_SUCCESS) {
345 return ldb_module_done(ac->req, NULL, NULL, ret);
349 if (ac->inject) {
350 /* for each record returned post-process to add any derived
351 attributes that have been asked for */
352 ret = inject_extended_dn_out(ares, ldb,
353 ac->extended_type, ac->remove_guid,
354 ac->remove_sid);
355 if (ret != LDB_SUCCESS) {
356 return ldb_module_done(ac->req, NULL, NULL, ret);
360 if ((p && p->normalise) || ac->inject) {
361 const struct ldb_val *val = ldb_msg_find_ldb_val(ares->message, "distinguishedName");
362 if (val) {
363 ldb_msg_remove_attr(ares->message, "distinguishedName");
364 if (ac->inject) {
365 ret = ldb_msg_add_steal_string(ares->message, "distinguishedName",
366 ldb_dn_get_extended_linearized(ares->message, ares->message->dn, ac->extended_type));
367 } else {
368 ret = ldb_msg_add_linearized_dn(ares->message,
369 "distinguishedName",
370 ares->message->dn);
372 if (ret != LDB_SUCCESS) {
373 return ldb_oom(ldb);
378 if (p && p->dereference) {
379 control = ldb_reply_get_control(ares, DSDB_OPENLDAP_DEREFERENCE_CONTROL);
381 if (control && control->data) {
382 dereference_control = talloc_get_type(control->data, struct dsdb_openldap_dereference_result_control);
386 /* Walk the returned elements (but only if we have a schema to
387 * interpret the list with) */
388 for (i = 0; ac->schema && i < msg->num_elements; i++) {
389 bool make_extended_dn;
390 const struct dsdb_attribute *attribute;
391 attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
392 if (!attribute) {
393 continue;
396 if (p->normalise) {
397 /* If we are also in 'normalise' mode, then
398 * fix the attribute names to be in the
399 * correct case */
400 msg->elements[i].name = talloc_strdup(msg->elements, attribute->lDAPDisplayName);
401 if (!msg->elements[i].name) {
402 ldb_oom(ldb);
403 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
407 /* distinguishedName has been dealt with above */
408 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) {
409 continue;
412 /* Look to see if this attributeSyntax is a DN */
413 if (dsdb_dn_oid_to_format(attribute->syntax->ldap_oid) == DSDB_INVALID_DN) {
414 continue;
417 make_extended_dn = ac->inject;
419 /* Always show plain DN in case of Object(OR-Name) syntax */
420 if (make_extended_dn) {
421 make_extended_dn = (strcmp(attribute->syntax->ldap_oid, DSDB_SYNTAX_OR_NAME) != 0);
424 for (j = 0; j < msg->elements[i].num_values; j++) {
425 const char *dn_str;
426 struct ldb_dn *dn;
427 struct dsdb_dn *dsdb_dn = NULL;
428 struct ldb_val *plain_dn = &msg->elements[i].values[j];
430 if (!checked_reveal_control) {
431 have_reveal_control =
432 ldb_request_get_control(req, LDB_CONTROL_REVEAL_INTERNALS) != NULL;
433 checked_reveal_control = true;
436 /* this is a fast method for detecting deleted
437 linked attributes, working on the unparsed
438 ldb_val */
439 if (dsdb_dn_is_deleted_val(plain_dn) && !have_reveal_control) {
440 /* it's a deleted linked attribute,
441 and we don't have the reveal control */
442 memmove(&msg->elements[i].values[j],
443 &msg->elements[i].values[j+1],
444 (msg->elements[i].num_values-(j+1))*sizeof(struct ldb_val));
445 msg->elements[i].num_values--;
446 j--;
447 continue;
451 dsdb_dn = dsdb_dn_parse(msg, ldb, plain_dn, attribute->syntax->ldap_oid);
453 if (!dsdb_dn || !ldb_dn_validate(dsdb_dn->dn)) {
454 ldb_asprintf_errstring(ldb,
455 "could not parse %.*s in %s on %s as a %s DN",
456 (int)plain_dn->length, plain_dn->data,
457 msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
458 attribute->syntax->ldap_oid);
459 talloc_free(dsdb_dn);
460 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
462 dn = dsdb_dn->dn;
464 /* don't let users see the internal extended
465 GUID components */
466 if (!have_reveal_control) {
467 const char *accept[] = { "GUID", "SID", "WKGUID", NULL };
468 ldb_dn_extended_filter(dn, accept);
471 if (p->normalise) {
472 ret = fix_dn(ldb, dn);
473 if (ret != LDB_SUCCESS) {
474 talloc_free(dsdb_dn);
475 return ldb_module_done(ac->req, NULL, NULL, ret);
479 /* If we are running in dereference mode (such
480 * as against OpenLDAP) then the DN in the msg
481 * above does not contain the extended values,
482 * and we need to look in the dereference
483 * result */
485 /* Look for this value in the attribute */
487 if (dereference_control) {
488 ret = handle_dereference(dn,
489 dereference_control->attributes,
490 msg->elements[i].name,
491 &msg->elements[i].values[j]);
492 if (ret != LDB_SUCCESS) {
493 talloc_free(dsdb_dn);
494 return ldb_module_done(ac->req, NULL, NULL, ret);
498 if (make_extended_dn) {
499 dn_str = dsdb_dn_get_extended_linearized(msg->elements[i].values,
500 dsdb_dn, ac->extended_type);
501 } else {
502 dn_str = dsdb_dn_get_linearized(msg->elements[i].values,
503 dsdb_dn);
506 if (!dn_str) {
507 ldb_oom(ldb);
508 talloc_free(dsdb_dn);
509 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
511 msg->elements[i].values[j] = data_blob_string_const(dn_str);
512 talloc_free(dsdb_dn);
514 if (msg->elements[i].num_values == 0) {
515 /* we've deleted all of the values from this
516 * element - remove the element */
517 memmove(&msg->elements[i],
518 &msg->elements[i+1],
519 (msg->num_elements-(i+1))*sizeof(struct ldb_message_element));
520 msg->num_elements--;
521 i--;
524 return ldb_module_send_entry(ac->req, msg, ares->controls);
527 static int extended_callback_ldb(struct ldb_request *req, struct ldb_reply *ares)
529 return extended_callback(req, ares, NULL);
532 static int extended_callback_openldap(struct ldb_request *req, struct ldb_reply *ares)
534 return extended_callback(req, ares, handle_dereference_openldap);
537 static int extended_callback_fds(struct ldb_request *req, struct ldb_reply *ares)
539 return extended_callback(req, ares, handle_dereference_fds);
542 static int extended_dn_out_search(struct ldb_module *module, struct ldb_request *req,
543 int (*callback)(struct ldb_request *req, struct ldb_reply *ares))
545 struct ldb_control *control;
546 struct ldb_control *storage_format_control;
547 struct ldb_extended_dn_control *extended_ctrl = NULL;
548 struct extended_search_context *ac;
549 struct ldb_request *down_req;
550 char **new_attrs;
551 const char * const *const_attrs;
552 struct ldb_context *ldb = ldb_module_get_ctx(module);
553 int ret;
554 bool critical;
556 struct extended_dn_out_private *p = talloc_get_type(ldb_module_get_private(module), struct extended_dn_out_private);
558 /* The schema manipulation does not apply to special DNs */
559 if (ldb_dn_is_special(req->op.search.base)) {
560 return ldb_next_request(module, req);
563 /* check if there's an extended dn control */
564 control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
565 if (control && control->data) {
566 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
567 if (!extended_ctrl) {
568 return LDB_ERR_PROTOCOL_ERROR;
572 /* Look to see if, as we are in 'store DN+GUID+SID' mode, the
573 * client is after the storage format (to fill in linked
574 * attributes) */
575 storage_format_control = ldb_request_get_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID);
576 if (!control && storage_format_control && storage_format_control->data) {
577 extended_ctrl = talloc_get_type(storage_format_control->data, struct ldb_extended_dn_control);
578 if (!extended_ctrl) {
579 ldb_set_errstring(ldb, "extended_dn_out: extended_ctrl was of the wrong data type");
580 return LDB_ERR_PROTOCOL_ERROR;
584 ac = talloc_zero(req, struct extended_search_context);
585 if (ac == NULL) {
586 return ldb_oom(ldb);
589 ac->module = module;
590 ac->schema = dsdb_get_schema(ldb, ac);
591 ac->req = req;
592 ac->inject = false;
593 ac->remove_guid = false;
594 ac->remove_sid = false;
596 const_attrs = req->op.search.attrs;
598 /* We only need to do special processing if we were asked for
599 * the extended DN, or we are 'store DN+GUID+SID'
600 * (!dereference) mode. (This is the normal mode for LDB on
601 * tdb). */
602 if (control || (storage_format_control && p && !p->dereference)) {
603 ac->inject = true;
604 if (extended_ctrl) {
605 ac->extended_type = extended_ctrl->type;
606 } else {
607 ac->extended_type = 0;
610 /* check if attrs only is specified, in that case check wether we need to modify them */
611 if (req->op.search.attrs && !is_attr_in_list(req->op.search.attrs, "*")) {
612 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
613 ac->remove_guid = true;
615 if (! is_attr_in_list(req->op.search.attrs, "objectSid")) {
616 ac->remove_sid = true;
618 if (ac->remove_guid || ac->remove_sid) {
619 new_attrs = copy_attrs(ac, req->op.search.attrs);
620 if (new_attrs == NULL) {
621 return ldb_oom(ldb);
624 if (ac->remove_guid) {
625 if (!add_attrs(ac, &new_attrs, "objectGUID"))
626 return ldb_operr(ldb);
628 if (ac->remove_sid) {
629 if (!add_attrs(ac, &new_attrs, "objectSid"))
630 return ldb_operr(ldb);
632 const_attrs = (const char * const *)new_attrs;
637 ret = ldb_build_search_req_ex(&down_req,
638 ldb, ac,
639 req->op.search.base,
640 req->op.search.scope,
641 req->op.search.tree,
642 const_attrs,
643 req->controls,
644 ac, callback,
645 req);
646 LDB_REQ_SET_LOCATION(down_req);
647 if (ret != LDB_SUCCESS) {
648 return ret;
651 /* mark extended DN and storage format controls as done */
652 if (control) {
653 critical = control->critical;
654 control->critical = 0;
657 if (storage_format_control) {
658 storage_format_control->critical = 0;
661 /* Add in dereference control, if we were asked to, we are
662 * using the 'dereference' mode (such as with an OpenLDAP
663 * backend) and have the control prepared */
664 if (control && p && p->dereference && p->dereference_control) {
665 ret = ldb_request_add_control(down_req,
666 DSDB_OPENLDAP_DEREFERENCE_CONTROL,
667 critical, p->dereference_control);
668 if (ret != LDB_SUCCESS) {
669 return ret;
673 /* perform the search */
674 return ldb_next_request(module, down_req);
677 static int extended_dn_out_ldb_search(struct ldb_module *module, struct ldb_request *req)
679 return extended_dn_out_search(module, req, extended_callback_ldb);
682 static int extended_dn_out_openldap_search(struct ldb_module *module, struct ldb_request *req)
684 return extended_dn_out_search(module, req, extended_callback_openldap);
687 static int extended_dn_out_fds_search(struct ldb_module *module, struct ldb_request *req)
689 return extended_dn_out_search(module, req, extended_callback_fds);
692 static int extended_dn_out_ldb_init(struct ldb_module *module)
694 int ret;
696 struct extended_dn_out_private *p = talloc(module, struct extended_dn_out_private);
697 struct dsdb_extended_dn_store_format *dn_format;
699 ldb_module_set_private(module, p);
701 if (!p) {
702 return ldb_oom(ldb_module_get_ctx(module));
705 dn_format = talloc(p, struct dsdb_extended_dn_store_format);
706 if (!dn_format) {
707 talloc_free(p);
708 return ldb_oom(ldb_module_get_ctx(module));
711 dn_format->store_extended_dn_in_ldb = true;
712 ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
713 if (ret != LDB_SUCCESS) {
714 talloc_free(p);
715 return ret;
718 p->dereference = false;
719 p->normalise = false;
721 ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
722 if (ret != LDB_SUCCESS) {
723 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
724 "extended_dn_out: Unable to register control with rootdse!\n");
725 return ldb_operr(ldb_module_get_ctx(module));
728 return ldb_next_init(module);
731 static int extended_dn_out_dereference_init(struct ldb_module *module, const char *attrs[])
733 int ret;
734 unsigned int i = 0;
735 struct extended_dn_out_private *p = talloc_zero(module, struct extended_dn_out_private);
736 struct dsdb_extended_dn_store_format *dn_format;
737 struct dsdb_openldap_dereference_control *dereference_control;
738 struct dsdb_attribute *cur;
739 struct ldb_context *ldb = ldb_module_get_ctx(module);
740 const struct dsdb_schema *schema;
742 ldb_module_set_private(module, p);
744 if (!p) {
745 return ldb_oom(ldb);
748 dn_format = talloc(p, struct dsdb_extended_dn_store_format);
749 if (!dn_format) {
750 talloc_free(p);
751 return ldb_oom(ldb_module_get_ctx(module));
754 dn_format->store_extended_dn_in_ldb = false;
756 ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
757 if (ret != LDB_SUCCESS) {
758 talloc_free(p);
759 return ret;
762 p->dereference = true;
764 /* At the moment, servers that need dereference also need the
765 * DN and attribute names to be normalised */
766 p->normalise = true;
768 ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
769 if (ret != LDB_SUCCESS) {
770 ldb_debug(ldb, LDB_DEBUG_ERROR,
771 "extended_dn_out: Unable to register control with rootdse!\n");
772 return ldb_operr(ldb);
775 ret = ldb_next_init(module);
777 if (ret != LDB_SUCCESS) {
778 return ret;
781 schema = dsdb_get_schema(ldb, p);
782 if (!schema) {
783 /* No schema on this DB (yet) */
784 return LDB_SUCCESS;
787 p->dereference_control = dereference_control
788 = talloc_zero(p, struct dsdb_openldap_dereference_control);
790 if (!p->dereference_control) {
791 return ldb_oom(ldb);
794 for (cur = schema->attributes; cur; cur = cur->next) {
795 if (dsdb_dn_oid_to_format(cur->syntax->ldap_oid) == DSDB_INVALID_DN) {
796 continue;
798 dereference_control->dereference
799 = talloc_realloc(p, dereference_control->dereference,
800 struct dsdb_openldap_dereference *, i + 2);
801 if (!dereference_control) {
802 return ldb_oom(ldb);
804 dereference_control->dereference[i] = talloc(dereference_control->dereference,
805 struct dsdb_openldap_dereference);
806 if (!dereference_control->dereference[i]) {
807 return ldb_oom(ldb);
809 dereference_control->dereference[i]->source_attribute = cur->lDAPDisplayName;
810 dereference_control->dereference[i]->dereference_attribute = attrs;
811 i++;
812 dereference_control->dereference[i] = NULL;
814 return LDB_SUCCESS;
817 static int extended_dn_out_openldap_init(struct ldb_module *module)
819 static const char *attrs[] = {
820 "entryUUID",
821 "objectSid",
822 NULL
825 return extended_dn_out_dereference_init(module, attrs);
828 static int extended_dn_out_fds_init(struct ldb_module *module)
830 static const char *attrs[] = {
831 "nsUniqueId",
832 "sambaSID",
833 NULL
836 return extended_dn_out_dereference_init(module, attrs);
839 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
840 .name = "extended_dn_out_ldb",
841 .search = extended_dn_out_ldb_search,
842 .init_context = extended_dn_out_ldb_init,
845 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_openldap_module_ops = {
846 .name = "extended_dn_out_openldap",
847 .search = extended_dn_out_openldap_search,
848 .init_context = extended_dn_out_openldap_init,
851 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_fds_module_ops = {
852 .name = "extended_dn_out_fds",
853 .search = extended_dn_out_fds_search,
854 .init_context = extended_dn_out_fds_init,