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 int rdn_name_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
41 enum ldb_scope scope, struct ldb_parse_tree *tree,
42 const char * const *attrs, struct ldb_message ***res)
44 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_search\n");
45 return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
48 static struct ldb_message_element *rdn_name_find_attribute(const struct ldb_message *msg, const char *name)
52 for (i = 0; i < msg->num_elements; i++) {
53 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
54 return &msg->elements[i];
61 /* add_record: add crateTimestamp/modifyTimestamp attributes */
62 static int rdn_name_add_record(struct ldb_module *module, const struct ldb_message *msg)
64 struct ldb_message *msg2;
65 struct ldb_message_element *attribute;
66 struct ldb_dn_component *rdn;
69 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
71 /* do not manipulate our control entries */
72 if (ldb_dn_is_special(msg->dn)) {
73 return ldb_next_add_record(module, msg);
76 /* Perhaps someone above us knows better */
77 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
78 return ldb_next_add_record(module, msg);
81 msg2 = talloc(module, struct ldb_message);
87 msg2->num_elements = msg->num_elements;
88 msg2->private_data = msg->private_data;
89 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
90 for (i = 0; i < msg2->num_elements; i++) {
91 msg2->elements[i] = msg->elements[i];
94 rdn = ldb_dn_get_rdn(msg2, msg2->dn);
100 if (ldb_msg_add_value(msg2, "name", &rdn->value) != 0) {
105 attribute = rdn_name_find_attribute(msg2, rdn->name);
108 if (ldb_msg_add_value(msg2, rdn->name, &rdn->value) != 0) {
113 const struct ldb_attrib_handler *handler
114 = ldb_attrib_handler(module->ldb, rdn->name);
115 for (i=0; i < attribute->num_values; i++) {
116 if (handler->comparison_fn(module->ldb, msg2, &rdn->value, &attribute->values[i]) == 0) {
117 /* overwrite so it matches in case */
118 attribute->values[i] = rdn->value;
122 if (i == attribute->num_values) {
123 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
124 "RDN mismatch on %s: %s",
125 ldb_dn_linearize(msg2, msg2->dn), rdn->name);
131 ret = ldb_next_add_record(module, msg2);
137 /* modify_record: change modifyTimestamp as well */
138 static int rdn_name_modify_record(struct ldb_module *module, const struct ldb_message *msg)
140 struct ldb_message *msg2;
141 struct ldb_message_element *attribute;
142 struct ldb_dn_component *rdn;
145 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_modify_record\n");
147 /* do not manipulate our control entries */
148 if (ldb_dn_is_special(msg->dn)) {
149 return ldb_next_modify_record(module, msg);
152 /* Perhaps someone above us knows better */
153 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
154 return ldb_next_modify_record(module, msg);
157 msg2 = talloc(module, struct ldb_message);
163 msg2->num_elements = msg->num_elements;
164 msg2->private_data = msg->private_data;
165 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
166 for (i = 0; i < msg2->num_elements; i++) {
167 msg2->elements[i] = msg->elements[i];
170 rdn = ldb_dn_get_rdn(msg2, msg2->dn);
176 if (ldb_msg_add_value(msg2, "name", &rdn->value) != 0) {
181 attribute = rdn_name_find_attribute(msg2, "name");
187 attribute->flags = LDB_FLAG_MOD_REPLACE;
189 ret = ldb_next_modify_record(module, msg2);
195 static int rdn_name_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
197 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_rename_record\n");
198 return ldb_next_rename_record(module, olddn, newdn);
201 static const struct ldb_module_ops rdn_name_ops = {
203 .search_bytree = rdn_name_search_bytree,
204 .add_record = rdn_name_add_record,
205 .modify_record = rdn_name_modify_record,
206 .rename_record = rdn_name_rename_record
210 /* the init function */
211 #ifdef HAVE_DLOPEN_DISABLED
212 struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
214 struct ldb_module *rdn_name_module_init(struct ldb_context *ldb, const char *options[])
217 struct ldb_module *ctx;
219 ctx = talloc(ldb, struct ldb_module);
223 ctx->private_data = NULL;
225 ctx->prev = ctx->next = NULL;
226 ctx->ops = &rdn_name_ops;