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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb extended dn control module
30 * Description: this module builds a special dn
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_errors.h"
38 #include "ldb/include/ldb_private.h"
39 #include "dsdb/samdb/samdb.h"
40 #include "librpc/gen_ndr/ndr_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,
103 const struct ldb_val *val;
110 dn = ldb_dn_linearize(msg, msg->dn);
114 /* retrieve object_guid */
115 guid = samdb_result_guid(msg, "objectGUID");
116 object_guid = GUID_string(msg, &guid);
121 ldb_msg_remove_attr(msg, "objectGUID");
123 /* retrieve object_sid */
125 sid = samdb_result_dom_sid(msg, msg, "objectSID");
127 object_sid = dom_sid_string(msg, sid);
132 ldb_msg_remove_attr(msg, "objectSID");
135 /* TODO: handle type */
140 new_dn = talloc_asprintf(msg, "<GUID=%s>;<SID=%s>;%s",
141 object_guid, object_sid, dn);
143 new_dn = talloc_asprintf(msg, "<GUID=%s>;%s",
154 msg->dn = ldb_dn_explode_or_special(msg, new_dn);
158 val = ldb_msg_find_ldb_val(msg, "distinguishedName");
160 ldb_msg_remove_attr(msg, "distinguishedName");
161 if (ldb_msg_add_steal_string(msg, "distinguishedName", new_dn))
169 static int extended_search(struct ldb_module *module, struct ldb_control *control, struct ldb_request *req)
171 struct ldb_result *extended_result;
172 struct ldb_control **saved_controls;
173 struct ldb_extended_dn_control *extended_ctrl;
175 const char * const *saved_attrs = NULL;
177 BOOL remove_guid = False;
178 BOOL remove_sid = False;
180 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
181 if (!extended_ctrl) {
182 return LDB_ERR_PROTOCOL_ERROR;
185 /* save it locally and remove it from the list */
186 if (!save_controls(control, req, &saved_controls)) {
187 return LDB_ERR_OPERATIONS_ERROR;
190 /* check if attrs only is specified, in that case check wether we need to modify them */
191 if (req->op.search.attrs) {
192 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
195 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
198 if (remove_guid || remove_sid) {
199 new_attrs = copy_attrs(req, req->op.search.attrs);
201 return LDB_ERR_OPERATIONS_ERROR;
203 saved_attrs = req->op.search.attrs;
206 if (!add_attrs(req, &new_attrs, "objectGUID"))
207 return LDB_ERR_OPERATIONS_ERROR;
210 if (!add_attrs(req, &new_attrs, "objectSID"))
211 return LDB_ERR_OPERATIONS_ERROR;
214 req->op.search.attrs = (const char * const *)new_attrs;
218 ret = ldb_next_request(module, req);
220 /* put request back into original shape */
221 /* TODO: build a new req and don't touch the original one */
223 if (req->controls) talloc_free(req->controls);
224 req->controls = saved_controls;
227 talloc_free(new_attrs);
228 req->op.search.attrs = saved_attrs;
231 if (ret != LDB_SUCCESS) {
235 extended_result = req->op.search.res;
237 for (i = 0; i < extended_result->count; i++) {
238 /* TODO: the following funtion updates only dn and
239 * distinguishedName. We still need to address other
240 * DN entries like objectCategory
242 if (!inject_extended_dn(extended_result->msgs[i],
244 remove_guid, remove_sid)) {
245 return LDB_ERR_OPERATIONS_ERROR;
253 struct extended_async_context {
255 struct ldb_module *module;
257 int (*up_callback)(struct ldb_context *, void *, struct ldb_async_result *);
260 const char * const *attrs;
266 static int extended_async_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
268 struct extended_async_context *ac;
270 if (!context || !ares) {
271 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
275 ac = talloc_get_type(context, struct extended_async_context);
277 if (ares->type == LDB_REPLY_ENTRY) {
278 /* for each record returned post-process to add any derived
279 attributes that have been asked for */
280 if (!inject_extended_dn(ares->message, ac->extended_type, ac->remove_guid, ac->remove_sid)) {
285 return ac->up_callback(ldb, ac->up_context, ares);
289 return LDB_ERR_OPERATIONS_ERROR;
292 static int extended_search_async(struct ldb_module *module, struct ldb_control *control, struct ldb_request *req)
294 struct ldb_extended_dn_control *extended_ctrl;
295 struct ldb_control **saved_controls;
296 struct extended_async_context *ac;
297 struct ldb_request *down_req;
301 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
302 if (!extended_ctrl) {
303 return LDB_ERR_PROTOCOL_ERROR;
306 ac = talloc(req, struct extended_async_context);
308 return LDB_ERR_OPERATIONS_ERROR;
312 ac->up_context = req->async.context;
313 ac->up_callback = req->async.callback;
314 ac->timeout = req->async.timeout;
315 ac->attrs = req->op.search.attrs;
316 ac->remove_guid = False;
317 ac->remove_sid = False;
318 ac->extended_type = extended_ctrl->type;
320 down_req = talloc_zero(req, struct ldb_request);
321 if (down_req == NULL) {
322 return LDB_ERR_OPERATIONS_ERROR;
325 down_req->operation = req->operation;
326 down_req->op.search.base = req->op.search.base;
327 down_req->op.search.scope = req->op.search.scope;
328 down_req->op.search.tree = req->op.search.tree;
330 /* check if attrs only is specified, in that case check wether we need to modify them */
331 if (req->op.search.attrs) {
332 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
333 ac->remove_guid = True;
335 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
336 ac->remove_sid = True;
338 if (ac->remove_guid || ac->remove_sid) {
339 new_attrs = copy_attrs(down_req, req->op.search.attrs);
340 if (new_attrs == NULL)
341 return LDB_ERR_OPERATIONS_ERROR;
343 if (ac->remove_guid) {
344 if (!add_attrs(down_req, &new_attrs, "objectGUID"))
345 return LDB_ERR_OPERATIONS_ERROR;
347 if (ac->remove_sid) {
348 if (!add_attrs(down_req, &new_attrs, "objectSID"))
349 return LDB_ERR_OPERATIONS_ERROR;
352 down_req->op.search.attrs = (const char * const *)new_attrs;
356 down_req->controls = req->controls;
358 /* save it locally and remove it from the list */
359 /* we do not need to replace them later as we
360 * are keeping the original req intact */
361 if (!save_controls(control, down_req, &saved_controls)) {
362 return LDB_ERR_OPERATIONS_ERROR;
365 down_req->creds = req->creds;
367 down_req->async.context = ac;
368 down_req->async.callback = extended_async_callback;
369 down_req->async.timeout = req->async.timeout;
371 /* perform the search */
372 ret = ldb_next_request(module, down_req);
374 /* do not free down_req as the call results may be linked to it,
375 * it will be freed when the upper level request get freed */
376 if (ret == LDB_SUCCESS) {
377 req->async.handle = down_req->async.handle;
383 static int extended_request(struct ldb_module *module, struct ldb_request *req)
385 struct ldb_control *control;
387 /* check if there's an extended dn control */
388 control = get_control_from_list(req->controls, LDB_CONTROL_EXTENDED_DN_OID);
389 if (control == NULL) {
390 /* not found go on */
391 return ldb_next_request(module, req);
394 switch (req->operation) {
397 return extended_search(module, control, req);
399 case LDB_ASYNC_SEARCH:
400 return extended_search_async(module, control, req);
403 return LDB_ERR_OPERATIONS_ERROR;
408 static int extended_init(struct ldb_module *module)
410 struct ldb_request *req;
413 req = talloc(module, struct ldb_request);
415 return LDB_ERR_OPERATIONS_ERROR;
418 req->operation = LDB_REQ_REGISTER;
419 req->op.reg.oid = LDB_CONTROL_EXTENDED_DN_OID;
420 req->controls = NULL;
422 ret = ldb_request(module->ldb, req);
423 if (ret != LDB_SUCCESS) {
424 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "extended_dn: Unable to register control with rootdse!\n");
426 return LDB_ERR_OPERATIONS_ERROR;
430 return ldb_next_init(module);
433 static const struct ldb_module_ops extended_dn_ops = {
434 .name = "extended_dn",
435 .request = extended_request,
436 .init_context = extended_init
439 int ldb_extended_dn_init(void)
441 return ldb_register_module(&extended_dn_ops);