schema_init.c 105 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
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

#include "portable.h"

#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
11
#include <limits.h>
12
13

#include <ac/ctype.h>
14
#include <ac/errno.h>
15
16
17
18
19
#include <ac/string.h>
#include <ac/socket.h>

#include "slap.h"
#include "ldap_pvt.h"
20

21
22
#include "ldap_utf8.h"

23
24
25
26
27
28
29
#include "lutil_hash.h"
/* We should replace MD5 with a faster hash */
#define HASH_BYTES				LUTIL_HASH_BYTES
#define HASH_CONTEXT			lutil_HASH_CTX
#define HASH_Init(c)			lutil_HASHInit(c)
#define HASH_Update(c,buf,len)	lutil_HASHUpdate(c,buf,len)
#define HASH_Final(d,c)			lutil_HASHFinal(d,c)
30

31
/* recycled validatation routines */
32
#define berValidate						blobValidate
33
34
35
36

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

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

48
49
/* approx matching rules */
#define directoryStringApproxMatchOID	"1.3.6.1.4.1.4203.666.4.4"
Gary Williams's avatar
Gary Williams committed
50
51
52
#define directoryStringApproxMatch	approxMatch
#define directoryStringApproxIndexer	approxIndexer
#define directoryStringApproxFilter	approxFilter
53
#define IA5StringApproxMatchOID			"1.3.6.1.4.1.4203.666.4.5"
Gary Williams's avatar
Gary Williams committed
54
#define IA5StringApproxMatch			approxMatch
55
#define IA5StringApproxIndexer			approxIndexer
Gary Williams's avatar
Gary Williams committed
56
#define IA5StringApproxFilter			approxFilter
57

58
59
60
61
/* orderring matching rules */
#define caseIgnoreOrderingMatch			caseIgnoreMatch
#define caseExactOrderingMatch			caseExactMatch

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

#define OpenLDAPaciMatch				NULL
#define authPasswordMatch				NULL
70
71

/* recycled indexing/filtering routines */
72
73
#define dnIndexer				caseExactIgnoreIndexer
#define dnFilter				caseExactIgnoreFilter
74

75
76
77
78
79
#define telephoneNumberIndexer			caseIgnoreIA5Indexer
#define telephoneNumberFilter			caseIgnoreIA5Filter
#define telephoneNumberSubstringsIndexer	caseIgnoreIA5SubstringsIndexer
#define telephoneNumberSubstringsFilter		caseIgnoreIA5SubstringsFilter

80
81
82
83
/* must match OIDs below */
#define caseExactMatchOID			"2.5.13.5"
#define caseExactSubstringsMatchOID		"2.5.13.7"

84
85
86
87
static char *strcasechr( const char *str, int c )
{
	char *lower = strchr( str, TOLOWER(c) );
	char *upper = strchr( str, TOUPPER(c) );
88

89
90
91
92
93
94
95
96
	if( lower && upper ) {
		return lower < upper ? lower : upper;
	} else if ( lower ) {
		return lower;
	} else {
		return upper;
	}
}
97

