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