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>
19 #ifdef SLAP_EXTENDED_SCHEMA
22 struct berval cir_name;
26 static Avlnode *cr_index = NULL;
27 static LDAP_SLIST_HEAD(CRList, slap_content_rule) cr_list
28 = LDAP_SLIST_HEAD_INITIALIZER(&cr_list);
35 const struct cindexrec *cir1 = v_cir1;
36 const struct cindexrec *cir2 = v_cir2;
37 int i = cir1->cir_name.bv_len - cir2->cir_name.bv_len;
40 return strcasecmp( cir1->cir_name.bv_val, cir2->cir_name.bv_val );
48 const struct berval *name = v_name;
49 const struct cindexrec *cir = v_cir;
50 int i = name->bv_len - cir->cir_name.bv_len;
53 return strncasecmp( name->bv_val, cir->cir_name.bv_val, name->bv_len );
57 cr_find( const char *crname )
61 bv.bv_val = (char *)crname;
62 bv.bv_len = strlen( crname );
64 return( cr_bvfind( &bv ) );
68 cr_bvfind( struct berval *crname )
70 struct cindexrec *cir;
72 cir = avl_find( cr_index, crname, cr_index_name_cmp );
75 return( cir->cir_cr );
82 cr_destroy_one( ContentRule *c )
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);
100 avl_free(cr_index, ldap_memfree);
102 while( !LDAP_SLIST_EMPTY(&cr_list) ) {
103 c = LDAP_SLIST_FIRST(&cr_list);
104 LDAP_SLIST_REMOVE_HEAD(&cr_list, scr_next);
116 struct cindexrec *cir;
119 LDAP_SLIST_NEXT( scr, scr_next ) = NULL;
120 LDAP_SLIST_INSERT_HEAD(&cr_list, scr, scr_next);
122 if ( scr->scr_oid ) {
123 cir = (struct cindexrec *)
124 ch_calloc( 1, sizeof(struct cindexrec) );
125 cir->cir_name.bv_val = scr->scr_oid;
126 cir->cir_name.bv_len = strlen( scr->scr_oid );
129 assert( cir->cir_name.bv_val );
130 assert( cir->cir_cr );
132 if ( avl_insert( &cr_index, (caddr_t) cir,
133 cr_index_cmp, avl_dup_error ) )
137 return SLAP_SCHERR_CR_DUP;
140 /* FIX: temporal consistency check */
141 assert( cr_bvfind(&cir->cir_name) != NULL );
144 if ( (names = scr->scr_names) ) {
146 cir = (struct cindexrec *)
147 ch_calloc( 1, sizeof(struct cindexrec) );
148 cir->cir_name.bv_val = *names;
149 cir->cir_name.bv_len = strlen( *names );
152 assert( cir->cir_name.bv_val );
153 assert( cir->cir_cr );
155 if ( avl_insert( &cr_index, (caddr_t) cir,
156 cr_index_cmp, avl_dup_error ) )
160 return SLAP_SCHERR_CR_DUP;
163 /* FIX: temporal consistency check */
164 assert( cr_bvfind(&cir->cir_name) != NULL );
181 if( scr->scr_oc_oids_aux == NULL ) return 0;
183 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
187 scr->scr_auxiliaries = ch_calloc( naux+1, sizeof(ObjectClass *));
189 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
190 ObjectClass *soc = scr->scr_auxiliaries[naux]
191 = oc_find(scr->scr_oc_oids_aux[naux]);
193 *err = scr->scr_oc_oids_aux[naux];
194 return SLAP_SCHERR_CLASS_NOT_FOUND;
197 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
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;
216 char **attrs = scr->scr_at_oids_must;
223 sat = at_find(*attrs1);
226 return SLAP_SCHERR_ATTR_NOT_FOUND;
229 if( is_at_operational( sat )) (*op)++;
231 if ( at_find_in_list(sat, scr->scr_required) < 0) {
232 if ( at_append_to_list(sat, &scr->scr_required) ) {
234 return SLAP_SCHERR_OUTOFMEM;
238 return SLAP_SCHERR_CR_BAD_AT;
252 char **attrs = scr->scr_at_oids_may;
259 sat = at_find(*attrs1);
262 return SLAP_SCHERR_ATTR_NOT_FOUND;
265 if( is_at_operational( sat )) (*op)++;
267 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
268 at_find_in_list(sat, scr->scr_allowed) < 0 )
270 if ( at_append_to_list(sat, &scr->scr_allowed) ) {
272 return SLAP_SCHERR_OUTOFMEM;
276 return SLAP_SCHERR_CR_BAD_AT;
290 char **attrs = scr->scr_at_oids_not;
297 sat = at_find(*attrs1);
300 return SLAP_SCHERR_ATTR_NOT_FOUND;
303 if( is_at_operational( sat )) (*op)++;
305 /* FIXME: should also make sure attribute type is not
306 a required attribute of the structural class or
307 any auxiliary class */
308 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
309 at_find_in_list(sat, scr->scr_allowed) < 0 &&
310 at_find_in_list(sat, scr->scr_precluded) < 0 )
312 if ( at_append_to_list(sat, &scr->scr_precluded) ) {
314 return SLAP_SCHERR_OUTOFMEM;
318 return SLAP_SCHERR_CR_BAD_AT;
337 if ( cr->cr_names != NULL ) {
340 for( i=0; cr->cr_names[i]; i++ ) {
341 if( !slap_valid_descr( cr->cr_names[i] ) ) {
342 return SLAP_SCHERR_BAD_DESCR;
347 if ( !OID_LEADCHAR( cr->cr_oid[0] )) {
348 /* Expand OID macros */
349 char *oid = oidm_find( cr->cr_oid );
352 return SLAP_SCHERR_OIDM;
354 if ( oid != cr->cr_oid ) {
355 ldap_memfree( cr->cr_oid );
360 scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
361 AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
363 scr->scr_sclass = oc_find(cr->cr_oid);
364 if ( !scr->scr_sclass ) {
366 return SLAP_SCHERR_CLASS_NOT_FOUND;
369 /* check object class usage */
370 if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL )
373 return SLAP_SCHERR_CR_BAD_STRUCT;
376 if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++;
378 code = cr_add_auxiliaries( scr, &op, err );
379 if ( code != 0 ) return code;
381 code = cr_create_required( scr, &op, err );
382 if ( code != 0 ) return code;
384 code = cr_create_allowed( scr, &op, err );
385 if ( code != 0 ) return code;
387 code = cr_create_precluded( scr, &op, err );
388 if ( code != 0 ) return code;
390 if( user && op ) return SLAP_SCHERR_CR_BAD_AUX;
392 code = cr_insert(scr,err);
399 cr_schema_info( Entry *e )
401 #ifdef SLAP_EXTENDED_SCHEMA
402 AttributeDescription *ad_ditContentRules
403 = slap_schema.si_ad_ditContentRules;
411 LDAP_SLIST_FOREACH(cr, &cr_list, scr_next) {
412 if ( ldap_contentrule2bv( &cr->scr_crule, &val ) == NULL ) {
417 if( cr->scr_flags & SLAP_CR_HIDE ) continue;
420 Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n",
421 (long) val.bv_len, val.bv_val, 0 );
425 nval.bv_val = cr->scr_oid;
426 nval.bv_len = strlen(cr->scr_oid);
428 if( attr_merge_one( e, ad_ditContentRules, &val, &nval ) )
430 if( attr_merge_one( e, ad_ditContentRules, &val ) )
435 ldap_memfree( val.bv_val );