summaryrefslogtreecommitdiffstats
path: root/source4/dsdb/samdb/ldb_modules/extended_dn_store.c
blob: 42970dae39ee627a1f2cacf28552378f9c62e50a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/* 
   ldb database library

   Copyright (C) Simo Sorce 2005-2008
   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007-2009

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 *  Name: ldb
 *
 *  Component: ldb extended dn control module
 *
 *  Description: this module builds a special dn for returned search
 *  results nad creates the special DN in the backend store for new
 *  values.
 *
 *  This also has the curious result that we convert <SID=S-1-2-345>
 *  in an attribute value into a normal DN for the rest of the stack
 *  to process
 *
 *  Authors: Simo Sorce
 *           Andrew Bartlett
 */

#include "includes.h"
#include <ldb.h>
#include <ldb_errors.h>
#include <ldb_module.h>
#include "librpc/gen_ndr/ndr_misc.h"
#include "dsdb/samdb/samdb.h"
#include "libcli/security/security.h"
#include "dsdb/samdb/ldb_modules/util.h"
#include <time.h>

struct extended_dn_replace_list {
	struct extended_dn_replace_list *next;
	struct dsdb_dn *dsdb_dn;
	TALLOC_CTX *mem_ctx;
	struct ldb_val *replace_dn;
	struct extended_dn_context *ac;
	struct ldb_request *search_req;
	bool fpo_enabled;
	bool require_object;
	bool got_entry;
};


struct extended_dn_context {
	const struct dsdb_schema *schema;
	struct ldb_module *module;
	struct ldb_context *ldb;
	struct ldb_request *req;
	struct ldb_request *new_req;

	struct extended_dn_replace_list *ops;
	struct extended_dn_replace_list *cur;

	/*
	 * Used by the FPO-enabled attribute validation.
	 */
	struct dsdb_trust_routing_table *routing_table;
};


static struct extended_dn_context *extended_dn_context_init(struct ldb_module *module,
							    struct ldb_request *req)
{
	struct extended_dn_context *ac;
	struct ldb_context *ldb = ldb_module_get_ctx(module);
	ac = talloc_zero(req, struct extended_dn_context);
	if (ac == NULL) {
		ldb_oom(ldb);
		return NULL;
	}

	ac->schema = dsdb_get_schema(ldb, ac);
	ac->module = module;
	ac->ldb = ldb;
	ac->req = req;

	return ac;
}

static int extended_replace_dn(struct extended_dn_replace_list *os,
			       struct ldb_dn *dn)
{
	struct dsdb_dn *dsdb_dn = NULL;
	const char *str = NULL;

	/*
	 * Rebuild with the string or binary 'extra part' the
	 * DN may have had as a prefix
	 */
	dsdb_dn = dsdb_dn_construct(os, dn,
				    os->dsdb_dn->extra_part,
				    os->dsdb_dn->oid);
	if (dsdb_dn == NULL) {
		return ldb_module_operr(os->ac->module);
	}

	str = dsdb_dn_get_extended_linearized(os->mem_ctx,
					      dsdb_dn, 1);
	if (str == NULL) {
		return ldb_module_operr(os->ac->module);
	}

	/*
	 * Replace the DN with the extended version of the DN
	 * (ie, add SID and GUID)
	 */
	*os->replace_dn = data_blob_string_const(str);
	os->got_entry = true;
	return LDB_SUCCESS;
}

