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