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, 0, 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_calloc( 1, 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 */
92 vals[1].bv_val = NULL;
95 while ( (s = ldif_getline( &next )) != NULL ) {
96 if ( *s == '\n' || *s == '\0' ) {
100 if ( ldif_parse_line( s, &type, &vals[0].bv_val, &vals[0].bv_len ) != 0 ) {
102 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
103 "str2entry: NULL (parse_line)\n" ));
105 Debug( LDAP_DEBUG_TRACE,
106 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
111 if ( strcasecmp( type, "dn" ) == 0 ) {
112 struct berval *pdn = NULL;
116 if ( e->e_dn != NULL ) {
118 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
119 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
120 (long) e->e_id, e->e_dn,
121 vals[0].bv_val != NULL ? 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,
126 vals[0].bv_val != NULL ? vals[0].bv_val : "" );
128 if( vals[0].bv_val != NULL ) free( vals[0].bv_val );
133 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname );
134 free( vals[0].bv_val );
135 if( rc != LDAP_SUCCESS ) {
137 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
138 "entry %ld has invalid DN \"%s\"\n",
140 pdn->bv_val ? pdn->bv_val : "" ));
142 Debug( LDAP_DEBUG_ANY, "str2entry: "
143 "entry %ld has invalid DN \"%s\"\n",
145 pdn->bv_val ? pdn->bv_val : "", 0 );
154 rc = slap_str2ad( type, &ad, &text );
156 if( rc != LDAP_SUCCESS ) {
158 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
159 "str2entry: str2ad(%s): %s\n", type, text ));
161 Debug( slapMode & SLAP_TOOL_MODE
162 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
163 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
165 if( slapMode & SLAP_TOOL_MODE ) {
167 free( vals[0].bv_val );
172 rc = slap_str2undef_ad( type, &ad, &text );
173 if( rc != LDAP_SUCCESS ) {
175 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
176 "str2entry: str2undef_ad(%s): %s\n", type, text ));
178 Debug( LDAP_DEBUG_ANY,
179 "<= str2entry: str2undef_ad(%s): %s\n",
183 free( vals[0].bv_val );
189 if( slapMode & SLAP_TOOL_MODE ) {
191 slap_syntax_validate_func *validate =
192 ad->ad_type->sat_syntax->ssyn_validate;
193 slap_syntax_transform_func *pretty =
194 ad->ad_type->sat_syntax->ssyn_pretty;
197 rc = pretty( ad->ad_type->sat_syntax,
200 } else if( validate ) {
202 * validate value per syntax
204 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
208 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
209 "str2entry: no validator for syntax %s\n",
210 ad->ad_type->sat_syntax->ssyn_oid ));
212 Debug( LDAP_DEBUG_ANY,
213 "str2entry: no validator for syntax %s\n",
214 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
217 free( vals[0].bv_val );
224 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
225 "str2entry: invalid value for syntax %s\n",
226 ad->ad_type->sat_syntax->ssyn_oid ));
228 Debug( LDAP_DEBUG_ANY,
229 "str2entry: invalid value for syntax %s\n",
230 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
233 free( vals[0].bv_val );
239 free( vals[0].bv_val );
244 rc = attr_merge( e, ad, vals );
247 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
248 "str2entry: NULL (attr_merge)\n" ));
250 Debug( LDAP_DEBUG_ANY,
251 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
254 free( vals[0].bv_val );
260 free( vals[0].bv_val );
263 /* check to make sure there was a dn: line */
264 if ( e->e_dn == NULL ) {
266 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
267 "str2entry: entry %ld has no dn.\n",
270 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
271 (long) e->e_id, 0, 0 );
278 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
279 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
281 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
282 e->e_dn, (unsigned long) e, 0 );
288 #define GRABSIZE BUFSIZ
290 #define MAKE_SPACE( n ) { \
291 while ( ecur + (n) > ebuf + emaxsize ) { \
293 offset = (int) (ecur - ebuf); \
294 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
295 emaxsize + GRABSIZE ); \
296 emaxsize += GRABSIZE; \
297 ecur = ebuf + offset; \
312 * In string format, an entry looks like this:
314 * [<attr>: <value>\n]*
320 if ( e->e_dn != NULL ) {
322 tmplen = e->e_name.bv_len;
323 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
324 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
327 /* put the attributes */
328 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
329 /* put "<type>:[:] <value>" line for each value */
330 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
332 tmplen = a->a_desc->ad_cname.bv_len;
333 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
334 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
335 a->a_desc->ad_cname.bv_val,
336 bv->bv_val, bv->bv_len );
343 return( (char *) ebuf );
347 entry_free( Entry *e )
349 /* free an entry structure */
352 /* e_private must be freed by the caller */
353 assert( e->e_private == NULL );
357 if ( e->e_dn != NULL ) {
361 if ( e->e_ndn != NULL ) {
366 /* free attributes */
367 attrs_free( e->e_attrs );
374 * These routines are used only by Backend.
376 * the Entry has three entry points (ways to find things):
378 * by entry e.g., if you already have an entry from the cache
379 * and want to delete it. (really by entry ptr)
380 * by dn e.g., when looking for the base object of a search
381 * by id e.g., for search candidates
383 * these correspond to three different avl trees that are maintained.
387 entry_cmp( Entry *e1, Entry *e2 )
389 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
393 entry_dn_cmp( Entry *e1, Entry *e2 )
395 /* compare their normalized UPPERCASED dn's */
396 int rc = e1->e_nname.bv_len - e2->e_nname.bv_len;
398 return( strcmp( e1->e_ndn, e2->e_ndn ) );
402 entry_id_cmp( Entry *e1, Entry *e2 )
404 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
409 /* This is like a ber_len */
411 entry_lenlen(ber_len_t len)
425 entry_putlen(unsigned char **buf, ber_len_t len)
427 ber_len_t lenlen = entry_lenlen(len);
430 **buf = (unsigned char) len;
433 **buf = 0x80 | ((unsigned char) lenlen - 1);
434 for (i=lenlen-1; i>0; i--) {
435 (*buf)[i] = (unsigned char) len;
443 entry_getlen(unsigned char **buf)
460 /* Flatten an Entry into a buffer. The buffer is filled with just the
461 * strings/bervals of all the entry components. Each field is preceded
462 * by its length, encoded the way ber_put_len works. Every field is NUL
463 * terminated. The entire buffer size is precomputed so that a single
464 * malloc can be performed. The entry size is also recorded,
465 * to aid in entry_decode.
467 int entry_encode(Entry *e, struct berval *bv)
469 ber_len_t siz = sizeof(Entry);
470 ber_len_t len, dnlen, ndnlen;
476 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
477 "entry_encode: id: 0x%08lx \"%s\"\n",
478 (long) e->e_id, e->e_dn ));
480 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
481 (long) e->e_id, e->e_dn, 0 );
483 dnlen = e->e_name.bv_len;
484 ndnlen = e->e_nname.bv_len;
485 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
486 len += entry_lenlen(dnlen);
487 len += entry_lenlen(ndnlen);
488 for (a=e->e_attrs; a; a=a->a_next) {
489 /* For AttributeDesc, we only store the attr name */
490 siz += sizeof(Attribute);
491 len += a->a_desc->ad_cname.bv_len+1;
492 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
493 for (i=0; a->a_vals[i].bv_val; i++) {
494 siz += sizeof(struct berval);
495 len += a->a_vals[i].bv_len + 1;
496 len += entry_lenlen(a->a_vals[i].bv_len);
498 len += entry_lenlen(i);
499 siz += sizeof(struct berval); /* empty berval at end */
501 len += 1; /* NUL byte at end */
502 len += entry_lenlen(siz);
504 bv->bv_val = ch_malloc(len);
505 ptr = (unsigned char *)bv->bv_val;
506 entry_putlen(&ptr, siz);
507 entry_putlen(&ptr, dnlen);
508 AC_MEMCPY(ptr, e->e_dn, dnlen);
511 entry_putlen(&ptr, ndnlen);
512 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
516 for (a=e->e_attrs; a; a=a->a_next) {
517 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
518 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
519 a->a_desc->ad_cname.bv_len);
520 ptr += a->a_desc->ad_cname.bv_len;
523 for (i=0; a->a_vals[i].bv_val; i++);
524 entry_putlen(&ptr, i);
525 for (i=0; a->a_vals[i].bv_val; i++) {
526 entry_putlen(&ptr, a->a_vals[i].bv_len);
527 memcpy(ptr, a->a_vals[i].bv_val,
528 a->a_vals[i].bv_len);
529 ptr += a->a_vals[i].bv_len;
538 /* Retrieve an Entry that was stored using entry_encode above.
539 * We malloc a single block with the size stored above for the Entry
540 * and all if its Attributes. We also must lookup the stored
541 * attribute names to get AttributeDescriptions. To detect if the
542 * attributes of an Entry are later modified, we note that e->e_attr
543 * is always a constant offset from (e).
545 * Note: everything is stored in a single contiguous block, so
546 * you can not free individual attributes or names from this
547 * structure. Attempting to do so will likely corrupt memory.
549 int entry_decode(struct berval *bv, Entry **e)
556 AttributeDescription *ad;
557 unsigned char *ptr = (unsigned char *)bv->bv_val;
560 i = entry_getlen(&ptr);
562 i = entry_getlen(&ptr);
563 x->e_name.bv_val = ptr;
564 x->e_name.bv_len = i;
566 i = entry_getlen(&ptr);
567 x->e_nname.bv_val = ptr;
568 x->e_nname.bv_len = i;
571 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
572 "entry_decode: \"%s\"\n", x->e_dn ));
574 Debug( LDAP_DEBUG_TRACE,
575 "entry_decode: \"%s\"\n",
578 x->e_private = bv->bv_val;
580 /* A valid entry must have at least one attr, so this
581 * pointer can never be NULL
583 x->e_attrs = (Attribute *)(x+1);
584 bptr = (BerVarray)x->e_attrs;
587 while (i = entry_getlen(&ptr)) {
588 struct berval bv = { i, ptr };
590 a->a_next = (Attribute *)bptr;
592 a = (Attribute *)bptr;
594 rc = slap_bv2ad( &bv, &ad, &text );
596 if( rc != LDAP_SUCCESS ) {
598 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
599 "entry_decode: str2ad(%s): %s\n", ptr, text ));
601 Debug( LDAP_DEBUG_TRACE,
602 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
604 rc = slap_bv2undef_ad( &bv, &ad, &text );
606 if( rc != LDAP_SUCCESS ) {
608 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
609 "entry_decode: str2undef_ad(%s): %s\n", ptr, text));
611 Debug( LDAP_DEBUG_ANY,
612 "<= entry_decode: str2undef_ad(%s): %s\n",
620 bptr = (BerVarray)(a+1);
623 j = entry_getlen(&ptr);
626 i = entry_getlen(&ptr);
628 bptr->bv_val = (char *)ptr;
640 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
641 "entry_decode: %s\n", x->e_dn ));
643 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",