]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_init.c
dn_validate/dn_normalize has been rewritten by
[openldap] / servers / slapd / schema_init.c
1 /* schema_init.c - init builtin schema */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17 #include "ldap_pvt.h"
18
19 #define UTF8MATCH 1
20
21 #ifdef USE_MD5
22 #include "lutil_md5.h"
23 /* We should replace MD5 with a faster hash */
24 #define HASH_BYTES                              LUTIL_MD5_BYTES
25 #define HASH_CONTEXT                    lutil_MD5_CTX
26 #define HASH_Init(c)                    lutil_MD5Init(c)
27 #define HASH_Update(c,buf,len)  lutil_MD5Update(c,buf,len)
28 #define HASH_Final(d,c)                 lutil_MD5Final(d,c)
29 #else
30 #include "lutil_hash.h"
31 /* We should replace MD5 with a faster hash */
32 #define HASH_BYTES                              LUTIL_HASH_BYTES
33 #define HASH_CONTEXT                    lutil_HASH_CTX
34 #define HASH_Init(c)                    lutil_HASHInit(c)
35 #define HASH_Update(c,buf,len)  lutil_HASHUpdate(c,buf,len)
36 #define HASH_Final(d,c)                 lutil_HASHFinal(d,c)
37 #endif
38
39 /* recycled validatation routines */
40 #define berValidate                                             blobValidate
41
42 /* unimplemented pretters */
43 #define dnPretty                                                NULL
44 #define integerPretty                                   NULL
45
46 /* recycled matching routines */
47 #define bitStringMatch                                  octetStringMatch
48 #define integerMatch                                    caseIgnoreIA5Match
49 #define numericStringMatch                              caseIgnoreIA5Match
50 #define objectIdentifierMatch                   caseIgnoreIA5Match
51 #define telephoneNumberMatch                    caseIgnoreIA5Match
52 #define telephoneNumberSubstringsMatch  caseIgnoreIA5SubstringsMatch
53 #define generalizedTimeMatch                    caseIgnoreIA5Match
54 #define generalizedTimeOrderingMatch    caseIgnoreIA5Match
55 #define uniqueMemberMatch                               dnMatch
56
57 /* approx matching rules */
58 #define directoryStringApproxMatchOID   "1.3.6.1.4.1.4203.666.4.4"
59 #define directoryStringApproxMatch      approxMatch
60 #define directoryStringApproxIndexer    approxIndexer
61 #define directoryStringApproxFilter     approxFilter
62 #define IA5StringApproxMatchOID                 "1.3.6.1.4.1.4203.666.4.5"
63 #define IA5StringApproxMatch                    approxMatch
64 #define IA5StringApproxIndexer                  approxIndexer
65 #define IA5StringApproxFilter                   approxFilter
66
67 /* orderring matching rules */
68 #define caseIgnoreOrderingMatch                 caseIgnoreMatch
69 #define caseExactOrderingMatch                  caseExactMatch
70
71 /* unimplemented matching routines */
72 #define caseIgnoreListMatch                             NULL
73 #define caseIgnoreListSubstringsMatch   NULL
74 #define protocolInformationMatch                NULL
75 #define integerFirstComponentMatch              NULL
76
77 #define OpenLDAPaciMatch                                NULL
78 #define authPasswordMatch                               NULL
79
80 /* recycled indexing/filtering routines */
81 #define dnIndexer                                               caseIgnoreIndexer
82 #define dnFilter                                                caseIgnoreFilter
83 #define integerIndexer                                  caseIgnoreIA5Indexer
84 #define integerFilter                                   caseIgnoreIA5Filter
85
86 #define telephoneNumberIndexer                  caseIgnoreIA5Indexer
87 #define telephoneNumberFilter                   caseIgnoreIA5Filter
88 #define telephoneNumberSubstringsIndexer        caseIgnoreIA5SubstringsIndexer
89 #define telephoneNumberSubstringsFilter         caseIgnoreIA5SubstringsFilter
90
91 static char *strcasechr( const char *str, int c )
92 {
93         char *lower = strchr( str, TOLOWER(c) );
94         char *upper = strchr( str, TOUPPER(c) );
95
96         if( lower && upper ) {
97                 return lower < upper ? lower : upper;
98         } else if ( lower ) {
99                 return lower;
100         } else {
101                 return upper;
102         }
103 }
104
105 static int
106 octetStringMatch(
107         int *matchp,
108         slap_mask_t flags,
109         Syntax *syntax,
110         MatchingRule *mr,
111         struct berval *value,
112         void *assertedValue )
113 {
114         int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;
115
116         if( match == 0 ) {
117                 match = memcmp( value->bv_val,
118                         ((struct berval *) assertedValue)->bv_val,
119                         value->bv_len );
120         }
121
122         *matchp = match;
123         return LDAP_SUCCESS;
124 }
125
126 /* Index generation function */
127 int octetStringIndexer(
128         slap_mask_t use,
129         slap_mask_t flags,
130         Syntax *syntax,
131         MatchingRule *mr,
132         struct berval *prefix,
133         struct berval **values,
134         struct berval ***keysp )
135 {
136         int i;
137         size_t slen, mlen;
138         struct berval **keys;
139         HASH_CONTEXT   HASHcontext;
140         unsigned char   HASHdigest[HASH_BYTES];
141         struct berval digest;
142         digest.bv_val = HASHdigest;
143         digest.bv_len = sizeof(HASHdigest);
144
145         /* we should have at least one value at this point */
146         assert( values != NULL && values[0] != NULL );
147
148         for( i=0; values[i] != NULL; i++ ) {
149                 /* just count them */
150         }
151
152         keys = ch_malloc( sizeof( struct berval * ) * (i+1) );
153
154         slen = strlen( syntax->ssyn_oid );
155         mlen = strlen( mr->smr_oid );
156
157         for( i=0; values[i] != NULL; i++ ) {
158                 HASH_Init( &HASHcontext );
159                 if( prefix != NULL && prefix->bv_len > 0 ) {
160                         HASH_Update( &HASHcontext,
161                                 prefix->bv_val, prefix->bv_len );
162                 }
163                 HASH_Update( &HASHcontext,
164                         syntax->ssyn_oid, slen );
165                 HASH_Update( &HASHcontext,
166                         mr->smr_oid, mlen );
167                 HASH_Update( &HASHcontext,
168                         values[i]->bv_val, values[i]->bv_len );
169                 HASH_Final( HASHdigest, &HASHcontext );
170
171                 keys[i] = ber_bvdup( &digest );
172         }
173
174         keys[i] = NULL;
175
176         *keysp = keys;
177
178         return LDAP_SUCCESS;
179 }
180
181 /* Index generation function */
182 int octetStringFilter(
183         slap_mask_t use,
184         slap_mask_t flags,
185         Syntax *syntax,
186         MatchingRule *mr,
187         struct berval *prefix,
188         void * assertValue,
189         struct berval ***keysp )
190 {
191         size_t slen, mlen;
192         struct berval **keys;
193         HASH_CONTEXT   HASHcontext;
194         unsigned char   HASHdigest[HASH_BYTES];
195         struct berval *value = (struct berval *) assertValue;
196         struct berval digest;
197         digest.bv_val = HASHdigest;
198         digest.bv_len = sizeof(HASHdigest);
199
200         slen = strlen( syntax->ssyn_oid );
201         mlen = strlen( mr->smr_oid );
202
203         keys = ch_malloc( sizeof( struct berval * ) * 2 );
204
205         HASH_Init( &HASHcontext );
206         if( prefix != NULL && prefix->bv_len > 0 ) {
207                 HASH_Update( &HASHcontext,
208                         prefix->bv_val, prefix->bv_len );
209         }
210         HASH_Update( &HASHcontext,
211                 syntax->ssyn_oid, slen );
212         HASH_Update( &HASHcontext,
213                 mr->smr_oid, mlen );
214         HASH_Update( &HASHcontext,
215                 value->bv_val, value->bv_len );
216         HASH_Final( HASHdigest, &HASHcontext );
217
218         keys[0] = ber_bvdup( &digest );
219         keys[1] = NULL;
220
221         *keysp = keys;
222
223         return LDAP_SUCCESS;
224 }
225
226 static int
227 dnValidate(
228         Syntax *syntax,
229         struct berval *in )
230 {
231         int rc;
232         char *dn;
233
234         if( in->bv_len == 0 ) return LDAP_SUCCESS;
235
236         dn = get_validated_dn( in->bv_val, 0, 0 );
237
238         rc = ( dn == NULL ) ? LDAP_INVALID_SYNTAX : LDAP_SUCCESS;
239
240         if ( dn != NULL )
241                 ch_free( dn );
242
243         return rc;
244 }
245
246 int
247 dnNormalize(
248         Syntax *syntax,
249         struct berval *val,
250         struct berval **normalized )
251 {
252         struct berval *out;
253
254         if ( val->bv_len == 0 ) {
255                 out = ber_bvdup( val );
256         } else {
257                 char *dn;
258 #ifdef USE_DN_NORMALIZE
259                 dn = get_validated_dn( val->bv_val, 1, 1 );
260 #else
261                 dn = get_validated_dn( val->bv_val, 0, 0 );
262 #endif
263                 if( dn == NULL ) {
264                         return LDAP_INVALID_SYNTAX;
265                 }
266
267                 out = (struct berval *)ch_malloc(sizeof(struct berval));
268                 out->bv_val = dn;
269                 out->bv_len = strlen( dn );
270         }
271
272         *normalized = out;
273         return LDAP_SUCCESS;
274 }
275
276 static int
277 dnMatch(
278         int *matchp,
279         slap_mask_t flags,
280         Syntax *syntax,
281         MatchingRule *mr,
282         struct berval *value,
283         void *assertedValue )
284 {
285         int match;
286         struct berval *asserted = (struct berval *) assertedValue;
287         
288         match = value->bv_len - asserted->bv_len;
289
290         if( match == 0 ) {
291 #ifdef USE_DN_NORMALIZE
292                 match = strcmp( value->bv_val, asserted->bv_val );
293 #else
294                 match = strcasecmp( value->bv_val, asserted->bv_val );
295 #endif
296         }
297
298 #ifdef NEW_LOGGING
299         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
300                    "dnMatch: %d\n    %s\n    %s\n", match,
301                    value->bv_val, asserted->bv_val ));
302 #else
303         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
304             match, value->bv_val, asserted->bv_val );
305 #endif
306
307
308         *matchp = match;
309         return LDAP_SUCCESS;
310 }
311
312 static int
313 nameUIDValidate(
314         Syntax *syntax,
315         struct berval *in )
316 {
317         int rc;
318         struct berval *dn;
319
320         if( in->bv_len == 0 ) return LDAP_SUCCESS;
321
322         dn = ber_bvdup( in );
323
324         if( dn->bv_val[dn->bv_len-1] == '\'' ) {
325                 /* assume presence of optional UID */
326                 ber_len_t i;
327
328                 for(i=dn->bv_len-2; i>2; i--) {
329                         if( dn->bv_val[i] != '0' &&     dn->bv_val[i] != '1' ) {
330                                 break;
331                         }
332                 }
333                 if( dn->bv_val[i] != '\'' ) {
334                         return LDAP_INVALID_SYNTAX;
335                 }
336                 if( dn->bv_val[i-1] != 'B' ) {
337                         return LDAP_INVALID_SYNTAX;
338                 }
339                 if( dn->bv_val[i-2] != '#' ) {
340                         return LDAP_INVALID_SYNTAX;
341                 }
342
343                 /* trim the UID to allow use of dn_validate */
344                 dn->bv_val[i-2] = '\0';
345         }
346
347         rc = dn_validate( dn->bv_val ) == NULL
348                 ? LDAP_INVALID_SYNTAX : LDAP_SUCCESS;
349
350         ber_bvfree( dn );
351         return rc;
352 }
353
354 static int
355 nameUIDNormalize(
356         Syntax *syntax,
357         struct berval *val,
358         struct berval **normalized )
359 {
360         struct berval *out = ber_bvdup( val );
361
362         if( out->bv_len != 0 ) {
363                 char *dn;
364                 ber_len_t dnlen;
365                 char *uid = NULL;
366                 ber_len_t uidlen = 0;
367
368                 if( out->bv_val[out->bv_len-1] == '\'' ) {
369                         /* assume presence of optional UID */
370                         uid = strrchr( out->bv_val, '#' );
371
372                         if( uid == NULL ) {
373                                 ber_bvfree( out );
374                                 return LDAP_INVALID_SYNTAX;
375                         }
376
377                         uidlen = out->bv_len - (out->bv_val - uid);
378                         /* temporarily trim the UID */
379                         *uid = '\0';
380                 }
381
382 #ifdef USE_DN_NORMALIZE
383                 dn = dn_normalize( out->bv_val );
384 #else
385                 dn = dn_validate( out->bv_val );
386 #endif
387
388                 if( dn == NULL ) {
389                         ber_bvfree( out );
390                         return LDAP_INVALID_SYNTAX;
391                 }
392
393                 dnlen = strlen(dn);
394
395                 if( uidlen ) {
396                         /* restore the separator */
397                         *uid = '#';
398                         /* shift the UID */
399                         SAFEMEMCPY( &dn[dnlen], uid, uidlen );
400                 }
401
402                 out->bv_val = dn;
403                 out->bv_len = dnlen + uidlen;
404         }
405
406         *normalized = out;
407         return LDAP_SUCCESS;
408 }
409
410 static int
411 inValidate(
412         Syntax *syntax,
413         struct berval *in )
414 {
415         /* any value allowed */
416         return LDAP_OTHER;
417 }
418
419 static int
420 blobValidate(
421         Syntax *syntax,
422         struct berval *in )
423 {
424         /* any value allowed */
425         return LDAP_SUCCESS;
426 }
427
428 static int
429 bitStringValidate(
430         Syntax *syntax,
431         struct berval *in )
432 {
433         ber_len_t i;
434
435         /* very unforgiving validation, requires no normalization
436          * before simplistic matching
437          */
438         if( in->bv_len < 3 ) {
439                 return LDAP_INVALID_SYNTAX;
440         }
441
442         if( in->bv_val[0] != 'B' ||
443                 in->bv_val[1] != '\'' ||
444                 in->bv_val[in->bv_len-1] != '\'' )
445         {
446                 return LDAP_INVALID_SYNTAX;
447         }
448
449         for( i=in->bv_len-2; i>1; i-- ) {
450                 if( in->bv_val[i] != '0' && in->bv_val[i] != '1' ) {
451                         return LDAP_INVALID_SYNTAX;
452                 }
453         }
454
455         return LDAP_SUCCESS;
456 }
457
458 /*
459  * Handling boolean syntax and matching is quite rigid.
460  * A more flexible approach would be to allow a variety
461  * of strings to be normalized and prettied into TRUE
462  * and FALSE.
463  */
464 static int
465 booleanValidate(
466         Syntax *syntax,
467         struct berval *in )
468 {
469         /* very unforgiving validation, requires no normalization
470          * before simplistic matching
471          */
472
473         if( in->bv_len == 4 ) {
474                 if( !memcmp( in->bv_val, "TRUE", 4 ) ) {
475                         return LDAP_SUCCESS;
476                 }
477         } else if( in->bv_len == 5 ) {
478                 if( !memcmp( in->bv_val, "FALSE", 5 ) ) {
479                         return LDAP_SUCCESS;
480                 }
481         }
482
483         return LDAP_INVALID_SYNTAX;
484 }
485
486 static int
487 booleanMatch(
488         int *matchp,
489         slap_mask_t flags,
490         Syntax *syntax,
491         MatchingRule *mr,
492         struct berval *value,
493         void *assertedValue )
494 {
495         /* simplistic matching allowed by rigid validation */
496         struct berval *asserted = (struct berval *) assertedValue;
497         *matchp = value->bv_len != asserted->bv_len;
498         return LDAP_SUCCESS;
499 }
500
501 #if UTF8MATCH
502 /* case insensitive UTF8 strncmp with offset for second string */
503 static int
504 UTF8oncasecmp(
505         struct berval *right,
506         struct berval *left,
507         ber_len_t len,
508         ber_len_t offset )
509 {
510         ber_len_t r, l;
511         ber_len_t rlen, llen;
512         ber_len_t rslen, lslen;
513         ldap_unicode_t ru, lu;
514         ldap_unicode_t ruu, luu;
515
516         rslen = len < right->bv_len ? len : right->bv_len;
517         lslen = len + offset < left->bv_len ? len : left->bv_len;
518
519         for( r = 0, l = offset;
520                 r < rslen && l < lslen;
521                 r+=rlen, l+=llen )
522         {
523                 /*
524                  * XXYYZ: we convert to ucs4 even though -llunicode
525                  * expects ucs2 in an unsigned long
526                  */
527                 ru = ldap_utf8_to_ucs4( &right->bv_val[r] );
528                 if( ru == LDAP_UCS4_INVALID ) {
529                         return 1;
530                 }
531
532                 lu = ldap_utf8_to_ucs4( &left->bv_val[l] );
533                 if( lu == LDAP_UCS4_INVALID ) {
534                         return -1;
535                 }
536
537                 ruu = uctoupper( ru );
538                 luu = uctoupper( lu );
539
540                 if( ruu > luu ) {
541                         return 1;
542                 } else if( luu > ruu ) {
543                         return -1;
544                 }
545
546                 rlen = LDAP_UTF8_CHARLEN( &right->bv_val[r] );
547                 llen = LDAP_UTF8_CHARLEN( &left->bv_val[l] );
548         }
549
550         if( r < rslen ) {
551                 /* less left */
552                 return -1;
553         }
554
555         if( l < lslen ) {
556                 /* less right */
557                 return 1;
558         }
559
560         return 0;
561 }
562
563 static char *UTF8casechr( const char *str, const char *c )
564 {
565         char *p, *lower, *upper;
566         ldap_ucs4_t tch, ch = ldap_utf8_to_ucs4(c);
567
568         tch = uctolower ( ch );
569         for( p = (char *) str; *p != '\0'; LDAP_UTF8_INCR(p) ) {
570                 if( ldap_utf8_to_ucs4( p ) == tch ) {
571                         break;
572                 } 
573         }
574         lower = *p != '\0' ? p : NULL;
575
576         tch = uctoupper ( ch );
577         for( p = (char *) str; *p != '\0'; LDAP_UTF8_INCR(p) ) {
578                 if( ldap_utf8_to_ucs4( p ) == tch ) {
579                         break;
580                 } 
581         }
582         upper = *p != '\0' ? p : NULL;
583         
584         if( lower && upper ) {
585                 return lower < upper ? lower : upper;
586         } else if ( lower ) {
587                 return lower;
588         } else {
589                 return upper;
590         }
591 }
592 #endif
593
594 static int
595 UTF8StringValidate(
596         Syntax *syntax,
597         struct berval *in )
598 {
599         ber_len_t count;
600         int len;
601         unsigned char *u = in->bv_val;
602
603         if( !in->bv_len ) return LDAP_INVALID_SYNTAX;
604
605         for( count = in->bv_len; count > 0; count-=len, u+=len ) {
606                 /* get the length indicated by the first byte */
607                 len = LDAP_UTF8_CHARLEN( u );
608
609                 /* should not be zero */
610                 if( len == 0 ) return LDAP_INVALID_SYNTAX;
611
612                 /* make sure len corresponds with the offset
613                         to the next character */
614                 if( LDAP_UTF8_OFFSET( u ) != len ) return LDAP_INVALID_SYNTAX;
615         }
616
617         if( count != 0 ) return LDAP_INVALID_SYNTAX;
618
619         return LDAP_SUCCESS;
620 }
621
622 static int
623 UTF8StringNormalize(
624         Syntax *syntax,
625         struct berval *val,
626         struct berval **normalized )
627 {
628         struct berval *newval;
629         char *p, *q, *s;
630
631         newval = ch_malloc( sizeof( struct berval ) );
632
633         p = val->bv_val;
634
635         /* Ignore initial whitespace */
636         while ( ldap_utf8_isspace( p ) ) {
637                 LDAP_UTF8_INCR( p );
638         }
639
640         if( *p == '\0' ) {
641                 ch_free( newval );
642                 return LDAP_INVALID_SYNTAX;
643         }
644
645         newval->bv_val = ch_strdup( p );
646         p = q = newval->bv_val;
647         s = NULL;
648
649         while ( *p ) {
650                 int len;
651
652                 if ( ldap_utf8_isspace( p ) ) {
653                         len = LDAP_UTF8_COPY(q,p);
654                         s=q;
655                         p+=len;
656                         q+=len;
657
658                         /* Ignore the extra whitespace */
659                         while ( ldap_utf8_isspace( p ) ) {
660                                 LDAP_UTF8_INCR( p );
661                         }
662                 } else {
663                         len = LDAP_UTF8_COPY(q,p);
664                         s=NULL;
665                         p+=len;
666                         q+=len;
667                 }
668         }
669
670         assert( *newval->bv_val );
671         assert( newval->bv_val < p );
672         assert( q <= p );
673
674         /* cannot start with a space */
675         assert( !ldap_utf8_isspace(newval->bv_val) );
676
677         /*
678          * If the string ended in space, backup the pointer one
679          * position.  One is enough because the above loop collapsed
680          * all whitespace to a single space.
681          */
682
683         if ( s != NULL ) {
684                 q = s;
685         }
686
687         /* cannot end with a space */
688         assert( !ldap_utf8_isspace( LDAP_UTF8_PREV(q) ) );
689
690         /* null terminate */
691         *q = '\0';
692
693         newval->bv_len = q - newval->bv_val;
694         *normalized = newval;
695
696         return LDAP_SUCCESS;
697 }
698
699 #if defined(SLAPD_APPROX_MULTISTRING)
700
701 #if defined(SLAPD_APPROX_INITIALS)
702 #define SLAPD_APPROX_DELIMITER "._ "
703 #define SLAPD_APPROX_WORDLEN 2
704 #else
705 #define SLAPD_APPROX_DELIMITER " "
706 #define SLAPD_APPROX_WORDLEN 1
707 #endif
708
709 static int
710 approxMatch(
711         int *matchp,
712         slap_mask_t flags,
713         Syntax *syntax,
714         MatchingRule *mr,
715         struct berval *value,
716         void *assertedValue )
717 {
718         char *val, *assertv, **values, **words, *c;
719         int i, count, len, nextchunk=0, nextavail=0;
720
721
722         /* Isolate how many words there are */
723         val = ch_strdup( value->bv_val );
724         for( c=val,count=1; *c; c++ ) {
725                 c = strpbrk( c, SLAPD_APPROX_DELIMITER );
726                 if ( c == NULL ) break;
727                 *c = '\0';
728                 count++;
729         }
730
731         /* Get a phonetic copy of each word */
732         words = (char **)ch_malloc( count * sizeof(char *) );
733         values = (char **)ch_malloc( count * sizeof(char *) );
734         for( c=val,i=0;  i<count;  i++,c+=strlen(c)+1 ) {
735                 words[i] = c;
736                 values[i] = phonetic(c);
737         }
738
739
740         /* Work through the asserted value's words, to see if  at least some
741            of the words are there, in the same order. */
742         assertv = ch_strdup( ((struct berval *)assertedValue)->bv_val );
743         len = 0;
744         while ( nextchunk < ((struct berval *)assertedValue)->bv_len ) {
745                 len = strcspn( assertv + nextchunk, SLAPD_APPROX_DELIMITER);
746                 if( len == 0 ) {
747                         nextchunk++;
748                         continue;
749                 }
750 #if defined(SLAPD_APPROX_INITIALS)
751                 else if( len == 1 ) {
752                         /* Single letter words need to at least match one word's initial */
753                         for( i=nextavail; i<count; i++ )
754                                 if( !strncasecmp( assertv+nextchunk, words[i], 1 )) {
755                                         nextavail=i+1;
756                                         break;
757                                 }
758                 }
759 #endif
760                 else {
761                         /* Isolate the next word in the asserted value and phonetic it */
762                         assertv[nextchunk+len] = '\0';
763                         val = phonetic( assertv + nextchunk );
764
765                         /* See if this phonetic chunk is in the remaining words of *value */
766                         for( i=nextavail; i<count; i++ ){
767                                 if( !strcmp( val, values[i] ) ){
768                                         nextavail = i+1;
769                                         break;
770                                 }
771                         }
772                 }
773
774                 /* This chunk in the asserted value was NOT within the *value. */
775                 if( i >= count ) {
776                         nextavail=-1;
777                         break;
778                 }
779
780                 /* Go on to the next word in the asserted value */
781                 nextchunk += len+1;
782         }
783
784         /* If some of the words were seen, call it a match */
785         if( nextavail > 0 ) {
786                 *matchp = 0;
787         }
788         else {
789                 *matchp = 1;
790         }
791
792         /* Cleanup allocs */
793         ch_free( assertv );
794         for( i=0; i<count; i++ ) {
795                 ch_free( values[i] );
796         }
797         ch_free( values );
798         ch_free( words );
799         ch_free( val );
800
801         return LDAP_SUCCESS;
802 }
803
804
805 int 
806 approxIndexer(
807         slap_mask_t use,
808         slap_mask_t flags,
809         Syntax *syntax,
810         MatchingRule *mr,
811         struct berval *prefix,
812         struct berval **values,
813         struct berval ***keysp )
814 {
815         char *val, *c;
816         int i,j, len, wordcount, keycount=0;
817         struct berval **newkeys, **keys=NULL;
818
819
820         for( j=0; values[j] != NULL; j++ ) {
821
822                 /* Isolate how many words there are. There will be a key for each */
823                 val = ch_strdup( values[j]->bv_val );
824                 for( wordcount=0,c=val;  *c;  c++) {
825                         len = strcspn(c, SLAPD_APPROX_DELIMITER);
826                         if( len >= SLAPD_APPROX_WORDLEN ) wordcount++;
827                         c+= len;
828                         if (*c == '\0') break;
829                         *c = '\0';
830                 }
831
832                 /* Allocate/increase storage to account for new keys */
833                 newkeys = (struct berval **)ch_malloc( (keycount + wordcount + 1) 
834                    * sizeof(struct berval *) );
835                 memcpy( newkeys, keys, keycount * sizeof(struct berval *) );
836                 if( keys ) ch_free( keys );
837                 keys = newkeys;
838
839                 /* Get a phonetic copy of each word */
840                 for( c=val,i=0;  i<wordcount;  c+=len+1  ) {
841                         len = strlen( c );
842                         if( len < SLAPD_APPROX_WORDLEN ) continue;
843                         keys[keycount] = (struct berval *)ch_malloc( sizeof(struct berval) );
844                         keys[keycount]->bv_val = phonetic( c );
845                         keys[keycount]->bv_len = strlen( keys[keycount]->bv_val );
846                         keycount++;
847                         i++;
848                 }
849
850                 ch_free( val );
851         }
852         keys[keycount] = NULL;
853         *keysp = keys;
854
855         return LDAP_SUCCESS;
856 }
857
858
859 int 
860 approxFilter(
861         slap_mask_t use,
862         slap_mask_t flags,
863         Syntax *syntax,
864         MatchingRule *mr,
865         struct berval *prefix,
866         void * assertValue,
867         struct berval ***keysp )
868 {
869         char *val, *c;
870         int i, count, len;
871         struct berval **keys;
872
873
874         /* Isolate how many words there are. There will be a key for each */
875         val = ch_strdup( ((struct berval *)assertValue)->bv_val );
876         for( count=0,c=val;  *c;  c++) {
877                 len = strcspn(c, SLAPD_APPROX_DELIMITER);
878                 if( len >= SLAPD_APPROX_WORDLEN ) count++;
879                 c+= len;
880                 if (*c == '\0') break;
881                 *c = '\0';
882         }
883
884         /* Allocate storage for new keys */
885         keys = (struct berval **)ch_malloc( (count + 1) * sizeof(struct berval *) );
886
887         /* Get a phonetic copy of each word */
888         for( c=val,i=0;  i<count; c+=len+1 ) {
889                 len = strlen(c);
890                 if( len < SLAPD_APPROX_WORDLEN ) continue;
891                 keys[i] = (struct berval *)ch_malloc( sizeof(struct berval) );
892                 keys[i]->bv_val = phonetic( c );
893                 keys[i]->bv_len = strlen( keys[i]->bv_val );
894                 i++;
895         }
896
897         ch_free( val );
898
899         keys[count] = NULL;
900         *keysp = keys;
901
902         return LDAP_SUCCESS;
903 }
904
905
906 #else
907 /* No other form of Approximate Matching is defined */
908
909 static int
910 approxMatch(
911         int *matchp,
912         slap_mask_t flags,
913         Syntax *syntax,
914         MatchingRule *mr,
915         struct berval *value,
916         void *assertedValue )
917 {
918         char *vapprox, *avapprox;
919
920         vapprox = phonetic( value->bv_val );
921         avapprox = phonetic( ((struct berval *)assertedValue)->bv_val);
922
923         *matchp = strcmp( vapprox, avapprox );
924
925         ch_free( vapprox );
926         ch_free( avapprox );
927
928         return LDAP_SUCCESS;
929 }
930
931 int 
932 approxIndexer(
933         slap_mask_t use,
934         slap_mask_t flags,
935         Syntax *syntax,
936         MatchingRule *mr,
937         struct berval *prefix,
938         struct berval **values,
939         struct berval ***keysp )
940 {
941         int i;
942         struct berval **keys;
943
944
945         for( i=0; values[i] != NULL; i++ ) {
946                 /* just count them */
947         }
948         assert( i > 0 );
949
950         keys = (struct berval **)ch_malloc( sizeof( struct berval * ) * (i+1) );
951
952         /* Copy each value and run it through phonetic() */
953         for( i=0; values[i] != NULL; i++ ) {
954                 keys[i] = ch_malloc( sizeof( struct berval * ) );
955                 keys[i]->bv_val = phonetic( values[i]->bv_val );
956                 keys[i]->bv_len = strlen( keys[i]->bv_val );
957         }
958         keys[i] = NULL;
959
960         *keysp = keys;
961         return LDAP_SUCCESS;
962 }
963
964
965 int 
966 approxFilter(
967         slap_mask_t use,
968         slap_mask_t flags,
969         Syntax *syntax,
970         MatchingRule *mr,
971         struct berval *prefix,
972         void * assertValue,
973         struct berval ***keysp )
974 {
975         struct berval **keys;
976
977
978         keys = (struct berval **)ch_malloc( sizeof( struct berval * ) * 2 );
979
980         /* Copy the value and run it through phonetic() */
981         keys[0] = ch_malloc( sizeof( struct berval * ) );
982         keys[0]->bv_val = phonetic( ((struct berval *)assertValue)->bv_val );
983         keys[0]->bv_len = strlen( keys[0]->bv_val );
984         keys[1] = NULL;
985
986         *keysp = keys;
987         return LDAP_SUCCESS;
988 }
989 #endif
990
991
992 static int
993 caseExactMatch(
994         int *matchp,
995         slap_mask_t flags,
996         Syntax *syntax,
997         MatchingRule *mr,
998         struct berval *value,
999         void *assertedValue )
1000 {
1001         int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;
1002
1003         if( match == 0 ) {
1004                 match = strncmp( value->bv_val,
1005                         ((struct berval *) assertedValue)->bv_val,
1006                         value->bv_len );
1007         }
1008
1009         *matchp = match;
1010         return LDAP_SUCCESS;
1011 }
1012
1013 static int
1014 caseExactSubstringsMatch(
1015         int *matchp,
1016         slap_mask_t flags,
1017         Syntax *syntax,
1018         MatchingRule *mr,
1019         struct berval *value,
1020         void *assertedValue )
1021 {
1022         int match = 0;
1023         SubstringsAssertion *sub = assertedValue;
1024         struct berval left = *value;
1025         int i;
1026         ber_len_t inlen=0;
1027
1028         /* Add up asserted input length */
1029         if( sub->sa_initial ) {
1030                 inlen += sub->sa_initial->bv_len;
1031         }
1032         if( sub->sa_any ) {
1033                 for(i=0; sub->sa_any[i] != NULL; i++) {
1034                         inlen += sub->sa_any[i]->bv_len;
1035                 }
1036         }
1037         if( sub->sa_final ) {
1038                 inlen += sub->sa_final->bv_len;
1039         }
1040
1041         if( sub->sa_initial ) {
1042                 if( inlen > left.bv_len ) {
1043                         match = 1;
1044                         goto done;
1045                 }
1046
1047                 match = strncmp( sub->sa_initial->bv_val, left.bv_val,
1048                         sub->sa_initial->bv_len );
1049
1050                 if( match != 0 ) {
1051                         goto done;
1052                 }
1053
1054                 left.bv_val += sub->sa_initial->bv_len;
1055                 left.bv_len -= sub->sa_initial->bv_len;
1056                 inlen -= sub->sa_initial->bv_len;
1057         }
1058
1059         if( sub->sa_final ) {
1060                 if( inlen > left.bv_len ) {
1061                         match = 1;
1062                         goto done;
1063                 }
1064
1065                 match = strncmp( sub->sa_final->bv_val,
1066                         &left.bv_val[left.bv_len - sub->sa_final->bv_len],
1067                         sub->sa_final->bv_len );
1068
1069                 if( match != 0 ) {
1070                         goto done;
1071                 }
1072
1073                 left.bv_len -= sub->sa_final->bv_len;
1074                 inlen -= sub->sa_final->bv_len;
1075         }
1076
1077         if( sub->sa_any ) {
1078                 for(i=0; sub->sa_any[i]; i++) {
1079                         ber_len_t idx;
1080                         char *p;
1081
1082 retry:
1083                         if( inlen > left.bv_len ) {
1084                                 /* not enough length */
1085                                 match = 1;
1086                                 goto done;
1087                         }
1088
1089                         if( sub->sa_any[i]->bv_len == 0 ) {
1090                                 continue;
1091                         }
1092
1093                         p = strchr( left.bv_val, *sub->sa_any[i]->bv_val );
1094
1095                         if( p == NULL ) {
1096                                 match = 1;
1097                                 goto done;
1098                         }
1099
1100                         idx = p - left.bv_val;
1101                         assert( idx < left.bv_len );
1102
1103                         if( idx >= left.bv_len ) {
1104                                 /* this shouldn't happen */
1105                                 return LDAP_OTHER;
1106                         }
1107
1108                         left.bv_val = p;
1109                         left.bv_len -= idx;
1110
1111                         if( sub->sa_any[i]->bv_len > left.bv_len ) {
1112                                 /* not enough left */
1113                                 match = 1;
1114                                 goto done;
1115                         }
1116
1117                         match = strncmp( left.bv_val,
1118                                 sub->sa_any[i]->bv_val,
1119                                 sub->sa_any[i]->bv_len );
1120
1121                         if( match != 0 ) {
1122                                 left.bv_val++;
1123                                 left.bv_len--;
1124                                 goto retry;
1125                         }
1126
1127                         left.bv_val += sub->sa_any[i]->bv_len;
1128                         left.bv_len -= sub->sa_any[i]->bv_len;
1129                         inlen -= sub->sa_any[i]->bv_len;
1130                 }
1131         }
1132
1133 done:
1134         *matchp = match;
1135         return LDAP_SUCCESS;
1136 }
1137
1138 /* Index generation function */
1139 int caseExactIndexer(
1140         slap_mask_t use,
1141         slap_mask_t flags,
1142         Syntax *syntax,
1143         MatchingRule *mr,
1144         struct berval *prefix,
1145         struct berval **values,
1146         struct berval ***keysp )
1147 {
1148         int i;
1149         size_t slen, mlen;
1150         struct berval **keys;
1151         HASH_CONTEXT   HASHcontext;
1152         unsigned char   HASHdigest[HASH_BYTES];
1153         struct berval digest;
1154         digest.bv_val = HASHdigest;
1155         digest.bv_len = sizeof(HASHdigest);
1156
1157         /* we should have at least one value at this point */
1158         assert( values != NULL && values[0] != NULL );
1159
1160         for( i=0; values[i] != NULL; i++ ) {
1161                 /* just count them */
1162         }
1163
1164         keys = ch_malloc( sizeof( struct berval * ) * (i+1) );
1165
1166         slen = strlen( syntax->ssyn_oid );
1167         mlen = strlen( mr->smr_oid );
1168
1169         for( i=0; values[i] != NULL; i++ ) {
1170                 struct berval *value;
1171 #if UTF8MATCH
1172                 value = ber_bvstr( UTF8normalize( values[i]->bv_val, UTF8_NOCASEFOLD ) );
1173 #else
1174                 value = values[i];
1175 #endif
1176
1177                 HASH_Init( &HASHcontext );
1178                 if( prefix != NULL && prefix->bv_len > 0 ) {
1179                         HASH_Update( &HASHcontext,
1180                                 prefix->bv_val, prefix->bv_len );
1181                 }
1182                 HASH_Update( &HASHcontext,
1183                         syntax->ssyn_oid, slen );
1184                 HASH_Update( &HASHcontext,
1185                         mr->smr_oid, mlen );
1186                 HASH_Update( &HASHcontext,
1187                         value->bv_val, value->bv_len );
1188                 HASH_Final( HASHdigest, &HASHcontext );
1189
1190 #if UTF8MATCH
1191                 ber_bvfree( value );
1192 #endif
1193
1194                 keys[i] = ber_bvdup( &digest );
1195         }
1196
1197         keys[i] = NULL;
1198         *keysp = keys;
1199         return LDAP_SUCCESS;
1200 }
1201
1202 /* Index generation function */
1203 int caseExactFilter(
1204         slap_mask_t use,
1205         slap_mask_t flags,
1206         Syntax *syntax,
1207         MatchingRule *mr,
1208         struct berval *prefix,
1209         void * assertValue,
1210         struct berval ***keysp )
1211 {
1212         size_t slen, mlen;
1213         struct berval **keys;
1214         HASH_CONTEXT   HASHcontext;
1215         unsigned char   HASHdigest[HASH_BYTES];
1216         struct berval *value;
1217         struct berval digest;
1218         digest.bv_val = HASHdigest;
1219         digest.bv_len = sizeof(HASHdigest);
1220
1221         slen = strlen( syntax->ssyn_oid );
1222         mlen = strlen( mr->smr_oid );
1223
1224 #if UTF8MATCH
1225         value = ber_bvstr( UTF8normalize( ((struct berval *) assertValue)->bv_val, UTF8_NOCASEFOLD ) );
1226 #else
1227         value = (struct berval *) assertValue;
1228 #endif  
1229
1230         keys = ch_malloc( sizeof( struct berval * ) * 2 );
1231
1232         HASH_Init( &HASHcontext );
1233         if( prefix != NULL && prefix->bv_len > 0 ) {
1234                 HASH_Update( &HASHcontext,
1235                         prefix->bv_val, prefix->bv_len );
1236         }
1237         HASH_Update( &HASHcontext,
1238                 syntax->ssyn_oid, slen );
1239         HASH_Update( &HASHcontext,
1240                 mr->smr_oid, mlen );
1241         HASH_Update( &HASHcontext,
1242                 value->bv_val, value->bv_len );
1243         HASH_Final( HASHdigest, &HASHcontext );
1244
1245         keys[0] = ber_bvdup( &digest );
1246         keys[1] = NULL;
1247
1248 #if UTF8MATCH
1249         ber_bvfree( value );
1250 #endif
1251
1252         *keysp = keys;
1253         return LDAP_SUCCESS;
1254 }
1255
1256 /* Substrings Index generation function */
1257 int caseExactSubstringsIndexer(
1258         slap_mask_t use,
1259         slap_mask_t flags,
1260         Syntax *syntax,
1261         MatchingRule *mr,
1262         struct berval *prefix,
1263         struct berval **values,
1264         struct berval ***keysp )
1265 {
1266         ber_len_t i, nkeys;
1267         size_t slen, mlen;
1268         struct berval **keys;
1269         HASH_CONTEXT   HASHcontext;
1270         unsigned char   HASHdigest[HASH_BYTES];
1271         struct berval digest;
1272         digest.bv_val = HASHdigest;
1273         digest.bv_len = sizeof(HASHdigest);
1274
1275         /* we should have at least one value at this point */
1276         assert( values != NULL && values[0] != NULL );
1277
1278         nkeys=0;
1279         for( i=0; values[i] != NULL; i++ ) {
1280                 /* count number of indices to generate */
1281                 if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) {
1282                         continue;
1283                 }
1284
1285                 if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
1286                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
1287                                 nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
1288                                         ( SLAP_INDEX_SUBSTR_MINLEN - 1);
1289                         } else {
1290                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
1291                         }
1292                 }
1293
1294                 if( flags & SLAP_INDEX_SUBSTR_ANY ) {
1295                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
1296                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
1297                         }
1298                 }
1299
1300                 if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
1301                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
1302                                 nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
1303                                         ( SLAP_INDEX_SUBSTR_MINLEN - 1);
1304                         } else {
1305                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
1306                         }
1307                 }
1308         }
1309
1310         if( nkeys == 0 ) {
1311                 /* no keys to generate */
1312                 *keysp = NULL;
1313                 return LDAP_SUCCESS;
1314         }
1315
1316         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
1317
1318         slen = strlen( syntax->ssyn_oid );
1319         mlen = strlen( mr->smr_oid );
1320
1321         nkeys=0;
1322         for( i=0; values[i] != NULL; i++ ) {
1323                 ber_len_t j,max;
1324                 struct berval *value;
1325
1326                 if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) continue;
1327
1328 #if UTF8MATCH
1329                 value = ber_bvstr( UTF8normalize( values[i]->bv_val, UTF8_NOCASEFOLD ) );
1330 #else
1331                 value = values[i];
1332 #endif
1333
1334                 if( ( flags & SLAP_INDEX_SUBSTR_ANY ) &&
1335                         ( value->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) )
1336                 {
1337                         char pre = SLAP_INDEX_SUBSTR_PREFIX;
1338                         max = value->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1);
1339
1340                         for( j=0; j<max; j++ ) {
1341                                 HASH_Init( &HASHcontext );
1342                                 if( prefix != NULL && prefix->bv_len > 0 ) {
1343                                         HASH_Update( &HASHcontext,
1344                                                 prefix->bv_val, prefix->bv_len );
1345                                 }
1346
1347                                 HASH_Update( &HASHcontext,
1348                                         &pre, sizeof( pre ) );
1349                                 HASH_Update( &HASHcontext,
1350                                         syntax->ssyn_oid, slen );
1351                                 HASH_Update( &HASHcontext,
1352                                         mr->smr_oid, mlen );
1353                                 HASH_Update( &HASHcontext,
1354                                         &value->bv_val[j],
1355                                         SLAP_INDEX_SUBSTR_MAXLEN );
1356                                 HASH_Final( HASHdigest, &HASHcontext );
1357
1358                                 keys[nkeys++] = ber_bvdup( &digest );
1359                         }
1360                 }
1361
1362                 max = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
1363                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
1364
1365                 for( j=SLAP_INDEX_SUBSTR_MINLEN; j<=max; j++ ) {
1366                         char pre;
1367
1368                         if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
1369                                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
1370                                 HASH_Init( &HASHcontext );
1371                                 if( prefix != NULL && prefix->bv_len > 0 ) {
1372                                         HASH_Update( &HASHcontext,
1373                                                 prefix->bv_val, prefix->bv_len );
1374                                 }
1375                                 HASH_Update( &HASHcontext,
1376                                         &pre, sizeof( pre ) );
1377                                 HASH_Update( &HASHcontext,
1378                                         syntax->ssyn_oid, slen );
1379                                 HASH_Update( &HASHcontext,
1380                                         mr->smr_oid, mlen );
1381                                 HASH_Update( &HASHcontext,
1382                                         value->bv_val, j );
1383                                 HASH_Final( HASHdigest, &HASHcontext );
1384
1385                                 keys[nkeys++] = ber_bvdup( &digest );
1386                         }
1387
1388                         if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
1389                                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
1390                                 HASH_Init( &HASHcontext );
1391                                 if( prefix != NULL && prefix->bv_len > 0 ) {
1392                                         HASH_Update( &HASHcontext,
1393                                                 prefix->bv_val, prefix->bv_len );
1394                                 }
1395                                 HASH_Update( &HASHcontext,
1396                                         &pre, sizeof( pre ) );
1397                                 HASH_Update( &HASHcontext,
1398                                         syntax->ssyn_oid, slen );
1399                                 HASH_Update( &HASHcontext,
1400                                         mr->smr_oid, mlen );
1401                                 HASH_Update( &HASHcontext,
1402                                         &value->bv_val[value->bv_len-j], j );
1403                                 HASH_Final( HASHdigest, &HASHcontext );
1404
1405                                 keys[nkeys++] = ber_bvdup( &digest );
1406                         }
1407
1408                 }
1409
1410 #if UTF8MATCH
1411                 ber_bvfree( value );
1412 #endif
1413
1414         }
1415
1416         if( nkeys > 0 ) {
1417                 keys[nkeys] = NULL;
1418                 *keysp = keys;
1419         } else {
1420                 ch_free( keys );
1421                 *keysp = NULL;
1422         }
1423
1424         return LDAP_SUCCESS;
1425 }
1426
1427 int caseExactSubstringsFilter(
1428         slap_mask_t use,
1429         slap_mask_t flags,
1430         Syntax *syntax,
1431         MatchingRule *mr,
1432         struct berval *prefix,
1433         void * assertValue,
1434         struct berval ***keysp )
1435 {
1436         SubstringsAssertion *sa = assertValue;
1437         char pre;
1438         ber_len_t nkeys = 0;
1439         size_t slen, mlen, klen;
1440         struct berval **keys;
1441         HASH_CONTEXT   HASHcontext;
1442         unsigned char   HASHdigest[HASH_BYTES];
1443         struct berval *value;
1444         struct berval digest;
1445
1446         if( flags & SLAP_INDEX_SUBSTR_INITIAL && sa->sa_initial != NULL &&
1447                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1448         {
1449                 nkeys++;
1450         }
1451
1452         if( flags & SLAP_INDEX_SUBSTR_ANY && sa->sa_any != NULL ) {
1453                 ber_len_t i;
1454                 for( i=0; sa->sa_any[i] != NULL; i++ ) {
1455                         if( sa->sa_any[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
1456                                 /* don't bother accounting for stepping */
1457                                 nkeys += sa->sa_any[i]->bv_len -
1458                                         ( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
1459                         }
1460                 }
1461         }
1462
1463         if( flags & SLAP_INDEX_SUBSTR_FINAL && sa->sa_final != NULL &&
1464                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1465         {
1466                 nkeys++;
1467         }
1468
1469         if( nkeys == 0 ) {
1470                 *keysp = NULL;
1471                 return LDAP_SUCCESS;
1472         }
1473
1474         digest.bv_val = HASHdigest;
1475         digest.bv_len = sizeof(HASHdigest);
1476
1477         slen = strlen( syntax->ssyn_oid );
1478         mlen = strlen( mr->smr_oid );
1479
1480         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
1481         nkeys = 0;
1482
1483         if( flags & SLAP_INDEX_SUBSTR_INITIAL && sa->sa_initial != NULL &&
1484                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1485         {
1486                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
1487 #if UTF8MATCH
1488                 value = ber_bvstr( UTF8normalize( sa->sa_initial->bv_val, UTF8_NOCASEFOLD ) );
1489 #else
1490                 value = sa->sa_initial;
1491 #endif
1492
1493                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
1494                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
1495
1496                 HASH_Init( &HASHcontext );
1497                 if( prefix != NULL && prefix->bv_len > 0 ) {
1498                         HASH_Update( &HASHcontext,
1499                                 prefix->bv_val, prefix->bv_len );
1500                 }
1501                 HASH_Update( &HASHcontext,
1502                         &pre, sizeof( pre ) );
1503                 HASH_Update( &HASHcontext,
1504                         syntax->ssyn_oid, slen );
1505                 HASH_Update( &HASHcontext,
1506                         mr->smr_oid, mlen );
1507                 HASH_Update( &HASHcontext,
1508                         value->bv_val, klen );
1509                 HASH_Final( HASHdigest, &HASHcontext );
1510
1511 #if UTF8MATCH
1512                 ber_bvfree( value );
1513 #endif
1514                 keys[nkeys++] = ber_bvdup( &digest );
1515         }
1516
1517         if( flags & SLAP_INDEX_SUBSTR_ANY && sa->sa_any != NULL ) {
1518                 ber_len_t i, j;
1519                 pre = SLAP_INDEX_SUBSTR_PREFIX;
1520                 klen = SLAP_INDEX_SUBSTR_MAXLEN;
1521
1522                 for( i=0; sa->sa_any[i] != NULL; i++ ) {
1523                         if( sa->sa_any[i]->bv_len < SLAP_INDEX_SUBSTR_MAXLEN ) {
1524                                 continue;
1525                         }
1526
1527 #if UTF8MATCH
1528                         value = ber_bvstr( UTF8normalize( sa->sa_any[i]->bv_val, UTF8_NOCASEFOLD ) );
1529 #else
1530                         value = sa->sa_any[i];
1531 #endif
1532
1533                         for(j=0;
1534                                 j <= value->bv_len - SLAP_INDEX_SUBSTR_MAXLEN;
1535                                 j += SLAP_INDEX_SUBSTR_STEP )
1536                         {
1537                                 HASH_Init( &HASHcontext );
1538                                 if( prefix != NULL && prefix->bv_len > 0 ) {
1539                                         HASH_Update( &HASHcontext,
1540                                                 prefix->bv_val, prefix->bv_len );
1541                                 }
1542                                 HASH_Update( &HASHcontext,
1543                                         &pre, sizeof( pre ) );
1544                                 HASH_Update( &HASHcontext,
1545                                         syntax->ssyn_oid, slen );
1546                                 HASH_Update( &HASHcontext,
1547                                         mr->smr_oid, mlen );
1548                                 HASH_Update( &HASHcontext,
1549                                         &value->bv_val[j], klen ); 
1550                                 HASH_Final( HASHdigest, &HASHcontext );
1551
1552                                 keys[nkeys++] = ber_bvdup( &digest );
1553                         }
1554
1555 #if UTF8MATCH
1556                         ber_bvfree( value );
1557 #endif
1558                 }
1559         }
1560
1561         if( flags & SLAP_INDEX_SUBSTR_FINAL && sa->sa_final != NULL &&
1562                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1563         {
1564                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
1565 #if UTF8MATCH
1566                 value = ber_bvstr( UTF8normalize( sa->sa_final->bv_val, UTF8_NOCASEFOLD ) );
1567 #else
1568                 value = sa->sa_final;
1569 #endif
1570
1571                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
1572                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
1573
1574                 HASH_Init( &HASHcontext );
1575                 if( prefix != NULL && prefix->bv_len > 0 ) {
1576                         HASH_Update( &HASHcontext,
1577                                 prefix->bv_val, prefix->bv_len );
1578                 }
1579                 HASH_Update( &HASHcontext,
1580                         &pre, sizeof( pre ) );
1581                 HASH_Update( &HASHcontext,
1582                         syntax->ssyn_oid, slen );
1583                 HASH_Update( &HASHcontext,
1584                         mr->smr_oid, mlen );
1585                 HASH_Update( &HASHcontext,
1586                         &value->bv_val[value->bv_len-klen], klen );
1587                 HASH_Final( HASHdigest, &HASHcontext );
1588
1589 #if UTF8MATCH
1590                 ber_bvfree( value );
1591 #endif
1592                 keys[nkeys++] = ber_bvdup( &digest );
1593         }
1594
1595         if( nkeys > 0 ) {
1596                 keys[nkeys] = NULL;
1597                 *keysp = keys;
1598         } else {
1599                 ch_free( keys );
1600                 *keysp = NULL;
1601         }
1602
1603         return LDAP_SUCCESS;
1604 }
1605         
1606 static int
1607 caseIgnoreMatch(
1608         int *matchp,
1609         slap_mask_t flags,
1610         Syntax *syntax,
1611         MatchingRule *mr,
1612         struct berval *value,
1613         void *assertedValue )
1614 {
1615 #if UTF8MATCH
1616         *matchp = UTF8normcmp( value->bv_val,
1617                                ((struct berval *) assertedValue)->bv_val,
1618                                UTF8_CASEFOLD );
1619 #else
1620         int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;
1621
1622         if( match == 0 ) {
1623                 match = strncasecmp( value->bv_val,
1624                         ((struct berval *) assertedValue)->bv_val,
1625                         value->bv_len );
1626         }
1627
1628         *matchp = match;
1629 #endif
1630         return LDAP_SUCCESS;
1631 }
1632
1633 static int
1634 caseIgnoreSubstringsMatch(
1635         int *matchp,
1636         slap_mask_t flags,
1637         Syntax *syntax,
1638         MatchingRule *mr,
1639         struct berval *value,
1640         void *assertedValue )
1641 {
1642         int match = 0;
1643         SubstringsAssertion *sub = assertedValue;
1644         struct berval left = *value;
1645         int i;
1646         ber_len_t inlen=0;
1647
1648         /* Add up asserted input length */
1649         if( sub->sa_initial ) {
1650                 inlen += sub->sa_initial->bv_len;
1651         }
1652         if( sub->sa_any ) {
1653                 for(i=0; sub->sa_any[i] != NULL; i++) {
1654                         inlen += sub->sa_any[i]->bv_len;
1655                 }
1656         }
1657         if( sub->sa_final ) {
1658                 inlen += sub->sa_final->bv_len;
1659         }
1660
1661         if( sub->sa_initial ) {
1662                 if( inlen > left.bv_len ) {
1663                         match = 1;
1664                         goto done;
1665                 }
1666
1667 #if UTF8MATCH
1668                 match = UTF8oncasecmp( sub->sa_initial, &left,
1669                                      sub->sa_initial->bv_len, 0 );
1670 #else           
1671                 match = strncasecmp( sub->sa_initial->bv_val, left.bv_val,
1672                         sub->sa_initial->bv_len );
1673 #endif
1674
1675                 if( match != 0 ) {
1676                         goto done;
1677                 }
1678
1679                 left.bv_val += sub->sa_initial->bv_len;
1680                 left.bv_len -= sub->sa_initial->bv_len;
1681                 inlen -= sub->sa_initial->bv_len;
1682         }
1683
1684         if( sub->sa_final ) {
1685                 if( inlen > left.bv_len ) {
1686                         match = 1;
1687                         goto done;
1688                 }
1689
1690 #if UTF8MATCH
1691                 match = UTF8oncasecmp( sub->sa_final, &left,
1692                                        sub->sa_final->bv_len,
1693                                        left.bv_len - sub->sa_final->bv_len );
1694 #else           
1695                 match = strncasecmp( sub->sa_final->bv_val,
1696                         &left.bv_val[left.bv_len - sub->sa_final->bv_len],
1697                         sub->sa_final->bv_len );
1698 #endif
1699
1700                 if( match != 0 ) {
1701                         goto done;
1702                 }
1703
1704                 left.bv_len -= sub->sa_final->bv_len;
1705                 inlen -= sub->sa_final->bv_len;
1706         }
1707
1708         if( sub->sa_any ) {
1709                 for(i=0; sub->sa_any[i]; i++) {
1710                         ber_len_t idx;
1711                         char *p;
1712
1713 retry:
1714                         if( inlen > left.bv_len ) {
1715                                 /* not enough length */
1716                                 match = 1;
1717                                 goto done;
1718                         }
1719
1720                         if( sub->sa_any[i]->bv_len == 0 ) {
1721                                 continue;
1722                         }
1723
1724 #if UTF8MATCH
1725                         p = UTF8casechr( left.bv_val, sub->sa_any[i]->bv_val );
1726 #else
1727                         p = strcasechr( left.bv_val, *sub->sa_any[i]->bv_val );
1728 #endif
1729
1730                         if( p == NULL ) {
1731                                 match = 1;
1732                                 goto done;
1733                         }
1734
1735                         idx = p - left.bv_val;
1736                         assert( idx < left.bv_len );
1737
1738                         if( idx >= left.bv_len ) {
1739                                 /* this shouldn't happen */
1740                                 return LDAP_OTHER;
1741                         }
1742
1743                         left.bv_val = p;
1744                         left.bv_len -= idx;
1745
1746                         if( sub->sa_any[i]->bv_len > left.bv_len ) {
1747                                 /* not enough left */
1748                                 match = 1;
1749                                 goto done;
1750                         }
1751
1752 #if UTF8MATCH
1753                         match = UTF8oncasecmp( &left, sub->sa_any[i],
1754                                                sub->sa_any[i]->bv_len, 0 );
1755
1756                         if( match != 0 ) {
1757                                 int len = LDAP_UTF8_CHARLEN( left.bv_val );
1758                                 left.bv_val += len;
1759                                 left.bv_len -= len;
1760                                 goto retry;
1761                         }
1762 #else                   
1763                         match = strncasecmp( left.bv_val,
1764                                 sub->sa_any[i]->bv_val,
1765                                 sub->sa_any[i]->bv_len );
1766
1767                         if( match != 0 ) {
1768                                 left.bv_val++;
1769                                 left.bv_len--;
1770
1771                                 goto retry;
1772                         }
1773 #endif
1774
1775                         left.bv_val += sub->sa_any[i]->bv_len;
1776                         left.bv_len -= sub->sa_any[i]->bv_len;
1777                         inlen -= sub->sa_any[i]->bv_len;
1778                 }
1779         }
1780
1781 done:
1782         *matchp = match;
1783         return LDAP_SUCCESS;
1784 }
1785
1786 /* Index generation function */
1787 int caseIgnoreIndexer(
1788         slap_mask_t use,
1789         slap_mask_t flags,
1790         Syntax *syntax,
1791         MatchingRule *mr,
1792         struct berval *prefix,
1793         struct berval **values,
1794         struct berval ***keysp )
1795 {
1796         int i;
1797         size_t slen, mlen;
1798         struct berval **keys;
1799         HASH_CONTEXT   HASHcontext;
1800         unsigned char   HASHdigest[HASH_BYTES];
1801         struct berval digest;
1802         digest.bv_val = HASHdigest;
1803         digest.bv_len = sizeof(HASHdigest);
1804
1805         /* we should have at least one value at this point */
1806         assert( values != NULL && values[0] != NULL );
1807
1808         for( i=0; values[i] != NULL; i++ ) {
1809                 /* just count them */
1810         }
1811
1812         keys = ch_malloc( sizeof( struct berval * ) * (i+1) );
1813
1814         slen = strlen( syntax->ssyn_oid );
1815         mlen = strlen( mr->smr_oid );
1816
1817         for( i=0; values[i] != NULL; i++ ) {
1818                 struct berval *value;
1819 #if UTF8MATCH
1820                 value = ber_bvstr( UTF8normalize( values[i]->bv_val, UTF8_CASEFOLD ) );
1821 #else
1822                 value = ber_bvdup( values[i] );
1823                 ldap_pvt_str2upper( value->bv_val );
1824 #endif
1825                 HASH_Init( &HASHcontext );
1826                 if( prefix != NULL && prefix->bv_len > 0 ) {
1827                         HASH_Update( &HASHcontext,
1828                                 prefix->bv_val, prefix->bv_len );
1829                 }
1830                 HASH_Update( &HASHcontext,
1831                         syntax->ssyn_oid, slen );
1832                 HASH_Update( &HASHcontext,
1833                         mr->smr_oid, mlen );
1834                 HASH_Update( &HASHcontext,
1835                         value->bv_val, value->bv_len );
1836                 HASH_Final( HASHdigest, &HASHcontext );
1837
1838                 ber_bvfree( value );
1839
1840                 keys[i] = ber_bvdup( &digest );
1841         }
1842
1843         keys[i] = NULL;
1844         *keysp = keys;
1845         return LDAP_SUCCESS;
1846 }
1847
1848 /* Index generation function */
1849 int caseIgnoreFilter(
1850         slap_mask_t use,
1851         slap_mask_t flags,
1852         Syntax *syntax,
1853         MatchingRule *mr,
1854         struct berval *prefix,
1855         void * assertValue,
1856         struct berval ***keysp )
1857 {
1858         size_t slen, mlen;
1859         struct berval **keys;
1860         HASH_CONTEXT   HASHcontext;
1861         unsigned char   HASHdigest[HASH_BYTES];
1862         struct berval *value;
1863         struct berval digest;
1864         digest.bv_val = HASHdigest;
1865         digest.bv_len = sizeof(HASHdigest);
1866
1867         slen = strlen( syntax->ssyn_oid );
1868         mlen = strlen( mr->smr_oid );
1869
1870 #if UTF8MATCH
1871         value = ber_bvstr( UTF8normalize( ((struct berval *) assertValue)->bv_val, UTF8_CASEFOLD ) );
1872 #else
1873         value = ber_bvdup( (struct berval *) assertValue );
1874         ldap_pvt_str2upper( value->bv_val );
1875 #endif
1876
1877         keys = ch_malloc( sizeof( struct berval * ) * 2 );
1878
1879         HASH_Init( &HASHcontext );
1880         if( prefix != NULL && prefix->bv_len > 0 ) {
1881                 HASH_Update( &HASHcontext,
1882                         prefix->bv_val, prefix->bv_len );
1883         }
1884         HASH_Update( &HASHcontext,
1885                 syntax->ssyn_oid, slen );
1886         HASH_Update( &HASHcontext,
1887                 mr->smr_oid, mlen );
1888         HASH_Update( &HASHcontext,
1889                 value->bv_val, value->bv_len );
1890         HASH_Final( HASHdigest, &HASHcontext );
1891
1892         keys[0] = ber_bvdup( &digest );
1893         keys[1] = NULL;
1894
1895         ber_bvfree( value );
1896
1897         *keysp = keys;
1898
1899         return LDAP_SUCCESS;
1900 }
1901
1902 /* Substrings Index generation function */
1903 int caseIgnoreSubstringsIndexer(
1904         slap_mask_t use,
1905         slap_mask_t flags,
1906         Syntax *syntax,
1907         MatchingRule *mr,
1908         struct berval *prefix,
1909         struct berval **values,
1910         struct berval ***keysp )
1911 {
1912         ber_len_t i, nkeys;
1913         size_t slen, mlen;
1914         struct berval **keys;
1915         HASH_CONTEXT   HASHcontext;
1916         unsigned char   HASHdigest[HASH_BYTES];
1917         struct berval digest;
1918         digest.bv_val = HASHdigest;
1919         digest.bv_len = sizeof(HASHdigest);
1920
1921         /* we should have at least one value at this point */
1922         assert( values != NULL && values[0] != NULL );
1923
1924         nkeys=0;
1925         for( i=0; values[i] != NULL; i++ ) {
1926                 /* count number of indices to generate */
1927                 if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) {
1928                         continue;
1929                 }
1930
1931                 if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
1932                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
1933                                 nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
1934                                         ( SLAP_INDEX_SUBSTR_MINLEN - 1);
1935                         } else {
1936                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
1937                         }
1938                 }
1939
1940                 if( flags & SLAP_INDEX_SUBSTR_ANY ) {
1941                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
1942                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
1943                         }
1944                 }
1945
1946                 if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
1947                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
1948                                 nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
1949                                         ( SLAP_INDEX_SUBSTR_MINLEN - 1);
1950                         } else {
1951                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
1952                         }
1953                 }
1954         }
1955
1956         if( nkeys == 0 ) {
1957                 /* no keys to generate */
1958                 *keysp = NULL;
1959                 return LDAP_SUCCESS;
1960         }
1961
1962         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
1963
1964         slen = strlen( syntax->ssyn_oid );
1965         mlen = strlen( mr->smr_oid );
1966
1967         nkeys=0;
1968         for( i=0; values[i] != NULL; i++ ) {
1969                 int j,max;
1970                 struct berval *value;
1971
1972                 if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) continue;
1973
1974 #if UTF8MATCH
1975                 value = ber_bvstr( UTF8normalize( values[i]->bv_val, UTF8_CASEFOLD ) );
1976 #else
1977                 value = ber_bvdup( values[i] );
1978                 ldap_pvt_str2upper( value->bv_val );
1979 #endif
1980
1981                 if( ( flags & SLAP_INDEX_SUBSTR_ANY ) &&
1982                         ( value->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) )
1983                 {
1984                         char pre = SLAP_INDEX_SUBSTR_PREFIX;
1985                         max = value->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1);
1986
1987                         for( j=0; j<max; j++ ) {
1988                                 HASH_Init( &HASHcontext );
1989                                 if( prefix != NULL && prefix->bv_len > 0 ) {
1990                                         HASH_Update( &HASHcontext,
1991                                                 prefix->bv_val, prefix->bv_len );
1992                                 }
1993
1994                                 HASH_Update( &HASHcontext,
1995                                         &pre, sizeof( pre ) );
1996                                 HASH_Update( &HASHcontext,
1997                                         syntax->ssyn_oid, slen );
1998                                 HASH_Update( &HASHcontext,
1999                                         mr->smr_oid, mlen );
2000                                 HASH_Update( &HASHcontext,
2001                                         &value->bv_val[j],
2002                                         SLAP_INDEX_SUBSTR_MAXLEN );
2003                                 HASH_Final( HASHdigest, &HASHcontext );
2004
2005                                 keys[nkeys++] = ber_bvdup( &digest );
2006                         }
2007                 }
2008
2009                 max = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
2010                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
2011
2012                 for( j=SLAP_INDEX_SUBSTR_MINLEN; j<=max; j++ ) {
2013                         char pre;
2014
2015                         if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
2016                                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
2017                                 HASH_Init( &HASHcontext );
2018                                 if( prefix != NULL && prefix->bv_len > 0 ) {
2019                                         HASH_Update( &HASHcontext,
2020                                                 prefix->bv_val, prefix->bv_len );
2021                                 }
2022                                 HASH_Update( &HASHcontext,
2023                                         &pre, sizeof( pre ) );
2024                                 HASH_Update( &HASHcontext,
2025                                         syntax->ssyn_oid, slen );
2026                                 HASH_Update( &HASHcontext,
2027                                         mr->smr_oid, mlen );
2028                                 HASH_Update( &HASHcontext,
2029                                         value->bv_val, j );
2030                                 HASH_Final( HASHdigest, &HASHcontext );
2031
2032                                 keys[nkeys++] = ber_bvdup( &digest );
2033                         }
2034
2035                         if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
2036                                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
2037                                 HASH_Init( &HASHcontext );
2038                                 if( prefix != NULL && prefix->bv_len > 0 ) {
2039                                         HASH_Update( &HASHcontext,
2040                                                 prefix->bv_val, prefix->bv_len );
2041                                 }
2042                                 HASH_Update( &HASHcontext,
2043                                         &pre, sizeof( pre ) );
2044                                 HASH_Update( &HASHcontext,
2045                                         syntax->ssyn_oid, slen );
2046                                 HASH_Update( &HASHcontext,
2047                                         mr->smr_oid, mlen );
2048                                 HASH_Update( &HASHcontext,
2049                                         &value->bv_val[value->bv_len-j], j );
2050                                 HASH_Final( HASHdigest, &HASHcontext );
2051
2052                                 keys[nkeys++] = ber_bvdup( &digest );
2053                         }
2054
2055                 }
2056
2057                 ber_bvfree( value );
2058         }
2059
2060         if( nkeys > 0 ) {
2061                 keys[nkeys] = NULL;
2062                 *keysp = keys;
2063         } else {
2064                 ch_free( keys );
2065                 *keysp = NULL;
2066         }
2067
2068         return LDAP_SUCCESS;
2069 }
2070
2071 int caseIgnoreSubstringsFilter(
2072         slap_mask_t use,
2073         slap_mask_t flags,
2074         Syntax *syntax,
2075         MatchingRule *mr,
2076         struct berval *prefix,
2077         void * assertValue,
2078         struct berval ***keysp )
2079 {
2080         SubstringsAssertion *sa = assertValue;
2081         char pre;
2082         ber_len_t nkeys = 0;
2083         size_t slen, mlen, klen;
2084         struct berval **keys;
2085         HASH_CONTEXT   HASHcontext;
2086         unsigned char   HASHdigest[HASH_BYTES];
2087         struct berval *value;
2088         struct berval digest;
2089
2090         if((flags & SLAP_INDEX_SUBSTR_INITIAL) && sa->sa_initial != NULL &&
2091                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
2092         {
2093                 nkeys++;
2094         }
2095
2096         if((flags & SLAP_INDEX_SUBSTR_ANY) && sa->sa_any != NULL ) {
2097                 ber_len_t i;
2098                 for( i=0; sa->sa_any[i] != NULL; i++ ) {
2099                         if( sa->sa_any[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
2100                                 /* don't bother accounting for stepping */
2101                                 nkeys += sa->sa_any[i]->bv_len -
2102                                         ( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
2103                         }
2104                 }
2105         }
2106
2107         if((flags & SLAP_INDEX_SUBSTR_FINAL) && sa->sa_final != NULL &&
2108                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
2109         {
2110                 nkeys++;
2111         }
2112
2113         if( nkeys == 0 ) {
2114                 *keysp = NULL;
2115                 return LDAP_SUCCESS;
2116         }
2117
2118         digest.bv_val = HASHdigest;
2119         digest.bv_len = sizeof(HASHdigest);
2120
2121         slen = strlen( syntax->ssyn_oid );
2122         mlen = strlen( mr->smr_oid );
2123
2124         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
2125         nkeys = 0;
2126
2127         if((flags & SLAP_INDEX_SUBSTR_INITIAL) && sa->sa_initial != NULL &&
2128                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
2129         {
2130                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
2131 #if UTF8MATCH
2132                 value = ber_bvstr( UTF8normalize( sa->sa_initial->bv_val, UTF8_CASEFOLD ) );
2133 #else
2134                 value = ber_bvdup( sa->sa_initial );
2135                 ldap_pvt_str2upper( value->bv_val );
2136 #endif
2137
2138                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
2139                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
2140
2141                 HASH_Init( &HASHcontext );
2142                 if( prefix != NULL && prefix->bv_len > 0 ) {
2143                         HASH_Update( &HASHcontext,
2144                                 prefix->bv_val, prefix->bv_len );
2145                 }
2146                 HASH_Update( &HASHcontext,
2147                         &pre, sizeof( pre ) );
2148                 HASH_Update( &HASHcontext,
2149                         syntax->ssyn_oid, slen );
2150                 HASH_Update( &HASHcontext,
2151                         mr->smr_oid, mlen );
2152                 HASH_Update( &HASHcontext,
2153                         value->bv_val, klen );
2154                 HASH_Final( HASHdigest, &HASHcontext );
2155
2156                 ber_bvfree( value );
2157                 keys[nkeys++] = ber_bvdup( &digest );
2158         }
2159
2160         if((flags & SLAP_INDEX_SUBSTR_ANY) && sa->sa_any != NULL ) {
2161                 ber_len_t i, j;
2162                 pre = SLAP_INDEX_SUBSTR_PREFIX;
2163                 klen = SLAP_INDEX_SUBSTR_MAXLEN;
2164
2165                 for( i=0; sa->sa_any[i] != NULL; i++ ) {
2166                         if( sa->sa_any[i]->bv_len < SLAP_INDEX_SUBSTR_MAXLEN ) {
2167                                 continue;
2168                         }
2169
2170 #if UTF8MATCH
2171                         value = ber_bvstr( UTF8normalize( sa->sa_any[i]->bv_val, UTF8_CASEFOLD ) );
2172 #else
2173                         value = ber_bvdup( sa->sa_any[i] );
2174                         ldap_pvt_str2upper( value->bv_val );
2175 #endif
2176
2177                         for(j=0;
2178                                 j <= value->bv_len - SLAP_INDEX_SUBSTR_MAXLEN;
2179                                 j += SLAP_INDEX_SUBSTR_STEP )
2180                         {
2181                                 HASH_Init( &HASHcontext );
2182                                 if( prefix != NULL && prefix->bv_len > 0 ) {
2183                                         HASH_Update( &HASHcontext,
2184                                                 prefix->bv_val, prefix->bv_len );
2185                                 }
2186                                 HASH_Update( &HASHcontext,
2187                                         &pre, sizeof( pre ) );
2188                                 HASH_Update( &HASHcontext,
2189                                         syntax->ssyn_oid, slen );
2190                                 HASH_Update( &HASHcontext,
2191                                         mr->smr_oid, mlen );
2192                                 HASH_Update( &HASHcontext,
2193                                         &value->bv_val[j], klen );
2194                                 HASH_Final( HASHdigest, &HASHcontext );
2195
2196                                 keys[nkeys++] = ber_bvdup( &digest );
2197                         }
2198
2199                         ber_bvfree( value );
2200                 }
2201         }
2202
2203         if((flags & SLAP_INDEX_SUBSTR_FINAL) && sa->sa_final != NULL &&
2204                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
2205         {
2206                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
2207 #if UTF8MATCH
2208                 value = ber_bvstr( UTF8normalize( sa->sa_final->bv_val, UTF8_CASEFOLD ) );
2209 #else
2210                 value = ber_bvdup( sa->sa_final );
2211                 ldap_pvt_str2upper( value->bv_val );
2212 #endif
2213
2214                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
2215                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
2216
2217                 HASH_Init( &HASHcontext );
2218                 if( prefix != NULL && prefix->bv_len > 0 ) {
2219                         HASH_Update( &HASHcontext,
2220                                 prefix->bv_val, prefix->bv_len );
2221                 }
2222                 HASH_Update( &HASHcontext,
2223                         &pre, sizeof( pre ) );
2224                 HASH_Update( &HASHcontext,
2225                         syntax->ssyn_oid, slen );
2226                 HASH_Update( &HASHcontext,
2227                         mr->smr_oid, mlen );
2228                 HASH_Update( &HASHcontext,
2229                         &value->bv_val[value->bv_len-klen], klen );
2230                 HASH_Final( HASHdigest, &HASHcontext );
2231
2232                 ber_bvfree( value );
2233                 keys[nkeys++] = ber_bvdup( &digest );
2234         }
2235
2236         if( nkeys > 0 ) {
2237                 keys[nkeys] = NULL;
2238                 *keysp = keys;
2239         } else {
2240                 ch_free( keys );
2241                 *keysp = NULL;
2242         }
2243
2244         return LDAP_SUCCESS;
2245 }
2246         
2247 static int
2248 oidValidate(
2249         Syntax *syntax,
2250         struct berval *val )
2251 {
2252         ber_len_t i;
2253
2254         if( val->bv_len == 0 ) {
2255                 /* disallow empty strings */
2256                 return LDAP_INVALID_SYNTAX;
2257         }
2258
2259         if( OID_LEADCHAR(val->bv_val[0]) ) {
2260                 int dot = 0;
2261                 for(i=1; i < val->bv_len; i++) {
2262                         if( OID_SEPARATOR( val->bv_val[i] ) ) {
2263                                 if( dot++ ) return 1;
2264                         } else if ( OID_CHAR( val->bv_val[i] ) ) {
2265                                 dot = 0;
2266                         } else {
2267                                 return LDAP_INVALID_SYNTAX;
2268                         }
2269                 }
2270
2271                 return !dot ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
2272
2273         } else if( DESC_LEADCHAR(val->bv_val[0]) ) {
2274                 for(i=1; i < val->bv_len; i++) {
2275                         if( !DESC_CHAR(val->bv_val[i] ) ) {
2276                                 return LDAP_INVALID_SYNTAX;
2277                         }
2278                 }
2279
2280                 return LDAP_SUCCESS;
2281         }
2282         
2283         return LDAP_INVALID_SYNTAX;
2284 }
2285
2286 static int
2287 integerValidate(
2288         Syntax *syntax,
2289         struct berval *val )
2290 {
2291         ber_len_t i;
2292
2293         if( !val->bv_len ) return LDAP_INVALID_SYNTAX;
2294
2295         if( val->bv_val[0] == '+' || val->bv_val[0] == '-' ) {
2296                 if( val->bv_len < 2 ) return LDAP_INVALID_SYNTAX;
2297         } else if( !ASCII_DIGIT(val->bv_val[0]) ) {
2298                 return LDAP_INVALID_SYNTAX;
2299         }
2300
2301         for(i=1; i < val->bv_len; i++) {
2302                 if( !ASCII_DIGIT(val->bv_val[i]) ) return LDAP_INVALID_SYNTAX;
2303         }
2304
2305         return LDAP_SUCCESS;
2306 }
2307
2308 static int
2309 integerNormalize(
2310         Syntax *syntax,
2311         struct berval *val,
2312         struct berval **normalized )
2313 {
2314         int negative;
2315         struct berval *newval;
2316         char *p;
2317
2318         p = val->bv_val;
2319
2320         /* save sign */
2321         negative = ( *p == '-' );
2322         if( *p == '-' || *p == '+' ) p++;
2323
2324         /* Ignore leading zeros */
2325         while ( *p == '0' ) p++;
2326
2327         newval = (struct berval *) ch_malloc( sizeof(struct berval) );
2328
2329         if( *p == '\0' ) {
2330                 newval->bv_val = ch_strdup("0");
2331                 newval->bv_len = 1;
2332                 goto done;
2333         }
2334
2335         newval->bv_val = ch_malloc( val->bv_len + 1 );
2336         newval->bv_len = 0;
2337
2338         if( negative ) {
2339                 newval->bv_val[newval->bv_len++] = '-';
2340         }
2341
2342         for( ; *p != '\0'; p++ ) {
2343                 newval->bv_val[newval->bv_len++] = *p;
2344         }
2345
2346 done:
2347         *normalized = newval;
2348         return LDAP_SUCCESS;
2349 }
2350
2351 static int
2352 countryStringValidate(
2353         Syntax *syntax,
2354         struct berval *val )
2355 {
2356         if( val->bv_len != 2 ) return LDAP_INVALID_SYNTAX;
2357
2358         if( !SLAP_PRINTABLE(val->bv_val[0]) ) {
2359                 return LDAP_INVALID_SYNTAX;
2360         }
2361         if( !SLAP_PRINTABLE(val->bv_val[1]) ) {
2362                 return LDAP_INVALID_SYNTAX;
2363         }
2364
2365         return LDAP_SUCCESS;
2366 }
2367
2368 static int
2369 printableStringValidate(
2370         Syntax *syntax,
2371         struct berval *val )
2372 {
2373         ber_len_t i;
2374
2375         if( !val->bv_len ) return LDAP_INVALID_SYNTAX;
2376
2377         for(i=0; i < val->bv_len; i++) {
2378                 if( !SLAP_PRINTABLE(val->bv_val[i]) ) {
2379                         return LDAP_INVALID_SYNTAX;
2380                 }
2381         }
2382
2383         return LDAP_SUCCESS;
2384 }
2385
2386 static int
2387 printablesStringValidate(
2388         Syntax *syntax,
2389         struct berval *val )
2390 {
2391         ber_len_t i;
2392
2393         if( !val->bv_len ) return LDAP_INVALID_SYNTAX;
2394
2395         for(i=0; i < val->bv_len; i++) {
2396                 if( !SLAP_PRINTABLES(val->bv_val[i]) ) {
2397                         return LDAP_INVALID_SYNTAX;
2398                 }
2399         }
2400
2401         return LDAP_SUCCESS;
2402 }
2403
2404 static int
2405 IA5StringValidate(
2406         Syntax *syntax,
2407         struct berval *val )
2408 {
2409         ber_len_t i;
2410
2411         if( !val->bv_len ) return LDAP_INVALID_SYNTAX;
2412
2413         for(i=0; i < val->bv_len; i++) {
2414                 if( !isascii(val->bv_val[i]) ) return LDAP_INVALID_SYNTAX;
2415         }
2416
2417         return LDAP_SUCCESS;
2418 }
2419
2420 static int
2421 IA5StringNormalize(
2422         Syntax *syntax,
2423         struct berval *val,
2424         struct berval **normalized )
2425 {
2426         struct berval *newval;
2427         char *p, *q;
2428
2429         newval = ch_malloc( sizeof( struct berval ) );
2430
2431         p = val->bv_val;
2432
2433         /* Ignore initial whitespace */
2434         while ( ASCII_SPACE( *p ) ) {
2435                 p++;
2436         }
2437
2438         if( *p == '\0' ) {
2439                 ch_free( newval );
2440                 return LDAP_INVALID_SYNTAX;
2441         }
2442
2443         newval->bv_val = ch_strdup( p );
2444         p = q = newval->bv_val;
2445
2446         while ( *p ) {
2447                 if ( ASCII_SPACE( *p ) ) {
2448                         *q++ = *p++;
2449
2450                         /* Ignore the extra whitespace */
2451                         while ( ASCII_SPACE( *p ) ) {
2452                                 p++;
2453                         }
2454                 } else {
2455                         *q++ = *p++;
2456                 }
2457         }
2458
2459         assert( *newval->bv_val );
2460         assert( newval->bv_val < p );
2461         assert( q <= p );
2462
2463         /* cannot start with a space */
2464         assert( !ASCII_SPACE(*newval->bv_val) );
2465
2466         /*
2467          * If the string ended in space, backup the pointer one
2468          * position.  One is enough because the above loop collapsed
2469          * all whitespace to a single space.
2470          */
2471
2472         if ( ASCII_SPACE( q[-1] ) ) {
2473                 --q;
2474         }
2475
2476         /* cannot end with a space */
2477         assert( !ASCII_SPACE( q[-1] ) );
2478
2479         /* null terminate */
2480         *q = '\0';
2481
2482         newval->bv_len = q - newval->bv_val;
2483         *normalized = newval;
2484
2485         return LDAP_SUCCESS;
2486 }
2487
2488 static int
2489 caseExactIA5Match(
2490         int *matchp,
2491         slap_mask_t flags,
2492         Syntax *syntax,
2493         MatchingRule *mr,
2494         struct berval *value,
2495         void *assertedValue )
2496 {
2497         int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;
2498
2499         if( match == 0 ) {
2500                 match = strncmp( value->bv_val,
2501                         ((struct berval *) assertedValue)->bv_val,
2502                         value->bv_len );
2503         }
2504
2505         *matchp = match;
2506         return LDAP_SUCCESS;
2507 }
2508
2509 static int
2510 caseExactIA5SubstringsMatch(
2511         int *matchp,
2512         slap_mask_t flags,
2513         Syntax *syntax,
2514         MatchingRule *mr,
2515         struct berval *value,
2516         void *assertedValue )
2517 {
2518         int match = 0;
2519         SubstringsAssertion *sub = assertedValue;
2520         struct berval left = *value;
2521         int i;
2522         ber_len_t inlen=0;
2523
2524         /* Add up asserted input length */
2525         if( sub->sa_initial ) {
2526                 inlen += sub->sa_initial->bv_len;
2527         }
2528         if( sub->sa_any ) {
2529                 for(i=0; sub->sa_any[i] != NULL; i++) {
2530                         inlen += sub->sa_any[i]->bv_len;
2531                 }
2532         }
2533         if( sub->sa_final ) {
2534                 inlen += sub->sa_final->bv_len;
2535         }
2536
2537         if( sub->sa_initial ) {
2538                 if( inlen > left.bv_len ) {
2539                         match = 1;
2540                         goto done;
2541                 }
2542
2543                 match = strncmp( sub->sa_initial->bv_val, left.bv_val,
2544                         sub->sa_initial->bv_len );
2545
2546                 if( match != 0 ) {
2547                         goto done;
2548                 }
2549
2550                 left.bv_val += sub->sa_initial->bv_len;
2551                 left.bv_len -= sub->sa_initial->bv_len;
2552                 inlen -= sub->sa_initial->bv_len;
2553         }
2554
2555         if( sub->sa_final ) {
2556                 if( inlen > left.bv_len ) {
2557                         match = 1;
2558                         goto done;
2559                 }
2560
2561                 match = strncmp( sub->sa_final->bv_val,
2562                         &left.bv_val[left.bv_len - sub->sa_final->bv_len],
2563                         sub->sa_final->bv_len );
2564
2565                 if( match != 0 ) {
2566                         goto done;
2567                 }
2568
2569                 left.bv_len -= sub->sa_final->bv_len;
2570                 inlen -= sub->sa_final->bv_len;
2571         }
2572
2573         if( sub->sa_any ) {
2574                 for(i=0; sub->sa_any[i]; i++) {
2575                         ber_len_t idx;
2576                         char *p;
2577
2578 retry:
2579                         if( inlen > left.bv_len ) {
2580                                 /* not enough length */
2581                                 match = 1;
2582                                 goto done;
2583                         }
2584
2585                         if( sub->sa_any[i]->bv_len == 0 ) {
2586                                 continue;
2587                         }
2588
2589                         p = strchr( left.bv_val, *sub->sa_any[i]->bv_val );
2590
2591                         if( p == NULL ) {
2592                                 match = 1;
2593                                 goto done;
2594                         }
2595
2596                         idx = p - left.bv_val;
2597                         assert( idx < left.bv_len );
2598
2599                         if( idx >= left.bv_len ) {
2600                                 /* this shouldn't happen */
2601                                 return LDAP_OTHER;
2602                         }
2603
2604                         left.bv_val = p;
2605                         left.bv_len -= idx;
2606
2607                         if( sub->sa_any[i]->bv_len > left.bv_len ) {
2608                                 /* not enough left */
2609                                 match = 1;
2610                                 goto done;
2611                         }
2612
2613                         match = strncmp( left.bv_val,
2614                                 sub->sa_any[i]->bv_val,
2615                                 sub->sa_any[i]->bv_len );
2616
2617                         if( match != 0 ) {
2618                                 left.bv_val++;
2619                                 left.bv_len--;
2620                                 goto retry;
2621                         }
2622
2623                         left.bv_val += sub->sa_any[i]->bv_len;
2624                         left.bv_len -= sub->sa_any[i]->bv_len;
2625                         inlen -= sub->sa_any[i]->bv_len;
2626                 }
2627         }
2628
2629 done:
2630         *matchp = match;
2631         return LDAP_SUCCESS;
2632 }
2633
2634 /* Index generation function */
2635 int caseExactIA5Indexer(
2636         slap_mask_t use,
2637         slap_mask_t flags,
2638         Syntax *syntax,
2639         MatchingRule *mr,
2640         struct berval *prefix,
2641         struct berval **values,
2642         struct berval ***keysp )
2643 {
2644         int i;
2645         size_t slen, mlen;
2646         struct berval **keys;
2647         HASH_CONTEXT   HASHcontext;
2648         unsigned char   HASHdigest[HASH_BYTES];
2649         struct berval digest;
2650         digest.bv_val = HASHdigest;
2651         digest.bv_len = sizeof(HASHdigest);
2652
2653         /* we should have at least one value at this point */
2654         assert( values != NULL && values[0] != NULL );
2655
2656         for( i=0; values[i] != NULL; i++ ) {
2657                 /* just count them */
2658         }
2659
2660         keys = ch_malloc( sizeof( struct berval * ) * (i+1) );
2661
2662         slen = strlen( syntax->ssyn_oid );
2663         mlen = strlen( mr->smr_oid );
2664
2665         for( i=0; values[i] != NULL; i++ ) {
2666                 struct berval *value = values[i];
2667
2668                 HASH_Init( &HASHcontext );
2669                 if( prefix != NULL && prefix->bv_len > 0 ) {
2670                         HASH_Update( &HASHcontext,
2671                                 prefix->bv_val, prefix->bv_len );
2672                 }
2673                 HASH_Update( &HASHcontext,
2674                         syntax->ssyn_oid, slen );
2675                 HASH_Update( &HASHcontext,
2676                         mr->smr_oid, mlen );
2677                 HASH_Update( &HASHcontext,
2678                         value->bv_val, value->bv_len );
2679                 HASH_Final( HASHdigest, &HASHcontext );
2680
2681                 keys[i] = ber_bvdup( &digest );
2682         }
2683
2684         keys[i] = NULL;
2685         *keysp = keys;
2686         return LDAP_SUCCESS;
2687 }
2688
2689 /* Index generation function */
2690 int caseExactIA5Filter(
2691         slap_mask_t use,
2692         slap_mask_t flags,
2693         Syntax *syntax,
2694         MatchingRule *mr,
2695         struct berval *prefix,
2696         void * assertValue,
2697         struct berval ***keysp )
2698 {
2699         size_t slen, mlen;
2700         struct berval **keys;
2701         HASH_CONTEXT   HASHcontext;
2702         unsigned char   HASHdigest[HASH_BYTES];
2703         struct berval *value;
2704         struct berval digest;
2705         digest.bv_val = HASHdigest;
2706         digest.bv_len = sizeof(HASHdigest);
2707
2708         slen = strlen( syntax->ssyn_oid );
2709         mlen = strlen( mr->smr_oid );
2710
2711         value = (struct berval *) assertValue;
2712
2713         keys = ch_malloc( sizeof( struct berval * ) * 2 );
2714
2715         HASH_Init( &HASHcontext );
2716         if( prefix != NULL && prefix->bv_len > 0 ) {
2717                 HASH_Update( &HASHcontext,
2718                         prefix->bv_val, prefix->bv_len );
2719         }
2720         HASH_Update( &HASHcontext,
2721                 syntax->ssyn_oid, slen );
2722         HASH_Update( &HASHcontext,
2723                 mr->smr_oid, mlen );
2724         HASH_Update( &HASHcontext,
2725                 value->bv_val, value->bv_len );
2726         HASH_Final( HASHdigest, &HASHcontext );
2727
2728         keys[0] = ber_bvdup( &digest );
2729         keys[1] = NULL;
2730
2731         *keysp = keys;
2732         return LDAP_SUCCESS;
2733 }
2734
2735 /* Substrings Index generation function */
2736 int caseExactIA5SubstringsIndexer(
2737         slap_mask_t use,
2738         slap_mask_t flags,
2739         Syntax *syntax,
2740         MatchingRule *mr,
2741         struct berval *prefix,
2742         struct berval **values,
2743         struct berval ***keysp )
2744 {
2745         ber_len_t i, nkeys;
2746         size_t slen, mlen;
2747         struct berval **keys;
2748         HASH_CONTEXT   HASHcontext;
2749         unsigned char   HASHdigest[HASH_BYTES];
2750         struct berval digest;
2751         digest.bv_val = HASHdigest;
2752         digest.bv_len = sizeof(HASHdigest);
2753
2754         /* we should have at least one value at this point */
2755         assert( values != NULL && values[0] != NULL );
2756
2757         nkeys=0;
2758         for( i=0; values[i] != NULL; i++ ) {
2759                 /* count number of indices to generate */
2760                 if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) {
2761                         continue;
2762                 }
2763
2764                 if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
2765                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
2766                                 nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
2767                                         ( SLAP_INDEX_SUBSTR_MINLEN - 1);
2768                         } else {
2769                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
2770                         }
2771                 }
2772
2773                 if( flags & SLAP_INDEX_SUBSTR_ANY ) {
2774                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
2775                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
2776                         }
2777                 }
2778
2779                 if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
2780                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
2781                                 nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
2782                                         ( SLAP_INDEX_SUBSTR_MINLEN - 1);
2783                         } else {
2784                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
2785                         }
2786                 }
2787         }
2788
2789         if( nkeys == 0 ) {
2790                 /* no keys to generate */
2791                 *keysp = NULL;
2792                 return LDAP_SUCCESS;
2793         }
2794
2795         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
2796
2797         slen = strlen( syntax->ssyn_oid );
2798         mlen = strlen( mr->smr_oid );
2799
2800         nkeys=0;
2801         for( i=0; values[i] != NULL; i++ ) {
2802                 ber_len_t j,max;
2803                 struct berval *value;
2804
2805                 value = values[i];
2806                 if( value->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) continue;
2807
2808                 if( ( flags & SLAP_INDEX_SUBSTR_ANY ) &&
2809                         ( value->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) )
2810                 {
2811                         char pre = SLAP_INDEX_SUBSTR_PREFIX;
2812                         max = value->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1);
2813
2814                         for( j=0; j<max; j++ ) {
2815                                 HASH_Init( &HASHcontext );
2816                                 if( prefix != NULL && prefix->bv_len > 0 ) {
2817                                         HASH_Update( &HASHcontext,
2818                                                 prefix->bv_val, prefix->bv_len );
2819                                 }
2820
2821                                 HASH_Update( &HASHcontext,
2822                                         &pre, sizeof( pre ) );
2823                                 HASH_Update( &HASHcontext,
2824                                         syntax->ssyn_oid, slen );
2825                                 HASH_Update( &HASHcontext,
2826                                         mr->smr_oid, mlen );
2827                                 HASH_Update( &HASHcontext,
2828                                         &value->bv_val[j],
2829                                         SLAP_INDEX_SUBSTR_MAXLEN );
2830                                 HASH_Final( HASHdigest, &HASHcontext );
2831
2832                                 keys[nkeys++] = ber_bvdup( &digest );
2833                         }
2834                 }
2835
2836                 max = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
2837                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
2838
2839                 for( j=SLAP_INDEX_SUBSTR_MINLEN; j<=max; j++ ) {
2840                         char pre;
2841
2842                         if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
2843                                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
2844                                 HASH_Init( &HASHcontext );
2845                                 if( prefix != NULL && prefix->bv_len > 0 ) {
2846                                         HASH_Update( &HASHcontext,
2847                                                 prefix->bv_val, prefix->bv_len );
2848                                 }
2849                                 HASH_Update( &HASHcontext,
2850                                         &pre, sizeof( pre ) );
2851                                 HASH_Update( &HASHcontext,
2852                                         syntax->ssyn_oid, slen );
2853                                 HASH_Update( &HASHcontext,
2854                                         mr->smr_oid, mlen );
2855                                 HASH_Update( &HASHcontext,
2856                                         value->bv_val, j );
2857                                 HASH_Final( HASHdigest, &HASHcontext );
2858
2859                                 keys[nkeys++] = ber_bvdup( &digest );
2860                         }
2861
2862                         if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
2863                                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
2864                                 HASH_Init( &HASHcontext );
2865                                 if( prefix != NULL && prefix->bv_len > 0 ) {
2866                                         HASH_Update( &HASHcontext,
2867                                                 prefix->bv_val, prefix->bv_len );
2868                                 }
2869                                 HASH_Update( &HASHcontext,
2870                                         &pre, sizeof( pre ) );
2871                                 HASH_Update( &HASHcontext,
2872                                         syntax->ssyn_oid, slen );
2873                                 HASH_Update( &HASHcontext,
2874                                         mr->smr_oid, mlen );
2875                                 HASH_Update( &HASHcontext,
2876                                         &value->bv_val[value->bv_len-j], j );
2877                                 HASH_Final( HASHdigest, &HASHcontext );
2878
2879                                 keys[nkeys++] = ber_bvdup( &digest );
2880                         }
2881
2882                 }
2883         }
2884
2885         if( nkeys > 0 ) {
2886                 keys[nkeys] = NULL;
2887                 *keysp = keys;
2888         } else {
2889                 ch_free( keys );
2890                 *keysp = NULL;
2891         }
2892
2893         return LDAP_SUCCESS;
2894 }
2895
2896 int caseExactIA5SubstringsFilter(
2897         slap_mask_t use,
2898         slap_mask_t flags,
2899         Syntax *syntax,
2900         MatchingRule *mr,
2901         struct berval *prefix,
2902         void * assertValue,
2903         struct berval ***keysp )
2904 {
2905         SubstringsAssertion *sa = assertValue;
2906         char pre;
2907         ber_len_t nkeys = 0;
2908         size_t slen, mlen, klen;
2909         struct berval **keys;
2910         HASH_CONTEXT   HASHcontext;
2911         unsigned char   HASHdigest[HASH_BYTES];
2912         struct berval *value;
2913         struct berval digest;
2914
2915         if( flags & SLAP_INDEX_SUBSTR_INITIAL && sa->sa_initial != NULL &&
2916                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
2917         {
2918                 nkeys++;
2919         }
2920
2921         if( flags & SLAP_INDEX_SUBSTR_ANY && sa->sa_any != NULL ) {
2922                 ber_len_t i;
2923                 for( i=0; sa->sa_any[i] != NULL; i++ ) {
2924                         if( sa->sa_any[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
2925                                 /* don't bother accounting for stepping */
2926                                 nkeys += sa->sa_any[i]->bv_len -
2927                                         ( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
2928                         }
2929                 }
2930         }
2931
2932         if( flags & SLAP_INDEX_SUBSTR_FINAL && sa->sa_final != NULL &&
2933                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
2934         {
2935                 nkeys++;
2936         }
2937
2938         if( nkeys == 0 ) {
2939                 *keysp = NULL;
2940                 return LDAP_SUCCESS;
2941         }
2942
2943         digest.bv_val = HASHdigest;
2944         digest.bv_len = sizeof(HASHdigest);
2945
2946         slen = strlen( syntax->ssyn_oid );
2947         mlen = strlen( mr->smr_oid );
2948
2949         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
2950         nkeys = 0;
2951
2952         if( flags & SLAP_INDEX_SUBSTR_INITIAL && sa->sa_initial != NULL &&
2953                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
2954         {
2955                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
2956                 value = sa->sa_initial;
2957
2958                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
2959                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
2960
2961                 HASH_Init( &HASHcontext );
2962                 if( prefix != NULL && prefix->bv_len > 0 ) {
2963                         HASH_Update( &HASHcontext,
2964                                 prefix->bv_val, prefix->bv_len );
2965                 }
2966                 HASH_Update( &HASHcontext,
2967                         &pre, sizeof( pre ) );
2968                 HASH_Update( &HASHcontext,
2969                         syntax->ssyn_oid, slen );
2970                 HASH_Update( &HASHcontext,
2971                         mr->smr_oid, mlen );
2972                 HASH_Update( &HASHcontext,
2973                         value->bv_val, klen );
2974                 HASH_Final( HASHdigest, &HASHcontext );
2975
2976                 keys[nkeys++] = ber_bvdup( &digest );
2977         }
2978
2979         if( flags & SLAP_INDEX_SUBSTR_ANY && sa->sa_any != NULL ) {
2980                 ber_len_t i, j;
2981                 pre = SLAP_INDEX_SUBSTR_PREFIX;
2982                 klen = SLAP_INDEX_SUBSTR_MAXLEN;
2983
2984                 for( i=0; sa->sa_any[i] != NULL; i++ ) {
2985                         if( sa->sa_any[i]->bv_len < SLAP_INDEX_SUBSTR_MAXLEN ) {
2986                                 continue;
2987                         }
2988
2989                         value = sa->sa_any[i];
2990
2991                         for(j=0;
2992                                 j <= value->bv_len - SLAP_INDEX_SUBSTR_MAXLEN;
2993                                 j += SLAP_INDEX_SUBSTR_STEP )
2994                         {
2995                                 HASH_Init( &HASHcontext );
2996                                 if( prefix != NULL && prefix->bv_len > 0 ) {
2997                                         HASH_Update( &HASHcontext,
2998                                                 prefix->bv_val, prefix->bv_len );
2999                                 }
3000                                 HASH_Update( &HASHcontext,
3001                                         &pre, sizeof( pre ) );
3002                                 HASH_Update( &HASHcontext,
3003                                         syntax->ssyn_oid, slen );
3004                                 HASH_Update( &HASHcontext,
3005                                         mr->smr_oid, mlen );
3006                                 HASH_Update( &HASHcontext,
3007                                         &value->bv_val[j], klen ); 
3008                                 HASH_Final( HASHdigest, &HASHcontext );
3009
3010                                 keys[nkeys++] = ber_bvdup( &digest );
3011                         }
3012                 }
3013         }
3014
3015         if( flags & SLAP_INDEX_SUBSTR_FINAL && sa->sa_final != NULL &&
3016                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
3017         {
3018                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
3019                 value = sa->sa_final;
3020
3021                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
3022                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
3023
3024                 HASH_Init( &HASHcontext );
3025                 if( prefix != NULL && prefix->bv_len > 0 ) {
3026                         HASH_Update( &HASHcontext,
3027                                 prefix->bv_val, prefix->bv_len );
3028                 }
3029                 HASH_Update( &HASHcontext,
3030                         &pre, sizeof( pre ) );
3031                 HASH_Update( &HASHcontext,
3032                         syntax->ssyn_oid, slen );
3033                 HASH_Update( &HASHcontext,
3034                         mr->smr_oid, mlen );
3035                 HASH_Update( &HASHcontext,
3036                         &value->bv_val[value->bv_len-klen], klen );
3037                 HASH_Final( HASHdigest, &HASHcontext );
3038
3039                 keys[nkeys++] = ber_bvdup( &digest );
3040         }
3041
3042         if( nkeys > 0 ) {
3043                 keys[nkeys] = NULL;
3044                 *keysp = keys;
3045         } else {
3046                 ch_free( keys );
3047                 *keysp = NULL;
3048         }
3049
3050         return LDAP_SUCCESS;
3051 }
3052         
3053 static int
3054 caseIgnoreIA5Match(
3055         int *matchp,
3056         slap_mask_t flags,
3057         Syntax *syntax,
3058         MatchingRule *mr,
3059         struct berval *value,
3060         void *assertedValue )
3061 {
3062         int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;
3063
3064         if( match == 0 && value->bv_len ) {
3065                 match = strncasecmp( value->bv_val,
3066                         ((struct berval *) assertedValue)->bv_val,
3067                         value->bv_len );
3068         }
3069
3070         *matchp = match;
3071         return LDAP_SUCCESS;
3072 }
3073
3074 static int
3075 caseIgnoreIA5SubstringsMatch(
3076         int *matchp,
3077         slap_mask_t flags,
3078         Syntax *syntax,
3079         MatchingRule *mr,
3080         struct berval *value,
3081         void *assertedValue )
3082 {
3083         int match = 0;
3084         SubstringsAssertion *sub = assertedValue;
3085         struct berval left = *value;
3086         int i;
3087         ber_len_t inlen=0;
3088
3089         /* Add up asserted input length */
3090         if( sub->sa_initial ) {
3091                 inlen += sub->sa_initial->bv_len;
3092         }
3093         if( sub->sa_any ) {
3094                 for(i=0; sub->sa_any[i] != NULL; i++) {
3095                         inlen += sub->sa_any[i]->bv_len;
3096                 }
3097         }
3098         if( sub->sa_final ) {
3099                 inlen += sub->sa_final->bv_len;
3100         }
3101
3102         if( sub->sa_initial ) {
3103                 if( inlen > left.bv_len ) {
3104                         match = 1;
3105                         goto done;
3106                 }
3107
3108                 match = strncasecmp( sub->sa_initial->bv_val, left.bv_val,
3109                         sub->sa_initial->bv_len );
3110
3111                 if( match != 0 ) {
3112                         goto done;
3113                 }
3114
3115                 left.bv_val += sub->sa_initial->bv_len;
3116                 left.bv_len -= sub->sa_initial->bv_len;
3117                 inlen -= sub->sa_initial->bv_len;
3118         }
3119
3120         if( sub->sa_final ) {
3121                 if( inlen > left.bv_len ) {
3122                         match = 1;
3123                         goto done;
3124                 }
3125
3126                 match = strncasecmp( sub->sa_final->bv_val,
3127                         &left.bv_val[left.bv_len - sub->sa_final->bv_len],
3128                         sub->sa_final->bv_len );
3129
3130                 if( match != 0 ) {
3131                         goto done;
3132                 }
3133
3134                 left.bv_len -= sub->sa_final->bv_len;
3135                 inlen -= sub->sa_final->bv_len;
3136         }
3137
3138         if( sub->sa_any ) {
3139                 for(i=0; sub->sa_any[i]; i++) {
3140                         ber_len_t idx;
3141                         char *p;
3142
3143 retry:
3144                         if( inlen > left.bv_len ) {
3145                                 /* not enough length */
3146                                 match = 1;
3147                                 goto done;
3148                         }
3149
3150                         if( sub->sa_any[i]->bv_len == 0 ) {
3151                                 continue;
3152                         }
3153
3154                         p = strcasechr( left.bv_val, *sub->sa_any[i]->bv_val );
3155
3156                         if( p == NULL ) {
3157                                 match = 1;
3158                                 goto done;
3159                         }
3160
3161                         idx = p - left.bv_val;
3162                         assert( idx < left.bv_len );
3163
3164                         if( idx >= left.bv_len ) {
3165                                 /* this shouldn't happen */
3166                                 return LDAP_OTHER;
3167                         }
3168
3169                         left.bv_val = p;
3170                         left.bv_len -= idx;
3171
3172                         if( sub->sa_any[i]->bv_len > left.bv_len ) {
3173                                 /* not enough left */
3174                                 match = 1;
3175                                 goto done;
3176                         }
3177
3178                         match = strncasecmp( left.bv_val,
3179                                 sub->sa_any[i]->bv_val,
3180                                 sub->sa_any[i]->bv_len );
3181
3182                         if( match != 0 ) {
3183                                 left.bv_val++;
3184                                 left.bv_len--;
3185
3186                                 goto retry;
3187                         }
3188
3189                         left.bv_val += sub->sa_any[i]->bv_len;
3190                         left.bv_len -= sub->sa_any[i]->bv_len;
3191                         inlen -= sub->sa_any[i]->bv_len;
3192                 }
3193         }
3194
3195 done:
3196         *matchp = match;
3197         return LDAP_SUCCESS;
3198 }
3199
3200 /* Index generation function */
3201 int caseIgnoreIA5Indexer(
3202         slap_mask_t use,
3203         slap_mask_t flags,
3204         Syntax *syntax,
3205         MatchingRule *mr,
3206         struct berval *prefix,
3207         struct berval **values,
3208         struct berval ***keysp )
3209 {
3210         int i;
3211         size_t slen, mlen;
3212         struct berval **keys;
3213         HASH_CONTEXT   HASHcontext;
3214         unsigned char   HASHdigest[HASH_BYTES];
3215         struct berval digest;
3216         digest.bv_val = HASHdigest;
3217         digest.bv_len = sizeof(HASHdigest);
3218
3219         /* we should have at least one value at this point */
3220         assert( values != NULL && values[0] != NULL );
3221
3222         for( i=0; values[i] != NULL; i++ ) {
3223                 /* just count them */
3224         }
3225
3226         keys = ch_malloc( sizeof( struct berval * ) * (i+1) );
3227
3228         slen = strlen( syntax->ssyn_oid );
3229         mlen = strlen( mr->smr_oid );
3230
3231         for( i=0; values[i] != NULL; i++ ) {
3232                 struct berval *value = ber_bvdup( values[i] );
3233                 ldap_pvt_str2upper( value->bv_val );
3234
3235                 HASH_Init( &HASHcontext );
3236                 if( prefix != NULL && prefix->bv_len > 0 ) {
3237                         HASH_Update( &HASHcontext,
3238                                 prefix->bv_val, prefix->bv_len );
3239                 }
3240                 HASH_Update( &HASHcontext,
3241                         syntax->ssyn_oid, slen );
3242                 HASH_Update( &HASHcontext,
3243                         mr->smr_oid, mlen );
3244                 HASH_Update( &HASHcontext,
3245                         value->bv_val, value->bv_len );
3246                 HASH_Final( HASHdigest, &HASHcontext );
3247
3248                 ber_bvfree( value );
3249
3250                 keys[i] = ber_bvdup( &digest );
3251         }
3252
3253         keys[i] = NULL;
3254         *keysp = keys;
3255         return LDAP_SUCCESS;
3256 }
3257
3258 /* Index generation function */
3259 int caseIgnoreIA5Filter(
3260         slap_mask_t use,
3261         slap_mask_t flags,
3262         Syntax *syntax,
3263         MatchingRule *mr,
3264         struct berval *prefix,
3265         void * assertValue,
3266         struct berval ***keysp )
3267 {
3268         size_t slen, mlen;
3269         struct berval **keys;
3270         HASH_CONTEXT   HASHcontext;
3271         unsigned char   HASHdigest[HASH_BYTES];
3272         struct berval *value;
3273         struct berval digest;
3274         digest.bv_val = HASHdigest;
3275         digest.bv_len = sizeof(HASHdigest);
3276
3277         slen = strlen( syntax->ssyn_oid );
3278         mlen = strlen( mr->smr_oid );
3279
3280         value = ber_bvdup( (struct berval *) assertValue );
3281         ldap_pvt_str2upper( value->bv_val );
3282
3283         keys = ch_malloc( sizeof( struct berval * ) * 2 );
3284
3285         HASH_Init( &HASHcontext );
3286         if( prefix != NULL && prefix->bv_len > 0 ) {
3287                 HASH_Update( &HASHcontext,
3288                         prefix->bv_val, prefix->bv_len );
3289         }
3290         HASH_Update( &HASHcontext,
3291                 syntax->ssyn_oid, slen );
3292         HASH_Update( &HASHcontext,
3293                 mr->smr_oid, mlen );
3294         HASH_Update( &HASHcontext,
3295                 value->bv_val, value->bv_len );
3296         HASH_Final( HASHdigest, &HASHcontext );
3297
3298         keys[0] = ber_bvdup( &digest );
3299         keys[1] = NULL;
3300
3301         ber_bvfree( value );
3302
3303         *keysp = keys;
3304
3305         return LDAP_SUCCESS;
3306 }
3307
3308 /* Substrings Index generation function */
3309 int caseIgnoreIA5SubstringsIndexer(
3310         slap_mask_t use,
3311         slap_mask_t flags,
3312         Syntax *syntax,
3313         MatchingRule *mr,
3314         struct berval *prefix,
3315         struct berval **values,
3316         struct berval ***keysp )
3317 {
3318         ber_len_t i, nkeys;
3319         size_t slen, mlen;
3320         struct berval **keys;
3321         HASH_CONTEXT   HASHcontext;
3322         unsigned char   HASHdigest[HASH_BYTES];
3323         struct berval digest;
3324         digest.bv_val = HASHdigest;
3325         digest.bv_len = sizeof(HASHdigest);
3326
3327         /* we should have at least one value at this point */
3328         assert( values != NULL && values[0] != NULL );
3329
3330         nkeys=0;
3331         for( i=0; values[i] != NULL; i++ ) {
3332                 /* count number of indices to generate */
3333                 if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) {
3334                         continue;
3335                 }
3336
3337                 if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
3338                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
3339                                 nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
3340                                         ( SLAP_INDEX_SUBSTR_MINLEN - 1);
3341                         } else {
3342                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
3343                         }
3344                 }
3345
3346                 if( flags & SLAP_INDEX_SUBSTR_ANY ) {
3347                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
3348                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
3349                         }
3350                 }
3351
3352                 if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
3353                         if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
3354                                 nkeys += SLAP_INDEX_SUBSTR_MAXLEN -
3355                                         ( SLAP_INDEX_SUBSTR_MINLEN - 1);
3356                         } else {
3357                                 nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
3358                         }
3359                 }
3360         }
3361
3362         if( nkeys == 0 ) {
3363                 /* no keys to generate */
3364                 *keysp = NULL;
3365                 return LDAP_SUCCESS;
3366         }
3367
3368         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
3369
3370         slen = strlen( syntax->ssyn_oid );
3371         mlen = strlen( mr->smr_oid );
3372
3373         nkeys=0;
3374         for( i=0; values[i] != NULL; i++ ) {
3375                 int j,max;
3376                 struct berval *value;
3377
3378                 if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) continue;
3379
3380                 value = ber_bvdup( values[i] );
3381                 ldap_pvt_str2upper( value->bv_val );
3382
3383                 if( ( flags & SLAP_INDEX_SUBSTR_ANY ) &&
3384                         ( value->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) )
3385                 {
3386                         char pre = SLAP_INDEX_SUBSTR_PREFIX;
3387                         max = value->bv_len - ( SLAP_INDEX_SUBSTR_MAXLEN - 1);
3388
3389                         for( j=0; j<max; j++ ) {
3390                                 HASH_Init( &HASHcontext );
3391                                 if( prefix != NULL && prefix->bv_len > 0 ) {
3392                                         HASH_Update( &HASHcontext,
3393                                                 prefix->bv_val, prefix->bv_len );
3394                                 }
3395
3396                                 HASH_Update( &HASHcontext,
3397                                         &pre, sizeof( pre ) );
3398                                 HASH_Update( &HASHcontext,
3399                                         syntax->ssyn_oid, slen );
3400                                 HASH_Update( &HASHcontext,
3401                                         mr->smr_oid, mlen );
3402                                 HASH_Update( &HASHcontext,
3403                                         &value->bv_val[j],
3404                                         SLAP_INDEX_SUBSTR_MAXLEN );
3405                                 HASH_Final( HASHdigest, &HASHcontext );
3406
3407                                 keys[nkeys++] = ber_bvdup( &digest );
3408                         }
3409                 }
3410
3411                 max = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
3412                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
3413
3414                 for( j=SLAP_INDEX_SUBSTR_MINLEN; j<=max; j++ ) {
3415                         char pre;
3416
3417                         if( flags & SLAP_INDEX_SUBSTR_INITIAL ) {
3418                                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
3419                                 HASH_Init( &HASHcontext );
3420                                 if( prefix != NULL && prefix->bv_len > 0 ) {
3421                                         HASH_Update( &HASHcontext,
3422                                                 prefix->bv_val, prefix->bv_len );
3423                                 }
3424                                 HASH_Update( &HASHcontext,
3425                                         &pre, sizeof( pre ) );
3426                                 HASH_Update( &HASHcontext,
3427                                         syntax->ssyn_oid, slen );
3428                                 HASH_Update( &HASHcontext,
3429                                         mr->smr_oid, mlen );
3430                                 HASH_Update( &HASHcontext,
3431                                         value->bv_val, j );
3432                                 HASH_Final( HASHdigest, &HASHcontext );
3433
3434                                 keys[nkeys++] = ber_bvdup( &digest );
3435                         }
3436
3437                         if( flags & SLAP_INDEX_SUBSTR_FINAL ) {
3438                                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
3439                                 HASH_Init( &HASHcontext );
3440                                 if( prefix != NULL && prefix->bv_len > 0 ) {
3441                                         HASH_Update( &HASHcontext,
3442                                                 prefix->bv_val, prefix->bv_len );
3443                                 }
3444                                 HASH_Update( &HASHcontext,
3445                                         &pre, sizeof( pre ) );
3446                                 HASH_Update( &HASHcontext,
3447                                         syntax->ssyn_oid, slen );
3448                                 HASH_Update( &HASHcontext,
3449                                         mr->smr_oid, mlen );
3450                                 HASH_Update( &HASHcontext,
3451                                         &value->bv_val[value->bv_len-j], j );
3452                                 HASH_Final( HASHdigest, &HASHcontext );
3453
3454                                 keys[nkeys++] = ber_bvdup( &digest );
3455                         }
3456
3457                 }
3458
3459                 ber_bvfree( value );
3460         }
3461
3462         if( nkeys > 0 ) {
3463                 keys[nkeys] = NULL;
3464                 *keysp = keys;
3465         } else {
3466                 ch_free( keys );
3467                 *keysp = NULL;
3468         }
3469
3470         return LDAP_SUCCESS;
3471 }
3472
3473 int caseIgnoreIA5SubstringsFilter(
3474         slap_mask_t use,
3475         slap_mask_t flags,
3476         Syntax *syntax,
3477         MatchingRule *mr,
3478         struct berval *prefix,
3479         void * assertValue,
3480         struct berval ***keysp )
3481 {
3482         SubstringsAssertion *sa = assertValue;
3483         char pre;
3484         ber_len_t nkeys = 0;
3485         size_t slen, mlen, klen;
3486         struct berval **keys;
3487         HASH_CONTEXT   HASHcontext;
3488         unsigned char   HASHdigest[HASH_BYTES];
3489         struct berval *value;
3490         struct berval digest;
3491
3492         if((flags & SLAP_INDEX_SUBSTR_INITIAL) && sa->sa_initial != NULL &&
3493                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
3494         {
3495                 nkeys++;
3496         }
3497
3498         if((flags & SLAP_INDEX_SUBSTR_ANY) && sa->sa_any != NULL ) {
3499                 ber_len_t i;
3500                 for( i=0; sa->sa_any[i] != NULL; i++ ) {
3501                         if( sa->sa_any[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
3502                                 /* don't bother accounting for stepping */
3503                                 nkeys += sa->sa_any[i]->bv_len -
3504                                         ( SLAP_INDEX_SUBSTR_MAXLEN - 1 );
3505                         }
3506                 }
3507         }
3508
3509         if((flags & SLAP_INDEX_SUBSTR_FINAL) && sa->sa_final != NULL &&
3510                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
3511         {
3512                 nkeys++;
3513         }
3514
3515         if( nkeys == 0 ) {
3516                 *keysp = NULL;
3517                 return LDAP_SUCCESS;
3518         }
3519
3520         digest.bv_val = HASHdigest;
3521         digest.bv_len = sizeof(HASHdigest);
3522
3523         slen = strlen( syntax->ssyn_oid );
3524         mlen = strlen( mr->smr_oid );
3525
3526         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
3527         nkeys = 0;
3528
3529         if((flags & SLAP_INDEX_SUBSTR_INITIAL) && sa->sa_initial != NULL &&
3530                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
3531         {
3532                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
3533                 value = ber_bvdup( sa->sa_initial );
3534                 ldap_pvt_str2upper( value->bv_val );
3535
3536                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
3537                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
3538
3539                 HASH_Init( &HASHcontext );
3540                 if( prefix != NULL && prefix->bv_len > 0 ) {
3541                         HASH_Update( &HASHcontext,
3542                                 prefix->bv_val, prefix->bv_len );
3543                 }
3544                 HASH_Update( &HASHcontext,
3545                         &pre, sizeof( pre ) );
3546                 HASH_Update( &HASHcontext,
3547                         syntax->ssyn_oid, slen );
3548                 HASH_Update( &HASHcontext,
3549                         mr->smr_oid, mlen );
3550                 HASH_Update( &HASHcontext,
3551                         value->bv_val, klen );
3552                 HASH_Final( HASHdigest, &HASHcontext );
3553
3554                 ber_bvfree( value );
3555                 keys[nkeys++] = ber_bvdup( &digest );
3556         }
3557
3558         if((flags & SLAP_INDEX_SUBSTR_ANY) && sa->sa_any != NULL ) {
3559                 ber_len_t i, j;
3560                 pre = SLAP_INDEX_SUBSTR_PREFIX;
3561                 klen = SLAP_INDEX_SUBSTR_MAXLEN;
3562
3563                 for( i=0; sa->sa_any[i] != NULL; i++ ) {
3564                         if( sa->sa_any[i]->bv_len < SLAP_INDEX_SUBSTR_MAXLEN ) {
3565                                 continue;
3566                         }
3567
3568                         value = ber_bvdup( sa->sa_any[i] );
3569                         ldap_pvt_str2upper( value->bv_val );
3570
3571                         for(j=0;
3572                                 j <= value->bv_len - SLAP_INDEX_SUBSTR_MAXLEN;
3573                                 j += SLAP_INDEX_SUBSTR_STEP )
3574                         {
3575                                 HASH_Init( &HASHcontext );
3576                                 if( prefix != NULL && prefix->bv_len > 0 ) {
3577                                         HASH_Update( &HASHcontext,
3578                                                 prefix->bv_val, prefix->bv_len );
3579                                 }
3580                                 HASH_Update( &HASHcontext,
3581                                         &pre, sizeof( pre ) );
3582                                 HASH_Update( &HASHcontext,
3583                                         syntax->ssyn_oid, slen );
3584                                 HASH_Update( &HASHcontext,
3585                                         mr->smr_oid, mlen );
3586                                 HASH_Update( &HASHcontext,
3587                                         &value->bv_val[j], klen );
3588                                 HASH_Final( HASHdigest, &HASHcontext );
3589
3590                                 keys[nkeys++] = ber_bvdup( &digest );
3591                         }
3592
3593                         ber_bvfree( value );
3594                 }
3595         }
3596
3597         if((flags & SLAP_INDEX_SUBSTR_FINAL) && sa->sa_final != NULL &&
3598                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
3599         {
3600                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
3601                 value = ber_bvdup( sa->sa_final );
3602                 ldap_pvt_str2upper( value->bv_val );
3603
3604                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
3605                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
3606
3607                 HASH_Init( &HASHcontext );
3608                 if( prefix != NULL && prefix->bv_len > 0 ) {
3609                         HASH_Update( &HASHcontext,
3610                                 prefix->bv_val, prefix->bv_len );
3611                 }
3612                 HASH_Update( &HASHcontext,
3613                         &pre, sizeof( pre ) );
3614                 HASH_Update( &HASHcontext,
3615                         syntax->ssyn_oid, slen );
3616                 HASH_Update( &HASHcontext,
3617                         mr->smr_oid, mlen );
3618                 HASH_Update( &HASHcontext,
3619                         &value->bv_val[value->bv_len-klen], klen );
3620                 HASH_Final( HASHdigest, &HASHcontext );
3621
3622                 ber_bvfree( value );
3623                 keys[nkeys++] = ber_bvdup( &digest );
3624         }
3625
3626         if( nkeys > 0 ) {
3627                 keys[nkeys] = NULL;
3628                 *keysp = keys;
3629         } else {
3630                 ch_free( keys );
3631                 *keysp = NULL;
3632         }
3633
3634         return LDAP_SUCCESS;
3635 }
3636         
3637 static int
3638 numericStringValidate(
3639         Syntax *syntax,
3640         struct berval *in )
3641 {
3642         ber_len_t i;
3643
3644         for(i=0; i < in->bv_len; i++) {
3645                 if( !SLAP_NUMERIC(in->bv_val[i]) ) {
3646                         return LDAP_INVALID_SYNTAX;
3647                 }
3648         }
3649
3650         return LDAP_SUCCESS;
3651 }
3652
3653 static int
3654 numericStringNormalize(
3655         Syntax *syntax,
3656         struct berval *val,
3657         struct berval **normalized )
3658 {
3659         /* removal all spaces */
3660         struct berval *newval;
3661         char *p, *q;
3662
3663         newval = ch_malloc( sizeof( struct berval ) );
3664         newval->bv_val = ch_malloc( val->bv_len + 1 );
3665
3666         p = val->bv_val;
3667         q = newval->bv_val;
3668
3669         while ( *p ) {
3670                 if ( ASCII_SPACE( *p ) ) {
3671                         /* Ignore whitespace */
3672                         p++;
3673                 } else {
3674                         *q++ = *p++;
3675                 }
3676         }
3677
3678         assert( newval->bv_val <= p );
3679         assert( q <= p );
3680
3681         /* null terminate */
3682         *q = '\0';
3683
3684         newval->bv_len = q - newval->bv_val;
3685         *normalized = newval;
3686
3687         return LDAP_SUCCESS;
3688 }
3689
3690 static int
3691 objectIdentifierFirstComponentMatch(
3692         int *matchp,
3693         slap_mask_t flags,
3694         Syntax *syntax,
3695         MatchingRule *mr,
3696         struct berval *value,
3697         void *assertedValue )
3698 {
3699         int rc = LDAP_SUCCESS;
3700         int match;
3701         struct berval *asserted = (struct berval *) assertedValue;
3702         ber_len_t i;
3703         struct berval oid;
3704
3705         if( value->bv_len == 0 || value->bv_val[0] != '(' /*')'*/ ) {
3706                 return LDAP_INVALID_SYNTAX;
3707         }
3708
3709         /* trim leading white space */
3710         for( i=1; ASCII_SPACE(value->bv_val[i]) && i < value->bv_len; i++ ) {
3711                 /* empty */
3712         }
3713
3714         /* grab next word */
3715         oid.bv_val = &value->bv_val[i];
3716         oid.bv_len = value->bv_len - i;
3717         for( i=1; ASCII_SPACE(value->bv_val[i]) && i < oid.bv_len; i++ ) {
3718                 /* empty */
3719         }
3720         oid.bv_len = i;
3721
3722         /* insert attributeTypes, objectclass check here */
3723         if( OID_LEADCHAR(asserted->bv_val[0]) ) {
3724                 rc = objectIdentifierMatch( &match, flags, syntax, mr, &oid, asserted );
3725
3726         } else {
3727                 char *stored = ch_malloc( oid.bv_len + 1 );
3728                 AC_MEMCPY( stored, oid.bv_val, oid.bv_len );
3729                 stored[oid.bv_len] = '\0';
3730
3731                 if ( !strcmp( syntax->ssyn_oid, SLAP_SYNTAX_MATCHINGRULES_OID ) ) {
3732                         MatchingRule *asserted_mr = mr_find( asserted->bv_val );
3733                         MatchingRule *stored_mr = mr_find( stored );
3734
3735                         if( asserted_mr == NULL ) {
3736                                 rc = SLAPD_COMPARE_UNDEFINED;
3737                         } else {
3738                                 match = asserted_mr != stored_mr;
3739                         }
3740
3741                 } else if ( !strcmp( syntax->ssyn_oid,
3742                         SLAP_SYNTAX_ATTRIBUTETYPES_OID ) )
3743                 {
3744                         AttributeType *asserted_at = at_find( asserted->bv_val );
3745                         AttributeType *stored_at = at_find( stored );
3746
3747                         if( asserted_at == NULL ) {
3748                                 rc = SLAPD_COMPARE_UNDEFINED;
3749                         } else {
3750                                 match = asserted_at != stored_at;
3751                         }
3752
3753                 } else if ( !strcmp( syntax->ssyn_oid,
3754                         SLAP_SYNTAX_OBJECTCLASSES_OID ) )
3755                 {
3756                         ObjectClass *asserted_oc = oc_find( asserted->bv_val );
3757                         ObjectClass *stored_oc = oc_find( stored );
3758
3759                         if( asserted_oc == NULL ) {
3760                                 rc = SLAPD_COMPARE_UNDEFINED;
3761                         } else {
3762                                 match = asserted_oc != stored_oc;
3763                         }
3764                 }
3765
3766                 ch_free( stored );
3767         }
3768
3769 #ifdef NEW_LOGGING
3770         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
3771                    "objectIdentifierFirstComponentMatch: %d\n    %s\n    %s\n",
3772                    match, value->bv_val, asserted->bv_val ));
3773 #else
3774         Debug( LDAP_DEBUG_ARGS, "objectIdentifierFirstComponentMatch "
3775                 "%d\n\t\"%s\"\n\t\"%s\"\n",
3776             match, value->bv_val, asserted->bv_val );
3777 #endif
3778
3779
3780         if( rc == LDAP_SUCCESS ) *matchp = match;
3781         return rc;
3782 }
3783
3784 static int
3785 check_time_syntax (struct berval *val,
3786         int start,
3787         int *parts)
3788 {
3789         static int ceiling[9] = { 99, 99, 11, 30, 23, 59, 59, 12, 59 };
3790         static int mdays[2][12] = {
3791                 /* non-leap years */
3792                 { 30, 27, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30 },
3793                 /* leap years */
3794                 { 30, 28, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30 }
3795         };
3796         char *p, *e;
3797         int part, c, tzoffset, leapyear = 0 ;
3798
3799         if( val->bv_len == 0 ) {
3800                 return LDAP_INVALID_SYNTAX;
3801         }
3802
3803         p = (char *)val->bv_val;
3804         e = p + val->bv_len;
3805
3806         /* Ignore initial whitespace */
3807         while ( ( p < e ) && ASCII_SPACE( *p ) ) {
3808                 p++;
3809         }
3810
3811         if (e - p < 13 - (2 * start)) {
3812                 return LDAP_INVALID_SYNTAX;
3813         }
3814
3815         for (part = 0; part < 9; part++) {
3816                 parts[part] = 0;
3817         }
3818
3819         for (part = start; part < 7; part++) {
3820                 c = *p;
3821                 if ((part == 6) && (c == 'Z' || c == '+' || c == '-')) {
3822                         part++;
3823                         break;
3824                 }
3825                 p++;
3826                 c -= '0';
3827                 if (p == e) {
3828                         return LDAP_INVALID_SYNTAX;
3829                 }
3830                 if (c < 0 || c > 9) {
3831                         return LDAP_INVALID_SYNTAX;
3832                 }
3833                 parts[part] = c;
3834
3835                 c = *p++ - '0';
3836                 if (p == e) {
3837                         return LDAP_INVALID_SYNTAX;
3838                 }
3839                 if (c < 0 || c > 9) {
3840                         return LDAP_INVALID_SYNTAX;
3841                 }
3842                 parts[part] *= 10;
3843                 parts[part] += c;
3844
3845                 if (part == 2 || part == 3) {
3846                         parts[part]--;
3847                 }
3848                 if (parts[part] < 0) {
3849                         return LDAP_INVALID_SYNTAX;
3850                 }
3851                 if (parts[part] > ceiling[part]) {
3852                         return LDAP_INVALID_SYNTAX;
3853                 }
3854         }
3855
3856         /* leapyear check for the Gregorian calendar (year>1581) */
3857         if (((parts[1] % 4 == 0) && (parts[1] != 0)) ||
3858                 ((parts[0] % 4 == 0) && (parts[1] == 0)))
3859         {
3860                 leapyear = 1;
3861         }
3862
3863         if (parts[3] > mdays[leapyear][parts[2]]) {
3864                 return LDAP_INVALID_SYNTAX;
3865         }
3866         
3867         c = *p++;
3868         if (c == 'Z') {
3869                 tzoffset = 0; /* UTC */
3870         } else if (c != '+' && c != '-') {
3871                 return LDAP_INVALID_SYNTAX;
3872         } else {
3873                 if (c == '-') {
3874                         tzoffset = -1;
3875                 } else /* c == '+' */ {
3876                         tzoffset = 1;
3877                 }
3878
3879                 if (p > e - 4) {
3880                         return LDAP_INVALID_SYNTAX;
3881                 }
3882
3883                 for (part = 7; part < 9; part++) {
3884                         c = *p++ - '0';
3885                         if (c < 0 || c > 9) {
3886                                 return LDAP_INVALID_SYNTAX;
3887                         }
3888                         parts[part] = c;
3889
3890                         c = *p++ - '0';
3891                         if (c < 0 || c > 9) {
3892                                 return LDAP_INVALID_SYNTAX;
3893                         }
3894                         parts[part] *= 10;
3895                         parts[part] += c;
3896                         if (parts[part] < 0 || parts[part] > ceiling[part]) {
3897                                 return LDAP_INVALID_SYNTAX;
3898                         }
3899                 }
3900         }
3901
3902         /* Ignore trailing whitespace */
3903         while ( ( p < e ) && ASCII_SPACE( *p ) ) {
3904                 p++;
3905         }
3906         if (p != e) {
3907                 return LDAP_INVALID_SYNTAX;
3908         }
3909
3910         switch ( tzoffset ) {
3911         case -1: /* negativ offset to UTC, ie west of Greenwich  */
3912                 parts[4] += parts[7];
3913                 parts[5] += parts[8];
3914                 for (part = 6; --part > 0; ) { /* offset is just hhmm, no seconds */
3915                         if (part != 3) {
3916                                 c = ceiling[part];
3917                         } else {
3918                                 c = mdays[leapyear][parts[2]];
3919                         }
3920                         if (parts[part] > c) {
3921                                 parts[part] -= c + 1;
3922                                 parts[part - 1]++;
3923                         }
3924                 }
3925                 break;
3926         case 1: /* positive offset to UTC, ie east of Greenwich */
3927                 parts[4] -= parts[7];
3928                 parts[5] -= parts[8];
3929                 for (part = 6; --part > 0; ) {
3930                         if (part != 3) {
3931                                 c = ceiling[part];
3932                         } else {
3933                                 /* first arg to % needs to be non negativ */
3934                                 c = mdays[leapyear][(parts[2] - 1 + 12) % 12];
3935                         }
3936                         if (parts[part] < 0) {
3937                                 parts[part] += c + 1;
3938                                 parts[part - 1]--;
3939                         }
3940                 }
3941                 break;
3942         case 0: /* already UTC */
3943                 break;
3944         }
3945
3946         return LDAP_SUCCESS;
3947 }
3948
3949 static int
3950 utcTimeNormalize(
3951         Syntax *syntax,
3952         struct berval *val,
3953         struct berval **normalized )
3954 {
3955         struct berval *out;
3956         int parts[9], rc;
3957
3958         rc = check_time_syntax(val, 1, parts);
3959         if (rc != LDAP_SUCCESS) {
3960                 return rc;
3961         }
3962
3963         *normalized = NULL;
3964         out = ch_malloc( sizeof(struct berval) );
3965         if( out == NULL ) {
3966                 return LBER_ERROR_MEMORY;
3967         }
3968
3969         out->bv_val = ch_malloc( 14 );
3970         if ( out->bv_val == NULL ) {
3971                 ch_free( out );
3972                 return LBER_ERROR_MEMORY;
3973         }
3974
3975         sprintf( out->bv_val, "%02ld%02ld%02ld%02ld%02ld%02ldZ",
3976                 parts[1], parts[2] + 1, parts[3] + 1,
3977                 parts[4], parts[5], parts[6] );
3978         out->bv_len = 13;
3979         *normalized = out;
3980
3981         return LDAP_SUCCESS;
3982 }
3983
3984 static int
3985 utcTimeValidate(
3986         Syntax *syntax,
3987         struct berval *in )
3988 {
3989         int parts[9];
3990
3991         return check_time_syntax(in, 1, parts);
3992 }
3993
3994 static int
3995 generalizedTimeValidate(
3996         Syntax *syntax,
3997         struct berval *in )
3998 {
3999         int parts[9];
4000
4001         return check_time_syntax(in, 0, parts);
4002 }
4003
4004 static int
4005 generalizedTimeNormalize(
4006         Syntax *syntax,
4007         struct berval *val,
4008         struct berval **normalized )
4009 {
4010         struct berval *out;
4011         int parts[9], rc;
4012
4013         rc = check_time_syntax(val, 0, parts);
4014         if (rc != LDAP_SUCCESS) {
4015                 return rc;
4016         }
4017
4018         *normalized = NULL;
4019         out = ch_malloc( sizeof(struct berval) );
4020         if( out == NULL ) {
4021                 return LBER_ERROR_MEMORY;
4022         }
4023
4024         out->bv_val = ch_malloc( 16 );
4025         if ( out->bv_val == NULL ) {
4026                 ch_free( out );
4027                 return LBER_ERROR_MEMORY;
4028         }
4029
4030         sprintf( out->bv_val, "%02ld%02ld%02ld%02ld%02ld%02ld%02ldZ",
4031                 parts[0], parts[1], parts[2] + 1, parts[3] + 1,
4032                 parts[4], parts[5], parts[6] );
4033         out->bv_len = 15;
4034         *normalized = out;
4035
4036         return LDAP_SUCCESS;
4037 }
4038
4039 static int
4040 nisNetgroupTripleValidate(
4041         Syntax *syntax,
4042         struct berval *val )
4043 {
4044         char *p, *e;
4045         int commas = 0;
4046
4047         if ( val->bv_len == 0 ) {
4048                 return LDAP_INVALID_SYNTAX;
4049         }
4050
4051         p = (char *)val->bv_val;
4052         e = p + val->bv_len;
4053
4054 #if 0
4055         /* syntax does not allow leading white space */
4056         /* Ignore initial whitespace */
4057         while ( ( p < e ) && ASCII_SPACE( *p ) ) {
4058                 p++;
4059         }
4060 #endif
4061
4062         if ( *p != '(' /*')'*/ ) {
4063                 return LDAP_INVALID_SYNTAX;
4064         }
4065
4066         for ( p++; ( p < e ) && ( *p != ')' ); p++ ) {
4067                 if ( *p == ',' ) {
4068                         commas++;
4069                         if ( commas > 2 ) {
4070                                 return LDAP_INVALID_SYNTAX;
4071                         }
4072
4073                 } else if ( !ATTR_CHAR( *p ) ) {
4074                         return LDAP_INVALID_SYNTAX;
4075                 }
4076         }
4077
4078         if ( ( commas != 2 ) || ( *p != /*'('*/ ')' ) ) {
4079                 return LDAP_INVALID_SYNTAX;
4080         }
4081
4082         p++;
4083
4084 #if 0
4085         /* syntax does not allow trailing white space */
4086         /* Ignore trailing whitespace */
4087         while ( ( p < e ) && ASCII_SPACE( *p ) ) {
4088                 p++;
4089         }
4090 #endif
4091
4092         if (p != e) {
4093                 return LDAP_INVALID_SYNTAX;
4094         }
4095
4096         return LDAP_SUCCESS;
4097 }
4098
4099 static int
4100 bootParameterValidate(
4101         Syntax *syntax,
4102         struct berval *val )
4103 {
4104         char *p, *e;
4105
4106         if ( val->bv_len == 0 ) {
4107                 return LDAP_INVALID_SYNTAX;
4108         }
4109
4110         p = (char *)val->bv_val;
4111         e = p + val->bv_len;
4112
4113         /* key */
4114         for (; ( p < e ) && ( *p != '=' ); p++ ) {
4115                 if ( !ATTR_CHAR( *p ) ) {
4116                         return LDAP_INVALID_SYNTAX;
4117                 }
4118         }
4119
4120         if ( *p != '=' ) {
4121                 return LDAP_INVALID_SYNTAX;
4122         }
4123
4124         /* server */
4125         for ( p++; ( p < e ) && ( *p != ':' ); p++ ) {
4126                 if ( !ATTR_CHAR( *p ) ) {
4127                         return LDAP_INVALID_SYNTAX;
4128                 }
4129         }
4130
4131         if ( *p != ':' ) {
4132                 return LDAP_INVALID_SYNTAX;
4133         }
4134
4135         /* path */
4136         for ( p++; p < e; p++ ) {
4137                 if ( !ATTR_CHAR( *p ) ) {
4138                         return LDAP_INVALID_SYNTAX;
4139                 }
4140         }
4141
4142         return LDAP_SUCCESS;
4143 }
4144
4145 struct syntax_defs_rec {
4146         char *sd_desc;
4147         int sd_flags;
4148         slap_syntax_validate_func *sd_validate;
4149         slap_syntax_transform_func *sd_normalize;
4150         slap_syntax_transform_func *sd_pretty;
4151 #ifdef SLAPD_BINARY_CONVERSION
4152         slap_syntax_transform_func *sd_ber2str;
4153         slap_syntax_transform_func *sd_str2ber;
4154 #endif
4155 };
4156
4157 #define X_BINARY "X-BINARY-TRANSFER-REQUIRED 'TRUE' "
4158 #define X_NOT_H_R "X-NOT-HUMAN-READABLE 'TRUE' "
4159
4160 struct syntax_defs_rec syntax_defs[] = {
4161         {"( 1.3.6.1.4.1.1466.115.121.1.1 DESC 'ACI Item' " X_BINARY X_NOT_H_R ")",
4162                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, NULL, NULL, NULL},
4163         {"( 1.3.6.1.4.1.1466.115.121.1.2 DESC 'Access Point' " X_NOT_H_R ")",
4164                 0, NULL, NULL, NULL},
4165         {"( 1.3.6.1.4.1.1466.115.121.1.3 DESC 'Attribute Type Description' )",
4166                 0, NULL, NULL, NULL},
4167         {"( 1.3.6.1.4.1.1466.115.121.1.4 DESC 'Audio' " X_NOT_H_R ")",
4168                 SLAP_SYNTAX_BLOB, blobValidate, NULL, NULL},
4169         {"( 1.3.6.1.4.1.1466.115.121.1.5 DESC 'Binary' " X_NOT_H_R ")",
4170                 SLAP_SYNTAX_BER, berValidate, NULL, NULL},
4171         {"( 1.3.6.1.4.1.1466.115.121.1.6 DESC 'Bit String' )",
4172                 0, bitStringValidate, NULL, NULL },
4173         {"( 1.3.6.1.4.1.1466.115.121.1.7 DESC 'Boolean' )",
4174                 0, booleanValidate, NULL, NULL},
4175         {"( 1.3.6.1.4.1.1466.115.121.1.8 DESC 'Certificate' "
4176                 X_BINARY X_NOT_H_R ")",
4177                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
4178         {"( 1.3.6.1.4.1.1466.115.121.1.9 DESC 'Certificate List' "
4179                 X_BINARY X_NOT_H_R ")",
4180                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
4181         {"( 1.3.6.1.4.1.1466.115.121.1.10 DESC 'Certificate Pair' "
4182                 X_BINARY X_NOT_H_R ")",
4183                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
4184         {"( 1.3.6.1.4.1.1466.115.121.1.11 DESC 'Country String' )",
4185                 0, countryStringValidate, IA5StringNormalize, NULL},
4186         {"( 1.3.6.1.4.1.1466.115.121.1.12 DESC 'Distinguished Name' )",
4187                 0, dnValidate, dnNormalize, dnPretty},
4188         {"( 1.3.6.1.4.1.1466.115.121.1.13 DESC 'Data Quality' )",
4189                 0, NULL, NULL, NULL},
4190         {"( 1.3.6.1.4.1.1466.115.121.1.14 DESC 'Delivery Method' )",
4191                 0, NULL, NULL, NULL},
4192         {"( 1.3.6.1.4.1.1466.115.121.1.15 DESC 'Directory String' )",
4193                 0, UTF8StringValidate, UTF8StringNormalize, NULL},
4194         {"( 1.3.6.1.4.1.1466.115.121.1.16 DESC 'DIT Content Rule Description' )",
4195                 0, NULL, NULL, NULL},
4196         {"( 1.3.6.1.4.1.1466.115.121.1.17 DESC 'DIT Structure Rule Description' )",
4197                 0, NULL, NULL, NULL},
4198         {"( 1.3.6.1.4.1.1466.115.121.1.19 DESC 'DSA Quality' )",
4199                 0, NULL, NULL, NULL},
4200         {"( 1.3.6.1.4.1.1466.115.121.1.20 DESC 'DSE Type' )",
4201                 0, NULL, NULL, NULL},
4202         {"( 1.3.6.1.4.1.1466.115.121.1.21 DESC 'Enhanced Guide' )",
4203                 0, NULL, NULL, NULL},
4204         {"( 1.3.6.1.4.1.1466.115.121.1.22 DESC 'Facsimile Telephone Number' )",
4205                 0, printablesStringValidate, IA5StringNormalize, NULL},
4206         {"( 1.3.6.1.4.1.1466.115.121.1.23 DESC 'Fax' " X_NOT_H_R ")",
4207                 SLAP_SYNTAX_BLOB, NULL, NULL, NULL},
4208         {"( 1.3.6.1.4.1.1466.115.121.1.24 DESC 'Generalized Time' )",
4209                 0, generalizedTimeValidate, generalizedTimeNormalize, NULL},
4210         {"( 1.3.6.1.4.1.1466.115.121.1.25 DESC 'Guide' )",
4211                 0, NULL, NULL, NULL},
4212         {"( 1.3.6.1.4.1.1466.115.121.1.26 DESC 'IA5 String' )",
4213                 0, IA5StringValidate, IA5StringNormalize, NULL},
4214         {"( 1.3.6.1.4.1.1466.115.121.1.27 DESC 'Integer' )",
4215                 0, integerValidate, integerNormalize, integerPretty},
4216         {"( 1.3.6.1.4.1.1466.115.121.1.28 DESC 'JPEG' " X_NOT_H_R ")",
4217                 SLAP_SYNTAX_BLOB, blobValidate, NULL, NULL},
4218         {"( 1.3.6.1.4.1.1466.115.121.1.29 DESC 'Master And Shadow Access Points' )",
4219                 0, NULL, NULL, NULL},
4220         {"( 1.3.6.1.4.1.1466.115.121.1.30 DESC 'Matching Rule Description' )",
4221                 0, NULL, NULL, NULL},
4222         {"( 1.3.6.1.4.1.1466.115.121.1.31 DESC 'Matching Rule Use Description' )",
4223                 0, NULL, NULL, NULL},
4224         {"( 1.3.6.1.4.1.1466.115.121.1.32 DESC 'Mail Preference' )",
4225                 0, NULL, NULL, NULL},
4226         {"( 1.3.6.1.4.1.1466.115.121.1.33 DESC 'MHS OR Address' )",
4227                 0, NULL, NULL, NULL},
4228         {"( 1.3.6.1.4.1.1466.115.121.1.34 DESC 'Name And Optional UID' )",
4229                 0, nameUIDValidate, nameUIDNormalize, NULL},
4230         {"( 1.3.6.1.4.1.1466.115.121.1.35 DESC 'Name Form Description' )",
4231                 0, NULL, NULL, NULL},
4232         {"( 1.3.6.1.4.1.1466.115.121.1.36 DESC 'Numeric String' )",
4233                 0, numericStringValidate, numericStringNormalize, NULL},
4234         {"( 1.3.6.1.4.1.1466.115.121.1.37 DESC 'Object Class Description' )",
4235                 0, NULL, NULL, NULL},
4236         {"( 1.3.6.1.4.1.1466.115.121.1.38 DESC 'OID' )",
4237                 0, oidValidate, NULL, NULL},
4238         {"( 1.3.6.1.4.1.1466.115.121.1.39 DESC 'Other Mailbox' )",
4239                 0, IA5StringValidate, IA5StringNormalize, NULL},
4240         {"( 1.3.6.1.4.1.1466.115.121.1.40 DESC 'Octet String' )",
4241                 0, blobValidate, NULL, NULL},
4242         {"( 1.3.6.1.4.1.1466.115.121.1.41 DESC 'Postal Address' )",
4243                 0, UTF8StringValidate, UTF8StringNormalize, NULL},
4244         {"( 1.3.6.1.4.1.1466.115.121.1.42 DESC 'Protocol Information' )",
4245                 0, NULL, NULL, NULL},
4246         {"( 1.3.6.1.4.1.1466.115.121.1.43 DESC 'Presentation Address' )",
4247                 0, NULL, NULL, NULL},
4248         {"( 1.3.6.1.4.1.1466.115.121.1.44 DESC 'Printable String' )",
4249                 0, printableStringValidate, IA5StringNormalize, NULL},
4250         {"( 1.3.6.1.4.1.1466.115.121.1.49 DESC 'Supported Algorithm' "
4251                 X_BINARY X_NOT_H_R ")",
4252                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
4253         {"( 1.3.6.1.4.1.1466.115.121.1.50 DESC 'Telephone Number' )",
4254                 0, printableStringValidate, IA5StringNormalize, NULL},
4255         {"( 1.3.6.1.4.1.1466.115.121.1.51 DESC 'Teletex Terminal Identifier' )",
4256                 0, NULL, NULL, NULL},
4257         {"( 1.3.6.1.4.1.1466.115.121.1.52 DESC 'Telex Number' )",
4258                 0, printableStringValidate, IA5StringNormalize, NULL},
4259         {"( 1.3.6.1.4.1.1466.115.121.1.53 DESC 'UTC Time' )",
4260                 0, utcTimeValidate, utcTimeNormalize, NULL},
4261         {"( 1.3.6.1.4.1.1466.115.121.1.54 DESC 'LDAP Syntax Description' )",
4262                 0, NULL, NULL, NULL},
4263         {"( 1.3.6.1.4.1.1466.115.121.1.55 DESC 'Modify Rights' )",
4264                 0, NULL, NULL, NULL},
4265         {"( 1.3.6.1.4.1.1466.115.121.1.56 DESC 'LDAP Schema Definition' )",
4266                 0, NULL, NULL, NULL},
4267         {"( 1.3.6.1.4.1.1466.115.121.1.57 DESC 'LDAP Schema Description' )",
4268                 0, NULL, NULL, NULL},
4269         {"( 1.3.6.1.4.1.1466.115.121.1.58 DESC 'Substring Assertion' )",
4270                 0, NULL, NULL, NULL},
4271
4272         /* RFC 2307 NIS Syntaxes */
4273         {"( 1.3.6.1.1.1.0.0  DESC 'RFC2307 NIS Netgroup Triple' )",
4274                 0, nisNetgroupTripleValidate, NULL, NULL},
4275         {"( 1.3.6.1.1.1.0.1  DESC 'RFC2307 Boot Parameter' )",
4276                 0, bootParameterValidate, NULL, NULL},
4277
4278         /* OpenLDAP Experimental Syntaxes */
4279         {"( 1.3.6.1.4.1.4203.666.2.1 DESC 'OpenLDAP Experimental ACI' )",
4280                 0, UTF8StringValidate /* THIS WILL CHANGE FOR NEW ACI SYNTAX */,
4281                 NULL, NULL},
4282         {"( 1.3.6.1.4.1.4203.666.2.2 DESC 'OpenLDAP authPassword' )",
4283                 0, NULL, NULL, NULL},
4284
4285         /* OpenLDAP Void Syntax */
4286         {"( 1.3.6.1.4.1.4203.1.1.1 DESC 'OpenLDAP void' )" ,
4287                 SLAP_SYNTAX_HIDE, inValidate, NULL, NULL},
4288         {NULL, 0, NULL, NULL, NULL}
4289 };
4290
4291 struct mrule_defs_rec {
4292         char *                                          mrd_desc;
4293         slap_mask_t                                     mrd_usage;
4294         slap_mr_convert_func *          mrd_convert;
4295         slap_mr_normalize_func *        mrd_normalize;
4296         slap_mr_match_func *            mrd_match;
4297         slap_mr_indexer_func *          mrd_indexer;
4298         slap_mr_filter_func *           mrd_filter;
4299
4300         char *                                          mrd_associated;
4301 };
4302
4303 /*
4304  * Other matching rules in X.520 that we do not use (yet):
4305  *
4306  * 2.5.13.9             numericStringOrderingMatch
4307  * 2.5.13.15    integerOrderingMatch
4308  * 2.5.13.18    octetStringOrderingMatch
4309  * 2.5.13.19    octetStringSubstringsMatch
4310  * 2.5.13.25    uTCTimeMatch
4311  * 2.5.13.26    uTCTimeOrderingMatch
4312  * 2.5.13.31    directoryStringFirstComponentMatch
4313  * 2.5.13.32    wordMatch
4314  * 2.5.13.33    keywordMatch
4315  * 2.5.13.34    certificateExactMatch
4316  * 2.5.13.35    certificateMatch
4317  * 2.5.13.36    certificatePairExactMatch
4318  * 2.5.13.37    certificatePairMatch
4319  * 2.5.13.38    certificateListExactMatch
4320  * 2.5.13.39    certificateListMatch
4321  * 2.5.13.40    algorithmIdentifierMatch
4322  * 2.5.13.41    storedPrefixMatch
4323  * 2.5.13.42    attributeCertificateMatch
4324  * 2.5.13.43    readerAndKeyIDMatch
4325  * 2.5.13.44    attributeIntegrityMatch
4326  */
4327
4328 struct mrule_defs_rec mrule_defs[] = {
4329         /*
4330          * EQUALITY matching rules must be listed after associated APPROX
4331          * matching rules.  So, we list all APPROX matching rules first.
4332          */
4333         {"( " directoryStringApproxMatchOID " NAME 'directoryStringApproxMatch' "
4334                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
4335                 SLAP_MR_EQUALITY_APPROX | SLAP_MR_EXT,
4336                 NULL, NULL,
4337                 directoryStringApproxMatch,
4338                 directoryStringApproxIndexer, 
4339                 directoryStringApproxFilter,
4340                 NULL},
4341
4342         {"( " IA5StringApproxMatchOID " NAME 'IA5StringApproxMatch' "
4343                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
4344                 SLAP_MR_EQUALITY_APPROX | SLAP_MR_EXT,
4345                 NULL, NULL,
4346                 IA5StringApproxMatch,
4347                 IA5StringApproxIndexer, 
4348                 IA5StringApproxFilter,
4349                 NULL},
4350
4351         /*
4352          * Other matching rules
4353          */
4354         
4355         {"( 2.5.13.0 NAME 'objectIdentifierMatch' "
4356                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
4357                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4358                 NULL, NULL,
4359                 objectIdentifierMatch, caseIgnoreIA5Indexer, caseIgnoreIA5Filter,
4360                 NULL},
4361
4362         {"( 2.5.13.1 NAME 'distinguishedNameMatch' "
4363                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )",
4364                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4365                 NULL, NULL,
4366                 dnMatch, dnIndexer, dnFilter,
4367                 NULL},
4368
4369         {"( 2.5.13.2 NAME 'caseIgnoreMatch' "
4370                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
4371                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4372                 NULL, NULL,
4373                 caseIgnoreMatch, caseIgnoreIndexer, caseIgnoreFilter,
4374                 directoryStringApproxMatchOID },
4375
4376         {"( 2.5.13.3 NAME 'caseIgnoreOrderingMatch' "
4377                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
4378                 SLAP_MR_ORDERING,
4379                 NULL, NULL,
4380                 caseIgnoreOrderingMatch, NULL, NULL,
4381                 NULL},
4382
4383         {"( 2.5.13.4 NAME 'caseIgnoreSubstringsMatch' "
4384                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
4385                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
4386                 NULL, NULL,
4387                 caseIgnoreSubstringsMatch,
4388                 caseIgnoreSubstringsIndexer,
4389                 caseIgnoreSubstringsFilter,
4390                 NULL},
4391
4392         {"( 2.5.13.5 NAME 'caseExactMatch' "
4393                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
4394                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4395                 NULL, NULL,
4396                 caseExactMatch, caseExactIndexer, caseExactFilter,
4397                 directoryStringApproxMatchOID },
4398
4399         {"( 2.5.13.6 NAME 'caseExactOrderingMatch' "
4400                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
4401                 SLAP_MR_ORDERING,
4402                 NULL, NULL,
4403                 caseExactOrderingMatch, NULL, NULL,
4404                 NULL},
4405
4406         {"( 2.5.13.7 NAME 'caseExactSubstringsMatch' "
4407                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
4408                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
4409                 NULL, NULL,
4410                 caseExactSubstringsMatch,
4411                 caseExactSubstringsIndexer,
4412                 caseExactSubstringsFilter,
4413                 NULL},
4414
4415         {"( 2.5.13.8 NAME 'numericStringMatch' "
4416                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.36 )",
4417                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4418                 NULL, NULL,
4419                 caseIgnoreIA5Match,
4420                 caseIgnoreIA5Indexer,
4421                 caseIgnoreIA5Filter,
4422                 NULL},
4423
4424         {"( 2.5.13.10 NAME 'numericStringSubstringsMatch' "
4425                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
4426                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
4427                 NULL, NULL,
4428                 caseIgnoreIA5SubstringsMatch,
4429                 caseIgnoreIA5SubstringsIndexer,
4430                 caseIgnoreIA5SubstringsFilter,
4431                 NULL},
4432
4433         {"( 2.5.13.11 NAME 'caseIgnoreListMatch' "
4434                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.41 )",
4435                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4436                 NULL, NULL,
4437                 caseIgnoreListMatch, NULL, NULL,
4438                 NULL},
4439
4440         {"( 2.5.13.12 NAME 'caseIgnoreListSubstringsMatch' "
4441                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
4442                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
4443                 NULL, NULL,
4444                 caseIgnoreListSubstringsMatch, NULL, NULL,
4445                 NULL},
4446
4447         {"( 2.5.13.13 NAME 'booleanMatch' "
4448                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 )",
4449                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4450                 NULL, NULL,
4451                 booleanMatch, NULL, NULL,
4452                 NULL},
4453
4454         {"( 2.5.13.14 NAME 'integerMatch' "
4455                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
4456                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4457                 NULL, NULL,
4458                 integerMatch, integerIndexer, integerFilter,
4459                 NULL},
4460
4461         {"( 2.5.13.16 NAME 'bitStringMatch' "
4462                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.6 )",
4463                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4464                 NULL, NULL,
4465                 bitStringMatch, NULL, NULL,
4466                 NULL},
4467
4468         {"( 2.5.13.17 NAME 'octetStringMatch' "
4469                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
4470                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4471                 NULL, NULL,
4472                 octetStringMatch, octetStringIndexer, octetStringFilter,
4473                 NULL},
4474
4475         {"( 2.5.13.20 NAME 'telephoneNumberMatch' "
4476                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.50 )",
4477                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4478                 NULL, NULL,
4479                 telephoneNumberMatch,
4480                 telephoneNumberIndexer,
4481                 telephoneNumberFilter,
4482                 NULL},
4483
4484         {"( 2.5.13.21 NAME 'telephoneNumberSubstringsMatch' "
4485                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
4486                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
4487                 NULL, NULL,
4488                 telephoneNumberSubstringsMatch,
4489                 telephoneNumberSubstringsIndexer,
4490                 telephoneNumberSubstringsFilter,
4491                 NULL},
4492
4493         {"( 2.5.13.22 NAME 'presentationAddressMatch' "
4494                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.43 )",
4495                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4496                 NULL, NULL,
4497                 NULL, NULL, NULL,
4498                 NULL},
4499
4500         {"( 2.5.13.23 NAME 'uniqueMemberMatch' "
4501                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.34 )",
4502                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4503                 NULL, NULL,
4504                 uniqueMemberMatch, NULL, NULL,
4505                 NULL},
4506
4507         {"( 2.5.13.24 NAME 'protocolInformationMatch' "
4508                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.42 )",
4509                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4510                 NULL, NULL,
4511                 protocolInformationMatch, NULL, NULL,
4512                 NULL},
4513
4514         {"( 2.5.13.27 NAME 'generalizedTimeMatch' "
4515                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )",
4516                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4517                 NULL, NULL,
4518                 generalizedTimeMatch, NULL, NULL,
4519                 NULL},
4520
4521         {"( 2.5.13.28 NAME 'generalizedTimeOrderingMatch' "
4522                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )",
4523                 SLAP_MR_ORDERING,
4524                 NULL, NULL,
4525                 generalizedTimeOrderingMatch, NULL, NULL,
4526                 NULL},
4527
4528         {"( 2.5.13.29 NAME 'integerFirstComponentMatch' "
4529                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
4530                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4531                 NULL, NULL,
4532                 integerFirstComponentMatch, NULL, NULL,
4533                 NULL},
4534
4535         {"( 2.5.13.30 NAME 'objectIdentifierFirstComponentMatch' "
4536                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
4537                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4538                 NULL, NULL,
4539                 objectIdentifierFirstComponentMatch, NULL, NULL,
4540                 NULL},
4541
4542         {"( 1.3.6.1.4.1.1466.109.114.1 NAME 'caseExactIA5Match' "
4543                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
4544                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4545                 NULL, NULL,
4546                 caseExactIA5Match, caseExactIA5Indexer, caseExactIA5Filter,
4547                 IA5StringApproxMatchOID },
4548
4549         {"( 1.3.6.1.4.1.1466.109.114.2 NAME 'caseIgnoreIA5Match' "
4550                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
4551                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
4552                 NULL, NULL,
4553                 caseIgnoreIA5Match, caseIgnoreIA5Indexer, caseIgnoreIA5Filter,
4554                 IA5StringApproxMatchOID },
4555
4556         {"( 1.3.6.1.4.1.1466.109.114.3 NAME 'caseIgnoreIA5SubstringsMatch' "
4557                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
4558                 SLAP_MR_SUBSTR,
4559                 NULL, NULL,
4560                 caseIgnoreIA5SubstringsMatch,
4561                 caseIgnoreIA5SubstringsIndexer,
4562                 caseIgnoreIA5SubstringsFilter,
4563                 NULL},
4564
4565         {"( 1.3.6.1.4.1.4203.1.2.1 NAME 'caseExactIA5SubstringsMatch' "
4566                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
4567                 SLAP_MR_SUBSTR,
4568                 NULL, NULL,
4569                 caseExactIA5SubstringsMatch,
4570                 caseExactIA5SubstringsIndexer,
4571                 caseExactIA5SubstringsFilter,
4572                 NULL},
4573
4574         {"( 1.3.6.1.4.1.4203.666.4.1 NAME 'authPasswordMatch' "
4575                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
4576                 SLAP_MR_EQUALITY,
4577                 NULL, NULL,
4578                 authPasswordMatch, NULL, NULL,
4579                 NULL},
4580
4581         {"( 1.3.6.1.4.1.4203.666.4.2 NAME 'OpenLDAPaciMatch' "
4582                 "SYNTAX 1.3.6.1.4.1.4203.666.2.1 )",
4583                 SLAP_MR_EQUALITY,
4584                 NULL, NULL,
4585                 OpenLDAPaciMatch, NULL, NULL,
4586                 NULL},
4587
4588         {NULL, SLAP_MR_NONE, NULL, NULL, NULL, NULL}
4589 };
4590
4591 int
4592 schema_init( void )
4593 {
4594         int             res;
4595         int             i;
4596
4597         /* we should only be called once (from main) */
4598         assert( schema_init_done == 0 );
4599
4600         for ( i=0; syntax_defs[i].sd_desc != NULL; i++ ) {
4601                 res = register_syntax( syntax_defs[i].sd_desc,
4602                     syntax_defs[i].sd_flags,
4603                     syntax_defs[i].sd_validate,
4604                     syntax_defs[i].sd_normalize,
4605                         syntax_defs[i].sd_pretty
4606 #ifdef SLAPD_BINARY_CONVERSION
4607                         ,
4608                     syntax_defs[i].sd_ber2str,
4609                         syntax_defs[i].sd_str2ber
4610 #endif
4611                 );
4612
4613                 if ( res ) {
4614                         fprintf( stderr, "schema_init: Error registering syntax %s\n",
4615                                  syntax_defs[i].sd_desc );
4616                         return LDAP_OTHER;
4617                 }
4618         }
4619
4620         for ( i=0; mrule_defs[i].mrd_desc != NULL; i++ ) {
4621                 if( mrule_defs[i].mrd_usage == SLAP_MR_NONE ) {
4622                         fprintf( stderr,
4623                                 "schema_init: Ingoring unusable matching rule %s\n",
4624                                  mrule_defs[i].mrd_desc );
4625                         continue;
4626                 }
4627
4628                 res = register_matching_rule(
4629                         mrule_defs[i].mrd_desc,
4630                         mrule_defs[i].mrd_usage,
4631                         mrule_defs[i].mrd_convert,
4632                         mrule_defs[i].mrd_normalize,
4633                     mrule_defs[i].mrd_match,
4634                         mrule_defs[i].mrd_indexer,
4635                         mrule_defs[i].mrd_filter,
4636                         mrule_defs[i].mrd_associated );
4637
4638                 if ( res ) {
4639                         fprintf( stderr,
4640                                 "schema_init: Error registering matching rule %s\n",
4641                                  mrule_defs[i].mrd_desc );
4642                         return LDAP_OTHER;
4643                 }
4644         }
4645         schema_init_done = 1;
4646         return LDAP_SUCCESS;
4647 }