1 /* entry.c - routines for dealing with entries */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
14 #include <ac/socket.h>
15 #include <ac/string.h>
20 static unsigned char *ebuf; /* buf returned by entry2str */
21 static unsigned char *ecur; /* pointer to end of currently used ebuf */
22 static int emaxsize;/* max size of ebuf */
27 const Entry slap_entry_root = {
28 NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL
31 int entry_destroy(void)
33 if ( ebuf ) free( ebuf );
47 struct berval vals[2];
49 struct berval nvals[2], *nvalsp;
51 AttributeDescription *ad;
56 * LDIF is used as the string format.
57 * An entry looks like this:
60 * [<attr>:[:] <value>\n]
61 * [<tab><continuedvalue>\n]*
64 * If a double colon is used after a type, it means the
65 * following value is encoded as a base 64 string. This
66 * happens if the value contains a non-printing character
71 LDAP_LOG( OPERATION, DETAIL1, "str2entry: \"%s\"\n", s ? s : "NULL", 0, 0 );
73 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
74 s ? s : "NULL", 0, 0 );
77 /* initialize reader/writer lock */
78 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
82 LDAP_LOG( OPERATION, ERR, "str2entry: entry allocation failed.\n", 0, 0, 0 );
84 Debug( LDAP_DEBUG_ANY,
85 "<= str2entry NULL (entry allocation failed)\n",
91 /* initialize entry */
96 vals[1].bv_val = NULL;
99 while ( (s = ldif_getline( &next )) != NULL ) {
100 if ( *s == '\n' || *s == '\0' ) {
104 if ( ldif_parse_line( s, &type, &vals[0].bv_val, &vals[0].bv_len ) != 0 ) {
106 LDAP_LOG( OPERATION, DETAIL1, "str2entry: NULL (parse_line)\n",0, 0, 0 );
108 Debug( LDAP_DEBUG_TRACE,
109 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
114 if ( strcasecmp( type, "dn" ) == 0 ) {
117 if ( e->e_dn != NULL ) {
119 LDAP_LOG( OPERATION, DETAIL1, "str2entry: "
120 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
121 (long) e->e_id, e->e_dn, vals[0].bv_val );
123 Debug( LDAP_DEBUG_ANY, "str2entry: "
124 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
125 (long) e->e_id, e->e_dn, vals[0].bv_val );
127 free( vals[0].bv_val );
132 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname );
133 if( rc != LDAP_SUCCESS ) {
135 LDAP_LOG( OPERATION, DETAIL1,
136 "str2entry: entry %ld has invalid DN \"%s\"\n",
137 (long) e->e_id, vals[0].bv_val, 0 );
139 Debug( LDAP_DEBUG_ANY, "str2entry: "
140 "entry %ld has invalid DN \"%s\"\n",
141 (long) e->e_id, vals[0].bv_val, 0 );
144 free( vals[0].bv_val );
147 free( vals[0].bv_val );
152 rc = slap_str2ad( type, &ad, &text );
154 if( rc != LDAP_SUCCESS ) {
156 LDAP_LOG( OPERATION, DETAIL1,
157 "str2entry: str2ad(%s): %s\n", type, text, 0 );
159 Debug( slapMode & SLAP_TOOL_MODE
160 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
161 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
163 if( slapMode & SLAP_TOOL_MODE ) {
165 free( vals[0].bv_val );
170 rc = slap_str2undef_ad( type, &ad, &text );
171 if( rc != LDAP_SUCCESS ) {
173 LDAP_LOG( OPERATION, DETAIL1,
174 "str2entry: str2undef_ad(%s): %s\n", type, text, 0 );
176 Debug( LDAP_DEBUG_ANY,
177 "<= str2entry: str2undef_ad(%s): %s\n",
181 free( vals[0].bv_val );
187 if( slapMode & SLAP_TOOL_MODE ) {
189 slap_syntax_validate_func *validate =
190 ad->ad_type->sat_syntax->ssyn_validate;
191 slap_syntax_transform_func *pretty =
192 ad->ad_type->sat_syntax->ssyn_pretty;
195 rc = pretty( ad->ad_type->sat_syntax,
198 } else if( validate ) {
200 * validate value per syntax
202 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
206 LDAP_LOG( OPERATION, INFO,
207 "str2entry: no validator for syntax %s\n",
208 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
210 Debug( LDAP_DEBUG_ANY,
211 "str2entry: no validator for syntax %s\n",
212 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
215 free( vals[0].bv_val );
222 LDAP_LOG( OPERATION, ERR,
223 "str2entry: invalid value for syntax %s\n",
224 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
226 Debug( LDAP_DEBUG_ANY,
227 "str2entry: invalid value for syntax %s\n",
228 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
231 free( vals[0].bv_val );
237 free( vals[0].bv_val );
244 nvals[0].bv_val = NULL;
246 if( ad->ad_type->sat_equality &&
247 ad->ad_type->sat_equality->smr_normalize )
249 rc = ad->ad_type->sat_equality->smr_normalize(
250 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
251 ad->ad_type->sat_syntax,
252 ad->ad_type->sat_equality,
253 &vals[0], &nvals[0] );
257 LDAP_LOG( OPERATION, DETAIL1,
258 "str2entry: NULL (smr_normalize %d)\n" , rc, 0, 0 );
260 Debug( LDAP_DEBUG_ANY,
261 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
265 free( vals[0].bv_val );
271 nvals[1].bv_val = NULL;
278 rc = attr_merge( e, ad, vals, nvalsp );
280 rc = attr_merge( e, ad, vals );
284 LDAP_LOG( OPERATION, DETAIL1,
285 "str2entry: NULL (attr_merge)\n" , 0, 0, 0 );
287 Debug( LDAP_DEBUG_ANY,
288 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
291 free( vals[0].bv_val );
297 free( vals[0].bv_val );
299 free( nvals[0].bv_val );
303 /* check to make sure there was a dn: line */
304 if ( e->e_dn == NULL ) {
306 LDAP_LOG( OPERATION, INFO,
307 "str2entry: entry %ld has no dn.\n", (long) e->e_id, 0, 0 );
309 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
310 (long) e->e_id, 0, 0 );
317 LDAP_LOG( OPERATION, DETAIL2,
318 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e, 0 );
320 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
321 e->e_dn, (unsigned long) e, 0 );
327 #define GRABSIZE BUFSIZ
329 #define MAKE_SPACE( n ) { \
330 while ( ecur + (n) > ebuf + emaxsize ) { \
332 offset = (int) (ecur - ebuf); \
333 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
334 emaxsize + GRABSIZE ); \
335 emaxsize += GRABSIZE; \
336 ecur = ebuf + offset; \
353 * In string format, an entry looks like this:
355 * [<attr>: <value>\n]*
361 if ( e->e_dn != NULL ) {
363 tmplen = e->e_name.bv_len;
364 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
365 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
368 /* put the attributes */
369 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
370 /* put "<type>:[:] <value>" line for each value */
371 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
373 tmplen = a->a_desc->ad_cname.bv_len;
374 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
375 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
376 a->a_desc->ad_cname.bv_val,
377 bv->bv_val, bv->bv_len );
384 return( (char *) ebuf );
388 entry_free( Entry *e )
390 /* free an entry structure */
393 /* e_private must be freed by the caller */
394 assert( e->e_private == NULL );
398 if ( e->e_dn != NULL ) {
402 if ( e->e_ndn != NULL ) {
407 if ( e->e_bv.bv_val != NULL ) {
408 free( e->e_bv.bv_val );
409 e->e_bv.bv_val = NULL;
412 /* free attributes */
413 attrs_free( e->e_attrs );
420 * These routines are used only by Backend.
422 * the Entry has three entry points (ways to find things):
424 * by entry e.g., if you already have an entry from the cache
425 * and want to delete it. (really by entry ptr)
426 * by dn e.g., when looking for the base object of a search
427 * by id e.g., for search candidates
429 * these correspond to three different avl trees that are maintained.
433 entry_cmp( Entry *e1, Entry *e2 )
435 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
439 entry_dn_cmp( const void *v_e1, const void *v_e2 )
441 /* compare their normalized UPPERCASED dn's */
442 const Entry *e1 = v_e1, *e2 = v_e2;
443 int rc = e1->e_nname.bv_len - e2->e_nname.bv_len;
445 return( strcmp( e1->e_ndn, e2->e_ndn ) );
449 entry_id_cmp( const void *v_e1, const void *v_e2 )
451 const Entry *e1 = v_e1, *e2 = v_e2;
452 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
457 /* This is like a ber_len */
459 entry_lenlen(ber_len_t len)
473 entry_putlen(unsigned char **buf, ber_len_t len)
475 ber_len_t lenlen = entry_lenlen(len);
478 **buf = (unsigned char) len;
481 **buf = 0x80 | ((unsigned char) lenlen - 1);
482 for (i=lenlen-1; i>0; i--) {
483 (*buf)[i] = (unsigned char) len;
491 entry_getlen(unsigned char **buf)
508 /* Flatten an Entry into a buffer. The buffer is filled with just the
509 * strings/bervals of all the entry components. Each field is preceded
510 * by its length, encoded the way ber_put_len works. Every field is NUL
511 * terminated. The entire buffer size is precomputed so that a single
512 * malloc can be performed. The entry size is also recorded,
513 * to aid in entry_decode.
515 int entry_encode(Entry *e, struct berval *bv)
517 ber_len_t siz = sizeof(Entry);
518 ber_len_t len, dnlen, ndnlen;
524 LDAP_LOG( OPERATION, DETAIL1, "entry_encode: id: 0x%08lx \"%s\"\n",
525 (long) e->e_id, e->e_dn, 0 );
527 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
528 (long) e->e_id, e->e_dn, 0 );
530 dnlen = e->e_name.bv_len;
531 ndnlen = e->e_nname.bv_len;
532 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
533 len += entry_lenlen(dnlen);
534 len += entry_lenlen(ndnlen);
535 for (a=e->e_attrs; a; a=a->a_next) {
536 /* For AttributeDesc, we only store the attr name */
537 siz += sizeof(Attribute);
538 len += a->a_desc->ad_cname.bv_len+1;
539 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
540 for (i=0; a->a_vals[i].bv_val; i++) {
541 siz += sizeof(struct berval);
542 len += a->a_vals[i].bv_len + 1;
543 len += entry_lenlen(a->a_vals[i].bv_len);
545 len += entry_lenlen(i);
546 siz += sizeof(struct berval); /* empty berval at end */
548 len += 1; /* NUL byte at end */
549 len += entry_lenlen(siz);
551 bv->bv_val = ch_malloc(len);
552 ptr = (unsigned char *)bv->bv_val;
553 entry_putlen(&ptr, siz);
554 entry_putlen(&ptr, dnlen);
555 AC_MEMCPY(ptr, e->e_dn, dnlen);
558 entry_putlen(&ptr, ndnlen);
559 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
563 for (a=e->e_attrs; a; a=a->a_next) {
564 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
565 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
566 a->a_desc->ad_cname.bv_len);
567 ptr += a->a_desc->ad_cname.bv_len;
570 for (i=0; a->a_vals[i].bv_val; i++);
571 entry_putlen(&ptr, i);
572 for (i=0; a->a_vals[i].bv_val; i++) {
573 entry_putlen(&ptr, a->a_vals[i].bv_len);
574 memcpy(ptr, a->a_vals[i].bv_val,
575 a->a_vals[i].bv_len);
576 ptr += a->a_vals[i].bv_len;
585 /* Retrieve an Entry that was stored using entry_encode above.
586 * We malloc a single block with the size stored above for the Entry
587 * and all if its Attributes. We also must lookup the stored
588 * attribute names to get AttributeDescriptions. To detect if the
589 * attributes of an Entry are later modified, we note that e->e_attr
590 * is always a constant offset from (e).
592 * Note: everything is stored in a single contiguous block, so
593 * you can not free individual attributes or names from this
594 * structure. Attempting to do so will likely corrupt memory.
596 int entry_decode(struct berval *bv, Entry **e)
603 AttributeDescription *ad;
604 unsigned char *ptr = (unsigned char *)bv->bv_val;
607 i = entry_getlen(&ptr);
609 i = entry_getlen(&ptr);
610 x->e_name.bv_val = ptr;
611 x->e_name.bv_len = i;
613 i = entry_getlen(&ptr);
614 x->e_nname.bv_val = ptr;
615 x->e_nname.bv_len = i;
618 LDAP_LOG( OPERATION, DETAIL2, "entry_decode: \"%s\"\n", x->e_dn, 0, 0 );
620 Debug( LDAP_DEBUG_TRACE,
621 "entry_decode: \"%s\"\n",
626 /* A valid entry must have at least one attr, so this
627 * pointer can never be NULL
629 x->e_attrs = (Attribute *)(x+1);
630 bptr = (BerVarray)x->e_attrs;
633 while ((i = entry_getlen(&ptr))) {
638 a->a_next = (Attribute *)bptr;
640 a = (Attribute *)bptr;
642 rc = slap_bv2ad( &bv, &ad, &text );
644 if( rc != LDAP_SUCCESS ) {
646 LDAP_LOG( OPERATION, INFO,
647 "entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
649 Debug( LDAP_DEBUG_TRACE,
650 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
652 rc = slap_bv2undef_ad( &bv, &ad, &text );
654 if( rc != LDAP_SUCCESS ) {
656 LDAP_LOG( OPERATION, INFO,
657 "entry_decode: str2undef_ad(%s): %s\n", ptr, text, 0 );
659 Debug( LDAP_DEBUG_ANY,
660 "<= entry_decode: str2undef_ad(%s): %s\n",
668 bptr = (BerVarray)(a+1);
671 count = j = entry_getlen(&ptr);
674 i = entry_getlen(&ptr);
676 bptr->bv_val = (char *)ptr;
686 if( count && ad->ad_type->sat_equality &&
687 ad->ad_type->sat_equality->smr_normalize )
689 a->a_nvals = ch_malloc((count+1)*sizeof(struct berval));
691 for(j=0; j<count; j++) {
692 rc = ad->ad_type->sat_equality->smr_normalize(
693 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
694 ad->ad_type->sat_syntax,
695 ad->ad_type->sat_equality,
696 &a->a_vals[j], &a->a_nvals[j] );
700 LDAP_LOG( OPERATION, DETAIL1,
701 "entry_decode: NULL (smr_normalize %d)\n",
704 Debug( LDAP_DEBUG_ANY,
705 "<= entry_decode NULL (smr_normalize %d)\n",
712 a->a_nvals[j].bv_val = NULL;
713 a->a_nvals[j].bv_len = 0;
719 if (a) a->a_next = NULL;
721 LDAP_LOG( OPERATION, DETAIL1, "entry_decode: %s\n", x->e_dn, 0, 0 );
723 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",