1 /* entry.c - routines for dealing with entries */
4 * Copyright 1998-2002 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 );
45 struct berval vals[2];
46 AttributeDescription *ad;
51 * LDIF is used as the string format.
52 * An entry looks like this:
55 * [<attr>:[:] <value>\n]
56 * [<tab><continuedvalue>\n]*
59 * If a double colon is used after a type, it means the
60 * following value is encoded as a base 64 string. This
61 * happens if the value contains a non-printing character
66 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
67 "str2entry: \"%s\"\n", s ? s : "NULL" ));
69 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
70 s ? s : "NULL", 0, 0 );
73 /* initialize reader/writer lock */
74 e = (Entry *) ch_malloc( sizeof(Entry) );
78 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
79 "str2entry: entry allocation failed.\n" ));
81 Debug( LDAP_DEBUG_ANY,
82 "<= str2entry NULL (entry allocation failed)\n",
88 /* initialize entry */
90 e->e_name.bv_val = NULL;
92 e->e_nname.bv_val = NULL;
93 e->e_nname.bv_len = 0;
98 vals[1].bv_val = NULL;
101 while ( (s = ldif_getline( &next )) != NULL ) {
102 if ( *s == '\n' || *s == '\0' ) {
106 if ( ldif_parse_line( s, &type, &vals[0].bv_val, &vals[0].bv_len ) != 0 ) {
108 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
109 "str2entry: NULL (parse_line)\n" ));
111 Debug( LDAP_DEBUG_TRACE,
112 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
117 if ( strcasecmp( type, "dn" ) == 0 ) {
118 struct berval *pdn = NULL;
122 if ( e->e_dn != NULL ) {
124 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
125 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
126 (long) e->e_id, e->e_dn,
127 vals[0].bv_val != NULL ? vals[0].bv_val : "" ));
129 Debug( LDAP_DEBUG_ANY, "str2entry: "
130 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
131 (long) e->e_id, e->e_dn,
132 vals[0].bv_val != NULL ? vals[0].bv_val : "" );
134 if( vals[0].bv_val != NULL ) free( vals[0].bv_val );
139 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname );
140 free( vals[0].bv_val );
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 );
160 rc = slap_str2ad( type, &ad, &text );
162 if( rc != LDAP_SUCCESS ) {
164 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
165 "str2entry: str2ad(%s): %s\n", type, text ));
167 Debug( slapMode & SLAP_TOOL_MODE
168 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
169 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
171 if( slapMode & SLAP_TOOL_MODE ) {
173 free( vals[0].bv_val );
178 rc = slap_str2undef_ad( type, &ad, &text );
179 if( rc != LDAP_SUCCESS ) {
181 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
182 "str2entry: str2undef_ad(%s): %s\n", type, text ));
184 Debug( LDAP_DEBUG_ANY,
185 "<= str2entry: str2undef_ad(%s): %s\n",
189 free( vals[0].bv_val );
195 if( slapMode & SLAP_TOOL_MODE ) {
197 slap_syntax_validate_func *validate =
198 ad->ad_type->sat_syntax->ssyn_validate;
199 slap_syntax_transform_func *pretty =
200 ad->ad_type->sat_syntax->ssyn_pretty;
203 rc = pretty( ad->ad_type->sat_syntax,
206 } else if( validate ) {
208 * validate value per syntax
210 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
214 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
215 "str2entry: no validator for syntax %s\n",
216 ad->ad_type->sat_syntax->ssyn_oid ));
218 Debug( LDAP_DEBUG_ANY,
219 "str2entry: no validator for syntax %s\n",
220 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
223 free( vals[0].bv_val );
230 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
231 "str2entry: invalid value for syntax %s\n",
232 ad->ad_type->sat_syntax->ssyn_oid ));
234 Debug( LDAP_DEBUG_ANY,
235 "str2entry: invalid value for syntax %s\n",
236 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
239 free( vals[0].bv_val );
245 free( vals[0].bv_val );
250 rc = attr_merge( e, ad, vals );
253 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
254 "str2entry: NULL (attr_merge)\n" ));
256 Debug( LDAP_DEBUG_ANY,
257 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
260 free( vals[0].bv_val );
266 free( vals[0].bv_val );
269 /* check to make sure there was a dn: line */
270 if ( e->e_dn == NULL ) {
272 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
273 "str2entry: entry %ld has no dn.\n",
276 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
277 (long) e->e_id, 0, 0 );
284 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
285 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
287 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
288 e->e_dn, (unsigned long) e, 0 );
294 #define GRABSIZE BUFSIZ
296 #define MAKE_SPACE( n ) { \
297 while ( ecur + (n) > ebuf + emaxsize ) { \
299 offset = (int) (ecur - ebuf); \
300 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
301 emaxsize + GRABSIZE ); \
302 emaxsize += GRABSIZE; \
303 ecur = ebuf + offset; \
318 * In string format, an entry looks like this:
320 * [<attr>: <value>\n]*
326 if ( e->e_dn != NULL ) {
328 tmplen = e->e_name.bv_len;
329 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
330 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
333 /* put the attributes */
334 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
335 /* put "<type>:[:] <value>" line for each value */
336 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
338 tmplen = a->a_desc->ad_cname.bv_len;
339 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
340 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
341 a->a_desc->ad_cname.bv_val,
342 bv->bv_val, bv->bv_len );
349 return( (char *) ebuf );
353 entry_free( Entry *e )
355 /* free an entry structure */
358 /* e_private must be freed by the caller */
359 assert( e->e_private == NULL );
363 if ( e->e_dn != NULL ) {
367 if ( e->e_ndn != NULL ) {
372 /* free attributes */
373 attrs_free( e->e_attrs );
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( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
399 entry_dn_cmp( Entry *e1, Entry *e2 )
401 /* compare their normalized UPPERCASED dn's */
402 int rc = e1->e_nname.bv_len - e2->e_nname.bv_len;
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].bv_val; i++) {
500 siz += sizeof(struct berval);
501 len += a->a_vals[i].bv_len + 1;
502 len += entry_lenlen(a->a_vals[i].bv_len);
504 len += entry_lenlen(i);
505 siz += sizeof(struct berval); /* empty berval at end */
507 len += 1; /* NUL byte at end */
508 len += entry_lenlen(siz);
510 bv->bv_val = ch_malloc(len);
511 ptr = (unsigned char *)bv->bv_val;
512 entry_putlen(&ptr, siz);
513 entry_putlen(&ptr, dnlen);
514 AC_MEMCPY(ptr, e->e_dn, dnlen);
517 entry_putlen(&ptr, ndnlen);
518 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
522 for (a=e->e_attrs; a; a=a->a_next) {
523 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
524 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
525 a->a_desc->ad_cname.bv_len);
526 ptr += a->a_desc->ad_cname.bv_len;
529 for (i=0; a->a_vals[i].bv_val; i++);
530 entry_putlen(&ptr, i);
531 for (i=0; a->a_vals[i].bv_val; i++) {
532 entry_putlen(&ptr, a->a_vals[i].bv_len);
533 memcpy(ptr, a->a_vals[i].bv_val,
534 a->a_vals[i].bv_len);
535 ptr += a->a_vals[i].bv_len;
544 /* Retrieve an Entry that was stored using entry_encode above.
545 * We malloc a single block with the size stored above for the Entry
546 * and all if its Attributes. We also must lookup the stored
547 * attribute names to get AttributeDescriptions. To detect if the
548 * attributes of an Entry are later modified, we note that e->e_attr
549 * is always a constant offset from (e).
551 * Note: everything is stored in a single contiguous block, so
552 * you can not free individual attributes or names from this
553 * structure. Attempting to do so will likely corrupt memory.
555 int entry_decode(struct berval *bv, Entry **e)
562 AttributeDescription *ad;
563 unsigned char *ptr = (unsigned char *)bv->bv_val;
566 i = entry_getlen(&ptr);
568 i = entry_getlen(&ptr);
569 x->e_name.bv_val = ptr;
570 x->e_name.bv_len = i;
572 i = entry_getlen(&ptr);
573 x->e_nname.bv_val = ptr;
574 x->e_nname.bv_len = i;
577 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
578 "entry_decode: \"%s\"\n", x->e_dn ));
580 Debug( LDAP_DEBUG_TRACE,
581 "entry_decode: \"%s\"\n",
584 x->e_private = bv->bv_val;
586 /* A valid entry must have at least one attr, so this
587 * pointer can never be NULL
589 x->e_attrs = (Attribute *)(x+1);
590 bptr = (BerVarray)x->e_attrs;
593 while (i = entry_getlen(&ptr)) {
594 struct berval bv = { i, ptr };
596 a->a_next = (Attribute *)bptr;
598 a = (Attribute *)bptr;
600 rc = slap_bv2ad( &bv, &ad, &text );
602 if( rc != LDAP_SUCCESS ) {
604 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
605 "entry_decode: str2ad(%s): %s\n", ptr, text ));
607 Debug( LDAP_DEBUG_TRACE,
608 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
610 rc = slap_bv2undef_ad( &bv, &ad, &text );
612 if( rc != LDAP_SUCCESS ) {
614 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
615 "entry_decode: str2undef_ad(%s): %s\n", ptr, text));
617 Debug( LDAP_DEBUG_ANY,
618 "<= entry_decode: str2undef_ad(%s): %s\n",
626 bptr = (BerVarray)(a+1);
629 j = entry_getlen(&ptr);
632 i = entry_getlen(&ptr);
634 bptr->bv_val = (char *)ptr;
646 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
647 "entry_decode: %s\n", x->e_dn ));
649 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",