62b8ce5112c1f6ef13a404b481f0fccff8bcef5a
[metze/samba/wip.git] / source4 / lib / ldb / modules / rdn_name.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Bartlet 2005
5    Copyright (C) Simo Sorce     2006-2008
6
7      ** NOTE! The following LGPL license applies to the ldb
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10    
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 3 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 */
24
25 /*
26  *  Name: rdb_name
27  *
28  *  Component: ldb rdn name module
29  *
30  *  Description: keep a consistent name attribute on objects manpulations
31  *
32  *  Author: Andrew Bartlet
33  *
34  *  Modifications:
35  *    - made the module async
36  *      Simo Sorce Mar 2006
37  */
38
39 #include "ldb_includes.h"
40
41 struct rename_context {
42
43         struct ldb_module *module;
44         struct ldb_request *req;
45
46         struct ldb_reply *ares;
47 };
48
49 static struct ldb_message_element *rdn_name_find_attribute(const struct ldb_message *msg, const char *name)
50 {
51         int i;
52
53         for (i = 0; i < msg->num_elements; i++) {
54                 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
55                         return &msg->elements[i];
56                 }
57         }
58
59         return NULL;
60 }
61
62 static int rdn_name_add_callback(struct ldb_request *req,
63                                  struct ldb_reply *ares)
64 {
65         struct rename_context *ac;
66
67         ac = talloc_get_type(req->context, struct rename_context);
68
69         if (!ares) {
70                 return ldb_module_done(ac->req, NULL, NULL,
71                                         LDB_ERR_OPERATIONS_ERROR);
72         }
73         if (ares->error != LDB_SUCCESS) {
74                 return ldb_module_done(ac->req, ares->controls,
75                                         ares->response, ares->error);
76         }
77
78         if (ares->type != LDB_REPLY_DONE) {
79                 return ldb_module_done(ac->req, NULL, NULL,
80                                         LDB_ERR_OPERATIONS_ERROR);
81         }
82
83         return ldb_module_done(ac->req, ares->controls,
84                                         ares->response, LDB_SUCCESS);
85 }
86
87 static int rdn_name_add(struct ldb_module *module, struct ldb_request *req)
88 {
89         struct ldb_request *down_req;
90         struct rename_context *ac;
91         struct ldb_message *msg;
92         struct ldb_message_element *attribute;
93         const struct ldb_schema_attribute *a;
94         const char *rdn_name;
95         struct ldb_val rdn_val;
96         int i, ret;
97
98         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
99
100         /* do not manipulate our control entries */
101         if (ldb_dn_is_special(req->op.add.message->dn)) {
102                 return ldb_next_request(module, req);
103         }
104
105         ac = talloc_zero(req, struct rename_context);
106         if (ac == NULL) {
107                 return LDB_ERR_OPERATIONS_ERROR;
108         }
109
110         ac->module = module;
111         ac->req = req;
112
113         msg = ldb_msg_copy_shallow(req, req->op.add.message);
114         if (msg == NULL) {
115                 return LDB_ERR_OPERATIONS_ERROR;
116         }
117
118         rdn_name = ldb_dn_get_rdn_name(msg->dn);
119         if (rdn_name == NULL) {
120                 talloc_free(ac);
121                 return LDB_ERR_OPERATIONS_ERROR;
122         }
123         
124         rdn_val = ldb_val_dup(msg, ldb_dn_get_rdn_val(msg->dn));
125         
126         /* Perhaps someone above us tried to set this? */
127         if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
128                 attribute->num_values = 0;
129         }
130
131         if (ldb_msg_add_value(msg, "name", &rdn_val, NULL) != 0) {
132                 talloc_free(ac);
133                 return LDB_ERR_OPERATIONS_ERROR;
134         }
135
136         attribute = rdn_name_find_attribute(msg, rdn_name);
137
138         if (!attribute) {
139                 if (ldb_msg_add_value(msg, rdn_name, &rdn_val, NULL) != 0) {
140                         talloc_free(ac);
141                         return LDB_ERR_OPERATIONS_ERROR;
142                 }
143         } else {
144                 a = ldb_schema_attribute_by_name(module->ldb, rdn_name);
145
146                 for (i = 0; i < attribute->num_values; i++) {
147                         ret = a->syntax->comparison_fn(module->ldb, msg,
148                                         &rdn_val, &attribute->values[i]);
149                         if (ret == 0) {
150                                 /* overwrite so it matches in case */
151                                 attribute->values[i] = rdn_val;
152                                 break;
153                         }
154                 }
155                 if (i == attribute->num_values) {
156                         ldb_debug_set(module->ldb, LDB_DEBUG_FATAL, 
157                                       "RDN mismatch on %s: %s (%s)", 
158                                       ldb_dn_get_linearized(msg->dn), rdn_name, rdn_val.data);
159                         talloc_free(ac);
160                         /* Match AD's error here */
161                         return LDB_ERR_INVALID_DN_SYNTAX;
162                 }
163         }
164
165         ret = ldb_build_add_req(&down_req, module->ldb, req,
166                                 msg,
167                                 req->controls,
168                                 ac, rdn_name_add_callback,
169                                 req);
170         if (ret != LDB_SUCCESS) {
171                 return ret;
172         }
173
174         talloc_steal(down_req, msg);
175
176         /* go on with the call chain */
177         return ldb_next_request(module, down_req);
178 }
179
180 static int rdn_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
181 {
182         struct rename_context *ac;
183
184         ac = talloc_get_type(req->context, struct rename_context);
185
186         if (!ares) {
187                 return ldb_module_done(ac->req, NULL, NULL,
188                                         LDB_ERR_OPERATIONS_ERROR);
189         }
190         if (ares->error != LDB_SUCCESS) {
191                 return ldb_module_done(ac->req, ares->controls,
192                                         ares->response, ares->error);
193         }
194
195         /* the only supported reply right now is a LDB_REPLY_DONE */
196         if (ares->type != LDB_REPLY_DONE) {
197                 return ldb_module_done(ac->req, NULL, NULL,
198                                         LDB_ERR_OPERATIONS_ERROR);
199         }
200
201         /* send saved controls eventually */
202         return ldb_module_done(ac->req, ac->ares->controls,
203                                 ac->ares->response, LDB_SUCCESS);
204 }
205
206 static int rdn_rename_callback(struct ldb_request *req, struct ldb_reply *ares)
207 {
208         struct rename_context *ac;
209         struct ldb_request *mod_req;
210         const char *rdn_name;
211         struct ldb_val rdn_val;
212         struct ldb_message *msg;
213         int ret;
214
215         ac = talloc_get_type(req->context, struct rename_context);
216
217         if (!ares) {
218                 goto error;
219         }
220         if (ares->error != LDB_SUCCESS) {
221                 return ldb_module_done(ac->req, ares->controls,
222                                         ares->response, ares->error);
223         }
224
225         /* the only supported reply right now is a LDB_REPLY_DONE */
226         if (ares->type != LDB_REPLY_DONE) {
227                 goto error;
228         }
229
230         /* save reply for caller */
231         ac->ares = talloc_steal(ac, ares);
232
233         msg = ldb_msg_new(ac);
234         if (msg == NULL) {
235                 goto error;
236         }
237         msg->dn = ldb_dn_copy(msg, ac->req->op.rename.newdn);
238         if (msg->dn == NULL) {
239                 goto error;
240         }
241         rdn_name = ldb_dn_get_rdn_name(ac->req->op.rename.newdn);
242         if (rdn_name == NULL) {
243                 goto error;
244         }
245         
246         rdn_val = ldb_val_dup(msg, ldb_dn_get_rdn_val(ac->req->op.rename.newdn));
247         
248         if (ldb_msg_add_empty(msg, rdn_name, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
249                 goto error;
250         }
251         if (ldb_msg_add_value(msg, rdn_name, &rdn_val, NULL) != 0) {
252                 goto error;
253         }
254         if (ldb_msg_add_empty(msg, "name", LDB_FLAG_MOD_REPLACE, NULL) != 0) {
255                 goto error;
256         }
257         if (ldb_msg_add_value(msg, "name", &rdn_val, NULL) != 0) {
258                 goto error;
259         }
260
261         ret = ldb_build_mod_req(&mod_req, ac->module->ldb,
262                                 ac, msg, NULL,
263                                 ac, rdn_modify_callback,
264                                 req);
265         if (ret != LDB_SUCCESS) {
266                 return ldb_module_done(ac->req, NULL, NULL, ret);
267         }
268         talloc_steal(mod_req, msg);
269
270         /* do the mod call */
271         return ldb_request(ac->module->ldb, mod_req);
272
273 error:
274         return ldb_module_done(ac->req, NULL, NULL,
275                                                 LDB_ERR_OPERATIONS_ERROR);
276 }
277
278 static int rdn_name_rename(struct ldb_module *module, struct ldb_request *req)
279 {
280         struct rename_context *ac;
281         struct ldb_request *down_req;
282         int ret;
283
284         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_rename\n");
285
286         /* do not manipulate our control entries */
287         if (ldb_dn_is_special(req->op.rename.newdn)) {
288                 return ldb_next_request(module, req);
289         }
290
291         ac = talloc_zero(req, struct rename_context);
292         if (ac == NULL) {
293                 return LDB_ERR_OPERATIONS_ERROR;
294         }
295
296         ac->module = module;
297         ac->req = req;
298
299         ret = ldb_build_rename_req(&down_req,
300                                    module->ldb,
301                                    ac,
302                                    req->op.rename.olddn,
303                                    req->op.rename.newdn,
304                                    req->controls,
305                                    ac,
306                                    rdn_rename_callback,
307                                    req);
308
309         if (ret != LDB_SUCCESS) {
310                 return LDB_ERR_OPERATIONS_ERROR;
311         }
312
313         /* rename first, modify "name" if rename is ok */
314         return ldb_next_request(module, down_req);
315 }
316
317 const struct ldb_module_ops ldb_rdn_name_module_ops = {
318         .name              = "rdn_name",
319         .add               = rdn_name_add,
320         .rename            = rdn_name_rename,
321 };