4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Andrew Tridgell 2005
6 Copyright (C) Simo Sorce 2004-2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Component: ldb objectguid module
27 * Description: add a unique objectGUID onto every new record
33 #include "ldb_module.h"
34 #include "librpc/gen_ndr/ndr_misc.h"
35 #include "param/param.h"
37 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
41 for (i = 0; i < msg->num_elements; i++) {
42 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
43 return &msg->elements[i];
51 add a time element to a record
53 static int add_time_element(struct ldb_message *msg, const char *attr, time_t t)
55 struct ldb_message_element *el;
58 if (ldb_msg_find_element(msg, attr) != NULL) {
62 s = ldb_timestring(msg, t);
67 if (ldb_msg_add_string(msg, attr, s) != 0) {
71 el = ldb_msg_find_element(msg, attr);
72 /* always set as replace. This works because on add ops, the flag
74 el->flags = LDB_FLAG_MOD_REPLACE;
80 add a uint64_t element to a record
82 static int add_uint64_element(struct ldb_message *msg, const char *attr, uint64_t v)
84 struct ldb_message_element *el;
86 if (ldb_msg_find_element(msg, attr) != NULL) {
90 if (ldb_msg_add_fmt(msg, attr, "%llu", (unsigned long long)v) != 0) {
94 el = ldb_msg_find_element(msg, attr);
95 /* always set as replace. This works because on add ops, the flag
97 el->flags = LDB_FLAG_MOD_REPLACE;
103 struct ldb_module *module;
104 struct ldb_request *req;
107 static int og_op_callback(struct ldb_request *req, struct ldb_reply *ares)
109 struct og_context *ac;
111 ac = talloc_get_type(req->context, struct og_context);
114 return ldb_module_done(ac->req, NULL, NULL,
115 LDB_ERR_OPERATIONS_ERROR);
117 if (ares->error != LDB_SUCCESS) {
118 return ldb_module_done(ac->req, ares->controls,
119 ares->response, ares->error);
122 if (ares->type != LDB_REPLY_DONE) {
124 return ldb_module_done(ac->req, NULL, NULL,
125 LDB_ERR_OPERATIONS_ERROR);
128 return ldb_module_done(ac->req, ares->controls,
129 ares->response, ares->error);
132 /* add_record: add objectGUID attribute */
133 static int objectguid_add(struct ldb_module *module, struct ldb_request *req)
135 struct ldb_context *ldb;
136 struct ldb_request *down_req;
137 struct ldb_message_element *attribute;
138 struct ldb_message *msg;
142 enum ndr_err_code ndr_err;
144 time_t t = time(NULL);
145 struct og_context *ac;
147 ldb = ldb_module_get_ctx(module);
149 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
151 /* do not manipulate our control entries */
152 if (ldb_dn_is_special(req->op.add.message->dn)) {
153 return ldb_next_request(module, req);
156 if ((attribute = objectguid_find_attribute(req->op.add.message, "objectGUID")) != NULL ) {
157 return ldb_next_request(module, req);
160 ac = talloc(req, struct og_context);
162 return LDB_ERR_OPERATIONS_ERROR;
167 /* we have to copy the message as the caller might have it as a const */
168 msg = ldb_msg_copy_shallow(ac, req->op.add.message);
170 talloc_free(down_req);
171 return LDB_ERR_OPERATIONS_ERROR;
175 guid = GUID_random();
177 ndr_err = ndr_push_struct_blob(&v, msg,
178 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
180 (ndr_push_flags_fn_t)ndr_push_GUID);
181 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
182 return LDB_ERR_OPERATIONS_ERROR;
185 ret = ldb_msg_add_value(msg, "objectGUID", &v, NULL);
190 if (add_time_element(msg, "whenCreated", t) != 0 ||
191 add_time_element(msg, "whenChanged", t) != 0) {
192 return LDB_ERR_OPERATIONS_ERROR;
195 /* Get a sequence number from the backend */
196 /* FIXME: ldb_sequence_number is a semi-async call,
197 * make sure this function is split and a callback is used */
198 ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
199 if (ret == LDB_SUCCESS) {
200 if (add_uint64_element(msg, "uSNCreated", seq_num) != 0 ||
201 add_uint64_element(msg, "uSNChanged", seq_num) != 0) {
202 return LDB_ERR_OPERATIONS_ERROR;
206 ret = ldb_build_add_req(&down_req, ldb, ac,
211 if (ret != LDB_SUCCESS) {
215 /* go on with the call chain */
216 return ldb_next_request(module, down_req);
219 /* modify_record: update timestamps */
220 static int objectguid_modify(struct ldb_module *module, struct ldb_request *req)
222 struct ldb_context *ldb;
223 struct ldb_request *down_req;
224 struct ldb_message *msg;
226 time_t t = time(NULL);
228 struct og_context *ac;
230 ldb = ldb_module_get_ctx(module);
232 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
234 /* do not manipulate our control entries */
235 if (ldb_dn_is_special(req->op.add.message->dn)) {
236 return ldb_next_request(module, req);
239 ac = talloc(req, struct og_context);
241 return LDB_ERR_OPERATIONS_ERROR;
246 /* we have to copy the message as the caller might have it as a const */
247 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
249 return LDB_ERR_OPERATIONS_ERROR;
252 if (add_time_element(msg, "whenChanged", t) != 0) {
253 return LDB_ERR_OPERATIONS_ERROR;
256 /* Get a sequence number from the backend */
257 ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
258 if (ret == LDB_SUCCESS) {
259 if (add_uint64_element(msg, "uSNChanged", seq_num) != 0) {
260 return LDB_ERR_OPERATIONS_ERROR;
264 ret = ldb_build_mod_req(&down_req, ldb, ac,
269 if (ret != LDB_SUCCESS) {
273 /* go on with the call chain */
274 return ldb_next_request(module, down_req);
277 _PUBLIC_ const struct ldb_module_ops ldb_objectguid_module_ops = {
278 .name = "objectguid",
279 .add = objectguid_add,
280 .modify = objectguid_modify,