static int extended_dn_handle_fpo_attr(struct extended_dn_replace_list *os)
{
	struct dom_sid target_sid = { 0, };
	struct dom_sid target_domain = { 0, };
	struct ldb_message *fmsg = NULL;
	char *fsid = NULL;
	const struct dom_sid *domain_sid = NULL;
	struct ldb_dn *domain_dn = NULL;
	const struct lsa_TrustDomainInfoInfoEx *tdo = NULL;
	uint32_t trust_attributes = 0;
	const char *no_attrs[] = { NULL, };
	struct ldb_result *res = NULL;
	NTSTATUS status;
	bool match;
	bool ok;
	int ret;

	/*
	 * DN doesn't exist yet
	 *
	 * Check if a foreign SID is specified,
	 * which would trigger the creation
	 * of a foreignSecurityPrincipal.
	 */
	status = dsdb_get_extended_dn_sid(os->dsdb_dn->dn,
					  &target_sid,
					  "SID");
	if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
		/*
		 * No SID specified
		 */
		return dsdb_module_werror(os->ac->module,
					  LDB_ERR_NO_SUCH_OBJECT,
					  WERR_NO_SUCH_USER,
					  "specified dn doesn't exist");
	}
	if (!NT_STATUS_IS_OK(status)) {
		return ldb_module_operr(os->ac->module);
	}
	if (ldb_dn_get_extended_comp_num(os->dsdb_dn->dn) != 1) {
		return dsdb_module_werror(os->ac->module,
					  LDB_ERR_NO_SUCH_OBJECT,
					  WERR_NO_SUCH_USER,
					  "specified extended component other than SID");
	}
	if (ldb_dn_get_comp_num(os->dsdb_dn->dn) != 0) {
		return dsdb_module_werror(os->ac->module,
					  LDB_ERR_NO_SUCH_OBJECT,
					  WERR_NO_SUCH_USER,
					  "specified more the SID");
	}

	target_domain = target_sid;
	sid_split_rid(&target_domain, NULL);

	match = dom_sid_equal(&global_sid_Builtin, &target_domain);
	if (match) {
		/*
		 * Non existing BUILTIN sid
		 */
		return dsdb_module_werror(os->ac->module,
				LDB_ERR_NO_SUCH_OBJECT,
				WERR_NO_SUCH_MEMBER,
				"specified sid doesn't exist in BUILTIN");
	}

	domain_sid = samdb_domain_sid(os->ac->ldb);
	if (domain_sid == NULL) {
		return ldb_module_operr(os->ac->module);
	}
	match = dom_sid_equal(domain_sid, &target_domain);
	if (match) {
		/*
		 * Non existing SID in our domain.
		 */
		return dsdb_module_werror(os->ac->module,
				LDB_ERR_UNWILLING_TO_PERFORM,
				WERR_DS_INVALID_GROUP_TYPE,
				"specified sid doesn't exist in domain");
	}

	if (os->ac->routing_table == NULL) {
		status = dsdb_trust_routing_table_load(os->ac->ldb, os->ac,
						       &os->ac->routing_table);
		if (!NT_STATUS_IS_OK(status)) {
			return ldb_module_operr(os->ac->module);
		}
	}

	tdo = dsdb_trust_domain_by_sid(os->ac->routing_table,
				       &target_domain, NULL);
	if (tdo != NULL) {
		trust_attributes = tdo->trust_attributes;
	}

	if (trust_attributes & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
		return dsdb_module_werror(os->ac->module,
				LDB_ERR_UNWILLING_TO_PERFORM,
				WERR_DS_INVALID_GROUP_TYPE,
				"specified sid doesn't exist in forest");
	}

	fmsg = ldb_msg_new(os);
	if (fmsg == NULL) {
		return ldb_module_oom(os->ac->module);
	}

	fsid = dom_sid_string(fmsg, &target_sid);
	if (fsid == NULL) {
		return ldb_module_oom(os->ac->module);
	}

	domain_dn = ldb_get_default_basedn(os->ac->ldb);
	if (domain_dn == NULL) {
		return ldb_module_operr(os->ac->module);
	}

	fmsg->dn = ldb_dn_copy(fmsg, domain_dn);
	if (fmsg->dn == NULL) {
		return ldb_module_oom(os->ac->module);
	}

	ok = ldb_dn_add_child_fmt(fmsg->dn,
				  "CN=%s,CN=ForeignSecurityPrincipals",
				  fsid);
	if (!ok) {
		return ldb_module_oom(os->ac->module);
	}

	ret = ldb_msg_add_string(fmsg, "objectClass", "foreignSecurityPrincipal");
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	ret = dsdb_module_add(os->ac->module, fmsg,
			      DSDB_FLAG_AS_SYSTEM |
			      DSDB_FLAG_NEXT_MODULE,
			      os->ac->req);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	ret = dsdb_module_search_dn(os->ac->module, fmsg, &res,
				    fmsg->dn, no_attrs,
				    DSDB_FLAG_AS_SYSTEM |
				    DSDB_FLAG_NEXT_MODULE |
				    DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT,
				    os->ac->req);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	/*
	 * dsdb_module_search_dn() guarantees exactly one result message
	 * on success.
	 */
	ret = extended_replace_dn(os, res->msgs[0]->dn);
	TALLOC_FREE(fmsg);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	return LDB_SUCCESS;
}

