1 /* entry.c - routines for dealing with entries */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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 Debug( LDAP_DEBUG_TRACE,
137 "<= str2entry ran past end of entry\n", 0, 0, 0 );
141 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
144 Debug( LDAP_DEBUG_TRACE,
145 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
149 if ( type[i].bv_len == dn_bv.bv_len &&
150 strcasecmp( type[i].bv_val, dn_bv.bv_val ) == 0 ) {
152 if ( e->e_dn != NULL ) {
153 Debug( LDAP_DEBUG_ANY, "str2entry: "
154 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
155 (long) e->e_id, e->e_dn, vals[i].bv_val );
159 rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
160 if( rc != LDAP_SUCCESS ) {
161 Debug( LDAP_DEBUG_ANY, "str2entry: "
162 "entry %ld has invalid DN \"%s\"\n",
163 (long) e->e_id, vals[i].bv_val, 0 );
166 if ( freeval[i] ) free( vals[i].bv_val );
167 vals[i].bv_val = NULL;
174 /* check to make sure there was a dn: line */
175 if ( BER_BVISNULL( &e->e_name )) {
176 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
177 (long) e->e_id, 0, 0 );
181 #define bvcasematch(bv1, bv2) ( ((bv1)->bv_len == (bv2)->bv_len) && (strncasecmp((bv1)->bv_val, (bv2)->bv_val, (bv1)->bv_len) == 0) )
183 /* Make sure all attributes with multiple values are contiguous */
189 for (i=0; i<lines; i++) {
190 for ( j=i+1; j<lines; j++ ) {
191 if ( bvcasematch( type+i, type+j )) {
192 /* out of order, move intervening attributes down */
196 for ( k=j; k>i; k-- ) {
199 freeval[k] = freeval[k-1];
212 for ( i=0; i<=lines; i++ ) {
214 if ( !ad || ( i<lines && !bvcasematch( type+i, &ad->ad_cname ))) {
216 rc = slap_bv2ad( type+i, &ad, &text );
218 if( rc != LDAP_SUCCESS ) {
219 Debug( slapMode & SLAP_TOOL_MODE
220 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
221 "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
222 if( slapMode & SLAP_TOOL_MODE ) {
226 rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
227 if( rc != LDAP_SUCCESS ) {
228 Debug( LDAP_DEBUG_ANY,
229 "<= str2entry: slap_str2undef_ad(%s): %s\n",
230 type[i].bv_val, text, 0 );
236 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
238 atail->a_next = (Attribute *) ch_malloc( sizeof(Attribute) );
239 atail = atail->a_next;
241 atail->a_desc = ad_prev;
242 atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
243 if( ad_prev->ad_type->sat_equality &&
244 ad_prev->ad_type->sat_equality->smr_normalize )
245 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
247 atail->a_nvals = NULL;
249 for ( j=0; j<attr_cnt; j++ ) {
251 atail->a_vals[j] = vals[k];
253 ber_dupbv( atail->a_vals+j, &vals[k] );
254 vals[k].bv_val = NULL;
255 if ( atail->a_nvals ) {
256 atail->a_nvals[j] = nvals[k];
257 nvals[k].bv_val = NULL;
261 BER_BVZERO( &atail->a_vals[j] );
262 if ( atail->a_nvals ) {
263 BER_BVZERO( &atail->a_nvals[j] );
265 atail->a_nvals = atail->a_vals;
268 if ( i == lines ) break;
271 if( slapMode & SLAP_TOOL_MODE ) {
273 slap_syntax_validate_func *validate =
274 ad->ad_type->sat_syntax->ssyn_validate;
275 slap_syntax_transform_func *pretty =
276 ad->ad_type->sat_syntax->ssyn_pretty;
279 #ifdef SLAP_ORDERED_PRETTYNORM
280 rc = ordered_value_pretty( ad,
281 &vals[i], &pval, NULL );
282 #else /* ! SLAP_ORDERED_PRETTYNORM */
283 rc = pretty( ad->ad_type->sat_syntax,
284 &vals[i], &pval, NULL );
285 #endif /* ! SLAP_ORDERED_PRETTYNORM */
287 } else if ( validate ) {
289 * validate value per syntax
291 #ifdef SLAP_ORDERED_PRETTYNORM
292 rc = ordered_value_validate( ad, &vals[i], LDAP_MOD_ADD );
293 #else /* ! SLAP_ORDERED_PRETTYNORM */
294 rc = validate( ad->ad_type->sat_syntax, &vals[i] );
295 #endif /* ! SLAP_ORDERED_PRETTYNORM */
298 Debug( LDAP_DEBUG_ANY,
299 "str2entry: attributeType %s #%d: "
300 "no validator for syntax %s\n",
301 ad->ad_cname.bv_val, attr_cnt,
302 ad->ad_type->sat_syntax->ssyn_oid );
307 Debug( LDAP_DEBUG_ANY,
308 "str2entry: invalid value "
309 "for attributeType %s #%d (syntax %s)\n",
310 ad->ad_cname.bv_val, attr_cnt,
311 ad->ad_type->sat_syntax->ssyn_oid );
316 if ( freeval[i] ) free( vals[i].bv_val );
322 if ( ad->ad_type->sat_equality &&
323 ad->ad_type->sat_equality->smr_normalize )
325 #ifdef SLAP_ORDERED_PRETTYNORM
326 rc = ordered_value_normalize(
327 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
329 ad->ad_type->sat_equality,
330 &vals[i], &nvals[i], NULL );
331 #else /* ! SLAP_ORDERED_PRETTYNORM */
332 rc = ad->ad_type->sat_equality->smr_normalize(
333 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
334 ad->ad_type->sat_syntax,
335 ad->ad_type->sat_equality,
336 &vals[i], &nvals[i], NULL );
337 #endif /* ! SLAP_ORDERED_PRETTYNORM */
340 Debug( LDAP_DEBUG_ANY,
341 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
350 atail->a_next = NULL;
351 e->e_attrs = ahead.a_next;
353 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
354 e->e_dn, (unsigned long) e, 0 );
358 for ( i=0; i<lines; i++ ) {
359 if ( freeval[i] ) free( vals[i].bv_val );
360 free( nvals[i].bv_val );
368 #define GRABSIZE BUFSIZ
370 #define MAKE_SPACE( n ) { \
371 while ( ecur + (n) > ebuf + emaxsize ) { \
373 offset = (int) (ecur - ebuf); \
374 ebuf = ch_realloc( ebuf, \
375 emaxsize + GRABSIZE ); \
376 emaxsize += GRABSIZE; \
377 ecur = ebuf + offset; \
394 * In string format, an entry looks like this:
396 * [<attr>: <value>\n]*
402 if ( e->e_dn != NULL ) {
404 tmplen = e->e_name.bv_len;
405 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
406 ldif_sput( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
409 /* put the attributes */
410 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
411 /* put "<type>:[:] <value>" line for each value */
412 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
414 tmplen = a->a_desc->ad_cname.bv_len;
415 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
416 ldif_sput( &ecur, LDIF_PUT_VALUE,
417 a->a_desc->ad_cname.bv_val,
418 bv->bv_val, bv->bv_len );
429 entry_clean( Entry *e )
431 /* free an entry structure */
434 /* e_private must be freed by the caller */
435 assert( e->e_private == NULL );
439 if ( !BER_BVISNULL( &e->e_name ) ) {
440 free( e->e_name.bv_val );
441 BER_BVZERO( &e->e_name );
443 if ( !BER_BVISNULL( &e->e_nname ) ) {
444 free( e->e_nname.bv_val );
445 BER_BVZERO( &e->e_nname );
448 if ( !BER_BVISNULL( &e->e_bv ) ) {
449 free( e->e_bv.bv_val );
450 BER_BVZERO( &e->e_bv );
453 /* free attributes */
454 attrs_free( e->e_attrs );
459 entry_free( Entry *e )
467 * These routines are used only by Backend.
469 * the Entry has three entry points (ways to find things):
471 * by entry e.g., if you already have an entry from the cache
472 * and want to delete it. (really by entry ptr)
473 * by dn e.g., when looking for the base object of a search
474 * by id e.g., for search candidates
476 * these correspond to three different avl trees that are maintained.
480 entry_cmp( Entry *e1, Entry *e2 )
482 return SLAP_PTRCMP( e1, e2 );
486 entry_dn_cmp( const void *v_e1, const void *v_e2 )
488 /* compare their normalized UPPERCASED dn's */
489 const Entry *e1 = v_e1, *e2 = v_e2;
491 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
495 entry_id_cmp( const void *v_e1, const void *v_e2 )
497 const Entry *e1 = v_e1, *e2 = v_e2;
498 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
501 /* This is like a ber_len */
502 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
503 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
506 entry_putlen(unsigned char **buf, ber_len_t len)
508 ber_len_t lenlen = entry_lenlen(len);
511 **buf = (unsigned char) len;
514 **buf = 0x80 | ((unsigned char) lenlen - 1);
515 for (i=lenlen-1; i>0; i--) {
516 (*buf)[i] = (unsigned char) len;
524 entry_getlen(unsigned char **buf)
541 /* Count up the sizes of the components of an entry */
542 void entry_partsize(Entry *e, ber_len_t *plen,
543 int *pnattrs, int *pnvals, int norm)
545 ber_len_t len, dnlen, ndnlen;
546 int i, nat = 0, nval = 0;
549 dnlen = e->e_name.bv_len;
550 len = dnlen + 1; /* trailing NUL byte */
551 len += entry_lenlen(dnlen);
553 ndnlen = e->e_nname.bv_len;
555 len += entry_lenlen(ndnlen);
557 for (a=e->e_attrs; a; a=a->a_next) {
558 /* For AttributeDesc, we only store the attr name */
560 len += a->a_desc->ad_cname.bv_len+1;
561 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
562 for (i=0; a->a_vals[i].bv_val; i++) {
564 len += a->a_vals[i].bv_len + 1;
565 len += entry_lenlen(a->a_vals[i].bv_len);
567 len += entry_lenlen(i);
568 nval++; /* empty berval at end */
569 if (norm && a->a_nvals != a->a_vals) {
570 for (i=0; a->a_nvals[i].bv_val; i++) {
572 len += a->a_nvals[i].bv_len + 1;
573 len += entry_lenlen(a->a_nvals[i].bv_len);
575 len += entry_lenlen(i); /* i nvals */
578 len += entry_lenlen(0); /* 0 nvals */
581 len += entry_lenlen(nat);
582 len += entry_lenlen(nval);
588 /* Add up the size of the entry for a flattened buffer */
589 ber_len_t entry_flatsize(Entry *e, int norm)
594 entry_partsize(e, &len, &nattrs, &nvals, norm);
595 len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
596 (nvals * sizeof(struct berval));
600 /* Flatten an Entry into a buffer. The buffer is filled with just the
601 * strings/bervals of all the entry components. Each field is preceded
602 * by its length, encoded the way ber_put_len works. Every field is NUL
603 * terminated. The entire buffer size is precomputed so that a single
604 * malloc can be performed. The entry size is also recorded,
605 * to aid in entry_decode.
607 int entry_encode(Entry *e, struct berval *bv)
609 ber_len_t len, dnlen, ndnlen;
610 int i, nattrs, nvals;
614 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
615 (long) e->e_id, e->e_dn, 0 );
616 dnlen = e->e_name.bv_len;
617 ndnlen = e->e_nname.bv_len;
619 entry_partsize( e, &len, &nattrs, &nvals, 1 );
622 bv->bv_val = ch_malloc(len);
623 ptr = (unsigned char *)bv->bv_val;
624 entry_putlen(&ptr, nattrs);
625 entry_putlen(&ptr, nvals);
626 entry_putlen(&ptr, dnlen);
627 AC_MEMCPY(ptr, e->e_dn, dnlen);
630 entry_putlen(&ptr, ndnlen);
631 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
635 for (a=e->e_attrs; a; a=a->a_next) {
636 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
637 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
638 a->a_desc->ad_cname.bv_len);
639 ptr += a->a_desc->ad_cname.bv_len;
642 for (i=0; a->a_vals[i].bv_val; i++);
643 entry_putlen(&ptr, i);
644 for (i=0; a->a_vals[i].bv_val; i++) {
645 entry_putlen(&ptr, a->a_vals[i].bv_len);
646 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
647 a->a_vals[i].bv_len);
648 ptr += a->a_vals[i].bv_len;
651 if (a->a_nvals != a->a_vals) {
652 entry_putlen(&ptr, i);
653 for (i=0; a->a_nvals[i].bv_val; i++) {
654 entry_putlen(&ptr, a->a_nvals[i].bv_len);
655 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
656 a->a_nvals[i].bv_len);
657 ptr += a->a_nvals[i].bv_len;
661 entry_putlen(&ptr, 0);
668 /* Retrieve an Entry that was stored using entry_encode above.
669 * We malloc a single block with the size stored above for the Entry
670 * and all of its Attributes. We also must lookup the stored
671 * attribute names to get AttributeDescriptions. To detect if the
672 * attributes of an Entry are later modified, we note that e->e_attr
673 * is always a constant offset from (e).
675 * Note: everything is stored in a single contiguous block, so
676 * you can not free individual attributes or names from this
677 * structure. Attempting to do so will likely corrupt memory.
679 #ifdef SLAP_ZONE_ALLOC
680 int entry_decode(struct berval *bv, Entry **e, void *ctx)
682 int entry_decode(struct berval *bv, Entry **e)
685 int i, j, count, nattrs, nvals;
690 AttributeDescription *ad;
691 unsigned char *ptr = (unsigned char *)bv->bv_val;
694 nattrs = entry_getlen(&ptr);
696 Debug( LDAP_DEBUG_ANY,
697 "entry_decode: attribute count was zero\n", 0, 0, 0);
700 nvals = entry_getlen(&ptr);
702 Debug( LDAP_DEBUG_ANY,
703 "entry_decode: value count was zero\n", 0, 0, 0);
706 i = sizeof(Entry) + (nattrs * sizeof(Attribute)) +
707 (nvals * sizeof(struct berval));
708 #ifdef SLAP_ZONE_ALLOC
709 x = slap_zn_calloc(1, i + bv->bv_len, ctx);
710 AC_MEMCPY((char*)x + i, bv->bv_val, bv->bv_len);
711 bv->bv_val = (char*)x + i;
712 ptr = (unsigned char *)bv->bv_val;
713 /* pointer is reset, now advance past nattrs and nvals again */
719 i = entry_getlen(&ptr);
720 x->e_name.bv_val = (char *) ptr;
721 x->e_name.bv_len = i;
723 i = entry_getlen(&ptr);
724 x->e_nname.bv_val = (char *) ptr;
725 x->e_nname.bv_len = i;
727 Debug( LDAP_DEBUG_TRACE,
728 "entry_decode: \"%s\"\n",
732 /* A valid entry must have at least one attr, so this
733 * pointer can never be NULL
735 x->e_attrs = (Attribute *)(x+1);
736 bptr = (BerVarray)x->e_attrs;
739 while ((i = entry_getlen(&ptr))) {
742 bv.bv_val = (char *) ptr;
744 a->a_next = (Attribute *)bptr;
746 a = (Attribute *)bptr;
748 rc = slap_bv2ad( &bv, &ad, &text );
750 if( rc != LDAP_SUCCESS ) {
751 Debug( LDAP_DEBUG_TRACE,
752 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
753 rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
755 if( rc != LDAP_SUCCESS ) {
756 Debug( LDAP_DEBUG_ANY,
757 "<= entry_decode: slap_str2undef_ad(%s): %s\n",
764 bptr = (BerVarray)(a+1);
767 #ifdef LDAP_COMP_MATCH
768 a->a_comp_data = NULL;
770 count = j = entry_getlen(&ptr);
773 i = entry_getlen(&ptr);
775 bptr->bv_val = (char *)ptr;
784 j = entry_getlen(&ptr);
788 i = entry_getlen(&ptr);
790 bptr->bv_val = (char *)ptr;
799 a->a_nvals = a->a_vals;
806 if (a) a->a_next = NULL;
807 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
813 Entry *entry_dup( Entry *e )
817 ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
820 ber_dupbv( &ret->e_name, &e->e_name );
821 ber_dupbv( &ret->e_nname, &e->e_nname );
822 ret->e_attrs = attrs_dup( e->e_attrs );
823 ret->e_ocflags = e->e_ocflags;
824 ret->e_bv.bv_val = NULL;
825 ret->e_bv.bv_len = 0;
826 ret->e_private = NULL;