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