schema_init.c 91.8 KB
Newer Older
1
2
3
/* schema_init.c - init builtin schema */
/* $OpenLDAP$ */
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5
6
7
8
9
10
11
12
13
14
15
16
17
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

#include "portable.h"

#include <stdio.h>

#include <ac/ctype.h>
#include <ac/string.h>
#include <ac/socket.h>

#include "slap.h"
#include "ldap_pvt.h"
18
19
#include "lutil_md5.h"

20
21
22
23
24
25
26
/* We should replace MD5 with a faster hash */
#define HASH_BYTES				LUTIL_MD5_BYTES
#define HASH_CONTEXT			lutil_MD5_CTX
#define HASH_Init(c)			lutil_MD5Init(c)
#define HASH_Update(c,buf,len)	lutil_MD5Update(c,buf,len)
#define HASH_Final(d,c)			lutil_MD5Final(d,c)

27
/* recycled validatation routines */
28
#define berValidate						blobValidate
29

30
/* recycled normalization routines */
31
32
33
#define faxNumberNormalize				numericStringNormalize
#define phoneNumberNormalize			numericStringNormalize
#define telexNumberNormalize			numericStringNormalize
34
35
36
37

/* unimplemented pretters */
#define dnPretty						NULL
#define integerPretty					NULL
38
39

/* recycled matching routines */
40
#define bitStringMatch					octetStringMatch
41
#define integerMatch					caseIgnoreIA5Match
42
43
44
#define numericStringMatch				caseIgnoreMatch
#define objectIdentifierMatch			numericStringMatch
#define telephoneNumberMatch			numericStringMatch
45
#define telephoneNumberSubstringsMatch	caseIgnoreIA5SubstringsMatch
46
47
#define generalizedTimeMatch			numericStringMatch
#define generalizedTimeOrderingMatch	numericStringMatch
48
#define uniqueMemberMatch				dnMatch
49

50
51
52
53
54
55
/* approx matching rules */
#define directoryStringApproxMatchOID	"1.3.6.1.4.1.4203.666.4.4"
#define directoryStringApproxMatch		NULL
#define IA5StringApproxMatchOID			"1.3.6.1.4.1.4203.666.4.5"
#define IA5StringApproxMatch			NULL

56
57
58
59
/* orderring matching rules */
#define caseIgnoreOrderingMatch			caseIgnoreMatch
#define caseExactOrderingMatch			caseExactMatch

60
/* unimplemented matching routines */
61
62
63
64
65
66
67
68
#define caseIgnoreListMatch				NULL
#define caseIgnoreListSubstringsMatch	NULL
#define presentationAddressMatch		NULL
#define protocolInformationMatch		NULL
#define integerFirstComponentMatch		NULL

#define OpenLDAPaciMatch				NULL
#define authPasswordMatch				NULL
69
70

/* recycled indexing/filtering routines */
71
72
#define dnIndexer						caseIgnoreIndexer
#define dnFilter						caseIgnoreFilter
73
74
#define integerIndexer					caseIgnoreIA5Indexer
#define integerFilter					caseIgnoreIA5Filter
75

76
77
78
79
static char *strcasechr( const char *str, int c )
{
	char *lower = strchr( str, TOLOWER(c) );
	char *upper = strchr( str, TOUPPER(c) );
80

81
82
83
84
85
86
87
88
	if( lower && upper ) {
		return lower < upper ? lower : upper;
	} else if ( lower ) {
		return lower;
	} else {
		return upper;
	}
}
89

90
91
92
static int
octetStringMatch(
	int *matchp,
93
	slap_mask_t flags,
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
	int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;

	if( match == 0 ) {
		match = memcmp( value->bv_val,
			((struct berval *) assertedValue)->bv_val,
			value->bv_len );
	}

	*matchp = match;
	return LDAP_SUCCESS;
}

/* Index generation function */
int octetStringIndexer(
113
114
	slap_mask_t use,
	slap_mask_t flags,
115
116
117
118
119
120
121
122
123
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	struct berval **values,
	struct berval ***keysp )
{
	int i;
	size_t slen, mlen;
	struct berval **keys;
124
125
	HASH_CONTEXT   HASHcontext;
	unsigned char   HASHdigest[HASH_BYTES];
126
	struct berval digest;
127
128
	digest.bv_val = HASHdigest;
	digest.bv_len = sizeof(HASHdigest);
129

130
131
132
	/* we should have at least one value at this point */
	assert( values != NULL && values[0] != NULL );

133
134
135
136
137
138
139
140
141
142
	for( i=0; values[i] != NULL; i++ ) {
		/* just count them */
	}