/* An extra layer of indirection because LDB does not allow the original request to be altered */

static int extended_final_callback(struct ldb_request *req, struct ldb_reply *ares)
{
	int ret = LDB_ERR_OPERATIONS_ERROR;
	struct extended_dn_context *ac;
	ac = talloc_get_type(req->context, struct extended_dn_context);

	if (ares->error != LDB_SUCCESS) {
		ret = ldb_module_done(ac->req, ares->controls,
				      ares->response, ares->error);
	} else {
		switch (ares->type) {
		case LDB_REPLY_ENTRY:
			
			ret = ldb_module_send_entry(ac->req, ares->message, ares->controls);
			break;
		case LDB_REPLY_REFERRAL:
			
			ret = ldb_module_send_referral(ac->req, ares->referral);
			break;
		case LDB_REPLY_DONE:
			
			ret = ldb_module_done(ac->req, ares->controls,
					      ares->response, ares->error);
			break;
		}
	}
	return ret;
}

static int extended_replace_callback(struct ldb_request *req, struct ldb_reply *ares)
{
	struct extended_dn_replace_list *os = talloc_get_type(req->context, 
							   struct extended_dn_replace_list);

	if (!ares) {
		return ldb_module_done(os->ac->req, NULL, NULL,
					LDB_ERR_OPERATIONS_ERROR);
	}
	if (ares->error == LDB_ERR_NO_SUCH_OBJECT) {
		if (os->got_entry) {
			/* This is in internal error... */
			int ret = ldb_module_operr(os->ac->module);
			return ldb_module_done(os->ac->req, NULL, NULL, ret);
		}

		if (os->require_object && os->fpo_enabled) {
			int ret;

			ret = extended_dn_handle_fpo_attr(os);
			if (ret != LDB_SUCCESS) {
				return ldb_module_done(os->ac->req, NULL, NULL,
						       ret);
			}
			/* os->got_entry is true at this point... */
		}

		if (!os->got_entry && os->require_object) {
			/*
			 * It's an error if the target doesn't exist,
			 * unless it's a delete.
			 */
			int ret = dsdb_module_werror(os->ac->module,
						LDB_ERR_CONSTRAINT_VIOLATION,
						WERR_DS_NAME_REFERENCE_INVALID,
						"Referenced object not found");
			return ldb_module_done(os->ac->req, NULL, NULL, ret);
		}

		/* Don't worry too much about dangling references */

		ldb_reset_err_string(os->ac->ldb);
		if (os->next) {
			struct extended_dn_replace_list *next;

			next = os->next;

			talloc_free(os);

			os = next;
			return ldb_next_request(os->ac->module, next->search_req);
		} else {
			/* Otherwise, we are done - let's run the
			 * request now we have swapped the DNs for the
			 * full versions */
			return ldb_next_request(os->ac->module, os->ac->new_req);
		}
	}
	if (ares->error != LDB_SUCCESS) {
		return ldb_module_done(os->ac->req, ares->controls,
					ares->response, ares->error);
	}

	/* Only entries are interesting, and we only want the olddn */
	switch (ares->type) {
	case LDB_REPLY_ENTRY:
	{
		/* This *must* be the right DN, as this is a base
		 * search.  We can't check, as it could be an extended
		 * DN, so a module below will resolve it */
		int ret;

		ret = extended_replace_dn(os, ares->message->dn);
		if (ret != LDB_SUCCESS) {
			return ldb_module_done(os->ac->req, NULL, NULL, ret);
		}
		/* os->got_entry is true at this point */
		break;
	}
	case LDB_REPLY_REFERRAL:
		/* ignore */
		break;

	case LDB_REPLY_DONE:

		talloc_free(ares);

		if (!os->got_entry && os->require_object && os->fpo_enabled) {
			int ret;

			ret = extended_dn_handle_fpo_attr(os);
			if (ret != LDB_SUCCESS) {
				return ldb_module_done(os->ac->req, NULL, NULL,
						       ret);
			}
			/* os->got_entry is true at this point... */
		}

		if (!os->got_entry && os->require_object) {
			/*
			 * It's an error if the target doesn't exist,
			 * unless it's a delete.
			 */
			int ret = dsdb_module_werror(os->ac->module,
						 LDB_ERR_CONSTRAINT_VIOLATION,
						 WERR_DS_NAME_REFERENCE_INVALID,
						 "Referenced object not found");
			return ldb_module_done(os->ac->req, NULL, NULL, ret);
		}

		/* Run the next search */

		if (os->next) {
			struct extended_dn_replace_list *next;

			next = os->next;

			talloc_free(os);

			os = next;
			return ldb_next_request(os->ac->module, next->search_req);
		} else {
			/* Otherwise, we are done - let's run the
			 * request now we have swapped the DNs for the
			 * full versions */
			return ldb_next_request(os->ac->module, os->ac->new_req);
		}
	}

	talloc_free(ares);
	return LDB_SUCCESS;
}

