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 ) {
134 tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
135 for ( j = 0; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
137 ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
138 if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
142 BER_BVZERO( &tmp->a_nvals[j] );
145 tmp->a_nvals = tmp->a_vals;
156 attrs_dup( Attribute *a )
158 Attribute *tmp, **next;
160 if( a == NULL ) return NULL;
165 for( ; a != NULL ; a = a->a_next ) {
166 *next = attr_dup( a );
167 next = &((*next)->a_next);
176 * attr_merge - merge the given type and value with the list of
177 * attributes in attrs.
179 * nvals must be NULL if the attribute has no normalizer.
180 * In this case, a->a_nvals will be set equal to a->a_vals.
182 * returns 0 everything went ok
189 AttributeDescription *desc,
197 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
198 if ( (*a)->a_desc == desc ) {
204 *a = attr_alloc( desc );
207 * FIXME: if the attribute already exists, the presence
208 * of nvals and the value of (*a)->a_nvals must be consistent
210 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
211 || ( nvals != NULL && (*a)->a_nvals != (*a)->a_vals ) );
214 rc = value_add( &(*a)->a_vals, vals );
216 if ( rc == LDAP_SUCCESS ) {
218 rc = value_add( &(*a)->a_nvals, nvals );
219 /* FIXME: what if rc != LDAP_SUCCESS ? */
221 (*a)->a_nvals = (*a)->a_vals;
229 attr_merge_normalize(
231 AttributeDescription *desc,
235 BerVarray nvals = NULL;
238 if ( desc->ad_type->sat_equality &&
239 desc->ad_type->sat_equality->smr_normalize )
243 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
245 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
246 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
247 rc = (*desc->ad_type->sat_equality->smr_normalize)(
248 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
249 desc->ad_type->sat_syntax,
250 desc->ad_type->sat_equality,
251 &vals[i], &nvals[i], memctx );
253 if ( rc != LDAP_SUCCESS ) {
254 BER_BVZERO( &nvals[i + 1] );
258 BER_BVZERO( &nvals[i] );
261 rc = attr_merge( e, desc, vals, nvals );
264 if ( nvals != NULL ) {
265 ber_bvarray_free_x( nvals, memctx );
273 AttributeDescription *desc,
275 struct berval *nval )
280 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
281 if ( (*a)->a_desc == desc ) {
287 *a = attr_alloc( desc );
290 rc = value_add_one( &(*a)->a_vals, val );
292 if ( rc == LDAP_SUCCESS ) {
294 rc = value_add_one( &(*a)->a_nvals, nval );
295 /* FIXME: what if rc != LDAP_SUCCESS ? */
297 (*a)->a_nvals = (*a)->a_vals;
304 attr_merge_normalize_one(
306 AttributeDescription *desc,
311 struct berval *nvalp = NULL;
314 if ( desc->ad_type->sat_equality &&
315 desc->ad_type->sat_equality->smr_normalize )
317 rc = (*desc->ad_type->sat_equality->smr_normalize)(
318 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
319 desc->ad_type->sat_syntax,
320 desc->ad_type->sat_equality,
321 val, &nval, memctx );
323 if ( rc != LDAP_SUCCESS ) {
329 rc = attr_merge_one( e, desc, val, nvalp );
330 if ( nvalp != NULL ) {
331 slap_sl_free( nval.bv_val, memctx );
337 * attrs_find - find attribute(s) by AttributeDescription
338 * returns next attribute which is subtype of provided description.
344 AttributeDescription *desc )
346 for ( ; a != NULL; a = a->a_next ) {
347 if ( is_ad_subtype( a->a_desc, desc ) ) {
356 * attr_find - find attribute by type
362 AttributeDescription *desc )
364 for ( ; a != NULL; a = a->a_next ) {
365 if ( a->a_desc == desc ) {
374 * attr_delete - delete the attribute type in list pointed to by attrs
375 * return 0 deleted ok
376 * 1 not found in list a
377 * -1 something bad happened
383 AttributeDescription *desc )
387 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
388 if ( (*a)->a_desc == desc ) {
389 Attribute *save = *a;
397 return LDAP_NO_SUCH_ATTRIBUTE;