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_includes.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_request *down_req;
90 struct rename_context *ac;
91 struct ldb_message *msg;
92 struct ldb_message_element *attribute;
93 const struct ldb_schema_attribute *a;
95 struct ldb_val rdn_val;
98 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_add_record\n");
100 /* do not manipulate our control entries */
101 if (ldb_dn_is_special(req->op.add.message->dn)) {
102 return ldb_next_request(module, req);
105 ac = talloc_zero(req, struct rename_context);
107 return LDB_ERR_OPERATIONS_ERROR;
113 msg = ldb_msg_copy_shallow(req, req->op.add.message);
115 return LDB_ERR_OPERATIONS_ERROR;
118 rdn_name = ldb_dn_get_rdn_name(msg->dn);
119 if (rdn_name == NULL) {
121 return LDB_ERR_OPERATIONS_ERROR;
124 rdn_val = ldb_val_dup(msg, ldb_dn_get_rdn_val(msg->dn));
126 /* Perhaps someone above us tried to set this? */
127 if ((attribute = rdn_name_find_attribute(msg, "name")) != NULL ) {
128 attribute->num_values = 0;
131 if (ldb_msg_add_value(msg, "name", &rdn_val, NULL) != 0) {
133 return LDB_ERR_OPERATIONS_ERROR;
136 attribute = rdn_name_find_attribute(msg, rdn_name);
139 if (ldb_msg_add_value(msg, rdn_name, &rdn_val, NULL) != 0) {
141 return LDB_ERR_OPERATIONS_ERROR;
144 a = ldb_schema_attribute_by_name(module->ldb, rdn_name);
146 for (i = 0; i < attribute->num_values; i++) {
147 ret = a->syntax->comparison_fn(module->ldb, msg,
148 &rdn_val, &attribute->values[i]);
150 /* overwrite so it matches in case */
151 attribute->values[i] = rdn_val;
155 if (i == attribute->num_values) {
156 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
157 "RDN mismatch on %s: %s (%s)",
158 ldb_dn_get_linearized(msg->dn), rdn_name, rdn_val.data);
160 /* Match AD's error here */
161 return LDB_ERR_INVALID_DN_SYNTAX;
165 ret = ldb_build_add_req(&down_req, module->ldb, req,
168 ac, rdn_name_add_callback,
170 if (ret != LDB_SUCCESS) {
174 talloc_steal(down_req, msg);
176 /* go on with the call chain */
177 return ldb_next_request(module, down_req);
180 static int rdn_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
182 struct rename_context *ac;
184 ac = talloc_get_type(req->context, struct rename_context);
187 return ldb_module_done(ac->req, NULL, NULL,
188 LDB_ERR_OPERATIONS_ERROR);
190 if (ares->error != LDB_SUCCESS) {
191 return ldb_module_done(ac->req, ares->controls,
192 ares->response, ares->error);
195 /* the only supported reply right now is a LDB_REPLY_DONE */
196 if (ares->type != LDB_REPLY_DONE) {
197 return ldb_module_done(ac->req, NULL, NULL,
198 LDB_ERR_OPERATIONS_ERROR);
201 /* send saved controls eventually */
202 return ldb_module_done(ac->req, ac->ares->controls,
203 ac->ares->response, LDB_SUCCESS);
206 static int rdn_rename_callback(struct ldb_request *req, struct ldb_reply *ares)
208 struct rename_context *ac;
209 struct ldb_request *mod_req;
210 const char *rdn_name;
211 struct ldb_val rdn_val;
212 struct ldb_message *msg;
215 ac = talloc_get_type(req->context, struct rename_context);
220 if (ares->error != LDB_SUCCESS) {
221 return ldb_module_done(ac->req, ares->controls,
222 ares->response, ares->error);
225 /* the only supported reply right now is a LDB_REPLY_DONE */
226 if (ares->type != LDB_REPLY_DONE) {
230 /* save reply for caller */
231 ac->ares = talloc_steal(ac, ares);
233 msg = ldb_msg_new(ac);
237 msg->dn = ldb_dn_copy(msg, ac->req->op.rename.newdn);
238 if (msg->dn == NULL) {
241 rdn_name = ldb_dn_get_rdn_name(ac->req->op.rename.newdn);
242 if (rdn_name == NULL) {
246 rdn_val = ldb_val_dup(msg, ldb_dn_get_rdn_val(ac->req->op.rename.newdn));
248 if (ldb_msg_add_empty(msg, rdn_name, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
251 if (ldb_msg_add_value(msg, rdn_name, &rdn_val, NULL) != 0) {
254 if (ldb_msg_add_empty(msg, "name", LDB_FLAG_MOD_REPLACE, NULL) != 0) {
257 if (ldb_msg_add_value(msg, "name", &rdn_val, NULL) != 0) {
261 ret = ldb_build_mod_req(&mod_req, ac->module->ldb,
263 ac, rdn_modify_callback,
265 if (ret != LDB_SUCCESS) {
266 return ldb_module_done(ac->req, NULL, NULL, ret);
268 talloc_steal(mod_req, msg);
270 /* do the mod call */
271 return ldb_request(ac->module->ldb, mod_req);
274 return ldb_module_done(ac->req, NULL, NULL,
275 LDB_ERR_OPERATIONS_ERROR);
278 static int rdn_name_rename(struct ldb_module *module, struct ldb_request *req)
280 struct rename_context *ac;
281 struct ldb_request *down_req;
284 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "rdn_name_rename\n");
286 /* do not manipulate our control entries */
287 if (ldb_dn_is_special(req->op.rename.newdn)) {
288 return ldb_next_request(module, req);
291 ac = talloc_zero(req, struct rename_context);
293 return LDB_ERR_OPERATIONS_ERROR;
299 ret = ldb_build_rename_req(&down_req,
302 req->op.rename.olddn,
303 req->op.rename.newdn,
309 if (ret != LDB_SUCCESS) {
310 return LDB_ERR_OPERATIONS_ERROR;
313 /* rename first, modify "name" if rename is ok */
314 return ldb_next_request(module, down_req);
317 const struct ldb_module_ops ldb_rdn_name_module_ops = {
320 .rename = rdn_name_rename,