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 );
68 struct berval vals[2];
69 struct berval nvals[2], *nvalsp;
70 AttributeDescription *ad, *ad_prev;
77 * LDIF is used as the string format.
78 * An entry looks like this:
81 * [<attr>:[:] <value>\n]
82 * [<tab><continuedvalue>\n]*
85 * If a double colon is used after a type, it means the
86 * following value is encoded as a base 64 string. This
87 * happens if the value contains a non-printing character
91 Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
92 s ? s : "NULL", 0, 0 );
94 /* initialize reader/writer lock */
95 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
98 Debug( LDAP_DEBUG_ANY,
99 "<= str2entry NULL (entry allocation failed)\n",
104 /* initialize entry */
107 /* dn + attributes */
109 vals[1].bv_val = NULL;
115 while ( (s = ldif_getline( &next )) != NULL ) {
116 if ( *s == '\n' || *s == '\0' ) {
120 if ( ldif_parse_line2( s, &type, vals, &freeval ) != 0 ) {
121 Debug( LDAP_DEBUG_TRACE,
122 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
126 if ( type.bv_len == dn_bv.bv_len &&
127 strcasecmp( type.bv_val, dn_bv.bv_val ) == 0 ) {
129 if ( e->e_dn != NULL ) {
130 Debug( LDAP_DEBUG_ANY, "str2entry: "
131 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
132 (long) e->e_id, e->e_dn, vals[0].bv_val );
133 if ( freeval ) free( vals[0].bv_val );
138 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname, NULL );
139 if ( freeval ) free( vals[0].bv_val );
140 if( rc != LDAP_SUCCESS ) {
141 Debug( LDAP_DEBUG_ANY, "str2entry: "
142 "entry %ld has invalid DN \"%s\"\n",
143 (long) e->e_id, vals[0].bv_val, 0 );
152 rc = slap_bv2ad( &type, &ad, &text );
154 if( rc != LDAP_SUCCESS ) {
155 Debug( slapMode & SLAP_TOOL_MODE
156 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
157 "<= str2entry: str2ad(%s): %s\n", type.bv_val, text, 0 );
158 if( slapMode & SLAP_TOOL_MODE ) {
160 if ( freeval ) free( vals[0].bv_val );
164 rc = slap_bv2undef_ad( &type, &ad, &text );
165 if( rc != LDAP_SUCCESS ) {
166 Debug( LDAP_DEBUG_ANY,
167 "<= str2entry: str2undef_ad(%s): %s\n",
168 type.bv_val, text, 0 );
170 if ( freeval ) free( vals[0].bv_val );
175 if ( ad != ad_prev ) {
179 if( slapMode & SLAP_TOOL_MODE ) {
181 slap_syntax_validate_func *validate =
182 ad->ad_type->sat_syntax->ssyn_validate;
183 slap_syntax_transform_func *pretty =
184 ad->ad_type->sat_syntax->ssyn_pretty;
187 rc = pretty( ad->ad_type->sat_syntax,
188 &vals[0], &pval, NULL );
190 } else if( validate ) {
192 * validate value per syntax
194 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
197 Debug( LDAP_DEBUG_ANY,
198 "str2entry: attributeType %s #%d: "
199 "no validator for syntax %s\n",
200 ad->ad_cname.bv_val, attr_cnt,
201 ad->ad_type->sat_syntax->ssyn_oid );
203 if ( freeval ) free( vals[0].bv_val );
208 Debug( LDAP_DEBUG_ANY,
209 "str2entry: invalid value "
210 "for attributeType %s #%d (syntax %s)\n",
211 ad->ad_cname.bv_val, attr_cnt,
212 ad->ad_type->sat_syntax->ssyn_oid );
214 if ( freeval ) free( vals[0].bv_val );
219 if ( freeval ) free( vals[0].bv_val );
226 nvals[0].bv_val = NULL;
228 if( ad->ad_type->sat_equality &&
229 ad->ad_type->sat_equality->smr_normalize )
231 rc = ad->ad_type->sat_equality->smr_normalize(
232 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
233 ad->ad_type->sat_syntax,
234 ad->ad_type->sat_equality,
235 &vals[0], &nvals[0], NULL );
238 Debug( LDAP_DEBUG_ANY,
239 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
242 if ( freeval ) free( vals[0].bv_val );
247 nvals[1].bv_val = NULL;
252 rc = attr_merge( e, ad, vals, nvalsp );
254 Debug( LDAP_DEBUG_ANY,
255 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
257 if ( freeval ) free( vals[0].bv_val );
261 if ( freeval ) free( vals[0].bv_val );
262 free( nvals[0].bv_val );
267 /* check to make sure there was a dn: line */
268 if ( e->e_dn == NULL ) {
269 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
270 (long) e->e_id, 0, 0 );
275 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
276 e->e_dn, (unsigned long) e, 0 );
281 #define GRABSIZE BUFSIZ
283 #define MAKE_SPACE( n ) { \
284 while ( ecur + (n) > ebuf + emaxsize ) { \
286 offset = (int) (ecur - ebuf); \
287 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
288 emaxsize + GRABSIZE ); \
289 emaxsize += GRABSIZE; \
290 ecur = ebuf + offset; \
307 * In string format, an entry looks like this:
309 * [<attr>: <value>\n]*
315 if ( e->e_dn != NULL ) {
317 tmplen = e->e_name.bv_len;
318 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
319 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
322 /* put the attributes */
323 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
324 /* put "<type>:[:] <value>" line for each value */
325 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
327 tmplen = a->a_desc->ad_cname.bv_len;
328 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
329 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
330 a->a_desc->ad_cname.bv_val,
331 bv->bv_val, bv->bv_len );
338 return( (char *) ebuf );
342 entry_clean( Entry *e )
344 /* free an entry structure */
347 /* e_private must be freed by the caller */
348 assert( e->e_private == NULL );
352 if ( !BER_BVISNULL( &e->e_name ) ) {
353 free( e->e_name.bv_val );
354 BER_BVZERO( &e->e_name );
356 if ( !BER_BVISNULL( &e->e_nname ) ) {
357 free( e->e_nname.bv_val );
358 BER_BVZERO( &e->e_nname );
361 if ( !BER_BVISNULL( &e->e_bv ) ) {
362 free( e->e_bv.bv_val );
363 BER_BVZERO( &e->e_bv );
366 /* free attributes */
367 attrs_free( e->e_attrs );
372 entry_free( Entry *e )
380 * These routines are used only by Backend.
382 * the Entry has three entry points (ways to find things):
384 * by entry e.g., if you already have an entry from the cache
385 * and want to delete it. (really by entry ptr)
386 * by dn e.g., when looking for the base object of a search
387 * by id e.g., for search candidates
389 * these correspond to three different avl trees that are maintained.
393 entry_cmp( Entry *e1, Entry *e2 )
395 return SLAP_PTRCMP( e1, e2 );
399 entry_dn_cmp( const void *v_e1, const void *v_e2 )
401 /* compare their normalized UPPERCASED dn's */
402 const Entry *e1 = v_e1, *e2 = v_e2;
404 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
408 entry_id_cmp( const void *v_e1, const void *v_e2 )
410 const Entry *e1 = v_e1, *e2 = v_e2;
411 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
414 #define entry_lenlen(l) ((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
415 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5
417 /* This is like a ber_len */
419 entry_lenlen(ber_len_t len)
434 entry_putlen(unsigned char **buf, ber_len_t len)
436 ber_len_t lenlen = entry_lenlen(len);
439 **buf = (unsigned char) len;
442 **buf = 0x80 | ((unsigned char) lenlen - 1);
443 for (i=lenlen-1; i>0; i--) {
444 (*buf)[i] = (unsigned char) len;
452 entry_getlen(unsigned char **buf)
469 /* Add up the size of the entry for a flattened buffer */
470 void entry_flatsize(Entry *e, ber_len_t *psiz, ber_len_t *plen, int norm)
472 ber_len_t siz = sizeof(Entry);
473 ber_len_t len, dnlen, ndnlen;
477 dnlen = e->e_name.bv_len;
478 len = dnlen + 1; /* trailing NUL byte */
479 len += entry_lenlen(dnlen);
481 ndnlen = e->e_nname.bv_len;
483 len += entry_lenlen(ndnlen);
485 for (a=e->e_attrs; a; a=a->a_next) {
486 /* For AttributeDesc, we only store the attr name */
487 siz += sizeof(Attribute);
488 len += a->a_desc->ad_cname.bv_len+1;
489 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
490 for (i=0; a->a_vals[i].bv_val; i++) {
491 siz += sizeof(struct berval);
492 len += a->a_vals[i].bv_len + 1;
493 len += entry_lenlen(a->a_vals[i].bv_len);
495 len += entry_lenlen(i);
496 siz += sizeof(struct berval); /* empty berval at end */
497 if (norm && a->a_nvals != a->a_vals) {
498 for (i=0; a->a_nvals[i].bv_val; i++) {
499 siz += sizeof(struct berval);
500 len += a->a_nvals[i].bv_len + 1;
501 len += entry_lenlen(a->a_nvals[i].bv_len);
503 len += entry_lenlen(i); /* i nvals */
504 siz += sizeof(struct berval);
506 len += entry_lenlen(0); /* 0 nvals */
509 len += 1; /* NUL byte at end */
510 len += entry_lenlen(siz);
515 /* Flatten an Entry into a buffer. The buffer is filled with just the
516 * strings/bervals of all the entry components. Each field is preceded
517 * by its length, encoded the way ber_put_len works. Every field is NUL
518 * terminated. The entire buffer size is precomputed so that a single
519 * malloc can be performed. The entry size is also recorded,
520 * to aid in entry_decode.
522 int entry_encode(Entry *e, struct berval *bv)
524 ber_len_t siz = sizeof(Entry);
525 ber_len_t len, dnlen, ndnlen;
530 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
531 (long) e->e_id, e->e_dn, 0 );
532 dnlen = e->e_name.bv_len;
533 ndnlen = e->e_nname.bv_len;
535 entry_flatsize( e, &siz, &len, 1 );
538 bv->bv_val = ch_malloc(len);
539 ptr = (unsigned char *)bv->bv_val;
540 entry_putlen(&ptr, siz);
541 entry_putlen(&ptr, dnlen);
542 AC_MEMCPY(ptr, e->e_dn, dnlen);
545 entry_putlen(&ptr, ndnlen);
546 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
550 for (a=e->e_attrs; a; a=a->a_next) {
551 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
552 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
553 a->a_desc->ad_cname.bv_len);
554 ptr += a->a_desc->ad_cname.bv_len;
557 for (i=0; a->a_vals[i].bv_val; i++);
558 entry_putlen(&ptr, i);
559 for (i=0; a->a_vals[i].bv_val; i++) {
560 entry_putlen(&ptr, a->a_vals[i].bv_len);
561 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
562 a->a_vals[i].bv_len);
563 ptr += a->a_vals[i].bv_len;
566 if (a->a_nvals != a->a_vals) {
567 entry_putlen(&ptr, i);
568 for (i=0; a->a_nvals[i].bv_val; i++) {
569 entry_putlen(&ptr, a->a_nvals[i].bv_len);
570 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
571 a->a_nvals[i].bv_len);
572 ptr += a->a_nvals[i].bv_len;
576 entry_putlen(&ptr, 0);
584 /* Retrieve an Entry that was stored using entry_encode above.
585 * We malloc a single block with the size stored above for the Entry
586 * and all of its Attributes. We also must lookup the stored
587 * attribute names to get AttributeDescriptions. To detect if the
588 * attributes of an Entry are later modified, we note that e->e_attr
589 * is always a constant offset from (e).
591 * Note: everything is stored in a single contiguous block, so
592 * you can not free individual attributes or names from this
593 * structure. Attempting to do so will likely corrupt memory.
595 #ifdef SLAP_ZONE_ALLOC
596 int entry_decode(struct berval *bv, Entry **e, void *ctx)
598 int entry_decode(struct berval *bv, Entry **e)
606 AttributeDescription *ad;
607 unsigned char *ptr = (unsigned char *)bv->bv_val;
610 i = entry_getlen(&ptr);
612 Debug( LDAP_DEBUG_ANY,
613 "entry_decode: entry length was zero\n", 0, 0, 0);
616 #ifdef SLAP_ZONE_ALLOC
617 x = slap_zn_calloc(1, i + bv->bv_len, ctx);
618 AC_MEMCPY((char*)x + i, bv->bv_val, bv->bv_len);
619 bv->bv_val = (char*)x + i;
620 ptr = (unsigned char *)bv->bv_val;
621 i = entry_getlen(&ptr);
625 i = entry_getlen(&ptr);
626 x->e_name.bv_val = (char *) ptr;
627 x->e_name.bv_len = i;
629 i = entry_getlen(&ptr);
630 x->e_nname.bv_val = (char *) ptr;
631 x->e_nname.bv_len = i;
633 Debug( LDAP_DEBUG_TRACE,
634 "entry_decode: \"%s\"\n",
638 /* A valid entry must have at least one attr, so this
639 * pointer can never be NULL
641 x->e_attrs = (Attribute *)(x+1);
642 bptr = (BerVarray)x->e_attrs;
645 while ((i = entry_getlen(&ptr))) {
648 bv.bv_val = (char *) ptr;
650 a->a_next = (Attribute *)bptr;
652 a = (Attribute *)bptr;
654 rc = slap_bv2ad( &bv, &ad, &text );
656 if( rc != LDAP_SUCCESS ) {
657 Debug( LDAP_DEBUG_TRACE,
658 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
659 rc = slap_bv2undef_ad( &bv, &ad, &text );
661 if( rc != LDAP_SUCCESS ) {
662 Debug( LDAP_DEBUG_ANY,
663 "<= entry_decode: str2undef_ad(%s): %s\n",
670 bptr = (BerVarray)(a+1);
673 #ifdef LDAP_COMP_MATCH
674 a->a_comp_data = NULL;
676 count = j = entry_getlen(&ptr);
679 i = entry_getlen(&ptr);
681 bptr->bv_val = (char *)ptr;
690 j = entry_getlen(&ptr);
694 i = entry_getlen(&ptr);
696 bptr->bv_val = (char *)ptr;
705 a->a_nvals = a->a_vals;
709 if (a) a->a_next = NULL;
710 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
716 Entry *entry_dup( Entry *e )
720 ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
723 ber_dupbv( &ret->e_name, &e->e_name );
724 ber_dupbv( &ret->e_nname, &e->e_nname );
725 ret->e_attrs = attrs_dup( e->e_attrs );
726 ret->e_ocflags = e->e_ocflags;
727 ret->e_bv.bv_val = NULL;
728 ret->e_bv.bv_len = 0;
729 ret->e_private = NULL;