98
99
100
static int
octetStringMatch(
	int *matchp,
101
	slap_mask_t flags,
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	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(
121
122
	slap_mask_t use,
	slap_mask_t flags,
123
124
125
126
127
128
129
130
131
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	struct berval **values,
	struct berval ***keysp )
{
	int i;
	size_t slen, mlen;
	struct berval **keys;
132
	HASH_CONTEXT   HASHcontext;
Gary Williams's avatar
Gary Williams committed
133
	unsigned char	HASHdigest[HASH_BYTES];
134
	struct berval digest;
135
136
	digest.bv_val = HASHdigest;
	digest.bv_len = sizeof(HASHdigest);
137
138
139
140
141

	for( i=0; values[i] != NULL; i++ ) {
		/* just count them */
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
142
143
144
	/* we should have at least one value at this point */
	assert( i > 0 );

145
146
147
148
149
150
	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++ ) {
151
		HASH_Init( &HASHcontext );
152
		if( prefix != NULL && prefix->bv_len > 0 ) {
153
			HASH_Update( &HASHcontext,
154
155
				prefix->bv_val, prefix->bv_len );
		}
156
		HASH_Update( &HASHcontext,
157
			syntax->ssyn_oid, slen );
158
		HASH_Update( &HASHcontext,
159
			mr->smr_oid, mlen );
160
		HASH_Update( &HASHcontext,
161
			values[i]->bv_val, values[i]->bv_len );
162
		HASH_Final( HASHdigest, &HASHcontext );
163
164
165
166
167
168
169
170
171
172
173
174
175

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

	keys[i] = NULL;

	*keysp = keys;

	return LDAP_SUCCESS;
}

/* Index generation function */
int octetStringFilter(
176
177
	slap_mask_t use,
	slap_mask_t flags,
178
179
180
181
182
183
184
185
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	void * assertValue,
	struct berval ***keysp )
{
	size_t slen, mlen;
	struct berval **keys;
186
	HASH_CONTEXT   HASHcontext;
Gary Williams's avatar
Gary Williams committed
187
	unsigned char	HASHdigest[HASH_BYTES];
188
189
	struct berval *value = (struct berval *) assertValue;
	struct berval digest;
190
191
	digest.bv_val = HASHdigest;
	digest.bv_len = sizeof(HASHdigest);
192
193
194
195
196
197

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

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

198
	HASH_Init( &HASHcontext );
199
	if( prefix != NULL && prefix->bv_len > 0 ) {
200
		HASH_Update( &HASHcontext,
201
202
			prefix->bv_val, prefix->bv_len );
	}
203
	HASH_Update( &HASHcontext,
204
		syntax->ssyn_oid, slen );
205
	HASH_Update( &HASHcontext,
206
		mr->smr_oid, mlen );
207
	HASH_Update( &HASHcontext,
208
		value->bv_val, value->bv_len );
209
	HASH_Final( HASHdigest, &HASHcontext );
210
211
212
213
214
215
216
217

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

	*keysp = keys;

	return LDAP_SUCCESS;
}
218

219
220
221
222
223
224
225
226
227
228
static int
dnValidate(
	Syntax *syntax,
	struct berval *in )
{
	int rc;
	char *dn;

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

229
	dn = ch_strdup( in->bv_val );
230

231
	if( dn == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
232
		return LDAP_INVALID_SYNTAX;
233

234
	} else if ( strlen( in->bv_val ) != in->bv_len ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
235
		rc = LDAP_INVALID_SYNTAX;
236

237
	} else if ( dn_validate( dn ) == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
238
		rc = LDAP_INVALID_SYNTAX;
239
240
241
242
243
244

	} else {
		rc = LDAP_SUCCESS;
	}

	ch_free( dn );
245
246
247
	return rc;
}

248
int
249
250
251
252
253
dnNormalize(
	Syntax *syntax,
	struct berval *val,
	struct berval **normalized )
{
254
	struct berval *out;
255

256
257
	if ( val->bv_len != 0 ) {
		char *dn;
258
		out = ber_bvstr( UTF8normalize( val, UTF8_CASEFOLD ) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
259

260
261
		dn = dn_validate( out->bv_val );

262
		if( dn == NULL ) {
263
264
265
			ber_bvfree( out );
			return LDAP_INVALID_SYNTAX;
		}
266

267
268
269
270
		out->bv_val = dn;
		out->bv_len = strlen( dn );
	} else {
		out = ber_bvdup( val );
271
272
273
274
275
276
277
278
	}

	*normalized = out;
	return LDAP_SUCCESS;
}

static int
dnMatch(
279
	int *matchp,
280
	slap_mask_t flags,
281
282
283
284
285
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
286
	int match;
287
288
	struct berval *asserted = (struct berval *) assertedValue;
	
289
290
291
	match = value->bv_len - asserted->bv_len;

	if( match == 0 ) {
292
#ifdef USE_DN_NORMALIZE
293
		match = strcmp( value->bv_val, asserted->bv_val );
294
#else
295
		match = strcasecmp( value->bv_val, asserted->bv_val );
296
#endif
297
298
	}

299
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
300
	LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
301
302
		"dnMatch: %d\n    %s\n    %s\n", match,
		value->bv_val, asserted->bv_val ));
303
#else
304
	Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
305
		match, value->bv_val, asserted->bv_val );
306
307
#endif

308
309
310

	*matchp = match;
	return LDAP_SUCCESS;
311
}
312

