]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Happy new year!
[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-2006 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 Attribute *
44 attr_alloc( AttributeDescription *ad )
45 {
46         Attribute *a = ch_malloc( sizeof(Attribute) );
47
48         a->a_desc = ad;
49         a->a_next = NULL;
50         a->a_flags = 0;
51         a->a_vals = NULL;
52         a->a_nvals = NULL;
53 #ifdef LDAP_COMP_MATCH
54         a->a_comp_data = NULL;
55 #endif
56
57         return a;
58 }
59
60 void
61 attr_free( Attribute *a )
62 {
63         if ( a->a_nvals && a->a_nvals != a->a_vals ) {
64                 ber_bvarray_free( a->a_nvals );
65         }
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"
69          * bit set. */
70         if ( a->a_vals != &slap_dummy_bv ) {
71                 ber_bvarray_free( a->a_vals );
72         }
73         free( a );
74 }
75
76 #ifdef LDAP_COMP_MATCH
77 void
78 comp_tree_free( Attribute *a )
79 {
80         Attribute *next;
81
82         for( ; a != NULL ; a = next ) {
83                 next = a->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 );
88                 }
89         }
90 }
91 #endif
92
93 void
94 attrs_free( Attribute *a )
95 {
96         Attribute *next;
97
98         for( ; a != NULL ; a = next ) {
99                 next = a->a_next;
100                 attr_free( a );
101         }
102 }
103
104 Attribute *
105 attr_dup( Attribute *a )
106 {
107         Attribute *tmp;
108
109         if ( a == NULL) return NULL;
110
111         tmp = attr_alloc( a->a_desc );
112
113         if ( a->a_vals != NULL ) {
114                 int     i;
115
116                 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
117                         /* EMPTY */ ;
118                 }
119
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;
124                         /* FIXME: error? */
125                 }
126                 BER_BVZERO( &tmp->a_vals[i] );
127
128                 /* a_nvals must be non null; it may be equal to a_vals */
129                 assert( a->a_nvals != NULL );
130
131                 if ( a->a_nvals != a->a_vals ) {
132                         int     j;
133
134                         tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
135                         for ( j = 0; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
136                                 assert( j < i );
137                                 ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
138                                 if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
139                                 /* FIXME: error? */
140                         }
141                         assert( j == i );
142                         BER_BVZERO( &tmp->a_nvals[j] );
143
144                 } else {
145                         tmp->a_nvals = tmp->a_vals;
146                 }
147
148         } else {
149                 tmp->a_vals = NULL;
150                 tmp->a_nvals = NULL;
151         }
152         return tmp;
153 }
154
155 Attribute *
156 attrs_dup( Attribute *a )
157 {
158         Attribute *tmp, **next;
159
160         if( a == NULL ) return NULL;
161
162         tmp = NULL;
163         next = &tmp;
164
165         for( ; a != NULL ; a = a->a_next ) {
166                 *next = attr_dup( a );
167                 next = &((*next)->a_next);
168         }
169         *next = NULL;
170
171         return tmp;
172 }
173
174
175 /*
176  * attr_merge - merge the given type and value with the list of
177  * attributes in attrs.
178  *
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.
181  *
182  * returns      0       everything went ok
183  *              -1      trouble
184  */
185
186 int
187 attr_merge(
188         Entry           *e,
189         AttributeDescription *desc,
190         BerVarray       vals,
191         BerVarray       nvals )
192 {
193         int rc;
194
195         Attribute       **a;
196
197         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
198                 if (  (*a)->a_desc == desc ) {
199                         break;
200                 }
201         }
202
203         if ( *a == NULL ) {
204                 *a = attr_alloc( desc );
205         } else {
206                 /*
207                  * FIXME: if the attribute already exists, the presence
208                  * of nvals and the value of (*a)->a_nvals must be consistent
209                  */
210                 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
211                                 || ( nvals != NULL && (*a)->a_nvals != (*a)->a_vals ) );
212         }
213
214         rc = value_add( &(*a)->a_vals, vals );
215
216         if ( rc == LDAP_SUCCESS ) {
217                 if ( nvals ) {
218                         rc = value_add( &(*a)->a_nvals, nvals );
219                         /* FIXME: what if rc != LDAP_SUCCESS ? */
220                 } else {
221                         (*a)->a_nvals = (*a)->a_vals;
222                 }
223         }
224
225         return rc;
226 }
227
228 int
229 attr_merge_normalize(
230         Entry           *e,
231         AttributeDescription *desc,
232         BerVarray       vals,
233         void     *memctx )
234 {
235         BerVarray       nvals = NULL;
236         int             rc;
237
238         if ( desc->ad_type->sat_equality &&
239                 desc->ad_type->sat_equality->smr_normalize )
240         {
241                 int     i;
242                 
243                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
244
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 );
252
253                         if ( rc != LDAP_SUCCESS ) {
254                                 BER_BVZERO( &nvals[i + 1] );
255                                 goto error_return;
256                         }
257                 }
258                 BER_BVZERO( &nvals[i] );
259         }
260
261         rc = attr_merge( e, desc, vals, nvals );
262
263 error_return:;
264         if ( nvals != NULL ) {
265                 ber_bvarray_free_x( nvals, memctx );
266         }
267         return rc;
268 }
269
270 int
271 attr_merge_one(
272         Entry           *e,
273         AttributeDescription *desc,
274         struct berval   *val,
275         struct berval   *nval )
276 {
277         int rc;
278         Attribute       **a;
279
280         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
281                 if ( (*a)->a_desc == desc ) {
282                         break;
283                 }
284         }
285
286         if ( *a == NULL ) {
287                 *a = attr_alloc( desc );
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