4 Copyright (C) Simo Sorce 2005
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
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 3 of the License, or (at your option) any later version.
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.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 * Component: ldb extended dn control module
29 * Description: this module builds a special dn
35 #include "ldb/include/ldb.h"
36 #include "ldb/include/ldb_errors.h"
37 #include "ldb/include/ldb_private.h"
38 #include "librpc/gen_ndr/ndr_misc.h"
39 #include "dsdb/samdb/samdb.h"
40 #include "libcli/security/security.h"
44 static bool is_attr_in_list(const char * const * attrs, const char *attr)
48 for (i = 0; attrs[i]; i++) {
49 if (strcasecmp(attrs[i], attr) == 0)
56 static char **copy_attrs(void *mem_ctx, const char * const * attrs)
61 for (num = 0; attrs[num]; num++);
63 new = talloc_array(mem_ctx, char *, num + 1);
64 if (!new) return NULL;
66 for(i = 0; i < num; i++) {
67 new[i] = talloc_strdup(new, attrs[i]);
78 static bool add_attrs(void *mem_ctx, char ***attrs, const char *attr)
83 for (num = 0; (*attrs)[num]; num++);
85 new = talloc_realloc(mem_ctx, *attrs, char *, num + 2);
86 if (!new) return false;
90 new[num] = talloc_strdup(new, attr);
91 if (!new[num]) return false;
98 static bool inject_extended_dn(struct ldb_message *msg,
99 struct ldb_context *ldb,
104 const struct ldb_val *val;
107 const DATA_BLOB *guid_blob;
108 const DATA_BLOB *sid_blob;
113 guid_blob = ldb_msg_find_ldb_val(msg, "objectGUID");
114 sid_blob = ldb_msg_find_ldb_val(msg, "objectSID");
121 /* return things in hexadecimal format */
123 const char *lower_guid_hex = strlower_talloc(msg, data_blob_hex_string(msg, guid_blob));
124 const char *lower_sid_hex = strlower_talloc(msg, data_blob_hex_string(msg, sid_blob));
125 if (!lower_guid_hex || !lower_sid_hex) {
128 new_dn = talloc_asprintf(msg, "<GUID=%s>;<SID=%s>;%s",
131 ldb_dn_get_linearized(msg->dn));
133 const char *lower_guid_hex = strlower_talloc(msg, data_blob_hex_string(msg, guid_blob));
134 if (!lower_guid_hex) {
137 new_dn = talloc_asprintf(msg, "<GUID=%s>;%s",
139 ldb_dn_get_linearized(msg->dn));
144 /* retrieve object_guid */
145 guid = samdb_result_guid(msg, "objectGUID");
146 object_guid = GUID_string(msg, &guid);
148 /* retrieve object_sid */
150 sid = samdb_result_dom_sid(msg, msg, "objectSID");
152 object_sid = dom_sid_string(msg, sid);
158 /* Normal, sane format */
160 new_dn = talloc_asprintf(msg, "<GUID=%s>;<SID=%s>;%s",
161 object_guid, object_sid,
162 ldb_dn_get_linearized(msg->dn));
164 new_dn = talloc_asprintf(msg, "<GUID=%s>;%s",
166 ldb_dn_get_linearized(msg->dn));
178 ldb_msg_remove_attr(msg, "objectGUID");
181 if (sid_blob && remove_sid) {
182 ldb_msg_remove_attr(msg, "objectSID");
185 msg->dn = ldb_dn_new(msg, ldb, new_dn);
186 if (! ldb_dn_validate(msg->dn))
189 val = ldb_msg_find_ldb_val(msg, "distinguishedName");
191 ldb_msg_remove_attr(msg, "distinguishedName");
192 if (ldb_msg_add_steal_string(msg, "distinguishedName", new_dn))
200 struct extended_context {
202 struct ldb_module *module;
204 int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
206 const char * const *attrs;
212 static int extended_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
214 struct extended_context *ac;
216 ac = talloc_get_type(context, struct extended_context);
218 if (ares->type == LDB_REPLY_ENTRY) {
219 /* for each record returned post-process to add any derived
220 attributes that have been asked for */
221 if (!inject_extended_dn(ares->message, ldb, ac->extended_type, ac->remove_guid, ac->remove_sid)) {
226 return ac->up_callback(ldb, ac->up_context, ares);
230 return LDB_ERR_OPERATIONS_ERROR;
233 static int extended_search(struct ldb_module *module, struct ldb_request *req)
235 struct ldb_control *control;
236 struct ldb_extended_dn_control *extended_ctrl = NULL;
237 struct ldb_control **saved_controls;
238 struct extended_context *ac;
239 struct ldb_request *down_req;
243 /* check if there's an extended dn control */
244 control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
245 if (control == NULL) {
246 /* not found go on */
247 return ldb_next_request(module, req);
251 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
252 if (!extended_ctrl) {
253 return LDB_ERR_PROTOCOL_ERROR;
257 ac = talloc(req, struct extended_context);
259 return LDB_ERR_OPERATIONS_ERROR;
263 ac->up_context = req->context;
264 ac->up_callback = req->callback;
265 ac->attrs = req->op.search.attrs;
266 ac->remove_guid = false;
267 ac->remove_sid = false;
269 ac->extended_type = extended_ctrl->type;
271 ac->extended_type = 0;
274 down_req = talloc_zero(req, struct ldb_request);
275 if (down_req == NULL) {
276 return LDB_ERR_OPERATIONS_ERROR;
279 down_req->operation = req->operation;
280 down_req->op.search.base = req->op.search.base;
281 down_req->op.search.scope = req->op.search.scope;
282 down_req->op.search.tree = req->op.search.tree;
284 /* check if attrs only is specified, in that case check wether we need to modify them */
285 if (req->op.search.attrs) {
286 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
287 ac->remove_guid = true;
289 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
290 ac->remove_sid = true;
292 if (ac->remove_guid || ac->remove_sid) {
293 new_attrs = copy_attrs(down_req, req->op.search.attrs);
294 if (new_attrs == NULL)
295 return LDB_ERR_OPERATIONS_ERROR;
297 if (ac->remove_guid) {
298 if (!add_attrs(down_req, &new_attrs, "objectGUID"))
299 return LDB_ERR_OPERATIONS_ERROR;
301 if (ac->remove_sid) {
302 if (!add_attrs(down_req, &new_attrs, "objectSID"))
303 return LDB_ERR_OPERATIONS_ERROR;
306 down_req->op.search.attrs = (const char * const *)new_attrs;
310 down_req->controls = req->controls;
312 /* save it locally and remove it from the list */
313 /* we do not need to replace them later as we
314 * are keeping the original req intact */
315 if (!save_controls(control, down_req, &saved_controls)) {
316 return LDB_ERR_OPERATIONS_ERROR;
319 down_req->context = ac;
320 down_req->callback = extended_callback;
321 ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
323 /* perform the search */
324 ret = ldb_next_request(module, down_req);
326 /* do not free down_req as the call results may be linked to it,
327 * it will be freed when the upper level request get freed */
328 if (ret == LDB_SUCCESS) {
329 req->handle = down_req->handle;
335 static int extended_init(struct ldb_module *module)
337 struct ldb_request *req;
340 req = talloc(module, struct ldb_request);
342 return LDB_ERR_OPERATIONS_ERROR;
345 req->operation = LDB_REQ_REGISTER_CONTROL;
346 req->op.reg_control.oid = LDB_CONTROL_EXTENDED_DN_OID;
347 req->controls = NULL;
349 ret = ldb_request(module->ldb, req);
350 if (ret != LDB_SUCCESS) {
351 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "extended_dn: Unable to register control with rootdse!\n");
353 return LDB_ERR_OPERATIONS_ERROR;
357 return ldb_next_init(module);
360 static const struct ldb_module_ops extended_dn_ops = {
361 .name = "extended_dn",
362 .search = extended_search,
363 .init_context = extended_init
366 int ldb_extended_dn_init(void)
368 return ldb_register_module(&extended_dn_ops);