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 = { NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL };
29 int entry_destroy(void)
31 if ( ebuf ) free( ebuf );
45 struct berval vals[2];
47 struct berval nvals[2];
49 AttributeDescription *ad;
54 * LDIF is used as the string format.
55 * An entry looks like this:
58 * [<attr>:[:] <value>\n]
59 * [<tab><continuedvalue>\n]*
62 * If a double colon is used after a type, it means the
63 * following value is encoded as a base 64 string. This
64 * happens if the value contains a non-printing character
69 LDAP_LOG( OPERATION, DETAIL1, "str2entry: \"%s\"\n", s ? s : "NULL", 0, 0 );
71 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
72 s ? s : "NULL", 0, 0 );
75 /* initialize reader/writer lock */
76 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
80 LDAP_LOG( OPERATION, ERR, "str2entry: entry allocation failed.\n", 0, 0, 0 );
82 Debug( LDAP_DEBUG_ANY,
83 "<= str2entry NULL (entry allocation failed)\n",
89 /* initialize entry */
93 vals[1].bv_val = NULL;
96 while ( (s = ldif_getline( &next )) != NULL ) {
97 if ( *s == '\n' || *s == '\0' ) {
101 if ( ldif_parse_line( s, &type, &vals[0].bv_val, &vals[0].bv_len ) != 0 ) {
103 LDAP_LOG( OPERATION, DETAIL1, "str2entry: NULL (parse_line)\n",0, 0, 0 );
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, DETAIL1, "str2entry: "
117 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
118 (long) e->e_id, e->e_dn, vals[0].bv_val );
120 Debug( LDAP_DEBUG_ANY, "str2entry: "
121 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
122 (long) e->e_id, e->e_dn, vals[0].bv_val );
124 free( vals[0].bv_val );
129 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname );
130 if( rc != LDAP_SUCCESS ) {
132 LDAP_LOG( OPERATION, DETAIL1,
133 "str2entry: entry %ld has invalid DN \"%s\"\n",
134 (long) e->e_id, vals[0].bv_val, 0 );
136 Debug( LDAP_DEBUG_ANY, "str2entry: "
137 "entry %ld has invalid DN \"%s\"\n",
138 (long) e->e_id, vals[0].bv_val, 0 );
141 free( vals[0].bv_val );
144 free( vals[0].bv_val );
149 rc = slap_str2ad( type, &ad, &text );
151 if( rc != LDAP_SUCCESS ) {
153 LDAP_LOG( OPERATION, DETAIL1,
154 "str2entry: str2ad(%s): %s\n", type, text, 0 );
156 Debug( slapMode & SLAP_TOOL_MODE
157 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
158 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
160 if( slapMode & SLAP_TOOL_MODE ) {
162 free( vals[0].bv_val );
167 rc = slap_str2undef_ad( type, &ad, &text );
168 if( rc != LDAP_SUCCESS ) {
170 LDAP_LOG( OPERATION, DETAIL1,
171 "str2entry: str2undef_ad(%s): %s\n", type, text, 0 );
173 Debug( LDAP_DEBUG_ANY,
174 "<= str2entry: str2undef_ad(%s): %s\n",
178 free( vals[0].bv_val );
184 if( slapMode & SLAP_TOOL_MODE ) {
186 slap_syntax_validate_func *validate =
187 ad->ad_type->sat_syntax->ssyn_validate;
188 slap_syntax_transform_func *pretty =
189 ad->ad_type->sat_syntax->ssyn_pretty;
192 rc = pretty( ad->ad_type->sat_syntax,
195 } else if( validate ) {
197 * validate value per syntax
199 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
203 LDAP_LOG( OPERATION, INFO,
204 "str2entry: no validator for syntax %s\n",
205 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
207 Debug( LDAP_DEBUG_ANY,
208 "str2entry: no validator for syntax %s\n",
209 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
212 free( vals[0].bv_val );
219 LDAP_LOG( OPERATION, ERR,
220 "str2entry: invalid value for syntax %s\n",
221 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
223 Debug( LDAP_DEBUG_ANY,
224 "str2entry: invalid value for syntax %s\n",
225 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
228 free( vals[0].bv_val );
234 free( vals[0].bv_val );
243 rc = attr_merge( e, ad, vals
250 LDAP_LOG( OPERATION, DETAIL1,
251 "str2entry: NULL (attr_merge)\n" , 0, 0, 0 );
253 Debug( LDAP_DEBUG_ANY,
254 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
257 free( vals[0].bv_val );
263 free( vals[0].bv_val );
265 free( nvals[0].bv_val );
269 /* check to make sure there was a dn: line */
270 if ( e->e_dn == NULL ) {
272 LDAP_LOG( OPERATION, INFO,
273 "str2entry: entry %ld has no dn.\n", (long) e->e_id, 0, 0 );
275 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
276 (long) e->e_id, 0, 0 );
283 LDAP_LOG( OPERATION, DETAIL2,
284 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e, 0 );
286 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
287 e->e_dn, (unsigned long) e, 0 );
293 #define GRABSIZE BUFSIZ
295 #define MAKE_SPACE( n ) { \
296 while ( ecur + (n) > ebuf + emaxsize ) { \
298 offset = (int) (ecur - ebuf); \
299 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
300 emaxsize + GRABSIZE ); \
301 emaxsize += GRABSIZE; \
302 ecur = ebuf + offset; \
319 * In string format, an entry looks like this:
321 * [<attr>: <value>\n]*
327 if ( e->e_dn != NULL ) {
329 tmplen = e->e_name.bv_len;
330 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
331 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
334 /* put the attributes */
335 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
336 /* put "<type>:[:] <value>" line for each value */
337 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
339 tmplen = a->a_desc->ad_cname.bv_len;
340 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
341 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
342 a->a_desc->ad_cname.bv_val,
343 bv->bv_val, bv->bv_len );
350 return( (char *) ebuf );
354 entry_free( Entry *e )
356 /* free an entry structure */
359 /* e_private must be freed by the caller */
360 assert( e->e_private == NULL );
364 if ( e->e_dn != NULL ) {
368 if ( e->e_ndn != NULL ) {
373 if ( e->e_bv.bv_val != NULL ) {
374 free( e->e_bv.bv_val );
375 e->e_bv.bv_val = NULL;
378 /* free attributes */
379 attrs_free( e->e_attrs );
386 * These routines are used only by Backend.
388 * the Entry has three entry points (ways to find things):
390 * by entry e.g., if you already have an entry from the cache
391 * and want to delete it. (really by entry ptr)
392 * by dn e.g., when looking for the base object of a search
393 * by id e.g., for search candidates
395 * these correspond to three different avl trees that are maintained.
399 entry_cmp( Entry *e1, Entry *e2 )
401 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
405 entry_dn_cmp( const void *v_e1, const void *v_e2 )
407 /* compare their normalized UPPERCASED dn's */
408 const Entry *e1 = v_e1, *e2 = v_e2;
409 int rc = e1->e_nname.bv_len - e2->e_nname.bv_len;
411 return( strcmp( e1->e_ndn, e2->e_ndn ) );
415 entry_id_cmp( const void *v_e1, const void *v_e2 )
417 const Entry *e1 = v_e1, *e2 = v_e2;
418 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
423 /* This is like a ber_len */
425 entry_lenlen(ber_len_t len)
439 entry_putlen(unsigned char **buf, ber_len_t len)
441 ber_len_t lenlen = entry_lenlen(len);
444 **buf = (unsigned char) len;
447 **buf = 0x80 | ((unsigned char) lenlen - 1);
448 for (i=lenlen-1; i>0; i--) {
449 (*buf)[i] = (unsigned char) len;
457 entry_getlen(unsigned char **buf)
474 /* Flatten an Entry into a buffer. The buffer is filled with just the
475 * strings/bervals of all the entry components. Each field is preceded
476 * by its length, encoded the way ber_put_len works. Every field is NUL
477 * terminated. The entire buffer size is precomputed so that a single
478 * malloc can be performed. The entry size is also recorded,
479 * to aid in entry_decode.
481 int entry_encode(Entry *e, struct berval *bv)
483 ber_len_t siz = sizeof(Entry);
484 ber_len_t len, dnlen, ndnlen;
490 LDAP_LOG( OPERATION, DETAIL1, "entry_encode: id: 0x%08lx \"%s\"\n",
491 (long) e->e_id, e->e_dn, 0 );
493 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
494 (long) e->e_id, e->e_dn, 0 );
496 dnlen = e->e_name.bv_len;
497 ndnlen = e->e_nname.bv_len;
498 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
499 len += entry_lenlen(dnlen);
500 len += entry_lenlen(ndnlen);
501 for (a=e->e_attrs; a; a=a->a_next) {
502 /* For AttributeDesc, we only store the attr name */
503 siz += sizeof(Attribute);
504 len += a->a_desc->ad_cname.bv_len+1;
505 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
506 for (i=0; a->a_vals[i].bv_val; i++) {
507 siz += sizeof(struct berval);
508 len += a->a_vals[i].bv_len + 1;
509 len += entry_lenlen(a->a_vals[i].bv_len);
511 len += entry_lenlen(i);
512 siz += sizeof(struct berval); /* empty berval at end */
514 len += 1; /* NUL byte at end */
515 len += entry_lenlen(siz);
517 bv->bv_val = ch_malloc(len);
518 ptr = (unsigned char *)bv->bv_val;
519 entry_putlen(&ptr, siz);
520 entry_putlen(&ptr, dnlen);
521 AC_MEMCPY(ptr, e->e_dn, dnlen);
524 entry_putlen(&ptr, ndnlen);
525 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
529 for (a=e->e_attrs; a; a=a->a_next) {
530 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
531 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
532 a->a_desc->ad_cname.bv_len);
533 ptr += a->a_desc->ad_cname.bv_len;
536 for (i=0; a->a_vals[i].bv_val; i++);
537 entry_putlen(&ptr, i);
538 for (i=0; a->a_vals[i].bv_val; i++) {
539 entry_putlen(&ptr, a->a_vals[i].bv_len);
540 memcpy(ptr, a->a_vals[i].bv_val,
541 a->a_vals[i].bv_len);
542 ptr += a->a_vals[i].bv_len;
551 /* Retrieve an Entry that was stored using entry_encode above.
552 * We malloc a single block with the size stored above for the Entry
553 * and all if its Attributes. We also must lookup the stored
554 * attribute names to get AttributeDescriptions. To detect if the
555 * attributes of an Entry are later modified, we note that e->e_attr
556 * is always a constant offset from (e).
558 * Note: everything is stored in a single contiguous block, so
559 * you can not free individual attributes or names from this
560 * structure. Attempting to do so will likely corrupt memory.
562 int entry_decode(struct berval *bv, Entry **e)
569 AttributeDescription *ad;
570 unsigned char *ptr = (unsigned char *)bv->bv_val;
573 i = entry_getlen(&ptr);
575 i = entry_getlen(&ptr);
576 x->e_name.bv_val = ptr;
577 x->e_name.bv_len = i;
579 i = entry_getlen(&ptr);
580 x->e_nname.bv_val = ptr;
581 x->e_nname.bv_len = i;
584 LDAP_LOG( OPERATION, DETAIL2, "entry_decode: \"%s\"\n", x->e_dn, 0, 0 );
586 Debug( LDAP_DEBUG_TRACE,
587 "entry_decode: \"%s\"\n",
592 /* A valid entry must have at least one attr, so this
593 * pointer can never be NULL
595 x->e_attrs = (Attribute *)(x+1);
596 bptr = (BerVarray)x->e_attrs;
599 while ((i = entry_getlen(&ptr))) {
604 a->a_next = (Attribute *)bptr;
606 a = (Attribute *)bptr;
608 rc = slap_bv2ad( &bv, &ad, &text );
610 if( rc != LDAP_SUCCESS ) {
612 LDAP_LOG( OPERATION, INFO,
613 "entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
615 Debug( LDAP_DEBUG_TRACE,
616 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
618 rc = slap_bv2undef_ad( &bv, &ad, &text );
620 if( rc != LDAP_SUCCESS ) {
622 LDAP_LOG( OPERATION, INFO,
623 "entry_decode: str2undef_ad(%s): %s\n", ptr, text, 0 );
625 Debug( LDAP_DEBUG_ANY,
626 "<= entry_decode: str2undef_ad(%s): %s\n",
634 bptr = (BerVarray)(a+1);
637 j = entry_getlen(&ptr);
640 i = entry_getlen(&ptr);
642 bptr->bv_val = (char *)ptr;
654 LDAP_LOG( OPERATION, DETAIL1, "entry_decode: %s\n", x->e_dn, 0, 0 );
656 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",