]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Change slap_sasl_authorized to take an Operation instead of a Connection,
[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 && 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         if ( nvals != NULL ) {
190                 ber_bvarray_free( nvals );
191         }
192         return rc;
193 }
194
195 int
196 attr_merge_one(
197         Entry           *e,
198         AttributeDescription *desc,
199         struct berval   *val,
200         struct berval   *nval
201 ) {
202         int rc;
203         Attribute       **a;
204
205         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
206                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
207                         break;
208                 }
209         }
210
211         if ( *a == NULL ) {
212                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
213                 (*a)->a_desc = desc;
214                 (*a)->a_vals = NULL;
215                 (*a)->a_nvals = NULL;
216                 (*a)->a_next = NULL;
217                 (*a)->a_flags = 0;
218         }
219
220         rc = value_add_one( &(*a)->a_vals, val );
221
222         if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
223         else (*a)->a_nvals = (*a)->a_vals;
224         return rc;
225 }
226
227 int
228 attr_merge_normalize_one(
229         Entry           *e,
230         AttributeDescription *desc,
231         struct berval   *val,
232         void            *memctx
233 ) {
234         struct berval   nval;
235         struct berval   *nvalp;
236         int             rc;
237
238         if ( desc->ad_type->sat_equality && desc->ad_type->sat_equality->smr_normalize ) {
239                 rc = (*desc->ad_type->sat_equality->smr_normalize)(
240                                 0,
241                                 desc->ad_type->sat_syntax,
242                                 desc->ad_type->sat_equality,
243                                 val, &nval, memctx );
244
245                 if ( rc != LDAP_SUCCESS ) {
246                         return rc;
247                 }
248                 nvalp = &nval;
249         } else {
250                 nvalp = NULL;
251         }
252
253         rc = attr_merge_one( e, desc, val, nvalp );
254         if ( nvalp != NULL ) {
255                 ch_free( nval.bv_val );
256         }
257         return rc;
258 }
259
260 /*
261  * attrs_find - find attribute(s) by AttributeDescription
262  * returns next attribute which is subtype of provided description.
263  */
264
265 Attribute *
266 attrs_find(
267     Attribute   *a,
268         AttributeDescription *desc
269 )
270 {
271         for ( ; a != NULL; a = a->a_next ) {
272                 if ( is_ad_subtype( a->a_desc, desc ) ) {
273                         return( a );
274                 }
275         }
276
277         return( NULL );
278 }
279
280 /*
281  * attr_find - find attribute by type
282  */
283
284 Attribute *
285 attr_find(
286     Attribute   *a,
287         AttributeDescription *desc
288 )
289 {
290         for ( ; a != NULL; a = a->a_next ) {
291                 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
292                         return( a );
293                 }
294         }
295
296         return( NULL );
297 }
298
299 /*
300  * attr_delete - delete the attribute type in list pointed to by attrs
301  * return       0       deleted ok
302  *              1       not found in list a
303  *              -1      something bad happened
304  */
305
306 int
307 attr_delete(
308     Attribute   **attrs,
309         AttributeDescription *desc
310 )
311 {
312         Attribute       **a;
313
314         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
315                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
316                         Attribute       *save = *a;
317                         *a = (*a)->a_next;
318                         attr_free( save );
319
320                         return LDAP_SUCCESS;
321                 }
322         }
323
324         return LDAP_NO_SUCH_ATTRIBUTE;
325 }
326