4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Andrew Tridgell 2005
6 Copyright (C) Simo Sorce 2004-2008
8 ** NOTE! The following LGPL license applies to the ldb
9 ** library. This does NOT imply that all of Samba is released
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
29 * Component: ldb objectguid module
31 * Description: add a unique objectGUID onto every new record
37 #include "ldb_module.h"
38 #include "ldb_private.h"
39 #include "librpc/gen_ndr/ndr_misc.h"
40 #include "param/param.h"
42 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
46 for (i = 0; i < msg->num_elements; i++) {
47 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
48 return &msg->elements[i];
56 add a time element to a record
58 static int add_time_element(struct ldb_message *msg, const char *attr, time_t t)
60 struct ldb_message_element *el;
63 if (ldb_msg_find_element(msg, attr) != NULL) {
67 s = ldb_timestring(msg, t);
72 if (ldb_msg_add_string(msg, attr, s) != 0) {
76 el = ldb_msg_find_element(msg, attr);
77 /* always set as replace. This works because on add ops, the flag
79 el->flags = LDB_FLAG_MOD_REPLACE;
85 add a uint64_t element to a record
87 static int add_uint64_element(struct ldb_message *msg, const char *attr, uint64_t v)
89 struct ldb_message_element *el;
91 if (ldb_msg_find_element(msg, attr) != NULL) {
95 if (ldb_msg_add_fmt(msg, attr, "%llu", (unsigned long long)v) != 0) {
99 el = ldb_msg_find_element(msg, attr);
100 /* always set as replace. This works because on add ops, the flag
102 el->flags = LDB_FLAG_MOD_REPLACE;
108 struct ldb_module *module;
109 struct ldb_request *req;
112 static int og_op_callback(struct ldb_request *req, struct ldb_reply *ares)
114 struct og_context *ac;
116 ac = talloc_get_type(req->context, struct og_context);
119 return ldb_module_done(ac->req, NULL, NULL,
120 LDB_ERR_OPERATIONS_ERROR);
122 if (ares->error != LDB_SUCCESS) {
123 return ldb_module_done(ac->req, ares->controls,
124 ares->response, ares->error);
127 if (ares->type != LDB_REPLY_DONE) {
129 return ldb_module_done(ac->req, NULL, NULL,
130 LDB_ERR_OPERATIONS_ERROR);
133 return ldb_module_done(ac->req, ares->controls,
134 ares->response, ares->error);
137 /* add_record: add objectGUID attribute */
138 static int objectguid_add(struct ldb_module *module, struct ldb_request *req)
140 struct ldb_context *ldb;
141 struct ldb_request *down_req;
142 struct ldb_message_element *attribute;
143 struct ldb_message *msg;
147 enum ndr_err_code ndr_err;
149 time_t t = time(NULL);
150 struct og_context *ac;
152 ldb = ldb_module_get_ctx(module);
154 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
156 /* do not manipulate our control entries */
157 if (ldb_dn_is_special(req->op.add.message->dn)) {
158 return ldb_next_request(module, req);
161 if ((attribute = objectguid_find_attribute(req->op.add.message, "objectGUID")) != NULL ) {
162 return ldb_next_request(module, req);
165 ac = talloc(req, struct og_context);
167 return LDB_ERR_OPERATIONS_ERROR;
172 /* we have to copy the message as the caller might have it as a const */
173 msg = ldb_msg_copy_shallow(ac, req->op.add.message);
175 talloc_free(down_req);
176 return LDB_ERR_OPERATIONS_ERROR;
180 guid = GUID_random();
182 ndr_err = ndr_push_struct_blob(&v, msg,
183 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
185 (ndr_push_flags_fn_t)ndr_push_GUID);
186 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
187 return LDB_ERR_OPERATIONS_ERROR;
190 ret = ldb_msg_add_value(msg, "objectGUID", &v, NULL);
195 if (add_time_element(msg, "whenCreated", t) != 0 ||
196 add_time_element(msg, "whenChanged", t) != 0) {
197 return LDB_ERR_OPERATIONS_ERROR;
200 /* Get a sequence number from the backend */
201 /* FIXME: ldb_sequence_number is a semi-async call,
202 * make sure this function is split and a callback is used */
203 ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
204 if (ret == LDB_SUCCESS) {
205 if (add_uint64_element(msg, "uSNCreated", seq_num) != 0 ||
206 add_uint64_element(msg, "uSNChanged", seq_num) != 0) {
207 return LDB_ERR_OPERATIONS_ERROR;
211 ret = ldb_build_add_req(&down_req, ldb, ac,
216 if (ret != LDB_SUCCESS) {
217 return LDB_ERR_OPERATIONS_ERROR;
220 /* go on with the call chain */
221 return ldb_next_request(module, down_req);
224 /* modify_record: update timestamps */
225 static int objectguid_modify(struct ldb_module *module, struct ldb_request *req)
227 struct ldb_context *ldb;
228 struct ldb_request *down_req;
229 struct ldb_message *msg;
231 time_t t = time(NULL);
233 struct og_context *ac;
235 ldb = ldb_module_get_ctx(module);
237 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
239 /* do not manipulate our control entries */
240 if (ldb_dn_is_special(req->op.add.message->dn)) {
241 return ldb_next_request(module, req);
244 ac = talloc(req, struct og_context);
246 return LDB_ERR_OPERATIONS_ERROR;
251 /* we have to copy the message as the caller might have it as a const */
252 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
254 return LDB_ERR_OPERATIONS_ERROR;
257 if (add_time_element(msg, "whenChanged", t) != 0) {
258 return LDB_ERR_OPERATIONS_ERROR;
261 /* Get a sequence number from the backend */
262 ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
263 if (ret == LDB_SUCCESS) {
264 if (add_uint64_element(msg, "uSNChanged", seq_num) != 0) {
265 return LDB_ERR_OPERATIONS_ERROR;
269 ret = ldb_build_mod_req(&down_req, ldb, ac,
274 if (ret != LDB_SUCCESS) {
275 return LDB_ERR_OPERATIONS_ERROR;
278 /* go on with the call chain */
279 return ldb_next_request(module, down_req);
282 _PUBLIC_ const struct ldb_module_ops ldb_objectguid_module_ops = {
283 .name = "objectguid",
284 .add = objectguid_add,
285 .modify = objectguid_modify,