1 /* entry.c - routines for dealing with entries */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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.
33 #include <ac/socket.h>
34 #include <ac/string.h>
39 static char *ebuf; /* buf returned by entry2str */
40 static char *ecur; /* pointer to end of currently used ebuf */
41 static int emaxsize;/* max size of ebuf */
46 const Entry slap_entry_root = {
47 NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL
51 * these mutexes must be used when calling the entry2str()
52 * routine since it returns a pointer to static data.
54 ldap_pvt_thread_mutex_t entry2str_mutex;
56 static const struct berval dn_bv = BER_BVC("dn");
61 * Allocate in chunks, minimum of 1000 at a time.
63 #define CHUNK_SIZE 1000
64 typedef struct slap_list {
65 struct slap_list *next;
67 static slap_list *entry_chunks;
68 static Entry *entry_list;
69 static ldap_pvt_thread_mutex_t entry_mutex;
71 int entry_destroy(void)
74 if ( ebuf ) free( ebuf );
79 for ( e=entry_chunks; e; e=entry_chunks ) {
80 entry_chunks = e->next;
84 ldap_pvt_thread_mutex_destroy( &entry_mutex );
85 ldap_pvt_thread_mutex_destroy( &entry2str_mutex );
86 return attr_destroy();
92 ldap_pvt_thread_mutex_init( &entry2str_mutex );
93 ldap_pvt_thread_mutex_init( &entry_mutex );
100 return str2entry2( s, 1 );
104 str2entry2( char *s, int checkvals )
108 struct berval *type, *vals, *nvals;
110 AttributeDescription *ad, *ad_prev;
115 Attribute ahead, *atail;
118 * LDIF is used as the string format.
119 * An entry looks like this:
122 * [<attr>:[:] <value>\n]
123 * [<tab><continuedvalue>\n]*
126 * If a double colon is used after a type, it means the
127 * following value is encoded as a base 64 string. This
128 * happens if the value contains a non-printing character
132 Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
133 s ? s : "NULL", 0, 0 );
138 Debug( LDAP_DEBUG_ANY,
139 "<= str2entry NULL (entry allocation failed)\n",
144 /* initialize entry */
147 /* dn + attributes */
155 lines = ldif_countlines( s );
156 type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
158 nvals = vals+lines+1;
159 freeval = (char *)(nvals+lines+1);
162 /* parse into individual values, record DN */
163 while ( (s = ldif_getline( &next )) != NULL ) {
165 if ( *s == '\n' || *s == '\0' ) {
170 Debug( LDAP_DEBUG_TRACE,
171 "<= str2entry ran past end of entry\n", 0, 0, 0 );
175 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
178 Debug( LDAP_DEBUG_TRACE,
179 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
183 if ( type[i].bv_len == dn_bv.bv_len &&
184 strcasecmp( type[i].bv_val, dn_bv.bv_val ) == 0 ) {
186 if ( e->e_dn != NULL ) {
187 Debug( LDAP_DEBUG_ANY, "str2entry: "
188 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
189 (long) e->e_id, e->e_dn, vals[i].bv_val );
193 rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
194 if( rc != LDAP_SUCCESS ) {
195 Debug( LDAP_DEBUG_ANY, "str2entry: "
196 "entry %ld has invalid DN \"%s\"\n",
197 (long) e->e_id, vals[i].bv_val, 0 );
200 if ( freeval[i] ) free( vals[i].bv_val );
201 vals[i].bv_val = NULL;
208 /* check to make sure there was a dn: line */
209 if ( BER_BVISNULL( &e->e_name )) {
210 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
211 (long) e->e_id, 0, 0 );
215 #define bvcasematch(bv1, bv2) ( ((bv1)->bv_len == (bv2)->bv_len) && (strncasecmp((bv1)->bv_val, (bv2)->bv_val, (bv1)->bv_len) == 0) )
217 /* Make sure all attributes with multiple values are contiguous */
223 for (i=0; i<lines; i++) {
224 for ( j=i+1; j<lines; j++ ) {
225 if ( bvcasematch( type+i, type+j )) {
226 /* out of order, move intervening attributes down */
230 for ( k=j; k>i; k-- ) {
233 freeval[k] = freeval[k-1];
246 for ( i=0; i<=lines; i++ ) {
248 if ( !ad || ( i<lines && !bvcasematch( type+i, &ad->ad_cname ))) {
250 rc = slap_bv2ad( type+i, &ad, &text );
252 if( rc != LDAP_SUCCESS ) {
253 Debug( slapMode & SLAP_TOOL_MODE
254 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
255 "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
256 if( slapMode & SLAP_TOOL_MODE ) {
260 rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
261 if( rc != LDAP_SUCCESS ) {
262 Debug( LDAP_DEBUG_ANY,
263 "<= str2entry: slap_str2undef_ad(%s): %s\n",
264 type[i].bv_val, text, 0 );
270 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
272 atail->a_next = attr_alloc( NULL );
273 atail = atail->a_next;
275 atail->a_desc = ad_prev;
276 atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
277 if( ad_prev->ad_type->sat_equality &&
278 ad_prev->ad_type->sat_equality->smr_normalize )
279 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
281 atail->a_nvals = NULL;
283 for ( j=0; j<attr_cnt; j++ ) {
285 atail->a_vals[j] = vals[k];
287 ber_dupbv( atail->a_vals+j, &vals[k] );
288 vals[k].bv_val = NULL;
289 if ( atail->a_nvals ) {
290 atail->a_nvals[j] = nvals[k];
291 nvals[k].bv_val = NULL;
295 BER_BVZERO( &atail->a_vals[j] );
296 if ( atail->a_nvals ) {
297 BER_BVZERO( &atail->a_nvals[j] );
299 atail->a_nvals = atail->a_vals;
302 if ( i == lines ) break;
305 if( slapMode & SLAP_TOOL_MODE ) {
307 slap_syntax_validate_func *validate =
308 ad->ad_type->sat_syntax->ssyn_validate;
309 slap_syntax_transform_func *pretty =
310 ad->ad_type->sat_syntax->ssyn_pretty;
313 rc = ordered_value_pretty( ad,
314 &vals[i], &pval, NULL );
316 } else if ( validate ) {
318 * validate value per syntax
320 rc = ordered_value_validate( ad, &vals[i], LDAP_MOD_ADD );
323 Debug( LDAP_DEBUG_ANY,
324 "str2entry: attributeType %s #%d: "
325 "no validator for syntax %s\n",
326 ad->ad_cname.bv_val, attr_cnt,
327 ad->ad_type->sat_syntax->ssyn_oid );
332 Debug( LDAP_DEBUG_ANY,
333 "str2entry: invalid value "
334 "for attributeType %s #%d (syntax %s)\n",
335 ad->ad_cname.bv_val, attr_cnt,
336 ad->ad_type->sat_syntax->ssyn_oid );
341 if ( freeval[i] ) free( vals[i].bv_val );
347 if ( ad->ad_type->sat_equality &&
348 ad->ad_type->sat_equality->smr_normalize )
350 rc = ordered_value_normalize(
351 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
353 ad->ad_type->sat_equality,
354 &vals[i], &nvals[i], NULL );
357 Debug( LDAP_DEBUG_ANY,
358 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
367 atail->a_next = NULL;
368 e->e_attrs = ahead.a_next;
370 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
371 e->e_dn, (unsigned long) e, 0 );
375 for ( i=0; i<lines; i++ ) {
376 if ( freeval[i] ) free( vals[i].bv_val );
377 free( nvals[i].bv_val );
385 #define GRABSIZE BUFSIZ
387 #define MAKE_SPACE( n ) { \
388 while ( ecur + (n) > ebuf + emaxsize ) { \
390 offset = (int) (ecur - ebuf); \
391 ebuf = ch_realloc( ebuf, \
392 emaxsize + GRABSIZE ); \
393 emaxsize += GRABSIZE; \
394 ecur = ebuf + offset; \
411 * In string format, an entry looks like this:
413 * [<attr>: <value>\n]*
419 if ( e->e_dn != NULL ) {
421 tmplen = e->e_name.bv_len;
422 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
423 ldif_sput( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
426 /* put the attributes */
427 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
428 /* put "<type>:[:] <value>" line for each value */
429 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
431 tmplen = a->a_desc->ad_cname.bv_len;
432 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
433 ldif_sput( &ecur, LDIF_PUT_VALUE,
434 a->a_desc->ad_cname.bv_val,
435 bv->bv_val, bv->bv_len );
446 entry_clean( Entry *e )
448 /* free an entry structure */
451 /* e_private must be freed by the caller */
452 assert( e->e_private == NULL );
457 if ( !BER_BVISNULL( &e->e_name ) ) {
458 free( e->e_name.bv_val );
459 BER_BVZERO( &e->e_name );
461 if ( !BER_BVISNULL( &e->e_nname ) ) {
462 free( e->e_nname.bv_val );
463 BER_BVZERO( &e->e_nname );
466 if ( !BER_BVISNULL( &e->e_bv ) ) {
467 free( e->e_bv.bv_val );
468 BER_BVZERO( &e->e_bv );
471 /* free attributes */
473 attrs_free( e->e_attrs );
481 entry_free( Entry *e )
485 ldap_pvt_thread_mutex_lock( &entry_mutex );
486 e->e_private = entry_list;
488 ldap_pvt_thread_mutex_unlock( &entry_mutex );
491 /* These parameters work well on AMD64 */
499 #define STRIDE_FACTOR (STRIDE*STRIPE)
502 entry_prealloc( int num )
504 Entry *e, **prev, *tmp;
510 #if STRIDE_FACTOR > 1
511 /* Round up to our stride factor */
512 num += STRIDE_FACTOR-1;
513 num /= STRIDE_FACTOR;
514 num *= STRIDE_FACTOR;
517 s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Entry));
518 s->next = entry_chunks;
522 for (i=0; i<STRIPE; i++) {
525 for (j=i; j<num; j+= STRIDE) {
527 prev = (Entry **)&e->e_private;
532 entry_list = (Entry *)(s+1);
542 ldap_pvt_thread_mutex_lock( &entry_mutex );
544 entry_prealloc( CHUNK_SIZE );
546 entry_list = e->e_private;
548 ldap_pvt_thread_mutex_unlock( &entry_mutex );
555 * These routines are used only by Backend.
557 * the Entry has three entry points (ways to find things):
559 * by entry e.g., if you already have an entry from the cache
560 * and want to delete it. (really by entry ptr)
561 * by dn e.g., when looking for the base object of a search
562 * by id e.g., for search candidates
564 * these correspond to three different avl trees that are maintained.
568 entry_cmp( Entry *e1, Entry *e2 )
570 return SLAP_PTRCMP( e1, e2 );
574 entry_dn_cmp( const void *v_e1, const void *v_e2 )
576 /* compare their normalized UPPERCASED dn's */
577 const Entry *e1 = v_e1, *e2 = v_e2;
579 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
583 entry_id_cmp( const void *v_e1, const void *v_e2 )
585 const Entry *e1 = v_e1, *e2 = v_e2;
586 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
589 /* This is like a ber_len */
590 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
591 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
594 entry_putlen(unsigned char **buf, ber_len_t len)
596 ber_len_t lenlen = entry_lenlen(len);
599 **buf = (unsigned char) len;
602 **buf = 0x80 | ((unsigned char) lenlen - 1);
603 for (i=lenlen-1; i>0; i--) {
604 (*buf)[i] = (unsigned char) len;
612 entry_getlen(unsigned char **buf)
629 /* Count up the sizes of the components of an entry */
630 void entry_partsize(Entry *e, ber_len_t *plen,
631 int *pnattrs, int *pnvals, int norm)
633 ber_len_t len, dnlen, ndnlen;
634 int i, nat = 0, nval = 0;
637 dnlen = e->e_name.bv_len;
638 len = dnlen + 1; /* trailing NUL byte */
639 len += entry_lenlen(dnlen);
641 ndnlen = e->e_nname.bv_len;
643 len += entry_lenlen(ndnlen);
645 for (a=e->e_attrs; a; a=a->a_next) {
646 /* For AttributeDesc, we only store the attr name */
648 len += a->a_desc->ad_cname.bv_len+1;
649 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
650 for (i=0; a->a_vals[i].bv_val; i++) {
652 len += a->a_vals[i].bv_len + 1;
653 len += entry_lenlen(a->a_vals[i].bv_len);
655 len += entry_lenlen(i);
656 nval++; /* empty berval at end */
657 if (norm && a->a_nvals != a->a_vals) {
658 for (i=0; a->a_nvals[i].bv_val; i++) {
660 len += a->a_nvals[i].bv_len + 1;
661 len += entry_lenlen(a->a_nvals[i].bv_len);
663 len += entry_lenlen(i); /* i nvals */
666 len += entry_lenlen(0); /* 0 nvals */
669 len += entry_lenlen(nat);
670 len += entry_lenlen(nval);
676 /* Add up the size of the entry for a flattened buffer */
677 ber_len_t entry_flatsize(Entry *e, int norm)
682 entry_partsize(e, &len, &nattrs, &nvals, norm);
683 len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
684 (nvals * sizeof(struct berval));
688 /* Flatten an Entry into a buffer. The buffer is filled with just the
689 * strings/bervals of all the entry components. Each field is preceded
690 * by its length, encoded the way ber_put_len works. Every field is NUL
691 * terminated. The entire buffer size is precomputed so that a single
692 * malloc can be performed. The entry size is also recorded,
693 * to aid in entry_decode.
695 int entry_encode(Entry *e, struct berval *bv)
697 ber_len_t len, dnlen, ndnlen;
698 int i, nattrs, nvals;
702 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
703 (long) e->e_id, e->e_dn, 0 );
704 dnlen = e->e_name.bv_len;
705 ndnlen = e->e_nname.bv_len;
707 entry_partsize( e, &len, &nattrs, &nvals, 1 );
710 bv->bv_val = ch_malloc(len);
711 ptr = (unsigned char *)bv->bv_val;
712 entry_putlen(&ptr, nattrs);
713 entry_putlen(&ptr, nvals);
714 entry_putlen(&ptr, dnlen);
715 AC_MEMCPY(ptr, e->e_dn, dnlen);
718 entry_putlen(&ptr, ndnlen);
719 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
723 for (a=e->e_attrs; a; a=a->a_next) {
724 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
725 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
726 a->a_desc->ad_cname.bv_len);
727 ptr += a->a_desc->ad_cname.bv_len;
730 for (i=0; a->a_vals[i].bv_val; i++);
731 entry_putlen(&ptr, i);
732 for (i=0; a->a_vals[i].bv_val; i++) {
733 entry_putlen(&ptr, a->a_vals[i].bv_len);
734 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
735 a->a_vals[i].bv_len);
736 ptr += a->a_vals[i].bv_len;
739 if (a->a_nvals != a->a_vals) {
740 entry_putlen(&ptr, i);
741 for (i=0; a->a_nvals[i].bv_val; i++) {
742 entry_putlen(&ptr, a->a_nvals[i].bv_len);
743 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
744 a->a_nvals[i].bv_len);
745 ptr += a->a_nvals[i].bv_len;
749 entry_putlen(&ptr, 0);
756 /* Retrieve an Entry that was stored using entry_encode above.
757 * First entry_header must be called to decode the size of the entry.
758 * Then a single block of memory must be malloc'd to accomodate the
759 * bervals and the bulk data. Next the bulk data is retrieved from
760 * the DB and parsed by entry_decode.
762 * Note: everything is stored in a single contiguous block, so
763 * you can not free individual attributes or names from this
764 * structure. Attempting to do so will likely corrupt memory.
766 int entry_header(EntryHeader *eh)
768 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
770 eh->nattrs = entry_getlen(&ptr);
772 Debug( LDAP_DEBUG_ANY,
773 "entry_header: attribute count was zero\n", 0, 0, 0);
776 eh->nvals = entry_getlen(&ptr);
778 Debug( LDAP_DEBUG_ANY,
779 "entry_header: value count was zero\n", 0, 0, 0);
782 eh->data = (char *)ptr;
786 #ifdef SLAP_ZONE_ALLOC
787 int entry_decode(EntryHeader *eh, Entry **e, void *ctx)
789 int entry_decode(EntryHeader *eh, Entry **e)
792 int i, j, count, nattrs, nvals;
797 AttributeDescription *ad;
798 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
804 x->e_attrs = attrs_alloc( nattrs );
805 ptr = (unsigned char *)eh->data;
806 i = entry_getlen(&ptr);
807 x->e_name.bv_val = (char *) ptr;
808 x->e_name.bv_len = i;
810 i = entry_getlen(&ptr);
811 x->e_nname.bv_val = (char *) ptr;
812 x->e_nname.bv_len = i;
814 Debug( LDAP_DEBUG_TRACE,
815 "entry_decode: \"%s\"\n",
820 bptr = (BerVarray)eh->bv.bv_val;
822 while ((i = entry_getlen(&ptr))) {
825 bv.bv_val = (char *) ptr;
827 rc = slap_bv2ad( &bv, &ad, &text );
829 if( rc != LDAP_SUCCESS ) {
830 Debug( LDAP_DEBUG_TRACE,
831 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
832 rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
834 if( rc != LDAP_SUCCESS ) {
835 Debug( LDAP_DEBUG_ANY,
836 "<= entry_decode: slap_str2undef_ad(%s): %s\n",
843 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
844 count = j = entry_getlen(&ptr);
848 i = entry_getlen(&ptr);
850 bptr->bv_val = (char *)ptr;
859 j = entry_getlen(&ptr);
863 i = entry_getlen(&ptr);
865 bptr->bv_val = (char *)ptr;
874 a->a_nvals = a->a_vals;
882 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
888 Entry *entry_dup( Entry *e )
895 ber_dupbv( &ret->e_name, &e->e_name );
896 ber_dupbv( &ret->e_nname, &e->e_nname );
897 ret->e_attrs = attrs_dup( e->e_attrs );
898 ret->e_ocflags = e->e_ocflags;
904 /* Duplicates an entry using a single malloc. Saves CPU time, increases
905 * heap usage because a single large malloc is harder to satisfy than
906 * lots of small ones, and the freed space isn't as easily reusable.
908 * Probably not worth using this function.
910 Entry *entry_dup_bv( Entry *e )
917 Attribute *src, *dst;
921 entry_partsize(e, &len, &nattrs, &nvals, 1);
923 ret->e_attrs = attrs_alloc( nattrs );
924 ret->e_ocflags = e->e_ocflags;
925 ret->e_bv.bv_len = len + nvals * sizeof(struct berval);
926 ret->e_bv.bv_val = ch_malloc( ret->e_bv.bv_len );
928 bvl = (struct berval *)ret->e_bv.bv_val;
929 ptr = (char *)(bvl + nvals);
931 ret->e_name.bv_len = e->e_name.bv_len;
932 ret->e_name.bv_val = ptr;
933 AC_MEMCPY( ptr, e->e_name.bv_val, e->e_name.bv_len );
934 ptr += e->e_name.bv_len;
937 ret->e_nname.bv_len = e->e_nname.bv_len;
938 ret->e_nname.bv_val = ptr;
939 AC_MEMCPY( ptr, e->e_nname.bv_val, e->e_nname.bv_len );
940 ptr += e->e_name.bv_len;
944 for (src = e->e_attrs; src; src=src->a_next,dst=dst->a_next ) {
946 dst->a_desc = src->a_desc;
947 dst->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
949 for ( i=0; src->a_vals[i].bv_val; i++ ) {
950 bvl->bv_len = src->a_vals[i].bv_len;
952 AC_MEMCPY( ptr, src->a_vals[i].bv_val, bvl->bv_len );
959 if ( src->a_vals != src->a_nvals ) {
961 for ( i=0; src->a_nvals[i].bv_val; i++ ) {
962 bvl->bv_len = src->a_nvals[i].bv_len;
964 AC_MEMCPY( ptr, src->a_nvals[i].bv_val, bvl->bv_len );