1 /* attr.c - routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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 attr_alloc( AttributeDescription *ad )
46 Attribute *a = ch_malloc( sizeof(Attribute) );
53 #ifdef LDAP_COMP_MATCH
54 a->a_comp_data = NULL;
61 attr_free( Attribute *a )
63 if ( a->a_nvals && a->a_nvals != a->a_vals ) {
64 ber_bvarray_free( a->a_nvals );
66 /* a_vals may be equal to slap_dummy_bv, a static empty berval;
67 * this is used as a placeholder for attributes that do not carry
68 * values, e.g. when proxying search entries with the "attrsonly"
70 if ( a->a_vals != &slap_dummy_bv ) {
71 ber_bvarray_free( a->a_vals );
76 #ifdef LDAP_COMP_MATCH
78 comp_tree_free( Attribute *a )
82 for( ; a != NULL ; a = next ) {
84 if ( component_destructor && a->a_comp_data ) {
85 if ( a->a_comp_data->cd_mem_op )
86 component_destructor( a->a_comp_data->cd_mem_op );
87 free ( a->a_comp_data );
94 attrs_free( Attribute *a )
98 for( ; a != NULL ; a = next ) {
105 attr_dup( Attribute *a )
109 if ( a == NULL) return NULL;
111 tmp = attr_alloc( a->a_desc );
113 if ( a->a_vals != NULL ) {
116 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
120 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
121 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
122 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
123 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
126 BER_BVZERO( &tmp->a_vals[i] );
128 /* a_nvals must be non null; it may be equal to a_vals */
129 assert( a->a_nvals != NULL );
131 if ( a->a_nvals != a->a_vals ) {
132 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
133 for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
134 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
135 if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
138 BER_BVZERO( &tmp->a_nvals[i] );
141 tmp->a_nvals = tmp->a_vals;
152 attrs_dup( Attribute *a )
154 Attribute *tmp, **next;
156 if( a == NULL ) return NULL;
161 for( ; a != NULL ; a = a->a_next ) {
162 *next = attr_dup( a );
163 next = &((*next)->a_next);
172 * attr_merge - merge the given type and value with the list of
173 * attributes in attrs.
175 * nvals must be NULL if the attribute has no normalizer.
176 * In this case, a->a_nvals will be set equal to a->a_vals.
178 * returns 0 everything went ok
185 AttributeDescription *desc,
193 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
194 if ( (*a)->a_desc == desc ) {
200 *a = attr_alloc( desc );
203 * FIXME: if the attribute already exists, the presence
204 * of nvals and the value of (*a)->a_nvals must be consistent
206 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
207 || ( nvals != NULL && (*a)->a_nvals != (*a)->a_vals ) );
210 rc = value_add( &(*a)->a_vals, vals );
212 if ( rc == LDAP_SUCCESS ) {
214 rc = value_add( &(*a)->a_nvals, nvals );
215 /* FIXME: what if rc != LDAP_SUCCESS ? */
217 (*a)->a_nvals = (*a)->a_vals;
225 attr_merge_normalize(
227 AttributeDescription *desc,
231 BerVarray nvals = NULL;
234 if ( desc->ad_type->sat_equality &&
235 desc->ad_type->sat_equality->smr_normalize )
239 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
241 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
242 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
243 rc = (*desc->ad_type->sat_equality->smr_normalize)(
244 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
245 desc->ad_type->sat_syntax,
246 desc->ad_type->sat_equality,
247 &vals[i], &nvals[i], memctx );
249 if ( rc != LDAP_SUCCESS ) {
250 BER_BVZERO( &nvals[i + 1] );
254 BER_BVZERO( &nvals[i] );
257 rc = attr_merge( e, desc, vals, nvals );
260 if ( nvals != NULL ) {
261 ber_bvarray_free_x( nvals, memctx );
269 AttributeDescription *desc,
271 struct berval *nval )
276 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
277 if ( (*a)->a_desc == desc ) {
283 *a = attr_alloc( desc );
286 rc = value_add_one( &(*a)->a_vals, val );
288 if ( rc == LDAP_SUCCESS ) {
290 rc = value_add_one( &(*a)->a_nvals, nval );
291 /* FIXME: what if rc != LDAP_SUCCESS ? */
293 (*a)->a_nvals = (*a)->a_vals;
300 attr_merge_normalize_one(
302 AttributeDescription *desc,
307 struct berval *nvalp = NULL;
310 if ( desc->ad_type->sat_equality &&
311 desc->ad_type->sat_equality->smr_normalize )
313 rc = (*desc->ad_type->sat_equality->smr_normalize)(
314 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
315 desc->ad_type->sat_syntax,
316 desc->ad_type->sat_equality,
317 val, &nval, memctx );
319 if ( rc != LDAP_SUCCESS ) {
325 rc = attr_merge_one( e, desc, val, nvalp );
326 if ( nvalp != NULL ) {
327 slap_sl_free( nval.bv_val, memctx );
333 * attrs_find - find attribute(s) by AttributeDescription
334 * returns next attribute which is subtype of provided description.
340 AttributeDescription *desc )
342 for ( ; a != NULL; a = a->a_next ) {
343 if ( is_ad_subtype( a->a_desc, desc ) ) {
352 * attr_find - find attribute by type
358 AttributeDescription *desc )
360 for ( ; a != NULL; a = a->a_next ) {
361 if ( a->a_desc == desc ) {
370 * attr_delete - delete the attribute type in list pointed to by attrs
371 * return 0 deleted ok
372 * 1 not found in list a
373 * -1 something bad happened
379 AttributeDescription *desc )
383 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
384 if ( (*a)->a_desc == desc ) {
385 Attribute *save = *a;
393 return LDAP_NO_SUCH_ATTRIBUTE;