Fix the mess with ldb includes.
[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_module.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_context *ldb;
90         struct ldb_request *down_req;
91         struct rename_context *ac;
92         struct ldb_message *msg;
93         struct ldb_message_element *attribute;
94         const struct ldb_schema_attribute *a;
95         const char *rdn_name;
96         struct ldb_val rdn_val;
97         int i, ret;
98
99         ldb = ldb_module_get_ctx(module);
100         ldb_debug(ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
101
102         /* do not manipulate our control entries */
103         if (ldb_dn_is_special(req->op.add.message->dn)) {
104                 return ldb_next_request(module, req);
105         }
106
107         ac = talloc_zero(req, struct rename_context);
108         if (ac == NULL) {
109                 return LDB_ERR_OPERATIONS_ERROR;
110         }
111
112         ac->module = module;
113         ac->req = req;
114
115         msg = ldb_msg_copy_shallow(req, req->op.add.message);
116         if (msg == NULL) {
117                 return LDB_ERR_OPERATIONS_ERROR;
118         }
119
120         rdn_name = ldb_dn_get_rdn_name(msg->dn);
121         if (rdn_name == NULL) {
122                 talloc_free(ac);
123                 return LDB_ERR_OPERATIONS_ERROR;
124         }
125         
126         rdn_val = ldb_val_dup(msg, ldb_dn_get_rdn_val(msg->dn));
127         
128         /* Perhaps someone above us tried to set this? */
129         if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
130                 attribute->num_values = 0;
131         }
132
133         if (ldb_msg_add_value(msg, "name", &rdn_val, NULL) != 0) {
134                 talloc_free(ac);
135                 return LDB_ERR_OPERATIONS_ERROR;
136         }
137
138         attribute = rdn_name_find_attribute(msg, rdn_name);
139
140         if (!attribute) {
141                 if (ldb_msg_add_value(msg, rdn_name, &rdn_val, NULL) != 0) {
142                         talloc_free(ac);
143                         return LDB_ERR_OPERATIONS_ERROR;
144                 }
145         } else {
146                 a = ldb_schema_attribute_by_name(ldb, rdn_name);
147
148                 for (i = 0; i < attribute->num_values; i++) {
149                         ret = a->syntax->comparison_fn(ldb, msg,
150                                         &rdn_val, &attribute->values[i]);
151                         if (ret == 0) {
152                                 /* overwrite so it matches in case */
153                                 attribute->values[i] = rdn_val;
154                                 break;
155                         }
156                 }
157                 if (i == attribute->num_values) {
158                         ldb_debug_set(ldb, LDB_DEBUG_FATAL, 
159                                       "RDN mismatch on %s: %s (%s)", 
160                                       ldb_dn_get_linearized(msg->dn), rdn_name, rdn_val.data);
161                         talloc_free(ac);
162                         /* Match AD's error here */
163                         return LDB_ERR_INVALID_DN_SYNTAX;
164                 }
165         }
166
167         ret = ldb_build_add_req(&down_req, ldb, req,
168                                 msg,
169                                 req->controls,
170                                 ac, rdn_name_add_callback,
171                                 req);
172         if (ret != LDB_SUCCESS) {
173                 return ret;
174         }
175
176         talloc_steal(down_req, msg);
177
178         /* go on with the call chain */
179         return ldb_next_request(module, down_req);
180 }
181
182 static int rdn_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
183 {
184         struct rename_context *ac;
185
186         ac = talloc_get_type(req->context, struct rename_context);
187
188         if (!ares) {
189                 return ldb_module_done(ac->req, NULL, NULL,
190                                         LDB_ERR_OPERATIONS_ERROR);
191         }
192         if (ares->error != LDB_SUCCESS) {
193                 return ldb_module_done(ac->req, ares->controls,
194                                         ares->response, ares->error);
195         }
196
197         /* the only supported reply right now is a LDB_REPLY_DONE */
198         if (ares->type != LDB_REPLY_DONE) {
199                 return ldb_module_done(ac->req, NULL, NULL,
200                                         LDB_ERR_OPERATIONS_ERROR);
201         }
202
203         /* send saved controls eventually */
204         return ldb_module_done(ac->req, ac->ares->controls,
205                                 ac->ares->response, LDB_SUCCESS);
206 }
207
208 static int rdn_rename_callback(struct ldb_request *req, struct ldb_reply *ares)
209 {
210         struct ldb_context *ldb;
211         struct rename_context *ac;
212         struct ldb_request *mod_req;
213         const char *rdn_name;
214         struct ldb_val rdn_val;
215         struct ldb_message *msg;
216         int ret;
217
218         ac = talloc_get_type(req->context, struct rename_context);
219         ldb = ldb_module_get_ctx(ac->module);
220
221         if (!ares) {
222                 goto error;
223         }
224         if (ares->error != LDB_SUCCESS) {
225                 return ldb_module_done(ac->req, ares->controls,
226                                         ares->response, ares->error);
227         }
228
229         /* the only supported reply right now is a LDB_REPLY_DONE */
230         if (ares->type != LDB_REPLY_DONE) {
231                 goto error;
232         }
233
234         /* save reply for caller */
235         ac->ares = talloc_steal(ac, ares);
236
237         msg = ldb_msg_new(ac);
238         if (msg == NULL) {
239                 goto error;
240         }
241         msg->dn = ldb_dn_copy(msg, ac->req->op.rename.newdn);
242         if (msg->dn == NULL) {
243                 goto error;
244         }
245         rdn_name = ldb_dn_get_rdn_name(ac->req->op.rename.newdn);
246         if (rdn_name == NULL) {
247                 goto error;
248         }
249         
250         rdn_val = ldb_val_dup(msg, ldb_dn_get_rdn_val(ac->req->op.rename.newdn));
251         
252         if (ldb_msg_add_empty(msg, rdn_name, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
253                 goto error;
254         }
255         if (ldb_msg_add_value(msg, rdn_name, &rdn_val, NULL) != 0) {
256                 goto error;
257         }
258         if (ldb_msg_add_empty(msg, "name", LDB_FLAG_MOD_REPLACE, NULL) != 0) {
259                 goto error;
260         }
261         if (ldb_msg_add_value(msg, "name", &rdn_val, NULL) != 0) {
262                 goto error;
263         }
264
265         ret = ldb_build_mod_req(&mod_req, ldb,
266                                 ac, msg, NULL,
267                                 ac, rdn_modify_callback,
268                                 req);
269         if (ret != LDB_SUCCESS) {
270                 return ldb_module_done(ac->req, NULL, NULL, ret);
271         }
272         talloc_steal(mod_req, msg);
273
274         /* do the mod call */
275         return ldb_request(ldb, mod_req);
276
277 error:
278         return ldb_module_done(ac->req, NULL, NULL,
279                                                 LDB_ERR_OPERATIONS_ERROR);
280 }
281
282 static int rdn_name_rename(struct ldb_module *module, struct ldb_request *req)
283 {
284         struct ldb_context *ldb;
285         struct rename_context *ac;
286         struct ldb_request *down_req;
287         int ret;
288
289         ldb = ldb_module_get_ctx(module);
290         ldb_debug(ldb, LDB_DEBUG_TRACE, "rdn_name_rename\n");
291
292         /* do not manipulate our control entries */
293         if (ldb_dn_is_special(req->op.rename.newdn)) {
294                 return ldb_next_request(module, req);
295         }
296
297         ac = talloc_zero(req, struct rename_context);
298         if (ac == NULL) {
299                 return LDB_ERR_OPERATIONS_ERROR;
300         }
301
302         ac->module = module;
303         ac->req = req;
304
305         ret = ldb_build_rename_req(&down_req,
306                                    ldb,
307                                    ac,
308                                    req->op.rename.olddn,
309                                    req->op.rename.newdn,
310                                    req->controls,
311                                    ac,
312                                    rdn_rename_callback,
313                                    req);
314
315         if (ret != LDB_SUCCESS) {
316                 return LDB_ERR_OPERATIONS_ERROR;
317         }
318
319         /* rename first, modify "name" if rename is ok */
320         return ldb_next_request(module, down_req);
321 }
322
323 const struct ldb_module_ops ldb_rdn_name_module_ops = {
324         .name              = "rdn_name",
325         .add               = rdn_name_add,
326         .rename            = rdn_name_rename,
327 };