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;
81 ldb = ldb_module_get_ctx(module);
83 ldb_debug(ldb, LDB_DEBUG_TRACE, "instancetype_add_record\n");
85 /* do not manipulate our control entries */
86 if (ldb_dn_is_special(req->op.add.message->dn)) {
87 return ldb_next_request(module, req);
90 if (ldb_msg_find_element(req->op.add.message, "instanceType")) {
91 unsigned int instanceType = ldb_msg_find_attr_as_uint(req->op.add.message, "instanceType", 0);
93 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
94 /* Do something in future */
97 /* TODO: we need to validate and possibly create a new
99 return ldb_next_request(module, req);
102 /* we have to copy the message as the caller might have it as a const */
103 msg = ldb_msg_copy_shallow(req, req->op.add.message);
106 return LDB_ERR_OPERATIONS_ERROR;
110 * TODO: calculate correct instance type
112 instance_type = INSTANCE_TYPE_WRITE;
114 ret = ldb_msg_add_fmt(msg, "instanceType", "%u", instance_type);
115 if (ret != LDB_SUCCESS) {
117 return LDB_ERR_OPERATIONS_ERROR;
120 ret = ldb_build_add_req(&down_req, ldb, req,
123 req->context, req->callback,
125 if (ret != LDB_SUCCESS) {
129 /* go on with the call chain */
130 return ldb_next_request(module, down_req);
133 _PUBLIC_ const struct ldb_module_ops ldb_instancetype_module_ops = {
134 .name = "instancetype",
135 .add = instancetype_add,