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; \
309 * In string format, an entry looks like this:
311 * [<attr>: <value>\n]*
317 if ( e->e_dn != NULL ) {
319 tmplen = e->e_name.bv_len;
320 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
321 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
324 /* put the attributes */
325 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
326 /* put "<type>:[:] <value>" line for each value */
327 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
329 tmplen = a->a_desc->ad_cname.bv_len;
330 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
331 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
332 a->a_desc->ad_cname.bv_val,
333 bv->bv_val, bv->bv_len );
340 return( (char *) ebuf );
344 entry_free( Entry *e )
346 /* free an entry structure */
349 /* e_private must be freed by the caller */
350 assert( e->e_private == NULL );
354 if ( e->e_dn != NULL ) {
358 if ( e->e_ndn != NULL ) {
363 if ( e->e_bv.bv_val != NULL ) {
364 free( e->e_bv.bv_val );
365 e->e_bv.bv_val = NULL;
368 /* free attributes */
369 attrs_free( e->e_attrs );
376 * These routines are used only by Backend.
378 * the Entry has three entry points (ways to find things):
380 * by entry e.g., if you already have an entry from the cache
381 * and want to delete it. (really by entry ptr)
382 * by dn e.g., when looking for the base object of a search
383 * by id e.g., for search candidates
385 * these correspond to three different avl trees that are maintained.
389 entry_cmp( Entry *e1, Entry *e2 )
391 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
395 entry_dn_cmp( Entry *e1, Entry *e2 )
397 /* compare their normalized UPPERCASED dn's */
398 int rc = e1->e_nname.bv_len - e2->e_nname.bv_len;
400 return( strcmp( e1->e_ndn, e2->e_ndn ) );
404 entry_id_cmp( Entry *e1, Entry *e2 )
406 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
411 /* This is like a ber_len */
413 entry_lenlen(ber_len_t len)
427 entry_putlen(unsigned char **buf, ber_len_t len)
429 ber_len_t lenlen = entry_lenlen(len);
432 **buf = (unsigned char) len;
435 **buf = 0x80 | ((unsigned char) lenlen - 1);
436 for (i=lenlen-1; i>0; i--) {
437 (*buf)[i] = (unsigned char) len;
445 entry_getlen(unsigned char **buf)
462 /* Flatten an Entry into a buffer. The buffer is filled with just the
463 * strings/bervals of all the entry components. Each field is preceded
464 * by its length, encoded the way ber_put_len works. Every field is NUL
465 * terminated. The entire buffer size is precomputed so that a single
466 * malloc can be performed. The entry size is also recorded,
467 * to aid in entry_decode.
469 int entry_encode(Entry *e, struct berval *bv)
471 ber_len_t siz = sizeof(Entry);
472 ber_len_t len, dnlen, ndnlen;
478 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
479 "entry_encode: id: 0x%08lx \"%s\"\n",
480 (long) e->e_id, e->e_dn ));
482 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
483 (long) e->e_id, e->e_dn, 0 );
485 dnlen = e->e_name.bv_len;
486 ndnlen = e->e_nname.bv_len;
487 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
488 len += entry_lenlen(dnlen);
489 len += entry_lenlen(ndnlen);
490 for (a=e->e_attrs; a; a=a->a_next) {
491 /* For AttributeDesc, we only store the attr name */
492 siz += sizeof(Attribute);
493 len += a->a_desc->ad_cname.bv_len+1;
494 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
495 for (i=0; a->a_vals[i].bv_val; i++) {
496 siz += sizeof(struct berval);
497 len += a->a_vals[i].bv_len + 1;
498 len += entry_lenlen(a->a_vals[i].bv_len);
500 len += entry_lenlen(i);
501 siz += sizeof(struct berval); /* empty berval at end */
503 len += 1; /* NUL byte at end */
504 len += entry_lenlen(siz);
506 bv->bv_val = ch_malloc(len);
507 ptr = (unsigned char *)bv->bv_val;
508 entry_putlen(&ptr, siz);
509 entry_putlen(&ptr, dnlen);
510 AC_MEMCPY(ptr, e->e_dn, dnlen);
513 entry_putlen(&ptr, ndnlen);
514 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
518 for (a=e->e_attrs; a; a=a->a_next) {
519 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
520 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
521 a->a_desc->ad_cname.bv_len);
522 ptr += a->a_desc->ad_cname.bv_len;
525 for (i=0; a->a_vals[i].bv_val; i++);
526 entry_putlen(&ptr, i);
527 for (i=0; a->a_vals[i].bv_val; i++) {
528 entry_putlen(&ptr, a->a_vals[i].bv_len);
529 memcpy(ptr, a->a_vals[i].bv_val,
530 a->a_vals[i].bv_len);
531 ptr += a->a_vals[i].bv_len;
540 /* Retrieve an Entry that was stored using entry_encode above.
541 * We malloc a single block with the size stored above for the Entry
542 * and all if its Attributes. We also must lookup the stored
543 * attribute names to get AttributeDescriptions. To detect if the
544 * attributes of an Entry are later modified, we note that e->e_attr
545 * is always a constant offset from (e).
547 * Note: everything is stored in a single contiguous block, so
548 * you can not free individual attributes or names from this
549 * structure. Attempting to do so will likely corrupt memory.
551 int entry_decode(struct berval *bv, Entry **e)
558 AttributeDescription *ad;
559 unsigned char *ptr = (unsigned char *)bv->bv_val;
562 i = entry_getlen(&ptr);
564 i = entry_getlen(&ptr);
565 x->e_name.bv_val = ptr;
566 x->e_name.bv_len = i;
568 i = entry_getlen(&ptr);
569 x->e_nname.bv_val = ptr;
570 x->e_nname.bv_len = i;
573 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
574 "entry_decode: \"%s\"\n", x->e_dn ));
576 Debug( LDAP_DEBUG_TRACE,
577 "entry_decode: \"%s\"\n",
582 /* A valid entry must have at least one attr, so this
583 * pointer can never be NULL
585 x->e_attrs = (Attribute *)(x+1);
586 bptr = (BerVarray)x->e_attrs;
589 while ((i = entry_getlen(&ptr))) {
594 a->a_next = (Attribute *)bptr;
596 a = (Attribute *)bptr;
598 rc = slap_bv2ad( &bv, &ad, &text );
600 if( rc != LDAP_SUCCESS ) {
602 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
603 "entry_decode: str2ad(%s): %s\n", ptr, text ));
605 Debug( LDAP_DEBUG_TRACE,
606 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
608 rc = slap_bv2undef_ad( &bv, &ad, &text );
610 if( rc != LDAP_SUCCESS ) {
612 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
613 "entry_decode: str2undef_ad(%s): %s\n", ptr, text));
615 Debug( LDAP_DEBUG_ANY,
616 "<= entry_decode: str2undef_ad(%s): %s\n",
624 bptr = (BerVarray)(a+1);
627 j = entry_getlen(&ptr);
630 i = entry_getlen(&ptr);
632 bptr->bv_val = (char *)ptr;
644 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
645 "entry_decode: %s\n", x->e_dn ));
647 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",