	keys = ch_malloc( sizeof( struct berval * ) * (i+1) );

	slen = strlen( syntax->ssyn_oid );
	mlen = strlen( mr->smr_oid );

	for( i=0; values[i] != NULL; i++ ) {
143
		HASH_Init( &HASHcontext );
144
		if( prefix != NULL && prefix->bv_len > 0 ) {
145
			HASH_Update( &HASHcontext,
146
147
				prefix->bv_val, prefix->bv_len );
		}
148
		HASH_Update( &HASHcontext,
149
			syntax->ssyn_oid, slen );
150
		HASH_Update( &HASHcontext,
151
			mr->smr_oid, mlen );
152
		HASH_Update( &HASHcontext,
153
			values[i]->bv_val, values[i]->bv_len );
154
		HASH_Final( HASHdigest, &HASHcontext );
155
156
157
158
159
160
161
162
163
164
165
166
167

		keys[i] = ber_bvdup( &digest );
	}

	keys[i] = NULL;

	*keysp = keys;

	return LDAP_SUCCESS;
}

/* Index generation function */
int octetStringFilter(
168
169
	slap_mask_t use,
	slap_mask_t flags,
170
171
172
173
174
175
176
177
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	void * assertValue,
	struct berval ***keysp )
{
	size_t slen, mlen;
	struct berval **keys;
178
179
	HASH_CONTEXT   HASHcontext;
	unsigned char   HASHdigest[HASH_BYTES];
180
181
	struct berval *value = (struct berval *) assertValue;
	struct berval digest;
182
183
	digest.bv_val = HASHdigest;
	digest.bv_len = sizeof(HASHdigest);
184
185
186
187
188
189

	slen = strlen( syntax->ssyn_oid );
	mlen = strlen( mr->smr_oid );

	keys = ch_malloc( sizeof( struct berval * ) * 2 );

190
	HASH_Init( &HASHcontext );
191
	if( prefix != NULL && prefix->bv_len > 0 ) {
192
		HASH_Update( &HASHcontext,
193
194
			prefix->bv_val, prefix->bv_len );
	}
195
	HASH_Update( &HASHcontext,
196
		syntax->ssyn_oid, slen );
197
	HASH_Update( &HASHcontext,
198
		mr->smr_oid, mlen );
199
	HASH_Update( &HASHcontext,
200
		value->bv_val, value->bv_len );
201
	HASH_Final( HASHdigest, &HASHcontext );
202
203
204
205
206
207
208
209

	keys[0] = ber_bvdup( &digest );
	keys[1] = NULL;

	*keysp = keys;

	return LDAP_SUCCESS;
}
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
static int
dnValidate(
	Syntax *syntax,
	struct berval *in )
{
	int rc;
	char *dn;

	if( in->bv_len == 0 ) return LDAP_SUCCESS;

	dn = ch_strdup( in->bv_val );

	rc = dn_validate( dn ) == NULL
		? LDAP_INVALID_SYNTAX : LDAP_SUCCESS;

	ch_free( dn );
	return rc;
}

static int
dnNormalize(
	Syntax *syntax,
	struct berval *val,
	struct berval **normalized )
{
	struct berval *out = ber_bvdup( val );

	if( out->bv_len != 0 ) {
		char *dn;
#ifdef USE_DN_NORMALIZE
		dn = dn_normalize( out->bv_val );
#else
		dn = dn_validate( out->bv_val );
#endif

		if( dn == NULL ) {
			ber_bvfree( out );
			return LDAP_INVALID_SYNTAX;
		}

		out->bv_val = dn;
		out->bv_len = strlen( dn );
	}

	*normalized = out;
	return LDAP_SUCCESS;
}

static int
dnMatch(
261
	int *matchp,
262
	slap_mask_t flags,
263
264
265
266
267
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
268
	int match;
269
270
	struct berval *asserted = (struct berval *) assertedValue;
	
271
272
273
	match = value->bv_len - asserted->bv_len;

	if( match == 0 ) {
274
#ifdef USE_DN_NORMALIZE
275
		match = strcmp( value->bv_val, asserted->bv_val );
276
#else
277
		match = strcasecmp( value->bv_val, asserted->bv_val );
278
#endif
279
280
281
282
283
284
285
	}

	Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
	    match, value->bv_val, asserted->bv_val );

	*matchp = match;
	return LDAP_SUCCESS;
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
static int
nameUIDValidate(
	Syntax *syntax,
	struct berval *in )
{
	int rc;
	struct berval *dn;

	if( in->bv_len == 0 ) return LDAP_SUCCESS;

