4 Copyright (C) Andrew Bartlet 2005
5 Copyright (C) Simo Sorce 2006-2008
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 * Component: ldb rdn name module
30 * Description: keep a consistent name attribute on objects manpulations
32 * Author: Andrew Bartlet
35 * - made the module async
39 #include "ldb_module.h"
41 struct rename_context {
43 struct ldb_module *module;
44 struct ldb_request *req;
46 struct ldb_reply *ares;
49 static struct ldb_message_element *rdn_name_find_attribute(const struct ldb_message *msg, const char *name)
53 for (i = 0; i < msg->num_elements; i++) {
54 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
55 return &msg->elements[i];
62 static int rdn_name_add_callback(struct ldb_request *req,
63 struct ldb_reply *ares)
65 struct rename_context *ac;
67 ac = talloc_get_type(req->context, struct rename_context);
70 return ldb_module_done(ac->req, NULL, NULL,
71 LDB_ERR_OPERATIONS_ERROR);
73 if (ares->error != LDB_SUCCESS) {
74 return ldb_module_done(ac->req, ares->controls,
75 ares->response, ares->error);
78 if (ares->type != LDB_REPLY_DONE) {
79 return ldb_module_done(ac->req, NULL, NULL,
80 LDB_ERR_OPERATIONS_ERROR);
83 return ldb_module_done(ac->req, ares->controls,
84 ares->response, LDB_SUCCESS);
87 static int rdn_name_add(struct ldb_module *module, struct ldb_request *req)
89 struct ldb_context *ldb;
90 struct ldb_request *down_req;
91 struct rename_context *ac;
92 struct ldb_message *msg;
93 struct ldb_message_element *attribute;
94 const struct ldb_schema_attribute *a;
96 struct ldb_val rdn_val;
99 ldb = ldb_module_get_ctx(module);
100 ldb_debug(ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
102 /* do not manipulate our control entries */
103 if (ldb_dn_is_special(req->op.add.message->dn)) {
104 return ldb_next_request(module, req);
107 ac = talloc_zero(req, struct rename_context);
109 return LDB_ERR_OPERATIONS_ERROR;
115 msg = ldb_msg_copy_shallow(req, req->op.add.message);
117 return LDB_ERR_OPERATIONS_ERROR;
120 rdn_name = ldb_dn_get_rdn_name(msg->dn);
121 if (rdn_name == NULL) {
123 return LDB_ERR_OPERATIONS_ERROR;
126 rdn_val = ldb_val_dup(msg, ldb_dn_get_rdn_val(msg->dn));
128 /* Perhaps someone above us tried to set this? */
129 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
130 attribute->num_values = 0;
133 if (ldb_msg_add_value(msg, "name", &rdn_val, NULL) != 0) {
135 return LDB_ERR_OPERATIONS_ERROR;
138 attribute = rdn_name_find_attribute(msg, rdn_name);
141 if (ldb_msg_add_value(msg, rdn_name, &rdn_val, NULL) != 0) {
143 return LDB_ERR_OPERATIONS_ERROR;
146 a = ldb_schema_attribute_by_name(ldb, rdn_name);
148 for (i = 0; i < attribute->num_values; i++) {
149 ret = a->syntax->comparison_fn(ldb, msg,
150 &rdn_val, &attribute->values[i]);
152 /* overwrite so it matches in case */
153 attribute->values[i] = rdn_val;
157 if (i == attribute->num_values) {
158 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
159 "RDN mismatch on %s: %s (%s)",
160 ldb_dn_get_linearized(msg->dn), rdn_name, rdn_val.data);
162 /* Match AD's error here */
163 return LDB_ERR_INVALID_DN_SYNTAX;
167 ret = ldb_build_add_req(&down_req, ldb, req,
170 ac, rdn_name_add_callback,
172 if (ret != LDB_SUCCESS) {
176 talloc_steal(down_req, msg);
178 /* go on with the call chain */
179 return ldb_next_request(module, down_req);
182 static int rdn_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
184 struct rename_context *ac;
186 ac = talloc_get_type(req->context, struct rename_context);
189 return ldb_module_done(ac->req, NULL, NULL,
190 LDB_ERR_OPERATIONS_ERROR);
192 if (ares->error != LDB_SUCCESS) {
193 return ldb_module_done(ac->req, ares->controls,
194 ares->response, ares->error);
197 /* the only supported reply right now is a LDB_REPLY_DONE */
198 if (ares->type != LDB_REPLY_DONE) {
199 return ldb_module_done(ac->req, NULL, NULL,
200 LDB_ERR_OPERATIONS_ERROR);
203 /* send saved controls eventually */
204 return ldb_module_done(ac->req, ac->ares->controls,
205 ac->ares->response, LDB_SUCCESS);
208 static int rdn_rename_callback(struct ldb_request *req, struct ldb_reply *ares)
210 struct ldb_context *ldb;
211 struct rename_context *ac;
212 struct ldb_request *mod_req;
213 const char *rdn_name;
214 struct ldb_val rdn_val;
215 struct ldb_message *msg;
218 ac = talloc_get_type(req->context, struct rename_context);
219 ldb = ldb_module_get_ctx(ac->module);
224 if (ares->error != LDB_SUCCESS) {
225 return ldb_module_done(ac->req, ares->controls,
226 ares->response, ares->error);
229 /* the only supported reply right now is a LDB_REPLY_DONE */
230 if (ares->type != LDB_REPLY_DONE) {
234 /* save reply for caller */
235 ac->ares = talloc_steal(ac, ares);
237 msg = ldb_msg_new(ac);
241 msg->dn = ldb_dn_copy(msg, ac->req->op.rename.newdn);
242 if (msg->dn == NULL) {
245 rdn_name = ldb_dn_get_rdn_name(ac->req->op.rename.newdn);
246 if (rdn_name == NULL) {
250 rdn_val = ldb_val_dup(msg, ldb_dn_get_rdn_val(ac->req->op.rename.newdn));
252 if (ldb_msg_add_empty(msg, rdn_name, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
255 if (ldb_msg_add_value(msg, rdn_name, &rdn_val, NULL) != 0) {
258 if (ldb_msg_add_empty(msg, "name", LDB_FLAG_MOD_REPLACE, NULL) != 0) {
261 if (ldb_msg_add_value(msg, "name", &rdn_val, NULL) != 0) {
265 ret = ldb_build_mod_req(&mod_req, ldb,
267 ac, rdn_modify_callback,
269 if (ret != LDB_SUCCESS) {
270 return ldb_module_done(ac->req, NULL, NULL, ret);
272 talloc_steal(mod_req, msg);
274 /* do the mod call */
275 return ldb_request(ldb, mod_req);
278 return ldb_module_done(ac->req, NULL, NULL,
279 LDB_ERR_OPERATIONS_ERROR);
282 static int rdn_name_rename(struct ldb_module *module, struct ldb_request *req)
284 struct ldb_context *ldb;
285 struct rename_context *ac;
286 struct ldb_request *down_req;
289 ldb = ldb_module_get_ctx(module);
290 ldb_debug(ldb, LDB_DEBUG_TRACE, "rdn_name_rename\n");
292 /* do not manipulate our control entries */
293 if (ldb_dn_is_special(req->op.rename.newdn)) {
294 return ldb_next_request(module, req);
297 ac = talloc_zero(req, struct rename_context);
299 return LDB_ERR_OPERATIONS_ERROR;
305 ret = ldb_build_rename_req(&down_req,
308 req->op.rename.olddn,
309 req->op.rename.newdn,
315 if (ret != LDB_SUCCESS) {
316 return LDB_ERR_OPERATIONS_ERROR;
319 /* rename first, modify "name" if rename is ok */
320 return ldb_next_request(module, down_req);
323 const struct ldb_module_ops ldb_rdn_name_module_ops = {
326 .rename = rdn_name_rename,