1 /* cr.c - content rule routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
28 struct berval cir_name;
32 static Avlnode *cr_index = NULL;
33 static LDAP_SLIST_HEAD(CRList, slap_content_rule) cr_list
34 = LDAP_SLIST_HEAD_INITIALIZER(&cr_list);
41 const struct cindexrec *cir1 = v_cir1;
42 const struct cindexrec *cir2 = v_cir2;
43 int i = cir1->cir_name.bv_len - cir2->cir_name.bv_len;
45 return strcasecmp( cir1->cir_name.bv_val, cir2->cir_name.bv_val );
53 const struct berval *name = v_name;
54 const struct cindexrec *cir = v_cir;
55 int i = name->bv_len - cir->cir_name.bv_len;
57 return strncasecmp( name->bv_val, cir->cir_name.bv_val, name->bv_len );
61 cr_find( const char *crname )
65 bv.bv_val = (char *)crname;
66 bv.bv_len = strlen( crname );
68 return( cr_bvfind( &bv ) );
72 cr_bvfind( struct berval *crname )
74 struct cindexrec *cir;
76 cir = avl_find( cr_index, crname, cr_index_name_cmp );
79 return( cir->cir_cr );
86 cr_destroy_one( ContentRule *c )
90 if (c->scr_auxiliaries) ldap_memfree(c->scr_auxiliaries);
91 if (c->scr_required) ldap_memfree(c->scr_required);
92 if (c->scr_allowed) ldap_memfree(c->scr_allowed);
93 if (c->scr_precluded) ldap_memfree(c->scr_precluded);
94 ldap_contentrule_free((LDAPContentRule *)c);
104 avl_free(cr_index, ldap_memfree);
106 while( !LDAP_SLIST_EMPTY(&cr_list) ) {
107 c = LDAP_SLIST_FIRST(&cr_list);
108 LDAP_SLIST_REMOVE_HEAD(&cr_list, scr_next);
120 struct cindexrec *cir;
123 LDAP_SLIST_NEXT( scr, scr_next ) = NULL;
124 LDAP_SLIST_INSERT_HEAD(&cr_list, scr, scr_next);
126 if ( scr->scr_oid ) {
127 cir = (struct cindexrec *)
128 ch_calloc( 1, sizeof(struct cindexrec) );
129 cir->cir_name.bv_val = scr->scr_oid;
130 cir->cir_name.bv_len = strlen( scr->scr_oid );
133 assert( cir->cir_name.bv_val );
134 assert( cir->cir_cr );
136 if ( avl_insert( &cr_index, (caddr_t) cir,
137 cr_index_cmp, avl_dup_error ) )
141 return SLAP_SCHERR_CR_DUP;
144 /* FIX: temporal consistency check */
145 assert( cr_bvfind(&cir->cir_name) != NULL );
148 if ( (names = scr->scr_names) ) {
150 cir = (struct cindexrec *)
151 ch_calloc( 1, sizeof(struct cindexrec) );
152 cir->cir_name.bv_val = *names;
153 cir->cir_name.bv_len = strlen( *names );
156 assert( cir->cir_name.bv_val );
157 assert( cir->cir_cr );
159 if ( avl_insert( &cr_index, (caddr_t) cir,
160 cr_index_cmp, avl_dup_error ) )
164 return SLAP_SCHERR_CR_DUP;
167 /* FIX: temporal consistency check */
168 assert( cr_bvfind(&cir->cir_name) != NULL );
185 if( scr->scr_oc_oids_aux == NULL ) return 0;
187 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
191 scr->scr_auxiliaries = ch_calloc( naux+1, sizeof(ObjectClass *));
193 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
194 ObjectClass *soc = scr->scr_auxiliaries[naux]
195 = oc_find(scr->scr_oc_oids_aux[naux]);
197 *err = scr->scr_oc_oids_aux[naux];
198 return SLAP_SCHERR_CLASS_NOT_FOUND;
201 if( soc->soc_flags & SLAP_OC_OPERATIONAL &&
202 soc != slap_schema.si_oc_extensibleObject )
207 if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
208 *err = scr->scr_oc_oids_aux[naux];
209 return SLAP_SCHERR_CR_BAD_AUX;
213 scr->scr_auxiliaries[naux] = NULL;
223 char **attrs = scr->scr_at_oids_must;
230 sat = at_find(*attrs1);
233 return SLAP_SCHERR_ATTR_NOT_FOUND;
236 if( is_at_operational( sat )) (*op)++;
238 if ( at_find_in_list(sat, scr->scr_required) < 0) {
239 if ( at_append_to_list(sat, &scr->scr_required) ) {
241 return SLAP_SCHERR_OUTOFMEM;
245 return SLAP_SCHERR_CR_BAD_AT;
259 char **attrs = scr->scr_at_oids_may;
266 sat = at_find(*attrs1);
269 return SLAP_SCHERR_ATTR_NOT_FOUND;
272 if( is_at_operational( sat )) (*op)++;
274 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
275 at_find_in_list(sat, scr->scr_allowed) < 0 )
277 if ( at_append_to_list(sat, &scr->scr_allowed) ) {
279 return SLAP_SCHERR_OUTOFMEM;
283 return SLAP_SCHERR_CR_BAD_AT;
297 char **attrs = scr->scr_at_oids_not;
304 sat = at_find(*attrs1);
307 return SLAP_SCHERR_ATTR_NOT_FOUND;
310 if( is_at_operational( sat )) (*op)++;
312 /* FIXME: should also make sure attribute type is not
313 a required attribute of the structural class or
314 any auxiliary class */
315 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
316 at_find_in_list(sat, scr->scr_allowed) < 0 &&
317 at_find_in_list(sat, scr->scr_precluded) < 0 )
319 if ( at_append_to_list(sat, &scr->scr_precluded) ) {
321 return SLAP_SCHERR_OUTOFMEM;
325 return SLAP_SCHERR_CR_BAD_AT;
344 if ( cr->cr_names != NULL ) {
347 for( i=0; cr->cr_names[i]; i++ ) {
348 if( !slap_valid_descr( cr->cr_names[i] ) ) {
349 return SLAP_SCHERR_BAD_DESCR;
354 if ( !OID_LEADCHAR( cr->cr_oid[0] )) {
355 /* Expand OID macros */
356 char *oid = oidm_find( cr->cr_oid );
359 return SLAP_SCHERR_OIDM;
361 if ( oid != cr->cr_oid ) {
362 ldap_memfree( cr->cr_oid );
367 scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
368 AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
370 scr->scr_sclass = oc_find(cr->cr_oid);
371 if ( !scr->scr_sclass ) {
373 return SLAP_SCHERR_CLASS_NOT_FOUND;
376 /* check object class usage */
377 if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL )
380 return SLAP_SCHERR_CR_BAD_STRUCT;
383 if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++;
385 code = cr_add_auxiliaries( scr, &op, err );
386 if ( code != 0 ) return code;
388 code = cr_create_required( scr, &op, err );
389 if ( code != 0 ) return code;
391 code = cr_create_allowed( scr, &op, err );
392 if ( code != 0 ) return code;
394 code = cr_create_precluded( scr, &op, err );
395 if ( code != 0 ) return code;
398 return SLAP_SCHERR_CR_BAD_AUX;
401 code = cr_insert(scr,err);
406 cr_schema_info( Entry *e )
408 AttributeDescription *ad_ditContentRules
409 = slap_schema.si_ad_ditContentRules;
415 LDAP_SLIST_FOREACH(cr, &cr_list, scr_next) {
416 if ( ldap_contentrule2bv( &cr->scr_crule, &val ) == NULL ) {
421 if( cr->scr_flags & SLAP_CR_HIDE ) continue;
424 Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n",
425 (long) val.bv_len, val.bv_val, 0 );
428 nval.bv_val = cr->scr_oid;
429 nval.bv_len = strlen(cr->scr_oid);
431 if( attr_merge_one( e, ad_ditContentRules, &val, &nval ) )
435 ldap_memfree( val.bv_val );