1 /* entry.c - routines for dealing with entries */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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 unsigned char *ebuf; /* buf returned by entry2str */
40 static unsigned 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
50 static const struct berval dn_bv = BER_BVC("dn");
52 int entry_destroy(void)
54 if ( ebuf ) free( ebuf );
65 return str2entry2( s, 1 );
69 str2entry2( char *s, int checkvals )
73 struct berval *nvalsp;
74 struct berval *type, *vals, *nvals;
76 AttributeDescription *ad, *ad_prev;
81 Attribute ahead, *atail;
84 * LDIF is used as the string format.
85 * An entry looks like this:
88 * [<attr>:[:] <value>\n]
89 * [<tab><continuedvalue>\n]*
92 * If a double colon is used after a type, it means the
93 * following value is encoded as a base 64 string. This
94 * happens if the value contains a non-printing character
98 Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
99 s ? s : "NULL", 0, 0 );
101 /* initialize reader/writer lock */
102 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
105 Debug( LDAP_DEBUG_ANY,
106 "<= str2entry NULL (entry allocation failed)\n",
111 /* initialize entry */
114 /* dn + attributes */
122 lines = ldif_countlines( s );
123 type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
125 nvals = vals+lines+1;
126 freeval = (char *)(nvals+lines+1);
129 /* parse into individual values, record DN */
130 while ( (s = ldif_getline( &next )) != NULL ) {
132 if ( *s == '\n' || *s == '\0' ) {
137 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
140 Debug( LDAP_DEBUG_TRACE,
141 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
145 if ( type[i].bv_len == dn_bv.bv_len &&
146 strcasecmp( type[i].bv_val, dn_bv.bv_val ) == 0 ) {
148 if ( e->e_dn != NULL ) {
149 Debug( LDAP_DEBUG_ANY, "str2entry: "
150 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
151 (long) e->e_id, e->e_dn, vals[i].bv_val );
155 rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
156 if( rc != LDAP_SUCCESS ) {
157 Debug( LDAP_DEBUG_ANY, "str2entry: "
158 "entry %ld has invalid DN \"%s\"\n",
159 (long) e->e_id, vals[i].bv_val, 0 );
162 if ( freeval[i] ) free( vals[i].bv_val );
163 vals[i].bv_val = NULL;
170 /* check to make sure there was a dn: line */
171 if ( BER_BVISNULL( &e->e_name )) {
172 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
173 (long) e->e_id, 0, 0 );
177 /* Make sure all attributes with multiple values are contiguous */
183 for (i=0; i<lines; i++) {
185 for ( j=i+1; j<lines; j++ ) {
186 if ( bvmatch( type+i, type+j )) {
187 /* out of order, move intervening attributes down */
192 for ( l=j; l>k; l-- ) {
195 freeval[l] = freeval[l-1];
207 for ( i=0; i<=lines; i++ ) {
209 if ( !ad || ( i<lines && !bvmatch( type+i, &ad->ad_cname ))) {
211 rc = slap_bv2ad( type+i, &ad, &text );
213 if( rc != LDAP_SUCCESS ) {
214 Debug( slapMode & SLAP_TOOL_MODE
215 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
216 "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
217 if( slapMode & SLAP_TOOL_MODE ) {
221 rc = slap_bv2undef_ad( type+i, &ad, &text );
222 if( rc != LDAP_SUCCESS ) {
223 Debug( LDAP_DEBUG_ANY,
224 "<= str2entry: str2undef_ad(%s): %s\n",
225 type[i].bv_val, text, 0 );
231 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
233 atail->a_next = (Attribute *) ch_malloc( sizeof(Attribute) );
234 atail = atail->a_next;
235 atail->a_desc = ad_prev;
236 atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
237 if( ad_prev->ad_type->sat_equality &&
238 ad_prev->ad_type->sat_equality->smr_normalize )
239 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
241 atail->a_nvals = NULL;
243 for ( j=0; j<attr_cnt; j++ ) {
245 atail->a_vals[j] = vals[k];
247 ber_dupbv( atail->a_vals+j, &vals[k] );
248 vals[k].bv_val = NULL;
249 if ( atail->a_nvals ) {
250 atail->a_nvals[j] = nvals[k];
251 nvals[k].bv_val = NULL;
255 BER_BVZERO( &atail->a_vals[j] );
256 if ( atail->a_nvals ) {
257 BER_BVZERO( &atail->a_nvals[j] );
259 atail->a_nvals = atail->a_vals;
262 if ( i == lines ) break;
265 if( slapMode & SLAP_TOOL_MODE ) {
267 slap_syntax_validate_func *validate =
268 ad->ad_type->sat_syntax->ssyn_validate;
269 slap_syntax_transform_func *pretty =
270 ad->ad_type->sat_syntax->ssyn_pretty;
273 rc = pretty( ad->ad_type->sat_syntax,
274 &vals[i], &pval, NULL );
276 } else if( validate ) {
278 * validate value per syntax
280 rc = validate( ad->ad_type->sat_syntax, &vals[i] );
283 Debug( LDAP_DEBUG_ANY,
284 "str2entry: attributeType %s #%d: "
285 "no validator for syntax %s\n",
286 ad->ad_cname.bv_val, attr_cnt,
287 ad->ad_type->sat_syntax->ssyn_oid );
292 Debug( LDAP_DEBUG_ANY,
293 "str2entry: invalid value "
294 "for attributeType %s #%d (syntax %s)\n",
295 ad->ad_cname.bv_val, attr_cnt,
296 ad->ad_type->sat_syntax->ssyn_oid );
301 if ( freeval[i] ) free( vals[i].bv_val );
307 if( ad->ad_type->sat_equality &&
308 ad->ad_type->sat_equality->smr_normalize )
310 rc = ad->ad_type->sat_equality->smr_normalize(
311 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
312 ad->ad_type->sat_syntax,
313 ad->ad_type->sat_equality,
314 &vals[i], &nvals[i], NULL );
317 Debug( LDAP_DEBUG_ANY,
318 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
327 atail->a_next = NULL;
328 e->e_attrs = ahead.a_next;
330 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
331 e->e_dn, (unsigned long) e, 0 );
335 for ( i=0; i<lines; i++ ) {
336 if ( freeval[i] ) free( vals[i].bv_val );
337 free( nvals[i].bv_val );
345 #define GRABSIZE BUFSIZ
347 #define MAKE_SPACE( n ) { \
348 while ( ecur + (n) > ebuf + emaxsize ) { \
350 offset = (int) (ecur - ebuf); \
351 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
352 emaxsize + GRABSIZE ); \
353 emaxsize += GRABSIZE; \
354 ecur = ebuf + offset; \
371 * In string format, an entry looks like this:
373 * [<attr>: <value>\n]*
379 if ( e->e_dn != NULL ) {
381 tmplen = e->e_name.bv_len;
382 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
383 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
386 /* put the attributes */
387 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
388 /* put "<type>:[:] <value>" line for each value */
389 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
391 tmplen = a->a_desc->ad_cname.bv_len;
392 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
393 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
394 a->a_desc->ad_cname.bv_val,
395 bv->bv_val, bv->bv_len );
402 return( (char *) ebuf );
406 entry_clean( Entry *e )
408 /* free an entry structure */
411 /* e_private must be freed by the caller */
412 assert( e->e_private == NULL );
416 if ( !BER_BVISNULL( &e->e_name ) ) {
417 free( e->e_name.bv_val );
418 BER_BVZERO( &e->e_name );
420 if ( !BER_BVISNULL( &e->e_nname ) ) {
421 free( e->e_nname.bv_val );
422 BER_BVZERO( &e->e_nname );
425 if ( !BER_BVISNULL( &e->e_bv ) ) {
426 free( e->e_bv.bv_val );
427 BER_BVZERO( &e->e_bv );
430 /* free attributes */
431 attrs_free( e->e_attrs );
436 entry_free( Entry *e )
444 * These routines are used only by Backend.
446 * the Entry has three entry points (ways to find things):
448 * by entry e.g., if you already have an entry from the cache
449 * and want to delete it. (really by entry ptr)
450 * by dn e.g., when looking for the base object of a search
451 * by id e.g., for search candidates
453 * these correspond to three different avl trees that are maintained.
457 entry_cmp( Entry *e1, Entry *e2 )
459 return SLAP_PTRCMP( e1, e2 );
463 entry_dn_cmp( const void *v_e1, const void *v_e2 )
465 /* compare their normalized UPPERCASED dn's */
466 const Entry *e1 = v_e1, *e2 = v_e2;
468 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
472 entry_id_cmp( const void *v_e1, const void *v_e2 )
474 const Entry *e1 = v_e1, *e2 = v_e2;
475 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
478 /* This is like a ber_len */
479 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
480 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
483 entry_putlen(unsigned char **buf, ber_len_t len)
485 ber_len_t lenlen = entry_lenlen(len);
488 **buf = (unsigned char) len;
491 **buf = 0x80 | ((unsigned char) lenlen - 1);
492 for (i=lenlen-1; i>0; i--) {
493 (*buf)[i] = (unsigned char) len;
501 entry_getlen(unsigned char **buf)
518 /* Count up the sizes of the components of an entry */
519 void entry_partsize(Entry *e, ber_len_t *plen,
520 int *pnattrs, int *pnvals, int norm)
522 ber_len_t len, dnlen, ndnlen;
523 int i, nat = 0, nval = 0;
526 dnlen = e->e_name.bv_len;
527 len = dnlen + 1; /* trailing NUL byte */
528 len += entry_lenlen(dnlen);
530 ndnlen = e->e_nname.bv_len;
532 len += entry_lenlen(ndnlen);
534 for (a=e->e_attrs; a; a=a->a_next) {
535 /* For AttributeDesc, we only store the attr name */
537 len += a->a_desc->ad_cname.bv_len+1;
538 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
539 for (i=0; a->a_vals[i].bv_val; i++) {
541 len += a->a_vals[i].bv_len + 1;
542 len += entry_lenlen(a->a_vals[i].bv_len);
544 len += entry_lenlen(i);
545 nval++; /* empty berval at end */
546 if (norm && a->a_nvals != a->a_vals) {
547 for (i=0; a->a_nvals[i].bv_val; i++) {
549 len += a->a_nvals[i].bv_len + 1;
550 len += entry_lenlen(a->a_nvals[i].bv_len);
552 len += entry_lenlen(i); /* i nvals */
555 len += entry_lenlen(0); /* 0 nvals */
558 len += entry_lenlen(0); /* NUL byte at end */
559 len += entry_lenlen(nat);
560 len += entry_lenlen(nval);
566 /* Add up the size of the entry for a flattened buffer */
567 ber_len_t entry_flatsize(Entry *e, int norm)
572 entry_partsize(e, &len, &nattrs, &nvals, norm);
573 len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
574 (nvals * sizeof(struct berval));
578 /* Flatten an Entry into a buffer. The buffer is filled with just the
579 * strings/bervals of all the entry components. Each field is preceded
580 * by its length, encoded the way ber_put_len works. Every field is NUL
581 * terminated. The entire buffer size is precomputed so that a single
582 * malloc can be performed. The entry size is also recorded,
583 * to aid in entry_decode.
585 int entry_encode(Entry *e, struct berval *bv)
587 ber_len_t len, dnlen, ndnlen;
588 int i, nattrs, nvals;
592 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
593 (long) e->e_id, e->e_dn, 0 );
594 dnlen = e->e_name.bv_len;
595 ndnlen = e->e_nname.bv_len;
597 entry_partsize( e, &len, &nattrs, &nvals, 1 );
600 bv->bv_val = ch_malloc(len);
601 ptr = (unsigned char *)bv->bv_val;
602 entry_putlen(&ptr, nattrs);
603 entry_putlen(&ptr, nvals);
604 entry_putlen(&ptr, dnlen);
605 AC_MEMCPY(ptr, e->e_dn, dnlen);
608 entry_putlen(&ptr, ndnlen);
609 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
613 for (a=e->e_attrs; a; a=a->a_next) {
614 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
615 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
616 a->a_desc->ad_cname.bv_len);
617 ptr += a->a_desc->ad_cname.bv_len;
620 for (i=0; a->a_vals[i].bv_val; i++);
621 entry_putlen(&ptr, i);
622 for (i=0; a->a_vals[i].bv_val; i++) {
623 entry_putlen(&ptr, a->a_vals[i].bv_len);
624 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
625 a->a_vals[i].bv_len);
626 ptr += a->a_vals[i].bv_len;
629 if (a->a_nvals != a->a_vals) {
630 entry_putlen(&ptr, i);
631 for (i=0; a->a_nvals[i].bv_val; i++) {
632 entry_putlen(&ptr, a->a_nvals[i].bv_len);
633 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
634 a->a_nvals[i].bv_len);
635 ptr += a->a_nvals[i].bv_len;
639 entry_putlen(&ptr, 0);
646 /* Retrieve an Entry that was stored using entry_encode above.
647 * We malloc a single block with the size stored above for the Entry
648 * and all of its Attributes. We also must lookup the stored
649 * attribute names to get AttributeDescriptions. To detect if the
650 * attributes of an Entry are later modified, we note that e->e_attr
651 * is always a constant offset from (e).
653 * Note: everything is stored in a single contiguous block, so
654 * you can not free individual attributes or names from this
655 * structure. Attempting to do so will likely corrupt memory.
657 #ifdef SLAP_ZONE_ALLOC
658 int entry_decode(struct berval *bv, Entry **e, void *ctx)
660 int entry_decode(struct berval *bv, Entry **e)
663 int i, j, count, nattrs, nvals;
668 AttributeDescription *ad;
669 unsigned char *ptr = (unsigned char *)bv->bv_val;
672 nattrs = entry_getlen(&ptr);
674 Debug( LDAP_DEBUG_ANY,
675 "entry_decode: attribute count was zero\n", 0, 0, 0);
678 nvals = entry_getlen(&ptr);
680 Debug( LDAP_DEBUG_ANY,
681 "entry_decode: value count was zero\n", 0, 0, 0);
684 i = sizeof(Entry) + (nattrs * sizeof(Attribute)) +
685 (nvals * sizeof(struct berval));
686 #ifdef SLAP_ZONE_ALLOC
687 x = slap_zn_calloc(1, i + bv->bv_len, ctx);
688 AC_MEMCPY((char*)x + i, bv->bv_val, bv->bv_len);
689 bv->bv_val = (char*)x + i;
690 ptr = (unsigned char *)bv->bv_val;
691 /* pointer is reset, now advance past nattrs and nvals again */
697 i = entry_getlen(&ptr);
698 x->e_name.bv_val = (char *) ptr;
699 x->e_name.bv_len = i;
701 i = entry_getlen(&ptr);
702 x->e_nname.bv_val = (char *) ptr;
703 x->e_nname.bv_len = i;
705 Debug( LDAP_DEBUG_TRACE,
706 "entry_decode: \"%s\"\n",
710 /* A valid entry must have at least one attr, so this
711 * pointer can never be NULL
713 x->e_attrs = (Attribute *)(x+1);
714 bptr = (BerVarray)x->e_attrs;
717 while ((i = entry_getlen(&ptr))) {
720 bv.bv_val = (char *) ptr;
722 a->a_next = (Attribute *)bptr;
724 a = (Attribute *)bptr;
726 rc = slap_bv2ad( &bv, &ad, &text );
728 if( rc != LDAP_SUCCESS ) {
729 Debug( LDAP_DEBUG_TRACE,
730 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
731 rc = slap_bv2undef_ad( &bv, &ad, &text );
733 if( rc != LDAP_SUCCESS ) {
734 Debug( LDAP_DEBUG_ANY,
735 "<= entry_decode: str2undef_ad(%s): %s\n",
742 bptr = (BerVarray)(a+1);
745 #ifdef LDAP_COMP_MATCH
746 a->a_comp_data = NULL;
748 count = j = entry_getlen(&ptr);
751 i = entry_getlen(&ptr);
753 bptr->bv_val = (char *)ptr;
762 j = entry_getlen(&ptr);
766 i = entry_getlen(&ptr);
768 bptr->bv_val = (char *)ptr;
777 a->a_nvals = a->a_vals;
784 if (a) a->a_next = NULL;
785 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
791 Entry *entry_dup( Entry *e )
795 ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
798 ber_dupbv( &ret->e_name, &e->e_name );
799 ber_dupbv( &ret->e_nname, &e->e_nname );
800 ret->e_attrs = attrs_dup( e->e_attrs );
801 ret->e_ocflags = e->e_ocflags;
802 ret->e_bv.bv_val = NULL;
803 ret->e_bv.bv_len = 0;
804 ret->e_private = NULL;