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",
57 *text = "no objectclass attribute";
58 return oldattrs != NULL
59 ? LDAP_OBJECT_CLASS_VIOLATION
60 : LDAP_NO_OBJECT_CLASS_MODS;
65 /* check that the entry has required attrs for each oc */
66 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
67 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
68 Debug( LDAP_DEBUG_ANY,
69 "Objectclass \"%s\" not defined\n",
70 aoc->a_vals[i]->bv_val, 0, 0 );
73 char *s = oc_check_required( e, aoc->a_vals[i] );
76 Debug( LDAP_DEBUG_ANY,
77 "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
78 e->e_dn, aoc->a_vals[i]->bv_val, s );
79 *text = "missing required attribute";
80 ret = LDAP_OBJECT_CLASS_VIOLATION;
86 if ( ret != LDAP_SUCCESS ) {
90 /* check that each attr in the entry is allowed by some oc */
91 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
92 #ifdef SLAPD_SCHEMA_NOT_COMPAT
93 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
95 ret = oc_check_allowed( a->a_type, aoc->a_vals );
98 #ifdef SLAPD_SCHEMA_NOT_COMPAT
99 char *type = a->a_desc->ad_cname->bv_val;
101 char *type = a->a_type;
103 Debug( LDAP_DEBUG_ANY,
104 "Entry (%s), attr \"%s\" not allowed\n",
106 *text = "attribute not allowed";
115 oc_check_required( Entry *e, struct berval *ocname )
122 Debug( LDAP_DEBUG_TRACE,
123 "oc_check_required entry (%s), objectclass \"%s\"\n",
124 e->e_dn, ocname, 0 );
126 /* find global oc defn. it we don't know about it assume it's ok */
127 if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
131 /* check for empty oc_required */
132 if(oc->soc_required == NULL) {
136 /* for each required attribute */
137 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
138 at = oc->soc_required[i];
139 /* see if it's in the entry */
140 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
141 #ifdef SLAPD_SCHEMA_NOT_COMPAT
142 if( a->a_desc->ad_type == at ) {
149 strcmp( a->a_type, at->sat_oid ) == 0 ) {
154 /* Empty name list => not found */
159 if ( strcasecmp( a->a_type, *pp ) == 0 ) {
169 /* not there => schema violation */
171 #ifdef SLAPD_SCHEMA_NOT_COMPAT
172 return at->sat_cname;
174 if ( at->sat_names && at->sat_names[0] ) {
175 return at->sat_names[0];
188 #ifdef SLAPD_SCHEMA_NOT_COMPAT
193 struct berval **ocl )
198 #ifdef SLAPD_SCHEMA_NOT_COMPAT
199 Debug( LDAP_DEBUG_TRACE,
200 "oc_check_allowed type \"%s\"\n",
201 at->sat_cname, 0, 0 );
203 /* always allow objectclass attribute */
204 if ( strcasecmp( at->sat_cname, "objectclass" ) == 0 ) {
214 Debug( LDAP_DEBUG_TRACE,
215 "oc_check_allowed type \"%s\"\n", type, 0, 0 );
217 /* always allow objectclass attribute */
218 if ( strcasecmp( type, "objectclass" ) == 0 ) {
223 #ifdef SLAPD_SCHEMA_NOT_COMPAT
225 * All operational attributions are allowed by schema rules.
227 if( is_at_operational(at) ) {
232 * The "type" we have received is actually an AttributeDescription.
233 * Let's find out the corresponding type.
235 p = strchr( type, ';' );
237 t = ch_malloc( p-type+1 );
238 strncpy( t, type, p-type );
240 Debug( LDAP_DEBUG_TRACE,
241 "oc_check_allowed type \"%s\" from \"%s\"\n",
250 * All operational attributions are allowed by schema rules.
252 if ( oc_check_op_attr( t ) ) {
257 /* check that the type appears as req or opt in at least one oc */
258 for ( i = 0; ocl[i] != NULL; i++ ) {
259 /* if we know about the oc */
260 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
261 /* does it require the type? */
262 for ( j = 0; oc->soc_required != NULL &&
263 oc->soc_required[j] != NULL; j++ )
265 #ifdef SLAPD_SCHEMA_NOT_COMPAT
266 if( at == oc->soc_required[j] ) {
270 at = oc->soc_required[j];
272 strcmp(at->sat_oid, t ) == 0 ) {
281 if ( strcasecmp( *pp, t ) == 0 ) {
290 /* does it allow the type? */
291 for ( j = 0; oc->soc_allowed != NULL &&
292 oc->soc_allowed[j] != NULL; j++ )
294 #ifdef SLAPD_SCHEMA_NOT_COMPAT
295 if( at == oc->soc_allowed[j] ) {
299 at = oc->soc_allowed[j];
301 strcmp( at->sat_oid, t ) == 0 ) {
310 if ( strcasecmp( *pp, t ) == 0 ||
311 strcmp( *pp, "*" ) == 0 ) {
320 /* maybe the next oc allows it */
322 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
323 /* we don't know about the oc. assume it allows it */
332 #ifndef SLAPD_SCHEMA_NOT_COMPAT
337 /* not allowed by any oc */
338 return LDAP_OBJECT_CLASS_VIOLATION;