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_bytree(struct ldb_module *module, const struct ldb_dn *base,
41 enum ldb_scope scope, struct ldb_parse_tree *tree,
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_bytree(module, base, scope, tree, attrs, res);
48 static int add_time_element(struct ldb_module *module, struct ldb_message *msg,
49 const char *attr_name, const char *time_string, unsigned int flags)
51 struct ldb_message_element *attribute = NULL;
55 for (i = 0; i < msg->num_elements; i++) {
56 if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) {
61 if (ldb_msg_add_string(module->ldb, msg, attr_name, time_string) != 0) {
65 for (i = 0; i < msg->num_elements; i++) {
66 if (ldb_attr_cmp(attr_name, msg->elements[i].name) == 0) {
67 attribute = &msg->elements[i];
76 attribute->flags = flags;
81 /* add_record: add crateTimestamp/modifyTimestamp attributes */
82 static int timestamps_add_record(struct ldb_module *module, const struct ldb_message *msg)
84 struct ldb_message *msg2 = NULL;
90 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_add_record\n");
92 /* do not manipulate our control entries */
93 if (ldb_dn_is_special(msg->dn)) {
94 return ldb_next_add_record(module, msg);
98 tm = gmtime(&timeval);
103 msg2 = talloc(module, struct ldb_message);
108 /* formatted like: 20040408072012.0Z */
109 timestr = talloc_asprintf(msg2, "%04u%02u%02u%02u%02u%02u.0Z",
110 tm->tm_year+1900, tm->tm_mon+1,
111 tm->tm_mday, tm->tm_hour, tm->tm_min,
118 msg2->num_elements = msg->num_elements;
119 msg2->private_data = msg->private_data;
120 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
121 for (i = 0; i < msg2->num_elements; i++) {
122 msg2->elements[i] = msg->elements[i];
125 add_time_element(module, msg2, "createTimestamp", timestr, LDB_FLAG_MOD_ADD);
126 add_time_element(module, msg2, "modifyTimestamp", timestr, LDB_FLAG_MOD_ADD);
127 add_time_element(module, msg2, "whenCreated", timestr, LDB_FLAG_MOD_ADD);
128 add_time_element(module, msg2, "whenChanged", timestr, LDB_FLAG_MOD_ADD);
131 ret = ldb_next_add_record(module, msg2);
134 ret = ldb_next_add_record(module, msg);
140 /* modify_record: change modifyTimestamp as well */
141 static int timestamps_modify_record(struct ldb_module *module, const struct ldb_message *msg)
143 struct ldb_message *msg2 = NULL;
149 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_modify_record\n");
151 /* do not manipulate our control entries */
152 if (ldb_dn_is_special(msg->dn)) {
153 return ldb_next_modify_record(module, msg);
156 timeval = time(NULL);
157 tm = gmtime(&timeval);
162 msg2 = talloc(module, struct ldb_message);
167 /* formatted like: 20040408072012.0Z */
168 timestr = talloc_asprintf(msg2,
169 "%04u%02u%02u%02u%02u%02u.0Z",
170 tm->tm_year+1900, tm->tm_mon+1,
171 tm->tm_mday, tm->tm_hour, tm->tm_min,
178 msg2->num_elements = msg->num_elements;
179 msg2->private_data = msg->private_data;
180 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg2->num_elements);
181 for (i = 0; i < msg2->num_elements; i++) {
182 msg2->elements[i] = msg->elements[i];
185 add_time_element(module, msg2, "modifyTimestamp", timestr, LDB_FLAG_MOD_REPLACE);
186 add_time_element(module, msg2, "whenChanged", timestr, LDB_FLAG_MOD_REPLACE);
188 ret = ldb_next_modify_record(module, msg2);
195 static const struct ldb_module_ops timestamps_ops = {
196 .name = "timestamps",
197 .search_bytree = timestamps_search_bytree,
198 .add_record = timestamps_add_record,
199 .modify_record = timestamps_modify_record
203 /* the init function */
204 #ifdef HAVE_DLOPEN_DISABLED
205 struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
207 struct ldb_module *timestamps_module_init(struct ldb_context *ldb, const char *options[])
210 struct ldb_module *ctx;
212 ctx = talloc(ldb, struct ldb_module);
216 ctx->private_data = NULL;
218 ctx->prev = ctx->next = NULL;
219 ctx->ops = ×tamps_ops;