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;
111 /* retrieve object_guid */
112 guid = samdb_result_guid(msg, "objectGUID");
113 object_guid = GUID_string(msg, &guid);
118 ldb_msg_remove_attr(msg, "objectGUID");
120 /* retrieve object_sid */
122 sid = samdb_result_dom_sid(msg, msg, "objectSID");
124 object_sid = dom_sid_string(msg, sid);
129 ldb_msg_remove_attr(msg, "objectSID");
132 /* TODO: handle type */
137 new_dn = talloc_asprintf(msg, "<GUID=%s>;<SID=%s>;%s",
138 object_guid, object_sid,
139 ldb_dn_get_linearized(msg->dn));
141 new_dn = talloc_asprintf(msg, "<GUID=%s>;%s",
143 ldb_dn_get_linearized(msg->dn));
153 msg->dn = ldb_dn_new(msg, ldb, new_dn);
154 if (! ldb_dn_validate(msg->dn))
157 val = ldb_msg_find_ldb_val(msg, "distinguishedName");
159 ldb_msg_remove_attr(msg, "distinguishedName");
160 if (ldb_msg_add_steal_string(msg, "distinguishedName", new_dn))
168 struct extended_context {
170 struct ldb_module *module;
172 int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
174 const char * const *attrs;
180 static int extended_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
182 struct extended_context *ac;
184 if (!context || !ares) {
185 ldb_set_errstring(ldb, "NULL Context or Result in callback");
189 ac = talloc_get_type(context, struct extended_context);
191 if (ares->type == LDB_REPLY_ENTRY) {
192 /* for each record returned post-process to add any derived
193 attributes that have been asked for */
194 if (!inject_extended_dn(ares->message, ldb, ac->extended_type, ac->remove_guid, ac->remove_sid)) {
199 return ac->up_callback(ldb, ac->up_context, ares);
203 return LDB_ERR_OPERATIONS_ERROR;
206 static int extended_search(struct ldb_module *module, struct ldb_request *req)
208 struct ldb_control *control;
209 struct ldb_extended_dn_control *extended_ctrl;
210 struct ldb_control **saved_controls;
211 struct extended_context *ac;
212 struct ldb_request *down_req;
216 /* check if there's an extended dn control */
217 control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
218 if (control == NULL) {
219 /* not found go on */
220 return ldb_next_request(module, req);
223 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
224 if (!extended_ctrl) {
225 return LDB_ERR_PROTOCOL_ERROR;
228 ac = talloc(req, struct extended_context);
230 return LDB_ERR_OPERATIONS_ERROR;
234 ac->up_context = req->context;
235 ac->up_callback = req->callback;
236 ac->attrs = req->op.search.attrs;
237 ac->remove_guid = False;
238 ac->remove_sid = False;
239 ac->extended_type = extended_ctrl->type;
241 down_req = talloc_zero(req, struct ldb_request);
242 if (down_req == NULL) {
243 return LDB_ERR_OPERATIONS_ERROR;
246 down_req->operation = req->operation;
247 down_req->op.search.base = req->op.search.base;
248 down_req->op.search.scope = req->op.search.scope;
249 down_req->op.search.tree = req->op.search.tree;
251 /* check if attrs only is specified, in that case check wether we need to modify them */
252 if (req->op.search.attrs) {
253 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
254 ac->remove_guid = True;
256 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
257 ac->remove_sid = True;
259 if (ac->remove_guid || ac->remove_sid) {
260 new_attrs = copy_attrs(down_req, req->op.search.attrs);
261 if (new_attrs == NULL)
262 return LDB_ERR_OPERATIONS_ERROR;
264 if (ac->remove_guid) {
265 if (!add_attrs(down_req, &new_attrs, "objectGUID"))
266 return LDB_ERR_OPERATIONS_ERROR;
268 if (ac->remove_sid) {
269 if (!add_attrs(down_req, &new_attrs, "objectSID"))
270 return LDB_ERR_OPERATIONS_ERROR;
273 down_req->op.search.attrs = (const char * const *)new_attrs;
277 down_req->controls = req->controls;
279 /* save it locally and remove it from the list */
280 /* we do not need to replace them later as we
281 * are keeping the original req intact */
282 if (!save_controls(control, down_req, &saved_controls)) {
283 return LDB_ERR_OPERATIONS_ERROR;
286 down_req->context = ac;
287 down_req->callback = extended_callback;
288 ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
290 /* perform the search */
291 ret = ldb_next_request(module, down_req);
293 /* do not free down_req as the call results may be linked to it,
294 * it will be freed when the upper level request get freed */
295 if (ret == LDB_SUCCESS) {
296 req->handle = down_req->handle;
302 static int extended_init(struct ldb_module *module)
304 struct ldb_request *req;
307 req = talloc(module, struct ldb_request);
309 return LDB_ERR_OPERATIONS_ERROR;
312 req->operation = LDB_REQ_REGISTER_CONTROL;
313 req->op.reg_control.oid = LDB_CONTROL_EXTENDED_DN_OID;
314 req->controls = NULL;
316 ret = ldb_request(module->ldb, req);
317 if (ret != LDB_SUCCESS) {
318 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "extended_dn: Unable to register control with rootdse!\n");
320 return LDB_ERR_OPERATIONS_ERROR;
324 return ldb_next_init(module);
327 static const struct ldb_module_ops extended_dn_ops = {
328 .name = "extended_dn",
329 .search = extended_search,
330 .init_context = extended_init
333 int ldb_extended_dn_init(void)
335 return ldb_register_module(&extended_dn_ops);