r23798: updated old Temple Place FSF addresses to new URL
[samba.git] / source4 / dsdb / samdb / ldb_modules / objectguid.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004-2006
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6    Copyright (C) Andrew Tridgell 2005
7
8      ** NOTE! The following LGPL license applies to the ldb
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11    
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 3 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 */
25
26 /*
27  *  Name: ldb
28  *
29  *  Component: ldb objectguid module
30  *
31  *  Description: add a unique objectGUID onto every new record
32  *
33  *  Author: Simo Sorce
34  */
35
36 #include "includes.h"
37 #include "ldb/include/ldb_includes.h"
38 #include "librpc/gen_ndr/ndr_misc.h"
39
40 static struct ldb_message_element *objectguid_find_attribute(const struct ldb_message *msg, const char *name)
41 {
42         int i;
43
44         for (i = 0; i < msg->num_elements; i++) {
45                 if (ldb_attr_cmp(name, msg->elements[i].name) == 0) {
46                         return &msg->elements[i];
47                 }
48         }
49
50         return NULL;
51 }
52
53 /*
54   add a time element to a record
55 */
56 static int add_time_element(struct ldb_message *msg, const char *attr, time_t t)
57 {
58         struct ldb_message_element *el;
59         char *s;
60
61         if (ldb_msg_find_element(msg, attr) != NULL) {
62                 return 0;
63         }
64
65         s = ldb_timestring(msg, t);
66         if (s == NULL) {
67                 return -1;
68         }
69
70         if (ldb_msg_add_string(msg, attr, s) != 0) {
71                 return -1;
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 0;
80 }
81
82 /*
83   add a uint64_t element to a record
84 */
85 static int add_uint64_element(struct ldb_message *msg, const char *attr, uint64_t v)
86 {
87         struct ldb_message_element *el;
88
89         if (ldb_msg_find_element(msg, attr) != NULL) {
90                 return 0;
91         }
92
93         if (ldb_msg_add_fmt(msg, attr, "%llu", (unsigned long long)v) != 0) {
94                 return -1;
95         }
96
97         el = ldb_msg_find_element(msg, attr);
98         /* always set as replace. This works because on add ops, the flag
99            is ignored */
100         el->flags = LDB_FLAG_MOD_REPLACE;
101
102         return 0;
103 }
104
105 /* add_record: add objectGUID attribute */
106 static int objectguid_add(struct ldb_module *module, struct ldb_request *req)
107 {
108         struct ldb_request *down_req;
109         struct ldb_message_element *attribute;
110         struct ldb_message *msg;
111         struct ldb_val v;
112         struct GUID guid;
113         uint64_t seq_num;
114         NTSTATUS nt_status;
115         int ret;
116         time_t t = time(NULL);
117
118         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
119
120         /* do not manipulate our control entries */
121         if (ldb_dn_is_special(req->op.add.message->dn)) {
122                 return ldb_next_request(module, req);
123         }
124
125         if ((attribute = objectguid_find_attribute(req->op.add.message, "objectGUID")) != NULL ) {
126                 return ldb_next_request(module, req);
127         }
128
129         down_req = talloc(req, struct ldb_request);
130         if (down_req == NULL) {
131                 return LDB_ERR_OPERATIONS_ERROR;
132         }
133
134         *down_req = *req;
135
136         /* we have to copy the message as the caller might have it as a const */
137         down_req->op.add.message = msg = ldb_msg_copy_shallow(down_req, req->op.add.message);
138         if (msg == NULL) {
139                 talloc_free(down_req);
140                 return LDB_ERR_OPERATIONS_ERROR;
141         }
142
143         /* a new GUID */
144         guid = GUID_random();
145
146         nt_status = ndr_push_struct_blob(&v, msg, &guid, 
147                                          (ndr_push_flags_fn_t)ndr_push_GUID);
148         if (!NT_STATUS_IS_OK(nt_status)) {
149                 talloc_free(down_req);
150                 return LDB_ERR_OPERATIONS_ERROR;
151         }
152
153         ret = ldb_msg_add_value(msg, "objectGUID", &v, NULL);
154         if (ret) {
155                 talloc_free(down_req);
156                 return ret;
157         }
158         
159         if (add_time_element(msg, "whenCreated", t) != 0 ||
160             add_time_element(msg, "whenChanged", t) != 0) {
161                 talloc_free(down_req);
162                 return LDB_ERR_OPERATIONS_ERROR;
163         }
164
165         /* Get a sequence number from the backend */
166         ret = ldb_sequence_number(module->ldb, LDB_SEQ_NEXT, &seq_num);
167         if (ret == LDB_SUCCESS) {
168                 if (add_uint64_element(msg, "uSNCreated", seq_num) != 0 ||
169                     add_uint64_element(msg, "uSNChanged", seq_num) != 0) {
170                         talloc_free(down_req);
171                         return LDB_ERR_OPERATIONS_ERROR;
172                 }
173         }
174
175         ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
176
177         /* go on with the call chain */
178         ret = ldb_next_request(module, down_req);
179
180         /* do not free down_req as the call results may be linked to it,
181          * it will be freed when the upper level request get freed */
182         if (ret == LDB_SUCCESS) {
183                 req->handle = down_req->handle;
184         }
185
186         return ret;
187 }
188
189 /* modify_record: update timestamps */
190 static int objectguid_modify(struct ldb_module *module, struct ldb_request *req)
191 {
192         struct ldb_request *down_req;
193         struct ldb_message *msg;
194         int ret;
195         time_t t = time(NULL);
196         uint64_t seq_num;
197
198         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectguid_add_record\n");
199
200         /* do not manipulate our control entries */
201         if (ldb_dn_is_special(req->op.add.message->dn)) {
202                 return ldb_next_request(module, req);
203         }
204
205         down_req = talloc(req, struct ldb_request);
206         if (down_req == NULL) {
207                 return LDB_ERR_OPERATIONS_ERROR;
208         }
209
210         *down_req = *req;
211
212         /* we have to copy the message as the caller might have it as a const */
213         down_req->op.mod.message = msg = ldb_msg_copy_shallow(down_req, req->op.mod.message);
214         if (msg == NULL) {
215                 talloc_free(down_req);
216                 return LDB_ERR_OPERATIONS_ERROR;
217         }
218
219         if (add_time_element(msg, "whenChanged", t) != 0) {
220                 talloc_free(down_req);
221                 return LDB_ERR_OPERATIONS_ERROR;
222         }
223
224         /* Get a sequence number from the backend */
225         ret = ldb_sequence_number(module->ldb, LDB_SEQ_NEXT, &seq_num);
226         if (ret == LDB_SUCCESS) {
227                 if (add_uint64_element(msg, "uSNChanged", seq_num) != 0) {
228                         talloc_free(down_req);
229                         return LDB_ERR_OPERATIONS_ERROR;
230                 }
231         }
232
233         ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
234
235         /* go on with the call chain */
236         ret = ldb_next_request(module, down_req);
237
238         /* do not free down_req as the call results may be linked to it,
239          * it will be freed when the upper level request get freed */
240         if (ret == LDB_SUCCESS) {
241                 req->handle = down_req->handle;
242         }
243
244         return ret;
245 }
246
247 static const struct ldb_module_ops objectguid_ops = {
248         .name          = "objectguid",
249         .add           = objectguid_add,
250         .modify        = objectguid_modify,
251 };
252
253
254 int objectguid_module_init(void)
255 {
256         return ldb_register_module(&objectguid_ops);
257 }