1 /* cr.c - content rule routines */
4 * Copyright 1998-2002 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 ContentRule *cr_list = NULL;
34 const struct cindexrec *cir1 = v_cir1;
35 const struct cindexrec *cir2 = v_cir2;
36 int i = cir1->cir_name.bv_len - cir2->cir_name.bv_len;
39 return strcasecmp( cir1->cir_name.bv_val, cir2->cir_name.bv_val );
47 const struct berval *name = v_name;
48 const struct cindexrec *cir = v_cir;
49 int i = name->bv_len - cir->cir_name.bv_len;
52 return strncasecmp( name->bv_val, cir->cir_name.bv_val, name->bv_len );
56 cr_find( const char *crname )
60 bv.bv_val = (char *)crname;
61 bv.bv_len = strlen( crname );
63 return( cr_bvfind( &bv ) );
67 cr_bvfind( struct berval *crname )
69 struct cindexrec *cir;
71 cir = avl_find( cr_index, crname, cr_index_name_cmp );
74 return( cir->cir_cr );
85 avl_free(cr_index, ldap_memfree);
86 for (c=cr_list; c; c=n)
89 if (c->scr_auxiliaries) ldap_memfree(c->scr_auxiliaries);
90 if (c->scr_required) ldap_memfree(c->scr_required);
91 if (c->scr_allowed) ldap_memfree(c->scr_allowed);
92 if (c->scr_precluded) ldap_memfree(c->scr_precluded);
93 ldap_contentrule_free((LDAPContentRule *)c);
104 struct cindexrec *cir;
108 while ( *crp != NULL ) {
109 crp = &(*crp)->scr_next;
113 if ( scr->scr_oid ) {
114 cir = (struct cindexrec *)
115 ch_calloc( 1, sizeof(struct cindexrec) );
116 cir->cir_name.bv_val = scr->scr_oid;
117 cir->cir_name.bv_len = strlen( scr->scr_oid );
120 assert( cir->cir_name.bv_val );
121 assert( cir->cir_cr );
123 if ( avl_insert( &cr_index, (caddr_t) cir,
124 cr_index_cmp, avl_dup_error ) )
128 return SLAP_SCHERR_CR_DUP;
131 /* FIX: temporal consistency check */
132 assert( cr_bvfind(&cir->cir_name) != NULL );
135 if ( (names = scr->scr_names) ) {
137 cir = (struct cindexrec *)
138 ch_calloc( 1, sizeof(struct cindexrec) );
139 cir->cir_name.bv_val = *names;
140 cir->cir_name.bv_len = strlen( *names );
143 assert( cir->cir_name.bv_val );
144 assert( cir->cir_cr );
146 if ( avl_insert( &cr_index, (caddr_t) cir,
147 cr_index_cmp, avl_dup_error ) )
151 return SLAP_SCHERR_CR_DUP;
154 /* FIX: temporal consistency check */
155 assert( cr_bvfind(&cir->cir_name) != NULL );
172 if( scr->scr_oc_oids_aux == NULL ) return 0;
174 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
178 scr->scr_auxiliaries = ch_calloc( naux+1, sizeof(ObjectClass *));
180 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
181 ObjectClass *soc = scr->scr_auxiliaries[naux]
182 = oc_find(scr->scr_oc_oids_aux[naux]);
184 *err = scr->scr_oc_oids_aux[naux];
185 return SLAP_SCHERR_CLASS_NOT_FOUND;
188 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
190 if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
191 *err = scr->scr_oc_oids_aux[naux];
192 return SLAP_SCHERR_CR_BAD_AUX;
196 scr->scr_auxiliaries[naux] = NULL;
207 char **attrs = scr->scr_at_oids_must;
214 sat = at_find(*attrs1);
217 return SLAP_SCHERR_ATTR_NOT_FOUND;
220 if( is_at_operational( sat )) (*op)++;
222 if ( at_find_in_list(sat, scr->scr_required) < 0) {
223 if ( at_append_to_list(sat, &scr->scr_required) ) {
225 return SLAP_SCHERR_OUTOFMEM;
229 return SLAP_SCHERR_CR_BAD_AT;
243 char **attrs = scr->scr_at_oids_may;
250 sat = at_find(*attrs1);
253 return SLAP_SCHERR_ATTR_NOT_FOUND;
256 if( is_at_operational( sat )) (*op)++;
258 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
259 at_find_in_list(sat, scr->scr_allowed) < 0 )
261 if ( at_append_to_list(sat, &scr->scr_allowed) ) {
263 return SLAP_SCHERR_OUTOFMEM;
267 return SLAP_SCHERR_CR_BAD_AT;
281 char **attrs = scr->scr_at_oids_not;
288 sat = at_find(*attrs1);
291 return SLAP_SCHERR_ATTR_NOT_FOUND;
294 if( is_at_operational( sat )) (*op)++;
296 /* FIXME: should also make sure attribute type is not
297 a required attribute of the structural class or
298 any auxiliary class */
299 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
300 at_find_in_list(sat, scr->scr_allowed) < 0 &&
301 at_find_in_list(sat, scr->scr_precluded) < 0 )
303 if ( at_append_to_list(sat, &scr->scr_precluded) ) {
305 return SLAP_SCHERR_OUTOFMEM;
309 return SLAP_SCHERR_CR_BAD_AT;
328 if ( cr->cr_names != NULL ) {
331 for( i=0; cr->cr_names[i]; i++ ) {
332 if( !slap_valid_descr( cr->cr_names[i] ) ) {
333 return SLAP_SCHERR_BAD_DESCR;
338 if ( !OID_LEADCHAR( cr->cr_oid[0] )) {
339 /* Expand OID macros */
340 char *oid = oidm_find( cr->cr_oid );
343 return SLAP_SCHERR_OIDM;
345 if ( oid != cr->cr_oid ) {
346 ldap_memfree( cr->cr_oid );
351 scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
352 AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
354 scr->scr_sclass = oc_find(cr->cr_oid);
355 if ( !scr->scr_sclass ) {
357 return SLAP_SCHERR_CLASS_NOT_FOUND;
360 /* check object class usage */
361 if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL )
364 return SLAP_SCHERR_CR_BAD_STRUCT;
367 if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++;
369 code = cr_add_auxiliaries( scr, &op, err );
370 if ( code != 0 ) return code;
372 code = cr_create_required( scr, &op, err );
373 if ( code != 0 ) return code;
375 code = cr_create_allowed( scr, &op, err );
376 if ( code != 0 ) return code;
378 code = cr_create_precluded( scr, &op, err );
379 if ( code != 0 ) return code;
381 if( user && op ) return SLAP_SCHERR_CR_BAD_AUX;
383 code = cr_insert(scr,err);
390 cr_schema_info( Entry *e )
392 #ifdef SLAP_EXTENDED_SCHEMA
393 struct berval vals[2];
396 AttributeDescription *ad_ditContentRules = slap_schema.si_ad_ditContentRules;
398 vals[1].bv_val = NULL;
400 for ( cr = cr_list; cr; cr = cr->scr_next ) {
401 if ( ldap_contentrule2bv( &cr->scr_crule, vals ) == NULL ) {
406 if( cr->scr_flags & SLAP_CR_HIDE ) continue;
410 Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n",
411 (long) vals[0].bv_len, vals[0].bv_val, 0 );
413 if( attr_merge( e, ad_ditContentRules, vals ) )
415 ldap_memfree( vals[0].bv_val );