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 #ifndef SLAPD_SCHEMA_NOT_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 != LDAP_SCHEMA_USER_APPLICATIONS;
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 ) {
266 #ifdef SLAPD_SCHEMA_COMPAT
267 /* Treat any attribute type with option as an unknown attribute type */
269 * The "type" we have received is actually an AttributeDescription.
270 * Let's find out the corresponding type.
272 p = strchr( type, ';' );
274 t = ch_malloc( p-type+1 );
275 strncpy( t, type, p-type );
277 Debug( LDAP_DEBUG_TRACE,
278 "oc_check_allowed type \"%s\" from \"%s\"\n",
289 * All operational attributions are allowed by schema rules.
291 if ( oc_check_op_attr( t ) ) {
295 /* check that the type appears as req or opt in at least one oc */
296 for ( i = 0; ocl[i] != NULL; i++ ) {
297 /* if we know about the oc */
298 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
299 /* does it require the type? */
300 for ( j = 0; oc->soc_required != NULL &&
301 oc->soc_required[j] != NULL; j++ ) {
302 at = oc->soc_required[j];
304 strcmp(at->sat_oid, t ) == 0 ) {
313 if ( strcasecmp( *pp, t ) == 0 ) {
321 /* does it allow the type? */
322 for ( j = 0; oc->soc_allowed != NULL &&
323 oc->soc_allowed[j] != NULL; j++ ) {
324 at = oc->soc_allowed[j];
326 strcmp( at->sat_oid, t ) == 0 ) {
335 if ( strcasecmp( *pp, t ) == 0 ||
336 strcmp( *pp, "*" ) == 0 ) {
344 /* maybe the next oc allows it */
346 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
347 /* we don't know about the oc. assume it allows it */
358 /* not allowed by any oc */
367 static Avlnode *oc_index = NULL;
368 static ObjectClass *oc_list = NULL;
372 struct oindexrec *oir1,
373 struct oindexrec *oir2
376 return (strcasecmp( oir1->oir_name, oir2->oir_name ));
382 struct oindexrec *oir
385 return (strcasecmp( name, oir->oir_name ));
389 oc_find( const char *ocname )
391 struct oindexrec *oir = NULL;
393 if ( (oir = (struct oindexrec *) avl_find( oc_index, ocname,
394 (AVL_CMP) oc_index_name_cmp )) != NULL ) {
395 return( oir->oir_oc );
409 AttributeType **satp;
415 sat = at_find(*attrs1);
418 return SLAP_SCHERR_ATTR_NOT_FOUND;
420 if ( at_find_in_list(sat, soc->soc_required) < 0) {
421 if ( at_append_to_list(sat, &soc->soc_required) ) {
423 return SLAP_SCHERR_OUTOFMEM;
428 /* Now delete duplicates from the allowed list */
429 for ( satp = soc->soc_required; *satp; satp++ ) {
430 i = at_find_in_list(*satp,soc->soc_allowed);
432 at_delete_from_list(i, &soc->soc_allowed);
452 sat = at_find(*attrs1);
455 return SLAP_SCHERR_ATTR_NOT_FOUND;
457 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
458 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
459 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
461 return SLAP_SCHERR_OUTOFMEM;
484 if ( !soc->soc_sups ) {
485 /* We are at the first recursive level */
494 soc->soc_sups = (ObjectClass **)ch_calloc(1,
495 nsups*sizeof(ObjectClass *));
500 soc1 = oc_find(*sups1);
503 return SLAP_SCHERR_CLASS_NOT_FOUND;
507 soc->soc_sups[nsups] = soc1;
509 code = oc_add_sups(soc,soc1->soc_sup_oids, err);
513 code = oc_create_required(soc,soc1->soc_at_oids_must,err);
516 code = oc_create_allowed(soc,soc1->soc_at_oids_may,err);
534 struct oindexrec *oir;
538 while ( *ocp != NULL ) {
539 ocp = &(*ocp)->soc_next;
543 if ( soc->soc_oid ) {
544 oir = (struct oindexrec *)
545 ch_calloc( 1, sizeof(struct oindexrec) );
546 oir->oir_name = soc->soc_oid;
548 if ( avl_insert( &oc_index, (caddr_t) oir,
549 (AVL_CMP) oc_index_cmp,
550 (AVL_DUP) avl_dup_error ) ) {
553 return SLAP_SCHERR_DUP_CLASS;
555 /* FIX: temporal consistency check */
556 oc_find(oir->oir_name);
558 if ( (names = soc->soc_names) ) {
560 oir = (struct oindexrec *)
561 ch_calloc( 1, sizeof(struct oindexrec) );
562 oir->oir_name = ch_strdup(*names);
564 if ( avl_insert( &oc_index, (caddr_t) oir,
565 (AVL_CMP) oc_index_cmp,
566 (AVL_DUP) avl_dup_error ) ) {
569 return SLAP_SCHERR_DUP_CLASS;
571 /* FIX: temporal consistency check */
572 oc_find(oir->oir_name);
581 LDAP_OBJECT_CLASS *oc,
588 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
589 memcpy( &soc->soc_oclass, oc, sizeof(LDAP_OBJECT_CLASS));
590 if ( (code = oc_add_sups(soc,soc->soc_sup_oids,err)) != 0 )
592 if ( (code = oc_create_required(soc,soc->soc_at_oids_must,err)) != 0 )
594 if ( (code = oc_create_allowed(soc,soc->soc_at_oids_may,err)) != 0 )
596 code = oc_insert(soc,err);
605 static Avlnode *syn_index = NULL;
606 static Syntax *syn_list = NULL;
610 struct sindexrec *sir1,
611 struct sindexrec *sir2
614 return (strcmp( sir1->sir_name, sir2->sir_name ));
620 struct sindexrec *sir
623 return (strcmp( name, sir->sir_name ));
627 syn_find( const char *synname )
629 struct sindexrec *sir = NULL;
631 if ( (sir = (struct sindexrec *) avl_find( syn_index, synname,
632 (AVL_CMP) syn_index_name_cmp )) != NULL ) {
633 return( sir->sir_syn );
639 syn_find_desc( const char *syndesc, int *len )
643 for (synp = syn_list; synp; synp = synp->ssyn_next)
644 if ((*len = dscompare( synp->ssyn_syn.syn_desc, syndesc, '{')))
656 struct sindexrec *sir;
659 while ( *synp != NULL ) {
660 synp = &(*synp)->ssyn_next;
664 if ( ssyn->ssyn_oid ) {
665 sir = (struct sindexrec *)
666 ch_calloc( 1, sizeof(struct sindexrec) );
667 sir->sir_name = ssyn->ssyn_oid;
669 if ( avl_insert( &syn_index, (caddr_t) sir,
670 (AVL_CMP) syn_index_cmp,
671 (AVL_DUP) avl_dup_error ) ) {
672 *err = ssyn->ssyn_oid;
674 return SLAP_SCHERR_DUP_SYNTAX;
676 /* FIX: temporal consistency check */
677 syn_find(sir->sir_name);
686 slap_syntax_validate_func *validate,
687 slap_syntax_transform_func *ber2str,
688 slap_syntax_transform_func *str2ber,
695 ssyn = (Syntax *) ch_calloc( 1, sizeof(Syntax) );
696 memcpy( &ssyn->ssyn_syn, syn, sizeof(LDAP_SYNTAX));
698 ssyn->ssyn_flags = flags;
699 ssyn->ssyn_validate = validate;
700 ssyn->ssyn_ber2str = ber2str;
701 ssyn->ssyn_str2ber = str2ber;
703 code = syn_insert(ssyn,err);
709 MatchingRule *mir_mr;
712 static Avlnode *mr_index = NULL;
713 static MatchingRule *mr_list = NULL;
717 struct mindexrec *mir1,
718 struct mindexrec *mir2
721 return (strcmp( mir1->mir_name, mir2->mir_name ));
727 struct mindexrec *mir
730 return (strcmp( name, mir->mir_name ));
734 mr_find( const char *mrname )
736 struct mindexrec *mir = NULL;
738 if ( (mir = (struct mindexrec *) avl_find( mr_index, mrname,
739 (AVL_CMP) mr_index_name_cmp )) != NULL ) {
740 return( mir->mir_mr );
752 struct mindexrec *mir;
756 while ( *mrp != NULL ) {
757 mrp = &(*mrp)->smr_next;
761 if ( smr->smr_oid ) {
762 mir = (struct mindexrec *)
763 ch_calloc( 1, sizeof(struct mindexrec) );
764 mir->mir_name = smr->smr_oid;
766 if ( avl_insert( &mr_index, (caddr_t) mir,
767 (AVL_CMP) mr_index_cmp,
768 (AVL_DUP) avl_dup_error ) ) {
771 return SLAP_SCHERR_DUP_RULE;
773 /* FIX: temporal consistency check */
774 mr_find(mir->mir_name);
776 if ( (names = smr->smr_names) ) {
778 mir = (struct mindexrec *)
779 ch_calloc( 1, sizeof(struct mindexrec) );
780 mir->mir_name = ch_strdup(*names);
782 if ( avl_insert( &mr_index, (caddr_t) mir,
783 (AVL_CMP) mr_index_cmp,
784 (AVL_DUP) avl_dup_error ) ) {
787 return SLAP_SCHERR_DUP_RULE;
789 /* FIX: temporal consistency check */
790 mr_find(mir->mir_name);
799 LDAP_MATCHING_RULE *mr,
800 slap_mr_convert_func *convert,
801 slap_mr_normalize_func *normalize,
802 slap_mr_match_func *match,
803 slap_mr_indexer_func *indexer,
804 slap_mr_filter_func *filter,
812 smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
813 memcpy( &smr->smr_mrule, mr, sizeof(LDAP_MATCHING_RULE));
815 smr->smr_convert = convert;
816 smr->smr_normalize = normalize;
817 smr->smr_match = match;
818 smr->smr_indexer = indexer;
819 smr->smr_filter = filter;
821 if ( smr->smr_syntax_oid ) {
822 if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
823 smr->smr_syntax = syn;
825 *err = smr->smr_syntax_oid;
826 return SLAP_SCHERR_SYN_NOT_FOUND;
830 return SLAP_SCHERR_MR_INCOMPLETE;
832 code = mr_insert(smr,err);
838 char * desc, int flags,
839 slap_syntax_validate_func *validate,
840 slap_syntax_transform_func *ber2str,
841 slap_syntax_transform_func *str2ber )
847 syn = ldap_str2syntax( desc, &code, &err);
849 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
850 ldap_scherr2str(code), err, desc );
854 code = syn_add( syn, flags, validate, ber2str, str2ber, &err );
856 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s %s in %s\n",
857 scherr2str(code), err, desc );
865 register_matching_rule(
867 slap_mr_convert_func *convert,
868 slap_mr_normalize_func *normalize,
869 slap_mr_match_func *match,
870 slap_mr_indexer_func *indexer,
871 slap_mr_filter_func *filter )
873 LDAP_MATCHING_RULE *mr;
877 mr = ldap_str2matchingrule( desc, &code, &err);
879 Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s before %s in %s\n",
880 ldap_scherr2str(code), err, desc );
884 code = mr_add( mr, convert, normalize, match, indexer, filter, &err );
886 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s for %s in %s\n",
887 scherr2str(code), err, desc );
894 #if defined( SLAPD_SCHEMA_DN )
897 syn_schema_info( Entry *e )
900 struct berval *vals[2];
906 for ( syn = syn_list; syn; syn = syn->ssyn_next ) {
907 val.bv_val = ldap_syntax2str( &syn->ssyn_syn );
909 val.bv_len = strlen( val.bv_val );
910 Debug( LDAP_DEBUG_TRACE, "Merging syn [%ld] %s\n",
911 (long) val.bv_len, val.bv_val, 0 );
912 attr_merge( e, "ldapSyntaxes", vals );
913 ldap_memfree( val.bv_val );
922 mr_schema_info( Entry *e )
925 struct berval *vals[2];
931 for ( mr = mr_list; mr; mr = mr->smr_next ) {
932 val.bv_val = ldap_matchingrule2str( &mr->smr_mrule );
934 val.bv_len = strlen( val.bv_val );
935 Debug( LDAP_DEBUG_TRACE, "Merging mr [%ld] %s\n",
936 (long) val.bv_len, val.bv_val, 0 );
937 attr_merge( e, "matchingRules", vals );
938 ldap_memfree( val.bv_val );
947 oc_schema_info( Entry *e )
950 struct berval *vals[2];
956 for ( oc = oc_list; oc; oc = oc->soc_next ) {
957 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
959 val.bv_len = strlen( val.bv_val );
960 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
961 (long) val.bv_len, val.bv_val, 0 );
962 attr_merge( e, "objectClasses", vals );
963 ldap_memfree( val.bv_val );
972 schema_info( Connection *conn, Operation *op, char **attrs, int attrsonly )
976 struct berval *vals[2];
981 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
984 e->e_dn = ch_strdup( SLAPD_SCHEMA_DN );
985 e->e_ndn = ch_strdup( SLAPD_SCHEMA_DN );
986 (void) dn_normalize( e->e_ndn );
990 char *rdn = ch_strdup( SLAPD_SCHEMA_DN );
991 val.bv_val = strchr( rdn, '=' );
993 if( val.bv_val != NULL ) {
995 val.bv_len = strlen( ++val.bv_val );
997 attr_merge( e, rdn, vals );
1003 if ( syn_schema_info( e ) ) {
1004 /* Out of memory, do something about it */
1008 if ( mr_schema_info( e ) ) {
1009 /* Out of memory, do something about it */
1013 if ( at_schema_info( e ) ) {
1014 /* Out of memory, do something about it */
1018 if ( oc_schema_info( e ) ) {
1019 /* Out of memory, do something about it */
1025 val.bv_len = sizeof("top")-1;
1026 attr_merge( e, "objectClass", vals );
1028 val.bv_val = "LDAPsubentry";
1029 val.bv_len = sizeof("LDAPsubentry")-1;
1030 attr_merge( e, "objectClass", vals );
1032 val.bv_val = "subschema";
1033 val.bv_len = sizeof("subschema")-1;
1034 attr_merge( e, "objectClass", vals );
1036 val.bv_val = "extensibleObject";
1037 val.bv_len = sizeof("extensibleObject")-1;
1038 attr_merge( e, "objectClass", vals );
1040 send_search_entry( &backends[0], conn, op,
1041 e, attrs, attrsonly, NULL );
1042 send_search_result( conn, op, LDAP_SUCCESS,
1043 NULL, NULL, NULL, NULL, 1 );
1052 oc_print( ObjectClass *oc )
1057 printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
1058 if ( oc->soc_required != NULL ) {
1059 mid = "\trequires ";
1060 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
1061 printf( "%s%s", mid,
1062 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
1065 if ( oc->soc_allowed != NULL ) {
1067 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
1068 printf( "%s%s", mid,
1069 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
1076 int is_entry_objectclass(
1083 if( e == NULL || oc == NULL || *oc == '\0' )
1087 * find objectClass attribute
1089 attr = attr_find(e->e_attrs, "objectclass");
1091 if( attr == NULL ) {
1092 /* no objectClass attribute */
1096 bv.bv_val = (char *) oc;
1097 bv.bv_len = strlen( bv.bv_val );
1099 #ifdef SLAPD_SCHEMA_COMPAT
1100 if( value_find(attr->a_vals, &bv, attr->a_syntax, 1) != 0) {
1101 /* entry is not of this objectclass */