]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Moved IDL cache code to standalone functions.
[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  * For SLAP_NVALUES: nvals must be NULL if the attribute has no
115  * normalizer. 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->smr_normalize ) {
166                 int     i;
167                 
168                 for ( i = 0; vals[i].bv_val; i++);
169
170                 nvals = ch_calloc( sizeof(struct berval), i + 1 );
171                 for ( i = 0; vals[i].bv_val; i++ ) {
172                         rc = (*desc->ad_type->sat_equality->smr_normalize)(
173                                         0,
174                                         desc->ad_type->sat_syntax,
175                                         desc->ad_type->sat_equality,
176                                         &vals[i], &nvals[i], memctx );
177
178                         if ( rc != LDAP_SUCCESS ) {
179                                 nvals[i+1].bv_val = NULL;
180                                 goto error_return;
181                         }
182                 }
183                 nvals[i].bv_val = NULL;
184         }
185
186         rc = attr_merge( e, desc, vals, nvals );
187
188 error_return:;
189         ber_bvarray_free( nvals );
190         return rc;
191 }
192
193 int
194 attr_merge_one(
195         Entry           *e,
196         AttributeDescription *desc,
197         struct berval   *val,
198         struct berval   *nval
199 ) {
200         int rc;
201         Attribute       **a;
202
203         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
204                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
205                         break;
206                 }
207         }
208
209         if ( *a == NULL ) {
210                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
211                 (*a)->a_desc = desc;
212                 (*a)->a_vals = NULL;
213                 (*a)->a_nvals = NULL;
214                 (*a)->a_next = NULL;
215                 (*a)->a_flags = 0;
216         }
217
218         rc = value_add_one( &(*a)->a_vals, val );
219
220         if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
221         else (*a)->a_nvals = (*a)->a_vals;
222         return rc;
223 }
224
225 int
226 attr_merge_normalize_one(
227         Entry           *e,
228         AttributeDescription *desc,
229         struct berval   *val,
230         void            *memctx
231 ) {
232         struct berval   nval;
233         int             rc;
234
235         if ( desc->ad_type->sat_equality->smr_normalize ) {
236                 rc = (*desc->ad_type->sat_equality->smr_normalize)(
237                                 0,
238                                 desc->ad_type->sat_syntax,
239                                 desc->ad_type->sat_equality,
240                                 val, &nval, memctx );
241
242                 if ( rc != LDAP_SUCCESS ) {
243                         return rc;
244                 }
245         }
246
247         rc = attr_merge_one( e, desc, val, &nval );
248         ch_free( nval.bv_val );
249         return rc;
250 }
251
252 /*
253  * attrs_find - find attribute(s) by AttributeDescription
254  * returns next attribute which is subtype of provided description.
255  */
256
257 Attribute *
258 attrs_find(
259     Attribute   *a,
260         AttributeDescription *desc
261 )
262 {
263         for ( ; a != NULL; a = a->a_next ) {
264                 if ( is_ad_subtype( a->a_desc, desc ) ) {
265                         return( a );
266                 }
267         }
268
269         return( NULL );
270 }
271
272 /*
273  * attr_find - find attribute by type
274  */
275
276 Attribute *
277 attr_find(
278     Attribute   *a,
279         AttributeDescription *desc
280 )
281 {
282         for ( ; a != NULL; a = a->a_next ) {
283                 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
284                         return( a );
285                 }
286         }
287
288         return( NULL );
289 }
290
291 /*
292  * attr_delete - delete the attribute type in list pointed to by attrs
293  * return       0       deleted ok
294  *              1       not found in list a
295  *              -1      something bad happened
296  */
297
298 int
299 attr_delete(
300     Attribute   **attrs,
301         AttributeDescription *desc
302 )
303 {
304         Attribute       **a;
305
306         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
307                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
308                         Attribute       *save = *a;
309                         *a = (*a)->a_next;
310                         attr_free( save );
311
312                         return LDAP_SUCCESS;
313                 }
314         }
315
316         return LDAP_NO_SUCH_ATTRIBUTE;
317 }
318