]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
70600c4eb942fd4a362c160728fba4948ed5578c
[openldap] / servers / slapd / attr.c
1 /* attr.c - routines for dealing with attributes */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #endif
34
35 #include <ac/ctype.h>
36 #include <ac/errno.h>
37 #include <ac/socket.h>
38 #include <ac/string.h>
39 #include <ac/time.h>
40
41 #include "slap.h"
42
43 void
44 attr_free( Attribute *a )
45 {
46         if ( a->a_nvals && a->a_nvals != a->a_vals )
47                 ber_bvarray_free( a->a_nvals );
48         ber_bvarray_free( a->a_vals );
49         free( a );
50 }
51
52 void
53 attrs_free( Attribute *a )
54 {
55         Attribute *next;
56
57         for( ; a != NULL ; a = next ) {
58                 next = a->a_next;
59                 attr_free( a );
60         }
61 }
62
63 Attribute *
64 attr_dup( Attribute *a )
65 {
66         Attribute *tmp;
67
68         if ( a == NULL) return NULL;
69
70         tmp = ch_malloc( sizeof(Attribute) );
71
72         if ( a->a_vals != NULL ) {
73                 int i;
74
75                 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
76                         /* EMPTY */ ;
77                 }
78
79                 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
80                 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
81                         ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
82                         if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
83                         /* FIXME: error? */
84                 }
85                 BER_BVZERO( &tmp->a_vals[i] );
86
87                 /* a_nvals must be non null; it may be equal to a_vals */
88                 assert( a->a_nvals );
89
90                 if ( a->a_nvals != a->a_vals ) {
91                         tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
92                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
93                                 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
94                                 if ( BER_BVISNULL( &tmp->a_nvals[i] ) ) break;
95                                 /* FIXME: error? */
96                         }
97                         BER_BVZERO( &tmp->a_nvals[i] );
98
99                 } else {
100                         tmp->a_nvals = tmp->a_vals;
101                 }
102
103         } else {
104                 tmp->a_vals = NULL;
105                 tmp->a_nvals = NULL;
106         }
107
108         tmp->a_desc = a->a_desc;
109         tmp->a_next = NULL;
110         tmp->a_flags = 0;
111
112         return tmp;
113 }
114
115 Attribute *
116 attrs_dup( Attribute *a )
117 {
118         Attribute *tmp, **next;
119
120         if( a == NULL ) return NULL;
121
122         tmp = NULL;
123         next = &tmp;
124
125         for( ; a != NULL ; a = a->a_next ) {
126                 *next = attr_dup( a );
127                 next = &((*next)->a_next);
128         }
129         *next = NULL;
130
131         return tmp;
132 }
133
134
135
136 /*
137  * attr_merge - merge the given type and value with the list of
138  * attributes in attrs.
139  *
140  * nvals must be NULL if the attribute has no normalizer.
141  * In this case, a->a_nvals will be set equal to a->a_vals.
142  *
143  * returns      0       everything went ok
144  *              -1      trouble
145  */
146
147 int
148 attr_merge(
149         Entry           *e,
150         AttributeDescription *desc,
151         BerVarray       vals,
152         BerVarray       nvals )
153 {
154         int rc;
155
156         Attribute       **a;
157
158         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
159                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
160                         break;
161                 }
162         }
163
164         if ( *a == NULL ) {
165                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
166                 (*a)->a_desc = desc;
167                 (*a)->a_vals = NULL;
168                 (*a)->a_nvals = NULL;
169                 (*a)->a_next = NULL;
170                 (*a)->a_flags = 0;
171         }
172
173         rc = value_add( &(*a)->a_vals, vals );
174
175         if ( rc == LDAP_SUCCESS ) {
176                 if ( nvals ) {
177                         rc = value_add( &(*a)->a_nvals, nvals );
178                         /* FIXME: what if rc != LDAP_SUCCESS ? */
179                 } else {
180                         (*a)->a_nvals = (*a)->a_vals;
181                 }
182         }
183
184         return rc;
185 }
186
187 int
188 attr_merge_normalize(
189         Entry           *e,
190         AttributeDescription *desc,
191         BerVarray       vals,
192         void     *memctx )
193 {
194         BerVarray       nvals = NULL;
195         int             rc;
196
197         if ( desc->ad_type->sat_equality &&
198                 desc->ad_type->sat_equality->smr_normalize )
199         {
200                 int     i;
201                 
202                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
203
204                 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
205                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
206                         rc = (*desc->ad_type->sat_equality->smr_normalize)(
207                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
208                                         desc->ad_type->sat_syntax,
209                                         desc->ad_type->sat_equality,
210                                         &vals[i], &nvals[i], memctx );
211
212                         if ( rc != LDAP_SUCCESS ) {
213                                 BER_BVZERO( &nvals[i + 1] );
214                                 goto error_return;
215                         }
216                 }
217                 BER_BVZERO( &nvals[i] );
218         }
219
220         rc = attr_merge( e, desc, vals, nvals );
221
222 error_return:;
223         if ( nvals != NULL ) {
224                 ber_bvarray_free_x( nvals, memctx );
225         }
226         return rc;
227 }
228
229 int
230 attr_merge_one(
231         Entry           *e,
232         AttributeDescription *desc,
233         struct berval   *val,
234         struct berval   *nval )
235 {
236         int rc;
237         Attribute       **a;
238
239         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
240                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
241                         break;
242                 }
243         }
244
245         if ( *a == NULL ) {
246                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
247                 (*a)->a_desc = desc;
248                 (*a)->a_vals = NULL;
249                 (*a)->a_nvals = NULL;
250                 (*a)->a_next = NULL;
251                 (*a)->a_flags = 0;
252         }
253
254         rc = value_add_one( &(*a)->a_vals, val );
255
256         if ( rc == LDAP_SUCCESS ) {
257                 if ( nval ) {
258                         rc = value_add_one( &(*a)->a_nvals, nval );
259                         /* FIXME: what if rc != LDAP_SUCCESS ? */
260                 } else {
261                         (*a)->a_nvals = (*a)->a_vals;
262                 }
263         }
264         return rc;
265 }
266
267 int
268 attr_merge_normalize_one(
269         Entry           *e,
270         AttributeDescription *desc,
271         struct berval   *val,
272         void            *memctx )
273 {
274         struct berval   nval;
275         struct berval   *nvalp = NULL;
276         int             rc;
277
278         if ( desc->ad_type->sat_equality &&
279                 desc->ad_type->sat_equality->smr_normalize )
280         {
281                 rc = (*desc->ad_type->sat_equality->smr_normalize)(
282                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
283                                 desc->ad_type->sat_syntax,
284                                 desc->ad_type->sat_equality,
285                                 val, &nval, memctx );
286
287                 if ( rc != LDAP_SUCCESS ) {
288                         return rc;
289                 }
290                 nvalp = &nval;
291         }
292
293         rc = attr_merge_one( e, desc, val, nvalp );
294         if ( nvalp != NULL ) {
295                 slap_sl_free( nval.bv_val, memctx );
296         }
297         return rc;
298 }
299
300 /*
301  * attrs_find - find attribute(s) by AttributeDescription
302  * returns next attribute which is subtype of provided description.
303  */
304
305 Attribute *
306 attrs_find(
307     Attribute   *a,
308         AttributeDescription *desc )
309 {
310         for ( ; a != NULL; a = a->a_next ) {
311                 if ( is_ad_subtype( a->a_desc, desc ) ) {
312                         return( a );
313                 }
314         }
315
316         return( NULL );
317 }
318
319 /*
320  * attr_find - find attribute by type
321  */
322
323 Attribute *
324 attr_find(
325     Attribute   *a,
326         AttributeDescription *desc )
327 {
328         for ( ; a != NULL; a = a->a_next ) {
329                 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
330                         return( a );
331                 }
332         }
333
334         return( NULL );
335 }
336
337 /*
338  * attr_delete - delete the attribute type in list pointed to by attrs
339  * return       0       deleted ok
340  *              1       not found in list a
341  *              -1      something bad happened
342  */
343
344 int
345 attr_delete(
346     Attribute   **attrs,
347         AttributeDescription *desc )
348 {
349         Attribute       **a;
350
351         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
352                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
353                         Attribute       *save = *a;
354                         *a = (*a)->a_next;
355                         attr_free( save );
356
357                         return LDAP_SUCCESS;
358                 }
359         }
360
361         return LDAP_NO_SUCH_ATTRIBUTE;
362 }
363