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 timestamps module
30 * Description: add object timestamping functionality
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
41 const char *error_string;
44 static int timestamps_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, "timestamps_search\n");
49 return ldb_next_search(module, base, scope, expression, attrs, res);
52 static int timestamps_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, "timestamps_search\n");
57 return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
60 static int add_time_element(struct ldb_module *module, struct ldb_message *msg,
61 const char *attr_name, const char *time_string, unsigned int flags)
63 struct ldb_val *values;
67 for (i = 0; i < msg->num_elements; i++) {
68 if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) {
73 msg->elements = talloc_realloc(msg, msg->elements,
74 struct ldb_message_element, msg->num_elements + 1);
75 name = talloc_strdup(msg->elements, attr_name);
76 timestr = talloc_strdup(msg->elements, time_string);
77 values = talloc(msg->elements, struct ldb_val);
78 if (!msg->elements || !name || !timestr || !values) {
82 msg->elements[msg->num_elements].name = name;
83 msg->elements[msg->num_elements].flags = flags;
84 msg->elements[msg->num_elements].num_values = 1;
85 msg->elements[msg->num_elements].values = values;
86 msg->elements[msg->num_elements].values[0].data = timestr;
87 msg->elements[msg->num_elements].values[0].length = strlen(timestr);
89 msg->num_elements += 1;
94 /* add_record: add crateTimestamp/modifyTimestamp attributes */
95 static int timestamps_add_record(struct ldb_module *module, const struct ldb_message *msg)
97 struct ldb_message *msg2 = NULL;
103 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_add_record\n");
105 if (msg->dn[0] == '@') { /* do not manipulate our control entries */
106 return ldb_next_add_record(module, msg);
109 timeval = time(NULL);
110 tm = gmtime(&timeval);
115 msg2 = talloc(module, struct ldb_message);
120 /* formatted like: 20040408072012.0Z */
121 timestr = talloc_asprintf(msg2, "%04u%02u%02u%02u%02u%02u.0Z",
122 tm->tm_year+1900, tm->tm_mon+1,
123 tm->tm_mday, tm->tm_hour, tm->tm_min,
130 msg2->num_elements = msg->num_elements;
131 msg2->private_data = msg->private_data;
132 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
133 for (i = 0; i < msg2->num_elements; i++) {
134 msg2->elements[i] = msg->elements[i];
137 add_time_element(module, msg2, "createTimestamp", timestr, LDB_FLAG_MOD_ADD);
138 add_time_element(module, msg2, "modifyTimestamp", timestr, LDB_FLAG_MOD_ADD);
139 add_time_element(module, msg2, "whenCreated", timestr, LDB_FLAG_MOD_ADD);
140 add_time_element(module, msg2, "whenChanged", timestr, LDB_FLAG_MOD_ADD);
143 ret = ldb_next_add_record(module, msg2);
146 ret = ldb_next_add_record(module, msg);
152 /* modify_record: change modifyTimestamp as well */
153 static int timestamps_modify_record(struct ldb_module *module, const struct ldb_message *msg)
155 struct ldb_message *msg2 = NULL;
161 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_modify_record\n");
163 if (msg->dn[0] == '@') { /* do not manipulate our control entries */
164 return ldb_next_modify_record(module, msg);
167 timeval = time(NULL);
168 tm = gmtime(&timeval);
173 msg2 = talloc(module, struct ldb_message);
178 /* formatted like: 20040408072012.0Z */
179 timestr = talloc_asprintf(msg2,
180 "%04u%02u%02u%02u%02u%02u.0Z",
181 tm->tm_year+1900, tm->tm_mon+1,
182 tm->tm_mday, tm->tm_hour, tm->tm_min,
189 msg2->num_elements = msg->num_elements;
190 msg2->private_data = msg->private_data;
191 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
192 for (i = 0; i < msg2->num_elements; i++) {
193 msg2->elements[i] = msg->elements[i];
196 add_time_element(module, msg2, "modifyTimestamp", timestr, LDB_FLAG_MOD_REPLACE);
197 add_time_element(module, msg2, "whenChanged", timestr, LDB_FLAG_MOD_REPLACE);
200 ret = ldb_next_modify_record(module, msg2);
203 ret = ldb_next_modify_record(module, msg);
209 static int timestamps_delete_record(struct ldb_module *module, const char *dn)
211 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_delete_record\n");
212 return ldb_next_delete_record(module, dn);
215 static int timestamps_rename_record(struct ldb_module *module, const char *olddn, const char *newdn)
217 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_rename_record\n");
218 return ldb_next_rename_record(module, olddn, newdn);
221 static int timestamps_lock(struct ldb_module *module, const char *lockname)
223 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_lock\n");
224 return ldb_next_named_lock(module, lockname);
227 static int timestamps_unlock(struct ldb_module *module, const char *lockname)
229 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_unlock\n");
230 return ldb_next_named_unlock(module, lockname);
233 /* return extended error information */
234 static const char *timestamps_errstring(struct ldb_module *module)
236 struct private_data *data = (struct private_data *)module->private_data;
238 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_errstring\n");
239 if (data->error_string) {
242 error = data->error_string;
243 data->error_string = NULL;
247 return ldb_next_errstring(module);
250 static int timestamps_destructor(void *module_ctx)
252 /* struct ldb_module *ctx = module_ctx; */
253 /* put your clean-up functions here */
257 static const struct ldb_module_ops timestamps_ops = {
258 .name = "timestamps",
259 .search = timestamps_search,
260 .search_bytree = timestamps_search_bytree,
261 .add_record = timestamps_add_record,
262 .modify_record = timestamps_modify_record,
263 .delete_record = timestamps_delete_record,
264 .rename_record = timestamps_rename_record,
265 .named_lock = timestamps_lock,
266 .named_unlock = timestamps_unlock,
267 .errstring = timestamps_errstring
271 /* the init function */
272 #ifdef HAVE_DLOPEN_DISABLED
273 struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
275 struct ldb_module *timestamps_module_init(struct ldb_context *ldb, const char *options[])
278 struct ldb_module *ctx;
279 struct private_data *data;
281 ctx = talloc(ldb, struct ldb_module);
285 data = talloc(ctx, struct private_data);
291 data->error_string = NULL;
292 ctx->private_data = data;
294 ctx->prev = ctx->next = NULL;
295 ctx->ops = ×tamps_ops;
297 talloc_set_destructor (ctx, timestamps_destructor);