1 /* schema.c - routines to enforce schema definitions */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
19 static char * oc_check_required(Entry *e, char *ocname);
20 static int oc_check_allowed(char *type, struct berval **ocl);
23 * oc_check - check that entry e conforms to the schema required by
24 * its object class(es). returns 0 if so, non-zero otherwise.
28 oc_schema_check( Entry *e )
36 /* find the object class attribute - could error out here */
37 if ( (aoc = attr_find( e->e_attrs, "objectclass" )) == NULL ) {
38 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
43 /* check that the entry has required attrs for each oc */
44 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
45 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
46 Debug( LDAP_DEBUG_ANY,
47 "Objectclass \"%s\" not defined\n",
48 aoc->a_vals[i]->bv_val, 0, 0 );
52 char *s = oc_check_required( e, aoc->a_vals[i]->bv_val );
55 Debug( LDAP_DEBUG_ANY,
56 "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
57 e->e_dn, aoc->a_vals[i]->bv_val, s );
67 /* check that each attr in the entry is allowed by some oc */
68 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
69 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
70 Debug( LDAP_DEBUG_ANY,
71 "Entry (%s), attr \"%s\" not allowed\n",
72 e->e_dn, a->a_type, 0 );
81 oc_check_required( Entry *e, char *ocname )
89 Debug( LDAP_DEBUG_TRACE,
90 "oc_check_required entry (%s), objectclass \"%s\"\n",
93 /* find global oc defn. it we don't know about it assume it's ok */
94 if ( (oc = oc_find( ocname )) == NULL ) {
98 /* check for empty oc_required */
99 if(oc->soc_required == NULL) {
103 /* for each required attribute */
104 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
105 at = oc->soc_required[i];
106 /* see if it's in the entry */
107 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
109 strcmp( a->a_type, at->sat_oid ) == 0 ) {
114 /* Empty name list => not found */
119 if ( strcasecmp( a->a_type, *pp ) == 0 ) {
128 /* not there => schema violation */
130 if ( at->sat_names && at->sat_names[0] ) {
131 return at->sat_names[0];
142 oc_check_allowed( char *type, struct berval **ocl )
150 Debug( LDAP_DEBUG_TRACE,
151 "oc_check_allowed type \"%s\"\n", type, 0, 0 );
153 /* always allow objectclass attribute */
154 if ( strcasecmp( type, "objectclass" ) == 0 ) {
158 #ifdef SLAPD_SCHEMA_COMPAT
159 /* Treat any attribute type with option as an unknown attribute type */
161 * The "type" we have received is actually an AttributeDescription.
162 * Let's find out the corresponding type.
164 p = strchr( type, ';' );
166 t = ch_malloc( p-type+1 );
167 strncpy( t, type, p-type );
169 Debug( LDAP_DEBUG_TRACE,
170 "oc_check_allowed type \"%s\" from \"%s\"\n",
181 * All operational attributions are allowed by schema rules.
183 if ( oc_check_op_attr( t ) ) {
187 /* check that the type appears as req or opt in at least one oc */
188 for ( i = 0; ocl[i] != NULL; i++ ) {
189 /* if we know about the oc */
190 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
191 /* does it require the type? */
192 for ( j = 0; oc->soc_required != NULL &&
193 oc->soc_required[j] != NULL; j++ ) {
194 at = oc->soc_required[j];
196 strcmp(at->sat_oid, t ) == 0 ) {
205 if ( strcasecmp( *pp, t ) == 0 ) {
213 /* does it allow the type? */
214 for ( j = 0; oc->soc_allowed != NULL &&
215 oc->soc_allowed[j] != NULL; j++ ) {
216 at = oc->soc_allowed[j];
218 strcmp( at->sat_oid, t ) == 0 ) {
227 if ( strcasecmp( *pp, t ) == 0 ||
228 strcmp( *pp, "*" ) == 0 ) {
236 /* maybe the next oc allows it */
238 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
239 /* we don't know about the oc. assume it allows it */
250 /* not allowed by any oc */
255 #ifdef SLAPD_SCHEMA_COMPAT
256 /* these shouldn't be hardcoded */
258 static char *oc_op_usermod_attrs[] = {
260 * these are operational attributes which are
261 * not defined as NO-USER_MODIFICATION and
262 * which slapd supports modification of.
265 * Likely candidate, "aci"
270 static char *oc_op_attrs[] = {
272 * these are operational attributes
273 * most could be user modifiable
284 "supportedExtension",
286 "supportedSASLMechanisms",
287 "supportedLDAPversion",
288 "supportedACIMechanisms",
289 "subschemaSubentry", /* NO USER MOD */
294 /* this list should be extensible */
295 static char *oc_op_no_usermod_attrs[] = {
297 * Operational and 'no user modification' attributes
298 * which are STORED in the directory server.
313 * check to see if attribute is 'operational' or not.
316 oc_check_op_attr( const char *type )
318 #ifndef SLAPD_SCHEMA_NOT_COMPAT
319 return charray_inlist( oc_op_attrs, type )
320 || charray_inlist( oc_op_usermod_attrs, type )
321 || charray_inlist( oc_op_no_usermod_attrs, type );
323 AttributeType *at = at_find( type );
325 if( at == NULL ) return 0;
327 return at->sat_usage != LDAP_SCHEMA_USER_APPLICATIONS;
332 * check to see if attribute can be user modified or not.
335 oc_check_op_usermod_attr( const char *type )
337 #ifdef SLAPD_SCHEMA_COMPAT
338 return charray_inlist( oc_op_usermod_attrs, type );
340 /* not (yet) in schema */
346 * check to see if attribute is 'no user modification' or not.
349 oc_check_op_no_usermod_attr( const char *type )
351 #ifdef SLAPD_SCHEMA_COMPAT
352 return charray_inlist( oc_op_no_usermod_attrs, type );
354 AttributeType *at = at_find( type );
356 if( at == NULL ) return 0;
358 return at->sat_no_user_mod;
368 static Avlnode *oc_index = NULL;
369 static ObjectClass *oc_list = NULL;
373 struct oindexrec *oir1,
374 struct oindexrec *oir2
377 return (strcasecmp( oir1->oir_name, oir2->oir_name ));
383 struct oindexrec *oir
386 return (strcasecmp( name, oir->oir_name ));
390 oc_find( const char *ocname )
392 struct oindexrec *oir = NULL;
394 if ( (oir = (struct oindexrec *) avl_find( oc_index, ocname,
395 (AVL_CMP) oc_index_name_cmp )) != NULL ) {
396 return( oir->oir_oc );
410 AttributeType **satp;
416 sat = at_find(*attrs1);
419 return SLAP_SCHERR_ATTR_NOT_FOUND;
421 if ( at_find_in_list(sat, soc->soc_required) < 0) {
422 if ( at_append_to_list(sat, &soc->soc_required) ) {
424 return SLAP_SCHERR_OUTOFMEM;
429 /* Now delete duplicates from the allowed list */
430 for ( satp = soc->soc_required; *satp; satp++ ) {
431 i = at_find_in_list(*satp,soc->soc_allowed);
433 at_delete_from_list(i, &soc->soc_allowed);
453 sat = at_find(*attrs1);
456 return SLAP_SCHERR_ATTR_NOT_FOUND;
458 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
459 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
460 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
462 return SLAP_SCHERR_OUTOFMEM;
485 if ( !soc->soc_sups ) {
486 /* We are at the first recursive level */
495 soc->soc_sups = (ObjectClass **)ch_calloc(1,
496 nsups*sizeof(ObjectClass *));
501 soc1 = oc_find(*sups1);
504 return SLAP_SCHERR_CLASS_NOT_FOUND;
508 soc->soc_sups[nsups] = soc1;
510 code = oc_add_sups(soc,soc1->soc_sup_oids, err);
514 code = oc_create_required(soc,soc1->soc_at_oids_must,err);
517 code = oc_create_allowed(soc,soc1->soc_at_oids_may,err);
535 struct oindexrec *oir;
539 while ( *ocp != NULL ) {
540 ocp = &(*ocp)->soc_next;
544 if ( soc->soc_oid ) {
545 oir = (struct oindexrec *)
546 ch_calloc( 1, sizeof(struct oindexrec) );
547 oir->oir_name = soc->soc_oid;
549 if ( avl_insert( &oc_index, (caddr_t) oir,
550 (AVL_CMP) oc_index_cmp,
551 (AVL_DUP) avl_dup_error ) ) {
554 return SLAP_SCHERR_DUP_CLASS;
556 /* FIX: temporal consistency check */
557 oc_find(oir->oir_name);
559 if ( (names = soc->soc_names) ) {
561 oir = (struct oindexrec *)
562 ch_calloc( 1, sizeof(struct oindexrec) );
563 oir->oir_name = ch_strdup(*names);
565 if ( avl_insert( &oc_index, (caddr_t) oir,
566 (AVL_CMP) oc_index_cmp,
567 (AVL_DUP) avl_dup_error ) ) {
570 return SLAP_SCHERR_DUP_CLASS;
572 /* FIX: temporal consistency check */
573 oc_find(oir->oir_name);
582 LDAP_OBJECT_CLASS *oc,
589 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
590 memcpy( &soc->soc_oclass, oc, sizeof(LDAP_OBJECT_CLASS));
591 if ( (code = oc_add_sups(soc,soc->soc_sup_oids,err)) != 0 )
593 if ( (code = oc_create_required(soc,soc->soc_at_oids_must,err)) != 0 )
595 if ( (code = oc_create_allowed(soc,soc->soc_at_oids_may,err)) != 0 )
597 code = oc_insert(soc,err);
606 static Avlnode *syn_index = NULL;
607 static Syntax *syn_list = NULL;
611 struct sindexrec *sir1,
612 struct sindexrec *sir2
615 return (strcmp( sir1->sir_name, sir2->sir_name ));
621 struct sindexrec *sir
624 return (strcmp( name, sir->sir_name ));
628 syn_find( const char *synname )
630 struct sindexrec *sir = NULL;
632 if ( (sir = (struct sindexrec *) avl_find( syn_index, synname,
633 (AVL_CMP) syn_index_name_cmp )) != NULL ) {
634 return( sir->sir_syn );
640 syn_find_desc( const char *syndesc, int *len )
644 for (synp = syn_list; synp; synp = synp->ssyn_next)
645 if ((*len = dscompare( synp->ssyn_syn.syn_desc, syndesc, '{')))
657 struct sindexrec *sir;
660 while ( *synp != NULL ) {
661 synp = &(*synp)->ssyn_next;
665 if ( ssyn->ssyn_oid ) {
666 sir = (struct sindexrec *)
667 ch_calloc( 1, sizeof(struct sindexrec) );
668 sir->sir_name = ssyn->ssyn_oid;
670 if ( avl_insert( &syn_index, (caddr_t) sir,
671 (AVL_CMP) syn_index_cmp,
672 (AVL_DUP) avl_dup_error ) ) {
673 *err = ssyn->ssyn_oid;
675 return SLAP_SCHERR_DUP_SYNTAX;
677 /* FIX: temporal consistency check */
678 syn_find(sir->sir_name);
687 slap_syntax_validate_func *validate,
688 slap_syntax_transform_func *ber2str,
689 slap_syntax_transform_func *str2ber,
696 ssyn = (Syntax *) ch_calloc( 1, sizeof(Syntax) );
697 memcpy( &ssyn->ssyn_syn, syn, sizeof(LDAP_SYNTAX));
699 ssyn->ssyn_flags = flags;
700 ssyn->ssyn_validate = validate;
701 ssyn->ssyn_ber2str = ber2str;
702 ssyn->ssyn_str2ber = str2ber;
704 code = syn_insert(ssyn,err);
710 MatchingRule *mir_mr;
713 static Avlnode *mr_index = NULL;
714 static MatchingRule *mr_list = NULL;
718 struct mindexrec *mir1,
719 struct mindexrec *mir2
722 return (strcmp( mir1->mir_name, mir2->mir_name ));
728 struct mindexrec *mir
731 return (strcmp( name, mir->mir_name ));
735 mr_find( const char *mrname )
737 struct mindexrec *mir = NULL;
739 if ( (mir = (struct mindexrec *) avl_find( mr_index, mrname,
740 (AVL_CMP) mr_index_name_cmp )) != NULL ) {
741 return( mir->mir_mr );
753 struct mindexrec *mir;
757 while ( *mrp != NULL ) {
758 mrp = &(*mrp)->smr_next;
762 if ( smr->smr_oid ) {
763 mir = (struct mindexrec *)
764 ch_calloc( 1, sizeof(struct mindexrec) );
765 mir->mir_name = smr->smr_oid;
767 if ( avl_insert( &mr_index, (caddr_t) mir,
768 (AVL_CMP) mr_index_cmp,
769 (AVL_DUP) avl_dup_error ) ) {
772 return SLAP_SCHERR_DUP_RULE;
774 /* FIX: temporal consistency check */
775 mr_find(mir->mir_name);
777 if ( (names = smr->smr_names) ) {
779 mir = (struct mindexrec *)
780 ch_calloc( 1, sizeof(struct mindexrec) );
781 mir->mir_name = ch_strdup(*names);
783 if ( avl_insert( &mr_index, (caddr_t) mir,
784 (AVL_CMP) mr_index_cmp,
785 (AVL_DUP) avl_dup_error ) ) {
788 return SLAP_SCHERR_DUP_RULE;
790 /* FIX: temporal consistency check */
791 mr_find(mir->mir_name);
800 LDAP_MATCHING_RULE *mr,
801 slap_mr_convert_func *convert,
802 slap_mr_normalize_func *normalize,
803 slap_mr_match_func *match,
804 slap_mr_indexer_func *indexer,
805 slap_mr_filter_func *filter,
813 smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
814 memcpy( &smr->smr_mrule, mr, sizeof(LDAP_MATCHING_RULE));
816 smr->smr_convert = convert;
817 smr->smr_normalize = normalize;
818 smr->smr_match = match;
819 smr->smr_indexer = indexer;
820 smr->smr_filter = filter;
822 if ( smr->smr_syntax_oid ) {
823 if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
824 smr->smr_syntax = syn;
826 *err = smr->smr_syntax_oid;
827 return SLAP_SCHERR_SYN_NOT_FOUND;
831 return SLAP_SCHERR_MR_INCOMPLETE;
833 code = mr_insert(smr,err);
839 char * desc, int flags,
840 slap_syntax_validate_func *validate,
841 slap_syntax_transform_func *ber2str,
842 slap_syntax_transform_func *str2ber )
848 syn = ldap_str2syntax( desc, &code, &err);
850 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
851 ldap_scherr2str(code), err, desc );
855 code = syn_add( syn, flags, validate, ber2str, str2ber, &err );
857 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s %s in %s\n",
858 scherr2str(code), err, desc );
866 register_matching_rule(
868 slap_mr_convert_func *convert,
869 slap_mr_normalize_func *normalize,
870 slap_mr_match_func *match,
871 slap_mr_indexer_func *indexer,
872 slap_mr_filter_func *filter )
874 LDAP_MATCHING_RULE *mr;
878 mr = ldap_str2matchingrule( desc, &code, &err);
880 Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s before %s in %s\n",
881 ldap_scherr2str(code), err, desc );
885 code = mr_add( mr, convert, normalize, match, indexer, filter, &err );
887 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s for %s in %s\n",
888 scherr2str(code), err, desc );
895 #if defined( SLAPD_SCHEMA_DN )
898 syn_schema_info( Entry *e )
901 struct berval *vals[2];
907 for ( syn = syn_list; syn; syn = syn->ssyn_next ) {
908 val.bv_val = ldap_syntax2str( &syn->ssyn_syn );
910 val.bv_len = strlen( val.bv_val );
911 Debug( LDAP_DEBUG_TRACE, "Merging syn [%ld] %s\n",
912 (long) val.bv_len, val.bv_val, 0 );
913 attr_merge( e, "ldapSyntaxes", vals );
914 ldap_memfree( val.bv_val );
923 mr_schema_info( Entry *e )
926 struct berval *vals[2];
932 for ( mr = mr_list; mr; mr = mr->smr_next ) {
933 val.bv_val = ldap_matchingrule2str( &mr->smr_mrule );
935 val.bv_len = strlen( val.bv_val );
936 Debug( LDAP_DEBUG_TRACE, "Merging mr [%ld] %s\n",
937 (long) val.bv_len, val.bv_val, 0 );
938 attr_merge( e, "matchingRules", vals );
939 ldap_memfree( val.bv_val );
948 oc_schema_info( Entry *e )
951 struct berval *vals[2];
957 for ( oc = oc_list; oc; oc = oc->soc_next ) {
958 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
960 val.bv_len = strlen( val.bv_val );
961 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
962 (long) val.bv_len, val.bv_val, 0 );
963 attr_merge( e, "objectClasses", vals );
964 ldap_memfree( val.bv_val );
973 schema_info( Connection *conn, Operation *op, char **attrs, int attrsonly )
977 struct berval *vals[2];
982 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
985 e->e_dn = ch_strdup( SLAPD_SCHEMA_DN );
986 e->e_ndn = ch_strdup( SLAPD_SCHEMA_DN );
987 (void) dn_normalize( e->e_ndn );
991 char *rdn = ch_strdup( SLAPD_SCHEMA_DN );
992 val.bv_val = strchr( rdn, '=' );
994 if( val.bv_val != NULL ) {
996 val.bv_len = strlen( ++val.bv_val );
998 attr_merge( e, rdn, vals );
1004 if ( syn_schema_info( e ) ) {
1005 /* Out of memory, do something about it */
1009 if ( mr_schema_info( e ) ) {
1010 /* Out of memory, do something about it */
1014 if ( at_schema_info( e ) ) {
1015 /* Out of memory, do something about it */
1019 if ( oc_schema_info( e ) ) {
1020 /* Out of memory, do something about it */
1026 val.bv_len = sizeof("top")-1;
1027 attr_merge( e, "objectClass", vals );
1029 val.bv_val = "LDAPsubentry";
1030 val.bv_len = sizeof("LDAPsubentry")-1;
1031 attr_merge( e, "objectClass", vals );
1033 val.bv_val = "subschema";
1034 val.bv_len = sizeof("subschema")-1;
1035 attr_merge( e, "objectClass", vals );
1037 val.bv_val = "extensibleObject";
1038 val.bv_len = sizeof("extensibleObject")-1;
1039 attr_merge( e, "objectClass", vals );
1041 send_search_entry( &backends[0], conn, op,
1042 e, attrs, attrsonly, NULL );
1043 send_search_result( conn, op, LDAP_SUCCESS,
1044 NULL, NULL, NULL, NULL, 1 );
1053 oc_print( ObjectClass *oc )
1058 printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
1059 if ( oc->soc_required != NULL ) {
1060 mid = "\trequires ";
1061 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
1062 printf( "%s%s", mid,
1063 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
1066 if ( oc->soc_allowed != NULL ) {
1068 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
1069 printf( "%s%s", mid,
1070 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
1077 int is_entry_objectclass(
1084 if( e == NULL || oc == NULL || *oc == '\0' )
1088 * find objectClass attribute
1090 attr = attr_find(e->e_attrs, "objectclass");
1092 if( attr == NULL ) {
1093 /* no objectClass attribute */
1097 bv.bv_val = (char *) oc;
1098 bv.bv_len = strlen( bv.bv_val );
1100 #ifdef SLAPD_SCHEMA_COMPAT
1101 if( value_find(attr->a_vals, &bv, attr->a_syntax, 1) != 0) {
1102 /* entry is not of this objectclass */