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 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * Component: ldb instancetype module
28 * Description: add an instanceType onto every new record
30 * Author: Andrew Bartlett
34 #include "ldb_module.h"
35 #include "librpc/gen_ndr/ndr_misc.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "../libds/common/flags.h"
40 struct ldb_module *module;
41 struct ldb_request *req;
44 static int it_callback(struct ldb_request *req, struct ldb_reply *ares)
46 struct ldb_context *ldb;
47 struct it_context *ac;
49 ac = talloc_get_type(req->context, struct it_context);
50 ldb = ldb_module_get_ctx(ac->module);
53 return ldb_module_done(ac->req, NULL, NULL,
54 LDB_ERR_OPERATIONS_ERROR);
56 if (ares->error != LDB_SUCCESS) {
57 return ldb_module_done(ac->req, ares->controls,
58 ares->response, ares->error);
61 if (ares->type != LDB_REPLY_DONE) {
62 ldb_set_errstring(ldb, "Invalid reply type!");
63 return ldb_module_done(ac->req, NULL, NULL,
64 LDB_ERR_OPERATIONS_ERROR);
67 return ldb_module_done(ac->req, ares->controls,
68 ares->response, ares->error);
71 /* add_record: add instancetype attribute */
72 static int instancetype_add(struct ldb_module *module, struct ldb_request *req)
74 struct ldb_context *ldb;
75 struct ldb_request *down_req;
76 struct ldb_message *msg;
77 struct it_context *ac;
78 uint32_t instance_type;
80 const struct ldb_control *partition_ctrl;
81 const struct dsdb_control_current_partition *partition;
83 ldb = ldb_module_get_ctx(module);
85 ldb_debug(ldb, LDB_DEBUG_TRACE, "instancetype_add_record\n");
87 /* do not manipulate our control entries */
88 if (ldb_dn_is_special(req->op.add.message->dn)) {
89 return ldb_next_request(module, req);
92 partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
93 if (!partition_ctrl) {
94 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
95 "instancetype_add: no current partition control found");
96 return LDB_ERR_CONSTRAINT_VIOLATION;
99 partition = talloc_get_type(partition_ctrl->data,
100 struct dsdb_control_current_partition);
101 SMB_ASSERT(partition && partition->version == DSDB_CONTROL_CURRENT_PARTITION_VERSION);
103 ac = talloc(req, struct it_context);
105 return LDB_ERR_OPERATIONS_ERROR;
110 /* we have to copy the message as the caller might have it as a const */
111 msg = ldb_msg_copy_shallow(ac, req->op.add.message);
114 return LDB_ERR_OPERATIONS_ERROR;
118 * TODO: calculate correct instance type
120 instance_type = INSTANCE_TYPE_WRITE;
121 if (ldb_dn_compare(partition->dn, msg->dn) == 0) {
122 instance_type |= INSTANCE_TYPE_IS_NC_HEAD;
123 if (ldb_dn_compare(msg->dn, samdb_base_dn(ldb)) != 0) {
124 instance_type |= INSTANCE_TYPE_NC_ABOVE;
128 ret = ldb_msg_add_fmt(msg, "instanceType", "%u", instance_type);
129 if (ret != LDB_SUCCESS) {
131 return LDB_ERR_OPERATIONS_ERROR;
134 ret = ldb_build_add_req(&down_req, ldb, ac,
139 if (ret != LDB_SUCCESS) {
143 /* go on with the call chain */
144 return ldb_next_request(module, down_req);
147 _PUBLIC_ const struct ldb_module_ops ldb_instancetype_module_ops = {
148 .name = "instancetype",
149 .add = instancetype_add,