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 ac = talloc_get_type(context, struct extended_context);
186 if (ares->type == LDB_REPLY_ENTRY) {
187 /* for each record returned post-process to add any derived
188 attributes that have been asked for */
189 if (!inject_extended_dn(ares->message, ldb, ac->extended_type, ac->remove_guid, ac->remove_sid)) {
194 return ac->up_callback(ldb, ac->up_context, ares);
198 return LDB_ERR_OPERATIONS_ERROR;
201 static int extended_search(struct ldb_module *module, struct ldb_request *req)
203 struct ldb_control *control;
204 struct ldb_extended_dn_control *extended_ctrl;
205 struct ldb_control **saved_controls;
206 struct extended_context *ac;
207 struct ldb_request *down_req;
211 /* check if there's an extended dn control */
212 control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
213 if (control == NULL) {
214 /* not found go on */
215 return ldb_next_request(module, req);
218 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
219 if (!extended_ctrl) {
220 return LDB_ERR_PROTOCOL_ERROR;
223 ac = talloc(req, struct extended_context);
225 return LDB_ERR_OPERATIONS_ERROR;
229 ac->up_context = req->context;
230 ac->up_callback = req->callback;
231 ac->attrs = req->op.search.attrs;
232 ac->remove_guid = false;
233 ac->remove_sid = false;
234 ac->extended_type = extended_ctrl->type;
236 down_req = talloc_zero(req, struct ldb_request);
237 if (down_req == NULL) {
238 return LDB_ERR_OPERATIONS_ERROR;
241 down_req->operation = req->operation;
242 down_req->op.search.base = req->op.search.base;
243 down_req->op.search.scope = req->op.search.scope;
244 down_req->op.search.tree = req->op.search.tree;
246 /* check if attrs only is specified, in that case check wether we need to modify them */
247 if (req->op.search.attrs) {
248 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
249 ac->remove_guid = true;
251 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
252 ac->remove_sid = true;
254 if (ac->remove_guid || ac->remove_sid) {
255 new_attrs = copy_attrs(down_req, req->op.search.attrs);
256 if (new_attrs == NULL)
257 return LDB_ERR_OPERATIONS_ERROR;
259 if (ac->remove_guid) {
260 if (!add_attrs(down_req, &new_attrs, "objectGUID"))
261 return LDB_ERR_OPERATIONS_ERROR;
263 if (ac->remove_sid) {
264 if (!add_attrs(down_req, &new_attrs, "objectSID"))
265 return LDB_ERR_OPERATIONS_ERROR;
268 down_req->op.search.attrs = (const char * const *)new_attrs;
272 down_req->controls = req->controls;
274 /* save it locally and remove it from the list */
275 /* we do not need to replace them later as we
276 * are keeping the original req intact */
277 if (!save_controls(control, down_req, &saved_controls)) {
278 return LDB_ERR_OPERATIONS_ERROR;
281 down_req->context = ac;
282 down_req->callback = extended_callback;
283 ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
285 /* perform the search */
286 ret = ldb_next_request(module, down_req);
288 /* do not free down_req as the call results may be linked to it,
289 * it will be freed when the upper level request get freed */
290 if (ret == LDB_SUCCESS) {
291 req->handle = down_req->handle;
297 static int extended_init(struct ldb_module *module)
299 struct ldb_request *req;
302 req = talloc(module, struct ldb_request);
304 return LDB_ERR_OPERATIONS_ERROR;
307 req->operation = LDB_REQ_REGISTER_CONTROL;
308 req->op.reg_control.oid = LDB_CONTROL_EXTENDED_DN_OID;
309 req->controls = NULL;
311 ret = ldb_request(module->ldb, req);
312 if (ret != LDB_SUCCESS) {
313 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "extended_dn: Unable to register control with rootdse!\n");
315 return LDB_ERR_OPERATIONS_ERROR;
319 return ldb_next_init(module);
322 static const struct ldb_module_ops extended_dn_ops = {
323 .name = "extended_dn",
324 .search = extended_search,
325 .init_context = extended_init
328 int ldb_extended_dn_init(void)
330 return ldb_register_module(&extended_dn_ops);