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