]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Better fix for #3671
[openldap] / servers / slapd / attr.c
1 /* attr.c - routines for dealing with attributes */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
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.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #endif
34
35 #include <ac/ctype.h>
36 #include <ac/errno.h>
37 #include <ac/socket.h>
38 #include <ac/string.h>
39 #include <ac/time.h>
40
41 #include "slap.h"
42
43 void
44 attr_free( Attribute *a )
45 {
46         if ( a->a_nvals && a->a_nvals != a->a_vals ) {
47                 ber_bvarray_free( a->a_nvals );
48         }
49         ber_bvarray_free( a->a_vals );
50         free( a );
51 }
52
53 #ifdef LDAP_COMP_MATCH
54 void
55 comp_tree_free( Attribute *a )
56 {
57         Attribute *next;
58
59         for( ; a != NULL ; a = next ) {
60                 next = a->a_next;
61                 if ( component_destructor && a->a_comp_data ) {
62                         if ( a->a_comp_data->cd_mem_op )
63                                 component_destructor( a->a_comp_data->cd_mem_op );
64                         free ( a->a_comp_data );
65                 }
66         }
67 }
68 #endif
69
70 void
71 attrs_free( Attribute *a )
72 {
73         Attribute *next;
74
75         for( ; a != NULL ; a = next ) {
76                 next = a->a_next;
77                 attr_free( a );
78         }
79 }
80
81 Attribute *
82 attr_dup( Attribute *a )
83 {
84         Attribute *tmp;
85
86         if ( a == NULL) return NULL;
87
88         tmp = ch_malloc( sizeof(Attribute) );
89
90         if ( a->a_vals != NULL ) {
91                 int i;
92
93                 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
94                         /* EMPTY */ ;
95                 }
96
97                 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
98                 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
99                         ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
100                         if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
101                         /* FIXME: error? */
102                 }
103                 BER_BVZERO( &tmp->a_vals[i] );
104
105                 /* a_nvals must be non null; it may be equal to a_vals */
106                 assert( a->a_nvals );
107
108                 if ( a->a_nvals != a->a_vals ) {
109                         tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
110                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
111                                 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
112                                 if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
113                                 /* FIXME: error? */
114                         }
115                         BER_BVZERO( &tmp->a_nvals[i] );
116
117                 } else {
118                         tmp->a_nvals = tmp->a_vals;
119                 }
120
121         } else {
122                 tmp->a_vals = NULL;
123                 tmp->a_nvals = NULL;
124         }
125
126         tmp->a_desc = a->a_desc;
127         tmp->a_next = NULL;
128         tmp->a_flags = 0;
129 #ifdef LDAP_COMP_MATCH
130         tmp->a_comp_data = NULL;
131 #endif
132
133         return tmp;
134 }
135
136 Attribute *
137 attrs_dup( Attribute *a )
138 {
139         Attribute *tmp, **next;
140
141         if( a == NULL ) return NULL;
142
143         tmp = NULL;
144         next = &tmp;
145
146         for( ; a != NULL ; a = a->a_next ) {
147                 *next = attr_dup( a );
148                 next = &((*next)->a_next);
149         }
150         *next = NULL;
151
152         return tmp;
153 }
154
155
156
157 /*
158  * attr_merge - merge the given type and value with the list of
159  * attributes in attrs.
160  *
161  * nvals must be NULL if the attribute has no normalizer.
162  * In this case, a->a_nvals will be set equal to a->a_vals.
163  *
164  * returns      0       everything went ok
165  *              -1      trouble
166  */
167
168 int
169 attr_merge(
170         Entry           *e,
171         AttributeDescription *desc,
172         BerVarray       vals,
173         BerVarray       nvals )
174 {
175         int rc;
176
177         Attribute       **a;
178
179         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
180                 if (  (*a)->a_desc == desc ) {
181                         break;
182                 }
183         }
184
185         if ( *a == NULL ) {
186                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
187                 (*a)->a_desc = desc;
188                 (*a)->a_vals = NULL;
189                 (*a)->a_nvals = NULL;
190                 (*a)->a_next = NULL;
191                 (*a)->a_flags = 0;
192 #ifdef LDAP_COMP_MATCH
193                 (*a)->a_comp_data = NULL;
194 #endif
195         } else {
196                 /*
197                  * FIXME: if the attribute already exists, the presence
198                  * of nvals and the value of (*a)->a_nvals must be consistent
199                  */
200                 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
201                                 || ( nvals != NULL && (*a)->a_nvals != (*a)->a_vals ) );
202         }
203
204         rc = value_add( &(*a)->a_vals, vals );
205
206         if ( rc == LDAP_SUCCESS ) {
207                 if ( nvals ) {
208                         rc = value_add( &(*a)->a_nvals, nvals );
209                         /* FIXME: what if rc != LDAP_SUCCESS ? */
210                 } else {
211                         (*a)->a_nvals = (*a)->a_vals;
212                 }
213         }
214
215         return rc;
216 }
217
218 int
219 attr_merge_normalize(
220         Entry           *e,
221         AttributeDescription *desc,
222         BerVarray       vals,
223         void     *memctx )
224 {
225         BerVarray       nvals = NULL;
226         int             rc;
227
228         if ( desc->ad_type->sat_equality &&
229                 desc->ad_type->sat_equality->smr_normalize )
230         {
231                 int     i;
232                 
233                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
234
235                 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
236                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
237                         rc = (*desc->ad_type->sat_equality->smr_normalize)(
238                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
239                                         desc->ad_type->sat_syntax,
240                                         desc->ad_type->sat_equality,
241                                         &vals[i], &nvals[i], memctx );
242
243                         if ( rc != LDAP_SUCCESS ) {
244                                 BER_BVZERO( &nvals[i + 1] );
245                                 goto error_return;
246                         }
247                 }
248                 BER_BVZERO( &nvals[i] );
249         }
250
251         rc = attr_merge( e, desc, vals, nvals );
252
253 error_return:;
254         if ( nvals != NULL ) {
255                 ber_bvarray_free_x( nvals, memctx );
256         }
257         return rc;
258 }
259
260 int
261 attr_merge_one(
262         Entry           *e,
263         AttributeDescription *desc,
264         struct berval   *val,
265         struct berval   *nval )
266 {
267         int rc;
268         Attribute       **a;
269
270         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
271                 if ( (*a)->a_desc == desc ) {
272                         break;
273                 }
274         }
275
276         if ( *a == NULL ) {
277                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
278                 (*a)->a_desc = desc;
279                 (*a)->a_vals = NULL;
280                 (*a)->a_nvals = NULL;
281                 (*a)->a_next = NULL;
282                 (*a)->a_flags = 0;
283 #ifdef LDAP_COMP_MATCH
284                 (*a)->a_comp_data = NULL;
285 #endif
286         }
287
288         rc = value_add_one( &(*a)->a_vals, val );
289
290         if ( rc == LDAP_SUCCESS ) {
291                 if ( nval ) {
292                         rc = value_add_one( &(*a)->a_nvals, nval );
293                         /* FIXME: what if rc != LDAP_SUCCESS ? */
294                 } else {
295                         (*a)->a_nvals = (*a)->a_vals;
296                 }
297         }
298         return rc;
299 }
300
301 int
302 attr_merge_normalize_one(
303         Entry           *e,
304         AttributeDescription *desc,
305         struct berval   *val,
306         void            *memctx )
307 {
308         struct berval   nval;
309         struct berval   *nvalp = NULL;
310         int             rc;
311
312         if ( desc->ad_type->sat_equality &&
313                 desc->ad_type->sat_equality->smr_normalize )
314         {
315                 rc = (*desc->ad_type->sat_equality->smr_normalize)(
316                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
317                                 desc->ad_type->sat_syntax,
318                                 desc->ad_type->sat_equality,
319                                 val, &nval, memctx );
320
321                 if ( rc != LDAP_SUCCESS ) {
322                         return rc;
323                 }
324                 nvalp = &nval;
325         }
326
327         rc = attr_merge_one( e, desc, val, nvalp );
328         if ( nvalp != NULL ) {
329                 slap_sl_free( nval.bv_val, memctx );
330         }
331         return rc;
332 }
333
334 /*
335  * attrs_find - find attribute(s) by AttributeDescription
336  * returns next attribute which is subtype of provided description.
337  */
338
339 Attribute *
340 attrs_find(
341     Attribute   *a,
342         AttributeDescription *desc )
343 {
344         for ( ; a != NULL; a = a->a_next ) {
345                 if ( is_ad_subtype( a->a_desc, desc ) ) {
346                         return( a );
347                 }
348         }
349
350         return( NULL );
351 }
352
353 /*
354  * attr_find - find attribute by type
355  */
356
357 Attribute *
358 attr_find(
359     Attribute   *a,
360         AttributeDescription *desc )
361 {
362         for ( ; a != NULL; a = a->a_next ) {
363                 if ( a->a_desc == desc ) {
364                         return( a );
365                 }
366         }
367
368         return( NULL );
369 }
370
371 /*
372  * attr_delete - delete the attribute type in list pointed to by attrs
373  * return       0       deleted ok
374  *              1       not found in list a
375  *              -1      something bad happened
376  */
377
378 int
379 attr_delete(
380     Attribute   **attrs,
381         AttributeDescription *desc )
382 {
383         Attribute       **a;
384
385         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
386                 if ( (*a)->a_desc == desc ) {
387                         Attribute       *save = *a;
388                         *a = (*a)->a_next;
389                         attr_free( save );
390
391                         return LDAP_SUCCESS;
392                 }
393         }
394
395         return LDAP_NO_SUCH_ATTRIBUTE;
396 }
397