]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_init.c
152d8d44212440e5e8a5cfce30ff5b4653bd33d7
[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 #include "lutil_md5.h"
19
20 /* recycled validatation routines */
21 #define berValidate                                             blobValidate
22
23 /* unimplemented validators */
24 #define bitStringValidate                               NULL
25
26 /* recycled normalization routines */
27 #define faxNumberNormalize                              numericStringNormalize
28 #define phoneNumberNormalize                    numericStringNormalize
29 #define telexNumberNormalize                    numericStringNormalize
30 #define integerNormalize                                numericStringNormalize
31
32 /* unimplemented normalizers */
33 #define bitStringNormalize                              NULL
34
35 /* unimplemented pretters */
36 #define dnPretty                                                NULL
37 #define integerPretty                                   NULL
38
39 /* recycled matching routines */
40 #define caseIgnoreMatch                                 caseIgnoreIA5Match
41 #define caseIgnoreOrderingMatch                 caseIgnoreMatch
42 #define caseIgnoreSubstringsMatch               caseIgnoreIA5SubstringsMatch
43
44 #define caseExactMatch                                  caseExactIA5Match
45 #define caseExactOrderingMatch                  caseExactMatch
46 #define caseExactSubstringsMatch                caseExactIA5SubstringsMatch
47
48 #define numericStringMatch                              caseIgnoreMatch
49 #define objectIdentifierMatch                   numericStringMatch
50 #define integerMatch                                    numericStringMatch
51 #define telephoneNumberMatch                    numericStringMatch
52 #define telephoneNumberSubstringsMatch  caseIgnoreIA5SubstringsMatch
53 #define generalizedTimeMatch                    numericStringMatch
54 #define generalizedTimeOrderingMatch    numericStringMatch
55
56 /* approx matching rules */
57 #define directoryStringApproxMatchOID   "1.3.6.1.4.1.4203.666.4.4"
58 #define directoryStringApproxMatch              NULL
59 #define IA5StringApproxMatchOID                 "1.3.6.1.4.1.4203.666.4.5"
60 #define IA5StringApproxMatch                    NULL
61
62 /* unimplemented matching routines */
63 #define caseIgnoreListMatch                             NULL
64 #define caseIgnoreListSubstringsMatch   NULL
65 #define bitStringMatch                                  NULL
66 #define presentationAddressMatch                NULL
67 #define uniqueMemberMatch                               NULL
68 #define protocolInformationMatch                NULL
69 #define integerFirstComponentMatch              NULL
70
71 #define OpenLDAPaciMatch                                NULL
72 #define authPasswordMatch                               NULL
73
74 /* recycled indexing/filtering routines */
75 #define caseIgnoreIndexer                               caseIgnoreIA5Indexer
76 #define caseIgnoreFilter                                caseIgnoreIA5Filter
77 #define caseExactIndexer                                caseExactIA5Indexer
78 #define caseExactFilter                                 caseExactIA5Filter
79 #define dnIndexer                                               caseIgnoreIndexer
80 #define dnFilter                                                caseIgnoreFilter
81
82 #define caseIgnoreSubstringsIndexer             caseIgnoreIA5SubstringsIndexer
83 #define caseIgnoreSubstringsFilter              caseIgnoreIA5SubstringsFilter
84 #define caseExactSubstringsIndexer              caseExactIA5SubstringsIndexer
85 #define caseExactSubstringsFilter               caseExactIA5SubstringsFilter
86
87
88 static int
89 octetStringMatch(
90         int *matchp,
91         unsigned flags,
92         Syntax *syntax,
93         MatchingRule *mr,
94         struct berval *value,
95         void *assertedValue )
96 {
97         int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;
98
99         if( match == 0 ) {
100                 match = memcmp( value->bv_val,
101                         ((struct berval *) assertedValue)->bv_val,
102                         value->bv_len );
103         }
104
105         *matchp = match;
106         return LDAP_SUCCESS;
107 }
108
109 /* Index generation function */
110 int octetStringIndexer(
111         unsigned flags,
112         Syntax *syntax,
113         MatchingRule *mr,
114         struct berval *prefix,
115         struct berval **values,
116         struct berval ***keysp )
117 {
118         int i;
119         size_t slen, mlen;
120         struct berval **keys;
121         lutil_MD5_CTX   MD5context;
122         unsigned char   MD5digest[16];
123         struct berval digest;
124         digest.bv_val = MD5digest;
125         digest.bv_len = sizeof(MD5digest);
126
127         for( i=0; values[i] != NULL; i++ ) {
128                 /* just count them */
129         }
130
131         assert( i > 0 );
132
133         keys = ch_malloc( sizeof( struct berval * ) * (i+1) );
134
135         slen = strlen( syntax->ssyn_oid );
136         mlen = strlen( mr->smr_oid );
137
138         for( i=0; values[i] != NULL; i++ ) {
139                 lutil_MD5Init( &MD5context );
140                 if( prefix != NULL && prefix->bv_len > 0 ) {
141                         lutil_MD5Update( &MD5context,
142                                 prefix->bv_val, prefix->bv_len );
143                 }
144                 lutil_MD5Update( &MD5context,
145                         syntax->ssyn_oid, slen );
146                 lutil_MD5Update( &MD5context,
147                         mr->smr_oid, mlen );
148                 lutil_MD5Update( &MD5context,
149                         values[i]->bv_val, values[i]->bv_len );
150                 lutil_MD5Final( MD5digest, &MD5context );
151
152                 keys[i] = ber_bvdup( &digest );
153         }
154
155         keys[i] = NULL;
156
157         *keysp = keys;
158
159         return LDAP_SUCCESS;
160 }
161
162 /* Index generation function */
163 int octetStringFilter(
164         unsigned flags,
165         Syntax *syntax,
166         MatchingRule *mr,
167         struct berval *prefix,
168         void * assertValue,
169         struct berval ***keysp )
170 {
171         size_t slen, mlen;
172         struct berval **keys;
173         lutil_MD5_CTX   MD5context;
174         unsigned char   MD5digest[LUTIL_MD5_BYTES];
175         struct berval *value = (struct berval *) assertValue;
176         struct berval digest;
177         digest.bv_val = MD5digest;
178         digest.bv_len = sizeof(MD5digest);
179
180         slen = strlen( syntax->ssyn_oid );
181         mlen = strlen( mr->smr_oid );
182
183         keys = ch_malloc( sizeof( struct berval * ) * 2 );
184
185         lutil_MD5Init( &MD5context );
186         if( prefix != NULL && prefix->bv_len > 0 ) {
187                 lutil_MD5Update( &MD5context,
188                         prefix->bv_val, prefix->bv_len );
189         }
190         lutil_MD5Update( &MD5context,
191                 syntax->ssyn_oid, slen );
192         lutil_MD5Update( &MD5context,
193                 mr->smr_oid, mlen );
194         lutil_MD5Update( &MD5context,
195                 value->bv_val, value->bv_len );
196         lutil_MD5Final( MD5digest, &MD5context );
197
198         keys[0] = ber_bvdup( &digest );
199         keys[1] = NULL;
200
201         *keysp = keys;
202
203         return LDAP_SUCCESS;
204 }
205
206 static int
207 dnValidate(
208         Syntax *syntax,
209         struct berval *in )
210 {
211         int rc;
212         char *dn;
213
214         if( in->bv_len == 0 ) return LDAP_SUCCESS;
215
216         dn = ch_strdup( in->bv_val );
217
218         rc = dn_validate( dn ) == NULL
219                 ? LDAP_INVALID_SYNTAX : LDAP_SUCCESS;
220
221         ch_free( dn );
222         return rc;
223 }
224
225 static int
226 dnNormalize(
227         Syntax *syntax,
228         struct berval *val,
229         struct berval **normalized )
230 {
231         struct berval *out = ber_bvdup( val );
232
233         if( out->bv_len != 0 ) {
234                 char *dn;
235 #ifdef USE_DN_NORMALIZE
236                 dn = dn_normalize( out->bv_val );
237 #else
238                 dn = dn_validate( out->bv_val );
239 #endif
240
241                 if( dn == NULL ) {
242                         ber_bvfree( out );
243                         return LDAP_INVALID_SYNTAX;
244                 }
245
246                 out->bv_val = dn;
247                 out->bv_len = strlen( dn );
248         }
249
250         *normalized = out;
251         return LDAP_SUCCESS;
252 }
253
254 static int
255 dnMatch(
256         int *matchp,
257         unsigned flags,
258         Syntax *syntax,
259         MatchingRule *mr,
260         struct berval *value,
261         void *assertedValue )
262 {
263         int match;
264         struct berval *asserted = (struct berval *) assertedValue;
265         
266         match = value->bv_len - asserted->bv_len;
267
268         if( match == 0 ) {
269 #ifdef USE_DN_NORMALIZE
270                 match = strcmp( value->bv_val, asserted->bv_val );
271 #else
272                 match = strcasecmp( value->bv_val, asserted->bv_val );
273 #endif
274         }
275
276         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
277             match, value->bv_val, asserted->bv_val );
278
279         *matchp = match;
280         return LDAP_SUCCESS;
281 }
282
283 static int
284 inValidate(
285         Syntax *syntax,
286         struct berval *in )
287 {
288         /* any value allowed */
289         return LDAP_OTHER;
290 }
291
292 static int
293 blobValidate(
294         Syntax *syntax,
295         struct berval *in )
296 {
297         /* any value allowed */
298         return LDAP_SUCCESS;
299 }
300
301 /*
302  * Handling boolean syntax and matching is quite rigid.
303  * A more flexible approach would be to allow a variety
304  * of strings to be normalized and prettied into TRUE
305  * and FALSE.
306  */
307 static int
308 booleanValidate(
309         Syntax *syntax,
310         struct berval *in )
311 {
312         /* very unforgiving validation, requires no normalization
313          * before simplistic matching
314          */
315
316         if( in->bv_len == 4 ) {
317                 if( !memcmp( in->bv_val, "TRUE", 4 ) ) {
318                         return LDAP_SUCCESS;
319                 }
320         } else if( in->bv_len == 5 ) {
321                 if( !memcmp( in->bv_val, "FALSE", 5 ) ) {
322                         return LDAP_SUCCESS;
323                 }
324         }
325
326         return LDAP_INVALID_SYNTAX;
327 }
328
329 static int
330 booleanMatch(
331         int *matchp,
332         unsigned flags,
333         Syntax *syntax,
334         MatchingRule *mr,
335         struct berval *value,
336         void *assertedValue )
337 {
338         /* simplistic matching allowed by rigid validation */
339         struct berval *asserted = (struct berval *) assertedValue;
340         *matchp = value->bv_len != asserted->bv_len;
341         return LDAP_SUCCESS;
342 }
343
344 static int
345 UTF8StringValidate(
346         Syntax *syntax,
347         struct berval *in )
348 {
349         ber_len_t count;
350         int len;
351         unsigned char *u = in->bv_val;
352
353         if( !in->bv_len ) return LDAP_INVALID_SYNTAX;
354
355         for( count = in->bv_len; count > 0; count-=len, u+=len ) {
356                 /* get the length indicated by the first byte */
357                 len = LDAP_UTF8_CHARLEN( u );
358
359                 /* should not be zero */
360                 if( len == 0 ) return LDAP_INVALID_SYNTAX;
361
362                 /* make sure len corresponds with the offset
363                         to the next character */
364                 if( LDAP_UTF8_OFFSET( u ) != len ) return LDAP_INVALID_SYNTAX;
365         }
366
367         if( count != 0 ) return LDAP_INVALID_SYNTAX;
368
369         return LDAP_SUCCESS;
370 }
371
372 static int
373 UTF8StringNormalize(
374         Syntax *syntax,
375         struct berval *val,
376         struct berval **normalized )
377 {
378         struct berval *newval;
379         char *p, *q, *s;
380
381         newval = ch_malloc( sizeof( struct berval ) );
382
383         p = val->bv_val;
384
385         /* Ignore initial whitespace */
386         while ( ldap_utf8_isspace( p ) ) {
387                 LDAP_UTF8_INCR( p );
388         }
389
390         if( *p == '\0' ) {
391                 ch_free( newval );
392                 return LDAP_INVALID_SYNTAX;
393         }
394
395         newval->bv_val = ch_strdup( p );
396         p = q = newval->bv_val;
397         s = NULL;
398
399         while ( *p ) {
400                 int len;
401
402                 if ( ldap_utf8_isspace( p ) ) {
403                         len = LDAP_UTF8_COPY(q,p);
404                         s=q;
405                         p+=len;
406                         q+=len;
407
408                         /* Ignore the extra whitespace */
409                         while ( ldap_utf8_isspace( p ) ) {
410                                 LDAP_UTF8_INCR( p );
411                         }
412                 } else {
413                         len = LDAP_UTF8_COPY(q,p);
414                         s=NULL;
415                         p+=len;
416                         q+=len;
417                 }
418         }
419
420         assert( *newval->bv_val );
421         assert( newval->bv_val < p );
422         assert( p >= q );
423
424         /* cannot start with a space */
425         assert( !ldap_utf8_isspace(newval->bv_val) );
426
427         /*
428          * If the string ended in space, backup the pointer one
429          * position.  One is enough because the above loop collapsed
430          * all whitespace to a single space.
431          */
432
433         if ( s != NULL ) {
434                 q = s;
435         }
436
437         /* cannot end with a space */
438         assert( !ldap_utf8_isspace( LDAP_UTF8_PREV(q) ) );
439
440         /* null terminate */
441         *q = '\0';
442
443         newval->bv_len = q - newval->bv_val;
444         *normalized = newval;
445
446         return LDAP_SUCCESS;
447 }
448
449 static int
450 oidValidate(
451         Syntax *syntax,
452         struct berval *val )
453 {
454         ber_len_t i;
455
456         if( val->bv_len == 0 ) return 0;
457
458         if( OID_LEADCHAR(val->bv_val[0]) ) {
459                 int dot = 0;
460                 for(i=1; i < val->bv_len; i++) {
461                         if( OID_SEPARATOR( val->bv_val[i] ) ) {
462                                 if( dot++ ) return 1;
463                         } else if ( OID_CHAR( val->bv_val[i] ) ) {
464                                 dot = 0;
465                         } else {
466                                 return LDAP_INVALID_SYNTAX;
467                         }
468                 }
469
470                 return !dot ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
471
472         } else if( DESC_LEADCHAR(val->bv_val[0]) ) {
473                 for(i=1; i < val->bv_len; i++) {
474                         if( !DESC_CHAR(val->bv_val[i] ) ) {
475                                 return LDAP_INVALID_SYNTAX;
476                         }
477                 }
478
479                 return LDAP_SUCCESS;
480         }
481         
482         return LDAP_INVALID_SYNTAX;
483 }
484
485 static int
486 integerValidate(
487         Syntax *syntax,
488         struct berval *val )
489 {
490         ber_len_t i;
491
492         if( !val->bv_len ) return LDAP_INVALID_SYNTAX;
493
494         for(i=0; i < val->bv_len; i++) {
495                 if( !ASCII_DIGIT(val->bv_val[i]) ) return LDAP_INVALID_SYNTAX;
496         }
497
498         return LDAP_SUCCESS;
499 }
500
501 static int
502 printableStringValidate(
503         Syntax *syntax,
504         struct berval *val )
505 {
506         ber_len_t i;
507
508         if( !val->bv_len ) return LDAP_INVALID_SYNTAX;
509
510         for(i=0; i < val->bv_len; i++) {
511                 if( !isprint(val->bv_val[i]) ) return LDAP_INVALID_SYNTAX;
512         }
513
514         return LDAP_SUCCESS;
515 }
516
517 static int
518 IA5StringValidate(
519         Syntax *syntax,
520         struct berval *val )
521 {
522         ber_len_t i;
523
524         if( !val->bv_len ) return LDAP_INVALID_SYNTAX;
525
526         for(i=0; i < val->bv_len; i++) {
527                 if( !isascii(val->bv_val[i]) ) return LDAP_INVALID_SYNTAX;
528         }
529
530         return LDAP_SUCCESS;
531 }
532
533 static int
534 IA5StringConvert(
535         Syntax *syntax,
536         struct berval *in,
537         struct berval **out )
538 {
539         ldap_unicode_t *u;
540         ber_len_t i, len = in->bv_len;
541         struct berval *bv = ch_malloc( sizeof(struct berval) );
542
543         bv->bv_len = len * sizeof( ldap_unicode_t );
544         u = (ldap_unicode_t *) ch_malloc( bv->bv_len + sizeof(ldap_unicode_t) );
545         bv->bv_val = (char *) u;
546
547         for(i=0; i < len; i++ ) {
548                 /*
549                  * IA5StringValidate should have been called to ensure
550                  * input is limited to IA5.
551                  */
552                 u[i] = in->bv_val[i];
553         }
554         u[i] = 0;
555
556         *out = bv;
557         return LDAP_SUCCESS;
558 }
559
560 static int
561 IA5StringNormalize(
562         Syntax *syntax,
563         struct berval *val,
564         struct berval **normalized )
565 {
566         struct berval *newval;
567         char *p, *q;
568
569         newval = ch_malloc( sizeof( struct berval ) );
570
571         p = val->bv_val;
572
573         /* Ignore initial whitespace */
574         while ( ASCII_SPACE( *p ) ) {
575                 p++;
576         }
577
578         if( *p == '\0' ) {
579                 ch_free( newval );
580                 return LDAP_INVALID_SYNTAX;
581         }
582
583         newval->bv_val = ch_strdup( p );
584         p = q = newval->bv_val;
585
586         while ( *p ) {
587                 if ( ASCII_SPACE( *p ) ) {
588                         *q++ = *p++;
589
590                         /* Ignore the extra whitespace */
591                         while ( ASCII_SPACE( *p ) ) {
592                                 p++;
593                         }
594                 } else {
595                         *q++ = *p++;
596                 }
597         }
598
599         assert( *newval->bv_val );
600         assert( newval->bv_val < p );
601         assert( p <= q );
602
603         /* cannot start with a space */
604         assert( !ASCII_SPACE(*newval->bv_val) );
605
606         /*
607          * If the string ended in space, backup the pointer one
608          * position.  One is enough because the above loop collapsed
609          * all whitespace to a single space.
610          */
611
612         if ( ASCII_SPACE( q[-1] ) ) {
613                 --q;
614         }
615
616         /* cannot end with a space */
617         assert( !ASCII_SPACE( q[-1] ) );
618
619         /* null terminate */
620         *q = '\0';
621
622         newval->bv_len = q - newval->bv_val;
623         *normalized = newval;
624
625         return LDAP_SUCCESS;
626 }
627
628 static int
629 caseExactIA5Match(
630         int *matchp,
631         unsigned flags,
632         Syntax *syntax,
633         MatchingRule *mr,
634         struct berval *value,
635         void *assertedValue )
636 {
637         int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;
638
639         if( match == 0 ) {
640                 match = strncmp( value->bv_val,
641                         ((struct berval *) assertedValue)->bv_val,
642                         value->bv_len );
643         }
644
645         *matchp = match;
646         return LDAP_SUCCESS;
647 }
648
649 static int
650 caseExactIA5SubstringsMatch(
651         int *matchp,
652         unsigned flags,
653         Syntax *syntax,
654         MatchingRule *mr,
655         struct berval *value,
656         void *assertedValue )
657 {
658         int match = 0;
659         SubstringsAssertion *sub = assertedValue;
660         struct berval left = *value;
661         int i;
662         ber_len_t inlen=0;
663
664         /* Add up asserted input length */
665         if( sub->sa_initial ) {
666                 inlen += sub->sa_initial->bv_len;
667         }
668         if( sub->sa_any ) {
669                 for(i=0; sub->sa_any[i] != NULL; i++) {
670                         inlen += sub->sa_any[i]->bv_len;
671                 }
672         }
673         if( sub->sa_final ) {
674                 inlen += sub->sa_final->bv_len;
675         }
676
677         if( sub->sa_initial ) {
678                 if( inlen > left.bv_len ) {
679                         match = 1;
680                         goto done;
681                 }
682
683                 match = strncmp( sub->sa_initial->bv_val, left.bv_val,
684                         sub->sa_initial->bv_len );
685
686                 if( match != 0 ) {
687                         goto done;
688                 }
689
690                 left.bv_val += sub->sa_initial->bv_len;
691                 left.bv_len -= sub->sa_initial->bv_len;
692                 inlen -= sub->sa_initial->bv_len;
693         }
694
695         if( sub->sa_final ) {
696                 if( inlen > left.bv_len ) {
697                         match = 1;
698                         goto done;
699                 }
700
701                 match = strncmp( sub->sa_final->bv_val,
702                         &left.bv_val[left.bv_len - sub->sa_final->bv_len],
703                         sub->sa_final->bv_len );
704
705                 if( match != 0 ) {
706                         goto done;
707                 }
708
709                 left.bv_len -= sub->sa_final->bv_len;
710                 inlen -= sub->sa_final->bv_len;
711         }
712
713         if( sub->sa_any ) {
714                 for(i=0; sub->sa_any[i]; i++) {
715                         ber_len_t idx;
716                         char *p;
717
718 retry:
719                         if( inlen > left.bv_len ) {
720                                 /* not enough length */
721                                 match = 1;
722                                 goto done;
723                         }
724
725                         if( sub->sa_any[i]->bv_len == 0 ) {
726                                 continue;
727                         }
728
729                         p = strchr( left.bv_val, *sub->sa_any[i]->bv_val );
730
731                         if( p == NULL ) {
732                                 match = 1;
733                                 goto done;
734                         }
735
736                         idx = p - left.bv_val;
737                         assert( idx < left.bv_len );
738
739                         if( idx >= left.bv_len ) {
740                                 /* this shouldn't happen */
741                                 return LDAP_OTHER;
742                         }
743
744                         left.bv_val = p;
745                         left.bv_len -= idx;
746
747                         if( sub->sa_any[i]->bv_len > left.bv_len ) {
748                                 /* not enough left */
749                                 match = 1;
750                                 goto done;
751                         }
752
753                         match = strncmp( left.bv_val,
754                                 sub->sa_any[i]->bv_val,
755                                 sub->sa_any[i]->bv_len );
756
757                         if( match != 0 ) {
758                                 left.bv_val++;
759                                 left.bv_len--;
760                                 goto retry;
761                         }
762
763                         left.bv_val += sub->sa_any[i]->bv_len;
764                         left.bv_len -= sub->sa_any[i]->bv_len;
765                         inlen -= sub->sa_any[i]->bv_len;
766                 }
767         }
768
769 done:
770         *matchp = match;
771         return LDAP_SUCCESS;
772 }
773
774 /* Index generation function */
775 int caseExactIA5Indexer(
776         unsigned flags,
777         Syntax *syntax,
778         MatchingRule *mr,
779         struct berval *prefix,
780         struct berval **values,
781         struct berval ***keysp )
782 {
783         int i;
784         size_t slen, mlen;
785         struct berval **keys;
786         lutil_MD5_CTX   MD5context;
787         unsigned char   MD5digest[16];
788         struct berval digest;
789         digest.bv_val = MD5digest;
790         digest.bv_len = sizeof(MD5digest);
791
792         for( i=0; values[i] != NULL; i++ ) {
793                 /* just count them */
794         }
795
796         assert( i > 0 );
797
798         keys = ch_malloc( sizeof( struct berval * ) * (i+1) );
799
800         slen = strlen( syntax->ssyn_oid );
801         mlen = strlen( mr->smr_oid );
802
803         for( i=0; values[i] != NULL; i++ ) {
804                 struct berval *value = values[i];
805
806                 lutil_MD5Init( &MD5context );
807                 if( prefix != NULL && prefix->bv_len > 0 ) {
808                         lutil_MD5Update( &MD5context,
809                                 prefix->bv_val, prefix->bv_len );
810                 }
811                 lutil_MD5Update( &MD5context,
812                         syntax->ssyn_oid, slen );
813                 lutil_MD5Update( &MD5context,
814                         mr->smr_oid, mlen );
815                 lutil_MD5Update( &MD5context,
816                         value->bv_val, value->bv_len );
817                 lutil_MD5Final( MD5digest, &MD5context );
818
819                 keys[i] = ber_bvdup( &digest );
820         }
821
822         keys[i] = NULL;
823         *keysp = keys;
824         return LDAP_SUCCESS;
825 }
826
827 /* Index generation function */
828 int caseExactIA5Filter(
829         unsigned flags,
830         Syntax *syntax,
831         MatchingRule *mr,
832         struct berval *prefix,
833         void * assertValue,
834         struct berval ***keysp )
835 {
836         size_t slen, mlen;
837         struct berval **keys;
838         lutil_MD5_CTX   MD5context;
839         unsigned char   MD5digest[LUTIL_MD5_BYTES];
840         struct berval *value;
841         struct berval digest;
842         digest.bv_val = MD5digest;
843         digest.bv_len = sizeof(MD5digest);
844
845         slen = strlen( syntax->ssyn_oid );
846         mlen = strlen( mr->smr_oid );
847
848         value = (struct berval *) assertValue;
849
850         keys = ch_malloc( sizeof( struct berval * ) * 2 );
851
852         lutil_MD5Init( &MD5context );
853         if( prefix != NULL && prefix->bv_len > 0 ) {
854                 lutil_MD5Update( &MD5context,
855                         prefix->bv_val, prefix->bv_len );
856         }
857         lutil_MD5Update( &MD5context,
858                 syntax->ssyn_oid, slen );
859         lutil_MD5Update( &MD5context,
860                 mr->smr_oid, mlen );
861         lutil_MD5Update( &MD5context,
862                 value->bv_val, value->bv_len );
863         lutil_MD5Final( MD5digest, &MD5context );
864
865         keys[0] = ber_bvdup( &digest );
866         keys[1] = NULL;
867
868         *keysp = keys;
869         return LDAP_SUCCESS;
870 }
871 /* Substrings Index generation function */
872 int caseExactIA5SubstringsIndexer(
873         unsigned flags,
874         Syntax *syntax,
875         MatchingRule *mr,
876         struct berval *prefix,
877         struct berval **values,
878         struct berval ***keysp )
879 {
880         int i, nkeys, types;
881         size_t slen, mlen;
882         struct berval **keys;
883         lutil_MD5_CTX   MD5context;
884         unsigned char   MD5digest[16];
885         struct berval digest;
886         digest.bv_val = MD5digest;
887         digest.bv_len = sizeof(MD5digest);
888
889         types = 0;
890         if( flags & SLAP_MR_SUBSTR_INITIAL ) types++;
891         if( flags & SLAP_MR_SUBSTR_FINAL ) types++;
892         /* no SUBSTR_ANY indexing */
893
894         nkeys=0;
895         for( i=0; values[i] != NULL; i++ ) {
896                 /* count number of indices to generate */
897                 if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) {
898                         continue;
899                 }
900
901                 if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
902                         nkeys += SLAP_INDEX_SUBSTR_MAXLEN - ( SLAP_INDEX_SUBSTR_MINLEN - 1);
903                 } else {
904                         nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
905                 }
906         }
907         assert( i > 0 );
908
909         if( nkeys == 0 ) {
910                 /* no keys to generate */
911                 *keysp = NULL;
912                 return LDAP_SUCCESS;
913         }
914
915         nkeys *= types; /* We need to generate keys for each type */
916         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
917
918         slen = strlen( syntax->ssyn_oid );
919         mlen = strlen( mr->smr_oid );
920
921         nkeys=0;
922         for( i=0; values[i] != NULL; i++ ) {
923                 int j,max;
924                 struct berval *value;
925
926                 value = values[i];
927
928                 if( value->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) continue;
929
930                 max = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
931                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
932
933                 for( j=SLAP_INDEX_SUBSTR_MINLEN; j<=max; j++ ) {
934                         char pre;
935
936                         if( flags & SLAP_MR_SUBSTR_INITIAL ) {
937                                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
938                                 lutil_MD5Init( &MD5context );
939                                 if( prefix != NULL && prefix->bv_len > 0 ) {
940                                         lutil_MD5Update( &MD5context,
941                                                 prefix->bv_val, prefix->bv_len );
942                                 }
943                                 lutil_MD5Update( &MD5context,
944                                         &pre, sizeof( pre ) );
945                                 lutil_MD5Update( &MD5context,
946                                         syntax->ssyn_oid, slen );
947                                 lutil_MD5Update( &MD5context,
948                                         mr->smr_oid, mlen );
949                                 lutil_MD5Update( &MD5context,
950                                         value->bv_val, j );
951                                 lutil_MD5Final( MD5digest, &MD5context );
952
953                                 keys[nkeys++] = ber_bvdup( &digest );
954                         }
955
956                         if( flags & SLAP_MR_SUBSTR_FINAL ) {
957                                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
958                                 lutil_MD5Init( &MD5context );
959                                 if( prefix != NULL && prefix->bv_len > 0 ) {
960                                         lutil_MD5Update( &MD5context,
961                                                 prefix->bv_val, prefix->bv_len );
962                                 }
963                                 lutil_MD5Update( &MD5context,
964                                         &pre, sizeof( pre ) );
965                                 lutil_MD5Update( &MD5context,
966                                         syntax->ssyn_oid, slen );
967                                 lutil_MD5Update( &MD5context,
968                                         mr->smr_oid, mlen );
969                                 lutil_MD5Update( &MD5context,
970                                         &value->bv_val[value->bv_len-j], j );
971                                 lutil_MD5Final( MD5digest, &MD5context );
972
973                                 keys[nkeys++] = ber_bvdup( &digest );
974                         }
975
976                 }
977         }
978
979         keys[nkeys] = NULL;
980         *keysp = keys;
981         return LDAP_SUCCESS;
982 }
983
984 int caseExactIA5SubstringsFilter(
985         unsigned flags,
986         Syntax *syntax,
987         MatchingRule *mr,
988         struct berval *prefix,
989         void * assertValue,
990         struct berval ***keysp )
991 {
992         SubstringsAssertion *sa = assertValue;
993         char pre;
994         int nkeys = 0;
995         size_t slen, mlen, klen;
996         struct berval **keys;
997         lutil_MD5_CTX   MD5context;
998         unsigned char   MD5digest[LUTIL_MD5_BYTES];
999         struct berval *value;
1000         struct berval digest;
1001
1002         if( sa->sa_initial != NULL &&
1003                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1004         {
1005                 nkeys++;
1006         }
1007         if( sa->sa_final != NULL &&
1008                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1009         {
1010                 nkeys++;
1011         }
1012
1013         if( nkeys == 0 ) {
1014                 *keysp = NULL;
1015                 return LDAP_SUCCESS;
1016         }
1017
1018         digest.bv_val = MD5digest;
1019         digest.bv_len = sizeof(MD5digest);
1020
1021         slen = strlen( syntax->ssyn_oid );
1022         mlen = strlen( mr->smr_oid );
1023
1024         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
1025         nkeys = 0;
1026
1027         if( sa->sa_initial != NULL &&
1028                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1029         {
1030                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
1031                 value = sa->sa_initial;
1032
1033                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
1034                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
1035
1036                 lutil_MD5Init( &MD5context );
1037                 if( prefix != NULL && prefix->bv_len > 0 ) {
1038                         lutil_MD5Update( &MD5context,
1039                                 prefix->bv_val, prefix->bv_len );
1040                 }
1041                 lutil_MD5Update( &MD5context,
1042                         &pre, sizeof( pre ) );
1043                 lutil_MD5Update( &MD5context,
1044                         syntax->ssyn_oid, slen );
1045                 lutil_MD5Update( &MD5context,
1046                         mr->smr_oid, mlen );
1047                 lutil_MD5Update( &MD5context,
1048                         value->bv_val, klen );
1049                 lutil_MD5Final( MD5digest, &MD5context );
1050
1051                 keys[nkeys++] = ber_bvdup( &digest );
1052         }
1053
1054         if( sa->sa_final != NULL &&
1055                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1056         {
1057                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
1058                 value = sa->sa_final;
1059
1060                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
1061                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
1062
1063                 lutil_MD5Init( &MD5context );
1064                 if( prefix != NULL && prefix->bv_len > 0 ) {
1065                         lutil_MD5Update( &MD5context,
1066                                 prefix->bv_val, prefix->bv_len );
1067                 }
1068                 lutil_MD5Update( &MD5context,
1069                         &pre, sizeof( pre ) );
1070                 lutil_MD5Update( &MD5context,
1071                         syntax->ssyn_oid, slen );
1072                 lutil_MD5Update( &MD5context,
1073                         mr->smr_oid, mlen );
1074                 lutil_MD5Update( &MD5context,
1075                         &value->bv_val[value->bv_len-klen], klen );
1076                 lutil_MD5Final( MD5digest, &MD5context );
1077
1078                 keys[nkeys++] = ber_bvdup( &digest );
1079         }
1080
1081         keys[nkeys] = NULL;
1082
1083         *keysp = keys;
1084         return LDAP_SUCCESS;
1085 }
1086         
1087 static int
1088 caseIgnoreIA5Match(
1089         int *matchp,
1090         unsigned flags,
1091         Syntax *syntax,
1092         MatchingRule *mr,
1093         struct berval *value,
1094         void *assertedValue )
1095 {
1096         int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;
1097
1098         if( match == 0 ) {
1099                 match = strncasecmp( value->bv_val,
1100                         ((struct berval *) assertedValue)->bv_val,
1101                         value->bv_len );
1102         }
1103
1104         *matchp = match;
1105         return LDAP_SUCCESS;
1106 }
1107
1108 static char *strcasechr( const char *str, int c )
1109 {
1110         char *lower = strchr( str, TOLOWER(c) );
1111         char *upper = strchr( str, TOUPPER(c) );
1112
1113         if( lower && upper ) {
1114                 return lower < upper ? lower : upper;
1115         } else if ( lower ) {
1116                 return lower;
1117         } else {
1118                 return upper;
1119         }
1120 }
1121
1122 static int
1123 caseIgnoreIA5SubstringsMatch(
1124         int *matchp,
1125         unsigned flags,
1126         Syntax *syntax,
1127         MatchingRule *mr,
1128         struct berval *value,
1129         void *assertedValue )
1130 {
1131         int match = 0;
1132         SubstringsAssertion *sub = assertedValue;
1133         struct berval left = *value;
1134         int i;
1135         ber_len_t inlen=0;
1136
1137         /* Add up asserted input length */
1138         if( sub->sa_initial ) {
1139                 inlen += sub->sa_initial->bv_len;
1140         }
1141         if( sub->sa_any ) {
1142                 for(i=0; sub->sa_any[i] != NULL; i++) {
1143                         inlen += sub->sa_any[i]->bv_len;
1144                 }
1145         }
1146         if( sub->sa_final ) {
1147                 inlen += sub->sa_final->bv_len;
1148         }
1149
1150         if( sub->sa_initial ) {
1151                 if( inlen > left.bv_len ) {
1152                         match = 1;
1153                         goto done;
1154                 }
1155
1156                 match = strncasecmp( sub->sa_initial->bv_val, left.bv_val,
1157                         sub->sa_initial->bv_len );
1158
1159                 if( match != 0 ) {
1160                         goto done;
1161                 }
1162
1163                 left.bv_val += sub->sa_initial->bv_len;
1164                 left.bv_len -= sub->sa_initial->bv_len;
1165                 inlen -= sub->sa_initial->bv_len;
1166         }
1167
1168         if( sub->sa_final ) {
1169                 if( inlen > left.bv_len ) {
1170                         match = 1;
1171                         goto done;
1172                 }
1173
1174                 match = strncasecmp( sub->sa_final->bv_val,
1175                         &left.bv_val[left.bv_len - sub->sa_final->bv_len],
1176                         sub->sa_final->bv_len );
1177
1178                 if( match != 0 ) {
1179                         goto done;
1180                 }
1181
1182                 left.bv_len -= sub->sa_final->bv_len;
1183                 inlen -= sub->sa_final->bv_len;
1184         }
1185
1186         if( sub->sa_any ) {
1187                 for(i=0; sub->sa_any[i]; i++) {
1188                         ber_len_t idx;
1189                         char *p;
1190
1191 retry:
1192                         if( inlen > left.bv_len ) {
1193                                 /* not enough length */
1194                                 match = 1;
1195                                 goto done;
1196                         }
1197
1198                         if( sub->sa_any[i]->bv_len == 0 ) {
1199                                 continue;
1200                         }
1201
1202                         p = strcasechr( left.bv_val, *sub->sa_any[i]->bv_val );
1203
1204                         if( p == NULL ) {
1205                                 match = 1;
1206                                 goto done;
1207                         }
1208
1209                         idx = p - left.bv_val;
1210                         assert( idx < left.bv_len );
1211
1212                         if( idx >= left.bv_len ) {
1213                                 /* this shouldn't happen */
1214                                 return LDAP_OTHER;
1215                         }
1216
1217                         left.bv_val = p;
1218                         left.bv_len -= idx;
1219
1220                         if( sub->sa_any[i]->bv_len > left.bv_len ) {
1221                                 /* not enough left */
1222                                 match = 1;
1223                                 goto done;
1224                         }
1225
1226                         match = strncasecmp( left.bv_val,
1227                                 sub->sa_any[i]->bv_val,
1228                                 sub->sa_any[i]->bv_len );
1229
1230                         if( match != 0 ) {
1231                                 left.bv_val++;
1232                                 left.bv_len--;
1233
1234                                 goto retry;
1235                         }
1236
1237                         left.bv_val += sub->sa_any[i]->bv_len;
1238                         left.bv_len -= sub->sa_any[i]->bv_len;
1239                         inlen -= sub->sa_any[i]->bv_len;
1240                 }
1241         }
1242
1243 done:
1244         *matchp = match;
1245         return LDAP_SUCCESS;
1246 }
1247
1248 /* Index generation function */
1249 int caseIgnoreIA5Indexer(
1250         unsigned flags,
1251         Syntax *syntax,
1252         MatchingRule *mr,
1253         struct berval *prefix,
1254         struct berval **values,
1255         struct berval ***keysp )
1256 {
1257         int i;
1258         size_t slen, mlen;
1259         struct berval **keys;
1260         lutil_MD5_CTX   MD5context;
1261         unsigned char   MD5digest[16];
1262         struct berval digest;
1263         digest.bv_val = MD5digest;
1264         digest.bv_len = sizeof(MD5digest);
1265
1266         for( i=0; values[i] != NULL; i++ ) {
1267                 /* just count them */
1268         }
1269
1270         assert( i > 0 );
1271
1272         keys = ch_malloc( sizeof( struct berval * ) * (i+1) );
1273
1274         slen = strlen( syntax->ssyn_oid );
1275         mlen = strlen( mr->smr_oid );
1276
1277         for( i=0; values[i] != NULL; i++ ) {
1278                 struct berval *value = ber_bvdup( values[i] );
1279                 ldap_pvt_str2upper( value->bv_val );
1280
1281                 lutil_MD5Init( &MD5context );
1282                 if( prefix != NULL && prefix->bv_len > 0 ) {
1283                         lutil_MD5Update( &MD5context,
1284                                 prefix->bv_val, prefix->bv_len );
1285                 }
1286                 lutil_MD5Update( &MD5context,
1287                         syntax->ssyn_oid, slen );
1288                 lutil_MD5Update( &MD5context,
1289                         mr->smr_oid, mlen );
1290                 lutil_MD5Update( &MD5context,
1291                         value->bv_val, value->bv_len );
1292                 lutil_MD5Final( MD5digest, &MD5context );
1293
1294                 ber_bvfree( value );
1295
1296                 keys[i] = ber_bvdup( &digest );
1297         }
1298
1299         keys[i] = NULL;
1300         *keysp = keys;
1301         return LDAP_SUCCESS;
1302 }
1303
1304 /* Index generation function */
1305 int caseIgnoreIA5Filter(
1306         unsigned flags,
1307         Syntax *syntax,
1308         MatchingRule *mr,
1309         struct berval *prefix,
1310         void * assertValue,
1311         struct berval ***keysp )
1312 {
1313         size_t slen, mlen;
1314         struct berval **keys;
1315         lutil_MD5_CTX   MD5context;
1316         unsigned char   MD5digest[LUTIL_MD5_BYTES];
1317         struct berval *value;
1318         struct berval digest;
1319         digest.bv_val = MD5digest;
1320         digest.bv_len = sizeof(MD5digest);
1321
1322         slen = strlen( syntax->ssyn_oid );
1323         mlen = strlen( mr->smr_oid );
1324
1325         value = ber_bvdup( (struct berval *) assertValue );
1326         ldap_pvt_str2upper( value->bv_val );
1327
1328         keys = ch_malloc( sizeof( struct berval * ) * 2 );
1329
1330         lutil_MD5Init( &MD5context );
1331         if( prefix != NULL && prefix->bv_len > 0 ) {
1332                 lutil_MD5Update( &MD5context,
1333                         prefix->bv_val, prefix->bv_len );
1334         }
1335         lutil_MD5Update( &MD5context,
1336                 syntax->ssyn_oid, slen );
1337         lutil_MD5Update( &MD5context,
1338                 mr->smr_oid, mlen );
1339         lutil_MD5Update( &MD5context,
1340                 value->bv_val, value->bv_len );
1341         lutil_MD5Final( MD5digest, &MD5context );
1342
1343         keys[0] = ber_bvdup( &digest );
1344         keys[1] = NULL;
1345
1346         ber_bvfree( value );
1347
1348         *keysp = keys;
1349         return LDAP_SUCCESS;
1350 }
1351
1352 /* Substrings Index generation function */
1353 int caseIgnoreIA5SubstringsIndexer(
1354         unsigned flags,
1355         Syntax *syntax,
1356         MatchingRule *mr,
1357         struct berval *prefix,
1358         struct berval **values,
1359         struct berval ***keysp )
1360 {
1361         int i, nkeys, types;
1362         size_t slen, mlen;
1363         struct berval **keys;
1364         lutil_MD5_CTX   MD5context;
1365         unsigned char   MD5digest[16];
1366         struct berval digest;
1367         digest.bv_val = MD5digest;
1368         digest.bv_len = sizeof(MD5digest);
1369
1370         types = 0;
1371         if( flags & SLAP_MR_SUBSTR_INITIAL ) types++;
1372         if( flags & SLAP_MR_SUBSTR_FINAL ) types++;
1373         /* no SUBSTR_ANY indexing */
1374
1375         nkeys=0;
1376         for( i=0; values[i] != NULL; i++ ) {
1377                 /* count number of indices to generate */
1378                 if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) {
1379                         continue;
1380                 }
1381
1382                 if( values[i]->bv_len >= SLAP_INDEX_SUBSTR_MAXLEN ) {
1383                         nkeys += SLAP_INDEX_SUBSTR_MAXLEN - ( SLAP_INDEX_SUBSTR_MINLEN - 1);
1384                 } else {
1385                         nkeys += values[i]->bv_len - ( SLAP_INDEX_SUBSTR_MINLEN - 1 );
1386                 }
1387         }
1388         assert( i > 0 );
1389
1390         if( nkeys == 0 ) {
1391                 /* no keys to generate */
1392                 *keysp = NULL;
1393                 return LDAP_SUCCESS;
1394         }
1395
1396         nkeys *= types; /* We need to generate keys for each type */
1397         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
1398
1399         slen = strlen( syntax->ssyn_oid );
1400         mlen = strlen( mr->smr_oid );
1401
1402         nkeys=0;
1403         for( i=0; values[i] != NULL; i++ ) {
1404                 int j,max;
1405                 struct berval *value;
1406
1407                 if( values[i]->bv_len < SLAP_INDEX_SUBSTR_MINLEN ) continue;
1408
1409                 max = SLAP_INDEX_SUBSTR_MAXLEN < values[i]->bv_len
1410                         ? SLAP_INDEX_SUBSTR_MAXLEN : values[i]->bv_len;
1411
1412                 value = ber_bvdup( values[i] );
1413                 ldap_pvt_str2upper( value->bv_val );
1414
1415                 for( j=SLAP_INDEX_SUBSTR_MINLEN; j<=max; j++ ) {
1416                         char pre;
1417
1418                         if( flags & SLAP_MR_SUBSTR_INITIAL ) {
1419                                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
1420                                 lutil_MD5Init( &MD5context );
1421                                 if( prefix != NULL && prefix->bv_len > 0 ) {
1422                                         lutil_MD5Update( &MD5context,
1423                                                 prefix->bv_val, prefix->bv_len );
1424                                 }
1425                                 lutil_MD5Update( &MD5context,
1426                                         &pre, sizeof( pre ) );
1427                                 lutil_MD5Update( &MD5context,
1428                                         syntax->ssyn_oid, slen );
1429                                 lutil_MD5Update( &MD5context,
1430                                         mr->smr_oid, mlen );
1431                                 lutil_MD5Update( &MD5context,
1432                                         value->bv_val, j );
1433                                 lutil_MD5Final( MD5digest, &MD5context );
1434
1435                                 keys[nkeys++] = ber_bvdup( &digest );
1436                         }
1437
1438                         if( flags & SLAP_MR_SUBSTR_FINAL ) {
1439                                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
1440                                 lutil_MD5Init( &MD5context );
1441                                 if( prefix != NULL && prefix->bv_len > 0 ) {
1442                                         lutil_MD5Update( &MD5context,
1443                                                 prefix->bv_val, prefix->bv_len );
1444                                 }
1445                                 lutil_MD5Update( &MD5context,
1446                                         &pre, sizeof( pre ) );
1447                                 lutil_MD5Update( &MD5context,
1448                                         syntax->ssyn_oid, slen );
1449                                 lutil_MD5Update( &MD5context,
1450                                         mr->smr_oid, mlen );
1451                                 lutil_MD5Update( &MD5context,
1452                                         &value->bv_val[value->bv_len-j], j );
1453                                 lutil_MD5Final( MD5digest, &MD5context );
1454
1455                                 keys[nkeys++] = ber_bvdup( &digest );
1456                         }
1457
1458                 }
1459
1460                 ber_bvfree( value );
1461         }
1462
1463         keys[nkeys] = NULL;
1464         *keysp = keys;
1465         return LDAP_SUCCESS;
1466 }
1467
1468 int caseIgnoreIA5SubstringsFilter(
1469         unsigned flags,
1470         Syntax *syntax,
1471         MatchingRule *mr,
1472         struct berval *prefix,
1473         void * assertValue,
1474         struct berval ***keysp )
1475 {
1476         SubstringsAssertion *sa = assertValue;
1477         char pre;
1478         int nkeys = 0;
1479         size_t slen, mlen, klen;
1480         struct berval **keys;
1481         lutil_MD5_CTX   MD5context;
1482         unsigned char   MD5digest[LUTIL_MD5_BYTES];
1483         struct berval *value;
1484         struct berval digest;
1485
1486         if( sa->sa_initial != NULL &&
1487                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1488         {
1489                 nkeys++;
1490         }
1491         if( sa->sa_final != NULL &&
1492                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1493         {
1494                 nkeys++;
1495         }
1496
1497         if( nkeys == 0 ) {
1498                 *keysp = NULL;
1499                 return LDAP_SUCCESS;
1500         }
1501
1502         digest.bv_val = MD5digest;
1503         digest.bv_len = sizeof(MD5digest);
1504
1505         slen = strlen( syntax->ssyn_oid );
1506         mlen = strlen( mr->smr_oid );
1507
1508         keys = ch_malloc( sizeof( struct berval * ) * (nkeys+1) );
1509         nkeys = 0;
1510
1511         if( sa->sa_initial != NULL &&
1512                 sa->sa_initial->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1513         {
1514                 pre = SLAP_INDEX_SUBSTR_INITIAL_PREFIX;
1515                 value = ber_bvdup( sa->sa_initial );
1516                 ldap_pvt_str2upper( value->bv_val );
1517
1518                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
1519                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
1520
1521                 lutil_MD5Init( &MD5context );
1522                 if( prefix != NULL && prefix->bv_len > 0 ) {
1523                         lutil_MD5Update( &MD5context,
1524                                 prefix->bv_val, prefix->bv_len );
1525                 }
1526                 lutil_MD5Update( &MD5context,
1527                         &pre, sizeof( pre ) );
1528                 lutil_MD5Update( &MD5context,
1529                         syntax->ssyn_oid, slen );
1530                 lutil_MD5Update( &MD5context,
1531                         mr->smr_oid, mlen );
1532                 lutil_MD5Update( &MD5context,
1533                         value->bv_val, klen );
1534                 lutil_MD5Final( MD5digest, &MD5context );
1535
1536                 ber_bvfree( value );
1537                 keys[nkeys++] = ber_bvdup( &digest );
1538         }
1539
1540         if( sa->sa_final != NULL &&
1541                 sa->sa_final->bv_len >= SLAP_INDEX_SUBSTR_MINLEN )
1542         {
1543                 pre = SLAP_INDEX_SUBSTR_FINAL_PREFIX;
1544                 value = ber_bvdup( sa->sa_final );
1545                 ldap_pvt_str2upper( value->bv_val );
1546
1547                 klen = SLAP_INDEX_SUBSTR_MAXLEN < value->bv_len
1548                         ? SLAP_INDEX_SUBSTR_MAXLEN : value->bv_len;
1549
1550                 lutil_MD5Init( &MD5context );
1551                 if( prefix != NULL && prefix->bv_len > 0 ) {
1552                         lutil_MD5Update( &MD5context,
1553                                 prefix->bv_val, prefix->bv_len );
1554                 }
1555                 lutil_MD5Update( &MD5context,
1556                         &pre, sizeof( pre ) );
1557                 lutil_MD5Update( &MD5context,
1558                         syntax->ssyn_oid, slen );
1559                 lutil_MD5Update( &MD5context,
1560                         mr->smr_oid, mlen );
1561                 lutil_MD5Update( &MD5context,
1562                         &value->bv_val[value->bv_len-klen], klen );
1563                 lutil_MD5Final( MD5digest, &MD5context );
1564
1565                 ber_bvfree( value );
1566                 keys[nkeys++] = ber_bvdup( &digest );
1567         }
1568
1569         keys[nkeys] = NULL;
1570
1571         *keysp = keys;
1572         return LDAP_SUCCESS;
1573 }
1574         
1575 static int
1576 numericStringNormalize(
1577         Syntax *syntax,
1578         struct berval *val,
1579         struct berval **normalized )
1580 {
1581         /* similiar to IA5StringNormalize except removes all spaces */
1582         struct berval *newval;
1583         char *p, *q;
1584
1585         newval = ch_malloc( sizeof( struct berval ) );
1586
1587         p = val->bv_val;
1588
1589         /* Ignore initial whitespace */
1590         while ( ASCII_SPACE( *p ) ) {
1591                 p++;
1592         }
1593
1594         if( *p == '\0' ) {
1595                 ch_free( newval );
1596                 return LDAP_INVALID_SYNTAX;
1597         }
1598
1599         newval->bv_val = ch_strdup( p );
1600         p = q = newval->bv_val;
1601
1602         while ( *p ) {
1603                 if ( ASCII_SPACE( *p ) ) {
1604                         /* Ignore whitespace */
1605                         p++;
1606                 } else {
1607                         *q++ = *p++;
1608                 }
1609         }
1610
1611         assert( *newval->bv_val );
1612         assert( newval->bv_val < p );
1613         assert( q <= p );
1614
1615         /* cannot start with a space */
1616         assert( !ASCII_SPACE(*newval->bv_val) );
1617
1618         /* cannot end with a space */
1619         assert( !ASCII_SPACE( q[-1] ) );
1620
1621         /* null terminate */
1622         *q = '\0';
1623
1624         newval->bv_len = q - newval->bv_val;
1625         *normalized = newval;
1626
1627         return LDAP_SUCCESS;
1628 }
1629
1630 static int
1631 objectIdentifierFirstComponentMatch(
1632         int *matchp,
1633         unsigned flags,
1634         Syntax *syntax,
1635         MatchingRule *mr,
1636         struct berval *value,
1637         void *assertedValue )
1638 {
1639         int rc = LDAP_SUCCESS;
1640         int match;
1641         struct berval *asserted = (struct berval *) assertedValue;
1642         ber_len_t i;
1643         struct berval oid;
1644
1645         if( value->bv_len == 0 || value->bv_val[0] != '(' /*')'*/ ) {
1646                 return LDAP_INVALID_SYNTAX;
1647         }
1648
1649         /* trim leading white space */
1650         for( i=1; ASCII_SPACE(value->bv_val[i]) && i < value->bv_len; i++ ) {
1651                 /* empty */
1652         }
1653
1654         /* grab next word */
1655         oid.bv_val = &value->bv_val[i];
1656         oid.bv_len = value->bv_len - i;
1657         for( i=1; ASCII_SPACE(value->bv_val[i]) && i < oid.bv_len; i++ ) {
1658                 /* empty */
1659         }
1660         oid.bv_len = i;
1661
1662         /* insert attributeTypes, objectclass check here */
1663         if( OID_LEADCHAR(asserted->bv_val[0]) ) {
1664                 rc = objectIdentifierMatch( &match, flags, syntax, mr, &oid, asserted );
1665
1666         } else {
1667                 char *stored = ch_malloc( oid.bv_len + 1 );
1668                 AC_MEMCPY( stored, oid.bv_val, oid.bv_len );
1669                 stored[oid.bv_len] = '\0';
1670
1671                 if ( !strcmp( syntax->ssyn_oid, SLAP_SYNTAX_MATCHINGRULES_OID ) ) {
1672                         MatchingRule *asserted_mr = mr_find( asserted->bv_val );
1673                         MatchingRule *stored_mr = mr_find( stored );
1674
1675                         if( asserted_mr == NULL ) {
1676                                 rc = SLAPD_COMPARE_UNDEFINED;
1677                         } else {
1678                                 match = asserted_mr != stored_mr;
1679                         }
1680
1681                 } else if ( !strcmp( syntax->ssyn_oid,
1682                         SLAP_SYNTAX_ATTRIBUTETYPES_OID ) )
1683                 {
1684                         AttributeType *asserted_at = at_find( asserted->bv_val );
1685                         AttributeType *stored_at = at_find( stored );
1686
1687                         if( asserted_at == NULL ) {
1688                                 rc = SLAPD_COMPARE_UNDEFINED;
1689                         } else {
1690                                 match = asserted_at != stored_at;
1691                         }
1692
1693                 } else if ( !strcmp( syntax->ssyn_oid,
1694                         SLAP_SYNTAX_OBJECTCLASSES_OID ) )
1695                 {
1696                         ObjectClass *asserted_oc = oc_find( asserted->bv_val );
1697                         ObjectClass *stored_oc = oc_find( stored );
1698
1699                         if( asserted_oc == NULL ) {
1700                                 rc = SLAPD_COMPARE_UNDEFINED;
1701                         } else {
1702                                 match = asserted_oc != stored_oc;
1703                         }
1704                 }
1705
1706                 ch_free( stored );
1707         }
1708
1709         Debug( LDAP_DEBUG_ARGS, "objectIdentifierFirstComponentMatch "
1710                 "%d\n\t\"%s\"\n\t\"%s\"\n",
1711             match, value->bv_val, asserted->bv_val );
1712
1713         if( rc == LDAP_SUCCESS ) *matchp = match;
1714         return rc;
1715 }
1716
1717 static int
1718 check_time_syntax (struct berval *val,
1719         int start,
1720         int *parts)
1721 {
1722         static int ceiling[9] = { 99, 99, 11, 30, 23, 59, 59, 12, 59 };
1723         static int mdays[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1724         char *p, *e;
1725         int part, c, neg = 0;
1726
1727         if( val->bv_len == 0 )
1728                 return LDAP_INVALID_SYNTAX;
1729
1730         p = (char *)val->bv_val;
1731         e = p + val->bv_len;
1732
1733         /* Ignore initial whitespace */
1734         while ( ( p < e ) && ASCII_SPACE( *p ) ) {
1735                 p++;
1736         }
1737
1738         if (e - p < 13 - (2 * start))
1739                 return LDAP_INVALID_SYNTAX;
1740
1741         for (part = 0; part < 9; part++)
1742                 parts[part] = 0;
1743
1744         for (part = start; part < 7; part++) {
1745                 c = *p;
1746                 if ((part == 6)
1747                         && (c == 'Z'
1748                                 || c == '+'
1749                                 || c == '-'))
1750                 {
1751                         part++;
1752                         break;
1753                 }
1754                 p++;
1755                 c -= '0';
1756                 if (p == e)
1757                         return LDAP_INVALID_SYNTAX;
1758                 if (c < 0 || c > 9)
1759                         return LDAP_INVALID_SYNTAX;
1760                 parts[part] = c;
1761
1762                 c = *p++ - '0';
1763                 if (p == e)
1764                         return LDAP_INVALID_SYNTAX;
1765                 if (c < 0 || c > 9)
1766                         return LDAP_INVALID_SYNTAX;
1767                 parts[part] *= 10;
1768                 parts[part] += c;
1769
1770                 if (part == 2 || part == 3)
1771                         parts[part]--;
1772                 if (parts[part] < 0)
1773                         return LDAP_INVALID_SYNTAX;
1774                 if (parts[part] > ceiling[part])
1775                         return LDAP_INVALID_SYNTAX;
1776         }
1777         if (parts[2] == 1) {
1778                 if (parts[3] > mdays[parts[2]])
1779                         return LDAP_INVALID_SYNTAX;
1780                 if (parts[1] & 0x03) {
1781                         /* FIXME:  This is an incomplete leap-year
1782                          * check that fails in 2100, 2200, 2300,
1783                          * 2500, 2600, 2700, ...
1784                          */
1785                         if (parts[3] > mdays[parts[2]] - 1)
1786                                 return LDAP_INVALID_SYNTAX;
1787                 }
1788         }
1789         c = *p++;
1790         if (c == 'Z') {
1791                 /* all done */
1792         } else if (c != '+' && c != '-') {
1793                 return LDAP_INVALID_SYNTAX;
1794         } else {
1795                 if (c == '-')
1796                         neg = 1;
1797                 if (p > e - 4)
1798                         return LDAP_INVALID_SYNTAX;
1799                 for (part = 7; part < 9; part++) {
1800                         c = *p++ - '0';
1801                         if (c < 0 || c > 9)
1802                                 return LDAP_INVALID_SYNTAX;
1803                         parts[part] = c;
1804
1805                         c = *p++ - '0';
1806                         if (c < 0 || c > 9)
1807                                 return LDAP_INVALID_SYNTAX;
1808                         parts[part] *= 10;
1809                         parts[part] += c;
1810                         if (parts[part] < 0 || parts[part] > ceiling[part])
1811                                 return LDAP_INVALID_SYNTAX;
1812                 }
1813         }
1814
1815         /* Ignore trailing whitespace */
1816         while ( ( p < e ) && ASCII_SPACE( *p ) ) {
1817                 p++;
1818         }
1819         if (p != e)
1820                 return LDAP_INVALID_SYNTAX;
1821
1822         if (neg == 0) {
1823                 parts[4] += parts[7];
1824                 parts[5] += parts[8];
1825                 for (part = 7; --part > 0; ) {
1826                         if (part != 3)
1827                                 c = ceiling[part];
1828                         else {
1829                                 /* FIXME:  This is an incomplete leap-year
1830                                  * check that fails in 2100, 2200, 2300,
1831                                  * 2500, 2600, 2700, ...
1832                                  */
1833                                 c = mdays[parts[2]];
1834                                 if (parts[2] == 1)
1835                                         c--;
1836                         }
1837                         if (parts[part] > c) {
1838                                 parts[part] -= c + 1;
1839                                 parts[part - 1]++;
1840                         }
1841                 }
1842         } else {
1843                 parts[4] -= parts[7];
1844                 parts[5] -= parts[8];
1845                 for (part = 7; --part > 0; ) {
1846                         if (part != 3)
1847                                 c = ceiling[part];
1848                         else {
1849                                 /* FIXME:  This is an incomplete leap-year
1850                                  * check that fails in 2100, 2200, 2300,
1851                                  * 2500, 2600, 2700, ...
1852                                  */
1853                                 c = mdays[(parts[2] - 1) % 12];
1854                                 if (parts[2] == 2)
1855                                         c--;
1856                         }
1857                         if (parts[part] < 0) {
1858                                 parts[part] += c + 1;
1859                                 parts[part - 1]--;
1860                         }
1861                 }
1862         }
1863
1864         return LDAP_SUCCESS;
1865 }
1866
1867 static int
1868 utcTimeNormalize(
1869         Syntax *syntax,
1870         struct berval *val,
1871         struct berval **normalized )
1872 {
1873         struct berval *out;
1874         int parts[9], rc;
1875
1876         rc = check_time_syntax(val, 1, parts);
1877         if (rc != LDAP_SUCCESS) {
1878                 return rc;
1879         }
1880
1881         *normalized = NULL;
1882         out = ch_malloc( sizeof(struct berval) );
1883         if( out == NULL )
1884                 return LBER_ERROR_MEMORY;
1885
1886         out->bv_val = ch_malloc( 14 );
1887         if ( out->bv_val == NULL ) {
1888                 ch_free( out );
1889                 return LBER_ERROR_MEMORY;
1890         }
1891
1892         sprintf( out->bv_val, "%02ld%02ld%02ld%02ld%02ld%02ldZ",
1893                                 parts[1], parts[2] + 1, parts[3] + 1,
1894                                 parts[4], parts[5], parts[6] );
1895         out->bv_len = 13;
1896         *normalized = out;
1897
1898         return LDAP_SUCCESS;
1899 }
1900
1901 static int
1902 utcTimeValidate(
1903         Syntax *syntax,
1904         struct berval *in )
1905 {
1906         int parts[9];
1907
1908         return check_time_syntax(in, 1, parts);
1909 }
1910
1911 static int
1912 generalizedTimeValidate(
1913         Syntax *syntax,
1914         struct berval *in )
1915 {
1916         int parts[9];
1917
1918         return check_time_syntax(in, 0, parts);
1919 }
1920
1921 static int
1922 generalizedTimeNormalize(
1923         Syntax *syntax,
1924         struct berval *val,
1925         struct berval **normalized )
1926 {
1927         struct berval *out;
1928         int parts[9], rc;
1929
1930         rc = check_time_syntax(val, 0, parts);
1931         if (rc != LDAP_SUCCESS) {
1932                 return rc;
1933         }
1934
1935         *normalized = NULL;
1936         out = ch_malloc( sizeof(struct berval) );
1937         if( out == NULL )
1938                 return LBER_ERROR_MEMORY;
1939
1940         out->bv_val = ch_malloc( 16 );
1941         if ( out->bv_val == NULL ) {
1942                 ch_free( out );
1943                 return LBER_ERROR_MEMORY;
1944         }
1945
1946         sprintf( out->bv_val, "%02ld%02ld%02ld%02ld%02ld%02ld%02ldZ",
1947                                 parts[0], parts[1], parts[2] + 1, parts[3] + 1,
1948                                 parts[4], parts[5], parts[6] );
1949         out->bv_len = 15;
1950         *normalized = out;
1951
1952         return LDAP_SUCCESS;
1953 }
1954
1955 struct syntax_defs_rec {
1956         char *sd_desc;
1957         int sd_flags;
1958         slap_syntax_validate_func *sd_validate;
1959         slap_syntax_transform_func *sd_normalize;
1960         slap_syntax_transform_func *sd_pretty;
1961 #ifdef SLAPD_BINARY_CONVERSION
1962         slap_syntax_transform_func *sd_ber2str;
1963         slap_syntax_transform_func *sd_str2ber;
1964 #endif
1965 };
1966
1967 #define X_HIDE "X-HIDE 'TRUE' "
1968 #define X_BINARY "X-BINARY-TRANSFER-REQUIRED 'TRUE' "
1969 #define X_NOT_H_R "X-NOT-HUMAN-READABLE 'TRUE' "
1970
1971 struct syntax_defs_rec syntax_defs[] = {
1972         {"( 1.3.6.1.4.1.1466.115.121.1.1 DESC 'ACI Item' " X_BINARY X_NOT_H_R ")",
1973                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, NULL, NULL, NULL},
1974         {"( 1.3.6.1.4.1.1466.115.121.1.2 DESC 'Access Point' " X_NOT_H_R ")",
1975                 0, NULL, NULL, NULL},
1976         {"( 1.3.6.1.4.1.1466.115.121.1.3 DESC 'Attribute Type Description' )",
1977                 0, NULL, NULL, NULL},
1978         {"( 1.3.6.1.4.1.1466.115.121.1.4 DESC 'Audio' " X_NOT_H_R ")",
1979                 SLAP_SYNTAX_BLOB, blobValidate, NULL, NULL},
1980         {"( 1.3.6.1.4.1.1466.115.121.1.5 DESC 'Binary' " X_BINARY X_NOT_H_R ")",
1981                 SLAP_SYNTAX_BER, berValidate, NULL, NULL},
1982         {"( 1.3.6.1.4.1.1466.115.121.1.6 DESC 'Bit String' )",
1983                 0, bitStringValidate, bitStringNormalize, NULL },
1984         {"( 1.3.6.1.4.1.1466.115.121.1.7 DESC 'Boolean' )",
1985                 0, booleanValidate, NULL, NULL},
1986         {"( 1.3.6.1.4.1.1466.115.121.1.8 DESC 'Certificate' "
1987                 X_BINARY X_NOT_H_R ")",
1988                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
1989         {"( 1.3.6.1.4.1.1466.115.121.1.9 DESC 'Certificate List' "
1990                 X_BINARY X_NOT_H_R ")",
1991                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
1992         {"( 1.3.6.1.4.1.1466.115.121.1.10 DESC 'Certificate Pair' "
1993                 X_BINARY X_NOT_H_R ")",
1994                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
1995         {"( 1.3.6.1.4.1.1466.115.121.1.11 DESC 'Country String' )",
1996                 0, NULL, NULL, NULL},
1997         {"( 1.3.6.1.4.1.1466.115.121.1.12 DESC 'Distinguished Name' )",
1998                 0, dnValidate, dnNormalize, dnPretty},
1999         {"( 1.3.6.1.4.1.1466.115.121.1.13 DESC 'Data Quality' )",
2000                 0, NULL, NULL, NULL},
2001         {"( 1.3.6.1.4.1.1466.115.121.1.14 DESC 'Delivery Method' )",
2002                 0, NULL, NULL, NULL},
2003         {"( 1.3.6.1.4.1.1466.115.121.1.15 DESC 'Directory String' )",
2004                 0, UTF8StringValidate, UTF8StringNormalize, NULL},
2005         {"( 1.3.6.1.4.1.1466.115.121.1.16 DESC 'DIT Content Rule Description' )",
2006                 0, NULL, NULL, NULL},
2007         {"( 1.3.6.1.4.1.1466.115.121.1.17 DESC 'DIT Structure Rule Description' )",
2008                 0, NULL, NULL, NULL},
2009         {"( 1.3.6.1.4.1.1466.115.121.1.19 DESC 'DSA Quality' )",
2010                 0, NULL, NULL, NULL},
2011         {"( 1.3.6.1.4.1.1466.115.121.1.20 DESC 'DSE Type' )",
2012                 0, NULL, NULL, NULL},
2013         {"( 1.3.6.1.4.1.1466.115.121.1.21 DESC 'Enhanced Guide' )",
2014                 0, NULL, NULL, NULL},
2015         {"( 1.3.6.1.4.1.1466.115.121.1.22 DESC 'Facsimile Telephone Number' )",
2016                 0, IA5StringValidate, faxNumberNormalize, NULL},
2017         {"( 1.3.6.1.4.1.1466.115.121.1.23 DESC 'Fax' " X_NOT_H_R ")",
2018                 SLAP_SYNTAX_BLOB, NULL, NULL, NULL},
2019         {"( 1.3.6.1.4.1.1466.115.121.1.24 DESC 'Generalized Time' )",
2020                 0, generalizedTimeValidate, generalizedTimeNormalize, NULL},
2021         {"( 1.3.6.1.4.1.1466.115.121.1.25 DESC 'Guide' )",
2022                 0, NULL, NULL, NULL},
2023         {"( 1.3.6.1.4.1.1466.115.121.1.26 DESC 'IA5 String' )",
2024                 0, IA5StringValidate, IA5StringNormalize, NULL},
2025         {"( 1.3.6.1.4.1.1466.115.121.1.27 DESC 'Integer' )",
2026                 0, integerValidate, integerNormalize, integerPretty},
2027         {"( 1.3.6.1.4.1.1466.115.121.1.28 DESC 'JPEG' " X_NOT_H_R ")",
2028                 SLAP_SYNTAX_BLOB, blobValidate, NULL, NULL},
2029         {"( 1.3.6.1.4.1.1466.115.121.1.29 DESC 'Master And Shadow Access Points' )",
2030                 0, NULL, NULL, NULL},
2031         {"( 1.3.6.1.4.1.1466.115.121.1.30 DESC 'Matching Rule Description' )",
2032                 0, NULL, NULL, NULL},
2033         {"( 1.3.6.1.4.1.1466.115.121.1.31 DESC 'Matching Rule Use Description' )",
2034                 0, NULL, NULL, NULL},
2035         {"( 1.3.6.1.4.1.1466.115.121.1.32 DESC 'Mail Preference' )",
2036                 0, NULL, NULL, NULL},
2037         {"( 1.3.6.1.4.1.1466.115.121.1.33 DESC 'MHS OR Address' )",
2038                 0, NULL, NULL, NULL},
2039         {"( 1.3.6.1.4.1.1466.115.121.1.34 DESC 'Name And Optional UID' )",
2040                 0, NULL, NULL, NULL},
2041         {"( 1.3.6.1.4.1.1466.115.121.1.35 DESC 'Name Form Description' )",
2042                 0, NULL, NULL, NULL},
2043         {"( 1.3.6.1.4.1.1466.115.121.1.36 DESC 'Numeric String' )",
2044                 0, IA5StringValidate, numericStringNormalize, NULL},
2045         {"( 1.3.6.1.4.1.1466.115.121.1.37 DESC 'Object Class Description' )",
2046                 0, NULL, NULL, NULL},
2047         {"( 1.3.6.1.4.1.1466.115.121.1.38 DESC 'OID' )",
2048                 0, oidValidate, NULL, NULL},
2049         {"( 1.3.6.1.4.1.1466.115.121.1.39 DESC 'Other Mailbox' )",
2050                 0, NULL, NULL, NULL},
2051         {"( 1.3.6.1.4.1.1466.115.121.1.40 DESC 'Octet String' )",
2052                 0, blobValidate, NULL, NULL},
2053         {"( 1.3.6.1.4.1.1466.115.121.1.41 DESC 'Postal Address' )",
2054                 0, blobValidate, NULL, NULL},
2055         {"( 1.3.6.1.4.1.1466.115.121.1.42 DESC 'Protocol Information' )",
2056                 0, NULL, NULL, NULL},
2057         {"( 1.3.6.1.4.1.1466.115.121.1.43 DESC 'Presentation Address' )",
2058                 0, NULL, NULL, NULL},
2059         {"( 1.3.6.1.4.1.1466.115.121.1.44 DESC 'Printable String' )",
2060                 0, printableStringValidate, NULL, NULL},
2061         {"( 1.3.6.1.4.1.1466.115.121.1.49 DESC 'Supported Algorithm' "
2062                 X_BINARY X_NOT_H_R ")",
2063                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
2064         {"( 1.3.6.1.4.1.1466.115.121.1.50 DESC 'Telephone Number' )",
2065                 0, IA5StringValidate, phoneNumberNormalize, NULL},
2066         {"( 1.3.6.1.4.1.1466.115.121.1.51 DESC 'Teletex Terminal Identifier' )",
2067                 0, NULL, NULL, NULL},
2068         {"( 1.3.6.1.4.1.1466.115.121.1.52 DESC 'Telex Number' )",
2069                 0, IA5StringValidate, telexNumberNormalize, NULL},
2070         {"( 1.3.6.1.4.1.1466.115.121.1.53 DESC 'UTC Time' )",
2071                 0, utcTimeValidate, utcTimeNormalize, NULL},
2072         {"( 1.3.6.1.4.1.1466.115.121.1.54 DESC 'LDAP Syntax Description' )",
2073                 0, NULL, NULL, NULL},
2074         {"( 1.3.6.1.4.1.1466.115.121.1.55 DESC 'Modify Rights' )",
2075                 0, NULL, NULL, NULL},
2076         {"( 1.3.6.1.4.1.1466.115.121.1.56 DESC 'LDAP Schema Definition' )",
2077                 0, NULL, NULL, NULL},
2078         {"( 1.3.6.1.4.1.1466.115.121.1.57 DESC 'LDAP Schema Description' )",
2079                 0, NULL, NULL, NULL},
2080         {"( 1.3.6.1.4.1.1466.115.121.1.58 DESC 'Substring Assertion' )",
2081                 0, NULL, NULL, NULL},
2082
2083         /* OpenLDAP Experimental Syntaxes */
2084         {"( 1.3.6.1.4.1.4203.666.2.1 DESC 'OpenLDAP Experimental ACI' )",
2085                 0, IA5StringValidate /* THIS WILL CHANGE FOR NEW ACI SYNTAX */, NULL, NULL},
2086         {"( 1.3.6.1.4.1.4203.666.2.2 DESC 'OpenLDAP authPassword' )",
2087                 0, NULL, NULL, NULL},
2088         {"( 1.3.6.1.4.1.4203.666.2.3 DESC 'OpenLDAP void' " X_HIDE ")" ,
2089                 SLAP_SYNTAX_HIDE, inValidate, NULL, NULL},
2090 #if 0 /* not needed */
2091         {"( 1.3.6.1.4.1.4203.666.2.4 DESC 'OpenLDAP DN' " X_HIDE ")" ,
2092                 SLAP_SYNTAX_HIDE, inValidate, NULL, NULL},
2093 #endif
2094
2095         {NULL, 0, NULL, NULL, NULL}
2096 };
2097
2098 struct mrule_defs_rec {
2099         char *                                          mrd_desc;
2100         unsigned                                        mrd_usage;
2101         slap_mr_convert_func *          mrd_convert;
2102         slap_mr_normalize_func *        mrd_normalize;
2103         slap_mr_match_func *            mrd_match;
2104         slap_mr_indexer_func *          mrd_indexer;
2105         slap_mr_filter_func *           mrd_filter;
2106
2107         char *                                          mrd_associated;
2108 };
2109
2110 /*
2111  * Other matching rules in X.520 that we do not use (yet):
2112  *
2113  * 2.5.13.9             numericStringOrderingMatch
2114  * 2.5.13.13    booleanMatch
2115  * 2.5.13.15    integerOrderingMatch
2116  * 2.5.13.18    octetStringOrderingMatch
2117  * 2.5.13.19    octetStringSubstringsMatch
2118  * 2.5.13.25    uTCTimeMatch
2119  * 2.5.13.26    uTCTimeOrderingMatch
2120  * 2.5.13.31    directoryStringFirstComponentMatch
2121  * 2.5.13.32    wordMatch
2122  * 2.5.13.33    keywordMatch
2123  * 2.5.13.34    certificateExactMatch
2124  * 2.5.13.35    certificateMatch
2125  * 2.5.13.36    certificatePairExactMatch
2126  * 2.5.13.37    certificatePairMatch
2127  * 2.5.13.38    certificateListExactMatch
2128  * 2.5.13.39    certificateListMatch
2129  * 2.5.13.40    algorithmIdentifierMatch
2130  * 2.5.13.41    storedPrefixMatch
2131  * 2.5.13.42    attributeCertificateMatch
2132  * 2.5.13.43    readerAndKeyIDMatch
2133  * 2.5.13.44    attributeIntegrityMatch
2134  */
2135
2136 struct mrule_defs_rec mrule_defs[] = {
2137         /*
2138          * EQUALITY matching rules must be listed after associated APPROX
2139          * matching rules.  So, we list all APPROX matching rules first.
2140          */
2141         {"( " directoryStringApproxMatchOID " NAME 'directoryStringApproxMatch' "
2142                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
2143                 SLAP_MR_EQUALITY_APPROX | SLAP_MR_EXT,
2144                 NULL, NULL,
2145                 directoryStringApproxMatch, NULL, NULL,
2146                 NULL},
2147
2148         {"( " IA5StringApproxMatchOID " NAME 'IA5StringApproxMatch' "
2149                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
2150                 SLAP_MR_EQUALITY_APPROX | SLAP_MR_EXT,
2151                 NULL, NULL,
2152                 IA5StringApproxMatch, NULL, NULL,
2153                 NULL},
2154
2155         /*
2156          * Other matching rules
2157          */
2158         
2159         {"( 2.5.13.0 NAME 'objectIdentifierMatch' "
2160                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
2161                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2162                 NULL, NULL,
2163                 objectIdentifierMatch, caseIgnoreIA5Indexer, caseIgnoreIA5Filter,
2164                 NULL},
2165
2166         {"( 2.5.13.1 NAME 'distinguishedNameMatch' "
2167                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )",
2168                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2169                 NULL, NULL,
2170                 dnMatch, dnIndexer, dnFilter,
2171                 NULL},
2172
2173         {"( 2.5.13.2 NAME 'caseIgnoreMatch' "
2174                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
2175                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2176                 NULL, NULL,
2177                 caseIgnoreMatch, caseIgnoreIndexer, caseIgnoreFilter,
2178                 directoryStringApproxMatchOID },
2179
2180         {"( 2.5.13.3 NAME 'caseIgnoreOrderingMatch' "
2181                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
2182                 SLAP_MR_ORDERING,
2183                 NULL, NULL,
2184                 caseIgnoreOrderingMatch, NULL, NULL,
2185                 NULL},
2186
2187         {"( 2.5.13.4 NAME 'caseIgnoreSubstringsMatch' "
2188                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
2189                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
2190                 NULL, NULL,
2191                 caseIgnoreSubstringsMatch,
2192                 caseIgnoreSubstringsIndexer,
2193                 caseIgnoreSubstringsFilter,
2194                 NULL},
2195
2196         {"( 2.5.13.5 NAME 'caseExactMatch' "
2197                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
2198                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2199                 NULL, NULL,
2200                 caseExactMatch, caseExactIndexer, caseExactFilter,
2201                 directoryStringApproxMatchOID },
2202
2203         {"( 2.5.13.6 NAME 'caseExactOrderingMatch' "
2204                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
2205                 SLAP_MR_ORDERING,
2206                 NULL, NULL,
2207                 caseExactOrderingMatch, NULL, NULL,
2208                 NULL},
2209
2210         {"( 2.5.13.7 NAME 'caseExactSubstringsMatch' "
2211                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
2212                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
2213                 NULL, NULL,
2214                 caseExactSubstringsMatch,
2215                 caseExactSubstringsIndexer,
2216                 caseExactSubstringsFilter,
2217                 NULL},
2218
2219         {"( 2.5.13.8 NAME 'numericStringMatch' "
2220                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.36 )",
2221                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2222                 NULL, NULL,
2223                 caseIgnoreIA5Match, NULL, NULL,
2224                 NULL},
2225
2226         {"( 2.5.13.10 NAME 'numericStringSubstringsMatch' "
2227                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
2228                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
2229                 NULL, NULL,
2230                 caseIgnoreIA5SubstringsMatch,
2231                 caseIgnoreIA5SubstringsIndexer,
2232                 caseIgnoreIA5SubstringsFilter,
2233                 NULL},
2234
2235         {"( 2.5.13.11 NAME 'caseIgnoreListMatch' "
2236                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.41 )",
2237                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2238                 NULL, NULL,
2239                 caseIgnoreListMatch, NULL, NULL,
2240                 NULL},
2241
2242         {"( 2.5.13.12 NAME 'caseIgnoreListSubstringsMatch' "
2243                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
2244                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
2245                 NULL, NULL,
2246                 caseIgnoreListSubstringsMatch, NULL, NULL,
2247                 NULL},
2248
2249         {"( 2.5.13.13 NAME 'booleanMatch' "
2250                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 )",
2251                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2252                 NULL, NULL,
2253                 booleanMatch, NULL, NULL,
2254                 NULL},
2255
2256         {"( 2.5.13.14 NAME 'integerMatch' "
2257                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
2258                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2259                 NULL, NULL,
2260                 integerMatch, NULL, NULL,
2261                 NULL},
2262
2263         {"( 2.5.13.16 NAME 'bitStringMatch' "
2264                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.6 )",
2265                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2266                 NULL, NULL,
2267                 bitStringMatch, NULL, NULL,
2268                 NULL},
2269
2270         {"( 2.5.13.17 NAME 'octetStringMatch' "
2271                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
2272                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2273                 NULL, NULL,
2274                 octetStringMatch, octetStringIndexer, octetStringFilter,
2275                 NULL},
2276
2277         {"( 2.5.13.20 NAME 'telephoneNumberMatch' "
2278                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.50 )",
2279                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2280                 NULL, NULL,
2281                 telephoneNumberMatch, NULL, NULL,
2282                 NULL},
2283
2284         {"( 2.5.13.21 NAME 'telephoneNumberSubstringsMatch' "
2285                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
2286                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
2287                 NULL, NULL,
2288                 telephoneNumberSubstringsMatch, NULL, NULL,
2289                 NULL},
2290
2291         {"( 2.5.13.22 NAME 'presentationAddressMatch' "
2292                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.43 )",
2293                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2294                 NULL, NULL,
2295                 presentationAddressMatch, NULL, NULL,
2296                 NULL},
2297
2298         {"( 2.5.13.23 NAME 'uniqueMemberMatch' "
2299                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.34 )",
2300                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2301                 NULL, NULL,
2302                 uniqueMemberMatch, NULL, NULL,
2303                 NULL},
2304
2305         {"( 2.5.13.24 NAME 'protocolInformationMatch' "
2306                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.42 )",
2307                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2308                 NULL, NULL,
2309                 protocolInformationMatch, NULL, NULL,
2310                 NULL},
2311
2312         {"( 2.5.13.27 NAME 'generalizedTimeMatch' "
2313                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )",
2314                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2315                 NULL, NULL,
2316                 generalizedTimeMatch, NULL, NULL,
2317                 NULL},
2318
2319         {"( 2.5.13.28 NAME 'generalizedTimeOrderingMatch' "
2320                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )",
2321                 SLAP_MR_ORDERING,
2322                 NULL, NULL,
2323                 generalizedTimeOrderingMatch, NULL, NULL,
2324                 NULL},
2325
2326         {"( 2.5.13.29 NAME 'integerFirstComponentMatch' "
2327                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
2328                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2329                 NULL, NULL,
2330                 integerFirstComponentMatch, NULL, NULL,
2331                 NULL},
2332
2333         {"( 2.5.13.30 NAME 'objectIdentifierFirstComponentMatch' "
2334                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
2335                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2336                 NULL, NULL,
2337                 objectIdentifierFirstComponentMatch, NULL, NULL,
2338                 NULL},
2339
2340         {"( 1.3.6.1.4.1.1466.109.114.1 NAME 'caseExactIA5Match' "
2341                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
2342                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2343                 NULL, NULL,
2344                 caseExactIA5Match, caseExactIA5Indexer, caseExactIA5Filter,
2345                 IA5StringApproxMatchOID },
2346
2347         {"( 1.3.6.1.4.1.1466.109.114.2 NAME 'caseIgnoreIA5Match' "
2348                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
2349                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
2350                 NULL, NULL,
2351                 caseIgnoreIA5Match, caseExactIA5Indexer, caseExactIA5Filter,
2352                 IA5StringApproxMatchOID },
2353
2354         {"( 1.3.6.1.4.1.1466.109.114.3 NAME 'caseIgnoreIA5SubstringsMatch' "
2355                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
2356                 SLAP_MR_SUBSTR,
2357                 NULL, NULL,
2358                 caseIgnoreIA5SubstringsMatch,
2359                 caseIgnoreIA5SubstringsIndexer,
2360                 caseIgnoreIA5SubstringsFilter,
2361                 NULL},
2362
2363         {"( 1.3.6.1.4.1.4203.666.4.3 NAME 'caseExactIA5SubstringsMatch' "
2364                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
2365                 SLAP_MR_SUBSTR,
2366                 NULL, NULL,
2367                 caseExactIA5SubstringsMatch,
2368                 caseExactIA5SubstringsIndexer,
2369                 caseExactIA5SubstringsFilter,
2370                 NULL},
2371
2372         {"( 1.3.6.1.4.1.4203.666.4.1 NAME 'authPasswordMatch' "
2373                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
2374                 SLAP_MR_EQUALITY,
2375                 NULL, NULL,
2376                 authPasswordMatch, NULL, NULL,
2377                 NULL},
2378
2379         {"( 1.3.6.1.4.1.4203.666.4.2 NAME 'OpenLDAPaciMatch' "
2380                 "SYNTAX 1.3.6.1.4.1.4203.666.2.1 )",
2381                 SLAP_MR_EQUALITY,
2382                 NULL, NULL,
2383                 OpenLDAPaciMatch, NULL, NULL,
2384                 NULL},
2385
2386         {NULL, SLAP_MR_NONE, NULL, NULL, NULL, NULL}
2387 };
2388
2389 int
2390 schema_init( void )
2391 {
2392         int             res;
2393         int             i;
2394
2395         /* we should only be called once (from main) */
2396         assert( schema_init_done == 0 );
2397
2398         for ( i=0; syntax_defs[i].sd_desc != NULL; i++ ) {
2399                 res = register_syntax( syntax_defs[i].sd_desc,
2400                     syntax_defs[i].sd_flags,
2401                     syntax_defs[i].sd_validate,
2402                     syntax_defs[i].sd_normalize,
2403                         syntax_defs[i].sd_pretty
2404 #ifdef SLAPD_BINARY_CONVERSION
2405                         ,
2406                     syntax_defs[i].sd_ber2str,
2407                         syntax_defs[i].sd_str2ber
2408 #endif
2409                 );
2410
2411                 if ( res ) {
2412                         fprintf( stderr, "schema_init: Error registering syntax %s\n",
2413                                  syntax_defs[i].sd_desc );
2414                         return LDAP_OTHER;
2415                 }
2416         }
2417
2418         for ( i=0; mrule_defs[i].mrd_desc != NULL; i++ ) {
2419                 if( mrule_defs[i].mrd_usage == SLAP_MR_NONE ) {
2420                         fprintf( stderr,
2421                                 "schema_init: Ingoring unusable matching rule %s\n",
2422                                  mrule_defs[i].mrd_desc );
2423                         continue;
2424                 }
2425
2426                 res = register_matching_rule(
2427                         mrule_defs[i].mrd_desc,
2428                         mrule_defs[i].mrd_usage,
2429                         mrule_defs[i].mrd_convert,
2430                         mrule_defs[i].mrd_normalize,
2431                     mrule_defs[i].mrd_match,
2432                         mrule_defs[i].mrd_indexer,
2433                         mrule_defs[i].mrd_filter,
2434                         mrule_defs[i].mrd_associated );
2435
2436                 if ( res ) {
2437                         fprintf( stderr,
2438                                 "schema_init: Error registering matching rule %s\n",
2439                                  mrule_defs[i].mrd_desc );
2440                         return LDAP_OTHER;
2441                 }
2442         }
2443         schema_init_done = 1;
2444         return LDAP_SUCCESS;
2445 }