1 /* entry.c - routines for dealing with entries */
4 * Copyright 1998-2003 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 = {
28 NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL
31 int entry_destroy(void)
33 if ( ebuf ) free( ebuf );
47 struct berval vals[2];
49 struct berval nvals[2], *nvalsp;
51 AttributeDescription *ad;
56 * LDIF is used as the string format.
57 * An entry looks like this:
60 * [<attr>:[:] <value>\n]
61 * [<tab><continuedvalue>\n]*
64 * If a double colon is used after a type, it means the
65 * following value is encoded as a base 64 string. This
66 * happens if the value contains a non-printing character
71 LDAP_LOG( OPERATION, DETAIL1, "str2entry: \"%s\"\n", s ? s : "NULL", 0, 0 );
73 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
74 s ? s : "NULL", 0, 0 );
77 /* initialize reader/writer lock */
78 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
82 LDAP_LOG( OPERATION, ERR, "str2entry: entry allocation failed.\n", 0, 0, 0 );
84 Debug( LDAP_DEBUG_ANY,
85 "<= str2entry NULL (entry allocation failed)\n",
91 /* initialize entry */
96 vals[1].bv_val = NULL;
99 while ( (s = ldif_getline( &next )) != NULL ) {
100 if ( *s == '\n' || *s == '\0' ) {
104 if ( ldif_parse_line( s, &type, &vals[0].bv_val, &vals[0].bv_len ) != 0 ) {
106 LDAP_LOG( OPERATION, DETAIL1, "str2entry: NULL (parse_line)\n",0, 0, 0 );
108 Debug( LDAP_DEBUG_TRACE,
109 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
114 if ( strcasecmp( type, "dn" ) == 0 ) {
117 if ( e->e_dn != NULL ) {
119 LDAP_LOG( OPERATION, DETAIL1, "str2entry: "
120 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
121 (long) e->e_id, e->e_dn, 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, vals[0].bv_val );
127 free( vals[0].bv_val );
132 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname );
133 if( rc != LDAP_SUCCESS ) {
135 LDAP_LOG( OPERATION, DETAIL1,
136 "str2entry: entry %ld has invalid DN \"%s\"\n",
137 (long) e->e_id, vals[0].bv_val, 0 );
139 Debug( LDAP_DEBUG_ANY, "str2entry: "
140 "entry %ld has invalid DN \"%s\"\n",
141 (long) e->e_id, vals[0].bv_val, 0 );
144 free( vals[0].bv_val );
147 free( vals[0].bv_val );
152 rc = slap_str2ad( type, &ad, &text );
154 if( rc != LDAP_SUCCESS ) {
156 LDAP_LOG( OPERATION, DETAIL1,
157 "str2entry: str2ad(%s): %s\n", type, text, 0 );
159 Debug( slapMode & SLAP_TOOL_MODE
160 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
161 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
163 if( slapMode & SLAP_TOOL_MODE ) {
165 free( vals[0].bv_val );
170 rc = slap_str2undef_ad( type, &ad, &text );
171 if( rc != LDAP_SUCCESS ) {
173 LDAP_LOG( OPERATION, DETAIL1,
174 "str2entry: str2undef_ad(%s): %s\n", type, text, 0 );
176 Debug( LDAP_DEBUG_ANY,
177 "<= str2entry: str2undef_ad(%s): %s\n",
181 free( vals[0].bv_val );
187 if( slapMode & SLAP_TOOL_MODE ) {
189 slap_syntax_validate_func *validate =
190 ad->ad_type->sat_syntax->ssyn_validate;
191 slap_syntax_transform_func *pretty =
192 ad->ad_type->sat_syntax->ssyn_pretty;
195 rc = pretty( ad->ad_type->sat_syntax,
198 } else if( validate ) {
200 * validate value per syntax
202 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
206 LDAP_LOG( OPERATION, INFO,
207 "str2entry: no validator for syntax %s\n",
208 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
210 Debug( LDAP_DEBUG_ANY,
211 "str2entry: no validator for syntax %s\n",
212 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
215 free( vals[0].bv_val );
222 LDAP_LOG( OPERATION, ERR,
223 "str2entry: invalid value for syntax %s\n",
224 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
226 Debug( LDAP_DEBUG_ANY,
227 "str2entry: invalid value for syntax %s\n",
228 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
231 free( vals[0].bv_val );
237 free( vals[0].bv_val );
244 nvals[0].bv_val = NULL;
247 if( ad->ad_type->sat_equality &&
248 ad->ad_type->sat_equality->smr_match &&
249 ad->ad_type->sat_syntax->ssyn_normalize )
251 rc = ad->ad_type->sat_syntax->ssyn_normalize(
252 ad->ad_type->sat_syntax,
253 &vals[0], &nvals[0] );
257 LDAP_LOG( OPERATION, DETAIL1,
258 "str2entry: NULL (ssyn_normalize %d)\n" , rc, 0, 0 );
260 Debug( LDAP_DEBUG_ANY,
261 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
264 free( vals[0].bv_val );
271 nvals[1].bv_val = NULL;
279 rc = attr_merge( e, ad, vals, nvalsp );
281 rc = attr_merge( e, ad, vals );
285 LDAP_LOG( OPERATION, DETAIL1,
286 "str2entry: NULL (attr_merge)\n" , 0, 0, 0 );
288 Debug( LDAP_DEBUG_ANY,
289 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
292 free( vals[0].bv_val );
298 free( vals[0].bv_val );
300 free( nvals[0].bv_val );
304 /* check to make sure there was a dn: line */
305 if ( e->e_dn == NULL ) {
307 LDAP_LOG( OPERATION, INFO,
308 "str2entry: entry %ld has no dn.\n", (long) e->e_id, 0, 0 );
310 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
311 (long) e->e_id, 0, 0 );
318 LDAP_LOG( OPERATION, DETAIL2,
319 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e, 0 );
321 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
322 e->e_dn, (unsigned long) e, 0 );
328 #define GRABSIZE BUFSIZ
330 #define MAKE_SPACE( n ) { \
331 while ( ecur + (n) > ebuf + emaxsize ) { \
333 offset = (int) (ecur - ebuf); \
334 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
335 emaxsize + GRABSIZE ); \
336 emaxsize += GRABSIZE; \
337 ecur = ebuf + offset; \
354 * In string format, an entry looks like this:
356 * [<attr>: <value>\n]*
362 if ( e->e_dn != NULL ) {
364 tmplen = e->e_name.bv_len;
365 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
366 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
369 /* put the attributes */
370 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
371 /* put "<type>:[:] <value>" line for each value */
372 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
374 tmplen = a->a_desc->ad_cname.bv_len;
375 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
376 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
377 a->a_desc->ad_cname.bv_val,
378 bv->bv_val, bv->bv_len );
385 return( (char *) ebuf );
389 entry_free( Entry *e )
391 /* free an entry structure */
394 /* e_private must be freed by the caller */
395 assert( e->e_private == NULL );
399 if ( e->e_dn != NULL ) {
403 if ( e->e_ndn != NULL ) {
408 if ( e->e_bv.bv_val != NULL ) {
409 free( e->e_bv.bv_val );
410 e->e_bv.bv_val = NULL;
413 /* free attributes */
414 attrs_free( e->e_attrs );
421 * These routines are used only by Backend.
423 * the Entry has three entry points (ways to find things):
425 * by entry e.g., if you already have an entry from the cache
426 * and want to delete it. (really by entry ptr)
427 * by dn e.g., when looking for the base object of a search
428 * by id e.g., for search candidates
430 * these correspond to three different avl trees that are maintained.
434 entry_cmp( Entry *e1, Entry *e2 )
436 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
440 entry_dn_cmp( const void *v_e1, const void *v_e2 )
442 /* compare their normalized UPPERCASED dn's */
443 const Entry *e1 = v_e1, *e2 = v_e2;
444 int rc = e1->e_nname.bv_len - e2->e_nname.bv_len;
446 return( strcmp( e1->e_ndn, e2->e_ndn ) );
450 entry_id_cmp( const void *v_e1, const void *v_e2 )
452 const Entry *e1 = v_e1, *e2 = v_e2;
453 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
458 /* This is like a ber_len */
460 entry_lenlen(ber_len_t len)
474 entry_putlen(unsigned char **buf, ber_len_t len)
476 ber_len_t lenlen = entry_lenlen(len);
479 **buf = (unsigned char) len;
482 **buf = 0x80 | ((unsigned char) lenlen - 1);
483 for (i=lenlen-1; i>0; i--) {
484 (*buf)[i] = (unsigned char) len;
492 entry_getlen(unsigned char **buf)
509 /* Flatten an Entry into a buffer. The buffer is filled with just the
510 * strings/bervals of all the entry components. Each field is preceded
511 * by its length, encoded the way ber_put_len works. Every field is NUL
512 * terminated. The entire buffer size is precomputed so that a single
513 * malloc can be performed. The entry size is also recorded,
514 * to aid in entry_decode.
516 int entry_encode(Entry *e, struct berval *bv)
518 ber_len_t siz = sizeof(Entry);
519 ber_len_t len, dnlen, ndnlen;
525 LDAP_LOG( OPERATION, DETAIL1, "entry_encode: id: 0x%08lx \"%s\"\n",
526 (long) e->e_id, e->e_dn, 0 );
528 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
529 (long) e->e_id, e->e_dn, 0 );
531 dnlen = e->e_name.bv_len;
532 ndnlen = e->e_nname.bv_len;
533 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
534 len += entry_lenlen(dnlen);
535 len += entry_lenlen(ndnlen);
536 for (a=e->e_attrs; a; a=a->a_next) {
537 /* For AttributeDesc, we only store the attr name */
538 siz += sizeof(Attribute);
539 len += a->a_desc->ad_cname.bv_len+1;
540 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
541 for (i=0; a->a_vals[i].bv_val; i++) {
542 siz += sizeof(struct berval);
543 len += a->a_vals[i].bv_len + 1;
544 len += entry_lenlen(a->a_vals[i].bv_len);
546 len += entry_lenlen(i);
547 siz += sizeof(struct berval); /* empty berval at end */
549 len += 1; /* NUL byte at end */
550 len += entry_lenlen(siz);
552 bv->bv_val = ch_malloc(len);
553 ptr = (unsigned char *)bv->bv_val;
554 entry_putlen(&ptr, siz);
555 entry_putlen(&ptr, dnlen);
556 AC_MEMCPY(ptr, e->e_dn, dnlen);
559 entry_putlen(&ptr, ndnlen);
560 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
564 for (a=e->e_attrs; a; a=a->a_next) {
565 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
566 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
567 a->a_desc->ad_cname.bv_len);
568 ptr += a->a_desc->ad_cname.bv_len;
571 for (i=0; a->a_vals[i].bv_val; i++);
572 entry_putlen(&ptr, i);
573 for (i=0; a->a_vals[i].bv_val; i++) {
574 entry_putlen(&ptr, a->a_vals[i].bv_len);
575 memcpy(ptr, a->a_vals[i].bv_val,
576 a->a_vals[i].bv_len);
577 ptr += a->a_vals[i].bv_len;
586 /* Retrieve an Entry that was stored using entry_encode above.
587 * We malloc a single block with the size stored above for the Entry
588 * and all if its Attributes. We also must lookup the stored
589 * attribute names to get AttributeDescriptions. To detect if the
590 * attributes of an Entry are later modified, we note that e->e_attr
591 * is always a constant offset from (e).
593 * Note: everything is stored in a single contiguous block, so
594 * you can not free individual attributes or names from this
595 * structure. Attempting to do so will likely corrupt memory.
597 int entry_decode(struct berval *bv, Entry **e)
604 AttributeDescription *ad;
605 unsigned char *ptr = (unsigned char *)bv->bv_val;
608 i = entry_getlen(&ptr);
610 i = entry_getlen(&ptr);
611 x->e_name.bv_val = ptr;
612 x->e_name.bv_len = i;
614 i = entry_getlen(&ptr);
615 x->e_nname.bv_val = ptr;
616 x->e_nname.bv_len = i;
619 LDAP_LOG( OPERATION, DETAIL2, "entry_decode: \"%s\"\n", x->e_dn, 0, 0 );
621 Debug( LDAP_DEBUG_TRACE,
622 "entry_decode: \"%s\"\n",
627 /* A valid entry must have at least one attr, so this
628 * pointer can never be NULL
630 x->e_attrs = (Attribute *)(x+1);
631 bptr = (BerVarray)x->e_attrs;
634 while ((i = entry_getlen(&ptr))) {
639 a->a_next = (Attribute *)bptr;
641 a = (Attribute *)bptr;
643 rc = slap_bv2ad( &bv, &ad, &text );
645 if( rc != LDAP_SUCCESS ) {
647 LDAP_LOG( OPERATION, INFO,
648 "entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
650 Debug( LDAP_DEBUG_TRACE,
651 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
653 rc = slap_bv2undef_ad( &bv, &ad, &text );
655 if( rc != LDAP_SUCCESS ) {
657 LDAP_LOG( OPERATION, INFO,
658 "entry_decode: str2undef_ad(%s): %s\n", ptr, text, 0 );
660 Debug( LDAP_DEBUG_ANY,
661 "<= entry_decode: str2undef_ad(%s): %s\n",
669 bptr = (BerVarray)(a+1);
672 j = entry_getlen(&ptr);
675 i = entry_getlen(&ptr);
677 bptr->bv_val = (char *)ptr;
689 LDAP_LOG( OPERATION, DETAIL1, "entry_decode: %s\n", x->e_dn, 0, 0 );
691 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",