]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
8a1121ce034241998480900e8cd92b5428482e3c
[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         struct berval   *nvalp;
234         int             rc;
235
236         if ( desc->ad_type->sat_equality && desc->ad_type->sat_equality->smr_normalize ) {
237                 rc = (*desc->ad_type->sat_equality->smr_normalize)(
238                                 0,
239                                 desc->ad_type->sat_syntax,
240                                 desc->ad_type->sat_equality,
241                                 val, &nval, memctx );
242
243                 if ( rc != LDAP_SUCCESS ) {
244                         return rc;
245                 }
246                 nvalp = &nval;
247         } else {
248                 nvalp = NULL;
249         }
250
251         rc = attr_merge_one( e, desc, val, nvalp );
252         if ( nvalp != NULL ) {
253                 ch_free( nval.bv_val );
254         }
255         return rc;
256 }
257
258 /*
259  * attrs_find - find attribute(s) by AttributeDescription
260  * returns next attribute which is subtype of provided description.
261  */
262
263 Attribute *
264 attrs_find(
265     Attribute   *a,
266         AttributeDescription *desc
267 )
268 {
269         for ( ; a != NULL; a = a->a_next ) {
270                 if ( is_ad_subtype( a->a_desc, desc ) ) {
271                         return( a );
272                 }
273         }
274
275         return( NULL );
276 }
277
278 /*
279  * attr_find - find attribute by type
280  */
281
282 Attribute *
283 attr_find(
284     Attribute   *a,
285         AttributeDescription *desc
286 )
287 {
288         for ( ; a != NULL; a = a->a_next ) {
289                 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
290                         return( a );
291                 }
292         }
293
294         return( NULL );
295 }
296
297 /*
298  * attr_delete - delete the attribute type in list pointed to by attrs
299  * return       0       deleted ok
300  *              1       not found in list a
301  *              -1      something bad happened
302  */
303
304 int
305 attr_delete(
306     Attribute   **attrs,
307         AttributeDescription *desc
308 )
309 {
310         Attribute       **a;
311
312         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
313                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
314                         Attribute       *save = *a;
315                         *a = (*a)->a_next;
316                         attr_free( save );
317
318                         return LDAP_SUCCESS;
319                 }
320         }
321
322         return LDAP_NO_SUCH_ATTRIBUTE;
323 }
324