4 Copyright (C) Simo Sorce 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb objectguid module
30 * Description: add a unique objectGUID onto every new record
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
40 static struct ldb_message_element *rdn_name_find_attribute(const struct ldb_message *msg, const char *name)
44 for (i = 0; i < msg->num_elements; i++) {
45 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
46 return &msg->elements[i];
53 static int rdn_name_add(struct ldb_module *module, struct ldb_request *req)
55 const struct ldb_message *msg = req->op.add.message;
56 struct ldb_message *msg2;
57 struct ldb_message_element *attribute;
58 struct ldb_dn_component *rdn;
61 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
63 /* do not manipulate our control entries */
64 if (ldb_dn_is_special(msg->dn)) {
65 return ldb_next_request(module, req);
68 /* Perhaps someone above us knows better */
69 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
70 return ldb_next_request(module, req);
73 msg2 = talloc(module, struct ldb_message);
79 msg2->num_elements = msg->num_elements;
80 msg2->private_data = msg->private_data;
81 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
82 for (i = 0; i < msg2->num_elements; i++) {
83 msg2->elements[i] = msg->elements[i];
86 rdn = ldb_dn_get_rdn(msg2, msg2->dn);
92 if (ldb_msg_add_value(msg2, "name", &rdn->value) != 0) {
97 attribute = rdn_name_find_attribute(msg2, rdn->name);
100 if (ldb_msg_add_value(msg2, rdn->name, &rdn->value) != 0) {
105 const struct ldb_attrib_handler *handler
106 = ldb_attrib_handler(module->ldb, rdn->name);
107 for (i=0; i < attribute->num_values; i++) {
108 if (handler->comparison_fn(module->ldb, msg2, &rdn->value, &attribute->values[i]) == 0) {
109 /* overwrite so it matches in case */
110 attribute->values[i] = rdn->value;
114 if (i == attribute->num_values) {
115 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
116 "RDN mismatch on %s: %s",
117 ldb_dn_linearize(msg2, msg2->dn), rdn->name);
123 req->op.add.message = msg2;
124 ret = ldb_next_request(module, req);
125 req->op.add.message = msg;
132 static int rdn_name_modify(struct ldb_module *module, struct ldb_request *req)
134 const struct ldb_message *msg = req->op.mod.message;
135 struct ldb_message *msg2;
136 struct ldb_message_element *attribute;
137 struct ldb_dn_component *rdn;
140 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_modify_record\n");
142 /* do not manipulate our control entries */
143 if (ldb_dn_is_special(msg->dn)) {
144 return ldb_next_request(module, req);
147 /* Perhaps someone above us knows better */
148 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
149 return ldb_next_request(module, req);
152 msg2 = talloc(module, struct ldb_message);
158 msg2->num_elements = msg->num_elements;
159 msg2->private_data = msg->private_data;
160 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
161 for (i = 0; i < msg2->num_elements; i++) {
162 msg2->elements[i] = msg->elements[i];
165 rdn = ldb_dn_get_rdn(msg2, msg2->dn);
171 if (ldb_msg_add_value(msg2, "name", &rdn->value) != 0) {
176 attribute = rdn_name_find_attribute(msg2, "name");
182 attribute->flags = LDB_FLAG_MOD_REPLACE;
184 req->op.add.message = msg2;
185 ret = ldb_next_request(module, req);
186 req->op.add.message = msg;
193 static int rdn_name_request(struct ldb_module *module, struct ldb_request *req)
195 switch (req->operation) {
198 return rdn_name_add(module, req);
201 return rdn_name_modify(module, req);
205 return ldb_next_request(module, req);
210 static const struct ldb_module_ops rdn_name_ops = {
212 .request = rdn_name_request
216 /* the init function */
217 #ifdef HAVE_DLOPEN_DISABLED
218 struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
220 struct ldb_module *rdn_name_module_init(struct ldb_context *ldb, const char *options[])
223 struct ldb_module *ctx;
225 ctx = talloc(ldb, struct ldb_module);
229 ctx->private_data = NULL;
231 ctx->prev = ctx->next = NULL;
232 ctx->ops = &rdn_name_ops;