4 Copyright (C) Simo Sorce 2005-2008
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007-2008
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 "librpc/gen_ndr/ndr_misc.h"
39 #include "librpc/ndr/libndr.h"
40 #include "dsdb/samdb/samdb.h"
42 struct extended_dn_out_private {
45 struct dsdb_openldap_dereference_control *dereference_control;
48 static bool is_attr_in_list(const char * const * attrs, const char *attr)
52 for (i = 0; attrs[i]; i++) {
53 if (ldb_attr_cmp(attrs[i], attr) == 0)
60 static char **copy_attrs(void *mem_ctx, const char * const * attrs)
65 for (num = 0; attrs[num]; num++);
67 nattrs = talloc_array(mem_ctx, char *, num + 1);
68 if (!nattrs) return NULL;
70 for(i = 0; i < num; i++) {
71 nattrs[i] = talloc_strdup(nattrs, attrs[i]);
82 static bool add_attrs(void *mem_ctx, char ***attrs, const char *attr)
87 for (num = 0; (*attrs)[num]; num++);
89 nattrs = talloc_realloc(mem_ctx, *attrs, char *, num + 2);
90 if (!nattrs) return false;
94 nattrs[num] = talloc_strdup(nattrs, attr);
95 if (!nattrs[num]) return false;
97 nattrs[num + 1] = NULL;
102 /* Fix the DN so that the relative attribute names are in upper case so that the DN:
103 cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
104 CN=Adminstrator,CN=users,DC=samba,DC=example,DC=com
108 static int fix_dn(struct ldb_dn *dn)
111 char *upper_rdn_attr;
113 for (i=0; i < ldb_dn_get_comp_num(dn); i++) {
114 /* We need the attribute name in upper case */
115 upper_rdn_attr = strupper_talloc(dn,
116 ldb_dn_get_component_name(dn, i));
117 if (!upper_rdn_attr) {
118 return LDB_ERR_OPERATIONS_ERROR;
121 /* And replace it with CN=foo (we need the attribute in upper case */
122 ret = ldb_dn_set_component(dn, i, upper_rdn_attr,
123 *ldb_dn_get_component_val(dn, i));
124 talloc_free(upper_rdn_attr);
125 if (ret != LDB_SUCCESS) {
132 /* Inject the extended DN components, so the DN cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
133 <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 */
135 static int inject_extended_dn_out(struct ldb_reply *ares,
136 struct ldb_context *ldb,
142 const DATA_BLOB *guid_blob;
143 const DATA_BLOB *sid_blob;
145 guid_blob = ldb_msg_find_ldb_val(ares->message, "objectGUID");
146 sid_blob = ldb_msg_find_ldb_val(ares->message, "objectSID");
149 ldb_set_errstring(ldb, "Did not find objectGUID to inject into extended DN");
150 return LDB_ERR_OPERATIONS_ERROR;
153 ret = ldb_dn_set_extended_component(ares->message->dn, "GUID", guid_blob);
154 if (ret != LDB_SUCCESS) {
158 ret = ldb_dn_set_extended_component(ares->message->dn, "SID", sid_blob);
159 if (ret != LDB_SUCCESS) {
165 ldb_msg_remove_attr(ares->message, "objectGUID");
168 if (sid_blob && remove_sid) {
169 ldb_msg_remove_attr(ares->message, "objectSID");
175 static int handle_dereference(struct ldb_dn *dn,
176 struct dsdb_openldap_dereference_result **dereference_attrs,
177 const char *attr, const DATA_BLOB *val)
179 const struct ldb_val *entryUUIDblob, *sid_blob;
180 struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
183 fake_msg.num_elements = 0;
185 /* Look for this attribute in the returned control */
186 for (j = 0; dereference_attrs && dereference_attrs[j]; j++) {
187 struct ldb_val source_dn = data_blob_string_const(dereference_attrs[j]->dereferenced_dn);
188 if (ldb_attr_cmp(dereference_attrs[j]->source_attribute, attr) == 0
189 && data_blob_cmp(&source_dn, val) == 0) {
190 fake_msg.num_elements = dereference_attrs[j]->num_attributes;
191 fake_msg.elements = dereference_attrs[j]->attributes;
195 if (!fake_msg.num_elements) {
198 /* Look for an OpenLDAP entryUUID */
200 entryUUIDblob = ldb_msg_find_ldb_val(&fake_msg, "entryUUID");
203 enum ndr_err_code ndr_err;
205 struct ldb_val guid_blob;
208 status = GUID_from_data_blob(entryUUIDblob, &guid);
210 if (!NT_STATUS_IS_OK(status)) {
211 return LDB_ERR_INVALID_DN_SYNTAX;
213 ndr_err = ndr_push_struct_blob(&guid_blob, NULL, NULL, &guid,
214 (ndr_push_flags_fn_t)ndr_push_GUID);
215 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
216 return LDB_ERR_INVALID_DN_SYNTAX;
219 ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
222 sid_blob = ldb_msg_find_ldb_val(&fake_msg, "objectSID");
224 /* Look for the objectSID */
226 ldb_dn_set_extended_component(dn, "SID", sid_blob);
232 struct extended_search_context {
233 struct ldb_module *module;
234 const struct dsdb_schema *schema;
235 struct ldb_request *req;
242 static int extended_callback(struct ldb_request *req, struct ldb_reply *ares)
244 struct extended_search_context *ac;
245 struct ldb_control *control;
246 struct dsdb_openldap_dereference_result_control *dereference_control = NULL;
248 struct ldb_message *msg = ares->message;
249 struct extended_dn_out_private *p;
251 ac = talloc_get_type(req->context, struct extended_search_context);
252 p = talloc_get_type(ldb_module_get_private(ac->module), struct extended_dn_out_private);
255 return ldb_module_done(ac->req, NULL, NULL,
256 LDB_ERR_OPERATIONS_ERROR);
258 if (ares->error != LDB_SUCCESS) {
259 return ldb_module_done(ac->req, ares->controls,
260 ares->response, ares->error);
263 switch (ares->type) {
264 case LDB_REPLY_REFERRAL:
265 return ldb_module_send_referral(ac->req, ares->referral);
268 return ldb_module_done(ac->req, ares->controls,
269 ares->response, LDB_SUCCESS);
270 case LDB_REPLY_ENTRY:
274 if (p && p->normalise) {
275 ret = fix_dn(ares->message->dn);
276 if (ret != LDB_SUCCESS) {
277 return ldb_module_done(ac->req, NULL, NULL, ret);
282 /* for each record returned post-process to add any derived
283 attributes that have been asked for */
284 ret = inject_extended_dn_out(ares, ldb_module_get_ctx(ac->module),
285 ac->extended_type, ac->remove_guid,
287 if (ret != LDB_SUCCESS) {
288 return ldb_module_done(ac->req, NULL, NULL, ret);
292 if ((p && p->normalise) || ac->inject) {
293 const struct ldb_val *val = ldb_msg_find_ldb_val(ares->message, "distinguishedName");
295 ldb_msg_remove_attr(ares->message, "distinguishedName");
297 ret = ldb_msg_add_steal_string(ares->message, "distinguishedName",
298 ldb_dn_get_extended_linearized(ares->message, ares->message->dn, ac->extended_type));
300 ret = ldb_msg_add_string(ares->message, "distinguishedName",
301 ldb_dn_get_linearized(ares->message->dn));
303 if (ret != LDB_SUCCESS) {
304 ldb_oom(ldb_module_get_ctx(ac->module));
305 return LDB_ERR_OPERATIONS_ERROR;
310 if (p && p->dereference) {
311 control = ldb_reply_get_control(ares, DSDB_OPENLDAP_DEREFERENCE_CONTROL);
313 if (control && control->data) {
314 dereference_control = talloc_get_type(control->data, struct dsdb_openldap_dereference_result_control);
318 /* Walk the retruned elements (but only if we have a schema to interpret the list with) */
319 for (i = 0; ac->schema && i < msg->num_elements; i++) {
320 const struct dsdb_attribute *attribute;
321 attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
327 /* If we are also in 'normalise' mode, then
328 * fix the attribute names to be in the
330 msg->elements[i].name = talloc_strdup(msg->elements, attribute->lDAPDisplayName);
331 if (!msg->elements[i].name) {
332 ldb_oom(ldb_module_get_ctx(ac->module));
333 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
337 /* distinguishedName has been dealt with above */
338 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) {
342 /* Look to see if this attributeSyntax is a DN */
343 if (strcmp(attribute->attributeSyntax_oid, "2.5.5.1") != 0 &&
344 strcmp(attribute->attributeSyntax_oid, "2.5.5.7") != 0) {
348 for (j = 0; j < msg->elements[i].num_values; j++) {
350 struct ldb_dn *dn = ldb_dn_from_ldb_val(ac, ldb_module_get_ctx(ac->module), &msg->elements[i].values[j]);
351 if (!dn || !ldb_dn_validate(dn)) {
352 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
357 if (ret != LDB_SUCCESS) {
358 return ldb_module_done(ac->req, NULL, NULL, ret);
362 /* If we are running in dereference mode (such
363 * as against OpenLDAP) then the DN in the msg
364 * above does not contain the extended values,
365 * and we need to look in the dereference
368 /* Look for this value in the attribute */
370 if (dereference_control) {
371 ret = handle_dereference(dn,
372 dereference_control->attributes,
373 msg->elements[i].name,
374 &msg->elements[i].values[j]);
375 if (ret != LDB_SUCCESS) {
376 return ldb_module_done(ac->req, NULL, NULL, ret);
381 dn_str = talloc_steal(msg->elements[i].values,
382 ldb_dn_get_linearized(dn));
384 dn_str = talloc_steal(msg->elements[i].values,
385 ldb_dn_get_extended_linearized(msg->elements[i].values,
386 dn, ac->extended_type));
389 ldb_oom(ldb_module_get_ctx(ac->module));
390 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
392 msg->elements[i].values[j] = data_blob_string_const(dn_str);
396 return ldb_module_send_entry(ac->req, msg, ares->controls);
400 static int extended_dn_out_search(struct ldb_module *module, struct ldb_request *req)
402 struct ldb_control *control;
403 struct ldb_control *storage_format_control;
404 struct ldb_extended_dn_control *extended_ctrl = NULL;
405 struct ldb_control **saved_controls;
406 struct extended_search_context *ac;
407 struct ldb_request *down_req;
409 const char * const *const_attrs;
412 struct extended_dn_out_private *p = talloc_get_type(ldb_module_get_private(module), struct extended_dn_out_private);
414 /* check if there's an extended dn control */
415 control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
416 if (control && control->data) {
417 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
418 if (!extended_ctrl) {
419 return LDB_ERR_PROTOCOL_ERROR;
423 /* Look to see if, as we are in 'store DN+GUID+SID' mode, the
424 * client is after the storage format (to fill in linked
426 storage_format_control = ldb_request_get_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID);
427 if (!control && storage_format_control && storage_format_control->data) {
428 extended_ctrl = talloc_get_type(storage_format_control->data, struct ldb_extended_dn_control);
429 if (!extended_ctrl) {
430 ldb_set_errstring(ldb_module_get_ctx(module), "extended_dn_out: extended_ctrl was of the wrong data type");
431 return LDB_ERR_PROTOCOL_ERROR;
435 ac = talloc_zero(req, struct extended_search_context);
437 ldb_oom(ldb_module_get_ctx(module));
438 return LDB_ERR_OPERATIONS_ERROR;
442 ac->schema = dsdb_get_schema(ldb_module_get_ctx(module));
445 ac->remove_guid = false;
446 ac->remove_sid = false;
448 const_attrs = req->op.search.attrs;
450 /* We only need to do special processing if we were asked for
451 * the extended DN, or we are 'store DN+GUID+SID'
452 * (!dereference) mode. (This is the normal mode for LDB on
454 if (control || (storage_format_control && p && !p->dereference)) {
457 ac->extended_type = extended_ctrl->type;
459 ac->extended_type = 0;
462 /* check if attrs only is specified, in that case check wether we need to modify them */
463 if (req->op.search.attrs && !is_attr_in_list(req->op.search.attrs, "*")) {
464 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
465 ac->remove_guid = true;
467 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
468 ac->remove_sid = true;
470 if (ac->remove_guid || ac->remove_sid) {
471 new_attrs = copy_attrs(ac, req->op.search.attrs);
472 if (new_attrs == NULL) {
473 ldb_oom(ldb_module_get_ctx(module));
474 return LDB_ERR_OPERATIONS_ERROR;
477 if (ac->remove_guid) {
478 if (!add_attrs(ac, &new_attrs, "objectGUID"))
479 return LDB_ERR_OPERATIONS_ERROR;
481 if (ac->remove_sid) {
482 if (!add_attrs(ac, &new_attrs, "objectSID"))
483 return LDB_ERR_OPERATIONS_ERROR;
485 const_attrs = (const char * const *)new_attrs;
490 ret = ldb_build_search_req_ex(&down_req,
491 ldb_module_get_ctx(module), ac,
493 req->op.search.scope,
497 ac, extended_callback,
499 if (ret != LDB_SUCCESS) {
503 /* Remove extended DN and storage format controls */
506 /* save it locally and remove it from the list */
507 /* we do not need to replace them later as we
508 * are keeping the original req intact */
509 if (!save_controls(control, down_req, &saved_controls)) {
510 return LDB_ERR_OPERATIONS_ERROR;
514 if (storage_format_control) {
515 /* save it locally and remove it from the list */
516 /* we do not need to replace them later as we
517 * are keeping the original req intact */
518 if (!save_controls(storage_format_control, down_req, &saved_controls)) {
519 return LDB_ERR_OPERATIONS_ERROR;
523 /* Add in dereference control, if we were asked to, we are
524 * using the 'dereference' mode (such as with an OpenLDAP
525 * backend) and have the control prepared */
526 if (control && p && p->dereference && p->dereference_control) {
527 ret = ldb_request_add_control(down_req,
528 DSDB_OPENLDAP_DEREFERENCE_CONTROL,
529 false, p->dereference_control);
530 if (ret != LDB_SUCCESS) {
535 /* perform the search */
536 return ldb_next_request(module, down_req);
539 static int extended_dn_out_ldb_init(struct ldb_module *module)
543 struct extended_dn_out_private *p = talloc(module, struct extended_dn_out_private);
545 ldb_module_set_private(module, p);
548 ldb_oom(ldb_module_get_ctx(module));
549 return LDB_ERR_OPERATIONS_ERROR;
552 p->dereference = false;
553 p->normalise = false;
555 ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
556 if (ret != LDB_SUCCESS) {
557 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
558 "extended_dn_out: Unable to register control with rootdse!\n");
559 return LDB_ERR_OPERATIONS_ERROR;
562 return ldb_next_init(module);
565 static int extended_dn_out_dereference_init(struct ldb_module *module)
568 struct extended_dn_out_private *p = talloc_zero(module, struct extended_dn_out_private);
569 struct dsdb_openldap_dereference_control *dereference_control;
570 struct dsdb_attribute *cur;
572 struct dsdb_schema *schema;
574 ldb_module_set_private(module, p);
577 ldb_oom(ldb_module_get_ctx(module));
578 return LDB_ERR_OPERATIONS_ERROR;
581 p->dereference = true;
583 /* At the moment, servers that need dereference also need the
584 * DN and attribute names to be normalised */
587 ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
588 if (ret != LDB_SUCCESS) {
589 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
590 "extended_dn_out: Unable to register control with rootdse!\n");
591 return LDB_ERR_OPERATIONS_ERROR;
594 ret = ldb_next_init(module);
596 if (ret != LDB_SUCCESS) {
600 schema = dsdb_get_schema(ldb_module_get_ctx(module));
602 /* No schema on this DB (yet) */
606 p->dereference_control = dereference_control
607 = talloc_zero(p, struct dsdb_openldap_dereference_control);
609 if (!p->dereference_control) {
610 ldb_oom(ldb_module_get_ctx(module));
611 return LDB_ERR_OPERATIONS_ERROR;
614 for (cur = schema->attributes; cur; cur = cur->next) {
615 static const char *attrs[] = {
621 if (strcmp(cur->syntax->attributeSyntax_oid, "2.5.5.1") != 0 &&
622 strcmp(cur->syntax->attributeSyntax_oid, "2.5.5.7") != 0) {
625 dereference_control->dereference
626 = talloc_realloc(p, dereference_control->dereference,
627 struct dsdb_openldap_dereference *, i + 2);
628 if (!dereference_control) {
629 ldb_oom(ldb_module_get_ctx(module));
630 return LDB_ERR_OPERATIONS_ERROR;
632 dereference_control->dereference[i] = talloc(dereference_control->dereference,
633 struct dsdb_openldap_dereference);
634 if (!dereference_control->dereference[i]) {
635 ldb_oom(ldb_module_get_ctx(module));
636 return LDB_ERR_OPERATIONS_ERROR;
638 dereference_control->dereference[i]->source_attribute = cur->lDAPDisplayName;
639 dereference_control->dereference[i]->dereference_attribute = attrs;
641 dereference_control->dereference[i] = NULL;
646 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
647 .name = "extended_dn_out_ldb",
648 .search = extended_dn_out_search,
649 .init_context = extended_dn_out_ldb_init,
653 _PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_dereference_module_ops = {
654 .name = "extended_dn_out_dereference",
655 .search = extended_dn_out_search,
656 .init_context = extended_dn_out_dereference_init,