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
35 #include "ldb_module.h"
36 #include "librpc/gen_ndr/ndr_misc.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "../libds/common/flags.h"
39 #include "dsdb/samdb/ldb_modules/util.h"
41 /* add_record: add instancetype attribute */
42 static int instancetype_add(struct ldb_module *module, struct ldb_request *req)
44 struct ldb_context *ldb = ldb_module_get_ctx(module);
45 struct ldb_request *down_req;
46 struct ldb_message *msg;
47 struct ldb_message_element *el;
48 uint32_t instanceType;
51 /* do not manipulate our control entries */
52 if (ldb_dn_is_special(req->op.add.message->dn)) {
53 return ldb_next_request(module, req);
56 ldb_debug(ldb, LDB_DEBUG_TRACE, "instancetype_add\n");
58 el = ldb_msg_find_element(req->op.add.message, "instanceType");
60 if (el->num_values != 1) {
61 ldb_set_errstring(ldb, "instancetype: the 'instanceType' attribute is single-valued!");
62 return LDB_ERR_UNWILLING_TO_PERFORM;
65 instanceType = ldb_msg_find_attr_as_uint(req->op.add.message,
67 if (!(instanceType & INSTANCE_TYPE_IS_NC_HEAD)) {
69 * If we have no NC add operation (no TYPE_IS_NC_HEAD)
70 * then "instanceType" can only be "0" or "TYPE_WRITE".
72 if ((instanceType != 0) &&
73 ((instanceType & INSTANCE_TYPE_WRITE) == 0)) {
74 ldb_set_errstring(ldb, "instancetype: if TYPE_IS_NC_HEAD wasn't set, then only TYPE_WRITE or 0 are allowed!");
75 return LDB_ERR_UNWILLING_TO_PERFORM;
79 * If we have a NC add operation then we need also the
80 * "TYPE_WRITE" flag in order to succeed.
82 if (!(instanceType & INSTANCE_TYPE_WRITE)) {
83 ldb_set_errstring(ldb, "instancetype: if TYPE_IS_NC_HEAD was set, then also TYPE_WRITE is requested!");
84 return LDB_ERR_UNWILLING_TO_PERFORM;
88 /* we did only tests, so proceed with the original request */
89 return ldb_next_request(module, req);
92 /* we have to copy the message as the caller might have it as a const */
93 msg = ldb_msg_copy_shallow(req, req->op.add.message);
99 * TODO: calculate correct instance type
101 instanceType = INSTANCE_TYPE_WRITE;
103 ret = samdb_msg_add_uint(ldb, msg, msg, "instanceType", instanceType);
104 if (ret != LDB_SUCCESS) {
108 ret = ldb_build_add_req(&down_req, ldb, req,
111 req, dsdb_next_callback,
113 LDB_REQ_SET_LOCATION(down_req);
114 if (ret != LDB_SUCCESS) {
118 /* go on with the call chain */
119 return ldb_next_request(module, down_req);
122 /* deny instancetype modification */
123 static int instancetype_mod(struct ldb_module *module, struct ldb_request *req)
125 struct ldb_context *ldb = ldb_module_get_ctx(module);
126 struct ldb_message_element *el;
128 /* do not manipulate our control entries */
129 if (ldb_dn_is_special(req->op.mod.message->dn)) {
130 return ldb_next_request(module, req);
133 ldb_debug(ldb, LDB_DEBUG_TRACE, "instancetype_mod\n");
135 el = ldb_msg_find_element(req->op.mod.message, "instanceType");
137 ldb_set_errstring(ldb, "instancetype: the 'instanceType' attribute can never be changed!");
138 return LDB_ERR_CONSTRAINT_VIOLATION;
141 return ldb_next_request(module, req);
144 static const struct ldb_module_ops ldb_instancetype_module_ops = {
145 .name = "instancetype",
146 .add = instancetype_add,
147 .modify = instancetype_mod
150 int ldb_instancetype_module_init(const char *version)
152 LDB_MODULE_CHECK_VERSION(version);
153 return ldb_register_module(&ldb_instancetype_module_ops);