4 Copyright (C) Amdrew Bartlett <abartlet@samba.org> 2007-2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * Component: ldb normalisation module
25 * Description: module to ensure all DNs and attribute names are normalised
27 * Author: Andrew Bartlett
31 #include "ldb/include/ldb.h"
32 #include "ldb/include/ldb_errors.h"
33 #include "ldb/include/ldb_private.h"
34 #include "dsdb/samdb/samdb.h"
36 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN
38 This should mean that if the parent is:
39 CN=Users,DC=samba,DC=example,DC=com
40 and a proposed child is
41 cn=Admins ,cn=USERS,dc=Samba,dc=example,dc=COM
43 The resulting DN should be:
45 CN=Admins,CN=Users,DC=samba,DC=example,DC=com
48 static int fix_dn(struct ldb_dn *dn)
53 for (i=0; i < ldb_dn_get_comp_num(dn); i++) {
54 /* We need the attribute name in upper case */
55 upper_rdn_attr = strupper_talloc(dn,
56 ldb_dn_get_component_name(dn, i));
57 if (!upper_rdn_attr) {
58 return LDB_ERR_OPERATIONS_ERROR;
61 /* And replace it with CN=foo (we need the attribute in upper case */
62 ret = ldb_dn_set_component(dn, i, upper_rdn_attr,
63 *ldb_dn_get_component_val(dn, i));
64 talloc_free(upper_rdn_attr);
65 if (ret != LDB_SUCCESS) {
72 static int normalise_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
74 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
75 struct ldb_request *orig_req = talloc_get_type(context, struct ldb_request);
79 /* Only entries are interesting, and we handle the case of the parent seperatly */
80 if (ares->type != LDB_REPLY_ENTRY) {
81 return orig_req->callback(ldb, orig_req->context, ares);
85 return orig_req->callback(ldb, orig_req->context, ares);
88 mem_ctx = talloc_new(ares);
91 return LDB_ERR_OPERATIONS_ERROR;
94 /* OK, we have one of *many* search results passing by here,
95 * but we should get them one at a time */
97 ret = fix_dn(ares->message->dn);
98 if (ret != LDB_SUCCESS) {
103 for (i = 0; i < ares->message->num_elements; i++) {
104 const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(schema, ares->message->elements[i].name);
108 /* Look to see if this attributeSyntax is a DN */
109 if (!((strcmp(attribute->attributeSyntax_oid, "2.5.5.1") == 0) ||
110 (strcmp(attribute->attributeSyntax_oid, "2.5.5.7") == 0))) {
113 for (j = 0; j < ares->message->elements[i].num_values; j++) {
115 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, ldb, &ares->message->elements[i].values[j]);
117 talloc_free(mem_ctx);
118 return LDB_ERR_OPERATIONS_ERROR;
121 if (ret != LDB_SUCCESS) {
122 talloc_free(mem_ctx);
125 dn_str = talloc_steal(ares->message->elements[i].values, ldb_dn_get_linearized(dn));
126 ares->message->elements[i].values[j] = data_blob_string_const(dn_str);
130 talloc_free(mem_ctx);
131 return orig_req->callback(ldb, orig_req->context, ares);
135 static int normalise_search(struct ldb_module *module, struct ldb_request *req)
138 struct ldb_request *down_req = talloc(req, struct ldb_request);
140 ldb_oom(module->ldb);
141 return LDB_ERR_OPERATIONS_ERROR;
145 down_req->context = req;
146 down_req->callback = normalise_search_callback;
148 ret = ldb_next_request(module, down_req);
150 /* do not free down_req as the call results may be linked to it,
151 * it will be freed when the upper level request get freed */
152 if (ret == LDB_SUCCESS) {
153 req->handle = down_req->handle;
159 _PUBLIC_ const struct ldb_module_ops ldb_normalise_module_ops = {
161 .search = normalise_search,