summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/modules/schema.c
blob: 1f9017976e3b11f0612a4b796dc80cd067171d19 (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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
/* 
   ldb database library

   Copyright (C) Simo Sorce  2004

     ** NOTE! The following LGPL license applies to the ldb
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
 *  Name: ldb
 *
 *  Component: ldb schema module
 *
 *  Description: add schema check functionality
 *
 *  Author: Simo Sorce
 */

#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"

struct attribute_syntax {
	const char *name;
	const char *syntax_id;
};

static struct attribute_syntax attrsyn[] = {
		{ "Object(DS-DN)", "2.5.5.1"},
		{ "String(Object-Identifier)", "2.5.5.2"},
		{ "", "2.5.5.3"},
		{ "String(Teletex)", "2.5.5.4"},
		{ "String(IA5)", "2.5.5.5"}, /* Also String(Printable) */
		{ "String(Numeric)", "2.5.5.6"},
		{ "Object(DN-Binary)", "2.5.5.7"}, /* Also Object(OR-Name) */
		{ "Boolean", "2.5.5.8"},
		{ "Integer", "2.5.5.9"}, /* Also Enumeration (3 types ?) ... */
		{ "String(Octet)", "2.5.5.10"}, /* Also Object(Replica-Link) */
		{ "String(UTC-Time)", "2.5.5.11"}, /* Also String(Generalized-Time) */
		{ "String(Unicode)", "2.5.5.12"},
		{ "Object(Presentation-Address)", "2.5.5.13"},
		{ "Object(DN-String)", "2.5.5.14"}, /* Also Object(Access-Point) */
		{ "String(NT-Sec-Desc))", "2.5.5.15"},
		{ "LargeInteger", "2.5.5.16"}, /* Also Interval ... */
		{ "String(Sid)", "2.5.5.17"}
	};

#define SCHEMA_TALLOC_CHECK(root, mem, ret) do { if (!mem) { talloc_free(root); return ret;} } while(0);

struct private_data {
	struct ldb_context *schema_db;
	const char *error_string;
};

/* close */
static int schema_close(struct ldb_module *module)
{
	return ldb_next_close(module);
}

/* search */
static int schema_search(struct ldb_module *module, const char *base,
		       enum ldb_scope scope, const char *expression,
		       const char * const *attrs, struct ldb_message ***res)
{
	return ldb_next_search(module, base, scope, expression, attrs, res); 
}

/* search_free */
static int schema_search_free(struct ldb_module *module, struct ldb_message **res)
{
	return ldb_next_search_free(module, res);
}

struct check_list {
	int check;
	char *name;
};

struct attr_list {
	int syntax;
	char *name;
};

struct objc_list {
	int aux;
	char *name;
};

struct schema_structures {
	struct check_list *cl;
	struct objc_list *ol;
	struct attr_list *must;
	struct attr_list *may;
	int num_cl;
	int num_objc;
	int num_must;
	int num_may;
};

/* add_record */
static int schema_add_record(struct ldb_module *module, const struct ldb_message *msg)
{
	struct private_data *data = (struct private_data *)module->private_data;
	struct ldb_message **srch;
	struct schema_structures *ss;
	int i, j, k, l;
	int ret;

	/* First implementation:
		Build up a list of must and mays from each objectclass
		Check all the musts are there and all the other attributes are mays
		Throw an error in case a check fail
		Free all structures and commit the change
	*/

	ss = talloc_p(module, struct schema_structures);
	if (!ss) {
		return -1;
	}

	ss->ol = NULL;
	ss->num_objc = 0;
	ss->num_cl = msg->num_elements;
	ss->cl = talloc_array_p(ss, struct check_list, ss->num_cl);
	SCHEMA_TALLOC_CHECK(ss, ss->cl, -1);
	for (i = 0, j = 0; i < msg->num_elements; i++) {
		if (strcasecmp(msg->elements[i].name, "objectclass") == 0) {
			ss->num_objc = msg->elements[i].num_values;
			ss->ol = talloc_array_p(ss, struct objc_list, ss->num_objc);
			SCHEMA_TALLOC_CHECK(ss, ss->ol, -1);
			for (k = 0; k < ss->num_objc; k++) {
				ss->ol[k].name = talloc_strndup(ss->ol, msg->elements[i].values[k].data, msg->elements[i].values[k].length);
				SCHEMA_TALLOC_CHECK(ss, ss->ol[k].name, -1);
				ss->ol[k].aux = 0;
			}
		}

		ss->cl[j].check = 0;
		ss->cl[j].name = talloc_strdup(ss->cl, msg->elements[i].name);
		SCHEMA_TALLOC_CHECK(ss, ss->cl[j].name, -1);
		j++;
	}

	/* find all other objectclasses recursively */
	ss->must = NULL;
	ss->may = NULL;
	ss->num_must = 0;
	ss->num_may = 0;
	for (i = 0; i < ss->num_objc; i++) {
		char *filter;

		filter = talloc_asprintf(ss, "lDAPDisplayName=%s", ss->ol[i].name);
		SCHEMA_TALLOC_CHECK(ss, filter, -1);
		ret = ldb_search(data->schema_db, NULL, LDB_SCOPE_SUBTREE, filter, NULL, &srch);
		if (ret == 0) {
			int ok;

			ok = 0;
			/* suppose auxiliary classess are not required */
			if (ss->ol[i].aux) {
				int d;
				ok = 1;
				ss->num_objc -= 1;
				for (d = i; d < ss->num_objc; d++) {
					ss->ol[d] = ss->ol[d + 1];
				}
				i -= 1;
			}
			if (!ok) {
				/* Schema Violation: Object Class Description Not Found */
				data->error_string = "ObjectClass not found";
				talloc_free(ss);
				return -1;
			}
			continue;
		} else {
			if (ret < 0) {
				/* Schema DB Error: Error occurred retrieving Object Class Description */
				data->error_string = "Internal error. Error retrieving schema objectclass";
				talloc_free(ss);
				return -1;
			}
			if (ret > 1) {
				/* Schema DB Error: Too Many Records */
				data->error_string = "Internal error. Too many records searching for schema objectclass";
				talloc_free(ss);
				return -1;
			}
		}

		/* Add inherited classes eliminating duplicates */
		/* fill in kust and may attribute lists */
		for (j = 0; j < (*srch)->num_elements; j++) {
			int o, is_aux, is_class;

			is_aux = 0;
			is_class = 0;
			if (strcasecmp((*srch)->elements[j].name, "systemAuxiliaryclass") == 0) {
				is_aux = 1;
				is_class = 1;
			}
			if (strcasecmp((*srch)->elements[j].name, "subClassOf") == 0) {
				is_class = 1;
			}

			if (is_class) {
				o = (*srch)->elements[j].num_values;
				ss->ol = talloc_realloc_p(ss, ss->ol, struct objc_list, ss->num_objc + o);
				SCHEMA_TALLOC_CHECK(ss, ss->ol, -1);
				for (k = 0, l = 0; k < o; k++) {
					int c, found, len;

					found = 0;
					for (c = 0; c < ss->num_objc; c++) {
						len = strlen(ss->ol[c].name);
						if (len == (*srch)->elements[j].values[k].length) {
							if (strncasecmp(ss->ol[c].name, (*srch)->elements[j].values[k].data, len) == 0) {
								found = 1;
								break;
							}
						}
					}
					if (!found) {
						ss->ol[l + ss->num_objc].name = talloc_strndup(ss->ol, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length);
						SCHEMA_TALLOC_CHECK(ss, ss->ol[l + ss->num_objc].name, -1);
						ss->ol[l + ss->num_objc].aux = is_aux;
						l++;
					}
				}
				ss->num_objc += l;
			} else {

				if (strcasecmp((*srch)->elements[j].name, "mustContain") == 0 || strcasecmp((*srch)->elements[j].name, "SystemMustContain") == 0) {
					int m;

					m = (*srch)->elements[j].num_values;

					ss->must = talloc_realloc_p(ss, ss->must, struct attr_list, ss->num_must + m);
					SCHEMA_TALLOC_CHECK(ss, ss->must, -1);
					for (k = 0, l = 0; k < m; k++) {
						int c, found, len;

						found = 0;
						for (c = 0; c < ss->num_must; c++) {
							len  = strlen(ss->must[c].name);
							if (len == (*srch)->elements[j].values[k].length) {
								if (strncasecmp(ss->must[c].name, (*srch)->elements[j].values[k].data, len) == 0) {
									found = 1;
									break;
								}
							}
						}
						if (!found) {
							ss->must[l + ss->num_must].name = talloc_strndup(ss->must, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length);
							SCHEMA_TALLOC_CHECK(ss, ss->must[l + ss->num_must].name, -1);
							l++;
						}
					}
					ss->num_must += l;
				}

				if (strcasecmp((*srch)->elements[j].name, "mayContain") == 0 || strcasecmp((*srch)->elements[j].name, "SystemMayContain") == 0) {
					int m;

					m = (*srch)->elements[j].num_values;

					ss->may = talloc_realloc_p(ss, ss->may, struct attr_list, ss->num_may + m);
					SCHEMA_TALLOC_CHECK(ss, ss->may, -1);
					for (k = 0, l = 0; k < m; k++) {
						int c, found, len;

						found = 0;
						for (c = 0; c < ss->num_may; c++) {
							len = strlen(ss->may[c].name);
							if (len == (*srch)->elements[j].values[k].length) {
								if (strncasecmp(ss->may[c].name, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length) == 0) {
									found = 1;
									break;
								}
							}
						}
						if (!found) {
							ss->may[l + ss->num_may].name = talloc_strndup(ss->may, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length);
							SCHEMA_TALLOC_CHECK(ss, ss->may[l + ss->num_may].name, -1);
							l++;
						}
					}
					ss->num_may += l;
				}
			}
		}

		ldb_search_free(data->schema_db, srch);
	}

	/* now check all musts are present */
	for (i = 0; i < ss->num_must; i++) {
		int found;

		found = 0;
		for (j = 0; j < ss->num_cl; j++) {
			if (strcasecmp(ss->must[i].name, ss->cl[j].name) == 0) {
				ss->cl[j].check = 1;
				found = 1;
				break;
			}
		}

		if ( ! found ) {
			/* TODO: set the error string */
			data->error_string = "Objectclass violation, a required attribute is missing";
			talloc_free(ss);
			return -1;
		}
	}

	/* now check all others atribs are found in mays */
	for (i = 0; i < ss->num_cl; i++) {

		if ( ! ss->cl[i].check ) {
			int found;

			found = 0;
			for (j = 0; j < ss->num_may; j++) {
				if (strcasecmp(ss->may[j].name, ss->cl[i].name) == 0) {
					ss->cl[i].check = 1;
					found = 1;
					break;
				}
			}

			if ( ! found ) {
				data->error_string = "Objectclass violation, an invalid attribute name was found";
				talloc_free(ss);
				return -1;
			}
		}
	}

	talloc_free(ss);

	return ldb_next_add_record(module, msg);
}

/* modify_record */
static int schema_modify_record(struct ldb_module *module, const struct ldb_message *msg)
{
	struct private_data *data = (struct private_data *)module->private_data;
	struct ldb_message **srch;
	struct schema_structures *ss, *ms;
	int i, j, k, l;
	int ret;

	/* First implementation:
		Retrieve the ldap entry and get the objectclasses,
		add msg contained objectclasses if any.
		Build up a list of must and mays from each objectclass
		Check all musts for the defined objectclass and it's specific
		inheritance are there.
		Check all other the attributes are mays or musts.
		Throw an error in case a check fail.
		Free all structures and commit the change.
	*/

	ss = talloc_p(module, struct schema_structures);
	if (!ss) {
		return -1;
	}

	ms = talloc_p(module, struct schema_structures);
	SCHEMA_TALLOC_CHECK(ss, ms, -1);

	ms->ol = NULL;
	ms->num_objc = 0;
	ms->num_cl = msg->num_elements;
	ms->cl = talloc_array_p(ms, struct check_list, ms->num_cl);
	SCHEMA_TALLOC_CHECK(ss, ms->cl, -1);
	for (i = 0, j = 0; i < msg->num_elements; i++) {
		if (strcasecmp(msg->elements[i].name, "objectclass") == 0) {
			ms->num_objc = msg->elements[i].num_values;
			ms->ol = talloc_array_p(ms, struct objc_list, ms->num_objc);
			SCHEMA_TALLOC_CHECK(ss, ms->ol, -1);
			for (k = 0; k < ms->num_objc; k++) {
				ms->ol[k].name = talloc_strndup(ms->ol, msg->elements[i].values[k].data, msg->elements[i].values[k].length);
				SCHEMA_TALLOC_CHECK(ss, ms->ol[k].name, -1);
				ms->ol[k].aux = 0;
			}
		}

		ms->cl[j].check = 0;
		ms->cl[j].name = talloc_strdup(ms->cl, msg->elements[i].name);
		SCHEMA_TALLOC_CHECK(ss, ms->cl[j].name, -1);
		j++;
	}

	/* find all modify objectclasses recursively if any objectclass is being added */
	ms->must = NULL;
	ms->may = NULL;
	ms->num_must = 0;
	ms->num_may = 0;
	for (i = 0; i < ms->num_objc; i++) {
		char *filter;

		filter = talloc_asprintf(ss, "lDAPDisplayName=%s", ms->ol[i].name);
		SCHEMA_TALLOC_CHECK(ss, filter, -1);
		ret = ldb_search(data->schema_db, NULL, LDB_SCOPE_SUBTREE, filter, NULL, &srch);
		if (ret == 0) {
			int ok;

			ok = 0;
			/* suppose auxiliary classess are not required */
			if (ms->ol[i].aux) {
				int d;
				ok = 1;
				ms->num_objc -= 1;
				for (d = i; d < ms->num_objc; d++) {
					ms->ol[d] = ms->ol[d + 1];
				}
				i -= 1;
			}
			if (!ok) {
				/* Schema Violation: Object Class Description Not Found */
				data->error_string = "ObjectClass not found";
				talloc_free(ss);
				return -1;
			}
			continue;
		} else {
			if (ret < 0) {
				/* Schema DB Error: Error occurred retrieving Object Class Description */
				data->error_string = "Internal error. Error retrieving schema objectclass";
				talloc_free(ss);
				return -1;
			}
			if (ret > 1) {
				/* Schema DB Error: Too Many Records */
				data->error_string = "Internal error. Too many records searching for schema objectclass";
				talloc_free(ss);
				return -1;
			}
		}

		/* Add inherited classes eliminating duplicates */
		/* fill in kust and may attribute lists */
		for (j = 0; j < (*srch)->num_elements; j++) {
			int o, is_aux, is_class;

			is_aux = 0;
			is_class = 0;
			if (strcasecmp((*srch)->elements[j].name, "systemAuxiliaryclass") == 0) {
				is_aux = 1;
				is_class = 1;
			}
			if (strcasecmp((*srch)->elements[j].name, "subClassOf") == 0) {
				is_class = 1;
			}

			if (is_class) {
				o = (*srch)->elements[j].num_values;
				ms->ol = talloc_realloc_p(ms, ms->ol, struct objc_list, ms->num_objc + o);
				SCHEMA_TALLOC_CHECK(ss, ms->ol, -1);
				for (k = 0, l = 0; k < o; k++) {
					int c, found, len;

					found = 0;
					for (c = 0; c < ms->num_objc; c++) {
						len = strlen(ms->ol[c].name);
						if (len == (*srch)->elements[j].values[k].length) {
							if (strncasecmp(ss->ol[c].name, (*srch)->elements[j].values[k].data, len) == 0) {
								found = 1;
								break;
							}
						}
					}
					if (!found) {
						ms->ol[l + ms->num_objc].name = talloc_strndup(ms->ol, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length);
						SCHEMA_TALLOC_CHECK(ss, ms->ol[l + ms->num_objc].name, -1);
						ms->ol[l + ms->num_objc].aux = is_aux;
						l++;
					}
				}
				ms->num_objc += l;
			} else {

				if (strcasecmp((*srch)->elements[j].name, "mustContain") == 0 || strcasecmp((*srch)->elements[j].name, "SystemMustContain") == 0) {
					int m;

					m = (*srch)->elements[j].num_values;

					ms->must = talloc_realloc_p(ms, ms->must, struct attr_list, ms->num_must + m);
					SCHEMA_TALLOC_CHECK(ss, ms->must, -1);
					for (k = 0, l = 0; k < m; k++) {
						int c, found, len;

						found = 0;
						for (c = 0; c < ms->num_must; c++) {
							len  = strlen(ms->must[c].name);
							if (len == (*srch)->elements[j].values[k].length) {
								if (strncasecmp(ms->must[c].name, (*srch)->elements[j].values[k].data, len) == 0) {
									found = 1;
									break;
								}
							}
						}
						if (!found) {
							ms->must[l + ms->num_must].name = talloc_strndup(ms->must, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length);
							SCHEMA_TALLOC_CHECK(ss, ms->must[l + ms->num_must].name, -1);
							l++;
						}
					}
					ms->num_must += l;
				}

				if (strcasecmp((*srch)->elements[j].name, "mayContain") == 0 || strcasecmp((*srch)->elements[j].name, "SystemMayContain") == 0) {
					int m;

					m = (*srch)->elements[j].num_values;

					ms->may = talloc_realloc_p(ms, ms->may, struct attr_list, ms->num_may + m);
					SCHEMA_TALLOC_CHECK(ss, ms->may, -1);
					for (k = 0, l = 0; k < m; k++) {
						int c, found, len;

						found = 0;
						for (c = 0; c < ms->num_may; c++) {
							len = strlen(ms->may[c].name);
							if (len == (*srch)->elements[j].values[k].length) {
								if (strncasecmp(ms->may[c].name, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length) == 0) {
									found = 1;
									break;
								}
							}
						}
						if (!found) {
							ms->may[l + ms->num_may].name = talloc_strndup(ms->may, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length);
							SCHEMA_TALLOC_CHECK(ss, ms->may[l + ms->num_may].name, -1);
							l++;
						}
					}
					ms->num_may += l;
				}
			}
		}

		ldb_search_free(data->schema_db, srch);
	}

	/* now search for the original object objectclasses */

	ss->ol = NULL;
	ss->num_objc = 0;

	/* find all other objectclasses recursively */
	{
		char *filter = talloc_asprintf(ss, "dn=%s", msg->dn);
		const char *attrs[] = {"objectClass", NULL};

		ret = ldb_search(module->ldb, NULL, LDB_SCOPE_SUBTREE, filter, attrs, &srch);
		if (ret == 1) {
			for (i = 0; i < msg->num_elements; i++) {
				ss->num_objc = (*srch)->elements[i].num_values;
				ss->ol = talloc_array_p(ss, struct objc_list, ss->num_objc);
				SCHEMA_TALLOC_CHECK(ss, ss->ol, -1);
				for (k = 0; k < ss->num_objc; k++) {
					ss->ol[k].name = talloc_strndup(ss->ol, (*srch)->elements[i].values[k].data, (*srch)->elements[i].values[k].length);
					SCHEMA_TALLOC_CHECK(ss, ss->ol[k].name, -1);
					ss->ol[k].aux = 0;
				}
			}
			ldb_search_free(module->ldb, srch);
		} else {
			ldb_search_free(module->ldb, srch);
			return -1;
		}
	}

	ss->must = NULL;
	ss->may = NULL;
	ss->num_must = 0;
	ss->num_may = 0;
	for (i = 0; i < ss->num_objc; i++) {
		char *filter;

		filter = talloc_asprintf(ss, "lDAPDisplayName=%s", ss->ol[i].name);
		SCHEMA_TALLOC_CHECK(ss, filter, -1);
		ret = ldb_search(data->schema_db, NULL, LDB_SCOPE_SUBTREE, filter, NULL, &srch);
		if (ret == 0) {
			int ok;

			ok = 0;
			/* suppose auxiliary classess are not required */
			if (ss->ol[i].aux) {
				int d;
				ok = 1;
				ss->num_objc -= 1;
				for (d = i; d < ss->num_objc; d++) {
					ss->ol[d] = ss->ol[d + 1];
				}
				i -= 1;
			}
			if (!ok) {
				/* Schema Violation: Object Class Description Not Found */
				data->error_string = "ObjectClass not found";
				talloc_free(ss);
				return -1;
			}
			continue;
		} else {
			if (ret < 0) {
				/* Schema DB Error: Error occurred retrieving Object Class Description */
				data->error_string = "Internal error. Error retrieving schema objectclass";
				talloc_free(ss);
				return -1;
			}
			if (ret > 1) {
				/* Schema DB Error: Too Many Records */
				data->error_string = "Internal error. Too many records searching for schema objectclass";
				talloc_free(ss);
				return -1;
			}
		}

		/* Add inherited classes eliminating duplicates */
		/* fill in kust and may attribute lists */
		for (j = 0; j < (*srch)->num_elements; j++) {
			int o, is_aux, is_class;

			is_aux = 0;
			is_class = 0;
			if (strcasecmp((*srch)->elements[j].name, "systemAuxiliaryclass") == 0) {
				is_aux = 1;
				is_class = 1;
			}
			if (strcasecmp((*srch)->elements[j].name, "subClassOf") == 0) {
				is_class = 1;
			}

			if (is_class) {
				o = (*srch)->elements[j].num_values;
				ss->ol = talloc_realloc_p(ss, ss->ol, struct objc_list, ss->num_objc + o);
				SCHEMA_TALLOC_CHECK(ss, ss->ol, -1);
				for (k = 0, l = 0; k < o; k++) {
					int c, found, len;

					found = 0;
					for (c = 0; c < ss->num_objc; c++) {
						len = strlen(ss->ol[c].name);
						if (len == (*srch)->elements[j].values[k].length) {
							if (strncasecmp(ss->ol[c].name, (*srch)->elements[j].values[k].data, len) == 0) {
								found = 1;
								break;
							}
						}
					}
					if (!found) {
						ss->ol[l + ss->num_objc].name = talloc_strndup(ss->ol, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length);
						SCHEMA_TALLOC_CHECK(ss, ss->ol[l + ss->num_objc].name, -1);
						ss->ol[l + ss->num_objc].aux = is_aux;
						l++;
					}
				}
				ss->num_objc += l;
			} else {

				if (strcasecmp((*srch)->elements[j].name, "mustContain") == 0 || strcasecmp((*srch)->elements[j].name, "SystemMustContain") == 0) {
					int m;

					m = (*srch)->elements[j].num_values;

					ss->must = talloc_realloc_p(ss, ss->must, struct attr_list, ss->num_must + m);
					SCHEMA_TALLOC_CHECK(ss, ss->must, -1);
					for (k = 0, l = 0; k < m; k++) {
						int c, found, len;

						found = 0;
						for (c = 0; c < ss->num_must; c++) {
							len  = strlen(ss->must[c].name);
							if (len == (*srch)->elements[j].values[k].length) {
								if (strncasecmp(ss->must[c].name, (*srch)->elements[j].values[k].data, len) == 0) {
									found = 1;
									break;
								}
							}
						}
						if (!found) {
							ss->must[l + ss->num_must].name = talloc_strndup(ss->must, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length);
							SCHEMA_TALLOC_CHECK(ss, ss->must[l + ss->num_must].name, -1);
							l++;
						}
					}
					ss->num_must += l;
				}

				if (strcasecmp((*srch)->elements[j].name, "mayContain") == 0 || strcasecmp((*srch)->elements[j].name, "SystemMayContain") == 0) {
					int m;

					m = (*srch)->elements[j].num_values;

					ss->may = talloc_realloc_p(ss, ss->may, struct attr_list, ss->num_may + m);
					SCHEMA_TALLOC_CHECK(ss, ss->may, -1);
					for (k = 0, l = 0; k < m; k++) {
						int c, found, len;

						found = 0;
						for (c = 0; c < ss->num_may; c++) {
							len = strlen(ss->may[c].name);
							if (len == (*srch)->elements[j].values[k].length) {
								if (strncasecmp(ss->may[c].name, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length) == 0) {
									found = 1;
									break;
								}
							}
						}
						if (!found) {
							ss->may[l + ss->num_may].name = talloc_strndup(ss->may, (*srch)->elements[j].values[k].data, (*srch)->elements[j].values[k].length);
							SCHEMA_TALLOC_CHECK(ss, ss->may[l + ss->num_may].name, -1);
							l++;
						}
					}
					ss->num_may += l;
				}
			}
		}

		ldb_search_free(data->schema_db, srch);
	}

	/* now check all entries are present either as musts or mays of curent objectclasses */
	/* do not return errors there may be attirbutes defined in new objectclasses */
	/* just mark them as being proved valid attribs */
	for (i = 0; i < ms->num_cl; i++) {
		int found;

		found = 0;
		for (j = 0; j < ss->num_may; j++) {
			if (strcasecmp(ss->may[j].name, ms->cl[i].name) == 0) {
				ms->cl[i].check = 1;
				found = 1;
				break;
			}
		}
		if ( ! found) {
			for (j = 0; j < ss->num_must; j++) {
				if (strcasecmp(ss->must[j].name, ms->cl[i].name) == 0) {
					ms->cl[i].check = 1;
					break;
				}
			}
		}
	}

	/* now check all new objectclasses musts are present */
	for (i = 0; i < ms->num_must; i++) {
		int found;

		found = 0;
		for (j = 0; j < ms->num_cl; j++) {
			if (strcasecmp(ms->must[i].name, ms->cl[j].name) == 0) {
				ms->cl[j].check = 1;
				found = 1;
				break;
			}
		}

		if ( ! found ) {
			/* TODO: set the error string */
			data->error_string = "Objectclass violation, a required attribute is missing";
			talloc_free(ss);
			return -1;
		}
	}

	/* now check all others atribs are found in mays */
	for (i = 0; i < ms->num_cl; i++) {

		if ( ! ms->cl[i].check ) {
			int found;

			found = 0;
			for (j = 0; j < ms->num_may; j++) {
				if (strcasecmp(ms->may[j].name, ms->cl[i].name) == 0) {
					ms->cl[i].check = 1;
					found = 1;
					break;
				}
			}

			if ( ! found ) {
				data->error_string = "Objectclass violation, an invalid attribute name was found";
				talloc_free(ss);
				return -1;
			}
		}
	}

	talloc_free(ss);

	return ldb_next_modify_record(module, msg);
}

/* delete_record */
static int schema_delete_record(struct ldb_module *module, const char *dn)
{
	struct private_data *data = (struct private_data *)module->private_data;
	return ldb_next_delete_record(module, dn);
}

/* rename_record */
static int schema_rename_record(struct ldb_module *module, const char *olddn, const char *newdn)
{
	return ldb_next_rename_record(module, olddn, newdn);
}

static int schema_named_lock(struct ldb_module *module, const char *name) {
	return ldb_next_named_lock(module, name);
}

static int schema_named_unlock(struct ldb_module *module, const char *name) {
	return ldb_next_named_unlock(module, name);
}

/* return extended error information */
static const char *schema_errstring(struct ldb_module *module)
{
	struct private_data *data = (struct private_data *)module->private_data;

	if (data->error_string) {
		const char *error;

		error = data->error_string;
		data->error_string = NULL;
		return error;
	}

	return ldb_next_errstring(module);
}

static const struct ldb_module_ops schema_ops = {
	"schema",
	schema_close, 
	schema_search,
	schema_search_free,
	schema_add_record,
	schema_modify_record,
	schema_delete_record,
	schema_rename_record,
	schema_named_lock,
	schema_named_unlock,
	schema_errstring,
};

#define SCHEMA_PREFIX		"schema:"
#define SCHEMA_PREFIX_LEN	7

#ifdef HAVE_DLOPEN_DISABLED
struct ldb_module *init_module(struct ldb_context *ldb, const char *options[])
#else
struct ldb_module *schema_module_init(struct ldb_context *ldb, const char *options[])
#endif
{
	struct ldb_module *ctx;
	struct private_data *data;
	char *db_url = NULL;
	int i;

	ctx = talloc_p(ldb, struct ldb_module);
	if (!ctx) {
		return NULL;
	}

	if (options) {
		for (i = 0; options[i] != NULL; i++) {
			if (strncmp(options[i], SCHEMA_PREFIX, SCHEMA_PREFIX_LEN) == 0) {
				db_url = talloc_strdup(ctx, &options[i][SCHEMA_PREFIX_LEN]);
				SCHEMA_TALLOC_CHECK(ctx, db_url, NULL);
			}
		}
	}

	if (!db_url) { /* search if it is defined in the calling ldb */
		int ret;
		const char * attrs[] = { "@SCHEMADB", NULL };
		struct ldb_message **msgs;

		ret = ldb_search(ldb, "", LDB_SCOPE_BASE, "dn=@MODULES", (const char * const *)attrs, &msgs);
		if (ret == 0) {
			ldb_debug(ldb, LDB_DEBUG_TRACE, "Schema DB not found\n");
			ldb_search_free(ldb, msgs);
			return NULL;
		} else {
			if (ret < 0) {
				ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for schema db, bailing out!\n", ldb_errstring(ldb));
				ldb_search_free(ldb, msgs);
				return NULL;
			}
			if (ret > 1) {
				ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found, bailing out\n");
				ldb_search_free(ldb, msgs);
				return NULL;
			}

			db_url = talloc_strndup(ctx, msgs[0]->elements[0].values[0].data, msgs[0]->elements[0].values[0].length);
			SCHEMA_TALLOC_CHECK(ctx, db_url, NULL);
		}

		ldb_search_free(ldb, msgs);
	}

	data = talloc_p(ctx, struct private_data);
	SCHEMA_TALLOC_CHECK(ctx, data, NULL);

	data->schema_db = ldb_connect(db_url, 0, NULL); 
	SCHEMA_TALLOC_CHECK(ctx, data->schema_db, NULL);

	data->error_string = NULL;
	ctx->private_data = data;
	ctx->ldb = ldb;
	ctx->prev = ctx->next = NULL;
	ctx->ops = &schema_ops;

	return ctx;
}