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)
31 if ( ebuf ) free( ebuf );
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 = dnPrettyNormal( NULL, &value, &e->e_name, &e->e_nname );
142 free( value.bv_val );
143 if( rc != LDAP_SUCCESS ) {
145 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
146 "entry %ld has invalid DN \"%s\"\n",
148 pdn->bv_val ? pdn->bv_val : "" ));
150 Debug( LDAP_DEBUG_ANY, "str2entry: "
151 "entry %ld has invalid DN \"%s\"\n",
153 pdn->bv_val ? pdn->bv_val : "", 0 );
162 rc = slap_str2ad( type, &ad, &text );
164 if( rc != LDAP_SUCCESS ) {
166 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
167 "str2entry: str2ad(%s): %s\n", type, text ));
169 Debug( slapMode & SLAP_TOOL_MODE
170 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
171 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
173 if( slapMode & SLAP_TOOL_MODE ) {
175 free( value.bv_val );
180 rc = slap_str2undef_ad( type, &ad, &text );
181 if( rc != LDAP_SUCCESS ) {
183 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
184 "str2entry: str2undef_ad(%s): %s\n", type, text ));
186 Debug( LDAP_DEBUG_ANY,
187 "<= str2entry: str2undef_ad(%s): %s\n",
191 free( value.bv_val );
197 if( slapMode & SLAP_TOOL_MODE ) {
199 slap_syntax_validate_func *validate =
200 ad->ad_type->sat_syntax->ssyn_validate;
201 slap_syntax_transform_func *pretty =
202 ad->ad_type->sat_syntax->ssyn_pretty;
205 rc = pretty( ad->ad_type->sat_syntax,
208 } else if( validate ) {
210 * validate value per syntax
212 rc = validate( ad->ad_type->sat_syntax, &value );
216 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
217 "str2entry: no validator for syntax %s\n",
218 ad->ad_type->sat_syntax->ssyn_oid ));
220 Debug( LDAP_DEBUG_ANY,
221 "str2entry: no validator for syntax %s\n",
222 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
225 free( value.bv_val );
232 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
233 "str2entry: invalid value for syntax %s\n",
234 ad->ad_type->sat_syntax->ssyn_oid ));
236 Debug( LDAP_DEBUG_ANY,
237 "str2entry: invalid value for syntax %s\n",
238 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
241 free( value.bv_val );
247 free( value.bv_val );
252 rc = attr_merge( e, ad, vals );
255 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
256 "str2entry: NULL (attr_merge)\n" ));
258 Debug( LDAP_DEBUG_ANY,
259 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
262 free( value.bv_val );
268 free( value.bv_val );
271 /* check to make sure there was a dn: line */
272 if ( e->e_dn == NULL ) {
274 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
275 "str2entry: entry %ld has no dn.\n",
278 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
279 (long) e->e_id, 0, 0 );
286 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
287 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
289 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
290 e->e_dn, (unsigned long) e, 0 );
296 #define GRABSIZE BUFSIZ
298 #define MAKE_SPACE( n ) { \
299 while ( ecur + (n) > ebuf + emaxsize ) { \
301 offset = (int) (ecur - ebuf); \
302 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
303 emaxsize + GRABSIZE ); \
304 emaxsize += GRABSIZE; \
305 ecur = ebuf + offset; \
320 * In string format, an entry looks like this:
322 * [<attr>: <value>\n]*
328 if ( e->e_dn != NULL ) {
330 tmplen = e->e_name.bv_len;
331 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
332 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
335 /* put the attributes */
336 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
337 /* put "<type>:[:] <value>" line for each value */
338 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
340 tmplen = a->a_desc->ad_cname.bv_len;
341 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
342 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
343 a->a_desc->ad_cname.bv_val,
344 bv->bv_val, bv->bv_len );
351 return( (char *) ebuf );
355 entry_free( Entry *e )
357 /* free an entry structure */
360 /* e_private must be freed by the caller */
361 assert( e->e_private == NULL );
365 if ( e->e_dn != NULL ) {
369 if ( e->e_ndn != NULL ) {
374 /* free attributes */
375 attrs_free( e->e_attrs );
382 * These routines are used only by Backend.
384 * the Entry has three entry points (ways to find things):
386 * by entry e.g., if you already have an entry from the cache
387 * and want to delete it. (really by entry ptr)
388 * by dn e.g., when looking for the base object of a search
389 * by id e.g., for search candidates
391 * these correspond to three different avl trees that are maintained.
395 entry_cmp( Entry *e1, Entry *e2 )
397 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
401 entry_dn_cmp( Entry *e1, Entry *e2 )
403 /* compare their normalized UPPERCASED dn's */
404 return( strcmp( e1->e_ndn, e2->e_ndn ) );
408 entry_id_cmp( Entry *e1, Entry *e2 )
410 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
415 /* This is like a ber_len */
417 entry_lenlen(ber_len_t len)
431 entry_putlen(unsigned char **buf, ber_len_t len)
433 ber_len_t lenlen = entry_lenlen(len);
436 **buf = (unsigned char) len;
439 **buf = 0x80 | ((unsigned char) lenlen - 1);
440 for (i=lenlen-1; i>0; i--) {
441 (*buf)[i] = (unsigned char) len;
449 entry_getlen(unsigned char **buf)
466 /* Flatten an Entry into a buffer. The buffer is filled with just the
467 * strings/bervals of all the entry components. Each field is preceded
468 * by its length, encoded the way ber_put_len works. Every field is NUL
469 * terminated. The entire buffer size is precomputed so that a single
470 * malloc can be performed. The entry size is also recorded,
471 * to aid in entry_decode.
473 int entry_encode(Entry *e, struct berval *bv)
475 ber_len_t siz = sizeof(Entry);
476 ber_len_t len, dnlen, ndnlen;
482 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
483 "entry_encode: id: 0x%08lx \"%s\"\n",
484 (long) e->e_id, e->e_dn ));
486 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
487 (long) e->e_id, e->e_dn, 0 );
489 dnlen = e->e_name.bv_len;
490 ndnlen = e->e_nname.bv_len;
491 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
492 len += entry_lenlen(dnlen);
493 len += entry_lenlen(ndnlen);
494 for (a=e->e_attrs; a; a=a->a_next) {
495 /* For AttributeDesc, we only store the attr name */
496 siz += sizeof(Attribute);
497 len += a->a_desc->ad_cname.bv_len+1;
498 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
499 for (i=0; a->a_vals[i]; i++) {
500 siz += sizeof(struct berval *);
501 siz += sizeof(struct berval);
502 len += a->a_vals[i]->bv_len + 1;
503 len += entry_lenlen(a->a_vals[i]->bv_len);
505 len += entry_lenlen(i);
506 siz += sizeof(struct berval *); /* NULL pointer at end */
508 len += 1; /* NUL byte at end */
509 len += entry_lenlen(siz);
511 bv->bv_val = ch_malloc(len);
512 ptr = (unsigned char *)bv->bv_val;
513 entry_putlen(&ptr, siz);
514 entry_putlen(&ptr, dnlen);
515 memcpy(ptr, e->e_dn, dnlen);
518 entry_putlen(&ptr, ndnlen);
519 memcpy(ptr, e->e_ndn, ndnlen);
523 for (a=e->e_attrs; a; a=a->a_next) {
524 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
525 memcpy(ptr, a->a_desc->ad_cname.bv_val,
526 a->a_desc->ad_cname.bv_len);
527 ptr += a->a_desc->ad_cname.bv_len;
530 for (i=0; a->a_vals[i]; i++);
531 entry_putlen(&ptr, i);
532 for (i=0; a->a_vals[i]; i++) {
533 entry_putlen(&ptr, a->a_vals[i]->bv_len);
534 memcpy(ptr, a->a_vals[i]->bv_val,
535 a->a_vals[i]->bv_len);
536 ptr += a->a_vals[i]->bv_len;
545 /* Retrieve an Entry that was stored using entry_encode above.
546 * We malloc a single block with the size stored above for the Entry
547 * and all if its Attributes. We also must lookup the stored
548 * attribute names to get AttributeDescriptions. To detect if the
549 * attributes of an Entry are later modified, we note that e->e_attr
550 * is always a constant offset from (e).
552 * Note: everything is stored in a single contiguous block, so
553 * you can not free individual attributes or names from this
554 * structure. Attempting to do so will likely corrupt memory.
556 int entry_decode(struct berval *bv, Entry **e)
563 AttributeDescription *ad;
564 unsigned char *ptr = (unsigned char *)bv->bv_val;
565 struct berval **bptr;
568 i = entry_getlen(&ptr);
570 i = entry_getlen(&ptr);
571 x->e_name.bv_val = ptr;
572 x->e_name.bv_len = i;
574 i = entry_getlen(&ptr);
575 x->e_nname.bv_val = ptr;
576 x->e_nname.bv_len = i;
579 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
580 "entry_decode: \"%s\"\n", x->e_dn ));
582 Debug( LDAP_DEBUG_TRACE,
583 "entry_decode: \"%s\"\n",
586 x->e_private = bv->bv_val;
588 /* A valid entry must have at least one attr, so this
589 * pointer can never be NULL
591 x->e_attrs = (Attribute *)(x+1);
592 bptr = (struct berval **)x->e_attrs;
595 while (i = entry_getlen(&ptr)) {
597 a->a_next = (Attribute *)bptr;
599 a = (Attribute *)bptr;
601 rc = slap_str2ad( ptr, &ad, &text );
603 if( rc != LDAP_SUCCESS ) {
605 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
606 "entry_decode: str2ad(%s): %s\n", ptr, text ));
608 Debug( LDAP_DEBUG_TRACE,
609 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
611 rc = slap_str2undef_ad( ptr, &ad, &text );
613 if( rc != LDAP_SUCCESS ) {
615 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
616 "entry_decode: str2undef_ad(%s): %s\n", ptr, text));
618 Debug( LDAP_DEBUG_ANY,
619 "<= entry_decode: str2undef_ad(%s): %s\n",
627 bptr = (struct berval **)(a+1);
629 j = entry_getlen(&ptr);
631 vptr = (struct berval *)(bptr + j + 1);
634 i = entry_getlen(&ptr);
637 vptr->bv_val = (char *)ptr;
643 bptr = (struct berval **)vptr;
648 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
649 "entry_decode: %s\n", x->e_dn ));
651 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",