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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
20 static int oc_check_allowed(
22 struct berval **oclist );
24 static int oc_check_allowed(char *type, struct berval **oclist);
26 static char * oc_check_required(Entry *e, struct berval *ocname);
29 * entry_schema_check - check that entry e conforms to the schema required
30 * by its object class(es).
32 * returns 0 if so, non-zero otherwise.
37 Entry *e, Attribute *oldattrs, const char** text )
43 #ifdef SLAPD_SCHEMA_NOT_COMPAT
44 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
46 static const char *ad_objectClass = "objectclass";
49 if( !global_schemacheck ) return LDAP_SUCCESS;
51 /* find the object class attribute - could error out here */
52 if ( (aoc = attr_find( e->e_attrs, ad_objectClass )) == NULL ) {
53 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
55 *text = "no objectclass attribute";
56 return oldattrs != NULL
57 ? LDAP_OBJECT_CLASS_VIOLATION
58 : LDAP_NO_OBJECT_CLASS_MODS;
63 /* check that the entry has required attrs for each oc */
64 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
65 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
66 Debug( LDAP_DEBUG_ANY,
67 "entry_check_schema(%s): objectclass \"%s\" not defined\n",
68 e->e_dn, aoc->a_vals[i]->bv_val, 0 );
71 char *s = oc_check_required( e, aoc->a_vals[i] );
74 Debug( LDAP_DEBUG_ANY,
75 "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
76 e->e_dn, aoc->a_vals[i]->bv_val, s );
77 *text = "missing required attribute";
78 ret = LDAP_OBJECT_CLASS_VIOLATION;
84 if ( ret != LDAP_SUCCESS ) {
88 /* check that each attr in the entry is allowed by some oc */
89 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
90 #ifdef SLAPD_SCHEMA_NOT_COMPAT
91 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
93 ret = oc_check_allowed( a->a_type, aoc->a_vals );
96 #ifdef SLAPD_SCHEMA_NOT_COMPAT
97 char *type = a->a_desc->ad_cname->bv_val;
99 char *type = a->a_type;
101 Debug( LDAP_DEBUG_ANY,
102 "Entry (%s), attr \"%s\" not allowed\n",
104 *text = "attribute not allowed";
113 oc_check_required( Entry *e, struct berval *ocname )
120 Debug( LDAP_DEBUG_TRACE,
121 "oc_check_required entry (%s), objectclass \"%s\"\n",
122 e->e_dn, ocname->bv_val, 0 );
124 /* find global oc defn. it we don't know about it assume it's ok */
125 if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
129 /* check for empty oc_required */
130 if(oc->soc_required == NULL) {
134 /* for each required attribute */
135 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
136 at = oc->soc_required[i];
137 /* see if it's in the entry */
138 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
139 #ifdef SLAPD_SCHEMA_NOT_COMPAT
140 if( a->a_desc->ad_type == at ) {
147 strcmp( a->a_type, at->sat_oid ) == 0 ) {
152 /* Empty name list => not found */
157 if ( strcasecmp( a->a_type, *pp ) == 0 ) {
167 /* not there => schema violation */
169 #ifdef SLAPD_SCHEMA_NOT_COMPAT
170 return at->sat_cname;
172 if ( at->sat_names && at->sat_names[0] ) {
173 return at->sat_names[0];
186 #ifdef SLAPD_SCHEMA_NOT_COMPAT
191 struct berval **ocl )
196 #ifdef SLAPD_SCHEMA_NOT_COMPAT
197 Debug( LDAP_DEBUG_TRACE,
198 "oc_check_allowed type \"%s\"\n",
199 at->sat_cname, 0, 0 );
201 /* always allow objectclass attribute */
202 if ( strcasecmp( at->sat_cname, "objectclass" ) == 0 ) {
212 Debug( LDAP_DEBUG_TRACE,
213 "oc_check_allowed type \"%s\"\n", type, 0, 0 );
215 /* always allow objectclass attribute */
216 if ( strcasecmp( type, "objectclass" ) == 0 ) {
221 #ifdef SLAPD_SCHEMA_NOT_COMPAT
223 * All operational attributions are allowed by schema rules.
225 if( is_at_operational(at) ) {
230 * The "type" we have received is actually an AttributeDescription.
231 * Let's find out the corresponding type.
233 p = strchr( type, ';' );
235 t = ch_malloc( p-type+1 );
236 strncpy( t, type, p-type );
238 Debug( LDAP_DEBUG_TRACE,
239 "oc_check_allowed type \"%s\" from \"%s\"\n",
248 * All operational attributions are allowed by schema rules.
250 if ( oc_check_op_attr( t ) ) {
255 /* check that the type appears as req or opt in at least one oc */
256 for ( i = 0; ocl[i] != NULL; i++ ) {
257 /* if we know about the oc */
258 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
259 /* does it require the type? */
260 for ( j = 0; oc->soc_required != NULL &&
261 oc->soc_required[j] != NULL; j++ )
263 #ifdef SLAPD_SCHEMA_NOT_COMPAT
264 if( at == oc->soc_required[j] ) {
268 at = oc->soc_required[j];
270 strcmp(at->sat_oid, t ) == 0 ) {
279 if ( strcasecmp( *pp, t ) == 0 ) {
288 /* does it allow the type? */
289 for ( j = 0; oc->soc_allowed != NULL &&
290 oc->soc_allowed[j] != NULL; j++ )
292 #ifdef SLAPD_SCHEMA_NOT_COMPAT
293 if( at == oc->soc_allowed[j] ) {
297 at = oc->soc_allowed[j];
299 strcmp( at->sat_oid, t ) == 0 ) {
308 if ( strcasecmp( *pp, t ) == 0 ||
309 strcmp( *pp, "*" ) == 0 ) {
318 /* maybe the next oc allows it */
320 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
321 /* we don't know about the oc. assume it allows it */
330 #ifndef SLAPD_SCHEMA_NOT_COMPAT
335 /* not allowed by any oc */
336 return LDAP_OBJECT_CLASS_VIOLATION;