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/include/ldb_includes.h"
38 #include "librpc/gen_ndr/ndr_misc.h"
39 #include "param/param.h"
41 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
45 for (i = 0; i < msg->num_elements; i++) {
46 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
47 return &msg->elements[i];
55 add a time element to a record
57 static int add_time_element(struct ldb_message *msg, const char *attr, time_t t)
59 struct ldb_message_element *el;
62 if (ldb_msg_find_element(msg, attr) != NULL) {
66 s = ldb_timestring(msg, t);
71 if (ldb_msg_add_string(msg, attr, s) != 0) {
75 el = ldb_msg_find_element(msg, attr);
76 /* always set as replace. This works because on add ops, the flag
78 el->flags = LDB_FLAG_MOD_REPLACE;
84 add a uint64_t element to a record
86 static int add_uint64_element(struct ldb_message *msg, const char *attr, uint64_t v)
88 struct ldb_message_element *el;
90 if (ldb_msg_find_element(msg, attr) != NULL) {
94 if (ldb_msg_add_fmt(msg, attr, "%llu", (unsigned long long)v) != 0) {
98 el = ldb_msg_find_element(msg, attr);
99 /* always set as replace. This works because on add ops, the flag
101 el->flags = LDB_FLAG_MOD_REPLACE;
107 struct ldb_module *module;
108 struct ldb_request *req;
111 static int og_op_callback(struct ldb_request *req, struct ldb_reply *ares)
113 struct og_context *ac;
115 ac = talloc_get_type(req->context, struct og_context);
118 return ldb_module_done(ac->req, NULL, NULL,
119 LDB_ERR_OPERATIONS_ERROR);
121 if (ares->error != LDB_SUCCESS) {
122 return ldb_module_done(ac->req, ares->controls,
123 ares->response, ares->error);
126 if (ares->type != LDB_REPLY_DONE) {
128 return ldb_module_done(ac->req, NULL, NULL,
129 LDB_ERR_OPERATIONS_ERROR);
132 return ldb_module_done(ac->req, ares->controls,
133 ares->response, ares->error);
136 /* add_record: add objectGUID attribute */
137 static int objectguid_add(struct ldb_module *module, struct ldb_request *req)
139 struct ldb_request *down_req;
140 struct ldb_message_element *attribute;
141 struct ldb_message *msg;
145 enum ndr_err_code ndr_err;
147 time_t t = time(NULL);
148 struct og_context *ac;
150 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
152 /* do not manipulate our control entries */
153 if (ldb_dn_is_special(req->op.add.message->dn)) {
154 return ldb_next_request(module, req);
157 if ((attribute = objectguid_find_attribute(req->op.add.message, "objectGUID")) != NULL ) {
158 return ldb_next_request(module, req);
161 ac = talloc(req, struct og_context);
163 return LDB_ERR_OPERATIONS_ERROR;
168 /* we have to copy the message as the caller might have it as a const */
169 msg = ldb_msg_copy_shallow(ac, req->op.add.message);
171 talloc_free(down_req);
172 return LDB_ERR_OPERATIONS_ERROR;
176 guid = GUID_random();
178 ndr_err = ndr_push_struct_blob(&v, msg,
179 lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),
181 (ndr_push_flags_fn_t)ndr_push_GUID);
182 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
183 return LDB_ERR_OPERATIONS_ERROR;
186 ret = ldb_msg_add_value(msg, "objectGUID", &v, NULL);
191 if (add_time_element(msg, "whenCreated", t) != 0 ||
192 add_time_element(msg, "whenChanged", t) != 0) {
193 return LDB_ERR_OPERATIONS_ERROR;
196 /* Get a sequence number from the backend */
197 /* FIXME: ldb_sequence_number is still SYNC now, when this changes,
198 * make sure this function is split and a callback is used */
199 ret = ldb_sequence_number(module->ldb, LDB_SEQ_NEXT, &seq_num);
200 if (ret == LDB_SUCCESS) {
201 if (add_uint64_element(msg, "uSNCreated", seq_num) != 0 ||
202 add_uint64_element(msg, "uSNChanged", seq_num) != 0) {
203 return LDB_ERR_OPERATIONS_ERROR;
207 ret = ldb_build_add_req(&down_req, module->ldb, ac,
212 if (ret != LDB_SUCCESS) {
213 return LDB_ERR_OPERATIONS_ERROR;
216 /* go on with the call chain */
217 return ldb_next_request(module, down_req);
220 /* modify_record: update timestamps */
221 static int objectguid_modify(struct ldb_module *module, struct ldb_request *req)
223 struct ldb_request *down_req;
224 struct ldb_message *msg;
226 time_t t = time(NULL);
228 struct og_context *ac;
230 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
232 /* do not manipulate our control entries */
233 if (ldb_dn_is_special(req->op.add.message->dn)) {
234 return ldb_next_request(module, req);
237 ac = talloc(req, struct og_context);
239 return LDB_ERR_OPERATIONS_ERROR;
244 /* we have to copy the message as the caller might have it as a const */
245 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
247 return LDB_ERR_OPERATIONS_ERROR;
250 if (add_time_element(msg, "whenChanged", t) != 0) {
251 return LDB_ERR_OPERATIONS_ERROR;
254 /* Get a sequence number from the backend */
255 ret = ldb_sequence_number(module->ldb, LDB_SEQ_NEXT, &seq_num);
256 if (ret == LDB_SUCCESS) {
257 if (add_uint64_element(msg, "uSNChanged", seq_num) != 0) {
258 return LDB_ERR_OPERATIONS_ERROR;
262 ret = ldb_build_mod_req(&down_req, module->ldb, ac,
267 if (ret != LDB_SUCCESS) {
268 return LDB_ERR_OPERATIONS_ERROR;
271 /* go on with the call chain */
272 return ldb_next_request(module, down_req);
275 _PUBLIC_ const struct ldb_module_ops ldb_objectguid_module_ops = {
276 .name = "objectguid",
277 .add = objectguid_add,
278 .modify = objectguid_modify,