r23795: more v2->v3 conversion
[samba.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
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, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 */
25
26 /*
27  *  Name: rdb_name
28  *
29  *  Component: ldb rdn name module
30  *
31  *  Description: keep a consistent name attribute on objects manpulations
32  *
33  *  Author: Andrew Bartlet
34  *
35  *  Modifications:
36  *    - made the module async
37  *      Simo Sorce Mar 2006
38  */
39
40 #include "ldb_includes.h"
41
42 static struct ldb_message_element *rdn_name_find_attribute(const struct ldb_message *msg, const char *name)
43 {
44         int i;
45
46         for (i = 0; i < msg->num_elements; i++) {
47                 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
48                         return &msg->elements[i];
49                 }
50         }
51
52         return NULL;
53 }
54
55 static int rdn_name_add(struct ldb_module *module, struct ldb_request *req)
56 {
57         struct ldb_request *down_req;
58         struct ldb_message *msg;
59         struct ldb_message_element *attribute;
60         const char *rdn_name;
61         struct ldb_val rdn_val;
62         int i, ret;
63
64         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
65
66         /* do not manipulate our control entries */
67         if (ldb_dn_is_special(req->op.add.message->dn)) {
68                 return ldb_next_request(module, req);
69         }
70
71         down_req = talloc(req, struct ldb_request);
72         if (down_req == NULL) {
73                 return LDB_ERR_OPERATIONS_ERROR;
74         }
75
76         *down_req = *req;
77
78         down_req->op.add.message = msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
79         if (msg == NULL) {
80                 return LDB_ERR_OPERATIONS_ERROR;
81         }
82
83         rdn_name = ldb_dn_get_rdn_name(msg->dn);
84         if (rdn_name == NULL) {
85                 talloc_free(down_req);
86                 return LDB_ERR_OPERATIONS_ERROR;
87         }
88         
89         rdn_val = ldb_val_dup(msg, ldb_dn_get_rdn_val(msg->dn));
90         
91         /* Perhaps someone above us tried to set this? */
92         if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
93                 attribute->num_values = 0;
94         }
95
96         if (ldb_msg_add_value(msg, "name", &rdn_val, NULL) != 0) {
97                 talloc_free(down_req);
98                 return LDB_ERR_OPERATIONS_ERROR;
99         }
100
101         attribute = rdn_name_find_attribute(msg, rdn_name);
102
103         if (!attribute) {
104                 if (ldb_msg_add_value(msg, rdn_name, &rdn_val, NULL) != 0) {
105                         talloc_free(down_req);
106                         return LDB_ERR_OPERATIONS_ERROR;
107                 }
108         } else {
109                 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(module->ldb, rdn_name);
110
111                 for (i = 0; i < attribute->num_values; i++) {
112                         if (a->syntax->comparison_fn(module->ldb, msg, &rdn_val, &attribute->values[i]) == 0) {
113                                 /* overwrite so it matches in case */
114                                 attribute->values[i] = rdn_val;
115                                 break;
116                         }
117                 }
118                 if (i == attribute->num_values) {
119                         ldb_debug_set(module->ldb, LDB_DEBUG_FATAL, 
120                                       "RDN mismatch on %s: %s (%s)", 
121                                       ldb_dn_get_linearized(msg->dn), rdn_name, rdn_val.data);
122                         talloc_free(down_req);
123                         return LDB_ERR_OPERATIONS_ERROR;
124                 }
125         }
126
127         /* go on with the call chain */
128         ret = ldb_next_request(module, down_req);
129
130         /* do not free down_req as the call results may be linked to it,
131          * it will be freed when the upper level request get freed */
132         if (ret == LDB_SUCCESS) {
133                 req->handle = down_req->handle;
134         }
135
136         return ret;
137 }
138
139 struct rename_context {
140
141         enum {RENAME_RENAME, RENAME_MODIFY} step;
142         struct ldb_request *orig_req;
143         struct ldb_request *down_req;
144         struct ldb_request *mod_req;
145 };
146
147 static int rdn_name_rename(struct ldb_module *module, struct ldb_request *req)
148 {
149         struct ldb_handle *h;
150         struct rename_context *ac;
151
152         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_rename\n");
153
154         /* do not manipulate our control entries */
155         if (ldb_dn_is_special(req->op.rename.newdn)) {
156                 return ldb_next_request(module, req);
157         }
158
159         h = talloc_zero(req, struct ldb_handle);
160         if (h == NULL) {
161                 return LDB_ERR_OPERATIONS_ERROR;
162         }
163
164         h->module = module;
165
166         ac = talloc_zero(h, struct rename_context);
167         if (ac == NULL) {
168                 return LDB_ERR_OPERATIONS_ERROR;
169         }
170
171         h->private_data = (void *)ac;
172
173         h->state = LDB_ASYNC_INIT;
174         h->status = LDB_SUCCESS;
175
176         ac->orig_req = req;
177         ac->down_req = talloc(req, struct ldb_request);
178         if (ac->down_req == NULL) {
179                 return LDB_ERR_OPERATIONS_ERROR;
180         }
181
182         *(ac->down_req) = *req;
183
184         ac->step = RENAME_RENAME;
185
186         req->handle = h;
187
188         /* rename first, modify "name" if rename is ok */
189         return ldb_next_request(module, ac->down_req);
190 }
191
192 static int rdn_name_rename_do_mod(struct ldb_handle *h) {
193
194         struct rename_context *ac;
195         const char *rdn_name;
196         struct ldb_val rdn_val;
197         struct ldb_message *msg;
198
199         ac = talloc_get_type(h->private_data, struct rename_context);
200
201         ac->mod_req = talloc_zero(ac, struct ldb_request);
202
203         ac->mod_req->operation = LDB_MODIFY;
204         ac->mod_req->op.mod.message = msg = ldb_msg_new(ac->mod_req);
205         if (msg == NULL) {
206                 return LDB_ERR_OPERATIONS_ERROR;
207         }
208
209         msg->dn = ldb_dn_copy(msg, ac->orig_req->op.rename.newdn);
210         if (msg->dn == NULL) {
211                 return LDB_ERR_OPERATIONS_ERROR;
212         }
213
214         rdn_name = ldb_dn_get_rdn_name(ac->orig_req->op.rename.newdn);
215         if (rdn_name == NULL) {
216                 return LDB_ERR_OPERATIONS_ERROR;
217         }
218         
219         rdn_val = ldb_val_dup(msg, ldb_dn_get_rdn_val(ac->orig_req->op.rename.newdn));
220         
221         if (ldb_msg_add_empty(msg, rdn_name, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
222                 return LDB_ERR_OPERATIONS_ERROR;
223         }
224         if (ldb_msg_add_value(msg, rdn_name, &rdn_val, NULL) != 0) {
225                 return LDB_ERR_OPERATIONS_ERROR;
226         }
227         if (ldb_msg_add_empty(msg, "name", LDB_FLAG_MOD_REPLACE, NULL) != 0) {
228                 return LDB_ERR_OPERATIONS_ERROR;
229         }
230         if (ldb_msg_add_value(msg, "name", &rdn_val, NULL) != 0) {
231                 return LDB_ERR_OPERATIONS_ERROR;
232         }
233
234         ldb_set_timeout_from_prev_req(h->module->ldb, ac->orig_req, ac->mod_req);
235
236         ac->step = RENAME_MODIFY;
237
238         /* do the mod call */
239         return ldb_request(h->module->ldb, ac->mod_req);
240 }
241
242 static int rename_wait(struct ldb_handle *handle)
243 {
244         struct rename_context *ac;
245         int ret;
246     
247         if (!handle || !handle->private_data) {
248                 return LDB_ERR_OPERATIONS_ERROR;
249         }
250
251         if (handle->state == LDB_ASYNC_DONE) {
252                 return handle->status;
253         }
254
255         handle->state = LDB_ASYNC_PENDING;
256         handle->status = LDB_SUCCESS;
257
258         ac = talloc_get_type(handle->private_data, struct rename_context);
259
260         switch(ac->step) {
261         case RENAME_RENAME:
262                 ret = ldb_wait(ac->down_req->handle, LDB_WAIT_NONE);
263                 if (ret != LDB_SUCCESS) {
264                         handle->status = ret;
265                         goto done;
266                 }
267                 if (ac->down_req->handle->status != LDB_SUCCESS) {
268                         handle->status = ac->down_req->handle->status;
269                         goto done;
270                 }
271
272                 if (ac->down_req->handle->state != LDB_ASYNC_DONE) {
273                         return LDB_SUCCESS;
274                 }
275
276                 /* rename operation done */
277                 return rdn_name_rename_do_mod(handle);
278
279         case RENAME_MODIFY:
280                 ret = ldb_wait(ac->mod_req->handle, LDB_WAIT_NONE);
281                 if (ret != LDB_SUCCESS) {
282                         handle->status = ret;
283                         goto done;
284                 }
285                 if (ac->mod_req->handle->status != LDB_SUCCESS) {
286                         handle->status = ac->mod_req->handle->status;
287                         goto done;
288                 }
289
290                 if (ac->mod_req->handle->state != LDB_ASYNC_DONE) {
291                         return LDB_SUCCESS;
292                 }
293
294                 break;
295
296         default:
297                 ret = LDB_ERR_OPERATIONS_ERROR;
298                 goto done;
299         }
300
301         ret = LDB_SUCCESS;
302
303 done:
304         handle->state = LDB_ASYNC_DONE;
305         return ret;
306 }
307
308 static int rename_wait_all(struct ldb_handle *handle) {
309
310         int ret;
311
312         while (handle->state != LDB_ASYNC_DONE) {
313                 ret = rename_wait(handle);
314                 if (ret != LDB_SUCCESS) {
315                         return ret;
316                 }
317         }
318
319         return handle->status;
320 }
321
322 static int rdn_name_wait(struct ldb_handle *handle, enum ldb_wait_type type)
323 {
324         if (type == LDB_WAIT_ALL) {
325                 return rename_wait_all(handle);
326         } else {
327                 return rename_wait(handle);
328         }
329 }
330
331 static const struct ldb_module_ops rdn_name_ops = {
332         .name              = "rdn_name",
333         .add               = rdn_name_add,
334         .rename            = rdn_name_rename,
335         .wait              = rdn_name_wait
336 };
337
338
339 int ldb_rdn_name_init(void)
340 {
341         return ldb_register_module(&rdn_name_ops);
342 }