1 /*
2 ldb database library
3
4 Copyright (C) Simo Sorce 2005-2008
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007-2009
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 * Name: ldb
23 *
24 * Component: ldb extended dn control module
25 *
26 * Description: this module builds a special dn for returned search
27 * results, and fixes some other aspects of the result (returned case issues)
28 * values.
29 *
30 * Authors: Simo Sorce
31 * Andrew Bartlett
32 */
33
34 #include "includes.h"
35 #include <ldb.h>
36 #include <ldb_errors.h>
37 #include <ldb_module.h>
38 #include "libcli/security/security.h"
39 #include "librpc/gen_ndr/ndr_misc.h"
40 #include "librpc/gen_ndr/ndr_security.h"
41 #include "librpc/ndr/libndr.h"
42 #include "dsdb/samdb/samdb.h"
43 #include "dsdb/samdb/ldb_modules/util.h"
44
45 struct extended_dn_out_private {
46 bool dereference;
47 bool normalise;
48 const char **attrs;
49 };
50
copy_attrs(void * mem_ctx,const char * const * attrs)51 static char **copy_attrs(void *mem_ctx, const char * const * attrs)
52 {
53 char **nattrs;
54 unsigned int i, num;
55
56 for (num = 0; attrs[num]; num++);
57
58 nattrs = talloc_array(mem_ctx, char *, num + 1);
59 if (!nattrs) return NULL;
60
61 for(i = 0; i < num; i++) {
62 nattrs[i] = talloc_strdup(nattrs, attrs[i]);
63 if (!nattrs[i]) {
64 talloc_free(nattrs);
65 return NULL;
66 }
67 }
68 nattrs[i] = NULL;
69
70 return nattrs;
71 }
72
add_attrs(void * mem_ctx,char *** attrs,const char * attr)73 static bool add_attrs(void *mem_ctx, char ***attrs, const char *attr)
74 {
75 char **nattrs;
76 unsigned int num;
77
78 for (num = 0; (*attrs)[num]; num++);
79
80 nattrs = talloc_realloc(mem_ctx, *attrs, char *, num + 2);
81 if (!nattrs) return false;
82
83 *attrs = nattrs;
84
85 nattrs[num] = talloc_strdup(nattrs, attr);
86 if (!nattrs[num]) return false;
87
88 nattrs[num + 1] = NULL;
89
90 return true;
91 }
92
93 /* Inject the extended DN components, so the DN cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
94 <GUID=541203ae-f7d6-47ef-8390-bfcf019f9583>;<SID=S-1-5-21-4177067393-1453636373-93818737-500>;cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com */
95
inject_extended_dn_out(struct ldb_reply * ares,struct ldb_context * ldb,int type,bool remove_guid,bool remove_sid)96 static int inject_extended_dn_out(struct ldb_reply *ares,
97 struct ldb_context *ldb,
98 int type,
99 bool remove_guid,
100 bool remove_sid)
101 {
102 int ret;
103 const DATA_BLOB *guid_blob;
104 const DATA_BLOB *sid_blob;
105
106 guid_blob = ldb_msg_find_ldb_val(ares->message, "objectGUID");
107 sid_blob = ldb_msg_find_ldb_val(ares->message, "objectSid");
108
109 if (!guid_blob) {
110 ldb_set_errstring(ldb, "Did not find objectGUID to inject into extended DN");
111 return LDB_ERR_OPERATIONS_ERROR;
112 }
113
114 ret = ldb_dn_set_extended_component(ares->message->dn, "GUID", guid_blob);
115 if (ret != LDB_SUCCESS) {
116 return ret;
117 }
118 if (sid_blob) {
119 ret = ldb_dn_set_extended_component(ares->message->dn, "SID", sid_blob);
120 if (ret != LDB_SUCCESS) {
121 return ret;
122 }
123 }
124
125 if (remove_guid) {
126 ldb_msg_remove_attr(ares->message, "objectGUID");
127 }
128
129 if (sid_blob && remove_sid) {
130 ldb_msg_remove_attr(ares->message, "objectSid");
131 }
132
133 return LDB_SUCCESS;
134 }
135
136 /* search */
137 struct extended_search_context {
138 struct ldb_module *module;
139 const struct dsdb_schema *schema;
140 struct ldb_request *req;
141 bool inject;
142 bool remove_guid;
143 bool remove_sid;
144 int extended_type;
145 };
146
147
148 /*
149 fix one-way links to have the right string DN, to cope with
150 renames of the target
151 */
fix_one_way_link(struct extended_search_context * ac,struct ldb_dn * dn,bool is_deleted_objects,bool * remove_value,uint32_t linkID)152 static int fix_one_way_link(struct extended_search_context *ac, struct ldb_dn *dn,
153 bool is_deleted_objects, bool *remove_value,
154 uint32_t linkID)
155 {
156 struct GUID guid;
157 NTSTATUS status;
158 int ret;
159 struct ldb_dn *real_dn;
160 uint32_t search_flags;
161 TALLOC_CTX *tmp_ctx = talloc_new(ac);
162 const char *attrs[] = { NULL };
163 struct ldb_result *res;
164
165 (*remove_value) = false;
166
167 status = dsdb_get_extended_dn_guid(dn, &guid, "GUID");
168 if (!NT_STATUS_IS_OK(status)) {
169 /* this is a strange DN that doesn't have a GUID! just
170 return the current DN string?? */
171 talloc_free(tmp_ctx);
172 return LDB_SUCCESS;
173 }
174
175 search_flags = DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SEARCH_ALL_PARTITIONS | DSDB_SEARCH_ONE_ONLY;
176
177 if (linkID == 0) {
178 /* You must ALWAYS show one-way links regardless of the state of the target */
179 search_flags |= (DSDB_SEARCH_SHOW_DELETED | DSDB_SEARCH_SHOW_RECYCLED);
180 }
181
182 ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE, attrs,
183 search_flags, ac->req, "objectguid=%s", GUID_string(tmp_ctx, &guid));
184 if (ret != LDB_SUCCESS || res->count != 1) {
185 /* if we can't resolve this GUID, then we don't
186 display the link. This could be a link to a NC that we don't
187 have, or it could be a link to a deleted object
188 */
189 (*remove_value) = true;
190 talloc_free(tmp_ctx);
191 return LDB_SUCCESS;
192 }
193 real_dn = res->msgs[0]->dn;
194
195 if (strcmp(ldb_dn_get_linearized(dn), ldb_dn_get_linearized(real_dn)) == 0) {
196 /* its already correct */
197 talloc_free(tmp_ctx);
198 return LDB_SUCCESS;
199 }
200
201 /* fix the DN by replacing its components with those from the
202 * real DN
203 */
204 if (!ldb_dn_replace_components(dn, real_dn)) {
205 talloc_free(tmp_ctx);
206 return ldb_operr(ldb_module_get_ctx(ac->module));
207 }
208 talloc_free(tmp_ctx);
209
210 return LDB_SUCCESS;
211 }
212
213
214 /*
215 this is called to post-process the results from the search
216 */
extended_callback(struct ldb_request * req,struct ldb_reply * ares)217 static int extended_callback(struct ldb_request *req, struct ldb_reply *ares)
218 {
219 struct extended_search_context *ac;
220 int ret;
221 unsigned int i, j, k;
222 struct ldb_message *msg;
223 struct extended_dn_out_private *p;
224 struct ldb_context *ldb;
225 bool have_reveal_control=false;
226
227 ac = talloc_get_type(req->context, struct extended_search_context);
228 p = talloc_get_type(ldb_module_get_private(ac->module), struct extended_dn_out_private);
229 ldb = ldb_module_get_ctx(ac->module);
230 if (!ares) {
231 return ldb_module_done(ac->req, NULL, NULL,
232 LDB_ERR_OPERATIONS_ERROR);
233 }
234 if (ares->error != LDB_SUCCESS) {
235 return ldb_module_done(ac->req, ares->controls,
236 ares->response, ares->error);
237 }
238
239 msg = ares->message;
240
241 switch (ares->type) {
242 case LDB_REPLY_REFERRAL:
243 return ldb_module_send_referral(ac->req, ares->referral);
244
245 case LDB_REPLY_DONE:
246 return ldb_module_done(ac->req, ares->controls,
247 ares->response, LDB_SUCCESS);
248 case LDB_REPLY_ENTRY:
249 break;
250 }
251
252 if (p && p->normalise) {
253 ret = dsdb_fix_dn_rdncase(ldb, ares->message->dn);
254 if (ret != LDB_SUCCESS) {
255 return ldb_module_done(ac->req, NULL, NULL, ret);
256 }
257 }
258
259 if (ac->inject) {
260 /* for each record returned post-process to add any derived
261 attributes that have been asked for */
262 ret = inject_extended_dn_out(ares, ldb,
263 ac->extended_type, ac->remove_guid,
264 ac->remove_sid);
265 if (ret != LDB_SUCCESS) {
266 return ldb_module_done(ac->req, NULL, NULL, ret);
267 }
268 }
269
270 if ((p && p->normalise) || ac->inject) {
271 const struct ldb_val *val = ldb_msg_find_ldb_val(ares->message, "distinguishedName");
272 if (val) {
273 ldb_msg_remove_attr(ares->message, "distinguishedName");
274 if (ac->inject) {
275 ret = ldb_msg_add_steal_string(ares->message, "distinguishedName",
276 ldb_dn_get_extended_linearized(ares->message, ares->message->dn, ac->extended_type));
277 } else {
278 ret = ldb_msg_add_linearized_dn(ares->message,
279 "distinguishedName",
280 ares->message->dn);
281 }
282 if (ret != LDB_SUCCESS) {
283 return ldb_oom(ldb);
284 }
285 }
286 }
287
288 have_reveal_control =
289 dsdb_request_has_control(req, LDB_CONTROL_REVEAL_INTERNALS);
290
291 /*
292 * Shortcut for repl_meta_data. We asked for the data
293 * 'as-is', so stop processing here!
294 */
295 if (have_reveal_control && p->normalise == false && ac->inject == true) {
296 return ldb_module_send_entry(ac->req, msg, ares->controls);
297 }
298
299 /* Walk the returned elements (but only if we have a schema to
300 * interpret the list with) */
301 for (i = 0; ac->schema && i < msg->num_elements; i++) {
302 bool make_extended_dn;
303 const struct dsdb_attribute *attribute;
304
305 attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
306 if (!attribute) {
307 continue;
308 }
309
310 if (p && p->normalise) {
311 /* If we are also in 'normalise' mode, then
312 * fix the attribute names to be in the
313 * correct case */
314 msg->elements[i].name = talloc_strdup(msg->elements, attribute->lDAPDisplayName);
315 if (!msg->elements[i].name) {
316 ldb_oom(ldb);
317 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
318 }
319 }
320
321 /* distinguishedName has been dealt with above */
322 if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) {
323 continue;
324 }
325
326 /* Look to see if this attributeSyntax is a DN */
327 if (attribute->dn_format == DSDB_INVALID_DN) {
328 continue;
329 }
330
331 make_extended_dn = ac->inject;
332
333 /* Always show plain DN in case of Object(OR-Name) syntax */
334 if (make_extended_dn) {
335 make_extended_dn = (strcmp(attribute->syntax->ldap_oid, DSDB_SYNTAX_OR_NAME) != 0);
336 }
337
338 for (k = 0, j = 0; j < msg->elements[i].num_values; j++) {
339 const char *dn_str;
340 struct ldb_dn *dn;
341 struct dsdb_dn *dsdb_dn = NULL;
342 struct ldb_val *plain_dn = &msg->elements[i].values[j];
343 bool is_deleted_objects = false;
344
345 /* this is a fast method for detecting deleted
346 linked attributes, working on the unparsed
347 ldb_val */
348 if (dsdb_dn_is_deleted_val(plain_dn) && !have_reveal_control) {
349 /* it's a deleted linked attribute,
350 and we don't have the reveal control */
351 /* we won't keep this one, so not incrementing k */
352 continue;
353 }
354
355
356 dsdb_dn = dsdb_dn_parse_trusted(msg, ldb, plain_dn, attribute->syntax->ldap_oid);
357
358 if (!dsdb_dn) {
359 ldb_asprintf_errstring(ldb,
360 "could not parse %.*s in %s on %s as a %s DN",
361 (int)plain_dn->length, plain_dn->data,
362 msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
363 attribute->syntax->ldap_oid);
364 talloc_free(dsdb_dn);
365 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
366 }
367 dn = dsdb_dn->dn;
368
369 /* we need to know if this is a link to the
370 deleted objects container for fixing one way
371 links */
372 if (dsdb_dn->extra_part.length == 16) {
373 char *hex_string = data_blob_hex_string_upper(req, &dsdb_dn->extra_part);
374 if (hex_string && strcmp(hex_string, DS_GUID_DELETED_OBJECTS_CONTAINER) == 0) {
375 is_deleted_objects = true;
376 }
377 talloc_free(hex_string);
378 }
379
380 if (p->normalise) {
381 ret = dsdb_fix_dn_rdncase(ldb, dn);
382 if (ret != LDB_SUCCESS) {
383 talloc_free(dsdb_dn);
384 return ldb_module_done(ac->req, NULL, NULL, ret);
385 }
386 }
387
388 /* Look for this value in the attribute */
389
390 /* note that we don't fixup objectCategory as
391 it should not be possible to move
392 objectCategory elements in the schema */
393 if (attribute->one_way_link &&
394 strcasecmp(attribute->lDAPDisplayName, "objectCategory") != 0) {
395 bool remove_value;
396 ret = fix_one_way_link(ac, dn, is_deleted_objects, &remove_value,
397 attribute->linkID);
398 if (ret != LDB_SUCCESS) {
399 talloc_free(dsdb_dn);
400 return ldb_module_done(ac->req, NULL, NULL, ret);
401 }
402 if (remove_value &&
403 !ldb_request_get_control(req, LDB_CONTROL_REVEAL_INTERNALS)) {
404 /* we show these with REVEAL
405 to allow dbcheck to find and
406 cleanup these orphaned links */
407 /* we won't keep this one, so not incrementing k */
408 continue;
409 }
410 }
411
412 if (make_extended_dn) {
413 if (!ldb_dn_validate(dsdb_dn->dn)) {
414 ldb_asprintf_errstring(ldb,
415 "could not parse %.*s in %s on %s as a %s DN",
416 (int)plain_dn->length, plain_dn->data,
417 msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
418 attribute->syntax->ldap_oid);
419 talloc_free(dsdb_dn);
420 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
421 }
422 /* don't let users see the internal extended
423 GUID components */
424 if (!have_reveal_control) {
425 const char *accept[] = { "GUID", "SID", NULL };
426 ldb_dn_extended_filter(dn, accept);
427 }
428 dn_str = dsdb_dn_get_extended_linearized(msg->elements[i].values,
429 dsdb_dn, ac->extended_type);
430 } else {
431 dn_str = dsdb_dn_get_linearized(msg->elements[i].values,
432 dsdb_dn);
433 }
434
435 if (!dn_str) {
436 ldb_oom(ldb);
437 talloc_free(dsdb_dn);
438 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
439 }
440 msg->elements[i].values[k] = data_blob_string_const(dn_str);
441 talloc_free(dsdb_dn);
442 k++;
443 }
444
445 if (k == 0) {
446 /* we've deleted all of the values from this
447 * element - remove the element */
448 ldb_msg_remove_element(msg, &msg->elements[i]);
449 i--;
450 } else {
451 msg->elements[i].num_values = k;
452 }
453 }
454 return ldb_module_send_entry(ac->req, msg, ares->controls);
455 }
456
extended_callback_ldb(struct ldb_request * req,struct ldb_reply * ares)457 static int extended_callback_ldb(struct ldb_request *req, struct ldb_reply *ares)
458 {
459 return extended_callback(req, ares);
460 }
461
extended_dn_out_search(struct ldb_module * module,struct ldb_request * req,int (* callback)(struct ldb_request * req,struct ldb_reply * ares))462 static int extended_dn_out_search(struct ldb_module *module, struct ldb_request *req,
463 int (*callback)(struct ldb_request *req, struct ldb_reply *ares))
464 {
465 struct ldb_control *control;
466 struct ldb_control *storage_format_control;
467 struct ldb_extended_dn_control *extended_ctrl = NULL;
468 struct extended_search_context *ac;
469 struct ldb_request *down_req;
470 char **new_attrs;
471 const char * const *const_attrs;
472 struct ldb_context *ldb = ldb_module_get_ctx(module);
473 int ret;
474
475 struct extended_dn_out_private *p = talloc_get_type(ldb_module_get_private(module), struct extended_dn_out_private);
476
477 /* The schema manipulation does not apply to special DNs */
478 if (ldb_dn_is_special(req->op.search.base)) {
479 return ldb_next_request(module, req);
480 }
481
482 /* check if there's an extended dn control */
483 control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
484 if (control && control->data) {
485 extended_ctrl = talloc_get_type(control->data, struct ldb_extended_dn_control);
486 if (!extended_ctrl) {
487 return LDB_ERR_PROTOCOL_ERROR;
488 }
489 }
490
491 /* Look to see if, as we are in 'store DN+GUID+SID' mode, the
492 * client is after the storage format (to fill in linked
493 * attributes) */
494 storage_format_control = ldb_request_get_control(req, DSDB_CONTROL_DN_STORAGE_FORMAT_OID);
495 if (!control && storage_format_control && storage_format_control->data) {
496 extended_ctrl = talloc_get_type(storage_format_control->data, struct ldb_extended_dn_control);
497 if (!extended_ctrl) {
498 ldb_set_errstring(ldb, "extended_dn_out: extended_ctrl was of the wrong data type");
499 return LDB_ERR_PROTOCOL_ERROR;
500 }
501 }
502
503 ac = talloc_zero(req, struct extended_search_context);
504 if (ac == NULL) {
505 return ldb_oom(ldb);
506 }
507
508 ac->module = module;
509 ac->schema = dsdb_get_schema(ldb, ac);
510 ac->req = req;
511 ac->inject = false;
512 ac->remove_guid = false;
513 ac->remove_sid = false;
514
515 const_attrs = req->op.search.attrs;
516
517 /* We only need to do special processing if we were asked for
518 * the extended DN, or we are 'store DN+GUID+SID'
519 * (!dereference) mode. (This is the normal mode for LDB on
520 * tdb). */
521 if (control || (storage_format_control && p)) {
522 ac->inject = true;
523 if (extended_ctrl) {
524 ac->extended_type = extended_ctrl->type;
525 } else {
526 ac->extended_type = 0;
527 }
528
529 /* check if attrs only is specified, in that case check whether we need to modify them */
530 if (req->op.search.attrs && !is_attr_in_list(req->op.search.attrs, "*")) {
531 if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
532 ac->remove_guid = true;
533 }
534 if (! is_attr_in_list(req->op.search.attrs, "objectSid")) {
535 ac->remove_sid = true;
536 }
537 if (ac->remove_guid || ac->remove_sid) {
538 new_attrs = copy_attrs(ac, req->op.search.attrs);
539 if (new_attrs == NULL) {
540 return ldb_oom(ldb);
541 }
542
543 if (ac->remove_guid) {
544 if (!add_attrs(ac, &new_attrs, "objectGUID"))
545 return ldb_operr(ldb);
546 }
547 if (ac->remove_sid) {
548 if (!add_attrs(ac, &new_attrs, "objectSid"))
549 return ldb_operr(ldb);
550 }
551 const_attrs = (const char * const *)new_attrs;
552 }
553 }
554 }
555
556 ret = ldb_build_search_req_ex(&down_req,
557 ldb, ac,
558 req->op.search.base,
559 req->op.search.scope,
560 req->op.search.tree,
561 const_attrs,
562 req->controls,
563 ac, callback,
564 req);
565 LDB_REQ_SET_LOCATION(down_req);
566 if (ret != LDB_SUCCESS) {
567 return ret;
568 }
569
570 /* mark extended DN and storage format controls as done */
571 if (control) {
572 control->critical = 0;
573 }
574
575 if (storage_format_control) {
576 storage_format_control->critical = 0;
577 }
578
579 /* perform the search */
580 return ldb_next_request(module, down_req);
581 }
582
extended_dn_out_ldb_search(struct ldb_module * module,struct ldb_request * req)583 static int extended_dn_out_ldb_search(struct ldb_module *module, struct ldb_request *req)
584 {
585 return extended_dn_out_search(module, req, extended_callback_ldb);
586 }
587
extended_dn_out_ldb_init(struct ldb_module * module)588 static int extended_dn_out_ldb_init(struct ldb_module *module)
589 {
590 int ret;
591
592 struct extended_dn_out_private *p = talloc(module, struct extended_dn_out_private);
593 struct dsdb_extended_dn_store_format *dn_format;
594
595 ldb_module_set_private(module, p);
596
597 if (!p) {
598 return ldb_oom(ldb_module_get_ctx(module));
599 }
600
601 dn_format = talloc(p, struct dsdb_extended_dn_store_format);
602 if (!dn_format) {
603 talloc_free(p);
604 return ldb_oom(ldb_module_get_ctx(module));
605 }
606
607 dn_format->store_extended_dn_in_ldb = true;
608 ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
609 if (ret != LDB_SUCCESS) {
610 talloc_free(p);
611 return ret;
612 }
613
614 p->dereference = false;
615 p->normalise = false;
616
617 ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
618 if (ret != LDB_SUCCESS) {
619 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
620 "extended_dn_out: Unable to register control with rootdse!\n");
621 return ldb_operr(ldb_module_get_ctx(module));
622 }
623
624 return ldb_next_init(module);
625 }
626
627 static const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
628 .name = "extended_dn_out_ldb",
629 .search = extended_dn_out_ldb_search,
630 .init_context = extended_dn_out_ldb_init,
631 };
632
633 /*
634 initialise the module
635 */
ldb_extended_dn_out_module_init(const char * version)636 _PUBLIC_ int ldb_extended_dn_out_module_init(const char *version)
637 {
638 int ret;
639 LDB_MODULE_CHECK_VERSION(version);
640 ret = ldb_register_module(&ldb_extended_dn_out_ldb_module_ops);
641 if (ret != LDB_SUCCESS) {
642 return ret;
643 }
644 return LDB_SUCCESS;
645 }
646