4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Andrew Tridgell 2005
6 Copyright (C) Simo Sorce 2004-2008
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.
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.
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/>.
25 * Component: ldb objectguid module
27 * Description: add a unique objectGUID onto every new record
33 #include "ldb_module.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "dsdb/samdb/ldb_modules/util.h"
36 #include "librpc/gen_ndr/ndr_misc.h"
37 #include "param/param.h"
40 add a time element to a record
42 static int add_time_element(struct ldb_message
*msg
, const char *attr
, time_t t
)
47 if (ldb_msg_find_element(msg
, attr
) != NULL
) {
51 s
= ldb_timestring(msg
, t
);
53 return LDB_ERR_OPERATIONS_ERROR
;
56 /* always set as replace. This works because on add ops, the flag
58 ret
= ldb_msg_append_string(msg
, attr
, s
, LDB_FLAG_MOD_REPLACE
);
59 if (ret
!= LDB_SUCCESS
) {
67 add a uint64_t element to a record
69 static int add_uint64_element(struct ldb_context
*ldb
, struct ldb_message
*msg
,
70 const char *attr
, uint64_t v
)
74 if (ldb_msg_find_element(msg
, attr
) != NULL
) {
78 /* always set as replace. This works because on add ops, the flag
80 ret
= samdb_msg_append_uint64(ldb
, msg
, msg
, attr
, v
, LDB_FLAG_MOD_REPLACE
);
81 if (ret
!= LDB_SUCCESS
) {
89 struct ldb_module
*module
;
90 struct ldb_request
*req
;
93 /* add_record: add objectGUID and timestamp attributes */
94 static int objectguid_add(struct ldb_module
*module
, struct ldb_request
*req
)
96 struct ldb_context
*ldb
;
97 struct ldb_request
*down_req
;
98 struct ldb_message
*msg
;
99 struct ldb_message_element
*el
;
103 time_t t
= time(NULL
);
104 struct og_context
*ac
;
106 ldb
= ldb_module_get_ctx(module
);
108 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "objectguid_add_record\n");
110 /* do not manipulate our control entries */
111 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
112 return ldb_next_request(module
, req
);
115 el
= ldb_msg_find_element(req
->op
.add
.message
, "objectGUID");
117 ldb_set_errstring(ldb
,
118 "objectguid: objectGUID must not be specified!");
119 return LDB_ERR_UNWILLING_TO_PERFORM
;
122 ac
= talloc(req
, struct og_context
);
129 /* we have to copy the message as the caller might have it as a const */
130 msg
= ldb_msg_copy_shallow(ac
, req
->op
.add
.message
);
133 return ldb_operr(ldb
);
137 guid
= GUID_random();
139 ret
= dsdb_msg_add_guid(msg
, &guid
, "objectGUID");
140 if (ret
!= LDB_SUCCESS
) {
144 if (add_time_element(msg
, "whenCreated", t
) != LDB_SUCCESS
||
145 add_time_element(msg
, "whenChanged", t
) != LDB_SUCCESS
) {
146 return ldb_operr(ldb
);
149 /* Get a sequence number from the backend */
150 ret
= ldb_sequence_number(ldb
, LDB_SEQ_NEXT
, &seq_num
);
151 if (ret
== LDB_SUCCESS
) {
152 if (add_uint64_element(ldb
, msg
, "uSNCreated",
153 seq_num
) != LDB_SUCCESS
||
154 add_uint64_element(ldb
, msg
, "uSNChanged",
155 seq_num
) != LDB_SUCCESS
) {
156 return ldb_operr(ldb
);
160 ret
= ldb_build_add_req(&down_req
, ldb
, ac
,
163 req
, dsdb_next_callback
,
165 LDB_REQ_SET_LOCATION(down_req
);
166 if (ret
!= LDB_SUCCESS
) {
170 /* go on with the call chain */
171 return ldb_next_request(module
, down_req
);
174 /* modify_record: update timestamps */
175 static int objectguid_modify(struct ldb_module
*module
, struct ldb_request
*req
)
177 struct ldb_context
*ldb
;
178 struct ldb_request
*down_req
;
179 struct ldb_message
*msg
;
180 struct ldb_message_element
*el
;
182 time_t t
= time(NULL
);
184 struct og_context
*ac
;
186 ldb
= ldb_module_get_ctx(module
);
188 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "objectguid_modify_record\n");
190 /* do not manipulate our control entries */
191 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
192 return ldb_next_request(module
, req
);
195 el
= ldb_msg_find_element(req
->op
.mod
.message
, "objectGUID");
197 ldb_set_errstring(ldb
,
198 "objectguid: objectGUID must not be specified!");
199 return LDB_ERR_CONSTRAINT_VIOLATION
;
202 ac
= talloc(req
, struct og_context
);
209 /* we have to copy the message as the caller might have it as a const */
210 msg
= ldb_msg_copy_shallow(ac
, req
->op
.mod
.message
);
212 return ldb_operr(ldb
);
215 if (add_time_element(msg
, "whenChanged", t
) != LDB_SUCCESS
) {
216 return ldb_operr(ldb
);
219 /* Get a sequence number from the backend */
220 ret
= ldb_sequence_number(ldb
, LDB_SEQ_NEXT
, &seq_num
);
221 if (ret
== LDB_SUCCESS
) {
222 if (add_uint64_element(ldb
, msg
, "uSNChanged",
223 seq_num
) != LDB_SUCCESS
) {
224 return ldb_operr(ldb
);
228 ret
= ldb_build_mod_req(&down_req
, ldb
, ac
,
231 req
, dsdb_next_callback
,
233 LDB_REQ_SET_LOCATION(down_req
);
234 if (ret
!= LDB_SUCCESS
) {
238 /* go on with the call chain */
239 return ldb_next_request(module
, down_req
);
242 static const struct ldb_module_ops ldb_objectguid_module_ops
= {
243 .name
= "objectguid",
244 .add
= objectguid_add
,
245 .modify
= objectguid_modify
248 int ldb_objectguid_module_init(const char *version
)
250 LDB_MODULE_CHECK_VERSION(version
);
251 return ldb_register_module(&ldb_objectguid_module_ops
);