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