	dn = ber_bvdup( in );

	if( dn->bv_val[dn->bv_len-1] == '\'' ) {
		/* assume presence of optional UID */
		ber_len_t i;

		for(i=dn->bv_len-2; i>2; i--) {
			if( dn->bv_val[i] != '0' &&	dn->bv_val[i] != '1' ) {
				break;
			}
		}
		if( dn->bv_val[i] != '\'' ) {
			return LDAP_INVALID_SYNTAX;
		}
		if( dn->bv_val[i-1] != 'B' ) {
			return LDAP_INVALID_SYNTAX;
		}
		if( dn->bv_val[i-2] != '#' ) {
			return LDAP_INVALID_SYNTAX;
		}

		/* trim the UID to allow use of dn_validate */
		dn->bv_val[i-2] = '\0';
	}

	rc = dn_validate( dn->bv_val ) == NULL
		? LDAP_INVALID_SYNTAX : LDAP_SUCCESS;

	ber_bvfree( dn );
	return rc;
}

static int
nameUIDNormalize(
	Syntax *syntax,
	struct berval *val,
	struct berval **normalized )
{
	struct berval *out = ber_bvdup( val );

	if( out->bv_len != 0 ) {
		char *dn;
		ber_len_t dnlen;
		char *uid = NULL;
		ber_len_t uidlen = 0;

		if( out->bv_val[out->bv_len-1] == '\'' ) {
			/* assume presence of optional UID */
			uid = strrchr( out->bv_val, '#' );

			if( uid == NULL ) {
				ber_bvfree( out );
				return LDAP_INVALID_SYNTAX;
			}

			uidlen = out->bv_len - (out->bv_val - uid);
			/* temporarily trim the UID */
			*uid = '\0';
		}

#ifdef USE_DN_NORMALIZE
		dn = dn_normalize( out->bv_val );
#else
		dn = dn_validate( out->bv_val );
#endif

		if( dn == NULL ) {
			ber_bvfree( out );
			return LDAP_INVALID_SYNTAX;
		}

		dnlen = strlen(dn);

		if( uidlen ) {
			/* restore the separator */
			*uid = '#';
			/* shift the UID */
			SAFEMEMCPY( &dn[dnlen], uid, uidlen );
		}

		out->bv_val = dn;
		out->bv_len = dnlen + uidlen;
	}

	*normalized = out;
	return LDAP_SUCCESS;
}

386
387
388
389
390
391
392
393
394
static int
inValidate(
	Syntax *syntax,
	struct berval *in )
{
	/* any value allowed */
	return LDAP_OTHER;
}

395
static int
396
blobValidate(
397
398
399
400
	Syntax *syntax,
	struct berval *in )
{
	/* any value allowed */
401
	return LDAP_SUCCESS;
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
static int
bitStringValidate(
	Syntax *syntax,
	struct berval *in )
{
	ber_len_t i;

	/* very unforgiving validation, requires no normalization
	 * before simplistic matching
	 */
	if( in->bv_len < 3 ) {
		return LDAP_INVALID_SYNTAX;
	}
	if( in->bv_val[0] != 'B' ||
		in->bv_val[1] != '\'' ||
		in->bv_val[in->bv_len-1] != '\'' )
	{
		return LDAP_INVALID_SYNTAX;
	}

	for( i=in->bv_len-2; i>1; i-- ) {
		if( in->bv_val[i] != '0' && in->bv_val[i] != '1' ) {
			return LDAP_INVALID_SYNTAX;
		}
	}

	return LDAP_SUCCESS;
}

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
/*
 * Handling boolean syntax and matching is quite rigid.
 * A more flexible approach would be to allow a variety
 * of strings to be normalized and prettied into TRUE
 * and FALSE.
 */
static int
booleanValidate(
	Syntax *syntax,
	struct berval *in )
{
	/* very unforgiving validation, requires no normalization
	 * before simplistic matching
	 */

	if( in->bv_len == 4 ) {
		if( !memcmp( in->bv_val, "TRUE", 4 ) ) {
			return LDAP_SUCCESS;
		}
	} else if( in->bv_len == 5 ) {
		if( !memcmp( in->bv_val, "FALSE", 5 ) ) {
			return LDAP_SUCCESS;
		}
	}

	return LDAP_INVALID_SYNTAX;
}

static int
booleanMatch(
	int *matchp,
464
	slap_mask_t flags,
465
466
467
468
469
470
471
472
473
474
475
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
	/* simplistic matching allowed by rigid validation */
	struct berval *asserted = (struct berval *) assertedValue;
	*matchp = value->bv_len != asserted->bv_len;
	return LDAP_SUCCESS;
}

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
#if 0
static int
UTF8casecmp(
	struct berval *right,
	struct berval *left )
{
	ber_len_t r, l;
	int rlen, llen;
	ldap_unicode_t ru, lu;
	ldap_unicode_t ruu, luu;

