1 /* attr.c - routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
37 #include <ac/socket.h>
38 #include <ac/string.h>
44 * Allocate in chunks, minimum of 1000 at a time.
46 #define CHUNK_SIZE 1000
47 typedef struct slap_list {
48 struct slap_list *next;
50 static slap_list *attr_chunks;
51 static Attribute *attr_list;
52 static ldap_pvt_thread_mutex_t attr_mutex;
55 attr_prealloc( int num )
62 s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Attribute));
63 s->next = attr_chunks;
66 a = (Attribute *)(s+1);
67 for ( ;num>1; num--) {
71 a->a_next = attr_list;
72 attr_list = (Attribute *)(s+1);
78 attr_alloc( AttributeDescription *ad )
82 ldap_pvt_thread_mutex_lock( &attr_mutex );
84 attr_prealloc( CHUNK_SIZE );
86 attr_list = a->a_next;
88 ldap_pvt_thread_mutex_unlock( &attr_mutex );
95 /* Return a list of num attrs */
97 attrs_alloc( int num )
99 Attribute *head = NULL;
102 ldap_pvt_thread_mutex_lock( &attr_mutex );
103 for ( a = &attr_list; *a && num > 0; a = &(*a)->a_next ) {
110 attr_prealloc( num > CHUNK_SIZE ? num : CHUNK_SIZE );
112 for ( ; *a && num > 0; a = &(*a)->a_next ) {
120 ldap_pvt_thread_mutex_unlock( &attr_mutex );
127 attr_clean( Attribute *a )
129 if ( a->a_nvals && a->a_nvals != a->a_vals &&
130 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
131 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
134 ber_bvarray_free( a->a_nvals );
137 /* a_vals may be equal to slap_dummy_bv, a static empty berval;
138 * this is used as a placeholder for attributes that do not carry
139 * values, e.g. when proxying search entries with the "attrsonly"
141 if ( a->a_vals != &slap_dummy_bv &&
142 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
143 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
146 ber_bvarray_free( a->a_vals );
152 #ifdef LDAP_COMP_MATCH
153 a->a_comp_data = NULL;
159 attr_free( Attribute *a )
162 ldap_pvt_thread_mutex_lock( &attr_mutex );
163 a->a_next = attr_list;
165 ldap_pvt_thread_mutex_unlock( &attr_mutex );
168 #ifdef LDAP_COMP_MATCH
170 comp_tree_free( Attribute *a )
174 for( ; a != NULL ; a = next ) {
176 if ( component_destructor && a->a_comp_data ) {
177 if ( a->a_comp_data->cd_mem_op )
178 component_destructor( a->a_comp_data->cd_mem_op );
179 free ( a->a_comp_data );
186 attrs_free( Attribute *a )
188 Attribute *b, *tail, *next;
200 ldap_pvt_thread_mutex_lock( &attr_mutex );
201 tail->a_next = attr_list;
203 ldap_pvt_thread_mutex_unlock( &attr_mutex );
208 attr_dup2( Attribute *tmp, Attribute *a )
210 if ( a->a_vals != NULL ) {
213 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
217 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
218 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
219 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
220 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
223 BER_BVZERO( &tmp->a_vals[i] );
225 /* a_nvals must be non null; it may be equal to a_vals */
226 assert( a->a_nvals != NULL );
228 if ( a->a_nvals != a->a_vals ) {
231 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
232 for ( j = 0; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
234 ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
235 if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
239 BER_BVZERO( &tmp->a_nvals[j] );
242 tmp->a_nvals = tmp->a_vals;
248 attr_dup( Attribute *a )
252 if ( a == NULL) return NULL;
254 tmp = attr_alloc( a->a_desc );
260 attrs_dup( Attribute *a )
263 Attribute *tmp, *anew;
265 if( a == NULL ) return NULL;
268 for( tmp=a,i=0; tmp; tmp=tmp->a_next ) {
272 anew = attrs_alloc( i );
274 for( tmp=anew; a; a=a->a_next ) {
275 tmp->a_desc = a->a_desc;
285 * attr_merge - merge the given type and value with the list of
286 * attributes in attrs.
288 * nvals must be NULL if the attribute has no normalizer.
289 * In this case, a->a_nvals will be set equal to a->a_vals.
291 * returns 0 everything went ok
298 AttributeDescription *desc,
306 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
307 if ( (*a)->a_desc == desc ) {
313 *a = attr_alloc( desc );
316 * FIXME: if the attribute already exists, the presence
317 * of nvals and the value of (*a)->a_nvals must be consistent
319 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
320 || ( nvals != NULL && (
321 ( (*a)->a_vals == NULL && (*a)->a_nvals == NULL )
322 || ( (*a)->a_nvals != (*a)->a_vals ) ) ) );
325 rc = value_add( &(*a)->a_vals, vals );
327 if ( rc == LDAP_SUCCESS ) {
329 rc = value_add( &(*a)->a_nvals, nvals );
330 /* FIXME: what if rc != LDAP_SUCCESS ? */
332 (*a)->a_nvals = (*a)->a_vals;
340 * if a normalization function is defined for the equality matchingRule
341 * of desc, the value is normalized and stored in nval; otherwise nval
346 AttributeDescription *desc,
351 int rc = LDAP_SUCCESS;
352 BerVarray nvals = NULL;
356 if ( desc->ad_type->sat_equality &&
357 desc->ad_type->sat_equality->smr_normalize )
361 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
363 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
364 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
365 rc = desc->ad_type->sat_equality->smr_normalize(
366 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
367 desc->ad_type->sat_syntax,
368 desc->ad_type->sat_equality,
369 &vals[i], &nvals[i], memctx );
371 if ( rc != LDAP_SUCCESS ) {
372 BER_BVZERO( &nvals[i + 1] );
376 BER_BVZERO( &nvals[i] );
380 if ( rc != LDAP_SUCCESS && nvals != NULL ) {
381 ber_bvarray_free_x( nvals, memctx );
388 attr_merge_normalize(
390 AttributeDescription *desc,
394 BerVarray nvals = NULL;
397 rc = attr_normalize( desc, vals, &nvals, memctx );
398 if ( rc == LDAP_SUCCESS ) {
399 rc = attr_merge( e, desc, vals, nvals );
400 if ( nvals != NULL ) {
401 ber_bvarray_free_x( nvals, memctx );
411 AttributeDescription *desc,
413 struct berval *nval )
418 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
419 if ( (*a)->a_desc == desc ) {
425 *a = attr_alloc( desc );
428 rc = value_add_one( &(*a)->a_vals, val );
430 if ( rc == LDAP_SUCCESS ) {
432 rc = value_add_one( &(*a)->a_nvals, nval );
433 /* FIXME: what if rc != LDAP_SUCCESS ? */
435 (*a)->a_nvals = (*a)->a_vals;
442 * if a normalization function is defined for the equality matchingRule
443 * of desc, the value is normalized and stored in nval; otherwise nval
448 AttributeDescription *desc,
453 int rc = LDAP_SUCCESS;
457 if ( desc->ad_type->sat_equality &&
458 desc->ad_type->sat_equality->smr_normalize )
460 rc = desc->ad_type->sat_equality->smr_normalize(
461 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
462 desc->ad_type->sat_syntax,
463 desc->ad_type->sat_equality,
466 if ( rc != LDAP_SUCCESS ) {
475 attr_merge_normalize_one(
477 AttributeDescription *desc,
481 struct berval nval = BER_BVNULL;
482 struct berval *nvalp = NULL;
485 rc = attr_normalize_one( desc, val, &nval, memctx );
486 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &nval ) ) {
490 rc = attr_merge_one( e, desc, val, nvalp );
491 if ( nvalp != NULL ) {
492 slap_sl_free( nval.bv_val, memctx );
498 * attrs_find - find attribute(s) by AttributeDescription
499 * returns next attribute which is subtype of provided description.
505 AttributeDescription *desc )
507 for ( ; a != NULL; a = a->a_next ) {
508 if ( is_ad_subtype( a->a_desc, desc ) ) {
517 * attr_find - find attribute by type
523 AttributeDescription *desc )
525 for ( ; a != NULL; a = a->a_next ) {
526 if ( a->a_desc == desc ) {
535 * attr_delete - delete the attribute type in list pointed to by attrs
536 * return 0 deleted ok
537 * 1 not found in list a
538 * -1 something bad happened
544 AttributeDescription *desc )
548 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
549 if ( (*a)->a_desc == desc ) {
550 Attribute *save = *a;
558 return LDAP_NO_SUCH_ATTRIBUTE;
564 ldap_pvt_thread_mutex_init( &attr_mutex );
573 for ( a=attr_chunks; a; a=attr_chunks ) {
574 attr_chunks = a->next;
577 ldap_pvt_thread_mutex_destroy( &attr_mutex );