]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Fix comment style.
[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 && (
212                                         ( (*a)->a_vals == NULL && (*a)->a_nvals == NULL )
213                                         || ( (*a)->a_nvals != (*a)->a_vals ) ) ) );
214         }
215
216         rc = value_add( &(*a)->a_vals, vals );
217
218         if ( rc == LDAP_SUCCESS ) {
219                 if ( nvals ) {
220                         rc = value_add( &(*a)->a_nvals, nvals );
221                         /* FIXME: what if rc != LDAP_SUCCESS ? */
222                 } else {
223                         (*a)->a_nvals = (*a)->a_vals;
224                 }
225         }
226
227         return rc;
228 }
229
230 int
231 attr_merge_normalize(
232         Entry           *e,
233         AttributeDescription *desc,
234         BerVarray       vals,
235         void     *memctx )
236 {
237         BerVarray       nvals = NULL;
238         int             rc;
239
240         if ( desc->ad_type->sat_equality &&
241                 desc->ad_type->sat_equality->smr_normalize )
242         {
243                 int     i;
244                 
245                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
246
247                 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
248                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
249                         rc = (*desc->ad_type->sat_equality->smr_normalize)(
250                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
251                                         desc->ad_type->sat_syntax,
252                                         desc->ad_type->sat_equality,
253                                         &vals[i], &nvals[i], memctx );
254
255                         if ( rc != LDAP_SUCCESS ) {
256                                 BER_BVZERO( &nvals[i + 1] );
257                                 goto error_return;
258                         }
259                 }
260                 BER_BVZERO( &nvals[i] );
261         }
262
263         rc = attr_merge( e, desc, vals, nvals );
264
265 error_return:;
266         if ( nvals != NULL ) {
267                 ber_bvarray_free_x( nvals, memctx );
268         }
269         return rc;
270 }
271
272 int
273 attr_merge_one(
274         Entry           *e,
275         AttributeDescription *desc,
276         struct berval   *val,
277         struct berval   *nval )
278 {
279         int rc;
280         Attribute       **a;
281
282         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
283                 if ( (*a)->a_desc == desc ) {
284                         break;
285                 }
286         }
287
288         if ( *a == NULL ) {
289                 *a = attr_alloc( desc );
290         }
291
292         rc = value_add_one( &(*a)->a_vals, val );
293
294         if ( rc == LDAP_SUCCESS ) {
295                 if ( nval ) {
296                         rc = value_add_one( &(*a)->a_nvals, nval );
297                         /* FIXME: what if rc != LDAP_SUCCESS ? */
298                 } else {
299                         (*a)->a_nvals = (*a)->a_vals;
300                 }
301         }
302         return rc;
303 }
304
305 int
306 attr_merge_normalize_one(
307         Entry           *e,
308         AttributeDescription *desc,
309         struct berval   *val,
310         void            *memctx )
311 {
312         struct berval   nval;
313         struct berval   *nvalp = NULL;
314         int             rc;
315
316         if ( desc->ad_type->sat_equality &&
317                 desc->ad_type->sat_equality->smr_normalize )
318         {
319                 rc = (*desc->ad_type->sat_equality->smr_normalize)(
320                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
321                                 desc->ad_type->sat_syntax,
322                                 desc->ad_type->sat_equality,
323                                 val, &nval, memctx );
324
325                 if ( rc != LDAP_SUCCESS ) {
326                         return rc;
327                 }
328                 nvalp = &nval;
329         }
330
331         rc = attr_merge_one( e, desc, val, nvalp );
332         if ( nvalp != NULL ) {
333                 slap_sl_free( nval.bv_val, memctx );
334         }
335         return rc;
336 }
337
338 /*
339  * attrs_find - find attribute(s) by AttributeDescription
340  * returns next attribute which is subtype of provided description.
341  */
342
343 Attribute *
344 attrs_find(
345     Attribute   *a,
346         AttributeDescription *desc )
347 {
348         for ( ; a != NULL; a = a->a_next ) {
349                 if ( is_ad_subtype( a->a_desc, desc ) ) {
350                         return( a );
351                 }
352         }
353
354         return( NULL );
355 }
356
357 /*
358  * attr_find - find attribute by type
359  */
360
361 Attribute *
362 attr_find(
363     Attribute   *a,
364         AttributeDescription *desc )
365 {
366         for ( ; a != NULL; a = a->a_next ) {
367                 if ( a->a_desc == desc ) {
368                         return( a );
369                 }
370         }
371
372         return( NULL );
373 }
374
375 /*
376  * attr_delete - delete the attribute type in list pointed to by attrs
377  * return       0       deleted ok
378  *              1       not found in list a
379  *              -1      something bad happened
380  */
381
382 int
383 attr_delete(
384     Attribute   **attrs,
385         AttributeDescription *desc )
386 {
387         Attribute       **a;
388
389         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
390                 if ( (*a)->a_desc == desc ) {
391                         Attribute       *save = *a;
392                         *a = (*a)->a_next;
393                         attr_free( save );
394
395                         return LDAP_SUCCESS;
396                 }
397         }
398
399         return LDAP_NO_SUCH_ATTRIBUTE;
400 }
401