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