1 /* schema.c - routines to enforce schema definitions */
12 static struct objclass *oc_find(char *ocname);
13 static int oc_check_required(Entry *e, char *ocname);
14 static int oc_check_allowed(char *type, struct berval **ocl);
17 * oc_check - check that entry e conforms to the schema required by
18 * its object class(es). returns 0 if so, non-zero otherwise.
22 oc_schema_check( Entry *e )
29 /* find the object class attribute - could error out here */
30 if ( (aoc = attr_find( e->e_attrs, "objectclass" )) == NULL ) {
31 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
36 /* check that the entry has required attrs for each oc */
37 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
38 if ( oc_check_required( e, aoc->a_vals[i]->bv_val ) != 0 ) {
39 Debug( LDAP_DEBUG_ANY,
40 "Entry (%s), required attr (%s) missing\n",
41 e->e_dn, aoc->a_vals[i]->bv_val, 0 );
50 /* check that each attr in the entry is allowed by some oc */
51 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
52 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
53 Debug( LDAP_DEBUG_ANY,
54 "Entry (%s), attr (%s) not allowed\n",
55 e->e_dn, a->a_type, 0 );
64 oc_check_required( Entry *e, char *ocname )
70 /* find global oc defn. it we don't know about it assume it's ok */
71 if ( (oc = oc_find( ocname )) == NULL ) {
75 /* check for empty oc_required */
76 if(oc->oc_required == NULL) {
80 /* for each required attribute */
81 for ( i = 0; oc->oc_required[i] != NULL; i++ ) {
82 /* see if it's in the entry */
83 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
84 if ( strcasecmp( a->a_type, oc->oc_required[i] )
90 /* not there => schema violation */
100 oc_check_allowed( char *type, struct berval **ocl )
105 /* always allow objectclass attribute */
106 if ( strcasecmp( type, "objectclass" ) == 0 ) {
110 /* check that the type appears as req or opt in at least one oc */
111 for ( i = 0; ocl[i] != NULL; i++ ) {
112 /* if we know about the oc */
113 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
114 /* does it require the type? */
115 for ( j = 0; oc->oc_required != NULL &&
116 oc->oc_required[j] != NULL; j++ ) {
117 if ( strcasecmp( oc->oc_required[j], type )
122 /* does it allow the type? */
123 for ( j = 0; oc->oc_allowed != NULL &&
124 oc->oc_allowed[j] != NULL; j++ ) {
125 if ( strcasecmp( oc->oc_allowed[j], type )
126 == 0 || strcmp( oc->oc_allowed[j], "*" )
132 /* maybe the next oc allows it */
134 /* we don't know about the oc. assume it allows it */
140 /* not allowed by any oc */
144 static struct objclass *
145 oc_find( char *ocname )
149 for ( oc = global_oc; oc != NULL; oc = oc->oc_next ) {
150 if ( strcasecmp( oc->oc_name, ocname ) == 0 ) {
161 oc_print( struct objclass *oc )
165 printf( "objectclass %s\n", oc->oc_name );
166 if ( oc->oc_required != NULL ) {
167 printf( "\trequires %s", oc->oc_required[0] );
168 for ( i = 1; oc->oc_required[i] != NULL; i++ ) {
169 printf( ",%s", oc->oc_required[i] );
173 if ( oc->oc_allowed != NULL ) {
174 printf( "\tallows %s", oc->oc_allowed[0] );
175 for ( i = 1; oc->oc_allowed[i] != NULL; i++ ) {
176 printf( ",%s", oc->oc_allowed[i] );