d7dc88500360738fc5aa08446e11f56ada45134e
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / objectguid.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5    Copyright (C) Andrew Tridgell 2005
6    Copyright (C) Simo Sorce  2004-2008
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /*
23  *  Name: ldb
24  *
25  *  Component: ldb objectguid module
26  *
27  *  Description: add a unique objectGUID onto every new record
28  *
29  *  Author: Simo Sorce
30  */
31
32 #include "includes.h"
33 #include "ldb_module.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "librpc/gen_ndr/ndr_misc.h"
36 #include "param/param.h"
37
38 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
39 {
40         unsigned int i;
41
42         for (i = 0; i < msg->num_elements; i++) {
43                 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
44                         return &msg->elements[i];
45                 }
46         }
47
48         return NULL;
49 }
50
51 /*
52   add a time element to a record
53 */
54 static int add_time_element(struct ldb_message *msg, const char *attr, time_t t)
55 {
56         struct ldb_message_element *el;
57         char *s;
58         int ret;
59
60         if (ldb_msg_find_element(msg, attr) != NULL) {
61                 return LDB_SUCCESS;
62         }
63
64         s = ldb_timestring(msg, t);
65         if (s == NULL) {
66                 return LDB_ERR_OPERATIONS_ERROR;
67         }
68
69         ret = ldb_msg_add_string(msg, attr, s);
70         if (ret != LDB_SUCCESS) {
71                 return ret;
72         }
73
74         el = ldb_msg_find_element(msg, attr);
75         /* always set as replace. This works because on add ops, the flag
76            is ignored */
77         el->flags = LDB_FLAG_MOD_REPLACE;
78
79         return LDB_SUCCESS;
80 }
81
82 /*
83   add a uint64_t element to a record
84 */
85 static int add_uint64_element(struct ldb_context *ldb, struct ldb_message *msg,
86                               const char *attr, uint64_t v)
87 {
88         struct ldb_message_element *el;
89         int ret;
90
91         if (ldb_msg_find_element(msg, attr) != NULL) {
92                 return LDB_SUCCESS;
93         }
94
95         ret = samdb_msg_add_uint64(ldb, msg, msg, attr, v);
96         if (ret != LDB_SUCCESS) {
97                 return ret;
98         }
99
100         el = ldb_msg_find_element(msg, attr);
101         /* always set as replace. This works because on add ops, the flag
102            is ignored */
103         el->flags = LDB_FLAG_MOD_REPLACE;
104
105         return LDB_SUCCESS;
106 }
107
108 struct og_context {
109         struct ldb_module *module;
110         struct ldb_request *req;
111 };
112
113 static int og_op_callback(struct ldb_request *req, struct ldb_reply *ares)
114 {
115         struct og_context *ac;
116
117         ac = talloc_get_type(req->context, struct og_context);
118
119         if (!ares) {
120                 return ldb_module_done(ac->req, NULL, NULL,
121                                         LDB_ERR_OPERATIONS_ERROR);
122         }
123         if (ares->error != LDB_SUCCESS) {
124                 return ldb_module_done(ac->req, ares->controls,
125                                         ares->response, ares->error);
126         }
127
128         if (ares->type != LDB_REPLY_DONE) {
129                 talloc_free(ares);
130                 return ldb_module_done(ac->req, NULL, NULL,
131                                         LDB_ERR_OPERATIONS_ERROR);
132         }
133
134         return ldb_module_done(ac->req, ares->controls,
135                                 ares->response, ares->error);
136 }
137
138 /* add_record: add objectGUID attribute */
139 static int objectguid_add(struct ldb_module *module, struct ldb_request *req)
140 {
141         struct ldb_context *ldb;
142         struct ldb_request *down_req;
143         struct ldb_message_element *attribute;
144         struct ldb_message *msg;
145         struct GUID guid;
146         uint64_t seq_num;
147         int ret;
148         time_t t = time(NULL);
149         struct og_context *ac;
150
151         ldb = ldb_module_get_ctx(module);
152
153         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
154
155         /* do not manipulate our control entries */
156         if (ldb_dn_is_special(req->op.add.message->dn)) {
157                 return ldb_next_request(module, req);
158         }
159
160         if ((attribute = objectguid_find_attribute(req->op.add.message, "objectGUID")) != NULL ) {
161                 return ldb_next_request(module, req);
162         }
163
164         ac = talloc(req, struct og_context);
165         if (ac == NULL) {
166                 return ldb_oom(ldb);
167         }
168         ac->module = module;
169         ac->req = req;
170
171         /* we have to copy the message as the caller might have it as a const */
172         msg = ldb_msg_copy_shallow(ac, req->op.add.message);
173         if (msg == NULL) {
174                 talloc_free(ac);
175                 return ldb_operr(ldb);
176         }
177
178         /* a new GUID */
179         guid = GUID_random();
180
181         ret = dsdb_msg_add_guid(msg, &guid, "objectGUID");
182         if (ret != LDB_SUCCESS) {
183                 return ret;
184         }
185         
186         if (add_time_element(msg, "whenCreated", t) != LDB_SUCCESS ||
187             add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
188                 return ldb_operr(ldb);
189         }
190
191         /* Get a sequence number from the backend */
192         /* FIXME: ldb_sequence_number is a semi-async call,
193          * make sure this function is split and a callback is used */
194         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
195         if (ret == LDB_SUCCESS) {
196                 if (add_uint64_element(ldb, msg, "uSNCreated",
197                                        seq_num) != LDB_SUCCESS ||
198                     add_uint64_element(ldb, msg, "uSNChanged",
199                                        seq_num) != LDB_SUCCESS) {
200                         return ldb_operr(ldb);
201                 }
202         }
203
204         ret = ldb_build_add_req(&down_req, ldb, ac,
205                                 msg,
206                                 req->controls,
207                                 ac, og_op_callback,
208                                 req);
209         LDB_REQ_SET_LOCATION(down_req);
210         if (ret != LDB_SUCCESS) {
211                 return ret;
212         }
213
214         /* go on with the call chain */
215         return ldb_next_request(module, down_req);
216 }
217
218 /* modify_record: update timestamps */
219 static int objectguid_modify(struct ldb_module *module, struct ldb_request *req)
220 {
221         struct ldb_context *ldb;
222         struct ldb_request *down_req;
223         struct ldb_message *msg;
224         int ret;
225         time_t t = time(NULL);
226         uint64_t seq_num;
227         struct og_context *ac;
228
229         ldb = ldb_module_get_ctx(module);
230
231         ldb_debug(ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
232
233         /* do not manipulate our control entries */
234         if (ldb_dn_is_special(req->op.add.message->dn)) {
235                 return ldb_next_request(module, req);
236         }
237
238         ac = talloc(req, struct og_context);
239         if (ac == NULL) {
240                 return ldb_oom(ldb);
241         }
242         ac->module = module;
243         ac->req = req;
244
245         /* we have to copy the message as the caller might have it as a const */
246         msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
247         if (msg == NULL) {
248                 return ldb_operr(ldb);
249         }
250
251         if (add_time_element(msg, "whenChanged", t) != LDB_SUCCESS) {
252                 return ldb_operr(ldb);
253         }
254
255         /* Get a sequence number from the backend */
256         ret = ldb_sequence_number(ldb, LDB_SEQ_NEXT, &seq_num);
257         if (ret == LDB_SUCCESS) {
258                 if (add_uint64_element(ldb, msg, "uSNChanged",
259                                        seq_num) != LDB_SUCCESS) {
260                         return ldb_operr(ldb);
261                 }
262         }
263
264         ret = ldb_build_mod_req(&down_req, ldb, ac,
265                                 msg,
266                                 req->controls,
267                                 ac, og_op_callback,
268                                 req);
269         LDB_REQ_SET_LOCATION(down_req);
270         if (ret != LDB_SUCCESS) {
271                 return ret;
272         }
273
274         /* go on with the call chain */
275         return ldb_next_request(module, down_req);
276 }
277
278 _PUBLIC_ const struct ldb_module_ops ldb_objectguid_module_ops = {
279         .name          = "objectguid",
280         .add           = objectguid_add,
281         .modify        = objectguid_modify,
282 };