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
,
357 bool is_deleted_objects
, bool *remove_value
)
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 is_deleted_objects
) {
382 search_flags
|= DSDB_SEARCH_SHOW_DELETED
;
385 ret
= dsdb_module_search(ac
->module
, tmp_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
, attrs
,
386 search_flags
, ac
->req
, "objectguid=%s", GUID_string(tmp_ctx
, &guid
));
387 if (ret
!= LDB_SUCCESS
|| res
->count
!= 1) {
388 /* if we can't resolve this GUID, then we don't
389 display the link. This could be a link to a NC that we don't
390 have, or it could be a link to a deleted object
392 (*remove_value
) = true;
393 talloc_free(tmp_ctx
);
396 real_dn
= res
->msgs
[0]->dn
;
398 if (strcmp(ldb_dn_get_linearized(dn
), ldb_dn_get_linearized(real_dn
)) == 0) {
399 /* its already correct */
400 talloc_free(tmp_ctx
);
404 /* fix the DN by replacing its components with those from the
407 if (!ldb_dn_replace_components(dn
, real_dn
)) {
408 talloc_free(tmp_ctx
);
409 return ldb_operr(ldb_module_get_ctx(ac
->module
));
411 talloc_free(tmp_ctx
);
418 this is called to post-process the results from the search
420 static int extended_callback(struct ldb_request
*req
, struct ldb_reply
*ares
,
421 int (*handle_dereference
)(struct ldb_dn
*dn
,
422 struct dsdb_openldap_dereference_result
**dereference_attrs
,
423 const char *attr
, const DATA_BLOB
*val
))
425 struct extended_search_context
*ac
;
426 struct ldb_control
*control
;
427 struct dsdb_openldap_dereference_result_control
*dereference_control
= NULL
;
430 struct ldb_message
*msg
;
431 struct extended_dn_out_private
*p
;
432 struct ldb_context
*ldb
;
433 bool have_reveal_control
=false, checked_reveal_control
=false;
435 ac
= talloc_get_type(req
->context
, struct extended_search_context
);
436 p
= talloc_get_type(ldb_module_get_private(ac
->module
), struct extended_dn_out_private
);
437 ldb
= ldb_module_get_ctx(ac
->module
);
439 return ldb_module_done(ac
->req
, NULL
, NULL
,
440 LDB_ERR_OPERATIONS_ERROR
);
442 if (ares
->error
!= LDB_SUCCESS
) {
443 return ldb_module_done(ac
->req
, ares
->controls
,
444 ares
->response
, ares
->error
);
449 switch (ares
->type
) {
450 case LDB_REPLY_REFERRAL
:
451 return ldb_module_send_referral(ac
->req
, ares
->referral
);
454 return ldb_module_done(ac
->req
, ares
->controls
,
455 ares
->response
, LDB_SUCCESS
);
456 case LDB_REPLY_ENTRY
:
460 if (p
&& p
->normalise
) {
461 ret
= fix_dn(ldb
, ares
->message
->dn
);
462 if (ret
!= LDB_SUCCESS
) {
463 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
468 /* for each record returned post-process to add any derived
469 attributes that have been asked for */
470 ret
= inject_extended_dn_out(ares
, ldb
,
471 ac
->extended_type
, ac
->remove_guid
,
473 if (ret
!= LDB_SUCCESS
) {
474 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
478 if ((p
&& p
->normalise
) || ac
->inject
) {
479 const struct ldb_val
*val
= ldb_msg_find_ldb_val(ares
->message
, "distinguishedName");
481 ldb_msg_remove_attr(ares
->message
, "distinguishedName");
483 ret
= ldb_msg_add_steal_string(ares
->message
, "distinguishedName",
484 ldb_dn_get_extended_linearized(ares
->message
, ares
->message
->dn
, ac
->extended_type
));
486 ret
= ldb_msg_add_linearized_dn(ares
->message
,
490 if (ret
!= LDB_SUCCESS
) {
496 if (p
&& p
->dereference
) {
497 control
= ldb_reply_get_control(ares
, DSDB_OPENLDAP_DEREFERENCE_CONTROL
);
499 if (control
&& control
->data
) {
500 dereference_control
= talloc_get_type(control
->data
, struct dsdb_openldap_dereference_result_control
);
504 /* Walk the returned elements (but only if we have a schema to
505 * interpret the list with) */
506 for (i
= 0; ac
->schema
&& i
< msg
->num_elements
; i
++) {
507 bool make_extended_dn
;
508 const struct dsdb_attribute
*attribute
;
510 attribute
= dsdb_attribute_by_lDAPDisplayName(ac
->schema
, msg
->elements
[i
].name
);
516 /* If we are also in 'normalise' mode, then
517 * fix the attribute names to be in the
519 msg
->elements
[i
].name
= talloc_strdup(msg
->elements
, attribute
->lDAPDisplayName
);
520 if (!msg
->elements
[i
].name
) {
522 return ldb_module_done(ac
->req
, NULL
, NULL
, LDB_ERR_OPERATIONS_ERROR
);
526 /* distinguishedName has been dealt with above */
527 if (ldb_attr_cmp(msg
->elements
[i
].name
, "distinguishedName") == 0) {
531 /* Look to see if this attributeSyntax is a DN */
532 if (attribute
->dn_format
== DSDB_INVALID_DN
) {
536 make_extended_dn
= ac
->inject
;
538 /* Always show plain DN in case of Object(OR-Name) syntax */
539 if (make_extended_dn
) {
540 make_extended_dn
= (strcmp(attribute
->syntax
->ldap_oid
, DSDB_SYNTAX_OR_NAME
) != 0);
543 for (j
= 0; j
< msg
->elements
[i
].num_values
; j
++) {
546 struct dsdb_dn
*dsdb_dn
= NULL
;
547 struct ldb_val
*plain_dn
= &msg
->elements
[i
].values
[j
];
548 bool is_deleted_objects
= false;
550 if (!checked_reveal_control
) {
551 have_reveal_control
=
552 ldb_request_get_control(req
, LDB_CONTROL_REVEAL_INTERNALS
) != NULL
;
553 checked_reveal_control
= true;
556 /* this is a fast method for detecting deleted
557 linked attributes, working on the unparsed
559 if (dsdb_dn_is_deleted_val(plain_dn
) && !have_reveal_control
) {
560 /* it's a deleted linked attribute,
561 and we don't have the reveal control */
562 memmove(&msg
->elements
[i
].values
[j
],
563 &msg
->elements
[i
].values
[j
+1],
564 (msg
->elements
[i
].num_values
-(j
+1))*sizeof(struct ldb_val
));
565 msg
->elements
[i
].num_values
--;
571 dsdb_dn
= dsdb_dn_parse(msg
, ldb
, plain_dn
, attribute
->syntax
->ldap_oid
);
573 if (!dsdb_dn
|| !ldb_dn_validate(dsdb_dn
->dn
)) {
574 ldb_asprintf_errstring(ldb
,
575 "could not parse %.*s in %s on %s as a %s DN",
576 (int)plain_dn
->length
, plain_dn
->data
,
577 msg
->elements
[i
].name
, ldb_dn_get_linearized(msg
->dn
),
578 attribute
->syntax
->ldap_oid
);
579 talloc_free(dsdb_dn
);
580 return ldb_module_done(ac
->req
, NULL
, NULL
, LDB_ERR_INVALID_DN_SYNTAX
);
584 /* we need to know if this is a link to the
585 deleted objects container for fixing one way
587 if (dsdb_dn
->extra_part
.length
== 16) {
588 char *hex_string
= data_blob_hex_string_upper(req
, &dsdb_dn
->extra_part
);
589 if (hex_string
&& strcmp(hex_string
, DS_GUID_DELETED_OBJECTS_CONTAINER
) == 0) {
590 is_deleted_objects
= true;
592 talloc_free(hex_string
);
595 /* don't let users see the internal extended
597 if (!have_reveal_control
) {
598 const char *accept
[] = { "GUID", "SID", NULL
};
599 ldb_dn_extended_filter(dn
, accept
);
603 ret
= fix_dn(ldb
, dn
);
604 if (ret
!= LDB_SUCCESS
) {
605 talloc_free(dsdb_dn
);
606 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
610 /* If we are running in dereference mode (such
611 * as against OpenLDAP) then the DN in the msg
612 * above does not contain the extended values,
613 * and we need to look in the dereference
616 /* Look for this value in the attribute */
618 if (dereference_control
) {
619 ret
= handle_dereference(dn
,
620 dereference_control
->attributes
,
621 msg
->elements
[i
].name
,
622 &msg
->elements
[i
].values
[j
]);
623 if (ret
!= LDB_SUCCESS
) {
624 talloc_free(dsdb_dn
);
625 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
629 /* note that we don't fixup objectCategory as
630 it should not be possible to move
631 objectCategory elements in the schema */
632 if (attribute
->one_way_link
&&
633 strcasecmp(attribute
->lDAPDisplayName
, "objectCategory") != 0) {
635 ret
= fix_one_way_link(ac
, dn
, is_deleted_objects
, &remove_value
);
636 if (ret
!= LDB_SUCCESS
) {
637 talloc_free(dsdb_dn
);
638 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
641 !ldb_request_get_control(req
, LDB_CONTROL_REVEAL_INTERNALS
)) {
642 /* we show these with REVEAL
643 to allow dbcheck to find and
644 cleanup these orphaned links */
645 memmove(&msg
->elements
[i
].values
[j
],
646 &msg
->elements
[i
].values
[j
+1],
647 (msg
->elements
[i
].num_values
-(j
+1))*sizeof(struct ldb_val
));
648 msg
->elements
[i
].num_values
--;
654 if (make_extended_dn
) {
655 dn_str
= dsdb_dn_get_extended_linearized(msg
->elements
[i
].values
,
656 dsdb_dn
, ac
->extended_type
);
658 dn_str
= dsdb_dn_get_linearized(msg
->elements
[i
].values
,
664 talloc_free(dsdb_dn
);
665 return ldb_module_done(ac
->req
, NULL
, NULL
, LDB_ERR_OPERATIONS_ERROR
);
667 msg
->elements
[i
].values
[j
] = data_blob_string_const(dn_str
);
668 talloc_free(dsdb_dn
);
670 if (msg
->elements
[i
].num_values
== 0) {
671 /* we've deleted all of the values from this
672 * element - remove the element */
673 memmove(&msg
->elements
[i
],
675 (msg
->num_elements
-(i
+1))*sizeof(struct ldb_message_element
));
680 return ldb_module_send_entry(ac
->req
, msg
, ares
->controls
);
683 static int extended_callback_ldb(struct ldb_request
*req
, struct ldb_reply
*ares
)
685 return extended_callback(req
, ares
, NULL
);
688 static int extended_callback_openldap(struct ldb_request
*req
, struct ldb_reply
*ares
)
690 return extended_callback(req
, ares
, handle_dereference_openldap
);
693 static int extended_callback_fds(struct ldb_request
*req
, struct ldb_reply
*ares
)
695 return extended_callback(req
, ares
, handle_dereference_fds
);
698 static int extended_dn_out_search(struct ldb_module
*module
, struct ldb_request
*req
,
699 int (*callback
)(struct ldb_request
*req
, struct ldb_reply
*ares
))
701 struct ldb_control
*control
;
702 struct ldb_control
*storage_format_control
;
703 struct ldb_extended_dn_control
*extended_ctrl
= NULL
;
704 struct extended_search_context
*ac
;
705 struct ldb_request
*down_req
;
707 const char * const *const_attrs
;
708 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
712 struct extended_dn_out_private
*p
= talloc_get_type(ldb_module_get_private(module
), struct extended_dn_out_private
);
714 /* The schema manipulation does not apply to special DNs */
715 if (ldb_dn_is_special(req
->op
.search
.base
)) {
716 return ldb_next_request(module
, req
);
719 /* check if there's an extended dn control */
720 control
= ldb_request_get_control(req
, LDB_CONTROL_EXTENDED_DN_OID
);
721 if (control
&& control
->data
) {
722 extended_ctrl
= talloc_get_type(control
->data
, struct ldb_extended_dn_control
);
723 if (!extended_ctrl
) {
724 return LDB_ERR_PROTOCOL_ERROR
;
728 /* Look to see if, as we are in 'store DN+GUID+SID' mode, the
729 * client is after the storage format (to fill in linked
731 storage_format_control
= ldb_request_get_control(req
, DSDB_CONTROL_DN_STORAGE_FORMAT_OID
);
732 if (!control
&& storage_format_control
&& storage_format_control
->data
) {
733 extended_ctrl
= talloc_get_type(storage_format_control
->data
, struct ldb_extended_dn_control
);
734 if (!extended_ctrl
) {
735 ldb_set_errstring(ldb
, "extended_dn_out: extended_ctrl was of the wrong data type");
736 return LDB_ERR_PROTOCOL_ERROR
;
740 ac
= talloc_zero(req
, struct extended_search_context
);
746 ac
->schema
= dsdb_get_schema(ldb
, ac
);
749 ac
->remove_guid
= false;
750 ac
->remove_sid
= false;
752 const_attrs
= req
->op
.search
.attrs
;
754 /* We only need to do special processing if we were asked for
755 * the extended DN, or we are 'store DN+GUID+SID'
756 * (!dereference) mode. (This is the normal mode for LDB on
758 if (control
|| (storage_format_control
&& p
&& !p
->dereference
)) {
761 ac
->extended_type
= extended_ctrl
->type
;
763 ac
->extended_type
= 0;
766 /* check if attrs only is specified, in that case check wether we need to modify them */
767 if (req
->op
.search
.attrs
&& !is_attr_in_list(req
->op
.search
.attrs
, "*")) {
768 if (! is_attr_in_list(req
->op
.search
.attrs
, "objectGUID")) {
769 ac
->remove_guid
= true;
771 if (! is_attr_in_list(req
->op
.search
.attrs
, "objectSid")) {
772 ac
->remove_sid
= true;
774 if (ac
->remove_guid
|| ac
->remove_sid
) {
775 new_attrs
= copy_attrs(ac
, req
->op
.search
.attrs
);
776 if (new_attrs
== NULL
) {
780 if (ac
->remove_guid
) {
781 if (!add_attrs(ac
, &new_attrs
, "objectGUID"))
782 return ldb_operr(ldb
);
784 if (ac
->remove_sid
) {
785 if (!add_attrs(ac
, &new_attrs
, "objectSid"))
786 return ldb_operr(ldb
);
788 const_attrs
= (const char * const *)new_attrs
;
793 ret
= ldb_build_search_req_ex(&down_req
,
796 req
->op
.search
.scope
,
802 LDB_REQ_SET_LOCATION(down_req
);
803 if (ret
!= LDB_SUCCESS
) {
807 /* mark extended DN and storage format controls as done */
809 critical
= control
->critical
;
810 control
->critical
= 0;
813 if (storage_format_control
) {
814 storage_format_control
->critical
= 0;
817 /* Add in dereference control, if we were asked to, we are
818 * using the 'dereference' mode (such as with an OpenLDAP
819 * backend) and have the control prepared */
820 if (control
&& p
&& p
->dereference
) {
821 ret
= extended_dn_out_dereference_setup_control(ldb
, p
);
822 if (ret
!= LDB_SUCCESS
) {
826 /* We should always have this, but before the schema
827 * is with us, things get tricky */
828 if (p
->dereference_control
) {
830 /* This control must *not* be critical,
831 * because if this particular request did not
832 * return any dereferencable attributes in the
833 * end, then OpenLDAP will reply with
834 * unavailableCriticalExtension, rather than
835 * just an empty return control */
836 ret
= ldb_request_add_control(down_req
,
837 DSDB_OPENLDAP_DEREFERENCE_CONTROL
,
838 false, p
->dereference_control
);
839 if (ret
!= LDB_SUCCESS
) {
845 /* perform the search */
846 return ldb_next_request(module
, down_req
);
849 static int extended_dn_out_ldb_search(struct ldb_module
*module
, struct ldb_request
*req
)
851 return extended_dn_out_search(module
, req
, extended_callback_ldb
);
854 static int extended_dn_out_openldap_search(struct ldb_module
*module
, struct ldb_request
*req
)
856 return extended_dn_out_search(module
, req
, extended_callback_openldap
);
859 static int extended_dn_out_fds_search(struct ldb_module
*module
, struct ldb_request
*req
)
861 return extended_dn_out_search(module
, req
, extended_callback_fds
);
864 static int extended_dn_out_ldb_init(struct ldb_module
*module
)
868 struct extended_dn_out_private
*p
= talloc(module
, struct extended_dn_out_private
);
869 struct dsdb_extended_dn_store_format
*dn_format
;
871 ldb_module_set_private(module
, p
);
874 return ldb_oom(ldb_module_get_ctx(module
));
877 dn_format
= talloc(p
, struct dsdb_extended_dn_store_format
);
880 return ldb_oom(ldb_module_get_ctx(module
));
883 dn_format
->store_extended_dn_in_ldb
= true;
884 ret
= ldb_set_opaque(ldb_module_get_ctx(module
), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME
, dn_format
);
885 if (ret
!= LDB_SUCCESS
) {
890 p
->dereference
= false;
891 p
->normalise
= false;
893 ret
= ldb_mod_register_control(module
, LDB_CONTROL_EXTENDED_DN_OID
);
894 if (ret
!= LDB_SUCCESS
) {
895 ldb_debug(ldb_module_get_ctx(module
), LDB_DEBUG_ERROR
,
896 "extended_dn_out: Unable to register control with rootdse!\n");
897 return ldb_operr(ldb_module_get_ctx(module
));
900 return ldb_next_init(module
);
903 static int extended_dn_out_dereference_init(struct ldb_module
*module
, const char *attrs
[])
906 struct extended_dn_out_private
*p
= talloc_zero(module
, struct extended_dn_out_private
);
907 struct dsdb_extended_dn_store_format
*dn_format
;
909 ldb_module_set_private(module
, p
);
912 return ldb_module_oom(module
);
915 dn_format
= talloc(p
, struct dsdb_extended_dn_store_format
);
918 return ldb_module_oom(module
);
921 dn_format
->store_extended_dn_in_ldb
= false;
923 ret
= ldb_set_opaque(ldb_module_get_ctx(module
), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME
, dn_format
);
924 if (ret
!= LDB_SUCCESS
) {
929 p
->dereference
= true;
932 /* At the moment, servers that need dereference also need the
933 * DN and attribute names to be normalised */
936 ret
= ldb_mod_register_control(module
, LDB_CONTROL_EXTENDED_DN_OID
);
937 if (ret
!= LDB_SUCCESS
) {
938 ldb_debug(ldb_module_get_ctx(module
), LDB_DEBUG_ERROR
,
939 "extended_dn_out: Unable to register control with rootdse!\n");
940 return ldb_operr(ldb_module_get_ctx(module
));
943 return ldb_next_init(module
);
946 static int extended_dn_out_openldap_init(struct ldb_module
*module
)
948 static const char *attrs
[] = {
954 return extended_dn_out_dereference_init(module
, attrs
);
957 static int extended_dn_out_fds_init(struct ldb_module
*module
)
959 static const char *attrs
[] = {
965 return extended_dn_out_dereference_init(module
, attrs
);
968 static const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops
= {
969 .name
= "extended_dn_out_ldb",
970 .search
= extended_dn_out_ldb_search
,
971 .init_context
= extended_dn_out_ldb_init
,
974 static const struct ldb_module_ops ldb_extended_dn_out_openldap_module_ops
= {
975 .name
= "extended_dn_out_openldap",
976 .search
= extended_dn_out_openldap_search
,
977 .init_context
= extended_dn_out_openldap_init
,
980 static const struct ldb_module_ops ldb_extended_dn_out_fds_module_ops
= {
981 .name
= "extended_dn_out_fds",
982 .search
= extended_dn_out_fds_search
,
983 .init_context
= extended_dn_out_fds_init
,
987 initialise the module
989 _PUBLIC_
int ldb_extended_dn_out_module_init(const char *version
)
992 LDB_MODULE_CHECK_VERSION(version
);
993 ret
= ldb_register_module(&ldb_extended_dn_out_ldb_module_ops
);
994 if (ret
!= LDB_SUCCESS
) {
997 ret
= ldb_register_module(&ldb_extended_dn_out_openldap_module_ops
);
998 if (ret
!= LDB_SUCCESS
) {
1001 ret
= ldb_register_module(&ldb_extended_dn_out_fds_module_ops
);
1002 if (ret
!= LDB_SUCCESS
) {