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 #ifndef SLAPD_SCHEMA_NOT_COMPAT
20 static int oc_check_allowed(char *type, struct berval **oclist);
22 static char * oc_check_required(Entry *e, struct berval *ocname);
25 * entry_schema_check - check that entry e conforms to the schema required
26 * by its object class(es).
28 * returns 0 if so, non-zero otherwise.
33 Entry *e, Attribute *oldattrs, const char** text )
39 #ifdef SLAPD_SCHEMA_NOT_COMPAT
40 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
42 static const char *ad_objectClass = "objectclass";
45 if( !global_schemacheck ) return LDAP_SUCCESS;
47 /* find the object class attribute - could error out here */
48 if ( (aoc = attr_find( e->e_attrs, ad_objectClass )) == NULL ) {
49 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
51 *text = "no objectclass attribute";
52 return oldattrs != NULL
53 ? LDAP_OBJECT_CLASS_VIOLATION
54 : LDAP_NO_OBJECT_CLASS_MODS;
59 /* check that the entry has required attrs for each oc */
60 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
61 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
62 Debug( LDAP_DEBUG_ANY,
63 "entry_check_schema(%s): objectclass \"%s\" not defined\n",
64 e->e_dn, aoc->a_vals[i]->bv_val, 0 );
67 char *s = oc_check_required( e, aoc->a_vals[i] );
70 Debug( LDAP_DEBUG_ANY,
71 "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
72 e->e_dn, aoc->a_vals[i]->bv_val, s );
73 *text = "missing required attribute";
74 ret = LDAP_OBJECT_CLASS_VIOLATION;
80 if ( ret != LDAP_SUCCESS ) {
84 /* check that each attr in the entry is allowed by some oc */
85 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
86 #ifdef SLAPD_SCHEMA_NOT_COMPAT
87 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
89 ret = oc_check_allowed( a->a_type, aoc->a_vals );
92 #ifdef SLAPD_SCHEMA_NOT_COMPAT
93 char *type = a->a_desc->ad_cname->bv_val;
95 char *type = a->a_type;
97 Debug( LDAP_DEBUG_ANY,
98 "Entry (%s), attr \"%s\" not allowed\n",
100 *text = "attribute not allowed";
109 oc_check_required( Entry *e, struct berval *ocname )
116 Debug( LDAP_DEBUG_TRACE,
117 "oc_check_required entry (%s), objectclass \"%s\"\n",
118 e->e_dn, ocname->bv_val, 0 );
120 /* find global oc defn. it we don't know about it assume it's ok */
121 if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
125 /* check for empty oc_required */
126 if(oc->soc_required == NULL) {
130 /* for each required attribute */
131 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
132 at = oc->soc_required[i];
133 /* see if it's in the entry */
134 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
135 #ifdef SLAPD_SCHEMA_NOT_COMPAT
136 if( a->a_desc->ad_type == at ) {
143 strcmp( a->a_type, at->sat_oid ) == 0 ) {
148 /* Empty name list => not found */
153 if ( strcasecmp( a->a_type, *pp ) == 0 ) {
163 /* not there => schema violation */
165 #ifdef SLAPD_SCHEMA_NOT_COMPAT
166 return at->sat_cname;
168 if ( at->sat_names && at->sat_names[0] ) {
169 return at->sat_names[0];
180 #ifndef SLAPD_SCHEMA_NOT_COMPAT
183 int oc_check_allowed(
184 #ifdef SLAPD_SCHEMA_NOT_COMPAT
189 struct berval **ocl )
194 #ifdef SLAPD_SCHEMA_NOT_COMPAT
195 Debug( LDAP_DEBUG_TRACE,
196 "oc_check_allowed type \"%s\"\n",
197 at->sat_cname, 0, 0 );
199 /* always allow objectclass attribute */
200 if ( strcasecmp( at->sat_cname, "objectclass" ) == 0 ) {
210 Debug( LDAP_DEBUG_TRACE,
211 "oc_check_allowed type \"%s\"\n", type, 0, 0 );
213 /* always allow objectclass attribute */
214 if ( strcasecmp( type, "objectclass" ) == 0 ) {
219 #ifdef SLAPD_SCHEMA_NOT_COMPAT
221 * All operational attributions are allowed by schema rules.
223 if( is_at_operational(at) ) {
228 * The "type" we have received is actually an AttributeDescription.
229 * Let's find out the corresponding type.
231 p = strchr( type, ';' );
233 t = ch_malloc( p-type+1 );
234 strncpy( t, type, p-type );
236 Debug( LDAP_DEBUG_TRACE,
237 "oc_check_allowed type \"%s\" from \"%s\"\n",
246 * All operational attributions are allowed by schema rules.
248 if ( oc_check_op_attr( t ) ) {
253 /* check that the type appears as req or opt in at least one oc */
254 for ( i = 0; ocl[i] != NULL; i++ ) {
255 /* if we know about the oc */
256 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
257 /* does it require the type? */
258 for ( j = 0; oc->soc_required != NULL &&
259 oc->soc_required[j] != NULL; j++ )
261 #ifdef SLAPD_SCHEMA_NOT_COMPAT
262 if( at == oc->soc_required[j] ) {
266 at = oc->soc_required[j];
268 strcmp(at->sat_oid, t ) == 0 ) {
277 if ( strcasecmp( *pp, t ) == 0 ) {
286 /* does it allow the type? */
287 for ( j = 0; oc->soc_allowed != NULL &&
288 oc->soc_allowed[j] != NULL; j++ )
290 #ifdef SLAPD_SCHEMA_NOT_COMPAT
291 if( at == oc->soc_allowed[j] ) {
295 at = oc->soc_allowed[j];
297 strcmp( at->sat_oid, t ) == 0 ) {
306 if ( strcasecmp( *pp, t ) == 0 ||
307 strcmp( *pp, "*" ) == 0 ) {
316 /* maybe the next oc allows it */
318 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
319 /* we don't know about the oc. assume it allows it */
328 #ifndef SLAPD_SCHEMA_NOT_COMPAT
333 /* not allowed by any oc */
334 return LDAP_OBJECT_CLASS_VIOLATION;