]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
82c94abaa01b7674056c51f31c27105b0740adcc
[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-2006 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 /*
44  * Allocate in chunks, minimum of 1000 at a time.
45  */
46 #define CHUNK_SIZE      1000
47 typedef struct slap_list {
48         struct slap_list *next;
49 } slap_list;
50 static slap_list *attr_chunks;
51 static Attribute *attr_list;
52 static ldap_pvt_thread_mutex_t attr_mutex;
53
54 int
55 attr_prealloc( int num )
56 {
57         Attribute *a;
58         slap_list *s;
59
60         if (!num) return 0;
61
62         s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Attribute));
63         s->next = attr_chunks;
64         attr_chunks = s;
65
66         a = (Attribute *)(s+1);
67         for ( ;num>1; num--) {
68                 a->a_next = a+1;
69                 a++;
70         }
71         a->a_next = attr_list;
72         attr_list = (Attribute *)(s+1);
73
74         return 0;
75 }
76
77 Attribute *
78 attr_alloc( AttributeDescription *ad )
79 {
80         Attribute *a;
81
82         ldap_pvt_thread_mutex_lock( &attr_mutex );
83         if ( !attr_list )
84                 attr_prealloc( CHUNK_SIZE );
85         a = attr_list;
86         attr_list = a->a_next;
87         a->a_next = NULL;
88         ldap_pvt_thread_mutex_unlock( &attr_mutex );
89         
90         a->a_desc = ad;
91
92         return a;
93 }
94
95 /* Return a list of num attrs */
96 Attribute *
97 attrs_alloc( int num )
98 {
99         Attribute *head = NULL;
100         Attribute **a;
101
102         ldap_pvt_thread_mutex_lock( &attr_mutex );
103         for ( a = &attr_list; *a && num > 0; a = &(*a)->a_next ) {
104                 if ( !head )
105                         head = *a;
106                 num--;
107         }
108         attr_list = *a;
109         if ( num > 0 ) {
110                 attr_prealloc( num > CHUNK_SIZE ? num : CHUNK_SIZE );
111                 *a = attr_list;
112                 for ( ; *a && num > 0; a = &(*a)->a_next ) {
113                         if ( !head )
114                                 head = *a;
115                         num--;
116                 }
117                 attr_list = *a;
118         }
119         *a = NULL;
120         ldap_pvt_thread_mutex_unlock( &attr_mutex );
121
122         return head;
123 }
124
125
126 void
127 attr_free( Attribute *a )
128 {
129         if ( a->a_nvals && a->a_nvals != a->a_vals &&
130                 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
131                 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
132                         free( a->a_nvals );
133                 } else {
134                         ber_bvarray_free( a->a_nvals );
135                 }
136         }
137         /* a_vals may be equal to slap_dummy_bv, a static empty berval;
138          * this is used as a placeholder for attributes that do not carry
139          * values, e.g. when proxying search entries with the "attrsonly"
140          * bit set. */
141         if ( a->a_vals != &slap_dummy_bv &&
142                 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
143                 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
144                         free( a->a_vals );
145                 } else {
146                         ber_bvarray_free( a->a_vals );
147                 }
148         }
149         memset( a, 0, sizeof( Attribute ));
150         ldap_pvt_thread_mutex_lock( &attr_mutex );
151         a->a_next = attr_list;
152         attr_list = a;
153         ldap_pvt_thread_mutex_unlock( &attr_mutex );
154 }
155
156 #ifdef LDAP_COMP_MATCH
157 void
158 comp_tree_free( Attribute *a )
159 {
160         Attribute *next;
161
162         for( ; a != NULL ; a = next ) {
163                 next = a->a_next;
164                 if ( component_destructor && a->a_comp_data ) {
165                         if ( a->a_comp_data->cd_mem_op )
166                                 component_destructor( a->a_comp_data->cd_mem_op );
167                         free ( a->a_comp_data );
168                 }
169         }
170 }
171 #endif
172
173 void
174 attrs_free( Attribute *a )
175 {
176         Attribute *next;
177
178         for( ; a != NULL ; a = next ) {
179                 next = a->a_next;
180                 attr_free( a );
181         }
182 }
183
184
185 static void
186 attr_dup2( Attribute *tmp, Attribute *a )
187 {
188         if ( a->a_vals != NULL ) {
189                 int     i;
190
191                 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
192                         /* EMPTY */ ;
193                 }
194
195                 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
196                 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
197                         ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
198                         if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
199                         /* FIXME: error? */
200                 }
201                 BER_BVZERO( &tmp->a_vals[i] );
202
203                 /* a_nvals must be non null; it may be equal to a_vals */
204                 assert( a->a_nvals != NULL );
205
206                 if ( a->a_nvals != a->a_vals ) {
207                         int     j;
208
209                         tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
210                         for ( j = 0; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
211                                 assert( j < i );
212                                 ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
213                                 if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
214                                 /* FIXME: error? */
215                         }
216                         assert( j == i );
217                         BER_BVZERO( &tmp->a_nvals[j] );
218
219                 } else {
220                         tmp->a_nvals = tmp->a_vals;
221                 }
222         }
223 }
224
225 Attribute *
226 attr_dup( Attribute *a )
227 {
228         Attribute *tmp;
229
230         if ( a == NULL) return NULL;
231
232         tmp = attr_alloc( a->a_desc );
233         attr_dup2( tmp, a );
234         return tmp;
235 }
236
237 Attribute *
238 attrs_dup( Attribute *a )
239 {
240         int i;
241         Attribute *tmp, *anew;
242
243         if( a == NULL ) return NULL;
244
245         /* count them */
246         for( tmp=a,i=0; tmp; tmp=tmp->a_next ) {
247                 i++;
248         }
249
250         anew = attrs_alloc( i );
251
252         for( tmp=anew; a; a=a->a_next ) {
253                 attr_dup2( tmp, a );
254                 tmp=tmp->a_next;
255         }
256
257         return anew;
258 }
259
260
261 /*
262  * attr_merge - merge the given type and value with the list of
263  * attributes in attrs.
264  *
265  * nvals must be NULL if the attribute has no normalizer.
266  * In this case, a->a_nvals will be set equal to a->a_vals.
267  *
268  * returns      0       everything went ok
269  *              -1      trouble
270  */
271
272 int
273 attr_merge(
274         Entry           *e,
275         AttributeDescription *desc,
276         BerVarray       vals,
277         BerVarray       nvals )
278 {
279         int rc;
280
281         Attribute       **a;
282
283         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
284                 if (  (*a)->a_desc == desc ) {
285                         break;
286                 }
287         }
288
289         if ( *a == NULL ) {
290                 *a = attr_alloc( desc );
291         } else {
292                 /*
293                  * FIXME: if the attribute already exists, the presence
294                  * of nvals and the value of (*a)->a_nvals must be consistent
295                  */
296                 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
297                                 || ( nvals != NULL && (
298                                         ( (*a)->a_vals == NULL && (*a)->a_nvals == NULL )
299                                         || ( (*a)->a_nvals != (*a)->a_vals ) ) ) );
300         }
301
302         rc = value_add( &(*a)->a_vals, vals );
303
304         if ( rc == LDAP_SUCCESS ) {
305                 if ( nvals ) {
306                         rc = value_add( &(*a)->a_nvals, nvals );
307                         /* FIXME: what if rc != LDAP_SUCCESS ? */
308                 } else {
309                         (*a)->a_nvals = (*a)->a_vals;
310                 }
311         }
312
313         return rc;
314 }
315
316 /*
317  * if a normalization function is defined for the equality matchingRule
318  * of desc, the value is normalized and stored in nval; otherwise nval 
319  * is NULL
320  */
321 int
322 attr_normalize(
323         AttributeDescription    *desc,
324         BerVarray               vals,
325         BerVarray               *nvalsp,
326         void                    *memctx )
327 {
328         int             rc = LDAP_SUCCESS;
329         BerVarray       nvals = NULL;
330
331         *nvalsp = NULL;
332
333         if ( desc->ad_type->sat_equality &&
334                 desc->ad_type->sat_equality->smr_normalize )
335         {
336                 int     i;
337                 
338                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
339
340                 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
341                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
342                         rc = desc->ad_type->sat_equality->smr_normalize(
343                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
344                                         desc->ad_type->sat_syntax,
345                                         desc->ad_type->sat_equality,
346                                         &vals[i], &nvals[i], memctx );
347
348                         if ( rc != LDAP_SUCCESS ) {
349                                 BER_BVZERO( &nvals[i + 1] );
350                                 break;
351                         }
352                 }
353                 BER_BVZERO( &nvals[i] );
354                 *nvalsp = nvals;
355         }
356
357 error_return:;
358         if ( rc != LDAP_SUCCESS && nvals != NULL ) {
359                 ber_bvarray_free_x( nvals, memctx );
360         }
361
362         return rc;
363 }
364
365 int
366 attr_merge_normalize(
367         Entry                   *e,
368         AttributeDescription    *desc,
369         BerVarray               vals,
370         void                    *memctx )
371 {
372         BerVarray       nvals = NULL;
373         int             rc;
374
375         rc = attr_normalize( desc, vals, &nvals, memctx );
376         if ( rc == LDAP_SUCCESS ) {
377                 rc = attr_merge( e, desc, vals, nvals );
378                 if ( nvals != NULL ) {
379                         ber_bvarray_free_x( nvals, memctx );
380                 }
381         }
382
383         return rc;
384 }
385
386 int
387 attr_merge_one(
388         Entry           *e,
389         AttributeDescription *desc,
390         struct berval   *val,
391         struct berval   *nval )
392 {
393         int rc;
394         Attribute       **a;
395
396         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
397                 if ( (*a)->a_desc == desc ) {
398                         break;
399                 }
400         }
401
402         if ( *a == NULL ) {
403                 *a = attr_alloc( desc );
404         }
405
406         rc = value_add_one( &(*a)->a_vals, val );
407
408         if ( rc == LDAP_SUCCESS ) {
409                 if ( nval ) {
410                         rc = value_add_one( &(*a)->a_nvals, nval );
411                         /* FIXME: what if rc != LDAP_SUCCESS ? */
412                 } else {
413                         (*a)->a_nvals = (*a)->a_vals;
414                 }
415         }
416         return rc;
417 }
418
419 /*
420  * if a normalization function is defined for the equality matchingRule
421  * of desc, the value is normalized and stored in nval; otherwise nval 
422  * is NULL
423  */
424 int
425 attr_normalize_one(
426         AttributeDescription *desc,
427         struct berval   *val,
428         struct berval   *nval,
429         void            *memctx )
430 {
431         int             rc = LDAP_SUCCESS;
432
433         BER_BVZERO( nval );
434
435         if ( desc->ad_type->sat_equality &&
436                 desc->ad_type->sat_equality->smr_normalize )
437         {
438                 rc = desc->ad_type->sat_equality->smr_normalize(
439                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
440                                 desc->ad_type->sat_syntax,
441                                 desc->ad_type->sat_equality,
442                                 val, nval, memctx );
443
444                 if ( rc != LDAP_SUCCESS ) {
445                         return rc;
446                 }
447         }
448
449         return rc;
450 }
451
452 int
453 attr_merge_normalize_one(
454         Entry           *e,
455         AttributeDescription *desc,
456         struct berval   *val,
457         void            *memctx )
458 {
459         struct berval   nval = BER_BVNULL;
460         struct berval   *nvalp = NULL;
461         int             rc;
462
463         rc = attr_normalize_one( desc, val, &nval, memctx );
464         if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &nval ) ) {
465                 nvalp = &nval;
466         }
467
468         rc = attr_merge_one( e, desc, val, nvalp );
469         if ( nvalp != NULL ) {
470                 slap_sl_free( nval.bv_val, memctx );
471         }
472         return rc;
473 }
474
475 /*
476  * attrs_find - find attribute(s) by AttributeDescription
477  * returns next attribute which is subtype of provided description.
478  */
479
480 Attribute *
481 attrs_find(
482     Attribute   *a,
483         AttributeDescription *desc )
484 {
485         for ( ; a != NULL; a = a->a_next ) {
486                 if ( is_ad_subtype( a->a_desc, desc ) ) {
487                         return( a );
488                 }
489         }
490
491         return( NULL );
492 }
493
494 /*
495  * attr_find - find attribute by type
496  */
497
498 Attribute *
499 attr_find(
500     Attribute   *a,
501         AttributeDescription *desc )
502 {
503         for ( ; a != NULL; a = a->a_next ) {
504                 if ( a->a_desc == desc ) {
505                         return( a );
506                 }
507         }
508
509         return( NULL );
510 }
511
512 /*
513  * attr_delete - delete the attribute type in list pointed to by attrs
514  * return       0       deleted ok
515  *              1       not found in list a
516  *              -1      something bad happened
517  */
518
519 int
520 attr_delete(
521     Attribute   **attrs,
522         AttributeDescription *desc )
523 {
524         Attribute       **a;
525
526         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
527                 if ( (*a)->a_desc == desc ) {
528                         Attribute       *save = *a;
529                         *a = (*a)->a_next;
530                         attr_free( save );
531
532                         return LDAP_SUCCESS;
533                 }
534         }
535
536         return LDAP_NO_SUCH_ATTRIBUTE;
537 }
538
539 int
540 attr_init( void )
541 {
542         ldap_pvt_thread_mutex_init( &attr_mutex );
543         return 0;
544 }
545
546 int
547 attr_destroy( void )
548 {
549         slap_list *a;
550
551         for ( a=attr_chunks; a; a=attr_chunks ) {
552                 attr_chunks = a->next;
553                 free( a );
554         }
555         ldap_pvt_thread_mutex_destroy( &attr_mutex );
556         return 0;
557 }