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 if ((strcmp(attribute->attributeSyntax_oid, "2.5.5.1") != 0) &&
109 (strcmp(attribute->attributeSyntax_oid, "2.5.5.7") != 0)) {
112 for (j = 0; j < ares->message->elements[i].num_values; j++) {
114 struct ldb_dn *dn = ldb_dn_new(mem_ctx, ldb, (const char *)ares->message->elements[i].values[j].data);
116 talloc_free(mem_ctx);
117 return LDB_ERR_OPERATIONS_ERROR;
119 ret = fix_dn(ares->message->dn);
120 if (ret != LDB_SUCCESS) {
121 talloc_free(mem_ctx);
124 dn_str = talloc_steal(ares->message->elements[i].values, ldb_dn_get_linearized(dn));
125 ares->message->elements[i].values[j] = data_blob_string_const(dn_str);
129 talloc_free(mem_ctx);
130 return orig_req->callback(ldb, orig_req->context, ares);
134 static int normalise_search(struct ldb_module *module, struct ldb_request *req)
137 struct ldb_request *down_req = talloc(req, struct ldb_request);
139 ldb_oom(module->ldb);
140 return LDB_ERR_OPERATIONS_ERROR;
144 down_req->context = req;
145 down_req->callback = normalise_search_callback;
147 ret = ldb_next_request(module, down_req);
149 /* do not free down_req as the call results may be linked to it,
150 * it will be freed when the upper level request get freed */
151 if (ret == LDB_SUCCESS) {
152 req->handle = down_req->handle;
158 static const struct ldb_module_ops normalise_ops = {
160 .search = normalise_search,
163 int ldb_normalise_init(void)
165 return ldb_register_module(&normalise_ops);