313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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;
			}
		}
Stig Venaas's avatar
Stig Venaas committed
334
335
336
337
		if( dn->bv_val[i] != '\'' ||
		    dn->bv_val[i-1] != 'B' ||
		    dn->bv_val[i-2] != '#' ) {
			ber_bvfree( dn );
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
			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;
}

408
409
410
411
412
413
414
415
416
static int
inValidate(
	Syntax *syntax,
	struct berval *in )
{
	/* any value allowed */
	return LDAP_OTHER;
}

417
static int
418
blobValidate(
419
420
421
422
	Syntax *syntax,
	struct berval *in )
{
	/* any value allowed */
423
	return LDAP_SUCCESS;
424
425
}

426
427
428
429
430
431
432
433
434
435
436
437
438
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;
	}
439

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
	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;
}

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
/*
 * 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,
487
	slap_mask_t flags,
488
489
490
491
492
493
494
495
496
497
498
	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;
}

499
500
501
502
503
504
505
506
507
static int
UTF8StringValidate(
	Syntax *syntax,
	struct berval *in )
{
	ber_len_t count;
	int len;
	unsigned char *u = in->bv_val;

508
509
	if( !in->bv_len ) return LDAP_INVALID_SYNTAX;

510
	for( count = in->bv_len; count > 0; count-=len, u+=len ) {
511
512
513
514
		/* get the length indicated by the first byte */
		len = LDAP_UTF8_CHARLEN( u );

		/* should not be zero */
515
		if( len == 0 ) return LDAP_INVALID_SYNTAX;
516
517
518

		/* make sure len corresponds with the offset
			to the next character */
519
		if( LDAP_UTF8_OFFSET( u ) != len ) return LDAP_INVALID_SYNTAX;
520
521
	}

522
	if( count != 0 ) return LDAP_INVALID_SYNTAX;
523

524
	return LDAP_SUCCESS;
525
526
527
528
529
530
531
532
533
}

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

536
	newval = ch_malloc( sizeof( struct berval ) );
537

538
	p = val->bv_val;
539

540
541
542
	/* Ignore initial whitespace */
	while ( ldap_utf8_isspace( p ) ) {
		LDAP_UTF8_INCR( p );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
543
	}
544

545
546
	if( *p == '\0' ) {
		ch_free( newval );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
547
548
		return LDAP_INVALID_SYNTAX;
	}
549

550
551
552
	newval->bv_val = ch_strdup( p );
	p = q = newval->bv_val;
	s = NULL;
553

