r8668: fixed a segv during upgrade of a very old ldb.
[ira/wip.git] / source4 / lib / 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 <time.h>
39
40 struct private_data {
41         const char *error_string;
42 };
43
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)
47 {
48         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
49         return ldb_next_search(module, base, scope, expression, attrs, res);
50 }
51
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)
55 {
56         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_search\n");
57         return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
58 }
59
60 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
61 {
62         int i;
63
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];
67                 }
68         }
69
70         return NULL;
71 }
72
73 /* add_record: add crateTimestamp/modifyTimestamp attributes */
74 static int objectguid_add_record(struct ldb_module *module, const struct ldb_message *msg)
75 {
76         struct ldb_message *msg2;
77         struct ldb_message_element *attribute;
78         struct GUID guid;
79         char *guidstr;
80         int ret, i;
81
82         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
83
84         if (msg->dn[0] == '@') { /* do not manipulate our control entries */
85                 return ldb_next_add_record(module, msg);
86         }
87
88         if ((attribute = objectguid_find_attribute(msg, "objectGUID")) != NULL ) {
89                 return ldb_next_add_record(module, msg);
90         }
91
92         msg2 = talloc(module, struct ldb_message);
93         if (!msg2) {
94                 return -1;
95         }
96
97         msg2->dn = msg->dn;
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];
103         }
104
105         /* a new GUID */
106         guid = GUID_random();
107         guidstr = GUID_string(msg2, &guid);
108         if (!guidstr) {
109                 return -1;
110         }
111
112         if (ldb_msg_add_string(module->ldb, msg2, "objectGUID", guidstr) != 0) {
113                 return -1;
114         }
115
116         ret = ldb_next_add_record(module, msg2);
117         talloc_free(msg2);
118
119         return ret;
120 }
121
122 /* modify_record: change modifyTimestamp as well */
123 static int objectguid_modify_record(struct ldb_module *module, const struct ldb_message *msg)
124 {
125         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_modify_record\n");
126         return ldb_next_modify_record(module, msg);
127 }
128
129 static int objectguid_delete_record(struct ldb_module *module, const char *dn)
130 {
131         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_delete_record\n");
132         return ldb_next_delete_record(module, dn);
133 }
134
135 static int objectguid_rename_record(struct ldb_module *module, const char *olddn, const char *newdn)
136 {
137         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_rename_record\n");
138         return ldb_next_rename_record(module, olddn, newdn);
139 }
140
141 static int objectguid_lock(struct ldb_module *module, const char *lockname)
142 {
143         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_lock\n");
144         return ldb_next_named_lock(module, lockname);
145 }
146
147 static int objectguid_unlock(struct ldb_module *module, const char *lockname)
148 {
149         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_unlock\n");
150         return ldb_next_named_unlock(module, lockname);
151 }
152
153 /* return extended error information */
154 static const char *objectguid_errstring(struct ldb_module *module)
155 {
156         struct private_data *data = (struct private_data *)module->private_data;
157
158         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_errstring\n");
159         if (data->error_string) {
160                 const char *error;
161
162                 error = data->error_string;
163                 data->error_string = NULL;
164                 return error;
165         }
166
167         return ldb_next_errstring(module);
168 }
169
170 static int objectguid_destructor(void *module_ctx)
171 {
172         /* struct ldb_module *ctx = module_ctx; */
173         /* put your clean-up functions here */
174         return 0;
175 }
176
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
188 };
189
190
191 /* the init function */
192 #ifdef HAVE_DLOPEN_DISABLED
193  struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
194 #else
195 struct ldb_module *objectguid_module_init(struct ldb_context *ldb, const char *options[])
196 #endif
197 {
198         struct ldb_module *ctx;
199         struct private_data *data;
200
201         ctx = talloc(ldb, struct ldb_module);
202         if (!ctx)
203                 return NULL;
204
205         data = talloc(ctx, struct private_data);
206         if (!data) {
207                 talloc_free(ctx);
208                 return NULL;
209         }
210
211         data->error_string = NULL;
212         ctx->private_data = data;
213         ctx->ldb = ldb;
214         ctx->prev = ctx->next = NULL;
215         ctx->ops = &objectguid_ops;
216
217         talloc_set_destructor (ctx, objectguid_destructor);
218
219         return ctx;
220 }