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"
43 static BOOL is_attr_in_list(const char * const * attrs, const char *attr)
47 for (i = 0; attrs[i]; i++) {
48 if (strcasecmp(attrs[i], attr) == 0)
55 static char **copy_attrs(void *mem_ctx, const char * const * attrs)
60 for (num = 0; attrs[num]; num++);
62 new = talloc_array(mem_ctx, char *, num + 1);
63 if (!new) return NULL;
65 for(i = 0; i < num; i++) {
66 new[i] = talloc_strdup(new, attrs[i]);
77 static BOOL add_attrs(void *mem_ctx, char ***attrs, const char *attr)
82 for (num = 0; (*attrs)[num]; num++);
84 new = talloc_realloc(mem_ctx, *attrs, char *, num + 2);
85 if (!new) return False;
89 new[num] = talloc_strdup(new, attr);
90 if (!new[num]) return False;
97 static BOOL inject_extended_dn(struct ldb_message *msg,
102 const struct ldb_val *val;
109 dn = ldb_dn_linearize(msg, msg->dn);
113 /* retrieve object_guid */
114 guid = samdb_result_guid(msg, "objectGUID");
115 object_guid = GUID_string(msg, &guid);
120 ldb_msg_remove_attr(msg, "objectGUID");
122 /* retrieve object_sid */
124 sid = samdb_result_dom_sid(msg, msg, "objectSID");
126 object_sid = dom_sid_string(msg, sid);
131 ldb_msg_remove_attr(msg, "objectSID");
134 /* TODO: handle type */
139 new_dn = talloc_asprintf(msg, "<GUID=%s>;<SID=%s>;%s",
140 object_guid, object_sid, dn);
142 new_dn = talloc_asprintf(msg, "<GUID=%s>;%s",
153 msg->dn = ldb_dn_explode_or_special(msg, new_dn);
157 val = ldb_msg_find_ldb_val(msg, "distinguishedName");
159 ldb_msg_remove_attr(msg, "distinguishedName");
160 if (ldb_msg_add_string(msg, "distinguishedName", new_dn))
168 static int extended_search(struct ldb_module *module, struct ldb_request *req)
170 struct ldb_result *extended_result;
171 struct ldb_control *control;
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 /* check if there's a paged request control */
181 control = get_control_from_list(req->controls, LDB_CONTROL_EXTENDED_DN_OID);
182 if (control == NULL) {
183 /* not found go on */
184 return ldb_next_request(module, req);
187 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
188 if (!extended_ctrl) {
189 return LDB_ERR_PROTOCOL_ERROR;
192 /* save it locally and remove it from the list */
193 if (!save_controls(control, req, &saved_controls)) {
194 return LDB_ERR_OPERATIONS_ERROR;
197 /* check if attrs only is specified, in that case check wether we need to modify them */
198 if (req->op.search.attrs) {
199 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
202 if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
205 if (remove_guid || remove_sid) {
206 new_attrs = copy_attrs(req, req->op.search.attrs);
208 return LDB_ERR_OPERATIONS_ERROR;
210 saved_attrs = req->op.search.attrs;
213 if (!add_attrs(req, &new_attrs, "objectGUID"))
214 return LDB_ERR_OPERATIONS_ERROR;
217 if (!add_attrs(req, &new_attrs, "objectSID"))
218 return LDB_ERR_OPERATIONS_ERROR;
221 req->op.search.attrs = (const char * const *)new_attrs;
225 ret = ldb_next_request(module, req);
227 /* put request back into original shape */
228 /* TODO: build a new req and don't touch the original one */
230 if (req->controls) talloc_free(req->controls);
231 req->controls = saved_controls;
234 talloc_free(new_attrs);
235 req->op.search.attrs = saved_attrs;
238 if (ret != LDB_SUCCESS) {
242 extended_result = req->op.search.res;
244 for (i = 0; i < extended_result->count; i++) {
245 /* TODO: the following funtion updates only dn and
246 * distinguishedName. We still need to address other
247 * DN entries like objectCategory
249 if (!inject_extended_dn(extended_result->msgs[i],
251 remove_guid, remove_sid)) {
252 return LDB_ERR_OPERATIONS_ERROR;
259 static int extended_request(struct ldb_module *module, struct ldb_request *req)
261 switch (req->operation) {
264 return extended_search(module, req);
267 return ldb_next_request(module, req);
272 static int extended_init(struct ldb_module *module)
274 struct ldb_request request;
277 request.operation = LDB_REQ_REGISTER;
278 request.op.reg.oid = LDB_CONTROL_EXTENDED_DN_OID;
279 request.controls = NULL;
281 ret = ldb_request(module->ldb, &request);
282 if (ret != LDB_SUCCESS) {
283 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "extended_dn: Unable to register control with rootdse!\n");
284 return LDB_ERR_OTHER;
287 return ldb_next_init(module);
290 static const struct ldb_module_ops extended_dn_ops = {
291 .name = "extended_dn",
292 .request = extended_request,
293 .init_context = extended_init
296 int ldb_extended_dn_init(void)
298 return ldb_register_module(&extended_dn_ops);