554
555
	while ( *p ) {
		int len;
556

557
558
559
560
561
		if ( ldap_utf8_isspace( p ) ) {
			len = LDAP_UTF8_COPY(q,p);
			s=q;
			p+=len;
			q+=len;
562

563
564
565
566
			/* Ignore the extra whitespace */
			while ( ldap_utf8_isspace( p ) ) {
				LDAP_UTF8_INCR( p );
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
567
		} else {
568
569
570
571
			len = LDAP_UTF8_COPY(q,p);
			s=NULL;
			p+=len;
			q+=len;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
572
		}
573
574
575
576
	}

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

579
580
581
582
583
584
585
586
587
588
589
	/* 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
590
	}
591

592
593
594
595
596
597
598
599
	/* 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;
600

601
	return LDAP_SUCCESS;
602
603
}

604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/* Returns Unicode cannonically normalized copy of a substring assertion
 * Skipping attribute description */
SubstringsAssertion *
UTF8SubstringsassertionNormalize(
	SubstringsAssertion *sa,
	char casefold )
{
	SubstringsAssertion *nsa;
	int i;

	nsa = (SubstringsAssertion *)ch_calloc( 1, sizeof(SubstringsAssertion) );
	if( nsa == NULL ) {
		return NULL;
	}

	if( sa->sa_initial != NULL ) {
620
		nsa->sa_initial = ber_bvstr( UTF8normalize( sa->sa_initial, casefold ) );
621
622
623
624
625
626
627
628
629
630
631
		if( nsa->sa_initial == NULL ) {
			goto err;
		}
	}

	if( sa->sa_any != NULL ) {
		for( i=0; sa->sa_any[i] != NULL; i++ ) {
			/* empty */
		}
		nsa->sa_any = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
		for( i=0; sa->sa_any[i] != NULL; i++ ) {
632
			nsa->sa_any[i] = ber_bvstr( UTF8normalize( sa->sa_any[i], casefold ) );
633
634
635
636
637
638
639
640
			if( nsa->sa_any[i] == NULL ) {
				goto err;
			}
		}
		nsa->sa_any[i] = NULL;
	}

	if( sa->sa_final != NULL ) {
641
		nsa->sa_final = ber_bvstr( UTF8normalize( sa->sa_final, casefold ) );
642
643
644
645
646
647
648
649
		if( nsa->sa_final == NULL ) {
			goto err;
		}
	}

	return nsa;

err:
650
	ber_bvfree( nsa->sa_final );
651
	ber_bvecfree( nsa->sa_any );
652
	ber_bvfree( nsa->sa_initial );
653
654
655
656
	ch_free( nsa );
	return NULL;
}

657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/* Strip characters with the 8th bit set */
char *
strip8bitChars(
	char *in )      
{
	char *p = in, *q;
  
	if( in == NULL ) {
		return NULL;
	}
	while( *p ) {
		if( *p & 0x80 ) {
			q = p;
			while( *++q & 0x80 ) {
				/* empty */
			}
			p = memmove(p, q, strlen(q) + 1);
		} else {
			p++;
		}
	}
	return in;
}

681
#ifndef SLAPD_APPROX_OLDSINGLESTRING
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699

#if defined(SLAPD_APPROX_INITIALS)
#define SLAPD_APPROX_DELIMITER "._ "
#define SLAPD_APPROX_WORDLEN 2
#else
#define SLAPD_APPROX_DELIMITER " "
#define SLAPD_APPROX_WORDLEN 1
#endif

static int
approxMatch(
	int *matchp,
	slap_mask_t flags,
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
700
	char *val, *nval, *assertv, **values, **words, *c;
701
	int i, count, len, nextchunk=0, nextavail=0;
702
	size_t avlen;
703

704
	/* Yes, this is necessary */
705
	nval = UTF8normalize( value, UTF8_NOCASEFOLD );
706
	if( nval == NULL ) {
707
708
709
		*matchp = 1;
		return LDAP_SUCCESS;
	}
710
	strip8bitChars( nval );
711
712

	/* Yes, this is necessary */
713
	assertv = UTF8normalize( ((struct berval *)assertedValue),
714
715
				 UTF8_NOCASEFOLD );
	if( assertv == NULL ) {
716
		ch_free( nval );
717
718
719
720
721
		*matchp = 1;
		return LDAP_SUCCESS;
	}
	strip8bitChars( assertv );
	avlen = strlen( assertv );
722
723

	/* Isolate how many words there are */
724
	for( c=nval,count=1; *c; c++ ) {
725
726
727
728
729
730
731
732
733
		c = strpbrk( c, SLAPD_APPROX_DELIMITER );
		if ( c == NULL ) break;
		*c = '\0';
		count++;
	}

	/* Get a phonetic copy of each word */
	words = (char **)ch_malloc( count * sizeof(char *) );
	values = (char **)ch_malloc( count * sizeof(char *) );
734
	for( c=nval,i=0;  i<count;  i++,c+=strlen(c)+1 ) {
735
736
737
738
		words[i] = c;
		values[i] = phonetic(c);
	}

739
	/* Work through the asserted value's words, to see if at least some
740
741
	   of the words are there, in the same order. */
	len = 0;
742
	while ( nextchunk < avlen ) {
743
		len = strcspn( assertv + nextchunk, SLAPD_APPROX_DELIMITER);
744
745
746
747
		if( len == 0 ) {
			nextchunk++;
			continue;
		}
748
#if defined(SLAPD_APPROX_INITIALS)
749
		else if( len == 1 ) {
750
751
			/* Single letter words need to at least match one word's initial */
			for( i=nextavail; i<count; i++ )
752
753
				if( !strncasecmp( assertv+nextchunk, words[i], 1 )) {
					nextavail=i+1;
754
					break;
755
				}
756
757
		}
#endif
758
		else {
759
760
761
762
763
764
765
766
767
768
769
			/* Isolate the next word in the asserted value and phonetic it */
			assertv[nextchunk+len] = '\0';
			val = phonetic( assertv + nextchunk );

			/* See if this phonetic chunk is in the remaining words of *value */
			for( i=nextavail; i<count; i++ ){
				if( !strcmp( val, values[i] ) ){
					nextavail = i+1;
					break;
				}
			}
770
			ch_free( val );
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
		}

		/* This chunk in the asserted value was NOT within the *value. */
		if( i >= count ) {
			nextavail=-1;
			break;
		}

		/* Go on to the next word in the asserted value */
		nextchunk += len+1;
	}

	/* If some of the words were seen, call it a match */
	if( nextavail > 0 ) {
		*matchp = 0;
	}
	else {
		*matchp = 1;
	}

	/* Cleanup allocs */
792
	free( assertv );
793
794
795
796
797
	for( i=0; i<count; i++ ) {
		ch_free( values[i] );
	}
	ch_free( values );
	ch_free( words );
798
	ch_free( nval );
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817

	return LDAP_SUCCESS;
}

int 
approxIndexer(
	slap_mask_t use,
	slap_mask_t flags,
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	struct berval **values,
	struct berval ***keysp )
{
	char *val, *c;
	int i,j, len, wordcount, keycount=0;
	struct berval **newkeys, **keys=NULL;

	for( j=0; values[j] != NULL; j++ ) {
818
		/* Yes, this is necessary */
819
		val = UTF8normalize( values[j], UTF8_NOCASEFOLD );
820
821
		strip8bitChars( val );

822
		/* Isolate how many words there are. There will be a key for each */
Gary Williams's avatar
Gary Williams committed
823
		for( wordcount=0,c=val;	 *c;  c++) {
824
825
826
827
828
829
830
831
832
			len = strcspn(c, SLAPD_APPROX_DELIMITER);
			if( len >= SLAPD_APPROX_WORDLEN ) wordcount++;
			c+= len;
			if (*c == '\0') break;
			*c = '\0';
		}

		/* Allocate/increase storage to account for new keys */
		newkeys = (struct berval **)ch_malloc( (keycount + wordcount + 1) 
Kurt Zeilenga's avatar
Kurt Zeilenga committed
833
			* sizeof(struct berval *) );
834
835
836
837
838
		memcpy( newkeys, keys, keycount * sizeof(struct berval *) );
		if( keys ) ch_free( keys );
		keys = newkeys;

		/* Get a phonetic copy of each word */
Gary Williams's avatar
Gary Williams committed
839
		for( c=val,i=0;	 i<wordcount;  c+=len+1	 ) {
840
841
842
843
844
845
846
847
848
			len = strlen( c );
			if( len < SLAPD_APPROX_WORDLEN ) continue;
			keys[keycount] = (struct berval *)ch_malloc( sizeof(struct berval) );
			keys[keycount]->bv_val = phonetic( c );
			keys[keycount]->bv_len = strlen( keys[keycount]->bv_val );
			keycount++;
			i++;
		}

849
		free( val );
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
	}
	keys[keycount] = NULL;
	*keysp = keys;

	return LDAP_SUCCESS;
}

int 
approxFilter(
	slap_mask_t use,
	slap_mask_t flags,
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	void * assertValue,
	struct berval ***keysp )
{
	char *val, *c;
	int i, count, len;
	struct berval **keys;

871
	/* Yes, this is necessary */
872
	val = UTF8normalize( ((struct berval *)assertValue),
873
874
875
876
877
878
879
880
881
			     UTF8_NOCASEFOLD );
	if( val == NULL ) {
		keys = (struct berval **)ch_malloc( sizeof(struct berval *) );
		keys[0] = NULL;
		*keysp = keys;
		return LDAP_SUCCESS;
	}
	strip8bitChars( val );

882
883
884
885
886
887
888
889
890
891
892
893
894
	/* Isolate how many words there are. There will be a key for each */
	for( count=0,c=val;  *c;  c++) {
		len = strcspn(c, SLAPD_APPROX_DELIMITER);
		if( len >= SLAPD_APPROX_WORDLEN ) count++;
		c+= len;
		if (*c == '\0') break;
		*c = '\0';
	}

	/* Allocate storage for new keys */
	keys = (struct berval **)ch_malloc( (count + 1) * sizeof(struct berval *) );

	/* Get a phonetic copy of each word */
Gary Williams's avatar
Gary Williams committed
895
	for( c=val,i=0;	 i<count; c+=len+1 ) {
896
897
		len = strlen(c);
		if( len < SLAPD_APPROX_WORDLEN ) continue;
898
		keys[i] = ber_bvstr( phonetic( c ) );
899
900
901
		i++;
	}

902
	free( val );
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923

	keys[count] = NULL;
	*keysp = keys;

	return LDAP_SUCCESS;
}


#else
/* No other form of Approximate Matching is defined */

static int
approxMatch(
	int *matchp,
	slap_mask_t flags,
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
	char *vapprox, *avapprox;
924
	char *s, *t;
925

926
	/* Yes, this is necessary */
927
	s = UTF8normalize( value, UTF8_NOCASEFOLD );
928
929
930
931
932
933
	if( s == NULL ) {
		*matchp = 1;
		return LDAP_SUCCESS;
	}

	/* Yes, this is necessary */
934
	t = UTF8normalize( ((struct berval *)assertedValue),
935
936
937
938
939
940
941
942
943
944
945
946
			   UTF8_NOCASEFOLD );
	if( t == NULL ) {
		free( s );
		*matchp = -1;
		return LDAP_SUCCESS;
	}

	vapprox = phonetic( strip8bitChars( s ) );
	avapprox = phonetic( strip8bitChars( t ) );

	free( s );
	free( t );
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967

	*matchp = strcmp( vapprox, avapprox );

	ch_free( vapprox );
	ch_free( avapprox );

	return LDAP_SUCCESS;
}

int 
approxIndexer(
	slap_mask_t use,
	slap_mask_t flags,
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	struct berval **values,
	struct berval ***keysp )
{
	int i;
	struct berval **keys;
968
	char *s;
969
970

	for( i=0; values[i] != NULL; i++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
971
		/* empty - just count them */
972
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
973
974

	/* we should have at least one value at this point */
975
976
977
978
979
980
	assert( i > 0 );

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

	/* Copy each value and run it through phonetic() */
	for( i=0; values[i] != NULL; i++ ) {
981
		/* Yes, this is necessary */
982
		s = UTF8normalize( values[i], UTF8_NOCASEFOLD );
983
984
985
986

		/* strip 8-bit chars and run through phonetic() */
		keys[i] = ber_bvstr( phonetic( strip8bitChars( s ) ) );
		free( s );
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
	}
	keys[i] = NULL;

	*keysp = keys;
	return LDAP_SUCCESS;
}


int 
approxFilter(
	slap_mask_t use,
	slap_mask_t flags,
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	void * assertValue,
	struct berval ***keysp )
{
	struct berval **keys;
1006
	char *s;
1007
1008
1009

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

1010
	/* Yes, this is necessary */
1011
	s = UTF8normalize( ((struct berval *)assertValue),
1012
1013
1014
1015
1016
1017
1018
1019
1020
			     UTF8_NOCASEFOLD );
	if( s == NULL ) {
		keys[0] = NULL;
	} else {
		/* strip 8-bit chars and run through phonetic() */
		keys[0] = ber_bvstr( phonetic( strip8bitChars( s ) ) );
		free( s );
		keys[1] = NULL;
	}
1021
1022
1023
1024
1025
1026
1027

	*keysp = keys;
	return LDAP_SUCCESS;
}
#endif


1028
static int
1029
caseExactMatch(
1030
	int *matchp,
1031
	slap_mask_t flags,
1032
1033
1034
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
1035
	void *assertedValue )
1036
{
1037
1038
1039
	*matchp = UTF8normcmp( value->bv_val,
		((struct berval *) assertedValue)->bv_val,
		UTF8_NOCASEFOLD );
1040
	return LDAP_SUCCESS;
1041
1042
}

1043
static int
1044
caseExactIgnoreSubstringsMatch(
1045
	int *matchp,
1046
	slap_mask_t flags,
1047
1048
1049
1050
1051
1052
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
	int match = 0;
1053
1054
	SubstringsAssertion *sub;
	struct berval left;
1055
1056
	int i;
	ber_len_t inlen=0;
1057
1058
1059
1060
	char *nav, casefold;

	casefold = strcmp( mr->smr_oid, caseExactSubstringsMatchOID )
		? UTF8_CASEFOLD : UTF8_NOCASEFOLD;
1061

1062
	nav = UTF8normalize( value, casefold );
1063
1064
1065
1066
1067
1068
	if( nav == NULL ) {
		match = 1;
		goto done;
	}
	left.bv_val = nav;
	left.bv_len = strlen( nav );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1069

1070
	sub = UTF8SubstringsassertionNormalize( assertedValue, casefold );
1071
1072
1073
1074
	if( sub == NULL ) {
		match = -1;
		goto done;
	}
1075

1076
	/* Add up asserted input length */
1077
1078
1079
1080
	if( sub->sa_initial ) {
		inlen += sub->sa_initial->bv_len;
	}
	if( sub->sa_any ) {
1081
1082
		for(i=0; sub->sa_any[i] != NULL; i++) {
			inlen += sub->sa_any[i]->bv_len;
1083
1084
1085
1086
1087
1088
1089
		}
	}
	if( sub->sa_final ) {
		inlen += sub->sa_final->bv_len;
	}

	if( sub->sa_initial ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1090
1091
1092
1093
1094
		if( inlen > left.bv_len ) {
			match = 1;
			goto done;
		}

1095
1096
		match = strncmp( sub->sa_initial->bv_val, left.bv_val,
			sub->sa_initial->bv_len );
1097
1098
1099
1100
1101
1102
1103

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

		left.bv_val += sub->sa_initial->bv_len;
		left.bv_len -= sub->sa_initial->bv_len;
1104
		inlen -= sub->sa_initial->bv_len;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1105
	}
1106

Kurt Zeilenga's avatar
Kurt Zeilenga committed
1107
	if( sub->sa_final ) {
1108
1109
1110
1111
		if( inlen > left.bv_len ) {
			match = 1;
			goto done;
		}
1112

1113
1114
1115
		match = strncmp( sub->sa_final->bv_val,
			&left.bv_val[left.bv_len - sub->sa_final->bv_len],
			sub->sa_final->bv_len );
1116
1117
1118
1119
1120
1121

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

		left.bv_len -= sub->sa_final->bv_len;
1122
		inlen -= sub->sa_final->bv_len;
1123
1124
1125
	}

	if( sub->sa_any ) {
1126
1127
1128
1129
1130
		for(i=0; sub->sa_any[i]; i++) {
			ber_len_t idx;
			char *p;

retry:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1131
1132
1133
1134
1135
1136
			if( inlen > left.bv_len ) {
				/* not enough length */
				match = 1;
				goto done;
			}

1137
1138
1139
1140
			if( sub->sa_any[i]->bv_len == 0 ) {
				continue;
			}

1141
			p = strchr( left.bv_val, *sub->sa_any[i]->bv_val );
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152

			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 */
Stig Venaas's avatar
Stig Venaas committed
1153
1154
1155
1156
1157
				free( nav );
				ch_free( sub->sa_final );
				ber_bvecfree( sub->sa_any );
				ch_free( sub->sa_initial );
				ch_free( sub );
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
				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;
			}

1170
1171
1172
			match = strncmp( left.bv_val,
				sub->sa_any[i]->bv_val,
				sub->sa_any[i]->bv_len );
1173
1174

			if( match != 0 ) {
1175
1176
				left.bv_val++;
				left.bv_len--;
1177
1178
1179
1180
1181
				goto retry;
			}

			left.bv_val += sub->sa_any[i]->bv_len;
			left.bv_len -= sub->sa_any[i]->bv_len;
1182
			inlen -= sub->sa_any[i]->bv_len;
1183
		}
1184
1185
1186
	}

done:
1187
1188
	free( nav );
	if( sub != NULL ) {
1189
		ber_bvfree( sub->sa_final );
1190
		ber_bvecfree( sub->sa_any );
1191
		ber_bvfree( sub->sa_initial );
1192
1193
		ch_free( sub );
	}
1194
1195
1196
1197
	*matchp = match;
	return LDAP_SUCCESS;
}

1198
/* Index generation function */
1199
int caseExactIgnoreIndexer(
1200
1201
	slap_mask_t use,
	slap_mask_t flags,
1202
1203
1204
1205
1206
1207
1208
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	struct berval **values,
	struct berval ***keysp )
{
	int i;
1209
	char casefold;
1210
1211
	size_t slen, mlen;
	struct berval **keys;
1212
	HASH_CONTEXT   HASHcontext;
Gary Williams's avatar
Gary Williams committed
1213
	unsigned char	HASHdigest[HASH_BYTES];
1214
	struct berval digest;
1215
1216
	digest.bv_val = HASHdigest;
	digest.bv_len = sizeof(HASHdigest);
1217
1218

	for( i=0; values[i] != NULL; i++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1219
		/* empty - just count them */
1220
1221
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
1222
1223
1224
	/* we should have at least one value at this point */
	assert( i > 0 );

1225
1226
1227
1228
1229
	keys = ch_malloc( sizeof( struct berval * ) * (i+1) );

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

1230
1231
1232
	casefold = strcmp( mr->smr_oid, caseExactMatchOID )
		? UTF8_CASEFOLD : UTF8_NOCASEFOLD;

1233
	for( i=0; values[i] != NULL; i++ ) {
1234
		struct berval *value;
1235
		value = ber_bvstr( UTF8normalize( values[i],
1236
			casefold ) );
1237

1238
		HASH_Init( &HASHcontext );
1239
		if( prefix != NULL && prefix->bv_len > 0 ) {
1240
			HASH_Update( &HASHcontext,
1241
1242
				prefix->bv_val, prefix->bv_len );
		}
1243
		HASH_Update( &HASHcontext,
1244
			syntax->ssyn_oid, slen );
1245
		HASH_Update( &HASHcontext,
1246
			mr->smr_oid, mlen );
1247
		HASH_Update( &HASHcontext,
1248
			value->bv_val, value->bv_len );
1249
		HASH_Final( HASHdigest, &HASHcontext );
1250

1251
1252
		ber_bvfree( value );

1253
1254
1255
1256
1257