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 "librpc/gen_ndr/ndr_misc.h"
40 #include "dsdb/samdb/samdb.h"
41 #include "libcli/security/security.h"
45 static BOOL is_attr_in_list(const char * const * attrs, const char *attr)
49 for (i = 0; attrs[i]; i++) {
50 if (strcasecmp(attrs[i], attr) == 0)
57 static char **copy_attrs(void *mem_ctx, const char * const * attrs)
62 for (num = 0; attrs[num]; num++);
64 new = talloc_array(mem_ctx, char *, num + 1);
65 if (!new) return NULL;
67 for(i = 0; i < num; i++) {
68 new[i] = talloc_strdup(new, attrs[i]);
79 static BOOL add_attrs(void *mem_ctx, char ***attrs, const char *attr)
84 for (num = 0; (*attrs)[num]; num++);
86 new = talloc_realloc(mem_ctx, *attrs, char *, num + 2);
87 if (!new) return False;
91 new[num] = talloc_strdup(new, attr);
92 if (!new[num]) return False;
99 static BOOL inject_extended_dn(struct ldb_message *msg,
104 const struct ldb_val *val;
111 dn = ldb_dn_linearize(msg, msg->dn);
115 /* retrieve object_guid */
116 guid = samdb_result_guid(msg, "objectGUID");
117 object_guid = GUID_string(msg, &guid);
122 ldb_msg_remove_attr(msg, "objectGUID");
124 /* retrieve object_sid */
126 sid = samdb_result_dom_sid(msg, msg, "objectSID");
128 object_sid = dom_sid_string(msg, sid);
133 ldb_msg_remove_attr(msg, "objectSID");
136 /* TODO: handle type */
141 new_dn = talloc_asprintf(msg, "<GUID=%s>;<SID=%s>;%s",
142 object_guid, object_sid, dn);
144 new_dn = talloc_asprintf(msg, "<GUID=%s>;%s",
155 msg->dn = ldb_dn_explode_or_special(msg, new_dn);
159 val = ldb_msg_find_ldb_val(msg, "distinguishedName");
161 ldb_msg_remove_attr(msg, "distinguishedName");
162 if (ldb_msg_add_steal_string(msg, "distinguishedName", new_dn))
170 struct extended_async_context {
172 struct ldb_module *module;
174 int (*up_callback)(struct ldb_context *, void *, struct ldb_async_result *);
176 const char * const *attrs;
182 static int extended_async_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
184 struct extended_async_context *ac;
186 if (!context || !ares) {
187 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
191 ac = talloc_get_type(context, struct extended_async_context);
193 if (ares->type == LDB_REPLY_ENTRY) {
194 /* for each record returned post-process to add any derived
195 attributes that have been asked for */
196 if (!inject_extended_dn(ares->message, ac->extended_type, ac->remove_guid, ac->remove_sid)) {
201 return ac->up_callback(ldb, ac->up_context, ares);
205 return LDB_ERR_OPERATIONS_ERROR;
208 static int extended_search_async(struct ldb_module *module, struct ldb_request *req)
210 struct ldb_control *control;
211 struct ldb_extended_dn_control *extended_ctrl;
212 struct ldb_control **saved_controls;
213 struct extended_async_context *ac;
214 struct ldb_request *down_req;
218 /* check if there's an extended dn control */
219 control = get_control_from_list(req->controls, LDB_CONTROL_EXTENDED_DN_OID);
220 if (control == NULL) {
221 /* not found go on */
222 return ldb_next_request(module, req);
225 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
226 if (!extended_ctrl) {
227 return LDB_ERR_PROTOCOL_ERROR;
230 ac = talloc(req, struct extended_async_context);
232 return LDB_ERR_OPERATIONS_ERROR;
236 ac->up_context = req->async.context;
237 ac->up_callback = req->async.callback;
238 ac->attrs = req->op.search.attrs;
239 ac->remove_guid = False;
240 ac->remove_sid = False;
241 ac->extended_type = extended_ctrl->type;
243 down_req = talloc_zero(req, struct ldb_request);
244 if (down_req == NULL) {
245 return LDB_ERR_OPERATIONS_ERROR;
248 down_req->operation = req->operation;
249 down_req->op.search.base = req->op.search.base;
250 down_req->op.search.scope = req->op.search.scope;
251 down_req->op.search.tree = req->op.search.tree;
253 /* check if attrs only is specified, in that case check wether we need to modify them */
254 if (req->op.search.attrs) {
255 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
256 ac->remove_guid = True;
258 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
259 ac->remove_sid = True;
261 if (ac->remove_guid || ac->remove_sid) {
262 new_attrs = copy_attrs(down_req, req->op.search.attrs);
263 if (new_attrs == NULL)
264 return LDB_ERR_OPERATIONS_ERROR;
266 if (ac->remove_guid) {
267 if (!add_attrs(down_req, &new_attrs, "objectGUID"))
268 return LDB_ERR_OPERATIONS_ERROR;
270 if (ac->remove_sid) {
271 if (!add_attrs(down_req, &new_attrs, "objectSID"))
272 return LDB_ERR_OPERATIONS_ERROR;
275 down_req->op.search.attrs = (const char * const *)new_attrs;
279 down_req->controls = req->controls;
281 /* save it locally and remove it from the list */
282 /* we do not need to replace them later as we
283 * are keeping the original req intact */
284 if (!save_controls(control, down_req, &saved_controls)) {
285 return LDB_ERR_OPERATIONS_ERROR;
288 down_req->async.context = ac;
289 down_req->async.callback = extended_async_callback;
290 ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
292 /* perform the search */
293 ret = ldb_next_request(module, down_req);
295 /* do not free down_req as the call results may be linked to it,
296 * it will be freed when the upper level request get freed */
297 if (ret == LDB_SUCCESS) {
298 req->async.handle = down_req->async.handle;
304 static int extended_init(struct ldb_module *module)
306 struct ldb_request *req;
309 req = talloc(module, struct ldb_request);
311 return LDB_ERR_OPERATIONS_ERROR;
314 req->operation = LDB_REQ_REGISTER;
315 req->op.reg.oid = LDB_CONTROL_EXTENDED_DN_OID;
316 req->controls = NULL;
318 ret = ldb_request(module->ldb, req);
319 if (ret != LDB_SUCCESS) {
320 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "extended_dn: Unable to register control with rootdse!\n");
322 return LDB_ERR_OPERATIONS_ERROR;
326 return ldb_next_init(module);
329 static const struct ldb_module_ops extended_dn_ops = {
330 .name = "extended_dn",
331 .search = extended_search_async,
332 .init_context = extended_init
335 int ldb_extended_dn_init(void)
337 return ldb_register_module(&extended_dn_ops);