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 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
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 *type, *vals, *nvals;
75 AttributeDescription *ad, *ad_prev;
80 Attribute ahead, *atail;
83 * LDIF is used as the string format.
84 * An entry looks like this:
87 * [<attr>:[:] <value>\n]
88 * [<tab><continuedvalue>\n]*
91 * If a double colon is used after a type, it means the
92 * following value is encoded as a base 64 string. This
93 * happens if the value contains a non-printing character
97 Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
98 s ? s : "NULL", 0, 0 );
100 /* initialize reader/writer lock */
101 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
104 Debug( LDAP_DEBUG_ANY,
105 "<= str2entry NULL (entry allocation failed)\n",
110 /* initialize entry */
113 /* dn + attributes */
121 lines = ldif_countlines( s );
122 type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
124 nvals = vals+lines+1;
125 freeval = (char *)(nvals+lines+1);
128 /* parse into individual values, record DN */
129 while ( (s = ldif_getline( &next )) != NULL ) {
131 if ( *s == '\n' || *s == '\0' ) {
136 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
139 Debug( LDAP_DEBUG_TRACE,
140 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
144 if ( type[i].bv_len == dn_bv.bv_len &&
145 strcasecmp( type[i].bv_val, dn_bv.bv_val ) == 0 ) {
147 if ( e->e_dn != NULL ) {
148 Debug( LDAP_DEBUG_ANY, "str2entry: "
149 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
150 (long) e->e_id, e->e_dn, vals[i].bv_val );
154 rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
155 if( rc != LDAP_SUCCESS ) {
156 Debug( LDAP_DEBUG_ANY, "str2entry: "
157 "entry %ld has invalid DN \"%s\"\n",
158 (long) e->e_id, vals[i].bv_val, 0 );
161 if ( freeval[i] ) free( vals[i].bv_val );
162 vals[i].bv_val = NULL;
169 /* check to make sure there was a dn: line */
170 if ( BER_BVISNULL( &e->e_name )) {
171 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
172 (long) e->e_id, 0, 0 );
176 /* Make sure all attributes with multiple values are contiguous */
182 for (i=0; i<lines; i++) {
183 for ( j=i+1; j<lines; j++ ) {
184 if ( bvmatch( type+i, type+j )) {
185 /* out of order, move intervening attributes down */
189 for ( k=j; k>i; k-- ) {
192 freeval[k] = freeval[k-1];
205 for ( i=0; i<=lines; i++ ) {
207 if ( !ad || ( i<lines && !bvmatch( type+i, &ad->ad_cname ))) {
209 rc = slap_bv2ad( type+i, &ad, &text );
211 if( rc != LDAP_SUCCESS ) {
212 Debug( slapMode & SLAP_TOOL_MODE
213 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
214 "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
215 if( slapMode & SLAP_TOOL_MODE ) {
219 rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
220 if( rc != LDAP_SUCCESS ) {
221 Debug( LDAP_DEBUG_ANY,
222 "<= str2entry: slap_str2undef_ad(%s): %s\n",
223 type[i].bv_val, text, 0 );
229 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
231 atail->a_next = (Attribute *) ch_malloc( sizeof(Attribute) );
232 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 #ifdef SLAP_ORDERED_PRETTYNORM
273 rc = ordered_value_pretty( ad,
274 &vals[i], &pval, NULL );
275 #else /* ! SLAP_ORDERED_PRETTYNORM */
276 rc = pretty( ad->ad_type->sat_syntax,
277 &vals[i], &pval, NULL );
278 #endif /* ! SLAP_ORDERED_PRETTYNORM */
280 } else if ( validate ) {
282 * validate value per syntax
284 #ifdef SLAP_ORDERED_PRETTYNORM
285 rc = ordered_value_validate( ad, &vals[i] );
286 #else /* ! SLAP_ORDERED_PRETTYNORM */
287 rc = validate( ad->ad_type->sat_syntax, &vals[i] );
288 #endif /* ! SLAP_ORDERED_PRETTYNORM */
291 Debug( LDAP_DEBUG_ANY,
292 "str2entry: attributeType %s #%d: "
293 "no validator for syntax %s\n",
294 ad->ad_cname.bv_val, attr_cnt,
295 ad->ad_type->sat_syntax->ssyn_oid );
300 Debug( LDAP_DEBUG_ANY,
301 "str2entry: invalid value "
302 "for attributeType %s #%d (syntax %s)\n",
303 ad->ad_cname.bv_val, attr_cnt,
304 ad->ad_type->sat_syntax->ssyn_oid );
309 if ( freeval[i] ) free( vals[i].bv_val );
315 if ( ad->ad_type->sat_equality &&
316 ad->ad_type->sat_equality->smr_normalize )
318 #ifdef SLAP_ORDERED_PRETTYNORM
319 rc = ordered_value_normalize(
320 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
322 ad->ad_type->sat_equality,
323 &vals[i], &nvals[i], NULL );
324 #else /* ! SLAP_ORDERED_PRETTYNORM */
325 rc = ad->ad_type->sat_equality->smr_normalize(
326 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
327 ad->ad_type->sat_syntax,
328 ad->ad_type->sat_equality,
329 &vals[i], &nvals[i], NULL );
330 #endif /* ! SLAP_ORDERED_PRETTYNORM */
333 Debug( LDAP_DEBUG_ANY,
334 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
343 atail->a_next = NULL;
344 e->e_attrs = ahead.a_next;
346 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
347 e->e_dn, (unsigned long) e, 0 );
351 for ( i=0; i<lines; i++ ) {
352 if ( freeval[i] ) free( vals[i].bv_val );
353 free( nvals[i].bv_val );
361 #define GRABSIZE BUFSIZ
363 #define MAKE_SPACE( n ) { \
364 while ( ecur + (n) > ebuf + emaxsize ) { \
366 offset = (int) (ecur - ebuf); \
367 ebuf = ch_realloc( ebuf, \
368 emaxsize + GRABSIZE ); \
369 emaxsize += GRABSIZE; \
370 ecur = ebuf + offset; \
387 * In string format, an entry looks like this:
389 * [<attr>: <value>\n]*
395 if ( e->e_dn != NULL ) {
397 tmplen = e->e_name.bv_len;
398 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
399 ldif_sput( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
402 /* put the attributes */
403 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
404 /* put "<type>:[:] <value>" line for each value */
405 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
407 tmplen = a->a_desc->ad_cname.bv_len;
408 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
409 ldif_sput( &ecur, LDIF_PUT_VALUE,
410 a->a_desc->ad_cname.bv_val,
411 bv->bv_val, bv->bv_len );
422 entry_clean( Entry *e )
424 /* free an entry structure */
427 /* e_private must be freed by the caller */
428 assert( e->e_private == NULL );
432 if ( !BER_BVISNULL( &e->e_name ) ) {
433 free( e->e_name.bv_val );
434 BER_BVZERO( &e->e_name );
436 if ( !BER_BVISNULL( &e->e_nname ) ) {
437 free( e->e_nname.bv_val );
438 BER_BVZERO( &e->e_nname );
441 if ( !BER_BVISNULL( &e->e_bv ) ) {
442 free( e->e_bv.bv_val );
443 BER_BVZERO( &e->e_bv );
446 /* free attributes */
447 attrs_free( e->e_attrs );
452 entry_free( Entry *e )
460 * These routines are used only by Backend.
462 * the Entry has three entry points (ways to find things):
464 * by entry e.g., if you already have an entry from the cache
465 * and want to delete it. (really by entry ptr)
466 * by dn e.g., when looking for the base object of a search
467 * by id e.g., for search candidates
469 * these correspond to three different avl trees that are maintained.
473 entry_cmp( Entry *e1, Entry *e2 )
475 return SLAP_PTRCMP( e1, e2 );
479 entry_dn_cmp( const void *v_e1, const void *v_e2 )
481 /* compare their normalized UPPERCASED dn's */
482 const Entry *e1 = v_e1, *e2 = v_e2;
484 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
488 entry_id_cmp( const void *v_e1, const void *v_e2 )
490 const Entry *e1 = v_e1, *e2 = v_e2;
491 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
494 /* This is like a ber_len */
495 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
496 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
499 entry_putlen(unsigned char **buf, ber_len_t len)
501 ber_len_t lenlen = entry_lenlen(len);
504 **buf = (unsigned char) len;
507 **buf = 0x80 | ((unsigned char) lenlen - 1);
508 for (i=lenlen-1; i>0; i--) {
509 (*buf)[i] = (unsigned char) len;
517 entry_getlen(unsigned char **buf)
534 /* Count up the sizes of the components of an entry */
535 void entry_partsize(Entry *e, ber_len_t *plen,
536 int *pnattrs, int *pnvals, int norm)
538 ber_len_t len, dnlen, ndnlen;
539 int i, nat = 0, nval = 0;
542 dnlen = e->e_name.bv_len;
543 len = dnlen + 1; /* trailing NUL byte */
544 len += entry_lenlen(dnlen);
546 ndnlen = e->e_nname.bv_len;
548 len += entry_lenlen(ndnlen);
550 for (a=e->e_attrs; a; a=a->a_next) {
551 /* For AttributeDesc, we only store the attr name */
553 len += a->a_desc->ad_cname.bv_len+1;
554 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
555 for (i=0; a->a_vals[i].bv_val; i++) {
557 len += a->a_vals[i].bv_len + 1;
558 len += entry_lenlen(a->a_vals[i].bv_len);
560 len += entry_lenlen(i);
561 nval++; /* empty berval at end */
562 if (norm && a->a_nvals != a->a_vals) {
563 for (i=0; a->a_nvals[i].bv_val; i++) {
565 len += a->a_nvals[i].bv_len + 1;
566 len += entry_lenlen(a->a_nvals[i].bv_len);
568 len += entry_lenlen(i); /* i nvals */
571 len += entry_lenlen(0); /* 0 nvals */
574 len += entry_lenlen(nat);
575 len += entry_lenlen(nval);
581 /* Add up the size of the entry for a flattened buffer */
582 ber_len_t entry_flatsize(Entry *e, int norm)
587 entry_partsize(e, &len, &nattrs, &nvals, norm);
588 len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
589 (nvals * sizeof(struct berval));
593 /* Flatten an Entry into a buffer. The buffer is filled with just the
594 * strings/bervals of all the entry components. Each field is preceded
595 * by its length, encoded the way ber_put_len works. Every field is NUL
596 * terminated. The entire buffer size is precomputed so that a single
597 * malloc can be performed. The entry size is also recorded,
598 * to aid in entry_decode.
600 int entry_encode(Entry *e, struct berval *bv)
602 ber_len_t len, dnlen, ndnlen;
603 int i, nattrs, nvals;
607 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
608 (long) e->e_id, e->e_dn, 0 );
609 dnlen = e->e_name.bv_len;
610 ndnlen = e->e_nname.bv_len;
612 entry_partsize( e, &len, &nattrs, &nvals, 1 );
615 bv->bv_val = ch_malloc(len);
616 ptr = (unsigned char *)bv->bv_val;
617 entry_putlen(&ptr, nattrs);
618 entry_putlen(&ptr, nvals);
619 entry_putlen(&ptr, dnlen);
620 AC_MEMCPY(ptr, e->e_dn, dnlen);
623 entry_putlen(&ptr, ndnlen);
624 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
628 for (a=e->e_attrs; a; a=a->a_next) {
629 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
630 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
631 a->a_desc->ad_cname.bv_len);
632 ptr += a->a_desc->ad_cname.bv_len;
635 for (i=0; a->a_vals[i].bv_val; i++);
636 entry_putlen(&ptr, i);
637 for (i=0; a->a_vals[i].bv_val; i++) {
638 entry_putlen(&ptr, a->a_vals[i].bv_len);
639 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
640 a->a_vals[i].bv_len);
641 ptr += a->a_vals[i].bv_len;
644 if (a->a_nvals != a->a_vals) {
645 entry_putlen(&ptr, i);
646 for (i=0; a->a_nvals[i].bv_val; i++) {
647 entry_putlen(&ptr, a->a_nvals[i].bv_len);
648 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
649 a->a_nvals[i].bv_len);
650 ptr += a->a_nvals[i].bv_len;
654 entry_putlen(&ptr, 0);
661 /* Retrieve an Entry that was stored using entry_encode above.
662 * We malloc a single block with the size stored above for the Entry
663 * and all of its Attributes. We also must lookup the stored
664 * attribute names to get AttributeDescriptions. To detect if the
665 * attributes of an Entry are later modified, we note that e->e_attr
666 * is always a constant offset from (e).
668 * Note: everything is stored in a single contiguous block, so
669 * you can not free individual attributes or names from this
670 * structure. Attempting to do so will likely corrupt memory.
672 #ifdef SLAP_ZONE_ALLOC
673 int entry_decode(struct berval *bv, Entry **e, void *ctx)
675 int entry_decode(struct berval *bv, Entry **e)
678 int i, j, count, nattrs, nvals;
683 AttributeDescription *ad;
684 unsigned char *ptr = (unsigned char *)bv->bv_val;
687 nattrs = entry_getlen(&ptr);
689 Debug( LDAP_DEBUG_ANY,
690 "entry_decode: attribute count was zero\n", 0, 0, 0);
693 nvals = entry_getlen(&ptr);
695 Debug( LDAP_DEBUG_ANY,
696 "entry_decode: value count was zero\n", 0, 0, 0);
699 i = sizeof(Entry) + (nattrs * sizeof(Attribute)) +
700 (nvals * sizeof(struct berval));
701 #ifdef SLAP_ZONE_ALLOC
702 x = slap_zn_calloc(1, i + bv->bv_len, ctx);
703 AC_MEMCPY((char*)x + i, bv->bv_val, bv->bv_len);
704 bv->bv_val = (char*)x + i;
705 ptr = (unsigned char *)bv->bv_val;
706 /* pointer is reset, now advance past nattrs and nvals again */
712 i = entry_getlen(&ptr);
713 x->e_name.bv_val = (char *) ptr;
714 x->e_name.bv_len = i;
716 i = entry_getlen(&ptr);
717 x->e_nname.bv_val = (char *) ptr;
718 x->e_nname.bv_len = i;
720 Debug( LDAP_DEBUG_TRACE,
721 "entry_decode: \"%s\"\n",
725 /* A valid entry must have at least one attr, so this
726 * pointer can never be NULL
728 x->e_attrs = (Attribute *)(x+1);
729 bptr = (BerVarray)x->e_attrs;
732 while ((i = entry_getlen(&ptr))) {
735 bv.bv_val = (char *) ptr;
737 a->a_next = (Attribute *)bptr;
739 a = (Attribute *)bptr;
741 rc = slap_bv2ad( &bv, &ad, &text );
743 if( rc != LDAP_SUCCESS ) {
744 Debug( LDAP_DEBUG_TRACE,
745 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
746 rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
748 if( rc != LDAP_SUCCESS ) {
749 Debug( LDAP_DEBUG_ANY,
750 "<= entry_decode: slap_str2undef_ad(%s): %s\n",
757 bptr = (BerVarray)(a+1);
760 #ifdef LDAP_COMP_MATCH
761 a->a_comp_data = NULL;
763 count = j = entry_getlen(&ptr);
766 i = entry_getlen(&ptr);
768 bptr->bv_val = (char *)ptr;
777 j = entry_getlen(&ptr);
781 i = entry_getlen(&ptr);
783 bptr->bv_val = (char *)ptr;
792 a->a_nvals = a->a_vals;
799 if (a) a->a_next = NULL;
800 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
806 Entry *entry_dup( Entry *e )
810 ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
813 ber_dupbv( &ret->e_name, &e->e_name );
814 ber_dupbv( &ret->e_nname, &e->e_nname );
815 ret->e_attrs = attrs_dup( e->e_attrs );
816 ret->e_ocflags = e->e_ocflags;
817 ret->e_bv.bv_val = NULL;
818 ret->e_bv.bv_len = 0;
819 ret->e_private = NULL;