4 Copyright (C) Simo Sorce 2004-2008
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
9 ** NOTE! The following LGPL license applies to the ldb
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 * Component: ldb instancetype module
32 * Description: add an instanceType onto every new record
34 * Author: Andrew Bartlett
38 #include "ldb_module.h"
39 #include "librpc/gen_ndr/ndr_misc.h"
40 #include "dsdb/samdb/samdb.h"
41 #include "dsdb/common/flags.h"
44 struct ldb_module *module;
45 struct ldb_request *req;
48 static int it_callback(struct ldb_request *req, struct ldb_reply *ares)
50 struct ldb_context *ldb;
51 struct it_context *ac;
53 ac = talloc_get_type(req->context, struct it_context);
54 ldb = ldb_module_get_ctx(ac->module);
57 return ldb_module_done(ac->req, NULL, NULL,
58 LDB_ERR_OPERATIONS_ERROR);
60 if (ares->error != LDB_SUCCESS) {
61 return ldb_module_done(ac->req, ares->controls,
62 ares->response, ares->error);
65 if (ares->type != LDB_REPLY_DONE) {
66 ldb_set_errstring(ldb, "Invalid reply type!");
67 return ldb_module_done(ac->req, NULL, NULL,
68 LDB_ERR_OPERATIONS_ERROR);
71 return ldb_module_done(ac->req, ares->controls,
72 ares->response, ares->error);
75 /* add_record: add instancetype attribute */
76 static int instancetype_add(struct ldb_module *module, struct ldb_request *req)
78 struct ldb_context *ldb;
79 struct ldb_request *down_req;
80 struct ldb_message *msg;
81 struct it_context *ac;
82 uint32_t instance_type;
84 const struct ldb_control *partition_ctrl;
85 const struct dsdb_control_current_partition *partition;
87 ldb = ldb_module_get_ctx(module);
89 ldb_debug(ldb, LDB_DEBUG_TRACE, "instancetype_add_record\n");
91 /* do not manipulate our control entries */
92 if (ldb_dn_is_special(req->op.add.message->dn)) {
93 return ldb_next_request(module, req);
96 partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
97 if (!partition_ctrl) {
98 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
99 "instancetype_add: no current partition control found");
100 return LDB_ERR_CONSTRAINT_VIOLATION;
103 partition = talloc_get_type(partition_ctrl->data,
104 struct dsdb_control_current_partition);
105 SMB_ASSERT(partition && partition->version == DSDB_CONTROL_CURRENT_PARTITION_VERSION);
107 ac = talloc(req, struct it_context);
109 return LDB_ERR_OPERATIONS_ERROR;
114 /* we have to copy the message as the caller might have it as a const */
115 msg = ldb_msg_copy_shallow(ac, req->op.add.message);
118 return LDB_ERR_OPERATIONS_ERROR;
122 * TODO: calculate correct instance type
124 instance_type = INSTANCE_TYPE_WRITE;
125 if (ldb_dn_compare(partition->dn, msg->dn) == 0) {
126 instance_type |= INSTANCE_TYPE_IS_NC_HEAD;
127 if (ldb_dn_compare(msg->dn, samdb_base_dn(ldb)) != 0) {
128 instance_type |= INSTANCE_TYPE_NC_ABOVE;
132 ret = ldb_msg_add_fmt(msg, "instanceType", "%u", instance_type);
133 if (ret != LDB_SUCCESS) {
135 return LDB_ERR_OPERATIONS_ERROR;
138 ret = ldb_build_add_req(&down_req, ldb, ac,
143 if (ret != LDB_SUCCESS) {
147 /* go on with the call chain */
148 return ldb_next_request(module, down_req);
151 _PUBLIC_ const struct ldb_module_ops ldb_instancetype_module_ops = {
152 .name = "instancetype",
153 .add = instancetype_add,