s4/rodc: RODC FAS initial implementation
[Samba/ekacnet.git] / source4 / dsdb / samdb / ldb_modules / extended_dn_out.c
blob39af87091cccf165b10d4d3e8f7f8a28eabe9e50
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_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_ERR_OPERATIONS_ERROR;
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, 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 = ares->message;
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 switch (ares->type) {
330 case LDB_REPLY_REFERRAL:
331 return ldb_module_send_referral(ac->req, ares->referral);
333 case LDB_REPLY_DONE:
334 return ldb_module_done(ac->req, ares->controls,
335 ares->response, LDB_SUCCESS);
336 case LDB_REPLY_ENTRY:
337 break;
340 if (p && p->normalise) {
341 ret = fix_dn(ares->message->dn);
342 if (ret != LDB_SUCCESS) {
343 return ldb_module_done(ac->req, NULL, NULL, ret);
347 if (ac->inject) {
348 /* for each record returned post-process to add any derived
349 attributes that have been asked for */
350 ret = inject_extended_dn_out(ares, ldb,
351 ac->extended_type, ac->remove_guid,
352 ac->remove_sid);
353 if (ret != LDB_SUCCESS) {
354 return ldb_module_done(ac->req, NULL, NULL, ret);
358 if ((p && p->normalise) || ac->inject) {
359 const struct ldb_val *val = ldb_msg_find_ldb_val(ares->message, "distinguishedName");
360 if (val) {
361 ldb_msg_remove_attr(ares->message, "distinguishedName");
362 if (ac->inject) {
363 ret = ldb_msg_add_steal_string(ares->message, "distinguishedName",
364 ldb_dn_get_extended_linearized(ares->message, ares->message->dn, ac->extended_type));
365 } else {
366 ret = ldb_msg_add_linearized_dn(ares->message,
367 "distinguishedName",
368 ares->message->dn);
370 if (ret != LDB_SUCCESS) {
371 ldb_oom(ldb);
372 return LDB_ERR_OPERATIONS_ERROR;
377 if (p && p->dereference) {
378 control = ldb_reply_get_control(ares, DSDB_OPENLDAP_DEREFERENCE_CONTROL);
380 if (control && control->data) {
381 dereference_control = talloc_get_type(control->data, struct dsdb_openldap_dereference_result_control);
385 /* Walk the returned elements (but only if we have a schema to
386 * interpret the list with) */
387 for (i = 0; ac->schema && i < msg->num_elements; i++) {
388 bool make_extended_dn;
389 const struct dsdb_attribute *attribute;
390 attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
391 if (!attribute) {
392 continue;
395 if (p->normalise) {
396 /* If we are also in 'normalise' mode, then
397 * fix the attribute names to be in the
398 * correct case */
399 msg->elements[i].name = talloc_strdup(msg->elements, attribute->lDAPDisplayName);
400 if (!msg->elements[i].name) {
401 ldb_oom(ldb);
402 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
406 /* distinguishedName has been dealt with above */
407 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) {
408 continue;
411 /* Look to see if this attributeSyntax is a DN */
412 if (dsdb_dn_oid_to_format(attribute->syntax->ldap_oid) == DSDB_INVALID_DN) {
413 continue;
416 make_extended_dn = ac->inject;
418 /* Always show plain DN in case of Object(OR-Name) syntax */
419 if (make_extended_dn) {
420 make_extended_dn = (strcmp(attribute->syntax->ldap_oid, DSDB_SYNTAX_OR_NAME) != 0);
423 for (j = 0; j < msg->elements[i].num_values; j++) {
424 const char *dn_str;
425 struct ldb_dn *dn;
426 struct dsdb_dn *dsdb_dn = NULL;
427 struct ldb_val *plain_dn = &msg->elements[i].values[j];
429 if (!checked_reveal_control) {
430 have_reveal_control =
431 ldb_request_get_control(req, LDB_CONTROL_REVEAL_INTERNALS) != NULL;
432 checked_reveal_control = true;
435 /* this is a fast method for detecting deleted
436 linked attributes, working on the unparsed
437 ldb_val */
438 if (dsdb_dn_is_deleted_val(plain_dn) && !have_reveal_control) {
439 /* it's a deleted linked attribute,
440 and we don't have the reveal control */
441 memmove(&msg->elements[i].values[j],
442 &msg->elements[i].values[j+1],
443 (msg->elements[i].num_values-(j+1))*sizeof(struct ldb_val));
444 msg->elements[i].num_values--;
445 j--;
446 continue;
450 dsdb_dn = dsdb_dn_parse(msg, ldb, plain_dn, attribute->syntax->ldap_oid);
452 if (!dsdb_dn || !ldb_dn_validate(dsdb_dn->dn)) {
453 ldb_asprintf_errstring(ldb,
454 "could not parse %.*s in %s on %s as a %s DN",
455 (int)plain_dn->length, plain_dn->data,
456 msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
457 attribute->syntax->ldap_oid);
458 talloc_free(dsdb_dn);
459 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
461 dn = dsdb_dn->dn;
463 /* don't let users see the internal extended
464 GUID components */
465 if (!have_reveal_control) {
466 const char *accept[] = { "GUID", "SID", "WKGUID", NULL };
467 ldb_dn_extended_filter(dn, accept);
470 if (p->normalise) {
471 ret = fix_dn(dn);
472 if (ret != LDB_SUCCESS) {
473 talloc_free(dsdb_dn);
474 return ldb_module_done(ac->req, NULL, NULL, ret);
478 /* If we are running in dereference mode (such
479 * as against OpenLDAP) then the DN in the msg
480 * above does not contain the extended values,
481 * and we need to look in the dereference
482 * result */
484 /* Look for this value in the attribute */
486 if (dereference_control) {
487 ret = handle_dereference(dn,
488 dereference_control->attributes,
489 msg->elements[i].name,
490 &msg->elements[i].values[j]);
491 if (ret != LDB_SUCCESS) {
492 talloc_free(dsdb_dn);
493 return ldb_module_done(ac->req, NULL, NULL, ret);
497 if (make_extended_dn) {
498 dn_str = dsdb_dn_get_extended_linearized(msg->elements[i].values,
499 dsdb_dn, ac->extended_type);
500 } else {
501 dn_str = dsdb_dn_get_linearized(msg->elements[i].values,
502 dsdb_dn);
505 if (!dn_str) {
506 ldb_oom(ldb);
507 talloc_free(dsdb_dn);
508 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
510 msg->elements[i].values[j] = data_blob_string_const(dn_str);
511 talloc_free(dsdb_dn);
513 if (msg->elements[i].num_values == 0) {
514 /* we've deleted all of the values from this
515 * element - remove the element */
516 memmove(&msg->elements[i],
517 &msg->elements[i+1],
518 (msg->num_elements-(i+1))*sizeof(struct ldb_message_element));
519 msg->num_elements--;
520 i--;
523 return ldb_module_send_entry(ac->req, msg, ares->controls);
526 static int extended_callback_ldb(struct ldb_request *req, struct ldb_reply *ares)
528 return extended_callback(req, ares, NULL);
531 static int extended_callback_openldap(struct ldb_request *req, struct ldb_reply *ares)
533 return extended_callback(req, ares, handle_dereference_openldap);
536 static int extended_callback_fds(struct ldb_request *req, struct ldb_reply *ares)
538 return extended_callback(req, ares, handle_dereference_fds);
541 static int extended_dn_out_search(struct ldb_module *module, struct ldb_request *req,
542 int (*callback)(struct ldb_request *req, struct ldb_reply *ares))
544 struct ldb_control *control;
545 struct ldb_control *storage_format_control;
546 struct ldb_extended_dn_control *extended_ctrl = NULL;
547 struct extended_search_context *ac;
548 struct ldb_request *down_req;
549 char **new_attrs;
550 const char * const *const_attrs;
551 struct ldb_context *ldb = ldb_module_get_ctx(module);
552 int ret;
554 struct extended_dn_out_private *p = talloc_get_type(ldb_module_get_private(module), struct extended_dn_out_private);
556 /* The schema manipulation does not apply to special DNs */
557 if (ldb_dn_is_special(req->op.search.base)) {
558 return ldb_next_request(module, req);
561 /* check if there's an extended dn control */
562 control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
563 if (control && control->data) {
564 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
565 if (!extended_ctrl) {
566 return LDB_ERR_PROTOCOL_ERROR;
570 /* Look to see if, as we are in 'store DN+GUID+SID' mode, the
571 * client is after the storage format (to fill in linked
572 * attributes) */
573 storage_format_control = ldb_request_get_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID);
574 if (!control && storage_format_control && storage_format_control->data) {
575 extended_ctrl = talloc_get_type(storage_format_control->data, struct ldb_extended_dn_control);
576 if (!extended_ctrl) {
577 ldb_set_errstring(ldb, "extended_dn_out: extended_ctrl was of the wrong data type");
578 return LDB_ERR_PROTOCOL_ERROR;
582 ac = talloc_zero(req, struct extended_search_context);
583 if (ac == NULL) {
584 ldb_oom(ldb);
585 return LDB_ERR_OPERATIONS_ERROR;
588 ac->module = module;
589 ac->schema = dsdb_get_schema(ldb, ac);
590 ac->req = req;
591 ac->inject = false;
592 ac->remove_guid = false;
593 ac->remove_sid = false;
595 const_attrs = req->op.search.attrs;
597 /* We only need to do special processing if we were asked for
598 * the extended DN, or we are 'store DN+GUID+SID'
599 * (!dereference) mode. (This is the normal mode for LDB on
600 * tdb). */
601 if (control || (storage_format_control && p && !p->dereference)) {
602 ac->inject = true;
603 if (extended_ctrl) {
604 ac->extended_type = extended_ctrl->type;
605 } else {
606 ac->extended_type = 0;
609 /* check if attrs only is specified, in that case check wether we need to modify them */
610 if (req->op.search.attrs && !is_attr_in_list(req->op.search.attrs, "*")) {
611 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
612 ac->remove_guid = true;
614 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
615 ac->remove_sid = true;
617 if (ac->remove_guid || ac->remove_sid) {
618 new_attrs = copy_attrs(ac, req->op.search.attrs);
619 if (new_attrs == NULL) {
620 ldb_oom(ldb);
621 return LDB_ERR_OPERATIONS_ERROR;
624 if (ac->remove_guid) {
625 if (!add_attrs(ac, &new_attrs, "objectGUID"))
626 return LDB_ERR_OPERATIONS_ERROR;
628 if (ac->remove_sid) {
629 if (!add_attrs(ac, &new_attrs, "objectSID"))
630 return LDB_ERR_OPERATIONS_ERROR;
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 if (ret != LDB_SUCCESS) {
647 return ret;
650 /* mark extended DN and storage format controls as done */
651 if (control) {
652 control->critical = 0;
655 if (storage_format_control) {
656 storage_format_control->critical = 0;
659 /* Add in dereference control, if we were asked to, we are
660 * using the 'dereference' mode (such as with an OpenLDAP
661 * backend) and have the control prepared */
662 if (control && p && p->dereference && p->dereference_control) {
663 ret = ldb_request_add_control(down_req,
664 DSDB_OPENLDAP_DEREFERENCE_CONTROL,
665 false, p->dereference_control);
666 if (ret != LDB_SUCCESS) {
667 return ret;
671 /* perform the search */
672 return ldb_next_request(module, down_req);
675 static int extended_dn_out_ldb_search(struct ldb_module *module, struct ldb_request *req)
677 return extended_dn_out_search(module, req, extended_callback_ldb);
680 static int extended_dn_out_openldap_search(struct ldb_module *module, struct ldb_request *req)
682 return extended_dn_out_search(module, req, extended_callback_openldap);
685 static int extended_dn_out_fds_search(struct ldb_module *module, struct ldb_request *req)
687 return extended_dn_out_search(module, req, extended_callback_fds);
690 static int extended_dn_out_ldb_init(struct ldb_module *module)
692 int ret;
694 struct extended_dn_out_private *p = talloc(module, struct extended_dn_out_private);
695 struct dsdb_extended_dn_store_format *dn_format;
697 ldb_module_set_private(module, p);
699 if (!p) {
700 ldb_oom(ldb_module_get_ctx(module));
701 return LDB_ERR_OPERATIONS_ERROR;
704 dn_format = talloc(p, struct dsdb_extended_dn_store_format);
705 if (!dn_format) {
706 talloc_free(p);
707 ldb_oom(ldb_module_get_ctx(module));
708 return LDB_ERR_OPERATIONS_ERROR;
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_ERR_OPERATIONS_ERROR;
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 ldb_oom(ldb);
746 return LDB_ERR_OPERATIONS_ERROR;
749 dn_format = talloc(p, struct dsdb_extended_dn_store_format);
750 if (!dn_format) {
751 talloc_free(p);
752 ldb_oom(ldb_module_get_ctx(module));
753 return LDB_ERR_OPERATIONS_ERROR;
756 dn_format->store_extended_dn_in_ldb = false;
758 ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
759 if (ret != LDB_SUCCESS) {
760 talloc_free(p);
761 return ret;
764 p->dereference = true;
766 /* At the moment, servers that need dereference also need the
767 * DN and attribute names to be normalised */
768 p->normalise = true;
770 ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
771 if (ret != LDB_SUCCESS) {
772 ldb_debug(ldb, LDB_DEBUG_ERROR,
773 "extended_dn_out: Unable to register control with rootdse!\n");
774 return LDB_ERR_OPERATIONS_ERROR;
777 ret = ldb_next_init(module);
779 if (ret != LDB_SUCCESS) {
780 return ret;
783 schema = dsdb_get_schema(ldb, p);
784 if (!schema) {
785 /* No schema on this DB (yet) */
786 return LDB_SUCCESS;
789 p->dereference_control = dereference_control
790 = talloc_zero(p, struct dsdb_openldap_dereference_control);
792 if (!p->dereference_control) {
793 ldb_oom(ldb);
794 return LDB_ERR_OPERATIONS_ERROR;
797 for (cur = schema->attributes; cur; cur = cur->next) {
798 if (dsdb_dn_oid_to_format(cur->syntax->ldap_oid) == DSDB_INVALID_DN) {
799 continue;
801 dereference_control->dereference
802 = talloc_realloc(p, dereference_control->dereference,
803 struct dsdb_openldap_dereference *, i + 2);
804 if (!dereference_control) {
805 ldb_oom(ldb);
806 return LDB_ERR_OPERATIONS_ERROR;
808 dereference_control->dereference[i] = talloc(dereference_control->dereference,
809 struct dsdb_openldap_dereference);
810 if (!dereference_control->dereference[i]) {
811 ldb_oom(ldb);
812 return LDB_ERR_OPERATIONS_ERROR;
814 dereference_control->dereference[i]->source_attribute = cur->lDAPDisplayName;
815 dereference_control->dereference[i]->dereference_attribute = attrs;
816 i++;
817 dereference_control->dereference[i] = NULL;
819 return LDB_SUCCESS;
822 static int extended_dn_out_openldap_init(struct ldb_module *module)
824 static const char *attrs[] = {
825 "entryUUID",
826 "objectSID",
827 NULL
830 return extended_dn_out_dereference_init(module, attrs);
833 static int extended_dn_out_fds_init(struct ldb_module *module)
835 static const char *attrs[] = {
836 "nsUniqueId",
837 "objectSID",
838 NULL
841 return extended_dn_out_dereference_init(module, attrs);
844 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
845 .name = "extended_dn_out_ldb",
846 .search = extended_dn_out_ldb_search,
847 .init_context = extended_dn_out_ldb_init,
850 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_openldap_module_ops = {
851 .name = "extended_dn_out_openldap",
852 .search = extended_dn_out_openldap_search,
853 .init_context = extended_dn_out_openldap_init,
856 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_fds_module_ops = {
857 .name = "extended_dn_out_fds",
858 .search = extended_dn_out_fds_search,
859 .init_context = extended_dn_out_fds_init,