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