packaging: Add missing quotes in smbprint
[Samba.git] / source4 / dsdb / samdb / ldb_modules / objectguid.c
blobcfc89184ff671010b6bfb970382fa7961c71a6bd
1 /*
2 ldb database library
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/>.
23 * Name: ldb
25 * Component: ldb objectguid module
27 * Description: add a unique objectGUID onto every new record
29 * Author: Simo Sorce
32 #include "includes.h"
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)
44 char *s;
45 int ret;
47 if (ldb_msg_find_element(msg, attr) != NULL) {
48 return LDB_SUCCESS;
51 s = ldb_timestring(msg, t);
52 if (s == NULL) {
53 return LDB_ERR_OPERATIONS_ERROR;
56 /* always set as replace. This works because on add ops, the flag
57 is ignored */
58 ret = ldb_msg_append_string(msg, attr, s, LDB_FLAG_MOD_REPLACE);
59 if (ret != LDB_SUCCESS) {
60 return ret;
63 return 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)
72 int ret;
74 if (ldb_msg_find_element(msg, attr) != NULL) {
75 return LDB_SUCCESS;
78 /* always set as replace. This works because on add ops, the flag
79 is ignored */
80 ret = samdb_msg_append_uint64(ldb, msg, msg, attr, v, LDB_FLAG_MOD_REPLACE);
81 if (ret != LDB_SUCCESS) {
82 return ret;
85 return LDB_SUCCESS;
88 struct og_context {
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;
100 struct GUID guid;
101 uint64_t seq_num;
102 int ret;
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");
116 if (el != NULL) {
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);
123 if (ac == NULL) {
124 return ldb_oom(ldb);
126 ac->module = module;
127 ac->req = req;
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);
131 if (msg == NULL) {
132 talloc_free(ac);
133 return ldb_operr(ldb);
136 /* a new GUID */
137 guid = GUID_random();
139 ret = dsdb_msg_add_guid(msg, &guid, "objectGUID");
140 if (ret != LDB_SUCCESS) {
141 return ret;
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,
161 msg,
162 req->controls,
163 req, dsdb_next_callback,
164 req);
165 LDB_REQ_SET_LOCATION(down_req);
166 if (ret != LDB_SUCCESS) {
167 return ret;
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;
181 int ret;
182 time_t t = time(NULL);
183 uint64_t seq_num;
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");
196 if (el != NULL) {
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);
203 if (ac == NULL) {
204 return ldb_oom(ldb);
206 ac->module = module;
207 ac->req = req;
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);
211 if (msg == NULL) {
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,
229 msg,
230 req->controls,
231 req, dsdb_next_callback,
232 req);
233 LDB_REQ_SET_LOCATION(down_req);
234 if (ret != LDB_SUCCESS) {
235 return ret;
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);