r13786: [merge] Add registration functions for LDB modules
[ira/wip.git] / source4 / 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/includes.h"
37
38 static struct ldb_message_element *rdn_name_find_attribute(const struct ldb_message *msg, const char *name)
39 {
40         int i;
41
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];
45                 }
46         }
47
48         return NULL;
49 }
50
51 static int rdn_name_add(struct ldb_module *module, struct ldb_request *req)
52 {
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;
57         int i, ret;
58
59         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
60
61         /* do not manipulate our control entries */
62         if (ldb_dn_is_special(msg->dn)) {
63                 return ldb_next_request(module, req);
64         }
65
66         msg2 = talloc(module, struct ldb_message);
67         if (!msg2) {
68                 return -1;
69         }
70
71         msg2->dn = msg->dn;
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];
77         }
78
79         rdn = ldb_dn_get_rdn(msg2, msg2->dn);
80         if (!rdn) {
81                 talloc_free(msg2);
82                 return -1;
83         }
84         
85         /* Perhaps someone above us tried to set this? */
86         if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
87                 attribute->num_values = 0;
88         }
89
90         if (ldb_msg_add_value(msg2, "name", &rdn->value) != 0) {
91                 talloc_free(msg2);
92                 return -1;
93         }
94
95         attribute = rdn_name_find_attribute(msg2, rdn->name);
96
97         if (!attribute) {
98                 if (ldb_msg_add_value(msg2, rdn->name, &rdn->value) != 0) {
99                         talloc_free(msg2);
100                         return -1;
101                 }
102         } else {
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;
109                                 break;
110                         }
111                 }
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);
116                         talloc_free(msg2);
117                         return -1;
118                 }
119         }
120
121         req->op.add.message = msg2;
122         ret = ldb_next_request(module, req);
123         req->op.add.message = msg;
124
125         talloc_free(msg2);
126
127         return ret;
128 }
129
130 static int rdn_name_modify(struct ldb_module *module, struct ldb_request *req)
131 {
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;
136         int ret, i;
137
138         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_modify_record\n");
139
140         /* do not manipulate our control entries */
141         if (ldb_dn_is_special(msg->dn)) {
142                 return ldb_next_request(module, req);
143         }
144
145         /* Perhaps someone above us knows better */
146         if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
147                 return ldb_next_request(module, req);
148         }
149
150         msg2 = talloc(module, struct ldb_message);
151         if (!msg2) {
152                 return -1;
153         }
154
155         msg2->dn = msg->dn;
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];
161         }
162         
163         rdn = ldb_dn_get_rdn(msg2, msg2->dn);
164         if (!rdn) {
165                 talloc_free(msg2);
166                 return -1;
167         }
168         
169         if (ldb_msg_add_value(msg2, "name", &rdn->value) != 0) {
170                 talloc_free(msg2);
171                 return -1;
172         }
173
174         attribute = rdn_name_find_attribute(msg2, "name");
175         if (!attribute) {
176                 talloc_free(msg2);
177                 return -1;
178         }
179
180         attribute->flags = LDB_FLAG_MOD_REPLACE;
181
182         req->op.add.message = msg2;
183         ret = ldb_next_request(module, req);
184         req->op.add.message = msg;
185
186         talloc_free(msg2);
187
188         return ret;
189 }
190
191 static int rdn_name_request(struct ldb_module *module, struct ldb_request *req)
192 {
193         switch (req->operation) {
194
195         case LDB_REQ_ADD:
196                 return rdn_name_add(module, req);
197
198         case LDB_REQ_MODIFY:
199                 return rdn_name_modify(module, req);
200
201
202         default:
203                 return ldb_next_request(module, req);
204
205         }
206 }
207
208 static const struct ldb_module_ops rdn_name_ops = {
209         .name              = "rdn_name",
210         .request           = rdn_name_request
211 };
212
213
214 int ldb_rdn_name_init(void)
215 {
216         return ldb_register_module(&rdn_name_ops);
217 }