4 Copyright (C) Simo Sorce 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb objectguid module
30 * Description: add a unique objectGUID onto every new record
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
38 #include "librpc/gen_ndr/ndr_misc.h"
41 static int objectguid_search(struct ldb_module *module, const struct ldb_dn *base,
42 enum ldb_scope scope, const char *expression,
43 const char * const *attrs, struct ldb_message ***res)
45 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
46 return ldb_next_search(module, base, scope, expression, attrs, res);
49 static int objectguid_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
50 enum ldb_scope scope, struct ldb_parse_tree *tree,
51 const char * const *attrs, struct ldb_message ***res)
53 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
54 return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
57 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
61 for (i = 0; i < msg->num_elements; i++) {
62 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
63 return &msg->elements[i];
70 /* add_record: add crateTimestamp/modifyTimestamp attributes */
71 static int objectguid_add_record(struct ldb_module *module, const struct ldb_message *msg)
74 struct ldb_message *msg2;
75 struct ldb_message_element *attribute;
80 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
82 if (ldb_dn_is_special(msg->dn)) { /* do not manipulate our control entries */
83 return ldb_next_add_record(module, msg);
86 if ((attribute = objectguid_find_attribute(msg, "objectGUID")) != NULL ) {
87 return ldb_next_add_record(module, msg);
90 msg2 = talloc(module, struct ldb_message);
96 msg2->num_elements = msg->num_elements;
97 msg2->private_data = msg->private_data;
98 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
99 for (i = 0; i < msg2->num_elements; i++) {
100 msg2->elements[i] = msg->elements[i];
104 guid = GUID_random();
106 nt_status = ndr_push_struct_blob(&v, msg2, &guid,
107 (ndr_push_flags_fn_t)ndr_push_GUID);
108 if (!NT_STATUS_IS_OK(nt_status)) {
112 ret = ldb_msg_add_value(module->ldb, msg2, "objectGUID", &v);
117 ret = ldb_next_add_record(module, msg2);
123 /* modify_record: change modifyTimestamp as well */
124 static int objectguid_modify_record(struct ldb_module *module, const struct ldb_message *msg)
126 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_modify_record\n");
127 return ldb_next_modify_record(module, msg);
130 static int objectguid_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
132 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_delete_record\n");
133 return ldb_next_delete_record(module, dn);
136 static int objectguid_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
138 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_rename_record\n");
139 return ldb_next_rename_record(module, olddn, newdn);
142 static int objectguid_start_trans(struct ldb_module *module)
144 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_start_trans\n");
145 return ldb_next_start_trans(module);
148 static int objectguid_end_trans(struct ldb_module *module, int status)
150 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_end_trans\n");
151 return ldb_next_end_trans(module, status);
154 static int objectguid_destructor(void *module_ctx)
156 /* struct ldb_module *ctx = module_ctx; */
157 /* put your clean-up functions here */
161 static const struct ldb_module_ops objectguid_ops = {
162 .name = "objectguid",
163 .search = objectguid_search,
164 .search_bytree = objectguid_search_bytree,
165 .add_record = objectguid_add_record,
166 .modify_record = objectguid_modify_record,
167 .delete_record = objectguid_delete_record,
168 .rename_record = objectguid_rename_record,
169 .start_transaction = objectguid_start_trans,
170 .end_transaction = objectguid_end_trans
174 /* the init function */
175 #ifdef HAVE_DLOPEN_DISABLED
176 struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
178 struct ldb_module *objectguid_module_init(struct ldb_context *ldb, const char *options[])
181 struct ldb_module *ctx;
183 ctx = talloc(ldb, struct ldb_module);
187 ctx->private_data = NULL;
189 ctx->prev = ctx->next = NULL;
190 ctx->ops = &objectguid_ops;
192 talloc_set_destructor (ctx, objectguid_destructor);