]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_init.c
5e7f8e8224ae81647fa1a7f80337101cd32b47fd
[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( inlen > left.bv_len ) {
553                 match = 1;
554                 goto done;
555         }
556
557         if( sub->sa_initial ) {
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                 if( inlen > left.bv_len ) {
570                         match = 1;
571                         goto done;
572                 }
573         }
574
575         if( sub->sa_final ) {
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                 if( inlen > left.bv_len ) {
588                         match = 1;
589                         goto done;
590                 }
591         }
592
593         if( sub->sa_any ) {
594                 for(i=0; sub->sa_any[i]; i++) {
595                         ber_len_t idx;
596                         char *p;
597
598 retry:
599                         if( sub->sa_any[i]->bv_len == 0 ) {
600                                 continue;
601                         }
602
603                         p = strchr( left.bv_val, *sub->sa_any[i]->bv_val );
604
605                         if( p == NULL ) {
606                                 match = 1;
607                                 goto done;
608                         }
609
610                         idx = p - left.bv_val;
611                         assert( idx < left.bv_len );
612
613                         if( idx >= left.bv_len ) {
614                                 /* this shouldn't happen */
615                                 return LDAP_OTHER;
616                         }
617
618                         left.bv_val = p;
619                         left.bv_len -= idx;
620
621                         if( sub->sa_any[i]->bv_len > left.bv_len ) {
622                                 /* not enough left */
623                                 match = 1;
624                                 goto done;
625                         }
626
627                         match = strncmp( left.bv_val,
628                                 sub->sa_any[i]->bv_val,
629                                 sub->sa_any[i]->bv_len );
630
631                         if( match != 0 ) {
632                                 goto retry;
633                         }
634
635                         left.bv_val += sub->sa_any[i]->bv_len;
636                         left.bv_len -= sub->sa_any[i]->bv_len;
637                         inlen -= sub->sa_any[i]->bv_len;
638
639                         if( inlen > left.bv_len ) {
640                                 /* not enough length */
641                                 match = 1;
642                                 goto done;
643                         }
644                 }
645         }
646
647 done:
648         *matchp = match;
649         return LDAP_SUCCESS;
650 }
651
652 static int
653 caseIgnoreIA5Match(
654         int *match,
655         unsigned use,
656         Syntax *syntax,
657         MatchingRule *mr,
658         struct berval *value,
659         void *assertedValue )
660 {
661         *match = strcasecmp( value->bv_val,
662                 ((struct berval *) assertedValue)->bv_val );
663         return LDAP_SUCCESS;
664 }
665
666 static char *strcasechr( const char *str, int c )
667 {
668         char *lower = strchr( str, TOLOWER(c) );
669         char *upper = strchr( str, TOUPPER(c) );
670
671         if( lower && upper ) {
672                 return lower < upper ? lower : upper;
673         } else if ( lower ) {
674                 return lower;
675         } else {
676                 return upper;
677         }
678 }
679
680 static int
681 caseIgnoreIA5SubstringsMatch(
682         int *matchp,
683         unsigned use,
684         Syntax *syntax,
685         MatchingRule *mr,
686         struct berval *value,
687         void *assertedValue )
688 {
689         int match = 0;
690         SubstringsAssertion *sub = assertedValue;
691         struct berval left = *value;
692         int i;
693         ber_len_t inlen=0;
694
695         /* Add up asserted input length */
696         if( sub->sa_initial ) {
697                 inlen += sub->sa_initial->bv_len;
698         }
699         if( sub->sa_any ) {
700                 for(i=0; sub->sa_any[i] != NULL; i++) {
701                         inlen += sub->sa_any[i]->bv_len;
702                 }
703         }
704         if( sub->sa_final ) {
705                 inlen += sub->sa_final->bv_len;
706         }
707
708         if( inlen > left.bv_len ) {
709                 match = 1;
710                 goto done;
711         }
712
713         if( sub->sa_initial ) {
714                 match = strncasecmp( sub->sa_initial->bv_val, left.bv_val,
715                         sub->sa_initial->bv_len );
716
717                 if( match != 0 ) {
718                         goto done;
719                 }
720
721                 left.bv_val += sub->sa_initial->bv_len;
722                 left.bv_len -= sub->sa_initial->bv_len;
723                 inlen -= sub->sa_initial->bv_len;
724
725                 if( inlen > left.bv_len ) {
726                         match = 1;
727                         goto done;
728                 }
729         }
730
731         if( sub->sa_final ) {
732                 match = strncasecmp( sub->sa_final->bv_val,
733                         &left.bv_val[left.bv_len - sub->sa_final->bv_len],
734                         sub->sa_final->bv_len );
735
736                 if( match != 0 ) {
737                         goto done;
738                 }
739
740                 left.bv_len -= sub->sa_final->bv_len;
741                 inlen -= sub->sa_final->bv_len;
742
743                 if( inlen > left.bv_len ) {
744                         match = 1;
745                         goto done;
746                 }
747         }
748
749         if( sub->sa_any ) {
750                 for(i=0; sub->sa_any[i]; i++) {
751                         ber_len_t idx;
752                         char *p;
753
754 retry:
755                         if( sub->sa_any[i]->bv_len == 0 ) {
756                                 continue;
757                         }
758
759                         p = strcasechr( left.bv_val, *sub->sa_any[i]->bv_val );
760
761                         if( p == NULL ) {
762                                 match = 1;
763                                 goto done;
764                         }
765
766                         idx = p - left.bv_val;
767                         assert( idx < left.bv_len );
768
769                         if( idx >= left.bv_len ) {
770                                 /* this shouldn't happen */
771                                 return LDAP_OTHER;
772                         }
773
774                         left.bv_val = p;
775                         left.bv_len -= idx;
776
777                         if( sub->sa_any[i]->bv_len > left.bv_len ) {
778                                 /* not enough left */
779                                 match = 1;
780                                 goto done;
781                         }
782
783                         match = strncasecmp( left.bv_val,
784                                 sub->sa_any[i]->bv_val,
785                                 sub->sa_any[i]->bv_len );
786
787                         if( match != 0 ) {
788                                 goto retry;
789                         }
790
791                         left.bv_val += sub->sa_any[i]->bv_len;
792                         left.bv_len -= sub->sa_any[i]->bv_len;
793                         inlen -= sub->sa_any[i]->bv_len;
794
795                         if( inlen > left.bv_len ) {
796                                 /* not enough length */
797                                 match = 1;
798                                 goto done;
799                         }
800                 }
801         }
802
803 done:
804         *matchp = match;
805         return LDAP_SUCCESS;
806 }
807
808 /* Index generation function */
809 int caseIgnoreIA5Indexer(
810         unsigned use,
811         Syntax *syntax,
812         MatchingRule *mr,
813         struct berval *prefix,
814         struct berval **values,
815         struct berval ***keysp )
816 {
817         int i;
818         size_t slen, mlen;
819         struct berval **keys;
820         lutil_MD5_CTX   MD5context;
821         unsigned char   MD5digest[16];
822         struct berval digest;
823         digest.bv_val = MD5digest;
824         digest.bv_len = sizeof(MD5digest);
825
826         for( i=0; values[i] != NULL; i++ ) {
827                 /* just count them */
828         }
829
830         assert( i > 0 );
831
832         keys = ch_malloc( sizeof( struct berval * ) * (i+1) );
833
834         slen = strlen( syntax->ssyn_oid );
835         mlen = strlen( mr->smr_oid );
836
837         for( i=0; values[i] != NULL; i++ ) {
838                 struct berval *value = ber_bvdup( values[i] );
839                 ldap_pvt_str2upper( value->bv_val );
840
841                 lutil_MD5Init( &MD5context );
842                 if( prefix != NULL && prefix->bv_len > 0 ) {
843                         lutil_MD5Update( &MD5context,
844                                 prefix->bv_val, prefix->bv_len );
845                 }
846                 lutil_MD5Update( &MD5context,
847                         syntax->ssyn_oid, slen );
848                 lutil_MD5Update( &MD5context,
849                         mr->smr_oid, mlen );
850                 lutil_MD5Update( &MD5context,
851                         value->bv_val, value->bv_len );
852                 lutil_MD5Final( MD5digest, &MD5context );
853
854                 ber_bvfree( value );
855
856                 keys[i] = ber_bvdup( &digest );
857         }
858
859         keys[i] = NULL;
860         *keysp = keys;
861         return LDAP_SUCCESS;
862 }
863
864 /* Index generation function */
865 int caseIgnoreIA5Filter(
866         unsigned use,
867         Syntax *syntax,
868         MatchingRule *mr,
869         struct berval *prefix,
870         void * assertValue,
871         struct berval ***keysp )
872 {
873         size_t slen, mlen;
874         struct berval **keys;
875         lutil_MD5_CTX   MD5context;
876         unsigned char   MD5digest[LUTIL_MD5_BYTES];
877         struct berval *value;
878         struct berval digest;
879         digest.bv_val = MD5digest;
880         digest.bv_len = sizeof(MD5digest);
881
882         slen = strlen( syntax->ssyn_oid );
883         mlen = strlen( mr->smr_oid );
884
885         value = ber_bvdup( (struct berval *) assertValue );
886         ldap_pvt_str2upper( value->bv_val );
887
888         keys = ch_malloc( sizeof( struct berval * ) * 2 );
889
890         lutil_MD5Init( &MD5context );
891         if( prefix != NULL && prefix->bv_len > 0 ) {
892                 lutil_MD5Update( &MD5context,
893                         prefix->bv_val, prefix->bv_len );
894         }
895         lutil_MD5Update( &MD5context,
896                 syntax->ssyn_oid, slen );
897         lutil_MD5Update( &MD5context,
898                 mr->smr_oid, mlen );
899         lutil_MD5Update( &MD5context,
900                 value->bv_val, value->bv_len );
901         lutil_MD5Final( MD5digest, &MD5context );
902
903         keys[0] = ber_bvdup( &digest );
904         keys[1] = NULL;
905
906         ber_bvfree( value );
907
908         *keysp = keys;
909         return LDAP_SUCCESS;
910 }
911
912 static int
913 NumericStringNormalize(
914         Syntax *syntax,
915         struct berval *val,
916         struct berval **normalized )
917 {
918         /* similiar to IA5StringNormalize except removes all spaces */
919         struct berval *newval;
920         char *p, *q;
921
922         newval = ch_malloc( sizeof( struct berval ) );
923
924         p = val->bv_val;
925
926         /* Ignore initial whitespace */
927         while ( isspace( *p ) ) {
928                 p++;
929         }
930
931         if( *p != '\0' ) {
932                 ch_free( newval );
933                 return LDAP_INVALID_SYNTAX;
934         }
935
936         newval->bv_val = ch_strdup( p );
937         p = q = newval->bv_val;
938
939         while ( *p ) {
940                 if ( isspace( *p ) ) {
941                         /* Ignore whitespace */
942                         p++;
943                 } else {
944                         *q++ = *p++;
945                 }
946         }
947
948         assert( *newval->bv_val );
949         assert( newval->bv_val < p );
950         assert( p <= q );
951
952         /* cannot start with a space */
953         assert( !isspace(*newval->bv_val) );
954
955         /* cannot end with a space */
956         assert( !isspace( q[-1] ) );
957
958         /* null terminate */
959         *q = '\0';
960
961         newval->bv_len = q - newval->bv_val;
962         *normalized = newval;
963
964         return LDAP_SUCCESS;
965 }
966
967 static int
968 check_time_syntax (struct berval *val,
969         int start,
970         int *parts)
971 {
972         static int ceiling[9] = { 99, 99, 11, 30, 23, 59, 59, 12, 59 };
973         static int mdays[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
974         char *p, *e;
975         int part, c, neg = 0;
976
977         if( val->bv_len == 0 )
978                 return LDAP_INVALID_SYNTAX;
979
980         p = (char *)val->bv_val;
981         e = p + val->bv_len;
982
983         /* Ignore initial whitespace */
984         while ( ( p < e ) && isspace( *p ) ) {
985                 p++;
986         }
987
988         if (e - p < 13 - (2 * start))
989                 return LDAP_INVALID_SYNTAX;
990
991         for (part = 0; part < 9; part++)
992                 parts[part] = 0;
993
994         for (part = start; part < 7; part++) {
995                 c = *p;
996                 if ((part == 6)
997                         && (c == 'Z'
998                                 || c == '+'
999                                 || c == '-'))
1000                 {
1001                         part++;
1002                         break;
1003                 }
1004                 p++;
1005                 c -= '0';
1006                 if (p == e)
1007                         return LDAP_INVALID_SYNTAX;
1008                 if (c < 0 || c > 9)
1009                         return LDAP_INVALID_SYNTAX;
1010                 parts[part] = c;
1011
1012                 c = *p++ - '0';
1013                 if (p == e)
1014                         return LDAP_INVALID_SYNTAX;
1015                 if (c < 0 || c > 9)
1016                         return LDAP_INVALID_SYNTAX;
1017                 parts[part] *= 10;
1018                 parts[part] += c;
1019
1020                 if (part == 2 || part == 3)
1021                         parts[part]--;
1022                 if (parts[part] < 0)
1023                         return LDAP_INVALID_SYNTAX;
1024                 if (parts[part] > ceiling[part])
1025                         return LDAP_INVALID_SYNTAX;
1026         }
1027         if (parts[2] == 1) {
1028                 if (parts[3] > mdays[parts[2]])
1029                         return LDAP_INVALID_SYNTAX;
1030                 if (parts[1] & 0x03) {
1031                         /* FIXME:  This is an incomplete leap-year
1032                          * check that fails in 2100, 2200, 2300,
1033                          * 2500, 2600, 2700, ...
1034                          */
1035                         if (parts[3] > mdays[parts[2]] - 1)
1036                                 return LDAP_INVALID_SYNTAX;
1037                 }
1038         }
1039         c = *p++;
1040         if (c == 'Z') {
1041                 /* all done */
1042         } else if (c != '+' && c != '-') {
1043                 return LDAP_INVALID_SYNTAX;
1044         } else {
1045                 if (c == '-')
1046                         neg = 1;
1047                 if (p > e - 4)
1048                         return LDAP_INVALID_SYNTAX;
1049                 for (part = 7; part < 9; part++) {
1050                         c = *p++ - '0';
1051                         if (c < 0 || c > 9)
1052                                 return LDAP_INVALID_SYNTAX;
1053                         parts[part] = c;
1054
1055                         c = *p++ - '0';
1056                         if (c < 0 || c > 9)
1057                                 return LDAP_INVALID_SYNTAX;
1058                         parts[part] *= 10;
1059                         parts[part] += c;
1060                         if (parts[part] < 0 || parts[part] > ceiling[part])
1061                                 return LDAP_INVALID_SYNTAX;
1062                 }
1063         }
1064
1065         /* Ignore trailing whitespace */
1066         while ( ( p < e ) && isspace( *p ) ) {
1067                 p++;
1068         }
1069         if (p != e)
1070                 return LDAP_INVALID_SYNTAX;
1071
1072         if (neg == 0) {
1073                 parts[4] += parts[7];
1074                 parts[5] += parts[8];
1075                 for (part = 7; --part > 0; ) {
1076                         if (part != 3)
1077                                 c = ceiling[part];
1078                         else {
1079                                 /* FIXME:  This is an incomplete leap-year
1080                                  * check that fails in 2100, 2200, 2300,
1081                                  * 2500, 2600, 2700, ...
1082                                  */
1083                                 c = mdays[parts[2]];
1084                                 if (parts[2] == 1)
1085                                         c--;
1086                         }
1087                         if (parts[part] > c) {
1088                                 parts[part] -= c + 1;
1089                                 parts[part - 1]++;
1090                         }
1091                 }
1092         } else {
1093                 parts[4] -= parts[7];
1094                 parts[5] -= parts[8];
1095                 for (part = 7; --part > 0; ) {
1096                         if (part != 3)
1097                                 c = ceiling[part];
1098                         else {
1099                                 /* FIXME:  This is an incomplete leap-year
1100                                  * check that fails in 2100, 2200, 2300,
1101                                  * 2500, 2600, 2700, ...
1102                                  */
1103                                 c = mdays[(parts[2] - 1) % 12];
1104                                 if (parts[2] == 2)
1105                                         c--;
1106                         }
1107                         if (parts[part] < 0) {
1108                                 parts[part] += c + 1;
1109                                 parts[part - 1]--;
1110                         }
1111                 }
1112         }
1113
1114         return LDAP_SUCCESS;
1115 }
1116
1117 static int
1118 utcTimeNormalize(
1119         Syntax *syntax,
1120         struct berval *val,
1121         struct berval **normalized )
1122 {
1123         struct berval *out;
1124         int parts[9], rc;
1125
1126         rc = check_time_syntax(val, 1, parts);
1127         if (rc != LDAP_SUCCESS) {
1128                 return rc;
1129         }
1130
1131         *normalized = NULL;
1132         out = ch_malloc( sizeof(struct berval) );
1133         if( out == NULL )
1134                 return LBER_ERROR_MEMORY;
1135
1136         out->bv_val = ch_malloc( 14 );
1137         if ( out->bv_val == NULL ) {
1138                 ch_free( out );
1139                 return LBER_ERROR_MEMORY;
1140         }
1141
1142         sprintf( out->bv_val, "%02ld%02ld%02ld%02ld%02ld%02ldZ",
1143                                 parts[1], parts[2] + 1, parts[3] + 1,
1144                                 parts[4], parts[5], parts[6] );
1145         out->bv_len = 13;
1146         *normalized = out;
1147
1148         return LDAP_SUCCESS;
1149 }
1150
1151 static int
1152 utcTimeValidate(
1153         Syntax *syntax,
1154         struct berval *in )
1155 {
1156         int parts[9];
1157
1158         return check_time_syntax(in, 1, parts);
1159 }
1160
1161 static int
1162 generalizedTimeNormalize(
1163         Syntax *syntax,
1164         struct berval *val,
1165         struct berval **normalized )
1166 {
1167         struct berval *out;
1168         int parts[9], rc;
1169
1170         rc = check_time_syntax(val, 0, parts);
1171         if (rc != LDAP_SUCCESS) {
1172                 return rc;
1173         }
1174
1175         *normalized = NULL;
1176         out = ch_malloc( sizeof(struct berval) );
1177         if( out == NULL )
1178                 return LBER_ERROR_MEMORY;
1179
1180         out->bv_val = ch_malloc( 16 );
1181         if ( out->bv_val == NULL ) {
1182                 ch_free( out );
1183                 return LBER_ERROR_MEMORY;
1184         }
1185
1186         sprintf( out->bv_val, "%02ld%02ld%02ld%02ld%02ld%02ld%02ldZ",
1187                                 parts[0], parts[1], parts[2] + 1, parts[3] + 1,
1188                                 parts[4], parts[5], parts[6] );
1189         out->bv_len = 15;
1190         *normalized = out;
1191
1192         return LDAP_SUCCESS;
1193 }
1194
1195 static int
1196 generalizedTimeValidate(
1197         Syntax *syntax,
1198         struct berval *in )
1199 {
1200         int parts[9];
1201
1202         return check_time_syntax(in, 0, parts);
1203 }
1204
1205 struct syntax_defs_rec {
1206         char *sd_desc;
1207         int sd_flags;
1208         slap_syntax_validate_func *sd_validate;
1209         slap_syntax_transform_func *sd_normalize;
1210         slap_syntax_transform_func *sd_pretty;
1211 #ifdef SLAPD_BINARY_CONVERSION
1212         slap_syntax_transform_func *sd_ber2str;
1213         slap_syntax_transform_func *sd_str2ber;
1214 #endif
1215 };
1216
1217 #define X_HIDE "X-HIDE 'TRUE' "
1218 #define X_BINARY "X-BINARY-TRANSFER-REQUIRED 'TRUE' "
1219 #define X_NOT_H_R "X-NOT-HUMAN-READABLE 'TRUE' "
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, blobValidate, NULL, 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, blobValidate, NULL, 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, NULL, NULL, 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 functions */
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 /* unimplemented matching functions */
1394 #define objectIdentifierMatch NULL
1395 #define caseIgnoreListMatch NULL
1396 #define caseIgnoreListSubstringsMatch NULL
1397 #define integerMatch NULL
1398 #define bitStringMatch NULL
1399 #define octetStringMatch NULL
1400 #define telephoneNumberMatch NULL
1401 #define telephoneNumberSubstringsMatch NULL
1402 #define presentationAddressMatch NULL
1403 #define uniqueMemberMatch NULL
1404 #define protocolInformationMatch NULL
1405 #define generalizedTimeMatch caseExactIA5Match
1406 #define generalizedTimeOrderingMatch caseExactIA5Match
1407 #define integerFirstComponentMatch NULL
1408 #define objectIdentifierFirstComponentMatch NULL
1409
1410 #define OpenLDAPaciMatch NULL
1411 #define authPasswordMatch NULL
1412
1413 /* unimplied indexer/filter routines */
1414 #define dnIndexer NULL
1415 #define dnFilter NULL
1416
1417 #define caseIgnoreIndexer               caseIgnoreIA5Indexer
1418 #define caseIgnoreFilter                caseIgnoreIA5Filter
1419 #define caseExactIndexer                caseExactIA5Indexer
1420 #define caseExactFilter                 caseExactIA5Filter
1421 #define caseExactIA5Indexer             caseIgnoreIA5Indexer
1422 #define caseExactIA5Filter              caseIgnoreIA5Filter
1423
1424 struct mrule_defs_rec mrule_defs[] = {
1425         {"( 2.5.13.0 NAME 'objectIdentifierMatch' "
1426                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
1427                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1428                 NULL, NULL, objectIdentifierMatch,
1429                 caseIgnoreIA5Indexer, caseIgnoreIA5Filter},
1430
1431         {"( 2.5.13.1 NAME 'distinguishedNameMatch' "
1432                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )",
1433                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1434                 NULL, NULL, dnMatch, dnIndexer, dnFilter},
1435
1436         {"( 2.5.13.2 NAME 'caseIgnoreMatch' "
1437                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1438                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1439                 NULL, NULL, caseIgnoreMatch, caseIgnoreIndexer, caseIgnoreFilter},
1440
1441         {"( 2.5.13.3 NAME 'caseIgnoreOrderingMatch' "
1442                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1443                 SLAP_MR_ORDERING,
1444                 NULL, NULL, caseIgnoreOrderingMatch, NULL, NULL},
1445
1446         {"( 2.5.13.4 NAME 'caseIgnoreSubstringsMatch' "
1447                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
1448                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
1449                 NULL, NULL, caseIgnoreSubstringsMatch, NULL, NULL},
1450
1451         /* Next three are not in the RFC's, but are needed for compatibility */
1452         {"( 2.5.13.5 NAME 'caseExactMatch' "
1453                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1454                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1455                 NULL, NULL, caseExactMatch, caseExactIndexer, caseExactFilter},
1456
1457         {"( 2.5.13.6 NAME 'caseExactOrderingMatch' "
1458                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1459                 SLAP_MR_ORDERING,
1460                 NULL, NULL, caseExactOrderingMatch, NULL, NULL},
1461
1462         {"( 2.5.13.7 NAME 'caseExactSubstringsMatch' "
1463                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
1464                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
1465                 NULL, NULL, caseExactSubstringsMatch, NULL, NULL},
1466
1467         {"( 2.5.13.8 NAME 'numericStringMatch' "
1468                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.36 )",
1469                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1470                 NULL, NULL, caseIgnoreIA5Match, NULL, NULL},
1471
1472         {"( 2.5.13.10 NAME 'numericStringSubstringsMatch' "
1473                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
1474                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
1475                 NULL, NULL, caseIgnoreIA5SubstringsMatch, NULL, NULL},
1476
1477         {"( 2.5.13.11 NAME 'caseIgnoreListMatch' "
1478                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.41 )",
1479                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1480                 NULL, NULL, caseIgnoreListMatch, NULL, NULL},
1481
1482         {"( 2.5.13.12 NAME 'caseIgnoreListSubstringsMatch' "
1483                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
1484                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
1485                 NULL, NULL, caseIgnoreListSubstringsMatch, NULL, NULL},
1486
1487         {"( 2.5.13.14 NAME 'integerMatch' "
1488                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
1489                 SLAP_MR_NONE | SLAP_MR_EXT,
1490                 NULL, NULL, integerMatch, NULL, NULL},
1491
1492         {"( 2.5.13.16 NAME 'bitStringMatch' "
1493                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.6 )",
1494                 SLAP_MR_NONE | SLAP_MR_EXT,
1495                 NULL, NULL, bitStringMatch, NULL, NULL},
1496
1497         {"( 2.5.13.17 NAME 'octetStringMatch' "
1498                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
1499                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1500                 NULL, NULL, octetStringMatch, octetStringIndexer, octetStringFilter},
1501
1502         {"( 2.5.13.20 NAME 'telephoneNumberMatch' "
1503                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.50 )",
1504                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1505                 NULL, NULL, telephoneNumberMatch, NULL, NULL},
1506
1507         {"( 2.5.13.21 NAME 'telephoneNumberSubstringsMatch' "
1508                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
1509                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
1510                 NULL, NULL, telephoneNumberSubstringsMatch, NULL, NULL},
1511
1512         {"( 2.5.13.22 NAME 'presentationAddressMatch' "
1513                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.43 )",
1514                 SLAP_MR_NONE | SLAP_MR_EXT,
1515                 NULL, NULL, presentationAddressMatch, NULL, NULL},
1516
1517         {"( 2.5.13.23 NAME 'uniqueMemberMatch' "
1518                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.34 )",
1519                 SLAP_MR_NONE | SLAP_MR_EXT,
1520                 NULL, NULL, uniqueMemberMatch, NULL, NULL},
1521
1522         {"( 2.5.13.24 NAME 'protocolInformationMatch' "
1523                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.42 )",
1524                 SLAP_MR_NONE | SLAP_MR_EXT,
1525                 NULL, NULL, protocolInformationMatch, NULL, NULL},
1526
1527         {"( 2.5.13.27 NAME 'generalizedTimeMatch' "
1528                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )",
1529                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1530                 NULL, NULL, generalizedTimeMatch, NULL, NULL},
1531
1532         {"( 2.5.13.28 NAME 'generalizedTimeOrderingMatch' "
1533                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )",
1534                 SLAP_MR_ORDERING,
1535                 NULL, NULL, generalizedTimeOrderingMatch, NULL, NULL},
1536
1537         {"( 2.5.13.29 NAME 'integerFirstComponentMatch' "
1538                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
1539                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1540                 NULL, NULL, integerFirstComponentMatch, NULL, NULL},
1541
1542         {"( 2.5.13.30 NAME 'objectIdentifierFirstComponentMatch' "
1543                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
1544                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1545                 NULL, NULL, objectIdentifierFirstComponentMatch, NULL, NULL},
1546
1547         {"( 1.3.6.1.4.1.1466.109.114.1 NAME 'caseExactIA5Match' "
1548                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
1549                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1550                 NULL, NULL, caseExactIA5Match, caseExactIA5Indexer, caseExactIA5Filter},
1551
1552         {"( 1.3.6.1.4.1.1466.109.114.2 NAME 'caseIgnoreIA5Match' "
1553                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
1554                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1555                 NULL, NULL, caseIgnoreIA5Match, caseExactIA5Indexer, caseExactIA5Filter},
1556
1557         {"( 1.3.6.1.4.1.1466.109.114.3 NAME 'caseIgnoreIA5SubstringsMatch' "
1558                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
1559                 SLAP_MR_SUBSTR,
1560                 NULL, NULL, caseIgnoreIA5SubstringsMatch, NULL, NULL},
1561
1562         {"( 1.3.6.1.4.1.4203.666.4.1 NAME 'authPasswordMatch' "
1563                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
1564                 SLAP_MR_EQUALITY,
1565                 NULL, NULL, authPasswordMatch, NULL, NULL},
1566
1567         {"( 1.3.6.1.4.1.4203.666.4.2 NAME 'OpenLDAPaciMatch' "
1568                 "SYNTAX 1.3.6.1.4.1.4203.666.2.1 )",
1569                 SLAP_MR_EQUALITY,
1570                 NULL, NULL, OpenLDAPaciMatch, NULL, NULL},
1571
1572         {NULL, SLAP_MR_NONE, NULL, NULL, NULL}
1573 };
1574
1575 int
1576 schema_init( void )
1577 {
1578         int             res;
1579         int             i;
1580
1581         /* we should only be called once (from main) */
1582         assert( schema_init_done == 0 );
1583
1584         for ( i=0; syntax_defs[i].sd_desc != NULL; i++ ) {
1585                 res = register_syntax( syntax_defs[i].sd_desc,
1586                     syntax_defs[i].sd_flags,
1587                     syntax_defs[i].sd_validate,
1588                     syntax_defs[i].sd_normalize,
1589                         syntax_defs[i].sd_pretty
1590 #ifdef SLAPD_BINARY_CONVERSION
1591                         ,
1592                     syntax_defs[i].sd_ber2str,
1593                         syntax_defs[i].sd_str2ber
1594 #endif
1595                 );
1596
1597                 if ( res ) {
1598                         fprintf( stderr, "schema_init: Error registering syntax %s\n",
1599                                  syntax_defs[i].sd_desc );
1600                         return LDAP_OTHER;
1601                 }
1602         }
1603
1604         for ( i=0; mrule_defs[i].mrd_desc != NULL; i++ ) {
1605                 if( mrule_defs[i].mrd_usage == SLAP_MR_NONE ) {
1606                         fprintf( stderr,
1607                                 "schema_init: Ingoring unusable matching rule %s\n",
1608                                  mrule_defs[i].mrd_desc );
1609                         continue;
1610                 }
1611
1612                 res = register_matching_rule(
1613                         mrule_defs[i].mrd_desc,
1614                         mrule_defs[i].mrd_usage,
1615                         mrule_defs[i].mrd_convert,
1616                         mrule_defs[i].mrd_normalize,
1617                     mrule_defs[i].mrd_match,
1618                         mrule_defs[i].mrd_indexer,
1619                         mrule_defs[i].mrd_filter );
1620
1621                 if ( res ) {
1622                         fprintf( stderr,
1623                                 "schema_init: Error registering matching rule %s\n",
1624                                  mrule_defs[i].mrd_desc );
1625                         return LDAP_OTHER;
1626                 }
1627         }
1628         schema_init_done = 1;
1629         return LDAP_SUCCESS;
1630 }