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