]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_init.c
02beabc58c58af28e08edda62705077ba42ce016
[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
19 #define berValidate blobValidate
20 static int
21 blobValidate(
22         Syntax *syntax,
23         struct berval *in )
24 {
25         /* any value allowed */
26         return 0;
27 }
28
29 static int
30 UTF8StringValidate(
31         Syntax *syntax,
32         struct berval *in )
33 {
34         ber_len_t count;
35         int len;
36         unsigned char *u = in->bv_val;
37
38         for( count = in->bv_len; count > 0; count-=len, u+=len ) {
39                 /* get the length indicated by the first byte */
40                 len = LDAP_UTF8_CHARLEN( u );
41
42                 /* should not be zero */
43                 if( len == 0 ) return -1;
44
45                 /* make sure len corresponds with the offset
46                         to the next character */
47                 if( LDAP_UTF8_OFFSET( u ) != len ) return -1;
48         }
49
50         if( count != 0 ) return -1;
51
52         return 0;
53 }
54
55 static int
56 UTF8StringNormalize(
57         unsigned use,
58         Syntax *syntax,
59         MatchingRule *mr,
60         struct berval *val,
61         struct berval **normalized )
62 {
63         struct berval *newval;
64         char *p, *q, *s;
65
66         newval = ch_malloc( sizeof( struct berval ) );
67
68         p = val->bv_val;
69
70         /* Ignore initial whitespace */
71         while ( ldap_utf8_isspace( p ) ) {
72                 LDAP_UTF8_INCR( p );
73         }
74
75         if( *p ) {
76                 ch_free( newval );
77                 return 1;
78         }
79
80         newval->bv_val = ch_strdup( p );
81         p = q = newval->bv_val;
82         s = NULL;
83
84         while ( *p ) {
85                 int len;
86
87                 if ( ldap_utf8_isspace( p ) ) {
88                         len = LDAP_UTF8_COPY(q,p);
89                         s=q;
90                         p+=len;
91                         q+=len;
92
93                         /* Ignore the extra whitespace */
94                         while ( ldap_utf8_isspace( p ) ) {
95                                 LDAP_UTF8_INCR( p );
96                         }
97                 } else {
98                         len = LDAP_UTF8_COPY(q,p);
99                         s=NULL;
100                         p+=len;
101                         q+=len;
102                 }
103         }
104
105         assert( *newval->bv_val );
106         assert( newval->bv_val < p );
107         assert( p <= q );
108
109         /* cannot start with a space */
110         assert( !ldap_utf8_isspace(newval->bv_val) );
111
112         /*
113          * If the string ended in space, backup the pointer one
114          * position.  One is enough because the above loop collapsed
115          * all whitespace to a single space.
116          */
117
118         if ( s != NULL ) {
119                 q = s;
120         }
121
122         /* cannot end with a space */
123         assert( !ldap_utf8_isspace( LDAP_UTF8_PREV(q) ) );
124
125         /* null terminate */
126         *q = '\0';
127
128         newval->bv_len = q - newval->bv_val;
129         *normalized = newval;
130
131         return 0;
132 }
133
134 static int
135 oidValidate(
136         Syntax *syntax,
137         struct berval *val )
138 {
139         ber_len_t i;
140
141         if( val->bv_len == 0 ) return 0;
142
143         if( isdigit(val->bv_val[0]) ) {
144                 int dot = 0;
145                 for(i=1; i < val->bv_len; i++) {
146                         if( val->bv_val[i] == '.' ) {
147                                 if( dot++ ) return 1;
148                         } else if ( isdigit(val->bv_val[i]) ) {
149                                 dot = 0;
150                         } else {
151                                 return 1;
152                         }
153                 }
154
155                 return !dot ? 0 : 1;
156
157         } else if( isalpha(val->bv_val[0]) ) {
158                 for(i=1; i < val->bv_len; i++) {
159                         if( !isalpha(val->bv_val[i] ) ) {
160                                 return 1;
161                         }
162                 }
163
164                 return 0;
165         }
166         
167         return 1;
168 }
169
170 static int
171 integerValidate(
172         Syntax *syntax,
173         struct berval *val )
174 {
175         ber_len_t i;
176
177         for(i=0; i < val->bv_len; i++) {
178                 if( !isdigit(val->bv_val[i]) ) return -1;
179         }
180
181         return 0;
182 }
183
184 static int
185 printableStringValidate(
186         Syntax *syntax,
187         struct berval *val )
188 {
189         ber_len_t i;
190
191         for(i=0; i < val->bv_len; i++) {
192                 if( !isprint(val->bv_val[i]) ) return -1;
193         }
194
195         return 0;
196 }
197
198 static int
199 IA5StringValidate(
200         Syntax *syntax,
201         struct berval *val )
202 {
203         ber_len_t i;
204
205         for(i=0; i < val->bv_len; i++) {
206                 if( !isascii(val->bv_val[i]) ) return -1;
207         }
208
209         return 0;
210 }
211
212 static int
213 IA5StringConvert(
214         Syntax *syntax,
215         struct berval *in,
216         struct berval **out )
217 {
218         ldap_unicode_t *u;
219         ber_len_t i, len = in->bv_len;
220         struct berval *bv = ch_malloc( sizeof(struct berval) );
221
222         bv->bv_len = len * sizeof( ldap_unicode_t );
223         bv->bv_val = (char *) u = ch_malloc( bv->bv_len + sizeof( ldap_unicode_t ) );;
224
225         for(i=0; i < len; i++ ) {
226                 /*
227                  * IA5StringValidate should have been called to ensure
228                  * input is limited to IA5.
229                  */
230                 u[i] = in->bv_val[i];
231         }
232         u[i] = 0;
233
234         *out = bv;
235         return 0;
236 }
237
238 static int
239 IA5StringNormalize(
240         unsigned use,
241         Syntax *syntax,
242         MatchingRule *mr,
243         struct berval *val,
244         struct berval **normalized )
245 {
246         struct berval *newval;
247         char *p, *q;
248
249         newval = ch_malloc( sizeof( struct berval ) );
250
251         p = val->bv_val;
252
253         /* Ignore initial whitespace */
254         while ( isspace( *p++ ) ) {
255                 /* EMPTY */  ;
256         }
257
258         if( *p ) {
259                 ch_free( newval );
260                 return 1;
261         }
262
263         newval->bv_val = ch_strdup( p );
264         p = q = newval->bv_val;
265
266         while ( *p ) {
267                 if ( isspace( *p ) ) {
268                         *q++ = *p++;
269
270                         /* Ignore the extra whitespace */
271                         while ( isspace( *p++ ) ) {
272                                 /* EMPTY */  ;
273                         }
274                 } else {
275                         *q++ = *p++;
276                 }
277         }
278
279         assert( *newval->bv_val );
280         assert( newval->bv_val < p );
281         assert( p <= q );
282
283         /* cannot start with a space */
284         assert( !isspace(*newval->bv_val) );
285
286         /*
287          * If the string ended in space, backup the pointer one
288          * position.  One is enough because the above loop collapsed
289          * all whitespace to a single space.
290          */
291
292         if ( isspace( q[-1] ) ) {
293                 --q;
294         }
295
296         /* cannot end with a space */
297         assert( !isspace( q[-1] ) );
298
299         /* null terminate */
300         *q = '\0';
301
302         newval->bv_len = q - newval->bv_val;
303         *normalized = newval;
304
305         return 0;
306 }
307
308 static int
309 caseExactIA5Match(
310         unsigned use,
311         Syntax *syntax,
312         MatchingRule *mr,
313         struct berval *value,
314         void *assertedValue )
315 {
316         return strcmp( value->bv_val,
317                 ((struct berval *) assertedValue)->bv_val );
318 }
319
320 static int
321 caseIgnoreIA5Match(
322         unsigned use,
323         Syntax *syntax,
324         MatchingRule *mr,
325         struct berval *value,
326         void *assertedValue )
327 {
328         return strcasecmp( value->bv_val,
329                 ((struct berval *) assertedValue)->bv_val );
330 }
331
332 static int
333 objectClassMatch(
334         unsigned use,
335         Syntax *syntax,
336         MatchingRule *mr,
337         struct berval *value,
338         void *assertedValue )
339 {
340         ObjectClass *oc = oc_find( value->bv_val );
341         ObjectClass *asserted = oc_find( ((struct berval *) assertedValue)->bv_val );
342
343         return oc == NULL || oc != asserted;
344 }
345
346 struct syntax_defs_rec {
347         char *sd_desc;
348         int sd_flags;
349         slap_syntax_validate_func *sd_validate;
350         slap_syntax_transform_func *sd_ber2str;
351         slap_syntax_transform_func *sd_str2ber;
352 };
353
354 #define X_BINARY "X-BINARY-TRANSFER-REQUIRED 'TRUE' "
355 #define X_NOT_H_R "X-NOT-HUMAN-READABLE 'TRUE' "
356
357 struct syntax_defs_rec syntax_defs[] = {
358         {"( 1.3.6.1.4.1.1466.115.121.1.1 DESC 'ACI Item' " X_BINARY X_NOT_H_R ")",
359                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, NULL, NULL, NULL},
360         {"( 1.3.6.1.4.1.1466.115.121.1.2 DESC 'Access Point' " X_NOT_H_R ")",
361                 0, NULL, NULL, NULL},
362         {"( 1.3.6.1.4.1.1466.115.121.1.3 DESC 'Attribute Type Description' )",
363                 0, NULL, NULL, NULL},
364         {"( 1.3.6.1.4.1.1466.115.121.1.4 DESC 'Audio' " X_NOT_H_R ")",
365                 SLAP_SYNTAX_BLOB, blobValidate, NULL, NULL},
366         {"( 1.3.6.1.4.1.1466.115.121.1.5 DESC 'Binary' " X_BINARY X_NOT_H_R ")",
367                 SLAP_SYNTAX_BER, berValidate, NULL, NULL},
368         {"( 1.3.6.1.4.1.1466.115.121.1.6 DESC 'Bit String' )",
369                 0, NULL, NULL, NULL},
370         {"( 1.3.6.1.4.1.1466.115.121.1.7 DESC 'Boolean' )",
371                 0, NULL, NULL, NULL},
372         {"( 1.3.6.1.4.1.1466.115.121.1.8 DESC 'Certificate' "
373                 X_BINARY X_NOT_H_R ")",
374                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
375         {"( 1.3.6.1.4.1.1466.115.121.1.9 DESC 'Certificate List' "
376                 X_BINARY X_NOT_H_R ")",
377                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
378         {"( 1.3.6.1.4.1.1466.115.121.1.10 DESC 'Certificate Pair' "
379                 X_BINARY X_NOT_H_R ")",
380                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
381         {"( 1.3.6.1.4.1.1466.115.121.1.11 DESC 'Country String' )",
382                 0, NULL, NULL, NULL},
383         {"( 1.3.6.1.4.1.1466.115.121.1.12 DESC 'DN' )",
384                 0, blobValidate, NULL, NULL},
385         {"( 1.3.6.1.4.1.1466.115.121.1.13 DESC 'Data Quality' )",
386                 0, NULL, NULL, NULL},
387         {"( 1.3.6.1.4.1.1466.115.121.1.14 DESC 'Delivery Method' )",
388                 0, NULL, NULL, NULL},
389         {"( 1.3.6.1.4.1.1466.115.121.1.15 DESC 'Directory String' )",
390                 0, UTF8StringValidate, NULL, NULL},
391         {"( 1.3.6.1.4.1.1466.115.121.1.16 DESC 'DIT Content Rule Description' )",
392                 0, NULL, NULL, NULL},
393         {"( 1.3.6.1.4.1.1466.115.121.1.17 DESC 'DIT Structure Rule Description' )",
394                 0, NULL, NULL, NULL},
395         {"( 1.3.6.1.4.1.1466.115.121.1.19 DESC 'DSA Quality' )",
396                 0, NULL, NULL, NULL},
397         {"( 1.3.6.1.4.1.1466.115.121.1.20 DESC 'DSE Type' )",
398                 0, NULL, NULL, NULL},
399         {"( 1.3.6.1.4.1.1466.115.121.1.21 DESC 'Enhanced Guide' )",
400                 0, NULL, NULL, NULL},
401         {"( 1.3.6.1.4.1.1466.115.121.1.22 DESC 'Facsimile Telephone Number' )",
402                 0, blobValidate, NULL, NULL},
403         {"( 1.3.6.1.4.1.1466.115.121.1.23 DESC 'Fax' " X_NOT_H_R ")",
404                 SLAP_SYNTAX_BLOB, NULL, NULL, NULL},
405         {"( 1.3.6.1.4.1.1466.115.121.1.24 DESC 'Generalized Time' )",
406                 0, NULL, NULL, NULL},
407         {"( 1.3.6.1.4.1.1466.115.121.1.25 DESC 'Guide' )",
408                 0, NULL, NULL, NULL},
409         {"( 1.3.6.1.4.1.1466.115.121.1.26 DESC 'IA5 String' )",
410                 0, IA5StringValidate, NULL, NULL},
411         {"( 1.3.6.1.4.1.1466.115.121.1.27 DESC 'Integer' )",
412                 0, integerValidate, NULL, NULL},
413         {"( 1.3.6.1.4.1.1466.115.121.1.28 DESC 'JPEG' " X_NOT_H_R ")",
414                 SLAP_SYNTAX_BLOB, NULL, NULL, NULL},
415         {"( 1.3.6.1.4.1.1466.115.121.1.29 DESC 'Master And Shadow Access Points' )",
416                 0, NULL, NULL, NULL},
417         {"( 1.3.6.1.4.1.1466.115.121.1.30 DESC 'Matching Rule Description' )",
418                 0, NULL, NULL, NULL},
419         {"( 1.3.6.1.4.1.1466.115.121.1.31 DESC 'Matching Rule Use Description' )",
420                 0, NULL, NULL, NULL},
421         {"( 1.3.6.1.4.1.1466.115.121.1.32 DESC 'Mail Preference' )",
422                 0, NULL, NULL, NULL},
423         {"( 1.3.6.1.4.1.1466.115.121.1.33 DESC 'MHS OR Address' )",
424                 0, NULL, NULL, NULL},
425         {"( 1.3.6.1.4.1.1466.115.121.1.34 DESC 'Name And Optional UID' )",
426                 0, NULL, NULL, NULL},
427         {"( 1.3.6.1.4.1.1466.115.121.1.35 DESC 'Name Form Description' )",
428                 0, NULL, NULL, NULL},
429         {"( 1.3.6.1.4.1.1466.115.121.1.36 DESC 'Numeric String' )",
430                 0, NULL, NULL, NULL},
431         {"( 1.3.6.1.4.1.1466.115.121.1.37 DESC 'Object Class Description' )",
432                 0, NULL, NULL, NULL},
433         {"( 1.3.6.1.4.1.1466.115.121.1.38 DESC 'OID' )",
434                 0, oidValidate, NULL, NULL},
435         {"( 1.3.6.1.4.1.1466.115.121.1.39 DESC 'Other Mailbox' )",
436                 0, NULL, NULL, NULL},
437         {"( 1.3.6.1.4.1.1466.115.121.1.40 DESC 'Octet String' )",
438                 0, blobValidate, NULL, NULL},
439         {"( 1.3.6.1.4.1.1466.115.121.1.41 DESC 'Postal Address' )",
440                 0, blobValidate, NULL, NULL},
441         {"( 1.3.6.1.4.1.1466.115.121.1.42 DESC 'Protocol Information' )",
442                 0, NULL, NULL, NULL},
443         {"( 1.3.6.1.4.1.1466.115.121.1.43 DESC 'Presentation Address' )",
444                 0, NULL, NULL, NULL},
445         {"( 1.3.6.1.4.1.1466.115.121.1.44 DESC 'Printable String' )",
446                 0, printableStringValidate, NULL, NULL},
447         {"( 1.3.6.1.4.1.1466.115.121.1.49 DESC 'Supported Algorithm' "
448                 X_BINARY X_NOT_H_R ")",
449                 SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER, berValidate, NULL, NULL},
450         {"( 1.3.6.1.4.1.1466.115.121.1.50 DESC 'Telephone Number' )",
451                 0, blobValidate, NULL, NULL},
452         {"( 1.3.6.1.4.1.1466.115.121.1.51 DESC 'Teletex Terminal Identifier' )",
453                 0, NULL, NULL, NULL},
454         {"( 1.3.6.1.4.1.1466.115.121.1.52 DESC 'Telex Number' )",
455                 0, NULL, NULL, NULL},
456         {"( 1.3.6.1.4.1.1466.115.121.1.53 DESC 'UTC Time' )",
457                 0, NULL, NULL, NULL},
458         {"( 1.3.6.1.4.1.1466.115.121.1.54 DESC 'LDAP Syntax Description' )",
459                 0, NULL, NULL, NULL},
460         {"( 1.3.6.1.4.1.1466.115.121.1.55 DESC 'Modify Rights' )",
461                 0, NULL, NULL, NULL},
462         {"( 1.3.6.1.4.1.1466.115.121.1.56 DESC 'LDAP Schema Definition' )",
463                 0, NULL, NULL, NULL},
464         {"( 1.3.6.1.4.1.1466.115.121.1.57 DESC 'LDAP Schema Description' )",
465                 0, NULL, NULL, NULL},
466         {"( 1.3.6.1.4.1.1466.115.121.1.58 DESC 'Substring Assertion' )",
467                 0, NULL, NULL, NULL},
468
469         /* OpenLDAP Experimental Syntaxes */
470         {"( " SLAPD_OID_ACI_SYNTAX " DESC 'OpenLDAP Experimental ACI' )",
471                 0, NULL, NULL, NULL},
472
473         {NULL, 0, NULL, NULL, NULL}
474 };
475
476 struct mrule_defs_rec {
477         char *                                          mrd_desc;
478         unsigned                                        mrd_usage;
479         slap_mr_convert_func *          mrd_convert;
480         slap_mr_normalize_func *        mrd_normalize;
481         slap_mr_match_func *            mrd_match;
482         slap_mr_indexer_func *          mrd_indexer;
483         slap_mr_filter_func *           mrd_filter;
484 };
485
486 /*
487  * Other matching rules in X.520 that we do not use:
488  *
489  * 2.5.13.9             numericStringOrderingMatch
490  * 2.5.13.13    booleanMatch
491  * 2.5.13.15    integerOrderingMatch
492  * 2.5.13.18    octetStringOrderingMatch
493  * 2.5.13.19    octetStringSubstringsMatch
494  * 2.5.13.25    uTCTimeMatch
495  * 2.5.13.26    uTCTimeOrderingMatch
496  * 2.5.13.31    directoryStringFirstComponentMatch
497  * 2.5.13.32    wordMatch
498  * 2.5.13.33    keywordMatch
499  * 2.5.13.34    certificateExactMatch
500  * 2.5.13.35    certificateMatch
501  * 2.5.13.36    certificatePairExactMatch
502  * 2.5.13.37    certificatePairMatch
503  * 2.5.13.38    certificateListExactMatch
504  * 2.5.13.39    certificateListMatch
505  * 2.5.13.40    algorithmIdentifierMatch
506  * 2.5.13.41    storedPrefixMatch
507  * 2.5.13.42    attributeCertificateMatch
508  * 2.5.13.43    readerAndKeyIDMatch
509  * 2.5.13.44    attributeIntegrityMatch
510  */
511
512 /* recycled matching functions */
513 #define caseIgnoreMatch caseIgnoreIA5Match
514 #define caseExactMatch caseExactIA5Match
515
516 /* unimplemented matching functions */
517 #define objectIdentifierMatch NULL
518 #define distinguishedNameMatch NULL
519 #define caseIgnoreOrderingMatch NULL
520 #define caseIgnoreSubstringsMatch NULL
521 #define caseExactOrderingMatch NULL
522 #define caseExactSubstringsMatch NULL
523 #define numericStringMatch NULL
524 #define numericStringSubstringsMatch NULL
525 #define caseIgnoreListMatch NULL
526 #define caseIgnoreListSubstringsMatch NULL
527 #define integerMatch NULL
528 #define bitStringMatch NULL
529 #define octetStringMatch NULL
530 #define telephoneNumberMatch NULL
531 #define telephoneNumberSubstringsMatch NULL
532 #define presentationAddressMatch NULL
533 #define uniqueMemberMatch NULL
534 #define protocolInformationMatch NULL
535 #define generalizedTimeMatch NULL
536 #define generalizedTimeOrderingMatch NULL
537 #define integerFirstComponentMatch NULL
538 #define objectIdentifierFirstComponentMatch NULL
539 #define caseIgnoreIA5SubstringsMatch NULL
540
541 struct mrule_defs_rec mrule_defs[] = {
542         {"( 2.5.13.0 NAME 'objectIdentifierMatch' "
543                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
544                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
545                 NULL, NULL, objectIdentifierMatch, NULL, NULL},
546
547         {"( 2.5.13.1 NAME 'distinguishedNameMatch' "
548                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )",
549                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
550                 NULL, NULL, distinguishedNameMatch, NULL, NULL},
551
552         {"( 2.5.13.2 NAME 'caseIgnoreMatch' "
553                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
554                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
555                 NULL, UTF8StringNormalize, caseIgnoreMatch, NULL, NULL},
556
557         {"( 2.5.13.3 NAME 'caseIgnoreOrderingMatch' "
558                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
559                 SLAP_MR_ORDERING,
560                 NULL, UTF8StringNormalize, caseIgnoreOrderingMatch, NULL, NULL},
561
562         {"( 2.5.13.4 NAME 'caseIgnoreSubstringsMatch' "
563                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
564                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
565                 NULL, UTF8StringNormalize, caseIgnoreSubstringsMatch, NULL, NULL},
566
567         /* Next three are not in the RFC's, but are needed for compatibility */
568         {"( 2.5.13.5 NAME 'caseExactMatch' "
569                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
570                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
571                 NULL, UTF8StringNormalize, caseExactMatch, NULL, NULL},
572
573         {"( 2.5.13.6 NAME 'caseExactOrderingMatch' "
574                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
575                 SLAP_MR_ORDERING,
576                 NULL, UTF8StringNormalize, caseExactOrderingMatch, NULL, NULL},
577
578         {"( 2.5.13.7 NAME 'caseExactSubstringsMatch' "
579                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
580                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
581                 NULL, UTF8StringNormalize, caseExactSubstringsMatch, NULL, NULL},
582
583         {"( 2.5.13.8 NAME 'numericStringMatch' "
584                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.36 )",
585                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
586                 NULL, NULL, numericStringMatch, NULL, NULL},
587
588         {"( 2.5.13.10 NAME 'numericStringSubstringsMatch' "
589                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
590                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
591                 NULL, NULL, numericStringSubstringsMatch, NULL, NULL},
592
593         {"( 2.5.13.11 NAME 'caseIgnoreListMatch' "
594                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.41 )",
595                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
596                 NULL, NULL, caseIgnoreListMatch, NULL, NULL},
597
598         {"( 2.5.13.12 NAME 'caseIgnoreListSubstringsMatch' "
599                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
600                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
601                 NULL, NULL, caseIgnoreListSubstringsMatch, NULL, NULL},
602
603         {"( 2.5.13.14 NAME 'integerMatch' "
604                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
605                 SLAP_MR_NONE | SLAP_MR_EXT,
606                 NULL, NULL, integerMatch, NULL, NULL},
607
608         {"( 2.5.13.16 NAME 'bitStringMatch' "
609                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.6 )",
610                 SLAP_MR_NONE | SLAP_MR_EXT,
611                 NULL, NULL, bitStringMatch, NULL, NULL},
612
613         {"( 2.5.13.17 NAME 'octetStringMatch' "
614                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
615                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
616                 NULL, NULL, octetStringMatch, NULL, NULL},
617
618         {"( 2.5.13.20 NAME 'telephoneNumberMatch' "
619                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.50 )",
620                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
621                 NULL, NULL, telephoneNumberMatch, NULL, NULL},
622
623         {"( 2.5.13.21 NAME 'telephoneNumberSubstringsMatch' "
624                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
625                 SLAP_MR_SUBSTR | SLAP_MR_EXT,
626                 NULL, NULL, telephoneNumberSubstringsMatch, NULL, NULL},
627
628         {"( 2.5.13.22 NAME 'presentationAddressMatch' "
629                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.43 )",
630                 SLAP_MR_NONE | SLAP_MR_EXT,
631                 NULL, NULL, presentationAddressMatch, NULL, NULL},
632
633         {"( 2.5.13.23 NAME 'uniqueMemberMatch' "
634                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.34 )",
635                 SLAP_MR_NONE | SLAP_MR_EXT,
636                 NULL, NULL, uniqueMemberMatch, NULL, NULL},
637
638         {"( 2.5.13.24 NAME 'protocolInformationMatch' "
639                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.42 )",
640                 SLAP_MR_NONE | SLAP_MR_EXT,
641                 NULL, NULL, protocolInformationMatch, NULL, NULL},
642
643         {"( 2.5.13.27 NAME 'generalizedTimeMatch' "
644                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )",
645                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
646                 NULL, NULL, generalizedTimeMatch, NULL, NULL},
647
648         {"( 2.5.13.28 NAME 'generalizedTimeOrderingMatch' "
649                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )",
650                 SLAP_MR_ORDERING,
651                 NULL, NULL, generalizedTimeOrderingMatch, NULL, NULL},
652
653         {"( 2.5.13.29 NAME 'integerFirstComponentMatch' "
654                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )",
655                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
656                 NULL, NULL, integerFirstComponentMatch, NULL, NULL},
657
658         {"( 2.5.13.30 NAME 'objectIdentifierFirstComponentMatch' "
659                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
660                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
661                 NULL, NULL, objectIdentifierFirstComponentMatch, NULL, NULL},
662
663         {"( 1.3.6.1.4.1.1466.109.114.1 NAME 'caseExactIA5Match' "
664                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
665                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
666                 NULL, IA5StringNormalize, caseExactIA5Match, NULL, NULL},
667
668         {"( 1.3.6.1.4.1.1466.109.114.2 NAME 'caseIgnoreIA5Match' "
669                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
670                 SLAP_MR_EQUALITY | SLAP_MR_EXT,
671                 NULL, IA5StringNormalize, caseIgnoreIA5Match, NULL, NULL},
672
673         {"( 1.3.6.1.4.1.1466.109.114.3 NAME 'caseIgnoreIA5SubstringsMatch' "
674                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
675                 SLAP_MR_SUBSTR,
676                 NULL, IA5StringNormalize, caseIgnoreIA5SubstringsMatch, NULL, NULL},
677
678         {NULL, SLAP_MR_NONE, NULL, NULL, NULL}
679 };
680
681 static int schema_init_done = 0;
682
683 int
684 schema_init( void )
685 {
686         int             res;
687         int             i;
688
689         /* we should only be called once (from main) */
690         assert( schema_init_done == 0 );
691
692         for ( i=0; syntax_defs[i].sd_desc != NULL; i++ ) {
693                 res = register_syntax( syntax_defs[i].sd_desc,
694                     syntax_defs[i].sd_flags,
695                     syntax_defs[i].sd_validate,
696                     syntax_defs[i].sd_ber2str,
697                         syntax_defs[i].sd_str2ber );
698
699                 if ( res ) {
700                         fprintf( stderr, "schema_init: Error registering syntax %s\n",
701                                  syntax_defs[i].sd_desc );
702                         return -1;
703                 }
704         }
705
706         for ( i=0; mrule_defs[i].mrd_desc != NULL; i++ ) {
707                 if( mrule_defs[i].mrd_usage == SLAP_MR_NONE ) {
708                         fprintf( stderr,
709                                 "schema_init: Ingoring unusable matching rule %s\n",
710                                  mrule_defs[i].mrd_desc );
711                         continue;
712                 }
713
714                 res = register_matching_rule(
715                         mrule_defs[i].mrd_desc,
716                         mrule_defs[i].mrd_usage,
717                         mrule_defs[i].mrd_convert,
718                         mrule_defs[i].mrd_normalize,
719                     mrule_defs[i].mrd_match,
720                         mrule_defs[i].mrd_indexer,
721                         mrule_defs[i].mrd_filter );
722
723                 if ( res ) {
724                         fprintf( stderr,
725                                 "schema_init: Error registering matching rule %s\n",
726                                  mrule_defs[i].mrd_desc );
727                         return -1;
728                 }
729         }
730         schema_init_done = 1;
731         return( 0 );
732 }
733
734 #ifdef SLAPD_SCHEMA_NOT_COMPAT
735 struct slap_internal_schema slap_schema;
736
737 struct slap_schema_oc_map {
738         char *ssom_name;
739         size_t ssom_offset;
740 } oc_map[] = {
741         { "alias", offsetof(struct slap_internal_schema, si_oc_alias) },
742         { "referral", offsetof(struct slap_internal_schema, si_oc_referral) },
743         { NULL, 0 }
744 };
745
746 struct slap_schema_ad_map {
747         char *ssam_name;
748         slap_mr_match_func *ssam_match;
749         size_t ssam_offset;
750 } ad_map[] = {
751         { "objectClass", objectClassMatch,
752                 offsetof(struct slap_internal_schema, si_ad_objectClass) },
753
754         { "creatorsName", NULL,
755                 offsetof(struct slap_internal_schema, si_ad_creatorsName) },
756         { "createTimestamp", NULL,
757                 offsetof(struct slap_internal_schema, si_ad_createTimestamp) },
758         { "modifiersName", NULL,
759                 offsetof(struct slap_internal_schema, si_ad_modifiersName) },
760         { "modifyTimestamp", NULL,
761                 offsetof(struct slap_internal_schema, si_ad_modifyTimestamp) },
762
763         { "subschemaSubentry", NULL,
764                 offsetof(struct slap_internal_schema, si_ad_subschemaSubentry) },
765
766         { "namingContexts", NULL,
767                 offsetof(struct slap_internal_schema, si_ad_namingContexts) },
768         { "supportedControl", NULL,
769                 offsetof(struct slap_internal_schema, si_ad_supportedControl) },
770         { "supportedExtension", NULL,
771                 offsetof(struct slap_internal_schema, si_ad_supportedExtension) },
772         { "supportedLDAPVersion", NULL,
773                 offsetof(struct slap_internal_schema, si_ad_supportedLDAPVersion) },
774 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
775         { "supportedACIMechanisms", NULL,
776                 offsetof(struct slap_internal_schema, si_ad_supportedACIMechanisms) },
777
778 #endif
779         { "supportedSASLMechanisms", NULL,
780                 offsetof(struct slap_internal_schema, si_ad_supportedSASLMechanisms) },
781
782         { "attributeTypes", NULL,
783                 offsetof(struct slap_internal_schema, si_ad_attributeTypes) },
784         { "ldapSyntaxes", NULL,
785                 offsetof(struct slap_internal_schema, si_ad_ldapSyntaxes) },
786         { "matchingRules", NULL,
787                 offsetof(struct slap_internal_schema, si_ad_matchingRules) },
788         { "objectClasses", NULL,
789                 offsetof(struct slap_internal_schema, si_ad_objectClasses) },
790
791         { "ref", NULL,
792                 offsetof(struct slap_internal_schema, si_ad_ref) },
793
794         { "entry", NULL,
795                 offsetof(struct slap_internal_schema, si_ad_entry) },
796         { "children", NULL,
797                 offsetof(struct slap_internal_schema, si_ad_children) },
798
799         { "userPassword", NULL,
800                 offsetof(struct slap_internal_schema, si_ad_userPassword) },
801         { "authPassword", NULL,
802                 offsetof(struct slap_internal_schema, si_ad_authPassword) },
803 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
804         { "krbName", NULL,
805                 offsetof(struct slap_internal_schema, si_ad_krbName) },
806 #endif
807
808         { NULL, NULL, 0 }
809 };
810
811 #endif
812
813 int
814 schema_prep( void )
815 {
816 #ifdef SLAPD_SCHEMA_NOT_COMPAT
817         int i;
818 #endif
819         /* we should only be called once after schema_init() was called */
820         assert( schema_init_done == 1 );
821
822 #ifdef SLAPD_SCHEMA_NOT_COMPAT
823         for( i=0; oc_map[i].ssom_name; i++ ) {
824                 ObjectClass ** ocp = (ObjectClass **)
825                         &(((char *) &slap_schema)[oc_map[i].ssom_offset]);
826
827                 *ocp = oc_find( oc_map[i].ssom_name );
828
829                 if( *ocp == NULL ) {
830                         fprintf( stderr,
831                                 "No objectClass \"%s\" defined in schema\n",
832                                 oc_map[i].ssom_name );
833                         return LDAP_OBJECT_CLASS_VIOLATION;
834                 }
835         }
836
837         for( i=0; ad_map[i].ssam_name; i++ ) {
838                 int rc;
839                 const char *text;
840
841                 AttributeDescription ** adp = (AttributeDescription **)
842                         &(((char *) &slap_schema)[ad_map[i].ssam_offset]);
843
844                 *adp = NULL;
845
846                 rc = slap_str2ad( ad_map[i].ssam_name, adp, &text );
847
848                 if( rc != LDAP_SUCCESS ) {
849                         fprintf( stderr,
850                                 "No attribute \"%s\" defined in schema\n",
851                                 ad_map[i].ssam_name );
852                         return rc;
853                 }
854
855                 if( ad_map[i].ssam_match ) {
856                         /* install custom matching routine */
857                         (*adp)->ad_type->sat_equality->smr_match = ad_map[i].ssam_match;
858                 }
859         }
860 #endif
861
862         ++schema_init_done;
863         return 0;
864 }