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++) {
184 for ( j=i+1; j<lines; j++ ) {
185 if ( bvmatch( type+i, type+j )) {
186 /* out of order, move intervening attributes down */
190 for ( k=j; k>i; k-- ) {
193 freeval[k] = freeval[k-1];
206 for ( i=0; i<=lines; i++ ) {
208 if ( !ad || ( i<lines && !bvmatch( type+i, &ad->ad_cname ))) {
210 rc = slap_bv2ad( type+i, &ad, &text );
212 if( rc != LDAP_SUCCESS ) {
213 Debug( slapMode & SLAP_TOOL_MODE
214 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
215 "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
216 if( slapMode & SLAP_TOOL_MODE ) {
220 rc = slap_bv2undef_ad( type+i, &ad, &text );
221 if( rc != LDAP_SUCCESS ) {
222 Debug( LDAP_DEBUG_ANY,
223 "<= str2entry: str2undef_ad(%s): %s\n",
224 type[i].bv_val, text, 0 );
230 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
232 atail->a_next = (Attribute *) ch_malloc( sizeof(Attribute) );
233 atail = atail->a_next;
234 atail->a_desc = ad_prev;
235 atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
236 if( ad_prev->ad_type->sat_equality &&
237 ad_prev->ad_type->sat_equality->smr_normalize )
238 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
240 atail->a_nvals = NULL;
242 for ( j=0; j<attr_cnt; j++ ) {
244 atail->a_vals[j] = vals[k];
246 ber_dupbv( atail->a_vals+j, &vals[k] );
247 vals[k].bv_val = NULL;
248 if ( atail->a_nvals ) {
249 atail->a_nvals[j] = nvals[k];
250 nvals[k].bv_val = NULL;
254 BER_BVZERO( &atail->a_vals[j] );
255 if ( atail->a_nvals ) {
256 BER_BVZERO( &atail->a_nvals[j] );
258 atail->a_nvals = atail->a_vals;
261 if ( i == lines ) break;
264 if( slapMode & SLAP_TOOL_MODE ) {
266 slap_syntax_validate_func *validate =
267 ad->ad_type->sat_syntax->ssyn_validate;
268 slap_syntax_transform_func *pretty =
269 ad->ad_type->sat_syntax->ssyn_pretty;
272 rc = pretty( ad->ad_type->sat_syntax,
273 &vals[i], &pval, NULL );
275 } else if( validate ) {
277 * validate value per syntax
279 rc = validate( ad->ad_type->sat_syntax, &vals[i] );
282 Debug( LDAP_DEBUG_ANY,
283 "str2entry: attributeType %s #%d: "
284 "no validator for syntax %s\n",
285 ad->ad_cname.bv_val, attr_cnt,
286 ad->ad_type->sat_syntax->ssyn_oid );
291 Debug( LDAP_DEBUG_ANY,
292 "str2entry: invalid value "
293 "for attributeType %s #%d (syntax %s)\n",
294 ad->ad_cname.bv_val, attr_cnt,
295 ad->ad_type->sat_syntax->ssyn_oid );
300 if ( freeval[i] ) free( vals[i].bv_val );
306 if( ad->ad_type->sat_equality &&
307 ad->ad_type->sat_equality->smr_normalize )
309 rc = ad->ad_type->sat_equality->smr_normalize(
310 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
311 ad->ad_type->sat_syntax,
312 ad->ad_type->sat_equality,
313 &vals[i], &nvals[i], NULL );
316 Debug( LDAP_DEBUG_ANY,
317 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
326 atail->a_next = NULL;
327 e->e_attrs = ahead.a_next;
329 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
330 e->e_dn, (unsigned long) e, 0 );
334 for ( i=0; i<lines; i++ ) {
335 if ( freeval[i] ) free( vals[i].bv_val );
336 free( nvals[i].bv_val );
344 #define GRABSIZE BUFSIZ
346 #define MAKE_SPACE( n ) { \
347 while ( ecur + (n) > ebuf + emaxsize ) { \
349 offset = (int) (ecur - ebuf); \
350 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
351 emaxsize + GRABSIZE ); \
352 emaxsize += GRABSIZE; \
353 ecur = ebuf + offset; \
370 * In string format, an entry looks like this:
372 * [<attr>: <value>\n]*
378 if ( e->e_dn != NULL ) {
380 tmplen = e->e_name.bv_len;
381 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
382 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
385 /* put the attributes */
386 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
387 /* put "<type>:[:] <value>" line for each value */
388 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
390 tmplen = a->a_desc->ad_cname.bv_len;
391 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
392 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
393 a->a_desc->ad_cname.bv_val,
394 bv->bv_val, bv->bv_len );
401 return( (char *) ebuf );
405 entry_clean( Entry *e )
407 /* free an entry structure */
410 /* e_private must be freed by the caller */
411 assert( e->e_private == NULL );
415 if ( !BER_BVISNULL( &e->e_name ) ) {
416 free( e->e_name.bv_val );
417 BER_BVZERO( &e->e_name );
419 if ( !BER_BVISNULL( &e->e_nname ) ) {
420 free( e->e_nname.bv_val );
421 BER_BVZERO( &e->e_nname );
424 if ( !BER_BVISNULL( &e->e_bv ) ) {
425 free( e->e_bv.bv_val );
426 BER_BVZERO( &e->e_bv );
429 /* free attributes */
430 attrs_free( e->e_attrs );
435 entry_free( Entry *e )
443 * These routines are used only by Backend.
445 * the Entry has three entry points (ways to find things):
447 * by entry e.g., if you already have an entry from the cache
448 * and want to delete it. (really by entry ptr)
449 * by dn e.g., when looking for the base object of a search
450 * by id e.g., for search candidates
452 * these correspond to three different avl trees that are maintained.
456 entry_cmp( Entry *e1, Entry *e2 )
458 return SLAP_PTRCMP( e1, e2 );
462 entry_dn_cmp( const void *v_e1, const void *v_e2 )
464 /* compare their normalized UPPERCASED dn's */
465 const Entry *e1 = v_e1, *e2 = v_e2;
467 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
471 entry_id_cmp( const void *v_e1, const void *v_e2 )
473 const Entry *e1 = v_e1, *e2 = v_e2;
474 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
477 /* This is like a ber_len */
478 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
479 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
482 entry_putlen(unsigned char **buf, ber_len_t len)
484 ber_len_t lenlen = entry_lenlen(len);
487 **buf = (unsigned char) len;
490 **buf = 0x80 | ((unsigned char) lenlen - 1);
491 for (i=lenlen-1; i>0; i--) {
492 (*buf)[i] = (unsigned char) len;
500 entry_getlen(unsigned char **buf)
517 /* Count up the sizes of the components of an entry */
518 void entry_partsize(Entry *e, ber_len_t *plen,
519 int *pnattrs, int *pnvals, int norm)
521 ber_len_t len, dnlen, ndnlen;
522 int i, nat = 0, nval = 0;
525 dnlen = e->e_name.bv_len;
526 len = dnlen + 1; /* trailing NUL byte */
527 len += entry_lenlen(dnlen);
529 ndnlen = e->e_nname.bv_len;
531 len += entry_lenlen(ndnlen);
533 for (a=e->e_attrs; a; a=a->a_next) {
534 /* For AttributeDesc, we only store the attr name */
536 len += a->a_desc->ad_cname.bv_len+1;
537 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
538 for (i=0; a->a_vals[i].bv_val; i++) {
540 len += a->a_vals[i].bv_len + 1;
541 len += entry_lenlen(a->a_vals[i].bv_len);
543 len += entry_lenlen(i);
544 nval++; /* empty berval at end */
545 if (norm && a->a_nvals != a->a_vals) {
546 for (i=0; a->a_nvals[i].bv_val; i++) {
548 len += a->a_nvals[i].bv_len + 1;
549 len += entry_lenlen(a->a_nvals[i].bv_len);
551 len += entry_lenlen(i); /* i nvals */
554 len += entry_lenlen(0); /* 0 nvals */
557 len += entry_lenlen(nat);
558 len += entry_lenlen(nval);
564 /* Add up the size of the entry for a flattened buffer */
565 ber_len_t entry_flatsize(Entry *e, int norm)
570 entry_partsize(e, &len, &nattrs, &nvals, norm);
571 len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
572 (nvals * sizeof(struct berval));
576 /* Flatten an Entry into a buffer. The buffer is filled with just the
577 * strings/bervals of all the entry components. Each field is preceded
578 * by its length, encoded the way ber_put_len works. Every field is NUL
579 * terminated. The entire buffer size is precomputed so that a single
580 * malloc can be performed. The entry size is also recorded,
581 * to aid in entry_decode.
583 int entry_encode(Entry *e, struct berval *bv)
585 ber_len_t len, dnlen, ndnlen;
586 int i, nattrs, nvals;
590 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
591 (long) e->e_id, e->e_dn, 0 );
592 dnlen = e->e_name.bv_len;
593 ndnlen = e->e_nname.bv_len;
595 entry_partsize( e, &len, &nattrs, &nvals, 1 );
598 bv->bv_val = ch_malloc(len);
599 ptr = (unsigned char *)bv->bv_val;
600 entry_putlen(&ptr, nattrs);
601 entry_putlen(&ptr, nvals);
602 entry_putlen(&ptr, dnlen);
603 AC_MEMCPY(ptr, e->e_dn, dnlen);
606 entry_putlen(&ptr, ndnlen);
607 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
611 for (a=e->e_attrs; a; a=a->a_next) {
612 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
613 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
614 a->a_desc->ad_cname.bv_len);
615 ptr += a->a_desc->ad_cname.bv_len;
618 for (i=0; a->a_vals[i].bv_val; i++);
619 entry_putlen(&ptr, i);
620 for (i=0; a->a_vals[i].bv_val; i++) {
621 entry_putlen(&ptr, a->a_vals[i].bv_len);
622 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
623 a->a_vals[i].bv_len);
624 ptr += a->a_vals[i].bv_len;
627 if (a->a_nvals != a->a_vals) {
628 entry_putlen(&ptr, i);
629 for (i=0; a->a_nvals[i].bv_val; i++) {
630 entry_putlen(&ptr, a->a_nvals[i].bv_len);
631 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
632 a->a_nvals[i].bv_len);
633 ptr += a->a_nvals[i].bv_len;
637 entry_putlen(&ptr, 0);
644 /* Retrieve an Entry that was stored using entry_encode above.
645 * We malloc a single block with the size stored above for the Entry
646 * and all of its Attributes. We also must lookup the stored
647 * attribute names to get AttributeDescriptions. To detect if the
648 * attributes of an Entry are later modified, we note that e->e_attr
649 * is always a constant offset from (e).
651 * Note: everything is stored in a single contiguous block, so
652 * you can not free individual attributes or names from this
653 * structure. Attempting to do so will likely corrupt memory.
655 #ifdef SLAP_ZONE_ALLOC
656 int entry_decode(struct berval *bv, Entry **e, void *ctx)
658 int entry_decode(struct berval *bv, Entry **e)
661 int i, j, count, nattrs, nvals;
666 AttributeDescription *ad;
667 unsigned char *ptr = (unsigned char *)bv->bv_val;
670 nattrs = entry_getlen(&ptr);
672 Debug( LDAP_DEBUG_ANY,
673 "entry_decode: attribute count was zero\n", 0, 0, 0);
676 nvals = entry_getlen(&ptr);
678 Debug( LDAP_DEBUG_ANY,
679 "entry_decode: value count was zero\n", 0, 0, 0);
682 i = sizeof(Entry) + (nattrs * sizeof(Attribute)) +
683 (nvals * sizeof(struct berval));
684 #ifdef SLAP_ZONE_ALLOC
685 x = slap_zn_calloc(1, i + bv->bv_len, ctx);
686 AC_MEMCPY((char*)x + i, bv->bv_val, bv->bv_len);
687 bv->bv_val = (char*)x + i;
688 ptr = (unsigned char *)bv->bv_val;
689 /* pointer is reset, now advance past nattrs and nvals again */
695 i = entry_getlen(&ptr);
696 x->e_name.bv_val = (char *) ptr;
697 x->e_name.bv_len = i;
699 i = entry_getlen(&ptr);
700 x->e_nname.bv_val = (char *) ptr;
701 x->e_nname.bv_len = i;
703 Debug( LDAP_DEBUG_TRACE,
704 "entry_decode: \"%s\"\n",
708 /* A valid entry must have at least one attr, so this
709 * pointer can never be NULL
711 x->e_attrs = (Attribute *)(x+1);
712 bptr = (BerVarray)x->e_attrs;
715 while ((i = entry_getlen(&ptr))) {
718 bv.bv_val = (char *) ptr;
720 a->a_next = (Attribute *)bptr;
722 a = (Attribute *)bptr;
724 rc = slap_bv2ad( &bv, &ad, &text );
726 if( rc != LDAP_SUCCESS ) {
727 Debug( LDAP_DEBUG_TRACE,
728 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
729 rc = slap_bv2undef_ad( &bv, &ad, &text );
731 if( rc != LDAP_SUCCESS ) {
732 Debug( LDAP_DEBUG_ANY,
733 "<= entry_decode: str2undef_ad(%s): %s\n",
740 bptr = (BerVarray)(a+1);
743 #ifdef LDAP_COMP_MATCH
744 a->a_comp_data = NULL;
746 count = j = entry_getlen(&ptr);
749 i = entry_getlen(&ptr);
751 bptr->bv_val = (char *)ptr;
760 j = entry_getlen(&ptr);
764 i = entry_getlen(&ptr);
766 bptr->bv_val = (char *)ptr;
775 a->a_nvals = a->a_vals;
782 if (a) a->a_next = NULL;
783 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
789 Entry *entry_dup( Entry *e )
793 ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
796 ber_dupbv( &ret->e_name, &e->e_name );
797 ber_dupbv( &ret->e_nname, &e->e_nname );
798 ret->e_attrs = attrs_dup( e->e_attrs );
799 ret->e_ocflags = e->e_ocflags;
800 ret->e_bv.bv_val = NULL;
801 ret->e_bv.bv_len = 0;
802 ret->e_private = NULL;