/* We have a 'normal' DN in the inbound request.  We need to find out
 * what the GUID and SID are on the DN it points to, so we can
 * construct an extended DN for storage.
 *
 * This creates a list of DNs to look up, and the plain DN to replace
 */

static int extended_store_replace(struct extended_dn_context *ac,
				  TALLOC_CTX *callback_mem_ctx,
				  struct ldb_dn *self_dn,
				  struct ldb_val *plain_dn,
				  bool is_delete, 
				  const struct dsdb_attribute *schema_attr)
{
	const char *oid = schema_attr->syntax->ldap_oid;
	int ret;
	struct extended_dn_replace_list *os;
	static const char *attrs[] = {
		"objectSid",
		"objectGUID",
		NULL
	};
	uint32_t ctrl_flags = 0;
	bool is_untrusted = ldb_req_is_untrusted(ac->req);

	os = talloc_zero(ac, struct extended_dn_replace_list);
	if (!os) {
		return ldb_oom(ac->ldb);
	}

	os->ac = ac;
	
	os->mem_ctx = callback_mem_ctx;

	os->dsdb_dn = dsdb_dn_parse(os, ac->ldb, plain_dn, oid);
	if (!os->dsdb_dn || !ldb_dn_validate(os->dsdb_dn->dn)) {
		talloc_free(os);
		ldb_asprintf_errstring(ac->ldb,
				       "could not parse %.*s as a %s DN", (int)plain_dn->length, plain_dn->data,
				       oid);
		return LDB_ERR_INVALID_DN_SYNTAX;
	}

	if (self_dn != NULL) {
		ret = ldb_dn_compare(self_dn, os->dsdb_dn->dn);
		if (ret == 0) {
			/*
			 * If this is a reference to the object
			 * itself during an 'add', we won't
			 * be able to find the object.
			 */
			talloc_free(os);
			return LDB_SUCCESS;
		}
	}

	if (is_delete && !ldb_dn_has_extended(os->dsdb_dn->dn)) {
		/* NO need to figure this DN out, this element is
		 * going to be deleted anyway, and because it's not
		 * extended, we have enough information to do the
		 * delete */
		talloc_free(os);
		return LDB_SUCCESS;
	}
	
		
	os->replace_dn = plain_dn;

	/* The search request here might happen to be for an
	 * 'extended' style DN, such as <GUID=abced...>.  The next
	 * module in the stack will convert this into a normal DN for
	 * processing */
	ret = ldb_build_search_req(&os->search_req,
				   ac->ldb, os, os->dsdb_dn->dn, LDB_SCOPE_BASE, NULL, 
				   attrs, NULL, os, extended_replace_callback,
				   ac->req);
	LDB_REQ_SET_LOCATION(os->search_req);
	if (ret != LDB_SUCCESS) {
		talloc_free(os);
		return ret;
	}

	/*
	 * By default we require the presence of the target.
	 */
	os->require_object = true;

	/*
	 * Handle FPO-enabled attributes, see
	 * [MS-ADTS] 3.1.1.5.2.3 Special Classes and Attributes:
	 *
	 *   FPO-enabled attributes: member, msDS-MembersForAzRole,
	 *     msDS-NeverRevealGroup, msDS-NonMembers, msDS-RevealOnDemandGroup,
	 *     msDS-ServiceAccount.
	 *
	 * Note there's no msDS-ServiceAccount in any schema (only
	 * msDS-HostServiceAccount and that's not an FPO-enabled attribute
	 * at least not in W2008R2)
	 *
	 * msDS-NonMembers always generates NOT_SUPPORTED against W2008R2.
	 *
	 * See also [MS-SAMR] 3.1.1.8.9 member.
	 */
	switch (schema_attr->attributeID_id) {
	case DRSUAPI_ATTID_member:
	case DRSUAPI_ATTID_msDS_MembersForAzRole:
	case DRSUAPI_ATTID_msDS_NeverRevealGroup:
	case DRSUAPI_ATTID_msDS_RevealOnDemandGroup:
		os->fpo_enabled = true;
		break;

	case DRSUAPI_ATTID_msDS_HostServiceAccount:
		/* This is NOT a FPO-enabled attribute */
		break;

	case DRSUAPI_ATTID_msDS_NonMembers:
		return dsdb_module_werror(os->ac->module,
					  LDB_ERR_UNWILLING_TO_PERFORM,
					  WERR_NOT_SUPPORTED,
					  "msDS-NonMembers is not supported");
	}

	if (schema_attr->linkID == 0) {
		/*
		 * None linked attributes allow references
		 * to deleted objects.
		 */
		ctrl_flags |= DSDB_SEARCH_SHOW_RECYCLED;
	}

	if (is_delete) {
		/*
		 * On delete want to be able to
		 * find a deleted object, but
		 * it's not a problem if they doesn't
		 * exist.
		 */
		ctrl_flags |= DSDB_SEARCH_SHOW_RECYCLED;
		os->require_object = false;
	}

	if (!is_untrusted) {
		struct ldb_control *ctrl = NULL;

		/*
		 * During provision or dbcheck we may not find
		 * an object.
		 */

		ctrl = ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID);
		if (ctrl != NULL) {
			os->require_object = false;
		}
		ctrl = ldb_request_get_control(ac->req, DSDB_CONTROL_DBCHECK);
		if (ctrl != NULL) {
			os->require_object = false;
		}
	}

	ret = dsdb_request_add_controls(os->search_req,
					DSDB_FLAG_AS_SYSTEM |
					ctrl_flags |
					DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
	if (ret != LDB_SUCCESS) {
		talloc_free(os);
		return ret;
	}

	if (ac->ops) {
		ac->cur->next = os;
	} else {
		ac->ops = os;
	}
	ac->cur = os;

	return LDB_SUCCESS;
}


