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, { 0, "" }, { 0, "" }, NULL, NULL };
29 int entry_destroy(void)
46 struct berval *vals[2];
47 AttributeDescription *ad;
52 * LDIF is used as the string format.
53 * An entry looks like this:
56 * [<attr>:[:] <value>\n]
57 * [<tab><continuedvalue>\n]*
60 * If a double colon is used after a type, it means the
61 * following value is encoded as a base 64 string. This
62 * happens if the value contains a non-printing character
67 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
68 "str2entry: \"%s\"\n", s ? s : "NULL" ));
70 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
71 s ? s : "NULL", 0, 0 );
74 /* initialize reader/writer lock */
75 e = (Entry *) ch_malloc( sizeof(Entry) );
79 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
80 "str2entry: entry allocation failed.\n" ));
82 Debug( LDAP_DEBUG_ANY,
83 "<= str2entry NULL (entry allocation failed)\n",
89 /* initialize entry */
91 e->e_name.bv_val = NULL;
93 e->e_nname.bv_val = NULL;
94 e->e_nname.bv_len = 0;
103 while ( (s = ldif_getline( &next )) != NULL ) {
104 if ( *s == '\n' || *s == '\0' ) {
108 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
110 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
111 "str2entry: NULL (parse_line)\n" ));
113 Debug( LDAP_DEBUG_TRACE,
114 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
119 if ( strcasecmp( type, "dn" ) == 0 ) {
120 struct berval *pdn = NULL;
124 if ( e->e_dn != NULL ) {
126 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
127 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
128 (long) e->e_id, e->e_dn,
129 value.bv_val != NULL ? value.bv_val : "" ));
131 Debug( LDAP_DEBUG_ANY, "str2entry: "
132 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
133 (long) e->e_id, e->e_dn,
134 value.bv_val != NULL ? value.bv_val : "" );
136 if( value.bv_val != NULL ) free( value.bv_val );
141 rc = dnPretty( NULL, &value, &pdn );
142 if( rc != LDAP_SUCCESS ) {
144 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
145 "entry %ld has invalid DN \"%s\"\n",
147 pdn->bv_val ? pdn->bv_val : "" ));
149 Debug( LDAP_DEBUG_ANY, "str2entry: "
150 "entry %ld has invalid DN \"%s\"\n",
152 pdn->bv_val ? pdn->bv_val : "", 0 );
154 if( value.bv_val != NULL ) free( value.bv_val );
159 e->e_name.bv_val = pdn->bv_val != NULL
160 ? pdn->bv_val : ch_strdup( "" );
161 e->e_name.bv_len = pdn->bv_len;
167 rc = slap_str2ad( type, &ad, &text );
169 if( rc != LDAP_SUCCESS ) {
171 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
172 "str2entry: str2ad(%s): %s\n", type, text ));
174 Debug( slapMode & SLAP_TOOL_MODE
175 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
176 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
178 if( slapMode & SLAP_TOOL_MODE ) {
180 free( value.bv_val );
185 rc = slap_str2undef_ad( type, &ad, &text );
186 if( rc != LDAP_SUCCESS ) {
188 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
189 "str2entry: str2undef_ad(%s): %s\n", type, text ));
191 Debug( LDAP_DEBUG_ANY,
192 "<= str2entry: str2undef_ad(%s): %s\n",
196 free( value.bv_val );
202 if( slapMode & SLAP_TOOL_MODE ) {
204 slap_syntax_validate_func *validate =
205 ad->ad_type->sat_syntax->ssyn_validate;
206 slap_syntax_transform_func *pretty =
207 ad->ad_type->sat_syntax->ssyn_pretty;
210 rc = pretty( ad->ad_type->sat_syntax,
213 } else if( validate ) {
215 * validate value per syntax
217 rc = validate( ad->ad_type->sat_syntax, &value );
221 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
222 "str2entry: no validator for syntax %s\n",
223 ad->ad_type->sat_syntax->ssyn_oid ));
225 Debug( LDAP_DEBUG_ANY,
226 "str2entry: no validator for syntax %s\n",
227 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
230 free( value.bv_val );
237 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
238 "str2entry: invalid value for syntax %s\n",
239 ad->ad_type->sat_syntax->ssyn_oid ));
241 Debug( LDAP_DEBUG_ANY,
242 "str2entry: invalid value for syntax %s\n",
243 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
246 free( value.bv_val );
252 free( value.bv_val );
258 rc = attr_merge( e, ad, vals );
262 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
263 "str2entry: NULL (attr_merge)\n" ));
265 Debug( LDAP_DEBUG_ANY,
266 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
269 free( value.bv_val );
275 free( value.bv_val );
278 /* check to make sure there was a dn: line */
279 if ( e->e_dn == NULL ) {
281 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
282 "str2entry: entry %ld has no dn.\n",
285 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
286 (long) e->e_id, 0, 0 );
292 /* generate normalized dn */
294 struct berval *ndn = NULL;
296 rc = dnNormalize( NULL, &e->e_name, &ndn );
297 if( rc != LDAP_SUCCESS ) {
299 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
300 "str2entry: entry %ld has invalid dn: %s\n",
301 (long) e->e_id, e->e_dn ));
303 Debug( LDAP_DEBUG_ANY,
304 "str2entry: entry %ld has invalid dn: %s\n",
305 (long) e->e_id, e->e_dn, 0 );
316 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
317 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
319 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
320 e->e_dn, (unsigned long) e, 0 );
326 #define GRABSIZE BUFSIZ
328 #define MAKE_SPACE( n ) { \
329 while ( ecur + (n) > ebuf + emaxsize ) { \
331 offset = (int) (ecur - ebuf); \
332 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
333 emaxsize + GRABSIZE ); \
334 emaxsize += GRABSIZE; \
335 ecur = ebuf + offset; \
350 * In string format, an entry looks like this:
352 * [<attr>: <value>\n]*
358 if ( e->e_dn != NULL ) {
360 tmplen = e->e_name.bv_len;
361 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
362 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
365 /* put the attributes */
366 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
367 /* put "<type>:[:] <value>" line for each value */
368 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
370 tmplen = a->a_desc->ad_cname.bv_len;
371 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
372 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
373 a->a_desc->ad_cname.bv_val,
374 bv->bv_val, bv->bv_len );
381 return( (char *) ebuf );
385 entry_free( Entry *e )
387 /* free an entry structure */
390 /* e_private must be freed by the caller */
391 assert( e->e_private == NULL );
395 if ( e->e_dn != NULL ) {
399 if ( e->e_ndn != NULL ) {
404 /* free attributes */
405 attrs_free( e->e_attrs );
412 * These routines are used only by Backend.
414 * the Entry has three entry points (ways to find things):
416 * by entry e.g., if you already have an entry from the cache
417 * and want to delete it. (really by entry ptr)
418 * by dn e.g., when looking for the base object of a search
419 * by id e.g., for search candidates
421 * these correspond to three different avl trees that are maintained.
425 entry_cmp( Entry *e1, Entry *e2 )
427 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
431 entry_dn_cmp( Entry *e1, Entry *e2 )
433 /* compare their normalized UPPERCASED dn's */
434 return( strcmp( e1->e_ndn, e2->e_ndn ) );
438 entry_id_cmp( Entry *e1, Entry *e2 )
440 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
445 /* This is like a ber_len */
447 entry_lenlen(ber_len_t len)
461 entry_putlen(unsigned char **buf, ber_len_t len)
463 ber_len_t lenlen = entry_lenlen(len);
466 **buf = (unsigned char) len;
469 **buf = 0x80 | ((unsigned char) lenlen - 1);
470 for (i=lenlen-1; i>0; i--) {
471 (*buf)[i] = (unsigned char) len;
479 entry_getlen(unsigned char **buf)
496 /* Flatten an Entry into a buffer. The buffer is filled with just the
497 * strings/bervals of all the entry components. Each field is preceded
498 * by its length, encoded the way ber_put_len works. Every field is NUL
499 * terminated. The entire buffer size is precomputed so that a single
500 * malloc can be performed. The entry size is also recorded,
501 * to aid in entry_decode.
503 int entry_encode(Entry *e, struct berval *bv)
505 ber_len_t siz = sizeof(Entry);
506 ber_len_t len, dnlen, ndnlen;
512 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
513 "entry_encode: id: 0x%08lx \"%s\"\n",
514 (long) e->e_id, e->e_dn ));
516 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
517 (long) e->e_id, e->e_dn, 0 );
519 dnlen = e->e_name.bv_len;
520 ndnlen = e->e_nname.bv_len;
521 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
522 len += entry_lenlen(dnlen);
523 len += entry_lenlen(ndnlen);
524 for (a=e->e_attrs; a; a=a->a_next) {
525 /* For AttributeDesc, we only store the attr name */
526 siz += sizeof(Attribute);
527 len += a->a_desc->ad_cname.bv_len+1;
528 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
529 for (i=0; a->a_vals[i]; i++) {
530 siz += sizeof(struct berval *);
531 siz += sizeof(struct berval);
532 len += a->a_vals[i]->bv_len + 1;
533 len += entry_lenlen(a->a_vals[i]->bv_len);
535 len += entry_lenlen(i);
536 siz += sizeof(struct berval *); /* NULL pointer at end */
538 len += 1; /* NUL byte at end */
539 len += entry_lenlen(siz);
541 bv->bv_val = ch_malloc(len);
542 ptr = (unsigned char *)bv->bv_val;
543 entry_putlen(&ptr, siz);
544 entry_putlen(&ptr, dnlen);
545 memcpy(ptr, e->e_dn, dnlen);
548 entry_putlen(&ptr, ndnlen);
549 memcpy(ptr, e->e_ndn, ndnlen);
553 for (a=e->e_attrs; a; a=a->a_next) {
554 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
555 memcpy(ptr, a->a_desc->ad_cname.bv_val,
556 a->a_desc->ad_cname.bv_len);
557 ptr += a->a_desc->ad_cname.bv_len;
560 for (i=0; a->a_vals[i]; i++);
561 entry_putlen(&ptr, i);
562 for (i=0; a->a_vals[i]; i++) {
563 entry_putlen(&ptr, a->a_vals[i]->bv_len);
564 memcpy(ptr, a->a_vals[i]->bv_val,
565 a->a_vals[i]->bv_len);
566 ptr += a->a_vals[i]->bv_len;
575 /* Retrieve an Entry that was stored using entry_encode above.
576 * We malloc a single block with the size stored above for the Entry
577 * and all if its Attributes. We also must lookup the stored
578 * attribute names to get AttributeDescriptions. To detect if the
579 * attributes of an Entry are later modified, we note that e->e_attr
580 * is always a constant offset from (e).
582 * Note: everything is stored in a single contiguous block, so
583 * you can not free individual attributes or names from this
584 * structure. Attempting to do so will likely corrupt memory.
586 int entry_decode(struct berval *bv, Entry **e)
593 AttributeDescription *ad;
594 unsigned char *ptr = (unsigned char *)bv->bv_val;
595 struct berval **bptr;
598 i = entry_getlen(&ptr);
600 i = entry_getlen(&ptr);
601 x->e_name.bv_val = ptr;
602 x->e_name.bv_len = i;
604 i = entry_getlen(&ptr);
605 x->e_nname.bv_val = ptr;
606 x->e_nname.bv_len = i;
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",