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 #define entry_lenlen(l) ((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
479 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5
481 /* This is like a ber_len */
483 entry_lenlen(ber_len_t len)
498 entry_putlen(unsigned char **buf, ber_len_t len)
500 ber_len_t lenlen = entry_lenlen(len);
503 **buf = (unsigned char) len;
506 **buf = 0x80 | ((unsigned char) lenlen - 1);
507 for (i=lenlen-1; i>0; i--) {
508 (*buf)[i] = (unsigned char) len;
516 entry_getlen(unsigned char **buf)
533 /* Add up the size of the entry for a flattened buffer */
534 void entry_flatsize(Entry *e, ber_len_t *psiz, ber_len_t *plen, int norm)
536 ber_len_t siz = sizeof(Entry);
537 ber_len_t len, dnlen, ndnlen;
541 dnlen = e->e_name.bv_len;
542 len = dnlen + 1; /* trailing NUL byte */
543 len += entry_lenlen(dnlen);
545 ndnlen = e->e_nname.bv_len;
547 len += entry_lenlen(ndnlen);
549 for (a=e->e_attrs; a; a=a->a_next) {
550 /* For AttributeDesc, we only store the attr name */
551 siz += sizeof(Attribute);
552 len += a->a_desc->ad_cname.bv_len+1;
553 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
554 for (i=0; a->a_vals[i].bv_val; i++) {
555 siz += sizeof(struct berval);
556 len += a->a_vals[i].bv_len + 1;
557 len += entry_lenlen(a->a_vals[i].bv_len);
559 len += entry_lenlen(i);
560 siz += sizeof(struct berval); /* empty berval at end */
561 if (norm && a->a_nvals != a->a_vals) {
562 for (i=0; a->a_nvals[i].bv_val; i++) {
563 siz += sizeof(struct berval);
564 len += a->a_nvals[i].bv_len + 1;
565 len += entry_lenlen(a->a_nvals[i].bv_len);
567 len += entry_lenlen(i); /* i nvals */
568 siz += sizeof(struct berval);
570 len += entry_lenlen(0); /* 0 nvals */
573 len += 1; /* NUL byte at end */
574 len += entry_lenlen(siz);
579 /* Flatten an Entry into a buffer. The buffer is filled with just the
580 * strings/bervals of all the entry components. Each field is preceded
581 * by its length, encoded the way ber_put_len works. Every field is NUL
582 * terminated. The entire buffer size is precomputed so that a single
583 * malloc can be performed. The entry size is also recorded,
584 * to aid in entry_decode.
586 int entry_encode(Entry *e, struct berval *bv)
588 ber_len_t siz = sizeof(Entry);
589 ber_len_t len, dnlen, ndnlen;
594 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
595 (long) e->e_id, e->e_dn, 0 );
596 dnlen = e->e_name.bv_len;
597 ndnlen = e->e_nname.bv_len;
599 entry_flatsize( e, &siz, &len, 1 );
602 bv->bv_val = ch_malloc(len);
603 ptr = (unsigned char *)bv->bv_val;
604 entry_putlen(&ptr, siz);
605 entry_putlen(&ptr, dnlen);
606 AC_MEMCPY(ptr, e->e_dn, dnlen);
609 entry_putlen(&ptr, ndnlen);
610 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
614 for (a=e->e_attrs; a; a=a->a_next) {
615 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
616 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
617 a->a_desc->ad_cname.bv_len);
618 ptr += a->a_desc->ad_cname.bv_len;
621 for (i=0; a->a_vals[i].bv_val; i++);
622 entry_putlen(&ptr, i);
623 for (i=0; a->a_vals[i].bv_val; i++) {
624 entry_putlen(&ptr, a->a_vals[i].bv_len);
625 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
626 a->a_vals[i].bv_len);
627 ptr += a->a_vals[i].bv_len;
630 if (a->a_nvals != a->a_vals) {
631 entry_putlen(&ptr, i);
632 for (i=0; a->a_nvals[i].bv_val; i++) {
633 entry_putlen(&ptr, a->a_nvals[i].bv_len);
634 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
635 a->a_nvals[i].bv_len);
636 ptr += a->a_nvals[i].bv_len;
640 entry_putlen(&ptr, 0);
648 /* Retrieve an Entry that was stored using entry_encode above.
649 * We malloc a single block with the size stored above for the Entry
650 * and all of its Attributes. We also must lookup the stored
651 * attribute names to get AttributeDescriptions. To detect if the
652 * attributes of an Entry are later modified, we note that e->e_attr
653 * is always a constant offset from (e).
655 * Note: everything is stored in a single contiguous block, so
656 * you can not free individual attributes or names from this
657 * structure. Attempting to do so will likely corrupt memory.
659 #ifdef SLAP_ZONE_ALLOC
660 int entry_decode(struct berval *bv, Entry **e, void *ctx)
662 int entry_decode(struct berval *bv, Entry **e)
670 AttributeDescription *ad;
671 unsigned char *ptr = (unsigned char *)bv->bv_val;
674 i = entry_getlen(&ptr);
676 Debug( LDAP_DEBUG_ANY,
677 "entry_decode: entry length was zero\n", 0, 0, 0);
680 #ifdef SLAP_ZONE_ALLOC
681 x = slap_zn_calloc(1, i + bv->bv_len, ctx);
682 AC_MEMCPY((char*)x + i, bv->bv_val, bv->bv_len);
683 bv->bv_val = (char*)x + i;
684 ptr = (unsigned char *)bv->bv_val;
685 i = entry_getlen(&ptr);
689 i = entry_getlen(&ptr);
690 x->e_name.bv_val = (char *) ptr;
691 x->e_name.bv_len = i;
693 i = entry_getlen(&ptr);
694 x->e_nname.bv_val = (char *) ptr;
695 x->e_nname.bv_len = i;
697 Debug( LDAP_DEBUG_TRACE,
698 "entry_decode: \"%s\"\n",
702 /* A valid entry must have at least one attr, so this
703 * pointer can never be NULL
705 x->e_attrs = (Attribute *)(x+1);
706 bptr = (BerVarray)x->e_attrs;
709 while ((i = entry_getlen(&ptr))) {
712 bv.bv_val = (char *) ptr;
714 a->a_next = (Attribute *)bptr;
716 a = (Attribute *)bptr;
718 rc = slap_bv2ad( &bv, &ad, &text );
720 if( rc != LDAP_SUCCESS ) {
721 Debug( LDAP_DEBUG_TRACE,
722 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
723 rc = slap_bv2undef_ad( &bv, &ad, &text );
725 if( rc != LDAP_SUCCESS ) {
726 Debug( LDAP_DEBUG_ANY,
727 "<= entry_decode: str2undef_ad(%s): %s\n",
734 bptr = (BerVarray)(a+1);
737 #ifdef LDAP_COMP_MATCH
738 a->a_comp_data = NULL;
740 count = j = entry_getlen(&ptr);
743 i = entry_getlen(&ptr);
745 bptr->bv_val = (char *)ptr;
754 j = entry_getlen(&ptr);
758 i = entry_getlen(&ptr);
760 bptr->bv_val = (char *)ptr;
769 a->a_nvals = a->a_vals;
773 if (a) a->a_next = NULL;
774 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
780 Entry *entry_dup( Entry *e )
784 ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
787 ber_dupbv( &ret->e_name, &e->e_name );
788 ber_dupbv( &ret->e_nname, &e->e_nname );
789 ret->e_attrs = attrs_dup( e->e_attrs );
790 ret->e_ocflags = e->e_ocflags;
791 ret->e_bv.bv_val = NULL;
792 ret->e_bv.bv_len = 0;
793 ret->e_private = NULL;