/* add */
static int extended_dn_add(struct ldb_module *module, struct ldb_request *req)
{
	struct extended_dn_context *ac;
	int ret;
	unsigned int i, j;

	if (ldb_dn_is_special(req->op.add.message->dn)) {
		/* do not manipulate our control entries */
		return ldb_next_request(module, req);
	}

	ac = extended_dn_context_init(module, req);
	if (!ac) {
		return ldb_operr(ldb_module_get_ctx(module));
	}

	if (!ac->schema) {
		/* without schema, this doesn't make any sense */
		talloc_free(ac);
		return ldb_next_request(module, req);
	}

	for (i=0; i < req->op.add.message->num_elements; i++) {
		const struct ldb_message_element *el = &req->op.add.message->elements[i];
		const struct dsdb_attribute *schema_attr
			= dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
		if (!schema_attr) {
			continue;
		}

		/* We only setup an extended DN GUID on DN elements */
		if (schema_attr->dn_format == DSDB_INVALID_DN) {
			continue;
		}

		if (schema_attr->attributeID_id == DRSUAPI_ATTID_distinguishedName) {
			/* distinguishedName values are ignored */
			continue;
		}

		/* Before we setup a procedure to modify the incoming message, we must copy it */
		if (!ac->new_req) {
			struct ldb_message *msg = ldb_msg_copy(ac, req->op.add.message);
			if (!msg) {
				return ldb_oom(ldb_module_get_ctx(module));
			}
		   
			ret = ldb_build_add_req(&ac->new_req, ac->ldb, ac, msg, req->controls, ac, extended_final_callback, req);
			LDB_REQ_SET_LOCATION(ac->new_req);
			if (ret != LDB_SUCCESS) {
				return ret;
			}
		}
		/* Re-calculate el */
		el = &ac->new_req->op.add.message->elements[i];
		for (j = 0; j < el->num_values; j++) {
			ret = extended_store_replace(ac, ac->new_req,
						     req->op.add.message->dn,
						     &el->values[j],
						     false, schema_attr);
			if (ret != LDB_SUCCESS) {
				return ret;
			}
		}
	}

	/* if no DNs were set continue */
	if (ac->ops == NULL) {
		talloc_free(ac);
		return ldb_next_request(module, req);
	}

	/* start with the searches */
	return ldb_next_request(module, ac->ops->search_req);
}

