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