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"
41 const char *error_string;
44 static int objectguid_search(struct ldb_module *module, const char *base,
45 enum ldb_scope scope, const char *expression,
46 const char * const *attrs, struct ldb_message ***res)
48 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
49 return ldb_next_search(module, base, scope, expression, attrs, res);
52 static int objectguid_search_bytree(struct ldb_module *module, const char *base,
53 enum ldb_scope scope, struct ldb_parse_tree *tree,
54 const char * const *attrs, struct ldb_message ***res)
56 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
57 return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
60 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
64 for (i = 0; i < msg->num_elements; i++) {
65 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
66 return &msg->elements[i];
73 /* add_record: add crateTimestamp/modifyTimestamp attributes */
74 static int objectguid_add_record(struct ldb_module *module, const struct ldb_message *msg)
76 struct ldb_message *msg2;
77 struct ldb_message_element *attribute;
82 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
84 if (msg->dn[0] == '@') { /* do not manipulate our control entries */
85 return ldb_next_add_record(module, msg);
88 if ((attribute = objectguid_find_attribute(msg, "objectGUID")) != NULL ) {
89 return ldb_next_add_record(module, msg);
92 msg2 = talloc(module, struct ldb_message);
98 msg2->num_elements = msg->num_elements;
99 msg2->private_data = msg->private_data;
100 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
101 for (i = 0; i < msg2->num_elements; i++) {
102 msg2->elements[i] = msg->elements[i];
106 guid = GUID_random();
107 guidstr = GUID_string(msg2, &guid);
112 if (ldb_msg_add_string(module->ldb, msg2, "objectGUID", guidstr) != 0) {
116 ret = ldb_next_add_record(module, msg2);
122 /* modify_record: change modifyTimestamp as well */
123 static int objectguid_modify_record(struct ldb_module *module, const struct ldb_message *msg)
125 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_modify_record\n");
126 return ldb_next_modify_record(module, msg);
129 static int objectguid_delete_record(struct ldb_module *module, const char *dn)
131 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_delete_record\n");
132 return ldb_next_delete_record(module, dn);
135 static int objectguid_rename_record(struct ldb_module *module, const char *olddn, const char *newdn)
137 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_rename_record\n");
138 return ldb_next_rename_record(module, olddn, newdn);
141 static int objectguid_lock(struct ldb_module *module, const char *lockname)
143 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_lock\n");
144 return ldb_next_named_lock(module, lockname);
147 static int objectguid_unlock(struct ldb_module *module, const char *lockname)
149 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_unlock\n");
150 return ldb_next_named_unlock(module, lockname);
153 /* return extended error information */
154 static const char *objectguid_errstring(struct ldb_module *module)
156 struct private_data *data = (struct private_data *)module->private_data;
158 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_errstring\n");
159 if (data->error_string) {
162 error = data->error_string;
163 data->error_string = NULL;
167 return ldb_next_errstring(module);
170 static int objectguid_destructor(void *module_ctx)
172 /* struct ldb_module *ctx = module_ctx; */
173 /* put your clean-up functions here */
177 static const struct ldb_module_ops objectguid_ops = {
178 .name = "objectguid",
179 .search = objectguid_search,
180 .search_bytree = objectguid_search_bytree,
181 .add_record = objectguid_add_record,
182 .modify_record = objectguid_modify_record,
183 .delete_record = objectguid_delete_record,
184 .rename_record = objectguid_rename_record,
185 .named_lock = objectguid_lock,
186 .named_unlock = objectguid_unlock,
187 .errstring = objectguid_errstring
191 /* the init function */
192 #ifdef HAVE_DLOPEN_DISABLED
193 struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
195 struct ldb_module *objectguid_module_init(struct ldb_context *ldb, const char *options[])
198 struct ldb_module *ctx;
199 struct private_data *data;
201 ctx = talloc(ldb, struct ldb_module);
205 data = talloc(ctx, struct private_data);
211 data->error_string = NULL;
212 ctx->private_data = data;
214 ctx->prev = ctx->next = NULL;
215 ctx->ops = &objectguid_ops;
217 talloc_set_destructor (ctx, objectguid_destructor);