1 /* schema_check.c - routines to enforce schema definitions */
4 * Copyright 1998-2000 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, struct berval *ocname);
22 * entry_schema_check - check that entry e conforms to the schema required
23 * by its object class(es).
25 * returns 0 if so, non-zero otherwise.
30 Entry *e, Attribute *oldattrs, const char** text )
36 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
39 if( !global_schemacheck ) return LDAP_SUCCESS;
41 /* find the object class attribute - could error out here */
42 if ( (aoc = attr_find( e->e_attrs, ad_objectClass )) == NULL ) {
44 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
45 "entry_schema_check: No objectClass for entry (%s).\n", e->e_dn ));
47 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
51 *text = "no objectClass attribute";
52 return LDAP_OBJECT_CLASS_VIOLATION;
55 /* check that the entry has required attrs for each oc */
56 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
57 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
59 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
60 "entry_schema_check: dn (%s), objectClass \"%s\" not recognized\n",
61 e->e_dn, aoc->a_vals[i]->bv_val ));
63 Debug( LDAP_DEBUG_ANY,
64 "entry_check_schema(%s): objectClass \"%s\" not recognized\n",
65 e->e_dn, aoc->a_vals[i]->bv_val, 0 );
68 *text = "unrecognized object class";
69 return LDAP_OBJECT_CLASS_VIOLATION;
72 char *s = oc_check_required( e, aoc->a_vals[i] );
76 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
77 "entry_schema_check: dn (%s) oc \"%s\" requires att \"%s\"\n",
78 e->e_dn, aoc->a_vals[i]->bv_val, s ));
80 Debug( LDAP_DEBUG_ANY,
81 "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
82 e->e_dn, aoc->a_vals[i]->bv_val, s );
85 *text = "missing required attribute";
86 return LDAP_OBJECT_CLASS_VIOLATION;
89 if( oc == slap_schema.si_oc_extensibleObject ) {
103 /* check that each attr in the entry is allowed by some oc */
104 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
105 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
107 char *type = a->a_desc->ad_cname->bv_val;
109 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
110 "entry_schema_check: Entry (%s) attr \"%s\" not allowed.\n",
113 Debug( LDAP_DEBUG_ANY,
114 "Entry (%s), attr \"%s\" not allowed\n",
118 *text = "attribute not allowed";
127 oc_check_required( Entry *e, struct berval *ocname )
135 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
136 "oc_check_required: dn (%s), objectClass \"%s\"\n",
137 e->e_dn, ocname->bv_val ));
139 Debug( LDAP_DEBUG_TRACE,
140 "oc_check_required entry (%s), objectClass \"%s\"\n",
141 e->e_dn, ocname->bv_val, 0 );
145 /* find global oc defn. it we don't know about it assume it's ok */
146 if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
150 /* check for empty oc_required */
151 if(oc->soc_required == NULL) {
155 /* for each required attribute */
156 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
157 at = oc->soc_required[i];
158 /* see if it's in the entry */
159 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
160 if( a->a_desc->ad_type == at ) {
164 /* not there => schema violation */
166 return at->sat_cname;
173 int oc_check_allowed(
175 struct berval **ocl )
181 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
182 "oc_check_allowed: type \"%s\"\n", at->sat_cname ));
184 Debug( LDAP_DEBUG_TRACE,
185 "oc_check_allowed type \"%s\"\n",
186 at->sat_cname, 0, 0 );
190 /* always allow objectClass attribute */
191 if ( strcasecmp( at->sat_cname, "objectClass" ) == 0 ) {
197 * All operational attributions are allowed by schema rules.
199 if( is_at_operational(at) ) {
203 /* check that the type appears as req or opt in at least one oc */
204 for ( i = 0; ocl[i] != NULL; i++ ) {
205 /* if we know about the oc */
206 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
207 /* does it require the type? */
208 for ( j = 0; oc->soc_required != NULL &&
209 oc->soc_required[j] != NULL; j++ )
211 if( at == oc->soc_required[j] ) {
215 /* does it allow the type? */
216 for ( j = 0; oc->soc_allowed != NULL &&
217 oc->soc_allowed[j] != NULL; j++ )
219 if( at == oc->soc_allowed[j] ) {
223 /* maybe the next oc allows it */
225 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
226 /* we don't know about the oc. assume it allows it */
236 /* not allowed by any oc */
237 return LDAP_OBJECT_CLASS_VIOLATION;