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