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 int entry_destroy(void)
52 if ( ebuf ) free( ebuf );
66 struct berval vals[2];
67 struct berval nvals[2], *nvalsp;
68 AttributeDescription *ad, *ad_prev;
75 * LDIF is used as the string format.
76 * An entry looks like this:
79 * [<attr>:[:] <value>\n]
80 * [<tab><continuedvalue>\n]*
83 * If a double colon is used after a type, it means the
84 * following value is encoded as a base 64 string. This
85 * happens if the value contains a non-printing character
89 Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
90 s ? s : "NULL", 0, 0 );
92 /* initialize reader/writer lock */
93 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
96 Debug( LDAP_DEBUG_ANY,
97 "<= str2entry NULL (entry allocation failed)\n",
102 /* initialize entry */
105 /* dn + attributes */
107 vals[1].bv_val = NULL;
113 while ( (s = ldif_getline( &next )) != NULL ) {
114 if ( *s == '\n' || *s == '\0' ) {
118 if ( ldif_parse_line2( s, &type, &vals[0].bv_val, &vals[0].bv_len,
120 Debug( LDAP_DEBUG_TRACE,
121 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
125 if ( strcasecmp( type, "dn" ) == 0 ) {
127 if ( e->e_dn != NULL ) {
128 Debug( LDAP_DEBUG_ANY, "str2entry: "
129 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
130 (long) e->e_id, e->e_dn, vals[0].bv_val );
131 if ( freeval ) free( vals[0].bv_val );
136 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname, NULL );
137 if ( freeval ) free( vals[0].bv_val );
138 if( rc != LDAP_SUCCESS ) {
139 Debug( LDAP_DEBUG_ANY, "str2entry: "
140 "entry %ld has invalid DN \"%s\"\n",
141 (long) e->e_id, vals[0].bv_val, 0 );
150 rc = slap_str2ad( type, &ad, &text );
152 if( rc != LDAP_SUCCESS ) {
153 Debug( slapMode & SLAP_TOOL_MODE
154 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
155 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
156 if( slapMode & SLAP_TOOL_MODE ) {
158 if ( freeval ) free( vals[0].bv_val );
162 rc = slap_str2undef_ad( type, &ad, &text );
163 if( rc != LDAP_SUCCESS ) {
164 Debug( LDAP_DEBUG_ANY,
165 "<= str2entry: str2undef_ad(%s): %s\n",
168 if ( freeval ) free( vals[0].bv_val );
173 if ( ad != ad_prev ) {
177 if( slapMode & SLAP_TOOL_MODE ) {
179 slap_syntax_validate_func *validate =
180 ad->ad_type->sat_syntax->ssyn_validate;
181 slap_syntax_transform_func *pretty =
182 ad->ad_type->sat_syntax->ssyn_pretty;
185 rc = pretty( ad->ad_type->sat_syntax,
186 &vals[0], &pval, NULL );
188 } else if( validate ) {
190 * validate value per syntax
192 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
195 Debug( LDAP_DEBUG_ANY,
196 "str2entry: attributeType %s #%d: "
197 "no validator for syntax %s\n",
198 ad->ad_cname.bv_val, attr_cnt,
199 ad->ad_type->sat_syntax->ssyn_oid );
201 if ( freeval ) free( vals[0].bv_val );
206 Debug( LDAP_DEBUG_ANY,
207 "str2entry: invalid value "
208 "for attributeType %s #%d (syntax %s)\n",
209 ad->ad_cname.bv_val, attr_cnt,
210 ad->ad_type->sat_syntax->ssyn_oid );
212 if ( freeval ) free( vals[0].bv_val );
217 if ( freeval ) free( vals[0].bv_val );
224 nvals[0].bv_val = NULL;
226 if( ad->ad_type->sat_equality &&
227 ad->ad_type->sat_equality->smr_normalize )
229 rc = ad->ad_type->sat_equality->smr_normalize(
230 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
231 ad->ad_type->sat_syntax,
232 ad->ad_type->sat_equality,
233 &vals[0], &nvals[0], NULL );
236 Debug( LDAP_DEBUG_ANY,
237 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
240 if ( freeval ) free( vals[0].bv_val );
245 nvals[1].bv_val = NULL;
250 rc = attr_merge( e, ad, vals, nvalsp );
252 Debug( LDAP_DEBUG_ANY,
253 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
255 if ( freeval ) free( vals[0].bv_val );
259 if ( freeval ) free( vals[0].bv_val );
260 free( nvals[0].bv_val );
265 /* check to make sure there was a dn: line */
266 if ( e->e_dn == NULL ) {
267 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
268 (long) e->e_id, 0, 0 );
273 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
274 e->e_dn, (unsigned long) e, 0 );
279 #define GRABSIZE BUFSIZ
281 #define MAKE_SPACE( n ) { \
282 while ( ecur + (n) > ebuf + emaxsize ) { \
284 offset = (int) (ecur - ebuf); \
285 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
286 emaxsize + GRABSIZE ); \
287 emaxsize += GRABSIZE; \
288 ecur = ebuf + offset; \
305 * In string format, an entry looks like this:
307 * [<attr>: <value>\n]*
313 if ( e->e_dn != NULL ) {
315 tmplen = e->e_name.bv_len;
316 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
317 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
320 /* put the attributes */
321 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
322 /* put "<type>:[:] <value>" line for each value */
323 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
325 tmplen = a->a_desc->ad_cname.bv_len;
326 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
327 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
328 a->a_desc->ad_cname.bv_val,
329 bv->bv_val, bv->bv_len );
336 return( (char *) ebuf );
340 entry_clean( Entry *e )
342 /* free an entry structure */
345 /* e_private must be freed by the caller */
346 assert( e->e_private == NULL );
350 if ( !BER_BVISNULL( &e->e_name ) ) {
351 free( e->e_name.bv_val );
352 BER_BVZERO( &e->e_name );
354 if ( !BER_BVISNULL( &e->e_nname ) ) {
355 free( e->e_nname.bv_val );
356 BER_BVZERO( &e->e_nname );
359 if ( !BER_BVISNULL( &e->e_bv ) ) {
360 free( e->e_bv.bv_val );
361 BER_BVZERO( &e->e_bv );
364 /* free attributes */
365 attrs_free( e->e_attrs );
370 entry_free( Entry *e )
378 * These routines are used only by Backend.
380 * the Entry has three entry points (ways to find things):
382 * by entry e.g., if you already have an entry from the cache
383 * and want to delete it. (really by entry ptr)
384 * by dn e.g., when looking for the base object of a search
385 * by id e.g., for search candidates
387 * these correspond to three different avl trees that are maintained.
391 entry_cmp( Entry *e1, Entry *e2 )
393 return SLAP_PTRCMP( e1, e2 );
397 entry_dn_cmp( const void *v_e1, const void *v_e2 )
399 /* compare their normalized UPPERCASED dn's */
400 const Entry *e1 = v_e1, *e2 = v_e2;
402 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
406 entry_id_cmp( const void *v_e1, const void *v_e2 )
408 const Entry *e1 = v_e1, *e2 = v_e2;
409 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
412 #define entry_lenlen(l) ((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
413 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5
415 /* This is like a ber_len */
417 entry_lenlen(ber_len_t len)
432 entry_putlen(unsigned char **buf, ber_len_t len)
434 ber_len_t lenlen = entry_lenlen(len);
437 **buf = (unsigned char) len;
440 **buf = 0x80 | ((unsigned char) lenlen - 1);
441 for (i=lenlen-1; i>0; i--) {
442 (*buf)[i] = (unsigned char) len;
450 entry_getlen(unsigned char **buf)
467 /* Add up the size of the entry for a flattened buffer */
468 void entry_flatsize(Entry *e, ber_len_t *psiz, ber_len_t *plen, int norm)
470 ber_len_t siz = sizeof(Entry);
471 ber_len_t len, dnlen, ndnlen;
475 dnlen = e->e_name.bv_len;
476 len = dnlen + 1; /* trailing NUL byte */
477 len += entry_lenlen(dnlen);
479 ndnlen = e->e_nname.bv_len;
481 len += entry_lenlen(ndnlen);
483 for (a=e->e_attrs; a; a=a->a_next) {
484 /* For AttributeDesc, we only store the attr name */
485 siz += sizeof(Attribute);
486 len += a->a_desc->ad_cname.bv_len+1;
487 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
488 for (i=0; a->a_vals[i].bv_val; i++) {
489 siz += sizeof(struct berval);
490 len += a->a_vals[i].bv_len + 1;
491 len += entry_lenlen(a->a_vals[i].bv_len);
493 len += entry_lenlen(i);
494 siz += sizeof(struct berval); /* empty berval at end */
495 if (norm && a->a_nvals != a->a_vals) {
496 for (i=0; a->a_nvals[i].bv_val; i++) {
497 siz += sizeof(struct berval);
498 len += a->a_nvals[i].bv_len + 1;
499 len += entry_lenlen(a->a_nvals[i].bv_len);
501 len += entry_lenlen(i); /* i nvals */
502 siz += sizeof(struct berval);
504 len += entry_lenlen(0); /* 0 nvals */
507 len += 1; /* NUL byte at end */
508 len += entry_lenlen(siz);
513 /* Flatten an Entry into a buffer. The buffer is filled with just the
514 * strings/bervals of all the entry components. Each field is preceded
515 * by its length, encoded the way ber_put_len works. Every field is NUL
516 * terminated. The entire buffer size is precomputed so that a single
517 * malloc can be performed. The entry size is also recorded,
518 * to aid in entry_decode.
520 int entry_encode(Entry *e, struct berval *bv)
522 ber_len_t siz = sizeof(Entry);
523 ber_len_t len, dnlen, ndnlen;
528 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
529 (long) e->e_id, e->e_dn, 0 );
530 dnlen = e->e_name.bv_len;
531 ndnlen = e->e_nname.bv_len;
533 entry_flatsize( e, &siz, &len, 1 );
536 bv->bv_val = ch_malloc(len);
537 ptr = (unsigned char *)bv->bv_val;
538 entry_putlen(&ptr, siz);
539 entry_putlen(&ptr, dnlen);
540 AC_MEMCPY(ptr, e->e_dn, dnlen);
543 entry_putlen(&ptr, ndnlen);
544 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
548 for (a=e->e_attrs; a; a=a->a_next) {
549 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
550 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
551 a->a_desc->ad_cname.bv_len);
552 ptr += a->a_desc->ad_cname.bv_len;
555 for (i=0; a->a_vals[i].bv_val; i++);
556 entry_putlen(&ptr, i);
557 for (i=0; a->a_vals[i].bv_val; i++) {
558 entry_putlen(&ptr, a->a_vals[i].bv_len);
559 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
560 a->a_vals[i].bv_len);
561 ptr += a->a_vals[i].bv_len;
564 if (a->a_nvals != a->a_vals) {
565 entry_putlen(&ptr, i);
566 for (i=0; a->a_nvals[i].bv_val; i++) {
567 entry_putlen(&ptr, a->a_nvals[i].bv_len);
568 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
569 a->a_nvals[i].bv_len);
570 ptr += a->a_nvals[i].bv_len;
574 entry_putlen(&ptr, 0);
582 /* Retrieve an Entry that was stored using entry_encode above.
583 * We malloc a single block with the size stored above for the Entry
584 * and all of its Attributes. We also must lookup the stored
585 * attribute names to get AttributeDescriptions. To detect if the
586 * attributes of an Entry are later modified, we note that e->e_attr
587 * is always a constant offset from (e).
589 * Note: everything is stored in a single contiguous block, so
590 * you can not free individual attributes or names from this
591 * structure. Attempting to do so will likely corrupt memory.
593 #ifdef SLAP_ZONE_ALLOC
594 int entry_decode(struct berval *bv, Entry **e, void *ctx)
596 int entry_decode(struct berval *bv, Entry **e)
604 AttributeDescription *ad;
605 unsigned char *ptr = (unsigned char *)bv->bv_val;
608 i = entry_getlen(&ptr);
610 Debug( LDAP_DEBUG_ANY,
611 "entry_decode: entry length was zero\n", 0, 0, 0);
614 #ifdef SLAP_ZONE_ALLOC
615 x = slap_zn_calloc(1, i + bv->bv_len, ctx);
616 AC_MEMCPY((char*)x + i, bv->bv_val, bv->bv_len);
617 bv->bv_val = (char*)x + i;
618 ptr = (unsigned char *)bv->bv_val;
619 i = entry_getlen(&ptr);
623 i = entry_getlen(&ptr);
624 x->e_name.bv_val = (char *) ptr;
625 x->e_name.bv_len = i;
627 i = entry_getlen(&ptr);
628 x->e_nname.bv_val = (char *) ptr;
629 x->e_nname.bv_len = i;
631 Debug( LDAP_DEBUG_TRACE,
632 "entry_decode: \"%s\"\n",
636 /* A valid entry must have at least one attr, so this
637 * pointer can never be NULL
639 x->e_attrs = (Attribute *)(x+1);
640 bptr = (BerVarray)x->e_attrs;
643 while ((i = entry_getlen(&ptr))) {
646 bv.bv_val = (char *) ptr;
648 a->a_next = (Attribute *)bptr;
650 a = (Attribute *)bptr;
652 rc = slap_bv2ad( &bv, &ad, &text );
654 if( rc != LDAP_SUCCESS ) {
655 Debug( LDAP_DEBUG_TRACE,
656 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
657 rc = slap_bv2undef_ad( &bv, &ad, &text );
659 if( rc != LDAP_SUCCESS ) {
660 Debug( LDAP_DEBUG_ANY,
661 "<= entry_decode: str2undef_ad(%s): %s\n",
668 bptr = (BerVarray)(a+1);
671 #ifdef LDAP_COMP_MATCH
672 a->a_comp_data = NULL;
674 count = j = entry_getlen(&ptr);
677 i = entry_getlen(&ptr);
679 bptr->bv_val = (char *)ptr;
688 j = entry_getlen(&ptr);
692 i = entry_getlen(&ptr);
694 bptr->bv_val = (char *)ptr;
703 a->a_nvals = a->a_vals;
707 if (a) a->a_next = NULL;
708 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
714 Entry *entry_dup( Entry *e )
718 ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
721 ber_dupbv( &ret->e_name, &e->e_name );
722 ber_dupbv( &ret->e_nname, &e->e_nname );
723 ret->e_attrs = attrs_dup( e->e_attrs );
724 ret->e_ocflags = e->e_ocflags;
725 ret->e_bv.bv_val = NULL;
726 ret->e_bv.bv_len = 0;
727 ret->e_private = NULL;