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/includes.h"
38 static struct ldb_message_element *rdn_name_find_attribute(const struct ldb_message *msg, const char *name)
42 for (i = 0; i < msg->num_elements; i++) {
43 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
44 return &msg->elements[i];
51 static int rdn_name_add(struct ldb_module *module, struct ldb_request *req)
53 const struct ldb_message *msg = req->op.add.message;
54 struct ldb_message *msg2;
55 struct ldb_message_element *attribute;
56 struct ldb_dn_component *rdn;
59 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
61 /* do not manipulate our control entries */
62 if (ldb_dn_is_special(msg->dn)) {
63 return ldb_next_request(module, req);
66 msg2 = talloc(module, struct ldb_message);
72 msg2->num_elements = msg->num_elements;
73 msg2->private_data = msg->private_data;
74 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
75 for (i = 0; i < msg2->num_elements; i++) {
76 msg2->elements[i] = msg->elements[i];
79 rdn = ldb_dn_get_rdn(msg2, msg2->dn);
85 /* Perhaps someone above us tried to set this? */
86 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
87 attribute->num_values = 0;
90 if (ldb_msg_add_value(msg2, "name", &rdn->value) != 0) {
95 attribute = rdn_name_find_attribute(msg2, rdn->name);
98 if (ldb_msg_add_value(msg2, rdn->name, &rdn->value) != 0) {
103 const struct ldb_attrib_handler *handler
104 = ldb_attrib_handler(module->ldb, rdn->name);
105 for (i=0; i < attribute->num_values; i++) {
106 if (handler->comparison_fn(module->ldb, msg2, &rdn->value, &attribute->values[i]) == 0) {
107 /* overwrite so it matches in case */
108 attribute->values[i] = rdn->value;
112 if (i == attribute->num_values) {
113 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
114 "RDN mismatch on %s: %s",
115 ldb_dn_linearize(msg2, msg2->dn), rdn->name);
121 req->op.add.message = msg2;
122 ret = ldb_next_request(module, req);
123 req->op.add.message = msg;
130 static int rdn_name_modify(struct ldb_module *module, struct ldb_request *req)
132 const struct ldb_message *msg = req->op.mod.message;
133 struct ldb_message *msg2;
134 struct ldb_message_element *attribute;
135 struct ldb_dn_component *rdn;
138 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_modify_record\n");
140 /* do not manipulate our control entries */
141 if (ldb_dn_is_special(msg->dn)) {
142 return ldb_next_request(module, req);
145 /* Perhaps someone above us knows better */
146 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
147 return ldb_next_request(module, req);
150 msg2 = talloc(module, struct ldb_message);
156 msg2->num_elements = msg->num_elements;
157 msg2->private_data = msg->private_data;
158 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
159 for (i = 0; i < msg2->num_elements; i++) {
160 msg2->elements[i] = msg->elements[i];
163 rdn = ldb_dn_get_rdn(msg2, msg2->dn);
169 if (ldb_msg_add_value(msg2, "name", &rdn->value) != 0) {
174 attribute = rdn_name_find_attribute(msg2, "name");
180 attribute->flags = LDB_FLAG_MOD_REPLACE;
182 req->op.add.message = msg2;
183 ret = ldb_next_request(module, req);
184 req->op.add.message = msg;
191 static int rdn_name_request(struct ldb_module *module, struct ldb_request *req)
193 switch (req->operation) {
196 return rdn_name_add(module, req);
199 return rdn_name_modify(module, req);
203 return ldb_next_request(module, req);
208 static const struct ldb_module_ops rdn_name_ops = {
210 .request = rdn_name_request
214 int ldb_rdn_name_init(void)
216 return ldb_register_module(&rdn_name_ops);