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