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 );
103 #define bvcasematch(bv1, bv2) (ber_bvstrcasecmp(bv1, bv2) == 0)
106 str2entry2( char *s, int checkvals )
110 struct berval *type, *vals, *nvals;
112 AttributeDescription *ad, *ad_prev;
117 Attribute ahead, *atail;
120 * LDIF is used as the string format.
121 * An entry looks like this:
124 * [<attr>:[:] <value>\n]
125 * [<tab><continuedvalue>\n]*
128 * If a double colon is used after a type, it means the
129 * following value is encoded as a base 64 string. This
130 * happens if the value contains a non-printing character
134 Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
135 s ? s : "NULL", 0, 0 );
140 Debug( LDAP_DEBUG_ANY,
141 "<= str2entry NULL (entry allocation failed)\n",
146 /* initialize entry */
149 /* dn + attributes */
157 lines = ldif_countlines( s );
158 type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
160 nvals = vals+lines+1;
161 freeval = (char *)(nvals+lines+1);
164 /* parse into individual values, record DN */
165 while ( (s = ldif_getline( &next )) != NULL ) {
167 if ( *s == '\n' || *s == '\0' ) {
172 Debug( LDAP_DEBUG_TRACE,
173 "<= str2entry ran past end of entry\n", 0, 0, 0 );
177 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
180 Debug( LDAP_DEBUG_TRACE,
181 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
185 if ( bvcasematch( &type[i], &dn_bv ) ) {
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 /* Make sure all attributes with multiple values are contiguous */
221 for (i=0; i<lines; i++) {
222 for ( j=i+1; j<lines; j++ ) {
223 if ( bvcasematch( type+i, type+j )) {
224 /* out of order, move intervening attributes down */
228 for ( k=j; k>i; k-- ) {
231 freeval[k] = freeval[k-1];
244 for ( i=0; i<=lines; i++ ) {
246 if ( !ad || ( i<lines && !bvcasematch( type+i, &ad->ad_cname ))) {
248 rc = slap_bv2ad( type+i, &ad, &text );
250 if( rc != LDAP_SUCCESS ) {
251 Debug( slapMode & SLAP_TOOL_MODE
252 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
253 "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
254 if( slapMode & SLAP_TOOL_MODE ) {
258 rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
259 if( rc != LDAP_SUCCESS ) {
260 Debug( LDAP_DEBUG_ANY,
261 "<= str2entry: slap_str2undef_ad(%s): %s\n",
262 type[i].bv_val, text, 0 );
267 /* require ';binary' when appropriate (ITS#5071) */
268 if ( slap_syntax_is_binary( ad->ad_type->sat_syntax ) && !slap_ad_is_binary( ad ) ) {
269 Debug( LDAP_DEBUG_ANY,
270 "str2entry: attributeType %s #%d: "
271 "needs ';binary' transfer as per syntax %s\n",
272 ad->ad_cname.bv_val, 0,
273 ad->ad_type->sat_syntax->ssyn_oid );
278 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
280 atail->a_next = attr_alloc( NULL );
281 atail = atail->a_next;
283 atail->a_desc = ad_prev;
284 atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
285 if( ad_prev->ad_type->sat_equality &&
286 ad_prev->ad_type->sat_equality->smr_normalize )
287 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
289 atail->a_nvals = NULL;
291 for ( j=0; j<attr_cnt; j++ ) {
293 atail->a_vals[j] = vals[k];
295 ber_dupbv( atail->a_vals+j, &vals[k] );
296 vals[k].bv_val = NULL;
297 if ( atail->a_nvals ) {
298 atail->a_nvals[j] = nvals[k];
299 nvals[k].bv_val = NULL;
303 BER_BVZERO( &atail->a_vals[j] );
304 if ( atail->a_nvals ) {
305 BER_BVZERO( &atail->a_nvals[j] );
307 atail->a_nvals = atail->a_vals;
310 if ( i == lines ) break;
313 if ( BER_BVISNULL( &vals[i] ) ) {
314 Debug( LDAP_DEBUG_ANY,
315 "str2entry: attributeType %s #%d: "
317 ad->ad_cname.bv_val, attr_cnt, 0 );
321 if( slapMode & SLAP_TOOL_MODE ) {
323 slap_syntax_validate_func *validate =
324 ad->ad_type->sat_syntax->ssyn_validate;
325 slap_syntax_transform_func *pretty =
326 ad->ad_type->sat_syntax->ssyn_pretty;
329 rc = ordered_value_pretty( ad,
330 &vals[i], &pval, NULL );
332 } else if ( validate ) {
334 * validate value per syntax
336 rc = ordered_value_validate( ad, &vals[i], LDAP_MOD_ADD );
339 Debug( LDAP_DEBUG_ANY,
340 "str2entry: attributeType %s #%d: "
341 "no validator for syntax %s\n",
342 ad->ad_cname.bv_val, attr_cnt,
343 ad->ad_type->sat_syntax->ssyn_oid );
348 Debug( LDAP_DEBUG_ANY,
349 "str2entry: invalid value "
350 "for attributeType %s #%d (syntax %s)\n",
351 ad->ad_cname.bv_val, attr_cnt,
352 ad->ad_type->sat_syntax->ssyn_oid );
357 if ( freeval[i] ) free( vals[i].bv_val );
363 if ( ad->ad_type->sat_equality &&
364 ad->ad_type->sat_equality->smr_normalize )
366 rc = ordered_value_normalize(
367 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
369 ad->ad_type->sat_equality,
370 &vals[i], &nvals[i], NULL );
373 Debug( LDAP_DEBUG_ANY,
374 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
383 atail->a_next = NULL;
384 e->e_attrs = ahead.a_next;
386 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
387 e->e_dn, (unsigned long) e, 0 );
391 for ( i=0; i<lines; i++ ) {
392 if ( freeval[i] ) free( vals[i].bv_val );
393 free( nvals[i].bv_val );
401 #define GRABSIZE BUFSIZ
403 #define MAKE_SPACE( n ) { \
404 while ( ecur + (n) > ebuf + emaxsize ) { \
406 offset = (int) (ecur - ebuf); \
407 ebuf = ch_realloc( ebuf, \
408 emaxsize + GRABSIZE ); \
409 emaxsize += GRABSIZE; \
410 ecur = ebuf + offset; \
427 * In string format, an entry looks like this:
429 * [<attr>: <value>\n]*
435 if ( e->e_dn != NULL ) {
437 tmplen = e->e_name.bv_len;
438 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
439 ldif_sput( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
442 /* put the attributes */
443 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
444 /* put "<type>:[:] <value>" line for each value */
445 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
447 tmplen = a->a_desc->ad_cname.bv_len;
448 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
449 ldif_sput( &ecur, LDIF_PUT_VALUE,
450 a->a_desc->ad_cname.bv_val,
451 bv->bv_val, bv->bv_len );
462 entry_clean( Entry *e )
464 /* free an entry structure */
467 /* e_private must be freed by the caller */
468 assert( e->e_private == NULL );
473 if ( !BER_BVISNULL( &e->e_name ) ) {
474 free( e->e_name.bv_val );
475 BER_BVZERO( &e->e_name );
477 if ( !BER_BVISNULL( &e->e_nname ) ) {
478 free( e->e_nname.bv_val );
479 BER_BVZERO( &e->e_nname );
482 if ( !BER_BVISNULL( &e->e_bv ) ) {
483 free( e->e_bv.bv_val );
484 BER_BVZERO( &e->e_bv );
487 /* free attributes */
489 attrs_free( e->e_attrs );
497 entry_free( Entry *e )
501 ldap_pvt_thread_mutex_lock( &entry_mutex );
502 e->e_private = entry_list;
504 ldap_pvt_thread_mutex_unlock( &entry_mutex );
507 /* These parameters work well on AMD64 */
515 #define STRIDE_FACTOR (STRIDE*STRIPE)
518 entry_prealloc( int num )
520 Entry *e, **prev, *tmp;
526 #if STRIDE_FACTOR > 1
527 /* Round up to our stride factor */
528 num += STRIDE_FACTOR-1;
529 num /= STRIDE_FACTOR;
530 num *= STRIDE_FACTOR;
533 s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Entry));
534 s->next = entry_chunks;
538 for (i=0; i<STRIPE; i++) {
541 for (j=i; j<num; j+= STRIDE) {
543 prev = (Entry **)&e->e_private;
548 entry_list = (Entry *)(s+1);
558 ldap_pvt_thread_mutex_lock( &entry_mutex );
560 entry_prealloc( CHUNK_SIZE );
562 entry_list = e->e_private;
564 ldap_pvt_thread_mutex_unlock( &entry_mutex );
571 * These routines are used only by Backend.
573 * the Entry has three entry points (ways to find things):
575 * by entry e.g., if you already have an entry from the cache
576 * and want to delete it. (really by entry ptr)
577 * by dn e.g., when looking for the base object of a search
578 * by id e.g., for search candidates
580 * these correspond to three different avl trees that are maintained.
584 entry_cmp( Entry *e1, Entry *e2 )
586 return SLAP_PTRCMP( e1, e2 );
590 entry_dn_cmp( const void *v_e1, const void *v_e2 )
592 /* compare their normalized UPPERCASED dn's */
593 const Entry *e1 = v_e1, *e2 = v_e2;
595 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
599 entry_id_cmp( const void *v_e1, const void *v_e2 )
601 const Entry *e1 = v_e1, *e2 = v_e2;
602 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
605 /* This is like a ber_len */
606 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
607 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
610 entry_putlen(unsigned char **buf, ber_len_t len)
612 ber_len_t lenlen = entry_lenlen(len);
615 **buf = (unsigned char) len;
618 **buf = 0x80 | ((unsigned char) lenlen - 1);
619 for (i=lenlen-1; i>0; i--) {
620 (*buf)[i] = (unsigned char) len;
628 entry_getlen(unsigned char **buf)
645 /* Count up the sizes of the components of an entry */
646 void entry_partsize(Entry *e, ber_len_t *plen,
647 int *pnattrs, int *pnvals, int norm)
649 ber_len_t len, dnlen, ndnlen;
650 int i, nat = 0, nval = 0;
653 dnlen = e->e_name.bv_len;
654 len = dnlen + 1; /* trailing NUL byte */
655 len += entry_lenlen(dnlen);
657 ndnlen = e->e_nname.bv_len;
659 len += entry_lenlen(ndnlen);
661 for (a=e->e_attrs; a; a=a->a_next) {
662 /* For AttributeDesc, we only store the attr name */
664 len += a->a_desc->ad_cname.bv_len+1;
665 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
666 for (i=0; a->a_vals[i].bv_val; i++) {
668 len += a->a_vals[i].bv_len + 1;
669 len += entry_lenlen(a->a_vals[i].bv_len);
671 len += entry_lenlen(i);
672 nval++; /* empty berval at end */
673 if (norm && a->a_nvals != a->a_vals) {
674 for (i=0; a->a_nvals[i].bv_val; i++) {
676 len += a->a_nvals[i].bv_len + 1;
677 len += entry_lenlen(a->a_nvals[i].bv_len);
679 len += entry_lenlen(i); /* i nvals */
682 len += entry_lenlen(0); /* 0 nvals */
685 len += entry_lenlen(nat);
686 len += entry_lenlen(nval);
692 /* Add up the size of the entry for a flattened buffer */
693 ber_len_t entry_flatsize(Entry *e, int norm)
698 entry_partsize(e, &len, &nattrs, &nvals, norm);
699 len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
700 (nvals * sizeof(struct berval));
704 /* Flatten an Entry into a buffer. The buffer is filled with just the
705 * strings/bervals of all the entry components. Each field is preceded
706 * by its length, encoded the way ber_put_len works. Every field is NUL
707 * terminated. The entire buffer size is precomputed so that a single
708 * malloc can be performed. The entry size is also recorded,
709 * to aid in entry_decode.
711 int entry_encode(Entry *e, struct berval *bv)
713 ber_len_t len, dnlen, ndnlen;
714 int i, nattrs, nvals;
718 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
719 (long) e->e_id, e->e_dn, 0 );
720 dnlen = e->e_name.bv_len;
721 ndnlen = e->e_nname.bv_len;
723 entry_partsize( e, &len, &nattrs, &nvals, 1 );
726 bv->bv_val = ch_malloc(len);
727 ptr = (unsigned char *)bv->bv_val;
728 entry_putlen(&ptr, nattrs);
729 entry_putlen(&ptr, nvals);
730 entry_putlen(&ptr, dnlen);
731 AC_MEMCPY(ptr, e->e_dn, dnlen);
734 entry_putlen(&ptr, ndnlen);
735 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
739 for (a=e->e_attrs; a; a=a->a_next) {
740 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
741 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
742 a->a_desc->ad_cname.bv_len);
743 ptr += a->a_desc->ad_cname.bv_len;
746 for (i=0; a->a_vals[i].bv_val; i++);
747 entry_putlen(&ptr, i);
748 for (i=0; a->a_vals[i].bv_val; i++) {
749 entry_putlen(&ptr, a->a_vals[i].bv_len);
750 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
751 a->a_vals[i].bv_len);
752 ptr += a->a_vals[i].bv_len;
755 if (a->a_nvals != a->a_vals) {
756 entry_putlen(&ptr, i);
757 for (i=0; a->a_nvals[i].bv_val; i++) {
758 entry_putlen(&ptr, a->a_nvals[i].bv_len);
759 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
760 a->a_nvals[i].bv_len);
761 ptr += a->a_nvals[i].bv_len;
765 entry_putlen(&ptr, 0);
772 /* Retrieve an Entry that was stored using entry_encode above.
773 * First entry_header must be called to decode the size of the entry.
774 * Then a single block of memory must be malloc'd to accomodate the
775 * bervals and the bulk data. Next the bulk data is retrieved from
776 * the DB and parsed by entry_decode.
778 * Note: everything is stored in a single contiguous block, so
779 * you can not free individual attributes or names from this
780 * structure. Attempting to do so will likely corrupt memory.
782 int entry_header(EntryHeader *eh)
784 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
786 eh->nattrs = entry_getlen(&ptr);
788 Debug( LDAP_DEBUG_ANY,
789 "entry_header: attribute count was zero\n", 0, 0, 0);
792 eh->nvals = entry_getlen(&ptr);
794 Debug( LDAP_DEBUG_ANY,
795 "entry_header: value count was zero\n", 0, 0, 0);
798 eh->data = (char *)ptr;
802 #ifdef SLAP_ZONE_ALLOC
803 int entry_decode(EntryHeader *eh, Entry **e, void *ctx)
805 int entry_decode(EntryHeader *eh, Entry **e)
808 int i, j, count, nattrs, nvals;
813 AttributeDescription *ad;
814 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
820 x->e_attrs = attrs_alloc( nattrs );
821 ptr = (unsigned char *)eh->data;
822 i = entry_getlen(&ptr);
823 x->e_name.bv_val = (char *) ptr;
824 x->e_name.bv_len = i;
826 i = entry_getlen(&ptr);
827 x->e_nname.bv_val = (char *) ptr;
828 x->e_nname.bv_len = i;
830 Debug( LDAP_DEBUG_TRACE,
831 "entry_decode: \"%s\"\n",
836 bptr = (BerVarray)eh->bv.bv_val;
838 while ((i = entry_getlen(&ptr))) {
841 bv.bv_val = (char *) ptr;
843 rc = slap_bv2ad( &bv, &ad, &text );
845 if( rc != LDAP_SUCCESS ) {
846 Debug( LDAP_DEBUG_TRACE,
847 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
848 rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
850 if( rc != LDAP_SUCCESS ) {
851 Debug( LDAP_DEBUG_ANY,
852 "<= entry_decode: slap_str2undef_ad(%s): %s\n",
859 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
860 count = j = entry_getlen(&ptr);
864 i = entry_getlen(&ptr);
866 bptr->bv_val = (char *)ptr;
875 j = entry_getlen(&ptr);
879 i = entry_getlen(&ptr);
881 bptr->bv_val = (char *)ptr;
890 a->a_nvals = a->a_vals;
898 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
904 Entry *entry_dup( Entry *e )
911 ber_dupbv( &ret->e_name, &e->e_name );
912 ber_dupbv( &ret->e_nname, &e->e_nname );
913 ret->e_attrs = attrs_dup( e->e_attrs );
914 ret->e_ocflags = e->e_ocflags;
920 /* Duplicates an entry using a single malloc. Saves CPU time, increases
921 * heap usage because a single large malloc is harder to satisfy than
922 * lots of small ones, and the freed space isn't as easily reusable.
924 * Probably not worth using this function.
926 Entry *entry_dup_bv( Entry *e )
933 Attribute *src, *dst;
937 entry_partsize(e, &len, &nattrs, &nvals, 1);
939 ret->e_attrs = attrs_alloc( nattrs );
940 ret->e_ocflags = e->e_ocflags;
941 ret->e_bv.bv_len = len + nvals * sizeof(struct berval);
942 ret->e_bv.bv_val = ch_malloc( ret->e_bv.bv_len );
944 bvl = (struct berval *)ret->e_bv.bv_val;
945 ptr = (char *)(bvl + nvals);
947 ret->e_name.bv_len = e->e_name.bv_len;
948 ret->e_name.bv_val = ptr;
949 AC_MEMCPY( ptr, e->e_name.bv_val, e->e_name.bv_len );
950 ptr += e->e_name.bv_len;
953 ret->e_nname.bv_len = e->e_nname.bv_len;
954 ret->e_nname.bv_val = ptr;
955 AC_MEMCPY( ptr, e->e_nname.bv_val, e->e_nname.bv_len );
956 ptr += e->e_name.bv_len;
960 for (src = e->e_attrs; src; src=src->a_next,dst=dst->a_next ) {
962 dst->a_desc = src->a_desc;
963 dst->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
965 for ( i=0; src->a_vals[i].bv_val; i++ ) {
966 bvl->bv_len = src->a_vals[i].bv_len;
968 AC_MEMCPY( ptr, src->a_vals[i].bv_val, bvl->bv_len );
975 if ( src->a_vals != src->a_nvals ) {
977 for ( i=0; src->a_nvals[i].bv_val; i++ ) {
978 bvl->bv_len = src->a_nvals[i].bv_len;
980 AC_MEMCPY( ptr, src->a_nvals[i].bv_val, bvl->bv_len );