]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_prep.c
now I remember why I introduced the 'has_ldapinfo_dn_ru' flag
[openldap] / servers / slapd / schema_prep.c
1 /* schema_prep.c - load builtin schema */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/ctype.h>
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "slap.h"
26 #include "ldap_pvt.h"
27 #include "ldap_pvt_uc.h"
28
29 #define OCDEBUG 0
30
31 int schema_init_done = 0;
32
33 struct slap_internal_schema slap_schema;
34
35 static int objectClassPretty(
36         struct slap_syntax *syntax,
37         struct berval * in,
38         struct berval * out,
39         void *ctx )
40 {
41         ObjectClass *oc = oc_bvfind( in );
42
43         if( oc != NULL ) {
44                 ber_dupbv_x( out, &oc->soc_cname, ctx );
45         } else {
46                 ber_dupbv_x( out, in, ctx );
47         }
48
49         return LDAP_SUCCESS;
50 }
51
52 static int
53 objectSubClassMatch(
54         int *matchp,
55         slap_mask_t flags,
56         Syntax *syntax,
57         MatchingRule *mr,
58         struct berval *value,
59         void *assertedValue )
60 {
61         struct berval *a = (struct berval *) assertedValue;
62         ObjectClass *oc = oc_bvfind( value );
63         ObjectClass *asserted = oc_bvfind( a );
64
65         if( asserted == NULL ) {
66                 if( OID_LEADCHAR( *a->bv_val ) ) {
67                         /* OID form, return FALSE */
68                         *matchp = 1;
69                         return LDAP_SUCCESS;
70                 }
71
72                 /* desc form, return undefined */
73                 return SLAPD_COMPARE_UNDEFINED;
74         }
75
76         if ( oc == NULL ) {
77                 /* unrecognized stored value */
78                 return SLAPD_COMPARE_UNDEFINED;
79         }
80
81         if( SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX( flags ) ) {
82                 *matchp = ( asserted != oc );
83         } else {
84                 *matchp = !is_object_subclass( asserted, oc );
85         }
86
87         return LDAP_SUCCESS;
88 }
89
90 static int objectSubClassIndexer( 
91         slap_mask_t use,
92         slap_mask_t mask,
93         struct slap_syntax *syntax,
94         struct slap_matching_rule *mr,
95         struct berval *prefix,
96         BerVarray values,
97         BerVarray *keysp,
98         void *ctx )
99 {
100         int rc, noc, i;
101         BerVarray ocvalues;
102         
103         for( noc=0; values[noc].bv_val != NULL; noc++ ) {
104                 /* just count em */;
105         }
106
107         /* over allocate */
108         ocvalues = sl_malloc( sizeof( struct berval ) * (noc+16), ctx );
109
110         /* copy listed values (and termination) */
111         for( i=0; i<noc; i++ ) {
112                 ObjectClass *oc = oc_bvfind( &values[i] );
113                 if( oc ) {
114                         ocvalues[i] = oc->soc_cname;
115                 } else {
116                         ocvalues[i] = values[i];
117                 }
118         }
119
120         ocvalues[i].bv_val = NULL;
121         ocvalues[i].bv_len = 0;
122
123         /* expand values */
124         for( i=0; i<noc; i++ ) {
125                 int j;
126                 ObjectClass *oc = oc_bvfind( &ocvalues[i] );
127                 if( oc == NULL || oc->soc_sups == NULL ) continue;
128                 
129                 for( j=0; oc->soc_sups[j] != NULL; j++ ) {
130                         int found = 0;
131                         ObjectClass *sup = oc->soc_sups[j];
132                         int k;
133
134                         for( k=0; k<noc; k++ ) {
135                                 if( bvmatch( &ocvalues[k], &sup->soc_cname ) ) {
136                                         found++;
137                                         break;
138                                 }
139                         }
140
141                         if( !found ) {
142                                 ocvalues = sl_realloc( ocvalues,
143                                         sizeof( struct berval ) * (noc+2), ctx );
144
145                                 assert( k == noc );
146
147                                 ocvalues[noc] = sup->soc_cname;
148
149                                 assert( ocvalues[noc].bv_val );
150                                 assert( ocvalues[noc].bv_len );
151
152                                 noc++;
153
154                                 ocvalues[noc].bv_len = 0;
155                                 ocvalues[noc].bv_val = NULL;
156                         }
157                 }
158         }
159
160         rc = octetStringIndexer( use, mask, syntax, mr,
161                 prefix, ocvalues, keysp, ctx );
162
163         sl_free( ocvalues, ctx );
164         return rc;
165 }
166
167 #define objectSubClassFilter octetStringFilter
168
169 static ObjectClassSchemaCheckFN rootDseObjectClass;
170 static ObjectClassSchemaCheckFN aliasObjectClass;
171 static ObjectClassSchemaCheckFN referralObjectClass;
172 static ObjectClassSchemaCheckFN subentryObjectClass;
173 static ObjectClassSchemaCheckFN dynamicObjectClass;
174
175 static struct slap_schema_oc_map {
176         char *ssom_name;
177         char *ssom_defn;
178         ObjectClassSchemaCheckFN *ssom_check;
179         slap_mask_t ssom_flags;
180         size_t ssom_offset;
181 } oc_map[] = {
182         { "top", "( 2.5.6.0 NAME 'top' "
183                         "DESC 'top of the superclass chain' "
184                         "ABSTRACT MUST objectClass )",
185                 0, 0, offsetof(struct slap_internal_schema, si_oc_top) },
186         { "extensibleObject", "( 1.3.6.1.4.1.1466.101.120.111 "
187                         "NAME 'extensibleObject' "
188                         "DESC 'RFC2252: extensible object' "
189                         "SUP top AUXILIARY )",
190                 0, SLAP_OC_OPERATIONAL,
191                 offsetof(struct slap_internal_schema, si_oc_extensibleObject) },
192         { "alias", "( 2.5.6.1 NAME 'alias' "
193                         "DESC 'RFC2256: an alias' "
194                         "SUP top STRUCTURAL "
195                         "MUST aliasedObjectName )",
196                 aliasObjectClass, SLAP_OC_ALIAS|SLAP_OC_OPERATIONAL,
197                 offsetof(struct slap_internal_schema, si_oc_alias) },
198         { "referral", "( 2.16.840.1.113730.3.2.6 NAME 'referral' "
199                         "DESC 'namedref: named subordinate referral' "
200                         "SUP top STRUCTURAL MUST ref )",
201                 referralObjectClass, SLAP_OC_REFERRAL|SLAP_OC_OPERATIONAL,
202                 offsetof(struct slap_internal_schema, si_oc_referral) },
203         { "LDAProotDSE", "( 1.3.6.1.4.1.4203.1.4.1 "
204                         "NAME ( 'OpenLDAProotDSE' 'LDAProotDSE' ) "
205                         "DESC 'OpenLDAP Root DSE object' "
206                         "SUP top STRUCTURAL MAY cn )",
207                 rootDseObjectClass, SLAP_OC_OPERATIONAL,
208                 offsetof(struct slap_internal_schema, si_oc_rootdse) },
209         { "subentry", "( 2.5.17.0 NAME 'subentry' "
210                         "SUP top STRUCTURAL "
211                         "MUST ( cn $ subtreeSpecification ) )",
212                 subentryObjectClass, SLAP_OC_SUBENTRY|SLAP_OC_OPERATIONAL,
213                 offsetof(struct slap_internal_schema, si_oc_subentry) },
214         { "subschema", "( 2.5.20.1 NAME 'subschema' "
215                 "DESC 'RFC2252: controlling subschema (sub)entry' "
216                 "AUXILIARY "
217                 "MAY ( dITStructureRules $ nameForms $ ditContentRules $ "
218                         "objectClasses $ attributeTypes $ matchingRules $ "
219                         "matchingRuleUse ) )",
220                 subentryObjectClass, SLAP_OC_OPERATIONAL,
221                 offsetof(struct slap_internal_schema, si_oc_subschema) },
222 #ifdef LDAP_DEVEL
223         { "collectiveAttributeSubentry", "( 2.5.17.2 "
224                         "NAME 'collectiveAttributeSubentry' "
225                         "AUXILIARY )",
226                 subentryObjectClass,
227                 SLAP_OC_COLLECTIVEATTRIBUTESUBENTRY|SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
228                 offsetof(struct slap_internal_schema, si_oc_collectiveAttributeSubentry) },
229         { "dynamicObject", "( 1.3.6.1.4.1.1466.101.119.2 "
230                         "NAME 'dynamicObject' "
231                         "DESC 'RFC2589: Dynamic Object' "
232                         "SUP top AUXILIARY )",
233                 dynamicObjectClass, SLAP_OC_DYNAMICOBJECT,
234                 offsetof(struct slap_internal_schema, si_oc_dynamicObject) },
235 #endif
236         { "glue", "( 1.3.6.1.4.1.4203.666.3.4 "
237                         "NAME 'glue' "
238                         "DESC 'Glue Entry' "
239                         "SUP top STRUCTURAL )",
240                 0, SLAP_OC_GLUE|SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
241                 offsetof(struct slap_internal_schema, si_oc_glue) },
242         { "syncConsumerSubentry", "( 1.3.6.1.4.1.4203.666.3.5 "
243                         "NAME 'syncConsumerSubentry' "
244                         "DESC 'Persistent Info for SyncRepl Consumer' "
245                         "AUXILIARY "
246                         "MAY syncreplCookie )",
247                 0, SLAP_OC_SYNCCONSUMERSUBENTRY|SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
248                 offsetof(struct slap_internal_schema, si_oc_syncConsumerSubentry) },
249         { "syncProviderSubentry", "( 1.3.6.1.4.1.4203.666.3.6 "
250                         "NAME 'syncProviderSubentry' "
251                         "DESC 'Persistent Info for SyncRepl Producer' "
252                         "AUXILIARY "
253                         "MAY contextCSN )",
254                 0, SLAP_OC_SYNCPROVIDERSUBENTRY|SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
255                 offsetof(struct slap_internal_schema, si_oc_syncProviderSubentry) },
256
257         { NULL, NULL, NULL, 0, 0 }
258 };
259
260 static AttributeTypeSchemaCheckFN rootDseAttribute;
261 static AttributeTypeSchemaCheckFN aliasAttribute;
262 static AttributeTypeSchemaCheckFN referralAttribute;
263 static AttributeTypeSchemaCheckFN subentryAttribute;
264 static AttributeTypeSchemaCheckFN administrativeRoleAttribute;
265 static AttributeTypeSchemaCheckFN dynamicAttribute;
266
267 static struct slap_schema_ad_map {
268         char *ssam_name;
269         char *ssam_defn;
270         AttributeTypeSchemaCheckFN *ssam_check;
271         slap_mask_t ssam_flags;
272         slap_syntax_validate_func *ssam_syn_validate;
273         slap_syntax_transform_func *ssam_syn_pretty;
274         slap_mr_convert_func *ssam_mr_convert;
275         slap_mr_normalize_func *ssam_mr_normalize;
276         slap_mr_match_func *ssam_mr_match;
277         slap_mr_indexer_func *ssam_mr_indexer;
278         slap_mr_filter_func *ssam_mr_filter;
279         size_t ssam_offset;
280 } ad_map[] = {
281         { "objectClass", "( 2.5.4.0 NAME 'objectClass' "
282                         "DESC 'RFC2256: object classes of the entity' "
283                         "EQUALITY objectIdentifierMatch "
284                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
285                 NULL, SLAP_AT_FINAL,
286                 NULL, objectClassPretty,
287                 NULL, NULL, objectSubClassMatch,
288                         objectSubClassIndexer, objectSubClassFilter,
289                 offsetof(struct slap_internal_schema, si_ad_objectClass) },
290
291         /* user entry operational attributes */
292         { "structuralObjectClass", "( 2.5.21.9 NAME 'structuralObjectClass' "
293                         "DESC 'X.500(93): structural object class of entry' "
294                         "EQUALITY objectIdentifierMatch "
295                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
296                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
297                 NULL, 0,
298                 NULL, objectClassPretty,
299                 NULL, NULL, objectSubClassMatch,
300                         objectSubClassIndexer, objectSubClassFilter,
301                 offsetof(struct slap_internal_schema, si_ad_structuralObjectClass) },
302         { "createTimestamp", "( 2.5.18.1 NAME 'createTimestamp' "
303                         "DESC 'RFC2252: time which object was created' "
304                         "EQUALITY generalizedTimeMatch "
305                         "ORDERING generalizedTimeOrderingMatch "
306                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
307                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
308                 NULL, 0,
309                 NULL, NULL,
310                 NULL, NULL, NULL, NULL, NULL,
311                 offsetof(struct slap_internal_schema, si_ad_createTimestamp) },
312         { "modifyTimestamp", "( 2.5.18.2 NAME 'modifyTimestamp' "
313                         "DESC 'RFC2252: time which object was last modified' "
314                         "EQUALITY generalizedTimeMatch "
315                         "ORDERING generalizedTimeOrderingMatch "
316                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
317                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
318                 NULL, 0,
319                 NULL, NULL,
320                 NULL, NULL, NULL, NULL, NULL,
321                 offsetof(struct slap_internal_schema, si_ad_modifyTimestamp) },
322         { "creatorsName", "( 2.5.18.3 NAME 'creatorsName' "
323                         "DESC 'RFC2252: name of creator' "
324                         "EQUALITY distinguishedNameMatch "
325                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
326                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
327                 NULL, 0,
328                 NULL, NULL,
329                 NULL, NULL, NULL, NULL, NULL,
330                 offsetof(struct slap_internal_schema, si_ad_creatorsName) },
331         { "modifiersName", "( 2.5.18.4 NAME 'modifiersName' "
332                         "DESC 'RFC2252: name of last modifier' "
333                         "EQUALITY distinguishedNameMatch "
334                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
335                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
336                 NULL, 0,
337                 NULL, NULL,
338                 NULL, NULL, NULL, NULL, NULL,
339                 offsetof(struct slap_internal_schema, si_ad_modifiersName) },
340         { "hasSubordinates", "( 2.5.18.9 NAME 'hasSubordinates' "
341                         "DESC 'X.501: entry has children' "
342                         "EQUALITY booleanMatch "
343                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
344                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
345                 NULL, 0,
346                 NULL, NULL,
347                 NULL, NULL, NULL, NULL, NULL,
348                 offsetof(struct slap_internal_schema, si_ad_hasSubordinates) },
349         { "subschemaSubentry", "( 2.5.18.10 NAME 'subschemaSubentry' "
350                         "DESC 'RFC2252: name of controlling subschema entry' "
351                         "EQUALITY distinguishedNameMatch "
352                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 SINGLE-VALUE "
353                         "NO-USER-MODIFICATION USAGE directoryOperation )",
354                 NULL, 0,
355                 NULL, NULL,
356                 NULL, NULL, NULL, NULL, NULL,
357                 offsetof(struct slap_internal_schema, si_ad_subschemaSubentry) },
358 #ifdef LDAP_DEVEL
359         { "collectiveAttributeSubentries", "( 2.5.18.12 "
360                         "NAME 'collectiveAttributeSubentries' "
361                         "EQUALITY distinguishedNameMatch "
362                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
363                         "NO-USER-MODIFICATION USAGE directoryOperation )",
364                 NULL, SLAP_AT_HIDE,
365                 NULL, NULL,
366                 NULL, NULL, NULL, NULL, NULL,
367                 offsetof(struct slap_internal_schema, si_ad_collectiveSubentries) },
368         { "collectiveExclusions", "( 2.5.18.7 NAME 'collectiveExclusions' "
369                         "EQUALITY objectIdentifierMatch "
370                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
371                         "USAGE directoryOperation )",
372                 NULL, SLAP_AT_HIDE,
373                 NULL, NULL,
374                 NULL, NULL, NULL, NULL, NULL,
375                 offsetof(struct slap_internal_schema, si_ad_collectiveExclusions) },
376 #endif
377
378         { "entryUUID", "( 1.3.6.1.4.1.4203.666.1.6 NAME 'entryUUID' "   
379                         "DESC 'UUID of the entry' "
380                         "EQUALITY UUIDMatch "
381                         "ORDERING UUIDOrderingMatch "
382                         "SYNTAX 1.3.6.1.4.1.4203.666.2.6 "
383                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
384                 NULL, SLAP_AT_HIDE,
385                 NULL, NULL,
386                 NULL, NULL, NULL, NULL, NULL,
387                 offsetof(struct slap_internal_schema, si_ad_entryUUID) },
388         { "entryCSN", "( 1.3.6.1.4.1.4203.666.1.7 NAME 'entryCSN' "
389                         "DESC 'change sequence number of the entry content' "
390                         "EQUALITY octetStringMatch "
391                         "ORDERING octetStringOrderingMatch "
392                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
393                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
394                 NULL, SLAP_AT_HIDE,
395                 NULL, NULL,
396                 NULL, NULL, NULL, NULL, NULL,
397                 offsetof(struct slap_internal_schema, si_ad_entryCSN) },
398         { "namingCSN", "( 1.3.6.1.4.1.4203.666.1.13 NAME 'namingCSN' "
399                         "DESC 'change sequence number of the entry naming (RDN)' "
400                         "EQUALITY octetStringMatch "
401                         "ORDERING octetStringOrderingMatch "
402                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
403                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
404                 NULL, SLAP_AT_HIDE,
405                 NULL, NULL,
406                 NULL, NULL, NULL, NULL, NULL,
407                 offsetof(struct slap_internal_schema, si_ad_namingCSN) },
408
409         { "superiorUUID", "( 1.3.6.1.4.1.4203.666.1.11 NAME 'superiorUUID' "   
410                         "DESC 'UUID of the superior entry' "
411                         "EQUALITY octetStringMatch "
412                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64} "
413                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
414                 NULL, SLAP_AT_HIDE,
415                 NULL, NULL,
416                 NULL, NULL, NULL, NULL, NULL,
417                 offsetof(struct slap_internal_schema, si_ad_superiorUUID) },
418
419         { "syncreplCookie", "( 1.3.6.1.4.1.4203.666.1.23 "
420                         "NAME 'syncreplCookie' "
421                         "DESC 'syncrepl Cookie for shadow copy' "
422                         "EQUALITY octetStringMatch "
423                         "ORDERING octetStringOrderingMatch "
424                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
425                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE dSAOperation )",
426                 NULL, SLAP_AT_HIDE,
427                 NULL, NULL,
428                 NULL, NULL, NULL, NULL, NULL,
429                 offsetof(struct slap_internal_schema, si_ad_syncreplCookie) },
430
431         { "contextCSN", "( 1.3.6.1.4.1.4203.666.1.25 "
432                         "NAME 'contextCSN' "
433                         "DESC 'the largest committed CSN of a context' "
434                         "EQUALITY octetStringMatch "
435                         "ORDERING octetStringOrderingMatch "
436                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
437                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE dSAOperation )",
438                 NULL, SLAP_AT_HIDE,
439                 NULL, NULL,
440                 NULL, NULL, NULL, NULL, NULL,
441                 offsetof(struct slap_internal_schema, si_ad_contextCSN) },
442
443 #ifdef LDAP_DEVEL
444         { "syncTimestamp", "( 1.3.6.1.4.1.4203.666.1.26 NAME 'syncTimestamp' "
445                         "DESC 'Time which object was replicated' "
446                         "EQUALITY generalizedTimeMatch "
447                         "ORDERING generalizedTimeOrderingMatch "
448                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
449                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE dSAOperation )",
450                 NULL, 0,
451                 NULL, NULL,
452                 NULL, NULL, NULL, NULL, NULL,
453                 offsetof(struct slap_internal_schema, si_ad_syncTimestamp) },
454 #endif
455
456         /* root DSE attributes */
457         { "altServer", "( 1.3.6.1.4.1.1466.101.120.6 NAME 'altServer' "
458                         "DESC 'RFC2252: alternative servers' "
459                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 USAGE dSAOperation )",
460                 rootDseAttribute, 0,
461                 NULL, NULL,
462                 NULL, NULL, NULL, NULL, NULL,
463                 offsetof(struct slap_internal_schema, si_ad_altServer) },
464         { "namingContexts", "( 1.3.6.1.4.1.1466.101.120.5 "
465                         "NAME 'namingContexts' "
466                         "DESC 'RFC2252: naming contexts' "
467                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 USAGE dSAOperation )",
468                 rootDseAttribute, 0,
469                 NULL, NULL,
470                 NULL, NULL, NULL, NULL, NULL,
471                 offsetof(struct slap_internal_schema, si_ad_namingContexts) },
472         { "supportedControl", "( 1.3.6.1.4.1.1466.101.120.13 "
473                         "NAME 'supportedControl' "
474                         "DESC 'RFC2252: supported controls' "
475                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 USAGE dSAOperation )",
476                 rootDseAttribute, 0,
477                 NULL, NULL,
478                 NULL, NULL, NULL, NULL, NULL,
479                 offsetof(struct slap_internal_schema, si_ad_supportedControl) },
480         { "supportedExtension", "( 1.3.6.1.4.1.1466.101.120.7 "
481                         "NAME 'supportedExtension' "
482                         "DESC 'RFC2252: supported extended operations' "
483                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 USAGE dSAOperation )",
484                 rootDseAttribute, 0,
485                 NULL, NULL,
486                 NULL, NULL, NULL, NULL, NULL,
487                 offsetof(struct slap_internal_schema, si_ad_supportedExtension) },
488         { "supportedLDAPVersion", "( 1.3.6.1.4.1.1466.101.120.15 "
489                         "NAME 'supportedLDAPVersion' "
490                         "DESC 'RFC2252: supported LDAP versions' "
491                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 USAGE dSAOperation )",
492                 rootDseAttribute, 0,
493                 NULL, NULL,
494                 NULL, NULL, NULL, NULL, NULL,
495                 offsetof(struct slap_internal_schema, si_ad_supportedLDAPVersion) },
496         { "supportedSASLMechanisms", "( 1.3.6.1.4.1.1466.101.120.14 "
497                         "NAME 'supportedSASLMechanisms' "
498                         "DESC 'RFC2252: supported SASL mechanisms'"
499                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 USAGE dSAOperation )",
500                 rootDseAttribute, 0,
501                 NULL, NULL,
502                 NULL, NULL, NULL, NULL, NULL,
503                 offsetof(struct slap_internal_schema, si_ad_supportedSASLMechanisms) },
504         { "supportedFeatures", "( 1.3.6.1.4.1.4203.1.3.5 "
505                         "NAME 'supportedFeatures' "
506                         "DESC 'features supported by the server' "
507                         "EQUALITY objectIdentifierMatch "
508                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
509                         "USAGE dSAOperation )",
510                 rootDseAttribute, 0,
511                 NULL, NULL,
512                 NULL, NULL, NULL, NULL, NULL,
513                 offsetof(struct slap_internal_schema, si_ad_supportedFeatures) },
514         { "monitorContext", "( 1.3.6.1.4.1.4203.666.1.10 "
515                         "NAME 'monitorContext' "
516                         "DESC 'monitor context' "
517                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
518                         "SINGLE-VALUE NO-USER-MODIFICATION "
519                         "USAGE dSAOperation )",
520                 rootDseAttribute, SLAP_AT_HIDE,
521                 NULL, NULL,
522                 NULL, NULL, NULL, NULL, NULL,
523                 offsetof(struct slap_internal_schema, si_ad_monitorContext) },
524         { "vendorName", "( 1.3.6.1.1.4 NAME 'vendorName' "
525                         "DESC 'RFC3045: name of implementation vendor' "
526                         "EQUALITY caseExactMatch "
527                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
528                         "SINGLE-VALUE NO-USER-MODIFICATION "
529                         "USAGE dSAOperation )",
530                 rootDseAttribute, 0,
531                 NULL, NULL,
532                 NULL, NULL, NULL, NULL, NULL,
533                 offsetof(struct slap_internal_schema, si_ad_vendorName) },
534         { "vendorVersion", "( 1.3.6.1.1.5 NAME 'vendorVersion' "
535                         "DESC 'RFC3045: version of implementation' "
536                         "EQUALITY caseExactMatch "
537                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
538                         "SINGLE-VALUE NO-USER-MODIFICATION "
539                         "USAGE dSAOperation )",
540                 rootDseAttribute, 0,
541                 NULL, NULL,
542                 NULL, NULL, NULL, NULL, NULL,
543                 offsetof(struct slap_internal_schema, si_ad_vendorVersion) },
544
545         /* subentry attributes */
546         { "administrativeRole", "( 2.5.18.5 NAME 'administrativeRole' "
547                         "EQUALITY objectIdentifierMatch "
548                         "USAGE directoryOperation "
549                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
550                 administrativeRoleAttribute, SLAP_AT_HIDE,
551                 NULL, NULL,
552                 NULL, NULL, NULL, NULL, NULL,
553                 offsetof(struct slap_internal_schema, si_ad_administrativeRole) },
554         { "subtreeSpecification", "( 2.5.18.6 NAME 'subtreeSpecification' "
555                         "SINGLE-VALUE "
556                         "USAGE directoryOperation "
557                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.45 )",
558                 subentryAttribute, SLAP_AT_HIDE,
559                 NULL, NULL,
560                 NULL, NULL, NULL, NULL, NULL,
561                 offsetof(struct slap_internal_schema, si_ad_subtreeSpecification) },
562
563         /* subschema subentry attributes */
564         { "ditStructureRules", "( 2.5.21.1 NAME 'dITStructureRules' "
565                         "DESC 'RFC2252: DIT structure rules' "
566                         "EQUALITY integerFirstComponentMatch "
567                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.17 "
568                         "USAGE directoryOperation ) ",
569                 subentryAttribute, SLAP_AT_HIDE,
570                 NULL, NULL,
571                 NULL, NULL, NULL, NULL, NULL,
572                 offsetof(struct slap_internal_schema, si_ad_ditStructureRules) },
573         { "ditContentRules", "( 2.5.21.2 NAME 'dITContentRules' "
574                         "DESC 'RFC2252: DIT content rules' "
575                         "EQUALITY objectIdentifierFirstComponentMatch "
576                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.16 USAGE directoryOperation )",
577                 subentryAttribute, SLAP_AT_HIDE,
578                 NULL, NULL,
579                 NULL, NULL, NULL, NULL, NULL,
580                 offsetof(struct slap_internal_schema, si_ad_ditContentRules) },
581         { "matchingRules", "( 2.5.21.4 NAME 'matchingRules' "
582                         "DESC 'RFC2252: matching rules' "
583                         "EQUALITY objectIdentifierFirstComponentMatch "
584                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.30 USAGE directoryOperation )",
585                 subentryAttribute, 0,
586                 NULL, NULL,
587                 NULL, NULL, NULL, NULL, NULL,
588                 offsetof(struct slap_internal_schema, si_ad_matchingRules) },
589         { "attributeTypes", "( 2.5.21.5 NAME 'attributeTypes' "
590                         "DESC 'RFC2252: attribute types' "
591                         "EQUALITY objectIdentifierFirstComponentMatch "
592                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.3 USAGE directoryOperation )",
593                 subentryAttribute, 0,
594                 NULL, NULL,
595                 NULL, NULL, NULL, NULL, NULL,
596                 offsetof(struct slap_internal_schema, si_ad_attributeTypes) },
597         { "objectClasses", "( 2.5.21.6 NAME 'objectClasses' "
598                         "DESC 'RFC2252: object classes' "
599                         "EQUALITY objectIdentifierFirstComponentMatch "
600                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.37 USAGE directoryOperation )",
601                 subentryAttribute, 0,
602                 NULL, NULL,
603                 NULL, NULL, NULL, NULL, NULL,
604                 offsetof(struct slap_internal_schema, si_ad_objectClasses) },
605         { "nameForms", "( 2.5.21.7 NAME 'nameForms' "
606                         "DESC 'RFC2252: name forms ' "
607                         "EQUALITY objectIdentifierFirstComponentMatch "
608                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.35 USAGE directoryOperation )",
609                 subentryAttribute, SLAP_AT_HIDE,
610                 NULL, NULL,
611                 NULL, NULL, NULL, NULL, NULL,
612                 offsetof(struct slap_internal_schema, si_ad_nameForms) },
613         { "matchingRuleUse", "( 2.5.21.8 NAME 'matchingRuleUse' "
614                         "DESC 'RFC2252: matching rule uses' "
615                         "EQUALITY objectIdentifierFirstComponentMatch "
616                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.31 USAGE directoryOperation )",
617                 subentryAttribute, 0,
618                 NULL, NULL,
619                 NULL, NULL, NULL, NULL, NULL,
620                 offsetof(struct slap_internal_schema, si_ad_matchingRuleUse) },
621
622         { "ldapSyntaxes", "( 1.3.6.1.4.1.1466.101.120.16 NAME 'ldapSyntaxes' "
623                         "DESC 'RFC2252: LDAP syntaxes' "
624                         "EQUALITY objectIdentifierFirstComponentMatch "
625                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.54 USAGE directoryOperation )",
626                 subentryAttribute, 0,
627                 NULL, NULL,
628                 NULL, NULL, NULL, NULL, NULL,
629                 offsetof(struct slap_internal_schema, si_ad_ldapSyntaxes) },
630
631         /* knowledge information */
632         { "aliasedObjectName", "( 2.5.4.1 "
633                         "NAME ( 'aliasedObjectName' 'aliasedEntryName' ) "
634                         "DESC 'RFC2256: name of aliased object' "
635                         "EQUALITY distinguishedNameMatch "
636                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 SINGLE-VALUE )",
637                 aliasAttribute, SLAP_AT_FINAL,
638                 NULL, NULL,
639                 NULL, NULL, NULL, NULL, NULL,
640                 offsetof(struct slap_internal_schema, si_ad_aliasedObjectName) },
641         { "ref", "( 2.16.840.1.113730.3.1.34 NAME 'ref' "
642                         "DESC 'namedref: subordinate referral URL' "
643                         "EQUALITY caseExactMatch "
644                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
645                         "USAGE distributedOperation )",
646                 referralAttribute, 0,
647                 NULL, NULL,
648                 NULL, NULL, NULL, NULL, NULL,
649                 offsetof(struct slap_internal_schema, si_ad_ref) },
650
651         /* access control internals */
652         { "entry", "( 1.3.6.1.4.1.4203.1.3.1 "
653                         "NAME 'entry' "
654                         "DESC 'OpenLDAP ACL entry pseudo-attribute' "
655                         "SYNTAX 1.3.6.1.4.1.4203.1.1.1 "
656                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE dSAOperation )",
657                 NULL, SLAP_AT_HIDE,
658                 NULL, NULL,
659                 NULL, NULL, NULL, NULL, NULL,
660                 offsetof(struct slap_internal_schema, si_ad_entry) },
661         { "children", "( 1.3.6.1.4.1.4203.1.3.2 "
662                         "NAME 'children' "
663                         "DESC 'OpenLDAP ACL children pseudo-attribute' "
664                         "SYNTAX 1.3.6.1.4.1.4203.1.1.1 "
665                         "SINGLE-VALUE NO-USER-MODIFICATION USAGE dSAOperation )",
666                 NULL, SLAP_AT_HIDE,
667                 NULL, NULL,
668                 NULL, NULL, NULL, NULL, NULL,
669                 offsetof(struct slap_internal_schema, si_ad_children) },
670         { "saslAuthzTo", "( 1.3.6.1.4.1.4203.666.1.8 "
671                         "NAME 'saslAuthzTo' "
672                         "DESC 'SASL proxy authorization targets' "
673                         "EQUALITY caseExactMatch "
674                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
675                         "USAGE distributedOperation )",
676                 NULL, SLAP_AT_HIDE,
677                 NULL, NULL,
678                 NULL, NULL, NULL, NULL, NULL,
679                 offsetof(struct slap_internal_schema, si_ad_saslAuthzTo) },
680         { "saslAuthzFrom", "( 1.3.6.1.4.1.4203.666.1.9 "
681                         "NAME 'saslAuthzFrom' "
682                         "DESC 'SASL proxy authorization sources' "
683                         "EQUALITY caseExactMatch "
684                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
685                         "USAGE distributedOperation )",
686                 NULL, SLAP_AT_HIDE,
687                 NULL, NULL,
688                 NULL, NULL, NULL, NULL, NULL,
689                 offsetof(struct slap_internal_schema, si_ad_saslAuthzFrom) },
690 #ifdef SLAPD_ACI_ENABLED
691         { "OpenLDAPaci", "( 1.3.6.1.4.1.4203.666.1.5 "
692                         "NAME 'OpenLDAPaci' "
693                         "DESC 'OpenLDAP access control information (experimental)' "
694                         "EQUALITY OpenLDAPaciMatch "
695                         "SYNTAX 1.3.6.1.4.1.4203.666.2.1 "
696                         "USAGE directoryOperation )",
697                 NULL, SLAP_AT_HIDE,
698                 NULL, NULL,
699                 NULL, NULL, NULL, NULL, NULL,
700                 offsetof(struct slap_internal_schema, si_ad_aci) },
701 #endif
702
703 #ifdef LDAP_DEVEL
704         { "entryTtl", "( 1.3.6.1.4.1.1466.101.119.3 NAME 'entryTtl' "
705                         "DESC 'RFC2589: entry time-to-live' "
706                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 SINGLE-VALUE "
707                         "NO-USER-MODIFICATION USAGE dSAOperation )",
708                 dynamicAttribute, 0,
709                 NULL, NULL,
710                 NULL, NULL, NULL, NULL, NULL,
711                 offsetof(struct slap_internal_schema, si_ad_entryTtl) },
712         { "dynamicSubtrees", "( 1.3.6.1.4.1.1466.101.119.4 "
713                         "NAME 'dynamicSubtrees' "
714                         "DESC 'RFC2589: dynamic subtrees' "
715                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 NO-USER-MODIFICATION "
716                         "USAGE dSAOperation )",
717                 rootDseAttribute, 0,
718                 NULL, NULL,
719                 NULL, NULL, NULL, NULL, NULL,
720                 offsetof(struct slap_internal_schema, si_ad_dynamicSubtrees) },
721 #endif
722
723         /* userApplication attributes (which system schema depends upon) */
724         { "distinguishedName", "( 2.5.4.49 NAME 'distinguishedName' "
725                         "DESC 'RFC2256: common supertype of DN attributes' "
726                         "EQUALITY distinguishedNameMatch "
727                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )",
728                 NULL, SLAP_AT_ABSTRACT,
729                 NULL, NULL,
730                 NULL, NULL, NULL, NULL, NULL,
731                 offsetof(struct slap_internal_schema, si_ad_distinguishedName) },
732         { "name", "( 2.5.4.41 NAME 'name' "
733                         "DESC 'RFC2256: common supertype of name attributes' "
734                         "EQUALITY caseIgnoreMatch "
735                         "SUBSTR caseIgnoreSubstringsMatch "
736                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} )",
737                 NULL, SLAP_AT_ABSTRACT,
738                 NULL, NULL,
739                 NULL, NULL, NULL, NULL, NULL,
740                 offsetof(struct slap_internal_schema, si_ad_name) },
741         { "cn", "( 2.5.4.3 NAME ( 'cn' 'commonName' ) "
742                         "DESC 'RFC2256: common name(s) for which the entity is known by' "
743                         "SUP name )",
744                 NULL, 0,
745                 NULL, NULL,
746                 NULL, NULL, NULL, NULL, NULL,
747                 offsetof(struct slap_internal_schema, si_ad_cn) },
748         { "userPassword", "( 2.5.4.35 NAME 'userPassword' "
749                         "DESC 'RFC2256/2307: password of user' "
750                         "EQUALITY octetStringMatch "
751                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{128} )",
752                 NULL, 0,
753                 NULL, NULL,
754                 NULL, NULL, NULL, NULL, NULL,
755                 offsetof(struct slap_internal_schema, si_ad_userPassword) },
756
757         { "labeledURI", "(  1.3.6.1.4.1.250.1.57 NAME 'labeledURI' "
758                         "DESC 'RFC2079: Uniform Resource Identifier with optional label' "
759                         "EQUALITY caseExactMatch "
760                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
761                 NULL, 0,
762                 NULL, NULL,
763                 NULL, NULL, NULL, NULL, NULL,
764                 offsetof(struct slap_internal_schema, si_ad_labeledURI) },
765
766 #ifdef SLAPD_AUTHPASSWD
767         { "authPassword", "( 1.3.6.1.4.1.4203.1.3.4 "
768                         "NAME 'authPassword' "
769                         "DESC 'RFC3112: authentication password attribute' "
770                         "EQUALITY 1.3.6.1.4.1.4203.1.2.2 "
771                         "SYNTAX 1.3.6.1.4.1.4203.1.1.2 )",
772                 NULL, 0,
773                 NULL, NULL,
774                 NULL, NULL, NULL, NULL, NULL,
775                 offsetof(struct slap_internal_schema, si_ad_authPassword) },
776         { "supportedAuthPasswordSchemes", "( 1.3.6.1.4.1.4203.1.3.3 "
777                         "NAME 'supportedAuthPasswordSchemes' "
778                         "DESC 'RFC3112: supported authPassword schemes' "
779                         "EQUALITY caseExactIA5Match "
780                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26{32} "
781                         "USAGE dSAOperation )",
782                 subschemaAttribute, 0,
783                 NULL, NULL,
784                 NULL, NULL, NULL, NULL, NULL,
785                 offsetof(struct slap_internal_schema, si_ad_authPassword) },
786 #endif
787 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
788         { "krbName", "( 1.3.6.1.4.1.250.1.32 "
789                         "NAME ( 'krbName' 'kerberosName' ) "
790                         "DESC 'Kerberos principal associated with object' "
791                         "EQUALITY caseIgnoreIA5Match "
792                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 "
793                         "SINGLE-VALUE )",
794                 NULL, 0,
795                 NULL, NULL,
796                 NULL, NULL, NULL, NULL, NULL,
797                 offsetof(struct slap_internal_schema, si_ad_krbName) },
798 #endif
799         { NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0 }
800 };
801
802 static AttributeType slap_at_undefined = {
803         { "1.1.1", NULL, NULL, 1, NULL,
804                 NULL, NULL, NULL, NULL,
805                 0, 0, 0, 1, 3, NULL }, /* LDAPAttributeType */
806         { sizeof("UNDEFINED")-1, "UNDEFINED" }, /* cname */
807         NULL, /* sup */
808         NULL, /* subtypes */
809         NULL, NULL, NULL, NULL, /* matching rules routines */
810         NULL, /* syntax (this may need to be defined) */
811         (AttributeTypeSchemaCheckFN *) 0, /* schema check function */
812         SLAP_AT_ABSTRACT|SLAP_AT_FINAL, /* mask */
813         { NULL }, /* next */
814         NULL /* attribute description */
815         /* mutex (don't know how to initialize it :) */
816 };
817
818 static struct slap_schema_mr_map {
819         char *ssmm_name;
820         size_t ssmm_offset;
821 } mr_map[] = {
822         { "caseExactIA5Match",
823                 offsetof(struct slap_internal_schema, si_mr_caseExactIA5Match) },
824         { "caseExactMatch",
825                 offsetof(struct slap_internal_schema, si_mr_caseExactMatch) },
826         { "caseExactSubstringsMatch",
827                 offsetof(struct slap_internal_schema, si_mr_caseExactSubstringsMatch) },
828         { "distinguishedNameMatch",
829                 offsetof(struct slap_internal_schema, si_mr_distinguishedNameMatch) },
830         { "integerMatch",
831                 offsetof(struct slap_internal_schema, si_mr_integerMatch) },
832         { "integerFirstComponentMatch",
833                 offsetof(struct slap_internal_schema,
834                         si_mr_integerFirstComponentMatch) },
835         { "objectIdentifierFirstComponentMatch",
836                 offsetof(struct slap_internal_schema,
837                         si_mr_objectIdentifierFirstComponentMatch) },
838         { NULL, 0 }
839 };
840
841 static struct slap_schema_syn_map {
842         char *sssm_name;
843         size_t sssm_offset;
844 } syn_map[] = {
845         { "1.3.6.1.4.1.1466.115.121.1.15",
846                 offsetof(struct slap_internal_schema, si_syn_directoryString) },
847         { "1.3.6.1.4.1.1466.115.121.1.12",
848                 offsetof(struct slap_internal_schema, si_syn_distinguishedName) },
849         { "1.3.6.1.4.1.1466.115.121.1.27",
850                 offsetof(struct slap_internal_schema, si_syn_integer) },
851         { "1.3.6.1.4.1.1466.115.121.1.40",
852                 offsetof(struct slap_internal_schema, si_syn_octetString) },
853         { NULL, 0 }
854 };
855
856 int
857 slap_schema_load( void )
858 {
859         int i;
860
861         for( i=0; syn_map[i].sssm_name; i++ ) {
862                 Syntax ** synp = (Syntax **)
863                         &(((char *) &slap_schema)[syn_map[i].sssm_offset]);
864
865                 assert( *synp == NULL );
866
867                 *synp = syn_find( syn_map[i].sssm_name );
868
869                 if( *synp == NULL ) {
870                         fprintf( stderr, "slap_schema_load: Syntax: "
871                                 "No syntax \"%s\" defined in schema\n",
872                                 syn_map[i].sssm_name );
873                         return LDAP_INVALID_SYNTAX;
874                 }
875         }
876
877         for( i=0; mr_map[i].ssmm_name; i++ ) {
878                 MatchingRule ** mrp = (MatchingRule **)
879                         &(((char *) &slap_schema)[mr_map[i].ssmm_offset]);
880
881                 assert( *mrp == NULL );
882
883                 *mrp = mr_find( mr_map[i].ssmm_name );
884
885                 if( *mrp == NULL ) {
886                         fprintf( stderr, "slap_schema_load: MatchingRule: "
887                                 "No matching rule \"%s\" defined in schema\n",
888                                 mr_map[i].ssmm_name );
889                         return LDAP_INAPPROPRIATE_MATCHING;
890                 }
891         }
892
893         for( i=0; ad_map[i].ssam_name; i++ ) {
894                 assert( ad_map[i].ssam_defn != NULL );
895                 {
896                         LDAPAttributeType *at;
897                         int             code;
898                         const char      *err;
899
900                         at = ldap_str2attributetype( ad_map[i].ssam_defn,
901                                 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
902                         if ( !at ) {
903                                 fprintf( stderr,
904                                         "slap_schema_load: AttributeType \"%s\": %s before %s\n",
905                                          ad_map[i].ssam_name, ldap_scherr2str(code), err );
906                                 return code;
907                         }
908
909                         if ( at->at_oid == NULL ) {
910                                 fprintf( stderr, "slap_schema_load: "
911                                         "AttributeType \"%s\": no OID\n",
912                                         ad_map[i].ssam_name );
913                                 return LDAP_OTHER;
914                         }
915
916                         code = at_add( at, &err );
917                         if ( code ) {
918                                 fprintf( stderr, "slap_schema_load: AttributeType "
919                                         "\"%s\": %s: \"%s\"\n",
920                                          ad_map[i].ssam_name, scherr2str(code), err );
921                                 return code;
922                         }
923                         ldap_memfree( at );
924                 }
925                 {
926                         int rc;
927                         const char *text;
928                         Syntax *syntax = NULL;
929
930                         AttributeDescription ** adp = (AttributeDescription **)
931                                 &(((char *) &slap_schema)[ad_map[i].ssam_offset]);
932
933                         assert( *adp == NULL );
934
935                         rc = slap_str2ad( ad_map[i].ssam_name, adp, &text );
936                         if( rc != LDAP_SUCCESS ) {
937                                 fprintf( stderr, "slap_schema_load: AttributeType \"%s\": "
938                                         "not defined in schema\n",
939                                         ad_map[i].ssam_name );
940                                 return rc;
941                         }
942
943                         if( ad_map[i].ssam_check ) {
944                                 /* install check routine */
945                                 (*adp)->ad_type->sat_check = ad_map[i].ssam_check;
946                         }
947                         /* install flags */
948                         (*adp)->ad_type->sat_flags |= ad_map[i].ssam_flags;
949
950                         /* install custom syntax routines */
951                         if( ad_map[i].ssam_syn_validate ||
952                                 ad_map[i].ssam_syn_pretty )
953                         {
954                                 Syntax *syn;
955
956                                 syntax = (*adp)->ad_type->sat_syntax;
957
958                                 syn = ch_malloc( sizeof( Syntax ) );
959                                 *syn = *syntax;
960
961                                 if( ad_map[i].ssam_syn_validate ) {
962                                         syn->ssyn_validate = ad_map[i].ssam_syn_validate;
963                                 }
964                                 if( ad_map[i].ssam_syn_pretty ) {
965                                         syn->ssyn_pretty = ad_map[i].ssam_syn_pretty;
966                                 }
967
968                                 (*adp)->ad_type->sat_syntax = syn;
969                         }
970
971                         /* install custom rule routines */
972                         if( ( (*adp)->ad_type->sat_equality != NULL &&
973                                         syntax == (*adp)->ad_type->sat_equality->smr_syntax ) ||
974                                 ad_map[i].ssam_mr_convert ||
975                                 ad_map[i].ssam_mr_normalize ||
976                                 ad_map[i].ssam_mr_match ||
977                                 ad_map[i].ssam_mr_indexer ||
978                                 ad_map[i].ssam_mr_filter )
979                         {
980                                 MatchingRule *mr = ch_malloc( sizeof( MatchingRule ) );
981                                 *mr = *(*adp)->ad_type->sat_equality;
982
983                                 if ( syntax == mr->smr_syntax ) {
984                                         mr->smr_syntax = (*adp)->ad_type->sat_syntax;
985                                 }
986                                 if ( ad_map[i].ssam_mr_convert ) {
987                                         mr->smr_convert = ad_map[i].ssam_mr_convert;
988                                 }
989                                 if ( ad_map[i].ssam_mr_normalize ) {
990                                         mr->smr_normalize = ad_map[i].ssam_mr_normalize;
991                                 }
992                                 if ( ad_map[i].ssam_mr_match ) {
993                                         mr->smr_match = ad_map[i].ssam_mr_match;
994                                 }
995                                 if ( ad_map[i].ssam_mr_indexer ) {
996                                         mr->smr_indexer = ad_map[i].ssam_mr_indexer;
997                                 }
998                                 if ( ad_map[i].ssam_mr_filter ) {
999                                         mr->smr_filter = ad_map[i].ssam_mr_filter;
1000                                 }
1001
1002                                 (*adp)->ad_type->sat_equality = mr;
1003                         }
1004                 }
1005         }
1006
1007         for( i=0; oc_map[i].ssom_name; i++ ) {
1008                 assert( oc_map[i].ssom_defn != NULL );
1009                 {
1010                         LDAPObjectClass *oc;
1011                         int             code;
1012                         const char      *err;
1013
1014                         oc = ldap_str2objectclass( oc_map[i].ssom_defn, &code, &err,
1015                                 LDAP_SCHEMA_ALLOW_ALL );
1016                         if ( !oc ) {
1017                                 fprintf( stderr, "slap_schema_load: ObjectClass "
1018                                         "\"%s\": %s before %s\n",
1019                                         oc_map[i].ssom_name, ldap_scherr2str(code), err );
1020                                 return code;
1021                         }
1022
1023                         if ( oc->oc_oid == NULL ) {
1024                                 fprintf( stderr, "slap_schema_load: ObjectClass "
1025                                         "\"%s\": no OID\n",
1026                                         oc_map[i].ssom_name );
1027                                 return LDAP_OTHER;
1028                         }
1029
1030                         code = oc_add(oc,0,&err);
1031                         if ( code ) {
1032                                 fprintf( stderr, "slap_schema_load: ObjectClass "
1033                                         "\"%s\": %s: \"%s\"\n",
1034                                         oc_map[i].ssom_name, scherr2str(code), err);
1035                                 return code;
1036                         }
1037
1038                         ldap_memfree(oc);
1039                 }
1040                 {
1041                         ObjectClass ** ocp = (ObjectClass **)
1042                                 &(((char *) &slap_schema)[oc_map[i].ssom_offset]);
1043
1044                         assert( *ocp == NULL );
1045
1046                         *ocp = oc_find( oc_map[i].ssom_name );
1047                         if( *ocp == NULL ) {
1048                                 fprintf( stderr, "slap_schema_load: "
1049                                         "ObjectClass \"%s\": not defined in schema\n",
1050                                         oc_map[i].ssom_name );
1051                                 return LDAP_OBJECT_CLASS_VIOLATION;
1052                         }
1053
1054                         if( oc_map[i].ssom_check ) {
1055                                 /* install check routine */
1056                                 (*ocp)->soc_check = oc_map[i].ssom_check;
1057                         }
1058                         /* install flags */
1059                         (*ocp)->soc_flags |= oc_map[i].ssom_flags;
1060                 }
1061         }
1062
1063         slap_at_undefined.sat_syntax = slap_schema.si_syn_distinguishedName;
1064         slap_schema.si_at_undefined = &slap_at_undefined;
1065
1066         return LDAP_SUCCESS;
1067 }
1068
1069 int
1070 slap_schema_check( void )
1071 {
1072         /* we should only be called once after schema_init() was called */
1073         assert( schema_init_done == 1 );
1074
1075         /*
1076          * cycle thru attributeTypes to build matchingRuleUse
1077          */
1078         if ( matching_rule_use_init() ) {
1079                 return LDAP_OTHER;
1080         }
1081
1082         ++schema_init_done;
1083         return LDAP_SUCCESS;
1084 }
1085
1086 static int rootDseObjectClass (
1087         Backend *be,
1088         Entry *e,
1089         ObjectClass *oc,
1090         const char** text,
1091         char *textbuf, size_t textlen )
1092 {
1093         *text = textbuf;
1094
1095         if( e->e_nname.bv_len ) {
1096                 snprintf( textbuf, textlen,
1097                         "objectClass \"%s\" only allowed in the root DSE",
1098                         oc->soc_oid );
1099                 return LDAP_OBJECT_CLASS_VIOLATION;
1100         }
1101
1102         /* we should not be called for the root DSE */
1103         assert( 0 );
1104         return LDAP_SUCCESS;
1105 }
1106
1107 static int aliasObjectClass (
1108         Backend *be,
1109         Entry *e,
1110         ObjectClass *oc,
1111         const char** text,
1112         char *textbuf, size_t textlen )
1113 {
1114         *text = textbuf;
1115
1116         if( !SLAP_ALIASES(be) ) {
1117                 snprintf( textbuf, textlen,
1118                         "objectClass \"%s\" not supported in context",
1119                         oc->soc_oid );
1120                 return LDAP_OBJECT_CLASS_VIOLATION;
1121         }
1122
1123         return LDAP_SUCCESS;
1124 }
1125
1126 static int referralObjectClass (
1127         Backend *be,
1128         Entry *e,
1129         ObjectClass *oc,
1130         const char** text,
1131         char *textbuf, size_t textlen )
1132 {
1133         *text = textbuf;
1134
1135         if( !SLAP_REFERRALS(be) ) {
1136                 snprintf( textbuf, textlen,
1137                         "objectClass \"%s\" not supported in context",
1138                         oc->soc_oid );
1139                 return LDAP_OBJECT_CLASS_VIOLATION;
1140         }
1141
1142         return LDAP_SUCCESS;
1143 }
1144
1145 static int subentryObjectClass (
1146         Backend *be,
1147         Entry *e,
1148         ObjectClass *oc,
1149         const char** text,
1150         char *textbuf, size_t textlen )
1151 {
1152         *text = textbuf;
1153
1154         if( !SLAP_SUBENTRIES(be) ) {
1155                 snprintf( textbuf, textlen,
1156                         "objectClass \"%s\" not supported in context",
1157                         oc->soc_oid );
1158                 return LDAP_OBJECT_CLASS_VIOLATION;
1159         }
1160
1161         if( oc != slap_schema.si_oc_subentry && !is_entry_subentry( e ) ) {
1162                 snprintf( textbuf, textlen,
1163                         "objectClass \"%s\" only allowed in subentries",
1164                         oc->soc_oid );
1165                 return LDAP_OBJECT_CLASS_VIOLATION;
1166         }
1167
1168         return LDAP_SUCCESS;
1169 }
1170
1171 static int dynamicObjectClass (
1172         Backend *be,
1173         Entry *e,
1174         ObjectClass *oc,
1175         const char** text,
1176         char *textbuf, size_t textlen )
1177 {
1178         *text = textbuf;
1179
1180         if( !SLAP_DYNAMIC(be) ) {
1181                 snprintf( textbuf, textlen,
1182                         "objectClass \"%s\" not supported in context",
1183                         oc->soc_oid );
1184                 return LDAP_OBJECT_CLASS_VIOLATION;
1185         }
1186
1187         return LDAP_SUCCESS;
1188 }
1189
1190 static int rootDseAttribute (
1191         Backend *be,
1192         Entry *e,
1193         Attribute *attr,
1194         const char** text,
1195         char *textbuf, size_t textlen )
1196 {
1197         *text = textbuf;
1198
1199         if( e->e_nname.bv_len ) {
1200                 snprintf( textbuf, textlen,
1201                         "attribute \"%s\" only allowed in the root DSE",
1202                         attr->a_desc->ad_cname.bv_val );
1203                 return LDAP_OBJECT_CLASS_VIOLATION;
1204         }
1205
1206         /* we should not be called for the root DSE */
1207         assert( 0 );
1208         return LDAP_SUCCESS;
1209 }
1210
1211 static int aliasAttribute (
1212         Backend *be,
1213         Entry *e,
1214         Attribute *attr,
1215         const char** text,
1216         char *textbuf, size_t textlen )
1217 {
1218         *text = textbuf;
1219
1220         if( !SLAP_ALIASES(be) ) {
1221                 snprintf( textbuf, textlen,
1222                         "attribute \"%s\" not supported in context",
1223                         attr->a_desc->ad_cname.bv_val );
1224                 return LDAP_OBJECT_CLASS_VIOLATION;
1225         }
1226
1227         if( !is_entry_alias( e ) ) {
1228                 snprintf( textbuf, textlen,
1229                         "attribute \"%s\" only allowed in the alias",
1230                         attr->a_desc->ad_cname.bv_val );
1231                 return LDAP_OBJECT_CLASS_VIOLATION;
1232         }
1233
1234         return LDAP_SUCCESS;
1235 }
1236
1237 static int referralAttribute (
1238         Backend *be,
1239         Entry *e,
1240         Attribute *attr,
1241         const char** text,
1242         char *textbuf, size_t textlen )
1243 {
1244         *text = textbuf;
1245
1246         if( !SLAP_REFERRALS(be) ) {
1247                 snprintf( textbuf, textlen,
1248                         "attribute \"%s\" not supported in context",
1249                         attr->a_desc->ad_cname.bv_val );
1250                 return LDAP_OBJECT_CLASS_VIOLATION;
1251         }
1252
1253         if( !is_entry_referral( e ) ) {
1254                 snprintf( textbuf, textlen,
1255                         "attribute \"%s\" only allowed in the referral",
1256                         attr->a_desc->ad_cname.bv_val );
1257                 return LDAP_OBJECT_CLASS_VIOLATION;
1258         }
1259
1260         return LDAP_SUCCESS;
1261 }
1262
1263 static int subentryAttribute (
1264         Backend *be,
1265         Entry *e,
1266         Attribute *attr,
1267         const char** text,
1268         char *textbuf, size_t textlen )
1269 {
1270         *text = textbuf;
1271
1272         if( !SLAP_SUBENTRIES(be) ) {
1273                 snprintf( textbuf, textlen,
1274                         "attribute \"%s\" not supported in context",
1275                         attr->a_desc->ad_cname.bv_val );
1276                 return LDAP_OBJECT_CLASS_VIOLATION;
1277         }
1278
1279         if( !is_entry_subentry( e ) ) {
1280                 snprintf( textbuf, textlen,
1281                         "attribute \"%s\" only allowed in the subentry",
1282                         attr->a_desc->ad_cname.bv_val );
1283                 return LDAP_OBJECT_CLASS_VIOLATION;
1284         }
1285
1286         return LDAP_SUCCESS;
1287 }
1288
1289 static int administrativeRoleAttribute (
1290         Backend *be,
1291         Entry *e,
1292         Attribute *attr,
1293         const char** text,
1294         char *textbuf, size_t textlen )
1295 {
1296         *text = textbuf;
1297
1298         if( !SLAP_SUBENTRIES(be) ) {
1299                 snprintf( textbuf, textlen,
1300                         "attribute \"%s\" not supported in context",
1301                         attr->a_desc->ad_cname.bv_val );
1302                 return LDAP_OBJECT_CLASS_VIOLATION;
1303         }
1304
1305         snprintf( textbuf, textlen,
1306                 "attribute \"%s\" not supported!",
1307                 attr->a_desc->ad_cname.bv_val );
1308         return LDAP_OBJECT_CLASS_VIOLATION;
1309 }
1310
1311 static int dynamicAttribute (
1312         Backend *be,
1313         Entry *e,
1314         Attribute *attr,
1315         const char** text,
1316         char *textbuf, size_t textlen )
1317 {
1318         *text = textbuf;
1319
1320         if( !SLAP_DYNAMIC(be) ) {
1321                 snprintf( textbuf, textlen,
1322                         "attribute \"%s\" not supported in context",
1323                         attr->a_desc->ad_cname.bv_val );
1324                 return LDAP_OBJECT_CLASS_VIOLATION;
1325         }
1326
1327         if( !is_entry_dynamicObject( e ) ) {
1328                 snprintf( textbuf, textlen,
1329                         "attribute \"%s\" only allowed in dynamic object",
1330                         attr->a_desc->ad_cname.bv_val );
1331                 return LDAP_OBJECT_CLASS_VIOLATION;
1332         }
1333
1334         return LDAP_SUCCESS;
1335 }