]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Unify use of BDB lockers
[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-2007 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_clean( 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         a->a_desc = NULL;
150         a->a_vals = NULL;
151         a->a_nvals = NULL;
152 #ifdef LDAP_COMP_MATCH
153         a->a_comp_data = NULL;
154 #endif
155         a->a_flags = 0;
156 }
157
158 void
159 attr_free( Attribute *a )
160 {
161         attr_clean( a );
162         ldap_pvt_thread_mutex_lock( &attr_mutex );
163         a->a_next = attr_list;
164         attr_list = a;
165         ldap_pvt_thread_mutex_unlock( &attr_mutex );
166 }
167
168 #ifdef LDAP_COMP_MATCH
169 void
170 comp_tree_free( Attribute *a )
171 {
172         Attribute *next;
173
174         for( ; a != NULL ; a = next ) {
175                 next = a->a_next;
176                 if ( component_destructor && a->a_comp_data ) {
177                         if ( a->a_comp_data->cd_mem_op )
178                                 component_destructor( a->a_comp_data->cd_mem_op );
179                         free ( a->a_comp_data );
180                 }
181         }
182 }
183 #endif
184
185 void
186 attrs_free( Attribute *a )
187 {
188         Attribute *b, *tail, *next;
189
190         if ( a ) {
191                 tail = a;
192                 do {
193                         next = a->a_next;
194                         attr_clean( a );
195                         a->a_next = b;
196                         b = a;
197                         a = next;
198                 } while ( next );
199
200                 ldap_pvt_thread_mutex_lock( &attr_mutex );
201                 tail->a_next = attr_list;
202                 attr_list = b;
203                 ldap_pvt_thread_mutex_unlock( &attr_mutex );
204         }
205 }
206
207 static void
208 attr_dup2( Attribute *tmp, Attribute *a )
209 {
210         if ( a->a_vals != NULL ) {
211                 int     i;
212
213                 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
214                         /* EMPTY */ ;
215                 }
216
217                 tmp->a_vals = ch_malloc( (i + 1) * sizeof(struct berval) );
218                 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
219                         ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
220                         if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
221                         /* FIXME: error? */
222                 }
223                 BER_BVZERO( &tmp->a_vals[i] );
224
225                 /* a_nvals must be non null; it may be equal to a_vals */
226                 assert( a->a_nvals != NULL );
227
228                 if ( a->a_nvals != a->a_vals ) {
229                         int     j;
230
231                         tmp->a_nvals = ch_malloc( (i + 1) * sizeof(struct berval) );
232                         for ( j = 0; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
233                                 assert( j < i );
234                                 ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
235                                 if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
236                                 /* FIXME: error? */
237                         }
238                         assert( j == i );
239                         BER_BVZERO( &tmp->a_nvals[j] );
240
241                 } else {
242                         tmp->a_nvals = tmp->a_vals;
243                 }
244         }
245 }
246
247 Attribute *
248 attr_dup( Attribute *a )
249 {
250         Attribute *tmp;
251
252         if ( a == NULL) return NULL;
253
254         tmp = attr_alloc( a->a_desc );
255         attr_dup2( tmp, a );
256         return tmp;
257 }
258
259 Attribute *
260 attrs_dup( Attribute *a )
261 {
262         int i;
263         Attribute *tmp, *anew;
264
265         if( a == NULL ) return NULL;
266
267         /* count them */
268         for( tmp=a,i=0; tmp; tmp=tmp->a_next ) {
269                 i++;
270         }
271
272         anew = attrs_alloc( i );
273
274         for( tmp=anew; a; a=a->a_next ) {
275                 tmp->a_desc = a->a_desc;
276                 attr_dup2( tmp, a );
277                 tmp=tmp->a_next;
278         }
279
280         return anew;
281 }
282
283
284 /*
285  * attr_merge - merge the given type and value with the list of
286  * attributes in attrs.
287  *
288  * nvals must be NULL if the attribute has no normalizer.
289  * In this case, a->a_nvals will be set equal to a->a_vals.
290  *
291  * returns      0       everything went ok
292  *              -1      trouble
293  */
294
295 int
296 attr_merge(
297         Entry           *e,
298         AttributeDescription *desc,
299         BerVarray       vals,
300         BerVarray       nvals )
301 {
302         int rc;
303
304         Attribute       **a;
305
306         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
307                 if (  (*a)->a_desc == desc ) {
308                         break;
309                 }
310         }
311
312         if ( *a == NULL ) {
313                 *a = attr_alloc( desc );
314         } else {
315                 /*
316                  * FIXME: if the attribute already exists, the presence
317                  * of nvals and the value of (*a)->a_nvals must be consistent
318                  */
319                 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
320                                 || ( nvals != NULL && (
321                                         ( (*a)->a_vals == NULL && (*a)->a_nvals == NULL )
322                                         || ( (*a)->a_nvals != (*a)->a_vals ) ) ) );
323         }
324
325         rc = value_add( &(*a)->a_vals, vals );
326
327         if ( rc == LDAP_SUCCESS ) {
328                 if ( nvals ) {
329                         rc = value_add( &(*a)->a_nvals, nvals );
330                         /* FIXME: what if rc != LDAP_SUCCESS ? */
331                 } else {
332                         (*a)->a_nvals = (*a)->a_vals;
333                 }
334         }
335
336         return rc;
337 }
338
339 /*
340  * if a normalization function is defined for the equality matchingRule
341  * of desc, the value is normalized and stored in nval; otherwise nval 
342  * is NULL
343  */
344 int
345 attr_normalize(
346         AttributeDescription    *desc,
347         BerVarray               vals,
348         BerVarray               *nvalsp,
349         void                    *memctx )
350 {
351         int             rc = LDAP_SUCCESS;
352         BerVarray       nvals = NULL;
353
354         *nvalsp = NULL;
355
356         if ( desc->ad_type->sat_equality &&
357                 desc->ad_type->sat_equality->smr_normalize )
358         {
359                 int     i;
360                 
361                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
362
363                 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
364                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
365                         rc = desc->ad_type->sat_equality->smr_normalize(
366                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
367                                         desc->ad_type->sat_syntax,
368                                         desc->ad_type->sat_equality,
369                                         &vals[i], &nvals[i], memctx );
370
371                         if ( rc != LDAP_SUCCESS ) {
372                                 BER_BVZERO( &nvals[i + 1] );
373                                 break;
374                         }
375                 }
376                 BER_BVZERO( &nvals[i] );
377                 *nvalsp = nvals;
378         }
379
380 error_return:;
381         if ( rc != LDAP_SUCCESS && nvals != NULL ) {
382                 ber_bvarray_free_x( nvals, memctx );
383         }
384
385         return rc;
386 }
387
388 int
389 attr_merge_normalize(
390         Entry                   *e,
391         AttributeDescription    *desc,
392         BerVarray               vals,
393         void                    *memctx )
394 {
395         BerVarray       nvals = NULL;
396         int             rc;
397
398         rc = attr_normalize( desc, vals, &nvals, memctx );
399         if ( rc == LDAP_SUCCESS ) {
400                 rc = attr_merge( e, desc, vals, nvals );
401                 if ( nvals != NULL ) {
402                         ber_bvarray_free_x( nvals, memctx );
403                 }
404         }
405
406         return rc;
407 }
408
409 int
410 attr_merge_one(
411         Entry           *e,
412         AttributeDescription *desc,
413         struct berval   *val,
414         struct berval   *nval )
415 {
416         int rc;
417         Attribute       **a;
418
419         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
420                 if ( (*a)->a_desc == desc ) {
421                         break;
422                 }
423         }
424
425         if ( *a == NULL ) {
426                 *a = attr_alloc( desc );
427         }
428
429         rc = value_add_one( &(*a)->a_vals, val );
430
431         if ( rc == LDAP_SUCCESS ) {
432                 if ( nval ) {
433                         rc = value_add_one( &(*a)->a_nvals, nval );
434                         /* FIXME: what if rc != LDAP_SUCCESS ? */
435                 } else {
436                         (*a)->a_nvals = (*a)->a_vals;
437                 }
438         }
439         return rc;
440 }
441
442 /*
443  * if a normalization function is defined for the equality matchingRule
444  * of desc, the value is normalized and stored in nval; otherwise nval 
445  * is NULL
446  */
447 int
448 attr_normalize_one(
449         AttributeDescription *desc,
450         struct berval   *val,
451         struct berval   *nval,
452         void            *memctx )
453 {
454         int             rc = LDAP_SUCCESS;
455
456         BER_BVZERO( nval );
457
458         if ( desc->ad_type->sat_equality &&
459                 desc->ad_type->sat_equality->smr_normalize )
460         {
461                 rc = desc->ad_type->sat_equality->smr_normalize(
462                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
463                                 desc->ad_type->sat_syntax,
464                                 desc->ad_type->sat_equality,
465                                 val, nval, memctx );
466
467                 if ( rc != LDAP_SUCCESS ) {
468                         return rc;
469                 }
470         }
471
472         return rc;
473 }
474
475 int
476 attr_merge_normalize_one(
477         Entry           *e,
478         AttributeDescription *desc,
479         struct berval   *val,
480         void            *memctx )
481 {
482         struct berval   nval = BER_BVNULL;
483         struct berval   *nvalp = NULL;
484         int             rc;
485
486         rc = attr_normalize_one( desc, val, &nval, memctx );
487         if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &nval ) ) {
488                 nvalp = &nval;
489         }
490
491         rc = attr_merge_one( e, desc, val, nvalp );
492         if ( nvalp != NULL ) {
493                 slap_sl_free( nval.bv_val, memctx );
494         }
495         return rc;
496 }
497
498 /*
499  * attrs_find - find attribute(s) by AttributeDescription
500  * returns next attribute which is subtype of provided description.
501  */
502
503 Attribute *
504 attrs_find(
505     Attribute   *a,
506         AttributeDescription *desc )
507 {
508         for ( ; a != NULL; a = a->a_next ) {
509                 if ( is_ad_subtype( a->a_desc, desc ) ) {
510                         return( a );
511                 }
512         }
513
514         return( NULL );
515 }
516
517 /*
518  * attr_find - find attribute by type
519  */
520
521 Attribute *
522 attr_find(
523     Attribute   *a,
524         AttributeDescription *desc )
525 {
526         for ( ; a != NULL; a = a->a_next ) {
527                 if ( a->a_desc == desc ) {
528                         return( a );
529                 }
530         }
531
532         return( NULL );
533 }
534
535 /*
536  * attr_delete - delete the attribute type in list pointed to by attrs
537  * return       0       deleted ok
538  *              1       not found in list a
539  *              -1      something bad happened
540  */
541
542 int
543 attr_delete(
544     Attribute   **attrs,
545         AttributeDescription *desc )
546 {
547         Attribute       **a;
548
549         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
550                 if ( (*a)->a_desc == desc ) {
551                         Attribute       *save = *a;
552                         *a = (*a)->a_next;
553                         attr_free( save );
554
555                         return LDAP_SUCCESS;
556                 }
557         }
558
559         return LDAP_NO_SUCH_ATTRIBUTE;
560 }
561
562 int
563 attr_init( void )
564 {
565         ldap_pvt_thread_mutex_init( &attr_mutex );
566         return 0;
567 }
568
569 int
570 attr_destroy( void )
571 {
572         slap_list *a;
573
574         for ( a=attr_chunks; a; a=attr_chunks ) {
575                 attr_chunks = a->next;
576                 free( a );
577         }
578         ldap_pvt_thread_mutex_destroy( &attr_mutex );
579         return 0;
580 }