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