1 /* entry.c - routines for dealing with entries */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
173 Debug( LDAP_DEBUG_TRACE,
174 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
178 if ( type[i].bv_len == dn_bv.bv_len &&
179 strcasecmp( type[i].bv_val, dn_bv.bv_val ) == 0 ) {
181 if ( e->e_dn != NULL ) {
182 Debug( LDAP_DEBUG_ANY, "str2entry: "
183 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
184 (long) e->e_id, e->e_dn, vals[i].bv_val );
188 rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
189 if( rc != LDAP_SUCCESS ) {
190 Debug( LDAP_DEBUG_ANY, "str2entry: "
191 "entry %ld has invalid DN \"%s\"\n",
192 (long) e->e_id, vals[i].bv_val, 0 );
195 if ( freeval[i] ) free( vals[i].bv_val );
196 vals[i].bv_val = NULL;
203 /* check to make sure there was a dn: line */
204 if ( BER_BVISNULL( &e->e_name )) {
205 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
206 (long) e->e_id, 0, 0 );
210 /* Make sure all attributes with multiple values are contiguous */
216 for (i=0; i<lines; i++) {
217 for ( j=i+1; j<lines; j++ ) {
218 if ( bvmatch( type+i, type+j )) {
219 /* out of order, move intervening attributes down */
223 for ( k=j; k>i; k-- ) {
226 freeval[k] = freeval[k-1];
239 for ( i=0; i<=lines; i++ ) {
241 if ( !ad || ( i<lines && !bvmatch( type+i, &ad->ad_cname ))) {
243 rc = slap_bv2ad( type+i, &ad, &text );
245 if( rc != LDAP_SUCCESS ) {
246 Debug( slapMode & SLAP_TOOL_MODE
247 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
248 "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
249 if( slapMode & SLAP_TOOL_MODE ) {
253 rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
254 if( rc != LDAP_SUCCESS ) {
255 Debug( LDAP_DEBUG_ANY,
256 "<= str2entry: slap_str2undef_ad(%s): %s\n",
257 type[i].bv_val, text, 0 );
263 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
265 atail->a_next = attr_alloc( NULL );
266 atail = atail->a_next;
268 atail->a_desc = ad_prev;
269 atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
270 if( ad_prev->ad_type->sat_equality &&
271 ad_prev->ad_type->sat_equality->smr_normalize )
272 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
274 atail->a_nvals = NULL;
276 for ( j=0; j<attr_cnt; j++ ) {
278 atail->a_vals[j] = vals[k];
280 ber_dupbv( atail->a_vals+j, &vals[k] );
281 vals[k].bv_val = NULL;
282 if ( atail->a_nvals ) {
283 atail->a_nvals[j] = nvals[k];
284 nvals[k].bv_val = NULL;
288 BER_BVZERO( &atail->a_vals[j] );
289 if ( atail->a_nvals ) {
290 BER_BVZERO( &atail->a_nvals[j] );
292 atail->a_nvals = atail->a_vals;
295 if ( i == lines ) break;
298 if( slapMode & SLAP_TOOL_MODE ) {
300 slap_syntax_validate_func *validate =
301 ad->ad_type->sat_syntax->ssyn_validate;
302 slap_syntax_transform_func *pretty =
303 ad->ad_type->sat_syntax->ssyn_pretty;
306 rc = ordered_value_pretty( ad,
307 &vals[i], &pval, NULL );
309 } else if ( validate ) {
311 * validate value per syntax
313 rc = ordered_value_validate( ad, &vals[i], LDAP_MOD_ADD );
316 Debug( LDAP_DEBUG_ANY,
317 "str2entry: attributeType %s #%d: "
318 "no validator for syntax %s\n",
319 ad->ad_cname.bv_val, attr_cnt,
320 ad->ad_type->sat_syntax->ssyn_oid );
325 Debug( LDAP_DEBUG_ANY,
326 "str2entry: invalid value "
327 "for attributeType %s #%d (syntax %s)\n",
328 ad->ad_cname.bv_val, attr_cnt,
329 ad->ad_type->sat_syntax->ssyn_oid );
334 if ( freeval[i] ) free( vals[i].bv_val );
340 if ( ad->ad_type->sat_equality &&
341 ad->ad_type->sat_equality->smr_normalize )
343 rc = ordered_value_normalize(
344 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
346 ad->ad_type->sat_equality,
347 &vals[i], &nvals[i], NULL );
350 Debug( LDAP_DEBUG_ANY,
351 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
360 atail->a_next = NULL;
361 e->e_attrs = ahead.a_next;
363 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
364 e->e_dn, (unsigned long) e, 0 );
368 for ( i=0; i<lines; i++ ) {
369 if ( freeval[i] ) free( vals[i].bv_val );
370 free( nvals[i].bv_val );
378 #define GRABSIZE BUFSIZ
380 #define MAKE_SPACE( n ) { \
381 while ( ecur + (n) > ebuf + emaxsize ) { \
383 offset = (int) (ecur - ebuf); \
384 ebuf = ch_realloc( ebuf, \
385 emaxsize + GRABSIZE ); \
386 emaxsize += GRABSIZE; \
387 ecur = ebuf + offset; \
404 * In string format, an entry looks like this:
406 * [<attr>: <value>\n]*
412 if ( e->e_dn != NULL ) {
414 tmplen = e->e_name.bv_len;
415 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
416 ldif_sput( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
419 /* put the attributes */
420 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
421 /* put "<type>:[:] <value>" line for each value */
422 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
424 tmplen = a->a_desc->ad_cname.bv_len;
425 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
426 ldif_sput( &ecur, LDIF_PUT_VALUE,
427 a->a_desc->ad_cname.bv_val,
428 bv->bv_val, bv->bv_len );
439 entry_clean( Entry *e )
441 /* free an entry structure */
444 /* e_private must be freed by the caller */
445 assert( e->e_private == NULL );
448 if ( !BER_BVISNULL( &e->e_name ) ) {
449 free( e->e_name.bv_val );
451 if ( !BER_BVISNULL( &e->e_nname ) ) {
452 free( e->e_nname.bv_val );
455 if ( !BER_BVISNULL( &e->e_bv ) ) {
456 free( e->e_bv.bv_val );
459 /* free attributes */
460 attrs_free( e->e_attrs );
462 memset(e, 0, sizeof(Entry));
466 entry_free( Entry *e )
470 ldap_pvt_thread_mutex_lock( &entry_mutex );
471 e->e_private = entry_list;
473 ldap_pvt_thread_mutex_unlock( &entry_mutex );
477 entry_prealloc( int num )
484 s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Entry));
485 s->next = entry_chunks;
489 for ( ;num>1; num--) {
493 e->e_private = entry_list;
494 entry_list = (Entry *)(s+1);
504 ldap_pvt_thread_mutex_lock( &entry_mutex );
506 entry_prealloc( CHUNK_SIZE );
508 entry_list = e->e_private;
510 ldap_pvt_thread_mutex_unlock( &entry_mutex );
517 * These routines are used only by Backend.
519 * the Entry has three entry points (ways to find things):
521 * by entry e.g., if you already have an entry from the cache
522 * and want to delete it. (really by entry ptr)
523 * by dn e.g., when looking for the base object of a search
524 * by id e.g., for search candidates
526 * these correspond to three different avl trees that are maintained.
530 entry_cmp( Entry *e1, Entry *e2 )
532 return SLAP_PTRCMP( e1, e2 );
536 entry_dn_cmp( const void *v_e1, const void *v_e2 )
538 /* compare their normalized UPPERCASED dn's */
539 const Entry *e1 = v_e1, *e2 = v_e2;
541 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
545 entry_id_cmp( const void *v_e1, const void *v_e2 )
547 const Entry *e1 = v_e1, *e2 = v_e2;
548 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
551 /* This is like a ber_len */
552 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
553 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
556 entry_putlen(unsigned char **buf, ber_len_t len)
558 ber_len_t lenlen = entry_lenlen(len);
561 **buf = (unsigned char) len;
564 **buf = 0x80 | ((unsigned char) lenlen - 1);
565 for (i=lenlen-1; i>0; i--) {
566 (*buf)[i] = (unsigned char) len;
574 entry_getlen(unsigned char **buf)
591 /* Count up the sizes of the components of an entry */
592 void entry_partsize(Entry *e, ber_len_t *plen,
593 int *pnattrs, int *pnvals, int norm)
595 ber_len_t len, dnlen, ndnlen;
596 int i, nat = 0, nval = 0;
599 dnlen = e->e_name.bv_len;
600 len = dnlen + 1; /* trailing NUL byte */
601 len += entry_lenlen(dnlen);
603 ndnlen = e->e_nname.bv_len;
605 len += entry_lenlen(ndnlen);
607 for (a=e->e_attrs; a; a=a->a_next) {
608 /* For AttributeDesc, we only store the attr name */
610 len += a->a_desc->ad_cname.bv_len+1;
611 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
612 for (i=0; a->a_vals[i].bv_val; i++) {
614 len += a->a_vals[i].bv_len + 1;
615 len += entry_lenlen(a->a_vals[i].bv_len);
617 len += entry_lenlen(i);
618 nval++; /* empty berval at end */
619 if (norm && a->a_nvals != a->a_vals) {
620 for (i=0; a->a_nvals[i].bv_val; i++) {
622 len += a->a_nvals[i].bv_len + 1;
623 len += entry_lenlen(a->a_nvals[i].bv_len);
625 len += entry_lenlen(i); /* i nvals */
628 len += entry_lenlen(0); /* 0 nvals */
631 len += entry_lenlen(nat);
632 len += entry_lenlen(nval);
638 /* Add up the size of the entry for a flattened buffer */
639 ber_len_t entry_flatsize(Entry *e, int norm)
644 entry_partsize(e, &len, &nattrs, &nvals, norm);
645 len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
646 (nvals * sizeof(struct berval));
650 /* Flatten an Entry into a buffer. The buffer is filled with just the
651 * strings/bervals of all the entry components. Each field is preceded
652 * by its length, encoded the way ber_put_len works. Every field is NUL
653 * terminated. The entire buffer size is precomputed so that a single
654 * malloc can be performed. The entry size is also recorded,
655 * to aid in entry_decode.
657 int entry_encode(Entry *e, struct berval *bv)
659 ber_len_t len, dnlen, ndnlen;
660 int i, nattrs, nvals;
664 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
665 (long) e->e_id, e->e_dn, 0 );
666 dnlen = e->e_name.bv_len;
667 ndnlen = e->e_nname.bv_len;
669 entry_partsize( e, &len, &nattrs, &nvals, 1 );
672 bv->bv_val = ch_malloc(len);
673 ptr = (unsigned char *)bv->bv_val;
674 entry_putlen(&ptr, nattrs);
675 entry_putlen(&ptr, nvals);
676 entry_putlen(&ptr, dnlen);
677 AC_MEMCPY(ptr, e->e_dn, dnlen);
680 entry_putlen(&ptr, ndnlen);
681 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
685 for (a=e->e_attrs; a; a=a->a_next) {
686 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
687 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
688 a->a_desc->ad_cname.bv_len);
689 ptr += a->a_desc->ad_cname.bv_len;
692 for (i=0; a->a_vals[i].bv_val; i++);
693 entry_putlen(&ptr, i);
694 for (i=0; a->a_vals[i].bv_val; i++) {
695 entry_putlen(&ptr, a->a_vals[i].bv_len);
696 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
697 a->a_vals[i].bv_len);
698 ptr += a->a_vals[i].bv_len;
701 if (a->a_nvals != a->a_vals) {
702 entry_putlen(&ptr, i);
703 for (i=0; a->a_nvals[i].bv_val; i++) {
704 entry_putlen(&ptr, a->a_nvals[i].bv_len);
705 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
706 a->a_nvals[i].bv_len);
707 ptr += a->a_nvals[i].bv_len;
711 entry_putlen(&ptr, 0);
718 /* Retrieve an Entry that was stored using entry_encode above.
719 * First entry_header must be called to decode the size of the entry.
720 * Then a single block of memory must be malloc'd to accomodate the
721 * bervals and the bulk data. Next the bulk data is retrieved from
722 * the DB and parsed by entry_decode.
724 * Note: everything is stored in a single contiguous block, so
725 * you can not free individual attributes or names from this
726 * structure. Attempting to do so will likely corrupt memory.
728 int entry_header(EntryHeader *eh)
730 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
732 eh->nattrs = entry_getlen(&ptr);
734 Debug( LDAP_DEBUG_ANY,
735 "entry_header: attribute count was zero\n", 0, 0, 0);
738 eh->nvals = entry_getlen(&ptr);
740 Debug( LDAP_DEBUG_ANY,
741 "entry_header: value count was zero\n", 0, 0, 0);
744 eh->data = (char *)ptr;
748 #ifdef SLAP_ZONE_ALLOC
749 int entry_decode(EntryHeader *eh, Entry **e, void *ctx)
751 int entry_decode(EntryHeader *eh, Entry **e)
754 int i, j, count, nattrs, nvals;
759 AttributeDescription *ad;
760 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
766 x->e_attrs = attrs_alloc( nattrs );
767 ptr = (unsigned char *)eh->data;
768 i = entry_getlen(&ptr);
769 x->e_name.bv_val = (char *) ptr;
770 x->e_name.bv_len = i;
772 i = entry_getlen(&ptr);
773 x->e_nname.bv_val = (char *) ptr;
774 x->e_nname.bv_len = i;
776 Debug( LDAP_DEBUG_TRACE,
777 "entry_decode: \"%s\"\n",
782 bptr = (BerVarray)eh->bv.bv_val;
784 while ((i = entry_getlen(&ptr))) {
787 bv.bv_val = (char *) ptr;
789 rc = slap_bv2ad( &bv, &ad, &text );
791 if( rc != LDAP_SUCCESS ) {
792 Debug( LDAP_DEBUG_TRACE,
793 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
794 rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
796 if( rc != LDAP_SUCCESS ) {
797 Debug( LDAP_DEBUG_ANY,
798 "<= entry_decode: slap_str2undef_ad(%s): %s\n",
805 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
806 count = j = entry_getlen(&ptr);
810 i = entry_getlen(&ptr);
812 bptr->bv_val = (char *)ptr;
821 j = entry_getlen(&ptr);
825 i = entry_getlen(&ptr);
827 bptr->bv_val = (char *)ptr;
836 a->a_nvals = a->a_vals;
844 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
850 Entry *entry_dup( Entry *e )
857 ber_dupbv( &ret->e_name, &e->e_name );
858 ber_dupbv( &ret->e_nname, &e->e_nname );
859 ret->e_attrs = attrs_dup( e->e_attrs );
860 ret->e_ocflags = e->e_ocflags;