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
50 struct ldb_module *module;
51 struct ldb_request *req;
53 const struct dsdb_schema *schema;
56 static int fix_dn(struct ldb_dn *dn)
61 for (i=0; i < ldb_dn_get_comp_num(dn); i++) {
62 /* We need the attribute name in upper case */
63 upper_rdn_attr = strupper_talloc(dn,
64 ldb_dn_get_component_name(dn, i));
65 if (!upper_rdn_attr) {
66 return LDB_ERR_OPERATIONS_ERROR;
69 /* And replace it with CN=foo (we need the attribute in upper case */
70 ret = ldb_dn_set_component(dn, i, upper_rdn_attr,
71 *ldb_dn_get_component_val(dn, i));
72 talloc_free(upper_rdn_attr);
73 if (ret != LDB_SUCCESS) {
80 static int normalize_search_callback(struct ldb_request *req, struct ldb_reply *ares)
82 struct ldb_message *msg;
83 struct norm_context *ac;
86 ac = talloc_get_type(req->context, struct norm_context);
89 return ldb_module_done(ac->req, NULL, NULL,
90 LDB_ERR_OPERATIONS_ERROR);
92 if (ares->error != LDB_SUCCESS) {
93 return ldb_module_done(ac->req, ares->controls,
94 ares->response, ares->error);
97 /* Only entries are interesting, and we handle the case of the parent seperatly */
100 case LDB_REPLY_ENTRY:
102 /* OK, we have one of *many* search results passing by here,
103 * but we should get them one at a time */
106 ret = fix_dn(msg->dn);
107 if (ret != LDB_SUCCESS) {
108 return ldb_module_done(ac->req, NULL, NULL, ret);
111 for (i = 0; i < msg->num_elements; i++) {
112 const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
116 /* Look to see if this attributeSyntax is a DN */
117 if (strcmp(attribute->attributeSyntax_oid, "2.5.5.1") == 0) {
120 for (j = 0; j < msg->elements[i].num_values; j++) {
122 struct ldb_dn *dn = ldb_dn_from_ldb_val(ac, ac->module->ldb, &msg->elements[i].values[j]);
124 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
127 if (ret != LDB_SUCCESS) {
128 return ldb_module_done(ac->req, NULL, NULL, ret);
130 dn_str = talloc_steal(msg->elements[i].values, ldb_dn_get_linearized(dn));
131 msg->elements[i].values[j] = data_blob_string_const(dn_str);
136 return ldb_module_send_entry(ac->req, msg);
138 case LDB_REPLY_REFERRAL:
140 return ldb_module_send_referral(ac->req, ares->referral);
144 return ldb_module_done(ac->req, ares->controls,
145 ares->response, ares->error);
152 static int normalise_search(struct ldb_module *module, struct ldb_request *req)
154 struct ldb_request *down_req;
155 struct norm_context *ac;
158 ac = talloc(req, struct norm_context);
160 return LDB_ERR_OPERATIONS_ERROR;
166 /* if schema not yet present just skip over */
167 ac->schema = dsdb_get_schema(ac->module->ldb);
168 if (ac->schema == NULL) {
170 return ldb_next_request(module, req);
173 ret = ldb_build_search_req_ex(&down_req, module->ldb, ac,
175 req->op.search.scope,
177 req->op.search.attrs,
179 ac, normalize_search_callback,
181 if (ret != LDB_SUCCESS) {
182 return LDB_ERR_OPERATIONS_ERROR;
185 return ldb_next_request(module, down_req);
190 _PUBLIC_ const struct ldb_module_ops ldb_normalise_module_ops = {
192 .search = normalise_search,