1 /* cr.c - content rule routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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>
29 struct berval cir_name;
33 static Avlnode *cr_index = NULL;
34 static LDAP_SLIST_HEAD(CRList, slap_content_rule) cr_list
35 = LDAP_SLIST_HEAD_INITIALIZER(&cr_list);
42 const struct cindexrec *cir1 = v_cir1;
43 const struct cindexrec *cir2 = v_cir2;
44 int i = cir1->cir_name.bv_len - cir2->cir_name.bv_len;
46 return strcasecmp( cir1->cir_name.bv_val, cir2->cir_name.bv_val );
54 const struct berval *name = v_name;
55 const struct cindexrec *cir = v_cir;
56 int i = name->bv_len - cir->cir_name.bv_len;
58 return strncasecmp( name->bv_val, cir->cir_name.bv_val, name->bv_len );
62 cr_find( const char *crname )
66 bv.bv_val = (char *)crname;
67 bv.bv_len = strlen( crname );
69 return( cr_bvfind( &bv ) );
73 cr_bvfind( struct berval *crname )
75 struct cindexrec *cir;
77 cir = avl_find( cr_index, crname, cr_index_name_cmp );
80 return( cir->cir_cr );
87 cr_destroy_one( ContentRule *c )
91 if (c->scr_auxiliaries) ldap_memfree(c->scr_auxiliaries);
92 if (c->scr_required) ldap_memfree(c->scr_required);
93 if (c->scr_allowed) ldap_memfree(c->scr_allowed);
94 if (c->scr_precluded) ldap_memfree(c->scr_precluded);
95 ldap_contentrule_free((LDAPContentRule *)c);
105 avl_free(cr_index, ldap_memfree);
107 while( !LDAP_SLIST_EMPTY(&cr_list) ) {
108 c = LDAP_SLIST_FIRST(&cr_list);
109 LDAP_SLIST_REMOVE_HEAD(&cr_list, scr_next);
121 struct cindexrec *cir;
124 LDAP_SLIST_NEXT( scr, scr_next ) = NULL;
125 LDAP_SLIST_INSERT_HEAD(&cr_list, scr, scr_next);
127 if ( scr->scr_oid ) {
128 cir = (struct cindexrec *)
129 ch_calloc( 1, sizeof(struct cindexrec) );
130 cir->cir_name.bv_val = scr->scr_oid;
131 cir->cir_name.bv_len = strlen( scr->scr_oid );
134 assert( cir->cir_name.bv_val );
135 assert( cir->cir_cr );
137 if ( avl_insert( &cr_index, (caddr_t) cir,
138 cr_index_cmp, avl_dup_error ) )
142 return SLAP_SCHERR_CR_DUP;
145 /* FIX: temporal consistency check */
146 assert( cr_bvfind(&cir->cir_name) != NULL );
149 if ( (names = scr->scr_names) ) {
151 cir = (struct cindexrec *)
152 ch_calloc( 1, sizeof(struct cindexrec) );
153 cir->cir_name.bv_val = *names;
154 cir->cir_name.bv_len = strlen( *names );
157 assert( cir->cir_name.bv_val );
158 assert( cir->cir_cr );
160 if ( avl_insert( &cr_index, (caddr_t) cir,
161 cr_index_cmp, avl_dup_error ) )
165 return SLAP_SCHERR_CR_DUP;
168 /* FIX: temporal consistency check */
169 assert( cr_bvfind(&cir->cir_name) != NULL );
186 if( scr->scr_oc_oids_aux == NULL ) return 0;
188 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
192 scr->scr_auxiliaries = ch_calloc( naux+1, sizeof(ObjectClass *));
194 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
195 ObjectClass *soc = scr->scr_auxiliaries[naux]
196 = oc_find(scr->scr_oc_oids_aux[naux]);
198 *err = scr->scr_oc_oids_aux[naux];
199 return SLAP_SCHERR_CLASS_NOT_FOUND;
202 if( soc->soc_flags & SLAP_OC_OPERATIONAL &&
203 soc != slap_schema.si_oc_extensibleObject )
208 if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
209 *err = scr->scr_oc_oids_aux[naux];
210 return SLAP_SCHERR_CR_BAD_AUX;
214 scr->scr_auxiliaries[naux] = NULL;
224 char **attrs = scr->scr_at_oids_must;
231 sat = at_find(*attrs1);
234 return SLAP_SCHERR_ATTR_NOT_FOUND;
237 if( is_at_operational( sat )) (*op)++;
239 if ( at_find_in_list(sat, scr->scr_required) < 0) {
240 if ( at_append_to_list(sat, &scr->scr_required) ) {
242 return SLAP_SCHERR_OUTOFMEM;
246 return SLAP_SCHERR_CR_BAD_AT;
260 char **attrs = scr->scr_at_oids_may;
267 sat = at_find(*attrs1);
270 return SLAP_SCHERR_ATTR_NOT_FOUND;
273 if( is_at_operational( sat )) (*op)++;
275 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
276 at_find_in_list(sat, scr->scr_allowed) < 0 )
278 if ( at_append_to_list(sat, &scr->scr_allowed) ) {
280 return SLAP_SCHERR_OUTOFMEM;
284 return SLAP_SCHERR_CR_BAD_AT;
298 char **attrs = scr->scr_at_oids_not;
305 sat = at_find(*attrs1);
308 return SLAP_SCHERR_ATTR_NOT_FOUND;
311 if( is_at_operational( sat )) (*op)++;
313 /* FIXME: should also make sure attribute type is not
314 a required attribute of the structural class or
315 any auxiliary class */
316 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
317 at_find_in_list(sat, scr->scr_allowed) < 0 &&
318 at_find_in_list(sat, scr->scr_precluded) < 0 )
320 if ( at_append_to_list(sat, &scr->scr_precluded) ) {
322 return SLAP_SCHERR_OUTOFMEM;
326 return SLAP_SCHERR_CR_BAD_AT;
345 if ( cr->cr_names != NULL ) {
348 for( i=0; cr->cr_names[i]; i++ ) {
349 if( !slap_valid_descr( cr->cr_names[i] ) ) {
350 return SLAP_SCHERR_BAD_DESCR;
355 if ( !OID_LEADCHAR( cr->cr_oid[0] )) {
356 /* Expand OID macros */
357 char *oid = oidm_find( cr->cr_oid );
360 return SLAP_SCHERR_OIDM;
362 if ( oid != cr->cr_oid ) {
363 ldap_memfree( cr->cr_oid );
368 scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
369 AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
371 scr->scr_sclass = oc_find(cr->cr_oid);
372 if ( !scr->scr_sclass ) {
374 return SLAP_SCHERR_CLASS_NOT_FOUND;
377 /* check object class usage */
378 if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL )
381 return SLAP_SCHERR_CR_BAD_STRUCT;
384 if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++;
386 code = cr_add_auxiliaries( scr, &op, err );
387 if ( code != 0 ) return code;
389 code = cr_create_required( scr, &op, err );
390 if ( code != 0 ) return code;
392 code = cr_create_allowed( scr, &op, err );
393 if ( code != 0 ) return code;
395 code = cr_create_precluded( scr, &op, err );
396 if ( code != 0 ) return code;
399 return SLAP_SCHERR_CR_BAD_AUX;
402 code = cr_insert(scr,err);
407 cr_schema_info( Entry *e )
409 AttributeDescription *ad_ditContentRules
410 = slap_schema.si_ad_ditContentRules;
416 LDAP_SLIST_FOREACH(cr, &cr_list, scr_next) {
417 if ( ldap_contentrule2bv( &cr->scr_crule, &val ) == NULL ) {
422 if( cr->scr_flags & SLAP_CR_HIDE ) continue;
425 Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n",
426 (long) val.bv_len, val.bv_val, 0 );
429 nval.bv_val = cr->scr_oid;
430 nval.bv_len = strlen(cr->scr_oid);
432 if( attr_merge_one( e, ad_ditContentRules, &val, &nval ) )
436 ldap_memfree( val.bv_val );