1 /* cr.c - content rule routines */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
20 struct berval cir_name;
24 static Avlnode *cr_index = NULL;
25 static LDAP_SLIST_HEAD(CRList, slap_content_rule) cr_list
26 = LDAP_SLIST_HEAD_INITIALIZER(&cr_list);
33 const struct cindexrec *cir1 = v_cir1;
34 const struct cindexrec *cir2 = v_cir2;
35 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 );
45 const struct berval *name = v_name;
46 const struct cindexrec *cir = v_cir;
47 int i = name->bv_len - cir->cir_name.bv_len;
49 return strncasecmp( name->bv_val, cir->cir_name.bv_val, name->bv_len );
53 cr_find( const char *crname )
57 bv.bv_val = (char *)crname;
58 bv.bv_len = strlen( crname );
60 return( cr_bvfind( &bv ) );
64 cr_bvfind( struct berval *crname )
66 struct cindexrec *cir;
68 cir = avl_find( cr_index, crname, cr_index_name_cmp );
71 return( cir->cir_cr );
78 cr_destroy_one( ContentRule *c )
82 if (c->scr_auxiliaries) ldap_memfree(c->scr_auxiliaries);
83 if (c->scr_required) ldap_memfree(c->scr_required);
84 if (c->scr_allowed) ldap_memfree(c->scr_allowed);
85 if (c->scr_precluded) ldap_memfree(c->scr_precluded);
86 ldap_contentrule_free((LDAPContentRule *)c);
96 avl_free(cr_index, ldap_memfree);
98 while( !LDAP_SLIST_EMPTY(&cr_list) ) {
99 c = LDAP_SLIST_FIRST(&cr_list);
100 LDAP_SLIST_REMOVE_HEAD(&cr_list, scr_next);
112 struct cindexrec *cir;
115 LDAP_SLIST_NEXT( scr, scr_next ) = NULL;
116 LDAP_SLIST_INSERT_HEAD(&cr_list, scr, scr_next);
118 if ( scr->scr_oid ) {
119 cir = (struct cindexrec *)
120 ch_calloc( 1, sizeof(struct cindexrec) );
121 cir->cir_name.bv_val = scr->scr_oid;
122 cir->cir_name.bv_len = strlen( scr->scr_oid );
125 assert( cir->cir_name.bv_val );
126 assert( cir->cir_cr );
128 if ( avl_insert( &cr_index, (caddr_t) cir,
129 cr_index_cmp, avl_dup_error ) )
133 return SLAP_SCHERR_CR_DUP;
136 /* FIX: temporal consistency check */
137 assert( cr_bvfind(&cir->cir_name) != NULL );
140 if ( (names = scr->scr_names) ) {
142 cir = (struct cindexrec *)
143 ch_calloc( 1, sizeof(struct cindexrec) );
144 cir->cir_name.bv_val = *names;
145 cir->cir_name.bv_len = strlen( *names );
148 assert( cir->cir_name.bv_val );
149 assert( cir->cir_cr );
151 if ( avl_insert( &cr_index, (caddr_t) cir,
152 cr_index_cmp, avl_dup_error ) )
156 return SLAP_SCHERR_CR_DUP;
159 /* FIX: temporal consistency check */
160 assert( cr_bvfind(&cir->cir_name) != NULL );
177 if( scr->scr_oc_oids_aux == NULL ) return 0;
179 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
183 scr->scr_auxiliaries = ch_calloc( naux+1, sizeof(ObjectClass *));
185 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
186 ObjectClass *soc = scr->scr_auxiliaries[naux]
187 = oc_find(scr->scr_oc_oids_aux[naux]);
189 *err = scr->scr_oc_oids_aux[naux];
190 return SLAP_SCHERR_CLASS_NOT_FOUND;
193 if( soc->soc_flags & SLAP_OC_OPERATIONAL &&
194 soc != slap_schema.si_oc_extensibleObject )
199 if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
200 *err = scr->scr_oc_oids_aux[naux];
201 return SLAP_SCHERR_CR_BAD_AUX;
205 scr->scr_auxiliaries[naux] = NULL;
215 char **attrs = scr->scr_at_oids_must;
222 sat = at_find(*attrs1);
225 return SLAP_SCHERR_ATTR_NOT_FOUND;
228 if( is_at_operational( sat )) (*op)++;
230 if ( at_find_in_list(sat, scr->scr_required) < 0) {
231 if ( at_append_to_list(sat, &scr->scr_required) ) {
233 return SLAP_SCHERR_OUTOFMEM;
237 return SLAP_SCHERR_CR_BAD_AT;
251 char **attrs = scr->scr_at_oids_may;
258 sat = at_find(*attrs1);
261 return SLAP_SCHERR_ATTR_NOT_FOUND;
264 if( is_at_operational( sat )) (*op)++;
266 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
267 at_find_in_list(sat, scr->scr_allowed) < 0 )
269 if ( at_append_to_list(sat, &scr->scr_allowed) ) {
271 return SLAP_SCHERR_OUTOFMEM;
275 return SLAP_SCHERR_CR_BAD_AT;
289 char **attrs = scr->scr_at_oids_not;
296 sat = at_find(*attrs1);
299 return SLAP_SCHERR_ATTR_NOT_FOUND;
302 if( is_at_operational( sat )) (*op)++;
304 /* FIXME: should also make sure attribute type is not
305 a required attribute of the structural class or
306 any auxiliary class */
307 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
308 at_find_in_list(sat, scr->scr_allowed) < 0 &&
309 at_find_in_list(sat, scr->scr_precluded) < 0 )
311 if ( at_append_to_list(sat, &scr->scr_precluded) ) {
313 return SLAP_SCHERR_OUTOFMEM;
317 return SLAP_SCHERR_CR_BAD_AT;
336 if ( cr->cr_names != NULL ) {
339 for( i=0; cr->cr_names[i]; i++ ) {
340 if( !slap_valid_descr( cr->cr_names[i] ) ) {
341 return SLAP_SCHERR_BAD_DESCR;
346 if ( !OID_LEADCHAR( cr->cr_oid[0] )) {
347 /* Expand OID macros */
348 char *oid = oidm_find( cr->cr_oid );
351 return SLAP_SCHERR_OIDM;
353 if ( oid != cr->cr_oid ) {
354 ldap_memfree( cr->cr_oid );
359 scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
360 AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
362 scr->scr_sclass = oc_find(cr->cr_oid);
363 if ( !scr->scr_sclass ) {
365 return SLAP_SCHERR_CLASS_NOT_FOUND;
368 /* check object class usage */
369 if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL )
372 return SLAP_SCHERR_CR_BAD_STRUCT;
375 if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++;
377 code = cr_add_auxiliaries( scr, &op, err );
378 if ( code != 0 ) return code;
380 code = cr_create_required( scr, &op, err );
381 if ( code != 0 ) return code;
383 code = cr_create_allowed( scr, &op, err );
384 if ( code != 0 ) return code;
386 code = cr_create_precluded( scr, &op, err );
387 if ( code != 0 ) return code;
390 return SLAP_SCHERR_CR_BAD_AUX;
393 code = cr_insert(scr,err);
398 cr_schema_info( Entry *e )
400 AttributeDescription *ad_ditContentRules
401 = slap_schema.si_ad_ditContentRules;
407 LDAP_SLIST_FOREACH(cr, &cr_list, scr_next) {
408 if ( ldap_contentrule2bv( &cr->scr_crule, &val ) == NULL ) {
413 if( cr->scr_flags & SLAP_CR_HIDE ) continue;
416 Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n",
417 (long) val.bv_len, val.bv_val, 0 );
420 nval.bv_val = cr->scr_oid;
421 nval.bv_len = strlen(cr->scr_oid);
423 if( attr_merge_one( e, ad_ditContentRules, &val, &nval ) )
427 ldap_memfree( val.bv_val );