]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Don't set bi_db_config since we have no config options
[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-2010 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         tmp->a_flags = a->a_flags & SLAP_ATTR_PERSISTENT_FLAGS;
215         if ( a->a_vals != NULL ) {
216                 unsigned        i, j;
217
218                 tmp->a_numvals = a->a_numvals;
219                 tmp->a_vals = ch_malloc( (tmp->a_numvals + 1) * sizeof(struct berval) );
220                 for ( i = 0; i < tmp->a_numvals; i++ ) {
221                         ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
222                         if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
223                         /* FIXME: error? */
224                 }
225                 BER_BVZERO( &tmp->a_vals[i] );
226
227                 /* a_nvals must be non null; it may be equal to a_vals */
228                 assert( a->a_nvals != NULL );
229
230                 if ( a->a_nvals != a->a_vals ) {
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_valfind(
286         Attribute *a,
287         unsigned flags,
288         struct berval *val,
289         unsigned *slot,
290         void *ctx )
291 {
292         struct berval nval = BER_BVNULL, *cval;
293         MatchingRule *mr;
294         const char *text;
295         int match = -1, rc;
296         unsigned i;
297
298         if ( flags & SLAP_MR_ORDERING )
299                 mr = a->a_desc->ad_type->sat_ordering;
300         else
301                 mr = a->a_desc->ad_type->sat_equality;
302
303         if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
304                 mr->smr_normalize )
305         {
306                 rc = (mr->smr_normalize)(
307                         flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX),
308                         a->a_desc->ad_type->sat_syntax,
309                         mr, val, &nval, ctx );
310
311                 if( rc != LDAP_SUCCESS ) {
312                         return LDAP_INVALID_SYNTAX;
313                 }
314                 cval = &nval;
315         } else {
316                 cval = val;
317         }
318
319         if ( a->a_flags & SLAP_ATTR_SORTED_VALS ) {
320                 /* Binary search */
321                 unsigned base = 0, n = a->a_numvals;
322
323                 while ( 0 < n ) {
324                         unsigned pivot = n >> 1;
325                         i = base + pivot;
326                         rc = value_match( &match, a->a_desc, mr, flags,
327                                 &a->a_nvals[i], cval, &text );
328                         if ( rc == LDAP_SUCCESS && match == 0 )
329                                 break;
330                         if ( match < 0 ) {
331                                 base = i+1;
332                                 n -= pivot+1;
333                         } else {
334                                 n = pivot;
335                         }
336                 }
337                 if ( match < 0 )
338                         i++;
339         } else {
340         /* Linear search */
341                 for ( i = 0; i < a->a_numvals; i++ ) {
342                         const char *text;
343
344                         rc = ordered_value_match( &match, a->a_desc, mr, flags,
345                                 &a->a_nvals[i], cval, &text );
346                         if ( rc == LDAP_SUCCESS && match == 0 )
347                                 break;
348                 }
349         }
350         if ( slot )
351                 *slot = i;
352         if ( match )
353                 rc = LDAP_NO_SUCH_ATTRIBUTE;
354         if ( nval.bv_val )
355                 slap_sl_free( nval.bv_val, ctx );
356
357         return rc;
358 }
359
360 int
361 attr_valadd(
362         Attribute *a,
363         BerVarray vals,
364         BerVarray nvals,
365         int nn )
366 {
367         int             i;
368         BerVarray       v2;
369
370         v2 = (BerVarray) SLAP_REALLOC( (char *) a->a_vals,
371                     (a->a_numvals + nn + 1) * sizeof(struct berval) );
372         if( v2 == NULL ) {
373                 Debug(LDAP_DEBUG_TRACE,
374                   "attr_valadd: SLAP_REALLOC failed.\n", 0, 0, 0 );
375                 return LBER_ERROR_MEMORY;
376         }
377         a->a_vals = v2;
378         if ( nvals ) {
379                 v2 = (BerVarray) SLAP_REALLOC( (char *) a->a_nvals,
380                                 (a->a_numvals + nn + 1) * sizeof(struct berval) );
381                 if( v2 == NULL ) {
382                         Debug(LDAP_DEBUG_TRACE,
383                           "attr_valadd: SLAP_REALLOC failed.\n", 0, 0, 0 );
384                         return LBER_ERROR_MEMORY;
385                 }
386                 a->a_nvals = v2;
387         } else {
388                 a->a_nvals = a->a_vals;
389         }
390
391         /* If sorted and old vals exist, must insert */
392         if (( a->a_flags & SLAP_ATTR_SORTED_VALS ) && a->a_numvals ) {
393                 unsigned slot;
394                 int j, rc;
395                 v2 = nvals ? nvals : vals;
396                 for ( i = 0; i < nn; i++ ) {
397                         rc = attr_valfind( a, SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX |
398                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
399                                 &v2[i], &slot, NULL );
400                         if ( rc != LDAP_NO_SUCH_ATTRIBUTE ) {
401                                 /* should never happen */
402                                 if ( rc == LDAP_SUCCESS )
403                                         rc = LDAP_TYPE_OR_VALUE_EXISTS;
404                                 return rc;
405                         }
406                         for ( j = a->a_numvals; j >= (int)slot; j-- ) {
407                                 a->a_vals[j+1] = a->a_vals[j];
408                                 if ( nvals )
409                                         a->a_nvals[j+1] = a->a_nvals[j];
410                         }
411                         ber_dupbv( &a->a_nvals[slot], &v2[i] );
412                         if ( nvals )
413                                 ber_dupbv( &a->a_vals[slot], &vals[i] );
414                         a->a_numvals++;
415                 }
416                 BER_BVZERO( &a->a_vals[a->a_numvals] );
417                 if ( a->a_vals != a->a_nvals )
418                         BER_BVZERO( &a->a_nvals[a->a_numvals] );
419         } else {
420                 v2 = &a->a_vals[a->a_numvals];
421                 for ( i = 0 ; i < nn; i++ ) {
422                         ber_dupbv( &v2[i], &vals[i] );
423                         if ( BER_BVISNULL( &v2[i] ) ) break;
424                 }
425                 BER_BVZERO( &v2[i] );
426
427                 if ( nvals ) {
428                         v2 = &a->a_nvals[a->a_numvals];
429                         for ( i = 0 ; i < nn; i++ ) {
430                                 ber_dupbv( &v2[i], &nvals[i] );
431                                 if ( BER_BVISNULL( &v2[i] ) ) break;
432                         }
433                         BER_BVZERO( &v2[i] );
434                 }
435                 a->a_numvals += i;
436         }
437         return 0;
438 }
439
440 /*
441  * attr_merge - merge the given type and value with the list of
442  * attributes in attrs.
443  *
444  * nvals must be NULL if the attribute has no normalizer.
445  * In this case, a->a_nvals will be set equal to a->a_vals.
446  *
447  * returns      0       everything went ok
448  *              -1      trouble
449  */
450
451 int
452 attr_merge(
453         Entry           *e,
454         AttributeDescription *desc,
455         BerVarray       vals,
456         BerVarray       nvals )
457 {
458         int i = 0;
459
460         Attribute       **a;
461
462         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
463                 if (  (*a)->a_desc == desc ) {
464                         break;
465                 }
466         }
467
468         if ( *a == NULL ) {
469                 *a = attr_alloc( desc );
470         } else {
471                 /*
472                  * FIXME: if the attribute already exists, the presence
473                  * of nvals and the value of (*a)->a_nvals must be consistent
474                  */
475                 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
476                                 || ( nvals != NULL && (
477                                         ( (*a)->a_vals == NULL && (*a)->a_nvals == NULL )
478                                         || ( (*a)->a_nvals != (*a)->a_vals ) ) ) );
479         }
480
481         if ( vals != NULL ) {
482                 for ( ; !BER_BVISNULL( &vals[i] ); i++ ) ;
483         }
484         return attr_valadd( *a, vals, nvals, i );
485 }
486
487 /*
488  * if a normalization function is defined for the equality matchingRule
489  * of desc, the value is normalized and stored in nval; otherwise nval 
490  * is NULL
491  */
492 int
493 attr_normalize(
494         AttributeDescription    *desc,
495         BerVarray               vals,
496         BerVarray               *nvalsp,
497         void                    *memctx )
498 {
499         int             rc = LDAP_SUCCESS;
500         BerVarray       nvals = NULL;
501
502         *nvalsp = NULL;
503
504         if ( desc->ad_type->sat_equality &&
505                 desc->ad_type->sat_equality->smr_normalize )
506         {
507                 int     i;
508                 
509                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
510
511                 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
512                 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
513                         rc = desc->ad_type->sat_equality->smr_normalize(
514                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
515                                         desc->ad_type->sat_syntax,
516                                         desc->ad_type->sat_equality,
517                                         &vals[i], &nvals[i], memctx );
518
519                         if ( rc != LDAP_SUCCESS ) {
520                                 BER_BVZERO( &nvals[i + 1] );
521                                 break;
522                         }
523                 }
524                 BER_BVZERO( &nvals[i] );
525                 *nvalsp = nvals;
526         }
527
528         if ( rc != LDAP_SUCCESS && nvals != NULL ) {
529                 ber_bvarray_free_x( nvals, memctx );
530         }
531
532         return rc;
533 }
534
535 int
536 attr_merge_normalize(
537         Entry                   *e,
538         AttributeDescription    *desc,
539         BerVarray               vals,
540         void                    *memctx )
541 {
542         BerVarray       nvals = NULL;
543         int             rc;
544
545         rc = attr_normalize( desc, vals, &nvals, memctx );
546         if ( rc == LDAP_SUCCESS ) {
547                 rc = attr_merge( e, desc, vals, nvals );
548                 if ( nvals != NULL ) {
549                         ber_bvarray_free_x( nvals, memctx );
550                 }
551         }
552
553         return rc;
554 }
555
556 int
557 attr_merge_one(
558         Entry           *e,
559         AttributeDescription *desc,
560         struct berval   *val,
561         struct berval   *nval )
562 {
563         Attribute       **a;
564
565         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
566                 if ( (*a)->a_desc == desc ) {
567                         break;
568                 }
569         }
570
571         if ( *a == NULL ) {
572                 *a = attr_alloc( desc );
573         }
574
575         return attr_valadd( *a, val, nval, 1 );
576 }
577
578 /*
579  * if a normalization function is defined for the equality matchingRule
580  * of desc, the value is normalized and stored in nval; otherwise nval 
581  * is NULL
582  */
583 int
584 attr_normalize_one(
585         AttributeDescription *desc,
586         struct berval   *val,
587         struct berval   *nval,
588         void            *memctx )
589 {
590         int             rc = LDAP_SUCCESS;
591
592         BER_BVZERO( nval );
593
594         if ( desc->ad_type->sat_equality &&
595                 desc->ad_type->sat_equality->smr_normalize )
596         {
597                 rc = desc->ad_type->sat_equality->smr_normalize(
598                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
599                                 desc->ad_type->sat_syntax,
600                                 desc->ad_type->sat_equality,
601                                 val, nval, memctx );
602
603                 if ( rc != LDAP_SUCCESS ) {
604                         return rc;
605                 }
606         }
607
608         return rc;
609 }
610
611 int
612 attr_merge_normalize_one(
613         Entry           *e,
614         AttributeDescription *desc,
615         struct berval   *val,
616         void            *memctx )
617 {
618         struct berval   nval = BER_BVNULL;
619         struct berval   *nvalp = NULL;
620         int             rc;
621
622         rc = attr_normalize_one( desc, val, &nval, memctx );
623         if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &nval ) ) {
624                 nvalp = &nval;
625         }
626
627         rc = attr_merge_one( e, desc, val, nvalp );
628         if ( nvalp != NULL ) {
629                 slap_sl_free( nval.bv_val, memctx );
630         }
631         return rc;
632 }
633
634 /*
635  * attrs_find - find attribute(s) by AttributeDescription
636  * returns next attribute which is subtype of provided description.
637  */
638
639 Attribute *
640 attrs_find(
641     Attribute   *a,
642         AttributeDescription *desc )
643 {
644         for ( ; a != NULL; a = a->a_next ) {
645                 if ( is_ad_subtype( a->a_desc, desc ) ) {
646                         return( a );
647                 }
648         }
649
650         return( NULL );
651 }
652
653 /*
654  * attr_find - find attribute by type
655  */
656
657 Attribute *
658 attr_find(
659     Attribute   *a,
660         AttributeDescription *desc )
661 {
662         for ( ; a != NULL; a = a->a_next ) {
663                 if ( a->a_desc == desc ) {
664                         return( a );
665                 }
666         }
667
668         return( NULL );
669 }
670
671 /*
672  * attr_delete - delete the attribute type in list pointed to by attrs
673  * return       0       deleted ok
674  *              1       not found in list a
675  *              -1      something bad happened
676  */
677
678 int
679 attr_delete(
680     Attribute   **attrs,
681         AttributeDescription *desc )
682 {
683         Attribute       **a;
684
685         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
686                 if ( (*a)->a_desc == desc ) {
687                         Attribute       *save = *a;
688                         *a = (*a)->a_next;
689                         attr_free( save );
690
691                         return LDAP_SUCCESS;
692                 }
693         }
694
695         return LDAP_NO_SUCH_ATTRIBUTE;
696 }
697
698 int
699 attr_init( void )
700 {
701         ldap_pvt_thread_mutex_init( &attr_mutex );
702         return 0;
703 }
704
705 int
706 attr_destroy( void )
707 {
708         slap_list *a;
709
710         for ( a=attr_chunks; a; a=attr_chunks ) {
711                 attr_chunks = a->next;
712                 free( a );
713         }
714         ldap_pvt_thread_mutex_destroy( &attr_mutex );
715         return 0;
716 }