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 );
86 avl_free(cr_index, ldap_memfree);
88 while( !LDAP_SLIST_EMPTY(&cr_list) ) {
89 c = LDAP_SLIST_FIRST(&cr_list);
90 LDAP_SLIST_REMOVE_HEAD(&cr_list, scr_next);
92 if (c->scr_auxiliaries) ldap_memfree(c->scr_auxiliaries);
93 if (c->scr_required) ldap_memfree(c->scr_required);
94 if (c->scr_allowed) ldap_memfree(c->scr_allowed);
95 if (c->scr_precluded) ldap_memfree(c->scr_precluded);
96 ldap_contentrule_free((LDAPContentRule *)c);
106 struct cindexrec *cir;
109 LDAP_SLIST_INSERT_HEAD(&cr_list, scr, scr_next);
111 if ( scr->scr_oid ) {
112 cir = (struct cindexrec *)
113 ch_calloc( 1, sizeof(struct cindexrec) );
114 cir->cir_name.bv_val = scr->scr_oid;
115 cir->cir_name.bv_len = strlen( scr->scr_oid );
118 assert( cir->cir_name.bv_val );
119 assert( cir->cir_cr );
121 if ( avl_insert( &cr_index, (caddr_t) cir,
122 cr_index_cmp, 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 cr_index_cmp, avl_dup_error ) )
149 return SLAP_SCHERR_CR_DUP;
152 /* FIX: temporal consistency check */
153 assert( cr_bvfind(&cir->cir_name) != NULL );
170 if( scr->scr_oc_oids_aux == NULL ) return 0;
172 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
176 scr->scr_auxiliaries = ch_calloc( naux+1, sizeof(ObjectClass *));
178 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
179 ObjectClass *soc = scr->scr_auxiliaries[naux]
180 = oc_find(scr->scr_oc_oids_aux[naux]);
182 *err = scr->scr_oc_oids_aux[naux];
183 return SLAP_SCHERR_CLASS_NOT_FOUND;
186 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
188 if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
189 *err = scr->scr_oc_oids_aux[naux];
190 return SLAP_SCHERR_CR_BAD_AUX;
194 scr->scr_auxiliaries[naux] = NULL;
205 char **attrs = scr->scr_at_oids_must;
212 sat = at_find(*attrs1);
215 return SLAP_SCHERR_ATTR_NOT_FOUND;
218 if( is_at_operational( sat )) (*op)++;
220 if ( at_find_in_list(sat, scr->scr_required) < 0) {
221 if ( at_append_to_list(sat, &scr->scr_required) ) {
223 return SLAP_SCHERR_OUTOFMEM;
227 return SLAP_SCHERR_CR_BAD_AT;
241 char **attrs = scr->scr_at_oids_may;
248 sat = at_find(*attrs1);
251 return SLAP_SCHERR_ATTR_NOT_FOUND;
254 if( is_at_operational( sat )) (*op)++;
256 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
257 at_find_in_list(sat, scr->scr_allowed) < 0 )
259 if ( at_append_to_list(sat, &scr->scr_allowed) ) {
261 return SLAP_SCHERR_OUTOFMEM;
265 return SLAP_SCHERR_CR_BAD_AT;
279 char **attrs = scr->scr_at_oids_not;
286 sat = at_find(*attrs1);
289 return SLAP_SCHERR_ATTR_NOT_FOUND;
292 if( is_at_operational( sat )) (*op)++;
294 /* FIXME: should also make sure attribute type is not
295 a required attribute of the structural class or
296 any auxiliary class */
297 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
298 at_find_in_list(sat, scr->scr_allowed) < 0 &&
299 at_find_in_list(sat, scr->scr_precluded) < 0 )
301 if ( at_append_to_list(sat, &scr->scr_precluded) ) {
303 return SLAP_SCHERR_OUTOFMEM;
307 return SLAP_SCHERR_CR_BAD_AT;
326 if ( cr->cr_names != NULL ) {
329 for( i=0; cr->cr_names[i]; i++ ) {
330 if( !slap_valid_descr( cr->cr_names[i] ) ) {
331 return SLAP_SCHERR_BAD_DESCR;
336 if ( !OID_LEADCHAR( cr->cr_oid[0] )) {
337 /* Expand OID macros */
338 char *oid = oidm_find( cr->cr_oid );
341 return SLAP_SCHERR_OIDM;
343 if ( oid != cr->cr_oid ) {
344 ldap_memfree( cr->cr_oid );
349 scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
350 AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
352 scr->scr_sclass = oc_find(cr->cr_oid);
353 if ( !scr->scr_sclass ) {
355 return SLAP_SCHERR_CLASS_NOT_FOUND;
358 /* check object class usage */
359 if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL )
362 return SLAP_SCHERR_CR_BAD_STRUCT;
365 if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++;
367 code = cr_add_auxiliaries( scr, &op, err );
368 if ( code != 0 ) return code;
370 code = cr_create_required( scr, &op, err );
371 if ( code != 0 ) return code;
373 code = cr_create_allowed( scr, &op, err );
374 if ( code != 0 ) return code;
376 code = cr_create_precluded( scr, &op, err );
377 if ( code != 0 ) return code;
379 if( user && op ) return SLAP_SCHERR_CR_BAD_AUX;
381 code = cr_insert(scr,err);
388 cr_schema_info( Entry *e )
390 #ifdef SLAP_EXTENDED_SCHEMA
391 struct berval vals[2];
394 AttributeDescription *ad_ditContentRules = slap_schema.si_ad_ditContentRules;
396 vals[1].bv_val = NULL;
398 LDAP_SLIST_FOREACH(cr, &cr_list, scr_next) {
399 if ( ldap_contentrule2bv( &cr->scr_crule, vals ) == NULL ) {
404 if( cr->scr_flags & SLAP_CR_HIDE ) continue;
408 Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n",
409 (long) vals[0].bv_len, vals[0].bv_val, 0 );
411 if( attr_merge( e, ad_ditContentRules, vals ) )
413 ldap_memfree( vals[0].bv_val );