	for( r=0, l=0;
		r < right->bv_len && l < left->bv_len;
		r+=rlen, l+=llen )
	{
		/*
		 * XXYYZ: we convert to ucs4 even though -llunicode
		 * expects ucs2 in an unsigned long
		 */
		ru = ldap_utf8_to_ucs4( &right->bv_val[r] );
		if( ru == LDAP_UCS4_INVALID ) {
			return 1;
		}

		lu = ldap_utf8_to_ucs4( &left->bv_val[l] );
		if( lu == LDAP_UCS4_INVALID ) {
			return -1;
		}

		ruu = uctoupper( ru );
		luu = uctoupper( lu );

		if( ruu > luu ) {
			return 1;
		} else if( luu > ruu ) {
			return -1;
		}

		rlen = LDAP_UTF8_CHARLEN( &right->bv_val[r] );
		llen = LDAP_UTF8_CHARLEN( &left->bv_val[l] );
	}

	if( r < right->bv_len ) {
		/* less left */
		return -1;
	}

	if( l < left->bv_len ) {
		/* less right */
		return 1;
	}

	return 0;
}
#endif

532
533
534
535
536
537
538
539
540
static int
UTF8StringValidate(
	Syntax *syntax,
	struct berval *in )
{
	ber_len_t count;
	int len;
	unsigned char *u = in->bv_val;

541
542
	if( !in->bv_len ) return LDAP_INVALID_SYNTAX;

543
	for( count = in->bv_len; count > 0; count-=len, u+=len ) {
544
545
546
547
		/* get the length indicated by the first byte */
		len = LDAP_UTF8_CHARLEN( u );

		/* should not be zero */
548
		if( len == 0 ) return LDAP_INVALID_SYNTAX;
549
550
551

		/* make sure len corresponds with the offset
			to the next character */
552
		if( LDAP_UTF8_OFFSET( u ) != len ) return LDAP_INVALID_SYNTAX;
553
554
	}

555
	if( count != 0 ) return LDAP_INVALID_SYNTAX;
556

557
	return LDAP_SUCCESS;
558
559
560
561
562
563
564
565
566
}

static int
UTF8StringNormalize(
	Syntax *syntax,
	struct berval *val,
	struct berval **normalized )
{
	struct berval *newval;
567
	char *p, *q, *s;
568

569
	newval = ch_malloc( sizeof( struct berval ) );
570

571
	p = val->bv_val;
572

573
574
575
	/* Ignore initial whitespace */
	while ( ldap_utf8_isspace( p ) ) {
		LDAP_UTF8_INCR( p );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
576
	}
577

578
579
	if( *p == '\0' ) {
		ch_free( newval );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
580
581
		return LDAP_INVALID_SYNTAX;
	}
582

583
584
585
	newval->bv_val = ch_strdup( p );
	p = q = newval->bv_val;
	s = NULL;
586

587
588
	while ( *p ) {
		int len;
589

590
591
592
593
594
		if ( ldap_utf8_isspace( p ) ) {
			len = LDAP_UTF8_COPY(q,p);
			s=q;
			p+=len;
			q+=len;
595

596
597
598
599
			/* Ignore the extra whitespace */
			while ( ldap_utf8_isspace( p ) ) {
				LDAP_UTF8_INCR( p );
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
600
		} else {
601
602
603
604
			len = LDAP_UTF8_COPY(q,p);
			s=NULL;
			p+=len;
			q+=len;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
605
		}
606
607
608
609
	}

	assert( *newval->bv_val );
	assert( newval->bv_val < p );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
610
	assert( q <= p );
611

612
613
614
615
616
617
618
619
620
621
622
	/* cannot start with a space */
	assert( !ldap_utf8_isspace(newval->bv_val) );

	/*
	 * If the string ended in space, backup the pointer one
	 * position.  One is enough because the above loop collapsed
	 * all whitespace to a single space.
	 */

	if ( s != NULL ) {
		q = s;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
623
	}
624

625
626
627
628
629
630
631
632
	/* cannot end with a space */
	assert( !ldap_utf8_isspace( LDAP_UTF8_PREV(q) ) );

	/* null terminate */
	*q = '\0';

	newval->bv_len = q - newval->bv_val;
	*normalized = newval;
633

634
	return LDAP_SUCCESS;
635
636
}

637
static int
638
caseExactMatch(
639
	int *matchp,
640
	slap_mask_t flags,
641
642
643
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
644
	void *assertedValue )
645
{
646
647
648
	int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;

	if( match == 0 ) {
649
650
651
		match = strncmp( value->bv_val,
			((struct berval *) assertedValue)->bv_val,
			value->bv_len );
652
653
654
	}

	*matchp = match;
655
	return LDAP_SUCCESS;
656
657
}

658
static int
659
caseExactSubstringsMatch(
660
	int *matchp,
661
	slap_mask_t flags,
662
663
664
665
666
667
668
669
670
671
672
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
	int match = 0;
	SubstringsAssertion *sub = assertedValue;
	struct berval left = *value;
	int i;
	ber_len_t inlen=0;

673
	/* Add up asserted input length */
674
675
676
677
	if( sub->sa_initial ) {
		inlen += sub->sa_initial->bv_len;
	}
	if( sub->sa_any ) {
678
679
		for(i=0; sub->sa_any[i] != NULL; i++) {
			inlen += sub->sa_any[i]->bv_len;
680
681
682
683
684
685
686
		}
	}
	if( sub->sa_final ) {
		inlen += sub->sa_final->bv_len;
	}

	if( sub->sa_initial ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
687
688
689
690
691
		if( inlen > left.bv_len ) {
			match = 1;
			goto done;
		}

692
693
		match = strncmp( sub->sa_initial->bv_val, left.bv_val,
			sub->sa_initial->bv_len );
694
695
696
697
698
699
700

		if( match != 0 ) {
			goto done;
		}

		left.bv_val += sub->sa_initial->bv_len;
		left.bv_len -= sub->sa_initial->bv_len;
701
		inlen -= sub->sa_initial->bv_len;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
702
	}
703

Kurt Zeilenga's avatar
Kurt Zeilenga committed
704
	if( sub->sa_final ) {
705
706
707
708
		if( inlen > left.bv_len ) {
			match = 1;
			goto done;
		}
709

710
711
712
		match = strncmp( sub->sa_final->bv_val,
			&left.bv_val[left.bv_len - sub->sa_final->bv_len],
			sub->sa_final->bv_len );
713
714
715
716
717
718

		if( match != 0 ) {
			goto done;
		}

		left.bv_len -= sub->sa_final->bv_len;
719
		inlen -= sub->sa_final->bv_len;
720
721
722
	}

	if( sub->sa_any ) {
723
724
725
726
727
		for(i=0; sub->sa_any[i]; i++) {
			ber_len_t idx;
			char *p;

retry:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
728
729
730
731
732
733
			if( inlen > left.bv_len ) {
				/* not enough length */
				match = 1;
				goto done;
			}

734
735
736
737
			if( sub->sa_any[i]->bv_len == 0 ) {
				continue;
			}

738
			p = strchr( left.bv_val, *sub->sa_any[i]->bv_val );
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761

			if( p == NULL ) {
				match = 1;
				goto done;
			}

			idx = p - left.bv_val;
			assert( idx < left.bv_len );

			if( idx >= left.bv_len ) {
				/* this shouldn't happen */
				return LDAP_OTHER;
			}

			left.bv_val = p;
			left.bv_len -= idx;

			if( sub->sa_any[i]->bv_len > left.bv_len ) {
				/* not enough left */
				match = 1;
				goto done;
			}

762
763
764
			match = strncmp( left.bv_val,
				sub->sa_any[i]->bv_val,
				sub->sa_any[i]->bv_len );
765
766

			if( match != 0 ) {
767
768
				left.bv_val++;
				left.bv_len--;
769
770
771
772
773
				goto retry;
			}

			left.bv_val += sub->sa_any[i]->bv_len;
			left.bv_len -= sub->sa_any[i]->bv_len;
774
			inlen -= sub->sa_any[i]->bv_len;
775
		}
776
777
778
779
780
781
782
	}

done:
	*matchp = match;
	return LDAP_SUCCESS;
}

783
/* Index generation function */
784
int caseExactIndexer(
785
786
	slap_mask_t use,
	slap_mask_t flags,
787
788
789
790
791
792
793
794
795
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	struct berval **values,
	struct berval ***keysp )
{
	int i;
	size_t slen, mlen;
	struct berval **keys;
796
797
	HASH_CONTEXT   HASHcontext;
	unsigned char   HASHdigest[HASH_BYTES];
798
	struct berval digest;
799
800
	digest.bv_val = HASHdigest;
	digest.bv_len = sizeof(HASHdigest);
801

802
803
804
	/* we should have at least one value at this point */
	assert( values != NULL && values[0] != NULL );

805
806
807
808
809
810
811
812
813
814
815
816
	for( i=0; values[i] != NULL; i++ ) {
		/* just count them */
	}

