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];
141 #ifdef SLAPD_SCHEMA_COMPAT
142 /* these shouldn't be hardcoded */
144 static char *oc_op_usermod_attrs[] = {
146 * these are operational attributes which are
147 * not defined as NO-USER_MODIFICATION and
148 * which slapd supports modification of.
151 * Likely candidate, "aci"
156 static char *oc_op_attrs[] = {
158 * these are operational attributes
159 * most could be user modifiable
170 "supportedExtension",
172 "supportedSASLMechanisms",
173 "supportedLDAPversion",
174 "supportedACIMechanisms",
175 "subschemaSubentry", /* NO USER MOD */
180 /* this list should be extensible */
181 static char *oc_op_no_usermod_attrs[] = {
183 * Operational and 'no user modification' attributes
184 * which are STORED in the directory server.
199 * check to see if attribute is 'operational' or not.
202 oc_check_op_attr( const char *type )
204 #ifdef SLAPD_SCHEMA_COMPAT
205 return charray_inlist( oc_op_attrs, type )
206 || charray_inlist( oc_op_usermod_attrs, type )
207 || charray_inlist( oc_op_no_usermod_attrs, type );
209 AttributeType *at = at_find( type );
211 if( at == NULL ) return 0;
213 return at->sat_usage != 0;
218 * check to see if attribute can be user modified or not.
221 oc_check_op_usermod_attr( const char *type )
223 #ifdef SLAPD_SCHEMA_COMPAT
224 return charray_inlist( oc_op_usermod_attrs, type );
226 /* not (yet) in schema */
232 * check to see if attribute is 'no user modification' or not.
235 oc_check_op_no_usermod_attr( const char *type )
237 #ifdef SLAPD_SCHEMA_COMPAT
238 return charray_inlist( oc_op_no_usermod_attrs, type );
240 AttributeType *at = at_find( type );
242 if( at == NULL ) return 0;
244 return at->sat_no_user_mod;
250 oc_check_allowed( char *type, struct berval **ocl )
258 Debug( LDAP_DEBUG_TRACE,
259 "oc_check_allowed type \"%s\"\n", type, 0, 0 );
261 /* always allow objectclass attribute */
262 if ( strcasecmp( type, "objectclass" ) == 0 ) {
267 * The "type" we have received is actually an AttributeDescription.
268 * Let's find out the corresponding type.
270 p = strchr( type, ';' );
272 t = ch_malloc( p-type+1 );
273 strncpy( t, type, p-type );
275 Debug( LDAP_DEBUG_TRACE,
276 "oc_check_allowed type \"%s\" from \"%s\"\n",
284 * All operational attributions are allowed by schema rules.
286 if ( oc_check_op_attr( t ) ) {
290 /* check that the type appears as req or opt in at least one oc */
291 for ( i = 0; ocl[i] != NULL; i++ ) {
292 /* if we know about the oc */
293 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
294 /* does it require the type? */
295 for ( j = 0; oc->soc_required != NULL &&
296 oc->soc_required[j] != NULL; j++ ) {
297 at = oc->soc_required[j];
299 strcmp(at->sat_oid, t ) == 0 ) {
308 if ( strcasecmp( *pp, t ) == 0 ) {
316 /* does it allow the type? */
317 for ( j = 0; oc->soc_allowed != NULL &&
318 oc->soc_allowed[j] != NULL; j++ ) {
319 at = oc->soc_allowed[j];
321 strcmp( at->sat_oid, t ) == 0 ) {
330 if ( strcasecmp( *pp, t ) == 0 ||
331 strcmp( *pp, "*" ) == 0 ) {
339 /* maybe the next oc allows it */
341 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
342 /* we don't know about the oc. assume it allows it */
353 /* not allowed by any oc */
362 static Avlnode *oc_index = NULL;
363 static ObjectClass *oc_list = NULL;
367 struct oindexrec *oir1,
368 struct oindexrec *oir2
371 return (strcasecmp( oir1->oir_name, oir2->oir_name ));
377 struct oindexrec *oir
380 return (strcasecmp( name, oir->oir_name ));
384 oc_find( const char *ocname )
386 struct oindexrec *oir = NULL;
388 if ( (oir = (struct oindexrec *) avl_find( oc_index, ocname,
389 (AVL_CMP) oc_index_name_cmp )) != NULL ) {
390 return( oir->oir_oc );
404 AttributeType **satp;
410 sat = at_find(*attrs1);
413 return SLAP_SCHERR_ATTR_NOT_FOUND;
415 if ( at_find_in_list(sat, soc->soc_required) < 0) {
416 if ( at_append_to_list(sat, &soc->soc_required) ) {
418 return SLAP_SCHERR_OUTOFMEM;
423 /* Now delete duplicates from the allowed list */
424 for ( satp = soc->soc_required; *satp; satp++ ) {
425 i = at_find_in_list(*satp,soc->soc_allowed);
427 at_delete_from_list(i, &soc->soc_allowed);
447 sat = at_find(*attrs1);
450 return SLAP_SCHERR_ATTR_NOT_FOUND;
452 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
453 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
454 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
456 return SLAP_SCHERR_OUTOFMEM;
479 if ( !soc->soc_sups ) {
480 /* We are at the first recursive level */
489 soc->soc_sups = (ObjectClass **)ch_calloc(1,
490 nsups*sizeof(ObjectClass *));
495 soc1 = oc_find(*sups1);
498 return SLAP_SCHERR_CLASS_NOT_FOUND;
502 soc->soc_sups[nsups] = soc1;
504 code = oc_add_sups(soc,soc1->soc_sup_oids, err);
508 code = oc_create_required(soc,soc1->soc_at_oids_must,err);
511 code = oc_create_allowed(soc,soc1->soc_at_oids_may,err);
529 struct oindexrec *oir;
533 while ( *ocp != NULL ) {
534 ocp = &(*ocp)->soc_next;
538 if ( soc->soc_oid ) {
539 oir = (struct oindexrec *)
540 ch_calloc( 1, sizeof(struct oindexrec) );
541 oir->oir_name = soc->soc_oid;
543 if ( avl_insert( &oc_index, (caddr_t) oir,
544 (AVL_CMP) oc_index_cmp,
545 (AVL_DUP) avl_dup_error ) ) {
548 return SLAP_SCHERR_DUP_CLASS;
550 /* FIX: temporal consistency check */
551 oc_find(oir->oir_name);
553 if ( (names = soc->soc_names) ) {
555 oir = (struct oindexrec *)
556 ch_calloc( 1, sizeof(struct oindexrec) );
557 oir->oir_name = ch_strdup(*names);
559 if ( avl_insert( &oc_index, (caddr_t) oir,
560 (AVL_CMP) oc_index_cmp,
561 (AVL_DUP) avl_dup_error ) ) {
564 return SLAP_SCHERR_DUP_CLASS;
566 /* FIX: temporal consistency check */
567 oc_find(oir->oir_name);
576 LDAP_OBJECT_CLASS *oc,
583 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
584 memcpy( &soc->soc_oclass, oc, sizeof(LDAP_OBJECT_CLASS));
585 if ( (code = oc_add_sups(soc,soc->soc_sup_oids,err)) != 0 )
587 if ( (code = oc_create_required(soc,soc->soc_at_oids_must,err)) != 0 )
589 if ( (code = oc_create_allowed(soc,soc->soc_at_oids_may,err)) != 0 )
591 code = oc_insert(soc,err);
600 static Avlnode *syn_index = NULL;
601 static Syntax *syn_list = NULL;
605 struct sindexrec *sir1,
606 struct sindexrec *sir2
609 return (strcmp( sir1->sir_name, sir2->sir_name ));
615 struct sindexrec *sir
618 return (strcmp( name, sir->sir_name ));
622 syn_find( const char *synname )
624 struct sindexrec *sir = NULL;
626 if ( (sir = (struct sindexrec *) avl_find( syn_index, synname,
627 (AVL_CMP) syn_index_name_cmp )) != NULL ) {
628 return( sir->sir_syn );
634 syn_find_desc( const char *syndesc, int *len )
638 for (synp = syn_list; synp; synp = synp->ssyn_next)
639 if ((*len = dscompare( synp->ssyn_syn.syn_desc, syndesc, '{')))
651 struct sindexrec *sir;
654 while ( *synp != NULL ) {
655 synp = &(*synp)->ssyn_next;
659 if ( ssyn->ssyn_oid ) {
660 sir = (struct sindexrec *)
661 ch_calloc( 1, sizeof(struct sindexrec) );
662 sir->sir_name = ssyn->ssyn_oid;
664 if ( avl_insert( &syn_index, (caddr_t) sir,
665 (AVL_CMP) syn_index_cmp,
666 (AVL_DUP) avl_dup_error ) ) {
667 *err = ssyn->ssyn_oid;
669 return SLAP_SCHERR_DUP_SYNTAX;
671 /* FIX: temporal consistency check */
672 syn_find(sir->sir_name);
681 slap_syntax_validate_func *validate,
682 slap_syntax_transform_func *ber2str,
683 slap_syntax_transform_func *str2ber,
690 ssyn = (Syntax *) ch_calloc( 1, sizeof(Syntax) );
691 memcpy( &ssyn->ssyn_syn, syn, sizeof(LDAP_SYNTAX));
693 ssyn->ssyn_flags = flags;
694 ssyn->ssyn_validate = validate;
695 ssyn->ssyn_ber2str = ber2str;
696 ssyn->ssyn_str2ber = str2ber;
698 code = syn_insert(ssyn,err);
704 MatchingRule *mir_mr;
707 static Avlnode *mr_index = NULL;
708 static MatchingRule *mr_list = NULL;
712 struct mindexrec *mir1,
713 struct mindexrec *mir2
716 return (strcmp( mir1->mir_name, mir2->mir_name ));
722 struct mindexrec *mir
725 return (strcmp( name, mir->mir_name ));
729 mr_find( const char *mrname )
731 struct mindexrec *mir = NULL;
733 if ( (mir = (struct mindexrec *) avl_find( mr_index, mrname,
734 (AVL_CMP) mr_index_name_cmp )) != NULL ) {
735 return( mir->mir_mr );
747 struct mindexrec *mir;
751 while ( *mrp != NULL ) {
752 mrp = &(*mrp)->smr_next;
756 if ( smr->smr_oid ) {
757 mir = (struct mindexrec *)
758 ch_calloc( 1, sizeof(struct mindexrec) );
759 mir->mir_name = smr->smr_oid;
761 if ( avl_insert( &mr_index, (caddr_t) mir,
762 (AVL_CMP) mr_index_cmp,
763 (AVL_DUP) avl_dup_error ) ) {
766 return SLAP_SCHERR_DUP_RULE;
768 /* FIX: temporal consistency check */
769 mr_find(mir->mir_name);
771 if ( (names = smr->smr_names) ) {
773 mir = (struct mindexrec *)
774 ch_calloc( 1, sizeof(struct mindexrec) );
775 mir->mir_name = ch_strdup(*names);
777 if ( avl_insert( &mr_index, (caddr_t) mir,
778 (AVL_CMP) mr_index_cmp,
779 (AVL_DUP) avl_dup_error ) ) {
782 return SLAP_SCHERR_DUP_RULE;
784 /* FIX: temporal consistency check */
785 mr_find(mir->mir_name);
794 LDAP_MATCHING_RULE *mr,
795 slap_mr_convert_func *convert,
796 slap_mr_normalize_func *normalize,
797 slap_mr_match_func *match,
798 slap_mr_indexer_func *indexer,
799 slap_mr_filter_func *filter,
807 smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
808 memcpy( &smr->smr_mrule, mr, sizeof(LDAP_MATCHING_RULE));
810 smr->smr_convert = convert;
811 smr->smr_normalize = normalize;
812 smr->smr_match = match;
813 smr->smr_indexer = indexer;
814 smr->smr_filter = filter;
816 if ( smr->smr_syntax_oid ) {
817 if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
818 smr->smr_syntax = syn;
820 *err = smr->smr_syntax_oid;
821 return SLAP_SCHERR_SYN_NOT_FOUND;
825 return SLAP_SCHERR_MR_INCOMPLETE;
827 code = mr_insert(smr,err);
833 char * desc, int flags,
834 slap_syntax_validate_func *validate,
835 slap_syntax_transform_func *ber2str,
836 slap_syntax_transform_func *str2ber )
842 syn = ldap_str2syntax( desc, &code, &err);
844 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
845 ldap_scherr2str(code), err, desc );
849 code = syn_add( syn, flags, validate, ber2str, str2ber, &err );
851 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s %s in %s\n",
852 scherr2str(code), err, desc );
860 register_matching_rule(
862 slap_mr_convert_func *convert,
863 slap_mr_normalize_func *normalize,
864 slap_mr_match_func *match,
865 slap_mr_indexer_func *indexer,
866 slap_mr_filter_func *filter )
868 LDAP_MATCHING_RULE *mr;
872 mr = ldap_str2matchingrule( desc, &code, &err);
874 Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s before %s in %s\n",
875 ldap_scherr2str(code), err, desc );
879 code = mr_add( mr, convert, normalize, match, indexer, filter, &err );
881 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s for %s in %s\n",
882 scherr2str(code), err, desc );
889 #if defined( SLAPD_SCHEMA_DN )
892 syn_schema_info( Entry *e )
895 struct berval *vals[2];
901 for ( syn = syn_list; syn; syn = syn->ssyn_next ) {
902 val.bv_val = ldap_syntax2str( &syn->ssyn_syn );
904 val.bv_len = strlen( val.bv_val );
905 Debug( LDAP_DEBUG_TRACE, "Merging syn [%ld] %s\n",
906 (long) val.bv_len, val.bv_val, 0 );
907 attr_merge( e, "ldapSyntaxes", vals );
908 ldap_memfree( val.bv_val );
917 mr_schema_info( Entry *e )
920 struct berval *vals[2];
926 for ( mr = mr_list; mr; mr = mr->smr_next ) {
927 val.bv_val = ldap_matchingrule2str( &mr->smr_mrule );
929 val.bv_len = strlen( val.bv_val );
930 Debug( LDAP_DEBUG_TRACE, "Merging mr [%ld] %s\n",
931 (long) val.bv_len, val.bv_val, 0 );
932 attr_merge( e, "matchingRules", vals );
933 ldap_memfree( val.bv_val );
942 oc_schema_info( Entry *e )
945 struct berval *vals[2];
951 for ( oc = oc_list; oc; oc = oc->soc_next ) {
952 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
954 val.bv_len = strlen( val.bv_val );
955 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
956 (long) val.bv_len, val.bv_val, 0 );
957 attr_merge( e, "objectClasses", vals );
958 ldap_memfree( val.bv_val );
967 schema_info( Connection *conn, Operation *op, char **attrs, int attrsonly )
971 struct berval *vals[2];
976 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
979 e->e_dn = ch_strdup( SLAPD_SCHEMA_DN );
980 e->e_ndn = ch_strdup( SLAPD_SCHEMA_DN );
981 (void) dn_normalize( e->e_ndn );
985 char *rdn = ch_strdup( SLAPD_SCHEMA_DN );
986 val.bv_val = strchr( rdn, '=' );
988 if( val.bv_val != NULL ) {
990 val.bv_len = strlen( ++val.bv_val );
992 attr_merge( e, rdn, vals );
998 if ( syn_schema_info( e ) ) {
999 /* Out of memory, do something about it */
1003 if ( mr_schema_info( e ) ) {
1004 /* Out of memory, do something about it */
1008 if ( at_schema_info( e ) ) {
1009 /* Out of memory, do something about it */
1013 if ( oc_schema_info( e ) ) {
1014 /* Out of memory, do something about it */
1020 val.bv_len = sizeof("top")-1;
1021 attr_merge( e, "objectClass", vals );
1023 val.bv_val = "LDAPsubentry";
1024 val.bv_len = sizeof("LDAPsubentry")-1;
1025 attr_merge( e, "objectClass", vals );
1027 val.bv_val = "subschema";
1028 val.bv_len = sizeof("subschema")-1;
1029 attr_merge( e, "objectClass", vals );
1031 val.bv_val = "extensibleObject";
1032 val.bv_len = sizeof("extensibleObject")-1;
1033 attr_merge( e, "objectClass", vals );
1035 send_search_entry( &backends[0], conn, op,
1036 e, attrs, attrsonly, NULL );
1037 send_search_result( conn, op, LDAP_SUCCESS,
1038 NULL, NULL, NULL, NULL, 1 );
1047 oc_print( ObjectClass *oc )
1052 printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
1053 if ( oc->soc_required != NULL ) {
1054 mid = "\trequires ";
1055 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
1056 printf( "%s%s", mid,
1057 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
1060 if ( oc->soc_allowed != NULL ) {
1062 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
1063 printf( "%s%s", mid,
1064 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
1071 int is_entry_objectclass(
1078 if( e == NULL || oc == NULL || *oc == '\0' )
1082 * find objectClass attribute
1084 attr = attr_find(e->e_attrs, "objectclass");
1086 if( attr == NULL ) {
1087 /* no objectClass attribute */
1091 bv.bv_val = (char *) oc;
1092 bv.bv_len = strlen( bv.bv_val );
1094 #ifdef SLAPD_SCHEMA_COMPAT
1095 if( value_find(attr->a_vals, &bv, attr->a_syntax, 1) != 0) {
1096 /* entry is not of this objectclass */