s4:resolve_oids LDB module - not really a change but a nicer method to call "talloc_r...
[Samba/nascimento.git] / source4 / dsdb / samdb / ldb_modules / instancetype.c
blob0a297d587a9e13744e4f093d22e6c24072ebd382
1 /*
2 ldb database library
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/>.
24 * Name: ldb
26 * Component: ldb instancetype module
28 * Description: add an instanceType onto every new record
30 * Author: Andrew Bartlett
33 #include "includes.h"
34 #include "ldb.h"
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 struct it_context {
42 struct ldb_module *module;
43 struct ldb_request *req;
44 struct ldb_request *add_req;
47 static int it_add_callback(struct ldb_request *req, struct ldb_reply *ares)
49 struct ldb_context *ldb;
50 struct it_context *ac;
52 ac = talloc_get_type(req->context, struct it_context);
53 ldb = ldb_module_get_ctx(ac->module);
55 if (!ares) {
56 return ldb_module_done(ac->req, NULL, NULL,
57 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 /* Add the boilerplate entries */
73 return ldb_module_done(ac->req, ares->controls,
74 ares->response, ares->error);
77 /* add_record: add instancetype attribute */
78 static int instancetype_add(struct ldb_module *module, struct ldb_request *req)
80 struct ldb_context *ldb;
81 struct ldb_request *down_req;
82 struct ldb_message *msg;
83 struct it_context *ac;
84 uint32_t instance_type;
85 int ret;
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 if (ldb_msg_find_element(req->op.add.message, "instanceType")) {
97 unsigned int instanceType = ldb_msg_find_attr_as_uint(req->op.add.message, "instanceType", 0);
98 if (!(instanceType & INSTANCE_TYPE_IS_NC_HEAD)) {
99 return ldb_next_request(module, req);
102 /* Forward the 'add' to the modules below, but if it
103 * succeeds, then we might need to add the boilerplate
104 * entries (lost+found, deleted objects) */
105 ac = talloc(req, struct it_context);
106 if (ac == NULL) {
107 return LDB_ERR_OPERATIONS_ERROR;
109 ac->module = module;
110 ac->req = req;
112 ret = ldb_build_add_req(&ac->add_req, ldb_module_get_ctx(ac->module), ac,
113 ac->req->op.add.message,
114 ac->req->controls,
115 ac, it_add_callback,
116 ac->req);
118 if (ret != LDB_SUCCESS) {
119 return ret;
122 /* Do the original add */
123 return ldb_next_request(ac->module, ac->add_req);
126 /* we have to copy the message as the caller might have it as a const */
127 msg = ldb_msg_copy_shallow(req, req->op.add.message);
128 if (msg == NULL) {
129 ldb_oom(ldb);
130 return LDB_ERR_OPERATIONS_ERROR;
134 * TODO: calculate correct instance type
136 instance_type = INSTANCE_TYPE_WRITE;
138 ret = ldb_msg_add_fmt(msg, "instanceType", "%u", instance_type);
139 if (ret != LDB_SUCCESS) {
140 ldb_oom(ldb);
141 return LDB_ERR_OPERATIONS_ERROR;
144 ret = ldb_build_add_req(&down_req, ldb, req,
145 msg,
146 req->controls,
147 req, dsdb_next_callback,
148 req);
149 if (ret != LDB_SUCCESS) {
150 return ret;
153 /* go on with the call chain */
154 return ldb_next_request(module, down_req);
157 _PUBLIC_ const struct ldb_module_ops ldb_instancetype_module_ops = {
158 .name = "instancetype",
159 .add = instancetype_add,