	keys = ch_malloc( sizeof( struct berval * ) * (i+1) );

	slen = strlen( syntax->ssyn_oid );
	mlen = strlen( mr->smr_oid );

	for( i=0; values[i] != NULL; i++ ) {
		struct berval *value = values[i];

817
		HASH_Init( &HASHcontext );
818
		if( prefix != NULL && prefix->bv_len > 0 ) {
819
			HASH_Update( &HASHcontext,
820
821
				prefix->bv_val, prefix->bv_len );
		}
822
		HASH_Update( &HASHcontext,
823
			syntax->ssyn_oid, slen );
824
		HASH_Update( &HASHcontext,
825
			mr->smr_oid, mlen );
826
		HASH_Update( &HASHcontext,
827
			value->bv_val, value->bv_len );
828
		HASH_Final( HASHdigest, &HASHcontext );
829
830
831
832
833
834
835
836
837
838

		keys[i] = ber_bvdup( &digest );
	}

	keys[i] = NULL;
	*keysp = keys;
	return LDAP_SUCCESS;
}

/* Index generation function */
839
int caseExactFilter(
840
841
	slap_mask_t use,
	slap_mask_t flags,
842
843
844
845
846
847
848
849
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	void * assertValue,
	struct berval ***keysp )
{
	size_t slen, mlen;
	struct berval **keys;
850
851
	HASH_CONTEXT   HASHcontext;
	unsigned char   HASHdigest[HASH_BYTES];
852
853
	struct berval *value;
	struct berval digest;
854
855
	digest.bv_val = HASHdigest;
	digest.bv_len = sizeof(HASHdigest);
856
857
858
859
860
861
862
863

