Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / attr.c
blob46339703959230738eb96be959d3dc4b7f4a9c62
1 /* attr.c - routines for dealing with attributes */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/attr.c,v 1.112.2.8 2008/07/10 00:17:13 quanah Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
6 * All rights reserved.
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.
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>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
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.
27 #include "portable.h"
29 #include <stdio.h>
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #endif
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>
41 #include "slap.h"
44 * Allocate in chunks, minimum of 1000 at a time.
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;
54 int
55 attr_prealloc( int num )
57 Attribute *a;
58 slap_list *s;
60 if (!num) return 0;
62 s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Attribute));
63 s->next = attr_chunks;
64 attr_chunks = s;
66 a = (Attribute *)(s+1);
67 for ( ;num>1; num--) {
68 a->a_next = a+1;
69 a++;
71 a->a_next = attr_list;
72 attr_list = (Attribute *)(s+1);
74 return 0;
77 Attribute *
78 attr_alloc( AttributeDescription *ad )
80 Attribute *a;
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 );
90 a->a_desc = ad;
92 return a;
95 /* Return a list of num attrs */
96 Attribute *
97 attrs_alloc( int num )
99 Attribute *head = NULL;
100 Attribute **a;
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--;
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--;
117 attr_list = *a;
119 *a = NULL;
120 ldap_pvt_thread_mutex_unlock( &attr_mutex );
122 return head;
126 void
127 attr_clean( Attribute *a )
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 );
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 );
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;
159 void
160 attr_free( Attribute *a )
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 );
169 #ifdef LDAP_COMP_MATCH
170 void
171 comp_tree_free( Attribute *a )
173 Attribute *next;
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 );
184 #endif
186 void
187 attrs_free( Attribute *a )
189 if ( a ) {
190 Attribute *b = (Attribute *)0xBAD, *tail, *next;
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 );
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 );
211 static void
212 attr_dup2( Attribute *tmp, Attribute *a )
214 tmp->a_flags = a->a_flags & SLAP_ATTR_PERSISTENT_FLAGS;
215 if ( a->a_vals != NULL ) {
216 int i;
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? */
225 BER_BVZERO( &tmp->a_vals[i] );
227 /* a_nvals must be non null; it may be equal to a_vals */
228 assert( a->a_nvals != NULL );
230 if ( a->a_nvals != a->a_vals ) {
231 int j;
233 tmp->a_nvals = ch_malloc( (tmp->a_numvals + 1) * sizeof(struct berval) );
234 for ( j = 0; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
235 assert( j < i );
236 ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
237 if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
238 /* FIXME: error? */
240 assert( j == i );
241 BER_BVZERO( &tmp->a_nvals[j] );
243 } else {
244 tmp->a_nvals = tmp->a_vals;
249 Attribute *
250 attr_dup( Attribute *a )
252 Attribute *tmp;
254 if ( a == NULL) return NULL;
256 tmp = attr_alloc( a->a_desc );
257 attr_dup2( tmp, a );
258 return tmp;
261 Attribute *
262 attrs_dup( Attribute *a )
264 int i;
265 Attribute *tmp, *anew;
267 if( a == NULL ) return NULL;
269 /* count them */
270 for( tmp=a,i=0; tmp; tmp=tmp->a_next ) {
271 i++;
274 anew = attrs_alloc( i );
276 for( tmp=anew; a; a=a->a_next ) {
277 tmp->a_desc = a->a_desc;
278 attr_dup2( tmp, a );
279 tmp=tmp->a_next;
282 return anew;
286 attr_valfind(
287 Attribute *a,
288 unsigned flags,
289 struct berval *val,
290 unsigned *slot,
291 void *ctx )
293 struct berval nval = BER_BVNULL, *cval;
294 MatchingRule *mr;
295 const char *text;
296 int match = -1, rc;
297 unsigned i;
299 if ( flags & SLAP_MR_ORDERING )
300 mr = a->a_desc->ad_type->sat_ordering;
301 else
302 mr = a->a_desc->ad_type->sat_equality;
304 if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
305 mr->smr_normalize )
307 rc = (mr->smr_normalize)(
308 flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX),
309 a->a_desc->ad_type->sat_syntax,
310 mr, val, &nval, ctx );
312 if( rc != LDAP_SUCCESS ) {
313 return LDAP_INVALID_SYNTAX;
315 cval = &nval;
316 } else {
317 cval = val;
320 if ( a->a_flags & SLAP_ATTR_SORTED_VALS ) {
321 /* Binary search */
322 unsigned base = 0, n = a->a_numvals;
324 while ( 0 < n ) {
325 unsigned pivot = n >> 1;
326 i = base + pivot;
327 rc = value_match( &match, a->a_desc, mr, flags,
328 &a->a_nvals[i], cval, &text );
329 if ( rc == LDAP_SUCCESS && match == 0 )
330 break;
331 if ( match < 0 ) {
332 base = i+1;
333 n -= pivot+1;
334 } else {
335 n = pivot;
338 if ( match < 0 )
339 i++;
340 } else {
341 /* Linear search */
342 for ( i = 0; i < a->a_numvals; i++ ) {
343 const char *text;
345 rc = ordered_value_match( &match, a->a_desc, mr, flags,
346 &a->a_nvals[i], cval, &text );
347 if ( rc == LDAP_SUCCESS && match == 0 )
348 break;
351 if ( slot )
352 *slot = i;
353 if ( match )
354 rc = LDAP_NO_SUCH_ATTRIBUTE;
355 if ( nval.bv_val )
356 slap_sl_free( nval.bv_val, ctx );
358 return rc;
362 attr_valadd(
363 Attribute *a,
364 BerVarray vals,
365 BerVarray nvals,
366 int nn )
368 int i;
369 BerVarray v2;
371 v2 = (BerVarray) SLAP_REALLOC( (char *) a->a_vals,
372 (a->a_numvals + nn + 1) * sizeof(struct berval) );
373 if( v2 == NULL ) {
374 Debug(LDAP_DEBUG_TRACE,
375 "attr_valadd: SLAP_REALLOC failed.\n", 0, 0, 0 );
376 return LBER_ERROR_MEMORY;
378 a->a_vals = v2;
379 if ( nvals ) {
380 v2 = (BerVarray) SLAP_REALLOC( (char *) a->a_nvals,
381 (a->a_numvals + nn + 1) * sizeof(struct berval) );
382 if( v2 == NULL ) {
383 Debug(LDAP_DEBUG_TRACE,
384 "attr_valadd: SLAP_REALLOC failed.\n", 0, 0, 0 );
385 return LBER_ERROR_MEMORY;
387 a->a_nvals = v2;
388 } else {
389 a->a_nvals = a->a_vals;
392 /* If sorted and old vals exist, must insert */
393 if (( a->a_flags & SLAP_ATTR_SORTED_VALS ) && a->a_numvals ) {
394 unsigned slot;
395 int j, rc;
396 v2 = nvals ? nvals : vals;
397 for ( i = 0; i < nn; i++ ) {
398 rc = attr_valfind( a, SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX |
399 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
400 &v2[i], &slot, NULL );
401 if ( rc != LDAP_NO_SUCH_ATTRIBUTE ) {
402 /* should never happen */
403 if ( rc == LDAP_SUCCESS )
404 rc = LDAP_TYPE_OR_VALUE_EXISTS;
405 return rc;
407 for ( j = a->a_numvals; j >= slot; j-- ) {
408 a->a_vals[j+1] = a->a_vals[j];
409 if ( nvals )
410 a->a_nvals[j+1] = a->a_nvals[j];
412 ber_dupbv( &a->a_nvals[slot], &v2[i] );
413 if ( nvals )
414 ber_dupbv( &a->a_vals[slot], &vals[i] );
415 a->a_numvals++;
417 BER_BVZERO( &a->a_vals[a->a_numvals] );
418 if ( a->a_vals != a->a_nvals )
419 BER_BVZERO( &a->a_nvals[a->a_numvals] );
420 } else {
421 v2 = &a->a_vals[a->a_numvals];
422 for ( i = 0 ; i < nn; i++ ) {
423 ber_dupbv( &v2[i], &vals[i] );
424 if ( BER_BVISNULL( &v2[i] ) ) break;
426 BER_BVZERO( &v2[i] );
428 if ( nvals ) {
429 v2 = &a->a_nvals[a->a_numvals];
430 for ( i = 0 ; i < nn; i++ ) {
431 ber_dupbv( &v2[i], &nvals[i] );
432 if ( BER_BVISNULL( &v2[i] ) ) break;
434 BER_BVZERO( &v2[i] );
436 a->a_numvals += i;
438 return 0;
442 * attr_merge - merge the given type and value with the list of
443 * attributes in attrs.
445 * nvals must be NULL if the attribute has no normalizer.
446 * In this case, a->a_nvals will be set equal to a->a_vals.
448 * returns 0 everything went ok
449 * -1 trouble
453 attr_merge(
454 Entry *e,
455 AttributeDescription *desc,
456 BerVarray vals,
457 BerVarray nvals )
459 int i = 0;
461 Attribute **a;
463 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
464 if ( (*a)->a_desc == desc ) {
465 break;
469 if ( *a == NULL ) {
470 *a = attr_alloc( desc );
471 } else {
473 * FIXME: if the attribute already exists, the presence
474 * of nvals and the value of (*a)->a_nvals must be consistent
476 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
477 || ( nvals != NULL && (
478 ( (*a)->a_vals == NULL && (*a)->a_nvals == NULL )
479 || ( (*a)->a_nvals != (*a)->a_vals ) ) ) );
482 if ( vals != NULL ) {
483 for ( ; !BER_BVISNULL( &vals[i] ); i++ ) ;
485 return attr_valadd( *a, vals, nvals, i );
489 * if a normalization function is defined for the equality matchingRule
490 * of desc, the value is normalized and stored in nval; otherwise nval
491 * is NULL
494 attr_normalize(
495 AttributeDescription *desc,
496 BerVarray vals,
497 BerVarray *nvalsp,
498 void *memctx )
500 int rc = LDAP_SUCCESS;
501 BerVarray nvals = NULL;
503 *nvalsp = NULL;
505 if ( desc->ad_type->sat_equality &&
506 desc->ad_type->sat_equality->smr_normalize )
508 int i;
510 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
512 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
513 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
514 rc = desc->ad_type->sat_equality->smr_normalize(
515 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
516 desc->ad_type->sat_syntax,
517 desc->ad_type->sat_equality,
518 &vals[i], &nvals[i], memctx );
520 if ( rc != LDAP_SUCCESS ) {
521 BER_BVZERO( &nvals[i + 1] );
522 break;
525 BER_BVZERO( &nvals[i] );
526 *nvalsp = nvals;
529 if ( rc != LDAP_SUCCESS && nvals != NULL ) {
530 ber_bvarray_free_x( nvals, memctx );
533 return rc;
537 attr_merge_normalize(
538 Entry *e,
539 AttributeDescription *desc,
540 BerVarray vals,
541 void *memctx )
543 BerVarray nvals = NULL;
544 int rc;
546 rc = attr_normalize( desc, vals, &nvals, memctx );
547 if ( rc == LDAP_SUCCESS ) {
548 rc = attr_merge( e, desc, vals, nvals );
549 if ( nvals != NULL ) {
550 ber_bvarray_free_x( nvals, memctx );
554 return rc;
558 attr_merge_one(
559 Entry *e,
560 AttributeDescription *desc,
561 struct berval *val,
562 struct berval *nval )
564 Attribute **a;
566 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
567 if ( (*a)->a_desc == desc ) {
568 break;
572 if ( *a == NULL ) {
573 *a = attr_alloc( desc );
576 return attr_valadd( *a, val, nval, 1 );
580 * if a normalization function is defined for the equality matchingRule
581 * of desc, the value is normalized and stored in nval; otherwise nval
582 * is NULL
585 attr_normalize_one(
586 AttributeDescription *desc,
587 struct berval *val,
588 struct berval *nval,
589 void *memctx )
591 int rc = LDAP_SUCCESS;
593 BER_BVZERO( nval );
595 if ( desc->ad_type->sat_equality &&
596 desc->ad_type->sat_equality->smr_normalize )
598 rc = desc->ad_type->sat_equality->smr_normalize(
599 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
600 desc->ad_type->sat_syntax,
601 desc->ad_type->sat_equality,
602 val, nval, memctx );
604 if ( rc != LDAP_SUCCESS ) {
605 return rc;
609 return rc;
613 attr_merge_normalize_one(
614 Entry *e,
615 AttributeDescription *desc,
616 struct berval *val,
617 void *memctx )
619 struct berval nval = BER_BVNULL;
620 struct berval *nvalp = NULL;
621 int rc;
623 rc = attr_normalize_one( desc, val, &nval, memctx );
624 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &nval ) ) {
625 nvalp = &nval;
628 rc = attr_merge_one( e, desc, val, nvalp );
629 if ( nvalp != NULL ) {
630 slap_sl_free( nval.bv_val, memctx );
632 return rc;
636 * attrs_find - find attribute(s) by AttributeDescription
637 * returns next attribute which is subtype of provided description.
640 Attribute *
641 attrs_find(
642 Attribute *a,
643 AttributeDescription *desc )
645 for ( ; a != NULL; a = a->a_next ) {
646 if ( is_ad_subtype( a->a_desc, desc ) ) {
647 return( a );
651 return( NULL );
655 * attr_find - find attribute by type
658 Attribute *
659 attr_find(
660 Attribute *a,
661 AttributeDescription *desc )
663 for ( ; a != NULL; a = a->a_next ) {
664 if ( a->a_desc == desc ) {
665 return( a );
669 return( NULL );
673 * attr_delete - delete the attribute type in list pointed to by attrs
674 * return 0 deleted ok
675 * 1 not found in list a
676 * -1 something bad happened
680 attr_delete(
681 Attribute **attrs,
682 AttributeDescription *desc )
684 Attribute **a;
686 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
687 if ( (*a)->a_desc == desc ) {
688 Attribute *save = *a;
689 *a = (*a)->a_next;
690 attr_free( save );
692 return LDAP_SUCCESS;
696 return LDAP_NO_SUCH_ATTRIBUTE;
700 attr_init( void )
702 ldap_pvt_thread_mutex_init( &attr_mutex );
703 return 0;
707 attr_destroy( void )
709 slap_list *a;
711 for ( a=attr_chunks; a; a=attr_chunks ) {
712 attr_chunks = a->next;
713 free( a );
715 ldap_pvt_thread_mutex_destroy( &attr_mutex );
716 return 0;