4 Copyright (C) Simo Sorce 2004-2006
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Component: ldb objectguid module
31 * Description: add a unique objectGUID onto every new record
37 #include "ldb/include/includes.h"
38 #include "librpc/gen_ndr/ndr_misc.h"
40 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
44 for (i = 0; i < msg->num_elements; i++) {
45 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
46 return &msg->elements[i];
53 /* add_record: add objectGUID attribute */
54 static int objectguid_add(struct ldb_module *module, struct ldb_request *req)
56 struct ldb_request *down_req;
57 struct ldb_message_element *attribute;
58 struct ldb_message *msg;
64 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
66 /* do not manipulate our control entries */
67 if (ldb_dn_is_special(req->op.add.message->dn)) {
68 return ldb_next_request(module, req);
71 if ((attribute = objectguid_find_attribute(req->op.add.message, "objectGUID")) != NULL ) {
72 return ldb_next_request(module, req);
75 down_req = talloc(req, struct ldb_request);
76 if (down_req == NULL) {
77 return LDB_ERR_OPERATIONS_ERROR;
82 /* we have to copy the message as the caller might have it as a const */
83 down_req->op.add.message = msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
85 return LDB_ERR_OPERATIONS_ERROR;
91 nt_status = ndr_push_struct_blob(&v, msg, &guid,
92 (ndr_push_flags_fn_t)ndr_push_GUID);
93 if (!NT_STATUS_IS_OK(nt_status)) {
97 ret = ldb_msg_add_value(msg, "objectGUID", &v);
102 ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
104 /* go on with the call chain */
105 ret = ldb_next_request(module, down_req);
107 /* do not free down_req as the call results may be linked to it,
108 * it will be freed when the upper level request get freed */
109 if (ret == LDB_SUCCESS) {
110 req->async.handle = down_req->async.handle;
116 static const struct ldb_module_ops objectguid_ops = {
117 .name = "objectguid",
118 .add = objectguid_add,
122 int objectguid_module_init(void)
124 return ldb_register_module(&objectguid_ops);