	slen = strlen( syntax->ssyn_oid );
	mlen = strlen( mr->smr_oid );

	value = (struct berval *) assertValue;

	keys = ch_malloc( sizeof( struct berval * ) * 2 );

864
	HASH_Init( &HASHcontext );
865
	if( prefix != NULL && prefix->bv_len > 0 ) {
866
		HASH_Update( &HASHcontext,
867
868
			prefix->bv_val, prefix->bv_len );
	}
869
	HASH_Update( &HASHcontext,
870
		syntax->ssyn_oid, slen );
871
	HASH_Update( &HASHcontext,
872
		mr->smr_oid, mlen );
873
	HASH_Update( &HASHcontext,
874
		value->bv_val, value->bv_len );
875
	HASH_Final( HASHdigest, &HASHcontext );
876
877
878
879
880
881
882

	keys[0] = ber_bvdup( &digest );
	keys[1] = NULL;

	*keysp = keys;
	return LDAP_SUCCESS;
}
883

884
/* Substrings Index generation function */
885
int caseExactSubstringsIndexer(
886
887
	slap_mask_t use,
	slap_mask_t flags,
888
889
890
891
892
893
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	struct berval **values,
	struct berval ***keysp )
{
894
	ber_len_t i, nkeys;
895
896
	size_t slen, mlen;
	struct berval **keys;
897
898
	HASH_CONTEXT   HASHcontext;
	unsigned char   HASHdigest[HASH_BYTES];
899
	struct berval digest;
900
901
	digest.bv_val = HASHdigest;
	digest.bv_len = sizeof(HASHdigest);
902

903
904
905
	/* we should have at least one value at this point */
	assert( values != NULL && values[0] != NULL );

906
907
908
	nkeys=0;
	for( i=0; values[i] != NULL; i++ ) {
		/* count number of indices to generate */
909
		if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) {
910
911
912
			continue;
		}

913
		if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
914
			if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
915
				nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
916
					( SLAP_INDEX_SUBSTR_MINLEN - 1);
917
			} else {
918
				nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
919
920
921
			}
		}

922
		if( flags & SLAP_INDEX_SUBSTR_ANY ) {
923
924
			if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
				nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
925
926
927
			}
		}

928
		if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
929
			if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
930
				nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
931
					( SLAP_INDEX_SUBSTR_MINLEN - 1);
