]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
7042d18c2b38f4e7a4cc0df377755101800e877c
[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 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 );
130
131                 if ( a->a_nvals != a->a_vals ) {
132                         tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
133                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
134                                 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
135                                 if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
136                                 /* FIXME: error? */
137                         }
138                         BER_BVZERO( &tmp->a_nvals[i] );
139
140                 } else {
141                         tmp->a_nvals = tmp->a_vals;
142                 }
143
144         } else {
145                 tmp->a_vals = NULL;
146                 tmp->a_nvals = NULL;
147         }
148         return tmp;
149 }
150
151 Attribute *
152 attrs_dup( Attribute *a )
153 {
154         Attribute *tmp, **next;
155
156         if( a == NULL ) return NULL;
157
158         tmp = NULL;
159         next = &tmp;
160
161         for( ; a != NULL ; a = a->a_next ) {
162                 *next = attr_dup( a );
163                 next = &((*next)->a_next);
164         }
165         *next = NULL;
166
167         return tmp;
168 }
169
170
171 /*
172  * attr_merge - merge the given type and value with the list of
173  * attributes in attrs.
174  *
175  * nvals must be NULL if the attribute has no normalizer.
176  * In this case, a->a_nvals will be set equal to a->a_vals.
177  *
178  * returns      0       everything went ok
179  *              -1      trouble
180  */
181
182 int
183 attr_merge(
184         Entry           *e,
185         AttributeDescription *desc,
186         BerVarray       vals,
187         BerVarray       nvals )
188 {
189         int rc;
190
191         Attribute       **a;
192
193         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
194                 if (  (*a)->a_desc == desc ) {
195                         break;
196                 }
197         }
198
199         if ( *a == NULL ) {
200                 *a = attr_alloc( desc );
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 = attr_alloc( desc );
284         }
285
286         rc = value_add_one( &(*a)->a_vals, val );
287
288         if ( rc == LDAP_SUCCESS ) {
289                 if ( nval ) {
290                         rc = value_add_one( &(*a)->a_nvals, nval );
291                         /* FIXME: what if rc != LDAP_SUCCESS ? */
292                 } else {
293                         (*a)->a_nvals = (*a)->a_vals;
294                 }
295         }
296         return rc;
297 }
298
299 int
300 attr_merge_normalize_one(
301         Entry           *e,
302         AttributeDescription *desc,
303         struct berval   *val,
304         void            *memctx )
305 {
306         struct berval   nval;
307         struct berval   *nvalp = NULL;
308         int             rc;
309
310         if ( desc->ad_type->sat_equality &&
311                 desc->ad_type->sat_equality->smr_normalize )
312         {
313                 rc = (*desc->ad_type->sat_equality->smr_normalize)(
314                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
315                                 desc->ad_type->sat_syntax,
316                                 desc->ad_type->sat_equality,
317                                 val, &nval, memctx );
318
319                 if ( rc != LDAP_SUCCESS ) {
320                         return rc;
321                 }
322                 nvalp = &nval;
323         }
324
325         rc = attr_merge_one( e, desc, val, nvalp );
326         if ( nvalp != NULL ) {
327                 slap_sl_free( nval.bv_val, memctx );
328         }
329         return rc;
330 }
331
332 /*
333  * attrs_find - find attribute(s) by AttributeDescription
334  * returns next attribute which is subtype of provided description.
335  */
336
337 Attribute *
338 attrs_find(
339     Attribute   *a,
340         AttributeDescription *desc )
341 {
342         for ( ; a != NULL; a = a->a_next ) {
343                 if ( is_ad_subtype( a->a_desc, desc ) ) {
344                         return( a );
345                 }
346         }
347
348         return( NULL );
349 }
350
351 /*
352  * attr_find - find attribute by type
353  */
354
355 Attribute *
356 attr_find(
357     Attribute   *a,
358         AttributeDescription *desc )
359 {
360         for ( ; a != NULL; a = a->a_next ) {
361                 if ( a->a_desc == desc ) {
362                         return( a );
363                 }
364         }
365
366         return( NULL );
367 }
368
369 /*
370  * attr_delete - delete the attribute type in list pointed to by attrs
371  * return       0       deleted ok
372  *              1       not found in list a
373  *              -1      something bad happened
374  */
375
376 int
377 attr_delete(
378     Attribute   **attrs,
379         AttributeDescription *desc )
380 {
381         Attribute       **a;
382
383         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
384                 if ( (*a)->a_desc == desc ) {
385                         Attribute       *save = *a;
386                         *a = (*a)->a_next;
387                         attr_free( save );
388
389                         return LDAP_SUCCESS;
390                 }
391         }
392
393         return LDAP_NO_SUCH_ATTRIBUTE;
394 }
395