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, char** text )
43 #ifdef SLAPD_SCHEMA_NOT_COMPAT
44 static AttributeDescription *objectClass = NULL;
46 static const char *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, objectClass )) == NULL ) {
53 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
56 *text = "no objectclass attribute";
57 return oldattrs != NULL
58 ? LDAP_OBJECT_CLASS_VIOLATION
59 : LDAP_NO_OBJECT_CLASS_MODS;
64 /* check that the entry has required attrs for each oc */
65 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
66 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
67 Debug( LDAP_DEBUG_ANY,
68 "Objectclass \"%s\" not defined\n",
69 aoc->a_vals[i]->bv_val, 0, 0 );
72 char *s = oc_check_required( e, aoc->a_vals[i] );
75 Debug( LDAP_DEBUG_ANY,
76 "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
77 e->e_dn, aoc->a_vals[i]->bv_val, s );
78 *text = "missing required attribute";
79 ret = LDAP_OBJECT_CLASS_VIOLATION;
85 if ( ret != LDAP_SUCCESS ) {
89 /* check that each attr in the entry is allowed by some oc */
90 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
91 #ifdef SLAPD_SCHEMA_NOT_COMPAT
92 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
94 ret = oc_check_allowed( a->a_type, aoc->a_vals );
97 #ifdef SLAPD_SCHEMA_NOT_COMPAT
98 char *type = a->a_desc->ad_cname->bv_val;
100 char *type = a->a_type;
102 Debug( LDAP_DEBUG_ANY,
103 "Entry (%s), attr \"%s\" not allowed\n",
105 *text = "attribute not allowed";
114 oc_check_required( Entry *e, struct berval *ocname )
121 Debug( LDAP_DEBUG_TRACE,
122 "oc_check_required entry (%s), objectclass \"%s\"\n",
123 e->e_dn, ocname, 0 );
125 /* find global oc defn. it we don't know about it assume it's ok */
126 if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
130 /* check for empty oc_required */
131 if(oc->soc_required == NULL) {
135 /* for each required attribute */
136 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
137 at = oc->soc_required[i];
138 /* see if it's in the entry */
139 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
140 #ifdef SLAPD_SCHEMA_NOT_COMPAT
141 if( a->a_desc->ad_type == at ) {
148 strcmp( a->a_type, at->sat_oid ) == 0 ) {
153 /* Empty name list => not found */
158 if ( strcasecmp( a->a_type, *pp ) == 0 ) {
168 /* not there => schema violation */
170 #ifdef SLAPD_SCHEMA_NOT_COMPAT
171 return at->sat_cname;
173 if ( at->sat_names && at->sat_names[0] ) {
174 return at->sat_names[0];
187 #ifdef SLAPD_SCHEMA_NOT_COMPAT
192 struct berval **ocl )
197 #ifdef SLAPD_SCHEMA_NOT_COMPAT
198 Debug( LDAP_DEBUG_TRACE,
199 "oc_check_allowed type \"%s\"\n",
200 at->sat_cname, 0, 0 );
202 /* always allow objectclass attribute */
203 if ( strcasecmp( at->sat_cname, "objectclass" ) == 0 ) {
213 Debug( LDAP_DEBUG_TRACE,
214 "oc_check_allowed type \"%s\"\n", type, 0, 0 );
216 /* always allow objectclass attribute */
217 if ( strcasecmp( type, "objectclass" ) == 0 ) {
222 #ifdef SLAPD_SCHEMA_NOT_COMPAT
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;