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"
42 const char *error_string;
45 static int objectguid_search(struct ldb_module *module, const struct ldb_dn *base,
46 enum ldb_scope scope, const char *expression,
47 const char * const *attrs, struct ldb_message ***res)
49 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
50 return ldb_next_search(module, base, scope, expression, attrs, res);
53 static int objectguid_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
54 enum ldb_scope scope, struct ldb_parse_tree *tree,
55 const char * const *attrs, struct ldb_message ***res)
57 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
58 return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
61 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
65 for (i = 0; i < msg->num_elements; i++) {
66 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
67 return &msg->elements[i];
74 /* add_record: add crateTimestamp/modifyTimestamp attributes */
75 static int objectguid_add_record(struct ldb_module *module, const struct ldb_message *msg)
78 struct ldb_message *msg2;
79 struct ldb_message_element *attribute;
84 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
86 if (ldb_dn_is_special(msg->dn)) { /* do not manipulate our control entries */
87 return ldb_next_add_record(module, msg);
90 if ((attribute = objectguid_find_attribute(msg, "objectGUID")) != NULL ) {
91 return ldb_next_add_record(module, msg);
94 msg2 = talloc(module, struct ldb_message);
100 msg2->num_elements = msg->num_elements;
101 msg2->private_data = msg->private_data;
102 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
103 for (i = 0; i < msg2->num_elements; i++) {
104 msg2->elements[i] = msg->elements[i];
108 guid = GUID_random();
110 nt_status = ndr_push_struct_blob(&v, msg2, &guid,
111 (ndr_push_flags_fn_t)ndr_push_GUID);
112 if (!NT_STATUS_IS_OK(nt_status)) {
116 ret = ldb_msg_add_value(module->ldb, msg2, "objectGUID", &v);
121 ret = ldb_next_add_record(module, msg2);
127 /* modify_record: change modifyTimestamp as well */
128 static int objectguid_modify_record(struct ldb_module *module, const struct ldb_message *msg)
130 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_modify_record\n");
131 return ldb_next_modify_record(module, msg);
134 static int objectguid_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
136 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_delete_record\n");
137 return ldb_next_delete_record(module, dn);
140 static int objectguid_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
142 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_rename_record\n");
143 return ldb_next_rename_record(module, olddn, newdn);
146 static int objectguid_start_trans(struct ldb_module *module)
148 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_start_trans\n");
149 return ldb_next_start_trans(module);
152 static int objectguid_end_trans(struct ldb_module *module, int status)
154 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_end_trans\n");
155 return ldb_next_end_trans(module, status);
158 /* return extended error information */
159 static const char *objectguid_errstring(struct ldb_module *module)
161 struct private_data *data = (struct private_data *)module->private_data;
163 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_errstring\n");
164 if (data->error_string) {
167 error = data->error_string;
168 data->error_string = NULL;
172 return ldb_next_errstring(module);
175 static int objectguid_destructor(void *module_ctx)
177 /* struct ldb_module *ctx = module_ctx; */
178 /* put your clean-up functions here */
182 static const struct ldb_module_ops objectguid_ops = {
183 .name = "objectguid",
184 .search = objectguid_search,
185 .search_bytree = objectguid_search_bytree,
186 .add_record = objectguid_add_record,
187 .modify_record = objectguid_modify_record,
188 .delete_record = objectguid_delete_record,
189 .rename_record = objectguid_rename_record,
190 .start_transaction = objectguid_start_trans,
191 .end_transaction = objectguid_end_trans,
192 .errstring = objectguid_errstring
196 /* the init function */
197 #ifdef HAVE_DLOPEN_DISABLED
198 struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
200 struct ldb_module *objectguid_module_init(struct ldb_context *ldb, const char *options[])
203 struct ldb_module *ctx;
204 struct private_data *data;
206 ctx = talloc(ldb, struct ldb_module);
210 data = talloc(ctx, struct private_data);
216 data->error_string = NULL;
217 ctx->private_data = data;
219 ctx->prev = ctx->next = NULL;
220 ctx->ops = &objectguid_ops;
222 talloc_set_destructor (ctx, objectguid_destructor);