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_free( Attribute *a )
46 if ( a->a_nvals && a->a_nvals != a->a_vals ) {
47 ber_bvarray_free( a->a_nvals );
49 /* a_vals may be equal to slap_dummy_bv, a static empty berval;
50 * this is used as a placeholder for attributes that do not carry
51 * values, e.g. when proxying search entries with the "attrsonly"
53 if ( a->a_vals != &slap_dummy_bv ) {
54 ber_bvarray_free( a->a_vals );
59 #ifdef LDAP_COMP_MATCH
61 comp_tree_free( Attribute *a )
65 for( ; a != NULL ; a = next ) {
67 if ( component_destructor && a->a_comp_data ) {
68 if ( a->a_comp_data->cd_mem_op )
69 component_destructor( a->a_comp_data->cd_mem_op );
70 free ( a->a_comp_data );
77 attrs_free( Attribute *a )
81 for( ; a != NULL ; a = next ) {
88 attr_dup( Attribute *a )
92 if ( a == NULL) return NULL;
94 tmp = ch_malloc( sizeof(Attribute) );
96 if ( a->a_vals != NULL ) {
99 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
103 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
104 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
105 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
106 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
109 BER_BVZERO( &tmp->a_vals[i] );
111 /* a_nvals must be non null; it may be equal to a_vals */
112 assert( a->a_nvals );
114 if ( a->a_nvals != a->a_vals ) {
115 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
116 for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
117 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
118 if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
121 BER_BVZERO( &tmp->a_nvals[i] );
124 tmp->a_nvals = tmp->a_vals;
132 tmp->a_desc = a->a_desc;
135 #ifdef LDAP_COMP_MATCH
136 tmp->a_comp_data = NULL;
143 attrs_dup( Attribute *a )
145 Attribute *tmp, **next;
147 if( a == NULL ) return NULL;
152 for( ; a != NULL ; a = a->a_next ) {
153 *next = attr_dup( a );
154 next = &((*next)->a_next);
164 * attr_merge - merge the given type and value with the list of
165 * attributes in attrs.
167 * nvals must be NULL if the attribute has no normalizer.
168 * In this case, a->a_nvals will be set equal to a->a_vals.
170 * returns 0 everything went ok
177 AttributeDescription *desc,
185 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
186 if ( (*a)->a_desc == desc ) {
192 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
195 (*a)->a_nvals = NULL;
198 #ifdef LDAP_COMP_MATCH
199 (*a)->a_comp_data = NULL;
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 = (Attribute *) ch_malloc( sizeof(Attribute) );
286 (*a)->a_nvals = NULL;
289 #ifdef LDAP_COMP_MATCH
290 (*a)->a_comp_data = NULL;
294 rc = value_add_one( &(*a)->a_vals, val );
296 if ( rc == LDAP_SUCCESS ) {
298 rc = value_add_one( &(*a)->a_nvals, nval );
299 /* FIXME: what if rc != LDAP_SUCCESS ? */
301 (*a)->a_nvals = (*a)->a_vals;
308 attr_merge_normalize_one(
310 AttributeDescription *desc,
315 struct berval *nvalp = NULL;
318 if ( desc->ad_type->sat_equality &&
319 desc->ad_type->sat_equality->smr_normalize )
321 rc = (*desc->ad_type->sat_equality->smr_normalize)(
322 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
323 desc->ad_type->sat_syntax,
324 desc->ad_type->sat_equality,
325 val, &nval, memctx );
327 if ( rc != LDAP_SUCCESS ) {
333 rc = attr_merge_one( e, desc, val, nvalp );
334 if ( nvalp != NULL ) {
335 slap_sl_free( nval.bv_val, memctx );
341 * attrs_find - find attribute(s) by AttributeDescription
342 * returns next attribute which is subtype of provided description.
348 AttributeDescription *desc )
350 for ( ; a != NULL; a = a->a_next ) {
351 if ( is_ad_subtype( a->a_desc, desc ) ) {
360 * attr_find - find attribute by type
366 AttributeDescription *desc )
368 for ( ; a != NULL; a = a->a_next ) {
369 if ( a->a_desc == desc ) {
378 * attr_delete - delete the attribute type in list pointed to by attrs
379 * return 0 deleted ok
380 * 1 not found in list a
381 * -1 something bad happened
387 AttributeDescription *desc )
391 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
392 if ( (*a)->a_desc == desc ) {
393 Attribute *save = *a;
401 return LDAP_NO_SUCH_ATTRIBUTE;