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 const struct ldb_message *msg = req->op.add.message;
59 struct ldb_message *msg2;
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(msg->dn)) {
68 return ldb_next_request(module, req);
71 msg2 = talloc(module, struct ldb_message);
77 msg2->num_elements = msg->num_elements;
78 msg2->private_data = msg->private_data;
79 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
80 for (i = 0; i < msg2->num_elements; i++) {
81 msg2->elements[i] = msg->elements[i];
84 rdn = ldb_dn_get_rdn(msg2, msg2->dn);
90 /* Perhaps someone above us tried to set this? */
91 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
92 attribute->num_values = 0;
95 if (ldb_msg_add_value(msg2, "name", &rdn->value) != 0) {
100 attribute = rdn_name_find_attribute(msg2, rdn->name);
103 if (ldb_msg_add_value(msg2, rdn->name, &rdn->value) != 0) {
108 const struct ldb_attrib_handler *handler
109 = ldb_attrib_handler(module->ldb, rdn->name);
110 for (i=0; i < attribute->num_values; i++) {
111 if (handler->comparison_fn(module->ldb, msg2, &rdn->value, &attribute->values[i]) == 0) {
112 /* overwrite so it matches in case */
113 attribute->values[i] = rdn->value;
117 if (i == attribute->num_values) {
118 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
119 "RDN mismatch on %s: %s",
120 ldb_dn_linearize(msg2, msg2->dn), rdn->name);
126 req->op.add.message = msg2;
127 ret = ldb_next_request(module, req);
128 req->op.add.message = msg;
135 static int rdn_name_modify(struct ldb_module *module, struct ldb_request *req)
137 const struct ldb_message *msg = req->op.mod.message;
138 struct ldb_message *msg2;
139 struct ldb_message_element *attribute;
140 struct ldb_dn_component *rdn;
143 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_modify_record\n");
145 /* do not manipulate our control entries */
146 if (ldb_dn_is_special(msg->dn)) {
147 return ldb_next_request(module, req);
150 /* Perhaps someone above us knows better */
151 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
152 return ldb_next_request(module, req);
155 msg2 = talloc(module, struct ldb_message);
161 msg2->num_elements = msg->num_elements;
162 msg2->private_data = msg->private_data;
163 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
164 for (i = 0; i < msg2->num_elements; i++) {
165 msg2->elements[i] = msg->elements[i];
168 rdn = ldb_dn_get_rdn(msg2, msg2->dn);
174 if (ldb_msg_add_value(msg2, "name", &rdn->value) != 0) {
179 attribute = rdn_name_find_attribute(msg2, "name");
185 attribute->flags = LDB_FLAG_MOD_REPLACE;
187 req->op.add.message = msg2;
188 ret = ldb_next_request(module, req);
189 req->op.add.message = msg;
196 static int rdn_name_add_async(struct ldb_module *module, struct ldb_request *req)
198 struct ldb_request *down_req;
199 struct ldb_message *msg;
200 struct ldb_message_element *attribute;
201 struct ldb_dn_component *rdn;
204 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
206 /* do not manipulate our control entries */
207 if (ldb_dn_is_special(req->op.add.message->dn)) {
208 return ldb_next_request(module, req);
211 down_req = talloc(module, struct ldb_request);
212 if (down_req == NULL) {
213 return LDB_ERR_OPERATIONS_ERROR;
216 msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
218 return LDB_ERR_OPERATIONS_ERROR;
221 rdn = ldb_dn_get_rdn(msg, msg->dn);
223 talloc_free(down_req);
224 return LDB_ERR_OPERATIONS_ERROR;
227 /* Perhaps someone above us tried to set this? */
228 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
229 attribute->num_values = 0;
232 if (ldb_msg_add_value(msg, "name", &rdn->value) != 0) {
233 talloc_free(down_req);
234 return LDB_ERR_OPERATIONS_ERROR;
237 attribute = rdn_name_find_attribute(msg, rdn->name);
240 if (ldb_msg_add_value(msg, rdn->name, &rdn->value) != 0) {
241 talloc_free(down_req);
242 return LDB_ERR_OPERATIONS_ERROR;
245 const struct ldb_attrib_handler *handler = ldb_attrib_handler(module->ldb, rdn->name);
247 for (i = 0; i < attribute->num_values; i++) {
248 if (handler->comparison_fn(module->ldb, msg, &rdn->value, &attribute->values[i]) == 0) {
249 /* overwrite so it matches in case */
250 attribute->values[i] = rdn->value;
254 if (i == attribute->num_values) {
255 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
256 "RDN mismatch on %s: %s",
257 ldb_dn_linearize(msg, msg->dn), rdn->name);
258 talloc_free(down_req);
259 return LDB_ERR_OPERATIONS_ERROR;
263 down_req->op.add.message = msg;
265 down_req->controls = req->controls;
266 down_req->creds = req->creds;
268 down_req->async.context = req->async.context;
269 down_req->async.callback = req->async.callback;
270 down_req->async.timeout = req->async.timeout;
272 /* go on with the call chain */
273 ret = ldb_next_request(module, down_req);
275 /* do not free down_req as the call results may be linked to it,
276 * it will be freed when the upper level request get freed */
277 if (ret == LDB_SUCCESS) {
278 req->async.handle = down_req->async.handle;
284 static int rdn_name_modify_async(struct ldb_module *module, struct ldb_request *req)
286 struct ldb_request *down_req;
287 struct ldb_message *msg;
288 struct ldb_message_element *attribute;
289 struct ldb_dn_component *rdn;
292 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_modify_record\n");
294 /* do not manipulate our control entries */
295 if (ldb_dn_is_special(req->op.mod.message->dn)) {
296 return ldb_next_request(module, req);
299 /* Perhaps someone above us knows better */
300 if ((attribute = rdn_name_find_attribute(req->op.mod.message, "name")) != NULL ) {
301 return ldb_next_request(module, req);
304 /* FIXME: are we sure we wont to change "name" on each and every modify operation ?? */
305 down_req = talloc(module, struct ldb_request);
306 if (down_req == NULL) {
307 return LDB_ERR_OPERATIONS_ERROR;
310 msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
312 return LDB_ERR_OPERATIONS_ERROR;
315 rdn = ldb_dn_get_rdn(msg, msg->dn);
317 talloc_free(down_req);
318 return LDB_ERR_OPERATIONS_ERROR;
321 if (ldb_msg_add_value(msg, "name", &rdn->value) != 0) {
322 talloc_free(down_req);
323 return LDB_ERR_OPERATIONS_ERROR;
326 attribute = rdn_name_find_attribute(msg, "name");
328 talloc_free(down_req);
329 return LDB_ERR_OPERATIONS_ERROR;
332 attribute->flags = LDB_FLAG_MOD_REPLACE;
334 down_req->op.add.message = msg;
336 down_req->controls = req->controls;
337 down_req->creds = req->creds;
339 down_req->async.context = req->async.context;
340 down_req->async.callback = req->async.callback;
341 down_req->async.timeout = req->async.timeout;
343 /* go on with the call chain */
344 ret = ldb_next_request(module, down_req);
346 /* do not free down_req as the call results may be linked to it,
347 * it will be freed when the upper level request get freed */
348 if (ret == LDB_SUCCESS) {
349 req->async.handle = down_req->async.handle;
355 static int rdn_name_request(struct ldb_module *module, struct ldb_request *req)
357 switch (req->operation) {
360 return rdn_name_add(module, req);
363 return rdn_name_modify(module, req);
366 return rdn_name_add_async(module, req);
368 case LDB_ASYNC_MODIFY:
369 return rdn_name_modify_async(module, req);
372 return ldb_next_request(module, req);
377 static const struct ldb_module_ops rdn_name_ops = {
379 .request = rdn_name_request
383 int ldb_rdn_name_init(void)
385 return ldb_register_module(&rdn_name_ops);