r10477: expose transactions outside ldb and change the API once more
[samba.git] / source / lib / ldb / modules / rdn_name.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb objectguid module
29  *
30  *  Description: add a unique objectGUID onto every new record
31  *
32  *  Author: Simo Sorce
33  */
34
35 #include "includes.h"
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
38 #include <time.h>
39
40 static int rdn_name_search(struct ldb_module *module, const struct ldb_dn *base,
41                                   enum ldb_scope scope, const char *expression,
42                                   const char * const *attrs, struct ldb_message ***res)
43 {
44         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_search\n");
45         return ldb_next_search(module, base, scope, expression, attrs, res);
46 }
47
48 static int rdn_name_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
49                                     enum ldb_scope scope, struct ldb_parse_tree *tree,
50                                     const char * const *attrs, struct ldb_message ***res)
51 {
52         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_search\n");
53         return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
54 }
55
56 static struct ldb_message_element *rdn_name_find_attribute(const struct ldb_message *msg, const char *name)
57 {
58         int i;
59
60         for (i = 0; i < msg->num_elements; i++) {
61                 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
62                         return &msg->elements[i];
63                 }
64         }
65
66         return NULL;
67 }
68
69 /* add_record: add crateTimestamp/modifyTimestamp attributes */
70 static int rdn_name_add_record(struct ldb_module *module, const struct ldb_message *msg)
71 {
72         struct ldb_message *msg2;
73         struct ldb_message_element *attribute;
74         struct ldb_dn_component *rdn;
75         int i, ret;
76
77         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
78
79         /* do not manipulate our control entries */
80         if (ldb_dn_is_special(msg->dn)) {
81                 return ldb_next_add_record(module, msg);
82         }
83
84         /* Perhaps someone above us knows better */
85         if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
86                 return ldb_next_add_record(module, msg);
87         }
88
89         msg2 = talloc(module, struct ldb_message);
90         if (!msg2) {
91                 return -1;
92         }
93
94         msg2->dn = msg->dn;
95         msg2->num_elements = msg->num_elements;
96         msg2->private_data = msg->private_data;
97         msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
98         for (i = 0; i < msg2->num_elements; i++) {
99                 msg2->elements[i] = msg->elements[i];
100         }
101
102         rdn = ldb_dn_get_rdn(msg2, msg2->dn);
103         if (!rdn) {
104                 talloc_free(msg2);
105                 return -1;
106         }
107         
108         if (ldb_msg_add_value(module->ldb, msg2, "name", &rdn->value) != 0) {
109                 talloc_free(msg2);
110                 return -1;
111         }
112
113         attribute = rdn_name_find_attribute(msg2, rdn->name);
114
115         if (!attribute) {
116                 if (ldb_msg_add_value(module->ldb, msg2, rdn->name, &rdn->value) != 0) {
117                         talloc_free(msg2);
118                         return -1;
119                 }
120         } else {
121                 const struct ldb_attrib_handler *handler
122                         = ldb_attrib_handler(module->ldb, rdn->name);
123                 for (i=0; i < attribute->num_values; i++) {
124                         if (handler->comparison_fn(module->ldb, msg2, &rdn->value, &attribute->values[i]) == 0) {
125                                 /* overwrite so it matches in case */
126                                 attribute->values[i] = rdn->value;
127                                 break;
128                         }
129                 }
130                 if (i == attribute->num_values) {
131                         char *error_string = talloc_asprintf(module, "RDN mismatch on %s: %s", ldb_dn_linearize(msg2, msg2->dn), rdn->name);
132                         if (error_string) {
133                                 ldb_set_errstring(module, error_string);
134                                 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "%s\n", error_string);
135                         }
136                         talloc_free(msg2);
137                         return -1;
138                 }
139         }
140
141         ret = ldb_next_add_record(module, msg2);
142         talloc_free(msg2);
143
144         return ret;
145 }
146
147 /* modify_record: change modifyTimestamp as well */
148 static int rdn_name_modify_record(struct ldb_module *module, const struct ldb_message *msg)
149 {
150         struct ldb_message *msg2;
151         struct ldb_message_element *attribute;
152         struct ldb_dn_component *rdn;
153         int ret, i;
154
155         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_modify_record\n");
156
157         /* do not manipulate our control entries */
158         if (ldb_dn_is_special(msg->dn)) {
159                 return ldb_next_modify_record(module, msg);
160         }
161
162         /* Perhaps someone above us knows better */
163         if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
164                 return ldb_next_modify_record(module, msg);
165         }
166
167         msg2 = talloc(module, struct ldb_message);
168         if (!msg2) {
169                 return -1;
170         }
171
172         msg2->dn = msg->dn;
173         msg2->num_elements = msg->num_elements;
174         msg2->private_data = msg->private_data;
175         msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
176         for (i = 0; i < msg2->num_elements; i++) {
177                 msg2->elements[i] = msg->elements[i];
178         }
179         
180         rdn = ldb_dn_get_rdn(msg2, msg2->dn);
181         if (!rdn) {
182                 talloc_free(msg2);
183                 return -1;
184         }
185         
186         if (ldb_msg_add_value(module->ldb, msg2, "name", &rdn->value) != 0) {
187                 talloc_free(msg2);
188                 return -1;
189         }
190
191         attribute = rdn_name_find_attribute(msg2, "name");
192         if (!attribute) {
193                 talloc_free(msg2);
194                 return -1;
195         }
196
197         attribute->flags = LDB_FLAG_MOD_REPLACE;
198
199         ret = ldb_next_modify_record(module, msg2);
200         talloc_free(msg2);
201
202         return ret;
203 }
204
205 static int rdn_name_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
206 {
207         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_delete_record\n");
208         return ldb_next_delete_record(module, dn);
209 }
210
211 static int rdn_name_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
212 {
213         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_rename_record\n");
214         return ldb_next_rename_record(module, olddn, newdn);
215 }
216
217 static int rdn_start_trans(struct ldb_module *module)
218 {
219         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_start_trans\n");
220         return ldb_next_start_trans(module);
221 }
222
223 static int rdn_end_trans(struct ldb_module *module)
224 {
225         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_end_trans\n");
226         return ldb_next_end_trans(module);
227 }
228
229 static int rdn_del_trans(struct ldb_module *module)
230 {
231         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_del_trans\n");
232         return ldb_next_del_trans(module);
233 }
234
235 static int rdn_name_destructor(void *module_ctx)
236 {
237         /* struct ldb_module *ctx = module_ctx; */
238         /* put your clean-up functions here */
239         return 0;
240 }
241
242 static const struct ldb_module_ops rdn_name_ops = {
243         .name              = "rdn_name",
244         .search            = rdn_name_search,
245         .search_bytree     = rdn_name_search_bytree,
246         .add_record        = rdn_name_add_record,
247         .modify_record     = rdn_name_modify_record,
248         .delete_record     = rdn_name_delete_record,
249         .rename_record     = rdn_name_rename_record,
250         .start_transaction = rdn_start_trans,
251         .end_transaction   = rdn_end_trans,
252         .del_transaction   = rdn_del_trans
253 };
254
255
256 /* the init function */
257 #ifdef HAVE_DLOPEN_DISABLED
258  struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
259 #else
260 struct ldb_module *rdn_name_module_init(struct ldb_context *ldb, const char *options[])
261 #endif
262 {
263         struct ldb_module *ctx;
264
265         ctx = talloc(ldb, struct ldb_module);
266         if (!ctx)
267                 return NULL;
268
269         ctx->private_data = NULL;
270         ctx->ldb = ldb;
271         ctx->prev = ctx->next = NULL;
272         ctx->ops = &rdn_name_ops;
273
274         talloc_set_destructor (ctx, rdn_name_destructor);
275
276         return ctx;
277 }