Use ldb_dn_from_ldb_val to avoid possible over-run of the value.
[kai/samba.git] / source4 / dsdb / samdb / ldb_modules / normalise.c
1 /* 
2    ldb database library
3
4    Copyright (C) Amdrew Bartlett <abartlet@samba.org> 2007-2008
5
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.
10    
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.
15    
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/>.
18 */
19
20 /*
21  *  Name: ldb
22  *
23  *  Component: ldb normalisation module
24  *
25  *  Description: module to ensure all DNs and attribute names are normalised
26  *
27  *  Author: Andrew Bartlett
28  */
29
30 #include "includes.h"
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"
35
36 /* Fix up the DN to be in the standard form, taking particular care to match the parent DN
37
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
42
43    The resulting DN should be:
44
45     CN=Admins,CN=Users,DC=samba,DC=example,DC=com
46    
47  */
48
49 struct norm_context {
50         struct ldb_module *module;
51         struct ldb_request *req;
52
53         const struct dsdb_schema *schema;
54 };
55
56 static int fix_dn(struct ldb_dn *dn) 
57 {
58         int i, ret;
59         char *upper_rdn_attr;
60
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;
67                 }
68                 
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) {
74                         return ret;
75                 }
76         }
77         return LDB_SUCCESS;
78 }
79
80 static int normalize_search_callback(struct ldb_request *req, struct ldb_reply *ares)
81 {
82         struct ldb_message *msg;
83         struct norm_context *ac;
84         int i, j, ret;
85
86         ac = talloc_get_type(req->context, struct norm_context);
87
88         if (!ares) {
89                 return ldb_module_done(ac->req, NULL, NULL,
90                                         LDB_ERR_OPERATIONS_ERROR);
91         }
92         if (ares->error != LDB_SUCCESS) {
93                 return ldb_module_done(ac->req, ares->controls,
94                                         ares->response, ares->error);
95         }
96
97         /* Only entries are interesting, and we handle the case of the parent seperatly */
98
99         switch (ares->type) {
100         case LDB_REPLY_ENTRY:
101
102                 /* OK, we have one of *many* search results passing by here,
103                  * but we should get them one at a time */
104                 msg = ares->message;
105
106                 ret = fix_dn(msg->dn);
107                 if (ret != LDB_SUCCESS) {
108                         return ldb_module_done(ac->req, NULL, NULL, ret);
109                 }
110
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);
113                         if (!attribute) {
114                                 continue;
115                         }
116                         /* Look to see if this attributeSyntax is a DN */
117                         if (!((strcmp(attribute->attributeSyntax_oid, "2.5.5.1") == 0) ||
118                               (strcmp(attribute->attributeSyntax_oid, "2.5.5.7") == 0))) {
119                                 continue;
120                         }
121                         for (j = 0; j < msg->elements[i].num_values; j++) {
122                                 const char *dn_str;
123                                 struct ldb_dn *dn = ldb_dn_from_ldb_val(ac, ac->module->ldb, &msg->elements[i].values[j]);
124                                 if (!dn) {
125                                         return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
126                                 }
127                                 ret = fix_dn(dn);
128                                 if (ret != LDB_SUCCESS) {
129                                         return ldb_module_done(ac->req, NULL, NULL, ret);
130                                 }
131                                 dn_str = talloc_steal(msg->elements[i].values, ldb_dn_get_linearized(dn));
132                                 msg->elements[i].values[j] = data_blob_string_const(dn_str);
133                                 talloc_free(dn);
134                         }
135                 }
136
137                 return ldb_module_send_entry(ac->req, msg);
138
139         case LDB_REPLY_REFERRAL:
140
141                 return ldb_module_send_referral(ac->req, ares->referral);
142
143         case LDB_REPLY_DONE:
144
145                 return ldb_module_done(ac->req, ares->controls,
146                                         ares->response, ares->error);
147         }
148
149         return LDB_SUCCESS;
150 }
151
152 /* search */
153 static int normalise_search(struct ldb_module *module, struct ldb_request *req)
154 {
155         struct ldb_request *down_req;
156         struct norm_context *ac;
157         int ret;
158
159         ac = talloc(req, struct norm_context);
160         if (ac == NULL) {
161                 return LDB_ERR_OPERATIONS_ERROR;
162         }
163
164         ac->module = module;
165         ac->req = req;
166
167         /* if schema not yet present just skip over */
168         ac->schema = dsdb_get_schema(ac->module->ldb);
169         if (ac->schema == NULL) {
170                 talloc_free(ac);
171                 return ldb_next_request(module, req);
172         }
173
174         ret = ldb_build_search_req_ex(&down_req, module->ldb, ac,
175                                         req->op.search.base,
176                                         req->op.search.scope,
177                                         req->op.search.tree,
178                                         req->op.search.attrs,
179                                         req->controls,
180                                         ac, normalize_search_callback,
181                                         req);
182         if (ret != LDB_SUCCESS) {
183                 return LDB_ERR_OPERATIONS_ERROR;
184         }
185
186         return ldb_next_request(module, down_req);
187 }
188
189
190
191 _PUBLIC_ const struct ldb_module_ops ldb_normalise_module_ops = {
192         .name              = "normalise",
193         .search            = normalise_search,
194 };