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