r10300: forgot to change the dsdb modules function names
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / objectguid.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb objectguid module
29  *
30  *  Description: add a unique objectGUID onto every new record
31  *
32  *  Author: Simo Sorce
33  */
34
35 #include "includes.h"
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
38 #include "librpc/gen_ndr/ndr_misc.h"
39 #include <time.h>
40
41 struct private_data {
42         const char *error_string;
43 };
44
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)
48 {
49         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
50         return ldb_next_search(module, base, scope, expression, attrs, res);
51 }
52
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)
56 {
57         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
58         return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
59 }
60
61 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
62 {
63         int i;
64
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];
68                 }
69         }
70
71         return NULL;
72 }
73
74 /* add_record: add crateTimestamp/modifyTimestamp attributes */
75 static int objectguid_add_record(struct ldb_module *module, const struct ldb_message *msg)
76 {
77         struct ldb_val v;
78         struct ldb_message *msg2;
79         struct ldb_message_element *attribute;
80         struct GUID guid;
81         NTSTATUS nt_status;
82         int ret, i;
83
84         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
85
86         if (ldb_dn_is_special(msg->dn)) { /* do not manipulate our control entries */
87                 return ldb_next_add_record(module, msg);
88         }
89
90         if ((attribute = objectguid_find_attribute(msg, "objectGUID")) != NULL ) {
91                 return ldb_next_add_record(module, msg);
92         }
93
94         msg2 = talloc(module, struct ldb_message);
95         if (!msg2) {
96                 return -1;
97         }
98
99         msg2->dn = msg->dn;
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];
105         }
106
107         /* a new GUID */
108         guid = GUID_random();
109
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)) {
113                 return -1;
114         }
115
116         ret = ldb_msg_add_value(module->ldb, msg2, "objectGUID", &v);
117         if (ret) {
118                 return ret;
119         }
120
121         ret = ldb_next_add_record(module, msg2);
122         talloc_free(msg2);
123
124         return ret;
125 }
126
127 /* modify_record: change modifyTimestamp as well */
128 static int objectguid_modify_record(struct ldb_module *module, const struct ldb_message *msg)
129 {
130         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_modify_record\n");
131         return ldb_next_modify_record(module, msg);
132 }
133
134 static int objectguid_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
135 {
136         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_delete_record\n");
137         return ldb_next_delete_record(module, dn);
138 }
139
140 static int objectguid_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
141 {
142         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_rename_record\n");
143         return ldb_next_rename_record(module, olddn, newdn);
144 }
145
146 static int objectguid_start_trans(struct ldb_module *module)
147 {
148         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_start_trans\n");
149         return ldb_next_start_trans(module);
150 }
151
152 static int objectguid_end_trans(struct ldb_module *module, int status)
153 {
154         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_end_trans\n");
155         return ldb_next_end_trans(module, status);
156 }
157
158 /* return extended error information */
159 static const char *objectguid_errstring(struct ldb_module *module)
160 {
161         struct private_data *data = (struct private_data *)module->private_data;
162
163         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_errstring\n");
164         if (data->error_string) {
165                 const char *error;
166
167                 error = data->error_string;
168                 data->error_string = NULL;
169                 return error;
170         }
171
172         return ldb_next_errstring(module);
173 }
174
175 static int objectguid_destructor(void *module_ctx)
176 {
177         /* struct ldb_module *ctx = module_ctx; */
178         /* put your clean-up functions here */
179         return 0;
180 }
181
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
193 };
194
195
196 /* the init function */
197 #ifdef HAVE_DLOPEN_DISABLED
198  struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
199 #else
200 struct ldb_module *objectguid_module_init(struct ldb_context *ldb, const char *options[])
201 #endif
202 {
203         struct ldb_module *ctx;
204         struct private_data *data;
205
206         ctx = talloc(ldb, struct ldb_module);
207         if (!ctx)
208                 return NULL;
209
210         data = talloc(ctx, struct private_data);
211         if (!data) {
212                 talloc_free(ctx);
213                 return NULL;
214         }
215
216         data->error_string = NULL;
217         ctx->private_data = data;
218         ctx->ldb = ldb;
219         ctx->prev = ctx->next = NULL;
220         ctx->ops = &objectguid_ops;
221
222         talloc_set_destructor (ctx, objectguid_destructor);
223
224         return ctx;
225 }