932
			} else {
933
				nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
934
			}
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
		}
	}

	if( nkeys == 0 ) {
		/* no keys to generate */
		*keysp = NULL;
		return LDAP_SUCCESS;
	}

	keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );

	slen = strlen( syntax->ssyn_oid );
	mlen = strlen( mr->smr_oid );

	nkeys=0;
	for( i=0; values[i] != NULL; i++ ) {
951
		ber_len_t j,max;
952
953
		struct berval *value;

954
		value = values[i];
955
		if( value->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) continue;
956

957
		if( ( flags & SLAP_INDEX_SUBSTR_ANY ) &&
958
			( value->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) )
959
960
		{
			char pre = SLAP_INDEX_SUBSTR_PREFIX;
961
			max = value->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1);
962

963
			for( j=0; j<max; j++ ) {
964
				HASH_Init( &HASHcontext );
965
				if( prefix != NULL && prefix->bv_len > 0 ) {
966
					HASH_Update( &HASHcontext,
967
968
						prefix->bv_val, prefix->bv_len );
				}
969

970
				HASH_Update( &HASHcontext,
971
					&pre, sizeof( pre ) );
972
				HASH_Update( &HASHcontext,
973
					syntax->ssyn_oid, slen );
974
				HASH_Update( &HASHcontext,
975
					mr->smr_oid, mlen );
976
				HASH_Update( &HASHcontext,
977
978
					&value->bv_val[j],
					SLAP_INDEX_SUBSTR_MAXLEN );
979
				HASH_Final( HASHdigest, &HASHcontext );
980
981
982
983
984

				keys[nkeys++] = ber_bvdup( &digest );
			}
		}

985
986
		max = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
			? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
987

988
		for( j=SLAP_INDEX_SUBSTR_MINLEN; j<=max; j++ ) {
989
990
			char pre;

991
			if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
992
				pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
993
				HASH_Init( &HASHcontext );
994
				if( prefix != NULL && prefix->bv_len > 0 ) {
995
					HASH_Update( &HASHcontext,
996
997
						prefix->bv_val, prefix->bv_len );
				}
998
				HASH_Update( &HASHcontext,
999
					&pre, sizeof( pre ) );
1000
				HASH_Update( &HASHcontext,
1001
					syntax->ssyn_oid, slen );
1002
				HASH_Update( &HASHcontext,
1003
					mr->smr_oid, mlen );
1004
				HASH_Update( &HASHcontext,
1005
					value->bv_val, j );
1006
				HASH_Final( HASHdigest, &HASHcontext );
1007
1008
1009
1010

				keys[nkeys++] = ber_bvdup( &digest );
			}

1011
			if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
1012
				pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
1013
				HASH_Init( &HASHcontext );
1014
				if( prefix != NULL && prefix->bv_len > 0 ) {
1015
					HASH_Update( &HASHcontext,
1016
1017
						prefix->bv_val, prefix->bv_len );
				}
1018
				HASH_Update( &HASHcontext,
1019
					&pre, sizeof( pre ) );
1020
				HASH_Update( &HASHcontext,
1021
					syntax->ssyn_oid, slen );
1022
				HASH_Update( &HASHcontext,
1023
					mr->smr_oid, mlen );
1024
				HASH_Update( &HASHcontext,
1025
					&value->bv_val[value->bv_len-j], j );
1026
				HASH_Final( HASHdigest, &HASHcontext );
1027
1028
1029
1030
1031
1032
1033

				keys[nkeys++] = ber_bvdup( &digest );
			}

		}
	}

1034
1035
1036
1037
1038
1039
1040
1041
	if( nkeys > 0 ) {
		keys[nkeys] = NULL;
		*keysp = keys;
	} else {
		ch_free( keys );
		*keysp = NULL;
	}

1042
1043
	return LDAP_SUCCESS;
}
1044

