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,
31 char *textbuf, size_t textlen )
36 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
41 /* check single-valued attrs for multiple values */
42 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
43 /* there should be at least one value */
45 assert( a->a_vals[0] != NULL );
47 /* if single value type, check for multiple values */
48 if( is_at_single_value( a->a_desc->ad_type ) &&
49 a->a_vals[1] != NULL )
51 char *type = a->a_desc->ad_cname.bv_val;
53 snprintf( textbuf, textlen,
54 "attribute '%s' cannot have multiple values",
58 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
59 "entry_schema_check: dn=\"%s\" %s\n",
62 Debug( LDAP_DEBUG_ANY,
64 e->e_dn, textbuf, 0 );
67 return LDAP_CONSTRAINT_VIOLATION;
71 if( !global_schemacheck ) return LDAP_SUCCESS;
73 /* find the object class attribute - could error out here */
74 if ( (aoc = attr_find( e->e_attrs, ad_objectClass )) == NULL ) {
76 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
77 "entry_schema_check: No objectClass for entry (%s).\n", e->e_dn ));
79 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
83 *text = "no objectClass attribute";
84 return LDAP_OBJECT_CLASS_VIOLATION;
87 /* check that the entry has required attrs for each oc */
88 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
89 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
90 snprintf( textbuf, textlen,
91 "unrecognized objectClass '%s'",
92 aoc->a_vals[i]->bv_val );
95 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
96 "entry_schema_check: dn (%s), %s\n",
99 Debug( LDAP_DEBUG_ANY,
100 "entry_check_schema(%s): \"%s\" not recognized\n",
101 e->e_dn, textbuf, 0 );
104 return LDAP_OBJECT_CLASS_VIOLATION;
107 char *s = oc_check_required( e, aoc->a_vals[i] );
110 snprintf( textbuf, textlen,
111 "object class '%s' requires attribute '%s'",
112 aoc->a_vals[i]->bv_val, s );
115 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
116 "entry_schema_check: dn=\"%s\" %s",
119 Debug( LDAP_DEBUG_ANY,
121 e->e_dn, textbuf, 0 );
124 return LDAP_OBJECT_CLASS_VIOLATION;
127 if( oc == slap_schema.si_oc_extensibleObject ) {
138 /* check that each attr in the entry is allowed by some oc */
139 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
140 int ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
141 if ( ret != LDAP_SUCCESS ) {
142 char *type = a->a_desc->ad_cname.bv_val;
144 snprintf( textbuf, textlen,
145 "attribute '%s' not allowed",
149 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
150 "entry_schema_check: dn=\"%s\" %s\n",
153 Debug( LDAP_DEBUG_ANY,
155 e->e_dn, textbuf, 0 );
166 oc_check_required( Entry *e, struct berval *ocname )
174 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
175 "oc_check_required: dn (%s), objectClass \"%s\"\n",
176 e->e_dn, ocname->bv_val ));
178 Debug( LDAP_DEBUG_TRACE,
179 "oc_check_required entry (%s), objectClass \"%s\"\n",
180 e->e_dn, ocname->bv_val, 0 );
184 /* find global oc defn. it we don't know about it assume it's ok */
185 if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
189 /* check for empty oc_required */
190 if(oc->soc_required == NULL) {
194 /* for each required attribute */
195 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
196 at = oc->soc_required[i];
197 /* see if it's in the entry */
198 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
199 if( a->a_desc->ad_type == at ) {
203 /* not there => schema violation */
205 return at->sat_cname.bv_val;
212 int oc_check_allowed(
214 struct berval **ocl )
220 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
221 "oc_check_allowed: type \"%s\"\n", at->sat_cname.bv_val ));
223 Debug( LDAP_DEBUG_TRACE,
224 "oc_check_allowed type \"%s\"\n",
225 at->sat_cname.bv_val, 0, 0 );
228 /* always allow objectClass attribute */
229 if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
234 * All operational attributions are allowed by schema rules.
236 if( is_at_operational(at) ) {
240 /* check that the type appears as req or opt in at least one oc */
241 for ( i = 0; ocl[i] != NULL; i++ ) {
242 /* if we know about the oc */
243 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
244 /* does it require the type? */
245 for ( j = 0; oc->soc_required != NULL &&
246 oc->soc_required[j] != NULL; j++ )
248 if( at == oc->soc_required[j] ) {
252 /* does it allow the type? */
253 for ( j = 0; oc->soc_allowed != NULL &&
254 oc->soc_allowed[j] != NULL; j++ )
256 if( at == oc->soc_allowed[j] ) {
260 /* maybe the next oc allows it */
264 /* not allowed by any oc */
265 return LDAP_OBJECT_CLASS_VIOLATION;