1 /* schema_check.c - routines to enforce schema definitions */
4 * Copyright 1998-1999 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.
36 schema_check_entry( Entry *e )
42 #ifdef SLAPD_SCHEMA_NOT_COMPAT
43 static AttributeDescription *objectClass = NULL;
45 static const char *objectClass = "objectclass";
49 if( !global_schemacheck ) return 0;
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",
58 /* check that the entry has required attrs for each oc */
59 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
60 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
61 Debug( LDAP_DEBUG_ANY,
62 "Objectclass \"%s\" not defined\n",
63 aoc->a_vals[i]->bv_val, 0, 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 );
82 /* check that each attr in the entry is allowed by some oc */
83 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
84 #ifdef SLAPD_SCHEMA_NOT_COMPAT
85 if ( oc_check_allowed( a->a_desc.ad_type, aoc->a_vals ) != 0 ) {
86 Debug( LDAP_DEBUG_ANY,
87 "Entry (%s), attr \"%s\" not allowed\n",
88 e->e_dn, a->a_desc.ad_cname->bv_val, 0 );
92 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
93 Debug( LDAP_DEBUG_ANY,
94 "Entry (%s), attr \"%s\" not allowed\n",
95 e->e_dn, a->a_type, 0 );
105 oc_check_required( Entry *e, struct berval *ocname )
112 Debug( LDAP_DEBUG_TRACE,
113 "oc_check_required entry (%s), objectclass \"%s\"\n",
114 e->e_dn, ocname, 0 );
116 /* find global oc defn. it we don't know about it assume it's ok */
117 if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
121 /* check for empty oc_required */
122 if(oc->soc_required == NULL) {
126 /* for each required attribute */
127 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
128 at = oc->soc_required[i];
129 /* see if it's in the entry */
130 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
131 #ifdef SLAPD_SCHEMA_NOT_COMPAT
132 if( a->a_desc.ad_type == at ) {
139 strcmp( a->a_type, at->sat_oid ) == 0 ) {
144 /* Empty name list => not found */
149 if ( strcasecmp( a->a_type, *pp ) == 0 ) {
159 /* not there => schema violation */
161 #ifdef SLAPD_SCHEMA_NOT_COMPAT
162 return at->sat_cname;
164 if ( at->sat_names && at->sat_names[0] ) {
165 return at->sat_names[0];
178 #ifdef SLAPD_SCHEMA_NOT_COMPAT
183 struct berval **ocl )
188 #ifdef SLAPD_SCHEMA_NOT_COMPAT
189 Debug( LDAP_DEBUG_TRACE,
190 "oc_check_allowed type \"%s\"\n",
191 at->sat_cname, 0, 0 );
193 /* always allow objectclass attribute */
194 if ( strcasecmp( at->sat_cname, "objectclass" ) == 0 ) {
204 Debug( LDAP_DEBUG_TRACE,
205 "oc_check_allowed type \"%s\"\n", type, 0, 0 );
207 /* always allow objectclass attribute */
208 if ( strcasecmp( type, "objectclass" ) == 0 ) {
213 #ifdef SLAPD_SCHEMA_NOT_COMPAT
214 if( is_at_operational(at) ) {
219 * The "type" we have received is actually an AttributeDescription.
220 * Let's find out the corresponding type.
222 p = strchr( type, ';' );
224 t = ch_malloc( p-type+1 );
225 strncpy( t, type, p-type );
227 Debug( LDAP_DEBUG_TRACE,
228 "oc_check_allowed type \"%s\" from \"%s\"\n",
237 * All operational attributions are allowed by schema rules.
239 if ( oc_check_op_attr( t ) ) {
244 /* check that the type appears as req or opt in at least one oc */
245 for ( i = 0; ocl[i] != NULL; i++ ) {
246 /* if we know about the oc */
247 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
248 /* does it require the type? */
249 for ( j = 0; oc->soc_required != NULL &&
250 oc->soc_required[j] != NULL; j++ )
252 #ifdef SLAPD_SCHEMA_NOT_COMPAT
253 if( at == oc->soc_required[j] ) {
257 at = oc->soc_required[j];
259 strcmp(at->sat_oid, t ) == 0 ) {
268 if ( strcasecmp( *pp, t ) == 0 ) {
277 /* does it allow the type? */
278 for ( j = 0; oc->soc_allowed != NULL &&
279 oc->soc_allowed[j] != NULL; j++ )
281 #ifdef SLAPD_SCHEMA_NOT_COMPAT
282 if( at == oc->soc_allowed[j] ) {
286 at = oc->soc_allowed[j];
288 strcmp( at->sat_oid, t ) == 0 ) {
297 if ( strcasecmp( *pp, t ) == 0 ||
298 strcmp( *pp, "*" ) == 0 ) {
307 /* maybe the next oc allows it */
309 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
310 /* we don't know about the oc. assume it allows it */
319 #ifndef SLAPD_SCHEMA_NOT_COMPAT
324 /* not allowed by any oc */