1 /* cr.c - content rule routines */
4 * Copyright 1998-2002 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 SLAP_EXTENDED_SCHEMA
22 struct berval cir_name;
26 static Avlnode *cr_index = NULL;
27 static ContentRule *cr_list = NULL;
31 struct cindexrec *cir1,
32 struct cindexrec *cir2 )
34 int i = cir1->cir_name.bv_len - cir2->cir_name.bv_len;
37 return strcasecmp( cir1->cir_name.bv_val, cir2->cir_name.bv_val );
43 struct cindexrec *cir )
45 int i = name->bv_len - cir->cir_name.bv_len;
48 return strncasecmp( name->bv_val, cir->cir_name.bv_val, name->bv_len );
52 cr_find( const char *crname )
56 bv.bv_val = (char *)crname;
57 bv.bv_len = strlen( crname );
59 return( cr_bvfind( &bv ) );
63 cr_bvfind( struct berval *crname )
65 struct cindexrec *cir;
67 cir = (struct cindexrec *) avl_find( cr_index, crname,
68 (AVL_CMP) cr_index_name_cmp );
71 return( cir->cir_cr );
82 avl_free(cr_index, ldap_memfree);
83 for (c=cr_list; c; c=n)
86 if (c->scr_auxiliaries) ldap_memfree(c->scr_auxiliaries);
87 if (c->scr_required) ldap_memfree(c->scr_required);
88 if (c->scr_allowed) ldap_memfree(c->scr_allowed);
89 if (c->scr_precluded) ldap_memfree(c->scr_precluded);
90 ldap_contentrule_free((LDAPContentRule *)c);
101 struct cindexrec *cir;
105 while ( *crp != NULL ) {
106 crp = &(*crp)->scr_next;
110 if ( scr->scr_oid ) {
111 cir = (struct cindexrec *)
112 ch_calloc( 1, sizeof(struct cindexrec) );
113 cir->cir_name.bv_val = scr->scr_oid;
114 cir->cir_name.bv_len = strlen( scr->scr_oid );
117 assert( cir->cir_name.bv_val );
118 assert( cir->cir_cr );
120 if ( avl_insert( &cr_index, (caddr_t) cir,
121 (AVL_CMP) cr_index_cmp,
122 (AVL_DUP) avl_dup_error ) )
126 return SLAP_SCHERR_CR_DUP;
129 /* FIX: temporal consistency check */
130 assert( cr_bvfind(&cir->cir_name) != NULL );
133 if ( (names = scr->scr_names) ) {
135 cir = (struct cindexrec *)
136 ch_calloc( 1, sizeof(struct cindexrec) );
137 cir->cir_name.bv_val = *names;
138 cir->cir_name.bv_len = strlen( *names );
141 assert( cir->cir_name.bv_val );
142 assert( cir->cir_cr );
144 if ( avl_insert( &cr_index, (caddr_t) cir,
145 (AVL_CMP) cr_index_cmp,
146 (AVL_DUP) avl_dup_error ) )
150 return SLAP_SCHERR_CR_DUP;
153 /* FIX: temporal consistency check */
154 assert( cr_bvfind(&cir->cir_name) != NULL );
171 if( scr->scr_oc_oids_aux == NULL ) return 0;
173 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
177 scr->scr_auxiliaries = ch_calloc( naux+1, sizeof(ObjectClass *));
179 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
180 ObjectClass *soc = scr->scr_auxiliaries[naux]
181 = oc_find(scr->scr_oc_oids_aux[naux]);
183 *err = scr->scr_oc_oids_aux[naux];
184 return SLAP_SCHERR_CLASS_NOT_FOUND;
187 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
189 if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
190 *err = scr->scr_oc_oids_aux[naux];
191 return SLAP_SCHERR_CR_BAD_AUX;
195 scr->scr_auxiliaries[naux] = NULL;
206 char **attrs = scr->scr_at_oids_must;
213 sat = at_find(*attrs1);
216 return SLAP_SCHERR_ATTR_NOT_FOUND;
219 if( is_at_operational( sat )) (*op)++;
221 if ( at_find_in_list(sat, scr->scr_required) < 0) {
222 if ( at_append_to_list(sat, &scr->scr_required) ) {
224 return SLAP_SCHERR_OUTOFMEM;
228 return SLAP_SCHERR_CR_BAD_AT;
242 char **attrs = scr->scr_at_oids_may;
249 sat = at_find(*attrs1);
252 return SLAP_SCHERR_ATTR_NOT_FOUND;
255 if( is_at_operational( sat )) (*op)++;
257 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
258 at_find_in_list(sat, scr->scr_allowed) < 0 )
260 if ( at_append_to_list(sat, &scr->scr_allowed) ) {
262 return SLAP_SCHERR_OUTOFMEM;
266 return SLAP_SCHERR_CR_BAD_AT;
280 char **attrs = scr->scr_at_oids_not;
287 sat = at_find(*attrs1);
290 return SLAP_SCHERR_ATTR_NOT_FOUND;
293 if( is_at_operational( sat )) (*op)++;
295 /* FIXME: should also make sure attribute type is not
296 a required attribute of the structural class or
297 any auxiliary class */
298 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
299 at_find_in_list(sat, scr->scr_allowed) < 0 &&
300 at_find_in_list(sat, scr->scr_precluded) < 0 )
302 if ( at_append_to_list(sat, &scr->scr_precluded) ) {
304 return SLAP_SCHERR_OUTOFMEM;
308 return SLAP_SCHERR_CR_BAD_AT;
327 if ( cr->cr_names != NULL ) {
330 for( i=0; cr->cr_names[i]; i++ ) {
331 if( !slap_valid_descr( cr->cr_names[i] ) ) {
332 return SLAP_SCHERR_BAD_DESCR;
337 if ( !OID_LEADCHAR( cr->cr_oid[0] )) {
338 /* Expand OID macros */
339 char *oid = oidm_find( cr->cr_oid );
342 return SLAP_SCHERR_OIDM;
344 if ( oid != cr->cr_oid ) {
345 ldap_memfree( cr->cr_oid );
350 scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
351 AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
353 scr->scr_sclass = oc_find(cr->cr_oid);
354 if ( !scr->scr_sclass ) {
356 return SLAP_SCHERR_CLASS_NOT_FOUND;
359 /* check object class usage */
360 if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL )
363 return SLAP_SCHERR_CR_BAD_STRUCT;
366 if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++;
368 code = cr_add_auxiliaries( scr, &op, err );
369 if ( code != 0 ) return code;
371 code = cr_create_required( scr, &op, err );
372 if ( code != 0 ) return code;
374 code = cr_create_allowed( scr, &op, err );
375 if ( code != 0 ) return code;
377 code = cr_create_precluded( scr, &op, err );
378 if ( code != 0 ) return code;
380 if( user && op ) return SLAP_SCHERR_CR_BAD_AUX;
382 code = cr_insert(scr,err);
389 cr_schema_info( Entry *e )
391 #ifdef SLAP_EXTENDED_SCHEMA
392 struct berval vals[2];
395 AttributeDescription *ad_ditContentRules = slap_schema.si_ad_ditContentRules;
397 vals[1].bv_val = NULL;
399 for ( cr = cr_list; cr; cr = cr->scr_next ) {
400 if ( ldap_contentrule2bv( &cr->scr_crule, vals ) == NULL ) {
405 if( cr->scr_flags & SLAP_CR_HIDE ) continue;
409 Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n",
410 (long) vals[0].bv_len, vals[0].bv_val, 0 );
412 attr_merge( e, ad_ditContentRules, vals );
413 ldap_memfree( vals[0].bv_val );