/* modify */
static int extended_dn_modify(struct ldb_module *module, struct ldb_request *req)
{
	/* Look over list of modifications */
	/* Find if any are for linked attributes */
	/* Determine the effect of the modification */
	/* Apply the modify to the linked entry */

	unsigned int i, j;
	struct extended_dn_context *ac;
	struct ldb_control *fix_links_control = NULL;
	struct ldb_control *fix_link_sid_ctrl = NULL;
	int ret;

	if (ldb_dn_is_special(req->op.mod.message->dn)) {
		/* do not manipulate our control entries */
		return ldb_next_request(module, req);
	}

	ac = extended_dn_context_init(module, req);
	if (!ac) {
		return ldb_operr(ldb_module_get_ctx(module));
	}

	if (!ac->schema) {
		talloc_free(ac);
		/* without schema, this doesn't make any sense */
		return ldb_next_request(module, req);
	}

	fix_links_control = ldb_request_get_control(req,
					DSDB_CONTROL_DBCHECK_FIX_DUPLICATE_LINKS);
	if (fix_links_control != NULL) {
		return ldb_next_request(module, req);
	}

	fix_link_sid_ctrl = ldb_request_get_control(ac->req,
					DSDB_CONTROL_DBCHECK_FIX_LINK_DN_SID);
	if (fix_link_sid_ctrl != NULL) {
		return ldb_next_request(module, req);
	}

	for (i=0; i < req->op.mod.message->num_elements; i++) {
		const struct ldb_message_element *el = &req->op.mod.message->elements[i];
		const struct dsdb_attribute *schema_attr
			= dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
		if (!schema_attr) {
			continue;
		}

		/* We only setup an extended DN GUID on these particular DN objects */
		if (schema_attr->dn_format == DSDB_INVALID_DN) {
			continue;
		}

		if (schema_attr->attributeID_id == DRSUAPI_ATTID_distinguishedName) {
			/* distinguishedName values are ignored */
			continue;
		}

		/* Before we setup a procedure to modify the incoming message, we must copy it */
		if (!ac->new_req) {
			struct ldb_message *msg = ldb_msg_copy(ac, req->op.mod.message);
			if (!msg) {
				talloc_free(ac);
				return ldb_oom(ac->ldb);
			}
		   
			ret = ldb_build_mod_req(&ac->new_req, ac->ldb, ac, msg, req->controls, ac, extended_final_callback, req);
			LDB_REQ_SET_LOCATION(ac->new_req);
			if (ret != LDB_SUCCESS) {
				talloc_free(ac);
				return ret;
			}
		}
		/* Re-calculate el */
		el = &ac->new_req->op.mod.message->elements[i];
		/* For each value being added, we need to setup the lookups to fill in the extended DN */
		for (j = 0; j < el->num_values; j++) {
			/* If we are just going to delete this
			 * element, only do a lookup if
			 * extended_store_replace determines it's an
			 * input of an extended DN */
			bool is_delete = (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE);

			ret = extended_store_replace(ac, ac->new_req,
						     NULL, /* self_dn to be ignored */
						     &el->values[j],
						     is_delete, schema_attr);
			if (ret != LDB_SUCCESS) {
				talloc_free(ac);
				return ret;
			}
		}
	}

	/* if DNs were set continue */
	if (ac->ops == NULL) {
		talloc_free(ac);
		return ldb_next_request(module, req);
	}

	/* start with the searches */
	return ldb_next_request(module, ac->ops->search_req);
}

static const struct ldb_module_ops ldb_extended_dn_store_module_ops = {
	.name		   = "extended_dn_store",
	.add               = extended_dn_add,
	.modify            = extended_dn_modify,
};

int ldb_extended_dn_store_module_init(const char *version)
{
	LDB_MODULE_CHECK_VERSION(version);
	return ldb_register_module(&ldb_extended_dn_store_module_ops);
}