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 "dsdb/samdb/samdb.h"
35 #include "librpc/gen_ndr/ndr_misc.h"
36 #include "param/param.h"
38 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
42 for (i = 0; i < msg->num_elements; i++) {
43 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
44 return &msg->elements[i];
52 add a time element to a record
54 static int add_time_element(struct ldb_message *msg, const char *attr, time_t t)
56 struct ldb_message_element *el;
60 if (ldb_msg_find_element(msg, attr) != NULL) {
64 s = ldb_timestring(msg, t);
66 return LDB_ERR_OPERATIONS_ERROR;
69 ret = ldb_msg_add_string(msg, attr, s);
70 if (ret != LDB_SUCCESS) {
74 el = ldb_msg_find_element(msg, attr);
75 /* always set as replace. This works because on add ops, the flag
77 el->flags = LDB_FLAG_MOD_REPLACE;
83 add a uint64_t element to a record
85 static int add_uint64_element(struct ldb_context *ldb, struct ldb_message *msg,
86 const char *attr, uint64_t v)
88 struct ldb_message_element *el;
91 if (ldb_msg_find_element(msg, attr) != NULL) {
95 ret = samdb_msg_add_uint64(ldb, msg, msg, attr, v);
96 if (ret != LDB_SUCCESS) {
100 el = ldb_msg_find_element(msg, attr);
101 /* always set as replace. This works because on add ops, the flag
103 el->flags = LDB_FLAG_MOD_REPLACE;
109 struct ldb_module *module;
110 struct ldb_request *req;
113 static int og_op_callback(struct ldb_request *req, struct ldb_reply *ares)
115 struct og_context *ac;
117 ac = talloc_get_type(req->context, struct og_context);
120 return ldb_module_done(ac->req, NULL, NULL,
121 LDB_ERR_OPERATIONS_ERROR);
123 if (ares->error != LDB_SUCCESS) {
124 return ldb_module_done(ac->req, ares->controls,
125 ares->response, ares->error);
128 if (ares->type != LDB_REPLY_DONE) {
130 return ldb_module_done(ac->req, NULL, NULL,
131 LDB_ERR_OPERATIONS_ERROR);
134 return ldb_module_done(ac->req, ares->controls,
135 ares->response, ares->error);
138 /* add_record: add objectGUID attribute */
139 static int objectguid_add(struct ldb_module *module, struct ldb_request *req)
141 struct ldb_context *ldb;
142 struct ldb_request *down_req;
143 struct ldb_message_element *attribute;
144 struct ldb_message *msg;
148 time_t t = time(NULL);
149 struct og_context *ac;
151 ldb = ldb_module_get_ctx(module);
153 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
155 /* do not manipulate our control entries */
156 if (ldb_dn_is_special(req->op.add.message->dn)) {
157 return ldb_next_request(module, req);
160 if ((attribute = objectguid_find_attribute(req->op.add.message, "objectGUID")) != NULL ) {
161 return ldb_next_request(module, req);
164 ac = talloc(req, struct og_context);
171 /* we have to copy the message as the caller might have it as a const */
172 msg = ldb_msg_copy_shallow(ac, req->op.add.message);
175 return ldb_operr(ldb);
179 guid = GUID_random();
181 ret = dsdb_msg_add_guid(msg, &guid, "objectGUID");
182 if (ret != LDB_SUCCESS) {
186 if (add_time_element(msg, "whenCreated", t) != LDB_SUCCESS ||
187 add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
188 return ldb_operr(ldb);
191 /* Get a sequence number from the backend */
192 /* FIXME: ldb_sequence_number is a semi-async call,
193 * make sure this function is split and a callback is used */
194 ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
195 if (ret == LDB_SUCCESS) {
196 if (add_uint64_element(ldb, msg, "uSNCreated",
197 seq_num) != LDB_SUCCESS ||
198 add_uint64_element(ldb, msg, "uSNChanged",
199 seq_num) != LDB_SUCCESS) {
200 return ldb_operr(ldb);
204 ret = ldb_build_add_req(&down_req, ldb, ac,
209 LDB_REQ_SET_LOCATION(down_req);
210 if (ret != LDB_SUCCESS) {
214 /* go on with the call chain */
215 return ldb_next_request(module, down_req);
218 /* modify_record: update timestamps */
219 static int objectguid_modify(struct ldb_module *module, struct ldb_request *req)
221 struct ldb_context *ldb;
222 struct ldb_request *down_req;
223 struct ldb_message *msg;
225 time_t t = time(NULL);
227 struct og_context *ac;
229 ldb = ldb_module_get_ctx(module);
231 ldb_debug(ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
233 /* do not manipulate our control entries */
234 if (ldb_dn_is_special(req->op.add.message->dn)) {
235 return ldb_next_request(module, req);
238 ac = talloc(req, struct og_context);
245 /* we have to copy the message as the caller might have it as a const */
246 msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
248 return ldb_operr(ldb);
251 if (add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
252 return ldb_operr(ldb);
255 /* Get a sequence number from the backend */
256 ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
257 if (ret == LDB_SUCCESS) {
258 if (add_uint64_element(ldb, msg, "uSNChanged",
259 seq_num) != LDB_SUCCESS) {
260 return ldb_operr(ldb);
264 ret = ldb_build_mod_req(&down_req, ldb, ac,
269 LDB_REQ_SET_LOCATION(down_req);
270 if (ret != LDB_SUCCESS) {
274 /* go on with the call chain */
275 return ldb_next_request(module, down_req);
278 _PUBLIC_ const struct ldb_module_ops ldb_objectguid_module_ops = {
279 .name = "objectguid",
280 .add = objectguid_add,
281 .modify = objectguid_modify,