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)
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"
45 struct extended_dn_out_private
{
48 struct dsdb_openldap_dereference_control
*dereference_control
;
51 static char **copy_attrs(void *mem_ctx
, const char * const * attrs
)
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
]);
73 static bool add_attrs(void *mem_ctx
, char ***attrs
, const char *attr
)
78 for (num
= 0; (*attrs
)[num
]; num
++);
80 nattrs
= talloc_realloc(mem_ctx
, *attrs
, char *, num
+ 2);
81 if (!nattrs
) return false;
85 nattrs
[num
] = talloc_strdup(nattrs
, attr
);
86 if (!nattrs
[num
]) return false;
88 nattrs
[num
+ 1] = NULL
;
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
)
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
) {
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
,
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");
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
) {
149 ret
= ldb_dn_set_extended_component(ares
->message
->dn
, "SID", sid_blob
);
150 if (ret
!= LDB_SUCCESS
) {
156 ldb_msg_remove_attr(ares
->message
, "objectGUID");
159 if (sid_blob
&& remove_sid
) {
160 ldb_msg_remove_attr(ares
->message
, "objectSID");
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 */
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
;
186 if (!fake_msg
.num_elements
) {
189 /* Look for an OpenLDAP entryUUID */
191 entryUUIDblob
= ldb_msg_find_ldb_val(&fake_msg
, "entryUUID");
194 struct ldb_val guid_blob
;
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 */
214 ldb_dn_set_extended_component(dn
, "SID", sid_blob
);
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 */
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
;
239 if (!fake_msg
.num_elements
) {
243 /* Look for the nsUniqueId */
245 nsUniqueIdBlob
= ldb_msg_find_ldb_val(&fake_msg
, "nsUniqueId");
246 if (nsUniqueIdBlob
) {
248 struct ldb_val guid_blob
;
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");
268 enum ndr_err_code ndr_err
;
270 struct ldb_val sid_blob
;
273 sid
= dom_sid_parse_length(NULL
, sidBlob
);
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
);
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
);
292 struct extended_search_context
{
293 struct ldb_module
*module
;
294 const struct dsdb_schema
*schema
;
295 struct ldb_request
*req
;
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
;
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
);
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
);
334 return ldb_module_done(ac
->req
, ares
->controls
,
335 ares
->response
, LDB_SUCCESS
);
336 case LDB_REPLY_ENTRY
:
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
);
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
,
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");
361 ldb_msg_remove_attr(ares
->message
, "distinguishedName");
363 ret
= ldb_msg_add_steal_string(ares
->message
, "distinguishedName",
364 ldb_dn_get_extended_linearized(ares
->message
, ares
->message
->dn
, ac
->extended_type
));
366 ret
= ldb_msg_add_linearized_dn(ares
->message
,
370 if (ret
!= LDB_SUCCESS
) {
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
);
396 /* If we are also in 'normalise' mode, then
397 * fix the attribute names to be in the
399 msg
->elements
[i
].name
= talloc_strdup(msg
->elements
, attribute
->lDAPDisplayName
);
400 if (!msg
->elements
[i
].name
) {
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) {
411 /* Look to see if this attributeSyntax is a DN */
412 if (dsdb_dn_oid_to_format(attribute
->syntax
->ldap_oid
) == DSDB_INVALID_DN
) {
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
++) {
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
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
--;
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
);
463 /* don't let users see the internal extended
465 if (!have_reveal_control
) {
466 const char *accept
[] = { "GUID", "SID", "WKGUID", NULL
};
467 ldb_dn_extended_filter(dn
, accept
);
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
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
);
501 dn_str
= dsdb_dn_get_linearized(msg
->elements
[i
].values
,
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
],
518 (msg
->num_elements
-(i
+1))*sizeof(struct ldb_message_element
));
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
;
550 const char * const *const_attrs
;
551 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
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
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
);
585 return LDB_ERR_OPERATIONS_ERROR
;
589 ac
->schema
= dsdb_get_schema(ldb
, ac
);
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
601 if (control
|| (storage_format_control
&& p
&& !p
->dereference
)) {
604 ac
->extended_type
= extended_ctrl
->type
;
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
) {
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
,
640 req
->op
.search
.scope
,
646 if (ret
!= LDB_SUCCESS
) {
650 /* mark extended DN and storage format controls as done */
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
) {
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
)
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
);
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
);
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
) {
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
[])
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
);
746 return LDB_ERR_OPERATIONS_ERROR
;
749 dn_format
= talloc(p
, struct dsdb_extended_dn_store_format
);
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
) {
764 p
->dereference
= true;
766 /* At the moment, servers that need dereference also need the
767 * DN and attribute names to be normalised */
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
) {
783 schema
= dsdb_get_schema(ldb
, p
);
785 /* No schema on this DB (yet) */
789 p
->dereference_control
= dereference_control
790 = talloc_zero(p
, struct dsdb_openldap_dereference_control
);
792 if (!p
->dereference_control
) {
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
) {
801 dereference_control
->dereference
802 = talloc_realloc(p
, dereference_control
->dereference
,
803 struct dsdb_openldap_dereference
*, i
+ 2);
804 if (!dereference_control
) {
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
]) {
812 return LDB_ERR_OPERATIONS_ERROR
;
814 dereference_control
->dereference
[i
]->source_attribute
= cur
->lDAPDisplayName
;
815 dereference_control
->dereference
[i
]->dereference_attribute
= attrs
;
817 dereference_control
->dereference
[i
] = NULL
;
822 static int extended_dn_out_openldap_init(struct ldb_module
*module
)
824 static const char *attrs
[] = {
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
[] = {
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
,