1 /* cr.c - content rule routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2014 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_STAILQ_HEAD(CRList, ContentRule) cr_list
34 = LDAP_STAILQ_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_STAILQ_EMPTY(&cr_list) ) {
107 c = LDAP_STAILQ_FIRST(&cr_list);
108 LDAP_STAILQ_REMOVE_HEAD(&cr_list, scr_next);
120 struct cindexrec *cir;
123 assert( scr != NULL );
125 if ( scr->scr_oid ) {
126 cir = (struct cindexrec *)
127 ch_calloc( 1, sizeof(struct cindexrec) );
128 cir->cir_name.bv_val = scr->scr_oid;
129 cir->cir_name.bv_len = strlen( scr->scr_oid );
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 if ( avl_insert( &cr_index, (caddr_t) cir,
153 cr_index_cmp, avl_dup_error ) )
157 return SLAP_SCHERR_CR_DUP;
160 /* FIX: temporal consistency check */
161 assert( cr_bvfind(&cir->cir_name) != NULL );
167 LDAP_STAILQ_INSERT_TAIL(&cr_list, scr, scr_next);
180 if( scr->scr_oc_oids_aux == NULL ) return 0;
182 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
186 scr->scr_auxiliaries = ch_calloc( naux+1, sizeof(ObjectClass *));
188 for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
189 ObjectClass *soc = scr->scr_auxiliaries[naux]
190 = oc_find(scr->scr_oc_oids_aux[naux]);
192 *err = scr->scr_oc_oids_aux[naux];
193 return SLAP_SCHERR_CLASS_NOT_FOUND;
196 if( soc->soc_flags & SLAP_OC_OPERATIONAL &&
197 soc != slap_schema.si_oc_extensibleObject )
202 if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
203 *err = scr->scr_oc_oids_aux[naux];
204 return SLAP_SCHERR_CR_BAD_AUX;
208 scr->scr_auxiliaries[naux] = NULL;
218 char **attrs = scr->scr_at_oids_must;
225 sat = at_find(*attrs1);
228 return SLAP_SCHERR_ATTR_NOT_FOUND;
231 if( is_at_operational( sat )) (*op)++;
233 if ( at_find_in_list(sat, scr->scr_required) < 0) {
234 if ( at_append_to_list(sat, &scr->scr_required) ) {
236 return SLAP_SCHERR_OUTOFMEM;
240 return SLAP_SCHERR_CR_BAD_AT;
254 char **attrs = scr->scr_at_oids_may;
261 sat = at_find(*attrs1);
264 return SLAP_SCHERR_ATTR_NOT_FOUND;
267 if( is_at_operational( sat )) (*op)++;
269 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
270 at_find_in_list(sat, scr->scr_allowed) < 0 )
272 if ( at_append_to_list(sat, &scr->scr_allowed) ) {
274 return SLAP_SCHERR_OUTOFMEM;
278 return SLAP_SCHERR_CR_BAD_AT;
292 char **attrs = scr->scr_at_oids_not;
299 sat = at_find(*attrs1);
302 return SLAP_SCHERR_ATTR_NOT_FOUND;
305 if( is_at_operational( sat )) (*op)++;
307 /* FIXME: should also make sure attribute type is not
308 a required attribute of the structural class or
309 any auxiliary class */
310 if ( at_find_in_list(sat, scr->scr_required) < 0 &&
311 at_find_in_list(sat, scr->scr_allowed) < 0 &&
312 at_find_in_list(sat, scr->scr_precluded) < 0 )
314 if ( at_append_to_list(sat, &scr->scr_precluded) ) {
316 return SLAP_SCHERR_OUTOFMEM;
320 return SLAP_SCHERR_CR_BAD_AT;
341 if ( cr->cr_names != NULL ) {
344 for( i=0; cr->cr_names[i]; i++ ) {
345 if( !slap_valid_descr( cr->cr_names[i] ) ) {
346 return SLAP_SCHERR_BAD_DESCR;
351 if ( !OID_LEADCHAR( cr->cr_oid[0] )) {
352 /* Expand OID macros */
353 char *oid = oidm_find( cr->cr_oid );
356 return SLAP_SCHERR_OIDM;
358 if ( oid != cr->cr_oid ) {
364 scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
365 AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
367 scr->scr_oidmacro = oidm;
368 scr->scr_sclass = oc_find(cr->cr_oid);
369 if ( !scr->scr_sclass ) {
371 code = SLAP_SCHERR_CLASS_NOT_FOUND;
375 /* check object class usage */
376 if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL )
379 code = 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 ) goto fail;
388 code = cr_create_required( scr, &op, err );
389 if ( code != 0 ) goto fail;
391 code = cr_create_allowed( scr, &op, err );
392 if ( code != 0 ) goto fail;
394 code = cr_create_precluded( scr, &op, err );
395 if ( code != 0 ) goto fail;
398 code = SLAP_SCHERR_CR_BAD_AUX;
402 code = cr_insert(scr,err);
403 if ( code == 0 && rscr )
412 cr_unparse( BerVarray *res, ContentRule *start, ContentRule *end, int sys )
416 struct berval bv, *bva = NULL, idx;
420 start = LDAP_STAILQ_FIRST( &cr_list );
422 /* count the result size */
424 for ( cr=start; cr; cr=LDAP_STAILQ_NEXT(cr, scr_next)) {
425 if ( sys && !(cr->scr_flags & SLAP_CR_HARDCODE)) continue;
427 if ( cr == end ) break;
432 bva = ch_malloc( (num+1) * sizeof(struct berval) );
440 for ( cr=start; cr; cr=LDAP_STAILQ_NEXT(cr, scr_next)) {
441 LDAPContentRule lcr, *lcrp;
442 if ( sys && !(cr->scr_flags & SLAP_CR_HARDCODE)) continue;
443 if ( cr->scr_oidmacro ) {
445 lcr.cr_oid = cr->scr_oidmacro;
448 lcrp = &cr->scr_crule;
450 if ( ldap_contentrule2bv( lcrp, &bv ) == NULL ) {
451 ber_bvarray_free( bva );
454 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
456 bva[i].bv_len = idx.bv_len + bv.bv_len;
457 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
458 strcpy( bva[i].bv_val, ibuf );
459 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
461 bva[i].bv_val = NULL;
462 ldap_memfree( bv.bv_val );
463 if ( cr == end ) break;
469 cr_schema_info( Entry *e )
471 AttributeDescription *ad_ditContentRules
472 = slap_schema.si_ad_ditContentRules;
478 LDAP_STAILQ_FOREACH(cr, &cr_list, scr_next) {
479 if ( ldap_contentrule2bv( &cr->scr_crule, &val ) == NULL ) {
484 if( cr->scr_flags & SLAP_CR_HIDE ) continue;
487 Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n",
488 (long) val.bv_len, val.bv_val, 0 );
491 nval.bv_val = cr->scr_oid;
492 nval.bv_len = strlen(cr->scr_oid);
494 if( attr_merge_one( e, ad_ditContentRules, &val, &nval ) )
498 ldap_memfree( val.bv_val );