4 Copyright (C) Andrew Bartlet 2005
5 Copyright (C) Simo Sorce 2006
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
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 2 of the License, or (at your option) any later version.
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.
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
29 * Component: ldb rdn name module
31 * Description: keep a consistent name attribute on objects manpulations
33 * Author: Andrew Bartlet
36 * - made the module async
41 #include "ldb/include/includes.h"
43 static struct ldb_message_element *rdn_name_find_attribute(const struct ldb_message *msg, const char *name)
47 for (i = 0; i < msg->num_elements; i++) {
48 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
49 return &msg->elements[i];
56 static int rdn_name_add(struct ldb_module *module, struct ldb_request *req)
58 struct ldb_request *down_req;
59 struct ldb_message *msg;
60 struct ldb_message_element *attribute;
61 struct ldb_dn_component *rdn;
64 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
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);
71 down_req = talloc(req, struct ldb_request);
72 if (down_req == NULL) {
73 return LDB_ERR_OPERATIONS_ERROR;
78 down_req->op.add.message = msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
80 return LDB_ERR_OPERATIONS_ERROR;
83 rdn = ldb_dn_get_rdn(msg, msg->dn);
85 talloc_free(down_req);
86 return LDB_ERR_OPERATIONS_ERROR;
89 /* Perhaps someone above us tried to set this? */
90 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
91 attribute->num_values = 0;
94 if (ldb_msg_add_value(msg, "name", &rdn->value) != 0) {
95 talloc_free(down_req);
96 return LDB_ERR_OPERATIONS_ERROR;
99 attribute = rdn_name_find_attribute(msg, rdn->name);
102 if (ldb_msg_add_value(msg, rdn->name, &rdn->value) != 0) {
103 talloc_free(down_req);
104 return LDB_ERR_OPERATIONS_ERROR;
107 const struct ldb_attrib_handler *handler = ldb_attrib_handler(module->ldb, rdn->name);
109 for (i = 0; i < attribute->num_values; i++) {
110 if (handler->comparison_fn(module->ldb, msg, &rdn->value, &attribute->values[i]) == 0) {
111 /* overwrite so it matches in case */
112 attribute->values[i] = rdn->value;
116 if (i == attribute->num_values) {
117 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
118 "RDN mismatch on %s: %s",
119 ldb_dn_linearize(msg, msg->dn), rdn->name);
120 talloc_free(down_req);
121 return LDB_ERR_OPERATIONS_ERROR;
125 /* go on with the call chain */
126 ret = ldb_next_request(module, down_req);
128 /* do not free down_req as the call results may be linked to it,
129 * it will be freed when the upper level request get freed */
130 if (ret == LDB_SUCCESS) {
131 req->async.handle = down_req->async.handle;
137 struct rename_async_context {
139 enum {RENAME_RENAME, RENAME_MODIFY} step;
140 struct ldb_request *orig_req;
141 struct ldb_request *down_req;
142 struct ldb_request *mod_req;
145 static int rdn_name_rename(struct ldb_module *module, struct ldb_request *req)
147 struct ldb_async_handle *h;
148 struct rename_async_context *ac;
150 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_rename\n");
152 /* do not manipulate our control entries */
153 if (ldb_dn_is_special(req->op.rename.newdn)) {
154 return ldb_next_request(module, req);
157 h = talloc_zero(req, struct ldb_async_handle);
159 return LDB_ERR_OPERATIONS_ERROR;
164 ac = talloc_zero(h, struct rename_async_context);
166 return LDB_ERR_OPERATIONS_ERROR;
169 h->private_data = (void *)ac;
171 h->state = LDB_ASYNC_INIT;
172 h->status = LDB_SUCCESS;
175 ac->down_req = talloc(req, struct ldb_request);
176 if (ac->down_req == NULL) {
177 return LDB_ERR_OPERATIONS_ERROR;
180 *(ac->down_req) = *req;
182 ac->step = RENAME_RENAME;
184 req->async.handle = h;
186 /* rename first, modify "name" if rename is ok */
187 return ldb_next_request(module, ac->down_req);
190 static int rdn_name_rename_do_mod(struct ldb_async_handle *h) {
192 struct rename_async_context *ac;
193 struct ldb_dn_component *rdn;
194 struct ldb_message *msg;
196 ac = talloc_get_type(h->private_data, struct rename_async_context);
198 rdn = ldb_dn_get_rdn(ac, ac->orig_req->op.rename.newdn);
200 return LDB_ERR_OPERATIONS_ERROR;
203 ac->mod_req = talloc_zero(ac, struct ldb_request);
205 ac->mod_req->operation = LDB_MODIFY;
206 ac->mod_req->op.mod.message = msg = ldb_msg_new(ac->mod_req);
208 return LDB_ERR_OPERATIONS_ERROR;
211 msg->dn = ldb_dn_copy(msg, ac->orig_req->op.rename.newdn);
212 if (msg->dn == NULL) {
213 return LDB_ERR_OPERATIONS_ERROR;
216 if (ldb_msg_add_empty(msg, rdn->name, LDB_FLAG_MOD_REPLACE) != 0) {
217 return LDB_ERR_OPERATIONS_ERROR;
219 if (ldb_msg_add_value(msg, rdn->name, &rdn->value) != 0) {
220 return LDB_ERR_OPERATIONS_ERROR;
222 if (ldb_msg_add_empty(msg, "name", LDB_FLAG_MOD_REPLACE) != 0) {
223 return LDB_ERR_OPERATIONS_ERROR;
225 if (ldb_msg_add_value(msg, "name", &rdn->value) != 0) {
226 return LDB_ERR_OPERATIONS_ERROR;
229 ac->mod_req->async.timeout = ac->orig_req->async.timeout;
231 ac->step = RENAME_MODIFY;
233 /* do the mod call */
234 return ldb_request(h->module->ldb, ac->mod_req);
237 static int rename_async_wait(struct ldb_async_handle *handle)
239 struct rename_async_context *ac;
242 if (!handle || !handle->private_data) {
243 return LDB_ERR_OPERATIONS_ERROR;
246 if (handle->state == LDB_ASYNC_DONE) {
247 return handle->status;
250 handle->state = LDB_ASYNC_PENDING;
251 handle->status = LDB_SUCCESS;
253 ac = talloc_get_type(handle->private_data, struct rename_async_context);
257 ret = ldb_async_wait(ac->down_req->async.handle, LDB_WAIT_NONE);
258 if (ret != LDB_SUCCESS) {
259 handle->status = ret;
262 if (ac->down_req->async.handle->status != LDB_SUCCESS) {
263 handle->status = ac->down_req->async.handle->status;
267 if (ac->down_req->async.handle->state != LDB_ASYNC_DONE) {
271 /* rename operation done */
272 return rdn_name_rename_do_mod(handle);
275 ret = ldb_async_wait(ac->mod_req->async.handle, LDB_WAIT_NONE);
276 if (ret != LDB_SUCCESS) {
277 handle->status = ret;
280 if (ac->mod_req->async.handle->status != LDB_SUCCESS) {
281 handle->status = ac->mod_req->async.handle->status;
285 if (ac->mod_req->async.handle->state != LDB_ASYNC_DONE) {
292 ret = LDB_ERR_OPERATIONS_ERROR;
299 handle->state = LDB_ASYNC_DONE;
303 static int rename_async_wait_all(struct ldb_async_handle *handle) {
307 while (handle->state != LDB_ASYNC_DONE) {
308 ret = rename_async_wait(handle);
309 if (ret != LDB_SUCCESS) {
314 return handle->status;
317 static int rdn_name_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
319 if (type == LDB_WAIT_ALL) {
320 return rename_async_wait_all(handle);
322 return rename_async_wait(handle);
326 static const struct ldb_module_ops rdn_name_ops = {
329 .rename = rdn_name_rename,
330 .async_wait = rdn_name_async_wait
334 int ldb_rdn_name_init(void)
336 return ldb_register_module(&rdn_name_ops);