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"
40 static int timestamps_search(struct ldb_module *module, const struct ldb_dn *base,
41 enum ldb_scope scope, const char *expression,
42 const char * const *attrs, struct ldb_message ***res)
44 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_search\n");
45 return ldb_next_search(module, base, scope, expression, attrs, res);
48 static int timestamps_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
49 enum ldb_scope scope, struct ldb_parse_tree *tree,
50 const char * const *attrs, struct ldb_message ***res)
52 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_search\n");
53 return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
56 static int add_time_element(struct ldb_module *module, struct ldb_message *msg,
57 const char *attr_name, const char *time_string, unsigned int flags)
59 struct ldb_message_element *attribute = NULL;
63 for (i = 0; i < msg->num_elements; i++) {
64 if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) {
69 if (ldb_msg_add_string(module->ldb, msg, attr_name, time_string) != 0) {
73 for (i = 0; i < msg->num_elements; i++) {
74 if (ldb_attr_cmp(attr_name, msg->elements[i].name) == 0) {
75 attribute = &msg->elements[i];
84 attribute->flags = flags;
89 /* add_record: add crateTimestamp/modifyTimestamp attributes */
90 static int timestamps_add_record(struct ldb_module *module, const struct ldb_message *msg)
92 struct ldb_message *msg2 = NULL;
98 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_add_record\n");
100 /* do not manipulate our control entries */
101 if (ldb_dn_is_special(msg->dn)) {
102 return ldb_next_add_record(module, msg);
105 timeval = time(NULL);
106 tm = gmtime(&timeval);
111 msg2 = talloc(module, struct ldb_message);
116 /* formatted like: 20040408072012.0Z */
117 timestr = talloc_asprintf(msg2, "%04u%02u%02u%02u%02u%02u.0Z",
118 tm->tm_year+1900, tm->tm_mon+1,
119 tm->tm_mday, tm->tm_hour, tm->tm_min,
126 msg2->num_elements = msg->num_elements;
127 msg2->private_data = msg->private_data;
128 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
129 for (i = 0; i < msg2->num_elements; i++) {
130 msg2->elements[i] = msg->elements[i];
133 add_time_element(module, msg2, "createTimestamp", timestr, LDB_FLAG_MOD_ADD);
134 add_time_element(module, msg2, "modifyTimestamp", timestr, LDB_FLAG_MOD_ADD);
135 add_time_element(module, msg2, "whenCreated", timestr, LDB_FLAG_MOD_ADD);
136 add_time_element(module, msg2, "whenChanged", timestr, LDB_FLAG_MOD_ADD);
139 ret = ldb_next_add_record(module, msg2);
142 ret = ldb_next_add_record(module, msg);
148 /* modify_record: change modifyTimestamp as well */
149 static int timestamps_modify_record(struct ldb_module *module, const struct ldb_message *msg)
151 struct ldb_message *msg2 = NULL;
157 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_modify_record\n");
159 /* do not manipulate our control entries */
160 if (ldb_dn_is_special(msg->dn)) {
161 return ldb_next_modify_record(module, msg);
164 timeval = time(NULL);
165 tm = gmtime(&timeval);
170 msg2 = talloc(module, struct ldb_message);
175 /* formatted like: 20040408072012.0Z */
176 timestr = talloc_asprintf(msg2,
177 "%04u%02u%02u%02u%02u%02u.0Z",
178 tm->tm_year+1900, tm->tm_mon+1,
179 tm->tm_mday, tm->tm_hour, tm->tm_min,
186 msg2->num_elements = msg->num_elements;
187 msg2->private_data = msg->private_data;
188 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
189 for (i = 0; i < msg2->num_elements; i++) {
190 msg2->elements[i] = msg->elements[i];
193 add_time_element(module, msg2, "modifyTimestamp", timestr, LDB_FLAG_MOD_REPLACE);
194 add_time_element(module, msg2, "whenChanged", timestr, LDB_FLAG_MOD_REPLACE);
196 ret = ldb_next_modify_record(module, msg2);
202 static int timestamps_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
204 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_delete_record\n");
205 return ldb_next_delete_record(module, dn);
208 static int timestamps_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
210 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_rename_record\n");
211 return ldb_next_rename_record(module, olddn, newdn);
214 static int timestamps_start_trans(struct ldb_module *module)
216 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_start_trans\n");
217 return ldb_next_start_trans(module);
220 static int timestamps_end_trans(struct ldb_module *module, int status)
222 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_end_trans\n");
223 return ldb_next_end_trans(module, status);
226 static int timestamps_destructor(void *module_ctx)
228 /* struct ldb_module *ctx = module_ctx; */
229 /* put your clean-up functions here */
233 static const struct ldb_module_ops timestamps_ops = {
234 .name = "timestamps",
235 .search = timestamps_search,
236 .search_bytree = timestamps_search_bytree,
237 .add_record = timestamps_add_record,
238 .modify_record = timestamps_modify_record,
239 .delete_record = timestamps_delete_record,
240 .rename_record = timestamps_rename_record,
241 .start_transaction = timestamps_start_trans,
242 .end_transaction = timestamps_end_trans
246 /* the init function */
247 #ifdef HAVE_DLOPEN_DISABLED
248 struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
250 struct ldb_module *timestamps_module_init(struct ldb_context *ldb, const char *options[])
253 struct ldb_module *ctx;
255 ctx = talloc(ldb, struct ldb_module);
259 ctx->private_data = NULL;
261 ctx->prev = ctx->next = NULL;
262 ctx->ops = ×tamps_ops;
264 talloc_set_destructor (ctx, timestamps_destructor);