1 /* entry.c - routines for dealing with entries */
4 * Copyright 1998-2000 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 = { NOID, "", "", NULL, NULL };
29 int entry_destroy(void)
47 struct berval *vals[2];
48 AttributeDescription *ad;
53 * LDIF is used as the string format.
54 * An entry looks like this:
57 * [<attr>:[:] <value>\n]
58 * [<tab><continuedvalue>\n]*
61 * If a double colon is used after a type, it means the
62 * following value is encoded as a base 64 string. This
63 * happens if the value contains a non-printing character
68 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
69 "str2entry: \"%s\"\n", s ? s : "NULL" ));
71 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
72 s ? s : "NULL", 0, 0 );
75 /* initialize reader/writer lock */
76 e = (Entry *) ch_malloc( sizeof(Entry) );
80 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
81 "str2entry: entry allocation failed.\n" ));
83 Debug( LDAP_DEBUG_ANY,
84 "<= str2entry NULL (entry allocation failed)\n",
90 /* initialize entry */
102 while ( (s = ldif_getline( &next )) != NULL ) {
103 if ( *s == '\n' || *s == '\0' ) {
107 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
109 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
110 "str2entry: NULL (parse_line)\n" ));
112 Debug( LDAP_DEBUG_TRACE,
113 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
118 if ( strcasecmp( type, "dn" ) == 0 ) {
123 if ( e->e_dn != NULL ) {
125 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
126 "entry %ld has multiple dns \"%s\" and \"%s\"\n",
127 (long) e->e_id, e->e_dn,
128 value.bv_val != NULL ? value.bv_val : "" ));
130 Debug( LDAP_DEBUG_ANY, "str2entry: "
131 "entry %ld has multiple dns \"%s\" and \"%s\"\n",
132 (long) e->e_id, e->e_dn,
133 value.bv_val != NULL ? value.bv_val : "" );
135 if( value.bv_val != NULL ) free( value.bv_val );
140 rc = dnPretty( NULL, &value, &pdn );
141 if( rc != LDAP_SUCCESS ) {
143 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
144 "entry %ld has invalid DN \"%s\"\n",
146 pdn->bv_val ? pdn->bv_val : "" ));
148 Debug( LDAP_DEBUG_ANY, "str2entry: "
149 "entry %ld has invalid DN \"%s\"\n",
151 pdn->bv_val ? pdn->bv_val : "", 0 );
153 if( value.bv_val != NULL ) free( value.bv_val );
158 e->e_dn = pdn->bv_val != NULL ? pdn->bv_val : ch_strdup( "" );
165 rc = slap_str2ad( type, &ad, &text );
167 if( rc != LDAP_SUCCESS ) {
169 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
170 "str2entry: str2ad(%s): %s\n", type, text ));
172 Debug( slapMode & SLAP_TOOL_MODE
173 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
174 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
176 if( slapMode & SLAP_TOOL_MODE ) {
178 free( value.bv_val );
183 rc = slap_str2undef_ad( type, &ad, &text );
184 if( rc != LDAP_SUCCESS ) {
186 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
187 "str2entry: str2undef_ad(%s): %s\n", type, text ));
189 Debug( LDAP_DEBUG_ANY,
190 "<= str2entry: str2undef_ad(%s): %s\n",
194 free( value.bv_val );
200 if( slapMode & SLAP_TOOL_MODE ) {
202 slap_syntax_validate_func *validate =
203 ad->ad_type->sat_syntax->ssyn_validate;
204 slap_syntax_transform_func *pretty =
205 ad->ad_type->sat_syntax->ssyn_pretty;
208 rc = pretty( ad->ad_type->sat_syntax,
211 } else if( validate ) {
213 * validate value per syntax
215 rc = validate( ad->ad_type->sat_syntax, &value );
219 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
220 "str2entry: no validator for syntax %s\n",
221 ad->ad_type->sat_syntax->ssyn_oid ));
223 Debug( LDAP_DEBUG_ANY,
224 "str2entry: no validator for syntax %s\n",
225 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
228 free( value.bv_val );
235 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
236 "str2entry: invalid value for syntax %s\n",
237 ad->ad_type->sat_syntax->ssyn_oid ));
239 Debug( LDAP_DEBUG_ANY,
240 "str2entry: invalid value for syntax %s\n",
241 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
244 free( value.bv_val );
250 free( value.bv_val );
256 rc = attr_merge( e, ad, vals );
260 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
261 "str2entry: NULL (attr_merge)\n" ));
263 Debug( LDAP_DEBUG_ANY,
264 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
267 free( value.bv_val );
273 free( value.bv_val );
276 /* check to make sure there was a dn: line */
277 if ( e->e_dn == NULL ) {
279 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
280 "str2entry: entry %ld has no dn.\n",
283 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
284 (long) e->e_id, 0, 0 );
290 /* generate normalized dn */
298 rc = dnNormalize( NULL, &dn, &ndn );
300 if( rc != LDAP_SUCCESS ) {
302 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
303 "str2entry: entry %ld has invalid dn: %s\n",
304 (long) e->e_id, e->e_ndn ));
306 Debug( LDAP_DEBUG_ANY,
307 "str2entry: entry %ld has invalid dn: %s\n",
308 (long) e->e_id, e->e_ndn, 0 );
314 e->e_ndn = ndn->bv_val;
319 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
320 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
322 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
323 e->e_dn, (unsigned long) e, 0 );
329 #define GRABSIZE BUFSIZ
331 #define MAKE_SPACE( n ) { \
332 while ( ecur + (n) > ebuf + emaxsize ) { \
334 offset = (int) (ecur - ebuf); \
335 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
336 emaxsize + GRABSIZE ); \
337 emaxsize += GRABSIZE; \
338 ecur = ebuf + offset; \
352 * In string format, an entry looks like this:
354 * [<attr>: <value>\n]*
360 if ( e->e_dn != NULL ) {
362 tmplen = strlen( e->e_dn );
363 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
364 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
367 /* put the attributes */
368 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
369 /* put "<type>:[:] <value>" line for each value */
370 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
372 tmplen = a->a_desc->ad_cname.bv_len;
373 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
374 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
375 a->a_desc->ad_cname.bv_val,
376 bv->bv_val, bv->bv_len );
383 return( (char *) ebuf );
387 entry_free( Entry *e )
389 /* free an entry structure */
392 /* e_private must be freed by the caller */
393 assert( e->e_private == NULL );
397 if ( e->e_dn != NULL ) {
401 if ( e->e_ndn != NULL ) {
406 /* free attributes */
407 attrs_free( e->e_attrs );
414 * These routines are used only by Backend.
416 * the Entry has three entry points (ways to find things):
418 * by entry e.g., if you already have an entry from the cache
419 * and want to delete it. (really by entry ptr)
420 * by dn e.g., when looking for the base object of a search
421 * by id e.g., for search candidates
423 * these correspond to three different avl trees that are maintained.
427 entry_cmp( Entry *e1, Entry *e2 )
429 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
433 entry_dn_cmp( Entry *e1, Entry *e2 )
435 /* compare their normalized UPPERCASED dn's */
436 return( strcmp( e1->e_ndn, e2->e_ndn ) );
440 entry_id_cmp( Entry *e1, Entry *e2 )
442 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
447 /* This is like a ber_len */
449 entry_lenlen(ber_len_t len)
463 entry_putlen(unsigned char **buf, ber_len_t len)
465 ber_len_t lenlen = entry_lenlen(len);
468 **buf = (unsigned char) len;
471 **buf = 0x80 | ((unsigned char) lenlen - 1);
472 for (i=lenlen-1; i>0; i--) {
473 (*buf)[i] = (unsigned char) len;
481 entry_getlen(unsigned char **buf)
498 /* Flatten an Entry into a buffer. The buffer is filled with just the
499 * strings/bervals of all the entry components. Each field is preceded
500 * by its length, encoded the way ber_put_len works. Every field is NUL
501 * terminated. The entire buffer size is precomputed so that a single
502 * malloc can be performed. The entry size is also recorded,
503 * to aid in entry_decode.
505 int entry_encode(Entry *e, struct berval *bv)
507 int siz = sizeof(Entry);
508 int len, dnlen, ndnlen;
514 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
515 "entry_encode: id: 0x%08lx \"%s\"\n",
516 (long) e->e_id, e->e_dn ));
518 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
519 (long) e->e_id, e->e_dn, 0 );
521 dnlen = strlen(e->e_dn);
522 ndnlen = strlen(e->e_ndn);
523 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
524 len += entry_lenlen(dnlen);
525 len += entry_lenlen(ndnlen);
526 for (a=e->e_attrs; a; a=a->a_next) {
527 /* For AttributeDesc, we only store the attr name */
528 siz += sizeof(Attribute);
529 len += a->a_desc->ad_cname.bv_len+1;
530 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
531 for (i=0; a->a_vals[i]; i++) {
532 siz += sizeof(struct berval *);
533 siz += sizeof(struct berval);
534 len += a->a_vals[i]->bv_len + 1;
535 len += entry_lenlen(a->a_vals[i]->bv_len);
537 len += entry_lenlen(i);
538 siz += sizeof(struct berval *); /* NULL pointer at end */
540 len += 1; /* NUL byte at end */
541 len += entry_lenlen(siz);
543 bv->bv_val = ch_malloc(len);
544 ptr = (unsigned char *)bv->bv_val;
545 entry_putlen(&ptr, siz);
546 entry_putlen(&ptr, dnlen);
547 memcpy(ptr, e->e_dn, dnlen);
550 entry_putlen(&ptr, ndnlen);
551 memcpy(ptr, e->e_ndn, ndnlen);
555 for (a=e->e_attrs; a; a=a->a_next) {
556 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
557 memcpy(ptr, a->a_desc->ad_cname.bv_val,
558 a->a_desc->ad_cname.bv_len);
559 ptr += a->a_desc->ad_cname.bv_len;
562 for (i=0; a->a_vals[i]; i++);
563 entry_putlen(&ptr, i);
564 for (i=0; a->a_vals[i]; i++) {
565 entry_putlen(&ptr, a->a_vals[i]->bv_len);
566 memcpy(ptr, a->a_vals[i]->bv_val,
567 a->a_vals[i]->bv_len);
568 ptr += a->a_vals[i]->bv_len;
577 /* Retrieve an Entry that was stored using entry_encode above.
578 * We malloc a single block with the size stored above for the Entry
579 * and all if its Attributes. We also must lookup the stored
580 * attribute names to get AttributeDescriptions. To detect if the
581 * attributes of an Entry are later modified, we note that e->e_attr
582 * is always a constant offset from (e).
584 * Note: everything is stored in a single contiguous block, so
585 * you can not free individual attributes or names from this
586 * structure. Attempting to do so will likely corrupt memory.
588 int entry_decode(struct berval *bv, Entry **e)
595 AttributeDescription *ad;
596 unsigned char *ptr = (unsigned char *)bv->bv_val;
597 struct berval **bptr;
600 i = entry_getlen(&ptr);
602 i = entry_getlen(&ptr);
605 i = entry_getlen(&ptr);
609 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
610 "entry_decode: \"%s\"\n", x->e_dn ));
612 Debug( LDAP_DEBUG_TRACE,
613 "entry_decode: \"%s\"\n",
616 x->e_private = bv->bv_val;
618 /* A valid entry must have at least one attr, so this
619 * pointer can never be NULL
621 x->e_attrs = (Attribute *)(x+1);
622 bptr = (struct berval **)x->e_attrs;
625 while (i = entry_getlen(&ptr)) {
627 a->a_next = (Attribute *)bptr;
629 a = (Attribute *)bptr;
631 rc = slap_str2ad( ptr, &ad, &text );
633 if( rc != LDAP_SUCCESS ) {
635 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
636 "entry_decode: str2ad(%s): %s\n", ptr, text ));
638 Debug( LDAP_DEBUG_TRACE,
639 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
641 rc = slap_str2undef_ad( ptr, &ad, &text );
643 if( rc != LDAP_SUCCESS ) {
645 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
646 "entry_decode: str2undef_ad(%s): %s\n", ptr, text));
648 Debug( LDAP_DEBUG_ANY,
649 "<= entry_decode: str2undef_ad(%s): %s\n",
657 bptr = (struct berval **)(a+1);
659 j = entry_getlen(&ptr);
661 vptr = (struct berval *)(bptr + j + 1);
664 i = entry_getlen(&ptr);
667 vptr->bv_val = (char *)ptr;
673 bptr = (struct berval **)vptr;
678 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
679 "entry_decode: %s\n", x->e_dn ));
681 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",