4 Copyright (C) Simo Sorce 2004-2006
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
38 #include "ldb/include/ldb_includes.h"
39 #include "librpc/gen_ndr/ndr_misc.h"
40 #include "param/param.h"
41 #include "dsdb/samdb/samdb.h"
42 #include "dsdb/common/flags.h"
44 /* add_record: add instancetype attribute */
45 static int instancetype_add(struct ldb_module *module, struct ldb_request *req)
47 struct ldb_request *down_req;
48 struct ldb_message *msg;
49 uint32_t instance_type;
51 const struct ldb_control *partition_ctrl;
52 const struct dsdb_control_current_partition *partition;
55 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "instancetype_add_record\n");
57 /* do not manipulate our control entries */
58 if (ldb_dn_is_special(req->op.add.message->dn)) {
59 return ldb_next_request(module, req);
62 partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
63 if (!partition_ctrl) {
64 ldb_debug_set(module->ldb, LDB_DEBUG_FATAL,
65 "instancetype_add: no current partition control found");
66 return LDB_ERR_CONSTRAINT_VIOLATION;
69 partition = talloc_get_type(partition_ctrl->data,
70 struct dsdb_control_current_partition);
71 SMB_ASSERT(partition && partition->version == DSDB_CONTROL_CURRENT_PARTITION_VERSION);
73 down_req = talloc(req, struct ldb_request);
74 if (down_req == NULL) {
75 return LDB_ERR_OPERATIONS_ERROR;
80 /* we have to copy the message as the caller might have it as a const */
81 down_req->op.add.message = msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
83 talloc_free(down_req);
84 return LDB_ERR_OPERATIONS_ERROR;
88 * TODO: calculate correct instance type
90 instance_type = INSTANCE_TYPE_WRITE;
91 if (ldb_dn_compare(partition->dn, msg->dn) == 0) {
92 instance_type |= INSTANCE_TYPE_IS_NC_HEAD;
93 if (ldb_dn_compare(msg->dn, samdb_base_dn(module->ldb)) != 0) {
94 instance_type |= INSTANCE_TYPE_NC_ABOVE;
98 ret = ldb_msg_add_fmt(msg, "instanceType", "%u", instance_type);
99 if (ret != LDB_SUCCESS) {
100 talloc_free(down_req);
101 ldb_oom(module->ldb);
102 return LDB_ERR_OPERATIONS_ERROR;
105 ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
107 /* go on with the call chain */
108 ret = ldb_next_request(module, down_req);
110 /* do not free down_req as the call results may be linked to it,
111 * it will be freed when the upper level request get freed */
112 if (ret == LDB_SUCCESS) {
113 req->handle = down_req->handle;
119 static const struct ldb_module_ops instancetype_ops = {
120 .name = "instancetype",
121 .add = instancetype_add,
125 int ldb_instancetype_init(void)
127 return ldb_register_module(&instancetype_ops);