]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_init.c
Update to latest devel codes
[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 #define faxNumberNormalize numericStringNormalize
1217 #define phoneNumberNormalize numericStringNormalize
1218 #define telexNumberNormalize numericStringNormalize
1219
1220 struct syntax_defs_rec syntax_defs[] = {
1221         {"( 1.3.6.1.4.1.1466.115.121.1.1 DESC 'ACI Item' " X_BINARY X_NOT_H_R ")",
1222                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, NULL, NULL, NULL},
1223         {"( 1.3.6.1.4.1.1466.115.121.1.2 DESC 'Access Point' " X_NOT_H_R ")",
1224                 0, NULL, NULL, NULL},
1225         {"( 1.3.6.1.4.1.1466.115.121.1.3 DESC 'Attribute Type Description' )",
1226                 0, NULL, NULL, NULL},
1227         {"( 1.3.6.1.4.1.1466.115.121.1.4 DESC 'Audio' " X_NOT_H_R ")",
1228                 SLAP_SYNTAX_BLOB, blobValidate, NULL, NULL},
1229         {"( 1.3.6.1.4.1.1466.115.121.1.5 DESC 'Binary' " X_BINARY X_NOT_H_R ")",
1230                 SLAP_SYNTAX_BER, berValidate, NULL, NULL},
1231         {"( 1.3.6.1.4.1.1466.115.121.1.6 DESC 'Bit String' )",
1232                 0, NULL, NULL, NULL},
1233         {"( 1.3.6.1.4.1.1466.115.121.1.7 DESC 'Boolean' )",
1234                 0, NULL, NULL, NULL},
1235         {"( 1.3.6.1.4.1.1466.115.121.1.8 DESC 'Certificate' "
1236                 X_BINARY X_NOT_H_R ")",
1237                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
1238         {"( 1.3.6.1.4.1.1466.115.121.1.9 DESC 'Certificate List' "
1239                 X_BINARY X_NOT_H_R ")",
1240                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
1241         {"( 1.3.6.1.4.1.1466.115.121.1.10 DESC 'Certificate Pair' "
1242                 X_BINARY X_NOT_H_R ")",
1243                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
1244         {"( 1.3.6.1.4.1.1466.115.121.1.11 DESC 'Country String' )",
1245                 0, NULL, NULL, NULL},
1246         {"( 1.3.6.1.4.1.1466.115.121.1.12 DESC 'Distinguished Name' )",
1247                 0, dnValidate, dnNormalize, NULL},
1248         {"( 1.3.6.1.4.1.1466.115.121.1.13 DESC 'Data Quality' )",
1249                 0, NULL, NULL, NULL},
1250         {"( 1.3.6.1.4.1.1466.115.121.1.14 DESC 'Delivery Method' )",
1251                 0, NULL, NULL, NULL},
1252         {"( 1.3.6.1.4.1.1466.115.121.1.15 DESC 'Directory String' )",
1253                 0, UTF8StringValidate, UTF8StringNormalize, NULL},
1254         {"( 1.3.6.1.4.1.1466.115.121.1.16 DESC 'DIT Content Rule Description' )",
1255                 0, NULL, NULL, NULL},
1256         {"( 1.3.6.1.4.1.1466.115.121.1.17 DESC 'DIT Structure Rule Description' )",
1257                 0, NULL, NULL, NULL},
1258         {"( 1.3.6.1.4.1.1466.115.121.1.19 DESC 'DSA Quality' )",
1259                 0, NULL, NULL, NULL},
1260         {"( 1.3.6.1.4.1.1466.115.121.1.20 DESC 'DSE Type' )",
1261                 0, NULL, NULL, NULL},
1262         {"( 1.3.6.1.4.1.1466.115.121.1.21 DESC 'Enhanced Guide' )",
1263                 0, NULL, NULL, NULL},
1264         {"( 1.3.6.1.4.1.1466.115.121.1.22 DESC 'Facsimile Telephone Number' )",
1265                 0, IA5StringValidate, faxNumberNormalize, NULL},
1266         {"( 1.3.6.1.4.1.1466.115.121.1.23 DESC 'Fax' " X_NOT_H_R ")",
1267                 SLAP_SYNTAX_BLOB, NULL, NULL, NULL},
1268         {"( 1.3.6.1.4.1.1466.115.121.1.24 DESC 'Generalized Time' )",
1269                 0, generalizedTimeValidate, generalizedTimeNormalize, NULL},
1270         {"( 1.3.6.1.4.1.1466.115.121.1.25 DESC 'Guide' )",
1271                 0, NULL, NULL, NULL},
1272         {"( 1.3.6.1.4.1.1466.115.121.1.26 DESC 'IA5 String' )",
1273                 0, IA5StringValidate, IA5StringNormalize, NULL},
1274         {"( 1.3.6.1.4.1.1466.115.121.1.27 DESC 'Integer' )",
1275                 0, integerValidate, NULL, NULL},
1276         {"( 1.3.6.1.4.1.1466.115.121.1.28 DESC 'JPEG' " X_NOT_H_R ")",
1277                 SLAP_SYNTAX_BLOB, NULL, NULL, NULL},
1278         {"( 1.3.6.1.4.1.1466.115.121.1.29 DESC 'Master And Shadow Access Points' )",
1279                 0, NULL, NULL, NULL},
1280         {"( 1.3.6.1.4.1.1466.115.121.1.30 DESC 'Matching Rule Description' )",
1281                 0, NULL, NULL, NULL},
1282         {"( 1.3.6.1.4.1.1466.115.121.1.31 DESC 'Matching Rule Use Description' )",
1283                 0, NULL, NULL, NULL},
1284         {"( 1.3.6.1.4.1.1466.115.121.1.32 DESC 'Mail Preference' )",
1285                 0, NULL, NULL, NULL},
1286         {"( 1.3.6.1.4.1.1466.115.121.1.33 DESC 'MHS OR Address' )",
1287                 0, NULL, NULL, NULL},
1288         {"( 1.3.6.1.4.1.1466.115.121.1.34 DESC 'Name And Optional UID' )",
1289                 0, NULL, NULL, NULL},
1290         {"( 1.3.6.1.4.1.1466.115.121.1.35 DESC 'Name Form Description' )",
1291                 0, NULL, NULL, NULL},
1292         {"( 1.3.6.1.4.1.1466.115.121.1.36 DESC 'Numeric String' )",
1293                 0, IA5StringValidate, numericStringNormalize, NULL},
1294         {"( 1.3.6.1.4.1.1466.115.121.1.37 DESC 'Object Class Description' )",
1295                 0, NULL, NULL, NULL},
1296         {"( 1.3.6.1.4.1.1466.115.121.1.38 DESC 'OID' )",
1297                 0, oidValidate, NULL, NULL},
1298         {"( 1.3.6.1.4.1.1466.115.121.1.39 DESC 'Other Mailbox' )",
1299                 0, NULL, NULL, NULL},
1300         {"( 1.3.6.1.4.1.1466.115.121.1.40 DESC 'Octet String' )",
1301                 0, blobValidate, NULL, NULL},
1302         {"( 1.3.6.1.4.1.1466.115.121.1.41 DESC 'Postal Address' )",
1303                 0, blobValidate, NULL, NULL},
1304         {"( 1.3.6.1.4.1.1466.115.121.1.42 DESC 'Protocol Information' )",
1305                 0, NULL, NULL, NULL},
1306         {"( 1.3.6.1.4.1.1466.115.121.1.43 DESC 'Presentation Address' )",
1307                 0, NULL, NULL, NULL},
1308         {"( 1.3.6.1.4.1.1466.115.121.1.44 DESC 'Printable String' )",
1309                 0, printableStringValidate, NULL, NULL},
1310         {"( 1.3.6.1.4.1.1466.115.121.1.49 DESC 'Supported Algorithm' "
1311                 X_BINARY X_NOT_H_R ")",
1312                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
1313         {"( 1.3.6.1.4.1.1466.115.121.1.50 DESC 'Telephone Number' )",
1314                 0, IA5StringValidate, phoneNumberNormalize, NULL},
1315         {"( 1.3.6.1.4.1.1466.115.121.1.51 DESC 'Teletex Terminal Identifier' )",
1316                 0, NULL, NULL, NULL},
1317         {"( 1.3.6.1.4.1.1466.115.121.1.52 DESC 'Telex Number' )",
1318                 0, IA5StringValidate, telexNumberNormalize, NULL},
1319         {"( 1.3.6.1.4.1.1466.115.121.1.53 DESC 'UTC Time' )",
1320                 0, utcTimeValidate, utcTimeNormalize, NULL},
1321         {"( 1.3.6.1.4.1.1466.115.121.1.54 DESC 'LDAP Syntax Description' )",
1322                 0, NULL, NULL, NULL},
1323         {"( 1.3.6.1.4.1.1466.115.121.1.55 DESC 'Modify Rights' )",
1324                 0, NULL, NULL, NULL},
1325         {"( 1.3.6.1.4.1.1466.115.121.1.56 DESC 'LDAP Schema Definition' )",
1326                 0, NULL, NULL, NULL},
1327         {"( 1.3.6.1.4.1.1466.115.121.1.57 DESC 'LDAP Schema Description' )",
1328                 0, NULL, NULL, NULL},
1329         {"( 1.3.6.1.4.1.1466.115.121.1.58 DESC 'Substring Assertion' )",
1330                 0, NULL, NULL, NULL},
1331
1332         /* OpenLDAP Experimental Syntaxes */
1333         {"( 1.3.6.1.4.1.4203.666.2.1 DESC 'OpenLDAP Experimental ACI' )",
1334                 0, IA5StringValidate /* THIS WILL CHANGE FOR NEW ACI SYNTAX */, NULL, NULL},
1335         {"( 1.3.6.1.4.1.4203.666.2.2 DESC 'OpenLDAP authPassword' )",
1336                 0, NULL, NULL, NULL},
1337         {"( 1.3.6.1.4.1.4203.666.2.3 DESC 'OpenLDAP void' " X_HIDE ")" ,
1338                 SLAP_SYNTAX_HIDE, inValidate, NULL, NULL},
1339 #if 0 /* not needed */
1340         {"( 1.3.6.1.4.1.4203.666.2.4 DESC 'OpenLDAP DN' " X_HIDE ")" ,
1341                 SLAP_SYNTAX_HIDE, inValidate, NULL, NULL},
1342 #endif
1343
1344         {NULL, 0, NULL, NULL, NULL}
1345 };
1346
1347 struct mrule_defs_rec {
1348         char *                                          mrd_desc;
1349         unsigned                                        mrd_usage;
1350         slap_mr_convert_func *          mrd_convert;
1351         slap_mr_normalize_func *        mrd_normalize;
1352         slap_mr_match_func *            mrd_match;
1353         slap_mr_indexer_func *          mrd_indexer;
1354         slap_mr_filter_func *           mrd_filter;
1355 };
1356
1357 /*
1358  * Other matching rules in X.520 that we do not use:
1359  *
1360  * 2.5.13.9             numericStringOrderingMatch
1361  * 2.5.13.13    booleanMatch
1362  * 2.5.13.15    integerOrderingMatch
1363  * 2.5.13.18    octetStringOrderingMatch
1364  * 2.5.13.19    octetStringSubstringsMatch
1365  * 2.5.13.25    uTCTimeMatch
1366  * 2.5.13.26    uTCTimeOrderingMatch
1367  * 2.5.13.31    directoryStringFirstComponentMatch
1368  * 2.5.13.32    wordMatch
1369  * 2.5.13.33    keywordMatch
1370  * 2.5.13.34    certificateExactMatch
1371  * 2.5.13.35    certificateMatch
1372  * 2.5.13.36    certificatePairExactMatch
1373  * 2.5.13.37    certificatePairMatch
1374  * 2.5.13.38    certificateListExactMatch
1375  * 2.5.13.39    certificateListMatch
1376  * 2.5.13.40    algorithmIdentifierMatch
1377  * 2.5.13.41    storedPrefixMatch
1378  * 2.5.13.42    attributeCertificateMatch
1379  * 2.5.13.43    readerAndKeyIDMatch
1380  * 2.5.13.44    attributeIntegrityMatch
1381  */
1382
1383
1384 /* recycled matching functions */
1385 #define caseIgnoreMatch caseIgnoreIA5Match
1386 #define caseIgnoreOrderingMatch caseIgnoreMatch
1387 #define caseIgnoreSubstringsMatch caseIgnoreIA5SubstringsMatch
1388 #define caseExactMatch caseExactIA5Match
1389 #define caseExactOrderingMatch caseExactMatch
1390 #define caseExactSubstringsMatch caseExactIA5SubstringsMatch
1391
1392 /* unimplemented matching functions */
1393 #define objectIdentifierMatch NULL
1394 #define caseIgnoreListMatch NULL
1395 #define caseIgnoreListSubstringsMatch NULL
1396 #define integerMatch NULL
1397 #define bitStringMatch NULL
1398 #define octetStringMatch NULL
1399 #define telephoneNumberMatch NULL
1400 #define telephoneNumberSubstringsMatch NULL
1401 #define presentationAddressMatch NULL
1402 #define uniqueMemberMatch NULL
1403 #define protocolInformationMatch NULL
1404 #define generalizedTimeMatch caseExactIA5Match
1405 #define generalizedTimeOrderingMatch caseExactIA5Match
1406 #define integerFirstComponentMatch NULL
1407 #define objectIdentifierFirstComponentMatch NULL
1408
1409 #define OpenLDAPaciMatch NULL
1410 #define authPasswordMatch NULL
1411
1412 /* unimplied indexer/filter routines */
1413 #define dnIndexer NULL
1414 #define dnFilter NULL
1415
1416 #define caseIgnoreIndexer               caseIgnoreIA5Indexer
1417 #define caseIgnoreFilter                caseIgnoreIA5Filter
1418 #define caseExactIndexer                caseExactIA5Indexer
1419 #define caseExactFilter                 caseExactIA5Filter
1420 #define caseExactIA5Indexer             caseIgnoreIA5Indexer
1421 #define caseExactIA5Filter              caseIgnoreIA5Filter
1422
1423 struct mrule_defs_rec mrule_defs[] = {
1424         {"( 2.5.13.0 NAME 'objectIdentifierMatch' "
1425                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
1426                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1427                 NULL, NULL, objectIdentifierMatch,
1428                 caseIgnoreIA5Indexer, caseIgnoreIA5Filter},
1429
1430         {"( 2.5.13.1 NAME 'distinguishedNameMatch' "
1431                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )",
1432                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1433                 NULL, NULL, dnMatch, dnIndexer, dnFilter},
1434
1435         {"( 2.5.13.2 NAME 'caseIgnoreMatch' "
1436                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1437                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1438                 NULL, NULL, caseIgnoreMatch, caseIgnoreIndexer, caseIgnoreFilter},
1439
1440         {"( 2.5.13.3 NAME 'caseIgnoreOrderingMatch' "
1441                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1442                 SLAP_MR_ORDERING,
1443                 NULL, NULL, caseIgnoreOrderingMatch, NULL, NULL},
1444
1445         {"( 2.5.13.4 NAME 'caseIgnoreSubstringsMatch' "
1446                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
1447                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
1448                 NULL, NULL, caseIgnoreSubstringsMatch, NULL, NULL},
1449
1450         /* Next three are not in the RFC's, but are needed for compatibility */
1451         {"( 2.5.13.5 NAME 'caseExactMatch' "
1452                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1453                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1454                 NULL, NULL, caseExactMatch, caseExactIndexer, caseExactFilter},
1455
1456         {"( 2.5.13.6 NAME 'caseExactOrderingMatch' "
1457                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1458                 SLAP_MR_ORDERING,
1459                 NULL, NULL, caseExactOrderingMatch, NULL, NULL},
1460
1461         {"( 2.5.13.7 NAME 'caseExactSubstringsMatch' "
1462                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
1463                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
1464                 NULL, NULL, caseExactSubstringsMatch, NULL, NULL},
1465
1466         {"( 2.5.13.8 NAME 'numericStringMatch' "
1467                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.36 )",
1468                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1469                 NULL, NULL, caseIgnoreIA5Match, NULL, NULL},
1470
1471         {"( 2.5.13.10 NAME 'numericStringSubstringsMatch' "
1472                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
1473                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
1474                 NULL, NULL, caseIgnoreIA5SubstringsMatch, NULL, NULL},
1475
1476         {"( 2.5.13.11 NAME 'caseIgnoreListMatch' "
1477                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.41 )",
1478                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1479                 NULL, NULL, caseIgnoreListMatch, NULL, NULL},
1480
1481         {"( 2.5.13.12 NAME 'caseIgnoreListSubstringsMatch' "
1482                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
1483                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
1484                 NULL, NULL, caseIgnoreListSubstringsMatch, NULL, NULL},
1485
1486         {"( 2.5.13.14 NAME 'integerMatch' "
1487                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
1488                 SLAP_MR_NONE | SLAP_MR_EXT,
1489                 NULL, NULL, integerMatch, NULL, NULL},
1490
1491         {"( 2.5.13.16 NAME 'bitStringMatch' "
1492                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.6 )",
1493                 SLAP_MR_NONE | SLAP_MR_EXT,
1494                 NULL, NULL, bitStringMatch, NULL, NULL},
1495
1496         {"( 2.5.13.17 NAME 'octetStringMatch' "
1497                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
1498                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1499                 NULL, NULL, octetStringMatch, octetStringIndexer, octetStringFilter},
1500
1501         {"( 2.5.13.20 NAME 'telephoneNumberMatch' "
1502                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.50 )",
1503                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1504                 NULL, NULL, telephoneNumberMatch, NULL, NULL},
1505
1506         {"( 2.5.13.21 NAME 'telephoneNumberSubstringsMatch' "
1507                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
1508                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
1509                 NULL, NULL, telephoneNumberSubstringsMatch, NULL, NULL},
1510
1511         {"( 2.5.13.22 NAME 'presentationAddressMatch' "
1512                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.43 )",
1513                 SLAP_MR_NONE | SLAP_MR_EXT,
1514                 NULL, NULL, presentationAddressMatch, NULL, NULL},
1515
1516         {"( 2.5.13.23 NAME 'uniqueMemberMatch' "
1517                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.34 )",
1518                 SLAP_MR_NONE | SLAP_MR_EXT,
1519                 NULL, NULL, uniqueMemberMatch, NULL, NULL},
1520
1521         {"( 2.5.13.24 NAME 'protocolInformationMatch' "
1522                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.42 )",
1523                 SLAP_MR_NONE | SLAP_MR_EXT,
1524                 NULL, NULL, protocolInformationMatch, NULL, NULL},
1525
1526         {"( 2.5.13.27 NAME 'generalizedTimeMatch' "
1527                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )",
1528                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1529                 NULL, NULL, generalizedTimeMatch, NULL, NULL},
1530
1531         {"( 2.5.13.28 NAME 'generalizedTimeOrderingMatch' "
1532                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )",
1533                 SLAP_MR_ORDERING,
1534                 NULL, NULL, generalizedTimeOrderingMatch, NULL, NULL},
1535
1536         {"( 2.5.13.29 NAME 'integerFirstComponentMatch' "
1537                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
1538                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1539                 NULL, NULL, integerFirstComponentMatch, NULL, NULL},
1540
1541         {"( 2.5.13.30 NAME 'objectIdentifierFirstComponentMatch' "
1542                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
1543                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1544                 NULL, NULL, objectIdentifierFirstComponentMatch, NULL, NULL},
1545
1546         {"( 1.3.6.1.4.1.1466.109.114.1 NAME 'caseExactIA5Match' "
1547                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
1548                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1549                 NULL, NULL, caseExactIA5Match, caseExactIA5Indexer, caseExactIA5Filter},
1550
1551         {"( 1.3.6.1.4.1.1466.109.114.2 NAME 'caseIgnoreIA5Match' "
1552                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
1553                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
1554                 NULL, NULL, caseIgnoreIA5Match, caseExactIA5Indexer, caseExactIA5Filter},
1555
1556         {"( 1.3.6.1.4.1.1466.109.114.3 NAME 'caseIgnoreIA5SubstringsMatch' "
1557                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
1558                 SLAP_MR_SUBSTR,
1559                 NULL, NULL, caseIgnoreIA5SubstringsMatch, NULL, NULL},
1560
1561         {"( 1.3.6.1.4.1.4203.666.4.1 NAME 'authPasswordMatch' "
1562                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
1563                 SLAP_MR_EQUALITY,
1564                 NULL, NULL, authPasswordMatch, NULL, NULL},
1565
1566         {"( 1.3.6.1.4.1.4203.666.4.2 NAME 'OpenLDAPaciMatch' "
1567                 "SYNTAX 1.3.6.1.4.1.4203.666.2.1 )",
1568                 SLAP_MR_EQUALITY,
1569                 NULL, NULL, OpenLDAPaciMatch, NULL, NULL},
1570
1571         {NULL, SLAP_MR_NONE, NULL, NULL, NULL}
1572 };
1573
1574 int
1575 schema_init( void )
1576 {
1577         int             res;
1578         int             i;
1579
1580         /* we should only be called once (from main) */
1581         assert( schema_init_done == 0 );
1582
1583         for ( i=0; syntax_defs[i].sd_desc != NULL; i++ ) {
1584                 res = register_syntax( syntax_defs[i].sd_desc,
1585                     syntax_defs[i].sd_flags,
1586                     syntax_defs[i].sd_validate,
1587                     syntax_defs[i].sd_normalize,
1588                         syntax_defs[i].sd_pretty
1589 #ifdef SLAPD_BINARY_CONVERSION
1590                         ,
1591                     syntax_defs[i].sd_ber2str,
1592                         syntax_defs[i].sd_str2ber
1593 #endif
1594                 );
1595
1596                 if ( res ) {
1597                         fprintf( stderr, "schema_init: Error registering syntax %s\n",
1598                                  syntax_defs[i].sd_desc );
1599                         return LDAP_OTHER;
1600                 }
1601         }
1602
1603         for ( i=0; mrule_defs[i].mrd_desc != NULL; i++ ) {
1604                 if( mrule_defs[i].mrd_usage == SLAP_MR_NONE ) {
1605                         fprintf( stderr,
1606                                 "schema_init: Ingoring unusable matching rule %s\n",
1607                                  mrule_defs[i].mrd_desc );
1608                         continue;
1609                 }
1610
1611                 res = register_matching_rule(
1612                         mrule_defs[i].mrd_desc,
1613                         mrule_defs[i].mrd_usage,
1614                         mrule_defs[i].mrd_convert,
1615                         mrule_defs[i].mrd_normalize,
1616                     mrule_defs[i].mrd_match,
1617                         mrule_defs[i].mrd_indexer,
1618                         mrule_defs[i].mrd_filter );
1619
1620                 if ( res ) {
1621                         fprintf( stderr,
1622                                 "schema_init: Error registering matching rule %s\n",
1623                                  mrule_defs[i].mrd_desc );
1624                         return LDAP_OTHER;
1625                 }
1626         }
1627         schema_init_done = 1;
1628         return LDAP_SUCCESS;
1629 }