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