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, { 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 ) {
114 if ( e->e_dn != NULL ) {
116 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
117 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
118 (long) e->e_id, e->e_dn,
119 vals[0].bv_val != NULL ? vals[0].bv_val : "" ));
121 Debug( LDAP_DEBUG_ANY, "str2entry: "
122 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
123 (long) e->e_id, e->e_dn,
124 vals[0].bv_val != NULL ? vals[0].bv_val : "" );
126 if( vals[0].bv_val != NULL ) free( vals[0].bv_val );
131 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname );
132 if( rc != LDAP_SUCCESS ) {
134 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
135 "entry %ld has invalid DN \"%s\"\n",
136 (long) e->e_id, vals[0].bv_val ));
138 Debug( LDAP_DEBUG_ANY, "str2entry: "
139 "entry %ld has invalid DN \"%s\"\n",
140 (long) e->e_id, vals[0].bv_val, 0 );
143 free( vals[0].bv_val );
146 free( vals[0].bv_val );
151 rc = slap_str2ad( type, &ad, &text );
153 if( rc != LDAP_SUCCESS ) {
155 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
156 "str2entry: str2ad(%s): %s\n", type, text ));
158 Debug( slapMode & SLAP_TOOL_MODE
159 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
160 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
162 if( slapMode & SLAP_TOOL_MODE ) {
164 free( vals[0].bv_val );
169 rc = slap_str2undef_ad( type, &ad, &text );
170 if( rc != LDAP_SUCCESS ) {
172 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
173 "str2entry: str2undef_ad(%s): %s\n", type, text ));
175 Debug( LDAP_DEBUG_ANY,
176 "<= str2entry: str2undef_ad(%s): %s\n",
180 free( vals[0].bv_val );
186 if( slapMode & SLAP_TOOL_MODE ) {
188 slap_syntax_validate_func *validate =
189 ad->ad_type->sat_syntax->ssyn_validate;
190 slap_syntax_transform_func *pretty =
191 ad->ad_type->sat_syntax->ssyn_pretty;
194 rc = pretty( ad->ad_type->sat_syntax,
197 } else if( validate ) {
199 * validate value per syntax
201 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
205 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
206 "str2entry: no validator for syntax %s\n",
207 ad->ad_type->sat_syntax->ssyn_oid ));
209 Debug( LDAP_DEBUG_ANY,
210 "str2entry: no validator for syntax %s\n",
211 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
214 free( vals[0].bv_val );
221 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
222 "str2entry: invalid value for syntax %s\n",
223 ad->ad_type->sat_syntax->ssyn_oid ));
225 Debug( LDAP_DEBUG_ANY,
226 "str2entry: invalid value for syntax %s\n",
227 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
230 free( vals[0].bv_val );
236 free( vals[0].bv_val );
241 rc = attr_merge( e, ad, vals );
244 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
245 "str2entry: NULL (attr_merge)\n" ));
247 Debug( LDAP_DEBUG_ANY,
248 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
251 free( vals[0].bv_val );
257 free( vals[0].bv_val );
260 /* check to make sure there was a dn: line */
261 if ( e->e_dn == NULL ) {
263 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
264 "str2entry: entry %ld has no dn.\n",
267 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
268 (long) e->e_id, 0, 0 );
275 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
276 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
278 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
279 e->e_dn, (unsigned long) e, 0 );
285 #define GRABSIZE BUFSIZ
287 #define MAKE_SPACE( n ) { \
288 while ( ecur + (n) > ebuf + emaxsize ) { \
290 offset = (int) (ecur - ebuf); \
291 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
292 emaxsize + GRABSIZE ); \
293 emaxsize += GRABSIZE; \
294 ecur = ebuf + offset; \
311 * In string format, an entry looks like this:
313 * [<attr>: <value>\n]*
319 if ( e->e_dn != NULL ) {
321 tmplen = e->e_name.bv_len;
322 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
323 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
326 /* put the attributes */
327 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
328 /* put "<type>:[:] <value>" line for each value */
329 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
331 tmplen = a->a_desc->ad_cname.bv_len;
332 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
333 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
334 a->a_desc->ad_cname.bv_val,
335 bv->bv_val, bv->bv_len );
342 return( (char *) ebuf );
346 entry_free( Entry *e )
348 /* free an entry structure */
351 /* e_private must be freed by the caller */
352 assert( e->e_private == NULL );
356 if ( e->e_dn != NULL ) {
360 if ( e->e_ndn != NULL ) {
365 if ( e->e_bv.bv_val != NULL ) {
366 free( e->e_bv.bv_val );
367 e->e_bv.bv_val = NULL;
370 /* free attributes */
371 attrs_free( e->e_attrs );
378 * These routines are used only by Backend.
380 * the Entry has three entry points (ways to find things):
382 * by entry e.g., if you already have an entry from the cache
383 * and want to delete it. (really by entry ptr)
384 * by dn e.g., when looking for the base object of a search
385 * by id e.g., for search candidates
387 * these correspond to three different avl trees that are maintained.
391 entry_cmp( Entry *e1, Entry *e2 )
393 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
397 entry_dn_cmp( Entry *e1, Entry *e2 )
399 /* compare their normalized UPPERCASED dn's */
400 int rc = e1->e_nname.bv_len - e2->e_nname.bv_len;
402 return( strcmp( e1->e_ndn, e2->e_ndn ) );
406 entry_id_cmp( Entry *e1, Entry *e2 )
408 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
413 /* This is like a ber_len */
415 entry_lenlen(ber_len_t len)
429 entry_putlen(unsigned char **buf, ber_len_t len)
431 ber_len_t lenlen = entry_lenlen(len);
434 **buf = (unsigned char) len;
437 **buf = 0x80 | ((unsigned char) lenlen - 1);
438 for (i=lenlen-1; i>0; i--) {
439 (*buf)[i] = (unsigned char) len;
447 entry_getlen(unsigned char **buf)
464 /* Flatten an Entry into a buffer. The buffer is filled with just the
465 * strings/bervals of all the entry components. Each field is preceded
466 * by its length, encoded the way ber_put_len works. Every field is NUL
467 * terminated. The entire buffer size is precomputed so that a single
468 * malloc can be performed. The entry size is also recorded,
469 * to aid in entry_decode.
471 int entry_encode(Entry *e, struct berval *bv)
473 ber_len_t siz = sizeof(Entry);
474 ber_len_t len, dnlen, ndnlen;
480 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
481 "entry_encode: id: 0x%08lx \"%s\"\n",
482 (long) e->e_id, e->e_dn ));
484 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
485 (long) e->e_id, e->e_dn, 0 );
487 dnlen = e->e_name.bv_len;
488 ndnlen = e->e_nname.bv_len;
489 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
490 len += entry_lenlen(dnlen);
491 len += entry_lenlen(ndnlen);
492 for (a=e->e_attrs; a; a=a->a_next) {
493 /* For AttributeDesc, we only store the attr name */
494 siz += sizeof(Attribute);
495 len += a->a_desc->ad_cname.bv_len+1;
496 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
497 for (i=0; a->a_vals[i].bv_val; i++) {
498 siz += sizeof(struct berval);
499 len += a->a_vals[i].bv_len + 1;
500 len += entry_lenlen(a->a_vals[i].bv_len);
502 len += entry_lenlen(i);
503 siz += sizeof(struct berval); /* empty berval at end */
505 len += 1; /* NUL byte at end */
506 len += entry_lenlen(siz);
508 bv->bv_val = ch_malloc(len);
509 ptr = (unsigned char *)bv->bv_val;
510 entry_putlen(&ptr, siz);
511 entry_putlen(&ptr, dnlen);
512 AC_MEMCPY(ptr, e->e_dn, dnlen);
515 entry_putlen(&ptr, ndnlen);
516 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
520 for (a=e->e_attrs; a; a=a->a_next) {
521 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
522 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
523 a->a_desc->ad_cname.bv_len);
524 ptr += a->a_desc->ad_cname.bv_len;
527 for (i=0; a->a_vals[i].bv_val; i++);
528 entry_putlen(&ptr, i);
529 for (i=0; a->a_vals[i].bv_val; i++) {
530 entry_putlen(&ptr, a->a_vals[i].bv_len);
531 memcpy(ptr, a->a_vals[i].bv_val,
532 a->a_vals[i].bv_len);
533 ptr += a->a_vals[i].bv_len;
542 /* Retrieve an Entry that was stored using entry_encode above.
543 * We malloc a single block with the size stored above for the Entry
544 * and all if its Attributes. We also must lookup the stored
545 * attribute names to get AttributeDescriptions. To detect if the
546 * attributes of an Entry are later modified, we note that e->e_attr
547 * is always a constant offset from (e).
549 * Note: everything is stored in a single contiguous block, so
550 * you can not free individual attributes or names from this
551 * structure. Attempting to do so will likely corrupt memory.
553 int entry_decode(struct berval *bv, Entry **e)
560 AttributeDescription *ad;
561 unsigned char *ptr = (unsigned char *)bv->bv_val;
564 i = entry_getlen(&ptr);
566 i = entry_getlen(&ptr);
567 x->e_name.bv_val = ptr;
568 x->e_name.bv_len = i;
570 i = entry_getlen(&ptr);
571 x->e_nname.bv_val = ptr;
572 x->e_nname.bv_len = i;
575 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
576 "entry_decode: \"%s\"\n", x->e_dn ));
578 Debug( LDAP_DEBUG_TRACE,
579 "entry_decode: \"%s\"\n",
584 /* A valid entry must have at least one attr, so this
585 * pointer can never be NULL
587 x->e_attrs = (Attribute *)(x+1);
588 bptr = (BerVarray)x->e_attrs;
591 while ((i = entry_getlen(&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",