1045
int caseExactSubstringsFilter(
1046
1047
	slap_mask_t use,
	slap_mask_t flags,
1048
1049
1050
1051
1052
1053
1054
1055
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	void * assertValue,
	struct berval ***keysp )
{
	SubstringsAssertion *sa = assertValue;
	char pre;
1056
	ber_len_t nkeys = 0;
1057
1058
	size_t slen, mlen, klen;
	struct berval **keys;
1059
1060
	HASH_CONTEXT   HASHcontext;
	unsigned char   HASHdigest[HASH_BYTES];
1061
1062
1063
	struct berval *value;
	struct berval digest;

1064
	if( flags & SLAP_INDEX_SUBSTR_INITIAL && sa->sa_initial != NULL &&
1065
		sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1066
1067
1068
	{
		nkeys++;
	}
1069

1070
	if( flags & SLAP_INDEX_SUBSTR_ANY && sa->sa_any != NULL ) {
1071
1072
		ber_len_t i;
		for( i=0; sa->sa_any[i] != NULL; i++ ) {
1073
			if( sa->sa_any[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
1074
				/* don't bother accounting for stepping */
1075
				nkeys += sa->sa_any[i]->bv_len -
1076
1077
1078
1079
1080
					( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
			}
		}
	}

1081
	if( flags & SLAP_INDEX_SUBSTR_FINAL && sa->sa_final != NULL &&
1082
		sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1083
1084
1085
1086
1087
1088
1089
1090
1091
	{
		nkeys++;
	}

	if( nkeys == 0 ) {
		*keysp = NULL;
		return LDAP_SUCCESS;
	}

1092
1093
	digest.bv_val = HASHdigest;
	digest.bv_len = sizeof(HASHdigest);
1094
1095
1096
1097
1098
1099
1100

	slen = strlen( syntax->ssyn_oid );
	mlen = strlen( mr->smr_oid );

	keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
	nkeys = 0;

1101
	if( flags & SLAP_INDEX_SUBSTR_INITIAL && sa->sa_initial != NULL &&
1102
		sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1103
1104
1105
1106
	{
		pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
		value = sa->sa_initial;

1107
1108
		klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
			? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
1109

1110
		HASH_Init( &HASHcontext );
1111
		if( prefix != NULL && prefix->bv_len > 0 ) {
1112
			HASH_Update( &HASHcontext,
1113
1114
				prefix->bv_val, prefix->bv_len );
		}
1115
		HASH_Update( &HASHcontext,
1116
			&pre, sizeof( pre ) );
1117
		HASH_Update( &HASHcontext,
1118
			syntax->ssyn_oid, slen );
1119
		HASH_Update( &HASHcontext,
1120
			mr->smr_oid, mlen );
1121
		HASH_Update( &HASHcontext,
1122
			value->bv_val, klen );
1123
		HASH_Final( HASHdigest, &HASHcontext );
1124
1125
1126
1127

		keys[nkeys++] = ber_bvdup( &digest );
	}

1128
	if( flags & SLAP_INDEX_SUBSTR_ANY && sa->sa_any != NULL ) {
1129
1130
		ber_len_t i, j;
		pre = SLAP_INDEX_SUBSTR_PREFIX;
1131
		klen = SLAP_INDEX_SUBSTR_MAXLEN;
1132
1133

		for( i=0; sa->sa_any[i] != NULL; i++ ) {
1134
			if( sa->sa_any[i]->bv_len < SLAP_INDEX_SUBSTR_MAXLEN ) {
1135
1136
1137
1138
1139
				continue;
			}

			value = sa->sa_any[i];

Kurt Zeilenga's avatar
Kurt Zeilenga committed
1140
			for(j=0;
1141
1142
				j <= value->bv_len - SLAP_INDEX_SUBSTR_MAXLEN;
				j += SLAP_INDEX_SUBSTR_STEP )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1143
			{
1144
				HASH_Init( &HASHcontext );
1145
				if( prefix != NULL && prefix->bv_len > 0 ) {
1146
					HASH_Update( &HASHcontext,
1147
1148
						prefix->bv_val, prefix->bv_len );
				}
1149
				HASH_Update( &HASHcontext,
1150
					&pre, sizeof( pre ) );
1151
				HASH_Update( &HASHcontext,
1152
					syntax->ssyn_oid, slen );
1153
				HASH_Update( &HASHcontext,
1154
					mr->smr_oid, mlen );
1155
				HASH_Update( &HASHcontext,
1156
					&value->bv_val[j], klen ); 
1157
				HASH_Final( HASHdigest, &HASHcontext );
1158
1159
1160
1161
1162
1163

				keys[nkeys++] = ber_bvdup( &digest );
			}
		}
	}

1164
	if( flags & SLAP_INDEX_SUBSTR_FINAL && sa->sa_final != NULL &&
1165
		sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1166
1167
1168
1169
	{
		pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
		value = sa->sa_final;

1170
1171
		klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
			? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
1172

1173
		HASH_Init( &HASHcontext );
1174
		if( prefix != NULL && prefix->bv_len > 0 ) {
1175
			HASH_Update( &HASHcontext,
1176
1177
				prefix->bv_val, prefix->bv_len );
		}
1178
		HASH_Update( &HASHcontext,
1179
			&pre, sizeof( pre ) );
1180
		HASH_Update( &HASHcontext,
1181
			syntax->ssyn_oid, slen );
1182
		HASH_Update( &HASHcontext,
1183
			mr->smr_oid, mlen );
1184
		HASH_Update( &HASHcontext,
1185
			&value->bv_val[value->bv_len-klen], klen );
1186
		HASH_Final( HASHdigest, &HASHcontext );